Skip to content

Commit 7f4c24d

Browse files
committed
SparseMatrix get and set methods
1 parent c07f5c7 commit 7f4c24d

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

ext/symengine/ruby_matrix.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,3 +645,45 @@ VALUE cmatrix_dense_eye(VALUE self, VALUE args)
645645
cresult);
646646
return result;
647647
}
648+
649+
VALUE cmatrix_sparse_get(VALUE self, VALUE r, VALUE c)
650+
{
651+
CDenseMatrix *this;
652+
Data_Get_Struct(self, CSparseMatrix, this);
653+
654+
basic_struct *cresult;
655+
VALUE result;
656+
657+
unsigned long cbasic_r;
658+
cbasic_r = NUM2ULONG(r);
659+
unsigned long cbasic_c;
660+
cbasic_c = NUM2ULONG(c);
661+
662+
cresult = basic_new_heap();
663+
664+
sparse_matrix_get_basic(cresult, this, cbasic_r, cbasic_c);
665+
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL, basic_free_heap,
666+
cresult);
667+
return result;
668+
}
669+
670+
VALUE cmatrix_sparse_set(VALUE self, VALUE r, VALUE c, VALUE operand)
671+
{
672+
CSparseMatrix *this;
673+
Data_Get_Struct(self, CSparseMatrix, this);
674+
675+
basic cbasic_operand;
676+
basic_new_stack(cbasic_operand);
677+
sympify(operand, cbasic_operand);
678+
679+
unsigned long cbasic_r;
680+
cbasic_r = NUM2ULONG(r);
681+
unsigned long cbasic_c;
682+
cbasic_c = NUM2ULONG(c);
683+
684+
sparse_matrix_set_basic(this, cbasic_r, cbasic_c, cbasic_operand);
685+
686+
basic_free_stack(cbasic_operand);
687+
688+
return self;
689+
}

ext/symengine/ruby_matrix.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ void cmatrix_sparse_free(void *ptr);
3636
VALUE cmatrix_sparse_alloc(VALUE klass);
3737
VALUE cmatrix_sparse_init(VALUE self, VALUE args);
3838
VALUE cmatrix_sparse_to_str(VALUE self);
39+
VALUE cmatrix_sparse_get(VALUE self, VALUE r, VALUE c);
40+
VALUE cmatrix_sparse_set(VALUE self, VALUE r, VALUE c, VALUE operand);
3941

4042
#endif // RUBY_MATRIX_H_

ext/symengine/symengine.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ void Init_symengine()
264264
rb_define_alloc_func(c_sparse_matrix, cmatrix_sparse_alloc);
265265
rb_define_method(c_sparse_matrix, "initialize", cmatrix_sparse_init, -2);
266266
rb_define_method(c_sparse_matrix, "to_s", cmatrix_sparse_to_str, 0);
267+
rb_define_method(c_sparse_matrix, "get", cmatrix_sparse_get, 2);
268+
rb_define_method(c_sparse_matrix, "set", cmatrix_sparse_set, 3);
267269

268270

269271
symengine_print_stack_on_segfault();

0 commit comments

Comments
 (0)