def _convert_to_scalar_function(coef, label): import core as cr if not callable(coef): register_base(label, cr.Function(lambda z: coef), overwrite=True) elif isinstance(coef, cr.Function): register_base(label, coef, overwrite=True) else: register_base(label, cr.Function(coef), overwrite=True) return ph.ScalarFunction(label)
def _simplify_product(a, b): # try to simplify expression containing ScalarFunctions scalar_func = None other_func = None for obj1, obj2 in [(a, b), (b, a)]: if isinstance(obj1, ScalarFunction): scalar_func = obj1 if isinstance(obj2, (FieldVariable, TestFunction, ScalarFunction)): other_func = obj2 break if scalar_func and other_func: s_func = get_base(scalar_func.data["func_lbl"], scalar_func.order[1]) o_func = get_base(other_func.data["func_lbl"], other_func.order[1]) if s_func.shape != o_func.shape: if s_func.shape[0] == 1: # only one function provided, use it for all others s_func = s_func[[0 for i in range(o_func.shape[0])]] else: raise ValueError("Cannot simplify Product due to dimension mismatch!") new_func = np.asarray([func.scale(scale_func) for func, scale_func in zip(o_func, s_func)]) new_name = new_func.tostring() register_base(new_name, new_func) if isinstance(other_func, (ScalarFunction, TestFunction)): a = other_func.__class__(function_label=new_name, order=other_func.order[1], location=other_func.location) elif isinstance(other_func, FieldVariable): # overwrite spatial derivative order, since derivation has been performed a = FieldVariable(function_label=new_name, weight_label=other_func.data["weight_lbl"], order=(other_func.order[0], 0), location=other_func.location) b = None return a, b