core/vnl/vnl_block.h
Go to the documentation of this file.
00001 // This is core/vnl/vnl_block.h
00002 #ifndef vnl_block_h_
00003 #define vnl_block_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author fsm
00010 //
00011 // \verbatim
00012 //  Modifications
00013 //   2009-03-30 Peter Vanroose - Added arg_min() & arg_max() and reimplemented min_value() & max_value()
00014 // \endverbatim
00015 //
00016 #include <vcl_compiler.h>
00017 
00018 void vnl_block_raise_exception(char const *FILE, int LINE, char const *why);
00019 
00020 //: return sum of elements
00021 template <class T> inline
00022 T vnl_block_sum(T const x[], unsigned n)
00023 {
00024   if (n == 0)
00025     return T(0);
00026   else {
00027     T ans = x[0];
00028     for (unsigned i=1; i<n; ++i)
00029       ans += x[i];
00030     return ans;
00031   }
00032 }
00033 
00034 //: return product of elements
00035 template <class T> inline
00036 T vnl_block_product(T const x[], unsigned n)
00037 {
00038   if (n == 0)
00039     return T(1);
00040   else {
00041     T ans = x[0];
00042     for (unsigned i=1; i<n; ++i)
00043       ans *= x[i];
00044     return ans;
00045   }
00046 }
00047 
00048 //: return smallest value.
00049 template <class T> inline
00050 T vnl_block_min_value(T const *x, unsigned n)
00051 {
00052   if (n == 0)
00053     vnl_block_raise_exception(__FILE__, __LINE__, "n is 0");
00054   T ans = *x;
00055   while (--n > 0)
00056     if (ans > *++x)
00057       ans = *x;
00058   return ans;
00059 }
00060 
00061 //: return largest value.
00062 template <class T> inline
00063 T vnl_block_max_value(T const *x, unsigned n)
00064 {
00065   if (n == 0)
00066     vnl_block_raise_exception(__FILE__, __LINE__, "n is 0");
00067   T ans = *x;
00068   while (--n > 0)
00069     if (ans < *++x)
00070       ans = *x;
00071   return ans;
00072 }
00073 
00074 //: return index of smallest value.
00075 template <class T> inline
00076 unsigned vnl_block_arg_min(T const x[], unsigned n)
00077 {
00078   if (n == 0)
00079     vnl_block_raise_exception(__FILE__, __LINE__, "n is 0");
00080   T tmp = *x;
00081   unsigned idx = 0;
00082   for (unsigned i=1; i<n; ++i)
00083     if (tmp > *++x)
00084       tmp = *x, idx = i;
00085   return idx;
00086 }
00087 
00088 //: return index of largest value.
00089 template <class T> inline
00090 unsigned vnl_block_arg_max(T const x[], unsigned n)
00091 {
00092   if (n == 0)
00093     vnl_block_raise_exception(__FILE__, __LINE__, "n is 0");
00094   T tmp = *x;
00095   unsigned idx = 0;
00096   for (unsigned i=1; i<n; ++i)
00097     if (tmp < *++x)
00098       tmp = *x, idx = i;
00099   return idx;
00100 }
00101 
00102 //: y[i] = x[i]
00103 template <class T> inline
00104 void vnl_block_copy(T const x[], T y[], unsigned n)
00105 {
00106   for (unsigned i=0; i<n; ++i)
00107     y[i] = x[i];
00108 }
00109 
00110 //: reverses sequence
00111 template <class T> inline
00112 void vnl_block_reverse(T x[], unsigned n)
00113 {
00114   T tmp;
00115   for (unsigned i=0; 2*i<n; ++i) {
00116     tmp = x[i];
00117     x[i] = x[n-1-i];
00118     x[n-1-i] = tmp;
00119   }
00120 }
00121 
00122 //: x[i] *= a
00123 template <class T> inline
00124 void vnl_block_scale(T a, T x[], unsigned n)
00125 {
00126   for (unsigned i=0; i<n; ++i)
00127     x[i] *= a;
00128 }
00129 
00130 //: y[i] = a * x[i]
00131 template <class T> inline
00132 void vnl_block_scale(T a, T const x[], T y[], unsigned n)
00133 {
00134   for (unsigned i=0; i<n; ++i)
00135     y[i] = a * x[i];
00136 }
00137 
00138 //: y[i] += x[i]
00139 template <class T> inline
00140 void vnl_block_add(T const x[], T y[], unsigned n)
00141 {
00142   for (unsigned i=0; i<n; ++i)
00143     y[i] += x[i];
00144 }
00145 
00146 //: z[i] = x[i] + y[i]
00147 template <class T> inline
00148 void vnl_block_add(T const x[], T const y[], T z[], unsigned n)
00149 {
00150   for (unsigned i=0; i<n; ++i)
00151     z[i] = x[i] + y[i];
00152 }
00153 
00154 //: z[i] = x[i] - y[i]
00155 template <class T> inline
00156 void vnl_block_sub(T const x[], T const y[], T z[], unsigned n)
00157 {
00158   for (unsigned i=0; i<n; ++i)
00159     z[i] = x[i] - y[i];
00160 }
00161 
00162 //: y[i] *= x[i]
00163 template <class T> inline
00164 void vnl_block_mul(T const x[], T y[], unsigned n)
00165 {
00166   for (unsigned i=0; i<n; ++i)
00167     y[i] *= x[i];
00168 }
00169 
00170 //: z[i]  = x[i] * y[i]
00171 template <class T> inline
00172 void vnl_block_mul(T const x[], T const y[], T z[], unsigned n)
00173 {
00174   for (unsigned i=0; i<n; ++i)
00175     z[i] = x[i] * y[i];
00176 }
00177 
00178 //: z[i]  = x[i] / y[i]
00179 template <class T> inline
00180 void vnl_block_div(T const x[], T const y[], T z[], unsigned n)
00181 {
00182   for (unsigned i=0; i<n; ++i)
00183     z[i] = x[i] / y[i];
00184 }
00185 
00186 //: y[i]  = -x[i]
00187 template <class T> inline
00188 void vnl_block_negate(T const x[], T y[], unsigned n)
00189 {
00190   for (unsigned i=0; i<n; ++i)
00191     y[i] = - x[i];
00192 }
00193 
00194 //: y[i]  = 1/x[i]
00195 template <class T> inline
00196 void vnl_block_invert(T const x[], T y[], unsigned n)
00197 {
00198   for (unsigned i=0; i<n; ++i)
00199     y[i] = T(1) / x[i];
00200 }
00201 
00202 //:  y[i] += a * x[i]
00203 template <class T> inline
00204 void vnl_block_axpy(T a, T const x[], T y[], unsigned n)
00205 {
00206   for (unsigned i=0; i<n; ++i)
00207     y[i] += a * x[i];
00208 }
00209 
00210 //: x[i]  = v
00211 template <class T> inline
00212 void vnl_block_fill(T x[], unsigned n, T value)
00213 {
00214   for (unsigned i=0; i<n; ++i)
00215     x[i] = value;
00216 }
00217 
00218 #endif // vnl_block_h_