/scnt-exs

General purpose solver of textual expressions in C++

Primary LanguageC++MIT LicenseMIT

SciNumTools v3
Expression Solver (EXS)

General purpose solver of textual expressions in C++

Introduction

This project is a reimplementation of an already existing Expression Solver into the C++ language. The original version of the code, written in Python, is available in the scinumtools.solver repository. In the future, this version is supposed to replace the original and further improve the performance of the scinumtools package.

Setup

For the purpose of developing and debugging, we created a simple setup.sh shell script that implements the most used routines: cleaning, building, testing, and running of the code. For more instructions on how to use this script, refer to its help section:

./setup.sh -h

Example of use

The Equation Solver is implemented in C++ as a header file template library. The main class Solver takes as a template argument the Atom class. A standard implementation of an Atom is provided in the code, nevertheless, it can be easily modified by the user. Source code of the following simple example can be found in the examples/DefaultSolver directory.

#include "../../src/exs.h"

using namespace exs;

int main() {
  Solver<Atom> solver;
  Atom atom = solver.solve("23 * 34.5 + 4");
  atom.print();
}

This example can be compiled and run using the setup.sh script mentioned above

./setup.sh -c -b -r DefaultSolver

and will print 797.5 into the terminal.

The list of all default operations and their order is initialized in the Solver class. However, individual operators and their order can be easily modified, as in the example below.

// modifying default operator symbols
OperatorList<Atom> operators;
operators.append(NOT_OPERATOR, std::make_shared<OperatorNot<Atom>>("N"));
operators.append(AND_OPERATOR, std::make_shared<OperatorAnd<Atom>>("A"));
operators.append(OR_OPERATOR,  std::make_shared<OperatorOr<Atom>>("O"));

// changing default operation steps
StepList steps;
steps.append(BINARY_OPERATION, {OR_OPERATOR});
steps.append(BINARY_OPERATION, {AND_OPERATOR});
steps.append(UNARY_OPERATION,  {NOT_OPERATOR});

Solver<Atom> solver(operators, steps);
Atom atom = solver.solve("N false A false O true");
atom.print();

The corresponding example can be compiled using the following command.

./setup.sh -c -b -r ModifiedSolver

More comprehensive examples (e.g. custom Atom and operator classes) are provided in the example directory, and additional code tests are implemented in the tests directory.