Esempio n. 1
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;
}
Esempio n. 2
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;
}