Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MBEM-2020
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
Container registry
Model registry
Operate
Environments
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Benjamin Berkels
MBEM-2020
Commits
d04af8b6
Commit
d04af8b6
authored
5 years ago
by
Benjamin Berkels
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
352e4078
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-321.ipynb
+108
-0
108 additions, 0 deletions
exp-321.ipynb
with
108 additions
and
0 deletions
exp-321.ipynb
0 → 100644
+
108
−
0
View file @
d04af8b6
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### IPython notebook for Example 3.2.1 from the lecture"
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"import numpy as np\n",
"from IPython.display import display, Math\n",
"\n",
"\n",
"def kappa(z):\n",
" return np.power(z, -1.5)*np.exp(-1/(4*z)) / (2*np.sqrt(np.pi))\n",
"\n",
"\n",
"n = 20\n",
"h = 1/n\n",
"\n",
"display(Math(r'\\text{Create }A,b_1,b_2\\text{ from Example 1.7.1}'))\n",
"A = np.zeros((n, n))\n",
"for i in range(n):\n",
" for j in range(i+1):\n",
" A[i, j] = h * kappa((i+1)*h - (j+0.5)*h)\n",
"\n",
"b_1 = A[:, 0]\n",
"b_2 = A[:, 1]"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"metadata": {},
"source": [
"display(Math(r'U^TAV=\\Sigma'))\n",
"U, sigma, VT = np.linalg.svd(A)"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"metadata": {},
"source": [
"print('sigma_1 = {:.4f}'.format(sigma[0]))\n",
"print('sigma_19 = {:.4f}'.format(sigma[18]))\n",
"print('sigma_20 = {:.2e}'.format(sigma[19]))"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"metadata": {},
"source": [
"display(Math(r'\\Sigma_{\\rm reg}^\\dagger:= {\\rm diag}(\\sigma_1^{-1}, \\ldots, \\sigma_{19}^{-1},0)'))\n",
"sigma_dagger = 1 / sigma\n",
"sigma_dagger[-1] = 0\n",
"display(Math(r'A_{\\rm reg}^\\dagger:=V \\Sigma_{\\rm reg}^\\dagger U^T'))\n",
"A_dagger_reg = np.matmul(np.matmul(VT.T, np.diag(sigma_dagger)), U.T)\n",
"display(Math(r'\\tilde z_{\\rm reg}= A_{\\rm reg}^\\dagger(b_1+b_2)'))\n",
"z_reg_tilde = np.matmul(A_dagger_reg, b_1+b_2)\n",
"z = np.zeros(n)"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"metadata": {},
"source": [
"z[0:2] = 1\n",
"print('||z_reg_tilde-z||_2 = {:.1e}'.format(np.linalg.norm(z_reg_tilde-z)))\n",
"display(Math(r'\\text{In Example 1.7.1, we had }\\|\\tilde z\\|_2=9.5\\cdot10^{24}\\text{ for }\\tilde z=A\\setminus (b_1+b_2)'))\n"
],
"outputs": [],
"execution_count": null
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
\ No newline at end of file
%% Cell type:markdown id: tags:
### IPython notebook for Example 3.2.1 from the lecture
%% Cell type:code id: tags:
```
python
import
numpy
as
np
from
IPython.display
import
display
,
Math
def
kappa
(
z
):
return
np
.
power
(
z
,
-
1.5
)
*
np
.
exp
(
-
1
/
(
4
*
z
))
/
(
2
*
np
.
sqrt
(
np
.
pi
))
n
=
20
h
=
1
/
n
display
(
Math
(
r
'
\text{Create }A,b_1,b_2\text{ from Example 1.7.1}
'
))
A
=
np
.
zeros
((
n
,
n
))
for
i
in
range
(
n
):
for
j
in
range
(
i
+
1
):
A
[
i
,
j
]
=
h
*
kappa
((
i
+
1
)
*
h
-
(
j
+
0.5
)
*
h
)
b_1
=
A
[:,
0
]
b_2
=
A
[:,
1
]
```
%% Cell type:code id: tags:
```
python
display
(
Math
(
r
'
U^TAV=\Sigma
'
))
U
,
sigma
,
VT
=
np
.
linalg
.
svd
(
A
)
```
%% Cell type:code id: tags:
```
python
print
(
'
sigma_1 = {:.4f}
'
.
format
(
sigma
[
0
]))
print
(
'
sigma_19 = {:.4f}
'
.
format
(
sigma
[
18
]))
print
(
'
sigma_20 = {:.2e}
'
.
format
(
sigma
[
19
]))
```
%% Cell type:code id: tags:
```
python
display
(
Math
(
r
'
\Sigma_{\rm reg}^\dagger:= {\rm diag}(\sigma_1^{-1}, \ldots, \sigma_{19}^{-1},0)
'
))
sigma_dagger
=
1
/
sigma
sigma_dagger
[
-
1
]
=
0
display
(
Math
(
r
'
A_{\rm reg}^\dagger:=V \Sigma_{\rm reg}^\dagger U^T
'
))
A_dagger_reg
=
np
.
matmul
(
np
.
matmul
(
VT
.
T
,
np
.
diag
(
sigma_dagger
)),
U
.
T
)
display
(
Math
(
r
'
\tilde z_{\rm reg}= A_{\rm reg}^\dagger(b_1+b_2)
'
))
z_reg_tilde
=
np
.
matmul
(
A_dagger_reg
,
b_1
+
b_2
)
z
=
np
.
zeros
(
n
)
```
%% Cell type:code id: tags:
```
python
z
[
0
:
2
]
=
1
print
(
'
||z_reg_tilde-z||_2 = {:.1e}
'
.
format
(
np
.
linalg
.
norm
(
z_reg_tilde
-
z
)))
display
(
Math
(
r
'
\text{In Example 1.7.1, we had }\|\tilde z\|_2=9.5\cdot10^{24}\text{ for }\tilde z=A\setminus (b_1+b_2)
'
))
```
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