Skip to content

Instantly share code, notes, and snippets.

@bmitc
Last active December 8, 2022 07:53
Show Gist options
  • Save bmitc/ddf79288c3af8ae2fca7a3e39a6f2098 to your computer and use it in GitHub Desktop.
Save bmitc/ddf79288c3af8ae2fca7a3e39a6f2098 to your computer and use it in GitHub Desktop.
Symbolic expressions in F#
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"# Symbolic Expressions in F#"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"F# Polyglot Notebook for the article [Symbolic Expressions in F#](https://bmitc.me/articles/symbolic-expressions-in-fsharp). This notebook contains all code and examples found in the article."
]
},
{
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"### How to run this notebook\n",
"\n",
"1. [Install .NET 7 SDK](https://dotnet.microsoft.com/en-us/download)\n",
"2. [Install Visual Studio Code](https://code.visualstudio.com/)\n",
"3. [Install the Polyglot Notebooks Visual Studio Code extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.dotnet-interactive-vscode)\n",
"4. Download this notebook and use the `> Polyglot Notebook: Open Notebook` command.\n",
"\n",
"That's it!"
]
},
{
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"### Utility functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c1c99dea",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"/// Prints out the input with a newline at the end\n",
"let print input = printfn \"%A\" input\n",
"\n",
"/// Prints out the intput with a newline at the end and prefixed\n",
"/// with the given label\n",
"let printWithLabel label input = printfn \"%s: %A\" label input"
]
},
{
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"### Introduction"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1a997a74",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let f x = cos(x*x)\n",
"f 2.1"
]
},
{
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"### Expressions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc554945",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"type Expression =\n",
" | X\n",
" | Constant of float\n",
" | Negation of Expression\n",
" | Sum of Expression * Expression\n",
" | Difference of Expression * Expression\n",
" | Product of Expression * Expression\n",
" | Quotient of Expression * Expression"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "86aeee70",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"X"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5a870314",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"Constant 2.0 |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f8ad1829",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"Product (X, X) |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aae34dc4",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"Product (Constant 4.1, X) |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "01b6a51b",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"Quotient (Constant 1.0, Sum (X, Constant 2.0)) |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c749d5b",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"Quotient (Constant 1.0, Difference (Sum (X, Constant 2.0), Quotient (Constant 1.0, X))) |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6287de7b",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"type Expression =\n",
" | X\n",
" | Constant of float\n",
" | Negation of Expression\n",
" | Sum of Expression * Expression\n",
" | Difference of Expression * Expression\n",
" | Product of Expression * Expression\n",
" | Quotient of Expression * Expression\n",
" | Sine of Expression\n",
" | Cosine of Expression\n",
" | Tangent of Expression\n",
" | Exponential of Expression\n",
" | Logarithm of Expression\n",
" | Power of Expression * exponent: int\n",
"\n",
" with\n",
"\n",
" static member C (x: int) = Constant x\n",
" static member C (x: float) = Constant x\n",
" static member (~-) expression = Negation expression\n",
" static member (+) (e1, e2) = Sum (e1, e2)\n",
" static member (-) (e1, e2) = Difference (e1, e2)\n",
" static member (*) (e1, e2) = Product (e1, e2)\n",
" static member (/) (e1, e2) = Quotient (e1, e2)\n",
" static member ( ** ) (e ,n) = Power (e, n)\n",
" static member Sin e = Sine e\n",
" static member Cos e = Cosine e\n",
" static member Tan e = Tangent e\n",
" static member Exp e = Exponential e\n",
" static member Log e = Logarithm e\n",
" static member Pow (e, n) = Power (e, n) // required format for the overload of **\n",
"\n",
"let e = Constant (exp 1)\n",
"let pi = Constant (System.Math.PI)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee5f7bcc",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"C 1 |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ec686f78",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"C 3.1 |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "637a5817",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"(+) 2 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9661a3f1",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"Product (Sine X, Cosine X) |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b8a5a767",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"Sine X * Cosine X |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "abf36eb4",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"Product (Sine X, Cosine X) = Sine X * Cosine X |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5386e3ac",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"Sin X"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7aed45f4",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"Expression.Sin X |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5d40d970",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"open type Expression"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "55195edf",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"Sin X |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2a25d2bc",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"Cos (Log X) / X |> print"
]
},
{
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"### Evaluating expressions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b34ce91",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let f = Sin X + Cos X"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78ba62e7",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"f 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c13e7d45",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"type Expression =\n",
" | X\n",
" | Sum of Expression * Expression"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8c93508f",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"Sum (X, X) |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "48281d8d",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let evaluate (a: float) expression =\n",
" match expression with\n",
" | X -> a"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "30556732",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"// We redefine and re-open our full expression type here since we have reused the name for some simple examples.\n",
"\n",
"type Expression =\n",
" | X\n",
" | Constant of float\n",
" | Negation of Expression\n",
" | Sum of Expression * Expression\n",
" | Difference of Expression * Expression\n",
" | Product of Expression * Expression\n",
" | Quotient of Expression * Expression\n",
" | Sine of Expression\n",
" | Cosine of Expression\n",
" | Tangent of Expression\n",
" | Exponential of Expression\n",
" | Logarithm of Expression\n",
" | Power of Expression * exponent: int\n",
"\n",
" with\n",
"\n",
" static member C (x: int) = Constant x\n",
" static member C (x: float) = Constant x\n",
" static member (~-) expression = Negation expression\n",
" static member (+) (e1, e2) = Sum (e1, e2)\n",
" static member (-) (e1, e2) = Difference (e1, e2)\n",
" static member (*) (e1, e2) = Product (e1, e2)\n",
" static member (/) (e1, e2) = Quotient (e1, e2)\n",
" static member ( ** ) (e ,n) = Power (e, n)\n",
" static member Sin e = Sine e\n",
" static member Cos e = Cosine e\n",
" static member Tan e = Tangent e\n",
" static member Exp e = Exponential e\n",
" static member Log e = Logarithm e\n",
" static member Pow (e, n) = Power (e, n) // required format for the overload of **\n",
"\n",
"let e = Constant (exp 1)\n",
"let pi = Constant (System.Math.PI)\n",
"\n",
"open type Expression"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f481cd91",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let rec evaluate a expression =\n",
" match expression with\n",
" | X -> a\n",
" | Constant f -> f\n",
" | Negation e -> evaluate a (Product (Constant -1.0, e))\n",
" | Product (e1, e2) -> (evaluate a e1) * (evaluate a e2)\n",
" | Quotient (e1, e2) -> (evaluate a e1) / (evaluate a e2)\n",
" | Sum (e1, e2) -> (evaluate a e1) + (evaluate a e2)\n",
" | Difference (e1, e2) -> (evaluate a e1) - (evaluate a e2)\n",
" | Sine e -> sin (evaluate a e)\n",
" | Cosine e -> cos (evaluate a e)\n",
" | Tangent e -> tan (evaluate a e)\n",
" | Exponential e -> exp (evaluate a e)\n",
" | Logarithm e -> log (evaluate a e)\n",
" | Power (e, c) -> pown (evaluate a e) c"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6b004667",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let f = Sine (Cosine (Product (Constant 2.0, Tangent (Constant 3.0))))\n",
"printWithLabel \"Normal\" (sin (cos (2.0 * tan 3.0)))\n",
"printWithLabel \"Evaluation\" (evaluate 0 f)\n",
"\n",
"let e2 = sin (X * X * X)\n",
"print e2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee259744",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let g = (Cos X) * (Sin X) / ( (C 2) * Exp( C 1 / X))\n",
"evaluate 1.0 g"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"polyglot_notebook": {
"kernelName": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let h = Pow (Cos X, 2) + Pow (Sin X, 2)\n",
"evaluate 1.0 h"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"evaluate 2.0 h"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"evaluate 3.0 h"
]
},
{
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"### Formatting expressions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
" C 1 - Pow (Cos X, 2) |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15f5ced3",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let rec format expression =\n",
" match expression with\n",
" | X -> \"x\"\n",
" | Constant 1.0 -> \"1\"\n",
" | Constant float -> string float\n",
" | Negation e -> sprintf \"-%s\" (format e)\n",
" | Product (Constant 1.0, e2) -> format e2\n",
" | Product (e1, Constant 1.0) -> format e1\n",
" | Product (Constant c, e2) -> sprintf \"%s%s\" (string c) (format e2)\n",
" | Product (e1, Constant c) -> format (Product (Constant c, e1))\n",
" | Product (e1, e2) -> sprintf \"(%s * %s)\" (format e1) (format e2)\n",
" | Quotient (e1, e2) -> sprintf \"(%s / %s)\" (format e1) (format e2)\n",
" | Sum (e1, e2) -> sprintf \"(%s + %s)\" (format e1) (format e2)\n",
" | Difference (e1, e2) -> sprintf \"(%s - %s)\" (format e1) (format e2)\n",
" | Sine e -> sprintf \"sin(%s)\" (format e)\n",
" | Cosine e -> sprintf \"cos(%s)\" (format e)\n",
" | Tangent e -> sprintf \"tan(%s)\" (format e)\n",
" | Exponential e -> sprintf \"exp(%s)\" (format e)\n",
" | Logarithm e -> sprintf \"log(%s)\" (format e)\n",
" | Power (e, 0) -> \"1\"\n",
" | Power (e, 1) -> format e\n",
" | Power (Sine e, n) -> sprintf \"sin^%d(%s)\" n (format e)\n",
" | Power (Cosine e, n) -> sprintf \"cos^%d(%s)\" n (format e)\n",
" | Power (Tangent e, n) -> sprintf \"tan^%d(%s)\" n (format e)\n",
" | Power (Power (e, n), m) -> format (Power (e, n+m))\n",
" | Power (e, n) -> sprintf \"%s^%d\" (format e) n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"polyglot_notebook": {
"kernelName": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"Pow (Cos (C 1 - C 2 * X), 2) |> format"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let rec formatAsLaTeX expression =\n",
" match expression with\n",
" | X -> \"x\"\n",
" | Constant 1.0 -> \"1\"\n",
" | Constant float -> string float\n",
" | Negation e -> sprintf \"-%s\" (formatAsLaTeX e)\n",
" | Product (Constant 1.0, e2) -> formatAsLaTeX e2\n",
" | Product (e1, Constant 1.0) -> formatAsLaTeX e1\n",
" | Product (Constant c, e2) -> sprintf \"%s\\cdot %s\" (string c) (formatAsLaTeX e2)\n",
" | Product (e1, Constant c) -> format (Product (Constant c, e1))\n",
" | Product (e1, e2) -> sprintf @\"(%s \\cdot %s)\" (formatAsLaTeX e1) (formatAsLaTeX e2)\n",
" | Quotient (e1, e2) -> sprintf @\"\\frac{%s}{%s}\" (formatAsLaTeX e1) (formatAsLaTeX e2)\n",
" | Sum (e1, e2) -> sprintf \"(%s + %s)\" (formatAsLaTeX e1) (formatAsLaTeX e2)\n",
" | Difference (e1, e2) -> sprintf \"(%s - %s)\" (formatAsLaTeX e1) (formatAsLaTeX e2)\n",
" | Sine e -> sprintf @\"\\sin(%s)\" (formatAsLaTeX e)\n",
" | Cosine e -> sprintf @\"\\cos(%s)\" (formatAsLaTeX e)\n",
" | Tangent e -> sprintf @\"\\tan(%s)\" (formatAsLaTeX e)\n",
" | Exponential e -> sprintf \"e^{%s}\" (formatAsLaTeX e)\n",
" | Logarithm e -> sprintf @\"\\log(%s)\" (formatAsLaTeX e)\n",
" | Power (e, 0) -> \"1\"\n",
" | Power (e, 1) -> formatAsLaTeX e\n",
" | Power (Sine e, n) -> sprintf @\"\\sin^%d(%s)\" n (formatAsLaTeX e)\n",
" | Power (Cosine e, n) -> sprintf @\"\\cos^%d(%s)\" n (formatAsLaTeX e)\n",
" | Power (Tangent e, n) -> sprintf @\"\\tan^%d(%s)\" n (formatAsLaTeX e)\n",
" | Power (Power (e, n), m) -> formatAsLaTeX (Power (e, n+m))\n",
" | Power (e, n) -> sprintf \"%s^{%d}\" (formatAsLaTeX e) n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bd71d0f3",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let f = (Cos X - Tan (C 3 * X)) / (Exp (C 4 * X - C 1))\n",
"f |> formatAsLaTeX"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"polyglot_notebook": {
"kernelName": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"f |> formatAsLaTeX |> MathString"
]
},
{
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"Some additional examples not in the article."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5310e457",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let g = Sin (X ** 2)\n",
"g |> formatAsLaTeX |> print\n",
"g |> formatAsLaTeX |> MathString"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "83171729",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let h = ((Cos (X - C 1)) ** 2) ** 2\n",
"h |> formatAsLaTeX |> print\n",
"h |> formatAsLaTeX |> MathString"
]
},
{
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"### Simplifying expressions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d1404aa2",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let simplify expression =\n",
" let rec simplifier expression =\n",
" match expression with\n",
" | Sum (Constant c1, Constant c2) -> Constant (c1 + c2)\n",
" | Sum (X, X) -> Product (Constant 2, X)\n",
" | Sum (Constant 0.0, e2) -> simplifier e2\n",
" | Sum (e1, Constant 0.0) -> simplifier e1\n",
" | Sum (Product (Constant c1, X), Product (Constant c2, X)) ->\n",
" simplifier (Product (Sum (Constant c1, Constant c2), X))\n",
" | Sum (Power (Cosine e1, 2), Power (Sine e2, 2)) when simplifier e1 = simplifier e2 -> Constant 1.0\n",
" | Product (X, X) -> Power (X, 2)\n",
" | Product (Constant 1.0, e2) -> simplifier e2\n",
" | Product (Constant 0.0, _) -> Constant 0\n",
" | Product (e1, Constant c) -> simplifier (Product (Constant c, e1))\n",
" | Constant _ as e -> e\n",
" | X -> X\n",
" | Negation e -> Negation (simplifier e)\n",
" | Product (e1, e2) -> Product (simplifier e1, simplifier e2)\n",
" | Quotient (e1, e2) -> Quotient (simplifier e1, simplifier e2)\n",
" | Sum (e1, e2) -> Sum (simplifier e1, simplifier e2)\n",
" | Difference (e1, e2) -> Difference (simplifier e1, simplifier e2)\n",
" | Sine e -> Sine (simplifier e)\n",
" | Cosine e -> Cosine (simplifier e)\n",
" | Tangent e -> Tangent (simplifier e)\n",
" | Exponential e -> Exponential (simplifier e)\n",
" | Logarithm e -> Logarithm (simplifier e)\n",
" | Power (e, 0) -> e\n",
" | Power (e, 1) -> simplifier e\n",
" | Power (e, n) -> Power (simplifier e, n)\n",
" // Run the simplifier five times to try and catch most simplifications\n",
" expression |> simplifier |> simplifier |> simplifier |> simplifier |> simplifier"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "647325c7",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let expression = (Cos X) ** 2 + (Sin X) ** 2\n",
"expression |> printWithLabel \"Original expression\"\n",
"expression |> simplify |> printWithLabel \"Simplified expression\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "023582a7",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let expression = (Cos (X + C 0)) ** 2 + (Sin (C 0 + X)) ** 2\n",
"expression |> printWithLabel \"Original expression\"\n",
"expression |> simplify |> printWithLabel \"Simplified expression\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "35dd00fe",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"(C 0 * X + C 2) * X + C 3 * X |> simplify |> format"
]
},
{
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"### Differentiating expressions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8061d02",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let differentiate expression =\n",
" let rec D expression =\n",
" match simplify expression with\n",
" | Constant _ -> Constant 0\n",
" | X -> Constant 1\n",
" | Negation e -> Negation (D e)\n",
" | Product (e1, e2) -> Sum (Product (D e1, e2), Product (e1, D e2))\n",
" | Quotient (e1, e2) -> ((D e1 * e2) - (e1 * D e2)) / Power (e2, 2)\n",
" | Sum (e1, e2) -> Sum (D e1, D e2)\n",
" | Difference (e1, e2) -> Difference (D e1, D e2)\n",
" | Sine e -> Product (Cosine e, D e)\n",
" | Cosine e -> Product (Negation (Sine e), D e)\n",
" | Tangent e -> Product (Product (Quotient (Constant 1, Cosine e), Quotient (Constant 1, Cosine e)), D e)\n",
" | Exponential e as exp -> Product (exp, D e)\n",
" | Logarithm e -> Product (Quotient (Constant 1, e), D e)\n",
" | Power (e, n) -> Product (Product (Constant n, Power (e, n-1)), D e)\n",
" D expression |> simplify"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e2a41c32",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let f = Cosine (Product (X, X))\n",
"differentiate f |> format"
]
},
{
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"An example that doesn't quite simplify as well as we'd like."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41746f5a",
"metadata": {
"dotnet_interactive": {
"language": "fsharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let expression = exp((Constant 3.0) * X * X)\n",
"differentiate expression |> simplify |> formatAsLaTeX |> MathString"
]
},
{
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"source": [
"### Conclusion"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"let xToTheNthPower n = List.fold (fun acc _ -> Product (X, acc)) X [1..n-1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"xToTheNthPower 1 |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"xToTheNthPower 2 |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"xToTheNthPower 3 |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"xToTheNthPower 4 |> print"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}
},
"outputs": [],
"source": [
"xToTheNthPower 5 |> print"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [
"c#",
"C#"
],
"languageName": "C#",
"name": "csharp"
},
{
"aliases": [],
"name": ".NET"
},
{
"aliases": [
"f#",
"F#"
],
"languageName": "F#",
"name": "fsharp"
},
{
"aliases": [],
"languageName": "HTML",
"name": "html"
},
{
"aliases": [],
"languageName": "KQL",
"name": "kql"
},
{
"aliases": [],
"languageName": "Mermaid",
"name": "mermaid"
},
{
"aliases": [
"powershell"
],
"languageName": "PowerShell",
"name": "pwsh"
},
{
"aliases": [],
"languageName": "SQL",
"name": "sql"
},
{
"aliases": [],
"name": "value"
},
{
"aliases": [
"frontend"
],
"name": "vscode"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment