我试图证明 there 某个函数不能为负。由于我没有设法证明,也没有说服自己这是真的,所以我对函数进行了如下编码:
test = function(s,t){
# s is a vector of positives reals of length d
# t is a vector of complexes of length d-1
d = length(s)
t = c(t, (1-sum(t*s[1:(d-1)]))/s[d])
modulii = abs((t+1)/(t-1))
return(max(modulii)-1)
}
# I want to minimize this test function over all
# s positive reals of length d
# t complex of length d-1.
# How can i do that ?
# simple starting points:
d = 3
s = runif(d)
t = complex(real = runif(d-1),imaginary = runif(d-1))
test(s,t) # should be positive.
我如何编写一个优化程序来最小化这个函数:
s[1],...s[d]
所有非负实数,其中 s[d]
严格为正。t[1],...,t[d-1]
所有复数值。?
我在处理 optim
和复数方面很挣扎。我想确保最小值不能为负 ;)
答案 0 :(得分:3)
定义一个函数 proj,它采用长度为 3*d-2 的向量,并从中生成一个包含 s 和 t 的列表,将前 d 个元素平方形成 s 并取下一个 d-1 和后面的 d-1 个元素作为t 的实部和虚部。然后定义 f 运行 proj 并将结果传递给 test 和 run。
d <- 3
proj <- function(x) {
d <- (length(x) + 2) / 3
list(s = head(x, d)^2, t = complex(x[seq(d+1, length = d-1)], tail(x, d-1)))
}
f <- function(x) with(proj(x), test(s, t))
result <- optim(rep(0.5, 3*d-2), f)
result
## $par
## [1] 1.0863555573 5.9011341467 -0.0009866435 -0.1252050359 1.0720624611
## [6] -0.3826544395 -6.2322265938
##
## $value
## [1] 8.911303e-09
##
## $counts
## function gradient
## 188 NA
##
## $convergence
## [1] 0
##
## $message
## NULL
proj(result$par)
## $s
## [1] 1.180168e+00 3.482338e+01 9.734655e-07
##
## $t
## [1] -0.3826544+0i -6.2322266+0i