Monday, October 3, 2022

Matrices in Python without Numpy: Part 1

Matrices in Python without Numpy:  Part 1


Python is a wonderful programming language and is a welcome addition to graphing calculators such as:

*  TI nSpire CX II  and TI nSpire CX II CAS

*  TI-84 Plus CE Python (it's become hard to find and hopefully it won't be the case in 2023)

*  HP Prime

*  Casio fx-CG 50

*  Casio fx-9750GIII and fx-9860GIII

*  Numworks

*  From France:  TI-83 Premium Python, TI-82 Advanced Edition Python, Casio Graph fx-35+ E II

As I understand at time, there is no numpy module for any of the calculators*.   

But we want to work with matrices.  So we will need to program code to allow work with matrices.  Welcome to the Matrices in Python without Numpy series!

* There is a linalg module for the HP Prime.  

This series will cover:

*  creating matrices and other basics

*  adding and multiplying matrices

*  determinant and inverse of 2 x 2 and 3 x 3 matrices

*  upper triangular matrices and general determinant

In this series, a single Python file is created,   Each of the commands are going to be a separate function within a define structure.   This allows to imported into other Python scripts by from matrix import *.  

Entering Matrices

Use square brackets to enter matrices:  

[ [ M11, M12, M13, ... ] , [ M21, M22, M23, ... ] , [ M31, M32, M33, ... ] ] 

Each row enclosing elements per column, and each row is separated by a comma.  All the rows are enclosed in square brackets.  

In this sense, matrices in this sense are nested lists.  Call elements by:

Call a row:   matrix[row]

Call the last row:  matrix[-1]

Call an element:  matrix[row][column]

Number of rows:  len(matrix)

Number of columns:  len(matrix[0])

Remember, in Python, indices start with 0 and go to row-1, column-1.  We will stick with the index convention to stay consistent.  

Screenshots are made using an emulator on

Creating and Printing Matrices

newmatrix(number of rows, number of columns).  

The matrix will be filled with zeros.


def newmatrix(r,c):

  M = []

  while len(M)<r:


    while len(M[-1])<c:


  return M


Creates an identity matrix, a square matrix with ones in the diagonal, zeroes everywhere else.  The identity matrix a fundamental matrix.


def identity(n):

  M = newmatrix(n,n)

  for i in range(n):


  return M

So far, we see resulting matrices in a row or scroll off the screen.  Let's make it so we can see matrices as they are written.  


Prints a matrix in text form.

def mprint(M):

  for r in M:

    print([x+0 for x in r])

List comprehension is a very powerful tool in Python.

In the following examples, we will store a 3 x 3 matrix:

mat1 = [ [ 1, 2, 3 ] , [ 4, 5, 6 ] , [ 7, 8, 9 ] ] 


The transpose function flips a matrix on it's diagonal.  For each element:

M^T:   M(row, col) → M^T(col, row)

def transpose(M):

  # get numbers of rows


  # get number of columns


  # create transpose matrix


  for i in range(r):

    for j in range(c):


  return MT

scalar(matrix, factor)

Next, we have scalar multiplication, multiply each element of the matrix by the factor.

def scalar(M,s):




  for i in range(r):

    for j in range(c):


  return MT


Finally, we have the a trace of a matrix.  The trace is the sum of all the diagonals of a square matrix.  If the matrix is not square, an error occurs.

def mtrace(M):




  if r !=c:

    return "Not a square matrix"



    for i in range(r):


    return t

In Python != means not equal.  

Coming in Part 2, let's add and multiply matrices.  

Happy computing,



Ives, Thom.  "BASIC Linear Algebra Tools in Pure Python without Numpy or Scipy"  Integrated Machine Learning & AI  December 11, 2018.

All original content copyright, © 2011-2022.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 

HP 20S: Gamma Function Approximation (Stirling's Formula)

HP 20S:  Gamma Function Approximation (Stirling's Formula) Introduction The gamma function uses the approximation sequence: Let t = x + ...