5

In Python, there's something called ctypes. With ctypes, one can call any shared library function and get the results back to your script. It supports all kinds of C-language types, such as int, double, long, struct, etc...

Is this possible with Mathematica too? Is there a full example on how to do this?

  • Yes, at least on Windows you can do this with .NET/Link. I don't know if it also works on other platforms. .NET/Link does, but it might be more trouble to set up and I do not know if this specific functionality is supported. – Szabolcs Jul 15 '16 at 10:48
  • Related, perhaps even duplicate?: (22679) – Mr.Wizard Jul 15 '16 at 10:48
  • Here's how to do it: http://reference.wolfram.com/language/NETLink/tutorial/CallingNETFromTheWolframLanguage.html#32076 – Szabolcs Jul 15 '16 at 10:50
  • 1
    @Szabolcs Would be disappointing if it's only for windows... because I do all my tough calculations on Linux. – The Quantum Physicist Jul 15 '16 at 10:50
  • 1
    @Mr.Wizard I would compare this with Python's C API, not ctypes. Because with C API you need to import some python headers, which is similar to what's given in the question there. The nice thing about ctypes is that you can write a native interface without requiring to import any non-native header from Python. This would be great if available for Mathematica! – The Quantum Physicist Jul 15 '16 at 10:52
  • Here's info on how to do it: http://mathematica.stackexchange.com/a/8443/12 Here's info on how to use NET/Link on Unix: http://mathematica.stackexchange.com/questions/20765/running-net-link-on-mono-on-macos-or-any-unix-or-linux-distro http://mathematica.stackexchange.com/questions/1967/using-net-4-0-from-netlink/1968#1968 You could consider rephrasing the question and asking specifically about DefineDLLFuntcion on non-Windows platforms. Please try it out and if it works, do still post your own answer. – Szabolcs Jul 15 '16 at 10:53
  • @Szabolcs Thanks! However the unix link only executes a program. It doesn't use a DLL/SO file. It seems this is not supported in linux. – The Quantum Physicist Jul 15 '16 at 10:55
  • @TheQuantumPhysicist I'm not sure what you mean. Did you get NET/Link working on your platform already? If you did, did you test DefineDLLFuntcion? – Szabolcs Jul 15 '16 at 10:56
  • @Szabolcs I don't use Windows for programming, so I can't test it. I need a unix example with linking to some so file, like libc.so.6, as you see in the examples of ctypes. That's the simplest and minimal working example. Right? – The Quantum Physicist Jul 15 '16 at 10:59
  • What I am trying to tell you is that you can probably do this on Linux. But you have to try if it works. You have all the examples to follow: I gave you a link on how to get NET/Link working on Linux with Mono. That's the first thing you have to do. Second, read up on how to load shared libraries with NET/Link (the other link I have you), and try if that method also works on Linux. It probably does. – Szabolcs Jul 15 '16 at 11:01
  • @Szabolcs OK. I'll try it on linux. If it works, I'll post it here as an answer. Thanks! – The Quantum Physicist Jul 15 '16 at 11:01
  • I should not that when I tried to get NET/Link working on Mac last time, I wasn't successful. So there may be obstacles. But I didn't put much effort into it. While all the steps to achieve this should be in the links I gave you, putting them together successfully in practice may not be trivial, so it will be very useful if you post your experience. This may also be useful: http://www.mono-project.com/docs/advanced/pinvoke/ – Szabolcs Jul 15 '16 at 11:13
  • @Szabolcs Thanks! I'll keep that in mind :) – The Quantum Physicist Jul 15 '16 at 11:18
  • Try Java link in Wolfram, then JNA or JNI to get from Java (or, better, Clojure) to the metal. I've done it. Takes a little work but screams for speed. – Reb.Cabin Jul 16 '16 at 00:04
  • @Reb.Cabin Would be great if you post a working example here :) – The Quantum Physicist Jul 16 '16 at 11:31
  • I've probably lost it. I was toying with factorization and deep zoom. I think I started here: https://github.com/kframework/mpfr-java but most of it worked with little drama. – Reb.Cabin Jul 17 '16 at 04:05

0 Answers0