spatial_stats 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/spatial_stats/csr_matrix.c +61 -42
- data/ext/spatial_stats/csr_matrix.h +2 -3
- data/ext/spatial_stats/spatial_stats.c +1 -1
- data/lib/spatial_stats/spatial_stats.so +0 -0
- data/lib/spatial_stats/version.rb +1 -1
- data/lib/spatial_stats/weights/weights_matrix.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c45774fe6829810899c98f75430ac47a135f839762663381e9621f983e7a0baa
|
4
|
+
data.tar.gz: 719b2421748188426d1c44b73c00744d66a65b8ac5e23034a22d875da5fbfd61
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ab6221b26cd81ab2af42d195349d188ec482d6cb346677a28d3a69c16e8472060645938f806df6f14ee76eabef11d0e7e12904513b47b005ebf75d6e898caac
|
7
|
+
data.tar.gz: 2d0a3df4f22008bef642dcff5c74b2ae8c8c4590314ea110a58fedf8a4c949649ba649f8bca77016fb1a0cf49d0571eee7fecd007d610d873693f98a8fbab6b6
|
@@ -28,60 +28,79 @@ VALUE csr_matrix_alloc(VALUE self)
|
|
28
28
|
return TypedData_Wrap_Struct(self, &csr_matrix_type, csr);
|
29
29
|
}
|
30
30
|
|
31
|
-
void mat_to_sparse(csr_matrix *csr, VALUE data, VALUE
|
31
|
+
void mat_to_sparse(csr_matrix *csr, VALUE data, VALUE keys, VALUE num_rows)
|
32
32
|
{
|
33
|
+
|
33
34
|
int nnz = 0;
|
34
|
-
int
|
35
|
-
int
|
35
|
+
int n = NUM2INT(num_rows);
|
36
|
+
int m;
|
37
|
+
|
38
|
+
VALUE key;
|
39
|
+
VALUE row;
|
40
|
+
VALUE entry;
|
41
|
+
VALUE key_lookup = rb_hash_new();
|
42
|
+
VALUE weight_sym = ID2SYM(rb_intern("weight"));
|
43
|
+
VALUE id_sym = ID2SYM(rb_intern("id"));
|
36
44
|
|
37
45
|
double *values;
|
38
46
|
int *col_index;
|
39
47
|
int *row_index;
|
40
48
|
|
41
49
|
int nz_idx;
|
42
|
-
double
|
50
|
+
double weight;
|
43
51
|
|
44
52
|
int i;
|
45
53
|
int j;
|
46
|
-
int index;
|
47
54
|
|
48
55
|
// first get number non zero count so we can alloc values and col_index
|
49
|
-
for (i = 0; i <
|
56
|
+
for (i = 0; i < n; i++)
|
50
57
|
{
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
58
|
+
key = rb_ary_entry(keys, i);
|
59
|
+
|
60
|
+
// set lookup index for this key
|
61
|
+
rb_hash_aset(key_lookup, key, INT2NUM(i));
|
62
|
+
|
63
|
+
// check the value of this row is actually an array
|
64
|
+
// if it is, add array len to nnz
|
65
|
+
row = rb_hash_aref(data, key);
|
66
|
+
Check_Type(row, T_ARRAY);
|
67
|
+
nnz += rb_array_len(row);
|
59
68
|
}
|
60
69
|
|
61
70
|
values = malloc(sizeof(double) * nnz);
|
62
71
|
col_index = malloc(sizeof(int) * nnz);
|
63
|
-
row_index = malloc(sizeof(int) * (
|
72
|
+
row_index = malloc(sizeof(int) * (n + 1));
|
64
73
|
|
65
|
-
// for every
|
74
|
+
// for every row, work through each hash
|
75
|
+
// in each hash, add the weight to values and get col_index
|
76
|
+
// by looking at the key_lookup of id.
|
77
|
+
// Row index will be computed by adding len of each row and updating array.
|
66
78
|
nz_idx = 0;
|
67
|
-
for (i = 0; i <
|
79
|
+
for (i = 0; i < n; i++)
|
68
80
|
{
|
69
81
|
row_index[i] = nz_idx;
|
70
|
-
|
82
|
+
|
83
|
+
key = rb_ary_entry(keys, i);
|
84
|
+
row = rb_hash_aref(data, key);
|
85
|
+
m = rb_array_len(row);
|
86
|
+
|
87
|
+
for (j = 0; j < m; j++)
|
71
88
|
{
|
72
|
-
|
73
|
-
entry
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
89
|
+
entry = rb_ary_entry(row, j);
|
90
|
+
Check_Type(entry, T_HASH);
|
91
|
+
|
92
|
+
key = rb_hash_aref(entry, id_sym);
|
93
|
+
weight = NUM2DBL(rb_hash_aref(entry, weight_sym));
|
94
|
+
|
95
|
+
// assign the nnz the weight
|
96
|
+
// get index in the keys array of key from lookup table
|
97
|
+
values[nz_idx] = weight;
|
98
|
+
col_index[nz_idx] = NUM2INT(rb_hash_aref(key_lookup, key));
|
99
|
+
nz_idx++;
|
80
100
|
}
|
81
101
|
}
|
82
|
-
row_index[
|
102
|
+
row_index[n] = nnz;
|
83
103
|
|
84
|
-
csr->m = m;
|
85
104
|
csr->n = n;
|
86
105
|
csr->nnz = nnz;
|
87
106
|
csr->values = values;
|
@@ -112,27 +131,27 @@ void mat_to_sparse(csr_matrix *csr, VALUE data, VALUE num_rows, VALUE num_cols)
|
|
112
131
|
*
|
113
132
|
* @return [CSRMatrix]
|
114
133
|
*/
|
115
|
-
VALUE csr_matrix_initialize(VALUE self, VALUE data, VALUE num_rows
|
134
|
+
VALUE csr_matrix_initialize(VALUE self, VALUE data, VALUE num_rows)
|
116
135
|
{
|
117
|
-
|
136
|
+
VALUE keys;
|
118
137
|
csr_matrix *csr;
|
119
138
|
TypedData_Get_Struct(self, csr_matrix, &csr_matrix_type, csr);
|
120
139
|
csr->init = 0;
|
121
140
|
|
122
|
-
Check_Type(data,
|
141
|
+
Check_Type(data, T_HASH);
|
123
142
|
Check_Type(num_rows, T_FIXNUM);
|
124
|
-
|
143
|
+
|
144
|
+
keys = rb_funcall(data, rb_intern("keys"), 0);
|
125
145
|
|
126
146
|
// check dimensions are correct
|
127
|
-
if (NUM2INT(num_rows)
|
147
|
+
if (NUM2INT(num_rows) != rb_array_len(keys))
|
128
148
|
{
|
129
|
-
rb_raise(rb_eArgError, "n_rows
|
149
|
+
rb_raise(rb_eArgError, "n_rows != keys.size, check your dimensions");
|
130
150
|
}
|
131
151
|
|
132
|
-
mat_to_sparse(csr, data,
|
152
|
+
mat_to_sparse(csr, data, keys, num_rows);
|
133
153
|
|
134
|
-
rb_iv_set(self, "@
|
135
|
-
rb_iv_set(self, "@n", num_cols);
|
154
|
+
rb_iv_set(self, "@n", num_rows);
|
136
155
|
rb_iv_set(self, "@nnz", INT2NUM(csr->nnz));
|
137
156
|
|
138
157
|
return self;
|
@@ -203,8 +222,8 @@ VALUE csr_matrix_row_index(VALUE self)
|
|
203
222
|
|
204
223
|
TypedData_Get_Struct(self, csr_matrix, &csr_matrix_type, csr);
|
205
224
|
|
206
|
-
result = rb_ary_new_capa(csr->
|
207
|
-
for (i = 0; i <= csr->
|
225
|
+
result = rb_ary_new_capa(csr->n + 1);
|
226
|
+
for (i = 0; i <= csr->n; i++)
|
208
227
|
{
|
209
228
|
rb_ary_store(result, i, INT2NUM(csr->row_index[i]));
|
210
229
|
}
|
@@ -239,11 +258,11 @@ VALUE csr_matrix_mulvec(VALUE self, VALUE vec)
|
|
239
258
|
rb_raise(rb_eArgError, "Dimension Mismatch CSRMatrix.n != vec.size");
|
240
259
|
}
|
241
260
|
|
242
|
-
result = rb_ary_new_capa(csr->
|
261
|
+
result = rb_ary_new_capa(csr->n);
|
243
262
|
|
244
263
|
// float *vals = (float *)DATA_PTR(result);
|
245
264
|
|
246
|
-
for (i = 0; i < csr->
|
265
|
+
for (i = 0; i < csr->n; i++)
|
247
266
|
{
|
248
267
|
tmp = 0;
|
249
268
|
for (jj = csr->row_index[i]; jj < csr->row_index[i + 1]; jj++)
|
@@ -285,7 +304,7 @@ VALUE csr_matrix_dot_row(VALUE self, VALUE vec, VALUE row)
|
|
285
304
|
}
|
286
305
|
|
287
306
|
i = NUM2INT(row);
|
288
|
-
if (!(i >= 0 && i < csr->
|
307
|
+
if (!(i >= 0 && i < csr->n))
|
289
308
|
{
|
290
309
|
rb_raise(rb_eArgError, "Index Error row_idx >= m or idx < 0");
|
291
310
|
}
|
@@ -4,7 +4,6 @@
|
|
4
4
|
typedef struct csr_matrix
|
5
5
|
{
|
6
6
|
char init;
|
7
|
-
int m;
|
8
7
|
int n;
|
9
8
|
int nnz;
|
10
9
|
double *values;
|
@@ -23,9 +22,9 @@ static const rb_data_type_t csr_matrix_type = {
|
|
23
22
|
0,
|
24
23
|
RUBY_TYPED_FREE_IMMEDIATELY};
|
25
24
|
|
26
|
-
void mat_to_sparse(csr_matrix *csr, VALUE data, VALUE
|
25
|
+
void mat_to_sparse(csr_matrix *csr, VALUE data, VALUE keys, VALUE num_rows);
|
27
26
|
VALUE csr_matrix_alloc(VALUE self);
|
28
|
-
VALUE csr_matrix_initialize(VALUE self, VALUE data, VALUE num_rows
|
27
|
+
VALUE csr_matrix_initialize(VALUE self, VALUE data, VALUE num_rows);
|
29
28
|
VALUE csr_matrix_values(VALUE self);
|
30
29
|
VALUE csr_matrix_col_index(VALUE self);
|
31
30
|
VALUE csr_matrix_row_index(VALUE self);
|
@@ -18,7 +18,7 @@ void Init_spatial_stats()
|
|
18
18
|
VALUE csr_matrix_class = rb_define_class_under(weights_mod, "CSRMatrix", rb_cData);
|
19
19
|
|
20
20
|
rb_define_alloc_func(csr_matrix_class, csr_matrix_alloc);
|
21
|
-
rb_define_method(csr_matrix_class, "initialize", csr_matrix_initialize,
|
21
|
+
rb_define_method(csr_matrix_class, "initialize", csr_matrix_initialize, 2);
|
22
22
|
rb_define_method(csr_matrix_class, "values", csr_matrix_values, 0);
|
23
23
|
rb_define_method(csr_matrix_class, "col_index", csr_matrix_col_index, 0);
|
24
24
|
rb_define_method(csr_matrix_class, "row_index", csr_matrix_row_index, 0);
|
Binary file
|