简体   繁体   English

有没有更有效(通用)的方法来在 Python 或 R 中为给定间隔上的函数找到 arg max?

[英]is there a more efficient (general) way to find a arg max for a function on a given interval in Python or R?

in math, arg max f(x) is to find a value of x to maximize the function f(x).在数学中,arg max f(x) 就是找到一个 x 的值来最大化函数 f(x)。

assume f(x) = sin(x), interval [0,𝜋], here is the code.假设 f(x) = sin(x),区间 [0,𝜋],这里是代码。

>>> x = []
>>> for i in range(1001):
...     x.append(i*np.pi/1000)
>>> sine_between_interval = np.sin(np.array(x))
>>> idx_max = np.argmax(sine_between_interval)
>>> idx_max
500

the question is, is there a more efficient (or general) way to find the solution value of a arg max on a given interval in Python or R?问题是,是否有更有效(或通用)的方法来在 Python 或 R 中的给定区间上找到 arg max 的解值?

Here is a solution in R :这是 R 中的解决方案:

optimize(function(x) sin(x), c(0, pi), maximum = TRUE)

$maximum $最大值

[1] 1.570796 [1] 1.570796

$objective $目标

[1] 1 [1] 1

where objective is your argmax目标是你的 argmax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM