Simplify a series in Mathematica - wolfram-mathematica

I'm having some issues simplifying some functions in mathematica. In a program I wrote I have a few functions calculated with the Sum function, many of the terms end up being multiplied by zero. I want the function to be printed but simplified. I try using FullSimplify, but it takes forever to run. I then tried using Simplify, but it doesn't work. Here is an example of a function I'm getting,
2. Sqrt[-(-1+x) x]+
0. Sin[2 ArcCos[-1+2 x]]+
0. Sin[3 ArcCos[-1+2 x]]+
0. Sin[4 ArcCos[-1+2 x]]+
0. Sin[5 ArcCos[-1+2 x]]+
0. Sin[6 ArcCos[-1+2 x]]+
0. Sin[7 ArcCos[-1+2 x]]+
0. Sin[8 ArcCos[-1+2 x]]+
0. Sin[9 ArcCos[-1+2 x]]+
0. Sin[10 ArcCos[-1+2 x]]+
0. Sin[11 ArcCos[-1+2 x]]+
0. Sin[12 ArcCos[-1+2 x]]+
0. Sin[13 ArcCos[-1+2 x]]+
0. Sin[14 ArcCos[-1+2 x]]+
0. Sin[15 ArcCos[-1+2 x]]+
0. Sin[16 ArcCos[-1+2 x]]+
0. Sin[17 ArcCos[-1+2 x]]+
0. Sin[18 ArcCos[-1+2 x]]+
0. Sin[19 ArcCos[-1+2 x]]+
0. Sin[20 ArcCos[-1+2 x]]+
0. Sin[21 ArcCos[-1+2 x]]+
0. Sin[22 ArcCos[-1+2 x]]+
0. Sin[23 ArcCos[-1+2 x]]+
0. Sin[24 ArcCos[-1+2 x]]+
0. Sin[25 ArcCos[-1+2 x]]+
0. Sin[26 ArcCos[-1+2 x]]
So, why wouldn't this simplify to
2. Sqrt[-(-1+x) x]
I also have a follow up question that is not exactly as important. But, lets say four of those terms have non-zero coefficients, is it possible to combine them into one term that is a numerical approximation of the original?
Thanks to anyone that replies!

Say hello to machine precision.
(* exact *)
0 Sin[x]
Out[1]= 0
(* machine precision *)
0. Sin[x]
Out[2]= 0. Sin[x]
Use Chop to set numbers absurdly close to zero, to 0 exactly.
expr = 2. Sqrt[-(-1 + x) x] + 0. Sin[2 ArcCos[-1 + 2 x]] +
0. Sin[3 ArcCos[-1 + 2 x]] + 0. Sin[4 ArcCos[-1 + 2 x]] +
0. Sin[5 ArcCos[-1 + 2 x]] + 0. Sin[6 ArcCos[-1 + 2 x]] +
0. Sin[7 ArcCos[-1 + 2 x]] + 0. Sin[8 ArcCos[-1 + 2 x]] +
0. Sin[9 ArcCos[-1 + 2 x]] + 0. Sin[10 ArcCos[-1 + 2 x]] +
0. Sin[11 ArcCos[-1 + 2 x]] + 0. Sin[12 ArcCos[-1 + 2 x]] +
0. Sin[13 ArcCos[-1 + 2 x]] + 0. Sin[14 ArcCos[-1 + 2 x]] +
0. Sin[15 ArcCos[-1 + 2 x]] + 0. Sin[16 ArcCos[-1 + 2 x]] +
0. Sin[17 ArcCos[-1 + 2 x]];
Chop[expr]
Out[4]= 2. Sqrt[(1 - x) x]

Behind those "0." terms there are hidden some very tiny (but non-zero) numbers. You can slash them using Chop.

Related

Summation of series 1 + (1+2+1) + (1+2+1+3+1+2+1)

The function is : F(n-1) n F(n-1)
Its a type of palindrome function called Zimmer Series.
The values would be : 1, 121, 1213121, ...
I want to figure the summation of the individual digits.
1 + (1+2+1) + (1+2+1+3+1+2+1) + ...
Any help is welcome.
Breaking this down into steps, we first find out a formula for the summation of a single value of the series and then we can find out the summation of said formula.
Expanding the definition you gave and manipulating it:
F(n) = n + 2F(n-1)
F(n) = n + 2(n-1) + 22(n-2) + 23(n-3) + ... + 2n-1
2F(n) = 2n + 22(n-1) + 23(n-2) + ... + 2n-1(2) + 2n
F(n) - 2F(n) = -F(n) = n - 2 - 22 - 23 - ... - 2n
From this and using the formula for Geometric Progression we can then get an expression for a single term of the series.
F(n) = (2n + 2n-1 + ... + 2) - n
= (2n+1 - 2) - n
Now we just have to work out the summation of this expression.
G(n) = Σ F(n) = Σ (2n+1 - 2 - n)
G(n) = (2n+2 - 22) - (2n) - (n(n+1)/2)
Simplifying this should hopefully give you the answer you seek!
G(n) = (2n+2 - (n(n+5)/2) - 22)
Trying this out on a few of the terms just to double check.
G(1) = (21+2 - (1(1+5)/2) - 22)
G(1) = 1
G(2) = (22+2 - (2(2+5)/2) - 22)
G(2) = 5 = 1 + (1 + 2 + 1)
G(3) = (23+2 - (3(3+5)/2) - 22)
G(3) = 16 = 1 + (1 + 2 + 1) + (1 + 2 + 1 + 3 + 1 + 2 + 1)
EDIT: Mark Dickinson is right, I misinterpreted the question, this solution is incorrect.
I think after the second term the sequence is of the form difference in Arithmetic Progression.
Let me show you how
Second Term = 1+2+1
Third Term = 1+2+1+3 + 1+2+1
Difference = 1+2+1+3 = 7
Third Term = 1+2+1+3+1+2+1
Fourth Term = 1+2+1+3+ 1+4+1+3 +1+2+1
Difference = 1+4+1+3 = 9
Fourth Term = 1+2+1+3+1+4+1+3+1+2+1
Fifth Term = 1+2+1+3+1+4+ 1+5+1+4 +1+3+1+2+1
Difference = 1+5+1+4 = 11
So as you can see the difference is in the arithmetic progression and you find the sum of the terms using the formula for the sum of numbers whose different are in Arithmetic Progression

Conditional transforming an expression when encountering default value in Mathematica

Say I have an expression x + x^2 + x^3, and I want to substitute x with y when the power of x is less than its maximal power, below is my code in Mathematica:
x + x^2 + x^3 /. x^n_. /; n < 3 -> y^n
But the result is y + y^2 + y^3 instead of y + y^2 + x^3. I don't know where is my mistake.
You could use Replace
Replace[x + x^2 + x^3, x^n_. /; n < 3 -> y^n, {1}]
The levelspec {1} holds the replacement to level 1 where the pattern is Power[x, n] (unless n is omitted). If replacement is at level 2, the x symbols inside the Power expressions are replaced, with the n_. default coming into play. ReplaceAll (/.) affects all levels, but Replace with levelspec {1} does the job.
Without with the n_. default an additional rule is required.
Replace[x + x^2 + x^3, {x^n_ /; n < 3 -> y^n, x -> y}, {1}]
Inverting the main rule allows use of ReplaceAll
x + x^2 + x^3 /. {x^n_ /; n >= 3 -> x^n, x -> y}
An alternative method would be to use Piecewise
Piecewise[{{y + y^2 + x^3, n < 3}}, x + x^2 + x^3]

Confused on how to find c and k for big O notation if f(x) = x^2+2x+1

I am studying big O notation from this book.
The deffinition of big O notation is:
We say that f (x) is O(g(x)) if there are constants C and k such that |f (x)| ≤ C|g(x)| whenever x > k.
Now here is the first example:
EXAMPLE 1 Show that f (x) = x^2 + 2x + 1 is O(x^2).
Solution: We observe that we can readily estimate the size of f (x) when x > 1 because x 1. It follows that
0 ≤ x^2 + 2x + 1 ≤ x^2 + 2x^2 + x^2 = 4x^2
whenever x > 1. Consequently, we can take C = 4 and k = 1 as witnesses to show that f (x) is O(x^2). That is, f (x) = x^2 + 2x + 1
1. (Note that it is not necessary to use absolute values here because all functions in these equalities are positive when x is positive.)
I honestly don't know how they got c = 4, looks like they jump straight to the equation manipulation and my algebra skills are pretty weak. However, I found another way through [The accepted answer to this question])What is an easy way for finding C and N when proving the Big-Oh of an Algorithm?) that says to add all coefficients to find c if k = 1. So x^2+2x+1 = 1+2+1 = 4.
Now for k = 2, I'm completely lost:
Alternatively, we can estimate the size of f (x) when x > 2. When x > 2, we have 2x ≤ x^2 and 1 ≤ x^2. Consequently, if x > 2, we have
0 ≤ x^2 + 2x + 1 ≤ x^2 + x^2 + x^2 = 3x^2.
It follows that C = 3 and k = 2 are also witnesses to the relation f (x) is O(x^2).
Can anyone explain what is happening? What method are they using?
First alternative:
C=4?
The C=4 come from the inequalities
0 ≤ x^2 + 2x + 1 ≤ x^2 + 2x^2 + x^2 = 4x^2 = C*x^2, with C=4 (+)
The second inequality in (+) is true for all x greater than 1, since, term by term
2x < 2x^2, given x>1
1 < x^2, given x>1
k = 1?
From above, we've shown that (+) holds as long as x is larger than 1, i.e.
(+) is true given x > k, with k=1
Second alternative:
k=2?
By the statement, we want to study f(x) for x larger than 2, i.e.
Study f(x) for x > k, k=2
Given x > 2, it's apparent that
0 ≤ x^2 + 2x + 1 ≤ x^2 + x^2 + x^2 = 3x^2 = C*x^2, with C=3 (++)
since, for x>2, we have
2x = x^2 given x=2 ==> 2x < x^2 given x>2
for x=2, 1 < x^2 = 4, so 1 < x^2 for all x>2
Both examples show that f(x) is O(x^2). By using your constants C and k, recall that then Big-O notation for f(x) can be summarized as something along the lines
... we can say that f(x) is O(g(x)) if we can find a constant C such
that |f(x)| is less than C|g(x)| or all x larger than k, i.e., for all
x>k. (*)
This, by no means, implies that we need to find a unique set of (C, k) to prove that some f(x) is some O(g(x)), just some set (C, k) such that (*) above holds.
See e.g. the following link for some reference on how to specify the asymptotic behaviour of a function:
https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/big-o-notation

Efficiancy of Horner's Rule to compute summation

How is Horner's rule an efficient method to compute summation of polynomials ?
int HornerRule( int array [] , unsigned int n , int x )
{
int result = array[n] ;
for( int i = n - 1 ; i >= 0 ; --i )
{
result = result *x + array[i];
}
return result ;
}
Horner's Rule takes advantage of the recursive definition of exponentiation:
x^y = x^(y-1) * x
Each higher-order term is evaluated using the results of the preceding term. Suppose you have a 3rd-degree polynomial; instead of evaluating x^3 by multiplying x times itself twice (2 operations), you multiply x times x^2 (which you already computed for the x^2 term), which is only 1 operation. Writing out the polynomial in a factored form,
(with coefficients of 1 for simplicity):
x^3 + x^2 + x + 1 = x(x^2 + x) + 1
= x(x(x + 1)) + 1
(You might notice that in this case, you have the same number of multiplications, 3 on each side. As the degree increases, though, the left-hand side adds k-1 multiplications when you add a term of degree k, but the right-hand side would add only a single multiplication:
x^4 + x^3 + x^2 + x + 1 = x(x^3 + x^2 + x) + 1
= x(x(x(x + 1)) + 1) + 1
)

Analysis of a specific algorithm running time with recursion

How would I go about calculating the runtime of this algorithm, so I can solve similar questions in the future?
For input size n satisfies the recurrence relation (for n>= 1)
T(n) = (2/n) * (T(0) + T(1) + ... + T(n-1)) + 5n
A way to get rid of sums is to compute differences, after multiplying by $n$ (allow me to write LaTeX, even if this site doesn't use it; I hope the formulas are understandable):
$$
(n + 1) a_{n + 1} - n a_n
= 2 a_n + 5
$$
$$
(n + 1) a_{n + 1} - (n + 2) a_n = 5
$$
This is a linear recurrence of the first order. A recurrence of the form:
$$
x_n - u_{n - 1} x_{n - 1} = f_n
$$
can be reduced to a sum by dividing by the summing factor $S_n = \prod_{0 \le k \le n} u_n$ to get:
$$
\frac{x_n}{S_n} - \frac{x_{n - 1}}{S_{n - 1}} = \frac{f_n}{S_n}
$$
Summing over $1 \le n \le N$ gives a solution to the equation.
In your particular case $S_n = \prod_{0 \le k \le n} \frac{n + 2}{n + 1} = n + 1$, so that:
$$
\frac{a_{k + 1}}{k + 2} - \frac{a_k}{k + 1} = \frac{5}{(k + 1) (k + 2)}
$$
\begin{align}
\frac{a_n}{n + 1} - \frac{a_0}{1}
&= 5 \sum_{0 \le k < n} \frac{1}{(k + 1) (k + 2} \
&= - 5 \sum_{0 \le k < n} \left( \frac{1}{k + 2} - \frac{1}{k + 1} \right) \
&= - 5 \left( \frac{1}{n + 1} - 1 \right) \
&= 5 \frac{n}{n + 1}
\end{align}
Finally:
$$
a_n = a_0 (n + 1) + 5 n
$$

Resources