Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is F# really better than C# for math?

Tags:

c#

math

f#

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

like image 597
Dmitri Nesteruk Avatar asked Dec 18 '08 23:12

Dmitri Nesteruk


People also ask

Is the F 5 a V8?

The vehicle features a 5.0 L direct-injected V8 producing 416 SAE hp (423 PS, 311 kW) at 6,600 rpm, while peak torque is 371 ft⋅lbf (503 N⋅m) at 5,200 rpm. The engine also features a two-stage intake system, engine oil and automatic transmission fluid coolers and an oil pump designed for high-speed cornering.

Is there a Lexus IS F?

A NEW BREED OF F SPORT Destined to get the world's attention, the 2022 IS 500 F SPORT Performance marks the debut of the Lexus F SPORT Performance line.


2 Answers

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.

like image 62
Tomas Petricek Avatar answered Sep 18 '22 17:09

Tomas Petricek


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.

like image 32
J D Avatar answered Sep 21 '22 17:09

J D