Pha*_*ung 3 max built-in julia
我目前正在学习 Julia 中的内置函数,特别是max
和maximum
,并且我正在寻求澄清它们的差异。
julia> max(1, 2, 3, 4)\n4\njulia> maximum([1, 2, 3, 4])\n4\n\nhelp?> max\nsearch: max maximum maximum! maxintfloat argmax typemax findmax findmax! Cintmax_t floatmax Cuintmax_t Matrix minmax BitMatrix macroexpand @macroexpand\n\n max(x, y, ...)\n\n\n Return the maximum of the arguments (with respect to isless). See also the maximum function to take the maximum element from a collection.\n\n Examples\n \xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\xe2\x89\xa1\n\n julia> max(2, 5, 1)\n 5\n
Run Code Online (Sandbox Code Playgroud)\n我认为这两个功能没有区别。似乎比它maximum()
有更多的功能max()
并且可以做所有的事情max()
。
为什么它们会分成两个不同的函数?为什么两者同时存在?您能给我一些这两个函数在不同上下文中工作的例子吗?
\n您在帖子中包含的文档清楚地表明:
另请参阅从集合中获取最大元素的最大值函数。
max()
对独立参数列表进行操作,而maximum()
对作为集合的单个参数进行操作。
当然,在您的琐碎且不切实际的示例中,这没有什么区别,因为您从文字值创建了集合作为文字值的临时实例。在更实际的应用程序中,您将使用适合您正在处理的输入的任何内容,这可能是也可能不是集合。虽然您可以以相同的方式实例化自变量的临时集合,但这将是不必要的处理开销。
在仅包含文字值的示例中,其中一个值的使用可能不清楚。但这是一个不切实际的例子。如果您有许多自变量,那么将它们整理到一个集合中只是为了确定最大值是低效的。
x = 0
y = 10
z = 5
max( x, y, z ) # maximum of x, y, z
maximum( [x,y,z] ) # Create a temporary collection from x,y,x and return maximum
a = [x,y,z]
maximum( a ) # Maximum of an existing collection
max( a ) # ERROR: a is not a collection.
Run Code Online (Sandbox Code Playgroud)
互动式: https: //julialang.org/learning/tryjulia/:
x = 0
y = 10
z = 5
max( x, y, z ) # maximum of x, y, z
maximum( [x,y,z] ) # Create a temporary collection from x,y,x and return maximum
a = [x,y,z]
maximum( a ) # Maximum of an existing collection
max( a ) # ERROR: a is not a collection.
Run Code Online (Sandbox Code Playgroud)
因此,您使用适合您所拥有的数据类型的函数,而您所拥有的数据类型取决于您在更大程序的更广泛上下文中所做的事情。如果迭代处理大量数据,或者处理在运行时“收集”的数据,并且可能随着时间的推移添加或删除数据,则您不太可能使用自变量。
归档时间: |
|
查看次数: |
1329 次 |
最近记录: |