mirror of
https://github.com/janishutz/eth-summaries.git
synced 2025-11-25 10:34:23 +00:00
[NumCS] ++ Code Examples
This commit is contained in:
Binary file not shown.
@@ -104,7 +104,7 @@ def fast_kron_vector_product(A: np.ndarray, B: np.ndarray, x: np.ndarray):
|
|||||||
# This will actually crash if x.shape[0] is not divisible by A.shape[0]
|
# This will actually crash if x.shape[0] is not divisible by A.shape[0]
|
||||||
bx = B * x.reshape(A.shape[0], round(x.shape[0] / A.shape[0]))
|
bx = B * x.reshape(A.shape[0], round(x.shape[0] / A.shape[0]))
|
||||||
# Then multiply a with the resulting vector
|
# Then multiply a with the resulting vector
|
||||||
y = A * bx
|
y = A @ bx
|
||||||
\end{code}
|
\end{code}
|
||||||
|
|
||||||
Um die oben erwähnte Laufzeit zu erreichen muss erst ein neuer Vektor berechnet werden, oben im Code \verb|bx| genannt, der eine Multiplikation von \verb|Bx_i| als Einträge hat.
|
Um die oben erwähnte Laufzeit zu erreichen muss erst ein neuer Vektor berechnet werden, oben im Code \verb|bx| genannt, der eine Multiplikation von \verb|Bx_i| als Einträge hat.
|
||||||
|
|||||||
@@ -58,7 +58,6 @@ oder das ganze mithilfe von Numpy:
|
|||||||
for k in range(n):
|
for k in range(n):
|
||||||
# Vectorized differences between $x_k$ and all $x$s
|
# Vectorized differences between $x_k$ and all $x$s
|
||||||
differences = x[k] - x
|
differences = x[k] - x
|
||||||
|
|
||||||
# Remove the $k$-th element (and handle edge cases for $k = 0$ and $k = n - 1$)
|
# Remove the $k$-th element (and handle edge cases for $k = 0$ and $k = n - 1$)
|
||||||
if k < n - 1 and k > 0:
|
if k < n - 1 and k > 0:
|
||||||
diff_processed = np.concatenate((differences[:k], differences[(k + 1) :]))
|
diff_processed = np.concatenate((differences[:k], differences[(k + 1) :]))
|
||||||
@@ -70,6 +69,20 @@ oder das ganze mithilfe von Numpy:
|
|||||||
return barweight
|
return barweight
|
||||||
\end{code}
|
\end{code}
|
||||||
|
|
||||||
|
Gleiche funktion, etwas kürzer:
|
||||||
|
|
||||||
|
\begin{code}{python}
|
||||||
|
def barycentric_weights(x: np.ndarray) -> np.ndarray:
|
||||||
|
n = len(x)
|
||||||
|
w = np.ones(n) # = barweight
|
||||||
|
# Compute the (non-inverted) product, avoiding case (x[i] - x[i]) = 0
|
||||||
|
for i in range(0, n, 1):
|
||||||
|
if (i-1 > 0): w[0:(i-1)] *= (x[0:(i-1)] - x[i])
|
||||||
|
if (i+1 < n): w[i+1:n] *= (x[i+1:n] - x[i])
|
||||||
|
# Invert all at once
|
||||||
|
return 1/w
|
||||||
|
\end{code}
|
||||||
|
|
||||||
Mit dem können wir dann ein Polynom mit der baryzentrischen Interpolationsformel interpolieren:
|
Mit dem können wir dann ein Polynom mit der baryzentrischen Interpolationsformel interpolieren:
|
||||||
\setcounter{numberingConfig}{0}
|
\setcounter{numberingConfig}{0}
|
||||||
\begin{formula}[]{Baryzentrische Interpolationsformel}
|
\begin{formula}[]{Baryzentrische Interpolationsformel}
|
||||||
@@ -94,17 +107,6 @@ Eine weitere Anwendung der Formel ist als Ausganspunkt für die Spektralmethode
|
|||||||
barweight: np.ndarray,
|
barweight: np.ndarray,
|
||||||
x: np.ndarray
|
x: np.ndarray
|
||||||
):
|
):
|
||||||
"""Compute an Interpolation polynomial p(x) using the barycentric interpolation formula
|
|
||||||
|
|
||||||
Args:
|
|
||||||
data_point_x: The data points' x-coordinate from which to interpolate (Stützstellen)
|
|
||||||
data_point_y: The data points' y-coordinates (Stützwerte)
|
|
||||||
barweight: Barycentric weights
|
|
||||||
x: The argument of the polynomial (the x in p(x))
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
The Interpolation polynomial evaluated at each x
|
|
||||||
"""
|
|
||||||
p_x = np.zeros_like(x)
|
p_x = np.zeros_like(x)
|
||||||
n = data_point_x.shape[0]
|
n = data_point_x.shape[0]
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
\subsection{Diskrete Fourier Transformation}
|
\subsection{Diskrete Fourier Transformation}
|
||||||
|
|
||||||
% NOTE: I'll do these 2 subchapters. Based on the lecture, we can leave out quite a lot here.
|
% NOTE: I'll do these 2 subchapters. Based on the lecture, we can leave out quite a lot here.
|
||||||
Lecture: 3.2.1 eigentlich nur Endergebnis wichtig. 3.2.2 viel anschaulicher und theo. Grundlage für Anwendung. 3.2.3 zeigt kurz code.
|
% Lecture: 3.2.1 eigentlich nur Endergebnis wichtig. 3.2.2 viel anschaulicher und theo. Grundlage für Anwendung. 3.2.3 zeigt kurz code.
|
||||||
% 1/sqrt(N) ist numerisch sehr schwer, d.h. wenn möglich nie nutzen. (d.h. ist bem 3.2.8 genau so definiert)
|
% 1/sqrt(N) ist numerisch sehr schwer, d.h. wenn möglich nie nutzen. (d.h. ist bem 3.2.8 genau so definiert)
|
||||||
% Bsp. 3.2.23 wichtig: zeigt anschaulich wieso DFT genial ist.
|
% Viele gute Bsps in 2.3.3, würde ich aber nicht hier übernehmen
|
||||||
% NOTE: script p.74 sum transformation has errors. he said he'll fix.
|
% NOTE: script p.74 sum transformation has errors. he said he'll fix.
|
||||||
|
|
||||||
|
% 2.3.4 relativ wichtig (Einführung Faltungen), tendenziell viel
|
||||||
|
% 2.4 FFT wichtig, aber kurz
|
||||||
|
|||||||
Reference in New Issue
Block a user