If a group algebra acts regularly on a module, can this module be identified as a left ideal?

Question: To be more specific, I am looking at $F_2[D_p]$, where $D_p$ is the dihedral group of order $2p$.
If this group acts regularly on the basis of a vector space $F_2^{2p}$, and there is a subspace of $F_2^{2p}$ that is fixed under the action of the group, is it true that this subspace can be identified with a left ideal of $F_2[D_p]$?  I guess the idea is that you can identify each basis element of $F_2^{2p}$ with an element in $D_p$.  But is it guaranteed we can do that as long as the action is regular?
Besides, it kinda bothers me that $F_2[D_p]$ is not semi-simple, since the characteristic (which is 2) divides the order of $D_p$.  This ring doesn't decompose.
Comment: What do you mean by "regular"?  If you mean that the group $D_p$ acts regularly on a basis, then that vector space is isomorphic to $F_2[D_p]$ by the definition of a regular action.
Comment: Yes, that's exactly what I mean by regular.  And I actually meant a subspace of $F_2^{2p}$ (corrected in the questions).   So does that mean this space is equivalent to a left ideal of the group ring?
Answer: The fact that the action is regular gives an equivariant isomorphism $F_2^{2p}\cong F_2[D_p]$.  This isomorphism sends your invariant subspace to an invariant subspace (under left multiplication) of $F_2[D_p]$. That is, a left ideal.  So, yes.
Comment: I don't understand why they are isomorphic...  Is there a way to explicitly construct the isomorphism?
Comment: You send 1 to any one of the basis vectors; there's a unique equivariant extension.
Comment: Yes, I figured that later.  Thanks for your help!


$A(1,1,1)$, $B(2,1,2)$, $C(3,2,1)$ and $D(2,3,2)$ form a tetrahedron. If $ABC$ is the base, then what is the height?

Question: $A(1,1,1)$, $B(2,1,2)$, $C(3,2,1)$ and $D(2,3,2)$ form a tetrahedron. If $ABC$ is the base, then what is the height?
I found out of the equation of the plane containing A, B and C. It is $$-x + 2y +z -2 = 0$$
Then I found out the distance between the plane and $D(2,3,2)$ using the following:
$$\begin{align} distance &= \frac{-(2) + 2(3) + (2) - 2}{\sqrt{(-1)^2 + (2)^2 + (1)^2}} \\
&= \frac{4}{\sqrt 6} \\
&= 2 \sqrt{\frac 2 3}  \end{align}$$
This distance should be the height, because it is the perpendicular distance between $D$ and $ABC$.
But, my book says the answer is $$\frac 13 \sqrt{\frac 23}$$
Which answer is correct? Also, if I'm wrong, why?
Comment: Your solution looks correct to me.
Answer: Your answer is correct.
An alternative solution is to compute with vectors, i.e. to evaluate
$$h=\frac{\frac16|\vec {AC}\cdot(\vec{AB}\times\vec{AD})|}{\frac12|\vec{AB}\times\vec{AD}|}$$


For $\alpha>0$, evaluate $\int^{+\infty}_0xe^{-x}\cos x\cos(x^2/\alpha)\,dx$

Question: For $\alpha>0$, prove\begin{align}\displaystyle\int_0^{\infty}xe^{-x}\cos(x)\cos\left(\dfrac{x^2}{\alpha}\right){\rm d}x=\dfrac{\alpha\sqrt{2\pi\alpha}}{8}e^{-\alpha/2} \end{align}
My attempt: Let $$I(b)=\displaystyle\int_0^{\infty}e^{-x}\sin(bx)\cos\left(\dfrac{x^2}{\alpha}\right){\rm d}x$$
Hence$$I'(1)=\displaystyle\int_0^{\infty}xe^{-x}\cos(x)\cos\left(\dfrac{x^2}{\alpha}\right){\rm d}x  $$
But \begin{align}I(b) & =\displaystyle\int_0^{\infty}e^{-x}\sin(bx)\cos\left(\dfrac{x^2}{\alpha}\right){\rm d}x\\&=\dfrac{1}{2}\displaystyle\int_0^{\infty}e^{-x}\left(\sin(bx-x^2/\alpha)+\sin(bx+x^2\alpha)\right){\rm d}x\\&=I_1+I_2\end{align}
But I have difficulty using contour integral to calculate $I_1$ or $I_2$ .
how to solve it using contour inregral?
Is there a more efficient method to solve this problem? I have a thought that Fourier Transform is a possible method.
Comment: To preserve sanity, define $c = \frac{b^2}{4a}$ and $d = \frac{b}{2a}$
\begin{align}
&\int_0^{\infty}e^{-x}\sin(bx-ax^2) dx +\int_0^{\infty} e^{-x}\sin(bx+ax^2)dx
\\&=\int_0^{\infty}e^{-x}\left(\sin(a(x+d)^2-c) -\sin(a(x-d)^2-c)\right)dx
\\&= \frac{1}{2}\cos(c)\int_{0}^{\infty}e^{-x}\sin(a(x+d)^{2})dx-\frac{1}{2}\sin(c)\int_{0}^{\infty}e^{-x}\cos(a(x+d)^{2})dx \\ &\qquad-\frac{1}{2}\cos(c)\int_{0}^{\infty}e^{-x}\sin\left(a(x-d)^{2}\right)dx+\frac{1}{2}\sin(c)\int_{0}^{\infty}e^{-x}\cos\left(a(x-d)^{2}\right)dx
\end{align}
Comment: You can brute force this. Break the sine functions up into complex exponentials, and you have four integrals to solve. Each has a result in terms of the Error Function, which cancels when you combine stuff
Comment: \begin{align}
\\&= \frac{1}{2}\cos(c)e^d\int_{d}^{\infty}e^{-x}\sin(ax^{2})dx-\frac{1}2\sin(c)e^d\int_{d}^{\infty}e^{-x}\cos(ax^2)dx - \\ & \qquad \frac{1}{2}\cos(c)e^{-d}\int_{-d}^{\infty}e^{-x}\sin\left(ax^2\right)dx+\frac{1}{2}\sin(c)e^{-d}\int_{-d}^{\infty}e^{-x}\cos(ax^2)dx
\end{align}
You can now use the Fresnel Integrals to finish with the right cancellations since the primitives of each integral are well known enough. Just break up each integral over bounds from $0$ to $\infty$, let the weird parts cancel, give the remaining pieces in terms of Fresnel Integrals, and cancel
Comment: This really just obfuscates the nice cancellation we get from the complex exponentials though
Answer: I will write
$$ J_{\pm} = \int_{0}^{\infty} xe^{-x}\cos\left(x\pm\frac{x^2}{\alpha}\right)\,\mathrm{d}x $$
so that your integral takes the form $\frac{1}{2}(J_{+} + J_{-})$. Then
\begin{align*}
J_{\pm}
&= \operatorname{Re}\left[ \int_{0}^{\infty} x\exp\left( -x + ix \pm \frac{ix^2}{\alpha}\right) \,\mathrm{d}x. \right]
\end{align*}
Now write $\mathbb{H}_{\text{right}} = \{ z \in \mathbb{C} : \operatorname{Re}(z) > 0 \}$. Then for each $a \in \mathbb{H}_{\text{right}}$, the map $z \mapsto \int_{0}^{\infty} x e^{-ax-zx^2} \, \mathrm{d}x$ is analytic on $\mathbb{H}_{\text{right}}$ and continuous on $\overline{\mathbb{H}_{\text{right}}}$. Moreover, if $a, z \in (0, \infty)$, then with $b = a^2/4z$,
\begin{align*}
\int_{0}^{\infty} x e^{-ax-zx^2} \, \mathrm{d}x
&= \int_{0}^{\infty} x \exp\bigg( -b \left(\frac{2z x}{a}+1\right)^2 + b \bigg) \, \mathrm{d}x \\
&= \frac{be^{b}}{z} \int_{1}^{\infty} (u-1) e^{-bu^2} \, \mathrm{d}u \\
&= \frac{be^{b}}{z} \left( \int_{1}^{\infty} u e^{-bu^2} \, \mathrm{d}u - \int_{0}^{\infty} e^{-bu^2} \, \mathrm{d}u + \int_{0}^{1} e^{-bu^2} \, \mathrm{d}u \right) \\
&= \frac{1}{2z} - \frac{\sqrt{\pi} a e^{b}}{4z^{3/2}} + \frac{be^{b}}{z} \int_{0}^{1} e^{-bu^2} \, \mathrm{d}u.
\end{align*}
Then by the principle of analytic continuation, this holds for all $a \in \mathbb{H}_{\text{right}}$ and $z \in \overline{\mathbb{H}_{\text{right}}}$. Then plugging $a = 1-i$ and $z = z_{\pm} = \pm i/\alpha$, we get $b = b_{\pm} = \mp \alpha /2 \in \mathbb{R}$.
\begin{align*}
J_{\pm}
&= \operatorname{Re}\bigg[ \frac{1}{2z} - \frac{\sqrt{\pi} a e^{b}}{4z^{3/2}} + \frac{be^{b}}{z} \int_{0}^{1} e^{-bu^2} \, \mathrm{d}u \bigg] \\
&= -\frac{\sqrt{\pi} \, e^{b}}{4} \operatorname{Re}\bigg[ \frac{a}{z^{3/2}} \bigg],
\end{align*}
By noting that $a_{+}/z_{+}^{3/2} = -\sqrt{2}\,\alpha^{3/2}$ and $a_{-}/z_{-}^{3/2} = i\sqrt{2}\,\alpha^{3/2}$, we get
$$ J_{+} = \frac{\sqrt{2\pi}}{4} \alpha^{3/2} e^{-\alpha/2}, \qquad J_{-} = 0. $$
This complete the proof.
Answer: Let $$I(\beta) =\int_{0}^{\infty} x e^{-x} \cos (x) \cos( \beta x^{2}) \, \mathrm dx \, , \quad \beta>0. $$
Let's take the Laplace transform of $I (\beta)$ and then switch the order of integration (which is permissible since the iterated integral converges absolutely).
$$ \begin{align}\mathcal{L} \{I(\beta)\} (s) &= \int_{0}^{\infty} \left(\int_{0}^{\infty} x e^{-x} \cos(x) \cos(\beta x^{2}) \, \mathrm dx \right)e^{-s \beta } \,  \mathrm d \beta \\ &= \int_{0}^{\infty} x e^{-x} \cos(x) \int_{0}^{\infty}\cos(\beta x^{2}) e^{- s \beta } \, \mathrm d \beta \, \mathrm dx \\ &= s \int_{0}^{\infty} \frac{x e^{-x}\cos (x)}{x^{4}+s^{2}} \, \mathrm dx \tag{1} \end{align} $$
To evaluate $(1)$, we can integrate the complex function $$f(z) = \frac{z e^{-z}e^{iz}}{z^{4}+s^{2}} $$ around a wedge-shaped contour that makes an angle of $\frac{\pi}{2}$ with the positive real axis (i.e., a closed quarter-circle in the first quadrant of the complex plane).
In the first quadrant of the complex plane, $\vert e^{-z} \vert \le 1$ and $\vert e^{iz} \vert \le 1$.  So the integral along the big arc clearly vanishes as the radius of the arc goes to $\infty$.
Integrating around the contour, we get $$ \begin{align} \int_{0}^{\infty} \frac{x e^{-x} e^{ix}}{x^{4}+s^{2}} \, \mathrm dx + \int_{\infty}^{0} \frac{(it)e^{-it} e^{-t}}{t^{4}+s^{2}} \, i \, \mathrm dt &= 2 \pi i \operatorname{Res} \left[f(z), e^{i \pi/4} \sqrt{s}  \right] \\ &= 2 \pi i \lim_{z \to e^{i \pi/4} \sqrt{s}}\frac{ze^{-z}e^{iz}}{4z^{3}} \\ &= 2 \pi i \, \frac{e^{i \pi/4}\sqrt{s} \, e^{-\sqrt{2s}} }{4e^{3 \pi i/4}s^{3/2}} \\ &= \pi \, \frac{e^{-\sqrt{2s}}}{2s}. \end{align}$$
And if we equate the real parts on both sides of the equation, we get $$2 \int_{0}^{\infty} \frac{xe^{-x} \cos(x) }{x^{4}+s^{2} } \, \mathrm dx = \pi \,  \frac{e^{-\sqrt{2s}}}{2s}.$$
Therefore, $$\mathcal{L} \{I(\beta)\} (s) = \frac{\pi e^{-\sqrt{2s}}}{4}. $$
Due to the uniqueness of the inverse Laplace transform , we only need to show that  $$\mathcal{L} \left\{\frac{\sqrt{2 \pi}}{8} \beta^{-3/2} e^{-1/(2 \beta)} \right\} (s) = \frac{\pi e^{-\sqrt{2s}}}{4}$$ in order to prove that $$I(\beta) =  \frac{\sqrt{2 \pi}}{8} \beta^{-3/2} e^{-1/(2 \beta)}.$$
From the answers here, we know that for $a,b>0$, $$\int_{0}^{\infty} \frac{\exp \left(-ax^{2}-b/x^{2}\right)}{x^{2}} \, \mathrm dx =  - \frac{1}{2} \sqrt{\frac{\pi}{a}} \, \frac{\partial }{\partial  b} e^{-2\sqrt{ab}} = \frac{1}{2} \sqrt{\frac{\pi}{b}} \, e^{-2\sqrt{ab}}.$$
(Differentiation under the integral sign is permissible since for any positive $c$ less than $b$, $\frac{\exp \left(-ax^{2}-b/x^{2}\right)}{x^{2}}$ is dominated by the integrable function $ \frac{\exp \left(-ax^{2}-c/x^{2}\right)}{x^{2}}$.)
Therefore, $$\begin{align} \mathcal{L} \left\{\frac{\sqrt{2 \pi}}{8} \beta^{-3/2} e^{-1/(2 \beta)} \right\} (s) &=  \frac{\sqrt{2 \pi}}{8} \int_{0}^{\infty}\beta^{-3/2} e^{-1/(2 \beta)} e^{-s \beta } \, \mathrm d \beta \\ &= \frac{\sqrt{2 \pi}}{4} \int_{0}^{\infty} \frac{\exp \left(-s u^{2} - (1/2)/u^{2} \right)}{u^{2}} \mathrm du \\ &= \frac{\sqrt{2 \pi}}{4}  \frac{\sqrt{2 \pi}}{2} \, e^{-\sqrt{2s}}  \\ &= \frac{ \pi \, e^{-\sqrt{2s}}}{4}. \end{align}$$


How to best find standard error *across* linear regression fits?

Question: So I have a scenario where there are $n = 8$ subjects, which are observed at 20 time points and having heteroscedasticity in their response. For example, consider the following:
num_datasets = 8;

x = [1:20]';

%define matrix for the response for 8 different datasets
Y = repmat(x,1,8) * nan;

for i = 1:size(X,2)
    Y(:,i) = 2*x + unifrnd(3,8)*randn(size(x));
end

So clearly each observation/subject has the same linear model relating their response ($y$) to the regressor ($x$), but the amounts/sources of noise vary between subject. Now, I know that the standard error for the linear regression fit has the form:
$$\sigma\sqrt{\frac{1}{n}+ \frac{(x^*-\bar x)^2}{\sum_{i=1}^n (x_i-\bar{x})^2} }$$
where $\sigma$ represents the standard deviation of the residuals of the fit, $n$ represents the number of samples in the observation (in my example above this would be 20, not 8), $(x^* - \bar x)$ represents the distance of each $x_i$ sample from the mean (which is why the standard error increases hyperbolically as you deviate from the mean), and then ${\sum_{i=1}^n (x_i-\bar{x})^2}$ is simply the variance in $x$.
However, if I interpret this equation correctly, I think this gives the standard error across the dimension of $x$, and doesn't directly tell me the standard error across subjects. In other words, I suspect it wouldn't be a good idea to use this formula for each subject and then take the mean standard error (please correct me if I am wrong). So I have 2 questions:

What would be the best way to calculate the standard error across subjects? Would it simply be to perform the fit for each subject, and then take the standard deviation of the fits?

What would the shape of the standard error of the fit look like, and what is the intuition behind that? Would it still be hyperbolic? I don't think it would, but actually really not sure.
Comment: I think this link will definitely help you
[What is the standard error of the within-subject standard deviation, sw?](https://www-users.york.ac.uk/~mb55/meas/seofsw.htm#:~:text=Normal%2C%20to%20estimate%20the%20standard%20error.%20The%20within-subject,this%20is%20the%20estimate%20of%20the%20standard%20deviation.)
Answer: It might be best to think about this situation in terms of meta-analysis: putting together information from several studies to estimate the model for the underlying population. Studies are combined by weighting them according to information they provide, typically inversely weighting each study by the variance of its estimates.
You can think about your case as representing 8 different "studies" (8 different subjects), with each having values of $y$ measured at 20 values of $x$. We assume that within each subject the standard assumptions of linear regression hold, in particular that observations are uncorrelated and the variance of $y$ about the regression is independent of the value of $x$. Unlike many practical meta-analyses that depend on reported summaries of results for each of several studies, you still have the individual data for each "study."
So if you want a model for the underlying population, one simple way to proceed would be to do each of the 8 individual regressions and determine the value of estimated residual variance $\hat\sigma_j^2$ for each subject $j$.* Then re-weight each individual data point inversely to that estimated variance for the corresponding subject, and perform a weighted least-squares regression over all the 160 data points.
What you call the "hyperbolic" shape of error in $\hat y$ for new predictions as a function of $x$ will be the same. It comes from the uncertainty in the estimate of the slope in the regression. The error is smallest ($\sigma/\sqrt{n}$) at the mean value of $x$, then increases with that shape simply because you are uncertain how quickly $y$ changes with $x$ as you move farther away from $\bar x$. Linear regression with uncorrelated observations weighted by their variances provides the best linear unbiased estimators (BLUE) of the regression coefficient,** and with the weighted regression combining all cases you now have an $n$ value of 160. So the width of that uncertainty area will tend to be minimized.
Some caution is in order, as the NIST page puts it:

The biggest disadvantage of weighted least squares, which many people are not aware of, is probably the fact that the theory behind this method is based on the assumption that the weights are known exactly. This is almost never the case in real applications, of course, so estimated weights must be used instead.

And as @cardinal put it:

Learning a variance is hard.

For a normal distribution with variance $\sigma^2$, the variance of a variance estimate $\hat\sigma^2$ from $n$ observations is $2\sigma^4/(n-1)$. So unless you have many data points and a reason to believe that there are substantial differences in the true $\sigma_j^2$ values among the subjects $j$ there might not be much benefit to this weighting approach.
The above assumes that all subjects have the same slopes and intercepts for the relationship between $y$ and $x$. One might interpret your suggestion to "take the standard deviation of the fits" as meaning that you expect true differences among subjects in these parameter values. In that case you could obtain estimates for the variances of intercepts and slopes among subjects with a mixed model. Weighting of the individual data points could still be done.

*I believe that there is a way to estimate all the within-subject variances and the shared regression coefficients in a single model, but I don't recall immediately what that is. It would probably require an iterative or maximum-likelihood approach. This is simple approach gets to the substance of your question.
**See the page linked on weighted regression. This assumes that the variances are known.
Answer: Question 1. What would be the best way to calculate the standard error across subjects? Would it simply be to perform the fit for each subject, and then take the standard deviation of the fits?
Option 1: Use weighted least squares. The Gauss Markov theorem tells us that the inverse variance weighted standard error estimator will be the Best Linear Unbiased Estimator (BLUE). Note that although the mean model is correct, and consequently the unweighted estimate is unbiased, there is added efficiency of using the iterative generalized least squares estimator to provide a better estimate of the residuals. It helps to identify the appropriate degrees of freedom for the intracluster variance. For reference, I've included the two-stage estimate, but I have trouble identifying the correct degree of freedom correction.
One interesting result that I'm working on is the idea that off-the-box software with options for intracluster correlation can provide consistent estimates of heteroscedasticity. That is, regardless of whether a sample is highly intracorrelated or highly variable, the net effect is a downweighting of that sample, and so the same optimal standard error can be obtained in either case.
Using unweighted residuals to estimate cluster variance, I'm finding is that it's hard to identify the appropriate degree of freedom for the intracluster variance estimate. I'm adding my code below for others to verify. $n-1$ is too conservative, and $n-2$ is too conservative.
Option 2: Use the sandwich variance estimator (heteroscedasticity consistent) or bootstrap.
Question 2: What would the shape of the standard error of the fit look like, and what is the intuition behind that? Would it still be hyperbolic? I don't think it would, but actually really not sure.
The limiting distribution of the error distribution is still normal provided that the sample "grows faster" in terms of number of time points rather than in the number of subjects, or at least such that subject level heteroscedasticity is somewhat bounded. The intuition is that it is a result of the Lyapunov central limit theorem.
require(gee)
`%covers%` <- function(x, y) x[1] < y & y < x[2]
sse.df <-function(x, df=1) {
  sum({x-mean(x)}^2)/{length(x)-df}
}
confint.gee <- function (object, parm, level = 0.95, ...)
{
  cf <- coef(object)
  pnames <- names(cf)
  if (missing(parm))
    parm <- pnames
  else if (is.numeric(parm))
    parm <- pnames[parm]
  a <- (1 - level)/2
  a <- c(a, 1 - a)
  # pct <- format.perc(a, 3)
  pct <- paste0(formatC(100*a, format='f', digits=1), '%')
  fac <- qnorm(a)
  ci <- array(NA, dim = c(length(parm), 2L), dimnames = list(parm,
                                                             pct))
  # ses <- sqrt(diag(vcov(object)))[parm]
  ses <- sqrt(diag(object$robust.variance))[parm]
  ci[] <- cf[parm] + ses %o% fac
  ci
}

do.one <- function() {
  s1 <- 1
  s2 <- 1
  nc <- 8
  nt <- 20
  i <- rep(1:8, each=nt)

  e <- rnorm(nc, 0, s1)[i] + rnorm(nc*nt, 0, s2)

  x <- rep(seq(-3, 3, length.out = nt), times=nc)
  y <- 2*x + e

  r <- lm.fit(cbind(1,x), y)$residuals

  wls <- lm(y ~ x, weights=rep(1/tapply(r^2, i, sse.df, df=1), each=nt))
  gls <- gls(y ~ x, correlation=corCompSymm(form=~1|i))

  gee <- gee(y ~ x, id = i)

  c( ## coverage of 80% CIs
    confint(wls, parm='x', level = .8) %covers% 2,
    confint(gee, parm='x', level = .8) %covers% 2,
    confint(gls, parm='x', level= 0.8) %covers% 2,
    vcov(wls)[2,2]^.5,
    gee$robust.variance[2,2]^.5,
    vcov(gls)[2,2]^.5
  )
}

set.seed(123)
out <- replicate(500, do.one())

## 80% coverage of CIs
rowMeans(out[1:3, ])

par(mfrow=c(1,3))
hist(out[4, ], xlab='Sigma two-pass', main='')
hist(out[5, ], xlab='Sigma GEE', main='')
hist(out[6, ], xlab='Sigma GLS', main='')

Gives us coverage of 70% for 2 degree of freedom WLS and 74% for the GEE. and 82.54% for GLS. Histograms of the standard error estimates show a closely normal distribution in all cases.


Integral involving hypergeometric function

Question: I've worked out the projection of a spherically symmetric power law volume density profile $\rho(r)=br^a$, i.e. its surface density $\sigma(R)$, and am now trying to integrate this in a series of annular bins to get the total mass in each, i.e. $\int_A^B\sigma(R)R\,{\rm d}R$. (Actually the profile is a continuous series of power laws, but that's more of a footnote.)
I've worked just about everything out, but am stuck on an integral of the form:
$$I(r,R,a)=\int\left(\left(\frac{r}{R}\right)^2-1\right)^{\frac{1}{2}}R^{a+2} {}_2{\rm F}_1\left(\frac{1}{2},-\frac{a}{2};\frac{3}{2};-\left(\left(\frac{r}{R}\right)^2-1\right)\right){\rm d}R$$
${}_2{\rm F}_1$ is the hypergeometric function, as defined here. $a$ and $r$ are constants, in this context.
Given that the solution of this integral will be used to calculate the elements of a big-ish matrix and I have some iteration in mind, integrating numerically is... inconvenient, but I'm stumped on finding an analytic solution. Is there any hope here, or should I start thinking about efficient numerical solutions?
Comment: Try expanding the Hypergeometric then integrating term by term
Comment: @Kyle thank you very much for your generous bounty!
Comment: @PaulEnta thanks for the answer! The solution of the integral goes into a computation for a measurement in astrophysics/cosmology I'm collaborating on at the moment, which we should start writing up for a journal soon. If you would like, I'd be happy to add an acknowledgement of your contribution along the lines of "The authors thank the math.stackexchange.com user Paul Enta for providing an analytic expression for eq X", or similar. There is usually a short section with such notes anyway. You may reach me by email at either of the addresses listed [here](https://www.rug.nl/staff/k.a.oman/).
Comment: I am happy to see that this answer can be useful to you. Thank you for proposing to acknowledge this contribution in your future paper. I think that it can be a good advertisment for MSE in your community. I wish you the best for this article and for the next ones!
Comment: @PaulEnta by the way we did finally write that paper and acknowledged your answer. You can find it at https://www.aanda.org/articles/aa/full_html/2021/06/aa40108-20/aa40108-20.html or at https://arxiv.org/abs/2106.11677
Comment: Thank you, @Kyle , impressing work! Cheers!
Answer: In the following, we assume $R>r$.
\begin{equation}
 I(r,R,a)=\int\left(\frac{r^2}{R^2}-1\right)^{\frac{1}{2}}R^{a+2} {}_2{\rm F}_1\left(\frac{1}{2},-\frac{a}{2};\frac{3}{2};1-\frac{r^2}{R^2}\right){\rm d}R
\end{equation}
changing $u=1-r^2/R^2$, we obtain
\begin{equation}
 I(r,R,a)=i\frac{r^{a+3}}{2}\int u^{\frac12}\left( 1-u \right)^{-\frac{a}{2}-\frac{5}{2}}{}_2{\rm F}_1\left(\frac{1}{2},-\frac{a}{2};\frac{3}{2};u\right){\rm d}u
\end{equation}
where the principal determination for the square root was taken. Using the indefinite integration formula:
\begin{equation}
 \int  z^{c-1 }(1 - z)^{b - c-1}{}_2{\rm F}_1\left( a,b;c;z \right)= z^c(1 - z)^{b - c}  \frac{\Gamma(c)}{\Gamma(c+1)} {}_2{\rm F}_1\left( 1+a,b;1+c;z \right)
\end{equation}
with $a=1/2,b=-a/2,c=3/2$, we find
\begin{equation}
  I(r,R,a)=-\frac{1}{3}R^{a+3}\left( \frac{r^2}{R^2}-1 \right)^{3/2} {}_2{\rm F}_1\left(\frac{3}{2},-\frac{a}{2};\frac{5}{2};1-\frac{r^2}{R^2}\right)
\end{equation}
which seems to be numerically correct.
Comment: There are not so many indefinite integrals for the hypergeometric functions and yours seemed to be "well conditioned".
Comment: Checks out, many thanks! Out of curiosity, and for future use, what led you to that integral identity for the hypergeometric function?


How to split up the renderd page in a tpl file?

Question: In my D7 template I have a tpl file called:
html--events.tpl.php
  <html class="no-js" lang="<?php print $language->language; ?>" dir="<?php print $language->dir; ?>"<?php print $rdf_namespaces; ?>>
<?php endif; ?>

  <?php print $head; ?>
  <title><?php print $head_title; ?></title>
  <?php print $styles; ?>
  <?php print $scripts; ?>

<body<?php print $attributes;?>>
  <?php print t('Skip to main content'); ?>

  <?php print $page_top; ?>
  <?php print $page; ?>
  <?php print $page_bottom; ?>

I would like to add a page header image to this page by changing the template.
So therefore I would like to split the rendered content of this page for e.g. like this:
  <?php print $page_top; ?>
  <?php print $pageheader; ?>
  <?php print "
/div>" ?>
  <?php print $pagetitle; ?>
  <?php print $pagemaincontnet; ?>
  <?php print $page_bottom; ?>

However the var $page contains already all the rendered header. How can I split the rendered page into smaller blocks? I tried to replace:
<?php print $page; ?>

into
 <?php print render($page['content']); ?>

but then got a message : Only variables can be passed by reference
Any suggestion is very welcome.
Answer: $page is the rendered content from page.tpl.php - you can't alter it in the HTML template without resorting to nasty string manipulation.
Fortunately your desired output lends itself nicely to being handled in the page template anyway. You won't automatically have the same template name suggestions available, although a theme/module might already make them available in your installation. If not, you can add it yourself:
function MYTHEME_preprocess_page(&$vars) {
  // This condition may need to change depending on your exact needs
  if (arg(0) == 'events' && !arg(1)) {
    $vars['theme_hook_suggestions'][] = 'page__events';
  }
}

Then in page--events.tpl.php you'll have access to the individual elements of the $page array, and you can render them as needed.


'Motion easing' effect in C#

Question: I need to increment an integer, but I want to ease the speed at which it's incremented. So say for example I have an int that is equal to 0, I would like this int to get to 100 eventually but increment progressively slower. Does anyone have experience with this?
Comment: Are you using WPF (so that you can introduce animations?) Otherwise, a worker thread with a timer and a interval formula?
Comment: I am using WPF yes, if hcb's solution doesn't do what I'm after then I'll look at using a DispatcherTimer. Thanks.
Answer: hcb's answer works for 100, but a different value would require a different ease value.
A more generalised answer would be to use a sine, which means the ease would be the same no matter what final value you wanted, or however many steps you take.
private void EaseIn(int easeTo)
{
     for (int n = 0; n < easeTo; n++)
     {
          double degrees = (n * 90) / easeTo;
          double easedN =  easeTo * Math.Sin(degrees * (Math.PI / 180));

          Console.WriteLine("Eased n = " + easedN.ToString());
     }
}

Answer: Like this?:
for (int i = 0; i <=100; i++)
{
    Thread.Sleep(i);
}

Or like this:
float j = 1;
float ease = 0.005;
for (float i = 0; i <=100; i+=j)
{
    j -= ease;
}

Comment: take 0.005 instead of 0.02 though, or else it won't go to 100.

'Motion easing' effect in C#


Terraform source error - Error: Failed to query available provider packages

Question: I am trying to deploy a new infrastructure using terraform (for the first time) and I am getting the following error. I've tried everything but nothing seems to fix the issue.
Looks like it is asking for a provider hashicorp/azure ?
Can anyone help please??
Initializing provider plugins...
- Finding latest version of hashicorp/azure...
- Finding hashicorp/azurerm versions matching "2.98.0"...
- Installing hashicorp/azurerm v2.98.0...
- Installed hashicorp/azurerm v2.98.0 (signed by HashiCorp)
╷
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider hashicorp/azure: provider registry registry.terraform.io does not have a provider named registry.terraform.io/hashicorp/azure
│
│ Did you intend to use terraform-providers/azure? If so, you must specify that source address in each module which requires that provider. To see which modules are currently depending on hashicorp/azure, run the following command:
│     terraform providers
╵

lucas@Azure:~$ terraform providers

Providers required by configuration:
.
├── provider[registry.terraform.io/hashicorp/azurerm] 2.98.0
└── provider[registry.terraform.io/hashicorp/azure]

The code that I am using to create the infrastructure is the below:
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=2.98.0"
    }
  }
}

provider "azurerm" {
  features {}
  subscription_id = "910910be-a61e-4e1f-a72a-7e43456c0836"
}

# Create a resource group
resource "azurerm_resource_group" "rg" {
  name     = "default"
  location = "West Europe"
}

# Create a virtual network
resource "azurerm_virtual_network" "vpc" {
  name                = "default-network"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  address_space       = ["10.0.0.0/16"]
}

# Create frontend subnet
resource "azurerm_subnet" "subnet_frontend" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.vpc.name
  address_prefixes     = ["10.0.1.0/24"]
}

# Create backend subnet
resource "azurerm_subnet" "subnet_backend" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.vpc.name
  address_prefixes     = ["10.0.2.0/24"]
}

# Create frontend network interface
resource "azurerm_network_interface" "frontend_nic" {
  name                = "frontend_nic"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.subnet_frontend.id
    private_ip_address_allocation = "Dynamic"
  }
}

# Create backend network interface
resource "azurerm_network_interface" "backend_nic" {
  name                = "backend_nic"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.subnet_backend.id
    private_ip_address_allocation = "Dynamic"
  }
}

# Create frontend VM based on module
resource "azure_instance" "frontend" {
  source   = "./vm"
  name     = "frontend"
  rg       = module.azurerm_resource_group.rg.name
  location = module.azurerm_resource_group.rg.location
  nic      = module.azurerm_network_interface.frontend_nic
}

# Create backend VM based on module
resource "azure_instance" "backend" {
  source   = "./vm"
  name     = "backend"
  rg       = module.azurerm_resource_group.rg.name
  location = module.azurerm_resource_group.rg.location
  nic      = module.azurerm_network_interface.backend_nic
}

My terraform version is: Terraform v1.1.5 and I am using on Azure CLI via bash.
Any idea of what is causing this issue and how to fix it?
Thanks!
Answer: This often happens when one accidentally specifies "hashicorp/azure" or "hashicorp/azurem" instead of "hashicorp/azurerm" in the required_providers block. Did you check the "vm" module referenced in the "azure_instance" module calls? There might be an erroneous "hashicorp/azure" specified there.
Comment: Hi lad. Yes, I've fixed this issue, thanks. Now I got another one. Please see below:
Comment: ```rror: Reference to undeclared module
│
│   on main.tf line 76, in resource "azurerm_virtual_machine" "frontend":
│   76:   location = module.azurerm_resource_group.rg.location
│
│ No module call named "azurerm_resource_group" is declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│   on main.tf line 85, in resource "azurerm_virtual_machine" "backend":
│   85:   location = module.azurerm_resource_group.rg.location
│
│ No module call named "azurerm_resource_group" is declared in the root module. ```
Comment: @cdub regarding the second error, from line 74 -> 85, replace `module.azurerm_resource_group.rg.name` with `azurerm_resource_group.rg.name` & repeat for other lines as you aren't using any modules..
Answer: My problem was more subtle.
All the provider were defined correctly but I had a data resource misspelled!
data "azure_virtual_network" "vnet" {
  name                = "my_vnet"
  resource_group_name = "my_rg"
}

As you can see I referred there to the data azure_ that is wrong. it should be:
data "azurerm_virtual_network" "vnet" {
  name                = "my_vnet"
  resource_group_name = "my_rg"
}

My suggestion is to search your code with the failing provider name. In this case azure_ to find the problem.


connecting signals and slots with different relations

Question: First of all I'd say I'm a noob to GUI programming. I use Qt 5.4.
I came up with this code while watching voidRealms videos.
connect(ui->horizontalSlider,SIGNAL(sliderMoved(int)),ui->progressBar,SLOT(setValue(int)));

Obviously this connects slider movement with progressbar fill. This actually works like
progressbarfill <- slidermovement.
How can I make into a different relation? Like
progressbarfill <- (slidermovement)/2 or something like that.
Comment: I'd strongly suggest you're reading [this section of the official documentation](http://doc.qt.io/qt-5/signalsandslots.html). I explains really well the basic concept of Signals & Slots.
Comment: Answering to your question - connect to different slot, which does the job. With Qt5 and c++11 you can use lambdas.
Comment: You can create new slot which will handle your conditions and connect it with this signal.
Answer: You need to create new slot for that purpose.
But in C++ 11 and Qt 5 style you can use labmdas! It is very comfortable for such short functions.
In your case:
connect(ui->horizontalSlider, &QSlider::sliderMoved, this, [this](int x) {
this->ui->progressBar->setValue(x / 2);
});


Eigenvalues of the vectors of

Question: I came across the following problem:
"Let $\mathbf a\in\mathbf{R}^n$ be a fixed $n$-component real, non-zero, vector. Let $A^+$ and $A^−$ be real $n\times n$ matrices with components:
$$(A^\pm)_{ij} = \delta_{ij} \pm a_ia_j $$
Obtain the eigenvalues of $A^\pm$ and describe the corresponding eigenvectors."
I've tried the general method for eigenvalues using the determinant, however, I got nowhere.
Any help/hints would be appreciated.
Thanks.
Edit:
Are the eigenvectors a, given that:
$$Aa=a+aa^Ta$$
$$Aa=a(I+a^Ta)$$
Hence the eigenvalues $(1+a^Ta)$
Also vectors orthogonal to a will be eigenvectors with eigenvalue 1.
Is this correct?
Comment: Hint, the matrices $A^\pm$ rewrite $A^\pm=I\pm\mathbf a\mathbf a^T$.
Comment: Write out $A^\pm v$ for $v\in \mathbb R^n$ in terms of $a$.
Comment: @V.Rossetto It's not $I$, it's $[\delta_{ij}]$
Comment: @Bilou06 Usually $\delta_{ij}=1$ if $i=j$ and $\delta_{ij}=0$ if $i\ne j$. Is your case different?
Comment: @V.Rossetto I haven't seen the notation for a while, but it reminds me something...
Comment: Hint using $A^+ - A^- = 2aa^T$, have a look to eigenvalues of $A^+$ that are orthogonal with $a$
Answer: \begin{eqnarray*}
\mathbf{A}^{\pm } &=&\mathbf{U}\pm \mathbf{aa} \\
\mathbf{A}^{\pm }\cdot \mathbf{a} &=&\mathbf{a}\pm \mathbf{aa\cdot a}=(1\pm
\mathbf{a}^{2})\mathbf{a}
\end{eqnarray*}
so the eigenvalues are $(1\pm \mathbf{a}^{2})$.
Comment: What about vectors perpendicular to **a**? Are they not also eigenvectors?
Comment: Yes, you are right, they are eigenvectors at the eigenvalue 1

Eigenvalues of the vectors of


Como somar um ano específico com o ano atual em JavaScript?

Question: Quero criar uma função em javascript que some o ano 1993 + ano_atual e que insira o resultado dentro do meu

E este é o meu código.

Comment: Qual valor vc espera retorna?
Comment: 2019 - 1993 = 26 Porém esse valor precisa ser automático.
Comment: Existe outro problema `22/05/1993` `11/02/2019` retornar 26,sendo diferença de 25
Comment: Valor da diferença de ano vai ser exato ou aproximado?
Comment: Será aproximado
Comment: `document.getElementById("calculo_ano").innerHTML = "Há " . soma_ano . "anos com você";` Porque tem esses pontos? Isso não é php,mas sim javascript.
Answer: Olá.
Para acha diferença de segundos,anos,e qualquer coisa.
Você calcular vF - vI.
vF = Valor final.
vI = Valor inicial.
Solução:

Eu espero ter ajudado. Boa tarde!
Comment: Show!!! Muito obrigado!
Comment: Mas porque o resultado aparece assim "HÁ -26 ANOS COM VOCÊ"
Comment: E agora? Funcionou?


Fitting a function with R

Question: I want to fit a function to these data:
s <- c(20:60)
I <- c(0, 0.007515662, 0.015878514, 0.024994325, 0.034728341, 0.044910579, 0.055344590,
       0.065818599, 0.076118441, 0.086040566, 0.095403934, 0.104059903, 0.111898792,
       0.118853016, 0.124896650, 0.130041857, 0.134332925, 0.137838655, 0.140644404,
       0.142844161, 0.144533652, 0.145804788, 0.146741667, 0.147418108, 0.147896553,
       0.148228057, 0.148453067, 0.148602679, 0.148700131, 0.148762313, 0.148801181,
       0.148824980, 0.148839256, 0.148847645, 0.148852473, 0.148855195, 0.148856698,
       0.148857511, 0.148857941, 0.148858164, 0.148858276)

The plot is the following:

I tried using nls as follows:
mod <- nls(I~exp(a+b*s), start=list(a=0, b=0))

but I think I got a bad fit.
This is the fit I got by nls method with these initial parameters:
(RSS.p <- sum(residuals(mod)^2))  # Residual sum of squares
(TSS <- sum((I - mean(I))^2))  # Total sum of squares
1 - (RSS.p/TSS)  # R-squared measure
0.611088

I am interesting in finding an expression for a function with parameters, not only in a good graphical fit (because then I want to treat this function analytically).
Comment: @CrishaD Well, IMO, your assumption was not incorrect, you could achieve a similar form if you i.e. modify your original model to ``nls(I ~ c - exp(a+b*s), start=list(c = 0.15, a = 0.4, b = -0.1))``. It's not a perfect fit, you could try adding some additional parameters, though now with a negative sign before the exponent and a constant term it becomes similar to exponential CDF, so it has the property to converge to a constant value (``c`` actually), just as your data implies.
Comment: because it seemed to me like a "mirror" exponential.. But probably it is a wrong assumption
Comment: What are `I` & `s`? Is there any theoretical reason to think the functional form is of one type or another? (Also, it isn't clear to me that this is an R coding question.)
Comment: `f <- approxfun(s, I); plot(s, I); lines(s, f(s), col = 2)`
Comment: Thanks rawr! The thing is that I do not only a plot, but I want the parameters of this function (because then I want to treat it analytically). That's because I thought nls was a good method to use.
Comment: @CrishaD The residual standard error t is nothing more than the positive square root of the mean square error. It's **NOT** the R-squared.
Comment: Please see this for calculating R-squared: http://robinlovelace.net/2013/10/23/nls-demonstation.html
Comment: An ordinary nonlinear logistic fit should probably work pretty well
Answer: You could try adding a constant term c and a negative sign to your original equation, plus an additional squared term, as suggested by other answers:
mod <- nls(I ~ c - exp(a + b*s + d*s^2), start=list(c = 0.15, a = 0.4, b = -0.1, d = 0))

to obtain such fit:
Answer: I will solve this first with nls and then show even better solutions with glm. Finally I will come back to nls and show an even higher fit, but which may be overfit.
mod <- nls(I~exp(a+b*s+c*s^2), start=list(a=0, b=0, c=0))
summary(mod)

yhat <- predict(mod,I,type="response")
plot(I,col="green")
lines(yhat, col="blue")

(RSS.p <- sum(residuals(mod)^2))  # Residual sum of squares
(TSS <- sum((I - mean(I))^2))  # Total sum of squares
1 - (RSS.p/TSS)  # R-squared measure

0.910058

This GLM fits even better:
mod <- glm(I~s+I(s^2))

McFadden R-squared is about .981
Here's the plot:
predict <- predict(mod,as.data.frame(s),type="response")
plot(predict)

We can perhaps make it a little better:
mod <- glm(I~s+I(s^2)+log(s))

McFadden R-squared is about .990
The far end of the plot starts to level off a little more, as in the actual data this way:
predict <- predict(mod,as.data.frame(s),type="response")
plot(predict)

I used the package BaylorEdPsych to get McFadden's psuedo R-squared.
Finally, using nls this model had the highest fit of all, but it may be overfit:
mod <- nls(I~exp(a+b*s+c*s^2+d*I(1/s)), start=list(a=0, b=0, c=0, d=0))
summary(mod)

yhat <- predict(mod,I,type="response")
plot(I,col="green")
lines(yhat, col="blue")

(RSS.p <- sum(residuals(mod)^2))  # Residual sum of squares
(TSS <- sum((I - mean(I))^2))  # Total sum of squares
1 - (RSS.p/TSS)  # R-squared measure

0.9972944

Fitting a function with R


Authentication guard for login page redirection in angular 6?

Question:
Here In Angular when I on login page that time my login url is http://localhost:3000/login and now I change without login like this http://localhost:3000/base/dashboard (this is right url) so without login it not redirects on dashboard page it is login on login page, but when I put wrong url without login like this http://localhost:3000/defrtfg but it redirects on pageNotFoundComponent(see below screenshot) but I want to redirect on login page when I not logged in how it is possible ?
Authgaurd.guard.ts
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';
import { UserService } from './services/user.service';

@Injectable({
  providedIn: 'root'
})
export class AuthguardGuard implements CanActivate{
constructor (private router : Router) {}

canActivate(
  next: ActivatedRouteSnapshot,
  state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    if(localStorage.getItem('username')){
       return true;
    }else{
      this.router.navigate(['/login']);
      return false;
    }
  }
}

app-routing.module.ts
const routes: Routes = [
 { path: '', redirectTo: 'login', pathMatch: 'full' },
 { path: 'login', component : LoginFormComponent },
 { path : 'base', component : BaseComponent,canActivate : [AuthguardGuard],
 children: [
   { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
   { path: 'dashboard', component : DashboardComponent },
 ]},
  { path: '**', component : PageNotFoundComponent },
];

Comment: You should change this `{ path: '**', component : PageNotFoundComponent },` to this `{ path: '**', component : LoginFormComponent }, `
Comment: @AbdulBasit it is not possible other way to resolve this ?, because PageNotFountComponent  I used when user change manually wrong url
Comment: you can add two routes of `**` one with `canActivate` and one without it
Answer: Try this:
{ path: '**', component : PageNotFoundComponent, canActivate : [AuthguardGuard]}


MySQL TestContainers init-script Schema Creation Failing

Question: Running into the following issue trying to create a schema in a MySQL Test Containers instance. It seems like the test user doesn't have the appropriate permissions?
Config:
spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver
spring.datasource.url=jdbc:tc:mysql:5.7.22:///test?TC_INITSCRIPT=component/db/init_mysql.sql

Caused by: org.testcontainers.ext.ScriptUtils$UncategorizedScriptException: Failed to execute database script from resource [create schema new_schema CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;]
    at org.testcontainers.ext.ScriptUtils.executeDatabaseScript(ScriptUtils.java:375)
    at org.testcontainers.ext.ScriptUtils.executeDatabaseScript(ScriptUtils.java:313)
    at org.testcontainers.jdbc.ContainerDatabaseDriver.runInitScriptIfRequired(ContainerDatabaseDriver.java:196)
    at org.testcontainers.jdbc.ContainerDatabaseDriver.connect(ContainerDatabaseDriver.java:132)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136)
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369)
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198)
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467)
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541)
    ... 61 more
Caused by: org.testcontainers.ext.ScriptUtils$ScriptStatementFailedException: Script execution failed (component/db/init_mysql.sql:1): create schema new_schema CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
    at org.testcontainers.jdbc.JdbcDatabaseDelegate.execute(JdbcDatabaseDelegate.java:49)
    at org.testcontainers.delegate.AbstractDatabaseDelegate.execute(AbstractDatabaseDelegate.java:34)
    at org.testcontainers.ext.ScriptUtils.executeDatabaseScript(ScriptUtils.java:362)
    ... 69 more
Caused by: java.sql.SQLSyntaxErrorException: Access denied for user 'test'@'%' to database 'new_schema'
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:782)
    at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:666)
    at org.testcontainers.jdbc.JdbcDatabaseDelegate.execute(JdbcDatabaseDelegate.java:42)
    ... 71 more

component/db/init_mysql.sql
create schema new_schema CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Seeing similar issue when creating the mysql image outside the JDBC integration:
private static final MySQLContainer mysqlContainer = new MySQLContainer<>(DockerImageName.parse("mysql:5.7"))
            .withInitScript("component/db/init_mysql.sql")
            .withUsername("user")
            .withPassword("password")
            .withExposedPorts(3306)
            .withReuse(true);

