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

Add test that matches TTB for MATLAB output of HOSVD #79

Merged
merged 1 commit into from
Mar 19, 2023
Merged
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
54 changes: 54 additions & 0 deletions tests/test_hosvd.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ def sample_tensor():
return params, tensorInstance


@pytest.fixture()
def sample_tensor_3way():
shape = (3, 3, 3)
data = np.array(range(1, 28)).reshape(shape, order="F")
params = {"data": data, "shape": shape}
tensorInstance = ttb.tensor().from_data(data, shape)
return params, tensorInstance


@pytest.mark.indevelopment
def test_hosvd_simple_convergence(capsys, sample_tensor):
(data, T) = sample_tensor
Expand Down Expand Up @@ -65,3 +74,48 @@ def test_hosvd_incorrect_dimorder(capsys, sample_tensor):
dimorder = 1
with pytest.raises(ValueError):
_ = ttb.hosvd(T, 1, dimorder=dimorder)


@pytest.mark.indevelopment
def test_hosvd_3way(capsys, sample_tensor_3way):
(data, T) = sample_tensor_3way
M = ttb.hosvd(T, 1e-4, verbosity=0)
capsys.readouterr()
print(f"M=\n{M}")
core = np.array(
[
[
[-8.301598119750199e01, -5.005881796972034e-03],
[-1.268039597172832e-02, 5.842630378620833e00],
],
[
[3.709974006281391e-02, -1.915213813096568e00],
[-5.157111619887230e-01, 5.243776123493664e-01],
],
]
)
fm0 = np.array(
[
[-5.452132631706279e-01, -7.321719955012304e-01],
[-5.767748638548937e-01, -2.576993904719336e-02],
[-6.083364645391598e-01, 6.806321174064961e-01],
]
)
fm1 = np.array(
[
[-4.756392343758577e-01, 7.791666394653051e-01],
[-5.719678320081717e-01, 7.865197061237804e-02],
[-6.682964296404851e-01, -6.218626982406427e-01],
]
)
fm2 = np.array(
[
[-1.922305666539489e-01, 8.924016710972924e-01],
[-5.140779746206554e-01, 2.627873081852611e-01],
[-8.359253825873615e-01, -3.668270547267537e-01],
]
)
assert np.allclose(M.core.data, core)
assert np.allclose(M.u[0], fm0)
assert np.allclose(M.u[1], fm1)
assert np.allclose(M.u[2], fm2)