Module array
Arrays with operations.
This module provides a function which returns a specialized constructor for ffi-based arrays of size n (zero-filled by default).
The arrays created with this constructor have overloaded (pointwise) operators + (add), - (sub), * (mul), / (div) and unary - (unm), as well as __tostring and #. The modulo operator % (mod) calculates the scalar product.
However, as these mathematical operations are not allowed to modify the original arrays, they create new arrays for each operation. If you do not want this, use the functions add, sub, mul, smul, div, unm provided as methods of an array object. These functions take an extra argument, namely a result buffer, which in turn is returned by these functions, so that the operations can be chained.
To clear (i.e. zero-fill) an array, the clear()
method can be called.
e.g.
local narray = require("array")(10)
local x, y = narray(), narray()
x[2] = 29
y:add(x,y) -- add y to x
print(y)
y:clear()
Functions
array (n) | Create a constructor for n-arrays. |
nmul (lhs, factor, res) | Multiplication with a number. |
add (lhs, rhs, res) | Pointwise addition |
sub (lhs, rhs, res) | Pointwise subtraction |
mul (lhs, rhs, res) | Pointwise product |
smul (lhs, rhs) | Scalar product |
div (lhs, rhs, res) | Pointwise division |
unm (lhs, res) | Unary minus |
clear (lhs) | Zero-fill the array |
Functions
- array (n)
-
Create a constructor for n-arrays.
Parameters:
- n array length
Returns:
-
n-array constructor
- nmul (lhs, factor, res)
-
Multiplication with a number.
Parameters:
- lhs n-array
- factor number
- res result n-array (buffer)
Returns:
-
result n-array
- add (lhs, rhs, res)
-
Pointwise addition
Parameters:
- lhs
- rhs
- res
- sub (lhs, rhs, res)
-
Pointwise subtraction
Parameters:
- lhs
- rhs
- res
- mul (lhs, rhs, res)
-
Pointwise product
Parameters:
- lhs
- rhs
- res
- smul (lhs, rhs)
-
Scalar product
Parameters:
- lhs
- rhs
- div (lhs, rhs, res)
-
Pointwise division
Parameters:
- lhs
- rhs
- res
- unm (lhs, res)
-
Unary minus
Parameters:
- lhs
- res
- clear (lhs)
-
Zero-fill the array
Parameters:
- lhs