sursumCorda

235 Reputation

8 Badges

0 years, 106 days

MaplePrimes Activity


These are questions asked by sursumCorda

For two nonempty lists (of the same length), 

F~(list1, ` $`, list2); # space-dollarsign

is (almost) equivalent to 

zip(F, list1, list2);

However, it appears that in practice, using `~` can be faster than its equivalent zip version.
Here is a typical test: 

restart;

x := combinat:-randperm(10^7):
y := combinat:-randperm(10^7):

undefine(f);

t2 := CodeTools[Usage](`~`[f](x, ` $`, y), iterations = 10)

memory used=0.62GiB, alloc change=1.21GiB, cpu time=51.35s, real time=14.77s, gc time=42.55s

 

t4 := CodeTools[Usage](zip(f, x, y), iterations = 10)

memory used=0.52GiB, alloc change=-4.00MiB, cpu time=53.88s, real time=16.25s, gc time=44.51s

 

evalb(t2 = t4)

true

(1)

NULL


 

Download `~`_and_zip.mw

But I cannot find any explanations in the documentation (as well as What is Maple's equivalent to Mathematica's MapThread?). Does anyone know why?

For a list containing at least one element, 

F~(list0); # element-wise

is (almost) equivalent to 

map(F, list0);

However, it seems that in practice, using `~` can be faster than its equivalent map version.
Here is a typical test: 

 

 

restart;

w := [`$`](0 .. 1e4):
x := [`$`](0 .. 2e3):
y := [`$`](0 .. 3e2):
z := [`$`](0 .. 4e1):

undefine(f)

NULL

 

 

# Compute a generalized outer product of [w, x] and [y, z] without using the inefficient `ArrayTools:-GeneralOuterProduct`.

""(* Note that we cannot use the ""unapply"" command here. (Use ""->"" instead.) *)""

p2 := CodeTools[Usage](`~`[proc (s4) options operator, arrow; `~`[proc (s3) options operator, arrow; `~`[proc (s2) options operator, arrow; `~`[proc (s1) options operator, arrow; f(s3, s1) end proc](s2) end proc]([y, z]) end proc](s4) end proc]([w, x]), iterations = 10)

memory used=282.53MiB, alloc change=0.59GiB, cpu time=21.67s, real time=7.41s, gc time=17.33s

p4 := CodeTools[Usage](map(proc (s4) options operator, arrow; map(proc (s3) options operator, arrow; map(proc (s2) options operator, arrow; map(proc (s1) options operator, arrow; f(s3, s1) end proc, s2) end proc, [y, z]) end proc, s4) end proc, [w, x]), iterations = 10)

memory used=230.48MiB, alloc change=-4.00MiB, cpu time=23.26s, real time=8.67s, gc time=17.71s

evalb(p2 = p4);

true

NULL


 

Download `~`_and_map.mw

But I cannot find any explanations in Comparison of element-wise operators and map function or Difference between 'map' and '~' (element-wise operation). Does anyone know why?

The command for doing syntactical exact-match substitutions is subs, however, subs applies transformation rules throughout an expression only once. The documentation of eval claims that the (recursive) evaluation is repeated until either the result does not change, the documentation of applyrule claims that applyrule … applies the rules until no rule can be applied any more, and the documentation of MmaTranslator[Mma][ReplaceRepeated] claims that the single ReplaceRepeated command performs replacements until expression no longer changes.
So, if I comprehend correctly, 

restart;
x := [[[[]]]]: # Remove empty lists from x repeatedly.
(*⒈*) eval['recurse'](x, [[] = 'NULL']);
(*⒉*) applyrule([[] = 'NULL'], x);
(*⒊*) MmaTranslator:-Mma:-ReplaceRepeated(x, [[] = NULL])

should all return NULL, but in fact, 

eval['recurse'](x, [[] = 'NULL']);
 = 
                            [[[[]]]]

applyrule([[] = 'NULL'], x);
Error, (in PatternMatching:-AlgStruct:-TableLookup) invalid input: unknown uses a 1st argument, x, which is missing
MmaTranslator:-Mma:-ReplaceRepeated(x, [[] = NULL]);
 = 
                            () = ()

In other words, none of these replacements is feasible. 

Have I missed something? (It seems to me that an explicit procedural do...until loop can be actually avoidable here!) 

The new command ArrayTools[GeneralOuterProduct] (introduced in Maple 2021) computes the generalized outer product of two rtables, and again, there exists a similar function Outer in Mma (cf. the end of this question). But in practice, it appears that this Maple command is not so fast as Mma's one. To begin with, we need to generate four lists: w, x, y, and z. Our goal is forming all possible combinations of the lowest‐level elements in a nested structure (rather than a flat structure). Now let us start the test.

In Mathematica (the real time is about ): 

And in Maple (the real time is about ): 
 

restart;

w := [`$`](0 .. 1e4):
x := [`$`](0 .. 2e3):
y := [`$`](0 .. 3e2):
z := [`$`](0 .. 4e1):

"time[real]((p1:=MmaTranslator:-Mma:-ReplaceRepeated(convert(ArrayTools:-GeneralOuterProduct(convert([w,x],Array,fill=NULL),()->`if`(nargs=2,`[]`(args),NULL),convert([y,z],Array,fill=NULL)),listlist),[]=NULL)))"

199.880

(1)

"time[real]((p2:=(s4->(s3->(s2->(s1->`[]`(s3,s1))~(s2))~([y,z]))~(s4))~([w,x])))"

7.699

(2)

p3 := parse(StringTools:-CharacterMap("{}", "[]", FileTools:-Text:-ReadFile("E:/data.txt")))

evalb(p1 = p2 and p2 = p3) = trueNULL


 

Download Outer.mw

As you can see, Maple and Mathematica returns identical results (∵p1p3); nevertheless, Maple consumes too much time: the ratio is 199.880/0.784176 ≈ 254.892. (What a wide gap between them!) 
So, is there any possibility of speeding up Maple's ArrayTools:-GeneralOuterProduct? Or any ideas of obtaining the same results in Maple efficiently?

Explanatory notes. Here is an illustrative animation: 

That is to say, a generalized map
E.g., here is a nested list: 

nl := [[[[s, t]], [u, [v, w]]], [[x, [y, z]]]]:

We can use map to apply the mapped function F to "each operand" (i.e., the first‐level parts) of : 

:-map(F, nl);
 = 
         [F([[[s, t]], [u, [v, w]]]), F([[x, [y, z]]])]

But in Mathematica, we can make further explorations: 

In[1]:= nl = {{{{s, t}}, {u, {v, w}}}, {{x, {y, z}}}}; 

In[2]:= Map[F, nl, {1}] (*Maple's result*)

Out[2]= {F[{{{s, t}}, {u, {v, w}}}], F[{{x, {y, z}}}]}

In[3]:= Map[F, nl, {2, -2}]

Out[3]= {{F[{F[{s, t}]}], F[{u, F[{v, w}]}]}, {F[{x, F[{y, z}]}]}}

In[4]:= Map[F, nl, {-3, 3}]

Out[4]= {{F[{F[{s, t}]}], F[{F[u], F[{v, w}]}]}, {F[{F[x], F[{y, z}]}]}}

In[5]:= Map[F, nl, {0, \[Infinity]}, Heads -> \[Not] True]

Out[5]= F[{F[{F[{F[{F[s], F[t]}]}], F[{F[u], F[{F[v], F[w]}]}]}], F[{F[{F[x], F[{F[y], F[z]}]}]}]}]

Note that the last case has been implemented in Maple as MmaTranslator[Mma][MapAll]:  

MmaTranslator:-Mma:-MapAll(F,nl);
 = 
   F([F([F([F([F(s), F(t)])]), F([F(u), F([F(v), F(w)])])]), 

     F([F([F(x), F([F(y), F(z)])])])])

Naturally, how to reproduce the other two results in Maple programmatically? (The output may not be easy to read or understand; I have added an addendum below.)

Addendum. It is also possible to display in "tree" structure (like dismantle) manually: 

`[]`
(
    `[]`
    (
        `[]`
        (
            `[]`
            (
                s
            ,
                t
            )
        )
    ,
        `[]`
        (
            u
        ,
            `[]`
            (
                v
            ,
                w
            )
        )
    )
,
    `[]`
    (
        `[]`
        (
            x
        ,
            `[]`
            (
                y
            ,
                z
            )
        )
    )
)

As you can see, the "depth" of  is five (0, 1, 2, 3, and 4), while the classical map just maps at the first "level". (Moreover, such descriptions may lead to a confusion.)

Supplement. Unfortunately, there remains a bug in the MmaTranslator[Mma][Level]. Compare: 

MmaTranslator:-Mma:-Level(nl, [4]); (*Maple*)
                             [v, w]

MmaTranslator:-Mma:-Level(nl, [-1]); (*Maple*)
          [s, t, u, v, w, x, y, z, -1, x, c, r, y, 2]

In[6]:= Level[nl, {4}] (*Mathematica*)

Out[6]= {s, t, v, w, y, z}

In[7]:= Level[nl, {-1}] (*Mathematica*)

Out[7]= {s, t, u, v, w, x, y, z}
1 2 3 4 5 Page 1 of 5