allow inplace operations.

This commit is contained in:
Vitor Santos Costa 2009-05-26 10:48:45 -05:00
parent 3c57286531
commit de7474a5d9
2 changed files with 53 additions and 21 deletions

View File

@ -53,6 +53,7 @@ typedef enum {
matrix_add/3, matrix_add/3,
matrix_inc/2, matrix_inc/2,
matrix_dec/2, matrix_dec/2,
matrix_mult/2,
matrix_inc/3, matrix_inc/3,
matrix_dec/3, matrix_dec/3,
matrix_arg_to_offset/3, matrix_arg_to_offset/3,

View File

@ -1714,7 +1714,8 @@ matrix_op(void)
int *mat1, *mat2; int *mat1, *mat2;
YAP_Term top = YAP_ARG3; YAP_Term top = YAP_ARG3;
op_type op; op_type op;
YAP_Term tf; YAP_Term tf = YAP_ARG4;
int create = TRUE;
if (!YAP_IsIntTerm(top)) { if (!YAP_IsIntTerm(top)) {
return FALSE; return FALSE;
@ -1730,6 +1731,9 @@ matrix_op(void)
/* Error */ /* Error */
return FALSE; return FALSE;
} }
if (tf == YAP_ARG1 || tf == YAP_ARG2) {
create = FALSE;
}
if (mat1[MAT_TYPE] == INT_MATRIX) { if (mat1[MAT_TYPE] == INT_MATRIX) {
long int *data1; long int *data1;
int dims = mat1[MAT_NDIMS]; int dims = mat1[MAT_NDIMS];
@ -1740,6 +1744,7 @@ matrix_op(void)
long int *data2; long int *data2;
long int *ndata; long int *ndata;
if (create)
tf = new_int_matrix(dims,mat1+MAT_DIMS,NULL); tf = new_int_matrix(dims,mat1+MAT_DIMS,NULL);
if (tf == YAP_TermNil()) { if (tf == YAP_TermNil()) {
return FALSE; return FALSE;
@ -1775,6 +1780,7 @@ matrix_op(void)
double *data2; double *data2;
double *ndata; double *ndata;
if (create)
tf = new_float_matrix(dims,mat1+MAT_DIMS,NULL); tf = new_float_matrix(dims,mat1+MAT_DIMS,NULL);
if (tf == YAP_TermNil()) { if (tf == YAP_TermNil()) {
return FALSE; return FALSE;
@ -1819,6 +1825,7 @@ matrix_op(void)
long int *data2; long int *data2;
double *ndata; double *ndata;
if (create)
tf = new_float_matrix(dims,mat1+MAT_DIMS,NULL); tf = new_float_matrix(dims,mat1+MAT_DIMS,NULL);
if (tf == YAP_TermNil()) { if (tf == YAP_TermNil()) {
return FALSE; return FALSE;
@ -1854,6 +1861,7 @@ matrix_op(void)
double *data2; double *data2;
double *ndata; double *ndata;
if (create)
tf = new_float_matrix(dims,mat1+MAT_DIMS,NULL); tf = new_float_matrix(dims,mat1+MAT_DIMS,NULL);
if (tf == YAP_TermNil()) { if (tf == YAP_TermNil()) {
return FALSE; return FALSE;
@ -2006,9 +2014,10 @@ static int
matrix_op_to_all(void) matrix_op_to_all(void)
{ {
int *mat; int *mat;
YAP_Term tf; YAP_Term tf = 0;
YAP_Term top = YAP_ARG2; YAP_Term top = YAP_ARG2;
op_type op; op_type op;
int create = FALSE;
if (!YAP_IsIntTerm(top)) { if (!YAP_IsIntTerm(top)) {
return FALSE; return FALSE;
@ -2019,6 +2028,9 @@ matrix_op_to_all(void)
/* Error */ /* Error */
return FALSE; return FALSE;
} }
if (YAP_IsVarTerm(YAP_ARG4)) {
create = TRUE;
}
/* create a new array with same dimensions */ /* create a new array with same dimensions */
if (mat[MAT_TYPE] == INT_MATRIX) { if (mat[MAT_TYPE] == INT_MATRIX) {
long int *data; long int *data;
@ -2031,12 +2043,17 @@ matrix_op_to_all(void)
long int *ndata; long int *ndata;
num = YAP_IntOfTerm(tnum); num = YAP_IntOfTerm(tnum);
data = matrix_long_data(mat, dims);
if (create) {
tf = new_int_matrix(dims,mat+(MAT_DIMS),NULL); tf = new_int_matrix(dims,mat+(MAT_DIMS),NULL);
if (tf == YAP_TermNil()) if (tf == YAP_TermNil())
return FALSE; return FALSE;
nmat = (int *)YAP_BlobOfTerm(tf); nmat = (int *)YAP_BlobOfTerm(tf);
data = matrix_long_data(mat, dims);
ndata = matrix_long_data(nmat, dims); ndata = matrix_long_data(nmat, dims);
} else {
nmat = mat;
ndata = data;
}
if (op == MAT_PLUS) { if (op == MAT_PLUS) {
int i; int i;
@ -2056,13 +2073,18 @@ matrix_op_to_all(void)
double num; double num;
double *ndata; double *ndata;
num = YAP_FloatOfTerm(tnum); num = YAP_FloatOfTerm(tnum);
if (create) {
tf = new_float_matrix(dims,mat+(MAT_DIMS),NULL); tf = new_float_matrix(dims,mat+(MAT_DIMS),NULL);
if (tf == YAP_TermNil()) if (tf == YAP_TermNil())
return FALSE; return FALSE;
nmat = (int *)YAP_BlobOfTerm(tf); nmat = (int *)YAP_BlobOfTerm(tf);
data = matrix_long_data(mat, dims);
ndata = matrix_double_data(nmat, dims); ndata = matrix_double_data(nmat, dims);
} else {
return FALSE;
}
data = matrix_long_data(mat, dims);
if (op == MAT_PLUS) { if (op == MAT_PLUS) {
int i; int i;
@ -2097,14 +2119,21 @@ matrix_op_to_all(void)
} else if (!YAP_IntOfTerm(tnum)) { } else if (!YAP_IntOfTerm(tnum)) {
return FALSE; return FALSE;
} else { } else {
if (!create)
return FALSE;
num = (double)YAP_IntOfTerm(tnum); num = (double)YAP_IntOfTerm(tnum);
} }
data = matrix_double_data(mat, dims);
if (create) {
tf = new_float_matrix(dims,mat+(MAT_DIMS),NULL); tf = new_float_matrix(dims,mat+(MAT_DIMS),NULL);
if (tf == YAP_TermNil()) if (tf == YAP_TermNil())
return FALSE; return FALSE;
nmat = (int *)YAP_BlobOfTerm(tf); nmat = (int *)YAP_BlobOfTerm(tf);
data = matrix_double_data(mat, dims);
ndata = matrix_double_data(nmat, dims); ndata = matrix_double_data(nmat, dims);
} else {
nmat = mat;
ndata = data;
}
switch(op) { switch(op) {
case MAT_PLUS: case MAT_PLUS:
{ {
@ -2137,7 +2166,9 @@ matrix_op_to_all(void)
return FALSE; return FALSE;
} }
} }
if (create)
return YAP_Unify(YAP_ARG4,tf); return YAP_Unify(YAP_ARG4,tf);
return YAP_Unify(YAP_ARG4,YAP_ARG1);
} }
/* given a matrix M and a set of dims, build a new reordered matrix to follow /* given a matrix M and a set of dims, build a new reordered matrix to follow