- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi!
I am looking for a fast way to convert between REAL and COMPLEX variables. The following code snippet shows you what I want to do,
real rtmp1(10), rtmp2(10)
complex ctmp(5)
! load with data.
do i = 1,10
rtmp1(i) = dble(i)
end do
! load the complex array.
ctmp(1:5) = cmplx(rtmp1(1:10:2), rtmp1(2:10:2))
... do some calculations ...
! assign the complex data toa realarray
rtmp2(1:10:2) = real(ctmp)
rtmp(2:10:2) = imag(ctmp)
This will give me rtmp2 = rtmp1 which is what I am looking for in this example.
Is there a faster way of doing this, i.e., without having to copy data using array slicing? Is there a way I can use pointers to point to the start of a memory address?
Thank you very much for your help.
Sincerely,
David
I am looking for a fast way to convert between REAL and COMPLEX variables. The following code snippet shows you what I want to do,
real rtmp1(10), rtmp2(10)
complex ctmp(5)
! load with data.
do i = 1,10
rtmp1(i) = dble(i)
end do
! load the complex array.
ctmp(1:5) = cmplx(rtmp1(1:10:2), rtmp1(2:10:2))
... do some calculations ...
! assign the complex data toa realarray
rtmp2(1:10:2) = real(ctmp)
rtmp(2:10:2) = imag(ctmp)
This will give me rtmp2 = rtmp1 which is what I am looking for in this example.
Is there a faster way of doing this, i.e., without having to copy data using array slicing? Is there a way I can use pointers to point to the start of a memory address?
Thank you very much for your help.
Sincerely,
David
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There was a recent thread on c.l.f where I had asked for help on a similar topic.
http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/c4a783f99fe3276/6a364d586f9b9a0e
You might have to hack using C_PTR if you want to avoid a full copy. F2008 standard has z%RE and z%IM but I don't think they are implemented in the compiler yet.
HTH.
http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/c4a783f99fe3276/6a364d586f9b9a0e
You might have to hack using C_PTR if you want to avoid a full copy. F2008 standard has z%RE and z%IM but I don't think they are implemented in the compiler yet.
HTH.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you are willing to use old-fashioned Fortran77, you can simply equivalence rtmp1 to ctmp. Then all you need to do is load rtmp, and the data is immeduately available in ctmp.
equivalence (ctmp(1),rtmp1(1))
At the end, you could probably just call a simple copy routine, if you ensure that any type checking is disabled (no explicit interface). The compiler would likely inline the routine. Data would be passed by reference, with no descriptor; inside the routine, both input and output would be declared with the same data type:
call mycopy(ctmp,rtmp2,10)
subroutine mycopy(a,b,n)
real a(n),b(n)
do i=1,n
b(i)=a(i)
enddo
end
Purists won't like this, for respectable reasons, but a lot of code used to be written this way.
There are dangers if you use such a routine to copy integers, (eg signalling NaNs get converted to quiet NaNs), but I'm not aware of any issues here, though of course you'd want to test.
And you can't use F90 array notation or assumed shape arrays for the actual or dummy arguments.
For a debug build, you'd get warnings unless youturned off interface generation and/or checking for this routine.
equivalence (ctmp(1),rtmp1(1))
At the end, you could probably just call a simple copy routine, if you ensure that any type checking is disabled (no explicit interface). The compiler would likely inline the routine. Data would be passed by reference, with no descriptor; inside the routine, both input and output would be declared with the same data type:
call mycopy(ctmp,rtmp2,10)
subroutine mycopy(a,b,n)
real a(n),b(n)
do i=1,n
b(i)=a(i)
enddo
end
Purists won't like this, for respectable reasons, but a lot of code used to be written this way.
There are dangers if you use such a routine to copy integers, (eg signalling NaNs get converted to quiet NaNs), but I'm not aware of any issues here, though of course you'd want to test.
And you can't use F90 array notation or assumed shape arrays for the actual or dummy arguments.
For a debug build, you'd get warnings unless youturned off interface generation and/or checking for this routine.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page