All published worksheets from http://sagenb.org
Image: ubuntu2004
Tutorial on the scientific use of Python
My name is Fabio Grazioso, and here is my picture:
One of the key features of SAGE is the ability to build "notebooks", where computing code is shown along with its output, and with other "material" assembled by the author.
This is the reason why I start with my picture: to show how to insert styled text and "external" image files in a notebook.
To insert a "Text cell" shift-click the blue inserting line
To insert an image file (from local hard drive) in a "text cell":
1) upload the file on SAGE server using the "Data" menu
2) use the "edit/insert image" button in the toolbar of the "text cell"
(this can be interesting to import a plot from other software)
more details can be found here: http://wiki.sagemath.org/quickref?action=AttachFile&do=get&target=quickref.pdf
Basic Python functions
to have the logarithm in base 2 we have to convert
loading SciPy
if we want something more sophisticated, we have to load "SciPy", the scientific library:
now we can use dirctly log2(), which is defined in SciPy and returns logarithm in base 2:
in general, to find help on SciPy visit the official website:
I also use google to search within that: typing "log2 site:www.scipy.org" into gooooogle
Arrays in SciPy
Python has a "basic" object which is the list, denoted by square brackets:
and it is possible to do nice things with lists:
but to do more sophisticated things we use scipy arrays:
at this link there is a nice page with a summary about SciPy arrays:
http://pages.physics.cornell.edu/~myers/teaching/ComputationalMethods/python/arrays.html
control structures: for cycle syntax
Linear algebra, solving system of linear equations
to do some linear algebra, another library is needed, so we load it:
here we are solving the system «coefficients * x = knowns»
plotting
Although SAGE has its own tools for plotting, advanced scientific plotting in python is done using the library "matplotlib" (see http://matplotlib.sourceforge.net/)
Here we show how to use matplotlib in SAGE.
more examples:
Interactive plotting
here we have the very useful "interact" feature, where we change a parameter using a slider
|
3D plotting
numerical integration
minimization: finding local minima for functions of multiple variables
here is the documentation for the minimization function:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fmin_slsqp.html