Compare commits
No commits in common. "55562889eb90fbb1589b291a39fc0b26ab008252" and "7b237246da925a92c600cdc25d1e446f3db4feec" have entirely different histories.
55562889eb
...
7b237246da
@ -6,14 +6,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"# Woche 2 - 17.-23. Oktober 2022 #\n",
|
"# Woche 2 - 17.-23. Oktober 2022 #\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Herzlich willkommen zur 2. Woche!\n",
|
"Herzlich willkommen zur 2. Woche!"
|
||||||
"\n",
|
|
||||||
"## Agenda ##\n",
|
|
||||||
"\n",
|
|
||||||
"- [x] Struktur\n",
|
|
||||||
"- [x] Aufwärme\n",
|
|
||||||
"- [x] Seminaraufgaben 2.x\n",
|
|
||||||
"- [ ] Besprechung von HA 1.x\n"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -53,11 +46,11 @@
|
|||||||
"X = ['a', 'b', 'c', 'd', 'e'];\n",
|
"X = ['a', 'b', 'c', 'd', 'e'];\n",
|
||||||
"Y = [2, 3, 5, 7, 11, 13, 17];\n",
|
"Y = [2, 3, 5, 7, 11, 13, 17];\n",
|
||||||
"Z = ['α', 'β', 'γ', 'δ'];\n",
|
"Z = ['α', 'β', 'γ', 'δ'];\n",
|
||||||
"f = [('a', 5), ('b', 5), ('c', 17), ('d', 2), ('e', 5)]\n",
|
"f = [('a', 5), ('b', 5), ('c', 17), ('d', 2)]\n",
|
||||||
"g = [(2, 'γ'), (3, 'α'), (5, 'β'), (7, 'δ'), (11, 'β'), (13, 'γ'), (17, 'β')];\n",
|
"g = [(2, 'γ'), (3, 'α'), (5, 'β'), (7, 'δ'), (11, 'β'), (13, 'γ'), (17, 'β')];\n",
|
||||||
"comp = Functions(\n",
|
"comp = Functions(\n",
|
||||||
" Function(name=('f', 'X', 'Y'), domain=X, codomain=Y, fct=f),\n",
|
" Function(name=('$f$', 'X', 'Y'), domain=X, codomain=Y, fct=f),\n",
|
||||||
" Function(name=('g', 'Y', 'Z'), domain=Y, codomain=Z, fct=g),\n",
|
" Function(name=('$g$', 'Y', 'Z'), domain=Y, codomain=Z, fct=g),\n",
|
||||||
");\n",
|
");\n",
|
||||||
"comp.draw(show_labels=True);"
|
"comp.draw(show_labels=True);"
|
||||||
]
|
]
|
||||||
@ -70,14 +63,14 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"A = np.random.randint(-100, 100, size=(4, 7));\n",
|
"A = np.random.randint(-100, 100, size=(4, 7));\n",
|
||||||
"\n",
|
"\n",
|
||||||
"X = randomset_alphabet(N=3);\n",
|
"X = randomset_alphabet(low=2, high=5);\n",
|
||||||
"Y = randomset_integers(N=5);\n",
|
"Y = randomset_integers(low=2, high=5);\n",
|
||||||
"Z = randomset_greek(N=7);\n",
|
"Z = randomset_greek(low=2, high=5);\n",
|
||||||
"f = random_function(X, Y, injective=True);\n",
|
"f = random_function(X, Y);\n",
|
||||||
"g = random_function(Y, Z, injective=True);\n",
|
"g = random_function(Y, Z);\n",
|
||||||
"comp = Functions(\n",
|
"comp = Functions(\n",
|
||||||
" Function(name=('f', 'X', 'Y'), domain=X, codomain=Y, fct=f),\n",
|
" Function(name=('$f$', 'X', 'Y'), domain=X, codomain=Y, fct=f),\n",
|
||||||
" Function(name=('g', 'Y', 'Z'), domain=Y, codomain=Z, fct=g),\n",
|
" Function(name=('$g$', 'Y', 'Z'), domain=Y, codomain=Z, fct=g),\n",
|
||||||
");\n",
|
");\n",
|
||||||
"comp.draw(show_labels=False);"
|
"comp.draw(show_labels=False);"
|
||||||
]
|
]
|
||||||
|
@ -66,24 +66,18 @@ class Function(Generic[T1,T2]):
|
|||||||
return Functions(self).draw();
|
return Functions(self).draw();
|
||||||
|
|
||||||
class Functions:
|
class Functions:
|
||||||
name: str;
|
|
||||||
fcts: list[Function];
|
fcts: list[Function];
|
||||||
|
|
||||||
def __init__(self, *f: Function):
|
def __init__(self, *f: Function):
|
||||||
self.fcts = list(f);
|
self.fcts = list(f);
|
||||||
self.name = r' \circ '.join([ fct.name[0] for fct in self.fcts ][::-1]);
|
|
||||||
|
|
||||||
def draw(self, show_labels: bool = True) -> Figure:
|
def draw(self, show_labels: bool = True) -> Figure:
|
||||||
N = len(self.fcts);
|
N = len(self.fcts);
|
||||||
obj = mplot.subplots(1, 1, constrained_layout=True);
|
obj = mplot.subplots(1, 1, constrained_layout=True);
|
||||||
fig: Figure = obj[0];
|
fig: Figure = obj[0];
|
||||||
axs: Axes = obj[1];
|
axs: Axes = obj[1];
|
||||||
axs.tick_params(axis='both', which='both', labelbottom=False, labelleft=False);
|
axs.tick_params(axis='both', which='both', left=False, right=False, top=False, bottom=False, labelbottom=False, labelleft=False);
|
||||||
mplot.title(f'Darstellung von ${self.name}$', fontdict={
|
mplot.title('');
|
||||||
'fontsize': 16,
|
|
||||||
'horizontalalignment': 'center',
|
|
||||||
'color': 'forestgreen',
|
|
||||||
});
|
|
||||||
mplot.xlabel('');
|
mplot.xlabel('');
|
||||||
mplot.ylabel('');
|
mplot.ylabel('');
|
||||||
mplot.margins(x=MARGIN, y=MARGIN);
|
mplot.margins(x=MARGIN, y=MARGIN);
|
||||||
@ -133,7 +127,7 @@ class Functions:
|
|||||||
|
|
||||||
for j, p in enumerate(p_codomain):
|
for j, p in enumerate(p_codomain):
|
||||||
y = f.codomain[j];
|
y = f.codomain[j];
|
||||||
marker = 'o' if (y in comp_range_next) else '.';
|
marker = 'o' if (y in comp_range_next) else 'x';
|
||||||
axs.scatter([p[0]], [p[1]], label='', color='black', marker=marker);
|
axs.scatter([p[0]], [p[1]], label='', color='black', marker=marker);
|
||||||
y_name = f.codomain[j];
|
y_name = f.codomain[j];
|
||||||
if show_labels:
|
if show_labels:
|
||||||
@ -144,16 +138,16 @@ class Functions:
|
|||||||
q = p_codomain[j];
|
q = p_codomain[j];
|
||||||
x = f.domain[i];
|
x = f.domain[i];
|
||||||
if k == 0 or (x in comp_range):
|
if k == 0 or (x in comp_range):
|
||||||
axs.plot([p[0], q[0]], [p[1], q[1]], label='', color='black', linewidth=1);
|
axs.plot([p[0], q[0]], [p[1], q[1]], label='', color='g', linewidth=2);
|
||||||
else:
|
else:
|
||||||
axs.plot([p[0], q[0]], [p[1], q[1]], label='', color='red', linestyle='--', linewidth=1);
|
axs.plot([p[0], q[0]], [p[1], q[1]], label='', color='g', linestyle='--', linewidth=1);
|
||||||
|
|
||||||
anchor = anchors[k];
|
anchor = anchors[k];
|
||||||
fct_name, X_name, Y_name = f.name;
|
fct_name, X_name, Y_name = f.name;
|
||||||
axs.annotate(text=f'${fct_name}$', xy=anchor[0], ha='center', size=FONTSIZE_FCT);
|
axs.annotate(text=f'{fct_name}', xy=anchor[0], ha='center', size=FONTSIZE_FCT);
|
||||||
if k == 0:
|
if k == 0:
|
||||||
axs.annotate(text=f'${X_name}$', xy=anchor[1], ha='center', size=FONTSIZE_FCT);
|
axs.annotate(text=f'{X_name}', xy=anchor[1], ha='center', size=FONTSIZE_FCT);
|
||||||
axs.annotate(text=f'${Y_name}$', xy=anchor[2], ha='center', size=FONTSIZE_FCT);
|
axs.annotate(text=f'{Y_name}', xy=anchor[2], ha='center', size=FONTSIZE_FCT);
|
||||||
axs.add_patch(FancyArrowPatch(
|
axs.add_patch(FancyArrowPatch(
|
||||||
anchor[3], anchor[4],
|
anchor[3], anchor[4],
|
||||||
connectionstyle = 'arc3,rad=0.5',
|
connectionstyle = 'arc3,rad=0.5',
|
||||||
@ -182,6 +176,8 @@ def oval(
|
|||||||
P[-1, :] = P[0, :];
|
P[-1, :] = P[0, :];
|
||||||
return P;
|
return P;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def random_points(
|
def random_points(
|
||||||
nr_points: int,
|
nr_points: int,
|
||||||
scale: tuple[float, float] = (1., 1.),
|
scale: tuple[float, float] = (1., 1.),
|
||||||
|
@ -34,18 +34,15 @@ T2 = TypeVar('T2');
|
|||||||
# METHODS
|
# METHODS
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
def randomset_integers(N: int = -1, low: int = 1, high: int = 1) -> list[int]:
|
def randomset_integers(low: int, high: int) -> list[int]:
|
||||||
if N == -1:
|
|
||||||
N = random.randint(low, high);
|
N = random.randint(low, high);
|
||||||
return list(range(1, N+1));
|
return list(range(1, N+1));
|
||||||
|
|
||||||
def randomset_alphabet(N: int = -1, low: int = 1, high: int = 1) -> list[int]:
|
def randomset_alphabet(low: int, high: int) -> list[int]:
|
||||||
if N == -1:
|
|
||||||
N = random.randint(low, high);
|
N = random.randint(low, high);
|
||||||
return list([a for k, a in enumerate(ALPHA) if k < N]);
|
return list([a for k, a in enumerate(ALPHA) if k < N]);
|
||||||
|
|
||||||
def randomset_greek(N: int = -1, low: int = 1, high: int = 1) -> list[int]:
|
def randomset_greek(low: int, high: int) -> list[int]:
|
||||||
if N == -1:
|
|
||||||
N = random.randint(low, high);
|
N = random.randint(low, high);
|
||||||
return list([a for k, a in enumerate(GREEK) if k < N]);
|
return list([a for k, a in enumerate(GREEK) if k < N]);
|
||||||
|
|
||||||
@ -55,39 +52,11 @@ def random_function(
|
|||||||
injective: Optional[bool] = None,
|
injective: Optional[bool] = None,
|
||||||
surjective: Optional[bool] = None,
|
surjective: Optional[bool] = None,
|
||||||
) -> list[tuple[T1, T2]]:
|
) -> list[tuple[T1, T2]]:
|
||||||
m = len(X);
|
# TODO: add feature to force injectivity/surjectivity, if possible.
|
||||||
n = len(Y);
|
# m = len(X);
|
||||||
if m == 0:
|
# n = len(Y);
|
||||||
return [];
|
# if m > n:
|
||||||
if n == 0:
|
# injective = False;
|
||||||
raise Exception(f'Impossible to create a function with {m} elements in the domain and {n} in the codomain.');
|
# if m < n:
|
||||||
match (injective, surjective):
|
# surjective = False;
|
||||||
case (True, _):
|
|
||||||
assert m <= n, f'Impossible to create an injective function with {m} elements in the domain and {n} in the codomain.';
|
|
||||||
Y = random.sample(Y, m);
|
|
||||||
return [(x, y) for x, y in zip(X, Y)];
|
|
||||||
case (_, True):
|
|
||||||
assert m >= n, f'Impossible to create an surjective function with {m} elements in the domain and {n} in the codomain.';
|
|
||||||
indexes = random.sample(list(range(m)), n);
|
|
||||||
g = [ (indexes[j], Y[j]) for j in range(n) ] \
|
|
||||||
+ [
|
|
||||||
(i, random.choice(Y))
|
|
||||||
for i in range(m)
|
|
||||||
if not i in indexes
|
|
||||||
];
|
|
||||||
g = sorted(g, key=lambda o: o[0]);
|
|
||||||
return [ (X[i], y) for (i, y) in g ];
|
|
||||||
case (False, _):
|
|
||||||
assert m > 1, f'Impossible to create a non-injective function with {m} elements in the domain.';
|
|
||||||
indexes = random.sample(list(range(m)), m);
|
|
||||||
g = random_function(indexes, Y);
|
|
||||||
[(i0, y0), (i1, y1)] = g[:2];
|
|
||||||
g[0] = (i0, y1);
|
|
||||||
g = sorted(g, key=lambda o: o[0]);
|
|
||||||
return [ (X[i], y) for (i, y) in g ];
|
|
||||||
case (_, False):
|
|
||||||
assert n > 1, f'Impossible to create a non-surjective function with {n} elements in the codomain.';
|
|
||||||
Y = random.sample(Y, n-1);
|
|
||||||
return random_function(X, Y);
|
|
||||||
case _:
|
|
||||||
return [ (x, random.choice(Y)) for x in X ];
|
return [ (x, random.choice(Y)) for x in X ];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user