vnl_vector.hxx
Go to the documentation of this file.
1 // This is core/vnl/vnl_vector.hxx
2 #ifndef vnl_vector_hxx_
3 #define vnl_vector_hxx_
4 //:
5 // \file
6 // \author VDN
7 // \date Feb 21, 1992
8 // \brief new lite version adapted from Matrix.h
9 //
10 // The parameterized vnl_vector<T> class implements 1D arithmetic vectors of a
11 // user specified type. The only constraint placed on the type is that
12 // it must overload the following operators: +, -, *, and /. Thus, it will
13 // be possible to have a vnl_vector over std::complex<T>. The vnl_vector<T>
14 // class is static in size, that is once a vnl_vector<T> of a particular
15 // size has been declared, elements cannot be added or removed. Using the
16 // set_size() method causes the vector to resize, but the contents will be
17 // lost.
18 //
19 // Each vector contains a protected data section that has a T* slot that
20 // points to the physical memory allocated for the one dimensional array. In
21 // addition, an integer specifies the number of elements for the
22 // vector. These values are provided in the constructors.
23 //
24 // Several constructors are provided. See .h file for descriptions.
25 //
26 // Methods are provided for destructive scalar and vector addition,
27 // multiplication, check for equality and inequality, fill, reduce, and access
28 // and set individual elements. Finally, both the input and output operators
29 // are overloaded to allow for formatted input and output of vector elements.
30 //
31 // vnl_vector is a special type of matrix, and is implemented for space and time
32 // efficiency. When vnl_vector is pre_multiplied by/with matrix, m*v, vnl_vector is
33 // implicitly a column matrix. When vnl_vector is post_multiplied by/with matrix
34 // v*m, vnl_vector is implicitly a row matrix.
35 //
36 
37 #include <cstdlib>
38 #include <vector>
39 #include <iostream>
40 #include <algorithm>
41 #include "vnl_vector.h"
42 
43 #ifdef _MSC_VER
44 # include <vcl_msvc_warnings.h>
45 #endif
46 #include <cassert>
47 
48 #include <vnl/vnl_math.h>
49 #include <vnl/vnl_matrix.h>
50 #include <vnl/vnl_numeric_traits.h>
51 
52 #include <vnl/vnl_c_vector.h>
53 
54 #include <vnl/vnl_sse.h>
55 
56 //--------------------------------------------------------------------------------
57 
58 # define vnl_vector_construct_hack()
59 
60 // This macro allocates the dynamic storage used by a vnl_vector.
61 
62 #define vnl_vector_alloc_blah(size) \
63 do { \
64  this->num_elmts = (size); \
65  this->data = (size) ? vnl_c_vector<T>::allocate_T(size) : 0; \
66 } while (false)
67 
68 // This macro deallocates the dynamic storage used by a vnl_vector.
69 #define vnl_vector_free_blah \
70 do { \
71  if (this->data) \
72  vnl_c_vector<T>::deallocate(this->data, this->num_elmts); \
73 } while (false)
74 
75 
76 //: Creates a vector with specified length. O(n).
77 // Elements are not initialized.
78 
79 template<class T>
81 {
84 }
85 
86 
87 //: Creates a vector of specified length, and initialize all elements with value. O(n).
88 
89 template<class T>
90 vnl_vector<T>::vnl_vector (size_t len, T const& value)
91 {
94  if (this->data) //VS2012 Runtime Library's std::fill_n has debug assertion when data is null
95  {
96  std::fill_n( this->data, len, value );
97  }
98 }
99 
100 //: Creates a vector of specified length and initialize first n elements with values. O(n).
101 
102 template<class T>
103 vnl_vector<T>::vnl_vector (size_t len, size_t n, T const values[])
104 {
107  if (n > 0) { // If user specified values
108  for (size_t i = 0; i < len && n; i++, n--) // Initialize first n elements
109  this->data[i] = values[i]; // with values
110  }
111 }
112 
113 
114 //: Creates a new copy of vector v. O(n).
115 template<class T>
117 {
119  vnl_vector_alloc_blah(v.num_elmts);
120  if ( v.data ) {
121  std::copy( v.data, v.data + v.num_elmts, this->data );
122  }
123 }
124 
125 //: Creates a vector from a block array of data, stored row-wise.
126 // Values in datablck are copied. O(n).
127 
128 template<class T>
129 vnl_vector<T>::vnl_vector (T const* datablck, size_t len)
130 {
133  std::copy( datablck, datablck + len, this->data );
134 }
135 
136 //------------------------------------------------------------
137 
138 template<class T>
140 {
143 #if VNL_CONFIG_CHECK_BOUNDS && (!defined NDEBUG)
144  if (u.size() != v.size())
145  vnl_error_vector_dimension ("vnl_vector<>::vnl_vector(v, v, vnl_vector_add_tag)", u.size(), v.size());
146 #endif
147  for (size_t i=0; i<num_elmts; ++i)
148  data[i] = u[i] + v[i];
149 }
150 
151 template<class T>
153 {
156 #if VNL_CONFIG_CHECK_BOUNDS && (!defined NDEBUG)
157  if (u.size() != v.size())
158  vnl_error_vector_dimension ("vnl_vector<>::vnl_vector(v, v, vnl_vector_sub_tag)", u.size(), v.size());
159 #endif
160  for (size_t i=0; i<num_elmts; ++i)
161  data[i] = u[i] - v[i];
162 }
163 
164 template<class T>
166 {
169  for (size_t i=0; i<num_elmts; ++i)
170  data[i] = u[i] * s;
171 }
172 
173 template<class T>
175 {
178  for (size_t i=0; i<num_elmts; ++i)
179  data[i] = u[i] / s;
180 }
181 
182 template<class T>
184 {
187  for (size_t i=0; i<num_elmts; ++i)
188  data[i] = u[i] + s;
189 }
190 
191 template<class T>
193 {
196  for (size_t i=0; i<num_elmts; ++i)
197  data[i] = u[i] - s;
198 }
199 
200 template<class T>
202 {
205 
206 #ifndef NDEBUG
207  if (M.cols() != v.size())
208  vnl_error_vector_dimension ("vnl_vector<>::vnl_vector(M, v, vnl_vector_mul_tag)", M.cols(), v.size());
209 #endif
210  vnl_sse<T>::matrix_x_vector(M.begin(), v.begin(), this->begin(), M.rows(), M.cols());
211 }
212 
213 template<class T>
215 {
218 #ifndef NDEBUG
219  if (v.size() != M.rows())
220  vnl_error_vector_dimension ("vnl_vector<>::vnl_vector(v, M, vnl_vector_mul_tag)", v.size(), M.rows());
221 #endif
222  vnl_sse<T>::vector_x_matrix(v.begin(), M.begin(), this->begin(), M.rows(), M.cols());
223 }
224 
225 template<class T>
227 {
228  if (data) destroy();
229 }
230 
231 //: Frees up the array inside vector. O(1).
232 
233 template<class T>
235 {
237 }
238 
239 template<class T>
241 {
242  if (data) {
243  destroy();
244  num_elmts = 0;
245  data = nullptr;
246  }
247 }
248 
249 template<class T>
251 {
252  if (this->data) {
253  // if no change in size, do not reallocate.
254  if (this->num_elmts == n)
255  return false;
256 
259  }
260  else {
261  // this happens if the vector is default constructed.
263  }
264  return true;
265 }
266 
267 #undef vnl_vector_alloc_blah
268 #undef vnl_vector_free_blah
269 
270 //------------------------------------------------------------
271 
272 //: Read a vnl_vector from an ascii std::istream.
273 // If the vector has nonzero size on input, read that many values.
274 // Otherwise, read to EOF.
275 template <class T>
276 bool vnl_vector<T>::read_ascii(std::istream& s)
277 {
278  bool size_known = (this->size() != 0);
279  if (size_known) {
280  for (size_t i = 0; i < this->size(); ++i) {
281  if ( ! (s >> (*this)(i)) ) {
282  return false;
283  }
284  }
285  return true;
286  }
287 
288  // Just read until EOF
289  std::vector<T> allvals;
290  size_t n = 0;
291  T value;
292  while ( s >> value ) {
293  allvals.push_back(value);
294  ++n;
295  }
296  this->set_size(n); //*this = vnl_vector<T>(n);
297  for (size_t i = 0; i < n; ++i)
298  (*this)[i] = allvals[i];
299  return true;
300 }
301 
302 template <class T>
304 {
305  vnl_vector<T> V;
306  V.read_ascii(s);
307  return V;
308 }
309 
310 //: Sets all elements of a vector to a specified fill value. O(n).
311 
312 template<class T>
314 vnl_vector<T>::fill (T const& value)
315 {
316  if (this->data) //VS2012 Runtime Library's std::fill_n has debug assertion when data is null
317  {
318  std::fill_n( this->data, this->num_elmts, value );
319  }
320  return *this;
321 }
322 
323 //: Sets elements of a vector to those in an array. O(n).
324 
325 template<class T>
328 {
329  std::copy( ptr, ptr + this->num_elmts, this->data );
330  return *this;
331 }
332 
333 //: Sets elements of an array to those in vector. O(n).
334 
335 template<class T>
336 void vnl_vector<T>::copy_out (T *ptr) const
337 {
338  std::copy( this->data, this->data + this->num_elmts, ptr );
339 }
340 
341 //: Copies rhs vector into lhs vector. O(n).
342 // Changes the dimension of lhs vector if necessary.
343 
344 template<class T>
346 {
347  if (this != &rhs) { // make sure *this != m
348  if (rhs.data) {
349  if (this->num_elmts != rhs.num_elmts)
350  this->set_size(rhs.size());
351  std::copy( rhs.data, rhs.data + this->num_elmts, this->data );
352  }
353  else {
354  // rhs is default-constructed.
355  clear();
356  }
357  }
358  return *this;
359 }
360 
361 //: Increments all elements of vector with value. O(n).
362 
363 template<class T>
365 {
366  for (size_t i = 0; i < this->num_elmts; i++)
367  this->data[i] += value;
368  return *this;
369 }
370 
371 //: Multiplies all elements of vector with value. O(n).
372 
373 template<class T>
375 {
376  for (size_t i = 0; i < this->num_elmts; i++)
377  this->data[i] *= value;
378  return *this;
379 }
380 
381 //: Divides all elements of vector by value. O(n).
382 
383 template<class T>
385 {
386  for (size_t i = 0; i < this->num_elmts; i++)
387  this->data[i] /= value;
388  return *this;
389 }
390 
391 
392 //: Mutates lhs vector with its addition with rhs vector. O(n).
393 
394 template<class T>
396 {
397 #ifndef NDEBUG
398  if (this->num_elmts != rhs.num_elmts)
399  vnl_error_vector_dimension ("operator+=", this->num_elmts, rhs.num_elmts);
400 #endif
401  for (size_t i = 0; i < this->num_elmts; i++)
402  this->data[i] += rhs.data[i];
403  return *this;
404 }
405 
406 
407 //: Mutates lhs vector with its subtraction with rhs vector. O(n).
408 
409 template<class T>
411 {
412 #ifndef NDEBUG
413  if (this->num_elmts != rhs.num_elmts)
414  vnl_error_vector_dimension ("operator-=", this->num_elmts, rhs.num_elmts);
415 #endif
416  for (size_t i = 0; i < this->num_elmts; i++)
417  this->data[i] -= rhs.data[i];
418  return *this;
419 }
420 
421 //: Pre-multiplies vector with matrix and stores result back in vector.
422 // v = m * v. O(m*n). Vector is assumed a column matrix.
423 
424 template<class T>
426 {
427 #ifndef NDEBUG
428  if (m.columns() != this->num_elmts) // dimensions do not match?
429  vnl_error_vector_dimension ("operator*=", this->num_elmts, m.columns());
430 #endif
431  T* temp= vnl_c_vector<T>::allocate_T(m.rows()); // Temporary
432  for (size_t i = 0; i < m.rows(); i++) { // For each index
433  temp[i] = (T)0; // Initialize element value
434  for (size_t k = 0; k < this->num_elmts; k++) // Loop over column values
435  temp[i] += (m.get(i,k) * this->data[k]); // Multiply
436  }
437  vnl_c_vector<T>::deallocate(this->data, this->num_elmts); // Free up the data space
438  num_elmts = m.rows(); // Set new num_elmts
439  this->data = temp; // Pointer to new storage
440  return *this; // Return vector reference
441 }
442 
443 //: Post-multiplies vector with matrix and stores result back in vector.
444 // v = v * m. O(m*n). Vector is assumed a row matrix.
445 
446 template<class T>
448 {
449 #ifndef NDEBUG
450  if (this->num_elmts != m.rows()) // dimensions do not match?
451  vnl_error_vector_dimension ("operator*=", this->num_elmts, m.rows());
452 #endif
453  T* temp= vnl_c_vector<T>::allocate_T(m.columns()); // Temporary
454  for (size_t i = 0; i < m.columns(); i++) { // For each index
455  temp[i] = (T)0; // Initialize element value
456  for (size_t k = 0; k < this->num_elmts; k++) // Loop over column values
457  temp[i] += (this->data[k] * m.get(k,i)); // Multiply
458  }
459  vnl_c_vector<T>::deallocate(this->data, num_elmts); // Free up the data space
460  num_elmts = m.columns(); // Set new num_elmts
461  this->data = temp; // Pointer to new storage
462  return *this; // Return vector reference
463 }
464 
465 
466 //: Creates new vector containing the negation of THIS vector. O(n).
467 
468 template<class T>
470 {
471  vnl_vector<T> result(this->num_elmts);
472  for (size_t i = 0; i < this->num_elmts; i++)
473  result.data[i] = - this->data[i]; // negate element
474  return result;
475 }
476 
477 //: Replaces elements with index beginning at start, by values of v. O(n).
478 
479 template<class T>
481 {
482  size_t stop = start + v.size();
483 #ifndef NDEBUG
484  if ( stop > this->num_elmts)
485  vnl_error_vector_dimension ("update", stop-start, v.size());
486 #endif
487  //std::copy_n( v.data, stop - start, this->data + start );
488  for (size_t i = start; i < stop; i++)
489  this->data[i] = v.data[i-start];
490  return *this;
491 }
492 
493 
494 //: Returns a subvector specified by the start index and length. O(n).
495 
496 template<class T>
497 vnl_vector<T> vnl_vector<T>::extract (size_t len, size_t start) const
498 {
499 #ifndef NDEBUG
500  size_t stop = start + len;
501  if (this->num_elmts < stop)
502  vnl_error_vector_dimension ("extract", stop-start, len);
503 #endif
504  vnl_vector<T> result(len);
505  //std::copy_n( this->data + start, len, result.data );
506  for (size_t i = 0; i < len; i++)
507  result.data[i] = data[start+i];
508  return result;
509 }
510 
511 //: Returns new vector whose elements are the products v1[i]*v2[i]. O(n).
512 
513 template<class T>
515 {
516 #ifndef NDEBUG
517  if (v1.size() != v2.size())
518  vnl_error_vector_dimension ("element_product", v1.size(), v2.size());
519 #endif
520 
521  vnl_vector<T> result(v1.size());
522 
523  vnl_sse<T>::element_product(v1.begin(), v2.begin(), result.begin(), v1.size());
524 
525  return result;
526 }
527 
528 //: Returns new vector whose elements are the quotients v1[i]/v2[i]. O(n).
529 
530 template<class T>
532 {
533 #ifndef NDEBUG
534  if (v1.size() != v2.size())
535  vnl_error_vector_dimension ("element_quotient", v1.size(), v2.size());
536 #endif
537  vnl_vector<T> result(v1.size());
538  for (size_t i = 0; i < v1.size(); i++)
539  result[i] = v1[i] / v2[i];
540  return result;
541 }
542 
543 //:
544 template <class T>
545 vnl_vector<T> vnl_vector<T>::apply(T (*f)(T const&)) const
546 {
547  vnl_vector<T> ret(size());
548  vnl_c_vector<T>::apply(this->data, num_elmts, f, ret.data);
549  return ret;
550 }
551 
552 //: Return the vector made by applying "f" to each element.
553 template <class T>
555 {
556  vnl_vector<T> ret(num_elmts);
557  vnl_c_vector<T>::apply(this->data, num_elmts, f, ret.data);
558  return ret;
559 }
560 
561 //: Returns the dot product of two nd-vectors, or [v1]*[v2]^T. O(n).
562 
563 template<class T>
564 T dot_product (vnl_vector<T> const& v1, vnl_vector<T> const& v2)
565 {
566 #ifndef NDEBUG
567  if (v1.size() != v2.size())
568  vnl_error_vector_dimension ("dot_product", v1.size(), v2.size());
569 #endif
571  v2.begin(),
572  v1.size());
573 }
574 
575 //: Hermitian inner product. O(n)
576 
577 template<class T>
578 T inner_product (vnl_vector<T> const& v1, vnl_vector<T> const& v2)
579 {
580 #ifndef NDEBUG
581  if (v1.size() != v2.size())
582  vnl_error_vector_dimension ("inner_product", v1.size(), v2.size());
583 #endif
585  v2.begin(),
586  v1.size());
587 }
588 
589 //: Returns the 'matrix element' <u|A|v> = u^t * A * v. O(mn).
590 
591 template<class T>
592 T bracket(vnl_vector<T> const &u, vnl_matrix<T> const &A, vnl_vector<T> const &v)
593 {
594 #ifndef NDEBUG
595  if (u.size() != A.rows())
596  vnl_error_vector_dimension("bracket",u.size(),A.rows());
597  if (A.columns() != v.size())
598  vnl_error_vector_dimension("bracket",A.columns(),v.size());
599 #endif
600  T brak(0);
601  for (size_t i=0; i<u.size(); ++i)
602  for (size_t j=0; j<v.size(); ++j)
603  brak += u[i]*A(i,j)*v[j];
604  return brak;
605 }
606 
607 //: Returns the nxn outer product of two nd-vectors, or [v1]^T*[v2]. O(n).
608 
609 template<class T>
611  vnl_vector<T> const& v2) {
612  vnl_matrix<T> out(v1.size(), v2.size());
613  for (size_t i = 0; i < out.rows(); i++) // v1.column() * v2.row()
614  for (size_t j = 0; j < out.columns(); j++)
615  out[i][j] = v1[i] * v2[j];
616  return out;
617 }
618 
619 
620 //--------------------------------------------------------------------------------
621 
622 template <class T>
625 {
626  for (size_t i=0;i<num_elmts/2;++i) {
627  T tmp=data[i];
628  data[i]=data[num_elmts-1-i];
629  data[num_elmts-1-i]=tmp;
630  }
631  return *this;
632 }
633 
634 template <class T>
636 vnl_vector<T>::flip(const size_t &b, const size_t &e)
637 {
638 
639 #if VNL_CONFIG_CHECK_BOUNDS && (!defined NDEBUG)
640  assert (!(b > this->num_elmts || e > this->num_elmts || b > e));
641 #endif
642 
643  for (size_t i=b;i<(e-b)/2+b;++i) {
644  T tmp=data[i];
645  const size_t endIndex = e - 1 - (i-b);
646  data[i]=data[endIndex];
647  data[endIndex]=tmp;
648  }
649  return *this;
650 
651 }
652 
653 template <class T>
655 vnl_vector<T>::roll(const int &shift) const
656 {
657  vnl_vector<T> v(this->num_elmts);
658  const size_t wrapped_shift = shift % this->num_elmts;
659  if (0 == wrapped_shift)
660  return v.copy_in(this->data_block());
661  for (size_t i = 0; i < this->num_elmts; ++i)
662  {
663  v[(i + wrapped_shift)%this->num_elmts] = this->data_block()[i];
664  }
665  return v;
666 }
667 
668 template <class T>
671 {
672  const size_t wrapped_shift = shift % this->num_elmts;
673  if (0 == wrapped_shift)
674  return *this;
675  return this->flip().flip(0,wrapped_shift).flip(wrapped_shift,this->num_elmts);
676 }
677 
678 template <class T>
680 {
681  std::swap(this->num_elmts, that.num_elmts);
682  std::swap(this->data, that.data);
683 }
684 
685 //--------------------------------------------------------------------------------
686 
687 // Disable warning caused when T is complex<float>. The static_cast
688 // to real_t constructs a complex<float> from a double.
689 #if defined(_MSC_VER)
690 # pragma warning (push)
691 # pragma warning (disable: 4244) /* conversion with loss of data */
692 #endif
693 
694 // fsm : cos_angle should return a T, or a double-precision extension
695 // of T. "double" is wrong since it won't work if T is complex.
696 template <class T>
697 T cos_angle(vnl_vector<T> const& a, vnl_vector<T> const& b)
698 {
699  typedef typename vnl_numeric_traits<T>::real_t real_t;
700  typedef typename vnl_numeric_traits<T>::abs_t abs_t;
701  typedef typename vnl_numeric_traits<abs_t>::real_t abs_r;
702 
703  real_t ab = inner_product(a,b);
704  real_t a_b = static_cast<real_t>(
705  std::sqrt( abs_r(a.squared_magnitude() * b.squared_magnitude()) ));
706  return T( ab / a_b);
707 }
708 
709 #if defined(_MSC_VER)
710 # pragma warning (pop)
711 #endif
712 
713 //: Returns smallest angle between two non-zero n-dimensional vectors. O(n).
714 
715 template<class T>
716 double angle (vnl_vector<T> const& a, vnl_vector<T> const& b)
717 {
718  typedef typename vnl_numeric_traits<T>::abs_t abs_t;
719  typedef typename vnl_numeric_traits<abs_t>::real_t abs_r;
720  const abs_r c = abs_r( cos_angle(a, b) );
721  // IMS: sometimes cos_angle returns 1+eps, which can mess up std::acos.
722  if (c >= 1.0) return 0;
723  if (c <= -1.0) return vnl_math::pi;
724  return std::acos( c );
725 }
726 
727 template <class T>
729 {
730  for (size_t i = 0; i < this->size();++i)
731  if (!vnl_math::isfinite( (*this)[i] ))
732  return false;
733 
734  return true;
735 }
736 
737 template <class T>
739 {
740  T const zero(0);
741  for (size_t i = 0; i < this->size();++i)
742  if ( !( (*this)[i] == zero) )
743  return false;
744 
745  return true;
746 }
747 
748 template <class T>
750 {
751  if (this->is_finite())
752  return;
753 
754  std::cerr << __FILE__ ": *** NAN FEVER **\n" << *this;
755  std::abort();
756 }
757 
758 template <class T>
760 {
761  if (this->size() != sz) {
762  std::cerr << __FILE__ ": Size is " << this->size() << ". Should be " << sz << '\n';
763  std::abort();
764  }
765 }
766 
767 template <class T>
768 bool vnl_vector<T>::is_equal(vnl_vector<T> const& rhs, double tol) const
769 {
770  if (this == &rhs) //Same object ? => equal.
771  return true;
772 
773  if (this->size() != rhs.size()) //Size different ?
774  return false;
775  for (size_t i = 0; i < size(); i++)
776  if (vnl_math::abs(this->data[i] - rhs.data[i]) > tol) //Element different ?
777  return false;
778 
779  return true;
780 }
781 
782 template<class T>
784 {
785  if (this == &rhs) // same object => equal.
786  return true;
787 
788  if (this->size() != rhs.size()) // Size different ?
789  return false; // Then not equal.
790  for (size_t i = 0; i < size(); i++) // For each index
791  if (!(this->data[i] == rhs.data[i])) // Element different ?
792  return false; // Then not equal.
793 
794  return true; // Else same; return true.
795 }
796 
797 //--------------------------------------------------------------------------------
798 
799 //: Overloads the output operator to print a vector. O(n).
800 
801 template<class T>
802 std::ostream& operator<< (std::ostream& s, vnl_vector<T> const& v)
803 {
804  for (size_t i = 0; i+1 < v.size(); ++i) // For each index in vector
805  s << v[i] << ' '; // Output data element
806  if (v.size() > 0) s << v[v.size()-1];
807  return s;
808 }
809 
810 //: Read a vnl_vector from an ascii std::istream.
811 // If the vector has nonzero size on input, read that many values.
812 // Otherwise, read to EOF.
813 template <class T>
814 std::istream& operator>>(std::istream& s, vnl_vector<T>& M)
815 {
816  M.read_ascii(s); return s;
817 }
818 
819 //--------------------------------------------------------------------------------
820 
821 // The instantiation macros are split because some functions (angle, cos_angle)
822 // shouldn't be instantiated for complex and/or integral types.
823 
824 #define VNL_VECTOR_INSTANTIATE_COMMON(T) \
825 template class VNL_EXPORT vnl_vector<T >; \
826 /* arithmetic, comparison etc */ \
827 /*template VNL_EXPORT vnl_vector<T > operator+(T const, vnl_vector<T > const &) ; */ \
828 /*template VNL_EXPORT vnl_vector<T > operator-(T const, vnl_vector<T > const &) ; */ \
829 /*template VNL_EXPORT vnl_vector<T > operator*(T const, vnl_vector<T > const &) ; */ \
830 template VNL_EXPORT vnl_vector<T > operator*(vnl_matrix<T > const &, vnl_vector<T > const &); \
831 /* element-wise */ \
832 template VNL_EXPORT vnl_vector<T > element_product(vnl_vector<T > const &, vnl_vector<T > const &); \
833 template VNL_EXPORT vnl_vector<T > element_quotient(vnl_vector<T > const &, vnl_vector<T > const &); \
834 /* dot products, angles etc */ \
835 template VNL_EXPORT T inner_product(vnl_vector<T > const &, vnl_vector<T > const &); \
836 template VNL_EXPORT T dot_product(vnl_vector<T > const &, vnl_vector<T > const &); \
837 template VNL_EXPORT T bracket(vnl_vector<T > const &, vnl_matrix<T > const &, vnl_vector<T > const &); \
838 template VNL_EXPORT vnl_matrix<T > outer_product(vnl_vector<T > const &,vnl_vector<T > const &); \
839 /* I/O */ \
840 template VNL_EXPORT std::ostream & operator<<(std::ostream &, vnl_vector<T > const &); \
841 template VNL_EXPORT std::istream & operator>>(std::istream &, vnl_vector<T > &)
842 
843 #define VNL_VECTOR_INSTANTIATE(T) \
844 VNL_VECTOR_INSTANTIATE_COMMON(T); \
845 template VNL_EXPORT T cos_angle(vnl_vector<T > const & , vnl_vector<T > const &); \
846 template VNL_EXPORT double angle(vnl_vector<T > const & , vnl_vector<T > const &)
847 
848 #define VNL_VECTOR_INSTANTIATE_COMPLEX(T) \
849 VNL_VECTOR_INSTANTIATE_COMMON(T); \
850 template VNL_EXPORT T cos_angle(vnl_vector<T > const & , vnl_vector<T > const &)
851 
852 #endif // vnl_vector_hxx_
static void deallocate(T **, const std::size_t n_when_allocated)
vnl_c_vector< T >::abs_t abs_t
Type def for norms.
Definition: vnl_matrix.h:499
vnl_vector_fixed< T, n > element_quotient(const vnl_vector_fixed< T, n > &a, const vnl_vector_fixed< T, n > &b)
unsigned int cols() const
Return the number of columns.
Definition: vnl_matrix.h:183
vnl_vector< T > & operator=(T const &v)
Set all elements to value v.
Definition: vnl_vector.h:174
vnl_vector< T > & operator-=(T value)
Subtract scalar value from all elements.
Definition: vnl_vector.h:183
void swap(double &a, double &b)
vnl_vector< T > & update(vnl_vector< T > const &, size_t start=0)
Replaces elements with index beginning at start, by values of v. O(n).
Definition: vnl_vector.hxx:480
An ordinary mathematical matrix.
vnl_vector< T > roll(const int &shift) const
Roll the vector forward by the specified shift.
Definition: vnl_vector.hxx:655
bool set_size(size_t n)
Resize to n elements.
Definition: vnl_vector.hxx:250
Templated zero/one/precision.
#define vnl_vector_construct_hack()
Definition: vnl_vector.hxx:58
Support for Streaming SIMD Extensions to speed up vector arithmetic.
vnl_vector< T > operator-() const
Unary minus operator.
Definition: vnl_vector.hxx:469
#define m
Definition: vnl_vector.h:43
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
vnl_vector< T > & pre_multiply(vnl_matrix< T > const &M)
this = M(*this) where M is a suitable matrix.
Definition: vnl_vector.hxx:425
vnl_vector< T > & operator *=(T)
Multiply all elements by scalar.
Definition: vnl_vector.hxx:374
vnl_vector< T > & operator+=(T)
Add scalar value to all elements.
Definition: vnl_vector.hxx:364
void destroy()
Frees up the array inside vector. O(1).
Definition: vnl_vector.hxx:234
Namespace with standard math functions.
iterator begin()
Iterator pointing to start of data.
Definition: vnl_matrix.h:620
bool read_ascii(std::istream &s)
Read from text stream.
Definition: vnl_vector.hxx:276
bool operator_eq(vnl_vector< T > const &v) const
Return true if *this == v.
Definition: vnl_vector.hxx:783
abs_t squared_magnitude() const
Return sum of squares of elements.
Definition: vnl_vector.h:279
~vnl_vector()
Destructor.
Definition: vnl_vector.hxx:226
std::ostream & operator<<(std::ostream &s, vnl_decnum const &r)
decimal output.
Definition: vnl_decnum.h:393
static void apply(T const *, unsigned, T(*f)(T), T *v_out)
vnl_vector< T > & flip()
Reverse the order of the elements.
Definition: vnl_vector.hxx:624
bool is_equal(vnl_vector< T > const &rhs, double tol) const
Return true if all elements of vectors are equal, within given tolerance.
Definition: vnl_vector.hxx:768
#define v
Definition: vnl_vector.h:42
void vnl_error_vector_dimension(char const *fcn, int l1, int l2)
Raise exception for invalid dimension.
Definition: vnl_error.cxx:30
vnl_vector()
Creates an empty vector. O(1).
Definition: vnl_vector.h:80
#define vnl_vector_alloc_blah(size)
Definition: vnl_vector.hxx:62
#define vnl_vector_free_blah
Definition: vnl_vector.hxx:69
vnl_vector< T > & operator/=(T)
Divide all elements by scalar.
Definition: vnl_vector.hxx:384
T bracket(vnl_vector< T > const &u, vnl_matrix< T > const &A, vnl_vector< T > const &v)
Returns the 'matrix element' <u|A|v> = u^t * A * v. O(mn).
Definition: vnl_vector.hxx:592
T cos_angle(vnl_vector< T > const &a, vnl_vector< T > const &b)
Definition: vnl_vector.hxx:697
iterator begin()
Iterator pointing to start of data.
Definition: vnl_vector.h:243
bool is_finite() const
Return true if it's finite.
Definition: vnl_vector.hxx:728
vnl_vector< T > extract(size_t len, size_t start=0) const
Returns a subvector specified by the start index and length. O(n).
Definition: vnl_vector.hxx:497
static T * allocate_T(const std::size_t n)
An ordinary mathematical matrix.
Definition: vnl_adjugate.h:22
static VNL_SSE_FORCE_INLINE void element_product(const T *x, const T *y, T *r, unsigned n)
Definition: vnl_sse.h:160
void clear()
Make the vector as if it had been default-constructed.
Definition: vnl_vector.hxx:240
void clear()
Make the matrix as if it had been default-constructed.
Definition: vnl_matrix.hxx:375
T inner_product(vnl_vector< T > const &v1, vnl_vector< T > const &v2)
Hermitian inner product. O(n).
Definition: vnl_vector.hxx:578
void swap(vnl_vector< T > &that)
Set this to that and that to this.
Definition: vnl_vector.hxx:679
static T inner_product(T const *, T const *, unsigned)
conjugate second.
vnl_vector< T > & post_multiply(vnl_matrix< T > const &M)
*this = (*this)*M where M is a suitable matrix.
Definition: vnl_vector.hxx:447
vnl_vector & fill(T const &v)
Set all values to v.
Definition: vnl_vector.hxx:314
bool isfinite(vnl_bignum const &x)
Definition: vnl_bignum.h:436
T dot_product(const vnl_vector_fixed< T, n > &a, const vnl_vector_fixed< T, n > &b)
size_t num_elmts
Definition: vnl_vector.h:397
vnl_vector & copy_in(T const *ptr)
Sets elements to ptr[i].
Definition: vnl_vector.hxx:327
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)
T const * data_block() const
Access the contiguous block storing the elements in the matrix row-wise. O(1).
Definition: vnl_matrix.h:601
static VNL_SSE_FORCE_INLINE void matrix_x_vector(const T *m, const T *v, T *r, unsigned rows, unsigned cols)
Definition: vnl_sse.h:199
static vnl_vector< T > read(std::istream &s)
Read from text stream.
Definition: vnl_vector.hxx:303
vnl_bignum abs(vnl_bignum const &x)
Definition: vnl_bignum.h:432
unsigned int size() const
Return the total number of elements stored by the matrix.
Definition: vnl_matrix.h:176
void destroy()
Delete data.
Definition: vnl_matrix.hxx:369
unsigned int rows() const
Return the number of rows.
Definition: vnl_matrix.h:179
T angle(const vnl_vector_fixed< T, n > &a, const vnl_vector_fixed< T, n > &b)
VNL_EXPORT std::istream & operator>>(std::istream &s, vnl_decnum &r)
decimal input.
Definition: vnl_decnum.cxx:411
vnl_vector< T > apply(T(*f)(T)) const
Applies function to elements.
Definition: vnl_vector.hxx:554
void assert_finite_internal() const
Definition: vnl_vector.hxx:749
bool is_finite() const
Return true if finite.
void copy_out(T *) const
Copy elements to ptr[i].
Definition: vnl_vector.hxx:336
bool set_size(unsigned r, unsigned c)
Resize to r rows by c columns. Old data lost.
Definition: vnl_matrix.hxx:390
static T dot_product(T const *, T const *, unsigned)
vnl_vector & roll_inplace(const int &shift)
Roll the vector forward by the specified shift.
Definition: vnl_vector.hxx:670
unsigned int columns() const
Return the number of columns.
Definition: vnl_matrix.h:187
static VNL_SSE_FORCE_INLINE void vector_x_matrix(const T *v, const T *m, T *r, unsigned rows, unsigned cols)
Definition: vnl_sse.h:189
Math on blocks of memory.
void assert_size_internal(size_t sz) const
Definition: vnl_vector.hxx:759
bool is_zero() const
Return true iff all the entries are zero.
Definition: vnl_vector.hxx:738