3

I'm trying to run code for a mathematical algorithm (Conjugate Gradient method). In doing so I input a double precision matrix, defined as such in my preamble. When compiling, I get the follow error:

A=RESHAPE((/ 0,8,0,4,26,8,0,17.5,0,0,0,17.5,0,2.5,-8,4,0,2.5,0,-5,26,0,-8,-5,0 
                        1
Error: Element in INTEGER(4) array constructor at (1) is REAL(4)
make: FTranProjectBuilder: Error: Execution exited with code 2 
*** [cg_main.o] Error 1

My definition in the program with the matrix being defined is given as such (the array definition is the first operation of my program):

PROGRAM cg_main 
IMPLICIT NONE 

INTEGER,PARAMETER                     ::d=5 !use a parameter for the dimensions (simple)
DOUBLE PRECISION,DIMENSION(d,d)       ::A !matrix
INTEGER,DIMENSION(2)                  ::order2 = (/ 2, 1 /) !matrix reshape order

[MORE DECLARATIONS HERE]

A=RESHAPE((/ 0,8,0,4,26,8,0,17.5,0,0,0,17.5,0,2.5,-8,4,0,2.5,0,-5,26,0,-8,-5,0 /),(/d,d/), order2) !specify dxd matrix

[MORE CODE HERE]

END PROGRAM 

The code works without the decimal numbers in my matrix input, but doesn't seem to with my decimals and I have no idea why.

1 Answer 1

2

All elements in an array constructor of the form of the question must be of the same declared type and same kind type parameters.  The compiler here complains because this constraint has been violated by mixing a real expression in with those integer expressions.

Simply making the first element a real/double precision element is not sufficient.  Perhaps the compiler is using the error message about an "integer(4) array constructor" as short hand for "the first element is integer(4) but not all other elements are".

To resolve this, with this form of the array constructor you should make all elements of the same type/kind (probably real/double precision).

There is also another form for array constructors:

[ real ::0,8,0,4,26,8,0,17.5,0,0,0,17.5 ... ]

which converts all elements to the type specified.  You can even use this form to create a size-zero array:

 [ real :: ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.