Created
July 28, 2017 16:29
-
-
Save andreisfedotov/9260799b400e45ec9a2fda383fac45d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class VectorOperations { | |
public static addition(vec1: number[], vec2: number[]) { | |
let res = []; | |
for (let i = 0; i < vec1.length; i++) { | |
res.push(vec1[i] + vec2[i]); | |
} | |
return res; | |
} | |
public static substraction(vec1: number[], vec2: number[]) { | |
let res = []; | |
for (let i = 0; i < vec1.length; i++) { | |
res.push(vec1[i] - vec2[i]); | |
} | |
return res; | |
} | |
public static multiplication(vec1: number[], vec2: number[]) { | |
let res = []; | |
for (let i = 0; i < vec1.length; i++) { | |
res.push(vec1[i] * vec2[i]); | |
} | |
return res; | |
} | |
public static division(vec1: number[], vec2: number[]) { | |
let res = []; | |
for (let i = 0; i < vec1.length; i++) { | |
if (vec2[i] != 0) { | |
res.push(vec1[i] / vec2[i]); | |
} | |
else { | |
res.push(undefined); | |
} | |
} | |
return res; | |
} | |
public static scalarAddition(vector: number[], scalar: number) { | |
let res = []; | |
for (let i = 0; i < vector.length; i++) { | |
res.push(vector[i] + scalar); | |
} | |
return res; | |
} | |
public static scalarSubstraction(vector: number[], scalar: number) { | |
let res = []; | |
for (let i = 0; i < vector.length; i++) { | |
res.push(vector[i] - scalar); | |
} | |
return res; | |
} | |
public static scalarMultiplication(vector: number[], scalar: number) { | |
let res = []; | |
for (let i = 0; i < vector.length; i++) { | |
res.push(vector[i] * scalar); | |
} | |
return res; | |
} | |
public static exponentiation(vector: number[], exp: number) { | |
let res = []; | |
for (let i = 0; i < vector.length; i++) { | |
res.push(Math.pow(vector[i], exp)); | |
} | |
return res; | |
} | |
public static expByVector(scalar: number, vector: number[]) { | |
let res = []; | |
for (let i = 0; i < vector.length; i++) { | |
res.push(Math.pow(scalar, vector[i])); | |
} | |
return res; | |
} | |
public static min(vector: number[], scalar: number) { | |
let res = []; | |
for(let i = 0; i < vector.length; i++) { | |
(vector[i] < scalar) ? res.push(vector[i]) : res.push(scalar); | |
} | |
return res; | |
} | |
public static median(vector: number[], notNullValues: boolean) { | |
let res = []; | |
if(notNullValues) { | |
vector = vector.filter( (v) => { return v != null }); | |
} | |
vector.sort((a, b) => {return a - b;}); | |
let half = Math.floor(vector.length / 2); | |
return (vector.length % 2) | |
? vector[half] | |
: (vector[half - 1] + vector[half]) / 2.0; | |
} | |
public static mean(vec1: number[], vec2: number[]) { | |
if (vec1.length != vec2.length) { | |
return; | |
} | |
let res = []; | |
let length = vec1.length; | |
for(let i = 0; i < length; i++) { | |
res.push((vec1[i] + vec2[i])/2); | |
} | |
return res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment