📄 mat_matrix.cpp
字号:
///////////////////////////////////////////////////////////
// //
// SAGA //
// //
// System for Automated Geoscientific Analyses //
// //
// Application Programming Interface //
// //
// Library: SAGA_API //
// //
//-------------------------------------------------------//
// //
// mat_matrix.cpp //
// //
// Copyright (C) 2005 by Olaf Conrad //
// //
//-------------------------------------------------------//
// //
// This file is part of 'SAGA - System for Automated //
// Geoscientific Analyses'. //
// //
// This library is free software; you can redistribute //
// it and/or modify it under the terms of the GNU Lesser //
// General Public License as published by the Free //
// Software Foundation, version 2.1 of the License. //
// //
// 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 Lesser General Public //
// License for more details. //
// //
// You should have received a copy of the GNU Lesser //
// General Public License along with this program; if //
// not, write to the Free Software Foundation, Inc., //
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, //
// USA. //
// //
//-------------------------------------------------------//
// //
// contact: Olaf Conrad //
// Institute of Geography //
// University of Goettingen //
// Goldschmidtstr. 5 //
// 37077 Goettingen //
// Germany //
// //
// e-mail: oconrad@saga-gis.org //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
#include <memory.h>
#include "mat_tools.h"
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool SG_Matrix_LU_Decomposition (int n, int *Permutation, double **Matrix, bool bSilent);
bool SG_Matrix_LU_Solve (int n, int *Permutation, double **Matrix, double *Vector, bool bSilent);
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
CSG_Vector::CSG_Vector(void)
{
_On_Construction();
}
//---------------------------------------------------------
CSG_Vector::CSG_Vector(const CSG_Vector &Vector)
{
_On_Construction();
Create(Vector);
}
bool CSG_Vector::Create(const CSG_Vector &Vector)
{
return( Assign(Vector) );
}
//---------------------------------------------------------
CSG_Vector::CSG_Vector(int n, double *Data)
{
_On_Construction();
Create(n, Data);
}
bool CSG_Vector::Create(int n, double *Data)
{
if( n > 0 )
{
if( n != m_n )
{
Destroy();
m_n = n;
m_z = (double *)SG_Malloc(m_n * sizeof(double));
}
if( Data )
{
memcpy(m_z, Data, m_n * sizeof(double));
}
else
{
memset(m_z, 0, m_n * sizeof(double));
}
return( true );
}
Destroy();
return( false );
}
//---------------------------------------------------------
CSG_Vector::~CSG_Vector(void)
{
Destroy();
}
bool CSG_Vector::Destroy(void)
{
if( m_z )
{
SG_Free(m_z);
m_z = NULL;
m_n = 0;
}
return( true );
}
//---------------------------------------------------------
void CSG_Vector::_On_Construction(void)
{
m_z = NULL;
m_n = 0;
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
CSG_String CSG_Vector::asString(void)
{
CSG_String s;
for(int i=0; i<m_n; i++)
{
s.Append(CSG_String::Format(SG_T("%f\n"), m_z[i]));
}
return( s );
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CSG_Vector::is_Equal(const CSG_Vector &Vector) const
{
if( m_n == Vector.m_n )
{
for(int i=0; i<m_n; i++)
{
if( m_z[i] != Vector.m_z[i] )
{
return( false );
}
}
return( true );
}
return( false );
}
//---------------------------------------------------------
bool CSG_Vector::Assign(double Scalar)
{
if( m_n > 0 )
{
for(int i=0; i<m_n; i++)
{
m_z[i] = Scalar;
}
return( true );
}
return( false );
}
//---------------------------------------------------------
bool CSG_Vector::Assign(const CSG_Vector &Vector)
{
if( Create(Vector.m_n) )
{
memcpy(m_z, Vector.m_z, m_n * sizeof(double));
return( true );
}
return( false );
}
//---------------------------------------------------------
bool CSG_Vector::Add(double Scalar)
{
if( m_n > 0 )
{
for(int i=0; i<m_n; i++)
{
m_z[i] += Scalar;
}
return( true );
}
return( false );
}
//---------------------------------------------------------
bool CSG_Vector::Add(const class CSG_Vector &Vector)
{
if( m_n == Vector.m_n && m_n > 0 )
{
for(int i=0; i<m_n; i++)
{
m_z[i] += Vector.m_z[i];
}
return( true );
}
return( false );
}
//---------------------------------------------------------
bool CSG_Vector::Subtract(const class CSG_Vector &Vector)
{
if( m_n == Vector.m_n && m_n > 0 )
{
for(int i=0; i<m_n; i++)
{
m_z[i] -= Vector.m_z[i];
}
return( true );
}
return( false );
}
//---------------------------------------------------------
bool CSG_Vector::Multiply(double Scalar)
{
if( m_n > 0 )
{
for(int i=0; i<m_n; i++)
{
m_z[i] *= Scalar;
}
return( true );
}
return( false );
}
//---------------------------------------------------------
bool CSG_Vector::Multiply(const class CSG_Vector &Vector)
{
if( m_n == Vector.m_n && m_n == 3 )
{
CSG_Vector v(*this);
m_z[0] = v[1] * Vector.m_z[2] - v[2] * Vector.m_z[1];
m_z[1] = v[2] * Vector.m_z[0] - v[0] * Vector.m_z[2];
m_z[2] = v[0] * Vector.m_z[1] - v[1] * Vector.m_z[0];
return( true );
}
return( false );
}
//---------------------------------------------------------
double CSG_Vector::Multiply_Scalar(const class CSG_Vector &Vector) const
{
double z = 0.0;
if( m_n == Vector.m_n )
{
for(int i=0; i<m_n; i++)
{
z += m_z[i] * Vector.m_z[i];
}
}
return( z );
}
//---------------------------------------------------------
bool CSG_Vector::Multiply(const CSG_Matrix &Matrix)
{
return( Assign(Matrix.Multiply(*this)) );
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CSG_Vector::operator == (const CSG_Vector &Vector) const
{
return( is_Equal(Vector) );
}
//---------------------------------------------------------
CSG_Vector & CSG_Vector::operator = (double Scalar)
{
Assign(Scalar);
return( *this );
}
CSG_Vector & CSG_Vector::operator = (const CSG_Vector &Vector)
{
Assign(Vector);
return( *this );
}
//---------------------------------------------------------
CSG_Vector & CSG_Vector::operator += (double Scalar)
{
Add(Scalar);
return( *this );
}
CSG_Vector & CSG_Vector::operator += (const class CSG_Vector &Vector)
{
Add(Vector);
return( *this );
}
//---------------------------------------------------------
CSG_Vector & CSG_Vector::operator -= (double Scalar)
{
Add(-Scalar);
return( *this );
}
CSG_Vector & CSG_Vector::operator -= (const class CSG_Vector &Vector)
{
Subtract(Vector);
return( *this );
}
//---------------------------------------------------------
CSG_Vector & CSG_Vector::operator *= (double Scalar)
{
Multiply(Scalar);
return( *this );
}
CSG_Vector & CSG_Vector::operator *= (const class CSG_Vector &Vector)
{
Multiply(Vector);
return( *this );
}
CSG_Vector & CSG_Vector::operator *= (const CSG_Matrix &Matrix)
{
Multiply(Matrix);
return( *this );
}
//---------------------------------------------------------
CSG_Vector CSG_Vector::operator + (double Scalar) const
{
CSG_Vector v(*this);
v.Add(Scalar);
return( v );
}
CSG_Vector CSG_Vector::operator + (const class CSG_Vector &Vector) const
{
CSG_Vector v(*this);
v.Add(Vector);
return( v );
}
//---------------------------------------------------------
CSG_Vector CSG_Vector::operator - (double Scalar) const
{
CSG_Vector v(*this);
v.Add(-Scalar);
return( v );
}
CSG_Vector CSG_Vector::operator - (const class CSG_Vector &Vector) const
{
CSG_Vector v(*this);
v.Subtract(Vector);
return( v );
}
//---------------------------------------------------------
CSG_Vector CSG_Vector::operator * (double Scalar) const
{
CSG_Vector v(*this);
v.Multiply(Scalar);
return( v );
}
double CSG_Vector::operator * (const class CSG_Vector &Vector) const
{
return( Multiply_Scalar(Vector) );
}
CSG_Vector operator * (double Scalar, const CSG_Vector &Vector)
{
return( Vector * Scalar );
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CSG_Vector::Set_Zero(void)
{
return( Create(m_n) );
}
//---------------------------------------------------------
bool CSG_Vector::Set_Unity(void)
{
double Length;
if( (Length = Get_Length()) > 0.0 )
{
for(int i=0; i<m_n; i++)
{
m_z[i] /= Length;
}
return( true );
}
return( false );
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
double CSG_Vector::Get_Length(void) const
{
if( m_n > 0 )
{
double z = 0.0;
for(int i=0; i<m_n; i++)
{
z += m_z[i] * m_z[i];
}
return( sqrt(z) );
}
return( 0.0 );
}
//---------------------------------------------------------
double CSG_Vector::Get_Angle(const CSG_Vector &Vector) const
{
if( m_n > Vector.m_n )
{
return( Vector.Get_Angle(*this) );
}
int i;
double A, B, z;
if( (A = Get_Length()) > 0.0 && (B = Vector.Get_Length()) > 0.0 )
{
for(i=0, z=0.0; i<m_n; i++)
{
z += Vector.m_z[i] * m_z[i];
}
for(i=m_n; i<Vector.m_n; i++)
{
z += Vector.m_z[i];
}
return( acos(z / (A * B)) );
}
return( 0.0 );
}
//---------------------------------------------------------
CSG_Vector CSG_Vector::Get_Unity(void) const
{
CSG_Vector v(*this);
v.Set_Unity();
return( v );
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
CSG_Matrix::CSG_Matrix(void)
{
_On_Construction();
}
//---------------------------------------------------------
CSG_Matrix::CSG_Matrix(const CSG_Matrix &Matrix)
{
_On_Construction();
Create(Matrix);
}
bool CSG_Matrix::Create(const CSG_Matrix &Matrix)
{
return( Assign(Matrix) );
}
//---------------------------------------------------------
CSG_Matrix::CSG_Matrix(int nx, int ny, double *Data)
{
_On_Construction();
Create(nx, ny, Data);
}
bool CSG_Matrix::Create(int nx, int ny, double *Data)
{
if( nx > 0 && ny > 0 )
{
if( nx != m_nx || ny != m_ny )
{
m_nx = nx;
m_ny = ny;
m_z = (double **)SG_Malloc(m_ny * sizeof(double *));
m_z[0] = (double *)SG_Malloc(m_ny * m_nx * sizeof(double ));
for(ny=1; ny<m_ny; ny++)
{
m_z[ny] = m_z[ny - 1] + nx;
}
}
if( Data )
{
memcpy(m_z[0], Data, m_ny * m_nx * sizeof(double));
}
else
{
memset(m_z[0], 0, m_ny * m_nx * sizeof(double));
}
return( true );
}
Destroy();
return( false );
}
//---------------------------------------------------------
CSG_Matrix::~CSG_Matrix(void)
{
Destroy();
}
bool CSG_Matrix::Destroy(void)
{
if( m_z )
{
SG_Free(m_z[0]);
SG_Free(m_z);
m_z = NULL;
m_nx = 0;
m_ny = 0;
}
return( true );
}
//---------------------------------------------------------
void CSG_Matrix::_On_Construction(void)
{
m_z = NULL;
m_nx = 0;
m_ny = 0;
}
///////////////////////////////////////////////////////////
// //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -