add log/2 and exp/2

This commit is contained in:
Vítor Santos de Costa 2008-10-22 00:40:15 +01:00
parent 7eeaf24db2
commit b3cb7b1071
2 changed files with 78 additions and 0 deletions

View File

@ -71,6 +71,8 @@ typedef enum {
matrix_agg_cols/3,
matrix_to_logs/1,
matrix_to_exps/1,
matrix_to_logs/2,
matrix_to_exps/2,
matrix_op/4,
matrix_op_to_all/4,
matrix_op_to_lines/4,

View File

@ -1035,6 +1035,43 @@ matrix_log_all(void)
return TRUE;
}
static int
matrix_log_all2(void)
{
int *mat;
mat = (int *)YAP_BlobOfTerm(YAP_ARG1);
if (!mat) {
/* Error */
return FALSE;
}
if (mat[MAT_TYPE] == INT_MATRIX) {
return FALSE;
} else {
YAP_Term out;
double *data = matrix_double_data(mat, mat[MAT_NDIMS]), *ndata;
int i;
int *nmat;
if (!YAP_IsVarTerm(YAP_ARG2)) {
out = YAP_ARG2;
} else {
out = new_float_matrix(mat[MAT_NDIMS], mat+MAT_DIMS, NULL);
if (out == YAP_TermNil())
return FALSE;
}
nmat = (int *)YAP_BlobOfTerm(out);
ndata = matrix_double_data(nmat, mat[MAT_NDIMS]);
for (i=0; i< mat[MAT_SIZE]; i++) {
ndata[i] = log(data[i]);
}
if (YAP_IsVarTerm(YAP_ARG2)) {
return YAP_Unify(YAP_ARG2, out);
}
}
return TRUE;
}
static int
matrix_exp_all(void)
{
@ -1058,6 +1095,43 @@ matrix_exp_all(void)
return TRUE;
}
static int
matrix_exp_all2(void)
{
int *mat;
mat = (int *)YAP_BlobOfTerm(YAP_ARG1);
if (!mat) {
/* Error */
return FALSE;
}
if (mat[MAT_TYPE] == INT_MATRIX) {
return FALSE;
} else {
YAP_Term out;
double *data = matrix_double_data(mat, mat[MAT_NDIMS]), *ndata;
int i;
int *nmat;
if (!YAP_IsVarTerm(YAP_ARG2)) {
out = YAP_ARG2;
} else {
out = new_float_matrix(mat[MAT_NDIMS], mat+MAT_DIMS, NULL);
if (out == YAP_TermNil())
return FALSE;
}
nmat = (int *)YAP_BlobOfTerm(out);
ndata = matrix_double_data(nmat, mat[MAT_NDIMS]);
for (i=0; i< mat[MAT_SIZE]; i++) {
ndata[i] = exp(data[i]);
}
if (YAP_IsVarTerm(YAP_ARG2)) {
return YAP_Unify(YAP_ARG2, out);
}
}
return TRUE;
}
static int
matrix_minarg(void)
{
@ -2810,6 +2884,8 @@ init_matrix(void)
YAP_UserCPredicate("matrix_select", matrix_select, 4);
YAP_UserCPredicate("matrix_to_logs", matrix_log_all,1);
YAP_UserCPredicate("matrix_to_exps", matrix_exp_all, 1);
YAP_UserCPredicate("matrix_to_logs", matrix_log_all2,2);
YAP_UserCPredicate("matrix_to_exps", matrix_exp_all2, 2);
YAP_UserCPredicate("matrix_sum_out", matrix_sum_out, 3);
YAP_UserCPredicate("matrix_sum_out_several", matrix_sum_out_several, 3);
YAP_UserCPredicate("matrix_sum_logs_out", matrix_sum_out_logs, 3);