Home
Products
Support
Blog
Resources
Company
NMath Matrix User's Guide
TOC |  Previous |  Next |  Index

3.6 Functions of Matrices

NMath Matrix provides a variety of functions that take structured sparse matrices as arguments.

Matrix Transposition

The structured sparse matrix classes provide Transpose() member functions for calculating the transpose of a matrix: B[i,j] = A[j,i]. Class MatrixFunctions also provides a static Transpose() method that returns the transpose of a matrix. Data is copied. For instance:

FloatTriDiagMatrix A = new FloatTriDiagMatrix( 50, 50 );
A.Diagonal(1)++;    // increments the superdiagonal
A.Diagonal(-1)--;   // decrements the subdiagonal
FloatTriDiagMatrix B = A.Transpose();
FloatTriDiagMatrix C = MatrixFunctions.Transpose( A );   // B == C

NOTE- By definition, a symmetric matrix is equal to its own transpose, so the Transpose() method has no effect for these types.

Matrix Inner Products

Class MatrixFunctions provides the static Product() method for calculating the inner product of a matrix and a vector:

DoubleVector data = new DoubleVector( 10, 1, 1 );
DoubleSymmetricMatrix A = new DoubleSymmetricMatrix( data, 4 );
DoubleVector x = new DoubleVector( 4, 1, 1 );
DoubleVector y = MatrixFunctions.Product( A, x );

For banded matrices, additional overloads of the Product() method calculate the product of two matrices. For example:

int rows = 8, cols = 6, lb = 2, ub = 1;
DoubleComplexVector data =
  new DoubleComplexVector( (ub+lb+1)*cols, 0, 1 );
DoubleComplexBandMatrix A =
  new DoubleComplexBandMatrix( data, rows, cols, lb, ub );
DoubleComplexBandMatrix B =
  new DoubleComplexBandMatrix( ++data, cols, cols, lb, ub );
DoubleComplexBandMatrix C =
  MatrixFunctions.Product( A, B );

Also for banded matrices, the static TransposeProduct() method on NMathFunctions computes the matrix inner product of the transpose of a given matrix and a second matrix. Thus, assuming A and B are DoubleBandMatrix instances, this code calculates the inner product of the transpose of A with B:

DoubleBandMatrix C = MatrixFunctions.TransposeProduct( A, B );

Matrix Norms

MatrixFunctions provides static functions OneNorm() to compute the 1-norm (or largest column sum) of a matrix, and InfinityNorm() to compute the infinity-norm (or largest row sum) of a matrix. For instance:

DoubleMatrix A = new DoubleMatrix( "3x3 [1 2 3  4 5 6  7 8 9]" );
double d1 = A.OneNorm();
double d2 = A.InfinityNorm();

The OneNorm() method has overloads for all structured sparse matrix types; InfinityNorm() only for banded and tridiagonal types.

Trigonometric and Transcendental Functions

In general, NMath Matrix does not provide trigonometric and transcendental functions for sparse matrix types. Such functions may change unstored zero values to non-zero values, thus changing a sparse matrix type into a general matrix. If you want to apply a trigonometric or transcendental function to all elements of a sparse matrix, including unstored zero values, convert the matrix to a general matrix first, using the ToGeneralMatrix() method. Alternatively, to apply a trigonometric or transcendental function only to stored values, apply the function to the underlying data vector. These techniques are described in more detail in Section 3.7.

Symmetric matrices are in a different category than the other sparse matrix types, because unstored values are not constrained to be zero. Therefore, NMath Matrix extends standard trigonometric functions Acos(), Asin(), Atan(), Cos(), Cosh(), Sin(), Sinh(), Tan(), and Tanh() to take symmetric matrix arguments. Class MatrixFunctions provides these functions as static methods. For instance, this code construct a symmetric matrix whose contents are the cosines of another symmetric matrix:

FloatMatrix genMat = new FloatMatrix( 10, 10, 0, Math.Pi/4 );
FloatSymmetricMatrix A = new FloatSymmetricMatrix( genMat );
FloatSymmetricMatrix cosA = MatrixFunctions.Cos( A );

The static Atan2() method takes two symmetric matrices and applies the two-argument arc tangent function to corresponding pairs of elements.

MatrixFunctions also provides standard transcendental functions Log() and Log10() that take symmetric matrix arguments.

Absolute Value

The static Abs() function on class MatrixFunctions applies the absolute value function to each element of a given matrix:

int order = 5, hb = 2;
DoubleComplexVector data = new DoubleComplexVector( "(0,0) (0,0) 
  (1,-2) (0,0) (2,-4) (3,-6) (4,-8) (5,-10) (6,-12) (7,-14) (8,-16) 
  (9,-18) (10,-20) (11,-22) (12,-24)" );
DoubleHermitianBandMatrix A =
  new DoubleHermitianBandMatrix( data, order, hb );
DoubleSymBandMatrix B = MatrixFunctions.Abs( A );

Complex Matrix Functions

Static methods Real() and Imag() on class MatrixFunctions return the real and imaginary part of the elements of a matrix. If the elements of the given matrix are real, Real() simply returns the given matrix and Imag() returns a matrix of the same dimensions containing all zeros.

Static methods Arg() and Conj() on class MatrixFunctions return the arguments (or phases) and complex conjugates of the elements of a matrix. If the elements of the given matrix are real, both methods simply return the given matrix.

For instance:

FloatComplex initValue = new FloatComplex( 1, -1.5F );
FloatComplex increment = new FloatComplex( 1, 0.25F );
FloatComplexMatrix getMat =
  new FloatComplexMatrix( 7, 6, initValue, increment );
FloatComplexTriDiagMatrix A =
  new FloatComplexTriDiagMatrix( getMat );

FloatTriDiagMatrix AImag = MatrixFunctions.Imag( A ); 

TOC |  Previous |  Next |  Index

Copyright © 2008 CenterSpace Software, LLC. All rights reserved.
All trademarks and registered trademarks mentioned on this web site are the property of their respective owners.
Contact Webmaster