master > master: src - methoden funktionieren
This commit is contained in:
parent
7b237246da
commit
96684d30a8
@ -66,18 +66,24 @@ 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', left=False, right=False, top=False, bottom=False, labelbottom=False, labelleft=False);
|
axs.tick_params(axis='both', which='both', labelbottom=False, labelleft=False);
|
||||||
mplot.title('');
|
mplot.title(f'Darstellung von ${self.name}$', fontdict={
|
||||||
|
'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);
|
||||||
@ -127,7 +133,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 'x';
|
marker = 'o' if (y in comp_range_next) else '.';
|
||||||
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:
|
||||||
@ -138,16 +144,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='g', linewidth=2);
|
axs.plot([p[0], q[0]], [p[1], q[1]], label='', color='black', linewidth=1);
|
||||||
else:
|
else:
|
||||||
axs.plot([p[0], q[0]], [p[1], q[1]], label='', color='g', linestyle='--', linewidth=1);
|
axs.plot([p[0], q[0]], [p[1], q[1]], label='', color='red', 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',
|
||||||
@ -176,8 +182,6 @@ 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,16 +34,19 @@ T2 = TypeVar('T2');
|
|||||||
# METHODS
|
# METHODS
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
def randomset_integers(low: int, high: int) -> list[int]:
|
def randomset_integers(N: int = -1, low: int = 1, high: int = 1) -> list[int]:
|
||||||
N = random.randint(low, high);
|
if N == -1:
|
||||||
|
N = random.randint(low, high);
|
||||||
return list(range(1, N+1));
|
return list(range(1, N+1));
|
||||||
|
|
||||||
def randomset_alphabet(low: int, high: int) -> list[int]:
|
def randomset_alphabet(N: int = -1, low: int = 1, high: int = 1) -> list[int]:
|
||||||
N = random.randint(low, high);
|
if N == -1:
|
||||||
|
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(low: int, high: int) -> list[int]:
|
def randomset_greek(N: int = -1, low: int = 1, high: int = 1) -> list[int]:
|
||||||
N = random.randint(low, high);
|
if N == -1:
|
||||||
|
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]);
|
||||||
|
|
||||||
def random_function(
|
def random_function(
|
||||||
@ -52,11 +55,39 @@ 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]]:
|
||||||
# TODO: add feature to force injectivity/surjectivity, if possible.
|
m = len(X);
|
||||||
# m = len(X);
|
n = len(Y);
|
||||||
# n = len(Y);
|
if m == 0:
|
||||||
# if m > n:
|
return [];
|
||||||
# injective = False;
|
if n == 0:
|
||||||
# if m < n:
|
raise Exception(f'Impossible to create a function with {m} elements in the domain and {n} in the codomain.');
|
||||||
# surjective = False;
|
match (injective, surjective):
|
||||||
return [ (x, random.choice(Y)) for x in X ];
|
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 ];
|
||||||
|
Loading…
Reference in New Issue
Block a user