Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MBEM-2021
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Benjamin Berkels
MBEM-2021
Commits
90ae0ca7
Commit
90ae0ca7
authored
4 years ago
by
Benjamin Berkels
Browse files
Options
Downloads
Patches
Plain Diff
improved the IPython notebook for Example 2.2.4
parent
4e9d6f32
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
exp-224.ipynb
+34
-24
34 additions, 24 deletions
exp-224.ipynb
with
34 additions
and
24 deletions
exp-224.ipynb
+
34
−
24
View file @
90ae0ca7
...
...
@@ -14,21 +14,24 @@
"%matplotlib inline\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from IPython.display import set_matplotlib_formats, display, Math\n",
"set_matplotlib_formats('svg')\n",
"from matplotlib_inline.backend_inline import set_matplotlib_formats\n",
"from IPython.display import display, Math\n",
"\n",
"set_matplotlib_formats(\"svg\")\n",
"\n",
"n = 40\n",
"h = 1 / n\n",
"\n",
"display(Math(r'''A = h \\begin{pmatrix} 1 & & & \\\\ 1 & 1 & &0 \\\\ \\vdots &\\vdots & \\ddots & \\\\ 1 & 1& \\dots & 1 \\end{pmatrix}'''))\n",
"display(Math(r\"\\text{Setting from Section 1.2:}\"))\n",
"display(Math(r\"A = h \\begin{pmatrix} 1 & & & \\\\ 1 & 1 & &0 \\\\ \\vdots &\\vdots & \\ddots & \\\\ 1 & 1& \\dots & 1 \\end{pmatrix}\\text{ (discrete integration matrix)}\"))\n",
"A = h * np.tril(np.ones((n, n), dtype=int), 0)\n",
"\n",
"display(Math(r
'''
y= \\frac{1}{n}(1,1,\\ldots,1)^T
'''
))\n",
"display(Math(r
\"
y= \\frac{1}{n}(1,1,\\ldots,1)^T
\\text{ (low frequency input)}\"
))\n",
"y = np.ones(n) / n\n",
"display(Math(r
'''
\\delta y = \\frac{1}{n}(1,-1,1,-1, \\ldots)^T
'''
))\n",
"display(Math(r
\"
\\delta y = \\frac{1}{n}(1,-1,1,-1, \\ldots)^T
\\text{ (high frequency pertubation)}\"
))\n",
"delta_y = y.copy()\n",
"delta_y[1::2] = -1 / n\n",
"display(Math(r
'''
\\Rightarrow \\|y\\|_2= \\|\\delta y\\|_2= \\frac{1}{\\sqrt{n}}
'''
))"
"display(Math(r
\"
\\Rightarrow \\|y\\|_2= \\|\\delta y\\|_2= \\frac{1}{\\sqrt{n}}
\"
))"
],
"outputs": [],
"execution_count": null
...
...
@@ -37,12 +40,12 @@
"cell_type": "code",
"metadata": {},
"source": [
"display(Math(r
'
U^TAV=\\Sigma
'
))\n",
"display(Math(r
\"
U^TAV=\\Sigma
\"
))\n",
"U, sigma, VT = np.linalg.svd(A)\n",
"\n",
"display(Math(r
'
\\hat y=U^Ty
'
))\n",
"display(Math(r
\"
\\hat y=U^Ty
\"
))\n",
"y_hat = np.matmul(U.T, y)\n",
"display(Math(r
'
\\hat{\\delta y}:= U^T \\delta y
'
))\n",
"display(Math(r
\"
\\hat{\\delta y}:= U^T \\delta y
\"
))\n",
"delta_y_hat = np.matmul(U.T, delta_y)\n",
"\n",
"sigma_inv = 1 / sigma\n",
...
...
@@ -55,9 +58,9 @@
"cell_type": "code",
"metadata": {},
"source": [
"plt.yscale(
'
log
'
)\n",
"plt.plot(j, np.abs(y_hat),
'.'
, label=r
'
$|(U^Ty)_j|$
'
)\n",
"plt.plot(j, np.abs(delta_y_hat),
'+'
, label=r
'
$|(U^T \\delta y)_j|$
'
)\n",
"plt.yscale(
\"
log
\"
)\n",
"plt.plot(j, np.abs(y_hat),
\".\"
, label=r
\"
$|(U^Ty)_j|$
\"
)\n",
"plt.plot(j, np.abs(delta_y_hat),
\"+\"
, label=r
\"
$|(U^T \\delta y)_j|$
\"
)\n",
"plt.legend()\n",
"plt.show()"
],
...
...
@@ -69,8 +72,8 @@
"metadata": {},
"source": [
"plt.close()\n",
"plt.yscale(
'
log
'
)\n",
"plt.plot(j, sigma_inv,
'*'
, label=r
'
$\\sigma_j^{-1}$
'
)\n",
"plt.yscale(
\"
log
\"
)\n",
"plt.plot(j, sigma_inv,
\"*\"
, label=r
\"
$\\sigma_j^{-1}$
\"
)\n",
"plt.legend()\n",
"plt.show()"
],
...
...
@@ -82,15 +85,22 @@
"metadata": {},
"source": [
"plt.close()\n",
"plt.yscale(
'
log
'
)\n",
"plt.plot(j, sigma_inv
*
np.abs(y_hat),
'.'
, label=r
'
$\\sigma_j^{-1}|(U^Ty)_j|$
'
)\n",
"plt.plot(j, sigma_inv
*
np.abs(delta_y_hat),
'+'
, label=r
'
$\\sigma_j^{-1}|(U^T \\delta y)_j|$
'
)\n",
"plt.yscale(
\"
log
\"
)\n",
"plt.plot(j, sigma_inv
*
np.abs(y_hat),
\".\"
, label=r
\"
$\\sigma_j^{-1}|(U^Ty)_j|$
\"
)\n",
"plt.plot(j, sigma_inv
*
np.abs(delta_y_hat),
\"+\"
, label=r
\"
$\\sigma_j^{-1}|(U^T \\delta y)_j|$
\"
)\n",
"plt.legend()\n",
"plt.show()\n",
"display(Math(r
'''
\\Rightarrow\\ \\|A^{-1} y\\|_2 = \\|\\Sigma^{-1} \\hat{y}\\|_2 \\ll \\|A^{-1} \\delta y\\|_2= \\|\\Sigma^{-1} \\hat{\\delta y}\\|_2\\ \\Rightarrow\\ Q(y,\\delta y) \\gg 1
'''))\n
"
"display(Math(r
\"
\\Rightarrow\\ \\|A^{-1} y\\|_2 = \\|\\Sigma^{-1} \\hat{y}\\|_2 \\ll \\|A^{-1} \\delta y\\|_2= \\|\\Sigma^{-1} \\hat{\\delta y}\\|_2\\ \\Rightarrow\\ Q(y,\\delta y) \\gg 1
\"))
"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"metadata": {},
"source": [],
"outputs": [],
"execution_count": null
}
],
"metadata": {
...
...
@@ -114,5 +124,5 @@
}
},
"nbformat": 4,
"nbformat_minor":
1
"nbformat_minor":
4
}
\ No newline at end of file
%% Cell type:markdown id: tags:
### IPython notebook for Example 2.2.4 from the lecture
%% Cell type:code id: tags:
```
python
%
matplotlib
inline
import
numpy
as
np
import
matplotlib.pyplot
as
plt
from
IPython.display
import
set_matplotlib_formats
,
display
,
Math
set_matplotlib_formats
(
'
svg
'
)
from
matplotlib_inline.backend_inline
import
set_matplotlib_formats
from
IPython.display
import
display
,
Math
n
=
40
h
=
1
/
n
set_matplotlib_formats
(
"
svg
"
)
display
(
Math
(
r
'''
A = h \begin{pmatrix} 1 & & & \\ 1 & 1 & &0 \\ \vdots &\vdots & \ddots & \\ 1 & 1& \dots & 1 \end{pmatrix}
'''
))
A
=
h
*
np
.
tril
(
np
.
ones
((
n
,
n
),
dtype
=
int
),
0
)
n
=
40
h
=
1
/
n
display
(
Math
(
r
'''
y= \frac{1}{n}(1,1,\ldots,1)^T
'''
))
y
=
np
.
ones
(
n
)
/
n
display
(
Math
(
r
'''
\delta y = \frac{1}{n}(1,-1,1,-1, \ldots)^T
'''
))
display
(
Math
(
r
"
\text{Setting from Section 1.2:}
"
))
display
(
Math
(
r
"
A = h \begin{pmatrix} 1 & & & \\ 1 & 1 & &0 \\ \vdots &\vdots & \ddots & \\ 1 & 1& \dots & 1 \end{pmatrix}\text{ (discrete integration matrix)}
"
))
A
=
h
*
np
.
tril
(
np
.
ones
((
n
,
n
),
dtype
=
int
),
0
)
display
(
Math
(
r
"
y= \frac{1}{n}(1,1,\ldots,1)^T\text{ (low frequency input)}
"
))
y
=
np
.
ones
(
n
)
/
n
display
(
Math
(
r
"
\delta y = \frac{1}{n}(1,-1,1,-1, \ldots)^T\text{ (high frequency pertubation)}
"
))
delta_y
=
y
.
copy
()
delta_y
[
1
::
2
]
=
-
1
/
n
display
(
Math
(
r
'''
\Rightarrow \|y\|_2= \|\delta y\|_2= \frac{1}{\sqrt{n}}
'''
))
delta_y
[
1
::
2
]
=
-
1
/
n
display
(
Math
(
r
"
\Rightarrow \|y\|_2= \|\delta y\|_2= \frac{1}{\sqrt{n}}
"
))
```
%% Cell type:code id: tags:
```
python
display
(
Math
(
r
'
U^TAV=\Sigma
'
))
display
(
Math
(
r
"
U^TAV=\Sigma
"
))
U
,
sigma
,
VT
=
np
.
linalg
.
svd
(
A
)
display
(
Math
(
r
'
\hat y=U^Ty
'
))
display
(
Math
(
r
"
\hat y=U^Ty
"
))
y_hat
=
np
.
matmul
(
U
.
T
,
y
)
display
(
Math
(
r
'
\hat{\delta y}:= U^T \delta y
'
))
display
(
Math
(
r
"
\hat{\delta y}:= U^T \delta y
"
))
delta_y_hat
=
np
.
matmul
(
U
.
T
,
delta_y
)
sigma_inv
=
1
/
sigma
sigma_inv
=
1
/
sigma
j
=
np
.
arange
(
1
,
41
)
```
%% Cell type:code id: tags:
```
python
plt
.
yscale
(
'
log
'
)
plt
.
plot
(
j
,
np
.
abs
(
y_hat
),
'
.
'
,
label
=
r
'
$|(U^Ty)_j|$
'
)
plt
.
plot
(
j
,
np
.
abs
(
delta_y_hat
),
'
+
'
,
label
=
r
'
$|(U^T \delta y)_j|$
'
)
plt
.
yscale
(
"
log
"
)
plt
.
plot
(
j
,
np
.
abs
(
y_hat
),
"
.
"
,
label
=
r
"
$|(U^Ty)_j|$
"
)
plt
.
plot
(
j
,
np
.
abs
(
delta_y_hat
),
"
+
"
,
label
=
r
"
$|(U^T \delta y)_j|$
"
)
plt
.
legend
()
plt
.
show
()
```
%% Cell type:code id: tags:
```
python
plt
.
close
()
plt
.
yscale
(
'
log
'
)
plt
.
plot
(
j
,
sigma_inv
,
'
*
'
,
label
=
r
'
$\sigma_j^{-1}$
'
)
plt
.
yscale
(
"
log
"
)
plt
.
plot
(
j
,
sigma_inv
,
"
*
"
,
label
=
r
"
$\sigma_j^{-1}$
"
)
plt
.
legend
()
plt
.
show
()
```
%% Cell type:code id: tags:
```
python
plt
.
close
()
plt
.
yscale
(
'
log
'
)
plt
.
plot
(
j
,
sigma_inv
*
np
.
abs
(
y_hat
),
'
.
'
,
label
=
r
'
$\sigma_j^{-1}|(U^Ty)_j|$
'
)
plt
.
plot
(
j
,
sigma_inv
*
np
.
abs
(
delta_y_hat
),
'
+
'
,
label
=
r
'
$\sigma_j^{-1}|(U^T \delta y)_j|$
'
)
plt
.
yscale
(
"
log
"
)
plt
.
plot
(
j
,
sigma_inv
*
np
.
abs
(
y_hat
),
"
.
"
,
label
=
r
"
$\sigma_j^{-1}|(U^Ty)_j|$
"
)
plt
.
plot
(
j
,
sigma_inv
*
np
.
abs
(
delta_y_hat
),
"
+
"
,
label
=
r
"
$\sigma_j^{-1}|(U^T \delta y)_j|$
"
)
plt
.
legend
()
plt
.
show
()
display
(
Math
(
r
'''
\Rightarrow\ \|A^{-1} y\|_2 = \|\Sigma^{-1} \hat{y}\|_2 \ll \|A^{-1} \delta y\|_2= \|\Sigma^{-1} \hat{\delta y}\|_2\ \Rightarrow\ Q(y,\delta y) \gg 1
'''
))
display
(
Math
(
r
"
\Rightarrow\ \|A^{-1} y\|_2 = \|\Sigma^{-1} \hat{y}\|_2 \ll \|A^{-1} \delta y\|_2= \|\Sigma^{-1} \hat{\delta y}\|_2\ \Rightarrow\ Q(y,\delta y) \gg 1
"
))
```
%% Cell type:code id: tags:
```
python
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment