Search code examples
c++stringmathginac

Who can tell me how to init a ex object with a string?


Does anyone use the GINAC? Can you tell me how to init an ex object with a string, or convert a string to ex?


Solution

  • If you have a string that contains the correct expression syntax, you can use the constructor documented here to turn it into an ex object.

    You need to supply a second argument, which must be a list (in the sense of a lst object) of symbols. This list must contain the user-defined symbols you use in the expression. If you don't use any user-defined symbols, use an empty list.

    Example with no user-defined symbols:

      using namespace std;
      using namespace GiNaC;
    
      ex myex("2+3",lst());      // Output will be '5'
    
      cout myex << endl;
    

    Example using two user-defined symbols:

      using namespace std;
      using namespace GiNaC;
    
      symbol x("x");
      symbol y("y");
      ex myex("x^3+y",lst(x,y));
    
      cout << myex + y << endl;     // Output will be '2*y+x^3'
    

    In the last example, you can see that the character 'y' in the input string "x^3+y" was indeed interpreted as the symbol y: myex + y is simplified to "2*y+x^3".