Giter Site home page Giter Site logo

malczak / sfexpression Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 349 KB

SFExpression allow to parse mathematics strings expression and calculate them in Swift

License: MIT License

Swift 26.21% Objective-C 1.73% C 72.05%
swift math expression-parsing math-parser math-parser-library

sfexpression's Introduction

SFExpression

SFExpression allow to parse mathematics strings expression and calculate them in Swift.

Library is a just a wrapper around a sffe math parser created for XaoS fractal zoomer. Because of a number of operations required to draw, zoom and animate complex plane fractals the library had been designed with execution speed in mind. Its quite slow when it comes to expression parsing but is very fast when comes to expression evaluation.

Internally library is transforming expression into a call stack. Expression evalutaion is done insitu even if expression variables are modified.

Main advantages

  • easily extendable - custom variables, custom functions etc.
  • can work in any number space (even algebraic space)
  • fast :)

Weak sides

  • complicated syntax of custom functions
  • not thread save

Custom variable accessing example

let ex = SFExpression()
ex["x"] = 0.0
ex.parse("2 * sin(x) * cos(x)")

ex["x"] = 2.0
var r = ex.eval() // r = -0.756802495307928

ex["x"] = 4.0
r = ex.eval() // r = 0.989358246623382

Variable binding is a way faster way to provide variables

var x = 0.0
let expr = SFExpression()
try! expr.bindVar("x", ptr: &x)
ex.parse("2 * sin(x) * cos(x)")
    
x = 2.0
var r = ex.eval() // r = -0.756802495307928

x = 4.0
r = ex.eval() // r = 0.989358246623382

Swift callback can be provided as a custom function

var x = 1.0, y = 2.0
let expr = SFExpression()
try! expr.bindVar("x", ptr: &x)
try! expr.bindVar("y", ptr: &y)

expr.addFunction("magick", params: 2) {
      (p: UnsafeMutablePointer<sfarg>, payload: UnsafeMutablePointer<Void>) -> UnsafeMutablePointer<sfarg> in
      var arg = SFArgument(p)
      let in_y = arg.param1()
      let in_x = arg.param2()
      arg.value = sin(in_x.value) + in_y.value;
      return in_x.memory;
    }
    
expr.parse("2 * magick_f(x;y)")
expr.eval()

Example above uses helper struct SFArgument to access method arguments. To improve eval time direct access can be used

var x = 1.0, y = 2.0
let expr = SFExpression()
try! expr.bindVar("x", ptr: &x)
try! expr.bindVar("y", ptr: &y)

expr.addFunction("magick_", params: 2) {
      (p: UnsafeMutablePointer<sfarg>, payload: UnsafeMutablePointer<Void>) -> UnsafeMutablePointer<sfarg> in
      let arg = p.memory;
      let in_y = arg.parg;
      let in_x = in_y.memory.parg;
      arg.value.memory = sin(in_x.memory.value.memory) + in_y.memory.value.memory
      return in_x;
    }
    
expr.parse("2 * magick_f(x;y)")
expr.eval()

Custom methods can also be provided as Swift or C functions.

sfexpression's People

Contributors

malczak avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.