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?

2
Contributors
9
Replies
10
Views
10 Years
Discussion Span
Last Post by Alex Edwards

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?

Is it required for you to use an exponent as a key?

I can think of 2 other ways of doing it if its not required.

Does it still involve using maps? because I have to use maps.

Does it still involve using maps? because I have to use maps.

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

Yeah is the first one.

x^2 + 9x - 10

c = 1, exp = 2
c = 9, exp = 1
c = 10, exp = 0

Yeah is the first one.

x^2 + 9x - 10

c = 1, exp = 2
c = 9, exp = 1
c = 10, exp = 0

Oh then it looks like you have the right approach.

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?

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

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

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.

For Multiplication...

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.

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.

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.

Good, because that would involve Regex or factoring the regular polynomial into a general polynomial for both lines and finding a matching factored polynomial from both lines.

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.