301 lines
7.6 KiB
Plaintext
301 lines
7.6 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "902e4b59-77b9-4db6-9ec2-3cff55766dd5",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-03-14T20:28:45.280232Z",
|
|
"start_time": "2026-03-14T20:28:45.240030Z"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"5j\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"\n",
|
|
"def abs(c):\n",
|
|
" return (c.real**2 + c.imag**2)**0.5\n",
|
|
"\n",
|
|
"def add(c1, c2):\n",
|
|
" return complex(c1.real + c2.real, c1.imag + c2.imag)\n",
|
|
"\n",
|
|
"def sub(c1, c2):\n",
|
|
" # (a+bi) - (c+di) = (a-c) + (b-d)i\n",
|
|
" return complex(c1.real - c2.real, c1.imag - c2.imag)\n",
|
|
"\n",
|
|
"def mul(c1, c2):\n",
|
|
" # (a+bi)(c+di) = (ac-bd) + (ad+bc)i\n",
|
|
" return complex(c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag + c1.imag * c2.real)\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"print(mul(1+2j, 2+1j))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "3fb82de2-875a-4dac-935a-e64bad48c895",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-03-14T20:28:45.320152Z",
|
|
"start_time": "2026-03-14T20:28:45.281685Z"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"(5, 5)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"def add(v1, v2):\n",
|
|
" return (v1[0] + v2[0], v1[1] + v2[1])\n",
|
|
"\n",
|
|
"def sub(v1, v2):\n",
|
|
" return (v1[0] - v2[0], v1[1] - v2[1])\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"print(add((2, 1), (3, 4)))\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "3a562f0e-162b-4760-ac6b-22587bf0bd60",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-03-14T20:28:45.659050Z",
|
|
"start_time": "2026-03-14T20:28:45.349987Z"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Intervall: Interval.Ropen(2, 5)\n",
|
|
"Mengenangabe: (2 <= x) & (x < 5)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from sympy import Interval, Symbol\n",
|
|
"\n",
|
|
"# 1. Variable definieren\n",
|
|
"x = Symbol('x')\n",
|
|
"\n",
|
|
"# 2. Intervall erstellen (z.B. [2, 5) - links abgeschlossen, rechts offen)\n",
|
|
"# left_open=False (Standard) bedeutet inklusive 2, right_open=True bedeutet exklusive 5\n",
|
|
"mein_intervall = Interval(2, 5, left_open=False, right_open=True)\n",
|
|
"\n",
|
|
"# 3. In Mengenangabe / Ungleichung umwandeln\n",
|
|
"mengen_angabe = mein_intervall.as_relational(x)\n",
|
|
"\n",
|
|
"print(f\"Intervall: {mein_intervall}\")\n",
|
|
"print(f\"Mengenangabe: {mengen_angabe}\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "5f024814-9f83-4d5f-a81f-4283e47e22b9",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-03-14T20:28:45.760535Z",
|
|
"start_time": "2026-03-14T20:28:45.660677Z"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Interval.Ropen(2, 5)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from sympy import Symbol, And\n",
|
|
"\n",
|
|
"x = Symbol('x')\n",
|
|
"\n",
|
|
"# Eine Mengenangabe als logische Verknüpfung (2 <= x < 5)\n",
|
|
"mengen_angabe = And(x >= 2, x < 5)\n",
|
|
"\n",
|
|
"# Umwandlung in ein Intervall-Objekt\n",
|
|
"intervall = mengen_angabe.as_set()\n",
|
|
"\n",
|
|
"print(intervall) \n",
|
|
"# Ausgabe: [2, 5)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "b58b7c16-2c79-46a4-bde0-1d8fcc16cfa9",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-03-14T20:28:45.791872Z",
|
|
"start_time": "2026-03-14T20:28:45.765958Z"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Interval(-2, 2)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from sympy import Symbol, solve_univariate_inequality\n",
|
|
"\n",
|
|
"x = Symbol('x')\n",
|
|
"\n",
|
|
"# Eine Ungleichung definieren\n",
|
|
"ungleichung = x**2 <= 4\n",
|
|
"\n",
|
|
"# Lösen und als Intervall ausgeben lassen (relational=False)\n",
|
|
"intervall = solve_univariate_inequality(ungleichung, x, relational=False)\n",
|
|
"\n",
|
|
"print(intervall)\n",
|
|
"# Ausgabe: [-2, 2]\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "dd0522d3-0612-44f1-9504-1ba48542e37e",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-03-14T20:28:45.850425Z",
|
|
"start_time": "2026-03-14T20:28:45.792950Z"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Interval.open(-8, 8)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from sympy import Symbol, solve_univariate_inequality, Abs\n",
|
|
"\n",
|
|
"# 1. Symbol als reell definieren\n",
|
|
"x = Symbol('x', real=True)\n",
|
|
"\n",
|
|
"# 2. Die SymPy-eigene Funktion Abs() verwenden\n",
|
|
"# Das verhindert, dass Python-Interne Funktionen dazwischenfunken\n",
|
|
"ungleichung = Abs(x) < 8\n",
|
|
"\n",
|
|
"# 3. Lösen\n",
|
|
"intervall = solve_univariate_inequality(ungleichung, x, relational=False)\n",
|
|
"\n",
|
|
"print(intervall)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "99ed5f78-8032-40eb-99ce-988de647549b",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-03-14T20:30:39.429411Z",
|
|
"start_time": "2026-03-14T20:30:39.352620Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import matplotlib.pyplot as plt\n",
|
|
"from sympy import Interval\n",
|
|
"\n",
|
|
"def plot_interval(intervall, x_range=(0, 10)):\n",
|
|
" fig, ax = plt.subplots(figsize=(8, 2))\n",
|
|
"\n",
|
|
" # Zahlenstrahl-Optik\n",
|
|
" ax.set_xlim(x_range)\n",
|
|
" ax.set_ylim(-1, 1)\n",
|
|
" ax.set_yticks([])\n",
|
|
" ax.spines['top'].set_visible(False)\n",
|
|
" ax.spines['right'].set_visible(False)\n",
|
|
" ax.spines['left'].set_visible(False)\n",
|
|
" ax.spines['bottom'].set_position('center')\n",
|
|
"\n",
|
|
" # Start- und Endpunkte extrahieren\n",
|
|
" start, end = float(intervall.start), float(intervall.end)\n",
|
|
"\n",
|
|
" # Die Linie für das Intervall zeichnen\n",
|
|
" ax.plot([start, end], [0, 0], color='blue', lw=4)\n",
|
|
"\n",
|
|
" # Punkte zeichnen: gefüllt = inklusive, weiß/leer = exklusive\n",
|
|
" ax.plot(start, 0, 'o', color='blue', mfc='blue' if not intervall.left_open else 'white', markersize=10)\n",
|
|
" ax.plot(end, 0, 'o', color='blue', mfc='blue' if not intervall.right_open else 'white', markersize=10)\n",
|
|
"\n",
|
|
" plt.title(f\"Intervall: {intervall}\")\n",
|
|
" plt.show()\n",
|
|
"\n",
|
|
"# Beispiel: [2, 5)\n",
|
|
"mein_intervall = Interval(2, 5, left_open=False, right_open=True)\n",
|
|
"plot_interval(mein_intervall, x_range=(0, 7))\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "cdb0080c9d06840a",
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2026-03-14T20:30:39.750872Z",
|
|
"start_time": "2026-03-14T20:30:39.440529Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from sympy import symbols, plot_implicit, And\n",
|
|
"\n",
|
|
"x = symbols('x')\n",
|
|
"# Zeichnet den Bereich auf der x-Achse\n",
|
|
"plot_implicit(And(x >= 2, x < 5), (x, 0, 7))\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.13.11"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|