def sym_dispose_map(): """user's str to sympy.expr function""" Flat = Function("MAdd") Comp = Function("MMul") Diff = Function("MSub") Quot = Function("MDiv") Self = lambda x: x Conv = Function("Conv") Flat.is_jump = False Comp.is_jump = False Diff.is_jump = True Quot.is_jump = True Conv.is_jump = True Self.is_jump = True Flat.keep = False Comp.keep = False Diff.keep = False Quot.keep = False Conv.keep = True Self.keep = True return { "MAdd": Flat, "MMul": Comp, "MSub": Diff, "MDiv": Quot, "Conv": Conv, "Self": Self }
def newfuncD(operation, name="Fc", keep=True, is_jump=False, check=True): """ Parameters ---------- operation : callable the detail of opearation only accept +,-,*,/,-(negative),x^n name:str name keep:bool the group size after this function. true is the input size,and false is 1. is_jump:bool the bool means the rem and rem_dim can be treat 2+ domension problems or not. check:bool check the function building """ func = Function(name) func.is_jump = is_jump func.keep = keep def npf(x): if isinstance(x, np.ndarray): if x.ndim == 2: if x.shape[0] == 2: res = operation(x[0], x[1]) if keep: assert isinstance(res, tuple) return np.array(res) else: # >=3 if is_jump: return x else: return np.array(operation(*x)) else: # 1 return x else: # number return x def gsymf(x): if isinstance(x, (np.ndarray, NewArray)): if x.shape[0] == 2: res = operation(x[0], x[1]) if keep: return NewArray(res) else: return res else: # >=3 if is_jump: return x else: res = operation(*x) if keep: return NewArray(res) else: return res else: # 1 return x def dimf(dim): if isinstance(dim, Dim): if dim.ndim == 1: return dim elif dim.shape[0] == 2: if keep: return Dim(operation(*dim)) else: return Dim(operation(*dim)) else: if is_jump: return dim else: return Dim(operation(*dim)) else: # number return dim res = { "func": func, "arity": 1, "name": name, "np_func": npf, "dim_func": dimf, "sym_func": gsymf } if check: check_funcD(res) return res