Joe Riel

9660 Reputation

23 Badges

20 years, 3 days

MaplePrimes Activity


These are answers submitted by Joe Riel

map[3](applyop,f,1,[[1,2,3],[4,5,6],[7,8,9]]);

If you don't need the minimum result, a much faster approach, applicable for the 4x4 problem, is to find the best solution for 8x2, then split each of those into 4x2.  Using the compiled version of Var with MinVal, above, I got a result in about 2 seconds, compared to 48 seconds for the minimum result. 

Be aware that the original problem, in just the two-set version, is NP-complete.

Here's an approach, using a modification of Carl's method, with a compiled procedure used to compute the minimal value. It runs in 48 seconds (Carl's took over 500 seconds on my machine).

Note: This code has been replaced one time, found a bug in previous version.

MinVal := proc(p :: Array(datatype=integer[4])
               , A :: Array(datatype=float[8])
               , lg :: Array(datatype=float[8])
               , pmin :: Array(datatype=integer[4])
               , m :: float
               , K :: float
               , n :: posint
               , num :: posint
              )
local i,j,v;
    # Initialize A, which is used as a scratchpad
    for j to num do
        A[j] := -K;
    end do;

    for i to n do
        j := p[i]+1;
        A[j] := A[j] + lg[i];
    end do;

    v := 0.0;
    for j to num do
        v := v + abs(A[j]);
    end do;

    # test for minimum
    if m <= v then
        v := m;
    else
        # update pmin
        for i to n do
            pmin[i] := p[i];
        end do;
    end if;
    return v;
end proc:


Var := proc(L::list(positive)
            , len :: posint
            , num :: posint
           )
local A, M, P, T, i, K, lg, n, p, pmin;
    n := numelems(L);
    if len*num <> n then
        error "invalid partition specification";
    end if;

    # allocate arrays used by MinVal
    A := Array(1..num, 'datatype'=float[8]);
    pmin := Array(1..n, 'datatype'=integer[4]);

    lg := Array(evalf(ln~(L)),'datatype'=float[8]);
    K := add(i, i=lg)/num;
    M := K*len;

    P := Iterator:-SetPartitions(n,[[len,num]]);

    for p in P do
        M := MinVal(p, A, lg, pmin, M, K, n, num);
    end do;

    # Convert pmin to a list of sets of indices into p
    T := ListTools:-Classify(i->pmin[i],[seq(1..n)]);
    return [seq(T[i],i=0..num-1)], exp(M);

end proc:


L := [1829.0, 1644.0, 1594.0, 1576.0, 1520.0, 1477.0, 1477.00, 1404.0
      , 1392.0, 1325.0, 1313.0, 1297.0, 1292.0, 1277.0, 1249.0, 1236.0]:

MinVal := Compiler:-Compile(MinVal):
CodeTools:-Usage((Var)(L, 4, 4));
memory used=2.39GiB, alloc change=0 bytes, cpu time=47.79s, real time=47.91s, gc time=1.94s
  [{1, 9, 13, 15}, {2, 4, 14, 16}, {3, 6, 10, 11}, {5, 7, 8, 12}], 1.006534690



An alternative technique is to use TopologicalSort to find a consistent ordering:

 

L := [map(`[]`@op,s)[]]:
TopologicalSort(L);
          [E,Z,F,P,A]

 Use ListTools:-Reverse to get the result sorted greatest to smallest.

Try ListTools:-PartialSums

It's not clear what you want.  Neither subs command makes much sense.  With the inplace option, you are substituting a matrix (CC) for theta, which appears in cos(theta).  The result doesn't make sense to Maple, it is the cosine of a Matrix.

Assign the following procedure, call it at the start of the worksheet, then execute the worksheet

SleepUntil := proc(tim::string, fmt :: string := "%D %T")
local delay,t0,t1;
uses %ST = StringTools;
    t1 := %ST:-ParseTime(fmt,tim);
    t0 := %ST:-ParseTime(fmt,%ST:-FormatTime(fmt));

    delay := ( t1:-second - t0:-second
               + 60*(t1:-minute - t0:-minute
                     + 60*(t1:-hour - t0:-hour
                           + 24*(t1:-yearDay - t0:-yearDay
                                 + 365*(t1:-year - t0:-year)))));
    Threads:-Sleep(delay);
end proc:

(SleepUntil)("12/25/13 11:58:00");

 

Combine the deqs and initial conditions into a single list or set, rather than a list of two sublists. It works fine then.

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.

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