« first day (1819 days earlier)      last day (2655 days later) » 

10:50 AM
I just noticed that ReplaceList[p + q + r, a__ + b__ :> {a}] returns {{p}, {q}, {r}, {p, q}, {p, r}, {q, r}} which is only half as many results as I'd expect. I was just about to post a question for this but after some searching I found mathematica.stackexchange.com/q/121564/2305 and now I'm not sure whether this is just another incarnation of this bug or whether this is some other behaviour.
Does anyone have 10.0 or earlier around to check whether the result is the same?
 
@MartinEnder fyi
 
Thanks. I think this is the question I was looking for.
sort of
it doesn't quite explain how I can end up with {p,r} or {q} in the results, I think.
 
 
1 hour later…
12:06 PM
Can anyone explain to me what is the difference between URLRead and URLExecute. As for someone who has only basic knowledge about web world.
 
 
1 hour later…
1:08 PM
@MartinEnder Look, it matches a pattern. You input is
In[4]:= p+q+r//FullForm
Out[4]//FullForm= Plus[p,q,r]
 
@halirutan Sorry, I don't see what you're getting at.
 
When you now try to match Plus[a__,b__] there is no way to bind e.g. a to q. What should happen with the p infront? It is not matched.
 
oh sorry, you weren't done yet
@halirutan exactly. but is matched.
(see Nasser's screenshot)
 
@MartinEnder Well, ...
 
I've since left a comment on the answer by MarcoB that I linked, because that thread is closest to the issue at hand
 
1:12 PM
@MartinEnder @halirutan isn't that about Plus' Orderless?
Block[{h},
 Attributes[h] = Attributes[Plus];
 MatchQ[h[p, q, r], h[q, ___]]
 ]
>True
 
yes, I've chosen Plus just as an example of an orderless head
 
Isn't that expected then?
 
well it's kinda inconsistent. ReplaceList doesn't really look at all possible permutations.
 
@MartinEnder @Kuba Yes, that is the answer.
 
okay, I probably haven't expressed clearly enough what my issue is
yes, I do expect h[q, ___] to match. But if we look at the larger example ReplaceList[p + q + r, a__ + b__ :> {a}] I started this off with, then there are really 12 possible matches if we look at all possible permutations. p + q + r can be ordered into six different permutations and then there are two ways for each of them to split them into a and b.
(or in other words, either a has one element, in which case there are three choices for a and then two orders for b for for each of them, or vice versa)
 
1:17 PM
@MartinEnder so you expect {q, p } together with {p, q}?
 
however, ReplaceList only gives 6 possible results because it doesn't return the possible orderings of the two-element sequence
@Kuba yes (or alternatively, the reason why it isn't there)
MarcoB's answer addressed a related issue by stating that Orderless only makes the pattern matcher try all permutations of the pattern, not the thing it's matching against.
But that is inconsistent with being able to assign a__ to q.
 
@MartinEnder I see know.
 
@MartinEnder @Kuba But if order doesn't matter, then one would expect that results {p,q} and {q,p} are treated as equal.
 
@halirutan I think so but how to explain that with docs
 
@Kuba Thats going to be hard.
> In matching patterns with Orderless functions, all possible orders of arguments are tried.
 
1:32 PM
Is it worth starting another question for this?
 
@MartinEnder Can we find an example, where the rule forces ReplaceList to give us at least a hint that it really tried all permutations?
 
@MartinEnder I think so, what would be nice is to link those related topics and ask for a big picture.
 
@halirutan it appears that it actually doesn't try those additional orders
Try:
SetAttribute[o, Orderless];
ReplaceList[o[1, 2, 3], o[a__, b__] /; b > 0 :> {{a}, {b}}]
(* {{{1, 2}, {3}}, {{1, 3}, {2}}, {{2, 3}, {1}}} *)
 
@MartinEnder I had thought of something like this:
o[i_, rest__] := Module[{}, True /; (Print[i]; False)];
SetAttributes[o, Orderless];
ReplaceList[o[1, 2, 3], o[a__, b__] :> {{a}, {b}}]
If you evaluate o[1,2,3] then i is printed but o[1,2,3] is still in its unevaluated form.
 
I don't think I understand why that does what it does.
 
1:44 PM
@MartinEnder It is a "special feature" of Module which returns like nothing happened, when the tail test returns false.
The True in the body can be anything. It is never reached.
 
huh okay
 
@MartinEnder I hoped that it printed something when ReplaceList does its work, but it doesn't
 
would you not consider my snippet proof that those permutations aren't attempted? clearly assigning 1 to a__ and 3, 2 to b__ would make the pattern work, but it's not included in the output.
 
@MartinEnder OK, can you explain this? Why would assigning 3,2 to b work? Basically, we have then the test
OK, I see..
In[56]:= Sequence[3,2]>0//Trace
Out[56]= {Sequence[3,2]>0,3>2>0,True}
 
Yeah it's basically Greater[b,0] which is Greater[3,2,0]
I guess I could also have used OrderedQ@Reverse@{b} or something.
 
2:04 PM
@MartinEnder To summarize, if the matcher would test all permutations, it should see the constallation {1, {3,2}} which makes the rule applyable. Since this is not included in the output, we assume that it doesn't actually tries this, right?
 
@MartinEnder If you are going to ask a Q about this, then you should carefully explain everything :)
 
yeah, I'll try
 
@MartinEnder please ping me here with the link to the question.
 
will do
 
2:11 PM
you're never going to see {{1},{2,3}} because the pattern a__ is greedy
 
@BenNiehoff How can you explain this output then?
In[60]:= ReplaceList[p+q+r,a__+b__:>{b}]
Out[60]= {{q,r},{p,r},{p,q},{r},{q},{p}}
 
interesting
oh, but your pattern has + in it as well, so both the original expression AND your pattern are Orderless
 
@BenNiehoff Yes, that is what we suspected. But when you look at this example here:
 
I don't think greediness is relevant for ReplaceList:
In[150]:= ReplaceList[{p, q, r}, {a__, b__} :> {a}]
Out[150]= {{p}, {p, q}}
 
37 mins ago, by Martin Ender
SetAttribute[o, Orderless];
ReplaceList[o[1, 2, 3], o[a__, b__] /; b > 0 :> {{a}, {b}}]
then the b in b>0 does not contain the orderless Plus anymore. And if it really tests all permutations, then we should see the result.
But clearly, we have a misconception of what really happens or this gets lost for optimization as it is not relevant in the use-cases.
 
2:21 PM
This is revealing:
In[25]:= ReplaceList[o[a, b, c, d], o[a__, b__] :> {a}]

Out[25]= {{a}, {b}, {c}, {d}, {a, b}, {a, c}, {a, d}, {b, c}, {b,
d}, {c, d}, {a, b, c}, {a, b, d}, {a, c, d}, {b, c, d}}
for Orderless o
 
It's the same thing, right? It tries all possible assignments to a and b, but no permutations within these assignments.
It seems like it operates under the assumption that sequences in orderless pattern matching are themselves only used in orderless contexts, so you only get one permutation.
 
I think it does try every ordering, but since the pattern itself is Orderless, it only records one ordering for each subsequence matched
In[26]:= ReplaceList[o[c, b, a], o[a__, b__] :> {a}]

Out[26]= {{a}, {b}, {c}, {a, b}, {a, c}, {b, c}}
 
Well, the test with the /; b > 0 condition suggests that it doesn't actually try unordered permutations.
 
here you can see that once it matches a subsequence, that subsequence gets sorted
 
@BenNiehoff I think this is because the left-hand side gets sorted implicitly, before any pattern matching is done.
 
2:24 PM
and
In[61]:= Attributes[BlankSequence]

Out[61]= {Protected}
is not orderless
(and shouldn't be)
 
if it did, then the attribute wouldn't refer to the elements it matches anyway
 
note that trying all combinations has quadratic complexity, whereas all permutations is factorial
my guess is, MMA assumes that since you have an Orderless function on the LHS of your pattern, it's nonsensical for you to care about the order of subsequences on the RHS
actually, sorry, "all combinations" = "all subsets" and is exponential. Still better than factorial :D
 
this doesn't seem to be limited to ReplaceList
In[169]:= MatchQ[o[1, 2, 3], o[a__, b__] /; {b} == {1, 3}]
          MatchQ[o[1, 2, 3], o[a__, b__] /; {b} == {3, 1}]
Out[169]= True
Out[170]= False
So the assumption is that it will try all permutations and lengths of sequences, but it will only ever give you ordered sequences.
 
it probably uses Sort before it looks for patterns, as that would be more efficient
 
2:42 PM
I think that doesn't even have anything to do with the pattern matching. The point of Orderless is that arguments get immediately sorted into canonical order. Try:
In[171]:= o[3, 1, 2]
Out[171]= o[1, 2, 3]
nevertheless it does some rearranging during the pattern matching, otherwise you couldn't assign 1, 3 to b (which is possible as per the previous snippet). But it stops doing the rearranging inside sequence patterns.
 
Anyone here know how to convert HSV colors to positions on the visible spectrum?
 
@Kuba One way to think of it is that URLExecute is like URLRead of the "Body" of the incoming response plus `ImportString.
URLExecute[req, "XYZ"] ~~~ ImportString[URLRead[req, "Body"], "XYZ"]
It's not quite as limited as I said, but its close enough for starting out
 
3:34 PM
@chuy was suspecting that but wasn't sure. Thanks :)
You never know if you are missing something when you are not very experienced in particular area.
 
 
7 hours later…
10:51 PM
I see many questions, where it is clear the user did not even bother to see what the code did before the final statement. They show 100 lines, all with ";" at the end of each line, except for the last one, and then ask why didn't the last line give the expected output or why it did nothing. When all they needed to do, is look at the output of lines before to see the problem down stream. This seems to happen so often.
 

« first day (1819 days earlier)      last day (2655 days later) »