LOGO-L> Diagonal Matrix
Author Message
LOGO-L> Diagonal Matrix

I just made a routine that does this:

show diagmatrix [1 2 3]
[[1 0 0] [0 2 0] [0 0 3]]

show newvec 5 1
[1 1 1 1 1]
show diagMatrix newvec 5 1
[[1 0 0 0 0] [0 1 0 0 0] [0 0 1 0 0] [0 0 0 1 0] [0 0 0 0 1]]

show diagMatrix [[1 2] [3 4] [5 6] [7 8]]
[[1 2 0 0 0] [0 3 4 0 0] [0 0 5 6 0] [0 0 0 7 8]]

From the J Introduction and Dictionary:

"The application of a function to diagonals of a table is
commonly useful, as in correlation, in convolution, and
in products of polynomial coefficients"

polynomial products.

(3*x^3-2*x^2+5*x-2)*(2*x^2+4*x-3) = 6*x^5+8*x^4-7*x^3+22*x^2-23*x+6

(3*x^3-2*x^2+5*x-2)     -> [[3 -2 5 -2]]
(2*x^2+4*x-3)           -> [[0 2 4 -3]]

show transpose [[3 -2 5 -2]]
[[3] [-2] [5] [-2]]

show matprod transpose [[3 -2 5 -2]] [[0 2 4 -3]]
[[0 6 12 -9] [0 -4 -8 6] [0 10 20 -15] [0 -4 -8 6]]

show diagmatrix matprod transpose [[3 -2 5 -2]] [[0 2 4 -3]]
[[0 6 12 -9 0 0 0] [0 0 -4 -8 6 0 0] [0 0 0 10 20 -15 0] [0 0 0 0 -4 -8 6]]

show reduce "vecsum diagmatrix matprod transpose [[3 -2 5 -2]] [[0 2 4 -3]]
[0 6 8 -7 22 -23 6]

Which are the coefficients of: 6*x^5+8*x^4-7*x^3+22*x^2-23*x+6 !!

This comes from bringing more of APL and J to LOGO.

Daniel
---------

to diagMatrix :v [:s count :v]
if (:s=1) [op :v]
op (fput (se first :v newVec :s-1 0) ~
(map [se 0 ?] (diagMatrix butfirst :v :s-1)))

end

to newVec :s :c ; size, coefficient
if (:s=0) [op []]
op fput :c newVec :s-1 :c
end

to vecsum :a :b
op (map "sum :a :b)
end

------------

"matprod" (aka. matmul) and "transpose" are the usuals.

to matprod :m1 :m2 [:tm2 transpose :m2] ; multiply two matrices
output map [[row] map [[col] dotprod :row :col] :tm2] :m1
end

bury "matprod

to dotprod :a :b        ; vector dot product
op apply "sum (map "product :a :b)
end

bury "dotprod

to transpose :matrix
if emptyp first :matrix [op []]
op fput firsts :matrix transpose bfs :matrix
end

bury "transpose

---------------------------------------------------------------

Fri, 06 Jul 2001 03:00:00 GMT

 Page 1 of 1 [ 1 post ]

Relevant Pages