在Fortran中使用MATMUL进行矢量乘法 [英] Vector multiplication using MATMUL in Fortran
本文介绍了在Fortran中使用MATMUL进行矢量乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将列向量(n,1)的一部分乘以另一行向量(1,n)的一部分。两部分的长度相同。所以我应该得到一个矩阵(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,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):错误#6241:参数的形状不一致或不一致。 [MATMUL]
pre
y(1:9,1:9)= MATMUL(x(1:9,1),m(1,1:9))
--------- ---- ^
test_cshift.f90(7):错误#6366:数组表达式的形状不一致。 [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
。
也是如此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).
Here is my simple code:
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
I'm Using:
ifort -g -debug -traceback -check all -ftrapuv test_cshift.f90
And I'm getting:
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))
解决方案The problem is that
x(1:9,1)
isn't of shape[9 1]
but[9]
. You need to usex(1:9, 1:1)
. The same goes form
.这篇关于在Fortran中使用MATMUL进行矢量乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文