Add fromRawData & copyFromRawData to Matrix class.
This commit is contained in:
@@ -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),
|
||||
|
||||
24
src/Matrix.h
24
src/Matrix.h
@@ -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
|
||||
|
||||
@@ -41,6 +41,27 @@ TEST_F(FunctionTester, matrixSlice) {
|
||||
B.printf();
|
||||
}
|
||||
|
||||
TEST_F(FunctionTester, RawDataMatrix) {
|
||||
double * dataA =new double[8];
|
||||
double * dataB =new double[8];
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
dataA[i]=(double)(i-3);
|
||||
dataB[i]=(double)(i+2);
|
||||
}
|
||||
Aurora::Matrix A = Aurora::Matrix::fromRawData(dataA,2,2,2);
|
||||
printf("A:\r\n");
|
||||
A.printf();
|
||||
Aurora::Matrix B = Aurora::Matrix::copyFromRawData(dataB,2,2,2);
|
||||
delete [] dataB;
|
||||
printf("B:\r\n");
|
||||
B.printf();
|
||||
A(Aurora::$,Aurora::$,1) = B(Aurora::$,Aurora::$,0);
|
||||
printf("New A:\r\n");
|
||||
A.printf();
|
||||
printf("New B:\r\n");
|
||||
B.printf();
|
||||
}
|
||||
|
||||
TEST_F(FunctionTester, sign) {
|
||||
double * dataA =Aurora::malloc(9);
|
||||
double * dataB =Aurora::malloc(9);
|
||||
|
||||
Reference in New Issue
Block a user