linalg Namespace Reference

Linear algebra namespace. More...


Classes

class  matrix
 A wrapper class for GNU Scientific Library matrices. More...
class  vector
 A wrapper class for GSL vectors. More...
class  vector_view
 A vector that doesn't own its data; rather, points to data owned by another vector. More...
class  slice
 Vector slices corresponding to GNU Octave ranges. More...

Typedefs

typedef vector point
 Useful alias, vectors are also points in space.

Functions

std::ostream & operator<< (std::ostream &os, const vector &v)
 Stream insertion operator.
vector operator>> (std::istream &is, vector &v)
 Stream extraction operator.
std::ostream & operator<< (std::ostream &os, const matrix &M)
 Stream insertion operator.
matrix operator>> (std::istream &is, matrix &v)
 Stream extraction operator.
vector operator* (double a, const vector &v)
 Scale a vector.
double norm (const vector &v)
 Euclidean norm of a vector.
matrix operator* (double a, const matrix &M)
 Scale a matrix.
matrix inv (const matrix &A)
 Matrix inverse, computed with LU factorisation.
matrix T (const matrix &A)
 Return copy of transposed matrix.
double tr (const matrix &A)
 Trace.
double det (matrix &A)
 Determinant.
double cond (matrix &A)
 L2 condition number, computed with SVD.


Detailed Description

Linear algebra namespace.

Typedef Documentation

Useful alias, vectors are also points in space.


Function Documentation

double linalg::cond ( matrix &  A  ) 

L2 condition number, computed with SVD.

00920                         {
00921     return A.cond();
00922   }

Here is the call graph for this function:

double linalg::det ( matrix &  A  ) 

Determinant.

00916                        {
00917     return A.det();
00918   }

Here is the call graph for this function:

matrix linalg::inv ( const matrix &  A  ) 

Matrix inverse, computed with LU factorisation.

00907                              {
00908     return A.inv();
00909   }

Here is the call graph for this function:

double linalg::norm ( const vector &  v  ) 

Euclidean norm of a vector.

00901                               {
00902     return v.norm();
00903   }

Here is the call graph for this function:

matrix linalg::operator* ( double  a,
const matrix &  M 
)

Scale a matrix.

00904                                              {
00905     return M*a;
00906   }

vector linalg::operator* ( double  a,
const vector &  v 
)

Scale a vector.

00898                                              {
00899     return v*a;
00900   }

std::ostream & linalg::operator<< ( std::ostream &  os,
const matrix &  M 
)

Stream insertion operator.

00786                                                           {
00787      os.setf(std::ios::scientific);
00788      os << std::setprecision(A.precision());
00789      for(size_t i = 1; i <= A.rows(); i++){
00790        for(size_t j = 1; j <= A.cols(); j++)
00791          os << " " << std::setw(A.precision()+6) << A(i,j) << " ";
00792        os << std::endl;
00793      }
00794      return os;
00795    }

Here is the call graph for this function:

std::ostream & linalg::operator<< ( std::ostream &  os,
const vector &  v 
)

Stream insertion operator.

00724                                                          {
00725     os.setf(std::ios::scientific);
00726     os << std::setprecision(v.precision());
00727     for(size_t i = 1; i <= v.size(); i++){
00728       os << " " << std::setw(v.precision()+6) << v(i) << " ";
00729     }
00730     os << std::endl;
00731      return os;
00732    }

Here is the call graph for this function:

matrix linalg::operator>> ( std::istream &  is,
matrix &  v 
)

Stream extraction operator.

