Library
Bernoulli number
CamiMath.bernoulliB
— MethodbernoulliB(n::Integer [; arr=false [, msg=true]])
Bernoulli numbers of index n
are defined by the recurrence relation
\[ B_n = - \frac{1}{n+1}\sum_{k=0}^{n-1}\frac{(n+1)!}{k!(n+1-k)}B_k,\]
with $B_0=1$ and $B_1=-1/2$. Including $B_0$ results in the even index convention $(B_{2n+1}=0$ for $n>1)$.
arr
: output in array formatmsg
: integer-overflow protection (IOP) - warning on activation
Examples:
julia> o = [bernoulliB(n) for n=0:5]; println(o)
Rational{Int64}[1//1, -1//2, 1//6, 0//1, -1//30, 0//1]
julia> o = bernoulliB(5; arr=true); println(o)
Rational{Int64}[1//1, -1//2, 1//6, 0//1, -1//30, 0//1]
julia> o = bernoulliB(big(5); arr=true); println(o)
Rational{BigInt}[1//1, -1//2, 1//6, 0//1, -1//30, 0//1]
julia> bernoulliB(60)
IOP capture: bernoulliB(60) converted to Rational{BigInt}
-1215233140483755572040304994079820246041491//56786730
julia> n = 60;
julia> bernoulliB(n; msg=false) == bernoulliB(n; msg=false, arr=true)[end]
true
Faulhaber polynomial
CamiMath.faulhaber_polynomial
— Methodfaulhaber_polynomial(n::Integer, p::Int [; msg=true])
Faulhaber polynomial of degree p
\[ F(n,p)=\sum_{j=0}^{p}c_{j}n^{j},\]
where n
is a positive integer and the coefficients are contained in the vector $c=[c_0,⋯\ c_p]$ given by faulhaber_polynom
.
msg
: integer-overflow protection (IOP) - warning on activation
Examples:
julia> faulhaber_polynomial(3, 6)
276
julia> faulhaber_polynomial(5, 30)
IOP capture: faulhaber_polynomial(5, 30) autoconverted to Rational{BigInt}
186552813930161650665
CamiMath.faulhaber_polynom
— Methodfaulhaber_polynom(p::Integer [; msg=true])
Vector representation of the coefficients of the faulhaber_polynomial
of degree p
,
\[ c=[c_0,⋯\ c_p],\]
where $c_0=0,\ \ c_j=\frac{1}{p}{\binom{p}{p-j}}B_{p-j}$, with $j∈\{ 1,⋯\ p\}$. The $B_{p-j}$ are bernoulliB
in the even index convention (but with $B_1=+\frac{1}{2}$ rather than $-\frac{1}{2}$).
msg
: integer-overflow protection (IOP) - warning on activation
(for p > 36
)
Example:
faulhaber_polynom(6)
7-element Vector{Rational{Int64}}:
0//1
0//1
-1//12
0//1
5//12
1//2
1//6
CamiMath.faulhaber_summation
— Methodfaulhaber_summation(n::Integer, p::Int [; msg=true])
Sum of the $p^{th}$ power of the first $n$ natural numbers
\[ \sum_{k=1}^{n}k^{p}=H_{n,-p}=F(n,p+1).\]
where $H_{n,-p}$ is a harmonicNumber
of power -p
and $F(n,p)$ a faulhaber_polynomial
of power p
.
msg
: integer-overflow protection (IOP) - warning on activation
Examples:
julia> faulhaber_summation(3,5)
276
julia> faulhaber_summation(3,60)
IOP capture: faulhaber_polynom autoconverted to Rational{BigInt}
42391158276369125018901280178
Fibonacci number
CamiMath.fibonacci
— Methodfibonacci(n::Integer [; arr=false [, msg=true]])
The sequence of integers, $F_0,⋯\ F_{nmax}$, in which each element is the sum of the two preceding ones,
\[ F_n = F_{n-1}+F_{n-2}.\]
with $F_1=1$ and $F_0=0$.
arr
: output full Pascal trianglemsg
: integer-overflow protection (IOP) - warning on activation
Examples:
julia> fibonacci(92)
7540113804746346429
julia> fibonacci(93)
IOP capture: fibonaci(93) converted to BigInt
12200160415121876738
julia> o = fibonacci(10; arr=true); println(o)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
HarmonicNumber
CamiMath.harmonicNumber
— MethodharmonicNumber(n::Integer [, p=1 [; arr=false [, msg=true]]])
Sum of the $p^{th}$ power of reciprocals of the first $n$ positive integers,
\[ H_{n,p}=\sum_{k=1}^{n}\frac{1}{k^p}.\]
arr
: output in array formatmsg
: integer-overflow protection (IOP) - warning on activation
Examples:
julia> o = [harmonicNumber(n) for n=1:8]; println(o)
Rational{Int64}[1//1, 3//2, 11//6, 25//12, 137//60, 49//20, 363//140, 761//280]
julia> harmonicNumber(8; arr=true)
(1//1, 3//2, 11//6, 25//12, 137//60, 49//20, 363//140, 761//280)
julia> harmonicNumber(42)
12309312989335019//2844937529085600
julia> harmonicNumber(43)
IOP capture: harmonicNumber(43, 1) converted to Rational{BigInt}
532145396070491417//122332313750680800
julia> harmonicNumber(12) == harmonicNumber(12, 1)
true
julia> harmonicNumber(12, -3) == faulhaber_summation(12, 3)
true
julia> o = [harmonicNumber(i, 5) for i=1:4]; println(o)
Rational{Int64}[1//1, 33//32, 8051//7776, 257875//248832]
julia> o = harmonicNumber(4, 5; arr=true); println(o)
(1//1, 33//32, 8051//7776, 257875//248832)
Integer partitioning
CamiMath.canonical_partitions
— Functioncanonical_partitions(n::Int, [m=0 , [sense=rev [; header=true]]])
Canonical partition of n
in parts of maximum size m
(m
= 0 for any size)
sense
: regular (reg
) or reverse (rev
) header
: unit partition included in output
Examples:
julia> canonical_partitions(6, 0, reg; header=true)
6-element Vector{Vector{Int64}}:
[6]
[5, 1]
[4, 2]
[3, 3]
[2, 2, 2]
[1, 1, 1, 1, 1, 1]
julia> canonical_partitions(6; header=true)
6-element Vector{Vector{Int64}}:
[1, 1, 1, 1, 1, 1]
[2, 2, 2]
[3, 3]
[4, 2]
[5, 1]
[6]
julia> canonical_partitions(6)
6-element Vector{Vector{Int64}}:
[1, 1, 1, 1, 1, 1]
[2, 2, 2]
[3, 3]
[4, 2]
[5, 1]
[6]
julia> o = canonical_partitions(9, 2); println(o)
[2, 2, 2, 2, 1]
julia> o = canonical_partitions(9, 3); println(o)
[3, 3, 3]
CamiMath.integer_partitions
— Functioninteger_partitions(n [,m [; transpose=false [, count=false]]])
default
: The integer partitions of n
count
: The number of integer partitions of n
transpose
= false
(m
> 0): partitions restricted to maximum part m
= true
(m
> 0): partitions restricted to maximum length m
`
definitions:
The integer partition of the positive integer n
is a nonincreasing sequence of positive integers p1, p2,... pk whose sum is n
. The elements of the sequence are called the parts of the partition.
Examples:
julia> integer_partitions(7)
15-element Vector{Vector{Int64}}:
[1, 1, 1, 1, 1, 1, 1]
[2, 2, 2, 1]
[2, 2, 1, 1, 1]
[2, 1, 1, 1, 1, 1]
[3, 3, 1]
[3, 2, 2]
[3, 2, 1, 1]
[3, 1, 1, 1, 1]
[4, 3]
[4, 2, 1]
[4, 1, 1, 1]
[5, 2]
[5, 1, 1]
[6, 1]
[7]
julia> integer_partitions(7; count=true)
15
julia> integer_partitions(7, 4; count=true)
3
julia> integer_partitions(7, 4)
3-element Vector{Vector{Int64}}:
[4, 3]
[4, 2, 1]
[4, 1, 1, 1]
julia> integer_partitions(7, 4; transpose=true)
3-element Vector{Vector{Int64}}:
[2, 2, 2, 1]
[3, 2, 1, 1]
[4, 1, 1, 1]
Lagrange polynom of tabulated function
CamiMath.lagrange_polynom
— Methodlagrange_polynom(f::Vector{T}, start::Int, stop::Int [, sense=fwd]) where T<:Real
lagrange_polynom(f::Vector{T}, itr::UnitRange [, sense=fwd]) where T<:Real
The coefficients of the polynomial
of degree $d =$stop
-start
running through $d+1$ subsequent points of the tabulated regular function $f[n]$. For sense
= `fwd these are the points $f[n:n+d]$, for sense
= bwd
the points $f[n-k:n]$. The corresponding polynomial
is most accurate near $f[n]$.
Examples:
julia> a = [0.0, 1.0, 4.0, 9.0, 16.0, 25.0];
julia> polynom = lagrange_polynom(a, 1, 4, fwd); println(polynom)
[0.0, 0.0, 1.0, 0.0]
julia> f(x) = polynomial(polynom, x);
julia> f(0.0), f(0.5), f(1.0), f(2.0), f(3.0)
(0.0, 0.25, 1.0, 4.0, 9.0)
julia> polynom = lagrange_polynom(a, 1:4, bwd); println(polynom)
[9.0, 6.0, 1.0, -4.440892098500626e-16]
julia> f(x) = polynomial(polynom, x);
julia> f(-3.0), f(-2.5), f(-2.0), f(-1.0), f(0.0)
(1.199040866595169e-14, 0.25000000000000694, 1.0000000000000036, 4.0, 9.0)
Laguerre polynomial
CamiMath.laguerreL
— MethodlaguerreL(n::Integer, x::T [; deriv=0 [, msg=true]]) where T<:Real
Laguerre polynomal of degree n
,
\[ L_{n}(x) = \frac{1}{n!}e^{x}\frac{d^{n}}{dx^{n}}(e^{-x}x^{n}) = \sum_{k=0}^{n}(-1)^{k}\binom{n}{n-k}\frac{x^{k}}{k!} = \sum_{k=0}^{n}c_k(n)x^{k}\]
where the $c_k(n)$ are Laguerre coefficients of laguerre_polynom
.
Example:
julia> polynom = laguerre_polynom(8); println(polynom)
(1//1, -8//1, 14//1, -28//3, 35//12, -7//15, 7//180, -1//630, 1//40320)
julia> polynomial(polynom, 5)
18029//8064
julia> laguerreL(8, 5)
18029//8064
julia> (xmin, Δx, xmax) = (0, 0.1, 11);
julia> n = 8;
julia> L = [laguerreL(n, x) for x=xmin:Δx:xmax];
julia> f = Float64.(L);
plot_function(f, xmin, Δx, xmax; title="laguerre polynomial (of degree $n)")
The plot is made using CairomMakie
. NB.: plot_function
is not included in the CamiXon
package.
CamiMath.generalized_laguerreL
— Methodgeneralized_laguerreL(n::Integer, α, x::T [; deriv=0 [, msg=true]]) where T<:Real
Generalized Laguerre polynomal of degree n
for parameter α
,
\[ L_{n}^{α}(x) = \frac{1}{n!}e^{x}x^{-α}\frac{d^{n}}{dx^{n}}(e^{-x}x^{n+α}) = \sum_{k=0}^{n}(-1)^{k}\binom{n+α}{n-k}\frac{x^{k}}{k!} = \sum_{k=0}^{n}c_k(n,α)x^{k}\]
where the $c_k(n,α)$ are the generalized Laguerre coefficients of generalized_laguerre_polynom
.
Example:
julia> polynom = generalized_laguerre_polynom(5, 3); println(polynom)
Rational{Int64}[56//1, -70//1, 28//1, -14//3, 1//3, -1//120]
julia> polynomial(polynom, 10.0)
-10.666666666667311
julia> generalized_laguerreL(5, 3, 10.0)
-10.666666666667311
CamiMath.laguerre_polynom
— Methodlaguerre_polynom(n::Integer [; msg=true])
The coefficients of laguerreL
for degree n
,
\[ v(n)=[c_0, c_1, \cdots\ c_n],\]
where
\[ c_k(n) = \frac{\Gamma(n+1)}{\Gamma(k+1)}\frac{(-1)^{k}}{(n-k)!}\frac{1}{k!}\]
with $k=0,1,⋯,n$.
msg
: integer-overflow protection (IOP) - warning on activation
Example:
julia> laguerre_polynom(7)
(1//1, -7//1, 21//2, -35//6, 35//24, -7//40, 7//720, -1//5040)
CamiMath.generalized_laguerre_polynom
— Functiongeneralized_laguerre_polynom(n::Int [, α=0 [; msg=true]])
The coefficients of generalized_laguerreL
for degree n
and parameter α
,
\[ v(n, α)=[c_0, c_1, \cdots\ c_n],\]
where
\[ c_k(n, α) = \frac{\Gamma(α+n+1)}{\Gamma(α+k+1)} \frac{(-1)^{k}}{(n-k)!}\frac{1}{k!}\]
with $k=0,1,⋯,n$.
msg
: integer-overflow protection (IOP) - warning on activation
Example:
julia> o = generalized_laguerre_polynom(6,3); println(o)
Rational{Int64}[84//1, -126//1, 63//1, -14//1, 3//2, -3//40, 1//720]
julia> o = generalized_laguerre_polynom(6,3.0); println(o)
[84.0, -126.0, 63.0, -14.0, 1.5, -0.075, 0.001388888888888889]
Hermite polynomial
CamiMath.hermiteH
— MethodhermiteH(n::Integer, x::T [; deriv=0 [, msg=true]]) where T<:Real
Hermite polynomal of degree n
,
\[ H_{n}(x) = (-1)^ne^{x^2}\frac{d^{n}}{dx^{n}}(e^{-x^2}) = \sum_{k=0}^{n}(-1)^{m}\frac{n!}{k!}\frac{(2x)^{k}}{m!} = \sum_{k=0}^{n}c_k(n)x^{k}\]
where $m = (n-k)/2$ and the $c_k(n)$ are hermite coefficients of hermite_polynom
.
Example:
julia> hermite_polynom(7)
(0, -1680, 0, 3360, 0, -1344, 0, 128)
julia> polynom = hermite_polynom(8)
(1680, 0, -13440, 0, 13440, 0, -3584, 0, 256)
julia> polynomial(polynom, 5)
52065680
julia> hermiteH(8, 5)
52065680
CamiMath.hermite_polynom
— Methodhermite_polynom(n::Integer [; msg=true])
The coefficients of hermiteH
for degree n
,
\[ v(n)=[c_0, c_1, \cdots\ c_n],\]
where
\[ c_k(n) = (-1)^{m}\frac{n!}{k!}\frac{2^{k}}{m!}\]
with $m = (n-k)/2$ and $k=0,1,⋯,n$.
msg
: integer-overflow protection (IOP) - warning on activation
Example:
julia> hermite_polynom(7)
(0, -1680, 0, 3360, 0, -1344, 0, 128)
Pascal triangle
CamiMath.pascal_triangle
— Methodpascal_triangle(n::Integer [; arr=false [, msg=true]])
Row n
of Pascal triangle, $r_n = [\binom{n}{1},\cdots\ \binom{n}{n}]$
arr
: output full Pascal trianglemsg
: integer-overflow protection (IOP) - warning on activation
(for n > 10000
)
Examples:
julia> [pascal_triangle(n) for n=0:5]
6-element Vector{Vector{Int64}}:
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
julia> pascal_triangle(5; arr=true)
5-element Vector{Vector{Int64}}:
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
CamiMath.pascal_next
— Methodpascal_next(row)
Next row
of binomial coefficients of the Pascal triangle.
Example:
julia> pascal_next([1, 4, 6, 4, 1])
6-element Vector{Int64}:
1
5
10
10
5
1
Permutations
CamiMath.permutations_unique_count
— Methodpermutations_unique_count(p::Vector{Vector{Integer}}, i::Int)
Number of unique permutations of the subarray $p[i]$.
Example:
p = [[1,2,3],[2,3,1,4,3]]
permutations_unique_count(p,2)
60
Factorial
CamiMath.bigfactorial
— Methodbigfactorial(n::Int [; msg=true])
The product of all positive integers less than or equal to n
,
\[n!=n(n-1)(n-2)⋯1.\]
In addition $0!=1$ by definition. For negative integers the factorial is zero.
msg
: integer-overflow protection (IOP) - warning on activation (forn > 20
)
Examples:
julia> bigfactorial(20) == factorial(20)
true
julia> bigfactorial(21)
IOP capture: bigfactorial(21) converted to BigInt
51090942171709440000
julia> bigfactorial(21; msg=false)
51090942171709440000
julia> factorial(21)
ERROR: OverflowError: 21 is too large to look up in the table; consider using
`factorial(big(21))` instead
Pochhammer product
CamiMath.pochhammer
— Methodpochhammer(x::T, p::Int) where T<:Real
Pochhammer symbol $(x)_{p}$ for non-negative integer p
,
\[(x)_{p}=\begin{cases} 1 & p=0\\ x(x+1)(x+2)⋯(x+p-1) & p>0 \end{cases}\]
Note that $(x)_{p}=0$ for $x=0,-1,⋯\ -(p-1)$
Examples:
julia> x = [-4,-3,-2,-1, 0, 1, 2 , 3, 4];
julia> pochhammer.(x, 5) == [0, 0, 0, 0, 0, 120, 720, 2520, 6720]
true
julia> pochhammer.(x, 0) == [1, 1, 1, 1, 1, 1, 1, 1, 1]
true
julia> o = [pochhammer.([x for x=0:-1:-p],p) for p=0:5]
6-element Vector{Vector{Int64}}:
[1]
[0, -1]
[0, 0, 2]
[0, 0, 0, -6]
[0, 0, 0, 0, 24]
[0, 0, 0, 0, 0, -120]
julia> o = [pochhammer.([x for x=0:p],p) for p=0:5]
6-element Vector{Vector{Int64}}:
[1]
[0, 1]
[0, 2, 6]
[0, 6, 24, 60]
[0, 24, 120, 360, 840]
[0, 120, 720, 2520, 6720, 15120]
julia> x = Rational{BigInt}(-1, 50);
julia> pochhammer(x, 20)
-21605762356630090481082546653745369902321614221999//9536743164062500000000000000000000