Carl Love

Carl Love

28150 Reputation

25 Badges

13 years, 351 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are replies submitted by Carl Love

If I define DistE(v, {u,w}) to be the distance from vertex v to edge {u,w} and DistV(v, u) to be the ordinary distance between vertices, then isn't it always true that DistE(v, {u,w}) = min(DistV(v,u), DistV(v,w))?

@Carl Love Although the TableInverse procedure that I gave above is quite efficient, inverting tables is an important enough operation that it's worth having a dedicated procedure for it, and some small extra efficiency can be gained that way. Anyway, the new procedure is still quite short:

TableInverse:= proc(t::table)
local x, T:= table();
     for x in indices(t, ':-pairs') do T[rhs(x)][lhs(x)]:= () od;
     {indices}~(T)
end proc
:


Very important: The time required to invert an entire large table by this procedure can be less than the time required to find the inverse of a single entry by the select method (as proposed by Kitonum). Here's an example of that:

#Example and timing comparison:
T:= table():
R:= rand(0..2^5):
#T will have 2^18 (~1/4-million) indices:
for i to 2^9 do for j to 2^9 do T[i,j]:= 'R'()$2 od od:

TI:= CodeTools:-Usage(TableInverse(T)):
memory used=52.03MiB, alloc change=2.00MiB, 
cpu time=547.00ms, real time=537.00ms, gc time=0ns

#Get an arbitrary entry:
j:= (6,7):
x:= T[j];
                             9, 11

#Get its (setwise) inverse from inverse table already constructed:
S1:= TI[x]:
#Verify that that inverse set contains the known index:
member([j], S1);
                              true

#Get one-point setwise inverse with select:
S2:= CodeTools:-Usage(select(i-> T[i[]]= x, {indices(T)})):
memory used=24.00MiB, alloc change=6.01MiB, 
cpu time=703.00ms, real time=716.00ms, gc time=0ns

#Verify that it's the same inverse set:
evalb(S1=S2);
                              true

Efficient use of tables is one of the most important things to learn for writing efficient Maple code.

@lcz In your procedure, you need to change Distance(P, u, j) to Distance(G, u, j). It was only due to an unlucky coincidence that your example worked with P.

@mapleatha Thanks, and a Vote Up for the Answer would be appreciated.

Apparently, in more-recent Maple, D has been updated to handle procedures containing symbolic operations such as convert (an arrow expression is considered a form of procedure). This is an impressive achievement. Nonetheless, it's still important to use unapply where appropriate. I'm glad that you've been able to put it into widespread use.

From decades of teaching and explaining Maple, I've noticed that understanding when to use unapply is usually a major milestone in one's learning of symbolic programming.

@David Sycamore 

Regarding your listplots error: You have a small spelling error: The command is plots:-listplot, not plots:-listplots.

Regarding your multiple-assignment error: I need to know which command line gave the error. It's either this one

(S,B):= CodeTools:-Usage(A(N)):

or this one

(i1,i2):= selectremove(k-> B[k], [$1..nops(S)]):

Please download and execute this worksheet. Do you still have errors?
BacktrackSeq.mw


Here's an updated plot showing 2^15 entries, with those that violate that inequality highlighted (this is precisely the contents of the attached worksheet):

restart:

A:= proc(N::And(posint, Not({1,2})))
local 
    n, m, p, U:= table([HFloat(1)= 1]), 
    B:= rtable(1..N, [false$2], datatype= truefalse), 
    A:= rtable(1..N, [1,0], datatype= float[8])
;
    for n from 2 to N-1 do
        p:= A[n];
        m:= U[p];
        B[n+1]:= type(m, posint); 
        A[n+1]:= `if`(B[n+1], n-m, min(abs~(p -~ A[..n-1])));
        U[p]:= n       
    od;
    ([seq](trunc(a), a= A), B)
end proc
:
N:= 2^15:
(S,B):= CodeTools:-Usage(A(N)):
memory used=2.19GiB, alloc change=0 bytes, 
cpu time=7.53s, real time=6.58s, gc time=3.20s

#Separate the indices by provenance of the entry:
(i1,i2):= selectremove(k-> B[k], {$1..nops(S)}):

#Find the indices that violate an inequality:
i3:= remove(n-> S[n]+S[n+1] <= n, {$1..nops(S)-1});
  i3 := {521, 1792, 2377, 3670, 4332, 5767, 6786, 7442, 29083}

#Fraction of entries generated by the "repeat rule":
evalf(nops(i1)/N);
                          0.8138732910

#Check whether all the inequality violations come from the repeat rule:
i3 subset i1;
                              true

#For the purpose of plotting only, separate out the violators:
i1:= i1 minus i3:

plot(
    [(i-> `[]`~(i, S[[i[]]]))~([i||(1..3)])[], [[0,0], [N,N]]],
    style= [point$3, line], 
    legend= [`Repeat  `, `Novel  `, `Large  `, y=x], 
    color= [red, blue, green, black], 
    symbolsize= [1,9,9], symbol= [point, diamond, solidcircle],
    thickness= 0, axes= boxed, axesfont= [TIMES, ROMAN, 8],
    axis[1,2]= [tickmarks= 8]
);

 

Greg, I agree with Tom: Definitely don't use implicitplot3d to plot balls. The error message that you got is unfortunate, and some people at Maplesoft may be interested in tracking it down. This is something that probably can't be debugged by a relatively inexperienced user. In particular, implicitplot3d is mostly externally coded, so you certainly wouldn't get very far looking into that. However, I wouldn't be surprised at all if the error was simply due to the massive amount of data contained in your balls. Using Tom's balls may be all you need to fix the problem. 

@ I don't see any attached file. Please try again. 

The following symbols are used in your PDF without the slightest definition, not even an equation; nor are they used in your worksheet, so that can't be used to figure out their meanings either: x, y, a, v__f, w, C, T, N.

"In the ... absence of lambda" makes no sense me, unless perhaps by "absence" you mean lambda=0.

I can see that the command Embed exists, but it is undocumented. What is it supposed to do?

@Carl Love Here's a combined plot of the entries computed by the "novel rule" and by the "repeat rule". It shows an interesting pattern.

A:= proc(N::And(posint, Not({1,2})))
local 
    n, m, p, U:= table([HFloat(1)= 1]), 
    B:= rtable(1..N, [false$2], datatype= truefalse), 
    A:= rtable(1..N, [1,0], datatype= float[8])
;
    for n from 2 to N-1 do
        p:= A[n];
        m:= U[p];
        B[n+1]:= type(m, posint); 
        A[n+1]:= `if`(B[n+1], n-m, min(abs~(p -~ A[..n-1])));
        U[p]:= n       
    od;
    ([seq](trunc(a), a= A), B)
end proc
:
N:= 2^13:
(S,B):= CodeTools:-Usage(A(N)):
memory used=152.32MiB, alloc change=27.49MiB, 
cpu time=469.00ms, real time=491.00ms, gc time=0ns

#Separate the indices into 2 lists: "repeat rule" and "novel rule":
(i1,i2):= selectremove(k-> B[k], [$1..nops(S)]):
evalf(nops(i1)/N);
                                0.7999267578

plot(
    [`[]`~(i1, S[i1]), `[]`~(i2, S[i2])],
    style= point, legend= [Repeat, Novel], color= [red, blue],
    symbolsize= [1,9], axes= boxed, symbol= [point, solidcircle]
);

@David Sycamore The sequence output by the procedure is sufficient for either of those two purposes. There is nothing that needs to be "adapted".

To plot: Using the sequence S already returned, do plots:-listplot(S, ...) where ... are options exactly as shown in the other Answers.

To check that inequality:

remove(n-> S[n]+S[n+1] <= n, [$1..nops(S)-1]);
               
[521, 1792, 2377, 3670, 4332, 5767, 6786, 7442]

The returned list is the values of n for which the inequality is false.

@cky1946 You can find the information on the help page ?plot,options. (There's a link to this from ?plots,matrixplot.) This is one of my most frequently reread help pages.

@tomleslie Thank you, Tom. I think that most of the more-abstract plotting commands should wrap their output with a display command such as plots:-display(..., overrideoption, _rest) so that any arguments not matching declared keyword parameters are just passed through to display.

@tomleslie Tom: Your confusion arises because although the kernel only "sees" 1-byte characters, the Maple user interfaces (including the plaintext command-line interface) do fully support multi-byte-encoded characters.

Vote up for an impressive solution. 

If you wish, your cat command can be replaced by sprintf("%a[%d:%d]", s, m-1, n). I find that easier to read, although I do realize that what's easier for me to read often doesn't correspond to what others find easier to read.

First 133 134 135 136 137 138 139 Last Page 135 of 711