apple / swift-numerics

Advanced mathematical types and functions for Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Module for Symbolic Calculations / Computer Algebra System

Wildchild9 opened this issue · comments

I think a module that performs symbolic calculations with expressions could be very useful. This would manipulate algebraic expressions by simplifying, factoring, expanding, etc., providing exact answers. Ideally with something like this, you would be able to define your own variables and functions along with rules to simplify them. I also think that this is something that should be including because it is something that is far from trivial to implement.

With Swift's custom operators and strong typing, this will be extremely powerful.

There are plenty of use case for this. Firstly, as previously, this would allow you to solve equations with exact value rather than estimations. Moreover, this would allow for the definition of types whose properties themselves are expressions, thus allowing you to perform calculations and easily derive formulas. For example, one could define a point type with expressions as their coordinates, enabling to easily get the formula for the distance between two points.

struct Point {
    var x, y: Expression
}

let p1 = Point(x: "x1", y: "y1")
let p2 = Point(x: "x2", y: "y2")
let lengthFormula = sqrt((p1.x - p2.x) ^ 2 + (p1.y - p2.y) ^ 2)

print(lengthFormula)
// Prints "√((x1 - x2)² + (y1 - y2)²)"

Expressions would also be able to do things like isolate for variables:

var expression: Expression = "x ^ 2 + 1 = 3y + 5"
expression.isolate(for: "x")

print(expression)
// Prints "x = ±√(3y + 4)"

For mathematical computations, something like this has the potential to really make Swift stand out, opening it to numerous applications.

Out-of-scope for Swift Numerics. I agree that it's interesting, but there's already a few years worth of projects lined up (and this is, after all, symbolic computation instead of numeric).

This should absolutely not be read as discouraging you or others to push for or work on this; I am only saying that this package isn't the right home for it.