Fix matrix operator bug on - and /.
This commit is contained in:
@@ -158,27 +158,28 @@ namespace Aurora{
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline Matrix operatorMxM_RR(CalcFuncD aFuncD, CalcFuncZ aFuncZ, const Aurora::Matrix &aMatrix,
|
inline Matrix operatorMxM_RR(CalcFuncD aFuncD, CalcFuncZ aFuncZ, const Aurora::Matrix &aMatrix,
|
||||||
Aurora::Matrix &&aOther) {
|
Aurora::Matrix &&aOther,bool oppside = false) {
|
||||||
std::cout << "use right ref operation m" << std::endl;
|
std::cout << "use right ref operation m" << std::endl;
|
||||||
if (aMatrix.compareShape(aOther)) {
|
if (aMatrix.compareShape(aOther)) {
|
||||||
int DimsStride = 1;
|
int DimsStride = 1;
|
||||||
|
double* X = oppside?aOther.getData():aMatrix.getData();
|
||||||
|
double* Y = oppside?aMatrix.getData():aOther.getData();
|
||||||
if (aMatrix.getValueType() != aOther.getValueType()) {
|
if (aMatrix.getValueType() != aOther.getValueType()) {
|
||||||
//aOther is not a complex matrix
|
//aOther is not a complex matrix
|
||||||
if (aMatrix.getValueType() == Complex) {
|
if (aMatrix.getValueType() == Complex) {
|
||||||
double *output = _MxM_NC_Calc(aFuncD, aMatrix.getDataSize(), aMatrix.getData(), aOther.getData(),
|
double *output = _MxM_NC_Calc(aFuncD,aMatrix.getDataSize(), X, Y,DimsStride);
|
||||||
DimsStride);
|
|
||||||
return Matrix::New(output, aOther);
|
return Matrix::New(output, aOther);
|
||||||
}
|
}
|
||||||
//aOther is a complex matrix, use aOther as output
|
//aOther is a complex matrix, use aOther as output
|
||||||
V_MxM_NC_Calc(aFuncD, aMatrix.getDataSize(), aMatrix.getData(), aOther.getData(), aOther.getData(),
|
V_MxM_NC_Calc(aFuncD, aMatrix.getDataSize(), X, Y, aOther.getData(),
|
||||||
DimsStride);
|
DimsStride);
|
||||||
return aOther;
|
return aOther;
|
||||||
} else if (aMatrix.getValueType() == Normal) {
|
} else if (aMatrix.getValueType() == Normal) {
|
||||||
V_MxM_NN_Calc(aFuncD, aMatrix.getDataSize(), aMatrix.getData(), aOther.getData(), aOther.getData(),
|
V_MxM_NN_Calc(aFuncD, aMatrix.getDataSize(), X, Y, aOther.getData(),
|
||||||
DimsStride);
|
DimsStride);
|
||||||
return aOther;
|
return aOther;
|
||||||
} else {
|
} else {
|
||||||
V_MxM_CC_Calc(aFuncZ, aMatrix.getDataSize(), aMatrix.getData(), aOther.getData(), aOther.getData(),
|
V_MxM_CC_Calc(aFuncZ, aMatrix.getDataSize(), X, Y, aOther.getData(),
|
||||||
DimsStride);
|
DimsStride);
|
||||||
return aOther;
|
return aOther;
|
||||||
}
|
}
|
||||||
@@ -289,6 +290,7 @@ namespace Aurora {
|
|||||||
if (slices > 0)vector.push_back(slices);
|
if (slices > 0)vector.push_back(slices);
|
||||||
Matrix ret({data, free}, vector);
|
Matrix ret({data, free}, vector);
|
||||||
if (type != Normal)ret.setValueType(type);
|
if (type != Normal)ret.setValueType(type);
|
||||||
|
ret.printf();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -349,7 +351,7 @@ namespace Aurora {
|
|||||||
return operatorMxM_RR(&vdAddI,&vzAddI,*this,std::forward<Matrix&&>(aMatrix));
|
return operatorMxM_RR(&vdAddI,&vzAddI,*this,std::forward<Matrix&&>(aMatrix));
|
||||||
}
|
}
|
||||||
Matrix operator+(Matrix &&aMatrix, const Matrix &aOther) {
|
Matrix operator+(Matrix &&aMatrix, const Matrix &aOther) {
|
||||||
return operatorMxM_RR(&vdAddI,&vzAddI,aOther,std::forward<Matrix&&>(aMatrix));
|
return operatorMxM_RR(&vdAddI,&vzAddI,aOther,std::forward<Matrix&&>(aMatrix),true);
|
||||||
}
|
}
|
||||||
|
|
||||||
//operation -
|
//operation -
|
||||||
@@ -366,7 +368,7 @@ namespace Aurora {
|
|||||||
return operatorMxM_RR(&vdSubI,&vzSubI,*this,std::forward<Matrix&&>(aMatrix));
|
return operatorMxM_RR(&vdSubI,&vzSubI,*this,std::forward<Matrix&&>(aMatrix));
|
||||||
}
|
}
|
||||||
Matrix operator-(Matrix &&aMatrix, const Matrix &aOther) {
|
Matrix operator-(Matrix &&aMatrix, const Matrix &aOther) {
|
||||||
return operatorMxM_RR(&vdSubI,&vzSubI,aOther,std::forward<Matrix&&>(aMatrix));
|
return operatorMxM_RR(&vdSubI,&vzSubI,aOther,std::forward<Matrix&&>(aMatrix),true);
|
||||||
}
|
}
|
||||||
|
|
||||||
//operation *
|
//operation *
|
||||||
@@ -383,7 +385,7 @@ namespace Aurora {
|
|||||||
return operatorMxM_RR(&vdMulI,&vzMulI,*this,std::forward<Matrix&&>(aMatrix));
|
return operatorMxM_RR(&vdMulI,&vzMulI,*this,std::forward<Matrix&&>(aMatrix));
|
||||||
}
|
}
|
||||||
Matrix operator*(Matrix &&aMatrix, const Matrix &aOther) {
|
Matrix operator*(Matrix &&aMatrix, const Matrix &aOther) {
|
||||||
return operatorMxM_RR(&vdMulI,&vzMulI,aOther,std::forward<Matrix&&>(aMatrix));
|
return operatorMxM_RR(&vdMulI,&vzMulI,aOther,std::forward<Matrix&&>(aMatrix),true);
|
||||||
}
|
}
|
||||||
|
|
||||||
//operation /
|
//operation /
|
||||||
@@ -400,7 +402,7 @@ namespace Aurora {
|
|||||||
return operatorMxM_RR(&vdDivI,&vzDivI,*this,std::forward<Matrix&&>(aMatrix));
|
return operatorMxM_RR(&vdDivI,&vzDivI,*this,std::forward<Matrix&&>(aMatrix));
|
||||||
}
|
}
|
||||||
Matrix operator/(Matrix &&aMatrix, const Matrix &aOther) {
|
Matrix operator/(Matrix &&aMatrix, const Matrix &aOther) {
|
||||||
return operatorMxM_RR(&vdDivI,&vzDivI,aOther,std::forward<Matrix&&>(aMatrix));
|
return operatorMxM_RR(&vdDivI,&vzDivI,aOther,std::forward<Matrix&&>(aMatrix),true);
|
||||||
}
|
}
|
||||||
|
|
||||||
//operator ^ (pow)
|
//operator ^ (pow)
|
||||||
@@ -449,7 +451,7 @@ namespace Aurora {
|
|||||||
std::vector<int> allDimIndex;
|
std::vector<int> allDimIndex;
|
||||||
int mode = 0;
|
int mode = 0;
|
||||||
for (int j = 0; j < 3; ++j) {
|
for (int j = 0; j < 3; ++j) {
|
||||||
if (dims[j]==$ && this->getDims()>j){
|
if (dims[j]==$ && getDims()>j){
|
||||||
++mode;
|
++mode;
|
||||||
allDimIndex.push_back(j);
|
allDimIndex.push_back(j);
|
||||||
}
|
}
|
||||||
@@ -481,7 +483,7 @@ namespace Aurora {
|
|||||||
//scalar mode or ALL $
|
//scalar mode or ALL $
|
||||||
case 0:
|
case 0:
|
||||||
default: {
|
default: {
|
||||||
return Matrix::MatrixSlice(1 , 1, startPointer,getValueType(), mode);
|
return Matrix::MatrixSlice(1 , 1, startPointer,getValueType(), 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -133,9 +133,11 @@ TEST_F(FunctionTester, MatrixCreate) {
|
|||||||
TEST_F(FunctionTester, matrixSlice) {
|
TEST_F(FunctionTester, matrixSlice) {
|
||||||
double * dataA =Aurora::malloc(8);
|
double * dataA =Aurora::malloc(8);
|
||||||
double * dataB =Aurora::malloc(8);
|
double * dataB =Aurora::malloc(8);
|
||||||
|
double * dataC =Aurora::malloc(8);
|
||||||
for (int i = 0; i < 8; ++i) {
|
for (int i = 0; i < 8; ++i) {
|
||||||
dataA[i]=(double)(i-3);
|
dataA[i]=(double)(i);
|
||||||
dataB[i]=(double)(i+2);
|
dataB[i]=(double)(1);
|
||||||
|
dataC[i]=(double)(9-i);
|
||||||
}
|
}
|
||||||
Aurora::Matrix A = Aurora::Matrix::New(dataA,2,2,2);
|
Aurora::Matrix A = Aurora::Matrix::New(dataA,2,2,2);
|
||||||
printf("A:\r\n");
|
printf("A:\r\n");
|
||||||
@@ -143,32 +145,53 @@ TEST_F(FunctionTester, matrixSlice) {
|
|||||||
Aurora::Matrix B = Aurora::Matrix::New(dataB,2,2,2);
|
Aurora::Matrix B = Aurora::Matrix::New(dataB,2,2,2);
|
||||||
printf("B:\r\n");
|
printf("B:\r\n");
|
||||||
B.printf();
|
B.printf();
|
||||||
A(Aurora::$,Aurora::$,1) = B(Aurora::$,Aurora::$,0);
|
Aurora::Matrix C = Aurora::Matrix::New(dataC,2,2,2);
|
||||||
printf("New A:\r\n");
|
printf("C:\r\n");
|
||||||
A.printf();
|
C.printf();
|
||||||
printf("New B:\r\n");
|
//2D slice
|
||||||
B.printf();
|
EXPECT_EQ(4.0,dataA[4]);
|
||||||
|
A(Aurora::$,Aurora::$,1) = B(0,Aurora::$,Aurora::$);
|
||||||
|
EXPECT_EQ(1,dataA[4]);
|
||||||
|
EXPECT_EQ(3,dataA[3]);
|
||||||
|
A(Aurora::$,1,Aurora::$) = B(Aurora::$,Aurora::$,0);
|
||||||
|
EXPECT_EQ(1.0,dataA[3]);
|
||||||
|
EXPECT_EQ(0.0,dataA[0]);
|
||||||
|
A(0,Aurora::$,Aurora::$) = B(Aurora::$,0,Aurora::$);
|
||||||
|
EXPECT_EQ(1.0,dataA[0]);
|
||||||
|
//vector slice
|
||||||
|
A(0,Aurora::$,0) = C(0,0,Aurora::$);
|
||||||
|
EXPECT_EQ(9.0,dataA[0]);
|
||||||
|
A(Aurora::$,0,0) = C(0,Aurora::$,1);
|
||||||
|
EXPECT_EQ(5.0,dataA[0]);
|
||||||
|
//error slice
|
||||||
|
EXPECT_EQ(1,A(Aurora::$,Aurora::$,Aurora::$).toMatrix().getDataSize() );
|
||||||
|
auto D =C(0,0,0).toMatrix();
|
||||||
|
EXPECT_EQ(1,D.getDataSize() );
|
||||||
|
EXPECT_EQ(9,D.getData()[0] );
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(FunctionTester, RawDataMatrix) {
|
TEST_F(FunctionTester, matrixOpertaor) {
|
||||||
double * dataA =new double[8];
|
//3D
|
||||||
double * dataB =new double[8];
|
{
|
||||||
for (int i = 0; i < 8; ++i) {
|
double * dataA =new double[8];
|
||||||
dataA[i]=(double)(i-3);
|
double * dataB =new double[8];
|
||||||
dataB[i]=(double)(i+2);
|
for (int i = 0; i < 8; ++i) {
|
||||||
|
dataA[i]=(double)(i);
|
||||||
|
dataB[i]=(double)(2);
|
||||||
|
}
|
||||||
|
Aurora::Matrix A = Aurora::Matrix::fromRawData(dataA,2,2,2);
|
||||||
|
DISPLAY_MATRIX(A)
|
||||||
|
Aurora::Matrix B = Aurora::Matrix::fromRawData(dataB,2,2,2);
|
||||||
|
DISPLAY_MATRIX(B)
|
||||||
|
auto C = (A*B)-B;
|
||||||
|
DISPLAY_MATRIX(C)
|
||||||
|
EXPECT_EQ(C.getData()[2],2);
|
||||||
|
C = A*B/2.0;
|
||||||
|
EXPECT_EQ(C.getData()[2],2);
|
||||||
|
C = A*B*B/2.0;
|
||||||
|
EXPECT_EQ(C.getData()[2],4);
|
||||||
}
|
}
|
||||||
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) {
|
TEST_F(FunctionTester, sign) {
|
||||||
|
|||||||
Reference in New Issue
Block a user