summaryrefslogtreecommitdiffstats
path: root/scilab/modules/boolean/sci_gateway/cpp/sci_bool2s.cpp
blob: dc78b12645c4401b8f62b8e5a69b693682e30cbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) 2011 - DIGITEO - Antoine ELIAS
*
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
*
*/
/*--------------------------------------------------------------------------*/

#include "boolean_gw.hxx"
#include "function.hxx"
#include "double.hxx"
#include "sparse.hxx"
#include "bool.hxx"
#include "overload.hxx"

extern "C"
{
#include "localization.h"
#include "Scierror.h"
}
/*--------------------------------------------------------------------------*/

types::Function::ReturnValue sci_bool2s(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    types::InternalType* pOut = NULL;
    if (in.size() != 1)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "bool2s", 1);
        return types::Function::Error;
    }

    if (in[0]->isDouble())
    {
        types::Double* pIn = in[0]->getAs<types::Double>();

        if (pIn->isComplex())
        {
            std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_bool2s";
            return Overload::call(wstFuncName, in, _iRetCount, out);
        }

        types::Double* pD = new types::Double(pIn->getDims(), pIn->getDimsArray());

        double* pdblIn  = pIn->get();
        double* pdblOut = pD->get();

        for (int i = 0 ; i < pIn->getSize() ; i++)
        {
            pdblOut[i] = static_cast<double>(pdblIn[i] != 0);
        }
        pOut = pD;
    }
    else if (in[0]->isBool())
    {
        types::Bool* pIn = in[0]->getAs<types::Bool>();
        types::Double* pD = new types::Double(pIn->getDims(), pIn->getDimsArray());

        int* piIn = pIn->get();
        double* pdblOut = pD->get();

        for (int i = 0 ; i < pIn->getSize() ; i++)
        {
            pdblOut[i] = static_cast<double>(piIn[i]);
        }
        pOut = pD;
    }
    else if (in[0]->isSparse())
    {
        types::Sparse* pSpIn = in[0]->getAs<types::Sparse>();

        if (pSpIn->isComplex())
        {
            std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_bool2s";
            return Overload::call(wstFuncName, in, _iRetCount, out);
        }

        types::Sparse* pSpOut = new types::Sparse(pSpIn->getRows(), pSpIn->getCols());

        int iNonZeros = static_cast<int>(pSpIn->nonZeros());

        //coords
        int* pRows = new int[iNonZeros * 2];
        pSpIn->outputRowCol(pRows);
        int* pCols = pRows + iNonZeros;

        //values
        double* pNonZeroR = new double[iNonZeros];
        double* pNonZeroI = new double[iNonZeros];
        pSpIn->outputValues(pNonZeroR, pNonZeroI);

        for (int i = 0; i < iNonZeros; i++)
        {
            pSpOut->set(pRows[i] - 1, pCols[i] - 1, static_cast<double>(pNonZeroR[i] != 0));
        }

        pOut = pSpOut;
        delete[] pNonZeroR;
        delete[] pNonZeroI;
        delete[] pRows;
    }
    else if (in[0]->isSparseBool())
    {
        types::SparseBool* pSpbIn = in[0]->getAs<types::SparseBool>();
        types::Sparse* pSpOut = new types::Sparse(pSpbIn ->getRows(), pSpbIn ->getCols());

        int iNonZeros = static_cast<int>(pSpbIn->nbTrue());

        //coords
        int* pRows = new int[iNonZeros * 2];
        pSpbIn->outputRowCol(pRows);
        int* pCols = pRows + iNonZeros;

        for (int i = 0; i < iNonZeros; i++)
        {
            pSpOut->set(pRows[i] - 1, pCols[i] - 1, 1);
        }

        pOut = pSpOut;
        delete[] pRows;
    }
    else
    {
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_bool2s";
        return Overload::call(wstFuncName, in, _iRetCount, out);
    }

    out.push_back(pOut);
    return types::Function::OK;
}
/*--------------------------------------------------------------------------*/