SHOW:
|
|
- or go back to the newest paste.
1 | #include <map> | |
2 | #include <iostream> | |
3 | #include <vector> | |
4 | #include <complex> | |
5 | #include <functional> | |
6 | ||
7 | using namespace std; | |
8 | class LINP | |
9 | { | |
10 | - | typedef map<int/*pow*/, double/*num*/, greater<int> > value_type; |
10 | + | |
11 | typedef map<int/*Index*/, double/*Coeff*/, greater<int> > value_type; | |
12 | ||
13 | - | LINP(): cont() |
13 | + | |
14 | - | { |
14 | + | LINP(): cont() { |
15 | cont[1] = 1; | |
16 | }; | |
17 | - | LINP(int rhs): cont() |
17 | + | LINP(int rhs): cont() { |
18 | - | { |
18 | + | |
19 | }; | |
20 | ||
21 | - | LINP operator [](int rhs) const//FIXME: Use operator *() to do this |
21 | + | LINP operator [](int rhs) const { |
22 | - | { |
22 | + | |
23 | tmp.cont.clear(); | |
24 | tmp.cont[rhs] = getCoeffOfIndex(1); | |
25 | - | tmp.cont[rhs] = getNumOfPow(1); |
25 | + | |
26 | } | |
27 | ||
28 | - | LINP operator *(double rhs) const |
28 | + | LINP& operator *=(double rhs) { |
29 | - | { |
29 | + | for (auto& v : cont) |
30 | v.second *= rhs; | |
31 | - | for (value_type::iterator it=tmp.cont.begin(); it!=tmp.cont.end(); ++it) |
31 | + | return *this; |
32 | - | it->second *= rhs; |
32 | + | |
33 | LINP operator *(double rhs) const { | |
34 | LINP tmp(*this); | |
35 | - | LINP operator +(const LINP& rhs) const |
35 | + | tmp *= rhs; |
36 | - | { |
36 | + | |
37 | } | |
38 | - | for (value_type::const_iterator it=rhs.cont.begin(); it!=rhs.cont.end(); ++it) |
38 | + | |
39 | - | tmp.cont[it->first] += it->second; |
39 | + | LINP operator *=(const LINP& rhs) const { |
40 | // Unimplimented | |
41 | } | |
42 | - | LINP operator -() const |
42 | + | LINP operator *(const LINP& rhs) const { |
43 | - | { |
43 | + | |
44 | tmp *= rhs; | |
45 | - | for (value_type::iterator it=tmp.cont.begin(); it!=tmp.cont.end(); ++it) |
45 | + | |
46 | - | it->second=-it->second; |
46 | + | |
47 | ||
48 | LINP& operator +=(const LINP& rhs) { | |
49 | - | LINP operator -(const LINP& rhs) const |
49 | + | for (auto& v : rhs.cont) |
50 | - | { |
50 | + | this->cont[v.first] += v.second; |
51 | - | return *this+(-rhs); |
51 | + | return *this; |
52 | } | |
53 | - | friend ostream& operator <<(ostream& os, const LINP& rhs) |
53 | + | |
54 | - | { |
54 | + | LINP operator +(const LINP& rhs) const { |
55 | LINP tmp(*this); | |
56 | - | it!=rhs.cont.end(); ++it) |
56 | + | tmp += rhs; |
57 | return tmp; | |
58 | } | |
59 | ||
60 | LINP& operator -=(const LINP& rhs) { | |
61 | for (auto& v : rhs.cont) | |
62 | this->cont[v.first] -= v.second; | |
63 | return *this; | |
64 | } | |
65 | LINP operator -(const LINP& rhs) const { | |
66 | LINP tmp(*this); | |
67 | tmp -= rhs; | |
68 | return tmp; | |
69 | } | |
70 | LINP operator -() const { | |
71 | LINP tmp(*this); | |
72 | for (auto& v : tmp.cont) | |
73 | v.second = -v.second; | |
74 | return tmp; | |
75 | } | |
76 | ||
77 | friend ostream& operator <<(ostream& os, const LINP& rhs) { | |
78 | for (value_type::const_iterator it=rhs.cont.begin(); | |
79 | - | int maxPow() const |
79 | + | it != rhs.cont.end(); ++it) |
80 | - | { |
80 | + | |
81 | if (it->second != 0) | |
82 | { | |
83 | - | int minPow() const |
83 | + | |
84 | - | { |
84 | + | |
85 | if (it->second < 0) | |
86 | cout << '-'; | |
87 | - | double getNumOfPow(int Pow) const |
87 | + | |
88 | - | { |
88 | + | |
89 | - | value_type::const_iterator it=cont.find(Pow); |
89 | + | |
90 | - | return it!=cont.end()? it->second : 0; |
90 | + | |
91 | { | |
92 | - | vector<complex<double> > solve() const throw(exception) //Solve: *this=0 |
92 | + | |
93 | - | { |
93 | + | |
94 | - | if (maxPow()>2 || minPow()<0) |
94 | + | |
95 | } | |
96 | } | |
97 | - | result.reserve(maxPow()); |
97 | + | |
98 | - | if (maxPow()==1) |
98 | + | |
99 | - | result.push_back(-getNumOfPow(0)/getNumOfPow(1)); |
99 | + | |
100 | return os; | |
101 | } | |
102 | - | complex<double> delta=pow(getNumOfPow(1), 2)-4*getNumOfPow(2)*getNumOfPow(0); |
102 | + | |
103 | - | result.push_back((-getNumOfPow(1)+sqrt(delta))/(2*getNumOfPow(2))); |
103 | + | int maxIndex() const { |
104 | - | result.push_back((-getNumOfPow(1)-sqrt(delta))/(2*getNumOfPow(2))); |
104 | + | |
105 | } | |
106 | int minIndex() const { | |
107 | return cont.rbegin()->first; | |
108 | } | |
109 | ||
110 | double getCoeffOfIndex(int Index) const { | |
111 | value_type::const_iterator it=cont.find(Index); | |
112 | return it != cont.end() ? it->second : 0; | |
113 | } | |
114 | ||
115 | vector<complex<double> > solve() const throw(exception) { | |
116 | if (maxIndex()>2 || minIndex()<0) | |
117 | throw std::exception(); | |
118 | vector<complex<double> > result; | |
119 | result.reserve(maxIndex()); | |
120 | if (maxIndex()==1) | |
121 | result.push_back(-getCoeffOfIndex(0)/getCoeffOfIndex(1)); | |
122 | else // maxPow()==2 | |
123 | { | |
124 | complex<double> delta=pow(getCoeffOfIndex(1), 2)-4*getCoeffOfIndex(2)*getCoeffOfIndex(0); | |
125 | result.push_back((-getCoeffOfIndex(1)+sqrt(delta))/(2*getCoeffOfIndex(2))); | |
126 | result.push_back((-getCoeffOfIndex(1)-sqrt(delta))/(2*getCoeffOfIndex(2))); | |
127 | } | |
128 | return result; | |
129 | } | |
130 | ||
131 | private: | |
132 | value_type cont; | |
133 | }; | |
134 | ||
135 | int main() | |
136 | { | |
137 | LINP linp=LINP()-(LINP()*0.5+1)-1; | |
138 | cout << linp << " = 0" << endl; | |
139 | cout << "X = " << linp.solve().front().real() << endl << endl; | |
140 | ||
141 | LINP linp2=LINP()[2]+LINP()*2+1; | |
142 | cout << linp2 << " = 0" << endl; | |
143 | cout << "X1 = X2 = " << linp2.solve().front().real() << endl << endl; | |
144 | ||
145 | LINP linp3=LINP()[2]+1; | |
146 | cout << linp3 << " = 0" << endl; | |
147 | vector<complex<double> > result=linp3.solve(); | |
148 | cout << "x1 = " << result[0].real() << " + " << result[0].imag() << 'i' << endl | |
149 | << "x2 = " << result[1].real() << " + " << result[1].imag() << 'i' << endl << endl; | |
150 | } |