Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vec.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 // Basic TNT numerical vector (0-based [i] AND 1-based (i) indexing )
34 //
35 
36 #ifndef TNT_VEC_H
37 #define TNT_VEC_H
38 
39 
40 #include "subscript.h"
41 #include <cstdlib>
42 #include <cassert>
43 #include <iostream>
44 #include <iomanip>
45 #include <sstream>
46 #include <string>
47 #include <Base/Math/Utils.hh>
48 #include <Base/Debug/Error.hh>
49 
50 
51 namespace TNT
52 {
53 
54  template <class T>
55  class Vector
56  {
57 
58 
59  public:
60 
62  typedef T value_type;
63  typedef T element_type;
64  typedef T* pointer;
65  typedef T* iterator;
66  typedef T& reference;
67  typedef const T* const_iterator;
68  typedef const T& const_reference;
69 
70  Subscript lbound() const { return 1;}
71 
72  protected:
73  T* v_;
74  T* vm1_; // pointer adjustment for optimzied 1-offset indexing
76 
77  // internal helper function to create the array
78  // of row pointers
79 
81  {
82  // adjust pointers so that they are 1-offset:
83  // v_[] is the internal contiguous array, it is still 0-offset
84  //
85  BIASASSERT(v_ == NULL);
86  v_ = new T[N];
87  BIASASSERT(v_ != NULL);
88  vm1_ = v_-1;
89  n_ = N;
90  }
91 
92  void copy(const T* v)
93  {
94  Subscript N = n_;
95  Subscript i;
96 
97 #ifdef TNT_UNROLL_LOOPS
98  Subscript Nmod4 = N & 3;
99  Subscript N4 = N - Nmod4;
100 
101  for (i=0; i<N4; i+=4)
102  {
103  v_[i] = v[i];
104  v_[i+1] = v[i+1];
105  v_[i+2] = v[i+2];
106  v_[i+3] = v[i+3];
107  }
108 
109  for (i=N4; i< N; i++)
110  v_[i] = v[i];
111 #else
112 
113  for (i=0; i< N; i++)
114  v_[i] = v[i];
115 #endif
116  }
117 
118  void set(const T& val)
119  {
120  Subscript N = n_;
121  Subscript i;
122 
123 #ifdef TNT_UNROLL_LOOPS
124  Subscript Nmod4 = N & 3;
125  Subscript N4 = N - Nmod4;
126 
127  for (i=0; i<N4; i+=4)
128  {
129  v_[i] = val;
130  v_[i+1] = val;
131  v_[i+2] = val;
132  v_[i+3] = val;
133  }
134 
135  for (i=N4; i< N; i++)
136  v_[i] = val;
137 #else
138 
139  for (i=0; i< N; i++)
140  v_[i] = val;
141 
142 #endif
143  }
144 
145 
146 
147  void destroy()
148  {
149  /* do nothing, if no memory has been previously allocated */
150  if (v_ == NULL)
151  return ;
152 
153  /* if we are here, then matrix was previously allocated */
154  delete [] (v_); v_ = NULL;
155  vm1_ = NULL;
156  n_ = 0;
157  }
158 
159 
160  public:
161 
162  // access
163 
164  iterator begin() { return v_;}
165  iterator end() { return v_ + n_; }
166  const iterator begin() const { return v_;}
167  const iterator end() const { return v_ + n_; }
168 
169  // destructor
170 
172  {
173  destroy();
174  }
175 
176  // constructors
177 
178  Vector() : v_(0), vm1_(0), n_(0) {};
179 
180  Vector(const Vector<T> &A) : v_(0), vm1_(0), n_(0)
181  {
182  initialize(A.n_);
183  copy(A.v_);
184  }
185 
186  Vector(Subscript N, const T& value = T()) : v_(0), vm1_(0), n_(0)
187  {
188  initialize(N);
189  set(value);
190  }
191 
192  Vector(Subscript N, const T* v) : v_(0), vm1_(0), n_(0)
193  {
194  initialize(N);
195  copy(v);
196  }
197 
198  //Vector(Subscript N, char *s) : v_(0), vm1_(0), n_(0)
199  //{
200  // initialize(N);
201  // std::istringstream ins(s);
202  // Subscript i;
203  // for (i=0; i<N; i++)
204  // ins >> v_[i];
205  //}
206 
207  Vector(const Subscript N, const std::string & s) : v_(0), vm1_(0), n_(0)
208  {
209  initialize(N);
210  std::istringstream ins(s);
211  Subscript i;
212  for (i=0; i<N; i++)
213  ins >> v_[i];
214  }
215 
216 
217 
218  // methods
219  //
221  {
222  if (n_ == N) return *this;
223 
224  destroy();
225  initialize(N);
226 
227  return *this;
228  }
229 
230 
231  // assignments
232  //
234  {
235  if (v_ == A.v_)
236  return *this;
237 
238  if (n_ == A.n_) // no need to re-alloc
239  copy(A.v_);
240 
241  else
242  {
243  destroy();
244  initialize(A.n_);
245  copy(A.v_);
246  }
247 
248  return *this;
249  }
250 
251  Vector<T>& operator=(const T& scalar)
252  {
253  set(scalar);
254  return *this;
255  }
256 
257  inline Subscript dim() const
258  {
259  return n_;
260  }
261 
262  inline Subscript size() const
263  {
264  return n_;
265  }
266 
267 
269  {
270 #ifdef TNT_BOUNDS_CHECK
271  BIASASSERT(1<=i);
272  BIASASSERT(i <= n_) ;
273 #endif
274  return vm1_[i];
275  }
276 
278  {
279 #ifdef TNT_BOUNDS_CHECK
280  BIASASSERT(1<=i);
281  BIASASSERT(i <= n_) ;
282 #endif
283  return vm1_[i];
284  }
285 
287  {
288 #ifdef TNT_BOUNDS_CHECK
289  BIASASSERT(0<=i);
290  BIASASSERT(i < n_) ;
291 #endif
292  return v_[i];
293  }
294 
296  {
297 #ifdef TNT_BOUNDS_CHECK
298  BIASASSERT(0<=i);
299  BIASASSERT(i < n_) ;
300 #endif
301  return v_[i];
302  }
303  };
304 
305 
306  /* *************************** I/O ********************************/
307 
308  /// \todo refactor operator<< into BIAS::Vector::Print function with flags, see BIAS::Matrix as example. JW
309  inline
310  std::ostream& operator<<(std::ostream &s, const Vector<char> &A)
311  {
312  std::ios_base::fmtflags oldFlags = s.flags(); // push
313 
314  Subscript N=A.dim();
315  s<<std::setprecision( std::numeric_limits<Subscript>::digits10 +1 );
316  s << N << std::endl;
317  const unsigned int dig = BIAS::DecimalsToStore<char>();
318  for (Subscript i=0; i<N; i++) {
319  s<<std::setw(3);
320  s<<std::setprecision( dig );
321  s<<(int)A[i]<< " " << std::endl;
322  }
323  s << std::endl;
324 
325  s.flags(oldFlags); // pop
326  return s;
327  }
328 
329  inline
330  std::ostream& operator<<(std::ostream &s, const Vector<unsigned char> &A)
331  {
332  std::ios_base::fmtflags oldFlags = s.flags(); // push
333 
334  Subscript N=A.dim();
335  s<<std::setprecision( std::numeric_limits<Subscript>::digits10 +1 );
336  s << N << std::endl;
337  const unsigned int dig = BIAS::DecimalsToStore<unsigned char>();
338  for (Subscript i=0; i<N; i++) {
339  s<<std::setw(3);
340  s<<std::setprecision( dig );
341  s<<(int)A[i]<< " " << std::endl;
342  }
343  s << std::endl;
344 
345  s.flags(oldFlags); // pop
346  return s;
347  }
348 
349 
350  template <class T>
351  inline
352  std::ostream& operator<<(std::ostream &s, const Vector<T> &A)
353  {
354  std::ios_base::fmtflags oldFlags = s.flags(); // push
355 
356  Subscript N=A.dim();
357  s<<std::setprecision( std::numeric_limits<Subscript>::digits10 +1 );
358  s << N << std::endl;
359  const unsigned int dig = BIAS::DecimalsToStore<T>();
360  for (Subscript i=0; i<N; i++) {
361  s<<std::setw(dig+8);
362  s<<std::setprecision( dig );
363  s<<A[i]<< " " << std::endl;
364  }
365  s << std::endl;
366 
367  s.flags(oldFlags); // pop
368  return s;
369  }
370 
371 
372  inline
373  std::istream & operator>>(std::istream &s, Vector<char> &A)
374  {
375  #ifdef min
376  #undef min
377  #endif
378  #ifdef max
379  #undef max
380  #endif
381  Subscript N;
382  s >> N;
383  if ( !(N == A.size() ))
384  {
385  A.newsize(N);
386  }
387  int tmp(0);
388  for (Subscript i=0; i<N; i++){
389  s >> tmp;
390  BIASASSERT(tmp>=std::numeric_limits<char>::min() );
391  BIASASSERT(tmp<=std::numeric_limits<char>::max() );
392  A[i]=(char)tmp;
393  }
394  return s;
395  }
396 
397 
398  inline
399  std::istream & operator>>(std::istream &s, Vector<unsigned char> &A)
400  {
401  Subscript N;
402  s >> N;
403  if ( !(N == A.size() ))
404  {
405  A.newsize(N);
406  }
407  int tmp(0);
408  for (Subscript i=0; i<N; i++){
409  s >> tmp;
410  BIASASSERT(tmp>=std::numeric_limits<unsigned char>::min() );
411  BIASASSERT(tmp<=std::numeric_limits<unsigned char>::max() );
412  A[i]=(unsigned char)tmp;
413  }
414  return s;
415  }
416 
417 
418  template <class T>
419  inline
420  std::istream & operator>>(std::istream &s, Vector<T> &A)
421  {
422  Subscript N;
423  s >> N;
424  if ( !(N == A.size() ))
425  {
426  A.newsize(N);
427  }
428  for (Subscript i=0; i<N; i++)
429  s >> A[i];
430  return s;
431  }
432 
433  // *******************[ basic matrix algorithms ]***************************
434 
435 
436  template <class T>
438  const Vector<T> &B)
439  {
440  Subscript N = A.dim();
441 
442  BIASASSERT(N==B.dim());
443 
444  Vector<T> tmp(N);
445  Subscript i;
446 
447  for (i=0; i<N; i++)
448  tmp[i] = A[i] + B[i];
449 
450  return tmp;
451  }
452 
453  template <class T>
455  const Vector<T> &B)
456  {
457  Subscript N = A.dim();
458 
459  BIASASSERT(N==B.dim());
460 
461  Vector<T> tmp(N);
462  Subscript i;
463 
464  for (i=0; i<N; i++)
465  tmp[i] = A[i] - B[i];
466 
467  return tmp;
468  }
469 
470  template <class T>
472  const Vector<T> &B)
473  {
474  Subscript N = A.dim();
475 
476  BIASASSERT(N==B.dim());
477 
478  Vector<T> tmp(N);
479  Subscript i;
480 
481  for (i=0; i<N; i++)
482  tmp[i] = A[i] * B[i];
483 
484  return tmp;
485  }
486 
487 
488  template <class T>
489  T dot_prod(const Vector<T> &A, const Vector<T> &B)
490  {
491  Subscript N = A.dim();
492  BIASASSERT(N == B.dim());
493 
494  Subscript i;
495  T sum = 0;
496 
497  for (i=0; i<N; i++)
498  sum += A[i] * B[i];
499 
500  return sum;
501  }
502 
503 } /* namespace TNT */
504 
505 #endif
506 // TNT_VEC_H
Vector< T > & operator=(const Vector< T > &A)
Definition: vec.h:233
reference operator[](Subscript i)
Definition: vec.h:286
void copy(const T *v)
Definition: vec.h:92
void set(const T &val)
Definition: vec.h:118
T * iterator
Definition: vec.h:65
T element_type
Definition: vec.h:63
std::istream & operator>>(std::istream &s, Fortran_Matrix< T > &A)
Definition: fmat.h:345
const_reference operator[](Subscript i) const
Definition: vec.h:295
Fortran_Matrix< T > operator-(const Fortran_Matrix< T > &A, const Fortran_Matrix< T > &B)
Definition: fmat.h:392
T * pointer
Definition: vec.h:64
T dot_prod(const Vector< T > &A, const Vector< T > &B)
Definition: vec.h:489
void destroy()
Definition: vec.h:147
TNT_SUBSCRIPT_TYPE Subscript
Definition: subscript.h:55
Subscript dim() const
Definition: vec.h:257
T & reference
Definition: vec.h:66
Vector(Subscript N, const T *v)
Definition: vec.h:192
Vector< T > & newsize(Subscript N)
Definition: vec.h:220
Vector(const Subscript N, const std::string &s)
Definition: vec.h:207
Vector(Subscript N, const T &value=T())
Definition: vec.h:186
iterator end()
Definition: vec.h:165
const iterator begin() const
Definition: vec.h:166
Vector()
Definition: vec.h:178
T * vm1_
Definition: vec.h:74
reference operator()(Subscript i)
Definition: vec.h:268
const T * const_iterator
Definition: vec.h:67
Vector< T > & operator=(const T &scalar)
Definition: vec.h:251
Fortran_Matrix< T > operator*(const Fortran_Matrix< T > &A, const Fortran_Matrix< T > &B)
Definition: fmat.h:484
T value_type
Definition: vec.h:62
Subscript lbound() const
Definition: vec.h:70
T * v_
Definition: vec.h:73
iterator begin()
Definition: vec.h:164
~Vector()
Definition: vec.h:171
void initialize(Subscript N)
Definition: vec.h:80
Subscript size_type
Definition: vec.h:61
Fortran_Matrix< T > operator+(const Fortran_Matrix< T > &A, const Fortran_Matrix< T > &B)
Definition: fmat.h:372
const iterator end() const
Definition: vec.h:167
Vector(const Vector< T > &A)
Definition: vec.h:180
Subscript size() const
Definition: vec.h:262
Subscript n_
Definition: vec.h:75
const T & const_reference
Definition: vec.h:68