Giter VIP home page Giter VIP logo

Comments (8)

JohanMabille avatar JohanMabille commented on May 18, 2024

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;

from xtensor.

sirinath avatar sirinath commented on May 18, 2024

What happens when x, y is not in scope?

from xtensor.

JohanMabille avatar JohanMabille commented on May 18, 2024

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 ?

from xtensor.

sirinath avatar sirinath commented on May 18, 2024

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, .....

from xtensor.

JohanMabille avatar JohanMabille commented on May 18, 2024

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.

from xtensor.

sirinath avatar sirinath commented on May 18, 2024

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

from xtensor.

JohanMabille avatar JohanMabille commented on May 18, 2024

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.

from xtensor.

SylvainCorlay avatar SylvainCorlay commented on May 18, 2024

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

from xtensor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.