Skip to content
Open
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
21 changes: 21 additions & 0 deletions Sofa/framework/Type/src/sofa/type/Mat.h
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,27 @@ class MatNoInit : public Mat<L,C,real>
}
};

template <sofa::Size N, typename real>
real determinant(const Mat<N, N, real>& mat)
{
real det = 0;
for (size_t p = 0; p < N; ++p)
{
Mat<N - 1, N - 1, real> submat;
for (size_t i = 1; i < N; ++i)
{
size_t colIndex = 0;
for (size_t j = 0; j < N; ++j)
{
if (j == p) continue;
submat(i - 1, colIndex++) = mat(i, j);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be more efficient to only create a vector of col and row indices that is reduced of one element for each determinant development ? So in the end your 'submatrix' is like a view on the original matrix, instead of copying the same values multiple times ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not try to reach high performances because I don't have the need. Currently, I use it only in init().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would your changes be easily introduced @bakpaul ?
if yes, we can add it inside this PR
else, it could be done in a later one

}
}
det += ((p % 2 == 0) ? 1 : -1) * mat(0, p) * determinant(submat);
}
return det;
}

/// Determinant of a 3x3 matrix.
template<class real>
constexpr real determinant(const Mat<3,3,real>& m) noexcept
Expand Down
9 changes: 9 additions & 0 deletions Sofa/framework/Type/test/MatTypes_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,12 @@ TEST(MatTypesTest, determinant3x3)
EXPECT_DOUBLE_EQ(sofa::type::determinant(sofa::type::Mat<3,3,SReal>{{0, 1, 0}, {0, 0, 1}, {1, 0, 0}}), 1_sreal);
EXPECT_DOUBLE_EQ(sofa::type::determinant(sofa::type::Mat<3,3,SReal>{{1, 1, 0}, {1, 0, 1}, {0, 1, 1}}), -2_sreal);
}

TEST(MatTypesTest, determinant4x4)
{
EXPECT_DOUBLE_EQ(sofa::type::determinant(sofa::type::Mat<4,4,SReal>{{1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1}}), 1_sreal);
EXPECT_DOUBLE_EQ(sofa::type::determinant(sofa::type::Mat<4,4,SReal>{{2,0,0,0}, {0,3,0,0}, {0,0,4,0}, {0,0,0,5}}), 120_sreal);
EXPECT_DOUBLE_EQ(sofa::type::determinant(sofa::type::Mat<4,4,SReal>{{0,1,0,0}, {1,0,0,0}, {0,0,1,0}, {0,0,0,1}}), -1_sreal);
EXPECT_DOUBLE_EQ(sofa::type::determinant(sofa::type::Mat<4,4,SReal>{{1,0,0,0}, {0,2,0,0}, {0,0,3,0}, {0,0,0,4}}), 24_sreal);
EXPECT_DOUBLE_EQ(sofa::type::determinant(sofa::type::Mat<4,4,SReal>{{1,3,5,9}, {1,3,1,7}, {4,3,9,7}, {5,2,0,9}}), -376_sreal);
}
Loading