more math, without numpy
In [1]:
from Goulib.notebook import * #useless here for now
from Goulib.math2 import *
In [2]:
v1=[1,2,3]
v2=[7,8,9]
dot(v1,v2)
Out[2]:
In [3]:
m1=[[1,2,3],[5,6,7],[7,8,9]]
dot(m1,v1)
Out[3]:
In [4]:
m2=transpose(m1)
dot(m1,m2)
Out[4]:
In [5]:
quad(1,3,2) # solves x^2+3*x+2 = 0
Out[5]:
In [6]:
quad(1,2,3,allow_complex=True) # solves x^2+2*x+3 = 0
Out[6]:
In [7]:
# in fact numpy.linalg.matrix_power has a bug for large powers
# https://github.com/numpy/numpy/issues/5166
import numpy as np
print(np.linalg.matrix_power([[1,2],[1,0]],100))
#but Goulib.math2.matrix_power is correct:
print(matrix_power([[1,2],[1,0]],100))
see OEIS example with many sequences calculated from math2 functions
In [8]:
fibonacci(int(1E18),1000000007) # 1'000'000'000'000'000'000-th fibonacci number almost instantaneously
Out[8]:
In [9]:
gcd(163231, 152057, 135749) # gcd of n numbers
Out[9]:
In [10]:
#extended GCD
#https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
x,y=158179,1729154
gcd,a,b=xgcd(x,y)
gcd,a,b, a*x+b*y==gcd
Out[10]:
In [11]:
primes(10) # n first primes
Out[11]:
In [12]:
sieve(50) # primes up to n
Out[12]:
In [13]:
is_prime(4547337172376300111955330758342147474062293202868155909489)
Out[13]:
In [14]:
list(prime_factors(1548))
Out[14]:
In [15]:
list(factorize(1548)) # group prime factors in a^b tuples
Out[15]:
In [16]:
sorted(list(divisors(1548)))
Out[16]:
In [17]:
from Goulib.itertools2 import first
first(filter(is_pandigital,fibonacci_gen())) #nice, isn't it ?
Out[17]:
In [18]:
levenshtein('hello','world')
Out[18]:
In [19]:
sets_levenshtein(set('hello'),set('world'))
Out[19]:
In [20]:
get_cardinal_name(1548)
Out[20]: