~daniele-bigoni/tensortoolbox/tt-docs

« back to all changes in this revision

Viewing changes to TensorToolbox/unittests/TestWTT_2.py

  • Committer: Daniele Bigoni
  • Date: 2015-01-19 11:10:20 UTC
  • Revision ID: dabi@dtu.dk-20150119111020-p0uckg4ab3xqzf47
merged with research

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This file is part of TensorToolbox.
 
3
#
 
4
# TensorToolbox is free software: you can redistribute it and/or modify
 
5
# it under the terms of the LGNU Lesser General Public License as published by
 
6
# the Free Software Foundation, either version 3 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# TensorToolbox is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# LGNU Lesser General Public License for more details.
 
13
#
 
14
# You should have received a copy of the LGNU Lesser General Public License
 
15
# along with TensorToolbox.  If not, see <http://www.gnu.org/licenses/>.
 
16
#
 
17
# DTU UQ Library
 
18
# Copyright (C) 2014-2015 The Technical University of Denmark
 
19
# Scientific Computing Section
 
20
# Department of Applied Mathematics and Computer Science
 
21
#
 
22
# Author: Daniele Bigoni
 
23
#
 
24
 
 
25
import logging
 
26
import sys
 
27
 
 
28
from aux import bcolors, print_ok, print_fail, print_summary
 
29
 
 
30
def run(maxprocs, PLOTTING=False, loglev=logging.WARNING):
 
31
 
 
32
    logging.basicConfig(level=loglev)
 
33
 
 
34
    import numpy as np
 
35
    import numpy.linalg as npla
 
36
    import itertools
 
37
    import time
 
38
 
 
39
    import TensorToolbox as DT
 
40
    import TensorToolbox.multilinalg as mla
 
41
 
 
42
    if PLOTTING:
 
43
        from matplotlib import pyplot as plt
 
44
 
 
45
    nsucc = 0
 
46
    nfail = 0
 
47
 
 
48
    #####################################################################################
 
49
    # Test matrix-vector product by computing the matrix-vector product
 
50
    #####################################################################################
 
51
    span = np.array([0.,1.])
 
52
    d = 2
 
53
    N = 16
 
54
    h = 1/float(N-1)
 
55
    eps = 1e-10
 
56
 
 
57
    # sys.stdout.write("Matrix-vector: Laplace  N=%4d   , d=%3d      [START] \n" % (N,d))
 
58
    # sys.stdout.flush()
 
59
 
 
60
    # Construct 2D Laplace (with 2nd order finite diff)
 
61
    D = -1./h**2. * ( np.diag(np.ones((N-1)),-1) + np.diag(np.ones((N-1)),1) + np.diag(-2.*np.ones((N)),0) )
 
62
    D[0,0:3] = np.array([1./(3.*h**2.),-2./(3.*h**2.),1./(3.*h**2.)])
 
63
    D[-1,-3:] = -np.array([1./(3.*h**2.),-2./(3.*h**2.),1./(3.*h**2.)])
 
64
    I = np.eye(N)
 
65
    FULL_LAP = np.zeros((N**d,N**d))
 
66
    for i in range(d):
 
67
        tmp = np.array([[1.]])
 
68
        for j in range(d):
 
69
            if i != j: tmp = np.kron(tmp,I)
 
70
            else: tmp = np.kron(tmp,D)
 
71
        FULL_LAP += tmp
 
72
 
 
73
    # Construction of TT Laplace operator
 
74
    CPtmp = []
 
75
    # D = -1./h**2. * ( np.diag(np.ones((N-1)),-1) + np.diag(np.ones((N-1)),1) + np.diag(-2.*np.ones((N)),0) )
 
76
    # I = np.eye(N)
 
77
    D_flat = D.flatten()
 
78
    I_flat = I.flatten()
 
79
    for i in range(d):
 
80
        CPi = np.empty((d,N**2))
 
81
        for alpha in range(d):
 
82
            if i != alpha:
 
83
                CPi[alpha,:] = I_flat
 
84
            else:
 
85
                CPi[alpha,:] = D_flat
 
86
        CPtmp.append(CPi)
 
87
 
 
88
    CP_lap = DT.Candecomp(CPtmp)
 
89
    TT_LAP = DT.TTmat(CP_lap,nrows=N,ncols=N)
 
90
    TT_LAP.build(eps)
 
91
    TT_LAP.rounding(eps)
 
92
    CPtmp = None
 
93
    CP_lap = None
 
94
 
 
95
    # Construct input vector
 
96
    X = np.linspace(span[0],span[1],N)
 
97
    SIN = np.sin(X)
 
98
    I = np.ones((N))
 
99
    FULL_SIN = np.zeros((N**d))
 
100
    for i in range(d):
 
101
        tmp = np.array([1.])
 
102
        for j in range(d):
 
103
            if i != j: tmp = np.kron(tmp,I)
 
104
            else: tmp = np.kron(tmp,SIN)
 
105
        FULL_SIN += tmp
 
106
 
 
107
    if PLOTTING:
 
108
        from mpl_toolkits.mplot3d import Axes3D
 
109
        from matplotlib import cm
 
110
        (XX,YY) = np.meshgrid(X,X)
 
111
        fig = plt.figure()
 
112
        if d == 2:
 
113
            # Plot function
 
114
            ax = fig.add_subplot(221,projection='3d')
 
115
            ax.plot_surface(XX,YY,FULL_SIN.reshape((N,N)),rstride=1, cstride=1, cmap=cm.coolwarm,
 
116
                            linewidth=0, antialiased=False)
 
117
            plt.show(block=False)
 
118
 
 
119
    # Construct TT input vector
 
120
    CPtmp = []
 
121
    for i in range(d):
 
122
        CPi = np.empty((d,N))
 
123
        for alpha in range(d):
 
124
            if i != alpha: CPi[alpha,:] = I
 
125
            else: CPi[alpha,:] = SIN
 
126
        CPtmp.append(CPi)
 
127
 
 
128
    CP_SIN = DT.Candecomp(CPtmp)
 
129
    W = [np.ones(N,dtype=float)/float(N) for i in range(d)]
 
130
    TT_SIN = DT.WTTvec(CP_SIN,W)
 
131
    TT_SIN.build()
 
132
    TT_SIN.rounding(eps)
 
133
 
 
134
    if PLOTTING and d == 2:
 
135
        # Plot function
 
136
        ax = fig.add_subplot(222,projection='3d')
 
137
        ax.plot_surface(XX,YY,TT_SIN.to_tensor(),rstride=1, cstride=1, cmap=cm.coolwarm,
 
138
                        linewidth=0, antialiased=False)
 
139
        plt.show(block=False)
 
140
 
 
141
    # Apply full laplacian
 
142
    FULL_RES = np.dot(FULL_LAP,FULL_SIN)
 
143
    if PLOTTING and d == 2:
 
144
        # Plot function
 
145
        ax = fig.add_subplot(223,projection='3d')
 
146
        ax.plot_surface(XX,YY,FULL_RES.reshape((N,N)),rstride=1, cstride=1, cmap=cm.coolwarm,
 
147
                        linewidth=0, antialiased=False)
 
148
        plt.show(block=False)
 
149
 
 
150
    # Apply TT laplacian
 
151
    TT_RES = mla.dot(TT_LAP,TT_SIN)
 
152
    if PLOTTING and d == 2:
 
153
        # Plot function
 
154
        ax = fig.add_subplot(224,projection='3d')
 
155
        ax.plot_surface(XX,YY,TT_RES.to_tensor(),rstride=1, cstride=1, cmap=cm.coolwarm,
 
156
                        linewidth=0, antialiased=False)
 
157
        plt.show(block=False)
 
158
 
 
159
    # Check results
 
160
    if not np.allclose(FULL_RES,TT_RES.to_tensor().flatten()):
 
161
        print_fail("2.1 Matrix-vector: Laplace  N=%4d   , d=%3d" % (N,d))
 
162
        nfail += 1
 
163
    else:
 
164
        print_ok("2.1 Matrix-vector: Laplace  N=%4d   , d=%3d" % (N,d))
 
165
        nsucc += 1
 
166
 
 
167
    #####################################################################################
 
168
    # Test matrix-vector product by computing the matrix-vector product of randomly generated input
 
169
    #####################################################################################
 
170
    span = np.array([0.,1.])
 
171
    d = 3
 
172
    nrows = [16,20,24]
 
173
    ncols = [16,12,14]
 
174
    if isinstance(nrows,int): nrows = [nrows for i in range(d)]
 
175
    if isinstance(ncols,int): ncols = [ncols for i in range(d)]
 
176
    eps = 1e-10
 
177
 
 
178
    # sys.stdout.write("Matrix-vector: Random\n  nrows=[%s],\n  ncols=[%s],  d=%3d      [START] \n" % (','.join(map(str,nrows)),','.join(map(str,ncols)),d))
 
179
    # sys.stdout.flush()
 
180
 
 
181
    # Construction of TT random matrix
 
182
    TT_RAND = DT.randmat(d,nrows,ncols)
 
183
 
 
184
    # Construct FULL random tensor
 
185
    FULL_RAND = TT_RAND.to_tensor()
 
186
    import itertools
 
187
    rowcol = list(itertools.chain(*[[ri,ci] for (ri,ci) in zip(nrows,ncols)]))
 
188
    FULL_RAND = np.reshape(FULL_RAND,rowcol)
 
189
    idxswap = range(0,2*d,2)
 
190
    idxswap.extend(range(1,2*d,2))
 
191
    FULL_RAND = np.transpose(FULL_RAND,axes=idxswap)
 
192
    FULL_RAND = np.reshape(FULL_RAND,(np.prod(nrows),np.prod(ncols)))
 
193
 
 
194
    # Construct TT random vector
 
195
    TT_VEC = DT.randvec(d,ncols)
 
196
 
 
197
    # Construct FULL random vector
 
198
    FULL_VEC = TT_VEC.to_tensor().flatten()
 
199
 
 
200
    # Apply TT
 
201
    TT_RES = mla.dot(TT_RAND,TT_VEC)
 
202
 
 
203
    # Apply FULL
 
204
    FULL_RES = np.dot(FULL_RAND,FULL_VEC)
 
205
 
 
206
    # Check results
 
207
    if not np.allclose(FULL_RES,TT_RES.to_tensor().flatten()):
 
208
        print_fail("2.2 Matrix-vector: Random  N=%4d   , d=%3d" % (N,d),'')
 
209
        nfail += 1
 
210
    else:
 
211
        print_ok("2.2 Matrix-vector: Random  N=%4d   , d=%3d" % (N,d))
 
212
        nsucc += 1
 
213
 
 
214
    print_summary("TT Matrix-Vector", nsucc, nfail)
 
215
    
 
216
    return (nsucc,nfail)
 
217
 
 
218
if __name__ == "__main__":
 
219
    # Number of processors to be used, defined as an additional arguement 
 
220
    # $ python TestTT.py N
 
221
    # Mind that the program in this case will run slower than the non-parallel case
 
222
    # due to the overhead for the creation and deletion of threads.
 
223
    if len(sys.argv) == 2:
 
224
        maxprocs = int(sys.argv[1])
 
225
    else:
 
226
        maxprocs = None
 
227
 
 
228
    run(maxprocs,PLOTTING=True, loglev=logging.INFO)