-
Notifications
You must be signed in to change notification settings - Fork 1
/
quadratic_form_contour_R2.py
90 lines (74 loc) · 3.27 KB
/
quadratic_form_contour_R2.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
82
83
84
85
86
87
88
89
90
from quadratic_form_contour import *
import numpy as np
from numpy import sin, cos, pi
from conjugacy_math import *
from plotly_vectors_R2 import draw_vector_R2
from plotly_tools import Scatter_R2
class Quad_Form_Contour_R2(Quad_Form_Contour):
def __init__(self):
super(Quad_Form_Contour_R2, self).__init__(
draw_vector_R2,
np.diag([4., 3.]),
np.eye(2),
np.eye(2),
np.zeros((2,)),
np.array( [-715.,349] ),
default_num_pts=80
)
return
def ellipsoid_from_ellipsoid_and_map( self, E, name=None, color=None, M=np.eye(2), center=np.zeros((2,)), num_pts=40 ):
c = center.flatten()
y = E[1]
E = np.vstack(( E[0].flatten() - c[0], E[1].flatten() - c[1] ))
E = M.dot(E)
E = np.vstack(( E[0].flatten() + c[0], E[1].flatten() + c[1] ))
hoverinfo = 'none' if name is None else 'name'
if color is None: color = 'rgb(0,255,0)'
ell = Scatter_R2(
x = E[0].flatten(),
y = E[1].flatten(),
name = name,
hoverinfo = hoverinfo,
color=color,
width=1.5,
mode = 'lines, text'
)
axes_max = np.max( np.abs(E) )
return ell, axes_max, E
def ellipsoid_from_unit_ball_and_map( self, name=None, color=None, M=np.eye(2), center=np.zeros((2,)), num_pts=40 ):
theta = np.linspace(0, 2 * pi, num=num_pts)
x = cos(theta)
y = sin(theta)
E = np.vstack(( x.flatten(), y.flatten() ))
c = center.flatten()
E = np.vstack(( E[0].flatten() + c[0], E[1].flatten() + c[1] ))
return self.ellipsoid_from_ellipsoid_and_map( E, name=name, color=color, M=M, center=c, num_pts=num_pts )
def compute_rotation_to_standard_basis(self, vs, debug_print=False):
"""
The goal of this function is to compute a matrix R that, when multiplied by the
given vectors vs, gives us the standard basis vectors in R^2.
"""
standard_basis = np.eye(2)
a = angle_between( vs[:, 0], standard_basis[0] )
a = -a if vs[1, 0] >= 0. else a
if debug_print: print("angle_1={}".format(a))
R = make_rotation_matrix_R2(angle=a, axis='x')
es = R.dot(vs)
assert check_matrices_equal_except_column_sign(es, standard_basis), \
"es doesn't equal standard_basis_vectors\nes={}\nsb={}".format(es, standard_basis)
if debug_print: print("completed rotation around x-axis")
if debug_print: print("R is a good map from the eigenvectors to the standard basis")
return R, es
def prep_line_data_for_vectors(self, lvs, mr, center=np.zeros((2,)), M=np.eye(2), color='rgb(255, 0, 0)', width=1.2):
c = self._origin.flatten() if center is None else center.flatten()
if M is None: M = self._default_M
data = []
eps = [-mr-5, mr+5]
lvs = M.dot(lvs)
l = np.array( [c + ep * (lvs[:,0]) for ep in eps] )
ls1 = Scatter_R2(l[:,0], l[:,1], color=color, width=width)
l = np.array( [c + ep * (lvs[:,1]) for ep in eps] )
ls2 = Scatter_R2(l[:,0], l[:,1], color=color, width=width)
data.append(ls1)
data.append(ls2)
return data