13
$\begingroup$

I have a functions written in an external language called GO. How can I quickly load them into the kernel for use as Mathematica functions?

The two ways I've tried are with MathLink and LibraryLink and it's overly complicated, however in version 10 there is RunProcess[]

Can anyone find a way of using this new feature to install an external function?

$\endgroup$
  • $\begingroup$ Or any other language for that matter... Seems like using the operating system piping with this function is the easiest way to link in functions no? $\endgroup$ – user5601 Nov 19 '14 at 3:49
  • $\begingroup$ I've heard that some internal functions were constructed like this. The alternative methods involve writing c-drivers for the linkers and can be hairy... $\endgroup$ – M.R. Nov 19 '14 at 18:35
  • $\begingroup$ @M.R. very interesting! which ones? $\endgroup$ – user5601 Nov 19 '14 at 19:48
  • $\begingroup$ @m.R. SemanticImport indeed uses "ProcessLink" (the family of functions like RunProcess, StartProcess, etc.) Indeed it is also written in Go. $\endgroup$ – Taliesin Beynon Nov 19 '14 at 23:18
  • $\begingroup$ @TaliesinBeynon Cool! If you would please give a little showcase example of how this works that would be great and I'll accept it :) $\endgroup$ – user5601 Nov 19 '14 at 23:31
25
$\begingroup$

Taking user5601's suggestion to do a little demo, I quickly whipped this up as an example of ProcessLink being used to do non-trivial communication between Mathematica and an external program, but with much less ceremony than using ProcessLink or MathLink.

Let's take this little Go program:

package main

import "net/http"
import "bufio"
import "os"
import "fmt"
import "html"
import "strings"

func main() {
    http.ListenAndServe(":8080", http.HandlerFunc(render))
}

func render(w http.ResponseWriter, req *http.Request) {
    var img, response, input string
    input = req.FormValue("input") // extract the input from the page
    if input != "" {
        fmt.Println(input)                  // send to mathematica
        reader := bufio.NewReader(os.Stdin) // get back result
        img, _ = reader.ReadString('\n')
        img = strings.TrimSpace(img)
        img = `<img src="data:image/png;base64,` + img + `"/>`
    }
    response = `<html><body><form action="result.html">Enter a Wolfram Language expression:<br><input type="text" name="input" size="65" value="` +
        html.EscapeString(input) + `"></form><br>The result was:<br>` + img + `</body></html>`
    w.Write([]byte(response)) // write back the new page
}

And this Mathematica program:

server = StartProcess["/Users/taliesinb/processLinkExample/main"]
While[True,
  input = ReadLine[server];
  If[input == "Quit[]", KillProcess[server]; Break[]];
  Print["Executing ", input];
  result = ToExpression[input];
  img = Developer`EncodeBase64[ExportString[Rasterize[result, ImageSize -> Small], "PNG"]];
  WriteLine[server, img]]

You will get a tiny web-server running on localhost:8080 that you can visit in your browser. Here's an example of what it will do:

enter image description here

Brought to you by ProcessLink (tm)!

$\endgroup$
  • $\begingroup$ Fantastic. We do not need webMathematica anymore. $\endgroup$ – user18792 Nov 20 '14 at 13:40
  • $\begingroup$ @taliesin bravo! $\endgroup$ – user5601 Nov 20 '14 at 15:47
  • $\begingroup$ Examples like this should be in the docs! What about arbitrary functions? $\endgroup$ – user5601 Nov 20 '14 at 18:25
  • $\begingroup$ @user5601 I can't write an arbitrary example.. what kind of thing do you want to do? $\endgroup$ – Taliesin Beynon Nov 20 '14 at 19:43
  • $\begingroup$ @TaliesinBeynon Ok, this would be awesome: import the python function lda from NumPy $\endgroup$ – user5601 Dec 4 '14 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.