From f65ea9ccbda3dd57ddad131c6b2cd3972f6d80d2 Mon Sep 17 00:00:00 2001 From: Samer Afach Date: Mon, 17 Oct 2016 18:11:29 +0200 Subject: [PATCH] First commit of Polymath --- PolymathTest.pro | 36 + README.md | 0 include/Polymath.h | 6 + include/internal/LapackAdapters.h | 56 + include/internal/Matrix.h | 1838 +++++++++++++++++++++++++++++ include/internal/StdAdapters.h | 344 ++++++ src/LapackAdapters.cpp | 1 + src/Matrix.cpp | 29 + src/Polymath.cpp | 1 + src/StdAdapters.cpp | 1 + tests/tests.cpp | 50 + tests/tests.h | 84 ++ 12 files changed, 2446 insertions(+) create mode 100644 PolymathTest.pro create mode 100644 README.md create mode 100644 include/Polymath.h create mode 100644 include/internal/LapackAdapters.h create mode 100644 include/internal/Matrix.h create mode 100644 include/internal/StdAdapters.h create mode 100644 src/LapackAdapters.cpp create mode 100644 src/Matrix.cpp create mode 100644 src/Polymath.cpp create mode 100644 src/StdAdapters.cpp create mode 100644 tests/tests.cpp create mode 100644 tests/tests.h diff --git a/PolymathTest.pro b/PolymathTest.pro new file mode 100644 index 0000000..00500a3 --- /dev/null +++ b/PolymathTest.pro @@ -0,0 +1,36 @@ +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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/include/Polymath.h b/include/Polymath.h new file mode 100644 index 0000000..ad82dff --- /dev/null +++ b/include/Polymath.h @@ -0,0 +1,6 @@ +#ifndef POLYMATH_H +#define POLYMATH_H + +#include "internal/Matrix.h" + +#endif // POLYMATH_H diff --git a/include/internal/LapackAdapters.h b/include/internal/LapackAdapters.h new file mode 100644 index 0000000..2b646e3 --- /dev/null +++ b/include/internal/LapackAdapters.h @@ -0,0 +1,56 @@ +#ifndef LAPACKADAPTER_H +#define LAPACKADAPTER_H + +#include + +#ifndef _CONCAT +#define _CONCAT(A, B) A ## B +#endif + +//#ifndef __INTEL_COMPILER +typedef int lapack_int; +typedef std::complex lapack_complex_float; +typedef std::complex 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(_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 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(size_var); \ +std::unique_ptr 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 diff --git a/include/internal/Matrix.h b/include/internal/Matrix.h new file mode 100644 index 0000000..97f7d56 --- /dev/null +++ b/include/internal/Matrix.h @@ -0,0 +1,1838 @@ +/* + * File: Matrix.h + * Author: Samer Afach + * + * Created on 01. Oktober 2016, 17:12 + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "LapackAdapters.h" +#include "StdAdapters.h" + + +#ifndef MATRIX_H +#define MATRIX_H + +#define ColMaj 0 +#define RowMaj 1 + + +/** + This class is an implementation of a matrix in mathematics. It can be used as a vector implementation as well. + */ + +namespace Poly +{ +extern std::string ComplexUnit; + +template +class Matrix; +} + + +namespace std +{ +template +std::string to_string(std::complex value) +{ + std::stringstream sstr; + + sstr << value.real(); + sstr << "+"; + sstr << value.imag(); + sstr << Poly::ComplexUnit; + return sstr.str(); +} + +} + +namespace Poly +{ + +template +struct ExtractType; + +template