Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Computational Data Flow Expression / DAG Builder #61

Closed
sirinath opened this issue Dec 23, 2016 · 8 comments
Closed

Computational Data Flow Expression / DAG Builder #61

sirinath opened this issue Dec 23, 2016 · 8 comments

Comments

@sirinath
Copy link

I want to be able to do something like the following

xt::xexp<double> exp_res1 = xt::xvar("x") + xt::xvar("y") + xt::xconst(3);
xt::xexp<double> exp_res2 = exp_res1 /  xt::xconst(2);

xt::xarray<double> res1 = exp_res1.set("x", arr1).set("y", arr2).eval();
xt::xarray<double> res2 = exp_res1.set("y", arr3).eval();

xt::xarray<double> res3 = exp_res2.eval();

Here I am reusing the expression and also doing the evaluation when needed.

@JohanMabille
Copy link
Member

JohanMabille commented Dec 24, 2016

You can already reuse an expression and evaluate it when needed, however named variables are not supported:

xt::array<double> x, y;
auto exp_res1 = x + y + 3; // exp_res1 type is xfunction<....>, not evaluated
auto exp_res2 = exp_res1 / 2; // exp_res2 type is xfunction<...>, not evaluated

// init x and y
x = arr1;
y = arr2;
// Forces evaluation of exp_res1 in res1. exp_res1 remains unchanged.
xt::xarray<double> res1 = exp_res1;

// changes y
y = arr3;
// Forces evaluation of exp_res1 (with new y) in res2. exp_res1 remains unchanged.
xt::xarray<double> res2 = exp_res1;

// Forces evaluation of exp_res1 (with new y) in res2. exp_res1 remains unchanged.
xt::xarray<double> res3 = exp_res2;

@sirinath
Copy link
Author

What happens when x, y is not in scope?

@JohanMabille
Copy link
Member

xfunction doesn't provide any way to retrieve its arguments, so you have to store references to x and y somewhere, that you can reach from the code that modifies them. I guess it can be problematic if you have sophisticated expressions with dozens of variables.

@SylvainCorlay any thought on implementing named variables ?

@sirinath
Copy link
Author

sirinath commented Dec 24, 2016

Also something like

xt::xarray<double> res = xt::xfunction("x + y + 3").setp("x", arr1).setp("y", arr2).eval()

res = xt::xfunction("f(x, y) = x + y + 3").setp("x", arr1).setp("y", arr2).eval()
res = xt::xfunction("f(x, y) = x + y + 3").setp(0, arr1).setp(1, arr2).eval()

res = xt::xfunction("f(x, y) = g(x) + h(y) + 3").setf("g", f1).setf("h", f2).setp("x", arr1).setp("y", arr2).eval()

Also have some predefined standard functions

xt::sin, xt::ln, xt::reg, .....

@JohanMabille
Copy link
Member

JohanMabille commented Dec 24, 2016

You're not supposed to call xfunction constructor by yourself.
So this code:

xt::array<double> res = xt:xfunction("x + y + 3")

should definitely be replaced by:

xt::array<double> res = x + y + 3;

or

auto exp_res = x + y + 3;

if you want to keep the expression for a deferred evaluation.

Besides, building xfunction based on string expression is not supported. And won't. Never. This library is a computation library, not a parser library. If you need such a feature, feel free to implement it as an aside project, but xtensor is not the right place where to do it.

Almost all the standard mathematical functions defined in the C++ standard library have their counterpart in xtensor. Check the xmath.hpp header and the doc. If you need one that is missing, please open another issue dedicated to this, so we don't mix up unrelated features.

@sirinath
Copy link
Author

As mentioned before, what if I do not have x and y when I set up the expression.

@JohanMabille
Copy link
Member

JohanMabille commented Dec 24, 2016

That's the same problem as the first one. The solution is to provide a way to retrieve the arguments of an xfunction (either with indexed or named getters), not a constructor that parses a string.

@SylvainCorlay
Copy link
Member

Thanks for this @sirinath I am closing this since it is out of scope at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants