Developer documentation

loops_vctops.hpp
Go to the documentation of this file.
1 /********************************************************************
2  * This file is part of the source code of the realroot library.
3  * Author(s): J.P. Pavone, GALAAD, INRIA
4  * $Id: vctops.hpp,v 1.1 2005/07/11 10:03:56 jppavone Exp $
5  ********************************************************************/
6 #ifndef realroot_SOLVE_SBDSLV_LOOPS_VCTOPS_H
7 #define realroot_SOLVE_SBDSLV_LOOPS_VCTOPS_H
8 //--------------------------------------------------------------------
9 #include <iostream>
10 #include <stdlib.h>
12 //--------------------------------------------------------------------
13 namespace mmx {
14 //--------------------------------------------------------------------
15 namespace vctops
16  {
17  /* SCalarSUBstract (in place) */
18  template<typename real_t> inline
19  void scsub( const real_t& sc, real_t * data, int sz, int st = 1)
20  { for ( int p = 0; p !=sz*st; p += st ) data[p] -= sc; };
21 
22  template<typename real_t> inline
23  void scsub( const real_t& sc, real_t * data, const real_t * src, int sz, int sta = 1, int stb = 1 )
24  { int pd,p; for ( pd = p = 0; p !=sz*sta; p += sta, pd += stb ) data[pd] = src[p]-sc; };
25 
26  template<typename real_t> inline
27  void accscmul( real_t * dst, const real_t& sc, real_t const * _src_, int sz, int stdst = 1, int stsrc = 1 )
28  {
29  real_t const * src = _src_;
30  while ( src != _src_ + sz*stsrc ) { *dst += *src*sc; src += stsrc; dst += stdst; };
31  };
32 
33  /* SCalarMULtiply (in place) */
34  template<typename real_t> inline
35  void scmul( const real_t& sc, real_t * data, int sz, int st = 1)
36  { for ( int p = 0; p != sz*st; p += st ) data[p] *= sc; };
37  /* SCalarDIVide (in place) */
38  template<typename real_t> inline
39  void scdiv( const real_t& sc, real_t * data, int sz, int st = 1)
40  { for ( int p = 0; p != sz*st; p += st ) data[p] /= sc; };
41 
42  /* DOTPRODuct */
43  template<typename real_t> inline
44  real_t dotprod( const real_t * a, const real_t * b, int sz, int sta = 1, int stb = 1)
45  {
46  real_t acc = 0.0;
47  for ( int p = 0; p != sz*sta; p += sta, b += stb ) acc += a[p]*(*b);
48  return acc;
49  };
50 
51  template<typename real_t> inline /* ParallelADD */
52  void padd( real_t * a, const real_t * b, unsigned sz, int sta = 1, int stb = 1 )
53  { for ( int p = 0; p != sz*sta; p += sta, b += stb ) a[p] += *b; };
54 
55  template<typename real_t> inline /* ParallelSUB */
56  void psub( real_t * a, const real_t * b, unsigned sz, int sta = 1, int stb = 1 )
57  { for ( int p = 0; p != sz*sta; p += sta, b += stb ) a[p] -= *b; };
58 
59  template<typename real_t> inline /* ParallelMUL */
60  void pmul( real_t * a, const real_t * b, unsigned sz, int sta = 1, int stb = 1 )
61  { for ( int p = 0; p != sz*sta; p += sta, b += stb ) a[p] *= *b; };
62 
63  template<typename real_t> inline /* ParallelDIV */
64  void pdiv( real_t * a, const real_t * b, unsigned sz, int sta = 1, int stb = 1 )
65  { int p; for ( p = 0; p != sz*sta; a[p] /= *b, p += sta, b += stb ) ; };
66 
67  template<typename real0, typename real_t> /* MIN and MAX Update */
68  void minmaxu( real0& min, real0& max, real_t * src, int sz, int st=1 )
69  {
70  int i,p;
71  for ( p = 0, i = 0; i < sz/2; i ++, p += 2*st )
72  {
73  if ( src[p] < src[p+st] )
74  {
75  if ( src[p] < min ) min = src[p];
76  if ( src[p+st] > max ) max = src[p+st];
77  }
78  else
79  {
80  if ( src[p] > max ) max = src[p];
81  if ( src[p+st] < min ) min = src[p+st];
82  };
83  };
84  };
85 
86  template<typename real0, typename real_t> /* MIN and MAX */
87  void minmax( real0& min, real0& max, real_t * src, int sz, int st=1 )
88  {
89  min = src[(sz-1)*st];
90  max = src[(sz-1)*st];
91  minmaxu(min,max,src,sz,st);
92  };
93 
94  template<typename real_t>
95  real_t min( real_t const * src, int sz, int st = 1 )
96  {
97  real_t r = src[0];
98  for ( int p = st; p != sz*st; p += st ) if ( r > src[p] ) r = src[p];
99  return r;
100  };
101 
102  template<typename real_t>
103  real_t max( real_t const * src, int sz, int st = 1 )
104  {
105  real_t r = src[0];
106  for ( int p = st; p != sz*st; p += st ) if ( r < src[p] ) r = src[p];
107  return r;
108  };
109 
110 
111  template<typename T> /* Uniform RANDom, store sz random values in [a,b] to data */
112  void urand( T * data, unsigned sz, const T& a, const T& b, int st = 1 )
113  {
114  for ( int p = 0; p != sz*st; p += st )
115  {
116  double t = ((double)rand()/(double)RAND_MAX);
117  data[p] = T(((1.0-t)*a + t*b));
118  };
119  };
120 
121  template<typename real_t> /* MEAN value */
122  real_t mean( real_t const * const data, int sz, int st )
123  {
124  real_t mv = 0.0;
125  for ( int p = 0; p != sz*st; mv += data[p], p += st ) ;
126  return mv/sz;
127  }
128 
129  template<typename real_t>
130  std::ostream& print( real_t const * const data, unsigned sz, int st = 1, std::ostream& out = std::cout )
131  {
132  int p = 0;
133  out << "[ ";
134  for ( unsigned i = 0; i < sz-1; i++, p += st )
135  {
136  out << data[p];
137  out << ", ";
138  };
139  out << data[p] << " ]";
140  return out;
141  };
142 
143  template<typename real_t> inline /* SiGN CouNT, count changes of sign */
144  unsigned sgncnt ( real_t const * b , unsigned sz, int st = 1 )
145  {
146  real_t const * last = b + (sz-1)*st;
147  unsigned c;
148  for ( c = 0; b != last; b += st )
149  c += *b > 0 != *(b+st) > 0;
150  return c;
151  };
152 
153  template<typename real_t> inline /* SiGN CHanGes, changes of sign ? */
154  bool sgnchg( real_t const * const b, unsigned sz, int st = 1 )
155  {
156  int p = st;
158  std::abs(b[(sz-1)*st]) < numerics::epsilon<real_t>::result ) return true;
159  bool pprv = b[0]>0;//numerics::epsilon<real_t>::result;
160  // bool pcurr;
161  for ( unsigned i = 1; i < sz; i++, p+= st )
162  if ( (b[p]>0) != pprv ) return true;
163  return false;
164  };
165 
166  template<typename real_t> inline /* squared euclidian distance */
167  real_t distance2( real_t const * const a, real_t const * const b, unsigned sz, int sta, int stb )
168  {
169  real_t s;
170  int pa,pb;
171  for ( s = pb = pa = 0; pa != sz*sta; pa += sta, pb += stb ) s += (a[pa]-b[pb])*(a[pa]-b[pb]);
172  return s;
173  };
174 
175  template<typename real_t> inline
176  real_t delta_max( real_t * a, real_t * b, unsigned sz, int st = 1 )
177  {
178  int p = st;
179  real_t tmp = a[0]-b[0];
180  for ( unsigned i = 1; i < sz; i++ , p += st )
181  if ( a[p]-b[p] > tmp ) tmp = a[p]-b[p];
182  return tmp;
183  };
184 
185  template<typename real_t> inline
186  int delta_max_index( real_t * a, real_t * b, unsigned sz, int st = 1 )
187  {
188  int mx = 0;
189  int p = st;
190  real_t tmp = a[0]-b[0];
191  for ( unsigned i = 1; i < sz; i++ , p += st )
192  if ( a[p]-b[p] > tmp ) { mx = i; tmp = a[p]-b[p]; };
193  return mx;
194  };
195 
196  template<typename real_t> inline
197  real_t sum( real_t const * const src, unsigned sz, int st = 1 )
198  {
199  real_t acc = (real_t)0.0;
200  for ( int i = 0; i != sz*st; acc += src[i], i += st )
201  ;
202  return acc;
203  }
204 
205  template<typename real_t> inline
206  unsigned set_conversion( real_t * src, unsigned sz, const real_t& epsilon = (real_t)0 )
207  {
208  std::sort(src,src+sz);
209  unsigned c = 0;
210  for ( unsigned i = 1; i < sz; i++ )
211  if ( src[i]-src[c] > epsilon ) src[++c] = src[i];
212  return c+1;
213  };
214 
215  template<typename real_t> inline
216  void scale( real_t * src, unsigned sz, const real_t& sc = (real_t)(1.0), int st= 1 )
217  { real_t mn,mx; minmax(mn,mx,src,sz,st); scmul(sc/(mx-mn),src,sz,st); };
218 
219  template< typename U, typename Y, typename Z > /* convolution 1d */
220  void convolution(U*dst,Y const*a,Z const*b, unsigned sza, unsigned szb, int sta=1, int stb=1, int stout=1)
221  {
222  assert((dst!=a)&&(dst!=b));
223  int p,ia,ib,q;
224  for ( p = 0; p != (sza+szb-1)*stout; dst[p] = (U)0, p += stout ) ;
225  for ( p = ia =0; ia != sza*sta; ia += sta, p += stout )
226  for ( ib = 0, q = p; ib != szb*stb; dst[q] += a[ia]*b[ib], ib += stb, q += stout ) ;
227  };
228 
229  template< typename U, typename Y, typename Z >
230  void self_convolution( U * dst, Y const * a, unsigned sza, int sta = 1, int stout = 1 )
231  {
232  int p,q,ia,ib;
233  for ( p = 0; p != (2*sza-1); dst[p] = 0, p += stout ) ;
234  for ( p = ia = 0; ia != sza*sta; dst[2*p] += a[ia]*a[ia], ia += sta, p += stout )
235  for ( ib = 0, q = p; ib != ia; dst[q] += 2*a[ia]*a[ib], ib += sta, q += stout ) ;
236  };
237  };
238 //--------------------------------------------------------------------
239 } //namespace mmx
240 /********************************************************************/
241 #endif //
Definition: system_epsilon.hpp:19
unsigned set_conversion(real_t *src, unsigned sz, const real_t &epsilon=(real_t) 0)
Definition: loops_vctops.hpp:206
int delta_max_index(real_t *a, real_t *b, unsigned sz, int st=1)
Definition: loops_vctops.hpp:186
const C & b
Definition: Interval_glue.hpp:25
void scdiv(const real_t &sc, real_t *data, int sz, int st=1)
Definition: loops_vctops.hpp:39
void accscmul(real_t *dst, const real_t &sc, real_t const *_src_, int sz, int stdst=1, int stsrc=1)
Definition: loops_vctops.hpp:27
unsigned sgncnt(real_t const *b, unsigned sz, int st=1)
Definition: loops_vctops.hpp:144
void scale(real_t *src, unsigned sz, const real_t &sc=(real_t)(1.0), int st=1)
Definition: loops_vctops.hpp:216
real_t mean(real_t const *const data, int sz, int st)
Definition: loops_vctops.hpp:122
std::ostream & print(real_t const *const data, unsigned sz, int st=1, std::ostream &out=std::cout)
Definition: loops_vctops.hpp:130
real_t distance2(real_t const *const a, real_t const *const b, unsigned sz, int sta, int stb)
Definition: loops_vctops.hpp:167
void scmul(const real_t &sc, real_t *data, int sz, int st=1)
Definition: loops_vctops.hpp:35
real_t min(real_t const *src, int sz, int st=1)
Definition: loops_vctops.hpp:95
void pdiv(real_t *a, const real_t *b, unsigned sz, int sta=1, int stb=1)
Definition: loops_vctops.hpp:64
real_t delta_max(real_t *a, real_t *b, unsigned sz, int st=1)
Definition: loops_vctops.hpp:176
real_t sum(real_t const *const src, unsigned sz, int st=1)
Definition: loops_vctops.hpp:197
void convolution(U *dst, Y const *a, Z const *b, unsigned sza, unsigned szb, int sta=1, int stb=1, int stout=1)
Definition: loops_vctops.hpp:220
void self_convolution(U *dst, Y const *a, unsigned sza, int sta=1, int stout=1)
Definition: loops_vctops.hpp:230
void minmax(real0 &min, real0 &max, real_t *src, int sz, int st=1)
Definition: loops_vctops.hpp:87
bool sgnchg(real_t const *const b, unsigned sz, int st=1)
Definition: loops_vctops.hpp:154
void abs(Interval< C, r > &x, const Interval< C, r > &a)
Definition: Interval_fcts.hpp:185
real_t dotprod(const real_t *a, const real_t *b, int sz, int sta=1, int stb=1)
Definition: loops_vctops.hpp:44
void minmaxu(real0 &min, real0 &max, real_t *src, int sz, int st=1)
Definition: loops_vctops.hpp:68
void urand(T *data, unsigned sz, const T &a, const T &b, int st=1)
Definition: loops_vctops.hpp:112
void scsub(const real_t &sc, real_t *data, int sz, int st=1)
Definition: loops_vctops.hpp:19
void pmul(real_t *a, const real_t *b, unsigned sz, int sta=1, int stb=1)
Definition: loops_vctops.hpp:60
real_t max(real_t const *src, int sz, int st=1)
Definition: loops_vctops.hpp:103
const C & c
Definition: Interval_glue.hpp:45
void psub(real_t *a, const real_t *b, unsigned sz, int sta=1, int stb=1)
Definition: loops_vctops.hpp:56
void padd(real_t *a, const real_t *b, unsigned sz, int sta=1, int stb=1)
Definition: loops_vctops.hpp:52
Definition: array.hpp:12
#define assert(expr, msg)
Definition: shared_object.hpp:57
Home