前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Sweet Snippet 之 Gram-Schmidt 正交化

Sweet Snippet 之 Gram-Schmidt 正交化

作者头像
用户2615200
发布2019-08-29 10:43:34
5680
发布2019-08-29 10:43:34
举报
文章被收录于专栏:tkokof 的技术,小趣及杂念

Gram-Schmidt 正交化的简单实现

Gram-Schmidt(格拉姆-施密特) 正交化可以正交化一组给定的向量,使这些向量两两垂直,这里列出一份简单的实现(Lua):

代码语言:javascript
复制
-- vector add
function add(a, b)
    if a and b and #a == #b then
        local ret = {}
        
        for i = 1, #a do
            table.insert(ret, a[i] + b[i])
        end
        
        return ret
    end
end

-- vector sub
function sub(a, b)
    if a and b and #a == #b then
        local ret = {}
        
        for i = 1, #a do
            table.insert(ret, a[i] - b[i])
        end
        
        return ret
    end
end

-- dot product
function dot(a, b)
    if a and b and #a == #b then
        local ret = 0
        
        for i = 1, #a do
            ret = ret + a[i] * b[i]
        end
        
        return ret
    end
end

-- magnitude
function mag(a)
    local val = dot(a, a)
    if val then
        return math.sqrt(val)
    end
end

-- normalize, do not change param
function norm(a)
    local magnitude = mag(a)
    if magnitude and magnitude ~= 0 then
        local normalize = {}
        
        for i = 1, #a do
            table.insert(normalize, a[i] / magnitude)
        end
        
        return normalize
    end
end

-- project a to b
function proj(a, b)
    if a and b and #a == #b then
        local norm_b = norm(b)
        local val = dot(a, norm_b)
        
        if val then
            local projection = {}
            
            for i = 1, #norm_b do
                table.insert(projection, norm_b[i] * val)
            end
            
            return projection
        end
    end
end

-- perpendicular a to b
function perp(a, b)
    local projection = proj(a, b)
    if projection then
        return sub(a - projection)
    end
end

-- gram schmidt
function gram_schmidt(...)
    local vecs = { ... }
    local ret = {}
    
    if #vecs > 0 then
        table.insert(ret, vecs[1])
    end
    
    for i = 2, #vecs do
        local base = vecs[i]
        for j = 1, i - 1 do
            base = sub(base, proj(vecs[i], vecs[j]))
        end
        table.insert(ret, base)
        vecs[i] = base
    end
    
    return table.unpack(ret)
end

-- use to check gram schmidt result
function check_perp(...)
    local vecs = { ... }
    for i = 1, #vecs - 1 do
        for j = i + 1, #vecs do
            local val = dot(vecs[i], vecs[j])
            if math.abs(val) > 0.001 then
                return false
            end
        end
    end
    
    return true
end

有兴趣的朋友可以试试这组向量的 Gram-Schmidt 正交化:

a=(1,0,0,1)b=(0,1,0,1)c=(0,0,1,1)d=(0,1,1,1) \begin{aligned} & a = ( 1, 0, 0, 1 ) \\ & b = ( 0, 1, 0, 1 ) \\ & c = ( 0, 0, 1, 1 ) \\ & d = ( 0, 1, 1, 1 ) \end{aligned} ​a=(1,0,0,1)b=(0,1,0,1)c=(0,0,1,1)d=(0,1,1,1)​

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年08月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Gram-Schmidt 正交化的简单实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档