summaryrefslogtreecommitdiff
path: root/modules/compatibility_functions/macros/mtlb_min.sci
blob: 6266ef24195500a0a7de8d5650b1108ed202b398 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS
//
// 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 [r,k]=mtlb_min(a,b,c)
    // Copyright INRIA
    // Emulation function for min() Matlab function
    // V.C.

    [lhs,rhs]=argn(0);

    if rhs==2 then
        if type(b)==10 then
            rhs=1
        end
    end

    if rhs==1 then //min(A)
        // One output arg
        if lhs==1 then
            if ~isreal(a,0) then
                if size(a,1)==1|size(a,2)==1 then
                    [r,k]=min(abs(a))
                    r=a(k)
                else
                    [r,k]=min(abs(a),"r")
                    r=[]
                    for l=1:size(a,2)
                        r=[r,a(k(l),l)]
                    end
                end
            else
                if size(a,1)==1|size(a,2)==1 then
                    r=min(a)
                else
                    r=min(a,"r")
                end
            end
        else
            if ~isreal(a,0) then
                if size(a,1)==1|size(a,2)==1 then
                    [r,k]=min(abs(a))
                    r=a(k)
                else
                    [r,k]=min(abs(a),"r")
                    r=[]
                    for l=1:size(a,2)
                        r=[r,a(k(l),l)]
                    end
                end
            else
                if size(a,1)==1|size(a,2)==1 then
                    [r,k]=min(a)
                else
                    [r,k]=min(a,"r")
                end
            end
        end
        // min(A,B) A and B have the same size
    elseif rhs==2 then
        // Only one output arg is possible...
        if lhs > 1 then
            error(msprintf(gettext("%s: Wrong number of output argument(s): %d expected.\n"),"mtlb_min", 1));
        end

        // Special case if A==[] and B==[]
        if isempty(a) | isempty(b) then
            r=[]
            return
        end

        if ~isreal(a)|~isreal(b) then
            if ~isreal(a,0)&isreal(b,0) then
                [r,k]=min(abs(a),b)
                for m=1:size(a,1)
                    for n=1:size(a,2)
                        if k(m,n)==1 then
                            r(m,n)=a(m,n)
                        end
                    end
                end
            elseif isreal(a,0)&~isreal(b,0) then
                [r,k]=min(a,abs(b))
                for m=1:size(a,1)
                    for n=1:size(a,2)
                        if k(m,n)==2 then
                            r(m,n)=b(m,n)
                        end
                    end
                end
            else
                [r,k]=min(abs(a),abs(b))
                for m=1:size(a,1)
                    for n=1:size(a,2)
                        if k(m,n)==1 then
                            r(m,n)=a(m,n)
                        else
                            r(m,n)=b(m,n)
                        end
                    end
                end
            end
        else
            r = min(a,b)
        end
        // min(a,[],num) with num>2 or num is a variable name
    elseif rhs==3 then
        //If num>number of dims of a
        if c>size(size(a),2) then
            r=a
            k=ones(size(a,1),size(a,2))
        else
            if c==1 then
                c="r"
            else
                c="c"
            end
            if isreal(a) then
                // a is a complex value
                [r,k]=min(a,c)
            else
                [r,k]=min(abs(a),c)
                // Replace values in r by original values of a
                if find(size(k)<>1)==[] then //Scalar
                    r=a(k)
                elseif size(k,1)==1 then // Row Vector
                    r=[]
                    for l=1:size(k,"*")
                        r=[r,a(k(l),l)]
                    end
                elseif size(k,2)==1 then // Column Vector
                    r=[]
                    for l=1:size(k,"*")
                        r=[r;a(l,k(l))]
                    end
                else
                    r=[]
                    for m=1:size(k,1)
                        tmp=[]
                        for n=1:size(k,2)
                            tmp=[tmp,a(k(m,n),n)];
                        end
                        r=[r;tmp]
                    end
                end
            end
        end
    end
endfunction