首页 > 解决方案 > 矩阵大小的一半缺失

问题描述

我想问为什么我在 Python 中的一些矩阵缺少大小值?重新定义的矩阵/数组显示了其中的数字,但是缺少大小,因此它给我带来了其他错误。为什么变量资源管理器缺少此信息,或者为什么 Python 无法识别大小?

发生情况的示例

代码:

import numpy as np
import pylab
from scipy.integrate import odeint
import matplotlib.pyplot as plt
#import random
import mpl_toolkits.mplot3d.axes3d as p3
import random as rand

ParticleCount = 100 ## of particles to generate energies for energy generation
binsize = 15 #Determines bin size for historgram of electron energies
S1 = 0.0200001 #Specify minimum radius particles may be generated
S2 = 0.0200002 #Specify maximum radius particles may be generated
phi1 = -90
phi2 = 90
phi1rad = phi1*(np.pi/180)
phi2rad = phi2*(np.pi/180)

#Generate random positions for each particle between s1 and s2
#ICPositions = np.array([])
ICPositions = np.empty([3,ParticleCount],dtype = float)
SphPositions = np.empty([3,ParticleCount],dtype = float)
for i in range(0,ParticleCount):
    #In Spherical: Generates random position with boundaries of: S1<r<S2
    rrand = (S2 - S1)*rand.uniform(0,1) + S1 #Random # generation for component x between s1 and s2
    thetarand = (2*np.pi)*rand.uniform(0,1) #Random # generation for component y between s1 and s2
    phirand = np.arcsin((np.sin(phi2rad) - np.sin(phi1rad))*rand.uniform(0,1) + np.sin(phi1rad))
    xrand = RStart*np.sin(phirand)*np.cos(thetarand)
    yrand = RStart*np.sin(phirand)*np.sin(thetarand)
    zrand = RStart*np.cos(phirand)
    sphArray = np.array([RStart,thetarand,phirand])
    randArray = np.array([xrand,yrand,zrand])
    randArray = np.array(randArray,dtype = float)
    if ICPositions.size == 0:
        ICPositions = np.array([randArray])
    else:
        ICPositions = np.append(ICPositions,[randArray],axis = 0)

print(ICPositions)

标签: python

解决方案


稍微清理一下你的代码......

import numpy as np
import random as rand

ParticleCount = 100 # of particles to generate energies for energy generation
S1 = 0.0200001 #Specify minimum radius particles may be generated
S2 = 0.0200002 #Specify maximum radius particles may be generated
phi1 = -20
phi2 = 20
phi1rad = phi1*(np.pi/180)
phi2rad = phi2*(np.pi/180)

#Generate random positions for each particle between s1 and s2
sphArray = np.zeros([3,ParticleCount])
randArray = np.empty([3,ParticleCount])
for i in range(0,ParticleCount):
    #In Spherical: Generates random position with boundaries of: S1<r<S2
    RStart = (S2 - S1)*rand.uniform(0,1) + S1 #Random # generation for component x between s1 and s2
    thetarand = (2*np.pi)*rand.uniform(0,1) #Random # generation for component y between s1 and s2
    phirand = np.arcsin((np.sin(phi2rad) - np.sin(phi1rad))*rand.uniform(0,1) + np.sin(phi1rad))
    xrand = RStart*np.cos(phirand)*np.cos(thetarand)
    yrand = RStart*np.cos(phirand)*np.sin(thetarand)
    zrand = RStart*np.sin(phirand)
    sphArray[:,i] = [RStart,thetarand,phirand]
    randArray[:,i] = [xrand,yrand,zrand]

一些提示:

  • Stackoverflow 并不意味着发布杂乱的代码,希望有人会为您清理它。
  • 如果将 phi 定义为 -90 到 90,则需要相应地调整从球坐标到笛卡尔坐标的转换。
  • 无需一遍又一遍地定义和附加数组。你事先知道它的大小。更好地创建它并在循环时填充它。
  • 没有信息丢失。您以不同方式定义了 SphPositions 和 randArray/SphArray。前者是二维数组,后者是一维数组。

推荐阅读