0.6.1 • Published 6 years ago

@hugov/math-code v0.6.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

math-code

small and minimal math equation system parser

ExampleSyntaxWorkingsToDoLicense

Example

make(`
wage = L(45, 55, city, work)
rent = L(15, 25, city)
cost = L(15, 20, city, -work) //work less, spend more!

save = pow(wage - rent - cost, 1 + N(1,2)%)
`)() // => { wage, rent, cost, save }

Syntax

Syntax is the same as Javascript with the following exceptions:

  • Object properties (a[0], a.x) are not permitted
  • Variable names prototype, toString, constructor, valueOf, toLocaleString are not permitted
  • Variables are automatically declared. No variable declaration (var a)
  • Percent sign (%) is converted to /100
  • All Math functions (cos, log, ...) and values (PI, ...) are global, along with the following random function generators.
    • Z() => random unit normal number
    • N(low, high [, ...risk factors]) => random normal number, 50% of times between low and high
    • L(low, high [, ...risk factors]) => random lognormal number, 50% of times between low and high
    • W(low, high [, ...risk factors]) => t => random normal number sum
    • R(low, high [, ...risk factors]) => t => random normal number product
  • Random number generators can have additional risk factor unit normal seeds for corrolated variables. For simplicity, the independent portion counts as one. For example, X = N(low, high, riskA, riskB) generates a random number from the unit normal seed z = ( Z() + riskA + riskB ) / 3

Workings

For the given input string:

  random = N(12, 34, a, b, c)

the following code gets compiled:

function(v) {
  this.a = this.Z()
  this.b = this.Z()
  this.c = this.Z()
  v.random = this[1]()
  return v
}.bind({Z: require('random-z'), N: require('grosso-modo').norm: , cos: Math.cos, //...and more
  0: N(12, 34),
  1: function() { return this[0]( this.Z() + this.a + this.b + this.c / 4 ) }
}, {v: 1})

ToDo

  • test negative correlation
  • ? allow inline comments
  • ? return to Math namespace to avoid blocking common variable names (max, min, ...)
  • ? y = Init(1) | Init.y = 1 for run-once recursive
  • ? allow prototype by using hasOwnProperty

License

MIT © Hugo Villeneuve

0.6.1

6 years ago

0.6.0

6 years ago

0.5.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.0

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago