【问题标题】:Half of Matrix Size Missing矩阵大小的一半缺失
【发布时间】:2020-02-16 03:38:32
【问题描述】:

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

Example of what happens

代码:

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)

【问题讨论】:

  • for i in range(0,ParticleCount): 中,您似乎在每个循环中都覆盖了 sphArray 和 randArray。首先创建空数组并为每个循环追加。

标签: python


【解决方案1】:

稍微清理了你的代码...

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。前者是二维数组,后者是一维数组。

【讨论】:

  • 感谢您的回答和提示,但我想解决要点。对于第一点;我是 Python 和一般编码世界的新手。我两个月前才开始使用 Python,我完全是自学成才的。我不使用这些论坛“希望有人会为我清理”我的代码。我使用的语法是基于我有限的编码知识,因此我可能使用的方法并不像更有经验的人所知道的那样理想。对于第二个要点,我将对此进行研究。
  • 第三个要点;我尝试了与您最初使用的方法相同的方法(不添加任何内容),由于某种原因,它向我抛出了一个错误,这就是我选择使用 if-else 语句的原因。第四点;缺少尺寸信息。数组大小是 (3,1) 而不是 (3,nothing)。它是一个已定义的向量,所以 Spyder 说它的一半没有大小是没有意义的。
  • 我最终想出了如何让它看到 (3,1),那就是在数组定义中添加一对额外的括号。仍然不明白它为什么需要它。
猜你喜欢
  • 2013-12-25
  • 1970-01-01
  • 2013-12-20
  • 2014-10-16
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
  • 2012-12-11
  • 1970-01-01
相关资源
最近更新 更多