Introduction
One alternative to MATLAB, Mathematica and Maple is to use SageMath. SageMath is build on open source programming language Python. There is a cloud based system cloud.sagemath.com [[https://cocalc.com ]]. In this course, we describe the use of 3 lightweight Python libraries:
- [Sympy]- Symbolic mathematics with Sympy.ipynb) for carrying out symbolic mathematical calculations;
- [Numpy]- Linear algebra with Numpy.ipynb) for dealing with large numeric arrays: perfect for linear algebra;
- [Pandas]- Data analysis with Pandas.ipynb) a brilliant tool for data analysis.
Variable
# Anything after a `#` will be ignored. This is what we call a 'comment' a = 20 # Assigning a value to a b = 21 # Assigning a value to b a * b # Calculating the product of a and b a / (a + b) # Calculating a / (a + b) a ** b # Calculating a raised to the power of b
Function
def proportion(a, b): """ We can use triple " to describe what our function does. Here for example: we're creating a function to calculate the proportion of a of a + b """ return a / (a + b) proportion? proportion(3, 1)
Function
Repeating things with for loopsWe can use a for loop to repeat bits of code with Python.
For example the following will calculate:
\[ \displaystyle \sum_{i=1}^9 i \]
total = 0 for i in range(10): total = total + i total
No comments:
Post a Comment