Re: Question about subscripts and polynomial

*To*: mathgroup at smc.vnet.net*Subject*: [mg107455] Re: [mg107355] Question about subscripts and polynomial*From*: DrMajorBob <btreat1 at austin.rr.com>*Date*: Sat, 13 Feb 2010 05:22:14 -0500 (EST)*References*: <201002100836.DAA21317@smc.vnet.net>*Reply-to*: drmajorbob at yahoo.com

In the statement listSum = SortBy[basicSum /. Plus -> List, order] the first rule changes the sum to a List, each item of which is passed to the "order" function with the name "term". So, "term" is one of the things added together in basicSum. The new "order" function, Clear[order] order[term_] := Replace[term, x_?(FreeQ[#, Subscript] &) :> 1, {1}] /. Times -> Plus /. Power -> Times /. Subscript[_, k_] :> k operates on one term, such as 2 Subscript[d, 1] Subscript[d, 2] Subscript[v, 1] The level one parts of that are 2, Subscript[d, 1], Subscript[d, 2], and Subscript[v, 1]. The Replace statement in "order" looks for level one parts that do NOT have a subscript anywhere, in this case the coefficient 2, and replaces them with 1. The result is now Subscript[d, 1] Subscript[d, 2] Subscript[v, 1] (no coefficient). Next the Times->Plus replacement changes Subscript[d, 1] Subscript[d, 2] Subscript[v, 1] to Subscript[d, 1] + Subscript[d, 2] + Subscript[v, 1]. Power->Times does nothing in this case, but it would change Power[Subscript[d, 2], 2] to 2 Subscript[d, 2], in a different term. Next, subscripted variables become subscripts only, so Subscript[d, 1] + Subscript[d, 2] + Subscript[v, 1] changes to 1 + 2 + 1, or 4. SortBy computes order applied to all the terms, like this: order /@ (basicSum /. Plus -> List) {3, 4, 5, 5, 6, 7, 4, 5, 6, 6, 7, 8, 5, 6, 7, 7, 8, 9} and then sorts (basicSum /. Plus -> List) according to that, so that the new list is in the correct order: listSum = SortBy[basicSum /. Plus -> List, order]; order /@ listSum {3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9} Bobby On Fri, 12 Feb 2010 04:19:53 -0600, Luca Zanotti Fragonara <Luca.Zanottifragonara at polito.it> wrote: > Thank you drmajorbob, you are getting me on my way. > > I have one question, the order[term_] script, I don't understand very > well what did you do in it. Because I want to understand very well what > you have done so that I'd be independent the next time. > If I've understood correctely you have written a sort of rules, that > after you can use to sort the List of the terms. > But how did you build this rule? > > With the command Subscript[_, k_] :> k you define k as the subscript of > each term, right? > but what is term? If you can suggest me some section of the help to read > I'd be very thankful, but I don't get it the way that you have chosen to > solve the problem. > > And, the problem, as the moment, is that sometimes, the terms of the > polynomial are not in the order that I want, so that's why I want to > understand what you've done so that I could fix it! > > Thank you in advance, > > Luca > > > > DrMajorBob ha scritto: >> I'm not entirely sure what ordering you want, but here's something to >> get you on the way. >> >> You can tweak the "order" function if needed. >> >> First, your original expansion: >> >> poly = d^2*v; >> Subscript[q, 1] = >> poly /. {d -> Sum[Subscript[d, i], {i, 1, 3}]} /. {v -> >> Sum[Subscript[v, i], {i, 1, 3}]}; >> basicSum = Expand[Subscript[q, 1]] >> >> (ugly output omitted; it looks fine in Mathematica) >> >> Here's the same thing transformed to a List and sorted by the total >> order of each product: >> >> order[term_] := >> term /. Times -> Plus /. Power -> Times /. Subscript[_, k_] :> k >> listSum = SortBy[basicSum /. Plus -> List, order] >> >> (ugly output omitted; it looks fine in Mathematica) >> >> That's a List, not a sum; the following is also NOT a sum, but it looks >> like one, sorted the same as listSum: >> >> displaySum = Infix[listSum, "+"] >> >> (ugly output omitted; it looks fine in Mathematica) >> >> To get back the original: >> >> backToBasics = Plus @@ displaySum[[1]]; >> basicSum === backToBasics >> >> True >> >> Bobby >> >> On Wed, 10 Feb 2010 02:36:40 -0600, Luca Zanotti Fragonara >> <Luca.Zanottifragonara at polito.it> wrote: >> >>> Hello everybody, >>> >>> I would like to write a Polynomial, in this way: >>> >>> Poly = d^2*v >>> Subscript[q, 1] = >>> Poly /. {d -> Sum[Subscript[d, i], {i, 1, 3}]} /. {v >>> ->Sum[Subscript[v, >>> i], {i, 1, 3}]} >>> Expand[Subscript[q, 1]] >>> >>> In this way I will obtain a polynomial in this form: >>> >>> d_1^2 v_1+2 d_1 d_2 v_1+d_2^2 v_1+2 d_1 d_3 v_1+2 d_2 d_3 v_1+d_3^2 >>> v_1+d_1^2 v_2+2 d_1 d_2 v_2+d_2^2 v_2+2 d_1 d_3 v_2+2 d_2 d_3 v_2+d_3^2 >>> v_2+d_1^2 v_3+2 d_1 d_2 v_3+d_2^2 v_3+2 d_1 d_3 v_3+2 d_2 d_3 >>> v_3+d_3^2 v_3 >>> >>> I would like to reorder the expanded polynomial in a way such that the >>> terms with lower subcripts indexes will be at the beginning of the >>> polynomial, and the terms with higher order of subscripts will be at >>> the >>> end (a sort of order due to the subscript instead of the power terms). >>> So the order should be something: >>> >>> Order 3: d_1^2 v_1 >>> Order 4: 2 d_1 d_2 v_1+d_1^2 v_2+... >>> Order 5: d_2^2 v_1+2 d_1 d_3 v_1+... >>> Order 6: 2 d_2 d_3 v_1+... >>> >>> I've tried to figure it out but I don't know which way to turn!!! >>> >>> Thank you in advance. >>> >>> Luca >>> >>> >>> >> >> -- DrMajorBob at yahoo.com

**References**:**Question about subscripts and polynomial***From:*Luca Zanotti Fragonara <Luca.Zanottifragonara@polito.it>