In Mathematica, how can I cut off the high-order terms in a polynomial? - wolfram-mathematica

For example, I have a polynomial y=a_0+a_1 x + ..... + a_50 x^50. Since I know that the high-order terms are imposing negligible effects on the evaluation of y, I want to cut off them and have something like y=a_0+a_1 x + ..... + a_10 x^10, the first eleven terms. How can I realize this?
I thank you all in advance.

In[1]:= y = a0 + a1*x + a2*x^2 + a3*x^3 + a4*x^4;
y /. x^b_ /; b >= 3 -> 0
Out[2]= a0 + a1 x + a2 x^2

The mathematically proper approach..
Series[ a0 + a1*x + a2*x^2 + a3*x^3 + a4*x^4, {x, 0, 2}] // Normal
-> a0 + a1 x + a2 x^2

If your polynomial is actually as simple as shown, with a term for every power of x and none others, you can simply use Take or Part to extract only those terms that you want because of the automatic ordering (in Plus) that Mathematica uses. For example:
exp1 = Expand[(1 + x)^9]
Take[exp1, 5]
1 + 9 x + 36 x^2 + 84 x^3 + 126 x^4 + 126 x^5 + 84 x^6 + 36 x^7 + 9 x^8 + x^9
1 + 9 x + 36 x^2 + 84 x^3 + 126 x^4
If it is not you will need something else. Bill's replacement rule is one concise and efficient method. For more complex manipulations you may wish to decompose the polynomial using CoefficientArrays, CoefficientRules, or CoefficientList.

There is a shortcut to the previous answers which is even more symbolic. You write, say,
y[x_] = a0 + a1 x + a2 x^2 + a3 x^3 + a4 x^4 + a5 x^5;
y[x] + O[x]^3
which gives you,
a0 + a1 x + a2 x^2 + O[x]^3

Related

Simple way to calculate number of chess material combinations

In chess, one player can have different material combinations, for example:
"1 queen, 2 rooks, 2 knights, 2 bishops, 8 pawns + the king" is one combination
if the player loses one bishop:
"1 queen, 2 rooks, 2 knights, 1 bishop, 8 pawns + the king" is another combination
..afterwards, if a pawn is promoted to a knight, then:
"1 queen, 2 rooks, 3 knights, 1 bishop, 7 pawns + the king" is another combination
OK, the following combination is not valid:
"5 queens, 5 rooks, 5 knights, 5 bishops, 2 pawns + the king"
since you lack of pawns to promote. (5 queens = 4 pawns needed) (5 rooks = 3 pawns needed) , etc. so 4 + 3 + 3 + 3 = 13 pawns needed. Since 2 pawns on the board, then at most 6 pawns could be promoted. Not valid.
How many valid material combinations are there?
I computed 8694 combinations using the following C code. The question is:
Do you find simpler/efficient algorithm to calculate it? (less cycles, less calculations, clearer code, etc.) ... or even a math formulae??
total = 0;
for (queens=0;queens<=9;queens++)
for (rooks=0;rooks<=10;rooks++)
for (bishops=0;bishops<=10;bishops++)
for (knights=0;knights<=10;knights++)
for (pawns=0;pawns<=8;pawns++)
{
pawnsRequested = 0;
if (queens>1) pawnsRequested += queens - 1;
if (rooks>2) pawnsRequested += rooks - 2;
if (bishops>2) pawnsRequested += bishops - 2;
if (knights>2) pawnsRequested += knights - 2;
if (8-pawns < pawnsRequested) continue;
total++;
}
printf("%i\n",total);
If the piece types were independent, then we could just multiply: 10 possibilities for the queens times 11 possibilities for the rooks times etc. We need to track pawn usage, however. There's a mathematical trick called generating functions where we can encode the possibilities for, e.g., rooks as
3 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8,
where the power of x denotes the number of pawns used, and the coefficient denotes the number of possibilities. Here, there are three possibilities that require no promoted pawns (0, 1, 2), one that requires one promoted pawn (3), one that requires two promoted pawns (4), etc. Now we can multiply each of the factors together (respectively, queens, rooks, bishops, knights, pawns).
(2 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8)
* (3 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8)
* (3 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8)
* (3 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8)
* (1 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8)
Here it is from Wolfram Alpha.
The coefficients of 1 through x^8, which are the number of possibilities for 0 to 8 pawns required, are 54, 135, 261, 443, 693, 1024, 1450, 1986, 2648, summing to 8694.

MinimalPolynomial[] for expressions containing symbols

I’m using Mathematica to solve some simple equations relating to geometry, and subsequently hard-coding those solutions in a different language†. Rather than have many pages of code, it would be more concise to code the solutions as a root of a polynomial.
Let’s take:
Solve[{
dist^2 == xstep^2 + ((h - 2 r)/(NR - 1))^2,
dist^2 == (w - 2 r - NC xstep)^2 + (h/2 - r - dist/2)^2
},{xstep, dist}]
That generates a “very large output”, heavy with fractions and square roots and fourth roots. Obviously the two solved variables are the roots of quartic equations.
Please, is there a version of MinimalPolynomial[] that will work on expressions containing symbols? All that’s wanted is the five coefficients of dist’s quartic.
Thank you.
† The “different language” is PostScript, and I really don’t have the expertise to write a //PostScriptForm function. Indeed, finding the optimal balance between recomputing repeated expressions and using “… dup … roll” would, in the general case, be slow.
I wrote a package called "Substitutions" (archived here), which pulls out a hierarchy of sub-expressions, minimizing the coding for complex expressions. It was included in the old MathSource library. Here's the description:
It is often useful, especially when using Mathematica for software
development, to apply substitutions to complex expressions to reduce
their form. For large expressions, this task can become tedious.
Substitutions[] was designed help with the process of finding a useful
set of substitutions to simplify an expression.
It is quite old now, but should still work.
Reduce may be what you want:
I've consolodated some of your symbol groups into A,B,C (not nesesary, makes it fit on screen)
Reduce[{dist^2 == xstep^2 + (A)^2 &&
dist^2 == (C - NC xstep)^2 + (B - dist/2)^2 , {xstep, dist}]]
this produces a fairly large output with a bunch of conditions.
If you have known constraints that preclude various degenrate cases it helps to specify (I made these up)
$Assumptions = B != 0 && B^2 != 3 C^2 && NC^2 != 3/4;
note $Assumptions is used by Simplify, but you need to explicitly add it to the Reduce expression..
Simplify[Reduce[{dist^2 == xstep^2 + (A)^2 &&
dist^2 == (C - NC xstep)^2 + (B - dist/2)^2 && $Assumptions }, {xstep, dist}]]
output.. not too unwiledy .. The Root expression contains the coefficients you seek..
(xstep ==
Root[9 A^4 - 40 A^2 B^2 + 16 B^4 - 24 A^2 C^2 + 32 B^2 C^2 +
16 C^4 + (48 A^2 C NC - 64 B^2 C NC -
64 C^3 NC) #1 + (18 A^2 - 40 B^2 - 24 C^2 - 24 A^2 NC^2 +
32 B^2 NC^2 + 96 C^2 NC^2) #1^2 + (48 C NC -
64 C NC^3) #1^3 + (9 - 24 NC^2 + 16 NC^4) #1^4 &, 1] ||
xstep ==
Root[9 A^4 - 40 A^2 B^2 + 16 B^4 - 24 A^2 C^2 + 32 B^2 C^2 +
16 C^4 + (48 A^2 C NC - 64 B^2 C NC -
64 C^3 NC) #1 + (18 A^2 - 40 B^2 - 24 C^2 - 24 A^2 NC^2 +
32 B^2 NC^2 + 96 C^2 NC^2) #1^2 + (48 C NC -
64 C NC^3) #1^3 + (9 - 24 NC^2 + 16 NC^4) #1^4 &, 2] ||
xstep ==
Root[9 A^4 - 40 A^2 B^2 + 16 B^4 - 24 A^2 C^2 + 32 B^2 C^2 +
16 C^4 + (48 A^2 C NC - 64 B^2 C NC -
64 C^3 NC) #1 + (18 A^2 - 40 B^2 - 24 C^2 - 24 A^2 NC^2 +
32 B^2 NC^2 + 96 C^2 NC^2) #1^2 + (48 C NC -
64 C NC^3) #1^3 + (9 - 24 NC^2 + 16 NC^4) #1^4 &, 3] ||
xstep ==
Root[9 A^4 - 40 A^2 B^2 + 16 B^4 - 24 A^2 C^2 + 32 B^2 C^2 +
16 C^4 + (48 A^2 C NC - 64 B^2 C NC -
64 C^3 NC) #1 + (18 A^2 - 40 B^2 - 24 C^2 - 24 A^2 NC^2 +
32 B^2 NC^2 + 96 C^2 NC^2) #1^2 + (48 C NC -
64 C NC^3) #1^3 + (9 - 24 NC^2 + 16 NC^4) #1^4 &, 4]) &&
3 A^2 + 4 B dist + xstep (8 C NC + 3 xstep) ==
4 (B^2 + C^2 + NC^2 xstep^2)

