1
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2
// Copyright (C) 2010 - Scilab Enterprises - Adeline CARNIS
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
10
function [m, k] = %sp_min(varargin)
16
// If the first argument is a list, it retrieves the number of sparse
18
if type(varargin(1)) == 15 then
20
error(msprintf(_("%s: Wrong size of input argument: %d expected.\n"), "%sp_min", 1))
23
rhs = length(varargin(1));
24
elements = varargin(1);
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))
34
// If rhs = 1, the first input argument must be a sparse
38
// Check if A is a sparse
40
error(msprintf(_("%s: Wrong type for input argument #%d: A sparse matrix expected.\n"), "%sp_min", 1))
43
// Retrieves entries of sparse matrix
44
[ij, v, mn]= spget(A1);
63
k = [ij(i,1) ij(i,2)];
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
72
if m > 0 & length(v)<> mn(1)*mn(2) then
78
if and(ij(:,2) <> i) then
86
if and(ij(:,1) <> i) then
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
97
posi = length(find(ij(:,1)==i));
100
// it is the last index of line where m = 0
101
if mn(2)*(i-1)+j == mn(2)*i then
105
pos = find(ij(mn(2)*(i-1)+j,2) == j);
106
if pos == [] & k == 0 then
122
// If rhs = 2, the second input argument can be a character or a sparse
126
error(msprintf(_("%s: Wrong number of output argument: %d expected"), "%sp_min", 1));
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));
136
select type(elements(2))
137
// If the second argument is a string
140
// Opts can be : 'c' 'r' or 'm'
141
ind = find(opts == ['c','r','m']);
143
error(msprintf(_("%s: Wrong value for input argument #%d: [''r'' ''c'' ''m''] expected.\n"),"%sp_min",2));
146
[ij, v, mn] = spget(A1);
148
// If mn(1) = 1, A1 is a row vector
150
// min(A1, 'r') = A1(1,:) because A1 is a row vector
153
// min(A1, 'c') or min(A1, 'm') = min(A1)
156
if m > 0 & length(v)<> mn(1)*mn(2) then
162
// If mn(2) = 1, A1 is a column vector
169
if m > 0 & length(v)<> mn(1)*mn(2) then
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
179
m = spzeros(mn(1),1);
181
pos = length(find(ij(:,1)==i));
188
// If opts = 'r' or 'm', the result is returned in row vector
190
m = spzeros(1,mn(2));
192
pos = length(find(ij(:,2)==i));
203
// If the second argument is a sparse
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));
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));
219
// Retrieve the indices of non-zeros
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));
235
error(msprintf(_("%s: Wrong type for input argument #%d: A sparse matrix or a character expected.\n"), "%sp_min", 2));
238
// Case : max(A1,A2,A3,..,An) or max(list(A1,A2,A3,..,An))
241
error(msprintf(_("%s: Wrong number of output argument: %d expected"), "%sp_min", 1));
243
// m is the first matrix
245
// Loop on the number of input arguments
249
// Check if An is a sparse
250
if type(An) <> 5 then
252
error(msprintf(_("%s: Wrong type for input argument #%d of list: A sparse matrix expected.\n"), "%sp_min", i))
254
error(msprintf(_("%s: Wrong type for input argument #%d: A sparse matrix expected.\n"), "%sp_min", i))
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))
269
if size(ij1,'r') == m1*n1 & size(ij2,'r') == m2*n2 then
270
[m,k] = min(full(m), full(An));