0
$\begingroup$

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?

$\endgroup$
5
  • 1
    $\begingroup$ 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. $\endgroup$
    – Spawn1701D
    Apr 15, 2013 at 2:40
  • $\begingroup$ 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 $\endgroup$ Apr 15, 2013 at 2:43
  • $\begingroup$ 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 :=. $\endgroup$
    – Spawn1701D
    Apr 15, 2013 at 2:48
  • $\begingroup$ thanks a lot, Spawn1701D $\endgroup$ Apr 15, 2013 at 3:01
  • $\begingroup$ @Spawn, maybe write an answer to settle this? :) $\endgroup$ Apr 15, 2013 at 3:50

2 Answers 2

4
$\begingroup$

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.

$\endgroup$
4
  • 1
    $\begingroup$ 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. $\endgroup$
    – Jens
    Apr 15, 2013 at 4:07
  • $\begingroup$ You just have to be careful around Evaluate. Especially with Plot. $\endgroup$
    – Spawn1701D
    Apr 15, 2013 at 4:10
  • $\begingroup$ I am not familiar with SetAttributes, and I will look into it now. Thank you @amr, Jens, and Spawn1701D $\endgroup$ Apr 15, 2013 at 4:15
  • $\begingroup$ 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. $\endgroup$
    – amr
    Apr 15, 2013 at 4:27
2
$\begingroup$

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.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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