Carl Love

Carl Love

26658 Reputation

25 Badges

11 years, 225 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are replies submitted by Carl Love

@emendes Yes, I knew that you actually wanted to multi-thread the code at a higher level. Doing so is even easier than my previous multi-threaded example because it requires no changes whatsoever to bulidAllPossibleModels. The following test is just to show the multi-threaded time. I started with a random list of 12,701 models of 3 entries and 3 monomials, and extended each to 57 models with 4 monomials.

restart:
MakeMonoList:= proc(alpha::symbol, V::list(symbol), dim::nonnegint)
local i, k, T;
    coeffs(expand((1+add(V))^dim), V, T);
    unapply(
        sort([T], rcurry(Groebner:-TestOrder, 'tdeg'(V[])))
        *~ [seq](alpha[i,k], k= 0..binomial(nops(V)+dim, dim)-1),
        i::posint,
        'proc_options'= 'remember'
    )
end proc
:
M:= MakeMonoList(alpha, [z,y,x], 3);

buildAllPossibleModels:= (model::list, M::procedure)->
    [for local i,m in model do
        map(subs, m=~ m+~ subs({`if`}(m::`+`, op(m), m)=~ (), M(i)), model)[]
    od]
:
R20:= rand(1..20):
Models_3:= CodeTools:-Usage([to 12701 do [seq](M(i)[R20()], i= 1..3) od]):
memory used=2.04MiB, alloc change=0 bytes, 
cpu time=47.00ms, real time=52.00ms, gc time=0ns

Models_4:= CodeTools:-Usage(
    Threads:-Map(buildAllPossibleModels, Models_3, M)
):
memory used=507.36MiB, alloc change=437.06MiB, 
cpu time=12.11s, real time=1.64s, gc time=21.59s

 

@emendes You asked:

  • Sorry for insisting on an explanation of how you built the function, but could you provide more details? 

Here is some more-verbose and likely slightly less-efficient code that does the same thing, in essentially the same order:

Student_buildAllPossibleModels:= proc(model::list, M::procedure)
local d:= nops(model), Models:= Array(1..d), i, m, Keep, mm;
    for i from 1 to d do
        m:= model[i];
        Keep:= {M(i)[]} minus {`if`(type(m, `+`), op(m), m)};
        Models[i]:= seq(subsop(i= m + mm, model), mm= Keep)
    end do;
    return [seq(Models)]
end proc
:

Let me know if that helps.

@emendes You asked:

  • If I give values to in other packages of the worksheet, would that somehow interfere with the results in buildAllPossibleModels

Yes, that interference would very likely happen and be problematic. It can be easily avoided by making a function of i. This would also avoid the need to pass i as a parameter. Here is a way to do it:

restart:
MakeMonoList:= proc(alpha::symbol, V::list(symbol), dim::nonnegint)
local i, k, T;
    coeffs(expand((1+add(V))^dim), V, T);
    unapply(
        sort([T], rcurry(Groebner:-TestOrder, 'tdeg'(V[])))
        *~ [seq](alpha[i,k], k= 0..binomial(nops(V)+dim, dim)-1),
        i
    )
end proc
:
#The order [z,y,x] is needed to duplicate your original monomial list:
M:= MakeMonoList(alpha, [z,y,x], 3);

model:= [x^2*y*alpha[1, 11], x*z^2*alpha[2, 15], y^2*z*alpha[3, 17] + y*z*alpha[3, 8]]:
buildAllPossibleModels:= (model::list, M::procedure)->
    [for local i,m in model do
        map(subs, m=~ m+~ subs({`if`}(m::`+`, op(m), m)=~ (), M(i)), model)[]
    od]
:
models:= CodeTools:-Usage(buildAllPossibleModels(model, M));

I included the parameter dim in MakeMonoList because I didn't know whether your dimension was always the same as the number of variables [x, y, z]. If they're always the same, the parameter can be removed and dim replaced with nops(V) in the code.

@acer What about controllers other than sliders (for example, dials)? Is any customization possible for those?

@emendes You need to pass in the index i, because it's not local. Like this:

buildAllPossibleModels:= proc(model::list, M::list, i::name)
description "This function returns all models generated from a given model and set of monomials.";
local k,m;
    [for k,m in model do
        map(subs, m=~ m+~ subs(i= k, {`if`}(m::`+`, op(m), m)=~ (), M), model)[]
    od]
end proc:

buildAllPossibleModels(model, M, i);

Note that I made a list, as in your original Question.

Yes, this code is threadsafe. Here is a threaded version:

buildAllPossibleModels:= module()
description "This function returns all models generated from a given model and set of monomials.";
local 
    m::thread_local,
    ModuleApply:= (model::list, M::list, i::name)-> local k;
        [Threads:-Seq](
            map(subs, (m:= model[k])=~ m+~ subs(i= k, {`if`}(m::`+`, op(m), m)=~ (), M), model)[],
            k= 1..nops(model)
        );
end module
:
buildAllPossibleModels(L1, L2, i);

@mmcdara Due to idiosyncracies of Maple, building a set, list, or sequence in a loop by appending to an existing set, list, or sequence is extremely inefficient: The execution time of each iteration is proportional to the current length of the set, list, or sequence.  Thus the time for the whole loop is proportional to the square of the final length.

That's not to say that it's always inefficient to build sets, lists, or sequences in loops; the inefficiency comes from building them by appending to an existing set, list, or sequence; it's not something inherently inefficient about loops.

This code of yours:

L1L2 := NULL:
while not T[finished] do 
  L1L2 := L1L2, T[nextvalue]() 
end do:
L1L2 := [L1L2]:

can be replaced by the far more efficient single line:

L1L2:= [seq](T[nextvalue](), 1..nops(L1)*nops(L2));

@mmcdara The original model is a list with 3 entries and 4 monomials. Each of the 56 new models is obtained by adding exactly one monomial to exactly one entry in the original model. Thus each new model has 3 entries and 5 monomials.

Please don't check off all the Product boxes in your Question headers. I changed your Products to just Maple, which is the only product that this Question is about.

@Preben Alsholm The quote from help page names that you show also has an error: "keyword" should be "reserved word".

@nm I wasn't saying that you were doing something wrong. Yes, this is a bug in Maple. The purpose of my Reply is to show that the bug can be isolated to the Physics Updates add-on package (not the core Physics package that's prepackaged with Maple). The Updates include numerous changes outside the Physics package.

Display the contents of the global variable libname. It'll be a sequence of file-directory names. One of them is for the Updates; it should be obvious which one. If you remove it from libname, it won't be used. This can be done temporarily.

There is a problem now that there are divergent versions of Maple with different bugs: with Updates and without.

@JAMET There is no difficulty with diff. But you shouldn't have quotes in the line with the solve command.

@Art Kalb It can be done without freeze thaw by temporarily changing the processed xis to something else, like this:

subs(
    xi= H(1)*xi, %xi= xi, 
    subsindets(expr, identical(xi)^integer, u-> subs(xi= %xi, H(op(2,u))*u))
);

@acer I think that your double subsindets can be replaced with

thaw(subs(xi= H(1)*xi, subsindets(expr, identical(xi)^integer, u-> freeze(H(op(2,u))*u))))

Is there any reason to not do this?

@C_R You can also use subs[eval](int= 1, expr). The 'eval' in this context is a keyword index-placed parameter for subs. It doesn't literally mean the eval command, although the effect is quite simiar.

@C_R This is not a feature or any special syntax of eval; rather, it's a feature of explicit numbers. When they are used as function symbols, they are their corresponding constant functions, and all arguments are ignored.

This is said in this somewhat awkwardly placed paragraph on help page ?function:

  • If, in this case, an expression such as 2(x) is a function application, it represents the constant function 2 (the function that is 2 everywhere). The result of evaluating it is the number 2.

The awkward part is that its previous paragraph, which is strictly about the corresponding 1-D- vs. 2-D-Input difference, could lead one to think that the 2nd paragraph is also about an input-mode nuance. The first words of the paragraph---"If, in this case, ..."---imply a logical link to the previous paragraph; yet, the 2nd paragraph is actually about something more general than input mode.

5 6 7 8 9 10 11 Last Page 7 of 691