in reply to minimizing simple linear equations

If you want comprehensive symbolic manipulation, I'm not sure where to point you to..

But for the case described above (and feel free to use this as a starting point to write your own comprehensive symbolic manipulation module), you can do:

$string =~ s/ //gs; # remove spaces $string =~ s/\((\w+)\*(\d+)\)\+\(\1\*(\d+))/($1\*(?{$2+$3})\)/gsx;

Which should do only exactly the substitution you describe above. (Needs testing)