Joe Riel

9660 Reputation

23 Badges

20 years, 10 days

MaplePrimes Activity


These are replies submitted by Joe Riel

Another problem is that the module doesn't appear to export anything useful to execute, other than Print, which doesn't do any computations.  So you'll need to correct that. 

my suggestion, which I tossed 'cause it was also slower than Roman's, is

collect(f, [x,y,z,...], 'distributed', HFloat);

If your goal is actually random polynomials, then you might be better off using the 'coeffs' option to randpoly.  A simple way is

randcoeff := rand(-99..99):
randpoly([x,y,z,t,u,v], 'degree'=20, 'dense', 'coeffs' = (()-> HFloat(randcoeff())));

 

Thanks for the method. Here's a shorter way that uses the same technique.  I've used a nifty trick described here.

findperm4 := proc(l1::list, l2::list)
local zap,cnt,L1,L2,n,i,T;

    zap := proc(x) procname(x) := 1; 0; end proc;
    cnt := proc(x) zap(x) := zap(x)+1; end proc;

    # Clear remember table of zap before each use
    zap := subsop(4=NULL, eval(zap));
    L1  := map(x -> [x,cnt(x)], l1);
    zap := subsop(4=NULL, eval(zap));
    L2  := map(x -> [x,cnt(x)], l2);

    n := nops(l1);
    T := table([seq(L1[i]=i, i=1..n)]);
    [seq(T[L2[i]], i = 1 .. n)];
end proc:

A couple of observations.  You don't want to use globals, instead use local scoping.  That is, make A and ee local to truc.  That is an expensive routine, it appears to be O(n^2). 

It does suggest an interesting problem.  Given two lists of size n, one a permutation of the other, compute the permutation of the integers 1..n that maps one to the other.  If the lists have unique elements, then this is easily solved in O(n) using a table.  With repeated elements it get more interesting, the trick is to avoid O(m^2) list building, where m is the number of repeated elements.  Here is one approach.  In addition to a table, it uses two rtables.  This should run in O(n).

findperm := proc(L1::list, L2::list, $)
local n,E,P,T,i,j,e,x;
    n := nops(L1);
    E := rtable(1..n);
    P := rtable(1..n);
    T := table();
    # Construct T, P, and E
    # T[x] is assigned the first position of x in L1.
    # P[i] points to the next position of L1[i]
    # E[i] points to the end of the chain, it is used to
    # avoid walking the chain to add an index.
    for i to n do
        x := L1[i];
        if assigned(T[x]) then
            j := T[x]; # get the location of the first x
            e := E[j]; # get the end of the chain
            E[j] := i; # update the position of the end of the chain
            if e = 0 then 
                P[j] := i; # add first link
            else
                P[e] := i; # add i to end of chain
            end if;
        else
            T[x] := i;
        end if;
    end do;
    # Construct the permutation; reuse E.
    for i to n do
        x := L2[i];
        j := T[x];
        E[i] := j;
        if P[j] <> 0 then
            T[x] := P[j];
        end if;
    end do;
    [seq(E[i],i=1..n)];
end proc:

I am confused by your selection description.  To do what you want, form a logical expression using Re and Im.  Say,

selectremove( z -> Re(z)<0 and Im(z) < 0, ..

I am confused by your selection description.  To do what you want, form a logical expression using Re and Im.  Say,

selectremove( z -> Re(z)<0 and Im(z) < 0, ..

With Maple13 you can do

   m =~ [a,b,c];

 

With Maple13 you can do

   m =~ [a,b,c];

 

On a 64-bit machine do you allow more variables, or higher exponents, or a combination? 

Why do you say it doesn't work?  Using command-line maple I get

(**) f := x -> if x <= 0 then 0 else 1 end if;
(**) f(2);                         
                                   1
(**) f(0);                         
                                   0
(**) f(-1);
                                   0

Why do you say it doesn't work?  Using command-line maple I get

(**) f := x -> if x <= 0 then 0 else 1 end if;
(**) f(2);                         
                                   1
(**) f(0);                         
                                   0
(**) f(-1);
                                   0

Notwithstanding Robert's point, you can combine the two calls to convert into one by using the 'compose' conversion:

convert(L, compose, set, list);

Simpler is [{L[]}]

Notwithstanding Robert's point, you can combine the two calls to convert into one by using the 'compose' conversion:

convert(L, compose, set, list);

Simpler is [{L[]}]

I assume you mean with the constraint that the 'sum' of each equals [1$12].  In that case, the problem is equivalent to finding the number of partitions of a set of m*k  elements into m sets of k elements (here k=3, m=4). 


k := 3: # specialization to 3
req := f(m) = binomial(m*k-1,k-1)*f(m-1):
rsol := rsolve({req, f(0)=1},f(m));
                     (-m - 1)  m                                1/2
                    2         9  GAMMA(m + 2/3) GAMMA(m + 1/3) 3
            rsol := -----------------------------------------------
                                          Pi


eval(rsol, m=4);
                                     15400

The recurrence f(m) = binomial(m*k-1,k-1)*f(m-1) comes from the following consideration. Choose the first set of a partition by picking the first element of the set of m*k elements, then choosing k-1 elements from the remainder.  There are binomial(m*k-1,k-1) ways to do this.  We are left with (m-1)*k elements. There are f(m-1) ways to partition these remaining elements. f(m) is the product.

Expanding f(m-1), etc, then expressing the binomials as factorials and canceling gives the general expression

fmk := (m*k)!/m!/(k!)^m:
eval(fmk, [m=4, k=3]);
                                                       15400
eval(fmk, [m=4, k=2]); # your previous problem
                                                        105
First 93 94 95 96 97 98 99 Last Page 95 of 195