Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to add log and exp support? #6

Closed
oschulz opened this issue Jun 25, 2020 · 3 comments
Closed

Possible to add log and exp support? #6

oschulz opened this issue Jun 25, 2020 · 3 comments

Comments

@oschulz
Copy link

oschulz commented Jun 25, 2020

Would it be possible to add

Base.exp(::Zero) = One()
Base.log(::One) = Zero()

without causing unforseen trouble somewhere? If so, maybe

import Base.^
^(x::Number, ::Zero) = One()
^(x::Number, ::One) = x
Base.sqrt(::One) = One()
Base.sqrt(::Zero) = Zero()

as well?

@oschulz
Copy link
Author

oschulz commented Jun 25, 2020

Ah, ^(x::Number, ::One) would cause some ambiguous dispatch. Would need

import Base.^

^(x::Number, ::Zero) = One()
^(x::Float16, ::Zero) = One()
^(x::Float32, ::Zero) = One()
^(x::Float64, ::Zero) = One()
^(x::BigFloat, ::Zero) = One()
^(x::AbstractFloat, ::Zero) = One()

^(x::Number, ::One) = x
^(x::Float16, ::One) = x
^(x::Float32, ::One) = x
^(x::BigFloat, ::One) = x
^(x::AbstractFloat, ::One) = x

Would that be too much "fine-tuning" resp. too fragile?

It would make things like A .^ Zero() run in constant time, with zero-sized output. :-) Not sure that's a relevant use-case, though.

@perrutquist
Copy link
Owner

Done.

^(x, p::One) doesn't need special-casing, because those methods typically start with something like p==1 && return x anyway. For example:

julia> @code_llvm 1.0^One()

;  @ math.jl:899 within `^'
define double @"julia_^_3800"(double) {
top:
;  @ math.jl:902 within `^'
  ret double %0
}

@oschulz
Copy link
Author

oschulz commented Jun 26, 2020

Awesome, thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants