Message Boards Message Boards

0
|
1718 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How to use OneIdentity with f and NestList[f,x,10]?

POSTED BY: Peter Burbery
2 Replies
Posted 2 years ago

Here (max) only has the Flat attribute, so the WL pattern matcher treats max[t1, t2, t3, t4] as max[ max[t1], max[t2, t3, t4]].

SetAttributes[max,Flat];
max[t1,t2,t3,t4]/.max[x_,y_]:>{x,y}
(* {max[t1],max[t2,t3,t4]} *)

The built-in symbol Max has attributes (Flat, OneIdentity), so the WL pattern matcher treats Max[t1, t2, t3, t4] as Max[t1, Max[t2, t3, t4]] here. The point is that OneIdentity ensures (t1) is not wrapped in Max after applying the rule.

Max[t1,t2,t3,t4]/.Max[x_,y_]:>{x,y}
(* {t1,Max[t2,t3,t4]} *)

The OneIdentity attribute also does something unrelated to the Flat attribute. Because the built-in symbol Plus has the OneIdentity attribute the WL pattern matcher treats (t) as Plus[t, defalut] below and the rule is applied.

t/.Plus[x_,y_:default]:>{x,y}
(* {t,default} *)

Next (plus) doesn't have the OneIdentity Attribute, so (t) below doesn't match the pattern.

ClearAll[plus];
t/.plus[x_,y_:default]:>{x,y}
(* t *)

Next we see Plus has a built-in default value of 0. That can be helpful since Plus[x, 0] evaluates to x.

Default[Plus]
(* 0 *)

In the next line the pattern (y_.) has a period at the end to indicate this argument is optional and the default for Plus should be used when the second argument is not provided. The symbol Optional is documented here. The previous output showed the built-in default for Plus. The WL pattern matcher would not treat (t) as in instance of Plus[t, 0] if Plus didn't have the OneIdentity Attribute.

t/.Plus[x_,y_.]:>{x,y}
(* {t,0} *)

In the next line the WL pattern matcher doesn't treat (t) as an instance of plus[t, 0] because (plus) doesn't have the OneIdentity Attribute.

Default[plus]=0;
t/.plus[x_,y_.]:>{x,y}
(* t *)

The built-in symbol Power also has a built-in default, but only for the second argument. That can be helpful since Power[x,1] evaluates to x.

Default[Power,2]
(* 1 *)

In the next line the WL pattern matcher treats (t) as Power[t,1].

t/.x_^(n_.):>x^(n+1)/(n+1)
(* t^2/2 *)

The built-in symbols in version 13.2 with the OneIdeitity attribute are: (And, BitAnd, BitOr, BitXor, Composition, Dot, GCD, Intersection, Join, LCM, Max, Min, NonCommutativeMultiply, Or, PermutationProduct, Plus, Power, RegionProduct, RightComposition, StringExpression, StringJoin, SymmetricDifference, TensorProduct, TensorWedge, Times, Union, Xor )

The built-in symbols in version 13.2 with built-in default values are (EntityFunction, LocalObject, LocalSymbol, N, Piecewise, Plus, Power, Times). We can find the default values for any of the above built-in symbols using code such as this.

DefaultValues[Power]
(* {HoldPattern[Default[Power,2]]:>1} *)
POSTED BY: Ted Ersek
Posted 2 years ago

Honestly, I don't have a great grasp of OneIdentity, but there is further info in the documentation. If you look at the documention for OneIdentity and then look in the Possible Issues section, you'll find some relevant info.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract