Blender V2.61 - r43446

fspvec.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 //  Templated sparse vector (Fortran conventions).
00030 //  Used primarily to interface with Fortran sparse matrix libaries.
00031 //  (CANNOT BE USED AS AN STL CONTAINER.)
00032 
00033 #ifndef FSPVEC_H
00034 #define FSPVEC_H
00035 
00036 #include "tnt.h"
00037 #include "vec.h"
00038 #include <cstdlib>
00039 #include <cassert>
00040 #include <iostream>
00041 
00042 using namespace std;
00043 
00044 namespace TNT
00045 {
00046 
00047 template <class T>
00048 class Fortran_Sparse_Vector 
00049 {
00050 
00051 
00052   public:
00053 
00054     typedef Subscript   size_type;
00055     typedef         T   value_type;
00056     typedef         T   element_type;
00057     typedef         T*  pointer;
00058     typedef         T*  iterator;
00059     typedef         T&  reference;
00060     typedef const   T*  const_iterator;
00061     typedef const   T&  const_reference;
00062 
00063     Subscript lbound() const { return 1;}
00064  
00065   protected:
00066     Vector<T>   val_;
00067     Vector<Subscript> index_;
00068     Subscript dim_;                  // prescribed dimension
00069 
00070 
00071   public:
00072 
00073     // size and shape information
00074 
00075     Subscript dim() const { return dim_; }
00076     Subscript num_nonzeros() const { return val_.dim(); }
00077 
00078     // access
00079 
00080     T& val(Subscript i) { return val_(i); }
00081     const T& val(Subscript i) const { return val_(i); }
00082 
00083     Subscript &index(Subscript i) { return index_(i); }
00084     const Subscript &index(Subscript i) const { return index_(i); }
00085 
00086     // constructors
00087 
00088     Fortran_Sparse_Vector() : val_(), index_(), dim_(0)  {};
00089     Fortran_Sparse_Vector(Subscript N, Subscript nz) : val_(nz), 
00090             index_(nz), dim_(N)  {};
00091     Fortran_Sparse_Vector(Subscript N, Subscript nz, const T *values,
00092         const Subscript *indices): val_(nz, values), index_(nz, indices),
00093             dim_(N) {}
00094 
00095     Fortran_Sparse_Vector(const Fortran_Sparse_Vector<T> &S): 
00096         val_(S.val_), index_(S.index_), dim_(S.dim_) {}
00097 
00098     // initialize from string, e.g.
00099     //
00100     //  Fortran_Sparse_Vector<T> A(N, 2, "1.0 2.1", "1 3");
00101     //
00102     Fortran_Sparse_Vector(Subscript N, Subscript nz, char *v,
00103         char *ind) : val_(nz, v), index_(nz, ind), dim_(N) {}
00104     
00105     // assignments
00106 
00107     Fortran_Sparse_Vector<T> & newsize(Subscript N, Subscript nz)
00108     {
00109         val_.newsize(nz);
00110         index_.newsize(nz);
00111         dim_ = N;
00112         return *this;
00113     }
00114 
00115     Fortran_Sparse_Vector<T> & operator=( const Fortran_Sparse_Vector<T> &A)
00116     {
00117         val_ = A.val_;
00118         index_ = A.index_;
00119         dim_ = A.dim_;
00120 
00121         return *this;
00122     }
00123 
00124     // methods
00125 
00126 
00127 
00128 };
00129 
00130 
00131 /* ***************************  I/O  ********************************/
00132 
00133 template <class T>
00134 ostream& operator<<(ostream &s, const Fortran_Sparse_Vector<T> &A)
00135 {
00136     // output format is :   N nz val1 ind1 val2 ind2 ... 
00137     Subscript nz=A.num_nonzeros();
00138 
00139     s <<  A.dim() << " " << nz << endl;
00140 
00141     for (Subscript i=1; i<=nz; i++)
00142         s   << A.val(i) << "  " << A.index(i) << endl;
00143     s << endl;
00144 
00145     return s;
00146 }
00147 
00148 
00149 template <class T>
00150 istream& operator>>(istream &s, Fortran_Sparse_Vector<T> &A)
00151 {
00152     // output format is :   N nz val1 ind1 val2 ind2 ... 
00153 
00154     Subscript N;
00155     Subscript nz;
00156 
00157     s >> N >> nz;
00158 
00159     A.newsize(N, nz);
00160 
00161     for (Subscript i=1; i<=nz; i++)
00162             s >>  A.val(i) >> A.index(i);
00163 
00164 
00165     return s;
00166 }
00167 
00168 } // namespace TNT
00169 
00170 #endif // FSPVEC_H
00171