UNPKG

3.92 kBJavaScriptView Raw
1var algebra = require('algebra')
2var notDefined = require('not-defined')
3
4var Real = algebra.Real
5var VectorSpace = algebra.VectorSpace
6
7var methodBinaryOperator = require('./features/methodBinaryOperator')
8var staticBinaryOperator = require('./features/staticBinaryOperator')
9var staticUnaryOperator = require('./features/staticUnaryOperator')
10
11var R2 = VectorSpace(Real)(2)
12var R3 = VectorSpace(Real)(3)
13
14describe('VectorSpace', () => {
15 var operator
16
17 describe('data', () => {
18 var v = new R2([0, 1])
19
20 it('is enumerable', () => {
21 v.propertyIsEnumerable('data').should.be.ok
22 })
23
24 it('is immutable', () => {
25 ;(() => {
26 'use strict'
27 v.data = [2, 1]
28 }).should.throwError()
29 })
30 })
31
32 describe('addition()', () => {
33 operator = 'addition'
34
35 it('is a static method', staticBinaryOperator(R2, operator, [0, 2], [-1, 3], [-1, 5]))
36
37 it('is a class method', methodBinaryOperator(R2, operator, [0, 1], [1, 1], [1, 2]))
38
39 it('accepts multiple arguments', () => {
40 R2.addition([1, -1], [2, -2], [3, -3]).should.deepEqual([6, -6])
41
42 var vector = new R2([1, -1])
43 vector.addition([2, -2], [3, -3]).data.should.eql([6, -6])
44 })
45 })
46
47 describe('subtraction()', () => {
48 operator = 'subtraction'
49
50 it('is a static method', staticBinaryOperator(R2, operator, [0, 2], [-1, 3], [1, -1]))
51
52 it('is a class method', methodBinaryOperator(R2, operator, [0, 1], [1, 1], [-1, 0]))
53
54 it('accepts multiple arguments', () => {
55 R2.subtraction([6, -6], [2, -2], [3, -3]).should.deepEqual([1, -1])
56
57 var vector = new R2([6, -6])
58 vector.subtraction([2, -2], [3, -3]).data.should.eql([1, -1])
59 })
60 })
61
62 describe('scalarProduct()', () => {
63 it('is a static operator', () => {
64 var data = R2.scalarProduct([0, 1], [1, 1])
65
66 data.should.eql(1)
67 })
68
69 it('is a class method', () => {
70 var vector1 = new R2([0, 1])
71 var vector2 = new R2([1, 1])
72
73 var scalar = vector1.scalarProduct(vector2)
74
75 scalar.data.should.be.eql(1)
76 })
77
78 it('is returns a scalar', () => {
79 var vector1 = new R2([0, 1])
80 var vector2 = new R2([1, 1])
81
82 var scalar = vector1.scalarProduct(vector2)
83
84 scalar.data.should.be.eql(1)
85 })
86 })
87
88 describe('dotProduct()', () => {
89 it('is an alias of scalarProduct()', () => {
90 R2.scalarProduct.should.be.eql(R2.dotProduct)
91
92 var vector = new R2([0, 1])
93 vector.scalarProduct.should.be.eql(vector.dotProduct)
94 })
95 })
96
97 describe('dot()', () => {
98 it('is an alias of scalarProduct()', () => {
99 R2.scalarProduct.should.be.eql(R2.dot)
100
101 var vector = new R2([0, 1])
102 vector.scalarProduct.should.be.eql(vector.dot)
103 })
104 })
105
106 describe('norm', () => {
107 it('is an attribute holding a scalar', () => {
108 var vector1 = new R2([0, 1])
109 var vector2 = new R3([1, 1, 2])
110
111 vector1.norm.data.should.be.eql(1)
112 vector2.norm.data.should.be.eql(6)
113 })
114 })
115
116 describe('norm()', () => {
117 var operator = 'norm'
118
119 it('is a static method', () => {
120 staticUnaryOperator(R2, operator, [0, 1], 1)
121 staticUnaryOperator(R3, operator, [1, 1, 2], 6)
122 })
123 })
124
125 describe('crossProduct()', () => {
126 var operator = 'crossProduct'
127
128 it('is a static method', () => {
129 staticBinaryOperator(R3, operator, [3, -3, 1], [4, 9, 2], [-15, -2, 39])
130 })
131
132 it('is a class method', () => {
133 methodBinaryOperator(R3, operator, [3, -3, 1], [-12, 12, -4], [0, 0, 0])
134 })
135
136 it('is defined only in dimension 3', () => {
137 notDefined(R2.cross).should.be.ok
138
139 var vector = new R2([1, 0])
140 notDefined(vector.cross).should.be.ok
141 })
142 })
143
144 describe('cross()', () => {
145 it('is an alias of crossProduct()', () => {
146 R3.crossProduct.should.be.eql(R3.cross)
147
148 var vector = new R3([1, 0, 1])
149 vector.crossProduct.should.be.eql(vector.cross)
150 })
151 })
152})