Conflict with hidden namesWhy is FreeQ so much faster for some specific variable names?How to avoid conflicts...
Why did UK NHS pay for homeopathic treatments?
How to manage expenditure when billing cycles and paycheck cycles are not aligned?
How can this Stack Exchange site have an animated favicon?
How do I deal with too many NPCs in my campaign?
If an object moving in a circle experiences centripetal force, then doesn't it also experience centrifugal force, because of Newton's third law?
What's the story to "WotC gave up on fixing Polymorph"?
1, 2, 4, 8, 16, ... 33?
What exactly did this mechanic sabotage on the American Airlines 737, and how dangerous was it?
Can I take NEW (still in their boxes) PC PARTS in my checked in luggage?
How do I set a custom order for folders on Windows 7 and 10?
Do we know the situation in Britain before Sealion (summer 1940)?
Late 1970's and 6502 chip facilities for operating systems
How to deal with a Homophobic PC
Why weren't the Death Star plans transmitted electronically?
Subverting the emotional woman and stoic man trope
Basic digital RC approximation filter in python (Micropython)
How can I repair this gas leak on my new range? Teflon tape isn't working
How to justify a team increase when the team is doing good?
Can a broken/split chain be reassembled?
2000s Animated TV show where teenagers could physically go into a virtual world
On the meaning of 'anyways' in "What Exactly Is a Quartz Crystal, Anyways?"
Magneto 2 How to call Helper function in observer file
How do you use the interjection for snorting?
What is the meaning of word 'crack' in chapter 33 of A Game of Thrones?
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
7 hours ago
1
$begingroup$
btwNames["*`Gap"]
gives{"ComputationalGeometry`Dump`Gap"}
$endgroup$
– kglr
7 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
5 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
7 hours ago
1
$begingroup$
btwNames["*`Gap"]
gives{"ComputationalGeometry`Dump`Gap"}
$endgroup$
– kglr
7 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
5 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
7 hours ago
1
$begingroup$
btwNames["*`Gap"]
gives{"ComputationalGeometry`Dump`Gap"}
$endgroup$
– kglr
7 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
5 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
7 hours ago
$begingroup$
you might consider using
TagSetDelayed
instead of making changes to Plus
: e.g., Gap /: Plus[x_?Positive, Gap[___]] := 0
$endgroup$
– kglr
7 hours ago
1
1
$begingroup$
btw
Names["*`Gap"]
gives {"ComputationalGeometry`Dump`Gap"}
$endgroup$
– kglr
7 hours ago
$begingroup$
btw
Names["*`Gap"]
gives {"ComputationalGeometry`Dump`Gap"}
$endgroup$
– kglr
7 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
5 hours ago
$begingroup$
Changing "Gap" to "gap12" leads to the same problem. It is not about capitalization.
$endgroup$
– Kevin O'Bryant
5 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
5 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
5 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
5 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
5 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
5 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
5 hours ago
$begingroup$
Indeed, using "gap12" (which has the same hash as "Gap") gives the same problem. Thanks!
$endgroup$
– Kevin O'Bryant
5 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
7 hours ago
1
$begingroup$
btw
Names["*`Gap"]
gives{"ComputationalGeometry`Dump`Gap"}
$endgroup$
– kglr
7 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
5 hours ago