Public Member Functions

QuadProgPP::Matrix< T > Class Template Reference

#include <Array.h>

Inheritance diagram for QuadProgPP::Matrix< T >:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 Matrix ()
 Matrix (const unsigned int n, const unsigned int m)
 Matrix (const T &a, const unsigned int n, const unsigned int m)
 Matrix (MType t, const T &a, const T &o, const unsigned int n, const unsigned int m)
 Matrix (MType t, const Vector< T > &v, const T &o, const unsigned int n, const unsigned int m)
 Matrix (const T *a, const unsigned int n, const unsigned int m)
 Matrix (const Matrix< T > &rhs)
 ~Matrix ()
T * operator[] (const unsigned int &i)
const T * operator[] (const unsigned int &i) const
void resize (const unsigned int n, const unsigned int m)
void resize (const T &a, const unsigned int n, const unsigned int m)
Vector< T > extractRow (const unsigned int i) const
Vector< T > extractColumn (const unsigned int j) const
Vector< T > extractDiag () const
Matrix< T > extractRows (const std::set< unsigned int > &indexes) const
Matrix< T > extractColumns (const std::set< unsigned int > &indexes) const
Matrix< T > extract (const std::set< unsigned int > &r_indexes, const std::set< unsigned int > &c_indexes) const
void set (const T *a, unsigned int n, unsigned int m)
void set (const std::set< unsigned int > &r_indexes, const std::set< unsigned int > &c_indexes, const Matrix< T > &m)
void setRow (const unsigned int index, const Vector< T > &v)
void setRow (const unsigned int index, const Matrix< T > &v)
void setRows (const std::set< unsigned int > &indexes, const Matrix< T > &m)
void setColumn (const unsigned int index, const Vector< T > &v)
void setColumn (const unsigned int index, const Matrix< T > &v)
void setColumns (const std::set< unsigned int > &indexes, const Matrix< T > &m)
unsigned int nrows () const
unsigned int ncols () const
Matrix< T > & operator= (const Matrix< T > &rhs)
Matrix< T > & operator= (const T &a)
Matrix< T > & operator+= (const Matrix< T > &rhs)
Matrix< T > & operator-= (const Matrix< T > &rhs)
Matrix< T > & operator*= (const Matrix< T > &rhs)
Matrix< T > & operator/= (const Matrix< T > &rhs)
Matrix< T > & operator^= (const Matrix< T > &rhs)
Matrix< T > & operator+= (const T &a)
Matrix< T > & operator-= (const T &a)
Matrix< T > & operator*= (const T &a)
Matrix< T > & operator/= (const T &a)
Matrix< T > & operator^= (const T &a)
 operator Vector< T > ()

Detailed Description

template<typename T>
class QuadProgPP::Matrix< T >

Definition at line 857 of file Array.h.


Constructor & Destructor Documentation

template<typename T >
QuadProgPP::Matrix< T >::Matrix (  )

Definition at line 916 of file Array.h.

  : n(0), m(0), v(0)
{}
template<typename T >
QuadProgPP::Matrix< T >::Matrix ( const unsigned int  n,
const unsigned int  m 
)

Definition at line 921 of file Array.h.

  : v(new T*[n])
{
  unsigned int i;
  this->n = n; this->m = m;
  v[0] = new T[m * n];
  for (i = 1; i < n; i++)
    v[i] = v[i - 1] + m;
}
template<typename T>
QuadProgPP::Matrix< T >::Matrix ( const T &  a,
const unsigned int  n,
const unsigned int  m 
)

Definition at line 932 of file Array.h.

  : v(new T*[n])
{
  unsigned int i, j;
  this->n = n; this->m = m;
  v[0] = new T[m * n];
  for (i = 1; i < n; i++)
    v[i] = v[i - 1] + m;
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      v[i][j] = a;
}
template<class T>
QuadProgPP::Matrix< T >::Matrix ( MType  t,
const T &  a,
const T &  o,
const unsigned int  n,
const unsigned int  m 
)

Definition at line 960 of file Array.h.

References QuadProgPP::DIAG.

  : v(new T*[n])
{ 
  unsigned int i, j;
  this->n = n; this->m = m;
  v[0] = new T[m * n]; 
  for (i = 1; i < n; i++) 
    v[i] = v[i - 1] + m; 
  switch (t)
    {
    case DIAG:
      for (i = 0; i < n; i++) 
        for (j = 0; j < m; j++) 
          if (i != j)
            v[i][j] = o; 
          else
            v[i][j] = a;
      break;
    default:
      throw std::logic_error("Matrix type not supported");
    }
} 
template<class T>
QuadProgPP::Matrix< T >::Matrix ( MType  t,
const Vector< T > &  v,
const T &  o,
const unsigned int  n,
const unsigned int  m 
)

Definition at line 984 of file Array.h.

References QuadProgPP::DIAG.

  : v(new T*[n])
{ 
  unsigned int i, j;
  this->n = n; this->m = m;
  v[0] = new T[m * n]; 
  for (i = 1; i < n; i++) 
    v[i] = v[i - 1] + m; 
  switch (t)
    {
    case DIAG:
      for (i = 0; i < n; i++) 
        for (j = 0; j < m; j++) 
          if (i != j)
            v[i][j] = o; 
          else
            v[i][j] = a[i];
      break;
    default:
      throw std::logic_error("Matrix type not supported");
    }
} 
template<class T>
QuadProgPP::Matrix< T >::Matrix ( const T *  a,
const unsigned int  n,
const unsigned int  m 
)

Definition at line 946 of file Array.h.

  : v(new T*[n])
{ 
  unsigned int i, j;
  this->n = n; this->m = m;
  v[0] = new T[m * n]; 
  for (i = 1; i < n; i++) 
    v[i] = v[i - 1] + m; 
  for (i = 0; i < n; i++) 
    for (j = 0; j < m; j++) 
      v[i][j] = *a++; 
} 
template<typename T>
QuadProgPP::Matrix< T >::Matrix ( const Matrix< T > &  rhs )

Definition at line 1008 of file Array.h.

  : v(new T*[rhs.n])
{
  unsigned int i, j;
  n = rhs.n; m = rhs.m;
  v[0] = new T[m * n]; 
  for (i = 1; i < n; i++) 
    v[i] = v[i - 1] + m;
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      v[i][j] = rhs[i][j];
}
template<typename T >
QuadProgPP::Matrix< T >::~Matrix (  )

Definition at line 1022 of file Array.h.

{ 
  if (v != 0) { 
    delete[] (v[0]); 
    delete[] (v); 
  } 
}

Member Function Documentation

template<typename T >
Matrix< T > QuadProgPP::Matrix< T >::extract ( const std::set< unsigned int > &  r_indexes,
const std::set< unsigned int > &  c_indexes 
) const [inline]

Definition at line 1167 of file Array.h.

{
  Matrix<T> tmp(r_indexes.size(), c_indexes.size());
  unsigned int i = 0, j;
        
  for (std::set<unsigned int>::const_iterator r_el = r_indexes.begin(); r_el != r_indexes.end(); r_el++)
    {
      if (*r_el >= n)
        throw std::logic_error("Error extracting submatrix: the indexes are out of matrix bounds");
      j = 0;
      for (std::set<unsigned int>::const_iterator c_el = c_indexes.begin(); c_el != c_indexes.end(); c_el++)
        {
          if (*c_el >= m)
            throw std::logic_error("Error extracting rows: the indexes are out of matrix bounds");
          tmp[i][j] = v[*r_el][*c_el];
          j++;
        }
      i++;
    }
        
  return tmp;
}
template<typename T >
Vector< T > QuadProgPP::Matrix< T >::extractColumn ( const unsigned int  j ) const [inline]

Definition at line 1099 of file Array.h.

Referenced by QuadProgPP::rank().

