I am trying to do some substitution on the basis of pattern matching. I am trying to replace $\frac{1}{a+i b}\to\frac{a-ib}{a^2+b^2}$ in the following way,

ruleExp = {Power[Plus[Complex[0,-1],a_],-1]->(a+I)/(a^2+1),
        Power[Plus[a_,Times[Complex[0,1],b_]],-1]->(a-I b)/(a^2+ b^2),
        Power[Plus[a_,Times[Complex[0,-1],b_]],-1]->(a+I b)/(a^2+ b^2)};

1/(a - I b) /. ruleExp (*Out:= (a + I b)/(a^2 + b^2)*)

1/(a + I b) /. ruleExp (*Out:= (a + I b)/(a^2 + b^2)*)

1/(a - I  ) /. ruleExp (*Out:= (a + I)/(a^2 + 1) *)    

1/(a + I  ) /. ruleExp (*Out:= (a - I)/(a^2 + 1) *)

As you can see I have to write 4 rules to match patterns with symbols and different signs, as I understand it this is robust but is there some smarter way to match all such expressions for numeric as well as symbolic expressions for all signs with less than these four rules?


1 Answer 1

lst = {1/(a - I b), 1/(a + I b), 1/(a - I), 1/(a + I)}


FullSimplify[ComplexExpand @ lst, ExcludedForms -> {_Complex}]

enter image description here


rule = pat : Power[u_ + _Complex  v_., _] :> 
   FullSimplify[ComplexExpand[pat], ExcludedForms -> {_Complex}];

lst /. rule

enter image description here


rule2 = Power[u_ + v_. Complex[x_, y_], p_.] :> 
   Power[u + v Complex[x, -y], -p]/(u^2 + v^2);

lst /. rule2

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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