Fortran 90/95 中的广播数组乘法 [英] Broadcast array multiplication in Fortran 90/95

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

问题描述

我想知道在 Fortran 中是否有更好(简洁)的方式来编码?我试图将 a(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

提前致谢!

推荐答案

表达式

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天全站免登陆