首页 > 解决方案 > 什么是宏@。来自 Julia,文档在哪里?

问题描述

我似乎找不到这个宏的定义@.,或者.它本身。我知道这是一个元素操作。但是,我怎样才能充分利用它对我来说仍然是一个秘密。

例如,JavaScript 具有 foreach(i,e){},您可以在其中使用 (i)ndex 和 (e)element 等。

标签: macrosjuliadocumentation

解决方案


当你不知道如何在 Julia 中做某事时,第一步是键入:?,然后是你的命令。在这种情况下,您会得到:

help?> @.
  @. expr

  Convert every function call or operator in expr into a "dot call" (e.g. convert f(x) to f.(x)), and convert every assignment in expr to a "dot assignment" (e.g. convert += to .+=).

  If you want to avoid adding dots for selected function calls in expr, splice those function calls in with $. For example, @. sqrt(abs($sort(x))) is equivalent to sqrt.(abs.(sort(x))) (no dot for sort).

  (@. is equivalent to a call to @__dot__.)

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> x = 1.0:3.0; y = similar(x);

  julia> @. y = x + 3 * sin(x)
  3-element Vector{Float64}:
   3.5244129544236893
   4.727892280477045
   3.4233600241796016

由于这是一个宏,因此有时使用以下命令更容易理解@macroexpand

julia> @macroexpand  @. y = x + 3 * sin(x)
:(y .= (+).(x, (*).(3, sin.(x))))

虽然这是使用运算符的功能表示(波兰符号)(即a+b写为+(a,b)) - 否则很清楚发生了什么!只需在任何地方添加一个点,现在您的代码就被矢量化了。


推荐阅读