Using the utilities below you could do the following:

makeSym > vec2LowerTri 0 1.5 0 2 1.5 0 2.5 2 1.5 0

0 1.5 2 2.5

1.5 0 1.5 2

2 1.5 0 1.5

2.5 2 1.5 0


NB.*zeroTri a Zeros triangular items of matrix determined by verb to left
NB. EG: < zeroTri mat   NB. zeros lower-tri items of mat
NB. EG: <: zeroTri mat  NB. zeros lower-tri, off-diag items of mat
NB. EG: > zeroTri mat   NB. zeros upper tri items of mat
NB. EG: = zeroTri mat   NB. zeros all off-diag items of mat
NB. EG: ~: zeroTri mat  NB. zeros diag items of mat
NB. EG: = zeroTri vector NB. creates Zero matrix with vector on diagonal
zeroTri=: 1 :'([: u/~ i.@#) * ]'

NB.*makeSym v Forces matrix to symmetrical matrix
NB. adds transpose to sum of off-diagonals
NB. good for re-ordered triangular matrices
NB.! will alter non-triangular matricies
makeSym=: (|: + ~: zeroTri) f.

NB.*getLowerTri v Retrieves lower triangular items of matrix as boxed rows
getLowerTri=: ([: <\ i.@#) {"1&.> <"1

NB.*getUpperTri v Retrieves upper triangular items of matrix as boxed rows
getUpperTri=: ([: <\. i.@#) {"1&.> <"1

NB.*calcTriCount v Calculates number of triangular items in a y by y matrix
NB. eg: calcTriCount 5
NB. inverse: calculates rows/cols of matrix formed by y triangular items
calcTriCount=: ([: -: *: + ]) :. (<.@:%:@:+:)

NB.*calcLowerTriVecIdx v Calculates the vector indicies of lower triangular
matrix positions
NB. eg:  (;getLowerTri i.10 10) {~ calcLowerTriVecIdx 7 3,2 9,4 3,3 4,:1 6
calcLowerTriVecIdx=: (0 0.5 0.5&p.@:{."1 + {:"1)@:\:~"1

NB.*IdotInv v Models the inverse of monadic I. given the length of the
result as left argument
NB. eg: 10 IdotInv I. 1 1 0 1 0 0 1 0 0 0
NB. see links in
http://www.jsoftware.com/pipermail/programming/2019-October/054047.html
IdotInv=: (0 $~ [) (#/.~@])`(~.@])`[} ]

NB.*vec2LowerTri v Box the matrix rows from a vector of the lower
triangular items of a matrix
NB. eg: vec2LowerTri 0 1.5 0 2 1.5 0 2.5 2 1.5 0
vec2LowerTri=: (] IdotInv i.&.(calcTriCount^:_1))@# <;.1 ]

On Thu, Oct 24, 2019 at 3:59 AM Raul Miller <rauldmil...@gmail.com> wrote:

> Sure, ...
>
> The , , ,: approach is cleaner than the > ; ; ; approach (and it's
> generically useful both for symmetric numeric matrices and for
> constructing constant matrices).
>
> Meanwhile, the -: trick is clever and concise (but not generic).
>
> Thanks,
>
> --
> Raul
>
> On Wed, Oct 23, 2019 at 5:23 AM 'Mike Day' via Programming
> <programm...@jsoftware.com> wrote:
> >
> > Or:
> >
> >    (+|:)-:0,3 0,4 3,:5 4 3 0
> >
> > ?
> >
> > This generalises the lean but ad hoc expression:
> >    -:@(+|:)@:(|.@(,\.))@:({. 3+i.@-@<: ) 5. NB. eg for 5 x 5
> >   0 1.5   2 2.5   3
> > 1.5   0 1.5   2 2.5
> >   2 1.5   0 1.5   2
> > 2.5   2 1.5   0 1.5
> >   3 2.5   2 1.5   0
> > ... the 3+ bit could be generalised with a bit more work...
> >
> > Mike
> > Sent from my iPad
> >
> > > On 23 Oct 2019, at 04:38, 'Jim Russell' via Programming <
> programm...@jsoftware.com> wrote:
> > >
> > >  D=: (+|:)>'';1.5;2 1.5;2.5 2 1.5 0
> > ----------------------------------------------------------------------
> > 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