Matrix Rotation of elements - Algorithms - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: General Forum (/forum-4.html) +--- Thread: Matrix Rotation of elements - Algorithms (/thread-14156.html) Matrix Rotation of elements - Algorithms - Ángel Martin - 12-12-2019 02:37 PM 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. RE: Matrix Rotation of elements - Algorithms - Csaba Tizedes - 12-12-2019 03:31 PM (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 RE: Matrix Rotation of elements - Algorithms - Ángel Martin - 12-12-2019 03:59 PM (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... RE: Matrix Rotation of elements - Algorithms - Ángel Martin - 12-15-2019 06:16 AM 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 RE: Matrix Rotation of elements - Algorithms - Ángel Martin - 12-17-2019 10:46 AM 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. RE: Matrix Rotation of elements - Algorithms - Csaba Tizedes - 12-17-2019 02:22 PM 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 ) 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 RE: Matrix Rotation of elements - Algorithms - Ángel Martin - 12-17-2019 03:51 PM 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/27    INT        int(m/2)8    E    9    -          int(m/2) - 110   I<>J    11   STO 00     0,00(int(m/2))12   LBL 00    13   DIM?       m,00n14   FRC        0,00n15   RCL 00     k,00l16   INT        k17   E    18   +          1+k19   STO 02    20   I<>J       0,00(1+k)21   -          0,00(n-k-1)22   RCL 02     k+123   +    24   STO 01     (k+1),00(n-k-1)24   LBL 0125   E    26   XEQ 05     shift row27   ISG 01    28   GTO 01     do next row29   DIM?       m,00n30   INT        m31   RCL 01     k+132   -          m-k-133   I<>J       0,00(m-k-1)34   RCL 02     1+k35   =    36   STO 01     (k+1),00(m-k-1)37   DIM?       m,00n38   RCL 00    39   I<>J       x,00k40   -          m,00(n-k)41   FRC        0,00(n-k)42   STO  02    0,00(n-k)43   LBL  02    44   E          increase row45   XEQ 06     shift column46   ISG  01    NEXT j47   GTO  02    do next column48   DIM?       m,00n49   FRC        0,00n50   I<>J       n 51   RCL  00    52   INT        k53   -          (n-k)54   E      55   LASTX      k56   +          2+k-157   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,00n62   RCL  00    k,00l63   -          (m-k),00(n-l)64   INT        m-k65   STO  02    (m-k)66   LBL  03    67   -1         decrease colum68   XEQ  05    shift row69   DSE  01    NEXT i70   GTO  03    do next row71   DIM?       m,00n72   RCL  00    k,00l73   -          (m-k),00(n-l)74   INT        m-k75   RCL 03     0,00(2-k-1)76   +    77   STO  01    (m-k),00(2+k-1)78   RCL  00    79   INT        k80   E      81   +          k+182   I<>J       0,00(1+k)83   STO  02    0,00(1+k)84   LBL  04    85   -1    86   XEQ  06    shift column87   DSE  01    NEXT j88   GTO  04    do next column89   ISG  00    NEXT k90   GTO  00    do next layer!91   ASWAP    92   MAT=       Make Matrix Equal 93   PURFL    94   MNAME?     restore Matrix Name95   RTN    96   LBL  05    97   I<>J       0,00198   RCL  01    99   INT    100  I<>J        i or j101  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 Pointer111  MR         Recall Element112  RCL  Z (1)    113  ASWAP    114  MSIJA      Sets Pointer115  X<>Y       Store by Row116  MS    117  ASWAP    118  END ` RE: Matrix Rotation of elements - Algorithms - Valentin Albillo - 12-17-2019 04:18 PM . 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. 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 ! Best regards. V. RE: Matrix Rotation of elements - Algorithms - Ángel Martin - 12-18-2019 06:29 AM (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. 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 ;-) RE: Matrix Rotation of elements - Algorithms - Paul Dale - 12-18-2019 07:05 AM Ángel, your typos are uncommon and your English is perfect