Commit 66fc581c authored by ggandus's avatar ggandus
Browse files

add file python_exemples

parent 362c0b7e
{
"cells": [
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"from ase import Atoms\n",
"from ase.visualize import view\n",
"from ase.io import read\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Atom('N', [0.0, 0.0, 1.0], index=0)"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"molecule = read('dummy_molecule.xyz')\n",
"view(molecule)\n",
"molecule[0]"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 0., 1.],\n",
" [0., 1., 0.],\n",
" [1., 0., 0.]])"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"molecule.get_positions()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n"
]
}
],
"source": [
"for atom in molecule:\n",
" print(atom.index)\n"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0. , 1.41421356, 1.41421356],\n",
" [0. , 0. , 1.41421356],\n",
" [0. , 0. , 0. ]])"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"distance = np.zeros([len(molecule.get_positions()),len(molecule.get_positions())])\n",
"\n",
"for atom in molecule:\n",
" for atom2 in molecule:\n",
" if atom2.index>atom.index:\n",
" distance[atom.index][atom2.index]=molecule.get_distance(atom.index,atom2.index)\n",
"\n",
" \n",
"distance"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:code id: tags:
``` python
from ase import Atoms
from ase.visualize import view
from ase.io import read
import numpy as np
```
%% Cell type:code id: tags:
``` python
molecule = read('dummy_molecule.xyz')
view(molecule)
molecule[0]
```
%%%% Output: execute_result
Atom('N', [0.0, 0.0, 1.0], index=0)
%% Cell type:code id: tags:
``` python
molecule.get_positions()
```
%%%% Output: execute_result
array([[0., 0., 1.],
[0., 1., 0.],
[1., 0., 0.]])
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
for atom in molecule:
print(atom.index)
```
%%%% Output: stream
0
1
2
%% Cell type:code id: tags:
``` python
distance = np.zeros([len(molecule.get_positions()),len(molecule.get_positions())])
for atom in molecule:
for atom2 in molecule:
if atom2.index>atom.index:
distance[atom.index][atom2.index]=molecule.get_distance(atom.index,atom2.index)
distance
```
%%%% Output: execute_result
array([[0. , 1.41421356, 1.41421356],
[0. , 0. , 1.41421356],
[0. , 0. , 0. ]])
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This cell is a *Markdown cell*. To create a markdown cell you have to go on the top menu and select *Markdown* from the curtain. For an overview of the basic commands of *Markdown cells* visit [this page](https://www.ibm.com/support/knowledgecenter/en/SSGNPV_1.1.3/dsx/markd-jupyter.html). You can modify this cell with a double click"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# NumPy\n",
"`numpy` is a fundamental Python package and it is mainly used for scientific calculations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Import `numpy` package calling it `np`. From now on everytime we want to use a `numpy` command we must refer to `numpy` as `np` "
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the following there are some examples for:\n",
"* [array creation](#sec1)\n",
"* [array manipulation](#sec2)\n",
" * [operations](#operations)\n",
" * [work with array](#arr)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***\n",
"<a id='sec1'></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Array Creation\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`numpy` offers many different tools for array creation. In the following there are some examples. I suggest to run all of them, see the output and try to modify something to understand better\n",
"### 1.direct definition"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 4, 5])"
]
},
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = np.array([1,2,3,4,5])\n",
"#the solo word corresponds to a print statement\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"get array shape:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5,)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"get array length:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"get array elements:"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*<font color=red> ATTENTION </font>: array elements start from 0*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"in the same way we can create a matrix:"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3],\n",
" [ 10, 20, 30],\n",
" [100, 200, 300]])"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y = np.array([[1,2,3],[10,20,30],[100,200,300]])\n",
"y"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 3)"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y.shape"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3])"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y[0]\n",
"#same as y[0,:]"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 10, 100])"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y[:,0]"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y[0][2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*<font color=red> ATTENTION </font>: the first index refers to the line and the second to the colum*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"### 2.`np.zeros` and `np.ones`\n",
"\n",
"creates an array of zeros [ones] of the specified length"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0., 0., 0., 0., 0.])"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=np.zeros(5)\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1., 1., 1., 1., 1.])"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y=np.ones(5)\n",
"y"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 1, 1, 1, 1])"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.ones(5, dtype=int)"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 1., 1., 1.],\n",
" [1., 1., 1., 1.]])"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],