[docs]defsum(x,axis:int=None):""" Sum of array elements over a given axis. See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.sum.html """ifnotis_casadi_type(x):return_onp.sum(x,axis=axis)else:ifaxis==0:return_cas.sum1(x).Telifaxis==1:return_cas.sum2(x)elifaxisisNone:returnsum(sum(x,axis=0),axis=0)else:raiseValueError("CasADi types can only be up to 2D, so `axis` must be None, 0, or 1.")
[docs]defmean(x,axis:int=None):""" Compute the arithmetic mean along the specified axis. See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.mean.html """ifnotis_casadi_type(x):return_onp.mean(x,axis=axis)else:ifaxis==0:returnsum(x,axis=0)/x.shape[0]elifaxis==1:returnsum(x,axis=1)/x.shape[1]elifaxisisNone:returnmean(mean(x,axis=0),axis=1)else:raiseValueError("CasADi types can only be up to 2D, so `axis` must be None, 0, or 1.")
# TODO trace()# def cumsum(x, axis: int = None):# """# Return the cumulative sum of the elements along a given axis.## See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.cumsum.html# """## if not is_casadi_type(x):# return _onp.cumsum(x, axis=axis)## else:# raise NotImplementedError# if axis is None:# return _cas.cumsum(_onp.flatten(x))
[docs]defprod(x,axis:int=None):""" Return the product of array elements over a given axis. See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.prod.html """ifnotis_casadi_type(x):return_onp.prod(x,axis=axis)else:return_cas.exp(sum(_cas.log(x),axis=axis))