Skip to content

Commit

Permalink
Merge pull request oseledets#33 from rballester/oseledets-develop
Browse files Browse the repository at this point in the history
Fixed print() for Python 2 and 3
  • Loading branch information
oseledets authored Mar 18, 2017
2 parents efccde8 + fc18a82 commit e3c24e0
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 26 deletions.
3 changes: 2 additions & 1 deletion tt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
try:
from core.tt import *
except:
Expand All @@ -8,7 +9,7 @@
try:
ctypes.CDLL("liblapack.so", ctypes.RTLD_GLOBAL)
except:
print "Did not find MKL or LAPACK library"
print("Did not find MKL or LAPACK library")
from core.tt import *

from multifuncrs import multifuncrs
Expand Down
9 changes: 5 additions & 4 deletions tt/amen/amen_mv.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import tt as _tt
import numpy as _np
from scipy.sparse import spdiags as _spdiags
Expand Down Expand Up @@ -112,7 +113,7 @@ def amen_mv(A, x, tol, y=None, z=None, nswp=20, kickrank=4,
'''

if renorm is 'gram':
print "Not implemented yet. Renorm is switched to 'direct'"
print("Not implemented yet. Renorm is switched to 'direct'")
renorm = 'direct'

if isinstance(x, _tt.vector):
Expand Down Expand Up @@ -436,12 +437,12 @@ def amen_mv(A, x, tol, y=None, z=None, nswp=20, kickrank=4,
phizy[i + 1], z[i], y[i], 'rl', return_norm=False)

if (verb > 1):
print 'amen-mv: swp=[%d,%d], dx=%.3e, r=%d, |y|=%.3e, |z|=%.3e' % (swp, i, dx, r, _np.linalg.norm(cry), nrmz)
print('amen-mv: swp=[%d,%d], dx=%.3e, r=%d, |y|=%.3e, |z|=%.3e' % (swp, i, dx, r, _np.linalg.norm(cry), nrmz))

# Stopping or reversing
if ((direct > 0) and (i == d - 1)) or ((direct < 0) and (i == 0)):
if (verb > 0):
print 'amen-mv: swp=%d{%d}, max_dx=%.3e, max_r=%d' % (swp, (1 - direct) / 2, max_dx, max(ry))
print('amen-mv: swp=%d{%d}, max_dx=%.3e, max_r=%d' % (swp, (1 - direct) / 2, max_dx, max(ry)))
if ((max_dx < tol) or (swp == nswp)) and (direct > 0):
break
else:
Expand Down Expand Up @@ -679,4 +680,4 @@ def _bfun3(Phi1, A, Phi2, x):
kickrank2=0, verb=True, init_qr=True, renorm='gram', fkick=False)
d = _tt.matvec(A, b).round(eps)

print (c[0] - d).norm() / d.norm()
print((c[0] - d).norm() / d.norm())
15 changes: 8 additions & 7 deletions tt/core/tools.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import numpy as _np
import math as _math
import copy as _cp
Expand Down Expand Up @@ -59,7 +60,7 @@ def get_core(i):
newr = min(rl * n, rr)
ccr = u[:, :newr].reshape((rl, n, newr), order='F')
v = _np.dot(_np.diag(s[:newr]), v[:newr, :])
# print ccr.shape
# print(ccr.shape)
# r x r . r x n x R -> r x n x R
nrm = _np.tensordot(nrm, ccr, (0, 0))
# r x n x R . r x n x R -> n x R x n x R
Expand All @@ -68,9 +69,9 @@ def get_core(i):
nrm = nrm.sum(axis=2) # R x R x n -> R x R
if nrm.size > 1:
raise Exception('too many numbers in norm')
# print "Norm calculated:", nrm
# print("Norm calculated:", nrm)
nrm = _np.sqrt(_np.linalg.norm(nrm))
# print "Norm predicted:", nrm
# print("Norm predicted:", nrm)
compression = compression * nrm / _np.sqrt(d - 1)
v = _np.array([[1.0]])

Expand All @@ -87,15 +88,15 @@ def get_core(i):
ss = _np.cumsum(s[::-1])[::-1]
newr = max(min([r for r in range(ss.size) if ss[
r] <= compression] + [min(rl * n, rr)]), 1)
# print "Rank % 4d replaced by % 4d" % (rr, newr)
# print("Rank % 4d replaced by % 4d" % (rr, newr))
ccr = u[:, :newr].reshape((rl, n, newr), order='F')
v = _np.dot(_np.diag(s[:newr]), v[:newr, :])
ccrs.append(ccr)
result = _vector.vector.from_list(ccrs)
if compression:
# print result
print "Norm actual:", result.norm(), " mean rank:", result.rmean()
# print "Norm very actual:", matvec(a,b).norm()
# print(result)
print("Norm actual:", result.norm(), " mean rank:", result.rmean())
# print("Norm very actual:", matvec(a,b).norm())
return result


Expand Down
7 changes: 4 additions & 3 deletions tt/multifuncrs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import numpy as np
from numpy import prod, nonzero, size
import math
Expand Down Expand Up @@ -229,7 +230,7 @@ def multifuncrs(X, funs, eps=1E-6,
s = np.ones((r, ))

if verb > 1:
print '=multifuncrs= block %d{%d}, dy: %3.3e, r: %d' % (i, dirn, dy[i], r)
print('=multifuncrs= block %d{%d}, dy: %3.3e, r: %d' % (i, dirn, dy[i], r))

# kicks and interfaces
if dirn > 0 and i < d - 1:
Expand Down Expand Up @@ -398,8 +399,8 @@ def multifuncrs(X, funs, eps=1E-6,
if cur_order[order_index] == 0:
order_index = order_index + 1
if verb > 0:
print '=multifuncrs= sweep %d{%d}, max_dy: %3.3e, erank: %g' % (swp, order_index, max_dy,
math.sqrt(np.dot(ry[:d], n * ry[1:]) / np.sum(n)))
print('=multifuncrs= sweep %d{%d}, max_dy: %3.3e, erank: %g' % (swp, order_index, max_dy,
math.sqrt(np.dot(ry[:d], n * ry[1:]) / np.sum(n))))

if last_sweep:
break
Expand Down
9 changes: 5 additions & 4 deletions tt/multifuncrs2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import numpy as np
from numpy import prod, nonzero, size
import math
Expand Down Expand Up @@ -176,7 +177,7 @@ def multifuncrs2(X, funs, eps=1e-6,
try:
dy = np.linalg.norm(newy - oldy) / np.linalg.norm(newy)
except ZeroDivisionError:
print 'Bad initial indices, the solution is exactly zero. Restarting'
print('Bad initial indices, the solution is exactly zero. Restarting')
return
max_dy = max(max_dy, dy)
# truncation
Expand Down Expand Up @@ -214,7 +215,7 @@ def multifuncrs2(X, funs, eps=1e-6,
r = u.shape[1]
s = np.ones((r, ))
if verb > 1:
print '=multifuncrs2= block %d{%d}, dy: %3.3e, r: %d' % (i, dirn, dy, r)
print('=multifuncrs2= block %d{%d}, dy: %3.3e, r: %d' % (i, dirn, dy, r))
#Kicks and interfaces
if dirn > 0 and i < d - 1:
u = u[:, :r]
Expand Down Expand Up @@ -470,8 +471,8 @@ def multifuncrs2(X, funs, eps=1e-6,
if cur_order[order_index] == 0:
order_index = order_index + 1
if verb > 0:
print '=multifuncrs= sweep %d{%d}, max_dy: %3.3e, erank: %g' % (swp, order_index, max_dy,
math.sqrt(np.dot(ry[:d], n * ry[1:]) / np.sum(n)))
print('=multifuncrs= sweep %d{%d}, max_dy: %3.3e, erank: %g' % (swp, order_index, max_dy,
math.sqrt(np.dot(ry[:d], n * ry[1:]) / np.sum(n))))
if max_dy < eps_exit and dirn > 0:
break
if order_index >= len(cur_order): # New global sweep
Expand Down
5 changes: 3 additions & 2 deletions tt/optimize/tt_min.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""This module contains a prototype implementation of the
TT-cross-based minimization procedure
"""
from __future__ import print_function
import numpy as np
import math
import tt
Expand Down Expand Up @@ -102,7 +103,7 @@ def min_func(fun, bounds_min, bounds_max, d=None, rmax=10,
x_full = J[ind_cur, :]
val = fun(x_full)
if verb:
print 'New record:', val, 'Point:', x_full, 'fevals:', fun_evals
print('New record:', val, 'Point:', x_full, 'fevals:', fun_evals)
cry = smooth_fun(cry, lm)
if (dirn < 0 and i > 0):
cry = reshape(cry, (ry[i], n[i] * ry[i + 1]))
Expand Down Expand Up @@ -213,7 +214,7 @@ def min_tens(tens, rmax=10, nswp=10, verb=True, smooth_fun=None):
x_full = J[ind_cur, :]
val = tens[x_full]
if verb:
print 'New record:', val, 'Point:', x_full, 'elements seen:', elements_seen
print('New record:', val, 'Point:', x_full, 'elements seen:', elements_seen)
cry = smooth_fun(cry, lm)
if dirn < 0 and i > 0:
cry = reshape(cry, (ry[i], n[i] * ry[i + 1]))
Expand Down
11 changes: 6 additions & 5 deletions tt/solvers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import numpy as np
import tt

Expand All @@ -17,23 +18,23 @@ def GMRES(A, u_0, b, eps=1E-6, restart=20, verb=0):
bnorm = b.norm()
curr_beta = beta
if verb:
print "/ Initial residual norm: %lf; mean rank:" % beta, r0.rmean()
print("/ Initial residual norm: %lf; mean rank:" % beta, r0.rmean())
m = restart
V = np.zeros(m + 1, dtype=object) # Krylov basis
V[0] = r0 * (1.0 / beta)
H = np.mat(np.zeros((m + 1, m), dtype=np.complex128, order='F'))
j = 0
while j < m and curr_beta / bnorm > eps:
delta = eps / (curr_beta / beta)
# print "Calculating new Krylov vector"
# print("Calculating new Krylov vector")
w = A(V[j], delta)
#w = w.round(delta)
for i in range(j + 1):
H[i, j] = tt.dot(w, V[i])
w = w + (-H[i, j]) * V[i]
w = w.round(delta)
if verb > 1:
print "|% 3d. New Krylov vector mean rank:" % (j + 1), w.rmean()
print("|% 3d. New Krylov vector mean rank:" % (j + 1), w.rmean())
H[j + 1, j] = w.norm()
V[j + 1] = w * (1 / H[j + 1, j])

Expand All @@ -44,14 +45,14 @@ def GMRES(A, u_0, b, eps=1E-6, restart=20, verb=0):
y, curr_beta, rank, s = np.linalg.lstsq(Hj, betae)
curr_beta = curr_beta[0]
if verb:
print "|% 3d. LSTSQ residual norm:" % (j + 1), curr_beta
print("|% 3d. LSTSQ residual norm:" % (j + 1), curr_beta)
j += 1
x = u_0
for i in range(j):
x = x + V[i] * y[i]
x = x.round(eps)
if verb:
print "\\ Solution mean rank:", x.rmean()
print("\\ Solution mean rank:", x.rmean())
u_0 = x
do_restart = (curr_beta / bnorm > eps)
return x

0 comments on commit e3c24e0

Please sign in to comment.