input
-- interactive input of
MuPAD objectsinput
allows interactive input of MuPAD
objects.
input( <prompt1>)
input( <prompt1,> x1, <prompt2,> x2...)
prompt1, prompt2... |
- | input prompts: character strings |
x1, x2... |
- | identifiers |
the last input
finput
, fprint
, fread
, ftextinput
, print
, read
, text2expr
, textinput
, write
input(
)
displays the prompt
``Please enter expression :
'' and waits for input by the
user. The input, terminated by pressing the <Return>
key, is parsed and returned unevaluatedly.input(
prompt1)
uses the character string
prompt1
instead of the default prompt ``Please enter
expression :
''.input(
<prompt1,> x1)
assigns the
input to the identifier x1
. The default prompt is used, if
no prompt string is specified.input
command. Each identifier in the sequence of arguments makes
input
return a prompt, waiting for input to be assigned to
the identifier. A character string preceding the identifier in the
argument sequence replaces the default prompt (see example 2). Arguments that are neither prompt strings nor
identifiers are ignored.x1
etc. may have values. These are
overwritten by input
.input
only parses the input objects for syntactical
correctness. It does not evaluate them. Use eval
to evaluate the results (see
example 3).input
is a function of the system kernel.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:
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:
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: