Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:

I would like to be able to join math variables. What I mean with this is that, for example, the following can be joined (I don't know if this is actually called joining):

2x^2 - 5x^2 + 2x + 3x - 5 + 6
join to (is equal to):
-3x^2 + 5x + 1

Is there a library or does someone have a code snippet for such actions? If you have a hint or an easy trick, also please tell me. Thanks.

Posted
Updated 25-Aug-09 10:28am
v2

1 solution

The process you're talking about is called simplifying. I would assume it's done by going through each element (where element 0 is 2x2, element 1 is -5x2, etc) and making a note of each unique index (the power). When you encounter an index that's occurred before, add the current element to the unique element that's got the matching index and move on.

After that you just need to display it. With this, you just iterate through the list of unique elements and print them.

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900