Created
August 7, 2012 09:42
-
-
Save MartinThoma/3283952 to your computer and use it in GitHub Desktop.
Create a small random matrix for practicing matrix calculations (jordan normal form)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import random, pprint, numpy | |
def numpyMatrixToWolframAlpha(A): | |
""" | |
Convert the represenation to one that Wolfram|Alpha accepts | |
""" | |
string = "{" | |
for i, line in enumerate(A): | |
if i > 0: | |
string += "," | |
string += "{" | |
for j, el in enumerate(numpy.array(line).reshape(-1,).tolist()): | |
if j > 0: | |
string += "," | |
string += str(el) | |
string += "}" | |
string += "}" | |
return string | |
def generateNiceInvertibleMatrix(n): | |
skipUglyValues = False | |
if n == 2: | |
a = b = c = d = 1 | |
while (a*d - b*c) == 0 or (a*d-b*c) > 13: | |
a = random.randint(-10, 10) | |
b = random.randint(-10, 10) | |
c = random.randint(-10, 10) | |
d = random.randint(-10, 10) | |
return numpy.matrix([[a, b], [c, d]]) | |
elif n == 3: | |
a = b = c = d = e = f = g = h = i = 1 | |
while (a*e*i - a*f*h - b*d*i + b*f*g + c*d*h - c*e*g) == 0 or \ | |
a*e*i - a*f*h - b*d*i + b*f*g + c*d*h - c*e*g > 13: | |
a = random.randint(-10, 10) | |
b = random.randint(-10, 10) | |
c = random.randint(-10, 10) | |
d = random.randint(-10, 10) | |
e = random.randint(-10, 10) | |
f = random.randint(-10, 10) | |
g = random.randint(-10, 10) | |
h = random.randint(-10, 10) | |
i = random.randint(-10, 10) | |
return numpy.matrix([[a, b, c], [d, e, f], [g, h, i]]) | |
elif n == 4: | |
a=b=c=d=e=f=g=h=i=j=k=l=m=n=o=p=1 | |
while (a*f*k*p-a*f*l*o-a*g*j*p+a*g*l*n+a*h*j*o-a*h*k*n-b*e*k*p+b*e*l*o+b*g*i*p-b*g*l*m-b*h*i*o+b*h*k*m+c*(e*j*p-e*l*n-f*i*p+f*l*m+h*i*n-h*j*m) + d*(-e*j*o+e*k*n+f*i*o-f*k*m-g*i*n+g*j*m)) == 0 or (a*f*k*p-a*f*l*o-a*g*j*p+a*g*l*n+a*h*j*o-a*h*k*n-b*e*k*p+b*e*l*o+b*g*i*p-b*g*l*m-b*h*i*o+b*h*k*m+c*(e*j*p-e*l*n-f*i*p+f*l*m+h*i*n-h*j*m) + d*(-e*j*o+e*k*n+f*i*o-f*k*m-g*i*n+g*j*m)) > 13: | |
a = random.randint(-10, 10) | |
b = random.randint(-10, 10) | |
c = random.randint(-10, 10) | |
d = random.randint(-10, 10) | |
e = random.randint(-10, 10) | |
f = random.randint(-10, 10) | |
g = random.randint(-10, 10) | |
h = random.randint(-10, 10) | |
i = random.randint(-10, 10) | |
j = random.randint(-10, 10) | |
k = random.randint(-10, 10) | |
l = random.randint(-10, 10) | |
m = random.randint(-10, 10) | |
n = random.randint(-10, 10) | |
o = random.randint(-10, 10) | |
p = random.randint(-10, 10) | |
return numpy.matrix([[a,b,c,d],[e,f,g,h],[i,j,k,l],[m,n,o,p]]) | |
elif n == 5: | |
while True: | |
try: | |
matrix = [] | |
for i in xrange(5): | |
line = [] | |
for j in xrange(n): | |
line.append(random.randint(-10, 10)) | |
matrix.append(line) | |
S = numpy.matrix(matrix) | |
T = numpy.linalg.inv(S) | |
noBreak = False | |
if skipUglyValues: | |
for number in numpy.array(T).reshape(-1,).tolist(): | |
if len(str(number)) > 4: | |
noBreak = True | |
if not noBreak: | |
break | |
except numpy.linalg.LinAlgError: | |
print S | |
print("Created non-invertible matrix. Next try.") | |
return S | |
def createPracticeMatrix(minN=2, maxN=5): | |
n = random.randint(minN, maxN) | |
# create diagonal matrix | |
matrix = [] | |
for i in xrange(n): | |
line = [] | |
for j in xrange(n): | |
if i != j: | |
line.append(0) | |
else: | |
line.append(random.randint(-10, 10)) | |
matrix.append(line) | |
D = numpy.matrix(matrix) | |
# base change matrix | |
S = generateNiceInvertibleMatrix(n) | |
T = numpy.linalg.inv(S) | |
return (S, D, T) | |
if __name__ == "__main__": | |
print("Here is your Matrix") | |
S, D, T = createPracticeMatrix() | |
print(S) | |
print(D) | |
print(T) | |
print("=") | |
print S*D*T | |
print numpyMatrixToWolframAlpha(S*D*T) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment