Add Matrix & Matrix max function and its test.
This commit is contained in:
@@ -306,6 +306,59 @@ Matrix Aurora::max(const Matrix &aMatrix, FunctionDirection direction, long& row
|
||||
}
|
||||
}
|
||||
|
||||
Matrix Aurora::max(const Matrix &aMatrix, const Matrix &aOther) {
|
||||
if (aMatrix.getDimSize(2)>1 || aMatrix.isComplex()) {
|
||||
std::cerr
|
||||
<< (aMatrix.getDimSize(2) > 1 ? "min() not support 3D data!" : "min() not support complex value type!")
|
||||
<< std::endl;
|
||||
return Matrix();
|
||||
}
|
||||
if (aOther.getDimSize(2)>1 || aOther.isComplex()) {
|
||||
std::cerr
|
||||
<< (aOther.getDimSize(2) > 1 ? "min() not support 3D data!" : "min() not support complex value type!")
|
||||
<< std::endl;
|
||||
return Matrix();
|
||||
}
|
||||
//same shape
|
||||
if (aMatrix.compareShape(aOther)){
|
||||
double* output = malloc(aMatrix.getDataSize());
|
||||
vdFmaxI(aMatrix.getDataSize(),aMatrix.getData(),1,aOther.getData(),1,output,1);
|
||||
return Matrix::New(output,aMatrix);
|
||||
}
|
||||
// one is scalar
|
||||
else if (aMatrix.getDataSize() == 1 || aOther.getDataSize() == 1){
|
||||
double scalar = (aMatrix.getDataSize() == 1)?aMatrix.getData()[0]:aOther.getData()[0];
|
||||
auto matrix = (aMatrix.getDataSize() == 1)?aOther:aMatrix;
|
||||
double* output = malloc(matrix.getDataSize());
|
||||
vdFmaxI(matrix.getDataSize(),matrix.getData(),1,&scalar,0,output,1);
|
||||
return Matrix::New(output,matrix);
|
||||
}
|
||||
else if (aMatrix.getDimSize(1) == 1 || aOther.getDimSize(0) == 1) {
|
||||
if (aMatrix.getDimSize(1) == 1){
|
||||
double* output = malloc(aOther.getDataSize());
|
||||
for (int i = 0; i < aOther.getDimSize(1); ++i) {
|
||||
vdFmaxI(aMatrix.getDataSize(), aMatrix.getData(), 1, aOther.getData() + aOther.getDimSize(0) * i, 1,
|
||||
output + aOther.getDimSize(0) * i, 1);
|
||||
}
|
||||
return Matrix::New(output,aOther);
|
||||
}
|
||||
else{
|
||||
double* output = malloc(aMatrix.getDataSize());
|
||||
for (int i = 0; i < aMatrix.getDimSize(0); ++i) {
|
||||
vdFmaxI(aOther.getDataSize(), aOther.getData(), 1, aMatrix.getData() + i, aMatrix.getDimSize(0),
|
||||
output + i, aOther.getDimSize(0));
|
||||
}
|
||||
return Matrix::New(output,aMatrix);
|
||||
}
|
||||
}
|
||||
else{
|
||||
std::cerr
|
||||
<< "min(A,B) with matrix must be like A[MxN] - B[1xN] or A[Mx1] - B[MxN]"
|
||||
<< std::endl;
|
||||
return Matrix();
|
||||
}
|
||||
}
|
||||
|
||||
Matrix Aurora::sum(const Matrix &aMatrix, FunctionDirection direction) {
|
||||
if (aMatrix.getDimSize(2)>1 ) {
|
||||
std::cerr<< "sum() not support 3D data!"
|
||||
|
||||
@@ -51,6 +51,15 @@ namespace Aurora
|
||||
*/
|
||||
Matrix min(const Matrix &aMatrix, const Matrix &aOther);
|
||||
|
||||
/**
|
||||
* 比较两个矩阵,求对应位置的最大值,不支持三维
|
||||
* @attention 矩阵形状不一样时,如A为[MxN],则B应为标量或[1xN]的行向量
|
||||
* @param aMatrix 目标矩阵1
|
||||
* @param aOther 目标矩阵2
|
||||
* @return 最大值矩阵
|
||||
*/
|
||||
Matrix max(const Matrix &aMatrix, const Matrix &aOther);
|
||||
|
||||
/**
|
||||
* 求矩阵和,可按行、列、单元, 目前不支持三维
|
||||
* @param aMatrix 目标矩阵
|
||||
|
||||
@@ -120,9 +120,12 @@ TEST_F(Function2D_Test, min) {
|
||||
|
||||
TEST_F(Function2D_Test, max) {
|
||||
double *dataA = new double[3]{1, 2, 3};
|
||||
double *dataC = new double[3]{1, 2, 4};
|
||||
double *dataB = new double[9]{2, 3, 3, 2, 2, 1, 3, 3, 3};
|
||||
auto A = Aurora::Matrix::fromRawData(dataA, 3, 1);
|
||||
auto B = Aurora::Matrix::fromRawData(dataB, 3, 3);
|
||||
auto C = Aurora::Matrix::fromRawData(dataC, 3, 1);
|
||||
|
||||
Aurora::Matrix ret = Aurora::max(B);
|
||||
EXPECT_EQ(1, ret.getDimSize(0));
|
||||
EXPECT_EQ(3, ret.getDimSize(1));
|
||||
@@ -150,6 +153,9 @@ TEST_F(Function2D_Test, max) {
|
||||
EXPECT_EQ(1, ret.getDimSize(0));
|
||||
EXPECT_EQ(1, ret.getDimSize(1));
|
||||
EXPECT_DOUBLE_EQ(3, ret.getData()[0]);
|
||||
|
||||
ret = Aurora::max(A,C);
|
||||
EXPECT_DOUBLE_EQ(4, ret.getData()[2]);
|
||||
}
|
||||
|
||||
TEST_F(Function2D_Test, sum) {
|
||||
|
||||
Reference in New Issue
Block a user