# prod(numeric(0)) surprise

16 messages
Open this post in threaded view
|

## prod(numeric(0)) surprise

 It surprised me that prod(numeric(0)) is 1. I guess if you say (operation(nothing) == identity element) this makes sense, but ??     Looking in the code, this makes sense: basically (s=1; for i=0 to length(x), multiply s by x[i]) -- which comes out to 1.    What *should* prod(numeric(0)) produce? I couldn't find the answer documented anywhere.    (And how about sum(numeric(0))==0, which for some reason makes more intuitive sense to me, but is really exactly the same thing -- consider exp(sum(log(numeric(0)))) ... ?)    cheers      Ben Bolker -- 620B Bartram Hall                            [hidden email] Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolkerBox 118525                                   (ph)  352-392-5697 Gainesville, FL 32611-8525                   (fax) 352-392-3704 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: prod(numeric(0)) surprise

 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. > >     Looking in the code, this makes sense: > basically (s=1; for i=0 to length(x), > multiply s by x[i]) -- which comes out to 1. > >    What *should* prod(numeric(0)) produce? > I couldn't find the answer documented anywhere. > >    (And how about sum(numeric(0))==0, > which for some reason makes more intuitive sense > to me, but is really exactly the same thing -- > 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 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: prod(numeric(0)) surprise

 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. > >> > > > That's a fairly standard mathematical convention, which is presumably > why sum and prod work that way. > > Duncan Murdoch    OK.  I guess I was expecting NaN/NA (as opposed to an error), but I take the "this makes everything else more complicated" point. Should this be documented or is it just too obvious ... ? (Funny -- I'm willing to take gamma(1)==1 without any argument or suggestion that it should be documented ...)    cheers      Ben -- 620B Bartram Hall                            [hidden email] Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolkerBox 118525                                   (ph)  352-392-5697 Gainesville, FL 32611-8525                   (fax) 352-392-3704 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: prod(numeric(0)) surprise

 >>>>> "Ben" == Ben Bolker <[hidden email]> >>>>>     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.     >>     >>>     >>     >>     >> 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 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: prod(numeric(0)) surprise

 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 <[hidden email]> writes: >>>>>> "Ben" == Ben Bolker <[hidden email]> >>>>>>     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 > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: prod(numeric(0)) surprise

 On 1/9/2006 12:40 PM, Martin Morgan 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, > I think the code below works as I'd expect.  Would you really like the last answer to be numeric(0)?  > x <- 1:10  > sum(x) [1] 55  > sum(x[x>5]) [1] 40  > sum(x[x>10]) [1] 0 Duncan Murdoch >>     >>>> 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 <[hidden email]> writes: > >>>>>>> "Ben" == Ben Bolker <[hidden email]> >>>>>>>     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 >> >> ______________________________________________ >> [hidden email] mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: prod(numeric(0)) surprise

 In reply to this post by Martin Morgan 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 <[hidden email]> 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 <[hidden email]> writes: > > >>>>>> "Ben" == Ben Bolker <[hidden email]> > >>>>>>     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 > > > > ______________________________________________ > > [hidden email] mailing list > > https://stat.ethz.ch/mailman/listinfo/r-devel> > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel> ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: prod(numeric(0)) surprise

 In reply to this post by Duncan Murdoch I guess I have to say yes, I'd exepct x <- 1:10 sum(x[x>10]) ==> numeric(0) this would be reinforced by recongnizing that numeric(0) is not zero, but nothing. I guess the summation over an empty set is an empty set, rather than a set containing the number 0. Certainly these exp(x[x>10]) ==> numeric(0) numeric(0) + 1 ==> numeric(0) would give me pause. Gabor Grothendieck <[hidden email]> writes: > The way to think about it is: > >    prod(rep(x,n)) == x^n > > and that works for n=0 too. Hmm, Not sure what to put in for x and n? do you mean x == numeric(0), n == 0 (0 copies of an empty set), x == ANY n == numeric(0) (an empty set of ANYthing), x == numeric(0), n == numeric(0) ? For all of these, x^n evaluates to numeric(0). Martin (Morgan) Duncan Murdoch <[hidden email]> writes: > On 1/9/2006 12:40 PM, Martin Morgan 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, >> > > I think the code below works as I'd expect.  Would you really like the > last answer to be numeric(0)? > >  > x <- 1:10 >  > sum(x) > [1] 55 >  > sum(x[x>5]) > [1] 40 >  > sum(x[x>10]) > [1] 0 > > Duncan Murdoch > >>>     >>>> 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 <[hidden email]> writes: >> >>>>>>>> "Ben" == Ben Bolker <[hidden email]> >>>>>>>>     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 >>> >>> ______________________________________________ >>> [hidden email] mailing list >>> https://stat.ethz.ch/mailman/listinfo/r-devel______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: prod(numeric(0)) surprise

 In reply to this post by Ben Bolker-2 If you haven't seen this in your math courses, perhaps this would help: http://en.wikipedia.org/wiki/Empty_setwhich says, in part: Operations on the empty set Operations performed on the empty set (as a set of things to be operated upon) can also be confusing. (Such operations are nullary operations.) For example, the sum of the elements of the empty set is zero, but the product of the elements of the empty set is one (see empty product). This may seem odd, since there are no elements of the empty set, so how could it matter whether they are added or multiplied (since "they" do not exist)? Ultimately, the results of these operations say more about the operation in question than about the empty set. For instance, notice that zero is the identity element for addition, and one is the identity element for multiplication. Andy From: Martin Morgan > > I guess I have to say yes, I'd exepct > > x <- 1:10 > sum(x[x>10]) ==> numeric(0) > > this would be reinforced by recongnizing that numeric(0) is not zero, > but nothing. I guess the summation over an empty set is an empty set, > rather than a set containing the number 0. Certainly these > > exp(x[x>10]) ==> numeric(0) > numeric(0) + 1 ==> numeric(0) > > would give me pause. > > > Gabor Grothendieck <[hidden email]> writes: > > > The way to think about it is: > > > >    prod(rep(x,n)) == x^n > > > > and that works for n=0 too. > > Hmm, Not sure what to put in for x and n? do you mean x == numeric(0), > n == 0 (0 copies of an empty set), x == ANY n == numeric(0) (an empty > set of ANYthing), x == numeric(0), n == numeric(0) ? For all of these, > x^n evaluates to numeric(0). > > Martin (Morgan) > > Duncan Murdoch <[hidden email]> writes: > > > On 1/9/2006 12:40 PM, Martin Morgan 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, > >> > > > > I think the code below works as I'd expect.  Would you > really like the > > last answer to be numeric(0)? > > > >  > x <- 1:10 > >  > sum(x) > > [1] 55 > >  > sum(x[x>5]) > > [1] 40 > >  > sum(x[x>10]) > > [1] 0 > > > > Duncan Murdoch > > > >>>     >>>> 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 <[hidden email]> writes: > >> > >>>>>>>> "Ben" == Ben Bolker <[hidden email]> > >>>>>>>>     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 > >>> > >>> ______________________________________________ > >>> [hidden email] mailing list > >>> https://stat.ethz.ch/mailman/listinfo/r-devel> > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel> > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: prod(numeric(0)) surprise

 On 1/9/2006 1:27 PM, Liaw, Andy wrote: > If you haven't seen this in your math courses, perhaps this would help: > > http://en.wikipedia.org/wiki/Empty_set> This is what is so great about Wikipedia:  it gives certainty where I'd only call it a fairly standard convention.  ;-) Duncan Murdoch > which says, in part: > > Operations on the empty set > > Operations performed on the empty set (as a set of things to be operated > upon) can also be confusing. (Such operations are nullary operations.) For > example, the sum of the elements of the empty set is zero, but the product > of the elements of the empty set is one (see empty product). This may seem > odd, since there are no elements of the empty set, so how could it matter > whether they are added or multiplied (since "they" do not exist)? > Ultimately, the results of these operations say more about the operation in > question than about the empty set. For instance, notice that zero is the > identity element for addition, and one is the identity element for > multiplication. > > > Andy > > > From: Martin Morgan >> >> I guess I have to say yes, I'd exepct >> >> x <- 1:10 >> sum(x[x>10]) ==> numeric(0) >> >> this would be reinforced by recongnizing that numeric(0) is not zero, >> but nothing. I guess the summation over an empty set is an empty set, >> rather than a set containing the number 0. Certainly these >> >> exp(x[x>10]) ==> numeric(0) >> numeric(0) + 1 ==> numeric(0) >> >> would give me pause. >> >> >> Gabor Grothendieck <[hidden email]> writes: >> >> > The way to think about it is: >> > >> >    prod(rep(x,n)) == x^n >> > >> > and that works for n=0 too. >> >> Hmm, Not sure what to put in for x and n? do you mean x == numeric(0), >> n == 0 (0 copies of an empty set), x == ANY n == numeric(0) (an empty >> set of ANYthing), x == numeric(0), n == numeric(0) ? For all of these, >> x^n evaluates to numeric(0). >> >> Martin (Morgan) >> >> Duncan Murdoch <[hidden email]> writes: >> >> > On 1/9/2006 12:40 PM, Martin Morgan 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, >> >> >> > >> > I think the code below works as I'd expect.  Would you >> really like the >> > last answer to be numeric(0)? >> > >> >  > x <- 1:10 >> >  > sum(x) >> > [1] 55 >> >  > sum(x[x>5]) >> > [1] 40 >> >  > sum(x[x>10]) >> > [1] 0 >> > >> > Duncan Murdoch >> > >> >>>     >>>> 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 <[hidden email]> writes: >> >> >> >>>>>>>> "Ben" == Ben Bolker <[hidden email]> >> >>>>>>>>     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 >> >>> >> >>> ______________________________________________ >> >>> [hidden email] mailing list >> >>> https://stat.ethz.ch/mailman/listinfo/r-devel>> >> ______________________________________________ >> [hidden email] mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel>> >> > > > ------------------------------------------------------------------------------ > Notice:  This e-mail message, together with any attachment...{{dropped}} ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: prod(numeric(0)) surprise

Open this post in threaded view
|

## Re: prod(numeric(0)) surprise

 In reply to this post by Martin Morgan On Mon, 9 Jan 2006, Martin Morgan wrote: > I guess I have to say yes, I'd exepct > > x <- 1:10 > sum(x[x>10]) ==> numeric(0) > > this would be reinforced by recongnizing that numeric(0) is not zero, > but nothing. I guess the summation over an empty set is an empty set, > rather than a set containing the number 0. Certainly these > > exp(x[x>10]) ==> numeric(0) > numeric(0) + 1 ==> numeric(0) > There are some fairly simple rules in how R does it.  You do need to distinguish between functions (binary operators) that map two vectors of length n to a vector of length n and functions such as prod and sum that map a vector of length n to a vector of length 1. The output of sum and prod is always of length 1, so sum(numeric(0)) and prod(numeric(0)) should be of length 1 (or give an error).  It is convenient that sum(c(x,y)) is equal to sum(x)+sum(y) and that prod(c(x,y)) is equal to prod(x)*prod(y), which motivates making sum(numeric(0)) give 0 and prod(numeric(0)) give 1. Single argument functions such as exp(numeric(0)) seem fairly obvious: you have no numbers and you exponentiate them so you still have no numbers. You could also argue based on c() and exp() commuting. The rules for binary operators are a little less tidy [my fault]. They come from the idea that x+1 should always add 1 to each element of x.  If you add 1 to each element of numeric(0) you get numeric(0).  The usual recycling rule says that the shorter vector should be repeated to make it the same length as the longer vector, so this is a wart.  On the other hand, you can't recycle a vector of length 0 to have length 1, so the usual recycling rule can't be applied here. This also makes matrix operations work, at least in the sense of getting matrices of the right dimension.   -thomas ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: prod(numeric(0)) surprise

 In reply to this post by Ben Bolker-2 On Mon, 9 Jan 2006, Thomas Lumley wrote: > On Mon, 9 Jan 2006, Martin Morgan wrote: > >> I guess I have to say yes, I'd exepct >> >> x <- 1:10 >> sum(x[x>10]) ==> numeric(0) >> >> this would be reinforced by recongnizing that numeric(0) is not zero, >> but nothing. I guess the summation over an empty set is an empty set, >> rather than a set containing the number 0. Certainly these >> >> exp(x[x>10]) ==> numeric(0) >> numeric(0) + 1 ==> numeric(0) >> > > There are some fairly simple rules in how R does it.  You do need to > distinguish between functions (binary operators) that map two vectors of > length n to a vector of length n and functions such as prod and sum that > map a vector of length n to a vector of length 1. > > The output of sum and prod is always of length 1, so sum(numeric(0)) and > prod(numeric(0)) should be of length 1 (or give an error).  It is > convenient that sum(c(x,y)) is equal to sum(x)+sum(y) and that > prod(c(x,y)) is equal to prod(x)*prod(y), which motivates making > sum(numeric(0)) give 0 and prod(numeric(0)) give 1. > > Single argument functions such as exp(numeric(0)) seem fairly obvious: you > have no numbers and you exponentiate them so you still have no numbers. > You could also argue based on c() and exp() commuting. > > The rules for binary operators are a little less tidy [my fault]. They > come from the idea that x+1 should always add 1 to each element of x.  If > you add 1 to each element of numeric(0) you get numeric(0).  The usual > recycling rule says that the shorter vector should be repeated to make it > the same length as the longer vector, so this is a wart.  On the other > hand, you can't recycle a vector of length 0 to have length 1, so the > usual recycling rule can't be applied here. This also makes matrix > operations work, at least in the sense of getting > matrices of the right dimension. There is an Svr4 addendum to the original S recycling rule which R implements: any vector/array expression with a length-0 component has a length-0 result. (Note the qualifier in there.)  See `S Programming' pp. 17,27 for more details.  So the `wart' is part of a general rule. -- Brian D. Ripley,                  [hidden email] Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/University of Oxford,             Tel:  +44 1865 272861 (self) 1 South Parks Road,                     +44 1865 272866 (PA) Oxford OX1 3TG, UK                Fax:  +44 1865 272595 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: prod(numeric(0)) surprise

 In reply to this post by Duncan Murdoch But this thread seems to have pointed to some inconsistencies: > cumprod( numeric(0) ) numeric(0) > cumsum( numeric(0) ) numeric(0) shouldn't this give the same as prod() and sum() in this case? Same with cummin() and cummax(). Kjetil On 1/9/06, Duncan Murdoch <[hidden email]> wrote: > > On 1/9/2006 12:40 PM, Martin Morgan 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, > > > > I think the code below works as I'd expect.  Would you really like the > last answer to be numeric(0)? > > > x <- 1:10 > > sum(x) > [1] 55 > > sum(x[x>5]) > [1] 40 > > sum(x[x>10]) > [1] 0 > > Duncan Murdoch > > >>     >>>> 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 <[hidden email]> writes: > > > >>>>>>> "Ben" == Ben Bolker <[hidden email]> > >>>>>>>     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 > >> > >> ______________________________________________ > >> [hidden email] mailing list > >> https://stat.ethz.ch/mailman/listinfo/r-devel> > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel>         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel