Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fspvec.h
1 /*
2 This file is distributed as part of the BIAS library (Basic ImageAlgorithmS)
3 but it has not been developed by the authors of BIAS.
4 
5 For copyright, author and license information see below.
6 */
7 
8 
9 /*
10 *
11 * Template Numerical Toolkit (TNT): Linear Algebra Module
12 *
13 * Mathematical and Computational Sciences Division
14 * National Institute of Technology,
15 * Gaithersburg, MD USA
16 *
17 *
18 * This software was developed at the National Institute of Standards and
19 * Technology (NIST) by employees of the Federal Government in the course
20 * of their official duties. Pursuant to title 17 Section 105 of the
21 * United States Code, this software is not subject to copyright protection
22 * and is in the public domain. The Template Numerical Toolkit (TNT) is
23 * an experimental system. NIST assumes no responsibility whatsoever for
24 * its use by other parties, and makes no guarantees, expressed or implied,
25 * about its quality, reliability, or any other characteristic.
26 *
27 * BETA VERSION INCOMPLETE AND SUBJECT TO CHANGE
28 * see http://math.nist.gov/tnt for latest updates.
29 *
30 */
31 
32 
33 // Templated sparse vector (Fortran conventions).
34 // Used primarily to interface with Fortran sparse matrix libaries.
35 // (CANNOT BE USED AS AN STL CONTAINER.)
36 
37 #ifndef FSPVEC_H
38 #define FSPVEC_H
39 
40 #include "tnt.h"
41 #include "vec.h"
42 #include <cstdlib>
43 #include <cassert>
44 #include <iostream>
45 #include <strstream>
46 
47 
48 
49 namespace TNT
50 {
51  template <class T>
53  {
54 
55 
56  public:
57 
59  typedef T value_type;
60  typedef T element_type;
61  typedef T* pointer;
62  typedef T* iterator;
63  typedef T& reference;
64  typedef const T* const_iterator;
65  typedef const T& const_reference;
66 
67  Subscript lbound() const { return 1;}
68 
69  protected:
72  Subscript dim_; // prescribed dimension
73 
74 
75  public:
76 
77  // size and shape information
78 
79  Subscript dim() const { return dim_; }
80  Subscript num_nonzeros() const { return val_.dim(); }
81 
82  // access
83 
84  T& val(Subscript i) { return val_(i); }
85  const T& val(Subscript i) const { return val_(i); }
86 
87  Subscript &index(Subscript i) { return index_(i); }
88  const Subscript &index(Subscript i) const { return index_(i); }
89 
90  // constructors
91 
94  index_(nz), dim_(N) {};
95  Fortran_Sparse_Vector(Subscript N, Subscript nz, const T *values,
96  const Subscript *indices): val_(nz, values), index_(nz, indices),
97  dim_(N) {}
98 
100  val_(S.val_), index_(S.index_), dim_(S.dim_) {}
101 
102  // initialize from string, e.g.
103  //
104  // Fortran_Sparse_Vector<T> A(N, 2, "1.0 2.1", "1 3");
105  //
107  char *ind) : val_(nz, v), index_(nz, ind), dim_(N) {}
108 
109  // assignments
110 
112  {
113  val_.newsize(nz);
114  index_.newsize(nz);
115  dim_ = N;
116  return *this;
117  }
118 
120  {
121  val_ = A.val_;
122  index_ = A.index_;
123  dim_ = A.dim_;
124 
125  return *this;
126  }
127 
128  // methods
129 
130 
131 
132  };
133 
134 
135  /* *************************** I/O ********************************/
136 
137  template <class T>
138  ostream& operator<<(ostream &s, const Fortran_Sparse_Vector<T> &A)
139  {
140  // output format is : N nz val1 ind1 val2 ind2 ...
141  Subscript nz=A.num_nonzeros();
142 
143  s << A.dim() << " " << nz << endl;
144 
145  for (Subscript i=1; i<=nz; i++)
146  s << A.val(i) << " " << A.index(i) << endl;
147  s << endl;
148 
149  return s;
150  }
151 
152 
153  template <class T>
154  istream& operator>>(istream &s, Fortran_Sparse_Vector<T> &A)
155  {
156  // output format is : N nz val1 ind1 val2 ind2 ...
157 
158  Subscript N;
159  Subscript nz;
160 
161  s >> N >> nz;
162 
163  A.newsize(N, nz);
164 
165  for (Subscript i=1; i<=nz; i++)
166  s >> A.val(i) >> A.index(i);
167 
168 
169  return s;
170  }
171 
172 } // namespace TNT
173 
174 #endif
175 // FSPVEC_H
Fortran_Sparse_Vector< T > & newsize(Subscript N, Subscript nz)
Definition: fspvec.h:111
Fortran_Sparse_Vector(Subscript N, Subscript nz)
Definition: fspvec.h:93
std::istream & operator>>(std::istream &s, Fortran_Matrix< T > &A)
Definition: fmat.h:345
Fortran_Sparse_Vector(Subscript N, Subscript nz, const T *values, const Subscript *indices)
Definition: fspvec.h:95
const T & val(Subscript i) const
Definition: fspvec.h:85
TNT_SUBSCRIPT_TYPE Subscript
Definition: subscript.h:55
const T & const_reference
Definition: fspvec.h:65
Fortran_Sparse_Vector(Subscript N, Subscript nz, char *v, char *ind)
Definition: fspvec.h:106
Vector< T > & newsize(Subscript N)
Definition: vec.h:220
Subscript & index(Subscript i)
Definition: fspvec.h:87
Fortran_Sparse_Vector(const Fortran_Sparse_Vector< T > &S)
Definition: fspvec.h:99
const Subscript & index(Subscript i) const
Definition: fspvec.h:88
const T * const_iterator
Definition: fspvec.h:64
Subscript dim() const
Definition: fspvec.h:79
Subscript lbound() const
Definition: fspvec.h:67
Fortran_Sparse_Vector< T > & operator=(const Fortran_Sparse_Vector< T > &A)
Definition: fspvec.h:119
Vector< Subscript > index_
Definition: fspvec.h:71
T & val(Subscript i)
Definition: fspvec.h:84
Subscript num_nonzeros() const
Definition: fspvec.h:80