Skip to content

Commit 0b58b40

Browse files
committed
Added LCM & GCM functions
1 parent 6d3b289 commit 0b58b40

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

AbMath/Utilities/Reverse Polish Notation/Math/DoFunctions.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,26 @@ public static double Min(params double[] Arguments)
5252
return Arguments[1];
5353
}
5454

55+
public static double Lcm(params double[] Arguments)
56+
{
57+
return (Arguments[0] * Arguments[1]) / Gcd(Arguments);
58+
}
59+
60+
public static double Gcd(params double[] Arguments)
61+
{
62+
Arguments[0] = Math.Abs(Arguments[0]);
63+
Arguments[1] = Math.Abs(Arguments[1]);
64+
return (Arguments[1] == 0) ? Arguments[0] : Gcd(Arguments[1], Arguments[0] % Arguments[1]);
65+
}
66+
5567
public static double ln(params double[] Arguments)
5668
{
5769
return Math.Log(Arguments[0]);
5870
}
5971

6072
public static double Log(params double[] Arguments)
6173
{
62-
if (Arguments.Length == 0)
74+
if (Arguments.Length == 1)
6375
{
6476
return Math.Log(Arguments[0]);
6577
}

AbMath/Utilities/Reverse Polish Notation/Reverse Polish Notation.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ namespace AbMath.Utilities
99
/// Reverse Polish Notation
1010
/// Used for math equations
1111
/// </summary>
12+
///
13+
14+
//TODO
15+
//ABS
16+
//ARCSIN,ARCTAN,ARCCOS
17+
//Random
18+
//Random(Min,Max)
19+
//Generify
20+
//Auto Scaling from decimal to double to Big Integer
21+
//Complex Number Support sqrt(-1) = i
22+
23+
//TODO
24+
//Add a data class so that other classes such as
25+
//the Tokenizer, Shunter, and the like don't need a copy of RPN.
26+
1227
public partial class RPN
1328
{
1429
public enum Assoc { Left, Right };

AbMath/Utilities/Reverse Polish Notation/Shunt.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public class Shunt : IShunt<string>
1919
Queue<string> Output;
2020
Stack<string> Operator;
2121

22+
//TODO: Implement Variadic Functions
23+
//See http://wcipeg.com/wiki/Shunting_yard_algorithm#Variadic_functions
24+
Stack<int> Arity;
25+
2226
public Shunt(RPN rpn)
2327
{
2428
RPN = rpn;

AbMath/Utilities/Reverse Polish Notation/Startup/Default Functions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ private void DefaultFunctions()
5050
Compute = new Run(DoFunctions.Round)
5151
});
5252

53+
AddFunction("gcd", new Functions
54+
{
55+
Arguments = 2,
56+
Compute = new Run(DoFunctions.Gcd)
57+
});
58+
59+
AddFunction("lcm", new Functions
60+
{
61+
Arguments = 2,
62+
Compute = new Run(DoFunctions.Lcm)
63+
});
64+
5365
AddFunction("ln", new Functions
5466
{
5567
Arguments = 1,

0 commit comments

Comments
 (0)