1

Usually (so far always) I try to use immutable collection in Scala, especially so that if I give a reference to a collection to some other part of my program I cannot override the original source by accident. Using breeze, I would like to know: Why was it decided to have DenseVector be a mutable collection?

Is this just a (maybe unwanted) side effect of using in Arrays in the background? If so, why were Arrays used, instead of another (immutable) collection?

Make42
  • 12,236
  • 24
  • 79
  • 155

2 Answers2

3
  1. Performance.

    Breeze uses netlib-java for its core linear algebra routines. This includes all the cubic time operations, matrix-matrix and matrix-vector multiplication. Special efforts are taken to ensure that arrays are not copied.

    A DenseVector backed by anything other than an array would be substantially slower. It could wrap an ImmutableArray which wraps an Array, but this would force some operations which can avoid copies by being in-place to copy, maybe interact weirdly with specialization, etc.

  2. I don't know how important this is (I suspect not much), but for people coming to Breeze from numerical computing (instead of from Scala) mutability is expected. E.g. it makes it simpler to port an algorithm which is implemented in Matlab or in R to Breeze.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • 2. I only seems half right: Execute `a=c(1,2); b=a; a[1]=0` in R, `a` will be `0 2 3` while b will be `1 2 3`. The equivalent in Scala would be an immutable `var`. – Make42 Nov 08 '16 at 10:58
  • Although, if DenseVector would be immutable I would have to write `a = a(1) := 0` – Make42 Nov 08 '16 at 11:04
  • Yes, Breeze behaves like Numpy in this respect, rather than R or Matlab (IIRC), which can potentially end up confusing R/Matlab users as well. Again, I suspect this is a weak reason (if at all). – Alexey Romanov Nov 08 '16 at 11:18
1

Performance. While functional programming provides excellent abstractions and tight code, it often doesn't offer the fastest execution. Java arrays offer far less overhead than Scala collections, and so are the go-to for highly repetitive numeric operations. Breeze DenseVectors are the same way, and are backed by java arrays under the hood.

Tim
  • 3,675
  • 12
  • 25