Commit d9e9530
Feature: Expr and GenExpr support numpy unary func like
* Add numpy ufunc support for expression classes
Introduced the ExprLike base class with __array_ufunc__ to enable numpy universal function (ufunc) support for Expr and GenExpr. Replaced standalone exp, log, sqrt, sin, and cos functions with numpy equivalents and mapped them for ufunc dispatch. This change improves interoperability with numpy and simplifies the codebase.
* Introduce ExprLike base class for expressions
Added a new ExprLike base class and made Expr inherit from it. This refactoring prepares the codebase for future extensions or polymorphism involving expression-like objects.
* Fix unary ufunc mapping in ExprLike class
Corrects the method call to use the first argument in the unary ufunc mapping, ensuring the correct object is used when applying numpy universal functions.
* Add tests for unary operations and numpy compatibility
Introduces tests for unary operations such as abs, sin, and sqrt, including their numpy equivalents, to ensure correct string representations and compatibility with numpy functions.
* Update CHANGELOG.md
* Add colon to ExprLike class definition
Added a missing colon to the ExprLike class definition in scip.pxd to conform with Python/Cython syntax.
* Fix test expectations for variable names in unary ops
Updated test assertions in test_unary to expect variable names 'x', 'y', and 'z' instead of 'x1'. This aligns the tests with the current string representation of expressions.
* Fix expected output format in test_unary
Updated the expected string in test_unary to match the correct output format by removing commas between list elements.
* Fix expected output for sin function in test_unary
Corrected the expected string in test_unary to use 'sin' instead of 'abs' for the sin([x, y, z]) test case.
* Add _evaluate method to Constant class
Introduces a cpdef _evaluate method to the Constant class, returning the constant's value. This provides a consistent evaluation interface for expressions.
* Update genexpr power tests for sqrt handling
Removes the NotImplementedError expectation when raising genexpr to sqrt(2) and instead asserts the result is a GenExpr. Adds a new test to expect TypeError when raising to sqrt('2').
* Update test_unary to use two variables instead of three
The test_unary function was moved and modified to test unary operations on lists containing two variables (x, y) instead of three (x, y, z). This streamlines the tests and aligns them with the current requirements.
* Refactor expression classes with ExprLike base
Introduced a new ExprLike base class to encapsulate common mathematical methods such as __abs__, exp, log, sqrt, sin, and cos. Updated Expr and GenExpr to inherit from ExprLike, reducing code duplication and improving maintainability.
* Add __array_ufunc__ to ExprLike type stub
Introduces the __array_ufunc__ method to the ExprLike class in the type stubs, enabling better compatibility with NumPy ufuncs.
* Update numpy import and type annotations in stubs
Changed 'import numpy' to 'import numpy as np' and updated type annotations to use 'np.ndarray' instead of 'numpy.ndarray' in the scip.pyi stub file. Also added UNARY_MAPPER as a Dict[np.ufunc, str].
* Update __array_ufunc__ type hints to use np.ufunc and str
Replaces 'Incomplete' type hints with more specific types (np.ufunc and str) for the __array_ufunc__ methods in ExprLike, MatrixExpr, and MatrixExprCons classes to improve type accuracy.
* Reorder and add math functions in scip.pyi
Reordered the declarations of math functions and added missing entries for log, sin, and sqrt in the scip.pyi stub file to improve completeness and maintain consistency.
* Reorder Operator and PATCH declarations in scip.pyi
Moved the Operator type annotation below PATCH to maintain consistent ordering of variable declarations in the scip.pyi file.
* Remove @disjoint_base decorator from ExprLike
The @disjoint_base decorator was removed from the ExprLike class in the type stub. This may reflect a change in the class hierarchy or decorator usage.
* Fix UNARY_MAPPER to use local math function references
Replaces numpy function references in UNARY_MAPPER with local aliases (exp, log, sqrt, sin, cos) to ensure correct mapping and avoid issues with numpy function identity.
* Update typing for UNARY_MAPPER in scip.pyi
Replaces the use of Dict from typing with the built-in dict for the UNARY_MAPPER type annotation. This modernizes the type hint and removes an unused import.
* Remove unused UNARY_MAPPER from type stub
Deleted the UNARY_MAPPER dictionary from scip.pyi as it is no longer needed or used in the type stub.
* Add return type annotations to ExprLike methods
Updated the ExprLike class methods (__abs__, exp, log, sqrt, sin, cos) to specify GenExpr as their return type in both the implementation and the type stub. This improves type safety and clarity for users and tools.
* Format: add blank line in GenExpr class
Insert a blank line after the 'cdef class GenExpr(ExprLike):' declaration in src/pyscipopt/expr.pxi to improve readability and code formatting.
* Refactor unary tests and add more unary ops
Consolidate repeated expected strings into temporary `res` variables for clarity and add tests covering additional unary functions (cos, exp, log) and their numpy equivalents. Also add scalar comparisons for sqrt, exp, log, sin, cos to improve test coverage and readability.
* Add array assertions for unary ops
Extend tests in tests/test_expr.py to verify elementwise behavior of unary functions with array inputs. Added assert all(...) checks for sqrt, exp, log, sin, and cos comparing results to numpy equivalents to ensure these functions handle array inputs correctly.
* Add test for invalid unary arcsin operation
Expand test_unary in tests/test_expr.py to assert that calling np.arcsin on the symbolic x raises a TypeError. This adds coverage for invalid unary operations to ensure the code properly reports type errors for unsupported operand types.
* Update CHANGELOG: numpy unary function notes
Add Unreleased notes for Expr and GenExpr support for numpy unary functions (np.sin, np.cos, np.sqrt, np.exp, np.log, np.absolute) and clarify that unary functions that apply a constant now return a constant instead of a GenExpr. Remove duplicate entries of these notes from the 6.1.0 and 6.0.0 sections.
* pyscipopt.sqrt(2) will return a Constant class
* Expect NotImplementedError for genexpr ** sqrt(2)
Update test to reflect that powering a generator expression by a non-constant expression should raise NotImplementedError. Replace the previous assertion that the operation succeeded and remove the TypeError case for sqrt("2"). Add a clarifying comment about only allowing power to constant expressions.
* Dispatch numpy ufuncs for expr functions
Replace per-function PyNumber_Check lambdas with a unified _dispatch_ufunc helper that vectorizes conversion of Python numbers to Constant via _to_const and np.frompyfunc. Removed the direct cimport of PyNumber_Check and updated exp/log/sqrt/sin/cos to call _dispatch_ufunc. _dispatch_ufunc also coerces ndarray results to MatrixExpr, centralizing numpy ufunc handling and improving array support for expression construction.
* Update expr tests for MatrixExpr and repr
Add MatrixExpr import and adjust unary-expression tests to match symbolic representations and matrix behavior. Replace numeric equality checks with string-based assertions for sqrt/exp/log/sin/cos, add a log([1,x]) repr check, and add isinstance checks for GenExpr and MatrixExpr to verify scalar vs. matrix expression types and nested/matrix shapes.
* Fix ufunc dispatch for scalars
update _dispatch_ufunc to distinguish iterables from scalars. For ndarray/list/tuple inputs the code applies the vectorized conversion and returns a MatrixExpr view for numpy arrays; for scalars it calls the scalar _to_const directly. This avoids incorrect wrapping of scalar inputs and ensures correct return types.
* Rename _dispatch_ufunc to _wrap_ufunc
* Use MatrixGenExpr for ufunc results
Return MatrixGenExpr (not MatrixExpr) from _wrap_ufunc for ndarray ufunc results in expr.pxi, and update tests to import and expect MatrixGenExpr. This aligns unary ufunc behavior (e.g., sqrt on lists/arrays) with the generator matrix expression type and fixes related type assertions in tests.
* Potential fix for pull request finding
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
* Update CHANGELOG.md
* Reject 'out' kwarg in __array_ufunc__
Add a guard to ExprLike.__array_ufunc__ that raises a TypeError if the numpy ufunc 'out' keyword argument is provided. Expression objects do not support writing into preallocated output arrays, so this explicit error prevents silent misuse or incorrect behavior when numpy ufuncs pass an 'out' parameter.
* Test: forbid np.sin out= on Expr/Variable
Add a unit test in tests/test_expr.py to assert that calling np.sin with an out= parameter on a Variable/Expr/GenExpr raises TypeError. This prevents in-place modification of expression objects via NumPy ufunc out arguments and documents the intended behavior.
* Use MatrixGenExpr view for GenExpr arrays
* Add docstring to _wrap_ufunc
Add an explanatory docstring to the _wrap_ufunc helper describing how it handles scalars and collections: converting numeric inputs to Constant expressions, applying the ufunc element-wise via np.frompyfunc for arrays/lists/tuples, and returning a MatrixGenExpr for ndarrays or a list/tuple of GenExprs otherwise. Documentation-only change; no functional behavior modified.
* Use Constant.log().exp() for pow with float base
directly use inner method
* Fix merge conflict artifacts in stubs and test imports
* Update src/pyscipopt/scip.pyi
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Clarify _wrap_ufunc docstring returns
Expand and reformat the _wrap_ufunc docstring to include a dedicated Returns section. The new text clarifies the function's return types for scalar vs. vector inputs (GenExpr/MatrixGenExpr) and explains that vectors yield an array of the same shape with the ufunc applied element-wise. Also minor formatting/line-wrap cleanup.
* Replace ufunc lambdas with documented functions
Replace terse lambda aliases for exp, log, sqrt, sin, and cos with full function definitions that call _wrap_ufunc. Each function now includes a detailed docstring describing parameters, behavior for scalars and vectors, and return types. This improves readability and documents expected input/outputs without changing functionality (still delegates to _wrap_ufunc and numpy ufuncs).
---------
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: João Dionísio <57299939+Joao-Dionisio@users.noreply.github.com>
Co-authored-by: Joao-Dionisio <joao.goncalves.dionisio@gmail.com>np.sin (#1170)1 parent 5329bc1 commit d9e9530
File tree
5 files changed
+303
-75
lines changed- src/pyscipopt
- tests
5 files changed
+303
-75
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
47 | 49 | | |
48 | 50 | | |
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
53 | 55 | | |
54 | | - | |
55 | | - | |
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
| |||
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
| 197 | + | |
197 | 198 | | |
198 | 199 | | |
199 | 200 | | |
| |||
223 | 224 | | |
224 | 225 | | |
225 | 226 | | |
226 | | - | |
| 227 | + | |
227 | 228 | | |
228 | 229 | | |
229 | 230 | | |
230 | 231 | | |
231 | 232 | | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
232 | 283 | | |
233 | 284 | | |
234 | | - | |
235 | | - | |
| 285 | + | |
| 286 | + | |
236 | 287 | | |
237 | 288 | | |
238 | 289 | | |
| |||
250 | 301 | | |
251 | 302 | | |
252 | 303 | | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | 304 | | |
257 | 305 | | |
258 | 306 | | |
| |||
356 | 404 | | |
357 | 405 | | |
358 | 406 | | |
359 | | - | |
| 407 | + | |
360 | 408 | | |
361 | 409 | | |
362 | | - | |
| 410 | + | |
363 | 411 | | |
364 | 412 | | |
365 | 413 | | |
| |||
518 | 566 | | |
519 | 567 | | |
520 | 568 | | |
521 | | - | |
| 569 | + | |
522 | 570 | | |
523 | 571 | | |
524 | 572 | | |
525 | 573 | | |
526 | 574 | | |
527 | 575 | | |
528 | 576 | | |
529 | | - | |
530 | | - | |
531 | | - | |
532 | 577 | | |
533 | 578 | | |
534 | 579 | | |
| |||
655 | 700 | | |
656 | 701 | | |
657 | 702 | | |
658 | | - | |
| 703 | + | |
659 | 704 | | |
660 | 705 | | |
661 | | - | |
| 706 | + | |
662 | 707 | | |
663 | 708 | | |
664 | 709 | | |
| |||
832 | 877 | | |
833 | 878 | | |
834 | 879 | | |
835 | | - | |
836 | | - | |
837 | | - | |
838 | | - | |
839 | | - | |
840 | | - | |
841 | | - | |
842 | | - | |
843 | | - | |
844 | | - | |
845 | | - | |
846 | | - | |
847 | | - | |
848 | | - | |
849 | | - | |
850 | | - | |
851 | | - | |
852 | | - | |
853 | | - | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
854 | 1033 | | |
855 | | - | |
856 | | - | |
857 | | - | |
858 | | - | |
859 | | - | |
860 | | - | |
861 | | - | |
862 | | - | |
863 | | - | |
864 | | - | |
865 | | - | |
866 | | - | |
867 | | - | |
868 | | - | |
869 | | - | |
870 | | - | |
871 | | - | |
872 | | - | |
873 | | - | |
874 | | - | |
875 | | - | |
876 | | - | |
877 | | - | |
878 | | - | |
879 | | - | |
880 | | - | |
881 | | - | |
882 | | - | |
883 | | - | |
884 | 1034 | | |
885 | 1035 | | |
886 | 1036 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2128 | 2128 | | |
2129 | 2129 | | |
2130 | 2130 | | |
2131 | | - | |
| 2131 | + | |
| 2132 | + | |
| 2133 | + | |
| 2134 | + | |
2132 | 2135 | | |
2133 | 2136 | | |
2134 | 2137 | | |
| |||
0 commit comments