Dúvidas sobre as classes Graphics e Graphics2D em Java

Question: Ando tendo muitas dúvidas em relação às bibliotecas gráficas de Java, que são as seguintes:

Qual a diferença entre sobrescrever o método paint(Graphics) ou o método paintComponent(Graphics)?
O que a classe Graphics2D traz de diferença em relação a classe Graphics?
Comment: Acho que são muitas duvidas para uma pergunta só. Recomendo que deixei apenas a 2 e a 3 que estão diretamente relacionadas.
Comment: Em resumo, [**swing é uma evoluçao do awt**](https://pt.stackoverflow.com/q/176271/28595) mas ele acaba por herdar muitas coisas do awt, como o método paint, que é o responsavel pelo desenho dos componentes no awt, mas no swing, a classe mae de todos os componentes foi reescrita(Component em awt e JComponent em swing), e foi deletado a paintcomponent o desenho da tela. Como toda classe na API herda de JComponent, e este método é o coracáo dela, acaba que ele é a melhor opcao para reescrita quando precisamos redesenhar um componente.
Comment: A segunda pergunta é parcialmente respondida no comentario anterior, somando-se ao fato que Graphics2D é uma evolucao de Graphics, preparada para fornecer melhores ferramentas para desenhos geometricos e formas, cores e outros detalhes na construcao de elementos. Ela fornece um conjunto de possibilidades maiores para desenho do que a Graphics.
Comment: @Articuno Então, ao utilizar elementos swing (como um JPanel por exemplo) é recomendado o uso do paintComponent?
Comment: Sim, porque tudo que tem J na frente é da API swing, e herda de JComponent, que é a classe que serve como uma especie de base pra todas classes de componentes em swing.  Se nao me engano, a propria classe `paint` passou a chamar a `paintComponent`, entao faz mais sentido sobrescrever a ultima para nao quebrar o desenho geral do que se esta fazendo.
Comment: Não sei se minha explicacao ficou boa, por isso nao respondi, mas se souber ingles, esse link é sua resposta completa para a questao 1: https://docs.oracle.com/javase/tutorial/uiswing/painting/closer.html Esse outro responde sua pergunta 2: https://docs.oracle.com/javase/tutorial/2d/overview/index.html
Comment: Entendi sua explicação, muito obrigado :D
Answer:
O método paint(Graphics) em componentes lightweight (esses do Swing) realiza essas três coisas, nessa ordem:

Pinta o conteúdo do componente propriamente dito ao chamar o método paintComponent(Graphics).
Pinta os subcomponentes ao chamar o método paintChildren(Graphics).
Pinta a borda do componente ao chamar o método paintBorder(Graphics).

O método paint(Graphics) também faz algumas coisas a mais para determinar qual que é a área que deverá ser pintada. Ele também lida com o caso de você estiver imprimindo o componente (para a impressora), chamando então os métodos print*(Graphics) ao invés de paint*(Graphics). Veja isso tudo no código-fonte correspondente.
Como você dificilmente vai querer alterar esse funcionamento padrão do paint(Graphics) (há pouca razão para isso), dificilmente faria sentido sobreescrever-se tal método. Assim sendo, sobreescreva o método paintComponent(Graphics) que é quem faz o desenho do componente de fato.
A classe Graphics2D é uma subclasse de Graphics. Ela tem diversos métodos a mais, basta dar uma olhada rápida no javadoc dela para perceber isso. O AWT sempre vai passar uma instância de Graphics2D para o seu método paintComponent(Graphics), portanto é seguro fazer-se o cast. Se não me engano, a razão para isso é histórica, e o ideal seria que os métodos a mais tivessem sido acrescentados na classe Graphics ao invés de em uma subclasse.

Veja mais sobre isso no tutorial da Oracle, embora a informação lá seja apresentada de forma desorganizada.
Também recomendo olhar o meu TCC da especialização que terminei em 2007, onde abordei esse assunto.


How to change drum pads pressure sensitivity in Novation Impulse 61 Midi Keyboard

Question: I bough new Novation Impulse 61 Midi keyboard. I am unable to find a settings to change drum pads pressure sensitivity on it. I did once but forgot how. Can anyone help me to find the settings on it?
Comment: The users manuals can be found here (https://customer.novationmusic.com/en/support/downloads?brand=Novation&product_by_type=488&download_type=user-guides), but I don't find information about how to do what you're asking. Possibly it's a setting at the signal receiving end?
Comment: In that case, I would check at the receiving end. Perhaps there are signal threshold settings there.
Comment: I have already check in doc but couldn't find anything explicit for it.
Answer: Not really an answer, but a bit long for a comment.

If your keyboard is MIDI, changing the signal at the receiver end as @Aaron suggested is always possible, and likely the best you can do if you are not satisfied with the velocity curve. However, keep in mind that doing so will reduce the velocity precision: velocity in MIDI is encoded on values between 0 and 127, and the drumkit is expecting values in this range. In a mathematical point of view, you cannot map 0-127 to 0-127 with a monotonic function and covering all the values if you are not using the identity function.
As a naive example, if you want the pad to be more sensible, you could think you multiplying the value given by the pad by 2. If you do so:

the receiver part will never receive any even values, you will jump from velocity 0 to velocity 2 without any steps in between,
all values above 64 will be mapped to 127 thus losing any kind of variations in the high velo range.

This is perhaps what you want, but in my opinion it is always a good thing to remember this when mapping MIDI values ;).
Edit:
After looking at the manual, end of page 19, you can change the velocity curve of the pads. I retain the original answer in case you want to do it on the receiver end.


Solaris/OpenSolaris FMA and memory false positives

Question: We have +500 servers built with Supermicro motherboards and Kingston memory and we usually see the following alerts:
<code># fmdump  -v
TIME                 UUID                                 SUNW-MSG-ID
Oct 27 15:49:44.9379 108510ec-b4e1-c94b-dd9f-f7b2969a4725 INTEL-8001-94
  100%  fault.memory.intel.dimm_ce

        Problem in: hc://:product-id=X7DB8:chassis-id=0123456789:server-id=hostname:serial=180104092839051c6a:part=KINGSTON:revision=C1/motherboard=0/memory-controller=1/dram-channel=3/dimm=0/rank=1
           Affects: hc://:product-id=X7DB8:chassis-id=0123456789:server-id=hostname:serial=180104092839051c6a:part=KINGSTON:revision=C1/motherboard=0/memory-controller=1/dram-channel=3/dimm=0/rank=1
               FRU: hc://:product-id=X7DB8:chassis-id=0123456789:server-id=hostname:serial=180104092839051c6a:part=KINGSTON:revision=C1/motherboard=0/memory-controller=1/dram-channel=3/dimm=0
          Location: DIMM4A
</code>
My question is: how trustworthy are these faults when you are running on non-Oracle hardware?
We tried almost everything (short of never using these components again) but the faults randomly come back (eg. replace dimm4a and after a few months dimm1b has a fault, replace all memories and motherboard and another fault shows up after a few days).
The memory we replace is tested for days with memtest and we can never find a problem. Other teams using the same hardware with Windows & Linux don't see it. Is Solaris being too sensitive?
Right now we are going over another round of memory replacements but it's becoming a pain. We couldn't find anything wrong with the servers either, they've been working just fine but the randomly appearing memory faults are scary. Should we ignore them?
OS: OpenSolaris 2009.6 (b111)
Answer: I can only guess but from what I've read up is that the fault you are experiencing is due to the fact the number of correctable ECC errors in a given time have been exceeded. This is sure a problem and should be addressed.
If however, you other team runs windows on these boxes and don't experience any issues this might be due the fact, that windows just corrects the correctable ECC error and keeps silent where OpenSolaris or FMA fire a warning.
It should definitively not being ignored. If I were you I'd take the time to further investigate the windows machine and if there is a possibility to check for those corrected, correctable ECC errors.


Probability and distribution function

Question: Can some one please tell me how to do it?
Let $(i,j)$ denote the numbers on the top faces when a pair of fair dice is rolled.
Let $X(i,j)=i+j-3$.
Find the range of $X$.
Find the probability distribution function $f_{X}(x)$ of $X$.
Find each of the following

$p(x>11)$
$p(x<7)$
$p(x \ge0)$
Comment: Where are you stuck? What have you tried?
Answer: The range: The values of $i$ and $j$ range from $1$ to $6$. So the smallest possible value of $i+j$ is $2$, and the largest is $12$. So $i+j-3$ ranges over the integers from $-1$ to $9$.
Thus the range of $X$ is the set of all integers $n$ such that $-1\le n\le 9$. You could write it also as $\{-1,0,1,1,3,4,5,6,7,8,9,\}$.
The probability distribution function: Recall that $f_X(x)=P(X=x)$. We need to specify $f_X(x)$ for all $x$ in the range.
As a start, we find $f_X(-1)=P(X=-1)$. We have $X=-1$ precisely if $i+j=2$, which happens if both dice show a $1$. The probability of this is $\frac{1}{6}\cdot\frac{1}{6}=\frac{1}{36}$.
Next we find $f_X(0)$. We have $X=0$ precisely if $i+j=3$. This happens if the yellow die shows $1$ and the pink shows $2$, or the other way around. So $f_X(0)=\frac{2}{36}$.
Next we find $f_X(1)$. Note that $X=1$ precisely if $i+j=4$, that is, if two dice give sum $4$. We have probably long known that the sum of two fair dice is $4$ with probability $\frac{3}{36}$.
Let's get slightly more abstract. Let $Y=X+3$. Then $Y=i+j$ is just the sum of two dice, and $X=k$ iff $Y=X+k$. So $f_X(1)=f_Y(4)=\frac{3}{36}$.
Similarly, $f_X(2)=f_Y(5)=\frac{4}{36}$, $f_X(3)=f_Y(6)=\frac{5}{36}$, $f_X(4)=f_Y(7)=\frac{6}{36}$, $f_X(5)=f_Y(8)=\frac{5}{36}$, and so on.
The numbered questions:  $1$. We want $P(X\gt 11)$. Since $X=Y-3$, We have $X\gt 11$ precisely if $Y \gt 14$. Of course the sum of two dice cannot be $\gt 14$, and therefore $P(X\gt 11)=0$.
$2$. This one is more work.  The most mechanical way to do it is to observe that $X \lt 7$ precisely if $X$ takes on the values $-1$ to $6$. So add up the values of $f_X(k)$ obtained earlier, $k=-1$ to $k=6$.
Or else note that this is the probability that $Y \lt 10$, that is, the probability that the sum $Y$ of two dice is less than $10$. It is easier to find the probability that $Y\ge 10$. This is $\frac{3}{36}+\frac{2}{36}+\frac{1}{36}=\frac{6}{36}$. So the probability that $Y \lt 10$ is $\frac{30}{36}$.
$3$. More of the same.

Probability and distribution function


What is the result of $\frac{\partial^2 \ell(\beta)}{\partial \beta \partial \beta^T}$?

Question: For logistic regression model, the log-likelihood function can be written as
$$\ell(\beta)=\sum_{i=1}^N(y_i\beta^Tx_i-\log(1+e^{\beta^Tx_i}))$$
This is a $(p+1)$ nonlinear equations in $\beta$. To solve this problem, we use the Newton-Raphson algorithm, which requires the second-derivative
$$\frac{\partial^2 \ell(\beta)}{\partial \beta \partial \beta^T}=-\sum_{i=1}^Nx_ix_i^Tp(x_i;\beta)(1-p(x_i;\beta)) \,(*)$$
where $p(x_i;\beta)=1-\frac{1}{1+e^{\beta^Tx_i}}$.
My question is how to get (*)?
I know that
$$\frac{\partial \ell}{\partial \beta }=\sum_i (y_ix_i-\frac{e^{\beta^Tx_i}x_i}{1+e^{\beta^Tx_i}})\in R^{(p+1)\times 1}$$
But what is $$\frac{\partial^2 \ell(\beta)}{\partial \beta \partial \beta^T}?$$
I get
$$\frac{\partial^2 \ell(\beta)}{\partial \beta \partial \beta^T}=\frac{\partial}{\partial \beta^T }(\sum_i (y_ix_i-\frac{e^{\beta^Tx_i}x_i}{1+e^{\beta^Tx_i}}))=?$$
Comment: Isn't just derivating on $z=\beta^{T}$?
Answer: A very good resource for many of the differentiation rules needed to derive these statistical methods is the matrix cookbook. In particular, I find it very helpful for keeping track of what does and does not get transposed when taking derivatives. In your case, we can take the ideas behind @Demetri Pananos's answer to its conclusion.
Let us instead work with $p(z) \equiv \frac{e^z}{1+e^z}$. First, note that we have $p'(z) = p(z) (1-p(z))$. Note that thinking about $p(z)$ as a probability gives a nice intuition for what this derivative corresponds to: it's nothing more than the variance of a Bernoulli random variable with probability $p(z)$! This intuition is so important that I will henceforth call $v(z) = p(z)(1-p(z))$. In other words, when we plug in $z=\beta^T x_i$ and $\beta$ is the true value, then this derivative is precisely the variance of a single observation conditional on $x_i$. Now, letting $f_i:\mathbb R\to \mathbb R^{p+1}$ be given by $p(z) x_i$, we have $f_i'(z) = v(z) x_i$, so combining this with the chain rule, we have $\frac{\partial p(\beta^T x_i)}{\partial \beta^T} = v(\beta^T x_i)\cdot x_i x_i^T$. Thus, we conclude that
$$\frac{\partial^2 \ell}{\partial \beta \partial \beta^T} = \sum_i v(\beta^T x_i)\cdot x_i x_i^T$$
Rather than dwelling on the details of the computation though, I think it is helpful to get some intuition for why the resultant Newton's method algorithm is sensible not just from a computational point of view but in fact from a statistical point of view. A few observations. First, let us rewrite the score function as
$$\frac{\partial \ell}{\partial \beta^T} = \sum_i \underbrace{(y_i - p(\beta^T x_i))}_{\text{Residuals}}\cdot\underbrace{x_i^T}_{\text{Regressors}}$$
The first order condition of logistic regression therefore has the same form (and is due to the same basic intuition) as the first order condition for OLS: to get an "optimal" predictor, you cannot have your regressors to still be correlated with your residuals since otherwise, you could clearly move $\beta$ in the direction of that correlation to get an even better predictor.
The only tricky aspect of the score function is that $p$ introduces a non-linearity, but by taking a Taylor approximation, we can temporarily ignore this issue. Specifically, let $\beta_j$ be a guess of $\beta$ which is not necessarily yet optimal, in the sense that $\frac{\partial \ell}{\partial \beta}\big|_{\beta = \beta_j} \neq 0$. Let us now take a Taylor approximation to determine how different values of $\beta_{j+1}$ might improve our situation:
$$\begin{aligned}\frac{\partial \ell}{\partial \beta}\Bigg |_{\beta = \beta_{j+1}} &\approx \sum_i \left[y_i - p(\beta_j^T x_i) - v(\beta_j^T) (\beta_{j+1}^T - \beta_j^T)x_i\right] x_i^T\\
& = \sum_i \underbrace{v(\beta_j^T)}_{\text{"Weights"}}\cdot \underbrace{\left[\frac{y_i - p(\beta_j^T x_i)}{v(\beta_j^T x_i)} - (\beta_{j+1} - \beta_j)^Tx_i\right]}_{\text{"Approximate Residuals"}} \cdot \underbrace{x_i^T}_{\text{Regressors}}\end{aligned}$$
Thus, to first order approximation, our update to $\beta$, $\beta_{j+1} - \beta_j$, is given by the solution a particular weighted least squares problem, whose well known solution is given by
$$\begin{aligned}\beta_{j+1}-\beta_j = \left(\sum_i \underbrace{x_ix_i^T}_{\text{"Regressor squared"}}\underbrace{v(\beta^T x_i)}_{\text{"Weights"}}\right)^{-1}\left(\sum_i \underbrace{x_i\frac{(y_i - p(\beta^T x_i))}{v(\beta^T x_i)}}_{\text{"Outcome times regressor"}}\underbrace{v(\beta^T x_i)}_{\text{"Weights"}}\right)\end{aligned}$$
You can check that this weighted least squares view exactly recovers the Newton's method iteration. Moreover, note that the weights of the weighted regression are quite sensible: they are proportional to the predicted variance of $y_i$ given $x_i$, and hence correspond to the intuition that at each step, we want to spend proportionately more of our effort getting it right for those observations that we think are especially hard to predict.
Answer: I'm not going to go through the entire calculation (mostly because I'm not confident that I can do it correctly, but after a couple minutes of algebra I think I can add some value).
First, let's note that if you have
$$ f(z) = \dfrac{1}{1 + \exp(-z)} \>, $$
then $$f'(z) = f(z)\Big(1-f(z)\Big) \>.$$
You can check that by either solving this differential equation, or by doing the algebra and checking that the LHS is the same as the RHS.
You can use this fact to determine that
$$ \dfrac{\partial}{\partial \beta} f(x^T\beta) = x^Tf(x^T\beta) \Big( 1-  f(x^T \beta) \Big) $$
by using the product rule.
These facts alone can get you very far in taking derivatives of the likelihood function, especially if you write it in the familiar form
$$ \ell(\beta) = \sum_i y_i \log(p_i) + (1-y_i) \log(1-p_i) $$
being sure to keep in mind that $p_i = p_i(x^T \beta)$


HP ProLiant DL380 G6 GPU installation

Question: I have a Proliant DL380 G6 and I want to build a Graphics card into it. To be specific, it is a GTX 960 from Asus. The card When I now want to build it in, I am pretty shure I need a pcie slot riser, which one would you recommend? And also, can I just plug the card in on the SAS Connector A or B on the motherboard? If no, how would I get power to the card? I post a picture of my server below.the server
Comment: This GPU will not fit in this server. It is more than 2 slots wide.
Answer: Your server is missing the entire PCIe riser assembly. It occupies the area outlined in pink below. You didn't notice that the rear part of the server is missing?

In order to use this system with PCIe cards, including your graphics card, you need the system's plastic air baffle (496061-001) and the entire DL380 G6 PCIe cage (496060-001).
Air baffle.

This is the PCIe riser cage which holds modular PCIe riser boards.

The cage takes one or two PCIe riser cards.

Risers can supply auxiliary power for some GPUs (see this guide), but it looks like your server is incomplete and missing other critical components.

Edit:
Here are the HP PCIe riser options from DL380 G6 quickspecs:
Comment: I have the Air baffle and the PCIe riser cage they were next to it ,but I dont have a PCIe riser card, which one would you use (do you know a product nacme)? And also, Is there a way to get extra power to the gpu ?
Comment: Where did you obtain this server? The face that your PCIe risers are missing is not normal. See the update at the bottom of my answer for part numbers.
Comment: I got this server used from a company for free so I  dont complaing ^^. But I can buy the extender card for like 6€. But do you think, with 2x460 Watt psu's I have enough power to take the gtx680 ?
Answer: You will need to use the x16 Riser card.
The cheapest I can find them being sold for is $105 with the average price being $185 (USD).
Good luck finding them for 6 euros. You can't use the 3 Slot riser that is sold ever where cheap since that is only x8.
These servers sell for only ~$140 on average for the whole system on eBay which is less $ than just the x16 riser card. Sadly they rarely include a x16 riser as they seem to be rare.
As for the power, both of mine include 2 750 watt PSU so that is strange that yours only have 460 watt PSUs. The 2nd PSU is only for Redundancy to kick in if 1 of them stops working as to prevent power loss & server downtime. They also don't have a bunch of connectors coming out from them to plug into your GPU. You may need to use an extra ATX power supply that you just turn on by connecting the green pin to black on the 24pin mobo cable and use the PCI-E power from it to power the GPU.

HP ProLiant DL380 G6 GPU installation


Bayesian mixture model joint posterior

Question: I am just starting to learn about bayesian mixture models. There is a few clarifications that I want to make which I am not sure myself. The graphical model below describes a gaussian mixture model where $\pi, \Psi, \Sigma, \mu, \boldsymbol{Z}$ are the latent unknowns that we would like to infer about the mixture model. $\mu,\Sigma, Z$ are vectors since we are parameterising by $K$ clusters.

From the model described, am i right to say that the joint posterior explicitly is
$$p(\boldsymbol{Z},\Sigma,\mu, \pi | \alpha, \mu_0, \Sigma_0, \Psi, \{X_n\}_{i=1}^N) = p(\pi|\alpha)\prod_{k=1}^Kp(\mu_k|\mu_0,\Sigma_0)p(\Sigma_k|\Psi)\prod_{i=1}^Np(z_i|\pi)p(x_i|z_i, \mu,\Sigma)$$
From this full joint posterior, how do i use conditional independence to compute the conditional posterior distributions for each variable to do gibbs sampling ?..
Comment: Shouldn't the "sigma" in the final factor of the joint posterior be uppercase?
Comment: What is the difference between joint distribution vs joint posterior ?
Comment: I've modified my answer slightly. See if that helps. BTW, your "equation" for the posterior distribution should be a proportionality, not an equality. The right-hand side is the joint distribution and not the joint posterior.
Answer: I think I can help. I'll give a partial answer and if you need more then let me know.
First I'd like to simplify the notation a bit. Let $X = (x_1, \ldots, x_N)$ denote the data, which is in parallel with $Z = (z_1, \ldots, z_N)$.
Also I will suppress the known parameters in the priors for the unknowns $(\pi,Z,\mu,\Sigma)$. With these changes, the joint posterior can be expressed as proportional to the joint distribution
\begin{equation}
p(\pi,Z,\mu,\Sigma|X) \propto p(\pi,Z,\mu,\Sigma,X) .
\end{equation}
The Gibbs sampler involves cycling through the full conditional distributions:
\begin{align}
p(\pi|X,Z,\mu,\Sigma) &\propto p(\pi,Z,\mu,\Sigma,X) \\
p(Z|X,\pi,\mu,\Sigma) &\propto p(\pi,Z,\mu,\Sigma,X) \\
p(\mu|X,\pi,Z,\Sigma) &\propto p(\pi,Z,\mu,\Sigma,X) \\
p(\Sigma|X,\pi,Z,\mu) &\propto p(\pi,Z,\mu,\Sigma,X) ,
\end{align}
where each conditional distribution is proportional to the joint distribution.
Let's begin with the distribution for $\pi$. We need to collect all the factors that involve $\pi$ and ignore the rest. Therefore,
\begin{equation}
p(\pi|X,Z,\mu,\Sigma) \propto p(\pi|\alpha)\,\prod_{i=1}^N p(z_i|\pi) .
\end{equation}
That's the main idea. You can apply it to the other unknowns.
I could stop here, but I think it will help to provide some interpretation for some of these symbols. In particular,
\begin{equation}
z_i \in \{1, \ldots, K\}
\end{equation}
is a latent classification variable the indicates which mixture component $x_i$ belongs to. Therefore,
\begin{equation}
p(x_i|z_i,\mu,\Sigma) = \textsf{N}(x_i|\mu_{z_i}, \Sigma_{z_i}) .
\end{equation}
In addition,
\begin{equation}
p(z_i|\pi) = \textsf{Categorical}(z_i|\pi) = \prod_{k=1}^K \pi_k^{1(z_i=k)}
\end{equation}
where
\begin{equation}
1(z_i=k) = \begin{cases}
1 & z_i = k \\
0 & \text{otherwise}
\end{cases} .
\end{equation}
Just to be clear, $p(z_i = k|\pi) = \pi_k$. So $\pi$ is a vector of $K$ probabilities that sum to one and (very likely, but not necessarily)
\begin{equation}
p(\pi|\alpha) = \textsf{Dirichlet}(\pi|\alpha) \propto \prod_{k=1}^K \pi_k^{\alpha_k-1} .
\end{equation}
You now have the prior and the conditional likelihood for $\pi$, which enough to figure out what the conditional posterior for $\pi$ is. So I will stop here.
Comment: Thanks @mef for the explanation. Is the posterior equation for the joint distribution that I have written correct ? I believe that $\alpha$ in the graphical model shouldn't be labelled a circle because it is not a variable we want to infer about
Comment: Sorry for the comment above, I did not phrase it correctly. I wanted to know whether the joint distribution for the mixture model is correctly written or have I missed something.
Comment: Also, the reason why I want to know how to write the joint distribution for finite mixture model is because I want to be able to write it for the infinite case. In many of the materials in the internet on DPMM, they do not show the full joint posterior but rather say that the $\pi$'s are integrated out of the model, leaving only the latent variable assignments $Z$ and cluster parameters $\theta$. If the joint distribution for the current finite mixture model is correctly written, then I can just replace $k$ in above with $\infty$

Bayesian mixture model joint posterior


A surjective endomorphism between f.g. $k$-algebra is an isomorphism?

Question: This is similar to this but different. The original argument requires Nakayama's lemma, I wonder if the following still holds:

Let $\varphi:A \rightarrow A$ be a surjective $k$-algebra homomorphism between finitely generated $k$-algebra $A$. Then $\varphi$ is injective.

Is this true? This would be useful in study of coordinate rings.
Comment: @user: Finitely generated doesn't mean finite-dimensional as $k$-vector spaces.
Comment: @darijgrinberg Yes. I have missed this part for sure as I have somehow had field extension in mind. Then this does not guarantee f.g. as module.
Comment: Are your $k$-algebras commutative? I suspect (but am not sure) that the answer is "no" without commutativity.
Comment: I was actually working with coordinate rings of affine varieties - which I believe are commutative? So I am also interested in commutative case.
Answer: For commutative rings, this is certainly true. The crucial hypothesis you need is that $A$ is Noetherian, which is true for finitely generated algebras over a field. You have a chain of ideals, $\ker \phi\subset\ker \phi^2\subset\ker \phi^3\subset\cdots$ and thus there is an $n$ such that $\ker \phi^n=\ker\phi^{n+1}=\cdots$. This can not happen unless $\ker\phi=0$ since $\phi$ is surjective.
Comment: Sorry, may you elaborate on the last step ? I don't see how $\ker \phi =0$...
Comment: Ok . let $a \in A$, so $a = \varphi^n(b_a)$ for some $b_a \in A$. Thus $\varphi(a) = \varphi^{n+1}(b_a)$. If $\varphi(a) =0$, $b_a \in \ker \varphi^{n+1}=\ker \varphi^n$ so $a = 0$. Is this what you had in mind? Or you had simpler proof?
Comment: @CL. Your argument is fine.
Comment: Thanks a lot Mohan, I think I will leave the question open until someone answer the noncommutative case.


Como obter uma valor de uma propriedade da array?

Question: A VÁRIAVEL ARMAZENZA:
$parcelas = DB::select("SELECT parcela_number FROM parcelas WHERE 3000 BETWEEN valor_min AND valor_max");

VAR_DUMP:
   array(1) {
  [0]=>
  object(stdClass)#144 (1) {
    ["parcela_number"]=>
    int(3)
  }
}

OBJETIVO:
$parcelas->parcela_number // RESULT 3

Comment: posta, por favor, o var_dump($parcelas); Daí eu consigo te ajudar
Comment: Ok, já postei o var_dump
Comment: ok tenta assim:
$parcelas[0]->parcela_number;

Me fala se deu certo, que daí te explico o porque etc
Comment: Deu certo, só que não entendi o porque, em outro caso semelhante funciona assim  $parcelas[0]->value.
Answer: André, vamos lá.
A ferramenta do var_dump() é muito interessante pois você compreende o tipo de retorno que você tem. Em seu retorno nós tivemos o seguinte:
array(1) {
  [0]=>
  object(stdClass)#144 (1) {
    ["parcela_number"]=>
    int(3)
  }
}

vc nota que a $parcela é um array. E depois temos um objeto. A maneira de acessar os dados de array e de um objeto são diferentes, por isso não estava conseguindo antes. E por ser um array não conseguimos acessar os dados via -> e sim por posição (índice).
Faço um foreach() na sua variável $parcelas que conseguirá pegar os dados via $parcela->
Eu não consigo criar um código para você, pois necessito de mais código seu para te dar um exemplo prático, mas acredito que conseguirá tranquilamente.
Para resolução do seu problema faça: $parcelas[0]->parcela_number;
Answer: Segundo a documentação do Laravel( https://laravel.com/docs/5.5/database#running-queries ), o Fachada DB juntamente com o método select. Ex:
$resultados = DB::select('select * from ...')

SEMPRE retornará um array e caso haja resultado, será um objeto dentro deste array.
Desta forma, o acesso deverá ser a partir de um array.
$resultados[0]; // para acessar o primeiro resultado

ou ainda:
foreach($resultados as $resultado) {
    echo $resultado->key;
}

para acessar todos os resultados.
A saber: Métodos interessantes para debug dd() e dump() disponíveis no Laravel.


Understanding training phase of an agent trained using deep Q-learning

Question: Assume we have a deterministic game and we want to train an agent using Deep Q-Learning (a.k.a. DQN) to play this game. We record the score of the agent at each episode and plot the scores versus the episode numbers. Assume the maximum score of the game is 1 and the minimum is -1.
Now, my question is this: if we trained an agent for 300k episodes and observed after a while the agent learns to get the score of 1 and it keeps doing that for the first 100k. However, after that, the score drops to around -1 and stays there until 300k-th episode. What can be the reason for this phenomena? Although the agent learns to play the game perfectly for a long time, why it suddenly forgets it?
Answer: This is called "catastrophic forgetting", and seems to occur quite frequently in DQN.
The problem you are seeing might go something like this internally (note this is a hand-wavy/conjecture argument, I have not measured thi happening numerically):

The agent learns a near-optimal strategy.
It follows that strategy, and only explores close to it.
The transition history becomes saturated with states and actions seen close to optimal behaviour, and that more often than not gain long-term rewards of optimal behaviour.
The model parameters, in order to obtain the best loss for those states, change so that they predict action values as accurately as possible for the current distribution of states and actions.
The accuracy of predictions for less-frequently seen states/actions gets worse due to over-fitting. Initially this is not noticed, because those state/action pairs are not being visited.
Eventually, a prediction for a bad action choice somewhere in the environment becomes high enough to get that action selected, perhaps whilst exploring. This makes the agent explore a "bad neighbourhood" of states and actions with low rewards.
The neural network tries to correct the predictions whilst taking these bad actions, but now the internal representations that were focused on the optimal path only work against it - any correction to this erroneously high prediction for a bad action will at least initially change parameters so that the values of all optimal state/actions are also reduced.
Depending on how far the over-fitting went before the agent made a poor enough prediction that made it misstep, it could either recover relatively quickly, or take a long time. The high error signals comparing predicted value against actual value could even cause it to diverge.

Some fixes to this are the same as for over-fitting - add regularisation to the neural network, test your progress (use monte-carlo estimates of the agent following the Q values perfectly over some number of episodes).
You might also be tempted to adjust what is stored in replay memory. It may help if you find a simple heuristic for this, or perhaps you are able to start the agent in a variety of random starting states. However, in general this is a really hard unsolved problem - the statistical dataset/population base assumed for supervised ML is not a perfect match for learning Q-functions, which don't have quite the same relationship to populations of observed transitions - at least not while exploring and learning optimal/better control.
There is ongoing research into this problem, with various proposed fixes, such as Overcoming Catastrophic Forgetting by Incremental Moment Matching.
Comment: @user491626: You can reduce epsilon, and that may help keep the estimates stable, but that is essentially the same as stopping learning. Minh et al.'s suggestion is similar to "early stopping" regularisation.
Comment: @user491626 If you think you have finished learning, then the value of epsilon is irrelevant in Q learning. Set it to zero in order to just use the best policy found. If you want to continue learning, then epsilon needs to be non-zero (best value depending on problem at hand). It is not always clear what the maximum score is, you will instead have measures of best score so far. Most of the Atari games played by DQN were not "solved" with a theoretical max score. In a deterministic game, you may still worry about distribution of start states. In a 2-player game, worry about opponent's policy.
Comment: @user491626: I'm not sure. Part of the problem is that generalisation (against one population) can actually work against you (derived feature representations in hidden layers get specialised to single population, and don't re-set fast enough in another population). Having less params won't necessarily save you from this "generalisation error leads to population shift" problem. As with supervised learning there is likely to be an optimal combination of hyperparameters, but that is not the entire answer.
Comment: Your question was specifically about Q Learning in DQN. However if you are facing lots of problems like this in environments you want to solve, it may be worth looking at policy gradient methods, such as Actor-Critic (A3C or A2C implementations) - policy gradients modify policy less radically on finding errors (no max function that will just switch action when an action reaches top of a sorted list), and can be more robust to the effect as a result.
Comment: Thanks for your comprehensive answer. For my case, the drop in the scores happens long after end of exploration phase (when epsilon became 0.01). Considering the fact that the environment of my problem is deterministic, can I set the minimum epsilon to 0 (instead of 0.01) to avoid the agent exploring those states with bad predictions?
Comment: One solution Mnih et al. [2015] propose to the "catastrophic forgetting" is to simply save the network parameters that resulted in the best test performance.
Comment: I am sorry to ask this as this is a different question, but why do we need to keep epsilon non-zero after the agent learns to get the maximum score? I see that almost all DQN paper assumes that the minimum epsilon is zero while all of their games are deterministic.
Comment: One more question about your suggested solution "add regularisation to the neural network": Does it mean that my neural net is unnecessarily complicated? And for example I can possibly reduce the number of nodes in the layers to avoid this issue?


Proof of $\lim_{s\to 1^+}\frac{\sum_p p^{-s}}{\ln(s-1)}=-1$

