private Matrix FromVM(MatrixVM matrix) { uint rows = (uint)matrix.Mask.GetLength(0); uint cols = (uint)matrix.Mask.GetLength(0); Matrix m = new Matrix(rows, cols); for (uint row = 0; row < rows; row++) { for (uint col = 0; col < cols; col++) { m[row, col] = matrix.Mask[row, col].Value; } } return(m); }
public bool IsMatricesConsistent(MatrixVM matrix1, MatrixVM matrix2) { return(matrix1.Rows == matrix2.Rows && matrix1.Columns == matrix2.Columns); }
public object Calculate(string str, MatrixVM[] matrices) { List <string> rpn = ConvertToRPN(str); Stack <object> value = new Stack <object>(); double i; double j; foreach (string elem in rpn) { if (elem.Length == 1 && Priorities.ContainsKey((Operator)elem[0])) { object secondOperand = value.Pop(); object firstOperand = value.Pop(); switch ((Operator)elem[0]) { case Operator.Plus: if (firstOperand is string && secondOperand is string && Double.TryParse((string)firstOperand, out i) && Double.TryParse((string)secondOperand, out j)) { value.Push((i + j).ToString()); } else if (firstOperand is MatrixVM && Double.TryParse((string)secondOperand, out i)) { value.Push((MatrixVM)firstOperand + i); } else if (firstOperand is MatrixVM && secondOperand is MatrixVM && IsMatricesConsistent((MatrixVM)firstOperand, (MatrixVM)secondOperand)) { value.Push((MatrixVM)firstOperand + (MatrixVM)secondOperand); } break; case Operator.Minus: if (firstOperand is string && secondOperand is string && Double.TryParse((string)firstOperand, out i) && Double.TryParse((string)secondOperand, out j)) { value.Push((i - j).ToString()); } else if (firstOperand is MatrixVM && Double.TryParse((string)secondOperand, out i)) { value.Push((MatrixVM)firstOperand - i); } else if (firstOperand is MatrixVM && secondOperand is MatrixVM && IsMatricesConsistent((MatrixVM)firstOperand, (MatrixVM)secondOperand)) { value.Push((MatrixVM)firstOperand - (MatrixVM)secondOperand); } break; case Operator.Divide: if (firstOperand is string && secondOperand is string && Double.TryParse((string)firstOperand, out i) && Double.TryParse((string)secondOperand, out j)) { value.Push((i / j).ToString()); } break; case Operator.Multiply: if (firstOperand is string && secondOperand is string && Double.TryParse((string)firstOperand, out i) && Double.TryParse((string)secondOperand, out j)) { value.Push((i * j).ToString()); } else if (firstOperand is MatrixVM && secondOperand is string && Double.TryParse((string)secondOperand, out i)) { value.Push((MatrixVM)firstOperand * i); } else if (firstOperand is MatrixVM && secondOperand is MatrixVM && ((MatrixVM)firstOperand).Columns == ((MatrixVM)secondOperand).Rows || ((MatrixVM)secondOperand).Rows == ((MatrixVM)firstOperand).Columns) { value.Push((MatrixVM)firstOperand * (MatrixVM)secondOperand); } break; } } else { MatrixVM matrix = matrices.FirstOrDefault((m) => m.Name == elem); if (matrix != null) { value.Push(matrix); } else { value.Push(elem); } } } if (value.Count == 0) { value.Push("Нет результата"); return(value.Pop()); } else { return(value.Pop()); } }