︠1bf96ad4-378c-474c-811f-953ae14abe34i︠ %html
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
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.
\nThis is the reason why I start with my picture: to show how to insert styled text and \"external\" image files in a notebook.
\nTo insert a \"Text cell\" shift-click the blue inserting line
\nTo insert an image file (from local hard drive) in a \"text cell\":
\n1) upload the file on SAGE server using the \"Data\" menu
\n2) use the \"edit/insert image\" button in the toolbar of the \"text cell\"
\n(this can be interesting to import a plot from other software)
\nmore details can be found here: http://wiki.sagemath.org/quickref?action=AttachFile&do=get&target=quickref.pdf
\n\n
to have the logarithm in base 2 we have to convert
︡a7e9e1c5-9f97-4e79-86ea-24349daccb14︡{"html": "to have the logarithm in base 2 we have to convert
"}︡ ︠6ac2e53e-8582-41fd-93f3-2ac307e236b5︠ N(log(12) / log(2) ) ︡f806511d-3a7f-415a-b597-20f4538f5aa4︡{"html": "\\newcommand{\\Bold}[1]{\\mathbf{#1}}3.58496250072116"}︡ ︠e2df0436-9436-4772-88b7-61d4a6c97b38i︠ %htmlif we want something more sophisticated, we have to load "SciPy", the scientific library:
︡44209857-2fc2-4d72-ab32-4128963f4b9a︡{"html": "if we want something more sophisticated, we have to load \"SciPy\", the scientific library:
"}︡ ︠eb1f4afc-6d49-4c5a-a7c9-1af4c2e87588︠ import scipy as sp ︡0b766bf6-df9a-4de4-bc72-ee8da0038a96︡︡ ︠0ac02995-fc19-4e15-8b94-4b4c53b5aa74i︠ %htmlnow we can use dirctly log2(), which is defined in SciPy and returns logarithm in base 2:
︡9999a0d6-0ca5-4abf-8231-03bbb011b310︡{"html": "now we can use dirctly log2(), which is defined in SciPy and returns logarithm in base 2:
"}︡ ︠889aca5d-c3f1-4301-ba2d-0ea3fcc6787f︠ sp.log2(12) ︡d481b391-f120-4f28-b9cd-135433d2d835︡{"html": "\\newcommand{\\Bold}[1]{\\mathbf{#1}}3.58496250072"}︡ ︠c60978ba-8add-4e34-8d17-216ad512ea40i︠ %htmlin 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
Python has a "basic" object which is the list, denoted by square brackets:
︡4bf53464-45d2-492a-a706-df22da1c9bfe︡{"html": "in general, to find help on SciPy visit the official website:
\n\nI also use google to search within that: typing \"log2 site:www.scipy.org\" into gooooogle
\nPython has a \"basic\" object which is the list, denoted by square brackets:
"}︡ ︠a2e0ac84-63b4-4f43-87b3-fd13758d9155︠ myList = [1,2,3,4,5] ︡5af034fa-87f1-4734-84bc-b116d5419a18︡︡ ︠46c442dc-899f-4cb8-830d-c2c2ae139befi︠ %htmland it is possible to do nice things with lists:
︡6ab61005-dac1-4979-af7d-4d9edd77bd3b︡{"html": "and it is possible to do nice things with lists:
"}︡ ︠67fcc752-d245-468b-8275-706441fa5f4b︠ mySecondList = [3+x for x in myList] print(mySecondList) ︡1bfef2dd-ac0e-42ac-9b8c-14998539876d︡{"stdout": "[4, 5, 6, 7, 8]"}︡ ︠b350b208-cde0-459e-af0b-1503a4593786i︠ %htmlbut to do more sophisticated things we use scipy arrays:
︡48f383e4-d5a7-49e3-84fe-c6723f2f3f01︡{"html": "but to do more sophisticated things we use scipy arrays:
"}︡ ︠e2fab4c9-6494-43fd-bca7-78bb53135645︠ myArray = sp.array([1,2,3,4,5]) print(myArray) ︡bc32891d-61a4-4558-9f7f-f3172a6ea346︡{"stdout": "[1 2 3 4 5]"}︡ ︠92b58586-ed88-4e33-bad8-058f3d8da13a︠ my2DArray = sp.array([[0,1,2,3,4,5,6,7,8,9,10],[0,10,20,30,40,50,60,70,80,90,100],[0,11,22,33,44,55,66,77,88,99,111]]) print(my2DArray) ︡5889e29f-05df-4e32-b776-0bae605da52b︡{"stdout": "[[ 0 1 2 3 4 5 6 7 8 9 10]\n [ 0 10 20 30 40 50 60 70 80 90 100]\n [ 0 11 22 33 44 55 66 77 88 99 111]]"}︡ ︠36101a85-f271-4357-8963-326ad0653375︠ print(sp.transpose(my2DArray)) ︡d74b39a0-2dcc-4c8d-a325-b22eed57b4b0︡{"stdout": "[[ 0 0 0]\n [ 1 10 11]\n [ 2 20 22]\n [ 3 30 33]\n [ 4 40 44]\n [ 5 50 55]\n [ 6 60 66]\n [ 7 70 77]\n [ 8 80 88]\n [ 9 90 99]\n [ 10 100 111]]"}︡ ︠d5069f45-b4b6-4234-8b57-13263d0005a2︠ mySlice = my2DArray[0:2, :] # NB first included, last excluded! print(mySlice) ︡e86d5cbd-e59b-4c93-b10d-c781a9606136︡{"stdout": "[[ 0 1 2 3 4 5 6 7 8 9 10]\n [ 0 10 20 30 40 50 60 70 80 90 100]]"}︡ ︠44bc6aa2-d977-415a-bb52-9ea0e6af693bi︠ %htmlat this link there is a nice page with a summary about SciPy arrays:
http://pages.physics.cornell.edu/~myers/teaching/ComputationalMethods/python/arrays.html
at this link there is a nice page with a summary about SciPy arrays:
\nhttp://pages.physics.cornell.edu/~myers/teaching/ComputationalMethods/python/arrays.html
\n\nLinear algebra, solving system of linear equations
to do some linear algebra, another library is needed, so we load it:
︡296d54c6-3c2e-4cf5-b6e3-eb456d082508︡{"html": "Linear algebra, solving system of linear equations
\n\nto do some linear algebra, another library is needed, so we load it:
"}︡ ︠7d74f50c-b5d5-4fcb-b683-66b9fee11d4d︠ coefficients = sp.array([[2,3,6], [3,4,0], [2,4,6]]) knowns = sp.array([-1,7,0]) print(coefficients) print(knowns) ︡9b0a8423-dd76-4bc2-9462-0e7656a0a64f︡{"stdout": "[[2 3 6]\n [3 4 0]\n [2 4 6]]\n[-1 7 0]"}︡ ︠5d3dd661-61ee-4baf-9df6-968ae88eb54a︠ from scipy import linalg #for some reason linalg has to be imported explicitly x = sp.linalg.solve(coefficients, knowns) print(x) ︡6f0e3ecb-b8eb-41e0-9d92-eb549c1b32ec︡{"stdout": "[ 1. 1. -1.]"}︡ ︠a84e3d27-796d-4f91-bc1f-465d74855706︠ coefficients = sp.matrix(coefficients) x = sp.matrix(x) coefficients * sp.transpose(x) ︡6695345b-0981-4f68-a315-2d2ff8764361︡{"html": "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\begin{array}{l}\n\\verb|[[-1.]|\\\\\n\\phantom{x}\\verb|[|\\phantom{x}\\verb|7.]|\\\\\n\\phantom{x}\\verb|[|\\phantom{x}\\verb|0.]]|\n\\end{array}"}︡ ︠cc13d025-d00d-4281-a76a-a414f86b3afai︠ %htmlhere we are solving the system «coefficients * x = knowns»
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.
here we are solving the system «coefficients * x = knowns»
\nAlthough SAGE has its own tools for plotting, advanced scientific plotting in python is done using the library \"matplotlib\" (see http://matplotlib.sourceforge.net/)
\nHere we show how to use matplotlib in SAGE.
\nmore examples:
︡744e4810-0dd5-415c-878b-422fc9008529︡{"html": "more examples:
"}︡ ︠729f2669-bd1a-4a76-93b2-e955bb652da7︠ import scipy as sp import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties Xrange=sp.linspace(0, 5, 501) # def f1(x): return sp.log2(x) def f2(x): return sp.sin(x) # legend font size fontP = FontProperties() #legend font fontP.set_size('small') #legend font fig=plt.figure() # # shrink the "plot box" to make room for the legend plotbox = plt.subplot(111) box = plotbox.get_position() plotbox.set_position([box.x0, box.y0, box.width, box.height * 0.8]) var=3 plt.plot(Xrange, f1(Xrange), linewidth=2.0, label='$f(x)$') # plt.plot(Xrange, f2(Xrange), label='n'+ str(var)) # plt.axes(plotbox).tick_params(labelsize=16) # this is to change the font size of the axes tick labels plt.legend(loc='best', ncol=2, labelspacing=0.01, columnspacing=2, prop=fontP, bbox_to_anchor=(1, 1.2)) # plt.savefig('fig') ︡c13368fa-e52f-40df-a36a-eb779010d158︡{"stdout": "Warning: divide by zero encountered in log2"}︡ ︠d5eeafd5-5ca3-4a12-b000-1fd39c409e67i︠ %html
here we have the very useful "interact" feature, where we change a parameter using a slider
︡e910ebc7-8e11-4eaf-a2ef-d91c0e8a9059︡{"html": "\n
here we have the very useful \"interact\" feature, where we change a parameter using a slider
"}︡ ︠9512dec1-6249-4807-a462-4b214d704b9e︠ import scipy as sp import matplotlib.pyplot as plt const12 = 0.5 * sp.ones(501) # definition of a constant in the plot @interact def _(xi=(0,1), xMax=(1,20), yMax=(1,20)): #here the "sliders" parameters are defined. xi=(1..2) is a discrete parameter Xrange=sp.linspace(0, xMax, 501) # definition of the abscissa range and resolution def eZ(d): return d/(0.25 + 2*d) def eZQ(d,xi): return d/(0.25 +(1-xi)*d) fig=plt.figure() ax = fig.add_subplot(111) ax.set_ylim([0, yMax]) #here limits in y are defined plt.plot(Xrange, eZ(Xrange)) plt.plot(Xrange, eZQ(Xrange, xi)) plt.plot(Xrange,Xrange, color='red') plt.plot(Xrange,const12, color='yellow') plt.savefig('fig') ︡bb2a5735-5717-43ee-a21b-33fb019b2fea︡{"html": "\n\n
| \n
here is the documentation for the minimization function:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fmin_slsqp.html
here is the documentation for the minimization function:
\nhttp://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fmin_slsqp.html
\n