Skip to content

Commit cc2344e

Browse files
committed
Minor update of diff doc.
1 parent 74c5c11 commit cc2344e

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

doc/specs/stdlib_math.md

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -658,11 +658,11 @@ Computes differences between adjacent elements of an array.
658658

659659
#### Syntax
660660

661-
For rank-1 array
661+
For a rank-1 array
662662
```fortran
663663
y = [[stdlib_math(module):diff(interface)]](x [, n, prepend, append])
664664
```
665-
and for rank-2 array
665+
and for a rank-2 array
666666
```fortran
667667
y = [[stdlib_math(module):diff(interface)]](x [, n, dim, prepend, append])
668668
```
@@ -677,35 +677,36 @@ Pure function.
677677

678678
#### Arguments
679679

680-
Note: The `x`, `prepend` and `append` arguments must have the same `type`, `kind` and `rank`.
681-
682-
`x`: Shall be a `real/integer` and `rank-1/rank-2` array.
680+
`x`: The array to take a difference of.
681+
Shall be a `real/integer` and `rank-1/rank-2` array.
683682
This argument is `intent(in)`.
684683

685-
`n`: Shall be an `integer` scalar.
686-
This argument is `intent(in)` and `optional`, which is `1` by default.
687-
It represents to calculate the n-th order difference.
688-
689-
`dim`: Shall be an `integer` scalar.
690-
This argument is `intent(in)` and `optional`, which is `1` by default.
691-
It gives the dimension of the input array along which the difference is calculated, between `1` and `rank(x)`.
684+
`n`: How many times to iteratively calculate the difference.
685+
Shall be an `integer` scalar.
686+
This argument is `intent(in)` and `optional`, and has value of `1` by default.
692687

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

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

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

704-
#### Result value
705704

706-
Note: That `y` generally has one fewer element than `x`.
705+
#### Result value
707706

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

710711
#### Example
711712

@@ -720,16 +721,16 @@ program demo_diff
720721
integer :: A(3, 3) = reshape([1, 7, 17, 3, 11, 19, 5, 13, 23], [3, 3])
721722
integer :: Y(3, 2)
722723
723-
print *, diff(i) !! [0, 1, 1, 2, 3, 5]
724-
print *, diff(x, 2) !! [5.0, 5.0, 5.0, 5.0]
724+
print *, diff(i) ! [0, 1, 1, 2, 3, 5]
725+
print *, diff(x, 2) ! [5.0, 5.0, 5.0, 5.0]
725726
726727
Y = diff(A, n=1, dim=2)
727-
print *, Y(1, :) !! [2, 2]
728-
print *, Y(2, :) !! [4, 2]
729-
print *, Y(3, :) !! [2, 4]
728+
print *, Y(1, :) ! [2, 2]
729+
print *, Y(2, :) ! [4, 2]
730+
print *, Y(3, :) ! [2, 4]
730731
731-
print *, diff(i, prepend=[0]) !! [1, 0, 1, 1, 2, 3, 5]
732-
print *, diff(i, append=[21]) !! [0, 1, 1, 2, 3, 5, 8]
732+
print *, diff(i, prepend=[0]) ! [1, 0, 1, 1, 2, 3, 5]
733+
print *, diff(i, append=[21]) ! [0, 1, 1, 2, 3, 5, 8]
733734
734735
end program demo_diff
735736
```

src/tests/math/test_stdlib_math.fypp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,12 @@ contains
452452
"diff(<rank-2>, n=1, dim=2) in test_diff_int_${k1}$ failed")
453453
if (allocated(error)) return
454454

455+
call check(error, all(diff(A, n=1, dim=2, prepend=reshape([${t1}$ :: 1], [1, 1]), &
456+
append=reshape([${t1}$ :: 2], [1, 1])) == reshape([${t1}$ :: 0, 2, 2, -3], [1, 4])), &
457+
"diff(<rank-2>, n=1, dim=2, prepend=reshape([${t1}$ :: 1], [1, 1]), &
458+
&append=reshape([${t1}$ :: 2], [1, 1])) in test_diff_int_${k1}$ failed")
459+
if (allocated(error)) return
460+
455461
!> size(B, dim) <= n
456462
call check(error, size(diff(B, 2)), 0, "size(diff(B, 2)) in test_diff_int_${k1}$ failed")
457463
if (allocated(error)) return

0 commit comments

Comments
 (0)