From 990d3a92d37e7c54627d8178c7cc01cac53a747d Mon Sep 17 00:00:00 2001 From: dwarning Date: Sun, 18 Jun 2023 10:07:09 +0200 Subject: [PATCH] llvm format --- src/include/ngspice/smpdefs.h | 50 +- src/include/ngspice/spmatrix.h | 320 +++-- src/maths/sparse/spAllocate.c | 535 +++----- src/maths/sparse/spBuild.c | 957 ++++++-------- src/maths/sparse/spConfig.h | 178 ++- src/maths/sparse/spDefs.h | 834 ++++++------ src/maths/sparse/spExtra.c | 23 +- src/maths/sparse/spFactor.c | 2228 ++++++++++++++------------------ src/maths/sparse/spOutput.c | 541 ++++---- src/maths/sparse/spSMP.c | 331 ++--- src/maths/sparse/spSolve.c | 390 +++--- src/maths/sparse/spUtils.c | 1544 ++++++++++------------ 12 files changed, 3423 insertions(+), 4508 deletions(-) diff --git a/src/include/ngspice/smpdefs.h b/src/include/ngspice/smpdefs.h index 70766d3a5..6bbd9d528 100644 --- a/src/include/ngspice/smpdefs.h +++ b/src/include/ngspice/smpdefs.h @@ -1,8 +1,8 @@ #ifndef ngspice_SMPDEFS_H #define ngspice_SMPDEFS_H -typedef struct MatrixFrame SMPmatrix; -typedef struct MatrixElement SMPelement; +typedef struct MatrixFrame SMPmatrix; +typedef struct MatrixElement SMPelement; /********** Copyright 1990 Regents of the University of California. All rights reserved. @@ -10,32 +10,32 @@ Author: 1985 Thomas L. Quarles Modified: 2000 AlansFixes **********/ -#include -#include #include "ngspice/complex.h" +#include +#include -int SMPaddElt( SMPmatrix *, int , int , double ); -double * SMPmakeElt( SMPmatrix * , int , int ); -void SMPcClear( SMPmatrix *); -void SMPclear( SMPmatrix *); -int SMPcLUfac( SMPmatrix *, double ); -int SMPluFac( SMPmatrix *, double , double ); -int SMPcReorder( SMPmatrix * , double , double , int *); -int SMPreorder( SMPmatrix * , double , double , double ); -void SMPcaSolve(SMPmatrix *Matrix, double RHS[], double iRHS[], - double Spare[], double iSpare[]); -void SMPcSolve( SMPmatrix *, double [], double [], double [], double []); -void SMPsolve( SMPmatrix *, double [], double []); -int SMPmatSize( SMPmatrix *); -int SMPnewMatrix( SMPmatrix **, int ); -void SMPdestroy( SMPmatrix *); -int SMPpreOrder( SMPmatrix *); -void SMPprint( SMPmatrix * , char *); -void SMPprintRHS( SMPmatrix * , char *, double*, double*); -void SMPgetError( SMPmatrix *, int *, int *); -int SMPcProdDiag( SMPmatrix *, SPcomplex *, int *); +int SMPaddElt(SMPmatrix *, int, int, double); +double *SMPmakeElt(SMPmatrix *, int, int); +void SMPcClear(SMPmatrix *); +void SMPclear(SMPmatrix *); +int SMPcLUfac(SMPmatrix *, double); +int SMPluFac(SMPmatrix *, double, double); +int SMPcReorder(SMPmatrix *, double, double, int *); +int SMPreorder(SMPmatrix *, double, double, double); +void SMPcaSolve(SMPmatrix *Matrix, double RHS[], double iRHS[], double Spare[], + double iSpare[]); +void SMPcSolve(SMPmatrix *, double[], double[], double[], double[]); +void SMPsolve(SMPmatrix *, double[], double[]); +int SMPmatSize(SMPmatrix *); +int SMPnewMatrix(SMPmatrix **, int); +void SMPdestroy(SMPmatrix *); +int SMPpreOrder(SMPmatrix *); +void SMPprint(SMPmatrix *, char *); +void SMPprintRHS(SMPmatrix *, char *, double *, double *); +void SMPgetError(SMPmatrix *, int *, int *); +int SMPcProdDiag(SMPmatrix *, SPcomplex *, int *); int SMPcDProd(SMPmatrix *Matrix, SPcomplex *pMantissa, int *pExponent); -SMPelement * SMPfindElt( SMPmatrix *, int , int , int ); +SMPelement *SMPfindElt(SMPmatrix *, int, int, int); int SMPcZeroCol(SMPmatrix *Matrix, int Col); int SMPcAddCol(SMPmatrix *Matrix, int Accum_Col, int Addend_Col); void SMPconstMult(SMPmatrix *, double); diff --git a/src/include/ngspice/spmatrix.h b/src/include/ngspice/spmatrix.h index c4151aca7..c6b61aba4 100644 --- a/src/include/ngspice/spmatrix.h +++ b/src/include/ngspice/spmatrix.h @@ -17,7 +17,6 @@ * Kenneth S. Kundert */ - /* * Revision and copyright information. * @@ -27,10 +26,7 @@ * $Revision: 1.2 $ */ - - - -#ifndef spOKAY +#ifndef spOKAY /* * IMPORTS @@ -40,13 +36,9 @@ * Macros that customize the sparse matrix routines. */ +#include #include "../../maths/sparse/spConfig.h" - - - - - /* * ERROR KEYWORDS * @@ -103,38 +95,34 @@ // * warnings. // */ -#include "ngspice/sperror.h" /* Spice error definitions. */ +#include "ngspice/sperror.h" /* Spice error definitions. */ /* Begin error macros. */ -#define spOKAY OK -#define spSMALL_PIVOT OK -#define spZERO_DIAG E_SINGULAR -#define spSINGULAR E_SINGULAR -#define spNO_MEMORY E_NOMEM -#define spPANIC E_BADMATRIX -#define spMANGLED E_BADMATRIX - -#define spFATAL E_BADMATRIX - - +#define spOKAY OK +#define spSMALL_PIVOT OK +#define spZERO_DIAG E_SINGULAR +#define spSINGULAR E_SINGULAR +#define spNO_MEMORY E_NOMEM +#define spPANIC E_BADMATRIX +#define spMANGLED E_BADMATRIX +#define spFATAL E_BADMATRIX /* * KEYWORD DEFINITIONS */ -#define spREAL double /*!< - * Defines the precision of the arithmetic used by - * \a Sparse will use. Double precision is suggested - * as being most appropriate for circuit simulation - * and for C. However, it is possible to change spREAL - * to a float for single precision arithmetic. Note - * that in C, single precision arithmetic is often - * slower than double precision. Sparse - * internally refers to spREALs as RealNumbers. - */ - - +#define spREAL \ + double /*!< \ + * Defines the precision of the arithmetic used by \ + * \a Sparse will use. Double precision is suggested \ + * as being most appropriate for circuit simulation \ + * and for C. However, it is possible to change spREAL \ + * to a float for single precision arithmetic. Note \ + * that in C, single precision arithmetic is often \ + * slower than double precision. Sparse \ + * internally refers to spREALs as RealNumbers. \ + */ /* * PARTITION TYPES @@ -159,37 +147,37 @@ /* Begin partition keywords. */ -#define spDEFAULT_PARTITION 0 /*!< - * Partition code for spPartition(). - * Indicates that the default partitioning - * mode should be used. - * \see spPartition() - */ -#define spDIRECT_PARTITION 1 /*!< - * Partition code for spPartition(). - * Indicates that all rows should be placed - * in the direct addressing partition. - * \see spPartition() - */ -#define spINDIRECT_PARTITION 2 /*!< - * Partition code for spPartition(). - * Indicates that all rows should be placed - * in the indirect addressing partition. - * \see spPartition() - */ -#define spAUTO_PARTITION 3 /*!< - * Partition code for spPartition(). - * Indicates that \a Sparse should chose - * the best partition for each row based - * on some simple rules. This is generally - * preferred. - * \see spPartition() - */ +#define spDEFAULT_PARTITION \ + 0 /*!< \ + * Partition code for spPartition(). \ + * Indicates that the default partitioning \ + * mode should be used. \ + * \see spPartition() \ + */ +#define spDIRECT_PARTITION \ + 1 /*!< \ + * Partition code for spPartition(). \ + * Indicates that all rows should be placed \ + * in the direct addressing partition. \ + * \see spPartition() \ + */ +#define spINDIRECT_PARTITION \ + 2 /*!< \ + * Partition code for spPartition(). \ + * Indicates that all rows should be placed \ + * in the indirect addressing partition. \ + * \see spPartition() \ + */ +#define spAUTO_PARTITION \ + 3 /*!< \ + * Partition code for spPartition(). \ + * Indicates that \a Sparse should chose \ + * the best partition for each row based \ + * on some simple rules. This is generally \ + * preferred. \ + * \see spPartition() \ + */ - - - - /* * MACRO FUNCTION DEFINITIONS */ @@ -198,66 +186,64 @@ /*! * Macro function that adds data to a real element in the matrix by a pointer. */ -#define spADD_REAL_ELEMENT(element,real) *(element) += real +#define spADD_REAL_ELEMENT(element, real) *(element) += real /*! * Macro function that adds data to a imaginary element in the matrix by * a pointer. */ -#define spADD_IMAG_ELEMENT(element,imag) *(element+1) += imag +#define spADD_IMAG_ELEMENT(element, imag) *(element + 1) += imag /*! * Macro function that adds data to a complex element in the matrix by * a pointer. */ -#define spADD_COMPLEX_ELEMENT(element,real,imag) \ -{ *(element) += real; \ - *(element+1) += imag; \ -} +#define spADD_COMPLEX_ELEMENT(element, real, imag) \ + { \ + *(element) += real; \ + *(element + 1) += imag; \ + } /*! * Macro function that adds data to each of the four real matrix elements * specified by the given template. */ -#define spADD_REAL_QUAD(template,real) \ -{ *((template).Element1) += real; \ - *((template).Element2) += real; \ - *((template).Element3Negated) -= real; \ - *((template).Element4Negated) -= real; \ -} +#define spADD_REAL_QUAD(template, real) \ + { \ + *((template).Element1) += real; \ + *((template).Element2) += real; \ + *((template).Element3Negated) -= real; \ + *((template).Element4Negated) -= real; \ + } /*! * Macro function that adds data to each of the four imaginary matrix * elements specified by the given template. */ -#define spADD_IMAG_QUAD(template,imag) \ -{ *((template).Element1+1) += imag; \ - *((template).Element2+1) += imag; \ - *((template).Element3Negated+1) -= imag; \ - *((template).Element4Negated+1) -= imag; \ -} +#define spADD_IMAG_QUAD(template, imag) \ + { \ + *((template).Element1 + 1) += imag; \ + *((template).Element2 + 1) += imag; \ + *((template).Element3Negated + 1) -= imag; \ + *((template).Element4Negated + 1) -= imag; \ + } /*! * Macro function that adds data to each of the four complex matrix * elements specified by the given template. */ -#define spADD_COMPLEX_QUAD(template,real,imag) \ -{ *((template).Element1) += real; \ - *((template).Element2) += real; \ - *((template).Element3Negated) -= real; \ - *((template).Element4Negated) -= real; \ - *((template).Element1+1) += imag; \ - *((template).Element2+1) += imag; \ - *((template).Element3Negated+1) -= imag; \ - *((template).Element4Negated+1) -= imag; \ -} +#define spADD_COMPLEX_QUAD(template, real, imag) \ + { \ + *((template).Element1) += real; \ + *((template).Element2) += real; \ + *((template).Element3Negated) -= real; \ + *((template).Element4Negated) -= real; \ + *((template).Element1 + 1) += imag; \ + *((template).Element2 + 1) += imag; \ + *((template).Element3Negated + 1) -= imag; \ + *((template).Element4Negated + 1) -= imag; \ + } - - - - - - /* * TYPE DEFINITION FOR EXTERNAL MATRIX ELEMENT REFERENCES * @@ -271,12 +257,8 @@ typedef spGenericPtr spMatrix; typedef spREAL spElement; /*! Declares the type of the Sparse error codes. */ -//typedef int spError; +// typedef int spError; - - - - /* TYPE DEFINITION FOR COMPONENT TEMPLATE */ /*! * This data structure is used to hold pointers to four related elements in @@ -291,18 +273,15 @@ typedef spREAL spElement; */ /* Begin `spTemplate'. */ -struct spTemplate -{ spElement *Element1; - spElement *Element2; - spElement *Element3Negated; - spElement *Element4Negated; +struct spTemplate { + spElement *Element1; + spElement *Element2; + spElement *Element3Negated; + spElement *Element4Negated; }; - - typedef struct MatrixFrame *MatrixPtr; - /* * FUNCTION TYPE DEFINITIONS * @@ -311,78 +290,69 @@ typedef struct MatrixFrame *MatrixPtr; /* Begin function declarations. */ -spcEXTERN void spClear( MatrixPtr ); -spcEXTERN spREAL spCondition( MatrixPtr, spREAL, int* ); -spcEXTERN MatrixPtr spCreate( int, int, int* ); -spcEXTERN void spDeleteRowAndCol( MatrixPtr, int, int ); -spcEXTERN void spDestroy( MatrixPtr ); -spcEXTERN int spElementCount( MatrixPtr ); -spcEXTERN int spOriginalCount( MatrixPtr ); -spcEXTERN int spError( MatrixPtr ); +spcEXTERN void spClear(MatrixPtr); +spcEXTERN spREAL spCondition(MatrixPtr, spREAL, int *); +spcEXTERN MatrixPtr spCreate(int, int, int *); +spcEXTERN void spDeleteRowAndCol(MatrixPtr, int, int); +spcEXTERN void spDestroy(MatrixPtr); +spcEXTERN int spElementCount(MatrixPtr); +spcEXTERN int spOriginalCount(MatrixPtr); +spcEXTERN int spError(MatrixPtr); #ifdef EOF - spcEXTERN void spErrorMessage( MatrixPtr, FILE*, char* ); +spcEXTERN void spErrorMessage(MatrixPtr, FILE *, char *); #else -# define spErrorMessage(a,b,c) spcFUNC_NEEDS_FILE(_spErrorMessage,stdio) +#define spErrorMessage(a, b, c) spcFUNC_NEEDS_FILE(_spErrorMessage, stdio) #endif - -spcEXTERN int spFactor( MatrixPtr ); -spcEXTERN int spFileMatrix( MatrixPtr, char*, char*, int, int, int ); -spcEXTERN int spFileStats( MatrixPtr, char*, char* ); -spcEXTERN int spFillinCount( MatrixPtr ); -spcEXTERN spElement *spFindElement( MatrixPtr, int, int ); -spcEXTERN int spGetAdmittance( MatrixPtr, int, int, - struct spTemplate* ); -spcEXTERN spElement *spGetElement( MatrixPtr, int, int ); -spcEXTERN spGenericPtr spGetInitInfo( spElement* ); -spcEXTERN int spGetOnes( MatrixPtr, int, int, int, - struct spTemplate* ); -spcEXTERN int spGetQuad( MatrixPtr, int, int, int, int, - struct spTemplate* ); -spcEXTERN int spGetSize( MatrixPtr, int ); -spcEXTERN int spInitialize( MatrixPtr, int (*pInit)(spElement *, spGenericPtr, int, int) ); -spcEXTERN void spInstallInitInfo( spElement*, spGenericPtr ); -spcEXTERN spREAL spLargestElement( MatrixPtr ); -spcEXTERN void spMNA_Preorder( MatrixPtr ); -spcEXTERN spREAL spNorm( MatrixPtr ); -spcEXTERN int spOrderAndFactor( MatrixPtr, spREAL[], spREAL, - spREAL, int ); -spcEXTERN void spPartition( MatrixPtr, int ); -spcEXTERN void spPrint( MatrixPtr, int, int, int ); -spcEXTERN spREAL spPseudoCondition( MatrixPtr ); -spcEXTERN spREAL spRoundoff( MatrixPtr, spREAL ); -spcEXTERN void spScale( MatrixPtr, spREAL[], spREAL[] ); -spcEXTERN void spSetComplex( MatrixPtr ); -spcEXTERN void spSetReal( MatrixPtr ); -spcEXTERN void spStripFills( MatrixPtr ); -spcEXTERN void spWhereSingular( MatrixPtr, int*, int* ); -spcEXTERN void spConstMult( MatrixPtr, double ); +spcEXTERN int spFactor(MatrixPtr); +spcEXTERN int spFileMatrix(MatrixPtr, char *, char *, int, int, int); +spcEXTERN int spFileStats(MatrixPtr, char *, char *); +spcEXTERN int spFillinCount(MatrixPtr); +spcEXTERN spElement *spFindElement(MatrixPtr, int, int); +spcEXTERN int spGetAdmittance(MatrixPtr, int, int, struct spTemplate *); +spcEXTERN spElement *spGetElement(MatrixPtr, int, int); +spcEXTERN spGenericPtr spGetInitInfo(spElement *); +spcEXTERN int spGetOnes(MatrixPtr, int, int, int, struct spTemplate *); +spcEXTERN int spGetQuad(MatrixPtr, int, int, int, int, struct spTemplate *); +spcEXTERN int spGetSize(MatrixPtr, int); +spcEXTERN int spInitialize(MatrixPtr, + int (*pInit)(spElement *, spGenericPtr, int, int)); +spcEXTERN void spInstallInitInfo(spElement *, spGenericPtr); +spcEXTERN spREAL spLargestElement(MatrixPtr); +spcEXTERN void spMNA_Preorder(MatrixPtr); +spcEXTERN spREAL spNorm(MatrixPtr); +spcEXTERN int spOrderAndFactor(MatrixPtr, spREAL[], spREAL, spREAL, int); +spcEXTERN void spPartition(MatrixPtr, int); +spcEXTERN void spPrint(MatrixPtr, int, int, int); +spcEXTERN spREAL spPseudoCondition(MatrixPtr); +spcEXTERN spREAL spRoundoff(MatrixPtr, spREAL); +spcEXTERN void spScale(MatrixPtr, spREAL[], spREAL[]); +spcEXTERN void spSetComplex(MatrixPtr); +spcEXTERN void spSetReal(MatrixPtr); +spcEXTERN void spStripFills(MatrixPtr); +spcEXTERN void spWhereSingular(MatrixPtr, int *, int *); +spcEXTERN void spConstMult(MatrixPtr, double); /* Functions with argument lists that are dependent on options. */ #if spCOMPLEX -spcEXTERN void spDeterminant( MatrixPtr, int*, spREAL*, spREAL* ); -#else /* NOT spCOMPLEX */ -spcEXTERN void spDeterminant( MatrixPtr, int*, spREAL* ); +spcEXTERN void spDeterminant(MatrixPtr, int *, spREAL *, spREAL *); +#else /* NOT spCOMPLEX */ +spcEXTERN void spDeterminant(MatrixPtr, int *, spREAL *); #endif /* NOT spCOMPLEX */ #if spCOMPLEX && spSEPARATED_COMPLEX_VECTORS -spcEXTERN int spFileVector( MatrixPtr, char* , - spREAL[], spREAL[]); -spcEXTERN void spMultiply( MatrixPtr, spREAL[], spREAL[], - spREAL[], spREAL[] ); -spcEXTERN void spMultTransposed( MatrixPtr, spREAL[], spREAL[], - spREAL[], spREAL[] ); -spcEXTERN void spSolve( MatrixPtr, spREAL[], spREAL[], spREAL[], - spREAL[] ); -spcEXTERN void spSolveTransposed( MatrixPtr, spREAL[], spREAL[], - spREAL[], spREAL[] ); -#else /* NOT (spCOMPLEX && spSEPARATED_COMPLEX_VECTORS) */ -spcEXTERN int spFileVector( MatrixPtr, char* , spREAL[] ); -spcEXTERN void spMultiply( MatrixPtr, spREAL[], spREAL[] ); -spcEXTERN void spMultTransposed( MatrixPtr, - spREAL[], spREAL[] ); -spcEXTERN void spSolve( MatrixPtr, spREAL[], spREAL[] ); -spcEXTERN void spSolveTransposed( MatrixPtr, - spREAL[], spREAL[] ); +spcEXTERN int spFileVector(MatrixPtr, char *, spREAL[], spREAL[]); +spcEXTERN void spMultiply(MatrixPtr, spREAL[], spREAL[], spREAL[], spREAL[]); +spcEXTERN void spMultTransposed(MatrixPtr, spREAL[], spREAL[], spREAL[], + spREAL[]); +spcEXTERN void spSolve(MatrixPtr, spREAL[], spREAL[], spREAL[], spREAL[]); +spcEXTERN void spSolveTransposed(MatrixPtr, spREAL[], spREAL[], spREAL[], + spREAL[]); +#else /* NOT (spCOMPLEX && spSEPARATED_COMPLEX_VECTORS) */ +spcEXTERN int spFileVector(MatrixPtr, char *, spREAL[]); +spcEXTERN void spMultiply(MatrixPtr, spREAL[], spREAL[]); +spcEXTERN void spMultTransposed(MatrixPtr, spREAL[], spREAL[]); +spcEXTERN void spSolve(MatrixPtr, spREAL[], spREAL[]); +spcEXTERN void spSolveTransposed(MatrixPtr, spREAL[], spREAL[]); #endif /* NOT (spCOMPLEX && spSEPARATED_COMPLEX_VECTORS) */ -#endif /* spOKAY */ +#endif /* spOKAY */ diff --git a/src/maths/sparse/spAllocate.c b/src/maths/sparse/spAllocate.c index f20357f85..a027794b3 100644 --- a/src/maths/sparse/spAllocate.c +++ b/src/maths/sparse/spAllocate.c @@ -6,8 +6,9 @@ * UC Berkeley */ /*!\file - * This file contains functions for allocating and freeing matrices, configuring them, and for - * accessing global information about the matrix (size, error status, etc.). + * This file contains functions for allocating and freeing matrices, + * configuring them, and for accessing global information about the matrix + * (size, error status, etc.). * * Objects that begin with the \a spc prefix are considered private * and should not be used. @@ -36,7 +37,6 @@ * ExpandTranslationArrays */ - /* * Revision and copyright information. * @@ -46,12 +46,10 @@ #ifdef notdef static char copyright[] = "Sparse1.4: Copyright (c) 1985-2003 by Kenneth S. Kundert"; -static char RCSid[] = - "@(#)$Header: /cvsroot/sparse/src/spAllocate.c,v 1.3 2003/06/29 04:19:52 kundert Exp $"; +static char RCSid[] = "@(#)$Header: /cvsroot/sparse/src/spAllocate.c,v 1.3 " + "2003/06/29 04:19:52 kundert Exp $"; #endif - - /* * IMPORTS * @@ -65,14 +63,10 @@ static char RCSid[] = */ #define spINSIDE_SPARSE -#include -#include "spConfig.h" #include "ngspice/spmatrix.h" +#include "spConfig.h" #include "spDefs.h" - - - - +#include /* * Global strings @@ -83,20 +77,15 @@ char spcErrorsMustBeCleared[] = "Error not cleared"; char spcMatrixMustBeFactored[] = "Matrix must be factored"; char spcMatrixMustNotBeFactored[] = "Matrix must not be factored"; - - - /* * Function declarations */ -//static spError ReserveElements( MatrixPtr, int ); -static void InitializeElementBlocks( MatrixPtr, int, int ); -static void RecordAllocation( MatrixPtr, void * ); -static void AllocateBlockOfAllocationList( MatrixPtr ); +// static spError ReserveElements( MatrixPtr, int ); +static void InitializeElementBlocks(MatrixPtr, int, int); +static void RecordAllocation(MatrixPtr, void *); +static void AllocateBlockOfAllocationList(MatrixPtr); - - /*! * Allocates and initializes the data structures associated with a matrix. * @@ -111,8 +100,8 @@ static void AllocateBlockOfAllocationList( MatrixPtr ); * \param Complex * Type of matrix. If \a Complex is 0 then the matrix is real, otherwise * the matrix will be complex. Note that if the routines are not set up - * to handle the type of matrix requested, then an \a spPANIC error will occur. - * Further note that if a matrix will be both real and complex, it must + * to handle the type of matrix requested, then an \a spPANIC error will + * occur. Further note that if a matrix will be both real and complex, it must * be specified here as being complex. * \param pError * Returns error flag, needed because function \a spError() will @@ -126,43 +115,37 @@ static void AllocateBlockOfAllocationList( MatrixPtr ); * A pointer to the matrix frame being created. */ -MatrixPtr -spCreate( - int Size, - int Complex, - int *pError -) -{ -unsigned SizePlusOne; -MatrixPtr Matrix; -int I; -int AllocatedSize; +MatrixPtr spCreate(int Size, int Complex, int *pError) { + unsigned SizePlusOne; + MatrixPtr Matrix; + int I; + int AllocatedSize; -/* Begin `spCreate'. */ -/* Clear error flag. */ + /* Begin `spCreate'. */ + /* Clear error flag. */ *pError = spOKAY; -/* Test for valid size. */ - vASSERT( (Size >= 0) AND (Size != 0 OR EXPANDABLE), "Invalid size" ); + /* Test for valid size. */ + vASSERT((Size >= 0) AND(Size != 0 OR EXPANDABLE), "Invalid size"); /* Test for valid type. */ #if NOT spCOMPLEX - ASSERT( NOT Complex ); + ASSERT(NOT Complex); #endif #if NOT REAL - ASSERT( Complex ); + ASSERT(Complex); #endif -/* Create Matrix. */ - AllocatedSize = MAX( Size, MINIMUM_ALLOCATED_SIZE ); + /* Create Matrix. */ + AllocatedSize = MAX(Size, MINIMUM_ALLOCATED_SIZE); SizePlusOne = (unsigned)(AllocatedSize + 1); - if ((Matrix = SP_MALLOC(struct MatrixFrame, 1)) == NULL) - { *pError = spNO_MEMORY; + if ((Matrix = SP_MALLOC(struct MatrixFrame, 1)) == NULL) { + *pError = spNO_MEMORY; return NULL; } -/* Initialize matrix */ + /* Initialize matrix */ Matrix->ID = SPARSE_ID; Matrix->Complex = Complex; Matrix->PreviousMatrixWasComplex = Complex; @@ -201,10 +184,11 @@ int AllocatedSize; Matrix->ElementsRemaining = 0; Matrix->FillinsRemaining = 0; - RecordAllocation( Matrix, Matrix ); - if (Matrix->Error == spNO_MEMORY) goto MemoryError; /* FIXME: Use of memory after free */ + RecordAllocation(Matrix, Matrix); + if (Matrix->Error == spNO_MEMORY) + goto MemoryError; /* FIXME: Use of memory after free */ -/* Take out the trash. */ + /* Take out the trash. */ Matrix->TrashCan.Real = 0.0; #if spCOMPLEX Matrix->TrashCan.Imag = 0.0; @@ -217,56 +201,56 @@ int AllocatedSize; Matrix->TrashCan.pInitInfo = NULL; #endif -/* Allocate space in memory for Diag pointer vector. */ - SP_CALLOC( Matrix->Diag, ElementPtr, SizePlusOne); + /* Allocate space in memory for Diag pointer vector. */ + SP_CALLOC(Matrix->Diag, ElementPtr, SizePlusOne); if (Matrix->Diag == NULL) goto MemoryError; -/* Allocate space in memory for FirstInCol pointer vector. */ - SP_CALLOC( Matrix->FirstInCol, ElementPtr, SizePlusOne); + /* Allocate space in memory for FirstInCol pointer vector. */ + SP_CALLOC(Matrix->FirstInCol, ElementPtr, SizePlusOne); if (Matrix->FirstInCol == NULL) goto MemoryError; -/* Allocate space in memory for FirstInRow pointer vector. */ - SP_CALLOC( Matrix->FirstInRow, ElementPtr, SizePlusOne); + /* Allocate space in memory for FirstInRow pointer vector. */ + SP_CALLOC(Matrix->FirstInRow, ElementPtr, SizePlusOne); if (Matrix->FirstInRow == NULL) goto MemoryError; -/* Allocate space in memory for IntToExtColMap vector. */ - if (( Matrix->IntToExtColMap = SP_MALLOC(int, SizePlusOne)) == NULL) + /* Allocate space in memory for IntToExtColMap vector. */ + if ((Matrix->IntToExtColMap = SP_MALLOC(int, SizePlusOne)) == NULL) goto MemoryError; -/* Allocate space in memory for IntToExtRowMap vector. */ - if (( Matrix->IntToExtRowMap = SP_MALLOC(int, SizePlusOne)) == NULL) + /* Allocate space in memory for IntToExtRowMap vector. */ + if ((Matrix->IntToExtRowMap = SP_MALLOC(int, SizePlusOne)) == NULL) goto MemoryError; -/* Initialize MapIntToExt vectors. */ - for (I = 1; I <= AllocatedSize; I++) - { Matrix->IntToExtRowMap[I] = I; + /* Initialize MapIntToExt vectors. */ + for (I = 1; I <= AllocatedSize; I++) { + Matrix->IntToExtRowMap[I] = I; Matrix->IntToExtColMap[I] = I; } #if TRANSLATE -/* Allocate space in memory for ExtToIntColMap vector. */ - if (( Matrix->ExtToIntColMap = SP_MALLOC(int, SizePlusOne)) == NULL) + /* Allocate space in memory for ExtToIntColMap vector. */ + if ((Matrix->ExtToIntColMap = SP_MALLOC(int, SizePlusOne)) == NULL) goto MemoryError; -/* Allocate space in memory for ExtToIntRowMap vector. */ - if (( Matrix->ExtToIntRowMap = SP_MALLOC(int, SizePlusOne)) == NULL) + /* Allocate space in memory for ExtToIntRowMap vector. */ + if ((Matrix->ExtToIntRowMap = SP_MALLOC(int, SizePlusOne)) == NULL) goto MemoryError; -/* Initialize MapExtToInt vectors. */ - for (I = 1; I <= AllocatedSize; I++) - { Matrix->ExtToIntColMap[I] = -1; + /* Initialize MapExtToInt vectors. */ + for (I = 1; I <= AllocatedSize; I++) { + Matrix->ExtToIntColMap[I] = -1; Matrix->ExtToIntRowMap[I] = -1; } Matrix->ExtToIntColMap[0] = 0; Matrix->ExtToIntRowMap[0] = 0; #endif -/* Allocate space for fill-ins and initial set of elements. */ - InitializeElementBlocks( Matrix, SPACE_FOR_ELEMENTS*AllocatedSize, - SPACE_FOR_FILL_INS*AllocatedSize ); + /* Allocate space for fill-ins and initial set of elements. */ + InitializeElementBlocks(Matrix, SPACE_FOR_ELEMENTS * AllocatedSize, + SPACE_FOR_FILL_INS * AllocatedSize); if (Matrix->Error == spNO_MEMORY) goto MemoryError; @@ -274,21 +258,13 @@ int AllocatedSize; MemoryError: -/* Deallocate matrix and return no pointer to matrix if there is not enough - memory. */ + /* Deallocate matrix and return no pointer to matrix if there is not enough + memory. */ *pError = spNO_MEMORY; - spDestroy( Matrix ); + spDestroy(Matrix); return NULL; } - - - - - - - - /* * ELEMENT ALLOCATION * @@ -312,34 +288,26 @@ MemoryError: * spNO_MEMORY */ -ElementPtr -spcGetElement( MatrixPtr Matrix ) -{ -ElementPtr pElement; +ElementPtr spcGetElement(MatrixPtr Matrix) { + ElementPtr pElement; -/* Begin `spcGetElement'. */ + /* Begin `spcGetElement'. */ -/* Allocate block of MatrixElements if necessary. */ - if (Matrix->ElementsRemaining == 0) - { pElement = SP_MALLOC(struct MatrixElement, ELEMENTS_PER_ALLOCATION); - RecordAllocation( Matrix, pElement ); - if (Matrix->Error == spNO_MEMORY) return NULL; + /* Allocate block of MatrixElements if necessary. */ + if (Matrix->ElementsRemaining == 0) { + pElement = SP_MALLOC(struct MatrixElement, ELEMENTS_PER_ALLOCATION); + RecordAllocation(Matrix, pElement); + if (Matrix->Error == spNO_MEMORY) + return NULL; Matrix->ElementsRemaining = ELEMENTS_PER_ALLOCATION; Matrix->NextAvailElement = pElement; } -/* Update Element counter and return pointer to Element. */ + /* Update Element counter and return pointer to Element. */ Matrix->ElementsRemaining--; return Matrix->NextAvailElement++; } - - - - - - - /* * ELEMENT ALLOCATION INITIALIZATION * @@ -370,53 +338,44 @@ ElementPtr pElement; * spNO_MEMORY */ -static void -InitializeElementBlocks( - MatrixPtr Matrix, - int InitialNumberOfElements, - int NumberOfFillinsExpected -) -{ -ElementPtr pElement; +static void InitializeElementBlocks(MatrixPtr Matrix, + int InitialNumberOfElements, + int NumberOfFillinsExpected) { + ElementPtr pElement; -/* Begin `InitializeElementBlocks'. */ + /* Begin `InitializeElementBlocks'. */ -/* Allocate block of MatrixElements for elements. */ + /* Allocate block of MatrixElements for elements. */ pElement = SP_MALLOC(struct MatrixElement, InitialNumberOfElements); - RecordAllocation( Matrix, pElement ); - if (Matrix->Error == spNO_MEMORY) return; + RecordAllocation(Matrix, pElement); + if (Matrix->Error == spNO_MEMORY) + return; Matrix->ElementsRemaining = InitialNumberOfElements; Matrix->NextAvailElement = pElement; -/* Allocate block of MatrixElements for fill-ins. */ + /* Allocate block of MatrixElements for fill-ins. */ pElement = SP_MALLOC(struct MatrixElement, NumberOfFillinsExpected); - RecordAllocation( Matrix, pElement ); - if (Matrix->Error == spNO_MEMORY) return; + RecordAllocation(Matrix, pElement); + if (Matrix->Error == spNO_MEMORY) + return; Matrix->FillinsRemaining = NumberOfFillinsExpected; Matrix->NextAvailFillin = pElement; -/* Allocate a fill-in list structure. */ - Matrix->FirstFillinListNode = SP_MALLOC(struct FillinListNodeStruct,1); - RecordAllocation( Matrix, Matrix->FirstFillinListNode ); - if (Matrix->Error == spNO_MEMORY) return; + /* Allocate a fill-in list structure. */ + Matrix->FirstFillinListNode = SP_MALLOC(struct FillinListNodeStruct, 1); + RecordAllocation(Matrix, Matrix->FirstFillinListNode); + if (Matrix->Error == spNO_MEMORY) + return; Matrix->LastFillinListNode = Matrix->FirstFillinListNode; Matrix->FirstFillinListNode->pFillinList = pElement; - Matrix->FirstFillinListNode->NumberOfFillinsInList =NumberOfFillinsExpected; + Matrix->FirstFillinListNode->NumberOfFillinsInList = + NumberOfFillinsExpected; Matrix->FirstFillinListNode->Next = NULL; return; } - - - - - - - - - /* * FILL-IN ALLOCATION * @@ -436,44 +395,42 @@ ElementPtr pElement; * spNO_MEMORY */ -ElementPtr -spcGetFillin( MatrixPtr Matrix ) -{ +ElementPtr spcGetFillin(MatrixPtr Matrix) { #if STRIP OR LINT -struct FillinListNodeStruct *pListNode; -ElementPtr pFillins; + struct FillinListNodeStruct *pListNode; + ElementPtr pFillins; #endif -/* Begin `spcGetFillin'. */ + /* Begin `spcGetFillin'. */ #if NOT STRIP OR LINT if (Matrix->FillinsRemaining == 0) - return spcGetElement( Matrix ); + return spcGetElement(Matrix); #endif #if STRIP OR LINT - if (Matrix->FillinsRemaining == 0) - { pListNode = Matrix->LastFillinListNode; + if (Matrix->FillinsRemaining == 0) { + pListNode = Matrix->LastFillinListNode; -/* First see if there are any stripped fill-ins left. */ - if (pListNode->Next != NULL) - { Matrix->LastFillinListNode = pListNode = pListNode->Next; + /* First see if there are any stripped fill-ins left. */ + if (pListNode->Next != NULL) { + Matrix->LastFillinListNode = pListNode = pListNode->Next; Matrix->FillinsRemaining = pListNode->NumberOfFillinsInList; Matrix->NextAvailFillin = pListNode->pFillinList; - } - else - { -/* Allocate block of fill-ins. */ + } else { + /* Allocate block of fill-ins. */ pFillins = SP_MALLOC(struct MatrixElement, ELEMENTS_PER_ALLOCATION); - RecordAllocation( Matrix, pFillins ); - if (Matrix->Error == spNO_MEMORY) return NULL; + RecordAllocation(Matrix, pFillins); + if (Matrix->Error == spNO_MEMORY) + return NULL; Matrix->FillinsRemaining = ELEMENTS_PER_ALLOCATION; Matrix->NextAvailFillin = pFillins; -/* Allocate a fill-in list structure. */ - pListNode->Next = SP_MALLOC(struct FillinListNodeStruct,1); - RecordAllocation( Matrix, pListNode->Next ); - if (Matrix->Error == spNO_MEMORY) return NULL; + /* Allocate a fill-in list structure. */ + pListNode->Next = SP_MALLOC(struct FillinListNodeStruct, 1); + RecordAllocation(Matrix, pListNode->Next); + if (Matrix->Error == spNO_MEMORY) + return NULL; Matrix->LastFillinListNode = pListNode = pListNode->Next; pListNode->pFillinList = pFillins; @@ -483,19 +440,11 @@ ElementPtr pFillins; } #endif -/* Update Fill-in counter and return pointer to Fill-in. */ + /* Update Fill-in counter and return pointer to Fill-in. */ Matrix->FillinsRemaining--; return Matrix->NextAvailFillin++; } - - - - - - - - /* * RECORD A MEMORY ALLOCATION * @@ -505,7 +454,7 @@ ElementPtr pFillins; * >>> Arguments: * Matrix (MatrixPtr) * Pointer to the matrix. - * AllocatedPtr + * AllocatedPtr * The pointer returned by malloc or calloc. These pointers are saved in * a list so that they can be easily freed. * @@ -513,45 +462,32 @@ ElementPtr pFillins; * spNO_MEMORY */ -static void -RecordAllocation( - MatrixPtr Matrix, - void *AllocatedPtr -) -{ -/* Begin `RecordAllocation'. */ -/* - * If Allocated pointer is NULL, assume that malloc returned a NULL pointer, - * which indicates a spNO_MEMORY error. - */ - if (AllocatedPtr == NULL) - { Matrix->Error = spNO_MEMORY; +static void RecordAllocation(MatrixPtr Matrix, void *AllocatedPtr) { + /* Begin `RecordAllocation'. */ + /* + * If Allocated pointer is NULL, assume that malloc returned a NULL pointer, + * which indicates a spNO_MEMORY error. + */ + if (AllocatedPtr == NULL) { + Matrix->Error = spNO_MEMORY; return; } -/* Allocate block of MatrixElements if necessary. */ - if (Matrix->RecordsRemaining == 0) - { AllocateBlockOfAllocationList( Matrix ); - if (Matrix->Error == spNO_MEMORY) - { SP_FREE(AllocatedPtr); + /* Allocate block of MatrixElements if necessary. */ + if (Matrix->RecordsRemaining == 0) { + AllocateBlockOfAllocationList(Matrix); + if (Matrix->Error == spNO_MEMORY) { + SP_FREE(AllocatedPtr); return; } } -/* Add Allocated pointer to Allocation List. */ + /* Add Allocated pointer to Allocation List. */ (++Matrix->TopOfAllocationList)->AllocatedPtr = AllocatedPtr; Matrix->RecordsRemaining--; return; - } - - - - - - - /* * ADD A BLOCK OF SLOTS TO ALLOCATION LIST * @@ -570,45 +506,36 @@ RecordAllocation( * spNO_MEMORY */ -static void -AllocateBlockOfAllocationList( MatrixPtr Matrix ) -{ -int I; -AllocationListPtr ListPtr; +static void AllocateBlockOfAllocationList(MatrixPtr Matrix) { + int I; + AllocationListPtr ListPtr; -/* Begin `AllocateBlockOfAllocationList'. */ -/* Allocate block of records for allocation list. */ - ListPtr = SP_MALLOC(struct AllocationRecord, (ELEMENTS_PER_ALLOCATION+1)); - if (ListPtr == NULL) - { Matrix->Error = spNO_MEMORY; + /* Begin `AllocateBlockOfAllocationList'. */ + /* Allocate block of records for allocation list. */ + ListPtr = SP_MALLOC(struct AllocationRecord, (ELEMENTS_PER_ALLOCATION + 1)); + if (ListPtr == NULL) { + Matrix->Error = spNO_MEMORY; return; } -/* String entries of allocation list into singly linked list. List is linked - such that any record points to the one before it. */ + /* String entries of allocation list into singly linked list. List is + linked such that any record points to the one before it. */ ListPtr->NextRecord = Matrix->TopOfAllocationList; Matrix->TopOfAllocationList = ListPtr; ListPtr += ELEMENTS_PER_ALLOCATION; - for (I = ELEMENTS_PER_ALLOCATION; I > 0; I--) - { ListPtr->NextRecord = ListPtr - 1; - ListPtr--; + for (I = ELEMENTS_PER_ALLOCATION; I > 0; I--) { + ListPtr->NextRecord = ListPtr - 1; + ListPtr--; } -/* Record allocation of space for allocation list on allocation list. */ + /* Record allocation of space for allocation list on allocation list. */ Matrix->TopOfAllocationList->AllocatedPtr = (void *)ListPtr; Matrix->RecordsRemaining = ELEMENTS_PER_ALLOCATION; return; } - - - - - - - /*! * Destroys a matrix and frees all memory associated with it. * @@ -626,50 +553,42 @@ AllocationListPtr ListPtr; * in the allocation list. */ -void -spDestroy( MatrixPtr Matrix ) -{ -AllocationListPtr ListPtr, NextListPtr; +void spDestroy(MatrixPtr Matrix) { + AllocationListPtr ListPtr, NextListPtr; -/* Begin `spDestroy'. */ - ASSERT_IS_SPARSE( Matrix ); + /* Begin `spDestroy'. */ + ASSERT_IS_SPARSE(Matrix); -/* Deallocate the vectors that are located in the matrix frame. */ - SP_FREE( Matrix->IntToExtColMap ); - SP_FREE( Matrix->IntToExtRowMap ); - SP_FREE( Matrix->ExtToIntColMap ); - SP_FREE( Matrix->ExtToIntRowMap ); - SP_FREE( Matrix->Diag ); - SP_FREE( Matrix->FirstInRow ); - SP_FREE( Matrix->FirstInCol ); - SP_FREE( Matrix->MarkowitzRow ); - SP_FREE( Matrix->MarkowitzCol ); - SP_FREE( Matrix->MarkowitzProd ); - SP_FREE( Matrix->DoCmplxDirect ); - SP_FREE( Matrix->DoRealDirect ); - SP_FREE( Matrix->Intermediate ); + /* Deallocate the vectors that are located in the matrix frame. */ + SP_FREE(Matrix->IntToExtColMap); + SP_FREE(Matrix->IntToExtRowMap); + SP_FREE(Matrix->ExtToIntColMap); + SP_FREE(Matrix->ExtToIntRowMap); + SP_FREE(Matrix->Diag); + SP_FREE(Matrix->FirstInRow); + SP_FREE(Matrix->FirstInCol); + SP_FREE(Matrix->MarkowitzRow); + SP_FREE(Matrix->MarkowitzCol); + SP_FREE(Matrix->MarkowitzProd); + SP_FREE(Matrix->DoCmplxDirect); + SP_FREE(Matrix->DoRealDirect); + SP_FREE(Matrix->Intermediate); -/* Sequentially step through the list of allocated pointers freeing pointers - * along the way. */ + /* Sequentially step through the list of allocated pointers freeing pointers + * along the way. */ ListPtr = Matrix->TopOfAllocationList; - while (ListPtr != NULL) - { NextListPtr = ListPtr->NextRecord; - if ((void *) ListPtr == ListPtr->AllocatedPtr) { - SP_FREE( ListPtr ); + while (ListPtr != NULL) { + NextListPtr = ListPtr->NextRecord; + if ((void *)ListPtr == ListPtr->AllocatedPtr) { + SP_FREE(ListPtr); } else { - SP_FREE( ListPtr->AllocatedPtr ); + SP_FREE(ListPtr->AllocatedPtr); } ListPtr = NextListPtr; } return; } - - - - - - /*! * This function returns the error status of the given matrix. * @@ -680,27 +599,17 @@ AllocationListPtr ListPtr, NextListPtr; * The pointer to the matrix for which the error status is desired. */ -int -spError( MatrixPtr Matrix ) -{ -/* Begin `spError'. */ +int spError(MatrixPtr Matrix) { + /* Begin `spError'. */ - if (Matrix != NULL) - { ASSERT_IS_SPARSE( Matrix ); + if (Matrix != NULL) { + ASSERT_IS_SPARSE(Matrix); return Matrix->Error; - } - else return spNO_MEMORY; /* This error may actually be spPANIC, - * no way to tell. */ + } else + return spNO_MEMORY; /* This error may actually be spPANIC, + * no way to tell. */ } - - - - - - - - /*! * This function returns the row and column number where the matrix was * detected as singular (if pivoting was allowed on the last factorization) @@ -716,29 +625,18 @@ spError( MatrixPtr Matrix ) * The column number. */ -void -spWhereSingular( - MatrixPtr Matrix, - int *pRow, - int *pCol -) -{ -/* Begin `spWhereSingular'. */ - ASSERT_IS_SPARSE( Matrix ); +void spWhereSingular(MatrixPtr Matrix, int *pRow, int *pCol) { + /* Begin `spWhereSingular'. */ + ASSERT_IS_SPARSE(Matrix); - if (Matrix->Error == spSINGULAR OR Matrix->Error == spZERO_DIAG) - { *pRow = Matrix->SingularRow; + if (Matrix->Error == spSINGULAR OR Matrix->Error == spZERO_DIAG) { + *pRow = Matrix->SingularRow; *pCol = Matrix->SingularCol; - } - else *pRow = *pCol = 0; + } else + *pRow = *pCol = 0; return; } - - - - - /*! * Returns the size of the matrix. Either the internal or external size of * the matrix is returned. @@ -752,14 +650,9 @@ spWhereSingular( * may differ if the \a TRANSLATE option is set true. */ -int -spGetSize( - MatrixPtr Matrix, - int External -) -{ -/* Begin `spGetSize'. */ - ASSERT_IS_SPARSE( Matrix ); +int spGetSize(MatrixPtr Matrix, int External) { + /* Begin `spGetSize'. */ + ASSERT_IS_SPARSE(Matrix); #if TRANSLATE if (External) @@ -771,13 +664,6 @@ spGetSize( #endif } - - - - - - - /*! * Forces matrix to be real. * @@ -785,18 +671,15 @@ spGetSize( * Pointer to matrix. */ -void -spSetReal( MatrixPtr Matrix ) -{ -/* Begin `spSetReal'. */ +void spSetReal(MatrixPtr Matrix) { + /* Begin `spSetReal'. */ - ASSERT_IS_SPARSE( Matrix ); - vASSERT( REAL, "Sparse not compiled to handle real matrices" ); + ASSERT_IS_SPARSE(Matrix); + vASSERT(REAL, "Sparse not compiled to handle real matrices"); Matrix->Complex = NO; return; } - /*! * Forces matrix to be complex. * @@ -804,44 +687,33 @@ spSetReal( MatrixPtr Matrix ) * Pointer to matrix. */ -void -spSetComplex( MatrixPtr Matrix ) -{ -/* Begin `spSetComplex'. */ +void spSetComplex(MatrixPtr Matrix) { + /* Begin `spSetComplex'. */ - ASSERT_IS_SPARSE( Matrix ); - vASSERT( spCOMPLEX, "Sparse not compiled to handle complex matrices" ); + ASSERT_IS_SPARSE(Matrix); + vASSERT(spCOMPLEX, "Sparse not compiled to handle complex matrices"); Matrix->Complex = YES; return; } - - - - - - - - /*! - * This function returns the number of fill-ins that currently exists in a matrix. + * This function returns the number of fill-ins that currently exists in a + * matrix. * * \param Matrix * Pointer to matrix. */ -int -spFillinCount( MatrixPtr Matrix ) -{ -/* Begin `spFillinCount'. */ +int spFillinCount(MatrixPtr Matrix) { + /* Begin `spFillinCount'. */ - ASSERT_IS_SPARSE( Matrix ); + ASSERT_IS_SPARSE(Matrix); return Matrix->Fillins; } - /*! - * This function returns the total number of elements (including fill-ins) that currently exists in a matrix. + * This function returns the total number of elements (including fill-ins) that + * currently exists in a matrix. * * \param Matrix * Pointer to matrix. @@ -849,21 +721,16 @@ spFillinCount( MatrixPtr Matrix ) /* FIXME: Seems no different size entries available anymore */ -int -spElementCount( MatrixPtr Matrix ) -{ -/* Begin `spElementCount'. */ +int spElementCount(MatrixPtr Matrix) { + /* Begin `spElementCount'. */ - ASSERT_IS_SPARSE( Matrix ); + ASSERT_IS_SPARSE(Matrix); return Matrix->Elements; } -int -spOriginalCount( MatrixPtr Matrix ) -{ +int spOriginalCount(MatrixPtr Matrix) { /* Begin `spOriginalCount'. */ - ASSERT_IS_SPARSE( Matrix ); + ASSERT_IS_SPARSE(Matrix); return Matrix->Elements; } - diff --git a/src/maths/sparse/spBuild.c b/src/maths/sparse/spBuild.c index c0baca585..82b1f7f7f 100644 --- a/src/maths/sparse/spBuild.c +++ b/src/maths/sparse/spBuild.c @@ -35,7 +35,6 @@ * ExpandTranslationArrays */ - /* * Revision and copyright information. * @@ -45,13 +44,10 @@ #ifdef notdef static char copyright[] = "Sparse1.4: Copyright (c) 1985-2003 by Kenneth S. Kundert"; -static char RCSid[] = - "@(#)$Header: /cvsroot/sparse/src/spBuild.c,v 1.3 2003/06/29 04:19:52 kundert Exp $"; +static char RCSid[] = "@(#)$Header: /cvsroot/sparse/src/spBuild.c,v 1.3 " + "2003/06/29 04:19:52 kundert Exp $"; #endif - - - /* * IMPORTS * @@ -65,31 +61,23 @@ static char RCSid[] = */ #define spINSIDE_SPARSE -#include -#include "spConfig.h" #include "ngspice/spmatrix.h" +#include "spConfig.h" #include "spDefs.h" - - - - +#include /* * Function declarations */ #if TRANSLATE -static void Translate( MatrixPtr, int*, int* ); -static void ExpandTranslationArrays( MatrixPtr, int ); +static void Translate(MatrixPtr, int *, int *); +static void ExpandTranslationArrays(MatrixPtr, int); #endif #if EXPANDABLE -static void EnlargeMatrix( MatrixPtr, int ); +static void EnlargeMatrix(MatrixPtr, int); #endif - - - - /*! * Sets every element of the matrix to zero and clears the error flag. * @@ -101,39 +89,37 @@ static void EnlargeMatrix( MatrixPtr, int ); * A pointer to the element being cleared. */ -void -spClear( MatrixPtr Matrix ) -{ -ElementPtr pElement; -int I; +void spClear(MatrixPtr Matrix) { + ElementPtr pElement; + int I; -/* Begin `spClear'. */ - ASSERT_IS_SPARSE( Matrix ); + /* Begin `spClear'. */ + ASSERT_IS_SPARSE(Matrix); /* Clear matrix. */ #if spCOMPLEX - if (Matrix->PreviousMatrixWasComplex OR Matrix->Complex) - { for (I = Matrix->Size; I > 0; I--) - { pElement = Matrix->FirstInCol[I]; - while (pElement != NULL) - { pElement->Real = 0.0; + if (Matrix->PreviousMatrixWasComplex OR Matrix->Complex) { + for (I = Matrix->Size; I > 0; I--) { + pElement = Matrix->FirstInCol[I]; + while (pElement != NULL) { + pElement->Real = 0.0; pElement->Imag = 0.0; pElement = pElement->NextInCol; } } - } - else + } else #endif - { for (I = Matrix->Size; I > 0; I--) - { pElement = Matrix->FirstInCol[I]; - while (pElement != NULL) - { pElement->Real = 0.0; + { + for (I = Matrix->Size; I > 0; I--) { + pElement = Matrix->FirstInCol[I]; + while (pElement != NULL) { + pElement->Real = 0.0; pElement = pElement->NextInCol; } } } -/* Empty the trash. */ + /* Empty the trash. */ Matrix->TrashCan.Real = 0.0; #if spCOMPLEX Matrix->TrashCan.Imag = 0.0; @@ -147,15 +133,6 @@ int I; return; } - - - - - - - - - /*! * This routine is used to find an element given its indices. It will not * create it if it does not exist. @@ -177,79 +154,64 @@ int I; * Pointer to an element in the matrix. */ -spElement * -spFindElement( - MatrixPtr Matrix, - int Row, - int Col -) -{ -ElementPtr pElement; -int StartAt = 0; -long int Min = LARGEST_LONG_INTEGER; -#define BorderRight 0 /* Start at left border, move right. */ -#define BorderDown 1 /* Start at top border, move down. */ -#define DiagRight 2 /* Start at diagonal, move right. */ -#define DiagDown 3 /* Start at diagonal, move down. */ +spElement *spFindElement(MatrixPtr Matrix, int Row, int Col) { + ElementPtr pElement; + int StartAt = 0; + long int Min = LARGEST_LONG_INTEGER; +#define BorderRight 0 /* Start at left border, move right. */ +#define BorderDown 1 /* Start at top border, move down. */ +#define DiagRight 2 /* Start at diagonal, move right. */ +#define DiagDown 3 /* Start at diagonal, move down. */ -/* Begin `spFindElement'. */ - if (Row == Col) return &Matrix->Diag[Row]->Real; + /* Begin `spFindElement'. */ + if (Row == Col) + return &Matrix->Diag[Row]->Real; -/* Determine where to start the search. */ - if (Matrix->RowsLinked) - { if ((Col >= Row) AND Matrix->Diag[Row]) - { Min = Col - Row; - StartAt = DiagRight; + /* Determine where to start the search. */ + if (Matrix->RowsLinked) { + if ((Col >= Row) AND Matrix->Diag[Row]) { + Min = Col - Row; + StartAt = DiagRight; + } else { + Min = Col; + StartAt = BorderRight; + } } - else - { Min = Col; - StartAt = BorderRight; - } - } - if ((Row >= Col) AND Matrix->Diag[Col]) - { if (Row - Col < Min) - StartAt = DiagDown; - } - else if (Row < Min) - StartAt = BorderDown; + if ((Row >= Col) AND Matrix->Diag[Col]) { + if (Row - Col < Min) + StartAt = DiagDown; + } else if (Row < Min) + StartAt = BorderDown; -/* Search column for element. */ - if ((StartAt == BorderDown) OR (StartAt == DiagDown)) - { if (StartAt == BorderDown) - pElement = Matrix->FirstInCol[Col]; - else - pElement = Matrix->Diag[Col]; + /* Search column for element. */ + if ((StartAt == BorderDown) OR(StartAt == DiagDown)) { + if (StartAt == BorderDown) + pElement = Matrix->FirstInCol[Col]; + else + pElement = Matrix->Diag[Col]; - while ((pElement != NULL) AND (pElement->Row < Row)) + while ((pElement != NULL) AND(pElement->Row < Row)) pElement = pElement->NextInCol; - if (pElement AND (pElement->Row == Row)) + if (pElement AND(pElement->Row == Row)) + return &pElement->Real; + else + return NULL; + } + + /* Search row for element. */ + if (StartAt == BorderRight) + pElement = Matrix->FirstInRow[Row]; + else + pElement = Matrix->Diag[Row]; + + while ((pElement != NULL) AND(pElement->Col < Col)) + pElement = pElement->NextInRow; + if (pElement AND(pElement->Col == Col)) return &pElement->Real; else return NULL; - } - -/* Search row for element. */ - if (StartAt == BorderRight) - pElement = Matrix->FirstInRow[Row]; - else - pElement = Matrix->Diag[Row]; - - while ((pElement != NULL) AND (pElement->Col < Col)) - pElement = pElement->NextInRow; - if (pElement AND (pElement->Col == Col)) - return &pElement->Real; - else - return NULL; } - - - - - - - - /*! * Finds element [Row,Col] and returns a pointer to it. If element is * not found then it is created and spliced into matrix. This routine @@ -284,64 +246,59 @@ long int Min = LARGEST_LONG_INTEGER; * Error is not cleared in this routine. */ -spElement * -spGetElement( - MatrixPtr Matrix, - int Row, - int Col -) -{ -ElementPtr pElement; +spElement *spGetElement(MatrixPtr Matrix, int Row, int Col) { + ElementPtr pElement; -/* Begin `spGetElement'. */ - ASSERT_IS_SPARSE( Matrix ); - vASSERT( Row >= 0 AND Col >= 0, "Negative row or column number" ); + /* Begin `spGetElement'. */ + ASSERT_IS_SPARSE(Matrix); + vASSERT(Row >= 0 AND Col >= 0, "Negative row or column number"); - if ((Row == 0) OR (Col == 0)) + if ((Row == 0) OR(Col == 0)) return &Matrix->TrashCan.Real; #if NOT TRANSLATE - vASSERT( NOT Matrix->Reordered, - "Set TRANSLATE to add elements to a reordered matrix" ); + vASSERT(NOT Matrix->Reordered, + "Set TRANSLATE to add elements to a reordered matrix"); #endif #if TRANSLATE - Translate( Matrix, &Row, &Col ); - if (Matrix->Error == spNO_MEMORY) return NULL; + Translate(Matrix, &Row, &Col); + if (Matrix->Error == spNO_MEMORY) + return NULL; #endif #if NOT TRANSLATE #if NOT EXPANDABLE - vASSERT( (Row <= Matrix->Size) AND (Col <= Matrix->Size), - "Row or column number too large" ); + vASSERT((Row <= Matrix->Size) AND(Col <= Matrix->Size), + "Row or column number too large"); #endif #if EXPANDABLE -/* Re-size Matrix if necessary. */ - if ((Row > Matrix->Size) OR (Col > Matrix->Size)) - EnlargeMatrix( Matrix, MAX(Row, Col) ); - if (Matrix->Error == spNO_MEMORY) return NULL; + /* Re-size Matrix if necessary. */ + if ((Row > Matrix->Size) OR(Col > Matrix->Size)) + EnlargeMatrix(Matrix, MAX(Row, Col)); + if (Matrix->Error == spNO_MEMORY) + return NULL; #endif #endif - if ((Row != Col) OR ((pElement = Matrix->Diag[Row]) == NULL)) - { /* - * Element does not exist or does not reside along diagonal. Search - * for element and if it does not exist, create it. - */ - pElement = spcCreateElement( Matrix, Row, Col, - &(Matrix->FirstInRow[Row]), - &(Matrix->FirstInCol[Col]), NO ); + if ((Row != Col) OR((pElement = Matrix->Diag[Row]) == + NULL)) { /* + * Element does not exist or does not reside + * along diagonal. Search for element and if + * it does not exist, create it. + */ + pElement = + spcCreateElement(Matrix, Row, Col, &(Matrix->FirstInRow[Row]), + &(Matrix->FirstInCol[Col]), NO); } -/* - * Cast pointer into a pointer to a RealNumber. This requires that Real - * be the first record in the MatrixElement structure. - */ + /* + * Cast pointer into a pointer to a RealNumber. This requires that Real + * be the first record in the MatrixElement structure. + */ return &pElement->Real; } - - /* * FIND ELEMENT BY SEARCHING COLUMN * @@ -374,42 +331,35 @@ ElementPtr pElement; * Pointer used to search through matrix. */ -ElementPtr -spcFindElementInCol(MatrixPtr Matrix, ElementPtr *LastAddr, - int Row, int Col, int CreateIfMissing) -{ - ElementPtr pElement; +ElementPtr spcFindElementInCol(MatrixPtr Matrix, ElementPtr *LastAddr, int Row, + int Col, int CreateIfMissing) { + ElementPtr pElement; /* Begin `spcFindElementInCol'. */ pElement = *LastAddr; /* Search for element. */ - while (pElement != NULL) - { - if (pElement->Row < Row) - { - /* Have not reached element yet. */ - LastAddr = &(pElement->NextInCol); - pElement = pElement->NextInCol; - } - else if (pElement->Row == Row) - { - /* Reached element. */ - return pElement; - } - else break; /* while loop */ + while (pElement != NULL) { + if (pElement->Row < Row) { + /* Have not reached element yet. */ + LastAddr = &(pElement->NextInCol); + pElement = pElement->NextInCol; + } else if (pElement->Row == Row) { + /* Reached element. */ + return pElement; + } else + break; /* while loop */ } /* Element does not exist and must be created. */ if (CreateIfMissing) - return spcCreateElementOld( Matrix, Row, Col, LastAddr, NO); + return spcCreateElementOld(Matrix, Row, Col, LastAddr, NO); else - return NULL; + return NULL; } - #if TRANSLATE - + /* * TRANSLATE EXTERNAL INDICES TO INTERNAL * @@ -442,67 +392,64 @@ spcFindElementInCol(MatrixPtr Matrix, ElementPtr *LastAddr, * the external to internal row number translation. */ -static void -Translate( - MatrixPtr Matrix, - int *Row, - int *Col -) -{ -int IntRow, IntCol, ExtRow, ExtCol; +static void Translate(MatrixPtr Matrix, int *Row, int *Col) { + int IntRow, IntCol, ExtRow, ExtCol; -/* Begin `Translate'. */ + /* Begin `Translate'. */ ExtRow = *Row; ExtCol = *Col; -/* Expand translation arrays if necessary. */ - if ((ExtRow > Matrix->AllocatedExtSize) OR - (ExtCol > Matrix->AllocatedExtSize)) - { - ExpandTranslationArrays( Matrix, MAX(ExtRow, ExtCol) ); - if (Matrix->Error == spNO_MEMORY) return; + /* Expand translation arrays if necessary. */ + if ((ExtRow > Matrix->AllocatedExtSize) + OR(ExtCol > Matrix->AllocatedExtSize)) { + ExpandTranslationArrays(Matrix, MAX(ExtRow, ExtCol)); + if (Matrix->Error == spNO_MEMORY) + return; } -/* Set ExtSize if necessary. */ - if ((ExtRow > Matrix->ExtSize) OR (ExtCol > Matrix->ExtSize)) + /* Set ExtSize if necessary. */ + if ((ExtRow > Matrix->ExtSize) OR(ExtCol > Matrix->ExtSize)) Matrix->ExtSize = MAX(ExtRow, ExtCol); -/* Translate external row or node number to internal row or node number. */ - if ((IntRow = Matrix->ExtToIntRowMap[ExtRow]) == -1) - { Matrix->ExtToIntRowMap[ExtRow] = ++Matrix->CurrentSize; + /* Translate external row or node number to internal row or node number. */ + if ((IntRow = Matrix->ExtToIntRowMap[ExtRow]) == -1) { + Matrix->ExtToIntRowMap[ExtRow] = ++Matrix->CurrentSize; Matrix->ExtToIntColMap[ExtRow] = Matrix->CurrentSize; IntRow = Matrix->CurrentSize; #if NOT EXPANDABLE - vASSERT( IntRow <= Matrix->Size, "Matrix size fixed" ); + vASSERT(IntRow <= Matrix->Size, "Matrix size fixed"); #endif #if EXPANDABLE -/* Re-size Matrix if necessary. */ + /* Re-size Matrix if necessary. */ if (IntRow > Matrix->Size) - EnlargeMatrix( Matrix, IntRow ); - if (Matrix->Error == spNO_MEMORY) return; + EnlargeMatrix(Matrix, IntRow); + if (Matrix->Error == spNO_MEMORY) + return; #endif Matrix->IntToExtRowMap[IntRow] = ExtRow; Matrix->IntToExtColMap[IntRow] = ExtRow; } -/* Translate external column or node number to internal column or node number.*/ - if ((IntCol = Matrix->ExtToIntColMap[ExtCol]) == -1) - { Matrix->ExtToIntRowMap[ExtCol] = ++Matrix->CurrentSize; + /* Translate external column or node number to internal column or node + * number.*/ + if ((IntCol = Matrix->ExtToIntColMap[ExtCol]) == -1) { + Matrix->ExtToIntRowMap[ExtCol] = ++Matrix->CurrentSize; Matrix->ExtToIntColMap[ExtCol] = Matrix->CurrentSize; IntCol = Matrix->CurrentSize; #if NOT EXPANDABLE - vASSERT( IntCol <= Matrix->Size, "Matrix size fixed" ); + vASSERT(IntCol <= Matrix->Size, "Matrix size fixed"); #endif #if EXPANDABLE -/* Re-size Matrix if necessary. */ + /* Re-size Matrix if necessary. */ if (IntCol > Matrix->Size) - EnlargeMatrix( Matrix, IntCol ); - if (Matrix->Error == spNO_MEMORY) return; + EnlargeMatrix(Matrix, IntCol); + if (Matrix->Error == spNO_MEMORY) + return; #endif Matrix->IntToExtRowMap[IntCol] = ExtCol; @@ -515,11 +462,6 @@ int IntRow, IntCol, ExtRow, ExtCol; } #endif - - - - - #if QUAD_ELEMENT /*! * Performs same function as spGetElement() except rather than one @@ -537,54 +479,35 @@ int IntRow, IntCol, ExtRow, ExtCol; * Pointer to the matrix that component is to be entered in. * \param Node1 * Row and column indices for elements. Must be in the range of [0..Size] - * unless the options \a EXPANDABLE or \a TRANSLATE are used. Node zero is the - * ground node. In no case may \a Node1 be less than zero. - * \param Node2 - * Row and column indices for elements. Must be in the range of [0..Size] - * unless the options \a EXPANDABLE or \a TRANSLATE are used. Node zero is the - * ground node. In no case may \a Node2 be less than zero. - * \param Template - * Collection of pointers to four elements that are later used to directly - * address elements. User must supply the template, this routine will - * fill it. + * unless the options \a EXPANDABLE or \a TRANSLATE are used. Node zero is + * the ground node. In no case may \a Node1 be less than zero. \param Node2 Row + * and column indices for elements. Must be in the range of [0..Size] unless the + * options \a EXPANDABLE or \a TRANSLATE are used. Node zero is the ground node. + * In no case may \a Node2 be less than zero. \param Template Collection of + * pointers to four elements that are later used to directly address elements. + * User must supply the template, this routine will fill it. */ -spError -spGetAdmittance( - spMatrix Matrix, - int Node1, - int Node2, - struct spTemplate *Template -) -{ +spError spGetAdmittance(spMatrix Matrix, int Node1, int Node2, + struct spTemplate *Template) { -/* Begin `spGetAdmittance'. */ - Template->Element1 = spGetElement(Matrix, Node1, Node1 ); - Template->Element2 = spGetElement(Matrix, Node2, Node2 ); - Template->Element3Negated = spGetElement( Matrix, Node2, Node1 ); - Template->Element4Negated = spGetElement( Matrix, Node1, Node2 ); - if - ( (Template->Element1 == NULL) - OR (Template->Element2 == NULL) - OR (Template->Element3Negated == NULL) - OR (Template->Element4Negated == NULL) - ) return spNO_MEMORY; + /* Begin `spGetAdmittance'. */ + Template->Element1 = spGetElement(Matrix, Node1, Node1); + Template->Element2 = spGetElement(Matrix, Node2, Node2); + Template->Element3Negated = spGetElement(Matrix, Node2, Node1); + Template->Element4Negated = spGetElement(Matrix, Node1, Node2); + if ((Template->Element1 == NULL) OR(Template->Element2 == NULL) + OR(Template->Element3Negated == NULL) + OR(Template->Element4Negated == NULL)) + return spNO_MEMORY; if (Node1 == 0) - SWAP( RealNumber*, Template->Element1, Template->Element2 ); + SWAP(RealNumber *, Template->Element1, Template->Element2); return spOKAY; } #endif /* QUAD_ELEMENT */ - - - - - - - - #if QUAD_ELEMENT /*! * Similar to spGetAdmittance(), except that spGetAdmittance() only @@ -627,43 +550,25 @@ spGetAdmittance( * fill it. */ -spError -spGetQuad( - spMatrix Matrix, - int Row1, - int Row2, - int Col1, - int Col2, - struct spTemplate *Template -) -{ -/* Begin `spGetQuad'. */ - Template->Element1 = spGetElement( Matrix, Row1, Col1); - Template->Element2 = spGetElement( Matrix, Row2, Col2 ); - Template->Element3Negated = spGetElement( Matrix, Row2, Col1 ); - Template->Element4Negated = spGetElement( Matrix, Row1, Col2 ); - if - ( (Template->Element1 == NULL) - OR (Template->Element2 == NULL) - OR (Template->Element3Negated == NULL) - OR (Template->Element4Negated == NULL) - ) return spNO_MEMORY; +spError spGetQuad(spMatrix Matrix, int Row1, int Row2, int Col1, int Col2, + struct spTemplate *Template) { + /* Begin `spGetQuad'. */ + Template->Element1 = spGetElement(Matrix, Row1, Col1); + Template->Element2 = spGetElement(Matrix, Row2, Col2); + Template->Element3Negated = spGetElement(Matrix, Row2, Col1); + Template->Element4Negated = spGetElement(Matrix, Row1, Col2); + if ((Template->Element1 == NULL) OR(Template->Element2 == NULL) + OR(Template->Element3Negated == NULL) + OR(Template->Element4Negated == NULL)) + return spNO_MEMORY; if (Template->Element1 == &(Matrix)->TrashCan.Real) - SWAP( RealNumber *, Template->Element1, Template->Element2 ); + SWAP(RealNumber *, Template->Element1, Template->Element2); return spOKAY; } #endif /* QUAD_ELEMENT */ - - - - - - - - #if QUAD_ELEMENT /*! * Addition of four structural ones to matrix by index. @@ -701,38 +606,23 @@ spGetQuad( * fill it. */ -spError -spGetOnes( - spMatrix Matrix, - int Pos, - int Neg, - int Eqn, - struct spTemplate *Template -) -{ -/* Begin `spGetOnes'. */ - Template->Element4Negated = spGetElement( Matrix, Neg, Eqn ); - Template->Element3Negated = spGetElement( Matrix, Eqn, Neg ); - Template->Element2 = spGetElement( Matrix, Pos, Eqn ); - Template->Element1 = spGetElement( Matrix, Eqn, Pos ); - if - ( (Template->Element1 == NULL) - OR (Template->Element2 == NULL) - OR (Template->Element3Negated == NULL) - OR (Template->Element4Negated == NULL) - ) return spNO_MEMORY; +spError spGetOnes(spMatrix Matrix, int Pos, int Neg, int Eqn, + struct spTemplate *Template) { + /* Begin `spGetOnes'. */ + Template->Element4Negated = spGetElement(Matrix, Neg, Eqn); + Template->Element3Negated = spGetElement(Matrix, Eqn, Neg); + Template->Element2 = spGetElement(Matrix, Pos, Eqn); + Template->Element1 = spGetElement(Matrix, Eqn, Pos); + if ((Template->Element1 == NULL) OR(Template->Element2 == NULL) + OR(Template->Element3Negated == NULL) + OR(Template->Element4Negated == NULL)) + return spNO_MEMORY; - spADD_REAL_QUAD( *Template, 1.0 ); + spADD_REAL_QUAD(*Template, 1.0); return spOKAY; } #endif /* QUAD_ELEMENT */ - - - - - - /* * FIND DIAGONAL * @@ -753,33 +643,21 @@ spGetOnes( * Pointer to an element in the matrix. */ -ElementPtr -spcFindDiag( - MatrixPtr Matrix, - int Index -) -{ -ElementPtr pElement; +ElementPtr spcFindDiag(MatrixPtr Matrix, int Index) { + ElementPtr pElement; -/* Begin `spcFindDiag'. */ + /* Begin `spcFindDiag'. */ pElement = Matrix->FirstInCol[Index]; -/* Search column for element. */ - while ((pElement != NULL) AND (pElement->Row < Index)) - pElement = pElement->NextInCol; - if (pElement AND (pElement->Row == Index)) - return pElement; + /* Search column for element. */ + while ((pElement != NULL) AND(pElement->Row < Index)) + pElement = pElement->NextInCol; + if (pElement AND(pElement->Row == Index)) + return pElement; else - return NULL; + return NULL; } - - - - - - - /* * CREATE AND SPLICE ELEMENT INTO MATRIX * @@ -819,62 +697,51 @@ ElementPtr pElement; * spNO_MEMORY */ -ElementPtr -spcCreateElement( - MatrixPtr Matrix, - int Row, - int Col, - ElementPtr *ppToLeft, - ElementPtr *ppAbove, - BOOLEAN Fillin -) -{ -ElementPtr pElement, pCreatedElement; +ElementPtr spcCreateElement(MatrixPtr Matrix, int Row, int Col, + ElementPtr *ppToLeft, ElementPtr *ppAbove, + BOOLEAN Fillin) { + ElementPtr pElement, pCreatedElement; -/* Begin `spcCreateElement'. */ + /* Begin `spcCreateElement'. */ -/* Find element immediately above the desired element. */ - pElement = *ppAbove; /* FIXME: Dereference of null pointer */ - while ((pElement != NULL) AND (pElement->Row < Row)) - { ppAbove = &pElement->NextInCol; - pElement = *ppAbove; + /* Find element immediately above the desired element. */ + pElement = *ppAbove; /* FIXME: Dereference of null pointer */ + while ((pElement != NULL) AND(pElement->Row < Row)) { + ppAbove = &pElement->NextInCol; + pElement = *ppAbove; } - if ((pElement != NULL) AND (pElement->Row == Row)) - return pElement; + if ((pElement != NULL) AND(pElement->Row == Row)) + return pElement; -/* The desired element does not exist, create it. */ - if (Fillin) - { pCreatedElement = spcGetFillin( Matrix ); - Matrix->Fillins++; + /* The desired element does not exist, create it. */ + if (Fillin) { + pCreatedElement = spcGetFillin(Matrix); + Matrix->Fillins++; -/* Update Markowitz counts and products. */ - ++Matrix->MarkowitzRow[Row]; - spcMarkoProd( Matrix->MarkowitzProd[Row], - Matrix->MarkowitzRow[Row], - Matrix->MarkowitzCol[Row] ); - if ((Matrix->MarkowitzRow[Row] == 1) AND - (Matrix->MarkowitzCol[Row] != 0)) - { - Matrix->Singletons--; + /* Update Markowitz counts and products. */ + ++Matrix->MarkowitzRow[Row]; + spcMarkoProd(Matrix->MarkowitzProd[Row], Matrix->MarkowitzRow[Row], + Matrix->MarkowitzCol[Row]); + if ((Matrix->MarkowitzRow[Row] == 1) + AND(Matrix->MarkowitzCol[Row] != 0)) { + Matrix->Singletons--; + } + ++Matrix->MarkowitzCol[Col]; + spcMarkoProd(Matrix->MarkowitzProd[Col], Matrix->MarkowitzCol[Col], + Matrix->MarkowitzRow[Col]); + if ((Matrix->MarkowitzRow[Col] != 0) + AND(Matrix->MarkowitzCol[Col] == 1)) { + Matrix->Singletons--; + } + } else { + pCreatedElement = spcGetElement(Matrix); + Matrix->NeedsOrdering = YES; } - ++Matrix->MarkowitzCol[Col]; - spcMarkoProd( Matrix->MarkowitzProd[Col], - Matrix->MarkowitzCol[Col], - Matrix->MarkowitzRow[Col] ); - if ((Matrix->MarkowitzRow[Col] != 0) AND - (Matrix->MarkowitzCol[Col] == 1)) - { - Matrix->Singletons--; - } - } - else - { pCreatedElement = spcGetElement( Matrix ); - Matrix->NeedsOrdering = YES; - } - if (pCreatedElement == NULL) return NULL; + if (pCreatedElement == NULL) + return NULL; Matrix->Elements++; -/* Initialize Element. */ + /* Initialize Element. */ pCreatedElement->Row = Row; pCreatedElement->Col = Col; pCreatedElement->Real = 0.0; @@ -885,63 +752,59 @@ ElementPtr pElement, pCreatedElement; pCreatedElement->pInitInfo = NULL; #endif -/* If element is on diagonal, store pointer in Diag. */ - if (Row == Col) Matrix->Diag[Row] = pCreatedElement; + /* If element is on diagonal, store pointer in Diag. */ + if (Row == Col) + Matrix->Diag[Row] = pCreatedElement; -/* Splice element into column. */ + /* Splice element into column. */ pCreatedElement->NextInCol = *ppAbove; *ppAbove = pCreatedElement; -/* Find Element immediately to the left of the fill-in. */ - if (Matrix->RowsLinked) - { pElement = *ppToLeft; - while (pElement != NULL) - { if (pElement->Col < Col) - { ppToLeft = &pElement->NextInRow; + /* Find Element immediately to the left of the fill-in. */ + if (Matrix->RowsLinked) { pElement = *ppToLeft; + while (pElement != NULL) { + if (pElement->Col < Col) { + ppToLeft = &pElement->NextInRow; + pElement = *ppToLeft; + } else + break; /* while loop */ } - else break; /* while loop */ - } -/* Splice element into row. */ - pCreatedElement->NextInRow = *ppToLeft; - *ppToLeft = pCreatedElement; + /* Splice element into row. */ + pCreatedElement->NextInRow = *ppToLeft; + *ppToLeft = pCreatedElement; } return pCreatedElement; } - -ElementPtr -spcCreateElementOld(MatrixPtr Matrix, int Row, int Col, - ElementPtr *LastAddr, int Fillin) -{ - ElementPtr pElement, pLastElement; - ElementPtr pCreatedElement; +ElementPtr spcCreateElementOld(MatrixPtr Matrix, int Row, int Col, + ElementPtr *LastAddr, int Fillin) { + ElementPtr pElement, pLastElement; + ElementPtr pCreatedElement; /* Begin `spcCreateElement'. */ - if (Matrix->RowsLinked) - { - /* Row pointers cannot be ignored. */ + if (Matrix->RowsLinked) { + /* Row pointers cannot be ignored. */ - if (Fillin) - { - pElement = spcGetFillin( Matrix ); + if (Fillin) { + pElement = spcGetFillin(Matrix); Matrix->Fillins++; - } - else - { - pElement = spcGetElement( Matrix ); - Matrix->Elements++; + } else { + pElement = spcGetElement(Matrix); + Matrix->Elements++; Matrix->NeedsOrdering = YES; } - if (pElement == NULL) return NULL; + if (pElement == NULL) + return NULL; - /* If element is on diagonal, store pointer in Diag. */ - if (Row == Col) Matrix->Diag[Row] = pElement; + /* If element is on diagonal, store pointer in Diag. */ + if (Row == Col) + Matrix->Diag[Row] = pElement; - /* Initialize Element. */ + /* Initialize Element. */ pCreatedElement = pElement; pElement->Row = Row; pElement->Col = Col; @@ -951,55 +814,50 @@ spcCreateElementOld(MatrixPtr Matrix, int Row, int Col, pElement->pInitInfo = NULL; #endif - /* Splice element into column. */ + /* Splice element into column. */ pElement->NextInCol = *LastAddr; *LastAddr = pElement; - /* Search row for proper element position. */ + /* Search row for proper element position. */ pElement = Matrix->FirstInRow[Row]; pLastElement = NULL; - while (pElement != NULL) - { - /* Search for element row position. */ - if (pElement->Col < Col) - { - /* Have not reached desired element. */ + while (pElement != NULL) { + /* Search for element row position. */ + if (pElement->Col < Col) { + /* Have not reached desired element. */ pLastElement = pElement; pElement = pElement->NextInRow; - } - else pElement = NULL; + } else + pElement = NULL; } - /* Splice element into row. */ + /* Splice element into row. */ pElement = pCreatedElement; - if (pLastElement == NULL) - { - /* Element is first in row. */ + if (pLastElement == NULL) { + /* Element is first in row. */ pElement->NextInRow = Matrix->FirstInRow[Row]; Matrix->FirstInRow[Row] = pElement; - } - else - { - /* Element is not first in row. */ + } else { + /* Element is not first in row. */ pElement->NextInRow = pLastElement->NextInRow; pLastElement->NextInRow = pElement; } - } - else - { - /* Matrix has not been factored yet. Thus get element rather - * than fill-in. Also, row pointers can be ignored. */ + } else { + /* Matrix has not been factored yet. Thus get element rather + * than fill-in. Also, row pointers can be ignored. */ - /* Allocate memory for Element. */ - pElement = spcGetElement( Matrix ); - Matrix->Elements++; - if (pElement == NULL) return NULL; + /* Allocate memory for Element. */ + pElement = spcGetElement(Matrix); + Matrix->Elements++; + if (pElement == NULL) + return NULL; - /* If element is on diagonal, store pointer in Diag. */ - if (Row == Col) Matrix->Diag[Row] = pElement; + /* If element is on diagonal, store pointer in Diag. */ + if (Row == Col) + Matrix->Diag[Row] = pElement; - /* Initialize Element. */ + /* Initialize Element. */ pCreatedElement = pElement; pElement->Row = Row; #if DEBUG @@ -1011,7 +869,7 @@ spcCreateElementOld(MatrixPtr Matrix, int Row, int Col, pElement->pInitInfo = NULL; #endif - /* Splice element into column. */ + /* Splice element into column. */ pElement->NextInCol = *LastAddr; *LastAddr = pElement; } @@ -1020,10 +878,6 @@ spcCreateElementOld(MatrixPtr Matrix, int Row, int Col, return pCreatedElement; } - - - - /* * * LINK ROWS @@ -1050,25 +904,22 @@ spcCreateElementOld(MatrixPtr Matrix, int Row, int Col, * Column currently being operated upon. */ -void -spcLinkRows( MatrixPtr Matrix ) -{ -ElementPtr pElement, *FirstInRowEntry; -ArrayOfElementPtrs FirstInRowArray; -int Col; +void spcLinkRows(MatrixPtr Matrix) { + ElementPtr pElement, *FirstInRowEntry; + ArrayOfElementPtrs FirstInRowArray; + int Col; -/* Begin `spcLinkRows'. */ + /* Begin `spcLinkRows'. */ FirstInRowArray = Matrix->FirstInRow; for (Col = Matrix->Size; Col >= 1; Col--) - FirstInRowArray[Col] = NULL; + FirstInRowArray[Col] = NULL; - for (Col = Matrix->Size; Col >= 1; Col--) - { -/* Generate row links for the elements in the Col'th column. */ + for (Col = Matrix->Size; Col >= 1; Col--) { + /* Generate row links for the elements in the Col'th column. */ pElement = Matrix->FirstInCol[Col]; - while (pElement != NULL) - { pElement->Col = Col; + while (pElement != NULL) { + pElement->Col = Col; FirstInRowEntry = &FirstInRowArray[pElement->Row]; pElement->NextInRow = *FirstInRowEntry; *FirstInRowEntry = pElement; @@ -1079,13 +930,8 @@ int Col; return; } - - - - - #if EXPANDABLE - + /* * ENLARGE MATRIX * @@ -1102,60 +948,55 @@ int Col; * The allocated size of the matrix before it is expanded. */ -static void -EnlargeMatrix( - MatrixPtr Matrix, - int NewSize -) -{ -int I, OldAllocatedSize = Matrix->AllocatedSize; +static void EnlargeMatrix(MatrixPtr Matrix, int NewSize) { + int I, OldAllocatedSize = Matrix->AllocatedSize; -/* Begin `EnlargeMatrix'. */ + /* Begin `EnlargeMatrix'. */ Matrix->Size = NewSize; if (NewSize <= OldAllocatedSize) return; -/* Expand the matrix frame. */ - NewSize = MAX( NewSize, (int)(EXPANSION_FACTOR * OldAllocatedSize) ); + /* Expand the matrix frame. */ + NewSize = MAX(NewSize, (int)(EXPANSION_FACTOR * OldAllocatedSize)); Matrix->AllocatedSize = NewSize; - if (( SP_REALLOC(Matrix->IntToExtColMap, int, NewSize+1)) == NULL) - { Matrix->Error = spNO_MEMORY; + if ((SP_REALLOC(Matrix->IntToExtColMap, int, NewSize + 1)) == NULL) { + Matrix->Error = spNO_MEMORY; return; } - if (( SP_REALLOC(Matrix->IntToExtRowMap, int, NewSize+1)) == NULL) - { Matrix->Error = spNO_MEMORY; + if ((SP_REALLOC(Matrix->IntToExtRowMap, int, NewSize + 1)) == NULL) { + Matrix->Error = spNO_MEMORY; return; } - if (( SP_REALLOC(Matrix->Diag, ElementPtr, NewSize+1)) == NULL) - { Matrix->Error = spNO_MEMORY; + if ((SP_REALLOC(Matrix->Diag, ElementPtr, NewSize + 1)) == NULL) { + Matrix->Error = spNO_MEMORY; return; } - if (( SP_REALLOC(Matrix->FirstInCol, ElementPtr, NewSize+1)) == NULL) - { Matrix->Error = spNO_MEMORY; + if ((SP_REALLOC(Matrix->FirstInCol, ElementPtr, NewSize + 1)) == NULL) { + Matrix->Error = spNO_MEMORY; return; } - if (( SP_REALLOC(Matrix->FirstInRow, ElementPtr, NewSize+1)) == NULL) - { Matrix->Error = spNO_MEMORY; + if ((SP_REALLOC(Matrix->FirstInRow, ElementPtr, NewSize + 1)) == NULL) { + Matrix->Error = spNO_MEMORY; return; } -/* - * Destroy the Markowitz and Intermediate vectors, they will be recreated - * in spOrderAndFactor(). - */ - SP_FREE( Matrix->MarkowitzRow ); - SP_FREE( Matrix->MarkowitzCol ); - SP_FREE( Matrix->MarkowitzProd ); - SP_FREE( Matrix->DoRealDirect ); - SP_FREE( Matrix->DoCmplxDirect ); - SP_FREE( Matrix->Intermediate ); + /* + * Destroy the Markowitz and Intermediate vectors, they will be recreated + * in spOrderAndFactor(). + */ + SP_FREE(Matrix->MarkowitzRow); + SP_FREE(Matrix->MarkowitzCol); + SP_FREE(Matrix->MarkowitzProd); + SP_FREE(Matrix->DoRealDirect); + SP_FREE(Matrix->DoCmplxDirect); + SP_FREE(Matrix->Intermediate); Matrix->InternalVectorsAllocated = NO; -/* Initialize the new portion of the vectors. */ - for (I = OldAllocatedSize+1; I <= NewSize; I++) - { Matrix->IntToExtColMap[I] = I; + /* Initialize the new portion of the vectors. */ + for (I = OldAllocatedSize + 1; I <= NewSize; I++) { + Matrix->IntToExtColMap[I] = I; Matrix->IntToExtRowMap[I] = I; Matrix->Diag[I] = NULL; Matrix->FirstInRow[I] = NULL; @@ -1166,14 +1007,8 @@ int I, OldAllocatedSize = Matrix->AllocatedSize; } #endif - - - - - - #if TRANSLATE - + /* * EXPAND TRANSLATION ARRAYS * @@ -1191,36 +1026,31 @@ int I, OldAllocatedSize = Matrix->AllocatedSize; * The allocated size of the translation arrays before being expanded. */ -static void -ExpandTranslationArrays( - MatrixPtr Matrix, - int NewSize -) -{ -int I, OldAllocatedSize = Matrix->AllocatedExtSize; +static void ExpandTranslationArrays(MatrixPtr Matrix, int NewSize) { + int I, OldAllocatedSize = Matrix->AllocatedExtSize; -/* Begin `ExpandTranslationArrays'. */ + /* Begin `ExpandTranslationArrays'. */ Matrix->ExtSize = NewSize; if (NewSize <= OldAllocatedSize) return; -/* Expand the translation arrays ExtToIntRowMap and ExtToIntColMap. */ - NewSize = MAX( NewSize, (int)(EXPANSION_FACTOR * OldAllocatedSize) ); + /* Expand the translation arrays ExtToIntRowMap and ExtToIntColMap. */ + NewSize = MAX(NewSize, (int)(EXPANSION_FACTOR * OldAllocatedSize)); Matrix->AllocatedExtSize = NewSize; - if (( SP_REALLOC(Matrix->ExtToIntRowMap, int, NewSize+1)) == NULL) - { Matrix->Error = spNO_MEMORY; + if ((SP_REALLOC(Matrix->ExtToIntRowMap, int, NewSize + 1)) == NULL) { + Matrix->Error = spNO_MEMORY; return; } - if (( SP_REALLOC(Matrix->ExtToIntColMap, int, NewSize+1)) == NULL) - { Matrix->Error = spNO_MEMORY; + if ((SP_REALLOC(Matrix->ExtToIntColMap, int, NewSize + 1)) == NULL) { + Matrix->Error = spNO_MEMORY; return; } -/* Initialize the new portion of the vectors. */ - for (I = OldAllocatedSize+1; I <= NewSize; I++) - { Matrix->ExtToIntRowMap[I] = -1; + /* Initialize the new portion of the vectors. */ + for (I = OldAllocatedSize + 1; I <= NewSize; I++) { + Matrix->ExtToIntRowMap[I] = -1; Matrix->ExtToIntColMap[I] = -1; } @@ -1228,14 +1058,6 @@ int I, OldAllocatedSize = Matrix->AllocatedExtSize; } #endif - - - - - - - - #if INITIALIZE /*! * Initialize the matrix. @@ -1272,61 +1094,51 @@ int I, OldAllocatedSize = Matrix->AllocatedExtSize; * \see spClear() */ -int -spInitialize( - MatrixPtr Matrix, - int (*pInit)( - spElement *pElement, - spGenericPtr pInitInfo, - int Row, - int Col - ) -) -{ -ElementPtr pElement; -int J, Error, Col; +int spInitialize(MatrixPtr Matrix, + int (*pInit)(spElement *pElement, spGenericPtr pInitInfo, + int Row, int Col)) { + ElementPtr pElement; + int J, Error, Col; -/* Begin `spInitialize'. */ - ASSERT_IS_SPARSE( Matrix ); + /* Begin `spInitialize'. */ + ASSERT_IS_SPARSE(Matrix); #if spCOMPLEX -/* Clear imaginary part of matrix if matrix is real but was complex. */ - if (Matrix->PreviousMatrixWasComplex AND NOT Matrix->Complex) - { for (J = Matrix->Size; J > 0; J--) - { pElement = Matrix->FirstInCol[J]; - while (pElement != NULL) - { pElement->Imag = 0.0; + /* Clear imaginary part of matrix if matrix is real but was complex. */ + if (Matrix->PreviousMatrixWasComplex AND NOT Matrix->Complex) { + for (J = Matrix->Size; J > 0; J--) { + pElement = Matrix->FirstInCol[J]; + while (pElement != NULL) { + pElement->Imag = 0.0; pElement = pElement->NextInCol; } } } #endif /* spCOMPLEX */ -/* Initialize the matrix. */ - for (J = Matrix->Size; J > 0; J--) - { pElement = Matrix->FirstInCol[J]; + /* Initialize the matrix. */ + for (J = Matrix->Size; J > 0; J--) { + pElement = Matrix->FirstInCol[J]; Col = Matrix->IntToExtColMap[J]; - while (pElement != NULL) - { if (pElement->pInitInfo == NULL) - { pElement->Real = 0.0; -# if spCOMPLEX - pElement->Imag = 0.0; -# endif - } - else - { Error = (*pInit)((RealNumber *)pElement, pElement->pInitInfo, + while (pElement != NULL) { + if (pElement->pInitInfo == NULL) { + pElement->Real = 0.0; +#if spCOMPLEX + pElement->Imag = 0.0; +#endif + } else { + Error = (*pInit)((RealNumber *)pElement, pElement->pInitInfo, Matrix->IntToExtRowMap[pElement->Row], Col); - if (Error) - { Matrix->Error = spMANGLED; + if (Error) { + Matrix->Error = spMANGLED; return Error; } - } pElement = pElement->NextInCol; } } -/* Empty the trash. */ + /* Empty the trash. */ Matrix->TrashCan.Real = 0.0; #if spCOMPLEX Matrix->TrashCan.Imag = 0.0; @@ -1340,9 +1152,6 @@ int J, Error, Col; return 0; } - - - /*! * This function installs a pointer to a data structure that is used * to contain initialization information to a matrix element. It is @@ -1356,19 +1165,13 @@ int J, Error, Col; * \see spInitialize() */ -void -spInstallInitInfo( - spElement *pElement, - spGenericPtr pInitInfo -) -{ -/* Begin `spInstallInitInfo'. */ - vASSERT( pElement != NULL, "Invalid element pointer" ); +void spInstallInitInfo(spElement *pElement, spGenericPtr pInitInfo) { + /* Begin `spInstallInitInfo'. */ + vASSERT(pElement != NULL, "Invalid element pointer"); ((ElementPtr)pElement)->pInitInfo = pInitInfo; } - /*! * This function returns a pointer to a data structure that is used * to contain initialization information to a matrix element. @@ -1382,13 +1185,9 @@ spInstallInitInfo( * * \see spInitialize() */ -spGenericPtr -spGetInitInfo( - spElement *pElement -) -{ -/* Begin `spGetInitInfo'. */ - vASSERT( pElement != NULL, "Invalid element pointer" ); +spGenericPtr spGetInitInfo(spElement *pElement) { + /* Begin `spGetInitInfo'. */ + vASSERT(pElement != NULL, "Invalid element pointer"); return (spGenericPtr)((ElementPtr)pElement)->pInitInfo; } diff --git a/src/maths/sparse/spConfig.h b/src/maths/sparse/spConfig.h index 3526653c3..95a3ec5e5 100644 --- a/src/maths/sparse/spConfig.h +++ b/src/maths/sparse/spConfig.h @@ -19,7 +19,6 @@ * Kenneth S. Kundert */ - /* * Revision and copyright information. * @@ -29,13 +28,9 @@ * $Revision: 1.5 $ */ - #ifndef spCONFIG_DEFS #define spCONFIG_DEFS - - - #ifdef spINSIDE_SPARSE /* * OPTIONS @@ -67,7 +62,7 @@ * slight speed and memory advantage if the routines are complied * to handle only real systems of equations. */ -#define REAL YES +#define REAL YES /*! * Setting this compiler flag true (1) makes the matrix @@ -78,7 +73,7 @@ * the size of the matrix need not be known before the matrix is * built. The matrix can be allocated with size zero and expanded. */ -#define EXPANDABLE YES +#define EXPANDABLE YES /*! * This option allows the set of external row and column numbers @@ -98,7 +93,7 @@ * vectors must be at least as large as the external size, which * is the value of the largest given row or column numbers. */ -#define TRANSLATE YES +#define TRANSLATE YES /*! * Causes the spInitialize(), spGetInitInfo(), and @@ -110,7 +105,7 @@ * column numbers as arguments. This allows the user to write * custom matrix initialization routines. */ -#define INITIALIZE NO +#define INITIALIZE NO /*! * Many matrices, and in particular node- and modified-node @@ -136,7 +131,7 @@ * options and constants are not used: \a MODIFIED_MARKOWITZ, * \a MAX_MARKOWITZ_TIES, and \a TIES_MULTIPLIER. */ -#define DIAGONAL_PIVOTING YES +#define DIAGONAL_PIVOTING YES /*! * This determines whether arrays start at an index of zero or one. @@ -151,7 +146,7 @@ * matrix. ARRAY_OFFSET must be either 0 or 1, no other offsets * are valid. */ -#define ARRAY_OFFSET YES +#define ARRAY_OFFSET YES /*! * This specifies that the modified Markowitz method of pivot @@ -173,19 +168,19 @@ * for use when working with very large matrices where the initial * factor time represents an unacceptable burden. \a NO is recommended. */ -#define MODIFIED_MARKOWITZ NO +#define MODIFIED_MARKOWITZ NO /*! * This specifies that the spDeleteRowAndCol() routine * should be compiled. Note that for this routine to be * compiled, both \a DELETE and \a TRANSLATE should be set true. */ -#define DELETE NO +#define DELETE NO /*! * This specifies that the spStripFills() routine should be compiled. */ -#define STRIP NO +#define STRIP NO /*! * This specifies that the routine that preorders modified node @@ -193,7 +188,7 @@ * in greater speed and accuracy if used with this type of * matrix. */ -#define MODIFIED_NODAL YES +#define MODIFIED_NODAL YES /*! * This specifies that the routines that allow four related @@ -202,7 +197,7 @@ * admittance. The routines affected by \a QUAD_ELEMENT are the * spGetAdmittance(), spGetQuad() and spGetOnes() routines. */ -#define QUAD_ELEMENT NO +#define QUAD_ELEMENT NO /*! * This specifies that the routines that solve the matrix as if @@ -210,7 +205,7 @@ * useful when performing sensitivity analysis using the adjoint * method. */ -#define TRANSPOSE YES +#define TRANSPOSE YES /*! * This specifies that the routine that performs scaling on the @@ -224,26 +219,26 @@ * solution by the user or the scaled factors may simply be * thrown away. \a NO is recommended. */ -#define SCALING NO +#define SCALING NO /*! * This specifies that routines that are used to document the * matrix, such as spPrint() and spFileMatrix(), should be * compiled. */ -#define DOCUMENTATION YES +#define DOCUMENTATION YES /*! * This specifies that routines that are used to multiply the * matrix by a vector, such as spMultiply() and spMultTransposed(), should be * compiled. */ -#define MULTIPLICATION YES +#define MULTIPLICATION YES /*! * This specifies that the routine spDeterminant() should be complied. */ -#define DETERMINANT YES +#define DETERMINANT YES /*! * This specifies that spLargestElement() and spRoundoff() should @@ -255,28 +250,28 @@ * If the bound increases greatly after using spFactor(), then the * matrix should probably be reordered. Recomend \a NO. */ -#define STABILITY NO +#define STABILITY NO /*! * This specifies that spCondition() and spNorm(), the code that * computes a good estimate of the condition number of the matrix, * should be compiled. Recomend \a NO. */ -#define CONDITION NO +#define CONDITION NO /*! * This specifies that spPseudoCondition(), the code that computes * a crude and easily fooled indicator of ill-conditioning in the * matrix, should be compiled. Recomend \a NO. */ -#define PSEUDOCONDITION NO +#define PSEUDOCONDITION NO /*! * This specifies that the \a FORTRAN interface routines should be * compiled. When interfacing to \a FORTRAN programs, the \a ARRAY_OFFSET * options should be set to NO. */ -#define FORTRAN NO +#define FORTRAN NO /*! * This specifies that additional error checking will be compiled. @@ -285,7 +280,7 @@ * the routines have been integrated in and are running smoothly, this * option should be turned off. \a YES is recommended. */ -#define DEBUG YES +#define DEBUG YES #endif /* spINSIDE_SPARSE */ @@ -300,7 +295,7 @@ * This specifies that the routines will be complied to handle * complex systems of equations. */ -#define spCOMPLEX 1 +#define spCOMPLEX 1 /*! * This specifies the format for complex vectors. If this is set @@ -313,16 +308,10 @@ * vector is represented by two arrays of \a spREALs, one with * the real terms, the other with the imaginary. \a NO is recommended. */ -#define spSEPARATED_COMPLEX_VECTORS 1 +#define spSEPARATED_COMPLEX_VECTORS 1 #ifdef spINSIDE_SPARSE - - - - - - /* * MATRIX CONSTANTS * @@ -339,28 +328,28 @@ * not be less than or equal to zero nor larger than one. * 0.001 is recommended. */ -#define DEFAULT_THRESHOLD 1.0e-3 +#define DEFAULT_THRESHOLD 1.0e-3 /*! * This indicates whether spOrderAndFactor() should use diagonal * pivoting as default. This issue only arises when * spOrderAndFactor() is called from spFactor(). \a YES is recommended. */ -#define DIAG_PIVOTING_AS_DEFAULT YES +#define DIAG_PIVOTING_AS_DEFAULT YES /*! * This number multiplied by the size of the matrix equals the number * of elements for which memory is initially allocated in spCreate(). * 6 is recommended. */ -#define SPACE_FOR_ELEMENTS 6 +#define SPACE_FOR_ELEMENTS 6 /*! * This number multiplied by the size of the matrix equals the number * of elements for which memory is initially allocated and specifically * reserved for fill-ins in spCreate(). 4 is recommended. */ -#define SPACE_FOR_FILL_INS 4 +#define SPACE_FOR_FILL_INS 4 /*! * The number of matrix elements requested from the malloc utility on @@ -370,7 +359,7 @@ * elements at a time (or some multiple thereof). * 31 is recommended. */ -#define ELEMENTS_PER_ALLOCATION 31 +#define ELEMENTS_PER_ALLOCATION 31 /*! * The minimum allocated size of a matrix. Note that this does not @@ -379,13 +368,13 @@ * allocated with an estimated size of zero. This number should not * be less than one. */ -#define MINIMUM_ALLOCATED_SIZE 6 +#define MINIMUM_ALLOCATED_SIZE 6 /*! * The amount the allocated size of the matrix is increased when it * is expanded. */ -#define EXPANSION_FACTOR 1.5 +#define EXPANSION_FACTOR 1.5 /*! * Some terminology should be defined. The Markowitz row count is the number @@ -412,7 +401,7 @@ * 100 is recommended. * \see TIES_MULTIPLIER */ -#define MAX_MARKOWITZ_TIES 100 +#define MAX_MARKOWITZ_TIES 100 /*! * Specifies the number of Markowitz ties that are allowed to occur @@ -429,7 +418,7 @@ * diagonal pivoting breaks down. 5 is recommended. * \see MAX_MARKOWITZ_TIES */ -#define TIES_MULTIPLIER 5 +#define TIES_MULTIPLIER 5 /*! * Which partition mode is used by spPartition() as default. @@ -441,77 +430,61 @@ * overhead, but speeds up both dense and sparse matrices, best if there * is a large number of matrices that can use the same ordering. */ -#define DEFAULT_PARTITION spAUTO_PARTITION +#define DEFAULT_PARTITION spAUTO_PARTITION /*! * The number of characters per page width. Set to 80 for terminal, * 132 for line printer. Controls how many columns printed by * spPrint() per page width. */ -#define PRINTER_WIDTH 80 +#define PRINTER_WIDTH 80 - - - - - - - - - #endif /* spINSIDE_SPARSE */ /* * PORTABILITY MACROS */ #ifdef __STDC__ -# define spcCONCAT(prefix,suffix) prefix ## suffix -# define spcQUOTE(x) # x -# define spcFUNC_NEEDS_FILE(func,file) \ - func ## _requires_ ## file ## _to_be_included_ +#define spcCONCAT(prefix, suffix) prefix##suffix +#define spcQUOTE(x) #x +#define spcFUNC_NEEDS_FILE(func, file) func##_requires_##file##_to_be_included_ #else -# define spcCONCAT(prefix,suffix) prefix/**/suffix -# define spcQUOTE(x) "x" -# define spcFUNC_NEEDS_FILE(func,file) \ - func/**/_requires_/**/file/**/_to_be_included_ +#define spcCONCAT(prefix, suffix) prefix /**/ suffix +#define spcQUOTE(x) "x" +#define spcFUNC_NEEDS_FILE(func, file) \ + func /**/ _requires_ /**/ file /**/ _to_be_included_ #endif #if defined(__cplusplus) || defined(c_plusplus) - /* - * Definitions for C++ - */ -# define spcEXTERN extern "C" -# define spcNO_ARGS -# define spcCONST const - typedef void *spGenericPtr; +/* + * Definitions for C++ + */ +#define spcEXTERN extern "C" +#define spcNO_ARGS +#define spcCONST const +typedef void *spGenericPtr; #else #ifdef __STDC__ - /* - * Definitions for ANSI C - */ -# define spcEXTERN extern -# define spcNO_ARGS void -# define spcCONST const - typedef void *spGenericPtr; -# else - /* - * Definitions for K&R C -- ignore function prototypes - */ -# define spcEXTERN extern -# define spcNO_ARGS -# define spcCONST - typedef char *spGenericPtr; +/* + * Definitions for ANSI C + */ +#define spcEXTERN extern +#define spcNO_ARGS void +#define spcCONST const +typedef void *spGenericPtr; +#else +/* + * Definitions for K&R C -- ignore function prototypes + */ +#define spcEXTERN extern +#define spcNO_ARGS +#define spcCONST +typedef char *spGenericPtr; #endif #endif #ifdef spINSIDE_SPARSE - - - - - - /* * MACHINE CONSTANTS * @@ -519,29 +492,24 @@ */ /* Begin machine constants. */ -#include #include +#include /*! The resolution of spREAL. */ -#define MACHINE_RESOLUTION DBL_EPSILON +#define MACHINE_RESOLUTION DBL_EPSILON /*! The largest possible value of spREAL. */ -#define LARGEST_REAL DBL_MAX +#define LARGEST_REAL DBL_MAX /*! The smalles possible positive value of spREAL. */ -#define SMALLEST_REAL DBL_MIN +#define SMALLEST_REAL DBL_MIN /*! The largest possible value of shorts. */ -#define LARGEST_SHORT_INTEGER SHRT_MAX +#define LARGEST_SHORT_INTEGER SHRT_MAX /*! The largest possible value of longs. */ -#define LARGEST_LONG_INTEGER LONG_MAX +#define LARGEST_LONG_INTEGER LONG_MAX - - - - - /* ANNOTATION */ /*! * This macro changes the amount of annotation produced by the matrix @@ -550,23 +518,23 @@ * the program. Possible values include \a NONE, \a ON_STRANGE_BEHAVIOR, and * \a FULL. \a NONE is recommended. */ -#define ANNOTATE NONE +#define ANNOTATE NONE /*! * A possible value for \a ANNOTATE. Disables all annotation. */ -#define NONE 0 +#define NONE 0 /*! * A possible value for \a ANNOTATE. Causes annotation to be produce * upon unusual occurances only. */ -#define ON_STRANGE_BEHAVIOR 1 +#define ON_STRANGE_BEHAVIOR 1 /*! * A possible value for \a ANNOTATE. Enables full annotation. */ -#define FULL 2 +#define FULL 2 #endif /* spINSIDE_SPARSE */ #endif /* spCONFIG_DEFS */ diff --git a/src/maths/sparse/spDefs.h b/src/maths/sparse/spDefs.h index 7aa8ee2b3..5c4fdb239 100644 --- a/src/maths/sparse/spDefs.h +++ b/src/maths/sparse/spDefs.h @@ -9,7 +9,6 @@ * matrix routines. These definitions are of no interest to the user. */ - /* * Revision and copyright information. * @@ -19,64 +18,55 @@ * $Revision: 1.2 $ */ - - - /* * If running lint, change some of the compiler options to get a more * complete inspection. */ #ifdef lint -#undef REAL -#undef spCOMPLEX -#undef EXPANDABLE -#undef TRANSLATE -#undef INITIALIZE -#undef DELETE -#undef STRIP -#undef MODIFIED_NODAL -#undef QUAD_ELEMENT -#undef TRANSPOSE -#undef SCALING -#undef DOCUMENTATION -#undef MULTIPLICATION -#undef DETERMINANT -#undef CONDITION -#undef PSEUDOCONDITION -#undef FORTRAN -#undef DEBUG +#undef REAL +#undef spCOMPLEX +#undef EXPANDABLE +#undef TRANSLATE +#undef INITIALIZE +#undef DELETE +#undef STRIP +#undef MODIFIED_NODAL +#undef QUAD_ELEMENT +#undef TRANSPOSE +#undef SCALING +#undef DOCUMENTATION +#undef MULTIPLICATION +#undef DETERMINANT +#undef CONDITION +#undef PSEUDOCONDITION +#undef FORTRAN +#undef DEBUG -#define REAL YES -#define spCOMPLEX YES -#define EXPANDABLE YES -#define TRANSLATE YES -#define INITIALIZE YES -#define DELETE YES -#define STRIP YES -#define MODIFIED_NODAL YES -#define QUAD_ELEMENT YES -#define TRANSPOSE YES -#define SCALING YES -#define DOCUMENTATION YES -#define MULTIPLICATION YES -#define DETERMINANT YES -#define CONDITION YES -#define PSEUDOCONDITION YES -#define FORTRAN YES -#define DEBUG YES +#define REAL YES +#define spCOMPLEX YES +#define EXPANDABLE YES +#define TRANSLATE YES +#define INITIALIZE YES +#define DELETE YES +#define STRIP YES +#define MODIFIED_NODAL YES +#define QUAD_ELEMENT YES +#define TRANSPOSE YES +#define SCALING YES +#define DOCUMENTATION YES +#define MULTIPLICATION YES +#define DETERMINANT YES +#define CONDITION YES +#define PSEUDOCONDITION YES +#define FORTRAN YES +#define DEBUG YES -#define LINT YES +#define LINT YES #else /* not lint */ -#define LINT NO +#define LINT NO #endif /* not lint */ - - - - - - /* * MACRO DEFINITIONS * @@ -89,50 +79,52 @@ /* Begin macros. */ /* Boolean data type */ -#define BOOLEAN int -#define NO 0 -#define YES 1 -#define NOT ! -#define AND && -#define OR || +#define BOOLEAN int +#define NO 0 +#define YES 1 +#define NOT ! +#define AND && +#define OR || /* NULL pointer */ -#ifndef NULL -#define NULL 0 +#ifndef NULL +#define NULL 0 #endif /* Define macros for validating matrix. */ -#define SPARSE_ID 0xDeadBeef /* Arbitrary. */ -#define IS_SPARSE(matrix) (((matrix) != NULL) AND \ - ((matrix)->ID == SPARSE_ID)) -#define NO_ERRORS(matrix) (((matrix)->Error >= spOKAY) AND \ - ((matrix)->Error < spFATAL)) -#define IS_FACTORED(matrix) ((matrix)->Factored AND \ - NOT (matrix)->NeedsOrdering) +#define SPARSE_ID 0xDeadBeef /* Arbitrary. */ +#define IS_SPARSE(matrix) (((matrix) != NULL) AND((matrix)->ID == SPARSE_ID)) +#define NO_ERRORS(matrix) \ + (((matrix)->Error >= spOKAY) AND((matrix)->Error < spFATAL)) +#define IS_FACTORED(matrix) ((matrix)->Factored AND NOT(matrix)->NeedsOrdering) -#define ASSERT_IS_SPARSE(matrix) vASSERT( IS_SPARSE(matrix), \ - spcMatrixIsNotValid ) -#define ASSERT_NO_ERRORS(matrix) vASSERT( NO_ERRORS(matrix), \ - spcErrorsMustBeCleared ) -#define ASSERT_IS_FACTORED(matrix) vASSERT( IS_FACTORED(matrix), \ - spcMatrixMustBeFactored ) -#define ASSERT_IS_NOT_FACTORED(matrix) vASSERT( NOT (matrix)->Factored, \ - spcMatrixMustNotBeFactored ) +#define ASSERT_IS_SPARSE(matrix) vASSERT(IS_SPARSE(matrix), spcMatrixIsNotValid) +#define ASSERT_NO_ERRORS(matrix) \ + vASSERT(NO_ERRORS(matrix), spcErrorsMustBeCleared) +#define ASSERT_IS_FACTORED(matrix) \ + vASSERT(IS_FACTORED(matrix), spcMatrixMustBeFactored) +#define ASSERT_IS_NOT_FACTORED(matrix) \ + vASSERT(NOT(matrix)->Factored, spcMatrixMustNotBeFactored) /* Macro commands */ /* Macro functions that return the maximum or minimum independent of type. */ -#define MAX(a,b) ((a) > (b) ? (a) : (b)) -#define MIN(a,b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) /* Macro function that returns the absolute value of a floating point number. */ -#define ABS(a) ((a) < 0 ? -(a) : (a)) +#define ABS(a) ((a) < 0 ? -(a) : (a)) /* Macro function that returns the square of a number. */ -#define SQR(a) ((a)*(a)) +#define SQR(a) ((a) * (a)) /* Macro procedure that swaps two entities. */ -#define SWAP(type, a, b) {type swapx; swapx = a; a = b; b = swapx;} - +#define SWAP(type, a, b) \ + { \ + type swapx; \ + swapx = a; \ + a = b; \ + b = swapx; \ + } /* * COMPLEX OPERATION MACROS @@ -141,233 +133,250 @@ /* Macro function that returns the approx absolute value of a complex number. */ #if spCOMPLEX -#define ELEMENT_MAG(ptr) (ABS((ptr)->Real) + ABS((ptr)->Imag)) +#define ELEMENT_MAG(ptr) (ABS((ptr)->Real) + ABS((ptr)->Imag)) #else -#define ELEMENT_MAG(ptr) ((ptr)->Real < 0.0 ? -(ptr)->Real : (ptr)->Real) +#define ELEMENT_MAG(ptr) ((ptr)->Real < 0.0 ? -(ptr)->Real : (ptr)->Real) #endif /* Complex assignment statements. */ -#define CMPLX_ASSIGN(to,from) \ -{ (to).Real = (from).Real; \ - (to).Imag = (from).Imag; \ -} -#define CMPLX_CONJ_ASSIGN(to,from) \ -{ (to).Real = (from).Real; \ - (to).Imag = -(from).Imag; \ -} -#define CMPLX_NEGATE_ASSIGN(to,from) \ -{ (to).Real = -(from).Real; \ - (to).Imag = -(from).Imag; \ -} -#define CMPLX_CONJ_NEGATE_ASSIGN(to,from) \ -{ (to).Real = -(from).Real; \ - (to).Imag = (from).Imag; \ -} -#define CMPLX_CONJ(a) (a).Imag = -(a).Imag -#define CMPLX_NEGATE(a) \ -{ (a).Real = -(a).Real; \ - (a).Imag = -(a).Imag; \ -} +#define CMPLX_ASSIGN(to, from) \ + { \ + (to).Real = (from).Real; \ + (to).Imag = (from).Imag; \ + } +#define CMPLX_CONJ_ASSIGN(to, from) \ + { \ + (to).Real = (from).Real; \ + (to).Imag = -(from).Imag; \ + } +#define CMPLX_NEGATE_ASSIGN(to, from) \ + { \ + (to).Real = -(from).Real; \ + (to).Imag = -(from).Imag; \ + } +#define CMPLX_CONJ_NEGATE_ASSIGN(to, from) \ + { \ + (to).Real = -(from).Real; \ + (to).Imag = (from).Imag; \ + } +#define CMPLX_CONJ(a) (a).Imag = -(a).Imag +#define CMPLX_NEGATE(a) \ + { \ + (a).Real = -(a).Real; \ + (a).Imag = -(a).Imag; \ + } /* Macro that returns the approx magnitude (L-1 norm) of a complex number. */ -#define CMPLX_1_NORM(a) (ABS((a).Real) + ABS((a).Imag)) +#define CMPLX_1_NORM(a) (ABS((a).Real) + ABS((a).Imag)) /* Macro that returns the approx magnitude (L-infinity norm) of a complex. */ -#define CMPLX_INF_NORM(a) (MAX (ABS((a).Real),ABS((a).Imag))) +#define CMPLX_INF_NORM(a) (MAX(ABS((a).Real), ABS((a).Imag))) /* Macro function that returns the magnitude (L-2 norm) of a complex number. */ -#define CMPLX_2_NORM(a) (sqrt((a).Real*(a).Real + (a).Imag*(a).Imag)) +#define CMPLX_2_NORM(a) (sqrt((a).Real * (a).Real + (a).Imag * (a).Imag)) /* Macro function that performs complex addition. */ -#define CMPLX_ADD(to,from_a,from_b) \ -{ (to).Real = (from_a).Real + (from_b).Real; \ - (to).Imag = (from_a).Imag + (from_b).Imag; \ -} +#define CMPLX_ADD(to, from_a, from_b) \ + { \ + (to).Real = (from_a).Real + (from_b).Real; \ + (to).Imag = (from_a).Imag + (from_b).Imag; \ + } /* Macro function that performs complex subtraction. */ -#define CMPLX_SUBT(to,from_a,from_b) \ -{ (to).Real = (from_a).Real - (from_b).Real; \ - (to).Imag = (from_a).Imag - (from_b).Imag; \ -} +#define CMPLX_SUBT(to, from_a, from_b) \ + { \ + (to).Real = (from_a).Real - (from_b).Real; \ + (to).Imag = (from_a).Imag - (from_b).Imag; \ + } /* Macro function that is equivalent to += operator for complex numbers. */ -#define CMPLX_ADD_ASSIGN(to,from) \ -{ (to).Real += (from).Real; \ - (to).Imag += (from).Imag; \ -} +#define CMPLX_ADD_ASSIGN(to, from) \ + { \ + (to).Real += (from).Real; \ + (to).Imag += (from).Imag; \ + } /* Macro function that is equivalent to -= operator for complex numbers. */ -#define CMPLX_SUBT_ASSIGN(to,from) \ -{ (to).Real -= (from).Real; \ - (to).Imag -= (from).Imag; \ -} +#define CMPLX_SUBT_ASSIGN(to, from) \ + { \ + (to).Real -= (from).Real; \ + (to).Imag -= (from).Imag; \ + } /* Macro function that multiplies a complex number by a scalar. */ -#define SCLR_MULT(to,sclr,cmplx) \ -{ (to).Real = (sclr) * (cmplx).Real; \ - (to).Imag = (sclr) * (cmplx).Imag; \ -} +#define SCLR_MULT(to, sclr, cmplx) \ + { \ + (to).Real = (sclr) * (cmplx).Real; \ + (to).Imag = (sclr) * (cmplx).Imag; \ + } /* Macro function that multiply-assigns a complex number by a scalar. */ -#define SCLR_MULT_ASSIGN(to,sclr) \ -{ (to).Real *= (sclr); \ - (to).Imag *= (sclr); \ -} +#define SCLR_MULT_ASSIGN(to, sclr) \ + { \ + (to).Real *= (sclr); \ + (to).Imag *= (sclr); \ + } /* Macro function that multiplies two complex numbers. */ -#define CMPLX_MULT(to,from_a,from_b) \ -{ (to).Real = (from_a).Real * (from_b).Real - \ - (from_a).Imag * (from_b).Imag; \ - (to).Imag = (from_a).Real * (from_b).Imag + \ - (from_a).Imag * (from_b).Real; \ -} +#define CMPLX_MULT(to, from_a, from_b) \ + { \ + (to).Real = \ + (from_a).Real * (from_b).Real - (from_a).Imag * (from_b).Imag; \ + (to).Imag = \ + (from_a).Real * (from_b).Imag + (from_a).Imag * (from_b).Real; \ + } /* Macro function that implements to *= from for complex numbers. */ -#define CMPLX_MULT_ASSIGN(to,from) \ -{ RealNumber to_real_ = (to).Real; \ - (to).Real = to_real_ * (from).Real - \ - (to).Imag * (from).Imag; \ - (to).Imag = to_real_ * (from).Imag + \ - (to).Imag * (from).Real; \ -} +#define CMPLX_MULT_ASSIGN(to, from) \ + { \ + RealNumber to_real_ = (to).Real; \ + (to).Real = to_real_ * (from).Real - (to).Imag * (from).Imag; \ + (to).Imag = to_real_ * (from).Imag + (to).Imag * (from).Real; \ + } /* Macro function that multiplies two complex numbers, the first of which is * conjugated. */ -#define CMPLX_CONJ_MULT(to,from_a,from_b) \ -{ (to).Real = (from_a).Real * (from_b).Real + \ - (from_a).Imag * (from_b).Imag; \ - (to).Imag = (from_a).Real * (from_b).Imag - \ - (from_a).Imag * (from_b).Real; \ -} +#define CMPLX_CONJ_MULT(to, from_a, from_b) \ + { \ + (to).Real = \ + (from_a).Real * (from_b).Real + (from_a).Imag * (from_b).Imag; \ + (to).Imag = \ + (from_a).Real * (from_b).Imag - (from_a).Imag * (from_b).Real; \ + } /* Macro function that multiplies two complex numbers and then adds them * to another. to = add + mult_a * mult_b */ -#define CMPLX_MULT_ADD(to,mult_a,mult_b,add) \ -{ (to).Real = (mult_a).Real * (mult_b).Real - \ - (mult_a).Imag * (mult_b).Imag + (add).Real; \ - (to).Imag = (mult_a).Real * (mult_b).Imag + \ - (mult_a).Imag * (mult_b).Real + (add).Imag; \ -} +#define CMPLX_MULT_ADD(to, mult_a, mult_b, add) \ + { \ + (to).Real = (mult_a).Real * (mult_b).Real - \ + (mult_a).Imag * (mult_b).Imag + (add).Real; \ + (to).Imag = (mult_a).Real * (mult_b).Imag + \ + (mult_a).Imag * (mult_b).Real + (add).Imag; \ + } /* Macro function that subtracts the product of two complex numbers from * another. to = subt - mult_a * mult_b */ -#define CMPLX_MULT_SUBT(to,mult_a,mult_b,subt) \ -{ (to).Real = (subt).Real - (mult_a).Real * (mult_b).Real + \ - (mult_a).Imag * (mult_b).Imag; \ - (to).Imag = (subt).Imag - (mult_a).Real * (mult_b).Imag - \ - (mult_a).Imag * (mult_b).Real; \ -} +#define CMPLX_MULT_SUBT(to, mult_a, mult_b, subt) \ + { \ + (to).Real = (subt).Real - (mult_a).Real * (mult_b).Real + \ + (mult_a).Imag * (mult_b).Imag; \ + (to).Imag = (subt).Imag - (mult_a).Real * (mult_b).Imag - \ + (mult_a).Imag * (mult_b).Real; \ + } /* Macro function that multiplies two complex numbers and then adds them * to another. to = add + mult_a* * mult_b where mult_a* represents mult_a * conjugate. */ -#define CMPLX_CONJ_MULT_ADD(to,mult_a,mult_b,add) \ -{ (to).Real = (mult_a).Real * (mult_b).Real + \ - (mult_a).Imag * (mult_b).Imag + (add).Real; \ - (to).Imag = (mult_a).Real * (mult_b).Imag - \ - (mult_a).Imag * (mult_b).Real + (add).Imag; \ -} +#define CMPLX_CONJ_MULT_ADD(to, mult_a, mult_b, add) \ + { \ + (to).Real = (mult_a).Real * (mult_b).Real + \ + (mult_a).Imag * (mult_b).Imag + (add).Real; \ + (to).Imag = (mult_a).Real * (mult_b).Imag - \ + (mult_a).Imag * (mult_b).Real + (add).Imag; \ + } /* Macro function that multiplies two complex numbers and then adds them * to another. to += mult_a * mult_b */ -#define CMPLX_MULT_ADD_ASSIGN(to,from_a,from_b) \ -{ (to).Real += (from_a).Real * (from_b).Real - \ - (from_a).Imag * (from_b).Imag; \ - (to).Imag += (from_a).Real * (from_b).Imag + \ - (from_a).Imag * (from_b).Real; \ -} +#define CMPLX_MULT_ADD_ASSIGN(to, from_a, from_b) \ + { \ + (to).Real += \ + (from_a).Real * (from_b).Real - (from_a).Imag * (from_b).Imag; \ + (to).Imag += \ + (from_a).Real * (from_b).Imag + (from_a).Imag * (from_b).Real; \ + } /* Macro function that multiplies two complex numbers and then subtracts them * from another. */ -#define CMPLX_MULT_SUBT_ASSIGN(to,from_a,from_b) \ -{ (to).Real -= (from_a).Real * (from_b).Real - \ - (from_a).Imag * (from_b).Imag; \ - (to).Imag -= (from_a).Real * (from_b).Imag + \ - (from_a).Imag * (from_b).Real; \ -} +#define CMPLX_MULT_SUBT_ASSIGN(to, from_a, from_b) \ + { \ + (to).Real -= \ + (from_a).Real * (from_b).Real - (from_a).Imag * (from_b).Imag; \ + (to).Imag -= \ + (from_a).Real * (from_b).Imag + (from_a).Imag * (from_b).Real; \ + } /* Macro function that multiplies two complex numbers and then adds them * to the destination. to += from_a* * from_b where from_a* represents from_a * conjugate. */ -#define CMPLX_CONJ_MULT_ADD_ASSIGN(to,from_a,from_b) \ -{ (to).Real += (from_a).Real * (from_b).Real + \ - (from_a).Imag * (from_b).Imag; \ - (to).Imag += (from_a).Real * (from_b).Imag - \ - (from_a).Imag * (from_b).Real; \ -} +#define CMPLX_CONJ_MULT_ADD_ASSIGN(to, from_a, from_b) \ + { \ + (to).Real += \ + (from_a).Real * (from_b).Real + (from_a).Imag * (from_b).Imag; \ + (to).Imag += \ + (from_a).Real * (from_b).Imag - (from_a).Imag * (from_b).Real; \ + } /* Macro function that multiplies two complex numbers and then subtracts them * from the destination. to -= from_a* * from_b where from_a* represents from_a * conjugate. */ -#define CMPLX_CONJ_MULT_SUBT_ASSIGN(to,from_a,from_b) \ -{ (to).Real -= (from_a).Real * (from_b).Real + \ - (from_a).Imag * (from_b).Imag; \ - (to).Imag -= (from_a).Real * (from_b).Imag - \ - (from_a).Imag * (from_b).Real; \ -} +#define CMPLX_CONJ_MULT_SUBT_ASSIGN(to, from_a, from_b) \ + { \ + (to).Real -= \ + (from_a).Real * (from_b).Real + (from_a).Imag * (from_b).Imag; \ + (to).Imag -= \ + (from_a).Real * (from_b).Imag - (from_a).Imag * (from_b).Real; \ + } /* * Macro functions that provide complex division. */ /* Complex division: to = num / den */ -#define CMPLX_DIV(to,num,den) \ -{ RealNumber r_, s_; \ - if (((den).Real >= (den).Imag AND (den).Real > -(den).Imag) OR \ - ((den).Real < (den).Imag AND (den).Real <= -(den).Imag)) \ - { r_ = (den).Imag / (den).Real; \ - s_ = (den).Real + r_*(den).Imag; \ - (to).Real = ((num).Real + r_*(num).Imag)/s_; \ - (to).Imag = ((num).Imag - r_*(num).Real)/s_; \ - } \ - else \ - { r_ = (den).Real / (den).Imag; \ - s_ = (den).Imag + r_*(den).Real; \ - (to).Real = (r_*(num).Real + (num).Imag)/s_; \ - (to).Imag = (r_*(num).Imag - (num).Real)/s_; \ - } \ -} +#define CMPLX_DIV(to, num, den) \ + { \ + RealNumber r_, s_; \ + if (((den).Real >= (den).Imag AND(den).Real > -(den).Imag) \ + OR((den).Real < (den).Imag AND(den).Real <= -(den).Imag)) { \ + r_ = (den).Imag / (den).Real; \ + s_ = (den).Real + r_ * (den).Imag; \ + (to).Real = ((num).Real + r_ * (num).Imag) / s_; \ + (to).Imag = ((num).Imag - r_ * (num).Real) / s_; \ + } else { \ + r_ = (den).Real / (den).Imag; \ + s_ = (den).Imag + r_ * (den).Real; \ + (to).Real = (r_ * (num).Real + (num).Imag) / s_; \ + (to).Imag = (r_ * (num).Imag - (num).Real) / s_; \ + } \ + } /* Complex division and assignment: num /= den */ -#define CMPLX_DIV_ASSIGN(num,den) \ -{ RealNumber r_, s_, t_; \ - if (((den).Real >= (den).Imag AND (den).Real > -(den).Imag) OR \ - ((den).Real < (den).Imag AND (den).Real <= -(den).Imag)) \ - { r_ = (den).Imag / (den).Real; \ - s_ = (den).Real + r_*(den).Imag; \ - t_ = ((num).Real + r_*(num).Imag)/s_; \ - (num).Imag = ((num).Imag - r_*(num).Real)/s_; \ - (num).Real = t_; \ - } \ - else \ - { r_ = (den).Real / (den).Imag; \ - s_ = (den).Imag + r_*(den).Real; \ - t_ = (r_*(num).Real + (num).Imag)/s_; \ - (num).Imag = (r_*(num).Imag - (num).Real)/s_; \ - (num).Real = t_; \ - } \ -} +#define CMPLX_DIV_ASSIGN(num, den) \ + { \ + RealNumber r_, s_, t_; \ + if (((den).Real >= (den).Imag AND(den).Real > -(den).Imag) \ + OR((den).Real < (den).Imag AND(den).Real <= -(den).Imag)) { \ + r_ = (den).Imag / (den).Real; \ + s_ = (den).Real + r_ * (den).Imag; \ + t_ = ((num).Real + r_ * (num).Imag) / s_; \ + (num).Imag = ((num).Imag - r_ * (num).Real) / s_; \ + (num).Real = t_; \ + } else { \ + r_ = (den).Real / (den).Imag; \ + s_ = (den).Imag + r_ * (den).Real; \ + t_ = (r_ * (num).Real + (num).Imag) / s_; \ + (num).Imag = (r_ * (num).Imag - (num).Real) / s_; \ + (num).Real = t_; \ + } \ + } /* Complex reciprocation: to = 1.0 / den */ -#define CMPLX_RECIPROCAL(to,den) \ -{ RealNumber r_; \ - if (((den).Real >= (den).Imag AND (den).Real > -(den).Imag) OR \ - ((den).Real < (den).Imag AND (den).Real <= -(den).Imag)) \ - { r_ = (den).Imag / (den).Real; \ - (to).Imag = -r_*((to).Real = 1.0/((den).Real + r_*(den).Imag)); \ - } \ - else \ - { r_ = (den).Real / (den).Imag; \ - (to).Real = -r_*((to).Imag = -1.0/((den).Imag + r_*(den).Real));\ - } \ -} - - +#define CMPLX_RECIPROCAL(to, den) \ + { \ + RealNumber r_; \ + if (((den).Real >= (den).Imag AND(den).Real > -(den).Imag) \ + OR((den).Real < (den).Imag AND(den).Real <= -(den).Imag)) { \ + r_ = (den).Imag / (den).Real; \ + (to).Imag = \ + -r_ * ((to).Real = 1.0 / ((den).Real + r_ * (den).Imag)); \ + } else { \ + r_ = (den).Real / (den).Imag; \ + (to).Real = \ + -r_ * ((to).Imag = -1.0 / ((den).Imag + r_ * (den).Real)); \ + } \ + } #endif - - /* * ASSERT and ABORT * @@ -379,52 +388,59 @@ */ #if DEBUG -#define ASSERT(condition) \ -{ if (NOT(condition)) \ - { (void)fflush(stdout); \ - (void)fprintf(stderr, "sparse: internal error detected in file `%s' at line %d.\n assertion `%s' failed.\n",\ - __FILE__, __LINE__, spcQUOTE(condition) ); \ - (void)fflush(stderr); \ - abort(); \ - } \ -} +#define ASSERT(condition) \ + { \ + if (NOT(condition)) { \ + (void)fflush(stdout); \ + (void)fprintf(stderr, \ + "sparse: internal error detected in file `%s' at " \ + "line %d.\n assertion `%s' failed.\n", \ + __FILE__, __LINE__, spcQUOTE(condition)); \ + (void)fflush(stderr); \ + abort(); \ + } \ + } #else #define ASSERT(condition) #endif #if DEBUG -#define vASSERT(condition,message) \ -{ if (NOT(condition)) \ - vABORT(message); \ -} +#define vASSERT(condition, message) \ + { \ + if (NOT(condition)) \ + vABORT(message); \ + } #else -#define vASSERT(condition,message) +#define vASSERT(condition, message) #endif #if DEBUG -#define vABORT(message) \ -{ (void)fflush(stdout); \ - (void)fprintf(stderr, "sparse: internal error detected in file `%s' at line %d.\n %s.\n", __FILE__, __LINE__, message );\ - (void)fflush(stderr); \ - abort(); \ -} +#define vABORT(message) \ + { \ + (void)fflush(stdout); \ + (void)fprintf(stderr, \ + "sparse: internal error detected in file `%s' at line " \ + "%d.\n %s.\n", \ + __FILE__, __LINE__, message); \ + (void)fflush(stderr); \ + abort(); \ + } -#define ABORT() \ -{ (void)fflush(stdout); \ - (void)fprintf(stderr, "sparse: internal error detected in file `%s' at line %d.\n", __FILE__, __LINE__ ); \ - (void)fflush(stderr); \ - abort(); \ -} +#define ABORT() \ + { \ + (void)fflush(stdout); \ + (void)fprintf( \ + stderr, \ + "sparse: internal error detected in file `%s' at line %d.\n", \ + __FILE__, __LINE__); \ + (void)fflush(stderr); \ + abort(); \ + } #else -#define vABORT(message) abort() -#define ABORT() abort() +#define vABORT(message) abort() +#define ABORT() abort() #endif - - - - - /* * IMAGINARY VECTORS * @@ -434,10 +450,10 @@ */ #if spCOMPLEX AND spSEPARATED_COMPLEX_VECTORS -#define IMAG_VECTORS , iRHS, iSolution -#define IMAG_RHS , iRHS -#define IMAG_RHS_DECL , RealVector iRHS -#define IMAG_VECT_DECL , RealVector iRHS, RealVector iSolution +#define IMAG_VECTORS , iRHS, iSolution +#define IMAG_RHS , iRHS +#define IMAG_RHS_DECL , RealVector iRHS +#define IMAG_VECT_DECL , RealVector iRHS, RealVector iSolution #else #define IMAG_VECTORS #define IMAG_RHS @@ -445,11 +461,6 @@ #define IMAG_VECT_DECL #endif - - - - - /* * MEMORY ALLOCATION */ @@ -459,34 +470,32 @@ spcEXTERN void *trealloc(const void *, size_t); spcEXTERN void txfree(const void *); spcEXTERN void abort(void); -#define SP_MALLOC(type,number) (type *) tmalloc((size_t)(number) * sizeof(type)) -#define SP_REALLOC(ptr,type,number) \ - ptr = (type *) trealloc(ptr, (size_t)(number) * sizeof(type)) -#define SP_FREE(ptr) { if ((ptr) != NULL) txfree(ptr); (ptr) = NULL; } +#define SP_MALLOC(type, number) (type *)tmalloc((size_t)(number) * sizeof(type)) +#define SP_REALLOC(ptr, type, number) \ + ptr = (type *)trealloc(ptr, (size_t)(number) * sizeof(type)) +#define SP_FREE(ptr) \ + { \ + if ((ptr) != NULL) \ + txfree(ptr); \ + (ptr) = NULL; \ + } #include "ngspice/config.h" /* A new calloc */ #ifndef HAVE_LIBGC -#define SP_CALLOC(ptr,type,number) \ -{ ptr = (type *) calloc((size_t)(number), sizeof(type)); \ -} +#define SP_CALLOC(ptr, type, number) \ + { ptr = (type *)calloc((size_t)(number), sizeof(type)); } #else /* HAVE_LIBCG */ -#define SP_CALLOC(ptr,type,number) \ -{ ptr = (type *) tmalloc((size_t)(number) * sizeof(type)); \ -} +#define SP_CALLOC(ptr, type, number) \ + { ptr = (type *)tmalloc((size_t)(number) * sizeof(type)); } #include -#define tmalloc(m) GC_malloc(m) -#define trealloc(m, n) GC_realloc((m), (n)) +#define tmalloc(m) GC_malloc(m) +#define trealloc(m, n) GC_realloc((m), (n)) #define tfree(m) #define txfree(m) #endif - - - - - /* * Utility Functions */ @@ -495,37 +504,25 @@ spcEXTERN void abort(void); * Used when computing Markowitz products. */ -#define spcMarkoProd(product, op1, op2) \ - if (( (op1) > LARGEST_SHORT_INTEGER AND (op2) != 0) OR \ - ( (op2) > LARGEST_SHORT_INTEGER AND (op1) != 0)) \ - { double fProduct = (double)(op1) * (double)(op2); \ - if (fProduct >= LARGEST_LONG_INTEGER) \ - (product) = LARGEST_LONG_INTEGER; \ - else \ - (product) = (long)fProduct; \ - } \ - else (product) = (op1)*(op2); +#define spcMarkoProd(product, op1, op2) \ + if (((op1) > LARGEST_SHORT_INTEGER AND(op2) != 0) \ + OR((op2) > LARGEST_SHORT_INTEGER AND(op1) != 0)) { \ + double fProduct = (double)(op1) * (double)(op2); \ + if (fProduct >= LARGEST_LONG_INTEGER) \ + (product) = LARGEST_LONG_INTEGER; \ + else \ + (product) = (long)fProduct; \ + } else \ + (product) = (op1) * (op2); - - - - - /* * REAL NUMBER */ /* Begin `RealNumber'. */ -typedef spREAL RealNumber, *RealVector; +typedef spREAL RealNumber, *RealVector; - - - - - - - /* * COMPLEX NUMBER DATA STRUCTURE * @@ -540,18 +537,11 @@ typedef spREAL RealNumber, *RealVector; /* Begin `ComplexNumber'. */ -typedef struct -{ RealNumber Real; - RealNumber Imag; +typedef struct { + RealNumber Real; + RealNumber Imag; } ComplexNumber, *ComplexVector; - - - - - - - /* * MATRIX ELEMENT DATA STRUCTURE * @@ -597,30 +587,23 @@ typedef struct /* Begin `MatrixElement'. */ -struct MatrixElement -{ RealNumber Real; +struct MatrixElement { + RealNumber Real; #if spCOMPLEX - RealNumber Imag; + RealNumber Imag; #endif - int Row; - int Col; - struct MatrixElement *NextInRow; - struct MatrixElement *NextInCol; + int Row; + int Col; + struct MatrixElement *NextInRow; + struct MatrixElement *NextInCol; #if INITIALIZE spGenericPtr pInitInfo; #endif }; -typedef struct MatrixElement *ElementPtr; -typedef ElementPtr *ArrayOfElementPtrs; +typedef struct MatrixElement *ElementPtr; +typedef ElementPtr *ArrayOfElementPtrs; - - - - - - - /* * ALLOCATION DATA STRUCTURE * @@ -638,21 +621,13 @@ typedef ElementPtr *ArrayOfElementPtrs; */ /* Begin `AllocationRecord'. */ -struct AllocationRecord -{ void *AllocatedPtr; - struct AllocationRecord *NextRecord; +struct AllocationRecord { + void *AllocatedPtr; + struct AllocationRecord *NextRecord; }; -typedef struct AllocationRecord *AllocationListPtr; +typedef struct AllocationRecord *AllocationListPtr; - - - - - - - - /* * FILL-IN LIST DATA STRUCTURE * @@ -673,21 +648,12 @@ typedef struct AllocationRecord *AllocationListPtr; */ /* Begin `FillinListNodeStruct'. */ -struct FillinListNodeStruct -{ ElementPtr pFillinList; - int NumberOfFillinsInList; - struct FillinListNodeStruct *Next; +struct FillinListNodeStruct { + ElementPtr pFillinList; + int NumberOfFillinsInList; + struct FillinListNodeStruct *Next; }; - - - - - - - - - /* * MATRIX FRAME DATA STRUCTURE * @@ -877,79 +843,77 @@ struct FillinListNodeStruct */ /* Begin `MatrixFrame'. */ -struct MatrixFrame -{ RealNumber AbsThreshold; - int AllocatedSize; - int AllocatedExtSize; - BOOLEAN Complex; - int CurrentSize; - ArrayOfElementPtrs Diag; - BOOLEAN *DoCmplxDirect; - BOOLEAN *DoRealDirect; - int Elements; - int Error; - int ExtSize; - int *ExtToIntColMap; - int *ExtToIntRowMap; - BOOLEAN Factored; - int Fillins; - ArrayOfElementPtrs FirstInCol; - ArrayOfElementPtrs FirstInRow; - unsigned long ID; - RealVector Intermediate; - BOOLEAN InternalVectorsAllocated; - int *IntToExtColMap; - int *IntToExtRowMap; - int *MarkowitzRow; - int *MarkowitzCol; - long *MarkowitzProd; - int MaxRowCountInLowerTri; - BOOLEAN NeedsOrdering; - BOOLEAN NumberOfInterchangesIsOdd; - BOOLEAN Partitioned; - int PivotsOriginalCol; - int PivotsOriginalRow; - char PivotSelectionMethod; - BOOLEAN PreviousMatrixWasComplex; - RealNumber RelThreshold; - BOOLEAN Reordered; - BOOLEAN RowsLinked; - int SingularCol; - int SingularRow; - int Singletons; - int Size; - struct MatrixElement TrashCan; +struct MatrixFrame { + RealNumber AbsThreshold; + int AllocatedSize; + int AllocatedExtSize; + BOOLEAN Complex; + int CurrentSize; + ArrayOfElementPtrs Diag; + BOOLEAN *DoCmplxDirect; + BOOLEAN *DoRealDirect; + int Elements; + int Error; + int ExtSize; + int *ExtToIntColMap; + int *ExtToIntRowMap; + BOOLEAN Factored; + int Fillins; + ArrayOfElementPtrs FirstInCol; + ArrayOfElementPtrs FirstInRow; + unsigned long ID; + RealVector Intermediate; + BOOLEAN InternalVectorsAllocated; + int *IntToExtColMap; + int *IntToExtRowMap; + int *MarkowitzRow; + int *MarkowitzCol; + long *MarkowitzProd; + int MaxRowCountInLowerTri; + BOOLEAN NeedsOrdering; + BOOLEAN NumberOfInterchangesIsOdd; + BOOLEAN Partitioned; + int PivotsOriginalCol; + int PivotsOriginalRow; + char PivotSelectionMethod; + BOOLEAN PreviousMatrixWasComplex; + RealNumber RelThreshold; + BOOLEAN Reordered; + BOOLEAN RowsLinked; + int SingularCol; + int SingularRow; + int Singletons; + int Size; + struct MatrixElement TrashCan; - AllocationListPtr TopOfAllocationList; - int RecordsRemaining; - ElementPtr NextAvailElement; - int ElementsRemaining; - ElementPtr NextAvailFillin; - int FillinsRemaining; + AllocationListPtr TopOfAllocationList; + int RecordsRemaining; + ElementPtr NextAvailElement; + int ElementsRemaining; + ElementPtr NextAvailFillin; + int FillinsRemaining; struct FillinListNodeStruct *FirstFillinListNode; struct FillinListNodeStruct *LastFillinListNode; }; -typedef struct MatrixFrame *MatrixPtr; - - - +typedef struct MatrixFrame *MatrixPtr; /* * Declarations */ -spcEXTERN ElementPtr spcGetElement( MatrixPtr ); -spcEXTERN ElementPtr spcGetFillin( MatrixPtr ); -spcEXTERN ElementPtr spcFindElementInCol( MatrixPtr, ElementPtr*, int, int, int ); -spcEXTERN ElementPtr spcFindDiag( MatrixPtr, int ); -spcEXTERN ElementPtr spcCreateElement( MatrixPtr, int, int, - ElementPtr*, ElementPtr*, int ); -spcEXTERN ElementPtr spcCreateElementOld( MatrixPtr, int, int, - ElementPtr*, int ); -spcEXTERN void spcCreateInternalVectors( MatrixPtr ); -spcEXTERN void spcLinkRows( MatrixPtr ); -spcEXTERN void spcColExchange( MatrixPtr, int, int ); -spcEXTERN void spcRowExchange( MatrixPtr, int, int ); +spcEXTERN ElementPtr spcGetElement(MatrixPtr); +spcEXTERN ElementPtr spcGetFillin(MatrixPtr); +spcEXTERN ElementPtr spcFindElementInCol(MatrixPtr, ElementPtr *, int, int, + int); +spcEXTERN ElementPtr spcFindDiag(MatrixPtr, int); +spcEXTERN ElementPtr spcCreateElement(MatrixPtr, int, int, ElementPtr *, + ElementPtr *, int); +spcEXTERN ElementPtr spcCreateElementOld(MatrixPtr, int, int, ElementPtr *, + int); +spcEXTERN void spcCreateInternalVectors(MatrixPtr); +spcEXTERN void spcLinkRows(MatrixPtr); +spcEXTERN void spcColExchange(MatrixPtr, int, int); +spcEXTERN void spcRowExchange(MatrixPtr, int, int); spcEXTERN char spcMatrixIsNotValid[]; spcEXTERN char spcErrorsMustBeCleared[]; diff --git a/src/maths/sparse/spExtra.c b/src/maths/sparse/spExtra.c index de32918b4..b9c25cb17 100644 --- a/src/maths/sparse/spExtra.c +++ b/src/maths/sparse/spExtra.c @@ -9,7 +9,6 @@ * >>> Other functions contained in this file: */ - /* * IMPORTS * @@ -23,25 +22,21 @@ */ #define spINSIDE_SPARSE -#include -#include "spConfig.h" #include "ngspice/spmatrix.h" +#include "spConfig.h" #include "spDefs.h" +#include -void -spConstMult( - MatrixPtr Matrix, - double constant -) -{ -ElementPtr pElement; -int I; -int size = Matrix->Size; +void spConstMult(MatrixPtr Matrix, double constant) { + ElementPtr pElement; + int I; + int size = Matrix->Size; - ASSERT_IS_SPARSE( Matrix ); + ASSERT_IS_SPARSE(Matrix); for (I = 1; I <= size; I++) { - for (pElement = Matrix->FirstInCol[I]; pElement; pElement = pElement->NextInCol) { + for (pElement = Matrix->FirstInCol[I]; pElement; + pElement = pElement->NextInCol) { pElement->Real *= constant; #if spCOMPLEX pElement->Imag *= constant; diff --git a/src/maths/sparse/spFactor.c b/src/maths/sparse/spFactor.c index 028dd11ee..9528e60f7 100644 --- a/src/maths/sparse/spFactor.c +++ b/src/maths/sparse/spFactor.c @@ -33,7 +33,6 @@ * ZeroPivot WriteStatus */ - /* * Revision and copyright information. * @@ -44,12 +43,10 @@ #ifdef notdef static char copyright[] = "Sparse1.4: Copyright (c) 1985-2003 by Kenneth S. Kundert"; -static char RCSid[] = - "@(#)$Header: /cvsroot/sparse/src/spFactor.c,v 1.3 2003/06/29 04:19:52 kundert Exp $"; +static char RCSid[] = "@(#)$Header: /cvsroot/sparse/src/spFactor.c,v 1.3 " + "2003/06/29 04:19:52 kundert Exp $"; #endif - - /* * IMPORTS * @@ -63,51 +60,44 @@ static char RCSid[] = */ #define spINSIDE_SPARSE -#include -#include "spConfig.h" #include "ngspice/spmatrix.h" +#include "spConfig.h" #include "spDefs.h" - - - - +#include /* * Function declarations */ #if spCOMPLEX -static int FactorComplexMatrix( MatrixPtr ); +static int FactorComplexMatrix(MatrixPtr); #endif -//static void CreateInternalVectors( MatrixPtr ); -static void CountMarkowitz( MatrixPtr, RealVector, int ); -static void MarkowitzProducts( MatrixPtr, int ); -static ElementPtr SearchForPivot( MatrixPtr, int, int ); -static ElementPtr SearchForSingleton( MatrixPtr, int ); +// static void CreateInternalVectors( MatrixPtr ); +static void CountMarkowitz(MatrixPtr, RealVector, int); +static void MarkowitzProducts(MatrixPtr, int); +static ElementPtr SearchForPivot(MatrixPtr, int, int); +static ElementPtr SearchForSingleton(MatrixPtr, int); #if DIAGONAL_PIVOTING -static ElementPtr QuicklySearchDiagonal( MatrixPtr, int ); -static ElementPtr SearchDiagonal( MatrixPtr, int ); +static ElementPtr QuicklySearchDiagonal(MatrixPtr, int); +static ElementPtr SearchDiagonal(MatrixPtr, int); #endif -static ElementPtr SearchEntireMatrix( MatrixPtr, int ); -static RealNumber FindLargestInCol( ElementPtr ); -static RealNumber FindBiggestInColExclude( MatrixPtr, ElementPtr, int ); -static void ExchangeRowsAndCols( MatrixPtr, ElementPtr, int ); -static void ExchangeColElements( MatrixPtr, int, ElementPtr, int, - ElementPtr, int ); -static void ExchangeRowElements( MatrixPtr, int, ElementPtr, int, - ElementPtr, int ); -static void RealRowColElimination( MatrixPtr, ElementPtr ); -static void ComplexRowColElimination( MatrixPtr, ElementPtr ); -static void UpdateMarkowitzNumbers( MatrixPtr, ElementPtr ); -static int MatrixIsSingular( MatrixPtr, int ); -static int ZeroPivot( MatrixPtr, int ); +static ElementPtr SearchEntireMatrix(MatrixPtr, int); +static RealNumber FindLargestInCol(ElementPtr); +static RealNumber FindBiggestInColExclude(MatrixPtr, ElementPtr, int); +static void ExchangeRowsAndCols(MatrixPtr, ElementPtr, int); +static void ExchangeColElements(MatrixPtr, int, ElementPtr, int, ElementPtr, + int); +static void ExchangeRowElements(MatrixPtr, int, ElementPtr, int, ElementPtr, + int); +static void RealRowColElimination(MatrixPtr, ElementPtr); +static void ComplexRowColElimination(MatrixPtr, ElementPtr); +static void UpdateMarkowitzNumbers(MatrixPtr, ElementPtr); +static int MatrixIsSingular(MatrixPtr, int); +static int ZeroPivot(MatrixPtr, int); #if (ANNOTATE == FULL) -static void WriteStatus( MatrixPtr, int ); +static void WriteStatus(MatrixPtr, int); #endif - - - /*! * This routine chooses a pivot order for the matrix and factors it * into \a LU form. It handles both the initial factorization and subsequent @@ -196,107 +186,104 @@ static void WriteStatus( MatrixPtr, int ); * */ -int -spOrderAndFactor( - MatrixPtr Matrix, - spREAL RHS[], - spREAL RelThreshold, - spREAL AbsThreshold, - int DiagPivoting -) -{ -ElementPtr pPivot; -int Step, Size; -ElementPtr SearchForPivot(); -RealNumber LargestInCol, FindLargestInCol(); +int spOrderAndFactor(MatrixPtr Matrix, spREAL RHS[], spREAL RelThreshold, + spREAL AbsThreshold, int DiagPivoting) { + ElementPtr pPivot; + int Step, Size; + ElementPtr SearchForPivot(); + RealNumber LargestInCol, FindLargestInCol(); -/* Begin `spOrderAndFactor'. */ - ASSERT_IS_SPARSE( Matrix ); - ASSERT_NO_ERRORS( Matrix ); - ASSERT_IS_NOT_FACTORED( Matrix ); + /* Begin `spOrderAndFactor'. */ + ASSERT_IS_SPARSE(Matrix); + ASSERT_NO_ERRORS(Matrix); + ASSERT_IS_NOT_FACTORED(Matrix); Matrix->Error = spOKAY; Size = Matrix->Size; - if (RelThreshold <= 0.0) RelThreshold = Matrix->RelThreshold; - if (RelThreshold > 1.0) RelThreshold = Matrix->RelThreshold; + if (RelThreshold <= 0.0) + RelThreshold = Matrix->RelThreshold; + if (RelThreshold > 1.0) + RelThreshold = Matrix->RelThreshold; Matrix->RelThreshold = RelThreshold; - if (AbsThreshold < 0.0) AbsThreshold = Matrix->AbsThreshold; + if (AbsThreshold < 0.0) + AbsThreshold = Matrix->AbsThreshold; Matrix->AbsThreshold = AbsThreshold; - if (NOT Matrix->NeedsOrdering) - { -/* Matrix has been factored before and reordering is not required. */ - for (Step = 1; Step <= Size; Step++) - { pPivot = Matrix->Diag[Step]; + if (NOT Matrix->NeedsOrdering) { + /* Matrix has been factored before and reordering is not required. */ + for (Step = 1; Step <= Size; Step++) { + pPivot = Matrix->Diag[Step]; if (!pPivot) { - fprintf(stderr, "Warning: spfactor.c, 230, Pivot for step = %d not found\n", Step); + fprintf( + stderr, + "Warning: spfactor.c, 230, Pivot for step = %d not found\n", + Step); Matrix->NeedsOrdering = YES; break; /* for loop */ } LargestInCol = FindLargestInCol(pPivot->NextInCol); - if ((LargestInCol * RelThreshold < ELEMENT_MAG(pPivot))) - { if (Matrix->Complex) - ComplexRowColElimination( Matrix, pPivot ); + if ((LargestInCol * RelThreshold < ELEMENT_MAG(pPivot))) { + if (Matrix->Complex) + ComplexRowColElimination(Matrix, pPivot); else - RealRowColElimination( Matrix, pPivot ); - } - else - { Matrix->NeedsOrdering = YES; + RealRowColElimination(Matrix, pPivot); + } else { + Matrix->NeedsOrdering = YES; break; /* for loop */ } } if (NOT Matrix->NeedsOrdering) goto Done; - else - { -/* - * A pivot was not large enough to maintain accuracy, - * so a partial reordering is required. - */ + else { + /* + * A pivot was not large enough to maintain accuracy, + * so a partial reordering is required. + */ #if (ANNOTATE >= ON_STRANGE_BEHAVIOR) printf("Reordering, Step = %1d\n", Step); #endif } } /* End of if(NOT Matrix->NeedsOrdering) */ - else - { -/* - * This is the first time the matrix has been factored. These few statements - * indicate to the rest of the code that a full reodering is required rather - * than a partial reordering, which occurs during a failure of a fast - * factorization. - */ + else { + /* + * This is the first time the matrix has been factored. These few + * statements indicate to the rest of the code that a full reodering is + * required rather than a partial reordering, which occurs during a + * failure of a fast factorization. + */ Step = 1; if (NOT Matrix->RowsLinked) - spcLinkRows( Matrix ); + spcLinkRows(Matrix); if (NOT Matrix->InternalVectorsAllocated) - spcCreateInternalVectors( Matrix ); + spcCreateInternalVectors(Matrix); if (Matrix->Error >= spFATAL) return Matrix->Error; } -/* Form initial Markowitz products. */ - CountMarkowitz( Matrix, RHS, Step ); - MarkowitzProducts( Matrix, Step ); + /* Form initial Markowitz products. */ + CountMarkowitz(Matrix, RHS, Step); + MarkowitzProducts(Matrix, Step); Matrix->MaxRowCountInLowerTri = -1; -/* Perform reordering and factorization. */ - for (; Step <= Size; Step++) - { pPivot = SearchForPivot( Matrix, Step, DiagPivoting ); - if (pPivot == NULL) return MatrixIsSingular( Matrix, Step ); - ExchangeRowsAndCols( Matrix, pPivot, Step ); + /* Perform reordering and factorization. */ + for (; Step <= Size; Step++) { + pPivot = SearchForPivot(Matrix, Step, DiagPivoting); + if (pPivot == NULL) + return MatrixIsSingular(Matrix, Step); + ExchangeRowsAndCols(Matrix, pPivot, Step); if (Matrix->Complex) - ComplexRowColElimination( Matrix, pPivot ); + ComplexRowColElimination(Matrix, pPivot); else - RealRowColElimination( Matrix, pPivot ); + RealRowColElimination(Matrix, pPivot); - if (Matrix->Error >= spFATAL) return Matrix->Error; - UpdateMarkowitzNumbers( Matrix, pPivot ); + if (Matrix->Error >= spFATAL) + return Matrix->Error; + UpdateMarkowitzNumbers(Matrix, pPivot); #if (ANNOTATE == FULL) - WriteStatus( Matrix, Step ); + WriteStatus(Matrix, Step); #endif } @@ -308,12 +295,6 @@ Done: return Matrix->Error; } - - - - - - /*! * This routine is the companion routine to spOrderAndFactor(). * Unlike spOrderAndFactor(), spFactor() cannot change the ordering. @@ -338,27 +319,27 @@ Done: * \see spOrderAndFactor() */ -int -spFactor( MatrixPtr Matrix ) -{ +int spFactor(MatrixPtr Matrix) { #if REAL -ElementPtr pElement; -ElementPtr pColumn; -int Step, Size; -RealNumber Mult; + ElementPtr pElement; + ElementPtr pColumn; + int Step, Size; + RealNumber Mult; #endif -/* Begin `spFactor'. */ - ASSERT_IS_SPARSE( Matrix ); - ASSERT_NO_ERRORS( Matrix ); - ASSERT_IS_NOT_FACTORED( Matrix ); + /* Begin `spFactor'. */ + ASSERT_IS_SPARSE(Matrix); + ASSERT_NO_ERRORS(Matrix); + ASSERT_IS_NOT_FACTORED(Matrix); - if (Matrix->NeedsOrdering) - { return spOrderAndFactor( Matrix, NULL, - 0.0, 0.0, DIAG_PIVOTING_AS_DEFAULT ); + if (Matrix->NeedsOrdering) { + return spOrderAndFactor(Matrix, NULL, 0.0, 0.0, + DIAG_PIVOTING_AS_DEFAULT); } - if (NOT Matrix->Partitioned) spPartition( Matrix, spDEFAULT_PARTITION ); + if (NOT Matrix->Partitioned) + spPartition(Matrix, spDEFAULT_PARTITION); #if spCOMPLEX - if (Matrix->Complex) return FactorComplexMatrix( Matrix ); + if (Matrix->Complex) + return FactorComplexMatrix(Matrix); #endif #if REAL @@ -369,67 +350,71 @@ RealNumber Mult; return (Matrix->Error = spOKAY); } - if (Matrix->Diag[1]->Real == 0.0) return ZeroPivot( Matrix, 1 ); + if (Matrix->Diag[1]->Real == 0.0) + return ZeroPivot(Matrix, 1); Matrix->Diag[1]->Real = 1.0 / Matrix->Diag[1]->Real; -/* Start factorization. */ - for (Step = 2; Step <= Size; Step++) - { if (Matrix->DoRealDirect[Step]) - { /* Update column using direct addressing scatter-gather. */ + /* Start factorization. */ + for (Step = 2; Step <= Size; Step++) { + if (Matrix->DoRealDirect[Step]) { /* Update column using direct + addressing scatter-gather. */ RealNumber *Dest = (RealNumber *)Matrix->Intermediate; -/* Scatter. */ + /* Scatter. */ pElement = Matrix->FirstInCol[Step]; - while (pElement != NULL) - { Dest[pElement->Row] = pElement->Real; + while (pElement != NULL) { + Dest[pElement->Row] = pElement->Real; pElement = pElement->NextInCol; } -/* Update column. */ + /* Update column. */ pColumn = Matrix->FirstInCol[Step]; - while (pColumn->Row < Step) /* FIXME: Access to Row results in dereference of NULL pointer */ - { pElement = Matrix->Diag[pColumn->Row]; + while (pColumn->Row < Step) /* FIXME: Access to Row results in + dereference of NULL pointer */ + { + pElement = Matrix->Diag[pColumn->Row]; pColumn->Real = Dest[pColumn->Row] * pElement->Real; while ((pElement = pElement->NextInCol) != NULL) Dest[pElement->Row] -= pColumn->Real * pElement->Real; pColumn = pColumn->NextInCol; } -/* Gather. */ + /* Gather. */ pElement = Matrix->Diag[Step]->NextInCol; - while (pElement != NULL) - { pElement->Real = Dest[pElement->Row]; + while (pElement != NULL) { + pElement->Real = Dest[pElement->Row]; pElement = pElement->NextInCol; } -/* Check for singular matrix. */ - if (Dest[Step] == 0.0) return ZeroPivot( Matrix, Step ); + /* Check for singular matrix. */ + if (Dest[Step] == 0.0) + return ZeroPivot(Matrix, Step); Matrix->Diag[Step]->Real = 1.0 / Dest[Step]; - } - else - { /* Update column using indirect addressing scatter-gather. */ + } else { /* Update column using indirect addressing scatter-gather. */ RealNumber **pDest = (RealNumber **)Matrix->Intermediate; -/* Scatter. */ + /* Scatter. */ pElement = Matrix->FirstInCol[Step]; - while (pElement != NULL) - { pDest[pElement->Row] = &pElement->Real; + while (pElement != NULL) { + pDest[pElement->Row] = &pElement->Real; pElement = pElement->NextInCol; } -/* Update column. */ + /* Update column. */ pColumn = Matrix->FirstInCol[Step]; - while (pColumn->Row < Step) /* FIXME: Access to Row results in dereference of NULL pointer */ - { pElement = Matrix->Diag[pColumn->Row]; + while (pColumn->Row < Step) /* FIXME: Access to Row results in + dereference of NULL pointer */ + { + pElement = Matrix->Diag[pColumn->Row]; Mult = (*pDest[pColumn->Row] *= pElement->Real); while ((pElement = pElement->NextInCol) != NULL) *pDest[pElement->Row] -= Mult * pElement->Real; pColumn = pColumn->NextInCol; } -/* Check for singular matrix. */ + /* Check for singular matrix. */ if (Matrix->Diag[Step]->Real == 0.0) - return ZeroPivot( Matrix, Step ); + return ZeroPivot(Matrix, Step); Matrix->Diag[Step]->Real = 1.0 / Matrix->Diag[Step]->Real; } } @@ -439,11 +424,6 @@ RealNumber Mult; #endif /* REAL */ } - - - - - #if spCOMPLEX /* * FACTOR COMPLEX MATRIX @@ -463,15 +443,13 @@ RealNumber Mult; * Error is cleared in this function. */ -static int -FactorComplexMatrix( MatrixPtr Matrix ) -{ -ElementPtr pElement; -ElementPtr pColumn; -int Step, Size; -ComplexNumber Mult, Pivot; +static int FactorComplexMatrix(MatrixPtr Matrix) { + ElementPtr pElement; + ElementPtr pColumn; + int Step, Size; + ComplexNumber Mult, Pivot; -/* Begin `FactorComplexMatrix'. */ + /* Begin `FactorComplexMatrix'. */ ASSERT(Matrix->Complex); Size = Matrix->Size; @@ -482,80 +460,89 @@ ComplexNumber Mult, Pivot; } pElement = Matrix->Diag[1]; - if (ELEMENT_MAG(pElement) == 0.0) return ZeroPivot( Matrix, 1 ); -/* Cmplx expr: *pPivot = 1.0 / *pPivot. */ - CMPLX_RECIPROCAL( *pElement, *pElement ); + if (ELEMENT_MAG(pElement) == 0.0) + return ZeroPivot(Matrix, 1); + /* Cmplx expr: *pPivot = 1.0 / *pPivot. */ + CMPLX_RECIPROCAL(*pElement, *pElement); -/* Start factorization. */ - for (Step = 2; Step <= Size; Step++) - { if (Matrix->DoCmplxDirect[Step]) - { /* Update column using direct addressing scatter-gather. */ - ComplexNumber *Dest; + /* Start factorization. */ + for (Step = 2; Step <= Size; Step++) { + if (Matrix->DoCmplxDirect[Step]) { /* Update column using direct + addressing scatter-gather. */ + ComplexNumber *Dest; Dest = (ComplexNumber *)Matrix->Intermediate; -/* Scatter. */ + /* Scatter. */ pElement = Matrix->FirstInCol[Step]; - while (pElement != NULL) - { Dest[pElement->Row] = *(ComplexNumber *)pElement; + while (pElement != NULL) { + Dest[pElement->Row] = *(ComplexNumber *)pElement; pElement = pElement->NextInCol; } -/* Update column. */ + /* Update column. */ pColumn = Matrix->FirstInCol[Step]; - while (pColumn->Row < Step) /* FIXME: Access to Row results in dereference of NULL pointer */ - { pElement = Matrix->Diag[pColumn->Row]; + while (pColumn->Row < Step) /* FIXME: Access to Row results in + dereference of NULL pointer */ + { + pElement = Matrix->Diag[pColumn->Row]; /* Cmplx expr: Mult = Dest[pColumn->Row] * (1.0 / *pPivot). */ CMPLX_MULT(Mult, Dest[pColumn->Row], *pElement); CMPLX_ASSIGN(*pColumn, Mult); - while ((pElement = pElement->NextInCol) != NULL) - { /* Cmplx expr: Dest[pElement->Row] -= Mult * pElement */ - CMPLX_MULT_SUBT_ASSIGN(Dest[pElement->Row],Mult,*pElement); + while ((pElement = pElement->NextInCol) != + NULL) { /* Cmplx expr: Dest[pElement->Row] -= Mult * + pElement */ + CMPLX_MULT_SUBT_ASSIGN(Dest[pElement->Row], Mult, + *pElement); } pColumn = pColumn->NextInCol; } -/* Gather. */ + /* Gather. */ pElement = Matrix->Diag[Step]->NextInCol; - while (pElement != NULL) - { *(ComplexNumber *)pElement = Dest[pElement->Row]; + while (pElement != NULL) { + *(ComplexNumber *)pElement = Dest[pElement->Row]; pElement = pElement->NextInCol; } -/* Check for singular matrix. */ + /* Check for singular matrix. */ Pivot = Dest[Step]; - if (CMPLX_1_NORM(Pivot) == 0.0) return ZeroPivot( Matrix, Step ); - CMPLX_RECIPROCAL( *Matrix->Diag[Step], Pivot ); - } - else - { /* Update column using direct addressing scatter-gather. */ - ComplexNumber **pDest; + if (CMPLX_1_NORM(Pivot) == 0.0) + return ZeroPivot(Matrix, Step); + CMPLX_RECIPROCAL(*Matrix->Diag[Step], Pivot); + } else { /* Update column using direct addressing scatter-gather. */ + ComplexNumber **pDest; pDest = (ComplexNumber **)Matrix->Intermediate; -/* Scatter. */ + /* Scatter. */ pElement = Matrix->FirstInCol[Step]; - while (pElement != NULL) - { pDest[pElement->Row] = (ComplexNumber *)pElement; + while (pElement != NULL) { + pDest[pElement->Row] = (ComplexNumber *)pElement; pElement = pElement->NextInCol; } -/* Update column. */ + /* Update column. */ pColumn = Matrix->FirstInCol[Step]; - while (pColumn->Row < Step) /* FIXME: Access to Row results in dereference of NULL pointer */ - { pElement = Matrix->Diag[pColumn->Row]; + while (pColumn->Row < Step) /* FIXME: Access to Row results in + dereference of NULL pointer */ + { + pElement = Matrix->Diag[pColumn->Row]; /* Cmplx expr: Mult = *pDest[pColumn->Row] * (1.0 / *pPivot). */ CMPLX_MULT(Mult, *pDest[pColumn->Row], *pElement); CMPLX_ASSIGN(*pDest[pColumn->Row], Mult); - while ((pElement = pElement->NextInCol) != NULL) - { /* Cmplx expr: *pDest[pElement->Row] -= Mult * pElement */ - CMPLX_MULT_SUBT_ASSIGN(*pDest[pElement->Row],Mult,*pElement); + while ((pElement = pElement->NextInCol) != + NULL) { /* Cmplx expr: *pDest[pElement->Row] -= Mult * + pElement */ + CMPLX_MULT_SUBT_ASSIGN(*pDest[pElement->Row], Mult, + *pElement); } pColumn = pColumn->NextInCol; } -/* Check for singular matrix. */ + /* Check for singular matrix. */ pElement = Matrix->Diag[Step]; - if (ELEMENT_MAG(pElement) == 0.0) return ZeroPivot( Matrix, Step ); - CMPLX_RECIPROCAL( *pElement, *pElement ); + if (ELEMENT_MAG(pElement) == 0.0) + return ZeroPivot(Matrix, Step); + CMPLX_RECIPROCAL(*pElement, *pElement); } } @@ -564,11 +551,6 @@ ComplexNumber Mult, Pivot; } #endif /* spCOMPLEX */ - - - - - /*! * This routine determines the cost to factor each row using both * direct and indirect addressing and decides, on a row-by-row basis, @@ -603,26 +585,22 @@ ComplexNumber Mult, Pivot; * \a spINDIRECT_PARTITION, or \a spAUTO_PARTITION. */ -void -spPartition( - MatrixPtr Matrix, - int Mode -) -{ -ElementPtr pElement, pColumn; -int Step, Size; -int *Nc, *No; -long *Nm; +void spPartition(MatrixPtr Matrix, int Mode) { + ElementPtr pElement, pColumn; + int Step, Size; + int *Nc, *No; + long *Nm; #if spCOMPLEX -BOOLEAN *DoCmplxDirect; + BOOLEAN *DoCmplxDirect; #endif #if REAL -BOOLEAN *DoRealDirect; + BOOLEAN *DoRealDirect; #endif -/* Begin `spPartition'. */ - ASSERT_IS_SPARSE( Matrix ); + /* Begin `spPartition'. */ + ASSERT_IS_SPARSE(Matrix); - if (Matrix->Partitioned) return; + if (Matrix->Partitioned) + return; Size = Matrix->Size; #if REAL DoRealDirect = Matrix->DoRealDirect; @@ -632,48 +610,51 @@ BOOLEAN *DoRealDirect; #endif Matrix->Partitioned = YES; -/* If partition is specified by the user, this is easy. */ - if (Mode == spDEFAULT_PARTITION) Mode = DEFAULT_PARTITION; - if (Mode == spDIRECT_PARTITION) - { for (Step = 1; Step <= Size; Step++) + /* If partition is specified by the user, this is easy. */ + if (Mode == spDEFAULT_PARTITION) + Mode = DEFAULT_PARTITION; + if (Mode == spDIRECT_PARTITION) { + for (Step = 1; Step <= Size; Step++) #if REAL DoRealDirect[Step] = YES; #endif #if spCOMPLEX - DoCmplxDirect[Step] = YES; + DoCmplxDirect[Step] = YES; #endif return; - } - else if (Mode == spINDIRECT_PARTITION) - { for (Step = 1; Step <= Size; Step++) + } else if (Mode == spINDIRECT_PARTITION) { + for (Step = 1; Step <= Size; Step++) #if REAL DoRealDirect[Step] = NO; #endif #if spCOMPLEX - DoCmplxDirect[Step] = NO; + DoCmplxDirect[Step] = NO; #endif return; - } - else vASSERT( Mode == spAUTO_PARTITION, "Invalid partition code" );; + } else + vASSERT(Mode == spAUTO_PARTITION, "Invalid partition code"); + ; -/* Otherwise, count all operations needed in when factoring matrix. */ + /* Otherwise, count all operations needed in when factoring matrix. */ Nc = (int *)Matrix->MarkowitzRow; No = (int *)Matrix->MarkowitzCol; Nm = (long *)Matrix->MarkowitzProd; -/* Start mock-factorization. */ - for (Step = 1; Step <= Size; Step++) - { Nc[Step] = No[Step] = Nm[Step] = 0; + /* Start mock-factorization. */ + for (Step = 1; Step <= Size; Step++) { + Nc[Step] = No[Step] = Nm[Step] = 0; pElement = Matrix->FirstInCol[Step]; - while (pElement != NULL) - { Nc[Step]++; + while (pElement != NULL) { + Nc[Step]++; pElement = pElement->NextInCol; } pColumn = Matrix->FirstInCol[Step]; - while (pColumn->Row < Step) /* FIXME: Access to Row results in dereference of NULL pointer */ - { pElement = Matrix->Diag[pColumn->Row]; + while (pColumn->Row < Step) /* FIXME: Access to Row results in + dereference of NULL pointer */ + { + pElement = Matrix->Diag[pColumn->Row]; Nm[Step]++; while ((pElement = pElement->NextInCol) != NULL) No[Step]++; @@ -681,24 +662,24 @@ BOOLEAN *DoRealDirect; } } - for (Step = 1; Step <= Size; Step++) - { -/* - * The following are just estimates based on a count on the number of - * machine instructions used on each machine to perform the various - * tasks. It was assumed that each machine instruction required the - * same amount of time (I don't believe this is true for the VAX, and - * have no idea if this is true for the 68000 family). For optimum - * performance, these numbers should be tuned to the machine. - * Nc is the number of nonzero elements in the column. - * Nm is the number of multipliers in the column. - * No is the number of operations in the inner loop. - */ + for (Step = 1; Step <= Size; Step++) { + /* + * The following are just estimates based on a count on the number of + * machine instructions used on each machine to perform the various + * tasks. It was assumed that each machine instruction required the + * same amount of time (I don't believe this is true for the VAX, and + * have no idea if this is true for the 68000 family). For optimum + * performance, these numbers should be tuned to the machine. + * Nc is the number of nonzero elements in the column. + * Nm is the number of multipliers in the column. + * No is the number of operations in the inner loop. + */ #define generic #ifdef hp9000s300 #if REAL - DoRealDirect[Step] = (Nm[Step] + No[Step] > 3*Nc[Step] - 2*Nm[Step]); + DoRealDirect[Step] = + (Nm[Step] + No[Step] > 3 * Nc[Step] - 2 * Nm[Step]); #endif #if spCOMPLEX /* On the hp350, it is never profitable to use direct for complex. */ @@ -709,27 +690,32 @@ BOOLEAN *DoRealDirect; #ifdef vax #if REAL - DoRealDirect[Step] = (Nm[Step] + No[Step] > 3*Nc[Step] - 2*Nm[Step]); + DoRealDirect[Step] = + (Nm[Step] + No[Step] > 3 * Nc[Step] - 2 * Nm[Step]); #endif #if spCOMPLEX - DoCmplxDirect[Step] = (Nm[Step] + No[Step] > 7*Nc[Step] - 4*Nm[Step]); + DoCmplxDirect[Step] = + (Nm[Step] + No[Step] > 7 * Nc[Step] - 4 * Nm[Step]); #endif #undef generic #endif #ifdef generic #if REAL - DoRealDirect[Step] = (Nm[Step] + No[Step] > 3*Nc[Step] - 2*Nm[Step]); + DoRealDirect[Step] = + (Nm[Step] + No[Step] > 3 * Nc[Step] - 2 * Nm[Step]); #endif #if spCOMPLEX - DoCmplxDirect[Step] = (Nm[Step] + No[Step] > 7*Nc[Step] - 4*Nm[Step]); + DoCmplxDirect[Step] = + (Nm[Step] + No[Step] > 7 * Nc[Step] - 4 * Nm[Step]); #endif #undef generic #endif } #if (ANNOTATE == FULL) - { int Ops = 0; + { + int Ops = 0; for (Step = 1; Step <= Size; Step++) Ops += No[Step]; printf("Operation count for inner loop of factorization = %d.\n", Ops); @@ -738,12 +724,6 @@ BOOLEAN *DoRealDirect; return; } - - - - - - /* * CREATE INTERNAL VECTORS * @@ -757,51 +737,50 @@ BOOLEAN *DoRealDirect; * spNO_MEMORY */ -void -spcCreateInternalVectors( MatrixPtr Matrix ) -{ -int Size; +void spcCreateInternalVectors(MatrixPtr Matrix) { + int Size; -/* Begin `spcCreateInternalVectors'. */ -/* Create Markowitz arrays. */ - Size= Matrix->Size; + /* Begin `spcCreateInternalVectors'. */ + /* Create Markowitz arrays. */ + Size = Matrix->Size; - if (Matrix->MarkowitzRow == NULL) - { if (( Matrix->MarkowitzRow = SP_MALLOC(int, Size+1)) == NULL) + if (Matrix->MarkowitzRow == NULL) { + if ((Matrix->MarkowitzRow = SP_MALLOC(int, Size + 1)) == NULL) Matrix->Error = spNO_MEMORY; } - if (Matrix->MarkowitzCol == NULL) - { if (( Matrix->MarkowitzCol = SP_MALLOC(int, Size+1)) == NULL) + if (Matrix->MarkowitzCol == NULL) { + if ((Matrix->MarkowitzCol = SP_MALLOC(int, Size + 1)) == NULL) Matrix->Error = spNO_MEMORY; } - if (Matrix->MarkowitzProd == NULL) - { if (( Matrix->MarkowitzProd = SP_MALLOC(long, Size+2)) == NULL) + if (Matrix->MarkowitzProd == NULL) { + if ((Matrix->MarkowitzProd = SP_MALLOC(long, Size + 2)) == NULL) Matrix->Error = spNO_MEMORY; } /* Create DoDirect vectors for use in spFactor(). */ #if REAL - if (Matrix->DoRealDirect == NULL) - { if (( Matrix->DoRealDirect = SP_MALLOC(BOOLEAN, Size+1)) == NULL) + if (Matrix->DoRealDirect == NULL) { + if ((Matrix->DoRealDirect = SP_MALLOC(BOOLEAN, Size + 1)) == NULL) Matrix->Error = spNO_MEMORY; } #endif #if spCOMPLEX - if (Matrix->DoCmplxDirect == NULL) - { if (( Matrix->DoCmplxDirect = SP_MALLOC(BOOLEAN, Size+1)) == NULL) + if (Matrix->DoCmplxDirect == NULL) { + if ((Matrix->DoCmplxDirect = SP_MALLOC(BOOLEAN, Size + 1)) == NULL) Matrix->Error = spNO_MEMORY; } #endif /* Create Intermediate vectors for use in MatrixSolve. */ #if spCOMPLEX - if (Matrix->Intermediate == NULL) - { if ((Matrix->Intermediate = SP_MALLOC(RealNumber,2*(Size+1))) == NULL) + if (Matrix->Intermediate == NULL) { + if ((Matrix->Intermediate = SP_MALLOC(RealNumber, 2 * (Size + 1))) == + NULL) Matrix->Error = spNO_MEMORY; } #else - if (Matrix->Intermediate == NULL) - { if ((Matrix->Intermediate = SP_MALLOC(RealNumber, Size+1)) == NULL) + if (Matrix->Intermediate == NULL) { + if ((Matrix->Intermediate = SP_MALLOC(RealNumber, Size + 1)) == NULL) Matrix->Error = spNO_MEMORY; } #endif @@ -811,12 +790,6 @@ int Size; return; } - - - - - - /* * COUNT MARKOWITZ * @@ -844,72 +817,68 @@ int Size; * The size of the matrix. */ -static void -CountMarkowitz( - MatrixPtr Matrix, - RealVector RHS, - int Step -) -{ -int Count, I, Size = Matrix->Size; -ElementPtr pElement; -int ExtRow; +static void CountMarkowitz(MatrixPtr Matrix, RealVector RHS, int Step) { + int Count, I, Size = Matrix->Size; + ElementPtr pElement; + int ExtRow; /* Begin `CountMarkowitz'. */ /* Correct array pointer for ARRAY_OFFSET. */ #if NOT ARRAY_OFFSET #if spSEPARATED_COMPLEX_VECTORS OR NOT spCOMPLEX - if (RHS != NULL) --RHS; + if (RHS != NULL) + --RHS; #else - if (RHS != NULL) - { if (Matrix->Complex) RHS -= 2; - else --RHS; - } + if (RHS != NULL) { + if (Matrix->Complex) + RHS -= 2; + else + --RHS; + } #endif #endif -/* Generate MarkowitzRow Count for each row. */ - for (I = Step; I <= Size; I++) - { -/* Set Count to -1 initially to remove count due to pivot element. */ + /* Generate MarkowitzRow Count for each row. */ + for (I = Step; I <= Size; I++) { + /* Set Count to -1 initially to remove count due to pivot element. */ Count = -1; pElement = Matrix->FirstInRow[I]; while (pElement != NULL AND pElement->Col < Step) pElement = pElement->NextInRow; - while (pElement != NULL) - { Count++; + while (pElement != NULL) { + Count++; pElement = pElement->NextInRow; } -/* Include nonzero elements in the RHS vector. */ + /* Include nonzero elements in the RHS vector. */ ExtRow = Matrix->IntToExtRowMap[I]; #if spSEPARATED_COMPLEX_VECTORS OR NOT spCOMPLEX if (RHS != NULL) - if (RHS[ExtRow] != 0.0) Count++; + if (RHS[ExtRow] != 0.0) + Count++; #else - if (RHS != NULL) - { if (Matrix->Complex) - { if ((RHS[2*ExtRow] != 0.0) OR (RHS[2*ExtRow+1] != 0.0)) + if (RHS != NULL) { + if (Matrix->Complex) { + if ((RHS[2 * ExtRow] != 0.0) OR(RHS[2 * ExtRow + 1] != 0.0)) Count++; - } - else if (RHS[I] != 0.0) Count++; + } else if (RHS[I] != 0.0) + Count++; } #endif Matrix->MarkowitzRow[I] = Count; } -/* Generate the MarkowitzCol count for each column. */ - for (I = Step; I <= Size; I++) - { -/* Set Count to -1 initially to remove count due to pivot element. */ + /* Generate the MarkowitzCol count for each column. */ + for (I = Step; I <= Size; I++) { + /* Set Count to -1 initially to remove count due to pivot element. */ Count = -1; pElement = Matrix->FirstInCol[I]; while (pElement != NULL AND pElement->Row < Step) pElement = pElement->NextInCol; - while (pElement != NULL) - { Count++; + while (pElement != NULL) { + Count++; pElement = pElement->NextInCol; } Matrix->MarkowitzCol[I] = Count; @@ -917,15 +886,6 @@ int ExtRow; return; } - - - - - - - - - /* * MARKOWITZ PRODUCTS * @@ -954,37 +914,31 @@ int ExtRow; * The size of the matrix. */ -static void -MarkowitzProducts( - MatrixPtr Matrix, - int Step -) -{ -int I, *pMarkowitzRow, *pMarkowitzCol; -long Product, *pMarkowitzProduct; -int Size = Matrix->Size; -double fProduct; +static void MarkowitzProducts(MatrixPtr Matrix, int Step) { + int I, *pMarkowitzRow, *pMarkowitzCol; + long Product, *pMarkowitzProduct; + int Size = Matrix->Size; + double fProduct; -/* Begin `MarkowitzProducts'. */ + /* Begin `MarkowitzProducts'. */ Matrix->Singletons = 0; pMarkowitzProduct = &(Matrix->MarkowitzProd[Step]); pMarkowitzRow = &(Matrix->MarkowitzRow[Step]); pMarkowitzCol = &(Matrix->MarkowitzCol[Step]); - for (I = Step; I <= Size; I++) - { -/* If chance of overflow, use real numbers. */ - if ((*pMarkowitzRow > LARGEST_SHORT_INTEGER AND *pMarkowitzCol != 0) OR - (*pMarkowitzCol > LARGEST_SHORT_INTEGER AND *pMarkowitzRow != 0)) - { fProduct = (double)(*pMarkowitzRow++) * (double)(*pMarkowitzCol++); + for (I = Step; I <= Size; I++) { + /* If chance of overflow, use real numbers. */ + if ((*pMarkowitzRow > LARGEST_SHORT_INTEGER AND * pMarkowitzCol != 0) + OR(*pMarkowitzCol > LARGEST_SHORT_INTEGER AND * pMarkowitzRow != + 0)) { + fProduct = (double)(*pMarkowitzRow++) * (double)(*pMarkowitzCol++); if (fProduct >= LARGEST_LONG_INTEGER) *pMarkowitzProduct++ = LARGEST_LONG_INTEGER; else *pMarkowitzProduct++ = (long)fProduct; - } - else - { Product = *pMarkowitzRow++ * *pMarkowitzCol++; + } else { + Product = *pMarkowitzRow++ * *pMarkowitzCol++; if ((*pMarkowitzProduct++ = Product) == 0) Matrix->Singletons++; } @@ -992,16 +946,6 @@ double fProduct; return; } - - - - - - - - - - /* * SEARCH FOR BEST PIVOT * @@ -1041,68 +985,56 @@ double fProduct; * spSMALL_PIVOT */ -static ElementPtr -SearchForPivot( - MatrixPtr Matrix, - int Step, - BOOLEAN DiagPivoting -) -{ -ElementPtr ChosenPivot; +static ElementPtr SearchForPivot(MatrixPtr Matrix, int Step, + BOOLEAN DiagPivoting) { + ElementPtr ChosenPivot; -/* Begin `SearchForPivot'. */ + /* Begin `SearchForPivot'. */ -/* If singletons exist, look for an acceptable one to use as pivot. */ - if (Matrix->Singletons) - { ChosenPivot = SearchForSingleton( Matrix, Step ); - if (ChosenPivot != NULL) - { Matrix->PivotSelectionMethod = 's'; + /* If singletons exist, look for an acceptable one to use as pivot. */ + if (Matrix->Singletons) { + ChosenPivot = SearchForSingleton(Matrix, Step); + if (ChosenPivot != NULL) { + Matrix->PivotSelectionMethod = 's'; return ChosenPivot; } } #if DIAGONAL_PIVOTING - if (DiagPivoting) - { -/* - * Either no singletons exist or they weren't acceptable. Take quick first - * pass at searching diagonal. First search for element on diagonal of - * remaining submatrix with smallest Markowitz product, then check to see - * if it okay numerically. If not, QuicklySearchDiagonal fails. - */ - ChosenPivot = QuicklySearchDiagonal( Matrix, Step ); - if (ChosenPivot != NULL) - { Matrix->PivotSelectionMethod = 'q'; + if (DiagPivoting) { + /* + * Either no singletons exist or they weren't acceptable. Take quick + * first pass at searching diagonal. First search for element on + * diagonal of remaining submatrix with smallest Markowitz product, then + * check to see if it okay numerically. If not, QuicklySearchDiagonal + * fails. + */ + ChosenPivot = QuicklySearchDiagonal(Matrix, Step); + if (ChosenPivot != NULL) { + Matrix->PivotSelectionMethod = 'q'; return ChosenPivot; } -/* - * Quick search of diagonal failed, carefully search diagonal and check each - * pivot candidate numerically before even tentatively accepting it. - */ - ChosenPivot = SearchDiagonal( Matrix, Step ); - if (ChosenPivot != NULL) - { Matrix->PivotSelectionMethod = 'd'; + /* + * Quick search of diagonal failed, carefully search diagonal and check + * each pivot candidate numerically before even tentatively accepting + * it. + */ + ChosenPivot = SearchDiagonal(Matrix, Step); + if (ChosenPivot != NULL) { + Matrix->PivotSelectionMethod = 'd'; return ChosenPivot; } } #endif /* DIAGONAL_PIVOTING */ -/* No acceptable pivot found yet, search entire matrix. */ - ChosenPivot = SearchEntireMatrix( Matrix, Step ); + /* No acceptable pivot found yet, search entire matrix. */ + ChosenPivot = SearchEntireMatrix(Matrix, Step); Matrix->PivotSelectionMethod = 'e'; return ChosenPivot; } - - - - - - - - /* * SEARCH FOR SINGLETON TO USE AS PIVOT * @@ -1139,159 +1071,134 @@ ElementPtr ChosenPivot; * access successive Markowitz products. */ -static ElementPtr -SearchForSingleton( - MatrixPtr Matrix, - int Step -) -{ -ElementPtr ChosenPivot; -int I; -long *pMarkowitzProduct; -int Singletons; -RealNumber PivotMag; +static ElementPtr SearchForSingleton(MatrixPtr Matrix, int Step) { + ElementPtr ChosenPivot; + int I; + long *pMarkowitzProduct; + int Singletons; + RealNumber PivotMag; -/* Begin `SearchForSingleton'. */ -/* Initialize pointer that is to scan through MarkowitzProduct vector. */ - pMarkowitzProduct = &(Matrix->MarkowitzProd[Matrix->Size+1]); - Matrix->MarkowitzProd[Matrix->Size+1] = Matrix->MarkowitzProd[Step]; + /* Begin `SearchForSingleton'. */ + /* Initialize pointer that is to scan through MarkowitzProduct vector. */ + pMarkowitzProduct = &(Matrix->MarkowitzProd[Matrix->Size + 1]); + Matrix->MarkowitzProd[Matrix->Size + 1] = Matrix->MarkowitzProd[Step]; -/* Decrement the count of available singletons, on the assumption that an - * acceptable one will be found. */ + /* Decrement the count of available singletons, on the assumption that an + * acceptable one will be found. */ Singletons = Matrix->Singletons--; -/* - * Assure that following while loop will always terminate, this is just - * preventive medicine, if things are working right this should never - * be needed. - */ - Matrix->MarkowitzProd[Step-1] = 0; + /* + * Assure that following while loop will always terminate, this is just + * preventive medicine, if things are working right this should never + * be needed. + */ + Matrix->MarkowitzProd[Step - 1] = 0; - while (Singletons-- > 0) - { -/* Singletons exist, find them. */ + while (Singletons-- > 0) { + /* Singletons exist, find them. */ -/* - * This is tricky. Am using a pointer to sequentially step through the - * MarkowitzProduct array. Search terminates when singleton (Product = 0) - * is found. Note that the conditional in the while statement - * ( *pMarkowitzProduct ) is true as long as the MarkowitzProduct is not - * equal to zero. The row (and column) index on the diagonal is then - * calculated by subtracting the pointer to the Markowitz product of - * the first diagonal from the pointer to the Markowitz product of the - * desired element, the singleton. - * - * Search proceeds from the end (high row and column numbers) to the - * beginning (low row and column numbers) so that rows and columns with - * large Markowitz products will tend to be move to the bottom of the - * matrix. However, choosing Diag[Step] is desirable because it would - * require no row and column interchanges, so inspect it first by - * putting its Markowitz product at the end of the MarkowitzProd - * vector. - */ + /* + * This is tricky. Am using a pointer to sequentially step through the + * MarkowitzProduct array. Search terminates when singleton (Product = + * 0) is found. Note that the conditional in the while statement ( + * *pMarkowitzProduct ) is true as long as the MarkowitzProduct is not + * equal to zero. The row (and column) index on the diagonal is then + * calculated by subtracting the pointer to the Markowitz product of + * the first diagonal from the pointer to the Markowitz product of the + * desired element, the singleton. + * + * Search proceeds from the end (high row and column numbers) to the + * beginning (low row and column numbers) so that rows and columns with + * large Markowitz products will tend to be move to the bottom of the + * matrix. However, choosing Diag[Step] is desirable because it would + * require no row and column interchanges, so inspect it first by + * putting its Markowitz product at the end of the MarkowitzProd + * vector. + */ - while ( *pMarkowitzProduct-- ) - { /* - * N bottles of beer on the wall; - * N bottles of beer. - * you take one down and pass it around; - * N-1 bottles of beer on the wall. - */ + while (*pMarkowitzProduct--) { /* + * N bottles of beer on the wall; + * N bottles of beer. + * you take one down and pass it around; + * N-1 bottles of beer on the wall. + */ } I = pMarkowitzProduct - Matrix->MarkowitzProd + 1; -/* Assure that I is valid. */ - if (I < Step) break; /* while (Singletons-- > 0) */ - if (I > Matrix->Size) I = Step; + /* Assure that I is valid. */ + if (I < Step) + break; /* while (Singletons-- > 0) */ + if (I > Matrix->Size) + I = Step; -/* Singleton has been found in either/both row or/and column I. */ - if ((ChosenPivot = Matrix->Diag[I]) != NULL) - { -/* Singleton lies on the diagonal. */ + /* Singleton has been found in either/both row or/and column I. */ + if ((ChosenPivot = Matrix->Diag[I]) != NULL) { + /* Singleton lies on the diagonal. */ PivotMag = ELEMENT_MAG(ChosenPivot); - if - ( PivotMag > Matrix->AbsThreshold AND - PivotMag > Matrix->RelThreshold * - FindBiggestInColExclude( Matrix, ChosenPivot, Step ) - ) return ChosenPivot; - } - else - { -/* Singleton does not lie on diagonal, find it. */ - if (Matrix->MarkowitzCol[I] == 0) - { ChosenPivot = Matrix->FirstInCol[I]; - while ((ChosenPivot != NULL) AND (ChosenPivot->Row < Step)) + if (PivotMag > Matrix->AbsThreshold AND PivotMag > + Matrix->RelThreshold * + FindBiggestInColExclude(Matrix, ChosenPivot, Step)) + return ChosenPivot; + } else { + /* Singleton does not lie on diagonal, find it. */ + if (Matrix->MarkowitzCol[I] == 0) { + ChosenPivot = Matrix->FirstInCol[I]; + while ((ChosenPivot != NULL) AND(ChosenPivot->Row < Step)) ChosenPivot = ChosenPivot->NextInCol; - if (ChosenPivot == NULL) - { /* Reduced column has no elements, matrix is singular. */ - break; - } - PivotMag = ELEMENT_MAG( ChosenPivot ); - if - ( PivotMag > Matrix->AbsThreshold AND - PivotMag > Matrix->RelThreshold * - FindBiggestInColExclude( Matrix, ChosenPivot, - Step ) - ) return ChosenPivot; - else - { if (Matrix->MarkowitzRow[I] == 0) - { ChosenPivot = Matrix->FirstInRow[I]; - while((ChosenPivot != NULL) AND (ChosenPivot->Col Matrix->AbsThreshold AND PivotMag > + Matrix->RelThreshold * + FindBiggestInColExclude(Matrix, ChosenPivot, Step)) + return ChosenPivot; + else { + if (Matrix->MarkowitzRow[I] == 0) { + ChosenPivot = Matrix->FirstInRow[I]; + while ((ChosenPivot != NULL) + AND(ChosenPivot->Col < Step)) ChosenPivot = ChosenPivot->NextInRow; - if (ChosenPivot == NULL) - {/* Reduced row has no elements, matrix is singular. */ - break; - } + if (ChosenPivot == + NULL) { /* Reduced row has no elements, matrix is + singular. */ + break; + } PivotMag = ELEMENT_MAG(ChosenPivot); - if - ( PivotMag > Matrix->AbsThreshold AND - PivotMag > Matrix->RelThreshold * - FindBiggestInColExclude( Matrix, - ChosenPivot, - Step ) - ) return ChosenPivot; + if (PivotMag > Matrix->AbsThreshold AND PivotMag > + Matrix->RelThreshold * + FindBiggestInColExclude(Matrix, ChosenPivot, + Step)) + return ChosenPivot; } } - } - else - { ChosenPivot = Matrix->FirstInRow[I]; - while ((ChosenPivot != NULL) AND (ChosenPivot->Col < Step)) + } else { + ChosenPivot = Matrix->FirstInRow[I]; + while ((ChosenPivot != NULL) AND(ChosenPivot->Col < Step)) ChosenPivot = ChosenPivot->NextInRow; - if (ChosenPivot == NULL) - { /* Reduced row has no elements, matrix is singular. */ - break; - } + if (ChosenPivot == NULL) { /* Reduced row has no elements, + matrix is singular. */ + break; + } PivotMag = ELEMENT_MAG(ChosenPivot); - if - ( PivotMag > Matrix->AbsThreshold AND - PivotMag > Matrix->RelThreshold * - FindBiggestInColExclude( Matrix, ChosenPivot, - Step ) - ) return ChosenPivot; + if (PivotMag > Matrix->AbsThreshold AND PivotMag > + Matrix->RelThreshold * + FindBiggestInColExclude(Matrix, ChosenPivot, Step)) + return ChosenPivot; } } -/* Singleton not acceptable (too small), try another. */ + /* Singleton not acceptable (too small), try another. */ } /* end of while(lSingletons>0) */ -/* - * All singletons were unacceptable. Restore Matrix->Singletons count. - * Initial assumption that an acceptable singleton would be found was wrong. - */ + /* + * All singletons were unacceptable. Restore Matrix->Singletons count. + * Initial assumption that an acceptable singleton would be found was wrong. + */ Matrix->Singletons++; return NULL; } - - - - - - - - - - - #if DIAGONAL_PIVOTING #if MODIFIED_MARKOWITZ /* @@ -1361,155 +1268,143 @@ RealNumber PivotMag; * to determine if off diagonals are placed symmetricly. */ -static ElementPtr -QuicklySearchDiagonal( - MatrixPtr Matrix, - int Step -) -{ -long MinMarkowitzProduct, *pMarkowitzProduct; -ElementPtr pDiag, pOtherInRow, pOtherInCol; -int I, NumberOfTies; -ElementPtr ChosenPivot, TiedElements[MAX_MARKOWITZ_TIES + 1]; -RealNumber Magnitude, LargestInCol, Ratio, MaxRatio; -RealNumber LargestOffDiagonal; -RealNumber FindBiggestInColExclude(); +static ElementPtr QuicklySearchDiagonal(MatrixPtr Matrix, int Step) { + long MinMarkowitzProduct, *pMarkowitzProduct; + ElementPtr pDiag, pOtherInRow, pOtherInCol; + int I, NumberOfTies; + ElementPtr ChosenPivot, TiedElements[MAX_MARKOWITZ_TIES + 1]; + RealNumber Magnitude, LargestInCol, Ratio, MaxRatio; + RealNumber LargestOffDiagonal; + RealNumber FindBiggestInColExclude(); -/* Begin `QuicklySearchDiagonal'. */ + /* Begin `QuicklySearchDiagonal'. */ NumberOfTies = -1; MinMarkowitzProduct = LARGEST_LONG_INTEGER; - pMarkowitzProduct = &(Matrix->MarkowitzProd[Matrix->Size+2]); - Matrix->MarkowitzProd[Matrix->Size+1] = Matrix->MarkowitzProd[Step]; + pMarkowitzProduct = &(Matrix->MarkowitzProd[Matrix->Size + 2]); + Matrix->MarkowitzProd[Matrix->Size + 1] = Matrix->MarkowitzProd[Step]; -/* Assure that following while loop will always terminate. */ - Matrix->MarkowitzProd[Step-1] = -1; + /* Assure that following while loop will always terminate. */ + Matrix->MarkowitzProd[Step - 1] = -1; -/* - * This is tricky. Am using a pointer in the inner while loop to - * sequentially step through the MarkowitzProduct array. Search - * terminates when the Markowitz product of zero placed at location - * Step-1 is found. The row (and column) index on the diagonal is then - * calculated by subtracting the pointer to the Markowitz product of - * the first diagonal from the pointer to the Markowitz product of the - * desired element. The outer for loop is infinite, broken by using - * break. - * - * Search proceeds from the end (high row and column numbers) to the - * beginning (low row and column numbers) so that rows and columns with - * large Markowitz products will tend to be move to the bottom of the - * matrix. However, choosing Diag[Step] is desirable because it would - * require no row and column interchanges, so inspect it first by - * putting its Markowitz product at the end of the MarkowitzProd - * vector. - */ + /* + * This is tricky. Am using a pointer in the inner while loop to + * sequentially step through the MarkowitzProduct array. Search + * terminates when the Markowitz product of zero placed at location + * Step-1 is found. The row (and column) index on the diagonal is then + * calculated by subtracting the pointer to the Markowitz product of + * the first diagonal from the pointer to the Markowitz product of the + * desired element. The outer for loop is infinite, broken by using + * break. + * + * Search proceeds from the end (high row and column numbers) to the + * beginning (low row and column numbers) so that rows and columns with + * large Markowitz products will tend to be move to the bottom of the + * matrix. However, choosing Diag[Step] is desirable because it would + * require no row and column interchanges, so inspect it first by + * putting its Markowitz product at the end of the MarkowitzProd + * vector. + */ - for(;;) /* Endless for loop. */ - { while (MinMarkowitzProduct < *(--pMarkowitzProduct)) - { /* - * N bottles of beer on the wall; - * N bottles of beer. - * You take one down and pass it around; - * N-1 bottles of beer on the wall. - */ + for (;;) /* Endless for loop. */ + { + while ( + MinMarkowitzProduct < + *(--pMarkowitzProduct)) { /* + * N bottles of beer on the wall; + * N bottles of beer. + * You take one down and pass it around; + * N-1 bottles of beer on the wall. + */ } I = pMarkowitzProduct - Matrix->MarkowitzProd; -/* Assure that I is valid; if I < Step, terminate search. */ - if (I < Step) break; /* Endless for loop */ - if (I > Matrix->Size) I = Step; + /* Assure that I is valid; if I < Step, terminate search. */ + if (I < Step) + break; /* Endless for loop */ + if (I > Matrix->Size) + I = Step; if ((pDiag = Matrix->Diag[I]) == NULL) continue; /* Endless for loop */ if ((Magnitude = ELEMENT_MAG(pDiag)) <= Matrix->AbsThreshold) continue; /* Endless for loop */ - if (*pMarkowitzProduct == 1) - { -/* Case where only one element exists in row and column other than diagonal. */ + if (*pMarkowitzProduct == 1) { + /* Case where only one element exists in row and column other than + * diagonal. */ -/* Find off diagonal elements. */ + /* Find off diagonal elements. */ pOtherInRow = pDiag->NextInRow; pOtherInCol = pDiag->NextInCol; - if (pOtherInRow == NULL AND pOtherInCol == NULL) - { pOtherInRow = Matrix->FirstInRow[I]; - while(pOtherInRow != NULL) - { if (pOtherInRow->Col >= Step AND pOtherInRow->Col != I) - break; - pOtherInRow = pOtherInRow->NextInRow; - } - pOtherInCol = Matrix->FirstInCol[I]; - while(pOtherInCol != NULL) - { if (pOtherInCol->Row >= Step AND pOtherInCol->Row != I) - break; - pOtherInCol = pOtherInCol->NextInCol; - } + if (pOtherInRow == NULL AND pOtherInCol == NULL) { + pOtherInRow = Matrix->FirstInRow[I]; + while (pOtherInRow != NULL) { + if (pOtherInRow->Col >= Step AND pOtherInRow->Col != I) + break; + pOtherInRow = pOtherInRow->NextInRow; + } + pOtherInCol = Matrix->FirstInCol[I]; + while (pOtherInCol != NULL) { + if (pOtherInCol->Row >= Step AND pOtherInCol->Row != I) + break; + pOtherInCol = pOtherInCol->NextInCol; + } } -/* Accept diagonal as pivot if diagonal is larger than off diagonals and the - * off diagonals are placed symmetricly. */ - if (pOtherInRow != NULL AND pOtherInCol != NULL) - { if (pOtherInRow->Col == pOtherInCol->Row) - { LargestOffDiagonal = MAX(ELEMENT_MAG(pOtherInRow), - ELEMENT_MAG(pOtherInCol)); - if (Magnitude >= LargestOffDiagonal) - { -/* Accept pivot, it is unlikely to contribute excess error. */ + /* Accept diagonal as pivot if diagonal is larger than off diagonals + * and the off diagonals are placed symmetricly. */ + if (pOtherInRow != NULL AND pOtherInCol != NULL) { + if (pOtherInRow->Col == pOtherInCol->Row) { + LargestOffDiagonal = + MAX(ELEMENT_MAG(pOtherInRow), ELEMENT_MAG(pOtherInCol)); + if (Magnitude >= LargestOffDiagonal) { + /* Accept pivot, it is unlikely to contribute excess + * error. */ return pDiag; } } } } - if (*pMarkowitzProduct < MinMarkowitzProduct) - { -/* Notice strict inequality in test. This is a new smallest MarkowitzProduct. */ + if (*pMarkowitzProduct < MinMarkowitzProduct) { + /* Notice strict inequality in test. This is a new smallest + * MarkowitzProduct. */ TiedElements[0] = pDiag; MinMarkowitzProduct = *pMarkowitzProduct; NumberOfTies = 0; - } - else - { -/* This case handles Markowitz ties. */ - if (NumberOfTies < MAX_MARKOWITZ_TIES) - { TiedElements[++NumberOfTies] = pDiag; + } else { + /* This case handles Markowitz ties. */ + if (NumberOfTies < MAX_MARKOWITZ_TIES) { + TiedElements[++NumberOfTies] = pDiag; if (NumberOfTies >= MinMarkowitzProduct * TIES_MULTIPLIER) break; /* Endless for loop */ } } } /* End of endless for loop. */ -/* Test to see if any element was chosen as a pivot candidate. */ + /* Test to see if any element was chosen as a pivot candidate. */ if (NumberOfTies < 0) return NULL; -/* Determine which of tied elements is best numerically. */ + /* Determine which of tied elements is best numerically. */ ChosenPivot = NULL; MaxRatio = 1.0 / Matrix->RelThreshold; - for (I = 0; I <= NumberOfTies; I++) - { pDiag = TiedElements[I]; + for (I = 0; I <= NumberOfTies; I++) { + pDiag = TiedElements[I]; Magnitude = ELEMENT_MAG(pDiag); - LargestInCol = FindBiggestInColExclude( Matrix, pDiag, Step ); + LargestInCol = FindBiggestInColExclude(Matrix, pDiag, Step); Ratio = LargestInCol / Magnitude; - if (Ratio < MaxRatio) - { ChosenPivot = pDiag; + if (Ratio < MaxRatio) { + ChosenPivot = pDiag; MaxRatio = Ratio; } } return ChosenPivot; } - - - - - - - - - -#else /* Not MODIFIED_MARKOWITZ */ +#else /* Not MODIFIED_MARKOWITZ */ /* * QUICK SEARCH OF DIAGONAL FOR PIVOT WITH CONVENTIONAL MARKOWITZ * CRITERION @@ -1560,93 +1455,91 @@ RealNumber FindBiggestInColExclude(); * to determine if off diagonals are placed symmetricly. */ -static ElementPtr -QuicklySearchDiagonal( - MatrixPtr Matrix, - int Step -) -{ -long MinMarkowitzProduct, *pMarkowitzProduct; -ElementPtr pDiag; -int I; -ElementPtr ChosenPivot, pOtherInRow, pOtherInCol; -RealNumber Magnitude, LargestInCol, LargestOffDiagonal; +static ElementPtr QuicklySearchDiagonal(MatrixPtr Matrix, int Step) { + long MinMarkowitzProduct, *pMarkowitzProduct; + ElementPtr pDiag; + int I; + ElementPtr ChosenPivot, pOtherInRow, pOtherInCol; + RealNumber Magnitude, LargestInCol, LargestOffDiagonal; -/* Begin `QuicklySearchDiagonal'. */ + /* Begin `QuicklySearchDiagonal'. */ ChosenPivot = NULL; MinMarkowitzProduct = LARGEST_LONG_INTEGER; - pMarkowitzProduct = &(Matrix->MarkowitzProd[Matrix->Size+2]); - Matrix->MarkowitzProd[Matrix->Size+1] = Matrix->MarkowitzProd[Step]; + pMarkowitzProduct = &(Matrix->MarkowitzProd[Matrix->Size + 2]); + Matrix->MarkowitzProd[Matrix->Size + 1] = Matrix->MarkowitzProd[Step]; -/* Assure that following while loop will always terminate. */ - Matrix->MarkowitzProd[Step-1] = -1; + /* Assure that following while loop will always terminate. */ + Matrix->MarkowitzProd[Step - 1] = -1; -/* - * This is tricky. Am using a pointer in the inner while loop to - * sequentially step through the MarkowitzProduct array. Search - * terminates when the Markowitz product of zero placed at location - * Step-1 is found. The row (and column) index on the diagonal is then - * calculated by subtracting the pointer to the Markowitz product of - * the first diagonal from the pointer to the Markowitz product of the - * desired element. The outer for loop is infinite, broken by using - * break. - * - * Search proceeds from the end (high row and column numbers) to the - * beginning (low row and column numbers) so that rows and columns with - * large Markowitz products will tend to be move to the bottom of the - * matrix. However, choosing Diag[Step] is desirable because it would - * require no row and column interchanges, so inspect it first by - * putting its Markowitz product at the end of the MarkowitzProd - * vector. - */ + /* + * This is tricky. Am using a pointer in the inner while loop to + * sequentially step through the MarkowitzProduct array. Search + * terminates when the Markowitz product of zero placed at location + * Step-1 is found. The row (and column) index on the diagonal is then + * calculated by subtracting the pointer to the Markowitz product of + * the first diagonal from the pointer to the Markowitz product of the + * desired element. The outer for loop is infinite, broken by using + * break. + * + * Search proceeds from the end (high row and column numbers) to the + * beginning (low row and column numbers) so that rows and columns with + * large Markowitz products will tend to be move to the bottom of the + * matrix. However, choosing Diag[Step] is desirable because it would + * require no row and column interchanges, so inspect it first by + * putting its Markowitz product at the end of the MarkowitzProd + * vector. + */ - for (;;) /* Endless for loop. */ - { while (*(--pMarkowitzProduct) >= MinMarkowitzProduct) - { /* Just passing through. */ + for (;;) /* Endless for loop. */ + { + while (*(--pMarkowitzProduct) >= + MinMarkowitzProduct) { /* Just passing through. */ } I = pMarkowitzProduct - Matrix->MarkowitzProd; -/* Assure that I is valid; if I < Step, terminate search. */ - if (I < Step) break; /* Endless for loop */ - if (I > Matrix->Size) I = Step; + /* Assure that I is valid; if I < Step, terminate search. */ + if (I < Step) + break; /* Endless for loop */ + if (I > Matrix->Size) + I = Step; if ((pDiag = Matrix->Diag[I]) == NULL) continue; /* Endless for loop */ if ((Magnitude = ELEMENT_MAG(pDiag)) <= Matrix->AbsThreshold) continue; /* Endless for loop */ - if (*pMarkowitzProduct == 1) - { -/* Case where only one element exists in row and column other than diagonal. */ + if (*pMarkowitzProduct == 1) { + /* Case where only one element exists in row and column other than + * diagonal. */ -/* Find off-diagonal elements. */ + /* Find off-diagonal elements. */ pOtherInRow = pDiag->NextInRow; pOtherInCol = pDiag->NextInCol; - if (pOtherInRow == NULL AND pOtherInCol == NULL) - { pOtherInRow = Matrix->FirstInRow[I]; - while(pOtherInRow != NULL) - { if (pOtherInRow->Col >= Step AND pOtherInRow->Col != I) - break; - pOtherInRow = pOtherInRow->NextInRow; - } - pOtherInCol = Matrix->FirstInCol[I]; - while(pOtherInCol != NULL) - { if (pOtherInCol->Row >= Step AND pOtherInCol->Row != I) - break; - pOtherInCol = pOtherInCol->NextInCol; - } + if (pOtherInRow == NULL AND pOtherInCol == NULL) { + pOtherInRow = Matrix->FirstInRow[I]; + while (pOtherInRow != NULL) { + if (pOtherInRow->Col >= Step AND pOtherInRow->Col != I) + break; + pOtherInRow = pOtherInRow->NextInRow; + } + pOtherInCol = Matrix->FirstInCol[I]; + while (pOtherInCol != NULL) { + if (pOtherInCol->Row >= Step AND pOtherInCol->Row != I) + break; + pOtherInCol = pOtherInCol->NextInCol; + } } -/* Accept diagonal as pivot if diagonal is larger than off-diagonals and the - * off-diagonals are placed symmetricly. */ - if (pOtherInRow != NULL AND pOtherInCol != NULL) - { if (pOtherInRow->Col == pOtherInCol->Row) - { LargestOffDiagonal = MAX(ELEMENT_MAG(pOtherInRow), - ELEMENT_MAG(pOtherInCol)); - if (Magnitude >= LargestOffDiagonal) - { -/* Accept pivot, it is unlikely to contribute excess error. */ + /* Accept diagonal as pivot if diagonal is larger than off-diagonals + * and the off-diagonals are placed symmetricly. */ + if (pOtherInRow != NULL AND pOtherInCol != NULL) { + if (pOtherInRow->Col == pOtherInCol->Row) { + LargestOffDiagonal = + MAX(ELEMENT_MAG(pOtherInRow), ELEMENT_MAG(pOtherInCol)); + if (Magnitude >= LargestOffDiagonal) { + /* Accept pivot, it is unlikely to contribute excess + * error. */ return pDiag; } } @@ -1655,25 +1548,17 @@ RealNumber Magnitude, LargestInCol, LargestOffDiagonal; MinMarkowitzProduct = *pMarkowitzProduct; ChosenPivot = pDiag; - } /* End of endless for loop. */ + } /* End of endless for loop. */ - if (ChosenPivot != NULL) - { LargestInCol = FindBiggestInColExclude( Matrix, ChosenPivot, Step ); - if( ELEMENT_MAG(ChosenPivot) <= Matrix->RelThreshold * LargestInCol ) + if (ChosenPivot != NULL) { + LargestInCol = FindBiggestInColExclude(Matrix, ChosenPivot, Step); + if (ELEMENT_MAG(ChosenPivot) <= Matrix->RelThreshold * LargestInCol) ChosenPivot = NULL; } return ChosenPivot; } #endif /* Not MODIFIED_MARKOWITZ */ - - - - - - - - /* * SEARCH DIAGONAL FOR PIVOT WITH MODIFIED MARKOWITZ CRITERION * @@ -1724,29 +1609,23 @@ RealNumber Magnitude, LargestInCol, LargestOffDiagonal; * Ratio of the largest element in its column to its magnitude. */ -static ElementPtr -SearchDiagonal( - MatrixPtr Matrix, - int Step -) -{ -int J; -long MinMarkowitzProduct, *pMarkowitzProduct; -int I; -ElementPtr pDiag; -int NumberOfTies = 0, Size = Matrix->Size; -ElementPtr ChosenPivot; -RealNumber Magnitude, Ratio, RatioOfAccepted = 0, LargestInCol; +static ElementPtr SearchDiagonal(MatrixPtr Matrix, int Step) { + int J; + long MinMarkowitzProduct, *pMarkowitzProduct; + int I; + ElementPtr pDiag; + int NumberOfTies = 0, Size = Matrix->Size; + ElementPtr ChosenPivot; + RealNumber Magnitude, Ratio, RatioOfAccepted = 0, LargestInCol; -/* Begin `SearchDiagonal'. */ + /* Begin `SearchDiagonal'. */ ChosenPivot = NULL; MinMarkowitzProduct = LARGEST_LONG_INTEGER; - pMarkowitzProduct = &(Matrix->MarkowitzProd[Size+2]); - Matrix->MarkowitzProd[Size+1] = Matrix->MarkowitzProd[Step]; + pMarkowitzProduct = &(Matrix->MarkowitzProd[Size + 2]); + Matrix->MarkowitzProd[Size + 1] = Matrix->MarkowitzProd[Step]; -/* Start search of diagonal. */ - for (J = Size+1; J > Step; J--) - { + /* Start search of diagonal. */ + for (J = Size + 1; J > Step; J--) { if (*(--pMarkowitzProduct) > MinMarkowitzProduct) continue; /* for loop */ if (J > Matrix->Size) @@ -1758,26 +1637,24 @@ RealNumber Magnitude, Ratio, RatioOfAccepted = 0, LargestInCol; if ((Magnitude = ELEMENT_MAG(pDiag)) <= Matrix->AbsThreshold) continue; /* for loop */ -/* Test to see if diagonal's magnitude is acceptable. */ - LargestInCol = FindBiggestInColExclude( Matrix, pDiag, Step ); + /* Test to see if diagonal's magnitude is acceptable. */ + LargestInCol = FindBiggestInColExclude(Matrix, pDiag, Step); if (Magnitude <= Matrix->RelThreshold * LargestInCol) continue; /* for loop */ - if (*pMarkowitzProduct < MinMarkowitzProduct) - { -/* Notice strict inequality in test. This is a new smallest MarkowitzProduct. */ + if (*pMarkowitzProduct < MinMarkowitzProduct) { + /* Notice strict inequality in test. This is a new smallest + * MarkowitzProduct. */ ChosenPivot = pDiag; MinMarkowitzProduct = *pMarkowitzProduct; RatioOfAccepted = LargestInCol / Magnitude; NumberOfTies = 0; - } - else - { -/* This case handles Markowitz ties. */ + } else { + /* This case handles Markowitz ties. */ NumberOfTies++; Ratio = LargestInCol / Magnitude; - if (Ratio < RatioOfAccepted) - { ChosenPivot = pDiag; + if (Ratio < RatioOfAccepted) { + ChosenPivot = pDiag; RatioOfAccepted = Ratio; } if (NumberOfTies >= MinMarkowitzProduct * TIES_MULTIPLIER) @@ -1788,15 +1665,6 @@ RealNumber Magnitude, Ratio, RatioOfAccepted = 0, LargestInCol; } #endif /* DIAGONAL_PIVOTING */ - - - - - - - - - /* * SEARCH ENTIRE MATRIX FOR BEST PIVOT * @@ -1852,69 +1720,61 @@ RealNumber Magnitude, Ratio, RatioOfAccepted = 0, LargestInCol; * spSMALL_PIVOT */ -static ElementPtr -SearchEntireMatrix( - MatrixPtr Matrix, - int Step -) -{ -int I, Size = Matrix->Size; -ElementPtr pElement; -int NumberOfTies = 0; -long Product, MinMarkowitzProduct; -ElementPtr ChosenPivot, pLargestElement = NULL; -RealNumber Magnitude, LargestElementMag, Ratio, RatioOfAccepted = 0, LargestInCol; -RealNumber FindLargestInCol(); +static ElementPtr SearchEntireMatrix(MatrixPtr Matrix, int Step) { + int I, Size = Matrix->Size; + ElementPtr pElement; + int NumberOfTies = 0; + long Product, MinMarkowitzProduct; + ElementPtr ChosenPivot, pLargestElement = NULL; + RealNumber Magnitude, LargestElementMag, Ratio, RatioOfAccepted = 0, + LargestInCol; + RealNumber FindLargestInCol(); -/* Begin `SearchEntireMatrix'. */ + /* Begin `SearchEntireMatrix'. */ ChosenPivot = NULL; LargestElementMag = 0.0; MinMarkowitzProduct = LARGEST_LONG_INTEGER; -/* Start search of matrix on column by column basis. */ - for (I = Step; I <= Size; I++) - { pElement = Matrix->FirstInCol[I]; + /* Start search of matrix on column by column basis. */ + for (I = Step; I <= Size; I++) { + pElement = Matrix->FirstInCol[I]; while (pElement != NULL AND pElement->Row < Step) pElement = pElement->NextInCol; - if((LargestInCol = FindLargestInCol(pElement)) == 0.0) + if ((LargestInCol = FindLargestInCol(pElement)) == 0.0) continue; /* for loop */ - while (pElement != NULL) - { -/* Check to see if element is the largest encountered so far. If so, record - its magnitude and address. */ - if ((Magnitude = ELEMENT_MAG(pElement)) > LargestElementMag) - { LargestElementMag = Magnitude; + while (pElement != NULL) { + /* Check to see if element is the largest encountered so far. If + so, record its magnitude and address. */ + if ((Magnitude = ELEMENT_MAG(pElement)) > LargestElementMag) { + LargestElementMag = Magnitude; pLargestElement = pElement; } -/* Calculate element's MarkowitzProduct. */ - spcMarkoProd( Product, Matrix->MarkowitzRow[pElement->Row], - Matrix->MarkowitzCol[pElement->Col] ); + /* Calculate element's MarkowitzProduct. */ + spcMarkoProd(Product, Matrix->MarkowitzRow[pElement->Row], + Matrix->MarkowitzCol[pElement->Col]); -/* Test to see if element is acceptable as a pivot candidate. */ - if ((Product <= MinMarkowitzProduct) AND - (Magnitude > Matrix->RelThreshold * LargestInCol) AND - (Magnitude > Matrix->AbsThreshold)) - { -/* Test to see if element has lowest MarkowitzProduct yet found, or whether it - is tied with an element found earlier. */ - if (Product < MinMarkowitzProduct) - { -/* Notice strict inequality in test. This is a new smallest MarkowitzProduct. */ + /* Test to see if element is acceptable as a pivot candidate. */ + if ((Product <= MinMarkowitzProduct) + AND(Magnitude > Matrix->RelThreshold * LargestInCol) + AND(Magnitude > Matrix->AbsThreshold)) { + /* Test to see if element has lowest MarkowitzProduct yet found, + or whether it is tied with an element found earlier. */ + if (Product < MinMarkowitzProduct) { + /* Notice strict inequality in test. This is a new smallest + * MarkowitzProduct. */ ChosenPivot = pElement; MinMarkowitzProduct = Product; RatioOfAccepted = LargestInCol / Magnitude; NumberOfTies = 0; - } - else - { -/* This case handles Markowitz ties. */ + } else { + /* This case handles Markowitz ties. */ NumberOfTies++; Ratio = LargestInCol / Magnitude; - if (Ratio < RatioOfAccepted) - { ChosenPivot = pElement; + if (Ratio < RatioOfAccepted) { + ChosenPivot = pElement; RatioOfAccepted = Ratio; } if (NumberOfTies >= MinMarkowitzProduct * TIES_MULTIPLIER) @@ -1922,13 +1782,14 @@ RealNumber FindLargestInCol(); } } pElement = pElement->NextInCol; - } /* End of while(pElement != NULL) */ - } /* End of for(Step) */ + } /* End of while(pElement != NULL) */ + } /* End of for(Step) */ - if (ChosenPivot != NULL) return ChosenPivot; + if (ChosenPivot != NULL) + return ChosenPivot; - if (LargestElementMag == 0.0) - { Matrix->Error = spSINGULAR; + if (LargestElementMag == 0.0) { + Matrix->Error = spSINGULAR; return NULL; } @@ -1936,17 +1797,6 @@ RealNumber FindLargestInCol(); return pLargestElement; } - - - - - - - - - - - /* * DETERMINE THE MAGNITUDE OF THE LARGEST ELEMENT IN A COLUMN * @@ -1975,15 +1825,13 @@ RealNumber FindLargestInCol(); * The magnitude of the currently active element. */ -static RealNumber -FindLargestInCol( ElementPtr pElement ) -{ -RealNumber Magnitude, Largest = 0.0; +static RealNumber FindLargestInCol(ElementPtr pElement) { + RealNumber Magnitude, Largest = 0.0; -/* Begin `FindLargestInCol'. */ -/* Search column for largest element beginning at Element. */ - while (pElement != NULL) - { if ((Magnitude = ELEMENT_MAG(pElement)) > Largest) + /* Begin `FindLargestInCol'. */ + /* Search column for largest element beginning at Element. */ + while (pElement != NULL) { + if ((Magnitude = ELEMENT_MAG(pElement)) > Largest) Largest = Magnitude; pElement = pElement->NextInCol; } @@ -1991,15 +1839,6 @@ RealNumber Magnitude, Largest = 0.0; return Largest; } - - - - - - - - - /* * DETERMINE THE MAGNITUDE OF THE LARGEST ELEMENT IN A COLUMN * EXCLUDING AN ELEMENT @@ -2039,36 +1878,31 @@ RealNumber Magnitude, Largest = 0.0; * The row number of element to be excluded from the search. */ -static RealNumber -FindBiggestInColExclude( - MatrixPtr Matrix, - ElementPtr pElement, - int Step -) -{ -int Row; -int Col; -RealNumber Largest, Magnitude; +static RealNumber FindBiggestInColExclude(MatrixPtr Matrix, ElementPtr pElement, + int Step) { + int Row; + int Col; + RealNumber Largest, Magnitude; -/* Begin `FindBiggestInColExclude'. */ + /* Begin `FindBiggestInColExclude'. */ Row = pElement->Row; Col = pElement->Col; pElement = Matrix->FirstInCol[Col]; -/* Travel down column until reduced submatrix is entered. */ - while ((pElement != NULL) AND (pElement->Row < Step)) + /* Travel down column until reduced submatrix is entered. */ + while ((pElement != NULL) AND(pElement->Row < Step)) pElement = pElement->NextInCol; -/* Initialize the variable Largest. */ + /* Initialize the variable Largest. */ if (pElement->Row != Row) Largest = ELEMENT_MAG(pElement); else Largest = 0.0; -/* Search rest of column for largest element, avoiding excluded element. */ - while ((pElement = pElement->NextInCol) != NULL) - { if ((Magnitude = ELEMENT_MAG(pElement)) > Largest) - { if (pElement->Row != Row) + /* Search rest of column for largest element, avoiding excluded element. */ + while ((pElement = pElement->NextInCol) != NULL) { + if ((Magnitude = ELEMENT_MAG(pElement)) > Largest) { + if (pElement->Row != Row) Largest = Magnitude; } } @@ -2076,15 +1910,6 @@ RealNumber Largest, Magnitude; return Largest; } - - - - - - - - - /* * EXCHANGE ROWS AND COLUMNS * @@ -2116,85 +1941,79 @@ RealNumber Largest, Magnitude; * of the reduced submatrix. */ -static void -ExchangeRowsAndCols( - MatrixPtr Matrix, - ElementPtr pPivot, - int Step -) -{ -int Row, Col; -long OldMarkowitzProd_Step, OldMarkowitzProd_Row, OldMarkowitzProd_Col; +static void ExchangeRowsAndCols(MatrixPtr Matrix, ElementPtr pPivot, int Step) { + int Row, Col; + long OldMarkowitzProd_Step, OldMarkowitzProd_Row, OldMarkowitzProd_Col; -/* Begin `ExchangeRowsAndCols'. */ + /* Begin `ExchangeRowsAndCols'. */ Row = pPivot->Row; Col = pPivot->Col; Matrix->PivotsOriginalRow = Row; Matrix->PivotsOriginalCol = Col; - if ((Row == Step) AND (Col == Step)) return; + if ((Row == Step) AND(Col == Step)) + return; -/* Exchange rows and columns. */ - if (Row == Col) - { spcRowExchange( Matrix, Step, Row ); - spcColExchange( Matrix, Step, Col ); - SWAP( long, Matrix->MarkowitzProd[Step], Matrix->MarkowitzProd[Row] ); - SWAP( ElementPtr, Matrix->Diag[Row], Matrix->Diag[Step] ); - } - else - { + /* Exchange rows and columns. */ + if (Row == Col) { + spcRowExchange(Matrix, Step, Row); + spcColExchange(Matrix, Step, Col); + SWAP(long, Matrix->MarkowitzProd[Step], Matrix->MarkowitzProd[Row]); + SWAP(ElementPtr, Matrix->Diag[Row], Matrix->Diag[Step]); + } else { -/* Initialize variables that hold old Markowitz products. */ + /* Initialize variables that hold old Markowitz products. */ OldMarkowitzProd_Step = Matrix->MarkowitzProd[Step]; OldMarkowitzProd_Row = Matrix->MarkowitzProd[Row]; OldMarkowitzProd_Col = Matrix->MarkowitzProd[Col]; -/* Exchange rows. */ - if (Row != Step) - { spcRowExchange( Matrix, Step, Row ); + /* Exchange rows. */ + if (Row != Step) { + spcRowExchange(Matrix, Step, Row); Matrix->NumberOfInterchangesIsOdd = - NOT Matrix->NumberOfInterchangesIsOdd; - spcMarkoProd( Matrix->MarkowitzProd[Row], - Matrix->MarkowitzRow[Row], - Matrix->MarkowitzCol[Row] ); + NOT Matrix->NumberOfInterchangesIsOdd; + spcMarkoProd(Matrix->MarkowitzProd[Row], Matrix->MarkowitzRow[Row], + Matrix->MarkowitzCol[Row]); -/* Update singleton count. */ - if ((Matrix->MarkowitzProd[Row]==0) != (OldMarkowitzProd_Row==0)) - { if (OldMarkowitzProd_Row == 0) + /* Update singleton count. */ + if ((Matrix->MarkowitzProd[Row] == 0) != + (OldMarkowitzProd_Row == 0)) { + if (OldMarkowitzProd_Row == 0) Matrix->Singletons--; else Matrix->Singletons++; } } -/* Exchange columns. */ - if (Col != Step) - { spcColExchange( Matrix, Step, Col ); + /* Exchange columns. */ + if (Col != Step) { + spcColExchange(Matrix, Step, Col); Matrix->NumberOfInterchangesIsOdd = - NOT Matrix->NumberOfInterchangesIsOdd; - spcMarkoProd( Matrix->MarkowitzProd[Col], - Matrix->MarkowitzCol[Col], - Matrix->MarkowitzRow[Col] ); + NOT Matrix->NumberOfInterchangesIsOdd; + spcMarkoProd(Matrix->MarkowitzProd[Col], Matrix->MarkowitzCol[Col], + Matrix->MarkowitzRow[Col]); -/* Update singleton count. */ - if ((Matrix->MarkowitzProd[Col]==0) != (OldMarkowitzProd_Col==0)) - { if (OldMarkowitzProd_Col == 0) + /* Update singleton count. */ + if ((Matrix->MarkowitzProd[Col] == 0) != + (OldMarkowitzProd_Col == 0)) { + if (OldMarkowitzProd_Col == 0) Matrix->Singletons--; else Matrix->Singletons++; } - Matrix->Diag[Col] = spcFindDiag( Matrix, Col ); + Matrix->Diag[Col] = spcFindDiag(Matrix, Col); } if (Row != Step) - Matrix->Diag[Row] = spcFindDiag( Matrix, Row ); - Matrix->Diag[Step] = spcFindDiag( Matrix, Step ); + Matrix->Diag[Row] = spcFindDiag(Matrix, Row); + Matrix->Diag[Step] = spcFindDiag(Matrix, Step); -/* Update singleton count. */ - Matrix->MarkowitzProd[Step] = Matrix->MarkowitzCol[Step] * - Matrix->MarkowitzRow[Step]; - if ((Matrix->MarkowitzProd[Step]==0) != (OldMarkowitzProd_Step==0)) - { if (OldMarkowitzProd_Step == 0) + /* Update singleton count. */ + Matrix->MarkowitzProd[Step] = + Matrix->MarkowitzCol[Step] * Matrix->MarkowitzRow[Step]; + if ((Matrix->MarkowitzProd[Step] == 0) != + (OldMarkowitzProd_Step == 0)) { + if (OldMarkowitzProd_Step == 0) Matrix->Singletons--; else Matrix->Singletons++; @@ -2203,14 +2022,6 @@ long OldMarkowitzProd_Step, OldMarkowitzProd_Row, OldMarkowitzProd_Col; return; } - - - - - - - - /* * EXCHANGE ROWS * @@ -2240,80 +2051,63 @@ long OldMarkowitzProd_Step, OldMarkowitzProd_Row, OldMarkowitzProd_Col; * Pointer to the element in Row2 to be exchanged. */ -void -spcRowExchange( - MatrixPtr Matrix, - int Row1, - int Row2 -) -{ -ElementPtr Row1Ptr, Row2Ptr; -int Column; -ElementPtr Element1, Element2; +void spcRowExchange(MatrixPtr Matrix, int Row1, int Row2) { + ElementPtr Row1Ptr, Row2Ptr; + int Column; + ElementPtr Element1, Element2; -/* Begin `spcRowExchange'. */ - if (Row1 > Row2) SWAP(int, Row1, Row2); + /* Begin `spcRowExchange'. */ + if (Row1 > Row2) + SWAP(int, Row1, Row2); Row1Ptr = Matrix->FirstInRow[Row1]; Row2Ptr = Matrix->FirstInRow[Row2]; - while (Row1Ptr != NULL OR Row2Ptr != NULL) - { -/* Exchange elements in rows while traveling from left to right. */ - if (Row1Ptr == NULL) - { Column = Row2Ptr->Col; + while (Row1Ptr != NULL OR Row2Ptr != NULL) { + /* Exchange elements in rows while traveling from left to right. */ + if (Row1Ptr == NULL) { + Column = Row2Ptr->Col; Element1 = NULL; Element2 = Row2Ptr; Row2Ptr = Row2Ptr->NextInRow; - } - else if (Row2Ptr == NULL) - { Column = Row1Ptr->Col; + } else if (Row2Ptr == NULL) { + Column = Row1Ptr->Col; Element1 = Row1Ptr; Element2 = NULL; Row1Ptr = Row1Ptr->NextInRow; - } - else if (Row1Ptr->Col < Row2Ptr->Col) - { Column = Row1Ptr->Col; + } else if (Row1Ptr->Col < Row2Ptr->Col) { + Column = Row1Ptr->Col; Element1 = Row1Ptr; Element2 = NULL; Row1Ptr = Row1Ptr->NextInRow; - } - else if (Row1Ptr->Col > Row2Ptr->Col) - { Column = Row2Ptr->Col; + } else if (Row1Ptr->Col > Row2Ptr->Col) { + Column = Row2Ptr->Col; Element1 = NULL; Element2 = Row2Ptr; Row2Ptr = Row2Ptr->NextInRow; - } - else /* Row1Ptr->Col == Row2Ptr->Col */ - { Column = Row1Ptr->Col; + } else /* Row1Ptr->Col == Row2Ptr->Col */ + { + Column = Row1Ptr->Col; Element1 = Row1Ptr; Element2 = Row2Ptr; Row1Ptr = Row1Ptr->NextInRow; Row2Ptr = Row2Ptr->NextInRow; } - ExchangeColElements( Matrix, Row1, Element1, Row2, Element2, Column); - } /* end of while(Row1Ptr != NULL OR Row2Ptr != NULL) */ + ExchangeColElements(Matrix, Row1, Element1, Row2, Element2, Column); + } /* end of while(Row1Ptr != NULL OR Row2Ptr != NULL) */ if (Matrix->InternalVectorsAllocated) - SWAP( int, Matrix->MarkowitzRow[Row1], Matrix->MarkowitzRow[Row2]); - SWAP( ElementPtr, Matrix->FirstInRow[Row1], Matrix->FirstInRow[Row2]); - SWAP( int, Matrix->IntToExtRowMap[Row1], Matrix->IntToExtRowMap[Row2]); + SWAP(int, Matrix->MarkowitzRow[Row1], Matrix->MarkowitzRow[Row2]); + SWAP(ElementPtr, Matrix->FirstInRow[Row1], Matrix->FirstInRow[Row2]); + SWAP(int, Matrix->IntToExtRowMap[Row1], Matrix->IntToExtRowMap[Row2]); #if TRANSLATE - Matrix->ExtToIntRowMap[ Matrix->IntToExtRowMap[Row1] ] = Row1; - Matrix->ExtToIntRowMap[ Matrix->IntToExtRowMap[Row2] ] = Row2; + Matrix->ExtToIntRowMap[Matrix->IntToExtRowMap[Row1]] = Row1; + Matrix->ExtToIntRowMap[Matrix->IntToExtRowMap[Row2]] = Row2; #endif return; } - - - - - - - - /* * EXCHANGE COLUMNS * @@ -2343,78 +2137,63 @@ ElementPtr Element1, Element2; * Pointer to the element in Col2 to be exchanged. */ -void -spcColExchange( - MatrixPtr Matrix, - int Col1, - int Col2 -) -{ -ElementPtr Col1Ptr, Col2Ptr; -int Row; -ElementPtr Element1, Element2; +void spcColExchange(MatrixPtr Matrix, int Col1, int Col2) { + ElementPtr Col1Ptr, Col2Ptr; + int Row; + ElementPtr Element1, Element2; -/* Begin `spcColExchange'. */ - if (Col1 > Col2) SWAP(int, Col1, Col2); + /* Begin `spcColExchange'. */ + if (Col1 > Col2) + SWAP(int, Col1, Col2); Col1Ptr = Matrix->FirstInCol[Col1]; Col2Ptr = Matrix->FirstInCol[Col2]; - while (Col1Ptr != NULL OR Col2Ptr != NULL) - { -/* Exchange elements in rows while traveling from top to bottom. */ - if (Col1Ptr == NULL) - { Row = Col2Ptr->Row; + while (Col1Ptr != NULL OR Col2Ptr != NULL) { + /* Exchange elements in rows while traveling from top to bottom. */ + if (Col1Ptr == NULL) { + Row = Col2Ptr->Row; Element1 = NULL; Element2 = Col2Ptr; Col2Ptr = Col2Ptr->NextInCol; - } - else if (Col2Ptr == NULL) - { Row = Col1Ptr->Row; + } else if (Col2Ptr == NULL) { + Row = Col1Ptr->Row; Element1 = Col1Ptr; Element2 = NULL; Col1Ptr = Col1Ptr->NextInCol; - } - else if (Col1Ptr->Row < Col2Ptr->Row) - { Row = Col1Ptr->Row; + } else if (Col1Ptr->Row < Col2Ptr->Row) { + Row = Col1Ptr->Row; Element1 = Col1Ptr; Element2 = NULL; Col1Ptr = Col1Ptr->NextInCol; - } - else if (Col1Ptr->Row > Col2Ptr->Row) - { Row = Col2Ptr->Row; + } else if (Col1Ptr->Row > Col2Ptr->Row) { + Row = Col2Ptr->Row; Element1 = NULL; Element2 = Col2Ptr; Col2Ptr = Col2Ptr->NextInCol; - } - else /* Col1Ptr->Row == Col2Ptr->Row */ - { Row = Col1Ptr->Row; + } else /* Col1Ptr->Row == Col2Ptr->Row */ + { + Row = Col1Ptr->Row; Element1 = Col1Ptr; Element2 = Col2Ptr; Col1Ptr = Col1Ptr->NextInCol; Col2Ptr = Col2Ptr->NextInCol; } - ExchangeRowElements( Matrix, Col1, Element1, Col2, Element2, Row); - } /* end of while(Col1Ptr != NULL OR Col2Ptr != NULL) */ + ExchangeRowElements(Matrix, Col1, Element1, Col2, Element2, Row); + } /* end of while(Col1Ptr != NULL OR Col2Ptr != NULL) */ if (Matrix->InternalVectorsAllocated) - SWAP( int, Matrix->MarkowitzCol[Col1], Matrix->MarkowitzCol[Col2]); - SWAP( ElementPtr, Matrix->FirstInCol[Col1], Matrix->FirstInCol[Col2]); - SWAP( int, Matrix->IntToExtColMap[Col1], Matrix->IntToExtColMap[Col2]); + SWAP(int, Matrix->MarkowitzCol[Col1], Matrix->MarkowitzCol[Col2]); + SWAP(ElementPtr, Matrix->FirstInCol[Col1], Matrix->FirstInCol[Col2]); + SWAP(int, Matrix->IntToExtColMap[Col1], Matrix->IntToExtColMap[Col2]); #if TRANSLATE - Matrix->ExtToIntColMap[ Matrix->IntToExtColMap[Col1] ] = Col1; - Matrix->ExtToIntColMap[ Matrix->IntToExtColMap[Col2] ] = Col2; + Matrix->ExtToIntColMap[Matrix->IntToExtColMap[Col1]] = Col1; + Matrix->ExtToIntColMap[Matrix->IntToExtColMap[Col2]] = Col2; #endif return; } - - - - - - /* * EXCHANGE TWO ELEMENTS IN A COLUMN * @@ -2451,74 +2230,60 @@ ElementPtr Element1, Element2; * Pointer used to traverse the column. */ -static void -ExchangeColElements( - MatrixPtr Matrix, - int Row1, - ElementPtr Element1, - int Row2, - ElementPtr Element2, - int Column -) -{ -ElementPtr *ElementAboveRow1, *ElementAboveRow2; -ElementPtr ElementBelowRow1, ElementBelowRow2; -ElementPtr pElement; +static void ExchangeColElements(MatrixPtr Matrix, int Row1, ElementPtr Element1, + int Row2, ElementPtr Element2, int Column) { + ElementPtr *ElementAboveRow1, *ElementAboveRow2; + ElementPtr ElementBelowRow1, ElementBelowRow2; + ElementPtr pElement; -/* Begin `ExchangeColElements'. */ -/* Search to find the ElementAboveRow1. */ + /* Begin `ExchangeColElements'. */ + /* Search to find the ElementAboveRow1. */ ElementAboveRow1 = &(Matrix->FirstInCol[Column]); pElement = *ElementAboveRow1; - while (pElement->Row < Row1) - { ElementAboveRow1 = &(pElement->NextInCol); + while (pElement->Row < Row1) { + ElementAboveRow1 = &(pElement->NextInCol); pElement = *ElementAboveRow1; } - if (Element1 != NULL) - { ElementBelowRow1 = Element1->NextInCol; - if (Element2 == NULL) - { -/* Element2 does not exist, move Element1 down to Row2. */ - if ( ElementBelowRow1 != NULL AND ElementBelowRow1->Row < Row2 ) - { -/* Element1 must be removed from linked list and moved. */ + if (Element1 != NULL) { + ElementBelowRow1 = Element1->NextInCol; + if (Element2 == NULL) { + /* Element2 does not exist, move Element1 down to Row2. */ + if (ElementBelowRow1 != NULL AND ElementBelowRow1->Row < Row2) { + /* Element1 must be removed from linked list and moved. */ *ElementAboveRow1 = ElementBelowRow1; -/* Search column for Row2. */ + /* Search column for Row2. */ pElement = ElementBelowRow1; - do - { ElementAboveRow2 = &(pElement->NextInCol); + do { + ElementAboveRow2 = &(pElement->NextInCol); pElement = *ElementAboveRow2; - } while (pElement != NULL AND pElement->Row < Row2); + } while (pElement != NULL AND pElement->Row < Row2); -/* Place Element1 in Row2. */ + /* Place Element1 in Row2. */ *ElementAboveRow2 = Element1; Element1->NextInCol = pElement; - *ElementAboveRow1 =ElementBelowRow1; + *ElementAboveRow1 = ElementBelowRow1; } Element1->Row = Row2; - } - else - { -/* Element2 does exist, and the two elements must be exchanged. */ - if ( ElementBelowRow1->Row == Row2) - { -/* Element2 is just below Element1, exchange them. */ + } else { + /* Element2 does exist, and the two elements must be exchanged. */ + if (ElementBelowRow1->Row == Row2) { + /* Element2 is just below Element1, exchange them. */ Element1->NextInCol = Element2->NextInCol; Element2->NextInCol = Element1; *ElementAboveRow1 = Element2; - } - else - { -/* Element2 is not just below Element1 and must be searched for. */ + } else { + /* Element2 is not just below Element1 and must be searched for. + */ pElement = ElementBelowRow1; - do - { ElementAboveRow2 = &(pElement->NextInCol); + do { + ElementAboveRow2 = &(pElement->NextInCol); pElement = *ElementAboveRow2; - } while (pElement->Row < Row2); + } while (pElement->Row < Row2); ElementBelowRow2 = Element2->NextInCol; -/* Switch Element1 and Element2. */ + /* Switch Element1 and Element2. */ *ElementAboveRow1 = Element2; Element2->NextInCol = ElementBelowRow1; *ElementAboveRow2 = Element1; @@ -2527,22 +2292,21 @@ ElementPtr pElement; Element1->Row = Row2; Element2->Row = Row1; } - } - else - { -/* Element1 does not exist. */ + } else { + /* Element1 does not exist. */ ElementBelowRow1 = pElement; -/* Find Element2. */ - if (ElementBelowRow1->Row != Row2) - { do - { ElementAboveRow2 = &(pElement->NextInCol); + /* Find Element2. */ + if (ElementBelowRow1->Row != Row2) { + do { + ElementAboveRow2 = &(pElement->NextInCol); pElement = *ElementAboveRow2; - } while (pElement->Row < Row2); + } while (pElement->Row < Row2); - ElementBelowRow2 = Element2->NextInCol;/* FIXME: ElementBelowRow2 is never read */ + ElementBelowRow2 = + Element2->NextInCol; /* FIXME: ElementBelowRow2 is never read */ -/* Move Element2 to Row1. */ + /* Move Element2 to Row1. */ *ElementAboveRow2 = Element2->NextInCol; *ElementAboveRow1 = Element2; Element2->NextInCol = ElementBelowRow1; @@ -2552,12 +2316,6 @@ ElementPtr pElement; return; } - - - - - - /* * EXCHANGE TWO ELEMENTS IN A ROW * @@ -2596,74 +2354,60 @@ ElementPtr pElement; * Pointer used to traverse the row. */ -static void -ExchangeRowElements( - MatrixPtr Matrix, - int Col1, - ElementPtr Element1, - int Col2, - ElementPtr Element2, - int Row -) -{ -ElementPtr *ElementLeftOfCol1, *ElementLeftOfCol2; -ElementPtr ElementRightOfCol1, ElementRightOfCol2; - ElementPtr pElement; +static void ExchangeRowElements(MatrixPtr Matrix, int Col1, ElementPtr Element1, + int Col2, ElementPtr Element2, int Row) { + ElementPtr *ElementLeftOfCol1, *ElementLeftOfCol2; + ElementPtr ElementRightOfCol1, ElementRightOfCol2; + ElementPtr pElement; -/* Begin `ExchangeRowElements'. */ -/* Search to find the ElementLeftOfCol1. */ + /* Begin `ExchangeRowElements'. */ + /* Search to find the ElementLeftOfCol1. */ ElementLeftOfCol1 = &(Matrix->FirstInRow[Row]); pElement = *ElementLeftOfCol1; - while (pElement->Col < Col1) - { ElementLeftOfCol1 = &(pElement->NextInRow); + while (pElement->Col < Col1) { + ElementLeftOfCol1 = &(pElement->NextInRow); pElement = *ElementLeftOfCol1; } - if (Element1 != NULL) - { ElementRightOfCol1 = Element1->NextInRow; - if (Element2 == NULL) - { -/* Element2 does not exist, move Element1 to right to Col2. */ - if ( ElementRightOfCol1 != NULL AND ElementRightOfCol1->Col < Col2 ) - { -/* Element1 must be removed from linked list and moved. */ + if (Element1 != NULL) { + ElementRightOfCol1 = Element1->NextInRow; + if (Element2 == NULL) { + /* Element2 does not exist, move Element1 to right to Col2. */ + if (ElementRightOfCol1 != NULL AND ElementRightOfCol1->Col < Col2) { + /* Element1 must be removed from linked list and moved. */ *ElementLeftOfCol1 = ElementRightOfCol1; -/* Search Row for Col2. */ + /* Search Row for Col2. */ pElement = ElementRightOfCol1; - do - { ElementLeftOfCol2 = &(pElement->NextInRow); + do { + ElementLeftOfCol2 = &(pElement->NextInRow); pElement = *ElementLeftOfCol2; - } while (pElement != NULL AND pElement->Col < Col2); + } while (pElement != NULL AND pElement->Col < Col2); -/* Place Element1 in Col2. */ + /* Place Element1 in Col2. */ *ElementLeftOfCol2 = Element1; Element1->NextInRow = pElement; - *ElementLeftOfCol1 =ElementRightOfCol1; + *ElementLeftOfCol1 = ElementRightOfCol1; } Element1->Col = Col2; - } - else - { -/* Element2 does exist, and the two elements must be exchanged. */ - if ( ElementRightOfCol1->Col == Col2) - { -/* Element2 is just right of Element1, exchange them. */ + } else { + /* Element2 does exist, and the two elements must be exchanged. */ + if (ElementRightOfCol1->Col == Col2) { + /* Element2 is just right of Element1, exchange them. */ Element1->NextInRow = Element2->NextInRow; Element2->NextInRow = Element1; *ElementLeftOfCol1 = Element2; - } - else - { -/* Element2 is not just right of Element1 and must be searched for. */ + } else { + /* Element2 is not just right of Element1 and must be searched + * for. */ pElement = ElementRightOfCol1; - do - { ElementLeftOfCol2 = &(pElement->NextInRow); + do { + ElementLeftOfCol2 = &(pElement->NextInRow); pElement = *ElementLeftOfCol2; - } while (pElement->Col < Col2); + } while (pElement->Col < Col2); ElementRightOfCol2 = Element2->NextInRow; -/* Switch Element1 and Element2. */ + /* Switch Element1 and Element2. */ *ElementLeftOfCol1 = Element2; Element2->NextInRow = ElementRightOfCol1; *ElementLeftOfCol2 = Element1; @@ -2672,22 +2416,22 @@ ElementPtr ElementRightOfCol1, ElementRightOfCol2; Element1->Col = Col2; Element2->Col = Col1; } - } - else - { -/* Element1 does not exist. */ + } else { + /* Element1 does not exist. */ ElementRightOfCol1 = pElement; -/* Find Element2. */ - if (ElementRightOfCol1->Col != Col2) - { do - { ElementLeftOfCol2 = &(pElement->NextInRow); + /* Find Element2. */ + if (ElementRightOfCol1->Col != Col2) { + do { + ElementLeftOfCol2 = &(pElement->NextInRow); pElement = *ElementLeftOfCol2; - } while (pElement->Col < Col2); + } while (pElement->Col < Col2); - ElementRightOfCol2 = Element2->NextInRow; /* FIXME: ElementRightOfCol2 is never read */ + ElementRightOfCol2 = + Element2 + ->NextInRow; /* FIXME: ElementRightOfCol2 is never read */ -/* Move Element2 to Col1. */ + /* Move Element2 to Col1. */ *ElementLeftOfCol2 = Element2->NextInRow; *ElementLeftOfCol1 = Element2; Element2->NextInRow = ElementRightOfCol1; @@ -2697,16 +2441,6 @@ ElementPtr ElementRightOfCol1, ElementRightOfCol2; return; } - - - - - - - - - - /* * PERFORM ROW AND COLUMN ELIMINATION ON REAL MATRIX * @@ -2734,50 +2468,46 @@ ElementPtr ElementRightOfCol1, ElementRightOfCol2; * spNO_MEMORY */ -static void -RealRowColElimination( - MatrixPtr Matrix, - ElementPtr pPivot -) -{ +static void RealRowColElimination(MatrixPtr Matrix, ElementPtr pPivot) { #if REAL -ElementPtr pSub, *ppAbove; -int Row; -ElementPtr pLower, pUpper; + ElementPtr pSub, *ppAbove; + int Row; + ElementPtr pLower, pUpper; -/* Begin `RealRowColElimination'. */ + /* Begin `RealRowColElimination'. */ -/* Test for zero pivot. */ - if (ABS(pPivot->Real) == 0.0) - { (void)MatrixIsSingular( Matrix, pPivot->Row ); + /* Test for zero pivot. */ + if (ABS(pPivot->Real) == 0.0) { + (void)MatrixIsSingular(Matrix, pPivot->Row); return; } pPivot->Real = 1.0 / pPivot->Real; pUpper = pPivot->NextInRow; - while (pUpper != NULL) - { -/* Calculate upper triangular element. */ + while (pUpper != NULL) { + /* Calculate upper triangular element. */ pUpper->Real *= pPivot->Real; pSub = pUpper->NextInCol; pLower = pPivot->NextInCol; - ppAbove = &pUpper->NextInCol; - while (pLower != NULL) - { Row = pLower->Row; + ppAbove = &pUpper->NextInCol; + while (pLower != NULL) { + Row = pLower->Row; -/* Find element in row that lines up with current lower triangular element. */ - while (pSub != NULL AND pSub->Row < Row) - { ppAbove = &pSub->NextInCol; + /* Find element in row that lines up with current lower triangular + * element. */ + while (pSub != NULL AND pSub->Row < Row) { + ppAbove = &pSub->NextInCol; pSub = pSub->NextInCol; - } + } -/* Test to see if desired element was not found, if not, create fill-in. */ - if (pSub == NULL OR pSub->Row > Row) - { pSub = spcCreateElement( Matrix, Row, pUpper->Col, - &pLower->NextInRow, ppAbove, YES ); - if (pSub == NULL) - { Matrix->Error = spNO_MEMORY; + /* Test to see if desired element was not found, if not, create + * fill-in. */ + if (pSub == NULL OR pSub->Row > Row) { + pSub = spcCreateElement(Matrix, Row, pUpper->Col, + &pLower->NextInRow, ppAbove, YES); + if (pSub == NULL) { + Matrix->Error = spNO_MEMORY; return; } } @@ -2791,14 +2521,6 @@ ElementPtr pLower, pUpper; #endif /* REAL */ } - - - - - - - - /* * PERFORM ROW AND COLUMN ELIMINATION ON COMPLEX MATRIX * @@ -2826,56 +2548,52 @@ ElementPtr pLower, pUpper; * spNO_MEMORY */ -static void -ComplexRowColElimination( - MatrixPtr Matrix, - ElementPtr pPivot -) -{ +static void ComplexRowColElimination(MatrixPtr Matrix, ElementPtr pPivot) { #if spCOMPLEX -ElementPtr pSub, *ppAbove; -int Row; -ElementPtr pLower, pUpper; + ElementPtr pSub, *ppAbove; + int Row; + ElementPtr pLower, pUpper; -/* Begin `ComplexRowColElimination'. */ + /* Begin `ComplexRowColElimination'. */ -/* Test for zero pivot. */ - if (ELEMENT_MAG(pPivot) == 0.0) - { (void)MatrixIsSingular( Matrix, pPivot->Row ); + /* Test for zero pivot. */ + if (ELEMENT_MAG(pPivot) == 0.0) { + (void)MatrixIsSingular(Matrix, pPivot->Row); return; } CMPLX_RECIPROCAL(*pPivot, *pPivot); pUpper = pPivot->NextInRow; - while (pUpper != NULL) - { -/* Calculate upper triangular element. */ -/* Cmplx expr: *pUpper = *pUpper * (1.0 / *pPivot). */ + while (pUpper != NULL) { + /* Calculate upper triangular element. */ + /* Cmplx expr: *pUpper = *pUpper * (1.0 / *pPivot). */ CMPLX_MULT_ASSIGN(*pUpper, *pPivot); pSub = pUpper->NextInCol; pLower = pPivot->NextInCol; ppAbove = &pUpper->NextInCol; - while (pLower != NULL) - { Row = pLower->Row; + while (pLower != NULL) { + Row = pLower->Row; -/* Find element in row that lines up with current lower triangular element. */ - while (pSub != NULL AND pSub->Row < Row) - { ppAbove = &pSub->NextInCol; + /* Find element in row that lines up with current lower triangular + * element. */ + while (pSub != NULL AND pSub->Row < Row) { + ppAbove = &pSub->NextInCol; pSub = pSub->NextInCol; - } + } -/* Test to see if desired element was not found, if not, create fill-in. */ - if (pSub == NULL OR pSub->Row > Row) - { pSub = spcCreateElement( Matrix, Row, pUpper->Col, - &pLower->NextInRow, ppAbove, YES ); - if (pSub == NULL) - { Matrix->Error = spNO_MEMORY; + /* Test to see if desired element was not found, if not, create + * fill-in. */ + if (pSub == NULL OR pSub->Row > Row) { + pSub = spcCreateElement(Matrix, Row, pUpper->Col, + &pLower->NextInRow, ppAbove, YES); + if (pSub == NULL) { + Matrix->Error = spNO_MEMORY; return; } } -/* Cmplx expr: pElement -= *pUpper * pLower. */ + /* Cmplx expr: pElement -= *pUpper * pLower. */ CMPLX_MULT_SUBT_ASSIGN(*pSub, *pUpper, *pLower); pSub = pSub->NextInCol; pLower = pLower->NextInCol; @@ -2886,10 +2604,6 @@ ElementPtr pLower, pUpper; #endif /* spCOMPLEX */ } - - - - /* * UPDATE MARKOWITZ NUMBERS * @@ -2913,63 +2627,55 @@ ElementPtr pLower, pUpper; * Points to matrix element in lower triangular row. */ -static void -UpdateMarkowitzNumbers( - MatrixPtr Matrix, - ElementPtr pPivot -) -{ -int Row, Col; -ElementPtr ColPtr, RowPtr; -int *MarkoRow = Matrix->MarkowitzRow, *MarkoCol = Matrix->MarkowitzCol; -double Product; +static void UpdateMarkowitzNumbers(MatrixPtr Matrix, ElementPtr pPivot) { + int Row, Col; + ElementPtr ColPtr, RowPtr; + int *MarkoRow = Matrix->MarkowitzRow, *MarkoCol = Matrix->MarkowitzCol; + double Product; -/* Begin `UpdateMarkowitzNumbers'. */ + /* Begin `UpdateMarkowitzNumbers'. */ -/* Update Markowitz numbers. */ - for (ColPtr = pPivot->NextInCol; ColPtr != NULL; ColPtr = ColPtr->NextInCol) - { Row = ColPtr->Row; + /* Update Markowitz numbers. */ + for (ColPtr = pPivot->NextInCol; ColPtr != NULL; + ColPtr = ColPtr->NextInCol) { + Row = ColPtr->Row; --MarkoRow[Row]; -/* Form Markowitz product while being cautious of overflows. */ - if ((MarkoRow[Row] > LARGEST_SHORT_INTEGER AND MarkoCol[Row] != 0) OR - (MarkoCol[Row] > LARGEST_SHORT_INTEGER AND MarkoRow[Row] != 0)) - { Product = (double)MarkoCol[Row] * (double)MarkoRow[Row]; + /* Form Markowitz product while being cautious of overflows. */ + if ((MarkoRow[Row] > LARGEST_SHORT_INTEGER AND MarkoCol[Row] != 0) OR( + MarkoCol[Row] > LARGEST_SHORT_INTEGER AND MarkoRow[Row] != 0)) { + Product = (double)MarkoCol[Row] * (double)MarkoRow[Row]; if (Product >= LARGEST_LONG_INTEGER) Matrix->MarkowitzProd[Row] = LARGEST_LONG_INTEGER; else Matrix->MarkowitzProd[Row] = (long)Product; - } - else Matrix->MarkowitzProd[Row] = MarkoRow[Row] * MarkoCol[Row]; + } else + Matrix->MarkowitzProd[Row] = MarkoRow[Row] * MarkoCol[Row]; if (MarkoRow[Row] == 0) Matrix->Singletons++; } - for (RowPtr = pPivot->NextInRow; RowPtr != NULL; RowPtr = RowPtr->NextInRow) - { Col = RowPtr->Col; + for (RowPtr = pPivot->NextInRow; RowPtr != NULL; + RowPtr = RowPtr->NextInRow) { + Col = RowPtr->Col; --MarkoCol[Col]; -/* Form Markowitz product while being cautious of overflows. */ - if ((MarkoRow[Col] > LARGEST_SHORT_INTEGER AND MarkoCol[Col] != 0) OR - (MarkoCol[Col] > LARGEST_SHORT_INTEGER AND MarkoRow[Col] != 0)) - { Product = (double)MarkoCol[Col] * (double)MarkoRow[Col]; + /* Form Markowitz product while being cautious of overflows. */ + if ((MarkoRow[Col] > LARGEST_SHORT_INTEGER AND MarkoCol[Col] != 0) OR( + MarkoCol[Col] > LARGEST_SHORT_INTEGER AND MarkoRow[Col] != 0)) { + Product = (double)MarkoCol[Col] * (double)MarkoRow[Col]; if (Product >= LARGEST_LONG_INTEGER) Matrix->MarkowitzProd[Col] = LARGEST_LONG_INTEGER; else Matrix->MarkowitzProd[Col] = (long)Product; - } - else Matrix->MarkowitzProd[Col] = MarkoRow[Col] * MarkoCol[Col]; - if ((MarkoCol[Col] == 0) AND (MarkoRow[Col] != 0)) + } else + Matrix->MarkowitzProd[Col] = MarkoRow[Col] * MarkoCol[Col]; + if ((MarkoCol[Col] == 0) AND(MarkoRow[Col] != 0)) Matrix->Singletons++; } return; } - - - - - /* * ZERO PIVOT ENCOUNTERED * @@ -2986,40 +2692,24 @@ double Product; * Index of diagonal that is zero. */ -static int -MatrixIsSingular( - MatrixPtr Matrix, - int Step -) -{ -/* Begin `MatrixIsSingular'. */ +static int MatrixIsSingular(MatrixPtr Matrix, int Step) { + /* Begin `MatrixIsSingular'. */ - Matrix->SingularRow = Matrix->IntToExtRowMap[ Step ]; - Matrix->SingularCol = Matrix->IntToExtColMap[ Step ]; + Matrix->SingularRow = Matrix->IntToExtRowMap[Step]; + Matrix->SingularCol = Matrix->IntToExtColMap[Step]; return (Matrix->Error = spSINGULAR); } +static int ZeroPivot(MatrixPtr Matrix, int Step) { + /* Begin `ZeroPivot'. */ -static int -ZeroPivot( - MatrixPtr Matrix, - int Step -) -{ -/* Begin `ZeroPivot'. */ - - Matrix->SingularRow = Matrix->IntToExtRowMap[ Step ]; - Matrix->SingularCol = Matrix->IntToExtColMap[ Step ]; + Matrix->SingularRow = Matrix->IntToExtRowMap[Step]; + Matrix->SingularCol = Matrix->IntToExtColMap[Step]; return (Matrix->Error = spZERO_DIAG); } - - - - - #if (ANNOTATE == FULL) - + /* * * WRITE STATUS @@ -3027,24 +2717,27 @@ ZeroPivot( * Write a summary of important variables to standard output. */ -static void -WriteStatus( - MatrixPtr Matrix, - int Step -) -{ -int I; +static void WriteStatus(MatrixPtr Matrix, int Step) { + int I; -/* Begin `WriteStatus'. */ + /* Begin `WriteStatus'. */ printf("Step = %1d ", Step); printf("Pivot found at %1d,%1d using ", Matrix->PivotsOriginalRow, - Matrix->PivotsOriginalCol); - switch(Matrix->PivotSelectionMethod) - { case 's': printf("SearchForSingleton\n"); break; - case 'q': printf("QuicklySearchDiagonal\n"); break; - case 'd': printf("SearchDiagonal\n"); break; - case 'e': printf("SearchEntireMatrix\n"); break; + Matrix->PivotsOriginalCol); + switch (Matrix->PivotSelectionMethod) { + case 's': + printf("SearchForSingleton\n"); + break; + case 'q': + printf("QuicklySearchDiagonal\n"); + break; + case 'd': + printf("SearchDiagonal\n"); + break; + case 'e': + printf("SearchEntireMatrix\n"); + break; } printf("MarkowitzRow = "); @@ -3085,6 +2778,5 @@ int I; printf("\n\n"); return; - } #endif /* ANNOTATE == FULL */ diff --git a/src/maths/sparse/spOutput.c b/src/maths/sparse/spOutput.c index 7928fbe43..6ed33ee85 100644 --- a/src/maths/sparse/spOutput.c +++ b/src/maths/sparse/spOutput.c @@ -25,7 +25,6 @@ * >>> Other functions contained in this file: */ - /* * Revision and copyright information. * @@ -36,13 +35,10 @@ #ifdef notdef static char copyright[] = "Sparse1.4: Copyright (c) 1985-2003 by Kenneth S. Kundert"; -static char RCSid[] = - "$Header: /cvsroot/sparse/src/spOutput.c,v 1.3 2003/06/29 04:19:52 kundert Exp $"; +static char RCSid[] = "$Header: /cvsroot/sparse/src/spOutput.c,v 1.3 " + "2003/06/29 04:19:52 kundert Exp $"; #endif - - - /* * IMPORTS * @@ -56,17 +52,13 @@ static char RCSid[] = */ #define spINSIDE_SPARSE -#include -#include "spConfig.h" #include "ngspice/spmatrix.h" +#include "spConfig.h" #include "spDefs.h" - - - - +#include #if DOCUMENTATION - + /*! * Formats and send the matrix to standard output. Some elementary * statistics are also output. The matrix is output in a format that is @@ -134,66 +126,60 @@ static char RCSid[] = * The largest expected external row or column number. */ -void -spPrint( - MatrixPtr Matrix, - int PrintReordered, - int Data, - int Header -) -{ -int J = 0; -int I, Row, Col, Size, Top, StartCol = 1, StopCol, Columns, ElementCount = 0; -double Magnitude, SmallestDiag = 0.0, SmallestElement = 0.0; -double LargestElement = 0.0, LargestDiag = 0.0; -ElementPtr pElement; +void spPrint(MatrixPtr Matrix, int PrintReordered, int Data, int Header) { + int J = 0; + int I, Row, Col, Size, Top, StartCol = 1, StopCol, Columns, + ElementCount = 0; + double Magnitude, SmallestDiag = 0.0, SmallestElement = 0.0; + double LargestElement = 0.0, LargestDiag = 0.0; + ElementPtr pElement; #if spCOMPLEX -ElementPtr pImagElements[PRINTER_WIDTH/10+1]; + ElementPtr pImagElements[PRINTER_WIDTH / 10 + 1]; #endif -int *PrintOrdToIntRowMap, *PrintOrdToIntColMap; + int *PrintOrdToIntRowMap, *PrintOrdToIntColMap; -/* Begin `spPrint'. */ - ASSERT_IS_SPARSE( Matrix ); + /* Begin `spPrint'. */ + ASSERT_IS_SPARSE(Matrix); Size = Matrix->Size; /* Create a packed external to internal row and column translation array. */ -# if TRANSLATE +#if TRANSLATE Top = Matrix->AllocatedExtSize; #else Top = Matrix->AllocatedSize; #endif - SP_CALLOC( PrintOrdToIntRowMap, int, Top + 1 ); - SP_CALLOC( PrintOrdToIntColMap, int, Top + 1 ); - if ( PrintOrdToIntRowMap == NULL OR PrintOrdToIntColMap == NULL) - { Matrix->Error = spNO_MEMORY; + SP_CALLOC(PrintOrdToIntRowMap, int, Top + 1); + SP_CALLOC(PrintOrdToIntColMap, int, Top + 1); + if (PrintOrdToIntRowMap == NULL OR PrintOrdToIntColMap == NULL) { + Matrix->Error = spNO_MEMORY; SP_FREE(PrintOrdToIntColMap); SP_FREE(PrintOrdToIntRowMap); return; } - for (I = 1; I <= Size; I++) - { PrintOrdToIntRowMap[ Matrix->IntToExtRowMap[I] ] = I; - PrintOrdToIntColMap[ Matrix->IntToExtColMap[I] ] = I; + for (I = 1; I <= Size; I++) { + PrintOrdToIntRowMap[Matrix->IntToExtRowMap[I]] = I; + PrintOrdToIntColMap[Matrix->IntToExtColMap[I]] = I; } -/* Pack the arrays. */ - for (J = 1, I = 1; I <= Top; I++) - { if (PrintOrdToIntRowMap[I] != 0) - PrintOrdToIntRowMap[ J++ ] = PrintOrdToIntRowMap[ I ]; + /* Pack the arrays. */ + for (J = 1, I = 1; I <= Top; I++) { + if (PrintOrdToIntRowMap[I] != 0) + PrintOrdToIntRowMap[J++] = PrintOrdToIntRowMap[I]; } - for (J = 1, I = 1; I <= Top; I++) - { if (PrintOrdToIntColMap[I] != 0) - PrintOrdToIntColMap[ J++ ] = PrintOrdToIntColMap[ I ]; + for (J = 1, I = 1; I <= Top; I++) { + if (PrintOrdToIntColMap[I] != 0) + PrintOrdToIntColMap[J++] = PrintOrdToIntColMap[I]; } -/* Print header. */ - if (Header) - { printf("MATRIX SUMMARY\n\n"); + /* Print header. */ + if (Header) { + printf("MATRIX SUMMARY\n\n"); printf("Size of matrix = %1d x %1d.\n", Size, Size); - if ( Matrix->Reordered AND PrintReordered ) + if (Matrix->Reordered AND PrintReordered) printf("Matrix has been reordered.\n"); putchar('\n'); - if ( Matrix->Factored ) + if (Matrix->Factored) printf("Matrix after factorization:\n"); else printf("Matrix before factorization:\n"); @@ -201,73 +187,78 @@ int *PrintOrdToIntRowMap, *PrintOrdToIntColMap; SmallestElement = LARGEST_REAL; SmallestDiag = SmallestElement; } - if (Size == 0) return; + if (Size == 0) + return; -/* Determine how many columns to use. */ + /* Determine how many columns to use. */ Columns = PRINTER_WIDTH; - if (Header) Columns -= 5; - if (Data) Columns = (Columns+1) / 10; + if (Header) + Columns -= 5; + if (Data) + Columns = (Columns + 1) / 10; -/* - * Print matrix by printing groups of complete columns until all the columns - * are printed. - */ + /* + * Print matrix by printing groups of complete columns until all the columns + * are printed. + */ J = 0; - while ( J <= Size ) + while (J <= Size) -/* Calculate index of last column to printed in this group. */ - { StopCol = StartCol + Columns - 1; + /* Calculate index of last column to printed in this group. */ + { + StopCol = StartCol + Columns - 1; if (StopCol > Size) StopCol = Size; -/* Label the columns. */ - if (Header) - { if (Data) - { printf(" "); - for (I = StartCol; I <= StopCol; I++) - { if (PrintReordered) + /* Label the columns. */ + if (Header) { + if (Data) { + printf(" "); + for (I = StartCol; I <= StopCol; I++) { + if (PrintReordered) Col = I; else Col = PrintOrdToIntColMap[I]; - printf(" %9d", Matrix->IntToExtColMap[ Col ]); + printf(" %9d", Matrix->IntToExtColMap[Col]); } printf("\n\n"); - } - else - { if (PrintReordered) - printf("Columns %1d to %1d.\n",StartCol,StopCol); - else - { printf("Columns %1d to %1d.\n", - Matrix->IntToExtColMap[ PrintOrdToIntColMap[StartCol] ], - Matrix->IntToExtColMap[ PrintOrdToIntColMap[StopCol] ]); + } else { + if (PrintReordered) + printf("Columns %1d to %1d.\n", StartCol, StopCol); + else { + printf( + "Columns %1d to %1d.\n", + Matrix->IntToExtColMap[PrintOrdToIntColMap[StartCol]], + Matrix->IntToExtColMap[PrintOrdToIntColMap[StopCol]]); } } } -/* Print every row ... */ - for (I = 1; I <= Size; I++) - { if (PrintReordered) + /* Print every row ... */ + for (I = 1; I <= Size; I++) { + if (PrintReordered) Row = I; else Row = PrintOrdToIntRowMap[I]; - if (Header) - { if (PrintReordered AND NOT Data) + if (Header) { + if (PrintReordered AND NOT Data) printf("%4d", I); else - printf("%4d", Matrix->IntToExtRowMap[ Row ]); - if (NOT Data) putchar(' '); + printf("%4d", Matrix->IntToExtRowMap[Row]); + if (NOT Data) + putchar(' '); } -/* ... in each column of the group. */ - for (J = StartCol; J <= StopCol; J++) - { if (PrintReordered) + /* ... in each column of the group. */ + for (J = StartCol; J <= StopCol; J++) { + if (PrintReordered) Col = J; else Col = PrintOrdToIntColMap[J]; pElement = Matrix->FirstInCol[Col]; - while(pElement != NULL AND pElement->Row != Row) + while (pElement != NULL AND pElement->Row != Row) pElement = pElement->NextInCol; #if spCOMPLEX @@ -277,23 +268,24 @@ int *PrintOrdToIntRowMap, *PrintOrdToIntColMap; if (pElement != NULL) -/* Case where element exists */ - { if (Data) + /* Case where element exists */ + { + if (Data) printf(" %9.3g", (double)pElement->Real); else putchar('x'); -/* Update status variables */ - if ( (Magnitude = ELEMENT_MAG(pElement)) > LargestElement ) + /* Update status variables */ + if ((Magnitude = ELEMENT_MAG(pElement)) > LargestElement) LargestElement = Magnitude; - if ((Magnitude < SmallestElement) AND (Magnitude != 0.0)) + if ((Magnitude < SmallestElement) AND(Magnitude != 0.0)) SmallestElement = Magnitude; ElementCount++; } -/* Case where element is structurally zero */ - else - { if (Data) + /* Case where element is structurally zero */ + else { + if (Data) printf(" ..."); else putchar('.'); @@ -302,52 +294,53 @@ int *PrintOrdToIntRowMap, *PrintOrdToIntColMap; putchar('\n'); #if spCOMPLEX - if (Matrix->Complex AND Data) - { if (Header) - printf(" "); - for (J = StartCol; J <= StopCol; J++) - { if (pImagElements[J - StartCol] != NULL) - { printf(" %8.2gj", - (double)pImagElements[J-StartCol]->Imag); - } - else printf(" "); + if (Matrix->Complex AND Data) { + if (Header) + printf(" "); + for (J = StartCol; J <= StopCol; J++) { + if (pImagElements[J - StartCol] != NULL) { + printf(" %8.2gj", + (double)pImagElements[J - StartCol]->Imag); + } else + printf(" "); } putchar('\n'); } #endif /* spCOMPLEX */ } -/* Calculate index of first column in next group. */ + /* Calculate index of first column in next group. */ StartCol = StopCol; StartCol++; putchar('\n'); } - if (Header) - { printf("\nLargest element in matrix = %-1.4g.\n", LargestElement); + if (Header) { + printf("\nLargest element in matrix = %-1.4g.\n", LargestElement); printf("Smallest element in matrix = %-1.4g.\n", SmallestElement); -/* Search for largest and smallest diagonal values */ - for (I = 1; I <= Size; I++) - { if (Matrix->Diag[I] != NULL) - { Magnitude = ELEMENT_MAG( Matrix->Diag[I] ); - if ( Magnitude > LargestDiag ) LargestDiag = Magnitude; - if ( Magnitude < SmallestDiag ) SmallestDiag = Magnitude; + /* Search for largest and smallest diagonal values */ + for (I = 1; I <= Size; I++) { + if (Matrix->Diag[I] != NULL) { + Magnitude = ELEMENT_MAG(Matrix->Diag[I]); + if (Magnitude > LargestDiag) + LargestDiag = Magnitude; + if (Magnitude < SmallestDiag) + SmallestDiag = Magnitude; } } - /* Print the largest and smallest diagonal values */ - if ( Matrix->Factored ) - { printf("\nLargest diagonal element = %-1.4g.\n", LargestDiag); + /* Print the largest and smallest diagonal values */ + if (Matrix->Factored) { + printf("\nLargest diagonal element = %-1.4g.\n", LargestDiag); printf("Smallest diagonal element = %-1.4g.\n", SmallestDiag); - } - else - { printf("\nLargest pivot element = %-1.4g.\n", LargestDiag); + } else { + printf("\nLargest pivot element = %-1.4g.\n", LargestDiag); printf("Smallest pivot element = %-1.4g.\n", SmallestDiag); } - /* Calculate and print sparsity and number of fill-ins created. */ - printf("\nDensity = %2.2f%%.\n", ((double)ElementCount * 100.0) - / (((double)Size * (double)Size))); + /* Calculate and print sparsity and number of fill-ins created. */ + printf("\nDensity = %2.2f%%.\n", ((double)ElementCount * 100.0) / + (((double)Size * (double)Size))); if (NOT Matrix->NeedsOrdering) printf("Number of fill-ins = %1d.\n", Matrix->Fillins); } @@ -359,16 +352,6 @@ int *PrintOrdToIntRowMap, *PrintOrdToIntColMap; return; } - - - - - - - - - - /*! * Writes matrix to file in format suitable to be read back in by the * matrix test program. @@ -408,128 +391,116 @@ int *PrintOrdToIntRowMap, *PrintOrdToIntColMap; * The size of the matrix. */ -int -spFileMatrix( - MatrixPtr Matrix, - char *File, - char *Label, - int Reordered, - int Data, - int Header -) -{ -int I, Size; -ElementPtr pElement; -int Row, Col, Err; -FILE *pMatrixFile; +int spFileMatrix(MatrixPtr Matrix, char *File, char *Label, int Reordered, + int Data, int Header) { + int I, Size; + ElementPtr pElement; + int Row, Col, Err; + FILE *pMatrixFile; -/* Begin `spFileMatrix'. */ - ASSERT_IS_SPARSE( Matrix ); + /* Begin `spFileMatrix'. */ + ASSERT_IS_SPARSE(Matrix); -/* Open file matrix file in write mode. */ + /* Open file matrix file in write mode. */ if ((pMatrixFile = fopen(File, "w")) == NULL) return 0; -/* Output header. */ + /* Output header. */ Size = Matrix->Size; - if (Header) - { if (Matrix->Factored AND Data) - { Err = fprintf - ( pMatrixFile, - "Warning : The following matrix is factored in to LU form.\n" - ); - if (Err < 0) return 0; + if (Header) { + if (Matrix->Factored AND Data) { + Err = fprintf( + pMatrixFile, + "Warning : The following matrix is factored in to LU form.\n"); + if (Err < 0) + return 0; } - if (fprintf(pMatrixFile, "%s\n", Label) < 0) return 0; - Err = fprintf( pMatrixFile, "%d\t%s\n", Size, - (Matrix->Complex ? "complex" : "real")); - if (Err < 0) return 0; + if (fprintf(pMatrixFile, "%s\n", Label) < 0) + return 0; + Err = fprintf(pMatrixFile, "%d\t%s\n", Size, + (Matrix->Complex ? "complex" : "real")); + if (Err < 0) + return 0; } - if (Size == 0) return 1; + if (Size == 0) + return 1; -/* Output matrix. */ - if (NOT Data) - { for (I = 1; I <= Size; I++) - { pElement = Matrix->FirstInCol[I]; - while (pElement != NULL) - { if (Reordered) - { Row = pElement->Row; + /* Output matrix. */ + if (NOT Data) { + for (I = 1; I <= Size; I++) { + pElement = Matrix->FirstInCol[I]; + while (pElement != NULL) { + if (Reordered) { + Row = pElement->Row; Col = I; - } - else - { Row = Matrix->IntToExtRowMap[pElement->Row]; + } else { + Row = Matrix->IntToExtRowMap[pElement->Row]; Col = Matrix->IntToExtColMap[I]; } pElement = pElement->NextInCol; - if (fprintf(pMatrixFile, "%d\t%d\n", Row, Col) < 0) return 0; + if (fprintf(pMatrixFile, "%d\t%d\n", Row, Col) < 0) + return 0; } } -/* Output terminator, a line of zeros. */ + /* Output terminator, a line of zeros. */ if (Header) - if (fprintf(pMatrixFile, "0\t0\n") < 0) return 0; + if (fprintf(pMatrixFile, "0\t0\n") < 0) + return 0; } #if spCOMPLEX - if (Data AND Matrix->Complex) - { for (I = 1; I <= Size; I++) - { pElement = Matrix->FirstInCol[I]; - while (pElement != NULL) - { if (Reordered) - { Row = pElement->Row; + if (Data AND Matrix->Complex) { + for (I = 1; I <= Size; I++) { + pElement = Matrix->FirstInCol[I]; + while (pElement != NULL) { + if (Reordered) { + Row = pElement->Row; Col = I; - } - else - { Row = Matrix->IntToExtRowMap[pElement->Row]; + } else { + Row = Matrix->IntToExtRowMap[pElement->Row]; Col = Matrix->IntToExtColMap[I]; } - Err = fprintf - ( pMatrixFile,"%d\t%d\t%-.15g\t%-.15g\n", - Row, Col, (double)pElement->Real, (double)pElement->Imag - ); - if (Err < 0) return 0; + Err = fprintf(pMatrixFile, "%d\t%d\t%-.15g\t%-.15g\n", Row, Col, + (double)pElement->Real, (double)pElement->Imag); + if (Err < 0) + return 0; pElement = pElement->NextInCol; } } -/* Output terminator, a line of zeros. */ + /* Output terminator, a line of zeros. */ if (Header) - if (fprintf(pMatrixFile,"0\t0\t0.0\t0.0\n") < 0) return 0; - + if (fprintf(pMatrixFile, "0\t0\t0.0\t0.0\n") < 0) + return 0; } #endif /* spCOMPLEX */ #if REAL - if (Data AND NOT Matrix->Complex) - { for (I = 1; I <= Size; I++) - { pElement = Matrix->FirstInCol[I]; - while (pElement != NULL) - { Row = Matrix->IntToExtRowMap[pElement->Row]; + if (Data AND NOT Matrix->Complex) { + for (I = 1; I <= Size; I++) { + pElement = Matrix->FirstInCol[I]; + while (pElement != NULL) { + Row = Matrix->IntToExtRowMap[pElement->Row]; Col = Matrix->IntToExtColMap[I]; - Err = fprintf - ( pMatrixFile,"%d\t%d\t%-.15g\n", - Row, Col, (double)pElement->Real - ); - if (Err < 0) return 0; + Err = fprintf(pMatrixFile, "%d\t%d\t%-.15g\n", Row, Col, + (double)pElement->Real); + if (Err < 0) + return 0; pElement = pElement->NextInCol; } } -/* Output terminator, a line of zeros. */ + /* Output terminator, a line of zeros. */ if (Header) - if (fprintf(pMatrixFile,"0\t0\t0.0\n") < 0) return 0; - + if (fprintf(pMatrixFile, "0\t0\t0.0\n") < 0) + return 0; } #endif /* REAL */ -/* Close file. */ - if (fclose(pMatrixFile) < 0) return 0; + /* Close file. */ + if (fclose(pMatrixFile) < 0) + return 0; return 1; } - - - - - - /*! * Writes vector to file in format suitable to be read back in by the * matrix test program. This routine should be executed after the function @@ -561,29 +532,25 @@ FILE *pMatrixFile; * The size of the matrix. */ -int -spFileVector( - MatrixPtr Matrix, - char *File, - spREAL RHS[] +int spFileVector(MatrixPtr Matrix, char *File, spREAL RHS[] #if spCOMPLEX AND spSEPARATED_COMPLEX_VECTORS - , spREAL iRHS[] + , + spREAL iRHS[] #endif -) -{ -int I, Size; +) { + int I, Size; #if spCOMPLEX -int Err; + int Err; #endif -FILE *pMatrixFile; + FILE *pMatrixFile; -/* Begin `spFileVector'. */ - ASSERT_IS_SPARSE( Matrix ); - vASSERT( RHS != NULL, "Vector missing" ); + /* Begin `spFileVector'. */ + ASSERT_IS_SPARSE(Matrix); + vASSERT(RHS != NULL, "Vector missing"); if (File) { /* Open File in write mode. */ - pMatrixFile = fopen(File,"w"); + pMatrixFile = fopen(File, "w"); if (pMatrixFile == NULL) return 0; } @@ -591,44 +558,39 @@ FILE *pMatrixFile; /* Correct array pointers for ARRAY_OFFSET. */ #if NOT ARRAY_OFFSET #if spCOMPLEX - if (Matrix->Complex) - { + if (Matrix->Complex) { #if spSEPARATED_COMPLEX_VECTORS - vASSERT( iRHS != NULL, "Imaginary vector missing" ); + vASSERT(iRHS != NULL, "Imaginary vector missing"); --RHS; --iRHS; #else RHS -= 2; #endif - } - else + } else #endif /* spCOMPLEX */ --RHS; #endif /* NOT ARRAY_OFFSET */ - -/* Output vector. */ + /* Output vector. */ Size = Matrix->Size; - if (Size == 0) return 1; + if (Size == 0) + return 1; #if spCOMPLEX - if (Matrix->Complex) - { + if (Matrix->Complex) { #if spSEPARATED_COMPLEX_VECTORS - for (I = 1; I <= Size; I++) - { Err = fprintf - ( pMatrixFile, "%-.15g\t%-.15g\n", - (double)RHS[I], (double)iRHS[I] - ); - if (Err < 0) return 0; + for (I = 1; I <= Size; I++) { + Err = fprintf(pMatrixFile, "%-.15g\t%-.15g\n", (double)RHS[I], + (double)iRHS[I]); + if (Err < 0) + return 0; } #else - for (I = 1; I <= Size; I++) - { Err = fprintf - ( pMatrixFile, "%-.15g\t%-.15g\n", - (double)RHS[2*I], (double)RHS[2*I+1] - ); - if (Err < 0) return 0; + for (I = 1; I <= Size; I++) { + Err = fprintf(pMatrixFile, "%-.15g\t%-.15g\n", (double)RHS[2 * I], + (double)RHS[2 * I + 1]); + if (Err < 0) + return 0; } #endif } @@ -637,26 +599,20 @@ FILE *pMatrixFile; else #endif #if REAL - { for (I = 1; I <= Size; I++) - { if (fprintf(pMatrixFile, "%-.15g\n", (double)RHS[I]) < 0) + { + for (I = 1; I <= Size; I++) { + if (fprintf(pMatrixFile, "%-.15g\n", (double)RHS[I]) < 0) return 0; } } #endif /* REAL */ -/* Close file. */ - if (fclose(pMatrixFile) < 0) return 0; + /* Close file. */ + if (fclose(pMatrixFile) < 0) + return 0; return 1; } - - - - - - - - /*! * Writes useful information concerning the matrix to a file. Should be * executed after the matrix is factored. @@ -690,27 +646,21 @@ FILE *pMatrixFile; * The smallest element in the matrix excluding zero elements. */ -int -spFileStats( - MatrixPtr Matrix, - char *File, - char *Label -) -{ -int Size, I; -ElementPtr pElement; -int NumberOfElements; -RealNumber Data, LargestElement, SmallestElement; -FILE *pStatsFile; +int spFileStats(MatrixPtr Matrix, char *File, char *Label) { + int Size, I; + ElementPtr pElement; + int NumberOfElements; + RealNumber Data, LargestElement, SmallestElement; + FILE *pStatsFile; -/* Begin `spFileStats'. */ - ASSERT_IS_SPARSE( Matrix ); + /* Begin `spFileStats'. */ + ASSERT_IS_SPARSE(Matrix); -/* Open File in append mode. */ + /* Open File in append mode. */ if ((pStatsFile = fopen(File, "a")) == NULL) return 0; -/* Output statistics. */ + /* Output statistics. */ Size = Matrix->Size; if (NOT Matrix->Factored) fprintf(pStatsFile, "Matrix has not been factored.\n"); @@ -720,18 +670,19 @@ FILE *pStatsFile; fprintf(pStatsFile, "Matrix is complex.\n"); else fprintf(pStatsFile, "Matrix is real.\n"); - fprintf(pStatsFile," Size = %d\n",Size); - if (Size == 0) return 1; + fprintf(pStatsFile, " Size = %d\n", Size); + if (Size == 0) + return 1; -/* Search matrix. */ + /* Search matrix. */ NumberOfElements = 0; LargestElement = 0.0; SmallestElement = LARGEST_REAL; - for (I = 1; I <= Size; I++) - { pElement = Matrix->FirstInCol[I]; - while (pElement != NULL) - { NumberOfElements++; + for (I = 1; I <= Size; I++) { + pElement = Matrix->FirstInCol[I]; + while (pElement != NULL) { + NumberOfElements++; Data = ELEMENT_MAG(pElement); if (Data > LargestElement) LargestElement = Data; @@ -741,29 +692,29 @@ FILE *pStatsFile; } } - SmallestElement = MIN( SmallestElement, LargestElement ); + SmallestElement = MIN(SmallestElement, LargestElement); -/* Output remaining statistics. */ + /* Output remaining statistics. */ fprintf(pStatsFile, " Initial number of elements = %d\n", NumberOfElements - Matrix->Fillins); fprintf(pStatsFile, " Initial average number of elements per row = %f\n", (double)(NumberOfElements - Matrix->Fillins) / (double)Size); - fprintf(pStatsFile, " Fill-ins = %d\n",Matrix->Fillins); + fprintf(pStatsFile, " Fill-ins = %d\n", Matrix->Fillins); fprintf(pStatsFile, " Average number of fill-ins per row = %f%%\n", (double)Matrix->Fillins / (double)Size); fprintf(pStatsFile, " Total number of elements = %d\n", NumberOfElements); fprintf(pStatsFile, " Average number of elements per row = %f\n", (double)NumberOfElements / (double)Size); - fprintf(pStatsFile," Density = %f%%\n", - (100.0*(double)NumberOfElements)/((double)Size*(double)Size)); - fprintf(pStatsFile," Relative Threshold = %e\n", Matrix->RelThreshold); - fprintf(pStatsFile," Absolute Threshold = %e\n", Matrix->AbsThreshold); - fprintf(pStatsFile," Largest Element = %e\n", LargestElement); - fprintf(pStatsFile," Smallest Element = %e\n\n\n", SmallestElement); + fprintf(pStatsFile, " Density = %f%%\n", + (100.0 * (double)NumberOfElements) / ((double)Size * (double)Size)); + fprintf(pStatsFile, " Relative Threshold = %e\n", Matrix->RelThreshold); + fprintf(pStatsFile, " Absolute Threshold = %e\n", Matrix->AbsThreshold); + fprintf(pStatsFile, " Largest Element = %e\n", LargestElement); + fprintf(pStatsFile, " Smallest Element = %e\n\n\n", SmallestElement); -/* Close file. */ + /* Close file. */ (void)fclose(pStatsFile); return 1; } diff --git a/src/maths/sparse/spSMP.c b/src/maths/sparse/spSMP.c index 743f86c49..982eb4050 100644 --- a/src/maths/sparse/spSMP.c +++ b/src/maths/sparse/spSMP.c @@ -34,7 +34,7 @@ * SMPcProdDiag * LoadGmin * SMPfindElt -*/ + */ /* * To replace SMP with Sparse, rename the file spSpice3.h to @@ -80,17 +80,14 @@ * * Copyright (c) 1985-2003 by Kenneth S. Kundert */ - + #ifdef notdef static char copyright[] = "Sparse1.4: Copyright (c) 1985-2003 by Kenneth S. Kundert"; -static char RCSid[] = - "@(#)$Header: /cvsroot/sparse/src/spSMP.c,v 1.3 2003/06/30 19:40:51 kundert Exp $"; +static char RCSid[] = "@(#)$Header: /cvsroot/sparse/src/spSMP.c,v 1.3 " + "2003/06/30 19:40:51 kundert Exp $"; #endif - - - /* * IMPORTS * @@ -101,229 +98,164 @@ static char RCSid[] = * Spice3's matrix macro definitions. */ -#include "ngspice/spmatrix.h" #include "ngspice/smpdefs.h" +#include "ngspice/spmatrix.h" #include "spDefs.h" -#define NO 0 -#define YES 1 +#define NO 0 +#define YES 1 -#define NG_IGNORE(x) (void)x +#define NG_IGNORE(x) (void)x static void LoadGmin(MatrixPtr Matrix, double Gmin); /* * SMPaddElt() */ -int -SMPaddElt( -SMPmatrix *Matrix, -int Row, int Col, -double Value) -{ - *spGetElement( Matrix, Row, Col ) = Value; - return spError( Matrix ); +int SMPaddElt(SMPmatrix *Matrix, int Row, int Col, double Value) { + *spGetElement(Matrix, Row, Col) = Value; + return spError(Matrix); } /* * SMPmakeElt() */ -double * -SMPmakeElt( -SMPmatrix *Matrix, -int Row, int Col) -{ - return spGetElement( Matrix, Row, Col ); +double *SMPmakeElt(SMPmatrix *Matrix, int Row, int Col) { + return spGetElement(Matrix, Row, Col); } /* * SMPcClear() */ -void -SMPcClear( -SMPmatrix *Matrix) -{ - spClear( Matrix ); -} +void SMPcClear(SMPmatrix *Matrix) { spClear(Matrix); } /* * SMPclear() */ -void -SMPclear( -SMPmatrix *Matrix) -{ - spClear( Matrix ); -} +void SMPclear(SMPmatrix *Matrix) { spClear(Matrix); } /* * SMPcLUfac() */ /*ARGSUSED*/ -int -SMPcLUfac( -SMPmatrix *Matrix, -double PivTol) -{ +int SMPcLUfac(SMPmatrix *Matrix, double PivTol) { NG_IGNORE(PivTol); - spSetComplex( Matrix ); - return spFactor( Matrix ); + spSetComplex(Matrix); + return spFactor(Matrix); } /* * SMPluFac() */ /*ARGSUSED*/ -int -SMPluFac( -SMPmatrix *Matrix, -double PivTol, double Gmin) -{ +int SMPluFac(SMPmatrix *Matrix, double PivTol, double Gmin) { NG_IGNORE(PivTol); - spSetReal( Matrix ); - LoadGmin( Matrix, Gmin ); - return spFactor( Matrix ); + spSetReal(Matrix); + LoadGmin(Matrix, Gmin); + return spFactor(Matrix); } /* * SMPcReorder() */ -int -SMPcReorder( -SMPmatrix *Matrix, -double PivTol, double PivRel, -int *NumSwaps) -{ +int SMPcReorder(SMPmatrix *Matrix, double PivTol, double PivRel, + int *NumSwaps) { *NumSwaps = 1; - spSetComplex( Matrix ); - return spOrderAndFactor( Matrix, NULL, - PivRel, PivTol, YES ); + spSetComplex(Matrix); + return spOrderAndFactor(Matrix, NULL, PivRel, PivTol, YES); } /* * SMPreorder() */ -int -SMPreorder( -SMPmatrix *Matrix, -double PivTol, double PivRel, double Gmin) -{ - spSetReal( Matrix ); - LoadGmin( Matrix, Gmin ); - return spOrderAndFactor( Matrix, NULL, - PivRel, PivTol, YES ); +int SMPreorder(SMPmatrix *Matrix, double PivTol, double PivRel, double Gmin) { + spSetReal(Matrix); + LoadGmin(Matrix, Gmin); + return spOrderAndFactor(Matrix, NULL, PivRel, PivTol, YES); } /* * SMPcaSolve() */ -void -SMPcaSolve( -SMPmatrix *Matrix, -double RHS[], double iRHS[], double Spare[], double iSpare[]) -{ +void SMPcaSolve(SMPmatrix *Matrix, double RHS[], double iRHS[], double Spare[], + double iSpare[]) { NG_IGNORE(Spare); NG_IGNORE(iSpare); #if spCOMPLEX - spSolveTransposed( Matrix, RHS, RHS, iRHS, iRHS ); + spSolveTransposed(Matrix, RHS, RHS, iRHS, iRHS); #else - spSolveTransposed( Matrix, RHS, RHS ); + spSolveTransposed(Matrix, RHS, RHS); #endif } /* * SMPcSolve() */ -void -SMPcSolve( -SMPmatrix *Matrix, -double RHS[], double iRHS[], double Spare[], double iSpare[]) -{ +void SMPcSolve(SMPmatrix *Matrix, double RHS[], double iRHS[], double Spare[], + double iSpare[]) { NG_IGNORE(Spare); NG_IGNORE(iSpare); #if spCOMPLEX - spSolve( Matrix, RHS, RHS, iRHS, iRHS ); + spSolve(Matrix, RHS, RHS, iRHS, iRHS); #else - spSolve( Matrix, RHS, RHS ); + spSolve(Matrix, RHS, RHS); #endif } /* * SMPsolve() */ -void -SMPsolve( -SMPmatrix *Matrix, -double RHS[], double Spare[]) -{ +void SMPsolve(SMPmatrix *Matrix, double RHS[], double Spare[]) { NG_IGNORE(Spare); #if spCOMPLEX - spSolve( Matrix, RHS, RHS, NULL, NULL ); + spSolve(Matrix, RHS, RHS, NULL, NULL); #else - spSolve( Matrix, RHS, RHS ); + spSolve(Matrix, RHS, RHS); #endif } /* * SMPmatSize() */ -int -SMPmatSize( -SMPmatrix *Matrix) -{ - return spGetSize( Matrix, 1 ); -} +int SMPmatSize(SMPmatrix *Matrix) { return spGetSize(Matrix, 1); } /* * SMPnewMatrix() */ -int -SMPnewMatrix( -SMPmatrix **pMatrix, -int size) -{ +int SMPnewMatrix(SMPmatrix **pMatrix, int size) { int Error; - *pMatrix = (SMPmatrix *)spCreate( size, 1, &Error ); + *pMatrix = (SMPmatrix *)spCreate(size, 1, &Error); return Error; } /* * SMPdestroy() */ -void -SMPdestroy( -SMPmatrix *Matrix) -{ - spDestroy( Matrix ); -} +void SMPdestroy(SMPmatrix *Matrix) { spDestroy(Matrix); } /* * SMPpreOrder() */ -int -SMPpreOrder( -SMPmatrix *Matrix) -{ - spMNA_Preorder( Matrix ); - return spError( Matrix ); +int SMPpreOrder(SMPmatrix *Matrix) { + spMNA_Preorder(Matrix); + return spError(Matrix); } /* * SMPprintRHS() */ -void -SMPprintRHS(SMPmatrix *Matrix, char *Filename, RealVector RHS, RealVector iRHS) -{ +void SMPprintRHS(SMPmatrix *Matrix, char *Filename, RealVector RHS, + RealVector iRHS) { #if spCOMPLEX - spFileVector( Matrix, Filename, RHS, iRHS ); + spFileVector(Matrix, Filename, RHS, iRHS); #else - spFileVector( Matrix, Filename, RHS ); + spFileVector(Matrix, Filename, RHS); #endif } @@ -331,66 +263,50 @@ SMPprintRHS(SMPmatrix *Matrix, char *Filename, RealVector RHS, RealVector iRHS) * SMPprint() */ /*ARGSUSED*/ -void -SMPprint( -SMPmatrix *Matrix, -char *Filename) -{ +void SMPprint(SMPmatrix *Matrix, char *Filename) { if (Filename) - spFileMatrix(Matrix, Filename, "Circuit Matrix", 0, 1, 1 ); + spFileMatrix(Matrix, Filename, "Circuit Matrix", 0, 1, 1); else - spPrint( Matrix, 0, 1, 1 ); + spPrint(Matrix, 0, 1, 1); } /* * SMPgetError() */ -void -SMPgetError( -SMPmatrix *Matrix, -int *Row, int *Col) -{ - spWhereSingular( Matrix, Row, Col ); +void SMPgetError(SMPmatrix *Matrix, int *Row, int *Col) { + spWhereSingular(Matrix, Row, Col); } /* * SMPcProdDiag() */ -int -SMPcProdDiag( -SMPmatrix *Matrix, -SPcomplex *pMantissa, -int *pExponent) -{ +int SMPcProdDiag(SMPmatrix *Matrix, SPcomplex *pMantissa, int *pExponent) { #if spCOMPLEX - spDeterminant( Matrix, pExponent, &(pMantissa->real), - &(pMantissa->imag) ); + spDeterminant(Matrix, pExponent, &(pMantissa->real), &(pMantissa->imag)); #else - spDeterminant( Matrix, pExponent, &(pMantissa->real) ); + spDeterminant(Matrix, pExponent, &(pMantissa->real)); #endif - return spError( Matrix ); + return spError(Matrix); } /* * SMPcDProd() */ -int -SMPcDProd(SMPmatrix *Matrix, SPcomplex *pMantissa, int *pExponent) -{ - double re, im, x, y, z; - int p; +int SMPcDProd(SMPmatrix *Matrix, SPcomplex *pMantissa, int *pExponent) { + double re, im, x, y, z; + int p; #if spCOMPLEX - spDeterminant( Matrix, &p, &re, &im); + spDeterminant(Matrix, &p, &re, &im); #else - spDeterminant( Matrix, &p, &re ); + spDeterminant(Matrix, &p, &re); im = 0.0; #endif #ifndef M_LN2 -#define M_LN2 0.69314718055994530942 +#define M_LN2 0.69314718055994530942 #endif #ifndef M_LN10 -#define M_LN10 2.30258509299404568402 +#define M_LN10 2.30258509299404568402 #endif #ifdef debug_print @@ -399,7 +315,7 @@ SMPcDProd(SMPmatrix *Matrix, SPcomplex *pMantissa, int *pExponent) /* Convert base 10 numbers to base 2 numbers, for comparison */ y = p * M_LN10 / M_LN2; - x = (int) y; + x = (int)y; y -= x; /* ASSERT @@ -419,45 +335,44 @@ SMPcDProd(SMPmatrix *Matrix, SPcomplex *pMantissa, int *pExponent) /* Re-normalize (re or im may be > 2.0 or both < 1.0 */ if (re != 0.0) { - y = logb(re); - if (im != 0.0) - z = logb(im); - else - z = 0; + y = logb(re); + if (im != 0.0) + z = logb(im); + else + z = 0; } else if (im != 0.0) { - z = logb(im); - y = 0; + z = logb(im); + y = 0; } else { - /* Singular */ - /*printf("10 -> singular\n");*/ - y = 0; - z = 0; + /* Singular */ + /*printf("10 -> singular\n");*/ + y = 0; + z = 0; } #ifdef debug_print printf(" ** renormalize changes = %g,%g\n", y, z); #endif if (y < z) - y = z; + y = z; #ifdef debug_print *pExponent = (int)(x + y); - x = scalbn(re, (int) -y); - z = scalbn(im, (int) -y); - printf(" ** values are: re %g, im %g, y %g, re' %g, im' %g\n", - re, im, y, x, z); + x = scalbn(re, (int)-y); + z = scalbn(im, (int)-y); + printf(" ** values are: re %g, im %g, y %g, re' %g, im' %g\n", re, im, y, x, + z); #endif - pMantissa->real = scalbn(re, (int) -y); - pMantissa->imag = scalbn(im, (int) -y); + pMantissa->real = scalbn(re, (int)-y); + pMantissa->imag = scalbn(im, (int)-y); #ifdef debug_print printf("Determinant 10->2: (%20g,%20g)^%d\n", pMantissa->real, - pMantissa->imag, *pExponent); + pMantissa->imag, *pExponent); #endif - return spError( Matrix ); + return spError(Matrix); } - /* * LOAD GMIN * @@ -468,17 +383,13 @@ SMPcDProd(SMPmatrix *Matrix, SPcomplex *pMantissa, int *pExponent) * use of this routine is not recommended. It is included here simply * for compatibility with Spice3. */ -static void -LoadGmin( -MatrixPtr Matrix, -double Gmin) -{ -int I; -ArrayOfElementPtrs Diag; -ElementPtr diag; +static void LoadGmin(MatrixPtr Matrix, double Gmin) { + int I; + ArrayOfElementPtrs Diag; + ElementPtr diag; -/* Begin `spLoadGmin'. */ - ASSERT_IS_SPARSE( Matrix ); + /* Begin `spLoadGmin'. */ + ASSERT_IS_SPARSE(Matrix); if (Gmin != 0.0) { Diag = Matrix->Diag; @@ -490,9 +401,6 @@ ElementPtr diag; return; } - - - /* * FIND ELEMENT * @@ -502,18 +410,17 @@ ElementPtr diag; * pointer to the new element is returned. */ -SMPelement * -SMPfindElt(SMPmatrix *Matrix, int Row, int Col, int CreateIfMissing) -{ +SMPelement *SMPfindElt(SMPmatrix *Matrix, int Row, int Col, + int CreateIfMissing) { ElementPtr Element; /* Begin `SMPfindElt'. */ - ASSERT_IS_SPARSE( Matrix ); + ASSERT_IS_SPARSE(Matrix); Row = Matrix->ExtToIntRowMap[Row]; Col = Matrix->ExtToIntColMap[Col]; if (Col == -1) - /* No element available */ + /* No element available */ return NULL; Element = Matrix->FirstInCol[Col]; @@ -521,37 +428,30 @@ SMPfindElt(SMPmatrix *Matrix, int Row, int Col, int CreateIfMissing) return Element; } - /* * SMPcZeroCol() */ -int -SMPcZeroCol(SMPmatrix *Matrix, int Col) -{ - ElementPtr Element; +int SMPcZeroCol(SMPmatrix *Matrix, int Col) { + ElementPtr Element; Col = Matrix->ExtToIntColMap[Col]; - for (Element = Matrix->FirstInCol[Col]; - Element != NULL; - Element = Element->NextInCol) - { - Element->Real = 0.0; + for (Element = Matrix->FirstInCol[Col]; Element != NULL; + Element = Element->NextInCol) { + Element->Real = 0.0; #if spCOMPLEX - Element->Imag = 0.0; + Element->Imag = 0.0; #endif } - return spError( Matrix ); + return spError(Matrix); } /* * SMPcAddCol() */ -int -SMPcAddCol(SMPmatrix *Matrix, int Accum_Col, int Addend_Col) -{ - ElementPtr Accum, Addend, *Prev; +int SMPcAddCol(SMPmatrix *Matrix, int Accum_Col, int Addend_Col) { + ElementPtr Accum, Addend, *Prev; Accum_Col = Matrix->ExtToIntColMap[Accum_Col]; Addend_Col = Matrix->ExtToIntColMap[Addend_Col]; @@ -566,7 +466,8 @@ SMPcAddCol(SMPmatrix *Matrix, int Accum_Col, int Addend_Col) Accum = *Prev; } if (!Accum || Accum->Row > Addend->Row) { - Accum = spcCreateElement(Matrix, Addend->Row, Accum_Col, Prev, 0, NO); + Accum = + spcCreateElement(Matrix, Addend->Row, Accum_Col, Prev, 0, NO); } Accum->Real += Addend->Real; #if spCOMPLEX @@ -575,28 +476,24 @@ SMPcAddCol(SMPmatrix *Matrix, int Accum_Col, int Addend_Col) Addend = Addend->NextInCol; } - return spError( Matrix ); + return spError(Matrix); } /* * SMPconstMult() */ -void -SMPconstMult(SMPmatrix *Matrix, double constant) -{ +void SMPconstMult(SMPmatrix *Matrix, double constant) { spConstMult(Matrix, constant); } /* * SMPmultiply() */ -void -SMPmultiply(SMPmatrix *Matrix, double *RHS, double *Solution, double *iRHS, double *iSolution) -{ +void SMPmultiply(SMPmatrix *Matrix, double *RHS, double *Solution, double *iRHS, + double *iSolution) { #if spCOMPLEX spMultiply(Matrix, RHS, Solution, iRHS, iSolution); #else spMultiply(Matrix, RHS, Solution); #endif } - diff --git a/src/maths/sparse/spSolve.c b/src/maths/sparse/spSolve.c index bdf412294..c5a392e01 100644 --- a/src/maths/sparse/spSolve.c +++ b/src/maths/sparse/spSolve.c @@ -24,7 +24,6 @@ * SolveComplexTransposedMatrix */ - /* * Revision and copyright information. * @@ -35,12 +34,10 @@ #ifdef notdef static char copyright[] = "Sparse1.4: Copyright (c) 1985-2003 by Kenneth S. Kundert"; -static char RCSid[] = - "@(#)$Header: /cvsroot/sparse/src/spSolve.c,v 1.3 2003/06/29 04:19:52 kundert Exp $"; +static char RCSid[] = "@(#)$Header: /cvsroot/sparse/src/spSolve.c,v 1.3 " + "2003/06/29 04:19:52 kundert Exp $"; #endif - - /* * IMPORTS * @@ -54,13 +51,10 @@ static char RCSid[] = */ #define spINSIDE_SPARSE -#include -#include "spConfig.h" #include "ngspice/spmatrix.h" +#include "spConfig.h" #include "spDefs.h" - - - +#include /* * Function declarations @@ -68,23 +62,16 @@ static char RCSid[] = #if spSEPARATED_COMPLEX_VECTORS #if spCOMPLEX -static void SolveComplexMatrix( MatrixPtr, - RealVector, RealVector, RealVector, RealVector ); -static void SolveComplexTransposedMatrix( MatrixPtr, - RealVector, RealVector, RealVector, RealVector ); +static void SolveComplexMatrix(MatrixPtr, RealVector, RealVector, RealVector, + RealVector); +static void SolveComplexTransposedMatrix(MatrixPtr, RealVector, RealVector, + RealVector, RealVector); #endif #else -static void SolveComplexMatrix( MatrixPtr, RealVector, RealVector ); -static void SolveComplexTransposedMatrix( MatrixPtr, - RealVector, RealVector ); +static void SolveComplexMatrix(MatrixPtr, RealVector, RealVector); +static void SolveComplexTransposedMatrix(MatrixPtr, RealVector, RealVector); #endif - - - - - - /*! * Performs forward elimination and back substitution to find the * unknown vector from the \a RHS vector and factored matrix. This @@ -144,33 +131,28 @@ static void SolveComplexTransposedMatrix( MatrixPtr, /*VARARGS3*/ -void -spSolve( - MatrixPtr Matrix, - spREAL RHS[], - spREAL Solution[] -# if spCOMPLEX AND spSEPARATED_COMPLEX_VECTORS - , spREAL iRHS[] - , spREAL iSolution[] -# endif -) -{ +void spSolve(MatrixPtr Matrix, spREAL RHS[], spREAL Solution[] +#if spCOMPLEX AND spSEPARATED_COMPLEX_VECTORS + , + spREAL iRHS[], spREAL iSolution[] +#endif +) { #if REAL -ElementPtr pElement; -RealVector Intermediate; -RealNumber Temp; -int I, *pExtOrder, Size; -ElementPtr pPivot; + ElementPtr pElement; + RealVector Intermediate; + RealNumber Temp; + int I, *pExtOrder, Size; + ElementPtr pPivot; #endif -/* Begin `spSolve'. */ - ASSERT_IS_SPARSE( Matrix ); - ASSERT_NO_ERRORS( Matrix ); - ASSERT_IS_FACTORED( Matrix ); + /* 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 ); + if (Matrix->Complex) { + SolveComplexMatrix(Matrix, RHS, Solution IMAG_VECTORS); return; } #endif @@ -185,39 +167,39 @@ ElementPtr pPivot; --Solution; #endif -/* Initialize Intermediate vector. */ + /* 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]; + /* 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; + 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]; + /* 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]; + 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. */ + /* Unscramble Intermediate vector while placing data in to Solution vector. + */ pExtOrder = &Matrix->IntToExtColMap[Size]; for (I = Size; I > 0; I--) Solution[*(pExtOrder--)] = Intermediate[I]; @@ -226,16 +208,6 @@ ElementPtr pPivot; #endif /* REAL */ } - - - - - - - - - - #if spCOMPLEX /*! * Performs forward elimination and back substitution to find the @@ -289,27 +261,23 @@ ElementPtr pPivot; * Temporary storage for entries in arrays. */ -static void -SolveComplexMatrix( - MatrixPtr Matrix, - RealVector RHS, - RealVector Solution -# if spSEPARATED_COMPLEX_VECTORS - , RealVector iRHS - , RealVector iSolution -# endif -) -{ -ElementPtr pElement; -ComplexVector Intermediate; -int I, *pExtOrder, Size; -ElementPtr pPivot; -#if NOT spSEPARATED_COMPLEX_VECTORS -ComplexVector ExtVector; +static void SolveComplexMatrix(MatrixPtr Matrix, RealVector RHS, + RealVector Solution +#if spSEPARATED_COMPLEX_VECTORS + , + RealVector iRHS, RealVector iSolution #endif -ComplexNumber Temp; +) { + ElementPtr pElement; + ComplexVector Intermediate; + int I, *pExtOrder, Size; + ElementPtr pPivot; +#if NOT spSEPARATED_COMPLEX_VECTORS + ComplexVector ExtVector; +#endif + ComplexNumber Temp; -/* Begin `SolveComplexMatrix'. */ + /* Begin `SolveComplexMatrix'. */ Size = Matrix->Size; Intermediate = (ComplexVector)Matrix->Intermediate; @@ -317,19 +285,22 @@ ComplexNumber Temp; /* Correct array pointers for ARRAY_OFFSET. */ #if NOT ARRAY_OFFSET #if spSEPARATED_COMPLEX_VECTORS - --RHS; --iRHS; - --Solution; --iSolution; + --RHS; + --iRHS; + --Solution; + --iSolution; #else - RHS -= 2; Solution -= 2; + RHS -= 2; + Solution -= 2; #endif #endif -/* Initialize Intermediate vector. */ + /* Initialize Intermediate vector. */ pExtOrder = &Matrix->IntToExtRowMap[Size]; #if spSEPARATED_COMPLEX_VECTORS - for (I = Size; I > 0; I--) - { Intermediate[I].Real = RHS[*(pExtOrder)]; + for (I = Size; I > 0; I--) { + Intermediate[I].Real = RHS[*(pExtOrder)]; Intermediate[I].Imag = iRHS[*(pExtOrder--)]; } #else @@ -338,47 +309,47 @@ ComplexNumber Temp; Intermediate[I] = ExtVector[*(pExtOrder--)]; #endif -/* Forward substitution. Solves Lc = b.*/ - for (I = 1; I <= Size; I++) - { Temp = Intermediate[I]; + /* Forward substitution. Solves Lc = b.*/ + for (I = 1; I <= Size; I++) { + Temp = Intermediate[I]; -/* This step of the substitution is skipped if Temp equals zero. */ - if ((Temp.Real != 0.0) OR (Temp.Imag != 0.0)) - { pPivot = Matrix->Diag[I]; -/* Cmplx expr: Temp *= (1.0 / Pivot). */ + /* This step of the substitution is skipped if Temp equals zero. */ + if ((Temp.Real != 0.0) OR(Temp.Imag != 0.0)) { + pPivot = Matrix->Diag[I]; + /* Cmplx expr: Temp *= (1.0 / Pivot). */ CMPLX_MULT_ASSIGN(Temp, *pPivot); Intermediate[I] = Temp; pElement = pPivot->NextInCol; - while (pElement != NULL) - { -/* Cmplx expr: Intermediate[Element->Row] -= Temp * *Element. */ - CMPLX_MULT_SUBT_ASSIGN(Intermediate[pElement->Row], - Temp, *pElement); + while (pElement != NULL) { + /* Cmplx expr: Intermediate[Element->Row] -= Temp * *Element. */ + CMPLX_MULT_SUBT_ASSIGN(Intermediate[pElement->Row], Temp, + *pElement); pElement = pElement->NextInCol; } } } -/* Backward Substitution. Solves Ux = c.*/ - for (I = Size; I > 0; I--) - { Temp = Intermediate[I]; + /* Backward Substitution. Solves Ux = c.*/ + for (I = Size; I > 0; I--) { + Temp = Intermediate[I]; pElement = Matrix->Diag[I]->NextInRow; - while (pElement != NULL) - { -/* Cmplx expr: Temp -= *Element * Intermediate[Element->Col]. */ - CMPLX_MULT_SUBT_ASSIGN(Temp, *pElement,Intermediate[pElement->Col]); + while (pElement != NULL) { + /* Cmplx expr: Temp -= *Element * Intermediate[Element->Col]. */ + CMPLX_MULT_SUBT_ASSIGN(Temp, *pElement, + Intermediate[pElement->Col]); pElement = pElement->NextInRow; } Intermediate[I] = Temp; } -/* Unscramble Intermediate vector while placing data in to Solution vector. */ + /* Unscramble Intermediate vector while placing data in to Solution vector. + */ pExtOrder = &Matrix->IntToExtColMap[Size]; #if spSEPARATED_COMPLEX_VECTORS - for (I = Size; I > 0; I--) - { Solution[*(pExtOrder)] = Intermediate[I].Real; + for (I = Size; I > 0; I--) { + Solution[*(pExtOrder)] = Intermediate[I].Real; iSolution[*(pExtOrder--)] = Intermediate[I].Imag; } #else @@ -391,19 +362,6 @@ ComplexNumber Temp; } #endif /* spCOMPLEX */ - - - - - - - - - - - - - #if TRANSPOSE /*! * Performs forward elimination and back substitution to find the @@ -458,33 +416,28 @@ ComplexNumber Temp; /*VARARGS3*/ -void -spSolveTransposed( - MatrixPtr Matrix, - spREAL RHS[], - spREAL Solution[] -# if spCOMPLEX AND spSEPARATED_COMPLEX_VECTORS - , spREAL iRHS[] - , spREAL iSolution[] -# endif -) -{ +void spSolveTransposed(MatrixPtr Matrix, spREAL RHS[], spREAL Solution[] +#if spCOMPLEX AND spSEPARATED_COMPLEX_VECTORS + , + spREAL iRHS[], spREAL iSolution[] +#endif +) { #if REAL -ElementPtr pElement; -RealVector Intermediate; -int I, *pExtOrder, Size; -ElementPtr pPivot; -RealNumber Temp; + ElementPtr pElement; + RealVector Intermediate; + int I, *pExtOrder, Size; + ElementPtr pPivot; + RealNumber Temp; #endif -/* Begin `spSolveTransposed'. */ - ASSERT_IS_SPARSE( Matrix ); - ASSERT_NO_ERRORS( Matrix ); - ASSERT_IS_FACTORED( Matrix ); + /* Begin `spSolveTransposed'. */ + ASSERT_IS_SPARSE(Matrix); + ASSERT_NO_ERRORS(Matrix); + ASSERT_IS_FACTORED(Matrix); #if spCOMPLEX - if (Matrix->Complex) - { SolveComplexTransposedMatrix( Matrix, RHS, Solution IMAG_VECTORS ); + if (Matrix->Complex) { + SolveComplexTransposedMatrix(Matrix, RHS, Solution IMAG_VECTORS); return; } #endif @@ -499,38 +452,37 @@ RealNumber Temp; --Solution; #endif -/* Initialize Intermediate vector. */ + /* Initialize Intermediate vector. */ pExtOrder = &Matrix->IntToExtColMap[Size]; for (I = Size; I > 0; I--) Intermediate[I] = RHS[*(pExtOrder--)]; -/* Forward elimination. */ - for (I = 1; I <= Size; I++) - { -/* This step of the elimination is skipped if Temp equals zero. */ - if ((Temp = Intermediate[I]) != 0.0) - { pElement = Matrix->Diag[I]->NextInRow; - while (pElement != NULL) - { Intermediate[pElement->Col] -= Temp * pElement->Real; + /* Forward elimination. */ + for (I = 1; I <= Size; I++) { + /* This step of the elimination is skipped if Temp equals zero. */ + if ((Temp = Intermediate[I]) != 0.0) { + pElement = Matrix->Diag[I]->NextInRow; + while (pElement != NULL) { + Intermediate[pElement->Col] -= Temp * pElement->Real; pElement = pElement->NextInRow; } - } } -/* Backward Substitution. */ - for (I = Size; I > 0; I--) - { pPivot = Matrix->Diag[I]; + /* Backward Substitution. */ + for (I = Size; I > 0; I--) { + pPivot = Matrix->Diag[I]; Temp = Intermediate[I]; pElement = pPivot->NextInCol; - while (pElement != NULL) - { Temp -= pElement->Real * Intermediate[pElement->Row]; + while (pElement != NULL) { + Temp -= pElement->Real * Intermediate[pElement->Row]; pElement = pElement->NextInCol; } Intermediate[I] = Temp * pPivot->Real; } -/* Unscramble Intermediate vector while placing data in to Solution vector. */ + /* Unscramble Intermediate vector while placing data in to Solution vector. + */ pExtOrder = &Matrix->IntToExtRowMap[Size]; for (I = Size; I > 0; I--) Solution[*(pExtOrder--)] = Intermediate[I]; @@ -540,15 +492,6 @@ RealNumber Temp; } #endif /* TRANSPOSE */ - - - - - - - - - #if TRANSPOSE AND spCOMPLEX /*! * Performs forward elimination and back substitution to find the @@ -605,27 +548,23 @@ RealNumber Temp; * Temporary storage for entries in arrays. */ -static void -SolveComplexTransposedMatrix( - MatrixPtr Matrix, - RealVector RHS, - RealVector Solution -# if spSEPARATED_COMPLEX_VECTORS - , RealVector iRHS - , RealVector iSolution -# endif -) -{ -ElementPtr pElement; -ComplexVector Intermediate; -int I, *pExtOrder, Size; -#if NOT spSEPARATED_COMPLEX_VECTORS -ComplexVector ExtVector; +static void SolveComplexTransposedMatrix(MatrixPtr Matrix, RealVector RHS, + RealVector Solution +#if spSEPARATED_COMPLEX_VECTORS + , + RealVector iRHS, RealVector iSolution #endif -ElementPtr pPivot; -ComplexNumber Temp; +) { + ElementPtr pElement; + ComplexVector Intermediate; + int I, *pExtOrder, Size; +#if NOT spSEPARATED_COMPLEX_VECTORS + ComplexVector ExtVector; +#endif + ElementPtr pPivot; + ComplexNumber Temp; -/* Begin `SolveComplexTransposedMatrix'. */ + /* Begin `SolveComplexTransposedMatrix'. */ Size = Matrix->Size; Intermediate = (ComplexVector)Matrix->Intermediate; @@ -633,19 +572,22 @@ ComplexNumber Temp; /* Correct array pointers for ARRAY_OFFSET. */ #if NOT ARRAY_OFFSET #if spSEPARATED_COMPLEX_VECTORS - --RHS; --iRHS; - --Solution; --iSolution; + --RHS; + --iRHS; + --Solution; + --iSolution; #else - RHS -= 2; Solution -= 2; + RHS -= 2; + Solution -= 2; #endif #endif -/* Initialize Intermediate vector. */ + /* Initialize Intermediate vector. */ pExtOrder = &Matrix->IntToExtColMap[Size]; #if spSEPARATED_COMPLEX_VECTORS - for (I = Size; I > 0; I--) - { Intermediate[I].Real = RHS[*(pExtOrder)]; + for (I = Size; I > 0; I--) { + Intermediate[I].Real = RHS[*(pExtOrder)]; Intermediate[I].Imag = iRHS[*(pExtOrder--)]; } #else @@ -654,46 +596,46 @@ ComplexNumber Temp; Intermediate[I] = ExtVector[*(pExtOrder--)]; #endif -/* Forward elimination. */ - for (I = 1; I <= Size; I++) - { Temp = Intermediate[I]; + /* Forward elimination. */ + for (I = 1; I <= Size; I++) { + Temp = Intermediate[I]; -/* This step of the elimination is skipped if Temp equals zero. */ - if ((Temp.Real != 0.0) OR (Temp.Imag != 0.0)) - { pElement = Matrix->Diag[I]->NextInRow; - while (pElement != NULL) - { -/* Cmplx expr: Intermediate[Element->Col] -= Temp * *Element. */ - CMPLX_MULT_SUBT_ASSIGN( Intermediate[pElement->Col], - Temp, *pElement); + /* This step of the elimination is skipped if Temp equals zero. */ + if ((Temp.Real != 0.0) OR(Temp.Imag != 0.0)) { + pElement = Matrix->Diag[I]->NextInRow; + while (pElement != NULL) { + /* Cmplx expr: Intermediate[Element->Col] -= Temp * *Element. */ + CMPLX_MULT_SUBT_ASSIGN(Intermediate[pElement->Col], Temp, + *pElement); pElement = pElement->NextInRow; } } } -/* Backward Substitution. */ - for (I = Size; I > 0; I--) - { pPivot = Matrix->Diag[I]; + /* Backward Substitution. */ + for (I = Size; I > 0; I--) { + pPivot = Matrix->Diag[I]; Temp = Intermediate[I]; pElement = pPivot->NextInCol; - while (pElement != NULL) - { -/* Cmplx expr: Temp -= Intermediate[Element->Row] * *Element. */ - CMPLX_MULT_SUBT_ASSIGN(Temp,Intermediate[pElement->Row],*pElement); + while (pElement != NULL) { + /* Cmplx expr: Temp -= Intermediate[Element->Row] * *Element. */ + CMPLX_MULT_SUBT_ASSIGN(Temp, Intermediate[pElement->Row], + *pElement); pElement = pElement->NextInCol; } -/* Cmplx expr: Intermediate = Temp * (1.0 / *pPivot). */ + /* Cmplx expr: Intermediate = Temp * (1.0 / *pPivot). */ CMPLX_MULT(Intermediate[I], Temp, *pPivot); } -/* Unscramble Intermediate vector while placing data in to Solution vector. */ + /* Unscramble Intermediate vector while placing data in to Solution vector. + */ pExtOrder = &Matrix->IntToExtRowMap[Size]; #if spSEPARATED_COMPLEX_VECTORS - for (I = Size; I > 0; I--) - { Solution[*(pExtOrder)] = Intermediate[I].Real; + for (I = Size; I > 0; I--) { + Solution[*(pExtOrder)] = Intermediate[I].Real; iSolution[*(pExtOrder--)] = Intermediate[I].Imag; } #else diff --git a/src/maths/sparse/spUtils.c b/src/maths/sparse/spUtils.c index 598560153..020b140eb 100644 --- a/src/maths/sparse/spUtils.c +++ b/src/maths/sparse/spUtils.c @@ -37,7 +37,6 @@ * ComplexCondition */ - /* * Revision and copyright information. * @@ -47,12 +46,10 @@ #ifdef notdef static char copyright[] = "Sparse1.4: Copyright (c) 1985-2003 by Kenneth S. Kundert"; -static char RCSid[] = - "@(#)$Header: /cvsroot/sparse/src/spUtils.c,v 1.4 2003/06/30 19:40:52 kundert Exp $"; +static char RCSid[] = "@(#)$Header: /cvsroot/sparse/src/spUtils.c,v 1.4 " + "2003/06/30 19:40:52 kundert Exp $"; #endif - - /* * IMPORTS * @@ -66,47 +63,36 @@ static char RCSid[] = */ #define spINSIDE_SPARSE -#include -#include "spConfig.h" #include "ngspice/spmatrix.h" +#include "spConfig.h" #include "spDefs.h" - - - - +#include /* * Function declarations */ #if MODIFIED_NODAL -static int CountTwins( MatrixPtr, int, ElementPtr*, ElementPtr* ); -static void SwapCols( MatrixPtr, ElementPtr, ElementPtr ); +static int CountTwins(MatrixPtr, int, ElementPtr *, ElementPtr *); +static void SwapCols(MatrixPtr, ElementPtr, ElementPtr); #endif #if spCOMPLEX AND SCALING -static void ScaleComplexMatrix( MatrixPtr, RealVector, RealVector ); +static void ScaleComplexMatrix(MatrixPtr, RealVector, RealVector); #endif #if spSEPARATED_COMPLEX_VECTORS -static void ComplexMatrixMultiply( MatrixPtr, - RealVector, RealVector, RealVector, RealVector ); -static void ComplexTransposedMatrixMultiply( MatrixPtr, - RealVector, RealVector, RealVector, RealVector ); +static void ComplexMatrixMultiply(MatrixPtr, RealVector, RealVector, RealVector, + RealVector); +static void ComplexTransposedMatrixMultiply(MatrixPtr, RealVector, RealVector, + RealVector, RealVector); #else -static void ComplexMatrixMultiply( MatrixPtr, - RealVector, RealVector ); -static void ComplexTransposedMatrixMultiply( MatrixPtr, - RealVector, RealVector ); +static void ComplexMatrixMultiply(MatrixPtr, RealVector, RealVector); +static void ComplexTransposedMatrixMultiply(MatrixPtr, RealVector, RealVector); #endif #if CONDITION #if spCOMPLEX -static RealNumber ComplexCondition( MatrixPtr, RealNumber, int* ); +static RealNumber ComplexCondition(MatrixPtr, RealNumber, int *); #endif #endif - - - - - #if MODIFIED_NODAL /*! * This routine massages modified node admittance matrices to remove @@ -200,48 +186,48 @@ static RealNumber ComplexCondition( MatrixPtr, RealNumber, int* ); * Number of symmetric twins corresponding to current zero diagonal. */ -void -spMNA_Preorder( MatrixPtr Matrix ) -{ -int J, Size; -ElementPtr pTwin1, pTwin2; -int Twins, StartAt = 1; -BOOLEAN Swapped, AnotherPassNeeded; +void spMNA_Preorder(MatrixPtr Matrix) { + int J, Size; + ElementPtr pTwin1, pTwin2; + int Twins, StartAt = 1; + BOOLEAN Swapped, AnotherPassNeeded; -/* Begin `spMNA_Preorder'. */ - ASSERT_IS_SPARSE( Matrix ); - ASSERT_NO_ERRORS( Matrix ); - ASSERT_IS_NOT_FACTORED( Matrix ); + /* Begin `spMNA_Preorder'. */ + ASSERT_IS_SPARSE(Matrix); + ASSERT_NO_ERRORS(Matrix); + ASSERT_IS_NOT_FACTORED(Matrix); - if (Matrix->RowsLinked) return; + if (Matrix->RowsLinked) + return; Size = Matrix->Size; Matrix->Reordered = YES; - do - { AnotherPassNeeded = Swapped = NO; + do { + AnotherPassNeeded = Swapped = NO; -/* Search for zero diagonals with lone twins. */ - for (J = StartAt; J <= Size; J++) - { if (Matrix->Diag[J] == NULL) - { Twins = CountTwins( Matrix, J, &pTwin1, &pTwin2 ); - if (Twins == 1) - { /* Lone twins found, swap rows. */ - SwapCols( Matrix, pTwin1, pTwin2 ); + /* Search for zero diagonals with lone twins. */ + for (J = StartAt; J <= Size; J++) { + if (Matrix->Diag[J] == NULL) { + Twins = CountTwins(Matrix, J, &pTwin1, &pTwin2); + if (Twins == 1) { /* Lone twins found, swap rows. */ + SwapCols(Matrix, pTwin1, pTwin2); Swapped = YES; - } - else if ((Twins > 1) AND NOT AnotherPassNeeded) - { AnotherPassNeeded = YES; + } else if ((Twins > 1) AND NOT AnotherPassNeeded) { + AnotherPassNeeded = YES; StartAt = J; } } } -/* All lone twins are gone, look for zero diagonals with multiple twins. */ - if (AnotherPassNeeded) - { for (J = StartAt; NOT Swapped AND (J <= Size); J++) - { if (Matrix->Diag[J] == NULL) - { Twins = CountTwins( Matrix, J, &pTwin1, &pTwin2 ); /* FIXME: Twins is never used */ - SwapCols( Matrix, pTwin1, pTwin2 ); + /* All lone twins are gone, look for zero diagonals with multiple twins. + */ + if (AnotherPassNeeded) { + for (J = StartAt; NOT Swapped AND(J <= Size); J++) { + if (Matrix->Diag[J] == NULL) { + Twins = + CountTwins(Matrix, J, &pTwin1, + &pTwin2); /* FIXME: Twins is never used */ + SwapCols(Matrix, pTwin1, pTwin2); Swapped = YES; } } @@ -250,9 +236,6 @@ BOOLEAN Swapped, AnotherPassNeeded; return; } - - - /* * COUNT TWINS * @@ -261,29 +244,24 @@ BOOLEAN Swapped, AnotherPassNeeded; * count is terminated early at two. */ -static int -CountTwins( - MatrixPtr Matrix, - int Col, - ElementPtr *ppTwin1, - ElementPtr *ppTwin2 -) -{ -int Row, Twins = 0; -ElementPtr pTwin1, pTwin2; +static int CountTwins(MatrixPtr Matrix, int Col, ElementPtr *ppTwin1, + ElementPtr *ppTwin2) { + int Row, Twins = 0; + ElementPtr pTwin1, pTwin2; -/* Begin `CountTwins'. */ + /* Begin `CountTwins'. */ pTwin1 = Matrix->FirstInCol[Col]; - while (pTwin1 != NULL) - { if (ABS(pTwin1->Real) == 1.0) - { Row = pTwin1->Row; + while (pTwin1 != NULL) { + if (ABS(pTwin1->Real) == 1.0) { + Row = pTwin1->Row; pTwin2 = Matrix->FirstInCol[Row]; - while ((pTwin2 != NULL) AND (pTwin2->Row != Col)) + while ((pTwin2 != NULL) AND(pTwin2->Row != Col)) pTwin2 = pTwin2->NextInCol; - if ((pTwin2 != NULL) AND (ABS(pTwin2->Real) == 1.0)) - { /* Found symmetric twins. */ - if (++Twins >= 2) return Twins; + if ((pTwin2 != NULL) AND(ABS(pTwin2->Real) == + 1.0)) { /* Found symmetric twins. */ + if (++Twins >= 2) + return Twins; (*ppTwin1 = pTwin1)->Col = Col; (*ppTwin2 = pTwin2)->Col = Row; } @@ -293,9 +271,6 @@ ElementPtr pTwin1, pTwin2; return Twins; } - - - /* * SWAP COLUMNS * @@ -303,22 +278,16 @@ ElementPtr pTwin1, pTwin2; * linked. */ -static void -SwapCols( - MatrixPtr Matrix, - ElementPtr pTwin1, - ElementPtr pTwin2 -) -{ -int Col1 = pTwin1->Col, Col2 = pTwin2->Col; +static void SwapCols(MatrixPtr Matrix, ElementPtr pTwin1, ElementPtr pTwin2) { + int Col1 = pTwin1->Col, Col2 = pTwin2->Col; -/* Begin `SwapCols'. */ + /* Begin `SwapCols'. */ - SWAP (ElementPtr, Matrix->FirstInCol[Col1], Matrix->FirstInCol[Col2]); - SWAP (int, Matrix->IntToExtColMap[Col1], Matrix->IntToExtColMap[Col2]); + SWAP(ElementPtr, Matrix->FirstInCol[Col1], Matrix->FirstInCol[Col2]); + SWAP(int, Matrix->IntToExtColMap[Col1], Matrix->IntToExtColMap[Col2]); #if TRANSLATE - Matrix->ExtToIntColMap[Matrix->IntToExtColMap[Col2]]=Col2; - Matrix->ExtToIntColMap[Matrix->IntToExtColMap[Col1]]=Col1; + Matrix->ExtToIntColMap[Matrix->IntToExtColMap[Col2]] = Col2; + Matrix->ExtToIntColMap[Matrix->IntToExtColMap[Col1]] = Col1; #endif Matrix->Diag[Col1] = pTwin2; @@ -328,14 +297,6 @@ int Col1 = pTwin1->Col, Col2 = pTwin2->Col; } #endif /* MODIFIED_NODAL */ - - - - - - - - #if SCALING /*! * This function scales the matrix to enhance the possibility of @@ -390,26 +351,22 @@ int Col1 = pTwin1->Col, Col2 = pTwin2->Col; * The scale factor being used on the current row or column. */ -void -spScale( - MatrixPtr Matrix, - spREAL RHS_ScaleFactors[], - spREAL SolutionScaleFactors[] -) -{ -ElementPtr pElement; -int I, lSize, *pExtOrder; -RealNumber ScaleFactor; +void spScale(MatrixPtr Matrix, spREAL RHS_ScaleFactors[], + spREAL SolutionScaleFactors[]) { + ElementPtr pElement; + int I, lSize, *pExtOrder; + RealNumber ScaleFactor; -/* Begin `spScale'. */ - ASSERT_IS_SPARSE( Matrix ); - ASSERT_NO_ERRORS( Matrix ); - ASSERT_IS_NOT_FACTORED( Matrix ); - if (NOT Matrix->RowsLinked) spcLinkRows( Matrix ); + /* Begin `spScale'. */ + ASSERT_IS_SPARSE(Matrix); + ASSERT_NO_ERRORS(Matrix); + ASSERT_IS_NOT_FACTORED(Matrix); + if (NOT Matrix->RowsLinked) + spcLinkRows(Matrix); #if spCOMPLEX - if (Matrix->Complex) - { ScaleComplexMatrix( Matrix, RHS_ScaleFactors, SolutionScaleFactors ); + if (Matrix->Complex) { + ScaleComplexMatrix(Matrix, RHS_ScaleFactors, SolutionScaleFactors); return; } #endif @@ -423,27 +380,27 @@ RealNumber ScaleFactor; --SolutionScaleFactors; #endif -/* Scale Rows */ + /* Scale Rows */ pExtOrder = &Matrix->IntToExtRowMap[1]; - for (I = 1; I <= lSize; I++) - { if ((ScaleFactor = RHS_ScaleFactors[*(pExtOrder++)]) != 1.0) - { pElement = Matrix->FirstInRow[I]; + for (I = 1; I <= lSize; I++) { + if ((ScaleFactor = RHS_ScaleFactors[*(pExtOrder++)]) != 1.0) { + pElement = Matrix->FirstInRow[I]; - while (pElement != NULL) - { pElement->Real *= ScaleFactor; + while (pElement != NULL) { + pElement->Real *= ScaleFactor; pElement = pElement->NextInRow; } } } -/* Scale Columns */ + /* Scale Columns */ pExtOrder = &Matrix->IntToExtColMap[1]; - for (I = 1; I <= lSize; I++) - { if ((ScaleFactor = SolutionScaleFactors[*(pExtOrder++)]) != 1.0) - { pElement = Matrix->FirstInCol[I]; + for (I = 1; I <= lSize; I++) { + if ((ScaleFactor = SolutionScaleFactors[*(pExtOrder++)]) != 1.0) { + pElement = Matrix->FirstInCol[I]; - while (pElement != NULL) - { pElement->Real *= ScaleFactor; + while (pElement != NULL) { + pElement->Real *= ScaleFactor; pElement = pElement->NextInCol; } } @@ -454,14 +411,6 @@ RealNumber ScaleFactor; } #endif /* SCALING */ - - - - - - - - #if spCOMPLEX AND SCALING /* * SCALE COMPLEX MATRIX @@ -519,18 +468,13 @@ RealNumber ScaleFactor; * The scale factor being used on the current row or column. */ -static void -ScaleComplexMatrix( - MatrixPtr Matrix, - RealVector RHS_ScaleFactors, - RealVector SolutionScaleFactors -) -{ -ElementPtr pElement; -int I, lSize, *pExtOrder; -RealNumber ScaleFactor; +static void ScaleComplexMatrix(MatrixPtr Matrix, RealVector RHS_ScaleFactors, + RealVector SolutionScaleFactors) { + ElementPtr pElement; + int I, lSize, *pExtOrder; + RealNumber ScaleFactor; -/* Begin `ScaleComplexMatrix'. */ + /* Begin `ScaleComplexMatrix'. */ lSize = Matrix->Size; /* Correct pointers to arrays for ARRAY_OFFSET */ @@ -539,28 +483,28 @@ RealNumber ScaleFactor; --SolutionScaleFactors; #endif -/* Scale Rows */ + /* Scale Rows */ pExtOrder = &Matrix->IntToExtRowMap[1]; - for (I = 1; I <= lSize; I++) - { if ((ScaleFactor = RHS_ScaleFactors[*(pExtOrder++)]) != 1.0) - { pElement = Matrix->FirstInRow[I]; + for (I = 1; I <= lSize; I++) { + if ((ScaleFactor = RHS_ScaleFactors[*(pExtOrder++)]) != 1.0) { + pElement = Matrix->FirstInRow[I]; - while (pElement != NULL) - { pElement->Real *= ScaleFactor; + while (pElement != NULL) { + pElement->Real *= ScaleFactor; pElement->Imag *= ScaleFactor; pElement = pElement->NextInRow; } } } -/* Scale Columns */ + /* Scale Columns */ pExtOrder = &Matrix->IntToExtColMap[1]; - for (I = 1; I <= lSize; I++) - { if ((ScaleFactor = SolutionScaleFactors[*(pExtOrder++)]) != 1.0) - { pElement = Matrix->FirstInCol[I]; + for (I = 1; I <= lSize; I++) { + if ((ScaleFactor = SolutionScaleFactors[*(pExtOrder++)]) != 1.0) { + pElement = Matrix->FirstInCol[I]; - while (pElement != NULL) - { pElement->Real *= ScaleFactor; + while (pElement != NULL) { + pElement->Real *= ScaleFactor; pElement->Imag *= ScaleFactor; pElement = pElement->NextInCol; } @@ -570,13 +514,6 @@ RealNumber ScaleFactor; } #endif /* SCALING AND spCOMPLEX */ - - - - - - - #if MULTIPLICATION /*! * Multiplies matrix by solution vector to find source vector. @@ -600,59 +537,54 @@ RealNumber ScaleFactor; * complex and \a spSEPARATED_COMPLEX_VECTORS is true. */ -void -spMultiply( - MatrixPtr Matrix, - spREAL RHS[], - spREAL Solution[] +void spMultiply(MatrixPtr Matrix, spREAL RHS[], spREAL Solution[] #if spCOMPLEX AND spSEPARATED_COMPLEX_VECTORS - , spREAL iRHS[] - , spREAL iSolution[] + , + spREAL iRHS[], spREAL iSolution[] #endif -) -{ +) { #if REAL -ElementPtr pElement; -RealVector Vector; -RealNumber Sum; -int I, *pExtOrder; + ElementPtr pElement; + RealVector Vector; + RealNumber Sum; + int I, *pExtOrder; #endif -/* Begin `spMultiply'. */ - ASSERT_IS_SPARSE( Matrix ); - ASSERT_IS_NOT_FACTORED( Matrix ); + /* Begin `spMultiply'. */ + ASSERT_IS_SPARSE(Matrix); + ASSERT_IS_NOT_FACTORED(Matrix); if (NOT Matrix->RowsLinked) - spcLinkRows(Matrix); + spcLinkRows(Matrix); if (NOT Matrix->InternalVectorsAllocated) - spcCreateInternalVectors( Matrix ); + spcCreateInternalVectors(Matrix); #if spCOMPLEX - if (Matrix->Complex) - { ComplexMatrixMultiply( Matrix, RHS, Solution IMAG_VECTORS ); + if (Matrix->Complex) { + ComplexMatrixMultiply(Matrix, RHS, Solution IMAG_VECTORS); return; } #endif #if REAL #if NOT ARRAY_OFFSET -/* Correct array pointers for ARRAY_OFFSET. */ + /* Correct array pointers for ARRAY_OFFSET. */ --RHS; --Solution; #endif -/* Initialize Intermediate vector with reordered Solution vector. */ + /* Initialize Intermediate vector with reordered Solution vector. */ Vector = Matrix->Intermediate; pExtOrder = &Matrix->IntToExtColMap[Matrix->Size]; for (I = Matrix->Size; I > 0; I--) Vector[I] = Solution[*(pExtOrder--)]; pExtOrder = &Matrix->IntToExtRowMap[Matrix->Size]; - for (I = Matrix->Size; I > 0; I--) - { pElement = Matrix->FirstInRow[I]; + for (I = Matrix->Size; I > 0; I--) { + pElement = Matrix->FirstInRow[I]; Sum = 0.0; - while (pElement != NULL) - { Sum += pElement->Real * Vector[pElement->Col]; + while (pElement != NULL) { + Sum += pElement->Real * Vector[pElement->Col]; pElement = pElement->NextInRow; } RHS[*pExtOrder--] = Sum; @@ -662,12 +594,6 @@ int I, *pExtOrder; } #endif /* MULTIPLICATION */ - - - - - - #if spCOMPLEX AND MULTIPLICATION /* * COMPLEX MATRIX MULTIPLICATION @@ -697,41 +623,40 @@ int I, *pExtOrder; * complex and spSEPARATED_COMPLEX_VECTORS is true. */ -static void -ComplexMatrixMultiply( - MatrixPtr Matrix, - RealVector RHS, - RealVector Solution +static void ComplexMatrixMultiply(MatrixPtr Matrix, RealVector RHS, + RealVector Solution #if spSEPARATED_COMPLEX_VECTORS - , RealVector iRHS - , RealVector iSolution + , + RealVector iRHS, RealVector iSolution #endif -) -{ -ElementPtr pElement; -ComplexVector Vector; -ComplexNumber Sum; -int I, *pExtOrder; +) { + ElementPtr pElement; + ComplexVector Vector; + ComplexNumber Sum; + int I, *pExtOrder; /* Begin `ComplexMatrixMultiply'. */ /* Correct array pointers for ARRAY_OFFSET. */ #if NOT ARRAY_OFFSET #if spSEPARATED_COMPLEX_VECTORS - --RHS; --iRHS; - --Solution; --iSolution; + --RHS; + --iRHS; + --Solution; + --iSolution; #else - RHS -= 2; Solution -= 2; + RHS -= 2; + Solution -= 2; #endif #endif -/* Initialize Intermediate vector with reordered Solution vector. */ + /* Initialize Intermediate vector with reordered Solution vector. */ Vector = (ComplexVector)Matrix->Intermediate; pExtOrder = &Matrix->IntToExtColMap[Matrix->Size]; #if spSEPARATED_COMPLEX_VECTORS - for (I = Matrix->Size; I > 0; I--) - { Vector[I].Real = Solution[*pExtOrder]; + for (I = Matrix->Size; I > 0; I--) { + Vector[I].Real = Solution[*pExtOrder]; Vector[I].Imag = iSolution[*(pExtOrder--)]; } #else @@ -740,13 +665,13 @@ int I, *pExtOrder; #endif pExtOrder = &Matrix->IntToExtRowMap[Matrix->Size]; - for (I = Matrix->Size; I > 0; I--) - { pElement = Matrix->FirstInRow[I]; + for (I = Matrix->Size; I > 0; I--) { + pElement = Matrix->FirstInRow[I]; Sum.Real = Sum.Imag = 0.0; - while (pElement != NULL) - { /* Cmplx expression : Sum += Element * Vector[Col] */ - CMPLX_MULT_ADD_ASSIGN( Sum, *pElement, Vector[pElement->Col] ); + while (pElement != + NULL) { /* Cmplx expression : Sum += Element * Vector[Col] */ + CMPLX_MULT_ADD_ASSIGN(Sum, *pElement, Vector[pElement->Col]); pElement = pElement->NextInRow; } @@ -761,13 +686,6 @@ int I, *pExtOrder; } #endif /* spCOMPLEX AND MULTIPLICATION */ - - - - - - - #if MULTIPLICATION AND TRANSPOSE /*! * Multiplies transposed matrix by solution vector to find source vector. @@ -791,57 +709,52 @@ int I, *pExtOrder; * complex and \a spSEPARATED_COMPLEX_VECTORS is true. */ -void -spMultTransposed( - MatrixPtr Matrix, - spREAL RHS[], - spREAL Solution[] +void spMultTransposed(MatrixPtr Matrix, spREAL RHS[], spREAL Solution[] #if spCOMPLEX AND spSEPARATED_COMPLEX_VECTORS - , spREAL iRHS[] - , spREAL iSolution[] + , + spREAL iRHS[], spREAL iSolution[] #endif -) -{ +) { #if REAL -ElementPtr pElement; -RealVector Vector; -RealNumber Sum; -int I, *pExtOrder; + ElementPtr pElement; + RealVector Vector; + RealNumber Sum; + int I, *pExtOrder; #endif -/* Begin `spMultTransposed'. */ - ASSERT_IS_SPARSE( Matrix ); - ASSERT_IS_NOT_FACTORED( Matrix ); + /* Begin `spMultTransposed'. */ + ASSERT_IS_SPARSE(Matrix); + ASSERT_IS_NOT_FACTORED(Matrix); if (NOT Matrix->InternalVectorsAllocated) - spcCreateInternalVectors( Matrix ); + spcCreateInternalVectors(Matrix); #if spCOMPLEX - if (Matrix->Complex) - { ComplexTransposedMatrixMultiply( Matrix, RHS, Solution IMAG_VECTORS ); + if (Matrix->Complex) { + ComplexTransposedMatrixMultiply(Matrix, RHS, Solution IMAG_VECTORS); return; } #endif #if REAL #if NOT ARRAY_OFFSET -/* Correct array pointers for ARRAY_OFFSET. */ + /* Correct array pointers for ARRAY_OFFSET. */ --RHS; --Solution; #endif -/* Initialize Intermediate vector with reordered Solution vector. */ + /* Initialize Intermediate vector with reordered Solution vector. */ Vector = Matrix->Intermediate; pExtOrder = &Matrix->IntToExtRowMap[Matrix->Size]; for (I = Matrix->Size; I > 0; I--) Vector[I] = Solution[*(pExtOrder--)]; pExtOrder = &Matrix->IntToExtColMap[Matrix->Size]; - for (I = Matrix->Size; I > 0; I--) - { pElement = Matrix->FirstInCol[I]; + for (I = Matrix->Size; I > 0; I--) { + pElement = Matrix->FirstInCol[I]; Sum = 0.0; - while (pElement != NULL) - { Sum += pElement->Real * Vector[pElement->Row]; + while (pElement != NULL) { + Sum += pElement->Real * Vector[pElement->Row]; pElement = pElement->NextInCol; } RHS[*pExtOrder--] = Sum; @@ -851,12 +764,6 @@ int I, *pExtOrder; } #endif /* MULTIPLICATION AND TRANSPOSE */ - - - - - - #if spCOMPLEX AND MULTIPLICATION AND TRANSPOSE /* * COMPLEX TRANSPOSED MATRIX MULTIPLICATION @@ -892,41 +799,41 @@ int I, *pExtOrder; * without a trace. */ -static void -ComplexTransposedMatrixMultiply( - MatrixPtr Matrix, - RealVector RHS, - RealVector Solution +static void ComplexTransposedMatrixMultiply(MatrixPtr Matrix, RealVector RHS, + RealVector Solution #if spSEPARATED_COMPLEX_VECTORS - , RealVector iRHS - , RealVector iSolution + , + RealVector iRHS, + RealVector iSolution #endif -) -{ -ElementPtr pElement; -ComplexVector Vector; -ComplexNumber Sum; -int I, *pExtOrder; +) { + ElementPtr pElement; + ComplexVector Vector; + ComplexNumber Sum; + int I, *pExtOrder; /* Begin `ComplexTransposedMatrixMultiply'. */ /* Correct array pointers for ARRAY_OFFSET. */ #if NOT ARRAY_OFFSET #if spSEPARATED_COMPLEX_VECTORS - --RHS; --iRHS; - --Solution; --iSolution; + --RHS; + --iRHS; + --Solution; + --iSolution; #else - RHS -= 2; Solution -= 2; + RHS -= 2; + Solution -= 2; #endif #endif -/* Initialize Intermediate vector with reordered Solution vector. */ + /* Initialize Intermediate vector with reordered Solution vector. */ Vector = (ComplexVector)Matrix->Intermediate; pExtOrder = &Matrix->IntToExtRowMap[Matrix->Size]; #if spSEPARATED_COMPLEX_VECTORS - for (I = Matrix->Size; I > 0; I--) - { Vector[I].Real = Solution[*pExtOrder]; + for (I = Matrix->Size; I > 0; I--) { + Vector[I].Real = Solution[*pExtOrder]; Vector[I].Imag = iSolution[*(pExtOrder--)]; } #else @@ -935,13 +842,13 @@ int I, *pExtOrder; #endif pExtOrder = &Matrix->IntToExtColMap[Matrix->Size]; - for (I = Matrix->Size; I > 0; I--) - { pElement = Matrix->FirstInCol[I]; + for (I = Matrix->Size; I > 0; I--) { + pElement = Matrix->FirstInCol[I]; Sum.Real = Sum.Imag = 0.0; - while (pElement != NULL) - { /* Cmplx expression : Sum += Element * Vector[Row] */ - CMPLX_MULT_ADD_ASSIGN( Sum, *pElement, Vector[pElement->Row] ); + while (pElement != + NULL) { /* Cmplx expression : Sum += Element * Vector[Row] */ + CMPLX_MULT_ADD_ASSIGN(Sum, *pElement, Vector[pElement->Row]); pElement = pElement->NextInCol; } @@ -956,13 +863,6 @@ int I, *pExtOrder; } #endif /* spCOMPLEX AND MULTIPLICATION AND TRANSPOSE */ - - - - - - - #if DETERMINANT /*! * This routine in capable of calculating the determinant of the @@ -1001,32 +901,29 @@ int I, *pExtOrder; * Temporary storage for real portion of determinant. */ -void -spDeterminant( - MatrixPtr Matrix, - int *pExponent, - spREAL *pDeterminant +void spDeterminant(MatrixPtr Matrix, int *pExponent, spREAL *pDeterminant #if spCOMPLEX - , spREAL *piDeterminant + , + spREAL *piDeterminant #endif -) -{ -int I, Size; -RealNumber Norm, nr, ni; -ComplexNumber Pivot, cDeterminant; +) { + int I, Size; + RealNumber Norm, nr, ni; + ComplexNumber Pivot, cDeterminant; -#define NORM(a) (nr = ABS((a).Real), ni = ABS((a).Imag), MAX (nr,ni)) +#define NORM(a) (nr = ABS((a).Real), ni = ABS((a).Imag), MAX(nr, ni)) -/* Begin `spDeterminant'. */ - ASSERT_IS_SPARSE( Matrix ); - ASSERT_NO_ERRORS( Matrix ); - ASSERT_IS_FACTORED( Matrix ); + /* Begin `spDeterminant'. */ + ASSERT_IS_SPARSE(Matrix); + ASSERT_NO_ERRORS(Matrix); + ASSERT_IS_FACTORED(Matrix); *pExponent = 0; - if (Matrix->Error == spSINGULAR) - { *pDeterminant = 0.0; + if (Matrix->Error == spSINGULAR) { + *pDeterminant = 0.0; #if spCOMPLEX - if (Matrix->Complex) *piDeterminant = 0.0; + if (Matrix->Complex) + *piDeterminant = 0.0; #endif return; } @@ -1035,50 +932,51 @@ ComplexNumber Pivot, cDeterminant; I = 0; #if spCOMPLEX - if (Matrix->Complex) /* Complex Case. */ - { cDeterminant.Real = 1.0; + if (Matrix->Complex) /* Complex Case. */ + { + cDeterminant.Real = 1.0; cDeterminant.Imag = 0.0; - while (++I <= Size) - { CMPLX_RECIPROCAL( Pivot, *Matrix->Diag[I] ); - CMPLX_MULT_ASSIGN( cDeterminant, Pivot ); + while (++I <= Size) { + CMPLX_RECIPROCAL(Pivot, *Matrix->Diag[I]); + CMPLX_MULT_ASSIGN(cDeterminant, Pivot); -/* Scale Determinant. */ - Norm = NORM( cDeterminant ); - if (Norm != 0.0) - { while (Norm >= 1.0e12) - { cDeterminant.Real *= 1.0e-12; + /* Scale Determinant. */ + Norm = NORM(cDeterminant); + if (Norm != 0.0) { + while (Norm >= 1.0e12) { + cDeterminant.Real *= 1.0e-12; cDeterminant.Imag *= 1.0e-12; *pExponent += 12; - Norm = NORM( cDeterminant ); + Norm = NORM(cDeterminant); } - while (Norm < 1.0e-12) - { cDeterminant.Real *= 1.0e12; + while (Norm < 1.0e-12) { + cDeterminant.Real *= 1.0e12; cDeterminant.Imag *= 1.0e12; *pExponent -= 12; - Norm = NORM( cDeterminant ); + Norm = NORM(cDeterminant); } } } -/* Scale Determinant again, this time to be between 1.0 <= x < 10.0. */ - Norm = NORM( cDeterminant ); - if (Norm != 0.0) - { while (Norm >= 10.0) - { cDeterminant.Real *= 0.1; + /* Scale Determinant again, this time to be between 1.0 <= x < 10.0. */ + Norm = NORM(cDeterminant); + if (Norm != 0.0) { + while (Norm >= 10.0) { + cDeterminant.Real *= 0.1; cDeterminant.Imag *= 0.1; (*pExponent)++; - Norm = NORM( cDeterminant ); + Norm = NORM(cDeterminant); } - while (Norm < 1.0) - { cDeterminant.Real *= 10.0; + while (Norm < 1.0) { + cDeterminant.Real *= 10.0; cDeterminant.Imag *= 10.0; (*pExponent)--; - Norm = NORM( cDeterminant ); + Norm = NORM(cDeterminant); } } if (Matrix->NumberOfInterchangesIsOdd) - CMPLX_NEGATE( cDeterminant ); + CMPLX_NEGATE(cDeterminant); *pDeterminant = cDeterminant.Real; *piDeterminant = cDeterminant.Imag; @@ -1088,33 +986,33 @@ ComplexNumber Pivot, cDeterminant; else #endif #if REAL - { /* Real Case. */ + { /* Real Case. */ *pDeterminant = 1.0; - while (++I <= Size) - { *pDeterminant /= Matrix->Diag[I]->Real; + while (++I <= Size) { + *pDeterminant /= Matrix->Diag[I]->Real; -/* Scale Determinant. */ - if (*pDeterminant != 0.0) - { while (ABS(*pDeterminant) >= 1.0e12) - { *pDeterminant *= 1.0e-12; + /* Scale Determinant. */ + if (*pDeterminant != 0.0) { + while (ABS(*pDeterminant) >= 1.0e12) { + *pDeterminant *= 1.0e-12; *pExponent += 12; } - while (ABS(*pDeterminant) < 1.0e-12) - { *pDeterminant *= 1.0e12; + while (ABS(*pDeterminant) < 1.0e-12) { + *pDeterminant *= 1.0e12; *pExponent -= 12; } } } -/* Scale Determinant again, this time to be between 1.0 <= x < 10.0. */ - if (*pDeterminant != 0.0) - { while (ABS(*pDeterminant) >= 10.0) - { *pDeterminant *= 0.1; + /* Scale Determinant again, this time to be between 1.0 <= x < 10.0. */ + if (*pDeterminant != 0.0) { + while (ABS(*pDeterminant) >= 10.0) { + *pDeterminant *= 0.1; (*pExponent)++; } - while (ABS(*pDeterminant) < 1.0) - { *pDeterminant *= 10.0; + while (ABS(*pDeterminant) < 1.0) { + *pDeterminant *= 10.0; (*pExponent)--; } } @@ -1125,15 +1023,8 @@ ComplexNumber Pivot, cDeterminant; } #endif /* DETERMINANT */ - - - - - - - #if STRIP - + /*! * Strips the matrix of all fill-ins. * @@ -1154,60 +1045,60 @@ ComplexNumber Pivot, cDeterminant; * A pointer to a node in the FillinList linked-list. */ -void -spStripFills( MatrixPtr Matrix ) -{ -struct FillinListNodeStruct *pListNode; +void spStripFills(MatrixPtr Matrix) { + struct FillinListNodeStruct *pListNode; -/* Begin `spStripFills'. */ - ASSERT_IS_SPARSE( Matrix ); - if (Matrix->Fillins == 0) return; + /* Begin `spStripFills'. */ + ASSERT_IS_SPARSE(Matrix); + if (Matrix->Fillins == 0) + return; Matrix->NeedsOrdering = YES; Matrix->Elements -= Matrix->Fillins; Matrix->Fillins = 0; -/* Mark the fill-ins. */ - { ElementPtr pFillin, pLastFillin; + /* Mark the fill-ins. */ + { + ElementPtr pFillin, pLastFillin; pListNode = Matrix->LastFillinListNode = Matrix->FirstFillinListNode; Matrix->FillinsRemaining = pListNode->NumberOfFillinsInList; Matrix->NextAvailFillin = pListNode->pFillinList; - while (pListNode != NULL) - { pFillin = pListNode->pFillinList; - pLastFillin = &(pFillin[ pListNode->NumberOfFillinsInList - 1 ]); + while (pListNode != NULL) { + pFillin = pListNode->pFillinList; + pLastFillin = &(pFillin[pListNode->NumberOfFillinsInList - 1]); while (pFillin <= pLastFillin) (pFillin++)->Row = 0; pListNode = pListNode->Next; } } -/* Unlink fill-ins by searching for elements marked with Row = 0. */ - { ElementPtr pElement, *ppElement; - int I, Size = Matrix->Size; + /* Unlink fill-ins by searching for elements marked with Row = 0. */ + { + ElementPtr pElement, *ppElement; + int I, Size = Matrix->Size; -/* Unlink fill-ins in all columns. */ - for (I = 1; I <= Size; I++) - { ppElement = &(Matrix->FirstInCol[I]); - while ((pElement = *ppElement) != NULL) - { if (pElement->Row == 0) - { *ppElement = pElement->NextInCol; /* Unlink fill-in. */ + /* Unlink fill-ins in all columns. */ + for (I = 1; I <= Size; I++) { + ppElement = &(Matrix->FirstInCol[I]); + while ((pElement = *ppElement) != NULL) { + if (pElement->Row == 0) { + *ppElement = pElement->NextInCol; /* Unlink fill-in. */ if (Matrix->Diag[pElement->Col] == pElement) Matrix->Diag[pElement->Col] = NULL; - } - else - ppElement = &pElement->NextInCol; /* Skip element. */ + } else + ppElement = &pElement->NextInCol; /* Skip element. */ } } -/* Unlink fill-ins in all rows. */ - for (I = 1; I <= Size; I++) - { ppElement = &(Matrix->FirstInRow[I]); - while ((pElement = *ppElement) != NULL) - { if (pElement->Row == 0) - *ppElement = pElement->NextInRow; /* Unlink fill-in. */ + /* Unlink fill-ins in all rows. */ + for (I = 1; I <= Size; I++) { + ppElement = &(Matrix->FirstInRow[I]); + while ((pElement = *ppElement) != NULL) { + if (pElement->Row == 0) + *ppElement = pElement->NextInRow; /* Unlink fill-in. */ else - ppElement = &pElement->NextInRow; /* Skip element. */ + ppElement = &pElement->NextInRow; /* Skip element. */ } } } @@ -1215,12 +1106,6 @@ struct FillinListNodeStruct *pListNode; } #endif - - - - - - #if TRANSLATE AND DELETE /*! * Deletes a row and a column from a matrix. @@ -1253,75 +1138,72 @@ struct FillinListNodeStruct *pListNode; * The local version Matrix->Size, the size of the matrix. */ -void -spDeleteRowAndCol( - MatrixPtr Matrix, - int Row, - int Col -) -{ -ElementPtr pElement, *ppElement, pLastElement; -int Size, ExtRow, ExtCol; +void spDeleteRowAndCol(MatrixPtr Matrix, int Row, int Col) { + ElementPtr pElement, *ppElement, pLastElement; + int Size, ExtRow, ExtCol; -/* Begin `spDeleteRowAndCol'. */ - ASSERT_IS_SPARSE( Matrix ); - vASSERT( (Row > 0) AND (Col > 0), "Nonpositive row or column number" ); - vASSERT( (Row <= Matrix->ExtSize) AND (Col <= Matrix->ExtSize), - "Row or column number too large" ); + /* Begin `spDeleteRowAndCol'. */ + ASSERT_IS_SPARSE(Matrix); + vASSERT((Row > 0) AND(Col > 0), "Nonpositive row or column number"); + vASSERT((Row <= Matrix->ExtSize) AND(Col <= Matrix->ExtSize), + "Row or column number too large"); Size = Matrix->Size; ExtRow = Row; ExtCol = Col; - if (NOT Matrix->RowsLinked) spcLinkRows( Matrix ); + if (NOT Matrix->RowsLinked) + spcLinkRows(Matrix); Row = Matrix->ExtToIntRowMap[Row]; Col = Matrix->ExtToIntColMap[Col]; - ASSERT( Row > 0 AND Col > 0 ); + ASSERT(Row > 0 AND Col > 0); -/* Move Row so that it is the last row in the matrix. */ - if (Row != Size) spcRowExchange( Matrix, Row, Size ); + /* Move Row so that it is the last row in the matrix. */ + if (Row != Size) + spcRowExchange(Matrix, Row, Size); -/* Move Col so that it is the last column in the matrix. */ - if (Col != Size) spcColExchange( Matrix, Col, Size ); + /* Move Col so that it is the last column in the matrix. */ + if (Col != Size) + spcColExchange(Matrix, Col, Size); -/* Correct Diag pointers. */ + /* Correct Diag pointers. */ if (Row == Col) - SWAP( ElementPtr, Matrix->Diag[Row], Matrix->Diag[Size] ) - else - { Matrix->Diag[Row] = spcFindDiag( Matrix, Row ); - Matrix->Diag[Col] = spcFindDiag( Matrix, Col ); + SWAP(ElementPtr, Matrix->Diag[Row], Matrix->Diag[Size]) + else { + Matrix->Diag[Row] = spcFindDiag(Matrix, Row); + Matrix->Diag[Col] = spcFindDiag(Matrix, Col); } -/* - * Delete last row and column of the matrix. - */ -/* Break the column links to every element in the last row. */ - pLastElement = Matrix->FirstInRow[ Size ]; - while (pLastElement != NULL) - { ppElement = &(Matrix->FirstInCol[ pLastElement->Col ]); - while ((pElement = *ppElement) != NULL) - { if (pElement == pLastElement) - *ppElement = NULL; /* Unlink last element in column. */ + /* + * Delete last row and column of the matrix. + */ + /* Break the column links to every element in the last row. */ + pLastElement = Matrix->FirstInRow[Size]; + while (pLastElement != NULL) { + ppElement = &(Matrix->FirstInCol[pLastElement->Col]); + while ((pElement = *ppElement) != NULL) { + if (pElement == pLastElement) + *ppElement = NULL; /* Unlink last element in column. */ else - ppElement = &pElement->NextInCol; /* Skip element. */ + ppElement = &pElement->NextInCol; /* Skip element. */ } pLastElement = pLastElement->NextInRow; } -/* Break the row links to every element in the last column. */ - pLastElement = Matrix->FirstInCol[ Size ]; - while (pLastElement != NULL) - { ppElement = &(Matrix->FirstInRow[ pLastElement->Row ]); - while ((pElement = *ppElement) != NULL) - { if (pElement == pLastElement) - *ppElement = NULL; /* Unlink last element in row. */ + /* Break the row links to every element in the last column. */ + pLastElement = Matrix->FirstInCol[Size]; + while (pLastElement != NULL) { + ppElement = &(Matrix->FirstInRow[pLastElement->Row]); + while ((pElement = *ppElement) != NULL) { + if (pElement == pLastElement) + *ppElement = NULL; /* Unlink last element in row. */ else - ppElement = &pElement->NextInRow; /* Skip element. */ + ppElement = &pElement->NextInRow; /* Skip element. */ } pLastElement = pLastElement->NextInCol; } -/* Clean up some details. */ + /* Clean up some details. */ Matrix->Size = Size - 1; Matrix->Diag[Size] = NULL; Matrix->FirstInRow[Size] = NULL; @@ -1335,13 +1217,6 @@ int Size, ExtRow, ExtCol; } #endif - - - - - - - #if PSEUDOCONDITION /*! * Computes the magnitude of the ratio of the largest to the smallest @@ -1364,41 +1239,32 @@ int Size, ExtRow, ExtCol; * Pointer to the matrix. */ -spREAL -spPseudoCondition( MatrixPtr Matrix ) -{ +spREAL spPseudoCondition(MatrixPtr Matrix) { int I; ArrayOfElementPtrs Diag; RealNumber MaxPivot, MinPivot, Mag; /* Begin `spPseudoCondition'. */ - ASSERT_IS_SPARSE( Matrix ); - ASSERT_NO_ERRORS( Matrix ); - ASSERT_IS_FACTORED( Matrix ); + ASSERT_IS_SPARSE(Matrix); + ASSERT_NO_ERRORS(Matrix); + ASSERT_IS_FACTORED(Matrix); if (Matrix->Error == spSINGULAR OR Matrix->Error == spZERO_DIAG) - return 0.0; + return 0.0; Diag = Matrix->Diag; - MaxPivot = MinPivot = ELEMENT_MAG( Diag[1] ); - for (I = 2; I <= Matrix->Size; I++) - { Mag = ELEMENT_MAG( Diag[I] ); - if (Mag > MaxPivot) - MaxPivot = Mag; - else if (Mag < MinPivot) - MinPivot = Mag; + MaxPivot = MinPivot = ELEMENT_MAG(Diag[1]); + for (I = 2; I <= Matrix->Size; I++) { + Mag = ELEMENT_MAG(Diag[I]); + if (Mag > MaxPivot) + MaxPivot = Mag; + else if (Mag < MinPivot) + MinPivot = Mag; } - ASSERT( MaxPivot > 0.0 ); + ASSERT(MaxPivot > 0.0); return MaxPivot / MinPivot; } #endif - - - - - - - #if CONDITION /*! * Computes an estimate of the condition number using a variation on @@ -1448,36 +1314,31 @@ spPseudoCondition( MatrixPtr Matrix ) * or \a spNO_MEMORY. */ -spREAL -spCondition( - MatrixPtr Matrix, - spREAL NormOfMatrix, - int *pError -) -{ -ElementPtr pElement; -RealVector T, Tm; -int I, K, Row; -ElementPtr pPivot; -int Size; -RealNumber E, Em, Wp, Wm, ASp, ASm, ASw, ASy, ASv, ASz, MaxY, ScaleFactor; -RealNumber Linpack, OLeary, InvNormOfInverse, ComplexCondition(); -#define SLACK 1e4 +spREAL spCondition(MatrixPtr Matrix, spREAL NormOfMatrix, int *pError) { + ElementPtr pElement; + RealVector T, Tm; + int I, K, Row; + ElementPtr pPivot; + int Size; + RealNumber E, Em, Wp, Wm, ASp, ASm, ASw, ASy, ASv, ASz, MaxY, ScaleFactor; + RealNumber Linpack, OLeary, InvNormOfInverse, ComplexCondition(); +#define SLACK 1e4 -/* Begin `spCondition'. */ - ASSERT_IS_SPARSE( Matrix ); - ASSERT_NO_ERRORS( Matrix ); - ASSERT_IS_FACTORED( Matrix ); + /* Begin `spCondition'. */ + ASSERT_IS_SPARSE(Matrix); + ASSERT_NO_ERRORS(Matrix); + ASSERT_IS_FACTORED(Matrix); *pError = Matrix->Error; - if (Matrix->Error >= spFATAL) return 0.0; - if (NormOfMatrix == 0.0) - { *pError = spSINGULAR; + if (Matrix->Error >= spFATAL) + return 0.0; + if (NormOfMatrix == 0.0) { + *pError = spSINGULAR; return 0.0; } #if spCOMPLEX if (Matrix->Complex) - return ComplexCondition( Matrix, NormOfMatrix, pError ); + return ComplexCondition(Matrix, NormOfMatrix, pError); #endif #if REAL @@ -1486,30 +1347,35 @@ RealNumber Linpack, OLeary, InvNormOfInverse, ComplexCondition(); #if spCOMPLEX Tm = Matrix->Intermediate + Size; #else - Tm = ALLOC( RealNumber, Size+1 ); - if (Tm == NULL) - { *pError = spNO_MEMORY; + Tm = ALLOC(RealNumber, Size + 1); + if (Tm == NULL) { + *pError = spNO_MEMORY; return 0.0; } #endif - for (I = Size; I > 0; I--) T[I] = 0.0; + for (I = Size; I > 0; I--) + T[I] = 0.0; -/* - * Part 1. Ay = e. - * Solve Ay = LUy = e where e consists of +1 and -1 terms with the sign - * chosen to maximize the size of w in Lw = e. Since the terms in w can - * get very large, scaling is used to avoid overflow. - */ + /* + * Part 1. Ay = e. + * Solve Ay = LUy = e where e consists of +1 and -1 terms with the sign + * chosen to maximize the size of w in Lw = e. Since the terms in w can + * get very large, scaling is used to avoid overflow. + */ -/* Forward elimination. Solves Lw = e while choosing e. */ + /* Forward elimination. Solves Lw = e while choosing e. */ E = 1.0; - for (I = 1; I <= Size; I++) - { pPivot = Matrix->Diag[I]; - if (T[I] < 0.0) Em = -E; else Em = E; + for (I = 1; I <= Size; I++) { + pPivot = Matrix->Diag[I]; + if (T[I] < 0.0) + Em = -E; + else + Em = E; Wm = (Em + T[I]) * pPivot->Real; - if (ABS(Wm) > SLACK) - { ScaleFactor = 1.0 / MAX( SQR( SLACK ), ABS(Wm) ); - for (K = Size; K > 0; K--) T[K] *= ScaleFactor; + if (ABS(Wm) > SLACK) { + ScaleFactor = 1.0 / MAX(SQR(SLACK), ABS(Wm)); + for (K = Size; K > 0; K--) + T[K] *= ScaleFactor; E *= ScaleFactor; Em *= ScaleFactor; Wm = (Em + T[I]) * pPivot->Real; @@ -1518,10 +1384,10 @@ RealNumber Linpack, OLeary, InvNormOfInverse, ComplexCondition(); ASp = ABS(T[I] - Em); ASm = ABS(Em + T[I]); -/* Update T for both values of W, minus value is placed in Tm. */ + /* Update T for both values of W, minus value is placed in Tm. */ pElement = pPivot->NextInCol; - while (pElement != NULL) - { Row = pElement->Row; + while (pElement != NULL) { + Row = pElement->Row; Tm[Row] = T[Row] - (Wm * pElement->Real); T[Row] -= (Wp * pElement->Real); ASp += ABS(T[Row]); @@ -1529,114 +1395,121 @@ RealNumber Linpack, OLeary, InvNormOfInverse, ComplexCondition(); pElement = pElement->NextInCol; } -/* If minus value causes more growth, overwrite T with its values. */ - if (ASm > ASp) - { T[I] = Wm; + /* If minus value causes more growth, overwrite T with its values. */ + if (ASm > ASp) { + T[I] = Wm; pElement = pPivot->NextInCol; - while (pElement != NULL) - { T[pElement->Row] = Tm[pElement->Row]; + while (pElement != NULL) { + T[pElement->Row] = Tm[pElement->Row]; pElement = pElement->NextInCol; } - } - else T[I] = Wp; + } else + T[I] = Wp; } -/* Compute 1-norm of T, which now contains w, and scale ||T|| to 1/SLACK. */ - for (ASw = 0.0, I = Size; I > 0; I--) ASw += ABS(T[I]); + /* Compute 1-norm of T, which now contains w, and scale ||T|| to 1/SLACK. */ + for (ASw = 0.0, I = Size; I > 0; I--) + ASw += ABS(T[I]); ScaleFactor = 1.0 / (SLACK * ASw); - if (ScaleFactor < 0.5) - { for (I = Size; I > 0; I--) T[I] *= ScaleFactor; + if (ScaleFactor < 0.5) { + for (I = Size; I > 0; I--) + T[I] *= ScaleFactor; E *= ScaleFactor; } -/* Backward Substitution. Solves Uy = w.*/ - for (I = Size; I >= 1; I--) - { pElement = Matrix->Diag[I]->NextInRow; - while (pElement != NULL) - { T[I] -= pElement->Real * T[pElement->Col]; + /* Backward Substitution. Solves Uy = w.*/ + for (I = Size; I >= 1; I--) { + pElement = Matrix->Diag[I]->NextInRow; + while (pElement != NULL) { + T[I] -= pElement->Real * T[pElement->Col]; pElement = pElement->NextInRow; } - if (ABS(T[I]) > SLACK) - { ScaleFactor = 1.0 / MAX( SQR( SLACK ), ABS(T[I]) ); - for (K = Size; K > 0; K--) T[K] *= ScaleFactor; + if (ABS(T[I]) > SLACK) { + ScaleFactor = 1.0 / MAX(SQR(SLACK), ABS(T[I])); + for (K = Size; K > 0; K--) + T[K] *= ScaleFactor; E *= ScaleFactor; } } -/* Compute 1-norm of T, which now contains y, and scale ||T|| to 1/SLACK. */ - for (ASy = 0.0, I = Size; I > 0; I--) ASy += ABS(T[I]); + /* Compute 1-norm of T, which now contains y, and scale ||T|| to 1/SLACK. */ + for (ASy = 0.0, I = Size; I > 0; I--) + ASy += ABS(T[I]); ScaleFactor = 1.0 / (SLACK * ASy); - if (ScaleFactor < 0.5) - { for (I = Size; I > 0; I--) T[I] *= ScaleFactor; + if (ScaleFactor < 0.5) { + for (I = Size; I > 0; I--) + T[I] *= ScaleFactor; ASy = 1.0 / SLACK; E *= ScaleFactor; } -/* Compute infinity-norm of T for O'Leary's estimate. */ + /* Compute infinity-norm of T for O'Leary's estimate. */ for (MaxY = 0.0, I = Size; I > 0; I--) - if (MaxY < ABS(T[I])) MaxY = ABS(T[I]); + if (MaxY < ABS(T[I])) + MaxY = ABS(T[I]); -/* - * Part 2. A* z = y where the * represents the transpose. - * Recall that A = LU implies A* = U* L*. - */ + /* + * Part 2. A* z = y where the * represents the transpose. + * Recall that A = LU implies A* = U* L*. + */ -/* Forward elimination, U* v = y. */ - for (I = 1; I <= Size; I++) - { pElement = Matrix->Diag[I]->NextInRow; - while (pElement != NULL) - { T[pElement->Col] -= T[I] * pElement->Real; + /* Forward elimination, U* v = y. */ + for (I = 1; I <= Size; I++) { + pElement = Matrix->Diag[I]->NextInRow; + while (pElement != NULL) { + T[pElement->Col] -= T[I] * pElement->Real; pElement = pElement->NextInRow; } - if (ABS(T[I]) > SLACK) - { ScaleFactor = 1.0 / MAX( SQR( SLACK ), ABS(T[I]) ); - for (K = Size; K > 0; K--) T[K] *= ScaleFactor; + if (ABS(T[I]) > SLACK) { + ScaleFactor = 1.0 / MAX(SQR(SLACK), ABS(T[I])); + for (K = Size; K > 0; K--) + T[K] *= ScaleFactor; ASy *= ScaleFactor; } } -/* Compute 1-norm of T, which now contains v, and scale ||T|| to 1/SLACK. */ - for (ASv = 0.0, I = Size; I > 0; I--) ASv += ABS(T[I]); + /* Compute 1-norm of T, which now contains v, and scale ||T|| to 1/SLACK. */ + for (ASv = 0.0, I = Size; I > 0; I--) + ASv += ABS(T[I]); ScaleFactor = 1.0 / (SLACK * ASv); - if (ScaleFactor < 0.5) - { for (I = Size; I > 0; I--) T[I] *= ScaleFactor; + if (ScaleFactor < 0.5) { + for (I = Size; I > 0; I--) + T[I] *= ScaleFactor; ASy *= ScaleFactor; } -/* Backward Substitution, L* z = v. */ - for (I = Size; I >= 1; I--) - { pPivot = Matrix->Diag[I]; + /* Backward Substitution, L* z = v. */ + for (I = Size; I >= 1; I--) { + pPivot = Matrix->Diag[I]; pElement = pPivot->NextInCol; - while (pElement != NULL) - { T[I] -= pElement->Real * T[pElement->Row]; + while (pElement != NULL) { + T[I] -= pElement->Real * T[pElement->Row]; pElement = pElement->NextInCol; } T[I] *= pPivot->Real; - if (ABS(T[I]) > SLACK) - { ScaleFactor = 1.0 / MAX( SQR( SLACK ), ABS(T[I]) ); - for (K = Size; K > 0; K--) T[K] *= ScaleFactor; + if (ABS(T[I]) > SLACK) { + ScaleFactor = 1.0 / MAX(SQR(SLACK), ABS(T[I])); + for (K = Size; K > 0; K--) + T[K] *= ScaleFactor; ASy *= ScaleFactor; } } -/* Compute 1-norm of T, which now contains z. */ - for (ASz = 0.0, I = Size; I > 0; I--) ASz += ABS(T[I]); + /* Compute 1-norm of T, which now contains z. */ + for (ASz = 0.0, I = Size; I > 0; I--) + ASz += ABS(T[I]); #if NOT spCOMPLEX - SP_FREE( Tm ); + SP_FREE(Tm); #endif Linpack = ASy / ASz; OLeary = E / MaxY; - InvNormOfInverse = MIN( Linpack, OLeary ); + InvNormOfInverse = MIN(Linpack, OLeary); return InvNormOfInverse / NormOfMatrix; #endif /* REAL */ } - - - - #if spCOMPLEX /* * ESTIMATE CONDITION NUMBER @@ -1659,184 +1532,191 @@ RealNumber Linpack, OLeary, InvNormOfInverse, ComplexCondition(); * spNO_MEMORY */ -static RealNumber -ComplexCondition( - MatrixPtr Matrix, - RealNumber NormOfMatrix, - int *pError -) -{ -ElementPtr pElement; -ComplexVector T, Tm; -int I, K, Row; -ElementPtr pPivot; -int Size; -RealNumber E, Em, ASp, ASm, ASw, ASy, ASv, ASz, MaxY, ScaleFactor; -RealNumber Linpack, OLeary, InvNormOfInverse; -ComplexNumber Wp, Wm; +static RealNumber ComplexCondition(MatrixPtr Matrix, RealNumber NormOfMatrix, + int *pError) { + ElementPtr pElement; + ComplexVector T, Tm; + int I, K, Row; + ElementPtr pPivot; + int Size; + RealNumber E, Em, ASp, ASm, ASw, ASy, ASv, ASz, MaxY, ScaleFactor; + RealNumber Linpack, OLeary, InvNormOfInverse; + ComplexNumber Wp, Wm; -/* Begin `ComplexCondition'. */ + /* Begin `ComplexCondition'. */ Size = Matrix->Size; T = (ComplexVector)Matrix->Intermediate; - Tm = ALLOC( ComplexNumber, Size+1 ); - if (Tm == NULL) - { *pError = spNO_MEMORY; + Tm = ALLOC(ComplexNumber, Size + 1); + if (Tm == NULL) { + *pError = spNO_MEMORY; return 0.0; } - for (I = Size; I > 0; I--) T[I].Real = T[I].Imag = 0.0; + for (I = Size; I > 0; I--) + T[I].Real = T[I].Imag = 0.0; -/* - * Part 1. Ay = e. - * Solve Ay = LUy = e where e consists of +1 and -1 terms with the sign - * chosen to maximize the size of w in Lw = e. Since the terms in w can - * get very large, scaling is used to avoid overflow. - */ + /* + * Part 1. Ay = e. + * Solve Ay = LUy = e where e consists of +1 and -1 terms with the sign + * chosen to maximize the size of w in Lw = e. Since the terms in w can + * get very large, scaling is used to avoid overflow. + */ -/* Forward elimination. Solves Lw = e while choosing e. */ + /* Forward elimination. Solves Lw = e while choosing e. */ E = 1.0; - for (I = 1; I <= Size; I++) - { pPivot = Matrix->Diag[I]; - if (T[I].Real < 0.0) Em = -E; else Em = E; + for (I = 1; I <= Size; I++) { + pPivot = Matrix->Diag[I]; + if (T[I].Real < 0.0) + Em = -E; + else + Em = E; Wm = T[I]; Wm.Real += Em; - ASm = CMPLX_1_NORM( Wm ); - CMPLX_MULT_ASSIGN( Wm, *pPivot ); - if (CMPLX_1_NORM(Wm) > SLACK) - { ScaleFactor = 1.0 / MAX( SQR( SLACK ), CMPLX_1_NORM(Wm) ); - for (K = Size; K > 0; K--) SCLR_MULT_ASSIGN( T[K], ScaleFactor ); + ASm = CMPLX_1_NORM(Wm); + CMPLX_MULT_ASSIGN(Wm, *pPivot); + if (CMPLX_1_NORM(Wm) > SLACK) { + ScaleFactor = 1.0 / MAX(SQR(SLACK), CMPLX_1_NORM(Wm)); + for (K = Size; K > 0; K--) + SCLR_MULT_ASSIGN(T[K], ScaleFactor); E *= ScaleFactor; Em *= ScaleFactor; ASm *= ScaleFactor; - SCLR_MULT_ASSIGN( Wm, ScaleFactor ); + SCLR_MULT_ASSIGN(Wm, ScaleFactor); } Wp = T[I]; Wp.Real -= Em; - ASp = CMPLX_1_NORM( Wp ); - CMPLX_MULT_ASSIGN( Wp, *pPivot ); + ASp = CMPLX_1_NORM(Wp); + CMPLX_MULT_ASSIGN(Wp, *pPivot); -/* Update T for both values of W, minus value is placed in Tm. */ + /* Update T for both values of W, minus value is placed in Tm. */ pElement = pPivot->NextInCol; - while (pElement != NULL) - { Row = pElement->Row; + while (pElement != NULL) { + Row = pElement->Row; /* Cmplx expr: Tm[Row] = T[Row] - (Wp * *pElement). */ - CMPLX_MULT_SUBT( Tm[Row], Wm, *pElement, T[Row] ); + CMPLX_MULT_SUBT(Tm[Row], Wm, *pElement, T[Row]); /* Cmplx expr: T[Row] -= Wp * *pElement. */ - CMPLX_MULT_SUBT_ASSIGN( T[Row], Wm, *pElement ); + CMPLX_MULT_SUBT_ASSIGN(T[Row], Wm, *pElement); ASp += CMPLX_1_NORM(T[Row]); ASm += CMPLX_1_NORM(Tm[Row]); pElement = pElement->NextInCol; } -/* If minus value causes more growth, overwrite T with its values. */ - if (ASm > ASp) - { T[I] = Wm; + /* If minus value causes more growth, overwrite T with its values. */ + if (ASm > ASp) { + T[I] = Wm; pElement = pPivot->NextInCol; - while (pElement != NULL) - { T[pElement->Row] = Tm[pElement->Row]; + while (pElement != NULL) { + T[pElement->Row] = Tm[pElement->Row]; pElement = pElement->NextInCol; } - } - else T[I] = Wp; + } else + T[I] = Wp; } -/* Compute 1-norm of T, which now contains w, and scale ||T|| to 1/SLACK. */ - for (ASw = 0.0, I = Size; I > 0; I--) ASw += CMPLX_1_NORM(T[I]); + /* Compute 1-norm of T, which now contains w, and scale ||T|| to 1/SLACK. */ + for (ASw = 0.0, I = Size; I > 0; I--) + ASw += CMPLX_1_NORM(T[I]); ScaleFactor = 1.0 / (SLACK * ASw); - if (ScaleFactor < 0.5) - { for (I = Size; I > 0; I--) SCLR_MULT_ASSIGN( T[I], ScaleFactor ); + if (ScaleFactor < 0.5) { + for (I = Size; I > 0; I--) + SCLR_MULT_ASSIGN(T[I], ScaleFactor); E *= ScaleFactor; } -/* Backward Substitution. Solves Uy = w.*/ - for (I = Size; I >= 1; I--) - { pElement = Matrix->Diag[I]->NextInRow; - while (pElement != NULL) - { /* Cmplx expr: T[I] -= T[pElement->Col] * *pElement. */ - CMPLX_MULT_SUBT_ASSIGN( T[I], T[pElement->Col], *pElement ); + /* Backward Substitution. Solves Uy = w.*/ + for (I = Size; I >= 1; I--) { + pElement = Matrix->Diag[I]->NextInRow; + while (pElement != + NULL) { /* Cmplx expr: T[I] -= T[pElement->Col] * *pElement. */ + CMPLX_MULT_SUBT_ASSIGN(T[I], T[pElement->Col], *pElement); pElement = pElement->NextInRow; } - if (CMPLX_1_NORM(T[I]) > SLACK) - { ScaleFactor = 1.0 / MAX( SQR( SLACK ), CMPLX_1_NORM(T[I]) ); - for (K = Size; K > 0; K--) SCLR_MULT_ASSIGN( T[K], ScaleFactor ); + if (CMPLX_1_NORM(T[I]) > SLACK) { + ScaleFactor = 1.0 / MAX(SQR(SLACK), CMPLX_1_NORM(T[I])); + for (K = Size; K > 0; K--) + SCLR_MULT_ASSIGN(T[K], ScaleFactor); E *= ScaleFactor; } } -/* Compute 1-norm of T, which now contains y, and scale ||T|| to 1/SLACK. */ - for (ASy = 0.0, I = Size; I > 0; I--) ASy += CMPLX_1_NORM(T[I]); + /* Compute 1-norm of T, which now contains y, and scale ||T|| to 1/SLACK. */ + for (ASy = 0.0, I = Size; I > 0; I--) + ASy += CMPLX_1_NORM(T[I]); ScaleFactor = 1.0 / (SLACK * ASy); - if (ScaleFactor < 0.5) - { for (I = Size; I > 0; I--) SCLR_MULT_ASSIGN( T[I], ScaleFactor ); + if (ScaleFactor < 0.5) { + for (I = Size; I > 0; I--) + SCLR_MULT_ASSIGN(T[I], ScaleFactor); ASy = 1.0 / SLACK; E *= ScaleFactor; } -/* Compute infinity-norm of T for O'Leary's estimate. */ + /* Compute infinity-norm of T for O'Leary's estimate. */ for (MaxY = 0.0, I = Size; I > 0; I--) - if (MaxY < CMPLX_1_NORM(T[I])) MaxY = CMPLX_1_NORM(T[I]); + if (MaxY < CMPLX_1_NORM(T[I])) + MaxY = CMPLX_1_NORM(T[I]); -/* - * Part 2. A* z = y where the * represents the transpose. - * Recall that A = LU implies A* = U* L*. - */ + /* + * Part 2. A* z = y where the * represents the transpose. + * Recall that A = LU implies A* = U* L*. + */ -/* Forward elimination, U* v = y. */ - for (I = 1; I <= Size; I++) - { pElement = Matrix->Diag[I]->NextInRow; - while (pElement != NULL) - { /* Cmplx expr: T[pElement->Col] -= T[I] * *pElement. */ - CMPLX_MULT_SUBT_ASSIGN( T[pElement->Col], T[I], *pElement ); + /* Forward elimination, U* v = y. */ + for (I = 1; I <= Size; I++) { + pElement = Matrix->Diag[I]->NextInRow; + while (pElement != + NULL) { /* Cmplx expr: T[pElement->Col] -= T[I] * *pElement. */ + CMPLX_MULT_SUBT_ASSIGN(T[pElement->Col], T[I], *pElement); pElement = pElement->NextInRow; } - if (CMPLX_1_NORM(T[I]) > SLACK) - { ScaleFactor = 1.0 / MAX( SQR( SLACK ), CMPLX_1_NORM(T[I]) ); - for (K = Size; K > 0; K--) SCLR_MULT_ASSIGN( T[K], ScaleFactor ); + if (CMPLX_1_NORM(T[I]) > SLACK) { + ScaleFactor = 1.0 / MAX(SQR(SLACK), CMPLX_1_NORM(T[I])); + for (K = Size; K > 0; K--) + SCLR_MULT_ASSIGN(T[K], ScaleFactor); ASy *= ScaleFactor; } } -/* Compute 1-norm of T, which now contains v, and scale ||T|| to 1/SLACK. */ - for (ASv = 0.0, I = Size; I > 0; I--) ASv += CMPLX_1_NORM(T[I]); + /* Compute 1-norm of T, which now contains v, and scale ||T|| to 1/SLACK. */ + for (ASv = 0.0, I = Size; I > 0; I--) + ASv += CMPLX_1_NORM(T[I]); ScaleFactor = 1.0 / (SLACK * ASv); - if (ScaleFactor < 0.5) - { for (I = Size; I > 0; I--) SCLR_MULT_ASSIGN( T[I], ScaleFactor ); + if (ScaleFactor < 0.5) { + for (I = Size; I > 0; I--) + SCLR_MULT_ASSIGN(T[I], ScaleFactor); ASy *= ScaleFactor; } -/* Backward Substitution, L* z = v. */ - for (I = Size; I >= 1; I--) - { pPivot = Matrix->Diag[I]; + /* Backward Substitution, L* z = v. */ + for (I = Size; I >= 1; I--) { + pPivot = Matrix->Diag[I]; pElement = pPivot->NextInCol; - while (pElement != NULL) - { /* Cmplx expr: T[I] -= T[pElement->Row] * *pElement. */ - CMPLX_MULT_SUBT_ASSIGN( T[I], T[pElement->Row], *pElement ); + while (pElement != + NULL) { /* Cmplx expr: T[I] -= T[pElement->Row] * *pElement. */ + CMPLX_MULT_SUBT_ASSIGN(T[I], T[pElement->Row], *pElement); pElement = pElement->NextInCol; } - CMPLX_MULT_ASSIGN( T[I], *pPivot ); - if (CMPLX_1_NORM(T[I]) > SLACK) - { ScaleFactor = 1.0 / MAX( SQR( SLACK ), CMPLX_1_NORM(T[I]) ); - for (K = Size; K > 0; K--) SCLR_MULT_ASSIGN( T[K], ScaleFactor ); + CMPLX_MULT_ASSIGN(T[I], *pPivot); + if (CMPLX_1_NORM(T[I]) > SLACK) { + ScaleFactor = 1.0 / MAX(SQR(SLACK), CMPLX_1_NORM(T[I])); + for (K = Size; K > 0; K--) + SCLR_MULT_ASSIGN(T[K], ScaleFactor); ASy *= ScaleFactor; } } -/* Compute 1-norm of T, which now contains z. */ - for (ASz = 0.0, I = Size; I > 0; I--) ASz += CMPLX_1_NORM(T[I]); + /* Compute 1-norm of T, which now contains z. */ + for (ASz = 0.0, I = Size; I > 0; I--) + ASz += CMPLX_1_NORM(T[I]); - SP_FREE( Tm ); + SP_FREE(Tm); Linpack = ASy / ASz; OLeary = E / MaxY; - InvNormOfInverse = MIN( Linpack, OLeary ); + InvNormOfInverse = MIN(Linpack, OLeary); return InvNormOfInverse / NormOfMatrix; } #endif /* spCOMPLEX */ - - - - /*! * Computes the L-infinity norm of an unfactored matrix. It is a fatal * error to pass this routine a factored matrix. @@ -1848,43 +1728,44 @@ ComplexNumber Wp, Wm; * Pointer to the matrix. */ -spREAL -spNorm( MatrixPtr Matrix ) -{ -ElementPtr pElement; -int I; -RealNumber Max = 0.0, AbsRowSum; +spREAL spNorm(MatrixPtr Matrix) { + ElementPtr pElement; + int I; + RealNumber Max = 0.0, AbsRowSum; -/* Begin `spNorm'. */ - ASSERT_IS_SPARSE( Matrix ); - ASSERT_NO_ERRORS( Matrix ); - ASSERT_IS_NOT_FACTORED( Matrix ); - if (NOT Matrix->RowsLinked) spcLinkRows( Matrix ); + /* Begin `spNorm'. */ + ASSERT_IS_SPARSE(Matrix); + ASSERT_NO_ERRORS(Matrix); + ASSERT_IS_NOT_FACTORED(Matrix); + if (NOT Matrix->RowsLinked) + spcLinkRows(Matrix); /* Compute row sums. */ #if REAL - if (NOT Matrix->Complex) - { for (I = Matrix->Size; I > 0; I--) - { pElement = Matrix->FirstInRow[I]; + if (NOT Matrix->Complex) { + for (I = Matrix->Size; I > 0; I--) { + pElement = Matrix->FirstInRow[I]; AbsRowSum = 0.0; - while (pElement != NULL) - { AbsRowSum += ABS( pElement->Real ); + while (pElement != NULL) { + AbsRowSum += ABS(pElement->Real); pElement = pElement->NextInRow; } - if (Max < AbsRowSum) Max = AbsRowSum; + if (Max < AbsRowSum) + Max = AbsRowSum; } } #endif #if spCOMPLEX - if (Matrix->Complex) - { for (I = Matrix->Size; I > 0; I--) - { pElement = Matrix->FirstInRow[I]; + if (Matrix->Complex) { + for (I = Matrix->Size; I > 0; I--) { + pElement = Matrix->FirstInRow[I]; AbsRowSum = 0.0; - while (pElement != NULL) - { AbsRowSum += CMPLX_1_NORM( *pElement ); + while (pElement != NULL) { + AbsRowSum += CMPLX_1_NORM(*pElement); pElement = pElement->NextInRow; } - if (Max < AbsRowSum) Max = AbsRowSum; + if (Max < AbsRowSum) + Max = AbsRowSum; } } #endif @@ -1892,11 +1773,6 @@ RealNumber Max = 0.0, AbsRowSum; } #endif /* CONDITION */ - - - - - #if STABILITY /*! * This routine, along with spRoundoff(), are used to gauge the stability of a @@ -1962,53 +1838,56 @@ RealNumber Max = 0.0, AbsRowSum; * Pointer to the matrix. */ -spREAL -spLargestElement( MatrixPtr Matrix ) -{ -int I; -RealNumber Mag, AbsColSum, Max = 0.0, MaxRow = 0.0, MaxCol = 0.0; -RealNumber Pivot; -ComplexNumber cPivot; -ElementPtr pElement, pDiag; +spREAL spLargestElement(MatrixPtr Matrix) { + int I; + RealNumber Mag, AbsColSum, Max = 0.0, MaxRow = 0.0, MaxCol = 0.0; + RealNumber Pivot; + ComplexNumber cPivot; + ElementPtr pElement, pDiag; -/* Begin `spLargestElement'. */ - ASSERT_IS_SPARSE( Matrix ); + /* Begin `spLargestElement'. */ + ASSERT_IS_SPARSE(Matrix); #if REAL - if (Matrix->Factored AND NOT Matrix->Complex) - { if (Matrix->Error == spSINGULAR) return 0.0; + if (Matrix->Factored AND NOT Matrix->Complex) { + if (Matrix->Error == spSINGULAR) + return 0.0; -/* Find the bound on the size of the largest element over all factorization. */ - for (I = 1; I <= Matrix->Size; I++) - { pDiag = Matrix->Diag[I]; + /* Find the bound on the size of the largest element over all + * factorization. */ + for (I = 1; I <= Matrix->Size; I++) { + pDiag = Matrix->Diag[I]; -/* Lower triangular matrix. */ + /* Lower triangular matrix. */ Pivot = 1.0 / pDiag->Real; - Mag = ABS( Pivot ); - if (Mag > MaxRow) MaxRow = Mag; + Mag = ABS(Pivot); + if (Mag > MaxRow) + MaxRow = Mag; pElement = Matrix->FirstInRow[I]; - while (pElement != pDiag) - { Mag = ABS( pElement->Real ); - if (Mag > MaxRow) MaxRow = Mag; + while (pElement != pDiag) { + Mag = ABS(pElement->Real); + if (Mag > MaxRow) + MaxRow = Mag; pElement = pElement->NextInRow; } -/* Upper triangular matrix. */ + /* Upper triangular matrix. */ pElement = Matrix->FirstInCol[I]; - AbsColSum = 1.0; /* Diagonal of U is unity. */ - while (pElement != pDiag) - { AbsColSum += ABS( pElement->Real ); + AbsColSum = 1.0; /* Diagonal of U is unity. */ + while (pElement != pDiag) { + AbsColSum += ABS(pElement->Real); pElement = pElement->NextInCol; } - if (AbsColSum > MaxCol) MaxCol = AbsColSum; + if (AbsColSum > MaxCol) + MaxCol = AbsColSum; } - } - else if (NOT Matrix->Complex) - { for (I = 1; I <= Matrix->Size; I++) - { pElement = Matrix->FirstInCol[I]; - while (pElement != NULL) - { Mag = ABS( pElement->Real ); - if (Mag > Max) Max = Mag; + } else if (NOT Matrix->Complex) { + for (I = 1; I <= Matrix->Size; I++) { + pElement = Matrix->FirstInCol[I]; + while (pElement != NULL) { + Mag = ABS(pElement->Real); + if (Mag > Max) + Max = Mag; pElement = pElement->NextInCol; } } @@ -2016,40 +1895,45 @@ ElementPtr pElement, pDiag; } #endif #if spCOMPLEX - if (Matrix->Factored AND Matrix->Complex) - { if (Matrix->Error == spSINGULAR) return 0.0; + if (Matrix->Factored AND Matrix->Complex) { + if (Matrix->Error == spSINGULAR) + return 0.0; -/* Find the bound on the size of the largest element over all factorization. */ - for (I = 1; I <= Matrix->Size; I++) - { pDiag = Matrix->Diag[I]; + /* Find the bound on the size of the largest element over all + * factorization. */ + for (I = 1; I <= Matrix->Size; I++) { + pDiag = Matrix->Diag[I]; -/* Lower triangular matrix. */ - CMPLX_RECIPROCAL( cPivot, *pDiag ); - Mag = CMPLX_INF_NORM( cPivot ); - if (Mag > MaxRow) MaxRow = Mag; + /* Lower triangular matrix. */ + CMPLX_RECIPROCAL(cPivot, *pDiag); + Mag = CMPLX_INF_NORM(cPivot); + if (Mag > MaxRow) + MaxRow = Mag; pElement = Matrix->FirstInRow[I]; - while (pElement != pDiag) - { Mag = CMPLX_INF_NORM( *pElement ); - if (Mag > MaxRow) MaxRow = Mag; + while (pElement != pDiag) { + Mag = CMPLX_INF_NORM(*pElement); + if (Mag > MaxRow) + MaxRow = Mag; pElement = pElement->NextInRow; } -/* Upper triangular matrix. */ + /* Upper triangular matrix. */ pElement = Matrix->FirstInCol[I]; - AbsColSum = 1.0; /* Diagonal of U is unity. */ - while (pElement != pDiag) - { AbsColSum += CMPLX_INF_NORM( *pElement ); + AbsColSum = 1.0; /* Diagonal of U is unity. */ + while (pElement != pDiag) { + AbsColSum += CMPLX_INF_NORM(*pElement); pElement = pElement->NextInCol; } - if (AbsColSum > MaxCol) MaxCol = AbsColSum; + if (AbsColSum > MaxCol) + MaxCol = AbsColSum; } - } - else if (Matrix->Complex) - { for (I = 1; I <= Matrix->Size; I++) - { pElement = Matrix->FirstInCol[I]; - while (pElement != NULL) - { Mag = CMPLX_INF_NORM( *pElement ); - if (Mag > Max) Max = Mag; + } else if (Matrix->Complex) { + for (I = 1; I <= Matrix->Size; I++) { + pElement = Matrix->FirstInCol[I]; + while (pElement != NULL) { + Mag = CMPLX_INF_NORM(*pElement); + if (Mag > Max) + Max = Mag; pElement = pElement->NextInCol; } } @@ -2059,9 +1943,6 @@ ElementPtr pElement, pDiag; return MaxRow * MaxCol; } - - - /*! * This routine, along with spLargestElement(), are used to gauge the * stability of a factorization. See description of spLargestElement() @@ -2080,41 +1961,39 @@ ElementPtr pElement, pDiag; * negative, the bound will be computed automatically. */ -spREAL -spRoundoff( - MatrixPtr Matrix, - spREAL Rho -) -{ -ElementPtr pElement; -int Count, I, MaxCount = 0; -RealNumber Reid, Gear; +spREAL spRoundoff(MatrixPtr Matrix, spREAL Rho) { + ElementPtr pElement; + int Count, I, MaxCount = 0; + RealNumber Reid, Gear; -/* Begin `spRoundoff'. */ - ASSERT_IS_SPARSE( Matrix ); - ASSERT_NO_ERRORS( Matrix ); - ASSERT_IS_FACTORED( Matrix ); + /* Begin `spRoundoff'. */ + ASSERT_IS_SPARSE(Matrix); + ASSERT_NO_ERRORS(Matrix); + ASSERT_IS_FACTORED(Matrix); -/* Compute Barlow's bound if it is not given. */ - if (Rho < 0.0) Rho = spLargestElement( Matrix ); + /* Compute Barlow's bound if it is not given. */ + if (Rho < 0.0) + Rho = spLargestElement(Matrix); -/* Find the maximum number of off-diagonals in L if not previously computed. */ - if (Matrix->MaxRowCountInLowerTri < 0) - { for (I = Matrix->Size; I > 0; I--) - { pElement = Matrix->FirstInRow[I]; + /* Find the maximum number of off-diagonals in L if not previously computed. + */ + if (Matrix->MaxRowCountInLowerTri < 0) { + for (I = Matrix->Size; I > 0; I--) { + pElement = Matrix->FirstInRow[I]; Count = 0; - while (pElement->Col < I) - { Count++; + while (pElement->Col < I) { + Count++; pElement = pElement->NextInRow; } - if (Count > MaxCount) MaxCount = Count; + if (Count > MaxCount) + MaxCount = Count; } Matrix->MaxRowCountInLowerTri = MaxCount; - } - else MaxCount = Matrix->MaxRowCountInLowerTri; + } else + MaxCount = Matrix->MaxRowCountInLowerTri; -/* Compute error bound. */ - Gear = 1.01*((MaxCount + 1) * Matrix->RelThreshold + 1.0) * SQR(MaxCount); + /* Compute error bound. */ + Gear = 1.01 * ((MaxCount + 1) * Matrix->RelThreshold + 1.0) * SQR(MaxCount); Reid = 3.01 * Matrix->Size; if (Gear < Reid) @@ -2124,12 +2003,6 @@ RealNumber Reid, Gear; } #endif - - - - - - #if DOCUMENTATION /*! * This routine prints a short message describing the error error state @@ -2145,56 +2018,53 @@ RealNumber Reid, Gear; * If zero-length string, no originator is printed. */ -void -spErrorMessage( - MatrixPtr Matrix, - FILE *Stream, - char *Originator -) -{ -int Row, Col, Error; +void spErrorMessage(MatrixPtr Matrix, FILE *Stream, char *Originator) { + int Row, Col, Error; -/* Begin `spErrorMessage'. */ + /* Begin `spErrorMessage'. */ if (Matrix == NULL) - Error = spNO_MEMORY; - else - { ASSERT_IS_SPARSE( Matrix ); - Error = Matrix->Error; + Error = spNO_MEMORY; + else { + ASSERT_IS_SPARSE(Matrix); + Error = Matrix->Error; } - if (Error == spOKAY) return; - if (Originator == NULL) Originator = "sparse"; - if (Stream == NULL) Stream = stderr; - if (Originator[0] != '\0') fprintf( Stream, "%s: ", Originator ); + if (Error == spOKAY) + return; + if (Originator == NULL) + Originator = "sparse"; + if (Stream == NULL) + Stream = stderr; + if (Originator[0] != '\0') + fprintf(Stream, "%s: ", Originator); if (Error >= spFATAL) - fprintf( Stream, "fatal error: "); + fprintf(Stream, "fatal error: "); else - fprintf( Stream, "warning: "); -/* - * Print particular error message. - * Do not use switch statement because error codes may not be unique. - */ + fprintf(Stream, "warning: "); + /* + * Print particular error message. + * Do not use switch statement because error codes may not be unique. + */ if (Error == spPANIC) - fprintf( Stream, "Sparse called improperly.\n"); + fprintf(Stream, "Sparse called improperly.\n"); else if (Error == spNO_MEMORY) - fprintf( Stream, "insufficient memory available.\n"); + fprintf(Stream, "insufficient memory available.\n"); else if (Error == spMANGLED) - fprintf( Stream, "matrix is mangled.\n"); - else if (Error == spSINGULAR) - { spWhereSingular( Matrix, &Row, &Col ); - fprintf( Stream, "singular matrix detected at row %d and column %d.\n", - Row, Col); - } - else if (Error == spZERO_DIAG) - { spWhereSingular( Matrix, &Row, &Col ); - fprintf( Stream, "zero diagonal detected at row %d and column %d.\n", - Row, Col); - } - else if (Error == spSMALL_PIVOT) - { fprintf( Stream, - "unable to find a pivot that is larger than absolute threshold.\n"); - } - else ABORT(); + fprintf(Stream, "matrix is mangled.\n"); + else if (Error == spSINGULAR) { + spWhereSingular(Matrix, &Row, &Col); + fprintf(Stream, "singular matrix detected at row %d and column %d.\n", + Row, Col); + } else if (Error == spZERO_DIAG) { + spWhereSingular(Matrix, &Row, &Col); + fprintf(Stream, "zero diagonal detected at row %d and column %d.\n", + Row, Col); + } else if (Error == spSMALL_PIVOT) { + fprintf( + Stream, + "unable to find a pivot that is larger than absolute threshold.\n"); + } else + ABORT(); if (Matrix != NULL) Matrix->Error = spOKAY;