Formal power series: Difference between revisions

m
Line 1,093:
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
'''Module''':
<lang julia>module FormalPowerSeries
 
using Printf
import Base.iterate, Base.eltype, Base.one, Base.show, Base.IteratorSize
import Base.IteratorEltype, Base.length, Base.size, Base.convert
 
_div(a, b) = a / b
Line 1,103 ⟶ 1,105:
abstract type AbstractFPS{T<:Number} end
 
Base.iteratorsizeIteratorSize(::AbstractFPS) = Base.IsInfinite()
Base.doneIteratorEltype(::AbstractFPS, ::Any) = falseBase.HasEltype()
Base.iteratoreltype(::AbstractFPS) = Base.HasEltype()
Base.eltype(::AbstractFPS{T}) where T = T
Base.one(::AbstractFPS{T}) where T = ConstantFPS(one(T))
Line 1,111 ⟶ 1,112:
function Base.show(io::IO, fps::AbstractFPS{T}) where T
itr = Iterators.take(fps, 8)
a, s = startiterate(itr)
a, s = next(itr, s)
print(io, a)
a, s = nextiterate(itr, s)
@printf(io, " %s %s⋅x",
ifelse(sign(a) ≥ 0, '+', '-'), abs(a))
local i = 2
while !done(it = iterate(itr, s)) != nothing
a, s = next(itr, s)it
@printf(io, " %s %s⋅x^%i",
ifelse(sign(a) ≥ 0, '+', '-'), abs(a), i)
Line 1,132:
Base.:-(a::AbstractFPS{T}) where T = MinusFPS{T,typeof(a)}(a)
 
function Base.startiterate(fps::MinusFPS) = start(fps.a)
v, s = nextiterate(fps.a, st)
function Base.next(fps::MinusFPS, st)
return n-v, s
v, s = next(fps.a, st)
end
function Base.nextiterate(fps::MinusFPS, st)
av, s = nextiterate(itrfps.a, sst)
return -v, s
end
Line 1,146 ⟶ 1,149:
Base.:-(a::AbstractFPS, b::AbstractFPS) = a + (-b)
 
function Base.startiterate(fps::SumFPS{T,A,B}) =where (start(fps.a){T, start(fps.b))A,B}
a1, s1 = iterate(fps.a)
function Base.next(fps::SumFPS{T,A,B}, st) where {T,A,B}
a2, s2 = iterate(fps.b)
return T(a1 + a2), (s1, s2)
end
function Base.nextiterate(fps::SumFPS{T,A,B}, st) where {T,A,B}
stateA, stateB = st
valueA, stateA = nextiterate(fps.a, stateA)
valueB, stateB = nextiterate(fps.b, stateB)
return T(valueA + valueB), (stateA, stateB)
end
Line 1,161 ⟶ 1,168:
ProductFPS{promote_type(A, B),typeof(a),typeof(b)}(a, b)
 
function Base.startiterate(fps::ProductFPS{T}) where T = (start(fps.a), start(fps.b), T[], T[])
a1, s1 = iterate(fps.a)
function Base.next(fps::ProductFPS{T,A,B}, st) where {T,A,B}
a2, s2 = iterate(fps.b)
T(sum(a1 .* a2)), (s1, s2, T[a1], T[a2])
end
function Base.nextiterate(fps::ProductFPS{T,A,B}, st) where {T,A,B}
stateA, stateB, listA, listB = st
valueA, stateA = nextiterate(fps.a, stateA)
valueB, stateB = nextiterate(fps.b, stateB)
push!(listA, valueA)
unshiftpushfirst!(listB, valueB)
return T(sum(listA .* listB)), (stateA, stateB, listA, listB)
end
Line 1,176 ⟶ 1,187:
differentiate(fps::AbstractFPS{T}) where T = DifferentiatedFPS{T,typeof(fps)}(fps)
 
function Base.startiterate(fps::DifferentiatedFPS{T,A}) where {T,A}
_, s = startiterate(fps.a)
_,return s = nextBase.iterate(fps.a, (zero(T), s))
n = zero(T)
return n, s
end
function Base.nextiterate(fps::DifferentiatedFPS{T,A}, st) where {T,A}
n, s = st
n += one(n)
v, s = nextiterate(fps.a, s)
return n * v, (n, s)
end
Line 1,197 ⟶ 1,206:
IntegratedFPS{Rational{T},typeof(fps)}(fps, k)
 
function Base.startiterate(fps::IntegratedFPS{T,A}, st=(0, 0)) where {T,A} = zero(T), start(fps.a)
if st == (0, 0)
function Base.next(fps::IntegratedFPS{T,A}, st) where {T,A}
return fps.k, (one(T), 0)
end
n, s = st
iszero(if n) &&== return fps.k, (one(n), sT)
v, s = nextiterate(fps.a, s)
else
v, s = iterate(fps.a, s)
end
r::T = _div(v, n)
n += one(n)
Line 1,212 ⟶ 1,226:
v::NTuple{N,T} where N
end
Base.startiterate(fps::FiniteFPS){T}, st= 1) where T =
st > endoflastindex(fps.v) ? (zero(T), st) : (fps.v[st], st + 1)
Base.next(fps::FiniteFPS{T}, st) where T =
st > endof(fps.v) ? (zero(T), st) : (fps.v[st], st + 1)
Base.convert(::Type{FiniteFPS}, x::Real) = FiniteFPS{typeof(x)}((x,))
FiniteFPS(r) = convert(FiniteFPS, r)
for op in (:+, :-, :*)
@eval Base.$op(x::Number, a::AbstractFPS) = $op(FiniteFPS(x), a)
Line 1,224 ⟶ 1,238:
k::T
end
Base.startiterate(c::ConstantFPS, ::Any=nothing) = c.k, nothing
Base.next(c::ConstantFPS, ::Any) = c.k, nothing
 
struct SineFPS{T} <: AbstractFPS{T} end
SineFPS() = SineFPS{Rational{Int}}()
function Base.startiterate(::SineFPS){T}, st= (0, 1, 1)) where T
function Base.next(::SineFPS{T}, st) where T
n, fac, s = st
local r::T
Line 1,246 ⟶ 1,258:
struct CosineFPS{T} <: AbstractFPS{T} end
CosineFPS() = CosineFPS{Rational{Int}}()
function Base.startiterate(::CosineFPS){T}, st= (0, 1, 1)) where T
function Base.next(::CosineFPS{T}, st) where T
n, fac, s = st
local r::T
Line 1,264 ⟶ 1,275:
 
'''Main''':
<lang julia>@show cosine =using .FormalPowerSeries.CosineFPS()
 
@show cosine = FormalPowerSeries.CosineFPS()
@show sine = FormalPowerSeries.SineFPS()
 
intcosine = FormalPowerSeries.integrate(cosine)
intsine = FormalPowerSeries.integrate(sine)
uminintsine = 1 - FormalPowerSeries.integrate(sine)
 
Line 1,278 ⟶ 1,292:
 
@assert coefsine == coefintcosine "The integral of cos should be sin"
@assert coefcosine == coefuminintsine "1 minus the integral of sin should be cos"</lang>
</lang>{{out}}
 
<pre>
{{out}}
<pre>cosine = FormalPowerSeries.CosineFPS() = 1//1 + 0//1⋅x - 1//2⋅x^2 + 0//1⋅x^3 + 1//24⋅x^4 + 0//1⋅x^5 - 1//720⋅x^6 + 0//1⋅x^7...
sine = FormalPowerSeries.SineFPS() = 0//1 + 1//1⋅x + 0//1⋅x^2 - 1//6⋅x^3 + 0//1⋅x^4 + 1//120⋅x^5 + 0//1⋅x^6 - 1//5040⋅x^7...</pre>
</pre>
 
=={{header|Kotlin}}==
4,102

edits