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