Sample Maplet Application: Hilbert Matrix
This worksheet demonstrates how to write a Maplet application that functions similarly to the LinearAlgebra[HilbertMatrix] Maplet application available in the Maplets[Examples] package. It is designed for experienced Maple authors.
The HilbertMatrix Maplet application provides a graphical interface to the LinearAlgebra[HilbertMatrix] function.
Given an offset s, the Hilbert matrix H is a matrix with entries Hi,j=1i+j−s. The default value is s=1. The only outputoption that can be used in this case is to set the matrix to readonly. By using user input, this procedure returns the calling sequence required to generate the desired Hilbert matrix or evaluates the result.
Thus, the Maplet application requests:
1. The row dimension.
2. The column dimension.
3. The offset.
4. Whether the matrix is read-only.
5. Whether the result is to be evaluated (or the calling sequence is to be returned).
Example Code
restart:
Invoke the Maplets Elements subpackage.
with(Maplets[Elements]):
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 Hilbert matrix. TextBox( "A Hilbert matrix with offset `s` is a matrix H with entries" " defined by H[i,j] = 1/(i+j-s).", 'height' = 3, 'width' = 35, 'editable' = 'false' ), # Five rows, each requesting the user for various # forms of input. The third text field has a default # value of 1. The second check box is selected # by default. BoxRow( BoxCell( "Row dimension:", 'halign'='left' ), BoxCell( TextField['TF1'](3, 'width'=5), 'halign'='right' ) ), BoxRow( BoxCell( "Column dimension:", 'halign'='left' ), BoxCell( TextField['TF2'](3, 'width'=5), 'halign'='right' ) ), BoxRow( BoxCell( "Offset:", 'halign'='left' ), BoxCell( TextField[TF3]("1", 'width'=10 ), 'halign'='right' ) ), BoxRow( BoxCell( "Return read-only matrix:", 'halign'='left' ), BoxCell( CheckBox['ChB1'](), '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 text fields and check boxes BoxCell( Button("OK", Shutdown(['TF1', 'TF2', 'TF3', '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, attempt to parse the results. As well, if the results do not parse, query the user for a correction by using the Maplets[Examples][GetInput] routine. The values of the check boxes should always parse because each result is the string true or false.
continue := true: if result = NULL then continue := false; else try r := parse( result[1] ); catch: try r := Maplets[Examples][GetInput]( "The row dimension does not parse:", 'value' = result[1] ); if type( r, 'string' ) then r := parse( r ); else error; end if; catch: continue := false; error "the row dimension does not parse: %0", result[1]; end try; end try; try c := parse( result[2] ); catch: try c := Maplets[Examples][GetInput]( "The column dimension does not parse:", 'value' = result[2] ); if type( c, 'string' ) then c := parse( c ); else error; end if; catch: continue := false; error "the column dimension does not parse: %0", result[2]; end try; end try; try s := parse( result[3] ); catch: try s := Maplets[Examples][GetInput]( "The offset does not parse:", 'value' = result[3] ); if type( s, 'string' ) then s := parse( s ); else error; end if; catch: continue := false; error "the offset does not parse: %0", result[3]; end try; end try; ro := parse( result[4] ); er := parse( result[5] ); [r, c, s, ro, er]; end if;
Next, the parsed values should be checked to verify that they are of the correct type.
if continue then if not type( [r], ['nonnegint'] ) then continue := false; error "the 1st entry `%1` is not of type `%2`", result[1], 'nonnegint'; end if; if not type( [c], ['nonnegint'] ) then continue := false; error "the 2nd entry `%1` is not of type `%2`", result[2], 'nonnegint'; end if; if not type( [s], ['algebraic'] ) then continue := false; error "the 3rd entry `%1` is not of type `%2`", result[3], 'algebraic'; end if; 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 opts := 'outputoptions' = ['readonly' = ro]; if er then answer := LinearAlgebra[HilbertMatrix]( r, c, s, opts ); else answer := ''LinearAlgebra[HilbertMatrix]''( r, c, s, opts ); end if; end if: answer;
Maplets[Examples][LinearAlgebra][HilbertMatrix]
The Maplets[Examples][LinearAlgebra][HilbertMatrix] 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,HilbertMatrix
To view the source code, enter:
print( Maplets[Examples][LinearAlgebra][HilbertMatrix] );
See Also
Maplets[Examples][LinearAlgebra], LinearAlgebra[HilbertMatrix]
Return to Index for Example Worksheets
Download Help Document