Half of Matrix Size Missing

2024/10/11 12:21:48

I wanted to ask why is it that some of my matrices in Python are missing a size value? The matrices/arrays that re defined show the numbers in them, but the size is missing and it is throwing me other errors because of it. Why is the variable explorer missing this info or why is it that Python can't identify the size?

Example of what happens

Code:

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 randParticleCount = 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<S2rrand = (S2 - S1)*rand.uniform(0,1) + S1 #Random # generation for component x between s1 and s2thetarand = (2*np.pi)*rand.uniform(0,1) #Random # generation for component y between s1 and s2phirand = 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)
Answer

Cleaned up your code a little...

import numpy as np
import random as randParticleCount = 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<S2RStart = (S2 - S1)*rand.uniform(0,1) + S1 #Random # generation for component x between s1 and s2thetarand = (2*np.pi)*rand.uniform(0,1) #Random # generation for component y between s1 and s2phirand = 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]

Some hints:

  • Stackoverflow is not meant to post messy code hoping that somebody will clean it up for you.
  • If you define phi from -90 to 90 you need to adapt the conversion from spherical to Cartesian coordinates accordingly.
  • No need to define and append arrays over and over. You know its size beforehand. Better create it and fill it while looping.
  • There is no information missing. You have defined SphPositions and randArray/SphArray differently. The former is a 2D array, the latter are 1D.
https://en.xdnf.cn/q/118330.html

Related Q&A

Clicking Side Panel Elements in Selenium Without IFrames

I want to download U.S. Department of Housing and Urban Development data using Pythons Selenium. Heres my code. import os from selenium import webdriver from webdriver_manager.chrome import ChromeDrive…

Library to extract data from open Excel workbooks

I am trying to extract data from workbooks that are already open. I have found the xlrd library, but it appears you can only use this with workbooks you open through Python. The workbooks I will use in…

Keras apply different Dense layer to each timestep

I have training data in the shape of (-1, 10) and I want to apply a different Dense layer to each timestep. Currently, I tried to achieve this by reshaping input to (-1, 20, 1) and then using a TimeDis…

Create a pass-through for an installed module to achieve an optional import

Im writing a library in python 3.7. Id like it to have as few dependencies as possible. For example, tqdm is nice to have but ideally Id like my library to work without it if its not there. Therefore, …

Django, Redis: Where to put connection-code

I have to query redis on every request in my Django-app. Where can I put the setup/ connection routine (r = redis.Redis(host=localhost, port=6379)) so that I can access and reuse the connection without…

Events and Bindings in tkinter does not work in loop

I am trying to create binding in a loop using tkinter module.from tkinter import * class Gui(Frame):def __init__(self, parent):Frame.__init__(self, parent) self.parent = parentself.initUI()def Arrays(…

Python Machine Learning Algorithm to Recognize Known Events

I have two sets of data. These data are logged voltages of two points A and B in a circuit. Voltage A is the main component of the circuit, and B is a sub-circuit. Every positive voltage in B is (1) co…

How can I replace Unicode characters in Python?

Im pulling Twitter data via their API and one of the tweets has a special character (the right apostrophe) and I keep getting an error saying that Python cant map or character map the character. Ive lo…

Filtering Pandas DataFrame using a condition on column values that are numpy arrays

I have a Pandas DataFrame called dt, which has two columns called A and B. The values of column B are numpy arrays; Something like this: index A B 0 a [1,2,3] 1 b [2,3,4] 2 c …

Creation a tridiagonal block matrix in python [duplicate]

This question already has answers here:Block tridiagonal matrix python(9 answers)Closed 6 years ago.How can I create this matrix using python ? Ive already created S , T , X ,W ,Y and Z as well as the…