示例#1
0
        public EpMatrix ParseEquations()
        {
            // Look: an example set
            // 3x + 7z = 9
            // x + y + z = 10
            // y + 2z = 6

            int numOfEquals = NumberOf (originalText, "=");
            int numOfNewline = NumberOf (originalText, "\n");

            if (numOfEquals != numOfNewline)
                return null;

            string[] lines = originalText.Split ('\n');

            System.Text.RegularExpressions.MatchCollection unknowns =
                System.Text.RegularExpressions.Regex.Matches (originalText, "[a-z]",
                System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace);

            string[] unknownsString = new string[unknowns.Count - 1];
            unknowns.CopyTo (unknownsString, 0);
            unknownsString = RemoveDuplicates (unknownsString);

            if (unknownsString.GetLength (0) != lines.GetLength (0)) {
                return null;
            }

            // Let's get started splitting lines into an array
            EpMatrix result = new EpMatrix(new int[lines.GetUpperBound (0),unknownsString.GetUpperBound (0)]);

            char[] delimiterChars = { '+', '-', '=' };
            string[,] setSplit = new string[numOfNewline, unknownsString.GetUpperBound (0)];
            for (int i = 0; i < lines.GetLength (0); i++) {
                string[] currentBits = lines [i].Split (delimiterChars);
                for (int j = 0; j < currentBits.GetLength (0); j++) {
                    setSplit[i, j] = currentBits [j];
                }
            }

            // by now, we have an array that, for the example set up top, now looks like this:
            // {(3x , 7z, 9),
            // (x, y, z, 10),
            // (y, 2z, 6)}

            // We now need to replace nx with n, where n is a number and x is an unknown,
            // so that every unknown appears exactly once in every row

            return result; // random return to statisfy commpiler
        }
示例#2
0
文件: EpMatrix.cs 项目: Epse/EpSets
		/// <summary>
		/// Swaps the two given rows
		/// </summary>
		/// <returns>An EpMatrix with the swap performed</returns>
		/// <param name="matrix">Matrix.</param>
		/// <param name="rowOne">Zero-based index for the first row.</param>
		/// <param name="rowTwo">Zero-based index for the second row.</param>
		public static EpMatrix SwapRows (EpMatrix matrix, int rowOne, int rowTwo)
		{
			// tempstore the two rows, then swap them in TheMatrix
			int[] firstRow = new int[matrix.GetNumOfColumns()-1];
			for (int i = 0; i < matrix.GetNumOfColumns (); i++) {
				firstRow [i] = matrix.GetElement (rowOne, i);
			}

			// Copy the second row into the first one
			for (int i = 0; i < matrix.GetNumOfColumns (); i++) {
				matrix.SetElement (rowOne, i, matrix.GetElement (rowTwo, i));
			}

			// And now the temp into the second row
			for (int i = 0; i < matrix.GetNumOfColumns (); i++) {
				matrix.SetElement (rowOne, i, firstRow [i]);
			}

			return matrix;
		}
示例#3
0
文件: EpMatrix.cs 项目: Epse/EpSets
		/// <summary>
		/// Turns the given matrix into it's row reduced echelon form.
		/// </summary>
		/// <param name="matrix">An EpMatrix to perform the operation on.</param>
		public static EpMatrix Rref (EpMatrix matrix)
		{
			int[,] rrem = matrix.GetMatrix ();

			int lead = 0, rowCount = rrem.GetLength(0), columnCount = rrem.GetLength(1);
			for (int r = 0; r < rowCount; r++)
			{
				if (columnCount <= lead) break;
				int i = r;
				while (rrem[i, lead] == 0)
				{
					i++;
					if (i == rowCount)
					{
						i = r;
						lead++;
						if (columnCount == lead)
						{
							lead--;
							break;
						}
					}
				}
				for (int j = 0; j < columnCount; j++)
				{
					int temp = rrem[r, j];
					rrem[r, j] = rrem[i, j];
					rrem[i, j] = temp;
				}
				int div = rrem[r, lead];
				if(div != 0)
					for (int j = 0; j < columnCount; j++) rrem[r, j] /= div;                
				for (int j = 0; j < rowCount; j++)
				{
					if (j != r)
					{
						int sub = rrem[j, lead];
						for (int k = 0; k < columnCount; k++) rrem[j, k] -= (sub * rrem[r, k]);
					}
				}
				lead++;
			}

			// Done!
			return new EpMatrix(rrem);
		}
示例#4
0
文件: EpMatrix.cs 项目: Epse/EpSets
		/// <summary>
		/// Performs a linear combination on two rows. Stores the result in the first row by convention.
		/// </summary>
		/// <returns>EpMatrix with the combination performed on it</returns>
		/// <param name="matrix">Matrix.</param>
		/// <param name="rowOne">Zero-based index for the first row</param>
		/// <param name="factorOne">Factor to multiply the first rwo by</param>
		/// <param name="rowTwo">Zero-based index for the second row</param>
		/// <param name="factorTwo">Factor to multiply the second row by</param>
		public static EpMatrix LinearCombination (EpMatrix matrix, int rowOne, int factorOne, int rowTwo, int factorTwo)
		{
			for (int i = 0; i < matrix.GetNumOfColumns (); i++) {
				matrix.SetElement (rowOne, i, 
					factorOne * matrix.GetElement (rowOne, i) + factorTwo * matrix.GetElement (rowTwo, i));
			}

			return matrix;
		}
示例#5
0
文件: EpMatrix.cs 项目: Epse/EpSets
		/// <summary>
		/// Adds two rows from the given matrix together. Stores the result in the first row by convention
		/// </summary>
		/// <returns>An EpMatrix with the rows added</returns>
		/// <param name="matrix">Matrix.</param>
		/// <param name="rowOne">Zero-based index for the first row. The result goes in here as well.</param>
		/// <param name="rowTwo">Zero-based index for the second row.</param>
		public static EpMatrix AddRows (EpMatrix matrix, int rowOne, int rowTwo)
		{
			for (int i = 0; i < matrix.GetNumOfColumns (); i++) {
				matrix.SetElement (rowOne, i, matrix.GetElement (rowTwo, i) + matrix.GetElement (rowOne, i));
			}

			return matrix;
		}
示例#6
0
文件: EpMatrix.cs 项目: Epse/EpSets
		/// <summary>
		/// Multiplies two rows together.
		/// </summary>
		/// <returns>An EpMatrix with the operation performed on it.</returns>
		/// <param name="matrix">Matrix.</param>
		/// <param name="theRow">Zero-based index for the row.</param>
		/// <param name="factor">Factor.</param>
		public static EpMatrix MultiplyRow (EpMatrix matrix, int theRow, int factor)
		{
			for (int i = 0; i < matrix.GetNumOfColumns (); i++) {
				matrix.SetElement (theRow, i, matrix.GetElement (theRow, i) * factor);
			}

			return matrix;
		}