fortran
changing array dimensions in fortran
See the RESHAPE intrinsic, e.g. http://gcc.gnu.org/onlinedocs/gfortran/RESHAPE.html Alternatively, if you want to avoid the copy (in some cases an optimizing compiler might be able to do a reshape without copying, e.g. if the RHS array is not used afterwards, but I wouldn’t count on it), as of Fortran 2003 you can assign pointers to targets of … Read more
Linking Fortran and C++ binaries using gcc
You’re looking for g++ main.o print_hi.o -o main -lgfortran to link in the standard Fortran libraries. You can also use gfortran by passing -lstdc++.
How to initialize two-dimensional arrays in Fortran
You can do that using reshape and shape intrinsics. Something like: INTEGER, DIMENSION(3, 3) :: array array = reshape((/ 1, 2, 3, 4, 5, 6, 7, 8, 9 /), shape(array)) But remember the column-major order. The array will be 1 4 7 2 5 8 3 6 9 after reshaping. So to get: 1 2 … Read more
OS X package installation depends on gfortran-4.8
Type these two lines in your terminal, direct quote: curl -O http://r.research.att.com/libs/gfortran-4.8.2-darwin13.tar.bz2 sudo tar fvxj gfortran-4.8.2-darwin13.tar.bz2 -C / It will download you the gfortran for Mavericks (which is missing in your system at the moment) and will install it in your system. At least, this solved the same problem for me (I’m running late 2011 … Read more
Correct use of modules, subroutines and functions in Fortran
Modules are always the right thing to use 😉 If you have a very simple F90 program you can include functions and subroutines in the ‘contains’ block: program simple implicit none integer :: x, y x = … y = myfunc(x) contains function myfunc(x) result(y) implicit none integer, intent(in) :: x integer :: y … … Read more
Reading a binary file with python
Read the binary file content like this: with open(fileName, mode=”rb”) as file: # b is important -> binary fileContent = file.read() then “unpack” binary data using struct.unpack: The start bytes: struct.unpack(“iiiii”, fileContent[:20]) The body: ignore the heading bytes and the trailing byte (= 24); The remaining part forms the body, to know the number of … Read more
Passing external function of multiple variables as a function of one variable in Fortran
You cannot make similar tricks with functions in Fortran directly. You also cannot return a closure in Fortran. Just write a wrapper. function wrap_f(x) result(res) … res = f(a,b,…) end function It can be an internal or module function and get a and b by the host association or it can use the module containing … Read more
Is There a Better Double-Precision Assignment in Fortran 90?
A real which isn’t marked as double precision will be assumed to be single precision. Just because sometime later you assign it to a double precision variable, or convert it to double precision, that doesn’t mean that the value will ‘magically’ be double precision. It doesn’t look ahead to see how the value will be … Read more
Procedure with assumed-shape dummy argument must have an explicit interface [duplicate]
Assumed shape dummy arguments (those with (:)) require explicit interface to the procedure to be available at the call site. That means the calling code must know how exactly the subroutine header looks like. See also Module calling an external procedure with implicit interface That explicit interface can be provided in several ways 1. preferred … Read more