A question for Oleg - forgive me if I am incorrect in raising this as I do
not feel so experienced to query J code as yet ...

 

Oleg, I think your solution has introduced a small, subtle bug compared to
Henry or Raul's, as your test 1&< is based on (1-px)^2, instead of on px.

 

Ronan's data (v1) does not produce any px's in the test range _1<:px<:1, so
your solution agrees with Henry/Raul in this instance.

   px =: (|. v1) p. (%~ i:) 30              NB. Ronan's px data from 60, v1

   (<./ , >./) px                           NB. Range of px values

_190.837 _30.0942                           NB. None in range _1<:px<:1

 

   oleg =:  3 : '+/ (* 1&<) *:-. y'

   henry =: [: +/ [: *: -. * _1&> + 1&<

   raul =:  [: +/ [: *: (1-]) * 1 < |

   0j6":(oleg , henry , raul) px

676667.956501 676667.956501 676667.956501   NB. No problems for Ronan's data

 

However, if the px's were to contain values within _1<:px<:1 ...

   px1 =: _2 _0.7 _0.3 0.3 0.7 2            NB. Make up test data

   (oleg , henry , raul) px1

13.58 10 10

 

The difference being the test on px or (1-px)^2 for px, between _1 and 0...

      > px1 ; ((* 1&<)*:-. px1) ; ([: *: -. * _1&> +. 1&<) px1

_2 _0.7 _0.3 0.3 0.7 2        NB. px1 made up test data

 9 2.89 1.69   0   0 0        NB. Oleg's test based on 1<(1-px1)^2

 9    0    0   0   0 1        NB. Henry/Raul's test based on _1<:px1<:1

 

Is this a correct analysis ?.../Rob Hodgkinson

 

PS: Written in Courier, but my emails seem to be transformed upon sending,
apologies if this does not display nicely.

 

-----Original Message-----

From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Oleg Kobchenko

Sent: Wednesday, 28 June 2006 4:49 PM

To: Programming forum

Subject: Re: [Jprogramming] Precision query

 

   0j6": +/ (* 1&<) *:-. (|. v1) p. (%~ i:) 30

676667.956501

 

 

----- Original Message ----

From: Henry Rich <[EMAIL PROTECTED]>

To: Programming forum <[email protected]>

Sent: Tuesday, June 27, 2006 9:56:31 PM

Subject: RE: [Jprogramming] Precision query

 

 

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

----------------------------------------------------------------------

For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to