1 | describe('API', () => {
|
2 | const algebra = require('algebra')
|
3 |
|
4 | const C = algebra.C
|
5 | const Complex = algebra.Complex
|
6 | const H = algebra.H
|
7 | const Quaternion = algebra.Quaternion
|
8 | const R = algebra.R
|
9 | const R2 = algebra.R2
|
10 | const R3 = algebra.R3
|
11 | const R2x2 = algebra.R2x2
|
12 | const Real = algebra.Real
|
13 | const CompositionAlgebra = algebra.CompositionAlgebra
|
14 | const MatrixSpace = algebra.MatrixSpace
|
15 | const TensorSpace = algebra.TensorSpace
|
16 | const VectorSpace = algebra.VectorSpace
|
17 |
|
18 | const binaryField = require('../src/binaryField')
|
19 |
|
20 | describe('About operators', () => {
|
21 | it('works', () => {
|
22 | const vector1 = new R2([1, 2])
|
23 | const vector2 = new R2([3, 4])
|
24 |
|
25 | R2.addition(vector1, [3, 4]).should.deepEqual([4, 6])
|
26 | R2.addition([1, 2], vector2).should.deepEqual([4, 6])
|
27 | R2.addition(vector1, vector2).should.deepEqual([4, 6])
|
28 |
|
29 | const vector3 = vector1.addition([3, 4])
|
30 | const vector4 = vector1.addition(vector2)
|
31 | R2.equality(vector3, vector4).should.be.ok
|
32 |
|
33 | vector1.addition(vector1, vector1).equality([4, 6]).should.be.ok
|
34 |
|
35 | vector1.data.should.deepEqual([1, 2])
|
36 | })
|
37 | })
|
38 |
|
39 | describe('CompositionAlgebra', () => {
|
40 | const Bit = CompositionAlgebra(binaryField)
|
41 |
|
42 | it('works', () => {
|
43 | Bit.contains(1).should.be.ok
|
44 | Bit.contains(4).should.be.ko
|
45 |
|
46 | const bit = new Bit(1)
|
47 | bit.addition(0).data.should.eql(1)
|
48 | })
|
49 | })
|
50 |
|
51 | describe('Byte', () => {
|
52 | it('is an octonion over binary field', () => {
|
53 | const Byte = CompositionAlgebra(binaryField, 8)
|
54 |
|
55 | const byte1 = new Byte([1, 0, 0, 0, 0, 0, 0, 0])
|
56 | const byte2 = new Byte([0, 1, 0, 0, 0, 0, 0, 0])
|
57 | const byte3 = new Byte([0, 0, 1, 0, 0, 0, 0, 0])
|
58 | const byte4 = new Byte([0, 0, 0, 1, 0, 0, 0, 0])
|
59 | const byte5 = new Byte([0, 0, 0, 0, 1, 0, 0, 0])
|
60 | const byte6 = new Byte([0, 0, 0, 0, 0, 1, 0, 0])
|
61 | const byte7 = new Byte([0, 0, 0, 0, 0, 0, 1, 0])
|
62 | const byte8 = new Byte([0, 0, 0, 0, 0, 0, 0, 1])
|
63 |
|
64 | byte1.mul(byte1).data.should.deepEqual([1, 0, 0, 0, 0, 0, 0, 0])
|
65 | byte2.mul(byte2).data.should.deepEqual([1, 0, 0, 0, 0, 0, 0, 0])
|
66 | byte3.mul(byte3).data.should.deepEqual([1, 0, 0, 0, 0, 0, 0, 0])
|
67 | byte4.mul(byte4).data.should.deepEqual([1, 0, 0, 0, 0, 0, 0, 0])
|
68 | byte5.mul(byte5).data.should.deepEqual([1, 0, 0, 0, 0, 0, 0, 0])
|
69 | byte6.mul(byte6).data.should.deepEqual([1, 0, 0, 0, 0, 0, 0, 0])
|
70 | byte7.mul(byte7).data.should.deepEqual([1, 0, 0, 0, 0, 0, 0, 0])
|
71 | byte8.mul(byte8).data.should.deepEqual([1, 0, 0, 0, 0, 0, 0, 0])
|
72 |
|
73 | const max = byte1.add(byte2).add(byte3).add(byte4)
|
74 | .add(byte5).add(byte6).add(byte7).add(byte8)
|
75 |
|
76 | max.data.should.deepEqual([1, 1, 1, 1, 1, 1, 1, 1])
|
77 | })
|
78 | })
|
79 |
|
80 | describe('Cyclic', () => {
|
81 | it('works', () => {
|
82 | const Cyclic = algebra.Cyclic
|
83 |
|
84 | const elements = ' abcdefghijklmnopqrstuvwyxz0123456789'
|
85 |
|
86 | const Alphanum = Cyclic(elements)
|
87 |
|
88 | Alphanum.addition('a', 'b').should.eql('c')
|
89 |
|
90 | const x = new Alphanum('a')
|
91 |
|
92 | const y = x.add('c', 'a', 't')
|
93 | .mul('i', 's')
|
94 | .add('o', 'n')
|
95 | .sub('t', 'h', 'e')
|
96 | .div('t', 'a', 'b', 'l', 'e')
|
97 |
|
98 | y.data.should.eql('s')
|
99 |
|
100 | const VectorStrings2 = algebra.VectorSpace(Alphanum)(2)
|
101 | const MatrixStrings2x2 = algebra.MatrixSpace(Alphanum)(2)
|
102 |
|
103 | const vectorOfStrings = new VectorStrings2(['o', 'k'])
|
104 | const matrixOfStrings = new MatrixStrings2x2(['c', 'o',
|
105 | 'o', 'l'])
|
106 | matrixOfStrings.mul(vectorOfStrings)
|
107 | .data.should.deepEqual(['x', 'y'])
|
108 |
|
109 | vectorOfStrings.mul(matrixOfStrings)
|
110 | .data.should.deepEqual(['x', 'y'])
|
111 | })
|
112 | })
|
113 |
|
114 | describe('Real', () => {
|
115 | it('works', () => {
|
116 | const Real = algebra.Real
|
117 |
|
118 | Real.addition(1, 2).should.eql(3)
|
119 |
|
120 | const pi = new Real(Math.PI)
|
121 | const twoPi = pi.mul(2)
|
122 |
|
123 | Real.subtraction(twoPi, 2 * Math.PI).should.eql(0)
|
124 | })
|
125 | })
|
126 |
|
127 | describe('Complex', () => {
|
128 | it('works', () => {
|
129 | const Complex = algebra.Complex
|
130 | const complex1 = new Complex([1, 2])
|
131 |
|
132 | complex1.conjugation().data.should.deepEqual([1, -2])
|
133 | })
|
134 | })
|
135 |
|
136 | describe('Common spaces', () => {
|
137 | describe('R', () => {
|
138 | it('is an alias of Real', () => {
|
139 | R.should.be.eql(Real)
|
140 | })
|
141 | })
|
142 |
|
143 | describe('R2', () => {
|
144 | it('is an alias of VectorSpace(Real)(2)', () => {
|
145 | R2.should.be.eql(VectorSpace(Real)(2))
|
146 | })
|
147 | })
|
148 |
|
149 | describe('R3', () => {
|
150 | it('is an alias of VectorSpace(Real)(3)', () => {
|
151 | R3.should.be.eql(VectorSpace(Real)(3))
|
152 | })
|
153 | })
|
154 |
|
155 | describe('R2x2', () => {
|
156 | it('is an alias of MatrixSpace(Real)(2)', () => {
|
157 | R2x2.should.be.eql(MatrixSpace(Real)(2))
|
158 | })
|
159 | })
|
160 |
|
161 | describe('C', () => {
|
162 | it('is an alias of Complex', () => {
|
163 | C.should.be.eql(Complex)
|
164 | })
|
165 | })
|
166 |
|
167 | describe('H', () => {
|
168 | it('is an alias of Quaternion', () => {
|
169 | H.should.be.eql(Quaternion)
|
170 | })
|
171 | })
|
172 | })
|
173 |
|
174 | describe('Vector', () => {
|
175 | describe('vector.dimension', () => {
|
176 | it('is an attribute', () => {
|
177 | const vector = new R2([1, 1])
|
178 |
|
179 | vector.dimension.should.eql(2)
|
180 | })
|
181 | })
|
182 |
|
183 | describe('addition', () => {
|
184 | it('works', () => {
|
185 | R2.addition([2, 1], [1, 2]).should.deepEqual([3, 3])
|
186 |
|
187 | const vector1 = new R2([2, 1])
|
188 | const vector2 = new R2([2, 2])
|
189 |
|
190 | const vector3 = vector1.addition(vector2)
|
191 |
|
192 | vector3.data.should.deepEqual([4, 3])
|
193 | })
|
194 | })
|
195 |
|
196 | describe('Cross product', () => {
|
197 | it('works', () => {
|
198 | R3.crossProduct([3, -3, 1], [4, 9, 2]).should.deepEqual([-15, -2, 39])
|
199 |
|
200 | const vector1 = new R3([3, -3, 1])
|
201 | const vector2 = new R3([4, 9, 2])
|
202 |
|
203 | const vector3 = vector1.crossProduct(vector2)
|
204 |
|
205 | vector3.data.should.deepEqual([-15, -2, 39])
|
206 | })
|
207 | })
|
208 | })
|
209 |
|
210 | describe('Tensor', () => {
|
211 | describe('equality', () => {
|
212 | it('works', () => {
|
213 | const T2x2x2 = TensorSpace(Real)([2, 2, 2])
|
214 |
|
215 | const tensor1 = new T2x2x2([1, 2, 3, 4, 5, 6, 7, 8])
|
216 | const tensor2 = new T2x2x2([2, 3, 4, 5, 6, 7, 8, 9])
|
217 |
|
218 | T2x2x2.equality(tensor1, tensor1).should.be.ok
|
219 | T2x2x2.equality(tensor1, tensor2).should.be.ko
|
220 |
|
221 | tensor1.equality(tensor1).should.be.ok
|
222 | tensor2.equality(tensor2).should.be.ko
|
223 | })
|
224 | })
|
225 | })
|
226 | })
|