@@ -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+
915VALUE 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+ }
0 commit comments