Math with Python
Mathematics is an essential component of many scientific and engineering disciplines. Python, with its powerful libraries and extensive mathematical functionality, has become a popular choice for performing mathematical calculations and solving complex problems.
In Python eco-system there are several modules that offer rich mathematical capabilities., “
"math"Numpy", and "sympy" are some of the most important packages to carry out from basic arithmetic to advanced calculations.
Numpy: A powerful library for numerical calculations with support for arrays and matrices.math: A module that provides mathematical functions and constants.SymPy: A library for symbolic mathematics, allowing for algebraic operations and equation solving.
Types of Numeric Variables in Python
In Python, numbers are classified into two main types: integers (int()) and floating-point numbers (float()). Integers are whole numbers without decimals, while floating-point numbers can have decimal places:
# integer
num1 = 5
# float
num2 = 3.14
Basic Arithmetic Operations
Basic calculations such as addition, subtraction, multiplication, and division can be done using the standard operators: +, -, *, and /.
a = 10
b = 5
addition = a + b
print(addition)
15
subtraction = a - b print(subtraction) 5
multiplication = a * b print(multiplication) 50
division = a / b print(division) 2.0
Fractions, Logarithms, Powers, and Square Roots
There are built-in functions and modules for handling fractions, logarithms, powers, and square roots. The "math" module offers functions like log(), pow(), and sqrt() to perform these operations. For example:
import math
# Natural logarithm of 10
natlog = math.log(10)
print(natlog)
2.302585092994046
# 2 raised to the power of 3
power = math.pow(2, 3)
print(power)
8
# Square root of 25
square_root = math.sqrt(25)
print(square_root)
5
Functions and Equations
Python can also solve mathematical functions and equations using modules like Numpy and SymPy. These modules have the tools for symbolic computation and numerical calculations.
To solve ecuations with Python, first we create math variables with the “symbols()” function. Then, for simple ecuations, the “solve()” function finds the value of the unknown variable
import numpy as np
import sympy as sp
from sympy import symbols, solve
# Declare the variable and define the ecuation
x = symbols('x')
ecua = 10 + 3 + x
# Solve the equation
solution = solve(ecua)
print(solution)
-13
The “solve()” function can also solve quadratic ecuations. In ecuations with two solutions, it returns a list.
from sympy import symbols, solve, eq
# Define the ecuation
x = symbols('x')
expression =x**2 + 10*x + 3
print(solve(expression,dict=True))
# Outcome
[{x: -5 - sqrt(22)}, {x: -5 + sqrt(22)}]
Trigonometric Functions with NumPy
The the trigonometric functions: cosine, sine, tangent, arcsine, arccosine, and arctangent are all included in the Numpy library. These functions are highly useful in fields like physics, engineering, and geometry:
import numpy as np
angle = np.pi/4
cosine = np.cos(angle)
sine = np.sin(angle)
tangent = np.tan(angle)
arcsine = np.arcsin(0.5)
arccosine = np.arccos(0.5)
arctangent = np.arctan(1)
Linear Algebra with Python
Linear algebra plays a fundamental role in many areas of mathematics, data science and computer science. Working with vectors and matrices is also possible with Python.
Vectors and Matrices
In Python, we can represent vectors and matrices using the Numpy module. To create vectors and matrices the "array()" function is necessary:
import numpy as np
# Create a vector
vector = np.array([1, 2, 3])
# Create a matrix
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix)
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Addition and Subtraction of Matrices
To add or subtract two matrices, we simply perform element-wise addition or subtraction. The matrices must have the same dimensions for these operations to be valid. Here’s an example:
import numpy as np
# Define two matrices
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
# Perform addition
addition = matrix1 + matrix2
## Outcome
print(addition)
array([[ 6, 8],
[10, 12]])
# Perform subtraction
subtraction = matrix1 - matrix2
## Outcome
print(subtraction)
array([[-4, -4],
[-4, -4]])
Multiplication and Division of Matrices
Matrix multiplication is an important operation in linear algebra. In Python, we can multiply matrices using the "dot()" function from the Numpy module. Division of matrices is not defined directly, but we can achieve it by multiplying a matrix by the inverse of another matrix.
import numpy as np
# Define two matrices
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
# Perform matrix multiplication
multiplication = np.dot(matrix1, matrix2)
## Outcome
print(multiplication)
array([[19, 22],
[43, 50]])
# Perform matrix division
division = np.dot(matrix1, np.linalg.inv(matrix2))
## Outcome
print(division)
array([[ 3., -2.],
[ 2., -1.]])
Conclusion
Python provides robust and efficient tools for performing various mathematical calculations and solving complex problems. Libraries like numpy, math, and sympy, turn Python into a suitable language for mathematical computation. Whether you are working on scientific research, data analysis, or engineering projects, Python’s math capabilities can greatly help you to improve productivity and effectiveness of projects and tasks.