Is F# really better than C# for math?

2019-01-14 05:46发布

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

标签: c# math f#
7条回答
在下西门庆
2楼-- · 2019-01-14 06:22

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

查看更多
Bombasti
3楼-- · 2019-01-14 06:22

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

Also: 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.

查看更多
贪生不怕死
4楼-- · 2019-01-14 06:28

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.

查看更多
Luminary・发光体
5楼-- · 2019-01-14 06:29

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.

查看更多
一夜七次
6楼-- · 2019-01-14 06:37

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.

查看更多
Anthone
7楼-- · 2019-01-14 06:39

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.

查看更多
登录 后发表回答