Any ideas on how I can achieve this without creating a custom docker image with the schema pre-created?
Any help would be greatly appreciated I feel like I must be missing something here.
Comment: Also raised here https://github.com/testcontainers/testcontainers-java/issues/682#issuecomment-734625909
Answer: Try creating MySQL container and use same password for root as for test user, in this case whatever.
@Container
private static MySQLContainer database = new MySQLContainer(MYSQL_DOCKER_TEST_VERSION)
            .withUsername("test")
            .withPassword("whatever")
...

Add properties dynamically via @DynamicPropertySource.
    @DynamicPropertySource
    static void databaseProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.datasource.url", database::getJdbcUrl);
        registry.add("spring.datasource.username", database::getUsername);
        registry.add("spring.datasource.password", database::getPassword);
        registry.add("flyway.user", () -> "root");
        registry.add("flyway.password", database::getPassword);
    }

I am not using flyway but I believe it should work.
Answer: As seen in the test created on testcontainers repository, the workaround is to specify the password for the root user and to use the root user in your test:
MySQLContainer<?> db = new MySQLContainer<>(image)
            .withUsername("test")
            .withPassword("test")
            .withEnv("MYSQL_ROOT_PASSWORD", "test")
            ...

Reference: https://github.com/testcontainers/testcontainers-java/commit/2fa00ddbd1f42a20656d5e39d58cdedfbd483dcf#diff-f49cf0d09246a5351504ca32a08deb74e80ac7d61ad012f02d8f09c36508737bR65
Answer: I had the same issue, and I solved it using the following:
    @Container
     public static JdbcDatabaseContainer mariaDB = (JdbcDatabaseContainer) new MariaDBContainer("mariadb:10.5.11-focal")
    .withDatabaseName("dbName")
    .withInitScript("db.migration/init.sql")
    .withUsername("your user")
    .withPassword("your password")
    .withUrlParam("param", "1")
    .withUrlParam("param", "2");

Basically, I'm creating the DB via the POJO. A bit hackish but works. But maybe the option of creating the root user with custom password is a better solution.
Answer: Worked around this by creating a custom MySQL image with the schemas pre created. Would be keen to hear if there is a better way though.
Comment: Hardly a bad way since pre created image makes container startup time shorter.
Comment: This is a perfectly valid way to solve the problem, even in some docs it is described as a good way to cut times when executing integration testing


Apps using firebase crash after 3-4 days of installing them on a device/simulator (iOS)

Question: I am somewhat new to developing apps for iOS and I have been creating a few demo projects in Xcode. My problem is the following:
I have been following a few tutorials from Udemy.com and I created an app using firebase, the app works fine for about 3-4 days of use. But after more time passes by, the application does not launch on my device or the simulator. I thought I must have done something wrong when I did the tutorials, so I did them again but the same thing happens after 3-4 days of installing the app. They immediately close after I try to launch them. However, they do work if I rebuild the app on my phone or on the simulator. I do not seem to understand why this happens. has anyone else come across this problem? Or is this a normal thing that happens until you upload the app via the app store? Thank you guys, hope someone here knows. Cheers!
Comment: Can u post ur crashlog on Output screen
Comment: Have u got app store developer account?
Comment: I have only a free developer account. Can this be a reason as to why apps just refuse to run on a device / simulator after a period of time?
Answer: in such case you can check your GoogleService-Info.plist file or get idea about what's wrong by check the App terminate reason printed in your output Console.


Explanation of the Groebner Basis of a pair of polynomials?

Question: I was working a problem in which I needed to reduce two polynomials in variables c,p,t into a single polynomial in c,t.  Note that t appears only once, at the end of the $q_2$ definition.
$$q_1\equiv 1 + 2 c^2 + 3 c^3 + 3 c^4 +
 3 c^5 + c^6 + (2 c + c^2 + 2 c^3 + c^4) p + (c + 5 c^2 + 6 c^3 +
    12 c^4 + 6 c^5) p^2 + (1 + 4 c^2 + 4 c^3) p^3 + (4 c + 3 c^2 +
    18 c^3 + 15 c^4) p^4 + (2 c + 6 c^2) p^5 + (1 + 12 c^2 +
    20 c^3) p^6 + 4 c p^7 + (3 c + 15 c^2) p^8 + p^9 + 6 c p^{10} + p^{12}$$
$$q_2\equiv 16 + 32 c^2 + 48 c^3 + 48 c^4 + 48 c^5 +
 16 c^6 + (16 c + 16 c^2 + 16 c^3) p + (16 c + 48 c^2 + 80 c^3 +
    160 c^4 + 80 c^5) p^2 +  32 c^2 p^3 + (16 c + 32 c^2 + 192 c^3 + 160 c^4) p^4 +  16 c p^5 + (96 c^2 + 160 c^3) p^6 + (16 c + 80 c^2) p^8 + 16 c p^{10} -
  t$$
A user on the Mathematica SE gave the following magical line of code that returns exactly what I needed:
<code>GroebnerBasis[{q1, q2}, {p, c}][[1]]
</code>
returns:
$$4096 + 8192 c^2 + 12288 c^3 + 12288 c^4 + 12288 c^5 + 4096 c^6 -  768 t - 256 c^2 t + 256 c^3 t + 256 c^4 t + 48 t^2 - 16 c^2 t^2 - t^3$$
Perfect!  And I've verified that it's correct. But I have no idea how this result was calculated, and the online explanations I read about the Groebner Basis were a bit over my head because I've never studied Ring Theory.
Can anyone explain to me how this result might have been calculated?  I don't need the specifics of how Mathematica does it, just what it is an how one can calculate it.  How does one eliminate p from a pair of polynomials like this?  What is the Groebner Basis as the concept relates to a pair of polynomials?  Can this be explained without Ring Theory?
Answer: In Linear Algebra the common way to directly solve a system of linear equations is by row reduction of the augmented matrix representing the system of equations, often called Gaussian Elimination.  It transforms the augmented matrix into a form that represents and equivalent system of equations but with maximal elimination of variables. The creation of a Groebner basis for a set of polynomials is an analogous process. It creates the 'smallest' set of polynomials that have the same set of zeros as the original set of polynomials.  That is, the set of common roots of the elements of the Groebner basis is the same as the set of common roots (zeroes) of the original set of polynomials.  The polynomials in the Groebner basis are usually much simpler than the original set.  This is the best I can do without going into a little commutative ring theory.  I hope it helps.
Comment: If your interested in exploring further, the book "Topics in Commutative Ring Theory" by John Watkins has an introductory treatment of this subject.


Serving port 443 over http creates 400 Bad Request Error instead of redirect

Question: So for posterity sake, I am trying to configure my server so that even when someone tries to go to go to http:// domain.com:443, they would be correctly redirected to the https version of the site (https:// domain.com).
When testing something like http:// domain.com:443, it does not redirect correctly to https:// domain.com, I instead get hit with a 400 Bad Request page with the following content:

Bad Request
Your browser sent a request that this server could not understand.
  Reason: You're speaking plain HTTP to an SSL-enabled server port.
  Instead use the HTTPS scheme to access this URL, please.
Apache/2.4.18  (Ubuntu) Server at sub.domain.com Port 443

I tried including the following lines in my 000-default.conf in the <VirtualHost *:80>:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]

But it didn't work.
This issue occurs on all domains, subdomains and the server IP itself.
Possibly related, trying to do a dry run of letsencrypt returns the following:
   Domain: domain.com
   Type:   connection
   Detail: Failed to connect to 184.108.40.206:443 for TLS-SNI-01
   challenge

For each and every domain listed in the sites-enabled folder.
Answer: TL;TR: you cannot serve both HTTP and HTTPS on the same port (443).
While it would be in theory possible to figure out based on the first data from the client if the client is sending a HTTP request (i.e. GET .. or similar) or is starting a TLS handshake (\x16\x03...) most web servers don't do this. Instead they expect the client to behave properly, that is use plain HTTP on one port (usually 80) and HTTPS on another port (usually 443).
Your URL of http://example.com:443 is causing the browser to make a plain HTTP request to port 443. But the server is expecting TLS there which means that your plain HTTP request is unexpected. Apache is at least nice enough to check if the incoming data for a plain HTTP request in this case so that it can offer you a more useful description:

Reason: You're speaking plain HTTP to an SSL-enabled server port. Instead use the HTTPS scheme to access this URL, please.

If you try such requests with other servers then they would either close the connection without any error at all or just hang because they are still hoping to get a TLS handshake from the client.
Comment: Thanks for the information. Though- I understand the difference, I'm not trying to server :443 over http, I'm trying to force a redirect when the user tries to invoke 443 on a http url.

