Prior in variational autoencoders

Question: I am currently dealing with variational autoencoders where I've read the original paper "An introduction to variational Bayes" from Kingma and Welling. I am currently still a little confused about the choice of the prior $p(z)$. For my understanding the variational autoencoder has the three possible distribution families we can choose:

The family of distributions for the encoder $q_\phi(z|x)$ parametrised by $\phi$.
The family of distribtuions for the decoder $p_\theta(x|z)$ parametrised by $\theta$.
The family of prior distributions $p_\theta(z)$.

Now for my understanding and also according to this notation used, the parameters for the decoder and the prior have to be the same, since overall we want to compute $p_\theta(x,z)$ with a neural network right?
Then, how is it possible to choose $p_\theta(z)=\mathcal{N}(0,I)$ while at the same time optimising the parameters $\theta$ computed by the decoder network?
As a third question: if not restricting the prior to be a standard normal distribution, how do we learn the parameters of the prior $p_\theta(z)$?

Edit: I am aware, that in practice we choose the prior distribution to be standard normal. But why are we allowed to do so?
For my understanding, the whole model (looking at the variational autoencoder as one model) we want to fit $p_\theta(x,z)=p_\theta(x|z) p_\theta(z)$ meaning the parameters $\theta$ are coming from the same parameter space for the decoder and the prior distribution. But when on the one hand fixing the prior distribution and on the other hand computing the parameters $\theta$ for the decoder, then we are kind of violating this assumption aren't we?
Answer:
Now for my understanding and also according to this notation used, the parameters for the decoder and the prior have to be the same, since overall we want to compute $p_\theta(x,z)$ with a neural network right?

Prior is the distribution for the parameters. Parameters come "from" the prior.

Then, how is it possible to choose $p_\theta(z)=\mathcal{N}(0,I)$ while at the same time optimising the parameters $\theta$ computed by the decoder network?

What you are optimizing is the posterior probability, so likelihood times prior. You choose the prior a priori, plug-in to the Bayes theorem and optimize. When optimizing you consider the prior.

As a third question: if not restricting the prior to be a standard normal distribution, how do we learn the parameters of the prior $p_\theta(z)$?

You assume them based on your prior knowledge, on what you can expect the distributions of the parameters to be before seeing the data. There's nothing special about Gaussians here. Moreover, the above prior choice is dictated rather by convenience than any deep mathematical argument.
It may be easier to understand on practical example, Keras's blog has a nice tutorial on autoencoders.
Comment: Thank you very much for your fast reply. I am aware that in practice we fix the prior distribution, but my question is: why are we allowed to do so? In the mathematical derivations of variational autoencoders, for my understanding we want the whole model to fit $p_\theta(x,z)=p_\theta(x|z)p_\theta(z)$ where here we indicate that also the parameters $\theta$ which are the parameters to be learned indicate the prior distribution over the latent variables $w$.
Comment: @Sidonie Here's an example of how to use a non-Gaussian prior distribution for the latent states https://stats.stackexchange.com/questions/471645/reparametrization-trick-with-non-gaussian-distributions/473721#473721
Answer: In the definition of the VAE, we make the ansatz that we can factor the joint density as $P^\theta_{x,z} = P^\theta_{x|z} P_z$. The the prior $P_z$ is any distribution specified by the experimenter. The hope is that the training procedure adjusts $\theta$ so that the decoder $P^\theta_{x|z}$ uses the entropy in $P_z$ to model $P_x\approx P^\theta_x$.
The training procedure of VAEs should find parameters $\hat\theta$ that cause the marginal of $z$ to approximate the specified prior, i.e. $\int_{dx} P^{\hat\theta}_{x,z} \approx P_z$ ...

If training converges to a meaningful optimum;
If the network has sufficient capacity, such that the approximation loss $D[ Q^\phi_{z|x} \| P_{z|x}]$ vanishes and the evidence lower-bound becomes tight;
If the encoding model used for amortized inference is expressive enough to predict the correct distribution parameters $\phi$ for each $x$ that achieve this minimal $D[ Q^\phi_{z|x} \| P_{z|x}]$.

If these conditions hold, one expects the average of $Q_{z|x}$ over the training data $\mathcal X = \{x_1,..,x_L\}$ to approximate the specified prior $P_z$, i.e. $\langle Q_{z|x} \rangle_{\mathcal X}\approx P_z$. (And I suppose this can be interpreted as reflecting the true marginal $P^\theta_{z}$ if $\mathcal X$ are a good sample of the true $P_x$).
In my experience, if $\langle Q_{z|x} \rangle_{\mathcal X}$ and $P_z$ differ substantially, the following might be true:

Decoder or encoder training failed (numerical problem, vanishing gradients, mode collapse, finding a poor local optimum)
The encoder/decoder lack capacity to map $z$ ↔ $x$
The latent space lacks capacity to describe all variability in $P_x$

I'm not sure whether adjusting the strength of the prior, or adding other penalties, could influence $Q_{z|x}$, to cause the above to be false. In the usual VAE the latents are Gaussian $z\sim\mathcal N(0,\boldsymbol I)$, so the decoding weights should be able to adjust to make any Gaussian distribution on the latents align with this (in principle and with the aforementioned caveats).
For Gaussian VAEs you can get the marginal $\langle Q_{z|x}\rangle_{\mathcal X}$ using the law of total variance. The mean for each component is the average over the training data $\mu = \langle \mu(x) \rangle_{x\in\mathcal X}$. The variance for each component is $\sigma^2 = \langle \sigma^2(x) \rangle_{x\in\mathcal X} + \operatorname{var}_{x\in\mathcal X}[\mu(x)]$.
I've seen this differ substantially from $P_z$ early in training. Since everything is a diagonal Gaussian, you could adjust the adjacent encoder/decoder weights/biases so that this matches $P_z$. I tried this and it may slightly accelerate the early phases of training.
All of this is more subtle in models where the latent $P_z$ is itself trainable. For example, The restricted Boltzmann machine uses Bernoulli latents with a parameter $p$. In this case the "prior" should adapt during training, since you can't guarantee that your latents will have the right distribution of feature frequencies to match the data a priori. There isn't much point is explicitly parameterize $P_z$ in the Gaussian VAE, since these parameters can be absorbed into the adjacent encoding/decoding layers.
TLDR:

When deriving the loss function, it is valid to assume $P^\theta_z = P_z$, where $P_z$ is fixed, inasmuch as we are searching for a model where this is true by construction. Minimizing this loss, when successful, will make $P^\theta_z \approx P_z$.
If your prior needs to adjust parameters that can't be handled by backprop + reparameterization trick, there may be good reason to update $P_z \gets P^\theta_z \approx \langle P^\theta_{z|x}\rangle_{\mathcal X}$ (or some approximation) during training. This, at least, was my understanding of the wake-sleep algorithm.
Sometimes, we don't find a model where $P^\theta_z \approx P_z$. Free energy is not minimized and we should be suspicious of the model. Maybe then, as you say, one should use $\langle P_{z|x}\rangle_{\mathcal X}$ to sample $z$.

I have more experience with restricted Boltzmann machines, where the generative $P_z$ does change during training. As far as I can tell, this shouldn't need to happen in VAEs with Gaussian latent spaces trained via backpropagation. Experts might want to weight in—I may have made some errors.

Prior in variational autoencoders


How many solutions are there to a congruence like $x^2 + x + 1 \equiv 0$ mod $p$ in $\mathbb{Z}/_{p}\mathbb{Z}$ where $p$ is an odd prime number?

Question: How can I find the number of solutions to a congruence like $x^2 + x + 1 \equiv 0$ mod $p$ in $\mathbb{Z}/_{p}\mathbb{Z}$ where $p$ is an odd prime number?
Why is that the case?
Comment: Why is what the case?
Comment: Why is it the case that that is the number of solutions? is what I meant to ask.
Answer: There is no solution if $p=2$, so we can take $p$ to be an odd prime. The congruence is equivalent to $4x^2+4x+4\equiv 0\pmod{p}$, or equivalently to $(2x+1)^2\equiv -3\pmod{p}$.
The congruence $y^2\equiv -3\pmod{p}$ has either $0$ solutions or $2$ solutions. To determine the primes $p$ for which there are $2$, calculate the Legendre symbol $(-3/p)$ in the usual way.
Remark: Essentially the same idea works for any quadratic congruence modulo an odd prime. Consider the congruence $ax^2+bx+c\equiv 0$, where $p$ does not divide $a$. The congruence is equivalent to $4a^2x^2+4abx+4ac\equiv 0\pmod{p}$, which can be rewritten as $(2ax+b)^2\equiv b^2-4ac\pmod{p}$.
If $b^2-4ac\equiv 0\pmod{p}$ there is a unique solution. Otherwise, there are $2$ solutions if $b^2-4ac$ is a quadratic residue of $p$, and $0$ if $b^2-4ac$ is a quadratic non-residue of $p$.
Comment: Yes, that's true. If for example $n=pq$ where $p$ and $q$ are distinct primes, we could have $(k/(pq))=1$ because $(k/p)$ and $(k/q)$ are both $-1$, so $k$ is NR modulo $p$ and therefore mod $n$. But if $(k/(p_1\cdots p_t))=-1$, then at least one of the $k/p_i$ is $-1$, so $k$ is NR modulo $n$.
Comment: Really a perfect answer to what I was asking. Thank you.
Comment: You are welcome. If you have a further question, I can answer it by adding material or in a comment.
Comment: I have a quick question yes.  If for a positive odd integer $n$ and an integer $k$, the Jacobi symbol ($\frac{k}{n}$) is $1$, then I think it does not follow that $k$ is a square in $\mathbb{Z}/_n\mathbb{Z}$. But if the Jacobi symbol is $-1$, then it DOES follow that $k$ is not a square in $\mathbb{Z}/_n\mathbb{Z}$.  Are these statements correct?


Does tempo affect use of non-harmony notes?

Question: Writing a four part piece for strings at a very slow tempo (55 bpm). On paper, it seems as though the non-chord notes I have selected should work, but I am finding some of them jarring. I am unsure if the problem is down to the slow tempo, and therefore the inevitable longer duration of the dissonances, or whether there is some other problem.
The bars concerned are the last two in the picture.
The harmony over the two bars is FM (IV 64) and F sharp dim triad, also in second inversion, as I am using C as a pedal point.

The first bar (FM 64), we have a passing note in the alto. This forms a 7th dissonance with the bass (2 bass parts here, both in bass clef), a sharp 4 dissonance with the tenor (alto clef), and a minor second dissonance with the soprano. Each dissonance is resolved to a consonance on the next beat (Bass octave, tenor P5 and soprano M6), but still doesn't seem quite right. Out of the two bars, this one is the least jarring, but I am still unsure about it.

the second bar (F sharp dim triad 64-last bar on the page) presents more of a problem. To go with the D escape note in the soprano part, I thought I would put in a B auxiliary note (highlighted) in the alto. I know that decorative notes should sound in 3rds/6ths with each other, and these form a minor third. However, perhaps down to the tempo, this seems to change the harmony to B minor for the listener, and does not sound right at all. Is this simply down to the tempo, or has a different mistake been made?

Thanks for your help!
Answer:
In the first measure the notes (vertically are): C-C-F-A-C then B-B-F-G-C, resolving to C-C-E-G-C. This middle "passing" chord looks like G7add11/B. Coexistence of major third (B) and perfect fourth (C) is something to be used with much care. A simple solution would be to raise the soprano melody to D on the last beat of the first measure. Alternatively you can avoiding the bass going down to B. If it stays at C, or goes up to D, the issue will be avoided.

At the end of measure 3 soprano and alto cross. Then in measure 4 you have D13 chord (also called D7/6 in classical music, I believe). This is a beautiful chord, but I'm not sure about the voicing... the most often I see this chord voiced as D-C-F#-B. Maybe doubling seventh is not the most fortunate choice? I'm not sure.
Comment: thanks for your answer! These 'passing chords' are just incidental, as I'm using two non chord notes at once. i thought that as long as these were prepared and resolved correctly, it wouldn't matter which dissonances were chosen in particular.  That first bar your'e talking about is FM second inversion, with B auxilliary note in the bass and G anticipation in the tenor. Both are struck at once, so I made sure to put them 3rd/6th apart. Both resolve correctly. The only potential problem I can see with this bar is that the dissonant 4th (C bass F tenor), moves to a dissonance before resolution.
Comment: Likewise with measure 4,  the harmony here is Fsharp dim second inversion, with escape note D in soprano and auxiliary note B in alto.  The C is therefore not the 7th, but the 5th. These non-chord notes are prepared and resolved by the book (I think!), but this measure is definitely problematic !
Comment: @EdB123 I guess we're speaking a bit different languages. But I worry that it is incorrect to assume that "it wouldn't matter which dissonances were chosen." I wrote what harmony I hear in this example. If you hear it differently, it's OK, but do you?
Answer: Does tempo affect use of non-harmony notes?
Not tempo so much as duration. A long non-harmonic note at a fast tempo still calls attention to that note.
The problem as I see it is strongly accented dissonances that overwhelm the consonances
In particular, in measure 17, the alto B is dissonant against both the bass C and the tenor F# (perfect fourth is dissonant here). Dissonance creates a natural accent, which is further emphasized by the fact that it's also syncopated — also a source of accent — and, by virtue of being longer than the surrounding material — an agogic accent.1
The solution I suggest is to mark the tenor in bars 16 and 17 one dynamic level lower than the other parts and add poco crescendo to the alto part.
Further discussion
1. Bar 16
Given the preceding material in bars 14 and 15, I don't find this especially jarring. However, it is important to bring out the alto part clearly.
It's easy for the chordal third (A) to get lost in the very strongly present C-F fourth (a dissonance in this context) plus the F-C fifth and C-C octave, which are very strong consonances. This relative loss of the A becomes problematic when the soprano and alto voices move, especially when they cross. The alto part must be heard clearly for the ear to follow the voice-leading properly.
2. Bar 17
Here the problem is that the F# so dominates the sound, being dissonant against the bass C, that I hear the F# as moving to B — a leap from a powerful tritone dissonance into a "double-dissonance": a major seventh against the bass C, and a perfect fourth (again a dissonance here) against the still-present F#.
Put another way, the effect is as though the alto is doubling the tenor, rather than the soprano. So I hear a leap of a fourth rather than a step of a major second.
Here again it's important to emphasize the alto line, but also to de-emphasize the tenor.

1 An agogic accent is created when one note is longer than the surrounding notes.
Comment: thanks for such a great answer- very illuminating. Given what you have said about the dominance of the F sharp, would you say that the natural dissonance of non-diatonic chord notes should be something to take into consideration when decorating any non-diatonic chord? Perhaps their duration should be limited? Also, I'd be intrigued to hear what you have to say about bar 14. We have the dissonance C-F between bass and tenor, which actually then becomes another dissonance, B-F with the introduction of the auxiliary note in the bass. I'm not sure what to think about it.
Comment: @EdB123 I'd say these question are more aesthetic than factual. If you'd like to discuss, I suggest a post in the chat room.
Answer: On the whole, I think the treatment of dissonance in common practice is consistently the same regardless of tempo. In other words the sense of "harshness" or right/wrong isn't about how long a dissonance is help but rather it's proper handling, the preparation and resolution.
A properly handled dissonance can be held for a long time, milked for drama/expressiveness, resolved properly, and it will sound fine. Do it wrong and it sounds weird. Speeding up the tempo or shortening the dissonant note value will only disguise the problem. Perhaps it will be less noticeable, but it will probably lack an authentic common practice sound. By "proper" I basically mean handle your dissonances according to standard non-chord tone definitions you find in most harmony textbooks.
In some ways you could say slow tempos invite holding dissonances longer. If a piece is very slow with little rhythmic movement, strong dissonances can aminate the music and give it forward drive while still being slow.
The other thing worth mentioning is taking care with chromatic dissonances. Relative to the tone of resolution, dissonances above tend to be diatonic while those below can be chromatic half steps that act like temporary leading tones. You could have dissonant chromatic half steps above, but probably in some kind of "borrowed" harmony context which just reframes what is diatonic. The main point is chromatic dissonant tones can stand out as very "wrong" if they aren't handle right.
Comment: thanks Michael. Yes, I hear essentially that sharpened notes should resolve upwards- not sure if this applicable across the board but I hear its a good rule of thumb. i have a question-if i may- regarding the FM 64 chord, moving to f sharp dim 64. This is a dissonance (P4) moving straight to another (augmented 4th) between the bass and tenor. However, it gets away with it. I was wondering if you could tell me why? I find myself trying to resolve all dissonances, but sometimes moving from one to dissonance to another works really well. Is there a common practice rule that backs this up?


Não consigo obter elementos de uma página com o Puppeteer

Question: Estou criando uma aplicação para baixar uma imagem do Instagram pela URL e estou utilizando o pacote puppeteer para realizar esta tarefa.
Dentro da chamada do método evaluate (método para rodar um código JS na página) eu tento obter e retornar um elemento de imagem.
O problema é que sempre que eu tento obter a imagem, eu acabo recebendo null como resultado. Veja o meu código abaixo:
const puppeteer = require("puppeteer");
const url = "https://www.instagram.com/p/<image_code>/";

async function getImageFrom(url) {

    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    await page.goto(url);

    const image = await page.evaluate(function() {
        return document.querySelector(".KL4Bh > img");
    });

    await browser.close();
    return image;
}

getImageFrom(url).then((image) => {
    console.log(image);
});

O que eu estou fazendo de errado? Já tentei utilizar o querySelectorAll buscando apenas por elementos com a tag  mas ele também me retorna apenas um array vazio.
Edit: Eu não sei o que aconteceu, mas o método está retornando undefined agora.
Comment: Este seletor (`.KL4Bh > img`) não me parece muito confiável e duradouro... É bem típico do Facebook mudar esse tipo de seletor com uma certa frequência. Apesar disso, não creio que seja o problema... Além disso, o que seria `None`? :P
Comment: Ops, foi mal eu confundi `null` com `None` kkk (mania de Python xD). Bom se o Instagram faz isso eu não sei, mas eu testei pegar o elemento direto no console do navegador e funcionou. Além disso, eu também testei obter apenas os elementos de `` como eu já havia dito na resposta e mesmo assim o `puppeteer` só retornava um array vazio.
Comment: Pelo o que pude ver aqui, parece que não é um problema com o Puppeteer. O Instagram insere a imagem via JavaScript. Se você tentar baixar o HTML usando alguma ferramenta como cURL, verá que o seletor que você informou não estará lá. Penso então que seja colocado via JavaScript. Eu não sei se o método `evaluate` espera que todos os JavaScripts da página sejam devidamente finalizados, mas pelo o que me parece não é o caso. Vale investigar isso.
Comment: Mas o objetivo do `puppeteer` até onde eu entendi é ser uma espécie de `selenium` onde ele renderiza a página para ser depois manipulada. Então mesmo que as imagens sejam adicionadas via JavaScript, o `puppeteer` deveria renderizar isto e me devolver os elementos.
Answer: Você provavelmente NÃO esta esperando o load completar ou pode ser que a forma como o instagram carregue seja dinamica e até depois do load, cada site faz de uma forma.
Troque isso:
const image = await page.evaluate(function() {
    return document.querySelector(".KL4Bh > img");
});

Por:
page.waitForSelector(selector[, options])

Para esperar o elemento "existir" e que já é uma função nativa (o evaluete seria JS dentro do webview gerado, o que é retrabalho desnecessário), exemplo:
page.waitForSelector('.KL4Bh > img');

Ou ao invés de trocar sete a propriedade waitUntil em options
page.goto(url[, options])

E nele aplicar o valor networkidle2 (que seria como o load e também não há mais de 2 conexões de rede por pelo menos 500ms), exemplo:
await page.goto('https://site.com', { waitUntil: 'load' });

Comment: Como assim trocar o `goto`? Não é este método que faz abrir uma página por uma URL? Eu tentei utilizar o `waitForSelector` e ele ficou esperando por muito tempo até que chegou uma hora em que o método gerou um erro dizendo que o tempo de 30000ms tinha sido excedido. Também tentei utilizar a opção `waitUntil: 'load'` mas ainda assim não funcionou.
Comment: Guilherme, uma coisa que estou vendo agora é que o resultado do `evaluate` não é mais `null` e sim `undefined`. Eu não sei quando esse resultado mudou, mas agora é isso que ele devolve.
Comment: @JeanExtreme002 não era trocar o goto, corrigi a resposta, mas de qualquer forma experimente trocar o evaluete por `page.waitForSelector` ... pois evaluete é pra executar um javascript e não tem pq fazer isto se já tem método nativo.
Comment: Este método funcionou, mas como posso obter os atributos do elemento como o `src` ? E se possível, poderia explicar na resposta o porquê da opção `waitUntil` não funcionar ? Eu gostaria de saber isso para caso eu precise executar o `evaluate` futuramente.
Comment: @JeanExtreme002 pode ter sido algo que você fez de errado, não sei, pode ser que o instagram mesmo após o load ainda tenha que carregar algo ... afinal, cada site trabalha de um jeito e inventa a propria moda, mesmo que as vezes seja meio sem noção (não tenho como testar se é isso mesmo).... Pegar o SRC acho que deve ser algo como `const propertyHandle = await element.getProperty('src');`, o element é o valor retornado pelo `page.waitForSelector`
Comment: Obrigado Guilherme pela sua ajuda, sua resposta foi bastante esclarecedora. Eu consegui descobrir o real problema do código e coloquei a solução na minha resposta.
Comment: @JeanExtreme002 e no que difere do que eu disse ? Você usou praticamente o `page.waitForSelector` que eu citei, na sua resposta você dá a entender que eu respondi algo errado sobre pegar o elemento, como se eu tivesse dito só pra pegar o SRC, sendo que isso eu nem falei na resposta, na verdade isso nem na sua pergunta é falado, é algo que vc trouxe depois nos comentários, DESCONTEXTUALIZANDO toda a pergunta, como pegar o "elemento inteiro" (o handler) eu respondi.
Comment: Não eu coloquei o `page.waitForSelector` como algo alternativo porque a pergunta era mais sobre o problema com o `evaluate`. A única parte na minha resposta que tem ligação com a sua é quando eu falo sobre a opção `waitUntil`, em momento nenhum disse ou quis dizer que sua resposta estava errada por pegar um elemento de forma X ou Y. E realmente a pergunta é sobre pegar um elemento inteiro e não apenas o `src`, e na minha resposta eu explico que pegar um elemento inteiro com o `evaluate` é errado e que pra isso se deve ser utilizado o `waitForSelector`.
Comment: De qualquer maneira, peço desculpa se pareceu que eu estava dizendo que sua resposta estava errada. Na minha resposta a solução final para obter um elemento por inteiro é a mesma que a sua resposta, mas na minha eu explico sobre a raiz do problema e dou detalhes sobre outras coisas. Levei horas atrás de horas vasculhando a internet inteira até encontrar um simples comentário no GitHub que me fez entender o problema de tudo.
Comment: A pergunta não falava de nada disso @JeanExtreme002, ai foi além, quase como uma pergunta camaleão. Respondi ao que foi perguntando e ajustei a parte que estava errada sobre o load. A resposta basicamente esta na parte que eu falei "Você provavelmente NÃO esta esperando o load completar" e no comentário e "afinal, cada site trabalha de um jeito e inventa a propria moda", não tem como dizer quando itens dinamicos vão carregar, pra isso o waitForSelector resolve
Answer: O método evaluate está retornando o valor null porque talvez o elemento não exista na página. Seja porque não foi carregado ainda ou porque ele realmente não existe na página atual mesmo tendo carregado todo o código JS.
A outra resposta diz para definir o valor "load" para a opção waitUntil, porém isso é redundante já que segundo a documentação dessa API, por padrão essa opção será "load".
Ainda assim esse não é o problema principal do seu código. Mesmo que o elemento tenha carregado na página, o método evaluate deve retornar undefined se tudo correr bem. Isso porque você está tentando retornar um objeto não serializável.
Se o seu objetivo é apenas obter o atributo src ou outro desse elemento que retorne uma string ou um outro valor serializável, faça-o dentro do método e o retorne como no código abaixo:
const image = await page.evaluate(function() {
    return document.querySelector(".KL4Bh > img").src;
});

Se você precisar do elemento por inteiro e não apenas de um atributo ou simples valor, utilize o método waitForSelector como é mostrado na outra resposta. O que esse método faz é esperar e retornar um elemento da página com o seletor que você definiu. Veja o código abaixo:
const image_element = await page.waitForSelector('.KL4Bh > img');

Este método irá retornar um objeto de ElementHandle. Para obter os atributos deste elemento, utilize o método getProperty(attr) que retorna um objeto de JSHandle.
Após ter obtido este objeto, utilize o método jsonValue() para obter o valor do atributo.
const image_element = await page.waitForSelector('.KL4Bh > img');
const image_property = await image_element.getProperty("src");

const src = await image_property.jsonValue();

Lembre-se de que todas essas ações devem ser feitas com o browser aberto. Sendo assim, você não pode fechar o browser antes de obter a propriedade da imagem ou receberá um erro.


Statistics counter based on user role

Question: I need to track some basic usage statistics for a subset of my authenticated user roles in Drupal 9.
Is there a way to make the core statistics counter only count node views based on user roles? The three counters it provides are all I need, I just need them to only record certain user roles.
Answer: The statistics module adds a js library to the node build. You can remove it if you don't want to count the node:
mymodule.module
<code>/**
 * Implements hook_ENTITY_TYPE_view_alter() for node entities.
 */
function mymodule_node_view_alter(array &$build) {
  $roles = \Drupal::currentUser()->getRoles();
  if (!in_array('special_role', $roles)) {
    if (isset($build['#attached']['library'])) {
      $key = array_search('statistics/drupal.statistics', $build['#attached']['library']);
      if ($key !== FALSE) {
        unset($build['#attached']['library'][$key]);
      }
    }
  }
}
</code>
This only works if the roles are altering permissions. If not, add a cache context for the user roles:
<code>$build['#cache']['contexts'][] = 'user.roles';
</code>
Comment: Yes, exactly. By default, the cached node is presented to any user having the same permissions.
Comment: That looks perfect thank you. I'm afraid I don't understand this line though `This only works if the roles are altering permissions.` could you explain what you mean by that?
Comment: Normally every role has a different set of permissions. But you could create multiple roles with the same permissions and then use them to control things not directly connected to permissions, like here.
Comment: Sorry, I'm even more confused now. The snippet makes sense; it removes the statistics js library if the user doesn't have the correct role. Why does the cache context matter here? I don't plan on changing user roles, Or is the same modified node getting presented to multiple users?
Comment: That is somewhat counter-intuitive to me but does explain some odd behavior with user roles I've run into before...

Statistics counter based on user role


Java2D Performance Issues

Question: I'm having performance oddities with Java2D. I know of the sun.java2d.opengl VM parameter to enable 3D acceleration for 2D, but even using that has some weird issues.
Here are results of tests I ran:
Drawing a 25x18 map with 32x32 pixel tiles on a JComponent
Image 1 = .bmp format, Image 2 = A .png format
Without -Dsun.java2d.opengl=true
120 FPS using .BMP image 1
13 FPS using .PNG image 2
With -Dsun.java2d.opengl=true
12 FPS using .BMP image 1
700 FPS using .PNG image 2
Without acceleration, I'm assuming some kind of transformation is taking place with every drawImage() I do in software, and is pulling down the FPS considerably in the case of .PNG. Why though, with acceleration, would the results switch (and PNG actually performs incredibly faster)?! Craziness!
.BMP Image 1 is translated to an image type of TYPE_INT_RGB. .PNG Image 2 is translated to an image type of TYPE_CUSTOM. In order to get consistent speed with and without opengl acceleration, I have to create a new BufferedImage with an image type of TYPE_INT_ARGB, and draw Image 1 or Image 2 to this new image.
Here are the results running with that:
Without -Dsun.java2d.opengl=true
120 FPS using .BMP image 1
120 FPS using .PNG image 2
With -Dsun.java2d.opengl=true
700 FPS using .BMP image 1
700 FPS using .PNG image 2
My real question is, can I assume that TYPE_INT_ARGB will be the native image type for all systems and platforms? I'm assuming this value could be different. Is there some way for me to get the native value so that I can always create new BufferedImages for maximum performance?
Thanks in advance...
Answer: I think I found a solution by researching and putting bits and pieces together from too many Google searches.
Here it is, comments and all:
<code>private BufferedImage toCompatibleImage(BufferedImage image)
{
    // obtain the current system graphical settings
    GraphicsConfiguration gfxConfig = GraphicsEnvironment.
        getLocalGraphicsEnvironment().getDefaultScreenDevice().
        getDefaultConfiguration();

    /*
     * if image is already compatible and optimized for current system
     * settings, simply return it
     */
    if (image.getColorModel().equals(gfxConfig.getColorModel()))
        return image;

    // image is not optimized, so create a new image that is
    BufferedImage newImage = gfxConfig.createCompatibleImage(
            image.getWidth(), image.getHeight(), image.getTransparency());

    // get the graphics context of the new image to draw the old image on
    Graphics2D g2d = newImage.createGraphics();

    // actually draw the image and dispose of context no longer needed
    g2d.drawImage(image, 0, 0, null);
    g2d.dispose();

    // return the new optimized image
    return newImage;
}
</code>
In my previous post, GraphicsConfiguration was what held the information needed to create optimized images on a system. It seems to work pretty well, but I would have thought Java would automatically do this for you. Obviously you can't get too comfortable with Java. :) I guess I ended up answering my own question. Oh well, hopefully it'll help some of you I've seen trying to make use of Java for 2D games.
Comment: This is absolutely awesome. It's making my code run much faster. Thank you very much.
Comment: I apologize for the great delay in marking this as the answer.
Comment: THANK YOU SO MUCH!
Answer: Well, this is old post but I'd like to share my findings about direct drawing with Swing/AWT, without BufferedImage.
Some kind of drawing, as 3D, are better done when painting directly to a int[] buffer. Once done the images, you can use an ImageProducer instance, like MemoryImageSource, to produce images. I'm assuming you know how to perform your drawings directly, without help of Graphics/Graphics2.
<code>    /**
* How to use MemoryImageSource to render images on JPanel
* Example by A.Borges (2015)
*/
public class MyCanvas extends JPanel implements Runnable {

public int pixel[];
public int width;
public int height;
private Image imageBuffer;
private MemoryImageSource mImageProducer;
private ColorModel cm;
private Thread thread;

public MyCanvas() {
    super(true);
    thread = new Thread(this, "MyCanvas Thread");
}

/**
 * Call it after been visible and after resizes.
 */
public void init(){
    cm = getCompatibleColorModel();
    width = getWidth();
    height = getHeight();
    int screenSize = width * height;
    if(pixel == null || pixel.length < screenSize){
        pixel = new int[screenSize];
    }
    mImageProducer =  new MemoryImageSource(width, height, cm, pixel,0, width);
    mImageProducer.setAnimated(true);
    mImageProducer.setFullBufferUpdates(true);
    imageBuffer = Toolkit.getDefaultToolkit().createImage(mImageProducer);
    if(thread.isInterrupted() || !thread.isAlive()){
        thread.start();
    }
}
/**
* Do your draws in here !!
* pixel is your canvas!
*/
public /* abstract */ void render(){
    // rubisch draw
    int[] p = pixel; // this avoid crash when resizing
    if(p.length != width * height) return;
    for(int x=0; x < width; x++){
        for(int y=0; y<height; y++){
            int color =  (((x + i) % 255) & 0xFF) << 16; //red
                color |= (((y + j) % 255) & 0xFF) <<  8; //green
                color |= (((y/2 + x/2 - j) % 255) & 0xFF) ;   //blue
            p[ x + y * width] = color;
        }
    }
    i += 1;
    j += 1;
}
private int i=1,j=256;

@Override
public void run() {
    while (true) {
        // request a JPanel re-drawing
        repaint();
        try {Thread.sleep(5);} catch (InterruptedException e) {}
    }
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    // perform draws on pixels
    render();
    // ask ImageProducer to update image
    mImageProducer.newPixels();
    // draw it on panel
    g.drawImage(this.imageBuffer, 0, 0, this);
}

/**
 * Overrides ImageObserver.imageUpdate.
 * Always return true, assuming that imageBuffer is ready to go when called
 */
@Override
public boolean imageUpdate(Image image, int a, int b, int c, int d, int e) {
    return true;
}
}// end class
</code>
Note we need unique instance of MemoryImageSource and Image. Do not create new Image or new ImageProducer for each frames, unless you have resized your JPanel. See init() method above.
In a rendering thread,  ask a repaint(). On Swing, repaint() will call the overridden paintComponent(), where it call your render() method and then ask your imageProducer to update image.
With Image done, draw it with Graphics.drawImage().
To have a compatible Image, use proper ColorModel when you create your Image. I use GraphicsConfiguration.getColorModel():
<code>/**
 * Get Best Color model available for current screen.
 * @return color model
 */
protected static ColorModel getCompatibleColorModel(){
    GraphicsConfiguration gfx_config = GraphicsEnvironment.
            getLocalGraphicsEnvironment().getDefaultScreenDevice().
            getDefaultConfiguration();
    return gfx_config.getColorModel();
}
</code>
Comment: Just wondering what a sort of people downvotes correct replies... It is (i) about Java2D; (ii) about high performance on drawing images; (iii) about rendering images coming from elsewhere (OpenGL, native renderer, software rendered, etc). So WHY  downvote???
Answer: From what I remember when I was thinking about doing graphics programming in Java, the built in libraries are slow. I was advised on GameDev.Net that anyone doing anything serious would have to use something like jogl
Comment: Well since the largest performance penalty is actually calling drawImage(), I'd consider 450 calls with 700 FPS pretty good. I'd have to write the same thing in jogl to see if there's a significant difference, but great progress has been made with Java2D in recent versions of Java.

Java2D Performance Issues


Show that $\min \{ |f(z)| : |z|=1 \} \leq |c_0| + |c_1|+ \dots + |c_m|$

Question:
Suppose that $f$ is holomorphic inside and on $\gamma(0;1)$, the circle centered at $0$ of radius $1$, with Taylor expansion $\sum_{n=0}^{\infty} c_nz^n$. Given that $f$ has $m$ zeros inside $\gamma(0;1)$, prove that
  $$\min \{ |f(z)| : |z|=1 \} \leq |c_0| + |c_1|+ \dots + |c_m|$$

I really don't know how to start. From the hypothesis we know that
$$\frac{1}{2\pi i}\int_{\gamma(0;1)}\frac{f'(z)}{f(z)} dz =m$$
but I'm not sure if that fact is useful at all. Maybe I should exploit the series expansion of $f(z)$, but I haven't come up with anything. Any help will be highly appreciated, and thanks in advance!
Comment: What does $\gamma(0,1)$ stand for? Is it the open disk of unit radius centred at the origin?
Comment: @Arian It is the circle centerd at zero with radius $1$, I'll edit the question to make that clearer.
Answer: I thank very much @See-Woo Lee who has indicated the right way to prove the issue, i.e., by contradiction.
Let  $f(z)=p(z)+q(z)$ with $\begin{cases}p(z)&=&\sum_{n=0}^{m}c_nz^n\\q(z)&=&\sum_{n=m+1}^{\infty} c_nz^n \end{cases}$.
Let $U$ be the unit disk and $\partial U$ be the unit circle.
Let us prove the result by contradiction.
Let us assume the contrary of what has to be proven, i.e., that
$$\forall z \in \partial U, \ |f(z)| > |c_0| + |c_1|+ \dots + |c_m|$$
thus, a fortiori
$$\forall z \in \partial U, \ |f(z)| > |c_0 + c_1 z+ \dots + c_m z^m|=|p(z)|=|f(z)-q(z)|$$
Using Rouché's theorem,  $f(z)$ and $f(z)-q(z)=p(z)$ have the same number of zeros inside the unit disk $U$, i.e., $m$ zeros.
But $q(z)=z^{m+1}r(z)$ where $r(z)$ is a holomorphic function; therefore $q(z)$ would have at least $m+1$ zeros in $U$, a contradiction with the previous sentence.
Comment: can you expand the reasoning ?
Comment: So I should proceed by comtradiction? Assume that $\vert \sum_{n=m+1}^{\infty} c_nz^n \vert < |f(z)|$. Then $g(z)$ has $m$ zeros inside the contour $\gamma(0;1)$, but I can't see any contradiction there.
Comment: I have provided a thorough proof now ; yes, you were on the good track by working by contradiction.
Comment: Thanks, @Hamza, for the correction of "polynomial" to "holomorphic".
Comment: excuse me because i do it without telling you
Comment: No problem. It's good that errors are corrected as soon as possible. I like in SE this spirit of co-operation.
Comment: Thank you! I didn't realized that it should have $m+1$ zeros at least.


Android drag imageview in RelativeLayout Resized

Question: I am currently making an android application which allow user to add some image from a list to the screen, and user is allowed to drag the image on it.
I can drag the image by using onTouch method.
But I faced a problem that when I drag the image view to the left or bottom size
The imageview will auto resize  and cannot drag it out to the layout.
How can I drag it outside the layout without resizing the image view?
<code>@Override
public boolean onTouch(View view, MotionEvent event)  {
    ImageView imageView = (ImageView)view;
    imageView.bringToFront();

    //  Handle event
    switch (event.getAction() & MotionEvent.ACTION_MASK)  {
        case MotionEvent.ACTION_DOWN:  {
            startPoint.set(event.getX(), event.getY());
            currentMode = EVENT_DRAG;
            break;
        }

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:  {
            currentMode = EVENT_NONE;
            break;
        }

        case MotionEvent.ACTION_MOVE:  {
            RelativeLayout.LayoutParams layoutPrarms = (RelativeLayout.LayoutParams)imageView.getLayoutParams();
            int left = layoutPrarms.leftMargin + (int)(event.getX() - startPoint.x);
            int top = layoutPrarms.topMargin + (int)(event.getY() - startPoint.y);
            layoutPrarms.leftMargin = left;
            layoutPrarms.topMargin = top;
            imageView.setLayoutParams(layoutPrarms);
            break;
        }
    }

    //  Indicate event was handled
    return true;
}
</code>
Answer: Try having your parent layout be a LinearLayout. I have only run into this problem using RelativeLayouts.
Comment: I've tried LinearLayout but still no luck :s, any other idea?


PHPMailer: Utilizando o campo email como remetente

Question: Estou com uma dúvida, tem como eu utilizar o e-mail que foi preenchido no campo "E-MAIL:" do formulário como remetente? Preciso que quando apertar no botão de resposta da caixa postal ele preencha com o email que for preenchido no formulário...
Answer: Não é preciso colocar o email como remetente. Você pode utilizar a função replyTo para isso. Ela tem intuito que quando o email for respondido pela caixa postal, a resposta vá para o email informado na função replyTo ao invés do email que enviou o email.
$mail->AddReplyTo('firstname.lastname@example.com', 'Reply to name');


Add node values in binary tree

Question: How do I print the sum of values under a node in a binary tree? Here's what I have so far:
public int sumT(Node nodo, int sum) {
    if (nodo == null) {
        return 0;
    } else {
        int sum = suma + nodo.getLeft().getValor();
        int sum1 = sumT(nodo.getLeft(), sum);
        int sum2 = sumT(nodo.getRight(), sum1);
        return sum2;

    }

}

Comment: You declare `int sum` when sum is already a parameter of sumT.
Comment: `suma` does not exist as far as I can see.
Comment: Looks to me like when you hit a leaf node (or any node with no left branch), you're going to get an exception at `nodo.getLeft().getValor()` *before* the recursive call on the next line tests for a null Node.
Answer: public int sumT(Node nodo)  {
  if  (nodo == null)
    return 0;
  return sumT(nodo.getLeft()) + sum(nodo.getRight()) + nodo.getValor();
}

This should be right. So you don´t have to set the sum as an argument in sumT.

Add node values in binary tree


problems with understanding types in haskell

Question: Hey guys so I'm suppose to takes a list of words and returns a list just like it but
with the following substitutions made each time they appear as consecutive words.
One example is you and turn it into u
I'm given the following:
hep :: [Word] -> [Word]
type Word = String

now what is giving me problem is that I'm trying to use case expressions so that I won't have to repeat code but I get the following error
Couldn't match expected type `Char' with actual type `[Char]'
In the pattern: "You"
In a case alternative: "You" -> "u" : hep xs
In the expression: case a of { "You" -> "u" : hep xs }

from the following code
hep [] = []
hep [a:xs] = case a of
    "You" -> "u":hep xs

Anyone tell me what the problem is?
Edit:
I have added the following code
hep [] = [[]]
hep (a:xs) = case a of
    "you" -> "u":hep xs
    "are" -> "r":hep xs
    "your" -> "ur":hep xs
    "boyfriend" -> "bf":hep xs
    "girlfriend" -> "gf":hep xs
    "great" -> "gr8":hep xs
    a -> a:hep xs

Now how would I be able to add a case so that if the list contains 2 or 3 certain words in an order, I could turn that into an acronym?
Ex
["By","The","way"] = ["btw"]

Comment: That gives me the exact same error just with "You" -> 'u'
Comment: works fine for me. i get an extra "" at the end of the returned list, not sure if that's intended
Comment: Weird, NOW it works for me lol.
How would I go about adding 2 more cases so that I could compare three words and abbreviate that?

Example:  "By", "The", "way"   would turn into "btw"
Answer: You're trying to match against a list of a list of Strings, but the type of hep is [Word] -> [Word], which contradicts that.  The error message is referring to this fact.
But I'm guessing what you actually want is this?
hep [] = []
hep (a:xs) = case a of
    "You" -> "u":hep xs
    a -> a:hep xs

Comment: Thanks, that helped me alot. Didn't notice the bracket was missing.

Btw, when I add extra cases I get an error. Imma edit the thread so you can read it more clearly.
Answer: Something like this?
"By" -> let (y:ys) = xs
        in if y=="The" && head ys=="Way"
              then "btw": hep (drop 2 xs)
              else a:hep xs

Although I wouldn't want to write that 50 times in a row. How about this?
import Data.List
import Data.Maybe

hep [] = []
hep (a:xs) = case a of
              "you" -> "u":hep xs
              "are" -> "r":hep xs
              "your" -> "ur":hep xs
              "boyfriend" -> "bf":hep xs
              "girlfriend" -> "gf":hep xs
              "great" -> "gr8":hep xs
              "by" -> matchPhrase 3
              "see" -> matchPhrase 2
              a -> a:hep xs
            where matchPhrase num =
                    let found = lookup (concat $ take num (a:xs)) phrase_list
                    in case found of
                        Just _ -> fromJust found : hep (drop num (a:xs))
                        Nothing -> a:hep xs

phrase_list = [("bytheway", "btw"), ("seeyou","cu")]


WSO2 API Manager - can it convert REST requests to SOAP requests on the backend?

Question: I'm exploring the use of the WSO2 API Manager for our legacy API, which is currently SOAP based. IDeally, I'd like to expose a RESTful interface to our customers and have API Manager convert that to a SOAP request that gets passed to our legacy system. Searching the WSO2 site, I see there's some way to do this using XSLT transformations, but only when using the WSo2 ESB product.
Is there a way to handle this transformation in the API Manager? How? If not, could I somehow take advantage of the ESB product and combine it with the API Manager product? Or is that overkill?
Thanks!
Answer: REST to SOAP conversion is a standout feature of the WSO2 ESB. To learn more about it look at the sample given at [1]. The Payload Factory Mediator used in this sample is a much newer type of mediator than the XSLT Mediator and is also more efficient in handling SOAP messages than XSLT Mediator.
The WSO2 API Manager is a platform for publishing, managing and governing APIs. The WSO2 API Manager is built using the componentalized Carbon framework, for which almost all components of the WSO2 ESB are utilized. Therefore technically all that can be done with the WSO2 ESB can be done by the WSO2 API Manager; however without the help of the GUI found in the WSO2 ESB; you will have to use the source view of the configuration which is xml based. But ofcourse to navigate around this obstacle you can always download the WSO2 ESB product (which is also free and open-source) and create all the necessary mediation sequences using the GUI and copy-n-paste the configuration from the WSO2 ESB to the WSO2 API Manager and you are done.
In my opinion it is the WSO2 ESB that you need. In your case I presume you just have one or a few SOAP APIs which you need to expose as REST APIs; you are looking more at the REST-SOAP conversion feature rather than the ability to manage hundreds of APIs. If you do not need the core functionalities of the API Manager such as publishing APIs, versioning, authenticating consumers, throttling based on policies, and monitoring API usage (for a complete list of features look at the product homepage at [2]), then you really do not want the API Manager. But if you have the API Management requirements you can go with WSO2 API Manager. I don't think you should go with the WSO2 API Manager just because you foresee some kind of API Management requirements coming ahead.
Using both the WSO2 ESB fronted by the WSO2 API Manager is also a solution. WSO2 even recommends this architecture if you are performing heavy mediation in the mediation layer. You can even start with the WSO2 ESB alone and later when the proper requirements come, decide to front it with the WSO2 API Manager. Fronting the WSO2 ESB with the WSO2 API Manager is a breeze.
[1] http://docs.wso2.org/wiki/display/ESB403/Sample+800+Introduction+to+REST+API
[2] http://wso2.com/products/api-manager/


Implementar classe pai

Question: Bom, pesquisei bastante, até tenho noção de como funciona, mas não consegui implementar, gostaria de uma luz. Tenho as classes Casa e Edificio, preciso criar uma classe pai Imovel, tendo Casa e Edificio como classes filhas, segue meu código:
 package br.edu.utfpr.exer05;

public class Porta {
    boolean aberta;
    String cor;
    double dimensaoX, dimensaoY, dimensaoZ;

    void abre() {
        this.aberta = true;
    }
    void fecha() {
        this.aberta = false;
    }
    void pinta(String cor) {
        this.cor = cor;
    }
    boolean estaAberta() {
        boolean estaAberta = false;
        if(aberta == true) estaAberta = true;
        return estaAberta;
    }

}

package br.edu.utfpr.exer05;

import java.util.ArrayList;

public class Edificio {
    String cor;
    int totalDePortas;
    int totalDeAndares;
    ArrayList<Porta> portas = new ArrayList<Porta>();

    void pinta(String cor) {
        this.cor = cor;
    }

    int quantasPortasEstaoAbertas() {
        int qtdPortasAbertas = 0;
        for(int i=0; i<portas.size(); i++) {
            portas.get(i);
            if(portas.get(i).estaAberta() == true) {
                qtdPortasAbertas++;
            }
        }
        return qtdPortasAbertas;
    }

    void adicionaPorta(Porta porta) {
        this.portas.add(porta);
    }

    int totalDePortas() {
        return this.portas.size();
    }

    void adicionaAndar() {
        this.totalDeAndares = totalDeAndares += 1;
    }

    int totalDeAndares() {
        return this.totalDeAndares;
    }
}

package br.edu.utfpr.exer05;

public class Casa {
    String cor;
    Porta porta1, porta2, porta3;

    void pinta(String cor) {
        this.cor = cor;
    }

    int quantasPortasEstaoAbertas() {
        int qtdPortasAbertas=0;
        if(porta1.estaAberta() == true) qtdPortasAbertas++;
        if(porta2.estaAberta() == true) qtdPortasAbertas++;
        if(porta3.estaAberta() == true) qtdPortasAbertas++;
        return qtdPortasAbertas;
    }

    int totalDePortas() {
        return 3;
    }
}

E aqui a classe que preciso tornar a "pai":
package br.edu.utfpr.exer05;

import java.util.ArrayList;

public class Imovel {
    String cor;
    ArrayList<Porta> portas = new ArrayList<Porta>();
}

As classes Casa e Edificio estao feitas de formas diferentes pois o exercício pedia, porém tem o mesmo objetivo. Bom, como proceder para criar uma classe pai nos moldes descritos?
Comment: Se for para criar a herança, so faltou o `extends` após o nome das classes assim: `public class Edificio extends Imovel`
Comment: Certo, valeu...Poderia mostrar um exemplo de como ficaria a classe Imovel?
Answer: Você deve criar a classe Imovel com os atributos e métodos que são comuns a todas as classes filhas, para esses atributos e métodos estarem visíveis as classes filhas o modificador de acesso deve ser protected ou public, feito isso você cria as classes filhas de Imovel no seu caso Casa e Edificio e faça elas herdarem/estenderem de Imovel.
Ficando da seguinte maneira:
public class Imovel{
   // Atributos e Métodos comuns às classes filhas
   protected List<Porta> portas;
   protected String cor;
}

public class Casa extends Imovel{
   // Atributos e Métodos específicos de Casa
}

public class Edificio extends Imovel{
   // Atributos e Métodos específicos de Edificio
}

Comment: Era isso que eu precisava, agora entendi. Muito obrigado pelo help.
Comment: estou com o mesmo problema

Implementar classe pai


Uniform Continuity of $\frac{x^3}{1+x^2}$ in R

Question: Is the function $$\frac {x^3}{1 + x^2}$$ Uniformly continuous in R?
What I tried was this:
$$|f(x) - f(y)| = \lvert {x - \frac{x}{1 + x^2} - y + \frac{y}{1+y^2}}\rvert $$
$$ = |x-y|\cdot|\frac{x^2 + xy + y^2 + x^2y^2}{(1+x^2)\cdot(1+y^2)}|$$
I could not go further.
Comment: Domain of the function? Have you shown uniform continuity of any functions before? Although not completely true, the derivative being unbounded often implies the lack of uniform continuity , so you can study the derivative of the function. If the derivative is bounded we can think of the mean value theorem. (If my hints are helpful, kindly add any attempts you make into the question post, this will count for context. Or you can answer my questions, and then I will request you to put the answers into the question post).
Comment: Hint
: It is $x-\frac x {1+x^{2}}$ and the second term tends to $0$ as $|x| \to \infty$.
Comment: The domain is R
Answer: Continuing with your calculation:
$$
\left| \frac{x^2 + xy + y^2 + x^2y^2}{(1+x^2)\cdot(1+y^2)} \right|
\le \frac{x^2 + y^2 + x^2y^2}{(1+x^2)\cdot(1+y^2)} + \frac{|xy|}{(1+x^2)\cdot(1+y^2)} \, .
$$
The first term is
$$
 \le \frac{1+x^2 + y^2 + x^2y^2}{(1+x^2)\cdot(1+y^2)} = 1 \, ,
$$
and the second term is
$$
 = \frac{|x|}{1+x^2} \cdot \frac{|y|}{1+y^2} \le \frac 12 \cdot \frac 12 \, ,
$$
using the inequality between geometric and arithmetic mean. It follows that
$$
|f(x) - f(y)| \le \frac 54 \cdot |x-y|
$$
so that the function is Lipschitz continuous, and in particular uniformly continuous.
Another approach (as mentioned in the comments) would be to compute
$$
f'(x) = \frac{x^2 (x^2 + 3)}{(1 + x^2)^2}
$$
and show that it is bounded (which follows e.g. from the fact that numerator and denominator have the same degree).
Comment: Thanks a lot for your response Mr Martin R
Comment: There is a shorter answer using my comment above. This fiunction is a difference of two uniformly continuous functions.


Probability density function that evolves with time according to a delay differential

Question: Consider a real valued variable $X(t)$ that evolves with time according to the delay differential
$\frac{dX(t)}{dt} = \alpha X(t-t_0) \int_{t_0}^\infty f(y) h(t-t_0,y) dy - \beta X(t) \int_{t_0}^\infty g(y) h(t,y) dy$
where $h(t,z)$ is a time-evolving probability density over $z$ that is determined by $X$
$h(t,z) \propto \exp{[-\beta \int_{t_0}^z g(y) dy]} \alpha X(t-z)\int_{t_0}^\infty f(y) h(t-z,y) dy$.
Assume that functions $f()$ and $g()$ behave nicely.
My question is: does this make sense? It seems rather circular. In particular I'm concerned that in the limit $z\rightarrow 0$ the expression for $h(.,.)$ approaches a function of itself at an identical time. Perhaps I need to make it $h(t+dt,z)$ on the left hand side? If so, is there an intuitive way to understand this that will help me solve these equations numerically?
A curiosity (sign I'm doing something wrong?) of this formulation is that $z$ seems to sometimes behave like a random variable and sometimes not.
Answer: So I was on the right track, but my PDE made no sense.
Let's simplify by letting $t_0=0$ and removing the delay. Then
$$ \frac{\partial X}{\partial t}(t,z) + \frac{\partial X}{\partial z}(t,z) = -\beta g(z) X(t,z) $$
fully specifies the decay along so-called characteristics (loci where $t=z + c$) and
$$ X(t,0) = \alpha \int_0^\infty f(y)X(t,y) dy $$
is a time varying, non-local (because of the integral) boundary condition which governs growth / renewal.
Apparently this style of model was originally conceived by Scottish physician and amateur mathematician A. G. McKendrick in 1926, by all accounts a remarkable fellow.
The PDE reduces to an ODE along characteristics, greatly simplifying its solution.
Thanks to my friend George Leigh for help with this one.
Answer: Ok, here's my own stab at this.
First thing to realise is that you're dealing with a partial differential equation. To see this, consider the initial condition required: the distribution $h$ over $z$ must be completely specified, not just at a single point. Of course you also have delays making this a delay partial differential: the full initial condition is actually a function of both $z$ (from $t_0$ to $\infty$) and $t$ (from $t-t_0$ to $t$).
The second thing to realise is that it makes more sense to consider $X$ alone as the unknown function of $t$ and $z$, and just use integrals of $X$ to achieve the work done by $h$ when needed. This simplifies the situation considerably, as well as removing the random - non-random variable weirdness:
$\frac{\partial X}{\partial t}(t,z) = \alpha \int_{t_0}^\infty X(t-z,y)f(y)dy$
$\frac{\partial X}{\partial z}(t,z) = -\beta \int_{t_0}^z X(t,y)g(y)dy$
The intuitive picture here is of a rectangular two dimensional sheet moving smoothly forward along one dimension (the time dimension) and smoothly changing shape as it does so with the "front corner" increasing as a function of the "back edge" and and the front edge decaying from that increase as a function of the front edge.
As far as numerical solutions go, this is an integro delay partial differential, so it might be hard, but note that the rates of change appear to be independent of each other so there are probably some simplifying tricks.


How to make the phone number unique in WordPress

Question: I have added 3 custom field on woocommerce registration page.
My code so far:-
/* Add Extra Registration Field in Woocommerce */

//Add First Name, Last Name, and Phone Number
function woocom_extra_register_fields() {?>

       <?php _e( 'Phone', 'woocommerce' ); ?>*
       <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" />

       <?php _e( 'First name', 'woocommerce' ); ?>*
       <input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />

       <?php _e( 'Last name', 'woocommerce' ); ?>*
       <input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />

       <?php
 }
 add_action( 'woocommerce_register_form_start', 'woocom_extra_register_fields' );

//Validate Fields
function woocom_validate_extra_register_fields( $username, $email, $validation_errors ) {

      if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {

             $validation_errors->add( 'billing_first_name_error', __( 'Error: First name is required!', 'woocommerce' ) );

      }

      if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {

             $validation_errors->add( 'billing_last_name_error', __( 'Error: Last name is required!.', 'woocommerce' ) );

      }
      if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {

             $validation_errors->add( 'billing_phone_error', __( 'Error: Mobile number is required!.', 'woocommerce' ) );

      }
         return $validation_errors;
}

add_action( 'woocommerce_register_post', 'woocom_validate_extra_register_fields', 10, 3 );

//Save Fields
function woocom_save_extra_register_fields( $customer_id ) {
    if ( isset( $_POST['billing_phone'] ) ) {
                 // Phone input filed which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
          }
      if ( isset( $_POST['billing_first_name'] ) ) {
             //First name field which is by default
             update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
             // First name field which is used in WooCommerce
             update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
      }
      if ( isset( $_POST['billing_last_name'] ) ) {
             // Last name field which is by default
             update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
             // Last name field which is used in WooCommerce
             update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
      }

}
add_action( 'woocommerce_created_customer', 'woocom_save_extra_register_fields' );

/* END Add Extra Registration Field in Woocommerce */

Everything is working fine but the problem is that its accepting the dupliacte phone numbers. How can i make the phone number field unique? Any idea will be really appreciated.
Thanks.
Comment: JQuery should help you to prevent duplicate values.
Answer: Try to use a get_users function by meta_value if value exists execute validation:
 if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {
             $validation_errors->add( 'billing_phone_error', __( 'Error: Mobile number is required!.', 'woocommerce' ) );

      }
if ( isset( $_POST['billing_phone'] ) ) {
   $hasPhoneNumber= get_users('meta_value='.$_POST['billing_phone']);
   if ( !empty($hasPhoneNumber)) {
     $validation_errors->add( 'billing_phone_error', __( 'Error: Mobile number is already used!.', 'woocommerce' ) );
   }
}

Comment: Thank you so much buxbeatz.. You saved my life :)


Making a request to a specific server with "speedtest-cli" for python

Question: I wrote a code that does a speed test every 3 hours using python. get_best_server() can make requests to servers far away from me. When I do it on the website, it really finds the closest city-based. I have the server id closest to me, how can I request it?
    import speedtest

    test = speedtest.Speedtest(secure=1)
    test.get_servers()
    best = test.get_best_server()

    print(f"Found:{best['host']} located in  {best['country']}")

    download_result = test.download()
    upload_result = test.upload()
    ping_result = test.results.ping

    print(f"Download Speed: {download_result / 1024 / 1024:.2f} Mbit/s")
    print(f"Upload Speed: {upload_result / 1024 / 1024:.2f} Mbit/s")
    print(f"Ping: {ping_result}s")

Answer: Late answer, but I face the same issue; here's how I solved it (official example) :
import speedtest

test = speedtest.Speedtest(secure=True)

#define the list of servers that you want to run against
test.get_servers(servers=["5738","10241"])

best = test.get_best_server()
print(f"Found:{best['host']} located in  {best['country']}")

download_result = test.download()
print(f"Download Speed: {download_result / 1024 / 1024:.2f} Mbit/s")

upload_result = test.upload()
print(f"Upload Speed: {upload_result / 1024 / 1024:.2f} Mbit/s")

ping_result = test.results.ping
print(f"Ping: {ping_result}s")

However, since you add secure=True if your server-list contains servers without HTTPS, it gives you an errors. Because of that, either remove secure=True or only include servers with HTTPS.


Is there a test to determine whether GLM overdispersion is significant?

Question: I'm creating Poisson GLMs in R. To check for overdispersion I'm looking at the ratio of residual deviance to degrees of freedom provided by summary(model.name).
Is there a cutoff value or test for this ratio to be considered "significant?" I know that if it's >1 then the data are overdispersed, but if I have ratios relatively close to 1 [for example, one ratio of 1.7 (residual deviance = 25.48, df=15) and another of 1.3 (rd = 324, df = 253)], should I still switch to quasipoisson/negative binomial? I found here this test for significance: 1-pchisq(residual deviance,df), but I've only seen that once, which makes me nervous. I also read (I can't find the source) that a ratio < 1.5 is generally safe. Opinions?
Answer: In the R package AER you will find the function dispersiontest, which implements a Test for Overdispersion by Cameron & Trivedi (1990).
It follows a simple idea: In a Poisson model, the mean is $E(Y)=\mu$ and the variance is $Var(Y)=\mu$ as well. They are equal. The test simply tests this assumption as a null hypothesis against an alternative where $Var(Y)=\mu + c * f(\mu)$ where the constant $c < 0$ means underdispersion and $c > 0$ means overdispersion. The function $f(.)$ is some monoton function (often linear or quadratic; the former is the default).The resulting test is equivalent to testing $H_0: c=0$ vs. $H_1: c \neq 0$ and the test statistic used is a $t$ statistic which is asymptotically standard normal under the null.
Example:
R> library(AER)
R> data(RecreationDemand)
R> rd <- glm(trips ~ ., data = RecreationDemand, family = poisson)
R> dispersiontest(rd,trafo=1)

Overdispersion test

data:  rd
z = 2.4116, p-value = 0.007941
alternative hypothesis: true dispersion is greater than 0
sample estimates:
dispersion
    5.5658

Here we clearly see that there is evidence of overdispersion (c is estimated to be 5.57) which speaks quite strongly against the assumption of equidispersion (i.e. c=0).
Note that if you not use trafo=1, it will actually do a test of $H_0: c^*=1$ vs. $H_1: c^* \neq 1$ with $c^*=c+1$ which has of course the same result as the other test apart from the test statistic being shifted by one. The reason for this, though, is that the latter corresponds to the common parametrization in a quasi-Poisson model.
Comment: I had to use `glm(trips ~ 1, data = data, family = poisson)` (i.e. `1` rather than `.` for my data), but great, thank you
Answer: An alternative is the odTest from the pscl library which compares the log-likelihood ratios of a Negative Binomial regression to the restriction of a Poisson regression $\mu =\mathrm{Var}$. The following result is obtained:
>library(pscl)

>odTest(NegBinModel)

Likelihood ratio test of H0: Poisson, as restricted NB model:
n.b., the distribution of the test-statistic under H0 is non-standard
e.g., see help(odTest) for details/references

Critical value of test statistic at the alpha= 0.05 level: 2.7055
Chi-Square Test Statistic =  52863.4998 p-value = < 2.2e-16

Here the null of the Poisson restriction is rejected in favour of my negative binomial regression NegBinModel. Why? Because the test statistic 52863.4998 exceeds 2.7055 with a p-value of < 2.2e-16.
The advantage of the AER dispersiontest is the returned object of class "htest" is easier to format (e.g. converting to LaTeX) than the classless 'odTest`.
Answer: Yet another option would be to use a likelihood-ratio test to show that a quasipoisson GLM with overdispersion is significantly better than a regular poisson GLM without overdispersion :
fit = glm(count ~ treatment, family="poisson", data=data)
fit.overdisp = glm(count ~ treatment, family="quasipoisson",
                             data=data)
summary(fit.overdisp)$dispersion # dispersion coefficient
pchisq(summary(fit.overdisp)$dispersion * fit$df.residual,
               fit$df.residual, lower = FALSE)
          # significance for overdispersion

Comment: I'm curious about this. From what I understand the quasipoisson method is not maximizing a 'true' likelihood, so would it be valid to compare the 'quasi-likelihood' from the quasipoisson fit with the 'true' likelihood of the regular poisson fit?
Comment: @bandwagoner Good question - not totally sure myself. Would be worth doing some proper simulations to look at the performance of the different tests suggested here in the answers to check which one performs best...
Answer: Another alternative is to use the P__disp function from the msme package. The P__disp function can be used to calculate the Pearson $\chi^2$ and Pearson dispersion statistics after fitting the model with glm or glm.nb.


Why is there a pattern for making orders of perfect squares (first one, second one, third one) by simply adding two to the next adding each time?

Question: For example, if I had a perfect square of $16$, which is the fourth perfect square, I would add nine to get to the fifth perfect square, $25$.  This is probably how it goes:$$1+3=4+5=9+7=16+9=25+11=36+13=49+15=64+17=81+19=100$$and it goes on forever.  It looks like the amount being added each time gets added by two each time as they move on to the next and the next and the next and the next.
Answer: Two ways of proving that:
- by induction
- by drawing something like: http://reflectionsinthewhy.files.wordpress.com/2011/09/sum-of-consecutive-odd-numbers.jpg?w=584&h=902
Answer: First, think of the set of numbers to the left of each equal sign as a member of a sequence. $\{1+3=4, 4+5=9, 9+7=16, 16+9=25,...a_n\}$ There's a pattern on the left side:$$a_{n_L}=n^2+2n+1$$The right side is $a_{n_R}=(n+1)^2$. Expanding this results in $n^2+2n+1$. Does that look familiar?Edit: I just realized this thread was a duplicate question. Oops...


Execute write on doc: It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.

Question: I am trying to load a certain script after page load executes, something like this:
function downloadJSAtOnload(){
            var element = document.createElement("script");
            element.src = "scriptSrc";
            document.body.appendChild(element);
        }

         if (window.addEventListener)
                  window.addEventListener("load", downloadJSAtOnload, false);
            else if (window.attachEvent)
                  window.attachEvent("onload", downloadJSAtOnload);
            else window.onload = downloadJSAtOnload;

And while this script seems to execute and download 'scriptSrc', and append it right before the end of the body tag, it yields the following message (not an error) in the console (chrome)
Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
What does this even mean? And am I supposed to do something differently? Even though I get the expected behavior?
Comment: It means the script you are loading executes `document.write`, which is not supported when you add it to the page via a `
Comment: recently found a fantastic library on github that gets around this "restriction" nicely: https://github.com/krux/postscribe
Comment: got it, but I can't change that script. Call it bureaucracy. Can I get the script to load synchronously? No right, cause it happens after page load.
Comment: Yes, you can load the script contents synchronously with AJAX, then `eval` it, though I'm not 100% sure whether that would work.
Comment: @univerio I am also facing same issue. `document.write('\x3Cscript type="text/javascript" src="http://ads.appnexus.com/ttj?id=xyzpqr&cb=${CACHEBUSTER}">\x3C/script>');` this is working perfectly but when I create a script using createElement('script') I am facing same issue. Can you suggest something here?
Comment: @univerio I'm fine if script run Synchronously, I set async false property too but no luck.
Comment: @RockStar Don't use `document.write`. If you can't change the script that uses it, use postscribe as suggested above.
Comment: @univerio I used postscribe looks like somehow it sort out my problem. But this tag we send to client so every JavaScript tags we needed to include this lib from cdn which is again synchronous. So what is best way to use that lib with our JavaScript tag which we give to client. If one page having 5 tags and each every loading postscribe from cdn synchronously not looks good.
Comment: Google ads is causing this issue for me
Comment: Same issue here with async set to false. I still get the same error. Any ideas if this is the same problem with document.write? Website url where this issue happens: https://teamhood.com/
Comment: See https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/dom/document.cc;l=4217-4226;drc=07b6b88e38cb2e2b8dfaccf25b27fefd66766349 and the spec https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#ignore-destructive-writes-counter: _Document objects have an ignore-destructive-writes counter, which is used in conjunction with the processing of script elements to prevent external scripts from being able to use document.write() to blow away the document by implicitly calling document.open(). Initially, the counter must be set to zero._
Answer: An asynchronously loaded script is likely going to run AFTER the document has been fully parsed and closed.  Thus, you can't use document.write() from such a script (well technically you can, but it won't do what you want).
You will need to replace any document.write() statements in that script with explicit DOM manipulations by creating the DOM elements and then inserting them into a particular parent with .appendChild() or .insertBefore() or setting .innerHTML or some mechanism for direct DOM manipulation like that.
For example, instead of this type of code in an inline script:

You would use this to replace the inline script above in a dynamically loaded script:
var container = document.getElementById("container");
var content = document.createElement("span");
content.style.color = "red";
content.innerHTML = "Hello";
container.appendChild(content);

Or, if there was no other content in the container that you needed to just append to, you could simply do this:
var container = document.getElementById("container");
container.innerHTML = 'Hello';

Comment: @Pacerier - Using `document.write()` after the document has finished loading causes the current document to be cleared and then the write goes into a new blank document.  If you want that behavior, then just clear the current DOM before inserting your content.  If you have a more detailed question than that, then please ask a new question, show the code you have and describe the desired outcome.
Comment: But am I not doing something similar when I state "document.body.appendChild(element)" ?
Comment: @jfriend, Re "*but it won't do what you want*", What if it does? **How do I enable it if I want it?** (This used to work on older browsers.)
Answer: A bit late to the party, but Krux has created a script for this, called Postscribe. We were able to use this to get past this issue.
Comment: what is best way to pass this lib with our JavaScript lib? Situation is that we give JS tags to client which runs on there website.
Comment: We gave JavaScript CDN link to website owner to place advertise using this tag. We can't able ask every owner to place this postscribe.js so we need to give it with our JS.
Comment: I'm not 100% sure what you're asking here @RockStar but any normal means of including JS should work. You could pull up https://cdnjs.com/libraries/postscribe and copy/paste the code into an existing JS file, or you could just add a
Comment: postscribe doesn't seem to do anything at all unless you explicitly use the `postscribe` function - very disappointing
Comment: @RockStar What is the solution which you got for your problem. Even I have the same problem
Answer: In case this is useful to anyone I had this same issue. I was bringing in a footer into a web page via jQuery.  Inside that footer were some Google scripts for ads and retargeting.  I had to move those scripts from the footer and place them directly in the page and that eliminated the notice.
Comment: I had the same issue, but the script was inside a bundle created by a Tag Manager. The issue was caused by the `async` attribute, and the only solution was to remove that attribute, as I am not allowed to move scripts from our Tag Manager.
Answer: You can also call
document.open() before document.write()

call
document.close()

when you're done.
It may not be best practice for a real webpage but for testing etc.. can be used.
Comment: Really useful for multiple writes is `document.writeln` with `document.body.style.whiteSpace = "pre";` at the end


Importing pages with different node ids to the live site

Question: We have a site in drupal 6.
It's placed in 2 different server with separate databases. One is used for staging and the one is used for live.
We have created a new language section and imported the data with xliff tool to the staging. We have completed the modifications in staging and now we need to push them to live.
But the thing is that the node id's are different in staging and live, and also some pages contains view , block and nodes.
And also I must say that I am a newbie in drupal.
I would appreciate if someone could show me a way to import this new language pages to the live site without using xliff.
Thanks in advance
Comment: I am closing this as too broad, but this topic has come up many times on this site.  Search for a while and see if any of the solutions help.  Unfortunately, this isn't the easiest operation in Drupal 6 and 7.  If you still need help with select portions, narrow your question down a bit and flag for re-opening.
Answer: I've had this problem myself in the past, and importing nodes into a livesite can be quite a head ache at times.
Luckily there's a great module available for D6 called Node Export. It allows you to import / export nodes using a host of different formats such as JSON, CSV, XML etc.
I've used it before exporting a large dataset and I was impressed with how easy it was to implement.
Hope this helps.
Comment: Thank you very much for the tip. I'll try it soon enough and write it the results here.
Comment: I'd like to thank you again. It has been very helpful and saved me a lot of time. Cheers.
Comment: No problem Onur, glad I could help out. I'm sure syncing content between D6 environments has given me a few extra grey hairs over the years :)


Does this sequence have a closed form representation?

Question: We know that
$$ \sum_{s=0}^\infty \frac{\lambda^{s}}{s!} = e^\lambda$$
Relatedly,
$$ \sum_{s=1}^\infty \frac{\lambda^{s}}{s!}s = \lambda \sum_{s=1}^\infty \frac{\lambda^{s-1}}{(s-1)!}$$
For which we can again find a closed form solution.
How about  $$ \sum_{s=1}^\infty \frac{\lambda^{s-1}}{(s-1)!}s$$?
I can't find any trick here to factor out the $s$. Is there any other definition or feature I can use?
Answer: $$\frac{\lambda^{s-1}}{(s-1)!}s=\frac{\lambda^{s-1}}{(s-1)!}(s-1)+\frac{\lambda^{s-1}}{(s-1)!}$$
Can you go on?
Answer: Hint
$$\sum_{s=1}^\infty \frac{\lambda^{s-1}}{(s-1)!}s=\frac{\mathrm d }{\mathrm d \lambda}\sum_{s=1}^\infty \frac{\lambda ^s}{(s-1)!}$$
Answer: $$\sum_{s\geq 1}\frac{s}{(s-1)!}\lambda^{s-1} = \sum_{n\geq 0}\frac{n+1}{n!}\lambda^{n}=\frac{d}{d\lambda}\left(\lambda e^{\lambda}\right)=\color{red}{(\lambda+1)\,e^{\lambda}}.$$
Comment: @FooBar: I set $s=n+1$, the wrote $(n+1)\lambda^{n+1}$ as $\lambda\cdot\frac{d}{d\lambda}\lambda^{n+1}$, and you (should) know that $$\sum_{n\geq 0}\frac{\lambda^{n+1}}{n!}=\lambda\cdot e^{\lambda}$$
Comment: However, my previous series dealt with $\lambda^s$ insted of $\lambda^{s-1}$. Now fixed.


Active Directory User Copy within the same domain/forest

Question: we have set up Active Directory Replication within the same domain/forest between two Windows Servers 2012. The Master Server, copies its contents to the secondary server. Unfortunately, for a number of users, instead of creating the user on the master server, we created the user on the secondary server.
Is there a way to manually copy each account from the secondary server to the Master server?
Comment: What is the actual problem? In a std config it shouldn't really matter if a user was created on the primary DC or the secondary DC as far as I know.
Comment: I don't understand your question. Please give us more details.
Comment: There is no primary/secondary concept for DCs. They may hold different FSMO, but all DCs have same data for same domain. Sounds like you have replication issue?
Comment: The problem is that when I go to the primary DC, the user doesn't appear there, but only on the secondary server were it was created. The replication is set one way: from the primary DC to the secondary DC, so replication works alright (i just checked it - created a user on the master instantly copied to the other), but one way.
Comment: scratch that... I had the console open for a while and it wasn't refreshing. I had to close and reopen it... then the user appeared.
Answer: The console was left open and it had to be closed and reopened for the user to appear. Notjusttime, as well as Strongline have made it clear that sync works both ways.


Three.js Objects not showing, there is only a black screen shown

Question: I've been having this similar issue with a few of my other three.js codes as well. I set up the js within html but the objects are not showing up. There's just a black screen showing up when I run the file.
The file is supposed to show rain drops falling off the sky when it is run.
I used atom to build it. I tried html preview on atom, atom live server internet explorer chrome and they all showed the same black screen when i tried running it.
<!DOCTYPE html>

    <meta charset=UTF-8 />
    <link rel="stylesheet" type="text/css" href="styles.css" />
    <title>RAIN ON ME BABY</title>

Answer: Your issue is actually a duplicate of: https://stackoverflow.com/a/57114890/5250847
With R016, it's not possible to create THREE.Points with THREE.Geometry. This is already fixed in dev and eventually with the next release R107 at the end of July.
I've copied your code to the following fiddle and replaced the build file with the current dev version which fixes the issue.
https://jsfiddle.net/76tw2jL0/
BTW: The best fix is actually the usage of THREE.BufferGeometry.


Update of the cell state functions in LSTM RNNs - Interplay of sigmoid and tanh

Question: In trying to get some sort of intuition into what goes on inside an LSTM, there is a step that has the potential to make things fall in place nicely.
Here is a diagram from this tutorial illustrating the step in question with nomenclature notes added (feel free to correct mistakes):

I understand $[h_{t-1}, x_t]$ as the vector (or matrix (?)) concatenating the previous activation layer or output $(h_{t-1})$ and the new input $x_t$, while sigma $(\sigma)$ stands for sigmoid activation. $\tilde C_t$ is the update gate (?) or proposed new cell state.
I presume that $\otimes$ in the diagram (minor design differences aside) is the Hadamard product.
Both $\sigma$ in the $i_t$ equation, and $\tanh$ in the $\tilde C_t$ formula take in the concatenated vector $[h_{t-1},x_t]$ with learned weights and biases.
The question is:

What is the intuition of the purpose behind obtaining the sigmoid
  activation and the tanh activation of the same vector, and then
  combining them through the element-wise multiplication (Hadamard)?

I see that sigmoid provides a weight for each element of the vector, but what is the purpose / convenience / usefulness in using $\tanh$? In part the answer lies in minimizing the vanishing gradient problem, but there are more patterns and ideas behind the $\sigma / \tanh$ interplay.
For instance, in this Quota answer:

When the portion of signal arrives, the gate regulates which parts of the signal should be allowed into the unit and how much of those parts should be allowed. That's why the gates use sigmoid activation function which takes values from 0 to 1 and not tanh (-1 to 1), because it functions like a filter.

... this concepts seem to fall in place.
Answer:
sigmoid squashes into the range 0 .. 1.  The output of a sigmoid is like a binary on/off. But a binary on/off is not differentiable, cannot learn through back-propagation, so we 'soften' the binary on/off 0/1, to a sigmoid, which is differentiable
the tanh is the actual updates that we want to apply, everywhere that the sigmoid is outputting 1, or near to 1.  tanh squashes into the range -1 .. 1. It can be negative as well as positive, since we might wish to apply negative changes to the cell state.
Comment: Thank you, Hugh, for your answer. Sensing a decreasing likelihood of responses, I continued digging out info, to the point of spilling over into a question + answer post. So I wonder if you could include some lines explaining that very specific operation vis-a-vis the difference between gate recurrent connections and cell recurrent connections.
Comment: Please feel free to go ahead and put your new-found knowledge into a separate answer. Its one of the best ways to learn, and consolidate your knowledge :-)
Comment: Can someone please provide a real example for the intuition behind tanh function? Before this there is a forget gate. Which could be considered as ignoring gender (mentioned in the blog). But for -1 rather than forgetting, are we deleting some information taken from the new input at this state t? As per my understanding, Sigmoid basically acts on previous state and tanh acts on current input.
Comment: I have upvoted 3 excellent questions of yours to compensate for the un-accepted vote on this current answer. I believe the OP is open to a more extensive answer, perhaps spurred by my very tentative answer, and would like to "reopen" it.


Apache gettext - cache persisting after restart

Question: Running apache2 through Plesk. When I update the .mo files via subversion, and then restart apache (either through commandline or through Plesk interface), the translations do not update. Modified timestamps on the files are correct.
Is there any way to manually reset the cache?
Thanks!
Answer: Turns out it wasn't a caching problem.
I had two languages en_US and de_DE. Gettext was only showing the key and not the translation for en_US, which wasn't noticeable because the keys are in English anyway.
So gettext was working for one language, and not for another...
Solution:

Add the en_US.UTF-8 locale using locale-gen en_US.UTF-8 (I'm on ubuntu).
Restart apache.

(You can check which locales are installed using locale -a.)


how to write the joint density of two correlated uniform random variables?

Question: Suppose the marginal distributions of $X$ and $Y$ are both $U[a,b]$, where $U[a,b]$ denotes uniform distribution on $[a,b]$. When $X$ and $Y$ are correlated, how to write their joint density for some prespecified measure of correlation?
Comment: In case it comes up, and I think it will (or at least should), do you know the term "copula"? Also, do you have a plot in mind? It would help to have some idea of what you mean by correlation. For instance, according to you, for $X \sim U(0,1)$, are $X$ and $X^2$ correlated even though the relationship isn't linear? (Remember that for this $X$, $cor(X,X^2)\ne 0$.)
Comment: @Dave Thanks. Yes, the correlation is not restricted to be linear. I guess indeed copula need to be used here.
Comment: One possibility, with positive correlation is as follows:
$f(x,y) = 2$ if $0<x<\frac12, 0<y<\frac12$ or $\frac12 < x <1, \frac 12 < y <1$, and 0 otherwise. Another with positive correlation has $X\equiv Y,$ concentrating all probability on a line. Similarly, $X\equiv -Y$ has negative correlation.
Comment: @BruceET in your first example, is that just filling in the first and third quadrants (so to speak)?
Comment: @Dave: That was my intention; never quite sure how JaX works in Comment mode.
Answer: I think the right place to start is what correlation means in a nicer setting: Gaussian, of course.
In the bivariate Gaussian distribution, the population correlation is given by the $\rho$ parameter. This tells us something about the fit to a line.
I have tried fitting marginal uniform distributions to a line and failed, but we can still get at the $\rho$ parameter of the bivariate Gaussian.
That $\rho$ parameter is the parameter of the bivariate Gaussian copula, and we can use a Gaussian copula with more than just Gaussian marginal distributions. We can use uniform marginal distributions and specify the parameter in the Gaussian copula, giving us a way to describe the relationship between the two marginal distributions.
When I have simulated this, I have not gotten the sample correlation to be the specified parameter of the Gaussian copula, so I do not know if this quite counts as specifying the correlation. However, the estimate in R's cor function is biased, perhaps accounting for this discrepency. I welcome discussion in the comments about this topic.
(Remember that a biased estimator isn't inherently a bad estimator; in fact, $s$ is a biased estimator for standard deviation, even though $s^2$ is unbiased for variance, so we use biased estimators all the time.)
library(copula)
set.seed(2020)
N <- 1000
R <- 10000

# define the copula
#
nc <- normalCopula(param = 0.81)

# Define the population distribution with the nc copula and U(0,2) marginals
#
unif_unif <- mvdc(nc, c("unif","unif"),list(list(min=0, max=2),list(min=0, max=2)))
v <- rep(NA, R)
for (i in 1:R){

  # Sample from the population
  #
  D_uu <- rMvdc(N, unif_unif)

  # Calculate the correlation
  #
  v[i] <- cor(D_uu[,1], D_uu[,2])
  if (i %% 250 == 0){print(i)}
}
plot(density(v))
abline(v=0.81)
mean(v) # 0.7963396, slightly lower than the specified 0.81

EDIT
Let's look at some pictures. Here is the last distribution produced in the simulation: plot(D_uu).

Now compare to a bivariate normal distribution with population correlation $0.81$: plot(qnorm(D_uu[, 1]/2), qnorm(D_uu[, 2]/2)). (This uses the same (Gaussian) copula to relate the marginals but gives each marginal a normal distribution.)

Both plots have the data kind of hugging the line $y=x$, and both see a bulge in the perpendicular direction. However, the behavior of the marginal normal distributions causes more points to be near the mean than in the marginal uniform distributions (consider the PDFs).


Can a first order homogeneous ODE have singular solution(s) (free of a constant)

Question: Can $$y'=\frac{px+qy}{rx+sy}$$ have singular (essential) solution(s) free of a constant of integration? Here, $p,q,r,s$ are independent of $x$ and $y$.
Comment: no, because $(y+c)'=y'$
Comment: For any real $c$, if $y(x)$ satisfies the DE, then so does $cy(x/c)$. Not sure if this helps you.
Answer: If $y=f(x)$ is solution of an ODE then $f(x)+C$ or $Cf(x/C)$ may or may not be a solution of the ODE For example $y'+y=0$, it's solution is $e^{-x}$ but $y= e^{-x}+3$ does not satisfy it. Similarly, for $y'+y^2=0$, $y=1/x$ is a solution but $y=3/(x/3)$ does not satisfy it. Here, the second ODE is non-linear and $y=1/x$ is its singular (fixed or essential) solution which is free of any arbitrary constant.
We want to bring to attention  that the commonly encountered first order homogeneous ODE: $$y'=\frac{px+qy}{rx+sy},\quad  p,q,r,s \in \Re ~~~(1)$$ may have singular solutions which are often missed out. To get them one should take $y=vx$ (take $v$ as constant for change!). We get $$sv^2+(r-q)v-p=0$$. So $$v_1,v_2=\frac{(q-r)\pm\sqrt{(r-q)^2+4ps}}{2s}.$$
If $(r-q)^2 \ge -4ps$, there will be two real one real root giving us the singular solutions of this (1) ODE as two lines $y=v_1x$ and $y=v_2x$.
One usually takes $y=vx$ and $y'=xv'+x$ in (1) to get the general solution having one constant but misses out the possible real singular solutions: $y=v_1x$ and $y=v_2x$ as brought out here.
Also, if (1) is written as $\dot X=M X$ as below $$ \frac{d}{dt} \begin{bmatrix} x \\
y \\ \end{bmatrix} = \begin{bmatrix} r & s\\ p & q \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix}$$
then $v_1$ and $v_2$ are nothing but eigenvalues of the matrix $M$.


How to remove a stylesheet provided by a contrib module?

Question: The new version of Field Group has a new stylesheet included by default that makes my theme look hideous.  Deleting the stylesheet from the module's folder fixes the problem but then I will have to delete it again when Field Group is updated again.
What's the right way (= not modifying the module's code directly) to do this?
Answer: I think using hook_css_alter() in a custom module/theme would be the easiest solution:
function MYMODULE_css_alter(&$css) {
  // Get the path to the module
  $path = drupal_get_path('module', 'field_group');

  // Remove the unneeded files from the css array
  unset($css[$path . '/field_group.css']);
  unset($css[$path . '/field_group.field_ui.css']);
}

There are also other CSS files in the Field Group module:

horizontal-tabs/horizontal-tabs.css
multipage/multipage.css

In case you're look to remove absolutely all the CSS that Field Group introduces.
Comment: Thank you! In this case I only needed to remove `multipage/multipage.css`.
Answer: You can also do this in your theme's .info file.  Check out the documentation from drupal.org.
If you want to remove css, just add a line like this in your .info file:
stylesheets[all][] = system-menus.css
If you have a stylesheet by that name in your theme, then that will replace the one provided by the module.  If you don't have that stylesheet in your theme, then Drupal will just omit that stylesheet altogether.
Looks like there was a bug with this method for a while, but it has seemingly been fixed.


Intermediate Value Theorem, an application problem

Question: The question is: if $$\frac{a_0}{1}+\frac{a_1}{2}+\dots+\frac{a_n}{n+1}=0$$
then, $a_0+a_1x+\dots+a_nx^n=0$ for some $x$ in the interval $[0,1]$.
My approach is to let $f(x)=a_0+a_1x+\dots+a_nx^n$ and the find $x_0$ and $x_1$ such that, for example, $f(x_0)\leq0$ and $f(x_1)\geq0$ and then use IVT to make the conclusion.
However, I do not know how to use the given condition, especially I am not sure about if $n$ is odd or even and if the leading coefficient, $a_n$ is positive or negative.
Any help will be appreciated. Thanks so much.
Answer: Hint: The given equation says that $\displaystyle\int_0^1 f(x)\,dx = 0$.
Is it possible to have $f(x) > 0$ for all $x \in [0,1]$? If not, $f(x_0) \le 0$ for some $x_0 \in [0,1]$.
Is it possible to have $f(x) < 0$ for all $x \in [0,1]$? If not, $f(x_1) \ge 0$ for some $x_1 \in [0,1]$.
Comment: thanks and I can guess the function to achieve my target now.
Answer: $$ \cdot f(x) = a_0+a_1x+\dots+a_nx^n , F(x)= \frac{a_0x}{1}+\frac{a_1x^2}{2}+\dots+\frac{a_n x^{n+1}}{n+1}.$$
Comment: $F(0) = F(1) = 0 \Rightarrow \exists z \in (0,1) $ such that $F'(z) = f(z) = 0$ by Rolle's Theorem.


Subring of $\mathbb{R}$

Question: In the field of rational numbers, the following subsets are given:
$$
M = \left\{ a + b\frac{1+\sqrt{17}}{2}\right\} a,b \in \mathbb{Z}\
\text{and}\
N = \left\{ a + b\frac{1+\sqrt{7}}{2}\right\} a,b \in \mathbb{Z}
$$
Show that $M$ is a subring of $\mathbb{R}$ and $N$ in NOT a subring of $\mathbb{R}$.
I tried using the following conditions which give us that a subset is a subring of a ring:
$1.\  a+b \in M$ for all $a,b \in M$
$2.\ ab \in M$ for all $a,b \in M$
$3.\ (M,+) \le (\mathbb{R},+)$
But I get stuck on the second one, multiplication. I can't get what is the difference between $\sqrt{17}$ and $\sqrt{7}$. Why the first is a subring, the second - NOT?
Any help would be appreciated!
Comment: Multiply $\frac{1 + \sqrt{a}}{2}$ by itself (where $a$ is either $7$ or $17$) and look at what you get, whether you can see that it is again of the form required. The problem is the $2$ in the denominator. This gives you a constraint.
Comment: @Sasha, In both subsets, I don't get the form I need. I can't get the hint you are saying.
Answer: $$ \left( \frac{1 + \sqrt{17}}{2}\right)^2 = \frac{9+\sqrt{17}}{2} = 4 + \frac{1+\sqrt{17}}{2}$$
$$ \left( \frac{1 + \sqrt{7}}{2}\right)^2 = \frac{4+\sqrt{7}}{2} = \frac{3}{2} + \frac{1+\sqrt{7}}{2}$$
$4$ is an integer, $\frac{3}{2}$ is not.

Subring of $\mathbb{R}$


Problema ao verificar se um tipo any é undefined no Typescript

Question: estou tendo um problema ao verificar se uma variável é undefined no seguinte código em typescript.
Atendimento:any;

this.Atendimento.Checkout.Observacao == undefined ? '' : this.Atendimento.Checkout.Observacao;

Se o Atendimento não retorna nada do webservice, consequentemente setando o Atendimento para undefined, o seguinte código dá erro dizendo que a variável this.Atendimento.Checkout.Observacao está undefined, ignorando o if, alguém saberia como resolver(Sem eu precisar declarar uma classe modelo, pois fazer isso daria um trabalhão no sistema) e por que ocorre esse problema?
Comment: Tem alguma forma de checar isso em um if?
Comment: Pedro o uso do Any em Typescript em muitas ocasiões é considerada uma má prática, exatamente como neste seu caso, você tem um objeto que contém um outro objeto que contém algumas propriedades, o ideal seria ter todos os nós tipados para evitar os temidos Undefined e não ter que ficar fazendo If nó por nó como no exemplo do nosso amigo Rodrigo.
Comment: Se não me engano é possível usar algo como `this.?Atendimento.Checkout.Observacao` mas não me lembro como funciona
Answer: Pode fazer a verificação a cada nó.

this.Atendimento == undefined || this.Atendimento.Checkout == undefined || this.Atendimento.Checkout.Observacao == undefined ? '' : this.Atendimento.Checkout.Observacao;
Comment: Pensei que tivesse um código menor para fazer essas validações, mas obrigado, ajudou bastante.
Answer: Se não estiveres a instanciar a tua variavel dentro de algum ctor, podes fazer logo. Se WebService garantir que o object Checkout é instanciado, não irás precisar de mais verificações sem ser se as observações são vazias ou com valor.
this.Atendimento === undefined ? alert("null") : alert("instanciado");

Exemplo aqui
Answer: Provavelmente o que está acontecendo é que this.Atendimento.Checkout está undefined, logo ao tentar acessar o atributo "Observacao" o erro é disparado.
Faça múltiplas validações como na resposta do Rodrigo Costa.