Skip to content

Commit 396fa7f

Browse files
authored
Explicit layout and fix dense tensor printing (#354)
* Push through explicit "F" order through a variety of places we had hard coded "F". * Update tensor printing to match MATLAB * Fix some rebase errors * Support updated typing in numpy 2.0
1 parent 5e1fddf commit 396fa7f

16 files changed

+392
-306
lines changed

pyttb/cp_als.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def cp_als( # noqa: PLR0912,PLR0913,PLR0915
9292
Iter 1: f = ... f-delta = ...
9393
Final f = ...
9494
>>> print(M) # doctest: +ELLIPSIS
95-
ktensor of shape (2, 2)
95+
ktensor of shape (2, 2) with order F
9696
weights=[108.4715... 8.6114...]
9797
factor_matrices[0] =
9898
[[0.4187... 0.3989...]
@@ -101,7 +101,7 @@ def cp_als( # noqa: PLR0912,PLR0913,PLR0915
101101
[[0.6188... 0.2581...]
102102
[0.7854... 0.9661...]]
103103
>>> print(Minit) # doctest: +ELLIPSIS
104-
ktensor of shape (2, 2)
104+
ktensor of shape (2, 2) with order F
105105
weights=[1. 1.]
106106
factor_matrices[0] =
107107
[[4.1702...e-01 7.2032...e-01]
@@ -185,7 +185,7 @@ def cp_als( # noqa: PLR0912,PLR0913,PLR0915
185185

186186
# Reduce dimorder to only those modes we will optimize
187187
dimorder_in = dimorder # save for output
188-
dimorder = [d for d in dimorder if d in optdims]
188+
dimorder = [int(d) for d in dimorder if d in optdims]
189189

190190
# Store the last MTTKRP result to accelerate fitness computation
191191
U_mttkrp = np.zeros((input_tensor.shape[dimorder[-1]], rank))

pyttb/cp_apr.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -917,8 +917,8 @@ def tt_cp_apr_pqnr( # noqa: PLR0912,PLR0913,PLR0915
917917
delg = np.zeros((rank, lbfgsMem))
918918
rho = np.zeros((lbfgsMem,))
919919
lbfgsPos = 0
920-
m_rowOLD = np.empty(())
921-
gradOLD = np.empty(())
920+
m_rowOLD = np.empty((), dtype=m_row.dtype)
921+
gradOLD = np.empty((), dtype=m_row.dtype)
922922

923923
# Iteratively solve the row subproblem with projected quasi-Newton steps
924924
for i in range(maxinneriters):
@@ -983,8 +983,8 @@ def tt_cp_apr_pqnr( # noqa: PLR0912,PLR0913,PLR0915
983983
isRowNOTconverged[jj] = 1
984984

985985
# Update the L-BFGS approximation.
986-
tmp_delm = m_row - m_rowOLD
987-
tmp_delg = gradM - gradOLD
986+
tmp_delm: np.ndarray = m_row - m_rowOLD
987+
tmp_delg: np.ndarray = gradM - gradOLD
988988
tmp_delm_dot = tmp_delm.dot(tmp_delg.transpose())
989989
if not np.any(tmp_delm_dot == 0):
990990
tmp_rho = 1 / tmp_delm_dot

pyttb/gcp/fg_est.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def estimate_helper(
162162
ndim = subs.shape[1]
163163

164164
# Create exploded U's from the model factor matrices
165-
Uexp = [np.empty(())] * ndim
165+
Uexp = [np.empty((), dtype=factors[0].dtype)] * ndim
166166
for k in range(ndim):
167167
Uexp[k] = factors[k][subs[:, k], :]
168168

pyttb/gcp/samplers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def nonzeros(
272272

273273
# Select nonzeros
274274
if samples == nnz:
275-
nidx = np.arange(0, nnz)
275+
nidx: np.ndarray = np.arange(0, nnz, dtype=int)
276276
elif with_replacement or samples < nnz:
277277
nidx = np.random.choice(nnz, size=samples, replace=with_replacement)
278278
else:

pyttb/hosvd.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def hosvd( # noqa: PLR0912,PLR0913,PLR0915
129129

130130
# Shrink!
131131
if sequential:
132-
Y = Y.ttm(factor_matrices[k].transpose(), k)
132+
Y = Y.ttm(factor_matrices[k].transpose(), int(k))
133133
# Extract final core
134134
if sequential:
135135
G = Y

0 commit comments

Comments
 (0)