Efficient mathematical processing using Scilab through PHP

Bring the Web and science together for a smarter world

A lot of scientific data is freely available on the Internet. Combine that with open source tools to efficiently process the data, and anyone can experiment and explore and share their results with the rest of the community. See one approach to community-driven science by connecting PHP to Scilab.


Daniel J Lewis, Web Developer & Computer Scientist

Daniel LewisDaniel Lewis is a professional computer scientist, Web developer, and knowledge engineer with Sift Media in the United Kingdom. He has also worked with companies such as OpenLink Software on technology evangelism tasks and The Open University on Social Semantic Web applications. His programming languages of choice include Java, Ruby, and PHP; he has academic experience in MATLAB, Scilab, and Octave. You can reach him at daniel@vanirsystems.com.

16 March 2010

Also available in Japanese Portuguese

Scripting languages like Ruby, Python, and PHP power modern-day server-side Web development. These languages are great because you can easily and rapidly build Web sites. However, their downfall is their inefficiency with complicated algorithms, such as those found in mathematics and the sciences.

Frequently used acronyms

  • GUI: Graphical user interface
  • HTML: Hypertext Markup Language
  • HTTP: Hypertext Transfer Protocol
  • UI: User interface
  • XHTML: Extensible HTML

More and more science data is appearing on the Web — often licensed to be free and open. With openness, acceptance, and availability come collaborative R&D. People all around the world are able to find patterns in your data and theirs, furthering humanity's understanding of the secrets of science.

Scientific computation often requires heavy processing and powerful systems. Such systems are often difficult to find — particularly on the Web, where all that scientific data resides. In this article, we'll investigate one particular way to merge the power of a particular bit of scientific software — Scilab — with the ease of development and Web-friendliness of a server-side language: PHP.

Scilab is a numerical computational package developed in 1990 by researchers from the INRIA and the cole nationale des ponts et chausses (ENPC). It is implemented as a high-level, numerically oriented programming language distributed under the GPL-compatible CeCILL language. Scilab provides simple matrix operations like multiplication, plus a library of high-level operations like complex multi-dimensional arithmetic and correlation. It can be used for signal processing, statistical analysis, image enhancement, fluid dynamics simulations, and numerical optimization.

Who can benefit from this article?

This article was written with scientists, mathematicians, and computer scientists in mind. It is not tied to any one system: Although I chose PHP, Apache, Scilab, Linux®, and XHTML, the same theories and methodologies could be applied to (for example), Ruby, Lighttpd, MATLAB, Mac OS X, and HTML5. The key thing is to be open and accepting, to be ready to explore and experiment.


I wrote and tested this article using Scilab V5.1.1, PHP V5.2.10, XHTML V1.0 Strict, and Apache V2.2.12 on Ubuntu kernel 9.10. The examples provided should work with new versions of each element and in some cases are backward-compatible.

Setting up Scilab

Scilab works on Linux/UNIX®, Mac OS X, or Windows® and is available for download from the Scilab Web site (see Resources). I wrote this article primarily for Linux/UNIX users, but you can find similar constructs on Mac OS X. For details on using Scilab on the Windows command line, see the Scilab Web site.

Prerequisites for Scilab that are not on your typical Linux/UNIX system include Java™ technology, Tcl, and Tk, which the Scilab engine sometimes uses for its UI, or the GNU Compiler Collection (GCC), which Scilab uses as part of its image and video processing toolkit. Alternatively, Scilab is available through some Linux and UNIX package managers, such as the Synaptic Package Manager using Ubuntu repositories.

Setting up PHP and Apache

PHP programs are scripts that need to be run through an interpreter to execute. You can download the interpreter from the PHP Web site (see Resources), and through some Linux and UNIX package managers.

You need Web server software to get PHP working as a server-side language. One such Web server is Apache HTTP Server, which is available on the Apache Web site (see Resources) or through some Linux and UNIX package managers.

Some systems can encapsulate Apache and PHP along with other applications like MySQL. One such example is XAMPP, but you may find that there are restrictions to performing the some of the tasks outlined in this article.


Scilab is a kind of mathematical algorithm language, but it will also seem familiar to the average computer programmer. It is a dynamic language specialized for mathematical and scientific applications, and it has strong features within matrix operations. For the mathematicians and scientists among us, Scilab is similar to the R programming language, and the system has similarities with the open source GNU Octave and the proprietary MATLAB.

Connecting to Scilab on the command line

Scilab comes with a rather useful GUI you can easily put Scilab script into. However, you can also use Scilab from the command line, which is what you must do to get your Web-based system working later. You end up with the following rather complicated command-line operation:

scilab -nwni -nb -e "m=[1 2 3; 3 4 5];disp(m);exit"

This is a bit confusing at first glance because it uses a strange combination of letters. First is the word scilab, which quite obviously fires up Scilab. Then the following flags (parameters for when Scilab loads) appear:

  • nwni— No windowing system is necessary.
  • nb— Do not show welcoming messages.
  • e— The next item on the line is an expression that the Scilab engine must execute.

