|
6 | 6 | from __future__ import annotations
|
7 | 7 |
|
8 | 8 | import pyttb as ttb
|
9 |
| -from .pyttb_utils import * |
| 9 | +from pyttb.pyttb_utils import * |
10 | 10 | import numpy as np
|
11 | 11 | import warnings
|
12 | 12 | import scipy.sparse as sparse
|
@@ -267,21 +267,21 @@ def from_function(cls, fun, shape, num_components):
|
267 | 267 |
|
268 | 268 | >>> np.random.seed(1)
|
269 | 269 | >>> K_random = ttb.ktensor.from_function(np.random.random_sample, (2, 3, 4), 2)
|
270 |
| - >>> print(K_random) |
| 270 | + >>> print(K_random) # doctest: +ELLIPSIS |
271 | 271 | ktensor of shape 2 x 3 x 4
|
272 | 272 | weights=[1. 1.]
|
273 | 273 | factor_matrices[0] =
|
274 |
| - [[4.17022005e-01 7.20324493e-01] |
275 |
| - [1.14374817e-04 3.02332573e-01]] |
| 274 | + [[4.1702...e-01 7.2032...e-01] |
| 275 | + [1.1437...e-04 3.0233...e-01]] |
276 | 276 | factor_matrices[1] =
|
277 |
| - [[0.14675589 0.09233859] |
278 |
| - [0.18626021 0.34556073] |
279 |
| - [0.39676747 0.53881673]] |
| 277 | + [[0.1467... 0.0923...] |
| 278 | + [0.1862... 0.3455...] |
| 279 | + [0.3967... 0.5388...]] |
280 | 280 | factor_matrices[2] =
|
281 |
| - [[0.41919451 0.6852195 ] |
282 |
| - [0.20445225 0.87811744] |
283 |
| - [0.02738759 0.67046751] |
284 |
| - [0.4173048 0.55868983]] |
| 281 | + [[0.4191... 0.6852...] |
| 282 | + [0.2044... 0.8781...] |
| 283 | + [0.0273... 0.6704...] |
| 284 | + [0.4173... 0.5586...]] |
285 | 285 |
|
286 | 286 | Create a `ktensor` with entries equal to 1:
|
287 | 287 |
|
@@ -529,41 +529,41 @@ def copy(self):
|
529 | 529 |
|
530 | 530 | >>> np.random.seed(1)
|
531 | 531 | >>> K = ttb.ktensor.from_function(np.random.random_sample, (2, 3, 4), 2)
|
532 |
| - >>> print(K) |
| 532 | + >>> print(K) # doctest: +ELLIPSIS |
533 | 533 | ktensor of shape 2 x 3 x 4
|
534 | 534 | weights=[1. 1.]
|
535 | 535 | factor_matrices[0] =
|
536 |
| - [[4.17022005e-01 7.20324493e-01] |
537 |
| - [1.14374817e-04 3.02332573e-01]] |
| 536 | + [[4.1702...e-01 7.2032...e-01] |
| 537 | + [1.1437...e-04 3.0233...e-01]] |
538 | 538 | factor_matrices[1] =
|
539 |
| - [[0.14675589 0.09233859] |
540 |
| - [0.18626021 0.34556073] |
541 |
| - [0.39676747 0.53881673]] |
| 539 | + [[0.1467... 0.0923...] |
| 540 | + [0.1862... 0.3455...] |
| 541 | + [0.3967... 0.5388...]] |
542 | 542 | factor_matrices[2] =
|
543 |
| - [[0.41919451 0.6852195 ] |
544 |
| - [0.20445225 0.87811744] |
545 |
| - [0.02738759 0.67046751] |
546 |
| - [0.4173048 0.55868983]] |
| 543 | + [[0.4191... 0.6852...] |
| 544 | + [0.2044... 0.8781...] |
| 545 | + [0.0273... 0.6704...] |
| 546 | + [0.4173... 0.5586...]] |
547 | 547 |
|
548 | 548 | Create a copy of the `ktensor` and change the weights:
|
549 | 549 |
|
550 | 550 | >>> K1 = K.copy()
|
551 | 551 | >>> K1.weights = np.array([2., 3.])
|
552 |
| - >>> print(K1) |
| 552 | + >>> print(K1) # doctest: +ELLIPSIS |
553 | 553 | ktensor of shape 2 x 3 x 4
|
554 | 554 | weights=[2. 3.]
|
555 | 555 | factor_matrices[0] =
|
556 |
| - [[4.17022005e-01 7.20324493e-01] |
557 |
| - [1.14374817e-04 3.02332573e-01]] |
| 556 | + [[4.1702...e-01 7.2032...e-01] |
| 557 | + [1.1437...e-04 3.023...e-01]] |
558 | 558 | factor_matrices[1] =
|
559 |
| - [[0.14675589 0.09233859] |
560 |
| - [0.18626021 0.34556073] |
561 |
| - [0.39676747 0.53881673]] |
| 559 | + [[0.1467... 0.0923...] |
| 560 | + [0.1862... 0.3455...] |
| 561 | + [0.3967... 0.5388...]] |
562 | 562 | factor_matrices[2] =
|
563 |
| - [[0.41919451 0.6852195 ] |
564 |
| - [0.20445225 0.87811744] |
565 |
| - [0.02738759 0.67046751] |
566 |
| - [0.4173048 0.55868983]] |
| 563 | + [[0.4191... 0.6852...] |
| 564 | + [0.2044... 0.8781...] |
| 565 | + [0.0273... 0.6704...] |
| 566 | + [0.4173... 0.5586...]] |
567 | 567 | """
|
568 | 568 | return ttb.ktensor.from_tensor_type(self)
|
569 | 569 |
|
@@ -743,15 +743,15 @@ def fixsigns(self, other=None):
|
743 | 743 | >>> K2.factor_matrices[0][1, 1] = - K2.factor_matrices[0][1, 1]
|
744 | 744 | >>> K2.factor_matrices[1][1, 1] = - K2.factor_matrices[1][1, 1]
|
745 | 745 | >>> K = K.fixsigns(K2)
|
746 |
| - >>> print(K) |
| 746 | + >>> print(K) # doctest: +ELLIPSIS |
747 | 747 | ktensor of shape 2 x 2
|
748 |
| - weights=[27.20294102 89.4427191 ] |
| 748 | + weights=[27.2029... 89.4427...] |
749 | 749 | factor_matrices[0] =
|
750 |
| - [[ 0.31622777 -0.4472136 ] |
751 |
| - [ 0.9486833 -0.89442719]] |
| 750 | + [[ 0.3162... -0.4472...] |
| 751 | + [ 0.9486... -0.8944...]] |
752 | 752 | factor_matrices[1] =
|
753 |
| - [[ 0.58123819 -0.6 ] |
754 |
| - [ 0.81373347 -0.8 ]] |
| 753 | + [[ 0.5812... -0.6...] |
| 754 | + [ 0.8137... -0.8...]] |
755 | 755 | """
|
756 | 756 | if other == None:
|
757 | 757 | for r in range(self.ncomponents):
|
@@ -842,6 +842,7 @@ def full(self):
|
842 | 842 | data[:, :] =
|
843 | 843 | [[29. 39.]
|
844 | 844 | [63. 85.]]
|
| 845 | + <BLANKLINE> |
845 | 846 | """
|
846 | 847 | data = self.weights @ ttb.khatrirao(self.factor_matrices, reverse=True).T
|
847 | 848 | return ttb.tensor.from_data(data, self.shape)
|
@@ -993,8 +994,8 @@ def mask(self, W):
|
993 | 994 |
|
994 | 995 | >>> W = ttb.tensor.from_data(np.array([[0, 1], [1, 0]]))
|
995 | 996 | >>> print(K.mask(W))
|
996 |
| - [[39.] |
997 |
| - [63.]] |
| 997 | + [[63.] |
| 998 | + [39.]] |
998 | 999 | """
|
999 | 1000 | # Error check
|
1000 | 1001 | if len(W.shape) != len(self.shape) or np.any(np.array(W.shape) > np.array(self.shape)):
|
@@ -1123,16 +1124,16 @@ def normalize(self, weight_factor=None, sort=False, normtype=2, mode=None):
|
1123 | 1124 | Example
|
1124 | 1125 | -------
|
1125 | 1126 | >>> K = ttb.ktensor.from_function(np.ones, (2, 3, 4), 2)
|
1126 |
| - >>> print(K.normalize()) |
| 1127 | + >>> print(K.normalize()) # doctest: +ELLIPSIS |
1127 | 1128 | ktensor of shape 2 x 3 x 4
|
1128 |
| - weights=[4.89897949 4.89897949] |
| 1129 | + weights=[4.898... 4.898...] |
1129 | 1130 | factor_matrices[0] =
|
1130 |
| - [[0.70710678 0.70710678] |
1131 |
| - [0.70710678 0.70710678]] |
| 1131 | + [[0.7071... 0.7071...] |
| 1132 | + [0.7071... 0.7071...]] |
1132 | 1133 | factor_matrices[1] =
|
1133 |
| - [[0.57735027 0.57735027] |
1134 |
| - [0.57735027 0.57735027] |
1135 |
| - [0.57735027 0.57735027]] |
| 1134 | + [[0.5773... 0.5773...] |
| 1135 | + [0.5773... 0.5773...] |
| 1136 | + [0.5773... 0.5773...]] |
1136 | 1137 | factor_matrices[2] =
|
1137 | 1138 | [[0.5 0.5]
|
1138 | 1139 | [0.5 0.5]
|
@@ -1215,16 +1216,16 @@ def nvecs(self, n, r, flipsign=True):
|
1215 | 1216 |
|
1216 | 1217 | >>> K = ttb.ktensor.from_function(np.ones, (2, 3, 4), 2)
|
1217 | 1218 | >>> nvecs1 = K.nvecs(0, 1)
|
1218 |
| - >>> print(nvecs1) |
1219 |
| - [[0.70710678] |
1220 |
| - [0.70710678]] |
| 1219 | + >>> print(nvecs1) # doctest: +ELLIPSIS |
| 1220 | + [[0.70710678...] |
| 1221 | + [0.70710678...]] |
1221 | 1222 |
|
1222 | 1223 | Compute first 2 leading eigenvectors for dimension 0:
|
1223 | 1224 |
|
1224 | 1225 | >>> nvecs2 = K.nvecs(0, 2)
|
1225 |
| - >>> print(nvecs2) |
1226 |
| - [[ 0.70710678 0.70710678] |
1227 |
| - [ 0.70710678 -0.70710678]] |
| 1226 | + >>> print(nvecs2) # doctest: +ELLIPSIS |
| 1227 | + [[ 0.70710678... 0.70710678...] |
| 1228 | + [ 0.70710678... -0.70710678...]] |
1228 | 1229 | """
|
1229 | 1230 | M = self.weights[:, None] @ self.weights[:, None].T
|
1230 | 1231 | for i in range(self.ndims):
|
@@ -1410,12 +1411,9 @@ def score(self, other, weight_penalty=True, threshold=0.99, greedy=True):
|
1410 | 1411 | -------
|
1411 | 1412 | Create two `ktensor` instances:
|
1412 | 1413 |
|
1413 |
| - >>> A = ttb.ktensor.from_data(np.array([2, 1, 3]), np.ones((3,3)), np.ones((4,3)), np.ones((5,3))) |
1414 |
| - >>> B = ttb.ktensor.from_data(np.array([2, 4]), np.ones((3,2)), np.ones((4,2)), np.ones((5,2))) |
1415 |
| -
|
1416 |
| - Compute `score` using `ktensor.weights`: |
1417 |
| -
|
1418 |
| - >>> score,Aperm,flag,perm = A.score(B) |
| 1414 | + >>> A = ttb.ktensor.from_data(np.array([2., 1., 3.]), np.ones((3,3)), np.ones((4,3)), np.ones((5,3))) |
| 1415 | + >>> B = ttb.ktensor.from_data(np.array([2., 4.]), np.ones((3,2)), np.ones((4,2)), np.ones((5,2))) |
| 1416 | + >>> score,Aperm,flag,perm = A.score(B) # Compute `score` using `ktensor.weights` |
1419 | 1417 | >>> print(score)
|
1420 | 1418 | 0.875
|
1421 | 1419 | >>> print(perm)
|
@@ -1531,15 +1529,15 @@ def symmetrize(self):
|
1531 | 1529 | Make the factor matrices of the `ktensor` symmetric:
|
1532 | 1530 |
|
1533 | 1531 | >>> K1 = K.symmetrize()
|
1534 |
| - >>> print(K1) |
| 1532 | + >>> print(K1) # doctest: +ELLIPSIS |
1535 | 1533 | ktensor of shape 2 x 2
|
1536 | 1534 | weights=[1. 1.]
|
1537 | 1535 | factor_matrices[0] =
|
1538 |
| - [[2.34043142 4.95196735] |
1539 |
| - [4.59606911 8.01245149]] |
| 1536 | + [[2.3404... 4.9519...] |
| 1537 | + [4.5960... 8.0124...]] |
1540 | 1538 | factor_matrices[1] =
|
1541 |
| - [[2.34043142 4.95196735] |
1542 |
| - [4.59606911 8.01245149]] |
| 1539 | + [[2.3404... 4.9519...] |
| 1540 | + [4.5960... 8.0124...]] |
1543 | 1541 | """
|
1544 | 1542 | # Check tensor dimensions for compatibility with symmetrization
|
1545 | 1543 | assert (self.shape == self.shape[0]*np.ones(self.ndims)).all(), "Tensor is not cubic -- cannot be symmetrized"
|
@@ -1607,20 +1605,20 @@ def tolist(self, mode=None):
|
1607 | 1605 | Spread weights equally to all factors and return list of factor matrices:
|
1608 | 1606 |
|
1609 | 1607 | >>> fm_list = K.tolist()
|
1610 |
| - >>> for fm in fm_list: print(fm) |
1611 |
| - [[1. 2.82842712] |
1612 |
| - [3. 5.65685425]] |
1613 |
| - [[ 5. 8.48528137] |
1614 |
| - [ 7. 11.3137085 ]] |
| 1608 | + >>> for fm in fm_list: print(fm) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1609 | + [[1. 2.8284...] |
| 1610 | + [3. 5.6568...]] |
| 1611 | + [[ 5. 8.4852...] |
| 1612 | + [ 7. 11.313...]] |
1615 | 1613 |
|
1616 | 1614 | Shift weight to single factor matrix and return list of factor matrices:
|
1617 | 1615 |
|
1618 | 1616 | >>> fm_list = K.tolist(0)
|
1619 |
| - >>> for fm in fm_list: print(fm) |
1620 |
| - [[ 8.60232527 40. ] |
1621 |
| - [25.8069758 80. ]] |
1622 |
| - [[0.58123819 0.6 ] |
1623 |
| - [0.81373347 0.8 ]] |
| 1617 | + >>> for fm in fm_list: print(fm) # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1618 | + [[ 8.6023... 40. ] |
| 1619 | + [25.8069... 80. ]] |
| 1620 | + [[0.5812... 0.6...] |
| 1621 | + [0.8137... 0.8...]] |
1624 | 1622 | """
|
1625 | 1623 | if mode is not None:
|
1626 | 1624 | if isinstance(mode, int) and mode in range(self.ndims):
|
@@ -2167,5 +2165,5 @@ def __repr__(self):
|
2167 | 2165 |
|
2168 | 2166 | if __name__ == "__main__":
|
2169 | 2167 | import doctest # pragma: no cover
|
2170 |
| - import pyttb as ttb # pragma: no cover |
| 2168 | + |
2171 | 2169 | doctest.testmod() # pragma: no cover
|
0 commit comments