Пример #1
0
void
spSolve(
    spMatrix eMatrix,
    spREAL RHS[],
    spREAL Solution[]
#   if spCOMPLEX AND spSEPARATED_COMPLEX_VECTORS
        , spREAL iRHS[]
        , spREAL iSolution[]
#   endif
)
{
MatrixPtr  Matrix = (MatrixPtr)eMatrix;
register  ElementPtr  pElement;
register  RealVector  Intermediate;
register  RealNumber  Temp;
register  int  I, *pExtOrder, Size;
ElementPtr  pPivot;
void SolveComplexMatrix();

/* Begin `spSolve'. */
    ASSERT_IS_SPARSE( Matrix );
    ASSERT_NO_ERRORS( Matrix );
    ASSERT_IS_FACTORED( Matrix );

#if spCOMPLEX
    if (Matrix->Complex)
    {   SolveComplexMatrix( Matrix, RHS, Solution IMAG_VECTORS );
        return;
    }
#endif

#if REAL
    Intermediate = Matrix->Intermediate;
    Size = Matrix->Size;

/* Correct array pointers for ARRAY_OFFSET. */
#if NOT ARRAY_OFFSET
    --RHS;
    --Solution;
#endif

/* Initialize Intermediate vector. */
    pExtOrder = &Matrix->IntToExtRowMap[Size];
    for (I = Size; I > 0; I--)
        Intermediate[I] = RHS[*(pExtOrder--)];

/* Forward elimination. Solves Lc = b.*/
    for (I = 1; I <= Size; I++)
    {
/* This step of the elimination is skipped if Temp equals zero. */
        if ((Temp = Intermediate[I]) != 0.0)
        {   pPivot = Matrix->Diag[I];
            Intermediate[I] = (Temp *= pPivot->Real);

            pElement = pPivot->NextInCol;
            while (pElement != NULL)
            {   Intermediate[pElement->Row] -= Temp * pElement->Real;
                pElement = pElement->NextInCol;
            }
        }
    }

/* Backward Substitution. Solves Ux = c.*/
    for (I = Size; I > 0; I--)
    {   Temp = Intermediate[I];
        pElement = Matrix->Diag[I]->NextInRow;
        while (pElement != NULL)
        {   Temp -= pElement->Real * Intermediate[pElement->Col];
            pElement = pElement->NextInRow;
        }
        Intermediate[I] = Temp;
    }

/* Unscramble Intermediate vector while placing data in to Solution vector. */
    pExtOrder = &Matrix->IntToExtColMap[Size];
    for (I = Size; I > 0; I--)
        Solution[*(pExtOrder--)] = Intermediate[I];

    return;
#endif /* REAL */
}
Пример #2
0
void
spSolve(char *eMatrix, RealVector  RHS, RealVector  Solution IMAG_VECTORS )
{
    MatrixPtr  Matrix = (MatrixPtr)eMatrix;
    register  ElementPtr  pElement;
    register  RealVector  Intermediate;
    register  RealNumber  Temp;
    register  int  I, *pExtOrder, Size;
    ElementPtr  pPivot;

    /* Begin `spSolve'. */
    ASSERT( IS_VALID(Matrix) AND IS_FACTORED(Matrix) );

#if spCOMPLEX
    if (Matrix->Complex)
    {
        SolveComplexMatrix( Matrix, RHS, Solution IMAG_VECTORS );
        return;
    }
#endif

#if REAL
    Intermediate = Matrix->Intermediate;
    Size = Matrix->Size;

    /* Correct array pointers for ARRAY_OFFSET. */
#if NOT ARRAY_OFFSET
    --RHS;
    --Solution;
#endif

    /* Initialize Intermediate vector. */
    pExtOrder = &Matrix->IntToExtRowMap[Size];
    for (I = Size; I > 0; I--)
    {
        Intermediate[I] = RHS[*(pExtOrder--)];
    }

    /* Forward elimination. Solves Lc = b.*/
    for (I = 1; I <= Size; I++)
    {

        /* This step of the elimination is skipped if Temp equals zero. */
        if ((Temp = Intermediate[I]) != 0.0)
        {
            pPivot = Matrix->Diag[I];
            if ( pPivot != 0 && ELEMENT_MAG(pPivot) >  Matrix->AbsThreshold )
            {
                /*jpc    Intermediate[I] = (Temp *= pPivot->Real);*/
                Intermediate[I] = (Temp /= pPivot->Real);

                pElement = pPivot->NextInCol;
                while (pElement != NULL)
                {
                    Intermediate[pElement->Row] -= Temp * pElement->Real;
                    pElement = pElement->NextInCol;
                }
            }
            else
            {
                Intermediate[I] = 0.0;
            }
        }
    }

    /* Backward Substitution. Solves Ux = c.*/
    /* modification for singular matrix a diagonal element can be a null pointer */

    for (I = Size ; I > 0; I--)
    {
        Temp = Intermediate[I];
        if ( Matrix->Diag[I] == 0) /* test for nul pointer */
        {
            Intermediate[I] = 0.0;
        }
        else
        {
            pElement = Matrix->Diag[I]->NextInRow;
            while (pElement != NULL)
            {
                Temp -= pElement->Real * Intermediate[pElement->Col];
                pElement = pElement->NextInRow;
            }
            Intermediate[I] = Temp;
        }
    }

    /* Unscramble Intermediate vector while placing data in to Solution vector. */
    pExtOrder = &Matrix->IntToExtColMap[Size];
    for (I = Size; I > 0; I--)
    {
        Solution[*(pExtOrder--)] = Intermediate[I];
    }

    return;
#endif /* REAL */
}
Пример #3
0
void
spSolve(MatrixPtr Matrix, RealVector RHS, RealVector Solution,
	RealVector iRHS, RealVector iSolution)
{
    ElementPtr  pElement;
    RealVector  Intermediate;
    RealNumber  Temp;
    int  I, *pExtOrder, Size;
    ElementPtr  pPivot;

    /* Begin `spSolve'. */
    assert( IS_VALID(Matrix) && IS_FACTORED(Matrix) );

    if (Matrix->Complex)
    {
	SolveComplexMatrix( Matrix, RHS, Solution, iRHS, iSolution );
        return;
    }

    Intermediate = Matrix->Intermediate;
    Size = Matrix->Size;

    /* Initialize Intermediate vector. */
    pExtOrder = &Matrix->IntToExtRowMap[Size];
    for (I = Size; I > 0; I--)
        Intermediate[I] = RHS[*(pExtOrder--)];

    /* Forward elimination. Solves Lc = b.*/
    for (I = 1; I <= Size; I++)
    {
   
	/* This step of the elimination is skipped if Temp equals zero. */
        if ((Temp = Intermediate[I]) != 0.0)
        {
	    pPivot = Matrix->Diag[I];
            Intermediate[I] = (Temp *= pPivot->Real);

            pElement = pPivot->NextInCol;
            while (pElement != NULL)
            {
		Intermediate[pElement->Row] -= Temp * pElement->Real;
                pElement = pElement->NextInCol;
            }
        }
    }

    /* Backward Substitution. Solves Ux = c.*/
    for (I = Size; I > 0; I--)
    {
	Temp = Intermediate[I];
        pElement = Matrix->Diag[I]->NextInRow;
        while (pElement != NULL)
        {
	    Temp -= pElement->Real * Intermediate[pElement->Col];
            pElement = pElement->NextInRow;
        }
        Intermediate[I] = Temp;
    }

    /* Unscramble Intermediate vector while placing data in to Solution vector. */
    pExtOrder = &Matrix->IntToExtColMap[Size];
    for (I = Size; I > 0; I--)
        Solution[*(pExtOrder--)] = Intermediate[I];

    return;
}