The Museum of HP Calculators

HP Forum Archive 20

[ Return to Index | Top of Index ]

matrix element operation
Message #1 Posted by Sok-khieng Chum Hun on 21 May 2011, 11:58 a.m.

Hi, is it possible to do matrix element operation on the hp48 or hp50g?

In exemple, in matlab, if A = [1 2 3] and B = [2 3 4]

A.*B gives [1*2 2*3 3*4] -> [2 6 12]

Or A./B gives [1/2 2/2 3/4] -> [.5 1 .75]

Or A.^B gives [1^1 2^3 3^4] -> [1 8 81]

Thank you :)

      
Re: matrix element operation
Message #2 Posted by Jose Gonzalez Divasson on 21 May 2011, 2:32 p.m.,
in response to message #1 by Sok-khieng Chum Hun

In HP50g at least it exists the function HADAMARD, that multiplies 2 matrices term by term. Very useful for business purposes, but not for physics or mathematics.

I don't know how to divide, because 1/[matrix] is a defined operation - and different.

      
Re: matrix element operation
Message #3 Posted by Thomas Klemm on 21 May 2011, 3:25 p.m.,
in response to message #1 by Sok-khieng Chum Hun

You could use lists instead of vectors:

1st example: A * B

4: {1 2 3} 3: {2 3 4} 2: 2 1: \<< * \>> DOLIST

(PRG -> LIST -> PROC -> DOLIS)

Cheers
Thomas

PS: I guess the next question will be, how to turn a vector into a list:

\<<
  OBJ\->
  OBJ\->
  DROP
  \->LIST
\>>
      
Re: matrix element operation
Message #4 Posted by Donald Ingram on 21 May 2011, 3:33 p.m.,
in response to message #1 by Sok-khieng Chum Hun

Enter the values as lists using { } or 1 2 3 3 ->LIST and you get the results you desire.

      
Re: matrix element operation
Message #5 Posted by Gilles Carpentier on 21 May 2011, 5:36 p.m.,
in response to message #1 by Sok-khieng Chum Hun

Hi,

On the 50G you can use the HADAMARD command for this type of matrices multiplication :

[1 2 3] [2 3 4] HADAMARD

-> [2 6 12 ]

To transform LIST <-> MATRICE, uses AXL

[ 1 2 3 ] AXL -> { 1 2 3 }

{ 1 2 3 } AXL -> [ 1 2 3 ]

[1 2 3] 'a' STO [2 3 4] 'b' STO

a b HADAMARD

a AXL b AXL / AXL

a AXL b AXL ^ AXL

You can create news commands

AXL SWAP AXL SWAP / AXL 'MDiv' STO

AXL SWAP AXL SWAP ^ AXL 'MPuis' STO

There is no need of DOLIST in this case. If you use lists instead of matrices * / and ^ does the job

{ 1 2 3 } { 2 3 4 } * -> { 2 6 12 }

{ 1 2 3 } { 2 3 4 } / -> { 1/2 2/3 3/4 }

{ 1 2 3 } { 2 3 4 } ^ -> { 1 8 81 }

Symbolic are allowed

{ 1 2 3 } { 'A' 'B' 'C' } ^ -> { 1^A 2^B 3^C }

Edited: 21 May 2011, 5:59 p.m.

            
Re: matrix element operation
Message #6 Posted by Thomas Klemm on 21 May 2011, 6:32 p.m.,
in response to message #5 by Gilles Carpentier

Quote:
There is no need of DOLIST in this case.

Thanks for the hint. This works also with the 48G.

Quote:
Symbolic are allowed

{ 1 2 3 } { 'A' 'B' 'C' } ^ -> { 1^A 2^B 3^C }


I get the following with the 48G:

{ 1 2 3 } { 'A' 'B' 'C' } ^ -> { 1 '2^B' '3^C' }

Cheers
Thomas

                  
Re: matrix element operation
Message #7 Posted by Gilles Carpentier on 21 May 2011, 6:54 p.m.,
in response to message #6 by Thomas Klemm

>I get the following with the 48G:

>{ 1 2 3 } { 'A' 'B' 'C' } ^ -> { 1 '2^B' '3^C' }

>Cheers >Thomas

Seems different on the 50

1 'A' ^ -> 1^A

EVAL -> 1

(I suppose that 'A' don't exists)

      
Re: matrix element operation
Message #8 Posted by Oliver Unter Ecker on 22 May 2011, 6:00 a.m.,
in response to message #1 by Sok-khieng Chum Hun

Not regarding the devices you ask, but probably still helpful:

On ND1, an iOS app with a feature set that closely resembles a 50g without CAS but with other additions (incl. programmability in RPL+ and JavaScript), "lists" and "vectors" are unified, and [1 2 3] [2 3 4] * will return [2 6 12].

"+", "-", "1/x" and all single-valued operators and functions (such as "sin", "!", "loggamma", ...(a list of >100)) work on both real and symbolic vectors.

This also works with large vectors (of, say, 10^5 elements).

The DOLIST and DOSUBS techniques mentioned also work, and, as on the 48 and 50g, can be used on more than two vectors at once.

48, 50g, ND1 have other vector-processing type functionality, btw, sometimes hiding under unusual names. The common "fold" operation is known as STREAM, for example.

      
Re: matrix element operation
Message #9 Posted by C.Ret on 22 May 2011, 4:25 p.m.,
in response to message #1 by Sok-khieng Chum Hun

Hi,

I also use MatLab, but I own only an HP-28S. That why to get .* .+ ./ or any vectorialized operation on matrix or line/column vector on my HP28, I have to use a dedicated program:

Considering two matrix/vectors A and B of the same shape :

A B  *  EXE2 returns matrix/vector A.*B
A B  /  EXE2 returns matrix/vector A./B
A B  ^  EXE2 returns matrix/vector A.^B
etc

where EXE2 is : -> B prg 1 @@ initialize i at first position DO DUP2 GET @@ A i GET OVER B SWAP GET @@ B i GET prg EVAL @@ execute 'prg' for each i-element PUTI @@ store result UNTIL DUP 1 == END @@ loop for each i-element DROP 'EXE2' STO

This way also si possible on HP48/49/50 and you dont have to worry about objet type list or array ! The only restriction is that the function pick exactly two arguments in the stack and returns only one (exactly as * / ^ + - etc. ) and a lot of dyadic functions or programs.

[ 1 2 3 ]   [ 10 20 30 ]  *   EXE2 is  [10 40 90]

[ 1 2 3 ] { 10 20 30 } * EXE2 is [10 40 90]

{ 1 2 3 } { 10 20 30 } * EXE2 is {10 40 90}

[[ 1 2 3 ]] { 10 20 30 } * EXE2 is [[10 40 90]]

[[ 1 ] [[ 10 ] [ 2 ] [ 40 ] [ 3 ]] [ 10 20 30 ] * EXE2 is [ 90 ]]

[ 1 2 3 ] [[ 10 ] [ 20 ] [ 30 ]] * EXE2 is [10 40 90]

'A' { 10 20 30 } * EXE2 is 'A' but comptutation have change contents of register 'A'

Edited: 22 May 2011, 4:48 p.m.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall