-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Accept only Number as input for sincos #30136
Conversation
By the way, am I suppose to write the entry in |
What happens for a square matrix? |
Related: #27254. |
Oh, indeed
right? |
Why is this a bad thing in itself? In fact, I'd go in the other direction and have the fallback for sincos(x) = (sin(x), cos(x)) I.e., none of this |
@tkoolen good point! I will do some tests to see if this does not cause any other problem. |
With the old definition, it was possible to call `sincos` with Char inputs, which is not defined for `sin` and `cos`. Hence, this commit modifies the structure of the `sincos` method definition to do the following: 1. If the input is `Float32` or `Float64`, call `sincos` to simultaneously compute the sine and cosine faster. 2. If the input is `Number`, try to convert it to float. a. If the converted `Number` is float, call `sincos` that will simultaneously compute the sine and cosine faster. b. Otherwise, compute the sine and cosine separately. This is the case if the input is `Complex`. 3. If the input is everything else, compute the sine and cosine separately. Hence, if the input type is not accepted by the functions `sin` and `cos`, then an error will be printed.
Hi guys, sorry for the delay. In fact @tkoolen I found a good explanation why removing the After thinking about it, the algorithm I am proposing in this commit is:
Is this good? Notice that it will work on every input supported by |
Yes that sounds good. |
I'm not sure I follow the prioritized list. If |
If you do not convert an |
What if I have |
Ah I see, sorry. Hum, I do not think about an easy way to correct this while keeping the current behavior. The only thing that I came up with was to add another specialization of |
Hm , just don’t convert anything ? |
In this case julia> @benchmark sincos(1.0)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 10.848 ns (0.00% GC)
median time: 11.129 ns (0.00% GC)
mean time: 11.251 ns (0.00% GC)
maximum time: 46.524 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 999
julia> @benchmark sincos(1)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 10.873 ns (0.00% GC)
median time: 11.130 ns (0.00% GC)
mean time: 11.273 ns (0.00% GC)
maximum time: 46.574 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 999
julia> sincos(x::Int) = (sin(x),cos(x))
sincos (generic function with 9 methods)
julia> @benchmark sincos(1)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 13.738 ns (0.00% GC)
median time: 13.769 ns (0.00% GC)
mean time: 13.886 ns (0.00% GC)
maximum time: 44.532 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 998 |
What's a use case for calling |
But wouldn’t it be like a strange behavior from the user point of view? Furthermore, it is not too uncommon. At my institute, we developed a satellite camera that has precisely 1 rad of FOV. You will see some code with IMHO, the gain to eliminate this conversion to float is not too expressive. What do you think? |
Why would |
It's not that it wouldn't accept non-floats, it's just that non-floats would go through the The real benefit of having methods besides |
Another argument I would give is that sin(x::Real) = sin(float(x)) Hence, since it is already done, I think it is better to also do this here and call an optimized method. The case of @pkofod, in which a new type of number is created, seems more restricted than someone using this function with an integer input. |
To summarize, when
Does anybody see a way to have the cake and eat it too? |
Maybe something like function sincos(x)
if applicable(float, x)
x′ = float(x)
if typeof(x) === typeof(x′)
# catch the stack overflow from #26552
throw(ArgumentError("Calling float on `x` did not change its type."))
end
return sincos(x′)
else
return (sin(x), cos(x))
end
end but with tightened type signatures for
which I think should also be changed, perhaps in a manner similar to what I proposed above. |
Hi @tkoolen , Now I think I am seeing the problem. Your solution seems interesting but we need to change a little bit because it will throw an error with |
To leave things simple, in sincos(x::T) where T<:Union{Integer, Rational, AbstractIrrational} = sincos(float(x))
sincos(x) = (sin(x), cos(x)) I cannot recall any other type in which we would like to convert it directly to This will solve my problem and the one mentioned by @tkoolen and @pkofod . Then, if you want, I can start to think about changing that
|
Guys, Although this last option is cleaner, it cannot be used this way. The file
Question: should I let this as it is or can I move the |
discussion seems to have died |
With the old definition, it was possible to call
sincos
with Char inputs, which is not defined forsin
andcos
. Hence, we should accept only Real variables as input forsincos
.I put this in a separated PR because this is a breaking change (kind of...).