From 1db7a9c21c34895840adf1e75e0412a0a9eb87e0 Mon Sep 17 00:00:00 2001 From: Julian T Date: Wed, 9 Sep 2020 17:08:37 +0200 Subject: Added nonworking solution to sig mm2 --- sem5/sig/mm2/opg2.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 sem5/sig/mm2/opg2.py diff --git a/sem5/sig/mm2/opg2.py b/sem5/sig/mm2/opg2.py new file mode 100644 index 0000000..c2b0815 --- /dev/null +++ b/sem5/sig/mm2/opg2.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +# 2020-09-09/sig/mm2/opg/2 #bb68 +# Første del kan findes i #91a6/P. + +import matplotlib.pyplot as plt +import numpy as np + +class Series: + def __init__(self): + self.conditions = [] + + def add_condition(self, *args): + self.conditions.extend(args) + + def evaluate(self, n): + res = 0 + for cond in self.conditions: + res += cond(n) + return res + + def gen(self, r): + res = np.zeros_like(r, dtype=float) + # Super inefficient + for i, v in enumerate(r): + res[i] = self.evaluate(v) + return res + + # Uhhh terrible + def fold(self, s, r): + res = np.zeros_like(r, dtype=float) + + # Evaluate own for the whole range r + start = r[0] + stop = start + len(r) + + # Terribly inefficient + for i, v in enumerate(r): + for k in range(start, stop): + res[i] += self.evaluate(v) * s.evaluate(v - k) + print(v, k, self.evaluate(v), s.evaluate(v - k)) + return res + +print("Starting") + +s1 = Series() +s1.add_condition(lambda n: 1 if n >= 0 and n <= 4 else 0) +s2 = Series() +s2.add_condition(lambda n: 1 if n >= 0 and n <= 9 else 0) + +n = np.arange(-10, 15) +plt.plot(n, s1.fold(s2, n), "o") +# print(n, s1.fold(s2, n)) +plt.show() + + -- cgit v1.2.3