Esempio n. 1
0
        static void Main(string[] args)
        {
            string matrixProduct;
            string inputFile = "";

            if (args.Length < 1)
            {
                Console.WriteLine("Error: No argument received for input file");
                return;
            }
            if (args.Length < 2)
            {
                Console.WriteLine("No argument received for string. String assumed to be the empty string.");
                inputFile     = args[0];
                matrixProduct = "";
            }
            else
            {
                inputFile     = args[0];
                matrixProduct = args[1];
            }

            var symbols = new HashSet <char>(MatrixSolver.Computations.Constants.RegularLanguage.Symbols);

            foreach (var symbol in matrixProduct)
            {
                if (!symbols.Contains(symbol))
                {
                    throw new ArgumentException($"String contained unknown symbol {symbol}");
                }
            }

            // TODO: Duplicate of main function. Expose this procedure somewhere to reduce duplicate code.
            var json = File.ReadAllText(inputFile);
            var data = JsonConvert.DeserializeObject <InputData>(json);

            data.ThrowIfNull();
            var vectorX  = new ImmutableVector2D(data.VectorX);
            var vectorY  = new ImmutableVector2D(data.VectorY);
            var matrices = data.Matrices.Select(m => new ImmutableMatrix2x2(As2DArray(m, 2, 2))).ToArray();

            Console.WriteLine($"Computing matrix product M = {matrixProduct}");
            var M = RegularLanguageHelper.MatrixProductStringToMatrix(matrixProduct);

            Console.WriteLine($"Calculated M = {M}");

            var expectedY = M * vectorX;

            if (expectedY.Equals(vectorY))
            {
                Console.WriteLine($"Mx = y is solved for M = {M}, x = {vectorX}, y = {vectorY}");
            }
            else
            {
                Console.WriteLine($"Expected vector y = {vectorY}, but instead found y = {expectedY}");
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("No argument for filename provided");
                return;
            }
            var fileName = args[0];

            var words = File.ReadAllText(fileName).Split(
                new[] { Environment.NewLine },
                StringSplitOptions.None
                );

            foreach (var word in words)
            {
                var matrix = RegularLanguageHelper.MatrixProductStringToMatrix(word);

                Console.WriteLine($"phi({word}) = {matrix}");
            }
        }
Esempio n. 3
0
        public void ConvertMatrixToCanonicalString_ConvertsCorrectly(string expectedWord)
        {
            // Convert the word to the actual matrix product
            var matrices = expectedWord
                           .Select(i => RegularLanguageHelper.MatrixLookup[i]);

            // Calculate the input matrix
            var inputMatrix = Constants.Matrices.I;

            foreach (var matrix in matrices)
            {
                inputMatrix *= matrix;
            }
            // Make sure our conversion steps assured the same matrix is retained
            Assert.True(RegularLanguageHelper.IsEqual(expectedWord, inputMatrix));
            // Finally, run the conversion
            var matrixProduct = MathematicalHelper.ConvertMatrixToCanonicalString(inputMatrix);

            // Ensure the same matrix product in canonical form is return
            Assert.True(RegularLanguageHelper.IsEqual(matrixProduct, inputMatrix));
            Assert.Equal(expectedWord, matrixProduct);
        }