From 8cec1440f23e1deccae1c9ff69c58d4e982b74e9 Mon Sep 17 00:00:00 2001 From: sunwen Date: Sun, 23 Apr 2023 11:44:24 +0800 Subject: [PATCH] Add log and log's unittest. --- src/Function.cpp | 2 +- src/Function.h | 2 +- src/Function1D.cpp | 19 +++++++++++++++++++ test/Function1D_Test.cpp | 12 ++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Function.cpp b/src/Function.cpp index 9b2fe1d..d9210e4 100644 --- a/src/Function.cpp +++ b/src/Function.cpp @@ -144,7 +144,7 @@ namespace Aurora { return result; } - double *malloc(int size, bool complex) { + double *malloc(size_t size, bool complex) { if (!complex) return (double *) mkl_malloc(size * sizeof(double), 64); return (double *) mkl_malloc(size * sizeof(std::complex), 64); } diff --git a/src/Function.h b/src/Function.h index ced7354..8521ea4 100644 --- a/src/Function.h +++ b/src/Function.h @@ -8,7 +8,7 @@ #include namespace Aurora{ - double* malloc(int size,bool complex = false); + double* malloc(size_t size,bool complex = false); void free(void* ptr); double* mul( double scalar, double * input, int size); double* mul( double* inputA, double * inputB, int size); diff --git a/src/Function1D.cpp b/src/Function1D.cpp index 985939b..f340fb0 100644 --- a/src/Function1D.cpp +++ b/src/Function1D.cpp @@ -303,3 +303,22 @@ Matrix Aurora::repmat(const Matrix& aMatrix,int aRowTimes, int aColumnTimes, int return Matrix(std::shared_ptr(resultData, Aurora::free), resultInfo, aMatrix.getValueType()); } + +Matrix Aurora::log(const Matrix& aMatrix, int aBaseNum) +{ + size_t size = aMatrix.getDataSize(); + double* data = Aurora::malloc(size); + vdLn(size, aMatrix.getData(), data); + if(aBaseNum != -1) + { + double baseNum = aBaseNum; + double temp; + for (size_t i = 0; i < size; i++) + { + vdLn(1, &baseNum, &temp); + data[i] /= temp; + } + } + + return Aurora::Matrix(std::shared_ptr(data,Aurora::free),std::vector{aMatrix.getDimSize(0),aMatrix.getDimSize(1),aMatrix.getDimSize(2)}); +} diff --git a/test/Function1D_Test.cpp b/test/Function1D_Test.cpp index 282152a..0a1115a 100644 --- a/test/Function1D_Test.cpp +++ b/test/Function1D_Test.cpp @@ -203,4 +203,16 @@ TEST_F(Function1D_Test, absAndSqrt) { EXPECT_TRUE(isnanf(matrixB.getData()[2])); } +TEST_F(Function1D_Test, log) { + double *dataP = new double[3]{1, 4, 6}; + auto matrixA = Aurora::Matrix::fromRawData(dataP, 3); + auto result = Aurora::log(matrixA); + EXPECT_DOUBLE_AE(result.getData()[0],0); + EXPECT_DOUBLE_AE(result.getData()[1],1.3863); + EXPECT_DOUBLE_AE(result.getData()[2],1.7918); + result = Aurora::log(matrixA,10); + EXPECT_DOUBLE_AE(result.getData()[0],0); + EXPECT_DOUBLE_AE(result.getData()[1],0.6021); + EXPECT_DOUBLE_AE(result.getData()[2],0.7782); +}