Introducción a python científico

python es un lenguaje genérico, así que, casi siempre, hay que importar módulos, o bien enteros:


In [1]:
import scipy as np

o bien submódulos:


In [2]:
from matplotlib import pyplot as plt

Para ver los gráficos dentro de la hoja (% es un comando "mágico")


In [3]:
%matplotlib inline

Vectores numéricos


In [37]:
x=np.linspace(1,10,100)

In [38]:
x


Out[38]:
array([ 1.        ,  1.09090909,  1.18181818,  1.27272727,  1.36363636,
        1.45454545,  1.54545455,  1.63636364,  1.72727273,  1.81818182,
        1.90909091,  2.        ,  2.09090909,  2.18181818,  2.27272727,
        2.36363636,  2.45454545,  2.54545455,  2.63636364,  2.72727273,
        2.81818182,  2.90909091,  3.        ,  3.09090909,  3.18181818,
        3.27272727,  3.36363636,  3.45454545,  3.54545455,  3.63636364,
        3.72727273,  3.81818182,  3.90909091,  4.        ,  4.09090909,
        4.18181818,  4.27272727,  4.36363636,  4.45454545,  4.54545455,
        4.63636364,  4.72727273,  4.81818182,  4.90909091,  5.        ,
        5.09090909,  5.18181818,  5.27272727,  5.36363636,  5.45454545,
        5.54545455,  5.63636364,  5.72727273,  5.81818182,  5.90909091,
        6.        ,  6.09090909,  6.18181818,  6.27272727,  6.36363636,
        6.45454545,  6.54545455,  6.63636364,  6.72727273,  6.81818182,
        6.90909091,  7.        ,  7.09090909,  7.18181818,  7.27272727,
        7.36363636,  7.45454545,  7.54545455,  7.63636364,  7.72727273,
        7.81818182,  7.90909091,  8.        ,  8.09090909,  8.18181818,
        8.27272727,  8.36363636,  8.45454545,  8.54545455,  8.63636364,
        8.72727273,  8.81818182,  8.90909091,  9.        ,  9.09090909,
        9.18181818,  9.27272727,  9.36363636,  9.45454545,  9.54545455,
        9.63636364,  9.72727273,  9.81818182,  9.90909091, 10.        ])

In [6]:
plt.plot(x)


Out[6]:
[<matplotlib.lines.Line2D at 0x7fe81affc8d0>]

Funciones de vectores


In [7]:
y=np.cos(x)

In [8]:
plt.plot(x , y )


Out[8]:
[<matplotlib.lines.Line2D at 0x7fe81af89668>]

Índices


In [9]:
x[0]


Out[9]:
1.0

In [10]:
y[0]


Out[10]:
0.5403023058681398

Probar: x.[TAB]


In [11]:
x.size


Out[11]:
100

In [12]:
x[100]   # OJO!!!!


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-12-b6d854d200d4> in <module>
----> 1 x[100]   # OJO!!!!

IndexError: index 100 is out of bounds for axis 0 with size 100

In [13]:
x[99]


Out[13]:
10.0

In [14]:
x[0:2]


Out[14]:
array([1.        , 1.09090909])

Estructuras de control

¡¡ Atención al sangrado !!


In [17]:
for xx in x:
    print( xx - 1 )


0.0
0.09090909090909083
0.18181818181818188
0.2727272727272727
0.36363636363636376
0.4545454545454546
0.5454545454545454
0.6363636363636362
0.7272727272727273
0.8181818181818183
0.9090909090909092
1.0
1.0909090909090908
1.1818181818181817
1.2727272727272725
1.3636363636363638
1.4545454545454546
1.5454545454545454
1.6363636363636367
1.7272727272727275
1.8181818181818183
1.9090909090909092
2.0
2.090909090909091
2.1818181818181817
2.272727272727273
2.3636363636363638
2.4545454545454546
2.5454545454545454
2.6363636363636362
2.7272727272727275
2.8181818181818183
2.909090909090909
3.0
3.090909090909091
3.1818181818181817
3.2727272727272734
3.3636363636363633
3.454545454545455
3.545454545454545
3.6363636363636367
3.7272727272727275
3.8181818181818183
3.909090909090909
4.0
4.090909090909091
4.181818181818182
4.2727272727272725
4.363636363636363
4.454545454545455
4.545454545454546
4.636363636363637
4.7272727272727275
4.818181818181818
4.909090909090909
5.0
5.090909090909091
5.181818181818182
5.2727272727272725
5.363636363636364
5.454545454545455
5.545454545454546
5.636363636363637
5.7272727272727275
5.818181818181818
5.909090909090909
6.0
6.090909090909091
6.181818181818182
6.2727272727272725
6.363636363636364
6.454545454545455
6.545454545454546
6.636363636363637
6.7272727272727275
6.818181818181818
6.909090909090909
7.0
7.09090909090909
7.181818181818182
7.272727272727273
7.363636363636363
7.454545454545455
7.545454545454547
7.636363636363637
7.727272727272727
7.818181818181818
7.90909090909091
8.0
8.090909090909092
8.181818181818182
8.272727272727273
8.363636363636363
8.454545454545455
8.545454545454545
8.636363636363637
8.727272727272727
8.818181818181818
8.90909090909091
9.0

In [19]:
if x[0] < 1.2 :
    print ("Menor que 1.2")


Menor que 1.2

In [21]:
for xx in x:
    if xx < 1.2 :
        print ( "Menor que 1.2" )


Menor que 1.2
Menor que 1.2
Menor que 1.2

Ficheros: entrada y salida


In [39]:
np.savetxt( 'vector.txt' , x )

In [40]:
cc = np.cos( x )

In [42]:
np.savetxt( 'cos.txt' , ( x , cc ) )

In [45]:
np.savetxt( 'cos.txt' , np.transpose(( x , cc )) )

In [47]:
np.savetxt( 'cos.txt' , np.transpose(( x , cc ))  , delimiter=',')

In [48]:
np.savetxt( 'cos.txt' , np.transpose(( x , cc ))  , delimiter=',' , fmt='%1.4e' )

In [32]:
x = y = z = np.arange(0.0,5.0,1.0)
#np.savetxt('test.out', x, delimiter=',')   # X is an array
np.savetxt('test.out', (x,y,z))   # x,y,z equal sized 1D arrays
#np.savetxt('test.out', x, fmt='%1.4e')   # use exponential notation

In [50]:
data = np.loadtxt('cos_sin.csv' ) # error !!!!!

In [50]:
data = np.loadtxt('cos_sin.csv' , delimiter=',' )

In [58]:
xx = data[ : , 0 ]
ss = data[ : , 2 ]

In [59]:
plt.plot( xx , ss )


Out[59]:
[<matplotlib.lines.Line2D at 0x7fe81ad28518>]