You need to do the 1-px thing like so: calcCost =: [: +/ [: *: -. * _1&> + 1&<
and then, if you want to see the result in full precision, you have to ask for it: 15j6 ": calcCost (|. v1) p. dxVect 676667.956501 The default display doesn't show the lower digits. It doesn't seem right to me to look for values outside of [0,1] and then square the distance from 1. Shouldn't you square the distance from 1 if it's >1, and square the distance from _1 if it's <_1? That is, -.@:| instead of -. above Henry Rich > -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Ronan Reilly > Sent: Tuesday, June 27, 2006 8:17 PM > To: Programming forum > Subject: [Jprogramming] Precision query > > 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 ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
