oseledets / TT-Toolbox

The git repository for the TT-Toolbox
Other
189 stars 73 forks source link

amen_cross() for multivariate function on refined grids #56

Closed temaltba closed 3 years ago

temaltba commented 3 years ago

Could you provide an example or two of using amen_cross() for multivariate/high-dimensional function evaluation. I'm new to the toolbox (and Matlab) and I can't quite figure out the right syntax.

I was able to replicate the 1D function example from the quick_start guide, i.e. evaluating f(x) = sqrt(x) on a refined grid:

d = 70; n = 2^d; h = 1/(n-1); x = tt_ones(2,d)*h; fun = @(x) sqrt(x); tt = amen_cross({x},fun,1e-10,'y0',x,'nswp',20); This gave me the same result as funcrs2() with very small relative error.

I also was able to calculate the sum of two tensors via

d = 70; n = 2^d; h = 1/(n-1); x1 = tt_ones(2,d)h; fun = @(x) x[1;1]; tt = amen_cross({x1,x1},fun,1e-10,'y0',x1,'nswp',20); This gave the save result (in norm) as doing (x1 + x1).

However, I'm having issues/errors when things are multivariate and nonlinear and/or multiplied. For example, could you provide the syntax for computing the function f(x,y,z) = z*exp(x/z) + y using amen_cross(), possibly on both coarse and refined grids? I just made this function up and chose it since it has nonlinear composition, addition, multiplication, and division.

dolgov commented 3 years ago

When the first argument to amen_cross is a cell array of tt_tensors, the function in the second argument is passed a matrix, where each column contains samples from the corresponding tt_tensor from the cell. So if we call something like tt = amen_cross({x,y,z},fun,1e-10,'y0',x,'nswp',20); the handle fun is still passed exactly one argument, which is a matrix S = [x(J), y(J), z(J)]. (Where J are some indices.) To assemble zexp(x/z) + y the handle should look like fun = @(S) S(:,3) . exp( S(:,1) ./ S(:,3) ) + S(:,2); % note that all products and divisions are elementwise!

Since TT-Toolbox works on discrete tensors, there is no such things as "coarse" or "refined" grids. If course, you can associate those to particular tensors in your particular problem, but general interfaces to TT-Toolbox functions should be the same.

There are helper functions to construct tensors of grid points though, you may look at the Matlab's own function meshgrid and the TT-Toolbox function tt_meshgrid_vert. The latter returns a cell array of tt_tensors compatible with amen_cross.

temaltba commented 3 years ago

Great, this is very helpful. Thanks!