title: I have two errors: 'RuntimeWarning: divide by zero encountered in double_scalars' ; 'RuntimeWarning: invalid value encountered in subtract'
body: <p>I do not understand how python is dividing by zero. This is my entire code:</p>
  
  <pre><code>from scipy import optimize
  import numpy as np
  from sympy import *
  
  from tkinter import *
  #import ipdb;ipdb.set_trace()
  y=[100,200]
  alp=[0.5,0.5]
  end=[1,2]
  e, t, a, w = symbols('e t a w')
  u= ((a ** a) * (24 * (1 - t) * w + e) * ((1 - a) ** (1 - a))) * (((1 - t) * w) ** (a-1))
  
  print(latex(u))
  def main(x, n):
  
      r = 0
      p = 0
      #j=-1 * ((alp[i] ** alp[i]) * (24 * (1 - x[i]) * y[i] + end[i]) * ((1 - alp[i]) ** (1 - alp[i]))) / ((1 - x[1] * y[i]) ** (1 - alp[i]))
      for i in range(len(y)):
          if i &gt;= n:
  
              r += -1 * ((alp[i] ** alp[i]) * (24 * (1 - x[1]) * y[i] + end[i]) * ((1 - alp[i]) ** (1 - alp[i]))) * (((1 - x[1]) * y[i]) ** (alp[i]-1))
  
  
          elif i &lt; n:
  
              p += -1 * ((alp[i] ** alp[i]) * (24 * y[i] + x[0] + end[i]) * ((1 - alp[i]) ** (1 - alp[i]))) * ((y[i]) ** (alp[i]-1))
      return r + p
  
  def tax_rev(n):
      r = 0
      for i in range(n, len(y)):
          r += y[i]
  
      return r
  
  bounds = [(0, np.inf), (0, 1)]
  
  mat = np.zeros((len(y), 4))
  
  
  for i in range(len(y)):
      # print(i)
      def constraint1(x):
          return 1 * (x[1] * tax_rev(i) - x[0] * (len(y) -i))
  
      cons1 = {'type': 'ineq', 'fun': constraint1}
  
      def co(x):
          return main(x, i)
  
      max = optimize.shgo(co, bounds=bounds, constraints=cons1)
  
      mat[i, 0] = round(max.x[0] ,5)
      mat[i, 1] = round(max.x[1] ,5)
      mat[i, 2] = -1 * round(max.fun ,5)
      mat[i, 3] = max.success
  
  tax_from= np.unravel_index(np.argmax(mat, axis=None), mat.shape)
  
  fin_tex = f'{100 * round(float(mat[tax_from[0] ,1]) ,15)}  % from those endowed with {y[tax_from[0]]}  or more. Transfer {round(float(mat[tax_from[0] ,0]) ,15)} units of money to those endowed with less than  {y[tax_from[0]]} .This would maximize utility in the society.'
  print(fin_tex)
  print(mat)
  </code></pre>
  
  <p>I am using this code to optimize an equation which can be seen in latex (equation in variable u). I do not understand why there is division by zero when I am not dividing anything by 0. I think this is causing the problem when I am optimizing as well.</p>
  
  <p>The division by zero, according to the error message I am receiving is here:</p>
  
  <pre><code>def main(x, n):
  
      r = 0
      p = 0
      #j=-1 * ((alp[i] ** alp[i]) * (24 * (1 - x[i]) * y[i] + end[i]) * ((1 - alp[i]) ** (1 - alp[i]))) / ((1 - x[1] * y[i]) ** (1 - alp[i]))
      for i in range(len(y)):
          if i &gt;= n:
  
              r += -1 * ((alp[i] ** alp[i]) * (24 * (1 - x[1]) * y[i] + end[i]) * ((1 - alp[i]) ** (1 - alp[i]))) * (((1 - x[1]) * y[i]) ** (alp[i]-1))
  
  
          elif i &lt; n:
  
              p += -1 * ((alp[i] ** alp[i]) * (24 * y[i] + x[0] + end[i]) * ((1 - alp[i]) ** (1 - alp[i]))) * ((y[i]) ** (alp[i]-1))
      return r + p
  </code></pre>
  
  <p>It is happening in the variable that I named r.</p>
  
  <p>I am new to Python and any help/advice on my code is welcome :)</p>