SciRuby / nmatrix

Dense and sparse linear algebra library for Ruby via SciRuby
Other
469 stars 133 forks source link

More user-friendly SVD interface #609

Open v0dro opened 6 years ago

v0dro commented 6 years ago

Currently to calculate SVD we need to call a function like gesvd. Having a simple Ruby wrapper over this called svd would be nicer.

wlevine commented 6 years ago

I think our current gesvd method actually has a pretty friendly interface (see http://sciruby.com/nmatrix/docs/NMatrix.html#method-i-gesvd ). The only thing unfriendly about it is the name. Is there anything else you had in mind?

v0dro commented 6 years ago

Just the name actually. And in case we support sparse matrices in the future it should also call the respective sparse method via svd.

gavrieltal commented 6 years ago

Hi, I'm new to Ruby and SciRuby and looking to get involved in an open source project, so sorry if I lack a concept of the organization of the files here. Right now, I just know that the gesvd function comes from lapack and atlas.

Would it be enough to add alias :gesvd :svdin lib/nmatrix/lapack_core.rb, lib/nmatrix/cruby/math.rb, lib/nmatrix/atlas.rb, and lib/nmatrix/lapacke.rb? There is no gesvd definition in the hyperlinked file lib/nmatrix/math.rb.

Also, would you expect Spec tests to demonstrate that the alias works?

Thanks

translunar commented 6 years ago

I don't think you'd add it in all of those places. Probably just lib/nmatrix/math.rb, or perhaps the math.rb in cruby/ (which place depends on whether there's a gesvd in the JRuby NMatrix or not). It's also possible JRuby has a different svd interface (I hope not), in which case you'd want to write a function in each of the two (C and J) math.rb files which provides a common interface.

Does that make sense?

Also, welcome. :)

gavrieltal commented 6 years ago

Based on github repo searches, gesvd is not defined in either lib/nmatrix/math.rb or lib/nmatrix/jruby/math.rb, only under cruby. So I suppose I would just add it under cruby?

Also, would you want to have spec tests checking that the alias works?

And, would you also like aliases for gesvd!, gesdd, and gesdd!?

Thanks for the welcome.

translunar commented 6 years ago

So, gesvd is in cruby because it's an interface for lapack/atlas/etc., which only exists on the C side. We should really have a common SVD interface for both JRuby and MRI (cruby), and that would go in lib/nmatrix/math.rb. Does that make sense?

It sounds like, though, it's going to be more than a simple alias — it's about coming up with an SVD interface that works for both JRuby and MRI and allows both to be executed as efficiently as possible. And yes, you would need specs for it.

Whether the ! functions are included just depends on whether there's an in-place JRuby SVD. There probably is, in which case, yes, you'd use svd!. If not, you'd raise a NotImplementedError when jruby? is true.

gavrieltal commented 6 years ago

I'm trying to figure out how to install nmatrix JRuby for the first time to try my work. Perhaps it's just inexperience with JRuby, but it seems hard to figure out. In addition to following the standard README instructions up until rake jruby, here is what I've tried:

gavriel@stockholm:~/Desktop/ruby/local.nmatrix$ rake jruby
x86_64-linux-gnu
Please run with JRuby

gavriel@stockholm:~/Desktop/ruby/local.nmatrix$ jruby -S rake jruby
Your bundle is locked to rake (10.5.0), but that version could not be found in any of the sources listed in your Gemfile. If you haven't changed sources, that means the author of rake (10.5.0) has removed it. You'll need to update your bundle to a different version of rake (10.5.0) that hasn't been removed in order to install.
Run `bundle install` to install missing gems

gavriel@stockholm:~/Desktop/ruby/local.nmatrix$ jruby -S rake
Your bundle is locked to rake (10.5.0), but that version could not be found in any of the sources listed in your Gemfile. If you haven't changed sources, that means the author of rake (10.5.0) has removed it. You'll need to update your bundle to a different version of rake (10.5.0) that hasn't been removed in order to install.
Run `bundle install` to install missing gems

gavriel@stockholm:~/Desktop/ruby/local.nmatrix$ sudo jruby -S bundle install
LoadError: no such file to load -- /usr/share/rubygems-integration/all/gems/bundler-1.13.6/exe/bundle
    load at org/jruby/RubyKernel.java:1059
  (root) at /usr/local/bin/bundle:22

gavriel@stockholm:~/Desktop/ruby/local.nmatrix$ sudo jruby -S bundle install rake
LoadError: no such file to load -- /usr/share/rubygems-integration/all/gems/bundler-1.13.6/exe/bundle
    load at org/jruby/RubyKernel.java:1059
  (root) at /usr/local/bin/bundle:22

gavriel@stockholm:~/Desktop/ruby/local.nmatrix$ sudo jruby -S gem install nmatrix
Building native extensions.  This could take a while...
ERROR:  Error installing nmatrix:
        ERROR: Failed to build gem native extension.

    /usr/share/jruby/bin/jruby -r ./siteconf20180820-9892-b6ltlv.rb extconf.rb
NotImplementedError: C extension support is not enabled. Pass -Xcext.enabled=true to JRuby or set JRUBY_OPTS.

   (root) at /usr/share/jruby/lib/ruby/shared/mkmf.rb:8
  require at org/jruby/RubyKernel.java:1040
   (root) at /usr/share/jruby/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:1
  require at /usr/share/jruby/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:54
  require at org/jruby/RubyKernel.java:1040
   (root) at /usr/share/jruby/lib/ruby/gems/shared/gems/nmatrix-0.2.4/lib/nmatrix/mkmf.rb:1
   (root) at extconf.rb:28

extconf failed, uncaught signal 1

Gem files will remain installed in /usr/share/jruby/lib/ruby/gems/shared/gems/nmatrix-0.2.4 for inspection.
Results logged to /usr/share/jruby/lib/ruby/gems/shared/extensions/universal-java-1.8/1.9/nmatrix-0.2.4/gem_make.out

gavriel@stockholm:~/Desktop/ruby/local.nmatrix$ sudo jruby -Xcext.enabled=true -S gem install nmatrix
Building native extensions.  This could take a while...
ERROR:  Error installing nmatrix:
        ERROR: Failed to build gem native extension.

    /usr/share/jruby/bin/jruby -r ./siteconf20180820-9969-4u2l4a.rb extconf.rb
NotImplementedError: C extension support is not enabled. Pass -Xcext.enabled=true to JRuby or set JRUBY_OPTS.

   (root) at /usr/share/jruby/lib/ruby/shared/mkmf.rb:8
  require at org/jruby/RubyKernel.java:1040
   (root) at /usr/share/jruby/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:1
  require at /usr/share/jruby/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:54
  require at org/jruby/RubyKernel.java:1040
   (root) at /usr/share/jruby/lib/ruby/gems/shared/gems/nmatrix-0.2.4/lib/nmatrix/mkmf.rb:1
   (root) at extconf.rb:28

extconf failed, uncaught signal 1

Gem files will remain installed in /usr/share/jruby/lib/ruby/gems/shared/gems/nmatrix-0.2.4 for inspection.
Results logged to /usr/share/jruby/lib/ruby/gems/shared/extensions/universal-java-1.8/1.9/nmatrix-0.2.4/gem_make.out
gavriel@stockholm:~/Desktop/ruby/local.nmatrix$ 
translunar commented 6 years ago

@gavrieltal I'm not sure how your comment relates to this issue. Can you please open a separate issue if you want help?