Skip to content

Commit d7c8b29

Browse files
committed
SparseMatrix ruby wrappers
1 parent b719126 commit d7c8b29

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

ext/symengine/ruby_matrix.c

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,48 @@ void cmatrix_dense_free(void *ptr)
66
dense_matrix_free(mat_ptr);
77
}
88

9+
void cmatrix_sparse_free(void *ptr)
10+
{
11+
CSparseMatrix *mat_ptr = ptr;
12+
sparse_matrix_free(mat_ptr);
13+
}
14+
915
VALUE cmatrix_dense_alloc(VALUE klass)
1016
{
1117
CDenseMatrix *mat_ptr = dense_matrix_new();
1218
return Data_Wrap_Struct(klass, NULL, cmatrix_dense_free, mat_ptr);
1319
}
1420

15-
VALUE cmatrix_to_str(VALUE self)
21+
VALUE cmatrix_sparse_alloc(VALUE klass)
22+
{
23+
CSparseMatrix *mat_ptr = sparse_matrix_new();
24+
return Data_Wrap_Struct(klass, NULL, cmatrix_sparse_free, mat_ptr);
25+
}
26+
27+
VALUE cmatrix_dense_to_str(VALUE self)
1628
{
1729
CDenseMatrix *this;
1830
char *str_ptr;
1931
VALUE result;
2032

2133
Data_Get_Struct(self, CDenseMatrix, this);
2234

23-
str_ptr = matrix_str(this);
35+
str_ptr = dense_matrix_str(this);
36+
result = rb_str_new_cstr(str_ptr);
37+
basic_str_free(str_ptr);
38+
39+
return result;
40+
}
41+
42+
VALUE cmatrix_sparse_to_str(VALUE self)
43+
{
44+
CSparseMatrix *this;
45+
char *str_ptr;
46+
VALUE result;
47+
48+
Data_Get_Struct(self, CSparseMatrix, this);
49+
50+
str_ptr = sparse_matrix_str(this);
2451
result = rb_str_new_cstr(str_ptr);
2552
basic_str_free(str_ptr);
2653

@@ -121,3 +148,41 @@ VALUE cmatrix_dense_init(VALUE self, VALUE args)
121148

122149
return self;
123150
}
151+
152+
VALUE cmatrix_sparse_init(VALUE self, VALUE args)
153+
{
154+
int argc = RARRAY_LEN(args);
155+
CSparseMatrix *this;
156+
Data_Get_Struct(self, CSparseMatrix, this);
157+
158+
if (argc == 0) {
159+
160+
// SymEngine::SparseMatrix()
161+
sparse_matrix_init(this);
162+
163+
} else if (argc == 2) {
164+
165+
// SymEngine::SparseMatrix(no_rows, no_cols)
166+
VALUE val1 = rb_ary_shift(args);
167+
VALUE val2 = rb_ary_shift(args);
168+
169+
if((TYPE(val1) == T_FIXNUM || TYPE(val1) == T_BIGNUM)
170+
&& (TYPE(val2) == T_FIXNUM || TYPE(val2) == T_BIGNUM) ) {
171+
172+
unsigned long int rows = NUM2ULONG(val1);
173+
unsigned long int cols = NUM2ULONG(val2);
174+
sparse_matrix_rows_cols(this, rows, cols);
175+
176+
} else {
177+
178+
rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, or two Numerics expected.");
179+
180+
}
181+
} else {
182+
183+
rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, or two Numerics expected.");
184+
185+
}
186+
187+
return self;
188+
}

ext/symengine/ruby_matrix.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
void cmatrix_dense_free(void *ptr);
88
VALUE cmatrix_dense_alloc(VALUE klass);
99
VALUE cmatrix_dense_init(VALUE self, VALUE args);
10-
VALUE cmatrix_to_str(VALUE self);
10+
VALUE cmatrix_dense_to_str(VALUE self);
11+
12+
void cmatrix_sparse_free(void *ptr);
13+
VALUE cmatrix_sparse_alloc(VALUE klass);
14+
VALUE cmatrix_sparse_init(VALUE self, VALUE args);
15+
VALUE cmatrix_sparse_to_str(VALUE self);
1116

1217
#endif // RUBY_MATRIX_H_

ext/symengine/symengine.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,13 @@ void Init_symengine()
236236
// DenseMatrix Methods
237237
rb_define_alloc_func(c_dense_matrix, cmatrix_dense_alloc);
238238
rb_define_method(c_dense_matrix, "initialize", cmatrix_dense_init, -2);
239-
rb_define_method(c_dense_matrix, "to_s", cmatrix_to_str, 0);
239+
rb_define_method(c_dense_matrix, "to_s", cmatrix_dense_to_str, 0);
240+
241+
// SparseMatrix Methods
242+
rb_define_alloc_func(c_sparse_matrix, cmatrix_sparse_alloc);
243+
rb_define_method(c_sparse_matrix, "initialize", cmatrix_sparse_init, -2);
244+
rb_define_method(c_sparse_matrix, "to_s", cmatrix_sparse_to_str, 0);
245+
240246

241247
symengine_print_stack_on_segfault();
242248
}

lib/symengine/matrix_base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module SymEngine
22
class MatrixBase
33
def inspect
4-
"#<#{self.class}(#{to_s})>"
4+
"#<#{self.class}()>"
55
end
66
end
77
end

0 commit comments

Comments
 (0)