Help with Numpy code

i have a small code snippet in a file and it works fine

but when i try it in another file it is not working and wondering why ?

here is code


import bpy 
import numpy as np 
import math
from math import *

import cmath
from cmath import rect
print ()
print ()

###
	
#	 Convert  to Rect cpx 
	
def P2R( radii, angles ):
	return radii * exp(1j*angles)
	
###
	


	
pi =  3.14159
	
r5  = 15
ang5 = 30											# deg
phi5 = ( pi/180) *( ang5 )							# angle in Rad
print (' polar  Abs = ' , r5 , ' Ang = ' ,ang5 , ' Deg  = ' ,  phi5 , ' Rad '  )
	
sd1 = P2R( r5 , phi5 )
print (' CPX Rec = ' , sd1 ) 
print ()
	

	
cp1 = P2R( 1.0 , radians(45.0) )					# Angle in radians
print ( 'cp1 = ' , cp1 )
	
	

it refuse to use NP codes in the Function for P2R

any help appreciated

thanks
happy bl

I’m not at all sure what your code is trying to do and I’m very unfamiliar with using Python in Blender, but you mentioned that numpy is not being used and I can’t see anywhere in your code where you are trying to use numpy. Normally a numpy function will be written as np.function() since you’ve imported it as np, and none of your code has that.

The exp(1j*angles) in your function seems to be the issue as the error message I got when running it is that the value should be real and not complex, so the 1j (usually interpreted as a complex number, same as i - square root of -1) is probably the issue.

Just checked the numpy docs and the function np.exp apparently can take complex arguments, so maybe you’ve left out the np there.

as i said i got this same code in another file and it works fine

the P2R is calling this J expon which should be using Cmath or Numpy

but not certain why one works and the other not!

so wondering why it works in one script and not the other one

thanks

here the script that works
can you see any differences ?

import bpy 
import numpy as np 
import math
from math import *


########## python  #########
	
def P2R(radii, angles):
	return radii * exp(1j*angles)
	
##########
	
	
def P2R( A, phi ):
	return A * ( np.cos(phi) + np.sin(phi)*1j )
	
########
	
import cmath

########
	
print (' CP1' ) 
print ()

cp1 = P2R( 1.0 , radians(45.0) )		# Angle in radians
print ( 'cp1 = ' , cp1 )
	
print ()
###
	
print ()
	
import numpy as np
from cmath import rect
	
print ()
	
pi =  3.14159
	
r5  = 15
ang5 = 30							# deg
phi5 = ( pi/180) *( ang5 )			# angle in Rad
print (' polar  Abs = ' , r5 , ' Ang = ' ,ang5 , ' Deg  = ' ,  phi5 , ' Rad '  )
	
sd1 = P2R( r5 , phi5 )
print (' CPX Rec = ' , sd1 ) 
print ()
nprect = np.vectorize(rect( r5, phi5 ) )		#		cmath.rect( r, phi )
	
print ( 'nprect  = ' ,nprect  )
print ()
	
	
r = 10
phi = 30						# Deg
phirad = (pi /180)*( phi )
	
	
cpx4 = cmath.rect( r, phirad )
	
print ('cpx4 = ',  cpx4 )
nprect = np.vectorize( cpx4 )
print ('nprect = ',  nprect )
	
	
	

thanks
happy bl

In the script that works there are two definitions of the function P2R, so the second one would replace the first. In your first file you only have the first, which contains the problematic complex number 1j. Also, the second one is using numpy, so that’s the one to go with.

This is the one to use

i did not see the 2 def function for P2R

i must be becoming blind LOL

thanks