-[[.:start]]
====== DM41X Matrix ======
** Matrix functions in the [[advantage_pac|Advantage Pac]] **
===== Background =====
The **matrix** functionality isn't a standalone program, with a menu, like [[dm41x_curve_fitting|CFIT]] and [[polynomial|PLY]].
It's a collection of individual programs that can be used for many matrix tasks. Each one has to be called individually as needed.
There are many commands, the most used are those for
* Dimensioning a new matrix ''MATDIM''
* Editing a matrix to enter values - there are 2 versions
* Real Matrix ''MEDIT''
* Complex Matrix ''CMEDIT''
* Inverting a matrix ''MINV''
* Transposing a matrix ''TRNPS''
* Finding the determinant ''MDET''
* Multiplying two matrices together ''M*M''
* Solving a system of equations defined by 2 matrices ''MSYS''
===== Shortcuts to Matrix Commands =====
To make things easier it's best to use the DM41X ''CST'' custom command shortcut mechanism.
Start with a blank ''CST'' and add the commands you need. Then save this ''CST'' so that it's easy to switch to other ''CST'' setups, and then return to the ''Matrix'' setup later.
I have the following
|A|''MATDIM''|I| |
|B|''MEDIT''|J| |
|C|''CMEDIT''|K| |
|D|''MINV''|L| |
|E|''M*M''|M|''ZK?YN''|
|F|''MDET''|N|''EMDIR'' |
|G|''TRNPS''|O|''EMDIRX''|
|H|''MSYS''|P|''PURFL''|
Matrices are saved in Extended Memory, and I put commands in CST to find and remove them once I'm finished: ''EMDIR'', ''EMDIRX'' and ''PURFL'' and I have ''ZK?YN'' to quickly access [[41z_module|]] complex number functions (assuming the module is //plugged-in//).
===== Basic Workflow =====
All matrix addressing is done via the ''ALPHA'' register
==== Dimension a new Matrix ====
* Put the ''name'' of the matrix in the ''ALPHA'' register.
* Use single letters ''A'', ''B'', ''C'' etc. - these will create the matrix in Extended Memory
* ALPHA 'A' ALPHA
* Put the dimensions in the ''X'' register
* this is in the form ''r.ccc'' with ''3'' decimal places for the column
* for a ''3x3'' : ''3.003'' (3 rows, 3 columns)
* for a ''4x2'' : ''4.002'' (4 rows, 2 columns)
* ''3.003''
* puts the dimensions in the ''X'' register ready for ''MATDIM'' to assign to the matrix named in ''ALPHA''
* Execute the ''MATDIM'' command from the ''CST'' menu ''A''
* CST 'A'
* This creates matrix ''A'' in extended memory
==== Populate a new Matrix ====
* Populate the matrix with values using ''MEDIT'' from the ''CST'' menu ''B''
For a ''3x3'' matrix
''
| 1 1 -1 |
| 4 6 6 |
| 1 8 9 |
''
* CST 'B'
* ''1:1='' '1''R/S'
* ''1:2='' '1''R/S'
* ''1:3='' '1'CHS'R/S'
* ''2:1='' '4''R/S'
* ''2:2='' '6''R/S'
* ''2:3='' '6''R/S'
* ''3:1='' '1''R/S'
* ''3:2='' '8''R/S'
* ''3:3='' '9''R/S'
===== Determinant =====
With ''A'' still in the ''ALPHA'' register use the ''MDET'' command from the ''CST'' menu ''F''
* CST 'F'
* ''-50.000''
* Matrix ''A'' will now be in ''LU Decomposition'' form.
* It can be used in this form for some other functions, but it's possible to get it back to ''normal'' form by **Inverting** twice using ''MINV'' from ''CST'' menu ''D''
* CST 'D'
* CST 'D'
* You can check the element values by using ''MEDIT'' again, but just cycling through each with 'R/S' and not changing any values
* it's possible to set flag ''08'' which prevents any changes to values
===== Multiplication =====
** Using ''M*M'' **
For matrix multiplication you need ''3'' matrix definitions:
* Matrix ''A'' and Matrix ''B'' are the two matrices to be multiplied
* Matrix ''A'' and Matrix ''B'' are dimensioned with ''MATDIM'' and populated with ''MEDIT''
* Matrix ''C'' is the matrix to hold the result
* Matrix ''C'' is simply dimensioned with ''MATDIM'' and will be filled automatically by the function ''M*M''
To multiply two matrices together they must have the correct dimensions
* Columns in ''A'' must equal Rows in ''B''
* Results matrix ''C'' will have the dimensions ''Rows of A x Columns of B''
==== Create Matrix A ====
Follow the same procedure as above.
Make sure ''ALPHA'' has only the letter ''A''
A different numerical example
''A'' will be a ''3x2'' matrix
* use ''MATDIM'' via ''CST'' menu
* ''3.002'' CST'A'
''
| 2 3 |
| 4 5 |
| 7 6 |''
* Use ''MEDIT'' via ''CST'' menu
* CST 'B'
* ''1:1='' '2''R/S'
* ''1:2='' '3''R/S'
* ''2:1='' '4''R/S'
* ''2:2='' '5''R/S'
* ''3:1='' '7''R/S'
* ''3:2='' '6''R/S'
==== Create Matrix B ====
''B'' will be a ''2x2'' matrix
Make sure ''ALPHA'' only has ''B'' and then dimension ''B'' with ''MATDIM'' from ''CST'' menu ''A''
* ALPHA 'B' ALPHA
* ''2.002'' CST'A'
''
| 2 3 |
| 4 5 |''
* Use ''MEDIT'' via ''CST'' menu
* CST 'B'
* ''1:1='' '2''R/S'
* ''1:2='' '3''R/S'
* ''2:1='' '4''R/S'
* ''2:2='' '5''R/S'
We now have defined the matrices to be multiplied together.
==== Create Matrix C ====
We need a ''results'' matrix ''C''
The dimensions of ''C'' will be ''rows of A'' and ''columns of B'' which is ''3'' rows and ''2'' columns : ''3.002''
Make sure ''ALPHA'' only has ''C'' and dimension ''C'' with ''MATDIM'' from ''CST'' menu ''A''
* ALPHA 'C' ALPHA
* ''3.002'' CST'A'
That's enough to create the ''results'' matrix
==== Multiply the matrices ====
* Put the matrix names in ''ALPHA'' in the form ''Matrix 1,Matrix2,Results Matrix''
* ''ALPHA'' = ''A,B,C''
* ALPHA 'A' ',' 'B' ',' 'C'ALPHA
* Execute ''M*M'' from the ''CST'' menu
* CST 'E'
* ''C'' will now hold the answer
==== View the result ====
* Put ''C'' in the ''ALPHA'' register
* ALPHA 'C' ALPHA
* Use ''MEDIT'' from the ''CST'' menu and cycle through the elements (it's a ''3x2'' matrix)
* CST 'B'
* ''1:1= 16.000?'' 'R/S'
* ''1:2= 21.000?'' 'R/S'
* ''2:1= 28.000?'' 'R/S'
* ''2:2= 37.000?'' 'R/S'
* ''3:1= 38.000?'' 'R/S'
* ''3:2= 51.000?'' 'R/S'
The ''result'' in ''C'' is
''
| 16 21 |
| 28 37 |
| 38 51 |''
===== System of Equations =====
** A simple example first **
==== Set of Equations ====
** 3 unknowns **
''
3x + 4y + 6z = 39.7
2x - 4y - 3z = -7.0
y + 8z = 63.4 ''
==== Matrix representation ====
=== Matrix A ===
''
| 3 4 6 |
| 2 -4 -3 |
| 0 1 8 |
''
* Dimensions ''3.003''
=== Matrix B ===
''
| 39.7 |
| -7.0 |
| 63.4 |
''
* Dimensions ''3.001''
Simultaneous Equations represented in Matrix Form
''
|A| x |C| = |B|
| 3 4 6 | | x | | 39.7 |
| 2 -4 -3 | x | y | = | -7.0 |
| 0 1 8 | | z | | 63.4 |''
==== Create Matrix A ====
* ALPHA 'A' ALPHA
* ''3.003'' CST 'A' (''MATDIM'')
* CST 'B' (''MEDIT'')
* ''1:1='' '3' 'R/S'
* ''1:2='' '4' 'R/S'
* ''1:3='' '6' 'R/S'
* ''2:1='' '2' 'R/S'
* ''2:2='' '4' CHS 'R/S'
* ''2:3='' '3' CHS 'R/S'
* ''3:1='' '0''R/S'
* ''3:2='' '1''R/S'
* ''3:3='' '8''R/S'
==== Create Matrix B ====
* ALPHA 'B' ALPHA
* ''3.001'' CST 'A' (''MATDIM'')
* CST 'B' (''MEDIT'')
* ''1:1='' '39.7' 'R/S'
* ''2:1='' '7' CHS 'R/S'
* ''3:1='' '63.4' 'R/S'
==== Run MSYS ====
* Put ''A,B'' in ''ALPHA''
* ALPHA 'A' ',' 'B' ALPHA
* Run ''MSYS'' - the solution matrix for ''x'', ''y'' and ''z'' will be placed in Matrix ''B''
* CST 'H'
==== Examine Matrix B ====
* Put ''B'' in ''ALPHA''
* ALPHA 'B' ALPHA
* Run ''MEDIT'' from ''CST'' menu
* CST 'B' (''MEDIT'')
* ''1:1='' ''1.500'' 'R/S/
* ''2:1='' ''-3.800'' 'R/S'
* ''3:1='' ''8.400'' 'R/S
==== The solution ====
''
x = 1.5
y = -3.8
z = 8.4 ''
==== Matrix A ====
* ''A'' is now in LU-decomposed format
* It can be restored to normal by **Inverting Twice**
* Put ''A'' in ''ALPHA''
* Run ''MINV'' via ''CST'' menu twice
* CST 'D' CST 'D'
* ''A'' is now back to normal and can be inspected and edited by ''MEDIT'' via the ''CST'' menu
===== Another way to solve a system =====
Instead of the ''MSYS'' function you could do it manually by entering Matrix ''A'' and Matrix ''B'', and define Matrix ''C'' such that
''A'' X ''C'' = ''B''
''C'' = ''B'' / ''A''
''C'' = ''A*'' X ''B'' where ''A*'' is ''Inverse A''
Create Matrix ''C'' with dimensions ''3.001'' in the usual way then and do:
* Put ''A'' in ''ALPHA''
* Invert ''A'' using ''MINV'' from ''CST'' menu (''A'' is replaced by its inverse)
* Put ''A,B,C'' in ''ALPHA''
* Multiply using ''M*M''
* this multiplies ''B'' by the inverse of ''A'' which is effectively dividing ''B'' by the original ''A''
* ''C'' now contains the solutions for ''x'', ''y'' and ''z''
* Put ''C'' in ''ALPHA''
* Examine ''C'' with ''MEDIT''
* ''1:1= 1.500'' 'R/S'
* ''2:1= -3.800'' 'R/S'
* ''3:1= 8.400'' 'R/s'
* This preserves ''B'' unlike the ''MSYS'' method.
* The Inverse of ''A'' can be reversed by repeating ''MINV'' with ''A'' in ''ALPHA''
* We can even run ''M*M'' on ''A'' and ''C'' to re-create ''B''
* Put ''A,C,B'' in ''ALPHA''
* Run ''M*M'' from ''CST'' menu
* ''B'' will be re-created
===== The Complex Matrix =====
It is possible to define, edit and carry out operations with matrices containing Complex Numbers.
The matrix is defined with **twice as many rows and columns** as appears necessary, and edited with the function ''CMEDIT'', in ''CST'' menu ''C''
==== Example Definition ====
''
| 1+j2 5+j6 |
| 3+j4 7+j8 |
''
Two rows and two columns ''2x2'' containing complex numbers, so the definition/dimension is ''4.004''
The numbers are actually saved as if in a larger real matrix as
''
| 1 -2 5 -6 |
| 2 1 6 5 |
| 3 -4 7 -8 |
| 4 3 8 7 |
''
The entry is via ''CMEDIT'' via ''CST'' menu ''C'', which asks for each ''Real'' and ''Imaginary'' part in turn
* ALPHA 'A' ALPHA
* ''4.004'' 'CST' 'A'
* 'CST' 'C'
* ''RE. 1:1='' 1 'R/S'
* ''IM. 1:1='' 2 'R/S'
* ''RE. 1:2='' 5 'R/S'
* ''IM. 1:2='' 6 'R/S'
* ''RE. 2:1='' 3 'R/S'
* ''IM. 2:1='' 4 'R/S'
* ''RE. 2:2='' 7 'R/S'
* ''IM. 2:2='' 8 'R/S'
==== Multiplication of Complex Matrices ====
=== Create a second matrix ===
* Matrix ''B'' of size ''2x1'' which can be multiplied by ''A'' as ''A'' x ''B''
''
| 3+j4 |
| 2+j6 |
''
=== Dimension second matrix ===
This is dimensioned as double the basic size of ''2x1'' : ''4.002'' and then populated with ''CMEDIT''
* ALPHA 'B' ALPHA
* ''4.002'' 'CST' 'A'
* 'CST' 'C'
* ''RE. 1:1='' 3 'R/S'
* ''IM. 1:1='' 4 'R/S'
* ''RE. 2:1='' 2 'R/S'
* ''IM. 2:1='' 6 'R/S'
=== Create a result matrix ===
Matrix 'C' which will be also be a ''2x1'' complex with dimension :''4.002''
* ALPHA 'C' ALPHA
* ''4.002'' 'CST' 'A'
=== Multiply A X B = C ===
* ALPHA 'A' ',' 'B' ',' 'C' ALPHA
* 'CST' 'E'
=== Inspect Result Matrix C ===
* ALPHA 'C'
* 'CST' 'C'
* ''RE. 1:1= -31.000'' 'R/S'
* ''IM. 1:1= 52.000'' 'R/S'
* ''RE. 2:1= -41.000'' 'R/S'
* ''IM. 2:1= 82.000'' 'R/S'
The result matrix is therefore
''
| -31+j52 |
| -41+j82 |
''
Which agrees with my long-hand calculation (thanks to [[41z_module|]] )
''
|(1+j2).(3+j4) + (5+j6).(2+j6)|
|(3+J4).(3+J4) + (7+J8).(2+J6)|
''
''
| (-5+j10) + (-26+j42) |
| (-7+j24) + (-34+j58) |
''
''
| -31+j52 |
| -41+j82 |
''
==== Systems of Complex Equations.... ====
Once you can enter and manipulate Complex Matrices you can also use ''MSYS'' to solve systems with complex matrices in the same way as usual - the only difference is entering the matrices as Complex.
===== Clearing Up Extended Memory =====
After creating and manipulating (and some finger trouble) there will be a bunch of files in Extended Memory.
Most will be simple matrix definitions with obvious names ''A'', ''B'', ''C'' etc. but occasionally you'll accidentally have run ''MATDIM'' with some nonsense in the ''ALPHA'' register and created a nonsensical filename in extended memory, which it's tricky to spell out correctly in ''ALPHA'' in order to run ''PURLF'' - this is where ''EMDIRX'' comes in.
To simplify clearing up I include ''EMDIR'', ''EMDIRX'' and ''PURFL'' in the ''CST'' menu.
Assuming you have other files in Extended Memory, the quickest way is to find the number of files in EM with ''EMDIR''.
* Run ''EMDIR'' from ''CST'' menu
* CST 'N'
* Count the files to find the number of the first file you've created using ''MATDIM'' - i.e. the first one //after// your other EM files
* Put this number in ''x''
* Run ''EMDIRX'' from ''CST'' menu
* CST 'O'
* this puts the ''name'' of the specified-numbered file in ''ALPHA'' and means you don't have to type it in yourself
* Run ''PURFL'' from ''CST'' menu to delete this filename
* CST 'P'
* Run ''EMDIRX'' again (with the same number in ''X'')
* If ''X'' returns as non-zero we've found another //junk-matrix// file in EM and ''ALPHA'' will have its name
* Run ''PURFL'' again
* Keep doing this until ''EMDIRX'' returns ''0.00'' in ''X'' which means there are no more files to delete
* all your original files are still safe, as they are numbered //below// the number we were searching for
===== Further Information =====
{{tag>calculator dm41x}}