omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    C programming

    Pythonista
    python 3.6 modules extension
    5
    8
    7884
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ismba98
      ismba98 last edited by

      How do I create modules written in c inside pythonista

      Matteo 1 Reply Last reply Reply Quote 0
      • omz
        omz last edited by

        This isn't possible.

        1 Reply Last reply Reply Quote 1
        • dgelessus
          dgelessus last edited by

          There is the ctypes module though, so if you want to call C functions, then you can do that with ctypes. You can even call functions from Python's C API, if you really want to, but that isn't very useful in many cases.

          1 Reply Last reply Reply Quote 0
          • Matteo
            Matteo @ismba98 last edited by Matteo

            @ismba98 Hi, actually it would be possible to run on the fly some C or Fortran codes using a remote python server (SageMathCell for example, it is free and works well). Some info here.

            For C codes you can read something here and here.

            For Fortran codes something here.

            Obviously you must be online with your Pythonista.

            As example of C code that you can edit in Pythonista and run using Pythonista and the remote server is the following:

            ## C file:
            C_code = """
            int fibonacci(int i)
            {
              if (i < 0) return -1; /* F(i) is not defined for negative integers! */
              if (i == 0) return 0;
              else if (i == 1) return 1;
              else return fibonacci(i-1) + fibonacci(i-2);
            }
            """
            
            ## Spyx file:
            Spyx_code = """
            cdef extern from "fibonacci.c":
                int fibonacci(int n)
            """
            
            ## Create the c file:
            with open("fibonacci.c", 'w') as f:
                f.write(C_code)
            
            ## Create the spyx file: 
            with open("fibonacci.spyx", 'w') as f:
                f.write(Spyx_code)
            
            load("fibonacci.spyx")
            num = 10
            print(fibonacci(num))
            
            
            
            ## Fortran77 file:
            FORTRAN_code = """
                  SUBROUTINE FIB(A,N)
            C
            C     CALCULATE FIRST N FIBONACCI NUMBERS
            C
                  INTEGER N
                  REAL*8 A(N)
                  DO I=1,N
                     IF (I.EQ.1) THEN
                        A(I) = 1.0D0
                     ELSEIF (I.EQ.2) THEN
                        A(I) = 1.0D0
                     ELSE
                        A(I) = A(I-1) + A(I-2)
                     ENDIF
                  ENDDO
                  END
            """
            
            fortran(FORTRAN_code, globals())
            import numpy as np
            num = 10
            a = np.array(xrange(1,num+1), dtype=float)
            fib(a, num)
            print(a)
            

            There is no reason to code in C or Fortran using Pythonista, but sometimes user wants to test some existing open source codes in these programming
            languages.

            Regards.
            Matteo

            1 Reply Last reply Reply Quote 0
            • augusto
              augusto last edited by

              @Matteo excuse my ignorance and my English, how do I install the sage package and where do I take the fortran function?
              I have an iPhone. Thanks

              Matteo 1 Reply Last reply Reply Quote 0
              • Matteo
                Matteo @augusto last edited by

                @augusto Hi, you don't need to install SageMath in Pythonista, it is impossible. You can use a set of python scripts, that run well in Pythonista, in order to send to the remote server some C or Fortran codes/pieces of code to test them.

                In short: with Pythonista you can do a lot of things (trust me, a lot), and a thing that you can do easly with Pythonista is to send some code to a remote python server (SageMathCell, please give credits to Andrey, the main maintainer, for this free service), wait for response and process the output, in order to use it in Pythonista, like a global variable that you can use as you want. You must be online to use the service.

                Please follow the instructions in the corresponding thread here. Try by yourself, you must download the last version of sage_interface, extract it in any folder of Pythonista, see what is inside that folder to understand how to send some Fortran code and obtain the output as a printed string or number/array.
                If you need more info, please ask in that thread.

                Thanks
                Regards

                1 Reply Last reply Reply Quote 0
                • augusto
                  augusto last edited by

                  Thank you @Matteo for your comments. What I did not understand was that their examples run on the sage side and not on the iOS device. Thanks a lot.

                  Matteo 1 Reply Last reply Reply Quote 0
                  • Matteo
                    Matteo @augusto last edited by

                    @augusto Yes, fortran codes run only by sage point of view, but you can use Pythonista to write fortran codes, test and use them when online, without using other ios apps that most likely are only able to send fortran code. Bye

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post
                    Powered by NodeBB Forums | Contributors