sage: l = [1,2,3]
sage: any([i == 2 for i in l])
True
sage: all([i == 2 for i in l])
False
--Mike
On 30/01/2008, Nils Bruin <nbr...@sfu.ca> wrote:
>
> On Jan 30, 2:49 pm, "Mike Hansen" <mhan...@gmail.com> wrote:
> [...]
> > name. Also, there are functions in Python which have similar
> > functionality.
> >
> > sage: l = [1,2,3]
> > sage: any([i == 2 for i in l])
> > True
> > sage: all([i == 2 for i in l])
> > False
>
> That's instructive. In that case, forall and exists should probably
> have their help extended by a pointer that if a witness is not
> required, the user should type:
>
> any(P(s) for s in S)
>
> or
>
> all(P(s) for s in S)
>
> That's actually a nicer syntax anyway and it would be a shame if
> people keep hobbling along with forall and exists when there is a much
> nicer option. (I got a tab completion hit on "forall" and "exists" and
> assumed those were the right functions to use. They are often not).
> That teaches me to *not* write patches :-)
On the contrary! But change it to a patch for the docstrong for
exists() and forall().
John
>
> > On Jan 30, 2008 2:45 PM, Nils Bruin <nbr...@sfu.ca> wrote:
> >
> >
> >
> > > Presently, because forall and exists return tuples rather than
> > > booleans, their return value is always True, so they cannot be used as
> > > a predicate, which is very unintuitive given their names. Proposed
> > > solution: optional parameter witness (default: False) that determines
> > > return value type.
> >
> > > Ticket (has patch)
> >
> > >http://trac.sagemath.org/sage_trac/attachment/ticket/1987
> >
>
--
John Cremona
Any chance you could also post a patch that adds this very important
insight to the documentation?
Also, in your posted patch you emphasize that forall and exists
are *NOT* suitable for use in an if, etc. I would have written that
to use them thus you have to use the ugly forall(...)[0], and it is
much nicer to use all(...) and any(...).
William
William
It's because here 0 is created a million times (because the preparser
turns this into a<Integer(0)):
sage: any(a<0 for a in L)
and here 0 is created only once:
sage: g=lambda a: a<0
sage: any(g(a) for a in L)
And yes, we do plan to sometime make it so the preparser intelligently factors
out constants, but haven't done this yet.
William
John
--
John Cremona
I meant: "not any(z)".
> John
>
> On 01/02/2008, Nils Bruin <nbr...@sfu.ca> wrote:
> >
> > I forgot one case. This is weird. Exists seems *faster* than any. That
> > should never be happening!
> >
> > sage: L=[1..10^6]
> > sage: L[10^6-3]=-1
> > sage: %timeit any(a<0 for a in L)
> > 10 loops, best of 3: 2.47 s per loop
> > sage: %timeit exists(L, lambda a: a<0)
> > 10 loops, best of 3: 2.83 s per loop
> > sage: g=lambda a: a<0
> > sage: %timeit any(g(a) for a in L)
> > 10 loops, best of 3: 1.19 s per loop
> > sage: %timeit exists(L, g)
> > 10 loops, best of 3: 1.04 s per loop
> >
> >
> > > >
> >
>
>
> --
> John Cremona
>
--
John Cremona