domain transformations
TRANSCRIPT
![Page 2: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/2.jpg)
Programming tasks can be
Challenging – unique problems and algorithmsDistinctly average – typical ‘working to spec’ Mind-numbingly boring – obvious stuff , manual
translation
1-to-1 translation from one domain/format to another is boring, wasteful and should (where possible) be automated.
In my case:Math in Word/MathTypeCalculations in Excel
LEVELS OF CHALLENGE
![Page 3: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/3.jpg)
XML-like notation Tags denote mathematical
constructs Special HTML entities used
for mathematical symbolsTwo varieties
Content (uncommon, easier to interpret, constrained)
Presentation (harder to interpret, noisy, much more common)
Supported by Word, MathType, Mathematica, Maple, etc.
<mml:math> <mml:mi>a</mml:mi> <mml:msup> <mml:mi>x</mml:mi> <mml:mn>2</mml:mn> </mml:msup> <mml:mo>+</mml:mo> <mml:mn>2</mml:mn> <mml:mi>x</mml:mi></mml:math>
MATHEMATICAL MARK-UP LANGUAGE
![Page 4: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/4.jpg)
Discriminated union Mutually recursive No tag attributes
Union case contains Constant
Mn(42) String literal
Mi("sin") Expression tuple
e.g. Mfrac(Mn(1),Mn(2))
Expression liste.g. Mtd([…])
type Expression = | Math of Expression list | Menclose of Expression list | Mrow of Expression list | Mi of string | MiNormal of string | Mo of string | Mtd of Expression list | Mtr of Expression list | Mn of string | Mroot of Expression * Expression | Mfrac of Expression * Expression | Msub of Expression * Expression | Msup of Expression * Expression
(etc.)
PARSING MATHML
![Page 5: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/5.jpg)
1. Precompute each union case constructor into a maplet cases = Refl ection.FSharpType.GetUnionCases (typeof<Expression>) |> Array.map(fun f -> (f.Name, Refl ection.FSharpValue.PreComputeUnionConstructor(f))) |> Map.ofArray
2. Parse text as XML Use a preamble to wire up MathML DTDs
3. Try to recursively create each case let ctor = cases.Item name (ctor parameters) :?> Expression For 1..3 parameters, we
Try it as a tuple first; and If failed, try as a list
4. If failed, substitute a 0 (zero) with a comment0 (* sorry, supporting integrals is just too hard *)
PARSING ALGORITHM
![Page 6: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/6.jpg)
42Depends on user
type42.0f (single)42.0 (double)42.0M (decimal)
Attempt to parse eng. notationE.g.
CONSTANT
Mn(42)
![Page 7: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/7.jpg)
𝜋User has to hint if
π is a variable or 3.14Math.PI
Constant calculations not precomputed Math.PI/2.0
User can avoid Greek identifiers rho
SYMBOLS
Mi(“π”)
![Page 8: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/8.jpg)
e, E or ⅇ
Different representationsSome apps use ⅇ
Again, user has to hint if it’s constant
Also, Math.Exp(x)
MORE SYMBOLS!
Mi(<any ‘e’>)
![Page 9: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/9.jpg)
abc No perfect solution
User can hint if vars are all single-letter
May need to inject multiplicationsa*b*c
IMPLIED MULTIPLICATION
Mi(“a”) :: Mi(“b”) :: Mi(“c”)
Mi(“abc”)
![Page 10: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/10.jpg)
𝑥2 , 𝑥𝑛 , 𝑥32We use
_ for subscript__ for superscript
Outputx*xMath.Pow(x, n)x_3__2
SUBSCRIPTS AND SUPERSCRIPTS
Use <msub> and <msup>
![Page 11: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/11.jpg)
𝑎𝑥6+𝑏𝑥3Math.Pow
ineffi cient for integral powers
If power is integralUser can inline up to some threshold x*x*x
Custom function with multiplications My.IntPow(x,8)
POWER INLINING
Msup(Mi(x),Mn(6))
![Page 12: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/12.jpg)
cheaper than pow
reduced by a pow
POWER INLINING IDEAS
![Page 13: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/13.jpg)
sin 2 𝑥MiNormal is
synthetic <mi
mathvariant=normal>
Function tablesMath.Sin
Magic function application operator
Some functions will be missingLeave as-is
FUNCTIONS
Mrow( MiNormal(“sin”) :: Mo() :: Mn(2) :: Mi(“x”))
![Page 14: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/14.jpg)
faster but confusing
speed of div ≈ sqrt
where
Precalculation
Const caches E.g., etc.
Subexpression extraction
POSSIBLE OPTIMIZATIONS
Add, Sub Mul Div,
Sqrt Pow
![Page 15: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/15.jpg)
Discriminated union Much easier code structuring
Pattern matching More concise, constrained switch statement
| "E" | " " | "e" when settings.EIsConstantⅇ Identify complex patterns
| Mo(op) :: (UnitOfMeasureBraces(units) as uu) :: t
Partial/recursive patterns Help us get rid of ‘noise’, e.g. <mrow> let rec (|Simple|_|) = function | SimpleAndPhysical x -> Some x | Mrow(content) -> match content with | Simple h :: [] -> Some h | _ -> None | _ -> None
Custom operators let (<@>) (dictName:Map<_,_>) keyName = if dictName.ContainsKey keyName then dictName.[keyName] else keyName
WHY F#?
![Page 16: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/16.jpg)
The Good
Drastically saves time
Reduces programmer error
Performs optimizations!
The Bad
Easy to break, lots of special cases
Doesn’t support some common constructs, e.g.
RESULTS
![Page 17: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/17.jpg)
Same idea butGot an existing
parser (C#)Better structuredAnalysis in C# (ugly)
More scope>1 available calculation
Web ServicesUI abstraction
Excel
C#, F#NMath
Code Web Svc
C++Boost
iPadapps
AND WHAT OF EXCEL?
![Page 18: Domain Transformations](https://reader035.vdocuments.net/reader035/viewer/2022062319/558d451fd8b42a45318b46b9/html5/thumbnails/18.jpg)
THE END
ASK QUESTIONS OR I’LL LEAVE
My domain transformers: MathSharp: http://activemesa.com/mathsharp X2C (Excel to Code) : http://activemesa.com/x2c
Contacts [email protected] @dnesteruk Skype: dmitri.nesteruk