|
@@ -1882,6 +1882,13 @@ adjugate :: proc{
|
|
adjugate_matrix4x4,
|
|
adjugate_matrix4x4,
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+cofactor :: proc{
|
|
|
|
+ cofactor_matrix1x1,
|
|
|
|
+ cofactor_matrix2x2,
|
|
|
|
+ cofactor_matrix3x3,
|
|
|
|
+ cofactor_matrix4x4,
|
|
|
|
+}
|
|
|
|
+
|
|
inverse_transpose :: proc{
|
|
inverse_transpose :: proc{
|
|
inverse_transpose_matrix1x1,
|
|
inverse_transpose_matrix1x1,
|
|
inverse_transpose_matrix2x2,
|
|
inverse_transpose_matrix2x2,
|
|
@@ -1944,9 +1951,9 @@ determinant_matrix3x3 :: proc "contextless" (m: $M/matrix[3, 3]$T) -> (det: T) {
|
|
}
|
|
}
|
|
@(require_results)
|
|
@(require_results)
|
|
determinant_matrix4x4 :: proc "contextless" (m: $M/matrix[4, 4]$T) -> (det: T) {
|
|
determinant_matrix4x4 :: proc "contextless" (m: $M/matrix[4, 4]$T) -> (det: T) {
|
|
- a := adjugate(m)
|
|
|
|
|
|
+ c := cofactor(m)
|
|
#no_bounds_check for i in 0..<4 {
|
|
#no_bounds_check for i in 0..<4 {
|
|
- det += m[0, i] * a[0, i]
|
|
|
|
|
|
+ det += m[0, i] * c[0, i]
|
|
}
|
|
}
|
|
return
|
|
return
|
|
}
|
|
}
|
|
@@ -1962,6 +1969,47 @@ adjugate_matrix1x1 :: proc "contextless" (x: $M/matrix[1, 1]$T) -> (y: M) {
|
|
|
|
|
|
@(require_results)
|
|
@(require_results)
|
|
adjugate_matrix2x2 :: proc "contextless" (x: $M/matrix[2, 2]$T) -> (y: M) {
|
|
adjugate_matrix2x2 :: proc "contextless" (x: $M/matrix[2, 2]$T) -> (y: M) {
|
|
|
|
+ y[0, 0] = +x[1, 1]
|
|
|
|
+ y[0, 1] = -x[0, 1]
|
|
|
|
+ y[1, 0] = -x[1, 0]
|
|
|
|
+ y[1, 1] = +x[0, 0]
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+@(require_results)
|
|
|
|
+adjugate_matrix3x3 :: proc "contextless" (m: $M/matrix[3, 3]$T) -> (y: M) {
|
|
|
|
+ y[0, 0] = +(m[1, 1] * m[2, 2] - m[2, 1] * m[1, 2])
|
|
|
|
+ y[1, 0] = -(m[1, 0] * m[2, 2] - m[2, 0] * m[1, 2])
|
|
|
|
+ y[2, 0] = +(m[1, 0] * m[2, 1] - m[2, 0] * m[1, 1])
|
|
|
|
+ y[0, 1] = -(m[0, 1] * m[2, 2] - m[2, 1] * m[0, 2])
|
|
|
|
+ y[1, 1] = +(m[0, 0] * m[2, 2] - m[2, 0] * m[0, 2])
|
|
|
|
+ y[2, 1] = -(m[0, 0] * m[2, 1] - m[2, 0] * m[0, 1])
|
|
|
|
+ y[0, 2] = +(m[0, 1] * m[1, 2] - m[1, 1] * m[0, 2])
|
|
|
|
+ y[1, 2] = -(m[0, 0] * m[1, 2] - m[1, 0] * m[0, 2])
|
|
|
|
+ y[2, 2] = +(m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1])
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+@(require_results)
|
|
|
|
+adjugate_matrix4x4 :: proc "contextless" (x: $M/matrix[4, 4]$T) -> (y: M) {
|
|
|
|
+ for i in 0..<4 {
|
|
|
|
+ for j in 0..<4 {
|
|
|
|
+ sign: T = 1 if (i + j) % 2 == 0 else -1
|
|
|
|
+ y[i, j] = sign * matrix_minor(x, j, i)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+@(require_results)
|
|
|
|
+cofactor_matrix1x1 :: proc "contextless" (x: $M/matrix[1, 1]$T) -> (y: M) {
|
|
|
|
+ y = x
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+@(require_results)
|
|
|
|
+cofactor_matrix2x2 :: proc "contextless" (x: $M/matrix[2, 2]$T) -> (y: M) {
|
|
y[0, 0] = +x[1, 1]
|
|
y[0, 0] = +x[1, 1]
|
|
y[0, 1] = -x[1, 0]
|
|
y[0, 1] = -x[1, 0]
|
|
y[1, 0] = -x[0, 1]
|
|
y[1, 0] = -x[0, 1]
|
|
@@ -1970,7 +2018,7 @@ adjugate_matrix2x2 :: proc "contextless" (x: $M/matrix[2, 2]$T) -> (y: M) {
|
|
}
|
|
}
|
|
|
|
|
|
@(require_results)
|
|
@(require_results)
|
|
-adjugate_matrix3x3 :: proc "contextless" (m: $M/matrix[3, 3]$T) -> (y: M) {
|
|
|
|
|
|
+cofactor_matrix3x3 :: proc "contextless" (m: $M/matrix[3, 3]$T) -> (y: M) {
|
|
y[0, 0] = +(m[1, 1] * m[2, 2] - m[2, 1] * m[1, 2])
|
|
y[0, 0] = +(m[1, 1] * m[2, 2] - m[2, 1] * m[1, 2])
|
|
y[0, 1] = -(m[1, 0] * m[2, 2] - m[2, 0] * m[1, 2])
|
|
y[0, 1] = -(m[1, 0] * m[2, 2] - m[2, 0] * m[1, 2])
|
|
y[0, 2] = +(m[1, 0] * m[2, 1] - m[2, 0] * m[1, 1])
|
|
y[0, 2] = +(m[1, 0] * m[2, 1] - m[2, 0] * m[1, 1])
|
|
@@ -1985,7 +2033,7 @@ adjugate_matrix3x3 :: proc "contextless" (m: $M/matrix[3, 3]$T) -> (y: M) {
|
|
|
|
|
|
|
|
|
|
@(require_results)
|
|
@(require_results)
|
|
-adjugate_matrix4x4 :: proc "contextless" (x: $M/matrix[4, 4]$T) -> (y: M) {
|
|
|
|
|
|
+cofactor_matrix4x4 :: proc "contextless" (x: $M/matrix[4, 4]$T) -> (y: M) {
|
|
for i in 0..<4 {
|
|
for i in 0..<4 {
|
|
for j in 0..<4 {
|
|
for j in 0..<4 {
|
|
sign: T = 1 if (i + j) % 2 == 0 else -1
|
|
sign: T = 1 if (i + j) % 2 == 0 else -1
|
|
@@ -2021,19 +2069,19 @@ inverse_transpose_matrix2x2 :: proc "contextless" (x: $M/matrix[2, 2]$T) -> (y:
|
|
|
|
|
|
@(require_results)
|
|
@(require_results)
|
|
inverse_transpose_matrix3x3 :: proc "contextless" (x: $M/matrix[3, 3]$T) -> (y: M) #no_bounds_check {
|
|
inverse_transpose_matrix3x3 :: proc "contextless" (x: $M/matrix[3, 3]$T) -> (y: M) #no_bounds_check {
|
|
- a := adjugate(x)
|
|
|
|
|
|
+ c := cofactor(x)
|
|
d := determinant(x)
|
|
d := determinant(x)
|
|
when intrinsics.type_is_integer(T) {
|
|
when intrinsics.type_is_integer(T) {
|
|
for i in 0..<3 {
|
|
for i in 0..<3 {
|
|
for j in 0..<3 {
|
|
for j in 0..<3 {
|
|
- y[i, j] = a[i, j] / d
|
|
|
|
|
|
+ y[i, j] = c[i, j] / d
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
id := 1/d
|
|
id := 1/d
|
|
for i in 0..<3 {
|
|
for i in 0..<3 {
|
|
for j in 0..<3 {
|
|
for j in 0..<3 {
|
|
- y[i, j] = a[i, j] * id
|
|
|
|
|
|
+ y[i, j] = c[i, j] * id
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -2042,22 +2090,22 @@ inverse_transpose_matrix3x3 :: proc "contextless" (x: $M/matrix[3, 3]$T) -> (y:
|
|
|
|
|
|
@(require_results)
|
|
@(require_results)
|
|
inverse_transpose_matrix4x4 :: proc "contextless" (x: $M/matrix[4, 4]$T) -> (y: M) #no_bounds_check {
|
|
inverse_transpose_matrix4x4 :: proc "contextless" (x: $M/matrix[4, 4]$T) -> (y: M) #no_bounds_check {
|
|
- a := adjugate(x)
|
|
|
|
|
|
+ c := cofactor(x)
|
|
d: T
|
|
d: T
|
|
for i in 0..<4 {
|
|
for i in 0..<4 {
|
|
- d += x[0, i] * a[0, i]
|
|
|
|
|
|
+ d += x[0, i] * c[0, i]
|
|
}
|
|
}
|
|
when intrinsics.type_is_integer(T) {
|
|
when intrinsics.type_is_integer(T) {
|
|
for i in 0..<4 {
|
|
for i in 0..<4 {
|
|
for j in 0..<4 {
|
|
for j in 0..<4 {
|
|
- y[i, j] = a[i, j] / d
|
|
|
|
|
|
+ y[i, j] = c[i, j] / d
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
id := 1/d
|
|
id := 1/d
|
|
for i in 0..<4 {
|
|
for i in 0..<4 {
|
|
for j in 0..<4 {
|
|
for j in 0..<4 {
|
|
- y[i, j] = a[i, j] * id
|
|
|
|
|
|
+ y[i, j] = c[i, j] * id
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -2090,19 +2138,19 @@ inverse_matrix2x2 :: proc "contextless" (x: $M/matrix[2, 2]$T) -> (y: M) {
|
|
|
|
|
|
@(require_results)
|
|
@(require_results)
|
|
inverse_matrix3x3 :: proc "contextless" (x: $M/matrix[3, 3]$T) -> (y: M) #no_bounds_check {
|
|
inverse_matrix3x3 :: proc "contextless" (x: $M/matrix[3, 3]$T) -> (y: M) #no_bounds_check {
|
|
- a := adjugate(x)
|
|
|
|
|
|
+ c := cofactor(x)
|
|
d := determinant(x)
|
|
d := determinant(x)
|
|
when intrinsics.type_is_integer(T) {
|
|
when intrinsics.type_is_integer(T) {
|
|
for i in 0..<3 {
|
|
for i in 0..<3 {
|
|
for j in 0..<3 {
|
|
for j in 0..<3 {
|
|
- y[i, j] = a[j, i] / d
|
|
|
|
|
|
+ y[i, j] = c[j, i] / d
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
id := 1/d
|
|
id := 1/d
|
|
for i in 0..<3 {
|
|
for i in 0..<3 {
|
|
for j in 0..<3 {
|
|
for j in 0..<3 {
|
|
- y[i, j] = a[j, i] * id
|
|
|
|
|
|
+ y[i, j] = c[j, i] * id
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -2111,22 +2159,22 @@ inverse_matrix3x3 :: proc "contextless" (x: $M/matrix[3, 3]$T) -> (y: M) #no_bou
|
|
|
|
|
|
@(require_results)
|
|
@(require_results)
|
|
inverse_matrix4x4 :: proc "contextless" (x: $M/matrix[4, 4]$T) -> (y: M) #no_bounds_check {
|
|
inverse_matrix4x4 :: proc "contextless" (x: $M/matrix[4, 4]$T) -> (y: M) #no_bounds_check {
|
|
- a := adjugate(x)
|
|
|
|
|
|
+ c := cofactor(x)
|
|
d: T
|
|
d: T
|
|
for i in 0..<4 {
|
|
for i in 0..<4 {
|
|
- d += x[0, i] * a[0, i]
|
|
|
|
|
|
+ d += x[0, i] * c[0, i]
|
|
}
|
|
}
|
|
when intrinsics.type_is_integer(T) {
|
|
when intrinsics.type_is_integer(T) {
|
|
for i in 0..<4 {
|
|
for i in 0..<4 {
|
|
for j in 0..<4 {
|
|
for j in 0..<4 {
|
|
- y[i, j] = a[j, i] / d
|
|
|
|
|
|
+ y[i, j] = c[j, i] / d
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
id := 1/d
|
|
id := 1/d
|
|
for i in 0..<4 {
|
|
for i in 0..<4 {
|
|
for j in 0..<4 {
|
|
for j in 0..<4 {
|
|
- y[i, j] = a[j, i] * id
|
|
|
|
|
|
+ y[i, j] = c[j, i] * id
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|