Python Forum
Vector field cross product - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Vector field cross product (/thread-12325.html)



Vector field cross product - Eduard - Aug-20-2018

This is my first time in this forum sorry if I posted in the wrong section. I just started using python 2 days ago and Im stuck on doing the cross product of a vector field I created, the code works fine when I remove the last line that does the cross product at the end. I keep getting the error:

Output:
TypeError: only length-1 arrays can be converted to Python scalars
import scipy
from scipy import constants
import math
import numpy as np
from numpy.polynomial import Laguerre
from scipy import special
from scipy import misc
import sympy
from sympy import *
import matplotlib.pyplot as plt
from matplotlib.colors import LightSource
from matplotlib.cbook import get_sample_data


# set up co-ordinate grid
x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)
z = np.linspace(-10, 10, 100)
X, Y, Z = np.meshgrid(x, y, z)

# wave number 
lamda = 500*10**(-9)
PI = scipy.pi
k = 2*PI/lamda   # add pi alter

# laguerre
l = 0
p = 6
C = np.sqrt(2*math.factorial(p)/(PI*math.factorial(p + l)))  # Laguerre constan


def f(x, y, z):
    r = np.sqrt(x**2 + y**2)
    ZR = 1  # find value
    w = np.sqrt((2/k)*(ZR**2 + z**2)/(ZR))
    L = scipy.special.assoc_laguerre(2*(r**2)/w**2, n=p, k=l)
    return C*(1/w)*((r**(abs(l)))*np.sqrt(2)/w)*np.exp((-r**2)/w**2)*L
    
# calculus stuff
dx = 1*10**(-9)
dy = 1*10**(-9)
dz = 1*10**(-9)
u = f(X, Y, Z)
udy =  (f(X,Y+dy,Z)-f(X,Y,Z)) / dy # y partial derivative
udx =  (f(X+dx,Y,Z)-f(X,Y,Z)) / dx # x partial derivative


# fields
# E,B
Ex = 1j*k*u*np.exp(1j*k*Z)
Ey = 0
Ez = -udx*np.exp(1j*k*Z)
E = [Ex,Ey,Ez]

Bx = 0
By = 1j*k*u*np.exp(1j*k*Z)
Bz = -udy*np.exp(1j*k*Z)
B =[Bx,By,Bz]


#real poynting

np.cross(E,B)
Thank you in advance for the help, I can not figure out what to do.


RE: Vector field cross product - Gribouillis - Aug-20-2018

I think the problem comes from the fact that By and Bz have shapes (100, 100, 100) and Bx is a number. Same with E.

It works for me with
Ex = ...
Ez = ...
Ey = np.zeros_like(Ex)
...
By = ...
Bx = np.zeros_like(By)
...
np.cross(E, B, axis=0)
The result has shape (3, 100, 100, 100). I don't know if this is what you want.


RE: Vector field cross product - Eduard - Aug-20-2018

Thank you this solved my problem. I dont know why I thought I could just put zero instead of an array of zeros