Is there a finite expression for $k$-th derivative of

begin{align}

f(x)={x^n}{e^{ – x}}

end{align}

# Tag: expression

## output formatting – Using Experimental`OptimizeExpression for expression optimization

I am trying to optimize the following expression (example only)

```
expr = Normal(Series((1 - y - 2 z)^-2, {z, 0, 3}))
```

whose output becomes

```
1/(1 - y)^2 - (4 z)/(-1 + y)^3 + (12 z^2)/(-1 + y)^4 - (32 z^3)/(-1 + y)^5
```

When using

```
Experimental`OptimizeExpression(expr)
```

I get

```
Experimental`OptimizedExpression(Block({Compile`$98},
Compile`$98 = -1 + y;
1/(1 - y)^2 - (4 z)/Compile`$98^3 + (12 z^2)/Compile`$98^4 - (32 z^3)/Compile`$98^5))
```

where `Compile``$98`

changes everytime I compile. Is there any way to replace these `Compile**`

?

The reason is I finally want to export this to C/Fortran where this symbol is problematic.

Another question: it says `Experimental`

, so it is not yet completely verified functionality?

## functions – MatrixPropertyDistribution error: expression “should be a scalar or vector expression” in documentation example

I’m new to StackExchange, but I am having a strange error using just downloaded Mathematica 12.1.1.0 (Student version). I’m trying to use the MatrixPropertyDistribution function, but Mathematica gives me an error even when I use the exact example that it gives in the documentation: https://reference.wolfram.com/language/ref/MatrixPropertyDistribution.html.

In particular, when I try to copy and paste the first example and try to run it,

`Mean(MatrixPropertyDistribution(Tr(x.x), x (Distributed) GaussianOrthogonalMatrixDistribution(2))) // N`

I get the error:

`MatrixPropertyDistribution::sclrvctr: Tr(x.x) should be a scalar or vector expression.`

Even if I do the expression:

`Mean(MatrixPropertyDistribution(1, x (Distributed) GaussianOrthogonalMatrixDistribution(2))) // N`

,

I get the absurd sounding error

`MatrixPropertyDistribution::sclrvctr: 1 should be a scalar or vector expression.`

What is going wrong here?

## I have two equation two unknowns, how to find there expression symbolically

I have a two-equation with two unknowns `r2`

and `t`

. I am trying to solve for them symbolically is it possible?

```
ClearAll("Global`*");
d1 = 2*r1;
d2 = 2*r2;
aixx = Simplify((π (d1^4 - d2^4))/64);
r1 = (r2 + t);
sigmax = Simplify((M*r1)/aixx);
f = sigmax
e(1) = D(f, {r2, 1})
e(2) = D(f, {t, 1})
sol = Solve({e(1) == 0, e(2) == 0}, {r2, t})
```

## differential equations – NDSolve with error that “Power::infy: Infinite expression 1/0.^0.4 encountered.”

Start with rational numbers and restrict `MaxStepSize`

```
b = 0.1 // Rationalize;
a = 0.6 // Rationalize;
p0 = 1.5 // Rationalize;
x0 = 1.3 // Rationalize;
eqns = {x'(t) == y(t),y'(t) ==p0*x(t)^2 -4 (1 + a b*x(t)^2 (x(t)^4 + 2 x(t)^-2 - 3)^(a - 1)) (x(t) -x(t)^(-5))};
ics = {x(0) == x0, y(0) == 0};
sol = NDSolveValue(Join(eqns, ics), {x , y}, {t, 0, 100 },MaxStepSize -> .1)
Plot(Through(sol(t)), {t, 0, 100})
```

## plotting – Problems with while loop Infinite expression 1/0 encountered

I’ve been trying to code a kinetic monte carlo simulation for a dimerization reaction, 2M —> D. However I have a kinetic Monte Carlo code for a first order decomposition reaction of AIBN (that’s the name of the chemical compound)

The code for AIBN decomposition works perfectly, but when I try to copy it to use it for the M compound, the code fails and Mathematica goes in a infinite loop saying: “Power: Infinte expression 1/0 encountered ” and then it says “Infinity:Indeterminite expression o Complexinfinity encountered”.

I don’t know why the AIBN code works and the dimerization code for M doesn’t if is exactly the same structure. Mathematica only graphs the analytical solution for M but doesn’t graph the outputstream resulting from the while Loop for kinetic Monte Carlo.

**Here is the code that works, for the AIBN decomposition**

```
AIBN0 = 0.01;(*mol/L*)
R = 1.98;TK = 60 + 273.15;
kdi = 2.89*10^(15)*Exp(-31130/(R*TK));(* s^-1*)
tf = 3600;(*%s*)
ta = {t, 0, tf};
AIBN = (AIBN0)*Exp(-kdi*t);
p1 = Plot(AIBN, ta , PlotStyle -> Red);
NAV = 2*10^6;
XAIBN = AIBN0*NAV;
kdiMC = kdi;
tb = 0.0;
AIBN = AIBN0;
Niter = 0;
file = OpenWrite("Result1.dat", FormatType -> OutputForm)
toc = Timing(
While(tb < tf,
Write(file, tb, " ", AIBN);
Niter = Niter + 1;
a0 = kdiMC*XAIBN;
XAIBN = XAIBN - 1;
AIBN = XAIBN/NAV;
tb = tb - Log(RandomReal())/a0));
Print(Niter, toc)
Close(file);
Result = Import("Result1.dat");
p2 = ListPlot(Result, DataRange -> {0, 600});
Show(p1, p2)
```

**Here the code that doesn’t work, for the M compound dimerization**

```
Mo = 5; (*mol/L*) (*Initial concentration of M*)
R = 1.98 (*J/(mol K)*); (*Ideal Gas constant*)
Tk = 383; (*K, Temperature*)
kdim = 2.52(10^4)Exp((-22347/(R*Tk))) (*L/(mol s)*) (*Kinetic constant*)
tf = 30000(60)(60); (*s, final reaction time, 30'000 hours*)
M = Mo/(1 + 2 kdim *t *Mo); (*Analytical solution*)
p6 = Plot(
M, {t, 0, tf},
PlotLabel -> "Concentration vs Time",
AxesLabel -> {"Time (s)", "Concentration (mol/L)"},
PlotStyle -> {Red}, PlotLegends -> {"(M)"})
NAV = 2*10^3; (*Avogadro number times Volume, (molecules L)/mol*)
XM = Mo*NAV; (* number of M molecules*)
kdimMC = 2*kdim/NAV; (*Monte Carlo kinetic constant*)
tb = 0.0; (*FIrst Iteration time*)
M = Mo; (*First Concentration Value of M*)
Niter = 0; (*First Iteration*)
file = OpenWrite("Result3.dat", FormatType -> OutputForm)
toc = Timing(
While(
tb <= tf,
Write(file, tb, " ", M);
Niter = Niter + 1;
a0 = kdimMC*XM*(XM - 1)/2;
XM = XM - 2;
M = XM/NAV;
tb = tb - Log(RandomReal())/a0
)
);
Print(Niter, toc)
Close(file);
Result = Import("Result3.dat");
p9 = ListPlot(Result, DataRange -> {0, 108000000});
Show(p6, p9)
```

## linear algebra – How to change the expression of the result of function `LinearSolve`

I want to solve the following linear equations:

$$left{begin{aligned}

x_{1}+3 x_{2}+x_{3} &=0 \

2 x_{1}+6 x_{2}+3 x_{3}-2 x_{4} &=0 \

-2 x_{1}-6 x_{2}-4 x_{4} &=0

end{aligned}right.$$

The form of the solution given in the textbook is as follows:

$$boldsymbol{X}=left(begin{array}{c}

x_{1} \

x_{2} \

x_{3} \

x_{4}

end{array}right)=left(begin{array}{rr}

-3 k_{1}-2 k_{2} \

k_{1} & 2 k_{2} \

k_{2}

end{array}right)=k_{1}left(begin{array}{r}

-3 \

1 \

0 \

0

end{array}right)+k_{2}left(begin{array}{r}

-2 \

0 \

2 \

1

end{array}right), quad k_{1}, k_{2} text { are arbitrary constants }$$

```
Solve({x1 + 3 x2 + x3 == 0,
2 x1 + 6 x2 + 3 x3 - 2 x4 == 0, -2 x1 - 6 x2 - 4 x4 == 0}, {x1, x2,
x3, x4})
LinearSolve({{1, 3, 1, 0}, {2, 6, 3, -2}, {-2, -6, 0, -4}}, {0, 0, 0})
NullSpace({{1, 3, 1, 0}, {2, 6, 3, -2}, {-2, -6, 0, -4}})
```

$$left{begin{aligned}

x_{1}+x_{2}-2 x_{3}-x_{4}=& 4 \

3 x_{1}-2 x_{2}-x_{3}+2 x_{4}=& 2 \

5 x_{2}+7 x_{3}+3 x_{4}=-2 \

2 x_{1}-3 x_{2}-5 x_{3}-x_{4}=& 4

end{aligned}right.$$

The form of the solution given in the textbook is as follows:

$$boldsymbol{X}=left(begin{array}{c}

x_{1} \

x_{2} \

x_{3} \

x_{4}

end{array}right)=left(begin{array}{r}

1 \

1 \

-1 \

0

end{array}right)+kleft(begin{array}{r}

-frac{2}{3} \

frac{1}{3} \

-frac{2}{3} \

1

end{array}right)=left(begin{array}{r}

1 \

1 \

-1 \

0

end{array}right)+k_{1}left(begin{array}{r}

-2 \

1 \

-2 \

3

end{array}right), quad k, k_{1} text { are arbitrary constants }$$

```
Solve({{1, 1, -2, -1}, {3, -2, -1, 2}, {0, 5, 7,
3}, {2, -3, -5, -1}}.{x1, x2, x3, x4} == {4, 2, -2, 4}, {x1, x2,
x3, x4})
LinearSolve({{1, 1, -2, -1}, {3, -2, -1, 2}, {0, 5, 7,
3}, {2, -3, -5, -1}}, {4, 2, -2, 4})
NullSpace({{1, 1, -2, -1}, {3, -2, -1, 2}, {0, 5, 7,
3}, {2, -3, -5, -1}})
```

But the solution form of the above code output is not the style of the textbook. How can we make the `LinearSolve`

function output consistent with the result form of the textbook (arbitrary constant can be replaced by `c`

)?

**In addition,** I have another problem. To prove that two linear equations have the same solution, we need the row vectors of their augmented matrices to be equivalent to each other.

For example, to prove that the linear equations represented by the following two matrices have the same system of solutions, I need to remove the meaningless zero row vector at the end:

```
RowReduce({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}})
RowReduce({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {2, 4, 6}})
```

I want to know what efficient way to get rid of the meaningless zero line vector at the end?

*By the way, it’s inconvenient to customize the display size of the inserted images.*

## Is there a function to convert a trigonmetric expression to an algebraic expression suitable for ComplexPlot?

According to H. A Priesley:

“both $cosθ$ and $sinθ$ can be written as simple algebraic functions of

$z$“:

$cosθ =frac{1}{2} (z + frac{1}{z}) tag1$

$sinθ =frac{1}{2i} (z − frac{1}{z})tag2$

Which is very handy but I’m not sure what function will convert say $frac{sinh(ax)}{sinh(bx)},$ ot $ frac{xcos ax}{1+x^2}coth frac{pi x}{4}$ to similar algebraic functions. The only function I can find is TrigtoExp, but this does not include the needed substution $ z = exp iθ$ to work with function ComplexPlot.

Q1. Is there a function to convert a trigonmetric expression to an algebraic expression suitable for ComplexPlot?

Q2. If not what is the best way to get around this?

## counting – Polynomial time counter of solutions of 2SAT expression with pure literals

As per the title, is there any polynomial time algorithm to count the number of satisfying arguments for a 2SAT expression with pure literals? An even shallower case: Is there any such counter when the literals in the expression are either all positive, or all negative?

## pr.probability – Inaccurate results for the analytical expression of $mathbb{E}left[ a mathcal{Q} left( sqrt{b } gamma right) right]$

I’m trying to plot a graph for the following expectation

$$mathbb{E}left( a mathcal{Q} left( sqrt{b } gamma right) right)=a 2^{-frac{kappa }{2}-1} b^{-frac{kappa }{2}} theta ^{-kappa } left(frac{, _2F_2left(frac{kappa }{2}+frac{1}{2},frac{kappa }{2};frac{1}{2},frac{kappa }{2}+1;frac{1}{2 b theta ^2}right)}{Gamma left(frac{kappa }{2}+1right)}-frac{kappa , _2F_2left(frac{kappa }{2}+frac{1}{2},frac{kappa }{2}+1;frac{3}{2},frac{kappa }{2}+frac{3}{2};frac{1}{2 b theta ^2}right)}{sqrt{2} sqrt{b} theta Gamma left(frac{kappa +3}{2}right)}right)$$

where $a$ and $b$ are constant values, $mathcal{Q}$ is the Gaussian Q-function, which is defined as $mathcal{Q}(x) = frac{1}{sqrt{2 pi}}int_{x}^{infty} e^{-u^2/2}du$ and $gamma$ is a random variable with Gamma distribition, i.e., $f_{gamma}(y) sim frac{1}{Gamma(kappa)theta^{kappa}} y^{kappa-1} e^{-y/theta} $ with $kappa > 0$ and $theta > 0$.

This equation was also found with Mathematica, so it seems to be correct.

Follows some examples, where I have checked the analytical results against the simulated ones.

When $kappa = 12.85$, $theta = 0.533397$, $a=3$ and $b = 1/5$ it returns the correct value $0.0218116$.

When $kappa = 12.85$, $theta = 0.475391$, $a=3$ and $b = 1/5$ it returns the correct value $0.0408816$.

When $kappa = 12.85$, $theta = 0.423692$, $a=3$ and $b = 1/5$ it returns the value $-1.49831$, which is negative. However, the correct result should be a value around $0.0585$.

When $kappa = 12.85$, $theta = 0.336551$, $a=3$ and $b = 1/5$ it returns the value $630902$. However, the correct result should be a value around $0.1277$.

Therefore, the issue happens as $theta$ decreases. For values of $theta > 0.423692$ the analytical matches the simulated results. The issue only happens when $theta <= 0.423692$.

I’d like to know if that is an accuracy issue or if I’m missing something here and if there is a way to correctly plot a graph that matches the simulation.