A C++ library for processing mathematical expressions with a fully customizable signature. It's capable of parsing mathematical expressions from a std::string. You can add new constants, functions, and operators to the signature at runtime.
C++20.
- ⚡ Single-header file
- 🚀 Lightweight and fast
- 🔗 No external dependencies
- 🛠️ Fully customizable
- ✅
<regex>-independent
-
Clone the repository
git clone https://github.com/Igorantivirus/MathWorker
-
Add the path to the include folder in your project settings
Provide your project with the path to the include folder from the cloned repository. For example, in VS22, you can do this as follows:- Visual Studio 2022
Go toProject Name->Properties->C/C++->General
In theAdditional Include Directoriesfield, paste the path to the "include" folder.
- Visual Studio 2022
-
Include the header file in your code
#include "MathWorker/MathWorker.hpp"
Work in progress.
- Example 1: Console Calculator
#include <iostream>
#include <MathWorker/MathWorker.hpp>
using namespace mathWorker;
int main()
{
// Basic algebraic signature and constants
Signature signature = generator::mathSignature();
VariableContext constants = generator::baseConstants();
MathParser parser(signature, std::make_unique<BaseTokenizer>(signature));
FunctionConnector connector;
std::string s;
while (true)
{
try
{
std::getline(std::cin, s);
std::cout << parser.parse(s)->replace(constants)->calculate(signature)->toString() << '\n';
}
catch (const ParseException& e)
{
std::cout << "Error: " << e.what() << ", type of error: " << static_cast<int>(e.type()) << '\n';
}
catch (...)
{
std::cout << "Unknown error" << '\n';
}
}
return 0;
}- Example 2: Custom Signature
#include <iostream>
#include <MathWorker/MathWorker.hpp>
using namespace mathWorker;
int main()
{
Signature signature;
VariableContext constants;
MathParser parser(signature, std::make_unique<BaseTokenizer>(signature));
// Custom operator
signature.addOperator("@", [](const std::vector<MathNodeP>& params)->MathNodeP
{
ComplexType left = params[0]->getNumberForced();
ComplexType right = params[1]->getNumberForced();
ComplexType res = left + right + left * right;
return std::make_unique<ValueNode>(res);
}, 0);
FunctionConnector connector;
// Adding a function (one of 3 ways)
connector.addFunction(signature, "f(x)=2@x@17");
std::string s = "f(1)@2";
std::cout << parser.parse(s)->replace(constants)->calculate(signature)->toString() << '\n';
return 0;
}Output: 323
- Memory management overhaul
- Add template support
- Rework function addition (to prevent errors)
- Output the parsed expression in LaTeX format
- Release
The MIT License