Skip to content

Commit

Permalink
apply 2to3 to ./documentation and ./experimental
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Walter committed Aug 20, 2013
1 parent 74248f9 commit cefaa9a
Show file tree
Hide file tree
Showing 78 changed files with 668 additions and 606 deletions.
8 changes: 4 additions & 4 deletions documentation/AD_tutorial_TU_Berlin/example1_qr.py
Expand Up @@ -7,13 +7,13 @@
B = UTPM.dot(Q,R)

# check that the results are correct
print 'Q.T Q - 1\n',UTPM.dot(Q.T,Q) - numpy.eye(N)
print 'QR - A\n',B - A
print 'triu(R) - R\n', UTPM.triu(R) - R
print('Q.T Q - 1\n',UTPM.dot(Q.T,Q) - numpy.eye(N))
print('QR - A\n',B - A)
print('triu(R) - R\n', UTPM.triu(R) - R)

# QR decomposition, UTPM reverse
Bbar = UTPM(numpy.random.rand(D,P,M,N))
Qbar,Rbar = UTPM.pb_dot(Bbar, Q, R, B)
Abar = UTPM.pb_qr(Qbar, Rbar, A, Q, R)

print 'Abar - Bbar\n',Abar - Bbar
print('Abar - Bbar\n',Abar - Bbar)
Expand Up @@ -29,9 +29,9 @@ def alpha(beta):
res_2_list = []
res_3_list = []

betas = range(0,200,10)
betas = list(range(0,200,10))
for beta in betas:
print 'perform QR decomposition'
print('perform QR decomposition')
a = alpha(beta)
A = a[0]*dot(x1,x1.T) + a[1]*dot(x2,x2.T) + a[2]*dot(x3,x3.T)

Expand Down
Expand Up @@ -35,10 +35,10 @@
# Method 1: Naive approach
Apinv = dot(inv(dot(A.T,A)),A.T)

print 'naive approach: A Apinv A - A = 0 \n', dot(dot(A, Apinv),A) - A
print 'naive approach: Apinv A Apinv - Apinv = 0 \n', dot(dot(Apinv, A),Apinv) - Apinv
print 'naive approach: (Apinv A)^T - Apinv A = 0 \n', dot(Apinv, A).T - dot(Apinv, A)
print 'naive approach: (A Apinv)^T - A Apinv = 0 \n', dot(A, Apinv).T - dot(A, Apinv)
print('naive approach: A Apinv A - A = 0 \n', dot(dot(A, Apinv),A) - A)
print('naive approach: Apinv A Apinv - Apinv = 0 \n', dot(dot(Apinv, A),Apinv) - Apinv)
print('naive approach: (Apinv A)^T - Apinv A = 0 \n', dot(Apinv, A).T - dot(Apinv, A))
print('naive approach: (A Apinv)^T - A Apinv = 0 \n', dot(A, Apinv).T - dot(A, Apinv))


# Method 2: Using the differentiated QR decomposition
Expand All @@ -47,7 +47,7 @@
tmp2 = solve(R, tmp1)
Apinv = tmp2

print 'QR approach: A Apinv A - A = 0 \n', dot(dot(A, Apinv),A) - A
print 'QR approach: Apinv A Apinv - Apinv = 0 \n', dot(dot(Apinv, A),Apinv) - Apinv
print 'QR approach: (Apinv A)^T - Apinv A = 0 \n', dot(Apinv, A).T - dot(Apinv, A)
print 'QR approach: (A Apinv)^T - A Apinv = 0 \n', dot(A, Apinv).T - dot(A, Apinv)
print('QR approach: A Apinv A - A = 0 \n', dot(dot(A, Apinv),A) - A)
print('QR approach: Apinv A Apinv - Apinv = 0 \n', dot(dot(Apinv, A),Apinv) - Apinv)
print('QR approach: (Apinv A)^T - Apinv A = 0 \n', dot(Apinv, A).T - dot(Apinv, A))
print('QR approach: (A Apinv)^T - A Apinv = 0 \n', dot(A, Apinv).T - dot(A, Apinv))
4 changes: 2 additions & 2 deletions documentation/AD_tutorial_TU_Berlin/example4_eigh.py
Expand Up @@ -10,6 +10,6 @@
L = UTPM.diag(l)
B = UTPM.dot(Q,UTPM.dot(L,Q.T))

print 'B = \n', B
print('B = \n', B)
l2,Q2 = UTPM.eigh(B)
print 'l2 - l =\n',l2 - l
print('l2 - l =\n',l2 - l)
Expand Up @@ -6,8 +6,8 @@ def f(x):
x = [UTPS([3,1,0],P=2), UTPS([7,0,1],P=2)]
y = f(x)

print 'normal function evaluation y_0 = f(x_0) = ', y.data[0]
print 'gradient evaluation g(x_0) = ', y.data[1:]
print('normal function evaluation y_0 = f(x_0) = ', y.data[0])
print('gradient evaluation g(x_0) = ', y.data[1:])



Expand Down
Expand Up @@ -12,13 +12,13 @@ def f_fcn(x):

S = array([[1,0,1],[0,1,1]], dtype=float)
P = S.shape[1]
print 'seed matrix with P = %d directions S = \n'%P, S
print('seed matrix with P = %d directions S = \n'%P, S)
x1 = UTPS(zeros(1+2*P), P = P)
x2 = UTPS(zeros(1+2*P), P = P)
x1.data[0] = 3; x1.data[1::2] = S[0,:]
x2.data[0] = 7; x2.data[1::2] = S[1,:]
y = f_fcn([x1,x2])
print 'x1=',x1; print 'x2=',x2; print 'y=',y
print('x1=',x1); print('x2=',x2); print('y=',y)
H = zeros((2,2),dtype=float)
H[0,0] = 2*y.coeff[0,2]
H[1,0] = H[0,1] = (y.coeff[2,2] - y.coeff[0,2] - y.coeff[1,2])
Expand All @@ -32,8 +32,8 @@ def H_fcn(x):
-(sin(x[1])*x[0])**2*sin(x[0]+cos(x[1])*x[0])
return array([[H11, H21],[H21,H22]])

print 'symbolic Hessian - AD Hessian = \n', H - H_fcn([3,7])
print 'exact interpolation Hessian H(x_0) = \n', H
print('symbolic Hessian - AD Hessian = \n', H - H_fcn([3,7]))
print('exact interpolation Hessian H(x_0) = \n', H)



Expand Down
Expand Up @@ -18,6 +18,6 @@
v1bar += v2bar * vm1; vm1bar += v2bar * v1
v0bar -= v1bar * sin(v0)
g1 = y.data[1:]; g2 = numpy.array([vm1bar.data[0], v0bar.data[0]])
print 'UTPS gradient g(x_0)=', g1
print 'reverse gradient g(x_0)=', g2
print 'Hessian H(x_0)=\n',numpy.vstack([vm1bar.data[1:], v0bar.data[1:]])
print('UTPS gradient g(x_0)=', g1)
print('reverse gradient g(x_0)=', g2)
print('Hessian H(x_0)=\n',numpy.vstack([vm1bar.data[1:], v0bar.data[1:]]))
54 changes: 27 additions & 27 deletions documentation/ICCS2010/accuracy_and_runtime_check.py
Expand Up @@ -14,11 +14,11 @@


D,P,M,N = 5,4,100,5
print ''
print '-----------------------------------------------------------------------------------------------------------'
print 'testing SPMD (%d datasets) differentiated QR decomposition for matrix A.shape = (%d,%d) up to %d\'th order'%(P,M,N,D-1)
print '-----------------------------------------------------------------------------------------------------------'
print ''
print('')
print('-----------------------------------------------------------------------------------------------------------')
print('testing SPMD (%d datasets) differentiated QR decomposition for matrix A.shape = (%d,%d) up to %d\'th order'%(P,M,N,D-1))
print('-----------------------------------------------------------------------------------------------------------')
print('')

A = utpm.UTPM(numpy.random.rand(D,P,M,N))

Expand All @@ -36,7 +36,7 @@

# check that the pushforward computed correctly
B = utpm.UTPM.dot(Q,R)
print 'largest error for all QR decompositions and derivative degrees is: ',numpy.max(A.data - B.data)
print('largest error for all QR decompositions and derivative degrees is: ',numpy.max(A.data - B.data))


