Exemplo n.º 1
0
 public TertiaryOpExpression(BooleanFunction f, BooleanExpression first, BooleanExpression second, BooleanExpression third)
 {
     F      = f;
     First  = first;
     Second = second;
     Third  = third;
 }
Exemplo n.º 2
0
        public static bool CheckMonotony(BooleanFunction f, bool verbose = false)
        {
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (!AreAdjacent(i, j) || !IsLessThan(i, j))
                    {
                        continue;
                    }

                    if (f.EvalAt(i) > f.EvalAt(j))
                    {
                        if (verbose)
                        {
                            Console.WriteLine("F ∉ T_<=, т.к. F({0}) > F({1})", PointAsString(i), PointAsString(j));
                        }

                        return(false);
                    }
                }
            }

            if (verbose)
            {
                Console.WriteLine("F ∈ T_<=, т.к. я на всех наборах проверял, честно!");
            }

            return(true);
        }
Exemplo n.º 3
0
        public BooleanFunction DirectionalDeriv(params BooleanVariable[] variables)
        {
            Dictionary <BooleanVariable, bool> values;

            int res = 0;

            for (int i = 0; i < 8; i++)
            {
                res *= 2;

                values = new Dictionary <BooleanVariable, bool> {
                    { BooleanVariable.A, i / 4 == 1 },
                    { BooleanVariable.B, i / 2 % 2 == 1 },
                    { BooleanVariable.C, i % 2 == 1 },
                };

                BooleanFunction leftOp  = this;
                BooleanFunction rightOp = this;

                foreach (var variable in variables)
                {
                    leftOp  = leftOp.FixVar(variable, values[variable] ? 1 : 0);
                    rightOp = rightOp.FixVar(variable, values[variable] ? 0 : 1);
                }

                res += leftOp.EvalAt(i) ^ rightOp.EvalAt(i);
            }

            return(new BooleanFunction((byte)res));
        }
Exemplo n.º 4
0
        public static void PrintMaclauren0EqOr(int n)
        {
            BooleanFunction f = new BooleanFunction((byte)n);

            Console.WriteLine("F(A,B,C) = " + f.GetTailorStringEq(7));

            Console.WriteLine();
        }
Exemplo n.º 5
0
        static void PrintJustTruthTable(BooleanFunction f)
        {
            Console.WriteLine((new BooleanFunction(0x0F)).ToString("A"));
            Console.WriteLine((new BooleanFunction(0x33)).ToString("B"));
            Console.WriteLine((new BooleanFunction(0x55)).ToString("C"));

            Console.WriteLine(f.ToString("F"));
        }
Exemplo n.º 6
0
        public static void PrintExpressionFor3DirDerivative(int n)
        {
            BooleanFunction f = new BooleanFunction((byte)n);

            Console.WriteLine(DirDerivStrings[6] + " = " + BooleanExpression.FindMininalExpressionInBasis(
                                  f.DirectionalDeriv(VarLists[6]).Eval(), AvdoshinBases[0], AvdoshinVars[0]).ToString());

            Console.WriteLine();
        }
Exemplo n.º 7
0
        public static void Print3DirectionalDerivative(int n)
        {
            BooleanFunction f = new BooleanFunction((byte)n);

            PrintJustTruthTable(f);

            Console.WriteLine(f.DirectionalDeriv(VarLists[6]).ToString(DirDerivStrings[6]));

            Console.WriteLine();
        }
Exemplo n.º 8
0
        public BooleanFunction Deriv(params BooleanVariable[] variables)
        {
            BooleanFunction res = this;

            foreach (var variable in variables)
            {
                res = new BooleanFunction((byte)(res.FixVar(variable, 0).Eval() ^ res.FixVar(variable, 1).Eval()));
            }

            return(res);
        }
Exemplo n.º 9
0
        public static void PrintTailor0EqOr(int n)
        {
            BooleanFunction f = new BooleanFunction((byte)n);

            for (int i = 0; i < 8; i++)
            {
                Console.WriteLine(ReprStrings[i] + ": F(A,B,C) = " + f.GetTailorStringEq(i));
            }

            Console.WriteLine();
        }
Exemplo n.º 10
0
        public static void PrintExpressionsFor2DirDerivatives(int n)
        {
            BooleanFunction f = new BooleanFunction((byte)n);

            for (int i = 3; i <= 5; i++)
            {
                Console.WriteLine(DirDerivStrings[i] + " = " + BooleanExpression.FindMininalExpressionInBasis(
                                      f.DirectionalDeriv(VarLists[i]).Eval(), AvdoshinBases[0], AvdoshinVars[0]).ToString());
            }

            Console.WriteLine();
        }
Exemplo n.º 11
0
        public static void Print2DirectionalDerivatives(int n)
        {
            BooleanFunction f = new BooleanFunction((byte)n);

            PrintJustTruthTable(f);

            for (int i = 3; i <= 5; i++)
            {
                Console.WriteLine(f.DirectionalDeriv(VarLists[i]).ToString(DirDerivStrings[i]));
            }

            Console.WriteLine();
        }
Exemplo n.º 12
0
        public static void PrintClosedClasses(int n)
        {
            BooleanFunction f = new BooleanFunction((byte)n);

            PostClosedClasses.CheckConservationZero(f, true);

            PostClosedClasses.CheckConservationOne(f, true);

            PostClosedClasses.CheckSelfDuality(f, true);

            PostClosedClasses.CheckMonotony(f, true);

            PostClosedClasses.CheckLinearity(f, true);

            Console.WriteLine();
        }
Exemplo n.º 13
0
        static public IEnumerable <TertiaryOpExpression> CombineTertiary(BooleanFunction f,
                                                                         BooleanExpression firstExpression, BooleanExpression anotherExpression, BooleanExpression thirdExpression)
        {
            yield return(new TertiaryOpExpression(f, firstExpression.Clone(), anotherExpression.Clone(),
                                                  thirdExpression.Clone()));

            yield return(new TertiaryOpExpression(f, firstExpression.Clone(), thirdExpression.Clone(),
                                                  anotherExpression.Clone()));

            yield return(new TertiaryOpExpression(f, anotherExpression.Clone(), firstExpression.Clone(),
                                                  thirdExpression.Clone()));

            yield return(new TertiaryOpExpression(f, thirdExpression.Clone(), firstExpression.Clone(),
                                                  anotherExpression.Clone()));

            yield return(new TertiaryOpExpression(f, anotherExpression.Clone(), thirdExpression.Clone(),
                                                  firstExpression.Clone()));

            yield return(new TertiaryOpExpression(f, thirdExpression.Clone(), anotherExpression.Clone(),
                                                  firstExpression.Clone()));
        }
Exemplo n.º 14
0
        public static bool CheckConservationZero(BooleanFunction f, bool verbose = false)
        {
            if (f.EvalAt(0) == 0)
            {
                if (verbose)
                {
                    Console.WriteLine("F ∈ T_0, т.к. F(0,0,0) = 0");
                }

                return(true);
            }
            else
            {
                if (verbose)
                {
                    Console.WriteLine("F ∉ T_0, т.к. F(0,0,0) = 1");
                }

                return(false);
            }
        }
Exemplo n.º 15
0
        public static bool CheckConservationOne(BooleanFunction f, bool verbose = false)
        {
            if (f.EvalAt(7) == 1)
            {
                if (verbose)
                {
                    Console.WriteLine("F ∈ T_1, т.к. F(1,1,1) = 1");
                }

                return(true);
            }
            else
            {
                if (verbose)
                {
                    Console.WriteLine("F ∉ T_1, т.к. F(1,1,1) = 0");
                }

                return(false);
            }
        }
Exemplo n.º 16
0
        public static bool CheckSelfDuality(BooleanFunction f, bool verbose = false)
        {
            for (int i = 0; i < 8; i++)
            {
                if (f.EvalAt(i) == f.EvalAt(7 - i))
                {
                    if (verbose)
                    {
                        Console.WriteLine("F ∉ T_*, т.к. F({0}) = F({1}) = {2}", PointAsString(i), PointAsString(7 - i), f.EvalAt(i));
                    }

                    return(false);
                }
            }

            if (verbose)
            {
                Console.WriteLine("F ∈ T_*, т.к. я на всех наборах проверял, честно!");
            }

            return(true);
        }
Exemplo n.º 17
0
        public static bool CheckLinearity(BooleanFunction f, bool verbose = false)
        {
            string zhegalkinRep = f.GetTailorStringXor(0);

            foreach (string s in new string[] { "AB", "BC", "AC", "ABC", })
            {
                if (zhegalkinRep.Contains(s))
                {
                    if (verbose)
                    {
                        Console.WriteLine("F ∉ T_L, т.к. F(A,B,C) = " + zhegalkinRep);
                    }

                    return(false);
                }
            }

            if (verbose)
            {
                Console.WriteLine("F ∈ T_L, т.к. F(A,B,C) = " + zhegalkinRep);
            }

            return(true);
        }
Exemplo n.º 18
0
        static void PrintJustTruthTable(BooleanFunction f)
        {
            Console.WriteLine((new BooleanFunction(0x0F)).ToString("A"));
            Console.WriteLine((new BooleanFunction(0x33)).ToString("B"));
            Console.WriteLine((new BooleanFunction(0x55)).ToString("C"));

            Console.WriteLine(f.ToString("F"));
        }
Exemplo n.º 19
0
        public static bool CheckConservationOne(BooleanFunction f, bool verbose = false)
        {
            if(f.EvalAt(7) == 1) {
                if(verbose)
                    Console.WriteLine("F ∈ T_1, т.к. F(1,1,1) = 1");

                return true;
            }
            else {
                if(verbose)
                    Console.WriteLine("F ∉ T_1, т.к. F(1,1,1) = 0");

                return false;
            }
        }
Exemplo n.º 20
0
        public BooleanFunction Deriv(params BooleanVariable[] variables)
        {
            BooleanFunction res = this;

            foreach(var variable in variables) {
                res = new BooleanFunction((byte)(res.FixVar(variable, 0).Eval() ^ res.FixVar(variable, 1).Eval()));
            }

            return res;
        }
Exemplo n.º 21
0
        public static BooleanExpression FindMininalExpressionForBinary(BooleanExpression targetBinary, BooleanFunction f,
                                                                       List <BooleanExpression> availableExpressions)
        {
            var queue            = new ImprovisedPriorityQueue <BooleanExpression>(20);
            var knownTruthTables = new HashSet <byte>();
            var knownExpressions = new HashSet <BooleanExpression>();

            foreach (var expression in availableExpressions)
            {
                queue.TryEnqueue(expression, 1);
            }

            while (queue.Count != 0)
            {
                var curExperession = queue.Dequeue();

                byte truthTable = curExperession.Eval();

                if (knownTruthTables.Contains(truthTable))
                {
                    continue;
                }

                if ((curExperession.Eval() & 0xAA) == (targetBinary.Eval() & 0xAA))
                {
                    return(curExperession);
                }

                knownExpressions.Add(curExperession);
                knownTruthTables.Add(truthTable);

                foreach (var anotherExpression in knownExpressions)
                {
                    foreach (var thirdExpression in knownExpressions)
                    {
                        foreach (var neighbourExpression in CombineTertiary(f, curExperession, anotherExpression, thirdExpression))
                        {
                            queue.TryEnqueue(neighbourExpression, neighbourExpression.CountOps());
                        }
                    }
                }
            }

            throw new CouldntFindExpressionException();
        }
Exemplo n.º 22
0
 public static bool[] GetFunctionClasses(BooleanFunction f)
 {
     return(new bool[] { CheckConservationZero(f), CheckConservationOne(f), CheckSelfDuality(f), CheckMonotony(f), CheckLinearity(f), });
 }
