Skip to content

Commit 8f25be5

Browse files
authored
Disallow array * Tangent (#501)
* disallow array * Tangent * tests
1 parent fee64bf commit 8f25be5

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/tangent_arithmetic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Base.:+(a::Tangent{P}, b::P) where {P} = b + a
147147
# We intentionally do not define, `Base.*(::Tangent, ::Tangent)` as that is not meaningful
148148
# In general one doesn't have to represent multiplications of 2 differentials
149149
# Only of a differential and a scaling factor (generally `Real`)
150-
for T in (:Any,)
150+
for T in (:Number,)
151151
@eval Base.:*(s::$T, tangent::Tangent) = map(x -> s * x, tangent)
152152
@eval Base.:*(tangent::Tangent, s::$T) = map(x -> x * s, tangent)
153153
end

test/tangent_types/tangent.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ end
9191
@test getproperty(Tangent{NT}(; b=(@thunk 2.0^2)), 2) == 4.0
9292

9393
# TODO: uncomment this once https://github.com/JuliaLang/julia/issues/35516
94-
@test_broken haskey(Tangent{Tuple{Float64}}(2.0), 1) == true
94+
if VERSION >= v"1.8-"
95+
@test haskey(Tangent{Tuple{Float64}}(2.0), 1) == true
96+
else
97+
@test_broken haskey(Tangent{Tuple{Float64}}(2.0), 1) == true
98+
end
9599
@test_broken hasproperty(Tangent{Tuple{Float64}}(2.0), 2) == false
96100

97101
@test length(Tangent{Foo}(; x=2.5)) == 1
@@ -331,6 +335,10 @@ end
331335
d = Tangent{Dict}(Dict(4 => 3.0))
332336
two_d = Tangent{Dict}(Dict(4 => 2 * 3.0))
333337
@test 2 * d == two_d == d * 2
338+
339+
@test_throws MethodError [1, 2] * Tangent{Foo}(; y=1.5, x=2.5)
340+
@test_throws MethodError [1, 2] * d
341+
@test_throws MethodError Tangent{Foo}(; y=1.5, x=2.5) * @thunk [1 2; 3 4]
334342
end
335343

336344
@testset "show" begin

0 commit comments

Comments
 (0)