Skip to content

Commit

Permalink
taylor fix api
Browse files Browse the repository at this point in the history
  • Loading branch information
fsxbhyy committed Oct 28, 2023
1 parent 0ecf012 commit a8a18ef
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 92 deletions.
11 changes: 6 additions & 5 deletions example/taylor_expansion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ using FeynmanDiagram.Taylor
using FeynmanDiagram.ComputationalGraphs:
eval!, forwardAD, node_derivative, backAD, build_all_leaf_derivative, count_operation
using FeynmanDiagram.Utility:
taylorexpansion!, build_derivative_backAD!
taylorexpansion, build_derivative_backAD!
g1 = Graph([])
g2 = Graph([])
g3 = Graph([]) #, factor=2.0)
g4 = Graph([])
g5 = Graph([])
g6 = Graph([])
G3 = g1
G4 = 1.0 * g1 * g1
G4 = 1.0 * g1 * g2
G5 = 1.0 * (3.0 * G3 + 0.5 * G4)
#G6 = (0.5 * g1 * g2 + 0.5 * g2 * g3)
#G6 = (g1 + g2) * (0.5 * g1 + g3) * g1 # (0.5 * g1 + g3)
Expand All @@ -23,10 +23,11 @@ using FeynmanDiagram.Taylor:


# set_variables("x y", order=3)
# @time T5 = taylorexpansion!(G5)
# @time T5 = taylorexpansion(G5)
# print(T5)
set_variables("x y z a", order=7)
@time T5 = taylorexpansion!(G6)
set_variables("x y", order=[3, 2])
#set_variables("x y z a", order=[1, 2, 3, 2])
@time T5 = taylorexpansion(G6)
#order = [0, 0, 0, 0, 0, 0]
#@time print(T5.coeffs[order])
print("$(count_operation(T5.coeffs))\n")
Expand Down
47 changes: 38 additions & 9 deletions src/TaylorSeries/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ function Base.:-(g1::TaylorSeries{T}, c::S) where {T,S<:Number}
end

"""
function taylor_binomial(o1::Array{Int,1}, o2::Array{Int,1})
function taylor_binomial(o1::Vector{Int}, o2::Vector{Int})
Return the taylor binomial prefactor when product two high-order derivatives with order o1 and o2.
# Arguments:
- `o1` Order of first derivative
- `o2` Order of second derivative
"""
function taylor_binomial(o1::Array{Int,1}, o2::Array{Int,1})
function taylor_binomial(o1::Vector{Int}, o2::Vector{Int})
@assert length(o1) == length(o2)
result = 1
for i in eachindex(o1)
Expand All @@ -143,14 +143,14 @@ end


"""
function taylor_factorial(o::Array{Int,1})
function taylor_factorial(o::Vector{Int})
Return the taylor factorial prefactor with order o.
# Arguments:
- `o` Order of the taylor coefficient
"""
function taylor_factorial(o::Array{Int,1})
function taylor_factorial(o::Vector{Int})
result = 1
for i in eachindex(o)
result *= factorial(o[i])
Expand All @@ -172,7 +172,7 @@ function Base.:*(g1::TaylorSeries{T}, g2::TaylorSeries{T}) where {T}
for (order1, coeff1) in g1.coeffs
for (order2, coeff2) in g2.coeffs
order = order1 + order2
if sum(order) <= _params_Taylor_.order
if all(order .<= get_order())
#combination_coeff = taylor_binomial(order1, order2)
if haskey(g.coeffs, order)
#g.coeffs[order] += combination_coeff * coeff1 * coeff2
Expand All @@ -187,17 +187,46 @@ function Base.:*(g1::TaylorSeries{T}, g2::TaylorSeries{T}) where {T}
return g
end

# """
# function Base.:*(g1::TaylorSeries{T}, g2::TaylorSeries{T}) where {T}

# Returns a taylor series `g1 * g2` representing the product of `g2` with `g1`.

# # Arguments:
# - `g1` First taylor series
# - `g2` Second taylor series
# """
# function multi_product(glist::Vector{TaylorSeries{T}}) where {T}
# result = TaylorSeries{T}()
# idxtuple = (keys(glist.coeffs) for g in glist)
# for idx in collect(Iterators.product(idxtuple...))
# orderidx = collect(orderidx)
# totalorder = sum(glist[i].coeffs[orderidx[i]] for i in eachindex(glist))
# if all(totalorder .<= get_order())
# #combination_coeff = taylor_binomial(order1, order2)
# if haskey(g.coeffs, order)
# #g.coeffs[order] += combination_coeff * coeff1 * coeff2
# g.coeffs[order] += coeff1 * coeff2
# else
# #g.coeffs[order] = combination_coeff * coeff1 * coeff2
# g.coeffs[order] = coeff1 * coeff2
# end

# end
# end
# return g
# end

