MathLink, J/Link, and webMathematica®


 webMathematica is a registered trademark of  Wolfram Research Inc.

 (Return to Introduction to Mathematica)
    (Go to webMathematica demos)

For tasks requiring interfaces to the external environment Mathematica provides MathLink, which allows Mathematica programs to communicate with external programs or vice versa. MathLink can be used with programs written in C, Java, or other languages.

J/Link is an extension of MathLink providing additional power and ease-of-use to programmers using the Java language. Using J/Link and JSP (Java Server Pages), webMathematica provides the capability of creating pages with dynamic, interactive mathematical content on the Worldwide Web.


Integrating Mathematica with other languages: MathLink

The diagram below shows the overall architecture of Mathematica. "Lightning bolts" show communications links, double-headed arrows indicate direct program calling:

Diagram showing Mathematica kernel linked to command line, GUI notebook front end, and other programs via MathLink

The actual execution of Mathematica expressions is done in the kernel. This may be running on the same machine as the front end, or on a different machine on a network. The kernel might run on a different machine to take advantage of a more powerful workstation to do computations, or to allow multiple users to share a kernel. The front end, which is normally the notebook interface, is connected to the kernel via a C-language API called MathLink.

MathLink is an open API, and it may be used for other purposes as well. For example, if someone wanted to create a different, perhaps enhanced, front end for Mathematica, this could be done using MathLink. A more common use of MathLink is to allow external programs access to Mathematica computational functions, or to allow Mathematica to access external functions. Typical uses might include:

  • Providing special-purpose computations for an application, to avoid needing to code them. An example of this is Wolfram's Mathematica Link for Excel. This product functions as a plug-in to the Microsoft Excel™ spreadsheet, and provides full access to Mathematica for functions that Excel does not provide.

  • Allowing Mathematica to use high-performance computing routines written in other languages. Although Mathematica's performance is excellent considering its high level and ease of use, it cannot always compete with the performance of the best hand-coded functions written in C or Fortran. Where maximum performance is needed for specific computationally intense functions, MathLink can be used to obtain it.

  • Providing access to external data sources. Mathematica can import a variety of external formats, such as spreadsheets, but it cannot directly access many desirable sources: corporate databases, realtime data from sensors, etc. Interface programs may be written using MathLink to allow Mathematica to directly access such sources. Commercial product examples include Database Access Kit from Wolfram Research and Mathematica Link for LabVIEW from BetterVIEW Consulting.

  • Providing access to sophisticated platform user interface controls in a Mathematica computation running in the notebook front end.

Though MathLink is a C API, it can be used for other languages; even (with a small translation module written in assembler) languages that do not use the C calling conventions.

For more information on MathLink, there is documentation and sample code in the /AddOns/MathLink directory that comes as part of Mathematica. There's also a good book, MathLink: Network Programming with Mathematica, by Chikara Miyaji and Paul Abbott.


Extending MathLink: J/Link

Wolfram Research developed a very nice integration of MathLink with Java, called J/Link. It has excellent facilities for linking Mathematica to Java, or vice versa, and is well-documented and easy to use. In addition to providing a high-level interface to MathLink, it also adds classes to Java for such things as displaying Mathematica graphics and typeset expressions in a Java window.

This diagram shows the relationship of J/Link to Java, MathLink, and other components:

Diagram showing Mathematica kernel linked to Java programs via J/Link

Stub functions written in C provide the link to the MathLink interface. They are defined as native mathods in the Java class NativeLink. The Java KernelLink class provides the a higher-level interface which is normally what a Java programmer would use to access facilities of Mathematica (for example, solving symbolic equations, numerical calculations, or producing graphics).

Unless you really need C, J/Link is a better alternative for most of the things you might want to do with MathLink. More detailed information on J/Link can be found on the Wolfram Research web site.


Mathematica on the web: webMathematica

The following diagram illustrates how webMathematica is supported by a web server/Java servlet combination such as Apache Tomcat:

Diagram showing the architecture of webMathematica, and how MSP pages access the Mathematica kernel using J/Link

On the bottom, a request from a web browser with the suffix .jsp is directed to an instance of the JspServlet class, which parses and complies the page into a Java servlet; the servlet executes and builds an HTML response to the browser.

Similarly, a request from a web browser with the suffix .msp is directed to an instance of the MSP class (supplied as a component of webMathematica). The MSP instance acquires a Mathematica kernel instance from a shared pool, and uses J/Link to execute Mathematica functions coded in the MSP (Mathematica Server Pages) script language. The result is incorporated in the HTML response to the client browser.

MSP looks a lot like standard Mathematica code imbedded in HTML. As an example, here is a fragment of code from an MSP script that fits a Weibull distribution to empirical data:

<%Mathlet Needs["Reliability`"]; %>
<%Mathlet
  MSPBlock[
    {$$data},
    fit = ksWeibullGof[$$data];
  ]
%>
Fitted Weibull shape parameter is <%Mathlet ToString[fit[[2]]] %>,
scale parameter is <%Mathlet ToString[fit[[3]]] %>, <br>
Kolmogorov-Smirnov statistic is <%Mathlet ToString[fit[[6]]] %>. <br>
Plot of empirical versus fitted data: <br><br>
<%Mathlet
  MSPBlock[
    {$$data},
    MSPShow[
      empiricalCDF[$$data, Plot->False, ReturnGraphic->True,
        PlotWith -> {fitWeibull[empiricalReliability[$$data]][[3]]}]]
  ] %>

Anything between <%Mathlet and %> is MSP code executed by the Mathematica kernel; anything else is HTML which is passed unchanged to the client browser.

There are webMathematica examples on this site. Additional examples and more detailed technical information on webMathematica can be found on the Wolfram Research web site.