Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Say I have two arbitrary real constants $p,q$ that satisfy $p^2+q^2=1$. Is there any way to easily declare this relation in Mathematica?

Edit

More specifically I have this (among others) expression:

$$\frac{\sqrt{\frac{p^4 q^4(p^2-q^2)^2}{p^4 + q^4}}}{\sqrt{p^{10}+p^2q^8+p^8(-2+q^2)+2p^4q^4(-2+q^2)+q^6(-1+q^2)^2+p^6(1+2q^4)}}$$

Divide[Sqrt[Divide[p^4 q^4 (p^2 - q^2)^2, p^4 + q^4]], 
 Sqrt[p^{10} + p^2 q^8 + p^8 (-2 + q^2) + 2 p^4 q^4 (-2 + q^2) + 
   q^6 (-1 + q^2)^2 + p^6 (1 + 2 q^4)]]

and am asking whether there is a way to have Mathematica reduce it using the relation $\;q^2+p^2=1$.

share|improve this question
    
I am using Mathematica to do some tedious derivatives for me where the functions involved depend on arbitrary constants $a,b$ subject to to the relation $a^2+b^2=1$ which I feel will simplify my results. I was wondering if there is any way to declare this relation in Mathematica so that it may reduce the output, if possible. –  D. Clark Jan 31 at 23:07
    
Related or a duplicate: How Can I use Solve/Reduce Output –  Artes Jan 31 at 23:11
    
This is unclear what you mean by declaring. Simplifying, reducing, solving, substituting, etc. –  Artes Jan 31 at 23:23
2  
Not really, this isn't how people normally work in Mathematica, and there's no way to declare such a relation. This is why people think that your question is confusing. (To be fair, $Assumptions is similar to what you describe, but it probably won't help you much.) To make the question clear: give examples of calculations that you want to do with a and b and explain how you need to use the relationship between a and b in those calculations. The answer to you question will depend on precisely what sorts of calculations you need to do and how this relationship will be used. –  Szabolcs Jan 31 at 23:41
2  
please include the mathematica code for your expression. It might be possible and I (and others) might be willing to take a stab at answering it, but I am certainly not willing to enter that expression by hand into mathematica syntax. –  rm -rf Feb 1 at 3:15

2 Answers 2

up vote 5 down vote accepted

There are many possible ways to tackle this task.

expr = Sqrt[ ((p^4 q^4 (p^2 - q^2))/(p^4 + q^4))]/Sqrt[ p^10 + p^2 q^8 
       + p^8 (-2 + q^2) + 2 p^4 q^4 (-2 + q^2) + q^6 (-1 + q^2)^2 + p^6 (1 + 2 q^4)];

Standard approach uses simplification techniques like Simplify or FullSimplify, the latter appears to be unnecessary here. Let's use the former function:

Simplify[ expr, p^2 + q^2 == 1]
 Sqrt[(p^4 q^4 (1 - 2 q^2))/(p^4 + q^4)]/Sqrt[q^2 - 5 q^4 + 8 q^6 - 4 q^8]

So we've got a slightly simplified expression, however one might be still dissatisfied with this result. Althoug one could play with ComplexityFunction but we provide another direct way using Eliminate and exploiting a formal variable a to eliminate q:

Eliminate[{expr == a, p^2 + q^2 == 1}, q]
a^2 (-1 + 4 p^2 - 6 p^4 + 4 p^6) == p^2 (1 - p^2)

So we have an expression a^2 (i.e. expr^2) written in terms of p. Let's now eliminate p:

Eliminate[{expr == a, p^2 + q^2 == 1}, p]
a^2 (-1 + 4 q^2 - 6 q^4 + 4 q^6) == q^2 (-1 + q^2)

We can also use Simplify with a replacement rule exploiting a simple pattern, i.e. substituting all even powers of p with an expression involving only q:

Simplify[ expr /. p^b_?EvenQ /; b > 0 -> (1 - q^2)^(b/2)]
Sqrt[-((q^4 (-1 + q^2)^2 (-1 + 2 q^2))/(1 - 2 q^2 + 2 q^4))]/Sqrt[q^2 - 5 q^4 
                                                                   + 8 q^6 - 4 q^8]     
share|improve this answer
1  
My problem was in using only one equals sign, believe it or not. Thanks for your help, you saved me a bunch of time. –  D. Clark Feb 1 at 0:36
3  
@D.Clark That's why you should always post your code here! –  belisarius Feb 1 at 5:05

This is a slightly corrected version of code I and others have used in MathGroup, StackOverflow, and MSE. It is at heart a recursive PolynomialReduce.

replacementFunction[expr_, rep_, vars_] := 
 Module[{num = Numerator[expr], den = Denominator[expr], 
   hed = Head[expr], base, expon}, 
  If[PolynomialQ[num, vars] && 
    PolynomialQ[den, vars] && ! NumberQ[den], 
   replacementFunction[num, rep, vars]/
    replacementFunction[den, rep, vars], 
   If[hed === Power && Length[expr] == 2, 
    base = replacementFunction[expr[[1]], rep, vars];
    expon = replacementFunction[expr[[2]], rep, vars];
    PolynomialReduce[base^expon, rep, vars][[2]], 
    If[Head[hed] === Symbol && 
      MemberQ[Attributes[Evaluate[hed]], NumericFunction],
     Map[replacementFunction[#, rep, vars] &, expr], 
     PolynomialReduce[expr, rep, vars][[2]]]]]]

On your example it will eliminate a variable. Whether this is useful depends on what you really require.

expr = Sqrt[((p^4 q^4 (p^2 - q^2))/(p^4 + q^4))]/
   Sqrt[p^10 + p^2 q^8 + p^8 (-2 + q^2) + 2 p^4 q^4 (-2 + q^2) + 
     q^6 (-1 + q^2)^2 + p^6 (1 + 2 q^4)];

replacementFunction[expr, p^2 + q^2 - 1, {p, q}]

(* Out[299]= Sqrt[-((q^4 (-1 + q^2)^2 (-1 + 2 q^2))/(
 1 - 2 q^2 + 2 q^4))]/Sqrt[-q^2 (-1 + q^2) (-1 + 2 q^2)^2] *)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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