将阵列广播到不同的形状(添加“假”尺寸) [英] broadcast an array to different shape (adding "fake" dimensions)

查看:164
本文介绍了将阵列广播到不同的形状(添加“假”尺寸)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中(使用numpy),我可以将数组广播到不同的形状:

 >>> import numpy as np 
>>> a = np.array([2,3,4])
>>> b = np.zeros((3,2))
>>> b [:,:] = np.zeros((3,2))
>>> b [:,:] = a [:,np.newaxis]#<-np.newaxis允许a被广播为与b相同的形状。
>>> b
array([[2.,2.],
[3.,3.],
[4.,4.]])
>>> ; c = np.zeros((2,3))
>>> c [:,:] = a [np.newaxis,:]
>>> c
array([[2.,3.,4.],
[2.,3.,4.]])

有什么方法可以在 fortran 中实现相同效果?我有一个子程序,期望传递一个 2D 数组 - 我想将我的一维数组广播到2D,就像我已经演示的那样以上。由于它看起来很可能很重要,所以我的二维数组确实有一个明确的接口。



作为一个附注,我认为这个 <$ 可能会提供功能

 真实,维度(3):: arr1d 
reshape(arr1d,(/ 3,3 /),order =(/ 1,/ 1))

但在阅读文档后,我认为这不可能,因为 order 似乎需要包含所有数字1到N。

编辑:为了更清楚一点,我正在寻找一种简单的方法来在输入<$ c $上创建几个变换c> a 例如:

案例1

  b(i,j).eq。 a(i)!为所有j,甚至只是j = 1,2 



case 2

  b(j,i ).eq。 a(i)!for all j,or even just j = 1,2 

奖励积分 1 用于任意维度:

  b(i,j,k).eq。 a(i,j)
b(i,k,j).eq。 a(i,j)

等。

< - >

=h2_lin>解决方案

我不确定你要完成的是什么,但这里有几个可能有帮助的片段。

reshape 可以带一个可选的参数,称为 pad ,它可以用来提供所需的'extra'元素你重塑成一个数组,比你开始时的元素多一些,比如从3x4到2x4x2。你可能对 spread 感兴趣。 code>函数,它是为'upranking'数组设计的,即将一个rank-N数组放入并放出一个rank-N + 1数组。你的第二个副本中的片段可以被重写为:

$ p $ array $ array $ d $ spread $(array1d,2,2)
code>

在这个例子中,第二个参数是传播第一个参数以产生输出的维度。第三个参数是要创建的输入数组的副本数量。



PS对 spread 的调用也许应该是 spread(array1d,1,2),我没有检查过它。



编辑回应OP的编辑问题

两种情况下,1和2分别通过分布在维度2和1上而得到满足。在Fortran中

  b = spread(a,2,j)





$ b $

由于 spread 返回一个排名为1的数组大于其第一个参数的等级,它提供了寻求的任意维度。但是,因为显示3级以上级别的数组非常耗费空间,所以我不打算这样做。


In python (using numpy), I can broadcast an array to a different shape:

>>> import numpy as np
>>> a = np.array([2,3,4])
>>> b = np.zeros((3,2))
>>> b[:,:] = np.zeros((3,2))
>>> b[:,:] = a[:,np.newaxis]  #<-- np.newaxis allows `a` to be "broadcasted" to the same shape as b.
>>> b
array([[ 2.,  2.],
       [ 3.,  3.],
       [ 4.,  4.]])
>>> c = np.zeros((2,3))
>>> c[:,:] = a[np.newaxis,:]
>>> c
array([[ 2.,  3.,  4.],
       [ 2.,  3.,  4.]])

Is there any way to achieve the same effect in fortran? I have a subroutine which expects a 2D array to be passed in -- I would like to "broadcast" my 1-D arrays up to 2-D as I've demonstrated above. As it seems that it is likely to matter, my 2D array does have an explicit interface.

As a side note, I thought that this functionality might be provided by the reshape intrinsic, -- Something like:

real,dimension(3) :: arr1d
reshape(arr1d, (/3,3/), order=(/1,/1))

but after reading the docs, I don't think that this is possible since order seems to need to include all the numbers 1 to "N".

Edit: To be a little more clear, I'm looking for a simply way to create a couple of transforms on an input a such that:

case 1

b(i,j) .eq. a(i)  !for all j, or even just j=1,2

and

case 2

b(j,i) .eq. a(i)  !for all j, or even just j=1,2

bonus points1 for arbitrary dimensionality:

b(i,j,k) .eq. a(i,j)
b(i,k,j) .eq. a(i,j)

etc.

1disclaimer -- I don't actually have SO super powers to bestow bonus points upon the answerer ;-)

解决方案

I'm not sure what you are trying to accomplish but here are a couple of fragments which may help.

reshape can take an optional argument, called pad, which can be used to provide the 'extra' elements needed when you reshape into an array with more elements than you started with, say from 3x4 to 2x4x2.

You may also be interested in the spread function which is designed for 'upranking' arrays, that is taking a rank-N array in and putting out a rank-N+1 array. The fragment in your second copy could be rewritten as

array2d = spread(array1d,2,2)

In this example the second argument is the dimension along which to spread the first argument to make the output. The third argument is the number of copies of the input array to make.

PS The call to spread should perhaps be spread(array1d,1,2), I haven't checked it.

EDIT in response to OP's editing of question

The two cases, 1 and 2, are satisfied by spreading across dimensions 2 and 1 respectively. In Fortran

b = spread(a,2,j)

and

b = spread(a,1,j)

Since spread returns an array with rank 1 greater than the rank of its first argument, it provides the sought-for arbitrary dimensionality. However, since it's so space-consuming to show arrays of rank-3 and above I'm not going to.

这篇关于将阵列广播到不同的形状(添加“假”尺寸)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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