Associativity of upvalue
Clash 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]
associativity
 |Â
show 5 more comments
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]
associativity
You probably need to set default values for the patternsa_
andb_
likea_.
andb_.
, otherwise[ScriptCapitalN][a x + b y]
does not match the pattern ofmultiplier_ [ScriptCapitalN][a x + b y]
â LLlAMnYP
yesterday
My previous comment aboutTimes
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
 |Â
show 5 more comments
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]
associativity
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
associativity
edited yesterday
Henrik Schumacher
37.5k249107
37.5k249107
asked yesterday
Display Name
42338
42338
You probably need to set default values for the patternsa_
andb_
likea_.
andb_.
, otherwise[ScriptCapitalN][a x + b y]
does not match the pattern ofmultiplier_ [ScriptCapitalN][a x + b y]
â LLlAMnYP
yesterday
My previous comment aboutTimes
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
 |Â
show 5 more comments
You probably need to set default values for the patternsa_
andb_
likea_.
andb_.
, otherwise[ScriptCapitalN][a x + b y]
does not match the pattern ofmultiplier_ [ScriptCapitalN][a x + b y]
â LLlAMnYP
yesterday
My previous comment aboutTimes
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
 |Â
show 5 more comments
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.
Thank you for the explanation. But isn't the casec==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
TheUpValue
in your attempt is associated withTimes
. I replace scriptcapitalN with justN
for brevity:N[x] + b N[y]
isPlus[N[x], Times[b, N[y]]]
.Times
is present at level 1 of the expression, so MMA checksUpValues
for the symbolTimes
. However,N[x] + N[y]
isPlus[N[x], N[y]]
, so MMA has no waying of realizing that it has to apply theUpValue
defined forTimes
. 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
add a comment |Â
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.
Thank you for the explanation. But isn't the casec==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
TheUpValue
in your attempt is associated withTimes
. I replace scriptcapitalN with justN
for brevity:N[x] + b N[y]
isPlus[N[x], Times[b, N[y]]]
.Times
is present at level 1 of the expression, so MMA checksUpValues
for the symbolTimes
. However,N[x] + N[y]
isPlus[N[x], N[y]]
, so MMA has no waying of realizing that it has to apply theUpValue
defined forTimes
. 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
add a comment |Â
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.
Thank you for the explanation. But isn't the casec==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
TheUpValue
in your attempt is associated withTimes
. I replace scriptcapitalN with justN
for brevity:N[x] + b N[y]
isPlus[N[x], Times[b, N[y]]]
.Times
is present at level 1 of the expression, so MMA checksUpValues
for the symbolTimes
. However,N[x] + N[y]
isPlus[N[x], N[y]]
, so MMA has no waying of realizing that it has to apply theUpValue
defined forTimes
. 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
add a comment |Â
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.
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.
edited yesterday
Henrik Schumacher
37.5k249107
37.5k249107
answered yesterday
LLlAMnYP
9,7161756
9,7161756
Thank you for the explanation. But isn't the casec==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
TheUpValue
in your attempt is associated withTimes
. I replace scriptcapitalN with justN
for brevity:N[x] + b N[y]
isPlus[N[x], Times[b, N[y]]]
.Times
is present at level 1 of the expression, so MMA checksUpValues
for the symbolTimes
. However,N[x] + N[y]
isPlus[N[x], N[y]]
, so MMA has no waying of realizing that it has to apply theUpValue
defined forTimes
. 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
add a comment |Â
Thank you for the explanation. But isn't the casec==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
TheUpValue
in your attempt is associated withTimes
. I replace scriptcapitalN with justN
for brevity:N[x] + b N[y]
isPlus[N[x], Times[b, N[y]]]
.Times
is present at level 1 of the expression, so MMA checksUpValues
for the symbolTimes
. However,N[x] + N[y]
isPlus[N[x], N[y]]
, so MMA has no waying of realizing that it has to apply theUpValue
defined forTimes
. 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
add a comment |Â
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
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
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
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
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
You probably need to set default values for the patterns
a_
andb_
likea_.
andb_.
, otherwise[ScriptCapitalN][a x + b y]
does not match the pattern ofmultiplier_ [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