tags:

views:

2483

answers:

7

Unmanaged languages notwithstanding, is F# really better than C# for implementing math? And if that's the case, why?

A: 

I am not sure if its better or worse but there is certainly a difference in the approach. Static languages over specify how a problem will be solved. Functional languages like F# or Haskell do not do that and are more tailored at how a mathematician would solve a particular problem. Then you have books like this that tout python to be good at it. If you are talking from a performance point of view nothing can beat C. If you are talking from libraries I believe Functional Langauges (F# and the likes), Fortan (yes its not dead yet), Python have excellent libraries for math.

Perpetualcoder
+3  A: 

This post looks like it might be relevant: http://fsharpnews.blogspot.com/2007/05/ffts-again.html

Also: http://stackoverflow.com/questions/144227/c-f-performance-comparison

The biggest advantage for pure math is what PerpetualCoder said, F# looks more like a math problem so it's going to be easier for a mathematician to write. It reminded me a lot of MATLAB when I looked at it.

jcollum
+5  A: 

I'm from a maths background, and have looked at F#, but I still prefer C# for most purposes. There are a couple of things that F# makes easier, but in general I still prefer C# by a large margin.

Some of the touted F# benefits (immutability, higher-order functions, etc) can still be done in C# (using delegates etc for the latter). This is even more apparent when using C# 3.0 with lambda support, which makes it very easy and expressive to declare functional code.

From a maintenance angle, I'm of the view that suitably named properties etc are easier to use (over full life-cycle) than tuples and head/tail lists, but that might just be me.

One of the areas where C# lets itself down for maths is in generics and their support for operators. So I spend some time addressing this ;-p My results are available in MiscUtil, with overview here.

Marc Gravell
A: 

One of the great advantages of functional languages is the fact they they can run on multi-processor or multi-core systems, in parallel without requiring you to change any code. That means you can speed up your algorithms by simply adding cores.

DanJ
Well, that applies to functional *code*, not just functional *languages*. See Parallel Extensions to do *exactly* this in C#. Parallel Extensions will (IIRC) be bolted into .NET 4.0
Marc Gravell
+20  A: 

Hi,

I think most of the important points were already mentioned by someone else:

  1. F# lets you solve problems in a way mathematicians think about them
  2. Thanks to higher-order functions, you can use simpler concepts to solve difficult problems
  3. Everything is immutable by default, which makes the program easier to understand (and also easier to parallelize)

It is definitely true that you can use some of the F# concepts in C# 3.0, but there are limitations. You cannot use any recursive computations (because C# doesn't have tail-recursion) and this is how you write primitive computations in functional/mathematical way. Also, writing complex higher order functions (that take other functions as arguments) in C# is difficult, because you have to write types explicitly (while in F#, types are inferred, but also automatically generalized, so you don't have to explicitly make a function generic).

Also, I think the following point from Marc Gravell isn't a valid objection:

From a maintenance angle, I'm of the view that suitably named properties etc are easier to use (over full life-cycle) than tuples and head/tail lists, but that might just be me.

This is of course true. However, the great thing about F# is that you can start writing the program using tuples & head/tail lists and later in the development process turn it into a program that uses .NET IEnumerables and types with properties (and that's how I believe typical F# programmer works*). Tuples etc. and F# interactive development tools give you a great way to quickly prototype solutions (and when doing something mathematical, this is essential because most of the development is just experimenting when you're looking for the best solution). Once you have the prototype, you can use simple source code transformations to wrap the code inisde an F# type (which can also be used from C# as an ordinary class). F# also gives you a lot of ways to optimize the code later in terms of performance.

This gives you the benefits of easy to use langauges (e.g. Python), which many people use for prototyping phase. However, you don't have to rewrite the whole program later once you're done with prototyping using an efficient language (e.g. C++ or perhaps C#), because F# is both "easy to use" and "efficient" and you can fluently switch between these two styles.

(*) I also use this style in my functional programming book.

Tomas Petricek
+8  A: 

F# supports units of measure, which can be very useful for math work.

Craig Stuntz
It's a life saver for anything physics-related!
Benjol
@Benjol this one feature has helped me greatly in game development. <MP> <HP> <Pixel> <GameDay> ... time, networking (bytes, packets, etc). The thing I love about units of measure is that it allows me to name my variables for what they are used for and not worry about what they contain. e.g. `let damage = 5<HP>, 10<Shield>` instead of `let hpDamage, shieldDamage = 5, 10`
gradbot
@gradbot, it helps me because data often comes in in a mix of mm/m/km, and the internal representation is (supposed to be, at least) all in m, before being output again in various formats. Could UoM be the antidote to hungarian notation? :)
Benjol
+7  A: 

F# has many enormous benefits over C# in the context of mathematical programs:

  • F# interactive sessions let you run code on-the-fly to obtain results immediately and even visualize them, without having to build and execute a complete application.

  • F# supports some features that can provide massive performance improvements in the context of mathematics. Most notably, the combination of inline and higher-order functions allow mathematical code to be elegantly factored without adversely affecting performance. C# cannot express this.

  • F# supports some features that make it possible to implement mathematical concepts far more naturally than can be obtained in C#. For example, tail calls make it much easier to implement recurrence relations simply and reliably. C# cannot express this either.

  • Mathematical problems often require the use of more sophisticated data structures and algorithms. Expressing complicated solutions is vastly easier with F# compared to C#.

If you would like a case study, I converted an implementation of QR decomposition over System.Double from 2kLOC of C#. The F# was only 100 lines of code, runs over 10× faster and is generalized over the type of number so it works not only on float32, float and System.Numerics.Complex but can even be applied to symbolic matrices to obtain symbolic results!

FWIW, I write books on this subject as well as commercial software.

Jon Harrop