Skip to content
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

Add Sum Method #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
83 changes: 83 additions & 0 deletions ext/elementwise.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,89 @@ VALUE nm_sin(VALUE self){
return Data_Wrap_Struct(NMatrix, NULL, nm_free, result);
}

/*
* Elementwise sum operator.
* Takes in the given matrix
* and returns the sum of all the elements of the matrix
*/
VALUE nm_sum(VALUE self){
nmatrix* input;
Data_Get_Struct(self, nmatrix, input);

VALUE result;

Choose a reason for hiding this comment

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

I think that this line isn't necessary


switch(input->dtype){
case nm_bool:
{
bool* input_elements = (bool*)input->elements;
int sum = 0;
for(size_t index = 0; index < input->count; index++)
{
sum += input_elements[index];
}
result = INT2NUM(sum);
break;
}
case nm_int:
{
int* input_elements = (int*)input->elements;
int sum = 0;
for(size_t index = 0; index < input->count; index++){
sum += input_elements[index];
}
result = INT2NUM(sum);
break;
}
case nm_float32:
{
float* input_elements = (float*)input->elements;
float sum = 0;
for(size_t index = 0; index < input->count; index++){
sum += sin(input_elements[index]);
}
result = DBL2NUM((double)sum);
break;
}
case nm_float64:
{
double* input_elements = (double*)input->elements;
double sum = 0;
for(size_t index = 0; index < input->count; index++){
sum += input_elements[index];
}
result = DBL2NUM(sum);
break;
}
case nm_complex32:
{
complex float* input_elements = (complex float*)input->elements;
complex float sum = 0 + 0*I;
for(size_t index = 0; index < input->count; index++){
sum += input_elements[index];
}
double real = (double)creal(sum);
double imag = (double)cimag(sum);
result = rb_Complex(DBL2NUM(real), DBL2NUM(imag));
break;
}
case nm_complex64:
{
complex double* input_elements = (complex double*)input->elements;
complex double sum = 0 + 0*I;
for(size_t index = 0; index < input->count; index++){

Choose a reason for hiding this comment

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

I have seen that you have this cycle in each case of this condition, I think that you could move this in a function, WDYT?. Also I think that you could create a function that calcule this operation and in each case "cast" the response, but maybe this could be weird 🤔

sum += input_elements[index];
}
double real = creal(sum);
double imag = cimag(sum);
result = rb_Complex(DBL2NUM(real), DBL2NUM(imag));
break;
}
}

return result;
}

#define DEF_UNARY_RUBY_ACCESSOR(oper, name) \
static VALUE nm_##name(VALUE self) { \
nmatrix* input; \
Expand Down
2 changes: 2 additions & 0 deletions ext/ruby_nmatrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ DECL_ELEMENTWISE_RUBY_ACCESSOR(divide)


VALUE nm_sin(VALUE self);
VALUE nm_sum(VALUE self);

#define DECL_UNARY_RUBY_ACCESSOR(name) static VALUE nm_##name(VALUE self);
DECL_UNARY_RUBY_ACCESSOR(cos)
Expand Down Expand Up @@ -609,6 +610,7 @@ void Init_nmatrix() {
rb_define_method(NMatrix, "*", nm_multiply, 1);
rb_define_method(NMatrix, "/", nm_divide, 1);

rb_define_method(NMatrix, "sum", nm_sum, 0);
rb_define_method(NMatrix, "sin", nm_sin, 0);
rb_define_method(NMatrix, "cos", nm_cos, 0);
rb_define_method(NMatrix, "tan", nm_tan, 0);
Expand Down
27 changes: 26 additions & 1 deletion test/elementwise_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def setup

@boolean_left = NMatrix.new [2,2],[true, false, true, false], :nm_bool
@boolean_right = NMatrix.new [2,2],[true, true, false, false], :nm_bool

@int = NMatrix.new [2, 2], [1, 2, 3, 4], :nm_int
end

def test_add
Expand Down Expand Up @@ -49,6 +51,30 @@ def test_subtract
assert_equal answer, result
end

def test_sum
result = 12.4
answer = @left.sum
assert_in_delta answer, result, 0.01
end

def test_sum_bool
result = 2
answer = @boolean_left.sum
assert_equal answer, result
end

def test_sum_int
result = 10
answer = @int.sum
assert_equal answer, result
end

def test_sum_complex
result = (12.4 + 0.0i)
answer = @complex_left.sum
assert_in_delta answer, result, 0.01
end

def test_sin
result = NMatrix.new [2,2], @left.elements.map{ |x| Math.send(:sin, x) }
answer = @left.sin
Expand All @@ -66,5 +92,4 @@ def test_tan
answer = @left.tan
assert_equal answer, result
end

end
1 change: 0 additions & 1 deletion test/nmatrix_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,4 @@ def test_slicing
assert_equal @m[0, 0..1, 0..1], @s
assert_equal @m_int[0, 0..1, 0..1], @s_int
end

end