vnl_vector_fixed.h
Go to the documentation of this file.
1 // This is core/vnl/vnl_vector_fixed.h
2 #ifndef vnl_vector_fixed_h_
3 #define vnl_vector_fixed_h_
4 //:
5 // \file
6 // \brief Fixed length stack-stored vector
7 //
8 // The operators are inlined because (1) they are small and
9 // (2) we then have less explicit instantiation trouble.
10 //
11 // \author Andrew W. Fitzgibbon, Oxford RRG
12 // \date 04 Aug 96
13 //
14 // \verbatim
15 // Modifications
16 // LSB Manchester 16/3/01 Binary I/O added
17 // Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
18 // Oct.2002 - Amitha Perera - decoupled vnl_vector and vnl_vector_fixed for
19 // space efficiency, removed necessity for vnl_vector_fixed_ref
20 // Jun.2003 - Paul Smyth - added as_fixed_ref() to convert to fixed-size ref
21 // removed duplicate cross_3d
22 // Jun.2003 - Peter Vanroose - added cross_2d
23 // Oct.2003 - Peter Vanroose - removed deprecated x(), y(), z(), t()
24 // Mar.2009 - Peter Vanroose - added arg_min() and arg_max()
25 // Oct.2010 - Peter Vanroose - mutators and setters now return *this
26 // \endverbatim
27 
28 #include <cstring>
29 #include <iosfwd>
30 #include <cassert>
31 #ifdef _MSC_VER
32 # include <vcl_msvc_warnings.h>
33 #endif
34 #include "vnl_vector.h"
35 #include "vnl_vector_ref.h"
36 #include <vnl/vnl_c_vector.h>
37 #include <vnl/vnl_matrix.h> // outerproduct
38 #include <vnl/vnl_config.h> // for VNL_CONFIG_CHECK_BOUNDS
39 #include <vnl/vnl_error.h>
40 #include "vnl/vnl_export.h"
41 
42 template <class T, unsigned int n> class vnl_vector_fixed;
43 template <class T, unsigned int num_rows, unsigned int num_cols> class vnl_matrix_fixed;
44 
45 //: Fixed length stack-stored, space-efficient vector.
46 // vnl_vector_fixed is a fixed-length, stack storage vector. It has
47 // the same storage size as a C-style array. It is not related via
48 // inheritance to vnl_vector. However, it can be converted cheaply to
49 // a vnl_vector_ref.
50 //
51 // In most cases, a vnl_vector_fixed can be used where a vnl_vector is
52 // expected. There are some situations, however, when the automatic
53 // conversion cannot be applied. In those cases, you need to call the
54 // as_ref() method to perform an explicit conversion. This occurs most
55 // often when the called function is templated, since the user-defined
56 // conversion operators are then suppressed.
57 // \code
58 // template<class T>
59 // void do_something( const vnl_vector<T>& v );
60 // ...
61 // vnl_vector_fixed<double,4> my_vec;
62 //
63 // do_something( my_vec );
64 // // Error: no do_something( vnl_vector_fixed<double,4> ) found
65 //
66 // do_something( my_vec.as_ref() ); // works
67 // \endcode
68 //
69 // Since the conversion operator creates a temporary vnl_vector_ref
70 // object, the conversion cannot be used directly to a function that
71 // expects a non-const vnl_vector reference. Use
72 // vnl_vector_ref::non_const method for this (and only this).
73 // \code
74 // void mutator( vnl_vector<double>& v );
75 // ...
76 // vnl_vector_fixed<double,4> my_vec;
77 // mutator( my_vec.as_ref().non_const() );
78 // \endcode
79 // If the mutator only accesses the data, all should be fine. If the
80 // mutator attempts to resize the vector, you are doomed.
81 //
82 // vnl_vector_fixed defines most of the operators defined by
83 // vnl_vector, and does so efficiently. If you try to mix
84 // vnl_vector_fixed and vnl_vector, however, you will probably get a
85 // vnl_vector result, with the corresponding malloc cost.
86 template <class T, unsigned int n>
87 class VNL_EXPORT vnl_vector_fixed
88 {
89  protected:
90  T data_[n];
91 
92  public:
93  typedef vnl_vector_fixed<T,n> self;
94  typedef size_t size_type;
95  // Compile-time accessible attribute to get the dimensionality of the vector.
96  enum { SIZE = n };
97 
98  // Don't out-of-line the constructors, as extra the function call
99  // adds a significant overhead. (memcpy is often implemented with a
100  // couple of assembly instructions.)
101 
102  //: Construct an uninitialized n-vector
103  vnl_vector_fixed() = default;
104 
105  //: Copy constructor
106  // The dimensions must match.
107  vnl_vector_fixed( const vnl_vector_fixed<T,n>& rhs ) = default;
108  vnl_vector_fixed( vnl_vector_fixed<T,n>&& rhs ) = default;
109  //: Copy operator
110  vnl_vector_fixed<T,n>& operator=( const vnl_vector_fixed<T,n>& rhs ) = default;
111  vnl_vector_fixed<T,n>& operator=( vnl_vector_fixed<T,n>&& rhs ) = default;
112 
113 
114  //: Construct a fixed-n-vector copy of \a rhs.
115  // The dimensions must match.
117  {
118  assert( n == rhs.size() );
119  std::memcpy( data_, rhs.data_block(), sizeof data_ );
120  }
121 
122  //: Constructs n-vector with all elements initialised to \a v
123  explicit vnl_vector_fixed( const T& v ) { fill( v ); }
124 
125  //: Construct a fixed-n-vector initialized from \a datablck
126  // The data \e must have enough data. No checks performed.
127  explicit vnl_vector_fixed( const T* datablck )
128  {
129  std::memcpy( data_, datablck, sizeof data_ );
130  }
131 
132  //: Convenience constructor for 2-D vectors
133  // While this constructor is sometimes useful, consider using
134  // vnl_double_2 or vnl_float_2 instead.
135  vnl_vector_fixed( const T& x0, const T& x1 )
136  {
137  if ( n != 2 )
138  {
139  #ifndef NDEBUG
140  vnl_error_vector_dimension("vnl_vector_fixed()", 2, n);
141  #endif
142  return;
143  }
144 
145  data_[0] = x0; data_[1] = x1;
146  }
147 
148  //: Convenience constructor for 3-D vectors
149  // While this constructor is sometimes useful, consider using
150  // vnl_double_3 or vnl_float_3 instead.
151  vnl_vector_fixed( const T& x0, const T& x1, const T& x2 )
152  {
153  if ( n != 3 )
154  {
155  #ifndef NDEBUG
156  vnl_error_vector_dimension("vnl_vector_fixed()", 3, n);
157  #endif
158  return;
159  }
160  data_[0] = x0; data_[1] = x1; data_[2] = x2;
161  }
162 
163  //: Convenience constructor for 4-D vectors
164  vnl_vector_fixed( const T& x0, const T& x1, const T& x2, const T& x3 )
165  {
166  if ( n != 4 )
167  {
168  #ifndef NDEBUG
169  vnl_error_vector_dimension("vnl_vector_fixed()", 4, n);
170  #endif
171  return;
172  }
173  data_[0] = x0; data_[1] = x1; data_[2] = x2; data_[3] = x3;
174  }
175 
176  //: Copy data from a dynamic vector
177  // The dimensions must match.
179  assert( n == rhs.size() );
180  std::memcpy( data_, rhs.data_block(), sizeof data_ );
181  return *this;
182  }
183 
184  //: Length of the vector.
185  // This is always \a n.
186  unsigned int size() const { return n; }
187 
188  //: Put value at given position in vector.
189  inline void put (unsigned int i, T const& v)
190  {
191 #if VNL_CONFIG_CHECK_BOUNDS
192  if (i >= this->size()) // If invalid index specified
193  vnl_error_vector_index("put", i); // Raise exception
194 #endif
195  this->data_[i] = v;
196  }
197 
198  //: Get value at element i
199  T get(unsigned int i) const;
200 
201  //: Set all values to v
202  vnl_vector_fixed& fill( T const& v )
203  {
204  for ( size_type i = 0; i < n; ++i )
205  this->data_[i] = v;
206  return *this;
207  }
208 
209  //: Sets elements to ptr[i]
210  // Note: ptr[i] must be valid for i=0..size()-1
211  vnl_vector_fixed& copy_in( T const * ptr )
212  {
213  for ( size_type i = 0; i < n; ++i )
214  data_[i] = ptr[i];
215  return *this;
216  }
217 
218  //: Copy elements to ptr[i]
219  // Note: ptr[i] must be valid for i=0..size()-1
220  void copy_out( T* ptr ) const
221  {
222  for ( size_type i = 0; i < n; ++i )
223  ptr[i] = data_[i];
224  }
225 
226  //: Sets elements to ptr[i]
227  // Note: ptr[i] must be valid for i=0..size()-1
228  vnl_vector_fixed& set( T const *ptr ) { return copy_in(ptr); }
229 
230  //: Return reference to the element at specified index.
231  // There are assert style boundary checks - #define NDEBUG to turn them off.
232  T & operator() (unsigned int i);
233 
234  //: Return reference to the element at specified index.
235  // There are assert style boundary checks - #define NDEBUG to turn them off.
236  T const & operator() (unsigned int i) const;
237 
238 
239  //: Return the i-th element
240  T& operator[] (const size_t i);
241 
242  //: Return the i-th element
243  const T& operator[] (const size_t i) const;
244 
245  //: Access the contiguous block storing the elements in the vector.
246  // O(1).
247  // data_block()[0] is the first element of the vector
248  T const* data_block() const;
249 
250  //: Access the contiguous block storing the elements in the vector.
251  // O(1).
252  // data_block()[0] is the first element of the vector
253  T * data_block();
254 
255  //----------------------------------------------------------------------
256  // Conversion to vnl_vector_ref.
257 
258  // The const version of as_ref should return a const vnl_vector_ref
259  // so that the vnl_vector_ref::non_const() cannot be used on
260  // it. This prevents a const vnl_vector_fixed from being cast into a
261  // non-const vnl_vector reference, giving a slight increase in type safety.
262 
263  //: Explicit conversion to a vnl_vector_ref.
264  // This is a cheap conversion for those functions that have an interface
265  // for vnl_vector but not for vnl_vector_fixed. There is also a
266  // conversion operator that should work most of the time.
267  // \sa vnl_vector_ref::non_const
268  vnl_vector_ref<T> as_ref() { return vnl_vector_ref<T>( n, data_ ); }
269 
270  //: Explicit conversion to a vnl_vector_ref.
271  // This is a cheap conversion for those functions that have an interface
272  // for vnl_vector but not for vnl_vector_fixed. There is also a
273  // conversion operator that should work most of the time.
274  // \sa vnl_vector_ref::non_const
275  const vnl_vector_ref<T> as_ref() const { return vnl_vector_ref<T>( n, const_cast<T*>(data_) ); }
276 
277  //: Cheap conversion to vnl_vector_ref
278  // Sometimes, such as with templated functions, the compiler cannot
279  // use this user-defined conversion. For those cases, use the
280  // explicit as_ref() method instead.
281  operator const vnl_vector_ref<T>() const { return vnl_vector_ref<T>( n, const_cast<T*>(data_) ); }
282 
283  //----------------------------------------------------------------------
284 
285  //: Type defs for iterators
286  typedef T element_type;
287  //: Type defs for iterators
288  typedef T *iterator;
289  //: Iterator pointing to start of data
290  iterator begin() { return data_; }
291 
292  //: Iterator pointing to element beyond end of data
293  iterator end() { return data_+n; }
294 
295  //: Const iterator type
296  typedef T const *const_iterator;
297  //: Iterator pointing to start of data
298  const_iterator begin() const { return data_; }
299  //: Iterator pointing to element beyond end of data
300  const_iterator end() const { return data_+n; }
301 
302 
303  //: Apply f to each element.
304  // Returns a new vector with the result.
305  vnl_vector_fixed<T,n> apply(T (*f)(T));
306 
307  //: Apply f to each element.
308  // Returns a new vector with the result.
309  vnl_vector_fixed<T,n> apply(T (*f)(const T&));
310 
311  //:
312  vnl_vector_fixed<T,n>& operator+=( T s ) { self::add( data_, s, data_ ); return *this; }
313 
314  //:
315  vnl_vector_fixed<T,n>& operator-=( T s ) { self::sub( data_, s, data_ ); return *this; }
316 
317  //:
318  vnl_vector_fixed<T,n>& operator*=( T s ) { self::mul( data_, s, data_ ); return *this; }
319 
320  //:
321  vnl_vector_fixed<T,n>& operator/=( T s ) { self::div( data_, s, data_ ); return *this; }
322 
323  //:
324  vnl_vector_fixed<T,n>& operator+=( const vnl_vector_fixed<T,n>& v ) { self::add( data_, v.data_block(), data_ ); return *this; }
325 
326  //:
327  vnl_vector_fixed<T,n>& operator-=( const vnl_vector_fixed<T,n>& v ) { self::sub( data_, v.data_block(), data_ ); return *this; }
328 
329  //:
331  {
332  assert( v.size() == n );
333  self::add( data_, v.data_block(), data_ ); return *this;
334  }
335 
336  //:
338  {
339  assert( v.size() == n );
340  self::sub( data_, v.data_block(), data_ ); return *this;
341  }
342 
343  //:
345  {
346  vnl_vector_fixed<T,n> result;
347  self::sub( (T)0, data_, result.data_ );
348  return result;
349  }
350 
351  //: Returns a subvector specified by the start index and length. O(n).
352  vnl_vector<T> extract (unsigned int len, unsigned int start=0) const
353  {
354  assert( start < n && start + len <= n );
355  return vnl_vector<T>( data_ + start, len );
356  }
357 
358  //: Convert to a vnl_vector.
359  vnl_vector<T> as_vector() const { return extract(n); }
360 
361  //: Replaces elements with index beginning at start, by values of v. O(n).
362  vnl_vector_fixed& update(vnl_vector<T> const&, unsigned int start=0);
363 
364  // norms etc
365  typedef typename vnl_c_vector<T>::abs_t abs_t;
366 
367  //: Return sum of squares of elements
368  abs_t squared_magnitude() const { return vnl_c_vector<T>::two_nrm2(begin(), size()); }
369 
370  //: Return magnitude (length) of vector
371  abs_t magnitude() const { return two_norm(); }
372 
373  //: Return sum of absolute values of the elements
374  abs_t one_norm() const { return vnl_c_vector<T>::one_norm(begin(), size()); }
375 
376  //: Return sqrt of sum of squares of values of elements
377  abs_t two_norm() const { return vnl_c_vector<T>::two_norm(begin(), size()); }
378 
379  //: Return largest absolute element value
380  abs_t inf_norm() const { return vnl_c_vector<T>::inf_norm(begin(), size()); }
381 
382  //: Normalise by dividing through by the magnitude
383  vnl_vector_fixed<T,n>& normalize() { vnl_c_vector<T>::normalize(begin(), size()); return *this; }
384 
385  // These next 6 functions are should really be helper functions since they aren't
386  // really proper functions on a vector in a philosophical sense.
387 
388  //: Root Mean Squares of values
389  abs_t rms () const { return vnl_c_vector<T>::rms_norm(begin(), size()); }
390 
391  //: Smallest value
392  T min_value () const { return vnl_c_vector<T>::min_value(begin(), size()); }
393 
394  //: Largest value
395  T max_value () const { return vnl_c_vector<T>::max_value(begin(), size()); }
396 
397  //: Location of smallest value
398  unsigned arg_min() const { return vnl_c_vector<T>::arg_min(begin(), size()); }
399 
400  //: Location of largest value
401  unsigned arg_max() const { return vnl_c_vector<T>::arg_max(begin(), size()); }
402 
403  //: Mean of values in vector
404  T mean() const { return vnl_c_vector<T>::mean(begin(), size()); }
405 
406  //: Sum of values in a vector
407  T sum() const { return vnl_c_vector<T>::sum(begin(), size()); }
408 
409  //: Reverse the order of the elements
410  // Element i swaps with element size()-1-i
411  vnl_vector_fixed& flip();
412 
413  //: Check that size()==sz if not, abort();
414  // This function does or tests nothing if NDEBUG is defined
415  void assert_size( unsigned sz ) const { assert( sz == n ); (void)sz; }
416 
417  //: Check that this is finite if not, abort();
418  // This function does or tests nothing if NDEBUG is defined
419  void assert_finite() const
420  {
421 #ifndef NDEBUG
422  assert_finite_internal();
423 #endif
424  }
425 
426  //: Return true if it's finite
427  bool is_finite() const;
428 
429  //: Return true iff all the entries are zero.
430  bool is_zero() const;
431 
432  //: Return true iff the size is zero.
433  bool empty() const { return n==0; }
434 
435  //: Return true if *this == v
436  bool operator_eq (vnl_vector_fixed<T,n> const& v) const
437  {
438  for ( size_type i = 0; i < n; ++i )
439  if ( (*this)[i] != v[i] )
440  return false;
441  return true;
442  }
443 
444  //: Return true if *this == v
445  bool operator_eq (vnl_vector<T> const& v) const
446  {
447  assert( v.size() == n );
448  for ( size_type i = 0; i < n; ++i )
449  if ( (*this)[i] != v[i] )
450  return false;
451  return true;
452  }
453 
454 
455  //: Read from text stream
456  bool read_ascii(std::istream& s);
457 
458  //: Display the vector
459  // Output each element separated by a single space.
460  void print( std::ostream& s ) const;
461 
462  public:
463  // Helper routines for arithmetic. n is the size, and is the
464  // template parameter.
465 
466  inline static void add( const T* a, const T* b, T* r )
467  {
468  for ( unsigned int i=0; i < n; ++i,++r,++a,++b )
469  *r = *a + *b;
470  }
471 
472  inline static void add( const T* a, T b, T* r )
473  {
474  for ( unsigned int i=0; i < n; ++i,++r,++a )
475  *r = *a + b;
476  }
477 
478  inline static void sub( const T* a, const T* b, T* r )
479  {
480  for ( unsigned int i=0; i < n; ++i,++r,++a,++b )
481  *r = *a - *b;
482  }
483 
484  inline static void sub( const T* a, T b, T* r )
485  {
486  for ( unsigned int i=0; i < n; ++i,++r,++a )
487  *r = *a - b;
488  }
489 
490  inline static void sub( T a, const T* b, T* r )
491  {
492  for ( unsigned int i=0; i < n; ++i,++r,++b )
493  *r = a - *b;
494  }
495 
496  inline static void mul( const T* a, const T* b, T* r )
497  {
498  for ( unsigned int i=0; i < n; ++i,++r,++a,++b )
499  *r = *a * *b;
500  }
501 
502  inline static void mul( const T* a, T b, T* r )
503  {
504  for ( unsigned int i=0; i < n; ++i,++r,++a )
505  *r = *a * b;
506  }
507 
508  inline static void div( const T* a, const T* b, T* r )
509  {
510  for ( unsigned int i=0; i < n; ++i,++r,++a,++b )
511  *r = *a / *b;
512  }
513 
514  inline static void div( const T* a, T b, T* r )
515  {
516  for ( unsigned int i=0; i < n; ++i,++r,++a )
517  *r = *a / b;
518  }
519 
520  private:
521  //: See assert_finite().
522  void assert_finite_internal() const;
523 };
524 
525 
526 // --- Vector-scalar operators ----------------------------------------
527 
528 //:
529 // \relatesalso vnl_vector_fixed
530 template<class T, unsigned int n>
532 {
534  vnl_vector_fixed<T,n>::add( v.data_block(), s, r.data_block() );
535  return r;
536 }
537 
538 //:
539 // \relatesalso vnl_vector_fixed
540 template<class T, unsigned int n>
541 inline vnl_vector_fixed<T,n> operator+( const T& s,
542  const vnl_vector_fixed<T,n>& v )
543 {
545  vnl_vector_fixed<T,n>::add( v.data_block(), s, r.data_block() );
546  return r;
547 }
548 
549 //:
550 // \relatesalso vnl_vector_fixed
551 template<class T, unsigned int n>
553 {
555  vnl_vector_fixed<T,n>::sub( v.data_block(), s, r.data_block() );
556  return r;
557 }
558 
559 //:
560 // \relatesalso vnl_vector_fixed
561 template<class T, unsigned int n>
562 inline vnl_vector_fixed<T,n> operator-( const T& s,
563  const vnl_vector_fixed<T,n>& v )
564 {
566  vnl_vector_fixed<T,n>::sub( s, v.data_block(), r.data_block() );
567  return r;
568 }
569 
570 //:
571 // \relatesalso vnl_vector_fixed
572 template<class T, unsigned int n>
574 {
576  vnl_vector_fixed<T,n>::mul( v.data_block(), s, r.data_block() );
577  return r;
578 }
579 
580 //:
581 // \relatesalso vnl_vector_fixed
582 template<class T, unsigned int n>
583 inline vnl_vector_fixed<T,n> operator*( const T& s,
584  const vnl_vector_fixed<T,n>& v )
585 {
587  vnl_vector_fixed<T,n>::mul( v.data_block(), s, r.data_block() );
588  return r;
589 }
590 
591 //:
592 // \relatesalso vnl_vector_fixed
593 template<class T, unsigned int n>
595 {
597  vnl_vector_fixed<T,n>::div( v.data_block(), s, r.data_block() );
598  return r;
599 }
600 
601 
602 // --- Vector-vector operators ----------------------------------------
603 //
604 // Includes overloads for the common case of mixing a fixed with a
605 // non-fixed. Because the operators are templated, the fixed will not
606 // be automatically converted to a non-fixed-ref. These do it for you.
607 
608 //:
609 // \relatesalso vnl_vector_fixed
610 template<class T, unsigned int n>
612 {
615  return r;
616 }
617 
618 //:
619 // \relatesalso vnl_vector
620 // \relatesalso vnl_vector_fixed
621 template<class T, unsigned int n>
623 {
624  return a.as_ref() + b;
625 }
626 
627 //:
628 // \relatesalso vnl_vector
629 // \relatesalso vnl_vector_fixed
630 template<class T, unsigned int n>
632 {
633  return a + b.as_ref();
634 }
635 
636 //:
637 // \relatesalso vnl_vector_fixed
638 template<class T, unsigned int n>
640 {
643  return r;
644 }
645 
646 //:
647 // \relatesalso vnl_vector
648 // \relatesalso vnl_vector_fixed
649 template<class T, unsigned int n>
651 {
652  return a.as_ref() - b;
653 }
654 
655 //:
656 // \relatesalso vnl_vector
657 // \relatesalso vnl_vector_fixed
658 template<class T, unsigned int n>
660 {
661  return a - b.as_ref();
662 }
663 
664 //:
665 // \relatesalso vnl_vector_fixed
666 template<class T, unsigned int n>
668 {
671  return r;
672 }
673 
674 //:
675 // \relatesalso vnl_vector
676 // \relatesalso vnl_vector_fixed
677 template<class T, unsigned int n>
679 {
680  assert( b.size() == n );
681  vnl_vector<T> r(n);
682  vnl_vector_fixed<T,n>::mul( a.data_block(), b.data_block(), r.data_block() );
683  return r;
684 }
685 
686 //:
687 // \relatesalso vnl_vector
688 // \relatesalso vnl_vector_fixed
689 template<class T, unsigned int n>
691 {
692  assert( a.size() == n );
693  vnl_vector<T> r(n);
694  vnl_vector_fixed<T,n>::mul( a.data_block(), b.data_block(), r.data_block() );
695  return r;
696 }
697 
698 //:
699 // \relatesalso vnl_vector_fixed
700 template<class T, unsigned int n>
702 {
705  return r;
706 }
707 
708 //:
709 // \relatesalso vnl_vector
710 // \relatesalso vnl_vector_fixed
711 template<class T, unsigned int n>
713 {
714  assert( b.size() == n );
715  vnl_vector<T> r(n);
716  vnl_vector_fixed<T,n>::div( a.data_block(), b.data_block(), r.data_block() );
717  return r;
718 }
719 
720 //:
721 // \relatesalso vnl_vector
722 // \relatesalso vnl_vector_fixed
723 template<class T, unsigned int n>
725 {
726  assert( a.size() == n );
727  vnl_vector<T> r(n);
728  vnl_vector_fixed<T,n>::div( a.data_block(), b.data_block(), r.data_block() );
729  return r;
730 }
731 
732 //:
733 // \relatesalso vnl_vector_fixed
734 template<class T, unsigned n>
736 {
737  return dot_product( a.as_ref(), b.as_ref() );
738 }
739 
740 //:
741 // \relatesalso vnl_vector
742 // \relatesalso vnl_vector_fixed
743 template<class T, unsigned n>
744 inline T dot_product( const vnl_vector_fixed<T,n>& a, const vnl_vector<T>& b )
745 {
746  return dot_product( a.as_ref(), b );
747 }
748 
749 //:
750 // \relatesalso vnl_vector
751 // \relatesalso vnl_vector_fixed
752 template<class T, unsigned n>
753 inline T dot_product( const vnl_vector<T>& a, const vnl_vector_fixed<T,n>& b )
754 {
755  return dot_product( a, b.as_ref() );
756 }
757 
758 //:
759 // \relatesalso vnl_vector
760 // \relatesalso vnl_vector_fixed
761 template<class T, unsigned int n>
763 {
764  return outer_product( a, b.as_ref());
765 }
766 
767 //:
768 // \relatesalso vnl_vector
769 // \relatesalso vnl_vector_fixed
770 template<class T, unsigned int n>
772 {
773  return outer_product( a.as_ref(), b);
774 }
775 
776 //:
777 // \relatesalso vnl_vector_fixed
778 template<class T, unsigned n>
779 inline T angle( const vnl_vector_fixed<T,n>& a, const vnl_vector_fixed<T,n>& b )
780 {
781  return angle( a.as_ref(), b.as_ref() );
782 }
783 
784 //:
785 // \relatesalso vnl_vector
786 // \relatesalso vnl_vector_fixed
787 template<class T, unsigned n>
788 inline T angle( const vnl_vector_fixed<T,n>& a, const vnl_vector<T>& b )
789 {
790  return angle( a.as_ref(), b );
791 }
792 
793 //:
794 // \relatesalso vnl_vector
795 // \relatesalso vnl_vector_fixed
796 template<class T, unsigned n>
797 inline T angle( const vnl_vector<T>& a, const vnl_vector_fixed<T,n>& b )
798 {
799  return angle( a, b.as_ref() );
800 }
801 
802 
803 //:
804 // \relatesalso vnl_vector_fixed
805 template<class T, unsigned n>
807 {
808  return vnl_vector_ssd( a.as_ref(), b.as_ref() );
809 }
810 
811 //:
812 // \relatesalso vnl_vector
813 // \relatesalso vnl_vector_fixed
814 template<class T, unsigned n>
815 inline T vnl_vector_ssd( const vnl_vector_fixed<T,n>& a, const vnl_vector<T>& b )
816 {
817  return vnl_vector_ssd( a.as_ref(), b );
818 }
819 
820 //:
821 // \relatesalso vnl_vector
822 // \relatesalso vnl_vector_fixed
823 template<class T, unsigned n>
824 inline T vnl_vector_ssd( const vnl_vector<T>& a, const vnl_vector_fixed<T,n>& b )
825 {
826  return vnl_vector_ssd( a, b.as_ref() );
827 }
828 
829 
830 //:
831 // \relatesalso vnl_vector_fixed
832 template<class T, unsigned int n>
833 inline bool operator==( const vnl_vector_fixed<T,n>& a, const vnl_vector_fixed<T,n>& b )
834 {
835  return a.operator_eq(b);
836 }
837 
838 //:
839 // \relatesalso vnl_vector
840 // \relatesalso vnl_vector_fixed
841 template<class T, unsigned int n>
842 inline bool operator==( vnl_vector_fixed<T,n> const& a, vnl_vector<T> const& b )
843 {
844  return a.operator_eq(b);
845 }
846 
847 //:
848 // \relatesalso vnl_vector
849 // \relatesalso vnl_vector_fixed
850 template<class T, unsigned int n>
851 inline bool operator==( vnl_vector<T> const& a, vnl_vector_fixed<T,n> const& b )
852 {
853  return b.operator_eq(a);
854 }
855 
856 //:
857 // \relatesalso vnl_vector_fixed
858 template<class T, unsigned int n>
859 inline bool operator!=( const vnl_vector_fixed<T,n>& a, const vnl_vector_fixed<T,n>& b )
860 {
861  return ! a.operator_eq(b);
862 }
863 
864 //:
865 // \relatesalso vnl_vector
866 // \relatesalso vnl_vector_fixed
867 template<class T, unsigned int n>
868 inline bool operator!=( vnl_vector_fixed<T,n> const& a, vnl_vector<T> const& b )
869 {
870  return ! a.operator_eq(b);
871 }
872 
873 //:
874 // \relatesalso vnl_vector
875 // \relatesalso vnl_vector_fixed
876 template<class T, unsigned int n>
877 inline bool operator!=( vnl_vector<T> const& a, vnl_vector_fixed<T,n> const& b )
878 {
879  return ! b.operator_eq(a);
880 }
881 
882 
883 // --- I/O operators -------------------------------------------------
884 
885 
886 //:
887 // \relatesalso vnl_vector_fixed
888 template<class T, unsigned int n>
889 inline
890 std::ostream& operator<< ( std::ostream& ostr, const vnl_vector_fixed<T,n>& v )
891 {
892  v.print( ostr );
893  return ostr;
894 }
895 
896 //:
897 // \relatesalso vnl_vector_fixed
898 template<class T, unsigned int n>
899 inline
900 std::istream& operator>> ( std::istream& ostr, vnl_vector_fixed<T,n>& v )
901 {
902  v.read_ascii( ostr );
903  return ostr;
904 }
905 
906 #endif // vnl_vector_fixed_h_
static void mul(const T *a, const T *b, T *r)
vnl_vector_fixed< T, n > element_quotient(const vnl_vector_fixed< T, n > &a, const vnl_vector_fixed< T, n > &b)
abs_t magnitude() const
Return magnitude (length) of vector.
vnl_vector_fixed & copy_in(T const *ptr)
Sets elements to ptr[i].
vnl_bignum operator+(vnl_bignum const &r1, long r2)
Returns the sum of two bignum numbers.
Definition: vnl_bignum.h:279
const vnl_vector_ref< T > as_ref() const
Explicit conversion to a vnl_vector_ref.
void copy_out(T *ptr) const
Copy elements to ptr[i].
T vnl_vector_ssd(vnl_vector< T > const &v1, vnl_vector< T > const &v2)
Euclidean Distance between two vectors.
Definition: vnl_vector.h:480
vnl_vector_fixed< T, n > & operator=(const vnl_vector< T > &rhs)
Copy data from a dynamic vector.
An ordinary mathematical matrix.
static void add(const T *a, T b, T *r)
static unsigned arg_min(T const *, unsigned)
Returns location of min value of the vector.
vnl_vector_fixed(const T &x0, const T &x1, const T &x2)
Convenience constructor for 3-D vectors.
vnl_vector using user-supplied storage
vnl_vector using user-supplied storage.
Definition: vnl_fwd.h:17
void assert_finite() const
Check that this is finite if not, abort();.
bool operator_eq(vnl_vector_fixed< T, n > const &v) const
Return true if *this == v.
T element_type
Type defs for iterators.
unsigned arg_min() const
Location of smallest value.
static T sum(T const *v, unsigned n)
vnl_vector_fixed< T, n > & operator-=(const vnl_vector< T > &v)
vnl_vector_fixed< T, n > element_product(const vnl_vector_fixed< T, n > &a, const vnl_vector_fixed< T, n > &b)
size_t size() const
Return the length, number of elements, dimension of this vector.
Definition: vnl_vector.h:126
T const * data_block() const
Access the contiguous block storing the elements in the vector. O(1).
Definition: vnl_vector.h:230
vnl_vector_fixed(const T &x0, const T &x1)
Convenience constructor for 2-D vectors.
static abs_t rms_norm(T const *p, unsigned n)
rms_norm : sqrt of mean sum of squared abs values.
Definition: vnl_c_vector.h:136
unsigned int size() const
Length of the vector.
vnl_vector_fixed(const T *datablck)
Construct a fixed-n-vector initialized from datablck.
static void sub(const T *a, T b, T *r)
void add(const vnl_bignum &b1, const vnl_bignum &b2, vnl_bignum &sum)
add two non-infinite vnl_bignum values and save their sum.
Definition: vnl_bignum.cxx:887
vnl_vector_ref< T > as_ref()
Explicit conversion to a vnl_vector_ref.
Fixed size, stack-stored, space-efficient matrix.
Definition: vnl_fwd.h:23
vnl_vector_fixed & fill(T const &v)
Set all values to v.
static void sub(T a, const T *b, T *r)
const_iterator end() const
Iterator pointing to element beyond end of data.
T const * const_iterator
Const iterator type.
vnl_vector_fixed< T, n > & operator-=(const vnl_vector_fixed< T, n > &v)
static abs_t one_norm(T const *p, unsigned n)
one_norm : sum of abs values.
Definition: vnl_c_vector.h:120
static abs_t two_norm(T const *p, unsigned n)
two_norm : sqrt of sum of squared abs values.
Definition: vnl_c_vector.h:124
abs_t one_norm() const
Return sum of absolute values of the elements.
iterator end()
Iterator pointing to element beyond end of data.
std::ostream & operator<<(std::ostream &s, vnl_decnum const &r)
decimal output.
Definition: vnl_decnum.h:393
vnl_vector_fixed(const T &x0, const T &x1, const T &x2, const T &x3)
Convenience constructor for 4-D vectors.
vnl_vector_fixed< T, n > & operator-=(T s)
#define v
Definition: vnl_vector.h:42
abs_t rms() const
Root Mean Squares of values.
void vnl_error_vector_dimension(char const *fcn, int l1, int l2)
Raise exception for invalid dimension.
Definition: vnl_error.cxx:30
vnl_vector_fixed(const T &v)
Constructs n-vector with all elements initialised to v.
abs_t squared_magnitude() const
Return sum of squares of elements.
T mean() const
Mean of values in vector.
bool operator==(int r1, vnl_finite_int< N > const &r2)
Definition: vnl_finite.h:385
void assert_size(unsigned sz) const
Check that size()==sz if not, abort();.
T * iterator
Type defs for iterators.
static unsigned arg_max(T const *, unsigned)
Returns location of max value of the vector.
static T min_value(T const *, unsigned)
Returns min value of the vector.
vnl_vector_fixed< T, n > & operator+=(const vnl_vector_fixed< T, n > &v)
T max_value() const
Largest value.
An ordinary mathematical matrix.
Definition: vnl_adjugate.h:22
T min_value() const
Smallest value.
static T mean(T const *p, unsigned n)
Definition: vnl_c_vector.h:108
vnl_vector_fixed< T, n > & operator+=(const vnl_vector< T > &v)
T const * data_block() const
Access the contiguous block storing the elements in the vector.
vnl_vector< T > extract(unsigned int len, unsigned int start=0) const
Returns a subvector specified by the start index and length. O(n).
T dot_product(const vnl_vector_fixed< T, n > &a, const vnl_vector_fixed< T, n > &b)
vnl_vector< T > as_vector() const
Convert to a vnl_vector.
vnl_numeric_traits< T >::abs_t abs_t
Definition: vnl_c_vector.h:42
abs_t inf_norm() const
Return largest absolute element value.
Mathematical vector class, templated by type of element.
Definition: vnl_fwd.h:16
VNL_EXPORT vnl_matrix_fixed< T, m, n > outer_product(vnl_vector_fixed< T, m > const &a, vnl_vector_fixed< T, n > const &b)
vnl_c_vector< T >::abs_t abs_t
const_iterator begin() const
Iterator pointing to start of data.
Fixed length stack-stored, space-efficient vector.
Definition: vnl_fwd.h:22
vnl_vector_fixed & set(T const *ptr)
Sets elements to ptr[i].
static void normalize(T *, unsigned n)
iterator begin()
Iterator pointing to start of data.
static void mul(const T *a, T b, T *r)
vnl_vector_fixed< T, n > & operator+=(T s)
abs_t two_norm() const
Return sqrt of sum of squares of values of elements.
static void div(const T *a, T b, T *r)
T angle(const vnl_vector_fixed< T, n > &a, const vnl_vector_fixed< T, n > &b)
vnl_vector_fixed(const vnl_vector< T > &rhs)
Construct a fixed-n-vector copy of rhs.
VNL_EXPORT std::istream & operator>>(std::istream &s, vnl_decnum &r)
decimal input.
Definition: vnl_decnum.cxx:411
vnl_bignum operator-(vnl_bignum const &r1, vnl_bignum const &r2)
Returns the difference of two bignum numbers.
Definition: vnl_bignum.h:290
bool operator!=(int r1, vnl_finite_int< N > const &r2)
Definition: vnl_finite.h:390
void vnl_error_vector_index(char const *fcn, int index)
Raise exception for invalid index.
Definition: vnl_error.cxx:21
static T max_value(T const *, unsigned)
Returns max value of the vector.
static void add(const T *a, const T *b, T *r)
vnl_vector_fixed< T, n > & normalize()
Normalise by dividing through by the magnitude.
void put(unsigned int i, T const &v)
Put value at given position in vector.
vnl_bignum operator/(vnl_bignum const &r1, vnl_bignum const &r2)
Returns the division of two bignum numbers.
Definition: vnl_bignum.h:349
bool empty() const
Return true iff the size is zero.
vnl_bignum operator *(vnl_bignum const &r1, vnl_bignum const &r2)
Returns the product of two bignum numbers.
Definition: vnl_bignum.h:302
vnl_vector_fixed< T, n > operator-() const
static abs_t two_nrm2(T const *p, unsigned n)
two_nrm2 : sum of squared abs values.
Definition: vnl_c_vector.h:132
Math on blocks of memory.
unsigned arg_max() const
Location of largest value.
static abs_t inf_norm(T const *p, unsigned n)
inf_norm : max of abs values.
Definition: vnl_c_vector.h:128
bool operator_eq(vnl_vector< T > const &v) const
Return true if *this == v.
static void sub(const T *a, const T *b, T *r)
T sum() const
Sum of values in a vector.
static void div(const T *a, const T *b, T *r)
vnl_vector_fixed< T, n > & operator/=(T s)