1

I have a function that produces an array within it, and I want to do work on the generated array outside of the function, in python. How can I make the function save the array such that this can be done?

Thanks!

My function is here:

def lane_emden(n, delxi=0.00001, xilim=25):

    theta = 1
    dtdx = 0
    xi = 0.01
    #Starting values

    dtdx_values = [dtdx]
    theta_values = [theta]
    xi_values = [xi]
    #Initial values for the lists

    while theta>=0 and xi<=xilim :
        dtdx_new = dtdx - ((2*(2/xi*dtdx)) + theta**n)*delxi
        theta_new = theta + delxi*dtdx_new
        xi_new = xi + delxi
        #Equations to create new values for iterative diff eq solving

        dtdx = dtdx_new
        theta = theta_new
        xi = xi_new
        #Replace the old values with the new ones

        dtdx_values.append(dtdx)
        theta_values.append(theta)
        xi_values.append(xi)
        #Store these new values in previously defined lists

    results = np.array((theta_values, xi_values))
    #create an array of the results (probably done incorrectly)
    return results
    #This is how I tried to get the array saved outside the function

I'm very new to Python, any help would be greatly appreciated!

[Edit] Function call as requested.

Input

lane_emden(5)

Output

array([[  1.00000000e+00,   1.00000000e+00,   1.00000000e+00, ...,
      2.10576105e-01,   2.10576063e-01,   2.10576022e-01],
   [  1.00000000e-02,   1.00100000e-02,   1.00200000e-02, ...,
      2.49999900e+01,   2.50000000e+01,   2.50000100e+01]])
Craig Gardner
  • 67
  • 1
  • 7
  • 3
    What makes you think what you tried didn't work? – Scott Hunter Nov 25 '15 at 20:22
  • 1
    As an ungly workaround you could use [global variables](http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them), but a nicer approach would be to pass a variable from one function to another or make the desired variable a class atribute. – albert Nov 25 '15 at 20:23
  • 4
    Can you show us the line(s) where you call this function? – Knells Nov 25 '15 at 20:23
  • I know it hasn't worked because when I tell it to print my returned array it says the name is not defined. – Craig Gardner Nov 25 '15 at 20:24
  • @MichałF That worked. I think the return function doesn't work how I think it works. Thanks! – Craig Gardner Nov 25 '15 at 20:29
  • That has worked. How do I mark this as answered? :S – Craig Gardner Nov 25 '15 at 20:31
  • The return function isn't a function. It's a statement. It returns the value from the function to outside the function. It can be any type, or if multiple arguments are passed, is a tuple. def a(x): return x; is similar to lambda x: x – Alex Huszagh Nov 25 '15 at 20:33
  • @CraigGardner I move my comment to be an aswer to you question. You can +1 by clicking up arrow. You should also be able to mark it as solution by clicking the tick symbol. – Michał Fita Nov 25 '15 at 20:34

2 Answers2

0

Looks like you are using numpy: results = np.array((theta_values, xi_values)). The documentation for numpy.array() states the first argument must be an array-like object:

An array, any object exposing the array interface, an object whose array method returns an array, or any (nested) sequence.

I think you want numpy.asarray() instead: results = np.asarray((theta_values, xi_values)).

katy lavallee
  • 2,741
  • 1
  • 28
  • 26
0

You already return your array, so now you just need to use it. array = lane_emden(3) for example.

Michał Fita
  • 1,183
  • 1
  • 7
  • 24