u,s,vh=sparse.linalg.svds(A,2)# 2 largest singular values
u,s,vh=sparse.linalg.svds(A,2)# 2 largest singular values
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## SciPy Example 5: Interpolation
## SciPy Example 5: Interpolation
* Interpolation of 1D and multi-D data (structured grid and unstructured points)
* Interpolation of 1D and multi-D data (structured grid and unstructured points)
* Splines and other polynomials
* Splines and other polynomials
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
importscipy.interpolateasinterpolate
importscipy.interpolateasinterpolate
x=np.linspace(0,10.0,12)
x=np.linspace(0,10.0,12)
y=np.sin(x)
y=np.sin(x)
y_int1=interpolate.interp1d(x,y)
y_int1=interpolate.interp1d(x,y)
y_int2=interpolate.PchipInterpolator(x,y)
y_int2=interpolate.PchipInterpolator(x,y)
importmatplotlib.pyplotasplt
importmatplotlib.pyplotasplt
plt.figure(figsize=(20,6))
plt.figure(figsize=(20,6))
plt.subplot(1,2,1)
plt.subplot(1,2,1)
plt.plot(x,y)
plt.plot(x,y)
x2=np.linspace(0,10.0,50)
x2=np.linspace(0,10.0,50)
plt.plot(x2,y_int1(x2))
plt.plot(x2,y_int1(x2))
plt.subplot(1,2,2)
plt.subplot(1,2,2)
plt.plot(x,y)
plt.plot(x,y)
im=plt.plot(x2,y_int2(x2))
im=plt.plot(x2,y_int2(x2))
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## NumPy/SciPy Summary
## NumPy/SciPy Summary
* NumPy: efficient handling of arrays
* NumPy: efficient handling of arrays
* SciPy: more advanced mathematical and numerical routines; uses NumPy under the hood *plus* other C/F libraries
* SciPy: more advanced mathematical and numerical routines; uses NumPy under the hood *plus* other C/F libraries
* Performance tips:
* Performance tips:
* Work on full arrays (slicing, NumPy routines...)
* Work on full arrays (slicing, NumPy routines...)
* identify hotspots and optimize them
* identify hotspots and optimize them
* profile
* profile
* code in Cython or C/Fortran + Python interface -- or try Numba
* code in Cython or C/Fortran + Python interface -- or try Numba
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Futher reading
## Futher reading
* Harris, C.R., Millman, K.J., van der Walt, S.J. et al. *Array programming with NumPy.***Nature** 585, 357–362 (2020). (https://doi.org/10.1038/s41586-020-2649-2)
* Harris, C.R., Millman, K.J., van der Walt, S.J. et al. *Array programming with NumPy.***Nature** 585, 357–362 (2020). (https://doi.org/10.1038/s41586-020-2649-2)
* Bressert, E. (2012). SciPy and NumPy (1st edition.). O'Reilly Media, Inc. (https://ebooks.mpdl.mpg.de/ebooks/Record/EB001944176)
* Bressert, E. (2012). SciPy and NumPy (1st edition.). O'Reilly Media, Inc. (https://ebooks.mpdl.mpg.de/ebooks/Record/EB001944176)
* There are numerous books on the topic available: https://ebooks.mpdl.mpg.de/ebooks/Search/Results?type=AllFields&lookfor=numpy
* There are numerous books on the topic available: https://ebooks.mpdl.mpg.de/ebooks/Search/Results?type=AllFields&lookfor=numpy
(MPG.eBooks work from any Max Planck IP address.)
(MPG.eBooks work from any Max Planck IP address.)
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Numpy Input and Output
# Numpy Input and Output
## Reading and writing NumPy arrays
## Reading and writing NumPy arrays
Possibilities:
Possibilities:
* NumPy's native IO facilities
* NumPy's native IO facilities
* HDF5 using H5PY
* HDF5 using H5PY
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Native NumPy Input/Output
## Native NumPy Input/Output
### Saving NumPy arrays to files
### Saving NumPy arrays to files
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
importos
importos
importnumpyasnp
importnumpyasnp
x=np.linspace(0.0,100.0,101)
x=np.linspace(0.0,100.0,101)
# save to text file (for debugging and small files only)
# save to text file (for debugging and small files only)