A Calustra - Eloy Coto Pereiro Home About Books

Mathematical Notation to Python Mapping

on

I did not attend college, so I learned a lot of things on my own. I'm good at Math, but because I learned math using programming languages, the Math language is always something I need to read ten times to understand. This is a map model for me. Nowadays I'm reading a lot of math papers, and I wanted to have a 1:1 map with raw Python.

1. Arithmetic & Basic Operators

Symbol Meaning Python Equivalent
\(+\) Addition
a + b
\(-\) Subtraction
a - b
\(\times\) or \(\cdot\) Multiplication
a * b
\(\div\) or \(/\) Division
a / b
\(//\) Floor Division
a // b
\(\bmod\) or \(\%\) Modulus (Remainder)
a % b
\(a^b\) Exponentiation
a ** b
\(\sqrt{x}\) Square root
math.sqrt(x)
\(\lfloor x \rfloor\) Floor function
math.floor(x)
\(\lceil x \rceil\) Ceiling function
math.ceil(x)

2. Algebraic Symbols

Symbol Meaning Python Equivalent
\(=\) Equal
a == b
\(\neq\) Not equal
a != b
\(<\) Less than
a < b
\(>\) Greater than
a > b
\(\leq\) Less than or equal
a <= b
\(\geq\) Greater than or equal
a >= b

3. Summation & Products

Symbol Meaning Python Equivalent
\(\sum_{i=1}^n a_i\) Summation
sum([1,2,3])
\(\prod_{i=1}^n a_i\) Product
math.prod([1,2,3])
\(\prod_{i=1}^n a_i \text{ where } n < 10\) Product with condition
def conditional_product(sequence):
    if len(sequence) >= 10:
        return "n must be less than 10"

    return math.prod(sequence)

conditional_product([1, 2, 3, 4])

4. Derivatives & Integrals

Symbol Meaning Python Equivalent
\(\frac{d}{dx}f(x)\) First derivative
def first_derivative(vals, step_size=1):
    derivatives = []
    for i in range(len(vals) - 1):
        derivatives.append((vals[i+1] - vals[i]) / step_size)
    return derivatives

val = [0,4,5,3,2,5,3,2]
print(first_derivative(val))
\(\frac{d^n}{dx^n}f(x)\) Higher-order derivative
def nth_derivative(vals, n, step_size=1):
    if n == 0:
        return vals
    elif n == 1:
        return first_derivative(vals, step_size)
    else:
        return first_derivative(nth_derivative(vals, n-1, step_size), step_size)

val = [0,4,5,3,2,5,3,2]
print(nth_derivative(val, 2))
\(\int f(x)dx\) Indefinite integral
def indefinite_integral(vals, step_size=1, const=0):
    integral = [const]
    for i in range(len(vals)):
        integral.append(integral[-1] + vals[i] * step_size)
    return integral[1:]

val = [0,4,5,3,2,5,3,2]
print(indefinite_integral(val))
\(\int_{a}^{b} f(x)dx\) Definite integral
def definite_integral(vals, a=0, b=None, step_size=1):
    if b is None:
        b = len(vals) - 1

    if a < 0 or b >= len(vals) or a > b:
        raise ValueError("Integration bounds out of range")

    result = 0
    for i in range(a, b):
        result += (vals[i] + vals[i+1]) / 2 * step_size
    return result

 val = [0,4,5,3,2,5,3,2]
 print(definite_integral(val, 2, 6))

5. Limits

Symbol Meaning Python Equivalent
\(\lim_{x \to a} f(x)\) Limit of a function
def limit(f, a, epsilon=1e-6):
    return (f(a + epsilon) + f(a - epsilon)) / 2

def f(x):
    return (x**2 - 1) / (x - 1) if x != 1 else None

print(limit(f, 1))

6. Trigonometry & Logarithms

Symbol Meaning Python Equivalent
\(\sin x\) Sine
math.sin(x)
\(\cos x\) Cosine
math.cos(x)
\(\tan x\) Tangent
math.tan(x)
\(\arcsin x\) Inverse sine
math.asin(x)
\(\arccos x\) Inverse cosine
math.acos(x)
\(\arctan x\) Inverse tangent
math.atan(x)
\(\log x\) Natural logarithm
math.log(x)
\(\log_b x\) Logarithm base b
math.log(x, b)
\(e^x\) Exponential function
math.exp(x)

7. Linear Algebra Symbols

Symbol Meaning Python Equivalent
\(A^T\) Matrix transpose
# Example
A = [[1, 2, 3], [4, 5, 6]]
print(transpose(A))
\(A^{-1}\) Matrix inverse
def matrix_inverse(matrix):
    n = len(matrix)
    augmented = [row[:] + [1 if i == j else 0 for j in range(n)] for i, row in enumerate(matrix)]

    for i in range(n):
        pivot = augmented[i][i]
        if pivot == 0:
            raise ValueError("Matrix is singular")

        for j in range(i, 2*n):
            augmented[i][j] /= pivot

        for k in range(n):
            if k != i:
                factor = augmented[k][i]
                for j in range(i, 2*n):
                    augmented[k][j] -= factor * augmented[i][j]

    inverse = [row[n:] for row in augmented]
    return inverse

A = [[4, 7], [2, 6]]
print(matrix_inverse(A))
print(numpy.linalg.inv(A))
\(\det(A)\) Determinant
def determinant(matrix):
    n = len(matrix)
    if n == 1:
        return matrix[0][0]
    if n == 2:
        return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]

    det = 0
    for c in range(n):
        submatrix = []
        for i in range(1, n):
            row = []
            for j in range(n):
                if j != c:
                    row.append(matrix[i][j])
            submatrix.append(row)

        det += ((-1) ** c) * matrix[0][c] * determinant(submatrix)

    return det

