Metamath - Meta mathematics. Symbolic functions and derivatives.

Overview

metamath

Meta mathematic

metamath is a tiny header-only library. It can be used for symbolic computations on single-variable functions, such as dynamic computations of derivatives. The operator precedence rules are naturally handled by the compiler. The library could be useful for building custom DSL's in C++.

func.h contains definitions for some of the cmath functions: Sin/Cos, Ln, Pow, Abs, Sqrt, Exp, more to come... Arithmetic operations with functions are supported:

auto f1 = 3 * x;
auto f2 = Ln(x);
auto f = f1 + f2;

auto y1 = f(2);
auto y2 = f(4);

as well as function composition:

auto f = Ln(x);
auto g = 3 * x;
auto h = f(g);

auto y1 = h(2);

Examples of Functions and Derivatives

Example:

using namespace metamath;

auto f = 3 * x * x;

std::cout << "f(x) = " << f << std::endl;
std::cout << "f(4) = " << f(4.f) << std::endl;
std::cout << "------" << std::endl;

// take derivative
auto df = derivative(f);
std::cout << "f`(x) = " << df << std::endl;
std::cout << "f`(4) = " << df(4.f) << std::endl;

This will produce the following output:

f(x) = 3 * x * x
f(4) = 48
------
f`(x) = ((0 * x + 3) * x + 3 * x)
f`(4) = 24

Example:

auto f =  4 * Sin(2 * x);

std::cout << "f(x) = " << f << std::endl;
std::cout << "f(pi) = " << f(M_PI) << std::endl;
std::cout << "f(pi/4) = " << f(M_PI/4.f) << std::endl;
std::cout << "------" << std::endl;

//take derivative
auto df = derivative(f);
std::cout << "f`(x) = " << df << std::endl;
std::cout << "f`(pi) = " << df(M_PI) << std::endl;
std::cout << "f`(pi/4) = " << df(M_PI/4.f) << std::endl;

This will produce the following output:

f(x) = 4 * sin(2 * x)
f(pi) = 6.99382e-07
f(pi/4) = 4
------
f`(x) = (0 * sin(2 * x) + 4 * cos(2 * x) * (0 * x + 2))
f`(pi) = 8
f`(pi/4) = -3.49691e-07

Build

Requirements

C++14 or later

Steps to build the sample

  • Suppose you cloned to [HOME]/work/metamath

  • For out-of-source, create a build folder in [HOME]/work, and go there.

      $mkdir build
      $cd build
    
  • Run cmake

      $cmake ../metamath
    
  • Build it

      $make
    
  • You can now run a sample (the sample source is in metamath/sample/)

      $./sample/mms
    
  • The sample output:

      Metamath sample
      ======
      f(x) = 3 * x * x
      f(4) = 48
      ------
      f`(x) = ((0 * x + 3) * x + 3 * x)
      f`(4) = 24
      ======
    
      ======
      f(x) = 3 * x
      f(2) = 6
      f(3) = 9
      ------
      f`(x) = (0 * x + 3)
      f`(2) = 3
      ======
    
      ======
      f(x) = ((1) / (x))
      f(2) = 0.5
      f(3) = 0.333333
      ------
      f`(x) = (((0 * x - 1)) / (x * x))
      f`(2.f) = -0.25
      ======
    
      ======
      f(x) = ((2 * (x + 1)) / (x))
      f(2) = 3
      f(3) = 2.66667
      ------
      f`(x) = ((((0 * (x + 1) + 2 * 1) * x - 2 * (x + 1))) / (x * x))
      f`(2) = -0.5
      ======
    
      ======
      f(x) = 4 * sin(2 * x)
      f(pi) = 6.99382e-07
      f(pi/4) = 4
      ------
      f`(x) = (0 * sin(2 * x) + 4 * cos(2 * x) * (0 * x + 2))
      f`(pi) = 8
      f`(pi/4) = -3.49691e-07
      ======
    
      ======
      f(x) = sqrt(x)
      f(4) = 2
      f(6) = 2.44949
      ------
      f`(x) = ((1) / (2 * sqrt(x)))
      f`(4) = 0.25
      f`(6) = 0.204124
      ======
    
      ======
      f(x) = (3 * x^2)
      f(4) = 144
      f(6) = 324
      ------
      f`(x) = 2 * (3 * x^1) * (0 * x + 3)
      f`(4) = 72
      f`(6) = 108
      ======
    
      ======
      f(x) = e^(3 * x)
      f(4) = 162755
      f(6) = 6.566e+07
      ------
      f`(x) = e^(3 * x) * (0 * x + 3)
      f`(4) = 488264
      f`(6) = 1.9698e+08
      ======
    
      ======
      f(x) = ln(3 * x)
      f(4) = 2.48491
      f(6) = 2.89037
      ------
      f`(x) = ((1) / (3 * x)) * (0 * x + 3)
      f`(4) = 0.25
      f`(6) = 0.166667
      ======
    
      ======
      f(x) = |3 * x|
      f(-4) = 12
      f(6) = 18
      ------
      f`(x) = ((3 * x) / (|3 * x|)) * (0 * x + 3)
      f`(-4) = -3
      f`(6) = 3
      ======
    
      ======
      f(x) = ln(x)
      g(x) = 3 * x
      h(x) = f(g(x)) = ln(3 * x)
      h(4) = 2.48491
      ------
      h`(x) = ((1) / (3 * x)) * (0 * x + 3)
      h`(4) = 0.25
      ======
    
You might also like...
Itpp - IT++ library mirror/fork. C++ library of mathematical, signal processing and communication classes and functions.

Introduction ************ IT++ is a C++ library of mathematical, signal processing and communication classes and functions. Its main use is in simula

Implementation of python itertools and builtin iteration functions for C++17

CPPItertools Range-based for loop add-ons inspired by the Python builtins and itertools library. Like itertools and the Python3 builtins, this library

C++ wrappers for SIMD intrinsics and parallelized, optimized mathematical functions (SSE, AVX, NEON, AVX512)

C++ wrappers for SIMD intrinsics and parallelized, optimized mathematical functions (SSE, AVX, NEON, AVX512)

Basic definitions and utility functions for GNSS raw measurement processing

gnss_comm Authors/Maintainers: CAO Shaozu (shaozu.cao AT gmail.com) The gnss_comm package contains basic definitions and utility functions for GNSS ra

A commented version of my libft, with details about how my algorithms work and simple main functions to compile them.
A commented version of my libft, with details about how my algorithms work and simple main functions to compile them.

LIBFT COMMENTED VERSION : 🔧 PART I : 📖 FT_STRCHR : 🔧 PART II : 📖 FT_SUBSTR : /* * DEFINITION : * CREATES A SUBSTRING FROM A STRING WITH PREDETER

📚 single header utf8 string functions for C and C++

📚 utf8.h A simple one header solution to supporting utf8 strings in C and C++. Functions provided from the C header string.h but with a utf8* prefix

⌨️ A 64-key keyboard with many layouts and functions
⌨️ A 64-key keyboard with many layouts and functions

English | 简体中文 Report Bug · Request Feature A 64-key keyboard with many layouts and functions ✨ Keyboard Introduction Gazlowe is a 64-key keyboard wit

Arduino library for basic aerial navigation functions used for processing Euler angles, direction cosine matrices, quaternions, frame conversions, and more.

navduino Arduino library for basic aerial navigation functions used for Euler angles Direction cosine matrices Quaternions Rodrigues Rotation Vectors

Releases(v1.0)
Owner
eg
eg
Armazena a tabela nutricional dos alimentos em um banco de dados (SQLITE), salva as quantidades em um arquivo EXCEL, informando se a meta diária foi batida.

QT-Controle-de-Dieta Armazena a tabela nutricional dos alimentos em um banco de dados (SQLITE), salva as quantidades em um arquivo EXCEL, informando s

null 1 Oct 26, 2021
Meta - static reflection tools for c++. i mostly use this with entt.

meta Static reflection tools for C++. I use it with EnTT but it can work with anything. The main features the library provides are: Registering types

Nikhilesh S 9 Jul 12, 2022
Examples for the "Introduction to programming" course given by me and @bzareva @ Faculty of Mathematics and Informatics, Sofia University (2021/22)

Теми от практикумите по "Увод в програмирането", зимен семестър 2021/2022, спец. "Информатика", група 5 Тема 1 (04.10.2021) : Променливи. Типове проме

null 13 Dec 21, 2022
This repository consists an implementation of the Algorithms encountered in Computer Science, Physics and Mathematics.

All the Algorithms you'll ever need xD This repository contains all the algorithms we have encountered in the fields of Computer Science, Mathematics

ACM VIT 7 Dec 3, 2022
Maat is an open-source Dynamic Symbolic Execution and Binary Analysis framework

About Maat is an open-source Dynamic Symbolic Execution and Binary Analysis framework. It provides various functionalities such as symbolic execution,

Trail of Bits 525 Dec 27, 2022
SANM: A Symbolic Asymptotic Numerical Solver

SANM: A Symbolic Asymptotic Numerical Solver This repository is the official implementation of the SANM solver described in our paper to appear at SIG

Kai Jia 28 Sep 22, 2022
🎻 Automatic Exploit Generation using symbolic execution

S2E Library This repository contains all the necessary components to build libs2e.so. This shared library is preloaded in QEMU to enable symbolic exec

ᴀᴇꜱᴏᴘʜᴏʀ 29 Jan 10, 2022
KLEE Symbolic Execution Engine

KLEE Symbolic Virtual Machine KLEE is a symbolic virtual machine built on top of the LLVM compiler infrastructure. Currently, there are two primary co

KLEE 2.1k Dec 24, 2022
Libft is an individual project at 42 that requires us to re-create some standard C library functions including some additional ones that can be used later to build a library of useful functions for the rest of the program.

Libft is an individual project at 42 that requires us to re-create some standard C library functions including some additional ones that can be used later to build a library of useful functions for the rest of the program.

Paulo Rafael Ramalho 0 Jan 1, 2023
null 313 Dec 31, 2022