Mojo-Numerics-and-Algorithms-group / NuMojo

NuMojo is a library for numerical computing in Mojo 🔥 similar to numpy in Python.
Apache License 2.0
112 stars 15 forks source link

[Add] Inverse of matrix using LU decomposition algorithm #101

Closed forFudan closed 2 months ago

forFudan commented 2 months ago

Reverse of matrix

This PR implements several functions of linear algebra so that we can calculate the inverse of a matrix, using LU decomposition algorithm. This also allows we solve the Ax=y.

The outcome is successfully tested against numpy.linalg.inv on 1000x1000 random matrix.

An example is at the end of this page.

Changelog of unreleased

Just as the Mojo repo does, I add changelog of unreleased changes in the docs folder. This allows users (and us) to track what have been added, removed, or fixed.

When we make a release, the items in this file will be edited and moved to changelog.md.

Example

import numojo as nm
fn main() raises:
    var A = nm.NDArray("[[1,0,1], [0,2,1], [1,1,1]]")
    var B = nm.math.linalg.solve.inverse(A)
    print("Original matrix:")
    print(A)
    print("Reversed matrix:")
    print(B)
    print("Verify whether AB = I:")
    print(A @ B)
Original matrix:
[[      1.0     0.0     1.0     ]
 [      0.0     2.0     1.0     ]
 [      1.0     1.0     1.0     ]]
2-D array  Shape: [3, 3]  DType: float64
Reversed matrix:
[[      -1.0    -1.0    2.0     ]
 [      -1.0    0.0     1.0     ]
 [      2.0     1.0     -2.0    ]]
2-D array  Shape: [3, 3]  DType: float64
Verify whether AB = I:
[[      1.0     0.0     0.0     ]
 [      0.0     1.0     0.0     ]
 [      0.0     0.0     1.0     ]]
2-D array  Shape: [3, 3]  DType: float64

@shivasankarka , to be aligned with Mojo repo, I changed the folder name doc to docs.