10 #include "jama/jama_svd.h" 
   11 #include "jama/jama_eig.h" 
   12 #include "jama/jama_lu.h" 
   14 #include "Constants.h" 
   15 #include "IException.h" 
   26       std::string m = 
"Invalid matrix dimensions";
 
   29     p_matrix = TNT::Array2D<double>(n, m, value);
 
   36     p_matrix = matrix.copy();
 
   47       std::string m = 
"Invalid matrix dimensions";
 
   51     for(
int i = 0; i < identity.Rows(); i++) {
 
   61     if(Rows() != Columns()) {
 
   62       std::string m = 
"Unable to calculate the determinant, the matrix is not square.";
 
   65     JAMA::LU<double> lu(p_matrix);
 
   73     if(Rows() != Columns()) {
 
   74       std::string m = 
"Unable to calculate the trace, the matrix is not square.";
 
   78     for(
int i = 0; i < Rows(); i++) {
 
   79       trace += p_matrix[i][i];
 
   88     if(Columns() != matrix.Rows()) {
 
   89       std::string m = 
"Incompatible matrix dimensions, cannot multiply the matrices.";
 
   92     TNT::Array2D<double> m(matrix.Rows(), matrix.Columns());
 
   93     for(
int i = 0; i < m.dim1(); i++) {
 
   94       for(
int j = 0; j < m.dim2(); j++) {
 
   95         m[i][j] = matrix[i][j];
 
   98     return Matrix(TNT::matmult(p_matrix, m));
 
  105     if(Rows() != matrix.Rows() || Columns() != matrix.Columns()) {
 
  106       std::string m = 
"Incompatible matrix dimensions, cannot add the matrices.";
 
  109     TNT::Array2D<double> m(matrix.Rows(), matrix.Columns());
 
  110     for(
int i = 0; i < m.dim1(); i++) {
 
  111       for(
int j = 0; j < m.dim2(); j++) {
 
  112         m[i][j] = matrix[i][j];
 
  115     return Matrix(p_matrix + m);
 
  122     if(Rows() != matrix.Rows() || Columns() != matrix.Columns()) {
 
  123       std::string m = 
"Incompatible matrix dimensions, cannot subtract the matrices.";
 
  126     TNT::Array2D<double> m(matrix.Rows(), matrix.Columns());
 
  127     for(
int i = 0; i < m.dim1(); i++) {
 
  128       for(
int j = 0; j < m.dim2(); j++) {
 
  129         m[i][j] = matrix[i][j];
 
  133     return Matrix(p_matrix - m);
 
  141     if(Rows() != matrix.Rows() || Columns() != matrix.Columns()) {
 
  142       std::string m = 
"Incompatible matrix dimensions, cannot multiply the matrices.";
 
  145     TNT::Array2D<double> m(matrix.Rows(), matrix.Columns());
 
  146     for(
int i = 0; i < m.dim1(); i++) {
 
  147       for(
int j = 0; j < m.dim2(); j++) {
 
  148         m[i][j] = matrix[i][j];
 
  151     return Matrix(p_matrix * m);
 
  158     Matrix product(Rows(), Columns());
 
  159     for(
int i = 0; i < Rows(); i++) {
 
  160       for(
int j = 0; j < Columns(); j++) {
 
  161         product[i][j] = p_matrix[i][j] * scalar;
 
  171     TNT::Array2D<double> transpose(p_matrix.dim2(), p_matrix.dim1());
 
  172     for(
int i = 0; i < transpose.dim1(); i++) {
 
  173       for(
int j = 0; j < transpose.dim2(); j++) {
 
  174         transpose[i][j] = p_matrix[j][i];
 
  184     if(Rows() != Columns()) {
 
  185       std::string m = 
"Unable to calculate the inverse, the matrix is not square.";
 
  188     TNT::Array2D<double> id(p_matrix.dim1(), p_matrix.dim2(), 0.0);
 
  189     for(
int i = 0; i < p_matrix.dim1(); i++) 
id[i][i] = 1;
 
  191     JAMA::LU<double> lu(p_matrix);
 
  192     if(lu.det() == 0.0) {
 
  193       std::string m = 
"Cannot take the inverse of the matrix";
 
  197     return Matrix(lu.solve(
id));
 
  204     if(Rows() != Columns()) {
 
  205       std::string m = 
"Unable to calculate eigenvalues, the matrix is not square.";
 
  208     JAMA::Eigenvalue<double> 
E(p_matrix);
 
  209     TNT::Array2D<double> D;
 
  212     std::vector<double> eigenvalues(D.dim1());
 
  213     for(
int i = 0; i < D.dim1(); i++) {
 
  214       eigenvalues[i] = D[i][i];
 
  225     if(Rows() != Columns()) {
 
  226       std::string m = 
"Unable to calculate eigenvectors, the matrix is not square.";
 
  229     JAMA::Eigenvalue<double> 
E(p_matrix);
 
  230     TNT::Array2D<double> V;
 
  240     for(
int i = 0; i < matrix.Rows(); i++) {
 
  241       for(
int j = 0; j < matrix.Columns(); j++) {
 
  243         if(j < matrix.Columns() - 1) os << 
" ";
 
  245       if(i < matrix.Rows() - 1) os << endl;