Terrence Brannon wrote:
>  I am surprised to find the Pike code more
>  readable for this particular problem.

I believe this is merely an artifact of your training.  You've written and read 
so much C (like) syntax now, that it appears "natural" to you. 

I do not believe someone ignorant of all programming languages would find the 
Pike more readable than the J.

That said:  J is an array language, and your problem is matrix muliplication.  
So it should be a clearly better fit.

But rememeber that J is a _rectangular_ array language; it does not handle 
ragged arrays well.  By ragged array, I mean items which differ in length.  In 
your example, the first item,  a  , is the result of single operation, rather 
than three, like the others.  

This is apparent even in the notation, as:

>  a =. ((0 { x) * (0 { y))

is shorter than:

>  b =. ((0 { x) * (1 { y)) + ((1 { x) * (3 { y)) 
>  c =. ((2 { x) * (0 { y)) + ((3 { x) * (2 { y)) 
>  d =. ((2 { x) * (1 { y)) + ((3 { x) * (3 { y))

Aside from that wrinkle, this is a simple matrix multiplication.  And, if we 
smooth the wrinkle, we can calculate it in one go with J:

           D    =. _2 ]\^:2: 0 0  _1 _1   0 1  1 3   2 0  3 2   2 1  3 3
           cmps =. [: +/@:(*/) 1 0 |: D {&>"1 ,&:<&:(,&0)

I smoothed the wrinkle by making the indicies rectangular; I filled the holes 
with  _1  , and then padded the input arrays with a trailing 0, which will not 
effect the overall result.

Other formulations of  D  are possible, and would simplify the definition of  
cmps  , but I thought the index array would be most recognizable in this format.

One last note:  J solutions are best when they can be derived from first 
principles.  Converting an array problem into a scalar language, then asking us 
to convert that back to an array solution, is a little like translating an 
English word problem to Chinese and asking us to give an answer in English.  

In fact, if you show us the original problem, we might be able to generalize 
the solution for you.

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

Reply via email to