~ubuntu-branches/ubuntu/raring/scilab/raring-proposed

« back to all changes in this revision

Viewing changes to modules/elementary_functions/macros/%sp_min.sci

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2012-08-30 14:42:38 UTC
  • mfrom: (1.4.7)
  • Revision ID: package-import@ubuntu.com-20120830144238-c1y2og7dbm7m9nig
Tags: 5.4.0-beta-3-1~exp1
* New upstream release
* Update the scirenderer dep
* Get ride of libjhdf5-java dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 
2
// Copyright (C) 2010 - Scilab Enterprises - Adeline CARNIS
 
3
// 
 
4
// This file must be used under the terms of the CeCILL.
 
5
// This source file is licensed as described in the file COPYING, which
 
6
// you should have received as part of this distribution.  The terms
 
7
// are also available at    
 
8
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 
9
 
 
10
function [m, k] = %sp_min(varargin)
 
11
    [lhs, rhs] = argn(0);
 
12
 
 
13
    elements = varargin;
 
14
    error_list = %f;
 
15
 
 
16
    // If the first argument is a list, it retrieves the number of sparse 
 
17
    // matrices in list
 
18
    if type(varargin(1)) == 15 then
 
19
        if rhs <> 1 then
 
20
            error(msprintf(_("%s: Wrong size of input argument: %d expected.\n"), "%sp_min", 1))
 
21
        end
 
22
 
 
23
        rhs = length(varargin(1));
 
24
        elements = varargin(1);
 
25
        error_list = %t; 
 
26
 
 
27
        // If the second argument of list is not a sparse -> ERROR
 
28
        if rhs == 2 & type(elements(2)) <> 5 then
 
29
            error(msprintf(_("%s: Wrong type for input argument #%d: A sparse matrix expected.\n"), "%sp_min", 1))
 
30
        end
 
31
    end
 
32
 
 
33
    select rhs
 
34
        // If rhs = 1, the first input argument must be a sparse
 
35
    case 1
 
36
        A1 = elements(1);
 
37
 
 
38
        // Check if A is a sparse
 
39
        if type(A1)<> 5 then
 
40
            error(msprintf(_("%s: Wrong type for input argument #%d: A sparse matrix expected.\n"), "%sp_min", 1))
 
41
        end
 
42
 
 
43
        // Retrieves entries of sparse matrix
 
44
        [ij, v, mn]= spget(A1);
 
45
        if lhs == 1 then
 
46
            if v == [] then
 
47
                m = 0;
 
48
            else
 
49
                m = min(v);
 
50
            end
 
51
        else
 
52
            if v == [] then
 
53
                m = 0;
 
54
                k = [1 1];
 
55
            else
 
56
                [m, i] = min(v);
 
57
                if mn(1) == 1 then
 
58
                    k = ij(i,2);
 
59
                else
 
60
                    if mn(2) == 1 then
 
61
                        k = ij(i,1);
 
62
                    else
 
63
                        k = [ij(i,1) ij(i,2)];
 
64
                    end
 
65
                end
 
66
            end
 
67
        end
 
68
 
 
69
        // If A1 contains positive values and the length of v is less then mn(1)
 
70
        // * mn(2), A1 contains one or several zeros
 
71
        // So, min = 0
 
72
        if m > 0 & length(v)<> mn(1)*mn(2) then
 
73
            m = 0;
 
74
            if lhs == 2 then
 
75
                k = 0;
 
76
                if mn(1) == 1 then
 
77
                    for i = 1:length(v)
 
78
                        if and(ij(:,2) <> i) then
 
79
                            k = i;
 
80
                            break;
 
81
                        end
 
82
                    end
 
83
                else
 
84
                    if mn(2) == 1 then
 
85
                        for i = 1:length(v)
 
86
                            if and(ij(:,1) <> i) then
 
87
                                k = i;
 
88
                                break;
 
89
                            end
 
90
                        end
 
91
                    else
 
92
                        // thanks to ij, find the position of zero value
 
93
                        // But ij contains only the indices of non zero values
 
94
                        // So check on first column the values 1:mn(1)
 
95
                        // if posi is less than mn(2) then there is zero value
 
96
                        for i = 1:mn(1)
 
97
                            posi = length(find(ij(:,1)==i));
 
98
                            if posi <> mn(2) then
 
99
                                for j = 1:mn(2)
 
100
                                    // it is the last index of line where m = 0
 
101
                                    if mn(2)*(i-1)+j ==  mn(2)*i then
 
102
                                        k = [i, j];
 
103
                                        break;
 
104
                                    else
 
105
                                        pos = find(ij(mn(2)*(i-1)+j,2) == j);
 
106
                                        if pos == [] & k == 0 then
 
107
                                            k = [i,j];
 
108
                                            break;
 
109
                                        end
 
110
                                    end
 
111
                                end
 
112
                                if k <> 0 then
 
113
                                    break;
 
114
                                end
 
115
                            end
 
116
                        end
 
117
                    end
 
118
                end
 
119
            end
 
120
        end
 
121
 
 
122
        // If rhs = 2, the second input argument can be a character or a sparse
 
123
        // matrix
 
124
    case 2
 
125
        if lhs == 2 then
 
126
            error(msprintf(_("%s: Wrong number of output argument: %d expected"), "%sp_min", 1));
 
127
        end
 
128
 
 
129
        A1 = elements(1);
 
130
 
 
131
        // Check if A is a sparse
 
132
        if type(A1) <> 5  then
 
133
            error(msprintf(_("%s: Wrong type for input argument #%d: A sparse matrix expected.\n"), "%sp_min", 1));
 
134
        end
 
135
 
 
136
        select type(elements(2))
 
137
            // If the second argument is a string
 
138
        case 10
 
139
            opts = elements(2);
 
140
            // Opts can be : 'c' 'r' or 'm'
 
141
            ind = find(opts == ['c','r','m']);
 
142
            if (ind == []) then
 
143
                error(msprintf(_("%s: Wrong value for input argument #%d: [''r'' ''c'' ''m''] expected.\n"),"%sp_min",2));
 
144
            end
 
145
 
 
146
            [ij, v, mn] = spget(A1);
 
147
 
 
148
            // If mn(1) = 1, A1 is a row vector
 
149
            if mn(1) == 1 then
 
150
                // min(A1, 'r') = A1(1,:) because A1 is a row vector
 
151
                if opts == 'r' then
 
152
                    m = A1;
 
153
                    // min(A1, 'c') or min(A1, 'm') = min(A1)
 
154
                else
 
