朱莉娅:外积函数 [英] julia: outer product function

查看:17
本文介绍了朱莉娅:外积函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R中,函数outer在结构上允许你取两个向量xy 同时为适用于每个组合的实际功能提供多个选项.例如,outer(x,y,'-') 创建了 xy 之间元素差异的外积"矩阵.朱莉娅有类似的吗?

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] 匹配第 ij 行向量的列向量.这意味着 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 ith 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 提供了广播(),它扩展数组参数中的单例维度以匹配另一个数组中的相应维度,而不使用额外的内存

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屋!

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