---Nick Kostirya wrote:
> Will you please tell me what is the optimal way to create
> upper triangular matrix and right triangular matrix?
I don't know about optimal but here are some verbs proposed by Roger on 9 Dec
1999.
NB.*tfv v triangular matrix from vector
tfv=: 3 : 0
n=. <: 2&!^:_1 #y
(-n) [\ (,<:/~i.n) #^:_1 y
)
NB.*vft vector from triangular matrix
vft=: ,@(<:/~)@[EMAIL PROTECTED] # ,
Here are more general purpose verbs that I use:
NB.*putTriMat v fills an upper/lower triangular square matrix with/without
diagonal.
NB. result is triangular square matrix filled with y according to options x
NB. y is numeric list of
NB. x is optional 2-item boolean vector specifying the
NB. triangular part to fill. Defaults to 1 1.
NB. 0{x specifies fill upper(1)/lower triangle
NB. 1{x specifies fill diagonal(1)/or not
putTriMat=: 3 : 0
1 1 putTriMat y
:
assert. (2=#,x) *. x e. 0 1
nrow=. ,/@:<:@:(2&!^:_1) #y NB. nrows of sqr matrix with #y tri elements
assert. (-:<.)nrow NB. len(y) = count of tri elements in square matrix
nrow=. nrow+ -.{:x NB. increase nrows if y doesn't include diag
cmp=. >`>:`<`<:@. (2&#. x) NB. choose case
mask=. , cmp/&i.~ nrow
(-nrow) [\ mask expand y
)
NB.*getTriMat v Extracts triangular parts of a matrix
NB. results is numeric vector from specified triangular part
NB. y is numeric matrix
NB. x is optional 2-item boolean vector specifiying
NB. the triangular part to extract. Defaults to 1 1
NB. 0{x specifies upper(1)/lower triangle
NB. 1{x specifies include(1)/exclude diagonal
getTriMat=: 3 : 0
1 1 getTriMat y
:
assert. (2=#,x) *. x e. 0 1
'nrow ncol'=. $y
cmp=. >`>:`<`<:@. (2&#. x) NB. choose case
mask=. , nrow cmp/&i. ncol
mask#,y
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm