mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-07 16:18:52 +00:00
Merge pull request #1420 from odin-lang/linalg-to-use-matrix-type
Update matrix types to be the native Odin `matrix` types
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package linalg
|
package linalg
|
||||||
|
|
||||||
import "core:math"
|
import "core:math"
|
||||||
|
import "core:builtin"
|
||||||
import "core:intrinsics"
|
import "core:intrinsics"
|
||||||
|
|
||||||
// Generic
|
// Generic
|
||||||
@@ -60,14 +61,7 @@ quaternion256_dot :: proc(a, b: $T/quaternion256) -> (c: f64) {
|
|||||||
dot :: proc{scalar_dot, vector_dot, quaternion64_dot, quaternion128_dot, quaternion256_dot}
|
dot :: proc{scalar_dot, vector_dot, quaternion64_dot, quaternion128_dot, quaternion256_dot}
|
||||||
|
|
||||||
inner_product :: dot
|
inner_product :: dot
|
||||||
outer_product :: proc(a: $A/[$M]$E, b: $B/[$N]E) -> (out: [M][N]E) where IS_NUMERIC(E) #no_bounds_check {
|
outer_product :: builtin.outer_product
|
||||||
for i in 0..<M {
|
|
||||||
for j in 0..<N {
|
|
||||||
out[i][j] = a[i]*b[j]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
quaternion_inverse :: proc(q: $Q) -> Q where IS_QUATERNION(Q) {
|
quaternion_inverse :: proc(q: $Q) -> Q where IS_QUATERNION(Q) {
|
||||||
return conj(q) * quaternion(1.0/dot(q, q), 0, 0, 0)
|
return conj(q) * quaternion(1.0/dot(q, q), 0, 0, 0)
|
||||||
@@ -163,65 +157,28 @@ identity :: proc($T: typeid/[$N][N]$E) -> (m: T) #no_bounds_check {
|
|||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
trace :: proc(m: $T/[$N][N]$E) -> (tr: E) {
|
trace :: builtin.matrix_trace
|
||||||
for i in 0..<N {
|
transpose :: builtin.transpose
|
||||||
tr += m[i][i]
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
transpose :: proc(a: $T/[$N][$M]$E) -> (m: (T when N == M else [M][N]E)) #no_bounds_check {
|
matrix_mul :: proc(a, b: $M/matrix[$N, N]$E) -> (c: M)
|
||||||
for j in 0..<M {
|
|
||||||
for i in 0..<N {
|
|
||||||
m[j][i] = a[i][j]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
matrix_mul :: proc(a, b: $M/[$N][N]$E) -> (c: M)
|
|
||||||
where !IS_ARRAY(E), IS_NUMERIC(E) #no_bounds_check {
|
where !IS_ARRAY(E), IS_NUMERIC(E) #no_bounds_check {
|
||||||
for i in 0..<N {
|
return a * b
|
||||||
for k in 0..<N {
|
|
||||||
for j in 0..<N {
|
|
||||||
c[k][i] += a[j][i] * b[k][j]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix_comp_mul :: proc(a, b: $M/[$J][$I]$E) -> (c: M)
|
matrix_comp_mul :: proc(a, b: $M/matrix[$I, $J]$E) -> (c: M)
|
||||||
where !IS_ARRAY(E), IS_NUMERIC(E) #no_bounds_check {
|
where !IS_ARRAY(E), IS_NUMERIC(E) #no_bounds_check {
|
||||||
for j in 0..<J {
|
return hadamard_product(a, b)
|
||||||
for i in 0..<I {
|
|
||||||
c[j][i] = a[j][i] * b[j][i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix_mul_differ :: proc(a: $A/[$J][$I]$E, b: $B/[$K][J]E) -> (c: [K][I]E)
|
matrix_mul_differ :: proc(a: $A/matrix[$I, $J]$E, b: $B/matrix[J, $K]E) -> (c: matrix[I, K]E)
|
||||||
where !IS_ARRAY(E), IS_NUMERIC(E), I != K #no_bounds_check {
|
where !IS_ARRAY(E), IS_NUMERIC(E), I != K #no_bounds_check {
|
||||||
for k in 0..<K {
|
return a * b
|
||||||
for j in 0..<J {
|
|
||||||
for i in 0..<I {
|
|
||||||
c[k][i] += a[j][i] * b[k][j]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
matrix_mul_vector :: proc(a: $A/[$I][$J]$E, b: $B/[I]E) -> (c: B)
|
matrix_mul_vector :: proc(a: $A/matrix[$I, $J]$E, b: $B/[J]E) -> (c: B)
|
||||||
where !IS_ARRAY(E), IS_NUMERIC(E) #no_bounds_check {
|
where !IS_ARRAY(E), IS_NUMERIC(E) #no_bounds_check {
|
||||||
for i in 0..<I {
|
return a * b
|
||||||
for j in 0..<J {
|
|
||||||
c[j] += a[i][j] * b[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
quaternion_mul_quaternion :: proc(q1, q2: $Q) -> Q where IS_QUATERNION(Q) {
|
quaternion_mul_quaternion :: proc(q1, q2: $Q) -> Q where IS_QUATERNION(Q) {
|
||||||
@@ -270,8 +227,8 @@ mul :: proc{
|
|||||||
vector_to_ptr :: proc(v: ^$V/[$N]$E) -> ^E where IS_NUMERIC(E), N > 0 #no_bounds_check {
|
vector_to_ptr :: proc(v: ^$V/[$N]$E) -> ^E where IS_NUMERIC(E), N > 0 #no_bounds_check {
|
||||||
return &v[0]
|
return &v[0]
|
||||||
}
|
}
|
||||||
matrix_to_ptr :: proc(m: ^$A/[$I][$J]$E) -> ^E where IS_NUMERIC(E), I > 0, J > 0 #no_bounds_check {
|
matrix_to_ptr :: proc(m: ^$A/matrix[$I, $J]$E) -> ^E where IS_NUMERIC(E), I > 0, J > 0 #no_bounds_check {
|
||||||
return &m[0][0]
|
return &m[0, 0]
|
||||||
}
|
}
|
||||||
|
|
||||||
to_ptr :: proc{vector_to_ptr, matrix_to_ptr}
|
to_ptr :: proc{vector_to_ptr, matrix_to_ptr}
|
||||||
|
|||||||
+520
-543
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,10 @@
|
|||||||
package linalg
|
package linalg
|
||||||
|
|
||||||
|
/*
|
||||||
|
These procedures are to allow for swizzling with non-compile (runtime) known components
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
Scalar_Components :: enum u8 {
|
Scalar_Components :: enum u8 {
|
||||||
x = 0,
|
x = 0,
|
||||||
r = 0,
|
r = 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user