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

mapreducedim attempts to compute f(zero(T)), but it may throw #10533

Closed
simonster opened this issue Mar 16, 2015 · 7 comments
Closed

mapreducedim attempts to compute f(zero(T)), but it may throw #10533

simonster opened this issue Mar 16, 2015 · 7 comments
Labels
bug Indicates an unexpected problem or unintended behavior

Comments

@simonster
Copy link
Member

This line assumes that f(zero(T)) is safe to call. This is not always the case:

julia> sum(x->sqrt(x-1), ones(5), 1)
ERROR: DomainError:
sqrt will only return a complex result if called with a complex argument.
try sqrt (complex(x))
 in sqrt at math.jl:133
 in anonymous at none:1
 in reducedim_init at reducedim.jl:96
 in sum at reducedim.jl:255
@simonster simonster added the bug Indicates an unexpected problem or unintended behavior label Mar 16, 2015
@simonster simonster changed the title reducedim attempts to compute f(zero(T)), but it may throw mapreducedim attempts to compute f(zero(T)), but it may throw Mar 16, 2015
@timholy
Copy link
Sponsor Member

timholy commented Mar 24, 2015

I suppose we could put that section in a try, and fall back to

julia/base/reducedim.jl

Lines 100 to 101 in 39d8aef

z = zero(sum(f, A))
Tr = typeof(z)
in the catch?

@StefanKarpinski
Copy link
Sponsor Member

Probably better to check applicability since inference can reason about that.

@timholy
Copy link
Sponsor Member

timholy commented Mar 24, 2015

We can check applicability, but sqrt(-1) isn't something inference can reason about. I guess we need both?

@simonster
Copy link
Member Author

If !isempty(A), maybe we could just use f(A[1]) here? Then we wouldn't have to compute the reduction twice when 0 isn't in the domain of f.

@JeffBezanson
Copy link
Sponsor Member

mapreduce here has the best current approach I think:

n = Int(length(A))

I can't believe we're computing the whole reduction twice in some cases. I don't know how I missed that. What's more, if zero is not applicable, the code still tries to do zero(sum(f,A)). Sure, it might be defined for the result of sum and not for T, but if we're bothering to check, why make that assumption?

Generally method_exists is a code smell. It's also irrelevant to this issue. While reduce has a little leeway in its definition, anything involving map is quite rigid: the argument function should only be called on elements of the passed collection.

@inkydragon
Copy link
Sponsor Member

Looks like it's fixed on 1.6.3?

julia> sum(x->sqrt(x-1), ones(5); init=1)
1.0

julia> versioninfo()
Julia Version 1.6.3
Commit ae8452a9e0 (2021-09-23 17:34 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-9400F CPU @ 2.90GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, skylake)

@brenhinkeller
Copy link
Sponsor Contributor

brenhinkeller commented Nov 16, 2022

Yeah, seems to be fixed nowadays; I think this is safe to close but feel free to reopen if not!

julia> sum(x->sqrt(x-1), ones(5); init=1)
1.0

julia> sum(x->sqrt(x-1), ones(5))
0.0

julia> versioninfo()
Julia Version 1.8.2
Commit 36034abf260 (2022-09-29 15:21 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin21.3.0)
  CPU: 10 × Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, apple-m1)
  Threads: 1 on 8 virtual cores

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

6 participants