1

I am trying to write a function that takes in a rank 1 matrix (a row vector) and outputs a column vector as the inbuilt TRANSPOSE() does not accept rank 1 matrices. When I call the following function using a row vector A = [1 2 3], I get:

forrtl: severe (40): recursive I/O operation, unit -1, file unknown

in the program's output window.

! Below is the code in the main program that ends up calling rowToColumnVec

REAL, DIMENSION(1:3,1) :: A, B, C ! Define 3 column vectors
INTEGER                :: length

length = SIZE(A, DIM = 1)
CALL oneDimInput(A, length)
CALL oneDimInput(B, length)

WRITE(*,*) "A = ", A 
READ(*,*) 

WRITE(*,*) "A transpose is ", rowToColumnVec(A)
READ(*,*)

! Below is code that exists in a separate .f90 file that is used by the main 
! program. I know that the main program has no problem running other 
! functions and subroutines from this .f90 file.

    FUNCTION rowToColumnVec(A)
        IMPLICIT NONE

        REAL, DIMENSION(1,1:3), INTENT(IN) :: A
        REAL, DIMENSION(1:3,1)             :: B
        REAL, DIMENSION(1:3,1)             :: rowToColumnVec

        B(1,1) = A(1,1)
        B(2,1) = A(1,2)
        B(3,1) = A(1,3)

        rowToColumnVec(1,1) = B(1,1)
        rowToColumnVec(2,1) = B(2,1)
        rowToColumnVec(3,1) = B(3,1)

        WRITE(*,*) "Shape B: ", SHAPE(B)
        READ(*,*) 

    END FUNCTION rowToColumnVec

What is going wrong?

Also, is there a different built in function for Rank 1 matrix transposition?

Edit: added code in main program that calls the function.

  • 1
    If you have `write(*,*) rowToColumnVec(x)` you indeed expect so-called recursive I/O. See [this answer](https://stackoverflow.com/a/24924196) for how to handle things. – francescalus Jun 07 '17 at 16:37
  • I think that did it @fracescalus – Unique Worldline Jun 07 '17 at 16:46
  • @High Performance Mark, can you elaborate why fortran doesn't like to differentiate between row and column vectors? I essentially need all the matrix manipulation abilities of MATLAB (addition, subtraction, multiplication, transposition, dot product, cross product, ...) but in Fortran, and I need to be able to perform those operations on row / column vectors. Since mulitplication of [1 2 3] * [1; 2; 3] vs. [1; 2; 3] * [1 2 3] are very different, how can I handle this in Fortran? – Unique Worldline Jun 07 '17 at 16:50
  • @HighPerformanceMark, where does your distaste come from? Since, in matrix algebra, diadic products and dot products are very different, a row and column distinction seems like it should be implemented in a helpful way. I just started learning FORTRAN two days ago, so I don't really know a lot, but such a distinction seems logical. – Unique Worldline Jun 07 '17 at 17:27
  • 1
    @UniqueWorldline FWIW, there is a builtin "transpose()" function (which seems to do the same thing as rowToColumnVec()). If you make column and row vectors with rank-2 arrays, I guess matmul( v, transpose(v) ) etc works as expected. On the other hand, if those vectors are given as rank-1 arrays, there is no distinction between "column" and "row" vectors (i.e., they are simply 1-d arrays), and matmul( A, u ) and matmul( u, A ) treat 'u' properly depending on the context (column and row, respectively). – roygvib Jun 08 '17 at 01:59
  • 1
    @UniqueWorldline It is not helpful to think about Fortran arrays as vectors and matrices. Vectors and matrices are linear algebra entities with precise meanings. Fortran arrays are just structured collections of values, nothing more. 1D Fortran arrays are NOT vectors, they are just numbers with indexes `(lbound:ubound)`. You can use them as matrices and vectors in `matmul()`, `dot_product()` and similar functions but there is no row or column distinction. – Vladimir F Героям слава Jun 08 '17 at 12:15
  • @roygvib and Vladimir F, thank you both for your comments. As I continue to figure out how much matix algebra functionality exists in the intrinsic libraries, I will keep these things in mind. – Unique Worldline Jun 08 '17 at 18:23

0 Answers0