{"id":319713,"date":"2021-03-16T21:01:44","date_gmt":"2021-03-16T21:01:44","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=319713"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=319713","title":{"rendered":"\u0413\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u044b\u0439 \u0441\u043f\u0443\u0441\u043a \u0432 Python"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1e5\/c2e\/731\/1e5c2e7314e9b651645962abf95dd555.jpeg\" width=\"1078\" height=\"720\"><figcaption><\/figcaption><\/figure>\n<h2>\u0417\u0430\u0434\u0430\u0447\u0430 \u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f<\/h2>\n<ul>\n<li>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0446\u0435\u043b\u044c &#8212; \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0439\u0434\u0435\u0442 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u0430 \u043d\u0430 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u043c \u0440\u0430\u0434\u0438\u0443\u0441\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u043c \u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d \u043d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0435<\/p>\n<\/li>\n<\/ul>\n<h2>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435, \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430<\/h2>\n<p>\u0420\u0430\u0431\u043e\u0447\u0430\u044f \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) \u0440\u0430\u0437\u0431\u0438\u0442\u0430 \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0447\u0435\u043a. \u0412\u044b\u0431\u0440\u0430\u043d\u044b \u0442\u043e\u0447\u043a\u0438 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u043e\u0432. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0432\u0441\u0435 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, \u0434\u0430\u044e\u0449\u0438\u0439 \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0417\u0430\u0442\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430.<\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e, numpy \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439 sinus, cosinus \u0438 exp. \u0422\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c matplotlib \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0438\u043a\u043e\u0432.<\/p>\n<pre><code class=\"python\">import numpy as np import matplotlib.pyplot as plot<\/code><\/pre>\n<p>\u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b<\/p>\n<pre><code class=\"python\">radius = 8                                  # working plane radius centre = (global_epsilon, global_epsilon)   # centre of the working circle arr_shape = 100                             # number of points processed \/ 360 step = radius \/ arr_shape                   # step between two points<\/code><\/pre>\n<p>arr_shape \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c 100, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u043e\u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0435, \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435. \u0418 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0435, \u0438\u043d\u0430\u0447\u0435 \u044d\u0442\u043e \u0438\u0441\u043f\u043e\u0440\u0442\u0438\u0442 \u0440\u0430\u0441\u0447\u0435\u0442\u044b.<\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f, \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0438\u043d\u0438\u043c\u0443\u043c:<\/p>\n<pre><code class=\"python\">def differentiable_function(x, y):     return np.sin(x) * np.exp((1 - np.cos(y)) ** 2) + \\            np.cos(y) * np.exp((1 - np.sin(x)) ** 2) + (x - y) ** 2<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/444\/705\/567\/4447055674a10fc82ee535ab61697108.png\" width=\"616\" height=\"68\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u0440\u0435\u0434\u0435\u043b \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0441\u0442\u0440\u0435\u043c\u0438\u0442\u0441\u044f \u043a \u043d\u0443\u043b\u044e, \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0440\u0430\u0434\u0438\u0443\u0441\u043e\u043c \u0440\u0430\u0431\u043e\u0447\u0435\u0439 \u043f\u043b\u043e\u0441\u043a\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"python\">global_epsilon = 0.000000001                # argument increment for derivative<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0433\u043e \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u043f\u043b\u043e\u0441\u043a\u043e\u0441\u0442\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c \u043f\u043e\u0432\u043e\u0440\u043e\u0442 \u0432\u0435\u043a\u0442\u043e\u0440\u0430:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0ff\/228\/720\/0ff2287204275fa949fa4ddf6c10087f.png\" width=\"575\" height=\"84\"><figcaption><\/figcaption><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043a \u0432\u0435\u043a\u0442\u043e\u0440\u0443 (x, 0), \u043f\u043e\u0432\u0435\u0440\u043d\u0443\u0442\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"python\">def rotate_vector(length, a):     return length * np.cos(a), length * np.sin(a)<\/code><\/pre>\n<p>\u0420\u0430\u0441\u0447\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u043e\u0439 \u043f\u043e \u043e\u0441\u0438 Y, \u0433\u0434\u0435 \u044d\u043f\u0441\u0438\u043b\u043e\u043d &#8212; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 y:<\/p>\n<pre><code class=\"python\">def derivative_y(epsilon, arg):     return (differentiable_function(arg, epsilon + global_epsilon) -              differentiable_function(arg, epsilon)) \/ global_epsilon<\/code><\/pre>\n<p>\u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u043e\u0439 \u043f\u043e \u043e\u0441\u0438 X, \u0433\u0434\u0435 \u044d\u043f\u0441\u0438\u043b\u043e\u043d &#8212; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 x:<\/p>\n<pre><code class=\"python\">def derivative_x(epsilon, arg):     return (differentiable_function(global_epsilon + epsilon, arg) -              differentiable_function(epsilon, arg)) \/ global_epsilon<\/code><\/pre>\n<p>\u0420\u0430\u0441\u0447\u0435\u0442 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u0430:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fcb\/1c2\/062\/fcb1c2062e66c16f268242b943bd45c3.png\" width=\"1093\" height=\"240\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043b\u044f 2D-\u0444\u0443\u043d\u043a\u0446\u0438\u0438, k \u0440\u0430\u0432\u043d\u043e \u043d\u0443\u043b\u044e<\/p>\n<pre><code class=\"python\">gradient = derivative_x(x, y) + derivative_y(y, x)<\/code><\/pre>\n<p>\u0421\u0445\u0435\u043c\u0430 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u043e\u0447\u0435\u043a<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/04e\/483\/6a9\/04e4836a983780c9542bc6c1636593a2.png\" width=\"1013\" height=\"607\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u043e\u0432.<\/p>\n<p>\u041b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u0441\u043c\u0435\u043d\u0435 \u0437\u043d\u0430\u043a\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u043e\u0439 \u0441 \u043c\u0438\u043d\u0443\u0441\u0430 \u043d\u0430 \u043f\u043b\u044e\u0441. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e\u0431 \u044d\u0442\u043e\u043c \u0437\u0434\u0435\u0441\u044c: <a href=\"https:\/\/en.wikipedia.org\/wiki\/Maxima_and_minima\" rel=\"noopener noreferrer nofollow\">https:\/\/en.wikipedia.org\/wiki\/Maxima_and_minima<\/a><\/p>\n<pre><code class=\"python\">def calculate_flip_points():     flip_points = np.array([0, 0])     points = np.zeros((360, arr_shape), dtype=bool)     cx, cy = centre      for i in range(arr_shape):         for alpha in range(360):             x, y = rotate_vector(step, alpha)             x = x * i + cx             y = y * i + cy             points[alpha][i] = derivative_x(x, y) + derivative_y(y, x) &gt; 0             if not points[alpha][i - 1] and points[alpha][i]:                 flip_points = np.vstack((flip_points, np.array([alpha, i - 1])))      return flip_points<\/code><\/pre>\n<p>\u0412\u044b\u0431\u043e\u0440 \u0442\u043e\u0447\u043a\u0438 \u0438\u0437 flip_points, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e:<\/p>\n<pre><code class=\"python\">def pick_estimates(positions):     vx, vy = rotate_vector(step, positions[1][0])     cx, cy = centre     best_x, best_y = cx + vx * positions[1][1], cy + vy * positions[1][1]      for index in range(2, len(positions)):         vx, vy = rotate_vector(step, positions[index][0])         x, y = cx + vx * positions[index][1], cy + vy * positions[index][1]         if differentiable_function(best_x, best_y) &gt; differentiable_function(x, y):             best_x = x             best_y = y      for index in range(360):         vx, vy = rotate_vector(step, index)         x, y = cx + vx * (arr_shape - 1), cy + vy * (arr_shape - 1)         if differentiable_function(best_x, best_y) &gt; differentiable_function(x, y):             best_x = x             best_y = y      return best_x, best_y<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430:<\/p>\n<pre><code class=\"python\">def gradient_descent(best_estimates, is_x):     derivative = derivative_x if is_x else derivative_y     best_x, best_y = best_estimates     descent_step = step     value = derivative(best_y, best_x)      while abs(value) &gt; global_epsilon:         descent_step *= 0.95         best_y = best_y - descent_step \\             if derivative(best_y, best_x) &gt; 0 else best_y + descent_step         value = derivative(best_y, best_x)      return best_y, best_x<\/code><\/pre>\n<p>\u041d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0442\u043e\u0447\u043a\u0438 \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u0430:<\/p>\n<pre><code>def find_minimum():     return gradient_descent(gradient_descent(pick_estimates(calculate_flip_points()), False), True)<\/code><\/pre>\n<p>\u0424\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0435\u0442\u043a\u0438 \u0442\u043e\u0447\u0435\u043a \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">def get_grid(grid_step):     samples = np.arange(-radius, radius, grid_step)     x, y = np.meshgrid(samples, samples)      return x, y, differentiable_function(x, y)<\/code><\/pre>\n<p>\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0433\u0440\u0430\u0444\u0438\u043a\u0430:<\/p>\n<pre><code class=\"python\">def draw_chart(point, grid):     point_x, point_y, point_z = point     grid_x, grid_y, grid_z = grid     plot.rcParams.update({         'figure.figsize': (4, 4),         'figure.dpi': 200,         'xtick.labelsize': 4,         'ytick.labelsize': 4     })     ax = plot.figure().add_subplot(111, projection='3d')     ax.scatter(point_x, point_y, point_z, color='red')     ax.plot_surface(grid_x, grid_y, grid_z, rstride=5, cstride=5, alpha=0.7)      plot.show()<\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f main:<\/p>\n<pre><code class=\"python\">if __name__ == '__main__':     min_x, min_y = find_minimum()     minimum = (min_x, min_y, differentiable_function(min_x, min_y))      draw_chart(minimum, get_grid(0.05))<\/code><\/pre>\n<p>\u0413\u0440\u0430\u0444\u0438\u043a:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1a7\/56e\/ac6\/1a756eac651aeb30e069654c59116d96.png\" width=\"827\" height=\"656\"><figcaption><\/figcaption><\/figure>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0442\u043e\u0447\u043d\u044b\u043c \u043f\u0440\u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f\u0445 \u0432 \u0431\u043e\u043b\u0435\u0435 \u043a\u0440\u0443\u043f\u043d\u043e\u043c \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0440\u0430\u0434\u0438\u0443\u0441 \u0440\u0430\u0431\u043e\u0447\u0435\u0439 \u043f\u043b\u043e\u0441\u043a\u043e\u0441\u0442\u0438 \u0440\u0430\u0432\u0435\u043d 1000, \u043d\u043e \u043e\u043d \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0442\u043e\u0447\u043d\u044b\u043c. \u041f\u043b\u044e\u0441 \u0432 \u043b\u044e\u0431\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0440\u0430\u0434\u0438\u0443\u0441 \u0431\u043e\u043b\u044c\u0448\u043e\u0439, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0432 \u0442\u043e\u043c \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0438, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043c\u0435\u0442\u043d\u0430 \u043d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0435.<\/p>\n<h2>\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434:<\/h2>\n<pre><code class=\"python\">import numpy as np import matplotlib.pyplot as plot   radius = 8                                  # working plane radius global_epsilon = 0.000000001                # argument increment for derivative centre = (global_epsilon, global_epsilon)   # centre of the working circle arr_shape = 100                             # number of points processed \/ 360 step = radius \/ arr_shape                   # step between two points   def differentiable_function(x, y):     return np.sin(x) * np.exp((1 - np.cos(y)) ** 2) + \\            np.cos(y) * np.exp((1 - np.sin(x)) ** 2) + (x - y) ** 2   def rotate_vector(length, a):     return length * np.cos(a), length * np.sin(a)   def derivative_x(epsilon, arg):     return (differentiable_function(global_epsilon + epsilon, arg) -             differentiable_function(epsilon, arg)) \/ global_epsilon   def derivative_y(epsilon, arg):     return (differentiable_function(arg, epsilon + global_epsilon) -             differentiable_function(arg, epsilon)) \/ global_epsilon   def calculate_flip_points():     flip_points = np.array([0, 0])     points = np.zeros((360, arr_shape), dtype=bool)     cx, cy = centre      for i in range(arr_shape):         for alpha in range(360):             x, y = rotate_vector(step, alpha)             x = x * i + cx             y = y * i + cy             points[alpha][i] = derivative_x(x, y) + derivative_y(y, x) &gt; 0             if not points[alpha][i - 1] and points[alpha][i]:                 flip_points = np.vstack((flip_points, np.array([alpha, i - 1])))      return flip_points   def pick_estimates(positions):     vx, vy = rotate_vector(step, positions[1][0])     cx, cy = centre     best_x, best_y = cx + vx * positions[1][1], cy + vy * positions[1][1]      for index in range(2, len(positions)):         vx, vy = rotate_vector(step, positions[index][0])         x, y = cx + vx * positions[index][1], cy + vy * positions[index][1]         if differentiable_function(best_x, best_y) &gt; differentiable_function(x, y):             best_x = x             best_y = y      for index in range(360):         vx, vy = rotate_vector(step, index)         x, y = cx + vx * (arr_shape - 1), cy + vy * (arr_shape - 1)         if differentiable_function(best_x, best_y) &gt; differentiable_function(x, y):             best_x = x             best_y = y      return best_x, best_y   def gradient_descent(best_estimates, is_x):     derivative = derivative_x if is_x else derivative_y     best_x, best_y = best_estimates     descent_step = step     value = derivative(best_y, best_x)      while abs(value) &gt; global_epsilon:         descent_step *= 0.95         best_y = best_y - descent_step \\             if derivative(best_y, best_x) &gt; 0 else best_y + descent_step         value = derivative(best_y, best_x)      return best_y, best_x   def find_minimum():     return gradient_descent(gradient_descent(pick_estimates(calculate_flip_points()), False), True)   def get_grid(grid_step):     samples = np.arange(-radius, radius, grid_step)     x, y = np.meshgrid(samples, samples)     return x, y, differentiable_function(x, y)   def draw_chart(point, grid):     point_x, point_y, point_z = point     grid_x, grid_y, grid_z = grid     plot.rcParams.update({         'figure.figsize': (4, 4),         'figure.dpi': 200,         'xtick.labelsize': 4,         'ytick.labelsize': 4     })     ax = plot.figure().add_subplot(111, projection='3d')     ax.scatter(point_x, point_y, point_z, color='red')     ax.plot_surface(grid_x, grid_y, grid_z, rstride=5, cstride=5, alpha=0.7)     plot.show()   if __name__ == '__main__':     min_x, min_y = find_minimum()     minimum = (min_x, min_y, differentiable_function(min_x, min_y))     draw_chart(minimum, get_grid(0.05))<\/code><\/pre>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/547424\/\"> https:\/\/habr.com\/ru\/post\/547424\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h2>\u0417\u0430\u0434\u0430\u0447\u0430 \u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f<\/h2>\n<ul>\n<li>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0446\u0435\u043b\u044c &#8212; \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0439\u0434\u0435\u0442 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u0430 \u043d\u0430 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u043c \u0440\u0430\u0434\u0438\u0443\u0441\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u043c \u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d \u043d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0435<\/p>\n<\/li>\n<\/ul>\n<h2>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435, \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430<\/h2>\n<p>\u0420\u0430\u0431\u043e\u0447\u0430\u044f \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) \u0440\u0430\u0437\u0431\u0438\u0442\u0430 \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0447\u0435\u043a. \u0412\u044b\u0431\u0440\u0430\u043d\u044b \u0442\u043e\u0447\u043a\u0438 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u043e\u0432. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0432\u0441\u0435 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, \u0434\u0430\u044e\u0449\u0438\u0439 \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0417\u0430\u0442\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430.<\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e, numpy \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439 sinus, cosinus \u0438 exp. \u0422\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c matplotlib \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0438\u043a\u043e\u0432.<\/p>\n<pre><code class=\"python\">import numpy as np import matplotlib.pyplot as plot<\/code><\/pre>\n<p>\u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b<\/p>\n<pre><code class=\"python\">radius = 8                                  # working plane radius centre = (global_epsilon, global_epsilon)   # centre of the working circle arr_shape = 100                             # number of points processed \/ 360 step = radius \/ arr_shape                   # step between two points<\/code><\/pre>\n<p>arr_shape \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c 100, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u043e\u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0435, \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435. \u0418 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0435, \u0438\u043d\u0430\u0447\u0435 \u044d\u0442\u043e \u0438\u0441\u043f\u043e\u0440\u0442\u0438\u0442 \u0440\u0430\u0441\u0447\u0435\u0442\u044b.<\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f, \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0438\u043d\u0438\u043c\u0443\u043c:<\/p>\n<pre><code class=\"python\">def differentiable_function(x, y):     return np.sin(x) * np.exp((1 - np.cos(y)) ** 2) + \\            np.cos(y) * np.exp((1 - np.sin(x)) ** 2) + (x - y) ** 2<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u0440\u0435\u0434\u0435\u043b \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0441\u0442\u0440\u0435\u043c\u0438\u0442\u0441\u044f \u043a \u043d\u0443\u043b\u044e, \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0440\u0430\u0434\u0438\u0443\u0441\u043e\u043c \u0440\u0430\u0431\u043e\u0447\u0435\u0439 \u043f\u043b\u043e\u0441\u043a\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"python\">global_epsilon = 0.000000001                # argument increment for derivative<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0433\u043e \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u043f\u043b\u043e\u0441\u043a\u043e\u0441\u0442\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c \u043f\u043e\u0432\u043e\u0440\u043e\u0442 \u0432\u0435\u043a\u0442\u043e\u0440\u0430:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043a \u0432\u0435\u043a\u0442\u043e\u0440\u0443 (x, 0), \u043f\u043e\u0432\u0435\u0440\u043d\u0443\u0442\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"python\">def rotate_vector(length, a):     return length * np.cos(a), length * np.sin(a)<\/code><\/pre>\n<p>\u0420\u0430\u0441\u0447\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u043e\u0439 \u043f\u043e \u043e\u0441\u0438 Y, \u0433\u0434\u0435 \u044d\u043f\u0441\u0438\u043b\u043e\u043d &#8212; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 y:<\/p>\n<pre><code class=\"python\">def derivative_y(epsilon, arg):     return (differentiable_function(arg, epsilon + global_epsilon) -              differentiable_function(arg, epsilon)) \/ global_epsilon<\/code><\/pre>\n<p>\u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u043e\u0439 \u043f\u043e \u043e\u0441\u0438 X, \u0433\u0434\u0435 \u044d\u043f\u0441\u0438\u043b\u043e\u043d &#8212; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 x:<\/p>\n<pre><code class=\"python\">def derivative_x(epsilon, arg):     return (differentiable_function(global_epsilon + epsilon, arg) -              differentiable_function(epsilon, arg)) \/ global_epsilon<\/code><\/pre>\n<p>\u0420\u0430\u0441\u0447\u0435\u0442 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u0430:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043b\u044f 2D-\u0444\u0443\u043d\u043a\u0446\u0438\u0438, k \u0440\u0430\u0432\u043d\u043e \u043d\u0443\u043b\u044e<\/p>\n<pre><code class=\"python\">gradient = derivative_x(x, y) + derivative_y(y, x)<\/code><\/pre>\n<p>\u0421\u0445\u0435\u043c\u0430 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0442\u043e\u0447\u0435\u043a<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u043e\u0432.<\/p>\n<p>\u041b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u0441\u043c\u0435\u043d\u0435 \u0437\u043d\u0430\u043a\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u043e\u0439 \u0441 \u043c\u0438\u043d\u0443\u0441\u0430 \u043d\u0430 \u043f\u043b\u044e\u0441. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e\u0431 \u044d\u0442\u043e\u043c \u0437\u0434\u0435\u0441\u044c: <a href=\"https:\/\/en.wikipedia.org\/wiki\/Maxima_and_minima\" rel=\"noopener noreferrer nofollow\">https:\/\/en.wikipedia.org\/wiki\/Maxima_and_minima<\/a><\/p>\n<pre><code class=\"python\">def calculate_flip_points():     flip_points = np.array([0, 0])     points = np.zeros((360, arr_shape), dtype=bool)     cx, cy = centre      for i in range(arr_shape):         for alpha in range(360):             x, y = rotate_vector(step, alpha)             x = x * i + cx             y = y * i + cy             points[alpha][i] = derivative_x(x, y) + derivative_y(y, x) &gt; 0             if not points[alpha][i - 1] and points[alpha][i]:                 flip_points = np.vstack((flip_points, np.array([alpha, i - 1])))      return flip_points<\/code><\/pre>\n<p>\u0412\u044b\u0431\u043e\u0440 \u0442\u043e\u0447\u043a\u0438 \u0438\u0437 flip_points, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e:<\/p>\n<pre><code class=\"python\">def pick_estimates(positions):     vx, vy = rotate_vector(step, positions[1][0])     cx, cy = centre     best_x, best_y = cx + vx * positions[1][1], cy + vy * positions[1][1]      for index in range(2, len(positions)):         vx, vy = rotate_vector(step, positions[index][0])         x, y = cx + vx * positions[index][1], cy + vy * positions[index][1]         if differentiable_function(best_x, best_y) &gt; differentiable_function(x, y):             best_x = x             best_y = y      for index in range(360):         vx, vy = rotate_vector(step, index)         x, y = cx + vx * (arr_shape - 1), cy + vy * (arr_shape - 1)         if differentiable_function(best_x, best_y) &gt; differentiable_function(x, y):             best_x = x             best_y = y      return best_x, best_y<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 \u0433\u0440\u0430\u0434\u0438\u0435\u043d\u0442\u043d\u043e\u0433\u043e \u0441\u043f\u0443\u0441\u043a\u0430:<\/p>\n<pre><code class=\"python\">def gradient_descent(best_estimates, is_x):     derivative = derivative_x if is_x else derivative_y     best_x, best_y = best_estimates     descent_step = step     value = derivative(best_y, best_x)      while abs(value) &gt; global_epsilon:         descent_step *= 0.95         best_y = best_y - descent_step \\             if derivative(best_y, best_x) &gt; 0 else best_y + descent_step         value = derivative(best_y, best_x)      return best_y, best_x<\/code><\/pre>\n<p>\u041d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0442\u043e\u0447\u043a\u0438 \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u0430:<\/p>\n<pre><code>def find_minimum():     return gradient_descent(gradient_descent(pick_estimates(calculate_flip_points()), False), True)<\/code><\/pre>\n<p>\u0424\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0435\u0442\u043a\u0438 \u0442\u043e\u0447\u0435\u043a \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"python\">def get_grid(grid_step):     samples = np.arange(-radius, radius, grid_step)     x, y = np.meshgrid(samples, samples)      return x, y, differentiable_function(x, y)<\/code><\/pre>\n<p>\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0433\u0440\u0430\u0444\u0438\u043a\u0430:<\/p>\n<pre><code class=\"python\">def draw_chart(point, grid):     point_x, point_y, point_z = point     grid_x, grid_y, grid_z = grid     plot.rcParams.update({         'figure.figsize': (4, 4),         'figure.dpi': 200,         'xtick.labelsize': 4,         'ytick.labelsize': 4     })     ax = plot.figure().add_subplot(111, projection='3d')     ax.scatter(point_x, point_y, point_z, color='red')     ax.plot_surface(grid_x, grid_y, grid_z, rstride=5, cstride=5, alpha=0.7)      plot.show()<\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f main:<\/p>\n<pre><code class=\"python\">if __name__ == '__main__':     min_x, min_y = find_minimum()     minimum = (min_x, min_y, differentiable_function(min_x, min_y))      draw_chart(minimum, get_grid(0.05))<\/code><\/pre>\n<p>\u0413\u0440\u0430\u0444\u0438\u043a:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0442\u043e\u0447\u043d\u044b\u043c \u043f\u0440\u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f\u0445 \u0432 \u0431\u043e\u043b\u0435\u0435 \u043a\u0440\u0443\u043f\u043d\u043e\u043c \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0440\u0430\u0434\u0438\u0443\u0441 \u0440\u0430\u0431\u043e\u0447\u0435\u0439 \u043f\u043b\u043e\u0441\u043a\u043e\u0441\u0442\u0438 \u0440\u0430\u0432\u0435\u043d 1000, \u043d\u043e \u043e\u043d \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0442\u043e\u0447\u043d\u044b\u043c. \u041f\u043b\u044e\u0441 \u0432 \u043b\u044e\u0431\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0440\u0430\u0434\u0438\u0443\u0441 \u0431\u043e\u043b\u044c\u0448\u043e\u0439, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0432 \u0442\u043e\u043c \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0438, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043c\u0435\u0442\u043d\u0430 \u043d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0435.<\/p>\n<h2>\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434:<\/h2>\n<pre><code class=\"python\">import numpy as np import matplotlib.pyplot as plot   radius = 8                                  # working plane radius global_epsilon = 0.000000001                # argument increment for derivative centre = (global_epsilon, global_epsilon)   # centre of the working circle arr_shape = 100                             # number of points processed \/ 360 step = radius \/ arr_shape                   # step between two points   def differentiable_function(x, y):     return np.sin(x) * np.exp((1 - np.cos(y)) ** 2) + \\            np.cos(y) * np.exp((1 - np.sin(x)) ** 2) + (x - y) ** 2   def rotate_vector(length, a):     return length * np.cos(a), length * np.sin(a)   def derivative_x(epsilon, arg):     return (differentiable_function(global_epsilon + epsilon, arg) -             differentiable_function(epsilon, arg)) \/ global_epsilon   def derivative_y(epsilon, arg):     return (differentiable_function(arg, epsilon + global_epsilon) -             differentiable_function(arg, epsilon)) \/ global_epsilon   def calculate_flip_points():     flip_points = np.array([0, 0])     points = np.zeros((360, arr_shape), dtype=bool)     cx, cy = centre      for i in range(arr_shape):         for alpha in range(360):             x, y = rotate_vector(step, alpha)             x = x * i + cx             y = y * i + cy             points[alpha][i] = derivative_x(x, y) + derivative_y(y, x) &gt; 0             if not points[alpha][i - 1] and points[alpha][i]:                 flip_points = np.vstack((flip_points, np.array([alpha, i - 1])))      return flip_points   def pick_estimates(positions):     vx, vy = rotate_vector(step, positions[1][0])     cx, cy = centre     best_x, best_y = cx + vx * positions[1][1], cy + vy * positions[1][1]      for index in range(2, len(positions)):         vx, vy = rotate_vector(step, positions[index][0])         x, y = cx + vx * positions[index][1], cy + vy * positions[index][1]         if differentiable_function(best_x, best_y) &gt; differentiable_function(x, y):             best_x = x             best_y = y      for index in range(360):         vx, vy = rotate_vector(step, index)         x, y = cx + vx * (arr_shape - 1), cy + vy * (arr_shape - 1)         if differentiable_function(best_x, best_y) &gt; differentiable_function(x, y):             best_x = x             best_y = y      return best_x, best_y   def gradient_descent(best_estimates, is_x):     derivative = derivative_x if is_x else derivative_y     best_x, best_y = best_estimates     descent_step = step     value = derivative(best_y, best_x)      while abs(value) &gt; global_epsilon:         descent_step *= 0.95         best_y = best_y - descent_step \\             if derivative(best_y, best_x) &gt; 0 else best_y + descent_step         value = derivative(best_y, best_x)      return best_y, best_x   def find_minimum():     return gradient_descent(gradient_descent(pick_estimates(calculate_flip_points()), False), True)   def get_grid(grid_step):     samples = np.arange(-radius, radius, grid_step)     x, y = np.meshgrid(samples, samples)     return x, y, differentiable_function(x, y)   def draw_chart(point, grid):     point_x, point_y, point_z = point     grid_x, grid_y, grid_z = grid     plot.rcParams.update({         'figure.figsize': (4, 4),         'figure.dpi': 200,         'xtick.labelsize': 4,         'ytick.labelsize': 4     })     ax = plot.figure().add_subplot(111, projection='3d')     ax.scatter(point_x, point_y, point_z, color='red')     ax.plot_surface(grid_x, grid_y, grid_z, rstride=5, cstride=5, alpha=0.7)     plot.show()   if __name__ == '__main__':     min_x, min_y = find_minimum()     minimum = (min_x, min_y, differentiable_function(min_x, min_y))     draw_chart(minimum, get_grid(0.05))<\/code><\/pre>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/547424\/\"> https:\/\/habr.com\/ru\/post\/547424\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-319713","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/319713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=319713"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/319713\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=319713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=319713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=319713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}