buildMatrix method

ComplexMatrix buildMatrix()

Builds the Sylvester matrix associated to the given polynomial.

Implementation

ComplexMatrix buildMatrix() {
  // Computing the derivative of the polynomial and the size of the matrix
  final coefficients = polynomial.coefficients;
  final derivative = polynomial.derivative().coefficients;
  final size = (coefficients.length - 1) + (derivative.length - 1);

  // Building the matrix with FIXED length lists (optimization)
  final flatData = List<Complex>.generate(
    size * size,
    (_) => const Complex.zero(),
    growable: false,
  );

  /* Iterating over the coefficients and placing them in the matrix. Since the
   * 2D array is "flattened", the way we have to access "cells" is this...
   *
   *  > flatData[arraySize * row + column] = value
   *
   * which is equivalent to 'flatData[row][column]' if 'flatData' was a 2D
   * array.
   */
  for (var i = 0; i < size - coefficients.length + 1; ++i) {
    for (var j = 0; j < coefficients.length; ++j) {
      flatData[size * i + (j + i)] = coefficients[j];
    }
  }

  var pos = 0;
  for (var i = size - coefficients.length + 1; i < size; ++i) {
    for (var j = 0; j < derivative.length; ++j) {
      flatData[size * i + (j + pos)] = derivative[j];
    }
    ++pos;
  }

  // Returning the Sylvester matrix.
  return ComplexMatrix.fromFlattenedData(
    rows: size,
    columns: size,
    data: flatData,
  );
}