sparrow
sparrow

Reputation: 297

TypeError: unsupported operand type(s) for /: 'float' and 'instancemethod'

I am getting the following error when I tried debug this code for other errors.

Traceback (most recent call last):
  File "testSweep.py", line 43, in <module>
    print sweep_operator(mesh, q)
  File "testSweep.py", line 32, in sweep_operator
    a = 2.0 * mu / dx
TypeError: unsupported operand type(s) for /: 'float' and 'instancemethod'

Any suggestion on this?

import numpy as np
class Mesh():
    def __init__(self, num_cells, num_angles, sigma_t, sigma_s, length):
        self.num_cells = num_cells 
        self.num_angles = num_angles 
        self.sigma_t = sigma_t  
        self.sigma_s = sigma_s 
        self.length = length   

    def dx(self, dif):
    # get the width of each cell
        self.dif = self.num_cells/self.length
        return self.dif

def sweep_operator(mesh, q) :
    mu, w = np.polynomial.legendre.leggauss(2*mesh.num_angles)
    mu = mu[mesh.num_angles:]
    w  = w[mesh.num_angles:]
    phi = np.zeros(mesh.num_cells)

    for o in range(0, 2) : 
        psi_edge = np.zeros(mesh.num_angles)
        i_min = 0; i_max = mesh.num_cells; i_inc = 1
        if o :
            i_min = mesh.num_cells-1; i_max = -1; i_inc = -1
        for i in range(i_min, i_max, i_inc) :
            a = 2.0 * mu / dx
            b = 1.0 / (mesh.sigma_t + a)
            psi_center = b * (q + a[:] * psi_edge[:]) 
            psi_edge[:] = 2.0*psi_center[:] - psi_edge[:]            
            phi[i] += np.dot(psi_center, w)
    return phi


mesh = Mesh(1000, 4, 1.0, 0.5, 10)
q = 1.0
dx = mesh.dx
print sweep_operator(mesh, q)

Upvotes: 0

Views: 4959

Answers (1)

Christopher Ian  Stern
Christopher Ian Stern

Reputation: 1412

You forgot the parentheses

dx = mesh.dx ()

So instead of a number, dx is a thing you can call to get a number from mesh - an method on the instance 'mesh'.

Upvotes: 2

Related Questions