Skip to content

Commit

Permalink
define ternary functions
Browse files Browse the repository at this point in the history
  • Loading branch information
longemen3000 committed Aug 30, 2023
1 parent 9cca572 commit 6cdc9e6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ForwardDiffOverMeasurements"
uuid = "9eb8ae02-809e-4b16-afbc-1cadb820c769"
authors = ["longemen3000 <longemen3000@gmail.com> and contributors"]
version = "0.1.2"
version = "0.1.3"

[deps]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Expand Down
70 changes: 67 additions & 3 deletions src/ForwardDiffOverMeasurements.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module ForwardDiffOverMeasurements

using ForwardDiff: Dual, DiffRules, NaNMath, LogExpFunctions, SpecialFunctions
using ForwardDiff: Dual, DiffRules, NaNMath, LogExpFunctions, SpecialFunctions,
using Measurements: Measurement
import Base: +,-,/,*,promote_rule
import Base: +,-,/,*,promote_rule
using ForwardDiff: AMBIGUOUS_TYPES, partials, values
using ForwardDiff: ForwardDiff

function promote_rule(::Type{Measurement{V}}, ::Type{Dual{T, V, N}}) where {T,V,N}
Dual{Measurement{T}, V, N}
Expand All @@ -20,14 +22,42 @@ function overload_ambiguous_binary(M,f)
∂y = Dual{Tx}(y)
$Mf(x,∂y)
end

@inline function $Mf(x::Measurement,y::Dual{Ty}) where {Ty}
∂x = Dual{Ty}(x)
$Mf(∂x,y)
end
end
end

macro define_ternary_dual_op2(f, xyz_body, xy_body, xz_body, yz_body, x_body, y_body, z_body)
FD = ForwardDiff
R = Measurement
defs = quote
@inline $(f)(x::$FD.Dual{Txy}, y::$FD.Dual{Txy}, z::$R) where {Txy} = $xy_body
@inline $(f)(x::$FD.Dual{Tx}, y::$FD.Dual{Ty}, z::$R) where {Tx, Ty} = Ty Tx ? $x_body : $y_body
@inline $(f)(x::$FD.Dual{Txz}, y::$R, z::$FD.Dual{Txz}) where {Txz} = $xz_body
@inline $(f)(x::$FD.Dual{Tx}, y::$R, z::$FD.Dual{Tz}) where {Tx,Tz} = Tz Tx ? $x_body : $z_body
@inline $(f)(x::$R, y::$FD.Dual{Tyz}, z::$FD.Dual{Tyz}) where {Tyz} = $yz_body
@inline $(f)(x::$R, y::$FD.Dual{Ty}, z::$FD.Dual{Tz}) where {Ty,Tz} = Tz Ty ? $y_body : $z_body
end
for Q in AMBIGUOUS_TYPES
expr = quote
@inline $(f)(x::$FD.Dual{Tx}, y::$R, z::$Q) where {Tx} = $x_body
@inline $(f)(x::$R, y::$FD.Dual{Ty}, z::$Q) where {Ty} = $y_body
@inline $(f)(x::$R, y::$Q, z::$FD.Dual{Tz}) where {Tz} = $z_body
end
append!(defs.args, expr.args)
end
expr = quote
@inline $(f)(x::$FD.Dual{Tx}, y::$R, z::$R) where {Tx} = $x_body
@inline $(f)(x::$R, y::$FD.Dual{Ty}, z::$R) where {Ty} = $y_body
@inline $(f)(x::$R, y::$R, z::$FD.Dual{Tz}) where {Tz} = $z_body
end
append!(defs.args, expr.args)
return esc(defs)
end

#use DiffRules.jl rules

for (M, f, arity) in DiffRules.diffrules(filter_modules = nothing)
Expand All @@ -44,4 +74,38 @@ for (M, f, arity) in DiffRules.diffrules(filter_modules = nothing)
end
end

#ternary overloads
@define_ternary_dual_op2(
Base.hypot,
ForwardDiff.calc_hypot(x, y, z, Txyz),
ForwardDiff.calc_hypot(x, y, z, Txy),
ForwardDiff.calc_hypot(x, y, z, Txz),
ForwardDiff.calc_hypot(x, y, z, Tyz),
ForwardDiff.calc_hypot(x, y, z, Tx),
ForwardDiff.calc_hypot(x, y, z, Ty),
ForwardDiff.calc_hypot(x, y, z, Tz),
)

@define_ternary_dual_op2(
Base.fma,
ForwardDiff.calc_fma_xyz(x, y, z), # xyz_body
ForwardDiff.calc_fma_xy(x, y, z), # xy_body
ForwardDiff.calc_fma_xz(x, y, z), # xz_body
Base.fma(y, x, z), # yz_body
Dual{Tx}(Base.fma(value(x), y, z), partials(x) * y), # x_body
Base.fma(y, x, z), # y_body
Dual{Tz}(Base.fma(x, y, value(z)), partials(z)) # z_body
)

@define_ternary_dual_op2(
Base.muladd,
ForwardDiff.calc_muladd_xyz(x, y, z), # xyz_body
ForwardDiff.calc_muladd_xy(x, y, z), # xy_body
ForwardDiff.calc_muladd_xz(x, y, z), # xz_body
Base.muladd(y, x, z), # yz_body
Dual{Tx}(Base.muladd(value(x), y, z), partials(x) * y), # x_body
Base.muladd(y, x, z), # y_body
Dual{Tz}(Base.muladd(x, y, value(z)), partials(z)) # z_body
)

end #module

2 comments on commit 6cdc9e6

@longemen3000
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/90512

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.3 -m "<description of version>" 6cdc9e6604694500fdca6f0fb9f3be758a936af7
git push origin v0.1.3

Please sign in to comment.