Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python Errors

Tags:
  1. May 6, 2016 #1

    BiGyElLoWhAt

    User Avatar
    Gold Member

    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:
    Code (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?
     
  2. jcsd
  3. May 6, 2016 #2

    haruspex

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    I'm no Python expert, but taking it to be similar to C++: where are I and j incremented?
     
  4. May 6, 2016 #3

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  5. May 6, 2016 #4

    BiGyElLoWhAt

    User Avatar
    Gold Member

    Code (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.
     
  6. May 6, 2016 #5

    BiGyElLoWhAt

    User Avatar
    Gold Member

    well, here's the whole code, because why not...
    Code (Text):
    # -*- 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.
     
  7. May 6, 2016 #6

    haruspex

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    What is the significance of the comma after "return tau"?
     
  8. May 6, 2016 #7

    BiGyElLoWhAt

    User Avatar
    Gold Member

    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.
     
  9. May 6, 2016 #8

    BiGyElLoWhAt

    User Avatar
    Gold Member

    It works fine now, is what I mean.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Python Errors
  1. Error in python book? (Replies: 3)

  2. Error on Python (Replies: 9)

  3. Key error on python (Replies: 0)

Loading...