📄 ap.h
字号:
/********************************************************************
AP Library version 1.1
Copyright (c) 2003-2007, Sergey Bochkanov (ALGLIB project).
See www.alglib.net or alglib.sources.ru for details.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer listed
in this license in the documentation and/or other materials
provided with the distribution.
- Neither the name of the copyright holders nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************/
#ifndef AP_H
#define AP_H
#include <stdlib.h>
#include <string>
#include <math.h>
/********************************************************************
Array bounds check
********************************************************************/
#define AP_ASSERT
#ifndef AP_ASSERT //
#define NO_AP_ASSERT // This code avoids definition of the
#endif // both AP_ASSERT and NO_AP_ASSERT symbols
#ifdef NO_AP_ASSERT //
#ifdef AP_ASSERT //
#undef NO_AP_ASSERT //
#endif //
#endif //
/********************************************************************
Current environment.
********************************************************************/
#ifndef AP_WIN32
#ifndef AP_UNKNOWN
#define AP_UNKNOWN
#endif
#endif
#ifdef AP_WIN32
#ifdef AP_UNKNOWN
#error Multiple environments are declared!
#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
{
/********************************************************************
Templates for vector operations
********************************************************************/
#include "apvt.h"
/********************************************************************
Service routines:
amalloc - allocates an aligned block of size bytes
afree - frees block allocated by amalloc
vlen - just alias for n2-n1+1
********************************************************************/
void* amalloc(size_t size, size_t alignment);
void afree(void *block);
int vlen(int n1, int n2);
/********************************************************************
Exception class.
********************************************************************/
class ap_error
{
public:
ap_error(){};
ap_error(const char *s){ msg = s; };
std::string msg;
static void make_assertion(bool bClause)
{ if(!bClause) throw ap_error(); };
static void make_assertion(bool bClause, const char *msg)
{ if(!bClause) throw ap_error(msg); };
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);
/********************************************************************
BLAS functions
********************************************************************/
double vdotproduct(const double *v1, const double *v2, int N);
complex vdotproduct(const complex *v1, const complex *v2, int N);
void vmove(double *vdst, const double* vsrc, int N);
void vmove(complex *vdst, const complex* vsrc, int N);
void vmoveneg(double *vdst, const double *vsrc, int N);
void vmoveneg(complex *vdst, const complex *vsrc, int N);
void vmove(double *vdst, const double *vsrc, int N, double alpha);
void vmove(complex *vdst, const complex *vsrc, int N, double alpha);
void vmove(complex *vdst, const complex *vsrc, int N, complex alpha);
void vadd(double *vdst, const double *vsrc, int N);
void vadd(complex *vdst, const complex *vsrc, int N);
void vadd(double *vdst, const double *vsrc, int N, double alpha);
void vadd(complex *vdst, const complex *vsrc, int N, double alpha);
void vadd(complex *vdst, const complex *vsrc, int N, complex alpha);
void vsub(double *vdst, const double *vsrc, int N);
void vsub(complex *vdst, const complex *vsrc, int N);
void vsub(double *vdst, const double *vsrc, int N, double alpha);
void vsub(complex *vdst, const complex *vsrc, int N, double alpha);
void vsub(complex *vdst, const complex *vsrc, int N, complex alpha);
void vmul(double *vdst, int N, double alpha);
void vmul(complex *vdst, int N, double alpha);
void vmul(complex *vdst, int N, complex alpha);
/********************************************************************
Template of a dynamical one-dimensional array
********************************************************************/
template<class T, bool Aligned = false>
class template_1d_array
{
public:
template_1d_array()
{
m_Vec=0;
m_iVecSize = 0;
m_iLow = 0;
m_iHigh = -1;
};
~template_1d_array()
{
if(m_Vec)
{
if( Aligned )
ap::afree(m_Vec);
else
delete[] m_Vec;
}
};
template_1d_array(const template_1d_array &rhs)
{
m_Vec=0;
m_iVecSize = 0;
m_iLow = 0;
m_iHigh = -1;
if( rhs.m_iVecSize!=0 )
setcontent(rhs.m_iLow, rhs.m_iHigh, rhs.getcontent());
};
const template_1d_array& operator=(const template_1d_array &rhs)
{
if( this==&rhs )
return *this;
if( rhs.m_iVecSize!=0 )
setcontent(rhs.m_iLow, rhs.m_iHigh, rhs.getcontent());
else
{
m_Vec=0;
m_iVecSize = 0;
m_iLow = 0;
m_iHigh = -1;
}
return *this;
};
const T& operator()(int i) const
{
#ifndef NO_AP_ASSERT
ap_error::make_assertion(i>=m_iLow && i<=m_iHigh);
#endif
return m_Vec[ i-m_iLow ];
};
T& operator()(int i)
{
#ifndef NO_AP_ASSERT
ap_error::make_assertion(i>=m_iLow && i<=m_iHigh);
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -