Lua实现矩阵的加减乘除

2023-05-16

Lua实现矩阵的加减乘除

参考文章:

https://blog.csdn.net/qq_54180412/article/details/122943327

https://www.bilibili.com/video/BV1434y187uj/?spm_id_from=333.337.search-card.all.click&vd_source=c52914e7c156e681a9f8a7825139815d

  矩阵的加、减、乘无需多说,主要是矩阵的除法,我使用的是伴随矩阵以及结合行列式的思路求解矩阵之间的除法。

版本一

Matrix = {
    __add = function(tbSource, tbDest)
        assert(tbSource,"tbSource not exist")
        assert(tbDest,  "tbDest not exist")
        if tbSource.nRow ~= tbDest.nRow 
            or tbSource.nColumn ~= tbDest.nColumn then
            print("row or column not equal...")
            return tbSource
        else
            for rowKey,rowValue in ipairs(tbSource.tbData) do
                for colKey,colValue in ipairs(tbSource.tbData[rowKey]) do
                    tbSource.tbData[rowKey][colKey] = 
                        tbSource.tbData[rowKey][colKey] + tbDest.tbData[rowKey][colKey]
                end
            end 
            return tbSource
        end
    end,

    __sub = function(tbSource, tbDest)
        assert(tbSource,"tbSource not exist")
        assert(tbDest,  "tbDest not exist")
        if tbSource.nRow ~= tbDest.nRow 
            or tbSource.nColumn ~= tbDest.nColumn then
            print("row or column not equal...")
            return tbSource
        else
            for rowKey,rowValue in ipairs(tbSource.tbData) do
                for colKey,colValue in ipairs(tbSource.tbData[rowKey]) do
                    tbSource.tbData[rowKey][colKey] = 
                        tbSource.tbData[rowKey][colKey] - tbDest.tbData[rowKey][colKey]
                end
            end 
            return tbSource
        end
    end,

    __mul = function(tbSource, tbDest)
        return Matrix:_MartixMul(tbSource, tbDest)
    end,

    __div = function(tbSource, tbDest)
        assert(tbSource,"tbSource not exist")
        assert(tbDest,  "tbDest not exist")
        
        local nDet = Matrix:_GetDetValue(tbDest)
        if nDet == 0 then
            print("matrix no inverse matrix...")
            return nil
        end
        local tbInverseDest = Matrix:_MatrixNumMul(Matrix:_GetCompanyMatrix(tbDest), 1 / nDet)
        -- Matrix:_GetCompanyMatrix(tbDest):Print()
        -- print(nDet)
        -- tbInverseDest:Print()
        return Matrix:_MartixMul(tbSource, tbInverseDest)
    end
}

-- 构建一个Matrix对象
function Matrix:New(data)
    assert(data,"data not exist")
    local tbMatrix = {}
    setmetatable(tbMatrix, self)
    self.__index    = self

    tbMatrix.tbData = data
    tbMatrix.nRow   = #data
    if tbMatrix.nRow > 0 then
        tbMatrix.nColumn = (#data[1])
    else
        tbMatrix.nColumn = 0
    end
    return tbMatrix
end

-- 输出当前Matrix数据信息
function Matrix:Print()
    for rowKey,rowValue in ipairs(self.tbData) do
        for colKey,colValue in ipairs(self.tbData[rowKey]) do
            io.write(self.tbData[rowKey][colKey],',')
        end
        print('')
    end
end

-- 切割,切去第rowIndex以及第colIndex列
function Matrix:_CutoffMatrix(tbMatrix, rowIndex, colIndex)
    assert(tbMatrix,"tbMatrix not exist")
    assert(rowIndex >= 1,"rowIndex < 1")
    assert(colIndex >= 1,"colIndex < 1")
    local tbRes   = Matrix:New({})
    tbRes.nRow    = tbMatrix.nRow    - 1
    tbRes.nColumn = tbMatrix.nColumn - 1
    for i = 1, tbMatrix.nRow - 1 do
        for j = 1, tbMatrix.nColumn - 1 do
            if tbRes.tbData[i] == nil then
                tbRes.tbData[i] = {}
            end
            
            local nRowDir = 0
            local nColDir = 0
            if i >= rowIndex then
                nRowDir = 1
            end

            if j >= colIndex then
                nColDir = 1
            end

            tbRes.tbData[i][j] = tbMatrix.tbData[i + nRowDir][j + nColDir]
        end
    end
    return tbRes
end


-- 获取矩阵的行列式对应的值
function Matrix:_GetDetValue(tbMatrix)
    assert(tbMatrix,"tbMatrix not exist")
    -- 当矩阵为一阶矩阵时,直接返回A中唯一的元素
    if tbMatrix.nRow == 1 then
        return tbMatrix.tbData[1][1]
    end

    local nAns = 0
    for i = 1, tbMatrix.nColumn do
        local nFlag = -1
        if i % 2 ~= 0 then
            nFlag = 1
        end

        nAns = 
            nAns + tbMatrix.tbData[1][i] * 
                Matrix:_GetDetValue(Matrix:_CutoffMatrix(tbMatrix, 1, i)) * nFlag
        -- print("_GetDetValue nflag:",nFlag)
    end
    return nAns
end


-- 获取矩阵的伴随矩阵
function Matrix:_GetCompanyMatrix(tbMatrix)
    assert(tbMatrix,"tbMatrix not exist")
    local tbRes   = Matrix:New({})
    -- 伴随矩阵与原矩阵存在转置关系
    tbRes.nRow    = tbMatrix.nColumn
    tbRes.nColumn = tbMatrix.nRow

    for i = 1, tbMatrix.nRow do
        for j = 1, tbMatrix.nColumn do
            local nFlag = 1
            if ((i + j) % 2) ~= 0 then
                nFlag = -1
            end       
            
            if tbRes.tbData[j] == nil then
                tbRes.tbData[j] = {}
            end
            -- print(Matrix:_GetDetValue(Matrix:_CutoffMatrix(tbMatrix, i, j)))
            -- Matrix:_CutoffMatrix(tbMatrix, i, j):Print()
            -- print("---11----")

            tbRes.tbData[j][i] = 
                Matrix:_GetDetValue(Matrix:_CutoffMatrix(tbMatrix, i, j)) * nFlag
        end
    end
    return tbRes
end


-- 矩阵数乘
function Matrix:_MatrixNumMul(tbMatrix, num)
    for i = 1, tbMatrix.nRow do
        for j = 1, tbMatrix.nColumn do
            tbMatrix.tbData[i][j] = tbMatrix.tbData[i][j] * num
        end
    end
    return tbMatrix
end


-- 矩阵相乘
function Matrix:_MartixMul(tbSource, tbDest)
    assert(tbSource,"tbSource not exist")
    assert(tbDest,  "tbDest not exist")
    if tbSource.nColumn ~= tbDest.nRow then
        print("column not equal row...")
        return tbSource
    else
        local tbRes = Matrix:New({})
        for i = 1, tbSource.nRow do
            for j = 1, tbDest.nColumn do
                if tbRes.tbData[i] == nil then
                    tbRes.tbData[i] = {}
                end
                
                if tbRes.tbData[i][j] == nil then
                    tbRes.tbData[i][j] = 0
                end

                for k = 1, tbSource.nColumn do
                    tbRes.tbData[i][j] = 
                        tbRes.tbData[i][j] + (tbSource.tbData[i][k] * tbDest.tbData[k][j])
                end
            end
        end
        tbRes.nRow    = tbSource.nRow
        tbRes.nColumn = tbDest.nColumn
        return tbRes
    end
end









-- 矩阵加法
local matrix1 = Matrix:New({{1,2,3},{4,5,6}})
local matrix2 = Matrix:New({{2,3,4},{5,6,7}})

matrix1 = matrix1 + matrix2
matrix1:Print()
print("-----------------------------------")

-- 矩阵减法
local matrix3 = Matrix:New({{1,1,1},{1,1,1}})
matrix1 = matrix1 - matrix3
matrix1:Print()
print("-----------------------------------")


-- 矩阵乘法
local matrix4 = Matrix:New({{1,2,3},{4,5,6}})
local matrix5 = Matrix:New({{7,8},{9,10},{11,12}})

local matrix6 = matrix4 * matrix5
matrix6:Print()
print("-----------------------------------")


-- 矩阵除法
local matrix7 = Matrix:New({{1,2,3},{4,5,6},{7,8,0}})
local matrix8 = Matrix:New({{1,2,1},{1,1,2},{2,1,1}})
local matrix9 = matrix7 / matrix8
matrix9:Print()

  输出结果:

效果图

版本二

  在使用第一版实现后,发现对Lua面向对象的思维不够清晰,顾查阅网上资料实现以下版本。

  Lua面向对象类的封装这里不做过多阐述,可参考文章:https://blog.csdn.net/qq135595696/article/details/128827618

local _class = {}

function class(super)
    local tbClassType = {}
    tbClassType.Ctor  = false
    tbClassType.super = super
    tbClassType.New   = function(...)
        local tbObj   = {}
        do
            local funcCreate
            funcCreate = function(tbClass,...)
                if tbClass.super then
                    funcCreate(tbClass.super,...)
                end
                
                if tbClass.Ctor then
                    tbClass.Ctor(tbObj,...)
                end
            end
            funcCreate(tbClassType,...)
        end
        -- 防止调用Ctor初始化时,在Ctor内部设置了元表的情况发生
        if getmetatable(tbObj) then
            getmetatable(tbObj).__index = _class[tbClassType] 
        else
            setmetatable(tbObj, { __index = _class[tbClassType] })
        end
        return tbObj
    end

    local vtbl          = {} 
    _class[tbClassType] = vtbl

    setmetatable(tbClassType, { __newindex = 
        function(tb,k,v)
            vtbl[k] = v
        end
    })

    if super then
        setmetatable(vtbl, { __index = 
            function(tb,k)
                local varRet = _class[super][k]
                vtbl[k]      = varRet
                return varRet
            end
        })
    end
    return tbClassType
end







Matrix = class()

function Matrix:Ctor(data)
    self.tbData = data
    self.nRow   = #data
    if self.nRow > 0 then
        self.nColumn = (#data[1])
    else
        self.nColumn = 0
    end

    setmetatable(self,{
        __add = function(tbSource, tbDest)
            assert(tbSource,"tbSource not exist")
            assert(tbDest,  "tbDest not exist")
            if tbSource.nRow ~= tbDest.nRow 
                or tbSource.nColumn ~= tbDest.nColumn then
                print("row or column not equal...")
                return tbSource
            else
                for rowKey,rowValue in ipairs(tbSource.tbData) do
                    for colKey,colValue in ipairs(tbSource.tbData[rowKey]) do
                        tbSource.tbData[rowKey][colKey] = 
                            tbSource.tbData[rowKey][colKey] + tbDest.tbData[rowKey][colKey]
                    end
                end 
                return tbSource
            end
        end,
    
        __sub = function(tbSource, tbDest)
            assert(tbSource,"tbSource not exist")
            assert(tbDest,  "tbDest not exist")
            if tbSource.nRow ~= tbDest.nRow 
                or tbSource.nColumn ~= tbDest.nColumn then
                print("row or column not equal...")
                return tbSource
            else
                for rowKey,rowValue in ipairs(tbSource.tbData) do
                    for colKey,colValue in ipairs(tbSource.tbData[rowKey]) do
                        tbSource.tbData[rowKey][colKey] = 
                            tbSource.tbData[rowKey][colKey] - tbDest.tbData[rowKey][colKey]
                    end
                end 
                return tbSource
            end
        end,
    
        __mul = function(tbSource, tbDest)
            return self:_MartixMul(tbSource, tbDest)
        end,
    
        __div = function(tbSource, tbDest)
            assert(tbSource,"tbSource not exist")
            assert(tbDest,  "tbDest not exist")
            
            local nDet = self:_GetDetValue(tbDest)
            if nDet == 0 then
                print("matrix no inverse matrix...")
                return nil
            end
            local tbInverseDest = self:_MatrixNumMul(self:_GetCompanyMatrix(tbDest), 1 / nDet)
            -- Matrix:_GetCompanyMatrix(tbDest):Print()
            -- print(nDet)
            -- tbInverseDest:Print()
            return self:_MartixMul(tbSource, tbInverseDest)
        end
    }
)


end

function Matrix:Print()
    for rowKey,rowValue in ipairs(self.tbData) do
        for colKey,colValue in ipairs(self.tbData[rowKey]) do
            io.write(self.tbData[rowKey][colKey],',')
        end
        print('')
    end
end

-- 切割,切去第rowIndex以及第colIndex列
function Matrix:_CutoffMatrix(tbMatrix, rowIndex, colIndex)
    assert(tbMatrix,"tbMatrix not exist")
    assert(rowIndex >= 1,"rowIndex < 1")
    assert(colIndex >= 1,"colIndex < 1")
    local tbRes   = Matrix.New({})
    tbRes.nRow    = tbMatrix.nRow    - 1
    tbRes.nColumn = tbMatrix.nColumn - 1
    for i = 1, tbMatrix.nRow - 1 do
        for j = 1, tbMatrix.nColumn - 1 do
            if tbRes.tbData[i] == nil then
                tbRes.tbData[i] = {}
            end
            
            local nRowDir = 0
            local nColDir = 0
            if i >= rowIndex then
                nRowDir = 1
            end

            if j >= colIndex then
                nColDir = 1
            end

            tbRes.tbData[i][j] = tbMatrix.tbData[i + nRowDir][j + nColDir]
        end
    end
    return tbRes
end

-- 获取矩阵的行列式对应的值
function Matrix:_GetDetValue(tbMatrix)
    assert(tbMatrix,"tbMatrix not exist")
    -- 当矩阵为一阶矩阵时,直接返回A中唯一的元素
    if tbMatrix.nRow == 1 then
        return tbMatrix.tbData[1][1]
    end

    local nAns = 0
    for i = 1, tbMatrix.nColumn do
        local nFlag = -1
        if i % 2 ~= 0 then
            nFlag = 1
        end

        nAns = 
            nAns + tbMatrix.tbData[1][i] * 
                self:_GetDetValue(self:_CutoffMatrix(tbMatrix, 1, i)) * nFlag
        -- print("_GetDetValue nflag:",nFlag)
    end
    return nAns
end


-- 获取矩阵的伴随矩阵
function Matrix:_GetCompanyMatrix(tbMatrix)
    assert(tbMatrix,"tbMatrix not exist")
    local tbRes   = Matrix.New({})
    -- 伴随矩阵与原矩阵存在转置关系
    tbRes.nRow    = tbMatrix.nColumn
    tbRes.nColumn = tbMatrix.nRow

    for i = 1, tbMatrix.nRow do
        for j = 1, tbMatrix.nColumn do
            local nFlag = 1
            if ((i + j) % 2) ~= 0 then
                nFlag = -1
            end       
            
            if tbRes.tbData[j] == nil then
                tbRes.tbData[j] = {}
            end
            -- print(Matrix:_GetDetValue(Matrix:_CutoffMatrix(tbMatrix, i, j)))
            -- Matrix:_CutoffMatrix(tbMatrix, i, j):Print()
            -- print("---11----")

            tbRes.tbData[j][i] = 
                self:_GetDetValue(self:_CutoffMatrix(tbMatrix, i, j)) * nFlag
        end
    end
    return tbRes
end


-- 矩阵数乘
function Matrix:_MatrixNumMul(tbMatrix, num)
    for i = 1, tbMatrix.nRow do
        for j = 1, tbMatrix.nColumn do
            tbMatrix.tbData[i][j] = tbMatrix.tbData[i][j] * num
        end
    end
    return tbMatrix
end


-- 矩阵相乘
function Matrix:_MartixMul(tbSource, tbDest)
    assert(tbSource,"tbSource not exist")
    assert(tbDest,  "tbDest not exist")
    if tbSource.nColumn ~= tbDest.nRow then
        print("column not equal row...")
        return tbSource
    else
        local tbRes = Matrix.New({})
        for i = 1, tbSource.nRow do
            for j = 1, tbDest.nColumn do
                if tbRes.tbData[i] == nil then
                    tbRes.tbData[i] = {}
                end
                
                if tbRes.tbData[i][j] == nil then
                    tbRes.tbData[i][j] = 0
                end

                for k = 1, tbSource.nColumn do
                    tbRes.tbData[i][j] = 
                        tbRes.tbData[i][j] + (tbSource.tbData[i][k] * tbDest.tbData[k][j])
                end
            end
        end
        tbRes.nRow    = tbSource.nRow
        tbRes.nColumn = tbDest.nColumn
        return tbRes
    end
end



-- 矩阵加法
local matrix1 = Matrix.New({{1,2,3},{4,5,6}})
local matrix2 = Matrix.New({{2,3,4},{5,6,7}})

matrix1 = matrix1 + matrix2
matrix1:Print()
print("-----------------------------------")

-- 矩阵减法
local matrix3 = Matrix.New({{1,1,1},{1,1,1}})
matrix1 = matrix1 - matrix3
matrix1:Print()
print("-----------------------------------")


-- 矩阵乘法
local matrix4 = Matrix.New({{1,2,3},{4,5,6}})
local matrix5 = Matrix.New({{7,8},{9,10},{11,12}})

local matrix6 = matrix4 * matrix5
matrix6:Print()
print("-----------------------------------")


-- 矩阵除法
local matrix7 = Matrix.New({{1,2,3},{4,5,6},{7,8,0}})
local matrix8 = Matrix.New({{1,2,1},{1,1,2},{2,1,1}})
local matrix9 = matrix7 / matrix8
matrix9:Print()

  输出结果:

效果图

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Lua实现矩阵的加减乘除 的相关文章

  • 3D打印机DIY之一------Prusa i3的材料清单和总体结构组装

    自己使用铝件和亚克力板组装了一台Prusa i3 3D打印机 xff0c 现在把详细的过程记录下来 总体效果图 xff1a 一 材料清单 元件数量总价2020欧式铝方管 xff1a 4根400mm 3根340mm 1根150mm 1根130
  • 位置式PID与增量式PID的介绍和代码实现

    PID分为位置式PID与增量式PID 一 位置式PID 1 表达式为 xff1a 2 缺点 xff1a 1 xff09 由于全量输出 xff0c 所以每次输出均与过去状态有关 xff0c 计算时要对ek进行累加 xff0c 工作量大 xff
  • 常见蓝牙模块介绍和AT指令

    目录 一 HC 05主从一体蓝牙模块 二 HC 06从机蓝牙模块 三 低功耗BLE蓝牙4 0模块 cc2540或cc2541 四 JDY 10 蓝牙4 0 BLE模块 五 蓝牙模块LAYOUT注意事项 xff1a 常见的蓝牙模块为 xff1
  • PID参数调节总结

    原文链接 xff1a 点击打开链接 经验 xff1a 1 采样频率低 xff08 如500ms xff09 xff0c Kp一般是0 01级别 xff1b 采样频率高 xff08 如1ms xff09 xff0c Kp一般是1级别 2 先只
  • 为Keil添加注释的快捷键

    Keil刚装上是没有注释快捷键的 xff0c 可以自己添加 xff0c Edit Configuration xff0c 然后选择 Shortcut Keys 标签页 xff0c 下拉找到 Comment Selection xff0c 然
  • C语言__attribute__的使用

    一 介绍 GNU C 的一大特色就是 attribute 机制 attribute 可以设置函数属性 xff08 Function Attribute xff09 变量属性 xff08 Variable Attribute xff09 和类
  • ros(30):while(ros::ok()){}理解与使用示例

    while ros ok 理解 while ros ok 是ros的一种循环模式 xff0c 和ros Rate loop rate ros spinOnce 等结合可以控制程序的执行顺序 ros Rate loop rate 10 设置循
  • Jetson TX2更换软件源

    TX2的软件源为国外服务器 xff0c 网速会很慢 xff0c 需要换国内的ARM源 备份 etc lib路径下的source list文件 xff0c 然后在终端 xff08 按ctrl 43 alt 43 T打开 xff09 执行以下命
  • 使用Dockerfile创建镜像

    Dockerfile是一个文本格式的配置文件 xff0c 可以使用Dockerfile来快速创建自定义的镜像 一 基本结构 Dockerfile由一行行命令语句组成 xff0c 并且支持以 开头的注释行 一般而言 xff0c Dockerf
  • 高质量嵌入式Linux C编程 第二章 数据 学习笔记

    一 什么是数据类型 xff1f 数据类型包含两方面的内容 xff0c 数据的表示和对数据加工的操作 数据的全部可能表示构成数据类型的值的集合 xff0c 数据全部合理的操作构成数据类型的操作集合 二 什么是变量 xff1f 其值在作用域内可
  • 高质量嵌入式Linux C编程 第三章 运算符、表达式学习

    一 运算符有哪几类 xff1f xff08 1 xff09 算数运算符 xff1a 43 43 43 七种 xff08 2 xff09 关系运算符 xff1a gt lt 61 61 gt 61 lt 61 xff01 61 六种 xff0
  • 设计一种可全向移动的球形机器人

    一 前言 之前在网上看到一种球形机器人 xff0c 觉得很有趣 xff0c 而且原理也比较简单 xff0c 大概就是把一辆小车放在一个透明的亚克力球中 xff0c 控制小车使球体滚动 xff0c 姿态控制算法与平衡小车类似 xff0c 然后
  • ROS-Python

    用python来编写ros话题 服务方面常用的几个点 xff1a 话题topic 1 初始化节点 rospy init node 34 节点名字 34 举例 xff1a rospy init node 34 test 34 anonymou
  • 关于SBUS信号在单片机中的一些个人理解

    最近一直在弄关于SBUS的编码与解码这方面的内容 xff0c 虽然网上资料很多 xff0c 但是网上资料太杂 xff0c 我找的一些资料可能是我理解的问题 xff0c 所以我摒弃了一些骚操作 xff0c 对于一些单片机学得不精的人来说 xf
  • C++中的指针

    以下为本人大一时阅读 C 43 43 Primer Plus 关于指针的章节所做的笔记 指针 一个函数只能返回一个值 xff0c 但是如果使用指针来传递变量 xff0c 则可以修改更多的值 xff0c 可以达到返回不止一个值的效果 用指针作
  • 四旋翼建模

    文章整理于 xff1a 共轴八旋翼无人飞行器姿态与航迹跟踪控制研究 彭程点击打开链接
  • 最新定时更新:Spring Cloud 各个版本整理汇总。最新为2022,Kilburn版本

    网址 xff1a https spring io 英文中文终结版本boot大版本boot代表说明Angel安吉尔SR61 2 X1 2 8GABrixton布里克斯顿SR71 3 X1 3 8GACamden卡梅登SR71 4 X1 4 2
  • 深度学习(23):SmoothL1Loss损失函数

    0 基本介绍 SmoothL1Loss是一种常用的损失函数 xff0c 通常用于回归任务中 xff0c 其相对于均方差 MSE 损失函数的优势在于对异常值 如过大或过小的离群点 的惩罚更小 xff0c 从而使模型更加健壮 SmoothL1L
  • Jetson Nano\NX\AGX 学习笔记

    Jetson Nano NX AGX 学习笔记 1 jetson平台安装pytorch torchvision2 yolov5模型部署deepstream x86 jetson平台2 0 下载 96 deepstream6 1 96 2 1
  • jetson系列硬件性能对比

    参考 Jetson Modules https developer nvidia com embedded jetson modules 第一种 4G 价格 1000元 1x 4K30 2x1080p60 4x1080p30 4x720p6

随机推荐

  • 遍历_EPROCESS->ObjectTable->HandleTableList链表枚举进程

    include lt ntifs h gt include lt ntddk h gt UCHAR PsGetProcessImageFileName in PEPROCESS Process HANDLE PsGetProcessInhe
  • Ubunut18.04安装Autoware1.12

    官方提供了两种安装Autoware的方法 xff1a Docker和Source两种方式 下边以Ubuntu18 04环境为例 xff0c 记录Source的安装方法 1 配置要求 1 1硬件 1 2软件 Qt安装 qmake span c
  • PuTTY在远程连接服务器之后,经常会断线提示“Software caused connection abort”,而且经常在很短的时间内就失去连接

    解决方案如下 xff1a 第一步 xff1a 设置服务器 1 修改服务器中 etc ssh sshd config 文件 xff0c 将LoginGraceTime的值设为0 xff0c TCPKeepAlive 设为yes 2 servi
  • Go语言循环语句

    Go语言循环语句 资料参考至菜鸟教程 在不少实际问题中有许多具有规律性的重复操作 xff0c 因此在程序中就需要重复执行某些语句 以下为大多编程语言循环程序的流程图 xff1a Go语言提供了以下几种类型循环处理语句 xff1a 循环类型描
  • Lua 协同程序(coroutine)

    Lua 协同程序 coroutine 参考文章 xff1a 菜鸟教程 https zhuanlan zhihu com p 480357405 https zhuanlan zhihu com p 76249973 Lua 协同程序 cor
  • Lua 变量

    Lua 变量 参考至菜鸟教程 变量在使用前 xff0c 需要在代码中进行声明 xff0c 即创建该变量 变量需要标识类型是因为编译程序执行代码之前需要知道如何给语句变量开辟存储区 xff0c 用于存储变量的值 Lua变量有三种类型 xff1
  • Lua 函数 - 可变参数

    Lua 函数 可变参数 参考至菜鸟教程 Lua函数可以接收可变数目的参数 xff0c 和C语言类似 xff0c 在函数参数列表中使用三点 表示函数有可变的参数 span class token keyword function span s
  • Lua 运算符 - 较为特殊部分

    Lua 运算符 较为特殊部分 参考至菜鸟教程 算术运算符 操作符描述实例 乘幂A 2 输出结果 100 负号 A 输出结果 10 整除运算符 gt 61 lua5 3 5 2 输出结果 2 在 lua 中 xff0c 用作除法运算 xff0
  • python(9):python循环打印进度条

    1 while 循环 Python的while循环可以打印进度条 xff0c 可以使用tqdm这个库来实现 tqdm是一个用于在Python中添加进度条的库 xff0c 它可以很容易地集成到while循环中 下面是一个简单的示例 xff0c
  • 平衡车直立PID调节总结

    苦战一周 xff0c 终于使平衡小车站了起来 xff0c PID无疑是我从学习板子至今遇到最困难的东西了 xff0c 并不是说它原理有多么复杂 xff0c 只是想让小车的效果更佳 xff0c 调参的过程无疑是漫长而艰难的 连续调了俩天的参数
  • Lua 字符串

    Lua 字符串 参考至菜鸟教程 字符串或串 String 是由数字 字母 下划线组成的一串字符 Lua 语言中字符串可以使用以下三种方式来表示 xff1a 单引号间的一串字符 双引号间的一串字符 与 间的一串字符 以上三种方式的字符串实例如
  • Lua 迭代器

    Lua 迭代器 参考文章 xff1a 菜鸟教程 https cloud tencent com developer article 2203215 迭代器 xff08 iterator xff09 是一种对象 xff0c 它能够用来遍历标准
  • Lua table(表)

    Lua table 表 参考至菜鸟教程 Lua table 使用关联型数组 xff0c 你可以用任意类型的值来作数组的索引 xff0c 但这个值不能是 nil Lua table 是不固定大小的 xff0c 你可以根据自己需要进行扩容 Lu
  • Lua 模块与包

    Lua 模块与包 参考至菜鸟教程 模块类似于一个封装库 xff0c 从 Lua 5 1 开始 xff0c Lua 加入了标准的模块管理机制 xff0c 可以把一些公用的代码放在一个文件里 xff0c 以 API 接口的形式在其他地方调用 x
  • Lua 文件I/O

    Lua 文件I O 参考至菜鸟教程 Lua I O 库用于读取和处理文件 分为简单模式 xff08 和C一样 xff09 完全模式 简单模式 xff08 simple model xff09 拥有一个当前输入文件和一个当前输出文件 xff0
  • Lua 错误处理

    Lua 错误处理 参考至菜鸟教程 程序运行中错误处理是必要的 xff0c 在我们进行文件操作 xff0c 数据转移及web service 调用过程中都会出现不可预期的错误 如果不注重错误信息的处理 xff0c 就会造成信息泄露 xff0c
  • Lua 调试(Debug)

    Lua 调试 Debug 参考至菜鸟教程 Lua 提供了 debug 库用于提供创建我们自定义调试器的功能 Lua 本身并未有内置的调试器 xff0c 但很多开发者共享了他们的 Lua 调试器代码 Lua 中 debug 库包含以下函数 x
  • Lua 垃圾回收

    Lua 垃圾回收 参考至菜鸟教程 Lua 采用了自动内存管理 这意味着你不用操心新创建的对象需要的内存如何分配出来 xff0c 也不用考虑在对象不再被使用后怎样释放它们所占用的内存 Lua运行了一个垃圾收集器来收集所有死对象 xff08 即
  • Lua 面向对象(详解)

    Lua 面向对象 xff08 详解 xff09 参考文章 xff1a https blog csdn net linxinfa article details 103254828 https zhuanlan zhihu com p 115
  • Lua实现矩阵的加减乘除

    Lua实现矩阵的加减乘除 参考文章 xff1a https blog csdn net qq 54180412 article details 122943327 https www bilibili com video BV1434y18