Python : ZeroDivisionError: float division by zero - python

I have got these code, But it gives a zero division error. I am not able to figure out what is wrong. I need your help. Thank you. :)
from math import sqrt
def inisialisasi():
filename = raw_input('File will be read? ')
return filename
def readdatafile(filename):
datafile = open('kalibration.txt','r')
datax = []; datay = []; datae = []; i = 0
for row in datafile:
i +=1
data = row.split()
x = float(data[0])
datax.append(x)
y = float(data[1])
datay.append(y)
e = float(data[2])
datae.append(e)
print 'x = %5.2f y = %5.2f e = %5.2f' % (x, y, e)
datafile.close()
n = i
print 'Jumlah data = ', n
return n, datax, datay, datae
def regresilinear(x, y, e):
sum1=0.0; sum2=0.0; sum3=0.0; sum4=0.0; sum5=0.0
for i in range(0, n):
sum1=sum1+(x[i]**2/e[i]**2)
sum2=sum2+(y[i]/e[i]**2)
sum3=sum3+(x[i]/e[i]**2)
sum4=sum4+(x[i]*y[i])/e[i]**2
sum5=sum5+1/e[i]**2
det = (sum5*sum1)-sum3**2
#parameter a dan b
a = ((sum1*sum2)-(sum3*sum4))/det
b = ((sum5*sum4)-(sum3)*(sum2))/det
#ralat
sigmaa2 = sum1/det
sigmab2 = sum5/det
sigmaa = sqrt(sigmaa2)
sigmab = sqrt(sigmab2)
return a, b, sigmaa, sigmab
filename = inisialisasi()
n, datax, datay, datae = readdatafile(filename)
a, b, sigmaa, sigmab = regresilinear(datax,datay, datae)
print 'a= %8.6f b= %8.6f sigmaa= %8.6f sigmab= %8.6f' % (a, b, sigmaa, sigmab)
error :
Traceback (most recent call last):
File "coba6.py", line 55, in
a, b, sigmaa, sigmab = regresilinear(datax, datay, datae)
File "coba6.py", line 42, in regresilinear
a = ((sum1*sum2)-(sum3*sum4))/det
ZeroDivisionError: float division by zero

