Exemplo n.º 1
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;
}