Numeric

This module provides simple numeric routines in Lua.

halmd.numeric.sum(t)

Compute the sum of the indexed elements of a table.

Parameters:t (table) – input table
Returns:sum over all indexed elements in t
halmd.numeric.prod(t)

Compute the product of the indexed elements of a table.

Parameters:t (table) – input table
Returns:product over all indexed elements in t
halmd.numeric.find_comp(t, comp)

Find the last value of a table that satisfies comp(a,b)

Parameters:
  • t (table) – input table
  • comp – callable that takes two elements of t and returns true or false
Returns:

last element in t that satisfied comp(a,b)

halmd.numeric.max(t)

Find the maximum value in a table

Parameters:t (table) – input table
Returns:maximum value in t
halmd.numeric.min(t)

Find the minimum value in a table

Parameters:t (table) – input table
Returns:minmum value in t
halmd.numeric.scalar_vector(size, value)

Create vector of given size with scalar value

Parameters:
  • size (number) – number of elements
  • value – value for each element of the vector
Returns:

vector of length \text{size} with each element set to value

halmd.numeric.scalar_matrix(rows, columns, value)

Create matrix of given size with scalar value

Parameters:
  • rows (number) – number of rows
  • columns (number) – number of columns
  • value – value for each element of the matrix
Returns:

matrix of dimension \text{rows} \times \text{columns} with each element set to value

halmd.numeric.trans(m)

Calculate transpose of matrix

Parameters:m (matrix) – input matrix
Returns:transpose of m
halmd.numeric.diag(m)

Return diagonal elements of n×n matrix

Parameters:m (matrix) – input square matrix
Returns:table of diagonal elements of m
halmd.numeric.add(t1, t2)

Add two vectors elementwisely

Parameters:
  • t1 (table) – n-dimensional vector
  • t2 (table) – n-dimensional vector
Returns:

elementwise sum of two vectors

halmd.numeric.subtract(t1, t2)

Subtract two vectors elementwisely

Parameters:
  • t1 (table) – n-dimensional vector
  • t2 (table) – n-dimensional vector
Returns:

elementwise difference of two vectors

halmd.numeric.add_scalar(t, a)

Add a scalar value to all elements of a vector

Parameters:
  • t (table) – n-dimensional vector
  • a (number) – the scalar to add
Returns:

table with values bigger by a.

halmd.numeric.subtract_scalar(t, a)

Subtract a scalar value from all elements of a vector

Parameters:
  • t (table) – n-dimensional vector
  • a (number) – the scalar to subtract
Returns:

table with values less by a.

halmd.numeric.multiply(t1, t2)

Multiply two vectors elementwisely

Parameters:
  • t1 (table) – n-dimensional vector
  • t2 (table) – n-dimensional vector
Returns:

elementwise product of two vectors

halmd.numeric.multiply_scalar(a, t)

Multiply all elements of a vector with the same scalar

Parameters:
  • a (number) – the scalar factor
  • t (table) – n-dimensional vector
Returns:

table with values multiplied by a

halmd.numeric.divide(t1, t2)

Divide two vectors elementwisely

Parameters:
  • t1 (table) – n-dimensional vector
  • t2 (table) – n-dimensional vector
Returns:

elementwise division of two vectors

halmd.numeric.divide_scalar(t, a)

Divide all elements of a vector by the same scalar value

Parameters:
  • t (table) – n-dimensional vector
  • a (number) – the scalar divisor
Returns:

table with values divided by a

halmd.numeric.norm_1(t)

Get 1-norm of a vector

Parameters:t (table) – n-dimensional vector
Returns:1-norm of the vector
halmd.numeric.norm_2(t)

Get 2-norm of a vector

Parameters:t (table) – n-dimensional vector
Returns:2-norm of the vector
halmd.numeric.offset_to_multi_index(offset, dims)

Convert one-dimensional offset to multi-dimensional index

Assumes contiguous storage of the array data in row-major order.

Parameters:
  • offset (number) – 1-based one-dimensional offset
  • dims (table) – dimensions (shape) of multi-dimensional array
Returns:

1-based multi-dimensional index of array element at offset

halmd.numeric.multi_index_to_offset(index, dims)

Convert multi-dimensional index to one-dimensional offset

Assumes contiguous storage of the array data in row-major order.

Parameters:
  • index (table) – 1-based multi-dimensional index
  • dims (table) – dimensions (shape) of multi-dimensional array
Returns:

1-based offset of array element at index

halmd.numeric.assert_close(value1, value2, tolerance, level)

Assert that two floating-point numbers are very close.

Given two floating point numbers a and b and a tolerance \epsilon > 0, the routine asserts that

|a - b| < \epsilon \min(|a|, |b|)

Parameters:
  • value1 (number) – value of a
  • value2 (number) – value of b
  • tolerance (number) – value of \epsilon (default: `numeric.limits.double.epsilon`)
  • level (number) – call stack level for error message (default: 2)
Returns:

value1

Provide floating-point limits, along the lines of std::numeric_limits in C++:

limits.double.epsilon = 2.2204460492503131e-16