core/vnl/vnl_c_vector.h
Go to the documentation of this file.
00001 // This is core/vnl/vnl_c_vector.h
00002 #ifndef vnl_c_vector_h_
00003 #define vnl_c_vector_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Math on blocks of memory
00010 //
00011 //    vnl_c_vector interfaces to low-level memory-block operations.
00012 //
00013 // \author Andrew W. Fitzgibbon, Oxford RRG
00014 // \date   12 Feb 1998
00015 //
00016 // \verbatim
00017 //  Modifications
00018 //   1998-02-12 AWF              Initial version.
00019 //   2001-03-26 LSB (Manchester) Tidied documentation
00020 //   2009-03-30 Peter Vanroose   added arg_min() and arg_max()
00021 // \endverbatim
00022 //
00023 //-----------------------------------------------------------------------------
00024 
00025 #include <vcl_iosfwd.h>
00026 #include <vnl/vnl_numeric_traits.h>
00027 #include <vcl_cstddef.h> // for vcl_size_t
00028 #include <vcl_cmath.h> // for vcl_sqrt
00029 
00030 // avoid messing about with aux_* functions for gcc 2.7 -- fsm
00031 template <class T, class S> void vnl_c_vector_one_norm(T const *p, unsigned n, S *out);
00032 template <class T, class S> void vnl_c_vector_two_norm(T const *p, unsigned n, S *out);
00033 template <class T, class S> void vnl_c_vector_inf_norm(T const *p, unsigned n, S *out);
00034 template <class T, class S> void vnl_c_vector_two_norm_squared(T const *p, unsigned n, S *out);
00035 template <class T, class S> void vnl_c_vector_rms_norm(T const *p, unsigned n, S *out);
00036 
00037 //: vnl_c_vector interfaces to lowlevel memory-block operations.
00038 export template <class T>
00039 class vnl_c_vector
00040 {
00041  public:
00042   typedef typename vnl_numeric_traits<T>::abs_t abs_t;
00043   typedef typename vnl_numeric_traits<T>::real_t real_t;
00044 
00045   static T sum(T const* v, unsigned n);
00046   static inline abs_t squared_magnitude(T const *p, unsigned n)
00047   { abs_t val; vnl_c_vector_two_norm_squared(p, n, &val); return val; }
00048   static void normalize(T *, unsigned n);
00049   static void apply(T const *, unsigned, T (*f)(T), T* v_out);
00050   static void apply(T const *, unsigned, T (*f)(T const&), T* v_out);
00051 
00052   //: y[i]  = x[i]
00053   static void copy    (T const *x, T       *y, unsigned);
00054 
00055   //:  y[i]  = a*x[i]
00056   static void scale   (T const *x, T       *y, unsigned, T const &);
00057 
00058   //: z[i]  = x[i] + y[i];
00059   static void add     (T const *x, T const *y, T *z, unsigned);
00060 
00061   //: z[i]  = x[i] + y;
00062   static void add     (T const *x, T const& y, T *z, unsigned);
00063 
00064   //: z[i]  = x[i] - y[i]
00065   static void subtract(T const *x, T const *y, T *z, unsigned);
00066 
00067   //: z[i]  = x[i] - y[i]
00068   static void subtract(T const *x, T const& y, T *z, unsigned);
00069 
00070   //: z[i]  = x[i] * y[i]
00071   static void multiply(T const *x, T const *y, T *z, unsigned);
00072 
00073   //: z[i]  = x[i] * y[i]
00074   static void multiply(T const *x, T const& y, T *z, unsigned);
00075 
00076   //: z[i]  = x[i] / y[i]
00077   static void divide  (T const *x, T const *y, T *z, unsigned);
00078 
00079   //: z[i]  = x[i] / y[i]
00080   static void divide  (T const *x, T const& y, T *z, unsigned);
00081 
00082   //: y[i]  = -x[i]
00083   // Note that this is a no-op when T is an unsigned type.
00084   static void negate  (T const *x, T       *y, unsigned);
00085 
00086   //: y[i]  = 1/x[i]
00087   static void invert  (T const *x, T       *y, unsigned);
00088 
00089   //:  y[i] += a*x[i]
00090   static void saxpy   (T const &a, T const *x, T *y, unsigned);
00091 
00092   //: x[i]  = v
00093   static void fill    (T *x, unsigned, T const &v);
00094 
00095 
00096   static void reverse (T *x, unsigned);
00097   static T dot_product  (T const *, T const *, unsigned);
00098 
00099   //: conjugate second
00100   static T inner_product(T const *, T const *, unsigned);
00101   static void conjugate(T const *, T *, unsigned);
00102 
00103   static T max_value(T const *, unsigned);
00104   static T min_value(T const *, unsigned);
00105   static unsigned arg_max(T const *, unsigned);
00106   static unsigned arg_min(T const *, unsigned);
00107 
00108   static T mean(T const *p, unsigned n) { return T(sum(p,n)/abs_t(n)); }
00109 
00110   //: The standard deviation
00111   // This method uses the 1/(n-1) normalisation, assuming that your
00112   // data is a sample of a population.
00113   static inline real_t std(T const *p, unsigned n) {
00114     return vcl_sqrt(real_t(sum_sq_diff_means(p, n))/real_t(abs_t(n-1)));}
00115 
00116   //: The sum of squared differences from the mean
00117   static T sum_sq_diff_means(T const* v, unsigned n);
00118 
00119   //:  one_norm : sum of abs values
00120   static inline abs_t one_norm(T const *p, unsigned n)
00121   { abs_t val; vnl_c_vector_one_norm(p, n, &val); return val; }
00122 
00123   //: two_norm : sqrt of sum of squared abs values
00124   static inline abs_t two_norm(T const *p, unsigned n)
00125   { abs_t val; vnl_c_vector_two_norm(p, n, &val); return val; }
00126 
00127  //: inf_norm : max of abs values
00128   static inline abs_t inf_norm(T const *p, unsigned n)
00129   { abs_t val; vnl_c_vector_inf_norm(p, n, &val); return val; }
00130 
00131   //: two_nrm2 : sum of squared abs values
00132   static inline abs_t two_nrm2(T const *p, unsigned n)
00133   { abs_t val; vnl_c_vector_two_norm_squared(p, n, &val); return val; }
00134 
00135   //: rms_norm : sqrt of mean sum of squared abs values
00136   static inline abs_t rms_norm(T const *p, unsigned n)
00137   { abs_t val; vnl_c_vector_rms_norm(p, n, &val); return val; }
00138 
00139   //: Euclidean Distance between two vectors.
00140   // Sum of Differences squared.
00141   static T euclid_dist_sq(T const *, T const *, unsigned);
00142 
00143   //: Memory allocation
00144   static T** allocate_Tptr(vcl_size_t n);
00145   static T*  allocate_T(vcl_size_t n);
00146   static void deallocate(T**, vcl_size_t n_when_allocated);
00147   static void deallocate(T*, vcl_size_t n_when_allocated);
00148 };
00149 
00150 //: Input & output
00151 // \relatesalso vnl_c_vector
00152 template <class T>
00153 vcl_ostream& print_vector(vcl_ostream&, T const*, unsigned);
00154 
00155 #endif // vnl_c_vector_h_