Examples
Practical code snippets for common tasks. Each example is self-contained.
Basic Math Operations
Calculator
from mathfunctionize import mathfunctionize as mf
# All arithmetic functions take a list of numbers
print(mf.addition([1, 2, 3])) # 6
print(mf.subtraction([10, 3, 2])) # 5
print(mf.multiplication([2, 3, 4])) # 24
print(mf.division([100, 5, 2])) # 10.0
print(mf.flatDivision([10, 3])) # 3
print(mf.modulo([10, 3])) # 1
print(mf.power([2, 3])) # 8
# Works with more than two elements
print(mf.addition([1, 2, 3, 4, 5])) # 15
print(mf.power([2, 3, 2])) # 64
Roots and Absolute Values
from mathfunctionize import mathfunctionize as mf
print(mf.squareRoot(256)) # 16.0
print(mf.cubeRoot(125)) # ~5.0
print(mf.nthRoot(1296, 4)) # 6.0
print(mf.absolute(-99.5)) # 99.5
print(mf.factorial(10)) # 3628800
print(mf.gamma(5)) # 24
Trigonometry
Computing Trig Values
from mathfunctionize import mathfunctionize as mf
# Convert 30 degrees to radians, then compute trig functions
angle = mf.degreeToRadian(30)
print(f"sin(30°) = {mf.sin(angle)}") # ~0.5
print(f"cos(30°) = {mf.cos(angle)}") # ~0.866
print(f"tan(30°) = {mf.tan(angle)}") # ~0.577
# Reciprocal trig functions
print(f"csc(30°) = {mf.csc(angle)}") # ~2.0
print(f"sec(30°) = {mf.sec(angle)}") # ~1.155
print(f"cot(30°) = {mf.cot(angle)}") # ~1.732
Inverse Trig and Conversions
from mathfunctionize import mathfunctionize as mf
# Inverse trig
print(mf.arcsine(0.5)) # ~0.5236 (≈ π/6)
print(mf.arctangent(1)) # ~0.7854 (≈ π/4)
# Degree / radian conversions
print(mf.degreeToRadian(360)) # ~6.2832
print(mf.radianToDegree(mf.pi)) # 180.0
# Handles angles outside [0, 360]
print(mf.degreeToRadian(450)) # same as 90° → ~1.5708
print(mf.degreeToRadian(-90)) # same as 270° → ~4.7124
Counting and Combinatorics
Permutations and Combinations
from mathfunctionize import mathfunctionize as mf
# How many ways to arrange 3 items from 5?
print(mf.permutations(5, 3)) # 60.0
# How many ways to choose 3 items from 5 (order doesn't matter)?
print(mf.combinations(5, 3)) # 10.0
# Circular permutations: seating 6 people around a table
print(mf.circularPermutations(6)) # 120
# Derangements: no item in its original position
print(mf.derangements(5)) # 44
Statistics
Descriptive Statistics
from mathfunctionize import mathfunctionize as mf
scores = [72, 85, 90, 88, 76, 95, 82, 91, 78, 84]
print(f"Mean: {mf.mean(scores)}")
print(f"Median: {mf.median(scores)}")
print(f"Mode: {mf.mode(scores)}")
print(f"Std Dev: {mf.standardDevation(scores)}")
print(f"Variance: {mf.variance(scores)}")
Probability
Bayes' Theorem: Disease Testing
from mathfunctionize import mathfunctionize as mf
# Disease prevalence: 1%
# Test sensitivity: 90% (true positive rate)
# Test false positive rate: 5%
posterior = mf.bayes_theorem(
priorA=0.01, # P(disease)
priorB=0.99, # P(no disease)
likelihoodA=0.9, # P(positive | disease)
likelihoodB=0.05 # P(positive | no disease)
)
print(f"P(disease | positive test) = {posterior:.4f}")
# ~0.1538 — only a 15% chance despite a positive test
Normal Distribution
from mathfunctionize import mathfunctionize as mf
# Standard normal (mean=0, stddev=1)
print(mf.normalPDF(0, 0, 1)) # 0.3989... (peak)
print(mf.normalPDF(1, 0, 1)) # 0.2420...
print(mf.normalCDF(0, 0, 1)) # 0.5 (50th percentile)
print(mf.normalCDF(1.96, 0, 1)) # ~0.975 (97.5th percentile)
# Custom distribution: IQ scores (mean=100, stddev=15)
print(mf.normalCDF(130, 100, 15)) # ~0.977
Uniform Distribution
from mathfunctionize import mathfunctionize as mf
# Uniform on [0, 10]
print(mf.uniformPDF(0, 10)) # 0.1
print(mf.uniformCDF(3, 0, 10)) # 0.3
print(mf.uniformCDF(7.5, 0, 10)) # 0.75
print(mf.uniformCDF(15, 0, 10)) # 1 (past upper bound)
Complex Numbers
Adding and Subtracting
from mathfunctionize import mathfunctionize as mf
# Complex numbers as strings
z1 = "3+4i"
z2 = "1-2i"
print(mf.complex_addition(z1, z2)) # "4.0+2.0i"
print(mf.complex_subtraction(z1, z2)) # "2.0+6.0i"
# Pure imaginary
print(mf.complex_addition("5i", "3i")) # "0.0+8.0i"
# Pure real
print(mf.complex_addition("7", "3")) # "10.0+0.0i"
Quantitative Analysis
Finding Extrema in Data
from mathfunctionize import mathfunctionize as mf
prices = [100, 95, 102, 88, 110, 105, 115, 90, 120]
# Local extrema (count and indices)
mins = mf.localMinimum(prices)
maxs = mf.localMaximum(prices)
print(f"Local minima: {mins}") # [count, [indices]]
print(f"Local maxima: {maxs}") # [count, [indices]]
# Global extrema (value and indices)
gmin = mf.globalMinimum(prices)
gmax = mf.globalMaximum(prices)
print(f"Global min: {gmin[0]} at index {gmin[1]}")
print(f"Global max: {gmax[0]} at index {gmax[1]}")
Linear Algebra
Matrix Arithmetic
from mathfunctionize import mathfunctionize as mf
A = [[1, 2, 3],
[4, 5, 6]]
B = [[7, 8, 9],
[10, 11, 12]]
print(mf.additionMatrix(A, B))
# [[8, 10, 12], [14, 16, 18]]
print(mf.subtractionMatrix(B, A))
# [[6, 6, 6], [6, 6, 6]]
Matrix Multiplication
from mathfunctionize import mathfunctionize as mf
A = [[1, 2],
[3, 4],
[5, 6]]
B = [[7, 8, 9],
[10, 11, 12]]
# 3×2 times 2×3 = 3×3
result = mf.multiplicationMatrix(A, B)
print(result)
# [[27, 30, 33], [61, 68, 75], [95, 106, 117]]
Determinants and Transposes
from mathfunctionize import mathfunctionize as mf
M = [[6, 1, 1],
[4, -2, 5],
[2, 8, 7]]
print(mf.determinant(M)) # -306
print(mf.transpose(M))
# [[6, 4, 2], [1, -2, 8], [1, 5, 7]]
# 1×1 determinant
print(mf.determinant([[42]])) # 42
Set Theory
Naive Set Operations
from mathfunctionize import mathfunctionize as mf
A = [1, 2, 3, 4]
B = [3, 4, 5, 6]
print(mf.union(A, B)) # [1, 2, 3, 4, 5, 6]
print(mf.intersection(A, B)) # [3, 4]
print(mf.difference(A, B)) # [1, 2]
print(mf.symmetricDifference(A, B)) # [1, 2, 5, 6]
print(mf.cartesianProduct([1, 2], [3, 4]))
# [[1, 3], [1, 4], [2, 3], [2, 4]]
print(mf.isSubset([1, 2], A)) # True
print(mf.isDisjoint([1, 2], [3, 4])) # True
print(mf.cardinality(A)) # 4
print(mf.powerSet([1, 2]))
# [[], [1], [2], [1, 2]]
ZFC Axioms
from mathfunctionize import mathfunctionize as mf
print(mf.extensionality([1, 2], [2, 1])) # True
print(mf.emptySet()) # []
print(mf.pairing(1, 2)) # [1, 2]
print(mf.axiomOfUnion([[1, 2], [2, 3], [4]])) # [1, 2, 3, 4]
print(mf.separation([1, 2, 3, 4, 5], lambda x: x > 3)) # [4, 5]
print(mf.replacement([1, 2, 3], lambda x: x * 2)) # [2, 4, 6]
print(mf.infinitySet(3)) # [[], [[]], [[], [[]]]]
print(mf.regularity([1, 2, 3])) # True
print(mf.axiomOfChoice([[1, 2], [3, 4], [5]])) # [1, 3, 5]
Metric Spaces
Distance Functions
from mathfunctionize import mathfunctionize as mf
p1 = [0, 0]
p2 = [3, 4]
print(mf.dist(p1, p2)) # 5.0 (euclidean)
print(mf.dist(p1, p2, "manhattan")) # 7
print(mf.dist(p1, p2, "chebyshev")) # 4
# Verify metric space axioms
d = lambda a, b: mf.dist(a, b)
S = [[0, 0], [1, 0], [0, 1]]
print(mf.isMetricSpace(d, S)) # True
Calculus
Limits, Derivatives, and Integrals
from mathfunctionize import mathfunctionize as mf
f = lambda x: x**2
# Limit: (x^2 - 1)/(x - 1) as x -> 1
g = lambda x: (x**2 - 1) / (x - 1)
print(mf.limit(g, "x", 1)) # 2.0
# Derivative of x^2 at x = 3
print(mf.derivative(f, 3)) # ~6.0
# Concavity of x^2 at origin
print(mf.concavity(f, 0)) # "concave up"
# Integral of x^2 from 0 to 1
print(mf.integral(f, 0, 1)) # ~0.3333...
# Continuity check
print(mf.continuity(f, 1)) # True
Complex Analysis
Conjugates and Roots of Unity
from mathfunctionize import mathfunctionize as mf
print(mf.conjugate("3+4i")) # "3-4i"
print(mf.conjugate("5-2i")) # "5+2i"
# 4th roots of unity
roots = mf.rootsOfUnity(4)
print(roots)
# ["1.0+0.0i", "0.0+1.0i", "-1.0+0.0i", "0.0-1.0i"]
Number Theory and Topology
Primality and Smoothness
from mathfunctionize import mathfunctionize as mf
# Primality testing
print(mf.isPrime(17)) # True
print(mf.isPrime(4)) # False
print(mf.isPrime(97)) # True
# Smoothness testing
print(mf.smooth(lambda x: x**3, 0)) # True
Polynomials
Evaluation, Roots, and Factoring
from mathfunctionize import mathfunctionize as mf
# Evaluate 2x^2 + 3x + 1 at x = 2
print(mf.polyEval([2, 3, 1], 2)) # 15
# Polynomial long division: (x^2 + 3x + 2) / (x + 1)
q, r = mf.divide([1, 3, 2], [1, 1])
print(f"Quotient: {q}, Remainder: {r}")
# Quotient: [1.0, 2.0], Remainder: []
# Find zeros of x^2 - 5x + 6
print(mf.zeros([1, -5, 6])) # [3.0, 2.0]
# Factor x^2 - 5x + 6
print(mf.factor([1, -5, 6]))
# [[[1, -3], 1], [[1, -2], 1]]