-
Notifications
You must be signed in to change notification settings - Fork 15
Matrix Ruby Wrappers #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 6 commits
7aab5b3
11badd1
7f918e0
8de5d9f
0b897b9
8f50218
b719126
d7c8b29
fc9db03
c72348e
ad72687
423259b
4c40373
e00ff7f
6d31369
fb787a1
42d3e95
c07f5c7
7f4c24d
5c739d1
ff06faa
6d6521f
6a7fa2b
e447e97
61cee1a
aaa2aa8
c1b8e6e
662680d
4820121
3389034
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| #include "ruby_matrix.h" | ||
|
|
||
| void cmatrix_dense_free(void *ptr) | ||
| { | ||
| CDenseMatrix *mat_ptr = ptr; | ||
| dense_matrix_free(mat_ptr); | ||
| } | ||
|
|
||
| VALUE cmatrix_dense_alloc(VALUE klass) | ||
| { | ||
| CDenseMatrix *mat_ptr = dense_matrix_new(); | ||
| return Data_Wrap_Struct(klass, NULL, cmatrix_dense_free, mat_ptr); | ||
| } | ||
|
|
||
| VALUE cmatrix_to_str(VALUE self) | ||
| { | ||
| CDenseMatrix *this; | ||
| char *str_ptr; | ||
| VALUE result; | ||
|
|
||
| Data_Get_Struct(self, CDenseMatrix, this); | ||
|
|
||
| str_ptr = matrix_str(this); | ||
| result = rb_str_new_cstr(str_ptr); | ||
| basic_str_free(str_ptr); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
|
|
||
| VALUE cmatrix_dense_init(VALUE self, VALUE args) | ||
| { | ||
| int argc = RARRAY_LEN(args); | ||
| CDenseMatrix *this; | ||
| Data_Get_Struct(self, CDenseMatrix, this); | ||
|
|
||
| if (argc == 0) { | ||
|
|
||
| // SymEngine::DenseMatrix() | ||
| dense_matrix(this); | ||
|
|
||
| } else if (argc == 1) { | ||
| // SymEngine::DenseMatrix(SymEngine::DenseMatrix) OR | ||
| // SymEngine::DenseMatrix(NMatrix) OR | ||
| // SymEngine::DenseMatrix(Array) | ||
|
|
||
| VALUE operand = rb_ary_shift(args); | ||
| char *s = rb_obj_classname(operand); | ||
|
|
||
| if(strcmp(s, "SymEngine::DenseMatrix") == 0) { | ||
|
||
|
|
||
| // SymEngine::DenseMatrix(SymEngine::DenseMatrix) | ||
| CDenseMatrix *temp; | ||
| Data_Get_Struct(operand, CDenseMatrix, temp); | ||
| dense_matrix_set(this, temp); | ||
|
|
||
| } else if(strcmp(s, "NMatrix") == 0) { | ||
|
|
||
| // SymEngine::DenseMatrix(NMatrix) | ||
| rb_raise(rb_eTypeError, "TODO"); | ||
|
|
||
| } else if(strcmp(s, "Array") == 0) { | ||
|
|
||
| // SymEngine::DenseMatrix(Array) | ||
| int counter = 0; | ||
|
|
||
| int rows = RARRAY_LEN(operand); | ||
| int cols = -1; | ||
| CVecBasic *cargs = vecbasic_new(); | ||
| basic x; | ||
| basic_new_stack(x); | ||
| int i; | ||
| for (i = 0; i < rows; i++) { | ||
| int j; | ||
| VALUE row = rb_ary_shift(operand); | ||
| if ( cols == -1 ) { | ||
| cols = RARRAY_LEN(row); | ||
| // Checking all rows for same col length | ||
| } else if (cols != RARRAY_LEN(row)) { | ||
| rb_raise(rb_eTypeError, "2D Array's rows contain different column counts"); | ||
| } | ||
|
|
||
| for(j = 0; j < cols; j++) { | ||
| sympify(rb_ary_shift(row), x); | ||
| vecbasic_push_back(cargs, x); | ||
| counter++; | ||
| } | ||
| } | ||
| dense_matrix_set_vec(this, rows, cols, cargs); | ||
|
|
||
| basic_free_stack(x); | ||
| vecbasic_free(cargs); | ||
|
|
||
| } else { | ||
| rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single SymEngine::DenseMatrix, a single NMatrix, a single Array or two Numerics expected."); | ||
| } | ||
|
|
||
| } else if (argc == 2) { | ||
|
|
||
| // SymEngine::DenseMatrix(no_rows, no_cols) | ||
|
||
| VALUE val1 = rb_ary_shift(args); | ||
| VALUE val2 = rb_ary_shift(args); | ||
|
|
||
| if((TYPE(val1) == T_FIXNUM || TYPE(val1) == T_BIGNUM) | ||
| && (TYPE(val2) == T_FIXNUM || TYPE(val2) == T_BIGNUM) ) { | ||
|
|
||
| unsigned long int rows = NUM2ULONG(val1); | ||
| unsigned long int cols = NUM2ULONG(val2); | ||
| dense_matrix_rows_cols(this, rows, cols); | ||
|
|
||
| } else { | ||
|
|
||
| rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single SymEngine::DenseMatrix, a single NMatrix, a single Array or two Numerics expected."); | ||
|
|
||
| } | ||
| } else { | ||
|
|
||
| rb_raise(rb_eTypeError, "Invalid Arguments. No Arguments, a single SymEngine::DenseMatrix, a single NMatrix, a single Array or two Numerics expected."); | ||
|
|
||
| } | ||
|
|
||
| return self; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #ifndef RUBY_MATRIX_H_ | ||
| #define RUBY_MATRIX_H_ | ||
|
|
||
| #include <ruby.h> | ||
| #include <symengine/cwrapper.h> | ||
|
|
||
| void cmatrix_dense_free(void *ptr); | ||
| VALUE cmatrix_dense_alloc(VALUE klass); | ||
| VALUE cmatrix_dense_init(VALUE self, VALUE args); | ||
| VALUE cmatrix_to_str(VALUE self); | ||
|
|
||
| #endif // RUBY_MATRIX_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| module SymEngine | ||
| class MatrixBase | ||
| def inspect | ||
| "#<#{self.class}(#{to_s})>" | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does
klassstand for here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was writing it by taking the example of cbasic_alloc. So assumed, the klass is something which is called when allocating the space bound defined in
rb_define_alloc_func(c_dense_matrix, cmatrix_dense_alloc);Shoud I remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
klassis the class object. https://github.com/symengine/symengine.rb/blob/master/ext/symengine/ruby_basic.h#L13