Skip to content

Commit 81cd306

Browse files
committed
Removed Arity Feature Flag
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.
1 parent d8dc589 commit 81cd306

File tree

6 files changed

+122
-35
lines changed

6 files changed

+122
-35
lines changed

AbMath/AbMath.dll

512 Bytes
Binary file not shown.

AbMath/Utilities/Reverse Polish Notation/Data.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,6 @@ public class Data
3333
public Queue<Term> Polish { get; set; }
3434
public bool ContainsVariables { get; private set; }
3535

36-
/**
37-
* This function enables vardiac functions,
38-
* this currentley makes complex functions not work
39-
*/
40-
public bool Vardiac = false;
41-
42-
4336
public Data(string equation)
4437
{
4538
Equation = equation;

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ public static double Round(params double[] Arguments)
3737
return Math.Round(Arguments[0] * Math.Pow(10, digits)) / Math.Pow(10, digits);
3838
}
3939

40-
//Two Arguments
4140
public static double Max(params double[] Arguments)
4241
{
43-
double max = 0;
42+
double max = Arguments[0];
4443
for (int i = 0; i < Arguments.Length; i++)
4544
{
4645
if (Arguments[i] > max)
@@ -54,11 +53,16 @@ public static double Max(params double[] Arguments)
5453

5554
public static double Min(params double[] Arguments)
5655
{
57-
if (Arguments[0] < Arguments[1])
56+
double min = Arguments[0];
57+
for (int i = 0; i < Arguments.Length; i++)
5858
{
59-
return Arguments[0];
59+
if (Arguments[i] < min)
60+
{
61+
min = Arguments[i];
62+
}
6063
}
61-
return Arguments[1];
64+
65+
return min;
6266
}
6367

6468
public static double Bounded(params double[] Arguments)

AbMath/Utilities/Reverse Polish Notation/Shunt.cs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Linq;
45
using System.Numerics;
56
using System.Text;
67
using CLI;
@@ -95,7 +96,7 @@ public Queue<Term> ShuntYard(List<Term> Tokens)
9596
Type = "Implicit Right";
9697
Implicit();
9798
}
98-
else if (Prev.IsRightBracket() && Token.IsFunction())
99+
else if (Prev.IsRightBracket() && Prev.Value != "," && Token.IsFunction())
99100
{
100101
Type = "Implicit Left Functional";
101102
OperatorRule(GenerateMultiply());
@@ -176,8 +177,15 @@ public Queue<Term> ShuntYard(List<Term> Tokens)
176177
}
177178

178179
Write(arityTables.GenerateFooter());
180+
Write($"Arity Count : {Arity.Count}");
181+
Write($"Arity Peek {Arity.SafePeek()}");
179182
Write("");
180183

184+
if (Arity.Count > 0)
185+
{
186+
throw new InvalidOperationException("Arity not completely assigned");
187+
}
188+
181189
SW.Stop();
182190
Write($"Execution Time {SW.ElapsedMilliseconds}(ms). Elapsed Ticks: {SW.ElapsedTicks}");
183191
Write($"Reverse Polish Notation:\n{Output.Print()}");
@@ -208,34 +216,17 @@ void RightBracketRule(Term Token)
208216
Term output = Operator.Pop();
209217
//This ensures that only functions
210218
//can have variable number of arguments
211-
if (Data.Vardiac && output.IsFunction() )
219+
if (output.IsFunction() )
212220
{
213-
int args = Arity.Pop();
214-
//TODO Variadic Function
215-
output.Arguments = args;
216-
//In the case of a composite function we must pop
217-
218-
if (args == 0)
219-
{
220-
args = 1;
221-
}
222-
223-
Arity.Push(args);
221+
output.Arguments = Arity.Pop();
224222
}
225223
Output.Enqueue(output);
226224
}
227225

228226
//For functions and composite functions the to work, we must return now.
229227
if (Token.Value == ",")
230228
{
231-
if (Arity.Count == 0)
232-
{
233-
Arity.Push(1);
234-
}
235-
else
236-
{
237-
Arity.Push(Arity.Pop() + 1);
238-
}
229+
Arity.Push(Arity.Pop() + 1);
239230
return;
240231
}
241232

