"# Wirkung der 1d-Poissongleichung auf einen Vektor\n",
"Es soll eine Funktion erstellt werden, die die Wirkung der 1d-Poissongleichung auf einen Vektor x simuliert. Die Idee dafür ist es am Anfang und Ende von x eine zusätzliche 0 einzufügen (sogenanntes zero padding) und dann entsprechende Ausschnitte auf x zu addieren."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0 1 2 3 0]\n"
]
}
],
"source": [
"from numpy import pad, array\n",
"\n",
"a=array([1,2,3])\n",
"print(pad(a,1,mode='constant'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nun die Funktion, die die Wirkung der 1d-Poissonmatrix auf einen Vektor berechnet:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def poisson1d(x):\n",
" h=pad(x,1,mode='constant')\n",
" y=2*x-h[:-2]-h[2:]\n",
" n=max(x.shape)+1\n",
" return y*n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Wir testen an zwei einfachen Beispielen, um uns von der Richtigkeit zu überzeugen:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 0 16]\n",
"[ 10. 0. 0. 0. 0. 0. 0. 0. 10.]\n"
]
}
],
"source": [
"from numpy import ones\n",
"\n",
"print(poisson1d(a))\n",
"\n",
"b=ones(9)\n",
"print(poisson1d(b))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Als Hinweise für den 2d-Fall: "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 0, 0, 0],\n",
" [0, 1, 2, 0],\n",
" [0, 3, 4, 0],\n",
" [0, 0, 0, 0]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c=array([[1,2],[3,4]])\n",
"pad(c,1, mode='constant')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2]\n",
" [3 4]]\n",
"[1 2 3 4]\n"
]
}
],
"source": [
"d=array([1,2,3,4])\n",
"n=max(d.shape)\n",
"e=d.reshape((int(n**(1/2)),int(n**(1/2))))\n",
"print(e)\n",
"print(e.ravel())"
]
}
],
"metadata": {
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
%% Cell type:markdown id: tags:
# Wirkung der 1d-Poissongleichung auf einen Vektor
Es soll eine Funktion erstellt werden, die die Wirkung der 1d-Poissongleichung auf einen Vektor x simuliert. Die Idee dafür ist es am Anfang und Ende von x eine zusätzliche 0 einzufügen (sogenanntes zero padding) und dann entsprechende Ausschnitte auf x zu addieren.
%% Cell type:code id: tags:
``` python
fromnumpyimportpad,array
a=array([1,2,3])
print(pad(a,1,mode='constant'))
```
%% Output
[0 1 2 3 0]
%% Cell type:markdown id: tags:
Nun die Funktion, die die Wirkung der 1d-Poissonmatrix auf einen Vektor berechnet:
%% Cell type:code id: tags:
``` python
defpoisson1d(x):
h=pad(x,1,mode='constant')
y=2*x-h[:-2]-h[2:]
n=max(x.shape)+1
returny*n
```
%% Cell type:markdown id: tags:
Wir testen an zwei einfachen Beispielen, um uns von der Richtigkeit zu überzeugen: