solrat.engine.generators.multiply module¶
- solrat.engine.generators.multiply.multiply(*args, is_complex=False, is_scalar=False)[source]¶
multiplies all args in order. If arg is a callable, it can be short-circuited.
usage:
result = multiply( delta(i, 1), lambda: expensive_calculation(i), # <- this will be evaluated only if previous terms are not 0 )
It is most efficient to put the most likely to be 0 terms first, like delta or 3j symbols.
==== NOTE ON CALLABLES ====
multiply is safe to use in loops on its own. It returns immediately, during the loop iteration, so the fact that lambdas in the arguments use references to the variables and not the actual values will not cause errors.
The only scenario where it can cause an error is if the multiply function itself is evaluated with delay. This behavior is not specific to multiply, it follows from the behavior of delayed evaluation as implemented in python.
The following code illustrates the potential problem:
callables = [] for i in [0, 1]: callables.append(lambda : multiply( lambda: delta(i, 1) # This will be evaluated for i==1 both times! )) # multiply should be evaluated here instead of being delayed. results = [c() for c in callables]