<?php
/**
* mpvfuncsolve.php, MaximaPHP Viewer - Function Solve
*
* Function Solve viewer for MaximaPHP.
*
* This file contains MPVFuncSolve class.
* @author Bowo Prasetyo <http://www.my-tool.com/maximaphp/equations/funcsolve>
* @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.
*
**************************************************************/
/**
* Function Solve viewer class for MaximaPHP.
*
*/
class MPVFuncSolve
{
/**
* Examples for expression
*/
var $examples = array
(
array( '(t + 1)*f(t) - (t + 3)*f(t + 1)/(t + 1) = (t - 1)/(t + 2)', 'f(t)' )
);
/**
* 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] ) . "</pre>\n";
}
$ex_out .= "\n";
return $ex_out;
}
/**
* Format output from MaximaPHP
* @param string $result output from MaximaPHP
* @return string
*/
function formatOutput( $result )
{
$result = preg_replace( '/.*(?=\n\s*:\| equation \|:)/s', '', $result );
$result = preg_replace( '/\(%i[0-9]+\).*(?=\n\s*:\| solution \|:)/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['equation'] ) )
$equation = $_POST['equation'];
else
$equation = '';
if ( isset( $_POST['function'] ) )
$function = $_POST['function'];
else
{
if ( isset( $_SESSION['function'] ) )
$function = $_SESSION['function'];
else
$function = '';
}
if ( isset( $_POST['example'] ) )
{
$last_input = '';
$equation = '';
}
if ( $equation == '' )
{
if ( $last_input != '' ) $equation = $last_input;
else
{
$m = mt_rand( 0, count( $this->examples ) - 1 );
$equation = $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 MPVFUNCSOLVE_TITLE ?></h3>
<?php echo MPVFUNCSOLVE_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 MPVFUNCSOLVE_MAIN_1 ?> </td><td><input type="text" name="equation" size="60" value="<?php echo $equation ?>" /><br />
<span class="eg"><?php echo MPVIEWER_EG ?> (t + 1)*f(t) - (t + 3)*f(t + 1)/(t + 1) = (t - 1)/(t + 2)</span></td></tr>
<tr><td><?php echo MPVFUNCSOLVE_MAIN_2 ?> </td><td><input type="text" name="function" size="40" value="<?php echo $function ?>" /> <span class="eg"><?php echo MPVIEWER_EG ?> f(t)</span></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' );
$maximaphp = new MaximaPHP( $equation, $last_input, $last_output );
$equation = $maximaphp->secure();
$maximaphp->setInput( $function );
$function = $maximaphp->secure();
/**
* Prints error message in case unsecure input
*/
if ( !$maximaphp->isSecure() )
{
$maximaphp->getSecurityWarning();
}
/**
* Otherwise send input to Maxima
*/
else if ( $equation != '' )
{
$tomaxima = <<<EOT
block
(
disp(":| equation |:"),
##EQUATION##
);
disp(":|o|:");
block
(
disp(":| solution |:"),
funcsolve(##EQUATION##, ##FUNCTION##)
)
EOT;
$_SESSION['last_input'] = $equation;
$_SESSION['function'] = $function;
/**
* Do some little pre-processing combining $equation and $tomaxima such as
*/
$tomaxima = str_replace( '##EQUATION##', $equation, $tomaxima );
$tomaxima = str_replace( '##FUNCTION##', $function, $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 );
$_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 MPVFUNCSOLVE_MAIN_3 ?><br /><br />
<textarea rows="20" cols="60" style="border:none" wrap="off" readonly="readonly">
<?php echo MPVFUNCSOLVE_MAIN_4 . "\n" . htmlentities( $tomaxima ) ?>
</textarea>
<?php
}
}
}
/**
* Print out examples
*/
echo $this->getExamples( $view == 'examples' ? 0 : 5 );
?>
<?php
}
}
?>