Beispiel #1
0
 public Matrix(IMatrixElement[] vector)
 {
     _matrix = new IMatrixElement[vector.Length, 1];
     for (int i = 0; i < vector.Length; i++)
         _matrix[i, 0] = vector[i].Clone();
     this._matrixChanged = true;
 }
Beispiel #2
0
        private void SetDestinationButton_Click(object sender, EventArgs e)
        {
            if (!IsDestinationPositionEmpty())
            {
                MessageBox.Show(Locales.Strings.EmptyPosition);
                return;
            }

            if (!IsMatrixInitialized())
            {
                MessageBox.Show(Locales.Strings.MatrixNotInitialized);
                return;
            }

            int.TryParse(DestinationXPos.Text, out int xPosition);
            int.TryParse(DestinationYPos.Text, out int yPosition);

            IMatrixElement destinationElement = MatrixFactory.CreateMatrixElement(MatrixElementType.Destination, xPosition - 1, yPosition - 1);

            try
            {
                Matrix.SetMatrixElement(destinationElement);
                DisplayMatrix();
                DestinationSet = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Beispiel #3
0
        private void SetStartPositionBttn_Click(object sender, EventArgs e)
        {
            if (!IsStartPositionEmpty())
            {
                MessageBox.Show(Locales.Strings.EmptyPosition);
                return;
            }

            if (!IsMatrixInitialized())
            {
                MessageBox.Show(Locales.Strings.MatrixNotInitialized);
                return;
            }

            int.TryParse(StartXPos.Text, out int xPosition);
            int.TryParse(StartYPos.Text, out int yPosition);

            try
            {
                IMatrixElement startElement = MatrixFactory.CreateMatrixElement(MatrixElementType.Start, xPosition - 1, yPosition - 1);
                Matrix.SetMatrixElement(startElement);
                DisplayMatrix();
                StartSet = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public static IGraph ConvertMatrixToGraph(IMatrix matrix)
        {
            IGraph graph = new Graph();

            foreach (IMatrixLine line in matrix.MatrixInstance)
            {
                foreach (IMatrixElement element in line.Line)
                {
                    IGraphVertex   vertex         = ConvertMatrixElementToVertex(element);
                    IMatrixElement elementAtRight = line.GetElementAtRight(element);
                    IMatrixElement elementAtLeft  = line.GetElementAtLeft(element);
                    IMatrixElement elementAbove   = matrix.GetElementAbove(line, element);
                    IMatrixElement elementBelow   = matrix.GetElementBelow(line, element);

                    if (!elementAtLeft.IsEmpty())
                    {
                        vertex.ConnectedVertices.Add(ConvertMatrixElementToVertex(elementAtLeft));
                    }
                    if (!elementAtRight.IsEmpty())
                    {
                        vertex.ConnectedVertices.Add(ConvertMatrixElementToVertex(elementAtRight));
                    }
                    if (!elementAbove.IsEmpty())
                    {
                        vertex.ConnectedVertices.Add(ConvertMatrixElementToVertex(elementAbove));
                    }
                    if (!elementBelow.IsEmpty())
                    {
                        vertex.ConnectedVertices.Add(ConvertMatrixElementToVertex(elementBelow));
                    }
                    graph.GraphInstance.Add(vertex);
                }
            }
            return(graph);
        }
        public IMatrixElement Mul(IMatrixElement e)
        {
            Dictionary<string, double> element2 = (e as MatrixElementWithVariables).element, res = new Dictionary<string, double>();
            foreach (var keyValue1 in this.element)
            {
                foreach (var keyValue2 in element2)
                {
                    string var;

                    if (keyValue1.Key == "" && keyValue2.Key == "")
                        var = "";
                    else if (keyValue1.Key == "")
                        var = keyValue2.Key;
                    else if (keyValue2.Key == "")
                        var = keyValue1.Key;
                    else if (keyValue1.Key == keyValue2.Key)
                        var = keyValue1.Key + "^2";
                    else
                        var = keyValue1.Key + "+" + keyValue2.Key;

                    //= var1;
                    //if (var1 != var2)
                    //    var += " * " + var2;
                    //else if (var1 != "")
                    //    var += "^2";
                    if (res.ContainsKey(var))
                        res[var] += keyValue1.Value * keyValue2.Value;
                    else
                        res.Add(var, keyValue1.Value * keyValue2.Value);
                }
            }
            return new MatrixElementWithVariables(res);
        }
Beispiel #6
0
 public Matrix(int rows, int cols,IMatrixElement element)
 {
     _matrix = new IMatrixElement[rows, cols];
     for (int i = 0; i < cols; i++)
         for (int j = 0; j < rows; j++)
             _matrix[j, i] = element;
     this._matrixChanged = true;
 }
        private static IGraphVertex ConvertMatrixElementToVertex(IMatrixElement element)
        {
            IGraphVertex vertex = new GraphVertex
            {
                MatrixElement = element
            };

            return(vertex);
        }
Beispiel #8
0
        public IMatrixElement GetElementAtRight(IMatrixElement element)
        {
            if (!HasElementAtRight(Line.IndexOf(element)))
            {
                return(GetEmptyElement());
            }

            return(Line[Line.IndexOf(element) + 1]);
        }
Beispiel #9
0
        public IMatrixElement GetElementAbove(IMatrixLine line, IMatrixElement element)
        {
            if (!HasLineAbove(MatrixInstance.IndexOf(line)))
            {
                return(GetEmptyElement());
            }

            int         indexOfElement = line.Line.IndexOf(element);
            IMatrixLine lineAbove      = MatrixInstance[MatrixInstance.IndexOf(line) - 1];

            return(lineAbove.GetElementAtIndex(indexOfElement));
        }
Beispiel #10
0
        public IMatrixElement GetElementBelow(IMatrixLine line, IMatrixElement element)
        {
            if (!HasLineBelow(MatrixInstance.IndexOf(line)))
            {
                return(GetEmptyElement());
            }

            int         indexOfElement = line.Line.IndexOf(element);
            IMatrixLine lineBelow      = MatrixInstance[MatrixInstance.IndexOf(line) + 1];

            return(lineBelow.GetElementAtIndex(indexOfElement));
        }
Beispiel #11
0
        public void SetMatrixElement(IMatrixElement element)
        {
            if (!IsElementInRange(element))
            {
                throw new ElementOutOfRangeException(Locales.Strings.ElementOutOfRange);
            }

            if (!IsRobotElementAlreadySet() && element.IsRobot())
            {
                StartPositionSet = true;
            }
            else if (IsRobotElementAlreadySet() && element.IsRobot())
            {
                throw new StartElementAlreadySetException(Locales.Strings.StartPositionAlreadySet);
            }

            if (IsElementAlreadyMarked(element))
            {
                throw new ElementAlreadySetException(Locales.Strings.PositionAlreadySet);
            }

            MatrixInstance[element.X].Line[element.Y] = element;
        }
 public IMatrixElement Sub(IMatrixElement e)
 {
     return new FunctionMatrixElement(this.Function - (e as FunctionMatrixElement).Function);
 }
        public IMatrixElement Mul(IMatrixElement e)
        {
            FunctionMatrixElement eq = e as FunctionMatrixElement;

            return new FunctionMatrixElement(eq.Function * this.Function);
        }
 public IMatrixElement Div(IMatrixElement e)
 {
     return new FunctionMatrixElement(this.Function / (e as FunctionMatrixElement).Function);
 }
 public IMatrixElement Add(IMatrixElement e)
 {
     return new FunctionMatrixElement(this.Function + (e as FunctionMatrixElement).Function);
 }
Beispiel #16
0
 private Function SetVariableValue(string name, IMatrixElement value)
 {
     if (value is FunctionMatrixElement)
     {
         return new Function(this.SetVariableValue(name, (value as FunctionMatrixElement).Function));
     }
     if (value is DoubleMatrixElement)
     {
         return new Function(this.SetVariableValue(name, (value as DoubleMatrixElement).D));
     }
     throw new Exception("Unsupported matrix element type");
 }
 public IMatrixElement Mul(IMatrixElement e)
 {
     DoubleMatrixElement e1 = e as DoubleMatrixElement;
     return new DoubleMatrixElement(D * e1.D);
 }
 public IMatrixElement Add(IMatrixElement e)
 {
     DoubleMatrixElement e1 = e as DoubleMatrixElement;
     return new DoubleMatrixElement(D + e1.D);
 }
Beispiel #19
0
 private bool IsElementAlreadyMarked(IMatrixElement element) => !MatrixInstance[element.X].Line[element.Y].IsAvailable();
Beispiel #20
0
 private bool IsElementInRange(IMatrixElement element) => element.X < Size && element.Y < Size && element.X >= 0 && element.Y >= 0;
 public IMatrixElement Div(IMatrixElement e)
 {
     return new DoubleMatrixElement(this.D / (e as DoubleMatrixElement).D);
 }
Beispiel #22
0
 /// <summary>
 /// Инициализация матрицы с помощью двумерно массива вещественных чисел
 /// </summary>
 /// <param name="matrix">Массив элементов матрицы</param>
 public Matrix(IMatrixElement[,] matrix)
 {
     _matrix = matrix.Clone() as IMatrixElement[,];
     this._matrixChanged = true;
 }
 public IMatrixElement Sub(IMatrixElement e)
 {
     return new DoubleMatrixElement(D - (e as DoubleMatrixElement).D);
 }
Beispiel #24
0
 /// <summary>
 /// Единичная матрица заданного размера.
 /// </summary>
 /// <param name="size">Размерность матрицы.</param>
 /// <returns></returns>
 public static Matrix IdentityMatrix(int size, IMatrixElement elementForGetType)
 {
     Matrix m = new Matrix(size, size,elementForGetType.Zero());
     for (int i = 0; i < size; i++)
         m[i, i] = elementForGetType.One();
     return m;
 }
        //public double ToDouble(double[] variablesValues)
        //{

        //}


        public IMatrixElement Div(IMatrixElement e)
        {
            throw new NotImplementedException();
        }
        public IMatrixElement Add(IMatrixElement e)
        {
            Dictionary<string, double> element2 = (e as MatrixElementWithVariables).element, res = new Dictionary<string, double>(element);
            foreach (string var1 in element2.Keys)
            {
                if (res.ContainsKey(var1))
                    res[var1] += element2[var1];
                else
                    res.Add(var1, element2[var1]);
            }

            return new MatrixElementWithVariables(res);
        }