I have long thought that J would be easier received by students who had 
never learned a scalar programming language.

Now that I teach highschool, I have had the chance to test that idea. 
Last year I took two very strong students and had them work on 
individual projects using J.  One of them used the year to rewrite the 
water-quality model that the State of North Carolina uses for approving 
water projects - so now a J application is used for that.  Cool!  So far 
I consider my hypothesis to be confirmed.

For this upcoming year I will have one student, and he is a true genius. 
  A real mathematician through and through.  We agreed at the end of the 
last school year that we would work together this coming year.  At the 
beginning of July he downloaded J, and then, when he wasn't going off to 
some camp or other, he worked at it a little off and on.  A couple of 
weeks ago he sent me what he had come up with.

I was flabbergasted, and you will be too when you see the code at the 
end of this message.  The problem he chose to work on was collision of 
moving spherical bodies in n-dimensional space.

Not only is he a mathematician, he has the makings of a programmer. 
When I sent him back a shorter version of his collision code, he replied,

   I think my coding is pretty comprehensible...

So take that, those of you who say J is hard to read!

Here it is, Junior's code, the code of a 16-year-old with no programming 
experience and two months' self-study in J.  This is what he sent me, 
with no alterations or suggestions by me.  You would have to undo the 
line-wraps if you wanted to study it carefully.  Note the sophisticated 
ideas he uses.  This shows, to me, what could happen if you approached 
the language on its own terms without trying to make it seem like other 
languages.  And if you were as brilliant as Junior.

Henry Rich



NB. Points are rank 2 arrays formed by positions ,: velocities
NB. so the shape is 2 , number of dimensions.
NB. All verbs work for n dimensional points.
NB. Points are treated as the centers of radius 1 spheres.

NB. Bounds are a rank 2 array formed by laminating the 2 element
NB. lists of max and min values for each dimension, so their axes
NB. must be reversed to correspond to points.

    sampbound=:3 2$0 30 10 40 20 50  NB. 0<x<30,10<y<40,20<z<50
    tb1=.(((- {.) % {:@])"1 |:)"2
    tb=:<./@:(#~ 0&<)"1...@tb1 f.   NB. gives the times each point will hit 
the bounds

    rp=:(({."1 + ?@:(0 $~ #) * -~/"1) ,: <:@+:@?@:(0 $~ #))"2 NB. 
generates a random point within the given bounds (right argument)
    gen=:rp@:($ ,:)             NB. Generates x points within bounds y 
which may or may not intersect
    dist=:(+/&.:*:)@:({.@:-)"2  NB. Distance between two points x and y
    test=:(0 = +/)@:((2 > dist/~) * </~@:(((<0 1) { ])"2)) # ]  NB. 
Removes all intersections between points

    pos=:(+/ ,: {:)"2 : (({...@] + (* {:)) ,: {:@])"2 NB. position of 
point y at time x, default 1
    a=.+/@:*:@{:
    b=.+/@:(*/)
    c=.+/@:*:@{.             NB. distance between two points at time t 
is %:@:(+/)@:((t ^ (i._3)) * (a , +:@:b , c))
NB. The following verbs operate on two points as right and left 
arguments and give data pertaining to the right one.
    t1=.((a %~ -@:b (+ , -) %:@:(*:@:b + a * 4 - c))@:-)"2 NB. times at 
which two spheres will intersect-may be negative or complex
    t=:<./@:(#~ 1 = *)@t1 f. NB. first positive real time of collision, 
otherwise _
    col=:(((0 > b % a) * (0 <: *:@:b + a * 4 - c))@:-)"2 f. NB. 
determines if two spheres will collide
    t2=:_"[email protected]           NB. gives the same result as t but is 
faster for points that do not collide
    pts=:{."2@(t pos ,:)     NB. gives position of point x ,: position 
of point y at time of collision
    m=.(% {.)@:(-/@:pts)     NB. vector with the same direction as the 
line between both point at the time of collision
    x1=.%/@:(+/@:(m * {:@] ,. m))
    a1=.x1 * m
    colv=:({:@] - a1 - a1~) f.   NB. velocity of point y after collision
    collide=:(colv ,:~ {:@:pts + colv * -.@:t) f. NB. point y after 1 
time unit, taking into account collision with x

    cola=:({.@:(t pos ]) ,: colv)"2 NB. point y immediately after collision
    colp=.(cola~ ,: cola)"2/        NB. operates on two points x,:y and 
gives positions immediately after collision
NB. These verbs operate on a rank three array of points, possibly with 
bounds as a left argument.
    ttab=:t2/~ : (t2/~...@] ,. tb)     NB. gives a table of collision 
times, also with bounds if bounds are given.
    tt=.<./@,@:ttab                 NB. gives time of the first collision
    i=.I.@:(+/"1@:(tt = ttab))      NB. gives the indices of the points 
that are colliding
    ptt=.<@:(i , 1 , (tt i."1~ [ tb i { ]))       NB. if a point is 
colliding with the bounds, gives the position of the velocity that will 
be reversed.
    mcolb=:(ptt { -...@])`(ptt)`(tt pos ]) } f.      NB. calculates the 
positions of points up to the first collision with bounds, not 
considering point collisions.
    mcolp=:i (((colp@:{)`[`((pos~ tt)@])) }) ] f. NB. calculates the 
position of its argument up to the next collision, does not consider bounds.
    mcol=:1e_10 pos ((mcolb`(mc...@]))@.(<:@#...@i.@:(+/@:(tt = ttab)))) 
NB. calculates position of arguments to next collision, counting bounds, 
with a small offset to avoid rounding error.



DIETER ENSSLEN wrote:
> thanks
> 
> I have never yet met a language as difficult as this, where I try to copy a 
> string of to me still totally meaningless symbols again and again and again 
> and still get errors
> 
> I hope to find compensating rewards
> 
> Dick
> ----------------------------------------------------------------------
> 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