From 3f12f0e0c16b7317f48d223af90ea8210f1dd24b Mon Sep 17 00:00:00 2001 From: kradchen Date: Fri, 28 Apr 2023 13:19:12 +0800 Subject: [PATCH] Add negative to Matrix --- src/Matrix.cpp | 28 ++++++++++++++++++++-------- test/Matrix_Test.cpp | 4 ++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/Matrix.cpp b/src/Matrix.cpp index 980fdb5..d8a7b8a 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -305,10 +305,10 @@ namespace Aurora { Matrix Matrix::New(double *data, int rows, int cols, int slices, ValueType type) { if (!data) return Matrix(); - std::vector vector; - vector.push_back(rows); - if (cols > 0)vector.push_back(cols); - if (slices > 0)vector.push_back(slices); + std::vector vector(3); + vector[0]=rows; + vector[1] = (cols > 0?cols:1); + vector[2] = (slices > 0?slices:1); Matrix ret({data, free}, vector); if (type != Normal)ret.setValueType(type); return ret; @@ -349,7 +349,10 @@ namespace Aurora { Matrix Matrix::deepCopy() const { double *newBuffer = malloc(getDataSize(), getValueType()==Complex); - cblas_dcopy(getDataSize() * getValueType(),getData(),1,newBuffer,1); + // size_t data_size = getDataSize() * getValueType(); +cblas_dcopy(getDataSize() * getValueType(),getData(),1,newBuffer,1); + // std::memcpy(newBuffer,getData(),data_size*sizeof(double)); + return New(newBuffer, getDimSize(0), getDimSize(1), @@ -679,7 +682,17 @@ namespace Aurora { } Matrix operator-(const Matrix &aMatrix) { - return -(std::forward(aMatrix.deepCopy())); + double *newBuffer = malloc(aMatrix.getDataSize(), aMatrix.getValueType()==Complex); + double zero = 0.0; + if (aMatrix.isComplex()){ + vdSubI( aMatrix.getDataSize(),&zero,0,aMatrix.getData(),2,newBuffer,2); + vdSubI( aMatrix.getDataSize(),&zero,0,aMatrix.getData()+1,2,newBuffer+1,2); + } + else{ + vdSubI( aMatrix.getDataSize(),&zero,0,aMatrix.getData(),1,newBuffer,1); + } + + return Matrix::New(newBuffer,aMatrix); } Matrix::MatrixSlice::MatrixSlice(int aSize,int aStride, double* aData, ValueType aType, int aSliceMode,int aSize2, int aStride2): @@ -742,8 +755,7 @@ namespace Aurora { cblas_dcopy(mSize,slice.mData,slice.mStride,mData,mStride); } else { - cblas_zcopy(mSize, (std::complex *) slice.mData, slice.mStride, - (std::complex *) mData, mStride); + cblas_dcopy(mSize*2, slice.mData, slice.mStride, mData, mStride); } break; } diff --git a/test/Matrix_Test.cpp b/test/Matrix_Test.cpp index 81aacd1..1c39381 100644 --- a/test/Matrix_Test.cpp +++ b/test/Matrix_Test.cpp @@ -280,5 +280,9 @@ TEST_F(Matrix_Test, matrixOpertaor) { DISPLAY_MATRIX(C); EXPECT_EQ(C.getData()[2], 0); EXPECT_EQ(C.getData()[3], 2); + + C= -B; + EXPECT_EQ(C.getData()[0], -2); + EXPECT_EQ(C.getData()[1], -2); } } \ No newline at end of file