Use pattern to collect terms in Mathematica - wolfram-mathematica
With Mathematica I would like collect terms from (1 + a + x + y)^4 according to the exponents of x and y, so
(1 + a + x + y)^4 = (...)x^0 y^0 + (...)x^1 y^0 + (...)x^0 y^1 + ...
The Mathematica help has a nice example which I tried to imitate:
D[f[Sqrt[ x^2 + 1 ]], {x, 3}]
Collect[%, Derivative[ _ ][ f ][ _ ], Together]
This collects derivative terms of the same order (and the same argument for f)
Can anyone explain why the following imitation does not work?
Collect[(1 + a + x + y)^4, x^_ y^_]
gives
(1 + a + x + y)^4
Any suggestions for a solution?
As per Sasha, you have to Expand the polynomial to use Collect. However, even then it isn't that simple of a problem. Using Collect you can group by two variables, but it depends on how you order them:
In[1]:= Collect[ (1 + a + x + y)^4 // Expand, {x, y}]
Out[1]:= 1 + 4 a + 6 a^2 + 4 a^3 + a^4 + x^4 +
(4 + 12 a + 12 a^2 + 4 a^3) y + (6 + 12 a + 6 a^2) y^2 +
(4 + 4 a) y^3 + y^4 + x^3 (4 + 4 a + 4 y) +
x^2 (6 + 12 a + 6 a^2 + (12 + 12 a) y + 6 y^2) +
x (4 + 12 a + 12 a^2 + 4 a^3 + (12 + 24 a + 12 a^2) y +
(12 + 12 a) y^2 + 4 y^3)
which pulls out any common factor of x resulting in coefficients that are polynomials in y. If you used {y,x} instead, Collect would pull out the common factors of y and you'd have polynomials in x.
Alternatively, you could supply a pattern, x^_ y^_ instead of {x,y}, but at least in v.7, this does not collect anything. The issue is that the pattern x^_ y^_ requires an exponent to be present, but in terms like x y^2 and x^2 y the exponent is implicit in at least one of the variables. Instead, we need to specify that a default value is acceptable, i.e. use x^_. y^_. which gives
Out[2]:= 1 + 4 a + 6 a^2 + 4 a^3 + a^4 + 4 x + 12 a x + 12 a^2 x + 4 a^3 x +
6 x^2 + 12 a x^2 + 6 a^2 x^2 + 4 x^3 + 4 a x^3 + x^4 + 4 y +
12 a y + 12 a^2 y + 4 a^3 y + (12 + 24 a + 12 a^2) x y +
(12 + 12 a) x^2 y + 4 x^3 y + 6 y^2 + 12 a y^2 + 6 a^2 y^2 +
(12 + 12 a) x y^2 + 6 x^2 y^2 + 4 y^3 + 4 a y^3 + 4 x y^3 + y^4
But, this only collects terms where both variables are present. Truthfully, I can't seem to come up with a pattern that would make Collect function like you want, but I have found an alternative.
I'd use CoefficientRules instead, although it does require a little post-processing to put the result back into polynomial form. Using your polynomial, you get
In[3]:= CoefficientRules[(1 + a + x + y)^4, {x, y}]
Out[3]:= {{4, 0} -> 1, {3, 1} -> 4, {3, 0} -> 4 + 4 a, {2, 2} -> 6,
{2, 1} -> 12 + 12 a, {2, 0} -> 6 + 12 a + 6 a^2, {1, 3} -> 4,
{1, 2} -> 12 + 12 a, {1, 1} -> 12 + 24 a + 12 a^2,
{1, 0} -> 4 + 12 a + 12 a^2 + 4 a^3, {0, 4} -> 1, {0, 3} -> 4 + 4 a,
{0, 2} -> 6 + 12 a + 6 a^2, {0, 1} -> 4 + 12 a + 12 a^2 + 4 a^3,
{0, 0} -> 1 + 4 a + 6 a^2 + 4 a^3 + a^4}
Now, if you're only interested in the coefficients themselves, then you're done. But, to transform this back into a polynomial, I'd use
In[4]:= Plus ## (Out[3] /. Rule[{a_, b_}, c_] :> x^a y^b c)
Out[4]:= 1 + 4 a + 6 a^2 + 4 a^3 + a^4 +
(4 + 12 a + 12 a^2 + 4 a^3) x +
(6 + 12 a + 6 a^2) x^2 + (4 + 4 a) x^3 + x^4 +
(4 + 12 a + 12 a^2 + 4 a^3) y + (12 + 24 a + 12 a^2) x y +
(12 + 12 a) x^2 y + 4 x^3 y + (6 + 12 a + 6 a^2) y^2 +
(12 + 12 a) x y^2 + 6 x^2 y^2 + (4 + 4 a) y^3 +
4 x y^3 + y^4
Edit: After thinking about it, there is one more simplification that can be done. Since the coefficients are polynomials in a, they may be factorable. So, instead of using what CoefficientRules gives directly, we use Factor to simplify:
In[5]:= Plus ## (Out[3] /. Rule[{a_, b_}, c_] :> x^a y^b Factor[c])
Out[5]:= (1 + a)^4 + 4 (1 + a)^3 x + 6 (1 + a)^2 x^2 + 4 (1 + a) x^3 + x^4 +
4 (1 + a)^3 y + 12 (1 + a)^2 x y + 12 (1 + a) x^2 y + 4 x^3 y +
6 (1 + a)^2 y^2 + 12 (1 + a) x y^2 + 6 x^2 y^2 + 4 (1 + a) y^3 +
4 x y^3 + y^4
As can be seen, the coefficients are considerably simplified by using Factor, and this result could have been anticipated by thinking of (1 + a + x + y)^4 as a simple trinomial with variables (1 + a), x, and y. With that in mind and replacing 1+a with z, CoefficientRules then gives:
In[6]:= CoefficientRules[(z + x + y)^4, {x, y, z}]
Out[6]:= {{4, 0, 0} -> 1, {3, 1, 0} -> 4, {3, 0, 1} -> 4,
{2, 2, 0} -> 6, {2, 1, 1} -> 12, {2, 0, 2} -> 6,
{1, 3, 0} -> 4, {1, 2, 1} -> 12, {1, 1, 2} -> 12,
{1, 0, 3} -> 4, {0, 4, 0} -> 1, {0, 3, 1} -> 4,
{0, 2, 2} -> 6, {0, 1, 3} -> 4, {0, 0, 4} -> 1}
Or, in polynomial form
Out[7]:= x^4 + 4 x^3 y + 6 x^2 y^2 + 4 x y^3 + y^4 + 4 x^3 z +
12 x^2 y z + 12 x y^2 z + 4 y^3 z + 6 x^2 z^2 + 12 x y z^2 +
6 y^2 z^2 + 4 x z^3 + 4 y z^3 + z^4
which when you replace z with (1 + a) gives the identical result shown in Out[5].
Collect is a structural operation, so you need to expand first.
Collect[(1 + a + x + y)^4 // Expand, x^_ y^_]
This works:
In[1]:= Collect[(1 + a + x + y)^4 // Expand, {x^_ y^_, x^_ y, x y^_, x y, x, y}]
Out[1]= 1 + 4 a + 6 a^2 +
4 a^3 + a^4 + (4 + 12 a + 12 a^2 + 4 a^3) x + (6 + 12 a + 6 a^2) x^2 + (4 +
4 a) x^3 + x^4 + (4 + 12 a + 12 a^2 + 4 a^3) y + (12 + 24 a +
12 a^2) x y + (12 + 12 a) x^2 y +
4 x^3 y + (6 + 12 a + 6 a^2) y^2 + (12 + 12 a) x y^2 +
6 x^2 y^2 + (4 + 4 a) y^3 + 4 x y^3 + y^4
Alternatively you can use Default as suggested by rcollyer:
In[2]:= Collect[(1 + a + x + y)^4 // Expand, {x^_. y^_., x, y}]
Out[2]= 1 + 4 a + 6 a^2 +
4 a^3 + a^4 + (4 + 12 a + 12 a^2 + 4 a^3) x + (6 + 12 a + 6 a^2) x^2 + (4 +
4 a) x^3 + x^4 + (4 + 12 a + 12 a^2 + 4 a^3) y + (12 + 24 a +
12 a^2) x y + (12 + 12 a) x^2 y +
4 x^3 y + (6 + 12 a + 6 a^2) y^2 + (12 + 12 a) x y^2 +
6 x^2 y^2 + (4 + 4 a) y^3 + 4 x y^3 + y^4
Plus ## MonomialList[(1 + a + x + y)^4, {x, y}]
This may be what you were looking for
In[1]:= TraditionalForm[Collect[(1 + a + x + y)^4 // Expand, {x, y}],
ParameterVariables :> {a}]
Out[1]:= x^4+x^3 (4 y+4 a+4)+x^2 (6 y^2+(12 a+12) y+6 a^2+12 a+6)+
x (4 y^3+(12 a+12) y^2+ (12 a^2+24 a+12) y+4 a^3+12 a^2+12 a+4)+
y^4+(4 a+4) y^3+(6 a^2+12 a+6) y^2+(4 a^3+12 a^2+12 a+4) y+
a^4+4 a^3+6 a^2+4 a+1
Related
Why can mathematica not solve my equation?
I can not get my head around why mathematica can not solve this equation: In[22]:= Solve[1/x^12 - 2/x^6 + 1/2 (-2 + x)^2 HeavisideTheta[-2 + x] == 0] During evaluation of In[22]:= Solve::nsmet: This system cannot be solved with the methods available to Solve. >> Out[22]= Solve[1/x^12 - 2/x^6 + 1/2 (-2 + x)^2 HeavisideTheta[-2 + x] == 0] using mathematica 9.0.1.0. Edit: In[24]:= Plot[1/x^12 - 2/x^6 + 1/2 (-2 + x)^2 HeavisideTheta[-2 + x], {x, 1, 3}]
FindRoot is often more aggressive FindRoot[1/x^12-2/x^6+1/2(-2+x)^2 HeavisideTheta[-2+x]==0, {x, 3}] and that almost instantly returns the solution.
If you want to look for analytic solutions ( which is what Solve does ), assume the step function has a value 0 or 1 , use Solve and check the step function assumption against the results: Select[ Solve[1/x^12 - 2/x^6 + 1/2 (-2 + x)^2 (0) == 0] , HeavisideTheta[-2 + x /. #] == 0 & ] {{x -> -(1/2^(1/6))}, {x -> 1/2^(1/6)}} Select[ Solve[1/x^12 - 2/x^6 + 1/2 (-2 + x)^2 (1) == 0] , HeavisideTheta[-2 + x /. #] == 1 & ] {{x -> Root[2 - 4 #1^6 + 4 #1^12 - 4 #1^13 + #1^14 &, 2]}} Of the three solutions, the one I guess you want is the last one , the root of a 14th order polynomial, which you need to eval numerically anyway: N[Root[2 - 4 #1^6 + 4 #1^12 - 4 #1^13 + #1^14 &, 2] ] 2.18999
All possible combinations for N numbers
Can you help me with some algorithm? I should find all possible combinations for N numbers: 1/N, 2/N, 3/N, ... , N-2/n, N-1/N, N/N For example take 4 numbers: A, B, C, D 1/4: A + B + C + D 2/4: A*B + A*C + A*D + B*C + B*D + C*D 3/4: A*B*C + A*B*D + A*C*D + B*C*D 4/4: A*B*C*D How can I solve this?
As you can notice, these are the coefficients of the polynomial (X + A)(X + B)(X + C)(X + D) when expanded. It suffices to implement multiplication of a polynomial by a monomial and use it iteratively. def PolyByMono(Poly, Mono): Poly.append(0) for i in range(len(Poly) - 2, -1, -1): Poly[i + 1]+= Mono * Poly[i] Poly[0]+= Mono return Poly def Expand(Numbers): Poly= [Numbers[0]] for M in Numbers[1:]: Poly= PolyByMono(Poly, M) print Poly Expand([1, 2, 3, 4]) gives: [10, 35, 50, 24]
The complex conjugate transpose in Mathematica
Is there an existing function for the complex conjugate transpose in Mathematica? The equivalent in matlab is the to the apostrophe operator (').
In your title you ask for conjugate transpose. That's just ConjugateTranspose. If you want the conjugate, it's just Conjugate. Input: a = {{3 + 2 I, 1 - I}, {2 - 5 I, 4 + 3 I}} ConjugateTranspose[a] Output: {{3 + 2 I, 1 - I}, {2 - 5 I, 4 + 3 I}} {{3 - 2 I, 2 + 5 I}, {1 + I, 4 - 3 I}} You can also use the Hermitian conjugate symbol, which you use by entering esc hc esc or \[HermitianConjugate].
Mathematica integral with many singularities
What's the best way of getting Mathematica 7 or 8 to do the integral NIntegrate[Exp[-x]/Sin[Pi x], {x, 0, 50}] There are poles at every integer - and we want the Cauchy principle value. The idea is to get a good approximation for the integral from 0 to infinity. With Integrate there is the option PrincipleValue -> True. With NIntegrate I can give it the option Exclusions -> (Sin[Pi x] == 0), or manually give it the poles by NIntegrate[Exp[-x]/Sin[Pi x], Evaluate[{x, 0, Sequence##Range[50], 50}]] The original command and the above two NIntegrate tricks give the result 60980 +/- 10. But they all spit out errors. What is the best way of getting a quick reliable result for this integral without Mathematica wanting to give errors?
Simon, is there reason to believe your integral is convergent ? In[52]:= f[k_Integer, eps_Real] := NIntegrate[Exp[-x]/Sin[Pi x], {x, k + eps, k + 1 - eps}] In[53]:= Sum[f[k, 1.0*10^-4], {k, 0, 50}] Out[53]= 2.72613 In[54]:= Sum[f[k, 1.0*10^-5], {k, 0, 50}] Out[54]= 3.45906 In[55]:= Sum[f[k, 1.0*10^-6], {k, 0, 50}] Out[55]= 4.19199 It looks like the problem is at x==0. Splitting integrand k+eps to k+1-eps for integer values of k: In[65]:= int = Sum[(-1)^k Exp[-k ], {k, 0, Infinity}] Integrate[ Exp[-x]/Sin[Pi x], {x, eps, 1 - eps}, Assumptions -> 0 < eps < 1/2] Out[65]= (1/((1 + E) (I + \[Pi])))E (2 E^(-1 + eps - I eps \[Pi]) Hypergeometric2F1[1, (I + \[Pi])/(2 \[Pi]), 3/2 + I/(2 \[Pi]), E^(-2 I eps \[Pi])] + 2 E^(I eps (I + \[Pi])) Hypergeometric2F1[1, (I + \[Pi])/(2 \[Pi]), 3/2 + I/(2 \[Pi]), E^(2 I eps \[Pi])]) In[73]:= N[int /. eps -> 10^-6, 20] Out[73]= 4.1919897038160855098 + 0.*10^-20 I In[74]:= N[int /. eps -> 10^-4, 20] Out[74]= 2.7261330651934049862 + 0.*10^-20 I In[75]:= N[int /. eps -> 10^-5, 20] Out[75]= 3.4590554287709991277 + 0.*10^-20 I As you see there is a logarithmic singularity. In[79]:= ser = Assuming[0 < eps < 1/32, FullSimplify[Series[int, {eps, 0, 1}]]] Out[79]= SeriesData[eps, 0, {(I*(-1 + E)*Pi - 2*(1 + E)*HarmonicNumber[-(-I + Pi)/(2*Pi)] + Log[1/(4*eps^2*Pi^2)] - 2*E*Log[2*eps*Pi])/(2*(1 + E)*Pi), (-1 + E)/((1 + E)*Pi)}, 0, 2, 1] In[80]:= Normal[ ser] /. {{eps -> 1.*^-6}, {eps -> 0.00001}, {eps -> 0.0001}} Out[80]= {4.191989703816426 - 7.603403526913691*^-17*I, 3.459055428805136 - 7.603403526913691*^-17*I, 2.726133068607085 - 7.603403526913691*^-17*I} EDIT Out[79] of the code above gives the series expansion for eps->0, and if these two logarithmic terms get combined, we get In[7]:= ser = SeriesData[eps, 0, {(I*(-1 + E)*Pi - 2*(1 + E)*HarmonicNumber[-(-I + Pi)/(2*Pi)] + Log[1/(4*eps^2*Pi^2)] - 2*E*Log[2*eps*Pi])/(2*(1 + E)* Pi), (-1 + E)/((1 + E)*Pi)}, 0, 2, 1]; In[8]:= Collect[Normal[PowerExpand //# (ser + O[eps])], Log[eps], FullSimplify] Out[8]= -(Log[eps]/\[Pi]) + ( I (-1 + E) \[Pi] - 2 (1 + E) (HarmonicNumber[-((-I + \[Pi])/(2 \[Pi]))] + Log[2 \[Pi]]))/(2 (1 + E) \[Pi]) Clearly the -Log[eps]/Pi came from the pole at x==0. So if one subtracts this, just like principle value method does this for other poles you end up with a finitely value: In[9]:= % /. Log[eps] -> 0 Out[9]= (I (-1 + E) \[Pi] - 2 (1 + E) (HarmonicNumber[-((-I + \[Pi])/(2 \[Pi]))] + Log[2 \[Pi]]))/(2 (1 + E) \[Pi]) In[10]:= N[%, 20] Out[10]= -0.20562403655659928968 + 0.*10^-21 I Of course, this result is difficult to verify numerically, but you might know more that I do about your problem. EDIT 2 This edit is to justify In[65] input that computes the original regularized integral. We are computing Sum[ Integrate[ Exp[-x]/Sin[Pi*x], {x, k+eps, k+1-eps}], {k, 0, Infinity}] == Sum[ Integrate[ Exp[-x-k]/Sin[Pi*(k+x)], {x, eps, 1-eps}], {k, 0, Infinity}] == Sum[ (-1)^k*Exp[-k]*Integrate[ Exp[-x]/Sin[Pi*x], {x, eps, 1-eps}], {k, 0, Infinity}] == Sum[ (-1)^k*Exp[-k], {k, 0, Infinity}] * Integrate[ Exp[-x]/Sin[Pi*x], {x, eps, 1-eps}] In the third line Sin[Pi*(k+x)] == (-1)^k*Sin[Pi*x] for integer k was used.
Simon, I haven't spent much time with your integral, but you should try looking at stationary phase approximation. What you have is a smooth function (exp), and a highly oscillatory function (sine). The work involved is now in brow-beating the 1/sin(x) into the form exp(if(x)) Alternatively, you could use the series expansion of the cosecant (not valid at poles): In[1]:=Series[Csc[x], {x, 0, 5}] (formatted) Out[1]=1/x + x/6 + 7/360 x^3 + 31/15120 x^5 +O[x]^6 Note that for all m>-1, you have the following: In[2]:=Integrate[x^m Exp[-x], {x, 0, Infinity}, Assumptions -> m > -1] Out[2]=Gamma[1+m] However, summing the series with the coefficients of cosecant (from wikipedia), not including 1/x Exp[-x] case, which doesn't converge on [0,Infinity]. c[m_] := (-1)^(m + 1) 2 (2^(2 m - 1) - 1) BernoulliB[2 m]/Factorial[2 m]; Sum[c[m] Gamma[1 + 2 m - 1], {m, 1, Infinity}] does not converge either... So, I'm not sure that you can work out an approximation for the integral to infinity, but I if you're satisfied with a solution upto some large N, I hope these help.
I have to agree with Sasha, the integral does not appear to be convergent. However, if you exclude x == 0 and break the integral into pieces Integrate[Exp[-x]/Sin[Pi x], {x, n + 1/2, n + 3/2}, PrincipalValue -> True] where n >= 0 && Element[n, Integers], then it seems you may get an alternating series I Sum[ (-1/E)^n, {n, 1, Infinity}] == - I / (1 + E ) Now, I only took it out to n == 4, but it looks reasonable. However, for the integral above with Assumptions -> Element[n, Integers] && n >= 0 Mathematica gives If[ 2 n >= 1, - I / E, Integrate[ ... ] ] which just doesn't conform to the individual cases. As an additional note, if the pole lies at the boundary of the integration region, i.e. your limits are {x, n, n + 1}, you only get DirectedInfinitys. A quick look at the plot implies that you with the limits {x, n, n + 1} you only have a strictly positive or negative integrand, so the infinite value may be due to the lack of compensation which {x, n + 1/2, n + 3/2} gives you. Checking with {x, n, n + 2}, however it only spits out the unevaluated integral.
How to select the powers of x termwise based on the results of its mod 3 in mathematica
I have a polynomial in x, for example, x^4/s + x^3 + x^2*s + x^3*s^2 + x What I want to do is: based on the result of (the exponent of x) mod 3, if it's 0, change the x^* to 1; if it's 1, change the x^* to 2; if it is 2, change x^* to 3. So I want to get: x^4 => 2 x^3 => 1 x^2 => 3 x^1 => 2 therefore, for the given example, I get 2/s+1+3s+s^2+2 How to do this programmably? Thanks!
The following: (x^4/s + x^3 + x^2*s + x^3*s^2 + x) /. x^(a : _ : 1) :> (Mod[a, 3] + 1) seems to do it. Edit: Answering the comment: In[4]:= (x^4/s + x^3 + x^2*s + x^3*s^2 + x) /. x^(a : _ : 1) :> (Mod[a, 3] /. {0 :> m, 1 :> n, 2 :> p}) Out[4]= m + n + n/s + p s + m s^2