Module elementwise

Module elementwise 

Source
Expand description

Element-wise mathematical operations with SIMD acceleration (abs, sqrt, etc.) SIMD-accelerated element-wise mathematical operations

This module provides high-performance implementations of common element-wise mathematical functions that are fundamental for scientific computing, numerical analysis, and data processing.

§Operations

§Basic Operations

  • Absolute Value (abs_simd): Computes |x| for each element
  • Sign (sign_simd): Computes sign(x) = +1, 0, or -1 for each element
  • Square Root (sqrt_simd): Computes √x for each element

§Exponential and Logarithmic

  • Exponential (exp_simd): Computes e^x for each element
  • Natural Logarithm (ln_simd): Computes ln(x) for each element

§Trigonometric Functions

  • Sine (sin_simd): Computes sin(x) for each element
  • Cosine (cos_simd): Computes cos(x) for each element
  • Tangent (tan_simd): Computes tan(x) for each element

§Hyperbolic Functions

  • Hyperbolic Sine (sinh_simd): Computes sinh(x) for each element
  • Hyperbolic Cosine (cosh_simd): Computes cosh(x) for each element
  • Hyperbolic Tangent (tanh_simd): Computes tanh(x) for each element

§Power Functions

  • Power (scalar) (powf_simd): Computes base^exp with scalar exponent
  • Power (array) (pow_simd): Computes base[i]^exp[i] element-wise

§Rounding Functions

  • Floor (floor_simd): Rounds down to largest integer <= x
  • Ceiling (ceil_simd): Rounds up to smallest integer >= x
  • Round (round_simd): Rounds to nearest integer (ties to even)
  • Fractional Part (fract_simd): Returns fractional part (x - floor(x))

§Inverse Trigonometric Functions

  • Arctangent (atan_simd): Computes atan(x) for each element
  • Arcsine (asin_simd): Computes asin(x) for each element
  • Arccosine (acos_simd): Computes acos(x) for each element
  • Two-argument arctangent (atan2_simd): Computes atan2(y, x) element-wise

§Logarithm Variants

  • Base-10 Logarithm (log10_simd): Computes log₁₀(x) for each element
  • Base-2 Logarithm (log2_simd): Computes log₂(x) for each element

§Clamping Operations

  • Clamp (clamp_simd): Constrains each element to [min, max] range

§Performance

All operations automatically use SIMD acceleration when:

  • Platform supports AVX2 (x86_64) or NEON (ARM)
  • Array size is large enough to benefit from vectorization
  • Array memory layout is contiguous

Falls back to scalar implementations for small arrays or unsupported platforms.

§Examples

use scirs2_core::ndarray::array;
use scirs2_core::ndarray_ext::elementwise::{abs_simd, sign_simd, sqrt_simd, sinh_simd, tanh_simd, floor_simd, atan2_simd, log10_simd, clamp_simd};

// Absolute value
let x = array![-3.0, -1.0, 0.0, 1.0, 3.0];
let abs_x = abs_simd(&x.view());
// Result: [3.0, 1.0, 0.0, 1.0, 3.0]

// Sign function (signum)
let signs = sign_simd(&x.view());
// Result: [-1.0, -1.0, 0.0, 1.0, 1.0]

// Square root
let y = array![1.0, 4.0, 9.0, 16.0, 25.0];
let sqrt_y = sqrt_simd(&y.view());
// Result: [1.0, 2.0, 3.0, 4.0, 5.0]

// Hyperbolic tangent (neural network activation)
let z = array![-1.0, 0.0, 1.0];
let tanh_z = tanh_simd(&z.view());
// Result: [-0.762, 0.0, 0.762]

// Floor (rounding down)
let w = array![1.2, 2.7, -1.3, -2.9];
let floor_w = floor_simd(&w.view());
// Result: [1.0, 2.0, -2.0, -3.0]

// Arctangent 2 (angle from coordinates)
let y_coords = array![1.0, 1.0, -1.0, -1.0];
let x_coords = array![1.0, -1.0, -1.0, 1.0];
let angles = atan2_simd(&y_coords.view(), &x_coords.view());
// Result: [π/4, 3π/4, -3π/4, -π/4]

// Base-10 logarithm (decibels, pH scale)
let powers = array![1.0, 10.0, 100.0, 1000.0];
let log10_powers = log10_simd(&powers.view());
// Result: [0.0, 1.0, 2.0, 3.0]

// Clamp values to range (pixel normalization)
let pixels = array![-0.5, 0.3, 0.7, 1.2, 1.8];
let normalized = clamp_simd(&pixels.view(), 0.0, 1.0);
// Result: [0.0, 0.3, 0.7, 1.0, 1.0]

Functions§

abs_simd
Compute the absolute value of each element (SIMD-accelerated).
acos_simd
Compute the arccosine of each element (SIMD-accelerated).
acosh_simd
Apply inverse hyperbolic cosine (acosh) using SIMD operations
add_simd
SIMD-accelerated element-wise addition
argmax_simd
SIMD-accelerated argmax (index of maximum)
argmin_simd
SIMD-accelerated argmin (index of minimum)
asin_simd
Compute the arcsine of each element (SIMD-accelerated).
asinh_simd
Apply inverse hyperbolic sine (asinh) using SIMD operations
atan2_simd
Compute the two-argument arctangent element-wise (SIMD-accelerated).
atan_simd
Compute the arctangent of each element (SIMD-accelerated).
atanh_simd
Apply inverse hyperbolic tangent (atanh) using SIMD operations
beta_simd
Compute the Beta function B(a, b) using SIMD operations
cbrt_simd
Compute the cube root of each element (SIMD-accelerated).
ceil_simd
Compute the ceiling (round up) of each element (SIMD-accelerated).
clamp_simd
Clamp each element to a specified range [min, max] (SIMD-accelerated).
clip_simd
SIMD-accelerated element-wise clipping (clamping)
copysign_simd
SIMD-accelerated copysign operation
cos_simd
Compute the cosine of each element (SIMD-accelerated).
cosh_simd
Compute the hyperbolic cosine of each element (SIMD-accelerated).
cosine_similarity_simd
SIMD-accelerated cosine similarity
cumprod_simd
SIMD-accelerated cumulative product
cumsum_simd
SIMD-accelerated cumulative sum (prefix sum)
diff_simd
SIMD-accelerated first-order difference
digamma_simd
Computes the element-wise digamma function ψ(x) = d/dx ln(Γ(x)) using SIMD acceleration.
distance_chebyshev_simd
SIMD-accelerated Chebyshev distance
distance_cosine_simd
SIMD-accelerated cosine distance
distance_euclidean_simd
SIMD-accelerated Euclidean distance
distance_manhattan_simd
SIMD-accelerated Manhattan distance
div_simd
SIMD-accelerated element-wise division
dot_simd
SIMD-accelerated dot product
elu_simd
Apply ELU (Exponential Linear Unit) activation using SIMD operations
erf_simd
Element-wise error function erf(x) = (2/√π) ∫₀ˣ e^(-t²) dt
erfc_simd
Element-wise complementary error function erfc(x) = 1 - erf(x)
erfcinv_simd
Element-wise inverse complementary error function erfcinv(y) = x such that erfc(x) = y
erfinv_simd
Element-wise inverse error function erfinv(y) = x such that erf(x) = y
exp2_simd
Compute 2^x for each element (SIMD-accelerated).
exp_m1_simd
Compute exp(x)-1 for each element (SIMD-accelerated, numerically stable).
exp_simd
Compute the exponential (e^x) of each element (SIMD-accelerated).
expm1_simd
SIMD-accelerated numerically stable exp(x) - 1
floor_simd
Compute the floor (round down) of each element (SIMD-accelerated).
fma_simd
SIMD-accelerated fused multiply-add
fract_simd
Compute the fractional part of each element (SIMD-accelerated).
gamma_simd
Compute the gamma function Γ(x) for each element (SIMD-accelerated).
gelu_simd
Compute the element-wise GELU (Gaussian Error Linear Unit) of an array.
hardsigmoid_simd
Apply Hardsigmoid activation using SIMD operations
hardswish_simd
Apply Hardswish activation using SIMD operations
hypot_simd
SIMD-accelerated hypotenuse calculation
leaky_relu_simd
Apply Leaky ReLU / PReLU activation using SIMD operations
lerp_simd
SIMD-accelerated linear interpolation
ln_1p_simd
Compute ln(1+x) for each element (SIMD-accelerated, numerically stable).
ln_beta_simd
Compute the Log-Beta function ln(B(a, b)) using SIMD operations
ln_gamma_simd
Computes the element-wise log-gamma function ln(Γ(x)) using SIMD acceleration.
ln_simd
Compute the natural logarithm (ln(x)) of each element (SIMD-accelerated).
log1p_simd
SIMD-accelerated numerically stable ln(1 + x)
log2_simd
Compute the base-2 logarithm of each element (SIMD-accelerated).
log10_simd
Compute the base-10 logarithm of each element (SIMD-accelerated).
log_softmax_simd
Apply Log-Softmax function using SIMD operations
log_sum_exp_simd
SIMD-accelerated log-sum-exp
logaddexp_simd
SIMD-accelerated logaddexp: log(exp(a) + exp(b))
logit_simd
SIMD-accelerated logit function: log(p / (1-p))
max_element_simd
SIMD-accelerated maximum element
max_simd
SIMD-accelerated element-wise maximum
mean_simd
SIMD-accelerated array mean
min_element_simd
SIMD-accelerated minimum element
min_simd
SIMD-accelerated element-wise minimum
mish_simd
SIMD-accelerated Mish activation function
mul_simd
SIMD-accelerated element-wise multiplication
norm_l1_simd
SIMD-accelerated L1 norm (Manhattan norm)
norm_linf_simd
SIMD-accelerated L-infinity norm (Chebyshev/max norm)
norm_simd
SIMD-accelerated L2 norm (Euclidean norm)
normalize_simd
SIMD-accelerated L2 normalization
pow_simd
Compute the element-wise power with array exponents (SIMD-accelerated).
powf_simd
Compute the power of each element with a scalar exponent (SIMD-accelerated).
powi_simd
Compute integer power (base^n) for each element (SIMD-accelerated).
prelu_simd
Alias for leaky_relu_simd - PReLU (Parametric ReLU)
recip_simd
Compute the reciprocal (multiplicative inverse) of each element (SIMD-accelerated).
relu_simd
SIMD-accelerated ReLU (Rectified Linear Unit)
round_simd
Compute the rounding to nearest integer of each element (SIMD-accelerated).
rsqrt_simd
SIMD-accelerated inverse square root: 1/sqrt(x)
scalar_mul_simd
SIMD-accelerated scalar multiplication
selu_simd
Apply SELU (Scaled Exponential Linear Unit) activation using SIMD operations
sigmoid_simd
Compute the element-wise sigmoid (logistic) function of an array.
sign_simd
Compute the sign (signum) of each element (SIMD-accelerated).
sin_simd
Compute the sine of each element (SIMD-accelerated).
sinc_simd
Apply Sinc function using SIMD operations
sincos_simd
SIMD-accelerated simultaneous sin and cos computation
sinh_simd
Compute the hyperbolic sine of each element (SIMD-accelerated).
smootherstep_simd
SIMD-accelerated smootherstep interpolation (Ken Perlin’s improved version)
smoothstep_simd
SIMD-accelerated smoothstep interpolation
softmax_simd
SIMD-accelerated softmax
softplus_simd
SIMD-accelerated Softplus activation function
sqrt_simd
Compute the square root of each element (SIMD-accelerated).
square_simd
SIMD-accelerated element-wise square: x²
standardize_simd
SIMD-accelerated standardization (z-score normalization)
std_simd
SIMD-accelerated standard deviation computation
sub_simd
SIMD-accelerated element-wise subtraction
sum_cubes_simd
SIMD-accelerated sum of cubes
sum_simd
SIMD-accelerated array sum
sum_squares_simd
SIMD-accelerated sum of squares
swish_simd
SIMD-accelerated Swish (SiLU - Sigmoid Linear Unit) activation function
tan_simd
Compute the tangent of each element (SIMD-accelerated).
tanh_simd
Compute the hyperbolic tangent of each element (SIMD-accelerated).
to_degrees_simd
Convert radians to degrees for each element (SIMD-accelerated).
to_radians_simd
Convert degrees to radians for each element (SIMD-accelerated).
trigamma_simd
Computes the element-wise trigamma function ψ’(x) = d²/dx² ln(Γ(x)) using SIMD acceleration.
trunc_simd
SIMD-accelerated truncation (round towards zero)
variance_simd
SIMD-accelerated variance computation
weighted_mean_simd
SIMD-accelerated weighted mean
weighted_sum_simd
SIMD-accelerated weighted sum