From 3fd711f4df15fcab7ebb0bd43d1d817644864fd8 Mon Sep 17 00:00:00 2001 From: Jared-Gibson Date: Thu, 16 Apr 2020 17:06:36 -0400 Subject: [PATCH 01/11] Created PartialFrac.h -added methods that I believe will be useful in creating a Partial Fraction Decomposition class --- src/CalcManager/CEngine/PartialFrac.h | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/CalcManager/CEngine/PartialFrac.h diff --git a/src/CalcManager/CEngine/PartialFrac.h b/src/CalcManager/CEngine/PartialFrac.h new file mode 100644 index 000000000..6f619d8a2 --- /dev/null +++ b/src/CalcManager/CEngine/PartialFrac.h @@ -0,0 +1,37 @@ +#pragma once +/** + * Module Name: PartialFrac.h + * Author: Jared Gibson + * Module Description: + * The class definition for the PartialFrac class responsible for performing + * basic partial fraction decomposition (no long division, no repeated roots, no imaginary roots) + * Created: 16-April-2020 + */ + +class PartialFrac +{ +public: + PartialFrac(); + ///\returns the partial fraction decomposition of the equation provided + std::string PartialFracDecomp(std::string equation); + +private: + ///\returns a vector where [0] is the numerator and [1] is denominator + std::vector splitEquationString(std::string equation); + ///\returns true if demon is in factored form + bool factoredForm(); + ///\returns a factored form of denom + std::string factorDenom(); + ///\returns a vector that holds a factor at each index + std::vector splitDenom(); + ///\returns a matrix with the coefficients for the partial frac decomp + std::vector> createCoeffMatrix(); + ///\returns the coefficients of the partial fract decomp + std::vector solveMatrix(); + ///numerator of the equation string provided to PartialFracDecomp + std::string num; + ///denominator of the equation string provided to PartialFracDecomp + std::string denom; + ///coefficient matrix + std::vector> coeffMatrix; +}; From e343dfd0708224c041f1f662006089dc4846781f Mon Sep 17 00:00:00 2001 From: Jared-Gibson Date: Thu, 16 Apr 2020 19:42:14 -0400 Subject: [PATCH 02/11] Implementing PartialFracDecomp & splitEquationString -Added the work-flow for PartialFracDecomp -Added implementation for splitEquationString --- src/CalcManager/CEngine/PartialFrac.cpp | 59 +++++++++++++++++++++ src/CalcManager/CEngine/PartialFrac.h | 12 +++-- src/CalcManager/CalcManager.vcxproj | 4 +- src/CalcManager/CalcManager.vcxproj.filters | 6 +++ 4 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 src/CalcManager/CEngine/PartialFrac.cpp diff --git a/src/CalcManager/CEngine/PartialFrac.cpp b/src/CalcManager/CEngine/PartialFrac.cpp new file mode 100644 index 000000000..ac5ba352b --- /dev/null +++ b/src/CalcManager/CEngine/PartialFrac.cpp @@ -0,0 +1,59 @@ +#include "PartialFrac.h" + +PartialFrac::PartialFrac() +{ + +} + +std::string PartialFrac::PartialFracDecomp(std::string equation) +{ + std::vector fraction = splitEquationString(equation); + std::string num = fraction[0]; + std::string denom = fraction[1]; + if (!factoredForm(denom)) + denom = factorDenom(denom); + std::vector> coeffMatrix = createCoeffMatrix(num, denom); + std::vector coeff = solveMatrix(coeffMatrix); + std::vector factors = splitDenom(denom); + std::string finalstring = stringBuilder(coeff, factors); + return finalstring; +} + +std::vector PartialFrac::splitEquationString(std::string equation) +{ + std::vector tmp; + //the following find statements scan for all types of ways the user could have typed their equation + int pos = equation.find(")/("); + if (!pos == std::string::npos) + { + tmp[0] = equation.substr(0, pos + 1); + tmp[1] = equation.substr(pos + 2, equation.length()); + return tmp; + } + pos = equation.find(")/"); + if (!pos == std::string::npos) + { + tmp[0] = equation.substr(0, pos + 1); + tmp[1] = equation.substr(pos + 2, equation.length()); + return tmp; + } + pos = equation.find("/("); + if (!pos == std::string::npos) + { + tmp[0] = equation.substr(0, pos); + tmp[1] = equation.substr(pos + 1, equation.length()); + return tmp; + } + pos = equation.find("/"); + if (!pos == std::string::npos) + { + tmp[0] = equation.substr(0, pos); + tmp[1] = equation.substr(pos + 1, equation.length()); + return tmp; + } + else + { + throw "Error in splitEquationString method; could not find a division symbol"; + } +} + diff --git a/src/CalcManager/CEngine/PartialFrac.h b/src/CalcManager/CEngine/PartialFrac.h index 6f619d8a2..773e29b71 100644 --- a/src/CalcManager/CEngine/PartialFrac.h +++ b/src/CalcManager/CEngine/PartialFrac.h @@ -19,15 +19,17 @@ class PartialFrac ///\returns a vector where [0] is the numerator and [1] is denominator std::vector splitEquationString(std::string equation); ///\returns true if demon is in factored form - bool factoredForm(); + bool factoredForm(std::string denom); ///\returns a factored form of denom - std::string factorDenom(); + std::string factorDenom(std::string denom); ///\returns a vector that holds a factor at each index - std::vector splitDenom(); + std::vector splitDenom(std::string denom); ///\returns a matrix with the coefficients for the partial frac decomp - std::vector> createCoeffMatrix(); + std::vector> createCoeffMatrix(std::string num, std::string denom); ///\returns the coefficients of the partial fract decomp - std::vector solveMatrix(); + std::vector solveMatrix(std::vector> matrix); + ///\returns a string that represents the partial frac decomp + std::string stringBuilder(std::vector coeff, std::vector factors); ///numerator of the equation string provided to PartialFracDecomp std::string num; ///denominator of the equation string provided to PartialFracDecomp diff --git a/src/CalcManager/CalcManager.vcxproj b/src/CalcManager/CalcManager.vcxproj index 80940c6a3..95b0a8d0c 100644 --- a/src/CalcManager/CalcManager.vcxproj +++ b/src/CalcManager/CalcManager.vcxproj @@ -281,6 +281,7 @@ + @@ -311,6 +312,7 @@ + @@ -346,4 +348,4 @@ - + \ No newline at end of file diff --git a/src/CalcManager/CalcManager.vcxproj.filters b/src/CalcManager/CalcManager.vcxproj.filters index ad7670a02..1f437f221 100644 --- a/src/CalcManager/CalcManager.vcxproj.filters +++ b/src/CalcManager/CalcManager.vcxproj.filters @@ -90,6 +90,9 @@ CEngine + + CEngine + @@ -161,5 +164,8 @@ Header Files + + CEngine + \ No newline at end of file From 75149056d0c3dc1c51cfacad9f8d7a0cd0304b81 Mon Sep 17 00:00:00 2001 From: Jared-Gibson Date: Fri, 17 Apr 2020 01:00:26 -0400 Subject: [PATCH 03/11] Small tweaks to PartialFrac class -renamed factorDenom to factorExpr -changed return value of factorExpr from a string to use a more useful representation of an equation --- src/CalcManager/CEngine/PartialFrac.cpp | 27 ++++++++++++++++++++++--- src/CalcManager/CEngine/PartialFrac.h | 25 +++++++++++++---------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/CalcManager/CEngine/PartialFrac.cpp b/src/CalcManager/CEngine/PartialFrac.cpp index ac5ba352b..568291d5b 100644 --- a/src/CalcManager/CEngine/PartialFrac.cpp +++ b/src/CalcManager/CEngine/PartialFrac.cpp @@ -10,9 +10,11 @@ std::string PartialFrac::PartialFracDecomp(std::string equation) std::vector fraction = splitEquationString(equation); std::string num = fraction[0]; std::string denom = fraction[1]; - if (!factoredForm(denom)) - denom = factorDenom(denom); - std::vector> coeffMatrix = createCoeffMatrix(num, denom); + std::vector> factoredNum; + std::vector> factoredDenom; + factoredNum = factorExpr(num); + factoredDenom = factorExpr(denom); + std::vector> coeffMatrix = createCoeffMatrix(factoredNum, factoredDenom); std::vector coeff = solveMatrix(coeffMatrix); std::vector factors = splitDenom(denom); std::string finalstring = stringBuilder(coeff, factors); @@ -57,3 +59,22 @@ std::vector PartialFrac::splitEquationString(std::string equation) } } +//bool PartialFrac::factoredForm(std::string denom) +//{ +// int pos = denom.find("^"); +// if (pos == std::string::npos) +// return true; +// else +// return false; +//} + +std::vector> PartialFrac::factorExpr(std::string equation) +{ + //probably going to use some Algebra class logic here, so I am going to leave this blank for now +} + +std::vector> PartialFrac::createCoeffMatrix(std::vector> num, std::vector> denom) +{ + + +} diff --git a/src/CalcManager/CEngine/PartialFrac.h b/src/CalcManager/CEngine/PartialFrac.h index 773e29b71..8cb749bd8 100644 --- a/src/CalcManager/CEngine/PartialFrac.h +++ b/src/CalcManager/CEngine/PartialFrac.h @@ -18,22 +18,25 @@ class PartialFrac private: ///\returns a vector where [0] is the numerator and [1] is denominator std::vector splitEquationString(std::string equation); - ///\returns true if demon is in factored form - bool factoredForm(std::string denom); - ///\returns a factored form of denom - std::string factorDenom(std::string denom); + struct factor { + std::string variable; + int constant; }; + ///\returns a factored form of denom using the factor struct to encapsulate a single factor + std::vector> factorExpr(std::string equation); ///\returns a vector that holds a factor at each index std::vector splitDenom(std::string denom); ///\returns a matrix with the coefficients for the partial frac decomp - std::vector> createCoeffMatrix(std::string num, std::string denom); + std::vector> createCoeffMatrix(std::vector> num, std::vector> denom); ///\returns the coefficients of the partial fract decomp std::vector solveMatrix(std::vector> matrix); ///\returns a string that represents the partial frac decomp std::string stringBuilder(std::vector coeff, std::vector factors); - ///numerator of the equation string provided to PartialFracDecomp - std::string num; - ///denominator of the equation string provided to PartialFracDecomp - std::string denom; - ///coefficient matrix - std::vector> coeffMatrix; + + /*************** + Methods that I think I no longer need + + ///\returns true if demon is in factored form + bool factoredForm(std::string denom); + ***************/ + }; From ec4c6a655fc86e08833dc3771e018f6e69e84083 Mon Sep 17 00:00:00 2001 From: Jared-Gibson Date: Mon, 27 Apr 2020 20:21:07 -0400 Subject: [PATCH 04/11] Working on createCoeffMatrix() -added code for finding LHS using Algebra class (waiting on pull requests) -added code for finding RHS using Algebra class (waiting on pull requests) --- src/CalcManager/CEngine/PartialFrac.cpp | 54 ++++++++++++++++++++++--- src/CalcManager/CEngine/PartialFrac.h | 7 ++-- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/CalcManager/CEngine/PartialFrac.cpp b/src/CalcManager/CEngine/PartialFrac.cpp index 568291d5b..3f10f5c08 100644 --- a/src/CalcManager/CEngine/PartialFrac.cpp +++ b/src/CalcManager/CEngine/PartialFrac.cpp @@ -10,8 +10,8 @@ std::string PartialFrac::PartialFracDecomp(std::string equation) std::vector fraction = splitEquationString(equation); std::string num = fraction[0]; std::string denom = fraction[1]; - std::vector> factoredNum; - std::vector> factoredDenom; + std::vector factoredNum; + std::vector factoredDenom; factoredNum = factorExpr(num); factoredDenom = factorExpr(denom); std::vector> coeffMatrix = createCoeffMatrix(factoredNum, factoredDenom); @@ -68,13 +68,57 @@ std::vector PartialFrac::splitEquationString(std::string equation) // return false; //} -std::vector> PartialFrac::factorExpr(std::string equation) +std::vector PartialFrac::factorExpr(std::string equation) { //probably going to use some Algebra class logic here, so I am going to leave this blank for now } -std::vector> PartialFrac::createCoeffMatrix(std::vector> num, std::vector> denom) +std::vector> PartialFrac::createCoeffMatrix(std::vector num, std::vector denom) { - + //finding the RHS equation + //for each position, the value is equal to all other positions multiplied together. + std::vector RHS; + for (int i = 0; i < denom.size(); i++) + { + std::string expression; + for (int j = 0; j < denom.size(); j++) + { + if (i != j) + { + std::string tmp = "(" + denom[j].variable + " " + denom[j].operation + " " + std::to_string(denom[j].constant) + ")"; + if (j == denom.size() - 1) + expression += tmp; + else + expression += tmp + " * "; + } + } + //Algebra intermediate = Algebra(expression); + //intermediate.format(); + //RHS[i] = intermediate.simplifyExpression(); + } + //finding LHS equation + std::vector LHS; + for (int i = 0; i < num.size(); i++) + { + std::string expression; + for (int j = 0; j < num.size(); j++) + { + if (i != j) + { + std::string tmp = "(" + num[j].variable + " " + num[j].operation + " " + std::to_string(num[j].constant) + ")"; + if (j == num.size() - 1) + expression += tmp; + else + expression += tmp + " * "; + } + } + // Algebra intermediate = Algebra(expression); + // intermediate.format(); + // LHS[i] = intermediate.simplifyExpression(); + } + //now that we have the expressions, now we need to add their scalars into the matrix + //assuming that the Algebra class has return expressions ordered from highest power to lowest power + + } diff --git a/src/CalcManager/CEngine/PartialFrac.h b/src/CalcManager/CEngine/PartialFrac.h index 8cb749bd8..8f6c8c228 100644 --- a/src/CalcManager/CEngine/PartialFrac.h +++ b/src/CalcManager/CEngine/PartialFrac.h @@ -19,14 +19,15 @@ class PartialFrac ///\returns a vector where [0] is the numerator and [1] is denominator std::vector splitEquationString(std::string equation); struct factor { - std::string variable; + char variable; + char operation; int constant; }; ///\returns a factored form of denom using the factor struct to encapsulate a single factor - std::vector> factorExpr(std::string equation); + std::vector factorExpr(std::string equation); ///\returns a vector that holds a factor at each index std::vector splitDenom(std::string denom); ///\returns a matrix with the coefficients for the partial frac decomp - std::vector> createCoeffMatrix(std::vector> num, std::vector> denom); + std::vector> createCoeffMatrix(std::vector num, std::vector denom); ///\returns the coefficients of the partial fract decomp std::vector solveMatrix(std::vector> matrix); ///\returns a string that represents the partial frac decomp From c574a48a46471e3cb81420cc3a891e5b4b6f700e Mon Sep 17 00:00:00 2001 From: Jared-Gibson Date: Mon, 27 Apr 2020 20:54:54 -0400 Subject: [PATCH 05/11] Fixed errors createCoeffMatrix -LHS calculation was incorrect; numerator just needs to have all its factors multiplied -fixed error in matrix resizing; the height of the matrix is determined by the largest power of the denominator not the numerator --- src/CalcManager/CEngine/PartialFrac.cpp | 33 +++++++++++++------------ src/CalcManager/CEngine/PartialFrac.h | 4 +-- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/CalcManager/CEngine/PartialFrac.cpp b/src/CalcManager/CEngine/PartialFrac.cpp index 3f10f5c08..c0410cd7b 100644 --- a/src/CalcManager/CEngine/PartialFrac.cpp +++ b/src/CalcManager/CEngine/PartialFrac.cpp @@ -98,24 +98,25 @@ std::vector> PartialFrac::createCoeffMatrix(std::vector LHS; + std::string expression; for (int i = 0; i < num.size(); i++) { - std::string expression; - for (int j = 0; j < num.size(); j++) - { - if (i != j) - { - std::string tmp = "(" + num[j].variable + " " + num[j].operation + " " + std::to_string(num[j].constant) + ")"; - if (j == num.size() - 1) - expression += tmp; - else - expression += tmp + " * "; - } - } - // Algebra intermediate = Algebra(expression); - // intermediate.format(); - // LHS[i] = intermediate.simplifyExpression(); + std::string tmp = "(" + num[i].variable + " " + num[i].operation + " " + std::to_string(num[i].constant) + ")"; + if (i == num.size() - 1) + expression += tmp; + else + expression += tmp + " * "; + } + // Algebra intermediate = Algebra(expression); + // intermediate.format(); + // std::string LHS = intermediate.simplifyExpression(); + + //creating the matrix to store the values; I am resizing the vectors since I need to start at the bottom right instead of the top left + std::vector> matrix; + matrix.resize(RHS.size() + 1); + for (int i = 0; i < matrix.size(); i++) + { + matrix[i].resize(RHS.size()); } //now that we have the expressions, now we need to add their scalars into the matrix //assuming that the Algebra class has return expressions ordered from highest power to lowest power diff --git a/src/CalcManager/CEngine/PartialFrac.h b/src/CalcManager/CEngine/PartialFrac.h index 8f6c8c228..c494690fd 100644 --- a/src/CalcManager/CEngine/PartialFrac.h +++ b/src/CalcManager/CEngine/PartialFrac.h @@ -19,8 +19,8 @@ class PartialFrac ///\returns a vector where [0] is the numerator and [1] is denominator std::vector splitEquationString(std::string equation); struct factor { - char variable; - char operation; + std::string variable; + std::string operation; int constant; }; ///\returns a factored form of denom using the factor struct to encapsulate a single factor std::vector factorExpr(std::string equation); From d56ffffa374d32c7f69bab1a3be82247c1636240 Mon Sep 17 00:00:00 2001 From: Jared-Gibson Date: Thu, 30 Apr 2020 23:26:07 -0400 Subject: [PATCH 06/11] Created logic for to extracting coeff from RHSexpressions -iterates through RHSexpressions -for each string in RHSexpressions, we iterate backwards since the constant will always be on the right hand side of the expression; convert the characters into an int to store into RHScoeff --- src/CalcManager/CEngine/PartialFrac.cpp | 42 +++++++++++++++++++++---- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/src/CalcManager/CEngine/PartialFrac.cpp b/src/CalcManager/CEngine/PartialFrac.cpp index c0410cd7b..ba06ab431 100644 --- a/src/CalcManager/CEngine/PartialFrac.cpp +++ b/src/CalcManager/CEngine/PartialFrac.cpp @@ -77,7 +77,8 @@ std::vector> PartialFrac::createCoeffMatrix(std::vector RHS; + std::vector RHSexpressions; + std::string LHSexpression; for (int i = 0; i < denom.size(); i++) { std::string expression; @@ -97,7 +98,7 @@ std::vector> PartialFrac::createCoeffMatrix(std::vector> PartialFrac::createCoeffMatrix(std::vector> matrix; - matrix.resize(RHS.size() + 1); + matrix.resize(RHSexpressions.size() + 1); for (int i = 0; i < matrix.size(); i++) { - matrix[i].resize(RHS.size()); + matrix[i].resize(RHSexpressions.size()); } //now that we have the expressions, now we need to add their scalars into the matrix - //assuming that the Algebra class has return expressions ordered from highest power to lowest power + // assuming that the Algebra class has return expressions ordered from highest power to lowest power with 'x' as the variable + //since we have them all in strings with white spaces separating terms and operators, we can parse the string for values. + std::vector> RHScoeffs; + for (int i = 0; i < RHSexpressions.size(); i++) + { + int innerpos = 0; + std::vector buffer; + for (std::string::reverse_iterator rit = RHSexpressions[i].rbegin(); rit != RHSexpressions[i].rend(); ++rit) + { + if (*rit == ' ') + { + if (buffer.size() != 0) //the above statement will trigger after we pass an operation character; this saves the code inside from executing on an empty buffer + { + std::stringstream ss; + for (int k = buffer.size() - 1; k >= 0; k++) + { + ss << buffer[k]; + } + ss >> RHScoeffs[i][innerpos]; + innerpos++; + } + + } + else if (*rit == 'x' || *rit == '+' || *rit == '-' || *rit == '*' || *rit == '/') // ignoring variables and operations + continue; + else + buffer.push_back(*rit); + } + } + std::vector LHScoeffs; } From 3c9cc6df2ba1008dc763d3052e308ccf66a890d7 Mon Sep 17 00:00:00 2001 From: Jared-Gibson Date: Sun, 3 May 2020 13:07:16 -0400 Subject: [PATCH 07/11] Logic for extracting LHS coefficients -uses similar logic to RHS, just without the outer for-loop -sketching out the matrix format --- src/CalcManager/CEngine/PartialFrac.cpp | 46 +++++++++++++++++++++---- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/src/CalcManager/CEngine/PartialFrac.cpp b/src/CalcManager/CEngine/PartialFrac.cpp index ba06ab431..6485da1cc 100644 --- a/src/CalcManager/CEngine/PartialFrac.cpp +++ b/src/CalcManager/CEngine/PartialFrac.cpp @@ -119,9 +119,10 @@ std::vector> PartialFrac::createCoeffMatrix(std::vector> RHScoeffs; for (int i = 0; i < RHSexpressions.size(); i++) { @@ -138,18 +139,51 @@ std::vector> PartialFrac::createCoeffMatrix(std::vector> RHScoeffs[i][innerpos]; - innerpos++; + ss >> RHScoeffs[i][innerpos++]; } } else if (*rit == 'x' || *rit == '+' || *rit == '-' || *rit == '*' || *rit == '/') // ignoring variables and operations continue; - else + else //number detected buffer.push_back(*rit); } } + //extracting LHS coefficients std::vector LHScoeffs; + int pos = 0; + std::vector buffer; + for (std::string::reverse_iterator rit = LHSexpression.rbegin(); rit != LHSexpression.rend(); ++rit) + { + if (*rit == ' ') + { + if (buffer.size() + != 0) // the above statement will trigger after we pass an operation character; this saves the code inside from executing on an empty buffer + { + std::stringstream ss; + for (int k = buffer.size() - 1; k >= 0; k++) + { + ss << buffer[k]; + } + ss >> LHScoeffs[pos++]; + } + } + else if (*rit == 'x' || *rit == '+' || *rit == '-' || *rit == '*' || *rit == '/') // ignoring variables and operations + continue; + else + buffer.push_back(*rit); + } + + /* + The matrix is formatted as follows + ------------------------------------------------------- + s^n coeff of -> | RHS[N-1] | RHS[N-2] | ... | RHS[1] | RHS[0] | LHS[M-1]| + s^n-1 coeff of-> | RHS[N-1] | RHS[N-2] | ... | RHS[1] | RHS[0] | LHS[M-2]| + ... | | ... | + s coeff of -> | RHS[N-1] | RHS[N-2] | ... | RHS[1] | RHS[0] | LHS[1] | + const coeff of -> | RHS[N-1] | RHS[N-2] | ... | RHS[1] | RHS[0] | LHS[0] | + ------------------------------------------------------- + */ } From 3eb57ad88992d0b42b142ddd3136497967aef45b Mon Sep 17 00:00:00 2001 From: Jared-Gibson Date: Sun, 3 May 2020 14:14:53 -0400 Subject: [PATCH 08/11] Completed createCoeffMatrix -added for-loops that add expression coefficients to a 2x2 integer matrix --- src/CalcManager/CEngine/PartialFrac.cpp | 37 +++++++++++++++++++------ src/CalcManager/CEngine/PartialFrac.h | 2 +- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/CalcManager/CEngine/PartialFrac.cpp b/src/CalcManager/CEngine/PartialFrac.cpp index 6485da1cc..053833c11 100644 --- a/src/CalcManager/CEngine/PartialFrac.cpp +++ b/src/CalcManager/CEngine/PartialFrac.cpp @@ -175,15 +175,36 @@ std::vector> PartialFrac::createCoeffMatrix(std::vector | RHS[N-1] | RHS[N-2] | ... | RHS[1] | RHS[0] | LHS[M-1]| - s^n-1 coeff of-> | RHS[N-1] | RHS[N-2] | ... | RHS[1] | RHS[0] | LHS[M-2]| - ... | | ... | - s coeff of -> | RHS[N-1] | RHS[N-2] | ... | RHS[1] | RHS[0] | LHS[1] | - const coeff of -> | RHS[N-1] | RHS[N-2] | ... | RHS[1] | RHS[0] | LHS[0] | - ------------------------------------------------------- + The matrix is formatted as follows: + ---------------------------------------------------------------------------------------------------------------- + s^n coeff of -> | RHS[M][N-1] | RHS[M][N-2] | ... | RHS[M][1] | RHS[M][0] | LHS[N-1] | matrix[x][0] + s^n-1 coeff of-> | RHS[M-1][N-1] | RHS[M-1][N-2] | ... | RHS[M-1][1] | RHS[M-1][0] | LHS[N-2] | matrix[x][1] + ... | ... | ... | ... | ... | ... | ... | ... + s coeff of -> | RHS[1][N-1] | RHS[1][N-2] | ... | RHS[1][1] | RHS[1][0] | LHS[1] | matrix[x][matrix[x].size()-2] + const coeff of -> | RHS[0][N-1] | RHS[0][N-2] | ... | RHS[0][1] | RHS[0][0] | LHS[0] | matrix[x][matrix[x].size()-1] + ---------------------------------------------------------------------------------------------------------------- + matrix[0] matrix[1] ... matrix[matrix.size()-3] matrix[matrix.size()-2] matrix[matrix.size()-1] */ + //LHS coeffs + //this will fill up the right-most column of the matrix + for (int N = 0; N < LHScoeffs.size(); N++) + { + matrix[matrix.size() - 1][matrix.size() - (N+1)] = LHScoeffs[N]; + } + + //RHS coeffs + //this will fill up the rest of the matrix + int N = 0; + for (int i = matrix[matrix.size() - 2].size(); i >= 0; i++) + { + for (int M = 0; M > RHScoeffs.size(); M++) + { + matrix[i][matrix[i].size() - (M + 1)] = RHScoeffs[M][N]; + } + N++; + } + + return matrix; } diff --git a/src/CalcManager/CEngine/PartialFrac.h b/src/CalcManager/CEngine/PartialFrac.h index c494690fd..ee6e0e266 100644 --- a/src/CalcManager/CEngine/PartialFrac.h +++ b/src/CalcManager/CEngine/PartialFrac.h @@ -36,7 +36,7 @@ class PartialFrac /*************** Methods that I think I no longer need - ///\returns true if demon is in factored form + ///\returns true if denom is in factored form bool factoredForm(std::string denom); ***************/ From 9a8f7384edb8938e7f72d9f4cce8645234755cb5 Mon Sep 17 00:00:00 2001 From: Jared-Gibson Date: Sun, 3 May 2020 18:42:58 -0400 Subject: [PATCH 09/11] Fixing errors in PartialFrac files --- src/CalcManager/CEngine/PartialFrac.cpp | 36 ++++++++++++++++++------- src/CalcManager/CEngine/PartialFrac.h | 3 +-- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/CalcManager/CEngine/PartialFrac.cpp b/src/CalcManager/CEngine/PartialFrac.cpp index 053833c11..bfeb551d7 100644 --- a/src/CalcManager/CEngine/PartialFrac.cpp +++ b/src/CalcManager/CEngine/PartialFrac.cpp @@ -24,30 +24,31 @@ std::string PartialFrac::PartialFracDecomp(std::string equation) std::vector PartialFrac::splitEquationString(std::string equation) { std::vector tmp; + tmp.resize(2); //the following find statements scan for all types of ways the user could have typed their equation - int pos = equation.find(")/("); - if (!pos == std::string::npos) + size_t pos = equation.find(")/("); + if (pos != std::string::npos) { tmp[0] = equation.substr(0, pos + 1); tmp[1] = equation.substr(pos + 2, equation.length()); return tmp; } pos = equation.find(")/"); - if (!pos == std::string::npos) + if (pos != std::string::npos) { tmp[0] = equation.substr(0, pos + 1); tmp[1] = equation.substr(pos + 2, equation.length()); return tmp; } pos = equation.find("/("); - if (!pos == std::string::npos) + if (pos != std::string::npos) { tmp[0] = equation.substr(0, pos); tmp[1] = equation.substr(pos + 1, equation.length()); return tmp; } pos = equation.find("/"); - if (!pos == std::string::npos) + if (pos != std::string::npos) { tmp[0] = equation.substr(0, pos); tmp[1] = equation.substr(pos + 1, equation.length()); @@ -71,6 +72,7 @@ std::vector PartialFrac::splitEquationString(std::string equation) std::vector PartialFrac::factorExpr(std::string equation) { //probably going to use some Algebra class logic here, so I am going to leave this blank for now + throw "Not implemented"; } std::vector> PartialFrac::createCoeffMatrix(std::vector num, std::vector denom) @@ -95,7 +97,7 @@ std::vector> PartialFrac::createCoeffMatrix(std::vector> PartialFrac::createCoeffMatrix(std::vector= 0; k++) + for (size_t k = buffer.size() - 1; k >= 0; k++) { ss << buffer[k]; } @@ -161,7 +163,7 @@ std::vector> PartialFrac::createCoeffMatrix(std::vector= 0; k++) + for (size_t k = buffer.size() - 1; k >= 0; k++) { ss << buffer[k]; } @@ -196,7 +198,7 @@ std::vector> PartialFrac::createCoeffMatrix(std::vector= 0; i++) + for (size_t i = matrix[matrix.size() - 2].size(); i >= 0; i++) { for (int M = 0; M > RHScoeffs.size(); M++) { @@ -206,5 +208,19 @@ std::vector> PartialFrac::createCoeffMatrix(std::vector PartialFrac::splitDenom(std::string denom) +{ + throw "Not implemented"; +} + +std::vector PartialFrac::solveMatrix(std::vector> matrix) +{ + throw "Not implemented"; +} + +std::string PartialFrac::stringBuilder(std::vector coeff, std::vector factors) +{ + throw "Not implemented"; } diff --git a/src/CalcManager/CEngine/PartialFrac.h b/src/CalcManager/CEngine/PartialFrac.h index ee6e0e266..d69a437d9 100644 --- a/src/CalcManager/CEngine/PartialFrac.h +++ b/src/CalcManager/CEngine/PartialFrac.h @@ -1,4 +1,5 @@ #pragma once +#include /** * Module Name: PartialFrac.h * Author: Jared Gibson @@ -14,8 +15,6 @@ class PartialFrac PartialFrac(); ///\returns the partial fraction decomposition of the equation provided std::string PartialFracDecomp(std::string equation); - -private: ///\returns a vector where [0] is the numerator and [1] is denominator std::vector splitEquationString(std::string equation); struct factor { From e9b6183a452376f9ad5f33aec5fb612f9cef4414 Mon Sep 17 00:00:00 2001 From: Jared-Gibson Date: Sun, 3 May 2020 19:16:56 -0400 Subject: [PATCH 10/11] splitEquationString unit test --- src/CalcManager/CEngine/PartialFrac.cpp | 3 +- .../CalculatorUnitTests.vcxproj | 1 + .../CalculatorUnitTests.vcxproj.filters | 3 ++ src/CalculatorUnitTests/Example.cpp | 2 +- src/CalculatorUnitTests/PartialFracTests.cpp | 39 +++++++++++++++++++ 5 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/CalculatorUnitTests/PartialFracTests.cpp diff --git a/src/CalcManager/CEngine/PartialFrac.cpp b/src/CalcManager/CEngine/PartialFrac.cpp index bfeb551d7..d5181372c 100644 --- a/src/CalcManager/CEngine/PartialFrac.cpp +++ b/src/CalcManager/CEngine/PartialFrac.cpp @@ -198,7 +198,8 @@ std::vector> PartialFrac::createCoeffMatrix(std::vector= 0; i++) + size_t startpos = matrix.size() - 2; + for (size_t i = matrix[startpos].size() - 1; i >= 0; i--) { for (int M = 0; M > RHScoeffs.size(); M++) { diff --git a/src/CalculatorUnitTests/CalculatorUnitTests.vcxproj b/src/CalculatorUnitTests/CalculatorUnitTests.vcxproj index 13375dd03..0b273b0c1 100644 --- a/src/CalculatorUnitTests/CalculatorUnitTests.vcxproj +++ b/src/CalculatorUnitTests/CalculatorUnitTests.vcxproj @@ -244,6 +244,7 @@ + diff --git a/src/CalculatorUnitTests/CalculatorUnitTests.vcxproj.filters b/src/CalculatorUnitTests/CalculatorUnitTests.vcxproj.filters index 2121a4616..cc076e3cb 100644 --- a/src/CalculatorUnitTests/CalculatorUnitTests.vcxproj.filters +++ b/src/CalculatorUnitTests/CalculatorUnitTests.vcxproj.filters @@ -33,6 +33,9 @@ Team 11 Unit Tests + + Team 11 Unit Tests + diff --git a/src/CalculatorUnitTests/Example.cpp b/src/CalculatorUnitTests/Example.cpp index 3e7fafe4d..41f75571e 100644 --- a/src/CalculatorUnitTests/Example.cpp +++ b/src/CalculatorUnitTests/Example.cpp @@ -3,7 +3,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; -namespace Example +namespace Team11_UnitTests { /// /// An example to showcase how to create unit tests; in order to run tests diff --git a/src/CalculatorUnitTests/PartialFracTests.cpp b/src/CalculatorUnitTests/PartialFracTests.cpp new file mode 100644 index 000000000..cc80c569c --- /dev/null +++ b/src/CalculatorUnitTests/PartialFracTests.cpp @@ -0,0 +1,39 @@ +#include "pch.h" +#include "CppUnitTest.h" +#include "CEngine\PartialFrac.h" + + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace Team11_UnitTests +{ + TEST_CLASS(PartialFracUnitTests) + { + public: + TEST_METHOD(test_splitEquationString) + { + PartialFrac testObj = PartialFrac(); + + std::string input1 = "(x+1)/(x+2)(x+3)"; + std::string expectedNum1 = "(x+1)"; + std::string expectedDenom1 = "(x+2)(x+3)"; + std::vector output1 = testObj.splitEquationString(input1); + Assert::AreEqual(output1[0], expectedNum1); + Assert::AreEqual(output1[1], expectedDenom1); + + std::string input2 = "x+1/(x+2)(x+3)"; + std::string expectedNum2 = "x+1"; + std::string expectedDenom2 = "(x+2)(x+3)"; + std::vector output2 = testObj.splitEquationString(input2); + Assert::AreEqual(output2[0], expectedNum2); + Assert::AreEqual(output2[1], expectedDenom2); + + std::string input3 = "x+1/x+2"; + std::string expectedNum3 = "x+1"; + std::string expectedDenom3 = "x+2"; + std::vector output3 = testObj.splitEquationString(input3); + Assert::AreEqual(output3[0], expectedNum3); + Assert::AreEqual(output3[1], expectedDenom3); + } + }; +} From b1d96cdbfc929e1fbb4983ea4794da1759f34f88 Mon Sep 17 00:00:00 2001 From: Jared-Gibson Date: Sun, 3 May 2020 19:24:25 -0400 Subject: [PATCH 11/11] createCoeffMatrix unit test -This method does not fully work as it requires the Algebra class which is not implemented at this time; however, I wrote a test case for it anyway --- src/CalculatorUnitTests/PartialFracTests.cpp | 42 ++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/CalculatorUnitTests/PartialFracTests.cpp b/src/CalculatorUnitTests/PartialFracTests.cpp index cc80c569c..f517f23a1 100644 --- a/src/CalculatorUnitTests/PartialFracTests.cpp +++ b/src/CalculatorUnitTests/PartialFracTests.cpp @@ -35,5 +35,47 @@ namespace Team11_UnitTests Assert::AreEqual(output3[0], expectedNum3); Assert::AreEqual(output3[1], expectedDenom3); } + //this method WILL fail since the algebra class is not implemented + TEST_METHOD(test_createCeoffMatrix) + { + PartialFrac testObj = PartialFrac(); + + std::vector num; + PartialFrac::factor tmp1; + tmp1.variable = "x"; + tmp1.operation = "+"; + tmp1.constant = 1; + num.push_back(tmp1); + + std::vector denom; + tmp1.variable = "x"; + tmp1.operation = "+"; + tmp1.constant = 2; + denom.push_back(tmp1); + tmp1.variable = "x"; + tmp1.operation = "+"; + tmp1.constant = 3; + denom.push_back(tmp1); + + /* + expected matrix + ------------- + | 1 | 1 | 1 | + | 3 | 2 | 1 | + ------------- + */ + std::vector> expectedOutput; + expectedOutput.resize(3); + expectedOutput[0].push_back(1); + expectedOutput[0].push_back(3); + expectedOutput[1].push_back(1); + expectedOutput[1].push_back(2); + expectedOutput[3].push_back(1); + expectedOutput[3].push_back(1); + + std::vector> output = testObj.createCoeffMatrix(num, denom); + + Assert::AreEqual(output, expectedOutput); + } }; }