Formal power series: Difference between revisions
m
→{{header|Julia}}
Line 1,093:
=={{header|Julia}}==
'''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.
Base.
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 =
a, s = next(itr, s)▼
print(io, a)
a, s =
@printf(io, " %s %s⋅x",
ifelse(sign(a) ≥ 0, '+', '-'), abs(a))
local i = 2
while
a, s =
@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.
function Base.next(fps::MinusFPS, st)▼
▲ v, s = next(fps.a, st)
end
return -v, s
end
Line 1,146 ⟶ 1,149:
Base.:-(a::AbstractFPS, b::AbstractFPS) = a + (-b)
function Base.
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
stateA, stateB = st
valueA, stateA =
valueB, 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.
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
stateA, stateB, listA, listB = st
valueA, stateA =
valueB, stateB =
push!(listA, valueA)
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.
_, s =
▲ return n, s
end
function Base.
n, s = st
n += one(n)
v, s =
return n * v, (n, s)
end
Line 1,197 ⟶ 1,206:
IntegratedFPS{Rational{T},typeof(fps)}(fps, k)
function Base.
if st == (0, 0)
return fps.k, (one(T), 0)
end
n, s = st
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.
▲ 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.
struct SineFPS{T} <: AbstractFPS{T} end
SineFPS() = SineFPS{Rational{Int}}()
function Base.
n, fac, s = st
local r::T
Line 1,246 ⟶ 1,258:
struct CosineFPS{T} <: AbstractFPS{T} end
CosineFPS() = CosineFPS{Rational{Int}}()
function Base.
n, fac, s = st
local r::T
Line 1,264 ⟶ 1,275:
'''Main''':
<lang julia>
@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>{{out}}▼
<pre>
▲{{out}}
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>
=={{header|Kotlin}}==
|