Python Errors

  • #1
BiGyElLoWhAt
Gold Member
1,570
116
I'm messing around with the swarzchild metric, and I keep getting errors. First, it was a memory, which I could have guessed, 10000x10000 array, so I lowered it to 1000x1000 and it moves past that point, now.
However, this is where I'm getting my error:
Python:
Gravity = zeros([1000,1000])
while i < 1000:
    while j < 1000:
        Gravity[i,j] = metric(sqrt((RS1*sin(ThetaS1)-i)**2+(RS1*cos(ThetaS1-j)**2)),Rs1,step,ThetaS1,omega1*step,speed1)
        Gravity[i,j] += metric(sqrt((RS2*sin(ThetaS2)-i)**2+(RS2*cos(ThetaS2-j)**2)),Rs2,step,ThetaS2,omega2*step,speed2)

print("Done filling\n")
On the first line of the fill, the one with all the 1's, it's giving me this error:
"ValueError: setting an array element with a sequence."
From googling around, it seems that
"the shape of the input list isn't a (generalised) "box" that can be turned into a multidimensional array." -stack exchange.
I was wondering what I am missing that's making this error out?
 

Answers and Replies

  • #2
haruspex
Science Advisor
Homework Helper
Insights Author
Gold Member
2020 Award
35,535
6,431
I'm messing around with the swarzchild metric, and I keep getting errors. First, it was a memory, which I could have guessed, 10000x10000 array, so I lowered it to 1000x1000 and it moves past that point, now.
However, this is where I'm getting my error:
Python:
Gravity = zeros([1000,1000])
while i < 1000:
    while j < 1000:
        Gravity[i,j] = metric(sqrt((RS1*sin(ThetaS1)-i)**2+(RS1*cos(ThetaS1-j)**2)),Rs1,step,ThetaS1,omega1*step,speed1)
        Gravity[i,j] += metric(sqrt((RS2*sin(ThetaS2)-i)**2+(RS2*cos(ThetaS2-j)**2)),Rs2,step,ThetaS2,omega2*step,speed2)

print("Done filling\n")
On the first line of the fill, the one with all the 1's, it's giving me this error:
"ValueError: setting an array element with a sequence."
From googling around, it seems that
"the shape of the input list isn't a (generalised) "box" that can be turned into a multidimensional array." -stack exchange.
I was wondering what I am missing that's making this error out?
I'm no Python expert, but taking it to be similar to C++: where are I and j incremented?
 
  • Like
Likes BiGyElLoWhAt
  • #3
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
685
This is not a Python error. It is a NumPy error. Python has no problem with a composite (list, array, map, ...) in which one element is a number, another is a string, yet another is a list, and yet another is a map. This can be a "Good Thing", particularly with maps. Because it can be (and oftentimes is) a good thing to do, python allows it.

NumPy does have problems with this. By default, NumPy tries to be dimensionally correct from a mathematical perspective. Your code starts with Gravity = zeros([1000,1000]). Each element of that 1000 by 1000 array is a number. Assigning a non-numerical value such as a string, a list, a vector, a map, a set, or some other random object doesn't make sense. Python: No complaint. NumPy: Big complaint. This, too, is a "Good Thing".

You haven't shown us what your metric function is doing. Presumably it is not returning something that is trivially reducible to a number. It is instead returning a sequence.
 
  • Like
Likes BiGyElLoWhAt
  • #4
BiGyElLoWhAt
Gold Member
1,570
116
Python:
def metric(r,rs, dt, theta,dtheta, v):
    tau2 = (1-rs/r)*dt**2 - ((1 - rs/r)**-1)*(v*dt)**2 - (r**2)*(dtheta**2)
    return tau2,
Here's the metric function.
I feel a little dumb for leaving out the increments, but adding them in didn't fix the problem.
 
  • #5
BiGyElLoWhAt
Gold Member
1,570
116
well, here's the whole code, because why not...
Code:
# -*- coding: utf-8 -*-
"""
Created on Fri May 06 14:42:16 2016

@author: Tyler
"""

from __future__ import division
from math import sin, cos, sqrt, pi
from numpy import zeros

step = 1
G = 6.674*10**(-11)
c = 3*10**8
Mass1 = 10**20
Mass2 = 10**30
RS1 = 10**3
RS2 = -10**3
ThetaS1= 0
ThetaS2 = pi
Rs1 = 2*G*Mass1/(c**2)
Rs2 = 2*G*Mass2/(c**2)
distance = 100
CoM1 = distance/(1+Mass1/Mass2) #Distance to center of mass of system
CoM2 = distance/(1+Mass2/Mass1) #Distance to center of mass of system
speed1 = sqrt(G*Mass2/CoM1)
speed2 = sqrt(G*Mass1/CoM2)
omega1 = speed1/CoM1
omega2 = speed2/CoM2


def Move_Stars(r, theta):
    x= r*cos(theta + step)
    y = r*sin(theta + step)
    return x,y
def metric(r,rs, dt, theta,dtheta, v):
    tau2 = (1-rs/r)*dt**2 - ((1 - rs/r)**-1)*(v*dt)**2 - (r**2)*(dtheta**2)
    return tau2,

#1 element represents 10000x10000 m
#zero = 5000,5000
i,j = 0,0

Array = [1000,1000]
Gravity = zeros(Array)
while i < 1000:
    while j < 1000:
        Gravity[i,j] = metric(sqrt((RS1*sin(ThetaS1)-i)**2+(RS1*cos(ThetaS1-j)**2)),Rs1,step,ThetaS1,omega1*step,speed1)
        Gravity[i,j] += metric(sqrt((RS2*sin(ThetaS2)-i)**2+(RS2*cos(ThetaS2-j)**2)),Rs2,step,ThetaS2,omega2*step,speed2)
        j+=1
    i+=1
print("Done filling\n")
I've also adjusted it a couple times, trying to define Gravity in different ways, but none of them have solved the problem.
 
  • #6
haruspex
Science Advisor
Homework Helper
Insights Author
Gold Member
2020 Award
35,535
6,431
What is the significance of the comma after "return tau"?
 
  • Like
Likes BiGyElLoWhAt
  • #7
BiGyElLoWhAt
Gold Member
1,570
116
Well, that's cool. I though I looked through it all pretty carefully. I fixed the comma, got a math domain error, and it turns out I also had a misplaced parentheses. Thanks everyone for pointing out my stupid mistakes. I'll try to look more carefully next time.
 
  • #8
BiGyElLoWhAt
Gold Member
1,570
116
It works fine now, is what I mean.
 

Related Threads on Python Errors

  • Last Post
Replies
9
Views
3K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
8
Views
3K
Replies
2
Views
9K
Replies
4
Views
925
Replies
5
Views
4K
Replies
27
Views
3K
Replies
5
Views
965
Replies
6
Views
4K
Top