Joe Riel

9530 Reputation

23 Badges

20 years, 25 days

MaplePrimes Activity


These are answers submitted by Joe Riel

To do this with an event in dsolve, change the event to the following (the first element is the trigger, the second is a condition that must be true for the trigger to activate.)

events=[[[diff(r(phi),phi)=0,r(phi)>0.5],halt]]

then, after calling dsolve,

plots:-odeplot(p,[[phi,r(phi)],[phi,diff(r(phi),phi)]],0..5);

Use the curve option to CurveFitting:-LeastSquares:

pts := [[1,2],[3,4]]:
CurveFitting:-LeastSquares(pts, x, curve=a*x);
                                    7/5*x

Editor choice is both a personal and practical issue.  I use Emacs almost exclusively, occasionally using vi or vim if working on a remote machine that doesn't have Emacs.  I also work almost exclusively on linux platforms, which constrains the choice of editor (Emacs is available on Windows, I used it for years, and on a Mac).  The downside of using Emacs is that it has a long learning curve.  The upside is that it is infinitely configurable. A practical upside is that with it you can then use the Emacs-based Maple debugger, as well as maplev-mode for editing the source files; both are emacs packages I've written and available on the Maplesoft Application Center.  For those who haven't used it, it's hard to appreciate how much easier it is to debug a Maple program using the Emacs-based Maple debugger compared to either (a) no debugger; (b) the command-line debugger, or (c) the Standard GUI debugger.

Creating packages and libraries is independent of editor choice.  The biggest influence, I think, is what OS you are using and what tools you are familar with.  On Linux or the Mac (which I don't use) things are pretty easy.  Windows is a bit of a pain.  When developing on Windows I used cygwin to get a familar shell, however, there are issues, most of which I've thankfully forgotten since I've been off Windows for quite a while.  

Just to give you an idea of my typical work-flow, here's what I did yesterday to create a small Maple package (named ToMK)

$ cd ~/maple
$ git-new-project ToMK
$ cd ToMK
$ sbin/setup

The git-new-project command is a custom script that clones a git repository that  holds the basic layout for a Maple project, including a package template, a Makefile, and some related stuff.  It creates the ToMK subdirectory under my maple directory and populates it with files and subdirectories.  I then open, in Emacs, the template file maple/src/ToMK.mpl and write the Maple code, a help page for it (Maplesoft has a custom system for creating help pages from text files), and some test cases, then ran make install from the ToMK directory.  That generated and installed the package (i.e. copied the Maple archive and help data base to the appropriate system directories).

A Maple package usually means a Maple module with the option package.  In Maple source this might look like

MyPackage := module()
option package;
export funcA, funcB, funcC;
$include <maple/src/funcA.mpl>
$include <maple/src/funcB.mpl>
$include <maple/src/funcC.mpl>
end module:

Here the $include statements are Maple preprocessor directives that tell Maple to load the corresponding file at that point.  This permits breaking the source for a package into multiple files. Those files, in turn, can have their own include statements.

One way to create a Maple archive (*.mla) file from this source is to append the line

LibraryTools:-Save('MyPackage', "MyPackage.mla");

to the end of that file and then process the file with the command-line version of Maple (called cmaple in Windows, maple in other OSes):

$ maple < MyPackage.mpl

That isn't quite right in that you need to tell Maple where to look for the include statements.  That is done with the -I option. 

Another point.  Before rerunning the command that creates the  MyPackage.mla file, you probably will want to remove the existing MyPackage.mla file. Not doing so can lead to weird results.  

After creating the MyPackage.mla file you need to copy it to where Maple can find it.  That can depend on how you initialize Maple. I put the file in ~/maple/toolbox/MyPackage/lib/.

The Simulate command does not handle algebraic systems.  This is a bug (as a minimum, the error message should be better) and I will submit an SCR. However, note the disclaimer in the AlgEquation help page: "This object cannot be used by many of the commands in the DynamicSystems package."  It exists to permit code generating systems without differential equations. 

Preben's explanation is correct.  An alternative solution, if you want to keep z local, is to change the assignment to w to

w := eval(b, :-z=z);

Your question is not clear.  Do you want a nested loop?  If so, what have you tried?  

Your assignment to f(x) is strange. The Standard GUI will generally prompt if you mean an assignment to f's remember table, or an assignment to a procedure.  The better way to do the latter is f := x -> 108+2*x;. That avoids any prompting.

 

selectremove returns two expressions, each of the same type as the original.  In the first example, the original expression is a product, consequently it returns two products.  Witha product, a 1 is returned if the predicate does not match, that is so that the multiplying the two expressions returns the original.

You could add a dummy variable to the original, call selectremove, then subtract it from the second expression. By dummy variable I mean one not used in the original; to ensure that you can use a local variable to a procedure.

Creating the points is tedious. Here is an approach to simplify that aspect, using Maple's embedded components:  EtchSketch.mw

Click on the startup code region (gear-like symbol on toolbar) to see the code. The code I hacked together allows you to draw each of the characters you need, then plot them.  The resulting plot is not technically a parametric plot, though you could use the resulting data to create parametric plots as Carl suggested.  

On the menubar, click Tools --> Options then select the Security tab. I'm guessing your AutoExecute Security Level is set to Disable.  If so, change it to Don't warn before execution (or one of the warn options).

Here's a nicer approach.  The minimal bounding box must share an edge with the convex hull. That means we only need to check each edge of the hull. A bonus is that Maple can compute the exact solution.

MinAreaEnclosingRectange := module()
export
    ModuleApply := proc(P :: list([numeric,numeric])
                        , { plot :: truefalse := false }
                       )
    local A,hull,X,Y,i,n,poly;
    uses plottools;
        hull := simplex['convexhull'](P);
        X := Array(map2(op,1,hull));
        Y := Array(map2(op,2,hull));
        n := upperbound(X,1);
        # compute minimal area
        A := min(seq(MinRect(copy(X),copy(Y),i,n), i=1..n));
        # unpack attributes of minimal area
        (A,poly) := attributes(A);
        if plot then
            print(plots:-display(polygon(poly),polygon(P)));
        end if;
        A, poly;
    end proc;

local
    MinRect := proc(X :: Array, Y :: Array, i :: posint, n :: posint)
    local A,c,j,pts,r,s,x,y,xi,xmin,xmax,yi,ymin,ymax;
        # shift i-th point to origin
        (xi,yi) := (X[i],Y[i]);
        X[..] := X - xi; # bracket on lhs does operation in-place
        Y[..] := Y - yi;
        # rotate X and Y so next point is on x-axis
        j := modp(i,n)+1;
        (x,y) := (X[j],Y[j]);
        r := sqrt(x^2 + y^2);
        (c,s) := (x/r,-y/r);
        (X[..],Y[..]) := (c*X-s*Y, s*X+c*Y);
        # coordinates of minimal bounding rectangle with sides parallel to x-y axes
        (xmin,xmax) := (min,max)(X);
        (ymin,ymax) := (min,max)(Y);
        A := (xmax-xmin)*(ymax-ymin);
        # translate coordinate to original
        pts := [[xmin,ymin],[xmax,ymin],[xmax,ymax],[xmin,ymax]];
        pts := map(proc(pt)
                   local x,y;
                       (x,y) := op(pt);
                       [c*x + s*y + xi, -s*x + c*y + yi];
                   end proc, pts);
        # return area, with attributes: area and points that define the box.
        # area is included so that an exact value is returned.
        setattribute(evalf(A), A, pts);
    end proc;
end module:

pts := [[0,2],[1,4],[2,3.5],[4,4],[5,1],[4,0.75],[3,0]]:
(MinAreaEnclosingRectange)(pts, 'plot');
252/13, [[-3/13, 28/13], [51/13, -8/13], [79/13, 34/13], [25/13, 70/13]]

A data table embedded component does not have a value property, which is what the Do command you are calling is attempting to assign to. Data tables work differently than other components; they are linked to a global Matrix. Modifying the entries of that Matrix changes the data table.  Try just removing the call to Do.

MinRectangle := module()
export
    ModuleApply := proc(pts :: list([numeric,numeric]))
    local Pts, i, n, x0, y0;
        n := nops(pts)-1;
        Pts := Array(1..n, 1..2, pts[2..], 'datatype'=float[8]);
        (x0,y0) := op(pts[1]);
        for i to n do
            Pts[i,1] := Pts[i,1]-x0;
            Pts[i,2] := Pts[i,2]-y0;
        end do;
        theta -> Area(Pts,theta);
    end proc;

export
    Area := proc(Pts, theta)
    local b,c,i,l,r,s,t,x,y;
        (t,b,l,r) := (0,0,0,0);
        (c,s) := (cos,sin)(theta);
        for i to upperbound(Pts,1) do
            x := c*Pts[i,1] - s*Pts[i,2];
            y := s*Pts[i,1] + c*Pts[i,2];
            if x < l then
                l := x;
            elif r < x then
                r := x;
            end if;
            if y < b then
                b := y;
            elif t < y then
                t := y;
            end if;
        end do;
        (r-l)*(t-b);
    end proc;

end module:

pts := [[0,2],[1,4],[2,3.5],[4,4],[5,1],[4,0.75],[3,0]]:
f := MinRectangle(pts);
plot(f, 0..Pi);
Optimization:-Minimize(f, 0..Pi);
                        [19.6000000073504, [1.24904577327331]]


This isn't quite right, Minimize gets stuck at a local minimum.  Manually picking the range gives

Optimization:-Minimize(f,0.5 .. 0.7);
                       [19.3846154031950, [0.588002605076272]]

From symmetry, one should only have to rotate from 0 to Pi/2 to find a minimum.

Some operations work differently on Arrays than they do on Matrices or Vector. Matrices and Vectors are considered mathematical entities, an Array is more a programmer's entity.  Consider 

A := Array(1..3, [1,2,3]):
A + 1;
              [2,3,4]

V := Vector([1,2,3]);
V + 1;
Error, a constant cannot be added to a Vector; use +~ ... 

That is an error because, mathematically, it doesn't make sense. Programmers don't necessarily care about that, so a scalar can be added to an Array by doing it element-wise.

The goal is not entirely clear to me.  The confusion comes from the given example; why is the first element always 2?  Is the purpose to find the list with the largest second element? Or the list with the maximum (signed) difference between the two elements? Or the list with the maximum absolute value between the two elements?

Regardless, a different approach, somewhat sophisticated, is to use Maple attributes with the max function.  This is more efficient than sorting.  Sorting is O(n*ln(n)), and since a non-builtin comparison procedure is used the scaling factor is significant (though, I'm guessing, probably acceptable unless you have a rather large input). The max function is O(n), with a better scaling factor. Here is an example that finds the element with the maximum signed difference.

B := RandomTools:-Generate(listlist(integer(range=1..5),10,2)):
attributes(max(seq(setattribute(Float(b[2]-b[1]),b), b=B)));
                                                             [1, 5]

Note that doing this in a do-loop is conceptually simpler and could be faster and use less memory since a duplicate expression sequence does not have to be constructed.

Why not just wrap the code in a procedure?  For example, if I'm looking at a Maple worksheet provided by a user on this forum, I'll frequently export the worksheet to a *.mpl file, add a proc() DEBUG(); ... end proc() around the whole thing, then read it into maple.  Actually, rather than DEBUG() I use mdc(debug), which launches the Emacs debugger (assuming you have it installed). 

First 30 31 32 33 34 35 36 Last Page 32 of 114