Fsharpplus: What nat-dependent Vector & Matrix operations should be implemented?

Created on 25 Sep 2019  路  3Comments  路  Source: fsprojects/FSharpPlus

Related: #178, #192

I'm currently trying to add type-safe vector & matrix types to F#+ but I'm not sure what to implement.

Here are the ones I can think of:

  • Basic collection operations (such as map and filter) in a type-safe manner (e.g. item indexed with type-level natural).
  • Addition and subtraction
  • Scalar multiplication
  • dot product and 3D cross product for vectors
  • matrix product for matrices
  • constants such as zero, one, unit vectors, identity matrix

but I can imagine many other necessary operations are missing.

Can anyone provide me a list of other operations which can take advantage of type-level naturals and/or should be implemented?

RFC help wanted

Most helpful comment

@nasosev After some experiments, I decided not to merge matrix and vector into one type: the conversion between 1D array and 2D array is expensive and the implementation of pretty printer and other type classes becomes much complicated (e.g. vectors are foldable but matrices are not).

All 3 comments

It might be possible to make them applicatives but we'll need a way to express an infinite vector for the pure operation.

@cannorin, in addition to what you mentioned, I think these operations could be valuable:

Transpose: Mat<n,m> -> Mat<m,n>
Tensor (Kronecker) product: Mat<n1,m1> -> Mat<n2,m2> -> Mat<n1 * n2, m1 * m2>
Direct sum:   Mat<n1,m1> -> Mat<n2,m2> -> Mat<n1 + n2, m1 + m2>
Vertical block sum: Mat<n1,m> -> Mat<n2,m> -> Mat<n1 + n2, m>
Horizontal block sum: Mat<n,m1> -> Mat<n,m2> -> Mat<n, m1 + m2>
Hadamard (element-wise) product: Mat<n,m> -> Mat<n,m> -> Mat<n, m>

I prefer to think of vectors as either (n,1) matrices or (1,n) matrices (covectors); this simplifies things as the vector operations are then special cases of the matrix ones. I think MATLAB adopts this philosophy but I鈥檓 not certain.

@nasosev After some experiments, I decided not to merge matrix and vector into one type: the conversion between 1D array and 2D array is expensive and the implementation of pretty printer and other type classes becomes much complicated (e.g. vectors are foldable but matrices are not).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aammfe picture aammfe  路  10Comments

abelbraaksma picture abelbraaksma  路  3Comments

cannorin picture cannorin  路  5Comments

cmeeren picture cmeeren  路  9Comments

ShalokShalom picture ShalokShalom  路  4Comments