This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Implementing numerical algorithms in Scala

2 replies
Tim van Beek
Joined: 2011-02-19,
User offline. Last seen 1 year 33 weeks ago.

Hi all,

I'm new to Scala and interested in Scala mostly because I need a
modern object-oriented programming language executable in a virtual
machine for the implementation of some basic and some advanced
numerical algorithms (like the ones described in the book "numerical
recipes"). It seems to be that Scala is much better suited for this
task than e.g. Java (which today has - to my knowledge - basically no
usable advanced numerical mathematics libraries), both because it
offers functional programming and because it is possible to implement
algebraic operations and let them look like basic operators like

c = a + b

where a, b, c are instances of a custom implementation of a field like
the complex numbers or the octonions. This may seem to be a trivial
point, but it seems to me that Java or .NET do not have much success
in the scientific computing community partly because the syntax looks
so clumsy to people used to C++ or FORTRAN.

I'd like to know if a similar project is under way already and what
others think about such a project (is there a better candidate than
Scala?).

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: Implementing numerical algorithms in Scala

On Sat, Feb 19, 2011 at 3:53 PM, Tim wrote:
> Hi all,
>
> I'm new to Scala and interested in Scala mostly because I need a
> modern object-oriented programming language executable in a virtual
> machine for the implementation of some basic and some advanced
> numerical algorithms (like the ones described in the book "numerical
> recipes"). It seems to be that Scala is much better suited for this
> task than e.g. Java (which today has - to my knowledge - basically no
> usable advanced numerical mathematics libraries), both because it
> offers functional programming and because it is possible to implement
> algebraic operations and let them look like basic operators like
>
>  c = a + b
>
> where a, b, c are instances of a custom implementation of a field like
> the complex numbers or the octonions. This may seem to be a trivial
> point, but it seems to me that Java or .NET do not have much success
> in the scientific computing community partly because the syntax looks
> so clumsy to people used to C++ or FORTRAN.
>
> I'd like to know if a similar project is under way already and what
> others think about such a project (is there a better candidate than
> Scala?).

Take a look at Scalala

http://code.google.com/p/scalala/wiki/QuickStart

-jason

pkolaczk
Joined: 2010-01-14,
User offline. Last seen 2 years 38 weeks ago.
Re: Implementing numerical algorithms in Scala

Hi,

We use Scala for numerical purposes - we build a mixed-mode circuit
simulator in it - quite a long time and I'm very happy with it. I would
never, ever change it to C++. The syntax is ok, but the syntax is
actually not that important as it seems - well, we have some matrix
operations here and there, but this is just a minor part of the code.
More important is the possibility to build powerful abstractions, and at
this Scala really rocks, compared to Java or C++.

As for performance, it could be better, but the culprit is most often
the JVM, not Scala. Inability to allocate small structures on the stack
(like complex numbers or tuples) is a pain, if you want to get 100% of
performance. Also JVM could be much better if it could use the generic
type information at runtime to specialize some code or remove unneeded
casts.

Fortunately, on the other hand, because of higher productivity you gain
from using Scala, you have more time for designing your algorithms.
Additionally cleaner designs mean more ocasions to gain performance.

We've just made some performance tests comparing our solution to some
other Berkeley SPICE 3 based commercial products created in C/C++ and
the times of collecting 100000 data points of some simple RLC circuit
are as follows:

- CircuitLogix (C++?): 5.9 secs
- TopSpice (C and C++): 3.2 secs
- our solution, (java -server): 2.7 secs

And we still see a lot of room for optimisation, which means
improvements of 2x or 3x are to be expected. Or even more, if we go
parallel, which should be easy when 2.9 finally comes out.

Hope my opinion helps.

Regards,
Piotr Kołaczkowski

W dniu 2011-02-19 15:53, Tim pisze:
> Hi all,
>
> I'm new to Scala and interested in Scala mostly because I need a
> modern object-oriented programming language executable in a virtual
> machine for the implementation of some basic and some advanced
> numerical algorithms (like the ones described in the book "numerical
> recipes"). It seems to be that Scala is much better suited for this
> task than e.g. Java (which today has - to my knowledge - basically no
> usable advanced numerical mathematics libraries), both because it
> offers functional programming and because it is possible to implement
> algebraic operations and let them look like basic operators like
>
> c = a + b
>
> where a, b, c are instances of a custom implementation of a field like
> the complex numbers or the octonions. This may seem to be a trivial
> point, but it seems to me that Java or .NET do not have much success
> in the scientific computing community partly because the syntax looks
> so clumsy to people used to C++ or FORTRAN.
>
> I'd like to know if a similar project is under way already and what
> others think about such a project (is there a better candidate than
> Scala?).
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland