~daniele-bigoni/tensortoolbox/tt-docs

« back to all changes in this revision

Viewing changes to Development/Examples/TensorWrapper/WrapperExample.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
#
 
26
# Construct two nested grids to exemplify the capabilities of the
 
27
# TensorWrapper.
 
28
#
 
29
 
 
30
import numpy as np
 
31
import itertools
 
32
import matplotlib.pyplot as plt
 
33
import TensorToolbox as TT
 
34
 
 
35
d = 2
 
36
x_fine = np.linspace(-1,1,7)
 
37
params = {'k': 1.}
 
38
 
 
39
def f(X,params):
 
40
    return np.max(X) * params['k']
 
41
 
 
42
TW = TT.TensorWrapper( f, [ x_fine ]*d, params, dtype=float )
 
43
 
 
44
x_coarse = np.linspace(-1,1,4)
 
45
TW.set_view( 'coarse', [x_coarse]*d )
 
46
 
 
47
TW.set_active_view('full')
 
48
TW[1,:]
 
49
TW[:,2]
 
50
TW.set_active_view('coarse')
 
51
TW[2,:]
 
52
 
 
53
grid_coarse = np.array( list( itertools.product( *([x_coarse]*d) ) ) )
 
54
grid_fine = np.array( list( itertools.product( *([x_fine]*d) ) ) )
 
55
 
 
56
fill_idxs = np.array(TW.get_fill_idxs())
 
57
grid_fine_fill = np.vstack( [x_fine[fill_idxs[:,0]], x_fine[fill_idxs[:,1]]] ).T
 
58
grid_coarse_fill = np.array( [ grid_fine_fill[i,:] for i in xrange(TW.get_fill_level()) if (grid_fine_fill[i,0] in x_coarse and grid_fine_fill[i,1] in x_coarse) ] )
 
59
 
 
60
fig = plt.figure(figsize=(16,5))
 
61
ax = fig.add_subplot(131)
 
62
ax.plot( grid_fine[:,0], grid_fine[:,1], 'o', markeredgecolor='k', markeredgewidth=1.5, markerfacecolor='w', label='global' )
 
63
ax.plot( grid_fine_fill[:,0], grid_fine_fill[:,1], 'o', markeredgecolor='k', markeredgewidth=1.5, markerfacecolor='k', label='eval' )
 
64
plt.xlim([-1.1,1.1])
 
65
plt.ylim([-1.1,1.1])
 
66
plt.legend()
 
67
ax = fig.add_subplot(132)
 
68
ax.plot( grid_fine[:,0], grid_fine[:,1], 'o', markeredgecolor='b', markeredgewidth=1.5, markerfacecolor='w', label='full')
 
69
ax.plot( grid_fine_fill[:,0], grid_fine_fill[:,1], 'o', markeredgecolor='k', markeredgewidth=1.5, markerfacecolor='k', label='eval' )
 
70
plt.xlim([-1.1,1.1])
 
71
plt.ylim([-1.1,1.1])
 
72
plt.legend()
 
73
ax = fig.add_subplot(133)
 
74
ax.plot( grid_coarse[:,0], grid_coarse[:,1], 'o', markeredgecolor='r', markeredgewidth=1.5, markerfacecolor='w', label='coarse')
 
75
ax.plot( grid_coarse_fill[:,0], grid_coarse_fill[:,1], 'o', markeredgecolor='k', markeredgewidth=1.5, markerfacecolor='k', label='eval' )
 
76
plt.xlim([-1.1,1.1])
 
77
plt.ylim([-1.1,1.1])
 
78
plt.legend()
 
79
plt.tight_layout()
 
80
plt.show(False)
 
81
 
 
82
#
 
83
# Quantics extension of the full view
 
84
#
 
85
TW.set_active_view('full')
 
86
 
 
87
plt.figure(figsize=(12,5))
 
88
plt.subplot(1,2,1)
 
89
plt.imshow(TW[:,:],interpolation='none')
 
90
plt.title('No extension')
 
91
 
 
92
TW.set_active_view('full')
 
93
TW.set_Q(2)
 
94
 
 
95
plt.subplot(1,2,2)
 
96
plt.imshow(TW[:,:],interpolation='none')
 
97
plt.title('Quantics extension')
 
98
plt.tight_layout()
 
99
plt.show(False)
 
100
 
 
101
#
 
102
# Reshape
 
103
#
 
104
TW.set_active_view('full')
 
105
TW.reshape((4,16))
 
106
plt.figure(figsize=(12,4))
 
107
plt.imshow(TW[:,:], interpolation='none')
 
108
plt.title('Reshaped')
 
109
plt.tight_layout()
 
110
plt.show(False)
 
111
 
 
112
 
 
113
TW.reshape([2,2,2,2,2,2])