Joe Riel

9660 Reputation

23 Badges

20 years, 22 days

MaplePrimes Activity


These are replies submitted by Joe Riel

Could you demonstrate how to patch the following.  Assume that you want to change the value of the local x to 24.  You aren't allowed to reassign f.

module()
global f;
local x := 23;
    f := () -> x;
end module:

I agree with Roman.  It would be more useful, not to mention a whole lot easier, to write standalone routines rather than attempting to patch the Maple library.  Besides, with modules it is not always possible to modify library code (that is, without access to the actual source).

Arrghh.  I had purposely tested this in what I had thought was a Maple 11 shell; apparently I was wrong.  Sorry about that.

Arrghh.  I had purposely tested this in what I had thought was a Maple 11 shell; apparently I was wrong.  Sorry about that.

Of course it is was fun.  And not much work, less than 15 minutes. A better approach might be to include a target (x2), stop when it was reached, and save the sequence of random numbers so that the generation can be replayed.

Here's a crude method for solving this puzzle with Maple.  In fact, it doesn't really solve the puzzle in that it does not show the construction, however, it wouldn't be too much work to do that if desired.  I just wanted to see whether the answer would pop-out.  To improve the odds, I only fed it 1 and x as the starting elements and looked for x2.  It did.

Puzzle := module()
global x;
export Create,Reset;
local elements, newElement, pickElement;
    pickElement := proc()
        op(RandomTools:-MersenneTwister:-GenerateInteger('range'=1..nops(elements)), elements);
    end proc;

    newElement := proc({maxlength :: posint := 50})
    local elem,num;
        num := RandomTools:-MersenneTwister:-GenerateInteger('range'=1..3);
        if num = 1 then
            elem := pickElement();
            if Testzero(elem) then return; end if;
            elem := 1/elem;
        elif num = 2 then
            elem := -pickElement();
        else
            elem := pickElement()-pickElement();
        end if;
        elem := simplify(elem);
        if length(elem) < maxlength then
            elements := elements union {simplify(elem)};
        end if;
    end proc:

    Create := proc(n::posint)
        to n do
            newElement();
        end do;
        elements;
    end proc;

    Reset := proc()
        elements := {1,x};
    end proc;

    Reset();

end module:

sort(convert(Puzzle:-Create(500),'list'),length);

Netscape fame?  You meant, of course, the more significant and longer lasting (X)emacs fame 8-).

I assume it is a known problem, however, I hadn't heard of it. While playing with finite fields a year ago, I made up what was, essentially, the same problem.

While Maple isn't particularly suited for solving this problem, it can be used to implement a solution. In fact, you can create a module that exports a redefined `*` operator implemented with the restricted operations:  Download 84_Puzzle.mpl

You're thinking too much (trying to find an exception).  The algorithm works over any field. You are given a and b, you need to form the product a*b.

It's doable. 

It's not clear how Maple could be effectively used here.  For those who want a hint, first figure out how to create x2.

Note that the seldom used 'sequential' type is equal to either a list or set.  So one could also do

proc(p :: Or(polynom, sequential(polynom))) ... end proc

The structured type Or is equivalent to set enclosure; see type,structured.

Note that the seldom used 'sequential' type is equal to either a list or set.  So one could also do

proc(p :: Or(polynom, sequential(polynom))) ... end proc

The structured type Or is equivalent to set enclosure; see type,structured.

Here's an approach that is considerably faster when the input is a random permutation.  For a list of length 100 it is 30x faster.  It only computes the length, not the actual sequence:

longestIncreasingSequenceLength2 := proc(L::list)
local n,i,j,G;
uses GraphTheory;
    n := nops(L);
    G := Digraph(n
                 , {seq(seq(`if`(L[i]<L[j]
                                 , [i,j]
                                 , NULL
                                )
                            , j = i+1..n)
                        , i = 1..n-1)}
                );
    LengthDAG(G);
end proc:

LengthDAG := proc(G)
local len, sources, v;
uses GraphTheory;
    len := proc(v)
    option cache;
    local j,dep;
    uses GraphTheory;
        dep := Departures(G,v);
        if dep = [] then
            1
        else
            1 + max(seq(procname(j), j in dep))
        end if;
    end proc;
    sources := select(v -> Arrivals(G,v)=[], Vertices(G));
    max(seq(len(v), v in sources));
end proc:

Here are the corresponding procedures for returning a longest sequence.  By using attributes, LongestPathOfDAG is practically identical to LengthDAG.

longestIncreasingSequence := proc(L::list)
local n,i,j,G;
uses GraphTheory;
    n := nops(L);
    G := Digraph(n
                 , {seq(seq(`if`(L[i]<L[j]
                                 , [i,j]
                                 , NULL
                                )
                            , j = i+1..n)
                        , i = 1..n-1)}
                );
    map(op,LongestPathOfDAG(G),L);
end proc:

LongestPathOfDAG := proc(G)
local len, sources, v;
uses GraphTheory;
    len := proc(v)
    option cache;
    local j,dep,longest;
    uses GraphTheory;
        dep := Departures(G,v);
        if dep = [] then
            setattribute(Float(1),v);
        else
            longest := max(seq(procname(j), j in dep));
            setattribute(1 + longest, v, attributes(longest));
        end if;
    end proc;
    sources := select(v -> Arrivals(G,v)=[], Vertices(G));
    [attributes(max(seq(len(v), v in sources)))];
end proc:
First 144 145 146 147 148 149 150 Last Page 146 of 195