{"id":16890881,"url":"https://github.com/alandefreitas/sympp","last_synced_at":"2025-08-09T13:05:33.922Z","repository":{"id":139490023,"uuid":"299464590","full_name":"alandefreitas/sympp","owner":"alandefreitas","description":"SymPP: A Symbolic Library that compiles itself","archived":false,"fork":false,"pushed_at":"2020-11-23T20:55:33.000Z","size":2330,"stargazers_count":13,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T09:21:45.646Z","etag":null,"topics":["compile","mathematical-modelling","numerical-analysis","optimization","symbolic","symbolic-computation"],"latest_commit_sha":null,"homepage":"https://alandefreitas.github.io/sympp/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alandefreitas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-29T00:35:52.000Z","updated_at":"2024-10-19T20:30:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"6b6030c7-9f62-4a48-abf6-101928289436","html_url":"https://github.com/alandefreitas/sympp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alandefreitas%2Fsympp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alandefreitas%2Fsympp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alandefreitas%2Fsympp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alandefreitas%2Fsympp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alandefreitas","download_url":"https://codeload.github.com/alandefreitas/sympp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248405049,"owners_count":21097885,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compile","mathematical-modelling","numerical-analysis","optimization","symbolic","symbolic-computation"],"created_at":"2024-10-13T17:04:47.674Z","updated_at":"2025-04-11T13:10:33.474Z","avatar_url":"https://github.com/alandefreitas.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sym\u003csup\u003epp\u003c/sup\u003e\n\n[![Build Status](https://img.shields.io/github/workflow/status/alandefreitas/sympp/SymPP?event=push\u0026label=Build\u0026logo=Github-Actions)](https://github.com/alandefreitas/sympp/actions?query=workflow%3ASymPP+event%3Apush)\n[![Website](https://img.shields.io/website-up-down-green-red/http/alandefreitas.github.io/sympp.svg?label=Documentation)](https://alandefreitas.github.io/sympp/)\n\u003c!-- [![Latest Release](https://img.shields.io/github/release/alandefreitas/sympp.svg?label=Download)](https://GitHub.com/alandefreitas/sympp/releases/) --\u003e\n\n![Math](https://www.wellesley.edu/sites/default/files/styles/news_refresh_hero/public/assets/dailyshot/ds_461390782.jpg?itok=jr0Buv1t)\n\n\u003cdiv style=\"text-align:center\"\u003e\n\u003cim src=\"documentation/im/front2d_b.svg\" alt=\"\" data-canonical-src=\"2-dimensional front\" width=\"800\" height=\"200\" /\u003e\n\u003c/div\u003e\n\nSymbolic computing deals with algorithms that manipulate mathematical expressions. They are useful for mathematical experiments, scientific computation, optimization, improving numerical methods, reducing approximation errors, and as an alternative when numerical analysis fails. However, algebraic manipulations tend to have very low performance, and that makes symbolic computing less attractive to a variety of fields that could otherwise benefit from it. In that context, this project proposes a library for symbolic computing whose symbols can compile themselves. SymPP can numerically evaluate expressions in runtime by 1) generating and compiling the symbolic tree as machine code, 2) concatenating recursive lambdas, or 3) with a depth-first search on the symbol nodes.\n\n[![Facebook](https://img.shields.io/twitter/url/http/shields.io.svg?style=social\u0026label=Share+on+Facebook\u0026logo=facebook)](https://www.facebook.com/sharer/sharer.php?t=SymPP:%20A%20Symbolic%20Library%20That%20Compiles%20Itself\u0026u=https://github.com/alandefreitas/sympp/)\n[![QZone](https://img.shields.io/twitter/url/http/shields.io.svg?style=social\u0026label=Share+on+QZone\u0026logo=qzone)](http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=https://github.com/alandefreitas/sympp/\u0026title=SymPP:%20A%20Symbolic%20Library%20That%20Compiles%20Itself\u0026summary=SymPP:%20A%20Symbolic%20Library%20That%20Compiles%20Itself)\n[![Weibo](https://img.shields.io/twitter/url/http/shields.io.svg?style=social\u0026label=Share+on+Weibo\u0026logo=sina-weibo)](http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=https://github.com/alandefreitas/sympp/\u0026title=SymPP:%20A%20Symbolic%20Library%20That%20Compiles%20Itself\u0026summary=SymPP:%20A%20Symbolic%20Library%20That%20Compiles%20Itself)\n[![Reddit](https://img.shields.io/twitter/url/http/shields.io.svg?style=social\u0026label=Share+on+Reddit\u0026logo=reddit)](http://www.reddit.com/submit?url=https://github.com/alandefreitas/sympp/\u0026title=SymPP:%20A%20Symbolic%20Library%20That%20Compiles%20Itself)\n[![Twitter](https://img.shields.io/twitter/url/http/shields.io.svg?label=Share+on+Twitter\u0026style=social)](https://twitter.com/intent/tweet?text=SymPP:%20A%20Symbolic%20Library%20That%20Compiles%20Itself\u0026url=https://github.com/alandefreitas/sympp/\u0026hashtags=SymbolicComputing,ComputerAlgebra,SymbolicComputation)\n[![LinkedIn](https://img.shields.io/twitter/url/http/shields.io.svg?style=social\u0026label=Share+on+LinkedIn\u0026logo=linkedin)](https://www.linkedin.com/shareArticle?mini=false\u0026url=https://github.com/alandefreitas/sympp/\u0026title=SymPP:%20A%20Symbolic%20Library%20That%20Compiles%20Itself)\n[![WhatsApp](https://img.shields.io/twitter/url/http/shields.io.svg?style=social\u0026label=Share+on+WhatsApp\u0026logo=whatsapp)](https://api.whatsapp.com/send?text=SymPP:%20A%20Symbolic%20Library%20That%20Compiles%20Itself:+https://github.com/alandefreitas/sympp/)\n[![Line.me](https://img.shields.io/twitter/url/http/shields.io.svg?style=social\u0026label=Share+on+Line.me\u0026logo=line)](https://lineit.line.me/share/ui?url=https://github.com/alandefreitas/sympp/\u0026text=SymPP:%20A%20Symbolic%20Library%20That%20Compiles%20Itself)\n[![Telegram.me](https://img.shields.io/twitter/url/http/shields.io.svg?style=social\u0026label=Share+on+Telegram.me\u0026logo=telegram)](https://telegram.me/share/url?url=https://github.com/alandefreitas/sympp/\u0026text=SymPP:%20A%20Symbolic%20Library%20That%20Compiles%20Itself)\n[![HackerNews](https://img.shields.io/twitter/url/http/shields.io.svg?style=social\u0026label=Share+on+HackerNews\u0026logo=y-combinator)](https://news.ycombinator.com/submitlink?u=https://github.com/alandefreitas/sympp/\u0026t=SymPP:%20A%20Symbolic%20Library%20That%20Compiles%20Itself)\n\n\u003e Please note that this is a work in progress. See our [roadmap](#roadmap) to understand some of our plans for the future. \n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\u003cdetails\u003e\n\u003csummary\u003eTable of Contents\u003c/summary\u003e\n\n- [Examples](#examples)\n  - [Hello World](#hello-world)\n  - [Operations](#operations)\n  - [Equations](#equations)\n  - [Simplification](#simplification)\n  - [Compiling expressions](#compiling-expressions)\n- [Benchmarks](#benchmarks)\n- [Integration](#integration)\n  - [Packages](#packages)\n  - [Build from source](#build-from-source)\n  - [CMake targets](#cmake-targets)\n  - [Other build systems](#other-build-systems)\n- [Limitations and Roadmap](#limitations-and-roadmap)\n- [Contributing](#contributing)\n  - [Contributors](#contributors)\n- [Thanks](#thanks)\n\n\u003c/details\u003e\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## Examples\n\nFor complete examples, see the directory [examples](./examples/).\n\n### Hello World\n\nBy passing a string literal to a `sym` object, we create a symbolic variable:\n\n```cpp\n#include \u003csympp/sympp.h\u003e\n\nint main() {\n    using namespace sympp;\n    using std::cout, std::endl;\n    sym a(\"Hello\");\n    sym b(\"World\");\n    sym c = a + b;\n    cout \u003c\u003c c \u003c\u003c endl;\n    return 0;\n}\n```\n\nInstead of trying to somehow immediately evaluate the expression, the result (`c`) stores the complete symbolic expression:\n\n```console\nHello+World\n```\n\n### Operations\n\nSymbols can represent constants, variables, numbers, functions, or complete expressions:\n\n```cpp\n#include \u003csympp/sympp.h\u003e\n\nint main() {\n    using namespace sympp;\n    using std::cout, std::endl;\n    // Constants\n    sym A(\"A\",10);\n    sym n(\"n\",3);\n    sym pi = constant::pi();\n\n    // Variables\n    sym x1(\"x_1\");\n    sym x2(\"x_2\");\n    sym x3(\"x_3\");\n\n    // Function terms\n    sym begin = A * n;\n    sym term1 = sympp::pow(x1, sym(2)) - A * sympp::cos(2 * pi * x1);\n    sym term2 = power(x2, sym(2)) - A * cosine(2 * pi * x2);\n    sym term3 = power(x3, sym(2)) - A * cosine(2 * pi * x3);\n\n    // Function\n    sym rastrigin = begin + term1 + term2 + term3;\n\n    cout \u003c\u003c rastrigin \u003c\u003c endl;\n    return 0;\n}\n```\n\nOutput:\n\n```console\nA*n+x_1^(2)-A*cos(2*pi*x_1)+x_2^(2)-A*cos(2*pi*x_2)+x_3^(2)-A*cos(2*pi*x_3)\n```\n\n### Equations\n\n\u003e ![Work in progress](https://image.flaticon.com/icons/png/128/2918/2918921.png) Work in progress\n\n### Simplification\n\n\u003e ![Work in progress](https://image.flaticon.com/icons/png/128/2918/2918921.png) Work in progress\n\n### Compiling expressions\n\n\u003e ![Work in progress](https://image.flaticon.com/icons/png/128/2918/2918921.png) Work in progress\n\n## Benchmarks\n\nThese benchmarks illustrate how we can have significant performance gains by compiling expressions. The compiled expressions not only perform better than the usual symbolic evaluation but also the original unsimplified numeric evaluation.\n\n\u003e ![Work in progress](https://image.flaticon.com/icons/png/128/2918/2918921.png) Work in progress\n\n## Integration\n\n### Packages\n\nYou can download the [binary packages from the CI artifacts](https://github.com/alandefreitas/sympp/actions?query=workflow%3ASymPP+event%3Apush) or build the library [from the source files](#build-from-source). \n\nOnce the package is installed, link your C++ program to the library and include the directories where you installed SymPP. \n\nUnless you changed the default options, the C++ library is likely to be in `/usr/local/` (Linux / Mac OS) or `C:/Program Files/` (Windows). The installer will try to find the directory where you usually keep your  libraries but that's not always perfect.\n\nIf you are using CMake, you can then find SymPP with the usual `find_package` command:\n\n```cmake\nfind_package(SymPP REQUIRED)\n# ...\ntarget_link_libraries(my_target PUBLIC sympp)\n```\n\nCMake should be able to locate the `SymPPConfig.cmake` script automatically if you installed the library under `/usr/local/` (Linux / Mac OS) or `C:/Program Files/` (Windows). Otherwise, you need to include your installation directory in `CMAKE_MODULE_PATH` first: \n\n```cmake\nlist(APPEND CMAKE_MODULE_PATH put/your/installation/directory/here)\nfind_package(SymPP REQUIRED)\n# ...\ntarget_link_libraries(my_target PUBLIC sympp)\n```\n\n### Build from source\n\n#### Dependencies\n\nThis section lists the dependencies you need before installing SymPP from source. The build script will try to find all these dependencies for you:\n\n* C++17\n* CMake 3.14 or higher\n* LibTCC (Embedded)\n\n\u003cdetails\u003e\n    \u003csummary\u003eInstructions: Linux/Ubuntu/GCC\u003c/summary\u003e\n    \nCheck your GCC version\n\n```bash\ng++ --version\n```\n\nThe output should have something like\n\n```console\ng++-8 (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0\n```\n\nIf you see a version before GCC-8, update it with\n\n```bash\nsudo apt update\nsudo apt install gcc-8\nsudo apt install g++-8\n```\n\nTo update to any other version, like GCC-9 or GCC-10:\n\n```bash\nsudo apt install build-essential\nsudo add-apt-repository ppa:ubuntu-toolchain-r/test\nsudo apt-get update\nsudo apt install g++-10\n```\n\nOnce you installed a newer version of GCC, you can link it to `update-alternatives`. For instance, if you have GCC-7 and GCC-10, you can link them with:\n\n```bash\nsudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 7\nsudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 7\nsudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10\nsudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 10\n```\n\nYou can now use `update-alternatives` to set you default `gcc` and `g++`:\n\n```bash\nupdate-alternatives --config g++\nupdate-alternatives --config gcc\n```\n\nCheck your CMake version:\n\n```bash\ncmake --version\n```\n\nIf it's older than CMake 3.14, update it with\n\n```bash\nsudo apt upgrade cmake\n```\n\nor download the most recent version from [cmake.org](https://cmake.org/).\n\n[Later](#build-the-examples) when running CMake, make sure you are using GCC-8 or higher by appending the following options:\n\n```bash\n-DCMAKE_C_COMPILER=/usr/bin/gcc-8 -DCMAKE_CXX_COMPILER=/usr/bin/g++-8\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n    \u003csummary\u003eInstructions: Mac Os/Clang\u003c/summary\u003e\n\nCheck your Clang version:\n\n```bash\nclang --version\n```\n\nThe output should have something like\n\n```console\nApple clang version 11.0.0 (clang-1100.0.33.8)\n```\n\nIf you see a version before Clang 11, update XCode in the App Store or update clang with homebrew. \n\nCheck your CMake version:\n\n```bash\ncmake --version\n```\n\nIf it's older than CMake 3.14, update it with\n\n```bash\nsudo brew upgrade cmake\n```\n\nor download the most recent version from [cmake.org](https://cmake.org/).\n\nIf the last command fails because you don't have [Homebrew](https://brew.sh) on your computer, you can install it with\n\n```bash\n/bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)\"\n```\n\nor you can follow the instructions in [https://brew.sh](https://brew.sh).\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n    \u003csummary\u003eInstructions: Windows/MSVC\u003c/summary\u003e\n    \n* Make sure you have a recent version of [Visual Studio](https://visualstudio.microsoft.com)\n* Download Git from [https://git-scm.com/download/win](https://git-scm.com/download/win) and install it\n* Download CMake from [https://cmake.org/download/](https://cmake.org/download/) and install it\n\n\u003c/details\u003e\n\n#### Build the Examples\n\nThis will build the examples in the `build/examples` directory:\n\n```bash\nmkdir build\ncmake -version\ncmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS=\"-O2\"\ncmake --build . -j 2 --config Release\n```\n\nOn windows, replace `-O2` with `/O2`.\n\n#### Installing SymPP from Source\n\nThis will install SymPP on your system:\n\n```bash\nmkdir build\ncmake -version\ncmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS=\"-O2\" -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF \ncmake --build . -j 2 --config Release\ncmake --install .\n```\n\nOn windows, replace `-O2` with `/O2`. You might need `sudo` for this last command.\n\n#### Building the packages\n\nThis will create the binary packages you can use to install SymPP on your system:\n\n```bash\nmkdir build\ncmake -version\ncmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS=\"-O2\" -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF\ncmake --build . -j 2 --config Release\ncmake --install .\ncpack .\n```\n\nOn windows, replace `-O2` with `/O2`. You might need `sudo` for this last command.\n\n### CMake targets\n\n#### Find it as a CMake Package\n\nIf you have the library installed, you can call\n\n```cmake\nfind_package(SymPP)\n```\n\nfrom your CMake build script.\n\nWhen creating your executable, link the library to the targets you want:\n\n```\nadd_executable(my_target main.cpp)\ntarget_link_libraries(my_target PUBLIC sympp)\n```\n\nAdd this header to your source files:\n\n```cpp\n#include \u003csympp/sympp.h\u003e\n```\n\n#### Use it as a CMake subdirectory\n\nYou can use SymPP directly in CMake projects without installing it. Check if you have [Cmake](http://cmake.org) 3.14+ installed:\n\n```bash\ncmake -version\n```\n\nClone the whole project\n \n```bash\ngit clone https://github.com/alandefreitas/sympp/\n```\n\nand add the subdirectory to your CMake project:\n\n```cmake\nadd_subdirectory(sympp)\n```\n\nWhen creating your executable, link the library to the targets you want:\n\n```\nadd_executable(my_target main.cpp)\ntarget_link_libraries(my_target PUBLIC sympp)\n```\n\nAdd this header to your source files:\n\n```cpp\n#include \u003csympp/sympp.h\u003e\n```\n\nHowever, it's always recommended to look for SymPP with `find_package` before including it as a subdirectory. Otherwise, we can get [ODR errors](https://en.wikipedia.org/wiki/One_Definition_Rule) in larger projects. \n\n#### CMake with Automatic Download\n\nCheck if you have [Cmake](http://cmake.org) 3.14+ installed:\n\n```bash\ncmake -version\n```\n\nInstall [CPM.cmake](https://github.com/TheLartians/CPM.cmake) and then:\n\n```cmake\nCPMAddPackage(\n    NAME SymPP\n    GITHUB_REPOSITORY alandefreitas/sympp\n    GIT_TAG origin/master # or whatever tag you want\n)\n# ...\ntarget_link_libraries(my_target PUBLIC sympp)\n```\n\nThen add this header to your source files:\n\n```cpp\n#include \u003csympp/sympp.h\u003e\n```\n\nHowever, it's always recommended to look for SymPP with `find_package` before including it as a subdirectory. You can use:\n\n```\noption(CPM_USE_LOCAL_PACKAGES \"Try `find_package` before downloading dependencies\" ON)\n```\n\nto let CPM.cmake do that for you. Otherwise, we can get [ODR errors](https://en.wikipedia.org/wiki/One_Definition_Rule) in larger projects.\n\n### Other build systems\n\nIf you want to use it in another build system you can either install the library (Section [*Installing*](#installing)) or you have to somehow rewrite the build script.\n\nIf you want to rewrite the build script, your project needs to 1) include the headers and compile all source files in the [`sources`](sources) directory, and 2) link the dependencies described in [`sources/CMakeLists.txt`](sources/CMakeLists.txt).\n\nThen add this header to your source files:\n\n```cpp\n#include \u003csympp/sympp.h\u003e\n```\n\n## Limitations and Roadmap\n\nThings SymPP needs to improve: \n\n* Lots of tests\n* Merge redundant `print` and `c_code` functions\n* More printing formats (inline, code, latex)\n* Smarter simplification functions\n\nThings SymPP doesn't do yet:\n\n* Serialization\n* Interval arithmetic \n* Black-box function lambdas\n* Calculus\n* Variable integration graphs (VIG)\n* Conditional VIGs\n* OpenCL\n* Matrices\n\n## Contributing\n\nThere are many ways in which you can contribute to this library:\n\n* Testing the library in new environments\n* Contributing with interesting examples\n* Developing new node types\n* Finding bugs in general\n* Whatever idea seems interesting to you\n\nIf contributing with code, please turn the pedantic mode ON (`-DBUILD_WITH_PEDANTIC_WARNINGS=ON`), don't forget cppcheck and clang-format.\n\n\u003cdetails\u003e\n    \u003csummary\u003eExample: CLion\u003c/summary\u003e\n    \n![CLion Settings with Pedantic Mode](./documentation/images/pedantic_clion.png)\n    \n\u003c/details\u003e\n\n### Contributors\n\n\u003c!-- readme: collaborators,contributors -start --\u003e \n\u003ctable\u003e\n\u003ctr\u003e\n    \u003ctd align=\"center\"\u003e\n        \u003ca href=\"https://github.com/alandefreitas\"\u003e\n            \u003cimg src=\"https://avatars0.githubusercontent.com/u/5369819?v=4\" width=\"100;\" alt=\"alandefreitas\"/\u003e\n            \u003cbr /\u003e\n            \u003csub\u003e\u003cb\u003eAlan De Freitas\u003c/b\u003e\u003c/sub\u003e\n        \u003c/a\u003e\n    \u003c/td\u003e\u003c/tr\u003e\n\u003c/table\u003e\n\u003c!-- readme: collaborators,contributors -end --\u003e\n\n## Thanks\n\nWe would like to thank the developers of these libraries:\n\n- [SymPy](https://www.sympy.org/): we often use SymPy as a reference for our interface\n- [SymbolicC++](http://symboliccpp.sourceforge.net): we used SymbolicC++ as a reference for our polymorphic design\n- [TinyCC](https://github.com/TinyCC/tinycc): we use TinyCC to compile expressions to machine code\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falandefreitas%2Fsympp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falandefreitas%2Fsympp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falandefreitas%2Fsympp/lists"}