A = [[1, 2], [3, 4]]
print(determinant(A))
print(numpy.linalg.det(A))
\(A \cdot B\) Matrix multiplication
def matrix_multiply(A, B):
    n, m = len(A), len(A[0])
    p = len(B[0])

    if m != len(B):
        raise ValueError("Matrix dimensions do not match for multiplication")

    result = [[0 for _ in range(p)] for _ in range(n)]

    for i in range(n):
        for j in range(p):
            for k in range(m):
                result[i][j] += A[i][k] * B[k][j]

    return result

A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
print(matrix_multiply(A, B))
\(\lambda\) Eigenvalue
def power_method(matrix, iterations=100, tolerance=1e-10):
    n = len(matrix)
    vector = [1] * n

    magnitude = (sum(x**2 for x in vector)) ** 0.5
    vector = [x / magnitude for x in vector]

    eigenvalue = 0
    for _ in range(iterations):
        new_vector = [0] * n
        for i in range(n):
            for j in range(n):
                new_vector[i] += matrix[i][j] * vector[j]

        new_eigenvalue = sum(new_vector[i] * vector[i] for i in range(n))

        magnitude = (sum(x**2 for x in new_vector)) ** 0.5
        new_vector = [x / magnitude for x in new_vector]

        if abs(new_eigenvalue - eigenvalue) < tolerance:
            break

        vector = new_vector
        eigenvalue = new_eigenvalue

    return eigenvalue, vector

A = [[2, 1], [1, 3]]
eigenvalue, eigenvector = power_method(A)
print(f"Dominant eigenvalue: {eigenvalue}")
print(f"Corresponding eigenvector: {eigenvector}")
print(numpy.linalg.eig(A))

9. Logic & Set Theory Symbols

Symbol Meaning Python Equivalent
\(\forall\) "For all"
for x in [1,2,3]:
\(\exists\) "There exists"
any([...])
\(\in\) "Element of"
x in [1,2,3]
\(\notin\) "Not element of"
x not in [1,2,3]
\(\cap\) Intersection
A & B
\(\cup\) Union
A | B
\(\subset\) Subset
A.issubset(B)

Tags

Related articles: