Enhanced Packages in Maple 9: Part 1 - Maple Programming Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : System : Information : Updates : Maple 9 : updates/Maple9/enhancedpackages1

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

CodeGeneration

  

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

(1)

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.

DEtools

• 

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&apos; ) 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) );

&ExponentialE;4B3Bg2g31BP&DifferentialD;x2BP&comma;&ExponentialE;4B3Bg2g31BP&DifferentialD;x2BP

(2)

k := 3:

ode := diff(y(x),x$2) + (k^2*sn*cn/dn)*diff(y(x),x) + 9*dn^2*y(x):

DEtools[dperiodic_sols](ode, y(x) );

4JacobiSNx&comma;333+JacobiSNx&comma;3&comma;4JacobiSNx&comma;333+JacobiSNx&comma;318JacobiSNx&comma;3JacobiCNx&comma;3JacobiDNx&comma;32+234+9JacobiSNx&comma;3JacobiCNx&comma;3JacobiDNx&comma;321

(3)

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&comma;&ExponentialE;WeierstrassZetax&comma;g2&comma;g3

(4)

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]);

M01000000110000001022x0010x202x0010x2−2000

(5)

DEtools['matrixDE'](M,x,solution=polynomial);

01x0000010001xx2000−1xx20000−12x0002xx2x32000&comma;000000

(6)
  

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&DifferentialD;&DifferentialD;xy1xy2x&comma;&DifferentialD;&DifferentialD;xy2xy3xy4x&comma;&DifferentialD;&DifferentialD;xy3xy5x&comma;&DifferentialD;&DifferentialD;xy4x2y1x2xy2xy5x&comma;&DifferentialD;&DifferentialD;xy5xx2y1x2xy3xy6x&comma;&DifferentialD;&DifferentialD;xy6xx2y2x+2y3x

(7)

vars := [y1(x), y2(x), y3(x), y4(x), y5(x), y6(x)];

varsy1x&comma;y2x&comma;y3x&comma;y4x&comma;y5x&comma;y6x

(8)

DEtools['polysols'](sys, vars);

0&comma;1&comma;x&comma;0&comma;0&comma;1&comma;1&comma;x&comma;x2&comma;−1&comma;x&comma;x2&comma;0&comma;−1&comma;2x&comma;2x&comma;x2&comma;x32

(9)

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]]];

sysp0&comma;1&comma;−1&comma;&comma;0&comma;1&comma;−1&comma;−1&comma;&comma;&comma;0&comma;1&comma;&comma;−1&comma;−2&comma;2x&comma;&comma;0&comma;1&comma;−1&comma;x2&comma;&comma;2x&comma;&comma;0&comma;1&comma;−1&comma;&comma;x2&comma;2&comma;&comma;&comma;0&comma;1

(10)

DEtools['polysols'](sysp, [0,0,0,0,0,0], x);

0&comma;1&comma;x&comma;0&comma;0&comma;1&comma;1&comma;x&comma;x2&comma;−1&comma;x&comma;x2&comma;0&comma;−1&comma;2x&comma;2x&comma;x2&comma;x32

(11)

LibraryTools

• 

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]();

LinearAlgebra

• 

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

withLinearAlgebra&colon;

MMatrix3&comma;5&comma;2&comma;4&colon;

MatrixFunctionM&comma;sinx&comma;x

sin72cosh39239cos72sinh392391039cos72sinh39239439cos72sinh39239sin72cosh392+39cos72sinh39239

(12)

SQMatrixFunctionM&comma;x12&comma;x

SQ222+7239222778539222739239222739222+72+39222778

(13)

mapradnormal&comma;SQ2

35−24

(14)
• 

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.

Examples

with(LinearAlgebra):

M := Matrix([[3,5],[-2,4]],storage=sparse,datatype=float):

b := Vector([1,2],datatype=float):

infolevel[LinearAlgebra]:=3;

infolevelLinearAlgebra3

(15)

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

(16)

Norm(M.x - b);

unknown:   hw_SpMatVecMulRR
unknown:   hw_SpMatVecMulRR
Norm:   "calling external function"
Norm:   "NAG: "   hw_f06raf

1.1102230246251565410−15

(17)

infolevel[LinearAlgebra]:=0;

infolevelLinearAlgebra0

(18)

ListTools

• 

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.

LREtools

• 

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');

2nk=0n1k4+k+1&comma;2nk=0n1k4+k+1

(19)
• 

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)];

sysx2y2x+3xy2x+2y2x2x2y1x4y1xx+y1x+1x2+y1x+1x&comma;y2x+1y1x

(20)

vars := [y1(x), y2(x)];

varsy1x&comma;y2x

(21)

LREtools['polysols'](sys, vars,{}, 'output'='basis');

_C1+x_C1+2_C2+xx1_C2&comma;x_C1+xx1_C2

(22)
• 

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+6n3+36n2+72n+60E3+n32n2+6E+4n3+8n2+6E2

(23)
• 

For information on the new LREtools[HypergeometricTerm] subpackage, see New Packages in Maple 9.

Slode

  

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));

odex2+2x4yx+3x3x2+2x&DifferentialD;&DifferentialD;xyx+x4+x3&DifferentialD;2&DifferentialD;x2yx

(24)

Slode[dAlembertian_formal_sol](ode,y(x));

x2_n=0x_n2+_n=0_n1=0_n112_n1_k=0_n11_k+32_k+2x_n_C0+&ExponentialE;2x_n=0x_n13_C1x

(25)

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);

odex12&DifferentialD;3&DifferentialD;x3yxx1x7&DifferentialD;2&DifferentialD;x2yx22x5&DifferentialD;&DifferentialD;xyx2yx

(26)

Slode[dAlembertian_formal_sol](ode,y(x));

_C0_n=0x_n+_C1_n=0_nx_n+_C2_n=0_n1=0_n1_n2=0_n11_k=0_n211_k+3x_n

(27)

SNAP

• 

The SNAP package contains a new function QRGCD, which computes an approximate right GCD of two univariate polynomials. For more information, see SNAP[QRGCD].

SolveTools

• 

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_Z43_Z2+25353RootOf_Z43_Z2+255&comma;y=RootOf_Z43_Z2+25

(28)
• 

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

CodeGeneration

DEtools

Enhanced Packages in Maple 9: Part 2

Index of New Maple 9 Features

infolevel

LibraryTools

LinearAlgebra

LinearFunctionalSystems[PolynomialSolution]

LinearFunctionalSystems[RationalSolution]

ListTools

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