8

Let's say I have a relation r^2 = x^2 + y^2. Now suppose after a calculation i get a complicated output of x and y, but which could in theory be simplified a lot by using the above relation. How do I tell Mathematica to do that?

I'm referring to situations where replacement rules x^2+y^2 -> r^2 and using Simplify/FullSimplify with Assumptions won't work, e.g. if the output is x/y + y/x = (x^2+y^2)/(xy) = r^2/(xy).

Simplification works really well with built in functions but not with user defined functions! So essentially I would like my functions to be treated like the built in functions!

H. Arponen
  • 547
  • 1
  • 8
  • 18
  • Welcome to StackOverflow. Please see [this FAQ](http://meta.stackexchange.com/questions/7931/faq-for-stack-exchange-sites) if you have questions about the site. – Mr.Wizard May 07 '11 at 01:43

2 Answers2

9

I believe you are looking for TransformationFunctions.

f = # /. x^2 + y^2 -> r^2 &;

Simplify[x/y + y/x, TransformationFunctions -> {Automatic, f}]

(* Out=  r^2/(x y)  *)
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • 4
    @Super, a word of caution about `TransformationFunctions`, the enable you to right replacement rules that violate mathematical rules. So, consider the transformation carefully before you use it. – rcollyer May 07 '11 at 04:48
  • @rcollyer Or impose trivial assumptions that disable a whole family of solutions (denominator NEQ 0 is the classical example) – Dr. belisarius May 07 '11 at 12:11
2

In the example you give

(x/y + y/x // Together) /. {x^2 + y^2 -> r^2}

==> r^2/(x y)

works. But I've learned that in many occasions replacements like this don't work. A tip I once got was to replace this replacement with one which has a more simpler LHS like: x^2 -> r^2-y^2 (or even x->Sqrt[r^2-y^2] if you know that the values of x and y allow this).

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
  • I think the Mma box should come with a big red surgeon general's warning: "Don't expect this software to write formulas like you do". Much frustration could be avoided. – Dr. belisarius May 07 '11 at 12:15
  • @Sjeord, that works precisely because `Together` makes the numerator `x^2 + y^2` without any other terms present. If they're were other terms present, the likelihood of it working goes down quite a bit. Truthfully, I don't know if the `TransformationFunction` given by Mr. Wizard would work in that case. – rcollyer May 07 '11 at 21:38