Skript berechnet mit Simpy Summen aus. Stellt sie mit Latex Formatierung (Summenzeichen = Summe) in einem interaktiven Fenster dar.
29 lines
871 B
Python
29 lines
871 B
Python
# %%
|
|
import sympy as sp
|
|
from IPython.display import display, Math
|
|
|
|
# Define the symbolic variable
|
|
k = sp.symbols("k")
|
|
|
|
# List of operations: each entry is [expression, (variable, lower_bound, upper_bound)]
|
|
operations = [
|
|
[k, (k, 4, 10)],
|
|
[2 * k, (k, 4, 10)],
|
|
[k**3, (k, -2, 2)],
|
|
[1 / k, (k, 2, 4)],
|
|
[2**k, (k, 0, 4)],
|
|
[(-1) ** k, (k, 4, 9)],
|
|
]
|
|
|
|
for operation in operations:
|
|
# Build the symbolic Sum object (Σ notation)
|
|
# *operation unpacks the list into separate arguments: expression, bounds
|
|
summenzeichen = sp.Sum(*operation)
|
|
|
|
# Compute the actual numeric result of the summation
|
|
summe = sp.summation(*operation)
|
|
|
|
# Convert both to LaTeX strings, join them with "=",
|
|
# wrap in Math() for rendering, and display in the interactive window
|
|
display(Math(r"{} = {}".format(sp.latex(summenzeichen), sp.latex(summe))))
|