头文件:
/* * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), [email protected] * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 2 or any later version. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * This program 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 General Public License for * more details. A copy of the GNU General Public License is available at: * http://www.fsf.org/licensing/licenses */ /***************************************************************************** * vectormath.h * * This file provides the basic math functions such as: * cos sin tan acos asin atan * abs exp log log10 sqrt pow * * Zhang Ming, 2010-08, Xi'an Jiaotong University. *****************************************************************************/ #ifndef VECTORMATH_H #define VECTORMATH_H #include <vector.h> namespace splab { template<typename Type> Vector<Type> abs( const Vector<Type>& ); template<typename Type> Vector<Type> cos( const Vector<Type>& ); template<typename Type> Vector<Type> sin( const Vector<Type>& ); template<typename Type> Vector<Type> tan( const Vector<Type>& ); template<typename Type> Vector<Type> acos( const Vector<Type>& ); template<typename Type> Vector<Type> asin( const Vector<Type>& ); template<typename Type> Vector<Type> atan( const Vector<Type>& ); template<typename Type> Vector<Type> exp( const Vector<Type>& ); template<typename Type> Vector<Type> log( const Vector<Type>& ); template<typename Type> Vector<Type> log10( const Vector<Type>& ); template<typename Type> Vector<Type> sqrt( const Vector<Type>& ); template<typename Type> Vector<Type> pow( const Vector<Type>&, const Vector<Type>& ); template<typename Type> Vector<Type> pow( const Vector<Type>&, const Type& ); template<typename Type> Vector<Type> pow( const Type&, const Vector<Type>& ); template<typename Type> Vector<Type> gauss( const Vector<Type>&, const Type&, const Type& ); #include <vectormath-impl.h> } // namespace splab #endif // VECTORMATH_H
实现文件:
/* * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), [email protected] * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 2 or any later version. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * This program 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 General Public License for * more details. A copy of the GNU General Public License is available at: * http://www.fsf.org/licensing/licenses */ /***************************************************************************** * vector-impl.h * * Implementation for Vector math functions. * * Zhang Ming, 2010-08, Xi'an Jiaotong University. *****************************************************************************/ template <typename Type> Vector<Type> abs( const Vector<Type> &v ) { Vector<Type> tmp( v.dim() ); typename Vector<Type>::iterator itrL = tmp.begin(); typename Vector<Type>::const_iterator itrR = v.begin(); while( itrL != tmp.end() ) *itrL++ = abs(*itrR++); return tmp; } template <typename Type> Vector<Type> cos( const Vector<Type> &v ) { Vector<Type> tmp( v.dim() ); typename Vector<Type>::iterator itrL = tmp.begin(); typename Vector<Type>::const_iterator itrR = v.begin(); while( itrL != tmp.end() ) *itrL++ = cos(*itrR++); return tmp; } template <typename Type> Vector<Type> sin( const Vector<Type> &v ) { Vector<Type> tmp( v.dim() ); typename Vector<Type>::iterator itrL = tmp.begin(); typename Vector<Type>::const_iterator itrR = v.begin(); while( itrL != tmp.end() ) *itrL++ = sin(*itrR++); return tmp; } template <typename Type> Vector<Type> tan( const Vector<Type> &v ) { Vector<Type> tmp( v.dim() ); typename Vector<Type>::iterator itrL = tmp.begin(); typename Vector<Type>::const_iterator itrR = v.begin(); while( itrL != tmp.end() ) *itrL++ = tan(*itrR++); return tmp; } template <typename Type> Vector<Type> acos( const Vector<Type> &v ) { Vector<Type> tmp( v.dim() ); typename Vector<Type>::iterator itrL = tmp.begin(); typename Vector<Type>::const_iterator itrR = v.begin(); while( itrL != tmp.end() ) *itrL++ = acos(*itrR++); return tmp; } template <typename Type> Vector<Type> asin( const Vector<Type> &v ) { Vector<Type> tmp( v.dim() ); typename Vector<Type>::iterator itrL = tmp.begin(); typename Vector<Type>::const_iterator itrR = v.begin(); while( itrL != tmp.end() ) *itrL++ = asin(*itrR++); return tmp; } template <typename Type> Vector<Type> atan( const Vector<Type> &v ) { Vector<Type> tmp( v.dim() ); typename Vector<Type>::iterator itrL = tmp.begin(); typename Vector<Type>::const_iterator itrR = v.begin(); while( itrL != tmp.end() ) *itrL++ = atan(*itrR++); return tmp; } template <typename Type> Vector<Type> exp( const Vector<Type> &v ) { Vector<Type> tmp( v.dim() ); typename Vector<Type>::iterator itrL = tmp.begin(); typename Vector<Type>::const_iterator itrR = v.begin(); while( itrL != tmp.end() ) *itrL++ = exp(*itrR++); return tmp; } template <typename Type> Vector<Type> log( const Vector<Type> &v ) { Vector<Type> tmp( v.dim() ); typename Vector<Type>::iterator itrL = tmp.begin(); typename Vector<Type>::const_iterator itrR = v.begin(); while( itrL != tmp.end() ) *itrL++ = log(*itrR++); return tmp; } template <typename Type> Vector<Type> log10( const Vector<Type> &v ) { Vector<Type> tmp( v.dim() ); typename Vector<Type>::iterator itrL = tmp.begin(); typename Vector<Type>::const_iterator itrR = v.begin(); while( itrL != tmp.end() ) *itrL++ = log10(*itrR++); return tmp; } template <typename Type> Vector<Type> sqrt( const Vector<Type> &v ) { Vector<Type> tmp( v.dim() ); typename Vector<Type>::iterator itrL = tmp.begin(); typename Vector<Type>::const_iterator itrR = v.begin(); while( itrL != tmp.end() ) *itrL++ = sqrt(*itrR++); return tmp; } template <typename Type> Vector<Type> pow( const Vector<Type> &b, const Vector<Type> &e ) { assert( b.dim() == e.dim() ); Vector<Type> tmp( b.dim() ); typename Vector<Type>::iterator itrL = tmp.begin(); typename Vector<Type>::const_iterator itrR1 = b.begin(); typename Vector<Type>::const_iterator itrR2 = e.begin(); while( itrL != tmp.end() ) *itrL++ = pow( *itrR1++, *itrR2++ ); return tmp; } template <typename Type> Vector<Type> pow( const Vector<Type> &b, const Type &e ) { Vector<Type> tmp( b.dim() ); typename Vector<Type>::iterator itrL = tmp.begin(); typename Vector<Type>::const_iterator itrR = b.begin(); while( itrL != tmp.end() ) *itrL++ = pow( *itrR++, e ); return tmp; } template <typename Type> Vector<Type> pow( const Type &b, const Vector<Type> &e ) { Vector<Type> tmp( e.dim() ); typename Vector<Type>::iterator itrL = tmp.begin(); typename Vector<Type>::const_iterator itrR = e.begin(); while( itrL != tmp.end() ) *itrL++ = pow( b, *itrR++ ); return tmp; } /** * Normal distribution with expectation "u" and variance "r". */ template <typename Type> Vector<Type> gauss( const Vector<Type> &x, const Type &u, const Type &r ) { Vector<Type> tmp(x); tmp = (tmp-u)*(tmp-u) / ( -2*r*r ); tmp = exp(tmp) / Type( (sqrt(2*PI)*r) ); return tmp; }
测试代码:
/***************************************************************************** * vectormath_test.cpp * * Math functions of vector testing. * * Zhang Ming, 2010-08, Xi'an Jiaotong University. *****************************************************************************/ #define BOUNDS_CHECK #include <iostream> #include <iomanip> #include <vectormath.h> using namespace std; using namespace splab; int main() { int N = 5; double a = 0, b = 2*PI; Vector<double> x = linspace( a, b, N ); cout << setiosflags(ios::fixed) << setprecision(4); cout << x << endl; cout << "sin of x : " << sin(x) << endl; cout << "cos of x : "<< cos(x) << endl; cout << "tan of x : " << tan(x) << endl; cout << "asin of x : "<< asin(x) << endl; cout << "acos of x : " << acos(x) << endl; cout << "atan of x : " << atan(x) << endl; cout << "exp of x : "<< exp(x) << endl; cout << "log of x : " << log(x) << endl; cout << "log10 of x : " << log10(x) << endl; a = 2.0; cout << "sqrt of x : "<< sqrt(x) << endl; cout << "pow of x : " << pow(x,x) << endl; cout << "pow of x : " << pow(x,a) << endl; cout << "pow of x : " << pow(a,x) << endl; cout << "The standard normal distribution : " << gauss( x, a, b ) << endl; return 0; }
运行结果:
size: 5 by 1 0.0000 1.5708 3.1416 4.7124 6.2832 sin of x : size: 5 by 1 0.0000 1.0000 0.0000 -1.0000 -0.0000 cos of x : size: 5 by 1 1.0000 0.0000 -1.0000 -0.0000 1.0000 tan of x : size: 5 by 1 0.0000 16331778728383844.0000 -0.0000 5443926242794615.0000 -0.0000 asin of x : size: 5 by 1 0.0000 nan nan nan nan acos of x : size: 5 by 1 1.5708 nan nan nan nan atan of x : size: 5 by 1 0.0000 1.0039 1.2626 1.3617 1.4130 exp of x : size: 5 by 1 1.0000 4.8105 23.1407 111.3178 535.4917 log of x : size: 5 by 1 -inf 0.4516 1.1447 1.5502 1.8379 log10 of x : size: 5 by 1 -inf 0.1961 0.4971 0.6732 0.7982 sqrt of x : size: 5 by 1 0.0000 1.2533 1.7725 2.1708 2.5066 pow of x : size: 5 by 1 1.0000 2.0327 36.4622 1487.9012 103540.9204 pow of x : size: 5 by 1 0.0000 2.4674 9.8696 22.2066 39.4784 pow of x : size: 5 by 1 1.0000 2.9707 8.8250 26.2162 77.8802 The standard normal distribution : size: 5 by 1 0.0604 0.0633 0.0625 0.0578 0.0503 Process returned 0 (0x0) execution time : 0.125 s Press any key to continue.