4

I am trying to do algebra the way we do by hand. For example,

test1=(x - b^2 + 3/(x-a)) + ((x+b)^2/((x-a)*(x+a))+z+(x+x)^3/(x-a))
test2=(x-a)*(x+a)
test3=test2*test1

output is:

 (x-a)*(x+a)*(x - b^2 + 3/(x-a)) + ((x+b)^2/((x-a)*(x+a))+z+(x+x)^3/(x-a))

but it should be:

 (x*(x-a)*(x+a) - b^2*(x-a)*(x+a) + 3*(x+a)) + ((x+b)^2)+z*(x-a)*(x+a)+(x+x)^3*(x+a))

I have tried Distribute, Factor, Cancel, Simplify but all of them expand everything and do not remove the denominator. It also removes the powers in terms. How to tell mathematica to eliminate denominator and without expanding numerators?

Aschoolar
  • 883
  • 5
  • 14
  • Maybe Apart[test3] or Simplify@Apart[test3] ? – user1066 Jun 12 '17 at 15:38
  • 1
    Depending on how general you want it to be, there are many ways: test2 # & /@ test1, Block[{test2}, Distribute[test2*test1]], Distribute[factor*test1] /. factor -> test2, Module[{p}, Distribute[test3 /. #] /. Reverse@# &[test2 -> p]]. – jkuczm Jun 13 '17 at 14:05
  • I have tried all of your codes for a simple function and it works. But for my complicated one it does not. I found a suspect but I am not sure why. I used the Denominator[function1] where the function is in the matrix form. I was able to get denominator. Then I created another function2 to do determinant of function1. It calculated the determinant which is function3 (my goal) . However when I do Denominator[function3] it returns 1. There is no denominator which is wrong. I do not know why it does not see denominator. I guess I am doing something wrong to declare a function. – Aschoolar Jun 14 '17 at 00:35

1 Answers1

4

The problem with Distribute is that the sums in test2 are getting distributed as well as the sum in test3. You could achieve what you want if the sums in test2 were inactive, so the following use of Inactivate/Inactive can be used to solve your problem:

Activate @ Distribute[Inactivate[Evaluate@test2, Plus] test1]

3 (a + x) + 8 x^3 (a + x) - b^2 (-a + x) (a + x) + x (-a + x) (a + x) + (b + x)^2 + (-a + x) (a + x) z

Update

The OP is using M9, and Inactive was introduced in M10. There is no need to use Inactive for this problem, the key is to change the head Plus to something else before using Distribute. So, in M9 the following should work:

Distribute[ReplaceAll[test2, Plus->plus] test1] /. plus->Plus

3 (a + x) + 8 x^3 (a + x) - b^2 (-a + x) (a + x) + x (-a + x) (a + x) + (b + x)^2 + (-a + x) (a + x) z

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
  • @Woll, it does not work (Math v9) – Aschoolar Jun 13 '17 at 00:41
  • @Woll, okay it works now. Thank you for help. – Aschoolar Jun 13 '17 at 14:00
  • @Woll, I marked as solved because you solved my example problem. For my actual much longer and complex problem it does not work but I expected it anyway. I can proceed manually anyway. Thanks. – Aschoolar Jun 13 '17 at 14:20
  • I solved the problem by simplifying(Factor, Distribute etc) matrix instead using its determinant result to simplify. Looks like determinant result was too complicated for mathematica to deal with. – Aschoolar Jun 15 '17 at 14:14