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

Add function bitsign (inverse of signbit) #33341

Closed
eschnett opened this issue Sep 20, 2019 · 8 comments
Closed

Add function bitsign (inverse of signbit) #33341

eschnett opened this issue Sep 20, 2019 · 8 comments
Labels
maths Mathematical functions speculative Whether the change will be implemented is speculative

Comments

@eschnett
Copy link
Contributor

The mathematical expression (-1)^n appears in many equations. It would be convenient to have an efficient standard implementation in Julia. This is, in effect, the inverse of the signbit function.

I propose the following implementation:

function bitsign(b::Bool)::Int
    1 - 2 * b   # this seems to be the most efficient way
end

function bitsign(b::I)::I where {I<:Signed}
    I(bitsign(isodd(b)))
end
@stevengj stevengj added maths Mathematical functions speculative Whether the change will be implemented is speculative labels Sep 24, 2019
@StefanKarpinski
Copy link
Member

Would this be generalized to complex powers as well?

@eschnett
Copy link
Contributor Author

@StefanKarpinski Are you looking for cis? Maybe a new function cispi, similar to sinpi and cospi?

@StefanKarpinski
Copy link
Member

I'm not looking for anything, just trying to reason about the function you've proposed. If the definition is that it computes (-1)^n then the fact that it always returns ±1 for integer values is a specialization; for complex arguments it should compute the more general function. Similarly, consider the behavior of sign for a complex argument:

julia> sign(1 + 1im)
0.7071067811865475 + 0.7071067811865475im

@eschnett
Copy link
Contributor Author

@StefanKarpinski I see; going for a generic definition makes sense. Of course, the efficient implementation (much more efficient than actually calculating (-1)^n) exists only for integer n.

If you allow the exponent to be any real or complex number, then the implementation would be (-1)^x = exp(im*pi*x) = cis(pi*x). That's also an expression that occurs frequently. The name bitsign doesn't work any more here.

@StefanKarpinski
Copy link
Member

Right, that's part of why I was asking—the name seemed a bit too specific to the implementation. I think calling it cispi might be good and mentioning it in the help text of signbit. I guess the biggest worry is that it's pretty non-obvious to most people that cispi(n) is (-1)^n.

@StefanKarpinski
Copy link
Member

It's also kind of amazing and unexpected that cispi is the inverse function of signbit.

@eschnett
Copy link
Contributor Author

eschnett commented May 6, 2020

I just realize that we don't need cispi, the existing cospi will do fine. Should we instead add a method for cospi (and sinpi, for symmetry) that acts on integers or booleans?

@vtjnash
Copy link
Member

vtjnash commented Feb 16, 2022

No consensus that this is something desired (rejected from #35792)

@vtjnash vtjnash closed this as completed Feb 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maths Mathematical functions speculative Whether the change will be implemented is speculative
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants