Add assumptions to functions #1565
Replies: 2 comments
-
TODO: Determine the best way to add a check that can be disabled in a release version of the code. In CMake the release version is defined by providing the preprocessor |
Beta Was this translation helpful? Give feedback.
-
This could also potentially be used to help with assumed positivity of the base of a power operator as discussed in PR #445 and to control division by zero (including in a modulo). However the code could become quite illegible if there are assertions at every power operator. It is therefore advised to only have assertions about the arguments at the start of the function |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
Sometimes functions have constraints which should be made explicit. Errors should be raised when these constraints are not followed.
E.g.
For this calculation we are assuming that x and y have the same size. Up until PR #437 this has been done by setting the size to None. This is now explicit as it is necessary in order to carry out this operation in C. In addition once this assumption is explicit then we know the size of c in the following function:
The problem arises when passing the function arrays of different sizes. In numpy this would raise a
ValueError
. A short test shows that in fortran calling the following (translated):gives
and the following:
gives
If the flag
-fcheck=bounds
is used then a Fortran Runtime Error occursThe method proposed to handle this case is to create "assumptions" for the arguments. These assumptions could then be checked during the translation or in the wrapper (when we no longer use f2py, or in the f2py_XX.f90 file) or in the code itself in debug mode.
E.g. for:
We would generate the assumption
shape(a,1)==shape(b,1)
If this is not true when we call the function then we can raise an error. In the cwrapper it is possible to raise python errors e.g. ValueError as numpy does
As @yguclu pointed out
So for the following codes:
Code A
Code B
we would generate an assumption shape(a,1)==shape(b,1) for the function test_add. We then have 3 options:
Beta Was this translation helpful? Give feedback.
All reactions