# matrix and vector explained to programers, JavaScript live coding matrix/vector functions 2019-12-02

- a matrix is just a function.
- a matrix is a “linear” function.

what is linear function? a linear function is a function that satisfies this property:

- f(x) + f(y) == f(x+y)
- s * f(x) == f(s * x)

where x and y are vectors, and s is a number. (vector is just a finite sequence of numbers. For example, 3d vector [6, 81, 71])

if matrix is just a function, why is it called matrix?

why is it called “linear”? because, when a function is linear, then, from a geometric point of view, the function transforms the space in a manner of a line. such as, scaling (zoom in/out), rotation, shear, flip.

best Mathematica book Buy at amazon

[etymology of matrix https://www.etymonline.com/word/matrix]

how do you actually define a matrix in code?

how do you actually compute matrix?

const lengthOfVector2D = (([x,y]) => Math.sqrt(x^2 + y^2)); const lengthOfVector3D = (([x,y,z]) => Math.sqrt(x^2 + y^2 + z^2)); const lengthOfVector4D = (([x,y,z,w]) => Math.sqrt(x^2 + y^2 + z^2 + w^2)); // generalized way to compute length in any dimension. Math.hypot is new in es2015 const f_vec_length = ((v) => Math.hypot(...v)) ; // normalize a vector means get the vector of same direction but has length 1 const normalizeVector2D = (([x,y]) => [ x/ Math.sqrt(x^2 + y^2) , y/ Math.sqrt(x^2 + y^2) ]); const normalizeVector3D = (([x,y,z]) => [ x/ Math.sqrt(x^2 + y^2 + z^2), y/ Math.sqrt(x^2 + y^2 + z^2), z/ Math.sqrt(x^2 + y^2 + z^2), ]); const normalizeVector4D = (([x,y,z,w]) => [ x/ Math.sqrt(x^2 + y^2 + z^2 + w^2), y/ Math.sqrt(x^2 + y^2 + z^2 + w^2), z/ Math.sqrt(x^2 + y^2 + z^2 + w^2), w/ Math.sqrt(x^2 + y^2 + z^2 + w^2), ]); // here's general function to compute unit vector of any dimension /* [ unit vector. v cannot be zero vector] */ // returns the unit vector of v // also known as normalize const normalizeVector = ( (v) => { const l = (Math.hypot(...v)); return v.map ( ((x) => x/l) ); } ); // can also be written in a condensed way by nested function const normalizeVector2 = ( (v) => ((l) => v.map ( ((x) => x/l) )) (Math.hypot(...v)) ); const rotate = (( [x,y], alpha) => const len = f_vec_length([x,y]); [ Math.cos(alpha) * len, Math.sin(alpha) * len ] ); // this is a unit vector [1,0] rotated by alpha // [ Math.cos(alpha) , Math.sin(alpha) ]

- matrix and vector explained to programers, in a way you can't fail to understand
- live coding JavaScript matrix/vector functions. translate the 2d to 3d ones. unit vector, normal, length, scale, rotate
- matrix is just a function. explain the math property that defines a linear function. explain geometrically why it's called linear. explain matrix multiplication.
- explain dot product. possibly explain cross product.

If you have a question, put $5 at patreon and message me.