EQ_EDIT is a simple C++ code that evaluate a mathematical expression with variables and some standard mathematical functions given as a string. The variables and functions used in the equation are defined and their values are passed to the function that evaluates the equation. The code is presented in a single header file EQ_EDIT.hpp which contains the EQ_EDIT class.
The code allows the following mathematical operators to be used in the equation:
+- Addition-- Subtraction*- Multiplication/- Division^- Power()- Parentheses
the following mathematical functions to be used in the equation:
abs- Absolute valueint- Integer partfrac- Fractional partrond- Roundlog- Logarithm (base 10)ln- Natural logarithmexp- Exponentialsin- Sinecos- Cosinetan- Tangentsh- Hyperbolic sinech- Hyperbolic cosineth- Hyperbolic tangentasin- Arcsineacos- Arccosineatan- Arctangentash- Hyperbolic arcsineach- Hyperbolic arccosineath- Hyperbolic arctangent
and the following mathematical constants to be used in the equation:
pi- π numbere- Euler's number
It is possible to add new functions and constants to the code by modifying the EQ_EDIT.hpp file.
The application uses the EQ_EDIT class to evaluate mathematical equations. The variables used in the equation are defined and their values are passed to the Value method of the func_eq object. This method evaluates the equation and returns the calculated value.
Another way to use the code is to define the variables and the equation using the setVar and setEquation methods of the EQ_EDIT class. The getValue method of the func_eq object, without any arguments, is then called to evaluate the equation.
To use the code, you need to include the EQ_EDIT.hpp header file in your code. You can then create an instance of the EQ_EDIT class and use it to evaluate mathematical equations. See the code examples below for more details.
A first example is presented in the example1.cpp file.
#include <iostream>
#include "EQ_EDIT.hpp"
using namespace std;
int main()
{
EQ_EDIT func_eq;
double x = 10;
double beta = 1.5;
string equation = "-(x^2/2)+20+2*beta+alpha";
double value = func_eq.getValue(true,equation,{{"x",x},{"beta",beta},{"alpha",0.5}});
cout<<"----------------------------------"<<endl;
cout<<" EXAMPLE 1 "<<endl;
cout<<"----------------------------------"<<endl;
cout << "Equation exp.: " << func_eq.getEquation() << endl;
cout << "Number of variables: " << func_eq.getNVar() << endl;
cout << "Value: " << value << endl;
return 0;
}Here is a description of each line of code:
#include "EQ_EDIT.hpp"Includes the EQ_EDIT.hpp header file which contains the EQ_EDIT class.
EQ_EDIT func_eq;Creates an instance of the EQ_EDIT class which is used to evaluate mathematical equations.
double x = 10;
double beta = 1.5;
string equation = "-(x^2/2)+20+2*beta+alpha";Defines the variables x, beta, and alpha and the equation to evaluate.
double value = func_eq.Value(true,equation,{{"x",x},{"beta",beta},{"alpha",0.5}});Calls the Value method of the func_eq object to evaluate the equation. The equation expression, the variables, and their values are passed as arguments to the Value method. The first argument of the Value method is a boolean that indicates if the equation expression should be calculated or not. If this argument is set to true, the Value method returns the calculated value of the equation.If this argument is set to false, it returns 0.
cout << "Equation: " << func_eq.getEquation() << endl;
cout << "Number of variables: " << func_eq.getNVar() << endl;
cout << "Value: " << value << endl;Displays the equation, the number of variables in the equation, and its calculated value.
Another example is presented in the example2.cpp file.
#include <iostream>
#include "EQ_EDIT.hpp"
using namespace std;
int main()
{
EQ_EDIT func_eq;
double x = 10;
double beta = 1.5;
func_eq.setVar({{"x",x},{"beta",beta},{"alpha",M_PI/4}});
func_eq.setEquation("-(x^2/2)+20+2*beta+sin(alpha)");
double value = func_eq.getValue();
cout<<"----------------------------------"<<endl;
cout<<" EXAMPLE 2 "<<endl;
cout<<"----------------------------------"<<endl;
cout << "Equation exp.: " << func_eq.getEquation() << endl;
cout << "Number of variables: " << func_eq.getNVar() << endl;
cout << "Value: " << value << endl;
return 0;
}In this example the variables are defined and the equation is set using the setVar and setEquation methods of the EQ_EDIT class:
func_eq.setVar({{"x",x},{"beta",beta},{"alpha",M_PI/4}});
func_eq.setEquation("-(x^2/2)+20+2*beta+sin(alpha)");
double value = func_eq.getValue();Sets the values of the variables and the equation to evaluate. The getValue method of the func_eq object, without any arguments, is then called to evaluate the equation.
This project is licensed under the GPL-3 license.
Unless you explicitly state otherwise, any contribution intentionally submitted by you for inclusion in this project shall be licensed as above, without any additional terms or conditions.