朱莉娅:外部产品功能 [英] julia: outer product function
问题描述
在R
中,函数outer
从结构上允许您获取两个向量x
和y
的外积,同时为应用于每种组合的实际函数提供了多个选项.例如,outer(x,y,'-')
创建一个由x
和y
之间的元素差异构成的外部乘积"矩阵.朱莉娅有类似的东西吗?
In R
, the function outer
structurally allows you to take the outer product of two vectors x
and y
while providing a number of options for the actual function applied to each combination. For example outer(x,y,'-')
creates an "outer product" matrix of the elementwise differences between x
and y
. Does Julia have something similar?
推荐答案
广播是在添加.
周围时发生的Julia操作.当两个容器的大小相同时,这是一个元素操作.示例:如果size(x)==size(y)
,则x.*y
是元素方式的.但是,当形状不匹配时,广播就会真正生效.如果其中之一是行向量,其中之一是列向量,则输出为2D,其中out[i,j]
与列向量的i
行与j
行向量匹配.这意味着x .* y
是一种写外部乘积的特殊方法,如果一个行是行向量而另一个是列向量.
Broadcast is the Julia operation which occurs when adding .
's around. When the two containers have the same size, it's an element-wise operation. Example: x.*y
is element-wise if size(x)==size(y)
. However, when the shapes don't match, then broadcast really comes into effect. If one of them is a row vector and one of them is a column vector, then the output will be 2D with out[i,j]
matching the i
th row of the column vector with the j
row vector. This means x .* y
is a peculiar way to write the outer product if one a row and the other is a column vector.
通常,广播正在做什么:
In general, what broadcast is doing is:
这在尺寸变大时很浪费,因此Julia提供了broadcast(),它在不使用额外内存的情况下扩展了数组参数中的单例尺寸以匹配另一个数组中的相应尺寸
This is wasteful when dimensions get large, so Julia offers broadcast(), which expands singleton dimensions in array arguments to match the corresponding dimension in the other array without using extra memory
(这是 Julia手册)
但是这会推广到所有其他二进制运算符,因此x .- y'
是您要寻找的.
But this generalizes to all of the other binary operators, so x .- y'
is what you're looking for.
这篇关于朱莉娅:外部产品功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!