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