Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding linear equation solver

  • Loading branch information...
commit 22efb219909bc84c3a0d1f95a5d7355682bb77b7 1 parent 1030d38
@eagle2com authored
View
BIN  LaplaceInverse.sdf
Binary file not shown
View
BIN  LaplaceInverse.suo
Binary file not shown
View
2  LaplaceInverse/LaplaceInverse.vcxproj
@@ -12,6 +12,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Factor.cpp" />
+ <ClCompile Include="src\LinearSystem.cpp" />
<ClCompile Include="src\Literal.cpp" />
<ClCompile Include="src\LiteralElement.cpp" />
<ClCompile Include="src\main.cpp" />
@@ -19,6 +20,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\Factor.h" />
+ <ClInclude Include="src\LinearSystem.h" />
<ClInclude Include="src\Literal.h" />
<ClInclude Include="src\LiteralElement.h" />
<ClInclude Include="src\TransferFunction.h" />
View
6 LaplaceInverse/LaplaceInverse.vcxproj.filters
@@ -30,6 +30,9 @@
<ClCompile Include="src\LiteralElement.cpp">
<Filter>Source Files</Filter>
</ClCompile>
+ <ClCompile Include="src\LinearSystem.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\Factor.h">
@@ -44,5 +47,8 @@
<ClInclude Include="src\LiteralElement.h">
<Filter>Header Files</Filter>
</ClInclude>
+ <ClInclude Include="src\LinearSystem.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
</ItemGroup>
</Project>
View
35 LaplaceInverse/src/LinearSystem.cpp
@@ -0,0 +1,35 @@
+#include "LinearSystem.h"
+#include <iostream>
+using namespace std;
+
+LinearSystem::LinearSystem(int size):m_size(size)
+{
+ m_matrix = new long double[size*(size+1)];
+ if(!m_matrix)
+ {
+ cout << "omg epic memory fail"<<endl;
+ }
+}
+
+const long double LinearSystem::Get(int n, int m)// line, column
+{
+ return m_matrix[n*(m_size+1) + m];
+}
+
+void LinearSystem::Set(int n, int m, long double value)
+{
+ m_matrix[n*(m_size+1) + m] = value;
+}
+
+void LinearSystem::Solve(void) // solve using the gauss method (Reduced echelon form)
+{
+ for(int var = 0 ; var < m_size; var++)
+ {
+
+ }
+}
+
+
+LinearSystem::~LinearSystem(void)
+{
+}
View
14 LaplaceInverse/src/LinearSystem.h
@@ -0,0 +1,14 @@
+#pragma once
+class LinearSystem
+{
+public:
+ LinearSystem(int size); //only one dimension is required; we assume we have n variables and n equations
+ ~LinearSystem(void);
+ const long double Get(int n, int m); // line, column
+ void Set(int n, int m, long double value);
+ void Solve(void);
+private:
+ long double *m_matrix;
+ int m_size;
+};
+
View
8 LaplaceInverse/src/LiteralElement.cpp
@@ -3,8 +3,10 @@
#include <cstdio>
using namespace std;
-LiteralElement::LiteralElement(void)
+LiteralElement::LiteralElement(bool interactive)
{
+ if(!interactive)
+ return;
int exp = 0;
int max_exp = 0;
long double coef;
@@ -53,7 +55,9 @@ void LiteralElement::Print(void)
else
sprintf(sign_buffer,"");
- if(it->second->coef == 1)
+ if(it->second->coef == 1 && it->first == 0)
+ sprintf(coef_buffer,"1");
+ else if(it->second->coef == 1)
sprintf(coef_buffer,"");
else
sprintf(coef_buffer,"%1.0lf",it->second->coef);
View
17 LaplaceInverse/src/LiteralElement.h
@@ -9,7 +9,7 @@ typedef LiteralMap::iterator LiteralMapIt;
class LiteralElement
{
public:
- LiteralElement(void);
+ LiteralElement(bool interactive = false);
~LiteralElement(void);
Literal* Get(unsigned int index)
{
@@ -45,7 +45,7 @@ class LiteralElement
LiteralElement* operator*(LiteralElement& l2)
{
- LiteralElement* result = new LiteralElement();
+ LiteralElement* result = new LiteralElement(false);
for(LiteralMapIt it1 = elements.begin();it1 != elements.end();it1++)
{
for(LiteralMapIt it2 = l2.elements.begin();it2 != l2.elements.end();it2++)
@@ -58,6 +58,19 @@ class LiteralElement
return result;
}
+ void operator*=(LiteralElement& l2)
+ {
+ for(LiteralMapIt it1 = elements.begin();it1 != elements.end();it1++)
+ {
+ for(LiteralMapIt it2 = l2.elements.begin();it2 != l2.elements.end();it2++)
+ {
+
+ this->Add((*(it1->second))*(*(it2->second)));
+ }
+ }
+ this->Simplify();
+ }
+
protected:
//std::vector<Literal*> elements; //all elements in basic denominator
std::map<unsigned int,Literal*> elements;
View
26 LaplaceInverse/src/TransferFunction.cpp
@@ -9,14 +9,18 @@ TransferFunction::TransferFunction(void)
void TransferFunction::AddNum(void)
{
- numerator.push_back(new Factor());
+ cout << "Adding numerator"<<endl;
+ numerator.push_back(new LiteralElement(true));
numerator.back()->Print();
cout<<endl;
}
void TransferFunction::AddDenom(void)
{
- denominator.push_back(new Factor());
+ cout << "Adding denominator"<<endl;
+ denominator.push_back(new LiteralElement(true));
+ denominator.back()->Print();
+ cout<<endl;
}
@@ -31,3 +35,21 @@ TransferFunction::~TransferFunction(void)
delete denominator[i];
}
}
+
+void TransferFunction::FindFactors(void)
+{
+ LiteralElement* final_numerator = new LiteralElement();
+ final_numerator->Add(new Literal(1,0));
+ for(int i = 0; i < numerator.size();i++)
+ {
+ final_numerator = (*final_numerator)*(*numerator[i]);
+ }
+
+ cout << "Expanded numerator: ";final_numerator->Print(); cout << endl;
+ cout << "Denominator: ";
+ for(int i = 0; i < denominator.size(); i++)
+ denominator[i]->Print();
+ cout << endl;
+ cout << "We will need "<<denominator.size()<<" coefficients"<<endl;
+
+}
View
12 LaplaceInverse/src/TransferFunction.h
@@ -1,9 +1,9 @@
#pragma once
-#include "Factor.h"
+#include "LiteralElement.h"
#include <vector>
-typedef std::vector<Factor*> FactorVec;
-typedef FactorVec::iterator FactorVecIt;
+typedef std::vector<LiteralElement*> ElementVec;
+typedef ElementVec::iterator ElementVecIt;
class TransferFunction
{
@@ -12,9 +12,11 @@ class TransferFunction
~TransferFunction(void);
void AddNum(void);
void AddDenom(void);
+ void FindFactors(void);
private:
- FactorVec numerator;
- FactorVec denominator;
+ ElementVec numerator;
+ ElementVec denominator;
+ ElementVec factors;
};
View
8 LaplaceInverse/src/main.cpp
@@ -13,9 +13,11 @@ TransferMap functions;
int main(int argc, char **argv)
{
- LiteralElement elem1;
+ TransferFunction f1;
+ f1.AddNum();
+ f1.AddDenom();
+ f1.AddDenom();
-
- elem1.Print();cout<<endl;
+ f1.FindFactors();
return 0;
}
Please sign in to comment.
Something went wrong with that request. Please try again.