Say I have many expressions of the form $$\text{expr} = \frac{1}{AB(C+D)^n E^m}$$ where $A,B,C,D$ and $E$ are symbols and $n,m$ arbitrary powers. In order to convert between the mathematica output and the output read by another software, I'd like to make the following replacements

expr /. {1/A -> A^-1, 1/B -> B^-1, 1/(C+D)^n -> (C+D)^-n, 1/E^m -> E^-m} so as to rewrite my expression equivalently as $$\text{expr} = A^{-1} B^{-1} (C+D)^{-n} E^{-m}$$

As far as I understand, mathematica would make replacements to my expression as a whole and not understand the subparts. Is there a way to enforce the replacement rule I described above?

  • $\begingroup$ It would be better to use FullForm to check the form to match first. $\endgroup$ Commented Oct 2, 2018 at 10:00
  • $\begingroup$ @ΑλέξανδροςΖεγγ Thanks but then how to render e.g Power[n2,-1] as n2^-1? I need to do this otherwise my software won't know what it is. $\endgroup$
    – CAF
    Commented Oct 2, 2018 at 11:15
  • $\begingroup$ I assume your "other" software is expecting a string as an input, e.g., "A^-1 * B^-1 * (C+D)^-n * E^-m", is that correct? $\endgroup$
    – Carl Woll
    Commented Oct 2, 2018 at 19:10
  • $\begingroup$ @Carl Woll yes, that’s correct, the terms have to be in multiplication as you wrote. I should have mentioned that. The other software by the way is Form. $\endgroup$
    – CAF
    Commented Oct 2, 2018 at 19:25

3 Answers 3


You are interested in creating a string version of your input, where negative powers are not converted to fractions. As @Alexei implies in his answer, the fraction form only occurs for negative numbers, so you can prevent it by modifying the exponent so that it is not a negative number. His solution was to replace it with a symbol, but another idea is to add a wrapper to the exponent. Compare:




Now, the usual function to create a string version is ToString with a second argument of InputForm (never use a single-argument ToString!). The problem with adding a wrapper is that InputForm usually displays the wrapper. However, there is a special symbol, SequenceForm, that is invisible in InputForm, e.g.:

ToString[SequenceForm[-1], InputForm]


Putting the above pieces together, you can create a function to construct your desired string:

myForm[expr_] := ToString[
    expr /. Power[z_, n_] :> Power[z, SequenceForm[n]],

For your example:

myForm[1/(a b (c+d)^n e^m)]

"a^-1*b^-1*(c + d)^-n*e^-m"

Finally, if you think you might have nested powers, the above function will need some modification so that all Power objects acquire a SequenceForm wrapper in the second argument.


There are various different OutputForms which aim to achieve something similair. InputForm seems to be similair to what you want to achieve.

You should allways include a minimal and complete example. I can't verify my code but this seems ok:

a = b/c
a // InputForm

Wolfram tutorial: link


Let us be sure that you are aware that the capital symbols C, D and E are protected in Mma, and should not be used for algebra.

As for the terms like (c+d)^-n, Mma will anyway output it in the form you wish:

expr1 = 1/((c + d)^n*e^m)

(* (c + d)^-n e^-m  *)

However, the expressions like

expr2 = 1/(a*b)
(*  1/(a b)  *)

are returned in a different format for reasons internal for Mma. There are a few ways around. The concrete one depends upon your ultimate aim. Since you need to import it into a software that you tell nothing about, I propose a very simple way. Let us reserve a symbol, say, "s" for a unity, and replace all expressions like 1/aby a^-s:

expr1*expr2 /. Power[A_, -1] -> A^-s

(* a^-s b^-s (c + d)^-n e^-m *)

Then you import it into your software, and only after that put s=1. Done.

Have fun!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.