| Did you know ... | Search Documentation: |
| Pack array -- archive/README.old.md |
Array.pl is a powerful library for SWI-Prolog that provides N-dimensional arrays (tensors) with an API inspired by Python's NumPy library. It is designed for performance and flexibility, featuring lazy evaluation and an extensible backend system.
reshape, transpose, slice) are performed by creating a new "view" on the underlying data without copying it. This makes these operations extremely fast and memory-efficient.opencl-headers and ocl-icd-devel on Fedora)The foreign libraries are built in `build/` before starting Prolog:
cmake -B build -DCMAKE_PREFIX_PATH=<swipl install prefix> cmake --build build
To use the library, simply load the main array.pl module in your Prolog code:
:- use_module(array).
?- A = array{}.fromlist([[1, 2], [3, 4]]).
A = array{data:data(1, 2, 3, 4), ...}.
?- Z = array{}.zeros([2, 3]).
Z = array{... shape:[2,3], op:constant(0)}.
?- O = array{}.ones([3, 2]).
O = array{... shape:[3,2], op:constant(1)}.
Operations are scheduled lazily. The @= operator is used to define an operation without immediately computing it. The actual computation is triggered when the result is requested, for example by calling `.tolist() or .realize()`.
?- A = array{}.fromlist([[1,2],[3,4]]),
B = array{}.fromlist([[5,6],[7,8]]),
C @= A + B.
C = array{...}.
?- List = C.tolist().
List = [[6, 8], [10, 12]].
These operations are zero-copy and very fast.
?- A = array{}.arange(6),
ListA = A.tolist().
ListA = [0, 1, 2, 3, 4, 5].
?- B = A.reshape([2, 3]),
ListB = B.tolist().
ListB = [[0, 1, 2], [3, 4, 5]].
?- C = B.transpose(),
ListC = C.tolist().
ListC = [[0, 3], [1, 4], [2, 5]].
?- A = array{}.fromlist([[1,2,3],[4,5,6]]),
S @= A.sum(),
SumValue = S.tolist().
SumValue = 21.
?- M @= A.max([1]), % Max along axis 1
MaxValues = M.tolist().
MaxValues = [3, 6].
Array.pl's power comes from a few core concepts:
array{}: The primary data structure, a Prolog dict representing an N-dimensional array. It contains metadata like shape, data type, and the operation that defines it.view{}: A dict that defines how to interpret the underlying data. It holds shape, strides, offset, and mask information. An array can have a stack of views, allowing for complex, non-contiguous memory layouts without data duplication.op: An operation in the computation graph. It can be a simple constant (constant(0)), a transformation on a parent array (reshape(Parent)), or an element-wise operation on multiple arrays (elementwise(+, [A, B])).tolist() or .realize()`), Array.pl creates a schedule by performing a topological sort of the dependency graph. This schedule is then compiled and executed by the target device.prolog, which generates and runs Prolog code.<=), so narrowing their result never narrows their inputs.To run the built-in test suites, use the following command from the project root:
swipl -g "load_files([array, view, runtime_prolog]), run_tests" -t halt
clpBNR as a data type and backend.