Post Reply 
Matrix Rotation of elements - Algorithms
12-12-2019, 02:37 PM
Post: #1
Matrix Rotation of elements - Algorithms
For no particular reason I've been looking into some matrix operations not mainstream, such as the element rotation, or "layer rotation". Note this is not the same as the "Rotation Matrix" used in transfromations theory - [ or is it? Somehow these two are begging to be connected in my mind... ]

Surely some scarce references exist (see here, here, and here) but I wonder if friendlier algorithms have been worked out that allow for an easier implementation using our trusted toolset in the Advantage and SandMatrix modules...

So to clarify, I'm after a single-element rotation algorithm, either clockwise or anti-clockwise. Rotating the entire matrix 90 degrees is actually very simple using the transpose and column swapping , but a single element is much harder.

Anyy inputs will be appreciated.

"To live or die by your own sword one must first learn to wield it aptly."
Find all posts by this user
Quote this message in a reply
12-12-2019, 03:31 PM
Post: #2
RE: Matrix Rotation of elements - Algorithms
(12-12-2019 02:37 PM)Ángel Martin Wrote:  I wonder if friendlier algorithms have been worked out

Which units are in play? 48xx? My 39gII arrived this week - maybe that?

Csaba
Find all posts by this user
Quote this message in a reply
12-12-2019, 03:59 PM
Post: #3
RE: Matrix Rotation of elements - Algorithms
(12-12-2019 03:31 PM)Csaba Tizedes Wrote:  Which units are in play? 48xx? My 39gII arrived this week - maybe that?

RPN if possible...

"To live or die by your own sword one must first learn to wield it aptly."
Find all posts by this user
Quote this message in a reply
12-15-2019, 06:16 AM (This post was last modified: 12-16-2019 12:54 PM by Ángel Martin.)
Post: #4
RE: Matrix Rotation of elements - Algorithms
here's a start, surely not the best approach but it seems to do the job:


for an (m x n) matrix, the rotated matrix (one single step, clockwise) bij is given as follows:-

FOR k = 0 TO int(m/2)-1 ; current layer

FOR j = k+1 TO (n-1-k) ; top row rightwards
b(1+k),(j+1) = a(1+k),j
NEXT j

FOR i = (1+k) TO (m-1-k) ; rightmost column downwards
b(i+1),(n-k) = a i,(n-k)
NEXT i

FOR j = (n-k) TO (2+k) STEP -1 ; bottom row leftwards
b(m-k),(j-1) = a(m-k),j
NEXT j

FOR i = (m-k) TO (2+k) STEP -1 ; leftfmost column upwards
b(i-1),(1+k) = a i,(1+k)
NEXT i

NEXT k ; next layer

Edited: corrected typos in indexes

"To live or die by your own sword one must first learn to wield it aptly."
Find all posts by this user
Quote this message in a reply
12-17-2019, 10:46 AM (This post was last modified: 12-17-2019 10:47 AM by Ángel Martin.)
Post: #5
RE: Matrix Rotation of elements - Algorithms
I don't think I said it before but when it comes to this kind of programs I really hate RPN, or more appropriately I hate FOCAL (let's put the blame where it belongs).

Really what in BASIC is a simple and quick affair (see previous post) becomes tricky, sticky and error prone beyond relief using FOCAL - all those sub-indexes and counters - all pitfall-ridden!

Regardless, the routine is done at last - will post it in the program section in a couple of days.

"To live or die by your own sword one must first learn to wield it aptly."
Find all posts by this user
Quote this message in a reply
12-17-2019, 02:22 PM
Post: #6
RE: Matrix Rotation of elements - Algorithms
My first idea was that it requires something "matrix-to-list" stuff and the lists can be more flexible manipulate. (I totally agree: for matrices the BASIC is better Tongue)

The second was, how the HP 15C reorganize the elements, when I change the dimension of the matrices?!?

The third was I need only a "row-extractor" and I will use it to copy one row into list, than I transpose the matrix and again extract and transpose and extract... Or something similar, and of course I must to put back the rotated list elements with the same way: put-transpose-put-transpose...

The fourth was: this is something "walking-inside-a-spirale-labirynth": I started in the center and if I found a wall, I turn left (or right) and again until I found the last element. If I copy the values into a list during my way, I can transform my matrix into a list.


This is a good exercise, I will workin' on it - after this Friday...

Csaba
Find all posts by this user
Quote this message in a reply
12-17-2019, 03:51 PM (This post was last modified: 12-17-2019 04:04 PM by Ángel Martin.)
Post: #7
RE: Matrix Rotation of elements - Algorithms
I actually use a scratch matrix and copy elements one at a time from the source to the scratch, and when done do the bulk renaming source = scratch.

Here's the routine. you need to be familiar with the SandMatrix functions (an extension of the Advantage, itself an extension of the CCD ARRAY group...). The element copying happens in LBL 05 towards the end of the program.


PHP Code:
1    LBL "M<)R"    
2    "|-,#"    
3    MAT=    
4    DIM?    
5    2    
6     
/         m/2
7    INT        int
(m/2)
8    E    
9    
-          int(m/2) - 1
10   I
<>J    
11   STO 00     0
,00(int(m/2))
12   LBL 00    
13   DIM
?       m,00n
14   FRC        0
,00n
15   RCL 00     k
,00l
16   INT        k
17   E    
18   
+          1+k
19   STO 02    
20   I
<>J       0,00(1+k)
21   -          0,00(n-k-1)
22   RCL 02     k+1
23   
+    
24   STO 01     (k+1),00(n-k-1)
24   LBL 01
25   E    
26   XEQ 05     shift row
27   ISG 01    
28   GTO 01     
do next row
29   DIM
?       m,00n
30   INT        m
31   RCL 01     k
+1
32   
-          m-k-1
33   I
<>J       0,00(m-k-1)
34   RCL 02     1+k
35   
=    
36   STO 01     (k+1),00(m-k-1)
37   DIM?       m,00n
38   RCL 00    
39   I
<>J       x,00k
40   
-          m,00(n-k)
41   FRC        0,00(n-k)
42   STO  02    0,00(n-k)
43   LBL  02    
44   E          increase row
45   XEQ 06     shift column
46   ISG  01    NEXT j
47   GTO  02    
do next column
48   DIM
?       m,00n
49   FRC        0
,00n
50   I
<>J       n 
51   RCL  00    
52   INT        k
53   
-          (n-k)
54   E      
55   LASTX      k
56   
+          2+k-1
57   I
<>J       0,00(2-k-1)
58   STO 03     0,00(2-k-1)
59   +          (n-k),00(2-k-1)
60   STO  01    (n-k),00(2-k-1)
61   DIM?       m,00n
62   RCL  00    k
,00l
63   
-          (m-k),00(n-l)
64   INT        m-k
65   STO  02    
(m-k)
66   LBL  03    
67   
-1         decrease colum
68   XEQ  05    shift row
69   DSE  01    NEXT i
70   GTO  03    
do next row
71   DIM
?       m,00n
72   RCL  00    k
,00l
73   
-          (m-k),00(n-l)
74   INT        m-k
75   RCL 03     0
,00(2-k-1)
76   +    
77   STO  01    (m-k),00(2+k-1)
78   RCL  00    
79   INT        k
80   E      
81   
+          k+1
82   I
<>J       0,00(1+k)
83   STO  02    0,00(1+k)
84   LBL  04    
85   
-1    
86   XEQ  06    shift column
87   DSE  01    NEXT j
88   GTO  04    
do next column
89   ISG  00    NEXT k
90   GTO  00    
do next layer!
91   ASWAP    
92   MAT
=       Make Matrix Equal 
93   PURFL    
94   MNAME
?     restore Matrix Name
95   RTN    
96   LBL  05    
97   I
<>J       0,001
98   RCL  01    
99   INT    
100  I
<>J        i or j
101  GTO  07     0
,00(1+k)
102  LBL  06    
103  RCL  01    
104  INT    
105  LBL  07    
106  RCL  02    
107  
+    
108  +    
109  LASTX    
110  MSIJA      Sets Pointer
111  MR         Recall Element
112  RCL  Z 
(1)    
113  ASWAP    
114  MSIJA      Sets Pointer
115  X
<>Y       Store by Row
116  MS    
117  ASWAP    
118  END 

"To live or die by your own sword one must first learn to wield it aptly."
Find all posts by this user
Quote this message in a reply
12-17-2019, 04:18 PM
Post: #8
RE: Matrix Rotation of elements - Algorithms
.
Hi, Ángel:

(12-17-2019 10:46 AM)Ángel Martin Wrote:  Really what in BASIC is a simple and quick affair (see previous post) becomes tricky, sticky and error prone beyond relief using FOCAL - all those sub-indexes and counters - all pitfall-ridden!

Hey, "Mr. Translator", it's "beyond belief", not "beyond relief". The "r" is far from the "b" in the keyboard so that's unlikely to be a typo. Wink

After ruffling your feathers a little, this matrix rotation business is but a simple mapping and yes, it's done much more easily in BASIC than in SEAL-L, you're finally seeing the light ! Smile

Best regards.
V.

  
All My Articles & other Materials here:  Valentin Albillo's HP Collection
 
Visit this user's website Find all posts by this user
Quote this message in a reply
12-18-2019, 06:29 AM (This post was last modified: 12-18-2019 06:36 AM by Ángel Martin.)
Post: #9
RE: Matrix Rotation of elements - Algorithms
(12-17-2019 04:18 PM)Valentin Albillo Wrote:  Hey, "Mr. Translator", it's "beyond belief", not "beyond relief". The "r" is far from the "b" in the keyboard so that's unlikely to be a typo. Wink

Nope, in this instance I wrote exactly what I wanted to say: beyond relief with 'R"- I'm very aware of the idiom and other colloquial expressions but to me language is a fluid thing, so it'll be the day when I stop expressing in my own peculiar way - I ain't translating!

I'm not saying that I don't make my regular dose of typos of course, but this time Sauron's eye has erred his aim ;=)

à bientôt,
ÁM

PS. SEAL-L, uh? pretty clever but unorthodox ;-)

"To live or die by your own sword one must first learn to wield it aptly."
Find all posts by this user
Quote this message in a reply
12-18-2019, 07:05 AM
Post: #10
RE: Matrix Rotation of elements - Algorithms
Ángel, your typos are uncommon and your English is perfect Smile
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 7 Guest(s)