forked from mateogianolio/vectorious
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sp_vector.js
109 lines (100 loc) · 2.96 KB
/
sp_vector.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
(function () {
'use strict';
var Vector = require('./vector'),
Matrix = require('./matrix');
try {
var nblas = require('nblas-plus');
} catch (error) {
return;
}
/**
* @method constructor
* @desc Creates a `SpVector` from the supplied arguments.
**/
function SpVector (data, indx, options) {
this.type = SpVector.defaultType;
if (options && options.type)
this.type = options.type;
this.length = 0;
if (options && options.length > 0)
this.length = options.length;
this.data = null; //Float64Array or Float32Array
this.indx = null; //Int32Array
if (data instanceof SpVector) {
//copy
var v = data;
this.data = new data.type(v.data);
this.indx = new Int32Array(v.indx);
this.type = v.type;
this.length = v.length;
} else if (data instanceof Array && indx instanceof Array) {
//convert to typed array
this.data = new this.type(data);
this.indx = new Int32Array(indx);
} else if (data && data.buffer && data.buffer instanceof ArrayBuffer) {
//assign
this.data = data;
if (indx instanceof Array)
this.indx = new Int32Array(indx);
else
this.indx = indx;
this.type = data.constructor;
} else if(!data && !indx && this.length > 0) {
//create empty
this.data = new this.type(this.length);
}
}
/**
* Default type for data
**/
SpVector.defaultType = Float64Array;
/**
* Performs dot multiplication with current sparse vector and dense `vector`
* @param {Vector} vector, dense vector
* @returns {Number} the dot product of the two vectors
**/
SpVector.prototype.dot = function (vector) {
if (vector.type != this.type)
throw new Error('types are different');
var res = nblas.usdot(this.data, this.indx, vector.data);
return res;
};
/**
* Adds dense `vector` to the current sparse vector.
* @param {Vector} vector, dense vector
* @returns {Vector} updated dense vector
**/
SpVector.prototype.add = function (vector) {
if (vector.type != this.type)
throw new Error('types are different');
nblas.usaxpy(this.data, this.indx, vector.data, +1);
return vector;
};
/**
* Subtracts dense `vector` from the current sparse vector.
* @param {Vector} vector, dense vector
* @returns {Vector} updated dense vector
**/
SpVector.prototype.subtract = function (vector) {
if (vector.type != this.type)
throw new Error('types are different');
nblas.usaxpy(this.data, this.indx, vector.data, -1);
return vector;
};
/**
* Converts current sparse vector to dense vector
* @returns {Vector} dense vector
*/
SpVector.prototype.toVector = function() {
if (this.length) {
var dv = new Vector(new this.type(this.length));
nblas.ussc(this.data, this.indx, dv.data);
return dv;
}
return null;
};
module.exports = SpVector;
try {
window.SpVector = SpVector;
} catch (e) {}
}());