Question: I am looking for a proof of $$\lim_{s\to 1^+}\frac{\sum_p p^{-s}}{\ln(s-1)}=-1.$$
If the proof is too long, a direct reference is fine. Here the sum $\sum_p$ denotes the sum over all prime numbers.
EDIT: Perhaps this is one of the examples Mathematica does not help. I put in $s=1.0001$ and calculate
Sum[(Prime[n])^(-1.0001),{n,1,10000}]/Log(0.0001)
I get $-0.294043$. I even use smaller $s$, and larger upper bound for $n$, but the answer is far from $-1$. If you can get it to close to $-1$, please let me know how you do it.
Comment: I don't know. I suggest you to start from [here](http://mathworld.wolfram.com/PrimeZetaFunction.html)
Comment: It is difficult to do becuase for $s$ close to $1$ you get "almost" harmonic series. Note that harmonic series diverges very slowly, so for $s\approx 1$ respective series will converge slowly. You need to try [acceleration method](http://en.wikipedia.org/wiki/Series_acceleration)
Comment: Is there an asymptotic formula $\sum_p p^{-s}$ that we can use?
Comment: PrimeZetaP[s] is the Mathematica function for $\sum_p p^{-s}$. Using that I get my limit approximation very close to $-1$.
Answer: Since
$$
\lim\limits_{s\to 1^+}(s-1)\zeta(s)=1\tag{1}
$$
it is enough to show that
$$
\lim\limits_{s\to 1^+}\frac{\sum\limits_{p\in P} p^{-s}}{\log\zeta(s)}=1\tag{2}
$$
Using Euler poduct formula we get
$$
\log\zeta(s)
=\log\prod\limits_{p\in P}\frac{1}{1-p^{-s}}
=\sum\limits_{p\in P}\log(1-p^{-s})
=\sum\limits_{p\in P}\sum\limits_{n=1}^\infty\frac{p^{-sn}}{n}\\
=\sum\limits_{p\in P}p^{-s}+\sum\limits_{p\in P}\sum\limits_{n=2}^\infty\frac{p^{-sn}}{n}\tag{3}
$$
We have the following bound
$$
\begin{align}
\left|\sum\limits_{p\in P}\sum\limits_{n=2}^\infty\frac{p^{-sn}}{n}\right|
&\leq\sum\limits_{p\in P}\sum\limits_{n=2}^\infty\frac{1}{n}\left|\frac{1}{p^{sn}}\right|\\
&=\sum\limits_{n=2}^\infty\sum\limits_{p\in P}\frac{1}{n}\frac{1}{|p^{sn}|}\\
&=\sum\limits_{n=2}^\infty\frac{1}{n}\sum\limits_{p\in P}\frac{1}{p^{\Re(s)n}|p^{\Im(s)n}|}\\
&=\sum\limits_{n=2}^\infty\frac{1}{n}\sum\limits_{p\in P}\frac{1}{p^{\Re(s)n}}\\
&\leq\sum\limits_{n=2}^\infty\frac{1}{n}\sum\limits_{p\in P}\frac{1}{p^{n}}\\
&\leq\sum\limits_{n=2}^\infty\frac{1}{n}\int_1^\infty\frac{1}{t^n}dt\\
&=\sum\limits_{n=2}^\infty\frac{1}{n}\frac{1}{n-1}\\
&=\sum\limits_{n=2}^\infty\left(\frac{1}{n-1}-\frac{1}{n}\right)\\
&=1\\
\end{align}\tag{4}
$$
From $(1)$ we know that $\lim\limits_{s\to 1^+}\log\zeta(s)=\infty$, then from bound $(4)$ and equality $(3)$ it follows that $(2)$ holds. And now we are done!


Converter data e hora da Nota Fiscal Eletrônica - Javascript

Question: A nota fiscal eletrônica gera um qr-code à partir de uma url. Essa url possui diversos parâmetros, um deles é a data e hora da emissão, que segundo a documentação da receita reza:

O valor deverá corresponder à conversão para hexadecimal em caixa
  baixa do valor no padrão UTC com máscara.

Em uma url que possuo, esse campo possui 50 bytes, conforme abaixo:
dhEmi=323031382D30372D32345431323A31323A33352D30333A3030

Eu gostaria de saber como posso converter este valor para um datetime válido em javascript
Answer: function hex2date(hexx) {
       const hex = hexx.toString();
       let datestr = '';
       for (let i = 0; (i < hex.length && hex.substr(i, 2) !== '00'); i += 2)
           datestr += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
       return datestr;
   }


Throttling curl_multi_exec to 3-4 processes instead of all of them without exiting loop

Question: I seem to be crashing my website by asking for too many connections at once (600~),
I would like to throttle this back to maybe 3-4 connections, sleep till free, then add more as needed.
The code that I was thinking about using is...
// execute the handles
$running = null;
do
{

    if ($running <= 4) curl_multi_exec($mh, $running); //add handle

    usleep(100); //sleep

} while($running > 0);

I do NOT think this is the right/sane way to do this though, can someone recommend a better way please?
Comment: Actually, it's kind of fine.  Instead of using a count though, establish a queue, pull a batch size off the queue, and keep that batch full until all the queue is drained.  Implementation example: http://www.onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/
Comment: Plus 1, thanks Bishop I'll read into it.
Answer: Native Solutions in PHP 5.5
There is an option for limiting the number of connections in PHP >= 5.5 using curl_multi_setopt and the CURLMOPT_MAXCONNECTS option.
http://php.net/manual/en/function.curl-multi-setopt.php
CURLMOPT_MAXCONNECTS
Pass a number that will be used as the maximum amount of simultaneously open connections that libcurl may cache. Default is 10. When the cache is full, curl closes the oldest one in the cache to prevent the number of open connections from increasing.
e.g.:
curl_multi_setopt ( $mh , CURLMOPT_MAXCONNECTS, 4);

Caveats - there are reported stability issues on CentOS, and PHP 5.5 or above is required.
In answer to your question about sanity:
The way you are doing it is sane in principle... but can get stuck in an infinite loop very easily.
Appreciate that $running is only updated when curl_multi_exec($mh, $running) is called because $running is passed as  a reference variable to curl_multi_exec;
Solutions
Try these free libraries which achieve what you want to do in an easy-to-swallow format.

RollingCURL: https://github.com/takinbo/rolling-curl
Guzzle - using Pools:  http://guzzle.readthedocs.org/en/latest/quickstart.html#concurrent-requests

Both of which are well tested and have built in maximum connection capabilities.


Turn off IE 8 Compatibility Mode for site

Question: My company uses IE8 as the default browser and by default compatibility mode is set for all intranet sites.  I'm building an intranet site that works when compatibility mode is turned off.  I'm using reset.css and several opensource javascript programs, e.g. datatables.
What I'd like to do is force compatibility mode off for my site.  Is there any programmatic way to do it?  I have tried setting the meta values
 <meta http-equiv="X-UA-Compatible" content="IE=IE8" />

and
 <meta http-equiv="X-UA-Compatible" content="IE=Edge" />

to no avail.
What's the most frustrating part is that Chrome and Firefox work great as is.
Comment: `IE=IE8` does not seem to be valid, `IE=8` functions as expected in my tests.

(Added as a comment rather than an edit as edits must be >6 characters)
Comment: I have recently tussled with the forces of compatibility mode (particularly with a view to tackling the default intranet "on" setting). I've written up how to get round the various issues I encountered here in case anyone else finds it useful: http://icanmakethiswork.blogspot.co.uk/2013/04/death-to-compatibility-mode.html
Answer: The <meta> tag has to be the first tag inside the , other than <title> and other <meta> elements.

The X-UA-compatible header is not case sensitive; however, it must appear in the Web page's header (the HEAD section) before all other elements, except for the title element and other meta elements.

http://msdn.microsoft.com/en-us/library/cc288325%28VS.85%29.aspx#DCModes
Answer: These must be the FIRST meta tag on your page. Perhaps that's the issue.
Comment: It doesn't have to be the first meta tag, but before all non-meta tags (except title).  So no before style and script, etc.


Holomorphic function on an interval

Question: If $f(x),g(x)$  are holomorphic functions (both functions are everywhere defined) and we know that for $\alpha \in [a,b];f(\alpha)=g(\alpha)$, does this mean that $f(x)=g(x)$?
Comment: Identity principle, see https://en.wikipedia.org/wiki/Identity_theorem.
Comment: Nit-pick: $[a, b]$ is a dubious notation when talking about complex numbers.
Answer: Yes. This is a particular case of the Identity Theorem, since an interval has accumulation points (actually, all of its points are accumulation points).

Holomorphic function on an interval


vba: log cleaning macro: how to optimize for faster execution?

Question: I'm running a macro that I made to delete the log contents and reformat the table in case that any format changes occurred while using it, the macro does what I intended but it takes about 30 minutes to finish running, all the while excel is completely frozen and unusable. Upon checking task manager on the process usage column, it seems excel is only using about 12.5% of my processing capacity, is there any way that I can make this multithread or that I can optimize the code to not take forever?
P.S. I decided to apply the merging of cells in batches as I figured doing it for all cells at once might have been what was making the code run much slower but I didn't notice any real difference in execution time.
Code below:

Sub ClearAndFormatLogTable()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Easy Mode") ' Replace with your sheet name

    ' Turn off ScreenUpdating and Events to improve performance
    Application.ScreenUpdating = False
    Application.EnableEvents = False

    ' Clear values and formats from range A7 to J50000
    ws.Range("A7:J50000").ClearContents
    ws.Range("A7:J50000").ClearFormats

    ' Merge cells in the first row (row 7)
    ws.Range("C7:J7").Merge
    ws.Range("C7").HorizontalAlignment = xlCenterAcrossSelection

    ' Copy format from the merged cell
    ws.Range("C7").Copy

    ' Apply the format in batches
    Dim i As Long
    For i = 8 To 50000 Step 1000
        ws.Range("C" & i & ":J" & Application.Min(i + 999, 50000)).PasteSpecial xlPasteFormats
    Next i

    Application.CutCopyMode = False ' Clear the copy mode

    ' Apply thick borders and dotted inner vertical divisions
    With ws.Range("B7:J50000")
        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeLeft).Weight = xlThick
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeRight).Weight = xlThick
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeTop).Weight = xlThick
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).Weight = xlThick
        .Borders(xlInsideHorizontal).LineStyle = xlDot
        .Borders(xlInsideVertical).LineStyle = xlDot
    End With

    ' Turn ScreenUpdating and Events back on
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub

Thanks in advance!
Comment: If you can't really speed this up then maybe consider having a pre-formatted range on a hidden sheet which you can copy-paste in one operation over to "easy Mode"   Or replace the whole sheet with a copy of a hidden template sheet.
Answer: A shorter version of setting borders:
Sub AddBorders()
    Dim tm As Double, ws As Worksheet
    Set ws = ActiveSheet
    tm = Timer
    ' Apply thick borders and dotted inner vertical divisions
    With ws.Range("B7:J50000")
        .Clear
        .BorderAround xlContinuous, xlThick
        .Borders(xlInsideHorizontal).LineStyle = xlDot
        .Borders(xlInsideVertical).LineStyle = xlDot
    End With
    MsgBox "Czas = " & Timer - tm    ' 0.95 s Borders
                                     ' 0.6 s BorderAround
End Sub

Comment: Thanks for the help MGonet, hadn't realized I had this option!
Answer: Sub ClearAndFormatLogTable()
'Counter=0  Turn off the Counter
Counter = 1: stTime = Timer
'....................................................................................................
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Easy Mode") ' Replace with your sheet name

    ' Turn off ScreenUpdating and Events to improve performance
    Application.ScreenUpdating = False
    Application.EnableEvents = False

    ' Clear values and formats from range A7 to J50000
    ws.Range("A7:J50000").ClearContents
    ws.Range("A7:J50000").ClearFormats

    ' Merge cells in the first row (row 7)
    ws.Range("C7:J7").Merge
    ws.Range("C7").HorizontalAlignment = xlCenterAcrossSelection

    'By using fill down instead of copy paste, results are obtained in 20-30 seconds.
    With ws.Range("C7:J50000")
        .FillDown
    End With

    ' Apply thick borders and dotted inner vertical divisions
    With ws.Range("B7:J50000")
        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeLeft).Weight = xlThick
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeRight).Weight = xlThick
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeTop).Weight = xlThick
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).Weight = xlThick
        .Borders(xlInsideHorizontal).LineStyle = xlDot
        .Borders(xlInsideVertical).LineStyle = xlDot
    End With

    ' Turn ScreenUpdating and Events back on
    Application.ScreenUpdating = True
    Application.EnableEvents = True
'....................................................................................................
    If Counter = 1 Then MsgBox _
    Round(Timer - stTime, 2) & " second", vbInformation, "Macro Time"
End Sub

Comment: By using fill down instead of copy paste, results are obtained in 20-30 seconds.
Comment: @RicardoBarreraVillarreal @cranky : Always use `Long`, not `Integer` to prevent overflow errors. `Integer` used only 2 bytes in the old days, but since 32bit OS, it's occupying 4 bytes anyhow, and the VBA runtime artificially limit it to use only 2 bytes for compatibility reasons.
Comment: It would be nice if you could elaborate what's the diffence between your solution and the one of OP and why you think yours is faster.
Comment: Although this is a nice approach (and personally I think there's no better one to the problem statement), I'd strongly suggest whenever faced with the scenario to basically clean and recreate a sheet follow TimWilliams comment: keeping a sheet format that you basically duplicate each run makes a lot make easier if there are changes later on.
Comment: @Sgdva Thanks for the suggestion, I will check that out, I didn't think of that solution as one would normally try to keep the workbook as clean and minimal as possible while the macro does all the work but it does make sense to do it how you suggest for super fast execution, perhaps I can just hide the template!
Comment: @cranky How am I supposed to declare this counter? as an Integer, single?
Comment: @RicardoBarreraVillarreal Integer
Comment: Thank you @cranky, this solution really helped my case, I can now run the macro in under one minute!
Comment: @RicardoBarreraVillarreal if this helped you and solved the problem, please check the answer with the green arrow :)
Comment: @Sgdva thanks for pointing it out, just checked the green checkmark!


proving continuity of a function from f(x+h) and f(x-h)

Question: Let f be such that lim as h approaches 0 of:
    (f(x+h) - f(x-h)) = 0
for all real numbers x. Does this imply that f is continuous?
Answer: No. By that definition
$$f(x) = \begin{cases} 1, & x \neq 0 \\ 0, & x = 0 \end{cases}$$ is continuous at $x = 0$.
Every (equivalent) definition of continuity at a point has to refer one way or another to the value of the function at that point. Your candidate definition does not and that's why it runs into trouble.


Template file not loading for block view

Question: I have a block view in the header. The suggested tpl files are block__mens_header (block--mens-header.tpl) as well as node--mens-header.tpl For some reason the more general tpl files are working but I need to get specific here. Is there a preprocess function I need before these will be seen?
Comment: Got node--mens-header.tpl to work but the block still isn't...?
Comment: Did you configured the block visibility setting to appear in some specific page(s)? Did you try to clear your cache as well?
Comment: The block is visible and i did clear the cache plenty of times.
Answer: I seem to remember, in drupal 6 at least, that you need to have block.tpl.php there, even if you don't modify it at all, in order for the specific block templates to be seen.
ADD:  If it's in there, you might try a preprocess_block() hook with something like:
<code>$variables['template_files'][] = 'block--mens-header';
</code>
stuck in it.  This should point it to block--mens-header.tpl.php in your theme directory.  Obviously, wrap that in some logic so it's only set for the block in question.
Comment: I do have the block.tpl file in the dir. Not sure what else could be the problem.
Comment: is this for drupal 7 or 6? I am using 7. Also can't say I know much PHP so what logic do I need...? Is there documentation I can read on this? I am trying to learn.Thanks
Comment: I don't know enough about d7 to rattle something off right here.  I'd suggest digging through drupal.org's documentation (A good start is http://drupal.org/theme-guide/6-7 ) or grab a good book.  It's pretty straightforward, but also complex, all wrapped up in one :)
Comment: oh...and make sure you clear your caches often....


Theme change by url

Question: Pretty new to drupal. I will have roughly seven themes and looking for the best way to change themes by url...  xxxx.com/parents = one theme  xxxxx.com/edu be another theme xxxx.com/daily  will be another theme. There will be about 7 different themes all together. What is the best way to way to make this happen.
Thank you
Answer: You can do this by using the following ways.

Use the ThemeKey module

ThemeKey allows you to define simple or sophisticated theme-switching
  rules which allow automatic selection of a theme depending on current
  path, taxonomy terms, language, node-type, and many, many other
  properties. It can also be easily extended to support additional
  properties exposed by other modules.

Add the following code to your settings.php
 $theme_urls = array(
   array('/admin','garland'), // Drupal already supports admin theme, anyway
   array('/users','fancy_user_theme'),
   array('/forum','forums_theme'),
);

foreach ( $theme_urls as $info) {
  if (strpos($_SERVER['REQUEST_URI'], $info[0]) === 0) {
    $conf['theme_default'] = $info[1];
  }
}

Theme change by url


The proof of shrinking coefficients using ridge regression through "spectral decomposition"

Question: I have understood how ridge regression shrinks coefficients towards zero geometrically.
Moreover I know how to prove that in the special "Orthonormal Case," but I am confused how that works in the general case via "Spectral decomposition."
Comment: You have stated you are confused, but what is your question?
Answer: The question appears to ask for a demonstration that Ridge Regression shrinks coefficient estimates towards zero, using a spectral decomposition.  The spectral decomposition can be understood as an easy consequence of the Singular Value Decomposition (SVD).  Therefore, this post starts with SVD.  It explains it in simple terms and then illustrates it with important applications.  Then it provides the requested (algebraic) demonstration.  (The algebra, of course, is identical to the geometric demonstration; it merely is couched in a different language.)

##What the SVD is
Any $n\times p$ matrix $X$, with $p \le n$, can be written $$X = UDV^\prime$$ where

$U$ is an $n\times p$ matrix.

The columns of $U$ have length $1$.
The columns of $U$ are mutually orthogonal.
They are called the principal components of $X$.

$V$ is a $p \times p$ matrix.

The columns of $V$ have length $1$.
The columns of $V$ are mutually orthogonal.
This makes $V$ a rotation of $\mathbb{R}^p$.

$D$ is a diagonal $p \times p$ matrix.

The diagonal elements $d_{11}, d_{22}, \ldots, d_{pp}$ are not negative.  These are the singular values of $X$.
If we wish, we may order them from largest to smallest.

Criteria (1) and (2) assert that both $U$ and $V$ are orthonormal matrices.  They can be neatly summarized by the conditions
$$U^\prime U = 1_p,\ V^\prime V = 1_p.$$
As a consequence (that $V$ represents a rotation), $VV^\prime = 1_p$ also.  This will be used in the Ridge Regression derivation below.
##What it does for us
It can simplify formulas.  This works both algebraically and conceptually.  Here are some examples.
###The Normal Equations
Consider the regression $y = X\beta + \varepsilon$ where, as usual, the $\varepsilon$ are independent and identically distributed according to a law that has zero expectation and finite variance $\sigma^2$.  The least squares solution via the Normal Equations is $$\hat\beta = (X^\prime X)^{-1} X^\prime y.$$  Applying the SVD and simplifying the resulting algebraic mess (which is easy) provides a nice insight:
$$(X^\prime X)^{-1} X^\prime = ((UDV^\prime)^\prime (UDV^\prime))^{-1} (UDV^\prime)^\prime \\= (VDU^\prime U D V^\prime)^{-1} (VDU^\prime) = VD^{-2}V^\prime VDU^\prime = VD^{-1}U^\prime.$$
The only difference between this and $X^\prime = VDU^\prime$ is that the reciprocals of the elements of $D$ are used!  In other words, the "equation" $y=X\beta$ is solved by "inverting" $X$: this pseudo-inversion undoes the rotations $U$ and $V^\prime$ (merely by transposing them) and undoes the multiplication (represented by $D$) separately in each principal direction.
For future reference, notice that "rotated" estimates $V^\prime \hat\beta $ are linear combinations of "rotated" responses $U^\prime y$.  The coefficients are inverses of the (positive) diagonal elements of $D$, equal to $d_{ii}^{-1}$.
###Covariance of the coefficient estimates
Recall that the covariance of the estimates is $$\text{Cov}(\hat\beta) = \sigma^2(X^\prime X)^{-1}.$$  Using the SVD, this becomes $$\sigma^2(V D^2 V^\prime)^{-1} = \sigma^2 V D^{-2} V^\prime.$$  In other words, the covariance acts like that of $k$ orthogonal variables, each with variances $d^2_{ii}$, that have been rotated in $\mathbb{R}^k$.
###The Hat matrix
The hat matrix is $$H = X(X^\prime X)^{-1} X^\prime.$$ By means of the preceding result we may rewrite it as $$H = (UDV^\prime)(VD^{-1}U^\prime) = UU^\prime.$$ Simple!
###Eigenanalysis (spectral decomposition)
Since $$X^\prime X = VDU^\prime U D V^\prime = VD^2V^\prime$$ and $$XX^\prime = UDV^\prime VDU^\prime = UD^2U^\prime,$$ it is immediate that

The eigenvalues of $X^\prime X$ and $XX^\prime$ are the squares of the singular values.
The columns of $V$ are the eigenvectors of $X^\prime X$.
The columns of $U$ are some of the eigenvectors of $X X^\prime$.  (Other eigenvectors exist but correspond to zero eigenvalues.)

SVD can diagnose and solve collinearity problems.
###Approximating the regressors
When you replace the smallest singular values with zeros, you will change the product $UDV^\prime$ only slightly.  Now, however, the zeros eliminate the corresponding columns of $U$, effectively reducing the number of variables.  Provided those eliminated columns have little correlation with $y$, this can work effectively as a variable-reduction technique.
##Ridge Regression
Let the columns of $X$ be standardized, as well as $y$ itself.  (This means we no longer need a constant column in $X$.)  For $\lambda \gt 0$ the ridge estimator is  $$\begin{aligned}\hat\beta_R &= (X^\prime X + \lambda)^{-1}X^\prime y \\
&= (VD^2V^\prime + \lambda\,1_p)^{-1}VDU^\prime y \\
&=  (VD^2V^\prime + \lambda V V^\prime)^{-1}VDU^\prime y \\
&= (V(D^2 + \lambda)V^\prime)^{-1} VDU^\prime y \\
&= V(D^2+\lambda)^{-1}V^\prime V DU^\prime y \\
&= V(D^2 + \lambda)^{-1} D U^\prime y.\end{aligned}$$
The difference between this and $\hat\beta$ is the replacement of $D^{-1} = D^{-2}D$ by $(D^2+\lambda)^{-1}D$. In effect, this multiplies the original by the fraction $D^2/(D^2+\lambda)$.  Because (when $\lambda \gt 0$) the denominator is obviously greater than the numerator, the parameter estimates "shrink towards zero."

This result has to be understood in the somewhat subtle sense alluded to previously: the rotated estimates $V^\prime\hat\beta_R$ are still linear combinations of the vectors $U^\prime y$, but each coefficient--which used to be $d_{ii}^{-1}$--has been multiplied by a factor of $d_{ii}^2/(d_{ii}^2 + \lambda)$.  As such, the rotated coefficients must shrink, but it is possible, when $\lambda$ is sufficiently small, for some of the $\hat\beta_R$ themselves actually to increase in size.
To avoid distractions, the case of one of more zero singular values was excluded in this discussion.  In such circumstances, if we conventionally take "$d_{ii}^{-1}$" to be zero, then everything still works.  This is what is going on when generalized inverses are used to solve the Normal equations.
Comment: the link to the notes is now broken
Comment: I understand what you mean now :)
I thought you meant you multiply the (projected) elements with $D^2/(D^2+\lambda)$. What you're (rightfully) saying is that you multiply the original (non regularized) $D^{-1}$ with the $D^2/(D^2+\lambda)$.

Both our algebras are correct. My reading, however, needs to be improved! My apologies!
Comment: @ whuber, oh , is it like this ? In the fitted value $\hat{y}$ we will use the coefficients estimates and as long as they are shrunk towards zero the same will happen for the fitted value.
Comment: (1) Part of the equation $UU^\prime=1_p$ asserts that the dot product of each column of $U$ with itself is $1$, whence each length is (by definition) $\sqrt{1}=1$. (2) $VV^\prime=1_p$ follows from the observation that $V$ is a rotation matrix, because this implies $V^{-1}$ is also a rotation matrix. Therefore $(V^{-1})^\prime(V^{-1})=1_p$. Plugging in $V^{-1}=V^\prime$ gives $VV^\prime=(V^\prime)^\prime V^\prime=1_p$.
Comment: When $X$ is symmetric, then by definition $$VDU^\prime=X^\prime=X=UDV^\prime.$$  Comparing the left and right sides immediately shows the diagonalization of a real symmetric matrix is a special case of the SVD and also suggests that in the SVD of a symmetric matrix, $U=V$.  That in fact is the case provided $X$ is nondegenerate--but proving it is not entirely elementary, so I won't go into details.
Comment: @ whuber, If I multiply the ridge solutions by $X$ (i.e. the fitted values) , I got something like this $$V(D^2/D^2+\lambda)U^TY,$$ So I become a little bit confused to what you have explained, could you please clarify that ?
Comment: @jeza I don't see how you can obtain that value when you multiply $X$ by $\hat\beta_R$: you don't seem to have followed the usual rules of matrix multiplication.
Comment: @whuber, my steps are : the ridge solutions is $$\hat{\beta}_{R}=(X^TX+\lambda I)^{-1}X^TY$$ , When multiply by $X$ , $$X\hat{\beta}_{R}=X(X^TX+\lambda I)^{-1}X^TY$$ we know that $(X^TX+\lambda I)^{-1}X^TY= V(D^2+\lambda I)^{-1}DU^TY$, thus the solutions after multiplying by $X$ will be $$U(D^2/(D^2+\lambda))U^TY$$
Comment: @jeza Okay, so what's the problem with that?
Comment: @  whuber, so both my solution and yours are right ? The first shrinks the estimates and the second shrinks the fitted values, because I become a little bit confused for both solutions.
Comment: @jeza Think of it this way: What happens to the fitted values as the parameter estimates shrink towards zero?
Comment: @whuber in my answer to amoeba's [Reversing ridge regression](http://stats.stackexchange.com/a/236232/127790) question I do something similar, but for the under-determined $X$ case ($n<p$). I have a note "... derivation omitted for the sake of brevity", and amoeba suggested to link here. Does this seem appropriate to you? (and/or is there better CV answer to point to?)
Comment: +1. A small terminological note: I would say that columns of $U$ are called "principal components" (as you wrote in the beginning) only if columns of $X$ have been centered.
Comment: @amoeba What would you call them otherwise? What would you call them if no nonzero constant vector were within the column space of $X$?
Comment: When you say "the denominator is obviously greater than the numerator", what is intended as denominator and numerator? -- is $D$ the numerator and $D^2+\lambda$ the denominator? If that was what you meant, how do we know that $D^2$ is not smaller than $D$ and/or that $\lambda$ isn't smaller than the difference between their diagonals?
Comment: If columns of $X$ are not centered, then I guess I'd simply say "left singular vectors". If I think of a 2D data cloud stretched from north to south but located far to the east, then the first principal component will be projection on the vertical axis, but the first left singular vector will be projection on the horizontal axis. It feels strange to refer to the latter as "principal component".
Comment: @whuber 1- I am not sure what this 'The columns of U have length 1' means ? 2- why vv' is equals I.
Comment: +1 For completeness sake, it would be good to define $\sigma^2$ when defining the covariance matrix of estimated parameters.
Comment: @ whuber, I have question. Now as I understood for any matrix $$X = UDV^\prime$$ . While, for the symmetric matrix it will be  $$X = UDU^\prime$$. I am confused between the two theorems, could you please clarify that.
Comment: A more elementary analysis of Ridge regression is at https://stats.stackexchange.com/a/297475/919.  @GeoMatt The only place I used the assumption $p \le n$ was in the initial description of the SVD.  So, when $p \gt n$, apply the SVD to $X^\prime$, transpose it to obtain $X=VDU^\prime$ (thereby switching the roles of $V$ and $U$), and proceed from there.
Comment: @jeza : the confusion is because whuber has a mistake in their presentation. $\hat{\beta}$ is given via the diagonal matrix $(D^2+\lambda I)^{-1} D$ which means you shrink by $d/(d^2+\lambda)$. The predictions, however, have a quadratic behaviour in the numerator. This is a mistake often seen in lectures notes online. I guess someone had a typo one day and it's just being copy pasted around.
Comment: @Jean-LucBouchot Would you be so kind as to indicate what the mistake might be so it could be corrected?  This answer was not copied or pasted from anywhere: it is original, as explained at the outset.  (Have you noted that the original singular value is $D^{-1}$? You seem to overlook that in your comment.)
Comment: The part " by (D2+λ)−1D. In effect, this multiplies the original by the fraction $D^2/(D^2+\lambda)$" is not correct. It should be a power 1 in the numerator.
However, if you're talking about the predictions, than indeed, you would get a power 2 but then you would get the matrix $U$ and $U'$ on the other side. This is obtained by left multiplying with the data matrix and re-using the SVD.
Comment: @Luc The "original" is $D^{-1},$ so your algebra appears to be incorrect.
Comment: Thanks -- I had to remove it, because the course notes are no longer on the Web.


Firebase App Check breaking the app on android

Question: I am trying to use Firebase App Check on android. Following the steps mentioned on this link prevents the app to work as intended and just by removing the library from dependencies and three-line code mentioned in the above link app starts working again as intended. I have a RecyclerView in the MainActivity that uses FirebaseRecyclerAdapter from the Firebase UI database library and it just stops working. I don't get the necessary items in my RecyclerView. I have the below code in my Application class's onCreate method. What should I do to make Firebase App Check work without breaking the functionality of the app?
 FirebaseApp.initializeApp(this);
        FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
        firebaseAppCheck.installAppCheckProviderFactory(
                SafetyNetAppCheckProviderFactory.getInstance());

Answer: Also install DebugAppCheckProviderFactory
 FirebaseApp.initializeApp(context)
        val firebaseAppCheck = FirebaseAppCheck.getInstance()
        firebaseAppCheck.installAppCheckProviderFactory(
            DebugAppCheckProviderFactory.getInstance())
        firebaseAppCheck.installAppCheckProviderFactory(
            SafetyNetAppCheckProviderFactory.getInstance())

NOTE : Make sure your Firebase Libraries are upto updated.. then try again.. it should work.


Independence of variables in expectation

Question: I know that $X$ and $Y$ are independent, and have an expression $$E[I(Y>X)*I(X>2)].$$
Is the independence between $X$ and $Y$ enough to say that $$E[I(Y>X)*I(X>2)] = E[I(Y>X)]*E[I(X>2)]?$$
This does not seem too difficult, yet I am having trouble justifying or disproving the equality.
$I()$ is the standard indicator function.
Thank you!
Comment: What is $I(\cdot)$?
Comment: Just clarified in the question, thanks @mhdadk
Comment: Let each of the ordered pairs $(1,0),$ $(1,2),$ $(3,0),$ $(3,2)$ have probability $1/4$ and suppose $X$ is the first component and $Y$ is the second component.  You can verify (easily) that $X$ and $Y$ are independent and that $E[I(Y\gt X)]=1/4,$ $E[I(X\gt 2)]=1/2.$  However, the event $Y\gt X \gt 2$ is empty, whence it has zero probability: the equation doesn't work out. I found this counterexample by drawing graphs of these events in Cartesian coordinates.
Comment: @mhdadk Indicator function. `I(Y > X)` is 1 iff Y > X, and 0 if otherwise.
Answer: No, it's not enough. Although $X$ and $Y$ are independent, the events $\{X<Y\}$ and $\{X>2\}$ are not. Let's say $Y$ is a constant random variable and is equal to $2$ with probability $1$. Then, $$E[I(X<2)I(X>2)]=0$$
But, $E[I(X<2)]E[I(X>2)]$ depends on $X$.


fragment disappear when came back from tab 3

Question: I have an Activity with a ViewPager containing Fragments.
the first fragment(book) create a nested fragment(singlebook) when click a button on it.
Problem is that the first fragment and its component are gone when i came back from fragment3. I think it is related onSaveState or onDestroy.
I've tried Override onDestroy and used childFragmentManager on parent fragment(book) but it doesn't add to back stack.
MainActivity.java :
public class MainActivity extends FragmentActivity {

ViewPager viewPager;
MyPagerAdapter adapter;
public FragmentManager fragmentManager = getSupportFragmentManager();

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tl_tabs);
    viewPager = (ViewPager) findViewById(R.id.vp_12);

    Fragment[] fragments = new Fragment[3];
    fragments[0] = new Book();
    fragments[1] = new Search();
    fragments[2] = new Study();
    adapter = new MyPagerAdapter(fragmentManager, fragments);
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
}
}

class MyPagerAdapter extends FragmentStatePagerAdapter {

    private Fragment[] fragments;

    public MyPagerAdapter(FragmentManager fm, Fragment[] fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return fragments[0];
            case 1:
                return fragments[1];
            case 2:
                return fragments[2];
            default:
                return fragments[0];
        }
    }

    @Override
    public int getCount() {
        return fragments.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position) {
            case 0:
                return "단어장";
            case 1:
                return "단어 검색";
            case 2:
                return "내 정보";
            default:
                return "";
        }
    }
}

Book.java :
public class Book extends Fragment {

FragmentManager fragmentManager;
private Button btn;
private EditText editText;

public Book() {
    // Required empty public constructor
}
@Override
public View onCreateView( LayoutInflater inflater,  ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_book, container, false);
    btn = (Button) view.findViewById(R.id.btn_toQuiz);
    editText =(EditText) view.findViewById(R.id.edit_text);
    ViewPager viewPager = (ViewPager) container;

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fragmentManager = getChildFragmentManager();

            fragmentManager.beginTransaction()
                    .add(R.id.fl_BookContainer,new singleBook(),"TAG")
                    .addToBackStack(null)
                    .commit();

        }
    });
    return view;
}

}

Comment: Did you try to use of parent FragmentManager to add the new fragment?
Comment: have you tried viewPager.setOffscreenPageLimit(3); where 3 is your number of pages
Comment: It works! thank you very much!
Comment: @MrazJason if you found this comment is helpful then mark as an upvote. it will help others also
Comment: how to upvote? I'm very new this site. I really appreciate again. where can i find upvote mark?...
Answer: By default FragmentStatePagerAdapter can only create two fragments and when you go to the third fragment it will remove the first one. so that the solution is you need to call setOffscreenPageLimit method save all fragments which added into pager adapter when set adapter into view pager.
Fragment[] fragments = new Fragment[3];
fragments[0] = new Book();
fragments[1] = new Search();
fragments[2] = new Study();
adapter = new MyPagerAdapter(fragmentManager, fragments);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(fragments.size());
tabLayout.setupWithViewPager(viewPager);

Answer: viewPager = (ViewPager) findViewById(R.id.vp_12);

adapter = new MyPagerAdapter(fragmentManager, fragments);
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);

To
viewPager = (ViewPager) findViewById(R.id.vp_12);
viewPager.setOffscreenPageLimit(3);

adapter = new MyPagerAdapter(getChildFragmentManager(), fragments);
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);


[function.unserialize]: Error at offset

Question: I initially came across this error while I migrated my site from local server to live server.
Notice: unserialize() [function.unserialize]: Error at offset 83 of 108 bytes in variable_initialize() (line 935 of /home/ebemorgi/kog.church/includes/bootstrap.inc).

I followed #9 from this drupal post , then I found out that
the variable causing the issue was authorize_filetransfer_connection_settings_ftp.
I deleted the above variable but that hung up my whole website.
Then I deleted the content in the variable table and re-populated it.
Initially the error didn't popup but when I tried installing a new module at the live server the error
popped up with an new offset value. Again the villain is authorize_filetransfer_connection_settings_ftp.
The error which I am now receiving is :
Notice: unserialize() [function.unserialize]: Error at offset 32 of 121 bytes in variable_initialize() (line 935 of /home/ebemorgi/kog.church/includes/bootstrap.inc).

I have made sure that I have updated the ftp details in my mysql dump file before importing it to live DB.
I'm not sure if this issue is related to Core, but I don't honestly know under which product to list it.
Any help is appreciated.
Answer: It looks as though you have invalid serialized data for that authorize_filetransfer_connection_settings_ftp variable.
If saving to the db manually, make sure you have serialized the data prior to db storage.
Some ways to set the variable include
variable_set('authorize_filetransfer_connection_settings_ftp', $value);
and
drush vset authorize_filetransfer_connection_settings_ftp 'value'
The former is a bit easier if setting an array value, otherwise you can do something like this with Drush:
drush php-eval 'variable_set("authorize_filetransfer_connection_settings_ftp",array("key1" => "value1", "key2" => "value2"));'

[function.unserialize]: Error at offset


How to evaluate $\lim\limits_{p \rightarrow \infty} \left(\sum_\limits{i=1}^n \left|x_i-y_i\right|^p\right)^{\frac{1}{p}}$

Question: I'd like to know why $\lim\limits_{p \rightarrow \infty} \left(\sum_\limits{i=1}^n \left|x_i-y_i\right|^p\right)^{\frac{1}{p}} = \max\limits_{1\le i \le n} \left| x_i-y_i\right|$ for $\mathbf{x},\mathbf{y}\in \mathbb{R}^n$.
So I started by checking a simpler expression:
$\lim\limits_{x\rightarrow \infty} ((6-3)^x+(5-1)^x)^{\frac{1}{x}}=4$
I don't know how to get 4. The expression inside the parenthesis is indeterminate $(\infty + \infty)$ and I don't know of any way to rewrite it so that I can remove the exponents.
Answer: $$
\lim_{x\to \infty} ((6-3)^x+(5-1)^x)^{1/x}=\lim_{x\to \infty} (3^x+4^x)^{1/x}=\lim_{x\to \infty}4\Bigl(\Bigl(\frac{3}{4}\Bigr)^x+1\Bigr)^{1/x}=4.
$$
For the general case, let $a_i=|x_i-y_i|$ and assume without loss of generality that $\max a_i=a_1>0$. Then
$$
\Bigl(\sum_{i=1}^na_i^p\Bigr)^{1/p}=a_1\Bigl(\sum_{i=1}^n\Bigl(\frac{a_i}{a_1}\Bigr)^p\Bigr)^{1/p}.
$$
Since
$$
1\le\sum_{i=1}^n\Bigl(\frac{a_i}{a_1}\Bigr)^p\le n,
$$
$$
\lim_{p\to\infty}\Bigl(\sum_{i=1}^n\Bigl(\frac{a_i}{a_1}\Bigr)^p\Bigr)^{1/p}=1.
$$


Solving $ \int_{0}^{2\pi} e^{-x} \lvert \sin x\rvert,dx $

Question: Since it involves an absolute value, I assume I need to split it into two cases? For $ 0 \le x \le \pi $
$$ \int_{0}^{\pi}  e^{-x} \sin x\,dx $$
and for $ \pi \le x \le 2\pi $
$$ \int_{\pi}^{2\pi}  e^{-x} \cdot(-\sin x)dx$$
I don't really know where to go from here. I've tried to integrate both of these, but I always end up with the wrong answer.
Comment: See http://math.stackexchange.com/questions/540295/integrate-eax-sinbx
Comment: Integration by parts!!!!!!!!!!!!!!
Answer: I want to point that the integrals
$$I = \int e^{ax}\cos{bx} \qquad J = \int e^{ax}\sin{bx}$$
can be solved by using Euler's formula, as follows
Consider the integral
$$A = \int e^{ax}\cos{bx}dx  -i\int e^{ax}\sin{bx}dx = \int e^{(a+ib)x} dx$$
Therefore $I = Re\{A\}$ and $J = Im\{A\}$
This approach is suitable for this problem
Answer: Both integrals are the same except the minus sign and the end points of the intervals. Let's use integration by part to find the anti-derivative for $e^{-x}\sin x$.
$\displaystyle \int e^{-x}\sin xdx = -\displaystyle \int \sin xd(e^{-x}) = -\left(\sin xe^{-x} - \displaystyle \int e^{-x}\cos xdx\right) = -\sin xe^{-x} + \displaystyle \int e^{-x}\cos xdx = -\sin xe^{-x} - \displaystyle \int \cos xd(e^{-x}) = -\sin xe^{-x} - \cos xe^{-x} - \displaystyle \int \sin xe^{-x}dx \Rightarrow \displaystyle \int \sin xe^{-x}dx = \dfrac{-\sin xe^{-x} - \cos xe^{-x}}{2}= F(x)$.
From here, to evaluate each integral, we use the FTC: $\displaystyle \int_{0}^{\pi} e^{-x}\sin xdx = F(\pi) - F(0)$, and similarly for the second one.
Comment: Seems I need to trust my math skills a bit more. That's the same answer I got. I must be doing something wrong when evaluating the integrals.
Comment: Keep practice like I did....
Comment: Definitely. Thanks for the help!


What methods are there to configure puppet to serve resources for multiple environments?

Question: I seem to come across two ways for using puppet in multiple environments:
1) Install a puppetmaster in each environment and only update the recipes from source control for that environment when ready to deploy the recipes in that environment.
2) Use one puppetmaster and use a variable in the puppet.conf of each client to specify the environment and then in the puppetmaster specify a different modulepath for each environment and each of those paths is updated to the branch of the recipe repository intended for that environment (e.g. dev, staging, production).
Only running one puppetmaster seems like it is one less piece of infrastructure to keep running but there is some additional complexity in the configuration.
Are there additional pros or cons to one of these methods or something which I'm missing entirely?
Answer: Option #1 won't scale. It'll be a pain to manage. Puppet supports environments for this very reason :)
Let a single puppetmaster serve multiple environments, each with their own manifest and modulepath directives. It's a really common approach that a lot of people use. Also remember that:

Puppet can manage your puppetmasterd.conf and puppet.conf config files.
Depending on how you call puppetd you can also use the --environment argument.
Comment: Do be aware of the limitations of Puppet's multi-environment support though.  95% of the time it's fine, but review the [documented caveats](http://docs.puppetlabs.com/guides/environment.html#caveats).  In particular, custom types/providers and master-side functions won't work properly and the environment has to be specified on the client, the ENC environment doesn't work properly.
Answer: You can call puppetd from the command line with --environtment. But that can be a pain to manage if you want to move many servers from one Env to another.
The foreman project acts an "external nodes" interface and lets you assign environments to hosts/hostgroups from a web front end. You can then have different modulepaths for different hosts. You can even change Environments for nodes from the interface, letting you move modulepaths from Dev->Prod, or however you want.
Answer: I think you should take a look at puppet tags
Comment: tags solves a different problem of only running a specific subset of modules instead of every module in the cache for that node.  As far as I understand puppet the concept of environments is entirely different.
Answer: Using puppet environments has a couple of drawbacks:

you can reliably set puppet environment only from agent — this might create some vulnerabilities if you store your passwords etc cleartext in your modules/templates.
custom functions are loaded only from a single environment which is used as default on puppetmaster — this means you'll be unable to keep different version of custom functions (if you use them) on different environments.

In a company I'm working for we are going to use 2 puppetmaster servers — one for production, and one for the rest of environments.
To ease managing puppet modules on several puppetmasters you can keep your modules in VCS like git and deploy new versions of them to puppetmasters through capistrano etc.
If you need several environments not to do versioning of your puppet modules but for providing different data to different nodes based on the environment (different variables, node classes etc) there are some more options:
* inherit nodes of different environments from basic "environment" nodes
* provide a per-environment set of parameters and classes for each node through ENC (Puppet Dashboard, Foreman) in which you create a group for each environment with needed params set and add your nodes to those.
* create a custom fact or a custom function returning your environment (based on local file/ AWS tag/DB query/whatever) and use that fact to serve different data in your manifests using conditionals/extlookup/hiera.


How to rearrange table in pandas in a format suitable for analysis in R?

Question: In pandas:
df = pd.DataFrame({'row1':['a','b','a','a','b','b','a','b','b','a'], 'row2':['x','x','y','y','y','x','x','y','x','y'],'col':[1,2,1,2,2,1,2,1,1,2],'val':[34,25,22,53,33,19,42,38,33,61]})

p = pd.pivot_table(df,values='val',rows=['row1','row2'],cols='col')

col         1   2
row1 row2
a    x     34  42
     y     22  57
b    x     26  25
     y     38  33

Is it possible to reshape the pivot table in a data frame suitable for analysis in R?, something of the type:
  row1 row2 col val
    a   x   1   34
    a   y   1   22
    a   x   2   42
    a   y   2   57
    b   x   1   26
    b   y   1   38
    b   x   2   25
    b   y   2   33

Answer: One way is to use stack:
In [11]: p.stack()
Out[11]:
row1  row2  col
a     x     1      34
            2      42
      y     1      22
            2      57
b     x     1      26
            2      25
      y     1      38
            2      33
dtype: int64

This doesn't give a name attribute, so you have to set it as you reset the index:
In [12]: df = p.stack().reset_index(name='val')

In [13]: df
Out[13]:
  row1 row2  col  val
0    a    x    1   34
1    a    x    2   42
2    a    y    1   22
3    a    y    2   57
4    b    x    1   26
5    b    x    2   25
6    b    y    1   38
7    b    y    2   33

Comment: Just throwing this out there, but if this is faster than `melt` it might be worth implementing `melt` in terms of this.
Comment: @PhillipCloud As things get big, I'm not sure it's much faster... a little surprising how *vast* majority of time is spent doing the reset_index.
Answer: You can do:
>>> pd.melt(p.reset_index(), id_vars=['row1', 'row2'],
            var_name='col', value_name='val')
  row1 row2  col  val
0    a    x    1   34
1    a    y    1   22
2    b    x    1   26
3    b    y    1   38
4    a    x    2   42
5    a    y    2   57
6    b    x    2   25
7    b    y    2   33

Which resets the index, making row1 and row2 columns:
>>> p.reset_index()
col row1 row2   1   2
0      a    x  34  42
1      a    y  22  57
2      b    x  26  25
3      b    y  38  33

And then pd.melt melts in all columns except row1 and row2 (id_vars parameter) into one column named val (value_name parameter) and keeps the information to which column they belonged in the column named col (var_name parameter).
Comment: Ah ha! I knew you'd eventually warm up to `melt`. Pun (sort of) intended :/
Comment: @PhillipCloud :D I use it. It's just that I hesitate to use it on SO since I don't want to melt the OP's brain :P But in this case it was really obvious... It should have been named unpivot :-/
Comment: Incidentally, we had a discussion about that over at pandas' github ([issue #4150](https://github.com/pydata/pandas/pull/4150)). I agree with you, but `melt` is taken from R (specifically, the `reshape2` package by Hadley Wickham) and it has been in `pandas` for awhile I believe, so we decided to stick with it. `pivot` in R is called `dcast` (`data.frame` `cast`).
Comment: @PhillipCloud I think we should *add* unpivot as a melt alias (or vice versa). Then everyone's a winner. ... It seems strange to use melt but not (d)cast terminology from R.
Comment: @AndyHayden I wholeheartedly agree :)


Wrong results when appending vector to itself using copy and back_inserter

Question: Inspired by this question, asking how to append a vector to itself, my first thought was the following (and yes, I realize insert is a better option now):
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> vec {1, 2, 3};
    std::copy (std::begin (vec), std::end (vec), std::back_inserter (vec));

    for (const auto &v : vec)
        std::cout << v << ' ';
}

However, this prints:
1 2 3 1 * 3

The * is a different number every time the program is run. The fact that it's only the 2 being replaced is peculiar, and if there actually is an explanation for that, I'd be interested to hear it. Continuing, if I append to a different vector (a copy of the original), it outputs correctly. It also outputs correctly if I add the following line before the copy one:
vec.reserve (2 * vec.size());

I was under the impression std::back_inserter was a safe way to add elements onto the end of a container, despite not reserving memory beforehand. If my understanding is correct, what's wrong with the copying line?
I assume it's nothing to do with the compiler, but I'm using GCC 4.7.1.
Answer: std::back_inserter creates an inserting iterator that inserts elements into a container.  Each time this iterator is dereferenced, it calls push_back on the container to append a new element to the container.
For a std::vector container, a call to push_back where v.size() == v.capacity() will result in a reallocation:  a new array is created to store the contents of the vector, its current contents are copied into the new array, and the old array is destroyed.  Any iterators into the vector at this time are invalidated, meaning they can no longer be used.
In your program, this includes the input range defined by begin(vec) and end(vec) from which the copy algorithm is copying.  The algorithm continues to use these iterators, even though they are invalidated, thus your program exhibits undefined behavior.

Even if your container had sufficient capacity, its behavior would still be undefined:  the specification states that, upon insertion, "if no reallocation happens, all the iterators and references before the insertion point remain valid" (C++11 §184.108.40.206/1).
The call to push_back is equivalent to insertion at the end, so the end iterator (std::end(vec)) that you passed into std::copy is invalidated after a single call to push_back.  If the input range is nonempty, the program therefore exhibits undefined behavior.

Note that the behavior of your program would be well-defined if you used a std::deque<int> or a std::list<int>, because neither of those containers invalidates iterators when elements are appended.
Comment: @jrok, Interesting question; it could be worth asking. "This question, which was inspired by this other question, which was inspired by yet another question..."
Comment: It is an interesting issue, BTW. "Iterator" is a more abstract concept than "pointer", so it is expected that end iterator might get invalidated by insertion even without reallocation. But what if I used pointers `&vec.front()` and `&vec.back() + 1` as the input range (assuming non-empty `vec`)? The standard does not guarantee the persistent validity of pre-computed `&vec.back() + 1` pointer, since it is a "reference" to or past the insertion point. But we know that this is a purely conceptual invalidation. In practical sense it should (and will) remain valid.
Comment: `std::copy_n(std::begin(vec), vec.size(), std::back_inserter(vec));` has got well defined behaviour, right? It doesn't rely on end iterator as range limit.
Comment: @jrok:  Yes, that has well-defined behavior.  @AndryT has an interesting point, too:  the restriction I quoted only names iterators and references, not pointers.  This is curious, and in my opinion is probably a defect in the specification, but if it is correct, then `copy(&vec.front(), &vec.back() + 1, back_inserter(vec));` would also work, assuming sufficient capacity.
Comment: @AndreyT:  I agree, it's a "conceptual" invalidation, and I would expect the code to work in practice.  But, I could imagine an overly pedantic debug iterator implementation checking for this invalidation (though, the Visual C++ iterator debugging does not complain).  I think that the omission of "pointer" from the list of invalidated things is an error of omission:  I can't imagine why a reference would be invalidated but not a pointer.
Comment: @James McNellis: I presumed that the term "references" in this case is not restricted to mean C++ references only, but rather used in a more broad sense: all "pointer-like" access paths to vector elements, i.e. includes pointers as well. I could be wrong though.
Comment: @JamesMcNellis, It just so happened that I got to use jrok's gem of code in another answer and it turns out that the standard says it does `*(first + i)` every iteration, so it will use the invalidated iterator. Just thought I'd let you know in case you remember it and go to use it.
Comment: @jrok, See above comment.
Comment: @chris if with "gem" you mean the code in my previous comment - yes, it assumes capacity to be at least twice the size, otherwise it indeed uses an invalidated iterator. Care to link me to the other answer you mentioned?
Comment: @jrok, Oh, then I took it the wrong way. Here you go: http://stackoverflow.com/questions/17636690/nice-way-to-append-a-vector-to-itself. Ben's answer is basically that, then.


Qual a melhor forma de rodar uma aplicação java como Serviço no Windows (32 ou 64 bits)?

Question: O que é necessário fazer para rodar um aplicativo Java (sem interface visual) como serviço do Windows. O programa usa basicamente a API de Filesystem e a API de rede (java.net, java.io e java.nio)
Se for necessário fazer build a partir dos fontes gostaria de usar GNU C++ em vez de Visual Studio.
Alguma dica ?
Answer: De acordo com uma pergunta semelhante no stackoverflow.com você pode utilizar ferramentas do Apache Commons Deamon para realizar isso.
Em específico existe o Procrun que é um conjunto de aplicativos que permite que você possa empacotar aplicações Java como serviços rodando em plano de fundo no Windows.
Eu consigo imaginar outra possibilidade, em .Net criar um programa como serviço do windows é uma tarefa 'trivial' e existem algumas formas de integrar bibliotecas Java para serem utilizadas com .Net, você pode combinar isso e criar um serviço .Net que utilize suas funções desenvolvidas em Java.
Answer: O Install4j cria instaladores para aplicações Java, que por sua vez podem ser instaladas como serviços no Windows. É um produto pago, mas permite gerenciar vários detalhes de uma instalação/configuração de serviço. Fica a dica.


Syntax Error OledbException delete statement

Question: I can't fugure out what my syntax error is here. Anyone spot it? Or am I going about this all wrong?
Dim myCommand As New OleDb.OleDbCommand("delete * from Team where intPlayerNo='" & txtUniformNo.Text & "'_ strFirstName='" & txtFirstName.Text & "'_ strLastName='" & txtLastName.Text & "'_ strParentName='" & txtParent.Text & "'_ strAddress='" & txtAddress.Text & "'_ strCity='" & txtCity.Text & "'_ strState='" & txtState.Text & "'_ strZipCode='" & txtZip.Text & "'_ strPhone='" & txtPhone.Text & "'_ intAge='" & txtAge.Text & "'", myConnection)

Comment: ALSO DB tables usually have something called a *Primary Key* which uniquely identifies each record so that you do not have to specify a dozen fields to delete one record.  One typo in a textbox and your SQL fails.  ALSO, there are a lot of underscores there that are in the wrong place no matter whether they are part of the column name or are supposed to be line continuation characters
Comment: @Steve: Without parameterised queries, _compromised_. ;)
Comment: do you really have columns named `strFirstName` and `intAge`??
Comment: Wrong SQL syntax (DELETE FROM...) and not using SQL Parameters.  Also possibly doing some data type conversions in the SQL
Comment: What is the database?
Comment: Access. When I tried to delete using only the primary key, I got an error telling me there were "missing parameters" in my statement.
Comment: My question is related to the `DELETE *` that while is not a correct SQL DELETE statement is nonetheless accepted if the database is MS-Access. Thus the syntax error stems from the WHERE statement and the missing AND/OR between conditions. Parameterized query while of uttermost importance is secondary in relation to the Syntax Error
Comment: @RamziMuasher what is then your primary key?
Answer: A delete statement is Delete From Team Where...
You would be best advised to use Parameterized Queries to avoid SQL Injection Attacks
Answer: Your syntax is wrong because you have multiple WHERE conditions but you don't use the correct AND/OR to connect the conditions together. Of course the DELETE * FROM table syntax is accepted only by MS-ACCESS, but the correct SQL syntax is DELETE FROM (without the *) and is accepted also by Access so it is better to use the standard.
Said that you need to use a parameterized query in your command and not a string concatenation
Dim cmdText = "DELETE FROM Team " & _
              "WHERE intPlayerNo = @playerNo AND " & _
              "strFirstName = @firstName AND " & _
              ".... and so on with the other fields "
Dim myCommand As New OleDb.OleDbCommand(cmdText, myConnection)
myCommand.Parameters.Add("@playerNo", OleDbType.VarWChar).Value = txtUniformNo.Text
myCommand.Parameters.Add("@firstName", OleDbType.VarWChar).Value = txtFirstName.Text
... continue with the other parameters required by the WHERE statement
myCommand.ExecuteNonQuery()

Using parameters will keep your code safe from Sql Injection and avoid syntax errors if some of your textbox values contains a single quote.
Keep in mind that if your table has a primary key, then your WHERE could simply reduced to only the PrimaryKeyFieldName = @Value


How to receive judgement result from model of Custom Vision with JavaScript

Question: I want to receive judgement result from model of Azure Custom Vision by using JavaScript.
I changed JavaScript code that this site has.
https://southcentralus.dev.cognitive.microsoft.com/docs/services/eb68250e4e954d9bae0c2650db79c653/operations/58acd3c1ef062f0344a42814
But I can't.
What is wrong with my code?
I changed IterationId, application, url, content-Type, Prediction-key, and data.
These part are enclosed with {} in the code below.
<code><!DOCTYPE html>
<html>
<head>
    <title>Human</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js">
</script>
</head>
<body>

<script type="text/javascript">
$(function() {
    var params = {
        // Request parameters
        "iterationId": "{Iteration id that showed in Performance Page}",
        "application": "{My Project name of Custom Vision}",
    };

    $.ajax({
        url: "{url that showed in "How to use the Prediction API"}" + $.param(params),
        beforeSend: function(xhrObj){
            // Request headers
            xhrObj.setRequestHeader("Content-Type","application/octet-stream");
            xhrObj.setRequestHeader("Prediction-key","{my prediction key that showed in "How to use the Prediction API"}");
        },
        type: "POST",
        // Request body
        data: "D:\some name\some name\image.jpg",
    })
    .done(function(data) {
        alert("success");
    })
    .fail(function() {
        alert("error");
    });
});
</script>
</body>
</html>
</code>
Of course, I expected showing "success".
But, the actual output is "error"......
Comment: What is the error you receive?
Comment: Exception message doesn't appear.
"error" means invoking this code.

.fail(function() {
        alert("error");
    });
Answer: Change your code to see what is the error that you get back:
(Note the new "error" parameter to the request)
<code>$.ajax({
    url: "{url that showed in "How to use the Prediction API"}" + $.param(params),
    beforeSend: function(xhrObj){
        // Request headers
        xhrObj.setRequestHeader("Content-Type","application/octet-stream");
        xhrObj.setRequestHeader("Prediction-key","{my prediction key that showed in "How to use the Prediction API"}");
    },
    type: "POST",
    // Request body
    data: "D:\some name\some name\image.jpg",
    error: function(xhr,status,error) {
        // >>>>>>>>>>>> CHECK HERE THE ERROR <<<<<<<<<<<<
    }
})
.done(function(data) {
    alert("success");
})
.fail(function() {
    alert("error");
});
</code>
Once you have the error, it would be easier to help you.
Answer: When I changed URL that this site has(https://southcentralus.dev.cognitive.microsoft.com/docs/services/eb68250e4e954d9bae0c2650db79c653/operations/58acd3c1ef062f0344a42814) in my code, I can get Success message.
And, I also write
    processData: false,
    contentType: false,
in ajax in my code