From 86a926158bdc9343877e1eef5b3f945cf53e7a83 Mon Sep 17 00:00:00 2001 From: Songchen Tan Date: Fri, 24 Feb 2023 17:53:51 +0000 Subject: [PATCH] Change slicing to tail --- .devcontainer/devcontainer.json | 2 +- benchmark/Manifest.toml | 2 +- src/primitive.jl | 30 ++++++++++++++++++++++++++++-- src/scalar.jl | 30 +++++------------------------- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 82759c5..eac0ea3 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -9,7 +9,7 @@ ] } }, - "postCreateCommand": "julia -e 'using Pkg; Pkg.add([\"Revise\", \"TestEnv\"])'", + "postCreateCommand": "julia -e 'using Pkg; Pkg.add([\"Revise\", \"TestEnv\", \"JuliaFormatter\", \"Cthulhu\"])'", "hostRequirements": { "cpus": 4 } diff --git a/benchmark/Manifest.toml b/benchmark/Manifest.toml index 66d6886..1e2e62d 100644 --- a/benchmark/Manifest.toml +++ b/benchmark/Manifest.toml @@ -1,6 +1,6 @@ # This file is machine-generated - editing it directly is not advised -julia_version = "1.8.5" +julia_version = "1.8.4" manifest_format = "2.0" project_hash = "141466748547092db2659f061bcf16b16bf160f5" diff --git a/src/primitive.jl b/src/primitive.jl index 9bb3bb9..da8b3eb 100644 --- a/src/primitive.jl +++ b/src/primitive.jl @@ -5,9 +5,21 @@ import Base: asin, acos, atan, acot, asec, acsc, asinh, acosh, atanh, acoth, ase import Base: sinc, cosc import Base: +, -, *, /, \, ^, >, <, >=, <=, == import Base: hypot, max, min +import Base: zero, one, adjoint, conj, transpose + +using Base: tail # Unary +@inline zero(::Type{TaylorScalar{T, N}}) where {T, N} = TaylorScalar{T, N}(zero(T)) +@inline one(::Type{TaylorScalar{T, N}}) where {T, N} = TaylorScalar{T, N}(one(T)) +@inline zero(::TaylorScalar{T, N}) where {T, N} = zero(TaylorScalar{T, N}) +@inline one(::TaylorScalar{T, N}) where {T, N} = one(TaylorScalar{T, N}) + +transpose(t::TaylorScalar) = t +adjoint(t::TaylorScalar) = t +conj(t::TaylorScalar) = t + ## Delegated @inline sqrt(t::TaylorScalar) = t^0.5 @@ -71,6 +83,16 @@ end # Binary +@inline +(a::Number, b::TaylorScalar) = TaylorScalar((a + value(b)[1]), tail(value(b))...) +@inline -(a::Number, b::TaylorScalar) = TaylorScalar((a - value(b)[1]), .-tail(value(b))...) +@inline *(a::Number, b::TaylorScalar) = TaylorScalar((a .* value(b))...) +@inline /(a::Number, b::TaylorScalar) = /(promote(a, b)...) + +@inline +(a::TaylorScalar, b::Number) = TaylorScalar((value(a)[1] + b), tail(value(a))...) +@inline -(a::TaylorScalar, b::Number) = TaylorScalar((value(a)[1] - b), tail(value(a))...) +@inline *(a::TaylorScalar, b::Number) = TaylorScalar((value(a) .* b)...) +@inline /(a::TaylorScalar, b::Number) = TaylorScalar((value(a) ./ b)...) + for op in [:>, :<, :(==), :(>=), :(<=)] @eval @inline $op(a::Number, b::TaylorScalar) = $op(a, value(b)[1]) @eval @inline $op(a::TaylorScalar, b::Number) = $op(value(a)[1], b) @@ -106,8 +128,12 @@ end ex = :($ex; TaylorScalar($([Symbol('v', i) for i in 1:N]...))) return :(@inbounds $ex) end -@inline *(a::TaylorScalar{T1, N}, b::TaylorScalar{T2, N}) where {T1,T2,N} = *(promote(a,b)...) -@inline /(a::TaylorScalar{T1, N}, b::TaylorScalar{T2, N}) where {T1,T2,N} = *(promote(a,b)...) +@inline function *(a::TaylorScalar{T1, N}, b::TaylorScalar{T2, N}) where {T1, T2, N} + *(promote(a, b)...) +end +@inline function /(a::TaylorScalar{T1, N}, b::TaylorScalar{T2, N}) where {T1, T2, N} + *(promote(a, b)...) +end @generated function ^(t::TaylorScalar{T, N}, n::S) where {S <: Number, T, N} ex = quote diff --git a/src/scalar.jl b/src/scalar.jl index 53fae9f..701bf75 100644 --- a/src/scalar.jl +++ b/src/scalar.jl @@ -1,5 +1,3 @@ -import Base: zero, one, adjoint, conj, transpose -import Base: +, -, *, / import Base: convert, promote_rule export TaylorScalar @@ -60,20 +58,13 @@ end @inline extract_derivative(r, i::Integer) = false @inline primal(t::TaylorScalar) = extract_derivative(t, 1) -@inline zero(::Type{TaylorScalar{T, N}}) where {T, N} = TaylorScalar{T, N}(zero(T)) -@inline one(::Type{TaylorScalar{T, N}}) where {T, N} = TaylorScalar{T, N}(one(T)) -@inline zero(::TaylorScalar{T, N}) where {T, N} = zero(TaylorScalar{T, N}) -@inline one(::TaylorScalar{T, N}) where {T, N} = one(TaylorScalar{T, N}) - -adjoint(t::TaylorScalar) = t -conj(t::TaylorScalar) = t - function promote_rule(::Type{TaylorScalar{T, N}}, ::Type{S}) where {T, S, N} TaylorScalar{promote_type(T, S), N} end -function promote_rule(::Type{TaylorScalar{T1, N}}, ::Type{TaylorScalar{T2,N}}) where {T1, T2, N} -TaylorScalar{promote_type(T1,T2), N} +function promote_rule(::Type{TaylorScalar{T1, N}}, + ::Type{TaylorScalar{T2, N}}) where {T1, T2, N} + TaylorScalar{promote_type(T1, T2), N} end # Number-like convention (I patched them after removing <: Number) @@ -82,17 +73,6 @@ convert(::Type{TaylorScalar{T, N}}, x::TaylorScalar{T, N}) where {T, N} = x function convert(::Type{TaylorScalar{T, N}}, x::S) where {T, S, N} TaylorScalar{T, N}(convert(T, x)) end -function convert(::Type{TaylorScalar{T1,N}},x::TaylorScalar{T2,N}) where {T1,T2,N} - TaylorScalar{T1,N}(convert.(T1,value(x))) +function convert(::Type{TaylorScalar{T1, N}}, x::TaylorScalar{T2, N}) where {T1, T2, N} + TaylorScalar{T1, N}(convert.(T1, value(x))) end -@inline +(a::Number, b::TaylorScalar) = TaylorScalar((a + value(b)[1]), value(b)[2:end]...) -@inline -(a::Number, b::TaylorScalar) = TaylorScalar((a - value(b)[1]), .-value(b)[2:end]...) -@inline *(a::Number, b::TaylorScalar) = TaylorScalar((a .* value(b))...) -@inline /(a::Number, b::TaylorScalar) = /(promote(a, b)...) - -@inline +(a::TaylorScalar, b::Number) = TaylorScalar((value(a)[1] + b), value(a)[2:end]...) -@inline -(a::TaylorScalar, b::Number) = TaylorScalar((value(a)[1] - b), value(a)[2:end]...) -@inline *(a::TaylorScalar, b::Number) = TaylorScalar((value(a) .* b)...) -@inline /(a::TaylorScalar, b::Number) = TaylorScalar((value(a) ./ b)...) - -transpose(t::TaylorScalar) = t