Fortran Coder

查看: 3358|回复: 3
打印 上一主题 下一主题

[求助] Fortran对矩阵的赋值报错于输出

[复制链接]

47

帖子

15

主题

0

精华

专家

F 币
311 元
贡献
158 点
跳转到指定楼层
楼主
发表于 2022-3-7 14:18:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 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


分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

954

帖子

0

主题

0

精华

大师

F 币
184 元
贡献
75 点

规矩勋章元老勋章新人勋章水王勋章热心勋章

QQ
沙发
发表于 2022-3-7 15:30:20 | 只看该作者
[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

160

帖子

2

主题

1

精华

大师

Vim

F 币
965 元
贡献
470 点

规矩勋章

板凳
发表于 2022-3-7 15:32:07 | 只看该作者
[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没有内置复数的描述符。

47

帖子

15

主题

0

精华

专家

F 币
311 元
贡献
158 点
地板
 楼主| 发表于 2022-3-7 18:46:48 | 只看该作者
Transpose 发表于 2022-3-7 15:32
[mw_shl_code=fortran,true]Program workfile
    Implicit none
    integer , parameter :: n = 1000

好的,感谢!
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-6-1 12:20

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表