It provides a lot of subroutines to manipulate vectors and implement vector operations.

Type

subroutines

Source

src/s_vector.f90

Naming Convention

Note
  • _i means integer version
  • _d means real(dp) version
  • _z means complex(dp) version

Usage

Mesh Generation

subroutine s_linspace_d(xmin, xmax, n, x)
subroutine s_linspace_z(xmin, xmax, n, x)

Purpose:

Create a linearly spaced vector with n points in the interval [xmin, xmax].

Arguments:

ArgumentTypeIntentDescription
xminreal(dp)/complex(dp)inStarting value of the interval
xmaxreal(dp)/complex(dp)inEnding value of the interval
nintegerinNumber of points
xreal(dp)/complex(dp)outOutput linearly spaced vector

Cumulative Operations

subroutine s_cumsum_i(n, v, vsum)
subroutine s_cumsum_d(n, v, vsum)
subroutine s_cumsum_z(n, v, vsum)

Purpose:

Calculate the cumulative sum of a vector.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
vinteger/real(dp)/complex(dp)inInput vector
vsuminteger/real(dp)/complex(dp)outCumulative sum vector

subroutine s_cumprod_i(n, v, vprod)
subroutine s_cumprod_d(n, v, vprod)
subroutine s_cumprod_z(n, v, vprod)

Purpose:

Calculate the cumulative product of a vector.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
vinteger/real(dp)/complex(dp)inInput vector
vprodinteger/real(dp)/complex(dp)outCumulative product vector

Mixing and Add Operations

subroutine s_mix_d(n, dx, dy, alpha)
subroutine s_mix_z(n, zx, zy, alpha)

Purpose:

Perform linear mixing of two vectors: y = (1 - alpha) * x + alpha * y.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vectors
dx/zxreal(dp)/complex(dp)inFirst vector
dy/zyreal(dp)/complex(dp)inoutSecond vector; on exit, contains the mixed result
alphareal(dp)inMixing parameter (0 ≤ α ≤ 1)

subroutine s_vecadd_i(n, ix, iy, alpha)
subroutine s_vecadd_d(n, dx, dy, alpha)
subroutine s_vecadd_z(n, zx, zy, alpha)

Purpose:

Add diagonal elements of a matrix to a vector: x = x + alpha * diag(y).

Arguments:

ArgumentTypeIntentDescription
nintegerinDimension of vector and matrix (n-by-n)
ix/dx/zxinteger/real(dp)/complex(dp)inoutVector; on exit, contains x + alpha * diag(y)
iy/dy/zyinteger/real(dp)/complex(dp)inn-by-n matrix
alphareal(dp)inPrefactor for the diagonal elements

Vector Products

subroutine s_dot_i(n, ix, iy, val)
subroutine s_dot_d(n, dx, dy, val)
subroutine s_dot_z(n, zx, zy, val)

Purpose:

Calculate the dot product (inner product) of two vectors: val = x · y.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vectors
ix/dx/zxinteger/real(dp)/complex(dp)inFirst vector
iy/dy/zyinteger/real(dp)/complex(dp)inSecond vector
valinteger/real(dp)/complex(dp)outDot product result

subroutine s_outer_i(n, m, ix, iy, ia)
subroutine s_outer_d(n, m, dx, dy, da)
subroutine s_outer_z(n, m, zx, zy, za)

Purpose:

Calculate the outer product of two vectors: A = x ⊗ y (A[i,j] = x[i] * y[j]).

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector x
mintegerinSize of vector y
ix/dx/zxinteger/real(dp)/complex(dp)inFirst vector (n elements)
iy/dy/zyinteger/real(dp)/complex(dp)inSecond vector (m elements)
ia/da/zainteger/real(dp)/complex(dp)outOuter product matrix (n-by-m)

subroutine s_cross_i(ix, iy, iz)
subroutine s_cross_d(dx, dy, dz)
subroutine s_cross_z(zx, zy, zz)

Purpose:

Calculate the cross product of two 3D vectors: z = x × y.

Arguments:

ArgumentTypeIntentDescription
ix/dx/zxinteger/real(dp)/complex(dp)inFirst 3D vector
iy/dy/zyinteger/real(dp)/complex(dp)inSecond 3D vector
iz/dz/zzinteger/real(dp)/complex(dp)outCross product result (3D vector)

Set Operations

subroutine s_diff_i(n, iv, diff)
subroutine s_diff_d(n, dv, diff)
subroutine s_diff_z(n, zv, diff)

Purpose:

Calculate the differences between consecutive elements: diff[i] = v[i+1] - v[i].

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of input vector
iv/dv/zvinteger/real(dp)/complex(dp)inInput vector
diffinteger/real(dp)/complex(dp)outDifference vector (size n-1)

subroutine s_unique_i(n, ix, m, iy)
subroutine s_unique_d(n, dx, m, dy)
subroutine s_unique_z(n, zx, m, zy)

Purpose:

Extract unique elements from a vector, preserving original order.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of input vector
ix/dx/zxinteger/real(dp)/complex(dp)inInput vector
mintegeroutNumber of unique elements
iy/dy/zyinteger/real(dp)/complex(dp)outUnique elements (preserving order, size n but only first m valid)

subroutine s_intersect_i(n, ix, m, iy, k, iz)
subroutine s_intersect_d(n, dx, m, dy, k, dz)
subroutine s_intersect_z(n, zx, m, zy, k, zz)

Purpose:

Find the intersection of two vectors (elements common to both).

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of first vector
ix/dx/zxinteger/real(dp)/complex(dp)inFirst input vector
mintegerinSize of second vector
iy/dy/zyinteger/real(dp)/complex(dp)inSecond input vector
kintegeroutNumber of elements in intersection
iz/dz/zzinteger/real(dp)/complex(dp)outIntersection elements (unique)

subroutine s_union_i(n, ix, m, iy, k, iz)
subroutine s_union_d(n, dx, m, dy, k, dz)
subroutine s_union_z(n, zx, m, zy, k, zz)

Purpose:

Find the union of two vectors (all unique elements from both).

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of first vector
ix/dx/zxinteger/real(dp)/complex(dp)inFirst input vector
mintegerinSize of second vector
iy/dy/zyinteger/real(dp)/complex(dp)inSecond input vector
kintegeroutNumber of elements in union
iz/dz/zzinteger/real(dp)/complex(dp)outUnion elements (unique, size n+m)

Statistics

subroutine s_stats_i(n, iv, mean, stddev)
subroutine s_stats_d(n, dv, mean, stddev)
subroutine s_stats_z(n, zv, mean, stddev)

Purpose:

Calculate the mean and standard deviation of a vector.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
iv/dv/zvinteger/real(dp)/complex(dp)inInput vector
meanreal(dp)/complex(dp)outMean value
stddevreal(dp)outStandard deviation

subroutine s_moment_i(n, iv, order, moment)
subroutine s_moment_d(n, dv, order, moment)
subroutine s_moment_z(n, zv, order, moment)

Purpose:

Calculate the k-th central moment of a vector: E[(X - μ)^k].

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
iv/dv/zvinteger/real(dp)/complex(dp)inInput vector
orderintegerinOrder of the moment
momentreal(dp)outk-th central moment

subroutine s_skewness_i(n, iv, skewness)
subroutine s_skewness_d(n, dv, skewness)
subroutine s_skewness_z(n, zv, skewness)

Purpose:

Calculate the skewness (asymmetry measure) of a distribution. Positive skew indicates right tail, negative indicates left tail.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
iv/dv/zvinteger/real(dp)/complex(dp)inInput vector
skewnessreal(dp)outSkewness value

subroutine s_kurtosis_i(n, iv, kurtosis)
subroutine s_kurtosis_d(n, dv, kurtosis)
subroutine s_kurtosis_z(n, zv, kurtosis)

Purpose:

Calculate the kurtosis (tailedness measure) of a distribution. Normal distribution has kurtosis = 3; higher values indicate heavier tails.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
iv/dv/zvinteger/real(dp)/complex(dp)inInput vector
kurtosisreal(dp)outKurtosis value

Distance and Norms

subroutine s_distance_i(n, ix, iy, dist)
subroutine s_distance_d(n, dx, dy, dist)
subroutine s_distance_z(n, zx, zy, dist)

Purpose:

Calculate the Euclidean distance between two vectors: dist = ||x - y||_2.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vectors
ix/dx/zxinteger/real(dp)/complex(dp)inFirst vector
iy/dy/zyinteger/real(dp)/complex(dp)inSecond vector
distreal(dp)outEuclidean distance

subroutine s_norm1_i(n, ix, norm)
subroutine s_norm1_d(n, dx, norm)
subroutine s_norm1_z(n, zx, norm)

Purpose:

Calculate the L1 norm (Manhattan norm) of a vector: ||v||_1 = Σ|v[i]|.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
ix/dx/zxinteger/real(dp)/complex(dp)inInput vector
normreal(dp)outL1 norm value

subroutine s_norm2_i(n, ix, norm)
subroutine s_norm2_d(n, dx, norm)
subroutine s_norm2_z(n, zx, norm)

Purpose:

Calculate the L2 norm (Euclidean norm) of a vector: ||v||_2 = √(Σ|v[i]|²).

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
ix/dx/zxinteger/real(dp)/complex(dp)inInput vector
normreal(dp)outL2 norm value

subroutine s_norminf_i(n, ix, norm)
subroutine s_norminf_d(n, dx, norm)
subroutine s_norminf_z(n, zx, norm)

Purpose:

Calculate the L∞ norm (maximum norm) of a vector: ||v||_∞ = max(|v[i]|).

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
ix/dx/zxinteger/real(dp)/complex(dp)inInput vector
normreal(dp)outL∞ norm value

Mathematical Operations

subroutine s_pow_i(n, ix, power)
subroutine s_pow_d(n, dx, power)
subroutine s_pow_z(n, zx, power)

Purpose:

Raise each element of a vector to a power (in-place): x[i] = x[i]^power.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
ix/dx/zxinteger/real(dp)/complex(dp)inoutVector; on exit, contains powered elements
powerreal(dp)inExponent

subroutine s_square_i(n, ix)
subroutine s_square_d(n, dx)
subroutine s_square_z(n, zx)

Purpose:

Square each element of a vector (in-place): x[i] = x[i]².

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
ix/dx/zxinteger/real(dp)/complex(dp)inoutVector; on exit, contains squared elements

subroutine s_sqrt_d(n, dx)
subroutine s_sqrt_z(n, zx)

Purpose:

Calculate the square root of each element (in-place): x[i] = √x[i].

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
dx/zxreal(dp)/complex(dp)inoutVector; on exit, contains square roots

subroutine s_exp_d(n, dx)
subroutine s_exp_z(n, zx)

Purpose:

Calculate the exponential of each element (in-place): x[i] = exp(x[i]).

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
dx/zxreal(dp)/complex(dp)inoutVector; on exit, contains exponentials

subroutine s_log_d(n, dx)
subroutine s_log_z(n, zx)

Purpose:

Calculate the natural logarithm of each element (in-place): x[i] = ln(x[i]).

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
dx/zxreal(dp)/complex(dp)inoutVector; on exit, contains natural logarithms

subroutine s_log10_d(n, dx)
subroutine s_log10_z(n, zx)

Purpose:

Calculate the base-10 logarithm of each element (in-place): x[i] = log₁₀(x[i]).

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
dx/zxreal(dp)/complex(dp)inoutVector; on exit, contains base-10 logarithms

Trigonometric Functions

subroutine s_sin_d(n, dx)
subroutine s_sin_z(n, zx)

Purpose:

Calculate the sine of each element (in-place): x[i] = sin(x[i]).

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
dx/zxreal(dp)/complex(dp)inoutVector (radians); on exit, contains sine values

subroutine s_cos_d(n, dx)
subroutine s_cos_z(n, zx)

Purpose:

Calculate the cosine of each element (in-place): x[i] = cos(x[i]).

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
dx/zxreal(dp)/complex(dp)inoutVector (radians); on exit, contains cosine values

subroutine s_tan_d(n, dx)
subroutine s_tan_z(n, zx)

Purpose:

Calculate the tangent of each element (in-place): x[i] = tan(x[i]).

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
dx/zxreal(dp)/complex(dp)inoutVector (radians); on exit, contains tangent values

subroutine s_sinh_d(n, dx)
subroutine s_sinh_z(n, zx)

Purpose:

Calculate the hyperbolic sine of each element (in-place): x[i] = sinh(x[i]).

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
dx/zxreal(dp)/complex(dp)inoutVector; on exit, contains hyperbolic sine values

subroutine s_cosh_d(n, dx)
subroutine s_cosh_z(n, zx)

Purpose:

Calculate the hyperbolic cosine of each element (in-place): x[i] = cosh(x[i]).

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
dx/zxreal(dp)/complex(dp)inoutVector; on exit, contains hyperbolic cosine values

subroutine s_tanh_d(n, dx)
subroutine s_tanh_z(n, zx)

Purpose:

Calculate the hyperbolic tangent of each element (in-place): x[i] = tanh(x[i]).

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
dx/zxreal(dp)/complex(dp)inoutVector; on exit, contains hyperbolic tangent values

Smoothing Operations

subroutine s_moving_average_i(n, ix, window_size, iy)
subroutine s_moving_average_d(n, dx, window_size, dy)
subroutine s_moving_average_z(n, zx, window_size, zy)

Purpose:

Apply a simple moving average filter with uniform weights.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of input vector
ix/dx/zxinteger/real(dp)/complex(dp)inInput vector
window_sizeintegerinWindow size for averaging (must be odd)
iy/dy/zyinteger/real(dp)/complex(dp)outSmoothed output vector

subroutine s_smooth_box_i(n, ix, window_size, iy)
subroutine s_smooth_box_d(n, dx, window_size, dy)
subroutine s_smooth_box_z(n, zx, window_size, zy)

Purpose:

Apply a box (rectangular) smoothing filter. Similar to moving average but with boundary handling.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of input vector
ix/dx/zxinteger/real(dp)/complex(dp)inInput vector
window_sizeintegerinHalf-width of the box filter
iy/dy/zyinteger/real(dp)/complex(dp)outSmoothed output vector

subroutine s_smooth_gaussian_i(n, ix, sigma, iy)
subroutine s_smooth_gaussian_d(n, dx, sigma, dy)
subroutine s_smooth_gaussian_z(n, zx, sigma, zy)

Purpose:

Apply a Gaussian smoothing filter with specified standard deviation.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of input vector
ix/dx/zxinteger/real(dp)/complex(dp)inInput vector
sigmareal(dp)inStandard deviation of the Gaussian kernel
iy/dy/zyinteger/real(dp)/complex(dp)outSmoothed output vector

Manipulation Operations

subroutine s_swap_i(n, ix, iy)
subroutine s_swap_d(n, dx, dy)
subroutine s_swap_z(n, zx, zy)

Purpose:

Exchange the contents of two vectors.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vectors
ix/dx/zxinteger/real(dp)/complex(dp)inoutFirst vector
iy/dy/zyinteger/real(dp)/complex(dp)inoutSecond vector

subroutine s_clip_i(n, ix, vmin, vmax, iy)
subroutine s_clip_d(n, dx, vmin, vmax, dy)
subroutine s_clip_z(n, zx, vmin, vmax, zy)

Purpose:

Clip (limit) the values in a vector to a specified range.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vectors
ix/dx/zxinteger/real(dp)/complex(dp)inInput vector
vmininteger/real(dp)/complex(dp)inMinimum value
vmaxinteger/real(dp)/complex(dp)inMaximum value
iy/dy/zyinteger/real(dp)/complex(dp)outClipped output vector

subroutine s_slice_i(n, ix, start_idx, count, iy)
subroutine s_slice_d(n, dx, start_idx, count, dy)
subroutine s_slice_z(n, zx, start_idx, count, zy)

Purpose:

Extract a contiguous slice from a vector starting at a given index.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of input vector
ix/dx/zxinteger/real(dp)/complex(dp)inInput vector
start_idxintegerinStarting index (1-based)
countintegerinNumber of elements to extract
iy/dy/zyinteger/real(dp)/complex(dp)outSliced output vector

subroutine s_take_i(n, ix, m, idx, iy)
subroutine s_take_d(n, dx, m, idx, dy)
subroutine s_take_z(n, zx, m, idx, zy)

Purpose:

Extract elements from a vector at specified indices.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of input vector
ix/dx/zxinteger/real(dp)/complex(dp)inInput vector
mintegerinNumber of indices
idxintegerinArray of indices to extract (1-based)
iy/dy/zyinteger/real(dp)/complex(dp)outOutput vector with selected elements

subroutine s_drop_i(n, ix, m, idx, iy)
subroutine s_drop_d(n, dx, m, idx, dy)
subroutine s_drop_z(n, zx, m, idx, zy)

Purpose:

Remove elements from a vector at specified indices.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of input vector
ix/dx/zxinteger/real(dp)/complex(dp)inInput vector
mintegerinNumber of indices to drop
idxintegerinArray of indices to drop (1-based)
iy/dy/zyinteger/real(dp)/complex(dp)outOutput vector with elements removed

subroutine s_shuffle_i(n, ix, seed)
subroutine s_shuffle_d(n, dx, seed)
subroutine s_shuffle_z(n, zx, seed)

Purpose:

Randomly shuffle the elements of a vector in place using Fisher-Yates algorithm.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector
ix/dx/zxinteger/real(dp)/complex(dp)inoutVector to shuffle (modified in place)
seedintegerinRandom seed for reproducibility

subroutine s_reverse_i(n, ix, iy)
subroutine s_reverse_d(n, dx, dy)
subroutine s_reverse_z(n, zx, zy)

Purpose:

Reverse the order of elements in a vector.

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vectors
ix/dx/zxinteger/real(dp)/complex(dp)inInput vector
iy/dy/zyinteger/real(dp)/complex(dp)outReversed output vector

subroutine s_concat_i(n, m, ix, iy, iz)
subroutine s_concat_d(n, m, dx, dy, dz)
subroutine s_concat_z(n, m, zx, zy, zz)

Purpose:

Concatenate two vectors: z = [x, y].

Arguments:

ArgumentTypeIntentDescription
nintegerinSize of vector a
ainteger/real(dp)/complex(dp)inFirst input vector
mintegerinSize of vector b
binteger/real(dp)/complex(dp)inSecond input vector
cinteger/real(dp)/complex(dp)outConcatenated output vector (size n+m)