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

Feature request: Element multiplication #3

Closed
dicom opened this issue Feb 4, 2011 · 2 comments
Closed

Feature request: Element multiplication #3

dicom opened this issue Feb 4, 2011 · 2 comments

Comments

@dicom
Copy link

dicom commented Feb 4, 2011

First of all, thanks for your work on this great Ruby gem!

On one of my recent projects, I needed to multiply all the elements of my NArray. I searched the documentation, hoping to find an equivalent to the .sum method, but it seems to me there is none. I think it would be useful to have such a method included in the NArray library. Below I will paste the solution I used, extending the narray class and converting to a ruby array and finally multiplying the elements:

# Multiplies all elements in the array and returns the resulting number:
class NArray
  def multiply
    self.to_a.inject(:*)
  end
end

a = NArray[2,3,7]
a.sum
=> 12
a.multiply
=> 42

Best regards,
Chris

@masa16
Copy link
Owner

masa16 commented Feb 4, 2011

added:
$ irb -I. -Ilib -r narray
irb(main):001:0> NArray.float(5).indgen!(1).prod
=> 120.0
irb(main):002:0> NArray.float(5).indgen!(1).cumprod
=> NArray.float(5):
[ 1.0, 2.0, 6.0, 24.0, 120.0 ]

@dicom
Copy link
Author

dicom commented Feb 4, 2011

That's great. Very fast reaction!
Thank you very much.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants