Sample Maplet Application: Bezout Matrix
This worksheet demonstrates how to write Maplet applications that function similarly to the LinearAlgebra[BezoutMatrix] Maplet application available in the Maplets[Examples] package. It is designed for experienced Maple authors.
The BezoutMatrix Maplet application provides a graphical interface to the LinearAlgebra[BezoutMatrix] function.
Given two univariate polynomials, the Bezout matrix of these polynomials is a square matrix of size equal to the maximum degree of the polynomials. The determinant of this resulting matrix is equal to the resultant of the polynomials with respect to a specific variable. The only outputoption that can be used in this case is to set the matrix to readonly. However, post-processing could determine that the polynomials are sparse, in which case the resulting matrix can also use sparse storage. By using user input, this procedure returns the calling sequence required to generate the desired Bezout matrix or evaluates the result.
Thus, the Maplet application requests:
1. Two polynomials.
2. Whether the matrix is read-only.
3. Whether the result is to be evaluated (or the calling sequence is to be returned).
Simple Example
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 Maplet application is simpler than the one used in the second example, but the post-processing is more complicated.
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 Bezout matrix. TextBox( "The Bezout matrix of two univariate polynomials is a square " "matrix of size equal to the maximum degree of the polynomials. " "The determinant of the Bezout matrix is equal to the resultant " "of the polynomials.", 'height' = 3, 'width' = 40, 'editable' = 'false' ), # Four rows, each requesting the user for various # forms of input. The second check box is selected # by default. BoxRow( BoxCell( "Enter 1st polynomial:", 'halign'='left' ), BoxCell( TextField['TF1'](), 'halign'='right' ) ), BoxRow( BoxCell( "Enter 2nd polynomial:", 'halign'='left' ), BoxCell( TextField['TF2'](), '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', '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. 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 p := parse( result[1] ); q := parse( result[2] ); ro := parse( result[3] ); er := parse( result[4] ); catch: continue := false; error "one of the results does not parse: %0", result; end try; [p, q, ro, er]; end if;
Next, the parsed values should be checked to verify whether they are of the correct type. If both are of type polynom, then select all indeterminates of the polynomials.
if continue then if type( [p], ['polynom'] ) then ip := select( 'type', indets( p ), 'name' ); else continue := false; error "the 1st entry `%1` is not of type `%2`", result[1], 'polynom'; end if; if type( [q], ['polynom'] ) then iq := select( 'type', indets( q ), 'name' ); else continue := false; error "the 2nd entry `%1` is not of type `%2`", result[2], 'polynom'; end if; variables := ip union iq; end if;
If there are no indeterminates, set var to NULL. If there is only one indeterminate, use that value as the variable. If there is more than one indeterminate, use the example Selection Maplet application to query the user for the indeterminate to use in determining the Bezout matrix. By using the unique option, the user can select at most one variable.
if continue then if nops( variables ) = 0 then var := NULL; elif nops( variables ) = 1 then var := variables[1]; else # Asking for 'x' or 'y' is more expected than 'y' or 'x', # so sort the variables lexicographically (if all are symbols). if type( variables, 'set'('symbol') ) then variables := sort([op( variables )], 'lexorder' ); end if; # Query the user for the variable with respect to which the # Bezout matrix is determined vresult := Maplets[Examples][Selection](variables, 'unique'); if type( vresult, 'posint' ) then var := variables[vresult]; else continue := false; end if; end if; var; end if;
Finally, 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[BezoutMatrix]( p, q, var, opts ); else answer := ''LinearAlgebra[BezoutMatrix]''( p, q, var, opts ); end if; end if: answer;
Integrated Example
This example displays a Maplet application in which selecting the OK button initiates post-processing performed by the function MyFunc. The MyFunc procedure determines the variable with respect to which the Bezout matrix is determined.
MyFunc uses the tool Maplets[Tools][Get], which queries a Maplet application for an entry. The features of the procedure Get simplify the processing.
MyFunc := proc() local p, q, variables, vresult; p := Maplets[Tools][Get]( 'TF1'::polynom, corrections ); q := Maplets[Tools][Get]( 'TF2'::polynom, corrections ); variables := select( 'type', indets( p ), 'name' ) union select( 'type', indets( q ), 'name' ); if nops( variables ) = 0 then NULL; elif nops( variables ) = 1 then op( variables ); else variables := sort([op( variables )], 'lexorder' ); # Query the user for the variable with respect to which the # Bezout matrix is determined vresult := Maplets[Examples][Selection](variables, 'unique'); if vresult = NULL then "no variable"; else variables[vresult]; end if; end if; end proc:
Maplet application information must be returned to the worksheet. A dummy TextField element (defined outside the Window element) is used so that the post-processing occurs only if the Maplet applications runs without error.
maplet2 := Maplet( Window[W1]( BoxLayout('vertical'=true, TextBox( 4..40, "The Bezout matrix of two univariate polynomials is a square matrix of size equal to the maximum degree of the polynomials. The determinant of the Bezout matrix is equal to the resultant of the polynomials.",'height' = 3, 'width' = 40, 'editable' = 'false' ), BoxRow(BoxCell( "Enter 1st polynomial:", 'halign'='left' ),BoxCell( TextField['TF1'](), 'halign'='right' )), BoxRow(BoxCell( "Enter 2nd polynomial:", 'halign'='left' ),BoxCell( TextField['TF2'](), '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(BoxCell( Button("OK", Shutdown(['TF1', 'TF2', 'ChB1', 'ChB2'])),'halign'='left' ),BoxCell( Button("Cancel", Shutdown()), 'halign'='right' )))), TextField['TF3']("no variable")):
result := Maplets[Display](maplet2);
if result = NULL then # the user selected Cancel elif result[3] <> "no result" then # the Maplet application closed without error p := parse( result[1] ); q := parse( result[2] ); var := parse( result[3] ); ro := parse( result[4] ); er := parse( result[5] ); opts := outputoptions = ['readonly' = ro]; if er then answer := LinearAlgebra[BezoutMatrix]( p, q, var, opts ); else answer := ''LinearAlgebra[BezoutMatrix]''( p, q, var, opts ); end if; end if; answer;
Maplets[Examples][LinearAlgebra][BezoutMatrix]
Maplets[Examples][LinearAlgebra][BezoutMatrix] 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,BezoutMatrix
To view the source code, enter:
print( Maplets[Examples][LinearAlgebra][BezoutMatrix] );
See Also
LinearAlgebra[BezoutMatrix], Maplets[Examples][LinearAlgebra]
Return to Index for Example Worksheets
Download Help Document