Learn R Programming

BigDataStatMeth (version 1.0.3)

bdDiag_divide_hdf5: Divide Diagonal Elements from HDF5 Matrices or Vectors

Description

Performs optimized diagonal division between two datasets stored in HDF5 format. Automatically detects whether inputs are matrices (extracts diagonals) or vectors (direct operation) and uses the most efficient approach. This function is ~50-250x faster than traditional matrix operations for diagonal computations.

Usage

bdDiag_divide_hdf5(
  filename,
  group,
  A,
  B,
  groupB = NULL,
  target = NULL,
  outgroup = NULL,
  outdataset = NULL,
  paral = NULL,
  threads = NULL,
  overwrite = NULL
)

Value

List with components:

fn

Character string with the HDF5 filename

ds

Character string with the full dataset path to the diagonal division result (group/dataset)

Arguments

filename

String. Path to the HDF5 file containing the datasets.

group

String. Group path containing the first dataset (A, dividend).

A

String. Name of the first dataset (dividend).

B

String. Name of the second dataset (divisor).

groupB

Optional string. Group path containing dataset B. If NULL, uses same group as A.

target

Optional string. Where to write result: "A", "B", or "new" (default: "new").

outgroup

Optional string. Output group path. Default is "OUTPUT".

outdataset

Optional string. Output dataset name. Default is "A_/_B" with .diag suffix if appropriate.

paral

Optional logical. Whether to use parallel processing. Default is FALSE.

threads

Optional integer. Number of threads for parallel processing. If NULL, uses maximum available threads.

overwrite

Optional logical. Whether to overwrite existing datasets. Default is FALSE.

Details

This function provides flexible diagonal division with automatic optimization:

  • Operation modes:

    • Matrix / Matrix: Extract diagonals → vector division → save as vector

    • Matrix / Vector: Extract diagonal → vector division → save as vector

    • Vector / Vector: Direct vector division (most efficient)

  • Performance features:

    • Uses optimized vector operations for maximum efficiency

    • Automatic type detection and dimension validation

    • Memory-efficient processing for large datasets

    • Parallel processing support for improved performance

  • Mathematical properties:

    • Element-wise division: result[i] = A[i] / B[i]

    • Division by zero results in infinity (IEEE 754 standard)

    • Handles special cases: ±inf, NaN, and subnormal numbers

    • Order matters: \(A / B \neq B / A\).

Examples

Run this code
if (FALSE) {
library(BigDataStatMeth)

# Create test matrices
N <- 1000
set.seed(123)
A <- matrix(rnorm(N*N), N, N)
B <- matrix(rnorm(N*N, mean=1), N, N)  # Avoid division by zero

# Save to HDF5
bdCreate_hdf5_matrix("test.hdf5", A, "data", "matrixA",
                     overwriteFile = TRUE)
bdCreate_hdf5_matrix("test.hdf5", B, "data", "matrixB",
                     overwriteFile = FALSE)

# Divide diagonals
result <- bdDiag_divide_hdf5("test.hdf5", "data", "matrixA", "matrixB",
                            outgroup = "results",
                            outdataset = "diagonal_ratio",
                            paral = TRUE)
}

Run the code above in your browser using DataLab