📄 ap.h
字号:
/********************************************************************
AP library.
See www.alglib.net or alglib.sources.ru for details.
********************************************************************/
#ifndef AP_H
#define AP_H
#include <stdlib.h>
#include <math.h>
/********************************************************************
Checking of the array boundaries mode.
********************************************************************/
//#define NO_AP_ASSERT
#define AP_ASSERT
#ifndef AP_ASSERT
#define NO_AP_ASSERT
#endif
#ifdef NO_AP_ASSERT
#ifdef AP_ASSERT
#undef NO_AP_ASSERT
#endif
#endif
/********************************************************************
This symbol is used for debugging. Do not define it and do not remove
comments.
********************************************************************/
//#define UNSAFE_MEM_COPY
/********************************************************************
Namespace of a standard library AlgoPascal.
********************************************************************/
namespace ap
{
/********************************************************************
Exception class.
********************************************************************/
class ap_error
{
public:
static void make_assertion(bool bClause)
{ if(!bClause) throw ap_error(); };
private:
};
/********************************************************************
Class defining a complex number with double precision.
********************************************************************/
class complex;
class complex
{
public:
complex():x(0.0),y(0.0){};
complex(const double &_x):x(_x),y(0.0){};
complex(const double &_x, const double &_y):x(_x),y(_y){};
complex(const complex &z):x(z.x),y(z.y){};
complex& operator= (const double& v){ x = v; y = 0.0; return *this; };
complex& operator+=(const double& v){ x += v; return *this; };
complex& operator-=(const double& v){ x -= v; return *this; };
complex& operator*=(const double& v){ x *= v; y *= v; return *this; };
complex& operator/=(const double& v){ x /= v; y /= v; return *this; };
complex& operator= (const complex& z){ x = z.x; y = z.y; return *this; };
complex& operator+=(const complex& z){ x += z.x; y += z.y; return *this; };
complex& operator-=(const complex& z){ x -= z.x; y -= z.y; return *this; };
complex& operator*=(const complex& z){ double t = x*z.x-y*z.y; y = x*z.y+y*z.x; x = t; return *this; };
complex& operator/=(const complex& z)
{
ap::complex result;
double e;
double f;
if( fabs(z.y)<fabs(z.x) )
{
e = z.y/z.x;
f = z.x+z.y*e;
result.x = (z.x+z.y*e)/f;
result.y = (z.y-z.x*e)/f;
}
else
{
e = z.x/z.y;
f = z.y+z.x*e;
result.x = (z.y+z.x*e)/f;
result.y = (-z.x+z.y*e)/f;
}
*this = result;
return *this;
};
double x, y;
};
const complex operator/(const complex& lhs, const complex& rhs);
const bool operator==(const complex& lhs, const complex& rhs);
const bool operator!=(const complex& lhs, const complex& rhs);
const complex operator+(const complex& lhs);
const complex operator-(const complex& lhs);
const complex operator+(const complex& lhs, const complex& rhs);
const complex operator+(const complex& lhs, const double& rhs);
const complex operator+(const double& lhs, const complex& rhs);
const complex operator-(const complex& lhs, const complex& rhs);
const complex operator-(const complex& lhs, const double& rhs);
const complex operator-(const double& lhs, const complex& rhs);
const complex operator*(const complex& lhs, const complex& rhs);
const complex operator*(const complex& lhs, const double& rhs);
const complex operator*(const double& lhs, const complex& rhs);
const complex operator/(const complex& lhs, const complex& rhs);
const complex operator/(const double& lhs, const complex& rhs);
const complex operator/(const complex& lhs, const double& rhs);
const double abscomplex(const complex &z);
const complex conj(const complex &z);
const complex csqr(const complex &z);
/********************************************************************
Template defining vector in memory. It is used by the basic
subroutines of linear algebra.
Vector consists of Length elements of type T, starting from an element,
which Data is pointed to. Interval between adjacent elements equals
the value of Step.
The class provides an access for reading only.
********************************************************************/
template<class T>
class const_raw_vector
{
public:
const_raw_vector(const T *Data, int Length, int Step):
pData(const_cast<T*>(Data)),iLength(Length),iStep(Step){};
const T* GetData() const
{ return pData; };
int GetLength() const
{ return iLength; };
int GetStep() const
{ return iStep; };
protected:
T *pData;
int iLength, iStep;
};
/********************************************************************
Template defining vector in memory, derived from const_raw_vector.
It is used by the basic subroutines of linear algebra.
Vector consists of Length elements of type T, starting from an element,
which Data is pointed to. Interval between adjacent elements equals
the value of Step.
The class provides an access both for reading and writing.
********************************************************************/
template<class T>
class raw_vector : public const_raw_vector<T>
{
public:
raw_vector(T *Data, int Length, int Step):const_raw_vector<T>(Data, Length, Step){};
T* GetData()
{ return const_raw_vector<T>::pData; };
};
/********************************************************************
Scalar product
********************************************************************/
template<class T>
T vdotproduct(const_raw_vector<T> v1, const_raw_vector<T> v2)
{
ap_error::make_assertion(v1.GetLength()==v2.GetLength());
if( v1.GetStep()==1 && v2.GetStep()==1 )
{
//
// fast
//
T r = 0;
const T *p1 = v1.GetData();
const T *p2 = v2.GetData();
int imax = v1.GetLength()/4;
int i;
for(i=imax; i!=0; i--)
{
r += (*p1)*(*p2) + p1[1]*p2[1] + p1[2]*p2[2] + p1[3]*p2[3];
p1+=4;
p2+=4;
}
for(i=0; i<v1.GetLength()%4; i++)
r += (*(p1++))*(*(p2++));
return r;
}
else
{
//
// general
//
int offset11 = v1.GetStep(), offset12 = 2*offset11, offset13 = 3*offset11, offset14 = 4*offset11;
int offset21 = v2.GetStep(), offset22 = 2*offset21, offset23 = 3*offset21, offset24 = 4*offset21;
T r = 0;
const T *p1 = v1.GetData();
const T *p2 = v2.GetData();
int imax = v1.GetLength()/4;
int i;
for(i=0; i<imax; i++)
{
r += (*p1)*(*p2) + p1[offset11]*p2[offset21] + p1[offset12]*p2[offset22] + p1[offset13]*p2[offset23];
p1+=offset14;
p2+=offset24;
}
for(i=0; i<v1.GetLength()%4; i++)
{
r += (*p1)*(*p2);
p1+=offset11;
p2+=offset21;
}
return r;
}
}
/********************************************************************
Copy one vector into another
********************************************************************/
template<class T>
void vmove(raw_vector<T> vdst, const_raw_vector<T> vsrc)
{
ap_error::make_assertion(vdst.GetLength()==vsrc.GetLength());
if( vdst.GetStep()==1 && vsrc.GetStep()==1 )
{
//
// fast
//
T *p1 = vdst.GetData();
const T *p2 = vsrc.GetData();
int imax = vdst.GetLength()/2;
int i;
for(i=imax; i!=0; i--)
{
*p1 = *p2;
p1[1] = p2[1];
p1 += 2;
p2 += 2;
}
if(vdst.GetLength()%2 != 0)
*p1 = *p2;
return;
}
else
{
//
// general
//
int offset11 = vdst.GetStep(), offset12 = 2*offset11, offset13 = 3*offset11, offset14 = 4*offset11;
int offset21 = vsrc.GetStep(), offset22 = 2*offset21, offset23 = 3*offset21, offset24 = 4*offset21;
T *p1 = vdst.GetData();
const T *p2 = vsrc.GetData();
int imax = vdst.GetLength()/4;
int i;
for(i=0; i<imax; i++)
{
*p1 = *p2;
p1[offset11] = p2[offset21];
p1[offset12] = p2[offset22];
p1[offset13] = p2[offset23];
p1 += offset14;
p2 += offset24;
}
for(i=0; i<vdst.GetLength()%4; i++)
{
*p1 = *p2;
p1 += vdst.GetStep();
p2 += vsrc.GetStep();
}
return;
}
}
/********************************************************************
Copy one vector multiplied by -1 into another.
********************************************************************/
template<class T>
void vmoveneg(raw_vector<T> vdst, const_raw_vector<T> vsrc)
{
ap_error::make_assertion(vdst.GetLength()==vsrc.GetLength());
if( vdst.GetStep()==1 && vsrc.GetStep()==1 )
{
//
// fast
//
T *p1 = vdst.GetData();
const T *p2 = vsrc.GetData();
int imax = vdst.GetLength()/2;
int i;
for(i=0; i<imax; i++)
{
*p1 = -*p2;
p1[1] = -p2[1];
p1 += 2;
p2 += 2;
}
if(vdst.GetLength()%2 != 0)
*p1 = -*p2;
return;
}
else
{
//
// general
//
int offset11 = vdst.GetStep(), offset12 = 2*offset11, offset13 = 3*offset11, offset14 = 4*offset11;
int offset21 = vsrc.GetStep(), offset22 = 2*offset21, offset23 = 3*offset21, offset24 = 4*offset21;
T *p1 = vdst.GetData();
const T *p2 = vsrc.GetData();
int imax = vdst.GetLength()/4;
int i;
for(i=imax; i!=0; i--)
{
*p1 = -*p2;
p1[offset11] = -p2[offset21];
p1[offset12] = -p2[offset22];
p1[offset13] = -p2[offset23];
p1 += offset14;
p2 += offset24;
}
for(i=0; i<vdst.GetLength()%4; i++)
{
*p1 = -*p2;
p1 += vdst.GetStep();
p2 += vsrc.GetStep();
}
return;
}
}
/********************************************************************
Copy one vector multiplied by a number into another vector.
********************************************************************/
template<class T, class T2>
void vmove(raw_vector<T> vdst, const_raw_vector<T> vsrc, T2 alpha)
{
ap_error::make_assertion(vdst.GetLength()==vsrc.GetLength());
if( vdst.GetStep()==1 && vsrc.GetStep()==1 )
{
//
// fast
//
T *p1 = vdst.GetData();
const T *p2 = vsrc.GetData();
int imax = vdst.GetLength()/4;
int i;
for(i=imax; i!=0; i--)
{
*p1 = alpha*(*p2);
p1[1] = alpha*p2[1];
p1[2] = alpha*p2[2];
p1[3] = alpha*p2[3];
p1 += 4;
p2 += 4;
}
for(i=0; i<vdst.GetLength()%4; i++)
*(p1++) = alpha*(*(p2++));
return;
}
else
{
//
// general
//
int offset11 = vdst.GetStep(), offset12 = 2*offset11, offset13 = 3*offset11, offset14 = 4*offset11;
int offset21 = vsrc.GetStep(), offset22 = 2*offset21, offset23 = 3*offset21, offset24 = 4*offset21;
T *p1 = vdst.GetData();
const T *p2 = vsrc.GetData();
int imax = vdst.GetLength()/4;
int i;
for(i=0; i<imax; i++)
{
*p1 = alpha*(*p2);
p1[offset11] = alpha*p2[offset21];
p1[offset12] = alpha*p2[offset22];
p1[offset13] = alpha*p2[offset23];
p1 += offset14;
p2 += offset24;
}
for(i=0; i<vdst.GetLength()%4; i++)
{
*p1 = alpha*(*p2);
p1 += vdst.GetStep();
p2 += vsrc.GetStep();
}
return;
}
}
/********************************************************************
Vector addition
********************************************************************/
template<class T>
void vadd(raw_vector<T> vdst, const_raw_vector<T> vsrc)
{
ap_error::make_assertion(vdst.GetLength()==vsrc.GetLength());
if( vdst.GetStep()==1 && vsrc.GetStep()==1 )
{
//
// fast
//
T *p1 = vdst.GetData();
const T *p2 = vsrc.GetData();
int imax = vdst.GetLength()/4;
int i;
for(i=imax; i!=0; i--)
{
*p1 += *p2;
p1[1] += p2[1];
p1[2] += p2[2];
p1[3] += p2[3];
p1 += 4;
p2 += 4;
}
for(i=0; i<vdst.GetLength()%4; i++)
*(p1++) += *(p2++);
return;
}
else
{
//
// general
//
int offset11 = vdst.GetStep(), offset12 = 2*offset11, offset13 = 3*offset11, offset14 = 4*offset11;
int offset21 = vsrc.GetStep(), offset22 = 2*offset21, offset23 = 3*offset21, offset24 = 4*offset21;
T *p1 = vdst.GetData();
const T *p2 = vsrc.GetData();
int imax = vdst.GetLength()/4;
int i;
for(i=0; i<imax; i++)
{
*p1 += *p2;
p1[offset11] += p2[offset21];
p1[offset12] += p2[offset22];
p1[offset13] += p2[offset23];
p1 += offset14;
p2 += offset24;
}
for(i=0; i<vdst.GetLength()%4; i++)
{
*p1 += *p2;
p1 += vdst.GetStep();
p2 += vsrc.GetStep();
}
return;
}
}
/********************************************************************
Add one vector multiplied by a number to another vector.
********************************************************************/
template<class T, class T2>
void vadd(raw_vector<T> vdst, const_raw_vector<T> vsrc, T2 alpha)
{
ap_error::make_assertion(vdst.GetLength()==vsrc.GetLength());
if( vdst.GetStep()==1 && vsrc.GetStep()==1 )
{
//
// fast
//
T *p1 = vdst.GetData();
const T *p2 = vsrc.GetData();
int imax = vdst.GetLength()/4;
int i;
for(i=imax; i!=0; i--)
{
*p1 += alpha*(*p2);
p1[1] += alpha*p2[1];
p1[2] += alpha*p2[2];
p1[3] += alpha*p2[3];
p1 += 4;
p2 += 4;
}
for(i=0; i<vdst.GetLength()%4; i++)
*(p1++) += alpha*(*(p2++));
return;
}
else
{
//
// general
//
int offset11 = vdst.GetStep(), offset12 = 2*offset11, offset13 = 3*offset11, offset14 = 4*offset11;
int offset21 = vsrc.GetStep(), offset22 = 2*offset21, offset23 = 3*offset21, offset24 = 4*offset21;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -