Documentation Center

  • Trial Software
  • Product Updates

Test Arguments

Check Types of Arguments

When writing a MuPAD® procedure, you can specify the type of arguments accepted by the procedure. To define a procedure that accepts only particular types of arguments, specify the expected types when defining the formal parameters of a procedure. For example, the formal parameter of the following procedure accepts only those arguments that belong to the domain DOM_INT:

f:= proc(k:DOM_INT)
begin
  sin(PI*k/4)
end_proc:

Therefore, only an integer number is a valid first argument for this procedure:

f(1)

The system compares the type of the formal parameter k and the type of an argument passed to the procedure. If the first argument that you pass to the procedure f is not an integer, MuPAD issues an error:

f(2/3)
Error: The type of argument number 1 must be 'DOM_INT'. The object '2/3' is incorrect.
  Evaluating: f

During a typical procedure call, for example a call to the solve or int function, MuPAD internally calls many other procedures. Testing argument types for each internal procedure call is computationally expensive. To provide better performance, MuPAD reduces the amount of type checks in the running code. By default, the system checks the types of arguments only in those procedures that you call interactively. If one procedure internally calls another procedure, MuPAD does not check types of the arguments of the internally called procedure. For example, create the following procedure g as a wrapper for the procedure f:

g := proc(n:Type::Numeric) begin f(n) end_proc:

MuPAD performs type checks only for the arguments of the procedure g, which you call interactively. It does not perform type checks for the arguments of the procedure f:

g(2/3)

The Pref::typeCheck function enables you to control type checking of procedure arguments. This function affects all procedures. It does not allow you to control type checking for individual procedures. The default setting of Pref::typeCheck is Interactive:

Pref::typeCheck()

To perform type checks in all procedure calls, including internal calls, set the value of Pref::typeCheck to Always:

Pref::typeCheck(Always):

Now, the system realizes that 2/3 is not a valid argument of the internally called procedure f:

g(2/3)
Error: The type of argument number 1 must be 'DOM_INT'. The object '2/3' is incorrect.
  Evaluating: f

To disable type checks in all procedure calls, including interactive calls, set the value of Pref::typeCheck to None:

Pref::typeCheck(None):

Now, the system does not check argument types in any procedure calls:

g(2/3), f(2/3)

To restore the default setting of Pref::typeCheck, use the NIL option:

Pref::typeCheck(NIL):

Check Arguments of Individual Procedures

When writing a procedure in MuPAD, you can include your own tests for the procedure arguments inside the procedure itself. You can test types, values, or any other properties of the arguments of a procedure. For example, create the procedure that calculates the arcsine function of a real number. Suppose, you want to limit possible results of this procedure to real numbers. In this case, an input argument of the procedure must belong to the interval [-1,1]. To ensure that the procedure accepts only the values from this interval, test the value of an input argument inside the procedure:

p := proc(x:Dom::Real)
begin
  if abs(x) > 1 then
    error("invalid number. Choose a value from the interval [-1,1].");
  end_if;
  arcsin(x)
end_proc:

Typically, when you call one MuPAD procedure, that procedure internally calls other MuPAD procedures. Some of these internal calls are multiple calls to the same procedure with different sets of arguments. Testing arguments for each internal procedure call can become computationally expensive. By default, the system uses the following general principle for testing arguments of a typical MuPAD procedure:

  • If you call a procedure interactively, the procedure performs all argument checks.

  • If one procedure internally calls another procedure, the second procedure skips argument checks.

Currently, the procedure p always checks whether the value of its argument belongs to the interval [-1,1]. To follow the general principle for testing arguments, the procedure must be able to recognize internal and interactive calls, and skip argument checking when a call is internal. For this task, MuPAD provides the testargs function. When you call testargs inside an interactive procedure call, testargs returns the value TRUE. For internal procedure calls, testargs returns the value FALSE by default. For example, rewrite your procedure p as follows:

p := proc(x)
begin
  if testargs() then
    if abs(x) > 1 then
      error("invalid number. Choose a value from the interval [-1,1].");
    end_if;
  end_if;
  arcsin(x)
end_proc:

When you call the procedure p, it checks whether the input argument belongs to the specified interval:

p(1/2), p(1), p(0)

p(10)
Error: invalid number. Choose a value from the interval [-1,1]. [p]

Now, write the simple wrapper procedure f that calls the procedure p:

f := proc(x) begin p(x) end_proc:

When the wrapper procedure f calls p, the procedure p does not check its arguments because testargs returns the value FALSE:

f(10)

The testargs function also allows you to switch to the argument checking mode. In this mode, MuPAD checks arguments of all procedures, regardless of how a procedure is called. This mode can slow down your computations. Use this mode only for debugging your code. To switch to the argument checking mode, set the value of testargs to TRUE:

testargs(TRUE):

In the argument checking mode, the procedure p checks its argument during interactive and internal calls:

p(10)
Error: invalid number. Choose a value from the interval [-1,1]. [p]
f(10)
Error: invalid number. Choose a value from the interval [-1,1]. [p]

Always restore testargs to its default value FALSE after you finish debugging:

testargs(FALSE):
Was this topic helpful?