Vectors and matrices

Element-wise against matrix arithmetic, indexing, and the aggregates.

v=[3412]
w=[123]

Between two vectors of equal length, * is element-wise: what a tabulated calculation wants, column against column.

elementwise=v·w=[3, 4, 12]·[1, 2, 3]=[3, 8, 36]

An inner product is asked for by name, so it is never confused with the above.

inner=dot⁡(v,w)=dot⁡([3, 4, 12],[1, 2, 3])=47

Scalars broadcast.

scaled=2·v=2·[3, 4, 12]=[6, 8, 24]

Aggregates.

total=sum⁡(v)=sum⁡([3, 4, 12])=19
smallest=min⁡(v)=min⁡([3, 4, 12])=3
largest=max⁡(v)=max⁡([3, 4, 12])=12
n=length⁡(v)=length⁡([3, 4, 12])=3

Indexing is one-based.

second=v2=([3, 4, 12])2=4

A vector is the column of n that rows and cols say it is, so it takes the column index as well: these are the same element. Any other column is out of bounds, which is what a column of one means.

alsosecond=v2,1=([3, 4, 12])2,1=4

Vectors carry units element by element, so a tabulated calculation keeps them.

f=[51015]⁢Hz
acc=[2.893.043.72]⁢ms2
stroke=acc(2·π·f)2=[2.89 m·s⁻², 3.04 m·s⁻², 3.72 m·s⁻²](2·π·[5 Hz, 10 Hz, 15 Hz])2=[0.00292818 m, 0.000770041 m, 0.000418794 m]
stroke=[2.92818 mm, 0.770041 mm, 0.418794 mm]

A matrix is a vector of rows, and is indexed row first.

K=[1234]
k21=K2,1=([[1, 2], [3, 4]])2,1=3

Between two matrices * is the matrix product, not element-wise. identity(n) writes the n×n one, which is dimensionless so that S - λ*identity(2) — the characteristic equation of a stress tensor — takes its dimension from λ.

I=identity⁡(2)=[[1, 0], [0, 1]]
product=K·I=[[1, 2], [3, 4]]·[[1, 0], [0, 1]]=[[1, 2], [3, 4]]

diag writes a matrix with a vector down its diagonal — a mass matrix, here. The zeros carry the diagonal's dimension so the result is usable in arithmetic.

mass=diag⁡([34]⁢kg)=[[3 kg, 0 kg], [0 kg, 4 kg]]

Linear algebra.

d=det⁡(K)=det⁡([[1, 2], [3, 4]])=-2
Kt=transpose⁡(K)=transpose⁡([[1, 2], [3, 4]])=[[1, 3], [2, 4]]
Kinv=inv⁡(K)=inv⁡([[1, 2], [3, 4]])=[[-2, 1], [1.5, -0.5]]

The check that inv is really an inverse.

backto_I=K·Kinv=[[1, 2], [3, 4]]·[[-2, 1], [1.5, -0.5]]=[[1, -1.11022e-16], [8.88178e-16, 1]]

The cross product is three-dimensional, and units come out of the products it is made of: metres crossed with newtons give newton-metres. -> N*m asks for the moment to be shown that way rather than in joules, which share the dimension but not the meaning.

r=[100]⁢m
F=[020]⁢N
M=cross⁡(r,F)=cross⁡([1 m, 0 m, 0 m],[0 N, 2 N, 0 N])=[0 N*m, 0 N*m, 2 N*m]

Parallel vectors cross to zero, which is the check worth writing down.

parallel=cross⁡(r,r)=cross⁡([1 m, 0 m, 0 m],[1 m, 0 m, 0 m])=[0 m², 0 m², 0 m²]

Shape and slicing. A vector answers as the column it is.

n=rows⁡(K)=rows⁡([[1, 2], [3, 4]])=2
second=col⁡(K,2)=col⁡([[1, 2], [3, 4]],2)=[2, 4]

Joining: side by side, and one above the other.

side=augment⁡([12]⁢m,[34]⁢m)=[[1 m, 3 m], [2 m, 4 m]]
piled=stack⁡([12]⁢m,[34]⁢m)=[1 m, 2 m, 3 m, 4 m]