Commit b3a9d07a authored by Martin Bauer's avatar Martin Bauer
Browse files

Additional utility function: remove sp.Float smaller than a threshold

parent ffee727a
......@@ -18,6 +18,20 @@ def prod(seq: Iterable[T]) -> T:
return reduce(operator.mul, seq, 1)
def remove_small_floats(expr, threshold):
"""Removes all sp.Float objects whose absolute value is smaller than threshold
>>> expr = sp.sympify("x + 1e-15 * y")
>>> remove_small_floats(expr, 1e-14)
if isinstance(expr, sp.Float) and sp.Abs(expr) < threshold:
return 0
new_args = [remove_small_floats(c, threshold) for c in expr.args]
return expr.func(*new_args) if new_args else expr
def is_integer_sequence(sequence: Iterable) -> bool:
"""Checks if all elements of the passed sequence can be cast to integers"""
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment