-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQMatrix.js
69 lines (58 loc) · 1.35 KB
/
QMatrix.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
const ProductQuantizer = require('./ProductQuantizer');
class QMatrix {
constructor () {
this.qnorm = false;
this.m = 0;
this.n = 0;
this.codesize = 0;
this.pq = new ProductQuantizer();
this.npq = new ProductQuantizer();
}
/**
*
* @param {FtzReader} ftzReader
*/
load(ftzReader) {
this.qnorm = ftzReader.readUInt8();
this.m = ftzReader.readInt64();
this.n = ftzReader.readInt64();
this.codesize = ftzReader.readInt32();
this.codes = ftzReader.readUInt8TypedArray(this.codesize);
this.pq.load(ftzReader);
if (this.qnorm) {
this.norm_codes = ftzReader.readUInt8TypedArray(this.m);
this.npq.load(ftzReader);
}
}
getM() {
return this.m;
}
getN() {
return this.n;
}
/**
*
* @param {Vector} vec
* @param {Number} i
*/
dotRow(vec, i) {
let norm = 1;
if (this.qnorm) {
norm = this.npq.get_centroids(0, this.norm_codes[i])[0];
}
return this.pq.mulcode(vec, this.codes, i, norm);
}
/**
*
* @param {Vector} x
* @param {Number} t
*/
addToVector(x, t) {
let norm = 1;
if (this.qnorm) {
norm = this.npq.get_centroids(0, this.norm_codes[t])[0];
}
this.pq.addcode(x, this.codes, t, norm);
}
}
module.exports = QMatrix;