{
  unsigned int i;
  if (j >= m)
    throw std::logic_error("Error in extractRow: trying to extract a row out of matrix bounds");
  Vector<T> tmp(n);
        
  for (i = 0; i < n; i++)
    tmp[i] = v[i][j];
        
  return tmp;
}
template<typename T >
Matrix< T > QuadProgPP::Matrix< T >::extractColumns ( const std::set< unsigned int > &  indexes ) const [inline]

Definition at line 1147 of file Array.h.

{
  Matrix<T> tmp(n, indexes.size());
  unsigned int i, j = 0;
        
  for (std::set<unsigned int>::const_iterator el = indexes.begin(); el != indexes.end(); el++)
    {
      for (i = 0; i < n; i++)
        {
          if (*el >= m)
            throw std::logic_error("Error extracting columns: the indexes are out of matrix bounds");
          tmp[i][j] = v[i][*el];
        }
      j++;
    }
        
  return tmp;
}
template<typename T >
Vector< T > QuadProgPP::Matrix< T >::extractDiag (  ) const [inline]

Definition at line 1113 of file Array.h.

References QuadProgPP::min().

Referenced by QuadProgPP::lu_det().

{
  unsigned int d = std::min(n, m), i;
  
  Vector<T> tmp(d);
        
  for (i = 0; i < d; i++)
    tmp[i] = v[i][i];
        
  return tmp;
        
}
template<typename T >
Vector< T > QuadProgPP::Matrix< T >::extractRow ( const unsigned int  i ) const [inline]

Definition at line 1089 of file Array.h.

Referenced by QuadProgPP::r_rank().

{
  if (i >= n)
    throw std::logic_error("Error in extractRow: trying to extract a row out of matrix bounds");
  Vector<T> tmp(v[i], m);
        
  return tmp;
}
template<typename T >
Matrix< T > QuadProgPP::Matrix< T >::extractRows ( const std::set< unsigned int > &  indexes ) const [inline]

Definition at line 1127 of file Array.h.

{
  Matrix<T> tmp(indexes.size(), m);
  unsigned int i = 0, j;
        
  for (std::set<unsigned int>::const_iterator el = indexes.begin(); el != indexes.end(); el++)
    {
      for (j = 0; j < m; j++)
        {
          if (*el >= n)
            throw std::logic_error("Error extracting rows: the indexes are out of matrix bounds");
          tmp[i][j] = v[*el][j];
        }
      i++;
    }
        
  return tmp;
}
template<typename T>
unsigned int QuadProgPP::Matrix< T >::ncols (  ) const [inline]
template<typename T>
unsigned int QuadProgPP::Matrix< T >::nrows (  ) const [inline]
template<typename T >
QuadProgPP::Matrix< T >::operator Vector< T > (  ) [inline]

Definition at line 1587 of file Array.h.

{
  if (n > 1 && m > 1)
    throw std::logic_error("Error matrix cast to vector: trying to cast a multi-dimensional matrix");
  if (n == 1)
    return extractRow(0);
  else
    return extractColumn(0);
}
template<typename T>
Matrix< T > & QuadProgPP::Matrix< T >::operator*= ( const Matrix< T > &  rhs ) [inline]

Definition at line 1474 of file Array.h.

References QuadProgPP::Matrix< T >::ncols(), and QuadProgPP::Matrix< T >::nrows().

{
  if (m != rhs.ncols() || n != rhs.nrows())
    throw std::logic_error("Operator*=: matrices have different sizes");
  for (unsigned int i = 0; i < n; i++)
    for (unsigned int j = 0; j < m; j++)
      v[i][j] *= rhs[i][j];
        
  return *this;
}
template<typename T>
Matrix< T > & QuadProgPP::Matrix< T >::operator*= ( const T &  a ) [inline]

Definition at line 1486 of file Array.h.

{
  for (unsigned int i = 0; i < n; i++)
    for (unsigned int j = 0; j < m; j++)
      v[i][j] *= a;
        
  return *this;
}
template<typename T>
Matrix< T > & QuadProgPP::Matrix< T >::operator+= ( const Matrix< T > &  rhs ) [inline]