00797                                               {
00798     using namespace std;
00799     string line, token;
00800     bool rowset = false;
00801     list<double> data;
00802      
00803     size_t rowsize = 0;
00804     size_t rows = 0;
00805     size_t cols = 0;
00806     while(getline(is, line)){
00807       line = utils::trim(line);
00808       //Blank row or comment character.
00809       if(line[0] == '#' or line.length() == 0) 
00810         continue;
00811          
00812       stringstream ss_line;      
00813       cols = 0;
00814       ss_line << line;
00815       while(ss_line >> token){
00816         if(token[0] == '#'){
00817           break; //Rest of line is comment.
00818         }
00819 
00820         //The following may fail on a C++ implementation that doesn't
00821         //obey IEEE arithmetic (IEC 559). We could check for those,
00822         //but do we really want to compile Octave on C++
00823         //implementations that don't follow IEEE arithmetic?
00824         else if(token == "NaN"){ 
00825           double x = std::numeric_limits<double>::quiet_NaN();
00826           data.push_back(x);
00827           cols++;
00828         }
00829         else if(token == "Inf"){
00830           double x = std::numeric_limits<double>::infinity();
00831           data.push_back(x);
00832           cols++;
00833         }
00834         else if(token == "-Inf"){
00835           double x = -std::numeric_limits<double>::infinity();
00836           data.push_back(x);
00837           cols++;
00838         }
00839         else if(token == ","){
00840           ss_line >> token;
00841         }
00842 
00843         //This also ignores commas and any other token. I think. If
00844         //there's garbage in the token, I have to see what happens
00845         //here. Do we also need to check for garbage?
00846         else{ 
00847           double x;
00848           stringstream ss_token;
00849           ss_token << token;
00850           ss_token >> x;
00851           data.push_back(x);
00852           cols++;
00853         }
00854 
00855       }
00856        
00857       //First row gives the number of columns, and all successive rows
00858       //must have the same number of elements. 
00859       if(!rowset){
00860         rowset = true;
00861         rowsize = cols;
00862       }
00863        
00864       if (cols != rowsize){
00865         badArgument exc;
00866         exc.reason = "Cannot read matrix: bad format in input";
00867         exc.file = __FILE__;
00868         exc.line = __LINE__;
00869         throw exc;
00870       }
00871       rows++;
00872     }
00873     if(rows == 0){
00874       endOfFile exc;
00875       exc.reason = "Cannot read empty matrix from input.";
00876       exc.file = __FILE__;
00877       exc.line = __LINE__;
00878       throw exc;
00879     }
00880     
00881     matrix M(rows,cols);
00882     typedef list<double>::iterator LI;
00883     
00884     LI k = data.begin();
00885     for(size_t i = 1; i <= rows; i++){
00886       for(size_t j = 1; j <= cols; j++){
00887         M(i,j) = *k;
00888         k++;
00889       }
00890     }
00891 
00892     A = M;    
00893     return A;
00894   }

Here is the call graph for this function:

vector linalg::operator>> ( std::istream &  is,
vector &  v 
)

Stream extraction operator.

00734                                                {
00735      using namespace std;
00736      string s;
00737      list<double> data;
00738      bool colvector = true;
00739      bool shouldbedone = false;
00740      while(getline(is, s)){
00741        s = utils::trim(s);
00742        if(s[0] == '#' or s.size() == 0) //Blank line or comment character
00743          continue;
00744 
00745        stringstream ss;
00746        ss << s;
00747 
00748        double x;
00749        size_t i = 0;
00750        while(ss >> x){
00751          if( (i > 0 and colvector == false) or (shouldbedone == true)){
00752            badArgument exc;
00753            exc.reason = "Cannot read vector: bad format in input";
00754            exc.file = __FILE__;
00755            exc.line = __LINE__;
00756            throw exc;
00757          }
00758          data.push_back(x);
00759          i++;
00760        }
00761        if(i > 1){
00762          colvector = false; //So it must be a row vector instead
00763          shouldbedone = true;
00764        }
00765      }
00766 
00767      if(data.size() == 0){
00768        endOfFile exc;
00769        exc.reason = "Cannot read empty vector from input.";
00770        exc.file = __FILE__;
00771        exc.line = __LINE__;
00772        throw exc;
00773      }
00774 
00775      vector w(data.size());
00776      typedef list<double>::iterator LI;
00777      size_t k = 1;
00778      for(LI i = data.begin(); i != data.end(); i++){
00779        w(k) = *i;
00780        k++;
00781      }
00782      v = w;
00783      return v;
00784    }

Here is the call graph for this function:

matrix linalg::T ( const matrix &  A  ) 

Return copy of transposed matrix.

00910                            {
00911     return A.T();
00912   }

Here is the call graph for this function:

double linalg::tr ( const matrix &  A  ) 

Trace.

00913                             {
00914     return A.tr();
00915   }

Here is the call graph for this function:


Generated on Fri Jun 6 17:28:29 2008 by  doxygen 1.5.6