Array2d.fromVector constructor

Array2d.fromVector(
  1. Array vals,
  2. int mRow
)

Construct a matrix from a one-dimensional packed array

  • vals One-dimensional array of doubles, packed by columns (ala Fortran).
  • mRow Number of rows.
  • FormatException: Array length must be a multiple of m.

Examples

var a =  Array2d.fromVector(Array([1, 2, 3, 4]), 2);

print(a);

/* output:
var aExpec = Array2d([
  Array([1, 3]),
  Array([2, 4])
]);
*/

Implementation

Array2d.fromVector(Array vals, int mRow) {
  var nCol = (mRow != 0 ? vals.length / mRow : 0).toInt();

  if (mRow * nCol != vals.length) {
    throw FormatException('Array length must be a multiple of mRow.');
  }

  l = List<Array>.filled(mRow, Array.empty());
  for (var i = 0; i < mRow; i++) {
    l[i] = Array.fixed(nCol);
  }

  for (var i = 0; i < mRow; i++) {
    for (var j = 0; j < nCol; j++) {
      this[i][j] = vals[i + j * mRow];
    }
  }
}