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.
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)).
arr = numpy.array([[1,2],[3,4]])
Initializing a 2x3 matrix with ones
aPMMatrix := PMMatrix onesRows: 2 cols: 3.
a = numpy.ones([2,3],dtype = int)
Initializing a 2x3 matrix with zeroes
aPMMatrix := PMMatrix zerosRows: 2 cols: 3.
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.
a = numpy.full([2,3],10)
Dimension of a matrix
To find the dimension of a matrix,
In NumPy, it would be
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,
Similar to numpy.sum()
Trace of a matrix
Similar to numpy.trace(), in Polymath, we can do
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,
Similar to numpy.argmax()
Flattening a matrix
To flatten a matrix on rows,
To flatten a matrix on columns,
Similar to numpy.flatten()
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
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,
Finding Determinant of a matrix
Finding Inverse of a matrix
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,
Similar to numpy.diagonal()
Rank of a matrix
Finding the rank of a matrix can be done by,
Similar to numpy.linalg.matrix_rank() in NumPy.
To orthogonalize a matrix,
To perform cholesky decomposition on a matrix
Similar to numpy.linalg.cholesky()
To perform QR decomposition of a matrix,
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.