# Is there any sense in trying to write a fast math?

**URL:** <https://www.ruby-forum.com/t/is-there-any-sense-in-trying-to-write-a-fast-math/240430>\
**Category:** JRuby\
**Created:** [April 14, 2014, 7:54am UTC](https://www.ruby-forum.com/t/is-there-any-sense-in-trying-to-write-a-fast-math/240430 "2014-04-14T07:54:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [April 14, 2014, 7:54am UTC](https://www.ruby-forum.com/t/is-there-any-sense-in-trying-to-write-a-fast-math/240430/1 "2014-04-14T07:54:30Z")

</div>

In processing ([processing.org](http://processing.org) programming language) many people  
implement fast sin/cos math using lookup tables, with either limited  
precision but accurate degree input. Or with limited precision (say 0.25  
degree) and limited accuracy, with radian input (and typically table is  
stored as array of floats). Is there any point in trying to wrap a java  
version into jruby (not for everyone, but just local to say  
ruby-processing) for graphical work.

On a related note I have written a 2D and 3D vector library  
[https://github.com/jashkenas/ruby-processing/blob/master/library/vecmath/lib/vec.rb](https://github.com/jashkenas/ruby-processing/blob/master/library/vecmath/lib/vec.rb)  
in purely ruby for use with ruby-processing, would I gain anything by  
re-implementing it in java and writing a jruby wrapper, like yokolets  
Fraction, that wraps Apache Commons Math API to provide a ruby version.  
[yokolet's notelets: Extending JRuby](http://yokolet.blogspot.co.uk/2011/05/extending-jruby.html).

---

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [April 26, 2014, 6:17pm UTC](https://www.ruby-forum.com/t/is-there-any-sense-in-trying-to-write-a-fast-math/240430/2 "2014-04-26T18:17:51Z")

</div>

OK no-one replied, I did more research and decided to put my foot in the  
water. Here is progress so far at implementing my 2D ruby vector library  
in JRuby (java)  
[Ruby Processing: Experiments with JRuby Integration (for ruby-processing)](http://learning-ruby-processing.blogspot.co.uk/2014/04/experiments-with-jruby-integration.html).

I did this because it looks to me that the biggest slowdown is calling  
java from ruby and vice-versa. I have yet to test performance, but it  
was an interesting exercise.

---

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [April 27, 2014, 4:32pm UTC](https://www.ruby-forum.com/t/is-there-any-sense-in-trying-to-write-a-fast-math/240430/3 "2014-04-27T16:32:30Z")

</div>

Hi Martin,

Why don’t you use MDMatrix class from MDArray?

I haven’t replied before because I don’t really understand why you need  
to  
do all those complex calls and don’t just import java methods to jruby.

Cheers,

Rodrigo  
Em 26/04/2014 13:18, “Martin P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) escreveu:

---

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [April 28, 2014, 8:36am UTC](https://www.ruby-forum.com/t/is-there-any-sense-in-trying-to-write-a-fast-math/240430/4 "2014-04-28T08:36:22Z")

</div>

I have now made my experimental JRuby extension available at github  
[https://github.com/ruby-processing/RVecMath](https://github.com/ruby-processing/RVecMath)  
I’m hesitant to offer it as possible template because it is my first  
attempt, and there is an absolute dearth of examples (apart from  
yokolets excellent example without which I would have never started).  
However judging my spec test it at least all seems to work. I have  
expanded yokolets example to include a static method (other than new)  
methods which take a variable number of arguments etc. Many of these  
additional features were partly achieved by trial and error. If I was  
confident I was right I would write a guide.

---

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [May 4, 2014, 10:29pm UTC](https://www.ruby-forum.com/t/is-there-any-sense-in-trying-to-write-a-fast-math/240430/5 "2014-05-04T22:29:19Z")

</div>

I have now added to the functionality of my Vecmath library  
[https://github.com/ruby-processing/RVecMath](https://github.com/ruby-processing/RVecMath) which will very likely get  
incorporated into ruby-processing. Perhaps the most useful feature is  
that of the ArcBall functionality, which can be implemented on the ruby  
side with a one liner (by making use of java reflection in processing).  
Less interesting is the degree input and degree precision sine / cosine  
lookup tables that manage at best 30-40% performance gain over regular  
Math.sin and Math.cos. In practice this (ie in a ruby-processing sketch)  
performance gain may be even more marginal.

---

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [April 27, 2014, 8:19pm UTC](https://www.ruby-forum.com/t/is-there-any-sense-in-trying-to-write-a-fast-math/240430/6 "2014-04-27T20:19:22Z")

</div>

I was only going on this  
[https://twitter.com/headius/status/349225854842306560](https://twitter.com/headius/status/349225854842306560) from Charlie. In  
my experiments it is almost pointless trying to do speed ups in java as  
these will not come through to ruby-processing performance. The point of  
ruby-processing is to be able to write regular ruby code. If you only  
wanted performance you would stick to vanilla processing. Performance is  
not bad, even with my Vecmath libary written in ruby, I reasoned that if  
it were pre-compiled it might work even better. From the users point of  
view my library will appear as though it were written in ruby.
