-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.js
More file actions
106 lines (104 loc) · 2.09 KB
/
math.js
File metadata and controls
106 lines (104 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
Math.dotproduct=function(a,b){
if(a.length==b.length){
dotproduct=0;
for(var x=0;x<a.length;x++){
dotproduct+=a[x]*b[x];
}
return dotproduct;
}
else{
console.log("Arrays are not equal length");
return;
}
}
Math.matrix={};
Math.matrix.analyse=function(a){
if(Array.isArray(a)){
var a={matrix:a};
a.rows=a.matrix.length;
a.cols=a.matrix[0].length;
for(var x=1;x<a.rows;x++){
if(a.cols!=a.matrix[x].length){
console.log("Inconsistent number of columns in matrix");
return;
}
}
return a;
}
else if(!Array.isArray(a) && typeof(a)=="object"){
return a;
}
}
Math.matrix.transpose=function(a){
var a=Math.matrix.analyse(a);
transpose=[];
for(var x=0;x<a.cols;x++){
transpose[x]=[];
for(var y=0;y<a.rows;y++){
transpose[x][y]=a.matrix[y][x];
}
}
return transpose;
}
Math.matrix.add=function(a,b){
var a=Math.matrix.analyse(a);
var b=Math.matrix.analyse(b);
if(a.rows==b.rows){
if(a.cols==b.cols){
var add=[];
for(var x=0;x<a.rows;x++){
add[x]=[];
for(var y=0;y<b.cols;y++){
add[x][y]=a.matrix[x][y]+b.matrix[x][y];
}
}
return add;
}
else{
console.log("Matrices do not have an equal number of columns");
return;
}
}
else{
console.log("Matrices do not have an equal number of rows");
return;
}
}
Math.matrix.multiply=function(a,b){
var a=Math.matrix.analyse(a);
var b=Math.matrix.analyse(b);
if(a.cols==b.rows){
multiply=[];
for(var x=0;x<a.rows;x++){
multiply[x]=[];
for(var y=0;y<b.cols;y++){
multiply[x][y]=Math.dotproduct(a.matrix[x],Math.matrix.transpose(b)[y]);
}
}
return multiply;
}
else{
console.log("Matrices are not mulitplicable")
}
}
Math.array={};
Math.array.max=function(arr){
var max = arr.reduce(function(a, b) {
return Math.max(a, b);
});
return max;
}
Math.isInt=function(a){
return Math.round(a)===a;
}
Math.factors=function(a){
var factors=[];
for(var x=1;x<=a;x++){
if(Math.isInt(a/x)){
factors.push(x);
}
}
return factors;
}
Math.hcf=function(a,b){
}