private void calculateNonReursievly(CompoundNumber[,] matrix) { if (matrix.GetUpperBound(0) == matrix.GetUpperBound(1)) { if (matrix.GetUpperBound(0) == 0) { this.value = matrix[0, 0]; } else if (matrix.GetUpperBound(0) == 1) { this.value = (matrix[0, 0] * matrix[1, 1]) - (matrix[0, 1] * matrix[1, 0]); } else if (matrix.GetUpperBound(0) == 2) { this.value = matrix[0, 0] * matrix[1, 1] * matrix[2, 2] + matrix[0, 1] * matrix[1, 2] * matrix[2, 0] + matrix[0, 2] * matrix[1, 0] * matrix[2, 1] - matrix[2, 0] * matrix[1, 1] * matrix[0, 2] - matrix[2, 1] * matrix[1, 2] * matrix[0, 0] - matrix[2, 2] * matrix[1, 0] * matrix[0, 1]; } //(0, 0) | (0, 1) | (0, 2) //(1, 0) | (1, 1) | (1, 2) //(2, 0) | (2, 1) | (2, 2) } else { throw new Exception("Rows and columns' number don't match"); } }
//TODO: Doesn't work, idk why /*public static void fill(Ecuation ecObj, int size) { * CompoundNumber[,] ecuation = new CompoundNumber[size, size + 1]; * CompoundNumber[] unkowns = new CompoundNumber[size]; * * //populate unknowns * for (int i = 0; i < unkowns.Length; i++) * unkowns[i] = generateRandomCompound(); * * //populate with constants * for (int i = 0; i < size; i++) * for (int j = 0; j < size; j++) * ecuation[i, j] = generateRandomCompound(); * * //find the results * for (int i = 0; i < size; i++) { * * CompoundNumber tmp = ecuation[i, 0] * unkowns[0]; * for (int j = 1; j < size; j++) * tmp = tmp + (ecuation[i, j] * unkowns[j]); * * ecuation[i, size] = tmp; * } * * ecObj.setEcuation(ecuation); * }*/ private static void hardcoded(Ecuation ecObj) { CompoundNumber[,] matrix = new CompoundNumber[3, 4] { { new CompoundNumber(-1), new CompoundNumber(2), new CompoundNumber(0), new CompoundNumber(1) }, { new CompoundNumber(1), new CompoundNumber(1), new CompoundNumber(1), new CompoundNumber(2) }, { new CompoundNumber(1), new CompoundNumber(-1), new CompoundNumber(-1), new CompoundNumber(0) } }; ecObj.setEcuation(matrix); }
public Determinant(int[,] matrix) { CompoundNumber[,] convertedMatrix = new CompoundNumber[matrix.GetUpperBound(0) + 1, matrix.GetUpperBound(1) + 1]; for (int i = 0; i <= matrix.GetUpperBound(0); i++) { for (int j = 0; j <= matrix.GetUpperBound(1); j++) { convertedMatrix[i, j] = new CompoundNumber(matrix[i, j]); } } initiateThis(convertedMatrix); }
private void calculateRecursievly(CompoundNumber[,] matrix) { Tuple <int, int> rowAndColumn = choseRowAndColumn(matrix); if (rowAndColumn.Item1 != -1 && rowAndColumn.Item2 == -1) { this.value = calculateDeterminantByRow(matrix, rowAndColumn.Item1); } else if (rowAndColumn.Item1 == -1 && rowAndColumn.Item2 != -1) { this.value = calculateDeterminantByColumn(matrix, rowAndColumn.Item1); } else { throw new Exception("Could not choose wheter to pick line or column"); } }
private CompoundNumber calculateDeterminantByColumn(CompoundNumber[,] matrix, int column) { List <CompoundNumber> determinantLists = new List <CompoundNumber>(); for (int i = 0; i < matrix.GetUpperBound(1); i++) { determinantLists.Add(new Determinant(matrix, i, column).getValue()); } CompoundNumber result = determinantLists[0]; for (int i = 1; i < determinantLists.Count(); i++) { result = result + determinantLists[i]; } return(result); }
private CompoundNumber calculateDeterminantByRow(CompoundNumber[,] matrix, int row) { List <CompoundNumber> determinantLists = new List <CompoundNumber>(); for (int i = 0; i <= matrix.GetUpperBound(0); i++) { determinantLists.Add(matrix[row, i] * new Determinant(matrix, row, i).getValue()); } CompoundNumber result = null; for (int i = 0; i < determinantLists.Count(); i++) { result = result != null ? result + (i % 2 != 0 ? new CompoundNumber(1) : new CompoundNumber(-1)) * determinantLists[i] : determinantLists[i]; } return(result); }
public static void fill(Ecuation ecObj) { Console.WriteLine("Introduceti numarul de necunoscute."); int size = Int32.Parse(Console.ReadLine()); //Read constants CompoundNumber[,] ecuation = new CompoundNumber[size, size + 1]; for (int i = 0; i < size; i++) { for (int j = 0; j < size + 1; j++) { if (j != size) { Console.Write("Numarator pentru [{0}, {1}] = ", i, j); } else { Console.Write("Numaratorul rezultatului pentru linia {0} = ", i); } int numerator = Int32.Parse(Console.ReadLine()); if (j != size) { Console.Write("Numitorul pentru [{0}, {1}] = ", i, j); } else { Console.Write("Numitorul rezultatului pentru linia {0} = ", i); } int denominator = Int32.Parse(Console.ReadLine()); ecuation[i, j] = new CompoundNumber(numerator, denominator); Console.WriteLine(); } } ecObj.setEcuation(ecuation); }
static void Main(string[] args) { //Lab1 //int[] input = readInput(true); //SortingAlgorythm bubbleSort = new BubbleSort(input); //bubbleSort.sort(); //bubbleSort.animate(); //SortingAlgorythm selectSort = new SelectSort2(input); //selectSort.sort(); //selectSort.animate(); //SortingAlgorythm combSort = new CombSort(input); //combSort.sort(); //combSort.animate(); //Lab2 //Ecuation ec = new Ecuation(3, 3); //double some = 3256532.6d; //Console.WriteLine(some.ToString()); //Lab3 //StudentNote studentNote = new StudentNote(false); //int student = 1; //int subj = 1; //Console.WriteLine("Media pentru studentul {0} la obiectul {1} este {2}", // studentNote.getStudents()[student], // studentNote.getSubjects()[subj], // studentNote.mediaPentruStudentLaObiect(student, subj)); CompoundNumber[,] matrix4 = new CompoundNumber[4, 5] { { new CompoundNumber(-1), new CompoundNumber(-2), new CompoundNumber(0), new CompoundNumber(3), new CompoundNumber(-6) }, { new CompoundNumber(2), new CompoundNumber(3), new CompoundNumber(4), new CompoundNumber(4), new CompoundNumber(22) }, { new CompoundNumber(2), new CompoundNumber(2), new CompoundNumber(3), new CompoundNumber(5), new CompoundNumber(20) }, { new CompoundNumber(3), new CompoundNumber(1), new CompoundNumber(-5), new CompoundNumber(7), new CompoundNumber(-34) } }; CompoundNumber[,] matrix3 = new CompoundNumber[3, 4] { { new CompoundNumber(-1), new CompoundNumber(2), new CompoundNumber(0), new CompoundNumber(6) }, { new CompoundNumber(2), new CompoundNumber(3), new CompoundNumber(4), new CompoundNumber(40) }, { new CompoundNumber(2), new CompoundNumber(2), new CompoundNumber(3), new CompoundNumber(30) } }; //TODO: Investigate determinant calculation for 3+ levels Ecuation ecuation = new Ecuation(); ecuation.setEcuation(matrix4); //ecuation.solve(); //ecuation.prettyPrint(); Console.WriteLine(new Determinant( new int[4, 4] { { 1, 2, 3, -2 }, { -1, 0, 5, 1 }, { 4, 1, -1, 3 }, { 3, 2, 1, 4 } } /*new int[4, 4] { * { 1, 0, 0, 0}, * { -1, 2, 8, -1}, * { -4, -7, -13, 11}, * { 3, -4, -8, 10} * }*/ ).getValue());//should be 264 Console.Read(); }
public Determinant(CompoundNumber[,] matrix, int excludeRow, int excludeColumn) { CompoundNumber[,] newMatrix = new CompoundNumber[matrix.GetUpperBound(0), matrix.GetUpperBound(1)]; CompoundNumber[,] tmpMatrix = new CompoundNumber[matrix.GetUpperBound(0) + 1, matrix.GetUpperBound(1) + 1]; //copy for (int i = 0; i <= matrix.GetUpperBound(0); i++) { for (int j = 0; j <= matrix.GetUpperBound(1); j++) { tmpMatrix[i, j] = matrix[i, j]; } } //displace rows for (int i = excludeRow; i < tmpMatrix.GetUpperBound(0); i++) { for (int j = 0; j <= tmpMatrix.GetUpperBound(1); j++) { tmpMatrix[i, j] = tmpMatrix[i + 1, j]; } } //displace columns for (int i = 0; i <= tmpMatrix.GetUpperBound(0); i++) { for (int j = excludeColumn; j < tmpMatrix.GetUpperBound(1); j++) { tmpMatrix[i, j] = tmpMatrix[i, j + 1]; } } //copy displaced data for (int i = 0; i < tmpMatrix.GetUpperBound(0); i++) { for (int j = 0; j < tmpMatrix.GetUpperBound(1); j++) { newMatrix[i, j] = tmpMatrix[i, j]; } } //[+] - //- + if (excludeRow % 2 == 0 && excludeColumn % 2 == 0) { this.sign = +1; } //+ [-] //- + else if (excludeRow % 2 == 0 && excludeColumn % 2 != 0) { this.sign = -1; } //+ - //[-] + else if (excludeRow % 2 != 0 && excludeColumn % 2 == 0) { this.sign = -1; } //+ - //- [+] else if (excludeRow % 2 != 0 && excludeColumn % 2 != 0) { this.sign = +1; } initiateThis(newMatrix); }