What good libraries are there for solving a system of non-linear equations in C++?

后端 未结 8 1573
情书的邮戳
情书的邮戳 2021-02-06 01:12

In a C++ application I\'m coding, I need to solve a system of non-linear equations (N equations, N unknowns).

The systems I\'m solving will be rather sm

8条回答
  •  星月不相逢
    2021-02-06 02:00

    Microsoft Z3 https://github.com/Z3Prover/z3/blob/master/examples/c%2B%2B/example.cpp

    also consider omnn::math: https://github.com/ohhmm/openmind/blob/master/omnn/math/test/08_System.cpp

    Lets say system of equations is like this:

    (x-a1)^2 + (y-b1)^2 = c1
    (x-a2)^2 + (y-b2)^2 = c2
    

    Then you have couple options:

    Valuable a1, a2, b1, b2; // init with values
    
    System sys;
    Variable x,y;
    sys << (x-a1)^2 + (y-b1)^2 - c1; // addin an equation as an equality to 0
    sys << (x-a2)^2 + (y-b2)^2 - c2;
    
    for(auto& solution : sys.Solve(x))
            std::cout << solution;
    

    alternative way is to make single equation (see why):

    ((x-a1)^2 + (y-b1)^2 - c1)^2 + ((x-a2)^2 + (y-b2)^2 - c2)^2 = 0

    Variable x,y;
    Valuable a1, a2, b1, b2; // init with values
    auto eq = ((x-a1)^2 + (y-b1)^2 - c1)^2 + ((x-a2)^2 + (y-b2)^2 - c2)^2;
    eq.SetView(Valuable::View::Equation);  // optional: equation optimizations
    // get y function:
    auto fn = eq(y);
    
    // show
    std::cout << fn << std::endl;
    
    // evaluate
    auto evaluate = fn;
    evaluate.eval(x, 10);
    evaluate.optimize(); // calculate
    // show calculated value at x=10:
    std::cout << evaluate << std::endl;
    

提交回复
热议问题