# What is the equivalent of element-wise division in python when denominator contains complex number

6 visualizzazioni (ultimi 30 giorni)
Arslan Saif il 7 Gen 2023
Risposto: Al Danial il 15 Gen 2023
The line:
V_modif(j-1,:)=Y_modif(j-1,:)./(i*2*pi*f);
contains i (iota ) in denominator.
It's python equivalent line would be:
V_modif[j - 1] = Y_modif[j - 1,:] / ( 1j*2 * np.pi * f)
but this gives zero in ouput and if you remove "1j" from denominator it gives corrcet output but contains only real part no imaginary part.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (3)

John D'Errico il 7 Gen 2023
Modificato: John D'Errico il 7 Gen 2023
That is the correct expression. Why it gives zero is hard to tell. You may have defined the variable i in your code already as a number. That would be a bad thing, if you then want to use i as sqrt(-1). However, 1i (OR 1j) will always work as sqrt(-1). Note my use of 1i there, just as you used 1j in python. Again, 1j also always works.
V_modif(j-1,:)=Y_modif(j-1,:)./(1i*2*pi*f);
Another possibility is it is not truly zero, but just a small number. If it shows up as 0.0000 in the command window, that means your format is simply a poor choice, and not truly zero.
help format
So you might try this command to show the true number.
format long g
##### 5 CommentiMostra 3 commenti meno recentiNascondi 3 commenti meno recenti
Arslan Saif il 7 Gen 2023
Yes you are right, this whole time I was discussing that I I have problem with python, matlab is working absolutely fine. I wanted to implement the same functionality in python.
John D'Errico il 8 Gen 2023
For that, you need to ask a question on a Python forum. I am quite sure it is doable, but you need to find a place where people are far more conversant in python.

Accedi per commentare.

Al Danial il 13 Gen 2023
Your Python code has a typographical bug, an easy one to make: if you work with complex numbers in Python, you cannot use j as a variable. Your Python code uses j as a subscript to V_modif and Y_modif and as the marker for the imaginary component, 1j, suggesting, as @John D'Errico aluded, that you defined j yourself to an integer value. That destroys j's property of defining complex literals. You'll need to use something else, maybe k ?, as the subscript to V_modif and Y_modif.
The problem doesn't appear in the matlab code because it uses i for the imaginary component, so j is OK to use as a subscript.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Al Danial il 14 Gen 2023
Ignore everything I wrote above, it's wrong! There's no problem using j as both a subscript and as a marker for a complex value; my test session had errors. I'll revisit this when I have a better idea.

Accedi per commentare.

Al Danial il 15 Gen 2023
Here's my next theory: your code uses the default np.float64 datatype for V_modif. The problem with this is the right hand side is complex but the left hand side only has room for the real parts. Consequently the complex terms are dropped. Here's code that demonstrates this:
import numpy as np
V_modif = np.zeros((2,3)) # default type of np.float64
Y_modif = np.ones((2,3))
f = np.array([12., 13., 14.])
j = 1
V_modif[j - 1] = Y_modif[j - 1,:] / ( 1j*2 * np.pi * f)
print('V_modif with default dtype=np.float64')
print(V_modif)
# redo, this time explicitly making V_modif complex
V_modif = np.zeros((2,3), dtype=np.complex128)
Y_modif = np.ones((2,3))
f = np.array([12., 13., 14.])
j = 1
V_modif[j - 1] = Y_modif[j - 1,:] / ( 1j*2 * np.pi * f)
print('V_modif with default dtype=np.complex128')
print(V_modif)
The output first shows the incorrect result because V_modif is real-only, then shows the correct result when V_modif is explicitly defined as a complex variable:
V_modif with default dtype=np.float64
[[0. 0. 0.]
[0. 0. 0.]]
V_modif with default dtype=np.complex128
[[0.-0.01326291j 0.-0.01224269j 0.-0.01136821j]
[0.+0.j 0.+0.j 0.+0.j ]]
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Call Python from MATLAB in Help Center e File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by