Add log and log's unittest.

This commit is contained in:
sunwen
2023-04-23 11:44:24 +08:00
parent 5e85d0f361
commit 8cec1440f2
4 changed files with 33 additions and 2 deletions

View File

@@ -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<double>), 64);
}

View File

@@ -8,7 +8,7 @@
#include <complex>
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);

View File

@@ -303,3 +303,22 @@ Matrix Aurora::repmat(const Matrix& aMatrix,int aRowTimes, int aColumnTimes, int
return Matrix(std::shared_ptr<double>(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<double>(data,Aurora::free),std::vector<int>{aMatrix.getDimSize(0),aMatrix.getDimSize(1),aMatrix.getDimSize(2)});
}

View File

@@ -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);
}