Commit f65ea9cc authored by Samer Afach's avatar Samer Afach
Browse files

First commit of Polymath

parents
QT += core
QT -= gui
CONFIG += c++11
TARGET = bin/tests
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
QMAKE_CXXFLAGS += -std=c++11 -pedantic-errors
SOURCES += \
tests/tests.cpp \
src/Polymath.cpp \
src/Matrix.cpp \
src/LapackAdapters.cpp
HEADERS += \
tests/tests.h \
include/Polymath.h \
include/internal/Matrix.h \
include/internal/LapackAdapters.h
INCLUDEPATH += include
INCLUDEPATH += tests
INCLUDEPATH += /usr/include/python3.4
LIBS += -lrt -lm -lpthread -fopenmp -lgsl -lgslcblas -lgfortran -llapack -lblas
LIBS += -larmadillo
LIBS += -L/usr/lib/python3.4/config-3.4m-x86_64-linux-gnu/
LIBS += -lpython3.4
#DEFINES += POLYMATH_DEBUG
#ifndef POLYMATH_H
#define POLYMATH_H
#include "internal/Matrix.h"
#endif // POLYMATH_H
#ifndef LAPACKADAPTER_H
#define LAPACKADAPTER_H
#include <complex>
#ifndef _CONCAT
#define _CONCAT(A, B) A ## B
#endif
//#ifndef __INTEL_COMPILER
typedef int lapack_int;
typedef std::complex<float> lapack_complex_float;
typedef std::complex<double> lapack_complex_double;
extern "C" void sgetrf_( lapack_int* m, lapack_int* n, float* a, lapack_int* lda, lapack_int* ipiv, lapack_int *info );
extern "C" void dgetrf_( lapack_int* m, lapack_int* n, double* a, lapack_int* lda, lapack_int* ipiv, lapack_int *info );
extern "C" void cgetrf_( lapack_int* m, lapack_int* n, lapack_complex_float* a, lapack_int* lda, lapack_int* ipiv, lapack_int *info );
extern "C" void zgetrf_( lapack_int* m, lapack_int* n, lapack_complex_double* a, lapack_int* lda, lapack_int* ipiv, lapack_int *info );
extern "C" void sgetri_( lapack_int* n, float* a, lapack_int* lda, lapack_int* ipiv, float* work, lapack_int* lwork, lapack_int *info );
extern "C" void dgetri_( lapack_int* n, double* a, lapack_int* lda, lapack_int* ipiv, double* work, lapack_int* lwork, lapack_int *info );
extern "C" void cgetri_( lapack_int* n, lapack_complex_float* a, lapack_int* lda, lapack_int* ipiv, lapack_complex_float* work, lapack_int* lwork, lapack_int *info );
extern "C" void zgetri_( lapack_int* n, lapack_complex_double* a, lapack_int* lda, lapack_int* ipiv, lapack_complex_double* work, lapack_int* lwork, lapack_int *info );
extern "C" void sspsv_( char* uplo, lapack_int* n, lapack_int* nrhs, float* ap, lapack_int* ipiv, float* b, lapack_int* ldb, lapack_int *info );
extern "C" void dspsv_( char* uplo, lapack_int* n, lapack_int* nrhs, double* ap, lapack_int* ipiv, double* b, lapack_int* ldb, lapack_int *info );
extern "C" void cspsv_( char* uplo, lapack_int* n, lapack_int* nrhs, lapack_complex_float* ap, lapack_int* ipiv, lapack_complex_float* b, lapack_int* ldb, lapack_int *info );
extern "C" void zspsv_( char* uplo, lapack_int* n, lapack_int* nrhs, lapack_complex_double* ap, lapack_int* ipiv, lapack_complex_double* b, lapack_int* ldb, lapack_int *info );
extern "C" void cheev_( char* jobz, char* uplo, lapack_int* n, lapack_complex_float* a, lapack_int* lda, float* w, lapack_complex_float* work, lapack_int* lwork, float* rwork, lapack_int *info );
extern "C" void zheev_( char* jobz, char* uplo, lapack_int* n, lapack_complex_double* a, lapack_int* lda, double* w, lapack_complex_double* work, lapack_int* lwork, double* rwork, lapack_int *info );
extern "C" void cheevd_( char* jobz, char* uplo, lapack_int* n, lapack_complex_float* a, lapack_int* lda, float* w, lapack_complex_float* work, lapack_int* lwork, float* rwork, lapack_int* lrwork, lapack_int* iwork, lapack_int* liwork, lapack_int *info );
extern "C" void zheevd_( char* jobz, char* uplo, lapack_int* n, lapack_complex_double* a, lapack_int* lda, double* w, lapack_complex_double* work, lapack_int* lwork, double* rwork, lapack_int* lrwork, lapack_int* iwork, lapack_int* liwork, lapack_int *info );
extern "C" void sgemm_(char * transa, char * transb, lapack_int * m, lapack_int * n, lapack_int * k, float* alpha, float * A, lapack_int * lda, float * B, lapack_int * ldb, float * beta, float *, lapack_int * ldc);
extern "C" void dgemm_(char * transa, char * transb, lapack_int * m, lapack_int * n, lapack_int * k, double * alpha, double * A, lapack_int * lda, double * B, lapack_int * ldb, double * beta, double *, lapack_int * ldc);
extern "C" void cgemm_(char*,char*,lapack_int*,lapack_int*,lapack_int*, lapack_complex_float*, lapack_complex_float*, lapack_int*, lapack_complex_float*,lapack_int*, lapack_complex_float*, lapack_complex_float*,lapack_int*);
extern "C" void zgemm_(char*, char*, lapack_int*, lapack_int*, lapack_int*, lapack_complex_double*, lapack_complex_double*,lapack_int*, lapack_complex_double*,lapack_int*, lapack_complex_double*,lapack_complex_double*,lapack_int*);
//#endif
#define Xgetri(TYPE,func_prefix,size_var) \
typedef TYPE TP; \
lapack_int n = static_cast<lapack_int>(_rows); \
lapack_int info = 0; \
lapack_int workspace_size = -1; \
lapack_int lda = std::max({1,n}); \
TP tmp_workspace_size = 0; \
std::unique_ptr<lapack_int[]> ipiv(new lapack_int[std::max({1,n})]); \
_CONCAT(func_prefix,getrf_(&n, &n, (TP*)&_matEls.front(), &lda, ipiv.get(), &info )); \
_CONCAT(func_prefix,getri_(&n, (TP*)&_matEls.front(), &lda, ipiv.get(), &tmp_workspace_size, &workspace_size, &info )); \
workspace_size = static_cast<lapack_int>(size_var); \
std::unique_ptr<TP[]> workspace(new TP[workspace_size]); \
_CONCAT(func_prefix,getri_(&n, (TP*)&_matEls.front(), &lda, ipiv.get(), workspace.get(), &workspace_size, &info )); \
if(info != 0) \
{ \
throw std::runtime_error("Unable to invert the matrix."); \
}
#endif // LAPACKADAPTER_H
This diff is collapsed.
#ifndef STDADAPTERS_H
#define STDADAPTERS_H
#include <functional>
namespace Poly
{
template <typename T, int M>
class Matrix;
}
namespace std
{
#ifndef _CONCAT
#define _CONCAT(A, B) A ## B
#endif
#define _UNARY_STD_ADAPTER(func_name) \
template <typename T, int M> \
Poly::Matrix<T,M> func_name(const Poly::Matrix<T,M>& mat) \
{ \
Poly::Matrix<T,M> res(mat.rows(), mat.columns()); \
std::transform(mat.begin(),mat.end(),res.begin(),std::pointer_to_unary_function<T,T>(std::ceil)); \
return res; \
}
#define _UNARY_STD_ADAPTER_INPLACE(func_name) \
template <typename T, int M> \
void _CONCAT(func_name, _inplace)(Poly::Matrix<T,M>& mat) \
{ \
std::transform(mat.begin(),mat.end(),mat.begin(),std::pointer_to_unary_function<T,T>(std::ceil)); \
}
#define _BINARY_STD_ADAPTER(func_name) \
template <typename T, typename U, int M> \
Poly::Matrix<T,M> func_name(const Poly::Matrix<T,M>& mat1st, const Poly::Matrix<U,M>& mat2nd) \
{ \
Poly::Matrix<T,M> res(mat1st.rows(), mat2nd.columns()); \
std::transform(mat1st.begin(),mat1st.end(),mat2nd.begin(),res.begin(),(T(*)(T,U)) std::func_name); \
return res; \
}
#define _BINARY_STD_ADAPTER_BIND1ST(func_name) \
template <typename T, typename U, int M> \
Poly::Matrix<T,M> func_name(T val, const Poly::Matrix<U,M>& mat2nd) \
{ \
Poly::Matrix<T,M> res(mat2nd.rows(), mat2nd.columns()); \
std::transform(mat2nd.begin(),mat2nd.end(),res.begin(),[&val](const U& y) { return std::func_name(val,y); }); \
return res; \
}
#define _BINARY_STD_ADAPTER_BIND2ND(func_name) \
template <typename T, typename U, int M> \
Poly::Matrix<T,M> func_name(const Poly::Matrix<T,M>& mat1st, U val) \
{ \
Poly::Matrix<T,M> res(mat1st.rows(), mat1st.columns()); \
std::transform(mat1st.begin(),mat1st.end(),res.begin(),[&val](const T& x) { return std::func_name(x,val); }); \
return res; \
}
#define _BINARY_STD_ADAPTER_BIND1ST_INPLACE(func_name) \
template <typename T, typename U, int M> \
void _CONCAT(func_name, _inplace)(T val, Poly::Matrix<U,M>& mat2nd) \
{ \
std::transform(mat2nd.begin(),mat2nd.end(),mat2nd.begin(),[&val](const U& y) { return std::func_name(val,y); }); \
}
#define _BINARY_STD_ADAPTER_BIND2ND_INPLACE(func_name) \
template <typename T, typename U, int M> \
void _CONCAT(func_name, _inplace)(Poly::Matrix<T,M>& mat1st, U val) \
{ \
std::transform(mat1st.begin(),mat1st.end(),mat1st.begin(),[&val](const T& x) { return std::func_name(x,val); }); \
}
#define _BINARY_STD_ADAPTER_INPLACE_1ST(func_name) \
template <typename T, typename U, int M> \
void _CONCAT(func_name, _inplace1st)(Poly::Matrix<T,M>& mat1st_and_out, const Poly::Matrix<U,M>& mat2nd) \
{ \
std::transform(mat1st_and_out.begin(),mat1st_and_out.end(),mat2nd.begin(),mat1st_and_out.begin(),(T(*)(T,U)) std::func_name); \
}
#define _BINARY_STD_ADAPTER_INPLACE_2ND(func_name) \
template <typename T, typename U, int M> \
void _CONCAT(func_name, _inplace2nd)(const Poly::Matrix<T,M>& mat1st, Poly::Matrix<U,M>& mat2nd_and_out) \
{ \
std::transform(mat1st.begin(),mat1st.end(),mat2nd_and_out.begin(),mat2nd_and_out.begin(),(T(*)(T,U)) std::func_name); \
}
_UNARY_STD_ADAPTER(abs)
_UNARY_STD_ADAPTER(acos)
_UNARY_STD_ADAPTER(acosh)
_UNARY_STD_ADAPTER(asin)
_UNARY_STD_ADAPTER(asinh)
_UNARY_STD_ADAPTER(atan)
_UNARY_STD_ADAPTER(atanh)
_UNARY_STD_ADAPTER(cbrt)
_UNARY_STD_ADAPTER(ceil)
_UNARY_STD_ADAPTER(cos)
_UNARY_STD_ADAPTER(cosh)
_UNARY_STD_ADAPTER(erf)
_UNARY_STD_ADAPTER(erfc)
_UNARY_STD_ADAPTER(exp)
_UNARY_STD_ADAPTER(exp2)
_UNARY_STD_ADAPTER(expm1)
_UNARY_STD_ADAPTER(fabs)
_UNARY_STD_ADAPTER(floor)
_UNARY_STD_ADAPTER(ilogb)
_UNARY_STD_ADAPTER(isalnum)
_UNARY_STD_ADAPTER(isalpha)
_UNARY_STD_ADAPTER(isblank)
_UNARY_STD_ADAPTER(iscntrl)
_UNARY_STD_ADAPTER(isdigit)
_UNARY_STD_ADAPTER(isgraph)
_UNARY_STD_ADAPTER(islower)
_UNARY_STD_ADAPTER(isnan)
_UNARY_STD_ADAPTER(isprint)
_UNARY_STD_ADAPTER(ispunct)
_UNARY_STD_ADAPTER(isspace)
_UNARY_STD_ADAPTER(isupper)
_UNARY_STD_ADAPTER(iswalnum)
_UNARY_STD_ADAPTER(iswalpha)
_UNARY_STD_ADAPTER(iswblank)
_UNARY_STD_ADAPTER(iswcntrl)
_UNARY_STD_ADAPTER(iswdigit)
_UNARY_STD_ADAPTER(iswgraph)
_UNARY_STD_ADAPTER(iswlower)
_UNARY_STD_ADAPTER(iswprint)
_UNARY_STD_ADAPTER(iswpunct)
_UNARY_STD_ADAPTER(iswspace)
_UNARY_STD_ADAPTER(iswupper)
_UNARY_STD_ADAPTER(iswxdigit)
_UNARY_STD_ADAPTER(isxdigit)
_UNARY_STD_ADAPTER(labs)
_UNARY_STD_ADAPTER(lgamma)
_UNARY_STD_ADAPTER(log)
_UNARY_STD_ADAPTER(log2)
_UNARY_STD_ADAPTER(log10)
_UNARY_STD_ADAPTER(nearbyint)
_UNARY_STD_ADAPTER(round)
_UNARY_STD_ADAPTER(sin)
_UNARY_STD_ADAPTER(sinh)
_UNARY_STD_ADAPTER(srand)
_UNARY_STD_ADAPTER(sqrt)
_UNARY_STD_ADAPTER(tan)
_UNARY_STD_ADAPTER(tanh)
_UNARY_STD_ADAPTER(tgamma)
_UNARY_STD_ADAPTER(tolower)
_UNARY_STD_ADAPTER(toupper)
_UNARY_STD_ADAPTER(towctrans)
_UNARY_STD_ADAPTER(towlower)
_UNARY_STD_ADAPTER(towupper)
_UNARY_STD_ADAPTER(trunc)
_UNARY_STD_ADAPTER(to_string)
_UNARY_STD_ADAPTER(to_wstring)
_UNARY_STD_ADAPTER(rint)
_UNARY_STD_ADAPTER(isfinite)
_UNARY_STD_ADAPTER(isinf)
_UNARY_STD_ADAPTER(isnormal)
_UNARY_STD_ADAPTER(signbit)
_UNARY_STD_ADAPTER_INPLACE(abs)
_UNARY_STD_ADAPTER_INPLACE(acos)
_UNARY_STD_ADAPTER_INPLACE(acosh)
_UNARY_STD_ADAPTER_INPLACE(asin)
_UNARY_STD_ADAPTER_INPLACE(asinh)
_UNARY_STD_ADAPTER_INPLACE(atan)
_UNARY_STD_ADAPTER_INPLACE(atanh)
_UNARY_STD_ADAPTER_INPLACE(cbrt)
_UNARY_STD_ADAPTER_INPLACE(ceil)
_UNARY_STD_ADAPTER_INPLACE(cos)
_UNARY_STD_ADAPTER_INPLACE(cosh)
_UNARY_STD_ADAPTER_INPLACE(erf)
_UNARY_STD_ADAPTER_INPLACE(erfc)
_UNARY_STD_ADAPTER_INPLACE(exp)
_UNARY_STD_ADAPTER_INPLACE(exp2)
_UNARY_STD_ADAPTER_INPLACE(expm1)
_UNARY_STD_ADAPTER_INPLACE(fabs)
_UNARY_STD_ADAPTER_INPLACE(ilogb)
_UNARY_STD_ADAPTER_INPLACE(isalnum)
_UNARY_STD_ADAPTER_INPLACE(isalpha)
_UNARY_STD_ADAPTER_INPLACE(isblank)
_UNARY_STD_ADAPTER_INPLACE(iscntrl)
_UNARY_STD_ADAPTER_INPLACE(isdigit)
_UNARY_STD_ADAPTER_INPLACE(isgraph)
_UNARY_STD_ADAPTER_INPLACE(islower)
_UNARY_STD_ADAPTER_INPLACE(isnan)
_UNARY_STD_ADAPTER_INPLACE(isprint)
_UNARY_STD_ADAPTER_INPLACE(ispunct)
_UNARY_STD_ADAPTER_INPLACE(isspace)
_UNARY_STD_ADAPTER_INPLACE(isupper)
_UNARY_STD_ADAPTER_INPLACE(iswalnum)
_UNARY_STD_ADAPTER_INPLACE(iswalpha)
_UNARY_STD_ADAPTER_INPLACE(iswblank)
_UNARY_STD_ADAPTER_INPLACE(iswcntrl)
_UNARY_STD_ADAPTER_INPLACE(iswdigit)
_UNARY_STD_ADAPTER_INPLACE(iswgraph)
_UNARY_STD_ADAPTER_INPLACE(iswlower)
_UNARY_STD_ADAPTER_INPLACE(iswprint)
_UNARY_STD_ADAPTER_INPLACE(iswpunct)
_UNARY_STD_ADAPTER_INPLACE(iswspace)
_UNARY_STD_ADAPTER_INPLACE(iswupper)
_UNARY_STD_ADAPTER_INPLACE(iswxdigit)
_UNARY_STD_ADAPTER_INPLACE(isxdigit)
_UNARY_STD_ADAPTER_INPLACE(labs)
_UNARY_STD_ADAPTER_INPLACE(lgamma)
_UNARY_STD_ADAPTER_INPLACE(log)
_UNARY_STD_ADAPTER_INPLACE(log2)
_UNARY_STD_ADAPTER_INPLACE(log10)
_UNARY_STD_ADAPTER_INPLACE(nearbyint)
_UNARY_STD_ADAPTER_INPLACE(sin)
_UNARY_STD_ADAPTER_INPLACE(sinh)
_UNARY_STD_ADAPTER_INPLACE(srand)
_UNARY_STD_ADAPTER_INPLACE(sqrt)
_UNARY_STD_ADAPTER_INPLACE(tan)
_UNARY_STD_ADAPTER_INPLACE(tanh)
_UNARY_STD_ADAPTER_INPLACE(tgamma)
_UNARY_STD_ADAPTER_INPLACE(tolower)
_UNARY_STD_ADAPTER_INPLACE(toupper)
_UNARY_STD_ADAPTER_INPLACE(towctrans)
_UNARY_STD_ADAPTER_INPLACE(towlower)
_UNARY_STD_ADAPTER_INPLACE(towupper)
_UNARY_STD_ADAPTER_INPLACE(trunc)
_UNARY_STD_ADAPTER_INPLACE(to_string)
_UNARY_STD_ADAPTER_INPLACE(to_wstring)
_UNARY_STD_ADAPTER_INPLACE(rint)
_UNARY_STD_ADAPTER_INPLACE(isfinite)
_UNARY_STD_ADAPTER_INPLACE(isinf)
_UNARY_STD_ADAPTER_INPLACE(isnormal)
_UNARY_STD_ADAPTER_INPLACE(signbit)
_BINARY_STD_ADAPTER(atan2)
_BINARY_STD_ADAPTER(remainder)
_BINARY_STD_ADAPTER(remquo)
_BINARY_STD_ADAPTER(fmax)
_BINARY_STD_ADAPTER(fmin)
_BINARY_STD_ADAPTER(fmod)
_BINARY_STD_ADAPTER(fdim)
_BINARY_STD_ADAPTER(hypot)
_BINARY_STD_ADAPTER(isgreater)
_BINARY_STD_ADAPTER(isgreaterequal)
_BINARY_STD_ADAPTER(isless)
_BINARY_STD_ADAPTER(islessequal)
_BINARY_STD_ADAPTER(islessgreater)
_BINARY_STD_ADAPTER(isunordered)
_BINARY_STD_ADAPTER(pow)
_BINARY_STD_ADAPTER_BIND1ST(atan2)
_BINARY_STD_ADAPTER_BIND1ST(remainder)
_BINARY_STD_ADAPTER_BIND1ST(remquo)
_BINARY_STD_ADAPTER_BIND1ST(fmax)
_BINARY_STD_ADAPTER_BIND1ST(fmin)
_BINARY_STD_ADAPTER_BIND1ST(fmod)
_BINARY_STD_ADAPTER_BIND1ST(fdim)
_BINARY_STD_ADAPTER_BIND1ST(hypot)
_BINARY_STD_ADAPTER_BIND1ST(isgreater)
_BINARY_STD_ADAPTER_BIND1ST(isgreaterequal)
_BINARY_STD_ADAPTER_BIND1ST(isless)
_BINARY_STD_ADAPTER_BIND1ST(islessequal)
_BINARY_STD_ADAPTER_BIND1ST(islessgreater)
_BINARY_STD_ADAPTER_BIND1ST(isunordered)
_BINARY_STD_ADAPTER_BIND1ST(pow)
_BINARY_STD_ADAPTER_BIND2ND(atan2)
_BINARY_STD_ADAPTER_BIND2ND(remainder)
_BINARY_STD_ADAPTER_BIND2ND(remquo)
_BINARY_STD_ADAPTER_BIND2ND(fmax)
_BINARY_STD_ADAPTER_BIND2ND(fmin)
_BINARY_STD_ADAPTER_BIND2ND(fmod)
_BINARY_STD_ADAPTER_BIND2ND(fdim)
_BINARY_STD_ADAPTER_BIND2ND(hypot)
_BINARY_STD_ADAPTER_BIND2ND(isgreater)
_BINARY_STD_ADAPTER_BIND2ND(isgreaterequal)
_BINARY_STD_ADAPTER_BIND2ND(isless)
_BINARY_STD_ADAPTER_BIND2ND(islessequal)
_BINARY_STD_ADAPTER_BIND2ND(islessgreater)
_BINARY_STD_ADAPTER_BIND2ND(isunordered)
_BINARY_STD_ADAPTER_BIND2ND(pow)
_BINARY_STD_ADAPTER_BIND1ST_INPLACE(atan2)
_BINARY_STD_ADAPTER_BIND1ST_INPLACE(remainder)
_BINARY_STD_ADAPTER_BIND1ST_INPLACE(remquo)
_BINARY_STD_ADAPTER_BIND1ST_INPLACE(fmax)
_BINARY_STD_ADAPTER_BIND1ST_INPLACE(fmin)
_BINARY_STD_ADAPTER_BIND1ST_INPLACE(fmod)
_BINARY_STD_ADAPTER_BIND1ST_INPLACE(fdim)
_BINARY_STD_ADAPTER_BIND1ST_INPLACE(hypot)
_BINARY_STD_ADAPTER_BIND1ST_INPLACE(isgreater)
_BINARY_STD_ADAPTER_BIND1ST_INPLACE(isgreaterequal)
_BINARY_STD_ADAPTER_BIND1ST_INPLACE(isless)
_BINARY_STD_ADAPTER_BIND1ST_INPLACE(islessequal)
_BINARY_STD_ADAPTER_BIND1ST_INPLACE(islessgreater)
_BINARY_STD_ADAPTER_BIND1ST_INPLACE(isunordered)
_BINARY_STD_ADAPTER_BIND1ST_INPLACE(pow)
_BINARY_STD_ADAPTER_BIND2ND_INPLACE(atan2)
_BINARY_STD_ADAPTER_BIND2ND_INPLACE(remainder)
_BINARY_STD_ADAPTER_BIND2ND_INPLACE(remquo)
_BINARY_STD_ADAPTER_BIND2ND_INPLACE(fmax)
_BINARY_STD_ADAPTER_BIND2ND_INPLACE(fmin)
_BINARY_STD_ADAPTER_BIND2ND_INPLACE(fmod)
_BINARY_STD_ADAPTER_BIND2ND_INPLACE(fdim)
_BINARY_STD_ADAPTER_BIND2ND_INPLACE(hypot)
_BINARY_STD_ADAPTER_BIND2ND_INPLACE(isgreater)
_BINARY_STD_ADAPTER_BIND2ND_INPLACE(isgreaterequal)
_BINARY_STD_ADAPTER_BIND2ND_INPLACE(isless)
_BINARY_STD_ADAPTER_BIND2ND_INPLACE(islessequal)
_BINARY_STD_ADAPTER_BIND2ND_INPLACE(islessgreater)
_BINARY_STD_ADAPTER_BIND2ND_INPLACE(isunordered)
_BINARY_STD_ADAPTER_BIND2ND_INPLACE(pow)
_BINARY_STD_ADAPTER_INPLACE_1ST(atan2)
_BINARY_STD_ADAPTER_INPLACE_1ST(remainder)
_BINARY_STD_ADAPTER_INPLACE_1ST(remquo)
_BINARY_STD_ADAPTER_INPLACE_1ST(fmax)
_BINARY_STD_ADAPTER_INPLACE_1ST(fmin)
_BINARY_STD_ADAPTER_INPLACE_1ST(fmod)
_BINARY_STD_ADAPTER_INPLACE_1ST(fdim)
_BINARY_STD_ADAPTER_INPLACE_1ST(hypot)
_BINARY_STD_ADAPTER_INPLACE_1ST(isgreater)
_BINARY_STD_ADAPTER_INPLACE_1ST(isgreaterequal)
_BINARY_STD_ADAPTER_INPLACE_1ST(isless)
_BINARY_STD_ADAPTER_INPLACE_1ST(islessequal)
_BINARY_STD_ADAPTER_INPLACE_1ST(islessgreater)
_BINARY_STD_ADAPTER_INPLACE_1ST(isunordered)
_BINARY_STD_ADAPTER_INPLACE_1ST(pow)
_BINARY_STD_ADAPTER_INPLACE_2ND(atan2)
_BINARY_STD_ADAPTER_INPLACE_2ND(remainder)
_BINARY_STD_ADAPTER_INPLACE_2ND(remquo)
_BINARY_STD_ADAPTER_INPLACE_2ND(fmax)
_BINARY_STD_ADAPTER_INPLACE_2ND(fmin)
_BINARY_STD_ADAPTER_INPLACE_2ND(fmod)
_BINARY_STD_ADAPTER_INPLACE_2ND(fdim)
_BINARY_STD_ADAPTER_INPLACE_2ND(hypot)
_BINARY_STD_ADAPTER_INPLACE_2ND(isgreater)
_BINARY_STD_ADAPTER_INPLACE_2ND(isgreaterequal)
_BINARY_STD_ADAPTER_INPLACE_2ND(isless)
_BINARY_STD_ADAPTER_INPLACE_2ND(islessequal)
_BINARY_STD_ADAPTER_INPLACE_2ND(islessgreater)
_BINARY_STD_ADAPTER_INPLACE_2ND(isunordered)
_BINARY_STD_ADAPTER_INPLACE_2ND(pow)
}
#endif // STDADAPTERS_H
#include "internal/LapackAdapters.h"
/*
* File: Matrix.cpp
* Author: Samer Afach
*
* Created on 01. Oktober 2016, 17:12
*/
#include "internal/Matrix.h"
namespace Poly
{
std::string ComplexUnit = "j";
std::string _lapack_eigens_exception_illegal_value(int info)
{
return std::string("Unable to compute eigenvalues. Value " + std::to_string(-info) + " had an illegal value.");
}
std::string _lapack_eigens_exception_converge_error(int info)
{
return std::string("Unable to compute eigenvalues. " + std::to_string(info) + " elements of an intermediate tridiagonal did not converge to zero.");
}
std::string _lapack_unsupported_type()
{
return std::string("LAPACK does not support the type you selected. Lapack only supports doubles, floats, complex<double> and complex<float>. If you haven't chosen these types, please contact the library writer about this error.");
}
std::string _unsupported_type(const std::string& function_name)
{
return std::string("The type you is not supported for the function " + function_name + ".");
}
}
#include "Polymath.h"
#include "StdAdapters.h"
#include <string>
#include <locale>
#include "tests.h"
int RunTests()
{
// Py_SetProgramName("MatricesTest"); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("import numpy as np");
PyRun_SimpleString("def compare_floats(f1,f2,tol):\n return np.isclose(f1,f2,rtol=tol)");
bool print = 0;
for(int i = 0; i < 10; i++)
{
// std::cout<<TestInverse<float>(2,print)<<std::endl;
for(long j = 2; j < 20; j++)
{
if(!TestInverse<float>(j,print))
{
std::cerr<<"Error testing float inverse. Try again to verify that this is not a statistical error."<<std::endl;
std::exit(1);
}
if(!TestInverse<double>(j,print))
{
std::cerr<<"Error testing double inverse. Try again to verify that this is not a statistical error."<<std::endl;
std::exit(1);
}
if(!TestInverse<std::complex<float>>(j,print))
{
std::cerr<<"Error testing complex float inverse. Try again to verify that this is not a statistical error."<<std::endl;
std::exit(1);
}
if(!TestInverse<std::complex<double>>(j,print))
{
std::cerr<<"Error testing complex double inverse. Try again to verify that this is not a statistical error."<<std::endl;
std::exit(1);
}
}
}
Py_Finalize();
return 0;
}
int main()
{
RunTests();
std::cout<<"Tests program exited with no errors."<<std::endl;
return 0;
}
#ifndef TESTS_H
#define TESTS_H
#include "Polymath.h"
#include <Python.h>
template <typename T>
int precision_per_type()
{
if(std::is_same<T,double>::value || std::is_same<T,std::complex<double>>::value)
return 10;
else if(std::is_same<T,float>::value || std::is_same<T,std::complex<float>>::value)
return 2;
else
return 2;
}
template <typename T>
std::string python_type_per_type()
{
return std::string("np.complex128");
if(std::is_same<T,double>::value)
{
return std::string("np.double");
}
else if(std::is_same<T,std::complex<double>>::value)
{
return std::string("np.complex128");
}
else if(std::is_same<T,float>::value)
{
return std::string("np.float32");
}
else if(std::is_same<T,std::complex<float>>::value)
{
return std::string("np.complex64");
}
else
{
return std::string("np.complex128");
}
}
template <typename T>
bool TestInverse(int len, bool print)
{
Poly::Matrix<T> mat_d = Poly::RandomMatrix<T>(len,len,0,10,std::random_device{}());
if(print) std::cout<<mat_d<<std::endl;
if(print) std::cout<<mat_d.asString(32,'[',']',',')<<std::endl;
int prec = precision_per_type<T>();
auto mat_d_i = mat_d.getInverse();
PyObject *main = PyImport_AddModule("__main__");
PyRun_SimpleString(std::string("data={}").c_str());
PyRun_SimpleString(std::string("data['a']=np.matrix(" + mat_d.asString(32,'[',']',',') + ",dtype="+python_type_per_type<T>()+")").c_str());
PyRun_SimpleString(std::string("data['b_c']=np.matrix(" + mat_d_i.asString(32,'[',']',',') + ",dtype="+std::string("np.complex128")+")").c_str());
PyRun_SimpleString(std::string("data['b_p']=np.linalg.inv(data['a'])").c_str());
if(print) std::cout << mat_d.columns() << std::endl;
if(print) PyRun_SimpleString(std::string("print('Cond num: ', np.linalg.cond(data['a']))").c_str());
if(print) PyRun_SimpleString(std::string("print('Cond num: ', np.linalg.cond(data['b_c']))").c_str());
if(print) PyRun_SimpleString(std::string("print('Cond num: ', np.linalg.cond(data['b_c']))").c_str());
if(print) PyRun_SimpleString("print(data['a'])");
if(print) PyRun_SimpleString("print(data['b_c'])");
if(print) PyRun_SimpleString("print(data['b_p'])");
if(print) PyRun_SimpleString("print(abs(data['b_c']-data['b_p']))");
PyRun_SimpleString("data['fb_p']=((data['b_p']).flatten().tolist())[0]");