Some minor stuff -
- your member functions and properties should be lower-case by PEP8
- you should add a property to get the discriminant, particularly since you use it in multiple places
- I don't know what
time_dependent and x_plus_3 do, nor why they're here. They seem like specific applications of the quadratic formula for a narrow situation. Given that, they should not be in this class.
- The returns from
vietas_formula should not be strings; you should return a tuple of floats. Similar for vertex. Currently this is premature stringizing. Your roots already does this correctly.
- If you wanted to be thorough,
d < 0 should not raise a ZeroError and instead should return complex roots. Python has built-in support for complex numbers.
self.x does not belong as a member on the class.
- Have you renamed
DeltaError to ZeroError?
Exception.__init__ should be changed to use super()
- The error message
Delta must be greater than 0! is not strictly true, and should be greater than or equal to zero.
- Why must
a, b, c be int? You should accept float.
Some major stuff: have you checked this for correctness? I see at least three algebraic errors, including that d = sqrt(b * b - 4 * a * c) is calling sqrt too early to catch failures, and -b / 2 * a has incorrect order of operations. Unit testing for this kind of code is both easy and important; while translating your code I ran into test failures and had to make corrections almost every step of the way.
Suggested
I still don't understand why your code needs to take a break (it's actually pretty funny. Maybe it's unionized?), but so be it:
from cmath import sqrt, isclose
from datetime import datetime, time
from numbers import Complex
from typing import Union, Tuple
RootTypes = Union[
Tuple[Complex],
Tuple[Complex, Complex],
]
WORK_HOURS_START = time(8)
WORK_HOURS_END = time(16)
class Quadratic:
def __init__(self, a: float, b: float, c: float):
self.a, self.b, self.c = a, b, c
def y(self, x: Complex) -> Complex:
a, b, c = self.a, self.b, self.c
return a*x*x + b*x + c
def dydx(self, x: Complex) -> Complex:
a, b = self.a, self.b
return 2*a*x + b
@property
def discriminant(self) -> float:
a, b, c = self.a, self.b, self.c
return b*b - 4*a*c
@property
def roots(self) -> RootTypes:
a, b, c, d = self.a, self.b, self.c, self.discriminant
if d == 0:
return -b/2/a,
sqrt_d = sqrt(d)
return (-b + sqrt_d)/2/a, (-b - sqrt_d)/2/a
@property
def vietas_formula(self) -> Tuple[
float, # sum
float, # product
]:
a, b, c = self.a, self.b, self.c
return -b/a, c/a
@property
def vertex(self) -> Tuple[
float, # p
float, # q
]:
a, b, c, d = self.a, self.b, self.c, self.discriminant
return -b/a/2, -d/a/4
def describe(self) -> str:
v1, v2 = self.vietas_formula
return (
f'Roots: {self.roots}\n'
f'Vieta constants: x1+x2={v1}, x1*x2={v2}\n'
f'Vertex: {self.vertex}'
)
def time_problem() -> None:
if WORK_HOURS_START <= datetime.now().time() < WORK_HOURS_END:
a, b, c = 1, 5, 6
quad = Quadratic(a, b, c)
print(quad.describe())
else:
print("I'm on a break for some reason.")
def abs_close(x: Complex, y: Complex) -> bool:
return isclose(x, y, abs_tol=1e-12)
def test_two_real() -> None:
q = Quadratic(1, 5, 6)
x1, x2 = q.roots
assert abs_close(0, x1.imag)
assert abs_close(0, x2.imag)
assert abs_close(0, q.y(x1))
assert abs_close(0, q.y(x2))
v1, v2 = q.vietas_formula
assert abs_close(v1, x1 + x2)
assert abs_close(v2, x1 * x2)
vx, vy = q.vertex
assert abs_close(0, q.dydx(vx))
assert abs_close(vy, q.y(vx))
def test_one_real() -> None:
q = Quadratic(9, -6, 1)
x, = q.roots
assert abs_close(0, x.imag)
assert abs_close(0, q.y(x))
# In this case Vieta's formula can be interpreted as two identical superimposed roots
x1, x2 = x, x
v1, v2 = q.vietas_formula
assert abs_close(v1, x1 + x2)
assert abs_close(v2, x1 * x2)
vx, vy = q.vertex
assert abs_close(0, q.dydx(vx))
assert abs_close(0, vy)
def test_two_complex() -> None:
q = Quadratic(4, -4, 3)
x1, x2 = q.roots
assert not abs_close(0, x1.imag)
assert not abs_close(0, x2.imag)
assert abs_close(0, q.y(x1))
assert abs_close(0, q.y(x2))
v1, v2 = q.vietas_formula
assert abs_close(v1, x1 + x2)
assert abs_close(v2, x1 * x2)
vx, vy = q.vertex
assert abs_close(0, q.dydx(vx))
assert abs_close(vy, q.y(vx))
def test() -> None:
test_two_real()
test_one_real()
test_two_complex()
if __name__ == '__main__':
test()
time_problem()
@property? \$\endgroup\$@propertyis a decorator; your wording just befuddled me a little. \$\endgroup\$@propertyoverall is quite good, all things considered. \$\endgroup\$