Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @a=map{[split'\+|(?=-)']} # Split each factor into components
- <>=~s/\n//r=~s/(\(.+?\))\^(\d)/$1x$2/ger=~/[^()]+/g; # Get factors by flattening exponents then splitting list on parenthesized expressions
- for(@{$a[0]}){
- $l{/\d/?'':$_}=/\d/?$_:1 # Initialize %l (hash from variable to coefficient) with first factor
- }
- for(1..$#a){ # Loop through other factors to multiply
- %b=(); # Temporary hash to store current result in
- for(@{$a[$_]}){ # Loop $_ through components of current factor
- while(($k,$v)=each%l){ # Loop $k,$v through variables and coefficients of current polynomial
- $b{/\d/?$k:join'',sort($_,split'',$k)}+=/\d/?$v*$_:$v # If component is a number multiply coefficients by it, otherwise append it to variables (and sort to keep unique indexes)
- }
- }
- %l=%b # Set current polynomial to temporary result
- }
- print(
- (join' ',map{"$l{$_}$_"}sort{length$b<=>length$a}keys%l) # Format hash as list ordered by degree
- =~s/\b1(?=[a-z])//gr=~s/-/- /gr=~s/\b \b/ + /gr=~s/(([a-z])\2+)/"$2^".length$1/gre) # String substitutions to do the rest of the formatting
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement