2 #ifndef vnl_vector_hxx_ 3 #define vnl_vector_hxx_ 44 # include <vcl_msvc_warnings.h> 58 # define vnl_vector_construct_hack() 62 #define vnl_vector_alloc_blah(size) \ 64 this->num_elmts = (size); \ 65 this->data = (size) ? vnl_c_vector<T>::allocate_T(size) : 0; \ 69 #define vnl_vector_free_blah \ 72 vnl_c_vector<T>::deallocate(this->data, this->num_elmts); \ 96 std::fill_n( this->
data, len, value );
108 for (
size_t i = 0; i < len && n; i++, n--)
109 this->
data[i] = values[i];
121 std::copy(
v.data,
v.data +
v.num_elmts, this->data );
133 std::copy( datablck, datablck + len, this->
data );
143 #if VNL_CONFIG_CHECK_BOUNDS && (!defined NDEBUG) 144 if (u.
size() !=
v.size())
147 for (
size_t i=0; i<num_elmts; ++i)
148 data[i] = u[i] +
v[i];
156 #if VNL_CONFIG_CHECK_BOUNDS && (!defined NDEBUG) 157 if (u.
size() !=
v.size())
160 for (
size_t i=0; i<num_elmts; ++i)
161 data[i] = u[i] -
v[i];
169 for (
size_t i=0; i<num_elmts; ++i)
178 for (
size_t i=0; i<num_elmts; ++i)
187 for (
size_t i=0; i<num_elmts; ++i)
196 for (
size_t i=0; i<num_elmts; ++i)
207 if (M.
cols() !=
v.size())
219 if (
v.size() != M.
rows())
254 if (this->num_elmts == n)
267 #undef vnl_vector_alloc_blah 268 #undef vnl_vector_free_blah 278 bool size_known = (this->
size() != 0);
280 for (
size_t i = 0; i < this->
size(); ++i) {
281 if ( ! (s >> (*
this)(i)) ) {
289 std::vector<T> allvals;
292 while ( s >> value ) {
293 allvals.push_back(value);
297 for (
size_t i = 0; i < n; ++i)
298 (*
this)[i] = allvals[i];
318 std::fill_n( this->
data, this->num_elmts, value );
329 std::copy( ptr, ptr + this->num_elmts, this->
data );
338 std::copy( this->
data, this->
data + this->num_elmts, ptr );
351 std::copy( rhs.
data, rhs.
data + this->num_elmts, this->data );
366 for (
size_t i = 0; i < this->num_elmts; i++)
367 this->
data[i] += value;
376 for (
size_t i = 0; i < this->num_elmts; i++)
377 this->
data[i] *= value;
386 for (
size_t i = 0; i < this->num_elmts; i++)
387 this->
data[i] /= value;
401 for (
size_t i = 0; i < this->num_elmts; i++)
416 for (
size_t i = 0; i < this->num_elmts; i++)
428 if (
m.columns() != this->num_elmts)
432 for (
size_t i = 0; i <
m.rows(); i++) {
434 for (
size_t k = 0; k < this->num_elmts; k++)
435 temp[i] += (
m.get(i,k) * this->
data[k]);
438 num_elmts =
m.rows();
450 if (this->num_elmts !=
m.rows())
454 for (
size_t i = 0; i <
m.columns(); i++) {
456 for (
size_t k = 0; k < this->num_elmts; k++)
457 temp[i] += (this->
data[k] *
m.get(k,i));
460 num_elmts =
m.columns();
472 for (
size_t i = 0; i < this->num_elmts; i++)
473 result.
data[i] = - this->data[i];
482 size_t stop = start +
v.size();
484 if ( stop > this->num_elmts)
488 for (
size_t i = start; i < stop; i++)
489 this->
data[i] =
v.data[i-start];
500 size_t stop = start + len;
501 if (this->num_elmts < stop)
506 for (
size_t i = 0; i < len; i++)
538 for (
size_t i = 0; i < v1.
size(); i++)
539 result[i] = v1[i] / v2[i];
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];
613 for (
size_t i = 0; i < out.rows(); i++)
614 for (
size_t j = 0; j < out.columns(); j++)
615 out[i][j] = v1[i] * v2[j];
626 for (
size_t i=0;i<num_elmts/2;++i) {
629 data[num_elmts-1-i]=tmp;
639 #if VNL_CONFIG_CHECK_BOUNDS && (!defined NDEBUG) 640 assert (!(b > this->num_elmts || e > this->num_elmts || b > e));
643 for (
size_t i=b;i<(e-b)/2+b;++i) {
645 const size_t endIndex = e - 1 - (i-b);
658 const size_t wrapped_shift = shift % this->num_elmts;
659 if (0 == wrapped_shift)
661 for (
size_t i = 0; i < this->num_elmts; ++i)
663 v[(i + wrapped_shift)%this->num_elmts] = this->
data_block()[i];
672 const size_t wrapped_shift = shift % this->num_elmts;
673 if (0 == wrapped_shift)
675 return this->flip().
flip(0,wrapped_shift).flip(wrapped_shift,this->num_elmts);
689 #if defined(_MSC_VER) 690 # pragma warning (push) 691 # pragma warning (disable: 4244) 704 real_t a_b = static_cast<real_t>(
709 #if defined(_MSC_VER) 710 # pragma warning (pop) 720 const abs_r c = abs_r(
cos_angle(a, b) );
722 if (c >= 1.0)
return 0;
723 if (c <= -1.0)
return vnl_math::pi;
724 return std::acos( c );
730 for (
size_t i = 0; i < this->
size();++i)
741 for (
size_t i = 0; i < this->
size();++i)
742 if ( !( (*
this)[i] == zero) )
754 std::cerr << __FILE__
": *** NAN FEVER **\n" << *
this;
761 if (this->
size() != sz) {
762 std::cerr << __FILE__
": Size is " << this->
size() <<
". Should be " << sz <<
'\n';
775 for (
size_t i = 0; i <
size(); i++)
790 for (
size_t i = 0; i <
size(); i++)
791 if (!(this->
data[i] == rhs.
data[i]))
804 for (
size_t i = 0; i+1 <
v.size(); ++i)
806 if (
v.size() > 0) s <<
v[
v.size()-1];
824 #define VNL_VECTOR_INSTANTIATE_COMMON(T) \ 825 template class VNL_EXPORT vnl_vector<T >; \ 830 template VNL_EXPORT vnl_vector<T > operator*(vnl_matrix<T > const &, vnl_vector<T > const &); \ 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 &); \ 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 &); \ 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 > &) 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 &) 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 &) 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.
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.
vnl_vector< T > & operator=(T const &v)
Set all elements to value v.
vnl_vector< T > & operator-=(T value)
Subtract scalar value from all elements.
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).
An ordinary mathematical matrix.
vnl_vector< T > roll(const int &shift) const
Roll the vector forward by the specified shift.
bool set_size(size_t n)
Resize to n elements.
Templated zero/one/precision.
#define vnl_vector_construct_hack()
Support for Streaming SIMD Extensions to speed up vector arithmetic.
vnl_vector< T > operator-() const
Unary minus operator.
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.
vnl_vector< T > & pre_multiply(vnl_matrix< T > const &M)
this = M(*this) where M is a suitable matrix.
vnl_vector< T > & operator *=(T)
Multiply all elements by scalar.
vnl_vector< T > & operator+=(T)
Add scalar value to all elements.
void destroy()
Frees up the array inside vector. O(1).
Namespace with standard math functions.
iterator begin()
Iterator pointing to start of data.
bool read_ascii(std::istream &s)
Read from text stream.
bool operator_eq(vnl_vector< T > const &v) const
Return true if *this == v.
abs_t squared_magnitude() const
Return sum of squares of elements.
std::ostream & operator<<(std::ostream &s, vnl_decnum const &r)
decimal output.
static void apply(T const *, unsigned, T(*f)(T), T *v_out)
vnl_vector< T > & flip()
Reverse the order of the elements.
bool is_equal(vnl_vector< T > const &rhs, double tol) const
Return true if all elements of vectors are equal, within given tolerance.
void vnl_error_vector_dimension(char const *fcn, int l1, int l2)
Raise exception for invalid dimension.
vnl_vector()
Creates an empty vector. O(1).
#define vnl_vector_alloc_blah(size)
#define vnl_vector_free_blah
vnl_vector< T > & operator/=(T)
Divide all elements by scalar.
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).
T cos_angle(vnl_vector< T > const &a, vnl_vector< T > const &b)
iterator begin()
Iterator pointing to start of data.
bool is_finite() const
Return true if it's finite.
vnl_vector< T > extract(size_t len, size_t start=0) const
Returns a subvector specified by the start index and length. O(n).
static T * allocate_T(const std::size_t n)
An ordinary mathematical matrix.
static VNL_SSE_FORCE_INLINE void element_product(const T *x, const T *y, T *r, unsigned n)
void clear()
Make the vector as if it had been default-constructed.
void clear()
Make the matrix as if it had been default-constructed.
T inner_product(vnl_vector< T > const &v1, vnl_vector< T > const &v2)
Hermitian inner product. O(n).
void swap(vnl_vector< T > &that)
Set this to that and that to this.
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.
vnl_vector & fill(T const &v)
Set all values to v.
bool isfinite(vnl_bignum const &x)
T dot_product(const vnl_vector_fixed< T, n > &a, const vnl_vector_fixed< T, n > &b)
vnl_vector & copy_in(T const *ptr)
Sets elements to ptr[i].
Mathematical vector class, templated by type of element.
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).
static VNL_SSE_FORCE_INLINE void matrix_x_vector(const T *m, const T *v, T *r, unsigned rows, unsigned cols)
static vnl_vector< T > read(std::istream &s)
Read from text stream.
vnl_bignum abs(vnl_bignum const &x)
unsigned int size() const
Return the total number of elements stored by the matrix.
void destroy()
Delete data.
unsigned int rows() const
Return the number of rows.
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.
vnl_vector< T > apply(T(*f)(T)) const
Applies function to elements.
void assert_finite_internal() const
bool is_finite() const
Return true if finite.
void copy_out(T *) const
Copy elements to ptr[i].
bool set_size(unsigned r, unsigned c)
Resize to r rows by c columns. Old data lost.
static T dot_product(T const *, T const *, unsigned)
vnl_vector & roll_inplace(const int &shift)
Roll the vector forward by the specified shift.
unsigned int columns() const
Return the number of columns.
static VNL_SSE_FORCE_INLINE void vector_x_matrix(const T *v, const T *m, T *r, unsigned rows, unsigned cols)
Math on blocks of memory.
void assert_size_internal(size_t sz) const
bool is_zero() const
Return true iff all the entries are zero.