I'm new to CAS and maxima. I'd like to know whether it's feasible to do the following:

1) I have a list of parameters e.g. a, b, c

2) In PHP, I have some maxima script stored as a string, involving a, b, c, e.g.:

do {
  a=random(20);
  b=random(20);
  c=random(20);
}while (!(a > b) || !(b > c))

Such that a, b, c are randomized to desired values and satisfy requirements.

3) Retrieve the values of a, b, c in PHP.

The purpose is to create randomized questions with reasonable parameters for students. So how can I execute the maxima script and retrieve the values of parameters? Is it suitable for my purpose?

有帮助吗?

解决方案 2

I dont really know how your code works but if you save the maxima as a php extension it could work. Place this line of code at the start of the php file

      <?php
       require_once("extension/Maxima.php");

       ?>

For the echo example

              echo $A ;

其他提示

You can pass the command string in a file to Maxima, that is supported by command-line Maxima.

if your OS is Linux/Unix/MacOS:

In PHP:

exec('maxima -q -b file');

or

system('maxima -q -b file');

if your OS is Win:

$maximaDir = 'D:/Program Files/Maxima-5.30.0'; // If your maxima is installed in elsewhere, please modified this location

exec($maximaDir.'/bin/maxima.bat -q -b "result.txt"');

In Maxima, you can use stringout(); to get the result in a file, then in PHP read the file as a string, you can do any other manipulations to the string as you want.

<?php

require_once($_SERVER['PM_BASE_CONFIG_PATH']);

class maxima_core {
  private $executable_command;
  protected $dbg_bool;
  protected $dbg_info;
  public function __construct($dbg=FALSE){
    $this->executable_command=constant('PM_MAXIMA_EXEC_CMD');
    $this->dbg_bool=$dbg;
    $this->dbg_info="";
  }
  protected function exec($query){// to include package that is loaded by init_command
    $descriptor = array(
      0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
      1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
      2 => array("file", "/tmp/error-log.txt", "a")//constant('PM_SERVER_LOG_DIR')."/maxima/error.log", "a") // stderr is a file to write to
    );
    $cwd=constant('PM_ACTIVITY_PLUGIN_URL')."/engine_solver";
    $MAXIMA_DIR = constant('PM_ACTIVITY_PLUGIN_DIR');
    $env=array();
    $init_command="display2d:false$" . "PM_ACTIVITY_PLUGIN_URL: \"" . $MAXIMA_DIR . "\"$";
    //'load("/home/gabriel/github/moodledata/stack/maximalocal.mac");';
    $exec_cmd=$this->executable_command." --quiet";
    // --userdir='".constant('PM_ACTIVITY_PLUGIN_DIR')."/engine_solver/maxima_userdir'";
    // change
    $result=NULL;
    $process=proc_open($exec_cmd,$descriptor,$pipes,$cwd,$env);
    if(is_resource($process)){
      if (!fwrite($pipes[0], $init_command)) {
        echo "<br />Could not write to the CAS process!<br />\n";
      } else {
        fwrite($pipes[0], $query);
        fwrite($pipes[0], "quit();");
        fclose($pipes[0]);
        $result=stream_get_contents($pipes[1]);
        fclose($pipes[1]);
        proc_close($process);
      }
    }
    return $result;
  }
  public function dbg_info(){
    return $this->dbg_info;
  }
}
?>

If the goal is to use Maxima to create homework problems which are presented by a web server, I think that there have been projects to do just that. I think one of them is named LON-CAPA -- a web search should find it. There might be some other projects mentioned on the related projects page at the Maxima web site. [1]

[1] http://maxima.sf.net/relatedprojects.html

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top