分配给复杂变量时带圆括号的错误 [英] Error with parentheses when assigning to complex variable

查看:204
本文介绍了分配给复杂变量时带圆括号的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  COMPLEX * 16,DIMENSION(4,1):我的Fortran代码中有以下一组命令: :INSTATE_BASISSTATES 
INSTATE_BASISSTATES(:,, :) =(0.0D0,0.0D0)
INSTATE_BASISSTATES(1,1)=((1.0D0 / SQRT(2)),0.0D0)
INSTATE_BASISSTATES (3,1)=((1.0D0 / SQRT(2)),0.0D0)

我在cygwin上使用gfortran运行/编译程序,出现错误。

  INSTATE_BASISSTATES(1,1)=(1.0D0 / DREAL(SQRT(2.0D0)),0.0D0)
1
错误:期望表达式中右括号(1)


INSTATE_BASISSTATES(3,1 )=(1.0D0 / DREAL(SQRT(2.0D0)),0.0D0)
1
错误:期望表达式中右括号(1)

可能是什么问题?是不是我的括号正确?

解决方案

在赋值语句的右侧,您试图使用复杂的文字常量。但是,

 (1.0D0 / DREAL(SQRT(2.0D0)),0.0D0)


$ b

不是这种常量的有效形式。



对于复杂字面上,真实和虚构的组件必须是命名常量或文字常量。 1.0D0 / DREAL(SQRT(2.0D0))既不是那些东西。对于您没有投诉的行,(0.0D0,0.0D0)的两个组件都是文字常量。



正如在这个其他答案中,你可以用想要的值创建一个命名常量并使用它。另外,因为你只是在做一个无聊的任务(它没有适用于初始化的各种限制等),你可以使用 cmplx 内在函数返回一个复合体值

  INSTATE_BASISSTATES(1,1)= CMPLX(1.0D0 / DREAL(SQRT(2.0D0)),0.0D0)

在这里,实部和虚部不需要是常数。你甚至可以注意到
$ b $ pre $ INPUT_BASISSTATES(1,1)= CMPLX(1.0D0 / DREAL(SQRT(2.0D0)))

也是如此:如果未提供虚数分量值,则返回的复数具有虚数分量零。



虽然有一些轻微的复杂性。默认情况下, cmplx 返回一个具有默认实数的复数。要返回匹配 complex * 16 (这不是标准的Fortran,但让我们假设它对应于双精度)的内容。您需要 CMPLX(...,[...],KIND = KIND(0d0))(或 KIND = KIND(INSTATE_BASISSTATES) )




作为一个附注,正如Vladimir F评论 dreal 不是标准的Fortran。您可以使用 dble real 以及合适的种类编号。但是我们也可以看到 sqrt(2d0)已经返回一个双精度实数,所以即使是冗余: 1 / sqrt(2d0) code>具有与原始较为繁琐的表达式相同的(数学)结果。同样如 2d0 **( - 0.5) sqrt(2d0)/ 2



你甚至可以用 $ b

  SQRT((5d-1,0) )

当我们看到 sqrt 也接受一个复杂的参数(在这种情况下是一个复杂的文字常量)。这种形式也避免了 kind = 说明符的尴尬:它的值和参数的类型一样。


I have the following set of commands in my Fortran code:

COMPLEX*16, DIMENSION(4,1) :: INSTATE_BASISSTATES    
INSTATE_BASISSTATES(:,:) = (0.0D0,0.0D0)
INSTATE_BASISSTATES(1,1) = ((1.0D0/SQRT(2)),0.0D0)
INSTATE_BASISSTATES(3,1) = ((1.0D0/SQRT(2)),0.0D0)

When I run/compile the program using gfortran on cygwin, I get the error

INSTATE_BASISSTATES(1,1) = (1.0D0/DREAL(SQRT(2.0D0)),0.0D0)
                                             1
Error: Expected a right parenthesis in expression at (1)


INSTATE_BASISSTATES(3,1) = (1.0D0/DREAL(SQRT(2.0D0)),0.0D0)
                                             1
Error: Expected a right parenthesis in expression at (1)

What could be the issue? Aren't my brackets correct?

解决方案

On the right hand side of the assignment statement you are trying to use a complex literal constant. However,

(1.0D0/DREAL(SQRT(2.0D0)),0.0D0)

isn't a valid form for such a constant.

For a complex literal, the real and imaginary components must be either named constants or literal constants. 1.0D0/DREAL(SQRT(2.0D0)) is neither of those things. For the line where you had no complaint, both components of (0.0D0,0.0D0) are literal constants.

As in this other answer you could make a named constant with the value wanted and use that. Alternatively, as you are just doing a boring assignment (which doesn't have various restrictions which apply to initialization, etc.,) you can use the cmplx intrinsic to return a complex value

INSTATE_BASISSTATES(1,1) = CMPLX(1.0D0/DREAL(SQRT(2.0D0)),0.0D0)

Here the real and imaginary components don't need to be constants. You could even note that

INSTATE_BASISSTATES(1,1) = CMPLX(1.0D0/DREAL(SQRT(2.0D0)))

works just as well: if the imaginary component value isn't provided, the returned complex has imaginary component zero.

There is a slight complication, though. cmplx by default returns a complex number with kind of the default real. To return something matching complex*16 (which isn't standard Fortran, but let's assume it corresponds to double precision) you'll need CMPLX(..., [...], KIND=KIND(0d0)) (or KIND=KIND(INSTATE_BASISSTATES))


As a side note, as Vladimir F comments dreal isn't standard Fortran. You could use dble, or real with a suitable kind number. But we can also see that sqrt(2d0) already returns a double precision real, so even those are redundant: 1/sqrt(2d0) has the same (mathematical) result as the original more cumbersome expression. As do 2d0**(-0.5) and sqrt(2d0)/2.

You could even replace the right hand side with

SQRT((5d-1,0))

as we see that sqrt also accepts a complex argument (in this case a complex literal constant). This form also avoids the awkwardness of the kind= specifier: its value has kind as well as type of the argument.

这篇关于分配给复杂变量时带圆括号的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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