1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2013, 2016, 2017 - Samuel GOUGEON
4 // Copyright (C) 2012 - 2016 - Scilab Enterprises
6 // This file is hereby licensed under the terms of the GNU GPL v2.0,
7 // pursuant to article 5.3.4 of the CeCILL v.2.1.
8 // This file was originally licensed under the terms of the CeCILL v2.1,
9 // and continues to be available under such terms.
10 // For more information, see the COPYING file which you should have received
11 // along with this program.
14 function mat = resize_matrix(mat, varargin)
17 // resize_matrix(M, nRows, nCols )
18 // resize_matrix(M, [nRows nCols] )
19 // resize_matrix(M, newSizes )
20 // resize_matrix(M, .. , resType )
21 // resize_matrix(M, .. , resType, padding )
22 // resize_matrix(M, .. , "" , padding )
29 M = grand(4, 3, "uin", -9, 9)
30 disp(_("RESIZING a matrix of DECIMALS:"))
33 com = [ "resize_matrix(M, 3, 4)"
34 "resize_matrix(M, [3 4 2])"
35 "resize_matrix(M, [3 4 2], """", %i)"
36 "resize_matrix(M, [3 4 2], ""string"", %i)"
40 execstr("disp("+c+")")
43 halt(ascii(10)+_("Type <Enter> to see an example with polynomials:"))
45 P = (1-x)^grand(4, 2, "uin", 0, 3)
48 com = [ "resize_matrix(P, 3, 3)"
49 "resize_matrix(P, [3 3 2])"
50 "resize_matrix(P, [3 3 2],"""", %z)"
54 execstr("disp("+c+")")
56 disp(_("The unknown variable of added values is forced to the P''s one"))
57 disp(_("Polynomials can''t be converted"))
59 halt(ascii(10)+_("Type <Enter> to see an example with character strings:"))
60 T = string(grand(4, 3, 2, "unf", 0, 1)*100)
63 com = [ "resize_matrix(T, 2, 5)"
64 "resize_matrix(T, [2 5], ""constant"")"
65 "resize_matrix(T, [2 5], """", ""abc"")"
66 "resize_matrix(T, [2 5], ""int16"", ""-1e4"")"
70 execstr("disp("+c+")")
72 warning(msprintf(_("Hypermatrices of character strings can''t be converted.\n")))
80 // if needed, default padding will be defined later:
82 // Default type of the result:
85 // ARGUMENTS ANALYSIS & CHECKING
86 // -----------------------------
87 if rhs==1 | rhs > 5 then
88 msg = _("%s: Wrong number of input argument(s): %d to %d expected.\n")
89 error(msprintf(msg, "resize_matrix", 2, 5))
92 if ~(or(type(mat)~=[15 16 17]) ..
93 & or(type(mat(:))==[1 2 4 5 6 8 10]))
94 msg = _("%s: Wrong type of input argument #%d: ""%s"" not supported.\n")
95 error(msprintf(msg, "resize_matrix", 1, typeof(mat(:))))
100 if ~isscalar(arg) then
105 if type(varg2)~=1 | ~isscalar(varg2) then
106 msg = _("%s: Wrong input argument #%d: An integer value expected.\n")
107 error(msprintf(msg, "resize_matrix", 2))
113 newsizes = [ arg varg2 ]
116 if or(type(mat) == [15 16]) & size(size(newsizes), "*") <> 1 then
117 msg = _("%s: Wrong sizes requested, cannot convert list to matrix.\n")
118 error(msprintf(msg, "resize_matrix"))
122 if size(varargin)>=nextvarg then
124 resType = varargin(nextvarg)
125 if typeof(resType)~="string"
126 resType = typeof(mat(:))
127 elseif resType=="" then
128 resType = typeof(mat(:))
129 nextvarg = nextvarg + 1
131 nextvarg = nextvarg + 1
134 if size(varargin)>=nextvarg then
135 padding = varargin(nextvarg)
138 if tpad~=type(mat(:)) then // TRYING to CONVERT the given PADDING
139 if or(type(mat(:))==[1 5 8]) then // numbers
140 if or(tpad==[4 6]) // boolean
141 padding = bool2s(tpad)
142 elseif tpad==10 // string
143 padding = strtod(padding)
145 elseif or(tpad==[2 9]) | tpad>10
148 elseif type(mat(:))==2 // polynomials
149 if or(tpad==[1 5 8]) then // decimals or encoded integers
150 padding = double(padding)
151 elseif or(tpad==[4 6]) // booleans
152 padding = bool2s(padding)
156 elseif or(type(mat(:))==[4 6]) // booleans
157 if or(tpad==[1 5 8]) then // numbers
158 padding = (padding~=0)
160 padding = (padding~="") // string
168 msg = _("%s: Wrong type for input argument #%d: cannot convert the padding value to the input matrix type.\n")
169 error(msprintf(msg, "resize_matrix", nextvarg+1))
172 padding = varargin(nextvarg)
173 if type(padding)==2 then
174 // the padding's unknown is forced to the mat's one
175 padding = varn(padding, varn(mat(1)))
181 // FORMATTING SIZES VECTORS
182 // ------------------------
183 // if the vector of new sizes is shorter than ndims(mat), it is padded with ones
185 newsizes = int(real(newsizes(:)'))
186 newsizes = [ newsizes ones(1, length(oldsizes)-length(newsizes)) ]
187 oldsizes = [ oldsizes ones(1, length(newsizes)-length(oldsizes)) ]
188 imax = length(newsizes)
192 if or(newsizes==0) then
196 k = (newsizes>0 & newsizes<oldsizes)
201 strcommand = strcommand + msprintf("1:%d",newsizes(i))
203 strcommand = strcommand + ":"
206 strcommand = strcommand + ","
209 execstr("mat = mat("+strcommand+")")
214 k = (newsizes>0 & newsizes>oldsizes)
217 // Padding with default: needs just to set the upper bounds
219 if or(type(mat(:))==[1 2 5 8]) // decimals, polynomials, decimal sparse, encoded integers
221 elseif or(type(mat(:))==[4 6]) // booleans, boolean sparse
223 elseif type(mat(:))==10 // text
230 strcommand = strcommand + msprintf("%d",newsizes(i))
232 strcommand = strcommand + "1"
235 strcommand = strcommand + ","
238 execstr("mat("+strcommand+")="+padding)
239 else // Explicit padding
246 s = strcat(string(s),",")
247 s = strsubst(s,"0",":")
248 padRange = string(oldsizes(i)+1)+":"+string(newsizes(i))
249 s = strsubst(s, string(i), padRange)
250 execstr("mat("+s+") = "+padding)
256 // CASTING : converting the type of the result
258 type_0 = typeof(mat(:))
260 if type_0=="polynomial"
261 msg = _("%s: conversion of polynomials is not supported\n")
262 error(msprintf(msg, "resize_matrix"))
263 elseif resType=="string"
265 elseif or(resType==["int8" "int16" "int32" "int64" "uint8" "uint16" "uint32" "uint64"])
267 mat = strtod(mat) // strings => decimals
269 // encoded integers, reals, complexes, booleans => #int#
270 if type(mat(:))==1 & ~isreal(mat)
271 mat = abs(mat) // taking the module of complexes
273 execstr("mat = "+resType+"(mat)")
274 elseif resType=="constant"
275 if type_0=="string" then
277 else // encoded integers, booleans => decimals
280 elseif resType=="boolean"
281 if type_0=="string" then
287 msg = _("%s: conversion into ""%s"" is not supported\n")
288 error(msprintf(msg, "resize_matrix", resType))