| | |
An alternative approach?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 58
Reputation:
Solved Threads: 2
I am using Maps from the STL to insert a polynomial.
The Key is the exponent and the data is the coefficent.
Basically I want to add/subtract and multiply them.
I have the addition/subtraction down but when it comes to multiplying
I have to add the exponents and multiply the coefficients.
I can't alter the exponent cause is the key.
Is there an alternate approach? Can the key be modified?
The Key is the exponent and the data is the coefficent.
Basically I want to add/subtract and multiply them.
I have the addition/subtraction down but when it comes to multiplying
I have to add the exponents and multiply the coefficients.
I can't alter the exponent cause is the key.
Is there an alternate approach? Can the key be modified?
•
•
•
•
I am using Maps from the STL to insert a polynomial.
The Key is the exponent and the data is the coefficent.
Basically I want to add/subtract and multiply them.
I have the addition/subtraction down but when it comes to multiplying
I have to add the exponents and multiply the coefficients.
I can't alter the exponent cause is the key.
Is there an alternate approach? Can the key be modified?
I can think of 2 other ways of doing it if its not required.
Yes.
I was thinking of something along the lines of--
Map<int, vector<int> >
--where you store the values in the map in such a way that if you ever add a key/value to the map that is the same, you can overwrite the old key with the new key and old value in the same vector as the new value..
Also, what kind of expressions are you doing? Something along the lines of--
x^2 + 9x - 10
-- ?
Or is it something like--
Keys (exponents) -> 2, 3, 4
Values (coefficients) -> 1, 10, 2
1^2 (+, -, /, *) 10^3 (+, -, /, *) 2^4
I need some more information
I was thinking of something along the lines of--
Map<int, vector<int> >
--where you store the values in the map in such a way that if you ever add a key/value to the map that is the same, you can overwrite the old key with the new key and old value in the same vector as the new value..
Also, what kind of expressions are you doing? Something along the lines of--
x^2 + 9x - 10
-- ?
Or is it something like--
Keys (exponents) -> 2, 3, 4
Values (coefficients) -> 1, 10, 2
1^2 (+, -, /, *) 10^3 (+, -, /, *) 2^4
I need some more information
Last edited by Alex Edwards; Jun 21st, 2008 at 12:39 am.
•
•
•
•
Yeah is the first one.
x^2 + 9x - 10
c = 1, exp = 2
c = 9, exp = 1
c = 10, exp = 0
Are you then replacing the x's with a said-value and then doing the math?
And the type of equations you're having issues with are --
x^2 * 9x / 10
--correct?
•
•
Join Date: Oct 2007
Posts: 58
Reputation:
Solved Threads: 2
I have to add/subtract/multiply 2 lines of polynominals
say line 1 is
x^2+3x+3
line 2 is
3x^2+4x+4
adding would be
4x^2+7x+7
and so on...
I have map<int, int>
exp as key and coef as data
I gotten the adding and subtracting but is the multiplying is giving me trouble because I have to modify the exp which is the key.
say line 1 is
x^2+3x+3
line 2 is
3x^2+4x+4
adding would be
4x^2+7x+7
and so on...
I have map<int, int>
exp as key and coef as data
I gotten the adding and subtracting but is the multiplying is giving me trouble because I have to modify the exp which is the key.
•
•
•
•
I have to add/subtract/multiply 2 lines of polynominals
say line 1 is
x^2+3x+3
line 2 is
3x^2+4x+4
adding would be
4x^2+7x+7
and so on...
I have map<int, int>
exp as key and coef as data
I gotten the adding and subtracting but is the multiplying is giving me trouble because I have to modify the exp which is the key.
What you could do is make another map a receiver map.
You know how many nodes you'll need because through multiplication you would need Line1 coefficients * Line2 coefficients, so you'll need 9 slots to store the new polynomial.
From there all you would need to do is multiply Node1 from Map1 with the All 3 nodes from Map2 and store the result nodes in the receiver map. Rinse and repeat for Node2 and Node3 from Map1.
If you run into the same exponent then simply do addition. You'll have at most Line1 * Line2 nodes, but its possible that if a key collision occurs then you should replace the old value with the sum of the new value and the old value - the key should be the same.
As for division im still thinking about it. Thanks for being a bit more thorough with the information.
Last edited by Alex Edwards; Jun 21st, 2008 at 1:07 am.
•
•
•
•
Oh I see,
multiply the polynomials and store the newly valued exp and coef in a new map?
Ill give it a shot and let you know. Thanks!
As for the division, I am not required to do it.
What happens if they're not factorable is beyond me - you'd have to literally store the entire expression in one node, or inform the user that they cannot be divided generally.
![]() |
Similar Threads
- Alternative approach to frames....? (Site Layout and Usability)
- GWBASIC and XP Pro (Legacy and Other Languages)
- Stopping PHP (JavaScript / DHTML / AJAX)
- CSS style in FireFox (HTML and CSS)
- checkbox in datagridview c# (C#)
- Problems downloading applications from websites (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: inheritance with input command
- Next Thread: C++ Inheritance Question
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





