Copy an arbitrarily precise rational from CGAL by serializing and deserializing to string or double. Can be useful to flatten the depth of operands, trim away precision or create non-reference counted copies for use in multi-threaded contexts.
(v, to_double=False)
| 83 | double_cache = {} |
| 84 | |
| 85 | def reserialize(v, to_double=False): |
| 86 | """ |
| 87 | Copy an arbitrarily precise rational from CGAL by serializing and |
| 88 | deserializing to string or double. Can be useful to flatten the |
| 89 | depth of operands, trim away precision or create non-reference counted |
| 90 | copies for use in multi-threaded contexts. |
| 91 | """ |
| 92 | st = v.to_string() |
| 93 | if to_double: |
| 94 | val = double_cache.get(st) |
| 95 | if val is not None: |
| 96 | return val |
| 97 | # @todo can we do this on the str? |
| 98 | stn = (-v).to_string() |
| 99 | val = double_cache.get(stn) |
| 100 | if val is not None: |
| 101 | return -val |
| 102 | d = create_epeck(v.to_double()) |
| 103 | double_cache[st] = d |
| 104 | return d |
| 105 | else: |
| 106 | # this does seem to shave off a bit of RAM usage, but even better |
| 107 | # would be to not compute the result altogether, so cache tuples of |
| 108 | # operation and operands prior to evaluating. But we don't know |
| 109 | # how expensive the serialization to str is... |
| 110 | val = epeck_cache.get(st) |
| 111 | if val: |
| 112 | return val |
| 113 | ep = create_epeck(st) |
| 114 | epeck_cache[st] = ep |
| 115 | return ep |
| 116 |