Location: PHPKode > projects > MaximaPHP > maximaphp/viewers/mpvmatrixfunction.php
<?php

/**
 * mpvmatrixfunction.php, MaximaPHP Viewer - Functions
 *
 * Functions viewer for MaximaPHP.
 * 
 * This file contains MPVMatrixFunction class.
 * @author Bowo Prasetyo <http://www.my-tool.com/maximaphp/matrix/function>
 * @version 0.1.3
 * @package MaximaPHP
 */

/**************************************************************
 *
 * Copyright (C) 2007 Bowo Prasetyo
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation below,
 * 
 * http://www.gnu.org/licenses/gpl.html#TOC1
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 **************************************************************/

/**
 * Functions viewer class for MaximaPHP.
 *
 */
class MPVMatrixFunction 
{
   /**
    * Examples for expression
    */
   var $examples = array
   (
      array( '', '' )
   );

   /**
    * Get examples
    * @param integer $count number of examples to get
    * @return string
    */
   function getExamples( $count )
   {
      $ex_out = MPVIEWER_GET_EXAMPLES;
      if ( $count == 0 ) $count = count ( $this->examples );
      for ( $i = 0; $i < $count; $i++ )
      {
         $ex_out .= "<pre style=\"background-color: #f0f9ff; border: 1px solid #cccccc; padding: 1px;\">" . htmlentities( $this->examples[$i][0] ) . '<br />' . htmlentities( $this->examples[$i][1] ) . '<br />' . "</pre>\n";
      }
      $ex_out .= "\n";
      return $ex_out;
   }

   /**
    * Format output from MaximaPHP
    * @param string $result output from MaximaPHP
    * @param boolean $simplify whether to simplify output or not
    * @param boolean $numeric whether to apply numeric or not
    * @return string
    */
   function formatOutput( $result, $simplify, $numeric )
   {
      /*
       * Do some post processing for the result here
       * you may want to do some string manipulations
       * using various preg_replace(...) such as
       *
       * $result = preg_replace( '/.*(?=\n\s*:\| matrix \|:)/s', '', $result );
       * if ( $simplify or $numeric )
       *    $result = preg_replace( '/\(%i[0-9]+\).*(?=\n\s*\:\| simplified \|:)/s', '', $result );
       * else
       *    $result = preg_replace( '/\(%i[0-9]+\).*(?=\n\s*:\| matrixfunction \|:)/s', '', $result );
       *
       */
      return $result;
   }

   /**
    * Show the viewer
    */
   function show( $qstring )
   {
      require_once( 'mprewriteurl.php' );

      if ( isset( $_SESSION['last_output'] ) )
         $last_output = $_SESSION['last_output'];
      else
         $last_output = '';

      if ( isset( $_SESSION['last_input'] ) )
         $last_input = $_SESSION['last_input'];
      else
         $last_input = '';

      if ( isset( $_GET['sub2'] ) )
         $view = $_GET['sub2'];
      else
         $view = 'text';
      

      if ( isset( $_POST['matrix'] ) )
         $matrix = stripslashes($_POST['matrix']);
      else
      {
         if ( isset( $_SESSION['matrix'] ) )
            $matrix = $_SESSION['matrix'];
         else
            $matrix = '';
      }

      if ( isset( $_POST['function'] ) )
         $function = $_POST['function'];
      else
      {
         if ( isset( $_SESSION['function'] ) )
            $function = $_SESSION['function'];
         else
            $function = 'value1';
      }

      if ( isset( $_POST['simplify'] ) )
         $simplify = TRUE;
      else
         $simplify = FALSE;

      if ( isset( $_POST['numeric'] ) )
         $numeric = TRUE;
      else
         $numeric = FALSE;

      if ( isset( $_POST['example'] ) )
      {
         $last_input = '';
         $matrix = '';
      }

      if ( $matrix == '' )
      {
         if ( $last_input != '' ) $matrix = $last_input;
         else 
         {
            $m = mt_rand( 0, count( $this->examples ) - 1 );
            
            $matrix = $this->examples[$m][0];
            $function = $this->examples[$m][1];
         }
      }

      /**
       * Link to change view
       */
      $view_link = $view == 'text' ? '<b>Text</b> | ' : '<a href="' . mprewriteurl( 'index.php?' . $qstring . 'sub2=text' ) . '">Text</a> | ';
      $view_link .= $view == 'mathml' ? '<b>MathML</b> | ' : '<a href="' . mprewriteurl( 'index.php?' . $qstring . 'sub2=mathml' ) . '">MathML</a> | ';
      $view_link .= $view == 'tex' ? '<b>TeX</b> | ' : '<a href="' . mprewriteurl( 'index.php?' . $qstring . 'sub2=tex') . '">TeX</a> | ';
      $view_link .= $view == 'maxima' ? '<b>Maxima</b> | ' : '<a href="' . mprewriteurl( 'index.php?' . $qstring . 'sub2=maxima' ) . '">Maxima</a> | ';
      $view_link .= $view == 'examples' ? '<b>' . MPVIEWER_EXAMPLES . '</b> | ' : '<a href="' . mprewriteurl( 'index.php?' . $qstring . 'sub2=examples' ) . '">' . MPVIEWER_EXAMPLES . '</a> | ';
      $view_link .= $view == 'code' ? '<b>' . MPVIEWER_CODE . '</b>' : '<a href="' . mprewriteurl( 'index.php?' . $qstring . 'sub2=code' ) . '">' . MPVIEWER_CODE . '</a>';
      $view_link .= "<br /><br />\n";

?>

   <h3><?php echo MPVMATRIXFUNCTION_TITLE ?></h3>
   <?php echo MPVMATRIXFUNCTION_MAIN_0 ?><br />

   <?php echo str_replace( '##VERSION2##', MAXIMAPHP_VERSION, str_replace( '##VERSION1##', MAXIMA_VERSION, MPVIEWER_POWERED_BY ) ) ?><br />

   
<?php
   /**
    * Print MathML message
    */
   if ( $view == 'mathml' )
   {
      echo MPVIEWER_MATHML_MSG . '<br />';
   }
?>

   <br />
   <?php echo $view_link ?>
   <form action="" method="post">
   <table>
   
   <tr><td valign="top"><?php echo MPVMATRIXFUNCTION_MAIN_1 ?> </td><td><textarea name="matrix" rows="10" cols="50"><?php echo htmlentities( $matrix ) ?></textarea><br /><span class="eg"><?php echo MPVIEWER_EG ?> </span></td></tr>
   <tr><td><?php echo MPVMATRIXFUNCTION_MAIN_2 ?> </td><td>
   <input type="radio" name="function" value="value1" <?php echo $function == 'value1' ? 'checked="checked"' : '' ?> /> Value 1
   <input type="radio" name="function" value="value2" <?php echo $function == 'value2' ? 'checked="checked"' : '' ?> /> Value 2
   </td></tr>
   <tr><td> </td><td><input type="checkbox" name="simplify" <?php echo $simplify ? 'checked="checked"' : '' ?> /> <?php echo MPVIEWER_SIMPLIFY ?></td></tr>
   <tr><td> </td><td><input type="checkbox" name="numeric" <?php echo $numeric ? 'checked="checked"' : '' ?> /> <?php echo MPVIEWER_NUMERIC ?></td></tr>
   <tr><td align="left"><input type="submit" name="submit" /></td><td align="right"><input type="submit" name="example" value=" <?php echo MPVIEWER_EXAMPLE ?> " /></td></tr>
   </table>
   </form>

<?php

      /**
       * Instantiate MaximaPHP if 'submit' or 'example' button was pressed
       */
      if ( isset( $_POST['submit'] ) or isset( $_POST['example'] ) )
      {
         require_once( 'maximaphp.php' );
   
         /**
          * Security check for all text inputs
          */
         $maximaphp = new MaximaPHP( $matrix, $last_input, $last_output );
         $matrix = $maximaphp->secure();

         /**
          * Prints error message in case unsecure input
          */
         if ( !$maximaphp->isSecure() )
         {
            $maximaphp->getSecurityWarning();
         }

         /**
          * Otherwise send input to Maxima
          */
         else if ( $matrix != '' ) 
         {
            $ev_simp = '; block(disp(":| simplified |:")##EV_NUMER_1##, fullratsimp(radcan(expand(%)))##EV_NUMER_2##)'; 
            $ev_numer_1 = ', numer:true, %enumer:true'; 
            $ev_numer_2 = ', scanmap(rational,%%)';
            $tomaxima = <<<EOT
/*
 * Put your Maxima code to do calculation here
 * it should be free of unsecure commands
 */
block
(
 disp(":| matrix |:"),
 [##MATRIX##]
);
disp(":|o|:");
block
(
 disp(":| matrixfunction |:"),
 matrixfunction(##MATRIX##)
)##EV_SIMP##
EOT;
            $_SESSION['last_input'] = $matrix;
            $_SESSION['function'] = $function;

            /**
             * Do some little pre-processing combining $matrix and $tomaxima such as
             *
             * $tomaxima = str_replace( '##MATRIX##', $matrix, $tomaxima );
             */
            $tomaxima = str_replace( '##EV_SIMP##', ($simplify or $numeric) ? $ev_simp : '', $tomaxima );
            $tomaxima = str_replace( '##EV_NUMER_1##', $numeric ? $ev_numer_1 : '', $tomaxima );
            $tomaxima = str_replace( '##EV_NUMER_2##', $numeric ? $ev_numer_2 : '', $tomaxima );

            /**
             * Set new input to Maxima
             */
            $maximaphp->setInput( $tomaxima );

            /**
             * Since it is already secure we could bypass security
             */
            $maximaphp->bypassSecurity();

            $result = $maximaphp->execute( $view );
            $result = $this->formatOutput( $result, $simplify, $numeric );

            $_SESSION['last_output'] = $maximaphp->lastOutput;

            /** 
             * Output the result
             */

?>

   <pre>
<?php
   printf( MPVIEWER_CALCULATION_TIME, $maximaphp->totalTime );
   echo $result;
?>
   </pre>

<?php        
            /** 
             * Print out Maxima code
             */
            if ( $view == 'code' )
            {
?>

<?php echo MPVMATRIXFUNCTION_MAIN_3 ?><br /><br />
<textarea rows="20" cols="60" style="border:none" wrap="off" readonly="readonly">
<?php echo MPVMATRIXFUNCTION_MAIN_4 . "\n" . htmlentities( $tomaxima ) ?>
</textarea>

<?php
            }
         }
      }
   
      /**
       * Print out examples
       */
      echo $this->getExamples( $view == 'examples' ? 0 : 5 );

?>

<?php
   }
}

?>
Return current item: MaximaPHP