Find an alternative for norm2() and move_alloc() in Fortran 95

218 Views Asked by At

I write code based on modern Fortran. For some reason, I want to modify it in a way that is compatible with the old version. Converting from the latest version to version 95 is desirable here. I have trouble with two intrinsic functions. "Mov_alloc" and "Norm2" are parts of these functions. I want to know: are there any intrinsic functions for them in Fortran 95? Or, are there any external functions that do the same job precisely?

1

There are 1 best solutions below

10
Vladimir F Героям слава On

You can easily implement norm2() yourself based on the definition. Some care must be taken if your numbers are so large that overflow is an issue. But the simplest version is as simple

norm2 = sqrt(sum(A**2))

There is no equivalent of move_alloc() in Fortran 95. You may need to use pointers instead of allocatable variables. You could implement your own version in C, but that would require many features from Fortran 2003-2018, so it makes little sense for you.


You can consider reallocating your arrays yourself and copying the data instead of doing move_alloc():

   if (allocated(B)) deallocate(B)
   allocate(B(lbound(A,1):ubound(A,1)))
   B(:) = A
   deallocate(A)

However, it is not the same as move_alloc().