-
Notifications
You must be signed in to change notification settings - Fork 10
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
keithdoggett
wants to merge
1
commit into
SciRuby:master
Choose a base branch
from
keithdoggett:sum-method
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Sum Method #51
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
||
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++){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think that this line isn't necessary