⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vnl_vector.h

📁 InsightToolkit-1.4.0(有大量的优化算法程序)
💻 H
📖 第 1 页 / 共 2 页
字号:

  //: Return a reference to this.
  // Useful in code which would prefer not to know if its argument
  // is a vector, vector_ref or a vector_fixed.  Note that it doesn't
  // return a vector_ref, so it's only useful in templates or macros.
  vnl_vector<T> const& as_ref() const { return *this; }

  //: Return a reference to this.
  vnl_vector<T>&       as_ref()       { return *this; }

  //: Applies function to elements
  vnl_vector<T> apply(T (*f)(T)) const;
  //: Applies function to elements
  vnl_vector<T> apply(T (*f)(T const&)) const;

  //: Returns a subvector specified by the start index and length. O(n).
  vnl_vector<T> extract (unsigned int len, unsigned int start=0) const;

  //: Replaces elements with index begining at start, by values of v. O(n).
  vnl_vector<T>& update (vnl_vector<T> const&, unsigned int start=0);

  // norms etc
  typedef typename vnl_c_vector<T>::abs_t abs_t;

  //: Return sum of squares of elements
  abs_t squared_magnitude() const { return vnl_c_vector<T>::two_nrm2(begin(), size()); }

  //: Return magnitude (length) of vector
  abs_t magnitude() const { return two_norm(); }

  //: Return sum of absolute values of the elements
  abs_t one_norm() const { return vnl_c_vector<T>::one_norm(begin(), size()); }

  //: Return sqrt of sum of squares of values of elements
  abs_t two_norm() const { return vnl_c_vector<T>::two_norm(begin(), size()); }

  //: Return largest absolute element value
  abs_t inf_norm() const { return vnl_c_vector<T>::inf_norm(begin(), size()); }

  //: Normalise by dividing through by the magnitude
  vnl_vector<T>& normalize() { vnl_c_vector<T>::normalize(begin(), size()); return *this; }

  // These next 6 functions are should really be helper functions since they aren't
  // really proper functions on a vector in a philosophial sense.

  //: Root Mean Squares of values
  abs_t rms     () const { return vnl_c_vector<T>::rms_norm(begin(), size()); }

  //: Smallest value
  T min_value () const { return vnl_c_vector<T>::min_value(begin(), size()); }

  //: Largest value
  T max_value () const { return vnl_c_vector<T>::max_value(begin(), size()); }

  //: Mean of values in vector
  T mean() const { return vnl_c_vector<T>::mean(begin(), size()); }

  //: Sum of values in a vector 
  T sum() const { return vnl_c_vector<T>::sum(begin(), size()); }

  //: Reverse the order of the elements
  //  Element i swaps with element size()-1-i
  void flip();

  //: Set this to that and that to this
  void swap(vnl_vector<T> & that);

  //: Return first element of vector
  T& x() const { return data[0]; }
  //: Return second element of vector
  T& y() const { return data[1]; }
  //: Return third element of vector
  T& z() const { return data[2]; }
  //: Return fourth element of vector
  T& t() const { return data[3]; }

#if VNL_CONFIG_LEGACY_METHODS
  //: Set the first element (with bound checking)
  void set_x(T const&xx) { if (size() >= 1) data[0] = xx; }
  //: Set the second element (with bound checking)
  void set_y(T const&yy) { if (size() >= 2) data[1] = yy; }
  //: Set the third element (with bound checking)
  void set_z(T const&zz) { if (size() >= 3) data[2] = zz; }
  //: Set the fourth element (with bound checking)
  void set_t(T const&tt) { if (size() >= 4) data[3] = tt; }
#endif

  //: Check that size()==sz if not, abort();
  // This function does or tests nothing if NDEBUG is defined
  void assert_size(unsigned sz) const {
#ifndef NDEBUG
    assert_size_internal(sz);
#endif
  }

  //: Check that this is finite if not, abort();
  // This function does or tests nothing if NDEBUG is defined
  void assert_finite() const {
#ifndef NDEBUG
    assert_finite_internal();
#endif
  }

  //: Return true if its finite
  bool is_finite() const;

  //: Return true iff all the entries are zero.
  bool is_zero() const;

  //: Return true iff the size is zero.
  bool empty() const { return !data || !num_elmts; }

  //: Return true if *this == v
  bool operator_eq (vnl_vector<T> const& v) const;

  //: Equality test
  bool operator==(vnl_vector<T> const &that) const { return  this->operator_eq(that); }

  //: Inequality test
  bool operator!=(vnl_vector<T> const &that) const { return !this->operator_eq(that); }

  //:
  // \deprecated Use make_size.
  bool resize (unsigned n) { return make_size(n); };

  //: Resize to n elements.
  // This is a destructive resize, in that the old data is lost if size() != \a n before the call.
  // If size() is already \a n, this is a null operation.
  bool make_size (unsigned n);

  //: Make the vector as if it had been default-constructed.
  void clear();


  //: Read from text stream
  bool read_ascii(vcl_istream& s);

  //: Read from text stream
  static vnl_vector<T> read(vcl_istream& s);


 protected:
  unsigned num_elmts;           // Number of elements
  T* data;                      // Pointer to the vnl_vector

  void assert_size_internal(unsigned sz) const;
  void assert_finite_internal() const;

  void destroy();

