UNPKG

5 kBJavaScriptView Raw
1describe('TensorSpace', () => {
2 const algebra = require('algebra')
3 const TensorSpace = algebra.TensorSpace
4 const Real = algebra.Real
5
6 const T2x2x2 = TensorSpace(Real)([2, 2, 2])
7
8 it('can create a Scalar', () => {
9 const indices = [1]
10
11 const Scalar = TensorSpace(Real)(indices)
12
13 Scalar.zero.should.be.eql(0)
14
15 Scalar.addition(1, 2).should.be.eql(3)
16 Scalar.addition(1, 2, 3).should.be.eql(6)
17
18 Scalar.subtraction(1, 2).should.be.eql(-1)
19 Scalar.subtraction(1, 2, 3).should.be.eql(-4)
20
21 const x = new Scalar(1)
22 x.data.should.be.eql(1)
23
24 x.addition(2).data.should.be.eql(3)
25 x.addition(2, 3, 4).data.should.be.eql(10)
26
27 x.subtraction(2).data.should.be.eql(-1)
28 x.subtraction(2, 3, 4).data.should.be.eql(-8)
29 })
30
31 it('can create a Vector', () => {
32 const indices = [2]
33
34 const Vector = TensorSpace(Real)(indices)
35
36 Vector.zero.should.be.eql([0, 0])
37
38 Vector.addition([1, 0], [1, -1]).should.be.eql([2, -1])
39 Vector.addition([1, 0], [1, -1], [-1, 1]).should.be.eql([1, 0])
40
41 Vector.subtraction([2, -1], [1, -1]).should.be.eql([1, 0])
42 Vector.subtraction([1, -1], [2, -2], [3, -3]).should.be.eql([-4, 4])
43
44 const v = new Vector([1, 2])
45 v.data.should.be.eql([1, 2])
46
47 v.addition([4, -1]).data.should.be.eql([5, 1])
48 v.addition([4, -1], [-1, 1]).data.should.be.eql([4, 2])
49
50 v.subtraction([2, 1]).data.should.be.eql([-1, 1])
51 v.subtraction([2, 1], [1, -1]).data.should.be.eql([-2, 2])
52 })
53
54 it('can create a Matrix', () => {
55 const indices = [2, 2]
56
57 const Matrix = TensorSpace(Real)(indices)
58
59 Matrix.zero.should.be.eql([0, 0,
60 0, 0])
61
62 Matrix.addition([1, 0,
63 0, 1], [1, -1,
64 0, 1]).should.be.eql([2, -1,
65 0, 2])
66
67 Matrix.addition([1, 0,
68 0, 1], [1, -1,
69 0, 1], [2, 1,
70 1, 2]).should.be.eql([4, 0,
71 1, 4])
72
73 Matrix.subtraction([1, 0,
74 0, 1], [1, -1,
75 0, 1]).should.be.eql([0, 1,
76 0, 0])
77
78 Matrix.subtraction([1, 0,
79 0, 1], [1, -1,
80 0, 1], [2, 1,
81 1, 2]).should.be.eql([-2, 0,
82 -1, -2])
83
84 const m = new Matrix([1, 2,
85 3, 4])
86
87 m.data.should.be.eql([1, 2,
88 3, 4])
89
90 m.addition([1, 0,
91 0, 1]).data.should.be.eql([2, 2,
92 3, 5])
93
94 m.subtraction([1, 0,
95 0, 1]).data.should.be.eql([0, 2,
96 3, 3])
97 })
98
99 describe('attribute', () => {
100 describe('order', () => {
101 it('is 0 for scalars', () => {
102 const Scalar = TensorSpace(Real)([1])
103 Scalar.order.should.eql(0)
104
105 const scalar1 = new Scalar(4)
106 scalar1.order.should.eql(0)
107 })
108
109 it('is 1 for vectors', () => {
110 const Vector = TensorSpace(Real)([2])
111 Vector.order.should.eql(1)
112
113 const vector1 = new Vector([1, 2])
114 vector1.order.should.eql(1)
115 })
116
117 it('is 2 for matrices', () => {
118 const Matrix = TensorSpace(Real)([2, 2])
119 Matrix.order.should.eql(2)
120
121 const matrix1 = new Matrix([1, 2,
122 3, 4])
123 matrix1.order.should.eql(2)
124 })
125 })
126 })
127
128 describe('operator', () => {
129 describe('addition', () => {
130 it('works', () => {
131 const tensor1 = new T2x2x2([1, 2, 3, 4, 5, 6, 7, 8])
132 const tensor2 = new T2x2x2([2, 3, 4, 5, 6, 7, 8, 9])
133 const resultData = [3, 5, 7, 9, 11, 13, 15, 17]
134
135 T2x2x2.addition(tensor1, tensor2).should.deepEqual(resultData)
136
137 const tensor3 = tensor1.addition(tensor2)
138 tensor3.data.should.deepEqual(resultData)
139 })
140 })
141
142 describe('subtraction', () => {
143 it('works', () => {
144 const tensor1 = new T2x2x2([1, 2, 3, 4, 5, 6, 7, 8])
145 const tensor2 = new T2x2x2([2, 3, 4, 5, 6, 7, 8, 9])
146 const resultData = [-1, -1, -1, -1, -1, -1, -1, -1]
147
148 T2x2x2.subtraction(tensor1, tensor2).should.deepEqual(resultData)
149
150 const tensor3 = tensor1.subtraction(tensor2)
151 tensor3.data.should.deepEqual(resultData)
152 })
153 })
154
155 describe('scalarMultiplication', () => {
156 it('works', () => {
157 const tensor1 = new T2x2x2([1, 2, 3, 4, 5, 6, 7, 8])
158 const scalar1 = new Real(2)
159 const resultData = [2, 4, 6, 8, 10, 12, 14, 16]
160
161 T2x2x2.scalarMultiplication(tensor1, scalar1).should.deepEqual(resultData)
162
163 const tensor2 = tensor1.scalarMultiplication(scalar1)
164 tensor2.data.should.deepEqual(resultData)
165 })
166 })
167 })
168})