︠b083473a-e807-45be-bbb9-686425fa54d2i︠ %md _Sage_ (or _SageMath_) stands for "System for Algebra and Geometry Experimentation" * Free and open-source algebra system. * Built on Python. * Available on CoCalc---but you can also install it on your own machine. * Lots of built-in functions available for mathematics. A basic documentation can be found [here](http://jephianlin.github.io/SageBasics.pdf). ︡48783d82-27d9-44da-9dd7-9c825d744f96︡{"done":true,"md":"\n_Sage_ (or _SageMath_) stands for \"System for Algebra and Geometry Experimentation\"\n* Free and open-source algebra system.\n* Built on Python.\n* Available on CoCalc---but you can also install it on your own machine.\n* Lots of built-in functions available for mathematics.\n\nA basic documentation can be found [here](http://jephianlin.github.io/SageBasics.pdf)."} ︠a071d6ab-f698-40e4-9325-3e486c6810b0s︠ ### Sage examples ### press shift+enter to evaluate the cell 1+1 ︡31ee386a-15bc-4183-a28a-bfdfb6be3f89︡ ︠d7fe3c63-1a39-47dd-ba26-52da2f4a3ac7s︠ ### use tab to auto-complete your typing ### (move your cursor to the end of "facto" and press tab) factor(12) ︡63b80c2e-685c-42c3-9114-994bcc38b42e︡ ︠e0b6a975-2b48-43e3-a1a4-8dbb81371719s︠ ### use equal sign to assign a value a=3; f(x)=x^2+1; M=matrix( [ [1,2], [3,4] ]); print M ︡ebd1ba6e-e403-4eb9-9cc5-c3253298d93c︡ ︠34b5bd24-770e-41a8-8e59-4f753c606d88s︠ ### use print to output the value print M; ### Python 2 only print(M); ### Python 2 or Python 3 ︡0273ff68-dea0-4278-8b65-0fb7cbc75f19︡ ︠3989fd9f-9e3d-4237-80fb-957d1228d7e7︠ ### now M is a matrix ### type M. and press tab to see available functions M.determinant() ︡e69366fe-ecc6-4397-99a6-d7e0297dfb05︡ ︠1e3668b5-e804-4549-9333-8dbdca3cbdcfs︠ ### "inverse" is a function under the object "matrix" ### evaluate M.inverse? to see the document M.inverse? ︡5edbffec-ee0f-4966-b664-085ffe0ddc0e︡ ︠f8f3ce24-2821-43f9-911a-3ca90825eb37︠ ### evaluate M.inverse?? to read the source code M.inverse?? ︡6ce42667-49f1-4fc9-a017-6c8b6339ec7f︡ ︠bf3fe100-27fc-49cc-bf26-accaf414c730︠ ### Exercise: ### Find the Taylor expansion of g at x=0. ### [Hint: use f. tab to see available functions ### or Google "taylor expansion, sage"] f(x)=x^2+1 g(x)=exp(-x^2); ### This does not hold in Python g.show(); ︡00dafb09-100c-425a-8021-4baf7d870526︡ ︠2cc997dc-ae4a-41d3-8481-6e7129705694s︠ ### Exercise: ### Find the determinant and the eigenvalues of X. X=matrix(3,range(9)); show(X); ︡fda2d7e3-b393-4105-ab1e-23c6a24f896a︡ ︠67c7f251-914d-4898-bf1c-3b67d70327bd︠ ### Exercise: ### Think of a math concept and search online ### to see how to define it in Sage. Then ### see the available functions under it. ### (E.g., g=graphs.CompleteGraph(5);) ︡79f8448e-25da-4fe0-9715-cbc678b0253c︡ ︠adfbbfe3-384d-43a2-94d3-325c6d304471s︠ ### list is a sequence of values f=[1,1,2,3,5,8]; ︡1bb01bbf-99df-4479-b943-a9c595e11488︡ ︠1549c476-3346-4939-a4c6-56a348ac43d2s︠ ### Use a[i] to call the value of a on index i f[4] ︡80f0f7d1-91fa-4551-bd3a-01c9d89c0a1b︡ ︠d4baec18-2103-4e6e-af3a-66a553d5b90bs︠ ### range(a,b) gives a list from a to b-1 range(5,10) ︡7aaef645-fda4-411f-a4fa-82e52a262dde︡ ︠b444d6fe-b67e-484d-a7d9-2098c0fee56cs︠ ### called "for loop" ### for list: ### do something; # indentation is important! Usually, 4 spaces or a tab. for i in range(5,10): print i^2; ### indentation is important!!!! ︡813db54f-ed64-44b3-979f-5eee73e17781︡ ︠cf65726b-ff0d-487c-a93e-2a9e66e55bea︠ ### called "if statement" ### if test: ### do something; k=10; ### k % 2 means the remainder of k divided by 2 if k % 2 == 0: print "k is even" ︡118bd8f0-f4d0-4fa0-9119-add6c77c08d4︡ ︠a9b7499d-7fc7-4c81-80a7-c477131d0ac0s︠ ### combining for loop and if statment can do many things for i in range(1,11): if i % 2 == 1: print i; ︡5eefdc0a-1a8e-4122-8bc7-109a18c282dc︡ ︠25c58b8c-e988-4cce-9202-b1b780cc31fas︠ ### a counter help you to count counter = 0; for i in range(1,11): if i % 2 == 1: counter = counter + 1; print counter; ︡49c08b40-2b32-4ffd-baa6-9a1f0ec4fd4b︡ ︠c4cd238d-0cb1-4ab9-a3e4-403ac0d56726s︠ ### def function_name(input_parameters): ### do something using input_parameters; ### return something; def how_many_odd_numbers(n): counter = 0; for i in range(1,n+1): if i % 2 == 1: counter = counter + 1; return counter; ︡5cfc689c-25b0-421f-96c4-d45439ff4747︡ ︠16312829-16b3-491d-b329-e77f62665e54s︠ how_many_odd_numbers(15) ︡0a6c6312-d25c-4c7f-88c7-18ca472c4c01︡ ︠773cbfbb-b5b4-485b-8d69-d4b566744ed5︠ ### greatest common divisor gcd(6,4) ︡5393bac4-6867-4c39-bb38-cb502103af07︡ ︠45535785-5fef-40cf-8d3c-8e132e113cb5s︠ ### prime decomposition factor(60) ︡6fb12408-6266-4bd5-b9b0-29e6e771325b︡ ︠800282c0-f41c-4be0-b092-41d370bbb389s︠ ### extract the factors and the powers list(factor(60)) ︡bb00d41e-efe8-4645-8ae6-345e515baffa︡ ︠9f20d4a5-6626-47fd-9308-d8da5f58503di︠ %md ## Project 1 Write a function to compute the Euler's totient function $\phi(n)$. _Do not use the function_ `euler_phi` _in Sage._ ︡1491331a-ff0e-4231-ae95-9c18acf6be1f︡{"done":true,"md":"\n## Project 1\nWrite a function to compute the Euler's totient function $\\phi(n)$.\n\n_Do not use the function_ `euler_phi` _in Sage._"} ︠e8a40d27-ed26-4b3c-81aa-4bc1b950f580︠ def phi(n): ### Your answers here counter=0; for k in range(1,n+1): if gcd(k,n)==1: counter+=1; return counter; ### test; for n in range(1,11): print "phi({})={}, {}=correct answer".format(n,phi(n),euler_phi(n)) ︡17f55808-1e21-49f3-9691-e614adb9a2de︡ ︠9521f063-083a-4bb6-b448-aef313add05ci︠ %md ## Project 2 Write a function to compute the Moebius function $\mu(n)$. _Do not use the function_ `moebius` _in Sage._ ︡bc1a6203-7757-4240-9efd-494b052c3ed8︡{"done":true,"md":"## Project 2\nWrite a function to compute the Moebius function $\\mu(n)$.\n\n_Do not use the function_ `moebius` _in Sage._"} ︠fc083c04-5715-45cc-aa5a-d4fc7ea91a40︠ def mu(n): ### Your answers here f = list(factor(n)); for p in f: if p[1] >= 2: return 0; ### now the powers are either 0 or 1 r = sum([p[1] for p in f]); return (-1)^r; ### test; for n in range(1,11): print "mu({})={}, {}=correct answer".format(n,mu(n),moebius(n)) ︡9b5c85d3-f6c0-4ce8-affe-2cb07272364a︡ ︠6a6551c2-dba9-4378-809b-1fc31e17c0aa︠