Example #1
0
vector< vector<Fraction> > SLESolver::BringToRowEchelonForm(std::vector< std::vector<Fraction> > augmentedMatrix)
{
	int rowLength = augmentedMatrix.size();
	int columnLength = augmentedMatrix[0].size();
	int lastPivotIndex = 0;
	for (int j = 0; j < columnLength; j++) {
		Fraction currentFraction = augmentedMatrix[lastPivotIndex][j];
		bool pivotFound = false;
		int nextLineIndex = lastPivotIndex + 1;
		if (currentFraction.GetNumerator() == 0) {
			int nonZeroRowIndex = 0;
			for (int k = nextLineIndex; k < rowLength; k++) {
				if (augmentedMatrix[k][j].GetNumerator() != 0) {
					nonZeroRowIndex = k;
					pivotFound = true;
					break;
				}
			}
			if (pivotFound) {
				_solutionWriter->WriteLine("R" + to_string(lastPivotIndex + 1) + "<->" + "R" + to_string(nonZeroRowIndex + 1));
				for (int k = j; k < columnLength; k++) {
					Fraction temp = augmentedMatrix[lastPivotIndex][k];
					augmentedMatrix[lastPivotIndex][k] = augmentedMatrix[nonZeroRowIndex][k];
					augmentedMatrix[nonZeroRowIndex][k] = temp;
				}
				PrintMatrix(augmentedMatrix);
			}
		}
		else {
			pivotFound = true;
		}
		if (pivotFound) {
			for (int k = nextLineIndex; k < rowLength; k++) {
				Fraction fractionToCheck = augmentedMatrix[k][j];
				if (fractionToCheck.GetNumerator() != 0) {
					int newNumerator = -fractionToCheck.GetNumerator() * augmentedMatrix[lastPivotIndex][j].GetDenominator();
					int newDenominator = fractionToCheck.GetDenominator() * augmentedMatrix[lastPivotIndex][j].GetNumerator();
					Fraction fractionToMultiplyBy(newNumerator, newDenominator);
					_solutionWriter->WriteLine("R" + to_string(lastPivotIndex + 1) + "*(" + fractionToMultiplyBy.GetString() + ") + " + "R" + to_string(k + 1));
					for (int t = j; t < columnLength; t++) {
						Fraction fractionToAdd(augmentedMatrix[lastPivotIndex][t]);
						fractionToAdd.Multiply(fractionToMultiplyBy);
						augmentedMatrix[k][t].Add(fractionToAdd);
					}
					PrintMatrix(augmentedMatrix);
				}
			}
			lastPivotIndex++;
			if (lastPivotIndex == rowLength) {
				break;
			}
		}
	}
	return augmentedMatrix;
}
Example #2
0
const Fraction operator*(const Fraction& term1, const Fraction& term2) {
	//Turned into improper fractions and then mulitplied through
	Fraction result;
	result.SetNumerator(((term1.GetWhole() * term1.GetDenominator()) + term1.GetNumerator()) * ((term2.GetWhole() * term2.GetDenominator()) + term2.GetNumerator()));
	result.SetDenominator(term1.GetDenominator() * term2.GetDenominator());
	result.Normalize();
	
	return result;
}
Example #3
0
const Fraction operator/(const Fraction& term1, const Fraction& term2) {
	//Multiplies by the reciprocal of term2
	Fraction result;
	result.SetNumerator(((term1.GetWhole() * term1.GetDenominator()) + term1.GetNumerator()) * term2.GetDenominator());
	result.SetDenominator(term1.GetDenominator() * ((term2.GetWhole() * term2.GetDenominator()) + term2.GetNumerator()));
	result.Normalize();
	
	return result;
}
Example #4
0
const Fraction operator-(const Fraction& term1, const Fraction& term2) {
	//Finds least commons denominator, turns into improper fraction and then subtracted
	Fraction frac1(0, ((term1.GetWhole() * term1.GetDenominator() * term2.GetDenominator()) + (term1.GetNumerator() * term2.GetDenominator())), (term1.GetDenominator() * term2.GetDenominator()));
	Fraction frac2(0, ((term2.GetWhole() * term2.GetDenominator() * term1.GetDenominator()) + (term2.GetNumerator() * term1.GetDenominator())), (term1.GetDenominator() * term2.GetDenominator()));
	
	Fraction result(0, (frac1.GetNumerator() - frac2.GetNumerator()), frac1.GetDenominator());
	result.Normalize();
	
	return result;
}