cba010a56235c1c55976ca1986bbf982933619c2..cb171b125c047de75cd2c7c6837e8781b8336699
2024-07-19 hbui
Graph Implementierung für die Aufgabe OK
cb171b diff | tree
2024-07-19 hbui
add RiemannSum Implementierung
a8c5c3 diff | tree
2 files modified
6 files added
200 ■■■■■ changed files
num-int/pyproject.toml 15 ●●●●● patch | view | raw | blame | history
num-int/src/numint/RiemannSum.py 65 ●●●●● patch | view | raw | blame | history
num-int/src/numint/__init__.py patch | view | raw | blame | history
num-int/test/__init__.py patch | view | raw | blame | history
num-int/test/numint/RiemannSum_test.py 29 ●●●●● patch | view | raw | blame | history
num-int/test/numint/__init__.py patch | view | raw | blame | history
stundenplan/src/pygraph/graphdemo.py 43 ●●●●● patch | view | raw | blame | history
stundenplan/tests/pygraph/graphdemo.tests.py 48 ●●●●● patch | view | raw | blame | history
num-int/pyproject.toml
New file
@@ -0,0 +1,15 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
[project]
name = "wettercdc"
version = "0.0.1"
[tool.setuptools.packages.find]
# All the following settings are optional:
where = ["src"]
[project.scripts]
numint = "numint.Riemann:main"
num-int/src/numint/RiemannSum.py
New file
@@ -0,0 +1,65 @@
def split(iteration=8):
    (a, b) = (0, 8)
    x = [a, b]
    parts = 1
    dx = (b - a)
    for n in range(1, iteration + 1):
        print(n, end=" → ")
        parts = 2 * parts
        dx = dx / 2
        for i in range(1, parts, 2):
            print(i, end=" ")
            x.insert(i, a + i * dx)
        print("\n   ", x)
    pass
def numint(f, a, b, epsilon=1e-3):
    dx = b - a
    y = [f(a), f(b)]
    s_left = dx * y[0]
    s_right = dx * y[1]
    (n, parts) = (1, 1)
    while abs(s_right - s_left) > epsilon:
        x = [a, b]  # debug only
        parts = 2 * parts
        dx = dx / 2
        n += 1
        print(n, " → ", parts)
        for i in range(1, parts, 2):
            x.insert(i, a + i * dx)  # debug only
            y.insert(i, f(a + i * dx))
            # print(i, end=" ")
        # print()
        # print("    ", x)
        # print("    ", y)
        s_left = sum(y[0:-1]) * dx
        s_right = sum(y[1:]) * dx
    return s_left, s_right
def numint_compact(f, a, b, epsilon=1e-3):
    dx = b - a
    y_l = f(a)
    y_r = f(b)
    s_left = dx * y_l
    s_right = dx * y_r
    (n, parts) = (1, 1)
    while abs(s_right - s_left) > epsilon:
        parts = 2 * parts
        dx = dx / 2
        n += 1
        print(n, " → ", parts)
        for i in range(1, parts, 2):
            # print(i, end=" ")
            y = f(a + i*dx)
            y_l += y
            y_r += y
        print()
        # print("    ", x)
        # print("    ", y)
        s_left = y_l * dx
        s_right = y_r * dx
    return s_left, s_right
num-int/src/numint/__init__.py
num-int/test/__init__.py
num-int/test/numint/RiemannSum_test.py
New file
@@ -0,0 +1,29 @@
import sys
import unittest
from numint.RiemannSum import split, numint, numint_compact
class RiemannSumTestCase(unittest.TestCase):
    def test_split_interval(self):
        split(iteration=6)
    def test_numint(self):
        def fn(x) : return x**2
        (a, b) = (0, 2)
        (l, r) = numint(fn, a, b)
        print(l, r)
        pass
    def test_numint_compact(self):
        def fn(x) : return x**2
        (a, b) = (0, 2)
        (l, r) = numint_compact(fn, a, b, epsilon=1E9 * sys.float_info.epsilon)
        print(l, r)
        pass
if __name__ == '__main__':
    unittest.main()