# check runtime
Expand All @@ -48,7 +48,7 @@
toc = time.time()
runtime_normal += toc - tic

print 'measured runtime ratio push_forward/normal: ', runtime_push_forward/runtime_normal
print('measured runtime ratio push_forward/normal: ', runtime_push_forward/runtime_normal)



Expand All @@ -57,11 +57,11 @@


D,P,N = 5,5,20
print ''
print '-----------------------------------------------------------------------------------------------------------'
print 'testing SPMD (%d datasets) push forward eig decomposition for matrix A.shape = (%d,%d) up to %d\'th order'%(P,N,N,D-1)
print '-----------------------------------------------------------------------------------------------------------'
print ''
print('')
print('-----------------------------------------------------------------------------------------------------------')
print('testing SPMD (%d datasets) push forward eig decomposition for matrix A.shape = (%d,%d) up to %d\'th order'%(P,N,N,D-1))
print('-----------------------------------------------------------------------------------------------------------')
print('')

# create symmetric matrix
A = utpm.UTPM(numpy.random.rand(D,P,N,N))
Expand All @@ -82,7 +82,7 @@
# check that the pushforward computed correctly
L = utpm.UTPM.diag(l)
B = utpm.UTPM.dot(utpm.UTPM.dot(Q,L), Q.T)
print 'largest error for all eigenvalue decompositions and derivative degrees is: ',numpy.max(A.data - B.data)
print('largest error for all eigenvalue decompositions and derivative degrees is: ',numpy.max(A.data - B.data))

# check runtime
runtime_normal = 0.
Expand All @@ -94,7 +94,7 @@
toc = time.time()
runtime_normal += toc - tic

print 'measured runtime ratio push_forward/normal: ', runtime_push_forward/runtime_normal
print('measured runtime ratio push_forward/normal: ', runtime_push_forward/runtime_normal)



Expand All @@ -104,11 +104,11 @@


D,P,M,N = 2,1,50,8
print ''
print '--------------------------------------------------------------------------------------------------------------------'
print 'testing SPMD (%d datasets) pullback qr decomposition for matrix A.shape = (%d,%d) up to first order'%(P,M,N)
print '--------------------------------------------------------------------------------------------------------------------'
print ''
print('')
print('--------------------------------------------------------------------------------------------------------------------')
print('testing SPMD (%d datasets) pullback qr decomposition for matrix A.shape = (%d,%d) up to first order'%(P,M,N))
print('--------------------------------------------------------------------------------------------------------------------')
print('')

# create symmetric matrix
A = utpm.UTPM(numpy.random.rand(D,P,M,N))
Expand All @@ -123,7 +123,7 @@
Qbar = utpm.UTPM(numpy.random.rand(D,P,M,N))
Rbar = utpm.UTPM(numpy.random.rand(D,P,N,N))
tic = time.time()
Abar = utpm.UTPM.bp_qr(Qbar, Rbar, A, Q, R)
Abar = utpm.UTPM.pb_qr(Qbar, Rbar, A, Q, R)
toc = time.time()
runtime_pullback = toc - tic

Expand All @@ -140,7 +140,7 @@
Qb = Qbar.data[0,0]
Qd = Q.data[1,0]

print ' (Abar, Adot) - (Rbar, Rdot) - (Qbar, Qdot) = %e'%( numpy.trace( numpy.dot(Ab.T, Ad)) - numpy.trace( numpy.dot(Rb.T, Rd) + numpy.dot(Qb.T, Qd)))
print(' (Abar, Adot) - (Rbar, Rdot) - (Qbar, Qdot) = %e'%( numpy.trace( numpy.dot(Ab.T, Ad)) - numpy.trace( numpy.dot(Rb.T, Rd) + numpy.dot(Qb.T, Qd))))



Expand All @@ -150,11 +150,11 @@


D,P,N = 2,1,8
print ''
print '--------------------------------------------------------------------------------------------------------------------'
print 'testing SPMD (%d datasets) pullback eigenvalue decomposition for matrix A.shape = (%d,%d) up to first order'%(P,N,N)
print '--------------------------------------------------------------------------------------------------------------------'
print ''
print('')
print('--------------------------------------------------------------------------------------------------------------------')
print('testing SPMD (%d datasets) pullback eigenvalue decomposition for matrix A.shape = (%d,%d) up to first order'%(P,N,N))
print('--------------------------------------------------------------------------------------------------------------------')
print('')

# create symmetric matrix
A = utpm.UTPM(numpy.random.rand(D,P,N,N))
Expand Down Expand Up @@ -186,4 +186,4 @@

Qb = Qbar.data[0,0]
Qd = Q.data[1,0]
print ' (Abar, Adot) - (Lbar, Ldot) - (Qbar, Qdot) = %e'%( numpy.trace( numpy.dot(Ab.T, Ad)) - numpy.trace( numpy.dot(Lb.T, Ld) + numpy.dot(Qb.T, Qd)))
print(' (Abar, Adot) - (Lbar, Ldot) - (Qbar, Qdot) = %e'%( numpy.trace( numpy.dot(Ab.T, Ad)) - numpy.trace( numpy.dot(Lb.T, Ld) + numpy.dot(Qb.T, Qd))))
4 changes: 2 additions & 2 deletions documentation/ICCS2010/accuracy_of_eigh_for_large_matrices.py
Expand Up @@ -14,8 +14,8 @@

l2,Q2 = UTPM.eigh(A)

print 'error between true and reconstructed eigenvalues'
print l.data[:,:,numpy.argsort(l.data[0,0])] - l2.data
print('error between true and reconstructed eigenvalues')
print(l.data[:,:,numpy.argsort(l.data[0,0])] - l2.data)



6 changes: 3 additions & 3 deletions documentation/ICCS2010/algopy_vs_pyadolc.py
Expand Up @@ -22,7 +22,7 @@
for nn,N in enumerate(N_list):
for nd,D in enumerate(D_list):

print 'running runtime tests for A.shape = (D,P,N,N) = %d, %d, %d, %d'%(D,P,N,N)
print('running runtime tests for A.shape = (D,P,N,N) = %d, %d, %d, %d'%(D,P,N,N))
A_data = numpy.random.rand(N,N,P,D)
Qbar_data = numpy.random.rand(1,N,N,P,D)
Rbar_data = numpy.random.rand(1,N,N,P,D)
Expand Down Expand Up @@ -92,9 +92,9 @@

push_forward_ratio = runtime_algopy_push_forward/runtime_pyadolc_push_forward
pullback_ratio = runtime_algopy_pullback/runtime_pyadolc_pullback
print 'relative runtime of the push forward: algopy/pyadolc =', push_forward_ratio
print('relative runtime of the push forward: algopy/pyadolc =', push_forward_ratio)

print 'relative runtime of the pullback: algopy/pyadolc =',pullback_ratio
print('relative runtime of the pullback: algopy/pyadolc =',pullback_ratio)

runtime_ratios_push_forward[nd,np,nn,r] = push_forward_ratio
runtime_ratios_pullback[nd,np,nn,r] = pullback_ratio
Expand Down
2 changes: 1 addition & 1 deletion documentation/ICCS2010/degenerated_eigenvalues.py
Expand Up @@ -51,7 +51,7 @@
# print l

# print 'check pushforward:'
print 'Q.T A Q - L =\n', UTPM.dot(Q.T, UTPM.dot(A,Q)) - L
print('Q.T A Q - L =\n', UTPM.dot(Q.T, UTPM.dot(A,Q)) - L)
# print 'Q.T Q - I =\n', UTPM.dot(Q.T, Q) - numpy.eye(N)
# print 'check pullback:'
# print 'error measure of the pullback = ', numpy.trace(numpy.dot(Abar.T, Adot)) - numpy.trace( numpy.dot(Lbar.T, Ldot) + numpy.dot(Qbar.T, Qdot))
Expand Down
2 changes: 1 addition & 1 deletion documentation/ICCS2010/odoe_example.py
Expand Up @@ -79,7 +79,7 @@
dPhidq = - 2* c * q0**-3

