Merge branch 'main' of https://gitea.fhgr.ch/zimmersandro/cds-404
This commit is contained in:
commit
5010997af7
44
src/FHGR_CDS_Nume_E_PYNU_SVD_Bildkompression.py
Normal file
44
src/FHGR_CDS_Nume_E_PYNU_SVD_Bildkompression.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
# 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
|
||||||
28
src/klausurvorbereitung/frobenius_norm.py
Normal file
28
src/klausurvorbereitung/frobenius_norm.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
"""
|
||||||
|
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)
|
||||||
|
"""
|
||||||
45
src/klausurvorbereitung/integration_simpson.py
Normal file
45
src/klausurvorbereitung/integration_simpson.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
"""
|
||||||
|
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
|
||||||
|
"""
|
||||||
45
src/klausurvorbereitung/integration_trapez.py
Normal file
45
src/klausurvorbereitung/integration_trapez.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
"""
|
||||||
|
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
|
||||||
|
"""
|
||||||
37
src/klausurvorbereitung/lr_zerlegung.py
Normal file
37
src/klausurvorbereitung/lr_zerlegung.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
"""
|
||||||
|
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]]
|
||||||
|
"""
|
||||||
7910
src/klausurvorbereitung/maclaurin_entwicklungen.html
Normal file
7910
src/klausurvorbereitung/maclaurin_entwicklungen.html
Normal file
File diff suppressed because one or more lines are too long
45
src/klausurvorbereitung/maclaurin_entwicklungen.py
Normal file
45
src/klausurvorbereitung/maclaurin_entwicklungen.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#%%
|
||||||
|
"""
|
||||||
|
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)
|
||||||
|
# %%
|
||||||
34
src/klausurvorbereitung/qr_zerlegung.py
Normal file
34
src/klausurvorbereitung/qr_zerlegung.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
"""
|
||||||
|
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 ]]
|
||||||
|
"""
|
||||||
43
src/klausurvorbereitung/regression_polyfit.py
Normal file
43
src/klausurvorbereitung/regression_polyfit.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
# %%
|
||||||
|
# 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")
|
||||||
28
src/klausurvorbereitung/spektral_norm.py
Normal file
28
src/klausurvorbereitung/spektral_norm.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
"""
|
||||||
|
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)
|
||||||
|
"""
|
||||||
42
src/klausurvorbereitung/svd_zerlegung.py
Normal file
42
src/klausurvorbereitung/svd_zerlegung.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
"""
|
||||||
|
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]
|
||||||
Loading…
x
Reference in New Issue
Block a user