"""
function getcoeff(g::TaylorSeries, order::Array{Int,1})
function getcoeff(g::TaylorSeries, order::Vector{Int})
Return the taylor coefficients with given order in taylor series g.
# Arguments:
- `g` Taylor series
- `order` Order of target coefficients
"""
function getcoeff(g::TaylorSeries, order::Array{Int,1})
function getcoeff(g::TaylorSeries, order::Vector{Int})
if haskey(g.coeffs, order)
return g.coeffs[order]
else
Expand All @@ -206,15 +235,15 @@ function getcoeff(g::TaylorSeries, order::Array{Int,1})
end

"""
function getderivative(g::TaylorSeries, order::Array{Int,1})
function getderivative(g::TaylorSeries, order::Vector{Int})
Return the derivative with given order in taylor series g.
# Arguments:
- `g` Taylor series
- `order` Order of derivative
"""
function getderivative(g::TaylorSeries, order::Array{Int,1})
function getderivative(g::TaylorSeries, order::Vector{Int})
if haskey(g.coeffs, order)
return taylor_factorial(order) * g.coeffs[order]
else
Expand Down
10 changes: 5 additions & 5 deletions src/TaylorSeries/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
# Members:
- `name::Symbol` name of the diagram
- `coeffs::Dict{Array{Int,1},T}` The taylor expansion coefficients. The integer array define the order of corresponding coefficient.
- `coeffs::Dict{Vector{Int},T}` The taylor expansion coefficients. The integer array define the order of corresponding coefficient.
"""
mutable struct TaylorSeries{T}
name::String # "" by default
coeffs::Dict{Array{Int,1},T}
coeffs::Dict{Vector{Int},T}

"""
function TaylorSeries{T}(coeffs::Dict{Array{Int,1},T}=Dict{Array{Int,1},T}(), name::String="") where {T}
function TaylorSeries{T}(coeffs::Dict{Vector{Int},T}=Dict{Vector{Int},T}(), name::String="") where {T}
Create a TaylorSeries based on given coefficients.
"""
function TaylorSeries{T}(coeffs::Dict{Array{Int,1},T}=Dict{Array{Int,1},T}(), name::String="") where {T}
function TaylorSeries{T}(coeffs::Dict{Vector{Int},T}=Dict{Vector{Int},T}(), name::String="") where {T}
return new{T}(name, coeffs)
end
end
Expand All @@ -33,7 +33,7 @@ function TaylorSeries(::Type{T}, nv::Int) where {T}
@assert 0 < nv get_numvars()
v = zeros(Int, get_numvars())
@inbounds v[nv] = 1
return TaylorSeries{T}(Dict{Array{Int,1},T}(v => one(T)))
return TaylorSeries{T}(Dict{Vector{Int},T}(v => one(T)))
end
TaylorSeries(nv::Int) = TaylorSeries(Float64, nv)

Expand Down
11 changes: 6 additions & 5 deletions src/TaylorSeries/parameter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
These parameters can be changed using [`set_variables`](@ref)
"""
mutable struct ParamsTaylor
order::Int
order::Vector{Int}
num_vars::Int
variable_names::Vector{String}
variable_symbols::Vector{Symbol}
Expand All @@ -23,10 +23,11 @@ end

ParamsTaylor(order, num_vars, variable_names) = ParamsTaylor(order, num_vars, variable_names, Symbol.(variable_names))

const _params_Taylor_ = ParamsTaylor(6, 2, ["x₁", "x₂"])
const _params_Taylor_ = ParamsTaylor([2, 2], 2, ["x₁", "x₂"])

## Utilities to get the maximum order, number of variables, their names and symbols
get_order() = _params_Taylor_.order
get_order(idx::Int) = _params_Taylor_.order[idx]
get_numvars() = _params_Taylor_.num_vars
get_variable_names() = _params_Taylor_.variable_names
get_variable_symbols() = _params_Taylor_.variable_symbols
Expand Down Expand Up @@ -69,9 +70,9 @@ julia> set_variables("x", order=6, numvars=2)
"""
function set_variables(::Type{R}, names::Vector{T}; order=get_order()) where
{R,T<:AbstractString}

order 1 || error("Order must be at least 1")

for o in order
o 1 || error("Order must be at least 1")
end
num_vars = length(names)
num_vars 1 || error("Number of variables must be at least 1")

Expand Down
5 changes: 4 additions & 1 deletion src/TaylorSeries/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ end
function pretty_print(a::TaylorSeries{T}) where {T}
#z = zero(a[0])
space = string("")
# bigO::String = bigOnotation[end] ?
# string(" + 𝒪(‖x‖", superscriptify(_params_Taylor_.order + 1), ")") :
# string("")
bigO::String = bigOnotation[end] ?
string(" + 𝒪(‖x‖", superscriptify(_params_Taylor_.order + 1), ")") :
string(" + 𝒪(", [_params_Taylor_.variable_names[i] * superscriptify(_params_Taylor_.order[i] + 1) for i in eachindex(_params_Taylor_.order)]..., ")") :
string("")
# iszero(a) && return string(space, z, space, bigO)
# strout::String = space
Expand Down
Loading

0 comments on commit a8a18ef

Please sign in to comment.