vm_math  1.0.7
vm_vmath.h
1 #ifndef vm_vmath_h_INCLUDED
2 #define vm_vmath_h_INCLUDED
3 
4 // File: vm_vmath.h
5 // Author: Terry Gaet
6 
7 /* --8<--8<--8<--8<--
8  *
9  * Copyright (C) 2006 Smithsonian Astrophysical Observatory
10  *
11  * This file is part of vm_math.cvs
12  *
13  * vm_math.cvs is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * vm_math.cvs is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the
25  * Free Software Foundation, Inc.
26  * 51 Franklin Street, Fifth Floor
27  * Boston, MA 02110-1301, USA
28  *
29  * -->8-->8-->8-->8-- */
30 
31 /****************************************************************************
32  * Description: collection of methods for vm_VMath
33  *
34  * History
35  *--------
36  * 0.0.0 1998-Jan-30 tjg original version
37  */
38 
39 #include <cstring> // memcpy strlen
40 #include <cmath> // sqrt fabs
41 #include <iostream> // ostream
42 #include <cstdio> // FILE*
43 
44 //########################################################################
45 // vm_VMath<T_fp,N_len>
46 //########################################################################
47 
48  //class vm_VMath<T_fp,N_len> vm_math.h <vm_math/vm_vmath.h>
77 template <class T_fp, int N_len>
78 class vm_VMath
79 {
80 private:
81 
82  enum ENelts_ { ENelts = N_len };
83 
84 public:
85 
91  typedef T_fp value_type;
92 
97 
107  inline static void copy( T_fp v[], T_fp const cv[] );
120  inline static void set( T_fp v[], T_fp r );
133  inline static void add_eq( T_fp v[], T_fp const cv[] );
134 
141  inline static void sub_eq( T_fp v[], T_fp const cv[] );
142 
149  inline static void mul_eq( T_fp v[], T_fp const cv[] );
150 
157  inline static void div_eq( T_fp v[], T_fp const cv[] );
158 
165  inline static void add_eq( T_fp v[], T_fp r );
166 
173  inline static void sub_eq( T_fp v[], T_fp r );
174 
181  inline static void mul_eq( T_fp v[], T_fp r );
182 
189  inline static void div_eq( T_fp v[], T_fp r );
190 
196  inline static void negate( T_fp v[] );
210  inline static void add( T_fp v[], T_fp const cv1[], T_fp const cv2[] );
211 
219  inline static void sub( T_fp v[], T_fp const cv1[], T_fp const cv2[] );
220 
228  inline static void mul( T_fp v[], T_fp const cv1[], T_fp const cv2[] );
229 
237  inline static void div( T_fp v[], T_fp const cv1[], T_fp const cv2[] );
238 
246  inline static void add( T_fp v[], T_fp const cv[], T_fp r );
247 
255  inline static void sub( T_fp v[], T_fp const cv[], T_fp r );
256 
264  inline static void mul( T_fp v[], T_fp const cv[], T_fp r );
265 
273  inline static void div( T_fp v[], T_fp const cv[], T_fp r );
274 
282  inline static void add( T_fp v[], T_fp r, T_fp const cv[] );
283 
291  inline static void sub( T_fp v[], T_fp r, T_fp const cv[] );
292 
300  inline static void mul( T_fp v[], T_fp r, T_fp const cv[] );
301 
309  inline static void div( T_fp v[], T_fp r, T_fp const cv[] );
312  // ---------------------------------------------------------------------
329  inline static void lincomb( T_fp res[],
330  T_fp c1, T_fp const v1[],
331  T_fp c2, T_fp const v2[] );
334  // ---------------------------------------------------------------------
348  inline static std::ostream&
349  print_on( std::ostream& os, T_fp const v[], int by,
350  char const prefix[] = "", char const postfix[] = "" );
351 
361  inline static void
362  cprint_on( FILE* of, T_fp const v[], int by,
363  char const prefix[] = "", char const postfix[] = "" );
365 };
366 
367 //########################################################################
368 //########################################################################
369 //
370 // # # # # # # # ###### ####
371 // # ## # # # ## # # #
372 // # # # # # # # # # ##### ####
373 // # # # # # # # # # # #
374 // # # ## # # # ## # # #
375 // # # # ###### # # # ###### ####
376 //
377 //########################################################################
378 //########################################################################
379 
380 //-------------------------------------------------------------------------
381 // copy routines
382 
383 template <class T_fp, int N_len>
384 inline void vm_VMath<T_fp,N_len>::
385 copy( T_fp v[], T_fp const cv[] )
386 { memcpy( v, cv, N_len * sizeof(T_fp) ); }
387 
388 //-------------------------------------------------------------------------
389 // set functions
390 
391 template <class T_fp, int N_len>
392 inline void vm_VMath<T_fp,N_len>::
393 set( T_fp v[], T_fp r )
394 { int n = N_len+1; while ( --n ) { *v++ = r; } }
395 
396 //-------------------------------------------------------------------------
397 // {add,sub,mul,div}_eq: componentwise operations.
398 
399 #define _tpl_Op_EQ_(_OPNAM_,_OPEQ_) \
400  template <class T_fp, int N_len> \
401  inline void vm_VMath<T_fp,N_len>::_OPNAM_( T_fp v[], T_fp const cv[] ) \
402  { int n = N_len+1; while ( --n ) { *v++ _OPEQ_ *cv++; } } \
403  template <class T_fp, int N_len> \
404  inline void vm_VMath<T_fp,N_len>::_OPNAM_( T_fp v[], T_fp r ) \
405  { int n = N_len+1; while ( --n ) { *v++ _OPEQ_ r; } }
406  _tpl_Op_EQ_(add_eq,+=)
407  _tpl_Op_EQ_(sub_eq,-=)
408  _tpl_Op_EQ_(mul_eq,*=)
409  _tpl_Op_EQ_(div_eq,/=)
410 #undef _tpl_Op_EQ_
411 
412 template <class T_fp, int N_len>
413 inline void vm_VMath<T_fp,N_len>::
414 negate( T_fp v[] )
415 { int n = N_len+1; while ( --n ) { *v = - *v; ++v; } }
416 
417 //-------------------------------------------------------------------------
418 // {add,sub,mul,div}: componentwise binary operations.
419 #define _tpl_BinOp_(_OPNAM_,_OP_) \
420  template <class T_fp, int N_len> \
421  inline void vm_VMath<T_fp,N_len>::_OPNAM_( T_fp v[], \
422  T_fp const cv1[], T_fp const cv2[] ) \
423  { int n = N_len+1; while ( --n ) { *v++ = *cv1++ _OP_ *cv2++; } } \
424  template <class T_fp, int N_len> \
425  inline void vm_VMath<T_fp,N_len>::_OPNAM_( T_fp v[], T_fp const cv[], T_fp r ) \
426  { int n = N_len+1; while ( --n ) { *v++ = *cv++ _OP_ r; } } \
427  template <class T_fp, int N_len> \
428  inline void vm_VMath<T_fp,N_len>::_OPNAM_( T_fp v[], T_fp r, T_fp const cv[] ) \
429  { int n = N_len+1; while ( --n ) { *v++ = r _OP_ *cv++; } }
430  _tpl_BinOp_(add,+)
431  _tpl_BinOp_(sub,-)
432  _tpl_BinOp_(mul,*)
433  _tpl_BinOp_(div,/)
434 #undef _tpl_BinOp_
435 
436 //-------------------------------------------------------------------------
437 // linear combinations
438 
439 template <class T_fp, int N_len>
440 inline void vm_VMath<T_fp,N_len>::
441 lincomb( T_fp res[],
442  T_fp c1, T_fp const v1[],
443  T_fp c2, T_fp const v2[] )
444 { int n = N_len+1; while ( --n ) { *res++ = c1 * *v1++ + c2 * *v2++; } }
445 
446 //-------------------------------------------------------------------------
447 // i/o
448 
449 template <class T_fp, int N_len>
450 inline std::ostream& vm_VMath<T_fp,N_len>::
451 print_on( std::ostream& os, T_fp const v[], int by,
452  char const prefix[], char const postfix[] )
453 {
454  if ( std::strlen(prefix) ) { os << prefix; }
455  int i = N_len / by;
456  while ( i-- )
457  {
458  int j = by;
459  while ( --j )
460  {
461  os << *v++ << " ";
462  }
463  os << *v++ << "\n";
464  }
465  if ( std::strlen(postfix) ) { os << postfix; }
466  return os;
467 }
468 
469 template <class T_fp, int N_len>
470 inline void vm_VMath<T_fp,N_len>::
471 cprint_on( FILE* of, T_fp const v[], int by,
472  char const prefix[], char const postfix[] )
473 {
474  if ( std::strlen(prefix) ) { std::fprintf(of, "%s", prefix); }
475  int i = N_len / by;
476  while ( i-- )
477  {
478  int j = by;
479  while ( --j )
480  {
481  fprintf(of, "%.18e ", *v++);
482  }
483  fprintf(of, "%.18e\n", *v++);
484  }
485  if ( std::strlen(postfix) ) { std::fprintf(of, "%s", postfix); }
486 }
487 
488 #endif /* vm_vmath.h */