2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011 - DIGITEO - Antoine ELIAS
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
12 /*--------------------------------------------------------------------------*/
14 #include "boolean_gw.hxx"
15 #include "function.hxx"
19 #include "overload.hxx"
23 #include "localization.h"
26 /*--------------------------------------------------------------------------*/
28 types::Function::ReturnValue sci_bool2s(types::typed_list &in, int _iRetCount, types::typed_list &out)
30 types::InternalType* pOut = NULL;
33 Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "bool2s", 1);
34 return types::Function::Error;
37 if (in[0]->isDouble())
39 types::Double* pIn = in[0]->getAs<types::Double>();
43 std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_bool2s";
44 return Overload::call(wstFuncName, in, _iRetCount, out);
47 types::Double* pD = new types::Double(pIn->getDims(), pIn->getDimsArray());
49 double* pdblIn = pIn->get();
50 double* pdblOut = pD->get();
52 for (int i = 0 ; i < pIn->getSize() ; i++)
54 pdblOut[i] = static_cast<double>(pdblIn[i] != 0);
58 else if (in[0]->isBool())
60 types::Bool* pIn = in[0]->getAs<types::Bool>();
61 types::Double* pD = new types::Double(pIn->getDims(), pIn->getDimsArray());
63 int* piIn = pIn->get();
64 double* pdblOut = pD->get();
66 for (int i = 0 ; i < pIn->getSize() ; i++)
68 pdblOut[i] = static_cast<double>(piIn[i]);
72 else if (in[0]->isSparse())
74 types::Sparse* pSpIn = in[0]->getAs<types::Sparse>();
76 if (pSpIn->isComplex())
78 std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_bool2s";
79 return Overload::call(wstFuncName, in, _iRetCount, out);
82 types::Sparse* pSpOut = new types::Sparse(pSpIn->getRows(), pSpIn->getCols());
84 int iNonZeros = static_cast<int>(pSpIn->nonZeros());
87 int* pRows = new int[iNonZeros * 2];
88 pSpIn->outputRowCol(pRows);
89 int* pCols = pRows + iNonZeros;
92 double* pNonZeroR = new double[iNonZeros];
93 double* pNonZeroI = new double[iNonZeros];
94 pSpIn->outputValues(pNonZeroR, pNonZeroI);
96 for (int i = 0; i < iNonZeros; i++)
98 pSpOut->set(pRows[i] - 1, pCols[i] - 1, static_cast<double>(pNonZeroR[i] != 0));
103 else if (in[0]->isSparseBool())
105 types::SparseBool* pSpbIn = in[0]->getAs<types::SparseBool>();
106 types::Sparse* pSpOut = new types::Sparse(pSpbIn ->getRows(), pSpbIn ->getCols());
108 int iNonZeros = static_cast<int>(pSpbIn->nbTrue());
111 int* pRows = new int[iNonZeros * 2];
112 pSpbIn->outputRowCol(pRows);
113 int* pCols = pRows + iNonZeros;
115 for (int i = 0; i < iNonZeros; i++)
117 pSpOut->set(pRows[i] - 1, pCols[i] - 1, 1);
125 std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_bool2s";
126 return Overload::call(wstFuncName, in, _iRetCount, out);
130 return types::Function::OK;
132 /*--------------------------------------------------------------------------*/