Skip to content

Commit 6d31369

Browse files
committed
convert() can convert 2D arrays into DenseMatrices
1 parent e00ff7f commit 6d31369

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

ext/symengine/ruby_matrix.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ VALUE cmatrix_dense_init(VALUE self, VALUE args)
100100
for (i = 0; i < rows; i++) {
101101
int j;
102102
VALUE row = rb_ary_shift(operand);
103+
104+
s = rb_obj_classname(row);
105+
if(strcmp(s, "Array") != 0){
106+
rb_raise(rb_eTypeError, "Not a 2D Array");
107+
}
108+
103109
if ( cols == -1 ) {
104110
cols = RARRAY_LEN(row);
105111
// Checking all rows for same col length

ext/symengine/ruby_utils.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,56 @@ VALUE cutils_sympify(VALUE self, VALUE operand)
44
{
55

66
VALUE result;
7+
char *s = rb_obj_classname(operand);
8+
9+
if(strcmp(s, "Array") == 0) {
10+
11+
CDenseMatrix *mat_result = dense_matrix_new();
12+
int counter = 0;
13+
14+
int rows = RARRAY_LEN(operand);
15+
int cols = -1;
16+
CVecBasic *cargs = vecbasic_new();
17+
basic x;
18+
basic_new_stack(x);
19+
int i;
20+
21+
for (i = 0; i < rows; i++) {
22+
int j;
23+
VALUE row = rb_ary_shift(operand);
24+
s = rb_obj_classname(row);
25+
if(strcmp(s, "Array") != 0){
26+
rb_raise(rb_eTypeError, "2D Array is required");
27+
}
28+
29+
if ( cols == -1 ) {
30+
cols = RARRAY_LEN(row);
31+
// Checking all rows for same col length
32+
} else if (cols != RARRAY_LEN(row)) {
33+
rb_raise(rb_eTypeError, "2D Array's rows contain different column counts");
34+
}
35+
36+
for(j = 0; j < cols; j++) {
37+
sympify(rb_ary_shift(row), x);
38+
vecbasic_push_back(cargs, x);
39+
counter++;
40+
}
41+
}
42+
43+
dense_matrix_set_vec(mat_result, rows, cols, cargs);
44+
45+
basic_free_stack(x);
46+
vecbasic_free(cargs);
47+
48+
result = Data_Wrap_Struct(c_dense_matrix, NULL, dense_matrix_free, mat_result);
49+
} else {
750

851
basic_struct *cbasic_operand;
952
cbasic_operand = basic_new_heap();
1053

1154
sympify(operand, cbasic_operand);
1255
result = Data_Wrap_Struct(Klass_of_Basic(cbasic_operand), NULL,
1356
cbasic_free_heap, cbasic_operand);
14-
57+
}
1558
return result;
1659
}

ext/symengine/ruby_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef RUBY_UTILS_H_
22
#define RUBY_UTILS_H_
33

4+
5+
#include "symengine.h"
46
#include "symengine_utils.h"
57

68
// Returns the Ruby Value after going through sympify

ext/symengine/symengine_utils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void sympify(VALUE operand2, basic_struct *cbasic_operand2)
8383
{
8484
VALUE ret = check_sympify(operand2, cbasic_operand2);
8585
if (ret == Qfalse) {
86-
rb_raise(rb_eTypeError, "%s can't be coerced into SymEngine::Basic",
86+
rb_raise(rb_eTypeError, "%s can't be coerced into a SymEngine type.",
8787
rb_obj_classname(operand2));
8888
}
8989
}

0 commit comments

Comments
 (0)