From bc992f552fbed30a28e480ceb0f1258b3742d6eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Tue, 16 Jul 2013 07:59:38 -0500 Subject: [PATCH] use new notation --- docs/yap.tex | 14 +++++++ library/matrix.yap | 5 ++- library/matrix/matrix.c | 83 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) diff --git a/docs/yap.tex b/docs/yap.tex index 710ac0207..a791ad097 100644 --- a/docs/yap.tex +++ b/docs/yap.tex @@ -9962,6 +9962,13 @@ Unify @var{Elems} with the list including all the elements in @var{Matrix}. Unify @var{Elem} with the element of @var{Matrix} at position @var{Position}. +@item matrix_get(+@var{Matrix}[+@var{Position}],-@var{Elem}) +@findex matrix_get/2 +@snindex matrix_get/2 +@cnindex matrix_get/2 + +Unify @var{Elem} with the element @var{Matrix}[@var{Position}]. + @item matrix_set(+@var{Matrix},+@var{Position},+@var{Elem}) @findex matrix_set/3 @snindex matrix_set/3 @@ -9970,6 +9977,13 @@ Unify @var{Elem} with the element of @var{Matrix} at position Set the element of @var{Matrix} at position @var{Position} to @var{Elem}. +@item matrix_set(+@var{Matrix}[+@var{Position}],+@var{Elem}) +@findex matrix_set/2 +@snindex matrix_set/2 +@cnindex matrix_set/2 + +Set the element of @var{Matrix}[@var{Position}] to @var{Elem}. + @item matrix_set_all(+@var{Matrix},+@var{Elem}) @findex matrix_set_all/2 @snindex matrix_set_all/2 diff --git a/library/matrix.yap b/library/matrix.yap index 926d8c183..c8fe890c7 100644 --- a/library/matrix.yap +++ b/library/matrix.yap @@ -84,7 +84,10 @@ typedef enum { matrix_set_all_that_disagree/5, matrix_expand/3, matrix_select/4, - matrix_column/3 + matrix_column/3, + matrix_get/2, + matrix_set/2, + op(100, fy, '[]') ]). :- load_foreign_files([matrix], [], init_matrix). diff --git a/library/matrix/matrix.c b/library/matrix/matrix.c index be680a6ca..c47cd329b 100644 --- a/library/matrix/matrix.c +++ b/library/matrix/matrix.c @@ -220,6 +220,30 @@ scan_dims(int ndims, YAP_Term tl, int dims[MAX_DIMS]) return TRUE; } +static int +scan_dims_args(int ndims, YAP_Term tl, int dims[MAX_DIMS]) +{ + int i; + + for (i = 0; i < ndims; i++) { + YAP_Term th; + int d; + + th = YAP_ArgOfTerm(2+i, tl); + if (!YAP_IsIntTerm(th)) { + /* ERROR */ + return FALSE; + } + d = YAP_IntOfTerm(th); + if (d < 0) { + /* ERROR */ + return FALSE; + } + dims[i] = d; + } + return TRUE; +} + static int cp_int_matrix(YAP_Term tl,YAP_Term matrix) { @@ -607,6 +631,44 @@ matrix_set(void) return TRUE; } +static int +matrix_set2(void) +{ + int dims[MAX_DIMS], *mat; + YAP_Term tf, t = YAP_ARG1; + + mat = (int *)YAP_BlobOfTerm(YAP_ArgOfTerm(1,t)); + if (!mat) { + /* Error */ + return FALSE; + } + if (!scan_dims_args(mat[MAT_NDIMS], t, dims)) { + /* Error */ + return FALSE; + } + tf = YAP_ARG2; + if (mat[MAT_TYPE] == INT_MATRIX) { + if (YAP_IsIntTerm(tf)) { + matrix_long_set(mat, dims, YAP_IntOfTerm(tf)); + } else if (YAP_IsFloatTerm(tf)) { + matrix_long_set(mat, dims, YAP_FloatOfTerm(tf)); + } else { + /* Error */ + return FALSE; + } + } else { + if (YAP_IsIntTerm(tf)) { + matrix_float_set(mat, dims, YAP_IntOfTerm(tf)); + } else if (YAP_IsFloatTerm(tf)) { + matrix_float_set(mat, dims, YAP_FloatOfTerm(tf)); + } else { + /* Error */ + return FALSE; + } + } + return TRUE; +} + static int matrix_set_all(void) { @@ -698,6 +760,25 @@ do_matrix_access(void) return YAP_Unify(tf, YAP_ARG3); } +static int +do_matrix_access2(void) +{ + int dims[MAX_DIMS], *mat; + YAP_Term tf, t = YAP_ARG1; + + mat = (int *)YAP_BlobOfTerm(YAP_ArgOfTerm(1, t)); + if (!mat) { + /* Error */ + return FALSE; + } + if (!scan_dims_args(mat[MAT_NDIMS], t, dims)) { + /* Error */ + return FALSE; + } + tf = matrix_access(mat, dims); + return YAP_Unify(tf, YAP_ARG2); +} + static int do_matrix_inc(void) { @@ -2989,9 +3070,11 @@ init_matrix(void) YAP_UserCPredicate("new_floats_matrix", new_floats_matrix, 4); YAP_UserCPredicate("new_floats_matrix_set", new_floats_matrix_set, 4); YAP_UserCPredicate("matrix_set", matrix_set, 3); + YAP_UserCPredicate("matrix_set", matrix_set2, 2); YAP_UserCPredicate("matrix_set_all", matrix_set_all, 2); YAP_UserCPredicate("matrix_add", matrix_add, 3); YAP_UserCPredicate("matrix_get", do_matrix_access, 3); + YAP_UserCPredicate("matrix_get", do_matrix_access2, 2); YAP_UserCPredicate("matrix_inc", do_matrix_inc, 2); YAP_UserCPredicate("matrix_dec", do_matrix_dec, 2); YAP_UserCPredicate("matrix_inc", do_matrix_inc2, 3);