简体   繁体   English

恒定匿名函数(先验未知的匿名函数)的输出数量

[英]Number of outputs from constant anonymous function (anonymous function not known a priori)

This question may initially appear similar to this other question but my situation is a little bit different. 这个问题最初看起来可能与此其他问题相似,但我的情况有所不同。

I have a function 'deriv' that takes a symbolic expression as its input, then takes the first derivative of that symbolic expression. 我有一个函数'deriv',它使用一个符号表达式作为输入,然后使用该符号表达式的一阶导数。 That derivative is then converted into an anonymous function using matlabFunction() , and is then evaluated over an array of points. 然后,使用matlabFunction()导数转换为匿名函数,然后对点数组进行求值。 I also use the anonymous function later in some other code. 稍后,我还将在其他一些代码中使用匿名函数。

The problem I'm having is that sometimes the input symbolic expression happens to be linear, and thus the derivative is constant; 我遇到的问题是,有时输入符号表达式恰好是线性的,因此导数是常数。 therefore the anonymous function is also a constant. 因此匿名函数也是一个常量。 When I evaluate the anonymous function over the array of points, I only get one output instead of an array of outputs. 当我对点数组评估匿名函数时,仅获得一个输出而不是输出数组。

Here's some code showing what I'm doing. 这是一些代码,显示我在做什么。 For the sake of simplicity here, let's assume that the symbolic input expressions will involve only one symbolic variable called q . 为了简化起见,让我们假设符号输入表达式仅包含一个称为q符号变量。

function[derivFun,derivVals] = deriv(input)
derivSym = diff(input,q);
derivFun = matlabFunction(derivSym,'vars',q);
evalPoints = [1;2;3;4;5]; %in my true application, a much larger array
derivVals = derivFun(evalPoints);
end

So if the input is q^2 , then the output derivVals will be [2;4;6;8;10]. 因此,如果输入为q^2 ,则输出derivVals将为[2; 4; 6; 8; 10]。 But if the input happens to be, say, 3*q , then derivVals will be 3 (just a single scalar). 但是,如果输入恰好是3*q ,则derivVals将为3(只是一个标量)。 What I'd like is for derivVals to be [3;3;3;3;3]. 我想要的是derivVals为[3; 3; 3; 3; 3]。 That is, I'd like derivVals to be the same size as evalPoints even if the input function happens to be linear (or constant). 也就是说,我想derivVals是大小相同evalPoints即使输入功能恰好是线性的(或恒定)。 And I don't know ahead of time what the input expression will be. 而且我不提前知道输入表达式是什么。

Can anyone give suggestions for a scheme that would do that? 任何人都可以提出建议的方案吗? I understand that a constant anonymous function will just return a single constant scalar, regardless of the size of its input. 我知道常量匿名函数将仅返回单个常量标量,而不管其输入的大小如何。 What I'm hoping for is perhaps some way to recognize when the anonymous function is constant and then still cause derivVals to be the same size as evalPoints . 我希望找到一种方法来识别匿名函数derivVals定,然后仍然使derivValsevalPoints大小相同。

I know that I could use a for loop to evaluate derivFun for every row of evalPoints , but I'd like to avoid using such a loop if possible. 我知道我可以使用for循环来评估derivFun的每一行的evalPoints ,但是我想尽可能避免使用这样的循环。

Thank you for your time and consideration. 感谢您的时间和考虑。

Not 100% sure I got the problem correctly. 不是100%确定我正确地得到了问题。 Would this solve your issue?: 这样可以解决您的问题吗?:

if isscalar(derivVals)
    derivVals = repmat(derivVals, size(evalPoints));
end

I think that this is a slightly simpler solution. 我认为这是一个稍微简单的解决方案。 The issue is that you're using matlabFunction , which simplifies down the equations and doesn't allow much customization. 问题是您使用的是matlabFunction ,它简化了方程式,并且不允许太多自定义。 However, you can create an anonymous function of an anonymous function. 但是,您可以创建匿名函数的匿名函数。 Just add the this line right after your matlabFunction line: 只需在您的matlabFunction行之后添加matlabFunction行:

derivFun = @(evalPoints)derivFun(evalPoints)+zeros(size(evalPoints));

This only evaluates the original derivFun once. 这只会对原始derivFun一次评估。 However, I do like you symvar solution (just remember that adding zeros is always better than multiplying ones ). 但是,我确实喜欢您的symvar解决方案(请记住,添加zeros始终比乘以ones更好)。

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

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