#include <stdio.h>
#include "spConfig.h"
#include "spMatrix.h"
#include "spDefs.h"
Defines | |
#define | spINSIDE_SPARSE |
#define | sfCreate sfcreate |
#define | sfStripFills sfstripfills |
#define | sfDestroy sfdestroy |
#define | sfClear sfclear |
#define | sfGetElement sfgetelement |
#define | sfGetAdmittance sfgetadmittance |
#define | sfGetQuad sfgetquad |
#define | sfGetOnes sfgetones |
#define | sfAdd1Real sfadd1real |
#define | sfAdd1Imag sfadd1imag |
#define | sfAdd1Complex sfadd1complex |
#define | sfAdd4Real sfadd4real |
#define | sfAdd4Imag sfadd4imag |
#define | sfAdd4Complex sfadd4complex |
#define | sfOrderAndFactor sforderandfactor |
#define | sfFactor sffactor |
#define | sfPartition sfpartition |
#define | sfSolve sfsolve |
#define | sfSolveTransposed sfsolvetransposed |
#define | sfPrint sfprint |
#define | sfFileMatrix sffilematrix |
#define | sfFileVector sffilevector |
#define | sfFileStats sffilestats |
#define | sfMNA_Preorder sfmna_preorder |
#define | sfScale sfscale |
#define | sfMultiply sfmultiply |
#define | sfMultTransposed sfmulttransposed |
#define | sfDeterminant sfdeterminant |
#define | sfError sferror |
#define | sfErrorMessage sferrormessage |
#define | sfWhereSingular sfwheresingular |
#define | sfGetSize sfgetsize |
#define | sfSetReal sfsetreal |
#define | sfSetComplex sfsetcomplex |
#define | sfFillinCount sffillincount |
#define | sfElementCount sfelementcount |
#define | sfDeleteRowAndCol sfdeleterowandcol |
#define | sfPseudoCondition sfpseudocondition |
#define | sfCondition sfcondition |
#define | sfNorm sfnorm |
#define | sfLargestElement sflargestelement |
#define | sfRoundoff sfroundoff |
#define | MATRIX_FILE_NAME "spMatrix" |
#define | STATS_FILE_NAME "spStats" |
Functions | |
long | sfCreate (int *Size, int *Complex, int *Error) |
void | sfDestroy (long *Matrix) |
void | sfStripFills (long *Matrix) |
void | sfClear (long *Matrix) |
long | sfGetElement (long *Matrix, int *Row, int *Col) |
int | sfGetAdmittance (long *Matrix, int *Node1, int *Node2, long Template[4]) |
int | sfGetQuad (long *Matrix, int *Row1, int *Row2, int *Col1, int *Col2, long Template[4]) |
int | sfGetOnes (long *Matrix, int *Pos, int *Neg, int *Eqn, long Template[4]) |
void | sfAdd1Real (long *Element, spREAL *Real) |
void | sfAdd1Imag (long *Element, spREAL *Imag) |
void | sfAdd1Complex (long *Element, spREAL *Real, spREAL *Imag) |
void | sfAdd4Real (long Template[4], spREAL *Real) |
void | sfAdd4Imag (long Template[4], spREAL *Imag) |
void | sfAdd4Complex (long Template[4], spREAL *Real, spREAL *Imag) |
int | sfOrderAndFactor (long *Matrix, spREAL RHS[], spREAL *RelThreshold, spREAL *AbsThreshold, long *DiagPivoting) |
int | sfFactor (long *Matrix) |
void | sfPartition (long *Matrix, int *Mode) |
void | sfSolve (long *Matrix, spREAL RHS[], spREAL Solution[]) |
void | sfSolveTransposed (long *Matrix, spREAL RHS[], spREAL Solution[]) |
void | sfPrint (long *Matrix, long *PrintReordered, long *Data, long *Header) |
long | sfFileMatrix (long *Matrix, long *Reordered, long *Data, long *Header) |
int | sfFileVector (long *Matrix, spREAL RHS[]) |
int | sfFileStats (long *Matrix) |
void | sfMNA_Preorder (long *Matrix) |
void | sfScale (long *Matrix, spREAL RHS_ScaleFactors[], spREAL SolutionScaleFactors[]) |
void | sfMultiply (long *Matrix, spREAL RHS[], spREAL Solution[]) |
void | sfMultTransposed (long *Matrix, spREAL RHS[], spREAL Solution[]) |
void | sfDeterminant (long *Matrix, spREAL *pDeterminant, spREAL *piDeterminant, int *pExponent) |
int | sfErrorState (long *Matrix) |
void | sfErrorMessage (long *Matrix) |
void | sfWhereSingular (long *Matrix, int *Row, int *Col) |
int | sfGetSize (long *Matrix, long *External) |
void | sfSetReal (long *Matrix) |
void | sfSetComplex (long *Matrix) |
int | sfFillinCount (long *Matrix) |
int | sfElementCount (long *Matrix) |
void | sfDeleteRowAndCol (long *Matrix, int *Row, int *Col) |
spREAL | sfPseudoCondition (long *Matrix) |
spREAL | sfCondition (long *Matrix, spREAL *NormOfMatrix, int *pError) |
spREAL | sfNorm (long *Matrix) |
spREAL | sfLargestElement (long *Matrix) |
spREAL | sfRoundoff (long *Matrix, spREAL *Rho) |
To ease porting this file to different operating systems, the names of the functions can be easily redefined (search for `Routine Renaming'). A simple example of a FORTRAN program that calls Sparse is included in this file (search for Example). When interfacing to a FORTRAN program, the ARRAY_OFFSET option should be set to NO (see spConfig.h).
DISCLAIMER: These interface routines were written by a C programmer who has little experience with FORTRAN. The routines have had minimal testing. Any interface between two languages is going to have portability problems, this one is no exception.
|
Adds a complex value to a matrix element. These elements are referenced by pointer, and so must already have been created by spGetElement().
|
|
Adds an imaginary value to a matrix element. These elements are referenced by pointer, and so must already have been created by spGetElement().
|
|
Adds a real value to a matrix element. These elements are referenced by pointer, and so must already have been created by spGetElement().
|
|
Adds a complex value to a set of four elements in a matrix. These elements are referenced by pointer, and so must already have been created by spGetAdmittance(), spGetQuad(), or spGetOnes().
|
|
Adds an imaginary value to a set of four elements in a matrix. These elements are referenced by pointer, and so must already have been created by spGetAdmittance(), spGetQuad(), or spGetOnes().
|
|
Adds a real value to a set of four elements in a matrix. These elements are referenced by pointer, and so must already have been created by spGetAdmittance(), spGetQuad(), or spGetOnes().
|
|
Sets every element of the matrix to zero and clears the error flag.
|
|
Computes an estimate of the condition number using a variation on the LINPACK condition number estimation algorithm. This quantity is an indicator of ill-conditioning in the matrix. To avoid problems with overflow, the reciprocal of the condition number is returned. If this number is small, and if the matrix is scaled such that uncertainties in the RHS and the matrix entries are equilibrated, then the matrix is ill-conditioned. If the this number is near one, the matrix is well conditioned. This routine must only be used after a matrix has been factored by sfOrderAndFactor() or sfFactor() and before it is cleared by sfClear() or spInitialize(). Unlike the LINPACK condition number estimator, this routines returns the L infinity condition number. This is an artifact of Sparse placing ones on the diagonal of the upper triangular matrix rather than the lower. This difference should be of no importance. References: A.K. Cline, C.B. Moler, G.W. Stewart, J.H. Wilkinson. An estimate for the condition number of a matrix. SIAM Journal on Numerical Analysis. Vol. 16, No. 2, pages 368-375, April 1979. J.J. Dongarra, C.B. Moler, J.R. Bunch, G.W. Stewart. LINPACK User's Guide. SIAM, 1979. Roger G. Grimes, John G. Lewis. Condition number estimation for sparse matrices. SIAM Journal on Scientific and Statistical Computing. Vol. 2, No. 4, pages 384-388, December 1981. Dianne Prost O'Leary. Estimating matrix condition numbers. SIAM Journal on Scientific and Statistical Computing. Vol. 1, No. 2, pages 205-209, June 1980.
|
|
Allocates and initializes the data structures associated with a matrix.
|
|
Deletes a row and a column from a matrix. Sparse will abort if an attempt is made to delete a row or column that doesn't exist.
|
|
Deallocates pointers and elements of matrix.
|
|
This routine in capable of calculating the determinant of the matrix once the LU factorization has been performed. Hence, only use this routine after spFactor() and before spClear(). The determinant equals the product of all the diagonal elements of the lower triangular matrix L, except that this product may need negating. Whether the product or the negative product equals the determinant is determined by the number of row and column interchanges performed. Note that the determinants of matrices can be very large or very small. On large matrices, the determinant can be far larger or smaller than can be represented by a floating point number. For this reason the determinant is scaled to a reasonable value and the logarithm of the scale factor is returned.
|
|
Returns the total number of total elements in the matrix. >>> Arguments: Matrix [INTEGER] Pointer to matrix. |
|
This function prints a Sparse error message to stderr.
|
|
This function is used to determine the error status of the given matrix.
|
|
This routine is the companion routine to spOrderAndFactor(). Unlike sfOrderAndFactor(), sfFactor() cannot change the ordering. It is also faster than sfOrderAndFactor(). The standard way of using these two routines is to first use sfOrderAndFactor() for the initial factorization. For subsequent factorizations, sfFactor() is used if there is some assurance that little growth will occur (say for example, that the matrix is diagonally dominant). If sfFactor() is called for the initial factorization of the matrix, then sfOrderAndFactor() is automatically called with the default threshold. This routine uses "row at a time" LU factorization. Pivots are associated with the lower triangular matrix and the diagonals of the upper triangular matrix are ones.
|
|
Writes matrix to file in format suitable to be read back in by the matrix test program. Data is sent to a file with a fixed name (MATRIX_FILE_NAME) because it is impossible to pass strings from FORTRAN to C in a manner that is portable.
|
|
Writes useful information concerning the matrix to a file. Should be executed after the matrix is factored. Data is sent to a file with a fixed name (STATS_FILE_NAME) because it is impossible to pass strings from FORTRAN to C in a manner that is portable.
|
|
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 sfFileMatrix.
|
|
Returns the number of fill-ins in the matrix. >>> Arguments: Matrix [INTEGER] Pointer to matrix. |
|
Performs same function as sfGetElement() except rather than one element, all four Matrix elements for a floating component are added. This routine also works if component is grounded. Positive elements are placed at [Node1,Node2] and [Node2,Node1]. This routine is only to be used after sfCreate() and before sfMNA_Preorder(), sfFactor() or sfOrderAndFactor().
|
|
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 is only to be used after spCreate() and before spMNA_Preorder(), spFactor() or spOrderAndFactor(). Returns a pointer to the Real portion of a matrix element. This pointer is later used by sfAddxxxxx() to directly access element.
|
|
Performs similar function to sfGetQuad() except this routine is meant for components that do not have an admittance representation. The following stamp is used: Pos Neg Eqn Pos [ . . 1 ] Neg [ . . -1 ] Eqn [ 1 -1 . ]
|
|
Similar to sfGetAdmittance(), except that sfGetAdmittance() only handles 2-terminal components, whereas sfGetQuad() handles simple 4-terminals as well. These 4-terminals are simply generalized 2-terminals with the option of having the sense terminals different from the source and sink terminals. sfGetQuad() adds four elements to the matrix. Positive elements occur at Row1,Col1 Row2,Col2 while negative elements occur at Row1,Col2 and Row2,Col1. The routine works fine if any of the rows and columns are zero. This routine is only to be used after sfCreate() and before sfMNA_Preorder(), sfFactor() or sfOrderAndFactor() unless TRANSLATE is set true.
|
|
Returns the size of the matrix. Either the internal or external size of the matrix is returned.
|
|
spLargestElement() finds the magnitude on the largest element in the matrix. If the matrix has not yet been factored, the largest element is found by direct search. If the matrix is factored, a bound on the largest element in any of the reduced submatrices is computed.
|
|
This routine massages modified node admittance matrices to remove zeros from the diagonal. It takes advantage of the fact that the row and column associated with a zero diagonal usually have structural ones placed symmetricly. This routine should be used only on modified node admittance matrices and should be executed after the matrix has been built but before the factorization begins. It should be executed for the initial factorization only and should be executed before the rows have been linked. Thus it should be run before using spScale(), spMultiply(), spDeleteRowAndCol(), or spNorm(). This routine exploits the fact that the structural one are placed in the matrix in symmetric twins. For example, the stamps for grounded and a floating voltage sources are grounded: floating: [ x x 1 ] [ x x 1 ] [ x x ] [ x x -1 ] [ 1 ] [ 1 -1 ] [ 1 ] [ 1 -1 ] [ x x 1 ] [ x x ] [ x x -1 ] [ 1 -1 ] [ x x 1 ] [ x x 1 ] [ x x -1 ] It is important to deal with any zero diagonals that only have one set of twins before dealing with those that have more than one because swapping row destroys the symmetry of any twins in the rows being swapped, which may limit future moves. Consider [ x x 1 ] [ x x -1 1 ] [ 1 -1 ] [ 1 ] [ x x 1 ] [ 1 ] [ 1 -1 ] [ x x -1 1 ] [ 1 -1 ] [ 1 ] [ x x 1 ] [ x x -1 1 ] [ x x 1 ] [ 1 -1 ] [ x x -1 1 ] [ 1 ] So we always take care of lone twins first. When none remain, we choose arbitrarily a set of twins for a diagonal with more than one set and swap the rows corresponding to that twin. We then deal with any lone twins that were created and repeat the procedure until no zero diagonals with symmetric twins remain. In this particular implementation, columns are swapped rather than rows. The algorithm used in this function was developed by Ken Kundert and Tom Quarles.
|
|
Multiplies matrix by solution vector to find source vector. Assumes matrix has not been factored. This routine can be used as a test to see if solutions are correct. It should not be used before PreorderFoModifiedNodal().
|
|
Multiplies transposed matrix by solution vector to find source vector. Assumes matrix has not been factored. This routine can be used as a test to see if solutions are correct. It should not be used before PreorderFoModifiedNodal().
|
|
Computes the L-infinity norm of an unfactored matrix. It is a fatal error to pass this routine a factored matrix.
|
|
This routine chooses a pivot order for the matrix and factors it into LU form. It handles both the initial factorization and subsequent factorizations when a reordering is desired. This is handled in a manner that is transparent to the user. The routine uses a variation of Gauss's method where the pivots are associated with L and the diagonal terms of U are one.
|
|
This routine determines the cost to factor each row using both direct and indirect addressing and decides, on a row-by-row basis, which addressing mode is fastest. This information is used in sfFactor() to speed the factorization. When factoring a previously ordered matrix using sfFactor(), Sparse operates on a row-at-a-time basis. For speed, on each step, the row being updated is copied into a full vector and the operations are performed on that vector. This can be done one of two ways, either using direct addressing or indirect addressing. Direct addressing is fastest when the matrix is relatively dense and indirect addressing is best when the matrix is quite sparse. The user selects the type of partition used with Mode. If Mode is set to spDIRECT_PARTITION, then the all rows are placed in the direct addressing partition. Similarly, if Mode is set to spINDIRECT_PARTITION, then the all rows are placed in the indirect addressing partition. By setting Mode to spAUTO_PARTITION, the user allows Sparse to select the partition for each row individually. sfFactor() generally runs faster if Sparse is allowed to choose its own partitioning, however choosing a partition is expensive. The time required to choose a partition is of the same order of the cost to factor the matrix. If you plan to factor a large number of matrices with the same structure, it is best to let Sparse choose the partition. Otherwise, you should choose the partition based on the predicted density of the matrix.
|
|
Formats and send the matrix to standard output. Some elementary statistics are also output. The matrix is output in a format that is readable by people.
|
|
Computes the magnitude of the ratio of the largest to the smallest pivots. This quantity is an indicator of ill-conditioning in the matrix. If this ratio is large, and if the matrix is scaled such that uncertainties in the RHS and the matrix entries are equilibrated, then the matrix is ill-conditioned. However, a small ratio does not necessarily imply that the matrix is well-conditioned. This routine must only be used after a matrix has been factored by sfOrderAndFactor() or sfFactor() and before it is cleared by sfClear() or spInitialize(). The pseudocondition is faster to compute than the condition number calculated by sfCondition(), but is not as informative.
|
|
This routine, along with spLargestElement(), are used to gauge the stability of a factorization. See description of spLargestElement() for more information.
|
|
This function scales the matrix to enhance the possibility of finding a good pivoting order. Note that scaling enhances accuracy of the solution only if it affects the pivoting order, so it makes no sense to scale the matrix before spFactor(). If scaling is desired it should be done before spOrderAndFactor(). There are several things to take into account when choosing the scale factors. First, the scale factors are directly multiplied against the elements in the matrix. To prevent roundoff, each scale factor should be equal to an integer power of the number base of the machine. Since most machines operate in base two, scale factors should be a power of two. Second, the matrix should be scaled such that the matrix of element uncertainties is equilibrated. Third, this function multiplies the scale factors by the elements, so if one row tends to have uncertainties 1000 times smaller than the other rows, then its scale factor should be 1024, not 1/1024. Fourth, to save time, this function does not scale rows or columns if their scale factors are equal to one. Thus, the scale factors should be normalized to the most common scale factor. Rows and columns should be normalized separately. For example, if the size of the matrix is 100 and 10 rows tend to have uncertainties near 1e-6 and the remaining 90 have uncertainties near 1e-12, then the scale factor for the 10 should be 1/1,048,576 and the scale factors for the remaining 90 should be 1. Fifth, since this routine directly operates on the matrix, it is necessary to apply the scale factors to the RHS and Solution vectors. It may be easier to simply use spOrderAndFactor() on a scaled matrix to choose the pivoting order, and then throw away the matrix. Subsequent factorizations, performed with spFactor(), will not need to have the RHS and Solution vectors descaled. Lastly, this function should not be executed before the function spMNA_Preorder.
|
|
Forces matrix to be complex.
|
|
Forces matrix to be real.
|
|
Performs forward elimination and back substitution to find the unknown vector from the RHS vector and factored matrix. This routine assumes that the pivots are associated with the lower triangular (L) matrix and that the diagonal of the upper triangular (U) matrix consists of ones. This routine arranges the computation in different way than is traditionally used in order to exploit the sparsity of the right-hand side. See the reference in spRevision.
|
|
Performs forward elimination and back substitution to find the unknown vector from the RHS vector and transposed factored matrix. This routine is useful when performing sensitivity analysis on a circuit using the adjoint method. This routine assumes that the pivots are associated with the untransposed lower triangular (L) matrix and that the diagonal of the untransposed upper triangular (U) matrix consists of ones.
|
|
Strips the matrix of all fill-ins.
|
|
This function returns the row and column number where the matrix was detected as singular or where a zero was detected on the diagonal.
|