@@ -295,11 +286,15 @@ bool Chain()
295286
void WriteFunction(Term Function)
296287
{
297288
Operator.Push(Function);
298-
Arity.Push(0);
289+
299290
if (Data.Functions[Function.Value].Arguments > 0)
300291
{
301292
Arity.Push(1);
302293
}
294+
else
295+
{
296+
Arity.Push(0);
297+
}
303298
}
304299

305300
Term GenerateMultiply()
@@ -328,15 +323,29 @@ void Dump()
328323
throw new ArgumentException("Error: Mismatched Parentheses or Brackets");
329324
}
330325
var output = Operator.Pop();
331-
326+
/*
332327
if (Data.Vardiac && Arity.Count > 0 && peek.IsFunction())
333328
{
334329
//TODO Variadic Function
335330
output.Arguments = Arity.Pop();
336331
}
332+
*/
337333

338334
Output.Enqueue(output);
339335
}
336+
337+
while ( Arity.Count > 0)
338+
{
339+
for (int i = 0; i < (Output.Count - 1); i++)
340+
{
341+
Output.Enqueue( Output.Dequeue() );
342+
}
343+
344+
var foo = Output.Dequeue();
345+
foo.Arguments = Arity.Pop();
346+
Output.Enqueue(foo);
347+
}
348+
340349
}
341350

342351
void Write(string message)

Unit Tester/Apportionment/Utilities/Reverse Polish Notation/PostFix.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ public void Sin()
7777
Assert.AreEqual(1, math.Compute());
7878
}
7979

80+
[Test]
81+
public void SinOfe()
82+
{
83+
RPN test = new RPN("sin(e/2)");
84+
test.Logger += Write;
85+
test.Compute();
86+
PostFix math = new PostFix(test);
87+
Assert.AreEqual(Math.Sin(Math.E/2), math.Compute());
88+
}
89+
8090
[Test]
8191
public void Cos()
8292
{
@@ -187,6 +197,64 @@ public void ComplexReset()
187197

188198
}
189199

200+
[Test]
201+
public void VardiacMax()
202+
{
203+
RPN test = new RPN("max(1, 2, 3)");
204+
205+
test.Logger += Write;
206+
test.Compute();
207+
208+
PostFix math = new PostFix(test);
209+
Assert.AreEqual(3, math.Compute());
210+
}
211+
212+
[Test]
213+
public void VardiacMin()
214+
{
215+
RPN test = new RPN("min(1, 2, 3)");
216+
217+
test.Logger += Write;
218+
test.Compute();
219+
220+
PostFix math = new PostFix(test);
221+
Assert.AreEqual(1, math.Compute());
222+
}
223+
224+
[Test]
225+
public void VardiacComposite()
226+
{
227+
RPN test = new RPN("sin(min (0, 1) )");
228+
229+
test.Logger += Write;
230+
test.Compute();
231+
232+
PostFix math = new PostFix(test);
233+
Assert.AreEqual(0, math.Compute());
234+
}
235+
236+
[Test]
237+
public void Max()
238+
{
239+
RPN test = new RPN("max(0, 1)");
240+
test.Logger += Write;
241+
test.Compute();
242+
243+
PostFix math = new PostFix(test);
244+
Assert.AreEqual(1, math.Compute());
245+
}
246+
247+
[Test]
248+
public void Min()
249+
{
250+
RPN test = new RPN("min(0, 1)");
251+
test.Logger += Write;
252+
test.Compute();
253+
254+
PostFix math = new PostFix(test);
255+
Assert.AreEqual(0, math.Compute());
256+
}
257+
190258
public void Write(object sender,string Event)
191259
{
192260
Console.WriteLine(Event);

Unit Tester/Apportionment/Utilities/Reverse Polish Notation/Tokenizer.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ public void VariableMultiplication()
182182
}
183183
}
184184

185+
[Test]
186+
public void ArityConstantMax()
187+
{
188+
RPN test = new RPN("max(1, pi)");
189+
test.Logger += Write;
190+
test.Compute();
191+
Console.WriteLine(test.Polish.Print());
192+
if ("1 pi max" != test.Polish.Print())
193+
{
194+
Assert.Fail();
195+
}
196+
}
197+
185198
[Test]
186199
public void VariableExponents()
187200
{

0 commit comments

Comments
 (0)