7017
|
1 ## Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006, 2007 Paul Kienzle |
3980
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
3980
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3980
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Mapping Function} {} mod (@var{x}, @var{y}) |
|
21 ## Compute modulo function, using |
|
22 ## |
|
23 ## @example |
|
24 ## x - y .* floor (x ./ y) |
|
25 ## @end example |
|
26 ## |
|
27 ## Note that this handles negative numbers correctly: |
|
28 ## @code{mod (-1, 3)} is 2, not -1 as @code{rem (-1, 3)} returns. |
|
29 ## Also, @code{mod (@var{x}, 0)} returns @var{x}. |
|
30 ## |
|
31 ## An error message is printed if the dimensions of the arguments do not |
|
32 ## agree, or if either of the arguments is complex. |
5642
|
33 ## @seealso{rem, round} |
3980
|
34 ## @end deftypefn |
|
35 |
|
36 ## Author: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
|
37 ## Modified by: Teemu Ikonen <tpikonen@pcu.helsinki.fi> |
|
38 ## Adapted by: jwe |
|
39 |
|
40 function r = mod (x, y) |
|
41 |
|
42 if (nargin != 2) |
6046
|
43 print_usage (); |
3980
|
44 endif |
|
45 |
6157
|
46 if (! size_equal (x, y) && ! (isscalar (x) || isscalar (y))) |
3980
|
47 error ("mod: argument sizes must agree"); |
|
48 endif |
|
49 |
|
50 ## Matlab allows complex arguments, but as far as I can tell, that's a |
|
51 ## bunch of hooey. |
|
52 |
4027
|
53 if (isreal (x) && isreal (y)) |
|
54 nz = y != 0.0; |
|
55 if (all (nz(:))) |
|
56 ## No elements of y are zero. |
|
57 r = x - y .* floor (x ./ y); |
4030
|
58 elseif (isscalar (y)) |
4027
|
59 ## y must be zero. |
|
60 r = x; |
|
61 else |
|
62 ## Some elements of y are zero. |
4030
|
63 if (isscalar (x)) |
4027
|
64 r = x * ones (size (y)); |
|
65 else |
|
66 r = x; |
|
67 x = x(nz); |
|
68 endif |
|
69 y = y(nz); |
|
70 r(nz) = x - y .* floor (x ./ y); |
|
71 endif |
|
72 else |
3980
|
73 error ("mod: complex arguments are not allowed"); |
|
74 endif |
|
75 |
|
76 endfunction |
|
77 |
|
78 ## empty input test |
|
79 %!assert (isempty(mod([], []))); |
|
80 |
|
81 ## x mod y, y != 0 tests |
|
82 %!assert (mod(5, 3), 2); |
|
83 %!assert (mod(-5, 3), 1); |
|
84 %!assert (mod(0, 3), 0); |
|
85 %!assert (mod([-5, 5, 0], [3, 3, 3]), [1, 2, 0]); |
|
86 %!assert (mod([-5; 5; 0], [3; 3; 3]), [1; 2; 0]); |
|
87 %!assert (mod([-5, 5; 0, 3], [3, 3 ; 3, 1]), [1, 2 ; 0, 0]); |
|
88 |
|
89 ## x mod 0 tests |
|
90 %!assert (mod(5, 0), 5); |
|
91 %!assert (mod(-5, 0), -5); |
|
92 %!assert (mod([-5, 5, 0], [3, 0, 3]), [1, 5, 0]); |
|
93 %!assert (mod([-5; 5; 0], [3; 0; 3]), [1; 5; 0]); |
|
94 %!assert (mod([-5, 5; 0, 3], [3, 0 ; 3, 1]), [1, 5 ; 0, 0]); |
|
95 %!assert (mod([-5, 5; 0, 3], [0, 0 ; 0, 0]), [-5, 5; 0, 3]); |
|
96 |
|
97 ## mixed scalar/matrix tests |
|
98 %!assert (mod([-5, 5; 0, 3], 0), [-5, 5; 0, 3]); |
|
99 %!assert (mod([-5, 5; 0, 3], 3), [1, 2; 0, 0]); |
|
100 %!assert (mod(-5,[0,0; 0,0]), [-5, -5; -5, -5]); |
|
101 %!assert (mod(-5,[3,0; 3,1]), [1, -5; 1, 0]); |
|
102 %!assert (mod(-5,[3,2; 3,1]), [1, 1; 1, 0]); |
|
103 |