Class: SpatialStats::Weights::CSRMatrix
- Inherits:
-
Object
- Object
- SpatialStats::Weights::CSRMatrix
- 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
- #m ⇒ Object readonly
- #n ⇒ Object readonly
- #nnz ⇒ Object readonly
Instance Method Summary collapse
-
#col_index ⇒ Array
Column indices of the non-zero values.
-
#coordinates ⇒ Hash
A hash representation of the matrix with coordinates as keys.
-
#dot_row(vec, row) ⇒ Float
Compute the dot product of the given row with the input vector.
-
#initialize(data, num_rows) ⇒ CSRMatrix
constructor
A new instance of CSRMatrix.
-
#mulvec(vec) ⇒ Array
Multiply matrix by the input vector.
-
#row_index ⇒ Array
Row indices of the non-zero values.
-
#values ⇒ Array
Non-zero values in the matrix.
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.
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 |
# File 'ext/spatial_stats/csr_matrix.c', line 138
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) != (int)RARRAY_LEN(keys)) // Explicit cast to int
{
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
#m ⇒ Object (readonly)
#n ⇒ Object (readonly)
#nnz ⇒ Object (readonly)
Instance Method Details
#col_index ⇒ Array
Column indices of the non-zero values.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'ext/spatial_stats/csr_matrix.c', line 192
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;
}
|
#coordinates ⇒ Hash
A hash representation of the matrix with coordinates as keys.
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 381 382 383 384 385 386 387 388 |
# File 'ext/spatial_stats/csr_matrix.c', line 348
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)[row].
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 317 318 319 320 321 322 323 324 |
# File 'ext/spatial_stats/csr_matrix.c', line 291
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 (RARRAY_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.
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 273 274 275 276 277 278 279 280 |
# File 'ext/spatial_stats/csr_matrix.c', line 247
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 (RARRAY_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_index ⇒ Array
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.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'ext/spatial_stats/csr_matrix.c', line 220
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;
}
|
#values ⇒ Array
Non-zero values in the matrix.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'ext/spatial_stats/csr_matrix.c', line 169
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;
}
|