An example variable assignment is a = 5;, where a is an example variable name, = is the assignment, and 5 is the assigned value. You end a line with a semicolon (;); otherwise, it prints to screen after execution.

The example in this article assigns a variable to a matrix. You build up a matrix using square brackets ([]), with each element separated with a space and each line separated with a semicolon. So in this example, you have m=[1 2 3; 3 4 5].

For your future Web-based system, you need to be specific about what you want to print to screen. You can do this using Scilab's disp function, which takes in a variable, disp(m). You then need to terminate Scilab to free computation resources, which you do with exit. For details on scripting in Scilab, see the Scilab Web site.

Connecting to Scilab through PHP

There are a few ways you can try to bridge the gap between the seemingly disparate systems of PHP and Scilab. One way would be to send direct Scilab command chains through its Java interface. Another would be to instantiate Scilab through the command line and pass Scilab expressions to it. I chose the latter method, as the Java interface is in its early stages and needs to improve to work with full functionality within PHP. Also, the latter method takes full control of the instantiation of Scilab and is not limited to pre-made interfaces.

The first example using PHP sends the simple Scilab command built in the previous section, which simply prints out a matrix to the screen in plain-text format. Listing 1 shows the code for this operation.

Listing 1. Outputting results sent to Scilab through PHP
        exec('scilab -nwni -nb -e "m=[1 2 3; 3 4 5];disp(m);exit;"', $output);

You use the exec function to send a command to the operating system. This command calls Scilab, just as you saw in the previous section:

  • exec—This handy PHP function sends a string for the operating system's command line to process. Some care is needed when using this function on Web sites: If you give people access to it, you could be giving them access to your server's hard disk, as well. Its first parameter is the command to send; the second parameter is the full multi-line outputted result.
  • print_r— This command simply prints any variable, including its structure. In this case, the $output variable is a one-dimensional array in which each element is a line of output.

The next example merges code in various languages — namely, the XHTML Web data document language, the PHP server-side language, and your call to Scilab through the Linux/UNIX command line. See Listing 2.

