Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matrix adjugate operations return transposed result #4584

Open
spahnke opened this issue Dec 16, 2024 · 0 comments · May be fixed by #4633
Open

Matrix adjugate operations return transposed result #4584

spahnke opened this issue Dec 16, 2024 · 0 comments · May be fixed by #4633

Comments

@spahnke
Copy link
Contributor

spahnke commented Dec 16, 2024

Context

  • Odin Version: dev-2024-12-nightly:cf53404

Expected Behavior

It should hold that $\textrm{adj}(M)\cdot M = \det(M)\cdot I$.

Current Behavior

I hope I'm not completely misunderstanding something here, but the result of using the adjugate procedure from either core:math/linalg, core:math/linalg/glsl, or core:math/linalg/hlsl returns a matrix that seems to be the transpose of the actual adjugate and the above equality doesn't hold. This is the case for 2x2, 3x3 and 4x4 matrices.

Failure Information (for bugs)

Steps to Reproduce

Minimal test program that prints the determinants and $\textrm{adj}(M)\cdot M$ for a 2x2, 3x3 and 4x4 matrix. The 3x3 example is from https://en.wikipedia.org/wiki/Adjugate_matrix

$$ M = \begin{pmatrix} -3 & 2 & -5 \\ -1 & 0 & -2 \\ 3 & -4 & 1 \end{pmatrix},\ \textrm{adj}(M) = \begin{pmatrix} -8 & 18 & -4 \\ -5 & 12 & -1 \\ 4 & -6 & 2 \end{pmatrix},\ \textrm{adj}(M)\cdot M = \begin{pmatrix} -6 & 0 & 0 \\ 0 & -6 & 0 \\ 0 & 0 & -6 \end{pmatrix} $$

package main

import "core:fmt"
import "core:math/linalg"
import glm "core:math/linalg/glsl"
import hlm "core:math/linalg/hlsl"

main :: proc() {
	m2 := matrix[2,2]int {
		-3, 2,
		-1, 0,
	}
	fmt.println(linalg.determinant(m2))
	fmt.printfln("%#v", linalg.adjugate(m2))
	fmt.printfln("%#v", linalg.adjugate(m2) * m2)
	fmt.printfln("%#v", m2 * linalg.adjugate(m2))
	fmt.printfln("%#v", glm.adjugate(m2) * m2)
	fmt.printfln("%#v", m2 * glm.adjugate(m2))
	fmt.printfln("%#v", hlm.adjugate(m2) * m2)
	fmt.printfln("%#v", m2 * hlm.adjugate(m2))

	// from https://en.wikipedia.org/wiki/Adjugate_matrix
	m3 := matrix[3,3]int {
		-3, 2, -5,
		-1, 0, -2,
		3, -4, 1,
	}
	fmt.println(linalg.determinant(m3))
	fmt.printfln("%#v", linalg.adjugate(m3))
	fmt.printfln("%#v", linalg.adjugate(m3) * m3)
	fmt.printfln("%#v", m3 * linalg.adjugate(m3))
	fmt.printfln("%#v", glm.adjugate(m3) * m3)
	fmt.printfln("%#v", m3 * glm.adjugate(m3))
	fmt.printfln("%#v", hlm.adjugate(m3) * m3)
	fmt.printfln("%#v", m3 * hlm.adjugate(m3))

	m4 := matrix[4,4]int {
		-3, 2, -5, 1,
		-1, 0, -2, 2,
		3, -4, 1, 3,
		4, 5, 6, 7,
	}
	fmt.println(linalg.determinant(m4))
	fmt.printfln("%#v", linalg.adjugate(m4))
	fmt.printfln("%#v", linalg.adjugate(m4) * m4)
	fmt.printfln("%#v", m4 * linalg.adjugate(m4))
	fmt.printfln("%#v", glm.adjugate(m4) * m4)
	fmt.printfln("%#v", m4 * glm.adjugate(m4))
	fmt.printfln("%#v", hlm.adjugate(m4) * m4)
	fmt.printfln("%#v", m4 * hlm.adjugate(m4))
}

Output:

2
matrix[
	0, 1,
	-2, -3,
]
matrix[
	-1, 0,
	9, -4,
]
matrix[
	-4, -9,
	0, -1,
]
matrix[
	-1, 0,
	9, -4,
]
matrix[
	-4, -9,
	0, -1,
]
matrix[
	-1, 0,
	9, -4,
]
matrix[
	-4, -9,
	0, -1,
]
-6
matrix[
	-8, -5, 4,
	18, 12, -6,
	-4, -1, 2,
]
matrix[
	41, -32, 54,
	-84, 60, -120,
	19, -16, 24,
]
matrix[
	80, 44, -34,
	16, 7, -8,
	-100, -64, 38,
]
matrix[
	41, -32, 54,
	-84, 60, -120,
	19, -16, 24,
]
matrix[
	80, 44, -34,
	16, 7, -8,
	-100, -64, 38,
]
matrix[
	41, -32, 54,
	-84, 60, -120,
	19, -16, 24,
]
matrix[
	80, 44, -34,
	16, 7, -8,
	-100, -64, 38,
]
-174
matrix[
	-144, -57, 105, 33,
	266, 92, -142, -96,
	-92, -5, 55, 9,
	-16, -16, 2, -6,
]
matrix[
	936, -543, 1137, 288,
	-1700, 620, -2232, -648,
	482, -359, 579, 126,
	46, -70, 78, -84,
]
matrix[
	1408, 364, -872, -342,
	296, 35, -211, -63,
	-1636, -592, 944, 474,
	90, 90, 54, -336,
]
matrix[
	936, -543, 1137, 288,
	-1700, 620, -2232, -648,
	482, -359, 579, 126,
	46, -70, 78, -84,
]
matrix[
	1408, 364, -872, -342,
	296, 35, -211, -63,
	-1636, -592, 944, 474,
	90, 90, 54, -336,
]
matrix[
	936, -543, 1137, 288,
	-1700, 620, -2232, -648,
	482, -359, 579, 126,
	46, -70, 78, -84,
]
matrix[
	1408, 364, -872, -342,
	296, 35, -211, -63,
	-1636, -592, 944, 474,
	90, 90, 54, -336,
]
@spahnke spahnke linked a pull request Dec 28, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant