00001
00002 #ifndef vnl_vector_h_
00003 #define vnl_vector_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <vcl_iosfwd.h>
00020 #include <vnl/vnl_tag.h>
00021 #include <vnl/vnl_c_vector.h>
00022 #include <vnl/vnl_config.h>
00023 #ifndef NDEBUG
00024 # include <vnl/vnl_error.h>
00025 # if VNL_CONFIG_CHECK_BOUNDS
00026 # include <vcl_cassert.h>
00027 # endif
00028 #else
00029 # undef VNL_CONFIG_CHECK_BOUNDS
00030 # define VNL_CONFIG_CHECK_BOUNDS 0
00031 # undef ERROR_CHECKING
00032 #endif
00033 #if VNL_CONFIG_LEGACY_METHODS
00034 # include <vcl_deprecated.h>
00035 #endif
00036
00037 export template <class T> class vnl_vector;
00038 export template <class T> class vnl_matrix;
00039
00040
00041
00042 #define v vnl_vector<T>
00043 #define m vnl_matrix<T>
00044 template <class T> T dot_product(v const&, v const&);
00045 template <class T> T inner_product(v const&, v const&);
00046 template <class T> T bracket(v const &, m const &, v const &);
00047 template <class T> T cos_angle(v const&, v const& );
00048 template <class T> double angle(v const&, v const&);
00049 template <class T> m outer_product(v const&, v const&);
00050 template <class T> v operator+(T, v const&);
00051 template <class T> v operator-(T, v const&);
00052 template <class T> v operator*(T, v const&);
00053
00054 template <class T> v operator*(v const&, m const&);
00055 template <class T> v element_product(v const&,v const&);
00056 template <class T> v element_quotient(v const&,v const&);
00057 template <class T> T vnl_vector_ssd(v const&, v const&);
00058 template <class T> void swap(v &, v &);
00059 #undef v
00060 #undef m
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 template<class T>
00074 class vnl_vector
00075 {
00076 public:
00077 friend class vnl_matrix<T>;
00078
00079
00080 vnl_vector() : num_elmts(0) , data(0) {}
00081
00082
00083
00084 explicit vnl_vector(unsigned len);
00085
00086
00087 vnl_vector(unsigned len, T const& v0);
00088
00089
00090 vnl_vector(unsigned len, int n, T const values[]);
00091
00092 #if VNL_CONFIG_LEGACY_METHODS // these constructors are deprecated and should not be used
00093
00094
00095
00096
00097 vnl_vector(unsigned len, T const& px, T const& py);
00098
00099
00100
00101
00102
00103 vnl_vector(unsigned len, T const& px, T const& py, T const& pz);
00104
00105
00106
00107
00108
00109 vnl_vector(unsigned len, T const& px, T const& py, T const& pz, T const& pw);
00110 #endif
00111
00112
00113 vnl_vector(T const* data_block,unsigned int n);
00114
00115
00116 vnl_vector(vnl_vector<T> const&);
00117
00118 #ifndef VXL_DOXYGEN_SHOULD_SKIP_THIS
00119
00120
00121
00122 vnl_vector(vnl_vector<T> const &, vnl_vector<T> const &, vnl_tag_add);
00123 vnl_vector(vnl_vector<T> const &, vnl_vector<T> const &, vnl_tag_sub);
00124 vnl_vector(vnl_vector<T> const &, T, vnl_tag_mul);
00125 vnl_vector(vnl_vector<T> const &, T, vnl_tag_div);
00126 vnl_vector(vnl_vector<T> const &, T, vnl_tag_add);
00127 vnl_vector(vnl_vector<T> const &, T, vnl_tag_sub);
00128 vnl_vector(vnl_matrix<T> const &, vnl_vector<T> const &, vnl_tag_mul);
00129 vnl_vector(vnl_vector<T> const &, vnl_matrix<T> const &, vnl_tag_mul);
00130 vnl_vector(vnl_vector<T> &that, vnl_tag_grab)
00131 : num_elmts(that.num_elmts), data(that.data)
00132 { that.num_elmts=0; that.data=0; }
00133
00134 #endif
00135
00136
00137 ~vnl_vector();
00138
00139
00140 unsigned size() const { return num_elmts; }
00141
00142
00143 inline void put(unsigned int i, T const&);
00144
00145
00146 inline T get(unsigned int i) const;
00147
00148
00149 void fill(T const& v);
00150
00151
00152
00153 void copy_in(T const * ptr);
00154
00155
00156
00157 void copy_out(T *) const;
00158
00159
00160
00161
00162 void set(T const *ptr) { copy_in(ptr); }
00163
00164
00165
00166 T & operator()(unsigned int i)
00167 {
00168 #if VNL_CONFIG_CHECK_BOUNDS
00169 assert(i<size());
00170 #endif
00171 return data[i];
00172 }
00173
00174
00175 T const & operator()(unsigned int i) const
00176 {
00177 #if VNL_CONFIG_CHECK_BOUNDS
00178 assert(i<size());
00179 #endif
00180 return data[i];
00181 }
00182
00183
00184 T & operator[](unsigned int i) { return data[i]; }
00185
00186 T const & operator[](unsigned int i) const { return data[i]; }
00187
00188
00189 vnl_vector<T>& operator=(T const&v) { fill(v); return *this; }
00190
00191
00192 vnl_vector<T>& operator=(vnl_vector<T> const& rhs);
00193
00194
00195 vnl_vector<T>& operator+=(T );
00196
00197
00198 vnl_vector<T>& operator-=(T value) { return *this += (-value); }
00199
00200
00201 vnl_vector<T>& operator*=(T );
00202
00203
00204 vnl_vector<T>& operator/=(T );
00205
00206
00207 vnl_vector<T>& operator+=(vnl_vector<T> const& rhs);
00208
00209
00210 vnl_vector<T>& operator-=(vnl_vector<T> const& rhs);
00211
00212
00213
00214 vnl_vector<T>& pre_multiply(vnl_matrix<T> const& M);
00215
00216
00217
00218 vnl_vector<T>& post_multiply(vnl_matrix<T> const& M);
00219
00220
00221
00222 vnl_vector<T>& operator*=(vnl_matrix<T> const& m) { return this->post_multiply(m); }
00223
00224
00225
00226 vnl_vector<T> operator+() const { return *this; }
00227
00228
00229
00230 vnl_vector<T> operator-() const;
00231
00232 vnl_vector<T> operator+(T v) const { return vnl_vector<T>(*this, v, vnl_tag_add()); }
00233 vnl_vector<T> operator-(T v) const { return vnl_vector<T>(*this, v, vnl_tag_sub()); }
00234 vnl_vector<T> operator*(T v) const { return vnl_vector<T>(*this, v, vnl_tag_mul()); }
00235 vnl_vector<T> operator/(T v) const { return vnl_vector<T>(*this, v, vnl_tag_div()); }
00236
00237 vnl_vector<T> operator+(vnl_vector<T> const& v) const { return vnl_vector<T>(*this, v, vnl_tag_add()); }
00238 vnl_vector<T> operator-(vnl_vector<T> const& v) const { return vnl_vector<T>(*this, v, vnl_tag_sub()); }
00239 vnl_vector<T> operator*(vnl_matrix<T> const& M) const { return vnl_vector<T>(*this, M, vnl_tag_mul()); }
00240
00241
00242
00243
00244
00245 T const* data_block() const { return data; }
00246
00247
00248
00249 T * data_block() { return data; }
00250
00251
00252 typedef T element_type;
00253
00254 typedef T *iterator;
00255
00256 iterator begin() { return data; }
00257
00258
00259 iterator end() { return data+num_elmts; }
00260
00261
00262 typedef T const *const_iterator;
00263
00264 const_iterator begin() const { return data; }
00265
00266 const_iterator end() const { return data+num_elmts; }
00267
00268
00269
00270
00271
00272 vnl_vector<T> const& as_ref() const { return *this; }
00273
00274
00275 vnl_vector<T>& as_ref() { return *this; }
00276
00277
00278 vnl_vector<T> apply(T (*f)(T)) const;
00279
00280 vnl_vector<T> apply(T (*f)(T const&)) const;
00281
00282
00283 vnl_vector<T> extract(unsigned int len, unsigned int start=0) const;
00284
00285
00286 vnl_vector<T>& update(vnl_vector<T> const&, unsigned int start=0);
00287
00288
00289 typedef typename vnl_c_vector<T>::abs_t abs_t;
00290
00291
00292 abs_t squared_magnitude() const { return vnl_c_vector<T>::two_nrm2(begin(), size()); }
00293
00294
00295 abs_t magnitude() const { return two_norm(); }
00296
00297
00298 abs_t one_norm() const { return vnl_c_vector<T>::one_norm(begin(), size()); }
00299
00300
00301 abs_t two_norm() const { return vnl_c_vector<T>::two_norm(begin(), size()); }
00302
00303
00304 abs_t inf_norm() const { return vnl_c_vector<T>::inf_norm(begin(), size()); }
00305
00306
00307 vnl_vector<T>& normalize() { vnl_c_vector<T>::normalize(begin(), size()); return *this; }
00308
00309
00310
00311
00312
00313 abs_t rms() const { return vnl_c_vector<T>::rms_norm(begin(), size()); }
00314
00315
00316 T min_value() const { return vnl_c_vector<T>::min_value(begin(), size()); }
00317
00318
00319 T max_value() const { return vnl_c_vector<T>::max_value(begin(), size()); }
00320
00321
00322 unsigned arg_min() const { return vnl_c_vector<T>::arg_min(begin(), size()); }
00323
00324
00325 unsigned arg_max() const { return vnl_c_vector<T>::arg_max(begin(), size()); }
00326
00327
00328 T mean() const { return vnl_c_vector<T>::mean(begin(), size()); }
00329
00330
00331 T sum() const { return vnl_c_vector<T>::sum(begin(), size()); }
00332
00333
00334
00335 void flip();
00336
00337
00338 void swap(vnl_vector<T> & that);
00339
00340 #if VNL_CONFIG_LEGACY_METHODS // these methods are deprecated and should not be used
00341
00342
00343 T& x() const { VXL_DEPRECATED("vnl_vector<T>::x()"); return data[0]; }
00344
00345
00346 T& y() const { VXL_DEPRECATED("vnl_vector<T>::y()"); return data[1]; }
00347
00348
00349 T& z() const { VXL_DEPRECATED("vnl_vector<T>::z()"); return data[2]; }
00350
00351
00352 T& t() const { VXL_DEPRECATED("vnl_vector<T>::t()"); return data[3]; }
00353
00354
00355 void set_x(T const&xx) { VXL_DEPRECATED("vnl_vector<T>::set_x()"); if (size() >= 1) data[0] = xx; }
00356
00357
00358 void set_y(T const&yy) { VXL_DEPRECATED("vnl_vector<T>::set_y()"); if (size() >= 2) data[1] = yy; }
00359
00360
00361 void set_z(T const&zz) { VXL_DEPRECATED("vnl_vector<T>::set_z()"); if (size() >= 3) data[2] = zz; }
00362
00363
00364 void set_t(T const&tt) { VXL_DEPRECATED("vnl_vector<T>::set_t()"); if (size() >= 4) data[3] = tt; }
00365 #endif // VNL_CONFIG_LEGACY_METHODS
00366
00367
00368
00369 void assert_size(unsigned sz) const {
00370 #ifndef NDEBUG
00371 assert_size_internal(sz);
00372 #endif
00373 }
00374
00375
00376
00377 void assert_finite() const {
00378 #ifndef NDEBUG
00379 assert_finite_internal();
00380 #endif
00381 }
00382
00383
00384 bool is_finite() const;
00385
00386
00387 bool is_zero() const;
00388
00389
00390 bool empty() const { return !data || !num_elmts; }
00391
00392
00393 bool operator_eq(vnl_vector<T> const& v) const;
00394
00395
00396 bool operator==(vnl_vector<T> const &that) const { return this->operator_eq(that); }
00397
00398
00399 bool operator!=(vnl_vector<T> const &that) const { return !this->operator_eq(that); }
00400
00401
00402
00403
00404 bool set_size(unsigned n);
00405
00406
00407 void clear();
00408
00409
00410
00411 bool read_ascii(vcl_istream& s);
00412
00413
00414 static vnl_vector<T> read(vcl_istream& s);
00415
00416 protected:
00417 unsigned num_elmts;
00418 T* data;
00419
00420 #if VCL_HAS_SLICED_DESTRUCTOR_BUG
00421
00422
00423 char vnl_vector_own_data;
00424 #endif
00425
00426 void assert_size_internal(unsigned sz) const;
00427 void assert_finite_internal() const;
00428
00429 void destroy();
00430
00431 #if VCL_NEED_FRIEND_FOR_TEMPLATE_OVERLOAD
00432 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00433 # define v vnl_vector<T>
00434 # define m vnl_matrix<T>
00435 #endif // DOXYGEN_SHOULD_SKIP_THIS
00436 friend T dot_product VCL_NULL_TMPL_ARGS (v const&, v const&);
00437 friend T inner_product VCL_NULL_TMPL_ARGS (v const&, v const&);
00438 friend T bracket VCL_NULL_TMPL_ARGS (v const&, m const&, v const&);
00439 friend T cos_angle VCL_NULL_TMPL_ARGS (v const&, v const&);
00440 friend double angle VCL_NULL_TMPL_ARGS (v const&, v const&);
00441 friend m outer_product VCL_NULL_TMPL_ARGS (v const&, v const&);
00442 friend v operator+ VCL_NULL_TMPL_ARGS (T const, v const&);
00443 friend v operator- VCL_NULL_TMPL_ARGS (T const, v const&);
00444 friend v operator* VCL_NULL_TMPL_ARGS (T const, v const&);
00445 friend v operator* VCL_NULL_TMPL_ARGS (m const&, v const&);
00446 friend v element_product VCL_NULL_TMPL_ARGS (v const&, v const&);
00447 friend v element_quotient VCL_NULL_TMPL_ARGS (v const&, v const&);
00448 # undef v
00449 # undef m
00450 #endif
00451
00452
00453 static void inline_function_tickler();
00454 };
00455
00456
00457
00458
00459
00460
00461
00462
00463 template <class T>
00464 inline T vnl_vector<T>::get(unsigned int index) const
00465 {
00466 #ifdef ERROR_CHECKING
00467 if (index >= this->num_elmts)
00468 vnl_error_vector_index("get", index);
00469 #endif
00470 return this->data[index];
00471 }
00472
00473
00474
00475
00476 template <class T>
00477 inline void vnl_vector<T>::put(unsigned int index, T const& value)
00478 {
00479 #ifdef ERROR_CHECKING
00480 if (index >= this->num_elmts)
00481 vnl_error_vector_index("put", index);
00482 #endif
00483 this->data[index] = value;
00484 }
00485
00486
00487
00488
00489 template<class T>
00490 inline vnl_vector<T> operator*(vnl_matrix<T> const& m, vnl_vector<T> const& v)
00491 {
00492 return vnl_vector<T>(m, v, vnl_tag_mul());
00493 }
00494
00495
00496
00497 template<class T>
00498 inline vnl_vector<T> operator+(T s, vnl_vector<T> const& v)
00499 {
00500 return vnl_vector<T>(v, s, vnl_tag_add());
00501 }
00502
00503
00504
00505 template<class T>
00506 inline vnl_vector<T> operator-(T s, vnl_vector<T> const& v)
00507 {
00508 return vnl_vector<T>(-v, s, vnl_tag_add());
00509 }
00510
00511
00512
00513 template<class T>
00514 inline vnl_vector<T> operator*(T s, vnl_vector<T> const& v)
00515 {
00516 return vnl_vector<T>(v, s, vnl_tag_mul());
00517 }
00518
00519
00520
00521 template<class T>
00522 inline void swap(vnl_vector<T> &a, vnl_vector<T> &b) { a.swap(b); }
00523
00524
00525
00526
00527 template<class T>
00528 inline T vnl_vector_ssd(vnl_vector<T> const& v1, vnl_vector<T> const& v2)
00529 {
00530 #ifndef NDEBUG
00531 if (v1.size() != v2.size())
00532 vnl_error_vector_dimension("vnl_vector_ssd", v1.size(), v2.size());
00533 #endif
00534 return vnl_c_vector<T>::euclid_dist_sq(v1.begin(), v2.begin(), v1.size());
00535 }
00536
00537
00538
00539
00540
00541 export template <class T> vcl_ostream& operator<<(vcl_ostream &, vnl_vector<T> const&);
00542
00543
00544 export template <class T> vcl_istream& operator>>(vcl_istream &, vnl_vector<T> &);
00545
00546 #endif // vnl_vector_h_