Skip to content

Instantly share code, notes, and snippets.

@sjhalayka
Created January 30, 2025 20:55
Show Gist options
  • Save sjhalayka/25f1af284a99d6134955aef9bddba6fd to your computer and use it in GitHub Desktop.
Save sjhalayka/25f1af284a99d6134955aef9bddba6fd to your computer and use it in GitHub Desktop.
template <typename size_t N>
double determinant_nxn(const MatrixXd& m)
{
if (m.cols() != m.rows())
{
cout << "Matrix must be " << N << "x" << N << endl;
return 0;
}
Vector_nD<N> a_nd;
for (size_t i = 0; i < N; i++)
a_nd.components[i] = m(0, i);
std::vector<Vector_nD<N>> vectors;
for (size_t i = 1; i < N; i++)
{
Vector_nD<N> non;
for (size_t j = 0; j < N; j++)
non.components[j] = m(i, j);
vectors.push_back(non);
}
// Compute the cross product
Vector_nD<N> result = Vector_nD<N>::cross_product(vectors);
for (size_t i = 0; i < result.components.size(); i++)
if (i % 2 == 1)
result.components[i] = -result.components[i];
double d_dot = Vector_nD<N>::dot_product(a_nd, result);
cout << d_dot << endl;
cout << m.determinant() << endl << endl;
return d_dot;
}
... and ...
int main(int argc, char** argv)
{
cout << std::scientific << endl;
// cout << setprecision(20) << endl;
const size_t N = 10;
MatrixXd m(N, N);
for (size_t i = 0; i < N; i++)
for (size_t j = 0; j < N; j++)
m(i, j) = static_cast<double>(rand());
determinant_nxn<N>(m);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment