summaryrefslogtreecommitdiffstats
path: root/scilab/modules/ast/src/cpp/types/singlepoly.cpp
blob: 00dd6eff5a914c25b441781fceb9ea741fd694bd (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
*  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
*  Copyright (C) 2008-2008 - DIGITEO - Antoine ELIAS
*
*  This file must be used under the terms of the CeCILL.
*  This source file is licensed as described in the file COPYING, which
*  you should have received as part of this distribution.  The terms
*  are also available at
*  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
*
*/
#include <sstream>
#include <cmath>
#include "singlepoly.hxx"
#include "double.hxx"
#include "tostring_common.hxx"
#include "configvariable.hxx"
#include "scilabWrite.hxx"

extern "C"
{
#include "log.h"
#include "exp.h"
#include "elem_common.h"
}

namespace types
{
SinglePoly::SinglePoly()
{
    double* pdblCoefR = NULL;
    int piDims[2] = {1, 1};
    create(piDims, 2, &pdblCoefR, NULL);
    pdblCoefR[0] = 0;
#ifndef NDEBUG
    Inspector::addItem(this);
#endif
}

SinglePoly::SinglePoly(double** _pdblCoefR, int _iRank)
{
    int piDims[2] = {_iRank + 1, 1};
    create(piDims, 2, _pdblCoefR, NULL);
#ifndef NDEBUG
    Inspector::addItem(this);
#endif
}

SinglePoly::SinglePoly(double** _pdblCoefR, double** _pdblCoefI, int _iRank)
{
    int piDims[2] = {_iRank + 1, 1};
    create(piDims, 2, _pdblCoefR, _pdblCoefI);
#ifndef NDEBUG
    Inspector::addItem(this);
#endif
}

SinglePoly::~SinglePoly()
{
    deleteAll();
#ifndef NDEBUG
    Inspector::removeItem(this);
#endif
}

void SinglePoly::deleteAll()
{
    delete[] m_pRealData;
    m_pRealData = NULL;
    deleteImg();
}

void SinglePoly::deleteImg()
{
    if (m_pImgData != NULL)
    {
        delete[] m_pImgData;
        m_pImgData = NULL;
    }
}

double SinglePoly::getNullValue()
{
    return 0;
}

SinglePoly* SinglePoly::createEmpty(int /*_iDims*/, int* _piDims, bool _bComplex)
{
    double* pdblData = NULL;
    SinglePoly* pSP = new SinglePoly(&pdblData, _piDims[0] - 1);
    pSP->setComplex(_bComplex);

    return pSP;
}

double* SinglePoly::allocData(int _iSize)
{
    double* pDbl = NULL;
    try
    {
        if (_iSize < 0)
        {
            m_pRealData = NULL;
            m_pImgData = NULL;
            char message[bsiz];
            os_sprintf(message, _("Can not allocate negative size (%d).\n"),  _iSize);
            throw ast::InternalError(message);
        }
        else
        {
            pDbl = new double[_iSize];
        }
    }
    catch (std::bad_alloc &/*e*/)
    {
        char message[bsiz];
        os_sprintf(message, _("Can not allocate %.2f MB memory.\n"),  (double) (_iSize * sizeof(double)) / 1.e6);
        throw ast::InternalError(message);
    }

    return pDbl;
}

double SinglePoly::copyValue(double _dblData)
{
    return _dblData;
}

int SinglePoly::getRank()
{
    return m_iSize - 1;
}

bool SinglePoly::setRank(int _iRank, bool bSave)
{
    double *pR = NULL;
    double *pI = NULL;
    if (bSave == false)
    {
        if (getRank() != _iRank)
        {
            int piDims[2] = {_iRank + 1, 1};
            if (m_pImgData == NULL)
            {
                deleteAll();
                create(piDims, 2, &pR, NULL);
            }
            else
            {
                deleteAll();
                create(piDims, 2, &pR, &pI);
            }

            return true;
        }

        return true;
    }
    else
    {
        double* pdblOldReal = m_pRealData;
        double* pdblOldImg  = m_pImgData;
        int iMinSize = Min(m_iSize, _iRank + 1);
        int piDims[2] = {_iRank + 1, 1};

        if (m_pImgData == NULL)
        {
            create(piDims, 2, &pR, NULL);
        }
        else
        {
            create(piDims, 2, &pR, &pI);
            memcpy(m_pImgData, pdblOldImg, iMinSize * sizeof(double));
        }

        memcpy(m_pRealData, pdblOldReal, iMinSize * sizeof(double));

        if (pdblOldImg)
        {
            delete[] pdblOldImg;
            pdblOldImg = NULL;
        }

        delete[] pdblOldReal;
        pdblOldReal = NULL;

        return true;
    }

    return false;
}

bool SinglePoly::setZeros()
{
    if (m_pRealData != NULL)
    {
        memset(m_pRealData, 0x00, m_iSize * sizeof(double));
    }
    else
    {
        return false;
    }

    if (isComplex() == true)
    {
        if (m_pImgData != NULL)
        {
            memset(m_pImgData, 0x00, m_iSize * sizeof(double));
        }
        else
        {
            return false;
        }
    }

    return true;
}

bool SinglePoly::setCoef(Double* _pdblCoefR)
{
    if (m_pRealData == NULL || _pdblCoefR == NULL)
    {
        return false;
    }

    double *pInR    = _pdblCoefR->getReal();
    double *pInI    = _pdblCoefR->getImg();

    return setCoef(pInR, pInI);
}

bool SinglePoly::setCoef(const double* _pdblCoefR, const double* _pdblCoefI)
{
    if (_pdblCoefI != NULL && isComplex() == false)
    {
        setComplex(true);
    }

    if (_pdblCoefR != NULL)
    {
        memcpy(m_pRealData, _pdblCoefR, m_iSize * sizeof(double));
    }

    if (_pdblCoefI != NULL)
    {
        memcpy(m_pImgData, _pdblCoefI, m_iSize * sizeof(double));
    }

    return true;
}

void SinglePoly::whoAmI()
{
    std::cout << "types::SinglePoly";
}

bool SinglePoly::evaluate(double _dblInR, double _dblInI, double *_pdblOutR, double *_pdblOutI)
{
    *_pdblOutR = 0;
    *_pdblOutI = 0;
    if (m_iSize == 0)
    {
        return true;
    }

    for (int i = 0 ; i < m_iSize ; i++)
    {
        //real part
        *_pdblOutR += m_pRealData[i] * std::pow(_dblInR, i);
        //only if variable is complex
        if (isComplex())
        {
            *_pdblOutR -= m_pImgData[i] * std::pow(_dblInI, i);
            //img part
            *_pdblOutI += m_pRealData[i] * std::pow(_dblInR, i);
        }
        *_pdblOutI += m_pRealData[i] * std::pow(_dblInI, i);
    }

    return true;
}

void SinglePoly::updateRank(void)
{
    int iNewRank = getRank();
    if (m_pImgData)
    {
        for (int i = getRank(); i > 0 ; i--)
        {
            if (std::fabs(m_pRealData[i]) == 0.0 && std::fabs(m_pImgData[i]) == 0.0)
            {
                iNewRank--;
            }
            else
            {
                break;
            }
        }
    }
    else
    {
        for (int i = getRank(); i > 0 ; i--)
        {
            if (std::fabs(m_pRealData[i]) == 0.0)
            {
                iNewRank--;
            }
            else
            {
                break;
            }
        }
    }

    if (iNewRank < getRank())
    {
        setRank(iNewRank, true);
    }
}

bool SinglePoly::toString(std::ostringstream& ostr)
{
    ostr << "FIXME : implement SinglePoly::toString" << std::endl;
    return true;
}

void SinglePoly::toStringReal(const std::string& _szVar, std::list<std::string>* _pListExp, std::list<std::string>* _pListCoef)
{
    toStringInternal(m_pRealData, _szVar, _pListExp, _pListCoef);
}

void SinglePoly::toStringImg(const std::string& _szVar, std::list<std::string>* _pListExp, std::list<std::string>* _pListCoef)
{
    if (isComplex() == false)
    {
        _pListExp->clear();
        _pListCoef->clear();
        return;
    }

    toStringInternal(m_pImgData, _szVar, _pListExp, _pListCoef);
}

bool SinglePoly::subMatrixToString(std::ostringstream& /*ostr*/, int* /*_piDims*/, int /*_iDims*/)
{
    return false;
}

void SinglePoly::toStringInternal(double *_pdblVal, const std::string& _szVar, std::list<std::string>* _pListExp, std::list<std::string>* _pListCoef)
{
    int iLineLen = ConfigVariable::getConsoleWidth();

    std::ostringstream ostemp;
    std::ostringstream ostemp2;

    ostemp << " ";
    ostemp2 << " ";

    int iLen = 0;
    int iLastFlush = 2;
    for (int i = 0 ; i < m_iSize ; i++)
    {
        if (_pdblVal[i] != 0)
        {
            DoubleFormat df;
            getDoubleFormat(_pdblVal[i], &df);

            if (iLen + df.iWidth + df.iSignLen >= iLineLen - 1)
            {
                iLastFlush = i;
                _pListExp->push_back(ostemp2.str());
                ostemp2.str(""); //reset stream
                addSpaces(&ostemp2, 11); //take from scilab ... why not ...

                _pListCoef->push_back(ostemp.str());
                ostemp.str(""); //reset stream
                addSpaces(&ostemp, 11); //take from scilab ... why not ...
            }

            bool bFirst = ostemp.str().size() == 1;

            // In scientific notation case bExp == true, so we have to print point (2.000D+10s)
            // In other case don't print point (2s)
            df.bPrintPoint = df.bExp;
            df.bPrintPlusSign = bFirst == false;
            df.bPrintOne = i == 0;
            addDoubleValue(&ostemp, _pdblVal[i], &df);

            if (i == 0)
            {
                iLen = static_cast<int>(ostemp.str().size());
            }
            else if (i == 1)
            {
                // add polynom name
                ostemp << _szVar;
                iLen = static_cast<int>(ostemp.str().size());
            }
            else
            {
                // add polynom name and exponent
                ostemp << _szVar;
                iLen = static_cast<int>(ostemp.str().size());
                addSpaces(&ostemp2, iLen - static_cast<int>(ostemp2.str().size()));
                ostemp2 << i;
                int iSize = static_cast<int>(ostemp2.str().size()) - iLen;
                addSpaces(&ostemp, iSize);
            }
        }
    }

    if (iLastFlush != 0)
    {
        if (ostemp.str() == " ")
        {
            ostemp << "  0";
            addSpaces(&ostemp2, static_cast<int>(ostemp.str().size()));
        }

        if (ostemp2.str() == " ")
        {
            // -1 because ostemp2 have already a space
            addSpaces(&ostemp2, static_cast<int>(ostemp.str().size()) - 1);
        }

        _pListExp->push_back(ostemp2.str());
        _pListCoef->push_back(ostemp.str());
    }

    return;
}

bool SinglePoly::operator==(const InternalType& it)
{
    if (const_cast<InternalType &>(it).isSinglePoly() == false)
    {
        return false;
    }

    SinglePoly* pP = const_cast<InternalType &>(it).getAs<SinglePoly>();

    if (getRank() != pP->getRank())
    {
        return false;
    }

    double *pdblReal = pP->get();
    for (int i = 0 ; i < getSize() ; i++)
    {
        if (m_pRealData[i] != pdblReal[i])
        {
            return false;
        }
    }

    //both complex
    if (isComplex() && pP->isComplex())
    {
        double *pdblImg = pP->getImg();
        for (int i = 0 ; i < m_iSize ; i++)
        {
            if (m_pImgData[i] != pdblImg[i])
            {
                return false;
            }
        }
    }
    //pdbl complex check all img values == 0
    else if (pP->isComplex())
    {
        double *pdblImg = pP->getImg();
        for (int i = 0 ; i < m_iSize ; i++)
        {
            if (pdblImg[i])
            {
                return false;
            }
        }
    }
    //complex check all img values == 0
    else if (isComplex())
    {
        for (int i = 0 ; i < m_iSize ; i++)
        {
            if (m_pImgData[i])
            {
                return false;
            }
        }
    }

    return true;
}

bool SinglePoly::operator!=(const InternalType& it)
{
    return !(*this == it);
}

SinglePoly* SinglePoly::clone()
{
    SinglePoly* pPoly = NULL;
    double *pR = NULL;
    if (isComplex())
    {
        double *pI = NULL;
        pPoly = new SinglePoly(&pR, &pI, getRank());
        pPoly->setCoef(m_pRealData, m_pImgData);
    }
    else
    {
        pPoly = new SinglePoly(&pR, getRank());
        pPoly->setCoef(m_pRealData, NULL);
    }
    return pPoly;
}

SinglePoly* SinglePoly::conjugate()
{
    if (isComplex())
    {
        double *pR = NULL;
        double *pI = NULL;
        SinglePoly * pPoly = new SinglePoly(&pR, &pI, getRank());

        Transposition::conjugate(m_iSize, m_pRealData, pR, m_pImgData, pI);

        return pPoly;
    }
    else
    {
        return clone();
    }
}

SinglePoly* operator*(const SinglePoly& _lhs, const SinglePoly& _rhs)
{
    SinglePoly& lhs = const_cast<SinglePoly &>(_lhs);
    SinglePoly& rhs = const_cast<SinglePoly &>(_rhs);
    SinglePoly* pOut = NULL;

    bool isComplexL = lhs.isComplex();
    bool isComplexR = rhs.isComplex();
    bool isComplexOut = isComplexL || isComplexR;

    int iRankL = lhs.getRank();
    int iRankR = rhs.getRank();
    int iRankOut = lhs.getRank() + rhs.getRank();

    double* pdblOutR = NULL;
    double* pdblOutI = NULL;
    double* pdblLR = lhs.get();
    double* pdblLI = lhs.getImg();
    double* pdblRR = rhs.get();
    double* pdblRI = rhs.getImg();

    if (isComplexOut)
    {
        pOut = new SinglePoly(&pdblOutR, &pdblOutI, iRankOut);
        memset(pdblOutR, 0x00, sizeof(double) * (iRankOut + 1));
        memset(pdblOutI, 0x00, sizeof(double) * (iRankOut + 1));
    }
    else
    {
        pOut = new SinglePoly(&pdblOutR, iRankOut);
        memset(pdblOutR, 0x00, sizeof(double) * (iRankOut + 1));
    }

    if (isComplexL)
    {
        if (isComplexR)
        {
            for (int i = 0 ; i < iRankL + 1; ++i)
            {
                for (int j = 0 ; j < iRankR + 1; ++j)
                {
                    pdblOutR[i + j]  += pdblLR[i] * pdblRR[j] - pdblLI[i] * pdblRI[j];
                    pdblOutI[i + j]  += pdblLI[i] * pdblRR[j] + pdblLR[i] * pdblRI[j];
                }
            }
        }
        else
        {
            for (int i = 0 ; i < iRankL + 1 ; ++i)
            {
                for (int j = 0 ; j < iRankR + 1 ; ++j)
                {
                    pdblOutR[i + j]  += pdblLR[i] * pdblRR[j];
                    pdblOutI[i + j]  += pdblLI[i] * pdblRR[j];
                }
            }
        }
    }
    else
    {
        if (isComplexR)
        {
            for (int i = 0 ; i < iRankL + 1 ; ++i)
            {
                for (int j = 0 ; j < iRankR + 1 ; ++j)
                {
                    pdblOutR[i + j]  += pdblLR[i] * pdblRR[j];
                    pdblOutI[i + j]  += pdblLR[i] * pdblRI[j];
                }
            }
        }
        else
        {
            for (int i = 0 ; i < iRankL + 1 ; ++i)
            {
                for (int j = 0 ; j < iRankR + 1 ; ++j)
                {
                    pdblOutR[i + j]  += pdblLR[i] * pdblRR[j];
                }
            }
        }
    }

    return pOut;
}
}