arithmetic_functions¶
shortfx.fxNumeric.arithmetic_functions
¶
Arithmetic operations module.
This module provides fundamental arithmetic operations including logarithms, exponentiation, root calculations, factorials, combinatorics, special mathematical functions, and numerical analysis (derivatives, integration, root finding, polynomial evaluation).
Functions¶
absolute_value(x: Union[int, float]) -> Union[int, float]
¶
Returns the absolute (non-negative) value of a number.
Description
Computes |x|, removing the sign of the number. Preserves the original type (int stays int, float stays float).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
The number. |
required |
Returns:
| Type | Description |
|---|---|
Union[int, float]
|
Union[int, float]: The absolute value of x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
Usage Example
absolute_value(-7) 7 absolute_value(-3.14) 3.14 absolute_value(0) 0
Cost: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
adaptive_simpson(f: Callable[[float], float], a: float, b: float, tol: float = 1e-10, max_depth: int = 50) -> float
¶
Adaptive Simpson's rule for numerical integration.
Recursively subdivides intervals where error is above tolerance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Function to integrate, f(x). |
required |
a
|
float
|
Lower bound. |
required |
b
|
float
|
Upper bound. |
required |
tol
|
float
|
Error tolerance. |
1e-10
|
max_depth
|
int
|
Maximum recursion depth. |
50
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate definite integral. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable. |
Example
import math round(adaptive_simpson(math.sin, 0, math.pi), 10) 2.0
Complexity: O(n) where n depends on function smoothness.
Source code in shortfx/fxNumeric/arithmetic_functions.py
3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 | |
additive_persistence(n: int) -> int
¶
Count iterations to reduce n to a single digit by summing its digits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of iterations. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Usage Example
additive_persistence(199) 3
Complexity: O(log(n) × iterations)
Source code in shortfx/fxNumeric/arithmetic_functions.py
adjugate_matrix(matrix: List[List[float]]) -> List[List[float]]
¶
Computes the adjugate (classical adjoint) of a square matrix.
adj(A)_ij = (-1)^(i+j) * M_ji where M_ji is the (j,i) minor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
List[List[float]]
|
Square matrix. |
required |
Returns:
| Type | Description |
|---|---|
List[List[float]]
|
Adjugate matrix. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of lists of numbers. |
ValueError
|
If matrix is not square or is empty. |
Example
adjugate_matrix([[1, 2], [3, 4]]) [[4.0, -2.0], [-3.0, 1.0]]
Complexity: O(n^2 * n!) — practical only for small matrices.
Source code in shortfx/fxNumeric/arithmetic_functions.py
4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 | |
bessel_i(x: float, n: int) -> float
¶
Modified Bessel function of the first kind, order n.
Description
Returns Iₙ(x). Equivalent to Excel BESSELI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value at which to evaluate. |
required |
n
|
int
|
Non-negative integer order. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Iₙ(x). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric or n is not int. |
ValueError
|
If n < 0. |
Example
round(bessel_i(1.5, 1), 6) 0.981666
Complexity: O(1) — delegates to scipy
Source code in shortfx/fxNumeric/arithmetic_functions.py
bessel_j(x: float, n: int) -> float
¶
Bessel function of the first kind, order n.
Description
Returns Jₙ(x). Equivalent to Excel BESSELJ.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Value at which to evaluate. |
required |
n
|
int
|
Non-negative integer order. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Jₙ(x). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric or n is not int. |
ValueError
|
If n < 0. |
Example
round(bessel_j(1.9, 2), 6) 0.329926
Complexity: O(1) — delegates to scipy
Source code in shortfx/fxNumeric/arithmetic_functions.py
bessel_k(x: float, n: int) -> float
¶
Modified Bessel function of the second kind, order n.
Description
Returns Kₙ(x). Equivalent to Excel BESSELK.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Positive value at which to evaluate. |
required |
n
|
int
|
Non-negative integer order. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Kₙ(x). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric or n is not int. |
ValueError
|
If n < 0 or x ≤ 0. |
Example
round(bessel_k(1.5, 1), 6) 0.277388
Complexity: O(1) — delegates to scipy
Source code in shortfx/fxNumeric/arithmetic_functions.py
bessel_y(x: float, n: int) -> float
¶
Bessel function of the second kind, order n.
Description
Returns Yₙ(x). Equivalent to Excel BESSELY.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Positive value at which to evaluate. |
required |
n
|
int
|
Non-negative integer order. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Yₙ(x). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric or n is not int. |
ValueError
|
If n < 0 or x ≤ 0. |
Example
round(bessel_y(2.5, 1), 6) 0.145918
Complexity: O(1) — delegates to scipy
Source code in shortfx/fxNumeric/arithmetic_functions.py
bisection_method(f: Callable[[float], float], a: float, b: float, tol: float = 1e-10, max_iter: int = 100) -> float
¶
Finds a root of f(x) = 0 in [a, b] using the bisection method.
Requires that f(a) and f(b) have opposite signs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
The function whose root is sought. |
required |
a
|
float
|
Lower bound of the interval. |
required |
b
|
float
|
Upper bound of the interval. |
required |
tol
|
float
|
Convergence tolerance on interval width. |
1e-10
|
max_iter
|
int
|
Maximum number of iterations. |
100
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate root x where f(x) ≈ 0. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable. |
ValueError
|
If f(a) and f(b) have the same sign. |
Example
round(bisection_method(lambda x: x**2 - 2, 1, 2), 8) 1.41421356
Complexity: O(max_iter) = O(log₂((b−a)/tol))
Source code in shortfx/fxNumeric/arithmetic_functions.py
bit_count(number: int) -> int
¶
Counts the number of set bits (1-bits) in a non-negative integer.
Description
Returns the population count (Hamming weight). Equivalent to Excel BITCOUNT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number
|
int
|
A non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of 1-bits. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not an integer. |
ValueError
|
If input is negative. |
Example
bit_count(13) 3 bit_count(255) 8 bit_count(0) 0
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
bit_lshift(number: int, shift_amount: int) -> int
¶
Shift number left by shift_amount bits.
Description
Returns number × 2^shift_amount. Equivalent to Excel BITLSHIFT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number
|
int
|
Non-negative integer (0 to 2^48 - 1). |
required |
shift_amount
|
int
|
Number of bit positions to shift (can be negative). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Left-shifted result. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not integers. |
ValueError
|
If number < 0 or number > 2^48 - 1. |
Example
bit_lshift(4, 2) 16
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
bit_rshift(number: int, shift_amount: int) -> int
¶
Shift number right by shift_amount bits.
Description
Returns number ÷ 2^shift_amount (integer). Equivalent to Excel BITRSHIFT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number
|
int
|
Non-negative integer (0 to 2^48 - 1). |
required |
shift_amount
|
int
|
Number of bit positions to shift (can be negative). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Right-shifted result. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not integers. |
ValueError
|
If number < 0 or number > 2^48 - 1. |
Example
bit_rshift(13, 2) 3
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
bitwise_and(a: int, b: int) -> int
¶
Returns the bitwise AND of two non-negative integers.
Description
Performs a bitwise AND operation. Equivalent to Excel BITAND.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
int
|
First non-negative integer. |
required |
b
|
int
|
Second non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The bitwise AND result. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not integers. |
ValueError
|
If inputs are negative. |
Example
bitwise_and(13, 25) 9 bitwise_and(0b1100, 0b1010) 8
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
bitwise_not(number: int, bit_width: int = 32) -> int
¶
Returns the bitwise NOT of a non-negative integer within a given bit width.
Description
Flips all bits of the number within the specified bit width. Equivalent to Excel BITNOT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number
|
int
|
A non-negative integer. |
required |
bit_width
|
int
|
The number of bits to consider (default 32). |
32
|
Returns:
| Type | Description |
|---|---|
int
|
The bitwise NOT result within the bit width. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not integers. |
ValueError
|
If number is negative or bit_width < 1. |
Example
bitwise_not(0, 8) 255 bitwise_not(13, 8) 242
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
bitwise_or(a: int, b: int) -> int
¶
Returns the bitwise OR of two non-negative integers.
Description
Performs a bitwise OR operation. Equivalent to Excel BITOR.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
int
|
First non-negative integer. |
required |
b
|
int
|
Second non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The bitwise OR result. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not integers. |
ValueError
|
If inputs are negative. |
Example
bitwise_or(13, 25) 29 bitwise_or(0b1100, 0b1010) 14
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
bitwise_xor(a: int, b: int) -> int
¶
Returns the bitwise XOR of two non-negative integers.
Description
Performs a bitwise exclusive OR operation. Equivalent to Excel BITXOR.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
int
|
First non-negative integer. |
required |
b
|
int
|
Second non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The bitwise XOR result. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not integers. |
ValueError
|
If inputs are negative. |
Example
bitwise_xor(13, 25) 20 bitwise_xor(0b1100, 0b1010) 6
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
cantor_pairing(a: int, b: int) -> int
¶
Compute the Cantor pairing function for two non-negative integers.
π(a, b) = (a + b)(a + b + 1)/2 + b
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
int
|
Non-negative integer. |
required |
b
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Cantor pair number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a or b is not an integer. |
ValueError
|
If a < 0 or b < 0. |
Usage Example
cantor_pairing(3, 4) 32
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
cantor_unpairing(z: int) -> tuple[int, int]
¶
Invert the Cantor pairing function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
int
|
Non-negative integer (Cantor pair number). |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
Tuple (a, b) such that cantor_pairing(a, b) = z. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If z is not an integer. |
ValueError
|
If z < 0. |
Usage Example
cantor_unpairing(32) (3, 4)
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
chebyshev_nodes(n: int, a: float = -1.0, b: float = 1.0) -> list[float]
¶
Generate n Chebyshev nodes on the interval [a, b].
Chebyshev nodes minimize the Runge phenomenon, making them ideal for polynomial interpolation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of nodes (≥ 1). |
required |
a
|
float
|
Left endpoint. |
-1.0
|
b
|
float
|
Right endpoint. |
1.0
|
Returns:
| Type | Description |
|---|---|
list[float]
|
List of Chebyshev node locations. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an int or endpoints are not numeric. |
ValueError
|
If n < 1 or a >= b. |
Example
[round(x, 4) for x in chebyshev_nodes(3)][0.866, 0.0, -0.866]
Complexity: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
cholesky_decomposition(matrix: List[List[float]]) -> List[List[float]]
¶
Performs Cholesky decomposition of a symmetric positive-definite matrix.
Decomposes A into L such that A = L * L^T, where L is lower triangular.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
List[List[float]]
|
Symmetric positive-definite square matrix. |
required |
Returns:
| Type | Description |
|---|---|
List[List[float]]
|
Lower triangular matrix L. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of lists of numbers. |
ValueError
|
If matrix is not square, not symmetric, or not positive-definite. |
Example
cholesky_decomposition([[4, 2], [2, 5]]) [[2.0, 0.0], [1.0, 2.0]]
Complexity: O(n^3)
Source code in shortfx/fxNumeric/arithmetic_functions.py
3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 | |
collatz_sequence(n: int) -> list[int]
¶
Generate the Collatz sequence starting from n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
The full Collatz sequence ending at 1. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
collatz_sequence(6) [6, 3, 10, 5, 16, 8, 4, 2, 1]
Complexity: Depends on the Collatz sequence length
Source code in shortfx/fxNumeric/arithmetic_functions.py
collatz_steps(n: int) -> int
¶
Count the number of Collatz steps to reach 1.
If n is even: n → n/2. If odd: n → 3n + 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of steps to reach 1. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
collatz_steps(27) 111
Complexity: Depends on the Collatz sequence length
Source code in shortfx/fxNumeric/arithmetic_functions.py
combinations(n: int, k: int) -> int
¶
Calculates the number of combinations C(n, k).
Description
Computes the binomial coefficient "n choose k", the number of ways to choose k items from n items without regard to order. C(n, k) = n! / (k! × (n-k)!)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Total number of items (non-negative). |
required |
k
|
int
|
Number of items to choose (0 ≤ k ≤ n). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of combinations. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n or k are not integers. |
ValueError
|
If n or k are negative, or if k > n. |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import combinations combinations(10, 3) 120 combinations(5, 0) 1
Cost: O(min(k, n-k))
Source code in shortfx/fxNumeric/arithmetic_functions.py
combinations_with_repetition(n: int, k: int) -> int
¶
Calculates combinations with repetition (multiset coefficient).
C(n+k-1, k) = (n+k-1)! / (k! * (n-1)!)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of types to choose from. |
required |
k
|
int
|
Number of items to choose. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of combinations with repetition. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n or k are not integers. |
ValueError
|
If n < 1 or k < 0. |
Example
combinations_with_repetition(3, 2) 6 combinations_with_repetition(4, 3) 20
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
common_log(x: float) -> float
¶
Calculates the common logarithm (base 10) of a number.
Description
Computes log₁₀(x), the common logarithm of x. This is widely used in scientific calculations, pH calculations, and decibel measurements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
The number for which to calculate the common logarithm. Must be positive. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The common logarithm of x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
ValueError
|
If x is not positive. |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import common_log common_log(10) 1.0 round(common_log(100), 10) 2.0 round(common_log(1), 10) 0.0
Cost: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
complementary_error_function(x: float) -> float
¶
Calculates the complementary error function erfc(x) = 1 - erf(x).
Description
Computes 1 - erf(x) with high accuracy, especially for large x where direct subtraction would lose precision. Commonly used in statistics and signal processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
A real number. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The complementary error function value at x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import complementary_error_function complementary_error_function(0) 1.0 round(complementary_error_function(1), 10) 0.1572992071
Cost: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
continued_fraction(numerator: int, denominator: int, max_terms: int = 20) -> List[int]
¶
Computes the continued fraction representation of a rational number.
Returns the list [a₀; a₁, a₂, …] such that
numerator/denominator = a₀ + 1/(a₁ + 1/(a₂ + …)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
numerator
|
int
|
Integer numerator. |
required |
denominator
|
int
|
Integer denominator (must not be zero). |
required |
max_terms
|
int
|
Maximum number of terms to compute. |
20
|
Returns:
| Type | Description |
|---|---|
List[int]
|
List of continued fraction coefficients. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not integers. |
ValueError
|
If denominator is zero. |
Example
continued_fraction(355, 113) [3, 7, 16]
Complexity: O(min(max_terms, log(denominator)))
Source code in shortfx/fxNumeric/arithmetic_functions.py
cross_product(vec_a: List[Union[int, float]], vec_b: List[Union[int, float]]) -> List[float]
¶
Computes the cross product of two 3D vectors.
a × b = [a₂b₃−a₃b₂, a₃b₁−a₁b₃, a₁b₂−a₂b₁].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vec_a
|
List[Union[int, float]]
|
First 3D vector [x, y, z]. |
required |
vec_b
|
List[Union[int, float]]
|
Second 3D vector [x, y, z]. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
The resulting 3D cross-product vector. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If vectors are not exactly 3 elements. |
Example
cross_product([1, 0, 0], [0, 1, 0]) [0, 0, 1]
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
cube_root(x: Union[int, float]) -> float
¶
Calculates the cube root of a number.
Description
Computes ∛x, finding the number that, when multiplied by itself three times, equals x. Works for both positive and negative real numbers. Uses math.cbrt for Python 3.11+ for better accuracy, with fallback for older versions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
The number for which to calculate the cube root. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The cube root of x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import cube_root cube_root(8) 2.0 cube_root(27) 3.0 cube_root(-8) -2.0 cube_root(0) 0.0
Cost: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
delta(number_1: float, number_2: float = 0) -> int
¶
Tests whether two values are equal (Kronecker delta).
Description
Returns 1 if number_1 equals number_2, otherwise 0. Equivalent to Excel DELTA.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number_1
|
float
|
The first number. |
required |
number_2
|
float
|
The second number (defaults to 0). |
0
|
Returns:
| Type | Description |
|---|---|
int
|
1 if the values are equal, 0 otherwise. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
Example
delta(5, 5) 1 delta(5, 4) 0 delta(0) 1
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
digit_product(n: int) -> int
¶
Compute the product of digits of a positive integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Product of digits (0 if any digit is 0). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Usage Example
digit_product(234) 24
Complexity: O(log n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
digital_sum(n: int) -> int
¶
Compute the digit sum of an integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Sum of digits. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Usage Example
digital_sum(12345) 15
Complexity: O(log n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
dot_product(vec_a: List[Union[int, float]], vec_b: List[Union[int, float]]) -> float
¶
Computes the dot product of two vectors.
result = Σ(aᵢ × bᵢ).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vec_a
|
List[Union[int, float]]
|
First numeric vector. |
required |
vec_b
|
List[Union[int, float]]
|
Second numeric vector (same length). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The scalar dot product. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If vectors are empty or have different lengths. |
Example
dot_product([1, 2, 3], [4, 5, 6]) 32
Complexity: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
double_factorial(n: int) -> int
¶
Calculates the double factorial of a non-negative integer.
Description
Computes n!! = n × (n-2) × (n-4) × ... down to 1 or 2. For example, 7!! = 7 × 5 × 3 × 1 = 105 and 6!! = 6 × 4 × 2 = 48. By convention, 0!! = 1 and 1!! = 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
A non-negative integer. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The double factorial of n. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import double_factorial double_factorial(7) 105 double_factorial(6) 48 double_factorial(0) 1
Cost: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
error_function(x: float) -> float
¶
Calculates the error function erf(x).
Description
Computes the Gauss error function, commonly used in probability, statistics, and partial differential equations. Values range from -1 to 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
A real number. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The error function value at x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import error_function error_function(0) 0.0 round(error_function(1), 10) 0.8427007929
Cost: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
euler_method(f: callable, y0: float, t0: float, t_end: float, steps: int = 100) -> list[tuple[float, float]]
¶
Solve an ODE y' = f(t, y) using Euler's method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
callable
|
Function |
required |
y0
|
float
|
Initial value y(t0). |
required |
t0
|
float
|
Start time. |
required |
t_end
|
float
|
End time. |
required |
steps
|
int
|
Number of integration steps. |
100
|
Returns:
| Type | Description |
|---|---|
list[tuple[float, float]]
|
List of |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable or other args not numeric. |
ValueError
|
If steps < 1. |
Example
pts = euler_method(lambda t, y: y, 1.0, 0.0, 1.0, 1000) round(pts[-1][1], 2) 2.72
Complexity: O(steps)
Source code in shortfx/fxNumeric/arithmetic_functions.py
exp(x: float) -> float
¶
Calculates e raised to the power of x.
Description
Computes e^x where e is Euler's number (≈ 2.71828). Uses the
optimized math.exp for numerical stability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
The exponent. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The value of e^x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import exp round(exp(1), 10) 2.7182818285 exp(0) 1.0
Cost: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
expm1(x: float) -> float
¶
Calculates e^x - 1 with high precision for small x.
Description
Computes e^x - 1 accurately for values of x close to zero,
where direct computation of exp(x) - 1 would lose precision.
Complementary to log1p.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
The exponent value. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The value of e^x - 1. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import expm1 expm1(0) 0.0 round(expm1(1e-10), 20) 1e-10
Cost: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
factorial(n: int) -> int
¶
Calculates the factorial of a non-negative integer.
Description
Computes n! = 1 * 2 * 3 * ... * n. Factorial of 0 is 1 by convention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
A non-negative integer. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The factorial of n. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Usage Example
factorial(5) 120 factorial(0) 1
Cost: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
fibonacci(n: int) -> int
¶
Returns the n-th Fibonacci number (0-indexed).
Uses iterative computation for efficiency and avoids recursion limits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
The position in the Fibonacci sequence (must be >= 0). |
required |
Returns:
| Type | Description |
|---|---|
int
|
The n-th Fibonacci number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n is negative. |
Usage Example
fibonacci(0) 0 fibonacci(1) 1 fibonacci(10) 55 fibonacci(20) 6765
Cost: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
fixed_point_iteration(g: Callable[[float], float], x0: float, tol: float = 1e-10, max_iter: int = 100) -> float
¶
Find a fixed point of g, i.e. x such that g(x) = x.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g
|
Callable[[float], float]
|
Iteration function g(x). |
required |
x0
|
float
|
Initial guess. |
required |
tol
|
float
|
Convergence tolerance. |
1e-10
|
max_iter
|
int
|
Maximum iterations. |
100
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate fixed point. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If g is not callable. |
RuntimeError
|
If convergence fails. |
Example
round(fixed_point_iteration(lambda x: (x + 2/x) / 2, 1.0), 6) 1.414214
Complexity: O(max_iter)
Source code in shortfx/fxNumeric/arithmetic_functions.py
floor_division(numerator: Union[int, float], denominator: Union[int, float]) -> Union[int, float]
¶
Performs integer (floor) division, truncating the result downwards.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
numerator
|
Union[int, float]
|
The dividend. |
required |
denominator
|
Union[int, float]
|
The divisor. |
required |
Returns:
| Type | Description |
|---|---|
Union[int, float]
|
Union[int, float]: The integer part of the quotient. |
Raises:
| Type | Description |
|---|---|
ZeroDivisionError
|
If the divisor is zero. |
Example
floor_division(5, 2) 2 floor_division(-5, 2) -3
Cost: O(1), integer division.
Source code in shortfx/fxNumeric/arithmetic_functions.py
force_float_division(numerator: Union[int, float], denominator: Union[int, float]) -> float
¶
Performs division ensuring the result is always a float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
numerator
|
Union[int, float]
|
The numerator. |
required |
denominator
|
Union[int, float]
|
The denominator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The division result as a floating-point number. |
Raises:
| Type | Description |
|---|---|
ZeroDivisionError
|
If the denominator is zero. |
Example
force_float_division(5, 2) 2.5 force_float_division(10, 3) 3.3333333333333335
Cost: O(1), arithmetic division.
Source code in shortfx/fxNumeric/arithmetic_functions.py
gamma(x: float) -> float
¶
Calculates the Gamma function Γ(x).
Description
Computes Γ(x), the extension of factorial to real numbers. For positive integers, Γ(n) = (n-1)!. The Gamma function is defined for all real numbers except non-positive integers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
A real number (not zero or a negative integer). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The value of the Gamma function at x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
ValueError
|
If x is zero or a negative integer. |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import gamma gamma(5) 24.0 round(gamma(0.5), 10) 1.7724538509
Cost: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
gaussian_quadrature(f: callable, a: float, b: float, n: int = 5) -> float
¶
Numerical integration via Gauss-Legendre quadrature.
Uses pre-computed nodes and weights for n = 1-5. For general n falls back to the midpoint rule (n subintervals).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
callable
|
Integrand function. |
required |
a
|
float
|
Lower bound. |
required |
b
|
float
|
Upper bound. |
required |
n
|
int
|
Number of quadrature points (1-5 for exact GL). |
5
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate integral value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable or bounds not numeric. |
ValueError
|
If n < 1. |
Example
import math round(gaussian_quadrature(math.sin, 0, math.pi, 5), 6) 2.000011
Complexity: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 | |
gcd_list(numbers: List[int]) -> int
¶
Calculates the GCD of a list of integers.
Description
Computes the Greatest Common Divisor of all integers in the list by iteratively applying GCD pairwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
numbers
|
List[int]
|
A list of integers (at least one element). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The GCD of all numbers in the list. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list or contains non-integers. |
ValueError
|
If the list is empty. |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import gcd_list gcd_list([12, 18, 24]) 6 gcd_list([7, 14, 21]) 7
Cost: O(n × log(min))
Source code in shortfx/fxNumeric/arithmetic_functions.py
gestep(number: float, step: float = 0) -> int
¶
Tests whether a number is greater than or equal to a step value.
Description
Returns 1 if number >= step, otherwise 0. Implements the Heaviside step function. Equivalent to Excel GESTEP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number
|
float
|
The value to test. |
required |
step
|
float
|
The threshold value (defaults to 0). |
0
|
Returns:
| Type | Description |
|---|---|
int
|
1 if number >= step, 0 otherwise. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
Example
gestep(5, 4) 1 gestep(3, 4) 0 gestep(0) 1
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
greatest_common_divisor(a: int, b: int) -> int
¶
Calculates the greatest common divisor (GCD) of two integers.
Description
Computes the largest positive integer that divides both a and b without remainder, using the Euclidean algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
int
|
First integer. |
required |
b
|
int
|
Second integer. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The GCD of a and b. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a or b are not integers. |
Usage Example
greatest_common_divisor(12, 8) 4 greatest_common_divisor(100, 75) 25 greatest_common_divisor(7, 13) 1
Cost: O(log(min(a, b)))
Source code in shortfx/fxNumeric/arithmetic_functions.py
identity_matrix(size: int) -> List[List[float]]
¶
Creates an identity matrix of the given size.
Description
Returns an n × n identity matrix with 1.0 on the main diagonal and 0.0 elsewhere. Equivalent to Excel MUNIT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
The dimension of the square identity matrix. |
required |
Returns:
| Type | Description |
|---|---|
List[List[float]]
|
An identity matrix as a list of lists. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If size is not an integer. |
ValueError
|
If size is less than 1. |
Example
identity_matrix(3) [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
Complexity: O(n^2)
Source code in shortfx/fxNumeric/arithmetic_functions.py
integer_partition_distinct(n: int) -> int
¶
Count partitions of n into distinct parts using DP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of partitions with distinct parts. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Usage Example
integer_partition_distinct(10) 10
Complexity: O(n²)
Source code in shortfx/fxNumeric/arithmetic_functions.py
is_automorphic(n: int) -> bool
¶
Check if n is an automorphic number (n² ends with n).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is automorphic. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Usage Example
is_automorphic(76) True
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
is_happy_number(n: int) -> bool
¶
Check if n is a happy number.
A number where iterated sum of squares of digits reaches 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Positive integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is happy. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
is_happy_number(19) True
Complexity: O(log n × iterations)
Source code in shortfx/fxNumeric/arithmetic_functions.py
is_narcissistic(n: int) -> bool
¶
Check if n is a narcissistic (Armstrong) number.
A k-digit number where sum of each digit raised to k equals n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if n is narcissistic. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Usage Example
is_narcissistic(153) True
Complexity: O(log n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
lcm(a: int, b: int) -> int
¶
Calculates the Least Common Multiple of two integers.
Description
Computes LCM(a, b) = |a × b| / GCD(a, b). The LCM is the smallest positive integer that is divisible by both a and b.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
int
|
First integer. |
required |
b
|
int
|
Second integer. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The least common multiple of a and b. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a or b are not integers. |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import lcm lcm(4, 6) 12 lcm(3, 7) 21 lcm(0, 5) 0
Cost: O(log(min(a, b)))
Source code in shortfx/fxNumeric/arithmetic_functions.py
lcm_list(numbers: List[int]) -> int
¶
Calculates the LCM of a list of integers.
Description
Computes the Least Common Multiple of all integers in the list by iteratively applying LCM pairwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
numbers
|
List[int]
|
A list of integers (at least one element). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The LCM of all numbers in the list. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list or contains non-integers. |
ValueError
|
If the list is empty. |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import lcm_list lcm_list([4, 6, 10]) 60 lcm_list([3, 5, 7]) 105
Cost: O(n × log(min))
Source code in shortfx/fxNumeric/arithmetic_functions.py
least_common_multiple(a: int, b: int) -> int
¶
Calculates the least common multiple (LCM) of two integers.
Description
Computes the smallest positive integer that is divisible by both a and b. Uses the relationship LCM(a, b) = |a * b| / GCD(a, b).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
int
|
First integer. |
required |
b
|
int
|
Second integer. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The LCM of a and b. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a or b are not integers. |
ValueError
|
If both a and b are zero. |
Usage Example
least_common_multiple(4, 6) 12 least_common_multiple(3, 7) 21 least_common_multiple(12, 18) 36
Cost: O(log(min(a, b)))
Source code in shortfx/fxNumeric/arithmetic_functions.py
log1p(x: float) -> float
¶
Calculates the natural logarithm of (1 + x) with high precision.
Description
Computes ln(1 + x) accurately for values of x close to zero, where direct computation of log(1 + x) would lose precision due to floating-point arithmetic limitations. Uses specialized algorithms for numerical stability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
The number for which to calculate log(1 + x). Must be greater than -1. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The natural logarithm of (1 + x). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
ValueError
|
If x is less than or equal to -1. |
Usage Example
import math from shortfx.fxNumeric.arithmetic_functions import log1p round(log1p(0), 10) 0.0 small_x = 1e-9 round(log1p(small_x), 15) # More precise than log(1+x) 0.000000001000000
Cost: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
log_base_n(x: float, base: Union[int, float]) -> float
¶
Calculates the logarithm of a number to a specified base N.
Description
Computes log_base(x) using the change of base formula: log_base(x) = ln(x) / ln(base). This allows logarithms with any positive base other than 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
The number for which to calculate the logarithm. Must be positive. |
required |
base
|
Union[int, float]
|
The base of the logarithm. Must be positive and not equal to 1. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The logarithm of x to the given base. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x or base are not numeric. |
ValueError
|
If x is not positive, base is not positive, or base is 1. |
Usage Example
import math from shortfx.fxNumeric.arithmetic_functions import log_base_n log_base_n(100, 10) 2.0 round(log_base_n(8, 2), 10) 3.0 round(log_base_n(math.e, 10), 10) 0.4342944819
Cost: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
log_gamma(x: float) -> float
¶
Calculates the natural logarithm of the absolute value of Γ(x).
Description
Computes ln|Γ(x)|, useful when Γ(x) is too large for direct computation but its logarithm is needed (e.g., in combinatorics and statistical distributions).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
A positive real number. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The natural logarithm of the absolute Gamma function at x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
ValueError
|
If x is not positive. |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import log_gamma round(log_gamma(5), 10) 3.1780538303
Cost: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
look_and_say(seed: str = '1', iterations: int = 5) -> str
¶
Generate the look-and-say sequence.
Each term describes the digits of the previous term (e.g., "1" → "11" → "21" → "1211" → ...).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
str
|
Initial string (default "1"). |
'1'
|
iterations
|
int
|
Number of iterations (default 5). |
5
|
Returns:
| Type | Description |
|---|---|
str
|
The resulting string after iterations. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If seed is not a string or iterations is not int. |
ValueError
|
If iterations < 0. |
Usage Example
look_and_say("1", 4) '111221'
Complexity: O(iterations × len(string))
Source code in shortfx/fxNumeric/arithmetic_functions.py
lu_decomposition(matrix: List[List[float]]) -> Tuple[List[List[float]], List[List[float]]]
¶
Performs LU decomposition of a square matrix (Doolittle algorithm).
Decomposes A into L (lower triangular with unit diagonal) and U (upper triangular) such that A = L * U.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
List[List[float]]
|
Square matrix as list of lists. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[List[List[float]], List[List[float]]]
|
Tuple (L, U). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of lists of numbers. |
ValueError
|
If matrix is not square, empty, or pivot is zero. |
Example
L, U = lu_decomposition([[2, 1], [4, 3]]) L [[1.0, 0.0], [2.0, 1.0]] U [[2.0, 1.0], [0.0, 1.0]]
Complexity: O(n^3)
Source code in shortfx/fxNumeric/arithmetic_functions.py
3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 | |
matrix_add(matrix_a: List[List[Union[int, float]]], matrix_b: List[List[Union[int, float]]]) -> List[List[float]]
¶
Adds two matrices element-wise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix_a
|
List[List[Union[int, float]]]
|
First matrix. |
required |
matrix_b
|
List[List[Union[int, float]]]
|
Second matrix (same dimensions). |
required |
Returns:
| Type | Description |
|---|---|
List[List[float]]
|
The sum matrix. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of lists. |
ValueError
|
If matrices are empty or have different dimensions. |
Example
matrix_add([[1, 2], [3, 4]], [[5, 6], [7, 8]]) [[6, 8], [10, 12]]
Complexity: O(m × n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
matrix_determinant(matrix: List[List[float]]) -> float
¶
Computes the determinant of a square matrix.
Description
Calculates the determinant using LU decomposition via recursive cofactor expansion. Equivalent to Excel MDETERM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
List[List[float]]
|
A square matrix represented as a list of lists. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The determinant value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of lists of numbers. |
ValueError
|
If the matrix is not square or is empty. |
Example
matrix_determinant([[1, 2], [3, 4]]) -2.0 matrix_determinant([[6, 1, 1], [4, -2, 5], [2, 8, 7]]) -306.0
Complexity: O(n^3)
Source code in shortfx/fxNumeric/arithmetic_functions.py
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 | |
matrix_eigenvalues_2x2(matrix: List[List[float]]) -> Tuple[complex, complex]
¶
Computes the eigenvalues of a 2×2 matrix using the characteristic equation.
For [[a, b], [c, d]], eigenvalues are roots of λ² - (a+d)λ + (ad - bc) = 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
List[List[float]]
|
A 2×2 matrix. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[complex, complex]
|
Tuple of two eigenvalues (may be complex). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a 2×2 list of lists of numbers. |
ValueError
|
If matrix is not 2×2. |
Example
matrix_eigenvalues_2x2([[2, 1], [1, 2]]) (3.0, 1.0)
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
matrix_frobenius_norm(matrix: List[List[float]]) -> float
¶
Computes the Frobenius norm of a matrix: sqrt(sum of squares of all elements).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
List[List[float]]
|
Matrix as list of lists. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Frobenius norm. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of lists of numbers. |
Example
round(matrix_frobenius_norm([[1, 2], [3, 4]]), 6) 5.477226
Complexity: O(m*n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
matrix_infinity_norm(matrix: List[List[float]]) -> float
¶
Computes the infinity norm of a matrix: max row-sum of absolute values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
List[List[float]]
|
Matrix as list of lists. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Infinity norm. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of lists of numbers. |
ValueError
|
If matrix is empty. |
Example
matrix_infinity_norm([[1, -2], [3, 4]]) 7
Complexity: O(m*n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
matrix_inverse(matrix: List[List[float]]) -> List[List[float]]
¶
Computes the inverse of a square matrix.
Description
Calculates the matrix inverse using Gauss-Jordan elimination with partial pivoting. Equivalent to Excel MINVERSE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
List[List[float]]
|
A square, non-singular matrix as a list of lists. |
required |
Returns:
| Type | Description |
|---|---|
List[List[float]]
|
The inverse matrix as a list of lists of floats. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of lists of numbers. |
ValueError
|
If the matrix is not square, empty, or singular. |
Example
result = matrix_inverse([[4, 7], [2, 6]]) [[round(x, 4) for x in row] for row in result][[0.6, -0.7], [-0.2, 0.4]]
Complexity: O(n^3)
Source code in shortfx/fxNumeric/arithmetic_functions.py
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 | |
matrix_multiply(matrix_a: List[List[float]], matrix_b: List[List[float]]) -> List[List[float]]
¶
Multiplies two matrices.
Description
Performs matrix multiplication A × B. The number of columns in A must equal the number of rows in B. Equivalent to Excel MMULT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix_a
|
List[List[float]]
|
First matrix (m × n). |
required |
matrix_b
|
List[List[float]]
|
Second matrix (n × p). |
required |
Returns:
| Type | Description |
|---|---|
List[List[float]]
|
The resulting matrix (m × p) as a list of lists. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of lists of numbers. |
ValueError
|
If matrices are empty or dimensions are incompatible. |
Example
matrix_multiply([[1, 2], [3, 4]], [[5, 6], [7, 8]]) [[19, 22], [43, 50]]
Complexity: O(m × n × p)
Source code in shortfx/fxNumeric/arithmetic_functions.py
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 | |
matrix_one_norm(matrix: List[List[float]]) -> float
¶
Computes the 1-norm of a matrix: max column-sum of absolute values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
List[List[float]]
|
Matrix as list of lists. |
required |
Returns:
| Type | Description |
|---|---|
float
|
1-norm. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of lists of numbers. |
ValueError
|
If matrix is empty. |
Example
matrix_one_norm([[1, -2], [3, 4]]) 6
Complexity: O(m*n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
matrix_power(matrix: List[List[float]], n: int) -> List[List[float]]
¶
Computes the n-th power of a square matrix A^n by repeated multiplication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
List[List[float]]
|
Square matrix. |
required |
n
|
int
|
Non-negative integer exponent. |
required |
Returns:
| Type | Description |
|---|---|
List[List[float]]
|
A^n as list of lists. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of lists, or n is not an int. |
ValueError
|
If matrix is not square or n < 0. |
Example
matrix_power([[1, 1], [0, 1]], 3) [[1, 3], [0, 1]]
Complexity: O(k^3 * log(n)) where k is matrix size.
Source code in shortfx/fxNumeric/arithmetic_functions.py
3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 | |
matrix_rank(matrix: List[List[float]], tol: float = 1e-10) -> int
¶
Computes the rank of a matrix via Gaussian elimination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
List[List[float]]
|
Matrix as list of lists. |
required |
tol
|
float
|
Tolerance for considering a pivot as zero. |
1e-10
|
Returns:
| Type | Description |
|---|---|
int
|
Rank of the matrix. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of lists of numbers. |
ValueError
|
If matrix is empty. |
Example
matrix_rank([[1, 2], [2, 4]]) 1 matrix_rank([[1, 0], [0, 1]]) 2
Complexity: O(mnmin(m,n))
Source code in shortfx/fxNumeric/arithmetic_functions.py
3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 | |
matrix_scalar_multiply(matrix: List[List[Union[int, float]]], scalar: Union[int, float]) -> List[List[float]]
¶
Multiplies every element of a matrix by a scalar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
List[List[Union[int, float]]]
|
A rectangular matrix. |
required |
scalar
|
Union[int, float]
|
The scalar multiplier. |
required |
Returns:
| Type | Description |
|---|---|
List[List[float]]
|
The scaled matrix. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If matrix is not a list of lists or scalar is not numeric. |
ValueError
|
If matrix is empty. |
Example
matrix_scalar_multiply([[1, 2], [3, 4]], 3) [[3, 6], [9, 12]]
Complexity: O(m × n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
matrix_trace(matrix: List[List[float]]) -> float
¶
Computes the trace of a square matrix (sum of diagonal elements).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
List[List[float]]
|
A square matrix as a list of lists. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The trace (sum of main diagonal). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If matrix is not a list of lists. |
ValueError
|
If matrix is empty or not square. |
Example
matrix_trace([[1, 2], [3, 4]]) 5
Complexity: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
matrix_transpose(matrix: List[List[float]]) -> List[List[float]]
¶
Transposes a matrix (swaps rows and columns).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
List[List[float]]
|
A non-empty rectangular matrix as a list of lists. |
required |
Returns:
| Type | Description |
|---|---|
List[List[float]]
|
The transposed matrix. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If matrix is not a list of lists. |
ValueError
|
If matrix is empty or rows have inconsistent lengths. |
Example
matrix_transpose([[1, 2, 3], [4, 5, 6]]) [[1, 4], [2, 5], [3, 6]]
Complexity: O(m * n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
modulo(number: Union[int, float], divisor: Union[int, float]) -> Union[int, float]
¶
Returns the remainder after dividing number by divisor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number
|
Union[int, float]
|
The dividend. |
required |
divisor
|
Union[int, float]
|
The divisor (must not be zero). |
required |
Returns:
| Type | Description |
|---|---|
Union[int, float]
|
The remainder of number / divisor. |
Raises:
| Type | Description |
|---|---|
ZeroDivisionError
|
If divisor is zero. |
Example
modulo(10, 3) 1 modulo(7.5, 2.0) 1.5
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
multinomial_coefficient(*args: int) -> int
¶
Compute the multinomial coefficient n! / (k1! * k2! * ... * km!).
Where n = sum(args). Equivalent to Excel's MULTINOMIAL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
int
|
Non-negative integers k1, k2, ..., km. |
()
|
Returns:
| Type | Description |
|---|---|
int
|
Multinomial coefficient as integer. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any argument is negative. |
Example
multinomial_coefficient(2, 3, 4) 1260 multinomial_coefficient(5) 1
Complexity: O(sum(args))
Source code in shortfx/fxNumeric/arithmetic_functions.py
multiplicative_persistence(n: int) -> int
¶
Count iterations to reduce n to a single digit by multiplying its digits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of iterations. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Usage Example
multiplicative_persistence(39) 3
Complexity: O(log(n) × iterations)
Source code in shortfx/fxNumeric/arithmetic_functions.py
natural_log(x: float) -> float
¶
Calculates the natural logarithm (base e) of a number.
Description
Computes ln(x) or log_e(x), the natural logarithm of x. The natural logarithm is the inverse of the exponential function e^x.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
The number for which to calculate the natural logarithm. Must be positive. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The natural logarithm of x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
ValueError
|
If x is not positive. |
Usage Example
import math from shortfx.fxNumeric.arithmetic_functions import natural_log natural_log(math.e) 1.0 round(natural_log(1), 10) 0.0 round(natural_log(10), 10) 2.302585093
Cost: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
newton_raphson(f: Callable[[float], float], df: Callable[[float], float], x0: float, tol: float = 1e-10, max_iter: int = 100) -> float
¶
Finds a root of f(x) = 0 using the Newton-Raphson method.
Requires the derivative df(x). Converges quadratically near simple roots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
The function whose root is sought. |
required |
df
|
Callable[[float], float]
|
The derivative of f. |
required |
x0
|
float
|
Initial guess. |
required |
tol
|
float
|
Convergence tolerance on |f(x)|. |
1e-10
|
max_iter
|
int
|
Maximum number of iterations. |
100
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate root x where f(x) ≈ 0. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f or df are not callable. |
ValueError
|
If derivative is zero (division by zero) or convergence fails. |
Example
round(newton_raphson(lambda x: x*2 - 2, lambda x: 2x, 1.0), 10) 1.4142135624
Complexity: O(max_iter)
Source code in shortfx/fxNumeric/arithmetic_functions.py
nth_root(x: Union[int, float], n: Union[int, float]) -> float
¶
Calculates the nth root of a number.
Description
Computes ⁿ√x or x^(1/n), generalizing square and cube roots to any degree n. For negative x, n must be an odd integer to return a real result. Handles edge cases like 0^(negative n) appropriately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
The number for which to calculate the root. |
required |
n
|
Union[int, float]
|
The degree of the root (e.g., 2 for square root). Must be non-zero. If x < 0, n must be odd. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The nth root of x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x or n are not numeric. |
ValueError
|
If n is 0, or if x is negative and n is even, or if x is 0 and n is negative. |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import nth_root nth_root(81, 4) # Fourth root of 81 3.0 nth_root(1000, 3) # Cube root of 1000 10.0 nth_root(-27, 3) # Cube root of -27 -3.0
Cost: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
numerical_derivative(f: Callable[[float], float], x: float, h: float = 1e-08) -> float
¶
Computes the numerical derivative using the central difference method.
f'(x) ≈ (f(x+h) − f(x−h)) / (2h)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
A callable that takes a float and returns a float. |
required |
x
|
float
|
The point at which to evaluate the derivative. |
required |
h
|
float
|
Step size (default 1e-8). |
1e-08
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate value of f'(x). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable or x/h are not numeric. |
ValueError
|
If h is zero. |
Example
import math round(numerical_derivative(math.sin, 0), 6) 1.0
Complexity: O(1) — two function evaluations
Source code in shortfx/fxNumeric/arithmetic_functions.py
permutations(n: int, k: int) -> int
¶
Calculates the number of permutations P(n, k).
Description
Computes the number of ways to arrange k items from n items where order matters. P(n, k) = n! / (n-k)!
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Total number of items (non-negative). |
required |
k
|
int
|
Number of items to arrange (0 ≤ k ≤ n). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of permutations. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n or k are not integers. |
ValueError
|
If n or k are negative, or if k > n. |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import permutations permutations(5, 3) 60 permutations(4, 4) 24
Cost: O(k)
Source code in shortfx/fxNumeric/arithmetic_functions.py
permutations_with_repetition(n: int, k: int) -> int
¶
Returns the number of permutations with repetition.
Description
Computes n^k — the number of ways to arrange k items chosen from n items where repetition is allowed. Equivalent to Excel PERMUTATIONA.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
The number of distinct items. |
required |
k
|
int
|
The number of items to choose. |
required |
Returns:
| Type | Description |
|---|---|
int
|
n raised to the power k. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not integers. |
ValueError
|
If n or k is negative, or n is 0 and k > 0. |
Example
permutations_with_repetition(3, 2) 9 permutations_with_repetition(10, 3) 1000
Complexity: O(log k) via exponentiation
Source code in shortfx/fxNumeric/arithmetic_functions.py
polynomial_evaluate(coefficients: List[float], x: float) -> float
¶
Evaluates a polynomial at x using Horner's method.
Coefficients are ordered from highest degree to lowest:
[aₙ, aₙ₋₁, …, a₁, a₀] represents aₙxⁿ + … + a₁x + a₀.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coefficients
|
List[float]
|
List of coefficients [aₙ, …, a₀]. |
required |
x
|
float
|
The point at which to evaluate. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Polynomial value at x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If coefficients is not a list or x is not numeric. |
ValueError
|
If coefficients is empty. |
Example
polynomial_evaluate([1, -3, 2], 2) # x²-3x+2 at x=2 0
Complexity: O(n) where n = len(coefficients)
Source code in shortfx/fxNumeric/arithmetic_functions.py
polynomial_roots_cubic(a: float, b: float, c: float, d: float) -> Tuple
¶
Finds the three roots of a cubic equation ax³ + bx² + cx + d = 0.
Uses Cardano's method. Returns a tuple of three roots (may be complex).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
float
|
Coefficient of x³ (must not be zero). |
required |
b
|
float
|
Coefficient of x². |
required |
c
|
float
|
Coefficient of x. |
required |
d
|
float
|
Constant term. |
required |
Returns:
| Type | Description |
|---|---|
Tuple
|
Tuple of three roots. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If a is zero. |
Example
roots = polynomial_roots_cubic(1, -6, 11, -6) # (x-1)(x-2)(x-3) sorted([round(r.real, 6) for r in roots]) [1.0, 2.0, 3.0]
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 | |
polynomial_roots_quadratic(a: float, b: float, c: float) -> Tuple[complex, complex]
¶
Finds the two roots of a quadratic equation ax² + bx + c = 0.
Returns complex roots when the discriminant is negative.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
float
|
Coefficient of x² (must not be zero). |
required |
b
|
float
|
Coefficient of x. |
required |
c
|
float
|
Constant term. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[complex, complex]
|
Tuple of two roots (may be complex). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric. |
ValueError
|
If a is zero. |
Example
polynomial_roots_quadratic(1, -3, 2) (2.0, 1.0) polynomial_roots_quadratic(1, 0, 1) (1j, -1j)
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
power(base: Union[int, float], exponent: Union[int, float]) -> float
¶
Calculates the value of a base raised to a specified exponent.
Description
Computes base^exponent, handling both positive and negative bases and exponents. Returns only real-valued results; complex results (e.g., negative base with non-integer exponent) raise an error. Properly handles edge cases like 0^0 = 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
Union[int, float]
|
The base number. |
required |
exponent
|
Union[int, float]
|
The exponent to which the base is raised. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The result of base raised to the power of exponent. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If base or exponent are not numeric. |
ValueError
|
If a negative base is raised to a non-integer exponent, or if 0 is raised to a negative exponent. |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import power power(2, 3) 8.0 power(9, 0.5) # Square root 3.0 power(-2, 3) -8.0 power(10, -2) 0.01
Cost: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
product_list(data: List[Union[int, float]]) -> Union[int, float]
¶
Calculates the product of all values in a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[Union[int, float]]
|
A list of numeric values. |
required |
Returns:
| Type | Description |
|---|---|
Union[int, float]
|
The product of all values. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If the list is empty. |
Example
product_list([1, 2, 3, 4]) 24 product_list([2.0, 3.0]) 6.0
Complexity: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
qr_decomposition(matrix: List[List[float]]) -> Tuple[List[List[float]], List[List[float]]]
¶
Performs QR decomposition via modified Gram-Schmidt orthogonalization.
Decomposes A (m×n, m >= n) into Q (m×n orthogonal) and R (n×n upper triangular) such that A = Q * R.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
List[List[float]]
|
Matrix with m >= n as list of lists. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[List[List[float]], List[List[float]]]
|
Tuple (Q, R). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of lists of numbers. |
ValueError
|
If matrix is empty, or m < n, or columns are linearly dependent. |
Example
Q, R = qr_decomposition([[1, 1], [0, 1], [1, 0]]) len(Q), len(R) (3, 2)
Complexity: O(m*n^2)
Source code in shortfx/fxNumeric/arithmetic_functions.py
3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 | |
quotient(dividend: Union[int, float], divisor: Union[int, float]) -> int
¶
Return the integer portion of a division (truncated toward zero).
Equivalent to Excel's QUOTIENT function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dividend
|
Union[int, float]
|
The number to be divided. |
required |
divisor
|
Union[int, float]
|
The number to divide by. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Integer result of division truncated toward zero. |
Raises:
| Type | Description |
|---|---|
ZeroDivisionError
|
If divisor is 0. |
Example
quotient(7, 2) 3 quotient(-7, 2) -3
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
reduce_to_modulo_range(x: Union[int, float], base: Union[int, float]) -> Union[int, float]
¶
Converts a value to a cyclic range using the modulo operation.
The result will be in the range [0, base) for positive numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
The numeric value to reduce. |
required |
base
|
Union[int, float]
|
The cycle base (the range size). Must be positive. |
required |
Returns:
| Type | Description |
|---|---|
Union[int, float]
|
Union[int, float]: The value reduced to the cyclic range. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If 'base' is zero or negative. |
Example
reduce_to_modulo_range(25, 24) 1 reduce_to_modulo_range(370, 360) 10
Cost: O(1), modulo operation.
Source code in shortfx/fxNumeric/arithmetic_functions.py
romberg_integrate(f: Callable[[float], float], a: float, b: float, max_order: int = 10, tol: float = 1e-12) -> float
¶
Romberg integration — recursive Richardson extrapolation on the trapezoid rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Function to integrate, f(x). |
required |
a
|
float
|
Lower bound. |
required |
b
|
float
|
Upper bound. |
required |
max_order
|
int
|
Maximum Richardson extrapolation order. |
10
|
tol
|
float
|
Convergence tolerance. |
1e-12
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate definite integral. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable. |
Example
import math round(romberg_integrate(math.sin, 0, math.pi), 10) 2.0
Complexity: O(2^max_order)
Source code in shortfx/fxNumeric/arithmetic_functions.py
runge_kutta_4_step(f: Callable[[float, float], float], t: float, y: float, h: float) -> Tuple[float, float]
¶
Performs one step of the classic 4th-order Runge-Kutta method.
Solves dy/dt = f(t, y) advancing from (t, y) to (t+h, y_new).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float, float], float]
|
The ODE function f(t, y). |
required |
t
|
float
|
Current time. |
required |
y
|
float
|
Current state value. |
required |
h
|
float
|
Step size. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (t + h, y_new). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable or t/y/h are not numeric. |
Example
dy/dt = y, y(0) = 1 → solution is e^t¶
t1, y1 = runge_kutta_4_step(lambda t, y: y, 0, 1.0, 0.1) round(y1, 6) 1.105171
Complexity: O(1) — four function evaluations per step
Source code in shortfx/fxNumeric/arithmetic_functions.py
safe_division_for_context(numerator: Union[int, float], denominator: Union[int, float], return_float: bool = True) -> Union[int, float]
¶
Performs division selecting the appropriate operator based on context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
numerator
|
Union[int, float]
|
The dividend. |
required |
denominator
|
Union[int, float]
|
The divisor. |
required |
return_float
|
bool
|
If True, performs float division; if False, floor division. |
True
|
Returns:
| Type | Description |
|---|---|
Union[int, float]
|
Union[int, float]: The division result. |
Raises:
| Type | Description |
|---|---|
ZeroDivisionError
|
If the divisor is zero. |
Example
safe_division_for_context(5, 2, return_float=True) 2.5 safe_division_for_context(5, 2, return_float=False) 2
Cost: O(1), division according to specified type.
Source code in shortfx/fxNumeric/arithmetic_functions.py
safe_sum_with_none(num1: Union[int, float, None], num2: Union[int, float, None]) -> Union[int, float]
¶
Performs addition of two numbers, safely handling None values.
Each argument is verified; None is replaced with 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num1
|
Union[int, float, None]
|
The first number (or None). |
required |
num2
|
Union[int, float, None]
|
The second number (or None). |
required |
Returns:
| Type | Description |
|---|---|
Union[int, float]
|
Union[int, float]: The result of the addition. |
Example
safe_sum_with_none(3, 5) 8 safe_sum_with_none(3, None) 3 safe_sum_with_none(None, None) 0
Cost: O(1), addition with None handling.
Source code in shortfx/fxNumeric/arithmetic_functions.py
secant_method(f: Callable[[float], float], x0: float, x1: float, tol: float = 1e-10, max_iter: int = 100) -> float
¶
Find a root of f using the secant method.
Unlike Newton-Raphson, does not require the derivative.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Continuous function f(x). |
required |
x0
|
float
|
First initial guess. |
required |
x1
|
float
|
Second initial guess. |
required |
tol
|
float
|
Convergence tolerance. |
1e-10
|
max_iter
|
int
|
Maximum iterations. |
100
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate root. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable. |
RuntimeError
|
If convergence fails. |
Example
round(secant_method(lambda x: x**2 - 2, 1.0, 2.0), 6) 1.414214
Complexity: O(max_iter)
Source code in shortfx/fxNumeric/arithmetic_functions.py
series_sum(x: float, n: int, m: int, coefficients: List[Union[int, float]]) -> float
¶
Compute a power series sum: sum(a_i * x^(n + i*m)).
Equivalent to Excel's SERIESSUM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Base value. |
required |
n
|
int
|
Initial power of x. |
required |
m
|
int
|
Step increment for each term's power. |
required |
coefficients
|
List[Union[int, float]]
|
List of coefficients [a_0, a_1, ...]. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Sum of the power series. |
Example
series_sum(2, 0, 1, [1, 2, 3]) 17.0 series_sum(3, 1, 2, [1, 1]) 30.0
Complexity: O(len(coefficients))
Source code in shortfx/fxNumeric/arithmetic_functions.py
sign(x: Union[int, float]) -> int
¶
Returns the sign of a number as -1, 0, or 1.
Description
Returns -1 for negative numbers, 0 for zero, and 1 for positive numbers. Useful for branching logic based on value polarity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
The number to inspect. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
-1, 0, or 1. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
Usage Example
sign(-42) -1 sign(0) 0 sign(3.14) 1
Cost: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
simpsons_integrate(f: Callable[[float], float], a: float, b: float, n: int = 1000) -> float
¶
Numerically integrates f(x) from a to b using Simpson's 1/3 rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
A callable that takes a float and returns a float. |
required |
a
|
float
|
Lower bound of integration. |
required |
b
|
float
|
Upper bound of integration. |
required |
n
|
int
|
Number of sub-intervals (must be even, default 1000). |
1000
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate value of ∫f(x)dx from a to b. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable or bounds/n have wrong types. |
ValueError
|
If n is not a positive even integer. |
Example
import math round(simpsons_integrate(math.sin, 0, math.pi, 100), 10) 2.0
Complexity: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
solve_linear_system(a: List[List[float]], b: List[float]) -> List[float]
¶
Solves Ax = b via Gaussian elimination with partial pivoting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
List[List[float]]
|
Coefficient matrix (n×n). |
required |
b
|
List[float]
|
Right-hand side vector (length n). |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
Solution vector x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not numeric lists. |
ValueError
|
If matrix is singular or dimensions mismatch. |
Example
solve_linear_system([[2, 1], [1, 3]], [5, 10]) [1.0, 3.0]
Complexity: O(n^3)
Source code in shortfx/fxNumeric/arithmetic_functions.py
4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 | |
sqrt_pi(number: float) -> float
¶
Calculates the square root of (number * pi).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number
|
float
|
A non-negative number. |
required |
Returns:
| Type | Description |
|---|---|
float
|
sqrt(number * pi). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If number is negative. |
Example
round(sqrt_pi(1), 6) 1.772454 round(sqrt_pi(2), 6) 2.506628
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
square_pyramidal_number(n: int) -> int
¶
Compute the n-th square pyramidal number = n(n+1)(2n+1)/6.
Sum of first n perfect squares.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The n-th square pyramidal number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Usage Example
square_pyramidal_number(5) 55
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
square_root(x: Union[int, float]) -> float
¶
Calculates the square root of a non-negative number.
Description
Computes √x, finding the non-negative number that, when multiplied by itself, equals x. Uses optimized algorithms for fast computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Union[int, float]
|
The number for which to calculate the square root. Must be non-negative. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The square root of x. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If x is not numeric. |
ValueError
|
If x is negative (would result in complex number). |
Usage Example
from shortfx.fxNumeric.arithmetic_functions import square_root square_root(9) 3.0 square_root(25.0) 5.0 square_root(0) 0.0
Cost: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
stern_brocot(n: int) -> list[int]
¶
Generate the first n terms of the Stern-Brocot sequence.
s(0)=0, s(1)=1, s(2k)=s(k), s(2k+1)=s(k)+s(k+1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of terms to generate (≥ 1). |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
List of first n terms. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 1. |
Usage Example
stern_brocot(10) [0, 1, 1, 2, 1, 3, 2, 3, 1, 4]
Complexity: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
sum_first_n_cubes(n: int) -> int
¶
Compute 1³ + 2³ + ··· + n³.
Uses closed form: [n(n+1)/2]².
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Sum of cubes from 1 to n. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Usage Example
sum_first_n_cubes(10) 3025
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
sum_first_n_powers(n: int, k: int) -> int
¶
Compute 1^k + 2^k + ··· + n^k.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
k
|
int
|
Non-negative exponent. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Sum of k-th powers from 1 to n. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n or k is not an integer. |
ValueError
|
If n < 0 or k < 0. |
Usage Example
sum_first_n_powers(5, 3) 225
Complexity: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
sum_first_n_squares(n: int) -> int
¶
Compute 1² + 2² + ··· + n².
Uses closed form: n(n+1)(2n+1)/6.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Sum of squares from 1 to n. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Usage Example
sum_first_n_squares(10) 385
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
sum_x2my2(array_x: List[Union[int, float]], array_y: List[Union[int, float]]) -> float
¶
Returns the sum of the difference of squares of corresponding values.
Description
Computes Σ(xᵢ² - yᵢ²) for paired arrays. Equivalent to Excel SUMX2MY2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array_x
|
List[Union[int, float]]
|
First array of numbers. |
required |
array_y
|
List[Union[int, float]]
|
Second array of numbers (same length as array_x). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The sum of xᵢ² - yᵢ² for each pair. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If arrays have different lengths or are empty. |
Example
sum_x2my2([2, 3, 9, 1, 8], [6, 5, 11, 7, 5]) -55 sum_x2my2([1, 2], [3, 4]) -20.0
Complexity: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
sum_x2py2(array_x: List[Union[int, float]], array_y: List[Union[int, float]]) -> float
¶
Returns the sum of the sum of squares of corresponding values.
Description
Computes Σ(xᵢ² + yᵢ²) for paired arrays. Equivalent to Excel SUMX2PY2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array_x
|
List[Union[int, float]]
|
First array of numbers. |
required |
array_y
|
List[Union[int, float]]
|
Second array of numbers (same length as array_x). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The sum of xᵢ² + yᵢ² for each pair. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If arrays have different lengths or are empty. |
Example
sum_x2py2([2, 3, 9, 1, 8], [6, 5, 11, 7, 5]) 521 sum_x2py2([1, 2], [3, 4]) 30.0
Complexity: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
sum_xmy2(array_x: List[Union[int, float]], array_y: List[Union[int, float]]) -> float
¶
Returns the sum of squares of differences of corresponding values.
Description
Computes Σ(xᵢ - yᵢ)² for paired arrays. Equivalent to Excel SUMXMY2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array_x
|
List[Union[int, float]]
|
First array of numbers. |
required |
array_y
|
List[Union[int, float]]
|
Second array of numbers (same length as array_x). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The sum of (xᵢ - yᵢ)² for each pair. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If arrays have different lengths or are empty. |
Example
sum_xmy2([2, 3, 9, 1, 8], [6, 5, 11, 7, 5]) 79 sum_xmy2([1, 2], [3, 4]) 8.0
Complexity: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
tetrahedral_number(n: int) -> int
¶
Compute the n-th tetrahedral number T_n = n(n+1)(n+2)/6.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The n-th tetrahedral number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Usage Example
tetrahedral_number(5) 35
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
trapezoidal_integrate(f: Callable[[float], float], a: float, b: float, n: int = 1000) -> float
¶
Numerically integrates f(x) from a to b using the trapezoidal rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
A callable that takes a float and returns a float. |
required |
a
|
float
|
Lower bound of integration. |
required |
b
|
float
|
Upper bound of integration. |
required |
n
|
int
|
Number of sub-intervals (default 1000). |
1000
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate value of ∫f(x)dx from a to b. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable or bounds/n have wrong types. |
ValueError
|
If n is not positive. |
Example
import math round(trapezoidal_integrate(math.sin, 0, math.pi, 10000), 6) 2.0
Complexity: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
triangular_number(n: int) -> int
¶
Compute the n-th triangular number T_n = n(n+1)/2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Non-negative integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The n-th triangular number. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If n is not an integer. |
ValueError
|
If n < 0. |
Usage Example
triangular_number(10) 55
Complexity: O(1)
Source code in shortfx/fxNumeric/arithmetic_functions.py
true_division(numerator: Union[int, float], denominator: Union[int, float]) -> float
¶
Performs "true" (floating-point) division, always returning a float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
numerator
|
Union[int, float]
|
The dividend. |
required |
denominator
|
Union[int, float]
|
The divisor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The division result as a float. |
Raises:
| Type | Description |
|---|---|
ZeroDivisionError
|
If the divisor is zero. |
Example
true_division(5, 2) 2.5 true_division(6, 2) 3.0
Cost: O(1), floating-point division.
Source code in shortfx/fxNumeric/arithmetic_functions.py
vector_angle(vec_a: List[Union[int, float]], vec_b: List[Union[int, float]]) -> float
¶
Computes the angle in radians between two vectors.
θ = arccos((A · B) / (‖A‖ × ‖B‖)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vec_a
|
List[Union[int, float]]
|
First numeric vector. |
required |
vec_b
|
List[Union[int, float]]
|
Second numeric vector (same length). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Angle in radians in [0, π]. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If inputs are not lists of numbers. |
ValueError
|
If vectors are empty, different lengths, or either is zero. |
Example
import math round(vector_angle([1, 0], [0, 1]), 4) 1.5708
Complexity: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
vector_magnitude(vec: List[Union[int, float]]) -> float
¶
Computes the Euclidean magnitude (L2 norm) of a vector.
‖v‖ = √(Σvᵢ²).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vec
|
List[Union[int, float]]
|
A numeric vector. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The magnitude as a non-negative float. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If vector is empty. |
Example
vector_magnitude([3, 4]) 5.0
Complexity: O(n)
Source code in shortfx/fxNumeric/arithmetic_functions.py
vector_normalize(vec: List[Union[int, float]]) -> List[float]
¶
Normalizes a vector to unit length (L2 norm = 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vec
|
List[Union[int, float]]
|
A numeric vector. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
The unit vector in the same direction. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If input is not a list of numbers. |
ValueError
|
If vector is empty or is a zero vector. |
Example
vector_normalize([3, 4]) [0.6, 0.8]
Complexity: O(n)