Skip to content

Commit

Permalink
Added LCM & GCM functions
Browse files Browse the repository at this point in the history
  • Loading branch information
65001 committed Apr 8, 2018
1 parent 6d3b289 commit 0b58b40
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
14 changes: 13 additions & 1 deletion AbMath/Utilities/Reverse Polish Notation/Math/DoFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,26 @@ public static double Min(params double[] Arguments)
return Arguments[1];
}

public static double Lcm(params double[] Arguments)
{
return (Arguments[0] * Arguments[1]) / Gcd(Arguments);
}

public static double Gcd(params double[] Arguments)
{
Arguments[0] = Math.Abs(Arguments[0]);
Arguments[1] = Math.Abs(Arguments[1]);
return (Arguments[1] == 0) ? Arguments[0] : Gcd(Arguments[1], Arguments[0] % Arguments[1]);
}

public static double ln(params double[] Arguments)
{
return Math.Log(Arguments[0]);
}

public static double Log(params double[] Arguments)
{
if (Arguments.Length == 0)
if (Arguments.Length == 1)
{
return Math.Log(Arguments[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ namespace AbMath.Utilities
/// Reverse Polish Notation
/// Used for math equations
/// </summary>
///

//TODO
//ABS
//ARCSIN,ARCTAN,ARCCOS
//Random
//Random(Min,Max)
//Generify
//Auto Scaling from decimal to double to Big Integer
//Complex Number Support sqrt(-1) = i

//TODO
//Add a data class so that other classes such as
//the Tokenizer, Shunter, and the like don't need a copy of RPN.

public partial class RPN
{
public enum Assoc { Left, Right };
Expand Down
4 changes: 4 additions & 0 deletions AbMath/Utilities/Reverse Polish Notation/Shunt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public class Shunt : IShunt<string>
Queue<string> Output;
Stack<string> Operator;

//TODO: Implement Variadic Functions
//See http://wcipeg.com/wiki/Shunting_yard_algorithm#Variadic_functions
Stack<int> Arity;

public Shunt(RPN rpn)
{
RPN = rpn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ private void DefaultFunctions()
Compute = new Run(DoFunctions.Round)
});

AddFunction("gcd", new Functions
{
Arguments = 2,
Compute = new Run(DoFunctions.Gcd)
});

AddFunction("lcm", new Functions
{
Arguments = 2,
Compute = new Run(DoFunctions.Lcm)
});

AddFunction("ln", new Functions
{
Arguments = 1,
Expand Down

0 comments on commit 0b58b40

Please sign in to comment.