Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for Matrix rescale methods #240

Merged
merged 3 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/linalg/Matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1880,7 +1880,7 @@ Matrix::rescale_cols_max()

// Get the max across all processes, if applicable.
double global_max[d_num_cols];
if (d_num_procs > 1)
if (d_distributed && d_num_procs > 1)
{
MPI_Allreduce(&local_max, &global_max, d_num_cols, MPI_DOUBLE, MPI_MAX,
MPI_COMM_WORLD);
Expand Down
116 changes: 116 additions & 0 deletions unit_tests/test_Matrix.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,122 @@ TEST(MatrixSerialTest, Test_get_first_n_columns)
EXPECT_DOUBLE_EQ(truncated_matrix->item(3, 1), 13.0);
}

TEST(MatrixSerialTest, Test_Matrix_rescale_rows_max)
{
// Matrix data to rescale.
double d_mat[12] = { 3.0, 5.0, 0.0,
-3.0, -2.0, -1.0,
-1.0, 3.0, -2.0,
2.0, -2.5, 0.5
};

// Target matrix data.
double d_mat2[12] = { 0.6, 1.0, 0.0,
-1.0, -2.0/3.0, -1.0/3.0,
-1.0/3.0, 1.0, -2.0/3.0,
2.0/2.5, -1.0, 0.5/2.5
};

CAROM::Matrix matrix(d_mat, 4, 3, false);
CAROM::Matrix target(d_mat2, 4, 3, false);

double abs_error = 1.0e-15; // absolute error threshold

matrix.rescale_rows_max();

for (int i = 0; i < 4; i++)
for (int j = 0; j < 3; j++)
EXPECT_NEAR(matrix.item(i, j), target.item(i, j), abs_error) <<
"(i, j) = (" << i << ", " << j << ")";
}

TEST(MatrixSerialTest, Test_Matrix_rescale_rows_max2)
{
// Matrix data to rescale.
double d_mat[12] = { 0.0, -1.0e-16, 1.0e-15,
-3.0, -2.0, -1.0,
-1.0, 3.0, -2.0,
2.0, -2.5, 0.5
};

// Target matrix data.
double d_mat2[12] = { 0.0, -1.0e-16, 1.0e-15,
-1.0, -2.0/3.0, -1.0/3.0,
-1.0/3.0, 1.0, -2.0/3.0,
2.0/2.5, -1.0, 0.5/2.5
};

CAROM::Matrix matrix(d_mat, 4, 3, false);
CAROM::Matrix target(d_mat2, 4, 3, false);

double abs_error = 1.0e-15; // absolute error threshold

matrix.rescale_rows_max();

for (int i = 0; i < 4; i++)
for (int j = 0; j < 3; j++)
EXPECT_NEAR(matrix.item(i, j), target.item(i, j), abs_error) <<
"(i, j) = (" << i << ", " << j << ")";
}

TEST(MatrixSerialTest, Test_Matrix_rescale_cols_max)
{
// Matrix data to rescale.
double d_mat[12] = { 3.0, 5.0, 0.0,
-3.0, -2.0, -1.0,
-1.0, 3.0, -2.0,
2.0, -2.5, 0.5
};

// Target matrix data.
double d_mat2[12] = { 1.0, 1.0, 0.0,
-1.0, -0.4, -0.5,
-1.0/3.0, 0.6, -1.0,
2.0/3.0, -0.5, 0.25
};

CAROM::Matrix matrix(d_mat, 4, 3, false);
CAROM::Matrix target(d_mat2, 4, 3, false);

double abs_error = 1.0e-15; // absolute error threshold

matrix.rescale_cols_max();

for (int i = 0; i < 4; i++)
for (int j = 0; j < 3; j++)
EXPECT_NEAR(matrix.item(i, j), target.item(i, j), abs_error) <<
"(i, j) = (" << i << ", " << j << ")";
}

TEST(MatrixSerialTest, Test_Matrix_rescale_cols_max2)
{
// Matrix data to rescale.
double d_mat[12] = { 3.0, 5.0, 1.0e-15,
-3.0, -2.0, 0.0,
-1.0, 3.0, -1.0e-16,
2.0, -2.5, 0.0
};

// Target matrix data.
double d_mat2[12] = { 1.0, 1.0, 1.0e-15,
-1.0, -0.4, 0.0,
-1.0/3.0, 0.6, -1.0e-16,
2.0/3.0, -0.5, 0.0
};

CAROM::Matrix matrix(d_mat, 4, 3, false);
CAROM::Matrix target(d_mat2, 4, 3, false);

double abs_error = 1.0e-15; // absolute error threshold

matrix.rescale_cols_max();

for (int i = 0; i < 4; i++)
for (int j = 0; j < 3; j++)
EXPECT_NEAR(matrix.item(i, j), target.item(i, j), abs_error) <<
"(i, j) = (" << i << ", " << j << ")";
}

TEST(MatrixSerialTest, Test_pMatrix_mult_reference)
{
/**
Expand Down