Implementing rules on functions in Mathematica - wolfram-mathematica

Suppose you have the following expression:
expr = f[p^(3) * q^(5) * m] * f[p^(-2) * q^(-5) * m] * f[p^(1/2) * q^(1) * m] + 5 * f[p^(1) * q^(2) * n] * f[q^(-2) * n] + s * f[p^(h) * q^(r) * j] * f[p^(1-h) * q^(-r) * j].
Moreover, suppose that the function f is such that
f[p^(a) * q^(b) * x] * f[p^(1-a) * q^(-b) * x] == 1
for any value (numerical or symbolic) of the exponents a,b and for any x.
This means that
expr == f[p^(1/2) * q^(1) * m] + 5 + s.
How can I teach Mathematica to recognise this property of f and then simplify expr according to it?
I tried to implement it as the following rule
/.f[p^(a_)*q^(b_)*x_]f[p^(1-a_)*q^(-b_)*x_]->1
but it doesn't work. It only works if you specify the numerical value of the exponents a and b, but not if you want them to be generic.
What is the right way to write such a rule?

The answer here may provide a solution : How to insert a subexpression into a larger expression in Mathematica? c/o Rojo
For example, using your expression expr and a larger expression A
expr = f[p^(a)*q^(b)*x] f[p^(1 - a)*q^(-b)*x];
A = 3 expr + z (f[p^(a)*q^(b)*x]) + y expr
doThat[expr_, vars_List] := Expand[Simplify[expr /. Flatten[
Solve[# == ToString##, First#Variables##] & /# vars]],
Alternatives ## ToString /# vars] /.
Thread[ToString /# vars -> vars];
done = doThat[A, {expr}];
ans = Simplify[done //. expr -> 1]
3 + y + z f[p^a q^b x]
The expected answer.
For general cases as per the comment, a pattern can be used, e.g.
expr = f[p^(a)*q^(b)*x] f[p^(1 - a)*q^(-b)*x];
A = 3 expr + z (f[p^(a)*q^(b)*x]) +
y f[p^(h)*q^(l)*x] f[p^(1 - h)*q^(-l)*x];
done = doThat[A, {expr}];
ans = Simplify[done //.
f[p^(a_)*q^(b_)*x] f[p^(1 - a_)*q^(-b_)*x] -> 1]
3 + y + z f[p^a q^b x]
But in the end it can simply be done by
A /. f[p^(a_)*q^(b_)*x] f[p^(1 - a_)*q^(-b_)*x] -> 1
3 + y + z f[p^a q^b x]
2nd edit
A = (3 f[p^(a)*q^(b)*x] f[p^(1 - a)*q^(-b)*x] +
z (f[p^(a)*q^(b)*x]) +
y f[p^(h)*q^(l)*x] f[p^(1 - h)*q^(-l)*x] f[p m])
A /. h -> 2 /. f[p^(a_)*q^(b_)*x_] f[p^(1 - a_)*q^(-b_)*x_] -> 1
3 + z f[p^a q^b x] + y f[m p] f[(q^-l x)/p] f[p^2 q^l x]
When h is 2 the second replacement no longer applies to the expression containing p^(1 - h) because the form has become p^(-1).
On the other hand, keeping the variables symbolic by using Z instead of 2
A /. h -> Z /. f[p^(a_)*q^(b_)*x_] f[p^(1 - a_)*q^(-b_)*x_] -> 1
3 + y f[m p] + z f[p^a q^b x]

Related

Using alternative special functions in Simplify and Expand

I have Mathematica expressions involving the special functions Erf[x] and Erfc[x], but I'd like to express them in terms of the scaled and translated version
F[x_] := CDF[NormalDistribution[0,1]][x]
throughout the notebook. This is because F[x] has an easy interpretation in the context of my problem.
Not sure whether I understand your problem, but I'm trying to answer my interpretation of what your saying.
So you have an expression in erf and erfc, like this
expr = Erf[x] + 1/Erfc[x] + Sin[Erf[x]] + Cos[Erfc[x]]
All it takes to replace erf and erfc with F is this:
expr //. {Erfc[x_] -> 2 F[-(x) Sqrt[2]], Erf[x_] -> 1 - Erfc[x]}
(* ==> 1 + Cos[2 F[-Sqrt[2] x]] + 1/(2 F[-Sqrt[2] x]) -
2 F[-Sqrt[2] x] + Sin[1 - 2 F[-Sqrt[2] x]]
*)
which with your definition of F[x] is indeed the same:
1 + Cos[2 F[-Sqrt[2] x]] + 1/(2 F[-Sqrt[2] x]) - 2 F[-Sqrt[2] x] +
Sin[1 - 2 F[-Sqrt[2] x]] /. F[x_] -> CDF[NormalDistribution[0, 1]][x]
(* ==> 1 + Cos[Erfc[x]] + 1/Erfc[x] - Erfc[x] + Sin[1 - Erfc[x]] *)

Proper way to simplify integral result in Mathematica given integer constraints

Evaluating the following integral should be non-zero, and mathematica correctly gives a non-zero result
Integrate[ Cos[ (Pi * x)/2 ]^2 * Cos[ (3*Pi*x)/2 ]^2, {x, -1, 1}]
However, attempting a more general integral:
FullSimplify[
Integrate[Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2],
{x, -1, 1}],
Element[{m, n}, Integers]]
yields zero, which is definitely not true for m = n = 1
I'd expect a conditional expression. Is it possible to "tell" mathematica about my constraints on m and n before the integral is evaluated so that it handles the special cases properly?
While I'm late to the party, no one has given a complete solution, thus far.
Sometimes, it pays to understand the integrand better before you integrate. Consider,
ef = TrigReduce[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2]]/.
Cos[a_] :> Cos[ Simplify[a, Element[{m,n}, Integers] ] ]
which returns
(2 Cos[(m - n) Pi x] + Cos[(1 + m - n) Pi x] + Cos[(1 - m + n) Pi x] +
Cos[(m + n) Pi x] + 2 Cos[(1 + m + n) Pi x] + Cos[(2 + m + n) Pi x] )/8
where each term has the form Cos[q Pi x] with integral q. Now, there are two cases to consider when integrating Cos[q Pi x] over -1 to 1 (where q is integral): q == 0 and q != 0.
Case q = 0: This is a special case that Mathematica misses in the general result, as it implies a constant integrand. (I'll often miss it, also, when doing this by hand, so Mathematica isn't entirely to blame.) So, the integral is 2, in this case.
Strictly speaking, this isn't true. When told to integrate Cos[ q Pi x ] over -1 < x < 1, Mathematica returns
2 Sin[ Pi q ]/( Pi q )
which is 0 except when q == 0. At that point, the function is undefined in the strict sense, but Limit[Sin[x]/x, q -> 0] == 1. As the singularity at q == 0 is removable, the integral is 2 when q -> 0. So, Mathematica does not miss it, it is just in a form not immediately recognized.
Case q != 0: Since Cos[Pi x] is periodic with period 2, an integral of Cos[q Pi x] from x == -1 to x == 1 will always be over q periods. In other words,
Integrate[ Cos[q Pi x], {x, -1, 1},
Assumptions -> (Element[ q, Integers ] && q != 0) ] == 0
Taken together, this means
Integrate[ Cos[q Pi x], {x, -1, 1}, Assumptions -> Element[ q, Integers ] ] ==
Piecewise[{{ q == 0, 2 }, { 0, q!=0 }}]
Using this, we can integrate the expanded form of the integrand via
intef = ef /. Cos[q_ Pi x] :> Piecewise[{{2, q == 0}, {0, q != 0}}] //
PiecewiseExpand
which admits non-integral solutions. To clean that up, we need to reduce the conditions to only those that have integral solutions, and we might as well simplify as we go:
(Piecewise[{#1,
LogicalExpand[Reduce[#2 , {m, n}, Integers]] //
Simplify[#] &} & ### #1, #2] & ## intef) /. C[1] -> m
\begin{Edit}
To limit confusion, internally Piecewise has the structure
{ { { value, condition } .. }, default }
In using Apply (##), the condition list is the first parameter and the default is the second. To process this, I need to simplify the condition for each value, so then I use the second short form of Apply (###) on the condition list so that for each value-condition pair I get
{ value, simplified condition }
The simplification process uses Reduce to restrict the conditions to integers, LogicalExpand to help eliminate redundancy, and Simplify to limit the number of terms. Reduce internally uses the arbitrary constant, C[1], which it sets as C[1] == m, so we set C[1] back to m to complete the simplification
\end{Edit}
which gives
Piecewise[{
{3/4, (1 + n == 0 || n == 0) && (1 + m == 0 || m == 0)},
{1/2, Element[m, Integers] &&
(n == m || (1 + m + n == 0 && (m <= -2 || m >= 1)))},
{1/4, (n == 1 + m || (1 + n == m && (m <= -1 || m >= 1)) ||
(m + n == 0 && (m >= 1 || m <= 0)) ||
(2 + m + n == 0 && (m <= -1 || m >= 0))) &&
Element[m, Integers]},
{0, True}
}
as the complete solution.
Another Edit: I should point out that both the 1/2 and 1/4 cases include the values for m and n in the 3/4 case. It appears that the 3/4 case may be the intersection of the other two, and, hence, their sum. (I have not done the calc out, but I strongly suspect it is true.) Piecewise evaluates the conditions in order (I think), so there is no chance of getting this incorrect.
Edit, again: The simplification of the Piecewise object is not as efficient as it could be. At issue is the placement of the replacement rule C[1] -> m. It happens to late in the process for Simplify to make use of it. But, if it is brought inside the LogicalExpand and assumptions are added to Simplify
(Piecewise[{#1,
LogicalExpand[Reduce[#2 , {m, n}, Integers] /. C[1] -> m] //
Simplify[#, {m, n} \[Element] Integers] &} & ### #1, #2] & ## intef)
then a much cleaner result is produce
Piecewise[{
{3/4, -2 < m < 1 && -2 < n < 1},
{1/2, (1 + m + n == 0 && (m >= 1 || m <= -2)) || m == n},
{1/4, 2 + m + n == 0 || (m == 1 + n && m != 0) || m + n == 0 || 1 + m == n},
{0, True}
}]
Not always zero ...
k = Integrate[
Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/ 2],
{x, -1, 1}, Assumptions -> Element[{m, n}, Integers]];
(*Let's find the zeroes of the denominator *)
d = Denominator[k];
s = Solve[d == 0, {m, n}]
(*The above integral is indeterminate at those zeroes, so let's compute
the integral again there (a Limit[] could also do the work) *)
denZ = Integrate[
Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/ 2] /.s,
{x, -1, 1}, Assumptions -> Element[{m, n}, Integers]];
(* All possible results are generated with m=1 *)
denZ /. m -> 1
(*
{1/4, 1/2, 1/4, 1/4, 1/2, 1/4}
*)
Visualizing those cases:
Plot[Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2]
/. s /. m -> 1, {x, -1, 1}]
Compare with a zero result integral one:
Plot[Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/ 2]
/. {m -> 1, n -> 4}, {x, -1, 1}]
If you just drop the whole FullSimplify part, mathematica does the integration neatly for you.
Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
2], {x, -1, 1}]
To include the condition that m and n are integers, it's better to use the Assumptions option in Integrate.
Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
2], {x, -1, 1}, Assumptions -> Element[{m, n}, Integers]]
Lets use some conclusive conditions about the two integers m=n||m!=n.
Assuming[{(n \[Element] Integers && m \[Element] Integers && m == n)},
Integrate[Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2],
{x, -1, 1}]]
The answer for this case is 1/2. For the other case it is
Assuming[{(n \[Element] Integers && m \[Element] Integers && m != n)},
Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
2], {x, -1, 1}]]
and the answer is 0.
However I am amazed to see that if we add this two conditions as an "either or stuff", Mathematica returns one zero after integration. I mean in case of the following I am getting only zero but not ``1/2||0`.
Assuming[{(n \[Element] Integers && m \[Element] Integers &&
m == n) || (n \[Element] Integers && m \[Element] Integers &&
m != n)},
Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
2], {x, -1, 1}]]
By the way we can see the conditions exclusively where this integral becomes Indeterminate.
res = Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
2], {x, -1, 1}] // Simplify
The output is here.
Now lets see all the relations m and n can have to make the Integral bad!
BadPart = (res*4 Pi);
Flatten#(Solve[(Denominator[#] == 0), m] & /#
Table[BadPart[[i]], {i, 1, Length#BadPart}] /.
Rule -> Equal) // TableForm
So these are the special cases which as Sjoerd mentioned are having infinite instances.
BR

Finding the Fixed Points of an Iterative Map

I need to find fixed points of iterative map x[n] == 1/2 x[n-1]^2 - Mu.
My approach:
Subscript[g, n_ ][Mu_, x_] := Nest[0.5 * x^2 - Mu, x, n]
fixedPoints[n_] := Solve[Subscript[g, n][Mu, x] == x, x]
Plot[
Evaluate[{x,
Table[Subscript[g, 1][Mu, x], {Mu, 0.5, 4, 0.5}]}
], {x, 0, 0.5}, Frame -> True]
I'll change notation slightly (mostly so I myself can understand it). You might want something like this.
y[n_, mu_, x_] := Nest[#^2/2 - mu &, x, n]
fixedPoints[n_] := Solve[y[n, mu, x] == x, x]
The salient feature is that the "function" being nested now really is a function, in correct format.
Example:
fixedPoints[2]
Out[18]= {{x -> -1 - Sqrt[-3 + 2*mu]},
{x -> -1 + Sqrt[-3 + 2*mu]},
{x -> 1 - Sqrt[ 1 + 2*mu]},
{x -> 1 + Sqrt[ 1 + 2*mu]}}
Daniel Lichtblau
First of all, there is an error in your approach. Nest takes a pure function. Also I would use exact input, i.e. 1/2 instead of 0.5 since Solve is a symbolic rather than numeric solver.
Subscript[g, n_Integer][Mu_, x_] := Nest[Function[z, 1/2 z^2 - Mu], x, n]
Then
In[17]:= fixedPoints[1]
Out[17]= {{x -> 1 - Sqrt[1 + 2 Mu]}, {x -> 1 + Sqrt[1 + 2 Mu]}}
A side note:
Look what happens when you start very near to a fixed point (weird :) :
f[z_, Mu_, n_] := Abs[N#Nest[1/2 #^2 - Mu &, z, n] - z]
g[mu_] := f[1 + Sqrt[1 + 2*mu] - mu 10^-8, mu, 10^4]
Plot[g[mu], {mu, 0, 3}, PlotRange -> {0, 7}]
Edit
In fact, it seems you have an autosimilar structure there:

Collect output of Roots[] into a list

If I do Roots[a x^2 + b x + c == 0, x], the output is
x == (-b - Sqrt[b^2 - 4 a c])/(2 a) ||
x == (-b + Sqrt[b^2 - 4 a c])/(2 a)
How do I collect the output of Roots into a list like so {(-b - Sqrt[b^2 - 4 a c])/(2 a), (-b + Sqrt[b^2 - 4 a c])/(2 a)} so that I can plot it?
An alternative (obvious?} method:
List ## Roots[a x^2 + b x + c == 0, x][[All, 2]]
giving
x /. {ToRules[Roots[a x^2 + b x + c == 0, x]]} // Flatten
==> {(-b - Sqrt[b^2 - 4 a c])/(2 a), (-b + Sqrt[b^2 - 4 a c])/(2 a)}

How to ask mathematica to compute higher order derivatives evaluated at 0

I have a function, let's say for example,
D[x^2*Exp[x^2], {x, 6}] /. x -> 0
And I want to replace 6 by a general integer n,
Or cases like the following:
Limit[Limit[D[D[x /((-1 + x) (1 - y) (-1 + x + x y)), {x, 3}], {y, 5}], {x -> 0}], {y -> 0}]
And I want to replace 3 and 5 by a general integer m and n respectively.
How to solve these two kinds of problems in general in mma?
Many thanks.
Can use SeriesCoefficient, sometimes.
InputForm[n! * SeriesCoefficient[x^2*Exp[x^2], {x,0,n}]]
Out[21]//InputForm=
n!*Piecewise[{{Gamma[n/2]^(-1), Mod[n, 2] == 0 && n >= 2}}, 0]
InputForm[mncoeff = m!*n! *
SeriesCoefficient[x/((-1+x)*(1-y)*(-1+x+x*y)), {x,0,m}, {y,0,n}]]
Out[22]//InputForm=
m!*n!*Piecewise[{{-1 + Binomial[m, 1 + n]*Hypergeometric2F1[1, -1 - n, m - n,
-1], m >= 1 && n > -1}}, 0]
Good luck extracting limits for m, n integer, in this second case.
Daniel Lichtblau
Wolfram Research
No sure if this is what you want, but you may try:
D[x^2*Exp[x^2], {x, n}] /. n -> 4 /. x -> 0
Another way:
f[x0_, n_] := n! SeriesCoefficient[x^2*Exp[x^2], {x, x0, n}]
f[0,4]
24
And of course, in the same line, for your other question:
f[m_, n_] :=
Limit[Limit[
D[D[x/((-1 + x) (1 - y) (-1 + x + x y)), {x, m}], {y, n}], {x ->
0}], {y -> 0}]
These answers don't give you an explicit form for the derivatives, though.

Resources