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

[stdlib_math] Add arange function. #480

Merged
merged 5 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
58 changes: 58 additions & 0 deletions doc/specs/stdlib_math.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,61 @@ program demo_logspace_rstart_cbase

end program demo_logspace_rstart_cbase
```
## `seq` - Creates an vector of `integer/real` type with evenly spaced values within a given interval.

### Status

Experimental

### Class

Pure function.

### Description

Creates an vector of `integer/real` type with evenly spaced values within a given interval.

### Syntax

`result = [[stdlib_math(module):seq(interface)]](start [, end, by])`

### Arguments

`start`: Shall be an `integer/real` scalar.
This is an `intent(in)` argument.

`end`: Shall be an `integer/real` scalar.
This is an `intent(in)` and `optional` argument.

`by`: Shall be an `integer/real` scalar and large than `0`.
This is an `intent(in)` and `optional` argument.

zoziha marked this conversation as resolved.
Show resolved Hide resolved
Warning:
If `by = 0`, the `by` argument will be corrected to `1/1.0` by the internal process of the `seq` function.
If `by < 0`, the `by` argument will be corrected to `abs(by)` by the internal process of the `seq` function.
zoziha marked this conversation as resolved.
Show resolved Hide resolved

### Return value

Vector of evenly spaced values.

For floating point arguments, the length of the result is `floor((end - start)/by) + 1`.

### Example

```fortran
program demo_math_seq
use stdlib_math, only: seq

print *, seq(3) !! [1,2,3]
print *, seq(3.0) !! [1.0,2.0,3.0]
print *, seq(-1) !! [1,0,-1]
print *, seq(0,2) !! [0,1,2]
print *, seq(1,-1) !! [1,0,-1]
print *, seq(0, 2, 2) !! [0,2]
print *, (1.0,1.0)*seq(3) !! [(1.0,1.0),(2.0,2.0),[3.0,3.0]]

print *, seq(0.0,2.0,-2.0) !! [0.0,2.0]. Not recommended: `by` argument is negative!
print *, seq(0.0,2.0,0.0) !! [0.0,1.0]. Not recommended: `by` argument is zero!

end program demo_math_seq
```
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(fppFiles
stdlib_math.fypp
stdlib_math_linspace.fypp
stdlib_math_logspace.fypp
stdlib_math_seq.fypp
stdlib_string_type.fypp
)

Expand Down
4 changes: 4 additions & 0 deletions src/Makefile.manual
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SRCFYPP =\
stdlib_linalg.fypp \
stdlib_linalg_diag.fypp \
stdlib_linalg_outer_product.fypp \
stdlib_math_seq.fypp \
stdlib_optval.fypp \
stdlib_quadrature.fypp \
stdlib_quadrature_trapz.fypp \
Expand Down Expand Up @@ -154,4 +155,7 @@ stdlib_math_linspace.o: \
stdlib_math.o
stdlib_math_logspace.o: \
stdlib_math_linspace.o
stdlib_math_seq.o: \
stdlib_math.o \
stdlib_kinds.o
stdlib_linalg_outer_product.o: stdlib_linalg.o
16 changes: 16 additions & 0 deletions src/stdlib_math.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module stdlib_math
public :: clip, linspace, logspace
public :: EULERS_NUMBER_SP, EULERS_NUMBER_DP, EULERS_NUMBER_QP
public :: DEFAULT_LINSPACE_LENGTH, DEFAULT_LOGSPACE_BASE, DEFAULT_LOGSPACE_LENGTH
public :: seq

integer, parameter :: DEFAULT_LINSPACE_LENGTH = 100
integer, parameter :: DEFAULT_LOGSPACE_LENGTH = 50
Expand Down Expand Up @@ -261,6 +262,21 @@ module stdlib_math

end interface

!> Version: experimental
!>
!> `seq` creates an vector of `integer/real` type
!> with evenly spaced values within a given interval.
interface seq
#:set RI_KINDS_TYPES = REAL_KINDS_TYPES + INT_KINDS_TYPES
#:for k1, t1 in RI_KINDS_TYPES
pure module function seq_${t1[0]}$_${k1}$(start, end, by) result(result)
${t1}$, intent(in) :: start
${t1}$, intent(in), optional :: end, by
${t1}$, allocatable :: result(:)
end function seq_${t1[0]}$_${k1}$
#:endfor
end interface seq

contains

#:for k1, t1 in IR_KINDS_TYPES
Expand Down
58 changes: 58 additions & 0 deletions src/stdlib_math_seq.fypp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#:include "common.fypp"
submodule(stdlib_math) stdlib_math_seq

implicit none

contains

#:for k1, t1 in REAL_KINDS_TYPES
!> `seq` creates an vector of `${t1}$` type
!> with evenly spaced values within a given interval.
pure module function seq_${t1[0]}$_${k1}$(start, end, by) result(result)

${t1}$, intent(in) :: start
${t1}$, intent(in), optional :: end, by
${t1}$, allocatable :: result(:)

${t1}$ :: start_, end_, by_
integer :: i

start_ = merge(start, 1.0_${k1}$, present(end))
end_ = merge(end, start, present(end))
by_ = sign(merge(merge(by, 1.0_${k1}$, by /= 0.0_${k1}$), &
1.0_${k1}$, present(by)), end_ - start_)

allocate(result(floor((end_ - start_)/by_) + 1))
#!TODO: ??
#! floor or ceiling, floor is better.
zoziha marked this conversation as resolved.
Show resolved Hide resolved

result = [(start_ + (i - 1)*by_, i=1, size(result), 1)]

end function seq_${t1[0]}$_${k1}$
#:endfor

#:for k1, t1 in INT_KINDS_TYPES
!> `seq` creates an vector of `${t1}$` type
!> with evenly spaced values within a given interval.
pure module function seq_${t1[0]}$_${k1}$(start, end, by) result(result)

${t1}$, intent(in) :: start
${t1}$, intent(in), optional :: end, by
${t1}$, allocatable :: result(:)

${t1}$ :: start_, end_, by_
${t1}$ :: i

start_ = merge(start, 1_${k1}$, present(end))
end_ = merge(end, start, present(end))
by_ = sign(merge(merge(by, 1_${k1}$, by /= 0_${k1}$), &
1_${k1}$, present(by) ), end_ - start_)

allocate(result((end_ - start_)/by_ + 1))

result = [(i, i=start_, end_, by_)]

end function seq_${t1[0]}$_${k1}$
#:endfor

end submodule stdlib_math_seq
3 changes: 2 additions & 1 deletion src/tests/math/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ADDTEST(stdlib_math)
ADDTEST(linspace)
ADDTEST(logspace)
ADDTEST(logspace)
ADDTEST(math_seq)
3 changes: 2 additions & 1 deletion src/tests/math/Makefile.manual
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PROGS_SRC = test_stdlib_math.f90 test_linspace.f90 test_logspace.f90
PROGS_SRC = test_stdlib_math.f90 test_linspace.f90 test_logspace.f90 \
test_math_seq.f90


include ../Makefile.manual.test.mk
53 changes: 53 additions & 0 deletions src/tests/math/test_math_seq.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
!> SPDX-Identifier: MIT
module test_math_seq

use stdlib_error, only: check
use stdlib_math, only: seq
implicit none

logical, private :: warn = .false.

contains

subroutine test_math_seq_real
!> Normal
call check(all(seq(3.0) == [1.0, 2.0, 3.0]), msg="all(seq(3.0) == [1.0,2.0,3.0]) failed.", warn=warn)
call check(all(seq(-1.0) == [1.0, 0.0, -1.0]), msg="all(seq(-1.0) == [1.0,0.0,-1.0]) failed.", warn=warn)
call check(all(seq(0.0, 2.0) == [0.0, 1.0, 2.0]), msg="all(seq(0.0,2.0) == [0.0,1.0,2.0]) failed.", warn=warn)
call check(all(seq(1.0, -1.0) == [1.0, 0.0, -1.0]), msg="all(seq(1.0,-1.0) == [1.0,0.0,-1.0]) failed.", warn=warn)
call check(all(seq(1.0, 1.0) == [1.0]), msg="all(seq(1.0,1.0) == [1.0]) failed.", warn=warn)
call check(all(seq(0.0, 2.0, 2.0) == [0.0, 2.0]), msg="all(seq(0.0,2.0,2.0) == [0.0,2.0]) failed.", warn=warn)
call check(all(seq(1.0, -1.0, 2.0) == [1.0, -1.0]), msg="all(seq(1.0,-1.0,2.0) == [1.0,-1.0]) failed.", warn=warn)
!> Not recommended
call check(all(seq(0.0, 2.0, -2.0) == [0.0, 2.0]), msg="all(seq(0.0,2.0,-2.0) == [0.0,2.0]) failed.", warn=warn)
call check(all(seq(1.0, -1.0, -2.0) == [1.0, -1.0]),msg="all(seq(1.0,-1.0,-2.0) == [1.0,-1.0]) failed.", warn=warn)
call check(all(seq(0.0, 2.0, 0.0) == [0.0,1.0,2.0]),msg="all(seq(0.0, 2.0, 0.0) == [0.0,1.0,2.0]) failed.", warn=warn)
end subroutine test_math_seq_real

subroutine test_math_seq_integer
!> Normal
call check(all(seq(3) == [1, 2, 3]), msg="all(seq(3) == [1,2,3]) failed.", warn=warn)
call check(all(seq(-1) == [1, 0, -1]), msg="all(seq(-1) == [1,0,-1]) failed.", warn=warn)
call check(all(seq(0, 2) == [0, 1, 2]), msg="all(seq(0,2) == [0,1,2]) failed.", warn=warn)
call check(all(seq(1, -1) == [1, 0, -1]), msg="all(seq(1,-1) == [1,0,-1]) failed.", warn=warn)
call check(all(seq(1, 1) == [1]), msg="all(seq(1,1) == [1]) failed.", warn=warn)
call check(all(seq(0, 2, 2) == [0, 2]), msg="all(seq(0,2,2) == [0,2]) failed.", warn=warn)
call check(all(seq(1, -1, 2) == [1, -1]), msg="all(seq(1,-1,2) == [1,-1]) failed.", warn=warn)
!> Not recommended
call check(all(seq(0, 2, -2) == [0, 2]), msg="all(seq(0,2,-2) == [0,2]) failed.", warn=warn)
call check(all(seq(1, -1, -2) == [1, -1]), msg="all(seq(1,-1,-2) == [1,-1]) failed.", warn=warn)
call check(all(seq(0, 2, 0) == [0,1,2]), msg="all(seq(0, 2, 0) == [0,1,2]) failed.", warn=warn)
end subroutine test_math_seq_integer

end module test_math_seq

program tester

use test_math_seq

call test_math_seq_real
call test_math_seq_integer

print *, "All tests in `test_math_seq` passed."

end program tester