PMMatrix in Pharo: Usage

Pharo is a language that is better than other languages like Python in many ways.

For starters, the whole syntax of Pharo fits on a postcard. Pharo has a built-in live coding IDE that allows you to inspect and modify the code while it is running. Pharo is purely object-oriented which makes it really convenient to use.

But Pharo is a new language and doesn’t have as many libraries as languages like Python have.

Here we will see the package, Polymath that supports scientific computations in Pharo similar to the library, NumPy in Python.

We will be specifically looking into the usage of the PMMatrix class in Polymath that handles most of the matrix computations.

Initializing a 3x4 null matrix

aPMMatrix := PMMatrix rows: 3 columns: 4.

Initializing a specific matrix

To initialize a matrix as
[ 1 2]
[ 3 4]

aPMMatrix := PMMatrix rows: #(#(1 2) #(3 4)).

Similar to,

arr = numpy.array([[1,2],[3,4]])

Initializing a 2x3 matrix with ones

aPMMatrix := PMMatrix onesRows: 2 cols: 3.

Similar to,

a = numpy.ones([2,3],dtype = int)

Initializing a 2x3 matrix with zeroes

aPMMatrix := PMMatrix zerosRows: 2 cols: 3.

Similar to,

a = numpy.zeros([2,3],dtype = int)

Initializing a 3x4 matrix with random values

aPMMatrix := PMMatrix rows: 3 columns: 4 random: 10.

Here, the values in the matrix will always be less than or equal to 10.
In NumPy, something similar would be,

a = numpy.random.rand(3,4)

Initializing a 2x3 matrix with one specific value

We are filling the whole 2x3 matrix with 10 here.

aPMMatrix := PMMatrix rows: 2 columns: 3 element: 10.

Similar to,

a = numpy.full([2,3],10)

Dimension of a matrix

To find the dimension of a matrix,

aPMMatrix dimension.

In NumPy, it would be


Matrix Product

To get the matrix product of matrices a and b,

c := a*b.

Note that here, b could also be a number instead of a matrix.

Element wise product

To multiply two matrices a and b element-wise,

c := a hadamardProduct: b.

Sum of rows

To find the sum of all rows of a matrix,

aPMMatrix sum.

Similar to numpy.sum()

Trace of a matrix

Similar to numpy.trace(), in Polymath, we can do

aPMMatrix tr.

Specify a condition on elements in matrix

" returns a boolean matrix with True if the element is lesser than 10 and False otherwise"
aPMMatrix < 10
"checking if values are equal to 10"
aPMMatrix = 10

This is exactly similar to,

a < 10 
a == 10

Argmax on columns and rows

To find argmax on all columns and rows of a matrix,

aPMMatrix argMaxOnColumns.
aPMMatrix argMaxOnRows.

Similar to numpy.argmax()

Flattening a matrix

To flatten a matrix on rows,

aPMMatrix flattenRows.

To flatten a matrix on columns,

aPMMatrix flattenColumns

Similar to numpy.flatten()

Accessing values

To access a value of a matrix at row 1, column 2

aPMMatrix at: 1 at: 2.

Note that in Pharo, we always follow 1th-indexing unlike Python

Changing values

To change the value at row 1, column 2 to a new element 10

aPMMatrix at: 1 at: 2 put: 10.

Accessing columns and rows

To get the 2nd column in a matrix,

aPMMatrix atColumn:  2.

Transpose of a matrix

In NumPy, we have numpy.transpose(). Similarly in Pharo,

aPMMatrix transpose.

Finding Determinant of a matrix

aPMMatrix determinant.

Finding Inverse of a matrix

aPMMatrix inverse.

Similar to numpy.linalg.inv()

Checking closeness of matrices

We can check if 2 matrices a and b are close to each other by a precision.
More formally, we check if the sum of the absolute values of the differences of each corresponding elements in the matrix is less than the given precision.

a closeTo: b precision: 0.0001.

Similar to numpy.isclose() in NumPy

Getting the Principal Diagonal

To get the principal diagonal of a matrix as a vector,

aPMMatrix principalDiagonal.

Similar to numpy.diagonal()

Rank of a matrix

Finding the rank of a matrix can be done by,

aPMMatrix rank.

Similar to numpy.linalg.matrix_rank() in NumPy.

Matrix Orthogonalization

To orthogonalize a matrix,

aPMMatrix orthogonalize.

Cholesky Decomposition

To perform cholesky decomposition on a matrix

aPMMatrix choleskyDecomposition.

Similar to numpy.linalg.cholesky()

QR Factorization

To perform QR decomposition of a matrix,

aPMMatrix qrFactorization.

These are some of the basic usages of the class PMMatrix in the Polymath package. Check out the Github repository for more.

If you have doubts, you can always ask them in the #polymath channel in the Pharo Discord community.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store