-
Notifications
You must be signed in to change notification settings - Fork 8
/
Vector class.js
55 lines (51 loc) · 1.93 KB
/
Vector class.js
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
/*
Description:
Create a Vector object that supports addition, subtraction, dot products, and norms. So, for example:
var a = new Vector([1, 2, 3]);
var b = new Vector([3, 4, 5]);
var c = new Vector([5, 6, 7, 8]);
a.add(b); // should return a new Vector([4, 6, 8])
a.subtract(b); // should return a new Vector([-2, -2, -2])
a.dot(b); // should return 1*3 + 2*4 + 3*5 = 26
a.norm(); // should return sqrt(1^2 + 2^2 + 3^2) = sqrt(14)
a.add(c); // throws an error
If you try to add, subtract, or dot two vectors with different lengths, you must throw an error!
Also provide:
a toString method, so that using the vectors from above, a.toString() === '(1,2,3)' (in Python, this is a __str__ method, so that str(a) == '(1,2,3)')
an equals method, to check that two vectors that have the same components are equal
Note: the test cases will utilize the user-provided equals method.
*/
var Vector = function (components) {
this.x = components;
};
Vector.prototype.add = function(b){
var a = this.x
b = b.x;
if(a.length !== b.length) throw new TypeError("Vectors have different length");
return new Vector(a.map(function(x,i){ return x + b[i]; }));
}
Vector.prototype.subtract = function(b){
var a = this.x;
b = b.x;
if(a.length !== b.length) throw new TypeError("Vectors have different length");
return new Vector(a.map(function(x,i){ return x - b[i]; }));
}
Vector.prototype.dot = function(b){
var a = this.x;
b = b.x;
if(a.length !== b.length) throw new TypeError("Vectors have different length");
return a.reduce(function(s,x,i){ return s + x * b[i]; },0);
}
Vector.prototype.equals = function(b){
var a = this.x;
b = b.x;
if(a.length !== b.length) return false;
return a.every(function(x,i){ return x === b[i]; });
}
Vector.prototype.norm = function(){
var a = this.x;
return Math.sqrt(a.reduce(function(s,x){ return s + x*x }, 0));
}
Vector.prototype.toString = function(){
return '(' + this.x.join(',') + ')';
}