|
楼主 |
发表于 2024-9-22 17:19:22
|
显示全部楼层
#include <iostream>
using namespace std;
class det_4d_solver
{
public:
det_4d_solver() {}
~det_4d_solver() {}
double get_rlt()
{
double det_3_1, det_3_2, det_3_3, det_3_4;
det_3_1 = det_3d(a[0], b[0], c[0], d[0]);
det_3_2 = det_3d(a[1], b[1], c[1], d[1]);
det_3_3 = det_3d(a[2], b[2], c[2], d[2]);
det_3_4 = det_3d(a[3], b[3], c[3], d[3]);
return a[0] * det_3_1 - a[1] * det_3_2 + a[2] * det_3_3 - a[3] * det_3_4;
}
void set_elms(double in_a[4], double in_b[4], double in_c[4], double in_d[4])
{
for (int i = 0; i < 4; ++i)
{
a[i] = in_a[i];
b[i] = in_b[i];
c[i] = in_c[i];
d[i] = in_d[i];
}
}
private:
double det_3d(double a1, double b1, double c1, double d1)
{
return (a1 * det_2d(b[1], c[1], b[2], c[2]) -
b1 * det_2d(a[1], c[1], a[2], c[2]) +
c1 * det_2d(a[1], b[1], a[2], b[2]) -
d1 * det_2d(a[1], b[1], a[2], b[2]));
}
double det_2d(double a, double b, double c, double d)
{
return (a * d - b * c);
}
private:
double a[4];
double b[4];
double c[4];
double d[4];
};
int main()
{
double a[4] = { 1, 2, 3, 4 };
double b[4] = { 4, 2, 5, 3 };
double c[4] = { 5, 7, 8.1, 4 };
double d[4] = { 6, 5, 2, 1 };
det_4d_solver solver_4d;
solver_4d.set_elms(a, b, c, d);
double rlt = solver_4d.get_rlt();
cout << "The result of det of 4x4 matrix is " << rlt << '\n';
}
检查
|
|