1 /*============================================================================
2         File: matrixTool.h
3      Purpose:
4     Revision: $Id: matrixTool.h,v 1.2 2002/05/13 21:07:45 philosophil Exp $
5   Created by: Philippe Lavoie          (26 January, 1998)
6  Modified by:
7 
8  Copyright notice:
9           Copyright (C) 1996-1999 Philippe Lavoie
10 
11 	  This library is free software; you can redistribute it and/or
12 	  modify it under the terms of the GNU Library General Public
13 	  License as published by the Free Software Foundation; either
14 	  version 2 of the License, or (at your option) any later version.
15 
16 	  This library is distributed in the hope that it will be useful,
17 	  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 	  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 	  Library General Public License for more details.
20 
21 	  You should have received a copy of the GNU Library General Public
22 	  License along with this library; if not, write to the Free
23 	  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 =============================================================================*/
25 
26 #ifndef _Matrix_matrixTool_h_
27 #define _Matrix_matrixTool_h_
28 
29 #include "matrix_global.h"
30 
31 namespace PLib {
32 
33 
34 
35 
36   template <class T>
compareT(T * a,T * b)37   inline int compareT(T*a, T* b){
38     if(*(a) > *(b))
39       return 1 ;
40     if(*(a) < *(b))
41       return -1 ;
42     return 0 ;
43   }
44 
45   // These functions are needed for qSort (in vector.h and matrix.h)
compareInt(const void * a,const void * b)46   inline int compareInt(const void* a, const void* b){
47     return compareT((int*)a,(int*)b) ;
48   }
49 
compareFloat(const void * a,const void * b)50   inline int compareFloat(const void* a, const void* b){
51     return compareT((float*)a,(float*)b) ;
52   }
53 
compareDouble(const void * a,const void * b)54   inline int compareDouble(const void* a, const void* b){
55     return compareT((float*)a,(float*)b) ;
56   }
57 
58   template <class T>
absolute(T a)59   inline T absolute(T a){
60     return ( a<T() ) ? -a : a ;
61   }
62 
absolute(double a)63   inline double absolute(double a) { return fabs(a) ; }
absolute(float a)64   inline float absolute(float a) { return fabs(a) ; }
65 
66   template <class T>
to2power(T a)67   inline T to2power(T a){
68     return a*a ;
69   }
70 
71   template <class T>
minimum(T a,T b)72   inline T minimum(T a, T b){
73     return a<b ? a : b ;
74   }
75 
76   template <class T>
maximum(T a,T b)77   inline T maximum(T a, T b){
78       return a>b ? a : b ;
79   }
80 
81   // Use minimumRef or maximumRef if you want to pass by reference
82   // You should only use this for special types and not for
83   // the base types
84   template <class T>
minimumRef(const T & a,const T & b)85   inline T minimumRef(const T &a, const T &b){
86     return a<b ? a : b ;
87   }
88 
89   template <class T>
maximumRef(const T & a,const T & b)90   inline T maximumRef(const T &a, const T &b){
91       return a>b ? a : b ;
92   }
93 
94 
95   // Some often used inline functions
96 
97   // definition for Complex, HPoint_3D, Point_3D, Vector2 and Coordinate
98   // follows
99 
minimum(Complex a,Complex b)100   inline Complex minimum(Complex a, Complex b){
101     double r,i ;
102     r = minimum(real(a),real(b)) ;
103     i = minimum(imag(a),imag(b)) ;
104     return Complex(r,i) ;
105   }
106 
maximum(Complex a,Complex b)107   inline Complex maximum(Complex a, Complex b){
108   double r,i ;
109   r = maximum(real(a),real(b)) ;
110   i = maximum(imag(a),imag(b)) ;
111   return Complex(r,i) ;
112   }
113 
114 
minimumByRef(const Complex & a,const Complex & b)115   inline Complex minimumByRef(const Complex &a, const Complex &b){
116     double r,i ;
117     r = minimum(real(a),real(b)) ;
118     i = minimum(imag(a),imag(b)) ;
119     return Complex(r,i) ;
120   }
121 
122 
maximumByRef(const Complex & a,const Complex & b)123   inline Complex maximumByRef(const Complex &a, const Complex &b){
124     double r,i ;
125     r = maximum(real(a),real(b)) ;
126     i = maximum(imag(a),imag(b)) ;
127     return Complex(r,i) ;
128   }
129 
130 
131   /*!
132     \fn void boundTo(T& a, T low, T high)
133     \brief bounds a variable to a region
134 
135     Bounds a value to a region, the variable $a$ is not changed
136     if it's already in the region. Otherwise it is set to one
137     of the boundary value.
138 
139     \param a  the variable to bound
140     \param low  the low bound value
141     \param  high  the higest bound value
142     \return The variable $a$ is now in the range $[low,high]$.
143 
144     \warning The variable low must be smaller then high.
145     \author Philippe Lavoie
146     \date 24 January 1997
147   */
boundTo(T & a,T low,T high)148   template<class T> inline void boundTo(T& a, T low, T high){
149     if(a<=low) a = low ;
150     if(a>=high) a = high ;
151   }
152 
153 } // end namespace
154 
155 #endif
156