36
36
37
37
# NumPy version and a convenient boolean flag
38
38
NUMPY_GE_2_0 = np .__version__ >= "2.0"
39
+ # handle different numpy versions
40
+ if NUMPY_GE_2_0 : # array-api compliant
41
+ nplshift = np .bitwise_left_shift
42
+ nprshift = np .bitwise_right_shift
43
+ npbinvert = np .bitwise_invert
44
+ else : # not array-api compliant
45
+ nplshift = np .left_shift
46
+ nprshift = np .right_shift
47
+ npbinvert = np .bitwise_not
48
+
49
+ # These functions in ufunc_map in ufunc_map_1param are implemented in numexpr and so we call
50
+ # those instead (since numexpr uses multithreading it is faster)
51
+ ufunc_map = {
52
+ np .add : "+" ,
53
+ np .subtract : "-" ,
54
+ np .multiply : "*" ,
55
+ np .divide : "/" ,
56
+ np .true_divide : "/" ,
57
+ np .floor_divide : "//" ,
58
+ np .power : "**" ,
59
+ np .less : "<" ,
60
+ np .less_equal : "<=" ,
61
+ np .greater : ">" ,
62
+ np .greater_equal : ">=" ,
63
+ np .equal : "==" ,
64
+ np .not_equal : "!=" ,
65
+ np .bitwise_and : "&" ,
66
+ np .bitwise_or : "|" ,
67
+ np .bitwise_xor : "^" ,
68
+ np .arctan2 : "arctan2" ,
69
+ nplshift : "<<" , # nplshift selected above according to numpy version
70
+ nprshift : ">>" , # nprshift selected above according to numpy version
71
+ np .remainder : "%" ,
72
+ np .nextafter : "nextafter" ,
73
+ np .copysign : "copysign" ,
74
+ np .hypot : "hypot" ,
75
+ np .maximum : "maximum" ,
76
+ np .minimum : "minimum" ,
77
+ }
78
+
79
+ # implemented in numexpr
80
+ ufunc_map_1param = {
81
+ np .sqrt : "sqrt" ,
82
+ np .sin : "sin" ,
83
+ np .cos : "cos" ,
84
+ np .tan : "tan" ,
85
+ np .arcsin : "arcsin" ,
86
+ np .arccos : "arccos" ,
87
+ np .arctan : "arctan" ,
88
+ np .sinh : "sinh" ,
89
+ np .cosh : "cosh" ,
90
+ np .tanh : "tanh" ,
91
+ np .arcsinh : "arcsinh" ,
92
+ np .arccosh : "arccosh" ,
93
+ np .arctanh : "arctanh" ,
94
+ np .exp : "exp" ,
95
+ np .expm1 : "expm1" ,
96
+ np .log : "log" ,
97
+ np .log10 : "log10" ,
98
+ np .log1p : "log1p" ,
99
+ np .log2 : "log2" ,
100
+ np .abs : "abs" ,
101
+ np .conj : "conj" ,
102
+ np .real : "real" ,
103
+ np .imag : "imag" ,
104
+ npbinvert : "~" , # npbinvert selected above according to numpy version
105
+ np .isnan : "isnan" ,
106
+ np .isfinite : "isfinite" ,
107
+ np .isinf : "isinf" ,
108
+ np .floor : "floor" ,
109
+ np .ceil : "ceil" ,
110
+ np .trunc : "trunc" ,
111
+ np .signbit : "signbit" ,
112
+ np .round : "round" ,
113
+ }
114
+
115
+ # implemented in python-blosc2
116
+ local_ufunc_map = {
117
+ np .logaddexp : blosc2 .logaddexp ,
118
+ np .logical_not : blosc2 .logical_not ,
119
+ np .logical_and : blosc2 .logical_and ,
120
+ np .logical_or : blosc2 .logical_or ,
121
+ np .logical_xor : blosc2 .logical_xor ,
122
+ }
39
123
40
124
41
125
@runtime_checkable
@@ -2928,17 +3012,6 @@ def chunkwise_logaddexp(inputs, output, offset):
2928
3012
return blosc2 .lazyudf (chunkwise_logaddexp , (x1 , x2 ), dtype = dtype , shape = x1 .shape )
2929
3013
2930
3014
2931
- # handle different numpy versions
2932
- if NUMPY_GE_2_0 : # array-api compliant
2933
- nplshift = np .bitwise_left_shift
2934
- nprshift = np .bitwise_right_shift
2935
- npbinvert = np .bitwise_invert
2936
- else : # not array-api compliant
2937
- nplshift = np .left_shift
2938
- nprshift = np .right_shift
2939
- npbinvert = np .bitwise_not
2940
-
2941
-
2942
3015
class Operand :
2943
3016
"""Base class for all operands in expressions."""
2944
3017
@@ -2989,92 +3062,12 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
2989
3062
if method != "__call__" :
2990
3063
return NotImplemented
2991
3064
2992
- # These functions in ufunc_map in ufunc_map_1param are implemented in numexpr and so we call
2993
- # those instead (since numexpr uses multithreading it is faster)
2994
- ufunc_map = {
2995
- np .add : "+" ,
2996
- np .subtract : "-" ,
2997
- np .multiply : "*" ,
2998
- np .divide : "/" ,
2999
- np .true_divide : "/" ,
3000
- np .floor_divide : "//" ,
3001
- np .power : "**" ,
3002
- np .less : "<" ,
3003
- np .less_equal : "<=" ,
3004
- np .greater : ">" ,
3005
- np .greater_equal : ">=" ,
3006
- np .equal : "==" ,
3007
- np .not_equal : "!=" ,
3008
- np .bitwise_and : "&" ,
3009
- np .bitwise_or : "|" ,
3010
- np .bitwise_xor : "^" ,
3011
- np .arctan2 : "arctan2" ,
3012
- nplshift : "<<" , # nplshift selected above according to numpy version
3013
- nprshift : ">>" , # nprshift selected above according to numpy version
3014
- np .remainder : "%" ,
3015
- np .nextafter : "nextafter" ,
3016
- np .copysign : "copysign" ,
3017
- np .hypot : "hypot" ,
3018
- np .maximum : "maximum" ,
3019
- np .minimum : "minimum" ,
3020
- }
3021
-
3022
- # implemented in numexpr
3023
- ufunc_map_1param = {
3024
- np .sqrt : "sqrt" ,
3025
- np .sin : "sin" ,
3026
- np .cos : "cos" ,
3027
- np .tan : "tan" ,
3028
- np .arcsin : "arcsin" ,
3029
- np .arccos : "arccos" ,
3030
- np .arctan : "arctan" ,
3031
- np .sinh : "sinh" ,
3032
- np .cosh : "cosh" ,
3033
- np .tanh : "tanh" ,
3034
- np .arcsinh : "arcsinh" ,
3035
- np .arccosh : "arccosh" ,
3036
- np .arctanh : "arctanh" ,
3037
- np .exp : "exp" ,
3038
- np .expm1 : "expm1" ,
3039
- np .log : "log" ,
3040
- np .log10 : "log10" ,
3041
- np .log1p : "log1p" ,
3042
- np .log2 : "log2" ,
3043
- np .abs : "abs" ,
3044
- np .conj : "conj" ,
3045
- np .real : "real" ,
3046
- np .imag : "imag" ,
3047
- npbinvert : "~" , # npbinvert selected above according to numpy version
3048
- np .isnan : "isnan" ,
3049
- np .isfinite : "isfinite" ,
3050
- np .isinf : "isinf" ,
3051
- np .floor : "floor" ,
3052
- np .ceil : "ceil" ,
3053
- np .trunc : "trunc" ,
3054
- np .signbit : "signbit" ,
3055
- np .round : "round" ,
3056
- }
3057
-
3058
- # implemented in python-blosc2
3059
- local_ufunc_map = {
3060
- np .logaddexp : logaddexp ,
3061
- np .logical_not : logical_not ,
3062
- np .logical_and : logical_and ,
3063
- np .logical_or : logical_or ,
3064
- np .logical_xor : logical_xor ,
3065
- }
3066
3065
if ufunc in local_ufunc_map :
3067
3066
return local_ufunc_map [ufunc ](* inputs )
3068
3067
3069
3068
if ufunc in ufunc_map :
3070
3069
value = inputs [0 ] if inputs [1 ] is self else inputs [1 ]
3071
3070
_check_allowed_dtypes (value )
3072
- # catch special case of multiplying two bools (not implemented in numexpr)
3073
- if ufunc_map [ufunc ] == "*" and blosc2 .result_type (value , self ) == blosc2 .bool_ :
3074
- return blosc2 .LazyExpr (new_op = (value , "&" , self ))
3075
- # catch special case of adding two bools (not implemented in numexpr)
3076
- if ufunc_map [ufunc ] == "+" and blosc2 .result_type (value , self ) == blosc2 .bool_ :
3077
- return blosc2 .LazyExpr (new_op = (value , "|" , self ))
3078
3071
return blosc2 .LazyExpr (new_op = (value , ufunc_map [ufunc ], self ))
3079
3072
3080
3073
if ufunc in ufunc_map_1param :
@@ -3086,8 +3079,6 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
3086
3079
3087
3080
def __add__ (self , value : int | float | blosc2 .Array , / ) -> blosc2 .LazyExpr :
3088
3081
_check_allowed_dtypes (value )
3089
- if blosc2 .result_type (value , self ) == blosc2 .bool_ :
3090
- return blosc2 .LazyExpr (new_op = (value , "|" , self ))
3091
3082
return blosc2 .LazyExpr (new_op = (self , "+" , value ))
3092
3083
3093
3084
def __iadd__ (self , value : int | float | blosc2 .Array , / ) -> blosc2 .LazyExpr :
@@ -3123,9 +3114,6 @@ def __rsub__(self, value: int | float | blosc2.Array, /) -> blosc2.LazyExpr:
3123
3114
@is_documented_by (multiply )
3124
3115
def __mul__ (self , value : int | float | blosc2 .Array , / ) -> blosc2 .LazyExpr :
3125
3116
_check_allowed_dtypes (value )
3126
- # catch special case of multiplying two bools (not implemented in numexpr)
3127
- if blosc2 .result_type (value , self ) == blosc2 .bool_ :
3128
- return blosc2 .LazyExpr (new_op = (value , "&" , self ))
3129
3117
return blosc2 .LazyExpr (new_op = (self , "*" , value ))
3130
3118
3131
3119
def __imul__ (self , value : int | float | blosc2 .Array , / ) -> blosc2 .LazyExpr :
0 commit comments