Marshall Hampton wrote:
> It occurred to me that maybe I should supply a more non-trivial
> example of rules/patterns/subs in mathematica. Here is just one: we
> replace exponents of polynomials with the famous 3x+1 sequence
> (Collatz, whatever) until they stabilize:
>
> In: {x^2, x^3 + x^200, x^4 + z^909} //. {y_^n_ /; Mod[n, 2] == 1 ->
> y^(3 n + 1), y_^n_ /; Mod[n, 2] == 0 -> y^(n/2)}
>
> Out: {x, 2 x, x + z}
>
Some interpretation:
/; == "such that" (gives a condition on the replacement)
a_ is a placeholder
y_^n_ matches a number to an exponent and lets y be the name of the base
while n is the exponent.
y_^n_ /; Mod[n,2] == 1 -> y^(3 n + 1) means:
if there is a base to an exponent such that the exponent is odd, then
call the base y and the exponent n and replace the y^n with y^(3n+1).
One other thing: //. means "keep running the replacement rule until
there is no change in the expression"
Marshall or anyone, please correct me if I've said something incorrect.
I too miss the power of the Mathematica replacement engine.
Thanks,
Jason
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---