KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
m_vector.h
Go to the documentation of this file.
1 /* ======================================================================== *
2  | |
3  | Copyright (c) 2007-2010, by members of University of Arizona Computer |
4  | Vision group (the authors) including |
5  | Kobus Barnard, Kyle Simek, Andrew Predoehl. |
6  | |
7  | For use outside the University of Arizona Computer Vision group please |
8  | contact Kobus Barnard. |
9  | |
10  * ======================================================================== */
11 
12 /* $Id: m_vector.h 18301 2014-11-26 19:17:13Z ksimek $ */
13 
14 #ifndef VECTOR_WRAP_H
15 #define VECTOR_WRAP_H
16 
39 #include "m/m_vector.h"
40 #include "m/m_vec_norm.h"
41 #include "m/m_vec_metric.h"
42 #include "m_cpp/m_serialization.h"
43 #include "l_cpp/l_exception.h"
44 
45 #include <algorithm>
46 #include <iterator>
47 #include <vector>
48 
49 #ifdef KJB_HAVE_BST_SERIAL
50 #include <boost/serialization/access.hpp>
51 #endif
52 
53 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
54 
55 namespace kjb {
56 
57 class Matrix;
58 class Vector;
59 class Int_vector;
60 
61 class Index_range;
62 
63 template <class V>
65 
68 
69 template <class T>
71 
87 class Vector
88 {
89 #ifdef KJB_HAVE_BST_SERIAL
90  friend class boost::serialization::access;
91 #endif
92 
93 public:
94 
95 
96  /* Class users: to make your code as maintanable as possible, use the
97  * *_type typedefs below instead of directly referencing the specific
98  * type.
99  */
100 
101  typedef double Value_type;
102  typedef kjb_c::Vector Impl_type;
103  typedef Matrix Mat_type;
105  typedef Vector Vec_type;
107 
108  // Below are typdefs required to adhere to the stl Container concept
109  typedef double value_type;
110  typedef double* pointer;
111  typedef const double* const_pointer;
112  typedef double& reference;
113  typedef double const_reference;
114 // typedef __gnu_cxx::__normal_iterator<value_type*, Vector> iterator;
115 // typedef __gnu_cxx::__normal_iterator<const value_type*, Vector>
116 // const_iterator;
118  typedef const value_type* const_iterator;
119  typedef std::reverse_iterator<iterator> reverse_iterator;
120  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
121  typedef int size_type;
122  typedef int difference_type;
123 
124 
125 private:
126  Impl_type* m_vector;
127 
128  static const char* const BAD_SEGMENT;
129 
135  void throw_bad_bounds( int ) const;
136 
143  void m_ensure_capacity(size_type c);
144 
151  template<typename InputIterator_>
152  void m_initialize_dispatch(InputIterator_ begin_,
153  InputIterator_ end_, std::input_iterator_tag);
154 
161  template<typename ForwardIterator_>
162  void m_initialize_dispatch(ForwardIterator_ begin_, ForwardIterator_ end_, std::forward_iterator_tag)
163  {
164  const size_type n = std::distance(begin_, end_);
165  m_ensure_capacity(n);
166 
167  m_vector->length = n;
168 
169  std::copy(begin_, end_, this->begin());
170  }
171 public:
172 
173  /* -------------------------------------------------------------------
174  * CONSTRUCTORS
175  * ------------------------------------------------------------------- */
176 
182  explicit Vector(int length = 0)
183  : m_vector(0)
184  {
185  // Test program was HERE.
186  ETX( kjb_c::get_target_vector(&m_vector, length) );
187  }
188 
189  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
190 
198  explicit Vector(unsigned length)
199  : m_vector(0)
200  {
201  // Test program was HERE.
202  ETX( kjb_c::get_target_vector(&m_vector, static_cast<int>(length)) );
203  }
204 
205  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
206 
214  explicit Vector(unsigned long length)
215  : m_vector(0)
216  {
217  // Test program was HERE.
218  ETX( kjb_c::get_target_vector(&m_vector, static_cast<int>(length)) );
219  }
220 
221  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
222 
227  : m_vector(0)
228  {
229  // Test program was HERE.
230  ETX( kjb_c::get_initialized_vector(&m_vector, length, num) );
231  }
232 
233  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
234 
238  Vector(int length, const Value_type* data)
239  : m_vector(0)
240  {
241  // Test program was HERE.
242  ETX( kjb_c::get_target_vector(&m_vector, length) );
243  std::copy( data, data + length, m_vector -> elements );
244  }
245 
246  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
247 
251  Vector( const Int_vector& );
252 
253  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
254 
258  Vector(int length, const float* data)
259  : m_vector(0)
260  {
261  // Test program was HERE.
262  ETX( kjb_c::get_target_vector(&m_vector, length) );
263  std::copy( data, data + length, m_vector -> elements );
264  }
265 
266  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
267 
286  explicit Vector(Impl_type* vec_ptr)
287  : m_vector( vec_ptr )
288  {
289  if ( 0 == vec_ptr )
290  {
291  // Test program was HERE.
292  ETX( kjb_c::get_target_vector(&m_vector, 0) );
293  }
294  //else
295  //{
296  // // Test program was HERE.
297  //}
298  }
299 
300  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
301 
306  Vector(const std::vector<double>& src);
307 
308  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
309 
313  explicit Vector(Value_type val)
314  : m_vector(0)
315  {
316  // Test program was HERE.
317  ETX( kjb_c::get_initialized_vector(&m_vector, 1, val) );
318  }
319 
320  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
321 
325  Vector (const Vector_view& view);
326 
327  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
328 
332  Vector (const Const_vector_view& view);
333 
334  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
335 
340  : m_vector(0)
341  {
342  // Test program was HERE.
343  ETX( kjb_c::get_target_vector(&m_vector, 2) );
344  m_vector->elements[0] = val1;
345  m_vector->elements[1] = val2;
346  }
347 
348  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
349 
354  : m_vector(0)
355  {
356  // Test program was HERE.
357  ETX( kjb_c::get_target_vector(&m_vector, 3) );
358  m_vector->elements[0] = val1;
359  m_vector->elements[1] = val2;
360  m_vector->elements[2] = val3;
361  }
362 
363  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
364 
369  : m_vector(0)
370  {
371  // Test program was HERE.
372  ETX( kjb_c::get_target_vector(&m_vector, 4) );
373  m_vector->elements[0] = val1;
374  m_vector->elements[1] = val2;
375  m_vector->elements[2] = val3;
376  m_vector->elements[3] = val4;
377  }
378 
379  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
380 
390  explicit Vector(const Impl_type& vec_ref)
391  : m_vector(0)
392  {
393  // Test program was HERE.
394  ETX( kjb_c::copy_vector(&m_vector, &vec_ref) );
395  }
396 
397  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
398 
403  explicit Vector(const Mat_type& src);
404 
405  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
406 
416  explicit Vector(const Mat_view_type& src);
417 
418  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
419 
423  Vector(const std::string& file_name);
424 
425  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
426 
433  Vector( const Vector& vec_ref )
434  : m_vector(0)
435  {
436  // Test program was HERE.
437  ETX( kjb_c::copy_vector(&m_vector, vec_ref.m_vector) );
438  }
439 
440  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
441 
442 #ifdef KJB_HAVE_CXX11
443 
446  Vector(Vector&& vec_ref)
447  : m_vector( nullptr )
448  {
449  m_vector = vec_ref.m_vector;
450  vec_ref.m_vector = 0;
451  }
452 #endif /* KJB_HAVE_CXX11 */
453 
454  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
455 
456  /* ---------------------------------------------------------------------
457  * "STL CONSTRUCTORS"
458  *
459  * These constructors are required for compliance with STL's
460  * "Sequence" concept
461  *
462  * ------------------------------------------------------------------- */
463 
472  template<typename InputIterator_>
473  Vector(InputIterator_ begin_, InputIterator_ end_)
474  : m_vector(0)
475  {
476  ETX( kjb_c::get_target_vector(&m_vector, 8) );
477  m_vector->length = 0;
478 
479  typedef typename std::iterator_traits<InputIterator_>::
480  iterator_category IterCategory_;
481  m_initialize_dispatch(begin_, end_, IterCategory_());
482  }
483 
484 
485  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
486 
491  {
492  // Test program was HERE.
493  kjb_c::free_vector(m_vector);
494  }
495 
496  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
497 
501  int get_length() const
502  {
503  // Test program was HERE.
504  return m_vector -> length;
505  }
506 
510  size_type size() const
511  {
512  return get_length();
513  }
514 
519  {
520  return INT_MAX;
521  }
522 
526  bool empty() const
527  {
528  return size() == 0;
529  }
530 
531  // iterators
538  {
539  return iterator(m_vector->elements);
540  }
541 
548  {
549  return const_iterator(m_vector->elements);
550  }
551 
558  {
559  return iterator(m_vector->elements + m_vector->length);
560  }
561 
568  {
569  return const_iterator(m_vector->elements + m_vector->length);
570  }
571 
578  {
579  return reverse_iterator(end());
580  }
581 
588  {
589  return const_reverse_iterator(end());
590  }
591 
598  {
599  return reverse_iterator(begin());
600  }
601 
608  {
609  return const_reverse_iterator(begin());
610  }
611 
612  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
613 
618  void reserve(int capacity)
619  {
620  m_ensure_capacity(capacity);
621  }
622  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
623 
632  {
633  // Test program was HERE.
634  ETX( kjb_c::get_random_vector(&m_vector, length) );
635  return *this;
636  }
637 
638  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
639 
648  {
649  // Test program was HERE.
650  return randomize( get_length() );
651  }
652 
653  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
654 
656  //* @brief Clone of randomize(int)
657  //* @deprecated The name is misleading -- do not use in new code.
658  //*/
659  //Vector& init_random( int length )
660  //{
662  //return randomize( length );
663  //}
664 
665  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
666 
672  {
673  // Test program was HERE.
674  ETX( kjb_c::get_zero_vector(&m_vector, length) );
675  return *this;
676  }
677 
678  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
679 
685  {
686  // Test program was HERE.
687  return zero_out( get_length() );
688  }
689 
690  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
691 
693  //* @brief Clone of zero_out(int)
694  //* @deprecated The name is misleading -- do not use in new code.
695  //*/
696  //Vector& init_zero( int length )
697  //{
699  //return zero_out( length );
700  //}
701 
702  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
703 
708  {
709  get_target_vector(&m_vector, 1);
710  this->operator[](0) = val;
711 
712  return *this;
713  }
714 
719  {
720  get_target_vector(&m_vector, 2);
721  this->operator[](0) = val1;
722  this->operator[](1) = val2;
723 
724  return *this;
725  }
726 
731  {
732  get_target_vector(&m_vector, 3);
733  this->operator[](0) = val1;
734  this->operator[](1) = val2;
735  this->operator[](2) = val3;
736 
737  return *this;
738  }
739 
744  {
745  get_target_vector(&m_vector, 4);
746  this->operator[](0) = val1;
747  this->operator[](1) = val2;
748  this->operator[](2) = val3;
749  this->operator[](3) = val4;
750 
751  return *this;
752  }
753 
754  /* -------------------------------------------------------------------
755  * ASSIGNMENT OPERATORS
756  * ------------------------------------------------------------------- */
757 
761  Vector& operator=(const Mat_view_type& vec_ref);
762 
767  Vector& operator=(const Impl_type& vec_ref)
768  {
769  if ( m_vector != &vec_ref )
770  {
771  // Test program was HERE.
772  ETX( kjb_c::copy_vector(&m_vector, &vec_ref) );
773  }
774  //else
775  //{
776  // // Test program was HERE.
777  //}
778  return *this;
779  }
780 
781  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
782 
783 #ifdef KJB_HAVE_CXX11
784 
787  Vector& operator=(Vector&& other)
788  {
789  if(this == &other)
790  return *this;
791 
792  kjb_c::free_vector( m_vector );
793  m_vector = other.m_vector;
794  other.m_vector = 0;
795  return *this;
796  }
797 #endif /* KJB_HAVE_CXX11 */
798 
799  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
800 
805  Vector& operator=(const Vector& src)
806  {
807  // Test program was HERE.
808  // call assignment operator for kjb_c::Vector
809  return operator=( *src.m_vector );
810  }
811 
812 
813  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
814 
836  Vector& replace(const Vector& iv, int offset, int begin, int length);
837 
838  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
839 
844  const Impl_type* get_c_vector() const
845  {
846  // Test program was HERE.
847  return m_vector;
848  }
849 
850  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
851 
858  {
859  // Test program was HERE.
860  return m_vector;
861  }
862 
863  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
864 
874  {
875  // Test program was HERE.
876  return m_vector;
877  }
878 
879  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
880 
884  void swap( Vector& other )
885  {
886  // Test program was HERE.
887  std::swap( m_vector, other.m_vector );
888  }
889 
890  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
891 
895  Vector& resize(int new_length, Value_type pad = Value_type(0) );
896 
897  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
898 
906  {
907  // Test program was HERE.
908  return m_vector -> elements[ i ];
909  }
910 
911  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
912 
919  const Value_type& operator[](int i) const
920  {
921  // Test program was HERE.
922  return m_vector -> elements[ i ];
923  }
924 
925  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
926 
933  {
934  // Test program was HERE.
935  return operator[]( i );
936  }
937 
938  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
939 
945  const Value_type& operator()(int i) const
946  {
947  // Test program was HERE.
948  return operator[]( i );
949  }
950 
951  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
952 
965  Vector_view operator[](const Index_range& idx);
966 
967  Const_vector_view operator[](const Index_range& idx) const;
968 
969  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
970 
975  void check_bounds( int i ) const
976  {
977  // Test program was HERE.
978  if ( i < 0 || get_length() <= i )
979  {
980  throw_bad_bounds( i );
981  }
982  }
983 
984  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
985 
991  Value_type& at( int i )
992  {
993  // Test program was HERE.
994  check_bounds( i );
995  return operator[]( i );
996  }
997 
998  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
999 
1005  const Value_type& at( int i ) const
1006  {
1007  // Test program was HERE.
1008  check_bounds( i );
1009  return operator[]( i );
1010  }
1011 
1012  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1013 
1017  iterator insert(iterator position, value_type t);
1018 
1019  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1020 
1024  void insert(iterator position, size_type N, value_type t);
1025 
1026  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1027 
1031  template<typename InputIterator>
1032  void insert(iterator position, InputIterator begin_, InputIterator end_);
1033 
1034  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1035 
1039  void assign(size_type N, value_type t);
1040 
1041  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1042 
1046  template<typename InputIterator>
1047  void assign(InputIterator begin_, InputIterator end_);
1048 
1049  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1050 
1056  iterator erase(iterator position);
1057 
1058  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1059 
1066  void erase(iterator begin_, iterator end_);
1067 
1068  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1069 
1075  void clear()
1076  {
1077  m_vector->length = 0;
1078  }
1079 
1080 
1081  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1087  {
1088  return operator[](0);
1089  }
1090 
1091  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1092 
1098  {
1099  return operator[](0);
1100  }
1101  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1102 
1108  {
1109  return operator[](size() - 1);
1110  }
1111 
1112  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1113 
1119  {
1120  return operator[](size() - 1);
1121  }
1122 
1123  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1124 
1129  {
1130  m_ensure_capacity(m_vector->length + 1);
1131 
1132  m_vector->length++;
1133  assert(m_vector->length <= m_vector->max_length);
1134  m_vector->elements[ (m_vector->length - 1) ] = x;
1135  }
1136 
1142  void pop_back()
1143  {
1144  m_vector->length--;
1145  }
1146 
1155  void write_row( const char* filename = 0 ) const
1156  {
1157  // Test program was HERE.
1158  ETX(kjb_c::write_row_vector_full_precision( m_vector, filename ));
1159  }
1160 
1161  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1162 
1170  void write_col( const char* filename = 0 ) const
1171  {
1172  // Test program was HERE.
1173  ETX(kjb_c::write_col_vector_with_header( m_vector, filename ));
1174  }
1175 
1176  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1183  void read( const char* filename = 0 )
1184  {
1185  // Test program was HERE.
1186  ETX(kjb_c::read_vector( &m_vector, filename ));
1187  }
1188 
1189  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1198  void write( const char* filename = 0 ) const
1199  {
1200  // Test program was HERE.
1201  write_col(filename);
1202  }
1203 
1204  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1205 
1206  /* ---------------------------------------------------------------------
1207  * ARITHMETIC OPERATORS
1208  *
1209  * Modifying operators; i.e., operators that modify the object. These
1210  * are member functions, as per the standard implementation. Operators
1211  * that do not modify the vector are non-members. Also, their corresponding
1212  * named methods (subtract, add, etc).
1213  * ------------------------------------------------------------------- */
1214 
1219  {
1220  // Test program was HERE.
1221  ETX( kjb_c::ow_multiply_vector_by_scalar(m_vector, op2) );
1222  return *this;
1223  }
1224 
1225  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1226 
1231  {
1232  // Test program was HERE.
1233  return operator*=( op2 );
1234  }
1235 
1236  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1237 
1241  Vector& ew_multiply(const Vector & op2)
1242  {
1243  ETX( kjb_c::ow_multiply_vectors(m_vector, op2.m_vector) );
1244  return *this;
1245  }
1246 
1247 
1248  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1249 
1254  {
1255  // Test program was HERE.
1256  ETX( kjb_c::ow_divide_vector_by_scalar(m_vector, op2) );
1257  return *this;
1258  }
1259 
1260  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1261 
1266  {
1267  // Test program was HERE.
1268  return operator/=( op2 );
1269  }
1270 
1271  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1272 
1276  Vector& ew_divide(const Vector & op2)
1277  {
1278  ETX( kjb_c::ow_divide_vectors(m_vector, op2.m_vector) );
1279  return *this;
1280  }
1281 
1282  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1283 
1288  {
1289  // Test program was HERE.
1290  ETX( kjb_c::ow_add_vectors(m_vector, op2.m_vector) );
1291  return *this;
1292  }
1293 
1294  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1295 
1299  Vector& operator+= (double x);;
1300 
1301  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1302 
1306  Vector& add (const Vector& op2)
1307  {
1308  // Test program was HERE.
1309  return operator+=( op2 );
1310  }
1311 
1312  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1313 
1318  {
1319  // Test program was HERE.
1320  ETX( kjb_c::ow_subtract_vectors(m_vector, op2.m_vector) );
1321  return *this;
1322  }
1323 
1324  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1325 
1329  Vector& operator-= (double x);
1330 
1331  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1332 
1336  Vector& subtract (const Vector& op2)
1337  {
1338  // Test program was HERE.
1339  return operator-=( op2 );
1340  }
1341 
1342  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1343 
1348  {
1349  // Test program was HERE.
1350  return operator*=( -1 );
1351  }
1352 
1353  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1354 
1361  Vec_type& mapcar( Mapper );
1362 
1363  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1364 
1384  Value_type min( int* min_index ) const
1385  {
1386  if ( 0 == get_length() )
1387  {
1388  // Test program was HERE.
1390  "Vector is empty; min is undefined" );
1391  }
1392  if ( 0 == min_index )
1393  {
1394  // Test program was HERE.
1395  //return kjb::min(*this);
1396  return kjb_c::min_vector_element(m_vector);
1397  }
1398  // Test program was HERE.
1399  Value_type min_val;
1400  *min_index = kjb_c::get_min_vector_element(m_vector, &min_val);
1401  return min_val;
1402  }
1403 
1404  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1405 
1425  Value_type max( int* max_index ) const
1426  {
1427  if ( 0 == get_length() )
1428  {
1429  // Test program was HERE.
1431  "Vector is empty; max is undefined" );
1432  }
1433  if ( 0 == max_index )
1434  {
1435  // Test program was HERE.
1436  //return kjb::max(*this);
1437  return kjb_c::max_vector_element(m_vector);
1438  }
1439  // Test program was HERE.
1440  Value_type max_val;
1441  *max_index = kjb_c::get_max_vector_element(m_vector, &max_val);
1442  return max_val;
1443  }
1444 
1445  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1446 
1453  double get_max_abs_difference(const Vector & op2) const;
1454 
1455  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1456 
1457 
1458 
1459  /* -------------------------------------------------------------------
1460  * VECTOR-SPECIFIC METHODS
1461  * ------------------------------------------------------------------- */
1462 
1474  kjb::Matrix hat() const;
1475 
1476  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1477 
1483  Vector& cross_with(const Vector& op2);
1484 
1485  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1486 
1491  {
1492  // Test program was HERE.
1493  return kjb_c::vector_magnitude(m_vector);
1494  }
1495 
1496  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1497 
1502  {
1503  // Test program was HERE.
1504  return kjb_c::sum_vector_squared_elements(m_vector);
1505  }
1506 
1507  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1508 
1513  {
1514  // Test program was HERE.
1515  return kjb_c::sum_vector_elements(m_vector);
1516  }
1517 
1518  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1519 
1524  Vector& normalize(kjb_c::Norm_method method =kjb_c::NORMALIZE_BY_MAGNITUDE)
1525  {
1526  // Test program was HERE.
1527  kjb_c::normalize_vector(&m_vector, m_vector, method);
1528  return *this;
1529  }
1530 
1534  Vector normalized(kjb_c::Norm_method method =kjb_c::NORMALIZE_BY_MAGNITUDE) const
1535  {
1536  Vector tmp(*this);
1537  tmp.normalize(method);
1538  return tmp;
1539  }
1540 
1541 private:
1542  template <class View_type>
1543  void init_from_view_(const View_type& vec_view);
1544 
1545  template<class Archive>
1546  void serialize(Archive &ar, const unsigned int version)
1547  {
1548  return kjb_serialize(ar, *this, version);
1549  }
1550 
1551 };
1552 
1553 
1554 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1555 
1556 /* ---------------------------------------------------------------------
1557  * TEMPLATE MEMBER FUNCTIONS
1558  *
1559  * Template member function definitions go here.
1560  * ------------------------------------------------------------------- */
1561 
1562 template<typename InputIterator_>
1563 void Vector::m_initialize_dispatch
1564 (
1565  InputIterator_ begin_,
1566  InputIterator_ end_,
1567  std::input_iterator_tag
1568 )
1569 {
1570  for(; begin_ != end_; ++begin_)
1571  {
1572  push_back(*begin_);
1573  }
1574 }
1575 
1576 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1577 
1578 template<typename InputIterator>
1579 void Vector::insert
1581  iterator position,
1582  InputIterator begin_,
1583  InputIterator end_
1584 )
1585 {
1586  const size_type N = end_ - begin_;
1587  const size_type offset = position - begin();
1588  assert(offset <= size());
1589 
1590  m_ensure_capacity(m_vector->length + N);
1591  position = begin() + offset;
1592 
1593  iterator old_end = this->end();
1594 
1595  m_vector->length += N;
1596 
1597  // corner case: empty vector. in this case, position=0 is a valid
1598  // iterator, but needs to be handled specially.
1599  if(position == 0 && m_vector->length == N)
1600  {
1601  position = m_vector->elements;
1602  }
1603 
1604  // shift old data
1605  std::copy(position, old_end, position + N);
1606 
1607  // copy new data
1608  std::copy(begin_, end_, position);
1609 }
1610 
1611 template<typename InputIterator>
1612 void Vector::assign(InputIterator begin_, InputIterator end_)
1613 {
1614  resize(0);
1615  insert(begin(), begin_, end_);
1616 }
1617 
1618 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1619 
1620 template <class View_type>
1621 void Vector::init_from_view_(const View_type& vec_view)
1622 {
1623  Vector result(vec_view.size());
1624  for( int i = 0; i < vec_view.size(); ++i)
1625  {
1626  result[i] = vec_view[i];
1627  }
1628  swap( result );
1629 }
1630 
1631 /* ---------------------------------------------------------------------
1632  * "NAMED CONSTRUCTORS"
1633  *
1634  * The "named constructor idiom" is when we use functions to create
1635  * objects. The benefit is that the names indicate the purpose of the
1636  * method. The following functions act as named constructors.
1637  * ------------------------------------------------------------------- */
1638 
1642 inline
1644 {
1645  // Test program was HERE.
1646  kjb_c::Vector* result = 0;
1647  ETX( kjb_c::get_random_vector( &result, length ) );
1648  return Vector(result);
1649 }
1650 
1651 
1652 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1653 
1658 
1659 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1660 
1665 inline
1667 {
1668  // Test program was HERE.
1669  return Vector(length, 0.0);
1670 }
1671 
1672 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1673 
1681 inline
1683 {
1684  // Test program was HERE.
1685  kjb_c::Vector* outvec = 0;
1686  ETX_2(kjb_c::copy_vector_segment(&outvec, iv.get_c_vector(), begin, length),
1687  "Failure in create_vector_from_vector_section : allocation error or bad indices");
1688  return Vector(outvec);
1689 }
1690 
1691 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1692 
1693 /* ---------------------------------------------------------------------
1694  * ARITHMETIC OPERATORS
1695  *
1696  * Non-modifying operators. These were members before, but I think most of us
1697  * agree that they should be non-members. The only members are the
1698  * modifying operators, such as +=, *=, /=, etc.
1699  * ------------------------------------------------------------------- */
1700 
1711 Vector operator*(const Vector& op1, const Matrix& op2);
1712 
1713 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1714 
1721 Vector operator*( const Matrix& op1, const Vector& op2 );
1722 
1723 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1724 
1728 inline
1730 {
1731  // Test program was HERE.
1732  return Vector(op1) *= op2;
1733 }
1734 
1735 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1736 
1743 inline
1745 {
1746  // Test program was HERE.
1747  return op2 * op1;
1748 }
1749 
1750 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1751 
1755 inline
1757 {
1758  // Test program was HERE.
1759  return Vector(op1) /= op2;
1760 
1761 }
1762 
1763 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1764 
1768 inline
1769 Vector operator+ (const Vector& op1, const Vector& op2)
1770 {
1771  // Test program was HERE.
1772  return Vector(op1) += op2;
1773 }
1774 
1775 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1776 
1781 inline
1782 Vector operator- (const Vector& op1, const Vector& op2)
1783 {
1784  // Test program was HERE.
1785  return Vector(op1) -= op2;
1786 }
1787 
1788 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1789 
1794 inline
1796 {
1797  // Test program was HERE.
1798  return op1 * (-1.0);
1799 }
1800 
1801 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1802 
1803 /* ---------------------------------------------------------------------
1804  * COMPARISON OPERATORS
1805  *
1806  * Comparison operators (==, !=).
1807  * ------------------------------------------------------------------- */
1808 
1813 bool operator==(const Vector& op1, const Vector::Impl_type& op2);
1814 
1815 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1816 
1823 inline
1824 bool operator==(const Vector::Impl_type& op1, const Vector& op2)
1825 {
1826  // Test program was HERE.
1827  return op2 == op1;
1828 }
1829 
1830 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1831 
1836 inline
1837 bool operator!=(const Vector& op1, const Vector::Impl_type &op2)
1838 {
1839  // Test program was HERE.
1840  return !(op1 == op2);
1841 }
1842 
1843 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1844 
1851 inline
1852 bool operator!=(const Vector::Impl_type& op1, const Vector& op2)
1853 {
1854  // Test program was HERE.
1855  return op2 != op1;
1856 }
1857 
1858 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1859 
1864 inline
1865 bool operator==(const Vector& op1, const Vector& op2)
1866 {
1867  // Test program was HERE.
1868  return op1 == *op2.get_c_vector();
1869 }
1870 
1871 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1872 
1877 inline
1878 bool operator!=(const Vector& op1, const Vector &op2)
1879 {
1880  // Test program was HERE.
1881  return op1 != *op2.get_c_vector();
1882 }
1883 
1884 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1885 
1886 /* ---------------------------------------------------------------------
1887  * ORDERING OPERATORS
1888  * These seem silly, but they're required to comply with the
1889  * stl "Forward container" concept. [I can easily think of
1890  * situations where this is necessary. Silly? I think not!]
1891  * ------------------------------------------------------------------- */
1892 
1896 bool operator<(const Vector& op1, const Vector &op2);
1897 
1901 inline
1902 bool operator<=(const Vector& op1, const Vector& op2)
1903 {
1904  return (op1 < op2) || (op1 == op2);
1905 }
1906 
1910 inline
1911 bool operator>(const Vector& op1, const Vector& op2)
1912 {
1913  return !(op1 < op2) && !(op1 == op2);
1914 }
1915 
1919 inline
1920 bool operator>=(const Vector& op1, const Vector& op2)
1921 {
1922  return !(op1 < op2);
1923 }
1924 
1925 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1926 
1933 std::ostream& operator<<(std::ostream& out, const Vector& m);
1934 
1935 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1936 
1946 std::ostream& stream_write_vector(std::ostream& ost, const Vector& m);
1947 
1948 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1949 
1958 std::istream& stream_read_vector(std::istream& ist, Vector& m);
1959 
1960 
1961 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1962 
1963 /* ---------------------------------------------------------------------
1964  * OTHER STUFF
1965  * ------------------------------------------------------------------- */
1966 
1973 inline
1975 {
1976  if ( op1.get_length() != op2.get_length() )
1977  {
1978  // Test program was HERE.
1980  }
1981  //else
1982  //{
1983  // // Test program was HERE.
1984  //}
1985  return kjb_c::max_abs_vector_difference( op1.get_c_vector(), op2.get_c_vector() );
1986 }
1987 
1988 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
1989 
1993 inline
1994 Vector::Value_type dot(const Vector& op1, const Vector& op2)
1995 {
1996  // Test program was HERE.
1997  Vector::Value_type result;
1998  ETX( kjb_c::get_dot_product(op1.get_c_vector(), op2.get_c_vector(), &result) );
1999  return result;
2000 }
2001 
2002 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
2003 
2009 Vector cross(const Vector& op1, const Vector& op2);
2010 
2011 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
2012 
2019 inline
2020 double norm1(const Vector& op)
2021 {
2022  double total = 0;
2023 
2024  for(Vector::const_iterator it = op.begin(); it != op.end(); ++it)
2025  total += fabs(*it);
2026 
2027  return total;
2028 }
2029 
2030 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
2031 
2038 inline
2039 double norm2(const Vector& op1)
2040 {
2041  return op1.magnitude();
2042 }
2043 
2044 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
2045 
2051 inline
2052 double vector_distance(const Vector& op1, const Vector& op2)
2053 {
2054  return kjb_c::vector_distance(op1.get_c_vector(), op2.get_c_vector());
2055 }
2056 
2057 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
2058 
2064 inline
2065 double vector_distance_squared(const Vector& op1, const Vector& op2)
2066 {
2067  return kjb_c::vector_distance_sqrd(op1.get_c_vector(), op2.get_c_vector());
2068 }
2069 
2070 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
2071 
2075 inline
2077 {
2078  // Test program was HERE.
2079  return kjb_c::max_vector_element(vec.get_c_vector());
2080 }
2081 
2082 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
2083 
2087 inline
2089 {
2090  // Test program was HERE.
2091  return kjb_c::min_vector_element(vec.get_c_vector());
2092 }
2093 
2094 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
2095 
2099 inline Vector cat_vectors(const Vector& first, const Vector& second)
2100 {
2101  Vector result = first;
2102  result.resize(first.size() + second.size());
2103  std::copy(second.begin(), second.end(), result.begin() + first.size());
2104  return result;
2105 }
2106 
2107 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
2108 
2112 template<class InputIterator>
2113 Vector cat_vectors(InputIterator first, InputIterator last)
2114 {
2115  int size = 0;
2116 
2117  for(InputIterator p = first; p != last; p++)
2118  {
2119  size += p->get_length();
2120  }
2121 
2122  Vector ret(size);
2123 
2124  size = 0;
2125  for(InputIterator p = first; p != last; p++)
2126  {
2127  for(int i = 0; i < p->get_length(); i++)
2128  {
2129  ret[size + i] = (*p)[i];
2130  }
2131  size += p->get_length();
2132  }
2133 
2134  return ret;
2135 }
2136 
2137 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
2138 
2143 template<class OutputIterator>
2144 OutputIterator separate_vector(const Vector& vec, int sz, OutputIterator result)
2145 {
2146  int n = vec.get_length() / sz;
2147 
2148  if(n == 0)
2149  {
2150  KJB_THROW_2(Illegal_argument, "separate_vectors: cannot desired vector size is too large.");
2151  }
2152 
2153  for(int i = 0; i < n; i++)
2154  {
2155  *result++ = create_vector_from_vector_section(vec, i * sz, sz);
2156  }
2157 
2158  return result;
2159 }
2160 
2165 std::vector<Vector> get_transpose(const std::vector<Vector>& m);
2166 
2168 Int_vector floor( const Vector& realv );
2169 
2170 /*
2171  * @brief generates a Vector of uniformly spaced points along a vector (including endpoints)
2172  * Ideally it should be matlab's "linspace"
2173  * @param double a - start point, double b - end point, double n - number of points
2174  * @author Josh Bowdish
2175  */
2176 kjb::Vector create_uniformly_spaced_vector(double a,double b, unsigned n);
2177 
2178 
2182 inline void swap(Vector& v1, Vector& v2) { v1.swap(v2); }
2183 
2184 }
2185 
2186 //namespace kjb
2187 
2188 
2189 #endif /*VECTOR_WRAP_H */
2190 
Vector & subtract(const Vector &op2)
Subtract vector from self, in-place, just like v -= delta_v.
Definition: m_vector.h:1336
Vector & resize(int new_length, Value_type pad=Value_type(0))
Resize vector, retaining previous values.
Definition: m_vector.cpp:242
Generic_vector_view< Vector > Vector_view
Definition: m_vector.h:64
double * pointer
Definition: m_vector.h:110
Vector & cross_with(const Vector &op2)
Compute cross product of op1 and op2.
Definition: m_vector.cpp:543
kjb::Vector create_uniformly_spaced_vector(double a, double b, unsigned n)
Definition: m_vector.cpp:667
double vector_distance(const Int_vector &op1, const Int_vector &op2)
Compute the Euclidian distance between two vectors.
Definition: l_int_vector.h:1569
Int_vector cross(const Int_vector &op1, const Int_vector &op2)
Compute cross product of op1 and op2.
Definition: l_int_vector.cpp:381
Vector & ew_multiply(const Vector &op2)
Element wise multiplication of two vectors.
Definition: m_vector.h:1241
void pop_back()
Returns the last element of the vector.
Definition: m_vector.h:1142
Vector & operator/=(Value_type op2)
Scalar division of self, in-place.
Definition: m_vector.h:1253
Vector create_zero_vector(int length)
Construct a vector containing zeroes.
Definition: m_vector.h:1666
#define ETX(a)
Definition: l_exception.h:67
double vector_distance_squared(const Int_vector &op1, const Int_vector &op2)
Compute the square of the Euclidian distance between two vectors.
Definition: l_int_vector.h:1581
Int_matrix::Value_type max(const Int_matrix &mat)
Return the maximum value in this matrix.
Definition: l_int_matrix.h:1397
reference back()
Returns the last element of the vector.
Definition: m_vector.h:1107
Vector create_random_vector(int length)
Construct a vector with values drawn from a uniform distribution over [0,1].
Definition: m_vector.h:1643
iterator insert(iterator position, value_type t)
A copy of t is inserted before position.
Definition: m_vector.cpp:282
size_type size() const
Alias to get_length(). Required to comply with stl Container concept.
Definition: m_vector.h:510
Vector & negate()
Negate self, in-place, just like v *= (-1).
Definition: m_vector.h:1347
bool empty() const
Returns true iff size is zero. Required to comply with stl Container concept.
Definition: m_vector.h:526
Vector cat_vectors(const Vector &first, const Vector &second)
Concatenate two vectors.
Definition: m_vector.h:2099
void write_col(const char *filename=0) const
Write vector as a column to a file, or to standard output.
Definition: m_vector.h:1170
Vector & divide(Value_type op2)
Scalar division of self, in-place, just like v /= 2.
Definition: m_vector.h:1265
Vector normalized(kjb_c::Norm_method method=kjb_c::NORMALIZE_BY_MAGNITUDE) const
non=mutating version of "normalize()"
Definition: m_vector.h:1534
Vector & replace(const Vector &iv, int offset, int begin, int length)
Copy elements from input vector iv into this vector, starting at some offset, leaving the rest of thi...
Definition: m_vector.cpp:222
Vector & operator+=(const Vector &op2)
Add vector to self, in-place, e.g., v += delta_v.
Definition: m_vector.h:1287
std::reverse_iterator< const_iterator > const_reverse_iterator
const Iterator type
Definition: m_vector.h:120
const_reverse_iterator rbegin() const
Definition: m_vector.h:587
int get_length() const
Return the length of the vector.
Definition: m_vector.h:501
Object thrown when an argument is of the wrong size or dimensions.
Definition: l_exception.h:426
Vector(int length, const Value_type *data)
Initialize from an array of doubles, of given length.
Definition: m_vector.h:238
iterator erase(iterator position)
erase element at position and shift all elements after p up by 1.
Definition: m_vector.cpp:343
Vector(unsigned long length)
Allocate vector of given length; contents are uninitialized.
Definition: m_vector.h:214
Int_matrix::Value_type max_abs_difference(const Int_matrix &op1, const Int_matrix &op2)
Find the largest difference between two matrices.
Definition: l_int_matrix.h:1364
double Value_type
data type of the elements
Definition: m_vector.h:101
const value_type * const_iterator
Definition: m_vector.h:118
void swap(Perspective_camera &cam1, Perspective_camera &cam2)
Swap two cameras.
Definition: perspective_camera.h:599
kjb_c::Vector Impl_type
the underlying implementation
Definition: m_vector.h:102
std::vector< Vector > get_transpose(const std::vector< Vector > &m)
Treat a std::vector of kjb::Vectors as a matrix and get its 'transpose'.
Definition: m_vector.cpp:631
const Value_type & at(int i) const
Safely subscript vector, e.g., A.at(10), to get an rvalue.
Definition: m_vector.h:1005
Value_type & at(int i)
Safely subscript vector, e.g., A.at(10), to get an lvalue.
Definition: m_vector.h:991
Vector Vec_type
the associated vector type
Definition: m_vector.h:105
void write_row(const char *filename=0) const
Write vector as a row to a file, or to standard output.
Definition: m_vector.h:1155
void push_back(Value_type x)
inserts an element at the back of the vector in amortized constant time.
Definition: m_vector.h:1128
bool operator<(const Face_detection &f1, const Face_detection &f2)
Compares to boxes using middle of box. Needed because we have associated containers of these...
Definition: d_facecom.cpp:147
Image operator-(const Image &im1, const Image &im2)
Subtract two images.
Definition: i_image.h:843
Vector(InputIterator_ begin_, InputIterator_ end_)
"Range constructor"
Definition: m_vector.h:473
#define KJB_THROW(ex)
Definition: l_exception.h:46
int size_type
Definition: m_vector.h:121
void read(const char *filename=0)
Read vector from a file, or from standard input.
Definition: m_vector.h:1183
double & reference
Definition: m_vector.h:112
Vector & set(Value_type val1, Value_type val2, Value_type val3)
Convert to a 3-vector and set its values to the arguments.
Definition: m_vector.h:730
Value_type(* Mapper)(Value_type)
element transformer fun
Definition: m_vector.h:106
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
Vector & randomize(int length)
Clobber current vector; resize and fill with random values.
Definition: m_vector.h:631
Impl_type *& get_underlying_representation_with_guilt()
Get pointer to the underlying kjb_c::Vector C struct.
Definition: m_vector.h:857
bool operator>=(const Int_vector &op1, const Int_vector &op2)
Test lexicographic ordering between vectors.
Definition: l_int_vector.h:1494
Vector & zero_out()
Clobber current vector contents with zeroes.
Definition: m_vector.h:684
Generic_matrix_view< Matrix > Mat_view_type
view type of the associated matrix type
Definition: m_vector.h:104
Value_type & operator[](int i)
Subscript vector like a C array, e.g., A[10], returning an lvalue.
Definition: m_vector.h:905
size_t length(const C &cner)
Counts the total number of elements in a 2D STL-style container.
Definition: l_util.h:17
Object thrown when a function cannot generate a valid result.
Definition: l_exception.h:516
reference front()
Returns the first element of the vector.
Definition: m_vector.h:1086
int difference_type
Definition: m_vector.h:122
const_reference front() const
Returns the first element of the vector.
Definition: m_vector.h:1097
Image operator+(const Image &op1, const Image &op2)
Add two images.
Definition: i_image.h:834
kjb::Matrix hat() const
Construct an "equivalent" skew-symmetric matrix from 3-vector.
Definition: m_vector.cpp:516
double value_type
Definition: m_vector.h:109
value_type * iterator
Definition: m_vector.h:117
const Value_type & operator()(int i) const
Fortran-style vector subscript, e.g., A(10), to get an rvalue.
Definition: m_vector.h:945
Vector & operator=(const Mat_view_type &vec_ref)
Assignment operator: assign from a single-row matrix-view.
Definition: m_vector.cpp:213
reverse_iterator rbegin()
Definition: m_vector.h:577
void clear()
delete all elements from vector
Definition: m_vector.h:1075
void kjb_serialize(Archive &ar, KJB_readable_writable &obj, const unsigned int version)
Definition: l_serialization.h:170
Vector & operator=(const Vector &src)
Assignment operator: assign from a kjb::Vector, a C++ object.
Definition: m_vector.h:805
Vector(Value_type val)
Create a single-element vector.
Definition: m_vector.h:313
Vector & operator=(const Impl_type &vec_ref)
Assignment operator: assign from a kjb_c::Vector, a C struct.
Definition: m_vector.h:767
~Vector()
Destructor – which just calls the KJB destructor.
Definition: m_vector.h:490
const double * const_pointer
Definition: m_vector.h:111
Vector(Value_type val1, Value_type val2)
Create a two-vector.
Definition: m_vector.h:339
Vector & zero_out(int length)
Clone of randomize(int)
Definition: m_vector.h:671
iterator end()
Definition: m_vector.h:557
Vector(Value_type val1, Value_type val2, Value_type val3, Value_type val4)
Create a four-vector.
Definition: m_vector.h:368
iterator begin()
Definition: m_vector.h:537
Definition: m_mat_view.h:55
Value_type & operator()(int i)
Fortran-style vector subscript, e.g., A(10), to get an lvalue.
Definition: m_vector.h:932
Vector(Value_type val1, Value_type val2, Value_type val3)
Create a three-vector.
Definition: m_vector.h:353
Int_matrix floor(const Matrix &m)
Definition: m_matrix.cpp:2026
x
Definition: APPgetLargeConnectedEdges.m:100
Vector & set(Value_type val)
Clone of zero_out(int)
Definition: m_vector.h:707
Vector & operator-=(const Vector &op2)
Subtract vector from self, in-place, e.g., v -= delta_v.
Definition: m_vector.h:1317
void reserve(int capacity)
Definition: m_vector.h:618
double get_max_abs_difference(const Vector &op2) const
returns the sum of the element-wise differences between this vector and the input vector ...
Definition: m_vector.cpp:475
const Value_type & operator[](int i) const
Subscript vector like a C array, e.g., A[10], returning an rvalue.
Definition: m_vector.h:919
Value_type magnitude() const
Return this vector's magnitude.
Definition: m_vector.h:1490
This class implements vectors, in the linear-algebra sense, restricted to integer-valued elements...
Definition: l_int_vector.h:83
const_reference back() const
Returns the last element of the vector.
Definition: m_vector.h:1118
Vec_type & mapcar(Mapper)
Transform the elements of a vector.
Definition: m_vector.cpp:502
Vector & multiply(Value_type op2)
Scalar multiply self, in-place, just like v *= 6.
Definition: m_vector.h:1230
bool operator>(const Int_vector &op1, const Int_vector &op2)
Test lexicographic ordering between vectors.
Definition: l_int_vector.h:1485
void check_bounds(int i) const
Test whether a subscript is valid, throw an exception if not.
Definition: m_vector.h:975
std::ofstream & operator<<(std::ofstream &out, const Quaternion &q)
Definition: turntable_camera.cpp:77
Vector & set(Value_type val1, Value_type val2)
Convert to a 2-vector and set its values to the arguments.
Definition: m_vector.h:718
Image operator/(const Image &op1, double op2)
Scale an image in channel space, yielding a new image.
Definition: i_image.h:825
const_iterator end() const
Definition: m_vector.h:567
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
void write(const char *filename=0) const
Write vector to a file, or to standard output.
Definition: m_vector.h:1198
bool operator!=(const Int_matrix &op1, const Int_matrix::Impl_type &op2)
Test for any difference between two matrices.
Definition: l_int_matrix.h:1274
Vector(int length=0)
Allocate vector of given length; contents are uninitialized.
Definition: m_vector.h:182
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
Value_type max(int *max_index) const
Find maximum element in the vector, both its value and index.
Definition: m_vector.h:1425
Matrix Mat_type
the associated matrix type
Definition: m_vector.h:103
Vector & normalize(kjb_c::Norm_method method=kjb_c::NORMALIZE_BY_MAGNITUDE)
Normalize vector in place by choice of method (default by magnitude).
Definition: m_vector.h:1524
bool operator==(const Int_matrix &op1, const Int_matrix::Impl_type &op2)
Test for exact equality between two matrices.
Definition: l_int_matrix.cpp:218
Int_vector create_vector_from_vector_section(const Int_vector &iv, int begin, int length)
Construct a vector by deep-copying a section of another vector.
Definition: l_int_vector.h:1252
#define ETX_2(a, msg)
Definition: l_exception.h:78
Vector(const Vector &vec_ref)
Copy ctor – calls the kjb_c function to copy a vector.
Definition: m_vector.h:433
Vector & randomize()
Clobber current vector contents with random values.
Definition: m_vector.h:647
Definition: l_index.h:65
Vector create_gauss_random_vector(int length)
Construct a vector with values drawn from a standard Gaussian distribution (mean 0, variance 1);.
Definition: m_vector.cpp:390
Int_matrix::Value_type min(const Int_matrix &mat)
Return the minimum value in this matrix.
Definition: l_int_matrix.h:1385
double norm1(const Vector &op)
Compute L1-norm of vector.
Definition: m_vector.h:2020
Vector(int length, Value_type num)
Allocate vector of given length; initialize all elts to 'num'.
Definition: m_vector.h:226
Vector & operator*=(Value_type op2)
Scalar multiply self, in-place, e.g., v *= 6.
Definition: m_vector.h:1218
size_type max_size() const
Maximum size vector can ever have. Currently defined as INT_MAX.
Definition: m_vector.h:518
std::reverse_iterator< iterator > reverse_iterator
Definition: m_vector.h:119
Vector(unsigned length)
Allocate vector of given length; contents are uninitialized.
Definition: m_vector.h:198
const_iterator begin() const
Definition: m_vector.h:547
void swap(Vector &other)
Swap the representations of two vectors.
Definition: m_vector.h:884
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
Definition: m_vec_view.h:56
Vector & set(Value_type val1, Value_type val2, Value_type val3, Value_type val4)
Convert to a 4-vector and set its values to the arguments.
Definition: m_vector.h:743
Vector(int length, const float *data)
Initialize from an array of floats, of given length.
Definition: m_vector.h:258
const Generic_vector_view< const Vector > Const_vector_view
Definition: m_vector.h:67
double norm2(const Int_vector &op1)
Compute l2-norm of vector.
Definition: l_int_vector.h:1558
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
long int dot(const Int_vector &op1, const Int_vector &op2)
Returns dot product of this and op2.
Definition: l_int_vector.h:1532
for m
Definition: APPgetLargeConnectedEdges.m:64
double vector_distance(const Vector &op1, const Vector &op2)
Compute the Euclidian distance between two vectors.
Definition: m_vector.h:2052
Vector & add(const Vector &op2)
Add vector to self, in-place, just like v += delta_v.
Definition: m_vector.h:1306
double const_reference
Definition: m_vector.h:113
reverse_iterator rend()
Definition: m_vector.h:597
Support for error handling exception classes in libKJB.
std::ostream & stream_write_vector(std::ostream &ost, const Vector &v)
Write vector to an output stream so it can be read with read_vector.
Definition: m_vector.cpp:598
Value_type magnitude_squared() const
Return this vector's squared magnitude.
Definition: m_vector.h:1501
const Impl_type * get_c_vector() const
Get const pointer to the underlying kjb_c::Vector C struct.
Definition: m_vector.h:844
bool operator<=(const Int_vector &op1, const Int_vector &op2)
Test lexicographic ordering between vectors.
Definition: l_int_vector.h:1476
Gsl_Vector operator*(double scalar, const Gsl_Vector &vector)
multiply scalar and vector, scalar written on the left side
Definition: gsl_vector.h:661
void assign(size_type N, value_type t)
Resizes to N, filling all values with t.
Definition: m_vector.cpp:306
Vector & ew_divide(const Vector &op2)
Element wise division of two vectors; result is stored in first.
Definition: m_vector.h:1276
std::istream & stream_read_vector(std::istream &ist, Vector &v)
Read vector from an input stream.
Definition: m_vector.cpp:615
Vector(Impl_type *vec_ptr)
Conversion ctor: claim ownership of an existing vector pointer (i.e., make a shallow copy)...
Definition: m_vector.h:286
OutputIterator separate_vector(const Vector &vec, int sz, OutputIterator result)
Separate the given vector into vectors of the give size; the vectors will be output into the output i...
Definition: m_vector.h:2144
Vector(const Impl_type &vec_ref)
Ctor copies contents (i.e., deep copy) of an existing vector.
Definition: m_vector.h:390
const_reverse_iterator rend() const
Definition: m_vector.h:607
Impl_type * get_underlying_representation_unsafe()
Get pointer to the underlying kjb_c::Vector C struct.
Definition: m_vector.h:873
Value_type min(int *min_index) const
Find minimum element in the vector, both its value and index.
Definition: m_vector.h:1384
Value_type sum_vector_elements() const
Return this vector's elements sum.
Definition: m_vector.h:1512