Method for interpolating value at the midpoint of a square, given the values, first and second derivatives at the corners?

All the practical examples of spatial interpolation I've been able to find work by sampling additional surrounding points to estimate the derivatives. But is there a simpler method if the derivatives are already known—and if you only need the value (and derivatives) for the single point at the center of the known points?
To illustrate: suppose for each of the points (1, 1), (-1, 1), (-1, -1), and (1, -1) you know f(x, y), f'(x), f''(x), f'(y), and f''(y) — and you want interpolated values at (0, 0) for f(x, y), f'(x), f''(x), f'(y), and f''(y).
First of all the problem as posed does not make sense. In multi-variable calculus we don't have derivatives, we have partial derivatives. Lots of them.
Suppose you have the value, first partial derivatives and second partial derivatives at the corners. So at each corner we know the value, the partial by x, the partial by y, the second partial by x by x, the second partial by x by y, and the second partial by y by y. We have 6 pieces of data per corner, for 24 pieces of data total.
Next what we do is try to fit this to an appropriate polynomial. 24 terms, that would be, a0 + a1 x + a2 y + a3 x^2 + a4 x y + a5 y^2 + a6 x^3 + a7 x^2 y + a8 x y^2 + a9 y^3 + a10 x^4 + a11 x^3 y + a12 x^2 y^2 + a13 x y^3 + a14 y^4 + a15 x^5 + a16 x^4 y + a17 x^3 y^2 + a18 x^2 y^3 + a18 x y^4 + a19 y^5 + a20 x^6 + a21 x^4 y^2 + a22 x^2 y^4 + a23 y^6. (I had to leave out some 6th power terms because I was hitting that 24 limit.)
If you calculate that out, matching up all of those values against all of those points you get 24 equations in 24 variables. Solve and you get all of the coefficients to use. Plug in the value (0, 0) and you have your interpolation.
Straightforward, tedious, and not for the faint of heart.

Matrix factorise of vector

Say I have a vector that looks like this:
1/2 a + 1/3 b
b + c
2a + c
1/3c + 4d
Mathematically this can be factorised into matrix and a vector:
Matrix:
1/2 1/3 0 0
0 1 1 0
2 0 1 0
0 0 1/3 4
Vector:
a
b
c
d
(My apologies for the formatting, perhaps someone could suggest how better to do it?)
Is there any way to get mathematica to do this matrix factorisation? In my specific case the terms are not simple expressions such as "a", "b", "c", "d". But are things indexed by a list, e.g.
W[{3,0,0,0}]
W[{1,1,0,0}]
W[{0,0,1,0}]
Thanks!
Possibly:
x = {1/2 a + 1/3 b,
b + c,
2 a + c,
1/3 c + 4 d};
CoefficientArrays[x, {a, b, c, d}][[2]] // MatrixForm
In the case that you want coefficients for all variables, use the compact form:
CoefficientArrays[x][[2]] // MatrixForm
In the case that you do not want coefficients of all variables, part [[1]] comes into play:
x2 = {1/2 a + 1/3 b + q - y,
b + c + 1/2 r,
2 a + c + 2 y,
1/3 c + 4 d};
CoefficientArrays[x2, {a, b, c, d}][[1]] // Normal
{q - y, r/2, 2 y, 0}
Such that you can reconstruct your expression.