num-int/test/numint/__init__.py
stundenplan/src/pygraph/graphdemo.py
@@ -1,23 +1,22 @@
class Graph:
    def __init__(self):
        self.adjacent: dict[int, set] = {}
        self.vertex_attribute: dict[int, dict] = {}
        self._adjacent: dict[int, set] = {}
        self._vertex_attribute: dict[int, dict] = {}
    def add_vertex(self, vertex):
        if vertex not in self.adjacent:
        if vertex not in self._adjacent:
            if not isinstance(vertex, int):
                raise TypeError(f"Argument {vertex} is not a valid vertex")
            self.adjacent[vertex] = set()
            self._adjacent[vertex] = set()
        pass
    def add_edge(self, u, v):
        self.add_vertex(u)
        self.add_vertex(v)
        v_neighbor = self.adjacent[v]
        v_neighbor = self._adjacent[v]
        if u not in v_neighbor:
            v_neighbor.add(u)
        u_neighbor = self.adjacent[u]
        u_neighbor = self._adjacent[u]
        if v not in u_neighbor:
            u_neighbor.add(v)
        pass
@@ -34,37 +33,37 @@
        :param properties:
        :return: self
        """
        if vertex not in self.adjacent:
        if vertex not in self._adjacent:
            raise ValueError(f"Graph does not include vertex {vertex}")
        old_attributes = self.vertex_attribute[vertex]
        self.vertex_attribute[vertex] = old_attributes | properties
        old_attributes = self._vertex_attribute[vertex]
        self._vertex_attribute[vertex] = old_attributes | properties
        return self
        pass
    def get_attribute(self, vertex):
        if vertex in self.adjacent:
            return self.vertex_attribute[vertex]
        if vertex in self._adjacent:
            return self._vertex_attribute[vertex]
        else:
            raise ValueError(f"Graph does not include vertex {vertex}")
        pass
    def vertices(self):
        return self.adjacent.keys()
        return self._adjacent.keys()
    def for_each_vertices(self, action):
        for v in self.vertices():
            action(v)
    def adjacent_of(self, vertex):
        if vertex not in self.adjacent:
        if vertex not in self._adjacent:
            raise ValueError(f"Graph does not include vertex {vertex}")
        return self.adjacent.get(vertex)
        return self._adjacent.get(vertex)
    def for_each_edges(self, action):
        visited_edges: dict[int, set] = {}
        for start in self.vertices():
            for end in self.adjacent_of(start):
                (first, second) = (start, end) if (start > end) else (end, start)
                (first, second) = (start, end) if (start >= end) else (end, start)
            if first in visited_edges:
                visited_adjacent = visited_edges.get(first)
                if second not in visited_adjacent:
@@ -76,13 +75,21 @@
                adjacent.add(second)
                action(start, end)
    def get_lecture_name(self, vertex):
        pass
    def set_lecture_name(self, vertex, name):
        pass
    def __repr__(self):
        text = ""
        sorted_key = sorted(self.adjacent.keys())
        sorted_key = sorted(self._adjacent.keys())
        last_key = sorted_key.pop()
        def adjacent_fn(vertex): return "[" + ", ".join( [str(v) for v in self.adjacent[vertex]] ) + "]"
        def adjacent_fn(vertex): return "[" + ", ".join([str(v) for v in self._adjacent[vertex]]) + "]"
        for key in sorted_key:
            text += f"{key} → {adjacent_fn(key)}\n"
        text += f"{last_key} → {adjacent_fn(last_key)}"
        return text
    def to_dot(self) -> str:
        pass
stundenplan/tests/pygraph/graphdemo.tests.py
@@ -35,6 +35,54 @@
            self.assertTrue(edge in edges)
        g.for_each_edges(action)
    def test_iterate_all_vertices(self):
        g = Graph()
        g.add_edge(1, 2)
        g.add_edge(1, 3)
        g.add_edge(2, 3)
        g.add_edge(2, 4)
        vertices = []
        for v in g.vertices():
            vertices.append(v)
        expected = [1, 2, 3, 4]
        self.assertListEqual(sorted(vertices), expected)
        pass
    def test_iterate_neighbor_vertex(self):
        g = Graph()
        g.add_edge(1, 2)
        g.add_edge(1, 3)
        g.add_edge(2, 3)
        g.add_edge(2, 4)
        g.add_edge(4, 1)
        vertices = []
        for v in g.adjacent_of(1):
            vertices.append(v)
        expected = [2, 3, 4]
        self.assertListEqual(sorted(vertices), expected)
        pass
    def test_not_add_float_to_vertex(self):
        g = Graph()
        try:
            g.add_vertex(1.2)
            pass
        except TypeError as ex:
            expected_msg = "Argument 1.2 is not a valid vertex"
            self.assertEqual(str(ex), expected_msg)
            pass
        pass
    def test_not_add_string_to_vertex(self):
        g = Graph()
        try:
            g.add_vertex("add")
        except TypeError as ex:
            expected_msg = 'Argument add is not a valid vertex'
            self.assertEqual(str(ex), expected_msg)
            pass
        pass
if __name__ == '__main__':
    unittest.main()