code: def geometric_series(q, n):
      
      q = np.asarray(q)
      if n < 0:
          raise ValueError('Finite geometric series is only defined for n>=0.')
      else:
          """q is scalar"""
          if q.ndim == 0:
              if q == 1:
                  s = (n + 1) * 1.0
                  return s
              else:
                  s = (1.0 - q ** (n + 1)) / (1.0 - q)
                  return s
          """q is ndarray"""
          s = np.zeros(np.shape(q), dtype=q.dtype)
          """All elements with value q=1"""
          ind = (q == 1.0)
          """For q=1 the sum has the value s=n+1"""
          s[ind] = (n + 1) * 1.0
          """All elements with value q\neq 1"""
          not_ind = np.logical_not(ind)
          s[not_ind] = (1.0 - q[not_ind] ** (n + 1)) / (1.0 - q[not_ind])
          return s
docs: Compute finite geometric series.
  
                                  \frac{1-q^{n+1}}{1-q}   q \neq 1
          \sum_{k=0}^{n} q^{k}=
                                   n+1                     q  = 1
  
      Parameters
      ----------
      q : array-like
          The common ratio of the geometric series.
      n : int
          The number of terms in the finite series.
  
      Returns
      -------
      s : float or ndarray
          The value of the finite series.