3

F# is often promoted as a functional language where data is immutable by default, however the elements of the matrix and vector types in the F# Powerpack are mutable. Why is this?

Furthermore, for which reason are sparse matrices implemented as immutable as opposed to normal matrices?

Tuur
  • 115
  • 1
  • 3

2 Answers2

4

The standard array type ('T[]) in F# is also mutable. You're mostly correct -- F# is a functional language where data immutability is encouraged, but not required. Basically, F# allows you to do write both mutable/imperative code and immutable/functional code; it's up to you to decide the best way to implement the code for your specific application.

Another reason for having mutable arrays and matrices is performance -- it is possible to implement very fast algorithms with immutable types, but users writing scientific computations usually only care about one thing: achieving maximum performance. That being that case, it follows that the arrays and matrices should be mutable.

Jack P.
  • 11,487
  • 1
  • 29
  • 34
3

For truly high performance, mutability is required, in one specific case : Provided that your code is perfectly optimized and that you master everything it is doing down to the cache (L1, L2) pattern of access of your program, then nothing beats a low level, to the metal approach.

This happens mostly only when you have one well specified problem that stays constant for 20 years, aka mostly in scientific tasks.

As soon as you depart from this specific case, in 99.99% the bottlenecks arise from having a too low level representation (induced by a low level langage) in which you can't express the final, real-world optimization trade-off of your problem at hand.

Bottom line, for performance, the following approach is the only way (i think) :

  • High level / algorithmic optimization first
  • Once every high level ways has been explored, low level optimization

You can see how as a consequence of that :

  • You should never optimize anything without FIRST measuring the impact : improvements should only be made if they yield enormous performance gains and/or do not degrade your domain logic.

  • You eventually will reach, if your problem is stable and well defined, the point where you will have no choice but to go to the low level, and play with memory/mutability

nicolas
  • 9,549
  • 3
  • 39
  • 83
  • I will argue against that, The compiler of any ML language should be capable of transforming a map/fold/reduce composition to a fully optimized form under the assumption of immutability, then the compiler will be able to determine where data dependencies arise. The only reasons F# matrices isn't immutable, are that this will require, that the compiler itself have the same optimization implemented for GPU instructions sets, which it has not. This leads to the need for someone making a optimized library. Since around 90% of all programmers prefer OO or imperative, hence mutable. – kam Feb 05 '20 at 09:06