How to convert the following IDL program lines to equivalent Python program lines?

Ben Bacarisse ben.usenet at bsb.me.uk
Thu Nov 28 07:45:27 EST 2019


Madhavan Bomidi <blmadhavan at gmail.com> writes:

> I have the following IDL program lines. I want to write equivalent
> Python program lines. Can anyone suggest me equivalent Pythons program
> lines?
>
> # ------------------- 1 ----------------------- #
> How to handle the format in IDL to Python?
>
> IDL program line:
>
> print,'Column ',J,' of product matrix A*AINV:',format='(1X,A7,I3,A27)'
>
>
> Python program line written by me:
>
> print('Column '+str(j)+' of product matrix A*AINV:')

You could just use

  print('Column', j, 'of product matrix A*AINV:')

but if you need to copy the I3 format:

  print('Column {0:3} of product matrix A*AINV:'.format(j))

I don't know what the 1X format does.  The A7 and A27 formats just seem
to involve the programmer counting the strings.  Very odd.

> #-------------------2 ------------------------ #
> How the JMP can be transformed in Python?
>
> IDL program lines:
>
>       FOR K=1,M DO BEGIN
>         FOR I=1,M DO BEGIN
>           IF I NE K THEN BEGIN
>             IF WK(K-1,K-1) EQ 0 THEN BEGIN
>               L=1
> JMP:          IF WK(L-1,K-1) EQ 0 THEN BEGIN
>                 L=L+1
>                 GOTO, JMP
>               ENDIF
>               FOR J=K,2*M DO BEGIN
>                 WK(K-1,J-1)=WK(K-1,J-1)+WK(L-1,J-1)
>               ENDFOR
>             ENDIF
>             U=-WK(I-1,K-1)/WK(K-1,K-1)
>             FOR J=K+1,2*M DO BEGIN
>               WK(I-1,J-1)=WK(I-1,J-1)+U*WK(K-1,J-1)
>             ENDFOR
>           ENDIF
>         ENDFOR
>       ENDFOR
>
> JMP: RETURN

This is very odd.  What does it mean when a label is duplicated in IDL?
If the second one were not there, this:

  JMP:          IF WK(L-1,K-1) EQ 0 THEN BEGIN
                  L=L+1
                  GOTO, JMP
                ENDIF

would just be a while loop:

                while WK[L-1,K-1] == 0:
                  L=L+1

<cut>

By the way, all those -1s suggest that the IDL was itself a translation
from a language with 1-based array indexing.  All that might be able to
be tidied up.

-- 
Ben.


More information about the Python-list mailing list