155
                    m = min(v', opts);
 
156
                    if m > 0 & length(v)<> mn(1)*mn(2) then
 
157
                        m = 0;
 
158
                    end
 
159
                end
 
160
            end
 
161
 
 
162
            // If mn(2) = 1, A1 is a column vector
 
163
            if mn(2) == 1 then
 
164
                if opts == 'c' then
 
165
                    m = A1;
 
166
                else
 
167
                    m = min(v, opts);
 
168
                end
 
169
                if m > 0 & length(v)<> mn(1)*mn(2) then
 
170
                    m = 0;
 
171
                end
 
172
            end
 
173
 
 
174
            // Return a sparse vector containing the max in terms of 'c', 'r' or 'm'
 
175
            if mn(1) <> 1 & mn(2) <> 1 then
 
176
                m = spzeros(mn(1),1);
 
177
                // If opts = 'c', the result is returned in column vector
 
178
                if opts == 'c' then
 
179
                    m = spzeros(mn(1),1);
 
180
                    for i = 1:mn(1)
 
181
                        pos = length(find(ij(:,1)==i));
 
182
                        if pos <> mn(2) then
 
183
                            m(i) = 0;
 
184
                        else
 
185
                            m(i) = min(A1(i,:));
 
186
                        end
 
187
                    end
 
188
                    // If opts = 'r' or 'm', the result is returned in row vector
 
189
                else
 
190
                    m = spzeros(1,mn(2));
 
191
                    for i = 1:mn(2)
 
192
                        pos = length(find(ij(:,2)==i));
 
193
                        if pos <> mn(1) then
 
194
                            m(i) = 0;
 
195
                        else
 
196
                            m(i) = min(A1(:,i));
 
197
                        end
 
198
                    end
 
199
                end
 
200
            end
 
201
 
 
202
        case 5
 
203
            // If the second argument is a sparse
 
204
            A2 = elements(2);
 
205
 
 
206
            // Check if A2 is a sparse
 
207
            if type(A2) <> 5 then
 
208
                error(msprintf(_("%s: Wrong type for input argument #%d: A sparse matrix expected.\n"), "%sp_min", 2));
 
209
            end
 
210
 
 
211
            [m1, n1] = size(A1);
 
212
            [m2, n2] = size(A2);
 
213
 
 
214
            // Check the size of A2
 
215
            if (m1 <> m2 | n1 <> n2) then
 
216
                error(msprintf(_("%s: Wrong size of input argument #%d: Same size as input argument #%d expected.\n"), "%sp_min", 2, 1));
 
217
            end
 
218
 
 
219
            // Retrieve the indices of non-zeros
 
220
            ij1 = spget(A1);
 
221
            ij2 = spget(A2);
 
222
 
 
223
            // A1 and A2 contain non zeros -> full
 
224
            if size(ij1,'r') == m1*n1 & size(ij2,'r') == m2*n2 then
 
225
                [m,k] = min(full(A1), full(A2));
 
226
                m = sparse(m);
 
227
                k = sparse(k);
 
228
            else
 
229
                m = A1;
 
230
                pos = find(m > A2);
 
231
                m(pos) = A2(pos);
 
232
            end
 
233
 
 
234
        else
 
235
            error(msprintf(_("%s: Wrong type for input argument #%d: A sparse matrix or a character expected.\n"), "%sp_min", 2));
 
236
        end
 
237
 
 
238
        // Case : max(A1,A2,A3,..,An) or max(list(A1,A2,A3,..,An))
 
239
    else
 
240
        if lhs == 2 then
 
241
            error(msprintf(_("%s: Wrong number of output argument: %d expected"), "%sp_min", 1));
 
242
        end
 
243
        // m is the first matrix
 
244
        m = elements(1);
 
245
        // Loop on the number of input arguments
 
246
        for i = 2:rhs
 
247
            An = elements(i);
 
248
 
 
249
            // Check if An is a sparse
 
250
            if type(An) <> 5 then
 
251
                if error_list then
 
252
                    error(msprintf(_("%s: Wrong type for input argument #%d of list: A sparse matrix expected.\n"), "%sp_min", i))
 
253
                else
 
254
                    error(msprintf(_("%s: Wrong type for input argument #%d: A sparse matrix expected.\n"), "%sp_min", i))
 
255
                end
 
256
            end
 
257
 
 
258
            [m1, n1] = size(m);
 
259
            [m2, n2] = size(An);
 
260
 
 
261
            // Check size
 
262
            if (m1 <> m2 | n1 <> n2) then
 
263
                error(msprintf(_("%s: Wrong size of input argument #%d: Same size as input argument #%d expected.\n"), "%sp_min", i, 1))
 
264
            end
 
265
 
 
266
            ij1 = spget(m);
 
267
            ij2 = spget(An);
 
268
 
 
269
            if size(ij1,'r') == m1*n1 & size(ij2,'r') == m2*n2 then
 
270
                [m,k] = min(full(m), full(An));
 
271
                m = sparse(m);
 
272
                k = sparse(k);
 
273
            else
 
274
                pos = find(m > An);
 
275
                m(pos) = An(pos);
 
276
            end
 
277
        end
 
278
    end
 
279
endfunction