Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to write a php code for factorizing algebra expression like (x^2-4X+4) to (x-2)(x-2).
Please show me the right direction, I'll be grateful..

Thanks
Posted

Right direction is parsing expression and building expression trees; factorizing and many other operations will be the operations working with those trees as operands.

This is a huge topic called Computer Algebra Systems (CAS), see http://en.wikipedia.org/wiki/Computer_algebra_system[^]. Ready to dedicate a good part of your life to this topic? My first advice would be throwing out PHP for something more serious… :-)

—SA
 
Share this answer
 
Comments
CPallini 1-Aug-11 4:18am    
Good answer, but the last sentence. :-)
(I like expecially the 'ready to dedicate...' part).
My 5.
Sergey Alexandrovich Kryukov 1-Aug-11 4:23am    
Thank you for voting. As to last sentence... well, just my opinion, may be biased, but not just mine. And I'm the author of some CAS work, so I know something about what I'm talking about... :-)
--SA
CPallini 1-Aug-11 4:27am    
Just curiosity: what programming language did you use?
Sergey Alexandrovich Kryukov 1-Aug-11 9:27am    
First, Ada, later C++, Delphi and C# -- all different implementations. This was our approach where we were able to use one of those languages as input language for CAS, which impose requirements to the language.
--SA
CPallini 1-Aug-11 9:29am    
'C++, Delphi and C#': on the MS route... :-)
Thank you.
As general point of view the SAKryukov answer is the correct one. However, if you need to factorize always simple expressions like the one you posted then you may just build a simple parser for the input expression (read one of the many tutorials available on parsing) and implement the silly (in such cases) decomposition alogorithm.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Aug-11 9:29am    
Agree, my 5, but try to imagine the purpose of it. I would guess today this is factorization, tomorrow something else. What's so good about factorization along? -- I don't know.
--SA
For a do-it-yoursel solution, you will need

- a tiny parser to extract the polynomial coefficients (a regular expression analyzer could probably do);

- a root finder; if you limit yourself to the 4th degree, analytic formulas are available http://mathforum.org/dr.math/faq/faq.cubic.equations.html[^]; otherwise you'll need an algorithm from numerical analysis. http://en.wikipedia.org/wiki/Root-finding_algorithm#Finding_roots_of_polynomials[^] (Caution, this material might horrify you.)

If you explain the exact purpose, maybe shortcuts are possible...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Aug-11 9:29am    
My 5.
--SA

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