Sample Maplet Application: QR DecompositionThis worksheet demonstrates how to write a Maplet application that functions similarly to the LinearAlgebra[QRDecomposition] Maplet application available in the Maplets[Examples] package. It is designed for experienced Maple authors.The QRDecomposition Maplet application provides a graphical interface to the LinearAlgebra[QRDecomposition] function.The QR decomposition of a matrix M is a decomposition of that matrix into two matrices, Q and R, being the orthonormal (unitary) and upper triangular factors, respectively.
By using user input, this procedure returns the calling sequence required to find the matrix norm or evaluates the result.Thus, the Maplet application requests:1. Whether to perform a full QR factorization.1. The output, one of: Q; R; Q and R; the rank; or Q, R, and the rank.2. Whether the matrix is real-valued.3. Whether the upper triangular factor R should be given upper triangular shape and storage.4. Whether the matrix(ces) are read-only.3. Whether the result is to be evaluated (or the calling sequence is to be returned).
<Text-field style="Heading 1" layout="Heading 1" bookmark="bkmrk0">Example Code</Text-field>restart:Invoke the Maplets Elements subpackage. with(Maplets[Elements]):Since it is not most efficient to enter a matrix in a Maplet application, the Maplets[Examples][LinearAlgebra][QRDecomposition] example Maplet application requires that the user include the matrix as an input.
For this example, the following matrix is used.M := <<1,2,3>|<2,3,5>|<3,5,12>>;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(BoxColumn(
# A paragraph describing QR decomposition.
TextBox( "QR decomposition factors the matrix M into two matrices:\134n"
"its orthonormal (unitary) Q and upper triangular R factors.",
'height' = 3, 'width'= 35, 'editable' = 'false' ),
# Six rows, each requesting the user for various
# forms of input. The second check box has a default
# value determined by the return value of a Maple call.
# The last check box is selected by default.
BoxRow(
BoxCell( "Perform full QR decomposition:", 'halign'='left' ),
BoxCell( CheckBox['ChB1'](), 'halign'='right' )
),
BoxRow(
BoxCell( "Output:", 'halign'='left' ),
BoxCell( DropDownBox['DDB1'](["Q, R", "Q", "R", "rank",
"Q, R, rank"]), 'halign'='right' )
),
BoxRow(
BoxCell( "Matrix has real entries:", 'halign'='left' ),
BoxCell( CheckBox['ChB2'](not has(M, I)), 'halign'='right' )
),
BoxRow(
BoxCell( "Upper-triangular shape and storage for R:", 'halign'='left' ),
BoxCell( CheckBox['ChB3'](), 'halign'='right' )
),
BoxRow(
BoxCell( "Return read-only matrix(ces):", 'halign'='left' ),
BoxCell( CheckBox['ChB4'](), 'halign'='right' )
),
BoxRow(
BoxCell( "Evaluate result:", 'halign'='left' ),
BoxCell( CheckBox['ChB5'](true), 'halign'='right' )
),
BoxRow(
# The OK button shuts down the Maplet application and
# returns the results in the drop-down and check boxes.
BoxCell( Button("OK", Shutdown(['ChB1', 'DDB1', 'ChB2', 'ChB3',
'ChB4', 'ChB5'])), '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. Since the Maplet application required no user input, there is no need to check the output.
If the user indicates that the matrix has real entries, then the conjugate option need not be set. if type( result, list ) then
continue := true;
fs := parse( result[1] );
o := [parse( result[2] )];
c := not parse( result[3] );
ut := parse( result[4] );
ro := parse( result[5] );
er := parse( result[6] );
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
opts := output = o, fullspan = fs, conjugate = c,
outputoptions[Q] = [readonly = ro],
outputoptions[R] = [readonly = ro, `if`(
ut,
'shape' = 'triangular[upper]',
NULL
), 'storage' = `if`(
ut,
'triangular[upper]',
'rectangular'
)]:
if er then
answer := LinearAlgebra[QRDecomposition]( M, opts );
else
answer := ''LinearAlgebra[QRDecomposition]''( M, opts );
end if;
end if:
answer;
<Text-field style="Heading 1" layout="Heading 1" bookmark="bkmrk1">Maplets[Examples][LinearAlgebra][QRDecomposition]</Text-field>The Maplets[Examples][LinearAlgebra][QRDecomposition] 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,QRDecompositionTo view the source code, enter:print( Maplets[Examples][LinearAlgebra][QRDecomposition] );
<Text-field style="Heading 1" layout="Heading 1" bookmark="bkmrk2">See Also</Text-field>Maplets[Examples][LinearAlgebra], LinearAlgebra[QRDecomposition]Return to Index for Example Worksheets