-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeterminant-visualize.py
More file actions
152 lines (110 loc) · 3.47 KB
/
Copy pathdeterminant-visualize.py
File metadata and controls
152 lines (110 loc) · 3.47 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# running command: streamlit run <filename>
from matplotlib import pyplot as plt
from matplotlib import patches
from functools import partial
import itertools as it
import streamlit as st
import operator as op
import numpy as np
import math
def to_polar(point: tuple[float, float], origin=(0, 0)):
dx = point[0] - origin[0]
dy = point[1] - origin[1]
d = math.sqrt(dx**2 + dy**2)
r = math.atan2(dy, dx)
return r, d
def partialize(points):
return [
list(map(op.itemgetter(i), points))
for i in range(len(points[0]))
]
def center_of_mass(points):
n = len(points)
return tuple(map(lambda x: sum(x)/n, partialize(points)))
def arange(points):
return sorted(
points,
key=partial(
to_polar,
origin=center_of_mass(points)
)
)
def plot_shape(points):
figure, axis = plt.subplots()
x, y = partialize(arange(points))
axis.scatter(x, y)
axis.scatter(*center_of_mass(points))
axis.plot(x + [x[0]], y + [y[0]])
return figure, axis
def binarize(x):
return bin(int(x))[2:]
def complete_square(n):
# perfect cube
return [
tuple(map(int, row.rjust(n, '0')))
for row in map(binarize, range(2**n))
]
def apply_matrix(matrix, shape):
return np.array([matrix @ shape[i] for i in range(len(shape))])
def line_by_points(p1, p2, axis, **kwargs):
return axis.plot(
(p1[0], p2[0]),
(p1[1], p2[1]),
**kwargs
)
def plot_shape(shape, color='black', ax=None, label=None):
if ax is None:
fig, ax = plt.subplots()
for point in shape:
axis.scatter(*point, c=color)
for p1, p2 in it.pairwise(shape):
line_by_points(p1, p2, ax, color=color)
line_by_points(shape[-1], shape[0], ax, color=color, label=label)
return ax
def evaluate(phrase):
try:
return eval(phrase)
except Exception:
return None
def evaluate_line(line):
if line.strip():
return list(map(evaluate, line.split()))
# polygon = patches.Rectangle((0, 0), 1, 1, edgecolor='red', facecolor='none')
# axis.add_patch(polygon)
if __name__ == '__main__':
st.set_page_config(page_title='Determinant')
placeholder = st.empty()
st.header('Shape Transformation by Matrix')
c1, c2 = st.columns(2)
c11, c12 = c1.columns(2)
a11 = c11.number_input('A11', value=1)
a21 = c11.number_input('A21', value=0)
a12 = c12.number_input('A12', value=0)
a22 = c12.number_input('A22', value=1)
shape = c2.text_area('Shape (square by default)', height=125, placeholder='x1 y1 \nx2 y2 \nx3 y3')
matrix = np.array([[a11, a12], [a21, a22]])
# matrix = list(map(evaluate_line, matrix.splitlines()))
n = len(matrix)
if n != 2:
placeholder.warning('Matrix must be 2D')
st.stop()
elif any(len(matrix[i]) != n for i in range(n)):
placeholder.warning('Matrix must be square')
st.stop()
if shape:
shape = list(map(evaluate_line, shape.splitlines()))
else:
shape = complete_square(n)
shape = np.array(arange(shape))
matrix = np.array(matrix)
new_shape = apply_matrix(matrix, shape)
st.subheader('')
figure, axis = plt.subplots()
# axis.set_title('Matrixes')
plot_shape(shape, ax=axis, color='blue', label='Original')
plot_shape(new_shape, ax=axis, color='red', label='Transformed')
axis.set_box_aspect(1)
axis.legend()
axis.grid(True)
# figure.tight_layout()
st.pyplot(figure)