Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@younata
Created October 26, 2014 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save younata/54cb2165359d4c413edd to your computer and use it in GitHub Desktop.
Save younata/54cb2165359d4c413edd to your computer and use it in GitHub Desktop.
MathKit
import MathKit
let p = SimplePolynomial(string: "3x^2 + x + 1")
let p2 = SimplePolynomial(string: "(x)(y)")
let p3 = SimplePolynomial(string: "thisIsOneLongVariable^2")
p + p2 // returns 3x^2 + x + (x)(y) + 1
p - p2 // returns 3x^2 + x - (x)(y) + 1
p * p2 // don't feel like figuring that one out.
p.valueAt(["x": 1]) // returns 5
p2.polynomialAt(["x": 1]) // returns y
p.differentiate("x") // returns 6x + 1
p2.differentiate("x") // returns y
p2.gradient() // returns an object equivalent to: ["x": SimplePolynomial(string: "y"), "y": SimplePolynomial(string: "x")]
p.solve() // returns nil because this doesn't have any roots (there is no value of x such that f(x) = 0)
p2.solve() // returns [["x": 0], ["y": 0]]
p.of(p2, at: "x") // returns 3x^2y^2 + (x)(y) + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment