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.

I am trying to perform a simple arithmetic in complex algebra.

       a := Subscript[a, 1] + I*Subscript[a, 2];
       b := Subscript[b, 1] + I*Subscript[b, 2];
       r[z_] := a/(b - z) + a\[Conjugate]/(b\[Conjugate] - z);

when I try to evaluate r[0], mathematica gives me the following message

       $RecursionLimit::reclim: Recursion depth of 256 exceeded.

I was expecting something like the following

       2*Re[a/b] = 2 (a_1 * b_1 + a_2 * b_2)/(b_1 ^2 + b_2 ^2)

Could someone please point out where I did mess up?

share|improve this question
1  
Subscript is a function too so Mathematica tries to evaluate the value of a ad infinitum inside it. Just change them to a1 and a2 for example. The same applies to b. –  Spawn1701D Apr 15 '13 at 2:40
    
unbelievable, it worked. and i have been trying to understand what is going on for an hour. How come subscript is a function, that makes no sense at all. I cannot define a variable a_1 ? in any case, thank you so much @ Spawn1701D –  Korben Dallas Apr 15 '13 at 2:43
    
At Mathematica every object is a function, that gives incredible power but it comes with a cost ... If you really need to have the subscripts, use ToString[Subscript[a, 1], TraditionalForm] when defining a and b. But make sure that you have cleared any previous values of them. And use = instead of :=. –  Spawn1701D Apr 15 '13 at 2:48
    
thanks a lot, Spawn1701D –  Korben Dallas Apr 15 '13 at 3:01
    
@Spawn, maybe write an answer to settle this? :) –  J. M. Apr 15 '13 at 3:50

2 Answers 2

up vote 4 down vote accepted

I did this and it seems to work:

SetAttributes[Subscript, HoldAll];

a := Subscript[a, 1] + I*Subscript[a, 2];
b := Subscript[b, 1] + I*Subscript[b, 2];
r[z_] := a/(b - z) + a\[Conjugate]/(b\[Conjugate] - z);

r[0] // ComplexExpand // Simplify

I'm not really sure if this will work in a more general setting though.

share|improve this answer
1  
Yes, that's a way around the problem (+1). And if you have other cases where Subscript needs its arguments evaluated, you just have to wrap those in Evaluate. –  Jens Apr 15 '13 at 4:07
    
You just have to be careful around Evaluate. Especially with Plot. –  Spawn1701D Apr 15 '13 at 4:10
    
I am not familiar with SetAttributes, and I will look into it now. Thank you @amr, Jens, and Spawn1701D –  Korben Dallas Apr 15 '13 at 4:15
    
Yea the attributes can be surprisingly powerful. I recently found this out when I realized I could set UndirectedEdge to Orderless and thus have Gather[{a <-> b, b <-> a}] work as expected. –  amr Apr 15 '13 at 4:27

Attributes are powerful, but so is With, which is what I would use here.

With[{a = Subscript[a, 1] + I*Subscript[a, 2],
      b = Subscript[b, 1] + I*Subscript[b, 2]}, 
  r[z_] := a/(b - z) + a\[Conjugate]/(b\[Conjugate] - z)]


r[0] // ComplexExpand // Simplify
(2*(Subscript[a, 1]*Subscript[b, 1] + Subscript[a, 2]*Subscript[b, 2])) /  
    (Subscript[b, 1]^2 + Subscript[b, 2]^2)

This works because the a on the lhs of assignment is now distinct from the a on the rhs. Same goes for b.

share|improve this answer
    
thanks @m_goldberg –  Korben Dallas Apr 17 '13 at 2:15

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.