栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在python numpy中创建随机正交矩阵

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在python numpy中创建随机正交矩阵

这是

rvs
从https://github.com/scipy/scipy/pull/5622/files中提取的方法,只需进行最小的更改-
足以作为独立的numpy函数运行。

import numpy as npdef rvs(dim=3):     random_state = np.random     H = np.eye(dim)     D = np.ones((dim,))     for n in range(1, dim):         x = random_state.normal(size=(dim-n+1,))         D[n-1] = np.sign(x[0])         x[0] -= D[n-1]*np.sqrt((x*x).sum())         # Householder transformation         Hx = (np.eye(dim-n+1) - 2.*np.outer(x, x)/(x*x).sum())         mat = np.eye(dim)         mat[n-1:, n-1:] = Hx         H = np.dot(H, mat)         # Fix the last sign such that the determinant is 1     D[-1] = (-1)**(1-(dim % 2))*D.prod()     # Equivalent to np.dot(np.diag(D), H) but faster, apparently     H = (D*H.T).T     return H

它符合沃伦的测试,https://stackoverflow.com/a/38426572/901925



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/626191.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号