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 if (pGTIn->isStruct())
67 out.push_back(new types::Struct());
69 else if (pGTIn->isCell())
71 out.push_back(new types::Cell());
75 out.push_back(types::Double::Empty());
78 return types::Function::OK;
81 pGTOut = pGTIn->clone()->getAs<types::GenericType>();
85 if (in[1]->isDouble() == false)
87 Scierror(999, _("%s: Wrong type for input argument #%d : A real matrix expected.\n"), "matrix", 2);
89 return types::Function::Error;
92 types::Double* pDblNewSize = in[1]->getAs<types::Double>();
94 if (pDblNewSize->isComplex())
96 Scierror(999, _("%s: Wrong type for input argument #%d : A real matrix expected.\n"), "matrix", 2);
98 return types::Function::Error;
101 iDims = pDblNewSize->getSize();
102 piSizes = new int[iDims];
104 for (int i = 0; i < iDims; i++)
106 piSizes[i] = static_cast<int>(pDblNewSize->get(i));
107 if (piSizes[i] == -1)
115 Scierror(999, _("%s: Wrong value for input argument #%d : Only one value can be equal to %d.\n"), "matrix", 2, -1);
118 return types::Function::Error;
121 else if (piSizes[i] < -1)
123 Scierror(999, _("%s: Wrong value for input argument #%d : At most %d expected.\n"), "matrix", 2, -1);
126 return types::Function::Error;
130 newSize *= piSizes[i];
136 iDims = static_cast<int>(in.size()) - 1;
137 piSizes = new int[iDims];
138 for (int i = 1; i < static_cast<int>(in.size()); i++)
140 if (in[i]->isDouble() == false)
142 Scierror(999, _("%s: Wrong type for input argument #%d : A real scalar expected.\n"), "matrix", i + 1);
145 return types::Function::Error;
148 types::Double* pDblNewSize = in[i]->getAs<types::Double>();
150 if (pDblNewSize->isComplex() || pDblNewSize->isScalar() == false)
152 Scierror(999, _("%s: Wrong type for input argument #%d : A real scalar expected.\n"), "matrix", i + 1);
155 return types::Function::Error;
158 piSizes[i - 1] = static_cast<int>(pDblNewSize->get(0));
159 if (piSizes[i - 1] == -1)
167 Scierror(999, _("%s: Wrong value for input argument #%d : Only one value can be equal to %d.\n"), "matrix", i + 1, -1);
170 return types::Function::Error;
173 else if (piSizes[i - 1] < -1)
175 Scierror(999, _("%s: Wrong value for input argument #%d : At most %d expected.\n"), "matrix", i + 1, -1);
178 return types::Function::Error;
182 newSize *= piSizes[i - 1];
189 piSizes[iLeastOne] = (int)pGTOut->getSize() / newSize;
192 if (pGTOut->isSparse() && iDims > 2)
194 Scierror(999, _("%s: Wrong value for input argument(s) : Sparse matrix cannot be reshaped beyond %d dimensions.\n"), "matrix", 2);
197 return types::Function::Error;
200 bOk = pGTOut->reshape(piSizes, iDims);
205 Scierror(999, _("%s: Input and output matrices must have the same number of elements.\n"), "matrix");
207 return types::Function::Error;
210 out.push_back(pGTOut);
211 return types::Function::OK;
213 /*--------------------------------------------------------------------------*/