python


How to create random orthonormal matrix in python numpy


Is there a method that I can call to create a random orthonormal matrix in python? Possibly using numpy? Or is there a way to create a orthonormal matrix using multiple numpy methods? Thanks.
This is the rvs method pulled from the https://github.com/scipy/scipy/pull/5622/files, with minimal change - just enough to run as a stand alone numpy function.
def 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
It matches Warren's test, http://stackoverflow.com/a/38426572/901925
Version 0.18 of scipy has scipy.stats.ortho_group and scipy.stats.special_ortho_group. The pull request where it was added is https://github.com/scipy/scipy/pull/5622
For example,
In [24]: from scipy.stats import ortho_group # Requires version 0.18 of scipy
In [25]: m = ortho_group.rvs(dim=3)
In [26]: m
Out[26]:
array([[-0.23939017, 0.58743526, -0.77305379],
[ 0.81921268, -0.30515101, -0.48556508],
[-0.52113619, -0.74953498, -0.40818426]])
In [27]: np.set_printoptions(suppress=True)
In [28]: m.dot(m.T)
Out[28]:
array([[ 1., 0., -0.],
[ 0., 1., 0.],
[-0., 0., 1.]])
You can obtain a random n x n orthogonal matrix Q, (uniformly distributed over the manifold of n x n orthogonal matrices) by performing a QR factorization of an n x n matrix with elements i.i.d. Gaussian random variables of mean 0 and variance 1. Here is an example:
import numpy as np
from scipy.linalg import qr
n = 3
H = np.random.randn(n, n)
Q, R = qr(H)
print (Q.dot(Q.T))
[[ 1.00000000e+00 -2.77555756e-17 2.49800181e-16]
[ -2.77555756e-17 1.00000000e+00 -1.38777878e-17]
[ 2.49800181e-16 -1.38777878e-17 1.00000000e+00]]

Related Links

HTMLTestRunner not working
How can I sum values in a list? [duplicate]
Python Dictionary Sorting Using Sorted
xlwt: 'CommonToken' object has no attribute 'txt'
Python repeat while user enters negative number
Assign different operators during python loop
When installing through pip, how do I tell matplotlib how to find tkinter?
Regex for weekdays in python [closed]
Detect Changes to Environment Variables
Select all by type: Geometry. Equivalent Python script?
Sorting algorithm doesn't work
How to avoid “Name 're' is not defined” error while connecting pycharm remote debugger?
Python - Generate binary list with itertools
Capture domain and path from URL with regex
Pylab animation multiple figures
Django Password Reset Email Error 10060

Categories

HOME
security
mql4
laravel-5.2
spring-cloud-contract
avl-tree
codeblocks
tee
pdo
riak
ada
pyephem
yosys
switching
widevine
fetch-api
websauna
cell-array
strophe
percentage
clockwork
canvasjs
swiftcharts
fabric-digits
node-horseman
phpspreadsheet
hashcat
forecasting
redhat-datavirt
vmd
poco-libraries
magiczoomplus
haskell-pipes
soundjs
spring-bean
amazon-clouddrive
gd
rpostgresql
httplib2
static-code-analysis
build-process
wcf-ria-services
shipitjs
project-online
distribute
spinlock
sun-codemodel
fill
interactive-brokers
bluesnap
void
difference
coldfusion-7
enyo
vips
datasnap
bridge
isml
web-component-tester
magento-1.12
libsndfile
ultrawingrid
odftoolkit
mov
funq
method-overriding
ifft
voldemort
healthvault
facebook-chat
sslexception
goinstant
dynamic-binding
visual-studio-2003
mailcore
pep8
xml-libxml
sequelpro
couchdb-lucene
gjs
visual-web-gui
maven-ant-tasks
hinstance
wchar
handwriting
phpcrawl
junit3
pivotal-crm
carbide
filetime
opcodes
writing

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App