Matrix.diagonal constructor

Matrix.diagonal(
  1. List<double> source, {
  2. DType dtype = DType.float32,
})

Creates a matrix, where elements from source are the elements for the matrix main diagonal, the rest of the elements are zero

import 'package:ml_linalg/matrix.dart';

void main() {
  final matrix = Matrix.diagonal([1, 2, 3, 4, 5]);

  print(matrix);
}

The output:

Matrix 5 x 5:
(1.0, 0.0, 0.0, 0.0, 0.0)
(0.0, 2.0, 0.0, 0.0, 0.0)
(0.0, 0.0, 3.0, 0.0, 0.0)
(0.0, 0.0, 0.0, 4.0, 0.0)
(0.0, 0.0, 0.0, 0.0, 5.0)

Implementation

factory Matrix.diagonal(
  List<double> source, {
  DType dtype = DType.float32,
}) {
  switch (dtype) {
    case DType.float32:
      return Float32Matrix.diagonal(source);

    case DType.float64:
      return Float64Matrix.diagonal(source);

    default:
      throw UnimplementedMatrixException(dtype);
  }
}