Skip to content

Commit

Permalink
Removed Arity Feature Flag
Browse files Browse the repository at this point in the history
Fixed Implicit Left Multiplication triggering when a constant function such as pi was after a comma.
Arity should be stable enough to be used in production, removed feature flag.
  • Loading branch information
65001 committed Nov 27, 2018
1 parent d8dc589 commit 81cd306
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 35 deletions.
Binary file modified AbMath/AbMath.dll
Binary file not shown.
7 changes: 0 additions & 7 deletions AbMath/Utilities/Reverse Polish Notation/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ public class Data
public Queue<Term> Polish { get; set; }
public bool ContainsVariables { get; private set; }

/**
* This function enables vardiac functions,
* this currentley makes complex functions not work
*/
public bool Vardiac = false;


public Data(string equation)
{
Equation = equation;
Expand Down
14 changes: 9 additions & 5 deletions AbMath/Utilities/Reverse Polish Notation/Math/DoFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ public static double Round(params double[] Arguments)
return Math.Round(Arguments[0] * Math.Pow(10, digits)) / Math.Pow(10, digits);
}

//Two Arguments
public static double Max(params double[] Arguments)
{
double max = 0;
double max = Arguments[0];
for (int i = 0; i < Arguments.Length; i++)
{
if (Arguments[i] > max)
Expand All @@ -54,11 +53,16 @@ public static double Max(params double[] Arguments)

public static double Min(params double[] Arguments)
{
if (Arguments[0] < Arguments[1])
double min = Arguments[0];
for (int i = 0; i < Arguments.Length; i++)
{
return Arguments[0];
if (Arguments[i] < min)
{
min = Arguments[i];
}
}
return Arguments[1];

return min;
}

public static double Bounded(params double[] Arguments)
Expand Down
55 changes: 32 additions & 23 deletions AbMath/Utilities/Reverse Polish Notation/Shunt.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Text;
using CLI;
Expand Down Expand Up @@ -95,7 +96,7 @@ public Queue<Term> ShuntYard(List<Term> Tokens)
Type = "Implicit Right";
Implicit();
}
else if (Prev.IsRightBracket() && Token.IsFunction())
else if (Prev.IsRightBracket() && Prev.Value != "," && Token.IsFunction())
{
Type = "Implicit Left Functional";
OperatorRule(GenerateMultiply());
Expand Down Expand Up @@ -176,8 +177,15 @@ public Queue<Term> ShuntYard(List<Term> Tokens)
}

Write(arityTables.GenerateFooter());
Write($"Arity Count : {Arity.Count}");
Write($"Arity Peek {Arity.SafePeek()}");
Write("");

if (Arity.Count > 0)
{
throw new InvalidOperationException("Arity not completely assigned");
}

SW.Stop();
Write($"Execution Time {SW.ElapsedMilliseconds}(ms). Elapsed Ticks: {SW.ElapsedTicks}");
Write($"Reverse Polish Notation:\n{Output.Print()}");
Expand Down Expand Up @@ -208,34 +216,17 @@ void RightBracketRule(Term Token)
Term output = Operator.Pop();
//This ensures that only functions
//can have variable number of arguments
if (Data.Vardiac && output.IsFunction() )
if (output.IsFunction() )
{
int args = Arity.Pop();
//TODO Variadic Function
output.Arguments = args;
//In the case of a composite function we must pop

if (args == 0)
{
args = 1;
}

Arity.Push(args);
output.Arguments = Arity.Pop();
}
Output.Enqueue(output);
}

//For functions and composite functions the to work, we must return now.
if (Token.Value == ",")
{
if (Arity.Count == 0)
{
Arity.Push(1);
}
else
{
Arity.Push(Arity.Pop() + 1);
}
Arity.Push(Arity.Pop() + 1);
return;
}

Expand Down Expand Up @@ -295,11 +286,15 @@ bool Chain()
void WriteFunction(Term Function)
{
Operator.Push(Function);
Arity.Push(0);

if (Data.Functions[Function.Value].Arguments > 0)
{
Arity.Push(1);
}
else
{
Arity.Push(0);
}
}

Term GenerateMultiply()
Expand Down Expand Up @@ -328,15 +323,29 @@ void Dump()
throw new ArgumentException("Error: Mismatched Parentheses or Brackets");
}
var output = Operator.Pop();

/*
if (Data.Vardiac && Arity.Count > 0 && peek.IsFunction())
{
//TODO Variadic Function
output.Arguments = Arity.Pop();
}
*/

Output.Enqueue(output);
}

while ( Arity.Count > 0)
{
for (int i = 0; i < (Output.Count - 1); i++)
{
Output.Enqueue( Output.Dequeue() );
}

var foo = Output.Dequeue();
foo.Arguments = Arity.Pop();
Output.Enqueue(foo);
}

}

void Write(string message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ public void Sin()
Assert.AreEqual(1, math.Compute());
}

[Test]
public void SinOfe()
{
RPN test = new RPN("sin(e/2)");
test.Logger += Write;
test.Compute();
PostFix math = new PostFix(test);
Assert.AreEqual(Math.Sin(Math.E/2), math.Compute());
}

[Test]
public void Cos()
{
Expand Down Expand Up @@ -187,6 +197,64 @@ public void ComplexReset()

}

[Test]
public void VardiacMax()
{
RPN test = new RPN("max(1, 2, 3)");

test.Logger += Write;
test.Compute();

PostFix math = new PostFix(test);
Assert.AreEqual(3, math.Compute());
}

[Test]
public void VardiacMin()
{
RPN test = new RPN("min(1, 2, 3)");

test.Logger += Write;
test.Compute();

PostFix math = new PostFix(test);
Assert.AreEqual(1, math.Compute());
}

[Test]
public void VardiacComposite()
{
RPN test = new RPN("sin(min (0, 1) )");

test.Logger += Write;
test.Compute();

PostFix math = new PostFix(test);
Assert.AreEqual(0, math.Compute());
}

[Test]
public void Max()
{
RPN test = new RPN("max(0, 1)");
test.Logger += Write;
test.Compute();

PostFix math = new PostFix(test);
Assert.AreEqual(1, math.Compute());
}

[Test]
public void Min()
{
RPN test = new RPN("min(0, 1)");
test.Logger += Write;
test.Compute();

PostFix math = new PostFix(test);
Assert.AreEqual(0, math.Compute());
}

public void Write(object sender,string Event)
{
Console.WriteLine(Event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ public void VariableMultiplication()
}
}

[Test]
public void ArityConstantMax()
{
RPN test = new RPN("max(1, pi)");
test.Logger += Write;
test.Compute();
Console.WriteLine(test.Polish.Print());
if ("1 pi max" != test.Polish.Print())
{
Assert.Fail();
}
}

[Test]
public void VariableExponents()
{
Expand Down

0 comments on commit 81cd306

Please sign in to comment.