def full(self, **kwargs): """convert a tucker representation to a full tensor object A = CORE (*1) Basis1 (*2) Basis2 (*3) Basis3 ..., with (*n) means n-mode product. from paper "A MULTILINEAR SINGULAR VALUE DECOMPOSITION" by LIEVEN DE LATHAUWER , BART DE MOOR , AND JOOS VANDEWALLE """ # if core is a single scalar value, make it in a n-D array shape if np.prod(self.core.shape) == 1: self.core = np.reshape(self.core, tuple(self.r)) if self.Fourier: res = self.fourier() else: res = self val = res.core.copy() for i in range(self.order): val = nModeProduct(val, res.basis[i].T, i) T = Tensor(name=res.name, val=val, order=0, N=val.shape, Fourier=False, **kwargs) if self.Fourier: T.fourier() return T
def full(self, **kwargs): "return a full tensor object" if self.order == 2: if self.Fourier: res = self.fourier(copy=True) else: res = self # generate Tensor in real domain val = np.einsum('i,ik,il->kl', res.core, res.basis[0], res.basis[1]) kwargsT = dict(name=res.name, val=val, order=0, N=val.shape, Fourier=False, fft_form='c') kwargsT.update(kwargs) T = Tensor(**kwargsT) if self.Fourier: T.fourier() return T else: raise NotImplementedError()
def test_even(self): print('\nChecking Tensors with even grid points...') for dim, n, fft_form in itertools.product([2, 3], [4, 5], fft_forms): msg = 'Tensors with: dim={}, n={}, fft_form={}'.format( dim, n, fft_form) N = dim * (n, ) M = tuple(2 * np.array(N)) u = Tensor(name='test', shape=(), N=N, Fourier=False, fft_form=fft_form) u.randomize() Fu = u.fourier(copy=True) FuM = Fu.project(M) uM = FuM.fourier() if n % 2 == 0: self.assertGreaterEqual(u.norm(), FuM.norm(), msg=msg) self.assertGreaterEqual(u.norm(componentwise=True), FuM.norm(componentwise=True), msg=msg) self.assertGreaterEqual(u.norm(), uM.norm(), msg=msg) self.assertGreaterEqual(u.norm(componentwise=True), uM.norm(componentwise=True), msg=msg) else: self.assertAlmostEqual(u.norm(), FuM.norm(), msg=msg) self.assertAlmostEqual(u.norm(componentwise=True), FuM.norm(componentwise=True), msg=msg) self.assertAlmostEqual(u.norm(), uM.norm(), msg=msg) self.assertAlmostEqual(u.norm(componentwise=True), uM.norm(componentwise=True), msg=msg) self.assertAlmostEqual(0, u.mean() - FuM.mean(), msg=msg) self.assertAlmostEqual(u.mean(), uM.mean(), msg=msg) # testing that that interpolation on double grid have exactly the same values slc = tuple(u.order * [ slice(None), ] + [slice(0, M[i], 2) for i in range(dim)]) self.assertAlmostEqual(0, np.linalg.norm(u.val - uM.val[slc]), msg=msg) print('...ok')
def full(self, **kwargs): """ convert TT to a full tensor object """ if self.Fourier: res = self.fourier() else: res = self val = vector.full(res) # Tensor with the default fft_form for full tensor T = Tensor(name=res.name, val=val, order=0, N=val.shape, Fourier=False, **kwargs) if self.Fourier: T.fourier() return T
def test_even(self): print('\nChecking Tensors with even grid points...') for dim, n, fft_form in itertools.product([2,3], [4,5], fft_forms): msg='Tensors with: dim={}, n={}, fft_form={}'.format(dim, n, fft_form) N=dim*(n,) M=tuple(2*np.array(N)) u=Tensor(name='test', shape=(), N=N, Fourier=False, fft_form=fft_form) u.randomize() Fu=u.fourier(copy=True) FuM=Fu.project(M) uM=FuM.fourier() if n%2 == 0: self.assertGreaterEqual(u.norm(), FuM.norm(), msg=msg) self.assertGreaterEqual(u.norm(componentwise=True), FuM.norm(componentwise=True), msg=msg) self.assertGreaterEqual(u.norm(), uM.norm(), msg=msg) self.assertGreaterEqual(u.norm(componentwise=True), uM.norm(componentwise=True), msg=msg) else: self.assertAlmostEqual(u.norm(), FuM.norm(), msg=msg) self.assertAlmostEqual(u.norm(componentwise=True), FuM.norm(componentwise=True), msg=msg) self.assertAlmostEqual(u.norm(), uM.norm(), msg=msg) self.assertAlmostEqual(u.norm(componentwise=True), uM.norm(componentwise=True), msg=msg) self.assertAlmostEqual(0, u.mean()-FuM.mean(), msg=msg) self.assertAlmostEqual(u.mean(), uM.mean(), msg=msg) # testing that that interpolation on double grid have exactly the same values slc=tuple(u.order*[slice(None),]+[slice(0,M[i],2) for i in range(dim)]) self.assertAlmostEqual(0, np.linalg.norm(u.val-uM.val[slc]), msg=msg) print('...ok')
def test_Fourier(self): print('\nChecking Fourier functions ...') for opt in [0, 'c']: a = SparseTensor(kind='cano', val=self.T2d, fft_form=opt) T = Tensor(val=self.T2d, order=0, N=self.T2d.shape, Fourier=False, fft_form=opt) self.assertAlmostEqual( norm(a.fourier().full(fft_form=opt).val - T.fourier(copy=True).val), 0) self.assertEqual( norm(a.fourier().fourier(real_output=True).full().val.imag), 0) a = SparseTensor(kind='tucker', val=self.T3d, fft_form=opt) T = Tensor(val=self.T3d, order=0, N=self.T3d.shape, Fourier=False, fft_form=opt) self.assertAlmostEqual( norm(a.fourier().full(fft_form=opt) - T.fourier(copy=True)), 0) self.assertEqual( norm(a.fourier().fourier(real_output=True).full().val.imag), 0) a = SparseTensor(kind='tt', val=self.T3d, fft_form=opt) T = Tensor(val=self.T3d, order=0, N=self.T3d.shape, Fourier=False, fft_form=opt) self.assertAlmostEqual( norm(a.fourier().full(fft_form=opt) - T.fourier(copy=True).val), 0) self.assertEqual( norm(a.fourier().fourier(real_output=True).full().val.imag), 0) # checking shifting fft_forms sparse_opt = 'sr' for full_opt in [0, 'c']: a = SparseTensor(kind='cano', val=self.T2d, fft_form=sparse_opt) T = Tensor(val=self.T2d, order=0, N=self.T2d.shape, Fourier=False, fft_form=full_opt) self.assertAlmostEqual( norm(a.fourier().full(fft_form=full_opt) - T.fourier(copy=True)), 0) self.assertAlmostEqual((a.fourier().set_fft_form(full_opt) - a.set_fft_form(full_opt).fourier()).norm(), 0) a = SparseTensor(kind='tucker', val=self.T3d, fft_form=sparse_opt) T = Tensor(val=self.T3d, order=0, N=self.T3d.shape, Fourier=False, fft_form=full_opt) self.assertAlmostEqual( norm(a.fourier().full(fft_form=full_opt) - T.fourier(copy=True)), 0) self.assertAlmostEqual((a.fourier().set_fft_form(full_opt) - a.set_fft_form(full_opt).fourier()).norm(), 0) a = SparseTensor(kind='tt', val=self.T3d, fft_form=sparse_opt) T = Tensor(val=self.T3d, order=0, N=self.T3d.shape, Fourier=False, fft_form=full_opt) self.assertAlmostEqual( norm(a.fourier().full(fft_form=full_opt) - T.fourier(copy=True)), 0) self.assertAlmostEqual((a.fourier().set_fft_form(full_opt) - a.set_fft_form(full_opt).fourier()).norm(), 0) print('...ok')