Fix bugs in determinant and inverse
This commit is contained in:
parent
8ab8af992a
commit
6332e3d770
1 changed files with 17 additions and 17 deletions
34
src/vec3.zig
34
src/vec3.zig
|
|
@ -90,7 +90,7 @@ pub fn mat3Mul(a: Mat3F64, b: Mat3F64) Mat3F64 {
|
||||||
pub fn mat3Det(a: Mat3F64) f64 {
|
pub fn mat3Det(a: Mat3F64) f64 {
|
||||||
return a.row1.x() * (a.row2.y() * a.row3.z() - a.row2.z() * a.row3.y()) -
|
return a.row1.x() * (a.row2.y() * a.row3.z() - a.row2.z() * a.row3.y()) -
|
||||||
a.row1.y() * (a.row2.x() * a.row3.z() - a.row2.z() * a.row3.x()) +
|
a.row1.y() * (a.row2.x() * a.row3.z() - a.row2.z() * a.row3.x()) +
|
||||||
a.row1.y() * (a.row2.x() * a.row3.y() - a.row2.y() * a.row3.x());
|
a.row1.z() * (a.row2.x() * a.row3.y() - a.row2.y() * a.row3.x());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mat3Inv(a: Mat3F64) Mat3F64 {
|
pub fn mat3Inv(a: Mat3F64) Mat3F64 {
|
||||||
|
|
@ -105,7 +105,7 @@ pub fn mat3Inv(a: Mat3F64) Mat3F64 {
|
||||||
);
|
);
|
||||||
const row2 = vec3Scale(
|
const row2 = vec3Scale(
|
||||||
.init(
|
.init(
|
||||||
a.row2.z() * a.row3.x() + a.row2.x() * a.row3.z(),
|
a.row2.z() * a.row3.x() - a.row2.x() * a.row3.z(),
|
||||||
a.row1.x() * a.row3.z() - a.row1.z() * a.row3.x(),
|
a.row1.x() * a.row3.z() - a.row1.z() * a.row3.x(),
|
||||||
a.row1.z() * a.row2.x() - a.row1.x() * a.row2.z(),
|
a.row1.z() * a.row2.x() - a.row1.x() * a.row2.z(),
|
||||||
),
|
),
|
||||||
|
|
@ -113,7 +113,7 @@ pub fn mat3Inv(a: Mat3F64) Mat3F64 {
|
||||||
);
|
);
|
||||||
const row3 = vec3Scale(
|
const row3 = vec3Scale(
|
||||||
.init(
|
.init(
|
||||||
a.row2.x() * a.row3.y() + a.row2.y() * a.row3.x(),
|
a.row2.x() * a.row3.y() - a.row2.y() * a.row3.x(),
|
||||||
a.row1.y() * a.row3.x() - a.row1.x() * a.row3.y(),
|
a.row1.y() * a.row3.x() - a.row1.x() * a.row3.y(),
|
||||||
a.row1.x() * a.row2.y() - a.row1.y() * a.row2.x(),
|
a.row1.x() * a.row2.y() - a.row1.y() * a.row2.x(),
|
||||||
),
|
),
|
||||||
|
|
@ -321,30 +321,30 @@ test "Mat3 mul works" {
|
||||||
test "mat determinant works" {
|
test "mat determinant works" {
|
||||||
const a: Mat3F64 = .{
|
const a: Mat3F64 = .{
|
||||||
.row1 = Vec3F64.init(1, 2, 3),
|
.row1 = Vec3F64.init(1, 2, 3),
|
||||||
.row2 = Vec3F64.init(0, 1, 5),
|
.row2 = Vec3F64.init(4, 1, 5),
|
||||||
.row3 = Vec3F64.init(0, 0, 9),
|
.row3 = Vec3F64.init(3, 1, 9),
|
||||||
};
|
};
|
||||||
try std.testing.expectApproxEqAbs(9, mat3Det(a), 1e-12);
|
try std.testing.expectApproxEqAbs(-35, mat3Det(a), 1e-12);
|
||||||
}
|
}
|
||||||
|
|
||||||
test "mat inverse works" {
|
test "mat inverse works" {
|
||||||
const a: Mat3F64 = .{
|
const a: Mat3F64 = .{
|
||||||
.row1 = Vec3F64.init(1, 2, 3),
|
.row1 = Vec3F64.init(1, 2, 3),
|
||||||
.row2 = Vec3F64.init(0, 1, 5),
|
.row2 = Vec3F64.init(4, 1, 5),
|
||||||
.row3 = Vec3F64.init(0, 0, 9),
|
.row3 = Vec3F64.init(3, 1, 9),
|
||||||
};
|
};
|
||||||
|
|
||||||
const ainv = mat3Inv(a);
|
const ainv = mat3Inv(a);
|
||||||
|
|
||||||
try std.testing.expectApproxEqAbs(1, ainv.row1.x(), 1e-12);
|
try std.testing.expectApproxEqAbs(-4.0 / 35.0, ainv.row1.x(), 1e-12);
|
||||||
try std.testing.expectApproxEqAbs(-2, ainv.row1.y(), 1e-12);
|
try std.testing.expectApproxEqAbs(3.0 / 7.0, ainv.row1.y(), 1e-12);
|
||||||
try std.testing.expectApproxEqAbs(7.0 / 9.0, ainv.row1.z(), 1e-12);
|
try std.testing.expectApproxEqAbs(-1.0 / 5.0, ainv.row1.z(), 1e-12);
|
||||||
try std.testing.expectApproxEqAbs(0, ainv.row2.x(), 1e-12);
|
try std.testing.expectApproxEqAbs(3.0 / 5.0, ainv.row2.x(), 1e-12);
|
||||||
try std.testing.expectApproxEqAbs(1, ainv.row2.y(), 1e-12);
|
try std.testing.expectApproxEqAbs(0, ainv.row2.y(), 1e-12);
|
||||||
try std.testing.expectApproxEqAbs(-5.0 / 9.0, ainv.row2.z(), 1e-12);
|
try std.testing.expectApproxEqAbs(-1.0 / 5.0, ainv.row2.z(), 1e-12);
|
||||||
try std.testing.expectApproxEqAbs(0, ainv.row3.x(), 1e-12);
|
try std.testing.expectApproxEqAbs(-1.0 / 35.0, ainv.row3.x(), 1e-12);
|
||||||
try std.testing.expectApproxEqAbs(0, ainv.row3.y(), 1e-12);
|
try std.testing.expectApproxEqAbs(-1.0 / 7.0, ainv.row3.y(), 1e-12);
|
||||||
try std.testing.expectApproxEqAbs(1.0 / 9.0, ainv.row3.z(), 1e-12);
|
try std.testing.expectApproxEqAbs(1.0 / 5.0, ainv.row3.z(), 1e-12);
|
||||||
}
|
}
|
||||||
test "vec3 mul mat3 works" {
|
test "vec3 mul mat3 works" {
|
||||||
const b: Mat3F64 = .{
|
const b: Mat3F64 = .{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue