2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011 - DIGITEO - Antoine ELIAS
4 * Copyright (C) 2012 - DIGITEO - Cedric DELAMARRE
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
16 /*--------------------------------------------------------------------------*/
17 #include "elem_func_gw.hxx"
18 #include "function.hxx"
23 #include "overload.hxx"
28 #include "localization.h"
31 /*--------------------------------------------------------------------------*/
32 types::Function::ReturnValue sci_matrix(types::typed_list &in, int _iRetCount, types::typed_list &out)
34 types::GenericType* pGTIn = NULL;
35 types::GenericType* pGTOut = NULL;
44 Scierror(77, _("%s: Wrong number of input argument(s): At least %d expected.\n"), "matrix", 2);
45 return types::Function::Error;
50 Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "matrix", 1);
51 return types::Function::Error;
54 if (in[0]->isArrayOf() == false &&
55 in[0]->isSparse() == false &&
56 in[0]->isSparseBool() == false)
58 std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_matrix";
59 return Overload::call(wstFuncName, in, _iRetCount, out);
62 pGTIn = in[0]->getAs<types::GenericType>();
63 if (pGTIn->getSize() == 0)
65 out.push_back(pGTIn->clone());
66 return types::Function::OK;
69 pGTOut = pGTIn->clone()->getAs<types::GenericType>();
73 if (in[1]->isDouble() == false)
75 Scierror(999, _("%s: Wrong type for input argument #%d : A real matrix expected.\n"), "matrix", 2);
77 return types::Function::Error;
80 types::Double* pDblNewSize = in[1]->getAs<types::Double>();
82 if (pDblNewSize->isComplex())
84 Scierror(999, _("%s: Wrong type for input argument #%d : A real matrix expected.\n"), "matrix", 2);
86 return types::Function::Error;
89 iDims = pDblNewSize->getSize();
90 piSizes = new int[iDims];
92 for (int i = 0; i < iDims; i++)
94 piSizes[i] = static_cast<int>(pDblNewSize->get(i));
103 Scierror(999, _("%s: Wrong value for input argument #%d : Only one value can be equal to %d.\n"), "matrix", 2, -1);
106 return types::Function::Error;
109 else if (piSizes[i] < -1)
111 Scierror(999, _("%s: Wrong value for input argument #%d : At most %d expected.\n"), "matrix", 2, -1);
114 return types::Function::Error;
118 newSize *= piSizes[i];
124 iDims = static_cast<int>(in.size()) - 1;
125 piSizes = new int[iDims];
126 for (int i = 1; i < static_cast<int>(in.size()); i++)
128 if (in[i]->isDouble() == false)
130 Scierror(999, _("%s: Wrong type for input argument #%d : A real scalar expected.\n"), "matrix", i + 1);
133 return types::Function::Error;
136 types::Double* pDblNewSize = in[i]->getAs<types::Double>();
138 if (pDblNewSize->isComplex() || pDblNewSize->isScalar() == false)
140 Scierror(999, _("%s: Wrong type for input argument #%d : A real scalar expected.\n"), "matrix", i + 1);
143 return types::Function::Error;
146 piSizes[i - 1] = static_cast<int>(pDblNewSize->get(0));
147 if (piSizes[i - 1] == -1)
155 Scierror(999, _("%s: Wrong value for input argument #%d : Only one value can be equal to %d.\n"), "matrix", i + 1, -1);
158 return types::Function::Error;
161 else if (piSizes[i - 1] < -1)
163 Scierror(999, _("%s: Wrong value for input argument #%d : At most %d expected.\n"), "matrix", i + 1, -1);
166 return types::Function::Error;
170 newSize *= piSizes[i - 1];
177 piSizes[iLeastOne] = (int)pGTOut->getSize() / newSize;
180 if (pGTOut->isSparse() && iDims > 2)
182 Scierror(999, _("%s: Wrong value for input argument(s) : Sparse matrix cannot be reshaped beyond %d dimensions.\n"), "matrix", 2);
185 return types::Function::Error;
188 bOk = pGTOut->reshape(piSizes, iDims);
193 Scierror(999, _("%s: Input and output matrices must have the same number of elements.\n"), "matrix");
195 return types::Function::Error;
198 out.push_back(pGTOut);
199 return types::Function::OK;
201 /*--------------------------------------------------------------------------*/