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

/**
 * mpvmatrixtrigonometry.php, MaximaPHP Viewer - Trigonometry
 *
 * Trigonometry viewer for MaximaPHP.
 * 
 * This file contains MPVMatrixTrigonometry 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.
 *
 **************************************************************/

/**
 * Trigonometry viewer class for MaximaPHP.
 *
 */
class MPVMatrixTrigonometry 
{
   /**
    * Examples for expression
    */
   var $examples = array
   (
      array( 'matrix( [a, b], [c, d] )', 'sin' ),
      array( 'matrix( [a, b], [c, d] )', 'cos' ),
      array( 'matrix( [a, b], [c, d] )', 'tan' ),
      array( 'matrix( [a, b], [c, d] )', 'cot' ),
      array( 'matrix( [a, b], [c, d] )', 'sec' ),
      array( 'matrix( [a, b], [c, d] )', 'csc' ),
      array( 'matrix( [a, b], [c, d] )', 'asin' ),
      array( 'matrix( [a, b], [c, d] )', 'acos' ),
      array( 'matrix( [a, b], [c, d] )', 'atan' ),
      array( 'matrix( [a, b], [c, d] )', 'acot' ),
      array( 'matrix( [a, b], [c, d] )', 'asec' ),
      array( 'matrix( [a, b], [c, d] )', 'acsc' ),
      array( 'matrix( [a, b], [c, d] )', 'sinh' ),
      array( 'matrix( [a, b], [c, d] )', 'cosh' ),
      array( 'matrix( [a, b], [c, d] )', 'tanh' ),
      array( 'matrix( [a, b], [c, d] )', 'coth' ),
      array( 'matrix( [a, b], [c, d] )', 'sech' ),
      array( 'matrix( [a, b], [c, d] )', 'csch' ),
      array( 'matrix( [a, b], [c, d] )', 'asinh' ),
      array( 'matrix( [a, b], [c, d] )', 'acosh' ),
      array( 'matrix( [a, b], [c, d] )', 'atanh' ),
      array( 'matrix( [a, b], [c, d] )', 'acoth' ),
      array( 'matrix( [a, b], [c, d] )', 'asech' ),
      array( 'matrix( [a, b], [c, d] )', 'acsch' ),
      array( 'matrix( [1, 2], [3, 4] )', 'sin' ),
      array( 'matrix( [1, 2], [3, 4] )', 'cos' ),
      array( 'matrix( [1, 2], [3, 4] )', 'tan' ),
      array( 'matrix( [1, 2], [3, 4] )', 'cot' ),
      array( 'matrix( [1, 2], [3, 4] )', 'sec' ),
      array( 'matrix( [1, 2], [3, 4] )', 'csc' ),
      array( 'matrix( [1, 2], [3, 4] )', 'asin' ),
      array( 'matrix( [1, 2], [3, 4] )', 'acos' ),
      array( 'matrix( [1, 2], [3, 4] )', 'atan' ),
      array( 'matrix( [1, 2], [3, 4] )', 'acot' ),
      array( 'matrix( [1, 2], [3, 4] )', 'asec' ),
      array( 'matrix( [1, 2], [3, 4] )', 'acsc' ),
      array( 'matrix( [1, 2], [3, 4] )', 'sinh' ),
      array( 'matrix( [1, 2], [3, 4] )', 'cosh' ),
      array( 'matrix( [1, 2], [3, 4] )', 'tanh' ),
      array( 'matrix( [1, 2], [3, 4] )', 'coth' ),
      array( 'matrix( [1, 2], [3, 4] )', 'sech' ),
      array( 'matrix( [1, 2], [3, 4] )', 'csch' ),
      array( 'matrix( [1, 2], [3, 4] )', 'asinh' ),
      array( 'matrix( [1, 2], [3, 4] )', 'acosh' ),
      array( 'matrix( [1, 2], [3, 4] )', 'atanh' ),
      array( 'matrix( [1, 2], [3, 4] )', 'acoth' ),
      array( 'matrix( [1, 2], [3, 4] )', 'asech' ),
      array( 'matrix( [1, 2], [3, 4] )', 'acsch' )
   );

   /**
    * 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 )
   {
      $result = preg_replace( '/.*(?=\n\s*:\| expression \|:)/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*:\| result \|:)/s', '', $result );
      $result = preg_replace( '/\n`rat\' replaced[^\n]+\n/U', '', $result );
      return $result;
   }

   /**
    * Format user input to Maxima matrix
    * @param string $input unser input from textarea
    * @return string
    */
   function toMatrix( $input )
   {
      $inputs = explode( "\n", $input ); 
      $input = '';
      foreach ( $inputs as $in )
      {
         $in = trim( $in, ", \t\n\r\0\x0B" );
         if ( $in != '' ) $input .= "[$in], ";
      }
      $input = rtrim( $input, ', ' );
      if ( strpos( $input, ',' ) === FALSE )
         return trim( $input, '[]' );
      else
         return "matrix( $input )";
   }

