00001 #ifndef vnl_vector_h_
00002 #define vnl_vector_h_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <vcl_iosfwd.h>
00014 #include <vcl_string.h>
00015 #include <vnl/vnl_tag.h>
00016 #include <vnl/vnl_error.h>
00017 #include <vnl/vnl_c_vector.h>
00018
00019 export template <class T> class vnl_vector;
00020 export template <class T> class vnl_matrix;
00021
00022
00023
00024 #define v vnl_vector<T>
00025 #define m vnl_matrix<T>
00026 template <class T> T dot_product (v const&, v const&);
00027 template <class T> T inner_product (v const&, v const&);
00028 template <class T> T bracket (v const &, m const &, v const &);
00029 template <class T> T cos_angle(v const&, v const& );
00030 template <class T> double angle (v const&, v const&);
00031 template <class T> m outer_product (v const&, v const&);
00032 template <class T> v operator+(T, v const&);
00033 template <class T> v operator-(T, v const&);
00034 template <class T> v operator*(T, v const&);
00035 template <class T> v operator*(m const&, v const&);
00036 template <class T> v element_product(v const&,v const&);
00037 template <class T> v element_quotient(v const&,v const&);
00038 template <class T> T cross_2d (v const&, v const&);
00039 template <class T> v cross_3d (v const&, v const&);
00040 #undef v
00041 #undef m
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 template<class T>
00055 class vnl_vector {
00056 public:
00057 friend class vnl_matrix<T>;
00058
00059
00060 vnl_vector () : num_elmts(0) , data(0) {}
00061
00062
00063
00064 vnl_vector (unsigned len);
00065
00066
00067 vnl_vector (unsigned len, T const& v0);
00068
00069
00070 vnl_vector (unsigned len, int n, T const values[]);
00071
00072
00073 vnl_vector (T const&, T const&, T const&);
00074
00075
00076 vnl_vector (T const* data_block,unsigned int n);
00077
00078
00079 vnl_vector (vnl_vector<T> const&);
00080
00081
00082
00083 vnl_vector (vnl_vector<T> &that, vnl_tag_grab)
00084 : num_elmts(that.num_elmts), data(that.data)
00085 { that.num_elmts=0; that.data=0; }
00086
00087 #ifndef VXL_DOXYGEN_SHOULD_SKIP_THIS
00088
00089
00090
00091 vnl_vector (vnl_vector<T> const &, vnl_vector<T> const &, const vnl_tag_add &);
00092 vnl_vector (vnl_vector<T> const &, vnl_vector<T> const &, const vnl_tag_sub &);
00093 vnl_vector (vnl_vector<T> const &, T, const vnl_tag_mul &);
00094 vnl_vector (vnl_vector<T> const &, T, const vnl_tag_div &);
00095 vnl_vector (vnl_vector<T> const &, T, const vnl_tag_add &);
00096 vnl_vector (vnl_vector<T> const &, T, const vnl_tag_sub &);
00097 vnl_vector (vnl_matrix<T> const &, vnl_vector<T> const &, const vnl_tag_mul &);
00098 vnl_vector (vnl_vector<T> const &, vnl_matrix<T> const &, const vnl_tag_mul &);
00099
00100 #endif
00101
00102
00103 ~vnl_vector() { if (data) destroy(); }
00104
00105
00106 unsigned size() const { return num_elmts; }
00107
00108
00109 inline void put (unsigned int i, T const&);
00110
00111
00112 inline T get (unsigned int i) const;
00113
00114
00115 void fill (T const& v);
00116
00117
00118
00119 void copy_in(T const * ptr);
00120
00121
00122
00123 void copy_out(T *) const;
00124
00125
00126
00127
00128 void set (T const *ptr) { copy_in(ptr); }
00129
00130
00131 T & operator() (unsigned int i) { return data[i]; }
00132
00133 T const & operator() (unsigned int i) const { return data[i]; }
00134
00135
00136 T & operator[] (unsigned int i) { return data[i]; }
00137
00138 T const & operator[] (unsigned int i) const { return data[i]; }
00139
00140
00141 vnl_vector<T>& operator= (T const&v) { fill(v); return *this; }
00142
00143
00144 vnl_vector<T>& operator= (vnl_vector<T> const& rhs);
00145
00146
00147 vnl_vector<T>& operator+= (T );
00148
00149
00150 vnl_vector<T>& operator-= (T value) { return *this += (-value); }
00151
00152
00153 vnl_vector<T>& operator*= (T );
00154
00155
00156 vnl_vector<T>& operator/= (T );
00157
00158
00159 vnl_vector<T>& operator+= (vnl_vector<T> const& rhs);
00160
00161
00162 vnl_vector<T>& operator-= (vnl_vector<T> const& rhs);
00163
00164
00165
00166 vnl_vector<T>& pre_multiply (vnl_matrix<T> const& M);
00167
00168
00169
00170 vnl_vector<T>& post_multiply (vnl_matrix<T> const& M);
00171
00172
00173
00174 vnl_vector<T>& operator*= (vnl_matrix<T> const& m) { return this->post_multiply(m); }
00175
00176
00177
00178
00179 vnl_vector<T> operator+ () const { return *this; }
00180
00181
00182
00183 vnl_vector<T> operator- () const;
00184
00185
00186 vnl_vector<T> operator+ (T v) const { return vnl_vector<T>(*this, v, vnl_tag_add()); }
00187 vnl_vector<T> operator- (T v) const { return vnl_vector<T>(*this, v, vnl_tag_sub()); }
00188 vnl_vector<T> operator* (T v) const { return vnl_vector<T>(*this, v, vnl_tag_mul()); }
00189 vnl_vector<T> operator/ (T v) const { return vnl_vector<T>(*this, v, vnl_tag_div()); }
00190
00191 vnl_vector<T> operator+ (vnl_vector<T> const& v) const { return vnl_vector<T>(*this, v, vnl_tag_add()); }
00192 vnl_vector<T> operator- (vnl_vector<T> const& v) const { return vnl_vector<T>(*this, v, vnl_tag_sub()); }
00193 vnl_vector<T> operator* (vnl_matrix<T> const& M) const { return vnl_vector<T>(*this, M, vnl_tag_mul()); }
00194
00195
00196
00197
00198
00199 T const* data_block () const { return data; }
00200
00201
00202
00203 T * data_block () { return data; }
00204
00205
00206 typedef T element_type;
00207
00208 typedef T *iterator;
00209
00210 iterator begin() { return data; }
00211
00212
00213 iterator end() { return data+num_elmts; }
00214
00215
00216 typedef T const *const_iterator;
00217
00218 const_iterator begin() const { return data; }
00219
00220 const_iterator end() const { return data+num_elmts; }
00221
00222
00223 vnl_vector<T> apply(T (*f)(T)) const;
00224
00225 vnl_vector<T> apply(T (*f)(T const&)) const;
00226
00227
00228 vnl_vector<T> extract (unsigned int len, unsigned int start=0) const;
00229
00230
00231 vnl_vector<T>& update (vnl_vector<T> const&, unsigned int start=0);
00232
00233
00234 typedef typename vnl_c_vector<T>::abs_t abs_t;
00235
00236
00237 abs_t squared_magnitude() const { return vnl_c_vector<T>::two_nrm2(begin(), size()); }
00238
00239
00240 abs_t magnitude() const { return two_norm(); }
00241
00242
00243 abs_t one_norm() const { return vnl_c_vector<T>::one_norm(begin(), size()); }
00244
00245
00246 abs_t two_norm() const { return vnl_c_vector<T>::two_norm(begin(), size()); }
00247
00248
00249 abs_t inf_norm() const { return vnl_c_vector<T>::inf_norm(begin(), size()); }
00250
00251
00252 abs_t rms () const { return vnl_c_vector<T>::rms_norm(begin(), size()); }
00253
00254
00255 T min_value () const { return vnl_c_vector<T>::min_value(begin(), size()); }
00256
00257
00258 T max_value () const { return vnl_c_vector<T>::max_value(begin(), size()); }
00259
00260
00261 T mean() const { return vnl_c_vector<T>::mean(begin(), size()); }
00262
00263
00264 vnl_vector<T>& normalize() { vnl_c_vector<T>::normalize(begin(), size()); return *this; }
00265
00266
00267
00268 void flip();
00269
00270
00271 void swap(vnl_vector<T> & that);
00272
00273
00274 T& x() const { return data[0]; }
00275
00276 T& y() const { return data[1]; }
00277
00278 T& z() const { return data[2]; }
00279
00280 T& t() const { return data[3]; }
00281
00282 #ifndef VXL_I_dont_want_crazy_methods_in_my_classes
00283
00284 void set_x(T const&xx) { if (size() >= 1) data[0] = xx; }
00285
00286 void set_y(T const&yy) { if (size() >= 2) data[1] = yy; }
00287
00288 void set_z(T const&zz) { if (size() >= 3) data[2] = zz; }
00289
00290 void set_t(T const&tt) { if (size() >= 4) data[3] = tt; }
00291 #endif
00292
00293
00294 void assert_size(unsigned sz) const;
00295
00296 void assert_finite() const;
00297
00298
00299 bool is_finite() const;
00300
00301
00302 bool operator_eq (vnl_vector<T> const& v) const;
00303
00304
00305 bool operator==(vnl_vector<T> const &that) const { return this->operator_eq(that); }
00306
00307
00308 bool operator!=(vnl_vector<T> const &that) const { return !this->operator_eq(that); }
00309
00310
00311
00312
00313 bool resize (unsigned n);
00314
00315
00316 void clear();
00317
00318
00319
00320 bool read_ascii(vcl_istream& s);
00321
00322
00323 static vnl_vector<T> read(vcl_istream& s);
00324
00325
00326 protected:
00327 unsigned num_elmts;
00328 T* data;
00329
00330 void destroy();
00331
00332 #if VCL_NEED_FRIEND_FOR_TEMPLATE_OVERLOAD
00333 # define v vnl_vector<T>
00334 # define m vnl_matrix<T>
00335 friend T dot_product VCL_NULL_TMPL_ARGS (v const&, v const&);
00336 friend T inner_product VCL_NULL_TMPL_ARGS (v const&, v const&);
00337 friend T bracket VCL_NULL_TMPL_ARGS (v const&, m const&, v const&);
00338 friend T cos_angle VCL_NULL_TMPL_ARGS (v const&, v const&);
00339 friend double angle VCL_NULL_TMPL_ARGS (v const&, v const&);
00340 friend m outer_product VCL_NULL_TMPL_ARGS (v const&, v const&);
00341 friend v operator+ VCL_NULL_TMPL_ARGS (T const, v const&);
00342 friend v operator- VCL_NULL_TMPL_ARGS (T const, v const&);
00343 friend v operator* VCL_NULL_TMPL_ARGS (T const, v const&);
00344 friend v operator* VCL_NULL_TMPL_ARGS (m const&, v const&);
00345 friend v element_product VCL_NULL_TMPL_ARGS (v const&, v const&);
00346 friend v element_quotient VCL_NULL_TMPL_ARGS (v const&, v const&);
00347 friend T cross_2d VCL_NULL_TMPL_ARGS (v const&, v const&);
00348 friend v cross_3d VCL_NULL_TMPL_ARGS (v const&, v const&);
00349 # undef v
00350 # undef m
00351 #endif
00352
00353
00354 static void inline_function_tickler();
00355 };
00356
00357
00358
00359
00360
00361
00362
00363
00364 template <class T>
00365 inline T vnl_vector<T>::get (unsigned int index) const {
00366 #if ERROR_CHECKING
00367 if (index >= this->num_elmts)
00368 vnl_error_vector_index ("get", index);
00369 #endif
00370 return this->data[index];
00371 }
00372
00373
00374
00375
00376 template <class T>
00377 inline void vnl_vector<T>::put (unsigned int index, T const& value) {
00378 #if ERROR_CHECKING
00379 if (index >= this->num_elmts)
00380 vnl_error_vector_index ("put", index);
00381 #endif
00382 this->data[index] = value;
00383 }
00384
00385
00386 template<class T>
00387 inline vnl_vector<T> operator* (vnl_matrix<T> const& m, vnl_vector<T> const& v) {
00388 return vnl_vector<T>(m, v, vnl_tag_mul());
00389 }
00390
00391
00392 template<class T>
00393 inline vnl_vector<T> operator+ (T s, vnl_vector<T> const& v) {
00394 return vnl_vector<T>(v, s, vnl_tag_add());
00395 }
00396
00397
00398 template<class T>
00399 inline vnl_vector<T> operator- (T s, vnl_vector<T> const& v) {
00400 return vnl_vector<T>(-v, s, vnl_tag_add());
00401 }
00402
00403
00404 template<class T>
00405 inline vnl_vector<T> operator* (T s, vnl_vector<T> const& v) {
00406 return vnl_vector<T>(v, s, vnl_tag_mul());
00407 }
00408
00409 template<class T>
00410 inline void swap(vnl_vector<T> &a, vnl_vector<T> &b) { a.swap(b); }
00411
00412
00413 export template <class T> vcl_ostream& operator<< (vcl_ostream &, vnl_vector<T> const&);
00414 export template <class T> vcl_istream& operator>> (vcl_istream &, vnl_vector<T> &);
00415
00416
00417 #endif // vnl_vector_h_