Skip to main content

taha_math_shiraz Documentation

This page is the main reference for the library. It explains each module with descriptions, examples, outputs, and practical notes, so it can be used both as a quick overview and as a detailed developer reference.

Note: This package is an educational/general-purpose toolkit for mathematics, numerical methods, graphs, simple cryptography, and helper utilities.

Table of Contents


Overview

The __init__.py file re-exports the public API so you can use the package directly from the root namespace.

import taha_math_shiraz as tms

print(tms.PI)
print(tms.sqrt(2))
print(tms.Graph())

Constants

File: constants.py

This section contains the core constants used across the package. They are the numeric foundation for trigonometric, logarithmic, and numerical routines.

  • PI — The constant pi with high precision

    print(tms.PI)
    

    Output: 3.141592653589793

  • TAU — Two times PI

    print(tms.TAU)
    

    Output: 6.283185307179586

  • E — Euler's number

    print(tms.E)
    

    Output: 2.718281828459045

  • INF — Positive infinity

    print(tms.INF)
    

    Output: inf

  • NAN — Not-a-Number value

    print(tms.NAN)
    

    Output: nan

  • SQRT2 — Square root of 2

  • LN2 — Natural logarithm of 2

  • LN10 — Natural logarithm of 10


Basic Functions

File: basics.py

These are lightweight, self-contained equivalents for basic numeric operations such as checking values, truncation, rounding, and floating-point remainder handling.

  • isnan(x) — Checks whether x is NaN

    result = tms.isnan(float("nan"))
    print(result)
    

    Output: True

  • isinf(x) — Checks whether x is infinite

    result = tms.isinf(float("inf"))
    print(result)
    

    Output: True

  • isfinite(x) — Checks whether x is finite

    result = tms.isfinite(3.5)
    print(result)
    

    Output: True

  • copysign(x, y) — Returns x with the sign of y

  • fabs(x) — Absolute value

    result = tms.fabs(-7.25)
    print(result)
    

    Output: 7.25

  • trunc(x) — Truncates toward zero

    result = tms.trunc(3.9)
    print(result)
    

    Output: 3

  • floor(x) — Floor

    result = tms.floor(3.9)
    print(result)
    

    Output: 3

  • ceil(x) — Ceiling

    result = tms.ceil(3.1)
    print(result)
    

    Output: 4

  • fmod(x, y) — Floating-point remainder

  • remainder(x, y) — Remainder based on rounding

  • modf(x) — Splits into fractional and integer parts

    result = tms.modf(3.75)
    print(result)
    

    Output: (0.75, 3.0)


Number Theory Core

File: numtheory_core.py

This module provides the minimal number-theory building blocks used by many other parts of the library, including factorial, gcd, lcm, and integer square root.

  • factorial(n) — Factorial

    print(tms.factorial(5))
    

    Output: 120

  • gcd(*args) — Greatest common divisor

    print(tms.gcd(12, 18))
    

    Output: 6

  • lcm(*args) — Least common multiple

    print(tms.lcm(4, 6))
    

    Output: 12

  • isqrt(n) — Integer square root

    print(tms.isqrt(17))
    

    Output: 4


Powers, Roots and Logs

File: power.py

This is the numerical core of the library. It provides square root, exponentiation, and logarithms without relying on Python’s math module.

  • sqrt(x) — Approximate square root

    print(round(tms.sqrt(9), 1))
    

    Output: 3.0

  • cbrt(x) — Approximate cube root

    print(round(tms.cbrt(27), 1))
    

    Output: 3.0

  • pow_(x, y) — General exponentiation

    print(tms.pow_(2, 5))
    

    Output: 32.0

  • exp(x) — Exponential function

    print(round(tms.exp(1), 6))
    

    Output: 2.718282

  • expm1(x) — exp(x) - 1

    result = tms.expm1(1)
    print(result)
    

    Output: 1.7182818284590438

  • log1p(x) — log(1+x)

    result = tms.log1p(1)
    print(result)
    

    Output: 0.6931471805599454

  • log(x, base=None) — Natural log or log with base

    print(round(tms.log(tms.E), 6))
    

    Output: 1.0

  • log2(x) — Base-2 logarithm

    result = tms.log2(8)
    print(result)
    

    Output: 3.0000000000000004

  • log10(x) — Base-10 logarithm

    result = tms.log10(1000)
    print(result)
    

    Output: 2.9999999999999996


Trigonometry and Hyperbolic Functions

File: trigonometry.py

All main trigonometric and hyperbolic functions live here. The implementations are based on manual approximations and series expansions to keep the package independent.

  • degrees(x) — Convert radians to degrees

    print(tms.degrees(tms.PI))
    

    Output: 180.0

  • radians(x) — Convert degrees to radians

    print(round(tms.radians(180), 6))
    

    Output: 3.141593

  • sin(x) — Sine

    print(round(tms.sin(tms.PI / 2), 6))
    

    Output: 1.0

  • cos(x) — Cosine

    print(round(tms.cos(0), 6))
    

    Output: 1.0

  • tan(x) — Tangent

    print(round(tms.tan(tms.PI / 4), 6))
    

    Output: 1.0

  • sinh(x) — Hyperbolic sine

    result = tms.sinh(1)
    print(result)
    

    Output: 1.1752011936438007

  • cosh(x) — Hyperbolic cosine

    result = tms.cosh(1)
    print(result)
    

    Output: 1.543080634815243

  • tanh(x) — Hyperbolic tangent

    result = tms.tanh(1)
    print(result)
    

    Output: 0.7615941559557647

  • asinh(x) — Inverse hyperbolic sine

    result = tms.asinh(1)
    print(result)
    

    Output: 0.8813735870195429

  • acosh(x) — Inverse hyperbolic cosine

    result = tms.acosh(2)
    print(result)
    

    Output: 1.3169578969248168

  • atanh(x) — Inverse hyperbolic tangent

    result = tms.atanh(0.5)
    print(result)
    

    Output: 0.5493061443340549

  • asin(x) — Inverse sine

    result = tms.asin(0.5)
    print(result)
    

    Output: 0.5235987755982988

  • acos(x) — Inverse cosine

    result = tms.acos(0.5)
    print(result)
    

    Output: 1.0471975511965979

  • atan(x) — Inverse tangent

    result = tms.atan(1)
    print(result)
    

    Output: 0.7853981633974483

  • atan2(y, x) — Angle with quadrant awareness

  • hypot(*coords) — Vector length

  • dist(p, q) — Euclidean distance


