Skip to content

Commit

Permalink
Merge pull request #1 from zoziha/function/diff
Browse files Browse the repository at this point in the history
Add `diff` function
  • Loading branch information
zoziha authored Jul 27, 2021
2 parents 788da98 + 15836bc commit 056ebc6
Show file tree
Hide file tree
Showing 29 changed files with 667 additions and 260 deletions.
62 changes: 62 additions & 0 deletions doc/specs/forlab_linalg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
title: LINALG
---

# LINALG

[TOC]

## `diff` - diff computes differences of arrays.

### Status

Experimental

### Description
y = diff(x) returns differences between adjacent elements of vector x.
y = diff(x, n) returns the nth difference by applying the diff(x) operator recursively n times.
B = diff(A) returns differences between adjacent elements of array A along the first dimension.
B = diff(A, n) returns the nth difference by applying the diff(A) operator recursively n times.
B = diff(A, dim) returns differences between adjacent elements of array A along the dimension given by dim.
B = diff(A, n, dim) returns the nth difference along the dimension given by dim by applying the diff(A, dim) operator recursively n times.


### Syntax

For vector:
`result = [[forlab_linalg(module):diff(interface)]](x [, n])`

For matrix:
`result = [[forlab_linalg(module):diff(interface)]](A [, n, dim])`

### Arguments

`x`: Shall be a `real` type of verctor.
`A`: Shall be a `real` type of matrix.

`n` (optional): Shall be a `integer` type.
`dim` (optional): Shall be a `integer` type.


### Return value

Return differences between adjacent elements of vector `x` or matrix `A`.

### Example

```fortran
program test_linalg_diff
use forlab_linalg, only: diff
use forlab_linalg, only: linspace
use forlab_io, only: disp
implicit none
real :: x(10)
call linspace(x, 0.0, 9.0)
call disp(x, "Linspace(x) : ")
call disp(diff(x), "Test_linalg_diff : ")
end program test_linalg_diff
```
11 changes: 11 additions & 0 deletions doc/specs/forlab_math.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: MATH
---

# MATH

[TOC]

## `randu`

## `randn`
15 changes: 6 additions & 9 deletions doc/specs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ This is and index/directory of the specifications (specs) for each new module/fe

- [IO](./forlab_io.html) - Input/output helper & convenience
- [math](./forlab_math.html) - Math functions
- [linalg](./stdlib_linalg.html) - Linear Algebra
- [logger](./stdlib_logger.html) - Runtime logging system
- [optval](./stdlib_optval.html) - Fallback value for optional arguments
- [quadrature](./stdlib_quadrature.html) - Numerical integration
- [sorting](./stdlib_sorting.html) - Sorting of rank one arrays
- [stats](./stdlib_stats.html) - Descriptive Statistics
- [stats_distribution_PRNG](./stdlib_stats_distribution_PRNG.html) - Probability Distributions random number generator
- [string\_type](./stdlib_string_type.html) - Basic string support
- [strings](./stdlib_strings.html) - String handling and manipulation routines
- [linalg](./forlab_linalg.html) - Linear Algebra
- [logger](./forlab_logger.html) - Runtime logging system
- [quadrature](./forlab_quadrature.html) - Numerical integration
- [sorting](./forlab_sorting.html) - Sorting of rank one arrays
- [stats](./forlab_stats.html) - Descriptive Statistics
- [strings](./forlab_strings.html) - String handling and manipulation routines

## Released/Stable Features & Modules

Expand Down
34 changes: 19 additions & 15 deletions fpm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,73 +26,77 @@ source-dir="src"
## [io] tests
[[test]]
name = "io_bin"
source-dir = "tests/io"
source-dir = "test/io"
main = "test_io_bin.f90"
[[test]]
name = "io_disp"
source-dir = "tests/io"
source-dir = "test/io"
main = "test_io_disp.f90"
[[test]]
name = "io_file"
source-dir = "tests/io"
source-dir = "test/io"
main = "test_io_file.f90"
[[test]]
name = "io_set_color"
source-dir = "tests/io"
source-dir = "test/io"
main = "test_io_set_color.f90"

## [linalg] tests
[[test]]
name = "linalg_diff"
source-dir = "test/linalg"
main = "test_linalg_diff.f90"
[[test]]
name = "linalg_i"
source-dir = "tests/linalg"
source-dir = "test/linalg"
main = "test_linalg_i.f90"
[[test]]
name = "linalg_linspace"
source-dir = "tests/linalg"
source-dir = "test/linalg"
main = "test_linalg_linspace.f90"
[[test]]
name = "linalg_tri"
source-dir = "tests/linalg"
source-dir = "test/linalg"
main = "test_linalg_tri.f90"
[[test]]
name = "linalg_x"
source-dir = "tests/linalg"
source-dir = "test/linalg"
main = "test_linalg_x.f90"

## [math] tests
[[test]]
name = "math_angle"
source-dir = "tests/math"
source-dir = "test/math"
main = "test_math_angle.f90"
[[test]]
name = "math_degcir"
source-dir = "tests/math"
source-dir = "test/math"
main = "test_math_degcir.f90"

## [stats] tests
[[test]]
name = "stats_rand"
source-dir = "tests/stats"
source-dir = "test/stats"
main = "test_stats_rand.f90"
[[test]]
name = "stats_var"
source-dir = "tests/stats"
source-dir = "test/stats"
main = "test_stats_var.f90"

## [strings] tests
[[test]]
name = "strings_format_string"
source-dir = "tests/strings"
source-dir = "test/strings"
main = "test_strings_format_string.f90"

## [sorting] tests
[[test]]
name = "sorting_sort"
source-dir = "tests/sorting"
source-dir = "test/sorting"
main = "test_sorting_sort.f90"

## [time] tests
[[test]]
name = "time_tioc"
source-dir = "tests/time"
source-dir = "test/time"
main = "test_time_tioc.f90"
69 changes: 1 addition & 68 deletions meta-src/forlab_.f95
Original file line number Diff line number Diff line change
Expand Up @@ -2154,76 +2154,9 @@ subroutine deg2utm1(lat, lon, east, north, zn, zl)
return
end subroutine deg2utm1

! diff
!-----------------------------------------------------------------------
! diff computes differences of arrays
!
! Syntax
!-----------------------------------------------------------------------
! y = diff(x)
! y = diff(x, n)
! B = diff(A)
! B = diff(A, n)
! B = diff(A, dim)
! B = diff(A, n, dim)
!
! Description
!-----------------------------------------------------------------------
! y = diff(x) returns differences between adjacent elements of vector x.
!
! y = diff(x, n) returns the nth difference by applying the diff(x)
! operator recursively n times.
!
! B = diff(A) returns differences between adjacent elements of array A
! along the first dimension.
!
! B = diff(A, n) returns the nth difference by applying the diff(A)
! operator recursively n times.
!
! B = diff(A, dim) returns differences between adjacent elements of
! array A along the dimension given by dim.
!
! B = diff(A, n, dim) returns the nth difference along the dimension
! given by dim by applying the diff(A, dim) operator recursively
! n times.

function diff1(x, n)
real(kind=RPRE), dimension(:), allocatable :: diff1
real(kind=RPRE), dimension(:), intent(in) :: x
integer(kind=IPRE), intent(in), optional :: n
integer(kind=IPRE) :: opt_n, i

opt_n = 1
if (present(n)) opt_n = n

diff1 = x
do i = 1, opt_n
diff1 = diff1(2:) - diff1(:size(diff1) - 1)
end do
return
end function diff1

function diff2(A, n, dim)
real(kind=RPRE), dimension(:, :), allocatable :: diff2
real(kind=RPRE), dimension(:, :), intent(in) :: A
integer(kind=IPRE), intent(in), optional :: n, dim
integer(kind=IPRE) :: opt_n, i

opt_n = 1
if (present(n)) opt_n = n

diff2 = A
if ((.not. present(dim)) .or. (dim == 1)) then
do i = 1, opt_n
diff2 = diff2(2:, :) - diff2(:size(diff2, 1) - 1, :)
end do
elseif (dim == 2) then
do i = 1, opt_n
diff2 = diff2(:, 2:) - diff2(:, :size(diff2, 2) - 1)
end do
end if
return
end function diff2


! find
!-----------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 056ebc6

Please sign in to comment.