added determinant

This commit is contained in:
Louis Heredero 2024-07-01 13:04:37 +02:00
parent b93db17806
commit 4904a80b50
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
3 changed files with 113 additions and 2 deletions

Binary file not shown.

View File

@ -25,3 +25,16 @@ $
$ $
E = #mat.display(E) E = #mat.display(E)
$ $
#let M = mat.mat(
(2, 4, 5, 6),
(-1, 5, 6, 9),
(3, 7, 1, -6),
(4, -2, 3, 5)
)
$M = #mat.display(M)$
#let d = mat.det(M)
$det(M) = #d$
#mat.det-steps(M)

View File

@ -43,9 +43,9 @@
return mat(..rows) return mat(..rows)
} }
#let display(mat) = { #let display(mat, ..args) = {
_check(mat) _check(mat)
math.mat(..mat.rows) math.mat(..mat.rows, ..args.named())
} }
#let add(mat1, mat2) = { #let add(mat1, mat2) = {
@ -184,4 +184,102 @@
} }
return mat2 return mat2
}
#let co-mat(mat1, elim-x, elim-y) = {
_check(mat1)
let mat2 = of-size(mat1.h - 1, mat1.w - 1)
for y in range(mat2.h) {
for x in range(mat2.w) {
let ox = if x < elim-x {x} else {x + 1}
let oy = if y < elim-y {y} else {y + 1}
mat2.rows.at(y).at(x) = mat1.rows.at(oy).at(ox)
}
}
return mat2
}
#let det(mat) = {
_check(mat)
if mat.w != mat.h {
panic("Matrix must be square")
}
if mat.w == 0 {
return 1
}
let res = 0
for y in range(mat.h) {
let sign = (1 - 2 * calc.rem(y, 2))
let co-fact = det(co-mat(mat, 0, y))
res += sign * mat.rows.at(y).at(0) * co-fact
}
return res
}
#let det-steps(mat) = {
_check(mat)
if mat.w != mat.h {
panic("Matrix must be square")
}
let res = []
let lines = ()
res += display(mat, delim: "|")
let ap = $&$.body
let terms = ((1, mat,),)
for _ in range(mat.w - 2) {
let line = [
#ap =
]
for (i, term) in terms.enumerate() {
let (coef, m) = term
let sub-terms = ()
for y in range(m.h) {
let c = (1 - 2 * calc.rem(y, 2)) * coef * m.rows.at(y).at(0)
let m2 = co-mat(m, 0, y)
if c != 0 {
sub-terms.push((c, m2))
}
if y + i != 0 and c >= 0 {
line += [+]
}
if c != 1 {
if c == -1 {
line += [-]
} else {
line += [#c #sym.dot]
}
}
line += display(m2, delim: "|")
}
terms.at(i) = sub-terms
}
lines.push(line + linebreak())
terms = terms.join()
}
res += lines.join()
res += [#ap =]
let total = 0
for (i, term) in terms.enumerate() {
let (coef, m) = term
let value = coef * det(m)
total += value
if value != 0 {
if i != 0 and value >= 0 {
res += [+]
}
res += [#value]
}
}
res += [=#total]
return math.equation(res)
} }