Sample Maplet Application: Singular Values
This worksheet demonstrates how to write a Maplet application that functions similarly to the LinearAlgebra[SingularValues] Maplet application available in the Maplets[Examples] package. It is designed for experienced Maple authors.
The SingularValues Maplet application provides a graphical interface to the LinearAlgebra[SingularValues] function. The singular values of a matrix M are the square roots of the eigenvalues of M multiplied by its Hermitian transpose. The possible outputs are a matrix of left singular vectors (denoted by U), a transposed matrix of right singular vectors (denoted by Vt) and a vector of the singular values (denoted by S).
By using user input, this procedure returns the calling sequence required to find the singular values or evaluates the result.
Thus, the Maplet application requests:
1. The output, one of: U; S; Vt; or U, S, and Vt.
2. Whether the matrix is real-valued.
3. Whether the vector or matrix(ces) are read-only.
3. 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]):
Since it is not most efficient to enter a matrix in a Maplet application, the Maplets[Examples][LinearAlgebra][SingularValues] 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('vertical'=true, # A paragraph describing singular values. TextBox( "The singular values of a matrix M are the square roots of " "the eigenvalues of M multiplied by its Hermitian transpose. " "U is a matrix of left singular vectors, S is a vector of the " "singular values, and Vt is a transposed matrix of right singular " "vectors.", 'height' = 5, 'width' = 40, 'editable' = 'false' ), # Four 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 last check box is selected by default. BoxRow( BoxCell( "Output:", 'halign'='left' ), BoxCell( DropDownBox['DDB1'](["S", "U", "Vt", "U, S, Vt"]), 'halign'='right' ) ), BoxRow( BoxCell( "Matrix has real entries:", 'halign'='left' ), BoxCell( CheckBox['ChB1'](not has(M, I)), 'halign'='right' ) ), BoxRow( BoxCell( "Return read-only vector or matrix(ces):", 'halign'='left' ), BoxCell( CheckBox['ChB2'](), 'halign'='right' ) ), BoxRow( BoxCell( "Evaluate result:", 'halign'='left' ), BoxCell( CheckBox['ChB3'](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(['DDB1', 'ChB1', 'ChB2', 'ChB3'])), '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. 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; o := subs( ["S" = 'S', "U" = 'U', "Vt" = 'Vt', "U, S, Vt" = '[U, S, Vt]'], result[1] ); c := not parse( result[2] ); ro := parse( result[3] ); er := parse( result[4] ); 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, conjugate = c, seq( outputoptions[i] = ['readonly' = ro], i = [S, U, Vt] ); if o <> 'S' then M := evalf( M ); end if; if er then answer := LinearAlgebra[SingularValues]( M, opts ); else answer := ''LinearAlgebra[SingularValues]''( M, opts ); end if; end if: answer;
Maplets[Examples][LinearAlgebra][SingularValues]
The Maplets[Examples][LinearAlgebra][SingularValues] 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,SingularValues
To view the source code, enter:
print( Maplets[Examples][LinearAlgebra][SingularValues] );
See Also
Maplets[Examples][LinearAlgebra], LinearAlgebra[SingularValues]
Return to Index for Example Worksheets
Download Help Document