Enhanced Packages in Maple 9: Part 1
Maple 9 contains many enhancements to existing packages. For additional enhancements, see Enhanced Packages in Maple 9: Part 2.
For information on new Maple 9 packages, see New Packages in Maple 9.
Enhanced Packages in Maple 9: Part 1 contains information for the following packages.
CodeGeneration package
DEtools package
LibraryTools package
LinearAlgebra package
ListTools package
LREtools package
Slode package
SNAP package
SolveTools package
CodeGeneration
DEtools
LibraryTools
LinearAlgebra
ListTools
LREtools
Slode
SNAP
SolveTools
Many improvements have been made to the CodeGeneration package. New exports allow translation of Maple code to the Visual Basic® and MATLAB® languages and provide users the ability to extend the current translation facilities. The translators accept modules as input, in addition to expressions, computation sequences, and procedures. These new features, along with other improvements in efficiency, coverage of the Maple language, and quality of the output, are described as follows.
with(CodeGeneration);
C,CSharp,Fortran,IntermediateCode,Java,JavaScript,Julia,LanguageDefinition,Matlab,Names,Perl,Python,R,Save,Swift,Translate,VisualBasic
Translation to New Languages
Two new exports, Matlab and VisualBasic, offer translation to the MATLAB® and Visual Basic languages, respectively. The following examples show translation of a computation sequence and a procedure.
x := 'x': s := 's': t := 't': r := 'r':
cs := [s=1.0+x, t=ln(s)*exp(-x), r=exp(-x)+x*t]:
Matlab(cs);
s = 0.10e1 + x; t = log(s) * exp(-x); r = exp(-x) + x * t;
f := proc(x, y) local a; if x < 1.0 then a := cos(y) else a := sin(y) end if; return 2*a; end proc:
VisualBasic(f);
Imports System.Math Public Module CodeGenerationModule Public Function f(ByVal x As Double, ByVal y As Double) As Double Dim a As Double If (x < 0.10E1) Then a = Cos(y) Else a = Sin(y) End If Return 0.2E1 * a End Function End Module
General information about translation using the CodeGeneration package can be found on the CodeGeneration help page, while information specific to the new MATLAB® and Visual Basic translators can be found on the MatlabDetails and VisualBasicDetails help pages.
User-Defined Translations
The translation facilities of the CodeGeneration package can be extended using the new CodeGeneration[LanguageDefinition] submodule. The CodeGeneration translators use predefined language definition modules, one for each target language the package supports. The LanguageDefinition submodule contains exports that allow you to override or add to translations specified in the existing definition modules or to specify an entirely new language definition module. Then, the Translate command can be used to translate using the new definitions.
A summary of the translation process is described on the LanguageDefinitionOverview help page.
Here, you define a language that extends ANSI C, and you define the correct translation of a Maple procedure named `my_function' to be `my_function_equivalent'.
LanguageDefinition:-Define( "SomeNewLanguage", extend="C", AddFunction("my_function", [numeric]::numeric, "my_function_equivalent") ): Translate( my_function(csc(x)+sin(x)), language="SomeNewLanguage" );
cg = my_function_equivalent(0.1e1 / sin(x) + sin(x));
Improved Coverage of the Maple Language
Modules are accepted as input by the CodeGeneration translators. For information, see CodeGeneration.
m := module() local g; export f; g := proc(x) cos(x)+sin(x) end proc: f := proc(y) if y>=0.0 and y<=2.0*Pi then g(y) else 0.0 end if end proc: end module:
Java(m);
import java.lang.Math; class m { public static double f (double y) { if (0.0e0 <= y && y <= 0.20e1 * Math.PI) return(g(y)); else return(0.0e0); } private static double g (double x) { return(Math.cos(x) + Math.sin(x)); } }
The Maple try statement is recognized by CodeGeneration, and translated to an appropriate equivalent in those supported languages for which such an equivalent exists.
tproc := proc(a, b) try a/b; catch: printf("An error %s occurred.\n", lasterror); end try; end proc:
Java(tproc);
class CodeGenerationClass { public static double tproc (double a, double b) throws Exception; { try { return(a / b); } catch (Exception except) { System.out.println("An error " + except.getMessage() + " occurred."); } } }
The translators accept more Maple constructs, even if they are unable to translate them. For example, in Maple 8, a list found in an unrecognized context produced an error. Now, such a list is translated unchanged, and a warning is issued. In these situations, the output is usually incorrect, but the user has the opportunity to modify it as required.
New Options
There are two new options, deducereturn and reduceanalysis, for controlling the analysis of the Maple input code.
When a procedure is provided as input, an attempt is made to interpret any implicit returns and convert them to explicit returns. However, if the deducereturn=false option is specified, no attempt at deducing an explicit return is made and any Maple code resulting in an implicit return is translated unchanged. An example is shown below; the first call shows the default behavior, while the second shows the effect of the deducereturn=false option.
f := proc(x) local y; y := x+10; end proc:
C(f);
int f (int x) { int y; y = x + 10; return(y); }
C(f, deducereturn=false);
void f (int x) { int y; y = x + 10; }
When a computation sequence, specified as a list of equations, is provided as input, the sequence is normally analyzed as a whole, so that type information collected during analysis of one assignment is applied to the analysis of subsequent assignments. The option reduceanalysis=true specifies that the computation sequence be analyzed one assignment at a time instead. The use of this option can significantly reduce the time and memory required for translation, though the target code may contain inconsistencies in type assignments.
More information about these and other options is available on the CodeGenerationOptions help page.
Miscellaneous
In Maple 8, the conversion of implicit returns to explicit returns always required the creation of a new variable to hold the return value. Now, a new variable is created only in cases where it is necessary, resulting in more compact output in most cases.
The DEtools package contains a new function DEtools[dperiodic_sols], which finds solutions of linear ODEs that have doubly-periodic coefficients. Doubly-periodic functions are typically represented in Weierstrass form (rational expressions in P and P' ) or Jacobi form (rational expressions of the Jacobi functions, for example, sn, cn, and dn).
alias( P = WeierstrassP(x,g2,g3), Pp = WeierstrassPPrime(x,g2,g3), sn = JacobiSN(x,k), cn = JacobiCN(x,k), dn = JacobiDN(x,k) ):
ode := diff(y(x),x$2) - (2*P + B)*y(x):
DEtools[dperiodic_sols](ode, y(x) );
ⅇ−4⁢B3−B⁢g2−g3⁢∫1B−Pⅆx2⁢B−P,ⅇ4⁢B3−B⁢g2−g3⁢∫1B−Pⅆx2⁢B−P
k := 3:
ode := diff(y(x),x$2) + (k^2*sn*cn/dn)*diff(y(x),x) + 9*dn^2*y(x):
−4⁢JacobiSN⁡x,333+JacobiSN⁡x,3,−4⁢JacobiSN⁡x,333+JacobiSN⁡x,3⁢18⁢JacobiSN⁡x,3⁢JacobiCN⁡x,3JacobiDN⁡x,32+234+9⁢JacobiSN⁡x,3⁢JacobiCN⁡x,3JacobiDN⁡x,32−1
ode := (Pp + (P^2))*diff(y(x),x$2) + ((P)^3-P*Pp-diff(P,x$2))*diff(y(x),x) + ((Pp)^2-(P)^2*Pp-P*diff(P,x$2))*y(x):
DEtools[dperiodic_sols](ode, y(x));
P,−ⅇWeierstrassZeta⁡x,g2,g3
k := 'k':
The DEtools package is extended by means of LinearFunctionalSystems package in searching for polynomial and rational solutions of systems of differential equations.
DEtools[matrixDE] accepts an option of the form solution = polynomial or solution = rational, which specifies to search for polynomial or rational solution. In this case, the function LinearFunctionalSystems[PolynomialSolution] or LinearFunctionalSystems[RationalSolution] is used to find solutions.
M := matrix(6,6,[0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,2,2*x,0,0,1,0,x^2,0,2*x,0,0,1,0,x^2,-2,0,0,0]);
M≔01000000110000001022⁢x0010x202⁢x0010x2−2000
DEtools['matrixDE'](M,x,solution=polynomial);
01x0000010001−x−x2000−1xx20000−1−2⁢x000−2⁢xx2x3−2000,000000
For DEtools[polysols] and DEtools[ratsols], a system of differential equations can be given either as a list of the equations in the first argument and a list of the variables in the second argument or as a list of lists of lists of coefficients of linear ODEs in the first argument, a list of the right-hand sides of such equations in the second argument, and the independent variable in the third argument. Each equation is represented as the sum of LODEs in one variable, each of them is given as a list of coefficients, so one equation is given as a list of lists of coefficients and a system as a list of lists of lists. The latter sequence is convenient for programming with the routines.
sys := [diff(y1(x), x) - y2(x), diff(y2(x), x) - y3(x) - y4(x), diff(y3(x), x) - y5(x), diff(y4(x), x) - 2*y1(x) - 2*x*y2(x) - y5(x), diff(y5(x), x) - x^2*y1(x) - 2*x*y3(x) - y6(x), diff(y6(x), x) - x^2*y2(x) + 2*y3(x)];
sys≔ⅆⅆxy1⁡x−y2⁡x,ⅆⅆxy2⁡x−y3⁡x−y4⁡x,ⅆⅆxy3⁡x−y5⁡x,ⅆⅆxy4⁡x−2⁢y1⁡x−2⁢x⁢y2⁡x−y5⁡x,ⅆⅆxy5⁡x−x2⁢y1⁡x−2⁢x⁢y3⁡x−y6⁡x,ⅆⅆxy6⁡x−x2⁢y2⁡x+2⁢y3⁡x
vars := [y1(x), y2(x), y3(x), y4(x), y5(x), y6(x)];
vars≔y1⁡x,y2⁡x,y3⁡x,y4⁡x,y5⁡x,y6⁡x
DEtools['polysols'](sys, vars);
0,1,x,0,0,1,1,−x,−x2,−1,x,x2,0,−1,−2⁢x,−2⁢x,x2,x3−2
sysp := [[[0,1],[-1]],[[],[0,1],[-1],[-1]],[[],[],[0,1],[],[-1]], [[-2],[-2*x],[],[0,1],[-1]],[[-x^2],[],[-2*x],[],[0,1],[-1]], [[],[-x^2],[2],[],[],[0,1]]];
sysp≔0,1,−1,,0,1,−1,−1,,,0,1,,−1,−2,−2⁢x,,0,1,−1,−x2,,−2⁢x,,0,1,−1,,−x2,2,,,0,1
DEtools['polysols'](sysp, [0,0,0,0,0,0], x);
The LibraryTools package contains a new routine LibraryTools[Browse]. The LibraryTools[Browse] routine displays a Maplet application interface that allows you to easily manipulate multiple libraries. By clicking buttons, you can create new libraries, navigate, and edit the contents of existing libraries.
LibraryTools[Browse]();
The LinearAlgebra package contains three new related routines: MatrixExponential, MatrixPower, and MatrixFunction. The more general MatrixFunction routine computes F(A) for an analytic function F and a Matrix A. The two routines MatrixExponential and MatrixPower are provided as convenient means to use this functionality for two common functions. More detail is available on the MatrixFunction help page.
Examples
with⁡LinearAlgebra:
M≔Matrix⁡3,5,−2,4:
MatrixFunction⁡M,sin⁡x,x
−39⁢cos⁡72⁢sinh⁡39239+sin⁡72⁢cosh⁡39210⁢39⁢cos⁡72⁢sinh⁡39239−4⁢39⁢cos⁡72⁢sinh⁡3923939⁢cos⁡72⁢sinh⁡39239+sin⁡72⁢cosh⁡392
SQ≔MatrixFunction⁡M,x12,x
SQ≔2⁢22+72−39⁢2⁢22−7785⁢39⁢2⁢22−739−2⁢39⁢2⁢22−7392⁢22+72+39⁢2⁢22−778
map⁡radnormal,SQ2
35−24
Three new classes of sparse iterative linear solver for floating-point systems have been introduced via the LinearAlgebra[LinearSolve] routine. These complement the existing real symmetric iterative solver, and cover the complex hermitian, real nonsymmetric, and complex nonhermitian cases.
with(LinearAlgebra):
M := Matrix([[3,5],[-2,4]],storage=sparse,datatype=float):
b := Vector([1,2],datatype=float):
infolevel[LinearAlgebra]:=3;
infolevelLinearAlgebra≔3
x := LinearSolve(M,b,method=SparseIterative);
LinearSolve: "using method" SparseIterative LinearSolve: "using method" SparseIterative LinearSolve: "calling external function" LinearSolve: "using CGS method" LinearSolve: "preconditioning with incomplete LU factorization" LinearSolve: "level of fill = " 0 LinearSolve: "using complete pivoting strategy" LinearSolve: "dimension of workspaces for preconditioner = " 16 LinearSolve: "using infinity norm in stopping criteria" LinearSolve: "setting maximum iterations to " 200 LinearSolve: "setting tolerance to " .10e-7 LinearSolve: "NAG" hw_f11zaf LinearSolve: "NAG" hw_f11daf LinearSolve: "NAG" hw_f11dcf LinearSolve: "number of iterations" 1 LinearSolve: "residual computed last as" 1.11022302462516e-015
x≔−0.2727272727272730.363636363636364
Norm(M.x - b);
unknown: hw_SpMatVecMulRR unknown: hw_SpMatVecMulRR Norm: "calling external function" Norm: "NAG: " hw_f06raf
1.11022302462515654×10−15
infolevel[LinearAlgebra]:=0;
infolevelLinearAlgebra≔0
The ListTools package contains a new function ListTools[Occurrences], which counts the number of occurrences of an item in a list. In addition to literal equality, other kinds of equivalences can be specified.
The LREtools[hypergeomsols] function uses an implementation based on the paper: M. van Hoeij, ``Finite Singularities and Hypergeometric Solutions of Linear Recurrence Equations.'' J. Pure Appl. Algebra, 139, p. 109-131 (1999). This algorithm can be substantially faster when the leading or trailing coefficient of the recurrence relation is a polynomial with complicated roots. As an example,
LREtools[hypergeomsols](u(n+2) = 2*(n^4+4*n^3+6*n^2+5*n+3)*(n^4+n+1)*u(n), u(n), {}, 'output'='basis');
−2n⁢∏k=0n−1⁡k4+k+1,2n⁢∏k=0n−1⁡k4+k+1
The LREtools package is extended by means of LinearFunctionalSystems package in searching for polynomial and rational solutions of systems of recurrence equations.
x := 'x':
sys := [y2(x)*x^2+3*y2(x)*x+2*y2(x)-2*y1(x)*x^2-4*y1(x)*x+y1(x+1)*x^2+y1(x+1)*x,y2(x+1)-y1(x)];
sys≔x2⁢y2⁡x+3⁢x⁢y2⁡x+2⁢y2⁡x−2⁢x2⁢y1⁡x−4⁢y1⁡x⁢x+y1⁡x+1⁢x2+y1⁡x+1⁢x,y2⁡x+1−y1⁡x
vars := [y1(x), y2(x)];
vars≔y1⁡x,y2⁡x
LREtools['polysols'](sys, vars,{}, 'output'='basis');
_C1+x⁢_C1+2⁢_C2+x⁢x−1⁢_C2,x⁢_C1+x⁢x−1⁢_C2
The LREtools package contains a new function LREtools[IsDesingularizable], which desingularizes linear difference operators with polynomial coefficients.
LREtools[IsDesingularizable]((n-2)*(n^3+2)*E+n,E,n,'leading','output'='operator');
true,1+6⁢n3+36⁢n2+72⁢n+60⁢E3+n3−2⁢n2+6⁢E+4⁢n3+8⁢n2+6⁢E2
For information on the new LREtools[HypergeometricTerm] subpackage, see New Packages in Maple 9.
The Slode package contains a new function Slode[dAlembertian_formal_sol], which finds formal solutions with d'Alembertian series to a homogeneous linear differential equation with polynomial coefficients.
ode:=(-4-x^2+2*x)*y(x)+(2*x-3*x^3-x^2)*diff(y(x),x)+(x^3-x^4)*diff(y(x),`$`(x,2));
ode≔−x2+2⁢x−4⁢y⁡x+−3⁢x3−x2+2⁢x⁢ⅆⅆxy⁡x+−x4+x3⁢ⅆ2ⅆx2y⁡x
Slode[dAlembertian_formal_sol](ode,y(x));
x2⁢−∑_n=0∞⁡x_n2+∑_n=0∞⁡∑_n1=0_n−1⁡−12_n1∏_k=0_n1−1⁡_k+2_k+32⁢x_n⁢_C0+ⅇ2x⁢∑_n=0∞⁡x_n−13⁢_C1x
ode:=(x-1)^2*diff(y(x),`$`(x,3))-(x-1)*(x-7)*diff(y(x),`$`(x,2))-2*(2*x-5)*diff(y(x),x)-2*y(x);
ode≔x−12⁢ⅆ3ⅆx3y⁡x−x−1⁢x−7⁢ⅆ2ⅆx2y⁡x−2⁢2⁢x−5⁢ⅆⅆxy⁡x−2⁢y⁡x
_C0⁢∑_n=0∞⁡x_n+_C1⁢∑_n=0∞⁡_n⁢x_n+_C2⁢∑_n=0∞⁡∑_n1=0_n−1⁡∑_n2=0_n1−1⁡∏_k=0_n2−1⁡1_k+3⁢x_n
The SNAP package contains a new function QRGCD, which computes an approximate right GCD of two univariate polynomials. For more information, see SNAP[QRGCD].
The SolveTools package contains a new function PolynomialSystem, which solves general systems of polynomial equations. For more information, see SolveTools[PolynomialSystem].
SolveTools[PolynomialSystem]({x^2+y^2-3,x*y+5},{x,y});
x=RootOf⁡_Z4−3⁢_Z2+2535−3⁢RootOf⁡_Z4−3⁢_Z2+255,y=RootOf⁡_Z4−3⁢_Z2+25
The SolveTools[CancelInverses] function has an option 'safe' that applies only those transformations that are valid everywhere. For more information, see SolveTools[CancelInverses].
See Also
alias
Enhanced Packages in Maple 9: Part 2
Index of New Maple 9 Features
infolevel
LinearFunctionalSystems[PolynomialSolution]
LinearFunctionalSystems[RationalSolution]
LREtools[hypergeomsols]
LREtools[IsDesingularizable]
Matrix
New Packages in Maple 9
proc
Slode/dAlembertian_formal_sol
SNAP[QRGCD]
SolveTools[PolynomialSystem]
trademarks
try/catch
Using Packages
Vector
with
Download Help Document