Skip to content

Commit 0839f4f

Browse files
committed
Update
1 parent 304b444 commit 0839f4f

File tree

2 files changed

+219
-76
lines changed

2 files changed

+219
-76
lines changed

highpymath/__init__.py

Lines changed: 157 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .highpymath import MathValueError as _mve
22

3-
__all__ = ['sum', 'sub', 'mul', 'div', 'MathValueError', 'exp', 'sqrt', 'log', 'reciprocal', 'factorial', 'linear', 'cpi']
3+
__all__ = ['sum', 'sub', 'mul', 'div', 'MathValueError', 'exp', 'sqrt', 'log', 'reciprocal', 'factorial', 'calc_pi', 'calc_e']
44

55
class MathValueError(_mve):
66
"""
@@ -229,62 +229,6 @@ def factorial(a: int, return_int: bool = False, return_string: bool = False):
229229
_result = str(_result)
230230
return _result
231231

232-
def linear(a: any = None, b: any = None, c: any = None, search_a: bool = False, search_b: bool = False, search_c: bool = False, return_int: bool = False, return_string: bool = False):
233-
"""
234-
Solve the Linear Function from type: a + b = c
235-
"""
236-
from .highpymath import linear_base_a as _linear_base_a
237-
from .highpymath import linear_base_b as _linear_base_b
238-
from .highpymath import linear_base_c as _linear_base_c
239-
return_float = True
240-
if return_int:
241-
return_float = False
242-
if search_a and search_b and search_c:
243-
raise MathValueError("You need to specify one of the 3 arguments")
244-
if search_a and search_b:
245-
raise MathValueError("You need to specify one of the 3 arguments")
246-
if search_a and search_c:
247-
raise MathValueError("You need to specify one of the 3 arguments")
248-
if search_b and search_c:
249-
raise MathValueError("You need to specify one of the 3 arguments")
250-
if search_a:
251-
if not isinstance(b, (int, float)):
252-
raise MathValueError("b must be a number")
253-
if not isinstance(c, (int, float)):
254-
raise MathValueError("c must be a number")
255-
if isinstance(b, int):
256-
b = float(b)
257-
if isinstance(c, int):
258-
c = float(c)
259-
_result = _linear_base_a(b=b, c=c)
260-
elif search_b:
261-
if not isinstance(a, (int, float)):
262-
raise MathValueError("a must be a number")
263-
if not isinstance(c, (int, float)):
264-
raise MathValueError("c must be a number")
265-
if isinstance(a, int):
266-
a = float(a)
267-
if isinstance(c, int):
268-
c = float(c)
269-
_result = _linear_base_b(a=a, c=c)
270-
elif search_c:
271-
if not isinstance(a, (int, float)):
272-
raise MathValueError("a must be a number")
273-
if not isinstance(b, (int, float)):
274-
raise MathValueError("b must be a number")
275-
if isinstance(a, int):
276-
a = float(a)
277-
if isinstance(b, int):
278-
b = float(b)
279-
_result = _linear_base_c(a=a, b=b)
280-
if return_int:
281-
_result = int(_result)
282-
elif return_float:
283-
_result = float(_result)
284-
if return_string:
285-
_result = str(_result)
286-
return _result
287-
288232
def sin(a: any, return_int: bool = False, return_string: bool = False):
289233
"""
290234
Create the Sinus of a Number.
@@ -435,15 +379,19 @@ def calc_pi(return_int: bool = False, return_string: bool = False):
435379

436380
pi = calc_pi()
437381

