From ggrothendieck at gmail.com  Mon Jan  9 18:53:36 2006
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Mon, 9 Jan 2006 12:53:36 -0500
Subject: [Rd] prod(numeric(0)) surprise
In-Reply-To: <6phslrxz4dg.fsf@gopher3.fhcrc.org>
References: <43C1C973.8080702@zoo.ufl.edu> <43C1CBB9.7090307@stats.uwo.ca>
	<43C1CD05.2030504@zoo.ufl.edu>
	<17346.5951.435355.767827@stat.math.ethz.ch>
	<6phslrxz4dg.fsf@gopher3.fhcrc.org>
Message-ID: <971536df0601090953v1aaaa905n97c5654e582a89b0@mail.gmail.com>

The way to think about it is:

   prod(rep(x,n)) == x^n

and that works for n=0 too.

On 1/9/06, Martin Morgan <mtmorgan at fhcrc.org> wrote:
> I'm a little confused. I understand that numeric(0) means an empty
> numeric vector, not the number 0 expressed as numeric. As it is now,
> prod(numeric(0)) generates something -- a vector of length 1
> containing the number 1 -- from nothing. I would have expected
>
> prod(numeric(0)) ==> numeric(0)
>
> this is consistent with
>
> numeric(0) ==> numeric(0)
> numeric(0) * 1 ==> numeric(0)
> cumprod(numeric(0)) ==> numeric(0)
>
> and, because concatenation occus before function evaluation,
>
> prod(c(numeric(0),1)) ==> prod( c(1) ) ==> 1
>
> I would expect sum() to behave the same way, e.g., sum(numeric(0)) ==>
> numeric(0). From below,
>
> >     >>>> consider exp(sum(log(numeric(0)))) ... ?)
> >     >>
> >     >> That's a fairly standard mathematical convention, which
> >     >> is presumably why sum and prod work that way.
> >     >>
> >     >> Duncan Murdoch
>
> I would have expected numeric(0) as the result (numeric(0) is the
> result from log(numeric(0)), etc).
>
> Martin (Morgan)
>
>
> Martin Maechler <maechler at stat.math.ethz.ch> writes:
>
> >>>>>> "Ben" == Ben Bolker <bolker at zoo.ufl.edu>
> >>>>>>     on Sun, 08 Jan 2006 21:40:05 -0500 writes:
> >
> >     Ben> Duncan Murdoch wrote:
> >     >> On 1/8/2006 9:24 PM, Ben Bolker wrote:
> >     >>
> >     >>> It surprised me that prod(numeric(0)) is 1.  I guess if
> >     >>> you say (operation(nothing) == identity element) this
> >     >>> makes sense, but ??
> >     >>
> >     >>
> >     >> What value were you expecting, or were you expecting an
> >     >> error?  I can't think how any other value could be
> >     >> justified, and throwing an error would make a lot of
> >     >> formulas more complicated.
> >     >>
> >     >>>
> >     >>
> >     >>>> consider exp(sum(log(numeric(0)))) ... ?)
> >     >>
> >     >> That's a fairly standard mathematical convention, which
> >     >> is presumably why sum and prod work that way.
> >     >>
> >     >> Duncan Murdoch
> >
> >     Ben>    OK.  I guess I was expecting NaN/NA (as opposed to
> >     Ben> an error), but I take the "this makes everything else
> >     Ben> more complicated" point.  Should this be documented or
> >     Ben> is it just too obvious ... ?  (Funny -- I'm willing to
> >     Ben> take gamma(1)==1 without any argument or suggestion
> >     Ben> that it should be documented ...)
> >
> > see?  so it looks to me as if you have finally convinced
> > yourself that '1' is the most reasonable result.. ;-)
> >
> > Anyway, I've added a sentence to help(prod)  {which matches
> > the sentence in help(sum), BTW}.
> >
> > Martin
> >
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] prod(numeric(0)) surprise
Gabor Grothendieck