Why are nested anonymous pure functions shielded from evaluation?

I tried the following code (ignoring the warning messages):

{#, # &, Function[{x}, #], Function[{#}, x], Function[{#}, #]} &@7 (*result: {7, #1 &, Function[{x}, 7], Function[{7}, x], Function[{7}, 7]}*) 

I wonder why #& was not changed into 7&. I saw a "possible issue" similar to this mentioned in ref/Slot, but I couldn’t find further documentation about it. Is it a bug or it is specially designed this way?

Why doesn’t Mathematica ComplexExpand integrals?

Consider this code example:

ComplexExpand[Re[Integrate[f[t], {t, a, b}]]] 

Mathematica gives me the result as

Re[Integrate[f[t], {t, a, b}]] 

which is obviously not helpful and not what should happen in my understanding. If all variables are real – and that’s what ComplexExpand assumes according to the documentation – then Re can be dropped from the expression. The same happens for Im. This seems to confuse FullSimplify which leaves me with a long expression that could be shorted. Why is this the case?

How to insert text in Table

I have

Table[{i^2, "mod",  i + 1, "=", Mod[i^2, i - 1]}, {i, 3, 4}] 

which gives me

{{9, mod, 4, =, 1}, {16, mod, 5, =, 1}} 

But I want to get

{{9 mod 4 = 1}, {16 mod 5 = 1}} 

How can I get rid of the commas in the output? I can drop the first comma in Table after i^2 but when dropping other commas things get messy. What am I doing wrong? What are the rules? I can’t find anything in Help.

Cycle detection with Mathematica

Suppose we have a recurrent process. And we know for sure that it is cyclical. That it has a period.

How do I find this period using Wolfram Mathematica?

An example process is provided here.

I am using code like this:

iteratedStates[init_]: = NestWhileList[process[#]&, init, Unequal, All];  period[init_]: = With[{data = iteratedStates[init]},     First@Differences@Flatten@Position[data, Last@data]     ]; 

It is not effective. I tried to implement various algorithms but their implementations were even slower ((

UPD. Yes, there is FindTransientRepeat
But here what we have:

data = ContinuedFraction[(Sqrt[12] + 2)/7, 10004]; Timing[Length@Last@FindTransientRepeat[data, 2]] {0.0624004, 6} Timing[First@Differences@Flatten@Position[data, Last@data]] {0.0156001, 6} 

I need store all data. So NestWhileList is needed. And it seems that my implementation is good enough

Turn definitions into rules

I know that this seems to be unnecessary, but here I have a bunch of assignments, like around 50, and I want to create rules for all these to have the freedom to apply these rules only on certain expressions. However I can’t find an efficient way to do this. Please help.

Clear is also not efficient because I have to put in all those names of the variables.

Tricks to optimize a maximization problem

I am dealing with the following piece of code, to study a problem in quantum mechanics:

L[n_] := KirchhoffMatrix[CompleteGraph[n]];  c[n_] := 1; w[n_, p_] := Table[KroneckerDelta[k, p], {k, 1, n}]; P[n_, p_] := KroneckerProduct[w[n, p], w[n, p]]; s[n_] := 1/Sqrt[n]*Table[1, {k, 1, n}]; Ps[n_] := KroneckerProduct[s[n], s[n]];  H[n_, \[Lambda]_] := \[Lambda] L[n] - P[n, c[n]];  U[n_, t_, \[Lambda]_] := MatrixExp[-I*t*H[n, \[Lambda]]]; \[Psi][n_, t_, \[Lambda]_] := U[n, t, \[Lambda]].s[n]; prs[n_, t_, \[Lambda]_] := Abs[w[n, c[n]].\[Psi][n, t, \[Lambda]]]^2;  Prob[n_] := NMaximize[prs[n, t, \[Lambda]], {t,\[Lambda]}][[1]] 

The NMaximize function takes quite a while on my machine to compute $ \text{Prob}(n)$ , so I would be interested in any suggestion to increase the efficiency of the code – taking into account that the input graph could be a different one. Probably the hardest part to compute is taking the exponential matrix of $ H$ , but I’m not aware of any way to optimize it.

How to fix the error and do thread to those equation

 bList = Table[b, {b, 0, 4.1, 0.1}];   \[Chi][bList_] = (Z/K1)*NIntegrate[Subscript[f, pp1][q]*Subscript[F, pp1][q]*BesselJ[0, q*bList]*q, {q, 0, 1}] +  (N/K1)*NIntegrate[Subscript[f, pn1][q]*Subscript[F, pn1][q]*BesselJ[0, q*bList]*q, {q, 0, 1}];  

I want to do Thread and Interpolation to those values but always give me this error NIntegrate::inumr: The integrand (0.0131326 r Sin[q r])/(1+(E^(1.75439 Plus[<<2>>])+E^(1.75439 Plus[<<2>>])) (0.5 +0.0882389 Power[<<2>>])^1.5) has evaluated to non-numerical values for all sampling points in the region with boundaries {{[Infinity],0.}}.

 Subscript[f, pp1][q_] =(6.254736279890945*(1 - 1.4511668475476842*i)*i)/E^(0.2115*q^2)  Subscript[F, pp1][q_] := ((4*Pi)/q)*NIntegrate[Subscript[\[Rho], p][r]*Sin[q*r]*r, {r, 0, Infinity}];    Subscript[\[Rho], p][r_]=0.013132593248303927/(1 + (E^(1.7543859649122808*(-2.380427976610103 - r)) + E^(1.7543859649122808*(-2.380427976610103 + r)))*(0.5 + 0.08823886490842314*r^2)^1.5)   Z = 6;N=6;K1 =1.55;  Subscript[f, pn1][q_] =(18.25974896615874*(1 - 0.8748275119106319*i)*i)/E^(0.2115*q^2)  Subscript[F, pn1][q_]:= ((4*Pi)/q)*NIntegrate[Subscript[\[Rho], n][r]*Sin[q*r]*r, {r, 0, Infinity}];   Subscript[\[Rho], n][r_]=0.012100952740630248/(1 + (E^(1.8975332068311195*(-2.47 + r)) + E^(-1.8975332068311195*(2.47 + r)))*(0.5 + 0.08195512137553475*r^2)) 

How to solve this analytic geometry problem completely

I want to find a plane that passes through points {1,0,0} and {0,1,0} and is tangent to surface $ z(x,y)=x^{2}+y^{2}$ .

Solve[{a, b, c}.{1, 0, 0} == d && a*0 + b*1 + c*0 == d &&    a*x0 + b*y0 + c*z0 == d && z0 == x0^2 + y0^2 &&    VectorAngle[{a, b, c}, {-2 x0, -2 y0, 1}] ==     0,(*MatrixRank[{2x0,2y0,1},{a,b,c}]\[Equal]1*){a, b, c, d, x0, y0,    z0}] 

But I can’t get the answer I want with the above code(the answer is $ z=0$ and $ 2x+2y-z=2$ ). What should I do?

NMinimize: How to avoid solutions that do not satisfy constraints within a certain tolerance?

I just started to use Mathematica a few weeks ago. Using NMinimze, I would like to avoid solutions that do not satisfy certain constraints (although they "almost" satisfy them). Do you know how to change the following command to find a solution satisfying "completely" all the constraints, solving the same minimization problem?

NMinimize[{((e*(1 - Sqrt[(g - e)^2 + (f - h)^2]) + (g - e)*(1 -        Sqrt[f^2 + e^2])) + (h*(1 -        Sqrt[(g - e)^2 + (f - h)^2]) + (f - h)*(1 -        Sqrt[g^2 + h^2])))/((g + f)*  Max[1 - Sqrt[(g - e)^2 + (f - h)^2], 1 - Sqrt[g^2 + h^2]]), 0 <= e <= 1, 0 <= f <= 1, e^2 + f^2 == 1, e <= g <= 1, 0 <= h <= f, Sqrt[(g - e)^2 + (f - h)^2] <= 1, g^2 + h^2 <= 1}, {e, f, g, h}]