Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7aab5b3
Classes for Matrices
rajithv Jun 21, 2016
11badd1
Alloc and Free methods for MatrixBase
rajithv Jun 21, 2016
7f918e0
DenseMatrix alloc and free funcs
rajithv Jun 21, 2016
8de5d9f
DenseMatrix() and SymEngine::DenseMatrix(no_rows, no_cols) working
rajithv Jun 22, 2016
0b897b9
DenseMatrix(Array) and DenseMatrix(SymEngine::DenseMatrix) working
rajithv Jun 22, 2016
8f50218
inspect and to_s for MatrixBase
rajithv Jun 23, 2016
b719126
Changed dense_matrix to dense_matrix_init
rajithv Jun 23, 2016
d7c8b29
SparseMatrix ruby wrappers
rajithv Jun 23, 2016
fc9db03
Getter and setter for DenseMatrix
rajithv Jun 23, 2016
c72348e
det, inv, transpose and submatrix
rajithv Jun 23, 2016
ad72687
+ operator for DenceMatrix
rajithv Jun 26, 2016
423259b
* operator for DenseMatrix
rajithv Jun 26, 2016
4c40373
LU for DenseMatrix
rajithv Jun 27, 2016
e00ff7f
LDL, LU_solve, FFLU, FFLDU working
rajithv Jun 27, 2016
6d31369
convert() can convert 2D arrays into DenseMatrices
rajithv Jun 28, 2016
fb787a1
numpy-like functions ones and zeros
rajithv Jun 28, 2016
42d3e95
diag method of numpy-like functions
rajithv Jun 29, 2016
c07f5c7
All numpy-like functions wrapped for DenseMatrix
rajithv Jul 2, 2016
7f4c24d
SparseMatrix get and set methods
rajithv Jul 3, 2016
5c739d1
equal and not equal operators for DenseMatrix
rajithv Jul 4, 2016
ff06faa
Changed code due to changes in Cwrapper, started spec
rajithv Jul 6, 2016
6d6521f
matrix spec update
rajithv Jul 7, 2016
6a7fa2b
changed inspect for DenseMatrix
rajithv Jul 7, 2016
e447e97
Updated matrix spec, better grouping for tests and row, column tests
rajithv Jul 7, 2016
61cee1a
Removing unwanted files, and updating symengine version
rajithv Jul 8, 2016
aaa2aa8
Formatting and fixing warnings
rajithv Jul 8, 2016
c1b8e6e
Merge branch 'master' of https://github.com/symengine/symengine.rb in…
rajithv Jul 8, 2016
662680d
Improvement to specs
rajithv Jul 11, 2016
4820121
Restructuring matrix constructors
rajithv Jul 13, 2016
3389034
Merge branch 'master' of https://github.com/symengine/symengine.rb in…
rajithv Jul 30, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/symengine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(RUBY_WRAPPER_SRC
ruby_function.c
ruby_ntheory.c
ruby_utils.c
ruby_matrix.c
symengine_utils.c
symengine.c
)
Expand Down
123 changes: 123 additions & 0 deletions ext/symengine/ruby_matrix.c
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does klass stand for here?

Copy link
Contributor Author

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Format with clang-format

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will format all code with clang-format. Please disregard formating issues for now.


// 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed in C++ layer, but not needed in Ruby right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@isuruf should I remove it?

I suppose this can be used to generate an empty Matrix of size RxC, where individual elements can be assigned with set. But again, there's no point in doing that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for that use case, it's better to use zeros

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;
}
12 changes: 12 additions & 0 deletions ext/symengine/ruby_matrix.h
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_
13 changes: 13 additions & 0 deletions ext/symengine/symengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "ruby_function.h"
#include "ruby_ntheory.h"
#include "ruby_utils.h"
#include "ruby_matrix.h"
#include "symengine_utils.h"
#include "symengine.h"

Expand Down Expand Up @@ -224,6 +225,18 @@ void Init_symengine()
rb_define_module_function(m_symengine, "fibonacci", cntheory_fibonacci, 1);
rb_define_module_function(m_symengine, "lucas", cntheory_lucas, 1);
rb_define_module_function(m_symengine, "binomial", cntheory_binomial, 2);

// MatrixBase Class
c_matrix_base = rb_define_class_under(m_symengine, "MatrixBase", rb_cObject);

// DenseMatrix and SparseMatrix Classes
c_dense_matrix = rb_define_class_under(m_symengine, "DenseMatrix", c_matrix_base);
c_sparse_matrix = rb_define_class_under(m_symengine, "SparseMatrix", c_matrix_base);

// DenseMatrix Methods
rb_define_alloc_func(c_dense_matrix, cmatrix_dense_alloc);
rb_define_method(c_dense_matrix, "initialize", cmatrix_dense_init, -2);
rb_define_method(c_dense_matrix, "to_s", cmatrix_to_str, 0);

symengine_print_stack_on_segfault();
}
3 changes: 3 additions & 0 deletions ext/symengine/symengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,8 @@ VALUE c_atanh;
VALUE c_acsch;
VALUE c_asech;
VALUE c_acoth;
VALUE c_matrix_base;
VALUE c_dense_matrix;
VALUE c_sparse_matrix;

#endif // SYMENGINE_H_
1 change: 1 addition & 0 deletions lib/symengine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ def Function(n)
require 'symengine/complex'
require 'symengine/complex_double'
require 'symengine/undef_function'
require 'symengine/matrix_base'
7 changes: 7 additions & 0 deletions lib/symengine/matrix_base.rb
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