-
Notifications
You must be signed in to change notification settings - Fork 280
Feature: Expr and GenExpr support NumPy binary functions like np.add
#1203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 7 commits
d5118a7
3a557ab
fcdc604
deee28a
554f534
8a3ba12
cdf3d12
08c5088
ab6805b
9fdab6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -246,7 +246,32 @@ cdef class ExprLike: | |
| ) | ||
|
|
||
| if method == "__call__": | ||
| if ufunc is np.absolute: | ||
| if arrays := [a for a in args if type(a) is np.ndarray]: | ||
| if any(a.dtype.kind not in "fiub" for a in arrays): | ||
| return NotImplemented | ||
| # If the np.ndarray is of numeric type, all arguments are converted to | ||
| # MatrixExpr or MatrixGenExpr and then the ufunc is applied. | ||
| return ufunc(*[_to_matrix(a) for a in args], **kwargs) | ||
|
||
|
|
||
| if ufunc is np.add: | ||
| return args[0] + args[1] | ||
| elif ufunc is np.subtract: | ||
| return args[0] - args[1] | ||
| elif ufunc is np.multiply: | ||
| return args[0] * args[1] | ||
| elif ufunc in {np.divide, np.true_divide}: | ||
| return args[0] / args[1] | ||
| elif ufunc is np.power: | ||
| return args[0] ** args[1] | ||
| elif ufunc is np.negative: | ||
| return -args[0] | ||
| elif ufunc is np.less_equal: | ||
| return args[0] <= args[1] | ||
| elif ufunc is np.greater_equal: | ||
| return args[0] >= args[1] | ||
| elif ufunc is np.equal: | ||
| return args[0] == args[1] | ||
| elif ufunc is np.absolute: | ||
| return args[0].__abs__() | ||
| elif ufunc is np.exp: | ||
| return args[0].exp() | ||
|
|
@@ -1031,6 +1056,12 @@ cdef inline object _wrap_ufunc(object x, object ufunc): | |
| return res.view(MatrixGenExpr) if isinstance(res, np.ndarray) else res | ||
| return ufunc(_to_const(x)) | ||
|
|
||
| cdef inline object _to_matrix(object arg): | ||
| if type(arg) is np.ndarray: | ||
| return arg.view(MatrixExpr) | ||
| matrix = MatrixExpr if isinstance(arg, Expr) else MatrixGenExpr | ||
| return np.array(arg, dtype=object).view(matrix) | ||
|
|
||
|
|
||
| def expr_to_nodes(expr): | ||
| '''transforms tree to an array of nodes. each node is an operator and the position of the | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.