Post Reply 
Some of Python's linalg commands in HP Prime
05-08-2021, 01:49 PM
Post: #1
Some of Python's linalg commands in HP Prime
Matrix Format [ [ row ], [ row ], … [ row ] ]

linspace(start, stop, number of points desired + 1)

arange(start, stop, step size); default step size: 1; returns a 1 row array from start to stop using step size

identity(n): returns an identity matrix as n x n

transpose(matrix): transpose of a matrix

inv(matrix): inverse of a matrix

shape(matrix): returns the dimensions of the matrix in an ordered pair (row, columns)

rref(matrix): row reduced echelon form of a matrix

det(matrix): determinant of a square matrix

peval(array of coefficients, x): polynomial evaluation (order is from high to low), can take complex arguments

horner(array of coefficients, x): polynomial evaluation using Horner’s method

pceoff(array of roots): returns an array representing a polynomial’s coefficients, can take complex arguments

proot(array of coefficients): returns an array of roots, can take complex arguments

add(array, array) or add(matrix, matrix): addition element by element

sub(array, array) or sub(matrix, matrix): subtraction element by element

dot(array, array): dot product

cross(array, array): cross product

imag(complex number): imaginary part – works on arrays and matrices

real(complex number): real part – works on arrays and matrices

I believe that fft and ifft have to do with fast fourier transforms.
Visit this user's website Find all posts by this user
Quote this message in a reply
05-08-2021, 11:00 PM
Post: #2
RE: Some of Python's linalg commands in HP Prime
thanks Eddie.

(05-08-2021 01:49 PM)Eddie W. Shore Wrote:  I believe that fft and ifft have to do with fast fourier transforms.

yup; the integrated Help has a few examples…

Cambridge, UK
41CL/DM41X 12/15C/16C DM15/16 17B/II/II+ 28S 42S/DM42 32SII 48GX 50g 35s WP34S PrimeG2 WP43S/pilot/C47
Casio, Rockwell 18R
Find all posts by this user
Quote this message in a reply
05-09-2021, 05:36 PM
Post: #3
RE: Some of Python's linalg commands in HP Prime
Those sound like the function names from the Prime CAS and the 50g.

BTW, pcoeff is misspelled. Smile
Find all posts by this user
Quote this message in a reply
05-09-2021, 05:58 PM
Post: #4
RE: Some of Python's linalg commands in HP Prime
(05-09-2021 05:36 PM)John Keith Wrote:  Those sound like the function names from the Prime CAS and the 50g.

BTW, pcoeff is misspelled. Smile

Sorry. Good catch!
Visit this user's website Find all posts by this user
Quote this message in a reply
05-11-2021, 06:47 PM
Post: #5
RE: Some of Python's linalg commands in HP Prime
oops, sorry, my comment re Help of course refers to the CAS commands, whereas this thread is about Python… I need more coffee.

Cambridge, UK
41CL/DM41X 12/15C/16C DM15/16 17B/II/II+ 28S 42S/DM42 32SII 48GX 50g 35s WP34S PrimeG2 WP43S/pilot/C47
Casio, Rockwell 18R
Find all posts by this user
Quote this message in a reply
09-08-2021, 10:10 PM
Post: #6
RE: Some of Python's linalg commands in HP Prime
(05-08-2021 01:49 PM)Eddie W. Shore Wrote:  Matrix Format [ [ row ], [ row ], … [ row ] ]

linspace(start, stop, number of points desired + 1)

arange(start, stop, step size); default step size: 1; returns a 1 row array from start to stop using step size

identity(n): returns an identity matrix as n x n

transpose(matrix): transpose of a matrix

inv(matrix): inverse of a matrix

shape(matrix): returns the dimensions of the matrix in an ordered pair (row, columns)

rref(matrix): row reduced echelon form of a matrix

det(matrix): determinant of a square matrix

peval(array of coefficients, x): polynomial evaluation (order is from high to low), can take complex arguments

horner(array of coefficients, x): polynomial evaluation using Horner’s method

pceoff(array of roots): returns an array representing a polynomial’s coefficients, can take complex arguments

proot(array of coefficients): returns an array of roots, can take complex arguments

add(array, array) or add(matrix, matrix): addition element by element

sub(array, array) or sub(matrix, matrix): subtraction element by element

dot(array, array): dot product

cross(array, array): cross product

imag(complex number): imaginary part – works on arrays and matrices

real(complex number): real part – works on arrays and matrices

I believe that fft and ifft have to do with fast fourier transforms.

Hi, I found it difficult to transpose a matrix with PYTHON. For instance:
transpose ([[1,2,3], [4,5,6]]) gives me this result: [[1,4], [3,2], [5,6]]
The correct result is: [[1,4], [2,5], [3,6]].
Is there a bag perhaps?
Find all posts by this user
Quote this message in a reply
09-10-2021, 04:55 AM
Post: #7
RE: Some of Python's linalg commands in HP Prime
There is indeed a bug in transpose for non square matrices.
Find all posts by this user
Quote this message in a reply
09-11-2021, 09:13 AM
Post: #8
RE: Some of Python's linalg commands in HP Prime
(09-10-2021 04:55 AM)parisse Wrote:  There is indeed a bug in transpose for non square matrices.

Thanks for the answer, Parisse. Since I had to use the "transpose" command in my Python program, I had to write a subroutine to transpose the arrays:

Code:

from linalg import *

def transposeRr(matriceRr):
    L=shape(matriceRr);
    r=L[0];
    c=L[1];
    matricezero=zeros(c,r);
    for u in range(0,r):
        for uu in range (0,c):
            matricezero[uu][u]=matriceRr[u][uu];
    return matricezero;
Find all posts by this user
Quote this message in a reply
09-11-2021, 10:36 AM
Post: #9
RE: Some of Python's linalg commands in HP Prime
(09-11-2021 09:13 AM)robmio Wrote:  Since I had to use the "transpose" command in my Python program, I had to write a subroutine to transpose the arrays ...

Is matrix simply list of list ? If yes, we can transpose with a 1-liner.

>>> transpose = lambda a: [list(r) for r in zip(*a)]
>>> transpose([[1,2,3], [4,5,6]])
[[1, 4], [2, 5], [3, 6]]
Find all posts by this user
Quote this message in a reply
09-11-2021, 11:17 AM
Post: #10
RE: Some of Python's linalg commands in HP Prime
(09-11-2021 10:36 AM)Albert Chan Wrote:  
(09-11-2021 09:13 AM)robmio Wrote:  Since I had to use the "transpose" command in my Python program, I had to write a subroutine to transpose the arrays ...

Is matrix simply list of list ? If yes, we can transpose with a 1-liner.

>>> transpose = lambda a: [list(r) for r in zip(*a)]
>>> transpose([[1,2,3], [4,5,6]])
[[1, 4], [2, 5], [3, 6]]

Congratulations! This short solution you proposed works very well in my program
Find all posts by this user
Quote this message in a reply
10-27-2022, 07:43 PM (This post was last modified: 10-27-2022 07:55 PM by Ioncubekh.)
Post: #11
RE: Some of Python's linalg commands in HP Prime
matrix() doesn't follow rules of matrix dot product. Possible bug related to post#7

Code:
from linalg import *
M1= matrix([ [1,2,3], [10,100,1000] ])

# Sum of rows of any Matrix
EachRowSum= dot( M1, ones( len(M1[0]), 1 ) )

# Sum of columns of any Matrix ... NOT WORKING
EachColSum= dot( ones( 1, len(M1) ), M1 )

HP Prime G1
Python via Android Termux...
Find all posts by this user
Quote this message in a reply
11-02-2022, 02:20 AM
Post: #12
RE: Some of Python's linalg commands in HP Prime
In the absence of multi-dimensional arrays (numpy) following commands using native zip(), map(), lambda, list constructions ...can be used to have similar effect while considering dimensions as well.
Few array examples & their equivalents

Code:

# x + b21 * k1 + b32 * k2
# where x, k1 & k2 are numpy arrays while b21 & b32 is some scalar value
[sum(i) for i in zip(x, [b21*i for i in k1], [b32*i for i in k2] )]
# r  / ( q*( abs(x)+abs(k1) ) )
# where r, x & k1 are equal shaped arrays & q is scalar value
[ (r[i] / ( q*( abs(x[i]) + abs(k1[i]) ) ) ) for i in range(len(x) ) ]

HP Prime G1
Python via Android Termux...
Find all posts by this user
Quote this message in a reply
Post Reply 




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