{"id":3099,"date":"2015-01-24T03:38:22","date_gmt":"2015-01-24T03:38:22","guid":{"rendered":"http:\/\/www.dientoan.us\/?p=3099"},"modified":"2018-06-19T11:31:59","modified_gmt":"2018-06-19T11:31:59","slug":"python-sage-and-matplotlib","status":"publish","type":"post","link":"http:\/\/www.dientoan.us\/?p=3099","title":{"rendered":"Python Sage and Matplotlib"},"content":{"rendered":"<div id=container>\n<div id=left style=\"width:50%;float:left\">The Python computer algebra package Sage and its built-in Matplolib are useful shortcuts to the development of integrated solution to teaching and grading numerous calculus exercises and concepts. This article summarizes major differences of plotting via Sage graphics vs. using Matplotlib directly. Even though I am not experienced with Python, my initial exposure to Python convinced me that the language\/library quality is superior to php in terms of plotting support, math libraries, and data types.<\/p>\n<p>1. Sage has its own built-in graphics. Axes created by Sage are more in line with textbook mathematical presentation with the default x and y-axis. Matplotlib default setup for axes looks more chart-like. Matplotlib&#8217;s plotting area and axis area can have different background colors.<br \/>\n2. Python uses indentation for conditional statements, continuation, etc. There is no need to have and ending semicolon at the end of the statement. Data types include lists, tuples, iterations. Iteration loops are much cleaner than C and php.<br \/>\n3. After Sage graphics is created, Matplotlib can be called directly to access options not provided by Sage (minor tick mark control, location and format of grid lines, etc).<br \/>\n4. Sage mathematical functions include gcf, lcm, fraction, f(x), derivative, matrix calculations, etc. This is a superior and much more flexible substitute for hand-held graphic calculator (TI-CAS and HP). Sage notebook and command line works on Android tablet and phone devices.<br \/>\n5. Definition and log files can be loaded or attached in addition to log and session information (see dictionary.sage).<\/p>\n<p>The following graph tests the following options provided by Sage\/Matplotlib.<br \/>\n<img src=\"\/images\/matplotlib.calculus.jpg\" width=267 height=200 border=1><br \/>\n6. Name of properties or functions can be found by <u>typing part of the initial characters and <Tab><\/u>.<br \/>\n7. Sage&#8217;s image file format does not support .jpg (only .png and .pdf) while <u>Matplotlib offers .jpg support<\/u> through PIL Python Image Library.<br \/>\n8. latex(f(x)) to convert standard string to LaTex format (can use $string$ to do the conversion).<br \/>\n9. <u>Power of Sage<\/u>:<br \/>\nFind the roots of quadratic equation:<br \/>\nsage: s=solve(3*x^2+4*x-11==0,x)<br \/>\nsage: s<br \/>\n[x == -1\/3*sqrt(37) &#8211; 2\/3, x == 1\/3*sqrt(37) &#8211; 2\/3]<br \/>\nCheck for the number of roots (can set up for real or imaginary).<br \/>\nsage: len(s)<br \/>\n2<br \/>\nsage: s[0]<br \/>\nx == -1\/3*sqrt(37) &#8211; 2\/3<br \/>\nsage: s[1]<br \/>\nx == 1\/3*sqrt(37) &#8211; 2\/3<br \/>\nsage: s[0].right()<br \/>\n-1\/3*sqrt(37) &#8211; 2\/3<br \/>\n<u>Return in 5 digits of 32-bit precision<\/u><br \/>\nsage: s[0].right().n(digits=5)<br \/>\n-2.6943<br \/>\nsage: s[0].right().n(prec=32)<br \/>\n-2.69425418\n<\/div>\n<div id=right style=\"margin-left:50%;padding-left:10px;\">\n10. Tuples and iterations:<br \/>\nsage: ptx=[0,1,-1,-2]<br \/>\nsage: pty=[2,3,-3,-2]<br \/>\nsage: ptx<br \/>\n[0, 1, -1, -2]<br \/>\nsage: pty<br \/>\n[2, 3, -3, -2]<br \/>\nsage: pts=zip(ptx,pty)<br \/>\nsage: pts<br \/>\n[(0, 2), (1, 3), (-1, -3), (-2, -2)]<br \/>\nsage: pts+=zip([4],[5])<br \/>\nsage: pts<br \/>\n[(0, 2), (1, 3), (-1, -3), (-2, -2), (4, 5)]<br \/>\nsage: pts[0]==[0,2]<br \/>\nFalse<br \/>\nsage: pts[0]==[(0,2)]<br \/>\nFalse<br \/>\nsage: pts[0]==(0,2)<br \/>\nTrue<br \/>\nsage: for ptn in pts: print &#8220;(%1d,%1d)&#8221;%ptn<br \/>\n(0,2)<br \/>\n(1,3)<br \/>\n(-1,-3)<br \/>\n(-2,-2)<br \/>\n(4,5)<\/p>\n<p>11. Function transformation using matrix.<br \/>\n#Transformation for function, not for point transformation.<br \/>\ndef t2dm(sx,dx,sy,dy):<br \/>\n    return matrix([[sx,0,-sx*dx],[0,sy,dy],[0,0,1]])<br \/>\ndef p2d(x,y):<br \/>\n    return matrix([[x],[y],[1]])<br \/>\ndef t2dp(M,x,y):<br \/>\n    return M*p2d(x,y)<\/p>\n<p>12. Customizations in the following graph.<br \/>\nPhim nguy\u00ean th\u1ee7y \u1edf d\u1ea1ng avi qu\u00e1 l\u1edbn b\u1ecb tr\u1ee5c tr\u1eb7c v\u1ec1 v\u1ea5n \u0111\u1ec1 b\u1ea3n quy\u1ec1n.<br \/>\n\u0110\u1ed5i sang d\u1ea1ng mp4 l\u00e0m phim nh\u1ecf l\u1ea1i \u0111\u1ec3 tr\u00e1nh vi ph\u1ea1m lu\u1eadt t\u00e1c quy\u1ec1n.<br \/>\nCh\u1ea3y m\u00e1u trong phim ch\u1eafc l\u00e0 c\u1ea3nh gi\u1ea3, n\u1eef di\u1ec5n vi\u00ean \u0111\u00f3ng k\u1ecbch kh\u00e1 gi\u1ed1ng.\n<\/p><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Python computer algebra package Sage and its built-in Matplolib are useful shortcuts to the development of integrated solution to teaching and grading numerous calculus exercises and concepts. This article summarizes major differences of plotting via Sage graphics vs. using &hellip; <a href=\"http:\/\/www.dientoan.us\/?p=3099\">\u0110\u1ecdc ti\u1ebfp <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/www.dientoan.us\/index.php?rest_route=\/wp\/v2\/posts\/3099"}],"collection":[{"href":"http:\/\/www.dientoan.us\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.dientoan.us\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.dientoan.us\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.dientoan.us\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3099"}],"version-history":[{"count":0,"href":"http:\/\/www.dientoan.us\/index.php?rest_route=\/wp\/v2\/posts\/3099\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.dientoan.us\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.dientoan.us\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3099"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.dientoan.us\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}