5.9. Math¶
Utilities and functions for math.
Functions not explicitly defined here fall back to the appropriate numpy-like math
function for the provided inputs (if one exists), through __getattr__().
The inputs can be numpy arrays, arraycontext arrays, or pymbolic
expressions.
- example::
With
numpyinput data:import mirgecom.math as mm x_np = np.array([0, np.pi/2, np.pi]) s_np = mm.sin(x_np) # Calls np.sin
or
arraycontextinput data:x_device = actx.from_numpy(x_np) s_device = mm.sin(x_device) # Calls actx.np.sin
or
pymbolicinput expression:x_sym = pmbl.var("x") s_sym = mm.sin(x_sym) # Creates an expression pmbl.var("sin")(x_sym)
- mirgecom.math.harmonic_mean(x, y)[source]¶
Return the harmonic mean of x and y.
The harmonic mean is defined as
\[\frac{2 x y}{x + y}.\]- Parameters:
x – A number, array type, or symbolic expression.
y – A number, array type, or symbolic expression.
- mirgecom.math.__getattr__(name)[source]¶
Return a function that calls an appropriate math function based on its inputs.
Returns a function that inspects the types of its input arguments and dispatches to the appropriate math function. If any of the arguments are symbolic, the function returns a
pymbolic.primitives.ExpressionNoderepresenting the call to name. If not, it next checks whether any of the arguments have array contexts. If so, it calls name from the array context’snumpyworkalike. And if none of the arguments have array contexts, it callsnumpy’s version of name.