goulib.math2

more math than math standard library, without numpy

Functions

_count(n, p)

abundance(n)

accsum(it)

Yield accumulated sums of iterable: accsum(count(1)) -> 1,3,6,10,...

allclose(a, b[, rel_tol, abs_tol])

angle(u, v[, unit])

baby_step_giant_step(y, a, n)

solves Discrete Logarithm Problem (DLP) y = a**x mod n

bernouilli(n[, init])

bernouilli_gen([init])

generator of Bernouilli numbers

bigomega(n)

Number of prime divisors of n counted with multiplicity

binomial(n, k)

binomial coefficient "n choose k" :param: n, k int :return: int, number of ways to chose n items in k, unordered

binomial_exponent(n, k, p)

bouncy(n[, up, down])

carmichael(n)

Carmichael function :return : int smallest positive integer m such that a^m mod n = 1 for every integer a between 1 and n that is coprime to n.

carries(a, b[, base, pos])

catalan(n)

Catalan numbers: C(n) = binomial(2n,n)/(n+1) = (2n)!/(n!(n+1)!).

catalan_gen()

Generate Catalan numbers: C(n) = binomial(2n,n)/(n+1) = (2n)!/(n!(n+1)!).

ceildiv(a, b)

chakravala(n)

solves x^2 - n*y^2 = 1 for x,y integers

chinese_remainder(m, a)

http://en.wikipedia.org/wiki/Chinese_remainder_theorem

choose(n, k)

binomial coefficient "n choose k" :param: n, k int :return: int, number of ways to chose n items in k, unordered

cmp(x, y)

Compare the two objects x and y and return an integer according to the outcome.

collatz(n)

collatz_gen([n])

collatz_period(n)

coprime(*args)

coprimes_gen(limit)

generates coprime pairs using Farey sequence

cousin_primes()

cumsum(it)

Yield accumulated sums of iterable: accsum(count(1)) -> 1,3,6,10,...

de_bruijn(k, n)

De Bruijn sequence for alphabet k and subsequences of length n.

diag(v)

Create a two-dimensional array with the flattened input as a diagonal.

digits(num[, base, rev])

digits_gen(num[, base])

generates int digits of num in base BACKWARDS

digsum(num[, f, base])

sum of digits

dist(a, b[, norm])

divisors(n)

dot(a, b[, default])

dot product

dot_mm(a, b[, default])

dot product for matrices

dot_mv(a, b[, default])

dot product for vectors

dot_vv(a, b[, default])

dot product for vectors

ecadd(p1, p2, p0, n)

ecdub(p, A, n)

ecmul(m, p, A, n)

erathostene(n)

euclid_gen()

generates Euclid numbers: 1 + product of the first n primes

euler_phi(n)

Euler totient function

eye(n)

factor_ecm(n[, B1, B2])

Factors n using the elliptic curve method, using Montgomery curves and an algorithm analogous to the two-phase variant of Pollard’s p-1 method.

factorial2(n)

factorial_gen([f])

Generator of factorial :param f: optional function to apply at each step

factorialk(n, k)

Multifactorial of n of order k, n(!!...!).

factorize(n)

find the prime factors of n along with their frequencies.

factors(n)

faulhaber(n, p)

sum of the p-th powers of the first n positive integers

fibonacci(n[, mod])

fibonacci series n-th element :param n: int can be extremely high, like 1e19 ! :param mod: int optional modulo

fibonacci_gen([max, mod])

Generate fibonacci serie (k=2)

format(x[, decimals])

formats a float with given number of decimals, but not an int

gamma_inverse(x)

Inverse the gamma function.

gcd(*args)

greatest common divisor of an arbitrary number of args

get_cardinal_name(num[, numbers])

Get cardinal name for number (0 to 1 million)

gpf(n)

greatest prime factor

hamming(s1, s2)

Calculate the Hamming distance between two iterables

heptagonal(n)

hexagonal(n)

icbrt(n)

integer cubic root

identity(n)

ilog(a, b[, upper_bound])

discrete logarithm x such that b^x=a

int_base(num, base)

int_or_float(x[, rel_tol, abs_tol])

integer_exponent(a[, b])

introot(n[, r])

integer r-th root

ipow(x, y[, z])

is_anagram(num1, num2[, base])

Check if 'num1' and 'num2' have the same digits in base

is_complex(x)

is_fibonacci(n)

returns True if n is in Fibonacci series

is_happy(n)

is_heptagonal(n)

is_hexagonal(n)

is_integer(x[, rel_tol, abs_tol])

is_lychrel(n[, limit])

is_multiple(n, factors)

return True if n has ONLY factors as prime factors

is_number(x)

is_octagonal(n)

is_palindromic(num[, base])

Check if 'num' in base 'base' is a palindrome, that's it, if it can be read equally from left to right and right to left.

is_pandigital(num[, base])

is_pentagonal(n)

is_perfect(n)

is_power(n)

is_prime(n[, oneisprime, tb, eb, mrb])

main primality test.

is_prime_euler(n[, eb])

Euler's primality test

is_primitive_root(x, m[, s])

returns True if x is a primitive root of m

is_pythagorean_triple(a, b, c)

is_real(x)

is_square(n)

is_triangle(x)

is_triangular(x)

isqrt(n)

integer square root

jacobi(a, p)

Computes the Jacobi symbol (a|p), where p is a positive odd number.

kempner(n)

"Kempner function, also called Smarandache function

kfibonacci(k[, mod])

k-fibonacci series n-th element

kfibonacci_gen(k[, init, max, mod])

Generate k-fibonacci serie

lambertW(z)

Lambert W function, principal branch.

lcm(*args)

least common multiple of any number of integers

legendre(a, p)

Functions to comptue the Legendre symbol (a|p).

legendre2(a, p)

Functions to comptue the Legendre symbol (a|p).

levenshtein(seq1, seq2)

levenshtein distance

log_binomial(n, k)

log_factorial(n)

longint(mantissa, exponent)

lpf(n)

greatest prime factor

lucasU(p, q)

lucasV(p, q)

lucas_lehmer(p)

Lucas Lehmer primality test for Mersenne exponent p

lucky_gen()

generates lucky numbers :see: https://en.wikipedia.org/wiki/Lucky_number :see: https://oeis.org/A000959

lychrel_count(n[, limit])

number of lychrel iterations before n becomes palindromic

lychrel_seq(n)

matrix_power(M, power[, mod])

maximum(m)

Compare N arrays and returns a new array containing the element-wise maxima

minimum(m)

Compare N arrays and returns a new array containing the element-wise minima

mlucas(v, a, n)

Helper function for williams_pp1().

mod_binomial(n, k, m[, q])

calculates C(n,k) mod m for large n,k,m

mod_div(a, b, m)

mod_fac(n, mod[, mod_is_prime])

modular factorial : return n! % modulo if module is prime, use Wilson's theorem https://en.wikipedia.org/wiki/Wilson%27s_theorem

mod_fact(n, m)

mod_inv(a, b)

mod_matmul(A, B[, mod])

mod_matpow(M, power[, mod])

mod_sqrt(n, p)

modular sqrt(n) mod p

moebius(n)

Möbius (or Moebius) function mu(n).

mul(nums[, init])

multiply(x, y)

Karatsuba fast multiplication algorithm

ncombinations(n, k)

binomial coefficient "n choose k" :param: n, k int :return: int, number of ways to chose n items in k, unordered

nextprime(n)

Determines, with some semblance of efficiency, the least prime number strictly greater than n.

norm(v[, order])

norm_1(v)

norm_2(v)

norm_inf(v)

num_from_digits(digits[, base])

number_of_digits(num[, base])

Return number of digits of num (expressed in base 'base')

number_of_divisors(n)

octagonal(n)

omega(n)

Number of distinct primes dividing n

partition(n)

The partition function p(n)

partitionsQ(n[, d])

pascal_gen()

Pascal's triangle read by rows: C(n,k) = binomial(n,k) = n!/(k!*(n-k)!), 0<=k<=n.

pentagonal(n)

pfactor(n)

Helper function for sprp.

pi_digits_gen()

generates pi digits as a sequence of INTEGERS ! using Jeremy Gibbons spigot generator

pisano_cycle(mod)

pisano_period(mod)

pollardRho_brent(n)

Brent’s improvement on Pollard’s rho algorithm.

pollard_pm1(n[, B1, B2])

Pollard’s p+1 algorithm, two-phase version.

polygonal(s, n)

pow(x, y[, z])

power_tower(v)

powertrain(n)

prevprime(n)

Determines, very inefficiently, the largest prime number strictly smaller than n.

prime_divisors(num[, start])

generates unique prime divisors (ordered) of num

prime_factors(num[, start])

generates all prime factors (ordered) of num

prime_ktuple(constellation)

generates tuples of primes with specified differences

primes(n)

memoized list of n first primes

primes_gen([start, stop])

generate prime numbers from start

primitive_root_gen(m)

generate primitive roots modulo m

primitive_roots(modulo)

primitive_triples()

generates primitive Pythagorean triplets x<y<z

proper_divisors(n)

proportional(nseats, votes)

assign n seats proportionaly to votes using the https://en.wikipedia.org/wiki/Hagenbach-Bischoff_quota method

pyramidal(n)

quad(a, b, c[, allow_complex])

solves quadratic equations aX^2+bX+c=0

random_prime(bits)

returns a random number of the specified bit length

rational_cycle(num, den)

periodic part of the decimal expansion of num/den.

rational_form(numerator, denominator)

information about the decimal representation of a rational number.

rational_str(n, d)

rectangular_repartition(x, n, h)

divide 1 into n fractions such that:

recurrence(factors, values[, cst, max, mod])

general generator for recurrences

repunit(n[, base, digit])

repunit_gen([base, digit])

generate repunits

reverse(i)

rint(v)

sat(x[, low, high])

saturates x between low and high

sets_dist(a, b)

sets_levenshtein(a, b)

levenshtein distance on sets

sexy_prime_quadruplets()

sexy_prime_triplets()

sexy_primes()

sieve(n[, oneisprime])

prime numbers from 2 to a prime < n

sigma(n)

sign(number)

sin_over_x(x)

numerically safe sin(x)/x

slerp(u, v, t)

spherical linear interpolation

sprp(n, a[, s, d])

Checks n for primality using the Strong Probable Primality Test to base a.

sqrt(n)

square root :return: int, float or complex depending on n

square(n)

str_base(num[, base, numerals])

sum_of_cubes(n)

sum_of_squares(n)

tetrahedral(n)

totient(n)

Euler totient function

trailing_zeros(a[, b])

transpose(m)

triangle(n)

triangular(n)

triangular_repartition(x, n)

divide 1 into n fractions such that:

triples()

generates all Pythagorean triplets triplets x<y<z sorted by hypotenuse z, then longest side y

twin_primes()

vecadd(a, b[, fillvalue])

addition of vectors of inequal lengths

veccompare(a, b)

compare values in 2 lists.

vecdiv(a, b)

quotient of vectors of inequal lengths

vecmul(a, b)

product of vectors of inequal lengths

vecneg(a)

unary negation

vecsub(a, b[, fillvalue])

substraction of vectors of inequal lengths

vecunit(v[, norm])

williams_pp1(n)

Williams’ p+1 algorithm.

xgcd(a, b)

Extended GCD

zeros(shape)

Classes

Sieve(init[, f])

General sieve