assert_almost_equal( dPhidq, qbar.data[1,0])
print 'symbolical - UTPM pullback = %e'%( numpy.abs(dPhidq - qbar.data[1,0]))
print('symbolical - UTPM pullback = %e'%( numpy.abs(dPhidq - qbar.data[1,0])))



Expand Down
Expand Up @@ -29,7 +29,7 @@ def alpha(beta):
res_2_list = []
res_3_list = []

betas = range(0,200,10)
betas = list(range(0,200,10))
for beta in betas:
a = alpha(beta)
A = a[0]*dot(x1,x1.T) + a[1]*dot(x2,x2.T) + a[2]*dot(x3,x3.T)
Expand Down
6 changes: 3 additions & 3 deletions documentation/examples/comparison_forward_reverse_mode.py
Expand Up @@ -55,7 +55,7 @@ def f(x,N):
J = y.x.data[1].T

# # checking against the analytical result
print 'J - A =\n', J - A.x.data[0,0]
print('J - A =\n', J - A.x.data[0,0])

# Now we want to compute the same Jacobian in the reverse mode of AD
# before we do that we have a look what the computational graph looks like:
Expand All @@ -80,11 +80,11 @@ def f(x,N):

# build Jacobian
J2 = numpy.vstack([J_row1.T, J_row2.T])
print 'J - J2 =\n', J - J2
print('J - J2 =\n', J - J2)

# one can also easiliy extract the Hessian which is here a (M,N,N)-tensor
# e.g. the hessian of y[1] is zero since y[1] is linear in x
print 'Hessian of y[1] w.r.t. x = \n',x.xbar.data[1,:,:,0]
print('Hessian of y[1] w.r.t. x = \n',x.xbar.data[1,:,:,0])



Expand Down
12 changes: 6 additions & 6 deletions documentation/examples/covariance_matrix_computation.py
Expand Up @@ -57,8 +57,8 @@
cg1.independentFunctionList = [J1, J2]
cg1.dependentFunctionList = [C]

print 'covariance matrix: C =\n',C
print 'check that Q2.T spans the nullspace of J2:\n', dot(J2,Q2.T)
print('covariance matrix: C =\n',C)
print('check that Q2.T spans the nullspace of J2:\n', dot(J2,Q2.T))

# METHOD 2: image space method (potentially numerically unstable)
cg2 = CGraph()
Expand All @@ -76,16 +76,16 @@
cg2.independentFunctionList = [J1, J2]
cg2.dependentFunctionList = [C2]

print 'covariance matrix: C =\n',C2
print 'difference between image and nullspace method:\n',C - C2
print('covariance matrix: C =\n',C2)
print('difference between image and nullspace method:\n',C - C2)

Cbar = UTPM(numpy.random.rand(D,P,N,N))

cg1.pullback([Cbar])

cg2.pullback([Cbar])
print 'J1\n',cg2.independentFunctionList[0].xbar - cg1.independentFunctionList[0].xbar
print 'J2\n',cg2.independentFunctionList[1].xbar - cg1.independentFunctionList[1].xbar
print('J1\n',cg2.independentFunctionList[0].xbar - cg1.independentFunctionList[0].xbar)
print('J2\n',cg2.independentFunctionList[1].xbar - cg1.independentFunctionList[1].xbar)



Expand Down
Expand Up @@ -12,21 +12,21 @@

D,P,N = 4,1,100

print 'create random matrix and perform QR decomposition '
print('create random matrix and perform QR decomposition ')
A = UTPM(numpy.random.rand(D,P,N,N))
Q,R = qr(A)

print 'define diagonal matrix'
print('define diagonal matrix')
l = UTPM(numpy.random.rand(D,P,N))
L = diag(l)

print 'transform to obtain non-diagonal matrix'
print('transform to obtain non-diagonal matrix')
A = dot(Q, dot(L,Q.T))

print 'reconstruct diagonal entries with eigh'
print('reconstruct diagonal entries with eigh')
l2,Q2 = eigh(A)

print 'absolute error true - reconstructed diagonal entries'
print l.data[:,:,numpy.argsort(l.data[0,0])] - l2.data
print('absolute error true - reconstructed diagonal entries')
print(l.data[:,:,numpy.argsort(l.data[0,0])] - l2.data)


0 comments on commit cefaa9a

Please sign in to comment.