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 3 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
72 changes: 72 additions & 0 deletions doc/specs/stdlib_math.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,75 @@ program demo_logspace_rstart_cbase

end program demo_logspace_rstart_cbase
```
## `arange`

### Status

Experimental

### Class

Pure function.

### Description

Create a vector of the `integer/real` type with given fixed spaced values within a given interval.
zoziha marked this conversation as resolved.
Show resolved Hide resolved

#### Note

Because of the `i` (`huge(integer :: i) = 2147483647`) index inside the `arange` function , the dimensional maximum length of array created by the `arange` function is `2147483647`.
zoziha marked this conversation as resolved.
Show resolved Hide resolved

### Syntax

`result = [[stdlib_math(module):arange(interface)]](start [, end, by])`
zoziha marked this conversation as resolved.
Show resolved Hide resolved

### Arguments

All arguments should be the same type and kind.

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

`end`: Shall be an `integer/real` scalar.
This is an `intent(in)` and `optional` argument.
The default `end` value is the inputted `start` value.

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

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 `arange` function.
If `by < 0`, the `by` argument will be corrected to `abs(by)` by the internal process of the `arange` function.

### Return value

Return a vector of fixed spaced spaced values.
zoziha marked this conversation as resolved.
Show resolved Hide resolved

For `integer` type arguments, the length of the result vector is `(end - start)/by + 1`.
For `real` type arguments, the length of the result vector is `floor((end - start)/by) + 1`.

### Example

```fortran
program demo_math_arange
use stdlib_math, only: arange

print *, arange(3) !! [1,2,3]
print *, arange(-1) !! [1,0,-1]
print *, arange(0,2) !! [0,1,2]
print *, arange(1,-1) !! [1,0,-1]
print *, arange(0, 2, 2) !! [0,2]

print *, arange(3.0) !! [1.0,2.0,3.0]
print *, arange(0.0,5.0) !! [0.0,1.0,2.0,3.0,4.0,5.0]
print *, arange(0.0,6.0,2.5) !! [0.0,2.5,5.0]

print *, (1.0,1.0)*arange(3) !! [(1.0,1.0),(2.0,2.0),[3.0,3.0]]

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

end program demo_math_arange
```
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_arange.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_arange.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_arange.o: \
stdlib_math.o \
stdlib_kinds.o
stdlib_linalg_outer_product.o: stdlib_linalg.o
17 changes: 17 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 :: arange

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

end interface

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

contains

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

contains

#:for k1, t1 in REAL_KINDS_TYPES
!> `arange` creates a vector of the `${t1}$` type
!> with evenly spaced values within a given interval.
pure module function arange_${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_)
zoziha marked this conversation as resolved.
Show resolved Hide resolved

allocate(result(floor((end_ - start_)/by_) + 1))
zoziha marked this conversation as resolved.
Show resolved Hide resolved

result = [(start_ + (i - 1)*by_, i=1, size(result), 1)]
zoziha marked this conversation as resolved.
Show resolved Hide resolved

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

#:for k1, t1 in INT_KINDS_TYPES
!> `arange` creates a vector of the `${t1}$` type
!> with evenly spaced values within a given interval.
pure module function arange_${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_)
zoziha marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

end submodule stdlib_math_arange
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_arange)
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_arange.f90


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

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

logical, private :: warn = .false.

contains

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

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

end module test_math_arange

program tester

use test_math_arange

call test_math_arange_real
call test_math_arange_integer

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

end program tester