<jupyter_start><jupyter_text>Math.NET Symbolics ExamplesThis is an example F Notebook showing how [Math.NET Symbolics](http://symbolics.mathdotnet.com/) can be used interactively.To get started, we need to load Math.NET Symbolics using Paket, open namespaces and make sure expression are presented nicely in LaTeX:<jupyter_code>#load "Paket.fsx"
Paket.Dependencies.Install """
frameworks: net45
source https://nuget.org/api/v2
nuget MathNet.Symbolics
"""
open MathNet.Numerics
open MathNet.Symbolics

open Operators

App.AddDisplayPrinter (fun (x:Expression) -> { ContentType = "text/latex"; Data = "$$" + LaTeX.format x + "$$" })<jupyter_output>The namespace or module 'MathNet' is not defined. Maybe you want one of the following:
   Math
The namespace or module 'MathNet' is not defined. Maybe you want one of the following:
   Math
The type 'Expression' is not defined.
The value, namespace, type or module 'LaTeX' is not defined. Maybe you want one of the following:
   LatexOutput<jupyter_text>Then let's create a few symbols we'd like to use in our algebraic expressions:<jupyter_code>let x = symbol "x"
let y = symbol "y"
let z = symbol "z"
let a = symbol "a"
let b = symbol "b"
let c = symbol "c"
let d = symbol "d"
let e = symbol "e"
let f = symbol "f"<jupyter_output>The value or constructor 'symbol' is not defined.
The value or constructor 'symbol' is not defined.
The value or constructor 'symbol' is not defined.
The value or constructor 'symbol' is not defined.
The value or constructor 'symbol' is not defined.
The value or constructor 'symbol' is not defined.
The value or constructor 'symbol' is not defined.
The value or constructor 'symbol' is not defined.
The value or constructor 'symbol' is not defined.<jupyter_text>Using these symbols we can then use normal F notation to construct algebraic expressions. Expressions are automatically simplifed.<jupyter_code>(a/b/(c*a))*(c*d/a)/d
(a/b)**c<jupyter_output>The value or constructor 'a' is not defined.
The value or constructor 'b' is not defined.
The value or constructor 'c' is not defined.<jupyter_text>You can use `Q` to force a number to become a symbolic rational number expression, e.g. if an operator is used with only numeric operands. Otherwise F would evaluate it, and e.g. perform an integer division as below. If at least one operand is an expression, the result will be an expression as well.<jupyter_code>3/2
3Q/2
(1Q/2)*sin(x)<jupyter_output>This numeric literal requires that a module 'NumericLiteralQ' defining functions FromZero, FromOne, FromInt32, FromInt64 and FromString be in scope
The value or constructor 'x' is not defined.<jupyter_text>Algebraic FunctionsSymbolics provides a set of F modules with functions to manipulate expressions.<jupyter_code>Exponential.expand (exp(2*x+y))
Trigonometric.expand (sin(a+x))
Trigonometric.contract (sin(a)*sin(b))
Trigonometric.contract (cos(x)**4)<jupyter_output>The value, namespace, type or module 'Trigonometric' is not defined.<jupyter_text>The functions are grouped by the context we interpret the expression in. Functions in `Polynomial` assume the expression is a single- or multivariate polynomial.<jupyter_code>Polynomial.quot x (x**3 - 2*x**2 - 4) (x-3)
(a/b)+(c/d)+(e/f)
Rational.expand (a/b + c/d + e/f)
Rational.rationalize (1+1/(1+1/x))
Polynomial.gcd x (x**7 - 4*x**5 - x**2 + 4) (x**5 - 4*x**3 - x**2 + 4)
Polynomial.polynomialExpansion x y (x**5 + 11*x**4 + 51*x**3 + 124*x**2 + 159*x + 86) (x**2 + 4*x + 5)
let expr = (8*a*x + 6*a*x**2)/(4*x*a) |> Rational.expand
expr
2*expr |> Algebraic.expand
Approximate.approximate expr
Rational.simplify x ((x**2-1)/(x+1))
Rational.simplify x (1/(1+1/(x+1)) + 2/(x+2))
(x**8 + 6*x**6 + 12*x**4 + 8*x**2)
(x**8 + 6*x**6 + 12*x**4 + 8*x**2) |> Polynomial.factorSquareFree x<jupyter_output>The value or constructor 'x' is not defined.
The value or constructor 'x' is not defined.
The value or constructor 'x' is not defined.
The value or constructor 'x' is not defined.
The value, namespace, type or module 'Polynomial' is not defined.<jupyter_text>Calculus<jupyter_code>Calculus.differentiate x (a*x**2 + b*x + c)
(x**3 - 12*x**2 - c) |> Calculus.tangentLine x 1Q
1/z |> Calculus.normalLine z 3Q<jupyter_output>The value or constructor 'z' is not defined.
The value, namespace, type or module 'Calculus' is not defined.<jupyter_text>Working with ExpressionsOf course we can also combine these functions to write more powerful function on top of them:<jupyter_code>let taylor (k:int) symbol a x =
    let rec impl n factorial sum dxn =
        if n = k then sum else
        let dxn_a = dxn |> Structure.substitute symbol a
        let dxn'  = dxn |> Calculus.differentiate symbol
        impl (n+1) (factorial*(n+1)) (sum + dxn_a/factorial*(symbol-a)**n) dxn'
    impl 0 1 zero x |> Algebraic.expand

taylor 4 x 0Q (sin(x)+cos(x))
let tangent symbol a x = taylor 2 symbol a x

tangent x 0Q (sin(x)+cos(x))<jupyter_output>The value or constructor 'taylor' is not defined.
The value or constructor 'x' is not defined.
This numeric literal requires that a module 'NumericLiteralQ' defining functions FromZero, FromOne, FromInt32, FromInt64 and FromString be in scope
The value or constructor 'x' is not defined.
The value or constructor 'x' is not defined.<jupyter_text>Simplistic solver<jupyter_code>let solve x a b =

    let a' = Rational.simplify x a
    let an = Rational.numerator a'
    let ad = Rational.denominator a'
    
    let b' = Rational.simplify x b
    let bn = Rational.numerator b'
    let bd = Rational.denominator b'
    
    let expr = an*bd - bn*ad |> Algebraic.expand
    
    if Polynomial.isPolynomial x expr then
        match Polynomial.coefficients x expr with
        | [||] -> Undefined
        | [| a |] -> x
        | [| a; b |] -> -a/b |> Rational.simplify x
        | _ -> failwith "higher polynomials not supported"
    else failwith "only general polynomial expressions supported for now"
    
// x/(x+5) = 7 --> x=?
solve x (x/(x + 5)) (number 7)
// sin(a)+x*cos(b) = c --> x=?
solve x (sin(a)+x*cos(b)) c<jupyter_output>The value or constructor 'solve' is not defined. Maybe you want one of the following:
   Some<jupyter_text>Formatting and Parsing Expressions<jupyter_code>Infix.parseOrUndefined "(a/b)*sin(x)"
Quotations.parse <@ fun x y -> -x*y/3 @>
MathML.formatContentStrict (x**(1Q/3))
MathML.parse """<csymbol cd="nums1">pi</csymbol>"""<jupyter_output>The value, namespace, type or module 'MathML' is not defined. Maybe you want one of the following:
   Math
{'fraction_non_alphanumeric': 0.11555555555555555, 'fraction_numerical': 0.017011494252873564, 'mean_word_length': 4.254428341384863, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 52, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '200836', 'license': 'MIT', 'n_tokens_mistral': 2294, 'n_tokens_neox': 2197, 'n_words': 798, 'path': 'notebooks/.ipynb_checkpoints/Math.NET Symbolics - Examples-checkpoint.ipynb', 'repo_name': 'allisterb/Sylvester', 'size': '6525'}