-
Notifications
You must be signed in to change notification settings - Fork 1
/
alu_demo.py
81 lines (51 loc) · 1.69 KB
/
alu_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from detviz import *
import math
print (''' The determinant is the area of a unit square that you see in the plot ''')
def draw_and_show(m, plt_name = "GRAPH"):
plot_setup(plt_name)
draw_matrix(m, "maroon", 0.5, "-", llgram = True)
draw_axes_for_matrix(m, "red", 0.1, "-")
plt.show()
def rotate_vector(v, rad):
assert (len(v) == 2)
return [v[0] * math.cos(rad) + v[1] * (-math.sin(rad)),
v[0] * math.sin(rad) + v[1] * math.cos(rad)]
def rotate(m, deg):
# sanity check
assert (len(m) == 2 or len(m) == 1)
assert (len(m[0]) == 2)
if len(m) == 2:
assert (len(m[1]) == 2)
ret = []
for i in range(0, len(m)):
ret.append(rotate_vector(m[i], math.radians(deg)))
return ret
def stick_to_x(m, vidx = 0):
# sanity check
assert (len(m) == 2 or len(m) == 1)
assert (len(m[0]) == 2)
if len(m) == 2:
assert (len(m[1]) == 2)
assert (vidx < len(m))
# calculate the degrees of the first vector off the x axis
deg = math.degrees(math.atan2(m[vidx][1], m[vidx][0]))
return rotate(m, -deg)
## main ########################################################################
## A
A = [[2, 1], [1, 2]]
## L
L = [[1, -0.5], [0, 1]]
## U
U = [[2, 0], [1, 1.5]]
## ROT A
ROTA = stick_to_x(A)
print "A ", A
print "ROTA", ROTA
plot_setup("A = LU")
draw_matrix(A, "maroon", 0.5, "-", llgram = True)
draw_matrix(L, "green", 0.5, "-", llgram = True)
draw_matrix(U, "cyan", 0.9, "-", llgram = True)
draw_matrix(ROTA, "red", 0.9, "--", llgram = True)
#draw_matrix(stick_to_x(A), "red", 0.5, "--", llgram = True)
plt.show()
################################################################################