AlgebraFs
所属分类:数学计算
开发工具:F#
文件大小:0KB
下载次数:0
上传日期:2020-05-20 08:11:28
上 传 者:
sh-1993
说明: 一个简单的计算机代数系统(CAS),用F#编写,用于娱乐和学习。
(A simple computer algebra system (CAS), written in F# for fun and learning.)
文件列表:
AlgebraConsole/ (0, 2020-05-20)
AlgebraConsole/AlgebraConsole.fsproj (332, 2020-05-20)
AlgebraConsole/Program.fs (384, 2020-05-20)
AlgebraFs.sln (2814, 2020-05-20)
AlgebraFs/ (0, 2020-05-20)
AlgebraFs/Algebra.fs (3131, 2020-05-20)
AlgebraFs/AlgebraFs.fsproj (525, 2020-05-20)
AlgebraFs/Calculus.fs (1397, 2020-05-20)
AlgebraFs/Evaluation.fs (2366, 2020-05-20)
AlgebraFs/Expression.fs (1435, 2020-05-20)
AlgebraFs/Parser.fs (3385, 2020-05-20)
AlgebraFs/Patterns.fs (1857, 2020-05-20)
AlgebraFs/Printer.fs (2572, 2020-05-20)
LICENSE (1066, 2020-05-20)
# AlgebraFs
This project is a simple computer algebra system (CAS), written entirely in pure F#, for pure fun. It uses an extremely simple expression language modelled by the union:
```fsharp
type Expr =
| Const of float
| Id of string
| Func of string * Expr list
```
Constructs in this expression language are either constants that are `float`s, symbolic identifiers of `string` or functions that have a name (the `string` part) and arguments (the `Expr list` part)
# Partial Active patterns
Active patterns are used extensively in this project to faciliate writing functions that operate on these kinds of expressions, example:
```fsharp
let (|Sin|_|) = function
| Func("sin", [expr]) -> Some expr
| _ -> None
let (|Cos|_|) = function
| Func("cos", [expr]) -> Some expr
| _ -> None
let (|Tan|_|) = function
| Func("tan", [expr]) -> Some expr
| _ -> None
```
Having these patterns defined, you could write the following:
```fsharp
// val isTrigonometric : Expr -> bool
let isTrigonometric = function
| Sin x -> true
| Cos x -> true
| Tan x -> true
| _ -> false
```
So the library is built around these patterns, evaluating an expression and differentiating a single variable (real) functions is built-in, example:
```fsharp
"sin(5x^2)"
|> Parser.tryParse
|> function
| ParseError msg -> printf "Parse error: %s" msg
| Parsed expr ->
Calculus.derivative expr (Id "x")
|> Algebra.fullSimplify
|> Printer.pretty
|> printfn "%s" // returns "cos(5*x^2) * 10x"
```
近期下载者:
相关文件:
收藏者: