NuMFor 9f2ab49 (2024-04-08)
Numerical (Modern) Fortran. Library for Simple Numerical computing
array_utils Module Reference

This module provides convenience routines to operate or get information on arrays. More...

Data Types

interface  save_array
 save_array Stores an 1D or 2D array to file or stdout More...
 

Functions/Subroutines

logical function, public allclose (a, b, rtol, atol)
 allclose returns True if two arrays are element-wise equal within a tolerance.
 
real(dp) function, dimension(:), allocatable, public merge_sorted (x1, x2, tolerance)
 This function creates a sorted array with values from two input sorted arrays.
 
real(dp) function, public std (x)
 std Computes the standard deviation of the array.
 
real(dp) function, public mean (x)
 mean Computes the arithmetic mean of the array.
 

Detailed Description

This module provides convenience routines to operate or get information on arrays.

Function/Subroutine Documentation

◆ allclose()

logical function, public allclose ( real(dp), dimension(:), intent(in) a,
real(dp), dimension(size(a)), intent(in) b,
real(dp), intent(in), optional rtol,
real(dp), intent(in), optional atol )

allclose returns True if two arrays are element-wise equal within a tolerance.

Very similar to Numpy allclose

The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(b)) and the absolute difference atol are added together to compare against the absolute difference between a and b.
If the following equation
abs(a - b) <= (atol + rtol * absolute(b))
is element-wise True, then allclose returns .True.

The above equation is not symmetric in a and b, so that allclose(a, b) might be different from allclose(b, a) in some rare cases.

Examples:

allclose([1e10,1e-7], [1.00001e10,1e-8])
! .False.
allclose([1e10,1e-8], [1.00001e10,1e-9])
! .True.
allclose([1e10,1e-8], [1.0001e10,1e-9])
! .False.
!
Returns
True if the two arrays are equal within the given tolerance; False otherwise.
Parameters
[in]aArray
[in]bArray
[in]rtolThe relative tolerance parameter. Default = 1.e-5
[in]atolThe absolute tolerance parameter. Default = 1.e-8

◆ mean()

real(dp) function, public mean ( real(dp), dimension(:), intent(in) x)

mean Computes the arithmetic mean of the array.

Note
the mean is basically: sum(x)/size(x)
Returns
Mean value
Parameters
[in]xInput array of real values

Referenced by histograms::histogram(), and std().

Here is the caller graph for this function:

◆ merge_sorted()

real(dp) function, dimension(:), allocatable, public merge_sorted ( real(dp), dimension(:), intent(in), target x1,
real(dp), dimension(:), intent(in), target x2,
real(dp), intent(in), optional tolerance )

This function creates a sorted array with values from two input sorted arrays.

Equal values (within tolerance) are only included once

Examples:

real(dp), dimension(:), allocatable :: a
real(dp), dimension(:), allocatable :: b
real(dp), dimension(:), allocatable :: c
integer :: j
a = [1.9999999999_dp, 2._dp, 2._dp, 3._dp]
b = [-1._dp, 2._dp, 3._dp, 4._dp]
print *, ""
print '(A)', repeat('-', 30)//' a '//repeat('-', 30)
print "(4(g0.12,1x))", a
print '(A)', repeat('-', 30)//' b '//repeat('-', 30)
print "(4(g0.12,1x))", b
c = merge_sorted(a, b)
j = 30 - int(len("merge_sorted(a,b)") / 2._dp)
print '(A)', repeat('-', 60)
print '(A)', repeat('-', j)//' merge_sorted(a,b) '//repeat('-', j)
print "(4(g0.12,1x))", c
c = merge_sorted(a, b, 1.e-6_dp)
j = 30 - int(len("merge_sorted(a,b,1.e-6)") / 2._dp)
print '(A)', repeat('-', j)//' merge_sorted(a,b,1.e-6) '//repeat('-', j)
print "(4(g0.12,1x))", c
c = merge_sorted(a, b, -1.e-6_dp)
j = 30 - int(len("merge_sorted(a,b,-1.e-6)") / 2._dp)
print '(A)', repeat('-', j)//' merge_sorted(a,b,-1.e-6) '//repeat('-', j)
print "(4(g0.12,1x))", c
!
! Outputs:
!
! ------------------------------ a ------------------------------
! 1.99999999990 2.00000000000 2.00000000000 3.00000000000
! ------------------------------ b ------------------------------
! -1.00000000000 2.00000000000 3.00000000000 4.00000000000
! ------------------------------------------------------------
! ---------------------- merge_sorted(a,b) ----------------------
! -1.00000000000 1.99999999990 2.00000000000 3.00000000000
! 4.00000000000
! ------------------- merge_sorted(a,b,1.e-6) -------------------
! -1.00000000000 1.99999999990 3.00000000000 4.00000000000
! ------------------ merge_sorted(a,b,-1.e-6) ------------------
! -1.00000000000 1.99999999990 2.00000000000 2.00000000000
! 2.00000000000 3.00000000000 3.00000000000 4.00000000000
!
Parameters
[in]x1First array
[in]x2Second array
[in]toleranceDefines the minimum value by which two numbers are considered different
Returns
Output array with values from both x1 and x2

References basic::small.

◆ std()

real(dp) function, public std ( real(dp), dimension(:), intent(in) x)

std Computes the standard deviation of the array.

Note
: Basically: sqrt(mean(x - mean(x))* alfa ) with alfa= (N/(N-1))
Returns
Standard deviation
Parameters
[in]xInput array of real values

References mean().

Referenced by histograms::histogram().

Here is the call graph for this function:
Here is the caller graph for this function: