用Fortran 90/95广播阵列乘法 [英] Broadcast array multiplication in Fortran 90/95

查看:217
本文介绍了用Fortran 90/95广播阵列乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这将有Fortran中是一个更好的(简洁)的方式来code呢?我想乘的每一列(3,3) B所的每个值(3)。我知道在Python有 np.multiply ,并且不知道Fortran语言。

I was wondering that would there be a better (succinct) way to code this in Fortran? I am trying to multiply each column of a(3, 3) by each value in b(3). I know in Python there is np.multiply, and not sure about Fortran.

!!! test.f90
program test
    implicit none
    integer, parameter :: dp=kind(0.d0)
    real(dp) :: a(3, 3)=reshape([1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3]),&
        b(3)=[1, 2, 3]
    integer :: i
    do i = 1, 3
        a(:, i) = a(:, i) * b(i)
    end do
    write(*, *) a
end program test

在此先感谢!

推荐答案

这位前pression

The expression

a * SPREAD(b,1,3)

会产生相同的结果作为循环。我将让你和别人去判断这是否是更简洁或以任何方式比循环更好。

will produce the same result as your loop. I'll leave it to you and to others to judge whether this is more succinct or in any way better than the loop.

这篇关于用Fortran 90/95广播阵列乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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