0

I am using the Neo library for linear algebra in Nim, and I would like to extract arbitrary rows from a matrix.

I can explicitly select a continuous sequence of rows as per the examples in the README, but can't select a disjoint subset of rows.

import neo

let x = randomMatrix(10, 4)
let some_rows = @[1,3,5]

echo x[2..4, All]  # works fine
echo x[some_rows, All] ## error
Leo
  • 25
  • 1
  • 4

1 Answers1

0

The first echo works because you are creating a Slice object, which neo has defined a proc for. The second echo uses a sequence of integers, and that kind of access is not defined in the neo library. Unfortunately Slices define contiguous closed ranges, you can't even specify steps to iterate in bigger increments than one, so there is no way to accomplish what you want.

Looking at the structure of a Matrix, it seems that it is highly optimised to avoid copying data. Matrix transformation operations seem to reuse the data of the previous matrix and change the access/dimensions. As such, a matrix transformation with arbitrary random would not be possible, the indexes in your example specifically access non contiguos data and this would need to be encoded somehow in the new structure. Plus if you wrote @[1,5,3] that would defeat any kind of normal iterative looping.

An alternative of course is to write a proc which accepts a sequence instead of a slice and then builds a new matrix copying data from the old one. This implies a performance penalty, but if you think this is a good addition to the library please request it in the issue tracker of the project. If it is not accepted, then you will need to write yourself such a proc for personal use in your programs.

Grzegorz Adam Hankiewicz
  • 7,349
  • 1
  • 36
  • 78