Exemplo n.º 23
0
        public static IEnumerable<TertiaryOpExpression> CombineTertiary(BooleanFunction f,
			BooleanExpression firstExpression, BooleanExpression anotherExpression, BooleanExpression thirdExpression)
        {
            yield return new TertiaryOpExpression(f, firstExpression.Clone(), anotherExpression.Clone(),
                thirdExpression.Clone());
            yield return new TertiaryOpExpression(f, firstExpression.Clone(), thirdExpression.Clone(),
                anotherExpression.Clone());
            yield return new TertiaryOpExpression(f, anotherExpression.Clone(), firstExpression.Clone(),
                thirdExpression.Clone());
            yield return new TertiaryOpExpression(f, thirdExpression.Clone(), firstExpression.Clone(),
                anotherExpression.Clone());
            yield return new TertiaryOpExpression(f, anotherExpression.Clone(), thirdExpression.Clone(),
                firstExpression.Clone());
            yield return new TertiaryOpExpression(f, thirdExpression.Clone(), anotherExpression.Clone(),
                firstExpression.Clone());
        }
Exemplo n.º 24
0
        public static bool CheckConservationZero(BooleanFunction f, bool verbose = false)
        {
            if(f.EvalAt(0) == 0) {
                if(verbose)
                    Console.WriteLine("F ∈ T_0, т.к. F(0,0,0) = 0");

                return true;
            }
            else {
                if(verbose)
                    Console.WriteLine("F ∉ T_0, т.к. F(0,0,0) = 1");

                return false;
            }
        }
Exemplo n.º 25
0
        public static bool CheckMonotony(BooleanFunction f, bool verbose = false)
        {
            for(int i = 0; i < 8; i++) {
                for(int j = 0; j < 8; j++) {
                    if(!AreAdjacent(i, j) || !IsLessThan(i, j))
                        continue;

                    if(f.EvalAt(i) > f.EvalAt(j)) {
                        if(verbose)
                            Console.WriteLine("F ∉ T_<=, т.к. F({0}) > F({1})", PointAsString(i), PointAsString(j));

                        return false;
                    }
                }
            }

            if(verbose)
                Console.WriteLine("F ∈ T_<=, т.к. я на всех наборах проверял, честно!");

            return true;
        }
Exemplo n.º 26
0
        public static bool CheckLinearity(BooleanFunction f, bool verbose = false)
        {
            string zhegalkinRep = f.GetTailorStringXor(0);

            foreach(string s in new string[] { "AB", "BC", "AC", "ABC", }) {
                if(zhegalkinRep.Contains(s)) {
                    if(verbose)
                        Console.WriteLine("F ∉ T_L, т.к. F(A,B,C) = " + zhegalkinRep);

                    return false;
                }
            }

            if(verbose)
                Console.WriteLine("F ∈ T_L, т.к. F(A,B,C) = " + zhegalkinRep);

            return true;
        }
Exemplo n.º 27
0
        public static void PrintTailor1XorAnd(int n)
        {
            BooleanFunction f = new BooleanFunction((byte)n);

            for(int i = 0; i < 8; i++) {
                Console.WriteLine(ReprStrings[i] + ": F(A,B,C) = " + f.GetTailorStringXor(i));
            }

            Console.WriteLine();
        }
Exemplo n.º 28
0
        public static void PrintDerivatives(int n)
        {
            BooleanFunction f = new BooleanFunction((byte)n);

            PrintJustTruthTable(f);

            for(int i = 0; i < 7; i++) {
                Console.WriteLine(f.Deriv(VarLists[i]).ToString(DerivStrings[i]));
            }

            Console.WriteLine();
        }
Exemplo n.º 29
0
        public static BooleanExpression FindMininalExpressionForBinary(BooleanExpression targetBinary, BooleanFunction f,
			List<BooleanExpression> availableExpressions)
        {
            var queue = new ImprovisedPriorityQueue<BooleanExpression>(20);
            var knownTruthTables = new HashSet<byte>();
            var knownExpressions = new HashSet<BooleanExpression>();

            foreach(var expression in availableExpressions) {
                queue.TryEnqueue(expression, 1);
            }

            while(queue.Count != 0) {
                var curExperession = queue.Dequeue();

                byte truthTable = curExperession.Eval();

                if(knownTruthTables.Contains(truthTable)) {
                    continue;
                }

                if((curExperession.Eval() & 0xAA) == (targetBinary.Eval() & 0xAA)) {
                    return curExperession;
                }

                knownExpressions.Add(curExperession);
                knownTruthTables.Add(truthTable);

                foreach(var anotherExpression in knownExpressions) {
                    foreach(var thirdExpression in knownExpressions) {
                        foreach(var neighbourExpression in CombineTertiary(f, curExperession, anotherExpression, thirdExpression)) {
                            queue.TryEnqueue(neighbourExpression, neighbourExpression.CountOps());
                        }
                    }
                }
            }

            throw new CouldntFindExpressionException();
        }
Exemplo n.º 30
0
        public static void PrintExpressionsForDerivatives(int n)
        {
            BooleanFunction f = new BooleanFunction((byte)n);

            for(int i = 0; i < 7; i++) {
                Console.WriteLine(DerivStrings[i] + " = " + BooleanExpression.FindMininalExpressionInBasis(
                    f.Deriv(VarLists[i]).Eval(), AvdoshinBases[0], AvdoshinVars[0]).ToString());
            }

            Console.WriteLine();
        }
Exemplo n.º 31
0
        public static bool CheckSelfDuality(BooleanFunction f, bool verbose = false)
        {
            for(int i = 0; i < 8; i++) {
                if(f.EvalAt(i) == f.EvalAt(7 - i)) {
                    if(verbose)
                        Console.WriteLine("F ∉ T_*, т.к. F({0}) = F({1}) = {2}", PointAsString(i), PointAsString(7 - i), f.EvalAt(i));

                    return false;
                }
            }

            if(verbose)
                Console.WriteLine("F ∈ T_*, т.к. я на всех наборах проверял, честно!");

            return true;
        }
Exemplo n.º 32
0
        public static void Print3DirectionalDerivative(int n)
        {
            BooleanFunction f = new BooleanFunction((byte)n);

            PrintJustTruthTable(f);

            Console.WriteLine(f.DirectionalDeriv(VarLists[6]).ToString(DirDerivStrings[6]));

            Console.WriteLine();
        }
Exemplo n.º 33
0
        public static void PrintClosedClasses(int n)
        {
            BooleanFunction f = new BooleanFunction((byte)n);

            PostClosedClasses.CheckConservationZero(f, true);

            PostClosedClasses.CheckConservationOne(f, true);

            PostClosedClasses.CheckSelfDuality(f, true);

            PostClosedClasses.CheckMonotony(f, true);

            PostClosedClasses.CheckLinearity(f, true);

            Console.WriteLine();
        }
Exemplo n.º 34
0
        public static void PrintExpressionFor3DirDerivative(int n)
        {
            BooleanFunction f = new BooleanFunction((byte)n);

            Console.WriteLine(DirDerivStrings[6] + " = " + BooleanExpression.FindMininalExpressionInBasis(
                f.DirectionalDeriv(VarLists[6]).Eval(), AvdoshinBases[0], AvdoshinVars[0]).ToString());

            Console.WriteLine();
        }
Exemplo n.º 35
0
 public static bool[] GetFunctionClasses(BooleanFunction f)
 {
     return new bool[] { CheckConservationZero(f), CheckConservationOne(f), CheckSelfDuality(f), CheckMonotony(f), CheckLinearity(f), };
 }
Exemplo n.º 36
0
 public TertiaryOpExpression(BooleanFunction f, BooleanExpression first, BooleanExpression second, BooleanExpression third)
 {
     F = f;
     First = first;
     Second = second;
     Third = third;
 }
Exemplo n.º 37
0
        public static void PrintMaclauren1XorAnd(int n)
        {
            BooleanFunction f = new BooleanFunction((byte)n);

            Console.WriteLine("F(A,B,C) = " + f.GetTailorStringXor(0));

            Console.WriteLine();
        }