Git Product home page Git Product logo

Comments (8)

nsajko avatar nsajko commented on September 13, 2024 1

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.

barucden avatar barucden commented on September 13, 2024 1

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.

mbauman avatar mbauman commented on September 13, 2024

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.

jariji avatar jariji commented on September 13, 2024

Ok then maybe a breaking change for 2.0.

from julia.

barucden avatar barucden commented on September 13, 2024

@mbauman

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.

mbauman avatar mbauman commented on September 13, 2024

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.

DNF2 avatar DNF2 commented on September 13, 2024

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.

barucden avatar barucden commented on September 13, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.