EDIT: The reason I am even trying to do this is because I've seen other web hosts handle these weird connections properly.
Comment: nginx also returns a 400 Bad Request error, stating: "The plain HTTP request was sent to HTTPS port"
Comment: Is there any way to maybe even throw a 301 up instead of the 400 error? I know it's probably a bit hacky... But I still want to mitigate the issue.
Comment: @Bitz Why do you want to do this?!
Comment: Because it seems pretty standard. Most websites I tested this on worked fine and I want to be able to measure to up that, I guess, haha.
(And I'm sure those who come across this page in a year or two would want an answer)
Comment: @Bitz: " I'm not trying to server :443 over http, I'm trying to force a redirect when the user tries to invoke 443 on a http url. " - A redirect is done with HTTP. Sou you want to serve your own HTTP response if the client accesses HTTP on the HTTPS port and that's exactly what the server will not allow you.
Comment: @Bitz: "..Because it seems pretty standard. Most websites I tested this on worked fine..":  I don't know what you've tried but: google.com and facebook.com just close the connection (don't try with a browser, because of HSTS they will only use https for these sites), arstechnica.com and baidu.com give error 400, microsoft.com and outlook.com hang and yandex.com interestingly returns a 200 response. None of these returns a 301. Thus 301 does not look like a standard behavior at all for me.
Comment: @Bitz this is something nginx will let you do, see
https://serverfault.com/questions/338700/redirect-http-example-com12345-to-https-example-com12345-in-nginx (they defined an internal response code 497 you can catch to redirect the user)
I arrived here trying to know if Apache can do the same, the answer seems to be no according to your exchanges, even though it knows it's this situation.
Answer: I had this same error and the culprit was that my config was trying to enable SSL on my non-ssl port. Any website address that starts with http:// will be funneled through port 80 (<VirtualHost *:80>) and that host doesn't want to know anything about SSL. Any sites that start with https:// will be funneled through port 443 (<VirtualHost *:443>)
The more direct issue was that I had the following SSL details under my *:80 host, which was making Apache try to enable SSL for a non-SSL connection.
SSLEngine on
SSLCertificateFile /bla.crt
SSLCertificateKeyFile /bla.key
SSLCertificateChainFile /bla.crt

After removing those lines from *:80 (and making sure they were present under <VirtualHost *:443>) My server popped back up again.
So, here are my <VirtualHost>s (all in /etc/apache2/sites-available/http.conf but might be different depending on your setup):
<VirtualHost *:80>

    -- snip --

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

</VirtualHost>

<VirtualHost *:443>

    -- snip --

    SSLEngine on
    SSLCertificateFile /var/www/ssl/STAR_iconcierge_net_au.crt
    SSLCertificateKeyFile /var/www/ssl/STAR_iconcierge_net_au.key
    SSLCertificateChainFile /var/www/ssl/STAR_iconcierge_net_au.crt

</VirtualHost>

Notice how *:80 (http) enacts a rewrite to *:443 (https), which then enforces SSL with the SSLEngine on directive.
Comment: Removing the first 4 lines worked for me.
Answer: What you are actually looking after is sending HSTS headers from the webserver back to the users, which tell the browser always to access the site using TLS. There isn't any 301 redirect happening there, as you can see from the development tools in any browser.
This happens with Google for example. Once you have visited Google the first time, it sent you a HSTS header, and your browser saved the header to a list. After, when you try to connect to google.com with http://, the browser changes the connection method automatically to https:// without any communication to the remote webserver.
This is one further security mechanism to prevent attacks on the security layer.
Answer: I had the same problem and I strugle around the world try to solve it and finally I could make my problem solved by using this way:
<VirtualHost *:443>
    ErrorDocument 400 "https://example.com/?error-bad-request"
</VirtualHost>

This will redirect your request http://example.com:443 to https://example.com/?error-bad-request
Hope this works with you.
Answer: Nothing that is inside the <VirtualHost *:80> section will be processed when you access the site using the port 443.
Test enabling the ssl version of the default site that come with apache2 and try that conf there inside <VirtualHost *:443>
Comment: I tried dropping the Rewrite lines I mentioned before into the `default-ssl.conf` file and ensured the sl in _sites-enabled_ pointed to the conf.
Restarted apache and sadly, no changes.
Comment: And you said that worked with named vhosts but not with the default unnamed vhost? Could you show the result of `ls -la site-enabled`
Comment: I thought that it was working for domains, but it seems like I was wrong and I tested it incorrectly, so the issue where http:/*:443 traffic isn't being transferred to https:// is universal for apache.

Also, here are the results of the command ls -la sites-enabled: http://i.imgur.com/UeFsWOB.png
Answer: My http/apache configuration was
<VirtualHost *>

I changed it to
<VirtualHost *:80>

And the problem went away....
Answer: In my experience i just deleted code in apache2.conf
<IfModule mod_ssl.c>
#Listen 443
</IfModule>

and httpd-vhosts.conf first entry custom settings .
restarted and worked
Comment: This just the SSL listener at all.


How to make my normal account can execute `sudo`?

Question: I installed bare-bone FreeBSD, and added a new user. And added sudo package with command pkg_add -r sudo.
When I logged into the box with new user account, it cannot run sudo command. What procedure is required to do this?.
Answer: Adjust your sudo configuration (sample.sudoers) using visudo.
Perhaps you what something like this.
username    ALL=(ALL) ALL

Answer: In Linux you should add it to sudoers group. It should be similar in FreeBSD case


Does python threading module speed up the execution time?

Question: I have a database containing employee information. I have 4000 employees. Each employee has an unique identification number.
I try to fetch employee information for each employee from the database using a python script. For 1 employee, the execution time for fetching info is 1 seconds. For 4000 employees, it makes 4000 seconds (67 minutes, who would like to wait that long?).
The employee infos should be stored in a dictionary, in the following format:
infos = {"ID1": ["info for employee 1"], "ID2": ["info for employee 2"], ... }

I'm thinking of doing the following to reduce the execution time:

Get employee id numbers
Divide the id numbers into 10 groups
Start 10 threads simultaneously
Make each thread use 1 of 10 employee id groups and fetch those employees' info
from database into separate dictionaries
In the end, combine those 10 dictionaries

Is it possible? Would this method reduce the execution time 10 times?
Comment: It will be a lot faster to issue one query that returns all employees, and then just discard any rows you don't care about.
Comment: 76 minutes is an incredibly long time.  Are you connecting to the database 4000 times, or do you connect just once?
Comment: I connect just once, but if I start 10 threads, I think I will have to use 10 separate connectors, right?
Comment: Can  you not just load the entire Employee data in a pandas dataframe in one go ? And then do the remainder of the processing in Python ?
Comment: Excuse me, what is pandas? The employee info is in a MySQL database.
Comment: Are you _sure_ you're not connecting 4000 times?  If you have a module or function that connects to the database, queries one employee ID, and then disconnects, and you call that module 4000 times...
Comment: @JohnGordon no, I use `MySQLdb` module, it has a `Connection` class. I create this object once in the beginning, and then pass SQL queries to it. It uses these queries to fetch employee infos. It stays connected all the time, ie it doesn't get disconnected after a query.
Comment: @JohnGordon hmm, interesting. I will give it a try. But I'm disappointed with `threading` module
Comment: @JohnGordon you were right, I fetched whole data from database to an array first, then discard the items I don't want. Execution time is reduced to 10 seconds. Thanks.
Answer: I think you're confusing threading with concurrency.
Threading is the act of simply using multiple threads of execution at the same time. This doesn't mean multiple actions are done simultaneously though... your processor still has to switch between the threads. This technique is useful when you're expecting to wait a long time for an operation to complete (like reading a large file) and you want other stuff to happen in the meantime (printing a progress bar, for example).
Concurrency is when you create multiple threads, and different threads get assigned to different cores in the processor. This is, of course, dependent on the hardware (you need a multicore processor to accomplish it).
Python's threading module allows for multiple threads to be created, sure; and in an ideal world it would automatically handle concurrency. But the default Python interpreter, CPython, does not allow for native concurrency due to the "Global Interpreter Lock". (See this page just above the section 17.1.3 heading and this SO post for more information about GIL.) In fact, using threading like you suggest may even make your execution slower (see here).
I've not done concurrency in Python, but I would advise you to check out the multiprocessing module as a possible solution.
Comment: It depends where the slowdown is happening.  If the database can only respond to one query per second, then even if you were able to _send_ the queries more quickly, it wouldn't help.
Comment: So you're saying it is not possible on a Windows 7 machine using Intel Core I5 CPU? Each thread will fetch info into a separate dictionary (ie they won't use the same memory location), and will use separate database connector objects, is it still not possible?
Comment: @alwbtc `threading` in the default Python interpreter will not allow for concurrent execution. The OS doesn't matter, and the number of cores available does not matter. Look into `multiprocessing` or a different implementation of Python other than CPython if you desperately want concurrent execution, but I think there's something else wrong in your code if that query takes over an hour to complete.


Combining two columns to single Column

Question: Having a data frame as below:
df1 = pd.DataFrame({'Name':['Raju', 'Divya', 'Geeta', 'Sita', 'Raju','Sita','Ramu'],
                    'Marks1':[80, 90, 'Nan', 88, 'Nan',40,'Nan'],'Marks2':['Nan','Nan', 30, 88,23,40,'Nan']})
df1

Need to combine/merge marks1 and marks2 and need to print output in the following format:
'Name':['Raju', 'Divya', 'Geeta', 'Sita', 'Raju','Sita','Ramu']

'Marks1':[80, 90,30, 88,23,40,'Nan']

Comment: Does any of the answers answer your question? Please mark as is :D
Answer: Try out with np.where
df1['Marks'] = np.where(df1.Marks1.eq('Nan'), df1.Marks2, df1.Marks1)

df1
Out[134]:
    Name Marks1 Marks2 Marks
0   Raju     80    Nan    80
1  Divya     90    Nan    90
2  Geeta    Nan     30    30
3   Sita     88     88    88
4   Raju    Nan     23    23
5   Sita     40     40    40
6   Ramu    Nan    Nan   Nan

Answer: You can set Names as index, bfill, slice and reset_index:
df1 = (df1.set_index('Name')
          .replace('Nan', pd.NA)
          .bfill(axis=1)['Marks1']
          .reset_index()
       )

Alternatively (and assuming string Nan), use boolean indexing and pop:
df1.loc[df1['Marks1'].eq('Nan'), 'Marks1'] = df1.pop('Marks2')
# if real NaN
# df1.loc[df1['Marks1'].isna(), 'Marks1'] = df1.pop('Marks2')

output:
    Name Marks1
0   Raju     80
1  Divya     90
2  Geeta     30
3   Sita     88
4   Raju     23
5   Sita     40
6   Ramu   <NA>

Answer: You can use .loc[] for this:
df1.loc[df1['Marks1'] == 'Nan', 'Marks1'] = df1.pop('Marks2')

Now for the output:
print("'Name':", df1.Name.to_list())
print("'Marks1':", df1.Marks1.to_list())

Output:
'Name': ['Raju', 'Divya', 'Geeta', 'Sita', 'Raju', 'Sita', 'Ramu']
'Marks1': [80, 90, 30, 88, 23, 40, 'Nan']

Combining two columns to single Column


How do I create a route that is specific for a subdomain?

Question: I'm looking to create a "subdomain" path to my custom controller running on a Drupal 8 site, with routing and hopefully without the need for an additional module.
The idea is to for example have a route like account.example.com or dev.example.com point to a custom controller I've made in my module. Or a path like api.example.com for external REST queries.
All the documentation describe just the traditional example.com/path methods, and googling resulted to nothing, but the domain module, which seems excessive and maybe for a different purpose even.
What I wish to achieve is not a separate installation sharing the content, but just a path to my controller that is domain-specific, like the i18n module is able to provide the language parameter as subdomain or path prefix.
Answer: If you want to set a controller for subdomain you just create a subdomain and handle it by DNS and web server, then set your subdomain path as root path for the subdomain on your web server, I mean both main domain and subdomain will be the same. As you said, you can create a route/controller for them (because the code and modules are the same, so routes will be available on both).
If you don't want the subdomains available on your main domain, on your handler check the domain name; if a request comes from the main domain, don't return the result for it. (Redirecting it to the homepage could be an idea.)
But the power of Symfony shows itself to us: Instead of manually handling the domain and subdomain filtering on the route, use host in the route definition.
   host: "dev.example.com"

Take a look at How to Match a Route Based on the Host for further help about using that directive.
Comment: I was hoping there'd be a more dynamic way of doing this also, without specifying actual subdomains with the DNS - possibly utilizing the method internationalization's url language detection uses, so in the future I could create subdomains dynamically for event's
Comment: You don't need to use static domains with `host`: See the example using {project_name}.example.com, which is not the same as project_name.example.com.
Comment: @AlariTruuts Drupal Couldn't handle localization subdomain by itself, you should  first create your subdomain on your  DNS Zone ( or in your apache or nginx config files).
Comment: @kiamlaluno yeah, he can use dynamic route, but it requires to specify his subdomains  on DNS before.
Comment: @zhilevan Apparently there are wildcard * domain names I can set so great, I guess I have my solution :)
Comment: @AlariTruuts yeah, good idea. ;)
Comment: This doesn't work, at least not anymore with 8.4.0 and later. (I'm speaking of the routing with host parameter)


How to set up AWS S3 credentials for PHP

Question: I'm new to S3 and am having a hard time setting up my credentials via a file. This is a working solution that I currently have using hard-coded credentials (IAM_KEY and IAM_SECRET are variables used to store my key):
// Using hard-coded credentials
    $s3 = new S3Client([
        'version'     => 'latest',
        'region'      => 'us-east-1',
        'credentials' => [
            'key'    => $IAM_KEY,
            'secret' => $IAM_SECRET ,
        ],
    ]);

However, I want to store the key and secret in a file so that it is not visible when I push my files onto GitHub. Here is my attempt that doesn't work:
// using credential file - not working :(
$s3 = S3Client::factory(
     array(
        'profile' => 'my_profile',
        'version' => 'latest',
        'region'  => 'us-east-1'
    ));

My credentials file is as following:
[my_profile]
aws_access_key_id = someKey
aws_secret_access_key = someKey

I've read the docs on AWS (https://docs.aws.amazon.com/cli/latest/userguide/cli-config-files.html) but am still stuck. It mentions that I have to store my credentials file in ~/.aws/credentials, however, I cannot find that directory (do I need to first install the command line tool for AWS?). Also, what is the extension of the credentials file?
I've been stuck on this for hours so any help will be appreciated.
Answer:
For AWS Profile(Creds file)
Yes you need to install AWS CLI tool and then use the following command to set the profile
aws configure
Visit this link for CLI configuration.
For using credentials configured in step 1 in your app, you should use Credential Provider from AWS SDK. Please read this link
My suggestion: read about IAM profiles -> here
Your app can obtain S3 permissions from IAM roles attached to AWS Resource whether its lambda or EC2
You should skip passing creds to the client or using Creds Provider.
Because when you create the S3 client in your code.
In Local Env: Once CLI is configured. S3 Client will automatically look for creds in multiple places like env vars or ~/.aws dir.
When Deployed: It will pick the permissions from IAM role attached to AWS resource where it is deployed either lambda or EC2.
Comment: I followed the instructions on installing the aws command line tool, and it installed successfully because I can see the files in /Users/jasonvl/Library/Python/3.6/bin/aws, however the aws --version command does not work. I tried adding export PATH=$Users/Library/Frameworks/Python.framework/Versions/3.5/bin:$PATH to my bash profile but still no luck. Thanks for your help though!
Answer: You can store your AWS credentials in a file which is ignored by way of a .gitignore file.
For example:
aws-credentials.php
<?php

    $IAM_KEY = 'key';
    $IAM_SECRET = 'secret';

?>

index.php
<?php

require_once('aws-credentials.php');

echo $IAM_KEY . "\n\n";
echo $IAM_SECRET . "\n\n";

?>

.gitignore
aws-credentials.php

Since the file "aws-credentials.php" is referenced in the .gitignore file, it won't be included when you commit your changes to git.
It's been a while since I've used PHP in a production environment but it would also be good practice to store the aws-credentials.php file outside of your web root, just to exclude the remote possibility of the file's contents being exposed in the event the web server is not configured to execute PHP files correctly.
If you're hosting the application on Amazon EC2, it is also good practice to not use IAM credentials at all, but instead use IAM roles.
Comment: It worked! Thank you. I spent hours trying to set up the command line tool for aws so that I can store my credentials in ~/aws but it didn't work. Your solution is a lot more simple.
Comment: I think the aws profile is much better approach


Domain driven design: How to deal with complex models with a lot of data fields?

Question: Well I am trying to apply domain driven design principles for my application, with a rich domain model that contains both data fields and business logic. I've read many DDD books, but it seems that their domain models(called entities) are very simple. It becomes a problem when I have a domain model with 10-15 data fields, such as the one below:
class Job extends DomainModel{

    protected int id;
    protected User employer;
    protected string position;
    protected string industry;
    protected string requirements;
    protected string responsibilities;
    protected string benefits;
    protected int vacancy;
    protected Money salary;
    protected DateTime datePosted;
    protected DateTime dateStarting;
    protected Interval duration;
    protected String status;
    protected float rating;

    //business logic below
}

As you see, this domain model contains a lot of data fields, and all of them are important and cannot be stripped away. I know that a good rich domain model should not contain setter methods, but rather pass its data to constructor, and mutate states using business logic. However, for the above domain model, I cannot pass everything to the constructor, as it will lead to 15+ parameters in constructor method. A method should not contain more than 6-7 parameters, dont you think?
So what can I do to deal with a domain model with a lot of data fields? Should I try to decompose it? If so, how? Or maybe, I should just use a Builder class or reflection to initialize its properties upon instantiation so I wont pollute the constructor with so many arguments? Can anyone give some advice? Thanks.
Answer: What you've missed is the concept of a Value Object. Value objects are small, immutable objects with meaning in the respective domain.
I don't know the specifics of your domain, but looking at your Job entity, there could be a value object JobDescription that looks like this:
class JobDescription {
    public JobDescription(string position, string requirements, string responsibilities) {
        Position = position;
        Requirements = requirements;
        Responsibilities = responsibilities;
    }

    public string Position {get;}
    public string Requirements {get;}
    public string Responsibilities {get;}
}

This is C# code, but I think the idea should be clear regardless of the language you are using.
The basic idea is to group values in a way that makes sense in the respective domain. This means of course that value objects can also contain other value objects.
You should also ensure that value objects are compared by value instead of by reference, e.g. by implementing IEquatable<T> in C#.
If you refactor your code with this approach, you will get fewer fields on your entity, so using constructor injection (which is highly recommended) becomes feasible again.

Further notes regarding your example code that are not directly connected to the question:

The domain model is the whole thing, an entity is part of it. So your base class should be called Entity and not DomainModel.
You should make the fields of your class private and provide protected accessors where required to maintain encapsulation.
Comment: I see, yeah I think I should start to implement more value objects, like the Money value object I already have. Thanks for the suggestion, I will start by composing data fields by smaller value objects, and use value objects in each entity. I do know protected fields sometimes break encapsulation, so I should switch to private in most cases, thanks too. I am not sure about the domain model vs entity thing though, to my understanding a domain model is an entity, or that an entity is a domain model with an identity, is this correct?
Comment: @LordYggdrasill A domain model is the whole set of "things" that have meaning in your domain, e.g. entities, value objects, etc. See for example [this article](https://en.wikipedia.org/wiki/Domain_model), the diagram shows a sample domain model that contains many classes.
Comment: I see, thanks. I understood this better after reading some articles about aggregate root.
Comment: Well sorry I am back with another question. I have decomposed the job entity into a few value objects: JobDescription, JobEnvironment, JobOrganization, etc. Now I am facing another issue. I think I should also put business logic to each of these value objects, but then there is a problem. The job entity by itself becomes quite anemic(most of business logic now delegated to the value objects inside it), and to call these methods become a bit more cumbersome. Instead of Job.countHoursOfWork(), I now need to call Job.getEnvironment().countHoursOfWork().
Comment: So is this the supposed behavior? The I need to call Job.getEnvironment().countHoursOfWork()? I think this makes the API a bit harder to follow. Maybe I should provide a method on Job too which delegates the call to JobEnvironment?
Answer: There's an awful lot going on in your Job domain model object - it seems to mix a huge number of concerns, and (to me at least) suggests a number of bounded contexts, some of which are easy to discern for the sake of making an example.

Remuneration (pay, benefits)
Organisational position (reporting line)
Person spec (skills)
Job specification (responsibilities)
etc.

When you consider the things that interact with your 'Job' model, are there any that need to inspect or mutate BOTH the Salary property and the Industry property, for example?
Without knowing the full nuances of the domain, the Salary you get for holding a position and the Industry you work in are not really connected, are they? Not a rhetorical point; these are the questions you NEED to ask the domain experts.
If they DON'T have any interaction then you have identified that these two things exist in two different BOUNDED CONTEXTS. The Salary side has no need of any interaction with the Industry side and vice versa, and even if they did, do they need to be held as state in the same process at the same time?
Think about the lifecycle of how a person becomes an employee; a person applies for a job. The job has a specification, salary range. The person attends an interview. The hirers offer the person the position. The person accepts. The person is now an employee, not a candidate any longer. The new employee is now accruing holiday and benefits and has a start date etc.
DDD teaches us that a single, unified view of the world rarely serves ANY of the concerns correctly. Please explore BOUNDED CONTEXTS - your software will be much more pliable and flexible as a result.
Comment: Yeah you have a point, in this way my Job domain model is more like an aggregate root, it may also contain other fields such as users who have applied to the job. Id definitely consider refactoring some fields into smaller domain objects(I think they will be, value objects? since they dont have identity outside of the Job aggregate root).


Configuring ip, subnet and gateway on eth interface

Question: I'm in terminal on RedHat 5 and I need to configure one of the two eth interface with different IP, GATEWAY and SUBNET.
I know ifconfig and route add but I'm not sure what I have to do for configureing the gateway on that particulary eth and, I think, only for that!
Thanks
Answer: route add default gw 220.127.116.11 eth0
Of course you need to replace 18.104.22.168 with the actual gateway.  This will add the route into the system routing table.


Is it possible to split the training DataLoader (and dataset) into training and validation datasets?

Question: The torchvision package provides easy access to commonly used datasets. You would use them like this:
trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                          shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root='./data', train=False,
                                       download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
                                         shuffle=False, num_workers=2)

Apparently, you can only switch between train=True and train=False. The docs explain:

train (bool, optional) – If True, creates dataset from training.pt,
  otherwise from test.pt.

But this goes against the common practice of having a three-way split. For serious work, I need another DataLoader with a validation set. Also, it would be nice to specify the split proportions myself. They don't say what percentage of the dataset is reserved for testing, maybe I would like to change that.
I assume that this is a conscious design decision. Everyone working on one of these datasets is supposed to use the same testset. That makes results comparable. But I still need to get a validation set out of the trainloader. Is it possible to split a DataLoader into two separate streams of data?
Answer: Meanwhile, I stumbled upon the method random_split. So, you don't split the DataLoader, but you split the Dataset:
torch.utils.data.random_split(dataset, lengths)


Inverse function -- need help

Question: I'm a senior software developer but my math lessons are a bit rusty. I know the name of what I want, but not anymore how to compute it ;)
I've found (by myself with Grapher.app) a simple easing function (for transitions), that's a bijection from $[0,1]$ to $[0,1]$:
$$ f(x) = \frac{27}{4} \cdot \left( \left( \frac{2x}3 \right) ^2 - \left( \frac{2x}3 \right) ^3 \right) $$
Edit: in fact, after simplifying, it's as simple as:
$$ f(x) = 3 x^2 - 2 x^3 $$
The derivatives at $0$ and $1$ are both zero, so it's a perfect ease-in-out function that's easy to compute.
In order to start my transitions from anywhere between $0$ and $1$, I need $f^{-1}$, the inverse of this function, but I'm completely lost as to where to start... Would someone shed some light on my oh-gosh-it-was-so-long-ago math course?
Comment: It's simpler and easier to understand if you write $f(x)=3x^2-2x^3$.
Comment: normal form: y=function of x,
inverse: x=function of y
Comment: Oh. My. Didn't even think of simplifying it. When I told you I'm rusty...
Answer: Take your equation $3 x^2 - 2 x^3 = y$, substitute $x = X + 1/2$, multiply by $2$ and expand to get
$-4X^3 + 3 X = 2 y - 1$.  Now note that $\sin(3t) = - 4 \sin(t)^3 + 3 \sin(t)$.  Thus
a solution is
$X = \sin(t)$ where $\sin(3t) = 2y-1$, i.e.
$$ x = \sin\left(\frac{\arcsin(2y-1)}{3}\right) + 1/2 $$
Note that $0 \le x \le 1$ when $0 \le y \le 1$.
Comment: What I've always wondered (and that's where the beauty of maths comes from) is: where did you take the "substitute x = X + 1/2" from? I mean, did you just have to say "shazam" to find this?
Comment: Wait wait wait... sinuses?
Comment: Okay, after re-reading it like ten times, it makes sense.
Comment: Take $x = X + a$ and see what $a$ has to be to eliminate the $X^2$ term.
Answer: The inverse function $g(y)$ satisfies $f[g(y)] = y$.  In this case:
$$\frac{27}{4} \cdot \left( \left( \frac{2g(y)}3 \right) ^2 - \left( \frac{2g(y)}3 \right) ^3 \right)  = y$$
Solve for $g(y)$ algebraically.  Might be tough because it's a cubic, but it is possible.
Answer: You can ask to wolfram alpha:
<code>1/2 (1+1/(1-2 x+2 sqrt(-x+x^2))^(1/3)+(1-2 x+2 sqrt(-x+x^2))^(1/3))
</code>
Or either you could implement the bisection method to solve $f(x) = y$. One should check, but it is possible that in this case the bisection method is faster than using the explicit formula for inverse.
Comment: Didn't even knew you could ask this to Wolfram. Thanks!
Comment: But notice that when $x \in (0,1)$, you're taking the square root of a negative number.  This is no accident: see http://en.wikipedia.org/wiki/Casus_irreducibilis

Inverse function -- need help


The sum of the square roots of $n$ distinct squarefree integers is never an integer. Elementary proof

Question: I'm trying to prove that, if $k_1, \cdots k_n$ are distinct squarefree integers than
$$
\sum_{i=1}^n \sqrt{k_i} \notin \mathbb{N}
$$
without using the theory of algebraic extensions nor Galois theory. the usual proof is the following:

The theorem is true for $n=2$. Assume it is true for $n-1$ squarefree integers, than we have that
$$
[ \mathbb{Q}(k_1, \cdots k_{n-1}) : \mathbb{Q} ]>1
$$
where $[K :L]$ is the degree of the field extension. Now assume
$$
\sum_{i=1}^n \sqrt{k_i} \in \mathbb{N}
$$
than
$$
1=[ \mathbb{Q}(k_1, \cdots ,k_{n-1},k_n) : \mathbb{Q} ]=[ \mathbb{Q}(k_1, \cdots ,k_{n-1},k_{n}) : \mathbb{Q}(k_1, \cdots ,k_{n-1} )] [ \mathbb{Q}(k_1, \cdots ,k_{n-1}) : \mathbb{Q} ]
$$
where I have used the tower lemma. This is absurd and so the theorem is proved by induction (to be more precise this proves the theorem for any set of integers that are not perfect squares)
EDIT
I will give some intuition of the proof that $$\mathbb{Q}(\sqrt{k_1}, \sum_{i=2}^{n} \sqrt{k_i})=\mathbb{Q}(\sum_{i=1}^{n} \sqrt{k_i})$$
To do this i will use the following particular case of the primitive element theorem:
If $a,b$ are algebraic over a field of characteristic $0$ than $F(a,b)=F(c)$ for some $c \in F(a,b)$. Moreover if $f$ is the minimal polynmial of $a$ than $c=a+db$ where $d \in F$ is such that $a+db$ is not a root of $f$ in its splitting field
As the minimal polynomial of $\sqrt{k_1}$ is $x^2-k_1$ we see that, if $\sum_{i=2}^n \sqrt{k_i} \ne \pm 2\sqrt{k_1}$ we can choose $d=1$
I will not give the full proof of this, but the core is to show that, if we call $g$ the minimal polynomial of $b$ and $h(x):= f(a+d(b-x))$ we have
$$
\gcd(g,h)=\gcd_{F(a+db)}(g,h)=x-b
$$
that implies that $b \in F(a+db)$ and so $F(a,b) \subseteq F(a+db)$

here and here there are other proofs that still uses Galois theory of at least field extensions
Is there any elementary proof that can be understood by people without knowledge in abstract algebra ?
The case $n=2$ can be proven by squaring the sum, but already in the case $n=3$ I don't know how to prove it without using field extensions
Update
As said in the comments if the integers are pairwise coprime the case $n=3$ can be proved by using only elementary methods indeed by squaring both members of the equation
$$
\sqrt{k_1}+\sqrt{k_2}=N-\sqrt{k_3}
$$
and regrouping all the integer terms we have
$$
2\sqrt{k_1k_2} +2N \sqrt{k_3}=N_2
$$
squaring again both members we arrive to $8N \sqrt{k_1k_2k_3}=N_3$ that is never satisfied for pairwise coprime squarefree integers
Comment: $2n\times 3n\times 6=(6n)^2$ is a perfect square of your type (take $n$ to be square free and prime to $6$).
Comment: pairwise coprime squarefree means the product is not a square;  let's see what your recent edit did.....  actually pairwise coprime and not squares  still says the product is not a square
Comment: FWIW I looked into this a long time back. There is a paper with an elementary (but very long) proof, but I don't know how to get to it. It that basically multiplies $( \sqrt{k_1} \pm \sqrt{k_2} \pm \ldots \pm \sqrt{k_n} )$, and makes an inductive argument that is similar to the field-theoretic approach.
Comment: Probably the most elementary way is that [here](https://math.stackexchange.com/a/136655/242) in the dupe. Be sure to read the linked introductions (for motivation and field theory language). The more general case (linear indepence) is also elementary (but wih a more complex induction).
Comment: @lulu thanks. It was only a necessary condition. Not sufficient
Comment: @WillJagy it removes a wrong characterization I gave. The pairwise above was a mistake. Should have been "distinct"
Comment: How do you justify your claim that the two conditions
$$
[ \mathbb{Q}(k_1, \cdots k_{n-1}) : \mathbb{Q} ] > 1\\
\sum_{i=1}^n \sqrt{k_i} \in \mathbb{N}
$$
together imply
$$
[ \mathbb{Q}(k_1, \cdots ,k_{n-1},k_n) : \mathbb{Q} ]=1
$$
??
Comment: @quasi $\mathbb{Q}(\sqrt{k_1}, \cdots, \sqrt{k_n})=\mathbb{Q}(\sum_i^n \sqrt{k_i})=\mathbb{Q}$ if $\sum_i^n \sqrt{k_i} \in \mathbb{N}$. The first one can be proven generalizing [this](https://math.stackexchange.com/questions/2446986/show-that-if-sqrta-sqrtb-neq-0-then-mathbbq-sqrta-sqrtb-m)
Comment: The link you refer to in your above comment only deals with the case $n=2$.
Comment: @quasi I added some details. As it is not the core of the question i didn't went in full details
Answer: Here are my notes from 10 years ago, which might have errors in them. I did not come up with this argument.
I know images aren't ideal, but I'm too lazy to type this up. Feel free to delete/edit/vote this solution as desired.
Note: Since 1 is (considered) a squarefree integer, OP's naive claim that $ \sqrt{ k_i } \not \in \mathbb{N}$ needs to be modified to remove $k_i = 1$.

(You can ignore the following note about how to use repeated squaring for $n \leq 4$. However, this doesn't extend to more variables.)

We're now ready to proof a slight variant of the theorem listed at the start, and is a slightly stronger statement than what OP asked for (as it allows for negative coefficients):

If $ \{ a_i \}$ are distinct numbers from the set $SF \backslash \{1 \}$, and $\{ b_i\}$ are any integers, then $ S = \sum b_i \sqrt{a_i } \in \mathbb{N}$ if and only if all $ b_i = 0 $ (and hence $ S = 0 $ ).

Motivation: The field theoretic idea here is that if $ S = \sum b_i \sqrt{a_i}$, then for any prime $ p \mid a_1$ replacing $\sqrt{p}$ with $ - \sqrt{p}$ throughout will still retain the validity of the expression. This implies that $ S = -b_1 \sqrt{a_i} + \sum \pm b_i \sqrt{ a_i } $ for a known set of $ \pm b_i$ where the signage depends only on whether $p \mid a_i $. Adding these two expressions, we get $ 2 S $ as the sum of fewer radical terms, so an induction approach could work.
We will merely show the weaker result that $ S = -b_1 \sqrt{a_1} + \sum_{i=2}^n \pm b_i\sqrt{a_i} $ for some suitable choice of signs, which is sufficient to add these expressions and reduce the number of terms.
The backwards part is obvious. We prove the forward part by induction.
Base case: If $ b_1 \sqrt{a_1} = S$ with $b_1 \neq 0$ then $b_1^2 a_1 = S^2$ and so $a_1 $ is a perfect square, which is not possible in $ SF \backslash \{ 1 \}$. Thus $b_1 = 0 $.
Induction step: Suppose it is true for $n-1$ summations.
Case 1: $ S = 0 $.
Multiply throughout by $\sqrt{a_i}$.
Since $a_i \neq a_1$, so $ a_i a_1 = k_i^2 a_i'$ where $a_i' \in SF \backslash \{ 1 \}$.
Thus $\sum_{i=2}^n (b_i k_i) \sqrt{a_i'} = - b_1 a_1 \in \mathbb{N}$.
Applying the induction hypothesis, we get $ b_i' = 0 \Rightarrow b_i = 0 $ for $i = 2$ to $n$, and  $  0 = S = b_1 \sqrt{a_1} \Rightarrow b_1 = 0 $.
Case 2: $ S \neq 0 $.
Set up $F_{L(\sqrt{a_1}, \sqrt{a_2}, \ldots \sqrt{a_n} )} (T) $ from the previous discussion, so $F_{L(\sqrt{a_1}, \sqrt{a_2}, \ldots \sqrt{a_n} )} (S) = 0  $, and
$$ 0 = \sqrt{a_1} P(a_1, a_2, \ldots a_n, S) + Q(a_1, a_2, \ldots , a_n, S).$$
The polynomials $P$ and $Q$ have integer coefficients, so when evaluated at the integers will give us integer values, thus $ 0 = \sqrt{a_1} P^* + Q^*$ and hence these values are also 0. Thus, $ 0 = -\sqrt{a_1} P^* + S^*$.
Now, consider
$$ \begin{array} { r l }
G_L(T) & = \prod [ T + L'(x_1, x_2, \ldots, x_n) ] \\
& = \prod [ T - L'(-x_1, x_2, \ldots, x_n) ] \\
& = -x_1 P(x_1^2, x_2^2, \ldots , x_n^2, T) + Q(x_1^2, x_2^2, \ldots , x_n^2, T). \end{array}  $$
Since $ 0 = - \sqrt{a_1} P^* + Q^* = \prod(S + L')$, so there exists some $S+L^* = 0$.
We have $S = -b_1 \sqrt{a_1} + \sum_{i=2}^{n} c_i \sqrt{a_i}$ where $ c_i \in \{ b_i, -b_i \}$.
Adding up the 2 expressions for $S$, we get that $2S = \sum_{i=2}^n (b_i + c_i) \sqrt{a_i}$ and hence by the induction hypothesis $2S = 0$.
This is a contradiction, so this case cannot occur.

Original version of the final part.
Comment: I completly understand why you don't want to type all this up. If these notes are online a good compromise could be to add the reference, but for me they are fine.
Comment: @Marco **Beware** The argument can't possibly be correct as written since it does not use the  (necessary) hypothesis that the radicands are squarefree. (else e.g. $\, \sqrt 8 - 2\sqrt 2  = 0).\,$ Calvin: please give a *complete, correct* proof (in particular show how to finish the proof).
Comment: @BillDubuque 1/ For clarity, I added in the statement of the theorem, which requires the $a_i$ to be distinct to avoid trivial cancellations. This wasn't made obvious in the writeup.           2/ The case of $S= 0 $ needs to be dealt with, and was glossed over in the writeup. I've since added details. The requirement of $a_i$ being distinct is used in $a_1 a_i \neq k_i^2$.   $\quad$ I believe that this works now, though let me know if you have any concerns.
Comment: I overlooked it. If it can help the mistake is to suppose $M \ne 0$ this surely happen if the radicands are distinct squarefree (in the linked dupe there is the link to the proof of llinearly independence of square-roots), but of course it need to be proven.
Answer: Distinct squarefree integers: $a_n$ with n=1..k, now first try:
$\sqrt{a_{1}}+\sqrt{a_{2}}=c$ with $c$ an integer at will.
$\sqrt{a_{2}}=-\sqrt{a_{1}}+c$ then square it:
$a_{2}=-2\sqrt{a_{1}}c+a_{1}+c^2$ -- since $a_1$ is a distinct squarefree integer this is not possible.
Again:
$\sqrt{a_{1}}+\sqrt{a_{2}}+\sqrt{a_{3}}=c$
$\sqrt{a_{2}}+\sqrt{a_{3}}=-\sqrt{a_{1}}+c$
$2\sqrt{a_{3}}\sqrt{a_{2}}+a_{2}+a_{3}=-2\sqrt{a_{1}}c+a_{1}+c^2$ and same on RHS as above -- not possible.
Continue ad libitum.
Comment: Unfortunately, repeated squaring will only work for up to 5 radicals with distinct square-free parts. Past 6, repeated squaring introduces even more radicals, that you cannot fully get rid of.
Comment: You can't continue. If you have $4$ square roots you will end up, after squaring with
$$a_1+a_2+a_3 +2\sqrt{a_1a_2} +2\sqrt{a_1a_3}+2\sqrt{a_2a_3}=a_4+c^2 -2 \sqrt{a_4}$$
Similarly if you  calculate
$$(\sqrt{a_1}+\sqrt{a_2})^2=(c+\sqrt{a_3}+\sqrt{a_4})^2$$ so still $4$ radicals
Comment: As you **can not** get rid of the roots the equation is invalid with integers only. For me it's evident enough to proof _The sum of the square roots of n distinct squarefree integers is never an integer._
Comment: @m-stgt  What you are doing is assuming that a linear combination (over the integers) of radicals is never an integer to prove that the sum of radicals is never an integer. We can't do that
Comment: @Marco -- _We can't do that?_ Then pls explain what's wrong with this contradiction: $a_{2}=-2\sqrt{a_{1}}c+a_{1}+c^2$ Maybe I overlook something, but even the sum of those equations will **with integers** never be possible. Sure, my "insight" is no evidence, fair enough.
Comment: You can do it with a single radical, but you can't do it if there is more than one radical. We don't know if $$2(\sqrt{a_1a_2}+\sqrt{a_1a_3}+\sqrt{a_2a_3}+\sqrt{a_4})$$ is an integer or not. That is basically what we are trying to prove *i.e.* that the sum of radicals is an integer iif the radicals theirself are rational