Skip to content
Snippets Groups Projects
Commit cbcd4423 authored by Christoph Ruegg's avatar Christoph Ruegg
Browse files

wip

parent c83bbd60
No related branches found
No related tags found
No related merge requests found
......@@ -30,12 +30,12 @@ module private InfixParser =
let abs p = between (str_ws "|") (str_ws "|") p |>> Expression.Abs
let number : Expression parser =
let options =
NumberLiteralOptions.AllowFraction
||| NumberLiteralOptions.AllowFractionWOIntegerPart
let options =
NumberLiteralOptions.AllowFraction
||| NumberLiteralOptions.AllowFractionWOIntegerPart
||| NumberLiteralOptions.AllowInfinity
||| NumberLiteralOptions.AllowExponent
numberLiteral options "number" .>> ws
|>> fun num ->
if num.IsInfinity then Expression.PositiveInfinity
......@@ -45,7 +45,7 @@ module private InfixParser =
let identifier : Expression parser =
let isMathChar = function | '\u03C0' | '\u221E' | '\u29DD' -> true | _ -> false
let isIdentifierFirstChar c = isLetter c || isMathChar c
let isIdentifierChar c = isLetter c || isDigit c || isMathChar c || c = '_'
let isIdentifierChar c = isLetter c || isDigit c || isMathChar c || c = '_'
many1Satisfy2L isIdentifierFirstChar isIdentifierChar "identifier" .>> ws
|>> function // differentating between constants and identifiers
| "pi" | "\u03C0" -> Expression.Constant Pi
......
namespace MathNet.Symbolics
open System
open System.Numerics
open MathNet.Numerics
open MathNet.Symbolics
open System.Linq.Expressions
[<StructuralEquality;NoComparison;RequireQualifiedAccess>]
type VisualExpression =
| Identifier of Symbol
| Constant of Constant
| PositiveInteger of BigInteger
| PositiveFloatingPoint of double
| Parenthesis of VisualExpression
| Prodct of VisualExpression list
| Fraction of VisualExpression * VisualExpression
| Negative of VisualExpression
| Sum of VisualExpression list
| Power of VisualExpression * VisualExpression
| Function of string * VisualExpression
| FunctionN of string * (VisualExpression list)
| ComplexInfinity
| Infinity
| Undefined
module VisualExpression =
let fromExpression expression =
let parenthesis priority threshold ve = if priority > threshold then VisualExpression.Parenthesis ve else ve
let rec convert priority = function
| Number n when n.IsNegative && n.IsInteger ->
VisualExpression.Negative (VisualExpression.PositiveInteger (-n.Numerator))
|> parenthesis priority 0
| Number n when n.IsNegative ->
VisualExpression.Negative (VisualExpression.Fraction (VisualExpression.PositiveInteger (-n.Numerator), VisualExpression.PositiveInteger n.Denominator))
|> parenthesis priority 1
| Number n when n.IsInteger ->
VisualExpression.PositiveInteger n.Numerator
| Number n ->
VisualExpression.Fraction (VisualExpression.PositiveInteger n.Numerator, VisualExpression.PositiveInteger n.Denominator)
|> parenthesis priority 1
| Approximation (Approximation.Real fp) when fp < 0.0 ->
VisualExpression.Negative (VisualExpression.PositiveFloatingPoint (-fp))
|> parenthesis priority 0
| Approximation (Approximation.Real fp) -> VisualExpression.PositiveFloatingPoint fp
| Approximation (Approximation.Complex fp) when fp.IsRealNonNegative() -> VisualExpression.PositiveFloatingPoint fp.Real
| Approximation (Approximation.Complex fp) when fp.IsReal() ->
VisualExpression.Negative (VisualExpression.PositiveFloatingPoint (-fp.Real))
|> parenthesis priority 0
| Approximation (Approximation.Complex fp) -> //when fp.Real = 0.0 && fp.Imaginary < 0.0 ->
failwith "TODO"
| Sum (x::xs) ->
| Identifier s -> VisualExpression.Identifier s
| Constant c -> VisualExpression.Constant c
| ComplexInfinity -> VisualExpression.ComplexInfinity
| PositiveInfinity -> VisualExpression.Infinity
| NegativeInfinity ->
VisualExpression.Negative VisualExpression.Infinity
|> parenthesis priority 0
| Undefined -> VisualExpression.Undefined
convert 1 expression
......@@ -6,7 +6,7 @@ type Function =
| Abs
| Ln | Exp
| Sin | Cos | Tan
| Cot | Sec | Csc
| Cot | Sec | Csc
| Cosh | Sinh | Tanh
| Asin | Acos | Atan
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment