Add fromRawData & copyFromRawData to Matrix class.

This commit is contained in:
Krad
2023-04-20 14:06:44 +08:00
parent 6ec61aa4cb
commit 9c0667a65a
3 changed files with 67 additions and 3 deletions

View File

@@ -206,15 +206,34 @@ namespace Aurora {
shapeMatrix.getValueType());
}
Matrix Matrix::New(const Matrix &shapeMatrix) {
double *newBuffer = malloc(shapeMatrix.getDataSize(), shapeMatrix.getValueType());
return New(newBuffer, shapeMatrix);
}
Matrix Matrix::fromRawData(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::copyFromRawData(double *data, int rows, int cols, int slices, ValueType type) {
int colsize = cols>0?cols:1;
int slicesize = slicesize>0?slicesize:1;
int size = rows*colsize*slicesize;
double *newBuffer = malloc(size, type);
cblas_dcopy(size*type,data,1,newBuffer,1);
return New(newBuffer,rows,cols,slices,type);
}
Matrix Matrix::deepCopy() const {
double *newBuffer = malloc(getDataSize(), getValueType());
memcpy(newBuffer, getData(), sizeof(double) * getDataSize() * getValueType());
double *newBuffer = malloc(getDataSize(), getValueType()==Complex);
cblas_dcopy(getDataSize() * getValueType(),getData(),1,newBuffer,1);
return New(newBuffer,
getDimSize(0),
getDimSize(1),

View File

@@ -41,6 +41,30 @@ namespace Aurora {
explicit Matrix(const Matrix::MatrixSlice& slice);
/**
* Create from a Raw data(double array).
* Use Raw data which create like new double[size]() as a data source
* and the share_ptr's deleter will be std::default_delete<double[]>
* @param data
* @param rows
* @param cols
* @param slices
* @param type
* @return
*/
static Matrix fromRawData(double *data, int rows, int cols = 0, int slices = 0, ValueType type = Normal);
/**
* Create from a Raw data(double array) with copy the data to a new mkl memory.
* @param data
* @param rows
* @param cols
* @param slices
* @param type
* @return
*/
static Matrix copyFromRawData(double *data, int rows, int cols = 0, int slices = 0, ValueType type = Normal);
/**
* New a mkl calculate based Matrix
* @attention Using New function, must use Aurora:malloc to get memory