Definition at line 1354 of file Array.h.

References QuadProgPP::Matrix< T >::ncols(), and QuadProgPP::Matrix< T >::nrows().

{
  if (m != rhs.ncols() || n != rhs.nrows())
    throw std::logic_error("Operator+=: matrices have different sizes");
  for (unsigned int i = 0; i < n; i++)
    for (unsigned int j = 0; j < m; j++)
      v[i][j] += rhs[i][j];
        
  return *this;
}
template<typename T>
Matrix< T > & QuadProgPP::Matrix< T >::operator+= ( const T &  a ) [inline]

Definition at line 1366 of file Array.h.

{
  for (unsigned int i = 0; i < n; i++)
    for (unsigned int j = 0; j < m; j++)
      v[i][j] += a;
        
  return *this;
}
template<typename T>
Matrix< T > & QuadProgPP::Matrix< T >::operator-= ( const T &  a ) [inline]

Definition at line 1429 of file Array.h.

{
  for (unsigned int i = 0; i < n; i++)
    for (unsigned int j = 0; j < m; j++)
      v[i][j] -= a;
        
  return *this;
}
template<typename T>
Matrix< T > & QuadProgPP::Matrix< T >::operator-= ( const Matrix< T > &  rhs ) [inline]

Definition at line 1417 of file Array.h.

References QuadProgPP::Matrix< T >::ncols(), and QuadProgPP::Matrix< T >::nrows().

{
  if (m != rhs.ncols() || n != rhs.nrows())
    throw std::logic_error("Operator-=: matrices have different sizes");
  for (unsigned int i = 0; i < n; i++)
    for (unsigned int j = 0; j < m; j++)
      v[i][j] -= rhs[i][j];
        
  return *this;
}
template<typename T>
Matrix< T > & QuadProgPP::Matrix< T >::operator/= ( const Matrix< T > &  rhs ) [inline]

Definition at line 1531 of file Array.h.

References QuadProgPP::Matrix< T >::ncols(), and QuadProgPP::Matrix< T >::nrows().

{
  if (m != rhs.ncols() || n != rhs.nrows())
    throw std::logic_error("Operator+=: matrices have different sizes");
  for (unsigned int i = 0; i < n; i++)
    for (unsigned int j = 0; j < m; j++)
      v[i][j] /= rhs[i][j];
        
  return *this;
}
template<typename T>
Matrix< T > & QuadProgPP::Matrix< T >::operator/= ( const T &  a ) [inline]

Definition at line 1543 of file Array.h.

{
  for (unsigned int i = 0; i < n; i++)
    for (unsigned int j = 0; j < m; j++)
      v[i][j] /= a;
        
  return *this;
}
template<typename T>
Matrix< T > & QuadProgPP::Matrix< T >::operator= ( const Matrix< T > &  rhs ) [inline]

Definition at line 1031 of file Array.h.

                : normal assignment via copying has been performed; 
// if matrix and rhs were different sizes, matrix 
// has been resized to match the size of rhs 
{ 
  unsigned int i, j;
  if (this != &rhs) 
    {
      resize(rhs.n, rhs.m);
      for (i = 0; i < n; i++) 
        for (j = 0; j < m; j++) 
          v[i][j] = rhs[i][j]; 
    } 
  return *this; 
} 
template<typename T>
Matrix< T > & QuadProgPP::Matrix< T >::operator= ( const T &  a ) [inline]

Definition at line 1048 of file Array.h.

{ 
  unsigned int i, j;
  for (i = 0; i < n; i++) 
    for (j = 0; j < m; j++) 
      v[i][j] = a; 
  return *this; 
} 
template<typename T>
T* QuadProgPP::Matrix< T >::operator[] ( const unsigned int &  i ) [inline]

Definition at line 869 of file Array.h.

{ return v[i]; } // Subscripting: row i
template<typename T>
const T* QuadProgPP::Matrix< T >::operator[] ( const unsigned int &  i ) const [inline]

Definition at line 870 of file Array.h.

{ return v[i]; }; // const subsctipting
template<typename T>
Matrix< T > & QuadProgPP::Matrix< T >::operator^= ( const T &  a ) [inline]

Definition at line 1577 of file Array.h.

References QuadProgPP::pow().

{
  for (unsigned int i = 0; i < n; i++)
    for (unsigned int j = 0; j < m; j++)
      v[i][j] = pow(v[i][j], a);
        
  return *this;
}
template<typename T>
Matrix< T > & QuadProgPP::Matrix< T >::operator^= ( const Matrix< T > &  rhs ) [inline]

Definition at line 1564 of file Array.h.

References QuadProgPP::Matrix< T >::ncols(), QuadProgPP::Matrix< T >::nrows(), and QuadProgPP::pow().

{
  if (m != rhs.ncols() || n != rhs.nrows())
    throw std::logic_error("Operator^=: matrices have different sizes");
  for (unsigned int i = 0; i < n; i++)
    for (unsigned int j = 0; j < m; j++)
      v[i][j] = pow(v[i][j], rhs[i][j]);
        
  return *this;
}
template<typename T>
void QuadProgPP::Matrix< T >::resize ( const T &  a,
const unsigned int  n,
const unsigned int  m 
) [inline]

Definition at line 1077 of file Array.h.

{
  unsigned int i, j;
  resize(n, m);
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      v[i][j] = a;
} 
template<typename T >
void QuadProgPP::Matrix< T >::resize ( const unsigned int  n,
const unsigned int  m 
) [inline]

Definition at line 1059 of file Array.h.

Referenced by QuadProgPP::operator>>(), and QuadProgPP::svd().

{
  unsigned int i;
  if (n == this->n && m == this->m)
    return;
  if (v != 0) 
    { 
      delete[] (v[0]); 
      delete[] (v); 
    } 
  this->n = n; this->m = m;
  v = new T*[n]; 
  v[0] = new T[m * n];  
  for (i = 1; i < n; i++)
    v[i] = v[i - 1] + m;
} 
template<typename T>
void QuadProgPP::Matrix< T >::set ( const T *  a,
unsigned int  n,
unsigned int  m 
) [inline]

Definition at line 1301 of file Array.h.

{
  if (this->n != n || this->m != m)
    resize(n, m);
  unsigned int k = 0;
  for (unsigned int i = 0; i < n; i++)
    for (unsigned int j = 0; j < m; j++)
      v[i][j] = a[k++];
}
template<typename T>
void QuadProgPP::Matrix< T >::set ( const std::set< unsigned int > &  r_indexes,
const std::set< unsigned int > &  c_indexes,
const Matrix< T > &  m 
) [inline]

Definition at line 1278 of file Array.h.

References QuadProgPP::Matrix< T >::ncols(), and QuadProgPP::Matrix< T >::nrows().

{
  unsigned int i = 0, j;
  if (c_indexes.size() != a.ncols() || r_indexes.size() != a.nrows())
    throw std::logic_error("Error setting matrix elements: ranges are not compatible");
        
  for (std::set<unsigned int>::const_iterator r_el = r_indexes.begin(); r_el != r_indexes.end(); r_el++)
    {
      if (*r_el >= n)
        throw std::logic_error("Error in set: trying to set a row out of matrix bounds");
      j = 0;
      for (std::set<unsigned int>::const_iterator c_el = c_indexes.begin(); c_el != c_indexes.end(); c_el++)
        {
          if (*c_el >= m)
            throw std::logic_error("Error in set: trying to set a column out of matrix bounds");
          v[*r_el][*c_el] = a[i][j];
          j++;
        }
      i++;
    }
}
template<typename T>
void QuadProgPP::Matrix< T >::setColumn ( const unsigned int  index,
const Vector< T > &  v 
) [inline]

Definition at line 1234 of file Array.h.

References QuadProgPP::Vector< T >::size().

Referenced by QuadProgPP::rank().

