macros - 什么是宏@。来自 Julia,文档在哪里?
问题描述
我似乎找不到这个宏的定义@.
,或者.
它本身。我知道这是一个元素操作。但是,我怎样才能充分利用它对我来说仍然是一个秘密。
例如,JavaScript 具有 foreach(i,e){},您可以在其中使用 (i)ndex 和 (e)element 等。
解决方案
当你不知道如何在 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)
) - 否则很清楚发生了什么!只需在任何地方添加一个点,现在您的代码就被矢量化了。
推荐阅读
- c++ - c++模板参数编译器无法推断
- google-cloud-platform - google cloud datalab和google cloud ai平台笔记本有什么区别?
- python - python中的数字识别(OpenCV和pytesseract)
- entity-framework-core - 在我在 Entity Framework Core 中完成 ThenInclude 之后进行过滤
- r - 使用 ggplot 具有多个方面的交互图
- java - 在 Thymeleaf 中添加表单
- php - 无法打开流:HTTP 请求失败!HTTP/1.1 404
- .net - web.config 中现有响应自动的 httpErrors 的行为
- python - JupyterLab 中的 pdb 未进入交互模式
- c++ - 谁管理参数中复制构造函数抛出的异常?