438-
def calc_e(max: int = 1000, return_int: bool = True, return_string: bool = False):
382+
def calc_e(max: int = 20, return_int: bool = False, return_string: bool = False):
439383
"""
440384
Calculate the euler number.
441385
"""
442-
from .highpymath import calc_e as _calc_e
386+
from .highpymath import factorial as _fact
443387
return_float = True
444388
if return_int:
445389
return_float = False
446-
_result = _calc_e(max=max)
390+
_result = 0
391+
i = 0
392+
while i < max:
393+
_result += 1 / _fact(i)
394+
i += 1
447395
if return_int:
448396
_result = int(_result)
449397
elif return_float:
@@ -452,4 +400,152 @@ def calc_e(max: int = 1000, return_int: bool = True, return_string: bool = False
452400
_result = str(_result)
453401
return _result
454402

455-
e = calc_e()
403+
e = calc_e()
404+
405+
class equation:
406+
"""
407+
Class to Solve equations.
408+
"""
409+
@staticmethod
410+
def quadratic(a: any, b: any, c: any = None, use_pq: bool = False, return_int: bool = False, return_string: bool = False) -> tuple:
411+
"""
412+
Function to Solve a Quadratic Equation.
413+
Attention:
414+
- If you use use_pq = True, a will be used as p and b will be used as q.
415+
- If you use use_pq = False, a will be used as a, b will be used as b and c will be used as c.
416+
"""
417+
from .highpymath import quadratic_base as _base
418+
from .highpymath import quadratic_pq as _pq
419+
return_float = True
420+
if return_int:
421+
return_float = False
422+
if not isinstance(a, (int, float)):
423+
raise MathValueError("a must be a Number")
424+
if not isinstance(b, (int, float)):
425+
raise MathValueError("b must be a Number")
426+
if not use_pq and c is None:
427+
raise MathValueError("c is set as None, but you don't use pq")
428+
if not use_pq and not isinstance(c, (int, float)):
429+
raise MathValueError("c must be a Number")
430+
if isinstance(a, int):
431+
a = float(a)
432+
if isinstance(b, int):
433+
b = float(b)
434+
if not use_pq and isinstance(c, int):
435+
c = float(c)
436+
if use_pq:
437+
p = a
438+
q = b
439+
if not use_pq:
440+
_result = _base(a=a, b=b, c=c)
441+
else:
442+
_result = _pq(p=p, q=q)
443+
_result1 = _result[0]
444+
_result2 = _result[1]
445+
if return_int:
446+
_result1 = int(_result1)
447+
_result2 = int(_result2)
448+
elif return_float:
449+
_result1 = float(_result1)
450+
_result2 = float(_result2)
451+
if return_string:
452+
_result1 = str(_result1)
453+
_result2 = str(_result2)
454+
return _result1, _result2
455+
456+
@staticmethod
457+
def linear(a: any = None, b: any = None, c: any = None, search_a: bool = False, search_b: bool = False, search_c: bool = False, return_int: bool = False, return_string: bool = False):
458+
"""
459+
Solve the Linear Function from type: a + b = c
460+
"""
461+
from .highpymath import linear_base_a as _linear_base_a
462+
from .highpymath import linear_base_b as _linear_base_b
463+
from .highpymath import linear_base_c as _linear_base_c
464+
return_float = True
465+
if return_int:
466+
return_float = False
467+
if search_a and search_b and search_c:
468+
raise MathValueError("You need to specify one of the 3 arguments")
469+
if search_a and search_b:
470+
raise MathValueError("You need to specify one of the 3 arguments")
471+
if search_a and search_c:
472+
raise MathValueError("You need to specify one of the 3 arguments")
473+
if search_b and search_c:
474+
raise MathValueError("You need to specify one of the 3 arguments")
475+
if search_a:
476+
if not isinstance(b, (int, float)):
477+
raise MathValueError("b must be a number")
478+
if not isinstance(c, (int, float)):
479+
raise MathValueError("c must be a number")
480+
if isinstance(b, int):
481+
b = float(b)
482+
if isinstance(c, int):
483+
c = float(c)
484+
_result = _linear_base_a(b=b, c=c)
485+
elif search_b:
486+
if not isinstance(a, (int, float)):
487+
raise MathValueError("a must be a number")
488+
if not isinstance(c, (int, float)):
489+
raise MathValueError("c must be a number")
490+
if isinstance(a, int):
491+
a = float(a)
492+
if isinstance(c, int):
493+
c = float(c)
494+
_result = _linear_base_b(a=a, c=c)
495+
elif search_c:
496+
if not isinstance(a, (int, float)):
497+
raise MathValueError("a must be a number")
498+
if not isinstance(b, (int, float)):
499+
raise MathValueError("b must be a number")
500+
if isinstance(a, int):
501+
a = float(a)
502+
if isinstance(b, int):
503+
b = float(b)
504+
_result = _linear_base_c(a=a, b=b)
505+
if return_int:
506+
_result = int(_result)
507+
elif return_float:
508+
_result = float(_result)
509+
if return_string:
510+
_result = str(_result)
511+
return _result
512+
513+
equation = equation()
514+
515+
__all__.append('equation')
516+
517+
def sqrt2(base: any, return_int: bool = False, return_string: bool = False):
518+
"""
519+
Calculate the Square Root of a Number.
520+
"""
521+
from .highpymath import sqrt2 as _sqrt2
522+
return_float = True
523+
if return_int:
524+
return_float = False
525+
_result = _sqrt2(base=base)
526+
if return_int:
527+
_result = int(_result)
528+
elif return_float:
529+
_result = float(_result)
530+
if return_string:
531+
_result = str(_result)
532+
return _result
533+
534+
__all__.append('sqrt2')
535+
536+
def exp2(base: any, return_int: bool = False, return_string: bool = False):
537+
"""
538+
Calculate the Exponentiation of a Number.
539+
"""
540+
from .highpymath import exp2 as _exp2
541+
return_float = True
542+
if return_int:
543+
return_float = False
544+
_result = _exp2(base=base)
545+
if return_int:
546+
_result = int(_result)
547+
elif return_float:
548+
_result = float(_result)
549+
if return_string:
550+
_result = str(_result)
551+
return _result

src/lib.rs

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ fn exp(base: PyFloat, power: PyFloat) -> PyResult<PyFloat> {
5353
Ok(base.powf(power))
5454
}
5555

56+
#[pyfunction]
57+
fn exp2(base: PyFloat) -> PyResult<PyFloat> {
58+
let _result = base.powf(2.0);
59+
Ok(_result)
60+
}
61+
5662
#[pyfunction]
5763
fn sqrt(base: PyFloat, power: PyFloat) -> PyResult<PyFloat> {
5864
if base < 0.0 && power % 2.0 == 0.0 {
@@ -62,6 +68,11 @@ fn sqrt(base: PyFloat, power: PyFloat) -> PyResult<PyFloat> {
6268
}
6369
}
6470

71+
#[pyfunction]
72+
fn sqrt2(base: PyFloat) -> PyResult<PyFloat> {
73+
Ok(base.sqrt())
74+
}
75+
6576
#[pyfunction]
6677
fn log(base: PyFloat, power: PyFloat) -> PyResult<PyFloat> {
6778
if base <= 0.0 || base == 1.0 {
@@ -174,28 +185,61 @@ fn calc_pi() -> PyResult<PyFloat> {
174185
Ok(_pi)
175186
}
176187

177-
fn facto(n: PyUInt) -> PyUInt {
178-
let mut result = 1;
179-
for i in 1..=n {
180-
let new_result = result * i;
181-
if new_result < result { // Überlaufprüfung
182-
panic!("Integer overflow during factorial calculation");
188+
#[pyfunction]
189+
fn quadratic_base(a: PyFloat, b: PyFloat, c: PyFloat) -> PyResult<(PyFloat, PyFloat)> {
190+
if a == 0.0 {
191+
return Err(MathValueError::new_err("Coefficient 'a' must not be zero"));
192+
}
193+
let discriminant = b.powf(2.0) - 4.0 * a * c;
194+
if discriminant.is_nan() || discriminant.is_infinite() {
195+
return Err(MathValueError::new_err("Discriminant calculation resulted in an invalid number"));
196+
}
197+
if discriminant < 0.0 {
198+
let sqrt_discriminant = (-discriminant).sqrt();
199+
let _result1 = (-b / (2.0 * a)) + (sqrt_discriminant / (2.0 * a));
200+
let _result2 = (-b / (2.0 * a)) - (sqrt_discriminant / (2.0 * a));
201+
if _result1.is_nan() || _result1.is_infinite() || _result2.is_nan() || _result2.is_infinite() {
202+
return Err(MathValueError::new_err("Result calculation resulted in an invalid number"));
183203
}
184-
result = new_result;
204+
Ok((_result1, _result2))
205+
} else {
206+
let sqrt_discriminant = discriminant.sqrt();
207+
let _result1 = (-b + sqrt_discriminant) / (2.0 * a);
208+
let _result2 = (-b - sqrt_discriminant) / (2.0 * a);
209+
if _result1.is_nan() || _result1.is_infinite() || _result2.is_nan() || _result2.is_infinite() {
210+
return Err(MathValueError::new_err("Result calculation resulted in an invalid number"));
211+
}
212+
Ok((_result1, _result2))
185213
}
186-
result
187214
}
188215

189216
#[pyfunction]
190-
fn calc_e(max: PyInt) -> PyResult<PyFloat> {
191-
let mut result: PyFloat = 0.0;
192-
for i in 0..max {
193-
let factorial_result = facto(i as PyUInt); // Typkonvertierung, falls notwendig
194-
result += 1.0 / factorial_result as PyFloat; // Korrekte Typumwandlung
217+
fn quadratic_pq(p: PyFloat, q: PyFloat) -> PyResult<(PyFloat, PyFloat)> {
218+
let discriminant = (p / 2.0).powf(2.0) - q;
219+
if discriminant.is_nan() || discriminant.is_infinite() {
220+
return Err(MathValueError::new_err("Discriminant calculation resulted in an invalid number"));
221+
}
222+
if discriminant < 0.0 {
223+
let sqrt_discriminant = (-discriminant).sqrt();
224+
let _result1 = -(p / 2.0) + sqrt_discriminant;
225+
let _result2 = -(p / 2.0) - sqrt_discriminant;
226+
if _result1.is_nan() || _result1.is_infinite() || _result2.is_nan() || _result2.is_infinite() {
227+
return Err(MathValueError::new_err("Result calculation resulted in an invalid number"));
228+
}
229+
Ok((_result1, _result2))
230+
} else {
231+
let sqrt_discriminant = discriminant.sqrt();
232+
let _result1 = -(p / 2.0) + sqrt_discriminant;
233+
let _result2 = -(p / 2.0) - sqrt_discriminant;
234+
if _result1.is_nan() || _result1.is_infinite() || _result2.is_nan() || _result2.is_infinite() {
235+
return Err(MathValueError::new_err("Result calculation resulted in an invalid number"));
236+
}
237+
Ok((_result1, _result2))
195238
}
196-
Ok(result)
197239
}
198240

241+
// ... bestehender Code ...
242+
199243
/// A Python module implemented in Rust.
200244
#[pymodule]
201245
fn highpymath(m: &PyModule) -> PyResult<()> {
@@ -204,7 +248,9 @@ fn highpymath(m: &PyModule) -> PyResult<()> {
204248
m.add_function(wrap_pyfunction!(mul, m)?)?;
205249
m.add_function(wrap_pyfunction!(div, m)?)?;
206250
m.add_function(wrap_pyfunction!(exp, m)?)?;
251+
m.add_function(wrap_pyfunction!(exp2, m)?)?;
207252
m.add_function(wrap_pyfunction!(sqrt, m)?)?;
253+
m.add_function(wrap_pyfunction!(sqrt2, m)?)?;
208254
m.add_function(wrap_pyfunction!(log, m)?)?;
209255
m.add_function(wrap_pyfunction!(factorial, m)?)?;
210256
m.add_function(wrap_pyfunction!(reciprocal, m)?)?;
@@ -219,7 +265,8 @@ fn highpymath(m: &PyModule) -> PyResult<()> {
219265
m.add_function(wrap_pyfunction!(atan, m)?)?;
220266
m.add_function(wrap_pyfunction!(arctan, m)?)?;
221267
m.add_function(wrap_pyfunction!(calc_pi, m)?)?;
222-
m.add_function(wrap_pyfunction!(calc_e, m)?)?;
268+
m.add_function(wrap_pyfunction!(quadratic_base, m)?)?;
269+
m.add_function(wrap_pyfunction!(quadratic_pq, m)?)?;
223270
m.add("MathValueError", m.py().get_type::<MathValueError>())?;
224271
Ok(())
225272
}

0 commit comments

Comments
 (0)