Skip to content

Igorantivirus/MathWorker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

110 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MathWorker

Русская версия

Description

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.

Dependencies

C++20.

Key Features

  • Single-header file
  • 🚀 Lightweight and fast
  • 🔗 No external dependencies
  • 🛠️ Fully customizable
  • <regex>-independent

Installation

  1. Clone the repository

    git clone https://github.com/Igorantivirus/MathWorker
  2. 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 to Project Name->Properties->C/C++->General
      In the Additional Include Directories field, paste the path to the "include" folder.
  3. Include the header file in your code

    #include "MathWorker/MathWorker.hpp"

Documentation

Work in progress.

Examples

  1. 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;
}
  1. 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

Road Map

  1. Memory management overhaul
  2. Add template support
  3. Rework function addition (to prevent errors)
  4. Output the parsed expression in LaTeX format
  5. Release

License

The MIT License

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors