Error when using the function "transpose" in Fortran [duplicate] - fortran

This question already has an answer here:
Vector multiplication using MATMUL in Fortran
(1 answer)
Closed 5 years ago.
I know that "transpose" in Fortran is an operator which flips a matrix over its diagonal. However, in the code below, I met an error and did not know why.
The code is:
program main
implicit none
real(8)::a(3,2),b(2,1)
a=reshape((/1.0,2.0,3.0,4.0,5.0,6.0/),(/3,2/))
b=reshape((/1.0,2.0/),(/2,1/))
write(*,*)a(1,1:2)
!Next sentence throw an error
a(1,1:2)=transpose(b)
end program
The error is:
error #6366: The shapes of the array expressions do not conform. [A]
I think "a(1,1:2)" is one line and two columns, which is the same as "transpose(b)", why the compiler told me that "the shape do not conform"?

You are wrong, a(1,1:2) is not a 2D array (which you call a matrix), it is a 1D array.
Bu using a(1,.. you are selecting a definite "row" in the "matrix" from which you take a "row vector" 1:2).
You must use
a(1:1,1:2)
for a 2D array of shape 1x2 (matrix with one line and two columns if you want).

Related

Generating and passing real array to procedure with assumed shape [duplicate]

This question already has answers here:
Sum and Construct an Array in one statement
(1 answer)
Implied-do loops in fortran for accumulated-sum-like array
(2 answers)
Is it possible to initialize a Fortran parameter array with a loop expression?
(2 answers)
Closed last year.
I want to pass a real array to a subroutine. The array is a real array with a lower bound of 0.1 and an upper bound of 1.0
real, dimension(10) :: r = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]
The array is passed successfully with the way of explicit shape dummy array. Now I want to change the increment or step size in the array as
real, dimension(10) :: r = [0.10,0.11,0.12,0.13,0.14...,1.0]
Ideally, the increment should be small enough. Therefore, I do not see the option of passing the array explicitly. The other way apparently is using an assumed shape dummy array and I have seen that it would need an explicit interface.
But the original problem remains the same as how to create that type of array before passing to the procedure. I have seen few ways of using the do loop Fortran DO loop, warning to use integer only
, Fortran: do-loop with real-type argument. It seems to me that they are just creating the incrementation and not creating any array.
So how to create an array with 0.1 lower bound, 1.0 upper bound, and increment e.g. 0.01? Or each incremental value should be passed to the procedure by following the mentioned references? What is the way? I hope it could be asked on this forum but somehow I could not figured it out.

Fortran retrieve matrix value outside of the bound without error [duplicate]

This question already has answers here:
Does Fortran have undefined behavior?
(3 answers)
I have a Fortran program that should give segmentation fault but it doesn't
(1 answer)
Closed 1 year ago.
I have a simple example of a code with two matrices, mat and mat2. mat is allocatable, the two matrices are constructed with 2X2 size.
program example
double precision , dimension(:,:), allocatable :: mat
double precision mat2(2,2)
integer nx,ny
nx=2
ny=2
allocate(mat(nx,ny))
mat(1,1)=1.d0
mat(1,2)=2.d0
mat(2,1)=3.d0
mat(2,2)=4.d0
mat2(1,1)=1.d0
mat2(1,2)=2.d0
mat2(2,1)=3.d0
mat2(2,2)=4.d0
write(*,*) mat(4,1)
write(*,*) mat2(4,1)
end program example
When compiling and executing the code using ifort (ifort version 2021.1), no error or warning is shown. The result is
4.00000000000000
4.00000000000000
When compiling and executing the code using gfortran, a warning appears :
main.f90:21.20:
write(*,*) mat2(4,1)
1
Warning: Array reference at (1) is out of bounds (4 > 2) in dimension 1
However, the execution gives the same result. I expected a segmentation fault error. This make debugging a complex program very difficult, since no error appear with this kind of error. Is this a normal behavior?

Removing leading zeros from Fortran string from int [duplicate]

I have been experimenting with the following code:
program hello
write(*,"(i9)") 10
end program hello
and varying the format string, trying to make write output a string just the size needed to represent the integer number, but so far I was unable to manage it. How to write 'fit' integers in Fortran?
A I0 edit descriptor is the correct way for output of integers with the correct width. This was introduced in Fortran 95. All current Fortran compilers which were Fortran 90 compilers have been updated to Fortran 95 years ago.

What is the meaning of 5:8 in an array dimension in Fortran? [duplicate]

This question already has an answer here:
Zero indexed array in Fortran?
(1 answer)
Closed 4 years ago.
I would like to know this example line.
Dimension A(265000, 5:8)
, especially 5:8
I can only know this A is going to be a 2 dimension, but I would like make sure what the 5:8 is.
Is it right to understand like 265000x5, 265000x6, 265000x7, 265000x8?
This is a two dimensional array, and 5:8 means the subscripts for the second dimension have values 5, 6, 7, 8.

FORALL causes stack overflow

I am implementing a genetic algorithm to numerically estimate some coefficients in a system of ODEs based on experimental data. I am just learning Fortran along as I implement the algorithms. My config is a Intel Fortran 2015 running on Windows 7/Visual Studio 2013, on an old i7 processor.
I have the following piece among a multitude of lines of code:
DO i = 1, N_CROMOSOMES
IF (population(9,i) < 0.0_DOUBLE) population(9, i) = square_error(population(1:8, i))
END DO
Where I just defined DOUBLE to be:
INTEGER, PARAMETER :: DOUBLE = 16
N_CROMOSOMES is an INTEGER argument to the function, that defines the size of the array population, which in turn is a (9 x N_CROMOSOMES) array of type REAL(KIND=DOUBLE). For each column on this array, its first 8 elements represent the 8 coefficients that I am estimating, and the ninth element is the error associated with that particular 8 guesses for the coefficients. square_error is the function that determines it.
In this point of the program, I have marked columns that were just created or that were altered as having an error of -1. Hence, the "IF (population(9,i)<0.0_DOUBLE)": I am checking the columns of the array whose error is -1 in order to compute their error.
The thing is, I just rewrote most of my code, and spent the past few days correcting mysterious bugs. Before this, the code worked just fine with a FORALL instead of DO. Now it says gives an error "stack overflow" when I use FORALL, but works with DO. But it takes a lot more time to do its job.
Does anyone knows the cause of this, and also, how to solve it? It is clear to me that my code can highly benefit from paralellization, but I am not so sure how to do it.
Thanks for your time.