Aggregates and Combinatorics

File: aggregates.py

This module groups together summation, product, combination, and permutation helpers that are heavily reused by statistics, probability, and number theory.

  • fsum(iterable) — More accurate summation

    result = tms.fsum([1, 2, 3, 4])
    print(result)
    

    Output: 10.0

  • prod(iterable, start=1) — Product

    result = tms.prod([2, 3, 4], 2)
    print(result)
    

    Output: 48

  • isclose(a, b, rel_tol, abs_tol) — Approximate comparison

    result = tms.isclose(1.0, 1.0000001, 1e-5, 1e-8)
    print(result)
    

    Output: True

  • comb(n, k) — Combinations

    print(tms.comb(5, 2))
    

    Output: 10

  • perm(n, k=None) — Permutations

    print(tms.perm(5, 2))
    

    Output: 20


Special Functions

File: special.py

Special functions such as gamma, erf, and floating-point helpers are placed here. They are useful for advanced numerical work and floating-point analysis.

  • lgamma(x) — Log-gamma

    result = tms.lgamma(3)
    print(result)
    

    Output: 0.6931471805599463

  • gamma(x) — Gamma function

    result = tms.gamma(4)
    print(result)
    

    Output: 6.0

  • erf(x) — Error function

    result = tms.erf(1)
    print(result)
    

    Output: 0.84270068974759

  • erfc(x) — Complementary error function

    result = tms.erfc(1)
    print(result)
    

    Output: 0.15729931025240995

  • frexp(x) — Mantissa and exponent

    result = tms.frexp(8)
    print(result)
    

    Output: (0.5, 4)

  • ldexp(m, e) — Combine mantissa and exponent

  • nextafter(x, y) — Next representable float toward y

  • ulp(x) — Unit in the last place

    result = tms.ulp(1.0)
    print(result)
    

    Output: 2.220446049250313e-16

  • signbit(x) — Sign bit

    result = tms.signbit(-2)
    print(result)
    

    Output: True


Advanced Number Theory

File: numbertheory.py

