13,864,078 members

#### Stats

84K views
92 bookmarked
Posted 19 Sep 2012
Licenced CPOL

# Yet Another Math Parser (YAMP)

, 30 Sep 2012
Constructing a fast math parser using Reflection to do numerics like Matlab.
 ```using System; namespace YAMP { class RandnFunction : ArgumentFunction { static readonly Random ran = new Random(); static bool buffered = false; static double buffer; public Value Function() { return new ScalarValue(Gaussian()); } public Value Function(ScalarValue dim) { var k = (int)dim.Value; if(k <= 1) return new ScalarValue(Gaussian()); var m = new MatrixValue(k, k); for(var i = 1; i <= k; i++) for(var j = 1; j <= k; j++) m[j, i] = new ScalarValue(Gaussian()); return m; } public Value Function(ScalarValue rows, ScalarValue cols) { var k = (int)rows.Value; var l = (int)cols.Value; var m = new MatrixValue(k, l); for(var i = 1; i <= l; i++) for(var j = 1; j <= k; j++) m[j, i] = new ScalarValue(Gaussian()); return m; } double Gaussian() { return Gaussian(1.0, 0.0); } double Gaussian(double sigma, double mu) { if(buffered) { buffered = false; return mu + sigma * buffer; } double s, u, v; do { u = ran.NextDouble() * 2.0 - 1.0; v = ran.NextDouble() * 2.0 - 1.0; s = u * u + v * v; } while(s == 0.0 || s >= 1.0); double a = Math.Sqrt(-2.0 * Math.Log(s) / s); double z = u * a; buffer = v * a; buffered = true; return mu + sigma * z; } } } ```

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

## Share

 Architect Germany
Florian lives in Munich, Germany. He started his programming career with Perl. After programming C/C++ for some years he discovered his favorite programming language C#. He did work at Siemens as a programmer until he decided to study Physics.

During his studies he worked as an IT consultant for various companies. After graduating with a PhD in theoretical particle Physics he is working as a senior technical consultant in the field of home automation and IoT.

Florian has been giving lectures in C#, HTML5 with CSS3 and JavaScript, software design, and other topics. He is regularly giving talks at user groups, conferences, and companies. He is actively contributing to open-source projects. Florian is the maintainer of AngleSharp, a completely managed browser engine.