fortran

Fortran element wise multiplication of a matrix and a vector

匆匆过客 提交于 2021-02-07 21:07:30
问题 Is there a simple and quick way to multiply a column of a matrix with element of a vector. We can do this explicitly, program test integer :: x(3,3), y(3), z(3,3) x = reshape([(i,i=1,9)],[3,3]) y = [1,2,3] do i=1,3 z(:,i) = x(:,i) * y(i) print *, z(:,i) enddo end program test Is there a way to perform the do loop in one line. For example in Numpy python we can do this to do the job in one shot z = np.einsum('ij,i->ij',x,y) #or z = x*y[:,None] 回答1: Try z = x * spread(y,1,3) and if that doesn't

checking for self-assignment in fortran overloaded assignment

喜夏-厌秋 提交于 2021-02-07 21:04:10
问题 I am trying to implement a polynomial class with fortran 2003, with overloaded arithmetic operations and assignments. The derived type maintains allocatable list of term definitions and coefficients, like this type polynomial private type(monomial),dimension(:),allocatable :: term double precision,dimension(:),allocatable :: coef integer :: nterms=0 contains ... end type polynomial interface assignment(=) module procedure :: polynomial_assignment end interface ... contains elemental

Fortran element wise multiplication of a matrix and a vector

﹥>﹥吖頭↗ 提交于 2021-02-07 21:02:11
问题 Is there a simple and quick way to multiply a column of a matrix with element of a vector. We can do this explicitly, program test integer :: x(3,3), y(3), z(3,3) x = reshape([(i,i=1,9)],[3,3]) y = [1,2,3] do i=1,3 z(:,i) = x(:,i) * y(i) print *, z(:,i) enddo end program test Is there a way to perform the do loop in one line. For example in Numpy python we can do this to do the job in one shot z = np.einsum('ij,i->ij',x,y) #or z = x*y[:,None] 回答1: Try z = x * spread(y,1,3) and if that doesn't

Passing both scalars and arrays (of any dimensions) from Fortran to C

别说谁变了你拦得住时间么 提交于 2021-02-07 19:14:27
问题 I have the following Fortran subroutine named show_value that calls a C function named show_value : INTERFACE SUBROUTINE show_value(variable) BIND(C, name = "show_value") USE, INTRINSIC :: iso_c_binding TYPE(*) :: variable END SUBROUTINE END INTERFACE The C function show_value : void show_value(const void *variable) { printf("%d\n", *(int *) variable); } The Fortran subroutine works well when passing scalars to it. Example: INTEGER :: x x = 12 call show_value(x) This will call the C function

Passing both scalars and arrays (of any dimensions) from Fortran to C

丶灬走出姿态 提交于 2021-02-07 19:12:59
问题 I have the following Fortran subroutine named show_value that calls a C function named show_value : INTERFACE SUBROUTINE show_value(variable) BIND(C, name = "show_value") USE, INTRINSIC :: iso_c_binding TYPE(*) :: variable END SUBROUTINE END INTERFACE The C function show_value : void show_value(const void *variable) { printf("%d\n", *(int *) variable); } The Fortran subroutine works well when passing scalars to it. Example: INTEGER :: x x = 12 call show_value(x) This will call the C function

Check whether file has been opened already

浪尽此生 提交于 2021-02-07 13:12:09
问题 I am writing a file reading library, and need to check whether a file has been opened so that I can skip the open statement and go directly to a read. How can this be achieved in fortran? 回答1: When one wants to know about connections to external files there is the inquire statement. There are two forms to this: inquire by file; inquire by unit. tom's answer shows inquire by unit. This tests whether unit 3 is connected to any file. One could then go on to ask the name of the connected file

Check whether file has been opened already

Deadly 提交于 2021-02-07 13:10:37
问题 I am writing a file reading library, and need to check whether a file has been opened so that I can skip the open statement and go directly to a read. How can this be achieved in fortran? 回答1: When one wants to know about connections to external files there is the inquire statement. There are two forms to this: inquire by file; inquire by unit. tom's answer shows inquire by unit. This tests whether unit 3 is connected to any file. One could then go on to ask the name of the connected file

generate a sequence array in fortran

可紊 提交于 2021-02-07 05:34:15
问题 Is there an intrinsic in Fortran that generates an array containing a sequence of numbers from a to b, similar to python's range() >>> range(1,5) [1, 2, 3, 4] >>> range(6,10) [6, 7, 8, 9] ? 回答1: No, there isn't. You can, however, initialize an array with a constructor that does the same thing, program arraycons implicit none integer :: i real :: a(10) = (/(i, i=2,20, 2)/) print *, a end program arraycons 回答2: If you need to support floats, here is a Fortran subroutine similar to linspace in

GO TO statements- Fortran to Matlab

筅森魡賤 提交于 2021-02-05 12:15:18
问题 I have been trying really hard to convert this grid search code from Fortran to Matlab, however I am not able to properly incorporate the GO TO statements (I am trying to use while loops but I think I need something else for them end the search). Any help would be greatly appreciated. vmax = -1.0E+15 amax_G = -1 askipR = REAL(ac_max - ac_min)/REAL(intA) askip = CEILING(askipR) DO acc0 = 1,intA+1 acc = ac_min + askipR * (acc0-1) cons = ( x1 - grida2(acc) ) / onetauc IF (cons<0.0) GOTO 102

Error: Incompatible ranks 0 and 1 in assignment at (1)

瘦欲@ 提交于 2021-02-05 11:20:50
问题 I'm working in a finite difference method on an irregular grid, this is the important part of the code: IMPLICIT DOUBLE PRECISION (A-Z) REAL*16 IPSI,ICORR,POT(20000),VA(20000),delta1(20000), $delta2(20000),R(20000),a,b,d COMPLEX Y(20000),TY2(50000),Z(20000),PSI0(20000),RES,DPSI,C, $CORR,OPK DO I=3,NR-1 delta1=R(I)-R(I-1) delta2=R(I+1)-R(I) a=(2/(delta1*(delta1+delta2))) b=(-2/(delta1*delta2)) d=(2/(delta2*(delta1+delta2))) TY2(I)=((d*Z(I+1))+(b*Z(I))+(a*Z(I-1))) ENDDO When I try to compile I