How to Use If Statements Inside a Do Loop with Complex Numbers?

  • Context: Mathematica 
  • Thread starter Thread starter Poirot
  • Start date Start date
  • Tags Tags
    If statement Loop
Click For Summary

Discussion Overview

The discussion centers around the use of If statements within a Do loop in Mathematica, specifically in the context of handling complex numbers. Participants explore issues related to syntax, debugging strategies, and the behavior of expressions involving complex roots.

Discussion Character

  • Technical explanation
  • Debugging strategies
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant describes their intention to use a Do loop to compute values that may become complex, seeking to apply negative roots for complex results and positive roots otherwise.
  • Another participant points out the need to use '==' for equality testing in Mathematica, suggesting a potential syntax error.
  • A different participant clarifies that they are not testing for equality but checking if an expression becomes complex, indicating a misunderstanding of the previous comment.
  • One participant suggests debugging techniques, such as printing values passed to the If statement to verify the logic flow and identify issues within the code.
  • Another participant questions whether the expression correctly returns the intended value or if it might return a boolean instead, highlighting potential confusion in the expression's evaluation.
  • One participant argues that the Element function will always return True for any number, proposing a change to check for real numbers instead and suggesting a reversal of the conditions in the If statement.

Areas of Agreement / Disagreement

Participants express differing views on the correct syntax and logic for handling complex numbers in the code. There is no consensus on the best approach to resolve the issues raised, and multiple competing views remain regarding the correct implementation.

Contextual Notes

Some participants note potential limitations in understanding how expressions are evaluated in Mathematica, as well as the implications of varying loop ranges on the values being computed.

Poirot
Messages
87
Reaction score
2
I have a Do loop running which, for some values will become complex, and I want to make it so for these situations, we take the negative root and for others the positive root. In this scenario, I've simplified what I actually have and chose to look at the case where I need it to take the negative root

Code:
Do[e[o] = 0.5 + o, {o, 0, 10}]
Do[If[Element[x[n, o] = (e[o] - n)^(1/2), Complexes], -[x[n, o]], 
  x[n, o]], {n, -10, 10}, {o, -10, 10}]

I can't seem to get the syntax right here, and if I print a table with the results it's as if the If is just being ignored.
Ideally, this would be the best approach for my problem as my actual code is quite complex and values in the Do loops are called upon, so I fear any other method will cause grief elsewhere.

Thanks for any help!
 
Physics news on Phys.org
When you are testing for equality, you need to use ==, not just =. For example If[a==b, step1, step2]
 
phyzguy said:
When you are testing for equality, you need to use ==, not just =. For example If[a==b, step1, step2]
I'm not testing for equality here, I have an expression that's defined for different values of n which I need to check if (for some values of n) x[n, o] becomes complex, and when this happens I need it to take the negative value of this.
 
Poirot said:
I have an expression that's defined for different values of n which I need to check

I don't know Mathematica, but I assume debugging strategy should be similar to other languages.

If it's not throwing an error - e.g. if your syntax is correct - then what else might be going on? One thing I might do is put in a statement to print the value being passed to "If"; this way I could see whether the appropriate value appears that ought to trigger the "If". If in fact the appropriate value does appear, that would indicate something else is wrong, maybe inside the "If" statement. In some cases I might pass a static value, or isolate the part of the routine that is problematic by some means, etc. Debugging to me, provided you have the syntax right, is fairly simple-minded; you can use print and other tricks to find out what the code is actually doing, versus what you think it should be doing.
 
Last edited:
I'm not fluent in mathematica, but are you sure that x[n, o] = (e[o] - n)^(1/2) returns x[n, o] ? Or does it return a boolean or something like that?

Also, in your first loop, o varies from 0 to 10, then in the 2nd loop it varies from -10 to 10. Where are the missing values of e[o] (from -10 to 0)?
 
OK, I think I see what you are trying to do. Element[x, Complexes] will always return True for any number x, since all numbers, even real numbers, are elements of the complex domain. Try replacing it with Element[x, Reals], and reverse the (then, else) conditions.
 
  • Like
Likes   Reactions: jack action and UsableThought

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
10K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 65 ·
3
Replies
65
Views
9K