On Fri, Sep 23, 2022 at 4:39 AM 'Martin R' via sage-devel <
sage-devel@googlegroups.com> wrote:

> @chris: that's a great point.  Although I thought that the convention for
> "_bool_" is to return False only if it provably False, and otherwise True?
>

That's not the convention for p-adics:

sage: bool(O(5^2))
False


> @john: yes, in my very particular case I can pre-allocate the list,
> compute an element and store it in the list if it is non-zero, otherwise
> stop.  But that feels very clumsy.  I.e.;
>
> lf = [None]*len(X)
> for i, x in enumerate(X):
>     f = compute_factor(x)
>     if f:
>         lf[i] = f
>     else:
>         p = f
>         break
> else:
>     p = prod(lf)
>
> Since in this very particular case, compute_factor likely dominates
> everything else, I can use append, too.  However, I certainly do not want
> to call compute_factor anymore once I have found a zero.
>

If you don't care about the balanced product, you can do
def short_circuit_mul(total, n):
    if total == 0:
        return total
    return total * n
p = itertools.accumulate(X, short_circuit_mul)

Since prod is supposed to be able to multiply a very general set of objects
in sage, I don't think it's a good idea to add short-circuiting code to it
in general.
David



>
> Martin
> On Friday, 23 September 2022 at 10:28:57 UTC+2 john.c...@gmail.com wrote:
>
>> If you really have a list and not just a generator, might it be worth
>> a first pass to see if there's a 0 in the list before forming the
>> product? With the tree structure for efficiently computing the
>> product, you might be doing a lot of multiplication before hitting the
>> zero item. I think that all(L) will be True if and only if L contains
>> a zero.
>>
>> John
>>
>> On Fri, 23 Sept 2022 at 09:11, chris wuthrich
>> <christian...@gmail.com> wrote:
>> >
>> > Handling with try and check would be bad, but even in general this is
>> an idea that may not be what we want. For instance there are cases where
>> the product is interesting even if it "is zero".
>> >
>> > sage: x = Qp(5,10)(0)
>> > sage: y = x.add_bigoh(5)
>> > sage: not x, not y
>> > (True, True)
>> > sage: x * 5, y * 5
>> > (0, O(5^6))
>> >
>> > Chris
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> Groups "sage-devel" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> an email to sage-devel+...@googlegroups.com.
>> > To view this discussion on the web visit
>> https://groups.google.com/d/msgid/sage-devel/28fee9ed-e94e-4f04-8e80-c02b585cafd3n%40googlegroups.com.
>>
>>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-devel+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sage-devel/de9719f7-1bbb-427a-8ab0-a8e12a5ea234n%40googlegroups.com
> <https://groups.google.com/d/msgid/sage-devel/de9719f7-1bbb-427a-8ab0-a8e12a5ea234n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-devel/CAChs6_kvXQfnKJfGenFbrq6CHiNqbDSHn4tJdbBzqx0VkjO2nw%40mail.gmail.com.

Reply via email to