diff --git a/library/matrix/matrix.c b/library/matrix/matrix.c index 5c06bdc8a..0ee9f68af 100644 --- a/library/matrix/matrix.c +++ b/library/matrix/matrix.c @@ -1,22 +1,20 @@ /************************************************************************* -* * -* YAP Prolog * -* * -* Yap Prolog was developed at NCCUP - Universidade do Porto * -* * -* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 * -* * -************************************************************************** -* * -* File: matrix.c * -* Last rev: * -* mods: * -* comments: numerical arrays * -* * -*************************************************************************/ + * * + * YAP Prolog * + * * + * Yap Prolog was developed at NCCUP - Universidade do Porto * + * * + * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 * + * * + ************************************************************************** + * * + * File: matrix.c * Last rev: + ** mods: * comments: numerical arrays * + * * + *************************************************************************/ -#include "config.h" #include "YapInterface.h" +#include "config.h" #include #if defined(__MINGW32__) || _MSC_VER #include @@ -43,96 +41,82 @@ /* maximal number of dimensions, 1024 should be enough */ #define MAX_DIMS 1024 -typedef enum { - INT_MATRIX, - FLOAT_MATRIX -} mat_data_type; +typedef enum { INT_MATRIX, FLOAT_MATRIX } mat_data_type; typedef enum { - MAT_TYPE=0, - MAT_BASE=1, - MAT_NDIMS=2, - MAT_SIZE=3, - MAT_ALIGN=4, - MAT_DIMS=5, + MAT_TYPE = 0, + MAT_BASE = 1, + MAT_NDIMS = 2, + MAT_SIZE = 3, + MAT_ALIGN = 4, + MAT_DIMS = 5, } mat_type; typedef enum { - MAT_PLUS=0, - MAT_SUB=1, - MAT_TIMES=2, - MAT_DIV=3, - MAT_IDIV=4, - MAT_ZDIV=5, - MAT_LOG=6, - MAT_EXP=7 + MAT_PLUS = 0, + MAT_SUB = 1, + MAT_TIMES = 2, + MAT_DIV = 3, + MAT_IDIV = 4, + MAT_ZDIV = 5, + MAT_LOG = 6, + MAT_EXP = 7 } op_type; YAP_Functor FunctorM; YAP_Atom AtomC; -static long int * -matrix_long_data(int *mat, int ndims) -{ - return (long int *)(mat+(MAT_DIMS+ndims)); +static long int *matrix_long_data(int *mat, int ndims) { + return (long int *)(mat + (MAT_DIMS + ndims)); } -static double * -matrix_double_data(int *mat, int ndims) -{ - return (double *)(mat+(MAT_DIMS+ndims)); +static double *matrix_double_data(int *mat, int ndims) { + return (double *)(mat + (MAT_DIMS + ndims)); } -static unsigned int -matrix_get_offset(int *mat, int* indx) -{ +static unsigned int matrix_get_offset(int *mat, int *indx) { unsigned int i, pos = mat[MAT_SIZE], off = 0; /* find where we are */ for (i = 0; i < mat[MAT_NDIMS]; i++) { int v; - pos /= mat[MAT_DIMS+i]; - v = indx[i]-mat[MAT_BASE]; - if (v >= mat[MAT_DIMS+i]) { + pos /= mat[MAT_DIMS + i]; + v = indx[i] - mat[MAT_BASE]; + if (v >= mat[MAT_DIMS + i]) { return off; } - off += pos*v; + off += pos * v; } return off; } -static void -matrix_get_index(int *mat, unsigned int offset, int* indx) -{ +static void matrix_get_index(int *mat, unsigned int offset, int *indx) { unsigned int i, pos = mat[MAT_SIZE]; /* find where we are */ for (i = 0; i < mat[MAT_NDIMS]; i++) { - pos /= mat[MAT_DIMS+i]; + pos /= mat[MAT_DIMS + i]; indx[i] = offset / pos; offset = offset % pos; } } -static void -matrix_next_index(int *dims, int ndims, int* indx) -{ +static void matrix_next_index(int *dims, int ndims, int *indx) { unsigned int i; /* find where we are */ - for (i = ndims; i >0; ) { + for (i = ndims; i > 0;) { i--; indx[i]++; - if (indx[i]!=dims[i]) return; + if (indx[i] != dims[i]) + return; indx[i] = 0; } } -static YAP_Term -new_int_matrix(int ndims, int dims[], long int data[]) -{ +static YAP_Term new_int_matrix(int ndims, int dims[], long int data[]) { unsigned int sz; - unsigned int i, nelems=1; + unsigned int i, nelems = 1; YAP_Term blob; int *mat; long int *bdata; @@ -140,13 +124,15 @@ new_int_matrix(int ndims, int dims[], long int data[]) /* in case we don't have enough room and need to shift the stack, we can't really afford to keep a pointer to the global */ - for (i=0;i< ndims;i++) { + for (i = 0; i < ndims; i++) { idims[i] = dims[i]; nelems *= dims[i]; } - sz = ((MAT_DIMS+1)*sizeof(int)+ndims*sizeof(int)+nelems*sizeof(long int))/sizeof(YAP_CELL); + sz = ((MAT_DIMS + 1) * sizeof(int) + ndims * sizeof(int) + + nelems * sizeof(long int)) / + sizeof(YAP_CELL); blob = YAP_MkBlobTerm(sz); - if (blob == YAP_TermNil()) { + if (blob == YAP_TermNil()) { return blob; } mat = (int *)YAP_BlobOfTerm(blob); @@ -154,20 +140,18 @@ new_int_matrix(int ndims, int dims[], long int data[]) mat[MAT_BASE] = 0; mat[MAT_NDIMS] = ndims; mat[MAT_SIZE] = nelems; - for (i=0;i< ndims;i++) { - mat[MAT_DIMS+i] = idims[i]; + for (i = 0; i < ndims; i++) { + mat[MAT_DIMS + i] = idims[i]; } - bdata = matrix_long_data(mat,ndims); + bdata = matrix_long_data(mat, ndims); if (data) - memmove((void *)bdata,(void *)data,sizeof(double)*nelems); + memmove((void *)bdata, (void *)data, sizeof(double) * nelems); return blob; } -static YAP_Term -new_float_matrix(int ndims, int dims[], double data[]) -{ - unsigned int sz; - unsigned int i, nelems=1; +static YAP_Term new_float_matrix(int ndims, int dims[], double data[]) { + unsigned int sz; + unsigned int i, nelems = 1; YAP_Term blob; int *mat; double *bdata; @@ -175,11 +159,13 @@ new_float_matrix(int ndims, int dims[], double data[]) /* in case we don't have enough room and need to shift the stack, we can't really afford to keep a pointer to the global */ - for (i=0;i< ndims;i++) { + for (i = 0; i < ndims; i++) { idims[i] = dims[i]; nelems *= dims[i]; } - sz = ((MAT_DIMS+1)*sizeof(int)+ndims*sizeof(int)+(nelems+1)*sizeof(double)+(sizeof(YAP_CELL)-1))/sizeof(YAP_CELL); + sz = ((MAT_DIMS + 1) * sizeof(int) + ndims * sizeof(int) + + (nelems + 1) * sizeof(double) + (sizeof(YAP_CELL) - 1)) / + sizeof(YAP_CELL); blob = YAP_MkBlobTerm(sz); if (blob == YAP_TermNil()) return blob; @@ -188,18 +174,16 @@ new_float_matrix(int ndims, int dims[], double data[]) mat[MAT_BASE] = 0; mat[MAT_NDIMS] = ndims; mat[MAT_SIZE] = nelems; - for (i=0;i< ndims;i++) { - mat[MAT_DIMS+i] = idims[i]; + for (i = 0; i < ndims; i++) { + mat[MAT_DIMS + i] = idims[i]; } - bdata = matrix_double_data(mat,ndims); + bdata = matrix_double_data(mat, ndims); if (data) - memmove((void *)bdata,(void *)data,sizeof(double)*nelems); + memmove((void *)bdata, (void *)data, sizeof(double) * nelems); return blob; } -static YAP_Bool -scan_dims(int ndims, YAP_Term tl, int dims[MAX_DIMS]) -{ +static YAP_Bool scan_dims(int ndims, YAP_Term tl, int dims[MAX_DIMS]) { int i; for (i = 0; i < ndims; i++) { @@ -229,16 +213,14 @@ scan_dims(int ndims, YAP_Term tl, int dims[MAX_DIMS]) return TRUE; } -static YAP_Bool -scan_dims_args(int ndims, YAP_Term tl, int dims[MAX_DIMS]) -{ +static YAP_Bool 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); + th = YAP_ArgOfTerm(2 + i, tl); if (!YAP_IsIntTerm(th)) { /* ERROR */ return FALSE; @@ -253,9 +235,7 @@ scan_dims_args(int ndims, YAP_Term tl, int dims[MAX_DIMS]) return TRUE; } -static YAP_Bool -cp_int_matrix(YAP_Term tl,YAP_Term matrix) -{ +static YAP_Bool cp_int_matrix(YAP_Term tl, YAP_Term matrix) { int *mat = (int *)YAP_BlobOfTerm(matrix); int i, nelems = mat[MAT_SIZE]; long int *j = matrix_long_data(mat, mat[MAT_NDIMS]); @@ -283,9 +263,7 @@ cp_int_matrix(YAP_Term tl,YAP_Term matrix) return TRUE; } -static YAP_Bool -cp_float_matrix(YAP_Term tl,YAP_Term matrix) -{ +static YAP_Bool cp_float_matrix(YAP_Term tl, YAP_Term matrix) { int *mat = (int *)YAP_BlobOfTerm(matrix); int i, nelems = mat[MAT_SIZE]; double *j = matrix_double_data(mat, mat[MAT_NDIMS]); @@ -316,10 +294,7 @@ cp_float_matrix(YAP_Term tl,YAP_Term matrix) return TRUE; } - -static YAP_Bool -set_int_matrix(YAP_Term matrix,long int set) -{ +static YAP_Bool set_int_matrix(YAP_Term matrix, long int set) { int *mat = (int *)YAP_BlobOfTerm(matrix); int i, nelems = mat[MAT_SIZE]; long int *j = matrix_long_data(mat, mat[MAT_NDIMS]); @@ -330,9 +305,7 @@ set_int_matrix(YAP_Term matrix,long int set) return TRUE; } -static YAP_Bool -set_float_matrix(YAP_Term matrix,double set) -{ +static YAP_Bool set_float_matrix(YAP_Term matrix, double set) { int *mat = (int *)YAP_BlobOfTerm(matrix); int i, nelems = mat[MAT_SIZE]; double *j = matrix_double_data(mat, mat[MAT_NDIMS]); @@ -343,31 +316,27 @@ set_float_matrix(YAP_Term matrix,double set) return TRUE; } -static YAP_Bool -new_ints_matrix(void) -{ +static YAP_Bool new_ints_matrix(void) { int ndims = YAP_IntOfTerm(YAP_ARG1); YAP_Term tl = YAP_ARG2, out; int dims[MAX_DIMS]; - + if (!scan_dims(ndims, tl, dims)) return FALSE; out = new_int_matrix(ndims, dims, NULL); if (out == YAP_TermNil()) return FALSE; - if (!cp_int_matrix(YAP_ARG3,out)) + if (!cp_int_matrix(YAP_ARG3, out)) return FALSE; return YAP_Unify(YAP_ARG4, out); } -static YAP_Bool -new_ints_matrix_set(void) -{ +static YAP_Bool new_ints_matrix_set(void) { int ndims = YAP_IntOfTerm(YAP_ARG1); YAP_Term tl = YAP_ARG2, out, tset = YAP_ARG3; int dims[MAX_DIMS]; long int set; - + if (!YAP_IsIntTerm(tset)) { return FALSE; } @@ -375,14 +344,12 @@ new_ints_matrix_set(void) if (!scan_dims(ndims, tl, dims)) return FALSE; out = new_int_matrix(ndims, dims, NULL); - if (!set_int_matrix(out,set)) + if (!set_int_matrix(out, set)) return FALSE; return YAP_Unify(YAP_ARG4, out); } -static YAP_Bool -new_floats_matrix(void) -{ +static YAP_Bool new_floats_matrix(void) { int ndims = YAP_IntOfTerm(YAP_ARG1); YAP_Term tl = YAP_ARG2, out; int dims[MAX_DIMS]; @@ -391,19 +358,17 @@ new_floats_matrix(void) out = new_float_matrix(ndims, dims, NULL); if (out == YAP_TermNil()) return FALSE; - if (!cp_float_matrix(YAP_ARG3,out)) + if (!cp_float_matrix(YAP_ARG3, out)) return FALSE; return YAP_Unify(YAP_ARG4, out); } -static YAP_Bool -new_floats_matrix_set(void) -{ +static YAP_Bool new_floats_matrix_set(void) { int ndims = YAP_IntOfTerm(YAP_ARG1); YAP_Term tl = YAP_ARG2, out, tset = YAP_ARG3; int dims[MAX_DIMS]; double set; - + if (!YAP_IsFloatTerm(tset)) { return FALSE; } @@ -411,32 +376,29 @@ new_floats_matrix_set(void) if (!scan_dims(ndims, tl, dims)) return FALSE; out = new_float_matrix(ndims, dims, NULL); - if (!set_float_matrix(out,set)) + if (!set_float_matrix(out, set)) return FALSE; return YAP_Unify(YAP_ARG4, out); } -static YAP_Term -float_matrix_to_list(int *mat) { +static YAP_Term float_matrix_to_list(int *mat) { double *data = matrix_double_data(mat, mat[MAT_NDIMS]); /* prepare for worst case with double taking two cells */ - if (YAP_RequiresExtraStack(6*mat[MAT_SIZE])) { + if (YAP_RequiresExtraStack(6 * mat[MAT_SIZE])) { mat = (int *)YAP_BlobOfTerm(YAP_ARG1); data = matrix_double_data(mat, mat[MAT_NDIMS]); } return YAP_FloatsToList(data, mat[MAT_SIZE]); } -static YAP_Term -mk_int_list(int nelems, int *data) -{ +static YAP_Term mk_int_list(int nelems, int *data) { YAP_Term tn = YAP_TermNil(); YAP_Term tf = tn; int i = 0; - for (i = nelems-1; i>= 0; i--) { - tf = YAP_MkPairTerm(YAP_MkIntTerm(data[i]),tf); + for (i = nelems - 1; i >= 0; i--) { + tf = YAP_MkPairTerm(YAP_MkIntTerm(data[i]), tf); if (tf == tn) { /* error */ return tn; @@ -445,15 +407,13 @@ mk_int_list(int nelems, int *data) return tf; } -static YAP_Term -mk_int_list2(int nelems, int base, int *data) -{ +static YAP_Term mk_int_list2(int nelems, int base, int *data) { YAP_Term tn = YAP_TermNil(); YAP_Term tf = tn; int i = 0; - for (i = nelems-1; i>= 0; i--) { - tf = YAP_MkPairTerm(YAP_MkIntTerm(data[i]+base),tf); + for (i = nelems - 1; i >= 0; i--) { + tf = YAP_MkPairTerm(YAP_MkIntTerm(data[i] + base), tf); if (tf == tn) { /* error */ return tn; @@ -462,15 +422,13 @@ mk_int_list2(int nelems, int base, int *data) return tf; } -static YAP_Term -mk_rep_int_list(int nelems, int data) -{ +static YAP_Term mk_rep_int_list(int nelems, int data) { YAP_Term tn = YAP_TermNil(); YAP_Term tf = tn; int i = 0; - for (i = nelems-1; i>= 0; i--) { - tf = YAP_MkPairTerm(YAP_MkIntTerm(data),tf); + for (i = nelems - 1; i >= 0; i--) { + tf = YAP_MkPairTerm(YAP_MkIntTerm(data), tf); if (tf == tn) { /* error */ return tn; @@ -479,15 +437,13 @@ mk_rep_int_list(int nelems, int data) return tf; } -static YAP_Term -mk_long_list(int nelems, long int *data) -{ +static YAP_Term mk_long_list(int nelems, long int *data) { YAP_Term tn = YAP_TermNil(); YAP_Term tf = tn; int i = 0; - for (i = nelems-1; i>= 0; i--) { - tf = YAP_MkPairTerm(YAP_MkIntTerm(data[i]),tf); + for (i = nelems - 1; i >= 0; i--) { + tf = YAP_MkPairTerm(YAP_MkIntTerm(data[i]), tf); if (tf == tn) { /* error */ return tn; @@ -496,119 +452,97 @@ mk_long_list(int nelems, long int *data) return tf; } - -static YAP_Term -long_matrix_to_list(int *mat) { +static YAP_Term long_matrix_to_list(int *mat) { long int *data = matrix_long_data(mat, mat[MAT_NDIMS]); /* prepare for worst case with longs evrywhere (3cells + 1) */ - if (YAP_RequiresExtraStack(5*mat[MAT_SIZE])) { + if (YAP_RequiresExtraStack(5 * mat[MAT_SIZE])) { mat = (int *)YAP_BlobOfTerm(YAP_ARG1); data = matrix_long_data(mat, mat[MAT_NDIMS]); } return mk_long_list(mat[MAT_SIZE], data); } -static YAP_Term -matrix_access(int *mat, int *indx) -{ +static YAP_Term matrix_access(int *mat, int *indx) { unsigned int off = matrix_get_offset(mat, indx); - if (mat[MAT_TYPE]==FLOAT_MATRIX) - return YAP_MkFloatTerm((matrix_double_data(mat,mat[MAT_NDIMS]))[off]); + if (mat[MAT_TYPE] == FLOAT_MATRIX) + return YAP_MkFloatTerm((matrix_double_data(mat, mat[MAT_NDIMS]))[off]); else - return YAP_MkIntTerm((matrix_long_data(mat,mat[MAT_NDIMS]))[off]); + return YAP_MkIntTerm((matrix_long_data(mat, mat[MAT_NDIMS]))[off]); } -static void -matrix_float_set(int *mat, int *indx, double nval) -{ +static void matrix_float_set(int *mat, int *indx, double nval) { unsigned int off = 0; off = matrix_get_offset(mat, indx); - (matrix_double_data(mat,mat[MAT_NDIMS]))[off] = nval; + (matrix_double_data(mat, mat[MAT_NDIMS]))[off] = nval; } -static void -matrix_long_set(int *mat, int *indx, long int nval) -{ +static void matrix_long_set(int *mat, int *indx, long int nval) { unsigned int off = matrix_get_offset(mat, indx); - (matrix_long_data(mat,mat[MAT_NDIMS]))[off] = nval; + (matrix_long_data(mat, mat[MAT_NDIMS]))[off] = nval; } -static void -matrix_float_set_all(int *mat, double nval) -{ +static void matrix_float_set_all(int *mat, double nval) { int i; - double *data = matrix_double_data(mat,mat[MAT_NDIMS]); + double *data = matrix_double_data(mat, mat[MAT_NDIMS]); - for (i = 0; i< mat[MAT_SIZE]; i++) + for (i = 0; i < mat[MAT_SIZE]; i++) data[i] = nval; } -static void -matrix_long_set_all(int *mat, long int nval) -{ +static void matrix_long_set_all(int *mat, long int nval) { int i; - long int *data = matrix_long_data(mat,mat[MAT_NDIMS]); + long int *data = matrix_long_data(mat, mat[MAT_NDIMS]); if (nval == 0) { - memset((void *)data,0,sizeof(long int)*mat[MAT_SIZE]); + memset((void *)data, 0, sizeof(long int) * mat[MAT_SIZE]); } else { - for (i = 0; i< mat[MAT_SIZE]; i++) + for (i = 0; i < mat[MAT_SIZE]; i++) data[i] = nval; } } -static void -matrix_float_add(int *mat, int *indx, double nval) -{ +static void matrix_float_add(int *mat, int *indx, double nval) { unsigned int off; - double *dat = matrix_double_data(mat,mat[MAT_NDIMS]); + double *dat = matrix_double_data(mat, mat[MAT_NDIMS]); off = matrix_get_offset(mat, indx); dat[off] += nval; } -static void -matrix_long_add(int *mat, int *indx, long int nval) -{ - long int *dat = matrix_long_data(mat,mat[MAT_NDIMS]); +static void matrix_long_add(int *mat, int *indx, long int nval) { + long int *dat = matrix_long_data(mat, mat[MAT_NDIMS]); unsigned int off = matrix_get_offset(mat, indx); dat[off] += nval; } -static void -matrix_inc(int *mat, int *indx) -{ +static void matrix_inc(int *mat, int *indx) { unsigned int off = matrix_get_offset(mat, indx); - if (mat[MAT_TYPE]==FLOAT_MATRIX) - (matrix_double_data(mat,mat[MAT_NDIMS])[off])++; + if (mat[MAT_TYPE] == FLOAT_MATRIX) + (matrix_double_data(mat, mat[MAT_NDIMS])[off])++; else - ((matrix_long_data(mat,mat[MAT_NDIMS]))[off])++; + ((matrix_long_data(mat, mat[MAT_NDIMS]))[off])++; } -static void -matrix_dec(int *mat, int *indx) -{ +static void matrix_dec(int *mat, int *indx) { unsigned int off = matrix_get_offset(mat, indx); - if (mat[MAT_TYPE]==FLOAT_MATRIX) - (matrix_double_data(mat,mat[MAT_NDIMS])[off])--; + if (mat[MAT_TYPE] == FLOAT_MATRIX) + (matrix_double_data(mat, mat[MAT_NDIMS])[off])--; else - ((matrix_long_data(mat,mat[MAT_NDIMS]))[off])--; + ((matrix_long_data(mat, mat[MAT_NDIMS]))[off])--; } -static YAP_Term -matrix_inc2(int *mat, int *indx) -{ +static YAP_Term matrix_inc2(int *mat, int *indx) { unsigned int off = matrix_get_offset(mat, indx); - if (mat[MAT_TYPE]==FLOAT_MATRIX) { - double *data = matrix_double_data(mat,mat[MAT_NDIMS]); + if (mat[MAT_TYPE] == FLOAT_MATRIX) { + double *data = matrix_double_data(mat, mat[MAT_NDIMS]); double d = data[off]; d++; data[off] = d; return YAP_MkFloatTerm(d); } else { - long int *data = matrix_long_data(mat,mat[MAT_NDIMS]); + long int *data = matrix_long_data(mat, mat[MAT_NDIMS]); long int d = data[off]; d++; data[off] = d; @@ -616,18 +550,16 @@ matrix_inc2(int *mat, int *indx) } } -static YAP_Term -matrix_dec2(int *mat, int *indx) -{ +static YAP_Term matrix_dec2(int *mat, int *indx) { unsigned int off = matrix_get_offset(mat, indx); - if (mat[MAT_TYPE]==FLOAT_MATRIX) { - double *data = matrix_double_data(mat,mat[MAT_NDIMS]); + if (mat[MAT_TYPE] == FLOAT_MATRIX) { + double *data = matrix_double_data(mat, mat[MAT_NDIMS]); double d = data[off]; d--; data[off] = d; return YAP_MkFloatTerm(d); } else { - long int *data = matrix_long_data(mat,mat[MAT_NDIMS]); + long int *data = matrix_long_data(mat, mat[MAT_NDIMS]); long int d = data[off]; d--; data[off] = d; @@ -635,13 +567,10 @@ matrix_dec2(int *mat, int *indx) } } - -static YAP_Bool -matrix_set(void) -{ +static YAP_Bool matrix_set(void) { int dims[MAX_DIMS], *mat; YAP_Term tf; - + mat = (int *)YAP_BlobOfTerm(YAP_ARG1); if (!mat) { /* Error */ @@ -674,13 +603,11 @@ matrix_set(void) return TRUE; } -static YAP_Bool -matrix_set2(void) -{ +static YAP_Bool matrix_set2(void) { int dims[MAX_DIMS], *mat; YAP_Term tf, t = YAP_ARG1; - - mat = (int *)YAP_BlobOfTerm(YAP_ArgOfTerm(1,t)); + + mat = (int *)YAP_BlobOfTerm(YAP_ArgOfTerm(1, t)); if (!mat) { /* Error */ return FALSE; @@ -712,12 +639,10 @@ matrix_set2(void) return TRUE; } -static YAP_Bool -matrix_set_all(void) -{ +static YAP_Bool matrix_set_all(void) { int *mat; YAP_Term tf; - + mat = (int *)YAP_BlobOfTerm(YAP_ARG1); if (!mat) { /* Error */ @@ -746,12 +671,10 @@ matrix_set_all(void) return TRUE; } -static YAP_Bool -matrix_add(void) -{ +static YAP_Bool matrix_add(void) { int dims[MAX_DIMS], *mat; YAP_Term tf; - + mat = (int *)YAP_BlobOfTerm(YAP_ARG1); if (!mat) { /* Error */ @@ -784,12 +707,10 @@ matrix_add(void) return TRUE; } -static YAP_Bool -do_matrix_access(void) -{ +static YAP_Bool do_matrix_access(void) { int dims[MAX_DIMS], *mat; YAP_Term tf; - + mat = (int *)YAP_BlobOfTerm(YAP_ARG1); if (!mat) { /* Error */ @@ -803,12 +724,10 @@ do_matrix_access(void) return YAP_Unify(tf, YAP_ARG3); } -static YAP_Bool -do_matrix_access2(void) -{ +static YAP_Bool 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 */ @@ -822,11 +741,9 @@ do_matrix_access2(void) return YAP_Unify(tf, YAP_ARG2); } -static YAP_Bool -do_matrix_inc(void) -{ +static YAP_Bool do_matrix_inc(void) { int dims[MAX_DIMS], *mat; - + mat = (int *)YAP_BlobOfTerm(YAP_ARG1); if (!mat) { /* Error */ @@ -840,11 +757,9 @@ do_matrix_inc(void) return TRUE; } -static YAP_Bool -do_matrix_dec(void) -{ +static YAP_Bool do_matrix_dec(void) { int dims[MAX_DIMS], *mat; - + mat = (int *)YAP_BlobOfTerm(YAP_ARG1); if (!mat) { /* Error */ @@ -858,11 +773,9 @@ do_matrix_dec(void) return TRUE; } -static YAP_Bool -do_matrix_inc2(void) -{ +static YAP_Bool do_matrix_inc2(void) { int dims[MAX_DIMS], *mat; - + mat = (int *)YAP_BlobOfTerm(YAP_ARG1); if (!mat) { /* Error */ @@ -872,15 +785,12 @@ do_matrix_inc2(void) /* Error */ return FALSE; } - return - YAP_Unify(matrix_inc2(mat, dims), YAP_ARG3); + return YAP_Unify(matrix_inc2(mat, dims), YAP_ARG3); } -static YAP_Bool -do_matrix_dec2(void) -{ +static YAP_Bool do_matrix_dec2(void) { int dims[MAX_DIMS], *mat; - + mat = (int *)YAP_BlobOfTerm(YAP_ARG1); if (!mat) { /* Error */ @@ -890,16 +800,13 @@ do_matrix_dec2(void) /* Error */ return FALSE; } - return - YAP_Unify(matrix_dec2(mat, dims), YAP_ARG3); + return YAP_Unify(matrix_dec2(mat, dims), YAP_ARG3); } -static YAP_Bool -matrix_to_list(void) -{ +static YAP_Bool matrix_to_list(void) { int *mat; YAP_Term tf; - + mat = (int *)YAP_BlobOfTerm(YAP_ARG1); if (!mat) { /* Error */ @@ -912,11 +819,9 @@ matrix_to_list(void) return YAP_Unify(YAP_ARG2, tf); } -static YAP_Bool -matrix_set_base(void) -{ +static YAP_Bool matrix_set_base(void) { int *mat; - + mat = (int *)YAP_BlobOfTerm(YAP_ARG1); if (!mat) { /* Error */ @@ -926,43 +831,36 @@ matrix_set_base(void) return TRUE; } - -static YAP_Bool -matrix_dims(void) -{ +static YAP_Bool matrix_dims(void) { int *mat; YAP_Term tf; - + mat = (int *)YAP_BlobOfTerm(YAP_ARG1); if (!mat) { /* Error */ return FALSE; } - tf = mk_int_list(mat[MAT_NDIMS],mat+MAT_DIMS); + tf = mk_int_list(mat[MAT_NDIMS], mat + MAT_DIMS); return YAP_Unify(YAP_ARG2, tf); } -static YAP_Bool -matrix_dims3(void) -{ +static YAP_Bool matrix_dims3(void) { int *mat; YAP_Term tf, tof; - + mat = (int *)YAP_BlobOfTerm(YAP_ARG1); if (!mat) { /* Error */ return FALSE; } - tf = mk_int_list(mat[MAT_NDIMS],mat+MAT_DIMS); - tof = mk_rep_int_list(mat[MAT_NDIMS],mat[MAT_BASE]); + tf = mk_int_list(mat[MAT_NDIMS], mat + MAT_DIMS); + tof = mk_rep_int_list(mat[MAT_NDIMS], mat[MAT_BASE]); return YAP_Unify(YAP_ARG2, tf) && YAP_Unify(YAP_ARG3, tof); } -static YAP_Bool -matrix_size(void) -{ +static YAP_Bool matrix_size(void) { int *mat; - + mat = (int *)YAP_BlobOfTerm(YAP_ARG1); if (!mat) { /* Error */ @@ -971,11 +869,9 @@ matrix_size(void) return YAP_Unify(YAP_ARG2, YAP_MkIntTerm(mat[MAT_SIZE])); } -static YAP_Bool -matrix_ndims(void) -{ +static YAP_Bool matrix_ndims(void) { int *mat; - + mat = (int *)YAP_BlobOfTerm(YAP_ARG1); if (!mat) { /* Error */ @@ -984,12 +880,10 @@ matrix_ndims(void) return YAP_Unify(YAP_ARG2, YAP_MkIntTerm(mat[MAT_NDIMS])); } -static YAP_Bool -matrix_type(void) -{ +static YAP_Bool matrix_type(void) { int *mat; YAP_Term tf; - + mat = (int *)YAP_BlobOfTerm(YAP_ARG1); if (!mat) { /* not an error, it may be called on a term matrix */ @@ -1003,9 +897,7 @@ matrix_type(void) return YAP_Unify(YAP_ARG2, tf); } -static YAP_Bool -matrix_arg_to_offset(void) -{ +static YAP_Bool matrix_arg_to_offset(void) { int indx[MAX_DIMS], *mat; unsigned int off; @@ -1023,9 +915,7 @@ matrix_arg_to_offset(void) return YAP_Unify(YAP_ARG3, YAP_MkIntTerm(off)); } -static YAP_Bool -matrix_offset_to_arg(void) -{ +static YAP_Bool matrix_offset_to_arg(void) { int indx[MAX_DIMS], *mat; unsigned int off; YAP_Term ti, tf; @@ -1045,65 +935,55 @@ matrix_offset_to_arg(void) return YAP_Unify(YAP_ARG3, tf); } -static unsigned int -scan_max_long(int sz, long int *data) -{ - int i, off=0; - long int max= data[0]; - for (i=1; imax) { - off=i; +static unsigned int scan_max_long(int sz, long int *data) { + int i, off = 0; + long int max = data[0]; + for (i = 1; i < sz; i++) { + if (data[i] > max) { + off = i; max = data[i]; } } return off; } -static unsigned int -scan_max_float(int sz, double *data) -{ - int i, off=0; - double max= data[0]; - for (i=1; imax) { +static unsigned int scan_max_float(int sz, double *data) { + int i, off = 0; + double max = data[0]; + for (i = 1; i < sz; i++) { + if (data[i] > max) { max = data[i]; - off=i; + off = i; } } return off; } -static unsigned int -scan_min_long(int sz, long int *data) -{ - int i, off=0; - long int max= data[0]; - for (i=1; i max) max = data[i]; + for (i = 1; i < mat[MAT_SIZE]; i++) { + if (data[i] > max) + max = data[i]; } - - for (i=0; i< mat[MAT_SIZE]; i++) { - data[i] = exp(data[i]-max); + + for (i = 0; i < mat[MAT_SIZE]; i++) { + data[i] = exp(data[i] - max); } } return TRUE; } -static YAP_Bool -matrix_exp_all2(void) -{ +static YAP_Bool matrix_exp_all2(void) { int *mat; mat = (int *)YAP_BlobOfTerm(YAP_ARG1); @@ -1324,13 +1191,13 @@ matrix_exp_all2(void) if (!YAP_IsVarTerm(YAP_ARG2)) { out = YAP_ARG2; } else { - out = new_float_matrix(mat[MAT_NDIMS], mat+MAT_DIMS, NULL); + out = new_float_matrix(mat[MAT_NDIMS], mat + MAT_DIMS, NULL); if (out == YAP_TermNil()) - return FALSE; + return FALSE; } nmat = (int *)YAP_BlobOfTerm(out); ndata = matrix_double_data(nmat, mat[MAT_NDIMS]); - for (i=0; i< mat[MAT_SIZE]; i++) { + for (i = 0; i < mat[MAT_SIZE]; i++) { ndata[i] = exp((double)data[i]); } if (YAP_IsVarTerm(YAP_ARG2)) { @@ -1345,13 +1212,13 @@ matrix_exp_all2(void) if (!YAP_IsVarTerm(YAP_ARG2)) { out = YAP_ARG2; } else { - out = new_float_matrix(mat[MAT_NDIMS], mat+MAT_DIMS, NULL); + out = new_float_matrix(mat[MAT_NDIMS], mat + MAT_DIMS, NULL); if (out == YAP_TermNil()) - return FALSE; + return FALSE; } nmat = (int *)YAP_BlobOfTerm(out); ndata = matrix_double_data(nmat, mat[MAT_NDIMS]); - for (i=0; i< mat[MAT_SIZE]; i++) { + for (i = 0; i < mat[MAT_SIZE]; i++) { ndata[i] = exp(data[i]); } if (YAP_IsVarTerm(YAP_ARG2)) { @@ -1361,9 +1228,7 @@ matrix_exp_all2(void) return TRUE; } -static YAP_Bool -matrix_minarg(void) -{ +static YAP_Bool matrix_minarg(void) { int indx[MAX_DIMS], *mat; unsigned int off; YAP_Term tf; @@ -1385,9 +1250,7 @@ matrix_minarg(void) return YAP_Unify(YAP_ARG2, tf); } -static YAP_Bool -matrix_sum(void) -{ +static YAP_Bool matrix_sum(void) { int *mat; YAP_Term tf; @@ -1409,48 +1272,52 @@ matrix_sum(void) double *data = matrix_double_data(mat, mat[MAT_NDIMS]); int i; double sum = 0.0; - + // function KahanSum(input) + double c = 0.0; // A running compensation for lost low-order bits. for (i = 0; i < mat[MAT_SIZE]; i++) { - sum += data[i]; + double y = data[i] - c; // So far, so good: c is zero. + double t = + sum + + y; // Alas, sum is big, y small, so low-order digits of y are lost. + c = (t - sum) - y; // (t - sum) cancels the high-order part of y; + // subtracting y recovers negative (low part of y) + sum = t; // Algebraically, c should always be zero. Beware + // overly-aggressive optimizing compilers! } tf = YAP_MkFloatTerm(sum); } return YAP_Unify(YAP_ARG2, tf); } -static void -add_int_lines(int total,int nlines,long int *mat0,long int *matf) -{ - int ncols = total/nlines, i; - for (i=0;i prdim) { - d = d*dims[j]; + d = d * dims[j]; j--; } - dd = d*dims[prdim]; - memset(ndata, 0, sizeof(double)*nmat[MAT_SIZE]); - for (i=0; i< mat[MAT_SIZE]; i++) { - YAP_Int k = i % d + (i/dd)*d; + dd = d * dims[prdim]; + memset(ndata, 0, sizeof(double) * nmat[MAT_SIZE]); + for (i = 0; i < mat[MAT_SIZE]; i++) { + YAP_Int k = i % d + (i / dd) * d; ndata[k] += exp(data[i]); } - for (i=0; i< nmat[MAT_SIZE]; i++) { + for (i = 0; i < nmat[MAT_SIZE]; i++) { ndata[i] = log(ndata[i]); } } @@ -2909,10 +2719,8 @@ matrix_sum_out_logs(void) } /* given a matrix M and a set of dims, sum out one of the dimensions -*/ -static YAP_Bool -matrix_sum_out_logs_several(void) -{ + */ +static YAP_Bool matrix_sum_out_logs_several(void) { int ndims, i, *dims, newdims; int indx[MAX_DIMS], nindx[MAX_DIMS], conv[MAX_DIMS]; YAP_Term tf, tconv; @@ -2923,10 +2731,10 @@ matrix_sum_out_logs_several(void) return FALSE; } ndims = mat[MAT_NDIMS]; - dims = mat+MAT_DIMS; + dims = mat + MAT_DIMS; /* we now have our target matrix, let us grab our conversion arguments */ tconv = YAP_ARG2; - for (i=0, newdims=0; i < ndims; i++) { + for (i = 0, newdims = 0; i < ndims; i++) { YAP_Term th; if (!YAP_IsPairTerm(tconv)) @@ -2944,28 +2752,28 @@ matrix_sum_out_logs_several(void) long int *data, *ndata; /* create a new matrix with the same size */ - tf = new_int_matrix(newdims,nindx,NULL); + tf = new_int_matrix(newdims, nindx, NULL); if (tf == YAP_TermNil()) return FALSE; /* in case the matrix moved */ mat = (int *)YAP_BlobOfTerm(YAP_ARG1); - nmat = (int *)YAP_BlobOfTerm(tf); - data = matrix_long_data(mat,ndims); - ndata = matrix_long_data(nmat,newdims); + nmat = (int *)YAP_BlobOfTerm(tf); + data = matrix_long_data(mat, ndims); + ndata = matrix_long_data(nmat, newdims); /* create a new matrix with smaller size */ - for (i=0;i offset: + magic_res = self.magic_matches(text) + return text, magic_res self.matches = [] prolog_res = self.shell.yapeng.mgoal(completions(text, self), "user",True) - if self.matches: - return text, self.matches - magic_res = self.magic_matches(text) - return text, magic_res + return text, self.matches + @@ -643,6 +644,7 @@ class YAPRun: # variable names should match strings # ask = True # launch the query + cell = raw_cell # cell has to exist so it can be stored/logged info = interactiveshell.ExecutionInfo( @@ -679,7 +681,6 @@ class YAPRun: # cell = self.shell.input_transformer_manager.transform_cell(raw_cell) # except SyntaxError: # preprocessing_exc_tuple = self.shell.syntax_error() # sys.exc_info() - cell = raw_cell # cell has to exist so it can be stored/logged for i in self.errors: try: (_,lin,pos,text) = i @@ -707,31 +708,23 @@ class YAPRun: # compiler = self.shell.compile if shell_futures else CachingCompiler() self.cell_name = str( self.shell.execution_count) if cell[0] == '%': + txt0 = cell.split(maxsplit = 1, sep = '\n') if cell[1] == '%': linec = False - mcell = cell.lstrip('%%') + magic = txt[0].lstrip('%%').strip() + body = txt[1] + self.result = True, self.shell.run_cell_magic(magic, line, body) else: linec = True - mcell = cell.lstrip('%') - txt0 = mcell.split(maxsplit = 2, sep = '\n') + magic = cell.lstrip('%').strip() txt = txt0[0].split(maxsplit = 2) magic = txt[0] - if len(txt) == 2: - line = txt[1] - else: - line = "" - if linec: - self.shell.run_line_magic(magic, line) - else: - if len(txt0) == 1: - cell = "" - else: - body = txt0[1]+'\n'+txt0[2] - self.result = True, self.shell.run_cell_magic(magic, line, body) - return self.result + self.shell.run_line_magic(magic, line) # Give the displayhook a reference to our ExecutionResult so it # can fill in the output value. self.shell.displayhook.exec_result = self.result + if syntaxErrors(self, text): + self.result.result = False has_raised = False try: self.yapeng.mgoal(streams(True),"user", True) @@ -743,7 +736,7 @@ class YAPRun: # trace=1, # count=0) # - + # def f(self, cell, state): # state = self.jupyter_query( cell )