Skip to content

Commit 42d3e95

Browse files
committed
diag method of numpy-like functions
1 parent fb787a1 commit 42d3e95

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

ext/symengine/ruby_matrix.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,12 +567,54 @@ VALUE cmatrix_dense_zeros(VALUE self, VALUE r, VALUE c)
567567

568568
return result;
569569
}
570-
/*
570+
571571
VALUE cmatrix_dense_diag(VALUE self, VALUE args)
572572
{
573+
int argc = RARRAY_LEN(args);
574+
575+
if (argc != 1 && argc != 2){
576+
rb_raise(rb_eTypeError, "Wrong number of arguments");
577+
}
578+
579+
CDenseMatrix *cresult;
580+
cresult = dense_matrix_new();
581+
VALUE result;
582+
583+
VALUE operand = rb_ary_shift(args);
584+
char *s = rb_obj_classname(operand);
585+
586+
CVecBasic *cargs;
587+
588+
if(strcmp(s, "Array") == 0) {
589+
cargs = vecbasic_new();
590+
basic x;
591+
basic_new_stack(x);
592+
int cols = RARRAY_LEN(operand);
593+
int j;
594+
for(j = 0; j < cols; j++) {
595+
sympify(rb_ary_shift(operand), x);
596+
vecbasic_push_back(cargs, x);
597+
}
598+
basic_free_stack(x);
599+
} else {
600+
rb_raise(rb_eTypeError, "Invalid Argument type");
601+
}
602+
603+
long int k = 0;
604+
605+
if (argc == 2) { // With offset
606+
k = NUM2LONG(rb_ary_shift(args));
607+
}
608+
609+
dense_matrix_diag(cresult, cargs, k);
610+
611+
result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free,
612+
cresult);
613+
vecbasic_free(cargs);
573614

615+
return result;
574616
}
575-
617+
/*
576618
VALUE cmatrix_dense_eye(VALUE self, VALUE args)
577619
{
578620

ext/symengine/ruby_matrix.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ VALUE cmatrix_dense_FFLU(VALUE self);
2929
VALUE cmatrix_dense_FFLDU(VALUE self);
3030
VALUE cmatrix_dense_ones(VALUE self, VALUE r, VALUE c);
3131
VALUE cmatrix_dense_zeros(VALUE self, VALUE r, VALUE c);
32-
/*VALUE cmatrix_dense_diag(VALUE self, VALUE args);
32+
VALUE cmatrix_dense_diag(VALUE self, VALUE args);/*
3333
VALUE cmatrix_dense_eye(VALUE self, VALUE args);*/
3434

3535
void cmatrix_sparse_free(void *ptr);

ext/symengine/symengine.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ void Init_symengine()
256256
// Numpy-like methods for DenseMatrix as module level methods
257257
rb_define_module_function(m_symengine, "ones", cmatrix_dense_ones, 2);
258258
rb_define_module_function(m_symengine, "zeros", cmatrix_dense_zeros, 2);
259-
/*rb_define_module_function(m_symengine, "diag", cmatrix_dense_diag, -2);
260-
rb_define_module_function(m_symengine, "eye", cmatrix_dense_eye, -2);*/
259+
rb_define_module_function(m_symengine, "diag", cmatrix_dense_diag, -2);
260+
/*rb_define_module_function(m_symengine, "eye", cmatrix_dense_eye, -2);*/
261261

262262

263263
// SparseMatrix Methods

0 commit comments

Comments
 (0)