函数中有一个参数控制返回值,如何写比较好?

下面是我写的一个函数,有一个参数用来控制返回值,但是写完感觉不够简洁,谁能给我一些改进的建议吗?

def speedest_descent(x0, q, g, G,e=1e-6, times=1000, detail=False):
    # 参数分别为初始点(numpy.array), 目标函数(function), 一阶导(function),
    # 二阶导(function), 精确度(float), 最大迭代次数(int), 标志参数(bool).
    # detail = False, 只返回最优解; detail = True, 返回最优解和求解过程.
    count = 0
    if detail == False:
        while 1:
            p0 = -g(x0) # 下降方向
            if np.sqrt(p0.dot(p0)) <= e:
                return x0
            a0 = p0.dot(p0)/G(x0).dot(p0).dot(p0) # 步长
            x0 = x0 + a0*p0
            if count >= times:
                return x0
    else:
        plist = []
        alist = []
        xlist = [x0]
        while 1:
            p0 = -g(x0) # 下降方向
            plist.append(p0)
            if np.sqrt(p0.dot(p0)) <= e:
                df = DataFrame([xlist, plist, alist], index=['xk', 'pk', 'ak']).T
                return x0, df
            a0 = p0.dot(p0)/G(x0).dot(p0).dot(p0) # 步长
            alist.append(a0)
            x0 = x0 + a0*p0
            xlist.append(x0)
            if count >= times:
                df = DataFrame([xlist, plist, alist], index=['xk', 'pk', 'ak']).T
                return x0, df
阅读 1.4k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进