12 #ifndef   Sparse_Matrix_h 
   13 #define   Sparse_Matrix_h 
   84     void reSize( 
unsigned, 
unsigned);
 
   85     void reShape(
unsigned, 
unsigned);
 
   92     void reSize(
unsigned newsize);
 
  157         if ( M.
m_val != 0 ) {
 
  174         if ( M.
m_val == 0 ) {
 
  194     for (
unsigned k=0; k<M.
m_size; ++k) {
 
  213     if ( m_same != same ) {
 
  241     if (m == 0 || n == 0) {
 
  283     if (m_capacity != 0) { 
 
  295     if (rows() != o.
rows() || cols() != o.
cols()) {
 
  299     for (
unsigned i=0; i<rows(); i++) {
 
  300         for (
unsigned j=0; j<cols(); j++) {
 
  301             if ( (*
this)(i,j) != o(i,j) ) {
 
  331         if (m_capacity != 0) { 
 
  346         if (m_capacity != 0) { 
 
  364     for (
unsigned k=0; k<m_size; ++k) {
 
  367         m_val[k] = o.
m_val[k];
 
  391     } 
else if (o.
m_val == 0) {
 
  398     } 
else if ( m_val != 0 ) {
 
  428     std::swap( this->m_I        , o.
m_I        );
 
  429     std::swap( this->m_J        , o.
m_J        );
 
  430     std::swap( this->m_val      , o.
m_val      );
 
  431     std::swap( this->m_size     , o.
m_size     );
 
  433     std::swap( this->m_rows     , o.
m_rows     );
 
  434     std::swap( this->m_cols     , o.
m_cols     );
 
  435     std::swap( this->m_same     , o.
m_same     );
 
  448     if (m != m_rows || n != m_cols) {
 
  458         if (m_capacity != 0) {
 
  460             delete [] m_I;     m_I = 0;
 
  461             delete [] m_J;     m_J = 0;
 
  462             delete [] m_val;   m_val = 0;
 
  524     if (m * n == m_rows * m_cols) {
 
  528         unsigned row = 0, col = 0;
 
  529         for (
unsigned k=0; k<m_size; ++k) {
 
  533             unsigned lineal = (m_I[k] * m_cols) + m_J[k]; 
 
  552     if ( m * n == m_rows * m_cols ) {
 
  562     assert( 
"Sparse_Matrix<E>::operator()()" && (i < rows()) );
 
  563     assert( 
"Sparse_Matrix<E>::operator()()" && (j < cols()) );
 
  565     for (
unsigned k=0; k<m_size; k++) {
 
  598     if (m_last_change != 0) {
 
  599         if ( *m_last_change == m_same ) { 
 
  601                 delete [] m_I;    m_I = 0;
 
  602                 delete [] m_J;    m_J = 0;
 
  603                 delete [] m_val;  m_val = 0;
 
  609                 unsigned k = (m_last_change - m_val); 
 
  610                 *m_last_change = m_val[m_size];
 
  611                 m_I[k] = m_I[m_size];
 
  612                 m_J[k] = m_J[m_size];
 
  624     assert( 
"Sparse_Matrix<E>::operator()()" && (i < rows()) );
 
  625     assert( 
"Sparse_Matrix<E>::operator()()" && (j < cols()) );
 
  628     for (
unsigned k=0; k<m_size; k++) {
 
  637     assert( (m_size <= m_capacity) && 
" => Agotado m_val[] Sparse_Matrix<E>::operator()()" );
 
  638     if (m_size == m_capacity) {
 
  639         unsigned new_capacity = m_capacity;
 
  640         if (m_capacity % 16 == 0) {
 
  648         reSize( new_capacity ); 
 
  653     m_val[m_size] = m_same;
 
  655     return m_val[m_size++];
 
  663     if ( newsize < m_size ) { 
 
  667     unsigned * new_I   = 
new unsigned [ newsize ]; 
 
  668     unsigned * new_J   = 
new unsigned [ newsize ];
 
  669     E        * new_val = 
new E        [ newsize ];
 
  670     if (m_capacity > 0) { 
 
  671         for (
unsigned k=0; k<m_size; ++k) {
 
  674             new_val[k] = m_val[k];
 
  683     m_capacity = newsize;
 
  704     assert( 
"Sparse_Matrix<E>::add()" && (rows() == O.
rows()) && (cols() == O.
cols()) );
 
  714     for ( ; pThis != pEND; ++pThis, ++pO) {
 
  738     assert( 
"Sparse_Matrix<E>::substract()" && (rows() == O.
rows()) && (cols() == O.
cols()) );
 
  748     for ( ; pThis != pEND; ++pThis, ++pO) {
 
  777     assert( (A.
cols() == B.
rows()) && 
" => Sparse_Matrix<E>::multiply()" );
 
  781     for (
unsigned i=0; i<rows(); i++) {
 
  782         for (
unsigned j=0; j<cols(); j++) {
 
  784             for (
unsigned k=0; k<A.
cols(); k++) {
 
  785                 sum = sum + A(i,k) * B(k,j);
 
  797 std::ostream& operator<<(std::ostream& COUT, const Sparse_Matrix<E>& M) {
 
  798     COUT << 
'[' << M.rows() << 
'x' << M.cols() << 
']' << std::endl;
 
  799     for (
unsigned i=0; i < M.rows(); ++i) {
 
  800         for (
unsigned j=0; j < M.cols(); ++j) {
 
  801             COUT << 
"  " << M(i,j);
 
  811     assert( 
"This code has not been tested" );
 
  815     for (
unsigned i=0; i<rows; i++) {
 
  816         for (
unsigned j=0; j<cols; j++) {
 
  833 #endif // Sparse_Matrix_h 
unsigned cols() const 
Cantidad de columnas de la Matriz. 
 
unsigned * m_I
Indice "i" de M(i,j) 0 <= i < m_capacity 
 
size_type capacity() const 
Cantidad máxima posible de valores diferentes que pueden ser almacenados en la matriz. 
 
Sparse_Matrix & move(Sparse_Matrix &o)
Traslada el valor de "o" a "*this". 
 
Sparse_Matrix & copy(const Sparse_Matrix &o)
Copia desde "o". 
 
Sparse_Matrix(unsigned m=1, unsigned n=1)
Constructor de vector. 
 
void clear()
Borra todos los valores de la Sparse_Matrix. 
 
Sparse_Matrix & swap(Sparse_Matrix &o)
Intercambia los valores de "*this" y "o". 
 
unsigned count() const 
Cantidad de valores almacenados en la matriz. 
 
void multiply(const Sparse_Matrix &, const Sparse_Matrix &)
Calcula la multiplicación  A * B  y la almacena en "*this". 
 
unsigned size_type
Tipo del tamaño de un objeto, similar al nombre usado en STL. 
 
value_type & reference
Tipo del objeto almacenado, similar al nombre usado en STL. 
 
E m_same
Valor almacenado en la mayor parte de la Sparse_Matrix. 
 
void add(const Sparse_Matrix &)
Le suma a "*this" la matriz "O". 
 
friend bool operator!=(const Sparse_Matrix &p, const Sparse_Matrix &q)
¿¿¿ (p != q) ??? 
 
Sparse_Matrix(const value_type V)
Matriz escalar de valor V. 
 
Funciones para manipular Matrix_BASE<>. 
 
std::istream & operator>>(std::istream &CIN, Matrix< E > &M)
Obtiene del flujo CIN el valor para M[][]. 
 
void reShape(unsigned, unsigned)
Le ajusta las dimensiones a la matriz. 
 
bool check_ok(const Matrix< T > &M)
Verifica la invariante de la clase. 
 
unsigned m_rows
Cantidad de filas de la matriz. 
 
void setDefault(const E &same)
Define el escalar que por defecto está en todas las entradas de la Sparse_Matrix. ...
 
unsigned m_size
Cantidad de valores insertados en los 3 vectores paralelos. 
 
friend Sparse_Matrix operator-(const Sparse_Matrix &A, const Sparse_Matrix &B)
Retorna A-B. 
 
friend class test_Sparse_Matrix
Datos de prueba para la clase. 
 
const value_type & const_reference
Tipo del objeto almacenado, similar al nombre usado en STL. 
 
unsigned size() const 
Cantidad de valores almacenados en la matriz. 
 
bool same(const Sparse_Matrix &o) const 
Retorna true si "o" comparte sus valores con "*this". 
 
friend bool check_ok(const Sparse_Matrix< T > &M)
Verifica la invariante de la clase. 
 
Esta es una clase matriz muy chirrisquitica almacenada como una matriz rala. 
 
unsigned rows() const 
Cantidad de filas de la matriz. 
 
bool equals(const Sparse_Matrix &o) const 
¿¿¿ (*this==o) ??? 
 
E value_type
Tipo del objeto almacenado, similar al nombre usado en STL. 
 
unsigned m_cols
Cantidad de columnas de la matris. 
 
reference operator()(unsigned, unsigned)
Retorna una referencia al elemento [i,j] de la matriz. 
 
unsigned * m_J
Indice "j" de M(i,j) 0 <= i < m_capacity 
 
void reserve(size_type _Count)
Ajusta la matriz para que pueda almacenar n valores diferentes a getDefault(). 
 
friend bool operator==(const Sparse_Matrix &p, const Sparse_Matrix &q)
¿¿¿ (p == q) ??? 
 
void reSize(unsigned, unsigned)
Le cambia las dimensiones a la matriz. 
 
E * m_val
Valor para M(i,j) 0 <= i < m_capacity 
 
friend Sparse_Matrix operator*(const Sparse_Matrix &A, const Sparse_Matrix &B)
Retorna A*B. 
 
unsigned m_capacity
Tamaño de los 3 vectores paralelos. 
 
~Sparse_Matrix()
Destructor. 
 
Sparse_Matrix & operator=(const Sparse_Matrix &o)
Sinónimo de this->copy(o) 
 
const E & getDefault()
Valor almacenado en la mayor parte de la Sparse_Matrix. 
 
friend Sparse_Matrix operator+(const Sparse_Matrix &A, const Sparse_Matrix &B)
Retorna A+B. 
 
void substract(const Sparse_Matrix &)
Le resta a "*this" la matriz "O".