Add vecnorm and vecnorm's unittest.
Add norm function with complex support.
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#include "Function1D.h"
|
#include "Function1D.h"
|
||||||
|
#include "Function2D.h"
|
||||||
#include "Function.h"
|
#include "Function.h"
|
||||||
|
|
||||||
//必须在Eigen之前
|
//必须在Eigen之前
|
||||||
@@ -420,12 +421,16 @@ Matrix Aurora::conj(const Matrix& aMatrix)
|
|||||||
|
|
||||||
double Aurora::norm(const Matrix& aMatrix, NormMethod aNormMethod)
|
double Aurora::norm(const Matrix& aMatrix, NormMethod aNormMethod)
|
||||||
{
|
{
|
||||||
if(aMatrix.isComplex() || aMatrix.isNull())
|
if(aMatrix.isNull())
|
||||||
{
|
{
|
||||||
return NAN;
|
return NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size = aMatrix.getDataSize();
|
size_t size = aMatrix.getDataSize();
|
||||||
|
if(aMatrix.isComplex())
|
||||||
|
{
|
||||||
|
size*=2;
|
||||||
|
}
|
||||||
int column = aMatrix.getDimSize(1);
|
int column = aMatrix.getDimSize(1);
|
||||||
int row = aMatrix.getDimSize(0);
|
int row = aMatrix.getDimSize(0);
|
||||||
if (aNormMethod == NormMethod::Norm1)
|
if (aNormMethod == NormMethod::Norm1)
|
||||||
@@ -433,7 +438,7 @@ double Aurora::norm(const Matrix& aMatrix, NormMethod aNormMethod)
|
|||||||
double value = 0;
|
double value = 0;
|
||||||
for(int i=0; i<column; ++i)
|
for(int i=0; i<column; ++i)
|
||||||
{
|
{
|
||||||
double temp = cblas_dasum(row, aMatrix($,i,$).toMatrix().getData(), 1);
|
double temp = Aurora::sum(abs(aMatrix($,i,$).toMatrix())).getData()[0];
|
||||||
if(temp > value)
|
if(temp > value)
|
||||||
{
|
{
|
||||||
value = temp;
|
value = temp;
|
||||||
@@ -450,9 +455,18 @@ double Aurora::norm(const Matrix& aMatrix, NormMethod aNormMethod)
|
|||||||
//columns > 1
|
//columns > 1
|
||||||
if(aMatrix.getDimSize(1) > 1)
|
if(aMatrix.getDimSize(1) > 1)
|
||||||
{
|
{
|
||||||
Eigen::Map<Eigen::MatrixXd> eMatrix(aMatrix.getData(),aMatrix.getDimSize(0),aMatrix.getDimSize(1));
|
if(aMatrix.isComplex())
|
||||||
Eigen::JacobiSVD<Eigen::MatrixXd> svd(eMatrix, Eigen::ComputeThinU | Eigen::ComputeThinV);
|
{
|
||||||
return svd.singularValues()(0);
|
Eigen::Map<Eigen::MatrixXcd> eMatrix((MKL_Complex16*)aMatrix.getData(), row, column);
|
||||||
|
Eigen::JacobiSVD<Eigen::MatrixXcd> svd(eMatrix, Eigen::ComputeThinU | Eigen::ComputeThinV);
|
||||||
|
return svd.singularValues()(0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Eigen::Map<Eigen::MatrixXd> eMatrix(aMatrix.getData(), row, column);
|
||||||
|
Eigen::JacobiSVD<Eigen::MatrixXd> svd(eMatrix, Eigen::ComputeThinU | Eigen::ComputeThinV);
|
||||||
|
return svd.singularValues()(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -507,3 +521,20 @@ Matrix Aurora::horzcat(const Matrix& aMatrix1, const Matrix& aMatrix2)
|
|||||||
|
|
||||||
return Matrix::New(resultData, row, column1+column2, 1, aMatrix1.getValueType());
|
return Matrix::New(resultData, row, column1+column2, 1, aMatrix1.getValueType());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Matrix Aurora::vecnorm(const Matrix& aMatrix, NormMethod aNormMethod, int aDim)
|
||||||
|
{
|
||||||
|
//only surpport aDim = 1 for now.
|
||||||
|
if(aDim != 1 || aNormMethod == NormMethod::NormF || aMatrix.isNull())
|
||||||
|
{
|
||||||
|
return Matrix();
|
||||||
|
}
|
||||||
|
int column = aMatrix.getDimSize(1);
|
||||||
|
double* resultData = Aurora::malloc(column);
|
||||||
|
for(int i=0; i<column; ++i)
|
||||||
|
{
|
||||||
|
resultData[i] = norm(aMatrix($,i,$).toMatrix(), aNormMethod);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Matrix::New(resultData,column);
|
||||||
|
}
|
||||||
|
|||||||
@@ -70,6 +70,8 @@ namespace Aurora {
|
|||||||
|
|
||||||
Matrix horzcat(const Matrix& aMatrix1, const Matrix& aMatrix2);
|
Matrix horzcat(const Matrix& aMatrix1, const Matrix& aMatrix2);
|
||||||
|
|
||||||
|
Matrix vecnorm(const Matrix& aMatrix, NormMethod aNormMethod, int aDim);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 多项式计算
|
* 多项式计算
|
||||||
* @brief 例如p[1 0 1],x[3 2 5],代表对多项式 y = x^2 + 1 求(x=3, x=2, x=5)时所有的y
|
* @brief 例如p[1 0 1],x[3 2 5],代表对多项式 y = x^2 + 1 求(x=3, x=2, x=5)时所有的y
|
||||||
|
|||||||
@@ -290,6 +290,7 @@ TEST_F(Function1D_Test, conj) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(Function1D_Test, norm) {
|
TEST_F(Function1D_Test, norm) {
|
||||||
|
//1Dim
|
||||||
double *data = new double[3]{1,2,-3};
|
double *data = new double[3]{1,2,-3};
|
||||||
auto matrix = Aurora::Matrix::fromRawData(data, 3);
|
auto matrix = Aurora::Matrix::fromRawData(data, 3);
|
||||||
auto result = Aurora::norm(matrix,Aurora::NormMethod::Norm1);
|
auto result = Aurora::norm(matrix,Aurora::NormMethod::Norm1);
|
||||||
@@ -299,6 +300,7 @@ TEST_F(Function1D_Test, norm) {
|
|||||||
result = Aurora::norm(matrix,Aurora::NormMethod::NormF);
|
result = Aurora::norm(matrix,Aurora::NormMethod::NormF);
|
||||||
EXPECT_DOUBLE_AE(result,3.74166);
|
EXPECT_DOUBLE_AE(result,3.74166);
|
||||||
|
|
||||||
|
//2Dims
|
||||||
data = new double[8]{1,2,-3,6,7,9,22.3,-8.6};
|
data = new double[8]{1,2,-3,6,7,9,22.3,-8.6};
|
||||||
matrix = Aurora::Matrix::fromRawData(data, 4,2);
|
matrix = Aurora::Matrix::fromRawData(data, 4,2);
|
||||||
result = Aurora::norm(matrix,Aurora::NormMethod::Norm1);
|
result = Aurora::norm(matrix,Aurora::NormMethod::Norm1);
|
||||||
@@ -307,6 +309,26 @@ TEST_F(Function1D_Test, norm) {
|
|||||||
EXPECT_DOUBLE_AE(result,26.7284);
|
EXPECT_DOUBLE_AE(result,26.7284);
|
||||||
result = Aurora::norm(matrix,Aurora::NormMethod::NormF);
|
result = Aurora::norm(matrix,Aurora::NormMethod::NormF);
|
||||||
EXPECT_DOUBLE_AE(result,27.4089);
|
EXPECT_DOUBLE_AE(result,27.4089);
|
||||||
|
|
||||||
|
//1Dim Complex
|
||||||
|
data = new double[6]{1,2,-3,4,5,-6};
|
||||||
|
matrix = Aurora::Matrix::fromRawData(data, 3,1,1,Aurora::Complex);
|
||||||
|
result = Aurora::norm(matrix,Aurora::NormMethod::Norm1);
|
||||||
|
EXPECT_DOUBLE_AE(result,15.0463);
|
||||||
|
result = Aurora::norm(matrix,Aurora::NormMethod::Norm2);
|
||||||
|
EXPECT_DOUBLE_AE(result,9.5394);
|
||||||
|
result = Aurora::norm(matrix,Aurora::NormMethod::NormF);
|
||||||
|
EXPECT_DOUBLE_AE(result,9.5394);
|
||||||
|
|
||||||
|
//2Dims Complex
|
||||||
|
data = new double[12]{1,2,-3,4,5,-6,7,8,9,22,24,25};
|
||||||
|
matrix = Aurora::Matrix::fromRawData(data, 3,2,1,Aurora::Complex);
|
||||||
|
result = Aurora::norm(matrix,Aurora::NormMethod::Norm1);
|
||||||
|
EXPECT_DOUBLE_AE(result,69.0553);
|
||||||
|
result = Aurora::norm(matrix,Aurora::NormMethod::Norm2);
|
||||||
|
EXPECT_DOUBLE_AE(result,43.5314);
|
||||||
|
result = Aurora::norm(matrix,Aurora::NormMethod::NormF);
|
||||||
|
EXPECT_DOUBLE_AE(result,44.3847);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(Function1D_Test, transpose) {
|
TEST_F(Function1D_Test, transpose) {
|
||||||
@@ -368,3 +390,41 @@ TEST_F(Function1D_Test, horzcat) {
|
|||||||
EXPECT_DOUBLE_EQ(result.getDimSize(0),3);
|
EXPECT_DOUBLE_EQ(result.getDimSize(0),3);
|
||||||
EXPECT_DOUBLE_EQ(result.getDimSize(1),2);
|
EXPECT_DOUBLE_EQ(result.getDimSize(1),2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(Function1D_Test, vecnrom) {
|
||||||
|
//1Dim
|
||||||
|
double *data = new double[3]{1,2,-3};
|
||||||
|
auto matrix = Aurora::Matrix::fromRawData(data, 3);
|
||||||
|
auto result = Aurora::vecnorm(matrix,Aurora::NormMethod::Norm1,1);
|
||||||
|
EXPECT_DOUBLE_AE(result.getData()[0],6);
|
||||||
|
result = Aurora::vecnorm(matrix,Aurora::NormMethod::Norm2,1);
|
||||||
|
EXPECT_DOUBLE_AE(result.getData()[0],3.74166);
|
||||||
|
|
||||||
|
//2Dims
|
||||||
|
data = new double[8]{1,2,-3,6,7,9,22.3,-8.6};
|
||||||
|
matrix = Aurora::Matrix::fromRawData(data, 4,2);
|
||||||
|
result = Aurora::vecnorm(matrix,Aurora::NormMethod::Norm1,1);
|
||||||
|
EXPECT_DOUBLE_AE(result.getData()[0],12);
|
||||||
|
EXPECT_DOUBLE_AE(result.getData()[1],46.9);
|
||||||
|
result = Aurora::vecnorm(matrix,Aurora::NormMethod::Norm2,1);
|
||||||
|
EXPECT_DOUBLE_AE(result.getData()[0],7.0711);
|
||||||
|
EXPECT_DOUBLE_AE(result.getData()[1],26.4811);
|
||||||
|
|
||||||
|
//1Dim Complex
|
||||||
|
data = new double[6]{1,2,-3,4,5,-6};
|
||||||
|
matrix = Aurora::Matrix::fromRawData(data, 3,1,1,Aurora::Complex);
|
||||||
|
result = Aurora::vecnorm(matrix,Aurora::NormMethod::Norm1,1);
|
||||||
|
EXPECT_DOUBLE_AE(result.getData()[0],15.0463);
|
||||||
|
result = Aurora::vecnorm(matrix,Aurora::NormMethod::Norm2,1);
|
||||||
|
EXPECT_DOUBLE_AE(result.getData()[0],9.5394);
|
||||||
|
|
||||||
|
//2Dims Complex
|
||||||
|
data = new double[12]{1,2,-3,4,5,-6,7,8,9,22,24,25};
|
||||||
|
matrix = Aurora::Matrix::fromRawData(data, 3,2,1,Aurora::Complex);
|
||||||
|
result = Aurora::vecnorm(matrix,Aurora::NormMethod::Norm1,1);
|
||||||
|
EXPECT_DOUBLE_AE(result.getData()[0],15.0463);
|
||||||
|
EXPECT_DOUBLE_AE(result.getData()[1],69.0553);
|
||||||
|
result = Aurora::vecnorm(matrix,Aurora::NormMethod::Norm2,1);
|
||||||
|
EXPECT_DOUBLE_AE(result.getData()[0],9.5394);
|
||||||
|
EXPECT_DOUBLE_AE(result.getData()[1],43.3474);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user