Previous Page Next Page Contents

input -- interactive input of MuPAD objects

Introduction

input allows interactive input of MuPAD objects.

Call(s)

input( <prompt1>)
input( <prompt1,> x1, <prompt2,> x2...)

Parameters

prompt1, prompt2... - input prompts: character strings
x1, x2... - identifiers

Returns

the last input

Related Functions

finput, fprint, fread, ftextinput, print, read, text2expr, textinput, write

Details

Example 1

The default prompt is displayed. The input is returned without evaluation:

>> input()
      Please enter expression : << 1 + 2 >>
      
                                   1 + 2

A character string is used as a prompt:

>> input("enter a number: ")
      enter a number: << 5 >>
      
                                     5

The input may be assigned to an identifier:

>> input(x)
      Please enter expression : << 5 >>
      
                                     5
>> x
                                     5

A user-defined prompt is used, the input is assigned to an identifier:

>> input("enter a number: ", x)
      enter a number: << 6 >>
      
                                     6
>> x
                                     6
>> delete x:

Example 2

If several objects are to be read, for each object a separate prompt can be defined:

>> input("enter a matrix: ", A, "enter a vector: ", x)
      enter a matrix: << matrix([[a11, a12], [a21, a22]]) >>
      enter a vector: << matrix([x1, x2]) >>
      
                             matrix([x1, x2])
>> A, x
                         +-          -+  +-    -+
                         |  a11, a12  |  |  x1  |
                         |            |, |      |
                         |  a21, a22  |  |  x2  |
                         +-          -+  +-    -+
>> delete A, x:

Example 3

The following procedure asks for an expression and a variable. After interactive input, the derivative of the expression with respect to the variable is computed:

>> interactiveDiff :=
     proc() 
       local f, x;
     begin
        f := input("enter an expression: ");
        x := input("enter an identifier: ");
        print(Unquoted, "The derivative of " . expr2text(f) .
              " with respect to ". expr2text(x) . " is:");
        diff(f, x)
     end_proc:
>> interactiveDiff()
      enter an expression: << x^2 + x*y^3 >>
      enter an identifier: << x >>
      
            The derivative of x^2 + x*y^3 with respect to x is:
      
                                        3
                                 2 x + y

The function input does not evaluate the input. This leads to the following unexpected result:

>> f := x^2 + x*y^3:
   z := x:
   interactiveDiff()
      enter an expression: << f >>
      enter an identifier: << z >>
      
                 The derivative of f with respect to z is:
      
                                     0

The following modification enforces full evaluation via eval:

>> interactiveDiff :=
     proc() 
       local f, x;
     begin
        f := eval(input("enter an expression: "));
        x := eval(input("enter an identifier: "));
        print(Unquoted, "The derivative of " . expr2text(f) .
              " with respect to ". expr2text(x) . " is:");
        diff(f, x)
     end_proc:
>> interactiveDiff()
      enter an expression: << f >>
      enter an identifier: << z >>
      
            The derivative of x^2 + x*y^3 with respect to x is:
      
                                        3
                                 2 x + y
>> delete interactiveDiff, f, z:

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000