is coersion always tried in conversion code?

19 views
Skip to first unread message

Dima Pasechnik

unread,
Nov 12, 2021, 9:53:33 AM11/12/21
to sage-devel
This came up on https://trac.sagemath.org/ticket/32851
- is there any speed benefit to explicitly try to do coersion before
trying conversion?

Thanks
Dima

David Roe

unread,
Nov 12, 2021, 11:15:53 AM11/12/21
to sage-devel
I'm not following the discussion on that ticket completely, but it's hard to see a situation where there'd be a speed benefit to trying coercion first.  Here's why:

Given two parents A and B, if you try to convert an element of A into B Sage computes and stores a convert map.  However, in order to ensure that this convert map is reasonable, the first thing it tries when computing it is to compute the coerce map instead. If that succeeds, it just stores and returns that.  So the computation of a coercion map is built in to the computation of a conversion.

This is a one-time cost for each pair of parents.  Afterward, when you try to convert an element, it looks that map up in a hash table and applies it.  Depending on the implementation of the map, you can get speedups by improving how it computes its image.  You can also get speedups if you're doing a loop of some kind by ensuring that you call the map only once rather than many times (e.g. setting one=RBF(1) outside the loop rather than adding the python int 1 each time).  But I doubt that explicitly calling coercion before conversion will help.

Of course, the coercion system is complicated and I could be wrong.  Benchmarking is the actual arbiter.
David

--
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/CAAWYfq06c_nU0ESR2T0NRp%3D-893LJdj2N7_8--X9ZYpTia4%2BSw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages