[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Double problem in Matlab's magic.m
From: |
Quentin Spencer |
Subject: |
Re: Double problem in Matlab's magic.m |
Date: |
Fri, 09 May 2003 15:50:02 +0200 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.3) Gecko/20030430 Debian/1.3-5 |
Henry F. Mollet wrote:
Presumably, 'mod' is modulus function but it's not in the GNU Octave
Function Index (Hard Copy for Octave version 2.0.13) and help cannot find
it in my copy either (GNU Octave, version 2.1.35 (powerpc-apple-darwin6.2).
However, the Octave program 'magic' by PK is using it?
Henry
The mod function was in octave-forge, but has been moved into the main
octave distribution sometime after 2.1.35. Here's the current CVS
version (minus some commented stuff at the end):
## Copyright (C) 1999, 2000 Paul Kienzle
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING. If not, write to the Free
## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
## 02111-1307, USA.
## -*- texinfo -*-
## @deftypefn {Mapping Function} {} mod (@var{x}, @var{y})
## Compute modulo function, using
##
## @example
## x - y .* floor (x ./ y)
## @end example
##
## Note that this handles negative numbers correctly:
## @code{mod (-1, 3)} is 2, not -1 as @code{rem (-1, 3)} returns.
## Also, @code{mod (@var{x}, 0)} returns @var{x}.
##
## An error message is printed if the dimensions of the arguments do not
## agree, or if either of the arguments is complex.
## @end deftypefn
## @seealso{rem, round}
## Author: Paul Kienzle <address@hidden>
## Modified by: Teemu Ikonen <address@hidden>
## Adapted by: jwe
function r = mod (x, y)
if (nargin != 2)
usage ("r = mod (x, y)");
endif
if (any (size (x) != size (y)) && ! (isscalar (x) || isscalar (y)))
error ("mod: argument sizes must agree");
endif
## Matlab allows complex arguments, but as far as I can tell, that's a
## bunch of hooey.
if (isreal (x) && isreal (y))
nz = y != 0.0;
if (all (nz(:)))
## No elements of y are zero.
r = x - y .* floor (x ./ y);
elseif (isscalar (y))
## y must be zero.
r = x;
else
## Some elements of y are zero.
if (isscalar (x))
r = x * ones (size (y));
else
r = x;
x = x(nz);
endif
y = y(nz);
r(nz) = x - y .* floor (x ./ y);
endif
else
error ("mod: complex arguments are not allowed");
endif
endfunction
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------