Skip to content

Commit

Permalink
Change slicing to tail
Browse files Browse the repository at this point in the history
  • Loading branch information
tansongchen committed Feb 24, 2023
1 parent a2e880b commit 86a9261
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion benchmark/Manifest.toml
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
30 changes: 28 additions & 2 deletions src/primitive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
30 changes: 5 additions & 25 deletions src/scalar.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Base: zero, one, adjoint, conj, transpose
import Base: +, -, *, /
import Base: convert, promote_rule

export TaylorScalar
Expand Down Expand Up @@ -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)
Expand All @@ -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

0 comments on commit 86a9261

Please sign in to comment.