Blender V2.61 - r43446

fcscmat.h

Go to the documentation of this file.
00001 
00004 /*
00005 
00006 *
00007 * Template Numerical Toolkit (TNT): Linear Algebra Module
00008 *
00009 * Mathematical and Computational Sciences Division
00010 * National Institute of Technology,
00011 * Gaithersburg, MD USA
00012 *
00013 *
00014 * This software was developed at the National Institute of Standards and
00015 * Technology (NIST) by employees of the Federal Government in the course
00016 * of their official duties. Pursuant to title 17 Section 105 of the
00017 * United States Code, this software is not subject to copyright protection
00018 * and is in the public domain.  The Template Numerical Toolkit (TNT) is
00019 * an experimental system.  NIST assumes no responsibility whatsoever for
00020 * its use by other parties, and makes no guarantees, expressed or implied,
00021 * about its quality, reliability, or any other characteristic.
00022 *
00023 * BETA VERSION INCOMPLETE AND SUBJECT TO CHANGE
00024 * see http://math.nist.gov/tnt for latest updates.
00025 *
00026 */
00027 
00028 
00029 
00030 //  Templated compressed sparse column matrix (Fortran conventions).
00031 //  uses 1-based offsets in storing row indices.
00032 //  Used primarily to interface with Fortran sparse matrix libaries.
00033 //  (CANNOT BE USED AS AN STL CONTAINER.)
00034 
00035 
00036 #ifndef FCSCMAT_H
00037 #define FCSCMAT_H
00038 
00039 #include <iostream>
00040 #include <cassert>
00041 #include "tnt.h"
00042 #include "vec.h"
00043 
00044 using namespace std;
00045 
00046 namespace TNT
00047 {
00048 
00049 template <class T>
00050 class Fortran_Sparse_Col_Matrix 
00051 {
00052 
00053    protected:
00054 
00055        Vector<T>           val_;       // data values (nz_ elements)
00056        Vector<Subscript>   rowind_;    // row_ind (nz_ elements)
00057        Vector<Subscript>   colptr_;    // col_ptr (n_+1 elements)
00058 
00059        int nz_;                   // number of nonzeros
00060        Subscript m_;              // global dimensions
00061        Subscript n_;
00062   
00063    public:
00064 
00065 
00066        Fortran_Sparse_Col_Matrix(void);
00067        Fortran_Sparse_Col_Matrix(const Fortran_Sparse_Col_Matrix<T> &S)
00068         : val_(S.val_), rowind_(S.rowind_), colptr_(S.colptr_), nz_(S.nz_),
00069             m_(S.m_), n_(S.n_) {};
00070        Fortran_Sparse_Col_Matrix(Subscript M, Subscript N, 
00071             Subscript nz, const T  *val,  const Subscript *r, 
00072             const Subscript *c) : val_(nz, val), rowind_(nz, r), 
00073             colptr_(N+1, c), nz_(nz), m_(M), n_(N) {};
00074 
00075        Fortran_Sparse_Col_Matrix(Subscript M, Subscript N, 
00076             Subscript nz, char *val,  char  *r, 
00077             char *c) : val_(nz, val), rowind_(nz, r), 
00078             colptr_(N+1, c), nz_(nz), m_(M), n_(N) {};
00079 
00080        Fortran_Sparse_Col_Matrix(Subscript M, Subscript N, 
00081             Subscript nz, const T  *val, Subscript *r, Subscript *c)
00082             : val_(nz, val), rowind_(nz, r), colptr_(N+1, c), nz_(nz), 
00083                     m_(M), n_(N) {};
00084     
00085       ~Fortran_Sparse_Col_Matrix() {};
00086         
00087 
00088        T &      val(Subscript i) { return val_(i); }
00089        const T &      val(Subscript i) const { return val_(i); }
00090 
00091        Subscript &   row_ind(Subscript i) { return rowind_(i); }
00092        const Subscript &   row_ind(Subscript i) const { return rowind_(i); }
00093 
00094        Subscript    col_ptr(Subscript i) { return colptr_(i);}
00095        const Subscript    col_ptr(Subscript i) const { return colptr_(i);}
00096 
00097 
00098        Subscript    num_cols() const { return m_;}
00099        Subscript    num_rows() const { return n_; }
00100 
00101        Subscript          dim(Subscript i) const 
00102        {
00103 #ifdef TNT_BOUNDS_CHECK
00104             assert( 1 <= i );
00105             assert( i <= 2 );
00106 #endif
00107             if (i==1) return m_;
00108             else if (i==2) return m_;
00109             else return 0;
00110         }
00111 
00112        Subscript          num_nonzeros() const {return nz_;};
00113        Subscript          lbound() const {return 1;}
00114 
00115 
00116 
00117        Fortran_Sparse_Col_Matrix& operator=(const 
00118             Fortran_Sparse_Col_Matrix &C)
00119         {
00120             val_ = C.val_;
00121             rowind_ = C.rowind_;
00122             colptr_ = C.colptr_;
00123             nz_ = C.nz_;
00124             m_ = C.m_;
00125             n_ = C.n_;
00126 
00127             return *this;
00128         }
00129 
00130        Fortran_Sparse_Col_Matrix& newsize(Subscript M, Subscript N, 
00131                 Subscript nz)
00132         {
00133             val_.newsize(nz);
00134             rowind_.newsize(nz);
00135             colptr_.newsize(N+1);
00136             return *this;
00137         }
00138 };
00139 
00140 template <class T>
00141 ostream& operator<<(ostream &s, const Fortran_Sparse_Col_Matrix<T> &A)
00142 {
00143     Subscript M=A.num_rows();
00144     Subscript N=A.num_cols();
00145 
00146     s << M << " " << N << " " << A.num_nonzeros() <<  endl;
00147 
00148 
00149     for (Subscript k=1; k<=N; k++)
00150     {
00151         Subscript start = A.col_ptr(k);
00152         Subscript end = A.col_ptr(k+1);
00153 
00154         for (Subscript i= start; i<end; i++)
00155         {
00156             s << A.row_ind(i) << " " << k << " " << A.val(i) << endl;
00157         }
00158     }
00159 
00160     return s;
00161 }
00162 
00163 
00164 } // namespace TNT
00165 
00166 #endif  /* FCSCMAT_H */
00167