Simplify Absolute Value in Mathematica

I currently have a large expression with many terms of the form
Abs[-2 b + 2 d1 m + l Tan[\[Theta]]]
I know, from the geometry of my problem, that
-2 b + 2 d1 m + l Tan[\[Theta]] > 0
However, when I try to simplify my expression,
Simplify[Abs[-2 b + 2 d1 m + l Tan[\[Theta]]], -2 b + 2 d1 m + l Tan[\[Theta]] > 0]
I just get back
Abs[-2 b + 2 d1 m + l Tan[\[Theta]]]
How can I make Mathematica simplify out the unnecessary absolute value?
EDIT 1
The full expression which I'm trying to simplify is
-(1/(2 (m - Tan[\[Theta]])))
Sqrt[1 + m^2] (B2 Sqrt[(-2 b + 2 d1 m + l Tan[\[Theta]])^2] +
B4 Sqrt[(-2 b + 2 d2 m + l Tan[\[Theta]])^2] +
B5 Sqrt[(2 b + 2 d3 m + l Tan[\[Theta]])^2] +
B7 Sqrt[(2 b + 2 d4 m + l Tan[\[Theta]])^2] +
B1 Sqrt[(2 b - 2 (d1 + l) m + l Tan[\[Theta]])^2] +
B3 Sqrt[(2 b - 2 (d2 + l) m + l Tan[\[Theta]])^2] +
B6 Sqrt[(-2 (b + (d3 + l) m) + l Tan[\[Theta]])^2] +
B8 Sqrt[(-2 (b + (d4 + l) m) + l Tan[\[Theta]])^2])
The terms being squared under each of the radicals is known to be a positive real number.
Since the terms are all known to be real and positive, squaring and taking the square-root will only give you the same number. Hence, you could do something like
expr /. Sqrt[(x___)^2] :> x
where expr is your giant expression above.
Here are two ideas:
1)
Simplify[Abs[-2 b + 2 d1 m + l Tan[\[Theta]]],
0 < \[Theta] < \[Pi]/2 && l > 0 && 2 d1 m > 0 && -2 b > 0]
2)
f[e_] := 100 Count[e, _Abs, {0, Infinity}] + LeafCount[e]
Simplify[Abs[-2 b + 2 d1 m + l Tan[\[Theta]]], -2 b + 2 d1 m +
l Tan[\[Theta]] > 0, ComplexityFunction -> f]
Th complexity function f makes Abs more expensive than Times. See docu for Simplify. Does that help?
If you only wish to remove specific instances of absolute value, you could do something along these lines:
Clear[removeAbs]
removeAbs[expr_, r_] := expr /. {Sqrt[r^2] :> r, Abs[r] :> r}
That way it only removes the absolute value from any expressions you specify:
In: removeAbs[Abs[x] + Abs[y], x]
Out: x + Abs[y]
I'll see if I can find a nicer looking solution than this.
I'm constantly stimied by things like Abs[a]^2, and stuff like using Assuming with a\[Element]Reals doesn't help.
I found some help here WolframMathWorld - Absolute Square with ComplexExpand[Abs[a]^2, TargetFunctions -> {Conjugate}], but sometimes it still returns stuff like Conjugate[Sqrt[a^2 + b^2]] and I've found wrapping a second ComplexExpand (without parameters) around that helps.

Resources