Class: SpatialStats::Weights::CSRMatrix

Inherits:
Data
  • Object
show all
Defined in:
ext/spatial_stats/spatial_stats.c,
ext/spatial_stats/spatial_stats.c

Overview

CSRMatrix partially implements a compressed sparse row matrix to perform spatial lag and other calculations. This will generally be used to store the weights of an observation set.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, num_rows) ⇒ CSRMatrix

A new instance of CSRMatrix. Uses a Dictionary of Keys (DOK) as input to represent a square matrix.

Examples:

weights = {
    'a' => [{ id: 'c', weight: 1 }],
    'b' => [{ id: 'b', weight: 1 }],
    'c' => [{ id: 'a', weight: 1 }]
}
num_rows = 3

csr = CSRMatrix.new(data, num_rows)

Parameters:

  • data (Array)

    in 1-D format

  • num_rows (Integer)

    in the 2-D representation



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
# File 'ext/spatial_stats/csr_matrix.c', line 130

VALUE csr_matrix_initialize(VALUE self, VALUE data, VALUE num_rows)
{
    VALUE keys;
    csr_matrix *csr;
    TypedData_Get_Struct(self, csr_matrix, &csr_matrix_type, csr);
    csr->init = 0;

    Check_Type(data, T_HASH);
    Check_Type(num_rows, T_FIXNUM);

    keys = rb_funcall(data, rb_intern("keys"), 0);

    // check dimensions are correct
    if (NUM2INT(num_rows) != rb_array_len(keys))
    {
        rb_raise(rb_eArgError, "n_rows != keys.size, check your dimensions");
    }

    mat_to_sparse(csr, data, keys, num_rows);

    rb_iv_set(self, "@n", num_rows);
    rb_iv_set(self, "@nnz", INT2NUM(csr->nnz));

    return self;
}

Instance Attribute Details

#mObject (readonly)

#nObject (readonly)

#nnzObject (readonly)

Instance Method Details

#col_indexArray

Column indices of the non-zero values.

Returns:

  • (Array)

    of the column indices.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'ext/spatial_stats/csr_matrix.c', line 184

VALUE csr_matrix_col_index(VALUE self)
{
    csr_matrix *csr;
    VALUE result;

    int i;

    TypedData_Get_Struct(self, csr_matrix, &csr_matrix_type, csr);

    result = rb_ary_new_capa(csr->nnz);
    for (i = 0; i < csr->nnz; i++)
    {
        rb_ary_store(result, i, INT2NUM(csr->col_index[i]));
    }

    return result;
}

#coordinatesHash

A hash representation of the matrix with coordinates as keys.

Examples:

data = [
        [0, 1, 0]
        [0, 0, 0],
        [1, 0, 1]
       ]
num_rows = 3
num_cols = 3
data = data.flatten!
csr = CSRMatrix.new(data, num_rows, num_cols)

csr.coordinates
# => {
        [0,1] => 1,
        [2,0] => 1,
        [2,2] => 1
     }

Returns:

  • (Hash)


340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'ext/spatial_stats/csr_matrix.c', line 340

VALUE csr_matrix_coordinates(VALUE self)
{
    csr_matrix *csr;
    VALUE result;

    int i;
    int k;

    VALUE key;
    VALUE val;
    int row_end;

    TypedData_Get_Struct(self, csr_matrix, &csr_matrix_type, csr);

    result = rb_hash_new();

    // iterate through every value in the matrix and assign it's coordinates
    // [x,y] as the key to the hash, with the value as the value.
    // Use i to keep track of what row we are on.
    i = 0;
    row_end = csr->row_index[1];
    for (k = 0; k < csr->nnz; k++)
    {
        if (k == row_end)
        {
            i++;
            row_end = csr->row_index[i + 1];
        }

        // store i,j coordinates j is col_index[k]
        key = rb_ary_new_capa(2);
        rb_ary_store(key, 0, INT2NUM(i));
        rb_ary_store(key, 1, INT2NUM(csr->col_index[k]));

        val = DBL2NUM(csr->values[k]);

        rb_hash_aset(result, key, val);
    }

    return result;
}

