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