Skip to content
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

Implement trueloc/falseloc #603

Merged
merged 6 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Features available from the latest git source

- new module `stdlib_array`
[#603](https://github.com/fortran-lang/stdlib/pull/603)
- new procedures `trueloc`, `falseloc`
- new module `stdlib_distribution_uniform`
[#272](https://github.com/fortran-lang/stdlib/pull/272)
- new module `stdlib_selection`
Expand Down
1 change: 1 addition & 0 deletions doc/specs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This is and index/directory of the specifications (specs) for each new module/fe

## Experimental Features & Modules

- [array](./stdlib_array.html) - Procedures for index manipulation and array handling
- [ascii](./stdlib_ascii.html) - Procedures for handling ASCII characters
- [bitsets](./stdlib_bitsets.html) - Bitset data types and procedures
- [error](./stdlib_error.html) - Catching and handling errors
Expand Down
89 changes: 89 additions & 0 deletions doc/specs/stdlib_array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
title: array
---

# The `stdlib_array` module

[TOC]

## Introduction

Module for index manipulation and array handling tasks.

## Procedures and methods provided


### `trueloc`

#### Status

Experimental

#### Description
awvwgk marked this conversation as resolved.
Show resolved Hide resolved

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

#### Return value

Returns an array of default integer size, with a maximum length of `size(array)` elements.

#### 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

#### Return value

Returns an array of default integer size, with a maximum length of `size(array)` elements.

#### 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
```
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ list(
fypp_f90("${fyppFlags}" "${fppFiles}" outFiles)

set(SRC
stdlib_array.f90
stdlib_error.f90
stdlib_logger.f90
stdlib_system.F90
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.manual
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ SRCFYPP = \
stdlib_version.fypp

SRC = f18estop.f90 \
stdlib_array.f90 \
stdlib_error.f90 \
stdlib_specialfunctions.f90 \
stdlib_specialfunctions_legendre.f90 \
Expand Down
60 changes: 60 additions & 0 deletions src/stdlib_array.f90
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
1 change: 1 addition & 0 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ list(
"-I${PROJECT_SOURCE_DIR}/src"
)

add_subdirectory(array)
add_subdirectory(ascii)
add_subdirectory(bitsets)
add_subdirectory(io)
Expand Down
1 change: 1 addition & 0 deletions src/tests/Makefile.manual
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ testdrive.F90:
$(FETCH) https://github.com/fortran-lang/test-drive/raw/v0.4.0/src/testdrive.F90 > $@

all test clean::
$(MAKE) -f Makefile.manual --directory=array $@
$(MAKE) -f Makefile.manual --directory=ascii $@
$(MAKE) -f Makefile.manual --directory=bitsets $@
$(MAKE) -f Makefile.manual --directory=io $@
Expand Down
1 change: 1 addition & 0 deletions src/tests/array/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ADDTEST(logicalloc)
4 changes: 4 additions & 0 deletions src/tests/array/Makefile.manual
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PROGS_SRC = test_logicalloc.f90


include ../Makefile.manual.test.mk
Loading