Skip to content
Snippets Groups Projects
bfmatrix.cpp 9.78 KiB
//
//    Definitions for class BFMatrix
//
//    Jesper Andersson, FMRIB Image Analysis Group
//
//    Copyright (C) 2007 University of Oxford 
//

#include <iostream>
#include <iomanip>
#include <fstream>
#include <boost/shared_ptr.hpp>
#include "newmat.h"
#include "newmatio.h"
#include "bfmatrix.h"

namespace MISCMATHS {

//
// Member functions for BFMatrix
//

void BFMatrix::print(const NEWMAT::Matrix&  m,
                     const std::string&     fname) const
{
  if (!fname.length()) {
    cout << endl << m << endl;
  }
  else {
    try {
      ofstream  fout(fname.c_str());
      fout << setprecision(10) << m;
    }
    catch(...) {
      std::string  errmsg("BFMatrix::print: Failed to write to file " + fname);
      throw BFMatrixException(errmsg);
    }
  }
}

//
// Member functions for SparseBFMatrix
//

//
// Concatenation of two matrices returning a third
//
void SparseBFMatrix::HorConcat(const BFMatrix& B, BFMatrix& AB) const
{
  try {
    const SparseBFMatrix& lB = dynamic_cast<const SparseBFMatrix&>(B);
    SparseBFMatrix& lAB = dynamic_cast<SparseBFMatrix&>(AB);
    if (Nrows() != lB.Nrows()) {throw BFMatrixException("SparseBFMatrix::HorConcat: Matrices must have same # of rows");}
    *(lAB.mp) = *mp | *(lB.mp);
  }
  catch (std::bad_cast) {
    throw BFMatrixException("SparseBFMatrix::HorConcat: dynamic cast error"); 
  }
}

void SparseBFMatrix::HorConcat(const NEWMAT::Matrix& B, BFMatrix& AB) const
{
  try {
    SparseBFMatrix& lAB = dynamic_cast<SparseBFMatrix&>(AB);
    if (int(Nrows()) != B.Nrows()) {throw BFMatrixException("SparseBFMatrix::HorConcat: Matrices must have same # of rows");}
    *(lAB.mp) = *mp | B;
  }
  catch (std::bad_cast) {
    throw BFMatrixException("SparseBFMatrix::HorConcat: dynamic cast error"); 
  }