-
Notifications
You must be signed in to change notification settings - Fork 189
implemented clip function #355
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2608434
implemented generic clip function
aman-godara 5d108de
addedunit test for clip function; added math subdirectory in CMakeLis…
aman-godara 243ac91
documented clip function in newly created stdlib_math.md file
aman-godara 336285d
improved test_math.fypp: used int_8, _int16, _int32,... & removed som…
aman-godara 008506a
Update doc/specs/stdlib_math.md
aman-godara a427220
Update doc/specs/stdlib_math.md
aman-godara 7ba9853
fix indentation
milancurcic 8922365
move the note about xmax > xmin requirement to the arguments spec
milancurcic d6f9773
Updated PR as per the feedback received
aman-godara d17df5e
Deleted test_math.fypp file from PR
aman-godara 41247e2
Merge branch 'master' into develop_clip
aman-godara 5987902
Fix separator in testing Makefile
awvwgk c3ae302
removed auto-generation of empty line by .fypp file, updated stdlib_m…
aman-godara d66cee2
changed indentation, added empty line in .fypp file back, replaced co…
aman-godara 542c024
removed wrapper test_clip, renamed test_math to test_stdlib_math
aman-godara fd684bd
improved aesthetics of test_stdlib_math.f90 file
aman-godara f4ffaa6
update program name and fix indentation
milancurcic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
--- | ||
title: math | ||
--- | ||
|
||
# The `stdlib_math` module | ||
|
||
[TOC] | ||
|
||
## Introduction | ||
|
||
`stdlib_math` module provides general purpose mathematical functions. | ||
|
||
|
||
## Procedures and Methods provided | ||
|
||
|
||
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> | ||
### `clip` function | ||
|
||
#### Description | ||
|
||
Returns a value which lies in the given interval [`xmin`, `xmax`] (interval is `xmin` and `xmax` inclusive) and is closest to the input value `x`. | ||
|
||
#### Syntax | ||
|
||
`res = [[stdlib_math(module):clip(interface)]] (x, xmin, xmax)` | ||
|
||
#### Status | ||
|
||
Experimental | ||
|
||
#### Class | ||
|
||
Elemental function. | ||
|
||
#### Argument(s) | ||
|
||
`x`: scalar of either `integer` or `real` type. This argument is `intent(in)`. | ||
`xmin`: scalar of either `integer` or `real` type. This argument is `intent(in)`. | ||
`xmax`: scalar of either `integer` or `real` type, which must be greater than or equal to `xmin`. This argument is `intent(in)`. | ||
|
||
Note: All arguments must have same `type` and same `kind`. | ||
|
||
#### Output value or Result value | ||
|
||
The output is a scalar of `type` and `kind` same as to that of the arguments. | ||
|
||
#### Examples | ||
|
||
##### Example 1: | ||
|
||
Here inputs are of type `integer` and kind `int32` | ||
```fortran | ||
program demo_clip_integer | ||
use stdlib_math, only: clip | ||
use stdlib_kinds, only: int32 | ||
implicit none | ||
integer(int32) :: x | ||
integer(int32) :: xmin | ||
integer(int32) :: xmax | ||
integer(int32) :: clipped_value | ||
|
||
xmin = -5_int32 | ||
xmax = 5_int32 | ||
x = 12_int32 | ||
|
||
clipped_value = clip(x, xmin, xmax) | ||
! clipped_value <- 5 | ||
end program demo_clip_integer | ||
``` | ||
|
||
##### Example 2: | ||
|
||
Here inputs are of type `real` and kind `sp` | ||
```fortran | ||
program demo_clip_real | ||
use stdlib_math, only: clip | ||
use stdlib_kinds, only: sp | ||
implicit none | ||
real(sp) :: x | ||
real(sp) :: xmin | ||
real(sp) :: xmax | ||
real(sp) :: clipped_value | ||
|
||
xmin = -5.769_sp | ||
xmax = 3.025_sp | ||
x = 3.025_sp | ||
|
||
clipped_value = clip(x, xmin, xmax) | ||
! clipped_value <- 3.02500010 | ||
end program demo_clip_real | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#:include "common.fypp" | ||
#:set IR_KINDS_TYPES = INT_KINDS_TYPES + REAL_KINDS_TYPES | ||
|
||
module stdlib_math | ||
use stdlib_kinds, only: int8, int16, int32, int64, sp, dp, qp | ||
|
||
implicit none | ||
private | ||
public :: clip | ||
|
||
interface clip | ||
#:for k1, t1 in IR_KINDS_TYPES | ||
module procedure clip_${k1}$ | ||
#:endfor | ||
end interface clip | ||
|
||
contains | ||
|
||
#:for k1, t1 in IR_KINDS_TYPES | ||
elemental function clip_${k1}$(x, xmin, xmax) result(res) | ||
${t1}$, intent(in) :: x | ||
${t1}$, intent(in) :: xmin | ||
${t1}$, intent(in) :: xmax | ||
${t1}$ :: res | ||
|
||
res = max(min(x, xmax), xmin) | ||
end function clip_${k1}$ | ||
|
||
#:endfor | ||
end module stdlib_math |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ADDTEST(stdlib_math) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
PROGS_SRC = test_stdlib_math.f90 | ||
|
||
|
||
include ../Makefile.manual.test.mk |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
! SPDX-Identifier: MIT | ||
|
||
program test_stdlib_math | ||
use stdlib_math, only: clip | ||
use stdlib_error, only: check | ||
use stdlib_kinds, only: int8, int16, int32, int64, sp, dp, qp | ||
implicit none | ||
|
||
! clip function | ||
! testing format: check(clip(x, xmin, xmax) == correct answer) | ||
! valid case: xmin is not greater than xmax | ||
! invalid case: xmin is greater than xmax | ||
|
||
! type: integer(int8), kind: int8 | ||
! valid test case | ||
call check(clip(2_int8, -2_int8, 5_int8) == 2_int8, & | ||
'clip_int8 failed for valid case', warn=.true.) | ||
call check(clip(127_int8, -127_int8, 0_int8) == 0_int8, & | ||
'clip_int8 failed for valid case', warn=.true.) | ||
! invalid test case | ||
call check(clip(2_int8, 5_int8, -2_int8) == 5_int8, & | ||
'clip_int8 failed for invalid case', warn=.true.) | ||
call check(clip(127_int8, 0_int8, -127_int8) == 0_int8, & | ||
'clip_int8 failed for invalid case', warn=.true.) | ||
|
||
! type: integer(int16), kind: int16 | ||
! valid test case | ||
call check(clip(2_int16, -2_int16, 5_int16) == 2_int16, & | ||
'clip_int16 failed for valid case', warn=.true.) | ||
call check(clip(32767_int16, -32767_int16, 0_int16) == 0_int16, & | ||
'clip_int16 failed for valid case', warn=.true.) | ||
! invalid test case | ||
call check(clip(2_int16, 5_int16, -2_int16) == 5_int16, & | ||
'clip_int16 failed for invalid case', warn=.true.) | ||
call check(clip(32767_int16, 0_int16, -32767_int16) == 0_int16, & | ||
'clip_int16 failed for invalid case', warn=.true.) | ||
|
||
! type: integer(int32), kind: int32 | ||
! valid test case | ||
call check(clip(2_int32, -2_int32, 5_int32) == 2_int32, & | ||
'clip_int32 failed for valid case', warn=.true.) | ||
call check(clip(-2147483647_int32, 0_int32, 2147483647_int32) == 0_int32, & | ||
'clip_int32 failed for valid case', warn=.true.) | ||
! invalid test case | ||
call check(clip(2_int32, 5_int32, -2_int32) == 5_int32, & | ||
'clip_int32 failed for invalid case', warn=.true.) | ||
call check(clip(-2147483647_int32, 2147483647_int32, 0_int32) == 2147483647_int32, & | ||
'clip_int32 failed for invalid case', warn=.true.) | ||
|
||
! type: integer(int64), kind: int64 | ||
! valid test case | ||
call check(clip(2_int64, -2_int64, 5_int64) == 2_int64, & | ||
'clip_int64 failed for valid case', warn=.true.) | ||
call check(clip(-922337203_int64, -10_int64, 25_int64) == -10_int64, & | ||
'clip_int64 failed for valid case', warn=.true.) | ||
! invalid test case | ||
call check(clip(2_int64, 5_int64, -2_int64) == 5_int64, & | ||
'clip_int64 failed for invalid case', warn=.true.) | ||
call check(clip(-922337203_int64, 25_int64, -10_int64) == 25_int64, & | ||
'clip_int64 failed for invalid case', warn=.true.) | ||
|
||
! type: real(sp), kind: sp | ||
! valid test case | ||
call check(clip(3.025_sp, -5.77_sp, 3.025_sp) == 3.025_sp, & | ||
'clip_sp failed for valid case', warn=.true.) | ||
call check(clip(0.0_sp, -1578.025_sp, -59.68_sp) == -59.68_sp, & | ||
'clip_sp failed for valid case', warn=.true.) | ||
! invalid test case | ||
call check(clip(3.025_sp, 3.025_sp, -5.77_sp) == 3.025_sp, & | ||
'clip_sp failed for invalid case', warn=.true.) | ||
call check(clip(0.0_sp, -59.68_sp, -1578.025_sp) == -59.68_sp, & | ||
'clip_sp failed for invalid case', warn=.true.) | ||
|
||
! type: real(dp), kind: dp | ||
! valid test case | ||
call check(clip(3.025_dp, -5.77_dp, 3.025_dp) == 3.025_dp, & | ||
'clip_dp failed for valid case', warn=.true.) | ||
call check(clip(-7.0_dp, 0.059668_dp, 1.00268_dp) == 0.059668_dp, & | ||
'clip_dp failed for valid case', warn=.true.) | ||
! invalid test case | ||
call check(clip(3.025_dp, 3.025_dp, -5.77_dp) == 3.025_dp, & | ||
'clip_dp failed for invalid case', warn=.true.) | ||
call check(clip(-7.0_dp, 1.00268_dp, 0.059668_dp) == 1.00268_dp, & | ||
'clip_dp failed for invalid case', warn=.true.) | ||
|
||
! type: real(qp), kind: qp | ||
! valid test case | ||
call check(clip(3.025_qp, -5.77_qp, 3.025_qp) == 3.025_qp, & | ||
'clip_qp failed for valid case', warn=.true.) | ||
call check(clip(-55891546.2_qp, -8958133457.23_qp, -689712245.23_qp) == -689712245.23_qp, & | ||
'clip_qp failed for valid case', warn=.true.) | ||
! invalid test case | ||
call check(clip(3.025_qp, 3.025_qp, -5.77_qp) == 3.025_qp, & | ||
'clip_qp failed for invalid case', warn=.true.) | ||
call check(clip(-55891546.2_qp, -689712245.23_qp, -8958133457.23_qp) == -689712245.23_qp, & | ||
'clip_qp failed for invalid case', warn=.true.) | ||
|
||
end program test_stdlib_math |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.