2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2008-2008 - DIGITEO - Antoine ELIAS
4 * Copyright (C) 2010-2010 - DIGITEO - Bruno JOFRET
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
14 #include "types_multiplication.hxx"
15 #include "types_addition.hxx"
19 #include "polynom.hxx"
20 #include "singlepoly.hxx"
22 #include "scilabexception.hxx"
26 #include "matrix_multiplication.h"
27 #include "matrix_addition.h"
28 #include "operation_f.h"
29 #include "localization.h"
30 #include "charEncoding.h"
31 #include "elem_common.h"
35 using namespace types;
37 InternalType *GenericTimes(InternalType *_pLeftOperand, InternalType *_pRightOperand)
39 InternalType *pResult = NULL;
40 GenericType::ScilabType TypeL = _pLeftOperand->getType();
41 GenericType::ScilabType TypeR = _pRightOperand->getType();
43 if (TypeL == GenericType::ScilabDouble && _pLeftOperand->getAs<Double>()->isEmpty())
45 return Double::Empty();
48 if (TypeR == GenericType::ScilabDouble && _pRightOperand->getAs<Double>()->isEmpty())
50 return Double::Empty();
56 if (TypeL == GenericType::ScilabDouble && TypeR == GenericType::ScilabDouble)
58 Double *pL = _pLeftOperand->getAs<Double>();
59 Double *pR = _pRightOperand->getAs<Double>();
61 int iResult = MultiplyDoubleByDouble(pL, pR, (Double**)&pResult);
64 throw ast::ScilabError(_W("Inconsistent row/column dimensions.\n"));
73 else if (TypeL == InternalType::ScilabDouble && TypeR == InternalType::ScilabPolynom)
75 Double *pL = _pLeftOperand->getAs<Double>();
76 Polynom *pR = _pRightOperand->getAs<types::Polynom>();
78 int iResult = MultiplyDoubleByPoly(pL, pR, (Polynom**)&pResult);
81 throw ast::ScilabError(_W("Inconsistent row/column dimensions.\n"));
90 else if (TypeL == InternalType::ScilabPolynom && TypeR == InternalType::ScilabDouble)
92 Polynom *pL = _pLeftOperand->getAs<types::Polynom>();
93 Double *pR = _pRightOperand->getAs<Double>();
95 int iResult = MultiplyPolyByDouble(pL, pR, (Polynom**)&pResult);
98 throw ast::ScilabError(_W("Inconsistent row/column dimensions.\n"));
107 else if (TypeL == InternalType::ScilabPolynom && TypeR == InternalType::ScilabPolynom)
109 Polynom *pL = _pLeftOperand->getAs<types::Polynom>();
110 Polynom *pR = _pRightOperand->getAs<types::Polynom>();
112 int iResult = MultiplyPolyByPoly(pL, pR, (Polynom**)&pResult);
115 throw ast::ScilabError(_W("Inconsistent row/column dimensions.\n"));
124 if (TypeL == GenericType::ScilabSparse && TypeR == GenericType::ScilabSparse)
126 Sparse *pL = _pLeftOperand->getAs<Sparse>();
127 Sparse *pR = _pRightOperand->getAs<Sparse>();
129 int iResult = MultiplySparseBySparse(pL, pR, (Sparse**)&pResult);
132 throw ast::ScilabError(_W("Inconsistent row/column dimensions.\n"));
141 if (TypeL == GenericType::ScilabDouble && TypeR == GenericType::ScilabSparse)
143 Double *pL = _pLeftOperand->getAs<Double>();
144 Sparse *pR = _pRightOperand->getAs<Sparse>();
146 int iResult = MultiplyDoubleBySparse(pL, pR, (GenericType**)&pResult);
149 throw ast::ScilabError(_W("Inconsistent row/column dimensions.\n"));
158 if (TypeL == GenericType::ScilabSparse && TypeR == GenericType::ScilabDouble)
160 Sparse *pL = _pLeftOperand->getAs<Sparse>();
161 Double *pR = _pRightOperand->getAs<Double>();
163 int iResult = MultiplySparseByDouble(pL, pR, (GenericType**)&pResult);
166 throw ast::ScilabError(_W("Inconsistent row/column dimensions.\n"));
173 ** Default case : Return NULL will Call Overloading.
179 int MultiplyDoubleByDouble(Double* _pDouble1, Double* _pDouble2, Double** _pDoubleOut)
181 if (_pDouble1->isScalar())
183 bool bComplex1 = _pDouble1->isComplex();
184 bool bComplex2 = _pDouble2->isComplex();
186 (*_pDoubleOut) = new Double(_pDouble2->getDims(), _pDouble2->getDimsArray(), bComplex1 | bComplex2);
188 if (bComplex1 == false && bComplex2 == false)
190 iMultiRealScalarByRealMatrix(_pDouble1->get(0), _pDouble2->get(), _pDouble2->getSize(), 1, (*_pDoubleOut)->get());
192 else if (bComplex1 == false && bComplex2 == true)
194 iMultiRealScalarByComplexMatrix(_pDouble1->get(0), _pDouble2->get(), _pDouble2->getImg(), _pDouble2->getSize(), 1, (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg());
196 else if (bComplex1 == true && bComplex2 == false)
198 iMultiComplexScalarByRealMatrix(_pDouble1->get(0), _pDouble1->getImg(0), _pDouble2->get(), _pDouble2->getSize(), 1, (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg());
200 else //if(bComplex1 == true && bComplex2 == true)
202 iMultiComplexScalarByComplexMatrix(_pDouble1->get(0), _pDouble1->getImg(0), _pDouble2->get(), _pDouble2->getImg(), _pDouble2->getSize(), 1, (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg());
208 if (_pDouble2->isScalar())
210 bool bComplex1 = _pDouble1->isComplex();
211 bool bComplex2 = _pDouble2->isComplex();
213 (*_pDoubleOut) = new Double(_pDouble1->getDims(), _pDouble1->getDimsArray(), bComplex1 | bComplex2);
215 if (bComplex1 == false && bComplex2 == false)
217 //Real Matrix by Real Scalar
218 iMultiRealScalarByRealMatrix(_pDouble2->get(0), _pDouble1->get(), _pDouble1->getSize(), 1, (*_pDoubleOut)->get());
220 else if (bComplex1 == false && bComplex2 == true)
222 //Real Matrix by Scalar Complex
223 iMultiComplexScalarByRealMatrix(_pDouble2->get(0), _pDouble2->getImg(0), _pDouble1->get(), _pDouble1->getSize(), 1, (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg());
225 else if (bComplex1 == true && bComplex2 == false)
227 iMultiRealScalarByComplexMatrix(_pDouble2->get(0, 0), _pDouble1->get(), _pDouble1->getImg(), _pDouble1->getSize(), 1, (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg());
229 else //if(bComplex1 == true && bComplex2 == true)
231 iMultiComplexScalarByComplexMatrix(_pDouble2->get(0, 0), _pDouble2->getImg(0, 0), _pDouble1->get(), _pDouble1->getImg(), _pDouble1->getSize(), 1, (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg());
237 if (_pDouble1->getDims() > 2 || _pDouble2->getDims() > 2 || _pDouble1->getCols() != _pDouble2->getRows())
242 bool bComplex1 = _pDouble1->isComplex();
243 bool bComplex2 = _pDouble2->isComplex();
244 (*_pDoubleOut) = new Double(_pDouble1->getRows(), _pDouble2->getCols(), bComplex1 | bComplex2);
246 if (bComplex1 == false && bComplex2 == false)
248 //Real Matrix by Real Matrix
249 iMultiRealMatrixByRealMatrix(
250 _pDouble1->get(), _pDouble1->getRows(), _pDouble1->getCols(),
251 _pDouble2->get(), _pDouble2->getRows(), _pDouble2->getCols(),
252 (*_pDoubleOut)->get());
254 else if (bComplex1 == false && bComplex2 == true)
256 //Real Matrix by Matrix Complex
257 iMultiRealMatrixByComplexMatrix(
258 _pDouble1->get(), _pDouble1->getRows(), _pDouble1->getCols(),
259 _pDouble2->get(), _pDouble2->getImg(), _pDouble2->getRows(), _pDouble2->getCols(),
260 (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg());
262 else if (bComplex1 == true && bComplex2 == false)
264 //Complex Matrix by Real Matrix
265 iMultiComplexMatrixByRealMatrix(
266 _pDouble1->get(), _pDouble1->getImg(), _pDouble1->getRows(), _pDouble1->getCols(),
267 _pDouble2->get(), _pDouble2->getRows(), _pDouble2->getCols(),
268 (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg());
270 else //if(bComplex1 == true && bComplex2 == true)
272 //Complex Matrix by Complex Matrix
273 iMultiComplexMatrixByComplexMatrix(
274 _pDouble1->get(), _pDouble1->getImg(), _pDouble1->getRows(), _pDouble1->getCols(),
275 _pDouble2->get(), _pDouble2->getImg(), _pDouble2->getRows(), _pDouble2->getCols(),
276 (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg());
281 int DotMultiplyDoubleByDouble(Double* _pDouble1, Double* _pDouble2, Double** _pDoubleOut)
283 bool bComplex1 = _pDouble1->isComplex();
284 bool bComplex2 = _pDouble2->isComplex();
285 bool bScalar1 = _pDouble1->isScalar();
286 bool bScalar2 = _pDouble2->isScalar();
290 (*_pDoubleOut) = new Double(_pDouble2->getDims(), _pDouble2->getDimsArray(), _pDouble1->isComplex() | _pDouble2->isComplex());
291 if (bComplex1 == false && bComplex2 == false)
293 iMultiRealScalarByRealMatrix(_pDouble1->get(0), _pDouble2->get(), _pDouble2->getSize(), 1, (*_pDoubleOut)->get());
295 else if (bComplex1 == false && bComplex2 == true)
297 iMultiRealScalarByComplexMatrix(_pDouble1->get(0), _pDouble2->get(), _pDouble2->getImg(), _pDouble2->getSize(), 1, (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg());
299 else if (bComplex1 == true && bComplex2 == false)
301 iMultiComplexScalarByRealMatrix(_pDouble1->get(0), _pDouble1->getImg(0), _pDouble2->get(), _pDouble2->getSize(), 1, (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg());
303 else //if(bComplex1 == true && bComplex2 == true)
305 iMultiComplexScalarByComplexMatrix(_pDouble1->get(0), _pDouble1->getImg(0), _pDouble2->get(), _pDouble2->getImg(), _pDouble2->getSize(), 1, (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg());
313 (*_pDoubleOut) = new Double(_pDouble1->getDims(), _pDouble1->getDimsArray(), _pDouble1->isComplex() | _pDouble2->isComplex());
314 if (bComplex1 == false && bComplex2 == false)
316 //Real Matrix by Real Scalar
317 iMultiRealScalarByRealMatrix(_pDouble2->get(0), _pDouble1->get(), _pDouble1->getSize(), 1, (*_pDoubleOut)->get());
319 else if (bComplex1 == false && bComplex2 == true)
321 //Real Matrix by Scalar Complex
322 iMultiComplexScalarByRealMatrix(_pDouble2->get(0), _pDouble2->getImg(0), _pDouble1->get(), _pDouble1->getSize(), 1, (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg());
324 else if (bComplex1 == true && bComplex2 == false)
326 iMultiRealScalarByComplexMatrix(_pDouble2->get(0), _pDouble1->get(), _pDouble1->getImg(), _pDouble1->getSize(), 1, (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg());
328 else //if(bComplex1 == true && bComplex2 == true)
330 iMultiComplexScalarByComplexMatrix(_pDouble2->get(0), _pDouble2->getImg(0), _pDouble1->get(), _pDouble1->getImg(), _pDouble1->getSize(), 1, (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg());
336 if (_pDouble1->getDims() != _pDouble2->getDims())
341 int* piDims1 = _pDouble1->getDimsArray();
342 int* piDims2 = _pDouble2->getDimsArray();
344 for (int i = 0 ; i < _pDouble1->getDims() ; i++)
346 if (piDims1[i] != piDims2[i])
352 (*_pDoubleOut) = new Double(_pDouble1->getDims(), _pDouble1->getDimsArray(), _pDouble1->isComplex() | _pDouble2->isComplex());
353 if (bComplex1 == false && bComplex2 == false)
355 iDotMultiplyRealMatrixByRealMatrix(_pDouble1->get(), _pDouble2->get(), (*_pDoubleOut)->get(), _pDouble1->getSize(), 1);
357 else if (bComplex1 == false && bComplex2 == true)
359 iDotMultiplyRealMatrixByComplexMatrix(_pDouble1->get(), _pDouble2->get(), _pDouble2->getImg(), (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg(), _pDouble1->getSize(), 1);
361 else if (bComplex1 == true && bComplex2 == false)
363 iDotMultiplyComplexMatrixByRealMatrix(_pDouble1->get(), _pDouble1->getImg(), _pDouble2->get(), (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg(), _pDouble1->getSize(), 1);
365 else //if(bComplex1 == true && bComplex2 == true)
367 iDotMultiplyComplexMatrixByComplexMatrix(_pDouble1->get(), _pDouble1->getImg(), _pDouble2->get(), _pDouble2->getImg(), (*_pDoubleOut)->get(), (*_pDoubleOut)->getImg(), _pDouble1->getSize(), 1);
373 int MultiplyDoubleByPoly(Double* _pDouble, Polynom* _pPoly, Polynom** _pPolyOut)
375 bool bComplex1 = _pDouble->isComplex();
376 bool bComplex2 = _pPoly->isComplex();
378 if (_pDouble->isScalar())
380 int* piRank = new int[_pPoly->getSize()];
381 for (int i = 0 ; i < _pPoly->getSize() ; i++)
383 piRank[i] = _pPoly->get(i)->getRank();
386 (*_pPolyOut) = new Polynom(_pPoly->getVariableName(), _pPoly->getDims(), _pPoly->getDimsArray(), piRank);
388 if (bComplex1 || bComplex2)
390 (*_pPolyOut)->setComplex(true);
393 for (int i = 0 ; i < _pPoly->getSize() ; i++)
395 SinglePoly *pPolyIn = _pPoly->get(i);
396 double* pRealIn = pPolyIn->get();
397 double* pImgIn = pPolyIn->getImg();
399 SinglePoly *pPolyOut = (*_pPolyOut)->get(i);
400 double* pRealOut = pPolyOut->get();
401 double* pImgOut = pPolyOut->getImg();
403 if (bComplex1 == false && bComplex2 == false)
405 iMultiRealScalarByRealMatrix(_pDouble->get(0), pRealIn, 1, pPolyIn->getSize(), pRealOut);
407 else if (bComplex1 == false && bComplex2 == true)
409 iMultiRealScalarByComplexMatrix(_pDouble->get(0), pRealIn, pImgIn, 1, pPolyIn->getSize(), pRealOut, pImgOut);
411 else if (bComplex1 == true && bComplex2 == false)
413 iMultiComplexScalarByRealMatrix(_pDouble->get(0), _pDouble->getImg(0), pRealIn, 1, pPolyIn->getSize(), pRealOut, pImgOut);
415 else if (bComplex1 == true && bComplex2 == true)
417 iMultiComplexScalarByComplexMatrix(_pDouble->get(0), _pDouble->getImg(0), pRealIn, pImgIn, 1, pPolyIn->getSize(), pRealOut, pImgOut);
420 (*_pPolyOut)->updateRank();
424 if (_pPoly->isScalar())
426 int* piRank = new int[_pDouble->getSize()];
427 for (int i = 0 ; i < _pDouble->getSize() ; i++)
429 piRank[i] = _pPoly->get(0)->getRank();
432 (*_pPolyOut) = new Polynom(_pPoly->getVariableName(), _pDouble->getDims(), _pDouble->getDimsArray(), piRank);
434 if (bComplex1 || bComplex2)
436 (*_pPolyOut)->setComplex(true);
439 double *pDoubleR = _pDouble->get();
440 double *pDoubleI = _pDouble->getImg();
442 SinglePoly *pPolyIn = _pPoly->get(0);
443 double* pRealIn = pPolyIn->get();
444 double* pImgIn = pPolyIn->getImg();
446 for (int i = 0 ; i < _pDouble->getSize() ; i++)
448 SinglePoly *pPolyOut = (*_pPolyOut)->get(i);
449 double* pRealOut = pPolyOut->get();
450 double* pImgOut = pPolyOut->getImg();
452 if (bComplex1 == false && bComplex2 == false)
454 iMultiRealScalarByRealMatrix(pDoubleR[i], pRealIn, 1, pPolyIn->getSize(), pRealOut);
456 else if (bComplex1 == false && bComplex2 == true)
458 iMultiRealScalarByComplexMatrix(pDoubleR[i], pRealIn, pImgIn, 1, pPolyIn->getSize(), pRealOut, pImgOut);
460 else if (bComplex1 == true && bComplex2 == false)
462 iMultiComplexScalarByRealMatrix(pDoubleR[i], pDoubleI[i], pRealIn, 1, pPolyIn->getSize(), pRealOut, pImgOut);
464 else if (bComplex1 == true && bComplex2 == true)
466 iMultiComplexScalarByComplexMatrix(pDoubleR[i], pDoubleI[i], pRealIn, pImgIn, 1, pPolyIn->getSize(), pRealOut, pImgOut);
470 (*_pPolyOut)->updateRank();
474 if (_pPoly->getDims() > 2 || _pDouble->getDims() > 2 || _pDouble->getCols() != _pPoly->getRows())
479 int* piRank = new int[_pDouble->getRows() * _pPoly->getCols()];
480 int iMaxRank = _pPoly->getMaxRank();
481 for (int i = 0 ; i < _pDouble->getRows() * _pPoly->getCols() ; i++)
483 piRank[i] = iMaxRank;
486 (*_pPolyOut) = new Polynom(_pPoly->getVariableName(), _pDouble->getRows(), _pPoly->getCols(), piRank);
488 if (bComplex1 || bComplex2)
490 (*_pPolyOut)->setComplex(true);
493 Double *pCoef = _pPoly->getCoef();
494 Double *pTemp = new Double(_pDouble->getRows(), pCoef->getCols(), bComplex1 || bComplex2);
496 if (bComplex1 == false && bComplex2 == false)
498 iMultiRealMatrixByRealMatrix(_pDouble->get(), _pDouble->getRows(), _pDouble->getCols(),
499 pCoef->get(), pCoef->getRows(), pCoef->getCols(),
502 else if (bComplex1 == false && bComplex2 == true)
504 iMultiRealMatrixByComplexMatrix(_pDouble->get(), _pDouble->getRows(), _pDouble->getCols(),
505 pCoef->get(), pCoef->getImg(), pCoef->getRows(), pCoef->getCols(),
506 pTemp->get(), pTemp->getImg());
509 else if (bComplex1 == true && bComplex2 == false)
511 iMultiComplexMatrixByRealMatrix(_pDouble->get(), _pDouble->getImg(), _pDouble->getRows(), _pDouble->getCols(),
512 pCoef->get(), pCoef->getRows(), pCoef->getCols(),
513 pTemp->get(), pTemp->getImg());
515 else //if(bComplex1 == true && bComplex2 == true)
517 iMultiComplexMatrixByComplexMatrix(_pDouble->get(), _pDouble->getImg(), _pDouble->getRows(), _pDouble->getCols(),
518 pCoef->get(), pCoef->getImg(), pCoef->getRows(), pCoef->getCols(),
519 pTemp->get(), pTemp->getImg());
522 (*_pPolyOut)->setCoef(pTemp);
523 (*_pPolyOut)->updateRank();
528 int MultiplyPolyByDouble(Polynom* _pPoly, Double* _pDouble, Polynom **_pPolyOut)
530 bool bComplex1 = _pPoly->isComplex();
531 bool bComplex2 = _pDouble->isComplex();
532 bool bScalar1 = _pPoly->isScalar();
533 bool bScalar2 = _pDouble->isScalar();
537 int* piRank = new int[_pDouble->getSize()];
538 for (int i = 0 ; i < _pDouble->getSize() ; i++)
540 piRank[i] = _pPoly->get(0)->getRank();
543 (*_pPolyOut) = new Polynom(_pPoly->getVariableName(), _pDouble->getDims(), _pDouble->getDimsArray(), piRank);
545 if (bComplex1 || bComplex2)
547 (*_pPolyOut)->setComplex(true);
550 double *pDoubleR = _pDouble->get();
551 double *pDoubleI = _pDouble->getImg();
553 SinglePoly *pPolyIn = _pPoly->get(0);
554 double* pRealIn = pPolyIn->get();
555 double* pImgIn = pPolyIn->getImg();
557 for (int i = 0 ; i < _pDouble->getSize() ; i++)
559 SinglePoly *pPolyOut = (*_pPolyOut)->get(i);
560 double* pRealOut = pPolyOut->get();
561 double* pImgOut = pPolyOut->getImg();
563 if (bComplex1 == false && bComplex2 == false)
565 iMultiRealScalarByRealMatrix(pDoubleR[i], pRealIn, 1, pPolyIn->getSize(), pRealOut);
567 else if (bComplex1 == false && bComplex2 == true)
569 iMultiComplexScalarByRealMatrix(pDoubleR[i], pDoubleI[i], pRealIn, 1, pPolyIn->getSize(), pRealOut, pImgOut);
571 else if (bComplex1 == true && bComplex2 == false)
573 iMultiRealScalarByComplexMatrix(pDoubleR[i], pRealIn, pImgIn, 1, pPolyIn->getSize(), pRealOut, pImgOut);
575 else if (bComplex1 == true && bComplex2 == true)
577 iMultiComplexScalarByComplexMatrix(pDoubleR[i], pDoubleI[i], pRealIn, pImgIn, 1, pPolyIn->getSize(), pRealOut, pImgOut);
581 (*_pPolyOut)->updateRank();
586 int* piRank = new int[_pPoly->getSize()];
587 for (int i = 0 ; i < _pPoly->getSize() ; i++)
589 piRank[i] = _pPoly->get(i)->getRank();
592 (*_pPolyOut) = new Polynom(_pPoly->getVariableName(), _pPoly->getDims(), _pPoly->getDimsArray(), piRank);
594 if (bComplex1 || bComplex2)
596 (*_pPolyOut)->setComplex(true);
599 for (int i = 0 ; i < _pPoly->getSize() ; i++)
601 SinglePoly *pPolyIn = _pPoly->get(i);
602 double* pRealIn = pPolyIn->get();
603 double* pImgIn = pPolyIn->getImg();
605 SinglePoly *pPolyOut = (*_pPolyOut)->get(i);
606 double* pRealOut = pPolyOut->get();
607 double* pImgOut = pPolyOut->getImg();
609 if (bComplex1 == false && bComplex2 == false)
611 iMultiRealScalarByRealMatrix(_pDouble->get(0), pRealIn, 1, pPolyIn->getSize(), pRealOut);
613 else if (bComplex1 == false && bComplex2 == true)
615 iMultiComplexScalarByRealMatrix(_pDouble->get(0), _pDouble->getImg(0), pRealIn, 1, pPolyIn->getSize(), pRealOut, pImgOut);
617 else if (bComplex1 == true && bComplex2 == false)
619 iMultiRealScalarByComplexMatrix(_pDouble->get(0), pRealIn, pImgIn, 1, pPolyIn->getSize(), pRealOut, pImgOut);
621 else if (bComplex1 == true && bComplex2 == true)
623 iMultiComplexScalarByComplexMatrix(_pDouble->get(0), _pDouble->getImg(0), pRealIn, pImgIn, 1, pPolyIn->getSize(), pRealOut, pImgOut);
627 (*_pPolyOut)->updateRank();
631 if (_pDouble->getDims() > 2 || _pPoly->getDims() > 2 || _pPoly->getCols() != _pDouble->getRows())
636 int* piRank = new int[_pPoly->getRows() * _pDouble->getCols()];
637 int iMaxRank = _pPoly->getMaxRank();
638 for (int i = 0 ; i < _pPoly->getRows() * _pDouble->getCols() ; i++)
640 piRank[i] = iMaxRank;
643 (*_pPolyOut) = new Polynom(_pPoly->getVariableName(), _pPoly->getRows(), _pDouble->getCols(), piRank);
645 if (bComplex1 || bComplex2)
647 (*_pPolyOut)->setComplex(true);
650 //Distribution a la mano par appels a des sous-fonctions ( iMulti...ScalarBy...Scalar ) plus iAdd...To... )
652 //for each line of _pPoly
653 for (int iRow1 = 0 ; iRow1 < _pPoly->getRows() ; iRow1++)
655 //for each col of _pDouble
656 for (int iCol2 = 0 ; iCol2 < _pDouble->getCols() ; iCol2++)
658 SinglePoly* pSPOut = (*_pPolyOut)->get(iRow1, iCol2);
661 //for each rows of _pDouble / cols of _pPoly
662 for (int iRow2 = 0 ; iRow2 < _pDouble->getRows() ; iRow2++)
664 // SinglePoly(iRow1, iRow2) * Double(iRow2, iCol2)
665 SinglePoly* pSPIn = _pPoly->get(iRow1, iRow2);
666 int iSize = pSPIn->getSize();
667 double* pdblMult = new double[iSize];
669 if (bComplex1 == false && bComplex2 == false)
671 //Real Matrix by Real Scalar
672 iMultiRealScalarByRealMatrix(_pDouble->get(iRow2, iCol2), pSPIn->get(), iSize, 1, pdblMult);
673 add(pSPOut->get(), (long long)iSize, pdblMult, pSPOut->get());
675 else if (bComplex1 == false && bComplex2 == true)
677 //Real Matrix by Scalar Complex
678 double* pdblMultImg = new double[iSize];
679 iMultiComplexScalarByRealMatrix(_pDouble->get(iRow2, iCol2), _pDouble->getImg(iRow2, iCol2), pSPIn->get(), pSPIn->getSize(), 1, pdblMult, pdblMultImg);
680 add(pSPOut->get(), pSPOut->getImg(), (long long)iSize, pdblMult, pdblMultImg, pSPOut->get(), pSPOut->getImg());
681 delete[] pdblMultImg;
683 else if (bComplex1 == true && bComplex2 == false)
685 double* pdblMultImg = new double[iSize];
686 iMultiRealScalarByComplexMatrix(_pDouble->get(iRow2, iCol2), pSPIn->get(), pSPIn->getImg(), pSPIn->getSize(), 1, pdblMult, pdblMultImg);
687 add(pSPOut->get(), pSPOut->getImg(), (long long)iSize, pdblMult, pdblMultImg, pSPOut->get(), pSPOut->getImg());
688 delete[] pdblMultImg;
690 else //if(bComplex1 == true && bComplex2 == true)
692 double* pdblMultImg = new double[iSize];
693 iMultiComplexScalarByComplexMatrix(_pDouble->get(iRow2, iCol2), _pDouble->getImg(iRow2, iCol2), pSPIn->get(), pSPIn->getImg(), pSPIn->getSize(), 1, pdblMult, pdblMultImg);
694 add(pSPOut->get(), pSPOut->getImg(), (long long)iSize, pdblMult, pdblMultImg, pSPOut->get(), pSPOut->getImg());
695 delete[] pdblMultImg;
699 }//for(int iRow2 = 0 ; iRow2 < _pDouble->getRows() ; iRow2++)
700 }//for(int iCol2 = 0 ; iCol2 < _pDouble->getCols() ; iCol2++)
701 }//for(int iRow1 = 0 ; iRow1 < _pPoly->getRows() ; iRow1++)
703 (*_pPolyOut)->updateRank();
707 int MultiplyPolyByPoly(Polynom* _pPoly1, Polynom* _pPoly2, Polynom** _pPolyOut)
709 bool bComplex1 = _pPoly1->isComplex();
710 bool bComplex2 = _pPoly2->isComplex();
712 if (_pPoly1->isScalar() && _pPoly2->isScalar())
714 //poly1(0) * poly2(0)
715 int iRank = _pPoly1->get(0)->getRank() + _pPoly2->get(0)->getRank();
716 (*_pPolyOut) = new Polynom(_pPoly1->getVariableName(), 1, 1, &iRank);
717 if (bComplex1 || bComplex2)
719 (*_pPolyOut)->setComplex(true);
722 if (bComplex1 == false && bComplex2 == false)
724 SinglePoly *pPoly1 = _pPoly1->get(0);
725 SinglePoly *pPoly2 = _pPoly2->get(0);
726 SinglePoly *pPolyOut = (*_pPolyOut)->get(0);
728 pPolyOut->setZeros();
730 iMultiScilabPolynomByScilabPolynom(
731 pPoly1->get(), pPoly1->getSize(),
732 pPoly2->get(), pPoly2->getSize(),
733 pPolyOut->get(), pPolyOut->getSize());
735 else if (bComplex1 == false && bComplex2 == true)
737 SinglePoly *pPoly1 = _pPoly1->get(0);
738 SinglePoly *pPoly2 = _pPoly2->get(0);
739 SinglePoly *pPolyOut = (*_pPolyOut)->get(0);
741 pPolyOut->setZeros();
743 iMultiScilabPolynomByComplexPoly(
744 pPoly1->get(), pPoly1->getSize(),
745 pPoly2->get(), pPoly2->getImg(), pPoly2->getSize(),
746 pPolyOut->get(), pPolyOut->getImg(), pPolyOut->getSize());
748 else if (bComplex1 == true && bComplex2 == false)
750 SinglePoly *pPoly1 = _pPoly1->get(0);
751 SinglePoly *pPoly2 = _pPoly2->get(0);
752 SinglePoly *pPolyOut = (*_pPolyOut)->get(0);
754 pPolyOut->setZeros();
756 iMultiComplexPolyByScilabPolynom(
757 pPoly1->get(), pPoly1->getImg(), pPoly1->getSize(),
758 pPoly2->get(), pPoly2->getSize(),
759 pPolyOut->get(), pPolyOut->getImg(), pPolyOut->getSize());
761 else if (bComplex1 == true && bComplex2 == true)
763 SinglePoly *pPoly1 = _pPoly1->get(0);
764 SinglePoly *pPoly2 = _pPoly2->get(0);
765 SinglePoly *pPolyOut = (*_pPolyOut)->get(0);
767 pPolyOut->setZeros();
769 iMultiComplexPolyByComplexPoly(
770 pPoly1->get(), pPoly1->getImg(), pPoly1->getSize(),
771 pPoly2->get(), pPoly2->getImg(), pPoly2->getSize(),
772 pPolyOut->get(), pPolyOut->getImg(), pPolyOut->getSize());
775 (*_pPolyOut)->updateRank();
779 if (_pPoly1->isScalar())
781 //poly1(0) * poly2(n)
782 int* piRank = new int[_pPoly2->getSize()];
783 for (int i = 0 ; i < _pPoly2->getSize() ; i++)
785 piRank[i] = _pPoly1->get(0)->getRank() + _pPoly2->get(i)->getRank();
788 (*_pPolyOut) = new Polynom(_pPoly1->getVariableName(), _pPoly2->getDims(), _pPoly2->getDimsArray(), piRank);
789 if (bComplex1 || bComplex2)
791 (*_pPolyOut)->setComplex(true);
796 SinglePoly *pPoly1 = _pPoly1->get(0);
797 if (bComplex1 == false && bComplex2 == false)
799 for (int iPoly = 0 ; iPoly < _pPoly2->getSize() ; iPoly++)
801 SinglePoly *pPoly2 = _pPoly2->get(iPoly);
802 SinglePoly *pPolyOut = (*_pPolyOut)->get(iPoly);
804 pPolyOut->setZeros();
806 iMultiScilabPolynomByScilabPolynom(
807 pPoly1->get(), pPoly1->getSize(),
808 pPoly2->get(), pPoly2->getSize(),
809 pPolyOut->get(), pPolyOut->getSize());
812 else if (bComplex1 == false && bComplex2 == true)
814 for (int iPoly = 0 ; iPoly < _pPoly2->getSize() ; iPoly++)
816 SinglePoly *pPoly2 = _pPoly2->get(iPoly);
817 SinglePoly *pPolyOut = (*_pPolyOut)->get(iPoly);
819 pPolyOut->setZeros();
821 iMultiScilabPolynomByComplexPoly(
822 pPoly1->get(), pPoly1->getSize(),
823 pPoly2->get(), pPoly2->getImg(), pPoly2->getSize(),
824 pPolyOut->get(), pPolyOut->getImg(), pPolyOut->getSize());
827 else if (bComplex1 == true && bComplex2 == false)
829 for (int iPoly = 0 ; iPoly < _pPoly2->getSize() ; iPoly++)
831 SinglePoly *pPoly2 = _pPoly2->get(iPoly);
832 SinglePoly *pPolyOut = (*_pPolyOut)->get(iPoly);
834 pPolyOut->setZeros();
836 iMultiComplexPolyByScilabPolynom(
837 pPoly1->get(), pPoly1->getImg(), pPoly1->getSize(),
838 pPoly2->get(), pPoly2->getSize(),
839 pPolyOut->get(), pPolyOut->getImg(), pPolyOut->getSize());
842 else if (bComplex1 == true && bComplex2 == true)
844 for (int iPoly = 0 ; iPoly < _pPoly2->getSize() ; iPoly++)
846 SinglePoly *pPoly2 = _pPoly2->get(iPoly);
847 SinglePoly *pPolyOut = (*_pPolyOut)->get(iPoly);
849 pPolyOut->setZeros();
851 iMultiComplexPolyByComplexPoly(
852 pPoly1->get(), pPoly1->getImg(), pPoly1->getSize(),
853 pPoly2->get(), pPoly2->getImg(), pPoly2->getSize(),
854 pPolyOut->get(), pPolyOut->getImg(), pPolyOut->getSize());
858 (*_pPolyOut)->updateRank();
862 if (_pPoly2->isScalar())
864 //poly1(n) * poly2(0)
865 int* piRank = new int[_pPoly1->getSize()];
866 for (int i = 0 ; i < _pPoly1->getSize() ; i++)
868 piRank[i] = _pPoly2->get(0)->getRank() + _pPoly1->get(i)->getRank();
871 (*_pPolyOut) = new Polynom(_pPoly1->getVariableName(), _pPoly1->getDims(), _pPoly1->getDimsArray(), piRank);
872 if (bComplex1 || bComplex2)
874 (*_pPolyOut)->setComplex(true);
878 SinglePoly *pPoly2 = _pPoly2->get(0);
879 if (bComplex1 == false && bComplex2 == false)
881 for (int iPoly = 0 ; iPoly < _pPoly1->getSize() ; iPoly++)
883 SinglePoly *pPoly1 = _pPoly1->get(iPoly);
884 SinglePoly *pPolyOut = (*_pPolyOut)->get(iPoly);
886 pPolyOut->setZeros();
888 iMultiScilabPolynomByScilabPolynom(
889 pPoly1->get(), pPoly1->getSize(),
890 pPoly2->get(), pPoly2->getSize(),
891 pPolyOut->get(), pPolyOut->getSize());
894 else if (bComplex1 == false && bComplex2 == true)
896 for (int iPoly = 0 ; iPoly < _pPoly1->getSize() ; iPoly++)
898 SinglePoly *pPoly1 = _pPoly1->get(iPoly);
899 SinglePoly *pPolyOut = (*_pPolyOut)->get(iPoly);
901 pPolyOut->setZeros();
903 iMultiScilabPolynomByComplexPoly(
904 pPoly1->get(), pPoly1->getSize(),
905 pPoly2->get(), pPoly2->getImg(), pPoly2->getSize(),
906 pPolyOut->get(), pPolyOut->getImg(), pPolyOut->getSize());
909 else if (bComplex1 == true && bComplex2 == false)
911 for (int iPoly = 0 ; iPoly < _pPoly1->getSize() ; iPoly++)
913 SinglePoly *pPoly1 = _pPoly1->get(iPoly);
914 SinglePoly *pPolyOut = (*_pPolyOut)->get(iPoly);
916 pPolyOut->setZeros();
918 iMultiComplexPolyByScilabPolynom(
919 pPoly1->get(), pPoly1->getImg(), pPoly1->getSize(),
920 pPoly2->get(), pPoly2->getSize(),
921 pPolyOut->get(), pPolyOut->getImg(), pPolyOut->getSize());
924 else if (bComplex1 == true && bComplex2 == true)
926 for (int iPoly = 0 ; iPoly < _pPoly1->getSize() ; iPoly++)
928 SinglePoly *pPoly1 = _pPoly1->get(iPoly);
929 SinglePoly *pPolyOut = (*_pPolyOut)->get(iPoly);
931 pPolyOut->setZeros();
933 iMultiComplexPolyByComplexPoly(
934 pPoly1->get(), pPoly1->getImg(), pPoly1->getSize(),
935 pPoly2->get(), pPoly2->getImg(), pPoly2->getSize(),
936 pPolyOut->get(), pPolyOut->getImg(), pPolyOut->getSize());
940 (*_pPolyOut)->updateRank();
944 if (_pPoly1->getDims() > 2 || _pPoly2->getDims() > 2 || _pPoly1->getCols() != _pPoly2->getRows())
950 int* piRank = new int[_pPoly1->getRows() * _pPoly2->getCols()];
951 int iMaxRank = _pPoly1->getMaxRank() + _pPoly2->getMaxRank();
952 for (int i = 0 ; i < _pPoly1->getRows() * _pPoly2->getCols() ; i++)
954 piRank[i] = iMaxRank;
957 (*_pPolyOut) = new Polynom(_pPoly1->getVariableName(), _pPoly1->getRows(), _pPoly2->getCols(), piRank);
958 if (bComplex1 || bComplex2)
960 (*_pPolyOut)->setComplex(true);
966 if (bComplex1 == false && bComplex2 == false)
968 double *pReal = NULL;
969 SinglePoly *pTemp = new SinglePoly(&pReal, (*_pPolyOut)->getMaxRank());
971 for (int iRow = 0 ; iRow < _pPoly1->getRows() ; iRow++)
973 for (int iCol = 0 ; iCol < _pPoly2->getCols() ; iCol++)
975 SinglePoly *pResult = (*_pPolyOut)->get(iRow, iCol);
978 for (int iCommon = 0 ; iCommon < _pPoly1->getCols() ; iCommon++)
980 SinglePoly *pL = _pPoly1->get(iRow, iCommon);
981 SinglePoly *pR = _pPoly2->get(iCommon, iCol);
985 iMultiScilabPolynomByScilabPolynom(
986 pL->get(), pL->getSize(),
987 pR->get(), pR->getSize(),
988 pTemp->get(), pL->getRank() + pR->getRank() + 1);
990 iAddScilabPolynomToScilabPolynom(
991 pResult->get(), pResult->getSize(),
992 pTemp->get(), pResult->getSize(),
993 pResult->get(), pResult->getSize());
998 else if (bComplex1 == false && bComplex2 == true)
1000 double *pReal = NULL;
1001 double *pImg = NULL;
1002 SinglePoly *pTemp = new SinglePoly(&pReal, &pImg, (*_pPolyOut)->getMaxRank());
1004 for (int iRow = 0 ; iRow < _pPoly1->getRows() ; iRow++)
1006 for (int iCol = 0 ; iCol < _pPoly2->getCols() ; iCol++)
1008 SinglePoly *pResult = (*_pPolyOut)->get(iRow, iCol);
1009 pResult->setZeros();
1011 for (int iCommon = 0 ; iCommon < _pPoly1->getCols() ; iCommon++)
1013 SinglePoly *pL = _pPoly1->get(iRow, iCommon);
1014 SinglePoly *pR = _pPoly2->get(iCommon, iCol);
1018 iMultiScilabPolynomByComplexPoly(
1019 pL->get(), pL->getSize(),
1020 pR->get(), pR->getImg(), pR->getSize(),
1021 pTemp->get(), pTemp->getImg(), pL->getRank() + pR->getRank() + 1);
1023 iAddComplexPolyToComplexPoly(
1024 pResult->get(), pResult->getImg(), pResult->getSize(),
1025 pTemp->get(), pTemp->getImg(), pResult->getSize(),
1026 pResult->get(), pResult->getImg(), pResult->getSize());
1031 else if (bComplex1 == true && bComplex2 == false)
1033 double *pReal = NULL;
1034 double *pImg = NULL;
1035 SinglePoly *pTemp = new SinglePoly(&pReal, &pImg, (*_pPolyOut)->getMaxRank());
1037 for (int iRow = 0 ; iRow < _pPoly1->getRows() ; iRow++)
1039 for (int iCol = 0 ; iCol < _pPoly2->getCols() ; iCol++)
1041 SinglePoly *pResult = (*_pPolyOut)->get(iRow, iCol);
1042 pResult->setZeros();
1044 for (int iCommon = 0 ; iCommon < _pPoly1->getCols() ; iCommon++)
1046 SinglePoly *pL = _pPoly1->get(iRow, iCommon);
1047 SinglePoly *pR = _pPoly2->get(iCommon, iCol);
1051 iMultiScilabPolynomByComplexPoly(
1052 pR->get(), pR->getSize(),
1053 pL->get(), pL->getImg(), pL->getSize(),
1054 pTemp->get(), pTemp->getImg(), pL->getRank() + pR->getRank() + 1);
1056 iAddComplexPolyToComplexPoly(
1057 pResult->get(), pResult->getImg(), pResult->getSize(),
1058 pTemp->get(), pTemp->getImg(), pResult->getSize(),
1059 pResult->get(), pResult->getImg(), pResult->getSize());
1064 else if (bComplex1 == true && bComplex2 == true)
1066 double *pReal = NULL;
1067 double *pImg = NULL;
1068 SinglePoly *pTemp = new SinglePoly(&pReal, &pImg, (*_pPolyOut)->getMaxRank());
1070 for (int iRow = 0 ; iRow < _pPoly1->getRows() ; iRow++)
1072 for (int iCol = 0 ; iCol < _pPoly2->getCols() ; iCol++)
1074 SinglePoly *pResult = (*_pPolyOut)->get(iRow, iCol);
1075 pResult->setZeros();
1077 for (int iCommon = 0 ; iCommon < _pPoly1->getCols() ; iCommon++)
1079 SinglePoly *pL = _pPoly1->get(iRow, iCommon);
1080 SinglePoly *pR = _pPoly2->get(iCommon, iCol);
1084 iMultiComplexPolyByComplexPoly(
1085 pL->get(), pL->getImg(), pL->getSize(),
1086 pR->get(), pR->getImg(), pR->getSize(),
1087 pTemp->get(), pTemp->getImg(), pL->getRank() + pR->getRank() + 1);
1089 iAddComplexPolyToComplexPoly(
1090 pResult->get(), pResult->getImg(), pResult->getSize(),
1091 pTemp->get(), pTemp->getImg(), pResult->getSize(),
1092 pResult->get(), pResult->getImg(), pResult->getSize());
1097 (*_pPolyOut)->updateRank();
1102 int MultiplySparseBySparse(Sparse* _pSparse1, Sparse* _pSparse2, Sparse** _pSparseOut)
1104 if (_pSparse1->isScalar())
1107 Double* pDbl = NULL;
1108 if (_pSparse1->isComplex())
1110 std::complex<double> dbl = _pSparse1->getImg(0, 0);
1111 pDbl = new Double(dbl.real(), dbl.imag());
1115 pDbl = new Double(_pSparse1->get(0, 0));
1118 MultiplyDoubleBySparse(pDbl, _pSparse2, (GenericType**)_pSparseOut);
1123 if (_pSparse2->isScalar())
1126 Double* pDbl = NULL;
1127 if (_pSparse2->isComplex())
1129 std::complex<double> dbl = _pSparse2->getImg(0, 0);
1130 pDbl = new Double(dbl.real(), dbl.imag());
1134 pDbl = new Double(_pSparse2->get(0, 0));
1137 MultiplySparseByDouble(_pSparse1, pDbl, (GenericType**)_pSparseOut);
1142 if (_pSparse1->getCols() != _pSparse2->getRows())
1147 *_pSparseOut = _pSparse1->multiply(*_pSparse2);
1151 int MultiplyDoubleBySparse(Double* _pDouble, Sparse *_pSparse, GenericType** _pOut)
1154 if (_pDouble->isScalar())
1157 Sparse* pOut = NULL;
1158 if (_pDouble->isComplex())
1160 std::complex<double> dbl(_pDouble->get(0), _pDouble->getImg(0));
1161 pOut = _pSparse->multiply(dbl);
1165 pOut = _pSparse->multiply(_pDouble->get(0));
1171 if (_pSparse->isScalar())
1176 if (_pSparse->isComplex())
1178 std::complex<double> dbl(_pSparse->getImg(0, 0));
1179 pD = new Double(dbl.real(), dbl.imag());
1183 pD = new Double(_pSparse->get(0, 0));
1186 InternalType* pIT = GenericDotTimes(_pDouble, pD);
1187 *_pOut = pIT->getAs<GenericType>();
1192 if (_pDouble->getCols() != _pSparse->getRows())
1197 //try to be smart and only compute for non zero values
1199 //get some information
1200 int iNonZeros = static_cast<int>(_pSparse->nonZeros());
1201 int* pRows = new int[iNonZeros * 2];
1202 _pSparse->outputRowCol(pRows);
1203 int* pCols = pRows + iNonZeros;
1204 double* pValR = new double[iNonZeros];
1205 double* pValI = new double[iNonZeros];
1206 _pSparse->outputValues(pValR, pValI);
1208 Double* pOut = new Double(_pDouble->getRows(), _pSparse->getCols(), _pDouble->isComplex() | _pSparse->isComplex());
1211 if (_pDouble->isComplex() == false && _pSparse->isComplex() == false)
1213 for (int i = 0 ; i < iNonZeros ; i++)
1215 int iRow = static_cast<int>(pRows[i]) - 1;
1216 int iCol = static_cast<int>(pCols[i]) - 1;
1217 double dbl = pValR[i];
1219 for (int j = 0 ; j < _pDouble->getRows() ; j++)
1221 double dblVal = _pDouble->get(j, iRow) * dbl;
1222 pOut->set(j, iCol, pOut->get(j, iCol) + dblVal);
1226 else if (_pDouble->isComplex() == false && _pSparse->isComplex() == true)
1228 //a * (b ci) -> ab ac
1229 for (int i = 0 ; i < iNonZeros ; i++)
1231 int iRow = static_cast<int>(pRows[i]) - 1;
1232 int iCol = static_cast<int>(pCols[i]) - 1;
1233 double dblR = pValR[i];
1234 double dblI = pValI[i];
1236 for (int j = 0 ; j < _pDouble->getRows() ; j++)
1238 double dblValR = _pDouble->get(j, iRow) * dblR;
1239 double dblValI = _pDouble->get(j, iRow) * dblI;
1240 pOut->set(j, iCol, pOut->get(j, iCol) + dblValR);
1241 pOut->setImg(j, iCol, pOut->getImg(j, iCol) + dblValI);
1245 else if (_pDouble->isComplex() == true && _pSparse->isComplex() == false)
1247 //(a bi) * c -> ac + bc
1248 for (int i = 0 ; i < iNonZeros ; i++)
1250 int iRow = static_cast<int>(pRows[i]) - 1;
1251 int iCol = static_cast<int>(pCols[i]) - 1;
1252 double dblR = pValR[i];
1254 for (int j = 0 ; j < _pDouble->getRows() ; j++)
1256 double dblValR = _pDouble->get(j, iRow) * dblR;
1257 double dblValI = _pDouble->getImg(j, iRow) * dblR;
1258 pOut->set(j, iCol, pOut->get(j, iCol) + dblValR);
1259 pOut->setImg(j, iCol, pOut->getImg(j, iCol) + dblValI);
1263 else if (_pDouble->isComplex() == true && _pSparse->isComplex() == true)
1265 for (int i = 0 ; i < iNonZeros ; i++)
1267 int iRow = static_cast<int>(pRows[i]) - 1;
1268 int iCol = static_cast<int>(pCols[i]) - 1;
1269 double dblR = pValR[i];
1270 double dblI = pValI[i];
1272 for (int j = 0 ; j < _pDouble->getRows() ; j++)
1274 double dblValR = _pDouble->get(j, iRow) * dblR - _pDouble->getImg(j, iRow) * dblI;
1275 double dblValI = _pDouble->get(j, iRow) * dblI + _pDouble->getImg(j, iRow) * dblR;
1276 pOut->set(j, iCol, pOut->get(j, iCol) + dblValR);
1277 pOut->setImg(j, iCol, pOut->getImg(j, iCol) + dblValI);
1290 int MultiplySparseByDouble(Sparse *_pSparse, Double*_pDouble, GenericType** _pOut)
1292 if (_pDouble->isScalar())
1295 Sparse* pOut = NULL;
1296 if (_pDouble->isComplex())
1298 std::complex<double> dbl(_pDouble->get(0), _pDouble->getImg(0));
1299 pOut = _pSparse->multiply(dbl);
1303 pOut = _pSparse->multiply(_pDouble->get(0));
1309 if (_pSparse->isScalar())
1314 if (_pSparse->isComplex())
1316 std::complex<double> dbl(_pSparse->getImg(0, 0));
1317 pD = new Double(dbl.real(), dbl.imag());
1321 pD = new Double(_pSparse->get(0, 0));
1324 InternalType* pIT = GenericDotTimes(_pDouble, pD);
1325 *_pOut = pIT->getAs<GenericType>();
1330 if (_pSparse->getCols() != _pDouble->getRows())
1335 //try to be smart and only compute for non zero values
1337 //get some information
1338 int iNonZeros = static_cast<int>(_pSparse->nonZeros());
1339 int* pRows = new int[iNonZeros * 2];
1340 _pSparse->outputRowCol(pRows);
1341 int* pCols = pRows + iNonZeros;
1342 double* pValR = new double[iNonZeros];
1343 double* pValI = new double[iNonZeros];
1344 _pSparse->outputValues(pValR, pValI);
1346 Double* pOut = new Double(_pSparse->getRows(), _pDouble->getCols(), _pDouble->isComplex() | _pSparse->isComplex());
1349 if (_pDouble->isComplex() == false && _pSparse->isComplex() == false)
1351 for (int i = 0 ; i < iNonZeros ; i++)
1353 int iRow = static_cast<int>(pRows[i]) - 1;
1354 int iCol = static_cast<int>(pCols[i]) - 1;
1355 double dbl = pValR[i];
1357 for (int j = 0 ; j < _pDouble->getCols() ; j++)
1359 double dblVal = _pDouble->get(iCol, j) * dbl;
1360 pOut->set(iRow, j, pOut->get(iRow, j) + dblVal);
1364 else if (_pDouble->isComplex() == false && _pSparse->isComplex() == true)
1366 //a * (b ci) -> ab ac
1367 for (int i = 0 ; i < iNonZeros ; i++)
1369 int iRow = static_cast<int>(pRows[i]) - 1;
1370 int iCol = static_cast<int>(pCols[i]) - 1;
1371 double dblR = pValR[i];
1372 double dblI = pValI[i];
1374 for (int j = 0 ; j < _pDouble->getCols() ; j++)
1376 double dblValR = _pDouble->get(iCol, j) * dblR;
1377 double dblValI = _pDouble->get(iCol, j) * dblI;
1378 pOut->set(iRow, j, pOut->get(iRow, j) + dblValR);
1379 pOut->setImg(iRow, j, pOut->getImg(iRow, j) + dblValI);
1383 else if (_pDouble->isComplex() == true && _pSparse->isComplex() == false)
1385 //(a bi) * c -> ac + bc
1386 for (int i = 0 ; i < iNonZeros ; i++)
1388 int iRow = static_cast<int>(pRows[i]) - 1;
1389 int iCol = static_cast<int>(pCols[i]) - 1;
1390 double dblR = pValR[i];
1392 for (int j = 0 ; j < _pDouble->getCols() ; j++)
1394 double dblValR = _pDouble->get(iCol, j) * dblR;
1395 double dblValI = _pDouble->getImg(iCol, j) * dblR;
1396 pOut->set(iRow, j, pOut->get(iRow, j) + dblValR);
1397 pOut->setImg(iRow, j, pOut->getImg(iRow, j) + dblValI);
1401 else if (_pDouble->isComplex() == true && _pSparse->isComplex() == true)
1403 for (int i = 0 ; i < iNonZeros ; i++)
1405 int iRow = static_cast<int>(pRows[i]) - 1;
1406 int iCol = static_cast<int>(pCols[i]) - 1;
1407 double dblR = pValR[i];
1408 double dblI = pValI[i];
1410 for (int j = 0 ; j < _pDouble->getCols() ; j++)
1412 double dblValR = _pDouble->get(iCol, j) * dblR - _pDouble->getImg(iCol, j) * dblI;
1413 double dblValI = _pDouble->get(iCol, j) * dblI + _pDouble->getImg(iCol, j) * dblR;
1414 pOut->set(iRow, j, pOut->get(iRow, j) + dblValR);
1415 pOut->setImg(iRow, j, pOut->getImg(iRow, j) + dblValI);
1428 int DotMultiplySparseBySparse(Sparse* _pSparse1, Sparse* _pSparse2, Sparse** _pOut)
1430 if (_pSparse1->isScalar() || _pSparse2->isScalar())
1432 //SP .* sp or sp .* SP
1433 return MultiplySparseBySparse(_pSparse1, _pSparse2, _pOut);
1436 if (_pSparse1->getRows() != _pSparse2->getRows() || _pSparse1->getCols() != _pSparse2->getCols())
1441 *_pOut = _pSparse1->dotMultiply(*_pSparse2);
1446 int DotMultiplyDoubleBySparse(Double* _pDouble, Sparse* _pSparse, GenericType** _pOut)
1448 if (_pDouble->isScalar())
1450 return MultiplyDoubleBySparse(_pDouble, _pSparse, _pOut);
1453 if (_pSparse->isScalar())
1455 return MultiplyDoubleBySparse(_pDouble, _pSparse, _pOut);
1458 if (_pSparse->getRows() != _pDouble->getRows() || _pSparse->getCols() != _pDouble->getCols())
1463 Sparse* pOut = new Sparse(_pDouble->getRows(), _pDouble->getCols(), _pSparse->isComplex() || _pDouble->isComplex());
1464 //get some information
1465 int iNonZeros = static_cast<int>(_pSparse->nonZeros());
1466 int* pRows = new int[iNonZeros * 2];
1467 _pSparse->outputRowCol(pRows);
1468 int* pCols = pRows + iNonZeros;
1470 if (_pDouble->isComplex() == false && _pSparse->isComplex() == false)
1472 for (int i = 0 ; i < iNonZeros ; i++)
1474 int iRow = static_cast<int>(pRows[i]) - 1;
1475 int iCol = static_cast<int>(pCols[i]) - 1;
1476 pOut->set(iRow, iCol, _pSparse->get(iRow, iCol) * _pDouble->get(iRow, iCol));
1479 else if (_pDouble->isComplex() == false && _pSparse->isComplex() == true)
1481 for (int i = 0 ; i < iNonZeros ; i++)
1483 int iRow = static_cast<int>(pRows[i]) - 1;
1484 int iCol = static_cast<int>(pCols[i]) - 1;
1485 std::complex<double> dbl = _pSparse->getImg(iRow, iCol);
1486 std::complex<double> newVal(dbl.real() * _pDouble->get(iRow, iCol), dbl.imag() * _pDouble->get(iRow, iCol));
1487 pOut->set(iRow, iCol, newVal);
1490 else if (_pDouble->isComplex() == true && _pSparse->isComplex() == false)
1492 for (int i = 0 ; i < iNonZeros ; i++)
1494 int iRow = static_cast<int>(pRows[i]) - 1;
1495 int iCol = static_cast<int>(pCols[i]) - 1;
1496 std::complex<double> dbl = _pSparse->getImg(iRow, iCol);
1497 std::complex<double> newVal(dbl.real() * _pDouble->get(iRow, iCol), dbl.real() * _pDouble->getImg(iRow, iCol));
1498 pOut->set(iRow, iCol, newVal);
1501 else if (_pDouble->isComplex() == true && _pSparse->isComplex() == true)
1503 for (int i = 0 ; i < iNonZeros ; i++)
1505 int iRow = static_cast<int>(pRows[i]) - 1;
1506 int iCol = static_cast<int>(pCols[i]) - 1;
1507 std::complex<double> dbl = _pSparse->getImg(iRow, iCol);
1508 double dblR = _pDouble->get(iRow, iCol) * dbl.real() - _pDouble->getImg(iRow, iCol) * dbl.imag();
1509 double dblI = _pDouble->getImg(iRow, iCol) * dbl.real() + _pDouble->get(iRow, iCol) * dbl.imag();
1511 std::complex<double> newVal(dblR, dblI);
1512 pOut->set(iRow, iCol, newVal);
1522 int DotMultiplySparseByDouble(Sparse* _pSparse, Double* _pDouble, GenericType** _pOut)
1524 return DotMultiplyDoubleBySparse(_pDouble, _pSparse, _pOut);
1527 int DotMultiplyPolyByDouble(Polynom* _pPoly, Double* _pDouble, Polynom** _pPolyOut)
1529 return DotMultiplyDoubleByPoly(_pDouble, _pPoly, _pPolyOut);
1532 int DotMultiplyDoubleByPoly(Double* _pDouble, Polynom* _pPoly, Polynom** _pPolyOut)
1534 int iSize = _pDouble->getSize();
1535 if (_pDouble->isScalar() == false &&
1536 _pPoly->isScalar() == false &&
1537 iSize != _pPoly->getSize())
1542 int* piRanks = new int[iSize];
1543 memset(piRanks, 0x00, iSize * sizeof(int));
1544 Polynom* pPolyTemp = new Polynom(_pPoly->getVariableName(), _pDouble->getDims(), _pDouble->getDimsArray(), piRanks);
1546 pPolyTemp->setCoef(_pDouble);
1547 int iErr = DotMultiplyPolyByPoly(pPolyTemp, _pPoly, _pPolyOut);
1552 int DotMultiplyPolyByPoly(Polynom* _pPoly1, Polynom* _pPoly2, Polynom** _pPolyOut)
1554 if (_pPoly1->isScalar() || _pPoly2->isScalar())
1556 return MultiplyPolyByPoly(_pPoly1, _pPoly2, _pPolyOut);
1560 if (_pPoly1->getSize() != _pPoly2->getSize())
1565 int* piRank = new int[_pPoly1->getSize()];
1566 for (int i = 0 ; i < _pPoly1->getSize() ; i++)
1568 piRank[i] = _pPoly1->get(i)->getRank() + _pPoly2->get(i)->getRank();
1571 (*_pPolyOut) = new Polynom(_pPoly1->getVariableName(), _pPoly1->getDims(), _pPoly1->getDimsArray(), piRank);
1573 if (_pPoly1->isComplex() && _pPoly2->isComplex())
1575 (*_pPolyOut)->setComplex(true);
1576 for (int i = 0; i < _pPoly1->getSize(); i++)
1578 SinglePoly *pSP1 = _pPoly1->get(i);
1579 SinglePoly *pSP2 = _pPoly2->get(i);
1580 SinglePoly *pSPOut = (*_pPolyOut)->get(i);
1584 iMultiComplexPolyByComplexPoly(
1585 pSP1->get(), pSP1->getImg(), pSP1->getSize(),
1586 pSP2->get(), pSP2->getImg(), pSP2->getSize(),
1587 pSPOut->get(), pSPOut->getImg(), pSPOut->getSize());
1591 else if (_pPoly1->isComplex())
1593 (*_pPolyOut)->setComplex(true);
1594 for (int i = 0; i < _pPoly1->getSize(); i++)
1596 SinglePoly *pSP1 = _pPoly1->get(i);
1597 SinglePoly *pSP2 = _pPoly2->get(i);
1598 SinglePoly *pSPOut = (*_pPolyOut)->get(i);
1602 iMultiComplexPolyByScilabPolynom(
1603 pSP1->get(), pSP1->getImg(), pSP1->getSize(),
1604 pSP2->get(), pSP2->getSize(),
1605 pSPOut->get(), pSPOut->getImg(), pSPOut->getSize());
1608 else if (_pPoly2->isComplex())
1610 (*_pPolyOut)->setComplex(true);
1611 for (int i = 0; i < _pPoly1->getSize(); i++)
1613 SinglePoly *pSP1 = _pPoly1->get(i);
1614 SinglePoly *pSP2 = _pPoly2->get(i);
1615 SinglePoly *pSPOut = (*_pPolyOut)->get(i);
1619 iMultiScilabPolynomByComplexPoly(
1620 pSP1->get(), pSP1->getSize(),
1621 pSP2->get(), pSP2->getImg(), pSP2->getSize(),
1622 pSPOut->get(), pSPOut->getImg(), pSPOut->getSize());
1627 for (int i = 0; i < _pPoly1->getSize(); i++)
1629 SinglePoly *pSP1 = _pPoly1->get(i);
1630 SinglePoly *pSP2 = _pPoly2->get(i);
1631 SinglePoly *pSPOut = (*_pPolyOut)->get(i);
1635 iMultiScilabPolynomByScilabPolynom(
1636 pSP1->get(), pSP1->getSize(),
1637 pSP2->get(), pSP2->getSize(),
1638 pSPOut->get(), pSPOut->getSize());