#if VCL_NEED_FRIEND_FOR_TEMPLATE_OVERLOAD
# define v vnl_vector<T>
# define m vnl_matrix<T>
  friend T      dot_product      VCL_NULL_TMPL_ARGS (v const&, v const&);
  friend T      inner_product    VCL_NULL_TMPL_ARGS (v const&, v const&);
  friend T      bracket          VCL_NULL_TMPL_ARGS (v const&, m const&, v const&);
  friend T      cos_angle        VCL_NULL_TMPL_ARGS (v const&, v const&);
  friend double angle            VCL_NULL_TMPL_ARGS (v const&, v const&);
  friend m      outer_product    VCL_NULL_TMPL_ARGS (v const&, v const&);
  friend v      operator+        VCL_NULL_TMPL_ARGS (T const,  v const&);
  friend v      operator-        VCL_NULL_TMPL_ARGS (T const,  v const&);
  friend v      operator*        VCL_NULL_TMPL_ARGS (T const,  v const&);
  friend v      operator*        VCL_NULL_TMPL_ARGS (m const&, v const&);
  friend v      element_product  VCL_NULL_TMPL_ARGS (v const&, v const&);
  friend v      element_quotient VCL_NULL_TMPL_ARGS (v const&, v const&);
  friend T      cross_2d         VCL_NULL_TMPL_ARGS (v const&, v const&);
  friend v      cross_3d         VCL_NULL_TMPL_ARGS (v const&, v const&);
# undef v
# undef m
#endif

  // inline function template instantiation hack for gcc 2.97 -- fsm
  static void inline_function_tickler();
};


// Definitions of inline functions


//: Gets the element at specified index and return its value. O(1).
// Range check is performed.

template <class T>
inline T vnl_vector<T>::get (unsigned int index) const {
#if ERROR_CHECKING
  if (index >= this->num_elmts)     // If invalid index specified
    vnl_error_vector_index ("get", index);  // Raise exception
#endif
  return this->data[index];
}

//: Puts the value at specified index. O(1).
// Range check is performed.

template <class T>
inline void vnl_vector<T>::put (unsigned int index, T const& value) {
#if ERROR_CHECKING
  if (index >= this->num_elmts)     // If invalid index specified
    vnl_error_vector_index ("put", index); // Raise exception
#endif
  this->data[index] = value;    // Assign data value
}

//: multiply matrix and (column) vector. O(m*n).
// \relates vnl_vector
// \relates vnl_matrix
template<class T>
inline vnl_vector<T> operator* (vnl_matrix<T> const& m, vnl_vector<T> const& v) {
  return vnl_vector<T>(m, v, vnl_tag_mul());
}

//: add scalar and vector. O(n).
// \relates vnl_vector
template<class T>
inline vnl_vector<T> operator+ (T s, vnl_vector<T> const& v) {
  return vnl_vector<T>(v, s, vnl_tag_add());
}

//: subtract vector from scalar. O(n).
// \relates vnl_vector
template<class T>
inline vnl_vector<T> operator- (T s, vnl_vector<T> const& v) {
  return vnl_vector<T>(-v, s, vnl_tag_add());
}

//: multiply scalar and vector. O(n).
// \relates vnl_vector
template<class T>
inline vnl_vector<T> operator* (T s, vnl_vector<T> const& v) {
  return vnl_vector<T>(v, s, vnl_tag_mul());
}

//: Interchange the two vectors
// \relates vnl_vector
template<class T>
inline void swap(vnl_vector<T> &a, vnl_vector<T> &b) { a.swap(b); }

//: Euclidean Distance between two vectors.
// Sum of Differences squared.
// \relates vnl_vector
template<class T>
inline T vnl_vector_ssd (vnl_vector<T> const& v1, vnl_vector<T> const& v2)
{
#ifndef NDEBUG
  if (v1.size() != v2.size())
    vnl_error_vector_dimension ("vnl_vector_ssd", v1.size(), v2.size());
#endif
  return vnl_c_vector<T>::euclid_dist_sq(v1.begin(), v2.begin(), v1.size());
}


// Non-vector Functions which are nevertheless very useful.


//: Write vector to a vcl_ostream
// \relates vnl_vector
export template <class T> vcl_ostream& operator<< (vcl_ostream &, vnl_vector<T> const&);
//: Read vector from a vcl_istream
// \relates vnl_vector
export template <class T> vcl_istream& operator>> (vcl_istream &, vnl_vector<T>      &);

#endif // vnl_vector_h_

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -