在 Fortran 中使用 MATMUL 进行向量乘法 [英] Vector multiplication using MATMUL in Fortran

查看:96
本文介绍了在 Fortran 中使用 MATMUL 进行向量乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将列向量 (n,1) 的一部分乘以另一个行向量 (1,n) 的一部分.两个部分的长度相同.所以我应该得到一个矩阵 (n,n).

I am trying to multiply part of a column vector (n,1) by a part of another row vector (1,n). Both parts have the same length. So I should get a matrix (n,n).

这是我的简单代码:

PROGRAM test_pack_1
REAL :: m(1,10), x(10,1), y(10,10)

m = reshape( (/ 1, -1, 3, 2, 1, 2, -2, -2, 1, 0 /), (/ 1, 10 /))
x = reshape( (/ 1, 0, 1, 1, 0, 1, 1, 0, 1, 0 /), (/ 10, 1 /))

y(1:9,1:9) = MATMUL(x(1:9,1),m(1,1:9))


DO j = 1,10
PRINT* ;WRITE(*,*) y(:,j)
ENDDO
print *

END PROGRAM

我正在使用:

ifort -g -debug -traceback -check all -ftrapuv test_cshift.f90

我得到:

test_cshift.f90(7): error #6241: The shapes of the arguments are inconsistent or nonconformable.   [MATMUL]
y(1:9,1:9) = MATMUL(x(1:9,1),m(1,1:9))
-------------^
test_cshift.f90(7): error #6366: The shapes of the array expressions do not conform.   [Y]
y(1:9,1:9) = MATMUL(x(1:9,1),m(1,1:9))

推荐答案

问题是 x(1:9,1) 不是形状 [9 1] 但<代码>[9].您需要使用 x(1:9, 1:1).m 也是如此.

The problem is that x(1:9,1) isn't of shape [9 1] but [9]. You need to use x(1:9, 1:1). The same goes for m.

这篇关于在 Fortran 中使用 MATMUL 进行向量乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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