Matrix.linspace constructor

Matrix.linspace(
  1. int start,
  2. int end, [
  3. int number = 50
])

Creates a row Matrix with equally spaced values between the start and end values (inclusive).

start: Start value. end: End value. number: Number of equally spaced points. Default is 50.

Example:

var m = Matrix.linespace(0, 10, 3);
print(m);
// Output:
// Matrix: 1x3
// [ 0 5 10 ]

Implementation

factory Matrix.linspace(int start, int end, [int number = 50]) {
  if (start.runtimeType != end.runtimeType) {
    throw Exception('Start and end must be of the same type');
  }

  if (number <= 0) {
    throw ArgumentError('Number must be a positive integer');
  }

  List<List<dynamic>> data = [];

  double step = (end - start) / (number - 1);
  List<dynamic> row = [];
  for (int i = 0; i < number; i++) {
    row.add(start + i * step);
  }
  data.add(row);

  return Matrix(data);
}