Math expression parser built with Point•Free's swift-parsing package

Overview

swift-math-parser

Basic math expression parser built with Point•Free's swift-parsing package.

NOTE: currently, this uses a fork of that fixes a parsing bug involving floating-point literals that have a trailing 'e' or 'E' specifier.

let parser = MathParser()
let evaluator = parser.parse('4 * sin(t * π) + 2 * sin(t * π)')
let v1 = evaluator.eval("t", value: 0.0) // 0.0
let v2 = evaluator.eval("t", value: 0.5) // 6.0
let v3 = evaluator.eval("t", value: 1.0) // 0.0

The parser will return nil if it is unable to completely parse the expression.

By default, the expression parser and evaluator handle the following symbols and functions:

  • Symbols: pi, π, and e
  • Functions: sin, cos, tan, log10, ln/loge, log2, exp, ceil, floor, round, sqrt

You can reference additional symbols or variables and functions by providing your own mapping functions. There are two places where this can be done:

  • MathParser.init
  • MathParser.Evaluator.eval

If a symbol or function does not exist during an eval call, the final result will be NaN. If a symbol is defined in during parsing, it will be replaced with the symbol value. Otherwise, it will be resolved during a future eval call. Same for function calls -- if the function is known during parsing and the argument is a known value, then it will be replaced with the function result. Otherwise, the function call will take place during an eval call.

You might also like...
A cross-platform Swift library for evaluating mathematical expressions at runtime

Introduction What? Why? How? Usage Installation Integration Symbols Variables Operators Functions Arrays Performance Caching Optimization Standard Lib

Numeric facilities for Swift

NumericAnnex NumericAnnex supplements the numeric facilities provided in the Swift standard library. Features The exponentiation operator ** and the c

Swift Matrix Library

Swift Matrix and Machine Learning Library Note: tensorflow/swift and apple/swift-numerics/issues/6 have or will have more complete support for NumPy-l

MRFoundation - A library to complement the Swift Standard Library

MRFoundation MRFoundation is a library to complement the Swift Standard Library.

Pure Swift 2.0 S-expression Parser

SwiftExP This is an S-expression parser written in pure Swift 2.0, where pure means without making any necessary usage of Foundation APIs at its core.

Swift-when - Expression switch support in Swift

Swift When - supporting switch expressions in Swift! What is it? Basically, it a

A delightful and expressive regular expression type for Swift.

Regex Pattern match like a boss. Usage Create: // Use `Regex.init(_:)` to build a regex from a static pattern let greeting = Regex("hello (world|univ

A Cross-Platform String and Regular Expression Library written in Swift.

Guitar 🎸 A Cross-Platform String and Regular Expression Library written in Swift. About This library seeks to add common string manipulation function

Approximate is a Swift package that provides implementations of floating point comparisons for the Swift ecosystem

Approximate Approximate floating point equality comparisons for the Swift Programming Language. Introduction Approximate is a Swift package that provi

Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable

Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable. Elevate should no longer be used for

CoreML-Face-Parsing - how to use face-parsing CoreML model in iOS
CoreML-Face-Parsing - how to use face-parsing CoreML model in iOS

CoreML-Face-Parsing The simple sample how to use face-parsing CoreML model in iO

A lightweight CSS parser for parsing and creating CSS stylesheets

SwiftCSSParser A lightweight CSS parser for Swift that uses cssparser (cpp) under the hood. Basic usage Here's a simple code snippet to get you starte

Multi-dimensional Swift math

Upsurge Upsurge implements multi-dimensional data structures and operations. It brings numpy-like operations to Swift. Upsurge no longer supports DSP

SwiftMath is a Swift framework providing some useful math constructs and functions

SwiftMath is a Swift framework providing some useful math constructs and functions, like complex numbers, vectors, matrices, quaternions, and polynomials.

UITextField character counter with lovable UX 💖. No math skills required 🙃.
UITextField character counter with lovable UX 💖. No math skills required 🙃.

TextFieldCounter UITextField character counter with lovable UX 💖. No math skills required 🙃. Features Set max length of UITextField. A beautiful an

iOS_Math_Practicing is a math practicing game in iOS.
iOS_Math_Practicing is a math practicing game in iOS.

iOS_Math_Practicing iOS_Math_Practicing is a math practicing game in iOS. Important! You can use the codes to do what ever you want, instead of using

Beautiful math equation rendering on iOS and MacOS
Beautiful math equation rendering on iOS and MacOS

iosMath iosMath is a library for displaying beautifully rendered math equations in iOS and MacOS applications. It typesets formulae written using the

It is a simple maths quiz app that will help users to test their math skills.

MathQuiz It is a simple maths quiz app that will help users to test their math skills. It has the following screens 1.Welcome screen with start button

Oovium's math engine

OoviumEngine The function parser and math engine of the iOS / macOS app Oovium, including a command line version: oov. Swift Package Manager It is pos

Releases(3.0.0)
Owner
Brad Howes
I've typed more code than I can remember.
Brad Howes
SwiftMath is a Swift framework providing some useful math constructs and functions

SwiftMath is a Swift framework providing some useful math constructs and functions, like complex numbers, vectors, matrices, quaternions, and polynomials.

Matteo Battaglio 175 Dec 2, 2022
Beautiful math equation rendering on iOS and MacOS

iosMath iosMath is a library for displaying beautifully rendered math equations in iOS and MacOS applications. It typesets formulae written using the

Kostub Deshmukh 1.3k Dec 21, 2022
Oovium's math engine

OoviumEngine The function parser and math engine of the iOS / macOS app Oovium, including a command line version: oov. Swift Package Manager It is pos

Joe Charlier 0 Aug 24, 2022
Arbitrary-precision arithmetic in pure Swift

Overview API Documentation License Requirements and Integration Implementation Notes Full-width multiplication and division primitives Why is there no

null 707 Dec 19, 2022
A collection of functions for statistical calculation written in Swift.

σ (sigma) - statistics library written in Swift This library is a collection of functions that perform statistical calculations in Swift. It can be us

Evgenii Neumerzhitckii 658 Jan 5, 2023
Swift Matrix Library

Swift Matrix and Machine Learning Library Note: tensorflow/swift and apple/swift-numerics/issues/6 have or will have more complete support for NumPy-l

Scott Sievert 591 Sep 5, 2022
Overload +-*/ operator for Swift, make it easier to use (and not so strict)

Easy-Cal-Swift Overview This file is an overloading of +-*/ operator for Swift, to make it easier to use (and not so strict) It can make your life wit

Wei Wang 272 Jun 29, 2022
Numpy-like library in swift. (Multi-dimensional Array, ndarray, matrix and vector library)

Matft Matft is Numpy-like library in Swift. Function name and usage is similar to Numpy. Matft Feature & Usage Declaration MfArray MfType Subscription

null 80 Dec 21, 2022
Swift Custom Operators for Mathematical Notation

Euler Euler uses custom operators in the "Math Symbols" character set to implement functions using traditional mathematical notation. Please keep in m

Mattt 1.1k Jan 4, 2023
VectorMath is a Swift library for Mac and iOS that implements common 2D and 3D vector and matrix functions

Purpose VectorMath is a Swift library for Mac and iOS that implements common 2D and 3D vector and matrix functions, useful for games or vector-based g

Nick Lockwood 341 Dec 31, 2022