MathKit

MathKit 是一个 Swift 库,用于处理、求解和绘制数学方程、表达式和函数。

特性

安装

Swift Package Manager

在你的 Package.swift 文件中添加 MathKit 作为依赖项

dependencies: [
.package(url: "https://github.com/liam923/MathKit.git", from: "1.0.1")
]

手动

只需下载并将 Sources/MathKit 文件夹拖放到你的项目中即可。

使用示例

在开始任何操作之前,我们需要导入 MathKit 并设置一个 System

import MathKit

let system = System()

简化或分解表达式

let expression = try! Expression(string: "3(x + 3)^2 - 29 + 4(x + 2) - 13x", system: system)
try! print(expression.simplify(system: system)) //  3x^2 + 9x + 6
try! print(expression.factor(system: system)) //  3(x + 1)(x + 2)

求解方程

let equation = try! Equation(string: "x^2 + 1 = 3 - x", system: system)
try! print(equation.solve(forVariable: system.variable(withSymbol: "x"))) // [1, -2]

求导数

let function = try! Expression(string: "deriv(5x^3 + ln(x) + sin(x), x, x)", system: system)
try! print(function.simplify(system: system)) // 15(x)^(2) + (x)^(-1) + cos(x)

绘制函数图像

// create the function to graph
let f = system.function(withName: "f", variables: [system.variable(withSymbol: "x")])
f.value = try! Expression(string: "x + 1", system: system)
// create the graph itself
let graph = GraphScene(size: CGSize(width: 500, height: 500))
graph.functions.append((f, true, .red))
graph.update() // scene is now an SKView that displays the graph of f(x) = x + 1

元数据

William Stevenson – liam923@verizon.net

根据 MIT 许可证分发。有关更多信息,请参见 LICENSE

https://github.com/liam923