Hong-Phuc Bui
6 days ago c97d23edee3e224f1bcba0add74b61934ad69ff7
Vorlesung 02.06.2025
3 files added
1 files deleted
96 ■■■■ changed files
04-schleife/canvas-bezier/blumen.py 23 ●●●●● patch | view | raw | blame | history
polymonial/README.md 23 ●●●●● patch | view | raw | blame | history
polymonial/polynomial.py 22 ●●●●● patch | view | raw | blame | history
polymonial/polynomial_test.py 28 ●●●●● patch | view | raw | blame | history
04-schleife/canvas-bezier/blumen.py
File was deleted
polymonial/README.md
New file
@@ -0,0 +1,23 @@
Eine Implementierung von Polynome als Python-Klasse nebst Unittest
**Virtuelle Umgebung**
(Wenn PyCharm nicht automatisch eine Umgebung erstellt)
```shell
python -m venv .venv
```
(Danach das Projekt mit der virtuellen Umgebung .venv verknüpfen!)
**Dependencies**
```shell
pip install pytest pytest-cov
```
**Run Unit-Test**
```shell
pytest *_test.py
```
polymonial/polynomial.py
New file
@@ -0,0 +1,22 @@
class Polynomial:
    def __init__(self, *argv: float):
        self.__coefficients = []
        for c in argv:
            self.__coefficients.append(c)
    def __repr__(self) -> str:
        coe = []
        for c in self.__coefficients:
            coe.append(f'{c}')
        return ' '.join(coe)
    def __str__(self):
        return self.__repr__()
    def  evaluate(self, x: float) -> tuple[float, list[float]]:
        p = self.__coefficients[-1]
        c = [p]
        for ak in self.__coefficients[-2::-1]:
            p = ak + (p * x)
            c.insert(0, p)
        return c[0], c[1:]
polymonial/polynomial_test.py
New file
@@ -0,0 +1,28 @@
from polynomial import Polynomial
EPSILON = 0.001
def test_polynomial_representation():
    c = Polynomial(5, 6, 0, -7)
    s = f'{c}'
    assert s == '5 6 0 -7'
def test_evaluate():
    l = Polynomial(-12.5, 3.6)
    x = 0.0
    (y, rest) = l.evaluate(x)
    assert abs(y + 12.5) < EPSILON
def test_evaluate_2():
    l = Polynomial(11, 7, -5, -4, 2)
    x = 2
    rest_expected = [-3, -5, 0, 2]
    y_expected = 5
    (y, rest) = l.evaluate(x)
    assert abs(y - y_expected) < EPSILON
    for (i, r) in enumerate( rest_expected ):
        assert abs(r - rest[i]) < EPSILON
if __name__ == "__main__":
    test_evaluate_2()