Compare commits

..

No commits in common. "main" and "serie_2" have entirely different histories.

22 changed files with 8 additions and 8659 deletions

View File

@ -1,6 +1,2 @@
numpy==2.4.3
matplotlib==3.10.8
ipykernel==7.2.0
scipy==1.17.1
black==26.3.1
sympy==1.14.0
numpy
matplotlib

View File

@ -1,44 +0,0 @@
# Python
# AUEM
# 2026-05-06
# Begin
# --------------------------------------------------------------------------------------
# Python initialisieren:
from PIL import Image, ImageOps
import matplotlib.pyplot as pl
import numpy as np
# Parameter:
N = 10
sc_r = 100
pr = 3
fig = 1
# Import:
img = Image.open("./Bild.JPG")
img_sw = ImageOps.grayscale(img)
A = np.asarray(img_sw)
# Berechnungen;
[U, S, Vt] = np.linalg.svd(A)
U_red = U[:, :N]
S_red = S[:N]
Vt_red = Vt[:N, :]
A_red = U_red @ np.diag(S_red) @ Vt_red
G = A.size
G_red = U_red.size + S_red.size + Vt_red.size
r = G_red / G
# Anzeige der Bilder:
fh = pl.figure(fig)
pl.imshow(A, cmap="gray")
fig = fig + 1
fh = pl.figure(fig)
pl.imshow(A_red, cmap="gray")
# Ausgabe:
print("--------------------------------------------------")
print(__file__)
print("--------------------------------------------------")
print(f"Groesse original: G = {G}")
print(f"Groesse komprimiert: G_red = {G_red}")
print(f"Datenkompression: r = {r*sc_r:#.{pr}g}%")
print("--------------------------------------------------")
# --------------------------------------------------------------------------------------
# End

View File

@ -1,12 +0,0 @@
# %%
import numpy as np
R = 1.0 * np.array(
[
[2, 3, 1],
[1, -2, -1],
[4, 1, -3],
]
)
L = 1.0 * np.array([8, 3, 6])

View File

@ -1,87 +0,0 @@
# Python initialisieren
import numpy as np
# Parameter
G = np.array(
[
[2, 1, 0, 2, 6],
[4, 2, 3, 3, 16],
[-2, -1, 6, -4, 2],
[-8, -4, 9, -11, -12],
[2, 1, -3, 3, 2],
]
)
pr = 3
# Funktionen
def LGLS_rref(G, tol=-1):
# Berechnungen
n_Z = G.shape[0]
n_S = G.shape[1]
if tol < 0:
tol = np.max(G.shape) * np.linalg.norm(G, ord=np.inf) * np.finfo(np.float64).eps
# Stufenform berechnen
pz = np.int_([])
ps = np.int_([])
ez = 0
n_R = 0
m_Z = n_Z - 1
H = np.copy(1.0 * G)
for es in range(0, n_S):
# Spalten - Pivot - Suche
if ez < m_Z:
mm = ez + np.argmax(np.abs(H[range(ez, n_Z), es]))
else:
mm = m_Z
if np.abs(H[mm][es]) > tol:
if mm != ez:
tmp = np.copy(H[ez])
H[ez] = H[mm]
H[mm] = tmp
# Division durch Pivot - Wert
p = H[ez][es]
H[ez][es] = 1.0
for jj in range(es + 1, n_S):
H[ez][jj] = H[ez][jj] / p
# Elimination vorwaerts :
for ii in range(ez + 1, n_Z):
q = H[ii][es]
H[ii][es] = 0.0
for jj in range(es + 1, n_S):
H[ii][jj] = H[ii][jj] - q * H[ez][jj]
n_R = n_R + 1
pz = np.append(pz, ez)
ps = np.append(ps, es)
if ez == m_Z:
break
ez = ez + 1
else:
for ii in range(ez, n_Z):
H[ii][es] = 0.0
# Stufenform reduzieren
for kk in range(n_R - 1, -1, -1):
ez = pz[kk]
es = ps[kk]
# Elimination rueckwaerts
for ii in range(ez - 1, -1, -1):
q = H[ii][es]
H[ii][es] = 0.0
for jj in range(es + 1, n_S):
H[ii][jj] = H[ii][jj] - q * H[ez][jj]
return H
# Berechnungen
H = LGLS_rref(G)
# Ausgabe
print(f"H = \n{np.array2string(H, precision=pr)}")

