bool ImplicitList::toString(std::wostringstream& ostr) { ostr << L" "; if (m_eStartType == ScilabDouble) { Double *pD = m_poStart->getAs<Double>(); ostr << printDouble(pD); } else //Polynom { Polynom* pMP = m_poStart->getAs<types::Polynom>(); ostr << printInLinePoly(pMP->get(0), pMP->getVariableName()); } ostr << L":"; if (m_eStepType == ScilabDouble) { Double *pD = m_poStep->getAs<Double>(); ostr << printDouble(pD); } else //Polynom { Polynom* pMP = m_poStep->getAs<types::Polynom>(); ostr << printInLinePoly(pMP->get(0), pMP->getVariableName()); } ostr << L":"; if (m_eEndType == ScilabDouble) { Double *pD = m_poEnd->getAs<Double>(); ostr << printDouble(pD); } else //Polynom { Polynom* pMP = m_poEnd->getAs<types::Polynom>(); ostr << printInLinePoly(pMP->get(0), pMP->getVariableName()); } ostr << std::endl; return true; }
void Polynom::sub(const Polynom &o) // works as -= { if (o.size() > size()) { a.resize(o.size(), 0); } int sz = std::min(o.size(), size()); for (int i = 0; i < sz; i ++) a[i] -= o.get(i); }
int DotPowerPolyByDouble(Polynom* _pPoly, Double* _pDouble, InternalType** _pOut) { if (_pDouble->isEmpty()) { //p .^ [] *_pOut = Double::Empty(); return 0; } int iSize = _pPoly->getSize(); if (_pPoly->isScalar()) { return PowerPolyByDouble(_pPoly, _pDouble, _pOut); } Double** pDblPower = new Double*[iSize]; double* pdblPower = _pDouble->get(); if (_pDouble->isScalar()) { if (pdblPower[0] < 0) { //call overload _pOut = NULL; delete[] pDblPower; return 0; } for (int i = 0; i < iSize; i++) { pDblPower[i] = new Double(pdblPower[0]); } } else if (_pDouble->getSize() == iSize) { for (int i = 0; i < iSize; i++) { if (pdblPower[i] < 0) { //call overload _pOut = NULL; delete[] pDblPower; return 0; } pDblPower[i] = new Double(pdblPower[i]); } } else { delete[] pDblPower; throw ast::InternalError(_W("Invalid exponent.\n")); } InternalType* pITTempOut = NULL; Polynom* pPolyTemp = new Polynom(_pPoly->getVariableName(), 1, 1); Polynom* pPolyOut = new Polynom(_pPoly->getVariableName(), _pPoly->getDims(), _pPoly->getDimsArray()); SinglePoly** pSPOut = pPolyOut->get(); SinglePoly** pSPTemp = pPolyTemp->get(); SinglePoly** pSP = _pPoly->get(); int iResult = 0; for (int i = 0; i < iSize; i++) { // set singlePoly of _pPoly in pPolyTemp without copy pSPTemp[0] = pSP[i]; iResult = PowerPolyByDouble(pPolyTemp, pDblPower[i], &pITTempOut); if (iResult) { break; } // get singlePoly of pITTempOut and set it in pPolyOut without copy SinglePoly** pSPTempOut = pITTempOut->getAs<Polynom>()->get(); pSPOut[i] = pSPTempOut[0]; // increase ref to avoid the delete of pSPTempOut[0] // which are setted in pSPOut without copy. pSPOut[i]->IncreaseRef(); delete pITTempOut; pSPOut[i]->DecreaseRef(); } //delete exp for(int i = 0; i < iSize; i++) { delete pDblPower[i]; } delete[] pDblPower; // delete temporary polynom // do not delete the last SinglePoly of _pPoly setted without copy in pPolyTemp pSPTemp[0]->IncreaseRef(); delete pPolyTemp; pSP[iSize - 1]->DecreaseRef(); switch (iResult) { case 1 : { delete pPolyOut; throw ast::InternalError(_W("Inconsistent row/column dimensions.\n")); } case 2 : { delete pPolyOut; throw ast::InternalError(_W("Invalid exponent.\n")); } default: //OK break; } *_pOut = pPolyOut; return 0; }
int PowerPolyByDouble(Polynom* _pPoly, Double* _pDouble, InternalType** _pOut) { bool bComplex1 = _pPoly->isComplex(); bool bScalar1 = _pPoly->isScalar(); double* bImg = _pDouble->getImg(); bool bNumericallyComplex1 = _pDouble->isNumericallyComplex(); if(!bNumericallyComplex1) { return 2; } if (_pDouble->isEmpty()) { //p ** [] *_pOut = Double::Empty(); return 0; } if (bScalar1) { //p ^ x or p ^ X int iRank = 0; int* piRank = new int[_pDouble->getSize()]; _pPoly->getRank(&iRank); for (int i = 0 ; i < _pDouble->getSize() ; i++) { int iInputRank = (int)_pDouble->get(i); if (iInputRank < 0) { //call overload _pOut = NULL; delete[] piRank; return 0; } piRank[i] = iRank * iInputRank; } Polynom* pOut = new Polynom(_pPoly->getVariableName(), _pDouble->getRows(), _pDouble->getCols(), piRank); delete[] piRank; pOut->setComplex(bComplex1); for (int i = 0 ; i < _pDouble->getSize() ; i++) { SinglePoly* pCoeffOut = pOut->get(i); int iCurrentRank = 0; int iLoop = (int)_pDouble->get(i); //initialize Out to 1 pCoeffOut->set(0, 1); //get a copy of p Polynom* pP = _pPoly->clone()->getAs<Polynom>(); pP->setComplex(_pPoly->isComplex()); while (iLoop) { SinglePoly* ps = pP->get()[0]; if (iLoop % 2) { int iRank = pP->getMaxRank(); if (bComplex1) { C2F(wpmul1)(pCoeffOut->get(), pCoeffOut->getImg(), &iCurrentRank, ps->get(), ps->getImg(), &iRank, pCoeffOut->get(), pCoeffOut->getImg()); } else { C2F(dpmul1)(pCoeffOut->get(), &iCurrentRank, ps->get(), &iRank, pCoeffOut->get()); } iCurrentRank += iRank; } iLoop /= 2; if (iLoop) { //p = p * p Polynom* pTemp = NULL; MultiplyPolyByPoly(pP, pP, &pTemp); pP->killMe(); pP = pTemp; } } pP->killMe(); } *_pOut = pOut; } return 0; }
int RDividePolyByDouble(Polynom* _pPoly, Double* _pDouble, Polynom** _pPolyOut) { bool bComplex1 = _pPoly->isComplex(); bool bComplex2 = _pDouble->isComplex(); bool bScalar1 = _pPoly->getRows() == 1 && _pPoly->getCols() == 1; bool bScalar2 = _pDouble->getRows() == 1 && _pDouble->getCols() == 1; Polynom *pTemp = NULL; //use only if _pPoly is scalar and _pDouble not. int iRowResult = 0; int iColResult = 0; int *piRank = NULL; /* if(bScalar1 && bScalar2) { iRowResult = 1; iColResult = 1; piRank = new int[1]; piRank[0] = _pPoly->get(0)->getRank(); } else */ if (bScalar1 == false && bScalar2 == false) { // call overload return 0; } if (bScalar2) { double dblDivR = _pDouble->get(0); double dblDivI = _pDouble->getImg(0); (*_pPolyOut) = _pPoly->clone()->getAs<Polynom>(); if (_pDouble->isComplex()) { (*_pPolyOut)->setComplex(true); } for (int i = 0 ; i < _pPoly->getSize() ; i++) { bool bComplex1 = _pPoly->isComplex(); bool bComplex2 = _pDouble->isComplex(); SinglePoly* pC = (*_pPolyOut)->get(i); if (bComplex1 == false && bComplex2 == false) { iRightDivisionRealMatrixByRealMatrix(pC->get(), 1, &dblDivR, 0, pC->get(), 1, pC->getSize()); } else if (bComplex1 == true && bComplex2 == false) { iRightDivisionComplexMatrixByRealMatrix(pC->get(), pC->getImg(), 1, &dblDivR, 0, pC->get(), pC->getImg(), 1, pC->getSize()); } else if (bComplex1 == false && bComplex2 == true) { iRightDivisionRealMatrixByComplexMatrix(pC->get(), 1, &dblDivR, &dblDivI, 0, pC->get(), pC->getImg(), 1, pC->getSize()); } else if (bComplex1 == true && bComplex2 == true) { iRightDivisionComplexMatrixByComplexMatrix(pC->get(), pC->getImg(), 1, &dblDivR, &dblDivI, 0, pC->get(), pC->getImg(), 1, pC->getSize()); } } return 0; } if (bScalar1) { //in this case, we have to create a temporary square polinomial matrix iRowResult = _pDouble->getCols(); iColResult = _pDouble->getRows(); piRank = new int[iRowResult * iRowResult]; int iMaxRank = _pPoly->getMaxRank(); for (int i = 0 ; i < iRowResult * iRowResult ; i++) { piRank[i] = iMaxRank; } pTemp = new Polynom(_pPoly->getVariableName(), iRowResult, iRowResult, piRank); if (bComplex1 || bComplex2) { pTemp->setComplex(true); } SinglePoly *pdblData = _pPoly->get(0); for (int i = 0 ; i < iRowResult ; i++) { pTemp->set(i, i, pdblData); } } (*_pPolyOut) = new Polynom(_pPoly->getVariableName(), iRowResult, iColResult, piRank); delete[] piRank; if (bComplex1 || bComplex2) { (*_pPolyOut)->setComplex(true); } if (bScalar2) { //[p] * cst for (int i = 0 ; i < _pPoly->getSize() ; i++) { SinglePoly *pPolyIn = _pPoly->get(i); double* pRealIn = pPolyIn->get(); double* pImgIn = pPolyIn->getImg(); SinglePoly *pPolyOut = (*_pPolyOut)->get(i); double* pRealOut = pPolyOut->get(); double* pImgOut = pPolyOut->getImg(); if (bComplex1 == false && bComplex2 == false) { iRightDivisionRealMatrixByRealMatrix( pRealIn, 1, _pDouble->getReal(), 0, pRealOut, 1, pPolyOut->getSize()); } else if (bComplex1 == false && bComplex2 == true) { iRightDivisionRealMatrixByComplexMatrix( pRealIn, 1, _pDouble->getReal(), _pDouble->getImg(), 0, pRealOut, pImgOut, 1, pPolyOut->getSize()); } else if (bComplex1 == true && bComplex2 == false) { iRightDivisionComplexMatrixByRealMatrix( pRealIn, pImgIn, 1, _pDouble->getReal(), 0, pRealOut, pImgOut, 1, pPolyOut->getSize()); } else if (bComplex1 == true && bComplex2 == true) { iRightDivisionComplexMatrixByComplexMatrix( pRealIn, pImgIn, 1, _pDouble->getReal(), _pDouble->getImg(), 0, pRealOut, pImgOut, 1, pPolyOut->getSize()); } } } else if (bScalar1) { for (int i = 0 ; i < pTemp->get(0)->getSize() ; i++) { Double *pCoef = pTemp->extractCoef(i); Double *pResultCoef = new Double(iRowResult, iColResult, pCoef->isComplex()); double *pReal = pResultCoef->getReal(); double *pImg = pResultCoef->getImg(); if (bComplex1 == false && bComplex2 == false) { double dblRcond = 0; iRightDivisionOfRealMatrix( pCoef->getReal(), iRowResult, iRowResult, _pDouble->getReal(), _pDouble->getRows(), _pDouble->getCols(), pReal, iRowResult, iColResult, &dblRcond); } else { double dblRcond = 0; iRightDivisionOfComplexMatrix( pCoef->getReal(), pCoef->getImg(), iRowResult, iRowResult, _pDouble->getReal(), _pDouble->getImg(), _pDouble->getRows(), _pDouble->getCols(), pReal, pImg, iRowResult, iColResult, &dblRcond); } (*_pPolyOut)->insertCoef(i, pResultCoef); delete pCoef; delete pResultCoef; } } return 0; }