[Jprogramming] Matrix Transformations based on local submatrices
OK, I'm trying to do some work with matrices that involves transformations based on local properties of a matrix (neighbouring elements). This is the sort of thing you may find in image-processing edge-detection, etc, or in some cellular automata of the type of Conway's Life. In other words, I want to be able to supply a matrix, and have it so processed that the result is a new matrix where each new element was determined by a function which processed the old elements neighbours. Any direction, relevant help pages, etc, much appreciated. If anyone would go as far as offering some sample code that would be great, too! As a trivial example, suppose I want to calculate the sum of the neighbours of each element Say we have a matrix m0 =. 3 3 $ :i.9 What I want is the verb neighbours whereby neighbours m0 11 19 13 23 40 27 17 31 19 Obviously, I am maily interested in how to extract the neighbours submatrix, without resorting to explicit looping, if that is possible. To make it a bit more interesting, how would I extract the neighbours if the matrix was a torus or infinite tiling, such that: neighb-torus m0 44 43 42 41 40 39 38 37 36 Thanks for any suggestions! Alex -- For information about J forums see http://www.jsoftware.com/forums.htm
Re: [Jprogramming] Matrix Transformations based on local submatrices
Also, take a look at Marshall Lochbaum's code: http://www.jsoftware.com/jwiki/Community/Conference2012/Talks/ImageProcessing On Tue, 13 Nov 2012, Alex Giannakopoulos wrote: OK, I'm trying to do some work with matrices that involves transformations based on local properties of a matrix (neighbouring elements). This is the sort of thing you may find in image-processing edge-detection, etc, or in some cellular automata of the type of Conway's Life. In other words, I want to be able to supply a matrix, and have it so processed that the result is a new matrix where each new element was determined by a function which processed the old elements neighbours. Any direction, relevant help pages, etc, much appreciated. If anyone would go as far as offering some sample code that would be great, too! As a trivial example, suppose I want to calculate the sum of the neighbours of each element Say we have a matrix m0 =. 3 3 $ :i.9 What I want is the verb neighbours whereby neighbours m0 11 19 13 23 40 27 17 31 19 Obviously, I am maily interested in how to extract the neighbours submatrix, without resorting to explicit looping, if that is possible. To make it a bit more interesting, how would I extract the neighbours if the matrix was a torus or infinite tiling, such that: neighb-torus m0 44 43 42 41 40 39 38 37 36 Thanks for any suggestions! Alex -- For information about J forums see http://www.jsoftware.com/forums.htm -- For information about J forums see http://www.jsoftware.com/forums.htm
Re: [Jprogramming] Matrix Transformations based on local submatrices
Here's a starter... m1=:1+i.3 3 1 2 3 4 5 6 7 8 9 m1=:1+i.3 3 Now I have to tackle the real problem Linda -Original Message- From: programming-boun...@forums.jsoftware.com [mailto:programming-boun...@forums.jsoftware.com] On Behalf Of Alex Giannakopoulos Sent: Tuesday, November 13, 2012 6:44 AM To: J Programming forum Subject: [Jprogramming] Matrix Transformations based on local submatrices OK, I'm trying to do some work with matrices that involves transformations based on local properties of a matrix (neighbouring elements). This is the sort of thing you may find in image-processing edge-detection, etc, or in some cellular automata of the type of Conway's Life. In other words, I want to be able to supply a matrix, and have it so processed that the result is a new matrix where each new element was determined by a function which processed the old elements neighbours. Any direction, relevant help pages, etc, much appreciated. If anyone would go as far as offering some sample code that would be great, too! As a trivial example, suppose I want to calculate the sum of the neighbours of each element Say we have a matrix m0 =. 3 3 $ :i.9 What I want is the verb neighbours whereby neighbours m0 11 19 13 23 40 27 17 31 19 Obviously, I am maily interested in how to extract the neighbours submatrix, without resorting to explicit looping, if that is possible. To make it a bit more interesting, how would I extract the neighbours if the matrix was a torus or infinite tiling, such that: neighb-torus m0 44 43 42 41 40 39 38 37 36 Thanks for any suggestions! Alex -- For information about J forums see http://www.jsoftware.com/forums.htm -- For information about J forums see http://www.jsoftware.com/forums.htm
Re: [Jprogramming] Matrix Transformations based on local submatrices
Brian, You are correct: the Rosetta Code produces a grid of constant size (which is the starting size.) I've just rechecked my script against the Rosetta site: there are only two lines in Rosetta and I've reproduced them both. The reason why your two expressions give different results is, I am pretty confident, because in 14 iterations the starting pattern has disappeared over the edge of the 5 by 5 grid but has not yet reached the edge of the 10 by 10. Roger - Original Message - From: Brian Schott schott.br...@gmail.com To: programm...@jsoftware.com Sent: Tuesday, November 13, 2012 5:04 PM Subject: Re: [Jprogramming] Matrix Transformations based on local submatrices Roger, Thanks for this clear comparision. I tried some experiments with the Rosetta code version comparing rp 5 and rp 10 and I get different results for the two, suggesting that maybe the Rosetta code does not produce a variable size grid. Perhaps that means that you did not use the whole Rosetta code version for your cases? For example, compare the following 2 cases which produce different results. ;/7}.life ^: ( 14) rp 5 ;/7}.life ^: ( 14) rp 10 -- (B=) -my sig Brian Schott -- For information about J forums see http://www.jsoftware.com/forums.htm -- For information about J forums see http://www.jsoftware.com/forums.htm
Re: [Jprogramming] Matrix Transformations based on local submatrices
Another option separates structure from data. Given an initial matrix M, build: DATA=: (,M),0 T=. 1 1}._1 _1}. i.2+$M NDX=: ($M) $1 T i.,2 T +2 0 ] 0, (2+$y) #. _1 ^ #: i. 4 Now, NDX { DATA will give you a 5 item list of matrices which represent the five desired sample points. Note that NDX only has to be computed once (unless you are changing the size of the data you are working with). If you want to change the padding value used for the off the edge case, you can change the last item in DATA. -- Raul On Tue, Nov 13, 2012 at 3:48 PM, neit...@gaertner.de wrote: OK, I'm trying to do some work with matrices that involves transformations based on local properties of a matrix (neighbouring elements). As Henry Rich pointed out: the real solution will go via ;.3 tessalations. Have a look at those. Without those ;.3 cuts, the classic approach is this: A little to the left Right down the middle A little to the right, X marks the spot [Godley Creme, This Sporting Life, 1978] Solve the torus first: use |.to wiggle m up down (and center), use |.1 to wiggle those matrices left right (and center) ending up with 9 matrices, the original one and eight shifted ones, where the neighbours get shifted to the original position. +/+/, and subtract the original matrix agin if you want to sum up neighbours only, and you are done. You'll need to use l r a lot to make this work, left as an healthy exercise for you. For the non-torus problem: Pad m with borders of zeroes (easy with , and ,.), do the torus sums, throw away the borders of the sums. Martin -- For information about J forums see http://www.jsoftware.com/forums.htm -- For information about J forums see http://www.jsoftware.com/forums.htm
Re: [Jprogramming] Arc consistency in J
I can only give a personal response. Maybe it is because I'm left handed. When I look at ((@#)(i.n))@(0) at or -Original Message- From: programming-boun...@forums.jsoftware.com [mailto:programming-boun...@forums.jsoftware.com] On Behalf Of Mike Day Sent: Monday, November 12, 2012 6:40 AM To: programm...@jsoftware.com Subject: Re: [Jprogramming] Arc consistency in J But what's wrong with @ that's preferable with [: ?(I think Th is has been asked before.) Aren't they both devices to represent what ordinary mathematicians just write as f g h ... for the composition of f g h ... ? Since J cleverly allows hooks and forks and interprets unbracketed trains of verbs as such, it needs some other way to recognise composition, and that's what both @ (and @:) and [: do for us - so why avoid one or the other of them? I agree it took a lot of time to get my head round the new way of seeing trains of verbs. Mike On 12/11/2012 7:57 AM, Linda Alvord wrote: Well, it was possible, once I managed to get the rank right. Thanks for providing hope that it could be done. Actually it is not too bad looking after all. A 0 0 1 0 0 0 0 0 0 0 2 0 0 0 1 0 0 0 0 1 0 0 2 2 0 adj=:((@#)(i.n))@(0) adj @#0 1 2 3 4@(0) adj A --TT---T-T---┐ │2││0 4│4│2 3│ L-++---+-+ f=: 13 :'(0y)([:#)1 i.#y' f (0 ]) ([: #)1 [: i. # f A --TT---T-T---┐ │2││0 4│4│2 3│ L-++---+-+ 5!:4 'adj' -- -- @ ---+- # -- -+- 0 1 2 3 4 │ -- @ -+ -- 0 L- -+- 5!:4 'f' -- 0 --+- │ L- ] │ -- [: │ -+- --+- -+L- # │ L- 1 │ │ -- [: L-+- i. L- # Linda -Original Message- From: programming-boun...@forums.jsoftware.com [mailto:programming-boun...@forums.jsoftware.com] On Behalf Of Mike Day Sent: Sunday, November 11, 2012 7:15 AM To: programm...@jsoftware.com Subject: Re: [Jprogramming] Arc consistency in J I don't think you can remove the @, although you may use the cap, [: , instead, but then you need to force the rank: ([:(#0 1 2 3@(0)))1 A +-++---+-+ |2||0 3|2| +-++---+-+ I don't like using 0 1 2 3 explicitly since it presumes you know the dimension of A, so I prefer either adja =: * @# i.@# NB. or use caps if @ is too horrible adja A +-++---+-+ |2||0 3|2| +-++---+-+ or adjI =: @I.@:* adjI A +-++---+-+ |2||0 3|2| +-++---+-+ I. is so useful that I've defined an I dfn in my Dyalog APL too! As for Raul's point about D, I understood it to represent the domains of the variables: if there are m variables and the domain of all their possible values is i.n, then 1 = D{~i,j means that variable number i may have value j . m and n are likely to be different. So iD is a boolean representing the a priori values that variable i might have. The consistency algorithm apparently examines which of these a priori values are consistent with the domains of the other variables given certain unary and binary constraints which are also inputs to the problem. Michal's example had m=n which tends to mislead the casual reader! I believe The m*m A array points to which constraints apply, so 0l=A{i,k means that binary constraint number l applies between variables i and k. This is why A isn't just a boolean adjacency matrix nor do the entries signify distances as they would in a weighted graph. I'm puzzled that the algorithm requires each binary relation to be presented in both direct and transposed form (this explains Michal's need to patch in a new index (to a transposed C) in the lower triangle of A for each index to a direct C in the upper triangle). Perhaps the later algorithms (arc-4... ?) deal with this difficulty. It strikes me that for a real problem, concise relations such as x : y, or 2x+3y5 can become pretty large and unwieldy C-tables, but perhaps that's unavoidable when working in the integers. (The other) Mike On 11/11/2012 10:16 AM, Linda Alvord wrote: Mike, I think this will work as an alternative to adj A 0 0 1 0 0 0 0 0 2 0 0 1 0 0 2 0 adj @#0 1 2 3@(0) adj A --TT---T-┐ │2││0 3│2│ L-++---+-- h 0 1 2 3 @#~ 0 ] h A --TT---T-┐ │2││0 3│2│ L-++---+-- Can anyone remove the final @ from h ? Linda -Original Message- From:programming-boun...@forums.jsoftware.com [mailto:programming-boun...@forums.jsoftware.com] On Behalf Of Raul Miller Sent: Saturday, November 10, 2012 12:44 PM To:programm...@jsoftware.com Subject: Re: [Jprogramming] Arc consistency in J On Sat, Nov 10, 2012 at 12:16 PM, Michal D.michal.dobrog...@gmail.com wrote: Here X is telling us to use the constraint c1 (presumably b/c C is not shown) between the variables 1 and 3 (0 based). Likewise, use the