-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_tests.js
182 lines (152 loc) · 4.93 KB
/
matrix_tests.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// @flow
/* eslint-env jasmine */
'use strict';
/* ::
import { Matrix, newCauchyMatrix } from './matrix';
import { Field256Element } from './field_256';
import { Field257Element } from './field_257';
import { BigRational } from './rational';
*/
/*
global
Matrix,
newCauchyMatrix,
Field256Element,
Field257Element,
BigRational,
*/
describe('Matrix', () => {
it('array constructor', () => {
const elements = [1, 2, 3, 4, 5, 6].map(x => new Field257Element(x));
const m = new Matrix(2, 3, elements);
expect(m._rows).toBe(2);
expect(m._columns).toBe(3);
expect(m._elements).not.toBe(elements);
expect(m._elements).toEqual(elements);
});
it('function constructor', () => {
const m = new Matrix(2, 3, (i, j) => new Field257Element(i + j));
expect(m._rows).toBe(2);
expect(m._columns).toBe(3);
expect(m._elements).toEqual(
[0, 1, 2, 1, 2, 3].map(x => new Field257Element(x))
);
});
it('times identity', () => {
const mElements = [1, 0, 0, 1].map(x => new Field257Element(x));
const m = new Matrix(2, 2, mElements);
const nElements = [1, 2, 3, 4, 5, 6].map(x => new Field257Element(x));
const n = new Matrix(2, 3, nElements);
const product = m.times(n);
expect(product._rows).toBe(2);
expect(product._columns).toBe(3);
expect(product._elements).toEqual(nElements);
});
it('times scalar', () => {
const mElements = [2, 0, 0, 2].map(x => new Field257Element(x));
const m = new Matrix(2, 2, mElements);
const nElements = [1, 2, 3, 4, 5, 6].map(x => new Field257Element(x));
const n = new Matrix(2, 3, nElements);
const product = m.times(n);
expect(product._rows).toBe(2);
expect(product._columns).toBe(3);
expect(product._elements).toEqual(
[2, 4, 6, 8, 10, 12].map(x => new Field257Element(x))
);
});
it('times arbitrary', () => {
const mElements = [7, 8, 9, 10].map(x => new Field257Element(x));
const m = new Matrix(2, 2, mElements);
const nElements = [1, 2, 3, 4, 5, 6].map(x => new Field257Element(x));
const n = new Matrix(2, 3, nElements);
const product = m.times(n);
expect(product._rows).toBe(2);
expect(product._columns).toBe(3);
expect(product._elements).toEqual(
[39, 54, 69, 49, 68, 87].map(x => new Field257Element(x))
);
});
it('inverse identity', () => {
const zero = Field257Element.Zero;
const one = Field257Element.One;
const size = 5;
const m = new Matrix(size, size, (i, j) => (i === j ? one : zero));
const mInv = m.inverse();
expect(mInv).toEqual(m);
});
it('inverse scalar rational', () => {
const zero = BigRational.Zero;
const one = BigRational.One;
const two = one.plus(one);
const oneHalf = one.dividedBy(two);
const size = 5;
const m = new Matrix(size, size, (i, j) => (i === j ? two : zero));
const mInv = m.inverse();
const mInvExpected = new Matrix(
size,
size,
(i, j) => (i === j ? oneHalf : zero)
);
expect(mInv).toEqual(mInvExpected);
});
it('inverse scalar GF(256)', () => {
const zero = Field256Element.Zero;
const one = Field256Element.One;
const two = new Field256Element(2);
const twoInv = one.dividedBy(two);
const size = 5;
const m = new Matrix(size, size, (i, j) => (i === j ? two : zero));
const mInv = m.inverse();
const mInvExpected = new Matrix(
size,
size,
(i, j) => (i === j ? twoInv : zero)
);
expect(mInv).toEqual(mInvExpected);
});
it('inverse GF(257)', () => {
const mElements = [1, 1, 1, 2, 4, 8, 3, 9, 27].map(
x => new Field257Element(x)
);
const m = new Matrix(3, 3, mElements);
const zero = Field257Element.Zero;
const one = Field257Element.One;
const identity = new Matrix(3, 3, (i, j) => (i === j ? one : zero));
const mInv = m.inverse();
expect(m.times(mInv)).toEqual(identity);
expect(mInv.times(m)).toEqual(identity);
});
it('inverse singular', () => {
const mElements = [1, 1, 1, 2, 4, 8, 3, 5, 9].map(
x => new Field257Element(x)
);
const m = new Matrix(3, 3, mElements);
expect(() => m.inverse()).toThrowError('Singular matrix');
});
it('Cauchy matrix GF(257)', () => {
const x = [3, 4].map(t => new Field257Element(t));
const y = [0, 1, 2].map(t => new Field257Element(t));
const m = newCauchyMatrix(x, y);
const mExpected = new Matrix(
x.length,
y.length,
[3, 2, 1, 4, 3, 2].map(t =>
Field257Element.One.dividedBy(new Field257Element(t))
)
);
expect(m).toEqual(mExpected);
});
it('Cauchy matrix GF(256)', () => {
const x = [3, 4].map(t => new Field256Element(t));
const y = [0, 1, 2].map(t => new Field256Element(t));
const m = newCauchyMatrix(x, y);
const mExpected = new Matrix(
x.length,
y.length,
[3, 2, 1, 4, 5, 6].map(t =>
Field256Element.One.dividedBy(new Field256Element(t))
)
);
expect(m).toEqual(mExpected);
});
});