Comments (8)
For any empty collection c
, isone(prod(c))
must hold, right? Given that, returning 1
doesn't seem that bad. It's not ideal for element types that don't subtype Number
, though.
from julia.
Oh, I see now that I completely missed what OP was reporting! My apologies, I'll hide my previous comments. Let me make it clear for any other confused contributors.
The issue is not returning the multiplicative identity for an empty collection. That is the standard definition in math. And programming languages (R, Common Lisp, numpy) follow that definition, including Julia:
julia> prod(Int[])
1
The documentation for prod
says that:
The value returned for empty itr can be specified by init. It must be the multiplicative identity (i.e. one) as it is unspecified whether init is used for non-empty collections.
The issue is guessing what the multiplicative identity is for an empty tuple. We do not guess it for Any[]
, but we guess it for Tuple(Any[])
.
julia> A = []
Any[]
julia> prod(A)
ERROR: MethodError: no method matching one(::Type{Any})
julia> prod(Tuple(A))
1
from julia.
Theoretically I agree, but pragmatically this method is often used for size tuples. This change would be surprisingly breaking for precisely that reason.
from julia.
Ok then maybe a breaking change for 2.0.
from julia.
this method is often used for size tuples
As in prod(size(A))
? Maybe Julia should provide a function for getting the number of elements in a collection then?
This issue is, IMHO, quite similar to #55379 in that Julia returns an (arbitrary) value for an empty collection.
from julia.
I dunno, someone could try it and see just how breaking it is, but unlike #55379, this isn't giving wildly wrong answers. That tends to happen when we guess at init
— this is guessing at the empty case.
from julia.
As in
prod(size(A))
? Maybe Julia should provide a function for getting the number of elements in a collection then?
That's what length(A)
does.
from julia.
Indeed! I never realized length
does this. I guess I always assumed it returned the number of elements along the first dimension (influenced by python's len
perhaps).
What's the motivation for prod(size(A))
then? People use it.
Is it true that any prod(size(A))
can be replaced by length(A)
?
I believe the pattern prod(size(A)[c:d]
cannot be replaced by length
, but size(A)[c:d]
should never return ()
, unless c>d
, which seems like a very marginal case.
from julia.
Related Issues (20)
- `rand_twiceprecision` tests intermittently failing on 32-bit Linux HOT 1
- `@time_imports` not showing extension parents HOT 3
- Type instabilities when using `mapslices` HOT 5
- Allocations of `eachslice` with variable `dims` HOT 3
- 4x regression for loading JLLs in 1.11, or likely the first one, such as micromamba_jll HOT 7
- `pkgdir` returns wrong dir for extension if extension is in its own folder
- Extension not loaded if trigger is a strong dependency [on 1.10] HOT 6
- `libutf8proc.a: error adding symbols: file format not recognized` blocking build on Windows 11 on ARM via WSL HOT 8
- 136x regression for LazyAritifacts HOT 4
- LinearAlgebra/addmul all-NAN batman? HOT 5
- `Markdown.parse(::LazyString)` errors
- Allow `stack` on empty collection HOT 1
- Learn program HOT 1
- `+` function string concatenation hint should not apply to zero args HOT 1
- Typed comprehension with begin/end is illegal syntax HOT 1
- Julia v1.11 (all RCs) breaks package SolidStateDetectors (not caught by PkgEval due to timeout)
- REPL precompilation workload is broken HOT 1
- `MethodError` thrown while preparing to throw `InexactError` in conversion of `Rational` to `Integer`
- `with_logger(NullLogger())` allocates in v1.11
- Artifact string resolution fails in Quarto/Jupyter context
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from julia.