Hi,
In light of the recent discussion on precision and rounding error, I have a
concrete example of a problem I'm having with rounding error (at least
that's where I think the problem lies).
The goal is to write a cost function for a polynomial evaluation so that any
result outside [-1,1] is penalised. The work is part of an implementation
of a genetic algorithm.
Here is a stripped down C function which I'm using as the basis for my J
implementation (plus a sample vector for evaluation):
====
#include "stdio.h"
double evaluate (int, double[]);
int main() {
double v1[]={54.735, 31.069, -79.616, -22.279, -76.028, -42.021, -34.925,
-44.066, -53.706};
printf ("evaluate: %f\n", evaluate(9, v1));
return;
}
double evaluate(int D, double tmp[]) {
int const M=60;
int i, j;
double px, x, dx, result=0.0;
x = -1.0;
dx = 2.0/(double)M;
for (i=0; i<=M; i++) {
px = tmp[0];
for (j=1; j<D; j++) {
px = x*px + tmp[j];
}
if (px<-1.0 || px>1.0) result+=(1.0-px)*(1.0-px);
x+=dx;
}
return result;
}
===
Here is the equivalent J code:
v1 =: 54.735 31.069 _79.616 _22.279 _76.028 _42.021 _34.925 _44.066 _53.706
dxVect =: +/\ _1, 60 $ 2 % 60
calcCost =: [: +/ [: *: ] * _1&> + 1&<
When I run the J code (J601 o beta) I get:
calcCost (|. v1) p. dxVect
665922
whereas running the C program gives me:
evaluate: 676667.956501
The problem seems to be with calcCost, and I was wondering if there was a
different way of formulating that function to reduce the discrepancy in the
two results.
Many thanks,
Ronan
--
Ronan Reilly
NUI Maynooth
t: +353-1-7083847
e: [EMAIL PROTECTED]
w: http://www.cs.nuim.ie; http://cortex.cs.nuim.ie
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm