MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
MatrixOps.h
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2011 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 
10 #ifndef _MATRIXOPS_H
11 #define _MATRIXOPS_H
12 //Few functions for performing simple matrix operations.
14 //Note that all matrices here are square, which is the type encountered in
15 //solving the differential equations associated with first-order kinetics of
16 //the Markov channel model.
17 //
18 //Author : Vishaka Datta S, 2011, NCBS
20 using std::vector;
21 
22 typedef vector< vector< double > > Matrix;
23 typedef vector< double > Vector;
24 
25 //Idea taken from the implementation of the DGETRF method in LAPACK. When
26 //the pivot is zero, we divide by a small number instead of simply throwing up
27 //an error and not returning a result.
28 #define EPSILON 1e-15
29 
30 #define FIRST 1
31 #define SECOND 2
32 
33 #define DUMMY 0
34 
35 //Just a debug function to print the matrix.
36 void matPrint( Matrix* );
37 
38 void vecPrint( Vector* );
39 
40 //Computes product of two square matrices.
41 //Version 1 : Returns the result in a new matrix.
43 
44 //Version 2 : Returns the result in either of the matrices passed in.
45 //The third parameter determines which matrix to destroy in order to return
46 //the result in it.
47 void matMatMul( Matrix*, Matrix*, unsigned int );
48 
49 //Special version to multiply upper and lower triangular matrices (in that
50 //order). Used specially by the matInv method. The result is stored in the
51 //first matrix.
52 //Thanks to the structure of this multiplication, the multiplication can be
53 //carried out in place.
54 void triMatMul( Matrix*, Matrix* );
55 
56 //Special matrix multiplication when the second matrix is a permutation matrix
57 //i.e. the columns are to be permuted.
58 //This helps in avoiding a matrix multiplication.
59 void matPermMul( Matrix*, vector< unsigned int >* );
60 
61 //Computes sum of two square matrices.
62 Matrix* matMatAdd( const Matrix*, const Matrix*, double, double );
63 
64 //Version 2 : Returns the result in either of the matrices that are passed as
65 //arguments.
66 void matMatAdd( Matrix*, Matrix*, double, double, unsigned int );
67 
68 //Adds k*I to given matrix. Original is intact.
69 Matrix* matEyeAdd( const Matrix*, double );
70 
71 //Version 2 : Returns the matrix in first argument i.e. original is destroyed.
72 void matEyeAdd( Matrix*, double, unsigned int );
73 
74 //Computes the result of multiplying all terms of a matrix by a scalar and then
75 //adding another scalar i.e. B = a*A + b.
76 //First argument is scale i.e. 'a' and second argument is shift i.e. 'b'.
77 Matrix* matScalShift( const Matrix*, double, double );
78 
79 void matScalShift( Matrix*, double, double, unsigned int );
80 
81 //Computes the result of multiplying a row vector with a matrix (in that order).
82 Vector* vecMatMul( const Vector*, Matrix* );
83 
84 //Computes the result of scaling and shifting a vector.
85 Vector* vecScalShift( const Vector*, double, double );
86 
87 void vecScalShift( Vector*, double, double, unsigned int );
88 
89 //Computes the result of multiplying a matrix with a column vector (in that order).
91 
92 //Computes the sum of two vectors.
93 Vector* vecVecScalAdd( const Vector*, const Vector*, double, double );
94 
95 //Version 2 : Returns the result in the first vector.
96 void vecVecScalAdd( Vector*, Vector*, double, double, unsigned int );
97 
98 //Trace of matrix.
99 double matTrace( Matrix* );
100 
101 //Calculate column norm of matrix.
102 double matColNorm( Matrix* );
103 
104 //Plain old matrix transpose i.e. done out-of-place.
105 Matrix* matTrans( Matrix* );
106 
107 //Matrix inverse implemented using LU-decomposition (Doolittle algorithm)
108 //Returns NULL if matrix is singular.
109 void matInv( Matrix*, vector< unsigned int >*, Matrix* );
110 
111 //Carry out partial pivoting.
112 double doPartialPivot( Matrix*, unsigned int, unsigned int, vector< unsigned int >*);
113 
115 //Memory allocation routines.
117 Matrix* matAlloc( unsigned int );
118 
119 Vector* vecAlloc( unsigned int );
120 
121 #endif
void matPrint(Matrix *)
Definition: MatrixOps.cpp:19
vector< double > Vector
Definition: MatrixOps.h:23
Matrix * matAlloc(unsigned int)
Definition: MatrixOps.cpp:480
Matrix * matScalShift(const Matrix *, double, double)
Definition: MatrixOps.cpp:175
Vector * vecAlloc(unsigned int)
Definition: MatrixOps.cpp:491
void triMatMul(Matrix *, Matrix *)
Definition: MatrixOps.cpp:67
void matPermMul(Matrix *, vector< unsigned int > *)
Definition: MatrixOps.cpp:88
double matTrace(Matrix *)
Definition: MatrixOps.cpp:271
Vector * matVecMul(Matrix *, Vector *)
Definition: MatrixOps.cpp:235
Matrix * matMatMul(Matrix *, Matrix *)
Definition: MatrixOps.cpp:36
Vector * vecVecScalAdd(const Vector *, const Vector *, double, double)
Definition: MatrixOps.cpp:249
double doPartialPivot(Matrix *, unsigned int, unsigned int, vector< unsigned int > *)
Definition: MatrixOps.cpp:314
Matrix * matEyeAdd(const Matrix *, double)
Definition: MatrixOps.cpp:147
Matrix * matMatAdd(const Matrix *, const Matrix *, double, double)
Definition: MatrixOps.cpp:111
vector< vector< double > > Matrix
Definition: MatrixOps.h:22
void matInv(Matrix *, vector< unsigned int > *, Matrix *)
Definition: MatrixOps.cpp:348
Vector * vecMatMul(const Vector *, Matrix *)
Definition: MatrixOps.cpp:201
Vector * vecScalShift(const Vector *, double, double)
Definition: MatrixOps.cpp:215
double matColNorm(Matrix *)
Definition: MatrixOps.cpp:282
void vecPrint(Vector *)
Definition: MatrixOps.cpp:29
Matrix * matTrans(Matrix *)
Definition: MatrixOps.cpp:300