Associativity of upvalue

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite












how can I ensure the following definition to be associative?



Unprotect[Times];
a_ f[x_] + b_ f[y_] ^:= f[a x + b y]
Protect[Times];


gives me



a f[x] + b f[y] + c f[z]



f[a x + b y] + c f[z]




I'd like it to be




f[a x + b y + c z]











share|improve this question























  • You probably need to set default values for the patterns a_ and b_ like a_. and b_., otherwise [ScriptCapitalN][a x + b y] does not match the pattern of multiplier_ [ScriptCapitalN][a x + b y]
    – LLlAMnYP
    yesterday











  • My previous comment about Times vs. Plus was of course wrong.
    – LLlAMnYP
    yesterday










  • Well it works with the default value. But I don't really understand why I need it.
    – Display Name
    yesterday










  • And if c=1 it does not work anymore.
    – Display Name
    yesterday






  • 1




    @LLlAMnYP That's easy to achieve. Just go to thw NinjaKot script and add ['\\\[ScriptCapitalN\]', 'f'], on line 599 =D
    – Henrik Schumacher
    yesterday















up vote
5
down vote

favorite












how can I ensure the following definition to be associative?



Unprotect[Times];
a_ f[x_] + b_ f[y_] ^:= f[a x + b y]
Protect[Times];


gives me



a f[x] + b f[y] + c f[z]



f[a x + b y] + c f[z]




I'd like it to be




f[a x + b y + c z]











share|improve this question























  • You probably need to set default values for the patterns a_ and b_ like a_. and b_., otherwise [ScriptCapitalN][a x + b y] does not match the pattern of multiplier_ [ScriptCapitalN][a x + b y]
    – LLlAMnYP
    yesterday











  • My previous comment about Times vs. Plus was of course wrong.
    – LLlAMnYP
    yesterday










  • Well it works with the default value. But I don't really understand why I need it.
    – Display Name
    yesterday










  • And if c=1 it does not work anymore.
    – Display Name
    yesterday






  • 1




    @LLlAMnYP That's easy to achieve. Just go to thw NinjaKot script and add ['\\\[ScriptCapitalN\]', 'f'], on line 599 =D
    – Henrik Schumacher
    yesterday













up vote
5
down vote

favorite









up vote
5
down vote

favorite











how can I ensure the following definition to be associative?



Unprotect[Times];
a_ f[x_] + b_ f[y_] ^:= f[a x + b y]
Protect[Times];


gives me



a f[x] + b f[y] + c f[z]



f[a x + b y] + c f[z]




I'd like it to be




f[a x + b y + c z]











share|improve this question















how can I ensure the following definition to be associative?



Unprotect[Times];
a_ f[x_] + b_ f[y_] ^:= f[a x + b y]
Protect[Times];


gives me



a f[x] + b f[y] + c f[z]



f[a x + b y] + c f[z]




I'd like it to be




f[a x + b y + c z]








associativity






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Henrik Schumacher

37.5k249107




37.5k249107










asked yesterday









Display Name

42338




42338











  • You probably need to set default values for the patterns a_ and b_ like a_. and b_., otherwise [ScriptCapitalN][a x + b y] does not match the pattern of multiplier_ [ScriptCapitalN][a x + b y]
    – LLlAMnYP
    yesterday











  • My previous comment about Times vs. Plus was of course wrong.
    – LLlAMnYP
    yesterday










  • Well it works with the default value. But I don't really understand why I need it.
    – Display Name
    yesterday










  • And if c=1 it does not work anymore.
    – Display Name
    yesterday






  • 1




    @LLlAMnYP That's easy to achieve. Just go to thw NinjaKot script and add ['\\\[ScriptCapitalN\]', 'f'], on line 599 =D
    – Henrik Schumacher
    yesterday

















  • You probably need to set default values for the patterns a_ and b_ like a_. and b_., otherwise [ScriptCapitalN][a x + b y] does not match the pattern of multiplier_ [ScriptCapitalN][a x + b y]
    – LLlAMnYP
    yesterday











  • My previous comment about Times vs. Plus was of course wrong.
    – LLlAMnYP
    yesterday










  • Well it works with the default value. But I don't really understand why I need it.
    – Display Name
    yesterday










  • And if c=1 it does not work anymore.
    – Display Name
    yesterday






  • 1




    @LLlAMnYP That's easy to achieve. Just go to thw NinjaKot script and add ['\\\[ScriptCapitalN\]', 'f'], on line 599 =D
    – Henrik Schumacher
    yesterday
















