To be clear, only <number>j where there is nothing between the number
and "j" produces a complex number in Python, like 1j. j by itself is a
normal variable name which isn't defined to anything by default (and
<number>x for any other letter x is a syntax error without a
mathematical operation between the number and x). So as long as you
remember that Python requires explicit multiplication, it's not a big
deal to use j as a variable name unrelated to complex numbers. And
indeed, it's quite common to use both i and j in Python programs for
index variables in for loops.

When using SymPy, it's better to use I for complex numbers because
this can be used with exact numbers, like 2*I/3, which is the exact
fraction 2/3 times sqrt(-1). Whereas Python complex literals are
always floating point, so 2j/3 will produce the inexact quantity
0.6666666666666666j. And even something like 1000000000000000001j
would be truncated to double precision.

>>> 1000000000000000001j
1e+18j
>>> 1000000000000000001*I
1000000000000000001*I

Aaron Meurer

On Mon, Jan 4, 2021 at 2:30 PM Thomas Ligon <[email protected]> wrote:
>
> Hello David,
> indeed, when I enter print(sqrt(-1)), I get I, just as you do.
> However, when I enter print(4s**2), it is flagged with an error "unexpected 
> token 's'", so I immediately see that I have something wrong. But, when I  
> enter print(4j**2), I get (-16 + 0j), so python is just making a complex 
> number out of it, and so I overlooked the error.
> This means that both j and I mean sqrt(-1).
> Just to add to the confusion, the mathematics paper I have been working on 
> was published in 1878 and the author used i and j as summation indices. But 
> then, someone else published an additional analysis in 1896 and avoided the 
> use of i as an index, using j and s instead, and saving i for sqrt(-1), and I 
> have been following that author's conventions. Now I think it would probably 
> be smart top avoid both i and j, or better, avoid i, I and j, because i is 
> used by mathematicians, I is used by sympy, and j is used by python and 
> electrical engineers.
>
>
> On Monday, January 4, 2021 at 5:22:07 PM UTC+1 [email protected] wrote:
>>
>> On 04/01/2021 10:37, Oscar Benjamin wrote:
>>
>> In Python 4j is the literal syntax to create an imaginary number (0 +
>> 4*I). You need to use 4*j.
>>
>> I don't understand this because Thomas Lignon imported sqrt from sympy, so 
>> why didn't he get the imaginary answer he was expecting?
>>
>> Indeed, trying this with Python 3.7 and SymPy 1.7, I get:
>>
>>  import sympy
>>  from sympy import sqrt
>>  sqrt(-1)
>> I
>>
>> David
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sympy/292aef1f-be93-41dc-b011-90beff19b300n%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAKgW%3D6JQQNv5OtuBWzd70h%3DQWo0S%3DQk65tgA1z3XxUt06KHx5g%40mail.gmail.com.

Reply via email to