{       
  if (j >= m)
    throw std::logic_error("Error in setColumn: trying to set a column out of matrix bounds");
  if (this->n != a.size())
    throw std::logic_error("Error setting matrix column: ranges are not compatible");
  for (unsigned int i = 0; i < nrows(); i++)
    v[i][j] = a[i];
}
template<typename T>
void QuadProgPP::Matrix< T >::setColumn ( const unsigned int  index,
const Matrix< T > &  v 
) [inline]

Definition at line 1245 of file Array.h.

References QuadProgPP::Matrix< T >::ncols(), and QuadProgPP::Matrix< T >::nrows().

{       
  if (j >= m)
    throw std::logic_error("Error in setColumn: trying to set a column out of matrix bounds");
  if (this->n != a.nrows())
    throw std::logic_error("Error setting matrix column: ranges are not compatible");
  if (a.ncols() != 1)
    throw std::logic_error("Error setting matrix column with a non-column matrix");
  for (unsigned int i = 0; i < nrows(); i++)
    v[i][j] = a[i][0];
}
template<typename T>
void QuadProgPP::Matrix< T >::setColumns ( const std::set< unsigned int > &  indexes,
const Matrix< T > &  m 
) [inline]

Definition at line 1259 of file Array.h.

References QuadProgPP::Matrix< T >::ncols(), and QuadProgPP::Matrix< T >::nrows().

{
  unsigned int j = 0;
        
  if (indexes.size() != a.ncols() || this->n != a.nrows())
    throw std::logic_error("Error setting matrix columns: ranges are not compatible");
  for (std::set<unsigned int>::const_iterator el = indexes.begin(); el != indexes.end(); el++)
    {
      for (unsigned int i = 0; i < nrows(); i++)
        {
          if (*el >= m)
            throw std::logic_error("Error in setColumns: trying to set a column out of matrix bounds");
          v[i][*el] = a[i][j];
        }
      j++;
    }
}
template<typename T>
void QuadProgPP::Matrix< T >::setRow ( const unsigned int  index,
const Vector< T > &  v 
) [inline]

Definition at line 1191 of file Array.h.

References QuadProgPP::Vector< T >::size().

Referenced by QuadProgPP::r_rank().

{       
  if (i >= n)
    throw std::logic_error("Error in setRow: trying to set a row out of matrix bounds");
  if (this->m != a.size())
    throw std::logic_error("Error setting matrix row: ranges are not compatible");
  for (unsigned int j = 0; j < ncols(); j++)
    v[i][j] = a[j];
}
template<typename T>
void QuadProgPP::Matrix< T >::setRow ( const unsigned int  index,
const Matrix< T > &  v 
) [inline]

Definition at line 1202 of file Array.h.

References QuadProgPP::Matrix< T >::ncols(), and QuadProgPP::Matrix< T >::nrows().

{       
  if (i >= n)
    throw std::logic_error("Error in setRow: trying to set a row out of matrix bounds");
  if (this->m != a.ncols())
    throw std::logic_error("Error setting matrix column: ranges are not compatible");
  if (a.nrows() != 1)
    throw std::logic_error("Error setting matrix column with a non-row matrix");
  for (unsigned int j = 0; j < ncols(); j++)
    v[i][j] = a[0][j];
}
template<typename T>
void QuadProgPP::Matrix< T >::setRows ( const std::set< unsigned int > &  indexes,
const Matrix< T > &  m 
) [inline]

Definition at line 1215 of file Array.h.

References QuadProgPP::Matrix< T >::ncols(), and QuadProgPP::Matrix< T >::nrows().

{
  unsigned int i = 0;
        
  if (indexes.size() != m.nrows() || this->m != m.ncols())
    throw std::logic_error("Error setting matrix rows: ranges are not compatible");
  for (std::set<unsigned int>::const_iterator el = indexes.begin(); el != indexes.end(); el++)
    {
      for (unsigned int j = 0; j < ncols(); j++)
        {
          if (*el >= n)
            throw std::logic_error("Error in setRows: trying to set a row out of matrix bounds");
          v[*el][j] = m[i][j];
        }
      i++;
    }
}

The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines