下面是两个测试,同样的数据,不同的版本,请诸君仔细看来:
1.Matlab版SVD分解
- H = [3.16991321031250,52.4425641326457,2.73475152482102;-8.76695007100685,43.4831885343255,-37.1705395356264;-1.59218748085971,-24.3510937156625,12.8339630267640];
- [U,S,V] = svd(H);
H = [3.16991321031250,52.4425641326457,2.73475152482102;-8.76695007100685,43.4831885343255,-37.1705395356264;-1.59218748085971,-24.3510937156625,12.8339630267640];
[U,S,V^T] = svd(H);
得出来的结果
U =
0.6124 0.7695 -0.1814
0.7073 -0.6357 -0.3091
-0.3531 0.0610 -0.9336
V =
-0.0479 0.2651 0.9630
0.9249 0.3759 -0.0575
-0.3772 0.8880 -0.2631
S =
77.2740 0 0
0 29.8639 0
0 0 3.7601
2.Python版SVD分解
- from numpy import *
- def test():
- Base = array([[1.92028349427775,0.938200267748656,8.61139811393332,6.71431139674026,3.47712671277525,2.62145317727807,2.42785357820962,3.59228210401861],[1.38874202829155,5.25404403859336,4.84853333552102,7.41257943454207,1.49997253831683,0.444540922782385,4.42402313001943,7.36340074301202],[6.96266337082995,5.30344218392863,3.93456361215266,5.20052467390387,5.86092067231462,7.54933267231179,6.87796085120107,3.94707475278763]])
- U, S, V = linalg.svd(H)
- if __name__==‘__main__’:
- test()
from numpy import *
def test():
Base = array([[1.92028349427775,0.938200267748656,8.61139811393332,6.71431139674026,3.47712671277525,2.62145317727807,2.42785357820962,3.59228210401861],[1.38874202829155,5.25404403859336,4.84853333552102,7.41257943454207,1.49997253831683,0.444540922782385,4.42402313001943,7.36340074301202],[6.96266337082995,5.30344218392863,3.93456361215266,5.20052467390387,5.86092067231462,7.54933267231179,6.87796085120107,3.94707475278763]])
U, S, V = linalg.svd(H)
if __name__=='__main__':
test()
得出的结果
U =
array([[ 0.61236946, 0.7694889 , -0.18135734],
[ 0.70732576, -0.63574186, -0.30906725],
[-0.35312027, 0.06098463, -0.93358821]])
V =
array([[-0.04785176, 0.92488861, -0.37721515],
[ 0.26505669, 0.37586648, 0.88795796],
[ 0.96304473, -0.05749305, -0.26313379]])
S =
array([ 77.27402814, 29.86391746, 3.76009282])
array([[ 0.61236946, 0.7694889 , -0.18135734],
[ 0.70732576, -0.63574186, -0.30906725],
[-0.35312027, 0.06098463, -0.93358821]])
V =
array([[-0.04785176, 0.92488861, -0.37721515],
[ 0.26505669, 0.37586648, 0.88795796],
[ 0.96304473, -0.05749305, -0.26313379]])
S =
array([ 77.27402814, 29.86391746, 3.76009282])
请注意,两者得出的U和S都是一致的,但是得出的V却是转置关系!!
在科学研究中,这不算是一个是个大问题,可是这一个小小的转置却给我带来了非常大的麻烦,这个svd分解只是我代码中很小的一部分,但是就是这个“转置”,浪费了我很多时间。
Python是一个伟大的语言,扩展之丰富那可谓“只有你想不到的,没有他不存在的!”,可是有时候多反而会闲的稍显杂乱,例如科学计算中我们经常用到numpy和scipy这两个库,可是这两个库却存在着大量相同的函数,你有你的numpy.pi,我有我的scipy.pi,你有求逆,我也有求逆,你又svd,我也有。。。重复的却标准不统一便造成了极大的不便。