Working with math
Documentation for WebSharper Math
Math in WebSharper
Higher level math is supported in WebSharper via the MathJS and MathJax JavaScript libraries. The usage of these libraries are the same as in JavaScript
with some small changes. JavaScript
would let you use any type in any function, but in WebSharper to use more than one type in a function we have to use the MathNumber
wrapper.
A little example:
In JavaScript
:
In WebSharper
:
Exception is when you use either only floats
, ints
, or Math.Units
Bignumbers
F# already supports BigInteger
(System.Numerics.BigInteger
or bigint
), but JavaScript
does not by default. With WebSharper the usage of these types and their operators are just as easy as working with integers.
Constructing BigInteger
:
Operations with these numbers are possible with the .Net way:
Complex number
Just like BigInteger
, Complex
is a member of System.Numerics
too, but JavaScript
does not support them. To use the Complex
type in our program we could construct it as we're used to it from .Net, but now we're able to do it with Math.Complex()
too which is able to construct a Complex
number by taking a string with the complex value.
After constructing the numbers, we can use them as we're used to it:
Fraction
The original float
type in JavaScript
has limitations with its precision, but it's solved with the Math.Fraction
type which has a much higher precision with its operations. To use this new Math.Fraction
, we have to call the Math.Fraction()
constructor.
We have many ways to create a Fraction, for example:
After
Vectors
For vector operations in WebSharper we have to use the MathJS.Math
functions and in those functions we have to use the MathNumber
wrapper for the vectors. There are few exceptions when we don't have to wrap these vectors. If the function only accepts vectors or matrices, then the wrapper isn't needed (but can be used). (Note that if you wrap these in MathNumber
, you might get a MathNumber
return value.)
Matrices
The same as for vectors, WebSharper grants a huge variety of Matrix operations and functions, but to use those, we need to wrap the matrices in MathNumber. As we have seen at the vectors, there are some functions where matrices can be used without the MathNumber
wrapper. (Note that if you wrap these in MathNumber
, you might get a MathNumber
return value.)
Units
WebSharper allows you to calculate with units too. Most of the functions from Math accept Math.Unit
s to work with. Units are a special kind of types. They have a value and a measurement. Values with different kind of measurements can be used in operations and it will calulate with the given measurements. Units can be freed from their measurements (for example) by dividing.
Rendering math with WebSharper
Rendering math expressions in WebSharper works with the MathJax JavaScript library. The documentation for MathJax extension can be found here.
MathJax, so WebSharper too allows you to use a variety of common math formatting systems but not only that, it supports more than one output formatting systems.
Supported input formats
Format | Loader Module |
---|---|
TeX | "input/tex" |
MathML | "input/mml" |
AsciiMath | "input/asciimath" |
Supported output formats
Format | Output Module |
---|---|
CHTML | "output/chtml" |
SVG | "output/svg" |
Rendering expresions
To render static expressions (it's part of the static html file or generated with WebSharper to the html file at the beginning), you only need to configure MathJax correctly. It will automatically typeset matching math content once the page loads (see an example here).
To render dynamically changing formulas, update the DOM with the new expression and call MathJax.Typeset()
— this replaces the old MathJax.Hub.Queue()
method used in version 2.
TeX
To render TeX expressions, ensure the loader module input/tex
is included in your MathJax configuration. This allows MathJax to correctly parse TeX input.
(TeX documentation)
MathML
To render MathML expressions, include the loader module input/mml
in your MathJax configuration. This tells MathJax to treat embedded MathML correctly.
(MathML documentation)
Ascii Math
To render AsciiMath expressions, include the loader module input/asciimath
in your MathJax configuration. This ensures that MathJax correctly parses AsciiMath notation.
(AsciiMath documentation)
An example for expressions
There are many functions in MathJS that calculates an expression, solves a problem. In this example we'll use the Math.Derivative
function to get a Node
with the result in it. A Node
then can be converted to a String
, but with the MathJax extension we can render the result if the formula is in TeX
format. To do that we have to set up MathJax
to parse and render TeX
formulas then by using the Node
's ToTex()
function we convert the result into a String
with the formula in TeX
formatting.
(Most of the functions don't result a Node
, but they can be converted to Node
by Math.Parse()
or by other means. (MathJax documentation))