Skip to content

Instantly share code, notes, and snippets.

@castaneai
Created June 13, 2012 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save castaneai/2923055 to your computer and use it in GitHub Desktop.
Save castaneai/2923055 to your computer and use it in GitHub Desktop.
グラム・シュミットの正規直交化法
# -*- coding: utf8 -*-
import math
'''
ベクトルの内積を計算する
'''
def inner(vector1, vector2):
return sum([component1 * component2 for component1, component2 in zip(vector1, vector2)])
'''
ベクトルのノルムを計算する
'''
def norm(vector):
return math.sqrt(sum([component * component for component in vector]))
'''
ベクトルに対してスカラー倍演算をする
'''
def scalar_multiplication(vector, scalar):
return tuple([scalar * component for component in vector])
'''
ベクトルの足し算をする
'''
def vector_add(vector1, vector2):
return tuple([component1 + component2 for component1, component2 in zip(vector1, vector2)])
'''
ベクトルの引き算をする
'''
def vector_sub(vector1, vector2):
vector2_temp = tuple([-1 * component for component in vector2])
return vector_add(vector1, vector2_temp)
'''
単位ベクトルを求める
'''
def vector_unit(vector):
return scalar_multiplication(vector, 1 / norm(vector))
'''
グラム・シュミットの正規直交化法を行う
'''
def orthonormalization(vector_list):
result_list = []
for index, vector in enumerate(vector_list):
if index == 0:
result = vector_unit(vector)
else:
unit_vector = vector_unit(vector_list[index - 1])
projection_vector = scalar_multiplication(unit_vector, inner(vector, unit_vector))
temp_vector = vector_sub(vector, projection_vector)
result = vector_unit(temp_vector)
result_list.append(result)
return result_list
if __name__ == '__main__':
vector_list = [
(1,1,0),
(0,-1,1),
(1,1,1)
]
print orthonormalization(vector_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment