Julia: Define +(::UniformScaling{Bool})

Created on 15 Jul 2020  路  4Comments  路  Source: JuliaLang/julia

In Julia, we can use -I:

julia> -I
UniformScaling{Int64}
-1*I

which is exactly the same as -1*I. However, +I is not defined:

julia> +I
ERROR: MethodError: no method matching +(::UniformScaling{Bool})
Closest candidates are:
  +(::UniformScaling, ::Number) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/uniformscaling.jl:109
  +(::UniformScaling, ::UniformScaling) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/uniformscaling.jl:114
  +(::UniformScaling, ::BitArray{2}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/uniformscaling.jl:116
  ...
Stacktrace:
 [1] top-level scope at REPL[136]:1

This seems confusing at first glance. Hence, I propose that we define +I as +1*I to keep consistency.

If this is approved, then I can do a PR.

linear algebra

Most helpful comment

Why not have

(+)(J::UniformScaling) = J

to avoid unnecessary promotion of J.位?

The only problem here is that we will have a different behavior between - and +:

julia> import Base: +

julia> (+)(J::UniformScaling) = J
+ (generic function with 290 methods)

julia> -I
UniformScaling{Int64}
-1*I

julia> +I
UniformScaling{Bool}
true*I

I think I'd do:

(+)(J::UniformScaling) = UniformScaling(+J.位)

This seems to be fine since it replicates that same behavior of -:

julia> import Base: +

julia> (+)(J::UniformScaling) = UniformScaling(+J.位)
+ (generic function with 167 methods)

julia> -I
UniformScaling{Int64}
-1*I

julia> +I
UniformScaling{Int64}
1*I

I will wait a couple of days for any new ideas and then I submit the PR.

All 4 comments

Why not have

(+)(J::UniformScaling) = J

to avoid unnecessary promotion of J.位?

I think I'd do:

(+)(J::UniformScaling) = UniformScaling(+J.位)

Why not have

(+)(J::UniformScaling) = J

to avoid unnecessary promotion of J.位?

The only problem here is that we will have a different behavior between - and +:

julia> import Base: +

julia> (+)(J::UniformScaling) = J
+ (generic function with 290 methods)

julia> -I
UniformScaling{Int64}
-1*I

julia> +I
UniformScaling{Bool}
true*I

I think I'd do:

(+)(J::UniformScaling) = UniformScaling(+J.位)

This seems to be fine since it replicates that same behavior of -:

julia> import Base: +

julia> (+)(J::UniformScaling) = UniformScaling(+J.位)
+ (generic function with 167 methods)

julia> -I
UniformScaling{Int64}
-1*I

julia> +I
UniformScaling{Int64}
1*I

I will wait a couple of days for any new ideas and then I submit the PR.

Done! In Brazil, couple of days === 8 days 馃槄 (sorry for the delay, completely forgot about this...)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Keno picture Keno  路  3Comments

TotalVerb picture TotalVerb  路  3Comments

wilburtownsend picture wilburtownsend  路  3Comments

i-apellaniz picture i-apellaniz  路  3Comments

omus picture omus  路  3Comments