Listing 2. Sending complex expressions to Scilab and rendering the output through PHP in XHTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<title>Example 2: Sending equations to Scilab using a PHP web interface</title>
<p>Example 2: Sending equations to Scilab using a PHP web interface<p>

    if(isset($_GET['code']) && $_GET['code'] != "") {
        $defaultcode = str_replace('\"', '"', $_GET['code']);
        $code = preg_replace("/[\n\r]/","", $_GET['code']);
        exec('scilab -nwni -nb -e "' . $code . ';exit;"', $output);
        echo '<div id="result">Result = ';
        foreach($output as $line) {
            echo "<br />";
        echo "</div>";
    } else {
        $defaultcode = "";
    <div id="codeeditor">
    <form action="webscilab.php" method="get">
        <textarea id="code" name="code" title="Code" rows="10" cols="30"
			style="Courier, monospace"><?php echo $defaultcode; ?>
		</textarea> <br />
        <input type="submit" id="submit" name="submit" 
			title="Submit" value="Submit" />

First, let's analyze the XHTML, focusing on the important areas. For detail about the areas this article doesn't cover, the W3Schools Web site is a good starting point (see Resources).

The code in Listing 2 is a simple XHTML form. It uses the HTTP GET method to refresh the current page with extra parameters. Within this form are two elements: a text area provided to allow users to enter plain Scilab code for execution and a Submit button that performs the form action.

PHP provides the real control in this code. When the page is loaded, the PHP code determines whether the page is a "reloaded" page from the form. If it is, the code simply takes the Scilab code from the form and sends it through to Scilab using the exec command. The values from the form are stored in PHP's $_GET array, which is an array of HTTP get parameters.

However, there are some important differences. First, because HTTP and Web browsers act in a certain way, you need a way of allowing people to re-edit and re-execute their code. So that you have to store the inputted code in a variable, replacing certain HTTP characters with their original values (in this case, quotation marks). You do this with PHP's str_replace function so you can print the characters to the default value of the textarea form element.

You must also act on the code. As it is being sent to Scilab from the command line, you must clean up newlines by deleting them (or, more accurately, replacing them with empty strings). In this case, you use something a bit more powerful: PHP's preg_replace, which takes a regular expression as its search term. As the Scilab code is put into the exec function, the exit command is concatenated to the end of it so you don't have to rely on the user to input the word exit each time.

Next, you have a PHP foreach loop, which loops through each outputted line and renders it nicely into XHTML, ready for the browser to display. The loop simply iterates through the array and prints each line as it would do on the command line, but enforcing an XHTML break-line element after each print.

Graphically sending equations and receiving answers

So far, you have a reasonably powerful application, but it's not terribly different from using the Scilab interface. Neither is it a particularly user-friendly way of working with the software. Therefore, the next task is to provide some kind of graphical interface in XHTML and PHP, which you can do by adding useful forms to generate powerful Scilab expressions.

The example that best illustrates the use of such forms is a simple sine calculator. Only the body of the XHTML is included in Listing 3 for the sake of simplicity and clarity.

Listing 3. A trigonometry-based calculator demonstrates dynamic generation of Scilab scripts
<p>Sine Calculator<p>
    $answer = "";
    if(isset($_GET['opposite']) && isset($_GET['hypotenuse'])) {
        $code = "result = " . $_GET['opposite'] . 
		  "/" . $_GET['hypotenuse'] . "; disp(result); exit";
        exec('scilab -nwni -nb -e "' . $code . '"', $output);
        $answer = $output[1];
    <div id="calculator">
    <form action="example3.php" method="get">
        <label for="opposite">Opposite</label><br />
		<input type="textbox" id="opposite" name="opposite" title="opposite" 
		  value="<?php echo $_GET['opposite']; ?>" /><br />
        <label for="hypotenuse">Hypotenuse</label><br />
		<input type="textbox" id="hypotenuse" name="hypotenuse" 
		  title="hypotenuse" value="<?php echo $_GET['hypotenuse']; ?>" />
		<br />
        <input type="submit" id="submit" name="submit" title="Submit" value="Submit" />
		<br /><br />
        <label for="result">Result</label><br />
		<input type="textbox" id="result" name="result" 
		  value="<?php echo trim($answer); ?>" /><br />

This code uses more or less the same methodology as the code editor in Listing 2. This time, you create a simple textbox form that corresponds to the opposite and hypotenuse of a right triangle. This form is then taken in, and a calculation is concatenated from the $_GET array. This idea is rather simple, but it can provide a foundation for a strong scientific or mathematical application.

Graphs from Scilab in PHP

Scilab has the ability to output a graph based on data. This functionality makes Scilab incredibly powerful and can be used within your Web application. Not only can it output a graph to screen but it also has the ability to output to a file, including file formats suitable for Web browser rendering (namely, GIFs and JPEGs). In the Scilab code in Listing 4, an example graph is printed to a JPEG file.

Listing 4. Example Scilab graph output script

There are many other ways of generating graphs, but this graph-to-file function xs2jpg is a useful one. To run this code through PHP, you need the graphics library. So, instead of using the nwni flag on the command line, you use nw, which still limits Scilab to command-line calculations, but loads the graphics libraries.

This functionality has the potential of being factored into PHP. The code in Listing 5 shows one such example based on the code in Listing 2. (Note that some manipulation of permissions is needed to get this code to work correctly, and there are alternatives that may work a bit better.)

Listing 5. A quick and dirty technique for outputting and showing a graph in XHTML from Scilab
<p>Example 4: Sending equations to Scilab and receiving JPEGs<p>

    if(isset($_GET['code']) && $_GET['code'] != "") {
        $filename = time() . ".jpg";
        $defaultcode = str_replace('\"', '"', $_GET['code']);
        $code = preg_replace("/[\n\r]/","", $_GET['code']);
        $exec = 'scilab -nw -nb -e "' . $code . 
			'xs2jpg(0,\"/var/www/webscilab/' . $filename . '\");exit;"';
        <div id="result">
          <img src="<?php echo $filename; ?>" alt="result" />
    } else {
        $defaultcode = "scf(0);
    <div id="codeeditor">
    <form action="example4.php" method="get">
        <textarea id="code" name="code" title="Code" 
		  rows="10" cols="30" style="Courier, monospace">
			<?php echo $defaultcode; ?>
		</textarea> <br />
        <input type="submit" id="submit" name="submit" title="Submit" value="Submit" />

Note the use of concatenation with the xs2jpg function in order to create the JPEG file. Also note the use of a time-stamp function to generate a (more or less) unique file name that can be linked to, as is done later in the XHTML img tag. This is far from being a full solution, as it is a bit experimental and relies on less-than-desirable permission settings, but you can see that such a task is possible.


This article showed how to connect to Scilab easily using PHP from the command line. You can send Scilab scripts from users to Scilab, and you can build easy-to-use interfaces that generate Scilab scripts, thus hiding the complexity of Scilab from users while maintaining functionality. Finally, you can experiment with Scilab's graph library and link to a saved graph through XHTML.

I hope you will continue to play with this unique software and work on the foundation that this article lays. In particular, get to know Scilab's simulated annealing and genetic algorithms functions, or the Image and Video Processing library, which provides the ability to manipulate uploaded pictures and movies. With efficient mathematical processing on the Web and by using open source tools and technologies, you have the ability to collaboratively build a smarter planet.


Sample codeos-php-scilab-webscilab.zip5KB



Get products and technologies



developerWorks: Sign in

Required fields are indicated with an asterisk (*).

Need an IBM ID?
Forgot your IBM ID?

Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.


All information submitted is secure.

Dig deeper into Open source on developerWorks

Zone=Open source
ArticleTitle=Efficient mathematical processing using Scilab through PHP