Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVD unittests #674

Open
wants to merge 1 commit into
base: svd
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions tests/2d/scipy/svd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from ulab import scipy as sp
from ulab import numpy as np

tol = 1e-8

A = np.zeros((3, 3))
U, S, Vh = sp.linalg.svd(A)
# print(np.dot(U * S, Vh))
B = np.dot(np.dot(U, S), Vh)
print("Zero Matrix S:", np.max(abs(S)) < tol)
print("Zero Matrix Compare:", np.max(abs(A - B)) < tol)
print()

A = np.eye(4)
U, S, Vh = sp.linalg.svd(A)
# print(np.dot(U * S, Vh))
print("Identity Matrix S:", np.max(abs(A - S)) < tol)
B = np.dot(np.dot(U, S), Vh)
print("Identity Matrix Compare:", np.max(abs(A - B)) < tol)
print()

A = np.diag([1., 2., 3., 4.])
S_exp = np.diag([4., 3., 2., 1.])
U, S, Vh = sp.linalg.svd(A)
# B = np.dot(U * S, Vh)
B = np.dot(np.dot(U, S), Vh)
print("Diagonal Matrix S:", np.max(abs(A - S)) < tol)
print("Diagonal Matrix Compare", np.max(abs(A - B)) < tol)
print()

A = np.array([
[0., 2., 0., 0.],
[-1., 0., 0., 0.],
[0., 0., -3., 0.],
[0., 0., 0., -4.],
])
U, S, Vh = sp.linalg.svd(A)
# B = np.dot(U * S, Vh)
B = np.dot(np.dot(U, S), Vh)
print("Simultaneous 4D Rotation Compare:", np.max(abs(A - B)) < tol)
print()

A = np.array([
[0., -1.],
[2., 0.],
[0., 0.]
])
U, S, Vh = sp.linalg.svd(A)
# B = np.dot(U * S, Vh)
B = np.dot(np.dot(U, S), Vh)
print("3x2 Compare:", np.max(abs(A - B)) < tol)
print()

A = np.array([
[0., -1., 0.],
[2., 0., 0.],
])
U, S, Vh = sp.linalg.svd(A)
# print(np.dot(U * S, Vh))
B = np.dot(np.dot(U, S), Vh)
print("2x3 Compare:", np.max(abs(A - B)) < tol)
print()

A = np.array([
[4., 0.],
[3., -5.],
])
U, S, Vh = sp.linalg.svd(A)
# B = np.dot(U * S, Vh)
B = np.dot(np.dot(U, S), Vh)
print("2x2 Compare", np.max(abs(A - B)) < tol)
print()

rng = np.random.Generator(123456)
A = rng.random((10, 10))
U, S, Vh = sp.linalg.svd(A)
# print(np.dot(U * S, Vh))
B = np.dot(np.dot(U, S), Vh)
print("10x10 Random Compare:", np.max(abs(A - B)) < tol)
print()

A = rng.random((10, 15))
U, S, Vh = sp.linalg.svd(A)
# print(np.dot(U * S, Vh))
B = np.dot(np.dot(U, S), Vh)
print("10x15 Random Compare:", np.max(abs(A - B)) < tol)
print()


A = rng.random((15, 10))
U, S, Vh = sp.linalg.svd(A)
# print(np.dot(U * S, Vh))
B = np.dot(np.dot(U, S), Vh)
print("15x10 Random Compare:", np.max(abs(A - B)) < tol)
print()
21 changes: 21 additions & 0 deletions tests/2d/scipy/svd.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Zero Matrix S: True
Zero Matrix Compare: True

Identity Matrix S: True
Identity Matrix Compare: True

Diagonal Matrix S: False
Diagonal Matrix Compare False

Simultaneous 4D Rotation Compare: False

3x2 Compare: False

2x3 Compare: False

2x2 Compare False

10x15 Random Compare: False

15x10 Random Compare: False

Loading