Skip to content

Commit

Permalink
math: fix adjoint of Matrix2
Browse files Browse the repository at this point in the history
Fix the adjoint of the Matrix2x2 implementation in the math module. This
also fixes the calculation of the inverse which depends on the adjoint.
  • Loading branch information
konimarti committed Dec 13, 2024
1 parent c9c3f33 commit e4efd08
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/std/math/math_matrix.c3
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ fn Real Matrix4x4.determinant(&self)

fn Matrix2x2 Matrix2x2.adjoint(&self)
{
return { self.m00, -self.m01, -self.m10, self.m11 };
return { self.m11, -self.m01, -self.m10, self.m00 };
}

fn Matrix3x3 Matrix3x3.adjoint(&self)
Expand Down
11 changes: 10 additions & 1 deletion test/unit/stdlib/math/matrix.c3
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,17 @@ fn void test_mat2()
assert(calc.m == value.m);
}

fn void! test_mat2_inverse()
{
Matrix2 a = { 3, 5, 6, 2 };
Matrix2 a_inv = a.inverse()!;
double sum = ((double[<4>])a_inv.mul(a).m).sum();
assert(math::abs(sum - 2.0) < math::FLOAT_EPSILON,
"wrong inverse: sum of all elements should be 2, but got: %g", sum);
}

fn void test_vec3()
{
Vec3 cross = Vec3{2,3,4}.cross(Vec3{5,6,7});
assert(cross == Vec3{-3,6,-3});
}
}

0 comments on commit e4efd08

Please sign in to comment.