Sample Maplet Application: Vector Norm
This worksheet demonstrates how to write a Maplet application that functions similarly to the LinearAlgebra[VectorNorm] Maplet application available in the Maplets[Examples] package. It is designed for experienced Maple authors.
The VectorNorm Maplet application provides a graphical interface to the LinearAlgebra[VectorNorm] function.
The vector norm of a vector V is a function ||V|| that satisfies all the conditions of a norm.
By using user input, this procedure returns the calling sequence required to find the vector norm or evaluates the result.
Thus, the Maplet application requests:
1. The norm, one of: infinity, 1, Euclidean (2), Frobenius, or any non-negative value.
2. Whether the vector is real-valued.
3. Whether the result is to be evaluated (or the calling sequence is to be returned).
|
Example Code
|
|
Invoke the Maplets Elements subpackage.
>
|
with(Maplets[Elements]):
|
Since it is not most efficient to enter a vector in a Maplet application, the Maplets[Examples][LinearAlgebra][VectorNorm] example Maplet application requires that the user include the vector as an input.
For this example, the following vector is used.
This example displays a Maplet application that queries the user for the inputs required, and then processes the results.
The variable continue is used to determine whether a previous section failed as a result of errors in user input or a Cancel click in either Maplet application.
>
|
# Define the Maplet application.
maplet := Maplet(BoxLayout('vertical'=true,
# A paragraph describing the vector norm.
TextBox( "A vector norm is a function ||V|| that satisfies "
"the conditions of a norm.",
'height' = 2, 'width' = 40, 'editable' = 'false' ),
# Three rows, each requesting the user for various
# forms of input. The first check box has a default
# value determined by the return value of a Maple call.
# The second check box is selected by default.
BoxRow(
BoxCell( "Norm (Select or enter a non-negative number):",
'halign'='left' ),
BoxCell( ComboBox['CoB1'](["infinity", "1", "Euclidean (2)",
"Frobenius"]), 'halign'='right' )
),
BoxRow(
BoxCell( "The vector has real entries:", 'halign'='left' ),
BoxCell( CheckBox['ChB1'](not has(V, I)), 'halign'='right' )
),
BoxRow(
BoxCell( "Evaluate result:", 'halign'='left' ),
BoxCell( CheckBox['ChB2'](true), 'halign'='right' )
),
BoxRow(
# The OK button shuts down the Maplet application and
# returns the results in the combo and check boxes
BoxCell( Button("OK", Shutdown(['CoB1', 'ChB1', 'ChB2'])),
'halign'='left' ),
# The Cancel button shuts down the Maplet application
# without returning any results.
BoxCell( Button("Cancel", Shutdown()), 'halign'='right' )
)
)):
|
>
|
# Display runs the Maplet application.
continue := false;
result := Maplets[Display](maplet);
|
Once the result from the Maplet application has been returned, check if the user clicked Cancel (by checking for a NULL result) or OK.
If the user selected OK, analyze the results. The user input need only be parsed if it is not one of the entries already on the combo box.
If the user indicates that the vector has real entries, then the conjugate option need not be set.
>
|
if type( result, list ) then
n := subs( ["infinity" = infinity, "1" = 1, "Euclidean (2)" = 2, "Frobenius" = Frobenius], result[1] );
if type( n, 'string' ) then
try
n := parse( n );
catch:
n := Maplets[Examples][GetInput]( "The following, expected to be of type `nonnegative`, does not parse:",
'value' = n );
if type( n, 'string' ) then
try
n := parse( n );
catch:
continue := false;
error "the expression `%1` does not parse", n;
end try;
end if;
end try;
if not type( n, 'nonnegative' ) then
continue := false;
error "the expression `%1` is not of type `%2`", n, nonnegative;
end if;
end if;
c := not parse( result[2] );
er := parse( result[3] );
continue := true;
else
continue := false;
end if:
|
Finally, we set the options, and assign the answer depending on whether the user requested an evaluation.
For this example, since answer is global, it evaluates fully. The extra level of unevaluation quotes is not necessary inside a procedure.
>
|
if continue then
if er then
answer := LinearAlgebra[VectorNorm]( V, n, conjugate = c );
else
answer := ''LinearAlgebra[VectorNorm]''( V, n, conjugate = c );
end if;
end if:
answer;
|
|
|
Maplets[Examples][LinearAlgebra][VectorNorm]
|
|
The Maplets[Examples][LinearAlgebra][VectorNorm] displays a Maplet application similar to that of the previous examples, though everything is wrapped in a procedure.
For help on this Maplet application, see:
>
|
?Maplets,Examples,VectorNorm
|
To view the source code, enter:
>
|
print( Maplets[Examples][LinearAlgebra][VectorNorm] );
|
|
Return to Index for Example Worksheets
|