Somehow, det is set equal to 0. Since you never explicitly prevent that from happening, a single input line like...
1.0 <whatever> 1.0
...can result in division by zero. (The y value doesn't matter in this case.) After substituting, you'll have:
sum1 = 0.0 + 1.0**2 / 1.0**2 # sum1 == 1.0
sum3 = 0.0 + 1.0 / 1.0**2 # sum3 == 1.0
sum5 = 0.0 + 1 / 1.0**2 # sum5 == 1.0
det = 1.0 * 1.0 - 1.0**2 # det == 0.0
...
a = <whatever> / det # KABOOM!
Actually, no input will also produce this error, since the for loop in regresilinear will never alter the various sum* variables from their default 0.0 values.
If you're sure your input isn't doing this, you might want to add print statements inside regresilinear to see how det is getting set to zero. (Or use the pdb debugger, if you're familiar with it.)
PS: It would be much easier to debug your functions if they weren't dependent on global data like n. You don't even need it, since it should always equal the length of the three lists datax, datay, and datae.
PPS: readdatafile completely ignores its filename parameter. So if the hard-coded kalibration.txt file happens to be present but empty, you'll get the exact same ZeroDivisionError.

What does your variable e look like? That is, what does it contain? If there's values of zero in it, you have your answer.

Related

Grid finder python: 3 parameters

I am trying to code a grid finder that will find the optimal values for my 3 parameters a, b and c based on two lists, x3data and y3data. My function is defined at the top.
delx=np.zeros(1)
dely=np.zeros(1)
def func(x, a, b, c):
#y = 100*a*x + 100*b
#y = y - 360.*np.floor(y/360.) # subtracting 360 times the nearest integer of y/360 from y
delx = np.cos(a*x + b) - c
dely = np.sin(a*x + b)
rad = np.sqrt(delx*delx+dely*dely)
y=np.arccos(delx/rad)
ysign1 = np.sign(dely)
ysign2 = (1. - ysign1)/2.
y = ysign2 * 2. * np.pi + (ysign1 *y)
y = y*180./np.pi # in degrees
return y
ndat=len(x3data)
ngrid=50
chimin=1.e10
apar_best = 0.0
bpar_best = 0.0
cpar_best = 0.0
for i in range(ngrid):
for j in range(ngrid):
for k in range(ngrid):
apar=10.*i/float(ngrid-1)
bpar=1.*j/float(ngrid-1)
cpar=1.*k/float(ngrid-1)
chi=0.
for l in range(ndat):
diff=(y3data[l]-func(x3data[l],apar,bpar, cpar))*np.pi/180.
chi=chi+(np.sin(diff))**2
if(chi<chimin):
print(i, j, k, apar, bpar, cpar, np.sqrt(chi/float(ndat)))
chimin=chi
apar_best=apar
bpar_best=bpar
cpar_best = cpar
print("best fit results:")
print(apar_best, bpar_best, cpar_best, np.sqrt(chimin/float(ndat)))
print("a =" , apar_best)
print("b =" , bpar_best)
print("c =" , cpar_best)
best fit results:
0.0 0.8367346938775511 0.673469387755102 6.264895969973369e-05
a = 0.0
b = 0.8367346938775511
c = 0.673469387755102
I keep getting the same values for a, b and c which are incorrect. They are shown above. Not sure where I am going wrong here. Any help would be appreciated.
Here is a short explanation of my grid finder line by line:
First, the code declares the length of the x3data array as the variable ndat, and the grid size as ngrid.
It then sets the chimin (minimum chi) value to a very large number.
It also declares three variables apar_best, bpar_best and cpar_best and sets them to 0.0.
The code then begins a for loop that runs the i variable from 0 to ngrid-1.
Inside the first for loop, there is another for loop that runs the j variable from 0 to ngrid-1.
Inside the second for loop, there is a third for loop that runs the k variable from 0 to ngrid-1.
For each set of i, j, and k values, the code declares three variables apar, bpar and cpar, and sets them to 10*i/ngrid-1, j/ngrid-1 and k/ngrid-1 respectively.
Next, it sets the chi value to 0.
After that, the code begins another for loop that runs the l variable from 0 to ndat.
Inside this loop, the code calculates the difference between the y3data and the func and stores it in the diff variable.
The code then calculates the chi value by squaring the sin of the diff, and adds it to the existing chi value.
If the chi value is less than the chimin value, it prints the values of i, j, k, apar, bpar, cpar and the square root of the chi value divided by ndat.
Finally, the code sets the chimin value to the current chi value, and the apar_best, bpar_best and cpar_best values to the current apar, bpar and cpar values.

When i try to print the mean and standard deviation, I am prompted with a name error: variable not defined

I am trying to print the mean and standard deviation however in its current form it doesnt recognize anything inside the loop. How would I go about correcting this to properly display what is intended. When i try to print the mean it says ex not defined.
import numpy as np
p = 0.44
q = 0.56
mu_1 = 26.5
sigma = 4.3
mu_2 = 76.4
n = 7
print( 'total number of jobs =', n)
lst_times = []
j = 0
def calc_avg_std(n):
while j < 100:
m = np.random.binomial(n,p)
easy_jobs = np.random.normal(mu_1,sigma,m)
n_chall = n-m
chall_jobs = np.random.exponential(mu_2,n_chall)
totalTime = sum(easy_jobs) + sum(chall_jobs)
lst_times.append(totalTime)
j = j + 1
ex = (mu_1 * p) + (mu_2 * q)
ex2 = (p *((mu_1**2)))+ (q*(mu_2**2)*2)
var = ex2-(ex**2)
stdev = np.sqrt(var)
return [ex , stdev]
print(' mean is',ex)
I tried this code without the def and return and runs properly but the professor insists that it should be implemented.
def is used to create a function. When you use return you return the values to the caller.
Replace your last prin witht the following lines:
call the function and keep the return values
print the returned values
mean, stdev = calc_avg_std(n)
print(mean)

Python (TypeError: 'float' object is not callable)

I keep getting this message (TypeError: 'float' object is not callable) from line 38 (np.log(m0-me*t)/me) does anyone know why? Would appreciate some help since I am new to python, thank you.
import math
import numpy as np
"""
Input
"""
ISP = 130 #[s]
t_burn = 3.0 #[s]
g0 = 9.81 #[m/s^2]
mf = 12.0 #[Kg]
D_R = 0.12 #[m]
C_D = 0.4
rho = 1.225
A = math.pi*D_R**2/4
Apogeu = 0.0
me = 0.1
v = []
h = []
while (Apogeu<3000):
m0 = mf + me
t = 0.0
while (t<=20):
if t==0.0:
v = 0.0
elif t<=t_burn:
v = ISP*g0*np.log(m0/(m0-me*t))-g0*t-C_D*0.5*A*v(t-0.01)**2*\
np.log(m0-me*t)/me
h = ISP*g0/me*((m0-me*t)*np.log((m0-me*t)/m0)+me*t)-g0*0.5*t**2\
-C_D*0.5*A*v(t-0.01)**2/me*((t-m0/me)*np.log(m0-me*t)+m0/me-t)
elif t>t_burn:
v = v(t_burn)-g0*t-C_D*0.5*A*v(t-0.01)**2/mf*t
h = h(t_burn) + v(t_burn)*t -0.5*g0*t**2-\
C_D*0.5*A*v(t-0.01)**2*t**2*0.5/mf
if h > Apogeu:
Apogeu = h
print("Apogeu=",Apogeu,"m")
t = t + 0.1
me = me + 0.1
I think it is this line:
v(t-0.01)
It should be:
v*(t-0.01)
Because v is not a function.
This is because the variable v is set to the float 0.0 in a previous loop.
if t == 0.0:
# v is set to the float 0.0 in one iteration of the loop.
v = 0.0
elif t <= t_burn:
# Then `v` is called as a function here, but it's still the float 0.0
# 👇
v = ISP*g0*np.log(m0/(m0-me*t))-g0*t-C_D*0.5*A*v(t-0.01)**2*\
np.log(m0-me*t)/me
It may help catch errors like this if your variables were a bit more verbose and you add more whitespace to your code.
if t==0.0:
v = 0.0
Then
v = ISP*g0*np.log(m0/(m0-me*t))-g0*t-C_D*0.5*A*v(t-0.01)**2*\
np.log(m0-me*t)/me
Specifically v(t-0.01). It's calling v. In fact, you made this mistake in several parts. Are you coming from Matlab perhaps? If you want to access some element in a list, use square brackets.

Sympy solve() gives wrong results

I use Sympy solve() function to solve a large number of equations. All variables in the equations are defined as symbols. Variables can start with the letter P or F. I use solve() to express one specific P variable (the one that I observe) with only F variables, so I use solve() to substitute all other P variables with F variables. The sum of the coefficients before the F variables is ideally 1 or almost 1 (e.g.: 0.99).
This produces good results till a certain point where the number of equations becomes pretty big and also their length. There the Sympy solve() function starts to give me wrong results. The sum of the coefficients becomes negative (e.g. -7,...). It looks like that the solve() function gets problems with substituting any carrying over all variables and their coefficients.
Is there a way to correct this problem?
Dictionary of equations under link: https://drive.google.com/open?id=1VBQucrDU-o1diCd6i4rR3MlRh95qycmK
import json
from sympy import Symbol, Add, Eq, solve
# Get data
# data from link above
with open("C:\\\\Test\\dict.json") as f:
equations = json.load(f)
comp =[]
expressions = []
for p, equation_components in equations.items():
p = Symbol(p)
comp.append(p)
expression = []
for name, multiplier in equation_components.items():
if type(multiplier) == float or type(multiplier) == int:
expression.append(Symbol(name) * multiplier)
else:
expression.append(Symbol(name) * Symbol(multiplier))
expressions.append(Eq(p, Add(*expression)))
# Solution for variable P137807
print("Solving...")
# Works for slice :364 !!!!!
solutions = solve(expressions[:364], comp[:364], simplify=False, rational=False)
# Gives wrong results for slice :366 and above !!!!!
# solutions = solve(expressions[:366], comp[:366], simplify=False, rational=False)
vm_symbol = Symbol("P137807")
solution_1 = solutions[vm_symbol]
print("\n")
print("Solution_1:")
print(solution_1)
print("\n")
#Sum of coefficients
list_sum = []
for i in solution_1.args:
if str(i.args[1]) != "ANaN":
list_sum.append(i.args[0])
coeff_sum = sum(list_sum)
print("Sum:")
print(coeff_sum)
...
I just wanted to mark the problem as solved and provide reference to the solution. Please look at numerical instability when solving n=385 linear equations with Float coefficients #17136.
The solution that worked for me was to use the following solver and not the Sympy solve() function:
def ssolve(eqs, syms):
"""return the solution of linear system of equations
with symbolic coefficients and a unique solution.
Examples
========
>>> eqs=[x-1,x+2*y-z-2,x+z+w-6,2*y+z+x-2]
>>> v=[x,y,z,w]
>>> ssolve(eqs, v)
{x: 1, z: 0, w: 5, y: 1/2}
"""
from sympy.solvers.solveset import linear_coeffs
v = list(syms)
N = len(v)
# convert equations to coefficient dictionaries
print('checking linearity')
d = []
v0 = v + [0]
for e in [i.rewrite(Add) for i in eqs]:
co = linear_coeffs(e, *v)
di = dict([(i, c) for i, c in zip(v0, co) if c or not i])
d.append(di)
print('forward solving')
sol = {}
impl = {}
done = False
while not done:
# check for those that are done
more = set([i for i, di in enumerate(d) if len(di) == 2])
did = 0
while more:
di = d[more.pop()]
c = di.pop(0)
x = list(di)[0]
a = di.pop(x)
K = sol[x] = -c/a
v.remove(x)
changed = True
did += 1
# update everyone else
for j, dj in enumerate(d):
if x not in dj:
continue
dj[0] += dj.pop(x)*K
if len(dj) == 2:
more.add(j)
if did: print('found',did,'definitions')
# solve implicitly for the next variable
dcan = [i for i in d if len(i) > 2]
if not dcan:
done = True
else:
# take shortest first
di = next(ordered(dcan, lambda i: len(i)))
done = False
x = next(ordered(i for i in di if i))
c = di.pop(x)
for k in di:
di[k] /= -c
impl[x] = di.copy()
di.clear()
v.remove(x)
# update everyone else
for j, dj in enumerate(d):
if x not in dj:
continue
done = False
c = dj.pop(x)
for k in impl[x]:
dj[k] = dj.get(k, 0) + impl[x][k]*c
have = set(sol)
sol[0] = 1
while N - len(have):
print(N - len(have), 'to backsub')
for k in impl:
if impl[k] and not set(impl[k]) - have - {0}:
sol[k] = sum(impl[k][vi]*sol[vi] for vi in impl[k])
impl[k].clear()
have.add(k)
sol.pop(0)
return sol

Python -- using regex on a class instance

I have a class that was taking in lists of 1's and 0's and performing GF(2) finite field arithmetic operations. It used to work until I tried to make it take the input in polynomial format. As for how the finite arithmetic will be done after fixing the regex issue, I was thinking about overloading the operators.
The actual code in parsePolyToListInput(input) works when outside the class. The problem seems to be in the regex, which errors that it will only take in a string (this makes sense), but does not seem to initialize with self.expr as a parameter (that's a problem). The #staticmethod just before the initialization was an attempt to salvage the unbound error as it the polynomial was passed in, but this is apparently completely wrong. Just to save you time if you decide to look at any of the arithmetic operations, modular inverse does not work (seems to be due to the formatting issue after every iteration of that while loop for division in the function and what the return type is):
import re
class gf2poly:
#binary arithemtic on polynomials
##staticmethod
def __init__(self,expr):
self.expr = expr
#self.expr = [int(i) for i in expr]
self.expr = gf2poly.parsePolyToListInput(self.expr)
def convert(self): #to clarify the input if necessary
convertToString = str(self.expr)
print "expression is %s"%(convertToString)
def id(self): #returns modulus 2 (1,0,0,1,1,....) for input lists
return [int(self.expr[i])%2 for i in range(len(self.expr))]
def listToInt(self): #converts list to integer for later use
result = gf2poly.id(self)
return int(''.join(map(str,result)))
def prepBinary(a,b): #converts to base 2 and orders min and max for use
a = gf2poly.listToInt(a); b = gf2poly.listToInt(b)
bina = int(str(a),2); binb = int(str(b),2)
a = min(bina,binb); b = max(bina,binb);
return a,b
#staticmethod
def outFormat(raw):
raw = str(raw[::-1]); g = [] #reverse binary string for enumeration
[g.append(i) for i,c in enumerate(raw) if c == '1']
processed = "x**"+' + x**'.join(map(str, g[::-1]))
if len(g) == 0: return 0 #return 0 if list empty
return processed #returns result in gf(2) polynomial form
def parsePolyToListInput(poly):
c = [int(i.group(0)) for i in re.finditer(r'\d+', poly)] #re.finditer returns an iterator
#m = max(c)
return [1 if x in c else 0 for x in xrange(max(c), -1, -1)]
#return d
def add(self,other): #accepts 2 lists as parameters
a = gf2poly.listToInt(self); b = gf2poly.listToInt(other)
bina = int(str(a),2); binb = int(str(b),2)
m = bina^binb; z = "{0:b}".format(m)
return z #returns binary string
def subtract(self,other): #basically same as add() but built differently
result = [self.expr[i] ^ other.expr[i] for i in range(len(max(self.expr,other.expr)))]
return int(''.join(map(str,result)))
def multiply(a,b): #a,b are lists like (1,0,1,0,0,1,....)
a,b = gf2poly.prepBinary(a,b)
g = []; bitsa = "{0:b}".format(a)
[g.append((b<<i)*int(bit)) for i,bit in enumerate(bitsa)]
m = reduce(lambda x,y: x^y,g); z = "{0:b}".format(m)
return z #returns product of 2 polynomials in gf2
def divide(a,b): #a,b are lists like (1,0,1,0,0,1,....)
a,b = gf2poly.prepBinary(a,b)
bitsa = "{0:b}".format(a); bitsb = "{0:b}".format(b)
difflen = len(str(bitsb)) - len(str(bitsa))
c = a<<difflen; q=0
while difflen >= 0 and b != 0: #a is divisor, b is dividend, b/a
q+=1<<difflen; b = b^c # b/a because of sorting in prep
lendif = abs(len(str(bin(b))) - len(str(bin(c))))
c = c>>lendif; difflen -= lendif
r = "{0:b}".format(b); q = "{0:b}".format(q)
return r,q #returns r remainder and q quotient in gf2 division
def remainder(a,b): #separate function for clarity when calling
r = gf2poly.divide(a,b)[0]; r = int(str(r),2)
return "{0:b}".format(r)
def quotient(a,b): #separate function for clarity when calling
q = gf2poly.divide(a,b)[1]; q = int(str(q),2)
return "{0:b}".format(q)
def extendedEuclideanGF2(a,b): # extended euclidean. a,b are GF(2) polynomials in list form
inita,initb=a,b; x,prevx=0,1; y,prevy = 1,0
while sum(b) != 0:
q = gf2poly.quotient(a,b);
q = list(q); q = [int(x) for x in q]
#q = list(q);
#q = tuple([int(i) for i in q])
q = gf2poly(q)
a,b = b,gf2poly.remainder(a,b);
#a = map(list, a);
#b = [list(x) for x in a];
#a = [int(x) for x in a]; b = [int(x) for x in b];
b = list(b); b = [int(x) for x in b]
#b = list(b);
#b = tuple([int(i) for i in b])
b = gf2poly(b)
#x,prevx = (prevx-q*x, x);
#y,prevy=(prevy-q*y, y)
print "types ",type(q),type(a),type(b)
#q=a//b; a,b = b,a%b; x,prevx = (prevx-q*x, x); y,prevy=(prevy-q*y, y)
#print("%d * %d + %d * %d = %d" % (inita,prevx,initb,prevy,a))
return a,prevx,prevy # returns gcd of (a,b), and factors s and t
def modular_inverse(a,mod): # where a,mod are GF(2) polynomials in list form
gcd,s,t = gf2poly.extendedEuclideanGF2(a,mod); mi = gf2poly.remainder(s,mod)
#gcd,s,t = ext_euc_alg_i(a,mod); mi = s%mod
if gcd !=1: return False
#print ("%d * %d mod %d = 1"%(a,mi,mod))
return mi # returns modular inverse of a,mod
I usually test it with this input:
a = x**14 + x**1 + x**0
p1 = gf2poly(a)
b = x**6 + x**2 + x**1
p2 = gf2poly(b)
The first thing you might notice about my code is that it's not very good. There are 2 reasons for that:
1) I wrote it so that the 1st version could do work in the finite field GF(2), and output in polynomial format. Then the next versions were supposed to be able to take polynomial inputs, and also perform the crucial 'modular inverse' function which is not working as planned (this means it's actually not working at all).
2) I'm teaching myself Python (I'm actually teaching myself programming overall), so any constructive criticism from pro Python programmers is welcome as I'm trying to break myself of beginner habits as quickly as possible.
EDIT:
Maybe some more of the code I've been testing with will help clarify what works and what doesn't:
t1 = [1,1,1]; t2 = [1,0,1]; t3 = [1,1]; t4 = [1, 0, 1, 1, 1, 1, 1]
t5 = [1,1,1,1]; t6 = [1,1,0,1]; t7 = [1,0,1,1,0]
f1 = gf2poly(t1); f2 = gf2poly(t2); f3 = gf2poly(t3); f4 = gf2poly(t4)
f5 = gf2poly(t5);f6 = gf2poly(t6);f7 = gf2poly(t7)
##print "subtract: ",a.subtract(b)
##print "add: ",a.add(b)
##print "multiply: ",gf2poly.multiply(f1,f3)
##print "multiply: ",gf2poly.multiply(f1,f2)
##print "multiply: ",gf2poly.multiply(f3,f4)
##print "degree a: ",a.degree()
##print "degree c: ",c.degree()
##print "divide: ",gf2poly.divide(f1,b)
##print "divide: ",gf2poly.divide(f4,a)
##print "divide: ",gf2poly.divide(f4,f2)
##print "divide: ",gf2poly.divide(f2,a)
##print "***********************************"
##print "quotient: ",gf2poly.quotient(f2,f5)
##print "remainder: ",gf2poly.remainder(f2,f5)
##testq = gf2poly.quotient(f4,f2)
##testr = gf2poly.remainder(f4,f2)
##print "quotient: ",testq,type(testq)
##print "remainder: ",testr,type(testr)
##print "***********************************"
##print "outFormat testp: ",gf2poly.outFormat(testq)
##print "outFormat testr: ",gf2poly.outFormat(testr)
##print "***********************************"
#print "gf2poly.modular_inverse(): ",gf2poly.modular_inverse(f2,f3)
print "p1 ",p1 #,type(f2),type(f3)
#print "parsePolyToListInput ",gf2poly.parsePolyToListInput(a)
Part of your problem is that you haven't declared self as an argument for parsePolyToListInput. When you call a method, the instance you call it on is implicitly bound as the first argument. Naming the first argument self is a convention, not a strict requirement - the instance is being bound to poly, which you then try to run a regexp over.
It looks me like there's some confusion in your design here about what's behavior of individual instances of the class and what's class-level or module-level behavior. In Python, it's perfectly acceptable to leave something that doesn't take an instance of a class as a parameter defined as a module-level function rather than shoehorning it in awkwardly. parsePolyToListInput might be one such function.
Your add implementation, similarly, has a comment saying it "accepts 2 lists as parameters". In fact, it's going to get a gf2poly instance as its first argument - this is probably right if you're planning to do operator overloading, but it means the second argument should also be a gf2poly instance as well.
EDIT:
Yeah, your example code shows a breakdown between class behavior and instance behavior. Either your multiply call should look something like this:
print "multiply: ",f1.multiply(f3)
Or multiply shouldn't be a method at all:
gfpoly.py:
def multiply(f1, f2):
a,b = prepBinary(a,b)
g = []; bitsa = "{0:b}".format(a)
[g.append((b<<i)*int(bit)) for i,bit in enumerate(bitsa)]
m = reduce(lambda x,y: x^y,g); z = "{0:b}".format(m)
return z #returns product of 2 polynomials in gf2
That latter approach is, for instance, how the standard math library does things.
The advantage of defining a multiplication method is that you could name it appropriately (http://docs.python.org/2/reference/datamodel.html#special-method-names) and use it with the * operator:
print "multiply: ",f1 *f3

Categories