# Gram-Schmidt正交化

## Classical Gram-Schmidt

https://nbviewer.jupyter.org/github/mitmath/18335/blob/master/notes/Gram-Schmidt.ipynb

## Modified Gram-Schmidt

https://wenku.baidu.com/view/6872ac728e9951e79b892700.html

## Python实现

``````# -*- coding: utf-8 -*-
import numpy as np

def cgs(A):
m, n = A.shape
Q = np.copy(A)
R = np.zeros([n, n], dtype='float64')

for i in range(n):
R[:i, i] = A[:, i].T.dot(Q[:, :i])
Q[:, [i]] = A[:, [i]] - Q[:, :i].dot(R[:i, [i]])

R[i, i] = np.linalg.norm(Q[:, i])
if np.abs(R[i, i]) < 1e-15:
Q[:, i] = np.zeros(m, dtype='float64')
R[i, i] = 0.
else:
Q[:, i] /= R[i, i]
return Q, R

def mgs(A):
m, n = A.shape
Q = np.copy(A)
R = np.zeros([n, n], dtype='float64')

for i in range(n):
R[i, i] = np.linalg.norm(Q[:, i])
if np.abs(R[i, i]) < 1e-15:
Q[:, i] = np.zeros(m, dtype='float64')
R[i, i] = 0.
else:
Q[:, i] /= R[i, i]

R[[i], i+1:] = Q[:, [i]].T.dot(Q[:, i+1:])
Q[:, i+1:] -= Q[:, [i]].dot(R[[i], i+1:])
return Q, R
``````
原文作者：峡谷相对论
原文地址: https://blog.csdn.net/qq_33552519/article/details/103325018
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。