Skip to content

Commit 4e22dbe

Browse files
committed
2 parents f66fe1c + 78a2260 commit 4e22dbe

File tree

1 file changed

+136
-2
lines changed

1 file changed

+136
-2
lines changed

README.md

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,138 @@
11
Mathos Parser
22
=============
3-
Mathos Parser (along with Mathos Parser Portable, and Expression Compiler) are currently transferred from CodePlex to GitHub. The old page can be found below:
4-
* http://mathosparser.codeplex.com/
3+
**Mathos Parser** is a mathematical expression parser, built on top of the .NET Framework, which allows you to parse all kinds of mathematical expressions, and in addition, add your own custom functions, operators, and variables (see the online demo).
4+
5+
##Features
6+
7+
* Parse all kinds of mathematical expressions.
8+
* Add conditional statements.
9+
* Customize operators. Add/edit existing operators, change behaviour of operators.
10+
* Programatically add variables before and on the run time.
11+
* Custom own functions with almost unlimited amount of arguments.
12+
* Using trig functions: sine, cosine, tangents, and also: arc sine, arc cosine, arc tangent.
13+
* Supports almost all of the functions available in System.Math, such as: pow, round, sqrt, rem, abs, and more!
14+
* Culture independent. No matter on what machine the library is being used - the same configurations for everyone.
15+
* Decimal operations.
16+
* And much more!
17+
18+
##Introduction
19+
20+
Mathos Parser is a part of Mathos Project, a project that provides useful methods, structures, etc, to make the life a little bit easier! This math parser is fully independent of Mathos project, so you can just use this library to archive a powerful math parsing experience.
21+
22+
##How to use
23+
24+
It's really easy to use and understand this math parser. In this topic I will try to show you some (not all) key features of this library.
25+
26+
* Adding a custom operator.
27+
* Adding a custom function.
28+
* Functions with more than one argument.
29+
* Programatically add variables.
30+
31+
###Adding custom operator
32+
33+
````
34+
// declaring the parser
35+
MathParser parser = new MathParser();
36+
37+
//customize the operator list
38+
parser.OperatorList = new List<string>() { "^", "%", "*", ":", "/", "+", "-", ">", "<", "=" };
39+
40+
// adding sqrt to the OperatorAction list
41+
parser.OperatorAction.Add("^", delegate(decimal numA, decimal numB)
42+
{
43+
return (decimal)Math.Pow((double)numA, (double)numB);
44+
});
45+
46+
// parsing and comparing
47+
Assert.IsTrue(parser.Parse("3^2") == (decimal)Math.Pow (3,2));
48+
````
49+
###Adding custom function
50+
````
51+
public void CustomFunctions()
52+
{
53+
/*
54+
* This test demonstrates three ways of adding a function
55+
* to the Math Parser
56+
*
57+
* 1) directly pointing to the function
58+
* 2) lambda expression
59+
* 3) anonymous method
60+
*/
61+
62+
MathParser parser = new MathParser();
63+
64+
//for long functions
65+
parser.LocalFunctions.Add("numberTimesTwo", NumberTimesTwoCustomFunction); // adding the function
66+
decimal resultA = parser.Parse("numberTimesTwo(3)");
67+
68+
//for short functions, use lambda expression, or anonymous method
69+
// 1) using lambda epxression (recommended)
70+
parser.LocalFunctions.Add("square", x => x[0] * x[0]);
71+
decimal resultB = parser.Parse("square(4)");
72+
73+
// 2) using anonymous method
74+
parser.LocalFunctions.Add("cube", delegate(decimal[] x)
75+
{
76+
return x[0] * x[0] * x[0];
77+
});
78+
decimal resultC = parser.Parse("cube(2)");
79+
80+
}
81+
public decimal NumberTimesTwoCustomFunction(decimal[] input)
82+
{
83+
return input[0] * 2;
84+
}
85+
````
86+
###Functions with more than one operator
87+
````
88+
/*
89+
* This example demonstrates the "anonymous method" way of adding
90+
* a function that can take more than one agument.
91+
*/
92+
93+
MathParser parser = new MathParser();
94+
95+
//for long functions
96+
parser.LocalFunctions.Add("log", delegate(decimal[] input) // adding the function
97+
{
98+
// input[0] is the number
99+
// input[1] is the base
100+
101+
if (input.Length == 1)
102+
{
103+
return (decimal)Math.Log((double)input[0]);
104+
}
105+
else if (input.Length == 2)
106+
{
107+
return (decimal)Math.Log((double)input[0], (double)input[1]);
108+
}
109+
else
110+
{
111+
return 0; // false
112+
}
113+
});
114+
115+
decimal resultA = parser.Parse("log(2)");
116+
decimal resultB = parser.Parse("log(2,3)");
117+
````
118+
###Programatically add variables
119+
```
120+
/*
121+
* when parsing an expression that requires
122+
* for instance a variable name declaration
123+
* or change, use ProgramaticallyParse().
124+
*/
125+
MathParser parser = new MathParser();
126+
127+
// first way, using let varname = value
128+
decimal resultA = parser.ProgrammaticallyParse("let a = 2pi");
129+
Assert.IsTrue (parser.Parse ("a") == (decimal)Math.PI*2);
130+
131+
// second way, using varname := value
132+
decimal resultC = parser.ProgrammaticallyParse("b := 20");
133+
Assert.IsTrue(parser.Parse("b") == 20);
134+
135+
// third way, using let varname be value
136+
decimal resultD = parser.ProgrammaticallyParse("let c be 25");
137+
Assert.IsTrue(resultD == 25);
138+
```

0 commit comments

Comments
 (0)