summaryrefslogtreecommitdiff
path: root/modules/elementary_functions/macros/modulo.sci
blob: 0e65511d4d9ecd6f738f5a75333846b31e36a0e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) INRIA
// Copyright (C) DIGITEO - 2011 - Allan CORNET
// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS
// Copyright (C) 2013 - Samuel GOUGEON : Bug 13002 : extension to hypermatrices & integers
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt

function i = modulo(n, m)

    [lhs, rhs] = argn(0);
    if rhs <> 2 then
        msg = _("%s: Wrong number of input argument(s): %d expected.\n")
        error(msprintf(msg, "modulo", 2))
    end

    mt = type(m($))
    nt = type(n($))
    // -----------------------  Checking arguments --------------------------

    if or(type(n)==[15 16]) | and(nt <> [1 2 8]) | (nt==1 & ~isreal(n))
        msg = _("%s: Wrong type for input argument #%d: Real, integer or polynomial matrix expected.\n")
        error(msprintf(msg, "modulo", 1))
    end

    if or(type(m)==[15 16]) |  and(mt <> [1 2 8]) | (mt==1 & ~isreal(m))
        msg = _("%s: Wrong type for input argument #%d: Real, integer or polynomial matrix expected.\n")
        error(msprintf(msg, "modulo", 2))
    end

    if (nt==8 | mt==8)  & nt~=mt
        msg = _("%s: Incompatible input arguments #%d and #%d: Same types expected.\n")
        error(msprintf(msg, "modulo", 1, 2))
    end

    // --------------------------  Processing ----------------------------

    if or(mt==[1 8]) & mt==nt then
        ms = size(m)
        ns = size(n)
        m = m(:)
        n = n(:)
        if length(n)>1 & length(m)>1 & or(ns <> ms) then
            msg = _("%s: Wrong size for input arguments: Same size expected.\n")
            error(msprintf(msg, "modulo"))
        end
        i = n - int(n ./ m) .* m
        i = iconvert(i, inttype(n))
        if length(m)>1 then
            s = ms
        else
            s = ns
        end
        i = matrix(i, s)
    else
        [i,q] = pdiv(n, m)
    end

endfunction