This is the largest number-theory toolkit in the package. It includes primality, factorization, modular arithmetic, sequences, polygonal numbers, and CRT utilities.

  • is_prime(n) — Prime test

    print(tms.is_prime(97))
    

    Output: True

  • next_prime(n) — Next prime

    result = tms.next_prime(29)
    print(result)
    

    Output: 31

  • prev_prime(n) — Previous prime

    result = tms.prev_prime(29)
    print(result)
    

    Output: 23

  • prime_factors(n) — Prime factor list

    result = tms.prime_factors(84)
    print(result)
    

    Output: [2, 2, 3, 7]

  • prime_factorization(n) — Prime factor dictionary

    result = tms.prime_factorization(84)
    print(result)
    

    Output: {2: 2, 3: 1, 7: 1}

  • divisors(n) — Divisors

    result = tms.divisors(12)
    print(result)
    

    Output: [1, 2, 3, 4, 6, 12]

  • divisor_count(n) — Number of divisors

    result = tms.divisor_count(12)
    print(result)
    

    Output: 6

  • divisor_sum(n) — Sum of divisors

    result = tms.divisor_sum(12)
    print(result)
    

    Output: 28

  • is_perfect(n) — Perfect number test

    result = tms.is_perfect(28)
    print(result)
    

    Output: True

  • is_abundant(n) — Abundant number test

    result = tms.is_abundant(12)
    print(result)
    

    Output: True

  • is_deficient(n) — Deficient number test

    result = tms.is_deficient(8)
    print(result)
    

    Output: True

  • euler_totient(n) — Euler's totient function

    result = tms.euler_totient(10)
    print(result)
    

    Output: 4

  • mobius(n) — Mobius function

    result = tms.mobius(30)
    print(result)
    

    Output: -1

  • fibonacci(n) — Fibonacci number

    print(tms.fibonacci(10))
    

    Output: 55

  • lucas(n) — Lucas number

    result = tms.lucas(10)
    print(result)
    

    Output: 123

  • tribonacci(n) — Tribonacci number

    result = tms.tribonacci(10)
    print(result)
    

    Output: 274

  • catalan(n) — Catalan number

    result = tms.catalan(5)
    print(result)
    

    Output: 42

  • bernoulli(n) — Bernoulli number

    result = tms.bernoulli(4)
    print(result)
    

    Output: -0.03333333333333399

  • harmonic(n) — Harmonic number

    result = tms.harmonic(5)
    print(result)
    

    Output: 2.283333333333333

  • digit_sum(n) — Sum of digits

    result = tms.digit_sum(12345)
    print(result)
    

    Output: 15

  • digit_product(n) — Product of digits

    result = tms.digit_product(1234)
    print(result)
    

    Output: 24

  • digital_root(n) — Digital root

    result = tms.digital_root(9876)
    print(result)
    

    Output: 3

  • is_palindrome_number(n) — Palindrome number test

    result = tms.is_palindrome_number(12321)
    print(result)
    

    Output: True

  • is_armstrong(n) — Armstrong number test

    result = tms.is_armstrong(153)
    print(result)
    

    Output: True

  • collatz_length(n) — Collatz sequence length

    result = tms.collatz_length(13)
    print(result)
    

    Output: 9

  • is_power_of_two(n) — Power-of-two test

    result = tms.is_power_of_two(16)
    print(result)
    

    Output: True

  • bit_length(n) — Binary length

    result = tms.bit_length(255)
    print(result)
    

    Output: 8

  • popcount(n) — Number of set bits

    result = tms.popcount(255)
    print(result)
    

    Output: 8

  • extended_gcd(a, b) — Extended Euclidean algorithm

  • mod_inverse(a, m) — Modular inverse

  • mod_pow(base, exp, mod) — Fast modular exponentiation

  • chinese_remainder(remainders, moduli) — Chinese remainder theorem

  • is_coprime(a, b) — Coprime test

  • jacobi_symbol(a, n) — Jacobi symbol

  • sieve_of_eratosthenes(limit) — Sieve of Eratosthenes

    result = tms.sieve_of_eratosthenes(30)
    print(result)
    

    Output: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

  • count_primes_below(limit) — Count primes below limit

    result = tms.count_primes_below(30)
    print(result)
    

    Output: 10

  • goldbach_pair(n) — Goldbach pair

    result = tms.goldbach_pair(28)
    print(result)
    

    Output: (5, 23)

  • is_amicable_pair(a, b) — Amicable pair test

  • find_amicable_pairs(limit) — Find amicable pairs

    result = tms.find_amicable_pairs(1000)
    print(result)
    

    Output: [(220, 284)]

  • polygonal_number(s, n) — General polygonal number

  • triangular_number(n) — Triangular number

    result = tms.triangular_number(7)
    print(result)
    

    Output: 28

  • square_number(n) — Square number

    result = tms.square_number(7)
    print(result)
    

    Output: 49

  • pentagonal_number(n) — Pentagonal number

    result = tms.pentagonal_number(4)
    print(result)
    

    Output: 22

  • hexagonal_number(n) — Hexagonal number

    result = tms.hexagonal_number(4)
    print(result)
    

    Output: 28

  • heptagonal_number(n) — Heptagonal number

    result = tms.heptagonal_number(4)
    print(result)
    

    Output: 34

  • octagonal_number(n) — Octagonal number

    result = tms.octagonal_number(4)
    print(result)
    

    Output: 40

  • is_triangular(n) — Triangular test

    result = tms.is_triangular(15)
    print(result)
    

    Output: True

  • is_square(n) — Square test

    result = tms.is_square(16)
    print(result)
    

    Output: True

  • is_pentagonal(n) — Pentagonal test

    result = tms.is_pentagonal(12)
    print(result)
    

    Output: True

  • pascal_triangle_row(n) — Row of Pascal's triangle

    result = tms.pascal_triangle_row(5)
    print(result)
    

    Output: [1, 5, 10, 10, 5, 1]

  • pascal_triangle(rows) — Pascal's triangle

    result = tms.pascal_triangle(5)
    print(result)
    

    Output: [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]

  • sum_of_squares(n) — Sum of squares

    result = tms.sum_of_squares(5)
    print(result)
    

    Output: 55

  • sum_of_cubes(n) — Sum of cubes

    result = tms.sum_of_cubes(5)
    print(result)
    

    Output: 225

  • is_perfect_square_fast(n) — Fast perfect-square test

    result = tms.is_perfect_square_fast(144)
    print(result)
    

    Output: True


Statistics

File: statistics_mod.py

This module covers descriptive and light inferential statistics, including averages, spread measures, correlation, and linear regression.

  • mean(data) — Arithmetic mean

    print(tms.mean([1, 2, 3, 4]))
    

    Output: 2.5

  • geometric_mean(data) — Geometric mean

    result = tms.geometric_mean([1, 2, 4, 8])
    print(result)
    

    Output: 2.8284271247461876

  • harmonic_mean(data) — Harmonic mean

    result = tms.harmonic_mean([1, 2, 4])
    print(result)
    

    Output: 1.7142857142857142

  • median(data) — Median

    print(tms.median([1, 3, 2]))
    

    Output: 2.0

  • median_low(data) — Lower median

    result = tms.median_low([1, 3, 2, 4])
    print(result)
    

    Output: 2

  • median_high(data) — Upper median

    result = tms.median_high([1, 3, 2, 4])
    print(result)
    

    Output: 3

  • mode(data) — Mode

    print(tms.mode([1, 2, 2, 3]))
    

    Output: 2

  • multimode(data) — Multiple modes

    result = tms.multimode([1, 1, 2, 2, 3])
    print(result)
    

    Output: [1, 2]

  • variance(data, xbar=None) — Sample variance

    print(round(tms.variance([1, 2, 3, 4]), 6))
    

    Output: 1.666667

  • pvariance(data, mu=None) — Population variance

    result = tms.pvariance([1, 2, 3, 4])
    print(result)
    

    Output: 1.25

  • stdev(data, xbar=None) — Sample standard deviation

    print(round(tms.stdev([1, 2, 3, 4]), 6))
    

    Output: 1.290994

  • pstdev(data, mu=None) — Population standard deviation

    result = tms.pstdev([1, 2, 3, 4])
    print(result)
    

    Output: 1.118033988749895

  • covariance(x, y) — Covariance

  • correlation(x, y) — Correlation

  • linear_regression(x, y) — Linear regression

  • quantiles(data, n=4) — Quantiles

    result = tms.quantiles([1, 2, 3, 4, 5, 6, 7, 8])
    print(result)
    

    Output: [2.75, 4.5, 6.25]

  • z_score(x, data) — Z-score

  • range_(data) — Range

    result = tms.range_([1, 9, 4, 2])
    print(result)
    

    Output: 8

  • midrange(data) — Midrange

    result = tms.midrange([1, 9, 4, 2])
    print(result)
    

    Output: 5.0

  • skewness(data) — Skewness

    result = tms.skewness([1, 2, 3, 4, 5])
    print(result)
    

    Output: 0.0

  • kurtosis(data) — Kurtosis

    result = tms.kurtosis([1, 2, 3, 4, 5])
    print(result)
    

    Output: -1.2999999999999996


Probability

File: probability.py

Basic distributions and probability helpers are provided here for educational examples and simple stochastic calculations.

  • binomial_probability(n, k, p) — Binomial probability

  • poisson_probability(lam, k) — Poisson probability

  • normal_pdf(x, mu=0.0, sigma=1.0) — Normal PDF

    result = tms.normal_pdf(0)
    print(result)
    

    Output: 0.39894228040143265

  • normal_cdf(x, mu=0.0, sigma=1.0) — Normal CDF

    result = tms.normal_cdf(0)
    print(result)
    

    Output: 0.5000000005

  • exponential_pdf(x, lam) — Exponential PDF

  • exponential_cdf(x, lam) — Exponential CDF

  • uniform_pdf(x, a, b) — Uniform PDF

  • uniform_cdf(x, a, b) — Uniform CDF

  • geometric_pdf(k, p) — Geometric probability

  • binomial_mean(n, p) — Binomial mean

  • binomial_variance(n, p) — Binomial variance


Linear Algebra

File: linalg.py

Vectors, matrices, and complex numbers are implemented here together with several geometric and linear-algebra helpers.

  • Vector(components) — Vector class

    v = tms.Vector([3, 4])\nprint(v.norm())
    

    Output: 5.0

  • Matrix(rows) — Matrix class

    m = tms.Matrix([[1, 2], [3, 4]])\nprint(m.determinant())
    

    Output: -2

  • Complex(real, imag=0.0) — Complex number class

    z = tms.Complex(3, 4)\nprint(z.modulus())
    

    Output: 5.0

  • vector_projection(a, b) — Vector projection

  • vector_reflection(v, normal) — Vector reflection

  • matrix_power(m, n) — Matrix power

  • matrix_from_function(rows, cols, f) — Matrix from function

  • rotation_matrix_2d(theta) — 2D rotation matrix

    result = tms.rotation_matrix_2d(tms.PI / 2)
    print(result)
    

    Output: Matrix([[4.5907218229751464e-17, -1.0000000000000002], [1.0000000000000002, 4.5907218229751464e-17]])

  • rotation_matrix_x(theta) — Rotation about x

    result = tms.rotation_matrix_x(tms.PI / 2)
    print(result)
    

    Output: Matrix([[1, 0, 0], [0, 4.5907218229751464e-17, -1.0000000000000002], [0, 1.0000000000000002, 4.5907218229751464e-17]])

  • rotation_matrix_y(theta) — Rotation about y

    result = tms.rotation_matrix_y(tms.PI / 2)
    print(result)
    

    Output: Matrix([[4.5907218229751464e-17, 0, 1.0000000000000002], [0, 1, 0], [-1.0000000000000002, 0, 4.5907218229751464e-17]])

  • rotation_matrix_z(theta) — Rotation about z

    result = tms.rotation_matrix_z(tms.PI / 2)
    print(result)
    

    Output: Matrix([[4.5907218229751464e-17, -1.0000000000000002, 0], [1.0000000000000002, 4.5907218229751464e-17, 0], [0, 0, 1]])

  • scaling_matrix(sx, sy) — Scaling matrix

  • translation_apply(point, dx, dy) — Apply translation


Geometry

File: geometry.py

Classic geometry formulas, distances, areas, volumes, and relations like the law of sines/cosines are included in this section.

  • distance_2d(p1, p2) — Distance in 2D

  • distance_3d(p1, p2) — Distance in 3D

  • triangle_area(a, b, c) — Triangle area from sides

  • triangle_area_coords(p1, p2, p3) — Triangle area from coordinates

  • circle_area(r) — Circle area

    result = tms.circle_area(3)
    print(result)
    

    Output: 28.274333882308138

  • circle_circumference(r) — Circle circumference

    result = tms.circle_circumference(3)
    print(result)
    

    Output: 18.84955592153876

  • sphere_volume(r) — Sphere volume

    result = tms.sphere_volume(3)
    print(result)
    

    Output: 113.09733552923254

  • sphere_surface_area(r) — Sphere surface area

    result = tms.sphere_surface_area(3)
    print(result)
    

    Output: 113.09733552923255

  • cylinder_volume(r, h) — Cylinder volume

  • cylinder_surface_area(r, h) — Cylinder surface area

  • cone_volume(r, h) — Cone volume

  • cone_surface_area(r, h) — Cone surface area

  • regular_polygon_area(n, side) — Regular polygon area

  • regular_polygon_perimeter(n, side) — Regular polygon perimeter

  • law_of_cosines(a, b, gamma_angle) — Law of cosines

  • law_of_sines_ratio(a, angle_a) — Law of sines ratio

  • heron_perimeter(a, b, c) — Sum of triangle sides

  • polygon_area_shoelace(points) — Shoelace formula

    result = tms.polygon_area_shoelace([(0, 0), (4, 0), (4, 3)])
    print(result)
    

    Output: 6.0

  • centroid(points) — Centroid

    result = tms.centroid([(0, 0), (4, 0), (4, 3)])
    print(result)
    

    Output: (2.6666666666666665, 1.0)

  • is_collinear(p1, p2, p3) — Collinearity test

  • midpoint(p1, p2) — Midpoint

  • slope(p1, p2) — Slope

  • line_intersection(p1, p2, p3, p4) — Line intersection

  • point_in_circle(p, center, r) — Point in circle

  • point_in_triangle(p, a, b, c) — Point in triangle


Numerical Calculus

File: calculus.py

This module provides numerical differentiation, integration, and root-finding methods for approximate calculus work.

  • derivative(f, x, h=1e-6) — Numerical derivative

  • second_derivative(f, x, h=1e-4) — Second numerical derivative

  • integral_trapezoid(f, a, b, n=1000) — Trapezoidal integration

  • integral_simpson(f, a, b, n=1000) — Simpson integration

  • newton_raphson(f, fprime, x0, tol, max_iter) — Newton-Raphson root finding

  • bisection_method(f, a, b, tol, max_iter) — Bisection method

  • taylor_series_exp(x, terms=20) — Taylor approximation of e^x

    result = tms.taylor_series_exp(1)
    print(result)
    

    Output: 2.7182818284590455

  • secant_method(f, x0, x1, tol, max_iter) — Secant method

  • golden_section_search(f, a, b, tol) — Golden-section search


Unit Conversions

File: conversions.py

Common unit conversions for temperature, distance, mass, and angle are grouped here.

  • convert_celsius_to_fahrenheit(c) — Celsius to Fahrenheit

    result = tms.convert_celsius_to_fahrenheit(0)
    print(result)
    

    Output: 32.0

  • convert_fahrenheit_to_celsius(f) — Fahrenheit to Celsius

    result = tms.convert_fahrenheit_to_celsius(32)
    print(result)
    

    Output: 0.0

  • convert_celsius_to_kelvin(c) — Celsius to Kelvin

    result = tms.convert_celsius_to_kelvin(0)
    print(result)
    

    Output: 273.15

  • convert_kelvin_to_celsius(k) — Kelvin to Celsius

    result = tms.convert_kelvin_to_celsius(273.15)
    print(result)
    

    Output: 0.0

  • convert_km_to_miles(km) — Kilometers to miles

    result = tms.convert_km_to_miles(1)
    print(result)
    

    Output: 0.621371

  • convert_miles_to_km(miles) — Miles to kilometers

    result = tms.convert_miles_to_km(1)
    print(result)
    

    Output: 1.6093444978925633

  • convert_kg_to_pounds(kg) — Kilograms to pounds

    result = tms.convert_kg_to_pounds(1)
    print(result)
    

    Output: 2.20462

  • convert_pounds_to_kg(lb) — Pounds to kilograms

    result = tms.convert_pounds_to_kg(1)
    print(result)
    

    Output: 0.45359290943563974

  • convert_degrees_to_radians(d) — Degrees to radians

  • convert_radians_to_degrees(r) — Radians to degrees


Interpolation

File: interpolation.py

Lagrange interpolation, Newton divided differences, continued fractions, and related number transformations are included here.

  • lagrange_interpolation(points, x) — Lagrange interpolation

  • newton_divided_difference(points, x) — Newton divided differences

  • linear_interpolation(x0, y0, x1, y1, x) — Linear interpolation

  • bilinear_interpolation(...) — Bilinear interpolation

  • continued_fraction(x, depth=15) — Continued fraction

    result = tms.continued_fraction(3.245)
    print(result)
    

    Output: [3, 4, 12, 3, 1, 247777268231, 4, 1, 2, 1, 2, 1, 34, 1, 3]

  • convergent_from_continued_fraction(terms) — Convergent from continued fraction

    result = tms.convergent_from_continued_fraction([1, 2, 2])
    print(result)
    

    Output: (7, 5)

  • egyptian_fraction(numerator, denominator) — Egyptian fraction


Pseudo-Random Utilities

File: randomutil.py

A simple linear congruential generator and sequence helper are provided in this module.

  • lcg_random(seed, a=1103515245, c=12345, m=2 ** 31) — One LCG step

    result = tms.lcg_random(2, 2, 2, 2)
    print(result)
    

    Output: 0

  • lcg_sequence(seed, n, a=1103515245, c=12345, m=2 ** 31) — LCG sequence

    result = tms.lcg_sequence(2, 2, 2, 2, 2)
    print(result)
    

    Output: [0, 0]


Base Conversion and Roman Numerals

File: baseconv.py

This module handles conversions between numeric bases and Roman numeral formatting/parsing.

  • to_base(n, base) — Convert integer to another base

    result = tms.to_base(2, 2)
    print(result)
    

    Output: '10'

  • from_base(s, base) — Convert from another base

  • roman_to_int(s) — Roman numeral to integer

  • int_to_roman(n) — Integer to Roman numeral

    result = tms.int_to_roman(2)
    print(result)
    

    Output: 'II'


Series

File: series.py

Arithmetic and geometric series formulas, including the convergent infinite geometric form, live here.

  • arithmetic_series_sum(a1, d, n) — Arithmetic series sum

    result = tms.arithmetic_series_sum(2, 2, 2)
    print(result)
    

    Output: 6.0

  • geometric_series_sum(a1, r, n) — Finite geometric series sum

    result = tms.geometric_series_sum(2, 2, 2)
    print(result)
    

    Output: 6.0

  • infinite_geometric_series_sum(a1, r) — Infinite geometric series sum


Activation Functions

File: activations.py

Useful activation and interpolation-style functions such as ReLU, sigmoid, and smoothstep are provided here.

  • lerp(a, b, t) — Linear interpolation

    result = tms.lerp(2, 2, 2)
    print(result)
    

    Output: 2

  • clamp(x, lo, hi) — Clamp value

    result = tms.clamp(2, 2, 2)
    print(result)
    

    Output: 2

  • smoothstep(edge0, edge1, x) — Smoothstep

  • sign(x) — Sign

    result = tms.sign(2)
    print(result)
    

    Output: 1

  • relu(x) — ReLU

    result = tms.relu(2)
    print(result)
    

    Output: 2

  • sigmoid(x) — Sigmoid

    result = tms.sigmoid(2)
    print(result)
    

    Output: 0.8807970779778825

  • softplus(x) — Softplus

    result = tms.softplus(2)
    print(result)
    

    Output: 2.1269280110429714

  • tanh_derivative(x) — Derivative of tanh

    result = tms.tanh_derivative(2)
    print(result)
    

    Output: 0.07065082485316443

  • sigmoid_derivative(x) — Derivative of sigmoid

    result = tms.sigmoid_derivative(2)
    print(result)
    

    Output: 0.10499358540350645


Metrics

File: metrics.py

Distance metrics and cosine similarity helpers are grouped in this module.

  • euclidean_norm(v) — Euclidean norm

  • manhattan_distance(p1, p2) — Manhattan distance

  • chebyshev_distance(p1, p2) — Chebyshev distance

  • minkowski_distance(p1, p2, p) — Minkowski distance

  • cosine_similarity(a, b) — Cosine similarity


Polynomials

File: polynomial.py

This is a major module for polynomial arithmetic, root finding, named orthogonal polynomials, and symbolic-style manipulations.

  • poly_eval(coeffs, x) — Evaluate polynomial

    print(tms.poly_eval([1, 0, -1], 3))
    

    Output: 8.0

  • poly_add(a, b) — Add polynomials

    print(tms.poly_add([1, 2], [3, 4]))
    

    Output: [4, 6]

  • poly_sub(a, b) — Subtract polynomials

  • poly_mul(a, b) — Multiply polynomials

    print(tms.poly_mul([1, 1], [1, -1]))
    

    Output: [1, 0, -1]

  • poly_scale(a, s) — Scale polynomial

  • poly_neg(a) — Negate polynomial

  • poly_divmod(a, b) — Polynomial long division

  • poly_div(a, b) — Quotient

  • poly_mod(a, b) — Remainder

  • poly_gcd(a, b) — Polynomial gcd

  • poly_differentiate(coeffs) — Derivative

  • poly_integrate(coeffs, c=0.0) — Indefinite integral

  • poly_roots_quadratic(a, b, c) — Quadratic roots

  • poly_roots_cubic(a, b, c, d) — Cubic roots

    result = tms.poly_roots_cubic(2, 2, 2, 2)
    print(result)
    

    Output: (-0.9999999999999998, (-5.551115123125783e-17, 1.0), (-5.551115123125783e-17, -1.0))

  • poly_companion_matrix(coeffs) — Companion matrix

  • poly_compose(f, g) — Polynomial composition

  • poly_from_roots(roots) — Build from roots

  • poly_degree(coeffs) — Polynomial degree

  • poly_strip(coeffs, tol=1e-12) — Strip leading zeros

  • poly_integral_definite(coeffs, a, b) — Definite integral

  • poly_to_str(coeffs, var='x') — String form

  • poly_lagrange(points) — Lagrange polynomial

  • poly_chebyshev_t(n) — Chebyshev T

    result = tms.poly_chebyshev_t(2)
    print(result)
    

    Output: [2.0, 0.0, -1.0]

  • poly_legendre(n) — Legendre polynomial

    result = tms.poly_legendre(2)
    print(result)
    

    Output: [1.5, 0.0, -0.5]

  • poly_hermite(n) — Hermite polynomial

    result = tms.poly_hermite(2)
    print(result)
    

    Output: [4.0, 0.0, -2.0]

  • poly_laguerre(n) — Laguerre polynomial

    result = tms.poly_laguerre(2)
    print(result)
    

    Output: [1.5, 0.0, 0.0]


Custom Data Types

File: datatypes.py

Custom numeric types such as Fraction, FixedDecimal, Interval, and Ratio are implemented here.

  • Fraction(numerator=0, denominator=1) — Rational fraction

    f = tms.Fraction(6, 8)\nprint(f)
    

    Output: 3/4

  • FixedDecimal(value=0, precision=2) — Fixed-precision decimal

    d = tms.FixedDecimal(3.14, 2)\nprint(float(d))
    

    Output: 3.14

  • Interval(lo, hi) — Numeric interval

    iv = tms.Interval(1, 5)\nprint(3 in iv)
    

    Output: True

  • Ratio(a, b) — Simplified ratio

    r = tms.Ratio(4, 6)\nprint(r.as_float())
    

    Output: 0.6666666666666666


Educational Cryptography

File: crypto.py

Educational cryptography helpers, checksums, hashes, Luhn routines, and classic ciphers like Caesar and RSA are included here.

  • caesar_encrypt(text, shift) — Caesar cipher

    print(tms.caesar_encrypt("ABC", 3))
    

    Output: DEF

  • caesar_decrypt(text, shift) — Caesar decryption

    print(tms.caesar_decrypt("DEF", 3))
    

    Output: ABC

  • caesar_bruteforce(text) — Try every shift

  • vigenere_encrypt(text, key) — Vigenere cipher

  • vigenere_decrypt(text, key) — Vigenere decryption

  • atbash_encrypt(text) — Atbash cipher

  • atbash_decrypt(text) — Atbash decryption

  • rot13(text) — ROT13

    print(tms.rot13("Hello"))
    

    Output: Uryyb

  • affine_encrypt(text, a, b) — Affine cipher

  • affine_decrypt(text, a, b) — Affine decryption

  • rail_fence_encrypt(text, rails) — Rail fence cipher

  • rail_fence_decrypt(text, rails) — Rail fence decryption

  • columnar_encrypt(text, key) — Columnar transposition

  • simple_substitution_encrypt(text, alphabet_map) — Simple substitution

  • simple_substitution_decrypt(text, alphabet_map) — Simple substitution decryption

  • is_prime_miller_rabin(n, witnesses=None) — Miller-Rabin primality test

    result = tms.is_prime_miller_rabin(2, 2)
    print(result)
    

    Output: True

  • rsa_generate_keys(p, q) — Generate RSA keys

    pub, priv = tms.rsa_generate_keys(61, 53)\nprint(pub[1])
    

    Output: 3233

  • rsa_encrypt(message, public_key) — RSA encryption

    pub, priv = tms.rsa_generate_keys(61, 53)\nprint(tms.rsa_encrypt(42, pub))
    

    Output: 2557

  • rsa_decrypt(ciphertext, private_key) — RSA decryption

    pub, priv = tms.rsa_generate_keys(61, 53)\nct = tms.rsa_encrypt(42, pub)\nprint(tms.rsa_decrypt(ct, priv))
    

    Output: 42

  • diffie_hellman_keypair(p, g, private_key) — Diffie-Hellman public key

  • diffie_hellman_shared_secret(public_other, private_self, p) — Shared secret

    result = tms.diffie_hellman_shared_secret(2, 2, 2)
    print(result)
    

    Output: 0

  • xor_cipher(data, key) — XOR cipher

  • text_to_bits(text) — Text to bits

  • bits_to_text(bits) — Bits to text

  • hamming_distance(s1, s2) — Hamming distance

  • checksum_sum(data) — Sum checksum

    result = tms.checksum_sum([1, 2, 3, 4])
    print(result)
    

    Output: 10

  • checksum_xor(data) — XOR checksum

    result = tms.checksum_xor([1, 2, 3, 4])
    print(result)
    

    Output: 4

  • luhn_check(number) — Luhn check

    result = tms.luhn_check(2)
    print(result)
    

    Output: False

  • luhn_generate(partial) — Generate Luhn check digit

    result = tms.luhn_generate(2)
    print(result)
    

    Output: 26

  • mod_hash(text, m=997) — Simple modular hash

  • rolling_hash(text, window, base=31, mod=10**9+7) — Rolling hash

  • string_similarity_coefficient(s1, s2) — String similarity coefficient


Graph

File: graph.py

The graph module provides adjacency-list graph representation, traversals, shortest paths, spanning trees, and ready-made graph constructors.

  • Graph(directed=False) — Graph class

    g = tms.Graph()\ng.add_edge("A", "B")\nprint(g.bfs("A"))
    

    Output: A

  • add_vertex(v) — Add a vertex

  • add_edge(u, v, weight=1) — Add an edge

  • remove_edge(u, v) — Remove an edge

  • remove_vertex(v) — Remove a vertex

  • neighbors(v) — Neighbors

  • weight(u, v) — Edge weight

  • vertices() — Vertex list

  • edges() — Edge list

  • degree(v) — Vertex degree

  • in_degree(v) — In-degree

  • out_degree(v) — Out-degree

  • has_edge(u, v) — Edge existence

  • bfs(start) — Breadth-first search

  • dfs(start) — Depth-first search

  • has_path(start, end) — Path existence

  • dijkstra(start) — Dijkstra

  • shortest_path(start, end) — Shortest path

  • bellman_ford(start) — Bellman-Ford

  • floyd_warshall() — Floyd-Warshall

  • is_connected() — Connected test

  • topological_sort() — Topological sort

  • minimum_spanning_tree_kruskal() — Minimum spanning tree

  • has_cycle_undirected() — Undirected cycle test

  • is_bipartite() — Bipartite test

  • connected_components() — Connected components

  • density() — Graph density

  • vertex_count() — Vertex count

  • edge_count() — Edge count

  • adjacency_matrix() — Adjacency matrix

  • degree_sequence() — Degree sequence

  • eccentricity(v) — Eccentricity

  • diameter() — Diameter

  • radius() — Radius

  • center() — Center

  • graph_from_edge_list(edges, directed=False) — Build from edge list

  • graph_complete(n) — Complete graph

    result = tms.graph_complete(2)
    print(result)
    

    Output: <taha_math_shiraz.graph.Graph object at 0x0000022C4A32C550>

  • graph_cycle(n) — Cycle graph

    result = tms.graph_cycle(2)
    print(result)
    

    Output: <taha_math_shiraz.graph.Graph object at 0x0000022C4A32C2D0>

  • graph_path(n) — Path graph

    result = tms.graph_path(2)
    print(result)
    

    Output: <taha_math_shiraz.graph.Graph object at 0x0000022C49EAAFD0>

  • graph_star(n) — Star graph

    result = tms.graph_star(2)
    print(result)
    

    Output: <taha_math_shiraz.graph.Graph object at 0x0000022C49EAB6F0>

  • traveling_salesman_brute(graph) — Brute-force TSP


Algorithms

File: algorithms.py

Sorting, searching, dynamic programming, and array/string algorithms are collected in this module.

  • bubble_sort(arr) — Bubble sort

    print(tms.bubble_sort([5, 3, 1]))
    

    Output: [1, 3, 5]

  • selection_sort(arr) — Selection sort

    result = tms.selection_sort([3, 1, 2])
    print(result)
    

    Output: [1, 2, 3]

  • insertion_sort(arr) — Insertion sort

    result = tms.insertion_sort([3, 1, 2])
    print(result)
    

    Output: [1, 2, 3]

  • merge_sort(arr) — Merge sort

    result = tms.merge_sort([3, 1, 2])
    print(result)
    

    Output: [1, 2, 3]

  • quick_sort(arr) — Quick sort

    result = tms.quick_sort([3, 1, 2])
    print(result)
    

    Output: [1, 2, 3]

  • heap_sort(arr) — Heap sort

    result = tms.heap_sort([3, 1, 2])
    print(result)
    

    Output: [1, 2, 3]

  • counting_sort(arr, max_val=None) — Counting sort

    result = tms.counting_sort([3, 1, 2])
    print(result)
    

    Output: [1, 2, 3]

  • radix_sort(arr) — Radix sort

    result = tms.radix_sort([3, 1, 2])
    print(result)
    

    Output: [1, 2, 3]

  • shell_sort(arr) — Shell sort

    result = tms.shell_sort([3, 1, 2])
    print(result)
    

    Output: [1, 2, 3]

  • bucket_sort(arr, num_buckets=10) — Bucket sort

    result = tms.bucket_sort([0.42, 0.32, 0.23])
    print(result)
    

    Output: [0.23, 0.32, 0.42]

  • binary_search(arr, target) — Binary search

    print(tms.binary_search([1, 3, 5, 7], 5))
    

    Output: 2

  • binary_search_leftmost(arr, target) — Leftmost binary search

  • binary_search_rightmost(arr, target) — Rightmost binary search

  • interpolation_search(arr, target) — Interpolation search

  • exponential_search(arr, target) — Exponential search

  • ternary_search(f, lo, hi, tol) — Ternary search

  • levenshtein_distance(s1, s2) — Levenshtein distance

    print(tms.levenshtein_distance("kitten", "sitting"))
    

    Output: 3

  • longest_common_subsequence(s1, s2) — LCS

    print(tms.longest_common_subsequence("ABCBDAB", "BDCAB"))
    

    Output: 4

  • longest_common_substring(s1, s2) — Longest common substring

  • knapsack_01(weights, values, capacity) — 0/1 knapsack

    print(tms.knapsack_01([1, 3, 4], [1, 4, 5], 4))
    

    Output: 5

  • coin_change(coins, amount) — Minimum coin change

    print(tms.coin_change([1, 5, 10], 12))
    

    Output: 3

  • longest_increasing_subsequence(arr) — LIS

    result = tms.longest_increasing_subsequence([10, 9, 2, 5, 3, 7, 101, 18])
    print(result)
    

    Output: 4

  • max_subarray(arr) — Maximum subarray sum

    result = tms.max_subarray([1, 2, 3, 4])
    print(result)
    

    Output: 10

  • max_subarray_indices(arr) — Max subarray indices

    result = tms.max_subarray_indices([1, 2, 3, 4])
    print(result)
    

    Output: (0, 3, 10)

  • two_sum(arr, target) — Two-sum

    result = tms.two_sum([1, 2, 3, 4], 2)
    print(result)
    

    Output: None

  • three_sum(arr, target) — Three-sum

    result = tms.three_sum([1, 2, 3, 4], 2)
    print(result)
    

    Output: []

  • flatten(nested) — Flatten nested structure

    result = tms.flatten(2)
    print(result)
    

    Output: [2]

  • rotate_list(arr, k) — Rotate list

    result = tms.rotate_list([1, 2, 3, 4], 2)
    print(result)
    

    Output: [3, 4, 1, 2]

  • chunk_list(arr, size) — Chunk list

    result = tms.chunk_list([1, 2, 3, 4], 2)
    print(result)
    

    Output: [[1, 2], [3, 4]]

  • sliding_window_max(arr, k) — Sliding window max

    result = tms.sliding_window_max([1, 2, 3, 4], 2)
    print(result)
    

    Output: [2, 3, 4]

  • sliding_window_sum(arr, k) — Sliding window sum

    result = tms.sliding_window_sum([1, 2, 3, 4], 2)
    print(result)
    

    Output: [3, 5, 7]

  • running_average(arr) — Running average

    result = tms.running_average([1, 2, 3, 4])
    print(result)
    

    Output: [1.0, 1.5, 2.0, 2.5]

  • cumulative_sum(arr) — Cumulative sum

    result = tms.cumulative_sum([1, 2, 3, 4])
    print(result)
    

    Output: [1, 3, 6, 10]

  • cumulative_product(arr) — Cumulative product

    result = tms.cumulative_product([1, 2, 3, 4])
    print(result)
    

    Output: [1, 2, 6, 24]

  • moving_average(arr, window) — Moving average

    result = tms.moving_average([1, 2, 3, 4], 2)
    print(result)
    

    Output: [1.5, 2.5, 3.5]

  • zip_with(f, a, b) — Apply function pairwise

  • difference_array(arr) — First difference array

    result = tms.difference_array([1, 2, 3, 4])
    print(result)
    

    Output: [1, 1, 1]

  • second_difference_array(arr) — Second difference array


Usage and Summary

Because everything is re-exported in __init__.py, the easiest way to use the package is:

import taha_math_shiraz as tms

print(tms.PI)
print(tms.sin(tms.PI / 2))
print(tms.mean([1, 2, 3, 4]))

You can also import specific modules directly when you only need one part of the library. That approach is useful when you want a clean namespace and only need a focused subsystem such as graphs, statistics, or cryptography.

Metadata

Release files for taha-math-shiraz 1.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for taha-math-shiraz 1.1.3
File Size Uploaded
taha_math_shiraz-1.1.3.tar.gz 106.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for taha-math-shiraz 1.1.3
File Interpreter ABI Platform
taha_math_shiraz-1.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 151.7 kB

Release files / taha_math_shiraz-1.1.3.tar.gz

Download URL taha_math_shiraz-1.1.3.tar.gz
Size 106.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5a67679e5f9cc44f6fbe1189fb1200c6bada8e0991ff03707cbc02e431f49339
BLAKE2b-256 checksum
How to use checksums
1a113bd198e6a627ede25160f16373a6e253aef48c1427c066b7288cea11aea7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / taha_math_shiraz-1.1.3-py3-none-any.whl

Download URL taha_math_shiraz-1.1.3-py3-none-any.whl
Size 45.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ec2510a8f5d2b089ad5866f77cc6752157f94c7a3a537bcddae8987e4c5f83ff
BLAKE2b-256 checksum
How to use checksums
785ae4d3928c5049c6daec786914f223449b25e2410daa3347abe23b2ab14979
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release history Release notifications | RSS feed

This release

1.1.3 This release

2 release files

1.1.2

2 release files

1.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page