Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fcscmat.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 
34 // Templated compressed sparse column matrix (Fortran conventions).
35 // uses 1-based offsets in storing row indices.
36 // Used primarily to interface with Fortran sparse matrix libaries.
37 // (CANNOT BE USED AS AN STL CONTAINER.)
38 
39 
40 #ifndef FCSCMAT_H
41 #define FCSCMAT_H
42 
43 #include <iostream>
44 #include <cassert>
45 #include "tnt.h"
46 #include "vec.h"
47 
48 
49 
50 namespace TNT
51 {
52  template <class T>
54  {
55 
56  protected:
57 
58  Vector<T> val_; // data values (nz_ elements)
59  Vector<Subscript> rowind_; // row_ind (nz_ elements)
60  Vector<Subscript> colptr_; // col_ptr (n_+1 elements)
61 
62  int nz_; // number of nonzeros
63  Subscript m_; // global dimensions
65 
66  public:
67 
68 
71  : val_(S.val_), rowind_(S.rowind_), colptr_(S.colptr_), nz_(S.nz_),
72  m_(S.m_), n_(S.n_) {};
74  Subscript nz, const T *val, const Subscript *r,
75  const Subscript *c) : val_(nz, val), rowind_(nz, r),
76  colptr_(N+1, c), nz_(nz), m_(M), n_(N) {};
77 
79  Subscript nz, char *val, char *r,
80  char *c) : val_(nz, val), rowind_(nz, r),
81  colptr_(N+1, c), nz_(nz), m_(M), n_(N) {};
82 
84  Subscript nz, const T *val, Subscript *r, Subscript *c)
85  : val_(nz, val), rowind_(nz, r), colptr_(N+1, c), nz_(nz),
86  m_(M), n_(N) {};
87 
89 
90 
91  T & val(Subscript i) { return val_(i); }
92  const T & val(Subscript i) const { return val_(i); }
93 
94  Subscript & row_ind(Subscript i) { return rowind_(i); }
95  const Subscript & row_ind(Subscript i) const { return rowind_(i); }
96 
98  const Subscript col_ptr(Subscript i) const { return colptr_(i);}
99 
100 
101  Subscript num_cols() const { return m_;}
102  Subscript num_rows() const { return n_; }
103 
105  {
106 #ifdef TNT_BOUNDS_CHECK
107  BIASASSERT( 1 <= i );
108  BIASASSERT( i <= 2 );
109 #endif
110  if (i==1) return m_;
111  else if (i==2) return m_;
112  else return 0;
113  }
114 
115  Subscript num_nonzeros() const {return nz_;};
116  Subscript lbound() const {return 1;}
117 
118 
119 
122  {
123  val_ = C.val_;
124  rowind_ = C.rowind_;
125  colptr_ = C.colptr_;
126  nz_ = C.nz_;
127  m_ = C.m_;
128  n_ = C.n_;
129 
130  return *this;
131  }
132 
134  Subscript nz)
135  {
136  val_.newsize(nz);
137  rowind_.newsize(nz);
138  colptr_.newsize(N+1);
139  return *this;
140  }
141  };
142 
143  template <class T>
144  ostream& operator<<(ostream &s, const Fortran_Sparse_Col_Matrix<T> &A)
145  {
146  Subscript M=A.num_rows();
147  Subscript N=A.num_cols();
148 
149  s << M << " " << N << " " << A.num_nonzeros() << endl;
150 
151 
152  for (Subscript k=1; k<=N; k++)
153  {
154  Subscript start = A.col_ptr(k);
155  Subscript end = A.col_ptr(k+1);
156 
157  for (Subscript i= start; i<end; i++)
158  {
159  s << A.row_ind(i) << " " << k << " " << A.val(i) << endl;
160  }
161  }
162 
163  return s;
164  }
165 
166 
167 
168 } // namespace TNT
169 
170 #endif
171 /* FCSCMAT_H */
172 
Fortran_Sparse_Col_Matrix(Subscript M, Subscript N, Subscript nz, const T *val, Subscript *r, Subscript *c)
Definition: fcscmat.h:83
Vector< Subscript > rowind_
Definition: fcscmat.h:59
Fortran_Sparse_Col_Matrix(Subscript M, Subscript N, Subscript nz, char *val, char *r, char *c)
Definition: fcscmat.h:78
Fortran_Sparse_Col_Matrix & newsize(Subscript M, Subscript N, Subscript nz)
Definition: fcscmat.h:133
Fortran_Sparse_Col_Matrix(const Fortran_Sparse_Col_Matrix< T > &S)
Definition: fcscmat.h:70
T & val(Subscript i)
Definition: fcscmat.h:91
Subscript & row_ind(Subscript i)
Definition: fcscmat.h:94
TNT_SUBSCRIPT_TYPE Subscript
Definition: subscript.h:55
Subscript lbound() const
Definition: fcscmat.h:116
const T & val(Subscript i) const
Definition: fcscmat.h:92
Subscript num_rows() const
Definition: fcscmat.h:102
Vector< T > & newsize(Subscript N)
Definition: vec.h:220
Subscript col_ptr(Subscript i)
Definition: fcscmat.h:97
Subscript num_cols() const
Definition: fcscmat.h:101
Fortran_Sparse_Col_Matrix & operator=(const Fortran_Sparse_Col_Matrix &C)
Definition: fcscmat.h:120
Vector< Subscript > colptr_
Definition: fcscmat.h:60
Fortran_Sparse_Col_Matrix(Subscript M, Subscript N, Subscript nz, const T *val, const Subscript *r, const Subscript *c)
Definition: fcscmat.h:73
Subscript dim(Subscript i) const
Definition: fcscmat.h:104
const Subscript & row_ind(Subscript i) const
Definition: fcscmat.h:95
const Subscript col_ptr(Subscript i) const
Definition: fcscmat.h:98
Subscript num_nonzeros() const
Definition: fcscmat.h:115