-
Notifications
You must be signed in to change notification settings - Fork 66
/
vector.py
712 lines (632 loc) · 24.7 KB
/
vector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
from __future__ import print_function, absolute_import, division
from six.moves import xrange
import numpy as _np
from numbers import Number as _Number
from . import tt_f90 as _tt_f90
from tt.core.utils import my_chop2
import warnings
class vector(object):
"""The main class for working with vectors in the TT-format. It constructs
new TT-vector. When called with no arguments, creates dummy object which
can be filled from outside. When ``a`` is specified, computes approximate
decomposition of array ``a`` with accuracy ``eps``:
:param a: A tensor to approximate.
:type a: ndarray
:param eps: Approximation accuracy
:type a: float
:param rmax: Maximal rank
:type rmax: int
>>> a = numpy.sin(numpy.arange(2 ** 10)).reshape([2] * 10, order='F')
>>> a = tt.vector(a)
>>> a.r
array([1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1], dtype=int32)
>>> # now let's try different accuracy
>>> b = numpy.random.rand(2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
>>> btt = tt.vector(b, 1E-14)
>>> btt.r
array([ 1, 2, 4, 8, 16, 32, 16, 8, 4, 2, 1], dtype=int32)
>>> btt = tt.vector(b, 1E-1)
>>> btt.r
array([ 1, 2, 4, 8, 14, 20, 14, 8, 4, 2, 1], dtype=int32)
Attributes:
d : int
Dimensionality of the tensor.
n : ndarray of shape (d,)
Mode sizes of the tensor: if :math:`n_i=\\texttt{n[i-1]}`, then the tensor has shape :math:`n_1\\times\ldots\\times n_d`.
r : ndarray of shape (d+1,)
TT-ranks of current TT decomposition of the tensor.
core : ndarray
Flatten (Fortran-ordered) TT cores stored sequentially in a one-dimensional array.
To get a list of three-dimensional cores, use ``tt.vector.to_list(my_tensor)``.
"""
def __init__(self, a=None, eps=1e-14, rmax=100000):
if a is None:
self.core = _np.array([0.0])
self.d = 0
self.n = _np.array([0], dtype=_np.int32)
self.r = _np.array([1], dtype=_np.int32)
self.ps = _np.array([0], dtype=_np.int32)
return
self.d = a.ndim
self.n = _np.array(a.shape, dtype=_np.int32)
r = _np.zeros((self.d + 1,), dtype=_np.int32)
ps = _np.zeros((self.d + 1,), dtype=_np.int32)
if (_np.iscomplex(a).any()):
if rmax is not None:
self.r, self.ps = _tt_f90.tt_f90.zfull_to_tt(
a.flatten('F'), self.n, self.d, eps, rmax)
else:
self.r, self.ps = _tt_f90.tt_f90.zfull_to_tt(
a.flatten('F'), self.n, self.d, eps)
self.core = _tt_f90.tt_f90.zcore.copy()
else:
if rmax is not None:
self.r, self.ps = _tt_f90.tt_f90.dfull_to_tt(
_np.real(a).flatten('F'), self.n, self.d, eps, rmax)
else:
self.r, self.ps = _tt_f90.tt_f90.dfull_to_tt(
_np.real(a).flatten('F'), self.n, self.d, eps)
self.core = _tt_f90.tt_f90.core.copy()
_tt_f90.tt_f90.tt_dealloc()
@staticmethod
def from_list(a, order='F'):
"""Generate TT-vectorr object from given TT cores.
:param a: List of TT cores.
:type a: list
:returns: vector -- TT-vector constructed from the given cores.
"""
d = len(a) # Number of cores
res = vector()
n = _np.zeros(d, dtype=_np.int32)
r = _np.zeros(d+1, dtype=_np.int32)
cr = _np.array([])
for i in xrange(d):
cr = _np.concatenate((cr, a[i].flatten(order)))
r[i] = a[i].shape[0]
r[i+1] = a[i].shape[2]
n[i] = a[i].shape[1]
res.d = d
res.n = n
res.r = r
res.core = cr
res.get_ps()
return res
@staticmethod
def to_list(tt):
"""Return list of TT cores a TT decomposition consists of.
:param tt: TT-vector.
:type tt: vector
:returns: list -- list of ``tt.d`` three-dimensional cores, ``i``-th core is an ndarray of shape ``(tt.r[i], tt.n[i], tt.r[i+1])``.
"""
d = tt.d
r = tt.r
n = tt.n
ps = tt.ps
core = tt.core
res = []
for i in xrange(d):
cur_core = core[ps[i] - 1:ps[i + 1] - 1]
cur_core = cur_core.reshape((r[i], n[i], r[i + 1]), order='F')
res.append(cur_core)
return res
@property
def erank(self):
""" Effective rank of the TT-vector """
r = self.r
n = self.n
d = self.d
if d <= 1:
er = 0e0
else:
sz = _np.dot(n * r[0:d], r[1:])
if sz == 0:
er = 0e0
else:
b = r[0] * n[0] + n[d - 1] * r[d]
if d is 2:
er = sz * 1.0 / b
else:
a = _np.sum(n[1:d - 1])
er = (_np.sqrt(b * b + 4 * a * sz) - b) / (2 * a)
return er
def __getitem__(self, index):
"""Get element of the TT-vector.
:param index: array_like (it supports slicing).
:returns: number -- an element of the tensor or a new tensor.
Examples:
Suppose that a is a 3-dimensional tt.vector of size 4 x 5 x 6
a[1, 2, 3] returns the element with index (1, 2, 3)
a[1, :, 1:3] returns a 2-dimensional tt.vector of size 5 x 2
"""
if len(index) != self.d:
print("Incorrect index length.")
return
# TODO: add tests.
# TODO: in case of requesting one element this implementation is slower
# than the old one.
running_fact = None
answ_cores = []
for i in xrange(self.d):
# r0, n, r1 = cores[i].shape
cur_core = self.core[self.ps[i] - 1:self.ps[i + 1] - 1]
cur_core = cur_core.reshape(
(self.r[i], self.n[i], self.r[i + 1]), order='F')
cur_core = cur_core[
:, index[i], :].reshape(
(self.r[i], -1), order='F')
if running_fact is None:
new_r0 = self.r[i]
cur_core = cur_core.copy()
else:
new_r0 = running_fact.shape[0]
cur_core = _np.dot(running_fact, cur_core)
cur_core = cur_core.reshape((new_r0, -1, self.r[i + 1]), order='F')
if cur_core.shape[1] == 1:
running_fact = cur_core.reshape((new_r0, -1), order='F')
else:
answ_cores.append(cur_core)
running_fact = None
if len(answ_cores) == 0:
return running_fact[0, 0]
if running_fact is not None:
answ_cores[-1] = _np.dot(answ_cores[-1], running_fact)
return self.from_list(answ_cores)
@property
def is_complex(self):
return _np.iscomplexobj(self.core)
def _matrix__complex_op(self, op):
return self.__complex_op(op)
def __complex_op(self, op):
crs = tensor.to_list(self)
newcrs = []
cr = crs[0]
rl, n, rr = cr.shape
newcr = _np.zeros((rl, n, rr * 2), dtype=_np.float)
newcr[:, :, :rr] = _np.real(cr)
newcr[:, :, rr:] = _np.imag(cr)
newcrs.append(newcr)
for i in xrange(1, self.d - 1):
cr = crs[i]
rl, n, rr = cr.shape
newcr = _np.zeros((rl * 2, n, rr * 2), dtype=_np.float)
newcr[:rl, :, :rr] = newcr[rl:, :, rr:] = _np.real(cr)
newcr[:rl, :, rr:] = _np.imag(cr)
newcr[rl:, :, :rr] = -_np.imag(cr)
newcrs.append(newcr)
cr = crs[-1]
rl, n, rr = cr.shape
if op in ['R', 'r', 'Re']:
# get real part
newcr = _np.zeros((rl * 2, n, rr), dtype=_np.float)
newcr[:rl, :, :] = _np.real(cr)
newcr[rl:, :, :] = -_np.imag(cr)
elif op in ['I', 'i', 'Im']:
# get imaginary part
newcr = _np.zeros((rl * 2, n, rr), dtype=_np.float)
newcr[:rl, :, :] = _np.imag(cr)
newcr[rl:, :, :] = _np.real(cr)
elif op in ['A', 'B', 'all', 'both']:
# get both parts (increase dimensionality)
newcr = _np.zeros((rl * 2, n, 2 * rr), dtype=_np.float)
newcr[:rl, :, :rr] = _np.real(cr)
newcr[rl:, :, :rr] = -_np.imag(cr)
newcr[:rl, :, rr:] = _np.imag(cr)
newcr[rl:, :, rr:] = _np.real(cr)
newcrs.append(newcr)
newcr = _np.zeros((rr * 2, 2, 1), dtype=_np.float)
newcr[:rr, 0, :] = newcr[rr:, 1, :] = 1.0
elif op in ['M']:
# get matrix modificated for real-arithm. solver
newcr = _np.zeros((rl * 2, n, 2 * rr), dtype=_np.float)
newcr[:rl, :, :rr] = _np.real(cr)
newcr[rl:, :, :rr] = -_np.imag(cr)
newcr[:rl, :, rr:] = _np.imag(cr)
newcr[rl:, :, rr:] = _np.real(cr)
newcrs.append(newcr)
newcr = _np.zeros((rr * 2, 4, 1), dtype=_np.float)
newcr[:rr, [0, 3], :] = 1.0
newcr[rr:, 1, :] = 1.0
newcr[rr:, 2, :] = -1.0
else:
raise ValueError(
"Unexpected parameter " +
op +
" at tt.vector.__complex_op")
newcrs.append(newcr)
return vector.from_list(newcrs)
def real(self):
"""Get real part of a TT-vector."""
return self.__complex_op('Re')
def imag(self):
"""Get imaginary part of a TT-vector."""
return self.__complex_op('Im')
def c2r(self):
"""Get real vector.from complex one suitable for solving complex linear system with real solver.
For tensor :math:`X(i_1,\\ldots,i_d) = \\Re X + i\\Im X` returns (d+1)-dimensional tensor
of form :math:`[\\Re X\\ \\Im X]`. This function is useful for solving complex linear system
:math:`\\mathcal{A}X = B` with real solver by transforming it into
.. math::
\\begin{bmatrix}\\Re\\mathcal{A} & -\\Im\\mathcal{A} \\\\
\\Im\\mathcal{A} & \\Re\\mathcal{A} \\end{bmatrix}
\\begin{bmatrix}\\Re X \\\\ \\Im X\\end{bmatrix} =
\\begin{bmatrix}\\Re B \\\\ \\Im B\\end{bmatrix}.
"""
return self.__complex_op('both')
def r2c(self):
"""Get complex vector.from real one made by ``tensor.c2r()``.
For tensor :math:`\\tilde{X}(i_1,\\ldots,i_d,i_{d+1})` returns complex tensor
.. math::
X(i_1,\\ldots,i_d) = \\tilde{X}(i_1,\\ldots,i_d,0) + i\\tilde{X}(i_1,\\ldots,i_d,1).
>>> a = tt.rand(2,10,5) + 1j * tt.rand(2,10,5)
>>> (a.c2r().r2c() - a).norm() / a.norm()
7.310562016615692e-16
"""
tmp = self.copy()
newcore = _np.array(tmp.core, dtype=_np.complex)
cr = newcore[tmp.ps[-2] - 1:tmp.ps[-1] - 1]
cr = cr.reshape((tmp.r[-2], tmp.n[-1], tmp.r[-1]), order='F')
cr[:, 1, :] *= 1j
newcore[tmp.ps[-2] - 1:tmp.ps[-1] - 1] = cr.flatten('F')
tmp.core = newcore
return sum(tmp, axis=tmp.d - 1)
# Print statement
def __repr__(self):
if self.d == 0:
return "Empty tensor"
res = "This is a %d-dimensional tensor \n" % self.d
r = self.r
d = self.d
n = self.n
for i in range(0, d):
res = res + ("r(%d)=%d, n(%d)=%d \n" % (i, r[i], i, n[i]))
res = res + ("r(%d)=%d \n" % (d, r[d]))
return res
def write(self, fname):
if _np.iscomplexobj(self.core):
_tt_f90.tt_f90.ztt_write_wrapper(
self.n, self.r, self.ps, self.core, fname)
else:
_tt_f90.tt_f90.dtt_write_wrapper(
self.n, self.r, self.ps, _np.real(
self.core), fname)
def full(self, asvector=False):
"""Returns full array (uncompressed).
.. warning::
TT compression allows to keep in memory tensors much larger than ones PC can handle in
raw format. Therefore this function is quite unsafe; use it at your own risk.
:returns: numpy.ndarray -- full tensor.
"""
# Generate correct size vector
sz = self.n.copy()
if self.r[0] > 1:
sz = _np.concatenate(([self.r[0]], sz))
if self.r[self.d] > 1:
sz = _np.concatenate(([self.r[self.d]], sz))
if (_np.iscomplex(self.core).any()):
a = _tt_f90.tt_f90.ztt_to_full(
self.n, self.r, self.ps, self.core, _np.prod(sz))
else:
a = _tt_f90.tt_f90.dtt_to_full(
self.n, self.r, self.ps, _np.real(
self.core), _np.prod(sz))
a = a.reshape(sz, order='F')
if asvector:
a=a.flatten(order='F')
return a
def __add__(self, other):
if other is None:
return self
c = vector()
c.r = _np.zeros((self.d + 1,), dtype=_np.int32)
c.ps = _np.zeros((self.d + 1,), dtype=_np.int32)
c.n = self.n
c.d = self.d
if (_np.iscomplex(self.core).any() or _np.iscomplex(other.core).any()):
c.r, c.ps = _tt_f90.tt_f90.ztt_add(
self.n, self.r, other.r, self.ps, other.ps, self.core + 0j, other.core + 0j)
c.core = _tt_f90.tt_f90.zcore.copy()
else:
# This could be a real fix in the case we fell to the real world
c.r, c.ps = _tt_f90.tt_f90.dtt_add(
self.n, self.r, other.r, self.ps, other.ps, _np.real(
self.core), _np.real(
other.core))
c.core = _tt_f90.tt_f90.core.copy()
_tt_f90.tt_f90.tt_dealloc()
return c
def __radd__(self, other):
if other is None:
return self
return other + self
def round(self, eps=1e-14, rmax=1000000):
"""Applies TT rounding procedure to the TT-vector and **returns rounded tensor**.
:param eps: Rounding accuracy.
:type eps: float
:param rmax: Maximal rank
:type rmax: int
:returns: tensor -- rounded TT-vector.
Usage example:
>>> a = tt.ones(2, 10)
>>> b = a + a
>>> print b.r
array([1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1], dtype=int32)
>>> b = b.round(1E-14)
>>> print b.r
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int32)
"""
c = vector()
c.n = _np.copy(self.n)
c.d = self.d
c.r = _np.copy(self.r)
c.ps = _np.copy(self.ps)
if (_np.iscomplex(self.core).any()):
_tt_f90.tt_f90.ztt_compr2(c.n, c.r, c.ps, self.core, eps, rmax)
c.core = _tt_f90.tt_f90.zcore.copy()
else:
_tt_f90.tt_f90.dtt_compr2(c.n, c.r, c.ps, self.core, eps, rmax)
c.core = _tt_f90.tt_f90.core.copy()
_tt_f90.tt_f90.tt_dealloc()
return c
def norm(self):
if (_np.iscomplex(self.core).any()):
nrm = _tt_f90.tt_f90.ztt_nrm(self.n, self.r, self.ps, self.core)
else:
nrm = _tt_f90.tt_f90.dtt_nrm(
self.n, self.r, self.ps, _np.real(self.core))
return nrm
def __rmul__(self, other):
c = vector()
c.d = self.d
c.n = self.n
if isinstance(other, _Number):
c.r = self.r.copy()
c.ps = self.ps.copy()
c.core = self.core.copy()
new_core = c.core[c.ps[0] - 1:c.ps[1] - 1]
new_core = new_core * other
c.core = _np.array(c.core, dtype=new_core.dtype)
c.core[c.ps[0] - 1:c.ps[1] - 1] = new_core
else:
c = _hdm(self, other)
return c
def __mul__(self, other):
c = vector()
c.d = self.d
c.n = self.n
if isinstance(other, _Number):
c.r = self.r.copy()
c.ps = self.ps.copy()
c.core = self.core.copy()
new_core = c.core[c.ps[0] - 1:c.ps[1] - 1]
new_core = new_core * other
c.core = _np.array(c.core, dtype=new_core.dtype)
c.core[c.ps[0] - 1:c.ps[1] - 1] = new_core
else:
c = _hdm(other, self)
return c
def __sub__(self, other):
c = self + (-1) * other
return c
def __kron__(self, other):
if other is None:
return self
a = self
b = other
c = vector()
c.d = a.d + b.d
c.n = _np.concatenate((a.n, b.n))
c.r = _np.concatenate((a.r[0:a.d], b.r[0:b.d + 1]))
c.get_ps()
c.core = _np.concatenate((a.core, b.core))
return c
def __dot__(self, other):
r1 = self.r
r2 = other.r
d = self.d
if (_np.iscomplex(self.core).any() or _np.iscomplex(other.core).any()):
dt = _np.zeros(r1[0] * r2[0] * r1[d] * r2[d], dtype=_np.complex)
dt = _tt_f90.tt_f90.ztt_dotprod(
self.n,
r1,
r2,
self.ps,
other.ps,
self.core + 0j,
other.core + 0j,
dt.size)
else:
dt = _np.zeros(r1[0] * r2[0] * r1[d] * r2[d])
dt = _tt_f90.tt_f90.dtt_dotprod(
self.n, r1, r2, self.ps, other.ps, _np.real(
self.core), _np.real(
other.core), dt.size)
if dt.size is 1:
dt = dt[0]
return dt
def __col__(self, k):
c = vector()
d = self.d
r = self.r.copy()
n = self.n.copy()
ps = self.ps.copy()
core = self.core.copy()
last_core = self.core[ps[d - 1] - 1:ps[d] - 1]
last_core = last_core.reshape((r[d - 1] * n[d - 1], r[d]), order='F')
last_core = last_core[:, k]
try:
r[d] = len(k)
except:
r[d] = 1
ps[d] = ps[d - 1] + r[d - 1] * n[d - 1] * r[d]
core[ps[d - 1] - 1:ps[d] - 1] = last_core.flatten('F')
c.d = d
c.n = n
c.r = r
c.ps = ps
c.core = core
return c
def __diag__(self):
from . import matrix as _matrix
cl = tensor.to_list(self)
d = self.d
r = self.r
n = self.n
res = []
dtype = self.core.dtype
for i in xrange(d):
cur_core = cl[i]
res_core = _np.zeros((r[i], n[i], n[i], r[i + 1]), dtype=dtype)
for s1 in xrange(r[i]):
for s2 in xrange(r[i + 1]):
res_core[
s1, :, :, s2] = _np.diag(
cur_core[
s1, :, s2].reshape(
n[i], order='F'))
res.append(res_core)
return _matrix.matrix.from_list(res)
def __neg__(self):
return self * (-1)
def get_ps(self):
self.ps = _np.cumsum(
_np.concatenate(
([1],
self.n *
self.r[
0:self.d] *
self.r[
1:self.d +
1]))).astype(
_np.int32)
def alloc_core(self):
self.core = _np.zeros((self.ps[self.d] - 1,), dtype=_np.float)
def copy(self):
c = vector()
c.core = self.core.copy()
c.d = self.d
c.n = self.n.copy()
c.r = self.r.copy()
c.ps = self.ps.copy()
return c
def rmean(self):
""" Calculates the mean rank of a TT-vector."""
if not _np.all(self.n):
return 0
# Solving quadratic equation ar^2 + br + c = 0;
a = _np.sum(self.n[1:-1])
b = self.n[0] + self.n[-1]
c = - _np.sum(self.n * self.r[1:] * self.r[:-1])
D = b ** 2 - 4 * a * c
r = 0.5 * (-b + _np.sqrt(D)) / a
return r
def qtt_fft1(self,tol,inverse=False, bitReverse=True):
""" Compute 1D (inverse) discrete Fourier Transform in the QTT format.
:param tol: error tolerance.
:type tol: float
:param inverse: whether do an inverse FFT or not.
:type inverse: Boolean
:param bitReverse: whether do the bit reversion or not. If this function is used as a subroutine for multi-dimensional qtt-fft, this option
need to be set False.
:type bitReverse: Boolean.
:returns: QTT-vector of FFT coefficients.
This is a python translation of the Matlab function "qtt_fft1" in Ivan Oseledets' project TT-Toolbox(https://github.com/oseledets/TT-Toolbox)
See S. Dolgov, B. Khoromskij, D. Savostyanov,
Superfast Fourier transform using QTT approximation,
J. Fourier Anal. Appl., 18(5), 2012.
"""
d = self.d
r = self.r.copy()
y = self.to_list(self)
if inverse:
twiddle =-1+1.22e-16j # exp(pi*1j)
else:
twiddle =-1-1.22e-16j # exp(-pi*1j)
for i in range(d-1, 0, -1):
r1= y[i].shape[0] # head r
r2= y[i].shape[2] # tail r
crd2 = _np.zeros((r1, 2, r2), order='F', dtype=complex)
# last block +-
crd2[:,0,:]= (y[i][:,0,:] + y[i][:,1,:])/_np.sqrt(2)
crd2[:,1,:]= (y[i][:,0,:] - y[i][:,1,:])/_np.sqrt(2)
# last block twiddles
y[i]= _np.zeros((r1*2, 2, r2),order='F',dtype=complex)
y[i][0:r1, 0, 0:r2]= crd2[:,0,:]
y[i][r1:r1*2, 1, 0:r2]= crd2[:,1,:]
#1..i-1 block twiddles and qr
rv=1;
for j in range(0, i):
cr=y[j]
r1= cr.shape[0] # head r
r2= cr.shape[2] # tail r
if j==0:
r[j]=r1
r[j+1] = r2*2
y[j] = _np.zeros((r[j], 2, r[j+1]),order='F',dtype=complex)
y[j][0:r1, :, 0:r2] = cr
y[j][0:r1, 0, r2 :r[j+1]] = cr[:,0,:]
y[j][0:r1, 1, r2 :r[j+1]] = twiddle**(1.0/(2**(i-j)))*cr[:,1,:]
else:
r[j]=r1*2
r[j+1] = r2*2
y[j] = _np.zeros((r[j], 2, r[j+1]),order='F',dtype=complex)
y[j][0:r1, :, 0:r2] = cr
y[j][r1:r[j], 0, r2 :r[j+1]] = cr[:,0,:]
y[j][r1:r[j], 1, r2 :r[j+1]] = twiddle**(1.0/(2**(i-j)))*cr[:,1,:]
y[j] = _np.reshape(y[j],( r[j], 2*r[j+1]),order='F')
y[j] = _np.dot(rv,y[j])
r[j] = y[j].shape[0]
y[j] = _np.reshape(y[j],( 2*r[j], r[j+1]),order='F')
y[j], rv = _np.linalg.qr(y[j])
y[j] = _np.reshape(y[j], (r[j], 2, rv.shape[0]),order='F')
y[i] = _np.reshape(y[i], (r[i], 2*r[i+1]),order='F')
y[i] = _np.dot(rv,y[i])
r[i] = rv.shape[0]
# backward svd
for j in range(i, 0,-1):
u,s,v = _np.linalg.svd(y[j], full_matrices=False)
rnew = my_chop2(s, _np.linalg.norm(s)*tol/_np.sqrt(i))
u=_np.dot(u[:, 0:rnew], _np.diag(s[0:rnew]))
v= v[0:rnew, :]
y[j] = _np.reshape(v, (rnew, 2, r[j+1]),order='F' )
y[j-1] = _np.reshape(y[j-1], (r[j-1]*2,r[j] ),order='F' )
y[j-1] = _np.dot(y[j-1], u)
r[j] = rnew
y[j-1] = _np.reshape(y[j-1], (r[j-1],r[j]*2 ),order='F' )
y[0] = _np.reshape(y[0], (r[0],2, r[1]), order='F' )
# FFT on the first block
y[0]=_np.transpose(y[0],(1,0,2))
y[0]=_np.reshape(y[0],(2, r[0]*r[1]),order='F')
y[0]= _np.dot( _np.array([[1,1],[1,-1]]), y[0])/_np.sqrt(2)
y[0]=_np.reshape(y[0],(2, r[0], r[1]),order='F')
y[0]=_np.transpose(y[0],(1,0,2))
if bitReverse:
# Reverse the train
y2=[None]*d
for i in range(d):
y2[d-i-1]= _np.transpose(y[i],(2,1,0))
y=self.from_list(y2)
else: # for multi-dimensional qtt_fft
y=self.from_list(y)
return y
def _hdm(a, b):
c = vector()
c.d = a.d
c.n = a.n
c.r = _np.zeros((a.d + 1, 1), dtype=_np.int32)
c.ps = _np.zeros((a.d + 1, 1), dtype=_np.int32)
if _np.iscomplexobj(a.core) or _np.iscomplexobj(b.core):
c.r, c.ps = _tt_f90.tt_f90.ztt_hdm(
a.n, a.r, b.r, a.ps, b.ps, a.core, b.core)
c.core = _tt_f90.tt_f90.zcore.copy()
else:
c.r, c.ps = _tt_f90.tt_f90.dtt_hdm(
a.n, a.r, b.r, a.ps, b.ps, a.core, b.core)
c.core = _tt_f90.tt_f90.core.copy()
_tt_f90.tt_f90.tt_dealloc()
return c
class tensor(vector): # For combatibility issues
def __init__(self, *args, **kwargs):
super(tensor, self).__init__(*args, **kwargs)
warnings.warn(
"tt.tensor is deprecated, use tt.vector instead",
DeprecationWarning)