You probably need to set default values for the patterns a_ and b_ like a_. and b_., otherwise [ScriptCapitalN][a x + b y] does not match the pattern of multiplier_ [ScriptCapitalN][a x + b y]
– LLlAMnYP
yesterday





You probably need to set default values for the patterns a_ and b_ like a_. and b_., otherwise [ScriptCapitalN][a x + b y] does not match the pattern of multiplier_ [ScriptCapitalN][a x + b y]
– LLlAMnYP
yesterday













My previous comment about Times vs. Plus was of course wrong.
– LLlAMnYP
yesterday




My previous comment about Times vs. Plus was of course wrong.
– LLlAMnYP
yesterday












Well it works with the default value. But I don't really understand why I need it.
– Display Name
yesterday




Well it works with the default value. But I don't really understand why I need it.
– Display Name
yesterday












And if c=1 it does not work anymore.
– Display Name
yesterday




And if c=1 it does not work anymore.
– Display Name
yesterday




1




1




@LLlAMnYP That's easy to achieve. Just go to thw NinjaKot script and add ['\\\[ScriptCapitalN\]', 'f'], on line 599 =D
– Henrik Schumacher
yesterday





@LLlAMnYP That's easy to achieve. Just go to thw NinjaKot script and add ['\\\[ScriptCapitalN\]', 'f'], on line 599 =D
– Henrik Schumacher
yesterday











1 Answer
1






active

oldest

votes

















up vote
9
down vote



accepted










f[a x + b y] + c f[z]


is not the same as



1 * f[a x + b y] + c f[z]


so the pattern



a_ f[x] + b_ f[y]


does not match it. But if you use a_. then it's replaced with the default value (1, for Times) if the multiplier is omitted. So then it will match.



However, you observe that if c == 1, then it doesn't work again. This is understandable, since if c == 1, the expression



f[a x + b y] + f[z]


does not contain Times at level 1 anymore. I suggest using more granular rules:



a_ f[x_] ^:= f[a x]

f[x_] + f[y_] ^:= f[x + y]


This achieves two things: the upvalue is attached to f and not to system symbols, and it solves the problem you mentioned in the comments.






share|improve this answer






















  • Thank you for the explanation. But isn't the case c==1 a variant of the first case, now with the two multipliers omitted? And shouldn't then the two multipliers be replaced with their default values? Or did I get it completely wrong what you tried to point out? Edit Or can only be one default value be replaced at a time?
    – Display Name
    yesterday







  • 1




    The UpValue in your attempt is associated with Times. I replace scriptcapitalN with just N for brevity: N[x] + b N[y] is Plus[N[x], Times[b, N[y]]]. Times is present at level 1 of the expression, so MMA checks UpValues for the symbol Times. However, N[x] + N[y] is Plus[N[x], N[y]], so MMA has no waying of realizing that it has to apply the UpValue defined for Times. So it doesn't even get round to doing that and attempting to substitute default values in the process.
    – LLlAMnYP
    yesterday










  • Thank you, makes perfectly sense.
    – Display Name
    yesterday










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");

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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181671%2fassociativity-of-upvalue%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
9
down vote



accepted










f[a x + b y] + c f[z]


is not the same as



1 * f[a x + b y] + c f[z]


so the pattern



a_ f[x] + b_ f[y]


does not match it. But if you use a_. then it's replaced with the default value (1, for Times) if the multiplier is omitted. So then it will match.



However, you observe that if c == 1, then it doesn't work again. This is understandable, since if c == 1, the expression



f[a x + b y] + f[z]


does not contain Times at level 1 anymore. I suggest using more granular rules:



a_ f[x_] ^:= f[a x]

f[x_] + f[y_] ^:= f[x + y]


This achieves two things: the upvalue is attached to f and not to system symbols, and it solves the problem you mentioned in the comments.






share|improve this answer






















  • Thank you for the explanation. But isn't the case c==1 a variant of the first case, now with the two multipliers omitted? And shouldn't then the two multipliers be replaced with their default values? Or did I get it completely wrong what you tried to point out? Edit Or can only be one default value be replaced at a time?
    – Display Name
    yesterday







  • 1




    The UpValue in your attempt is associated with Times. I replace scriptcapitalN with just N for brevity: N[x] + b N[y] is Plus[N[x], Times[b, N[y]]]. Times is present at level 1 of the expression, so MMA checks UpValues for the symbol Times. However, N[x] + N[y] is Plus[N[x], N[y]], so MMA has no waying of realizing that it has to apply the UpValue defined for Times. So it doesn't even get round to doing that and attempting to substitute default values in the process.
    – LLlAMnYP
    yesterday










  • Thank you, makes perfectly sense.
    – Display Name
    yesterday














up vote
9
down vote



accepted










f[a x + b y] + c f[z]


is not the same as



1 * f[a x + b y] + c f[z]


so the pattern



a_ f[x] + b_ f[y]


does not match it. But if you use a_. then it's replaced with the default value (1, for Times) if the multiplier is omitted. So then it will match.



However, you observe that if c == 1, then it doesn't work again. This is understandable, since if c == 1, the expression



f[a x + b y] + f[z]


does not contain Times at level 1 anymore. I suggest using more granular rules:



a_ f[x_] ^:= f[a x]

f[x_] + f[y_] ^:= f[x + y]


This achieves two things: the upvalue is attached to f and not to system symbols, and it solves the problem you mentioned in the comments.






share|improve this answer






















  • Thank you for the explanation. But isn't the case c==1 a variant of the first case, now with the two multipliers omitted? And shouldn't then the two multipliers be replaced with their default values? Or did I get it completely wrong what you tried to point out? Edit Or can only be one default value be replaced at a time?
    – Display Name
    yesterday







  • 1




    The UpValue in your attempt is associated with Times. I replace scriptcapitalN with just N for brevity: N[x] + b N[y] is Plus[N[x], Times[b, N[y]]]. Times is present at level 1 of the expression, so MMA checks UpValues for the symbol Times. However, N[x] + N[y] is Plus[N[x], N[y]], so MMA has no waying of realizing that it has to apply the UpValue defined for Times. So it doesn't even get round to doing that and attempting to substitute default values in the process.
    – LLlAMnYP
    yesterday










  • Thank you, makes perfectly sense.
    – Display Name
    yesterday












up vote
9
down vote



accepted







up vote
9
down vote



accepted






f[a x + b y] + c f[z]


is not the same as



1 * f[a x + b y] + c f[z]


so the pattern



a_ f[x] + b_ f[y]


does not match it. But if you use a_. then it's replaced with the default value (1, for Times) if the multiplier is omitted. So then it will match.



However, you observe that if c == 1, then it doesn't work again. This is understandable, since if c == 1, the expression



f[a x + b y] + f[z]


does not contain Times at level 1 anymore. I suggest using more granular rules:



a_ f[x_] ^:= f[a x]

f[x_] + f[y_] ^:= f[x + y]


This achieves two things: the upvalue is attached to f and not to system symbols, and it solves the problem you mentioned in the comments.






share|improve this answer














f[a x + b y] + c f[z]


is not the same as



1 * f[a x + b y] + c f[z]


so the pattern



a_ f[x] + b_ f[y]


does not match it. But if you use a_. then it's replaced with the default value (1, for Times) if the multiplier is omitted. So then it will match.



However, you observe that if c == 1, then it doesn't work again. This is understandable, since if c == 1, the expression



f[a x + b y] + f[z]


does not contain Times at level 1 anymore. I suggest using more granular rules:



a_ f[x_] ^:= f[a x]

f[x_] + f[y_] ^:= f[x + y]


This achieves two things: the upvalue is attached to f and not to system symbols, and it solves the problem you mentioned in the comments.







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday









Henrik Schumacher

37.5k249107




37.5k249107










answered yesterday









LLlAMnYP

9,7161756




9,7161756











  • Thank you for the explanation. But isn't the case c==1 a variant of the first case, now with the two multipliers omitted? And shouldn't then the two multipliers be replaced with their default values? Or did I get it completely wrong what you tried to point out? Edit Or can only be one default value be replaced at a time?
    – Display Name
    yesterday







  • 1




    The UpValue in your attempt is associated with Times. I replace scriptcapitalN with just N for brevity: N[x] + b N[y] is Plus[N[x], Times[b, N[y]]]. Times is present at level 1 of the expression, so MMA checks UpValues for the symbol Times. However, N[x] + N[y] is Plus[N[x], N[y]], so MMA has no waying of realizing that it has to apply the UpValue defined for Times. So it doesn't even get round to doing that and attempting to substitute default values in the process.
    – LLlAMnYP
    yesterday










  • Thank you, makes perfectly sense.
    – Display Name
    yesterday
















  • Thank you for the explanation. But isn't the case c==1 a variant of the first case, now with the two multipliers omitted? And shouldn't then the two multipliers be replaced with their default values? Or did I get it completely wrong what you tried to point out? Edit Or can only be one default value be replaced at a time?
    – Display Name
    yesterday







  • 1




    The UpValue in your attempt is associated with Times. I replace scriptcapitalN with just N for brevity: N[x] + b N[y] is Plus[N[x], Times[b, N[y]]]. Times is present at level 1 of the expression, so MMA checks UpValues for the symbol Times. However, N[x] + N[y] is Plus[N[x], N[y]], so MMA has no waying of realizing that it has to apply the UpValue defined for Times. So it doesn't even get round to doing that and attempting to substitute default values in the process.
    – LLlAMnYP
    yesterday










  • Thank you, makes perfectly sense.
    – Display Name
    yesterday















Thank you for the explanation. But isn't the case c==1 a variant of the first case, now with the two multipliers omitted? And shouldn't then the two multipliers be replaced with their default values? Or did I get it completely wrong what you tried to point out? Edit Or can only be one default value be replaced at a time?
– Display Name
yesterday





Thank you for the explanation. But isn't the case c==1 a variant of the first case, now with the two multipliers omitted? And shouldn't then the two multipliers be replaced with their default values? Or did I get it completely wrong what you tried to point out? Edit Or can only be one default value be replaced at a time?
– Display Name
yesterday





1




1




The UpValue in your attempt is associated with Times. I replace scriptcapitalN with just N for brevity: N[x] + b N[y] is Plus[N[x], Times[b, N[y]]]. Times is present at level 1 of the expression, so MMA checks UpValues for the symbol Times. However, N[x] + N[y] is Plus[N[x], N[y]], so MMA has no waying of realizing that it has to apply the UpValue defined for Times. So it doesn't even get round to doing that and attempting to substitute default values in the process.
– LLlAMnYP
yesterday




The UpValue in your attempt is associated with Times. I replace scriptcapitalN with just N for brevity: N[x] + b N[y] is Plus[N[x], Times[b, N[y]]]. Times is present at level 1 of the expression, so MMA checks UpValues for the symbol Times. However, N[x] + N[y] is Plus[N[x], N[y]], so MMA has no waying of realizing that it has to apply the UpValue defined for Times. So it doesn't even get round to doing that and attempting to substitute default values in the process.
– LLlAMnYP
yesterday












Thank you, makes perfectly sense.
– Display Name
yesterday




Thank you, makes perfectly sense.
– Display Name
yesterday

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181671%2fassociativity-of-upvalue%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

Installing NextGIS Connect into QGIS 3?

One-line joke