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
- Constants
- Basic Functions
- Number Theory Core
- Powers, Roots and Logs
- Trigonometry and Hyperbolic Functions
- Aggregates and Combinatorics
- Special Functions
- Advanced Number Theory
- Statistics
- Probability
- Linear Algebra
- Geometry
- Numerical Calculus
- Unit Conversions
- Interpolation
- Pseudo-Random Utilities
- Base Conversion and Roman Numerals
- Series
- Activation Functions
- Metrics
- Polynomials
- Custom Data Types
- Educational Cryptography
- Graph
- Algorithms
- Usage and Summary
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 precisionprint(tms.PI)
Output:
3.141592653589793 -
TAU— Two times PIprint(tms.TAU)
Output:
6.283185307179586 -
E— Euler's numberprint(tms.E)
Output:
2.718281828459045 -
INF— Positive infinityprint(tms.INF)
Output:
inf -
NAN— Not-a-Number valueprint(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 NaNresult = tms.isnan(float("nan")) print(result)
Output:
True -
isinf(x)— Checks whether x is infiniteresult = tms.isinf(float("inf")) print(result)
Output:
True -
isfinite(x)— Checks whether x is finiteresult = tms.isfinite(3.5) print(result)
Output:
True -
copysign(x, y)— Returns x with the sign of y -
fabs(x)— Absolute valueresult = tms.fabs(-7.25) print(result)
Output:
7.25 -
trunc(x)— Truncates toward zeroresult = tms.trunc(3.9) print(result)
Output:
3 -
floor(x)— Floorresult = tms.floor(3.9) print(result)
Output:
3 -
ceil(x)— Ceilingresult = 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 partsresult = 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)— Factorialprint(tms.factorial(5))
Output:
120 -
gcd(*args)— Greatest common divisorprint(tms.gcd(12, 18))
Output:
6 -
lcm(*args)— Least common multipleprint(tms.lcm(4, 6))
Output:
12 -
isqrt(n)— Integer square rootprint(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 rootprint(round(tms.sqrt(9), 1))
Output:
3.0 -
cbrt(x)— Approximate cube rootprint(round(tms.cbrt(27), 1))
Output:
3.0 -
pow_(x, y)— General exponentiationprint(tms.pow_(2, 5))
Output:
32.0 -
exp(x)— Exponential functionprint(round(tms.exp(1), 6))
Output:
2.718282 -
expm1(x)— exp(x) - 1result = 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 baseprint(round(tms.log(tms.E), 6))
Output:
1.0 -
log2(x)— Base-2 logarithmresult = tms.log2(8) print(result)
Output:
3.0000000000000004 -
log10(x)— Base-10 logarithmresult = 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 degreesprint(tms.degrees(tms.PI))
Output:
180.0 -
radians(x)— Convert degrees to radiansprint(round(tms.radians(180), 6))
Output:
3.141593 -
sin(x)— Sineprint(round(tms.sin(tms.PI / 2), 6))
Output:
1.0 -
cos(x)— Cosineprint(round(tms.cos(0), 6))
Output:
1.0 -
tan(x)— Tangentprint(round(tms.tan(tms.PI / 4), 6))
Output:
1.0 -
sinh(x)— Hyperbolic sineresult = tms.sinh(1) print(result)
Output:
1.1752011936438007 -
cosh(x)— Hyperbolic cosineresult = tms.cosh(1) print(result)
Output:
1.543080634815243 -
tanh(x)— Hyperbolic tangentresult = tms.tanh(1) print(result)
Output:
0.7615941559557647 -
asinh(x)— Inverse hyperbolic sineresult = tms.asinh(1) print(result)
Output:
0.8813735870195429 -
acosh(x)— Inverse hyperbolic cosineresult = tms.acosh(2) print(result)
Output:
1.3169578969248168 -
atanh(x)— Inverse hyperbolic tangentresult = tms.atanh(0.5) print(result)
Output:
0.5493061443340549 -
asin(x)— Inverse sineresult = tms.asin(0.5) print(result)
Output:
0.5235987755982988 -
acos(x)— Inverse cosineresult = tms.acos(0.5) print(result)
Output:
1.0471975511965979 -
atan(x)— Inverse tangentresult = 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 summationresult = tms.fsum([1, 2, 3, 4]) print(result)
Output:
10.0 -
prod(iterable, start=1)— Productresult = tms.prod([2, 3, 4], 2) print(result)
Output:
48 -
isclose(a, b, rel_tol, abs_tol)— Approximate comparisonresult = tms.isclose(1.0, 1.0000001, 1e-5, 1e-8) print(result)
Output:
True -
comb(n, k)— Combinationsprint(tms.comb(5, 2))
Output:
10 -
perm(n, k=None)— Permutationsprint(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-gammaresult = tms.lgamma(3) print(result)
Output:
0.6931471805599463 -
gamma(x)— Gamma functionresult = tms.gamma(4) print(result)
Output:
6.0 -
erf(x)— Error functionresult = tms.erf(1) print(result)
Output:
0.84270068974759 -
erfc(x)— Complementary error functionresult = tms.erfc(1) print(result)
Output:
0.15729931025240995 -
frexp(x)— Mantissa and exponentresult = 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 placeresult = tms.ulp(1.0) print(result)
Output:
2.220446049250313e-16 -
signbit(x)— Sign bitresult = 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 testprint(tms.is_prime(97))
Output:
True -
next_prime(n)— Next primeresult = tms.next_prime(29) print(result)
Output:
31 -
prev_prime(n)— Previous primeresult = tms.prev_prime(29) print(result)
Output:
23 -
prime_factors(n)— Prime factor listresult = tms.prime_factors(84) print(result)
Output:
[2, 2, 3, 7] -
prime_factorization(n)— Prime factor dictionaryresult = tms.prime_factorization(84) print(result)
Output:
{2: 2, 3: 1, 7: 1} -
divisors(n)— Divisorsresult = tms.divisors(12) print(result)
Output:
[1, 2, 3, 4, 6, 12] -
divisor_count(n)— Number of divisorsresult = tms.divisor_count(12) print(result)
Output:
6 -
divisor_sum(n)— Sum of divisorsresult = tms.divisor_sum(12) print(result)
Output:
28 -
is_perfect(n)— Perfect number testresult = tms.is_perfect(28) print(result)
Output:
True -
is_abundant(n)— Abundant number testresult = tms.is_abundant(12) print(result)
Output:
True -
is_deficient(n)— Deficient number testresult = tms.is_deficient(8) print(result)
Output:
True -
euler_totient(n)— Euler's totient functionresult = tms.euler_totient(10) print(result)
Output:
4 -
mobius(n)— Mobius functionresult = tms.mobius(30) print(result)
Output:
-1 -
fibonacci(n)— Fibonacci numberprint(tms.fibonacci(10))
Output:
55 -
lucas(n)— Lucas numberresult = tms.lucas(10) print(result)
Output:
123 -
tribonacci(n)— Tribonacci numberresult = tms.tribonacci(10) print(result)
Output:
274 -
catalan(n)— Catalan numberresult = tms.catalan(5) print(result)
Output:
42 -
bernoulli(n)— Bernoulli numberresult = tms.bernoulli(4) print(result)
Output:
-0.03333333333333399 -
harmonic(n)— Harmonic numberresult = tms.harmonic(5) print(result)
Output:
2.283333333333333 -
digit_sum(n)— Sum of digitsresult = tms.digit_sum(12345) print(result)
Output:
15 -
digit_product(n)— Product of digitsresult = tms.digit_product(1234) print(result)
Output:
24 -
digital_root(n)— Digital rootresult = tms.digital_root(9876) print(result)
Output:
3 -
is_palindrome_number(n)— Palindrome number testresult = tms.is_palindrome_number(12321) print(result)
Output:
True -
is_armstrong(n)— Armstrong number testresult = tms.is_armstrong(153) print(result)
Output:
True -
collatz_length(n)— Collatz sequence lengthresult = tms.collatz_length(13) print(result)
Output:
9 -
is_power_of_two(n)— Power-of-two testresult = tms.is_power_of_two(16) print(result)
Output:
True -
bit_length(n)— Binary lengthresult = tms.bit_length(255) print(result)
Output:
8 -
popcount(n)— Number of set bitsresult = 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 Eratosthenesresult = 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 limitresult = tms.count_primes_below(30) print(result)
Output:
10 -
goldbach_pair(n)— Goldbach pairresult = tms.goldbach_pair(28) print(result)
Output:
(5, 23) -
is_amicable_pair(a, b)— Amicable pair test -
find_amicable_pairs(limit)— Find amicable pairsresult = tms.find_amicable_pairs(1000) print(result)
Output:
[(220, 284)] -
polygonal_number(s, n)— General polygonal number -
triangular_number(n)— Triangular numberresult = tms.triangular_number(7) print(result)
Output:
28 -
square_number(n)— Square numberresult = tms.square_number(7) print(result)
Output:
49 -
pentagonal_number(n)— Pentagonal numberresult = tms.pentagonal_number(4) print(result)
Output:
22 -
hexagonal_number(n)— Hexagonal numberresult = tms.hexagonal_number(4) print(result)
Output:
28 -
heptagonal_number(n)— Heptagonal numberresult = tms.heptagonal_number(4) print(result)
Output:
34 -
octagonal_number(n)— Octagonal numberresult = tms.octagonal_number(4) print(result)
Output:
40 -
is_triangular(n)— Triangular testresult = tms.is_triangular(15) print(result)
Output:
True -
is_square(n)— Square testresult = tms.is_square(16) print(result)
Output:
True -
is_pentagonal(n)— Pentagonal testresult = tms.is_pentagonal(12) print(result)
Output:
True -
pascal_triangle_row(n)— Row of Pascal's triangleresult = tms.pascal_triangle_row(5) print(result)
Output:
[1, 5, 10, 10, 5, 1] -
pascal_triangle(rows)— Pascal's triangleresult = 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 squaresresult = tms.sum_of_squares(5) print(result)
Output:
55 -
sum_of_cubes(n)— Sum of cubesresult = tms.sum_of_cubes(5) print(result)
Output:
225 -
is_perfect_square_fast(n)— Fast perfect-square testresult = 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 meanprint(tms.mean([1, 2, 3, 4]))
Output:
2.5 -
geometric_mean(data)— Geometric meanresult = tms.geometric_mean([1, 2, 4, 8]) print(result)
Output:
2.8284271247461876 -
harmonic_mean(data)— Harmonic meanresult = tms.harmonic_mean([1, 2, 4]) print(result)
Output:
1.7142857142857142 -
median(data)— Medianprint(tms.median([1, 3, 2]))
Output:
2.0 -
median_low(data)— Lower medianresult = tms.median_low([1, 3, 2, 4]) print(result)
Output:
2 -
median_high(data)— Upper medianresult = tms.median_high([1, 3, 2, 4]) print(result)
Output:
3 -
mode(data)— Modeprint(tms.mode([1, 2, 2, 3]))
Output:
2 -
multimode(data)— Multiple modesresult = tms.multimode([1, 1, 2, 2, 3]) print(result)
Output:
[1, 2] -
variance(data, xbar=None)— Sample varianceprint(round(tms.variance([1, 2, 3, 4]), 6))
Output:
1.666667 -
pvariance(data, mu=None)— Population varianceresult = tms.pvariance([1, 2, 3, 4]) print(result)
Output:
1.25 -
stdev(data, xbar=None)— Sample standard deviationprint(round(tms.stdev([1, 2, 3, 4]), 6))
Output:
1.290994 -
pstdev(data, mu=None)— Population standard deviationresult = 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)— Quantilesresult = 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)— Rangeresult = tms.range_([1, 9, 4, 2]) print(result)
Output:
8 -
midrange(data)— Midrangeresult = tms.midrange([1, 9, 4, 2]) print(result)
Output:
5.0 -
skewness(data)— Skewnessresult = tms.skewness([1, 2, 3, 4, 5]) print(result)
Output:
0.0 -
kurtosis(data)— Kurtosisresult = 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 PDFresult = tms.normal_pdf(0) print(result)
Output:
0.39894228040143265 -
normal_cdf(x, mu=0.0, sigma=1.0)— Normal CDFresult = 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 classv = tms.Vector([3, 4])\nprint(v.norm())
Output:
5.0 -
Matrix(rows)— Matrix classm = tms.Matrix([[1, 2], [3, 4]])\nprint(m.determinant())
Output:
-2 -
Complex(real, imag=0.0)— Complex number classz = 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 matrixresult = 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 xresult = 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 yresult = 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 zresult = 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 arearesult = tms.circle_area(3) print(result)
Output:
28.274333882308138 -
circle_circumference(r)— Circle circumferenceresult = tms.circle_circumference(3) print(result)
Output:
18.84955592153876 -
sphere_volume(r)— Sphere volumeresult = tms.sphere_volume(3) print(result)
Output:
113.09733552923254 -
sphere_surface_area(r)— Sphere surface arearesult = 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 formularesult = tms.polygon_area_shoelace([(0, 0), (4, 0), (4, 3)]) print(result)
Output:
6.0 -
centroid(points)— Centroidresult = 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^xresult = 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 Fahrenheitresult = tms.convert_celsius_to_fahrenheit(0) print(result)
Output:
32.0 -
convert_fahrenheit_to_celsius(f)— Fahrenheit to Celsiusresult = tms.convert_fahrenheit_to_celsius(32) print(result)
Output:
0.0 -
convert_celsius_to_kelvin(c)— Celsius to Kelvinresult = tms.convert_celsius_to_kelvin(0) print(result)
Output:
273.15 -
convert_kelvin_to_celsius(k)— Kelvin to Celsiusresult = tms.convert_kelvin_to_celsius(273.15) print(result)
Output:
0.0 -
convert_km_to_miles(km)— Kilometers to milesresult = tms.convert_km_to_miles(1) print(result)
Output:
0.621371 -
convert_miles_to_km(miles)— Miles to kilometersresult = tms.convert_miles_to_km(1) print(result)
Output:
1.6093444978925633 -
convert_kg_to_pounds(kg)— Kilograms to poundsresult = tms.convert_kg_to_pounds(1) print(result)
Output:
2.20462 -
convert_pounds_to_kg(lb)— Pounds to kilogramsresult = 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 fractionresult = 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 fractionresult = 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 stepresult = tms.lcg_random(2, 2, 2, 2) print(result)
Output:
0 -
lcg_sequence(seed, n, a=1103515245, c=12345, m=2 ** 31)— LCG sequenceresult = 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 baseresult = 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 numeralresult = 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 sumresult = tms.arithmetic_series_sum(2, 2, 2) print(result)
Output:
6.0 -
geometric_series_sum(a1, r, n)— Finite geometric series sumresult = 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 interpolationresult = tms.lerp(2, 2, 2) print(result)
Output:
2 -
clamp(x, lo, hi)— Clamp valueresult = tms.clamp(2, 2, 2) print(result)
Output:
2 -
smoothstep(edge0, edge1, x)— Smoothstep -
sign(x)— Signresult = tms.sign(2) print(result)
Output:
1 -
relu(x)— ReLUresult = tms.relu(2) print(result)
Output:
2 -
sigmoid(x)— Sigmoidresult = tms.sigmoid(2) print(result)
Output:
0.8807970779778825 -
softplus(x)— Softplusresult = tms.softplus(2) print(result)
Output:
2.1269280110429714 -
tanh_derivative(x)— Derivative of tanhresult = tms.tanh_derivative(2) print(result)
Output:
0.07065082485316443 -
sigmoid_derivative(x)— Derivative of sigmoidresult = 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 polynomialprint(tms.poly_eval([1, 0, -1], 3))
Output:
8.0 -
poly_add(a, b)— Add polynomialsprint(tms.poly_add([1, 2], [3, 4]))
Output:
[4, 6] -
poly_sub(a, b)— Subtract polynomials -
poly_mul(a, b)— Multiply polynomialsprint(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 rootsresult = 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 Tresult = tms.poly_chebyshev_t(2) print(result)
Output:
[2.0, 0.0, -1.0] -
poly_legendre(n)— Legendre polynomialresult = tms.poly_legendre(2) print(result)
Output:
[1.5, 0.0, -0.5] -
poly_hermite(n)— Hermite polynomialresult = tms.poly_hermite(2) print(result)
Output:
[4.0, 0.0, -2.0] -
poly_laguerre(n)— Laguerre polynomialresult = 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 fractionf = tms.Fraction(6, 8)\nprint(f)
Output:
3/4 -
FixedDecimal(value=0, precision=2)— Fixed-precision decimald = tms.FixedDecimal(3.14, 2)\nprint(float(d))
Output:
3.14 -
Interval(lo, hi)— Numeric intervaliv = tms.Interval(1, 5)\nprint(3 in iv)
Output:
True -
Ratio(a, b)— Simplified ratior = 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 cipherprint(tms.caesar_encrypt("ABC", 3))
Output:
DEF -
caesar_decrypt(text, shift)— Caesar decryptionprint(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)— ROT13print(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 testresult = tms.is_prime_miller_rabin(2, 2) print(result)
Output:
True -
rsa_generate_keys(p, q)— Generate RSA keyspub, priv = tms.rsa_generate_keys(61, 53)\nprint(pub[1])
Output:
3233 -
rsa_encrypt(message, public_key)— RSA encryptionpub, priv = tms.rsa_generate_keys(61, 53)\nprint(tms.rsa_encrypt(42, pub))
Output:
2557 -
rsa_decrypt(ciphertext, private_key)— RSA decryptionpub, 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 secretresult = 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 checksumresult = tms.checksum_sum([1, 2, 3, 4]) print(result)
Output:
10 -
checksum_xor(data)— XOR checksumresult = tms.checksum_xor([1, 2, 3, 4]) print(result)
Output:
4 -
luhn_check(number)— Luhn checkresult = tms.luhn_check(2) print(result)
Output:
False -
luhn_generate(partial)— Generate Luhn check digitresult = 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 classg = 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 graphresult = tms.graph_complete(2) print(result)
Output:
<taha_math_shiraz.graph.Graph object at 0x0000022C4A32C550> -
graph_cycle(n)— Cycle graphresult = tms.graph_cycle(2) print(result)
Output:
<taha_math_shiraz.graph.Graph object at 0x0000022C4A32C2D0> -
graph_path(n)— Path graphresult = tms.graph_path(2) print(result)
Output:
<taha_math_shiraz.graph.Graph object at 0x0000022C49EAAFD0> -
graph_star(n)— Star graphresult = 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 sortprint(tms.bubble_sort([5, 3, 1]))
Output:
[1, 3, 5] -
selection_sort(arr)— Selection sortresult = tms.selection_sort([3, 1, 2]) print(result)
Output:
[1, 2, 3] -
insertion_sort(arr)— Insertion sortresult = tms.insertion_sort([3, 1, 2]) print(result)
Output:
[1, 2, 3] -
merge_sort(arr)— Merge sortresult = tms.merge_sort([3, 1, 2]) print(result)
Output:
[1, 2, 3] -
quick_sort(arr)— Quick sortresult = tms.quick_sort([3, 1, 2]) print(result)
Output:
[1, 2, 3] -
heap_sort(arr)— Heap sortresult = tms.heap_sort([3, 1, 2]) print(result)
Output:
[1, 2, 3] -
counting_sort(arr, max_val=None)— Counting sortresult = tms.counting_sort([3, 1, 2]) print(result)
Output:
[1, 2, 3] -
radix_sort(arr)— Radix sortresult = tms.radix_sort([3, 1, 2]) print(result)
Output:
[1, 2, 3] -
shell_sort(arr)— Shell sortresult = tms.shell_sort([3, 1, 2]) print(result)
Output:
[1, 2, 3] -
bucket_sort(arr, num_buckets=10)— Bucket sortresult = tms.bucket_sort([0.42, 0.32, 0.23]) print(result)
Output:
[0.23, 0.32, 0.42] -
binary_search(arr, target)— Binary searchprint(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 distanceprint(tms.levenshtein_distance("kitten", "sitting"))
Output:
3 -
longest_common_subsequence(s1, s2)— LCSprint(tms.longest_common_subsequence("ABCBDAB", "BDCAB"))
Output:
4 -
longest_common_substring(s1, s2)— Longest common substring -
knapsack_01(weights, values, capacity)— 0/1 knapsackprint(tms.knapsack_01([1, 3, 4], [1, 4, 5], 4))
Output:
5 -
coin_change(coins, amount)— Minimum coin changeprint(tms.coin_change([1, 5, 10], 12))
Output:
3 -
longest_increasing_subsequence(arr)— LISresult = tms.longest_increasing_subsequence([10, 9, 2, 5, 3, 7, 101, 18]) print(result)
Output:
4 -
max_subarray(arr)— Maximum subarray sumresult = tms.max_subarray([1, 2, 3, 4]) print(result)
Output:
10 -
max_subarray_indices(arr)— Max subarray indicesresult = tms.max_subarray_indices([1, 2, 3, 4]) print(result)
Output:
(0, 3, 10) -
two_sum(arr, target)— Two-sumresult = tms.two_sum([1, 2, 3, 4], 2) print(result)
Output:
None -
three_sum(arr, target)— Three-sumresult = tms.three_sum([1, 2, 3, 4], 2) print(result)
Output:
[] -
flatten(nested)— Flatten nested structureresult = tms.flatten(2) print(result)
Output:
[2] -
rotate_list(arr, k)— Rotate listresult = tms.rotate_list([1, 2, 3, 4], 2) print(result)
Output:
[3, 4, 1, 2] -
chunk_list(arr, size)— Chunk listresult = tms.chunk_list([1, 2, 3, 4], 2) print(result)
Output:
[[1, 2], [3, 4]] -
sliding_window_max(arr, k)— Sliding window maxresult = tms.sliding_window_max([1, 2, 3, 4], 2) print(result)
Output:
[2, 3, 4] -
sliding_window_sum(arr, k)— Sliding window sumresult = tms.sliding_window_sum([1, 2, 3, 4], 2) print(result)
Output:
[3, 5, 7] -
running_average(arr)— Running averageresult = tms.running_average([1, 2, 3, 4]) print(result)
Output:
[1.0, 1.5, 2.0, 2.5] -
cumulative_sum(arr)— Cumulative sumresult = tms.cumulative_sum([1, 2, 3, 4]) print(result)
Output:
[1, 3, 6, 10] -
cumulative_product(arr)— Cumulative productresult = tms.cumulative_product([1, 2, 3, 4]) print(result)
Output:
[1, 2, 6, 24] -
moving_average(arr, window)— Moving averageresult = 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 arrayresult = 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)
| File | Size | Uploaded | |
|---|---|---|---|
| taha_math_shiraz-1.1.3.tar.gz | 106.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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
|