An alternative approach?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 58
Reputation: ff4930 is an unknown quantity at this point 
Solved Threads: 2
ff4930 ff4930 is offline Offline
Junior Poster in Training

An alternative approach?

 
0
  #1
Jun 21st, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: An alternative approach?

 
0
  #2
Jun 21st, 2008
Originally Posted by ff4930 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 58
Reputation: ff4930 is an unknown quantity at this point 
Solved Threads: 2
ff4930 ff4930 is offline Offline
Junior Poster in Training

Re: An alternative approach?

 
0
  #3
Jun 21st, 2008
Does it still involve using maps? because I have to use maps.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: An alternative approach?

 
0
  #4
Jun 21st, 2008
Originally Posted by ff4930 View Post
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

I need some more information
Last edited by Alex Edwards; Jun 21st, 2008 at 12:39 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 58
Reputation: ff4930 is an unknown quantity at this point 
Solved Threads: 2
ff4930 ff4930 is offline Offline
Junior Poster in Training

Re: An alternative approach?

 
0
  #5
Jun 21st, 2008
Yeah is the first one.

x^2 + 9x - 10

c = 1, exp = 2
c = 9, exp = 1
c = 10, exp = 0
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: An alternative approach?

 
0
  #6
Jun 21st, 2008
Originally Posted by ff4930 View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 58
Reputation: ff4930 is an unknown quantity at this point 
Solved Threads: 2
ff4930 ff4930 is offline Offline
Junior Poster in Training

Re: An alternative approach?

 
0
  #7
Jun 21st, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: An alternative approach?

 
0
  #8
Jun 21st, 2008
Originally Posted by ff4930 View Post
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.
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.
Last edited by Alex Edwards; Jun 21st, 2008 at 1:07 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 58
Reputation: ff4930 is an unknown quantity at this point 
Solved Threads: 2
ff4930 ff4930 is offline Offline
Junior Poster in Training

Re: An alternative approach?

 
0
  #9
Jun 21st, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: An alternative approach?

 
0
  #10
Jun 21st, 2008
Originally Posted by ff4930 View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC