From 66e7b7e32f29e9aee611447cc5701329b5ec6c3e Mon Sep 17 00:00:00 2001 From: TerenceLiu Date: Mon, 20 Feb 2023 14:46:19 +0800 Subject: [PATCH] Update 'algorithm/optimization/gradient.py' --- algorithm/optimization/gradient.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/algorithm/optimization/gradient.py b/algorithm/optimization/gradient.py index 5f85bc5..29479a8 100644 --- a/algorithm/optimization/gradient.py +++ b/algorithm/optimization/gradient.py @@ -28,7 +28,7 @@ def array_mesh(data, n): array_mesh.append(data[i:i+n]) return np.vstack(array_mesh) -class gd_1d(object): +class gd1d(object): def __init__(self, environ:str="jupyterlab"): pio.renderers.default = environ # 'notebook' or 'colab' or 'jupyterlab' self.wg_expr = widgets.Text(value="sin(x) + sin((10.0 / 3.0) * x)", @@ -150,7 +150,7 @@ class gd2d(object): def initialization_default(self, environ): pio.renderers.default = environ # 'notebook' or 'colab' or 'jupyterlab' - self.wg_expr = widgets.Dropdown(options=[("(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)", "(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)"), ("x1 - x2 + 2*x1*x2 + 2*x1**2 + x2**2", "x1 - x2 + 2*x1*x2 + 2*x1**2 + x2**2")], value="(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)", descrption="Expression") + self.wg_expr = widgets.Dropdown(options=[("(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)", "(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)"), ("x1 - x2 + 2*x1*x2 + 2*x1**2 + x2**2", "x1 - x2 + 2*x1*x2 + 2*x1**2 + x2**2"), ("(4-x1)**2 + x2**2", "(4-x1)**2 + 10*x2**2"), ("x1**2 + x2**2", "x1**2 + 10*x2**2")], value="(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)", descrption="Expression") self.wg_x0 = widgets.Text(value="0,2", description="Startpoint:") self.wg_lr = widgets.FloatText(value="1e-1", description="step size:") self.wg_epsilon = widgets.FloatText(value="1e-5", description="criterion:") @@ -211,10 +211,11 @@ class gd2d(object): x1 = symbols("x1") x2 = symbols("x2") expr = sympify(self.wg_expr.value) - xx1 = np.arange(0, 5, 0.25) - xx2 = np.arange(0, 5, 0.25) - #xx1 = np.arange(np.array(self.xn_list)[:, 0].min() * 0.5, np.array(self.xn_list)[:, 0].max() * 1.5, 0.1) - #xx2 = np.arange(np.array(self.xn_list)[:, 1].min() * 0.5, np.array(self.xn_list)[:, 1].max() * 1.5, 0.1) + expression_list = ["(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)", "(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)"] + if self.wg_expr.value in expression_list: + xx1, xx2 = np.arange(0, 5, 0.25), np.arange(0, 5, 0.25) + else: + xx1, xx2 = np.arange(-5, 5, 0.25), np.arange(-5, 5, 0.25) xx1_tangent = np.arange(np.array(self.xn_list)[:, 0].min(), np.array(self.xn_list)[:, 0].max(), 0.1) xx2_tangent = np.arange(np.array(self.xn_list)[:, 1].min(), np.array(self.xn_list)[:, 1].max(), 0.1) xx1_o, xx2_o = xx1, xx2