常用数学函数矩阵版本的C++实现

头文件:

/*
 * 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
 */


/*****************************************************************************
 *                               matrixmath.h
 *
 * This file provides the basic math functions such as:
 *              cos    sin    tan    acos   asin   atan
 *              abs    exp    log    log10  sqrt   pow
 *
 * When debugging, use #define BOUNDS_CHECK above your "#include matrix.h"
 * line. When done debugging, comment out #define BOUNDS_CHECK for better
 * performance.
 *
 * Zhang Ming, 2010-01 (revised 2010-08), Xi'an Jiaotong University.
 *****************************************************************************/


#ifndef MATRIXMATH_H
#define MATRIXMATH_H


#include <matrix.h>


namespace splab
{

    template<typename Type> Matrix<Type> abs( const Matrix<Type>& );
    template<typename Type> Matrix<Type> cos( const Matrix<Type>& );
    template<typename Type> Matrix<Type> sin( const Matrix<Type>& );
    template<typename Type> Matrix<Type> tan( const Matrix<Type>& );
    template<typename Type> Matrix<Type> acos( const Matrix<Type>& );
    template<typename Type> Matrix<Type> asin( const Matrix<Type>& );
    template<typename Type> Matrix<Type> atan( const Matrix<Type>& );

    template<typename Type> Matrix<Type> exp( const Matrix<Type>& );
    template<typename Type> Matrix<Type> log( const Matrix<Type>& );
    template<typename Type> Matrix<Type> log10( const Matrix<Type>& );

    template<typename Type> Matrix<Type> sqrt( const Matrix<Type>& );
    template<typename Type> Matrix<Type> pow( const Matrix<Type>&,
                                              const Matrix<Type>& );
    template<typename Type> Matrix<Type> pow( const Matrix<Type>&,
                                              const Type& );
    template<typename Type> Matrix<Type> pow( const Type&,
                                              const Matrix<Type>& );


    #include <matrixmath-impl.h>

}
// namespace splab


#endif
// MATRIXMATH_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 Matrix math functions.
 *
 * Zhang Ming, 2010-08, Xi'an Jiaotong University.
 *****************************************************************************/


template <typename Type>
Matrix<Type> abs( const Matrix<Type> &A )
{
    int m = A.rows(),
        n = A.cols();
    Matrix<Type> tmp( m, n );

    for( int i=0; i<m; ++i )
        for( int j=0; j<n; ++j )
            tmp[i][j] = abs( A[i][j] );

    return tmp;
}


template <typename Type>
Matrix<Type> cos( const Matrix<Type> &A )
{
    int m = A.rows(),
        n = A.cols();
    Matrix<Type> tmp( m, n );

    for( int i=0; i<m; ++i )
        for( int j=0; j<n; ++j )
            tmp[i][j] = cos( A[i][j] );

    return tmp;
}


template <typename Type>
Matrix<Type> sin( const Matrix<Type> &A )
{
    int m = A.rows(),
        n = A.cols();
    Matrix<Type> tmp( m, n );

    for( int i=0; i<m; ++i )
        for( int j=0; j<n; ++j )
            tmp[i][j] = sin( A[i][j] );

    return tmp;
}


template <typename Type>
Matrix<Type> tan( const Matrix<Type> &A )
{
    int m = A.rows(),
        n = A.cols();
    Matrix<Type> tmp( m, n );

    for( int i=0; i<m; ++i )
        for( int j=0; j<n; ++j )
            tmp[i][j] = tan( A[i][j] );

    return tmp;
}


template <typename Type>
Matrix<Type> acos( const Matrix<Type> &A )
{
    int m = A.rows(),
        n = A.cols();
    Matrix<Type> tmp( m, n );

    for( int i=0; i<m; ++i )
        for( int j=0; j<n; ++j )
            tmp[i][j] = acos( A[i][j] );

    return tmp;
}


template <typename Type>
Matrix<Type> asin( const Matrix<Type> &A )
{
    int m = A.rows(),
        n = A.cols();
    Matrix<Type> tmp( m, n );

    for( int i=0; i<m; ++i )
        for( int j=0; j<n; ++j )
            tmp[i][j] = asin( A[i][j] );

    return tmp;
}


template <typename Type>
Matrix<Type> atan( const Matrix<Type> &A )
{
    int m = A.rows(),
        n = A.cols();
    Matrix<Type> tmp( m, n );

    for( int i=0; i<m; ++i )
        for( int j=0; j<n; ++j )
            tmp[i][j] = atan( A[i][j] );

    return tmp;
}


template <typename Type>
Matrix<Type> exp( const Matrix<Type> &A )
{
    int m = A.rows(),
        n = A.cols();
    Matrix<Type> tmp( m, n );

    for( int i=0; i<m; ++i )
        for( int j=0; j<n; ++j )
            tmp[i][j] = exp( A[i][j] );

    return tmp;
}


template <typename Type>
Matrix<Type> log( const Matrix<Type> &A )
{
    int m = A.rows(),
        n = A.cols();
    Matrix<Type> tmp( m, n );

    for( int i=0; i<m; ++i )
        for( int j=0; j<n; ++j )
            tmp[i][j] = log( A[i][j] );

    return tmp;
}


template <typename Type>
Matrix<Type> log10( const Matrix<Type> &A )
{
    int m = A.rows(),
        n = A.cols();
    Matrix<Type> tmp( m, n );

    for( int i=0; i<m; ++i )
        for( int j=0; j<n; ++j )
            tmp[i][j] = log10( A[i][j] );

    return tmp;
}


template <typename Type>
Matrix<Type> sqrt( const Matrix<Type> &A )
{
    int m = A.rows(),
        n = A.cols();
    Matrix<Type> tmp( m, n );

    for( int i=0; i<m; ++i )
        for( int j=0; j<n; ++j )
            tmp[i][j] = sqrt( A[i][j] );

    return tmp;
}


template <typename Type>
Matrix<Type> pow( const Matrix<Type> &B, const Matrix<Type> &E )
{
    int m = B.rows(),
        n = B.cols();
    assert( m == E.rows() );
    assert( n == E.cols() );

    Matrix<Type> tmp( m, n );
    for( int i=0; i<m; ++i )
        for( int j=0; j<n; ++j )
            tmp[i][j] = pow( B[i][j], E[i][j] );

    return tmp;
}


template <typename Type>
Matrix<Type> pow( const Matrix<Type> &B, const Type &e )
{
    int m = B.rows(),
        n = B.cols();
    Matrix<Type> tmp( m, n );

    for( int i=0; i<m; ++i )
        for( int j=0; j<n; ++j )
            tmp[i][j] = pow( B[i][j], e );

    return tmp;
}


template <typename Type>
Matrix<Type> pow( const Type &b, const Matrix<Type> &E )
{
    int m = E.rows(),
        n = E.cols();
    Matrix<Type> tmp( m, n );

    for( int i=0; i<m; ++i )
        for( int j=0; j<n; ++j )
            tmp[i][j] = pow( b, E[i][j] );

    return tmp;
}

测试代码:

/*****************************************************************************
 *                             matrixmath_test.cpp
 *
 * Math functions of matrix testing.
 *
 * Zhang Ming, 2010-08, Xi'an Jiaotong University.
 *****************************************************************************/


#define BOUNDS_CHECK

#include <iostream>
#include <iomanip>
#include <matrixmath.h>


using namespace std;
using namespace splab;


typedef double  Type;


int main()
{
    int N = 9;
	Type  a = 0, b = 2*PI;
	Vector<Type> array = linspace( a, b, N );

	Matrix<Type> x( 3, 3, array );

	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;

	return 0;
}

运行结果:

size: 3 by 3
0.0000  0.7854  1.5708
2.3562  3.1416  3.9270
4.7124  5.4978  6.2832

sin of x : size: 3 by 3
0.0000  0.7071  1.0000
0.7071  0.0000  -0.7071
-1.0000 -0.7071 -0.0000

cos of x : size: 3 by 3
1.0000  0.7071  0.0000
-0.7071 -1.0000 -0.7071
-0.0000 0.7071  1.0000

tan of x : size: 3 by 3
0.0000  1.0000  16331778728383844.0000
-1.0000 -0.0000 1.0000
5443926242794615.0000   -1.0000 -0.0000

asin of x : size: 3 by 3
0.0000  0.9033  nan
nan     nan     nan
nan     nan     nan

acos of x : size: 3 by 3
1.5708  0.6675  nan
nan     nan     nan
nan     nan     nan

atan of x : size: 3 by 3
0.0000  0.6658  1.0039
1.1694  1.2626  1.3214
1.3617  1.3909  1.4130

exp of x : size: 3 by 3
1.0000  2.1933  4.8105
10.5507 23.1407 50.7540
111.3178        244.1511        535.4917

log of x : size: 3 by 3
-inf    -0.2416 0.4516
0.8570  1.1447  1.3679
1.5502  1.7043  1.8379

log10 of x : size: 3 by 3
-inf    -0.1049 0.1961
0.3722  0.4971  0.5941
0.6732  0.7402  0.7982

sqrt of x : size: 3 by 3
0.0000  0.8862  1.2533
1.5350  1.7725  1.9817
2.1708  2.3447  2.5066

pow of x : size: 3 by 3
1.0000  0.8272  2.0327
7.5336  36.4622 215.2126
1487.9012       11732.6371      103540.9204

pow of x : size: 3 by 3
0.0000  0.6169  2.4674
5.5517  9.8696  15.4213
22.2066 30.2257 39.4784

pow of x : size: 3 by 3
1.0000  1.7236  2.9707
5.1202  8.8250  15.2104
26.2162 45.1855 77.8802


Process returned 0 (0x0)   execution time : 0.109 s
Press any key to continue.

你可能感兴趣的:(常用数学函数矩阵版本的C++实现)