Fortran Coder
标题: Fortran对矩阵的赋值报错于输出 [打印本页]
作者: sqs 时间: 2022-3-7 14:18
标题: Fortran对矩阵的赋值报错于输出
本帖最后由 sqs 于 2022-3-7 14:19 编辑
对于6*6或者更高阶的矩阵(二维),怎么简单的对里面的每个元素进行赋值呢(包括我的矩阵当中存在较多的元素为零)?
另外这个报错是为什么,怎么修改呢?
另外用print输出矩阵时,如何分行呢?尤其我矩阵当中的元素还是复数,write输出矩阵的格式是什么呢?
[Fortran] 纯文本查看 复制代码
Program workfile
Implicit none
integer , parameter :: n = 1000
real , parameter :: t1 = 3.12 , t2 = 0.29 , t3 = -0.0103
complex , parameter :: i = (0, 1) , t0 = (0,0)
real , parameter :: PI = acos(-1.0) , a0 = 1.42e-10
real :: b1(2), b2(2) , a1(2) , a2(2) , m1 , m2
complex H(3, 2)
real , allocatable :: k(:,:,:)
complex :: f1 , f2
integer :: x , y
allocate(k(2,n-1,n-1))
b1 = [ 2*PI/a0*sqrt(3.0) , 2*PI/a0 ]
b2 = [ 2*PI/a0*sqrt(3.0) , -2*PI/a0 ]
a1 = [ a0*sqrt(3.0)/2 , a0/2 ]
a2 = [ a0*sqrt(3.0)/2 , -a0/2 ]
Forall(x=1:size(k,2), y=1:size(k,3))
k(:,x,y) = (b1*x+b2*y)/n
End Forall
m1 = dot_product(a1 , k(:,1,2))
m2 = dot_product(a2 , k(:,1,2))
f1 = 1+exp(i*m1)+exp(i*m2)
f2 = 1+exp(-i*m1)+exp(-i*m2)
H = reshape((/t0, t1*f1, t0, t0, t0, t0,&
t1*f2, t0, t0, t2, t0, t3,&
t0, t0, t0, t1*f2, t0 ,t0,&
t0, t2, t1*f1,t0, t0, t2,&
t0, t0, t0, t0, t0, t1*f1,&
t0, t3, t0, t0, t1*f2, t0/),(/6 , 6/)
print * , H(:,:)
End Program workfile
报错如下:[Fortran] 纯文本查看 复制代码
work.f90:25:30:
25 | t1*f2, t0, t0, t2, t0, t3,&
| 1
Error: Element in COMPLEX(4) array constructor at (1) is REAL(4)
file:///C:\Users\Lenovo\AppData\Roaming\Tencent\Users\2597505881\QQ\WinTemp\RichOle\]U}@9S9_7O64~BG4H02K8VH.png
作者: vvt 时间: 2022-3-7 15:30
[Fortran] 纯文本查看 复制代码
Program workfile
Implicit none
integer , parameter :: n = 1000
real , parameter :: t1 = 3.12
complex , parameter :: i = (0, 1) , t0 = (0,0), t2 = 0.29 , t3 = -0.0103
real , parameter :: PI = acos(-1.0) , a0 = 1.42e-10
real :: b1(2), b2(2) , a1(2) , a2(2) , m1 , m2
complex H(6, 6)
real , allocatable :: k(:,:,:)
complex :: f1 , f2
integer :: x , y
allocate(k(2,n-1,n-1))
b1 = [ 2*PI/a0*sqrt(3.0) , 2*PI/a0 ]
b2 = [ 2*PI/a0*sqrt(3.0) , -2*PI/a0 ]
a1 = [ a0*sqrt(3.0)/2 , a0/2 ]
a2 = [ a0*sqrt(3.0)/2 , -a0/2 ]
Forall(x=1:size(k,2), y=1:size(k,3))
k(:,x,y) = (b1*x+b2*y)/n
End Forall
m1 = dot_product(a1 , k(:,1,2))
m2 = dot_product(a2 , k(:,1,2))
f1 = 1+exp(i*m1)+exp(i*m2)
f2 = 1+exp(-i*m1)+exp(-i*m2)
H = reshape([t0, t1*f1, t0, t0, t0, t0,&
t1*f2, t0, t0, t2, t0, t3,&
t0, t0, t0, t1*f2, t0 ,t0,&
t0, t2, t1*f1,t0, t0, t2,&
t0, t0, t0, t0, t0, t1*f1,&
t0, t3, t0, t0, t1*f2, t0],shape(H))
write(*,"(*(6('(',f8.5,',',f8.5,')',1x),/) )") H(:,:)
End Program workfile
作者: Transpose 时间: 2022-3-7 15:32
[Fortran] 纯文本查看 复制代码
Program workfile
Implicit none
integer , parameter :: n = 1000
real , parameter :: t1 = 3.12 , t2 = 0.29 , t3 = -0.0103
complex , parameter :: i = (0, 1) , t0 = (0,0)
real , parameter :: PI = acos(-1.0) , a0 = 1.42e-10
real :: b1(2), b2(2) , a1(2) , a2(2) , m1 , m2
complex H(6, 6)
real , allocatable :: k(:,:,:)
complex :: f1 , f2
integer :: x , y
integer::i1
allocate(k(2,n-1,n-1))
b1 = [ 2*PI/a0*sqrt(3.0) , 2*PI/a0 ]
b2 = [ 2*PI/a0*sqrt(3.0) , -2*PI/a0 ]
a1 = [ a0*sqrt(3.0)/2 , a0/2 ]
a2 = [ a0*sqrt(3.0)/2 , -a0/2 ]
Forall(x=1:size(k,2), y=1:size(k,3))
k(:,x,y) = (b1*x+b2*y)/n
End Forall
m1 = dot_product(a1 , k(:,1,2))
m2 = dot_product(a2 , k(:,1,2))
f1 = 1+exp(i*m1)+exp(i*m2)
f2 = 1+exp(-i*m1)+exp(-i*m2)
H = reshape([complex::&
t0, t1*f1, t0, t0, t0, t0,&
t1*f2, t0, t0, t2, t0, t3,&
t0, t0, t0, t1*f2, t0 ,t0,&
t0, t2, t1*f1,t0, t0, t2, &
t0, t0, t0, t0, t0, t1*f1,&
t0, t3, t0, t0, t1*f2, t0],[6 , 6])
do i1=1,6
write(*,*)H(i1,:)
end do
End Program workfile
数组构造器里面的数据类型要求相同,t2,t3都是real类型的,所以没法再一起使用,可以给构造器加上 `[complex::]`修饰,这样类型会自动转化为complex类型
输出分行的话,建议用循环,Fortran没有内置复数的描述符。
作者: sqs 时间: 2022-3-7 18:46
好的,感谢!
欢迎光临 Fortran Coder (http://bbs.fcode.cn/) |
Powered by Discuz! X3.2 |