#dot_row(vec, row) ⇒ Float

Compute the dot product of the given row with the input vector. Equivalent to mulvec(vec).

Parameters:

  • vec (Array)

    of length n.

  • row (Integer)

    of the dot product.

Returns:

  • (Float)

    of the result of the dot product.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'ext/spatial_stats/csr_matrix.c', line 283

VALUE csr_matrix_dot_row(VALUE self, VALUE vec, VALUE row)
{
    csr_matrix *csr;
    VALUE result;

    int i;
    int jj;
    double tmp;

    Check_Type(vec, T_ARRAY);
    Check_Type(row, T_FIXNUM);

    TypedData_Get_Struct(self, csr_matrix, &csr_matrix_type, csr);

    if (rb_array_len(vec) != csr->n)
    {
        rb_raise(rb_eArgError, "Dimension Mismatch CSRMatrix.n != vec.size");
    }

    i = NUM2INT(row);
    if (!(i >= 0 && i < csr->n))
    {
        rb_raise(rb_eArgError, "Index Error row_idx >= m or idx < 0");
    }

    tmp = 0;
    for (jj = csr->row_index[i]; jj < csr->row_index[i + 1]; jj++)
    {
        tmp += csr->values[jj] * NUM2DBL(rb_ary_entry(vec, csr->col_index[jj]));
    }

    result = DBL2NUM(tmp);
    return result;
}

#mulvec(vec) ⇒ Array

Multiply matrix by the input vector.

Parameters:

  • vec (Array)

    of length n.

Returns:

  • (Array)

    of the result of the multiplication.

See Also:



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'ext/spatial_stats/csr_matrix.c', line 239

VALUE csr_matrix_mulvec(VALUE self, VALUE vec)
{
    csr_matrix *csr;
    VALUE result;

    int i;
    int jj;
    double tmp;

    Check_Type(vec, T_ARRAY);

    TypedData_Get_Struct(self, csr_matrix, &csr_matrix_type, csr);

    if (rb_array_len(vec) != csr->n)
    {
        rb_raise(rb_eArgError, "Dimension Mismatch CSRMatrix.n != vec.size");
    }

    result = rb_ary_new_capa(csr->n);

    // float *vals = (float *)DATA_PTR(result);

    for (i = 0; i < csr->n; i++)
    {
        tmp = 0;
        for (jj = csr->row_index[i]; jj < csr->row_index[i + 1]; jj++)
        {
            tmp += csr->values[jj] * NUM2DBL(rb_ary_entry(vec, csr->col_index[jj]));
        }
        rb_ary_store(result, i, DBL2NUM(tmp));
    }

    return result;
}

#row_indexArray

Row indices of the non-zero values. Represents the start index of values in a row. For example [0,2,3] would represent a matrix with 2 rows, the first containing 2 non-zero values and the second containing 1. Length is num_rows + 1.

Used for row slicing operations.

Returns:

  • (Array)

    of the row indices.



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'ext/spatial_stats/csr_matrix.c', line 212

VALUE csr_matrix_row_index(VALUE self)
{
    csr_matrix *csr;
    VALUE result;

    int i;

    TypedData_Get_Struct(self, csr_matrix, &csr_matrix_type, csr);

    result = rb_ary_new_capa(csr->n + 1);
    for (i = 0; i <= csr->n; i++)
    {
        rb_ary_store(result, i, INT2NUM(csr->row_index[i]));
    }

    return result;
}

#valuesArray

Non-zero values in the matrix.

Returns:

  • (Array)

    of the non-zero values.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'ext/spatial_stats/csr_matrix.c', line 161

VALUE csr_matrix_values(VALUE self)
{
    csr_matrix *csr;
    VALUE result;

    int i;

    TypedData_Get_Struct(self, csr_matrix, &csr_matrix_type, csr);

    result = rb_ary_new_capa(csr->nnz);
    for (i = 0; i < csr->nnz; i++)
    {
        rb_ary_store(result, i, DBL2NUM(csr->values[i]));
    }

    return result;
}