Skip to content
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
Prev Previous commit
Minor update of diff doc.
  • Loading branch information
zoziha committed Jan 22, 2022
commit cc2344e3f724d4fd7dbe424a0048d8161a68fc37
59 changes: 30 additions & 29 deletions doc/specs/stdlib_math.md
Original file line number Diff line number Diff line change
Expand Up @@ -658,11 +658,11 @@ Computes differences between adjacent elements of an array.

#### Syntax

For rank-1 array
For a rank-1 array
```fortran
y = [[stdlib_math(module):diff(interface)]](x [, n, prepend, append])
```
and for rank-2 array
and for a rank-2 array
```fortran
y = [[stdlib_math(module):diff(interface)]](x [, n, dim, prepend, append])
```
Expand All @@ -677,35 +677,36 @@ Pure function.

#### Arguments

Note: The `x`, `prepend` and `append` arguments must have the same `type`, `kind` and `rank`.

`x`: Shall be a `real/integer` and `rank-1/rank-2` array.
`x`: The array to take a difference of.
Shall be a `real/integer` and `rank-1/rank-2` array.
This argument is `intent(in)`.

`n`: Shall be an `integer` scalar.
This argument is `intent(in)` and `optional`, which is `1` by default.
It represents to calculate the n-th order difference.

`dim`: Shall be an `integer` scalar.
This argument is `intent(in)` and `optional`, which is `1` by default.
It gives the dimension of the input array along which the difference is calculated, between `1` and `rank(x)`.
`n`: How many times to iteratively calculate the difference.
Shall be an `integer` scalar.
This argument is `intent(in)` and `optional`, and has value of `1` by default.

`prepend`: Shall be a `real/integer` and `rank-1/rank-2` array, which is no value by default.
This argument is `intent(in)` and `optional`.
`dim`: The dimension of the input array along which to calculate the difference.
Its value must be between `1` and `rank(x)`.
Shall be an `integer` scalar.
This argument is `intent(in)` and `optional` and has a value of `1` by default.

`append`: Shall be a `real/integer` and `rank-1/rank-2` array, which is no value by default.
This argument is `intent(in)` and `optional`.
`prepend`, `append`: Arrays to prepend or append to a along axis prior to performing the difference.
The dimension and shape must match a except along axis.
Shall be a `real/integer` and `rank-1/rank-2` array.
This argument is `intent(in)` and `optional`, which is no value by default.

Note:
- If the value of `n` is less than or equal to `0`, the return value of `y` is `x`. (Not recommended)
- If the value of `dim` is not equal to `1` or `2`,
`1` will be used by the internal process of `diff`. (Not recommended)
- The `x`, `prepend` and `append` arguments must have the same `type`, `kind` and `rank`.
- If the value of `n` is less than or equal to `0` (which is not recommended), the return value of `diff` is `x`.
- If the value of `dim` is not equal to `1` or `2` (which is not recommended),
`1` will be used by the internal process of `diff`.

#### Result value

Note: That `y` generally has one fewer element than `x`.
#### Result value

Returns a `real/integer` and `rank-1/rank-2` array.
Returns the finite difference of the input array.
Shall be a `real/integer` and `rank-1/rank-2` array.
When both `prepend` and `append` are not present, the result `y` has one fewer element than `x` alongside the dimension `dim`.

#### Example

Expand All @@ -720,16 +721,16 @@ program demo_diff
integer :: A(3, 3) = reshape([1, 7, 17, 3, 11, 19, 5, 13, 23], [3, 3])
integer :: Y(3, 2)

print *, diff(i) !! [0, 1, 1, 2, 3, 5]
print *, diff(x, 2) !! [5.0, 5.0, 5.0, 5.0]
print *, diff(i) ! [0, 1, 1, 2, 3, 5]
print *, diff(x, 2) ! [5.0, 5.0, 5.0, 5.0]

Y = diff(A, n=1, dim=2)
print *, Y(1, :) !! [2, 2]
print *, Y(2, :) !! [4, 2]
print *, Y(3, :) !! [2, 4]
print *, Y(1, :) ! [2, 2]
print *, Y(2, :) ! [4, 2]
print *, Y(3, :) ! [2, 4]

print *, diff(i, prepend=[0]) !! [1, 0, 1, 1, 2, 3, 5]
print *, diff(i, append=[21]) !! [0, 1, 1, 2, 3, 5, 8]
print *, diff(i, prepend=[0]) ! [1, 0, 1, 1, 2, 3, 5]
print *, diff(i, append=[21]) ! [0, 1, 1, 2, 3, 5, 8]

end program demo_diff
```
6 changes: 6 additions & 0 deletions src/tests/math/test_stdlib_math.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,12 @@ contains
"diff(<rank-2>, n=1, dim=2) in test_diff_int_${k1}$ failed")
if (allocated(error)) return

call check(error, all(diff(A, n=1, dim=2, prepend=reshape([${t1}$ :: 1], [1, 1]), &
append=reshape([${t1}$ :: 2], [1, 1])) == reshape([${t1}$ :: 0, 2, 2, -3], [1, 4])), &
"diff(<rank-2>, n=1, dim=2, prepend=reshape([${t1}$ :: 1], [1, 1]), &
&append=reshape([${t1}$ :: 2], [1, 1])) in test_diff_int_${k1}$ failed")
if (allocated(error)) return

!> size(B, dim) <= n
call check(error, size(diff(B, 2)), 0, "size(diff(B, 2)) in test_diff_int_${k1}$ failed")
if (allocated(error)) return
Expand Down