Linear algebra

Table of contents

  1. Monomorphic arrays
  2. Polymorphic arrays
  3. Vector algebra
  4. Matrix algebra
  5. Column and row vectors
  6. Complex numbers
  7. Matrix inversion
  8. Type conversion
  9. Indexing

Monomorphic arrays

Module la provides an array data type which can be used to calculate with coordinate vectors and matrices.

use la: vector, matrix

v = vector(1,2)

A = matrix(
  [1,2],
  [3,4]
)

print(A*v)

Polymorphic arrays

Module math.la provides the polymorphic version of the array data type.
use math.la: matrix

A = matrix(
  [1,2],
  [3,4]
)

print(A^40)

# Output (long integers instead of floating point numbers):
# matrix(
#   [38418114959269691024862069751, 55991602170538933080248818850],
#   [83987403255808399620373228275, 122405518215078090645235298026]
# )

It is possible to combine this with rational numbers. A rational number a/b is denoted as rat(a,b).

use math.la: matrix
use math.rational: rat

A = matrix(
  [rat(4,1),rat(2,3)],
  [rat(9,5),rat(1,2)]
)

print(A^2)

# Output:
# matrix(
#   [86/5, 3],
#   [81/10, 29/20]
# )

Vector algebra

Basic operations are stated directly.

use math.la: vector

a = vector(1,2)
b = vector(3,4)

# Linear combination
c = 2*a+4*b

# Scalar product
s = a*b

# Absolute value
r = abs(a)

# Unit vector
e = a/abs(a)

The orthogonal projection of v onto a line of direction u is given by:

P = |u,v| (u*v)/(u*u)*u

Matrix algebra

Basic operations are stated directly.

use math.la: vector, matrix, scalar
use math: pi, sin, cos

A = matrix(
  [1,2],
  [3,4]
)

rot = |phi| matrix(
  [cos(phi),-sin(phi)],
  [sin(phi),cos(phi)]
)

deg = pi/180
B = rot(90*deg)

# Multiplication matrix*vector
v = vector(1,2)
w = A*v

# Multiplication matrix*matrix
C = A*B

# Matrix power
C = A^2

# Transposition
C = A.T

# Trace
t = A.tr

# n×n identity matrix
E = scalar(n,1,0)

Column and row vectors

Column and row matrices may be used as coordinate vectors.

use math.la: matrix

# A row vector
v = matrix([1,2])

# A col vector
w = matrix([3,4]).T

# Scalar product
s = v*w

# Outer product
M = w*v

Complex numbers

Vectors and matrices of complex numbers are handled almost like the real versions, with the difference that scalar products need an explicit conjugation operation.

use math.la: vector, matrix

v = vector(1+2i,3+4i)
w = vector(5+6i,7+8i)

# Scalar product
s = v.conj*w

# Absolute value
r = abs(v)

A = matrix(
  [1+2i, 3+4i]
  [5+6i, 7+8i]
)

# Conjugate matrix
B = A.conj

# Conjugate transpose
B = A.T.conj
B = A.H

# Bra-ket notation
ketv = matrix([1+2i,3+4i]).T
ketw = matrix([5+6i,7+8i]).T
brav = ketv.H
braw = ketw.H

# Scalar product
s = brav*ketw

# Absolute value
# (uses Frobenius norm for compability with vectors)
r = abs(ketv)

Matrix inversion

There are functions, providing computation of determinant and inverse of a matrix.

use math.la: matrix
use math.la.inversion: det, inv

A = matrix(
  [1,2,3],
  [4,5,6],
  [7,7,9]
)

print(det(A))
print(inv(A))

Type conversion

Often one needs to convert between different representations.

use math.la: vector, matrix, array

# Turning a list into a vector
v = vector(*a)

# Turning a vector into a list
a = v.list

# Turning a ragged array into a matrix
A = matrix(*a)

Indexing

Vectors are indexed like lists.

v = vector(1,2)
print(v[0])

Sometimes, rather than indexing a vector, the elements should be unpacked into named coordinates.

# Unpacking an indexed object of unknown type
[x,y] = v

# Unpacking a function's argument
f = |[x,y]| x^2+y^2
print(f(vector(1,2)))