Files
Aurora/src/Matrix.cpp

356 lines
14 KiB
C++
Raw Normal View History

2023-04-18 13:31:14 +08:00
#include "Matrix.h"
#include <string>
#include <cstring>
#include <iostream>
#include <complex>
//必须在mkl.h和Eigen的头之前<complex>之后
#define MKL_Complex16 std::complex<double>
#include "mkl.h"
#include "Function.h"
namespace Aurora{
typedef void(*CalcFuncD)(const MKL_INT n, const double a[], const MKL_INT inca, const double b[],
const MKL_INT incb, double r[], const MKL_INT incr);
typedef void(*CalcFuncZ)(const MKL_INT n, const MKL_Complex16 a[], const MKL_INT inca, const MKL_Complex16 b[],
const MKL_INT incb, MKL_Complex16 r[], const MKL_INT incr);
inline Matrix operatorMxA(CalcFuncD aFunc, double aScalar, const Matrix &aMatrix) {
double *output = malloc(aMatrix.getDataSize(), aMatrix.getValueType() == Complex);
aFunc(aMatrix.getDataSize(), aMatrix.getData(), 1, &aScalar, 0, output, 1);
if (aMatrix.getValueType() == Complex) {
aFunc(aMatrix.getDataSize(), aMatrix.getData() + 1, 1, &aScalar, 0, output + 1,
1);
}
return Matrix::New(output, aMatrix.getDimSize(0), aMatrix.getDimSize(1), aMatrix.getDimSize(2),
aMatrix.getValueType());
}
inline Matrix operatorMxM(CalcFuncD aFuncD, CalcFuncZ aFuncZ, const Matrix &aMatrix,
const Matrix &aOther) {
if (!aMatrix.compareShape(aOther))return Matrix();
if (aMatrix.getValueType() != aOther.getValueType()) {
double *output = malloc(aMatrix.getDataSize(), true);
if (aMatrix.getValueType() == Complex) {
aFuncD(aMatrix.getDataSize(), aMatrix.getData(), 1, aOther.getData(), 1,
output, 1);
aFuncD(aMatrix.getDataSize(), aMatrix.getData() + 1, 1, aOther.getData(), 1,
output + 1,
1);
return Matrix::New(output, aMatrix);
}
aFuncD(aMatrix.getDataSize(), aMatrix.getData(), 1, aOther.getData(), 1, output,
1);
aFuncD(aMatrix.getDataSize(), aMatrix.getData(), 1, aOther.getData() + 1, 1,
output + 1, 1);
return Matrix::New(output, aOther);
} else if (aMatrix.getValueType() == Normal) {
double *output = malloc(aMatrix.getDataSize());
aFuncD(aMatrix.getDataSize(), aMatrix.getData(), 1, aOther.getData(), 1, output,
1);
return Matrix::New(output, aMatrix);
} else {
double *output = malloc(aMatrix.getDataSize(), true);
aFuncZ(aMatrix.getDataSize(), (std::complex<double> *) aMatrix.getData(), 1,
(std::complex<double> *) aOther.getData(), 1, (std::complex<double> *) output, 1);
return Matrix::New(output, aOther);
}
}
inline Matrix &operatorMxA_RR(CalcFuncD aFunc, double aScalar, Aurora::Matrix &&aMatrix) {
std::cout << "use right ref operation" << std::endl;
std::cout << "before operation" << std::endl;
aMatrix.printf();
if (aMatrix.getValueType() == Complex) {
aFunc(aMatrix.getDataSize(), aMatrix.getData(), 1, &aScalar, 0,
aMatrix.getData(),
1);
aFunc(aMatrix.getDataSize(), aMatrix.getData() + 1, 1, &aScalar, 0,
aMatrix.getData() + 1, 1);
} else {
aFunc(aMatrix.getDataSize(), aMatrix.getData(), 1, &aScalar, 0,
aMatrix.getData(),
1);
}
std::cout << "after operation" << std::endl;
aMatrix.printf();
return aMatrix;
}
inline Matrix operatorMxM_RR(CalcFuncD aFuncD, CalcFuncZ aFuncZ, const Aurora::Matrix &aMatrix,
Aurora::Matrix &&aOther) {
if (!aMatrix.compareShape(aOther))return Matrix();
std::cout << "use right ref operation m" << std::endl;
if (aMatrix.getValueType() != aOther.getValueType()) {
//aOther is not a complex matrix
if (aMatrix.getValueType() == Complex) {
double *output = malloc(aMatrix.getDataSize(), true);
aFuncD(aMatrix.getDataSize(), aMatrix.getData(), 1, aOther.getData(), 1,
output, 1);
aFuncD(aMatrix.getDataSize(), aMatrix.getData() + 1,1, aOther.getData(), 1,
output + 1,
1);
return Matrix::New(output, aOther);
}
//aOther is a complex matrix, use aOther as output
aFuncD(aMatrix.getDataSize(), aMatrix.getData(),1, aOther.getData(), 1,
aOther.getData(),
1);
aFuncD(aMatrix.getDataSize(), aMatrix.getData(), 1, aOther.getData() + 1, 1,
aOther.getData() + 1, 1);
return aOther;
} else if (aMatrix.getValueType() == Normal) {
aFuncD(aMatrix.getDataSize(), aMatrix.getData(), 1, aOther.getData(), 1,
aOther.getData(),
1);
return aOther;
} else {
aFuncZ(aMatrix.getDataSize(), (std::complex<double> *) aMatrix.getData(), 1,
(std::complex<double> *) aOther.getData(), 1, (std::complex<double> *) aOther.getData(), 1);
return aOther;
}
}
};
namespace Aurora {
Matrix::Matrix(std::shared_ptr<double> aData, std::vector<int> aInfo)
: mData(aData), mInfo(aInfo) {
}
bool Matrix::isNull() const {
return !mData || mInfo.empty();
}
int Matrix::getDims() const {
return mInfo.size();
}
double *Matrix::getData() const {
return mData.get();
}
int Matrix::getDimSize(int aIndex) const {
if (aIndex >= 0 && aIndex < 3 && aIndex < getDims()) {
return mInfo.at(aIndex);
}
return 0;
}
size_t Matrix::getDataSize() const {
if (!mData.get())return 0;
size_t ret = 1;
for (auto v: mInfo) {
ret *= v;
}
return ret;
}
bool Matrix::compareShape(const Matrix &other) const {
if (mInfo.size() != other.mInfo.size()) return false;
for (int i = 0; i < mInfo.size(); ++i) {
if (mInfo[i] != other.mInfo[i]) return false;
}
return true;
}
Matrix Matrix::getDataFromDims2(int aColumn) {
if (2 != getDims() || aColumn > mInfo.back()) {
return Matrix();
}
int rows = mInfo.at(0);
std::shared_ptr<double> resultData = std::shared_ptr<double>(new double[rows], std::default_delete<double[]>());
std::copy(mData.get() + (aColumn - 1) * rows, mData.get() + aColumn * rows, resultData.get());
std::vector<int> resultInfo = {rows};
Matrix result(resultData, resultInfo);
return result;
}
Matrix Matrix::getDataFromDims1(int aRow) {
if (1 != getDims() || aRow > mInfo.back()) {
return Matrix();
}
std::shared_ptr<double> resultData = std::shared_ptr<double>(new double[1], std::default_delete<double[]>());
resultData.get()[0] = mData.get()[aRow - 1];
std::vector<int> resultInfo{1};
Matrix result(resultData, resultInfo);
return result;
}
Matrix Matrix::New(double *data, int rows, int cols, int slices, ValueType type) {
if (!data) return Matrix();
std::vector<int> vector;
vector.push_back(rows);
if (cols > 0)vector.push_back(cols);
if (slices > 0)vector.push_back(slices);
Matrix ret({data, std::default_delete<double[]>()}, vector);
if (type != Normal)ret.setValueType(type);
return ret;
}
Matrix Matrix::New(double *data, const Matrix &shapeMatrix) {
return New(data,
shapeMatrix.getDimSize(0),
shapeMatrix.getDimSize(1),
shapeMatrix.getDimSize(2),
shapeMatrix.getValueType());
}
Matrix Matrix::New(const Matrix &shapeMatrix) {
double *newBuffer = malloc(shapeMatrix.getDataSize(), shapeMatrix.getValueType());
return New(newBuffer, shapeMatrix);
}
Matrix Matrix::deepCopy() const {
double *newBuffer = malloc(getDataSize(), getValueType());
memcpy(newBuffer, getData(), sizeof(double) * getDataSize() * getValueType());
return New(newBuffer,
getDimSize(0),
getDimSize(1),
getDimSize(2),
getValueType());
}
//operation +
Matrix Matrix::operator+(double aScalar) const { return operatorMxA(&vdAddI, aScalar, *this);}
Matrix operator+(double aScalar, const Matrix &matrix) {return matrix + aScalar;}
Matrix Matrix::operator+(const Matrix &matrix) const {return operatorMxM(vdAddI, vzAddI, *this, matrix);}
Matrix &operator+(double aScalar, Matrix &&matrix) {
return operatorMxA_RR(&vdAddI,aScalar, std::forward<Matrix&&>(matrix));
}
Matrix &operator+(Matrix &&matrix,double aScalar) {
return operatorMxA_RR(&vdAddI,aScalar, std::forward<Matrix&&>(matrix));
}
Matrix Matrix::operator+(Matrix &&aMatrix) const {
return operatorMxM_RR(&vdAddI,&vzAddI,*this,std::forward<Matrix&&>(aMatrix));
}
Matrix operator+(Matrix &&aMatrix, const Matrix &aOther) {
return operatorMxM_RR(&vdAddI,&vzAddI,aOther,std::forward<Matrix&&>(aMatrix));
}
//operation -
Matrix Matrix::operator-(double aScalar) const { return operatorMxA(&vdSubI, aScalar, *this);}
Matrix operator-(double aScalar, const Matrix &matrix) {return matrix - aScalar;}
Matrix Matrix::operator-(const Matrix &matrix) const {return operatorMxM(vdSubI, vzSubI, *this, matrix);}
Matrix &operator-(double aScalar, Matrix &&matrix) {
return operatorMxA_RR(&vdSubI,aScalar, std::forward<Matrix&&>(matrix));
}
Matrix &operator-(Matrix &&matrix,double aScalar) {
return operatorMxA_RR(&vdSubI,aScalar, std::forward<Matrix&&>(matrix));
}
Matrix Matrix::operator-(Matrix &&aMatrix) const {
return operatorMxM_RR(&vdSubI,&vzSubI,*this,std::forward<Matrix&&>(aMatrix));
}
Matrix operator-(Matrix &&aMatrix, const Matrix &aOther) {
return operatorMxM_RR(&vdSubI,&vzSubI,aOther,std::forward<Matrix&&>(aMatrix));
}
//operation *
Matrix Matrix::operator*(double aScalar) const { return operatorMxA(&vdMulI, aScalar, *this);}
Matrix operator*(double aScalar, const Matrix &matrix) {return matrix * aScalar;}
Matrix Matrix::operator*(const Matrix &matrix) const {return operatorMxM(vdMulI, vzMulI, *this, matrix);}
Matrix &operator*(double aScalar, Matrix &&matrix) {
return operatorMxA_RR(&vdMulI,aScalar, std::forward<Matrix&&>(matrix));
}
Matrix &operator*(Matrix &&matrix,double aScalar) {
return operatorMxA_RR(&vdMulI,aScalar, std::forward<Matrix&&>(matrix));
}
Matrix Matrix::operator*(Matrix &&aMatrix) const {
return operatorMxM_RR(&vdMulI,&vzMulI,*this,std::forward<Matrix&&>(aMatrix));
}
Matrix operator*(Matrix &&aMatrix, const Matrix &aOther) {
return operatorMxM_RR(&vdMulI,&vzMulI,aOther,std::forward<Matrix&&>(aMatrix));
}
//operation /
Matrix Matrix::operator/(double aScalar) const { return operatorMxA(&vdDivI, aScalar, *this);}
Matrix operator/(double aScalar, const Matrix &matrix) {return matrix / aScalar;}
Matrix Matrix::operator/(const Matrix &matrix) const {return operatorMxM(vdDivI, vzDivI, *this, matrix);}
Matrix &operator/(double aScalar, Matrix &&matrix) {
return operatorMxA_RR(&vdDivI,aScalar, std::forward<Matrix&&>(matrix));
}
Matrix &operator/(Matrix &&matrix,double aScalar) {
return operatorMxA_RR(&vdDivI,aScalar, std::forward<Matrix&&>(matrix));
}
Matrix Matrix::operator/(Matrix &&aMatrix) const {
return operatorMxM_RR(&vdDivI,&vzDivI,*this,std::forward<Matrix&&>(aMatrix));
}
Matrix operator/(Matrix &&aMatrix, const Matrix &aOther) {
return operatorMxM_RR(&vdDivI,&vzDivI,aOther,std::forward<Matrix&&>(aMatrix));
}
2023-04-19 11:30:05 +08:00
//operator ^ (pow)
Matrix Matrix::operator^(int times) const { return operatorMxA(&vdPowI, times, *this);}
Matrix operator^( Matrix &&matrix,int times) {
return operatorMxA(&vdPowI, times, std::forward<Matrix&&>(matrix));
}
2023-04-18 13:31:14 +08:00
void Matrix::printf() {
::printf("[");
for (int i = 0; i < mInfo[0]; ++i) {
::printf("[");
for (int j = 0; j < mInfo[1]; ++j) {
::printf("%f2, ",getData()[getDimSize(0)*j+i]);
}
::printf("]\r\n");
}
::printf("]\r\n");
}
Matrix::MatrixSlice Matrix::operator()(int aRowIdx, int aColIdx, int s) const {
if (aRowIdx == ALL_DIM) {
return Matrix::MatrixSlice((int) (aColIdx > 0 ? getDimSize(0) : getDataSize()), 1,
getData() + getDimSize(0) * (aColIdx > 0 ? aColIdx : 0));
}
else if (aColIdx == ALL_DIM){
return Matrix::MatrixSlice((int) (aRowIdx > 0 ? getDimSize(1) : getDataSize()), getDimSize(0),
getData() +(aRowIdx > 0 ? aRowIdx : 0));
}
return Matrix::MatrixSlice(1, 1, getData()+ getDimSize(0) * aColIdx +aRowIdx );
}
Matrix::MatrixSlice::MatrixSlice(int aSize,int aStride, double* aData, ValueType aType):
mData(aData),
mSize(aSize),
mStride(aStride),
mType(aType)
{
}
Matrix::MatrixSlice &Matrix::MatrixSlice::operator=(const Matrix::MatrixSlice &slice) {
if (this==&slice) return *this;
if (mSize == slice.mSize && mType == slice.mType){
if (mType== Normal)cblas_dcopy(mSize,slice.mData,slice.mStride,mData,mStride);
else cblas_zcopy(mSize,(std::complex<double>*)slice.mData,slice.mStride,(std::complex<double>*)mData,mStride);
}
return *this;
}
Matrix::MatrixSlice &Matrix::MatrixSlice::operator=(const Matrix &matrix) {
if (mSize == matrix.getDataSize() && mType == matrix.getValueType()){
if (mType== Normal)cblas_dcopy(mSize,matrix.getData(),1,mData,mStride);
else cblas_zcopy(mSize,(std::complex<double>*)matrix.getDataSize(),1,(std::complex<double>*)mData,mStride);
}
return *this;
}
Matrix Matrix::MatrixSlice::toMatrix() {
auto data = malloc(mSize ,mType == Complex);
if (mType== Normal)cblas_dcopy(mSize,mData,mStride,data,1);
else cblas_zcopy(mSize,(std::complex<double>*)mData,mStride,(std::complex<double>*)data,1);
return Matrix::New(data,mSize,0,0,mType);
}
}