Skip to content

Commit ad72687

Browse files
committed
+ operator for DenceMatrix
1 parent c72348e commit ad72687

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

ext/symengine/ruby_matrix.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,55 @@ VALUE cmatrix_dense_submatrix(VALUE self, VALUE r1, VALUE c1, VALUE r2, VALUE c2
308308
cresult);
309309
return result;
310310
}
311+
312+
VALUE cmatrix_dense_rows(VALUE self)
313+
{
314+
CDenseMatrix *this;
315+
Data_Get_Struct(self, CDenseMatrix, this);
316+
return ULONG2NUM( dense_matrix_rows(this));
317+
}
318+
319+
320+
VALUE cmatrix_dense_cols(VALUE self)
321+
{
322+
CDenseMatrix *this;
323+
Data_Get_Struct(self, CDenseMatrix, this);
324+
return ULONG2NUM( dense_matrix_cols(this));
325+
}
326+
327+
VALUE cmatrix_dense_add(VALUE self, VALUE operand)
328+
{
329+
CDenseMatrix *this;
330+
Data_Get_Struct(self, CDenseMatrix, this);
331+
332+
CDenseMatrix *cresult;
333+
VALUE result;
334+
335+
cresult = dense_matrix_new();
336+
337+
char *s = rb_obj_classname(operand);
338+
339+
if(strcmp(s, "SymEngine::DenseMatrix") == 0) {
340+
// Matrix Addition
341+
CDenseMatrix *coperand;
342+
Data_Get_Struct(operand, CDenseMatrix, coperand);
343+
344+
dense_matrix_add_matrix(cresult, this, coperand);
345+
346+
result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free,
347+
cresult);
348+
349+
dense_matrix_free(coperand);
350+
} else {
351+
// Scalar Addition
352+
basic_struct *coperand = basic_new_heap();
353+
sympify(operand, coperand);
354+
355+
dense_matrix_add_scalar(cresult, this, coperand);
356+
result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free,
357+
cresult);
358+
basic_free_heap(coperand);
359+
}
360+
361+
return result;
362+
}

ext/symengine/ruby_matrix.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ VALUE cmatrix_dense_det(VALUE self);
1818
VALUE cmatrix_dense_inv(VALUE self);
1919
VALUE cmatrix_dense_transpose(VALUE self);
2020
VALUE cmatrix_dense_submatrix(VALUE self, VALUE r1, VALUE c1, VALUE r2, VALUE c2, VALUE r_, VALUE c_);
21+
VALUE cmatrix_dense_rows(VALUE self);
22+
VALUE cmatrix_dense_cols(VALUE self);
23+
VALUE cmatrix_dense_add(VALUE self, VALUE operand);
24+
VALUE cmatrix_dense_mul(VALUE self, VALUE operand);
2125

2226
void cmatrix_sparse_free(void *ptr);
2327
VALUE cmatrix_sparse_alloc(VALUE klass);

ext/symengine/symengine.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ void Init_symengine()
243243
rb_define_method(c_dense_matrix, "inv", cmatrix_dense_inv, 0);
244244
rb_define_method(c_dense_matrix, "transpose", cmatrix_dense_transpose, 0);
245245
rb_define_method(c_dense_matrix, "submatrix", cmatrix_dense_submatrix, 6);
246+
rb_define_method(c_dense_matrix, "rows", cmatrix_dense_rows, 0);
247+
rb_define_method(c_dense_matrix, "cols", cmatrix_dense_cols, 0);
248+
rb_define_method(c_dense_matrix, "+", cmatrix_dense_add, 1);
249+
//rb_define_method(c_dense_matrix, "*", cmatrix_dense_mul, 1);
250+
246251

247252
// SparseMatrix Methods
248253
rb_define_alloc_func(c_sparse_matrix, cmatrix_sparse_alloc);

0 commit comments

Comments
 (0)