# Let's make one grid function for evvery component of that vector u_grids = [] for col in range(n_eqs): u_grids.append(GridFunction([t_domain], u_data[:, col])) # And a symbols for each us = sp.symbols(' '.join(['u%d' % i for i in range(n_eqs)])) symbol_foo_map = dict(zip(us, u_grids)) # We are going to build a block structured system d u0/dt @ all_pts, next component lhs_streams = [ compile_stream((sp.Derivative(usi, t), ), symbol_foo_map) for usi in us ] # NOTE: this is reused for each block columns = polynomials(us, 2) print columns rhs_stream = compile_stream(columns, symbol_foo_map) # A subset of this data is now used for training; where we can eval deriv # safely train_indices = np.random.choice(np.arange(5, n_samples - 5), n_train) # Now build it lhs = np.hstack([[lhs_stream((point, )) for point in train_indices] for lhs_stream in lhs_streams]) # This is a block rhs = np.zeros((len(lhs) / n_eqs, len(columns))) row_indices = np.arange(rhs.shape[1]) for row, point in zip(rhs, train_indices):
# All data t_domain = np.linspace(0, T_end, n_samples) x_domain = np.linspace(-0.5, 0.5, n_samples) T, X = np.meshgrid(t_domain, x_domain, indexing='ij') u_data = u_exact(T, X) # To build the model we sample the data (in interior indices) to get # the derivative right. For the rhs we keep it simple u_grid = GridFunction([t_domain, x_domain], u_data) x_grid = Coordinate([t_domain, x_domain], 1) lhs_stream = compile_stream((sp.Derivative(u, t), ), {u: u_grid}) foos = polynomials([u], 3) derivatives = Dx(u, 1, [3])[1:] # Don't include 0;th # Combine these guys columns = [(1./sp.cos(x))*sp.Derivative(u, x)] + \ list(foos) + \ list(derivatives) + \ [reduce(operator.mul, cs) for cs in itertools.product(foos, derivatives)] rhs_stream = compile_stream(columns, {u: u_grid}) # A subset of this data is now used for training; where we can eval deriv # safely train_indices = map( tuple, np.random.choice(np.arange(5, n_samples - 5), (n_train, 2))) # Now build it
u, t = sp.symbols('u t') # Use exact solution to generate the data u_exact_expr = sp.exp(t) u_exact = sp.lambdify(t, u_exact_expr, 'numpy') # All data t_domain = np.linspace(0, T_end, n_samples) u_data = u_exact(t_domain) # To build the model we sample the data (in interior indices) to get # the derivative right. For the rhs we keep it simple u_grid = GridFunction([t_domain], u_data) lhs_stream = compile_stream((sp.Derivative(u, t), ), {u: u_grid}) poly = polynomials([u], 4) foos = (sp.sin(u), sp.cos(u), sp.exp(-u)) columns = list(poly) + list(foos) + [ reduce(operator.mul, cs) for cs in itertools.product(poly[1:], foos) ] rhs_stream = compile_stream(columns, {u: u_grid}) # A subset of this data is now used for training; where we can eval deriv # safely train_indices = np.random.choice(np.arange(5, n_samples - 5), n_train) # Now build it lhs = np.array([lhs_stream((point, )) for point in train_indices])