Matrix Operations in YAP.

The YAP Matrix library tries to provide an unified framework for matricial operations.

Matrices are available by loading the library ```

:- library(matrix).





Matrices are indexable objects formed with Prolog terms, or opaque objects, or `malloc` chunk, or foreign objects.



They are multimensional objects providing entries of type:

- `terms</tt>: Prolog terms +`ints: bounded integers, represented as an opaque term. The maximum integer depends on hardware, but should be obtained from the natural size of the machine.


- `floats: floating-point numbers, represented as an opaque term.

Notice that the library does not support all known matrix operations. Please contact the YAP maintainers if you require extra functionality.






General matrix assignment operation. It evaluates the right-hand side according to the left-hand side and to the matrix:





- if __LHS__ is part of an integer or floating-point matrix, perform non-backtrackable assignment.


- other unify left-hand side and right-hand size.

High Level Interface to Matrix evaluation.

1 Initialization:

- `Mat = matrix(Dims,Opts)` where Dims and Opts are lists:


- `Mat = matrix(Dims)`, same as matrix(Dims, [])`


- `Mat = ListOfLists`


- `Mat = matrix(Dims) of Type


- `Mat = matrix(Dims) of [Number](classNumber.md)`


- `Mat = matrix(Dims) of Goal`


- `Mat = matrix(Dims) of Offset^Goal`


- `Mat = matrix(Dims) of call(Goal)`


- `Mat = vector(Dim,Opts)` same as `matrix([Dims], [])`


- `Mat = zeros(Dims)`, same as `matrix(Dims, [])`


- `Mat = matrix[Dims]`, same as `matrix(Dims, [])`


- `Mat = matrix[Dims] of Def, same as`matrix(Dims) of Def`




Dimensions may be given as: ListOfNumbers.






`Options are:

- One can use use old array routines (check @ref static_arrayU2fU_3 "static_array//3"3) for
  static storage (ie, heap allocated`)

1. `mat <== [1000,1001,1002]`
1. `vec <== matrix[1000] of int`
2. `zeros <== matrix[333] of 0`
` The local matrices allocate on stack:
1. `Var <== [[1000],[1001],[1002]]`



1. `Var <== matrix[1000] of term`


1. `Var <== matrix[333,2] of 3.1415`






YAP also supports foreign matrices.



Matrix elements can be accessed through the matrix_get//22 predicate or through an `R</tt>-inspired access notation (that uses the ciao style extension to`[]`). Examples include:





- Access the second row, third column of matrix `X</tt>. Indices start from `0`, ```

 _E_ <== _X_[2,3]

Access all the second row, the output is a list ofe elements. ```

L <== X[2,_]







- Access all the second, thrd and fourth rows, the output is a list of elements. ```

 _L_ <== _X_[2..4,_]
  • Access all the fifth, sixth and eight rows, the output is a list of elements. ```

L <== X[2..4+3,_] ```

The right-hand side supports the following operators:

  • []

written as M[ Offset]: obtain an element or list of elements of matrix M at offset Offset.

  • ..

I.. J generates a list with all integers from I to J, included.

  • +

add two numbers, add two matrices element-by-element, or add a number to all elements of a matrix or list.

  • -

subtract two numbers, subtract two matrices or lists element-by-element, or subtract a number from all elements of a matrix or list

  • *

multiply two numbers, multiply two matrices or lists element-by-element, or multiply a number from all elements of a matrix or list

  • log

natural logarithm of a number, matrix or list

  • exp

natural exponentiation of a number, matrix or list

The dimensions can be given as an integer, and the matrix will be indexed C-style from 0..( _Max_-1), or can be given as an interval Base.. Limit. In the latter case, matrices of integers and of floating-point numbers should have the same Base on every dimension.

Define:

1. MAX_DIMS:

Functions:

1. void * Yap_StaticArray(YAP_Atom na, size_t dim, static_array_types type, void start_addr, void p):

1. static YAP_Int * matrix_long_data(intptr_t *mat, intptr_t ndims):

1. static double * matrix_double_data(intptr_t *mat, intptr_t ndims):

1. static intptr_t matrix_get_offset(intptr_t mat, intptr_t indx):

1. static void matrix_get_index(intptr_t mat, intptr_t offset, intptr_t indx):

1. static size_t GET_OFFSET(YAP_Term t, M mat, intptr_t poffset):

1. static YAP_Bool GET_INDEX(M mat, intptr_t off, YAP_Term tf):

1. static void matrix_next_index(intptr_t dims, intptr_t ndims, intptr_t indx):

1. bool IS_MATRIX(YAP_Term):

1. static bool GET_MATRIX(YAP_Term inp, M *o):

1. static YAP_Bool is_matrix(void):

1. static YAP_Term new_int_matrix(intptr_t ndims, intptr_t dims[], intptr_t base, YAP_Int data[]):

1. static YAP_Term new_float_matrix(intptr_t ndims, intptr_t dims[], intptr_t base, double data[]):

1. static int scan_dims(YAP_Term tl, intptr_t dims[MAX_DIMS]):

1. static YAP_Bool cp_int_matrix(YAP_Term tl, M *m):

1. static YAP_Bool cp_float_matrix(YAP_Term tl, M *m):

1. static YAP_Bool set_int_matrix(YAP_Term matrix, YAP_Int set):

1. static YAP_Bool set_float_matrix(YAP_Term matrix, double set):

1. static YAP_Bool new_ints_matrix(void):

1. static YAP_Bool new_ints_matrix_set(void):

1. static YAP_Bool new_ints_matrix_copy(void):

1. static YAP_Bool new_floats_matrix_copy(void):

1. static YAP_Bool new_floats_matrix(void):

1. static YAP_Bool new_floats_matrix_set(void):

1. static YAP_Term mk_int_list(intptr_t nelems, intptr_t *data):

1. static YAP_Term mk_rep_int_list(intptr_t nelems, int data):

1. static void matrix_float_set(intptr_t mat, intptr_t indx, double nval):

1. static void matrix_long_set(intptr_t mat, intptr_t indx, YAP_Int nval):

1. static YAP_Bool matrix_get_one(void):

1. static YAP_Bool matrix_set_one(void):

1. static YAP_Term flist(YAP_Term t, double *f):

1. static YAP_Term ilist(YAP_Term t, YAP_Int *f):

1. static YAP_Term blist(YAP_Term t, YAP_Bool *f):

1. static YAP_Bool matrix_set_all(void):

1. static YAP_Bool matrix_add_to_all(void):

1. static YAP_Bool matrix_add(void):

1. static YAP_Bool matrix_inc(void):

1. static YAP_Bool matrix_in/3(void):

1. static YAP_Bool matrix_dec(void):

1. static YAP_Bool matrix_de/3(void):

1. static YAP_Bool matrix_min(void):

1. static YAP_Bool matrix_minarg(void):

1. static YAP_Bool matrix_max(void):

1. static YAP_Bool matrix_maxarg(void):

1. static YAP_Bool matrix_dims(void):

1. static YAP_Bool matrix_to_list(void):

1. static YAP_Bool matrix_set_base(void):

1. static YAP_Bool matrix_base(void):

1. static YAP_Bool matrix_dim/3(void):

1. static YAP_Bool matrix_size(void):

1. static YAP_Bool matrix_ndims(void):

1. static YAP_Bool matrix_short_type(void):

1. static YAP_Bool matrix_arg_to_offset(void):

1. static YAP_Bool matrix_offset_to_arg(void):

1. static YAP_Bool matrix_log_all(void):

1. static YAP_Bool matrix_log_al/2(void):

1. static YAP_Bool matrix_exp_all(void):

1. static YAP_Bool matrix_exp2_all(void):

1. static YAP_Bool matrix_exp_al/2(void):

1. static YAP_Bool matrix_sum(void):

1. static void add_int_lines(int total, intptr_t nlines, YAP_Int mat0, YAP_Int matf):

1. static void add_double_lines(int total, intptr_t nlines, double mat0, double matf):

1. static YAP_Bool matrix_agg_lines(void):

1. static void add_int_cols(int total, intptr_t nlines, YAP_Int mat0, YAP_Int matf):

1. static void add_double_cols(int total, intptr_t nlines, double mat0, double matf):

1. static YAP_Bool matrix_agg_cols(void):

1. static void div_int_by_lines(int total, intptr_t nlines, YAP_Int mat1, YAP_Int mat2, double *ndata):

1. static void div_int_by_dlines(int total, intptr_t nlines, YAP_Int mat1, double mat2, double *ndata):

1. static void div_float_long_by_lines(int total, intptr_t nlines, double mat1, YAP_Int mat2, double *ndata):

1. static void div_float_by_lines(int total, intptr_t nlines, double mat1, double mat2, double *ndata):

1. static YAP_Bool matrix_op_to_lines(void):

1. static YAP_Bool matrix_copy(void):

1. static void matrix_long_add_data(YAP_Int *nmat, int siz, YAP_Int mat1[], YAP_Int mat2[]):

1. static void matrix_long_double_add_data(double *nmat, int siz, YAP_Int mat1[], double mat2[]):

1. static void matrix_double_add_data(double *nmat, int siz, double mat1[], double mat2[]):

1. static void matrix_long_sub_data(YAP_Int *nmat, int siz, YAP_Int mat1[], YAP_Int mat2[]):

1. static void matrix_long_double_sub_data(double *nmat, int siz, YAP_Int mat1[], double mat2[]):

1. static void matrix_long_double_rsub_data(double *nmat, int siz, double mat1[], YAP_Int mat2[]):

1. static void matrix_double_sub_data(double *nmat, int siz, double mat1[], double mat2[]):

1. static void matrix_long_mult_data(YAP_Int *nmat, int siz, YAP_Int mat1[], YAP_Int mat2[]):

1. static void matrix_long_double_mult_data(double *nmat, int siz, YAP_Int mat1[], double mat2[]):

1. static void matrix_double_mult_data(double *nmat, int siz, double mat1[], double mat2[]):

1. static void matrix_long_div_data(YAP_Int *nmat, int siz, YAP_Int mat1[], YAP_Int mat2[]):

1. static void matrix_long_double_div_data(double *nmat, int siz, YAP_Int mat1[], double mat2[]):

1. static void matrix_long_double_div2_data(double *nmat, int siz, double mat1[], YAP_Int mat2[]):

1. static void matrix_double_div_data(double *nmat, int siz, double mat1[], double mat2[]):

1. static void matrix_long_zdiv_data(YAP_Int *nmat, int siz, YAP_Int mat1[], YAP_Int mat2[]):

1. static void matrix_long_double_zdiv_data(double *nmat, int siz, YAP_Int mat1[], double mat2[]):

1. static void matrix_double_zdiv_data(double *nmat, int siz, double mat1[], double mat2[]):

1. static YAP_Bool matrix_op(void):

1. static void add_int_by_cols(int total, intptr_t nlines, YAP_Int mat1, YAP_Int mat2, YAP_Int *ndata):

1. static void add_int_by_dcols(int total, intptr_t nlines, YAP_Int mat1, double mat2, double *ndata):

1. static void add_double_by_cols(int total, intptr_t nlines, double mat1, double mat2, double *ndata):

1. static YAP_Bool matrix_op_to_cols(void):

1. static YAP_Bool matrix_op_to_all(void):

1. static YAP_Bool matrix_transpose(void):

1. static YAP_Bool matrix_select(void):

1. static YAP_Bool matrix_column(void):

1. static YAP_Bool matrix_sum_out(void):

1. static YAP_Bool matrix_sum_out_several(void):

1. static YAP_Bool matrix_sum_out_logs(void):

1. static YAP_Bool matrix_sum_out_logs_several(void):

1. static YAP_Bool matrix_expand(void):

1. static YAP_Bool matrix_set_all_that_disagree(void):

1. static YAP_Bool matrix_m(void):

1. X_API void init_matrix(void):

Var:

1. YAP_Functor MFunctorM:

1. YAP_Functor MFunctorFloats:

1. YAP_Term MTermTrue:

1. YAP_Term MTermFalse:

1. YAP_Term MTermFail:

1. YAP_Atom MAtomC:

Typedef:

1. typedef enum static_array_type static_array_types:

1. typedef struct m M: : A matrix.

Enum:

1. big_blob_type:

1. static_array_type:

1. op_type: : A matrix is something of the form.

TYPE = {int,double} BASE = integer DIMS = an int DIM1 ... DIMn DATA in C format.

floating point matrixes may need to be aligned, so we always have an extra element at the end.