vm_math  1.0.7
vm_m3math.h
1 #ifndef vm_m3math_h_INCLUDED
2 #define vm_m3math_h_INCLUDED
3 
4 // File: vm_m3math.h
5 // Author: Terry Gaetz
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_M3Math
33  *
34  * History
35  *--------
36  * 0.0.0 1998-Jan-30 tjg original version
37  */
38 
39 #include <vm_math/vm_vmath.h> // vm_VMath<T,N>
40 #include <cstring> // memcpy strlen
41 #include <cmath> // sqrt fabs
42 #include <iostream> // ostream, <<
43 #include <cstdio> // FILE*
44 
45 //########################################################################
46 // vm_M3Math<T_fp>
47 //########################################################################
48 
76 template <class T_fp>
77 class vm_M3Math
78  : public vm_VMath<T_fp,9>
79 {
80 private:
81 
82  enum ENelts_ { ENelts = 9 };
83  enum EColStride_ { EColStride = 3 };
84  enum ERows_ { ERow0 = 0, ERow1 = 3, ERow2 = 6 };
85 
86 public:
87 
88  // typedef typename T_fp value_type;
89  typedef T_fp value_type;
90 
95 
108  static int at( int i, int j );
115 
124  inline static void init_by_row( T_fp m[],
125  T_fp const row0[],
126  T_fp const row1[],
127  T_fp const row2[] );
128 
137  inline static void init_by_col( T_fp m[],
138  T_fp const col0[],
139  T_fp const col1[],
140  T_fp const col2[] );
141 
151  inline static void dyad_product( T_fp m[], T_fp const v1[], T_fp const v2[] );
158 
166  inline static void inject_row( T_fp m[],
167  T_fp const row[], int whichrow );
168  //
169  //: copy the supplied vector into row whichrow of m.
170 
178  inline static void inject_col( T_fp m[],
179  T_fp const col[], int whichcol );
180 
188  inline static void extract_row( T_fp const m[],
189  T_fp row[], int whichrow );
190 
198  inline static void extract_col( T_fp const m[],
199  T_fp col[], int whichcol );
202  // ---------------------------------------------------------------------
207 
217  inline static void mvmult( T_fp res[], T_fp const m[], T_fp const v[] );
218 
228  inline static void mtvmult( T_fp res[], T_fp const m[], T_fp const v[] );
231  // ---------------------------------------------------------------------
236 
237  inline static void mmult( T_fp mres[], T_fp const m1[], T_fp const m2[] );
240  // ---------------------------------------------------------------------
253  inline static std::ostream&
254  print_on( std::ostream& os, T_fp const m[],
255  char const prefix[] = "", char const postfix[] = "" );
256 
265  inline static void
266  cprint_on( FILE* of, T_fp const m[],
267  char const prefix[] = "", char const postfix[] = "" );
270 };
271 
272 //########################################################################
273 //########################################################################
274 //
275 // # # # # # # # ###### ####
276 // # ## # # # ## # # #
277 // # # # # # # # # # ##### ####
278 // # # # # # # # # # # #
279 // # # ## # # # ## # # #
280 // # # # ###### # # # ###### ####
281 //
282 //########################################################################
283 //########################################################################
284 
285 //-------------------------------------------------------------------------
286 // index calculation
287 
288 template <class T_fp>
289 inline int vm_M3Math<T_fp>::
290 at( int i, int j )
291 { return i * EColStride + j; }
292 
293 //-------------------------------------------------------------------------
294 // initialize matrix by row or by column
295 
296 template <class T_fp>
297 inline void vm_M3Math<T_fp>::
298 init_by_row( T_fp m[],
299  T_fp const row0[],
300  T_fp const row1[],
301  T_fp const row2[] )
302 { memcpy( &m[ERow0], row0, 3 * sizeof(T_fp) );
303  memcpy( &m[ERow1], row1, 3 * sizeof(T_fp) );
304  memcpy( &m[ERow2], row2, 3 * sizeof(T_fp) ); }
305 
306 template <class T_fp>
307 inline void vm_M3Math<T_fp>::
308 init_by_col( T_fp m[],
309  T_fp const col0[],
310  T_fp const col1[],
311  T_fp const col2[] )
312 { m[0] = col0[0]; m[3] = col0[1]; m[6] = col0[2];
313  m[1] = col1[0]; m[4] = col1[1]; m[7] = col1[2];
314  m[2] = col2[0]; m[5] = col2[1]; m[8] = col2[2]; }
315 
316 //-------------------------------------------------------------------------
317 // copy vector to given row or column of m
318 
319 template <class T_fp>
320 inline void vm_M3Math<T_fp>::
321 inject_row( T_fp m[],
322  T_fp const row[], int whichrow )
323 { memcpy( &m[whichrow*EColStride], row, 3 * sizeof(T_fp) ); }
324 
325 template <class T_fp>
326 inline void vm_M3Math<T_fp>::
327 inject_col( T_fp m[],
328  T_fp const col[], int whichcol )
329 {
330  double* pm = &m[whichcol];
331  *pm = *col++; pm += EColStride;
332  *pm = *col++; pm += EColStride;
333  *pm = *col;
334 }
335 
336 //-------------------------------------------------------------------------
337 // copy a given row or column of m to a vector
338 
339 template <class T_fp>
340 inline void vm_M3Math<T_fp>::
341 extract_row( T_fp const m[],
342  T_fp row[], int whichrow )
343 { memcpy( row, &m[whichrow*EColStride], 3 * sizeof(T_fp) ); }
344 
345 template <class T_fp>
346 inline void vm_M3Math<T_fp>::
347 extract_col( T_fp const m[],
348  T_fp col[], int whichcol )
349 {
350  double const* pm = &m[whichcol];
351  *col = *pm; pm += EColStride; ++col;
352  *col = *pm; pm += EColStride; ++col;
353  *col = *pm;
354 }
355 
356 //-------------------------------------------------------------------------
357 // initialize a matrix to a dyadic product of vectors
358 
359 template <class T_fp>
360 inline void vm_M3Math<T_fp>::
361 dyad_product( T_fp m[], T_fp const v1[], T_fp const v2[] )
362 {
363  m[0] = v1[0] * v2[0]; m[1] = v1[0] * v2[1]; m[2] = v1[0] * v2[2];
364  m[3] = v1[1] * v2[0]; m[4] = v1[1] * v2[1]; m[5] = v1[1] * v2[2];
365  m[6] = v1[2] * v2[0]; m[7] = v1[2] * v2[1]; m[8] = v1[2] * v2[2];
366 }
367 
368 //-------------------------------------------------------------------------
369 // matrix-vector operations.
370 
371 template <class T_fp>
372 inline void vm_M3Math<T_fp>::
373 mvmult( T_fp res[], T_fp const m[], T_fp const v[] )
374 {
375  res[0] = m[0] * v[0] + m[1] * v[1] + m[2] * v[2];
376  res[1] = m[3] * v[0] + m[4] * v[1] + m[5] * v[2];
377  res[2] = m[6] * v[0] + m[7] * v[1] + m[8] * v[2];
378 }
379 
380 template <class T_fp>
381 inline void vm_M3Math<T_fp>::
382 mtvmult( T_fp res[], T_fp const m[], T_fp const v[] )
383 {
384  res[0] = m[0] * v[0] + m[3] * v[1] + m[6] * v[2];
385  res[1] = m[1] * v[0] + m[4] * v[1] + m[7] * v[2];
386  res[2] = m[2] * v[0] + m[5] * v[1] + m[8] * v[2];
387 }
388 
389 template <class T_fp>
390 inline void vm_M3Math<T_fp>::
391 mmult( T_fp mres[], T_fp const m1[], T_fp const m2[] )
392 {
393  mres[0] = m1[0] * m2[0] + m1[1] * m2[3] + m1[2] * m2[6];
394  mres[1] = m1[0] * m2[1] + m1[1] * m2[4] + m1[2] * m2[7];
395  mres[2] = m1[0] * m2[2] + m1[1] * m2[5] + m1[2] * m2[8];
396 
397  mres[3] = m1[3] * m2[0] + m1[4] * m2[3] + m1[5] * m2[6];
398  mres[4] = m1[3] * m2[1] + m1[4] * m2[4] + m1[5] * m2[7];
399  mres[5] = m1[3] * m2[2] + m1[4] * m2[5] + m1[5] * m2[8];
400 
401  mres[6] = m1[6] * m2[0] + m1[7] * m2[3] + m1[8] * m2[6];
402  mres[7] = m1[6] * m2[1] + m1[7] * m2[4] + m1[8] * m2[7];
403  mres[8] = m1[6] * m2[2] + m1[7] * m2[5] + m1[8] * m2[8];
404 }
405 
406 template <class T_fp>
407 inline std::ostream& vm_M3Math<T_fp>::
408 print_on( std::ostream& os, T_fp const m[],
409  char const prefix[], char const postfix[] )
410 {
411  if ( std::strlen(prefix) ) { os << prefix; }
412  os << m[0] << " " << m[1] << " " << m[2] << "\n"
413  << m[3] << " " << m[4] << " " << m[5] << "\n"
414  << m[6] << " " << m[7] << " " << m[8] << "\n";
415  if ( std::strlen(postfix) ) { os << postfix; }
416  return os;
417 }
418 
419 template <class T_fp>
420 inline void vm_M3Math<T_fp>::
421 cprint_on( FILE* of, T_fp const m[],
422  char const prefix[], char const postfix[] )
423 {
424  if ( std::strlen(prefix) ) { std::fprintf(of, "%s", prefix); }
425  std::fprintf(of, "%.18e %.18e %.18e\n", m[0], m[1], m[2]);
426  std::fprintf(of, "%.18e %.18e %.18e\n", m[3], m[4], m[5]);
427  std::fprintf(of, "%.18e %.18e %.18e\n", m[6], m[7], m[8]);
428  if ( std::strlen(postfix) ) { std::fprintf(of, "%s", postfix); }
429 }
430 
431 #endif /* vm_m3math.h */