[back]

Simple matrix calculation

PAIDA supports simple matrix calculations.

### Create 2*2 matrix
>> from paida.tools import Matrix
>> result1 = Matrix.create([[1.0, 2.0], [3.0, 4.0]])
>> result1
[[1.0, 2.0],
[3.0, 4.0]]

### 3*3 linked matrix
>> result2 = Matrix.create([[1.0, 2.0, 3.0], [2,0, 3.0, 4.0], [3.0, 4.0, 5.0]], link = True)
>> result2
[[1.0, 2.0, 3.0],
[2,0, 3.0, 4.0],
[3.0, 4.0, 5.0]]
>> result3 = result2[1:, 1:]
>> result3
[[3.0, 4.0],
[4.0, 5.0]]
>> result3[0, 0] = 9.0
>> result3
[[9.0, 4.0],
[4.0, 5.0]]
>> result2
[[1.0, 2.0, 3.0],
[2,0, 9.0, 4.0],
[3.0, 4.0, 5.0]]

### 3*3 Unit matrix
>> result4_1 = Matrix.createUnit(3)
>> result4_2 = Matrix.createUnit(3, link = True)

### Basic operation
>> result1 = Matrix.create([[1.0, 2.0], [3.0, 4.0]])
>> result1
[[1.0, 2.0],
[3.0, 4.0]]
>> result2 = Matrix.create([[2.0, 3.0], [4.0, 5.0]])
>> result2
[[2.0, 3.0],
[4.0, 5.0]]
>> result1 + result2
[[3.0, 5.0],
[7.0, 9.0]]
>> result1 - result2
[[-1.0, -1.0],
[-1.0, -1.0]]
>> result1 * result2 ### outputs as expected! (Scipy array doesn't)
[[10.0, 13.0],
[22.0, 29.0]]

### Some useful methods
>> result1.nRows()
2
>> result1.nColumns()
2
>> result1.toList()
[[1.0, 2.0], [3.0, 4.0]]
>> result1.setLint(True) ### changes to linked matrix
>> result1.setLint(False) ### changes to unlinked matrix
>> result1.inverse()
[[-2.0, 1.0],
[1.5, -0.5]]
>> result1.transpose()
[[1.0, 3.0],
[2.0, 4.0]]
>> result1.determinant()
-2.0
>> result1.cofactor(0, 0)
4.0
>> result1.eigenvalues() ### returns eigenvalues and eigenvectors
([(-0.37228132326901431, 0.0), (5.3722813232690143, 0.0)], [[-0.82456484013239373, 0.56576746496899233], [-0.41597355791928431, -0.90937670913212421]])

The eigenvalue is represented in the tuple form (Re, Im) as complex number. Then, in this case, the result is:
([eigenvalue1, eigenvalue2], [eigenvector1, eigenvector2])

Memo : Getting eigenvalues of very large size matrix will fail or take very very long time.

[back]