Conflict with hidden namesWhy is FreeQ so much faster for some specific variable names?How to avoid conflicts between local variable names and symbolic arguments in Block constructs?How to avoid nested With[]?Issue with scope of variablesOdd problem with ModuleSetting local variables with another functionModule inside Module. Conflict of names in argumentsUse of Module With Functions
What's the next step in this Unequal (Futoshiki) puzzle?
Is there a way to hide HTML source code yet keeping it effective?
Does "as soon as" imply simultaneity?
What's the story to "WotC gave up on fixing Polymorph"?
Is "ln" (natural log) and "log" the same thing if used in this answer?
Two trains move towards each other, a bird moves between them. How many trips can the bird make?
What is the difference between an astronaut in the ISS and a freediver in perfect neutral buoyancy?
Co-Supervisor comes to office to help her students which distracts me
1, 2, 4, 8, 16, ... 33?
Why is (inf + 0j)*1 == inf + nanj?
Do we have any particular tonal center in mind when we are NOT listening music?
Would Taiwan and China's dispute be solved if Taiwan gave up being the Republic of China?
Why does this image of Jupiter look so strange?
Do we know the situation in Britain before Sealion (summer 1940)?
Designing a time thief proof safe
Why did UK NHS pay for homeopathic treatments?
Is it impolite to ask for halal food when traveling to and in Thailand?
To what extent is it worthwhile to report check fraud / refund scams?
How to say "cheat sheet" in French
Going to France with limited French for a day
Is it possible to encode a message in such a way that can only be read by someone or something capable of seeing into the very near future?
What benefits does the Power Word Kill spell have?
Could Apollo astronauts see city lights from the moon?
Meaning of 小せェサル in the following sentence
Conflict with hidden names
Why is FreeQ so much faster for some specific variable names?How to avoid conflicts between local variable names and symbolic arguments in Block constructs?How to avoid nested With[]?Issue with scope of variablesOdd problem with ModuleSetting local variables with another functionModule inside Module. Conflict of names in argumentsUse of Module With Functions
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
The following code hangs Mathematica 12.0.0:
D[Log[1 - 1/p^Cos[t]], t, 4]
Unprotect[Plus];
Plus[x_?Positive, j_Gap] := 0;
Protect[Plus];
1 + 1
D[Log[1 - 1/p^Cos[t]], t, 4]
The weird line 3 is defining how to add numbers to objects with head "Gap", which is a structure I'm defining. (The right side of that line doesn't seem to matter, so I've made it 0 to simplify.) If one only asks for a 3rd derivative, or a fourth derivative of a simpler function, there is no problem.
But here's the surprise (to me): change "Gap" to "gap" or to "Gappe" and the issue disappears. My guess is that somewhere deep inside the trig simplification triggered by this derivative is a variable or function named "Gap", and somehow that's causing problems at the surface for me.
My question is simply this: What did I do wrong? Is this a bug, or is this behavior expected? Do I have to abandon using the head "Gap"? Is there a safer way to overload built-in functions?
scoping
$endgroup$
add a comment
|
$begingroup$
The following code hangs Mathematica 12.0.0:
D[Log[1 - 1/p^Cos[t]], t, 4]
Unprotect[Plus];
Plus[x_?Positive, j_Gap] := 0;
Protect[Plus];
1 + 1
D[Log[1 - 1/p^Cos[t]], t, 4]
The weird line 3 is defining how to add numbers to objects with head "Gap", which is a structure I'm defining. (The right side of that line doesn't seem to matter, so I've made it 0 to simplify.) If one only asks for a 3rd derivative, or a fourth derivative of a simpler function, there is no problem.
But here's the surprise (to me): change "Gap" to "gap" or to "Gappe" and the issue disappears. My guess is that somewhere deep inside the trig simplification triggered by this derivative is a variable or function named "Gap", and somehow that's causing problems at the surface for me.
My question is simply this: What did I do wrong? Is this a bug, or is this behavior expected? Do I have to abandon using the head "Gap"? Is there a safer way to overload built-in functions?
scoping
$endgroup$
3
$begingroup$
you might consider usingTagSetDelayed
instead of making changes toPlus
: e.g.,Gap /: Plus[x_?Positive, Gap[___]] := 0
$endgroup$
– kglr
8 hours ago
1
$begingroup$
btwNames["*`Gap"]
gives"ComputationalGeometry`Dump`Gap"
$endgroup$
– kglr
8 hours ago
1
$begingroup$
Remember how lots of times you see someone write something like: "In Mathematica built-in Symbols start with capital letters. Therefore it is accepted practice to not start user created symbol names with capital letters."? You have found another reason.
$endgroup$
– bill s
7 hours ago
$begingroup$
Changing "Gap" to "gap12" leads to the same problem. It is not about capitalization.
$endgroup$
– Kevin O'Bryant
6 hours ago
add a comment
|
$begingroup$
The following code hangs Mathematica 12.0.0:
D[Log[1 - 1/p^Cos[t]], t, 4]
Unprotect[Plus];
Plus[x_?Positive, j_Gap] := 0;
Protect[Plus];
1 + 1
D[Log[1 - 1/p^Cos[t]], t, 4]
The weird line 3 is defining how to add numbers to objects with head "Gap", which is a structure I'm defining. (The right side of that line doesn't seem to matter, so I've made it 0 to simplify.) If one only asks for a 3rd derivative, or a fourth derivative of a simpler function, there is no problem.
But here's the surprise (to me): change "Gap" to "gap" or to "Gappe" and the issue disappears. My guess is that somewhere deep inside the trig simplification triggered by this derivative is a variable or function named "Gap", and somehow that's causing problems at the surface for me.
My question is simply this: What did I do wrong? Is this a bug, or is this behavior expected? Do I have to abandon using the head "Gap"? Is there a safer way to overload built-in functions?
scoping
$endgroup$
The following code hangs Mathematica 12.0.0:
D[Log[1 - 1/p^Cos[t]], t, 4]
Unprotect[Plus];
Plus[x_?Positive, j_Gap] := 0;
Protect[Plus];
1 + 1
D[Log[1 - 1/p^Cos[t]], t, 4]
The weird line 3 is defining how to add numbers to objects with head "Gap", which is a structure I'm defining. (The right side of that line doesn't seem to matter, so I've made it 0 to simplify.) If one only asks for a 3rd derivative, or a fourth derivative of a simpler function, there is no problem.
But here's the surprise (to me): change "Gap" to "gap" or to "Gappe" and the issue disappears. My guess is that somewhere deep inside the trig simplification triggered by this derivative is a variable or function named "Gap", and somehow that's causing problems at the surface for me.
My question is simply this: What did I do wrong? Is this a bug, or is this behavior expected? Do I have to abandon using the head "Gap"? Is there a safer way to overload built-in functions?
scoping
scoping
asked 8 hours ago
Kevin O'BryantKevin O'Bryant
2212 silver badges7 bronze badges
2212 silver badges7 bronze badges
3
$begingroup$
you might consider usingTagSetDelayed
instead of making changes toPlus
: e.g.,Gap /: Plus[x_?Positive, Gap[___]] := 0
$endgroup$
– kglr
8 hours ago
1
$begingroup$
btwNames["*`Gap"]
gives"ComputationalGeometry`Dump`Gap"
$endgroup$
– kglr
8 hours ago
1
$begingroup$
Remember how lots of times you see someone write something like: "In Mathematica built-in Symbols start with capital letters. Therefore it is accepted practice to not start user created symbol names with capital letters."? You have found another reason.
$endgroup$
– bill s
7 hours ago
$begingroup$
Changing "Gap" to "gap12" leads to the same problem. It is not about capitalization.
$endgroup$
– Kevin O'Bryant
6 hours ago
add a comment
|
3
$begingroup$
you might consider usingTagSetDelayed
instead of making changes toPlus
: e.g.,Gap /: Plus[x_?Positive, Gap[___]] := 0
$endgroup$
– kglr
8 hours ago
1
$begingroup$
btwNames["*`Gap"]
gives"ComputationalGeometry`Dump`Gap"
$endgroup$
– kglr
8 hours ago
1
$begingroup$
Remember how lots of times you see someone write something like: "In Mathematica built-in Symbols start with capital letters. Therefore it is accepted practice to not start user created symbol names with capital letters."? You have found another reason.
$endgroup$
– bill s
7 hours ago
$begingroup$
Changing "Gap" to "gap12" leads to the same problem. It is not about capitalization.
$endgroup$
– Kevin O'Bryant
6 hours ago
3
3
$begingroup$
you might consider using
TagSetDelayed
instead of making changes to Plus
: e.g., Gap /: Plus[x_?Positive, Gap[___]] := 0
$endgroup$
– kglr
8 hours ago
$begingroup$
you might consider using
TagSetDelayed
instead of making changes to Plus
: e.g., Gap /: Plus[x_?Positive, Gap[___]] := 0
$endgroup$
– kglr
8 hours ago
1
1
$begingroup$
btw
Names["*`Gap"]
gives "ComputationalGeometry`Dump`Gap"
$endgroup$
– kglr
8 hours ago
$begingroup$
btw
Names["*`Gap"]
gives "ComputationalGeometry`Dump`Gap"
$endgroup$
– kglr
8 hours ago
1
1
$begingroup$
Remember how lots of times you see someone write something like: "In Mathematica built-in Symbols start with capital letters. Therefore it is accepted practice to not start user created symbol names with capital letters."? You have found another reason.
$endgroup$
– bill s
7 hours ago
$begingroup$
Remember how lots of times you see someone write something like: "In Mathematica built-in Symbols start with capital letters. Therefore it is accepted practice to not start user created symbol names with capital letters."? You have found another reason.
$endgroup$
– bill s
7 hours ago
$begingroup$
Changing "Gap" to "gap12" leads to the same problem. It is not about capitalization.
$endgroup$
– Kevin O'Bryant
6 hours ago
$begingroup$
Changing "Gap" to "gap12" leads to the same problem. It is not about capitalization.
$endgroup$
– Kevin O'Bryant
6 hours ago
add a comment
|
1 Answer
1
active
oldest
votes
$begingroup$
First, you should never give Plus
a downvalue. Always give your symbol upvalues instead by using TagSetDelayed
. Giving such a basic symbol in Mathematica a downvalue is sure to cause problems.
Now, I'm only guessing here, but this is my guess as to what is happening. When you give binary definitions to a Flat
, Orderless
function like Plus
, it can very easily lead to combinatorical explosions, where Mathematica tries lots of different orderings of the arguments to see if the binary rule will fire. I think that's what is happening in your example. For example, if we take the 3rd derivative, and then differentiate partial sums, we see an increase in timing consistent with a combinatorial explosion:
Unprotect[Plus];
Clear[Plus];
Plus[x_?Positive, j_Gap]:=0;
Protect[Plus];
r3 = D[Log[1-1/p^Cos[t]],t,3];
D[r3[[;;3]],t];//AbsoluteTiming
D[r3[[;;4]],t];//AbsoluteTiming
D[r3[[;;5]],t];//AbsoluteTiming
0.033469, Null
1.05953, Null
29.367, Null
I didn't have the patience to wait for the 6 term version to finish.
Now, for the hidden conflict question. Since Plus
is such an essential function, and it needs to be as fast as possible, I believe that internally Plus
will sometimes short circuit checking all possible orderings when an internal bloom filter says it is not needed. In this case, if Gap
is absent, then the downvalue can be avoided.
How does this bloom filter work? Basically, the bloom filter does a hash on an expression, and checks if the variable hash is absent. This means that if you choose a variable whose hash is not in the expression, then the code will be fast. If you choose a variable whose hash is in the expression, then the slow, combinatorial explosion causing code will be executed. This is why the code is fast for some variables, but not others.
See this answer for an overview for how this works. The upshot is the following:
With[e = r3, IntegerDigits[System`Private`GetContentCode[e], 2, 30]]
IntegerDigits[System`Private`GetContentCode[Gap], 2, 30]
IntegerDigits[System`Private`GetContentCode[gap], 2, 30]
IntegerDigits[System`Private`GetContentCode[Gappe], 2, 30]
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0
Notice how Gap
has an overlap with r3
, while gap
and Gappe
don't.
$endgroup$
$begingroup$
Why is the spelling of "Gap" relevant?
$endgroup$
– Kevin O'Bryant
6 hours ago
2
$begingroup$
@KevinO'Bryant Hopefully my update clarifies things.
$endgroup$
– Carl Woll
6 hours ago
$begingroup$
Indeed, using "gap12" (which has the same hash as "Gap") gives the same problem. Thanks!
$endgroup$
– Kevin O'Bryant
6 hours ago
add a comment
|
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f206560%2fconflict-with-hidden-names%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
First, you should never give Plus
a downvalue. Always give your symbol upvalues instead by using TagSetDelayed
. Giving such a basic symbol in Mathematica a downvalue is sure to cause problems.
Now, I'm only guessing here, but this is my guess as to what is happening. When you give binary definitions to a Flat
, Orderless
function like Plus
, it can very easily lead to combinatorical explosions, where Mathematica tries lots of different orderings of the arguments to see if the binary rule will fire. I think that's what is happening in your example. For example, if we take the 3rd derivative, and then differentiate partial sums, we see an increase in timing consistent with a combinatorial explosion:
Unprotect[Plus];
Clear[Plus];
Plus[x_?Positive, j_Gap]:=0;
Protect[Plus];
r3 = D[Log[1-1/p^Cos[t]],t,3];
D[r3[[;;3]],t];//AbsoluteTiming
D[r3[[;;4]],t];//AbsoluteTiming
D[r3[[;;5]],t];//AbsoluteTiming
0.033469, Null
1.05953, Null
29.367, Null
I didn't have the patience to wait for the 6 term version to finish.
Now, for the hidden conflict question. Since Plus
is such an essential function, and it needs to be as fast as possible, I believe that internally Plus
will sometimes short circuit checking all possible orderings when an internal bloom filter says it is not needed. In this case, if Gap
is absent, then the downvalue can be avoided.
How does this bloom filter work? Basically, the bloom filter does a hash on an expression, and checks if the variable hash is absent. This means that if you choose a variable whose hash is not in the expression, then the code will be fast. If you choose a variable whose hash is in the expression, then the slow, combinatorial explosion causing code will be executed. This is why the code is fast for some variables, but not others.
See this answer for an overview for how this works. The upshot is the following:
With[e = r3, IntegerDigits[System`Private`GetContentCode[e], 2, 30]]
IntegerDigits[System`Private`GetContentCode[Gap], 2, 30]
IntegerDigits[System`Private`GetContentCode[gap], 2, 30]
IntegerDigits[System`Private`GetContentCode[Gappe], 2, 30]
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0
Notice how Gap
has an overlap with r3
, while gap
and Gappe
don't.
$endgroup$
$begingroup$
Why is the spelling of "Gap" relevant?
$endgroup$
– Kevin O'Bryant
6 hours ago
2
$begingroup$
@KevinO'Bryant Hopefully my update clarifies things.
$endgroup$
– Carl Woll
6 hours ago
$begingroup$
Indeed, using "gap12" (which has the same hash as "Gap") gives the same problem. Thanks!
$endgroup$
– Kevin O'Bryant
6 hours ago
add a comment
|
$begingroup$
First, you should never give Plus
a downvalue. Always give your symbol upvalues instead by using TagSetDelayed
. Giving such a basic symbol in Mathematica a downvalue is sure to cause problems.
Now, I'm only guessing here, but this is my guess as to what is happening. When you give binary definitions to a Flat
, Orderless
function like Plus
, it can very easily lead to combinatorical explosions, where Mathematica tries lots of different orderings of the arguments to see if the binary rule will fire. I think that's what is happening in your example. For example, if we take the 3rd derivative, and then differentiate partial sums, we see an increase in timing consistent with a combinatorial explosion:
Unprotect[Plus];
Clear[Plus];
Plus[x_?Positive, j_Gap]:=0;
Protect[Plus];
r3 = D[Log[1-1/p^Cos[t]],t,3];
D[r3[[;;3]],t];//AbsoluteTiming
D[r3[[;;4]],t];//AbsoluteTiming
D[r3[[;;5]],t];//AbsoluteTiming
0.033469, Null
1.05953, Null
29.367, Null
I didn't have the patience to wait for the 6 term version to finish.
Now, for the hidden conflict question. Since Plus
is such an essential function, and it needs to be as fast as possible, I believe that internally Plus
will sometimes short circuit checking all possible orderings when an internal bloom filter says it is not needed. In this case, if Gap
is absent, then the downvalue can be avoided.
How does this bloom filter work? Basically, the bloom filter does a hash on an expression, and checks if the variable hash is absent. This means that if you choose a variable whose hash is not in the expression, then the code will be fast. If you choose a variable whose hash is in the expression, then the slow, combinatorial explosion causing code will be executed. This is why the code is fast for some variables, but not others.
See this answer for an overview for how this works. The upshot is the following:
With[e = r3, IntegerDigits[System`Private`GetContentCode[e], 2, 30]]
IntegerDigits[System`Private`GetContentCode[Gap], 2, 30]
IntegerDigits[System`Private`GetContentCode[gap], 2, 30]
IntegerDigits[System`Private`GetContentCode[Gappe], 2, 30]
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0
Notice how Gap
has an overlap with r3
, while gap
and Gappe
don't.
$endgroup$
$begingroup$
Why is the spelling of "Gap" relevant?
$endgroup$
– Kevin O'Bryant
6 hours ago
2
$begingroup$
@KevinO'Bryant Hopefully my update clarifies things.
$endgroup$
– Carl Woll
6 hours ago
$begingroup$
Indeed, using "gap12" (which has the same hash as "Gap") gives the same problem. Thanks!
$endgroup$
– Kevin O'Bryant
6 hours ago
add a comment
|
$begingroup$
First, you should never give Plus
a downvalue. Always give your symbol upvalues instead by using TagSetDelayed
. Giving such a basic symbol in Mathematica a downvalue is sure to cause problems.
Now, I'm only guessing here, but this is my guess as to what is happening. When you give binary definitions to a Flat
, Orderless
function like Plus
, it can very easily lead to combinatorical explosions, where Mathematica tries lots of different orderings of the arguments to see if the binary rule will fire. I think that's what is happening in your example. For example, if we take the 3rd derivative, and then differentiate partial sums, we see an increase in timing consistent with a combinatorial explosion:
Unprotect[Plus];
Clear[Plus];
Plus[x_?Positive, j_Gap]:=0;
Protect[Plus];
r3 = D[Log[1-1/p^Cos[t]],t,3];
D[r3[[;;3]],t];//AbsoluteTiming
D[r3[[;;4]],t];//AbsoluteTiming
D[r3[[;;5]],t];//AbsoluteTiming
0.033469, Null
1.05953, Null
29.367, Null
I didn't have the patience to wait for the 6 term version to finish.
Now, for the hidden conflict question. Since Plus
is such an essential function, and it needs to be as fast as possible, I believe that internally Plus
will sometimes short circuit checking all possible orderings when an internal bloom filter says it is not needed. In this case, if Gap
is absent, then the downvalue can be avoided.
How does this bloom filter work? Basically, the bloom filter does a hash on an expression, and checks if the variable hash is absent. This means that if you choose a variable whose hash is not in the expression, then the code will be fast. If you choose a variable whose hash is in the expression, then the slow, combinatorial explosion causing code will be executed. This is why the code is fast for some variables, but not others.
See this answer for an overview for how this works. The upshot is the following:
With[e = r3, IntegerDigits[System`Private`GetContentCode[e], 2, 30]]
IntegerDigits[System`Private`GetContentCode[Gap], 2, 30]
IntegerDigits[System`Private`GetContentCode[gap], 2, 30]
IntegerDigits[System`Private`GetContentCode[Gappe], 2, 30]
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0
Notice how Gap
has an overlap with r3
, while gap
and Gappe
don't.
$endgroup$
First, you should never give Plus
a downvalue. Always give your symbol upvalues instead by using TagSetDelayed
. Giving such a basic symbol in Mathematica a downvalue is sure to cause problems.
Now, I'm only guessing here, but this is my guess as to what is happening. When you give binary definitions to a Flat
, Orderless
function like Plus
, it can very easily lead to combinatorical explosions, where Mathematica tries lots of different orderings of the arguments to see if the binary rule will fire. I think that's what is happening in your example. For example, if we take the 3rd derivative, and then differentiate partial sums, we see an increase in timing consistent with a combinatorial explosion:
Unprotect[Plus];
Clear[Plus];
Plus[x_?Positive, j_Gap]:=0;
Protect[Plus];
r3 = D[Log[1-1/p^Cos[t]],t,3];
D[r3[[;;3]],t];//AbsoluteTiming
D[r3[[;;4]],t];//AbsoluteTiming
D[r3[[;;5]],t];//AbsoluteTiming
0.033469, Null
1.05953, Null
29.367, Null
I didn't have the patience to wait for the 6 term version to finish.
Now, for the hidden conflict question. Since Plus
is such an essential function, and it needs to be as fast as possible, I believe that internally Plus
will sometimes short circuit checking all possible orderings when an internal bloom filter says it is not needed. In this case, if Gap
is absent, then the downvalue can be avoided.
How does this bloom filter work? Basically, the bloom filter does a hash on an expression, and checks if the variable hash is absent. This means that if you choose a variable whose hash is not in the expression, then the code will be fast. If you choose a variable whose hash is in the expression, then the slow, combinatorial explosion causing code will be executed. This is why the code is fast for some variables, but not others.
See this answer for an overview for how this works. The upshot is the following:
With[e = r3, IntegerDigits[System`Private`GetContentCode[e], 2, 30]]
IntegerDigits[System`Private`GetContentCode[Gap], 2, 30]
IntegerDigits[System`Private`GetContentCode[gap], 2, 30]
IntegerDigits[System`Private`GetContentCode[Gappe], 2, 30]
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0
Notice how Gap
has an overlap with r3
, while gap
and Gappe
don't.
edited 6 hours ago
answered 6 hours ago
Carl WollCarl Woll
90.4k3 gold badges118 silver badges231 bronze badges
90.4k3 gold badges118 silver badges231 bronze badges
$begingroup$
Why is the spelling of "Gap" relevant?
$endgroup$
– Kevin O'Bryant
6 hours ago
2
$begingroup$
@KevinO'Bryant Hopefully my update clarifies things.
$endgroup$
– Carl Woll
6 hours ago
$begingroup$
Indeed, using "gap12" (which has the same hash as "Gap") gives the same problem. Thanks!
$endgroup$
– Kevin O'Bryant
6 hours ago
add a comment
|
$begingroup$
Why is the spelling of "Gap" relevant?
$endgroup$
– Kevin O'Bryant
6 hours ago
2
$begingroup$
@KevinO'Bryant Hopefully my update clarifies things.
$endgroup$
– Carl Woll
6 hours ago
$begingroup$
Indeed, using "gap12" (which has the same hash as "Gap") gives the same problem. Thanks!
$endgroup$
– Kevin O'Bryant
6 hours ago
$begingroup$
Why is the spelling of "Gap" relevant?
$endgroup$
– Kevin O'Bryant
6 hours ago
$begingroup$
Why is the spelling of "Gap" relevant?
$endgroup$
– Kevin O'Bryant
6 hours ago
2
2
$begingroup$
@KevinO'Bryant Hopefully my update clarifies things.
$endgroup$
– Carl Woll
6 hours ago
$begingroup$
@KevinO'Bryant Hopefully my update clarifies things.
$endgroup$
– Carl Woll
6 hours ago
$begingroup$
Indeed, using "gap12" (which has the same hash as "Gap") gives the same problem. Thanks!
$endgroup$
– Kevin O'Bryant
6 hours ago
$begingroup$
Indeed, using "gap12" (which has the same hash as "Gap") gives the same problem. Thanks!
$endgroup$
– Kevin O'Bryant
6 hours ago
add a comment
|
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f206560%2fconflict-with-hidden-names%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
$begingroup$
you might consider using
TagSetDelayed
instead of making changes toPlus
: e.g.,Gap /: Plus[x_?Positive, Gap[___]] := 0
$endgroup$
– kglr
8 hours ago
1
$begingroup$
btw
Names["*`Gap"]
gives"ComputationalGeometry`Dump`Gap"
$endgroup$
– kglr
8 hours ago
1
$begingroup$
Remember how lots of times you see someone write something like: "In Mathematica built-in Symbols start with capital letters. Therefore it is accepted practice to not start user created symbol names with capital letters."? You have found another reason.
$endgroup$
– bill s
7 hours ago
$begingroup$
Changing "Gap" to "gap12" leads to the same problem. It is not about capitalization.
$endgroup$
– Kevin O'Bryant
6 hours ago