View File

@ -1,21 +0,0 @@
#%%
# Python initialisieren
import numpy as np
# Parameter
G=1.*np.array([
[2,3,1,8],
[1,-2,-1,-3],
[4,1,-3,6]])
# Berechnung
m = np.max(G.shape)
n = np.linalg.norm(G)
e = np.finfo(np.float64).eps
tol = m * n * e
print(m)
print(n)
print(e)
print(tol)
# %%

View File

@ -1,28 +0,0 @@
"""
Frobenius-Norm ||A||F
A Matrix:
[4 9]
[2 7]
"""
import numpy as np
import sympy as sp
# Numpy
A = np.array([[4, 9], [2, 7]])
n = np.linalg.norm(A, ord="fro")
print(n.round(4))
# Sympy
A = sp.Matrix([[4, 9], [2, 7]])
n = A.norm(ord="fro")
print(n)
"""
Ausgabe:
12.2474
5*sqrt(6)
"""

View File

@ -1,45 +0,0 @@
"""
Simpson-Regel
Integration der funktion:
2
I = | (x + sin(x))^(2/3)) dx
0
"""
import numpy as np
import scipy.integrate as ig
# Parameter
x_0 = 0
x_E = 2
n = 10
N = 201
pr = 6
f = lambda x: np.sqrt(x + np.sin(x))
# Berechnung
for k in range(0, n):
x_data = np.linspace(x_0, x_E, N)
y_data = f(x_data)
I = ig.simpson(y=y_data, x=x_data)
print(f"I = {I:#.16g} | N = {N:g}")
N *= 2
print(f"I = {I:#.{pr}g}")
"""
Ausgabe:
I = 2.490070783046884 | N = 201
I = 2.490260152603558 | N = 402
I = 2.490326897146905 | N = 804
I = 2.490350466364332 | N = 1608
I = 2.490358796310514 | N = 3216
I = 2.490361741357393 | N = 6432
I = 2.490362782708113 | N = 12864
I = 2.490363150933651 | N = 25728
I = 2.490363281138151 | N = 51456
I = 2.490363327177379 | N = 102912
I = 2.49036
"""

View File

@ -1,45 +0,0 @@
"""
Trapez-Regel
Integration der funktion:
2
I = | (x + sin(x))^(2/3)) dx
0
"""
import numpy as np
import scipy.integrate as ig
# Parameter
x_0 = 0
x_E = 2
n = 10
N = 201
pr = 6
f = lambda x: np.sqrt(x + np.sin(x))
# Berechnung
for k in range(0, n):
x_data = np.linspace(x_0, x_E, N)
y_data = f(x_data)
I = ig.trapezoid(y=y_data, x=x_data)
print(f"I = {I:#.16g} | N = {N:g}")
N *= 2
print(f"I = {I:#.{pr}g}")
"""
Ausgabe:
I = 2.490070783046884 | N = 201
I = 2.490260152603558 | N = 402
I = 2.490326897146905 | N = 804
I = 2.490350466364332 | N = 1608
I = 2.490358796310514 | N = 3216
I = 2.490361741357393 | N = 6432
I = 2.490362782708113 | N = 12864
I = 2.490363150933651 | N = 25728
I = 2.490363281138151 | N = 51456
I = 2.490363327177379 | N = 102912
I = 2.49036
"""

View File

@ -1,37 +0,0 @@
"""
LR-Zerlegung
Matrix A:
[3, 2]
[1, 4]
"""
import numpy as np
import scipy as sc
# Parameter
A = np.array([[3, 2], [1, 4]])
pr = 3
[P, L, R] = sc.linalg.lu(A)
with np.printoptions(precision=pr):
print(f"P = \n{P}\n\nL = \n{L}\n\nR = \n{R}")
"""
Ausgabe:
P =
[[1. 0.]
[0. 1.]]
L =
[[1. 0. ]
[0.333 1. ]]
R =
[[3. 2. ]
[0. 3.333]]
"""

File diff suppressed because one or more lines are too long

View File

@ -1,45 +0,0 @@
#%%
"""
Maclaurin Entwicklungen
"""
import IPython.display as dp
import sympy as sp
# Konfig
sp.init_printing()
x = sp.symbols("x")
# Parameter
n = 2
F = sp.sqrt(1 + x)
# Berechnungen
T = sp.series(F, x, 0, n+2)
# Ausgabe
dp.display(F)
dp.display(T)
# Parameter
n = 2
F = sp.log(sp.sqrt(sp.cos(x)))
# Berechnungen
T = sp.series(F, x, 0, n+3)
# Ausgabe
dp.display(F)
dp.display(T)
# Parameter
n = 2
F = 1 / (1 + 2 * sp.sin(x))
# Berechnungen
T = sp.series(F, x, 0, n+1)
# Ausgabe
dp.display(F)
dp.display(T)
# %%

View File

@ -1,12 +0,0 @@
import numpy as np
P = [[0, 0, 1], [0, 1, 0], [1, 0, 0]]
L = [[1, 0, 0], [1, 1, 0], [1, 1, 1]]
R = [[3, 1, 1], [0, 3, 1], [0, 0, 0]]
PL = np.dot(P, L)
A = np.dot(PL, R)
print(A.trace())
Q = [[0, -1], [1, 0]]
R = [[1, 2], [0, 3]]
A = np.dot(Q, R)
print(A.trace())

View File

@ -1,34 +0,0 @@
"""
QR-Zerlegung
A Matrix:
[3 1]
[6 9]
"""
import numpy as np
import scipy as sc
# Parameter
A = np.array([[3, 1], [6, 9]])
pr = 3
# Berechnung
[Q, R] = sc.linalg.qr(A)
# Ausgabe
with np.printoptions(precision=pr):
print(f"Q = \n{Q}\n\n R = \n{R}")
"""
Ausgabe:
Q =
[[-0.447 -0.894]
[-0.894 0.447]]
R =
[[-6.708 -8.497]
[ 0. 3.13 ]]
"""

View File

@ -1,43 +0,0 @@
# %%
# Polyfit tutorial mit Plot
# Python initialisieren
import matplotlib.pyplot as plt
import numpy as np
# Parameter
x_0 = 1
x_E = 7.0
y_a = -2
y_b = 3
dg = 1
pr = 3
lw = 3
fig = 1
tc_x = np.r_[x_0 : x_E + 0.5 : 0.5]
tc_y = np.r_[y_a : y_b + 0.5 : 0.5]
# Daten
x_data = np.r_[x_0 : x_E + 1]
y_data = np.array([2.5, 2.2, 1.5, 1.0, 0.6, -0.3, -1.4])
# Berechnungen
p = np.polyfit(x_data, y_data, dg)
g_data = np.polyval(p, x_data)
# Ausgabe
print(60 * "*")
print("__file__")
print(60 * "*")
print(f"Steigung: m = {p[0]:#.{pr}g}")
print(f"y-Achsenabschnitt: q = {p[1]:#.{pr}g}")
# Plot
fh = plt.figure(fig)
plt.plot(x_data, g_data, linewidth=lw)
plt.plot(x_data, y_data, "o", linewidth=lw)
plt.xlabel(r"$x$")
plt.ylabel(r"$y$")
plt.xticks(tc_x)
plt.yticks(tc_y)
plt.grid(visible=True)
plt.axis("image")

View File

@ -1,23 +0,0 @@
import numpy as np
import sympy as sp
Q = [[0, 1, 0], [1, 0, 0], [0, 0, 1]] # Q Matrix
R = [[3, 1, 1], [0, 3, 1], [0, 0, 3]] # R Matrix
A = np.dot(Q, R) # Matrix multiplikation
print(np.linalg.det(A)) # Determinante
AT = np.transpose(A) # A transponiert
AI = np.linalg.inv(A) # A Inverse
print(AT == AI) # Orthogonal wenn True
print(A.trace()) # Spur von A
# Eigenwerte berechnen
M = sp.Matrix(A) # Sympy Matrix erstellen
spektrum = M.eigenvals() # Eigenwerte berechnen
print(list(spektrum.keys())) # Eigenwerte anzeigen
QA = np.dot(Q, A) # QR Zerlegung Gleichung umstellen
print(QA == R) # True wenn's stimmt

View File

@ -1,28 +0,0 @@
"""
Spektral-Norm ||A||2
A Matrix:
[4 9]
[2 7]
"""
import numpy as np
import sympy as sp
# Numpy
A = np.array([[4, 9], [2, 7]])
n = np.linalg.norm(A, ord=2)
print(n.round(4))
# Sympy
A = sp.Matrix([[4, 9], [2, 7]])
n = A.norm(ord=2)
print(n)
"""
Ausgabe:
12.2201
sqrt(5*sqrt(221) + 75)
"""

View File

@ -1,42 +0,0 @@
"""
Singulärwertzerlegung SVD
A Matrix:
[4 6]
[3 -8]
"""
import numpy as np
import scipy as sc
# Parameter
A = np.array([[4.0, 6.0], [3.0, -8.0]])
pr = 3
# Berechnung
[U, S, Vt] = sc.linalg.svd(A)
# Ausgabe
with np.printoptions(precision=pr):
print(f"U = \n{U}\n\nS = \n{S}\n\nV = \n{Vt.T}")
"""
Ausgabe:
U =
[[-0.6 -0.8]
[ 0.8 -0.6]]
S =
[10. 5.]
V =
[[-0. -1.]
[-1. -0.]]
"""
# S = Vektor mit den Singulärwerten (o1, o2)
# Nicht die ganze Matrix:
# [10 0]
# [0 5]

View File

@ -1,5 +0,0 @@
import sympy as sp
x = sp.symbols("x")
eq = sp.Eq(x**2 - 2 * x + 3, 0)
solution = sp.solve(eq, x)
print(solution)

View File

@ -1,41 +0,0 @@
# %%
# Python initialisieren
import matplotlib.pyplot as plt
import numpy as np
import scipy.integrate as ig
# Parameter
x_0 = 0.0
x_E = np.pi
N = 11
n = 5
pr = 6
lw = 3
fig = 1
# Funktion
f = lambda x: np.sin(x)
# Berechnungen
for k in range(0, n):
x_data = np.linspace(x_0, x_E, N)
y_data = f(x_data)
I = ig.trapezoid(y=y_data, x=x_data)
J = ig.simpson(y=y_data, x=x_data)
print(f"I = {I:#.16g}")
print(f"J = {J:#.16g}")
N *= 2
# Ausgabe
print(f"I = {I:#.{pr}g}")
print(f"J = {J:#.{pr}g}")
# Plot
fh = plt.figure(fig)
plt.plot(x_data, y_data, linewidth=lw)
plt.xlabel("x")
plt.ylabel("y")
plt.grid(visible=True)
plt.axis("image")
plt.show()
# %%

View File

@ -34,10 +34,7 @@ print(f"p) {np.log2(69.6)}")
# %%
print("Aufgabe 7")
# Parameter
a = 12.3
b = 8.14
pr = 3
ME = "cm"
a = 12.3, b = 8.14, pr = 3, ME = "cm"
# Berechnungen
c = np.sqrt(a**2+b**2)
@ -352,10 +349,10 @@ fh = plt.figure(fig)
plt.plot(x_data, f_data, linewidth = lw)
plt.xlabel(r"$x$")
plt.ylabel(r"$y$")
plt.xticks(np.linspace(x_0, x_E, N_t),("$0$", "$0.5\\,\\pi$",
"$1.0\\,\\pi$", "$1.5\\,\\pi$",
"$2.0\\,\\pi$", "$2.5\\,\\pi$",
"$3.0\\,\\pi$", "$3.5\\,\\pi$",
"$4.0\\,\\pi$"))
plt.xticks(np.linspace(x_0, x_E, N_t),("$0$", "$0.5\,\pi$",
"$1.0\,\pi$", "$1.5\,\pi$",
"$2.0\,\pi$", "$2.5\,\pi$",
"$3.0\,\pi$", "$3.5\,\pi$",
"$4.0\,\pi$"))
plt.grid(visible=True)
plt.axis("image")

View File

@ -1,114 +0,0 @@
# %%
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as ip
# %%
print(60 * "-")
print(__file__)
print("Aufgabe 2. Interpolationspolynome gemäss NEWTON-Schema berechnen")
# punkte [[x0, x1, x2, xn], [y0, y1, y2, yn]]
punkte = [
[np.array([4, 8]), np.array([1, -1])],
[np.array([-1, 1, 2]), np.array([15, 5, 9])],
[np.array([-1, 0, 1, 2]), np.array([-5, -1, -1, 1])],
]
for punkt in punkte:
# Parameter
x_data = punkt[0]
y_data = punkt[1]
x_0 = x_data[0]
x_E = x_data[-1]
N = 201
lw = 3
fig = 1
# Berechnung
n = np.size(y_data)
TAB = np.block([[y_data], [np.zeros((n - 1, n))]])
c_data = np.zeros(n)
c_data[0] = y_data[0]
for i in range(1, n):
for j in range(1, n):
TAB[i][j] = (TAB[i - 1][j] - TAB[i - 1][j - 1]) / (
x_data[j] - x_data[j - i]
)
c_data[i] = TAB[i][i]
# Funktionen:
def p(x):
d = 1
y = c_data[0]
for k in range(1, n):
d = d * (x - x_data[k - 1])
y = y + c_data[k] * d
return y
# Daten
u_data = np.linspace(x_0, x_E, N)
v_data = p(u_data)
fh = plt.figure(fig)
plt.plot(u_data, v_data, linewidth=lw)
plt.plot(x_data, y_data, "o", linewidth=lw)
plt.xlabel(r"$x$")
plt.ylabel(r"$y$")
plt.grid(visible=True)
plt.axis("image")
print(60 * "-")
# %%
print(75 * "-")
print(__file__)
print("Aufgabe 6. Polynom-Iterpolation vs. Cubic-Spline-Interpolation")
print(75 * "-")
# Parameter
x_data = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10])
y_data = np.array([1.0, 1.0, 1.0, 1.0, 1.0, 3.0, 3.0, 3.0, 3.0, 3])
x_0 = 0
x_E = 11
y_a = -1
y_b = 5
N = 501
lw = 3
sp = 0.2
fig = 1
tc_x = np.r_[x_0 : x_E + 1]
tc_y = np.r_[y_a : y_b + 0.5 : 0.5]
# Berechnung
po = ip.BarycentricInterpolator(x_data, y_data)
cs = ip.CubicSpline(x_data, y_data)
# Daten
u_data = np.linspace(x_0, x_E, N)
v_data = po(u_data)
w_data = cs(u_data)
# Plot
[fh, ax] = plt.subplot(2, 1)
ax[0].plot(u_data, v_data, linewidth=lw)
ax[0].plot(x_data, y_data, "o", linewidth=lw)
ax[0].set_xlabel(r"$x$")
ax[0].set_ylabel(r"$y$")
ax[0].set_xticks(tc_x)
ax[0].set_yticks(tc_y)
ax[0].grid(visible=True)
ax[0].axis([x_0, x_E, y_a, y_b])
ax[1].plot(u_data, w_data, linewidth=lw)
ax[1].plot(x_data, y_data, "o", linewidth=lw)
ax[1].set_xlabel(r"$x$")
ax[1].set_ylabel(r"$y$")
ax[1].set_xticks(tc_x)
ax[1].set_yticks(tc_y)
ax[1].grid(visible=True)
ax[1].axis([x_0, x_E, y_a, y_b])
plt.subplots_adjust(hspace=sp)
# %%

View File

@ -1,28 +0,0 @@
# %%
# Import
import numpy as np
import scipy as sp
# Parameter
PR = 3
matrizen = [
np.array([[3, 1], [6, 9]]),
np.array([[3, 2], [1, 4]]),
np.array([[3, 12], [1, 4]]),
np.array([[2, 3, 1], [1, 2, -1], [3, 5, 1]]),
np.array([[3, 5, 0], [5, 8, -1], [1, 2, -1]]),
np.array([[2, 4, 6], [1, 2, 3], [3, 6, 9]]),
]
# Berechnung
for matrix in matrizen:
[P, L, R] = sp.linalg.lu(matrix)
# Ausgabe
with np.printoptions(precision=PR):
print(f"\nA = \n{matrix}\n\nP = \n{P}\n\nL = \n{L}\n\nR = \n{R}\n")
print(60 * "-")
# %%