将其他参数传递给内置函数

编程入门 行业动态 更新时间:2024-10-27 12:24:10
本文介绍了将其他参数传递给内置函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在朱莉娅(Julia)上还很新,有一个问题可能看起来很简单.假设我有一个函数,我将其命名为test(x::Vector, arg1, arg2),其中x是变量的向量,并且该函数具有两个参数arg1& arg2.

I'm pretty new on Julia and have a question that may appear simple. Say I have a function, which I will name test(x::Vector, arg1, arg2) where x is a vector of variables and the function has two arguments arg1 & arg2.

我想针对向量x优化(最小化)函数test.我无法弄清楚如何使用Optim包中的optimize函数来接受两个参数值.在R中,可以执行以下操作:

I would like to optimize (minimize) the function test with respect to the vector x. I can't figure out how to use the optimize function from the Optim package that accepts two arguments values. In R, one may do as following:

optim(initial guest, test, arg1=value1,arg2=value2)

在Julia中是否有类似的方法来声明参数值?

Is there a similar way to declare argument value in Julia?

推荐答案

您可以定义另一个函数来修复这些参数的值.

You can define another function that fixes the value of those arguments.

# Function to minimize f(x::Vector, a, b) = (x[1] - a)^2 + (x[2] - b)^2 using Optim g(x::Vector) = f(x, 3, 4) optimize(g, [0.,0.])

您还可以使用匿名函数(但效率可能较低).

You could also use an anonymous function (but it may be less efficient).

optimize(x -> f(x,3,4), [0.,0.])

更多推荐

将其他参数传递给内置函数

本文发布于:2023-11-23 10:40:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621142.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!