1 /*
2 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 *  Copyright (C) 2008-2008 - DIGITEO - Antoine ELIAS
4 *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13 *
14 */
15 
16 // This code is separated in double.hxx
17 // but will be inlined in arrayof.hxx
18 //
19 // If you need additionnal headers, please add it in arrayof.hxx
20 
21 //#ifndef __ARRAYOF_HXX__
22 //    #error This file must only be include by arrayof.hxx
23 //#endif
24 
25 #ifndef __DOUBLE_HXX__
26 #define __DOUBLE_HXX__
27 
28 #include "arrayof.hxx"
29 #include "bool.hxx"
30 #include "types_transposition.hxx"
31 
32 namespace types
33 {
34 class EXTERN_AST Double : public ArrayOf<double>
35 {
36 public :
37     virtual						~Double();
38 
39     Double(double _dblReal);
40     Double(double _dblReal, double _dblImg);
41     Double(int _iRows, int _iCols, bool _bComplex = false, bool _bZComplex = false);
42     Double(int _iRows, int _iCols, double **_pdblReal);
43     Double(int _iRows, int _iCols, double **_pdblReal, double **_pdblImg);
44     Double(int _iDims, const int* _piDims, bool _bComplex = false, bool _bZComplex = false);
45 
46     static Double*              Empty();
47     static Double*              Identity(int _iRows, int _iCols);
48     static Double*              Identity(int _iDims, const int* _piDims);
49     static Double*              Identity(int _iDims, const int* _piDims, double _dblReal);
50     static Double*              Identity(int _iDims, const int* _piDims, double _dblReal, double _dblImg);
51 
52 
53     /*data management*/
54     double*                     getReal() const;
55     double                      getReal(int _iRows, int _iCols);
56     bool                        setInt(int* _piReal); //to translate int to double matrix
57 
58     /*zero or one set filler*/
59     bool                        setZeros();
60     bool                        setOnes();
61 
62     /*Config management*/
63     void                        whoAmI() override;
64     bool                        isEmpty();
65 
66     Double*                     clone() override;
67     bool                        fillFromCol(int _iCols, Double *_poSource);
68     bool                        fillFromRow(int _iRows, Double *_poSource);
69     Double*                     append(int _iRows, int _iCols, InternalType* _poSource) override;
70 
71     //bool                        append(int _iRows, int _iCols, Double *_poSource);
72 
73     bool                        operator==(const InternalType& it) override;
74     bool                        operator!=(const InternalType& it) override;
75 
isDouble()76     bool                        isDouble() override
77     {
78         return true;
79     }
80 
isComplex()81     bool isComplex() override
82     {
83         return (m_pImgData != NULL) || isViewAsZComplex();
84     }
85 
isNumericallyComplex(double tolerance=0)86     inline bool isNumericallyComplex(double tolerance = 0)
87     {
88         if (isComplex())
89         {
90             int listSize = getSize();
91             double* bImg = getImg();
92             for (int i = 0; i < listSize; i++)
93             {
94                 if (abs(bImg[i]) > tolerance)
95                 {
96                     return false;
97                 }
98             }
99         }
100         return true;
101     }
102 
103     bool isTrue() override;
104 
neg(InternalType * & out)105     bool neg(InternalType *& out) override
106     {
107         if (isEmpty())
108         {
109             out = this;
110             return true;
111         }
112 
113         return ArrayOf<double>::neg(out);
114     }
115 
setViewAsInteger(bool _bViewAsInteger=true)116     void                        setViewAsInteger(bool _bViewAsInteger = true)
117     {
118         m_bViewAsInteger = _bViewAsInteger;
119     }
isViewAsInteger()120     bool                        isViewAsInteger()
121     {
122         return m_bViewAsInteger;
123     }
124 
125     void                        convertToInteger();
126     void                        convertFromInteger();
127 
setViewAsZComplex(bool _bViewAsZComplex=true)128     void                        setViewAsZComplex(bool _bViewAsZComplex = true)
129     {
130         m_bViewAsZComplex = _bViewAsZComplex;
131     }
isViewAsZComplex()132     bool                        isViewAsZComplex()
133     {
134         return m_bViewAsZComplex;
135     }
136 
137     void                        convertToZComplex();
138     void                        convertFromZComplex();
139 
140     /* return type as string ( double, int, cell, list, ... )*/
getTypeStr() const141     virtual std::wstring        getTypeStr() const override
142     {
143         return L"constant";
144     }
145     /* return type as short string ( s, i, ce, l, ... )*/
getShortTypeStr() const146     virtual std::wstring        getShortTypeStr() const override
147     {
148         return L"s";
149     }
150 
getType(void)151     inline ScilabType           getType(void) override
152     {
153         return ScilabDouble;
154     }
getId(void)155     inline ScilabId             getId(void) override
156     {
157         return isIdentity() ? isComplex() ? IdIdentityComplex : IdIdentity
158                : isEmpty() ? IdEmpty
159                : isComplex() ? isScalar() ? IdScalarDoubleComplex
160                : IdDoubleComplex
161                : isScalar() ? IdScalarDouble
162                : IdDouble;
163     }
164 
conjugate(InternalType * & out)165     inline bool conjugate(InternalType *& out)
166     {
167         if (isEmpty() || isIdentity() || !isComplex())
168         {
169             out = clone();
170             return true;
171         }
172 
173         if (isScalar())
174         {
175             out = new Double(m_pRealData[0], -m_pImgData[0]);
176             return true;
177         }
178 
179         if (m_iDims == 2)
180         {
181             Double * pReturn = new Double(getCols(), getRows(), true);
182             out = pReturn;
183 
184             Transposition::conjugate(getSize(), m_pRealData, pReturn->m_pRealData, m_pImgData, pReturn->m_pImgData);
185             return true;
186         }
187 
188         return false;
189 
190     }
191 
adjoint(InternalType * & out)192     virtual bool adjoint(InternalType *& out) override
193     {
194         if (isEmpty())
195         {
196             out = this;
197             return true;
198         }
199 
200         if (isIdentity())
201         {
202             out = clone();
203             return true;
204         }
205 
206         if (isScalar())
207         {
208             if (isComplex())
209             {
210                 out = new Double(m_pRealData[0], -m_pImgData[0]);
211             }
212             else
213             {
214                 out = clone();
215             }
216 
217             return true;
218         }
219 
220         if (m_iDims == 2)
221         {
222             Double * pReturn = new Double(getCols(), getRows(), isComplex());
223             out = pReturn;
224             if (isComplex())
225             {
226                 Transposition::adjoint(getRows(), getCols(), m_pRealData, pReturn->m_pRealData, m_pImgData, pReturn->m_pImgData);
227             }
228             else
229             {
230                 Transposition::adjoint(getRows(), getCols(), m_pRealData, pReturn->m_pRealData);
231             }
232 
233             return true;
234         }
235 
236         return false;
237     }
238 
transpose(InternalType * & out)239     virtual bool transpose(InternalType *& out) override
240     {
241         if (isEmpty())
242         {
243             out = this;
244             return true;
245         }
246 
247         if (isIdentity() || isScalar())
248         {
249             out = clone();
250             return true;
251         }
252 
253         if (m_iDims == 2)
254         {
255             Double * pReturn = new Double(getCols(), getRows(), isComplex());
256             out = pReturn;
257             if (isComplex())
258             {
259                 Transposition::transpose(getRows(), getCols(), m_pRealData, pReturn->m_pRealData, m_pImgData, pReturn->m_pImgData);
260             }
261             else
262             {
263                 Transposition::transpose(getRows(), getCols(), m_pRealData, pReturn->m_pRealData);
264             }
265 
266             return true;
267         }
268 
269         return false;
270     }
271 
272     virtual ast::Exp*           getExp(const Location& loc) override;
273 
set(int _iPos,const double _data)274     virtual Double* set(int _iPos, const double _data) override
275     {
276         if (_iPos >= m_iSize)
277         {
278             return NULL;
279         }
280 
281         typedef Double* (Double::*set_t)(int, double);
282         Double* pIT = checkRef(this, (set_t)&Double::set, _iPos, _data);
283         if (pIT != this)
284         {
285             return pIT;
286         }
287 
288         m_pRealData[_iPos] = _data;
289         return this;
290     }
291 
set(int _iRows,int _iCols,const double _data)292     virtual Double* set(int _iRows, int _iCols, const double _data) override
293     {
294         return set(_iCols * getRows() + _iRows, _data);
295     }
296 
set(double * _pdata)297     virtual Double* set(double* _pdata) override
298     {
299         if (m_pRealData == NULL)
300         {
301             return NULL;
302         }
303 
304         typedef Double* (Double::*set_t)(double*);
305         Double* pIT = checkRef(this, (set_t)&Double::set, _pdata);
306         if (pIT != this)
307         {
308             return pIT;
309         }
310 
311         for (int i = 0; i < m_iSize; i++)
312         {
313             m_pRealData[i] = _pdata[i];
314         }
315 
316         return this;
317     }
318 
set(const double * _pdata)319     virtual Double* set(const double* _pdata) override
320     {
321         if (m_pRealData == NULL)
322         {
323             return NULL;
324         }
325 
326         typedef Double* (Double::*set_t)(const double*);
327         Double* pIT = checkRef(this, (set_t)&Double::set, _pdata);
328         if (pIT != this)
329         {
330             return pIT;
331         }
332 
333         for (int i = 0; i < m_iSize; i++)
334         {
335             m_pRealData[i] = _pdata[i];
336         }
337 
338         return this;
339     }
340 
isNativeType()341     virtual bool isNativeType() override
342     {
343         return true;
344     }
345 
fillDefaultValues()346     virtual void fillDefaultValues() override
347     {
348         int size = getSize();
349         memset(m_pRealData, 0x00, sizeof(double) * size);
350         if (isComplex())
351         {
352             memset(m_pImgData, 0x00, sizeof(double) * size);
353         }
354     }
355 
356 private:
357     virtual bool                subMatrixToString(std::wostringstream& ostr, int* _piDims, int _iDims) override;
358 
359     virtual double              getNullValue() override;
360     virtual Double*             createEmpty(int _iDims, int* _piDims, bool _bComplex = false) override;
361     virtual double              copyValue(double _dblData) override;
362     virtual void                deleteAll() override;
363     virtual void                deleteImg() override;
364     virtual double*             allocData(int _iSize) override;
deleteData(double)365     virtual void                deleteData(double /*data*/) override { }
366 
367     bool                        m_bViewAsInteger;
368     bool                        m_bViewAsZComplex;
369 
370 };
371 }
372 
373 #ifdef _MSC_VER
374 template class types::ArrayOf<double>; //Double
375 #endif
376 #endif /* !__DOUBLE_HXX__ */
377