Help / Phrases / 5. C. Special Matrices & Lists

From J Wiki
Jump to navigation Jump to search


>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help Phrases


5C. Special Matrices & Lists

Although these phrases are intended primarily for numeric matrices, many apply as well to arrays of characters, and to arrays of rank other than 2. For example:

   m3=: 1: j. # ;. _1
   u=: 1 0 0 0 1 1 0 1 
   m3 u
1j3 1 1j1 1

   d4=: m3@[ # ]
   u d4 'abcd'
a   bc d

   u d4 i.4 5
 0  1  2  3  4
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
 5  6  7  8  9
10 11 12 13 14
 0  0  0  0  0
15 16 17 18 19

   ]cm=: 3 3$'ABCDEFGHI'
ABC
DEF
GHI
   <"2 d5 cm
+--------+
|EF|DF|DE|
|HI|GI|GH|
+--+--+--|
|BC|AC|AB|
|HI|GI|GH|
+--+--+--|
|BC|AC|AB|
|EF|DF|DE|
+--------+

   m42 cm
+-----------+
|ADG|BEH|CFI|
+-----------+

   (>@m42 ; |:) cm	                 NB. Open of boxed columns is the transpose
+-------+
|ADG|ADG|
|BEH|BEH|
|CFI|CFI|
+-------+

   (] ; 0&d12 ; _1 0 1&d12) m=: i. 4 4	 NB. Band matrices
+-------------------------------+
| 0  1  2  3|0 0  0  0|0 1  0  0|
| 4  5  6  7|0 5  0  0|4 5  6  0|
| 8  9 10 11|0 0 10  0|0 9 10 11|
|12 13 14 15|0 0  0 15|0 0 14 15|
+-------------------------------+

Several equivalent phrases are provided for a number of cases to illustrate various approaches to the same problem. For example, m13 uses self-classification to produce an identity matrix, and m14 uses an equals table. No one solution should be considered to be the "best". If space or time required in execution is of prime concern, then the tools provided in Section 14A (Execution Time and Space) should be used to evaluate the different methods.

d0 =: $, Reshape as in APL
d1 =: $,: x copies of y
d2 =: -@[ [\ ] x-column matrix of the items of y
m3 =: 1: j. # ;. _1 Real 1; Imaginary # of 0's between 1's
d4 =: m3@[ # ] Expand
d5 =: 1&(|:\.)"2^:2 Minors [5, s.v. 'Outfix'] (e.g. <"2 d5 i. 3 3)
d6 =: <&.>&.>@{@(i.&.>"_)@$ { ] Minors [4, sect. 3.3]
m7 =: (<0 1)&|:Diagonal
m8 =: i.@# {"_1 ] Diagonal
m9 =: =@i.@# #&, ] Diagonal (tables only)
m10=: <"1@(,"0~)@i.@# { ] Diagonal
d11=: ir=: i.@#@] Indices of items of right argument
d12=: [(]*+./@((,@[+"0 1 ir)=/ir))] Band matrix (1 0 _1 d12 i. 5 5)
m13=: m14@# Identity matrix of order of # of items of y
m14=: id=: =@i. Identity matrix of order y
m15=: =/~@i. "
m16=: ,~ $ {.&1@>: "
m17=: -@>:@i. {."0 1: "
m18=: -@i. |."0 1 {.&1 "
m19=: ,&1@($&0)"0@i. "
m20=: 0&,^:(i.`1:) " (but not for y=0)
m21=: #:@(2&^)@i.@- "
m22=: [ D.1@i. "
m23=: slt=: >/~@i. Strict lower triangle
m24=: lt=: >:/~@i. Lower triangle
m25=: [\@($&1) "
m26=: +./\@=@i. "
m27=: >:@i. $"0 1: "
m28=: >:@i. {."0 1 $&1 "
m29=: i.@- }."0 1 $&1 "
m30=: 1&,^:(i.`1:) " (but not for y=0)
m31=: i.@- |.!.0"0 1 $&1 "
m32=: #:@(+/\)@(2&^)@i.@- "
m33=: ut=: -.@slt Upper triangle
m34=: +./\"1@=@i. " but not for y=0
m35=: |.!.0^:(i.`($&1)) " but not for y=0
m36=: -@i. |.!.0"0 1 $&1 "
m37=: #:@(+/\.)@(2&^)@i.@- "
m38=: [:%. ,~ $ {.&1 _1@>: "
m39=: ]\ %@(i.&.<:@+:) Hilbert matrix but not for y=0
a40=: lor=: [^: 1 lor gives left; 0 lor gives right
m41=: -.@(' '&E.) # ] Remove multiple blanks
m42=: <"1@|: Box each column of a matrix
m43=: i.@{:@$ <@:({"1)"0 _ ]"
m44=: [:, (i.@2: ,: #,1:)<@,;.3] "
d45=: <@|:;.1 |: Box columns
n46=: 1 0 1 0 0 1 d45 i.3 6 Box columns 0 1, 2 3 4, and 5
v47=: {.;}. Split first from rest; split y at x
d48=: i.~ ({.;}.@}.) ] Split y at first x, eliding first x
m49=: }:;{: Split rest from last
c50=: 2 : 'x@{. , y@}.' f c50 g applies f to first item; g to others
m51=: */~@i. Multiplication table of order y
m52=: >./~@i. Maximum table of order y
m53=: ] ]\ ([: <: +:) {. (- {. 1:) Counterdiagonal matrix of order y
m54=: [: *./@, lt@# >: lnz Is y lower triangular?
m55=: [: -. 0: e. [: , ] Is y zero-free?
m56=: lnz=: ] ~: 0: Locate nonzeros in y
m57=: [: *./@, ut@# >: lnz Is y upper triangular?
m58=: ~:/~@i. Nondiagonal matrix of order y
d59=: ;@([ +&.> <@i."0@]) i{x + i. i{y
m60=: -/\@i. Alternating series length y
m61=: +/\@i. First y triangular numbers




>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help Phrases