//# TabVecMath.h: Global functions for table vector mathematics //# Copyright (C) 1994,1995,1996,1999,2003 //# Associated Universities, Inc. Washington DC, USA. //# //# This library is free software; you can redistribute it and/or modify it //# under the terms of the GNU Library General Public License as published by //# the Free Software Foundation; either version 2 of the License, or (at your //# option) any later version. //# //# This library is distributed in the hope that it will be useful, but WITHOUT //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public //# License for more details. //# //# You should have received a copy of the GNU Library General Public License //# along with this library; if not, write to the Free Software Foundation, //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. //# //# Correspondence concerning AIPS++ should be addressed as follows: //# Internet email: aips2-request@nrao.edu. //# Postal address: AIPS++ Project Office //# National Radio Astronomy Observatory //# 520 Edgemont Road //# Charlottesville, VA 22903-2475 USA //# //# $Id$ #ifndef TABLES_TABVECMATH_H #define TABLES_TABVECMATH_H //# Global functions similar to those defined in ArrayMath are defined for //# the table vectors. Furthermore vector functions like norm are defined. //# Includes #include #include #include #include namespace casacore { //# NAMESPACE CASACORE - BEGIN // // Basic math for table vectors. // // // // // // These global functions do the basic math for table vectors. // This means addition, subtraction, multiplication, division // and negation. // In case two table vectors are used, the left and right operand // must be conformant (i.e. have equal length). // // // Add 2 table vectors storing result in first one. template inline void operator+= (TableVector& left, const TableVector& right); // Subtract 2 table vectors storing result in first one. template inline void operator-= (TableVector& left, const TableVector& right); // Multiple 2 table vectors storing result in first one. template inline void operator*= (TableVector& left, const TableVector& right); // Divide 2 table vectors storing result in first one. template inline void operator/= (TableVector& left, const TableVector& right); // Add a scalar to each element in the table vector. template inline void operator+= (TableVector& left, const T& right); // Subtract a scalar from each element in the table vector. template inline void operator-= (TableVector& left, const T& right); // Multiple each element in the table vector with a scalar. template inline void operator*= (TableVector& left, const T& right); // Divide each element in the table vector by a scalar. template inline void operator/= (TableVector& left, const T& right); // Unary plus. template inline TableVector operator+ (const TableVector&); // Unary minus. template inline TableVector operator- (const TableVector&); // Add 2 table vectors storing result in a new one. template inline TableVector operator+ (const TableVector& left, const TableVector& right); // Subtract 2 table vectors storing result in a new one. template inline TableVector operator- (const TableVector& left, const TableVector& right); // Multiple 2 table vectors storing result in a new one. template inline TableVector operator* (const TableVector& left, const TableVector& right); // Divide 2 table vectors storing result in a new one. template inline TableVector operator/ (const TableVector& left, const TableVector& right); // Add a scalar to each element in the table vector storing result // in a new table vector. template inline TableVector operator+ (const TableVector& left, const T& right); // Subtract a scalar from each element in the table vector storing result // in a new table vector. template inline TableVector operator- (const TableVector& left, const T& right); // Multiple each element in the table vector with a scalar storing result // in a new table vector. template inline TableVector operator* (const TableVector& left, const T& right); // Divide each element in the table vector by a scalar storing result // in a new table vector. template inline TableVector operator/ (const TableVector& left, const T& right); // Add a scalar to each element in the table vector storing result // in a new table vector. template inline TableVector operator+ (const T& left, const TableVector& right); // Subtract a scalar from each element in the table vector storing result // in a new table vector. template inline TableVector operator- (const T& left, const TableVector& right); // Multiple each element in the table vector with a scalar storing result // in a new table vector. template inline TableVector operator* (const T& left, const TableVector& right); // Divide each element in the table vector by a scalar storing result // in a new table vector. template inline TableVector operator/ (const T& left, const TableVector& right); // // // Transcendental math for table vectors. // // // // // // These global functions do the transcendental math for table vectors // for essentially all numeric types. // The functions are sin, sinh, exp, log, pow, etc.. // In case two table vectors are used, the left and right operand // must be conformant (i.e. have equal length). // // template inline TableVector cos (const TableVector&); template inline TableVector cosh (const TableVector&); template inline TableVector exp (const TableVector&); template inline TableVector log (const TableVector&); template inline TableVector log10(const TableVector&); template inline TableVector pow (const TableVector& value, const TableVector& exponent); template inline TableVector sin (const TableVector&); template inline TableVector sinh (const TableVector&); template inline TableVector sqrt (const TableVector&); // // // Further transcendental math for table vectors. // // // // // // These global functions do the transcendental math for table vectors // for a limited set of numeric types. // The functions are asin, ceil, etc.. // In case two table vectors are used, the left and right operand // must be conformant (i.e. have equal length). // // template inline TableVector acos (const TableVector&); template inline TableVector asin (const TableVector&); template inline TableVector atan (const TableVector&); template inline TableVector atan2(const TableVector& y, const TableVector& x); template inline TableVector ceil (const TableVector&); template inline TableVector fabs (const TableVector&); template inline TableVector floor(const TableVector&); template inline TableVector fmod (const TableVector& value, const TableVector& modulo); template inline TableVector pow (const TableVector& value, const double& exponent); template inline TableVector tan (const TableVector&); template inline TableVector tanh (const TableVector&); // // // Miscellaneous table vector operations. // // // // // // Fill a table vector or calculate the sum, product, minimum or // maximum of its elements. // // // This sets min and max to the min and max of the vector to avoid having // to do two passes with max() and min() separately. // Requires that the type "T" has comparison operators. template inline void minMax (T& min, T& max, const TableVector&); // The minimum element of the table vector. // Requires that the type "T" has comparison operators. template inline T min (const TableVector&); // The maximum element of the table vector. // Requires that the type "T" has comparison operators. template inline T max (const TableVector&); // Fills all elements of the table vector with a sequence starting with // "start" and incrementing by "inc" for each element. template inline void indgen (TableVector&, T start, T inc); // Fills all elements of the table vector with a sequence starting with // "start" incremented by one for each position in the table vector. template inline void indgen (TableVector&, T start); // Fills all elements of the table vector with a sequence starting with // 0 and ending with nelements() - 1. template inline void indgen (TableVector&); // Sum of all the elements of a table vector. template inline T sum (const TableVector&); // Product of all the elements of a table vector. // // product can easily overflow. // template inline T product (const TableVector&); // // // Vector operations on a table vector. // // // // // // Do vector operations on a table vector (like inner product). // // // The inner product of 2 table vectors. // The left and right operands must be conformant (i.e. have equal length). template inline T innerProduct (const TableVector& left, const TableVector& right); // The norm of a table vector. template inline T norm (const TableVector&); // The cross product of 2 table vectors containing 3 elements. template inline TableVector crossProduct (const TableVector& left, const TableVector& right); // //# Inline all these functions. //# The actual work is done by functions (tabVecRep...) operating on TabVecRep. //# Because the preprocessor of gcc-3 gives warnings when using the macro as //# e.g. TABVECMATHOPER(add,+,+=), the r is removed from the function name and //# put befroe the + in the macro call. #define TABVECMATHOPER(NAME,OP,OPA) \ template inline \ TableVector aips_name2(operato,OP) (const TableVector& tv, \ const T& v) \ { return TableVector (aips_name2(tabVecRepvalr,NAME) (tv.tabVec(), \ v)); } \ template inline \ TableVector aips_name2(operato,OP) (const T& v, \ const TableVector& tv) \ { return TableVector (aips_name2(tabVecRepvall,NAME) (v, \ tv.tabVec())); } \ template inline \ TableVector aips_name2(operato,OP) (const TableVector& l, \ const TableVector& r) \ { return TableVector (aips_name2(tabVecReptv,NAME) (l.tabVec(), \ r.tabVec())); } \ template inline \ void aips_name2(operato,OPA) (TableVector& tv, const T& v) \ { aips_name2(tabVecRepvalass,NAME) (tv.tabVec(), v); } \ template inline \ void aips_name2(operato,OPA) (TableVector& l, \ const TableVector& r) \ { aips_name2(tabVecReptvass,NAME) (l.tabVec(), r.tabVec()); } TABVECMATHOPER(add,r+,r+=) TABVECMATHOPER(sub,r-,r-=) TABVECMATHOPER(tim,r*,r*=) TABVECMATHOPER(div,r/,r/=) #define TABVECMATHFUNC(NAME) \ template inline \ TableVector NAME (const TableVector& tv) \ { return TableVector (aips_name2(tabVecRep,NAME) (tv.tabVec())); } #define TABVECMATHFUNC2(NAME) \ template inline \ TableVector NAME (const TableVector& l, \ const TableVector& r) \ { return TableVector (aips_name2(tabVecRep,NAME) (l.tabVec(), \ r.tabVec())); } TABVECMATHFUNC (cos) TABVECMATHFUNC (cosh) TABVECMATHFUNC (exp) TABVECMATHFUNC (log) TABVECMATHFUNC (log10) TABVECMATHFUNC2(pow) TABVECMATHFUNC (sin) TABVECMATHFUNC (sinh) TABVECMATHFUNC (sqrt) TABVECMATHFUNC (acos) TABVECMATHFUNC (asin) TABVECMATHFUNC (atan) TABVECMATHFUNC2(atan2) TABVECMATHFUNC (ceil) TABVECMATHFUNC (fabs) TABVECMATHFUNC (floor) TABVECMATHFUNC2(fmod) TABVECMATHFUNC (tan) TABVECMATHFUNC (tanh) template inline TableVector pow (const TableVector& tv, const double& exp) { return TableVector (tabVecReppowd (tv.tabVec(), exp)); } template inline T sum (const TableVector& tv) { return tabVecRepsum (tv.tabVec()); } template inline T product (const TableVector& tv) { return tabVecRepproduct (tv.tabVec()); } template inline void minMax (T& min, T& max, const TableVector& tv) { tabVecRepminmax (min, max, tv.tabVec()); } template inline T min (const TableVector& tv) { T Min,Max; tabVecRepminmax (Min, Max, tv.tabVec()); return Min; } template inline T max (const TableVector& tv) { T Min,Max; tabVecRepminmax (Min, Max, tv.tabVec()); return Max; } template inline void indgen (TableVector& tv, T start, T inc) { tabVecRepindgen (tv.tabVec(), start, inc); } template inline void indgen (TableVector& tv, T start) { tabVecRepindgen (tv.tabVec(), start, T(1)); } template inline void indgen (TableVector& tv) { tabVecRepindgen (tv.tabVec(), T(0), T(1)); } template inline T innerProduct (const TableVector& l, const TableVector& r) { return tabVecRepinnerproduct (l.tabVec(), r.tabVec()); } template inline T norm (const TableVector& tv) { return tabVecRepnorm (tv.tabVec()); } template inline TableVector crossProduct (const TableVector& l, const TableVector& r) { return TableVector (tabVecRepcrossproduct (l.tabVec(), r.tabVec())); } } //# NAMESPACE CASACORE - END #endif