3980
|
1 ## Copyright (C) 1999, 2000 Paul Kienzle |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
3980
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Mapping Function} {} mod (@var{x}, @var{y}) |
|
22 ## Compute modulo function, using |
|
23 ## |
|
24 ## @example |
|
25 ## x - y .* floor (x ./ y) |
|
26 ## @end example |
|
27 ## |
|
28 ## Note that this handles negative numbers correctly: |
|
29 ## @code{mod (-1, 3)} is 2, not -1 as @code{rem (-1, 3)} returns. |
|
30 ## Also, @code{mod (@var{x}, 0)} returns @var{x}. |
|
31 ## |
|
32 ## An error message is printed if the dimensions of the arguments do not |
|
33 ## agree, or if either of the arguments is complex. |
5642
|
34 ## @seealso{rem, round} |
3980
|
35 ## @end deftypefn |
|
36 |
|
37 ## Author: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
|
38 ## Modified by: Teemu Ikonen <tpikonen@pcu.helsinki.fi> |
|
39 ## Adapted by: jwe |
|
40 |
|
41 function r = mod (x, y) |
|
42 |
|
43 if (nargin != 2) |
|
44 usage ("r = mod (x, y)"); |
|
45 endif |
|
46 |
4844
|
47 if (((ndims (x) != ndims (y)) || any (size (x) != size (y))) && |
|
48 ! (isscalar (x) || isscalar (y))) |
3980
|
49 error ("mod: argument sizes must agree"); |
|
50 endif |
|
51 |
|
52 ## Matlab allows complex arguments, but as far as I can tell, that's a |
|
53 ## bunch of hooey. |
|
54 |
4027
|
55 if (isreal (x) && isreal (y)) |
|
56 nz = y != 0.0; |
|
57 if (all (nz(:))) |
|
58 ## No elements of y are zero. |
|
59 r = x - y .* floor (x ./ y); |
4030
|
60 elseif (isscalar (y)) |
4027
|
61 ## y must be zero. |
|
62 r = x; |
|
63 else |
|
64 ## Some elements of y are zero. |
4030
|
65 if (isscalar (x)) |
4027
|
66 r = x * ones (size (y)); |
|
67 else |
|
68 r = x; |
|
69 x = x(nz); |
|
70 endif |
|
71 y = y(nz); |
|
72 r(nz) = x - y .* floor (x ./ y); |
|
73 endif |
|
74 else |
3980
|
75 error ("mod: complex arguments are not allowed"); |
|
76 endif |
|
77 |
|
78 endfunction |
|
79 |
|
80 ## empty input test |
|
81 %!assert (isempty(mod([], []))); |
|
82 |
|
83 ## x mod y, y != 0 tests |
|
84 %!assert (mod(5, 3), 2); |
|
85 %!assert (mod(-5, 3), 1); |
|
86 %!assert (mod(0, 3), 0); |
|
87 %!assert (mod([-5, 5, 0], [3, 3, 3]), [1, 2, 0]); |
|
88 %!assert (mod([-5; 5; 0], [3; 3; 3]), [1; 2; 0]); |
|
89 %!assert (mod([-5, 5; 0, 3], [3, 3 ; 3, 1]), [1, 2 ; 0, 0]); |
|
90 |
|
91 ## x mod 0 tests |
|
92 %!assert (mod(5, 0), 5); |
|
93 %!assert (mod(-5, 0), -5); |
|
94 %!assert (mod([-5, 5, 0], [3, 0, 3]), [1, 5, 0]); |
|
95 %!assert (mod([-5; 5; 0], [3; 0; 3]), [1; 5; 0]); |
|
96 %!assert (mod([-5, 5; 0, 3], [3, 0 ; 3, 1]), [1, 5 ; 0, 0]); |
|
97 %!assert (mod([-5, 5; 0, 3], [0, 0 ; 0, 0]), [-5, 5; 0, 3]); |
|
98 |
|
99 ## mixed scalar/matrix tests |
|
100 %!assert (mod([-5, 5; 0, 3], 0), [-5, 5; 0, 3]); |
|
101 %!assert (mod([-5, 5; 0, 3], 3), [1, 2; 0, 0]); |
|
102 %!assert (mod(-5,[0,0; 0,0]), [-5, -5; -5, -5]); |
|
103 %!assert (mod(-5,[3,0; 3,1]), [1, -5; 1, 0]); |
|
104 %!assert (mod(-5,[3,2; 3,1]), [1, 1; 1, 0]); |
|
105 |