-
Notifications
You must be signed in to change notification settings - Fork 191
Implement trueloc/falseloc #603
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 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ae608ee
Implement trueloc/falseloc
awvwgk 7b5ffac
Add tests for empty and complete index list
awvwgk 4a4ac22
Use subroutine to implement logicalloc
awvwgk 7ddcbcc
Add equivalent pack to testsuite
awvwgk b015d9f
Update specification
awvwgk 1346066
Add relation of trueloc/falseloc with which/merge/pack
awvwgk 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
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,81 @@ | ||
--- | ||
title: array | ||
--- | ||
|
||
# The `stdlib_array` module | ||
|
||
[TOC] | ||
|
||
## Introduction | ||
|
||
Module for index manipulation and array handling tasks. | ||
|
||
## Procedures and methods provided | ||
|
||
|
||
### `trueloc` | ||
|
||
#### Status | ||
|
||
Experimental | ||
|
||
#### Description | ||
|
||
Turn a logical mask into an index array by selecting all true values. | ||
|
||
#### Syntax | ||
|
||
`call [[trueloc(function)]] (array[, lbound])` | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#### Arguments | ||
|
||
`array`: List of default logical arrays. This argument is `intent(in)`. | ||
|
||
`lbound`: Lower bound of the array to index. This argument is `optional` and `intent(in)`. | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#### Examples | ||
|
||
```fortran | ||
program demo | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use stdlib_array, only : trueloc | ||
implicit none | ||
real, allocatable :: array(:) | ||
allocate(array(500)) | ||
call random_number(array) | ||
array(trueloc(array > 0.5)) = 0.0 | ||
end program demo | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
|
||
### `falseloc` | ||
|
||
#### Status | ||
|
||
Experimental | ||
|
||
#### Description | ||
|
||
Turn a logical mask into an index array by selecting all false values. | ||
|
||
#### Syntax | ||
|
||
`call [[falseloc(function)]] (array[, lbound])` | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#### Arguments | ||
|
||
`array`: List of default logical arrays. This argument is `intent(in)`. | ||
|
||
`lbound`: Lower bound of the array to index. This argument is `optional` and `intent(in)`. | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#### Examples | ||
|
||
```fortran | ||
program demo | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use stdlib_array, only : falseloc | ||
implicit none | ||
real, allocatable :: array(:) | ||
allocate(array(-200:200)) | ||
call random_number(array) | ||
array(falseloc(array < 0.5), lbound(array)) = 0.0 | ||
end program demo | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` |
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,60 @@ | ||
! SPDX-Identifier: MIT | ||
|
||
!> Module for index manipulation and general array handling | ||
module stdlib_array | ||
implicit none | ||
private | ||
|
||
public :: trueloc, falseloc | ||
|
||
contains | ||
|
||
!> Return the positions of the true elements in array | ||
pure function trueloc(array, lbound) result(loc) | ||
!> Mask of logicals | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logical, intent(in) :: array(:) | ||
!> Lower bound of array to index | ||
integer, intent(in), optional :: lbound | ||
!> Locations of true elements | ||
integer :: loc(count(array)) | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
loc = logicalloc(array, .true., lbound) | ||
end function trueloc | ||
|
||
!> Return the positions of the false elements in array | ||
pure function falseloc(array, lbound) result(loc) | ||
!> Mask of logicals | ||
logical, intent(in) :: array(:) | ||
!> Lower bound of array to index | ||
integer, intent(in), optional :: lbound | ||
!> Locations of false elements | ||
integer :: loc(count(.not.array)) | ||
|
||
loc = logicalloc(array, .false., lbound) | ||
end function falseloc | ||
|
||
!> Return the positions of the truthy elements in array | ||
pure function logicalloc(array, truth, lbound) result(loc) | ||
!> Mask of logicals | ||
logical, intent(in) :: array(:) | ||
!> Truthy value | ||
logical, intent(in) :: truth | ||
!> Lower bound of array to index | ||
integer, intent(in), optional :: lbound | ||
!> Locations of truthy elements | ||
integer :: loc(count(array.eqv.truth)) | ||
integer :: i, pos, offset | ||
|
||
offset = 0 | ||
if (present(lbound)) offset = lbound - 1 | ||
|
||
i = 0 | ||
do pos = 1, size(array) | ||
if (array(pos).eqv.truth) then | ||
i = i + 1 | ||
loc(i) = pos + offset | ||
end if | ||
end do | ||
end function logicalloc | ||
|
||
end module stdlib_array |
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(logicalloc) |
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_logicalloc.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,154 @@ | ||
! SPDX-Identifier: MIT | ||
|
||
module test_logicalloc | ||
use stdlib_array, only : trueloc, falseloc | ||
use stdlib_string_type, only : string_type, len | ||
use testdrive, only : new_unittest, unittest_type, error_type, check | ||
implicit none | ||
private | ||
|
||
public :: collect_logicalloc | ||
|
||
contains | ||
|
||
!> Collect all exported unit tests | ||
subroutine collect_logicalloc(testsuite) | ||
!> Collection of tests | ||
type(unittest_type), allocatable, intent(out) :: testsuite(:) | ||
|
||
testsuite = [ & | ||
new_unittest("trueloc-where", test_trueloc_where), & | ||
new_unittest("trueloc-merge", test_trueloc_merge), & | ||
new_unittest("falseloc-where", test_falseloc_where), & | ||
new_unittest("falseloc-merge", test_falseloc_merge) & | ||
] | ||
end subroutine collect_logicalloc | ||
|
||
subroutine test_trueloc_where(error) | ||
!> Error handling | ||
type(error_type), allocatable, intent(out) :: error | ||
|
||
integer :: ndim | ||
real, allocatable :: avec(:), bvec(:), cvec(:) | ||
|
||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
do ndim = 100, 12000, 100 | ||
allocate(avec(ndim)) | ||
|
||
call random_number(avec) | ||
avec(:) = avec - 0.5 | ||
|
||
bvec = avec | ||
bvec(trueloc(bvec > 0)) = 0.0 | ||
|
||
cvec = avec | ||
where(cvec > 0) cvec = 0.0 | ||
|
||
call check(error, all(bvec == cvec)) | ||
deallocate(avec, bvec, cvec) | ||
if (allocated(error)) exit | ||
end do | ||
end subroutine test_trueloc_where | ||
|
||
subroutine test_trueloc_merge(error) | ||
!> Error handling | ||
type(error_type), allocatable, intent(out) :: error | ||
|
||
integer :: ndim | ||
real, allocatable :: avec(:), bvec(:), cvec(:) | ||
|
||
do ndim = 100, 12000, 100 | ||
allocate(avec(ndim)) | ||
|
||
call random_number(avec) | ||
avec(:) = avec - 0.5 | ||
|
||
bvec = avec | ||
bvec(trueloc(bvec > 0)) = 0.0 | ||
|
||
cvec = avec | ||
cvec(:) = merge(0.0, cvec, cvec > 0) | ||
|
||
call check(error, all(bvec == cvec)) | ||
deallocate(avec, bvec, cvec) | ||
if (allocated(error)) exit | ||
end do | ||
end subroutine test_trueloc_merge | ||
|
||
subroutine test_falseloc_where(error) | ||
!> Error handling | ||
type(error_type), allocatable, intent(out) :: error | ||
|
||
integer :: ndim | ||
real, allocatable :: avec(:), bvec(:), cvec(:) | ||
|
||
do ndim = 100, 12000, 100 | ||
allocate(avec(ndim)) | ||
|
||
call random_number(avec) | ||
avec(:) = avec - 0.5 | ||
|
||
bvec = avec | ||
bvec(falseloc(bvec > 0)) = 0.0 | ||
|
||
cvec = avec | ||
where(.not.(cvec > 0)) cvec = 0.0 | ||
|
||
call check(error, all(bvec == cvec)) | ||
deallocate(avec, bvec, cvec) | ||
if (allocated(error)) exit | ||
end do | ||
end subroutine test_falseloc_where | ||
|
||
subroutine test_falseloc_merge(error) | ||
!> Error handling | ||
type(error_type), allocatable, intent(out) :: error | ||
|
||
integer :: ndim | ||
real, allocatable :: avec(:), bvec(:), cvec(:) | ||
|
||
do ndim = 100, 12000, 100 | ||
allocate(avec(ndim)) | ||
|
||
call random_number(avec) | ||
avec(:) = avec - 0.5 | ||
|
||
bvec = avec | ||
bvec(falseloc(bvec > 0)) = 0.0 | ||
|
||
cvec = avec | ||
cvec(:) = merge(cvec, 0.0, cvec > 0) | ||
|
||
call check(error, all(bvec == cvec)) | ||
deallocate(avec, bvec, cvec) | ||
if (allocated(error)) exit | ||
end do | ||
end subroutine test_falseloc_merge | ||
|
||
end module test_logicalloc | ||
|
||
|
||
program tester | ||
use, intrinsic :: iso_fortran_env, only : error_unit | ||
use testdrive, only : run_testsuite, new_testsuite, testsuite_type | ||
use test_logicalloc, only : collect_logicalloc | ||
implicit none | ||
integer :: stat, is | ||
type(testsuite_type), allocatable :: testsuites(:) | ||
character(len=*), parameter :: fmt = '("#", *(1x, a))' | ||
|
||
stat = 0 | ||
|
||
testsuites = [ & | ||
new_testsuite("logicalloc", collect_logicalloc) & | ||
] | ||
|
||
do is = 1, size(testsuites) | ||
write(error_unit, fmt) "Testing:", testsuites(is)%name | ||
call run_testsuite(testsuites(is)%collect, error_unit, stat) | ||
end do | ||
|
||
if (stat > 0) then | ||
write(error_unit, '(i0, 1x, a)') stat, "test(s) failed!" | ||
error stop | ||
end if | ||
end program |
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.