renggli / dart-data

A fast and space efficient library to deal with data in Dart, Flutter and the web.
MIT License
20 stars 4 forks source link

How to find eigenvalues? #2

Closed Rubenxx00 closed 4 years ago

Rubenxx00 commented 4 years ago

Hi.

I'm trying to get eigenvalues for a matrix. I do:

var m = Matrix.builder.withType(DataType.float64).fromRows([
    [1, 0, 0, -1],
    [0, -1, 0, 0],
    [0, 0, 1, -1],
    [-1, 0, -1, 0]
  ]);
var e = eigenvalue(m);

I inspect the object but I can't find the result (-1, 1, 2).

Also, if I write DataType.numeric I get an exception... why?

Thanks for helping

renggli commented 4 years ago

It works for me:

library data.example;

import 'package:data/matrix.dart';
import 'package:data/type.dart';

void main() {
  final m = Matrix.builder.withType(DataType.float64).fromRows([
    [1, 0, 0, -1],
    [0, -1, 0, 0],
    [0, 0, 1, -1],
    [-1, 0, -1, 0]
  ]);
  final e = eigenvalue(m);
  print('eigenvector matrix: ${e.V}');
  print('block diagonal eigenvalue matrix: ${e.D}');
  print('real parts of the eigenvalues: ${e.realEigenvalues}');
  print('imaginary parts of the eigenvalues: ${e.imagEigenvalues}');
  print('complex eigenvalues: ${e.eigenvalues}');
}

Prints:

eigenvector matrix: RowMajorMatrix<double>[4, 4, float64]:
4.082483e-001 0.000000e+000 7.071068e-001 5.773503e-001
0.000000e+000 -1.000000e+000 0.000000e+000 -0.000000e+000
4.082483e-001 0.000000e+000 -7.071068e-001 5.773503e-001
8.164966e-001 0.000000e+000 1.732681e-016 -5.773503e-001
block diagonal eigenvalue matrix: DiagonalMatrix<double>[4, 4, float64]:
-1.000000e+000 0.000000e+000 0.000000e+000 0.000000e+000
0.000000e+000 -1.000000e+000 0.000000e+000 0.000000e+000
0.000000e+000 0.000000e+000 10.000000e-001 0.000000e+000
0.000000e+000 0.000000e+000 0.000000e+000 2.000000e+000
real parts of the eigenvalues: [-1.0000000000000002, -1.0, 0.9999999999999999, 1.9999999999999998]
imaginary parts of the eigenvalues: [0.0, 0.0, 0.0, 0.0]
complex eigenvalues: [Complex(-1.0000000000000002, 0.0), Complex(-1.0, 0.0), Complex(0.9999999999999999, 0.0), Complex(1.9999999999999998, 0.0)]