<?php
/**
* mpvintegrate.php, MaximaPHP Viewer - Integrate
*
* Integrate viewer for MaximaPHP.
*
* This file contains MPVIntegrate class.
* @author Bowo Prasetyo <http://www.my-tool.com/maximaphp/calculus/integrate>
* @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.
*
**************************************************************/
/**
* Integrate viewer class for MaximaPHP.
*
*/
class MPVIntegrate
{
/**
* Examples for expression
*/
var $examples = array
(
array( 'sin(x)^3', 'x' ),
array( 'x/sqrt(b^2 - x^2)', 'x' ),
array( 'cos(x)^2 * exp(x)', 'x', '0', '%pi'),
array( 'x^2 * exp(-x^2)', 'x', 'minf', 'inf')
);
/**
* 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++ )
{
if ( count($this->examples[$i]) == 2 )
$ex_out .= "<pre style=\"background-color: #f0f9ff; border: 1px solid #cccccc; padding: 1px;\">" . htmlentities( $this->examples[$i][0] ) . " in respect to " . htmlentities( $this->examples[$i][1] ) . "</pre>\n";
else
$ex_out .= "<pre style=\"background-color: #f0f9ff; border: 1px solid #cccccc; padding: 1px;\">" . htmlentities( $this->examples[$i][0] ) . " in respect to " . htmlentities( $this->examples[$i][1] ) . "\ndefinite from " . htmlentities( $this->examples[$i][3] ) . " to " . htmlentities( $this->examples[$i][3] ) . "</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 ( $numeric or $simplify )
$result = preg_replace( '/\(%i[0-9]+\).*(?=\n\s*\:\| simplified \|:)/s', '', $result );
else
$result = preg_replace( '/\(%i[0-9]+\).*(?=\n\s*:\| integral \|:)/s', '', $result );
$result = preg_replace( '/\n`rat\' replaced[^\n]+\n/U', '', $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['expression'] ) )
$expression = $_POST['expression'];
else
$expression = '';
if ( isset( $_POST['variable'] ) )
$variable = $_POST['variable'];
else
{
if ( isset( $_SESSION['variable'] ) )
$variable = $_SESSION['variable'];
else
$variable = '';
}
if ( isset( $_POST['simplify'] ) )
$simplify = TRUE;
else
$simplify = FALSE;
if ( isset( $_POST['numeric'] ) )
$numeric = TRUE;
else
$numeric = FALSE;
if ( isset( $_POST['definite'] ) )
$definite = TRUE;
else
$definite = FALSE;
if ( isset( $_POST['from'] ) )
$from = $_POST['from'];
else
{
if ( isset( $_SESSION['from'] ) )
$from = $_SESSION['from'];
else
$from = '';
}
if ( isset( $_POST['to'] ) )
$to = $_POST['to'];
else
{
if ( isset( $_SESSION['to'] ) )
$to = $_SESSION['to'];
else
$to = '';
}
if ( isset( $_POST['example'] ) )
{
$last_input = '';
$expression = '';
}
if ( $expression == '' )
{
if ( $last_input != '' ) $expression = $last_input;
else
{
$m = mt_rand( 0, count( $this->examples ) - 1 );
$expression = $this->examples[$m][0];
$variable = $this->examples[$m][1];
$definite = FALSE;
$from = '';
$to = '';
if ( count( $this->examples[$m] ) > 2 )
{
$definite = TRUE;
$from = $this->examples[$m][2];
$to = $this->examples[$m][3];
}
}
}
/**
* 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 MPVINTEGRATE_TITLE ?></h3>
<?php echo MPVINTEGRATE_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 MPVINTEGRATE_MAIN_1 ?> </td><td colspan="2"><input type="text" name="expression" size="60" value="<?php echo $expression ?>" /><br /><span class="eg"><?php echo MPVIEWER_EG ?> x/sqrt(c^2 - x^2)</span></td></tr>
<tr><td><?php echo MPVINTEGRATE_MAIN_2 ?> </td><td><input type="text" name="variable" size="5" value="<?php echo $variable ?>" /> <span class="eg"><?php echo MPVIEWER_EG ?> x</span></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> </td><td colspan="2"><input type="checkbox" name="definite" <?php echo $definite ? 'checked="checked"' : '' ?> /> <?php echo MPVINTEGRATE_MAIN_3 ?>
<?php echo MPVINTEGRATE_MAIN_4 ?> <input type="text" name="from" size="5" value="<?php echo $from ?>" />
<?php echo MPVINTEGRATE_MAIN_5 ?> <input type="text" name="to" size="5" value="<?php echo $to ?>" /> (<span class="eg"><?php echo MPVIEWER_EG ?> a to b</span>)
</td></tr>
<tr><td align="left"><input type="submit" name="submit" /></td><td colspan="2" 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' );
$maximaphp = new MaximaPHP( $expression, $last_input, $last_output );
$expression = $maximaphp->secure();
$maximaphp->setInput( $variable );
$variable = $maximaphp->secure();
$maximaphp->setInput( $from );
$from = $maximaphp->secure();
$maximaphp->setInput( $to );
$to = $maximaphp->secure();
/**
* Prints error message in case unsecure input
*/
if ( !$maximaphp->isSecure() )
{
$maximaphp->getSecurityWarning();
}
/**
* Otherwise send input to Maxima
*/
else if ( $expression != '' )
{
$eq = "##EXPRESSION##, ##VARIABLE##";
$eq_def = "##EXPRESSION##, ##VARIABLE##, ##FROM##, ##TO##";
$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
rational(z) := block
(
[n: num(z)],
[d: denom(z)],
[c: conjugate(d)],
expand((n*c)/(expand(d*c)))
);
block
(
disp(":| expression |:"),
[##EQ_DEF##]
);
disp(":|o|:");
block
(
disp(":| integral |:"),
integrate(##EQ_DEF##)
)##EV_SIMP##
EOT;
$_SESSION['last_input'] = $expression;
$_SESSION['variable'] = $variable;
$_SESSION['from'] = $from;
$_SESSION['to'] = $to;
/**
* Do some little pre-processing combining $expression and $tomaxima such as
*/
if ( $definite )
$tomaxima = str_replace( '##EQ_DEF##', $eq_def, $tomaxima );
else
$tomaxima = str_replace( '##EQ_DEF##', $eq, $tomaxima );
$tomaxima = str_replace( '##EXPRESSION##', $expression, $tomaxima );
$tomaxima = str_replace( '##VARIABLE##', $variable, $tomaxima );
$tomaxima = str_replace( '##FROM##', $from, $tomaxima );
$tomaxima = str_replace( '##TO##', $to, $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 MPVINTEGRATE_MAIN_6 ?><br /><br />
<textarea rows="20" cols="60" style="border:none" wrap="off" readonly="readonly">
<?php echo MPVINTEGRATE_MAIN_7 . "\n" . htmlentities( $tomaxima ) ?>
</textarea>
<?php
}
}
}
/**
* Print out examples
*/
echo $this->getExamples( $view == 'examples' ? 0 : 5 );
?>
<?php
}
}
?>