   /**
    * Format Maxima matrix to user input 
    * @param string $matrix matrix from Maxima
    * @return string
    */
   function fromMatrix( $matrix )
   {
      $input = $matrix;
      if ( preg_match_all( '/\[(.*)\]/Us', $matrix, $matches ) )
      {
         $input = '';
         foreach ( $matches[1] as $match )
         {
            $match = preg_replace( '/\s+/', ' ', trim( $match ) );
            $input .= $match . "\n";
         }
      }
      return $input;
   }

   /**
    * Check if a variable is integer
    */
   function isInteger( $x ) 
   {
      return ( is_numeric( $x ) ? intval( $x ) == $x : false );
   }

   /**
    * 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 = $this->toMatrix( 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 = 'sin';
      }

      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 MPVMATRIXTRIGONOMETRY_TITLE ?></h3>
   <?php echo MPVMATRIXTRIGONOMETRY_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 MPVMATRIXTRIGONOMETRY_MAIN_1 ?> </td><td><textarea name="matrix" rows="10" cols="50"><?php echo htmlentities( $this->fromMatrix( $matrix ) ) ?></textarea><br /></td></tr>
   <tr><td valign="top"><?php echo MPVMATRIXTRIGONOMETRY_MAIN_2 ?> </td><td>
   <table width="100%">
   <tr><td colspan="6"><?php echo MPVMATRIXTRIGONOMETRY_MAIN_5 ?>
   </td></tr>
   <tr><td><input type="radio" name="function" value="sin" <?php echo $function == 'sin' ? 'checked="checked"' : '' ?> /> Sin
   </td><td><input type="radio" name="function" value="cos" <?php echo $function == 'cos' ? 'checked="checked"' : '' ?> /> Cos
   </td><td><input type="radio" name="function" value="tan" <?php echo $function == 'tan' ? 'checked="checked"' : '' ?> /> Tan
   </td><td><input type="radio" name="function" value="cot" <?php echo $function == 'cot' ? 'checked="checked"' : '' ?> /> Cot
   </td><td><input type="radio" name="function" value="sec" <?php echo $function == 'sec' ? 'checked="checked"' : '' ?> /> Sec
   </td><td><input type="radio" name="function" value="csc" <?php echo $function == 'csc' ? 'checked="checked"' : '' ?> /> Csc
   </td></tr>
   <tr><td colspan="6"><?php echo MPVMATRIXTRIGONOMETRY_MAIN_6 ?>
   </td></tr>
   <tr><td><input type="radio" name="function" value="asin" <?php echo $function == 'asin' ? 'checked="checked"' : '' ?> />ASin
   </td><td><input type="radio" name="function" value="acos" <?php echo $function == 'acos' ? 'checked="checked"' : '' ?> />ACos
   </td><td><input type="radio" name="function" value="atan" <?php echo $function == 'atan' ? 'checked="checked"' : '' ?> />ATan
   </td><td><input type="radio" name="function" value="acot" <?php echo $function == 'acot' ? 'checked="checked"' : '' ?> />ACot
   </td><td><input type="radio" name="function" value="asec" <?php echo $function == 'asec' ? 'checked="checked"' : '' ?> />ASec
   </td><td><input type="radio" name="function" value="acsc" <?php echo $function == 'acsc' ? 'checked="checked"' : '' ?> />ACsc
   </td></tr>
   <tr><td colspan="6"><?php echo MPVMATRIXTRIGONOMETRY_MAIN_7 ?>
   </td></tr>
   <tr><td><input type="radio" name="function" value="sinh" <?php echo $function == 'sinh' ? 'checked="checked"' : '' ?> /> Sinh
   </td><td><input type="radio" name="function" value="cosh" <?php echo $function == 'cosh' ? 'checked="checked"' : '' ?> /> Cosh
   </td><td><input type="radio" name="function" value="tanh" <?php echo $function == 'tanh' ? 'checked="checked"' : '' ?> /> Tanh
   </td><td><input type="radio" name="function" value="coth" <?php echo $function == 'coth' ? 'checked="checked"' : '' ?> /> Coth
   </td><td><input type="radio" name="function" value="sech" <?php echo $function == 'sech' ? 'checked="checked"' : '' ?> /> Sech
   </td><td><input type="radio" name="function" value="csch" <?php echo $function == 'csch' ? 'checked="checked"' : '' ?> /> Csch
   </td></tr>
   <tr><td colspan="6"><?php echo MPVMATRIXTRIGONOMETRY_MAIN_8 ?>
   </td></tr>
   <tr><td><input type="radio" name="function" value="asinh" <?php echo $function == 'asinh' ? 'checked="checked"' : '' ?> />ASinh
   </td><td><input type="radio" name="function" value="acosh" <?php echo $function == 'acosh' ? 'checked="checked"' : '' ?> />ACosh
   </td><td><input type="radio" name="function" value="atanh" <?php echo $function == 'atanh' ? 'checked="checked"' : '' ?> />ATanh
   </td><td><input type="radio" name="function" value="acoth" <?php echo $function == 'acoth' ? 'checked="checked"' : '' ?> />ACoth
   </td><td><input type="radio" name="function" value="asech" <?php echo $function == 'asech' ? 'checked="checked"' : '' ?> />ASech
   </td><td><input type="radio" name="function" value="acsch" <?php echo $function == 'acsch' ? 'checked="checked"' : '' ?> />ACsch
   </td></tr></table>
   </td></tr>
   <tr><td> </td><td align="right"><input type="checkbox" name="simplify" <?php echo $simplify ? 'checked="checked"' : '' ?> /> <?php echo MPVIEWER_SIMPLIFY ?> <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 != '' ) 
         {
            $matrix_funcs = '';
            if ( strpos( $matrix, ',' ) !== FALSE )
            {
               $matrix_funcs = <<<EOT


make_matrix(l):= block
(
 [d: length(l)-1], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   a[j, i]: l[i+1][j]
  )
 ),
 genmatrix( a, d, d )
);

make_diagonal(l):= block
(
 [d: lsum (i, i, l[2])], 
 [array (a, fixnum, d, d)],
 k: 1, t: 1,
 for i: 1 thru d do block
 (

  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: l[1][k],
    if (t = l[2][k]) then block(k: k + 1, t: 1)
    else t: t + 1
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);
EOT;
            }
            if ( $function == 'sin' )
            {
               $expression = '"sin", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'sin(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


sin_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: sin(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_sin(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.sin_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_sin(%lm)';
               }
            }
            else if ( $function == 'cos' )
            {
               $expression = '"cos", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'cos(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


cos_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: cos(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_cos(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.cos_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_cos(%lm)';
               }
            }
            else if ( $function == 'tan' )
            {
               $expression = '"tan", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'tan(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


tan_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: tan(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_tan(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.tan_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_tan(%lm)';
               }
            }
            else if ( $function == 'cot' )
            {
               $expression = '"cot", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'cot(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


cot_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: cot(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_cot(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.cot_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_cot(%lm)';
               }
            }
            else if ( $function == 'sec' )
            {
               $expression = '"sec", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'sec(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


sec_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: sec(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_sec(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.sec_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_sec(%lm)';
               }
            }
            else if ( $function == 'csc' )
            {
               $expression = '"csc", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'csc(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


csc_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: csc(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_csc(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.csc_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_csc(%lm)';
               }
            }
            else if ( $function == 'asin' )
            {
               $expression = '"asin", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'asin(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


asin_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: asin(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_asin(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.asin_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_asin(%lm)';
               }
            }
            else if ( $function == 'acos' )
            {
               $expression = '"acos", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'acos(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


acos_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: acos(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_acos(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.acos_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_acos(%lm)';
               }
            }
            else if ( $function == 'atan' )
            {
               $expression = '"atan", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'atan(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


atan_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: atan(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_atan(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.atan_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_atan(%lm)';
               }
            }
            else if ( $function == 'acot' )
            {
               $expression = '"acot", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'acot(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


acot_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: acot(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_acot(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.acot_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_acot(%lm)';
               }
            }
            else if ( $function == 'asec' )
            {
               $expression = '"asec", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'asec(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


asec_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: asec(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_asec(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.asec_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_asec(%lm)';
               }
            }
            else if ( $function == 'acsc' )
            {
               $expression = '"acsc", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'acsc(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


acsc_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: acsc(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_acsc(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.acsc_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_acsc(%lm)';
               }
            }
            else if ( $function == 'sinh' )
            {
               $expression = '"sinh", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'sinh(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


sinh_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: sinh(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_sinh(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.sinh_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_sinh(%lm)';
               }
            }
            else if ( $function == 'cosh' )
            {
               $expression = '"cosh", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'cosh(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


cosh_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: cosh(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_cosh(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.cosh_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_cosh(%lm)';
               }
            }
            else if ( $function == 'tanh' )
            {
               $expression = '"tanh", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'tanh(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


tanh_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: tanh(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_tanh(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.tanh_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_tanh(%lm)';
               }
            }
            else if ( $function == 'coth' )
            {
               $expression = '"coth", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'coth(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


coth_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: coth(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_coth(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.coth_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_coth(%lm)';
               }
            }
            else if ( $function == 'sech' )
            {
               $expression = '"sech", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'sech(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


sech_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: sech(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_sech(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.sech_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_sech(%lm)';
               }
            }
            else if ( $function == 'csch' )
            {
               $expression = '"csch", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'csch(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


csch_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: csch(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_csch(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.csch_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_csch(%lm)';
               }
            }
            else if ( $function == 'asinh' )
            {
               $expression = '"asinh", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'asinh(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


asinh_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: asinh(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_asinh(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.asinh_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_asinh(%lm)';
               }
            }
            else if ( $function == 'acosh' )
            {
               $expression = '"acosh", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'acosh(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


acosh_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: acosh(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_acosh(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.acosh_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_acosh(%lm)';
               }
            }
            else if ( $function == 'atanh' )
            {
               $expression = '"atanh", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'atanh(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


atanh_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: atanh(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_atanh(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.atanh_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_atanh(%lm)';
               }
            }
            else if ( $function == 'acoth' )
            {
               $expression = '"acoth", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'acoth(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


acoth_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: acoth(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_acoth(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.acoth_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_acoth(%lm)';
               }
            }
            else if ( $function == 'asech' )
            {
               $expression = '"asech", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'asech(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


asech_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: asech(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_asech(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.asech_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_asech(%lm)';
               }
            }
            else if ( $function == 'acsch' )
            {
               $expression = '"acsch", ##MATRIX##';
               if ( strpos( $matrix, ',' ) === FALSE )
               {
                  $calculation = 'acsch(%lm)';
               }
               else
               {
                  $matrix_funcs .= <<<EOT


acsch_diagonal(m):= block
(
 [d: length(m)], 
 [array (a, fixnum, d, d)],
 for i: 1 thru d do block
 (
  for j: 1 thru d do block
  (
   if (i = j) then block
   (
    a[i, j]: acsch(m[i, j])
   )
   else block
   (
    a[i, j]: 0
   )
  )
 ),
 genmatrix( a, d, d )
);

matrix_acsch(m):= block
(
 [ev: eigenvectors(m)],
 [d: make_diagonal(ev[1])],
 [p: make_matrix(ev)],
 p.acsch_diagonal(d).invert(p)
);
EOT;
                  $calculation = 'matrix_acsch(%lm)';
               }
            }


            $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,%%)';
            $rational_code = <<<EOT

rational(z) := block
(
 [n: num(z)],
 [d: denom(z)],
 [c: conjugate(d)],
 expand((n*c)/(expand(d*c)))
);
EOT;
            $tomaxima = <<<EOT
/*
 * Put your Maxima code to do calculation here
 * it should be free of unsecure commands
 */##RATIONAL_CODE####MATRIX_FUNCS##
block
(
 disp(":| expression |:"),
 %lm: rationalize(##MATRIX##),
 [##EXPRESSION##]
);
disp(":|o|:");
block
(
 disp(":| result |:"),
 ##CALCULATION##
)##EV_SIMP##
EOT;
            $_SESSION['last_input'] = $matrix;
            $_SESSION['function'] = $function;

            /**
             * Do some little pre-processing combining $matrix and $tomaxima such as
             */
            $tomaxima = str_replace( '##EXPRESSION##', $expression, $tomaxima );
            $tomaxima = str_replace( '##MATRIX##', $matrix, $tomaxima );
            $tomaxima = str_replace( '##MATRIX_FUNCS##', $matrix_funcs, $tomaxima );
            $tomaxima = str_replace( '##CALCULATION##', $calculation, $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 );
            $tomaxima = str_replace( '##RATIONAL_CODE##', $numeric ? $rational_code : '', $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 MPVMATRIXTRIGONOMETRY_MAIN_3 ?><br /><br />
<textarea rows="20" cols="60" style="border:none" wrap="off" readonly="readonly">
<?php echo MPVMATRIXTRIGONOMETRY_MAIN_4 . "\n" . htmlentities( $tomaxima ) ?>
</textarea>

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

?>

<?php
   }
}

?>
Return current item: MaximaPHP