As a mathematician, a major part of my work is devoted to gaining new insights in my research area: numerical analysis. Beyond mathematical theorems and – most importantly – their proofs, many results in this field are naturally complemented and supported by numerical experiments.
Over the past years, I have worked on a variety of topics and, often in collaboration with colleagues, developed numerous code snippets that I would like to share with other researchers and students. While some of this material will be made available on GitHub, other parts will be presented directly on this website.
The motivation for hosting content here is to offer a more illustrative and accessible presentation. In particular, I hope this will be helpful for students who are still gaining experience with programming and may find platforms like GitHub overwhelming at first. Instead of separating theory and implementation, I aim to present code alongside detailed mathematical explanations. In many cases, the code can even be executed directly on the website, without requiring any additional technical setup.
The short example below illustrates this idea.
Basic Python Example
This blog post contains executable Python code that runs directly in your browser:
# This code runs in your browser!
import datetime
now = datetime.datetime.now()
print(f"Current time: {now.strftime('%Y-%m-%d %H:%M:%S')}")
print("Hello from Python in the browser!")
# Try some calculations
for i in range(5):
print(f"{i} squared = {i**2}")
import sys
from io import StringIO
from pyscript import document
def _run_3be0d625():
"""Execute the code and display output"""
output_div = document.getElementById("output-3be0d625")
output_div.innerHTML = ""
output_div.classList.remove("hidden")
_stdout = StringIO()
_old_stdout = sys.stdout
sys.stdout = _stdout
_mpl_used = False
try:
# This code runs in your browser!
import datetime
now = datetime.datetime.now()
print(f"Current time: {now.strftime('%Y-%m-%d %H:%M:%S')}")
print("Hello from Python in the browser!")
# Try some calculations
for i in range(5):
print(f"{i} squared = {i**2}")
# Check if matplotlib was used
if 'matplotlib' in sys.modules:
import matplotlib.pyplot as plt
if plt.get_fignums():
_mpl_used = True
fig = plt.gcf()
from pyscript import display
display(fig, target="output-3be0d625")
plt.close('all')
except Exception as e:
_stdout.write(f"Error: {e}\n")
import traceback
_stdout.write(traceback.format_exc())
finally:
sys.stdout = _old_stdout
output_text = _stdout.getvalue()
# Display text output if there is any and matplotlib wasn't used
if output_text and not _mpl_used:
pre = document.createElement("pre")
pre.style.margin = "0"
pre.style.whiteSpace = "pre-wrap"
pre.style.fontFamily = "Monaco, Menlo, 'Courier New', monospace"
pre.style.fontSize = "0.875rem"
pre.style.lineHeight = "1.5"
pre.textContent = output_text
output_div.appendChild(pre)
# Expose function to JavaScript global scope
import js
js.window.runCode3be0d625 = _run_3be0d625
Data Visualization
Now let's create an interactive plot:
import matplotlib.pyplot as plt
import numpy as np
# Generate data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
# Create plot
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x, y1, label='sin(x)', linewidth=2)
ax.plot(x, y2, label='cos(x)', linewidth=2)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Trigonometric Functions')
ax.legend()
ax.grid(True, alpha=0.3)
# Display the plot (PyScript automatically shows it)
plt.show()
import sys
from io import StringIO
from pyscript import document
def _run_9af6c75d():
"""Execute the code and display output"""
output_div = document.getElementById("output-9af6c75d")
output_div.innerHTML = ""
output_div.classList.remove("hidden")
_stdout = StringIO()
_old_stdout = sys.stdout
sys.stdout = _stdout
_mpl_used = False
try:
import matplotlib.pyplot as plt
import numpy as np
# Generate data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
# Create plot
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x, y1, label='sin(x)', linewidth=2)
ax.plot(x, y2, label='cos(x)', linewidth=2)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Trigonometric Functions')
ax.legend()
ax.grid(True, alpha=0.3)
# Display the plot (PyScript automatically shows it)
plt.show()
# Check if matplotlib was used
if 'matplotlib' in sys.modules:
import matplotlib.pyplot as plt
if plt.get_fignums():
_mpl_used = True
fig = plt.gcf()
from pyscript import display
display(fig, target="output-9af6c75d")
plt.close('all')
except Exception as e:
_stdout.write(f"Error: {e}\n")
import traceback
_stdout.write(traceback.format_exc())
finally:
sys.stdout = _old_stdout
output_text = _stdout.getvalue()
# Display text output if there is any and matplotlib wasn't used
if output_text and not _mpl_used:
pre = document.createElement("pre")
pre.style.margin = "0"
pre.style.whiteSpace = "pre-wrap"
pre.style.fontFamily = "Monaco, Menlo, 'Courier New', monospace"
pre.style.fontSize = "0.875rem"
pre.style.lineHeight = "1.5"
pre.textContent = output_text
output_div.appendChild(pre)
# Expose function to JavaScript global scope
import js
js.window.runCode9af6c75d = _run_9af6c75d