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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nick/tenmat docs #294

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
TENMAT: Remove tt_from_dense_matrix since tenmat exists.
  • Loading branch information
ntjohnson1 committed Dec 16, 2023
commit d2577d41767ef96d512a751eb0c26b7f8de72d58
35 changes: 0 additions & 35 deletions pyttb/pyttb_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,6 @@
import pyttb as ttb


def tt_from_dense_matrix(
matrix: np.ndarray,
shape: Tuple[int, ...],
mode: int,
idx: int,
) -> ttb.tensor:
"""
Helper function to wrap dense matrix into tensor.
Inverse of :class:`pyttb.tt_to_dense_matrix`

Parameters
----------
matrix:
Matrix to (re-)create tensor from.
mode:
Mode around which tensor was unwrapped
idx:
In {0,1}, idx of mode in matrix, s.b. 0 for tranpose=True

Returns
-------
Dense tensor.
"""
tensorInstance = ttb.tensor(matrix)
if idx == 0:
tensorInstance = tensorInstance.permute(np.array([1, 0]))
tensorInstance = tensorInstance.reshape(shape)
tensorInstance = tensorInstance.permute(
np.concatenate(
(np.arange(1, mode + 1), np.array([0]), np.arange(mode + 1, len(shape)))
)
)
return tensorInstance


def tt_union_rows(MatrixA: np.ndarray, MatrixB: np.ndarray) -> np.ndarray:
"""
Helper function to reproduce functionality of MATLABS intersect(a,b,'rows')
Expand Down
15 changes: 0 additions & 15 deletions tests/test_pyttb_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,6 @@
import pyttb.pyttb_utils as ttb_utils


def test_sptensor_from_dense_matrix():
tensorInstance = ttb.tensor(np.random.normal(size=(4, 4, 4)))
for mode in range(tensorInstance.ndims):
tensorCopy = tensorInstance.copy()
Xnt = tensorCopy.to_tenmat(np.array([mode]), copy=False).data.transpose()
Ynt = ttb_utils.tt_from_dense_matrix(Xnt, tensorCopy.shape, mode, 0)
assert tensorCopy.isequal(Ynt)

for mode in range(tensorInstance.ndims):
tensorCopy = tensorInstance.copy()
Xnt = tensorCopy.to_tenmat(np.array([mode]), copy=False).data
Ynt = ttb_utils.tt_from_dense_matrix(Xnt, tensorCopy.shape, mode, 1)
assert tensorCopy.isequal(Ynt)


def test_tt_union_rows():
a = np.array([[4, 6], [1, 9], [2, 6], [2, 6], [99, 0]])
b = np.array([[1, 7], [1, 8], [2, 6]])
Expand Down