如何将高斯消元应用到任意矩阵中? Lua中的数组

我正在尝试使用Gaussian Elimination来加速计算梯形矩阵的工作。

当我尝试运行它时,它并不起作用。

我想运行这个程序在这些矩阵上。 你能把这个程序应用在下面的矩阵上并告诉我它们给你什么吗 ?

--[[

在Lua中实现高斯消元

--]]

-- 输出矩阵
local function printmatrix (m)
    for _, row in ipairs (m) do
        io.write (table.concat (row, ', ') .. '\n')
   end
end

-- 读取CSV格式的矩阵
local function readcsv (file)
    io.input (file)
    local m = {columns = 0, rectangular = true}
    for line in io.lines () do
        local row = {}
        -- 下一行比较棘手,并遍历行中的所有条目
        for w in line:gmatch '[^,]+' do
            row [#row + 1] = tonumber (w) or 0
        end
        m [#m + 1] = row
        -- 更新矩阵维度
        m.rectangular = m.rectangular and (#row == m.columns or #m == 1)
        m.columns = #row > m.columns and #row or m.columns
    end
    return m
end

-- 如果m[r][c]为零则交换行r和一些行i >r以使m[r][c]为非零,如果可能的话
local function swap (m, r, c)
    local nrows, ncols = #m, m.columns
    if r <= 0 or r > nrows or c <= 0 or c > ncols then error 'Position out of range' end
    if m [r] [c] ~= 0 then
        -- 无事可做
        return
    end
    -- 找到合适的行
    local i = r + 1
    while i <= nrows and m [i] [c] == 0 do
        i = i + 1
    end
    if i <= nrows then
        m [r], m [i] = m [i], m [r]
    end
end

-- 如果m[r][c]为非零则应用行操作,使得i >r时每个m[i][c]==0
local function clear (m, r, c)
    local nrows, ncols = #m, m.columns
    if r <= 0 or r > nrows or c <= 0 or c > ncols then error 'Position out of range' end
    if m [r] [c] == 0 then
        -- 无事可做
        return
    end
    for i = r + 1, nrows do
        local f = m [i] [c] / m [r] [c]
        for j = 1, #m [i] do
            m [i] [j] = m [i] [j] - f * m [r] [j]
        end
    end
end

-- 将高斯消元应用于m,使其变为梯形形式
function echelon (m)
    local nrows, ncols = #m, m.columns
    local r, c = 1, 1 -- 当前位置
    while r <= nrows and c <= ncols do
        -- 尝试在此位置得到非零值
        swap (m, r, c)
        if m [r] [c] == 0 then
            -- 没有,所以移向右侧
            c = c + 1
        else
            clear (m, r, c)
            -- 完成,所以向对角线移动
            r = r + 1
            c = c + 1
        end
    end
end

local m = readcsv (arg [1])
print '原始矩阵:'
printmatrix (m)
if m.rectangular then
    echelon (m)
    print '梯形形式:'
    printmatrix (m)
else
    error '矩阵不是矩形的!'
end

在以下矩阵上运行该程序,您将得到什么:

1,3,5,7
2,1,-1,0
3,4,4,7
5,5,3,7
1, 1, 1, 1, 1
2, 2, 0, 1, 1
3, 3, 1, 2, 1
4, 4, 0, 0, 0
 1, 2, 3, 4, 5, 6, 7, 8, 9
-1,-2,-3,-4,-5,-6,-7,-8,-8
 1, 1, 1, 2, 2, 2, 3, 3, 3
 1, 2, 3, 1, 2, 3, 1, 2, 3
 0,-1,-2,-2,-3,-4,-4,-5,-6
 9, 8, 7, 6, 5, 4, 3, 2, 1
点赞
用户6632736
用户6632736

多么熟悉的一段代码。

for line in io.lines() do 循环内部包装在 if line ~= '' then ... end 中安全处理空行。

完成后:

alexander@maicube:~$ lua t.lua t1.csv
原始数据:
1, 3, 5, 7
2, 1, -1, 0
3, 4, 4, 7
5, 5, 3, 7
简化阶梯形矩阵:
1, 3, 5, 7
0, -5, -11, -14
0, 0, 0, 0
0, 0, 0, 0
alexander@maicube:~$ lua t.lua t2.csv
原始数据:
1, 1, 1, 1, 1
2, 2, 0, 1, 1
3, 3, 1, 2, 1
4, 4, 0, 0, 0
简化阶梯形矩阵:
1, 1, 1, 1, 1
0, 0, -2, -1, -1
0, 0, 0, -2, -2
0, 0, 0, 0, -1
alexander@maicube:~$ lua t.lua t3.csv
原始数据:
1, 2, 3, 4, 5, 6, 7, 8, 9
-1, -2, -3, -4, -5, -6, -7, -8, -8
1, 1, 1, 2, 2, 2, 3, 3, 3
1, 2, 3, 1, 2, 3, 1, 2, 3
0, -1, -2, -2, -3, -4, -4, -5, -6
9, 8, 7, 6, 5, 4, 3, 2, 1
简化阶梯形矩阵:
1, 2, 3, 4, 5, 6, 7, 8, 9
0, -1, -2, -2, -3, -4, -4, -5, -6
0, 0, 0, -3, -3, -3, -6, -6, -6
0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0
2020-11-01 16:14:37