使用可选值定义一个函数,默认情况下该函数是 mathematica 中函数的另一个参数的函数 [英] Defining a function with an optional value that is by default a function of another paramether of the function in mathematica

查看:21
本文介绍了使用可选值定义一个函数,默认情况下该函数是 mathematica 中函数的另一个参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试定义一个接受矩阵的函数,当其维度未作为输入提供时,在可选参数 d

I am trying to define a function that takes in a Matrix and when its dimensions are not provided as input, compute these dimensions in the optional parameter d

这不起作用,但给了你一个想法(选项参数需要是常量):

This does not work but gives you the idea (The options parameter need be constants):

Options[DimM] = {d -> Dimensions[A]};
DimM[A_?MatrixQ, OptionsPattern[]] := OptionValue@d;

实际上最简单的方法是输入一个不可能的值,然后在函数 def 中放入一个 if 条件

Indeed the simple way is to input an impossible value and in the function def put an if condition as in

Options[DimM] = {d -> 0};
DimM[A_?MatrixQ, OptionsPattern[]] :=If[OptionValue@d==0,Dimensions[A],OptionValue@d]

我怎样才能最有效地完成这项工作?

How can I accomplish this most efficiently?

推荐答案

对于您的原始表述,@Wreach 给出了很好的答案.但是,稍微重新考虑一下您的设计可能是有意义的:请注意,在任何情况下,d 都有一个(取决于输入参数)值.可选参数正是为此而设计的 - 可选.在您的情况下,默认参数似乎更合适.您可以使用 Automatic 进行设置,类似于 @WReach 建议的内容:

For your original formulation, @WReach gave a fine answer. However, it may make sense to reconsider your design a bit: note that you have a (dependent on input arguments) value for d in any case. Optional arguments are designed exactly for that - to be optional. In your case, a default argument seems more appropriate. You can set it up with Automatic, similarly to what @WReach suggested:

dimMAuto[a_?MatrixQ, d_: Automatic] :=
     If[d === Automatic, Dimensions[a], d];

要在代码中的多个地方使用它,您需要引入一个辅助变量或常量(使用 WithModule),以存储它价值.作为替代,您也可以使用以下代码:

To use this in more than one place in your code, you will however need to introduce an auxiliary variable or constant (using With or Module), to store this value. As an alternative, you can also use the following code:

Module[{dims},
  dimM[a_?MatrixQ, d_: dims] :=
      Block[{dims = Dimensions[a]}, 
          d]
] 

的优点是您可以在函数体的任何地方使用相同的原始参数 d.这里发生的事情相当重要:Module 用于生成唯一符号,然后将其作为 d 的默认值给出,并用于动态计算维度.请注意,Block 本地化的不是符号 dims,而是像 Module 生成的 dims$77542 这样的唯一符号.ModuleBlock 的这种组合使这种技术完全安全.使用示例:

which has the advantage that you can use the same original parameter d everywhere in the body of your function. What happens here is rather non-trivial: Module is used to generate a unique symbol, which is then given as a default for d and used to dynamically compute the dimensions. Note that Block localizes not the symbol dims, but the unique symbol like dims$77542 produced by Module. This combination of Module and Block makes this technique completely safe. Examples of use:

In[1466]:= dimM[IdentityMatrix[3],{1,1}]
Out[1466]= {1,1}

In[1467]:= dimM[IdentityMatrix[3]]
Out[1467]= {3,3}  

我认为 ModuleBlock 的这种组合是一种有趣的技术,可以找到其他用途.本质上,它是通过词法作用域(或者更准确地说,它在 Mathematica 中的模仿)使动态作用域变得安全的一个版本 - 因为动态作用域的主要危险之一是具有相同名称的动态本地化符号的无意冲突.

I think this combination of Module and Block is an interesting technique which may find other uses. Essentially, it is a version of dynamic scoping made safe by lexical scoping (or, more precisely, its imitation in Mathematica) - since one of the main dangers of dynamic scoping is inadvertent collisions of dynamically localized symbols with the same name.

关于一个不相关的问题 - 最好不要以大写字母开头变量和函数,因为它们可能会与系统符号发生冲突.

On an unrelated matter - it is best to not start your variables and functions with a capital letter, since they may collide with the system symbols.

这篇关于使用可选值定义一个函数,默认情况下该函数是 mathematica 中函数的另一个参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