sursumCorda

922 Reputation

13 Badges

2 years, 207 days

MaplePrimes Activity


These are questions asked by sursumCorda

Here is an apparent instance: 

interface(version);

maximize((1+20230321)*x*y-(x^2+y^2)*(1/2))``

`Standard Worksheet Interface, Maple 2023.0, Windows 10, March 6 2023 Build ID 1689885`

 

0

(1)

NULL

Download incorrectAgain.mw

Why???
Quite evidently, its supremum cannot be 0. (There are, of course, other approaches to compute it symbolically, yet I just wonder about the cause of this bug.)

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: 

First 13 14 15 16 17 18 19 Page 15 of 19