Matrix.identity constructor

Matrix.identity(
  1. int size, {
  2. DType dtype = DType.float32,
})

Creates a matrix of size * size dimension, where all the main diagonal elements are equal to 1, the rest of the elements are 0

import 'package:ml_linalg/matrix.dart';

void main() {
  final matrix = Matrix.identity(5);

  print(matrix);
}

The output:

Matrix 5 x 5:
(1.0, 0.0, 0.0, 0.0, 0.0)
(0.0, 1.0, 0.0, 0.0, 0.0)
(0.0, 0.0, 1.0, 0.0, 0.0)
(0.0, 0.0, 0.0, 1.0, 0.0)
(0.0, 0.0, 0.0, 0.0, 1.0)

Implementation

factory Matrix.identity(
  int size, {
  DType dtype = DType.float32,
}) {
  switch (dtype) {
    case DType.float32:
      return Float32Matrix.scalar(1.0, size);

    case DType.float64:
      return Float64Matrix.scalar(1.0, size);

    default:
      throw UnimplementedMatrixException(dtype);
  }
}