git.iko.soy/ikoHSE/SolveAvdoshin
on master help sign in
UI and API for representations (10-13)
commit4109923e5506087f6581e885ce22714030a578eb authorIlya Gulkov <ifranked@gmail.com> dateTue 6 Oct 2015, 15:36 (11 years ago) parentabd8fd5
2 files changed+225−163
SolveAvdoshin/BooleanFunctions.cs+221−163 SolveAvdoshin/SolveAvdoshin.cs+4
SolveAvdoshin/BooleanFunctions.cs+221−163
@@ -108,17 +108,182 @@ namespace SolveAvdoshin
108108
109109 public static void PrintAllDerivatives(int n)
110110 {
111− DerivativePrinting.PrintDerivatives(n);
111+ BooleanFunction f = new BooleanFunction((byte)n);
112+
113+ DerivativePrinting.PrintDerivatives(f);
114+
115+ DerivativePrinting.PrintExpressionsForDerivatives(f, AvdoshinBases[0], AvdoshinVars[0]);
116+
117+ DerivativePrinting.Print2DirectionalDerivatives(f);
118+
119+ DerivativePrinting.PrintExpressionsFor2DirDerivatives(f, AvdoshinBases[0], AvdoshinVars[0]);
112120
113− DerivativePrinting.PrintExpressionsForDerivatives(n, AvdoshinBases[0], AvdoshinVars[0]);
121+ DerivativePrinting.Print3DirectionalDerivative(f);
122+
123+ DerivativePrinting.PrintExpressionFor3DirDerivative(f, AvdoshinBases[0], AvdoshinVars[0]);
124+ }
125+
126+ public static void PrintAllRepresentations(int n)
127+ {
128+ BooleanFunction f = new BooleanFunction((byte)n);
114129
115− DerivativePrinting.Print2DirectionalDerivatives(n);
130+ RepresentationPrinting.PrintMaclauren1XorAnd(f);
116131
117− DerivativePrinting.PrintExpressionsFor2DirDerivatives(n, AvdoshinBases[0], AvdoshinVars[0]);
132+ RepresentationPrinting.PrintTailor1XorAnd(f);
118133
119− DerivativePrinting.Print3DirectionalDerivative(n);
134+ RepresentationPrinting.PrintMaclauren0EqOr(f);
120135
121− DerivativePrinting.PrintExpressionFor3DirDerivative(n, AvdoshinBases[0], AvdoshinVars[0]);
136+ RepresentationPrinting.PrintTailor0EqOr(f);
137+ }
138+ }
139+
140+ static class DerivativePrinting
141+ {
142+ static readonly BooleanVariable[][] VarLists = new BooleanVariable[][] {
143+ new BooleanVariable[] { BooleanVariable.A, },
144+ new BooleanVariable[] { BooleanVariable.B, },
145+ new BooleanVariable[] { BooleanVariable.C, },
146+ new BooleanVariable[] { BooleanVariable.A, BooleanVariable.B, },
147+ new BooleanVariable[] { BooleanVariable.B, BooleanVariable.C, },
148+ new BooleanVariable[] { BooleanVariable.A, BooleanVariable.C, },
149+ new BooleanVariable[] { BooleanVariable.A, BooleanVariable.B, BooleanVariable.C, },
150+ };
151+
152+ static readonly string[] DerivStrings = new string[] {
153+ "F'_A",
154+ "F'_B",
155+ "F'_C",
156+ "F''_AB",
157+ "F''_BC",
158+ "F''_AC",
159+ "F'''_ABC",
160+ };
161+
162+ static readonly string[] DirDerivStrings = new string[] {
163+ "F'_A",
164+ "F'_B",
165+ "F'_C",
166+ "F'_(A,B)",
167+ "F'_(B,C)",
168+ "F'_(A,C)",
169+ "F'_(A,B,C)",
170+ };
171+
172+ public static void PrintJustTruthTable(BooleanFunction f)
173+ {
174+ Console.WriteLine((new BooleanFunction(0x0F)).ToString("A"));
175+ Console.WriteLine((new BooleanFunction(0x33)).ToString("B"));
176+ Console.WriteLine((new BooleanFunction(0x55)).ToString("C"));
177+
178+ Console.WriteLine(f.ToString("F"));
179+ }
180+
181+ public static void PrintDerivatives(BooleanFunction f)
182+ {
183+ PrintJustTruthTable(f);
184+
185+ for(int i = 0; i < 7; i++) {
186+ Console.WriteLine(f.Deriv(VarLists[i]).ToString(DerivStrings[i]));
187+ }
188+
189+ Console.WriteLine();
190+ }
191+
192+ public static void Print2DirectionalDerivatives(BooleanFunction f)
193+ {
194+ PrintJustTruthTable(f);
195+
196+ for(int i = 3; i <= 5; i++) {
197+ Console.WriteLine(f.DirectionalDeriv(VarLists[6]).ToString(DirDerivStrings[i]));
198+ }
199+
200+ Console.WriteLine();
201+ }
202+
203+ public static void Print3DirectionalDerivative(BooleanFunction f)
204+ {
205+ PrintJustTruthTable(f);
206+
207+ Console.WriteLine(f.DirectionalDeriv(VarLists[6]).ToString(DirDerivStrings[6]));
208+
209+ Console.WriteLine();
210+ }
211+
212+ public static void PrintExpressionsForDerivatives(BooleanFunction f, BooleanOperation[] ops, BooleanVariable[] vars)
213+ {
214+ for(int i = 0; i < 7; i++) {
215+ Console.WriteLine(DerivStrings[i] + " = " + BooleanExpression.FindMininalExpressionInBasis(
216+ f.Deriv(VarLists[i]).Eval(), ops, vars).ToString());
217+ }
218+
219+ Console.WriteLine();
220+ }
221+
222+ public static void PrintExpressionsFor2DirDerivatives(BooleanFunction f, BooleanOperation[] ops, BooleanVariable[] vars)
223+ {
224+ for(int i = 3; i <= 5; i++) {
225+ Console.WriteLine(DirDerivStrings[i] + " = " + BooleanExpression.FindMininalExpressionInBasis(f.DirectionalDeriv(VarLists[i]).Eval(), ops, vars).ToString());
226+ }
227+
228+ Console.WriteLine();
229+ }
230+
231+ public static void PrintExpressionFor3DirDerivative(BooleanFunction f, BooleanOperation[] ops, BooleanVariable[] vars)
232+ {
233+ Console.WriteLine(DirDerivStrings[6] + " = " + BooleanExpression.FindMininalExpressionInBasis(
234+ f.DirectionalDeriv(VarLists[6]).Eval(), ops, vars).ToString());
235+
236+ Console.WriteLine();
237+ }
238+ }
239+
240+ static class RepresentationPrinting
241+ {
242+ static readonly string[] ReprStrings = new string[] {
243+ "(0,0,0)",
244+ "(0,0,1)",
245+ "(0,1,0)",
246+ "(0,1,1)",
247+ "(1,0,0)",
248+ "(1,0,1)",
249+ "(1,1,0)",
250+ "(1,1,1)",
251+ };
252+
253+ public static void PrintMaclauren1XorAnd(BooleanFunction f)
254+ {
255+ Console.WriteLine("F(A,B,C) = " +
256+ f.GetTailor(0, BooleanVariable.One, BooleanOperation.Xor, BooleanOperation.And));
257+
258+ Console.WriteLine();
259+ }
260+
261+ public static void PrintTailor1XorAnd(BooleanFunction f)
262+ {
263+ for(int i = 0; i < 8; i++) {
264+ Console.WriteLine(ReprStrings[i] + ": F(A,B,C) = " +
265+ f.GetTailor(i, BooleanVariable.One, BooleanOperation.Xor, BooleanOperation.And));
266+ }
267+
268+ Console.WriteLine();
269+ }
270+
271+ public static void PrintMaclauren0EqOr(BooleanFunction f)
272+ {
273+ Console.WriteLine("F(A,B,C) = " +
274+ f.GetTailor(0, BooleanVariable.Zero, BooleanOperation.Eq, BooleanOperation.Or));
275+
276+ Console.WriteLine();
277+ }
278+
279+ public static void PrintTailor0EqOr(BooleanFunction f)
280+ {
281+ for(int i = 0; i < 8; i++) {
282+ Console.WriteLine(ReprStrings[i] + ": F(A,B,C) = " +
283+ f.GetTailor(i, BooleanVariable.Zero, BooleanOperation.Eq, BooleanOperation.Or));
284+ }
285+
286+ Console.WriteLine();
122287 }
123288 }
124289
@@ -161,6 +326,11 @@ namespace SolveAvdoshin
161326 return caption.PadRight(10) + " " + String.Join(" ", digits);
162327 }
163328
329+ public BooleanExpression GetTailor(int point, BooleanVariable one, BooleanOperation xor, BooleanOperation and)
330+ {
331+ return new OpExpression(BooleanOperation.NOR, BooleanVariable.A, BooleanVariable.B);
332+ }
333+
164334 public BooleanFunction DirectionalDeriv(params BooleanVariable[] variables)
165335 {
166336 Dictionary<BooleanVariable, bool> values;
@@ -294,162 +464,7 @@ namespace SolveAvdoshin
294464 return (byte)res;
295465 }
296466 }
297−
298− static class DerivativePrinting
299− {
300− static readonly BooleanVariable[][] VarLists = new BooleanVariable[][] {
301− new BooleanVariable[] { BooleanVariable.A, },
302− new BooleanVariable[] { BooleanVariable.B, },
303− new BooleanVariable[] { BooleanVariable.C, },
304− new BooleanVariable[] { BooleanVariable.A, BooleanVariable.B, },
305− new BooleanVariable[] { BooleanVariable.B, BooleanVariable.C, },
306− new BooleanVariable[] { BooleanVariable.A, BooleanVariable.C, },
307− new BooleanVariable[] { BooleanVariable.A, BooleanVariable.B, BooleanVariable.C, },
308− };
309−
310− static readonly string[] DerivStrings = new string[] {
311− "F'_A",
312− "F'_B",
313− "F'_C",
314− "F''_AB",
315− "F''_BC",
316− "F''_AC",
317− "F'''_ABC",
318− };
319−
320− static readonly string[] DirDerivStrings = new string[] {
321− "F'_A",
322− "F'_B",
323− "F'_C",
324− "F'_(A,B)",
325− "F'_(B,C)",
326− "F'_(A,C)",
327− "F'_(A,B,C)",
328− };
329−
330− public static void PrintJustTruthTable(BooleanFunction f)
331− {
332− Console.WriteLine((new BooleanFunction(0x0F)).ToString("A"));
333− Console.WriteLine((new BooleanFunction(0x33)).ToString("B"));
334− Console.WriteLine((new BooleanFunction(0x55)).ToString("C"));
335−
336− Console.WriteLine(f.ToString("F"));
337− }
338−
339− public static void PrintDerivatives(int n)
340− {
341− BooleanFunction f = new BooleanFunction((byte)n);
342−
343− PrintJustTruthTable(f);
344−
345− for(int i = 0; i < 7; i++) {
346− Console.WriteLine(f.Deriv(VarLists[i]).ToString(DerivStrings[i]));
347− }
348−
349− Console.WriteLine();
350− }
351−
352− public static void Print2DirectionalDerivatives(int n)
353− {
354− BooleanFunction f = new BooleanFunction((byte)n);
355−
356− PrintJustTruthTable(f);
357−
358− for(int i = 3; i <= 5; i++) {
359− Console.WriteLine(f.DirectionalDeriv(VarLists[6]).ToString(DirDerivStrings[i]));
360− }
361−
362− Console.WriteLine();
363− }
364−
365− public static void Print3DirectionalDerivative(int n)
366− {
367− BooleanFunction f = new BooleanFunction((byte)n);
368−
369− PrintJustTruthTable(f);
370−
371− Console.WriteLine(f.DirectionalDeriv(VarLists[6]).ToString(DirDerivStrings[6]));
372−
373− Console.WriteLine();
374− }
375−
376− public static void PrintExpressionsForDerivatives(int n, BooleanOperation[] ops, BooleanVariable[] vars)
377− {
378− BooleanFunction f = new BooleanFunction((byte)n);
379−
380− for(int i = 0; i < 7; i++) {
381− Console.WriteLine(DerivStrings[i] + " = " + BooleanExpression.FindMininalExpressionInBasis(
382− f.Deriv(VarLists[i]).Eval(), ops, vars).ToString());
383− }
384−
385− Console.WriteLine();
386− }
387−
388− public static void PrintExpressionsFor2DirDerivatives(int n, BooleanOperation[] ops, BooleanVariable[] vars)
389− {
390− BooleanFunction f = new BooleanFunction((byte)n);
391−
392− for(int i = 3; i <= 5; i++) {
393− Console.WriteLine(DirDerivStrings[i] + " = " + BooleanExpression.FindMininalExpressionInBasis(f.DirectionalDeriv(VarLists[i]).Eval(), ops, vars).ToString());
394− }
395−
396− Console.WriteLine();
397− }
398−
399− public static void PrintExpressionFor3DirDerivative(int n, BooleanOperation[] ops, BooleanVariable[] vars)
400− {
401− BooleanFunction f = new BooleanFunction((byte)n);
402−
403− Console.WriteLine(DirDerivStrings[6] + " = " + BooleanExpression.FindMininalExpressionInBasis(
404− f.DirectionalDeriv(VarLists[6]).Eval(), ops, vars).ToString());
405−
406− Console.WriteLine();
407− }
408− }
409−
410− class ImprovisedPriorityQueue<T>
411− {
412− public int Capacity, Count;
413− Queue<T>[] Queues;
414−
415− public ImprovisedPriorityQueue(int capacity)
416− {
417− Capacity = capacity;
418− Count = 0;
419−
420− Queues = new Queue<T>[capacity + 1];
421−
422− for(int i = 1; i <= capacity; i++) {
423− Queues[i] = new Queue<T>();
424− }
425− }
426−
427− public bool TryEnqueue(T item, int priority)
428− {
429− if(priority < 1 || priority > Capacity)
430− return false;
431−
432− Count += 1;
433− Queues[priority].Enqueue(item);
434− return true;
435− }
436−
437− public T Dequeue()
438− {
439− if(Count == 0)
440− throw new Exception("ImprovisedPriorityQueue is empty");
441−
442− for(int i = 1; i <= Capacity; i++) {
443− if(Queues[i].Count != 0) {
444− Count -= 1;
445− return Queues[i].Dequeue();
446− }
447− }
448−
449− throw new Exception("ImprovisedPriorityQueue is empty");
450− }
451− }
452−
467+
453468 class CouldntFindExpressionException : Exception
454469 {
455470
@@ -463,7 +478,7 @@ namespace SolveAvdoshin
463478 public static readonly string[] OpSymbols = new string[] { "0", "↓", "=/>", "(!A)", "</=", "(!B)", "⨁", "|", "&", "==", "(B)", "<=", "(A)", "=>", "+", "1", };
464479
465480 abstract public byte Eval();
466− abstract new public string ToString();
481+ abstract override public string ToString();
467482 abstract public BooleanExpression Clone();
468483 abstract public int getSize();
469484
@@ -491,6 +506,49 @@ namespace SolveAvdoshin
491506 return OpSymbols[(int)op];
492507 }
493508
509+ class ImprovisedPriorityQueue<T>
510+ {
511+ public int Capacity, Count;
512+ Queue<T>[] Queues;
513+
514+ public ImprovisedPriorityQueue(int capacity)
515+ {
516+ Capacity = capacity;
517+ Count = 0;
518+
519+ Queues = new Queue<T>[capacity + 1];
520+
521+ for(int i = 1; i <= capacity; i++) {
522+ Queues[i] = new Queue<T>();
523+ }
524+ }
525+
526+ public bool TryEnqueue(T item, int priority)
527+ {
528+ if(priority < 1 || priority > Capacity)
529+ return false;
530+
531+ Count += 1;
532+ Queues[priority].Enqueue(item);
533+ return true;
534+ }
535+
536+ public T Dequeue()
537+ {
538+ if(Count == 0)
539+ throw new Exception("ImprovisedPriorityQueue is empty");
540+
541+ for(int i = 1; i <= Capacity; i++) {
542+ if(Queues[i].Count != 0) {
543+ Count -= 1;
544+ return Queues[i].Dequeue();
545+ }
546+ }
547+
548+ throw new Exception("ImprovisedPriorityQueue is empty");
549+ }
550+ }
551+
494552 public static BooleanExpression FindMininalExpressionInBasis(int n, BooleanOperation[] ops, BooleanVariable[] vars)
495553 {
496554 var queue = new ImprovisedPriorityQueue<BooleanExpression>(20);
SolveAvdoshin/SolveAvdoshin.cs+4
@@ -109,6 +109,10 @@ namespace SolveAvdoshin
109109 Console.WriteLine("\n\n4-9.\n");
110110
111111 BooleanFunctions.PrintAllDerivatives(answer);
112+
113+ Console.WriteLine("\n10-13.\n");
114+
115+ BooleanFunctions.PrintAllRepresentations(answer);
112116 }
113117 catch(FormatException e) {
114118 Console.WriteLine("Osheebka: " + e.Message);