-
-
Notifications
You must be signed in to change notification settings - Fork 51.1k
Expand file tree
/
Copy pathbraik_mac_construction.py
More file actions
160 lines (129 loc) · 4.29 KB
/
Copy pathbraik_mac_construction.py
File metadata and controls
160 lines (129 loc) · 4.29 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
153
154
155
156
157
158
159
160
"""
braikenridge_maclaurin_construction
https://mathworld.wolfram.com/ConicSection.html
5 Points define a conic section on a 2D normal orthogonal plane using this technique.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from numpy import array, linalg
@dataclass
class Point:
"""
A point defined by 2 floats representing a length on a normalized
orthogonal coordinate system
default coordinate is the origin
>>> Point(-1.0, 0.0)
Point(x=-1.0, y=0.0)
"""
x: float = 0.0
y: float = 0.0
def __post_init__(self) -> None:
if not isinstance(self.x, (int, float)):
raise TypeError("x must be an int or float numeric value")
if not isinstance(self.y, (int, float)):
raise TypeError("y must be an int or float numeric value")
@dataclass
class BraikMac:
"""
Given a list of 5 points, determine the corresponding
conic section equation and provide it to the user
| x**2 xy y**2 x y 1 |
| x1**2 x1y1 y1**2 x1 y1 1 |
| x2**2 x2y2 y2**2 x2 y2 1 | = 0
| x3**2 x3y3 y3**2 x3 y3 1 |
| x4**2 x4y4 y4**2 x4 y4 1 |
| x5**2 x5y5 y5**2 x5 y5 1 |
>>> p1 = Point(0.0,0.0)
>>> p2 = Point(5.0,0.0)
>>> p3 = Point(2.0,3.0)
>>> p4 = Point(1.0,10.0)
>>> p5 = Point(6.0,7.0)
>>> BraikMac([p1,p2,p3,p4,p5]) # doctest: +NORMALIZE_WHITESPACE
BraikMac(p_list=[Point(x=0.0, y=0.0), Point(x=5.0, y=0.0),
Point(x=2.0, y=3.0), Point(x=1.0, y=10.0), Point(x=6.0, y=7.0)])
"""
p_list: list[Point] = field(default_factory=list)
def __post_init__(self) -> None:
n = 0
for p in self.p_list:
if not isinstance(p, Point):
raise TypeError("Array must be point objects.")
n += 1
if n != 5:
raise TypeError("Array must be 5 point objects.")
@property
def generate(self) -> str:
x1 = self.p_list[0].x
y1 = self.p_list[0].y
x2 = self.p_list[1].x
y2 = self.p_list[1].y
x3 = self.p_list[2].x
y3 = self.p_list[2].y
x4 = self.p_list[3].x
y4 = self.p_list[3].y
x5 = self.p_list[4].x
y5 = self.p_list[4].y
x2_matrix = array(
[
[x1 * y1, y1**2, x1, y1, 1],
[x2 * y2, y2**2, x2, y2, 1],
[x3 * y3, y3**2, x3, y3, 1],
[x4 * y4, y4**2, x4, y4, 1],
[x5 * y5, y5**2, x5, y5, 1],
]
)
a = linalg.det(x2_matrix)
xy_matrix = array(
[
[x1**2, y1**2, x1, y1, 1],
[x2**2, y2**2, x2, y2, 1],
[x3**2, y3**2, x3, y3, 1],
[x4**2, y4**2, x4, y4, 1],
[x5**2, y5**2, x5, y5, 1],
]
)
b = -linalg.det(xy_matrix)
y2_matrix = array(
[
[x1**2, x1 * y1, x1, y1, 1],
[x2**2, x2 * y2, x2, y2, 1],
[x3**2, x3 * y3, x3, y3, 1],
[x4**2, x4 * y4, x4, y4, 1],
[x5**2, x5 * y5, x5, y5, 1],
]
)
c = linalg.det(y2_matrix)
x_matrix = array(
[
[x1**2, x1 * y1, y1**2, y1, 1],
[x2**2, x2 * y2, y2**2, y2, 1],
[x3**2, x3 * y3, y3**2, y3, 1],
[x4**2, x4 * y4, y4**2, y4, 1],
[x5**2, x5 * y5, y5**2, y5, 1],
]
)
d = -linalg.det(x_matrix)
y_matrix = array(
[
[x1**2, x1 * y1, y1**2, x1, 1],
[x2**2, x2 * y2, y2**2, x2, 1],
[x3**2, x3 * y3, y3**2, x3, 1],
[x4**2, x4 * y4, y4**2, x4, 1],
[x5**2, x5 * y5, y5**2, x5, 1],
]
)
e = linalg.det(y_matrix)
const_matrix = array(
[
[x1**2, x1 * y1, y1**2, x1, y1],
[x2**2, x2 * y2, y2**2, x2, y2],
[x3**2, x3 * y3, y3**2, x3, y3],
[x4**2, x4 * y4, y4**2, x4, y4],
[x5**2, x5 * y5, y5**2, x5, y5],
]
)
f = -linalg.det(const_matrix)
return f"0 = {a:+.2} X**2 {b:+.2} XY {c:+.2} Y**2 {d:+.2} X {e:+.2} Y {f:+.2}"
if __name__ == "__main__":
from doctest import testmod
testmod()