给定函数的最大值和极小值是函数在给定范围内或在函数的整个域中的最大和最小值。
挑战是使用任何你喜欢的方法来寻找给定多项式函数的局部最大值和极小值。别担心,我会尽我最大的努力来解释这个挑战,让它尽可能简单。
输入将包含所有的系数的单变量多项式,无论是递减或增加的幂阶(取决于你)。例如,
[3,-7,1]将代表3x^2 - 7x + 1[4,0,0,-3]将代表4x^3 - 3现在,假设我们的输入是[1,-12,45,8],它只不过是函数x^3 - 12x^2 + 45x + 8。
x^n的导数是n\times x^{n-1}。与x^n一起存在的任何常数项都是简单地乘以。另外,如果有加/减项,则它们的导数也分别加减。记住,任何常数值的导数都是零。以下是几个例子:
让我们以前面提到的例子为例,即[1,-12,45,8]。
[1,-12,45,8][62,58]而且,你也不会被迫用导数方法来做。你可以用任何你想用的方法。
[2,-8,0] -> (-8)
[2,3,-36,10] -> (91,-34)
[1,-8,22,-24,8] -> (-1,0,-1)
[1,0,0] -> (0)这是密码-高尔夫,所以最短的代码获胜。
发布于 2018-01-31 16:16:29
ASŒRḅ@Ðḟ
J’U×µṖÇḅ@€³ASŒRḅ@Ðḟ Helper Function; find all integer solutions to a polynomial
All integer roots are within the symmetric range of the sum of the absolute values of the coefficients
A Absolute Value (Of Each)
S Sum
ŒR Symmetric Range; `n -> [-n, n]`
Ðḟ Filter; keep elements where the result is falsy for:
ḅ@ Base conversion, which acts like the application of the polynomial
J’U×µṖÇḅ@€³ Main Link
J Range of length
’ Lowered
U Reversed
× Multiplied with the original list (last value is 0)
µ Begin new monadic chain
Ṗ Pop; all but the last element
Ç Apply last link (get all integer solutions of the derivative)
ḅ@€³ Base conversion of the polynomial into each of the solutions; apply polynomial to each solution of the derivative.发布于 2018-01-31 17:59:41
Polynomials包),57字节using Polynomials
x->map(Poly(x),roots(polyder(Poly(x))))取系数的递增顺序,即第一个输入是常数项。
示例运行:
julia> using Polynomials
julia> g = x -> map(Poly(x), roots(polyder(Poly(x))))
(::#1) (generic function with 1 method)
julia> g([8,45,-12,1])
2-element Array{Float64,1}:
58.0
62.0发布于 2018-01-31 18:39:43
https://codegolf.stackexchange.com/questions/154482
复制相似问题