I just read this thread of last October.

  The following phrase based on Take is type independent, accepts 
duplicates and fill elements. It does not "manipulate" data with 
* or /: or move it around with $ . It also handles an empty argument. 
Concise enough? a matter of compromise.

   diagonal =: ( {."_1~ -@:>:@:i.@:# )

   diagonal  99 1x1 0 _1j1 99
99       0 0    0  0
 0 2.71828 0    0  0
 0       0 0    0  0
 0       0 0 _1j1  0
 0       0 0    0 99

   <diagonal 'a b c d '  NB. suitable for text-diamond 2009/04/17
+--------+
|a       |
|        |
|  b     |
|        |
|    c   |
|        |
|      d |
|        |
+--------+

   diagonal (<s: ' tra'),<"0] 99 1x1 0 _1j1 99
+----+--+-------+-+----+--+
|`tra|  |       | |    |  |
+----+--+-------+-+----+--+
|    |99|       | |    |  |
+----+--+-------+-+----+--+
|    |  |2.71828| |    |  |
+----+--+-------+-+----+--+
|    |  |       |0|    |  |
+----+--+-------+-+----+--+
|    |  |       | |_1j1|  |
+----+--+-------+-+----+--+
|    |  |       | |    |99|
+----+--+-------+-+----+--+

   diagonal a:,3#<+`-
++-----+-----+-----+
||     |     |     |
++-----+-----+-----+
||+-+-+|     |     |
|||+|-||     |     |
||+-+-+|     |     |
++-----+-----+-----+
||     |+-+-+|     |
||     ||+|-||     |
||     |+-+-+|     |
++-----+-----+-----+
||     |     |+-+-+|
||     |     ||+|-||
||     |     |+-+-+|
++-----+-----+-----+

   $diagonal ''
0 0
  
  The lower triangle can be filled with a constant fill atom 
(type dependent): 

   ( {.!.'.'"_1~ -@:>:@:i.@:# )'abcdef'
a     
.b    
..c   
...d  
....e 
.....f
   or with a variable fill atom (but not tacitly in this form I think):
   '*'( 4 : '(->:i.# y) {.!.x "_1 y' ) 'abcdef'
a     
*b    
**c   
***d  
****e 
*****f

 ~ Gilles

---------- Original Message -----------
From: "Raul Miller" <[email protected]>
To: "Programming forum" <[email protected]>
Sent: Sat, 11 Oct 2008 14:40:18 -0400
Subject: [Jprogramming] diagonal arrays

> Given a vector, put it on the diagonal of an array:
> 
>    (<0 1)&|:inv 2 7 9 20
> |index error
>    (+/ .*(e.(<0 1 2 )&|:)@i.@(3##)) 2 7 9 20
> 2 0 0  0
> 0 7 0  0
> 0 0 9  0
> 0 0 0 20
>    (, $~ 2##)@(,"0 1*&0)2 7 9 20
> 2 0 0  0
> 0 7 0  0
> 0 0 9  0
> 0 0 0 20
> 
> Are there more concise approaches, that work?
> 
> Thanks,
> 
> -- 
> Raul
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
------- End of Original Message -------

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

Reply via email to