Python Forum
Vector field cross product
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Vector field cross product
#1
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.
Reply
#2
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.
Reply
#3
Thank you this solved my problem. I dont know why I thought I could just put zero instead of an array of zeros
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create a function vector Cooketaker 4 959 Dec-27-2022, 08:22 PM
Last Post: Cooketaker
  Executing Scipy Tensor Product John_Doe 1 2,355 Dec-06-2019, 10:28 AM
Last Post: paul18fr
  matrixes product paul18fr 0 2,111 Jul-18-2019, 07:32 PM
Last Post: paul18fr
  matrix by vector mcgrim 8 3,794 May-02-2019, 10:39 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020