31 lines
543 B
Text
31 lines
543 B
Text
require "matrix"
|
|
local fmt = require "fmt"
|
|
|
|
local function cramer(a, d)
|
|
local n = a:numrows()
|
|
local x = {}
|
|
local ad = a:det()
|
|
for c = 1, n do
|
|
local aa = a:copy()
|
|
for r = 1, n do aa:set(r, c, d:get(r, 1)) end
|
|
x[c] = aa:det() / ad
|
|
end
|
|
return x
|
|
end
|
|
|
|
local a = matrix.from({
|
|
{2, -1, 5, 1},
|
|
{3, 2, 2, -6},
|
|
{1, 3, 3, -1},
|
|
{5, -2, -3, 3}
|
|
})
|
|
|
|
local d = matrix.from({
|
|
{ -3},
|
|
{-32},
|
|
{-47},
|
|
{ 49}
|
|
})
|
|
|
|
local x = cramer(a, d)
|
|
fmt.print("Solution is %s", fmt.swrite(x))
|