-
Notifications
You must be signed in to change notification settings - Fork 183
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
Proposal for clamp #319
Comments
Do you think there will be much use for passing arrays to the If not, I would propose a fourth implementation option: A where clampThe procedures are auto-generated for each rank and type (real, double, ...)
The same logic applies to |
Concerning name I also have a personal preference for |
Is the usefulness of a where clamp to avoid it being elemental, like in #310 (comment)? If yes, is it so that clamp could be passed as a procedure pointer? If yes, it makes me think--how do you do it with a generic? I thought that generic can't be used as a procedure pointer because you need to specify the procedure interface in the list of dummy arguments. Or are there other benefits to non-elemental procedures? |
I don't think much but maybe some. I used it a few times in Python for 2-d arrays to clamp only within a radius and to do a masked clamp. There are alternative ways to do that though. But this could also be provided through specific functions that define |
No, in this context I was only playing with the idea of a generic but non-elemental interface to forbid usage such as:
which if the implementation were I haven't used the |
I see, that didn't occur to me. I can't think of an use for it. I agree that we should somehow forbid it or at least discourage it. I see a few solutions:
|
I would prefer to avoid run-time checking. This would make the procedures This leaves us with the first and third bullet points, meaning either Am I right that according to the specified behavior positive Inf values are mapped to |
Yes, I would too. We can |
My heart prefers the elemental approach (it's one of my favorite features of Fortran) but my head prefers the generic where clamp approach (compile-time enforcing of argument shape). Let's discuss it more broadly on the upcoming call. |
I wonder if this is something the standard should address - being able to explicitly enforce that |
Hello all, |
My current implementation with optional upper and lower bounds and 'hint' to the user if lower>upper (inspired by numpy). Probably
|
@art-rasa Yes, I think so, I've done this many times in the past (home cooked) and is very common in machine learning. Can you propose it in a separate issue? Ideally with an API, name suggestion, and example implementation. |
After discussing the elemental vs. where-clamp on the monthly call, I'm now more convinced that the elemental approach is just fine. The thing is, a where-clamp is a not so pretty hack to constrain how the user can invoke the function, and we're assuming that no user will want to use it exactly in the way that we would be preventing. So I'd rather have a more liberal and elegant solution, and point this out to the user in the docs. |
@milan Curcic <[email protected]> I concur. It is difficult to foresee use
patterns and most people will simply use it with scalar bounds, I imagine.
Actually,before you brought it up, I did not even think of using an array
for the bounds, though now that I write this, it is simply an extension on
min(a,b) and max(a,b) with both arguments an array and that is certainly
something I use from time to time!
Op wo 3 mrt. 2021 om 22:29 schreef Milan Curcic <[email protected]>:
… After discussion the elemental vs. where-clamp on the monthly call, I'm
now more convinced that the elemental approach is just fine. The thing is,
a where-clamp is a not so pretty hack to constrain how the user can invoke
the function, and we're assuming that some user may not want to use it
exactly in the way that we would be preventing. So I'd rather have a more
liberal and elegant solution, and point this out to the user in the docs.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#319 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN6YR344ONAOTQPFQVQXG3TB2S35ANCNFSM4XRNNGQQ>
.
|
What if user doesn't know for sure that the 2nd argument (xmin) is <= 3rd argument (xmax) but knows that this is the clip range in which the output should be? |
I would say that this is invalid use of the function, the user would simply need to ensure that this does not happen. It's not an uncommon situation that things are forbidden (square root of a negative real number, division by zero, exceeding the range of a certain datatype, ...). |
OK, let's leave it on user to provide smaller number as xmin and larger number as xmax. I liked the suggestions @MarDiehl has given above:
I think this provides user more enhanced feature in one function i.e. a user can give one of the two optional argument as None indicating that he/she doesn't want to use one of the two clips (xmin and xmax). If None is passed in both then it doesn't make any sense (we can raise a warning or return the input as the output) @MarDiehl can you please explain what this line does?
I thought of doing it by making a comparator function (func1) which gives -1, 0 or 1 as the output. |
I think it's important to have both--allow it to |
Hey!, how can I contribute now to get this issue closed? |
Okay, here are my suggestions:
|
This comment has been minimized.
This comment has been minimized.
@aman-godara Feel free to submit a patch for this issue. |
Intro
clamp(x, xmin, xmax)
is a function that clamps (or, clips) an input scalar or arrayx
so that the result is within given boundsxmin
andxmax
. For example:This function is common in standard libraries. I've used it occasionally in Fortran, and I use it often in Python. It provides an easy way to ensure that a variable with a finite (non-NaN, non-Inf) value stays within bounds.
Name
Python has
numpy.clip
, while C++, Julia, and Rust all haveclamp
. Soclamp
is more common. Bothclamp
andclip
are equally meaningful to me. I have slight preference forclip
because it's shorter and has a "sharper" sound to it.What inspired me to open this proposal is reading that
clamp
was stabilized in Rust 1.50.0.API
Alternative names for the bounds could be
low
andhigh
.Implementation
There are a few options that I know of, each of which produce different assembly instructions, and performance depending on optimization levels, array sizes, and whether the clamping is in a loop or over a whole array in a single call.
A min/max clamp
This is the simplest implementation I could think of:
There are two intrinsic function calls here, but maybe a good compiler will optimize one away. I don't know.
An if/then/else clamp
A branchless clamp
This avoids branching but introduces a floating-point error (~
epsilon(x)
) for some elements, even if x is not clipped.What module would this go to?
I don't think we have an appropriate existing module yet, but
stdlib_numeric
orstdlib_math
sound meaningful to me.The text was updated successfully, but these errors were encountered: