Post Reply 
HP 40gs how to get a submartix from a matrix
08-31-2023, 04:30 AM
Post: #1
HP 40gs how to get a submartix from a matrix
I have a 3x3 matrix:
M1 =
1,2,3
4,5,6
7,8,9

It is stored as:
[[1,2,3],[4,5,6],[7,8,9]] STO M1

Display M1
M1 ENTER

How do I get a sub-matrix such as:
M2 =
5,6
8,9

Are there any column and row operations or any indexing operations in order to get a sub matrix M2?
By indexing operations, using index and row column numbers as in arrays in python, matlab.

Thank you
Anthony of Sydney
Find all posts by this user
Quote this message in a reply
08-31-2023, 02:40 PM
Post: #2
RE: HP 40gs how to get a submartix from a matrix
I'm not familiar with the HP 40gs but you could use matrix multiplication.

In Python:
Code:
import numpy as np

M = np.array(
    [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
    ]
)

U = np.array(
    [
        [0, 1, 0],
        [0, 0, 1],
    ]
)

V = np.array(
    [
        [0, 0],
        [1, 0],
        [0, 1],
    ]
)

U @ M @ V

The result is:
Code:
array([[5, 6],
       [8, 9]])
Find all posts by this user
Quote this message in a reply
08-31-2023, 03:07 PM (This post was last modified: 08-31-2023 03:51 PM by Gilles.)
Post: #3
RE: HP 40gs how to get a submartix from a matrix
i've no HP40G here but perhaps :

MAKEMAT(M1(I+1,J+1),2,2)

2,2 are the size of the result matrix
First argument is the expression to calculate for each I,J position in the resulting matrix. No needs to loop, its automatic
Find all posts by this user
Quote this message in a reply
08-31-2023, 11:56 PM
Post: #4
RE: HP 40gs how to get a submartix from a matrix
Dear Thomas and Gilles,
Thank you both for your replies.
I learnt two new things:

In python, I learned a way of multiplying matrices:
To illustrate:
result = U @ M @ V
Is not the same as
result = U * M * V

In the first statement, the "@" does matrix multiplication.
But in the second statement, the "*" does element-by-element multiplication.

Also learnt that
result = V @ M @ U
produces
[[0 0 0] [0 0 0] [0 0 0]]

For the HP 40GS, one can do subsets of matrices:
[[1,2,3],[4,5,6],[7,8,9]] STO M1

MAKEMAT(M1,(I+1,J+1),2,2) STO M2. will produce a submatrix
[[5,6],[8,9]]

Question please
Is there a general method to get any 2x2 submatrix from a 3x3 submatrix using MAKEMAT?
Eg
[[1,3],[7,9]]

Thank you
Anthony ofvSydney
Find all posts by this user
Quote this message in a reply
09-01-2023, 02:31 AM
Post: #5
RE: HP 40gs how to get a submartix from a matrix
While I tried experimenting with the MAKEMAT in the hp 40gs, it seems to operate with ONLY adjacent rows and columns.
Eg:
MAKEMAT(M1(I,J+1),2,2) returns
[[2,3],[5,6]]

MAKEMAT(M1(I+1,J),2,2) returns
[[4,5],[7,1]]

It appears that you cannot use MAKEMAT to produce a desired result of

[[1,3],[4,6]]

BUT applying the method in the python example it is possible
In the original example we had M1.

We make a matrix M2 being our U
[[0,1,0],[0,0,1]] STO M2
We make a matrix M3 being our V
[[0,0],[1,0],[0,1]] STO M3

Then applying our python program U@M@V
M2*M1*M3
[[5,6],[8,9]]

Similarly apply this
We make a matrix M2 being our U
[[1,0,0],[0,0,1]] STO M2
We make a matrix M3 being our V
[[1,0],[0,0],[0,1]] STO M3

Then applying our python program U@M@V
M2*M1*M3
[[1,3],[7,9]]

Now to get [[1,3],[4,6]]
[[1,0,0],[0,1,0]] STO M2
We make a matrix M3 being our V
[[1,0],[0,0],[0,1]] STO M3

M2*M1*M3
[[1,3],[4,6]]

Thank you
Anthony, Sydney
Find all posts by this user
Quote this message in a reply
09-01-2023, 06:15 AM
Post: #6
RE: HP 40gs how to get a submartix from a matrix
(08-31-2023 11:56 PM)Anthony The Koala Wrote:  In python, I learned a way of multiplying matrices:
To illustrate:
result = U @ M @ V
Is not the same as
result = U * M * V

In the first statement, the "@" does matrix multiplication.
But in the second statement, the "*" does element-by-element multiplication.

The @ operator only works on objects of type array from numpy.
While you can multiply a list by an integer the result is probably not what you expected.

For objects of type Matrix from sympy you have to use the ordinary * operator:
Code:
P = U * M * V

This library allows to use fractions and return the result in LaTeX:
Code:
from sympy import latex

print(latex(P.inv()))

Code:
\left[\begin{matrix}-3 & 2\\\frac{8}{3} & - \frac{5}{3}\end{matrix}\right]

Or then:

\(
\left[\begin{matrix}-3 & 2\\\frac{8}{3} & - \frac{5}{3}\end{matrix}\right]
\)
Find all posts by this user
Quote this message in a reply
09-01-2023, 07:27 AM
Post: #7
RE: HP 40gs how to get a submartix from a matrix
Dear Thomas,
To get an invervse of a matrix P

import numpy as np
from numpy.linalg import inv
from sympy import latex

#code to calculate the matrix
#
#defined M1, M and M3
P = M2@M@M3
P = np.matrix(P)

#prints the output of latex output. Normally output to a tex file
print(latex(inv(P)))

Thank you
Anthony, Sydney
Find all posts by this user
Quote this message in a reply
09-03-2023, 08:02 AM (This post was last modified: 09-03-2023 03:20 PM by carey.)
Post: #8
RE: HP 40gs how to get a submartix from a matrix
(08-31-2023 04:30 AM)Anthony The Koala Wrote:  I have a 3x3 matrix:
M1 =
1,2,3
4,5,6
7,8,9

It is stored as:
[[1,2,3],[4,5,6],[7,8,9]] STO M1

Display M1
M1 ENTER

How do I get a sub-matrix such as:
M2 =
5,6
8,9

Are there any column and row operations or any indexing operations in order to get a sub matrix M2?
By indexing operations, using index and row column numbers as in arrays in python, matlab.

Thank you
Anthony of Sydney

Hi Anthony,

In answer to your original question using HP40GS commands, the command

SUB M2;M1;{2,2};{3,3}

entered on the Home screen extracts submatrix M2 from matrix M1, starting at {2,2} in a list using row, column notation and ending at {3,3}.

Before issuing the above SUB command, the original 3x3 matrix M1 and an arbitrary 2x2 M2 matrix must be created.

Attached are HP40GS emulator screenshots of the original matrix M1, arbitrary matrix M2 filled with zeros, and the Home screen showing the SUB command and the resulting sub-matrix M2 that you are seeking.

Additional Notes:
1) The above command also works within a program if the command line ends with a colon as follows
SUB M2;M1;{2,2};{3,3}:
In fact, the SUB command can be accessed from CMDS (Shift-MATH) on the keyboard which shows available program commands.
2) As the SUB command is not part of the 40GS CAS, it works also on the HP39GS (as well as the 39G+, 39G, and 38G).


Attached File(s) Thumbnail(s)
           
Find all posts by this user
Quote this message in a reply
Post Reply 




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