hand writing Chinese, JavaScript coding vector dot product function style, intro to constructed language 2019-12-14

handwriting Chinese, JavaScript code dot product vector any dimension function style, intro to constructed language 2019-12-14

topic talked:

xah talk show 2019-12-14 4zth3
xah talk show 2019-12-14

xah_talk_show_2019-12-14.txt

// we are going to write a bunch of functions that does matrix and vector multiplication
// and these functions will work in any dimensions

const cos = Math.cos;
const sin = Math.sin;
const π = Math.PI;

// dot([a,b,c], [x,y,z] );
// this is what dot product means
// is a*x + b*y + c*z

// dot product of 2 vectors, vector must be 2D
const f_vec_dot_2d = (([a1,a2] , [b1,b2] ) => (a1 * b1 + a2 * b2)) ;

// dot product of 3 vectors, vector must be 3D
const f_vec_dot_3d = (([a1,a2,a3] , [b1,b2,b3] ) =>
                      (a1 * b1 + a2 * b2 + a3 * b3)) ;

// dot product of 2 vectors, vectors can be of any dimension
const f_vec_dot = (( ... vectors) =>
       vectors . reduce ((v1, v2) => v1. map ((x,i) => x * v2[i])) .
       reduce ( (a,b) => (a+b))
);

const vA = [1,2];
const vB = [3,4];
console.log ( f_vec_dot_2d(vA, vB) ); // 11
console.log ( f_vec_dot(vA, vB) ); // 11

const vC = [1,2,3];
const vD = [3,4,9];
console.log ( f_vec_dot_3d(vC, vD) ); // 11
console.log ( f_vec_dot(vC, vD) ); // 11


// vlen2D returns the length of the vector of a 2d vector
const vlen2D = ((v) => Math.hypot(...v)) ;

// f_vec_length returns the length of the vector
const f_vec_length = ((v) => Math.hypot(...v)) ;

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