Eigenvalue
Eigenvalue

Reputation: 1133

accessing elements of a list and it isnt callable?

Just for reference my code

def makeList (start,end,h):
    a=[]
    a.append(start)
    n=(end-start)/h

    while(n!=0):    
        new=a[-1] +h
        a.append(a[-1] +h)
        n=n-1
    return a

def generateBase(xList,f):

    if(len(xList)==0):
        return []
    elif(xList[0]==0 or len(xList)==1):
        return ([(xList[0],0,0)] + generateBase(xList[1:],f))

    else:
        return ([(xList[0],0,(f(xList[0])))] + generateBase(xList[1:],f))

def getThird(a):
    return a[2]

def fdm(alpha,startx,endx,startt,endt,dx,dt,f):
    baseSolution=generateBase((makeList(startx,endx,dx)),f)
    totalSoltuion= baseSolution + startCalc(alpha,(makeList(startx,endx,dx)),(makeList(startt,endt,dt))[1:],baseSolution,dx,dt,[],[])

def startCalc(alpha,xList,tList,phiSolutions,dx,dt,newPhi,newX):
    print newPhi
    if(len(tList)==0):
        return []
    elif (len(xList)==1):
        return ([(xList[0],tList[0],0)] + startCalc(alpha,(newX + [xList[0]]),tList[1:],(newPhi + [(xList[0],tList[0],0) ]),dx,dt,[],[]))
    elif (xList[0]==0):
        return ([(xList[0],tList[0],0)] + startCalc(alpha,(xList[1:]),tList,phiSolutions,dx,dt,(newPhi + [(xList[0],tList[0],0)]),(newX + [xList[0]]))) 

    else:
        print getThird(phiSolutions[0])
        print getThird(phiSolutions[1])
        print getThird(phiSolutions[2])
        sol=newPhi(xList[0],tList[0],getThird(phiSolutions[0]),getThird(phiSolutions[1]),getThird(phiSolutions[2]),alpha)
        return ([sol] + startCalc(alpha,(xList[1:]),tList,phiSolutions[1:],dx,dt,(newPhi + [sol]),(newX + [xList[0]]))) 




def newPhi(x,t,phiL,phiC,phiR,dx,dt,alpha):
    return (x,t,(phiC + (alpha*(dt/(dx**2)))*(phiR-(2*phiC)+phiL)) )

def showMe(SolutionList):
    GraphSolution(SolutionList)

showMe(fdm(1,0,1,0,1,.1,.01,(lambda x:x+1)))

The issue is here

sol=newPhi(xList[0],tList[0],getThird(phiSolutions[0]),getThird(phiSolutions[1]),getThird(phiSolutions[2]),alpha)

I get this issue

TypeError: 'list' object is not callable

The things in these arrays are real numbers, i thought this issue only happens when i try to call a list like a function if i did like phiSolution(0) im not sure thought whats the issue, i print everything out and its a number, if someone could give me some insight that would be great.

Upvotes: 0

Views: 58

Answers (2)

skrrgwasme
skrrgwasme

Reputation: 9633

Look at your recursive call:

return ([(xList[0],tList[0],0)] + startCalc(alpha,(newX + [xList[0]]),tList[1:],(newPhi + [(xList[0],tList[0],0) ]),dx,dt,[],[]))

In particular, look carefully at your argument list. It breaks down to these arguments:

startCalc(alpha, # arg 1
          (newX + [xList[0]]), #arg 2
          tList[1:], #arg 3
          (newPhi + [(xList[0],tList[0],0) ]), # arg 4
          dx, # arg 5
          dt, # arg 6
          [], # arg 7
          []) # arg 8

According to your definition of startCalc...

startCalc(alpha,xList,tList,phiSolutions,dx,dt,newPhi,newX)

... newPhi is argument #7 by position. Therefore, when you make your tail-recursive call, an empty list is being assigned to newPhi. So newPhi is a list, and sol=newPhi(<anything>) is indeed attempting to call it. If you're trying to index into the list, use brackets:

sol = newPhi[index]

Also, as mentioned in another answer, you're using the identifier newPhi both as a function name and an input argument name to startCalc. You need to change one of these to resolve the conflict.

Upvotes: 2

Jonathan Eunice
Jonathan Eunice

Reputation: 22443

Because newPhi is a list, and therefore is not callable.

Note that you have defined newPhi twice: Once as a function (at about line 48 in the code, between the definitions of startCalc and showMe), and once as the 7th parameter to the startCalc function at about line 29. The definition in force when the TypeError is raised is the one where it's a startCalc parameter.

startCalc is called by the second line of the fdm function at about line 27:

    totalSoltuion= baseSolution + startCalc(alpha,(makeList(startx,endx,dx)),(makeList(startt,endt,dt))[1:],baseSolution,dx,dt,[],[])

And if you'll count 7 parameters in, you'll see that you're passing an empty list ([]).

If you want newPhi to be a function there, move the definition above the definition of startCalc, and rename the current newPhi parameter which is interfering with your newPhi function. You cannot have it be both a list (see lines 34 and 36) and a function (line 42).

Upvotes: 2

Related Questions