How to prevent recursion when setting a data value for a user in rules?

Question: I have a rule that is triggered on the event After updating an existing user account.  The rule performs an action, Set a data value, that changes one of the values of the user account fields.
However, doing this causes the user account to be saved again, which triggers After updating an existing user account a second time.  This causes some of my other rules that are triggered after updating a user account (such as those for sending notification e-mails) to be triggered twice.
I know this Set a data value rule is the issue because when I disable it, the account is only sent once, and the other rules that send notifications only send a single notification.
Is there a way to avoid this (single) recursion when setting a data value with a rule?
Answer: Your question seems like a variation of the question 'Why is my rule that reacts on event "After updating an existing user account" firing twice?'. The accepted answer in that question might help to understand your issue (what's causing it) also, ie:

some other module code is calling a user_save on hook_user_update triggering the rules event a second time.

That question also has another answer with a very interesting workaround, with an image which looks like so (a picture is worth more then 1000 words):
Answer: As per my understanding you need Rules Conditional module. We can run loops in rules action as well as we can put OR, AND conditions.
So at one time you can run loop for multiple actions which can solve your problem but you will have to workaround.
Edited texts --
You can set a field in profile with display hidden status. Name as per your like e.g. - WORKFLOW.
Use a taxonomy vocab for this field value where you add action names which you perform. If you have multiple action which need to be performed only once then set this field to have multiple value.
When you perform some action which need to perform only once, add one more action relevant to this action & for which a term is available in vocab such as "mailsent","sms-sent" etc..
Now go to the condition and add one more condition where you have to check whether that particular field WORKFLOW doesn't have this value.
So when once your mail action is performed, it will add a value "mailsent" & next time your condition will deny proceeding this rules s it will get "mailsent" in the field value..
I hope it helps..
Comment: My issue is that running the action **Set a data value** on the user profile causes the event **After updating an existing user account** to be performed again.  How does this module help with that problem?
Comment: You can run the same action in the loop when you are performing some more action such as sending email etc..
Comment: I don't want to run the same action multiple times.  Instead, I want the event **After updating an existing user account** to only be evaluated once.
Comment: @PatrickKenny As per my understanding & experience with rules, it doesn't invoke some hook, so that other action get performed again.
You need to combine your all action at one place which ever you want to perform, or use some field as condition in other rules which you don't want to be performed if you have such field like those which you are updating again..
Comment: @PatrickKenny - I have edited my answer & added one workaround solution, which may help you.
Comment: You misunderstand my question still.  I did not ask how to ensure that one given action is performed only once; I want to prevent the **After updating an existing user account** event from being triggered a second time (as a result of using a **Set a data value** action on a user) after the first **After updating an existing user account** event is triggered on an account save.
Comment: BTW, that downvote is not from me.  I wish people would comment when they downvote.
Comment: @PatrickKenny - Ok
Comment: sorry to interrupt these comments from @PatrickKenny ... I also don't like anonymous downvotes, which is why I've searched (and found!) for a way to find out who downvoted my answers. It's based on what I think an SE-bug (or design mistake?), which to me has become a handy feature ...
Comment: @Pierre.Vriens So how are you finding user who has downvoted?
Answer: This is not an ideal solution, but I just manually coded the necessary Set a data value behaviors in PHP in a custom module; this prevents the other rules from being triggered twice.
Comment: Did you try the solution which I gave you ? That works fine as I tried with different kind of event "Checkout complete" where I was setting value & it worked as I have gave example..
Comment: No, your solution will not work because this problem is specific to the **After updating an existing user account** event.
Comment: It will work. You just have to add one more hidden field in profile & set a data value when your rule work. And check the same value in condition when the rules get executed...
Comment: I also need some way to unset the value then, because the rules need to be called every time the account is saved.
Comment: Why do you need to unset? You set value in that rule only which you want to be executed once & check the value in same. Other rule which has to be executed again & again won't set any value neither it will check..
Comment: Sorry, I think you have completely misunderstood the question.  I do not have a few rules that need to only be executed once.  I have many rules that need to be executed multiple times.  For example, I need an e-mail to be sent every time the user profile is updated.  However, several of these rules are getting called twice because the **After updating an existing account** event is being erroneously executed twice.  I need this to only be executed once.  I cannot use the workaround because the rules that are called by the event are not one-time rules; they need to be called every user save.
Comment: If that is the case then I have misunderstood you question. You could have guessed it long ago..Sorry :(
Comment: @RajeevK That's ok, I think this is a really tricky problem.  Thanks for making an effort.
Answer: You shouldn't worry about the fact that the Rule is triggered twice. If you activate debugging at Workflow > Rules > Settings, a grey box ("Rules evaluation log") appears when a Rule is run. In your case, you'll see that the same Rule is triggered two times. However, the second time the following message appears:

Not evaluating reaction rule YOUR RULE to prevent recursion.

This proves that Rules is quite a smart module. :)
Comment: If it shows the rule is not evaluated, then it's not triggered twice.  I am aware of this feature, and it's great, but in my case the rule was actually getting evaluated twice (circumventing the feature), which is a big problem.


How Does Case 5 executed in this Switch statement?

Question: I have a following code(which is taken from a C book):
#include<stdio.h>

int main( )
{
  int k=4,j=0;

  switch(k)
  {
  case 3:
    j=300;

  case 4:
    j=400;

  case 5:
    j=500;
  }

  printf("%d",j);
}

When i run the above code, I get output as 500, but I expected it to be 400, can anyone why it is been printed 500 rather than 400?
(I am novice in C and I couldn't figure out what is the error in it!)
Comment: Please indent your code!
Answer: You need to break; at the end of a case block.
#include <stdio.h>
int main()
{
    int k = 4, j = 0;
    switch(k)
    {
        case 3:
            j = 300;
            break;
        case 4:
            j = 400;
            break;
        case 5:
            j=500;
            break;
    }
    printf("%d\n", j);
}

Answer: You need to break out of your cases otherwise it will run trough other cases:
int main( )
{
    int k=4,j=0;
    switch(k)
    {
    case 3:
        j=300;
        break;
    case 4:
        j=400;
        break;
    case 5:
        j=500;
        break;
    }
    printf("%d",j);
}

So in your case it did execute j=400 and then went to case 5: and execute j=500
Answer: There's no break statement after case 4, so execution "falls through" to case 5.


Determine if $\sum_{k=1}^\infty\frac{(-1)^k}{2k-1} \cos(2k-1)$ is a convergent series

Question: This series is from another question:
$$\sum_{k=1}^\infty\frac{(-1)^k}{2k-1} \cos(2k-1)$$
There, its value $-\pi/4$ is immediately determined. But how to be sure, a priori, that the series would converge?
I am new as regards this topic. I tried the Leibniz test, but it fails: due to the cosine, the series may not have alternating signs, even for large $n$. Then, how to determine if the series is convergent?
Comment: use $\cos(2k-1) \le 1$
Comment: @BowPark: your question is not so trivial to be answered in such a casual manner. In fact the inequalities written in this accepted answer are meaningless.
Comment: Well compare the series with $\sum 1/k^p$ with $p=1$ and thus series diverges. Remember harmonic series.
Comment: @AdityaDwivedi So, you mean using the [direct comparison test](https://en.wikipedia.org/wiki/Direct_comparison_test) between the series $\frac{(-1)^k}{2k - 1}$ and $\frac{(-1)^k}{2k - 1} \cos (2k - 1)$?
Comment: yes i said that
Comment: @AdityaDwivedi Thank you. If you want, you can create an answer.
Comment: @AdityaDwivedi: that test works for. Series of positive terms.
Comment: actually $ -1 \le \cos(2k-1) \le 1$ should be used
Comment: You have accepted a wrong answer. You should try to learn more and fix it.
Comment: @ParamanandSingh Yes, this test is only available for series of positive terms. But aren't the series of absolute values $\displaystyle \sum_{k=1}^{\infty} \left| \frac{(-1)^k }{2k-1} \right|$ and $\displaystyle \sum_{k=1}^{\infty} \left| \frac{(-1)^k }{2k-1} \cos(2k - 1) \right|$ series of positive terms?
Comment: @BowPark Absolute value series $\sum\left|\frac{(-1)^k}{2k-1}\right|$ diverges.
Comment: @ParamanandSingh I see that the ratio test is $1$ as regards $\sum \left| \frac{(-1)^k}{2k - 1} \right|$. If this is immediate (otherwise it would require another question), how do you obtain that it diverges?
Answer: Let $$S_n=\sum_{k=1}^{n}(-1)^k\cos(2k-1)$$ and $$T_n=\sum_{k=1}^{n}\cos(2k-1)$$ so that $$S_n+T_n=2\sum_{k\text{ even}, k\leq n} \cos(2k-1)$$ and $$T_n-S_n=2\sum_{k\text{ odd}, k\leq n} \cos(2k-1)$$ Using the formula for sums of trigonometric series with arguments in arithmetic progression one can show that the above sums are bounded for all $n$ so that $S_n$ is also bounded.
Now use $a_n=1/(2n-1)$ so that $a_n$ decreases and tends to $0$ and $b_n=(-1)^{n}\cos(2n-1)$ and the sum $S_n=\sum_{k=1}^{n}b_k$ is bounded and by Dirichlet test the series $\sum a_nb_n$ converges.


fiz um quadrado que se move para todos os lados e gostava de definir uns limites ate onde pode andar

Question: Resumidamente gostava de definir limites onde o quadrado possa andar, envio de seguida o meu codigo e agradeço qualquer ajuda.

var topo=50;
var esq = 200;

function cima(){
   topo -= 5;
   document.getElementById("quadrado").style.top = topo + "px";
}
function baixo(){
   topo += 5;
   document.getElementById("quadrado").style.top = topo + "px";
}

function esquerda(){
   esq -= 5;
   document.getElementById("quadrado").style.left = esq + "px";
}

function direita(){
   esq += 5;
   document.getElementById("quadrado").style.left = esq + "px";
}

function rosa(){

    document.body.style.backgroundColor = "pink";
}

function amarelo(){

    document.body.style.backgroundColor = "yellow";
}

function azul(){

    document.body.style.backgroundColor = "blue";
}
#quadrado{
        position: absolute;
        top: 50px;
        left: 200px;
        width: 120px;
        height: 120px;
        background-color: red;
 }
 table{
        width: 200px;
        border: none;
 }

Quadrado móvel

            <button onclick="cima();">Cima</button>

            <button onclick="esquerda();">Esquerda</button>

            <button onclick="direita();">Direita</button>

            <button onclick="baixo();">Baixo</button>

cores de fundo

            <button onclick="rosa();">pink</button>

            <button onclick="amarelo();">yelow</button>

            <button onclick="azul();">blue</button>

Comment: você pode validar o valor e caso esteja "fora" dos limites retornar sem alterar, por exemplo: `function cima(){  if (top < 0) return;
   topo -= 5;`, e pode fazer isso com todas as posições, basta colocar os limites. Editei sua pergunta pra ficar mais fácil de entender o funcionamento
Answer: Como comentei, em cada função antes de mudar o valor da posição basta validar com os valores limites:

var topo=50;
var esq = 200;

function cima(){
   if (topo < 0) return;
   topo -= 5;
   document.getElementById("quadrado").style.top = topo + "px";
}
function baixo(){
   if (topo > 100) return;
   topo += 5;
   document.getElementById("quadrado").style.top = topo + "px";
}

function esquerda(){
   if (esq < 150) return;
   esq -= 5;
   document.getElementById("quadrado").style.left = esq + "px";
}

function direita(){
   if (esq > 250) return;
   esq += 5;
   document.getElementById("quadrado").style.left = esq + "px";
}

function rosa(){
    document.body.style.backgroundColor = "pink";
}

function amarelo(){
    document.body.style.backgroundColor = "yellow";
}

function azul(){
    document.body.style.backgroundColor = "blue";
}
#quadrado{
        position: absolute;
        top: 50px;
        left: 200px;
        width: 120px;
        height: 120px;
        background-color: red;
 }
 table{
        width: 200px;
        border: none;
 }

Quadrado móvel

            <button onclick="cima();">Cima</button>

            <button onclick="esquerda();">Esquerda</button>

            <button onclick="direita();">Direita</button>

            <button onclick="baixo();">Baixo</button>

cores de fundo

            <button onclick="rosa();">pink</button>

            <button onclick="amarelo();">yelow</button>

            <button onclick="azul();">blue</button>


Variáveis declaradas dentro de blocos ou subprogramas são alocadas dinamicamente?

Question: Estou lendo o livro "Algoritmos e Programação com Exemplos em Pascal e C" e nele há dois parágrafos que me deixou na dúvida, são eles:

Duas formas alternativas são oferecidas por algumas linguagens de programação para gerenciar a alocação dinâmica: (1) declaração de variáveis dentro de blocos (Capítulo 4) ou em subprogramas (Capítulo 9); e (2) alocação de espaço por meio de ponteiros.
Na alocação dinâmica envolvendo variáveis declaradas dentro de blocos ou subprogramas, as variáveis armazenam valores da aplicação da mesma forma que as variáveis estáticas, mas elas são alocadas pelo sistema ao iniciar a execução do bloco ou subprograma e só persistem enquanto durar a execução do mesmo. As áreas de memória associadas a variáveis declaradas nessa modalidade são liberadas automaticamente pelo sistema.

Conforme a autora, a declaração de variáveis dentro de blocos ou em
subprogramas envolve alocação dinâmica. Esta correta esta afirmação? No meu entendimento, apenas a alocação por meio de ponteiros é considerada alocação dinâmica.
Comment: [Esta pergunta](https://pt.stackoverflow.com/questions/3797/o-que-s%C3%A3o-e-onde-est%C3%A3o-o-stack-e-heap) do @Maneiro responde a todas as suas questões. Não sei até que ponto não será duplicada mesmo
Comment: @Isac, o post que você se refere não responde a "todas" questões que fiz aqui. O meu questionamento está mais relacionado aos termos "alocação dinâmica" ou "alocação automática" do que propriamente o conceito de alocação na heap, stack, seguimento de código, seguimento de dados etc.
Comment: Se é esse o caso, parece-me que não deixou isso muito claro na pergunta.
Comment: Alguma das respostas resolveu sua dúvida? Acha que pode aceitar uma delas? Veja o [tour] como fazer isso, se ainda não o fez. Você ajudaria a comunidade identificando qual foi a melhor solução para você. Pode aceitar apenas uma delas. Mas pode votar em qualquer pergunta ou resposta que achar útil no site todo.
Answer: O termo alocação dinâmica não costuma ser usado nesse contexto. De certa forma até é verdade. Esse tipo de alocação ocorre na pilha, então ocorre sempre em tempo de execução, nesse aspecto podemos dizer que a alocação é dinâmica, em oposição a ser estática quando o dado já está no código binário e a alocação foi feita em tempo de compilação. Porém se pensar bem até esse dado não deixa de ser dinâmico porque a alocação efetiva só ocorre na carga do executável.
O termo caiu um pouco em desuso, mas o correto para a alocação na pilha é alocação automática, assim diferencia da estática. Em certo sentido a memória ali é estática já que toda a pilha costuma ter tamanho fixo e alocado antecipadamente. Então há uma alocação prévia geral na carga do executável e depois uma alocação interna durante a execução que cada stack frame.
Por tudo isso considera-se que essas alocações são sempre estáticas ou automáticas, e a alocação dinâmica é aquela que você pede para alocar no heap e ela é acessada por valor ou através de um referência em algum lugar.
A alocação dinâmica precisa de algum gerenciamento enquanto que a alocação estática é gerenciada automaticamente. Esse gerenciamento pode ser automático através de um coletor de lixo (garbage collector) ou manual.
O termo "variáveis estáticas" é completamente errado. A alocação pode ter essa característica, uma variável não.
A alocação dinâmica não precisa necessariamente ser através de ponteiros, mas quase sempre acaba sendo acessado assim. Mas de uma certa forma todo acesso é feito assim, é que alguns casos o endereço do ponteiro já está no código e provavelmente o processador torna o acesso sem custo ou até transparente. Tudo é feito com indireção.
Na memória automática nunca é liberado nada, apenas deixa-se de usar aquele espaço.
Portanto dentro do que se usa na nossa área a autora está errada, bem como as respostas postadas aqui (que inclusive erram em pontos que nem são o foco da pergunta). A autora fala de escopo e tempo de vida.
Comment: obrigado pelos esclarecimentos. De forma sucinta, acredito que o termo correto, neste caso, seria "alocação automática" visto que a alocação ocorre na stack e os recursos são liberados automaticamente ao fim da função ou bloco.
Comment: @RodrigoCosta A resposta resolveu sua dúvida? Acha que pode aceitá-la? Veja o [tour] se não souber como faz. Isso ajudaria muito indicar que a solução foi útil para você. Também pode votar em qualquer pergunta ou resposta que achar útil no site todo
Answer: Está correto, Alocação Dinâmica no contexto que é citado pela autora refere-se ao fato de que uma variável existente em um bloco de código, seja ele uma função, for, while, if, etc. Existe somente dentro daquele escopo de código. Ou seja, ela é criada quando aquele escopo é executado, e (possivelmente) liberada da memória quando sua execução termina.
Análisando um exemplo básico:
function foo() {
   var a = 4; // existe no escopo de foo() e bar().

   function bar() {
      var b = 5; //existe no escopo de bar(), não existe no escopo de foo();
   }
 // var b não existe aqui..
}
// var a e b não existe aqui...


How to stop your source IP address being seen by a site while using proxies?

Question: I'm trying to scrape some data off a site using proxies, I won't put any code here since it is a bit long and maybe not important for the question. I can just list these facts:

The code I have does work at times, meaning it should be good
After a couple dozen requests, the script gets blocked (even with rotating proxies!)
Now the interesting part: after getting blocked with one proxy provider, if I immediately switch to the next one, that one is blocked too!
I have tried with requests.session() and httpx.Client(), and both are failing in the same way.

So I think the only conclusion here is that my IP is somehow getting leaked by these proxies and shown to the site, triggering a block from it. Proxies in question are Brightdata Datacenter (cheap ones) and Residential proxies from proxy-cheap.com which should be a bit better.
 Is there a way to prevent my IP from being sent over with proxies? I don't really think there is a different option, how else could they block rotating proxies from different providers, after letting them work initially for a while.
 And by the way, I'm sure it's not some mistake by me, session is created like this session = httpx.Client(proxies={"http://":proxy_url, "https://":proxy_url}) So that every request uses the same proxy.
Comment: `httpx.Client` seems to work in a [generally equivalent](https://www.python-httpx.org/compatibility/#client-instances) way as `requests.Session`. According to [`requests` advanced usage guide](https://requests.readthedocs.io/en/latest/user/advanced/#session-objects) a `Session` object *persists cookies across all requests made from the Session instance*. AFAIU a server could identify a client by their cookies I think. If so, then you could try disabling support of cookies in the `Session`, or creating a new `Session` instance before you change proxy (in case server requires cookies)...
Comment: But I am new to web programming so don't take my words for granted.
Comment: Yes, they are very similar, that's why I tried with both since the code was just about the same. I haven't tried with urllib yet since that would take more modification.
Comment: I actually want to keep cookies, since the script is supposed to open a link, solve a captcha and then get data. I have a scraping thread, so each thread works with a separate session. And like I said, it works for a short while but then it just gets blocked. Maybe I should try better proxies like the answer below suggests, maybe I would have more luck.
Answer: I believe the easy solution would be buying an elite proxy, since some proxy will tell the server that request is made by a proxy.
Comment: Best I can try is probably Brightdata Residential, will see how that goes.
Comment: I tried Brightdata ISP proxies and it's the same issue with them too.
Comment: Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer).


Flexbox Horizontal List

Question: Boa tarde pessoal!
Estou querendo alinhar uma lista ul de forma que seus li fiquem a esquerda e o último li fique a direita. Não estou conseguindo fazer isso, alguém poderia me ajudar.
Eu poderia fazer isso de forma mais fácil se eu separasse essa li dessa ul mas eu queria manter junto.
Tentei pôr  o justify-self diretamente no úlimo  também mas não deu
Eu tentei conforme segue abaixo e não deu certo:
HTML

Funcionários

Clientes

Produtos

Serviços

Financeiro

CSS
.lista-menu {
display: flex;
justify-content: flex-start;
width: 100%;
border: 1px solid black;
}

.financeiro {
  justify-self: flex-end;
}

Answer: Seu HTML tem um erro de semântica que vc precisa corrigir, o filho direto da
 tem que ser a
 e não o
Depois, no CSS, usando Flexbox, para vc alinha no eixo X um filho de um conteiner flex, se usa margin e não justyfy
Repare abaixo, que ao colocar margin-left: auto no último filho ele vai colar na direita do container pai

.lista-menu {
  display: flex;
  justify-content: flex-start;
  width: 100%;
  border: 1px solid black;

  padding: 0;
}

.financeiro {
  margin-left: auto;
}

Funcionários

Clientes

Produtos

Serviços

Financeiro

Flexbox Horizontal List


Similar Matrices and Change of Basis

Question: I'm trying to understand a little better change of basis matrices and how they relate to determining if two matrices are similar.
Given finite vector spaces $V,W$ such that $\textrm{dim} V=\text{dim} W$ and a linear transformation $T:W\rightarrow V$ and ordered bases $V_B$ and $W_B$.
Now my book only covers the case where $W=V$ and defines similar matrices such that an invertible change of basis matrix $M$ exists such that:
$$[M]_{W_B}^{V_B}[T]_{W_B}[M]^{W_B}_{V_B}=[T]_{V_B}$$
Now how does this work when $W=V$?  Are the columns of the change of basis matrix still just the basis vectors of $W$ according to their coordinates in $V$?  Or does some type of mapping of $W_B$ to $V_B$ have to be done first?  I'm asking because I kind of was looking at a change of basis matrix as a special case where the transformation is the identity transformation.
I hope this question makes sense.
Comment: For distinct vector spaces there is no such thing as a change-of-basis matrix. Recall that (depending on how it is taught) either (1) the columns of the change-of-basis matrix express the vectors of one basis in coordinates on the other basis, or (2) a change-of-basis matrix describes the identity transformation using one of the bases at departure and the other at arrival. Neither description makes sense if you have a pair of basis of different vector spaces (in (2) because there is no identity transformation between them).
Answer: When $W = V$ you generally choose the same basis for $W$ as for $V$ when you change bases. (The point of doing this is, for example, so that you can sensibly use the corresponding matrix representation to take powers or exponentials of the corresponding linear transformation and to find eigenvalues and eigenvectors, etc.) When $W \neq V$ you are free (if you want) to change bases both in $V$ and in $W$, but I don't think people generally call the corresponding equivalence relation similarity. Similarity is very much a $W = V$ kind of phenomenon.
(Exercise: show that up to a change of basis in $V$ and in $W$, any linear transformation is uniquely determined by the dimension of its range.)
Comment: I didn't quite understand the exercise.  What do you mean by "up to a change of basis"?
Comment: @Robert: I mean that there is an equivalence relation on linear transformations $V \to W$ where two transformations are equivalent if there is a matrix that represents both of them using appropriate choices of bases in $V$ and $W$. The equivalence classes of this equivalence relation are precisely the linear transformations having range a fixed dimension.

Similar Matrices and Change of Basis


complex mapping

Question: Let $U$ be an open set of $\mathbb{C}$ containing $D=\{z\in \mathbb{C}: |z|<1\}$ and let $f:U\to \mathbb{C}$ be map defined by
$f(z)= e^{iψ} \frac{z-a}{1-\overline{a}z}$ for $a\in D$ and $ψ\in[0,2\pi]$.
Which of the following statements are true?

$|f(e^{iθ})| =1$ for $0<θ<2\pi$ .
$f$ maps  $\{z\in\mathbb{C}: |z|<1\}$ onto itself.
$f$ maps  $\{z\in \mathbb{C}: |z|\le 1\}$ into itself.
$f$  is one-one.

I am stuck on this problem . Can anyone help me please.................
Comment: i don't know where to begin..........
Answer: If you consider $|a|<1$, the functions of the form $f$ restricted to $D$ describe all of the automorphisms of the unit disk.
If you think about what this means, the boundary of the unit disk (the unit circle) is exactly where $|z|=1$. This should answer problem 1 when you think about the norm of $e^{i \theta}$. In other words, if $|z|=1$, then $|f|=1$.
For 2 and 3, recall that since $f$ restricted to $D$ is an automorphism, it takes domain onto the domain. just think what's in $D$.
For the last part, remember that $f$ takes $D$ onto $D$ and has an inverse since it is a conformal mapping. You can even explicitly state inverse and its domain.
Comment: @pankaj: It looks like jshin is answering using language and terminology you are unfamiliar with. Please take a look at [our FAQ on homework questions](http://meta.math.stackexchange.com/questions/1803/how-to-ask-a-homework-question) to see how you can improve your questions in a way that makes it easier for others to help you.
Comment: still not getting sir...........
Comment: Sir,give me some more hints for this question..
Comment: automorphisms of the unit disk??????means............

complex mapping


Como utilizar o "find" do BeautifulSoup para encontrar uma tag script com tipo específico?

Question: Há um tempo venho estudando como usar BeautifulSoup para conseguir encontrar conteúdo de tags etc.
Mas me deparei com um problema no qual o conteúdo que quero encontrar fica dentro de uma tag


Caption on CKEditor with Media embed only allows 128 characters

Question: Using Drupal 8, the Media module, and the CKEditor that comes with D8. When you add an image media item, you get your screen like this...

But that caption field only allows 128 characters. Media (I believe) seems to be setting a maxlength of 128 (maxlength="128"). You can still add more characters if you Embed, then edit the source it supplies, but it's still going to chop off what you've added if you double click on that image (it's technically a drupal-entity element) to bring the "Embed media" window up again.
Since this "Caption" field is not dealing with any kind of data/database storage constraints, 1.) can I change this and 2.) does this even make any sense for it to have a maxlength set?
If anyone has a clue how to change it, that'd be most helpful.
Comment: What's the caption used for? Is it rendered in its own tag or does it just go in the alt attribute for the image? If the latter, general advice is to keep alt tags at a max of 125 chars so that might make sense (not sure why it would be rounded up to a nice factor of 2 though, so it might not). If the former, there's probably no good technical reason to limit it; might even be a default. If you've already checked that the constraint isn't also in the DB, then a simple hook_form_alter would be the way to go
Comment: As the text is immediately just being dumped into the editor after hitting Embed, I'm as certain as I know how to be that it's not a data constraint issue. I AM considering the hook_form_alter route though, and am hoping for some progress with that tomorrow when I get a chance. Thanks!
Answer: This should do the trick no need to install any module, substituting 500 with whatever you want the limit to be
<code> function mymodule_form_entity_embed_dialog_alter(&$form, $form_state, $form_id) {
      if(isset($form['attributes']['data-caption'])) {
        $form['attributes']['data-caption']['#maxlength'] = '500';
      }
    }
</code>
Answer: I ended up installing the maxlength module, and altering the form as such:
<code>function mymodule_form_entity_embed_dialog_alter(&$form, $form_state, $form_id) {
  if(isset($form['attributes']['data-caption'])) {
    $form['attributes']['data-caption']['#maxlength'] = '524288';
    $form['attributes']['data-caption']['#maxlength_js'] = TRUE;
  }
}
</code>
Works great! It does create a dependency with the maxlength module, and puts the "limit" on the field (I went with what it said was the "default" of 524288), but other than that
Comment: What does the maxlength module contribute to the solution? I was able to use the same hook as you and only needed to set the #maxlength attribute to increase the limit past the default.
Comment: Sorry, it's been a while. For D7, doesn't the module give all of the associated JavaScript for counting down the number of characters left? And also enforce the validation of it on form submission?


TS error: Argument of type 'null' is not assignable to parameter of type 'Error | PromiseLike | undefined'.ts(2345)

Question: I am using Mobx Persist Store that saves MobX Store to Local Storage.
The docs doesn't have a TS version so I modified 2 lines (one in readStore & another in writeStore which you can compare with https://github.com/quarrant/mobx-persist-store#with-mobx-6) to solve TS errors but that caused another error:
import {
    persistence,
    useClear,
    useDisposers,
    isSynchronized,
    StorageAdapter,
} from 'mobx-persist-store'

import { FrameItStore } from '@/store/index'

function readStore(name: string) {
    return new Promise<string>((resolve) => {
        const data = localStorage.getItem(name) || '{}'
        resolve(JSON.parse(data))
    })
}

function writeStore(name: string, content: string) {
    return new Promise<Error | undefined>((resolve) => {
        localStorage.setItem(name, JSON.stringify(content))
        resolve(null)
    })
}

export default persistence({
    name: 'FrameItStore',
    properties: ['counter'],
    adapter: new StorageAdapter({
        read: readStore,
        write: writeStore,
    }),
    reactionOptions: {
        // optional
        delay: 2000,
    },
})(new FrameItStore())

I am getting an error on the null in resolve(null) inside of writeStore.
Here's the error:

Argument of type 'null' is not assignable to parameter of type 'Error | PromiseLike<Error | undefined> | undefined'.ts(2345)

How can I solve it?
Answer: unknown is not compatible with string or undefined. I believe that JSON.parse is returning unknown. So rather than just resolving JSON.parse(data) you have to determine its type. You can do this by using conditional checks. Once you are sure it's a string you can resolve it, otherwise resolve undefined.
function readStore(name: string) {
    return new Promise((resolve) => {
        const data = localStorage.getItem(name) || '{}'
        const output = JSON.parse(data);
        if (typeof output === "string") resolve(output);
        resolve(undefined);
    })
}

Answer: I think I solved it as I see no errors now. I had to remove JSON.parse from readStore as well as use resolve(undefined) instead of resolve(null) in writeStore like:
import {
    persistence,
    useClear,
    useDisposers,
    isSynchronized,
    StorageAdapter,
} from 'mobx-persist-store'

import { FrameItStore } from '@/store/index'

function readStore(name: string) {
    return new Promise<string>((resolve) => {
        const data = localStorage.getItem(name) || '{}'
        resolve(data)
    })
}

function writeStore(name: string, content: string) {
    return new Promise<Error | undefined>((resolve) => {
        localStorage.setItem(name, JSON.stringify(content))
        resolve(undefined)
    })
}

export const PersistState = persistence({
    name: 'FrameItStore',
    properties: ['counter'],
    adapter: new StorageAdapter({
        read: readStore,
        write: writeStore,
    }),
    reactionOptions: {
        // optional
        delay: 2000,
    },
})(new FrameItStore())


@PostConstruct method runs before flyway

Question: I know this kind of question has been asked before.
I have a method which is annotated with @PostConstruct.
The methods assumes that all Flyway scripts have been executed before invocation.
It seems that Flyway also uses @PostConstruct annotated methods and that these methods are called after my method.
I tried to annotate my method with @DependOn and different flyway beennames.
Unfortunately without success. Can anybody help me.
Comment: There are several ways to intercept in the process of bean creations. You can put `@DependsOn` at class level if you've activated component scanning. You might also consider to user `@Order` annotations or use BeanPostProcessors.
Answer: Solution:
I would set a dependency on the FlywayMigrationInitializer in the constructor. When the Initializer is created and set up, the migrations are run.
Or you can depend on the flywayInitializer bean (@DependsOn("flywayInitializer")). The bean is named flywayInitializer, of the class FlywayMigrationInitializer and it is created in FlywayAutoConfiguration.java.
FlywayMigrationInitializer implements InitializingBean and calls the migrate method in the afterPropertiesSet method.
Example:
@Component
// @DependsOn("flywayInitializer")
@Slf4j
public class TestPostConstruct {

    public TestPostConstruct(FlywayMigrationInitializer flywayForceInitialization) {
    }

    @PostConstruct
    public void testPostConstruct() {
      log.info("---->  in testPostConstruct");
    }
}

The Spring Boot log:
INFO 4760 --- [main] o.f.core.internal.command.DbMigrate      : Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.130s)
INFO 4760 --- [main] c.example.flywayinit.TestPostConstruct   : ---->  in testPostConstruct

Comment: Thanks. This works fine.
But why doesn't @DependsOn("flywayMigrationInitializer" ) @PostConstruct work?
Answer: I've tried to use @PostConstruct with Flyway migrations - it did not worked, so  @EventListener(ApplicationStartedEvent::class) worked.
It's not the same annotations but are similar and maybe can help someone.
Answer: For new Flyway this work (use Flyway callbacks)
    @Configuration
    class FlywayConfig(env: Environment) {
    private val env: Environment

    init {
        this.env = env
    }

    @Bean(initMethod = "migrate")
    fun flyway(dbLoadService: DbLoadService): Flyway {
        return Flyway(
            Flyway.configure()
                .baselineOnMigrate(true)
                .dataSource(
                    env.getRequiredProperty("spring.datasource.url"),
                    env.getRequiredProperty("spring.datasource.username"),
                    env.getRequiredProperty("spring.datasource.password")
                )
                //запуск загрузки из базы после окончания миграции
                .callbacks(FlywayMigrationsCompleteCallback {
                    dbLoadService.loadAllCertificateInformation()
                })
        )
    }

    class FlywayMigrationsCompleteCallback(private val callback: () -> Unit) : Callback {

    override fun supports(event: Event?, context: Context?): Boolean {
        return event == Event.AFTER_MIGRATE
    }

    override fun canHandleInTransaction(event: Event?, context: Context?): Boolean {
        return true
    }

    override fun handle(event: Event?, context: Context?) {
        callback()
    }

    override fun getCallbackName(): String {
        return FlywayMigrationsCompleteCallback::class.simpleName!!
     }
  }

  @Component
  class DbLoadService(private val certificateRepository:CertificateRepository) {
    @Volatile var  certificate: List<Certificate>?=null
    fun loadAllCertificateInformation(){
        val findAll = certificateRepository.findAll()
        runBlocking {
            certificate = findAll.toList()
        }
    }
}


How to get rid of 'security updates available' warnings?

Question: I am using the ShareThis module on my website. Unfortunately, recent release 2.3 is flagged as a security update, but it is has a major bug (settings can't be saved). I have reverted to release 2.2.
The consequence is that 'security updates available' warnings are displayed all the time in administration screens. Is it possible to get rid of them? I tried to find an option, but could not find any.
Answer: I had a similar problem recently (see How do I avoid checking for updates for a specific module?) and managed to get around it using hook_update_projects_alter(). You can use it to easily remove any module that you don't want to be part of the update process:
function MYMODULE_update_projects_alter(&$projects) {
  $blacklist = array(
    'sharethis',
    // Any others you might want
  );

  foreach ($blacklist as $module) {
    unset($projects[$module]);
  }
}

As Drupal no longer considers any module you put in the $blacklist array as one that needs to be updated, you should stop getting the security warning.
It would probably be a good idea to keep an eye on the progress of the bug in the ShareThis module so that you can remove this restriction when possible.
Comment: I believe your linked question had a pretty awesome answer too ;-)  +1 for the hook!
Comment: @greggles Yes that's true, I should have mentioned that; fixing/patching the vulnerability is obviously the most desirable solution.
Comment: As I say in my answer, this hides the problem. The update system is valuable - if there's a bug in the module we should spend time writing code to fix the bug rather than hiding an important update.
Answer: I'd say the right way to get rid of it is to fix the bug (e.g. by reviewing the patch in that issue) and deploying that patch on your server.
The vulnerability in the module is pretty serious. It would allow an attacker to take over your site. You don't want to be running an outdated version of the module if at all possible.
Comment: A new version with the fix is released. Please upgrade ASAP http://drupal.org/project/sharethis
Comment: I wonder if it'd be suitable instead to add an `if ($user->uid != 1){}`, to the `hook_update_projects_alter`...or to only show the warnings to certain user roles?  I understand you know a bit about Drupal security Greggles ;-)
Comment: I'm not going to be happy with any solution that involves staying on the older version of the module. CSRF that lets you inject XSS is a great way to have your site taken over. The patch to fix the module was just committed and a new release is on its way "soon". People can get the code from git or the dev release when it's rebuilt later today and be safe AND functional.


Bayes Rule. Given I flipped 10 heads in a row, what is likelihood I own a double headed coin?

Question: $$\Pr(A | B) = \frac{\Pr(B | A) \Pr(A)}{\Pr(B)}$$
So my $\Pr(A)$ is probability that a coin is double headed. For the sake of argument let's say 1 in 10,000 coins are double headed.
My $\Pr(B)$ is the probability of flipping 10 heads, which is 1 in $2^{10}$.
So naively I thought $\Pr(B|A)$ well the probability that I flip 10 heads given that I have a double headed coin is 1?
That leaves the probability that I have a double headed coin at $\frac{10^{-4}}{2^{-10}}$.
However this means that the probability will be > 1 for larger flips.
The step in which I have made a mistake is evaluating $\Pr(B|A)$. Can anyone help?
Comment: Likelihood is not the same as probability. The probability of getting $k$ Heads in $10$ independent tosses of a coin that turns up Heads with probability $p$ is $\binom{10}{k}p^k(1-p)^{10-k}$. The _likelihood function_ of $p$ is $\binom{10}{k}p^k(1-p)^{10-k}$ where $p$ is the variable and $k$ is the number of Heads that have been observed; that is, the data.  In your case, the likelihood function of $p$ is $p^{10}, 0\leq p \leq 1$, which has a maximum value at $p=1$. To estimate $p$ via Bayes' theorem, you need to describe what you know about $p$. Are the other $9,999$ coins fair?
Comment: Your $P(B)$ is incorrect as it stands. $P(B)$ can be expanded to $P(B|A)\cdot P(A) + P(B|\overline{A})\cdot P(\overline{A})$.
Comment: Yes they are fair, that is there is a 1 in 2 chance you will get a head
Comment: So my P(B) should be 1*10^(-4) + (2^(-10) * (0.9999))
Answer: Thanks to @COOLSerdash for spotting my mistake
It was in my evaulation of $P(B)$ that I made the mistake.
I did not account for that fact the $P(B)$ the probability I flip 10 heads in a row is dependent on the fact that 1 in 10,000 coins are double headed.
When properly evaluating $P(B) = P(B|A)⋅P(A)+P(B|\overline{A})⋅P(\overline{A})$ I end up with a roughly 9% probability the coin is double headed if I get 10 heads in a row.
Plotting the number of heads on the $x$-axis, and the probability on the $y$-axis, looks quite beautiful:


Is it meaningful to calculate predicted marginal effects of a count data model with an interaction effect?

Question: In a little regression model of mine, I estimate the following formula a a negative binomial regression type (it would hold for a Poisson regression as well):
$$
y = \beta * var1 + \gamma * var1 * binary + \delta * X + \epsilon
$$
where $X$ is a matrix of control variables that are all on a discrete scale. $var1$ however is not discrete while $binary$ of course is a binary variable. The second term, $ \gamma * var1 * binary$ is dedicated to check how $var$ acts different if $binary=1$.
Now I compute marginal effects, say with the mfx package for R. Since count data models are GLM, the marginal effects are evaluated at some value of all the other variables (typically the mean). But I wonder whether it is meaningful to evaluate $\partial y/\partial var1$ while holding $var1 * binary$ at the mean. Or did I misunderstand the concept of average marginal effects for GLMs?
Answer: In a Poisson model,
$$E[y \vert x]=\exp(\alpha + \beta x + \gamma x \cdot b).$$
The derivative would be
$$\frac{\partial E[y \vert x]}{\partial x}=\frac{\partial \exp(\alpha + \beta x + \gamma x \cdot b )}{\partial x}=\exp(\alpha + \beta x + \gamma x \cdot b)\cdot(\beta+\gamma b).$$
This is a function of $x$ and $b$, and folks create many types of marginal effects. One option is to use the means, but that might be weird for binary variables since you might evaluate the derivative for someone who is .75 female. It might be strange for continuous variables if $x$ has a weird distribution. Another option is to set all the dummies to their modes or to zero, the base.
Another option is to use own values of the covariates for each observation and then average the derivatives. Personally, I like this one the most.
mfx allows you to easily do the first with atmean=TRUE and the last with atmean=FALSE. However, I am not sure if it will handle interactions on the fly, so it may omit the $\gamma b$ term.
Comment: @MERose Thanks for spotting that. If you pick a public dataset (or post your data) and show your R code, it would be pretty easy to figure out by comparing it with Stata. Might be better to turn it into another question.
Comment: The R package `mfx` does not handle the case of interactions correctly (as most other implementations of marginal effects). A simple (but meaningless) example would be to add a factor to the `cars` data `cars$fac <- factor(rep(1:2, each = 25))`, fit a Poisson model `m <- glm(dist ~ speed * fac, data = cars, family = poisson)`, compare the naive marginal effects `exp(sum(coef(m) * colMeans(model.matrix(m)))) * coef(m)` with `poissonmfx(m, data = cars)`. This reports three (rather than two) marginal effects and uses the naive computations for the "two" speed-related variables.
Comment: I might ask the maintainer to know what `negbinmfx` does when it evaluates an interaction effect.
Comment: Two further comments: (1) A useful reference for this kind of problem in GLMs is Ai & Norton (2003, _Economics Letters_, 80, 123-129) who explain the issues in detail for logit and probit models. (2) If you are willing to look at _effects_ rather than _marginal effects_, then have a look at the R package `effects`. This correctly resolves interactions, e.g., `plot(allEffects(m))`.


Python - Comparar Listas, utilizando condições para determinar Inicio e Final da Pesquisa

Question: Introdução: Programa tem 1 Listas e 1 Conjunto:
Lista = Entrada de Dados; Conjunto: Dados de Comparação
lista_entrada = ['BBB','AAA','CCC','DDD','EEE']
    print()
    for item in lista_entrada:
        print("item")

lista_bd = {'AAA','BBB','CCC','DDD','EEE','FFF','GGG','EEE'}

Problema:
Como comparar a Lista com o Conjunto;
Imprimir os valores iguais, a partir do item: "AAA", não imprima valores anteriores ao item "AAA" contidos na lista. Imprima até o item "EEE"
Tentei algumas coisas, mas não sei lidar muito bem com laços de repetição e funções de busca em listas ou conjuntos :/ Aí sempre acabo caindo nesse problema
Answer: lista = ['BBB','AAA','CCC','DDD','EEE']
conjunto = {'AAA','BBB','CCC','DDD','EEE','FFF','GGG','EEE'}

aaa_encontrado = False
for item in lista:
    if item == "AAA":
        aaa_encontrado = True
    if aaa_encontrado and item in conjunto: #Imprimir os valores iguais,
        print(item)                         # a partir do item: "AAA"
    if item == "EEE":   # Imprima até o item "EEE"
        break # sai do for

Comment: Obrigado!
Eu realmente estava complicando tudo, vou me focar em resolver as coisas de modo sutil e eficaz
Obrigado pela paciência


Computing $\int_{0}^{1} \frac{x^{t}-1}{\ln x} d x$ without Feynman, where $Re(t)>-1$?

Question: Differentiating the integral w.r.t. $a$ yields
$$
\begin{aligned}
\frac{\partial}{\partial a} \int_{0}^{1} \frac{x^{a}-1}{\ln x} d x =\int_{0}^{1} \frac{x^{a} \ln x}{\ln x} d x
=\left[\frac{x^{a+1}}{a+1}\right]_{0}^{1}
&=\frac{1}{a+1}
\end{aligned}
$$
Integrating both sides from $a=0$ to $a=t$ yields the result
$$
\boxed{\int_{0}^{1} \frac{x^{t}-1}{\ln x}dx=\ln (t+1)}
$$
where $Re(t)>-1.$
Question: Can it be evaluated without Feynman?
Comment: Note${\int_{0}^{1} \frac{x^{t}-1}{\ln x}dx=\ln |t+1|}$ is false for $t\le-1$.
Comment: $
\int_{0}^{1} \frac{x^{t}-1}{\ln^n x}dx
$ is divergent
Comment: yes, thank you.
Comment: ... for $n\geqslant2$.
Comment: Thank you for your reminder.  The question 2 was deleted.
Comment: After $u=-\ln x$ it's a [Frullani integral](https://en.wikipedia.org/wiki/Frullani_integral) $\int_0^\infty\frac{f(au)-f(bu)}{u}du$ with $a:=t+1,\,b:=1,\,f(z):=1-e^{-z}$.
Comment: And if $t>-1$ you may omit the absolute value.  Probably you get a nice result if you allow $t$ complex and require $\operatorname{Re}t>-1$.  Of course then you **must** omit the absolute value.
Comment: Thank you for your suggestion!
Answer: Sure, we can use the dual to Feynman - turning the integral into a double integral:
$$\int_0^1\frac{x^t-1}{\log x}\:dx = \int_0^1\int_0^tx^y\:dy\:dx = \int_0^t \frac{1}{y+1}\:dy = \log(t+1)$$
by Fubini's theorem.
Answer: Using special functions
$$I=\int\frac{x^{t}-1}{\log( x)}\,d x=\int\frac{e^{(t+1) y}}{y}\,dy-\int\frac{e^y}{y}\,dy=\text{Ei}((t+1) y)-\text{Ei}(y)=\text{Ei}((t+1) \log (x))-\text{li}(x)$$
$$\lim_{x\to 0} \, \Big[\text{Ei}((t+1) \log (x))-\text{li}(x)\Big] =0$$
$$J_k=\int_0^{e^k}\frac{x^{t}-1}{\log( x)}\,d x=\text{Ei}(k(t+1))-\text{Ei}(k)$$ The only simple case is for $k=0$
$$J_0=\frac{1}{2} \left(\log (t+1)-\log \left(\frac{1}{t+1}\right)\right)=\log(t+1)$$
Answer: This is an answer from Hints on calculating the integral $\int_0^1\frac{x^{19}-1}{\ln x}\,dx$ of mine for $t=m$ an integer. Note
$$ \lim_{n\to\infty} n(x^{\frac{1}{n}}-1)=\ln x, \text{ for }x>0 $$
and hence, for $m\in\mathbb{N}$,
\begin{eqnarray}
\int_0^1\frac{x^m-1}{\ln x}dx&=&\int_0^1\sum_{i=0}^{m-1}\frac{x^i(x-1)}{\ln x}dx\\
&=&\int_0^1\sum_{i=0}^{m-1}\lim_{n\to\infty}\frac{x^i(x-1)}{n(x^{\frac{1}{n}}-1)}dx\\
&=&\lim_{n\to\infty}\sum_{i=0}^{m-1}\int_0^1\frac{x^i(x-1)}{n(x^{\frac{1}{n}}-1)}dx\\
&=&\lim_{n\to\infty}\frac{1}{n}\sum_{i=0}^{m-1}\int_0^1x^i\sum_{k=0}^{n-1}x^{\frac{k}{n}}dx\\
&=&\lim_{n\to\infty}\frac{1}{n}\sum_{i=0}^{m-1}\int_0^1\sum_{k=0}^{n-1}x^{\frac{k}{n}+i}dx\\
&=&\lim_{n\to\infty}\sum_{i=0}^{m-1}\frac{1}{n}\sum_{k=0}^{n-1}\frac{1}{\frac{k}{n}+i+1}\\
&=&\sum_{i=0}^{m-1}\int_0^1\frac{1}{x+i+1}dx\\
&=&\sum_{i=0}^{m-1}\ln\frac{i+2}{i+1}\\
&=&\ln(m+1).
\end{eqnarray}
Answer: $$
\begin{aligned}
\int_{0}^{1} \frac{x^{t}-1}{\ln x} d x &\stackrel{x \mapsto e^{-x}}{=}  \int_{0}^{\infty} \frac{e^{-x t} -1}{x} e^{-x} d x \\
&=\int_{0}^{\infty} \frac{e^{-x(t+1)}-e^{-x}}{x} d x
\end{aligned}
$$
Using the Frullani's Theorem,
we have
$$
\int_{0}^{\infty} \frac{e^{-x(t+1)}-e^{-x}}{x} d x=(0-1)  \ln \left(\frac{-1}{-(t+1)}\right)=\ln (t+1)
$$


Tail only first few character of a line

Question: I have a text log of computation which contains a line for each iteration.
We track the computation with
tail -f log.txt

But the lines of log file are very long and the tail output is unreadable.
I've tried this
tail -f log.txt | head -c 50

but it shows only beginning of the first line, but not lines which are produced after.
How can I dynamically display only first 50 characters of newly added line to the log file?
Thank you
Answer: Use cut:
tail -f log.txt | cut -b 1-50

Comment: just remember that output will be buffered, so you may have to wait a while before anything is shown
Answer: This is working for me:
tail -f log.txt | awk '{print substr ($0, 0, 50)}'

Tail only first few character of a line


Pointwise convergence of $n\sin(x/n)$

Question: $\lim\limits_{n\to\infty}\{f_n\}=\lim\limits_{n\to\infty} n\sin(x/n) = x$. Does this mean that $\{f_n\}$ converges pointwise for all $x$? But if $x=0$, then is $\lim\limits_{n\to\infty} n\sin(0/n)=0$ valid? Or am I confusing something?
Answer: If $x=0$ then $n\sin(\frac{x}{n})=n\sin(0)=0$.
Answer: Yes. It means. If $x=0$, then you have the sequence $0, 0, 0, ...$ which evidently converges to $0$. Thus $f_n$ converges pointwise for all $x$.

Pointwise convergence of $n\sin(x/n)$


Adding sin(x + a) + sin(x + b)

Question: I'm trying to prove
$$\forall a,b \in \mathbb{R} \exists c,d,e\in \mathbb{R}: f(x):=\sin(x+a) + \sin(x+b) = c \sin(x+d) + e$$
I attempted using $\sin(s) = \frac{e^{is} - e^{-is}}{2i}$ and ended up with an equality similar to $e^{ix} - \alpha e^{-ix}$, where usually $\alpha \neq 1$, which indicates that this way is impossible.
I also looked at the series expansion of sine, but had no idea where to even start.
This is part of a larger problem, and likely not required in the solution, but I am curious how this can be solved.
Comment: To be honest , I don't know why you need the extra $e$.
Answer: Do you get access to calculus to do this?
Your function $f$ is given by $$f(x)=\sin(x+a)+\sin(x+b)$$
And you propose that it is equal to $g$ given by $$g(x)=c\sin(x+d)+e$$
First of all, the midline of $f$ is clearly(?) at $y=0$, so if this will work, $e$ must be $0$.
Now note that both functions satisfy $y''=-y$. So if they have the same value at $x=0$ and derivative at $x=0$, then they are indeed the same function. To make this happen requires
$$\begin{align}
\sin(a)+\sin(b)=c\sin(d)\\
\cos(a)+\cos(b)=c\cos(d)
\end{align}$$
Which is a system of equations from which you could eliminate $c$ to solve for $d$ in terms of $a$ and $b$. And then you could solve for $c$ too.
This results in $$\sin(x+a)+\sin(x+b)=\frac{\sin(a)+\sin(b)}{\sin\left(\arctan\left(\frac{\sin(a)+\sin(b)}{\cos(a)+\cos(b)}\right)\right)}\sin\left(x+\arctan\left(\frac{\sin(a)+\sin(b)}{\cos(a)+\cos(b)}\right)\right)$$
(And the trig(inverse trig) parts could be simplified using $\sin(\arctan(z))=\frac{z}{\sqrt{1+z^2}}$.)
Comment: What is the downvoter's objection?
Comment: I down voted the answer because this is sorcery. Just kidding,  I accidentally clicked the down arrow instead of the up arrow. I don't know why I can't up vote it again. I will though, after 6 minutes or so...
Comment: Hmm, can't seem to up vote. Vote locked in. I'll +1 in the comments.
Comment: I just verified this for myself. Good and easy solution!
Comment: @PandaBear Oh OK. I don't care about the rep, just wanted to know why.
Answer: Another approach to prove this to yourself would be to choose special points like $x = \pi + a$ and $x = -b$, and so on, and solve for $c,d,e$.  After having solved for your constants, you can then go and verify your answer with as many points as you want (or, as said above, by using the addtion formulae).
I will hint that it will be easiest to use your initial equation and its first derivative.
Comment: Solving it this way seems to get quite complicated quickly. Are you using the trigo identities heavily or is there some trick to it?
Comment: @mafutrct: I have to admit that it's been years since I've done this, but I have solved this identity this way when proving circuit results to class.
Comment: I will say that choosing those particular points is potentially not optimal.  The key is to get the sine to take known values.  It might also be prudent to shift your overall equation before you start, so you can eliminate $a$ or $b$, i.e., by defining $z = x+a$, and then defining  $\beta = b -a$, and $\delta = d - a$ so you have fewer constants to worry about.
Comment: Thanks, that sounds like a sensible idea
Answer: Don't use complex exponentials, just expand everything using the addition formula for sine and group terms alike.
Comment: You mean $\sin(x+y) = \sin x \cos y + \cos x \sin y$?
Comment: Yes. That's exactly what I suggested.
Comment: Could you show a few steps? I'm stuck in line 3 already.

Adding sin(x + a) + sin(x + b)


WSO2 Message Broker (2.1.1): WebService API

Question: Is there documentation on the WebServices API? I found the WSDL of the Message Broker, I miss the message description in the WSDL.
https:/localhost:9443/services/EventBrokerService?wsdl
Answer: AFAIK, There is no document for the web-services API in WSO2 MB. You have to manually check the parameters and other soap action via WSDL. Please find the blog for "DISCOVERING ADMIN SERVICES IN A WSO2 (CARBON) SERVER"


iOS : Icon occasionally fails to transfer to device

Question: Does anyone know why this happens?  My project uses a local network, so it doesn't make sense to test it on just one device, so I want to install it on 20.  The way I do this is I build it once and then load it onto each device using the organizer.  However, about every 3 or 4 transfers, the icon fails to transfer with it, and instead I am left with a white icon only on the home screen.  If I launch it, and then check the multitask bar, the icon is there (and it is shown faded out on the home screen while the multitask bar is active).  If I transfer the app again, the icon comes just fine.  The devices are all 32 GB iPad 2.
The dimensions of the icon are correct (72x72 RGB24), and the name of the icon is correct (Icon-72.png).  I noticed that there are two entries in my info plist:  Icon files and Icon files (iOS 5).  My deployment target is 5.0, so should I erase the first one?  This program is big and takes a long time to transfer and install so I dislike loading it twice.  What can I do?
Comment: I don't know WHY this happens, so I won't submit my reply as an answer. However, I have had similar issues but removing the image files completely and then adding them again fixed the problem. It did happen several times before the images finally "took" and stopped disappearing. (Upvoting this question for a better solution/explanation)
Comment: I've seen this happen in the simulator to ALL the apps. Normally, after a few more builds it corrects itself. I've also see it happen on my device with iOS 5.1 installed. I've had it clear up after a restart though. I think this is a bug in system itself and not related to any of our code.
Comment: @ThomasHajcak Yes, lately I am noticing that as well.  Very odd...
Comment: Might be worth pasting this URL into the Apple bug report
Answer: The App icons (retina and normal) need to be placed on the root of your application.
To fix the problem I found that the best is to start the process from scratch:

remove the icons from your project
clean the build
delete Derived Data (Organizer->Projects) *
remove the icons from the app info plist file (Icon files and Icon files iOS5).
drag the icons into the Summary App Icon section
Build

* I do not know if this step is needed but I do it anyway while troubleshooting.
Comment: That is what I did in the first place.  It only fails to transfer occasionally so it is weird.
Answer: In newer versions of Xcode it doesn't seem to be happening. So I will chalk it up to a quirk in Xcode.


How to lock concurrent access to a shared webservice from multiple webservers?

Question: Our production environment consists in n Apache web servers accessed by browsers via a frontal load balancer. Very typical. 99% of our HTTP requests are gracefully handled by this parallelized architecture with no problem.
Unfortunately, 1% of the requests handled by Apache need to access to a tier SOAP webservice, on a foreign server, which is absolutely unable to handle concurrency, resulting in many critical errors.
I'm looking for a way to canalise those SOAP requests through a sort of proxy, able to queue and lock synchronously the requests to the foreign server.
Do standard proxies like Pound, Nginx, Squid or Varnish implement such queues ?
Answer: Amazon has a service that you could use. It's called Simple Queue Service. http://aws.amazon.com/sqs/
You can use this to queue jobs and pick them off the queue one at a time.

When a message is received, it becomes "locked" while being processed.
  This keeps other computers from processing the message simultaneously.
  If the message processing fails, the lock will expire and the message
  will be available again. In the case where the application needs more
  time for processing, the "lock" timeout can be changed dynamically via
  the ChangeMessageVisibility operation.

There are number of alternatives that could suit your use case:

Redis - http://redis.io/topics/transactions
Rabiitmq - http://www.rabbitmq.com/
Elasticmq - https://github.com/adamw/elasticmq
Mongodb - http://www.mattinsler.com/post/26536863600/why-and-how-i-replaced-amazon-sqs-with-mongodb
Comment: Thanks a lot for the clues. Redis seems quite an option since we also make abundant use of shared memory. Could be one stone two birds.
Answer: Redis may be a solution to this kind of problem. As it handles atomic operations.


Finding operator with given sequence of eigenvalues

Question: I am currently dealing with the spectral theorem for compact, self-adjoint operators. From this theorem, we know that for a linear, bounded, compact and self-adjoint operator the seuqence of eigenvalues is real and the only possible accumulation point is $0$. Hence, we can analyze e.g. the convergence of the sum of all eigenvalues of such operators.
I was wondering, if we there are statements for 'the other way around'. Assume, we have a sequence $(\lambda_n)_{n \in\mathbb{N}}$  of real values with accumulation point $0$. Let's assume that $(\lambda_n)_{n \in\mathbb{N}} \in \ell^1(\mathbb{R})$.
Is there any linear, bounded, compact, self-adjoint operator $T$, such that this operator has the eigenvalues $(\lambda_n)_{\mathbb{N}}$. In other words, is there an one-to-one correspondence between the set of $\ell^1$ sequences and such operators $T$, or at least some relation?
I haven't seen anything in my lecture notes or in books on this topic. Is this, because it is only an 'unconventional' question or is it because there is no such statement?
Thank you in advance for your help!
EDIT: Of course, we need an operator, acting on an infinite-dimensional space
Comment: you need at least infinite dimension
Comment: You can define the operator which has "matrix form" $\text{diag}(\lambda_1,\lambda_2,\dots)$. I think this is bounded, self-adjoint, and compact because $\lambda_n\to 0$?
Comment: Just make sure that the eigenspace associated with the non-zero eigenvalue is finite-dimensional. Then all you need is $\lambda_n\rightarrow 0$ as $n\rightarrow\infty$.
Comment: Thank you for your comments. Of course, we need at least infinite dimensions - I've added this in my text.

I suppose that the compact, bounded, self-adjoint and compact operators are kind of the 'most natural extension' of matrices to infinite dimensional vector spaces, so the diagonal-form might work.

@DisintegratingByParts if I get you right you suggest to take a sequence with only finitely many non-zero entries? This would be the easiest way to force finite-dimensional eigenspaces corresponding to the non-zero eigenvalues. So e.g.
 $\lambda_n = 1/n^2$ won't work?
Comment: Why not take $\ell^2$ with standard basis $e_k$, and define the operator $T$ by $Te_k = \lambda_ke_k$?
Answer: Let $\sigma = (\lambda_n)_{n=0}^\infty\subset \mathbb{R}$ be a sequence of real numbers accumulating at zero, such that $\sum|\lambda_n| < \infty$. (The summability condition implies that $|\lambda_n|$ has finite multiplicity and is bounded above.)
Let $H$ be a separable Hilbert space with inner product $\langle \cdot,\cdot\rangle$ and let $\{e_n\}_{n=0}^\infty$ be an orthonormal basis of $H$. Define an operator $T$ by setting $Te_n = \lambda_ne_n$, with domain
$$D(T) = \bigg\{ v \in H\ \bigg|\ \sum_n (\lambda_n\langle v, e_n\rangle)^2 < \infty \bigg\}$$
Then:

Because $\sigma$ is absolutely summable, the operator $T$ is bounded and has domain $D(T) = H$.
As $\langle e_j, T^*e_k \rangle = \lambda_k$, the operator $T$ is manifestly self-adjoint.
For each $k \geq 0$, define the finite rank operator $T_k$ by setting $$T_ke_n = \begin{cases}\lambda_ne_n, & n \leq k \\ 0, & n > k\end{cases}$$ Denoting by $\|\cdot \|$ the operator norm, observe that $\|T_k - T\| \leq \sup_{i > k} |\lambda_k| \to 0$, so $T_k\to T$ in the operator norm. As $T$ is the operator norm limit of finite rank operators, it is compact.


Is it possible to play this difficult trill on violin?

Question: There is a line in my violin music where there are two voices:

The higher voice is a trill G-Ab on the E string
The lower voice is a continuously held Db on the A string held throughout the trill

How would I play the trill while holding the Db? Is it even possible to do so?
Answer: Just tried it - it's a very awkward thing to play on a violin (easy on a guitar, though)
I used my ring finger on the A to hold the Db, and then use first and index fingers to trill on the E string. It's an annoying stretch, and getting the index finger in there is not comfortable at all.
Are the two voices there supposed to be played on the same violin? I would expect them to be played on two separate violins.
Comment: It's for one violin. Does your trill sound smooth, or is it so uncomfortable that it your hand simply can't do it consistently?
Comment: I can just about make it smooth, but not very fast and not for long. But I am out of practice so a more proficient violinist will find it easier.


How to override CSS Box Sizing for just one area on page?

Question: I have the following CSS inside my Twitter Bootstrap file:
<code>*,
*:before,
*:after {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}
</code>
It is causing a problem with my TinyMCE editor, mainly the resizing of the textarea. Anyway if I remove the above code all is good, but obviously I need it in place cause otherwise the rest of Bootstrap breaks.
So how do I override the above for just TinyMCE? I tried this:
<code>.tinymce-editor,
.tinymce-editor:before,
.tinymce-editor:after {
    -webkit-box-sizing: content-box !important;
    -moz-box-sizing: content-box !important;
    box-sizing: content-box !important;
}
</code>
But no luck. <code>.tinymce-editor</code> happens to be the div that my editor is inside.
Comment: @TK123 You can answer your own question. Instead of adding the answer to the question add it as an answer.
Comment: I was just going to suggest `.tinymce-editor *`.. you probably don't need `!important` though.
Comment: Try avoiding using `!important` by adding another level of selector, aka `.parent .tinymce-editor *,`
Comment: As @hitautodestruct said, you need to post the answer as an answer, not an edit.
Answer: Nevermind just got it:
<code>.tinymce-editor *,
.tinymce-editor *:before,
.tinymce-editor *:after {
    -webkit-box-sizing: content-box !important;
    -moz-box-sizing: content-box !important;
    box-sizing: content-box !important;
}
</code>
Comment: Just an FYI that the `*` selector has a harsh effect on performance  when there are many DOM elements on the page, or in you're case under the `.tinymce-editor` class.
Comment: Actually leave out the *. I wasn't able to make this work with the * left in. My problem was bootstrap stepping on datatables.net.
Comment: * did the worked, I added just the class name, but it was not working, .calss name and * did it.. thanks buddy.


Convert Python to PHP

Question: I tried to convert a script in Python to PHP. It didn't work.
def getL5(iValue,Pvalue):
PixelNoise = open(Pvalue + '.txt','r')
l5 = {}
Ivalue = int(iValue) % 10000
p_x = Ivalue % 100
p_y = int(math.floor(Ivalue / 100))
for iValue in PixelNoise:
    pixel,value = iValue.strip().split(':')
    l5[pixel] = value
PixelNoise.close()
return l5[str(p_x)+','+str(p_y)]

The above is the Python.
public function getL5($iValue,$Pvalue) {
$PixelNoise = fopen($Pvalue.'.txt', "r");
$L5 = array();
$Ivalue = intval ($iValue) % 10000;
$p_x = $Ivalue % 100;
$p_y = intval(floor($Ivalue / 100));
foreach ($PixelNoise as $iValue){
    $temp= explode(':', $iValue);
    $pixel=$temp[0];
    $value=$temp[1];
    $L5[$pixel] = $value;
}
fclose($PixelNoise);
return $L5[(string)$p_x.','.(string)$p_y];
}

The above is my PHP code. Where is it wrong?
Comment: Notice there is a `strip` in python you are not doing in PHP.  Don't know if that's causing the problem though, because you don't say what the symptoms are.  Do you honestly think that"don't work" is helpful diagnostics?
Comment: what error do you get ?
Answer: PHP cannot foreach through lines of a file.  One popular option is to use a while loop with fgets.
function getL5($iValue,$Pvalue) {
   $PixelNoise = fopen($Pvalue.'.txt', "r");
   $L5 = array();
   $Ivalue = intval ($iValue) % 10000;
   $p_x = $Ivalue % 100;
   $p_y = intval(floor($Ivalue / 100));
   if ($PixelNoise) {
       while ($iValue = fgets($PixelNoise)) {
           $temp= explode(':', trim($iValue));
           $pixel=$temp[0];
           $value=$temp[1];
           $L5[$pixel] = $value;
       }
       fclose($PixelNoise);
   }
   else {
     print "There was an error opening the file.";
     // You might want to do some error handling here
     // e.g. trigger an error, or make this function return false.
   }
   return $L5[(string)$p_x.','.(string)$p_y];

}
For consistency you can also use strval() just like intval() instead of the casting.    Also FYI the coding style is not one PHP developers will enjoy reading, consider perusing this advice.

Convert Python to PHP


Multiple Regression and Statistical Significance

Question: I have a two-part data set comprised of treated and untreated values.  When plotted on an X,Y axis, one expects the data two fall into two distinct lines with no expectation that either the slopes or the intercepts will be the same.
I am modeling this with a categorical/indicator value as follows, in order to capture the idea of non-parallel lines (I is the indicator variable):

Y = a0 + (a1-a0)*I + b0X + (b1 - b0)XI

Using Matlab's fitlm command, this is:

mdl = fitlm(T, 'Y ~ X * I')

where T is the table of data, we ask for a regression of Y expressed in XI, and the fitlm command then forces additional terms for I and X alone.
I have tested this in Matlab, and it gives me what I want in terms of coefficients-- I can make scatter plots of fake data, plot the two regression curves (one for each value of the indicator), see that they match the data, the coefficients seem to have the interpretations I desire, etc.
What I would like to be able to do is to run a test (a t-test, I think?) of the two slopes vs each other, and the two intercepts vs each other.  I.e., I would like to be to state that the slopes, say, are different values up to some significance, say, p < 0.05.
How does one do such a thing?
Example code for the fake data, model, and plot follows:
<code>X = [0:.01:10, 0:.01:10]';

Y(1:1001) = X(1:1001) + randn(1,1001)' * .1 ;
Y(1002:2002) = 1 + 0.5* X(1002:2002) + randn(1,1001)' *.1 ;
Y = Y';

I(1:1001) = ones(1,1001);
I(1002:2002) = zeros(1,1001);
I = I';

T = table(X, Y);
T.I = nominal(I);

mdl = fitlm(T, 'Y ~ X * I')

x = [0:.01:10];
y0 = mdl.Coefficients{1,1} + mdl.Coefficients{2,1}*x + mdl.Coefficients{3,1} + mdl.Coefficients{4,1}*x;
y1 = mdl.Coefficients{1,1} + mdl.Coefficients{2,1}*x;

scatter(X,Y);
hold on;
plot(x, y0, 'g');
plot(x,y1, 'r');
</code>
Answer: The coefficient of $XI$ is the estimate of the difference in slopes. The t-statistic for that coefficient (which you should be able to get in the usual regression table for the coefficients in the model) allows you to test for the difference.
Similarly the coefficient of $I$ is the estimate of the difference in intercepts, and the same comments apply. (However interpretation of that coefficient if the slopes are unequal may be difficult, since it relates only to the difference in mean when $X = 0$)
Comment: That result is so intuitive I am ashamed I did not see it myself.  Thank you.


Recasting where two types share a common base type

Question: We have some code like the following:
    class Base
    {
        public string Name;
    }

    class DeeperBase : Base
    {
    }

    class A : Base
    {
        public A()
        {
            Name = "A";
        }
    }

    class B : DeeperBase
    {
        public B()
        {
            Name = "B";
        }
    }

    static T Recast<T>(Base original) where T : Base, new()
    {
        if (!original.GetType().IsAssignableFrom(typeof(T)))
            throw new InvalidCastException();
        return new T();
    }

It looks like Recast is trying to check if the two types are compatible before returning a new T. The method fails, however, for the following code.
    public static void Main(string[] args)
    {
        var a = new A();
        var b = Recast(a); // exception
    }

What's the proper way to check that T and original share the greatest common base type (in this case, Base)?
Comment: Your code is not trying to cast A into a base type, it's trying to cast it to B which is clearly nonsense.
Comment: What are you trying to achieve here?
Comment: @Anton Gogolev In `Recast`, is there a way to check that `original` and `T` share a base type? I feel that there should be a way, and that we must be doing something wrong if an exception is being thrown.
Comment: on your Recast Method. (Base original) declared this way is a concrete type, so it fails because you are trying to pass the concrete class 'DeeperBase' as opposed to a type of Base. You would need to pass in a 'generic' as well.
Comment: I guess you are trying to check if both types has common parent. In that case you should use condition: if (!original.GetType().BaseType.IsAssignableFrom(typeof(T))). But even better for you is to use automapper (emit mappet etc.) and map A to B, because such  cast has no sense. Again, as @AntonGogolev asked, it is not clear what your goal is.
Answer: You are trying to cast an instance A to B, which it can't, since they are not the same type, nor does A derive from B.
There is no way to cast A to B. You could do a Recast<Base>(a), but that doesn't make sense. Note that you are not actually changing the type in any way, you are just returning a new instance of B, or throw an exception.
Comment: Our code stores all objects in a dictionary of type `Base`. `Recast` isn't supposed to return a cast of `original`, but instead replaces `original` with `T` in the dictionary. I guess I'm not really sure what the check is supposed to do, since `T` and `original` will always share `Base`.
Comment: Define "casted type". There is nothing useful going on here.


How to deal with new features in online learning?

Question: I do natural language processing and use words as features. But in online learning, the new training data will exist new words which does not appear in the previous data. How to deal with these new words? how to add new features in Stochastic Gradient Descent model in online learning?
Answer: I have been facing a similar problem for a while. I came up with the following strategies:

For large featurespaces such as with text data, you would ideally need to perform some kind of feature reduction. If you separate this step from the actual model learning step, and perform it once a month or so, you can see whether the new features are relevant enough to require updating the model. It is possible that while you get a lot of new words in that one month, they are not significant enough to warrant updating the (reduced) featurespace of your model.
You can set a threshold of word occurrences, and only when the number of occurrences of the new words cross that threshold, do you consider updating your model. This threshold can be learned using existing data on a cross-validation set.
Perform some kind of time-based attentuation. Perhaps old data samples are not that important, especially if you trying to model preferences, which can change over time.
Unfortunately, at the end, I decided that for my problem, I would have to retrain the model after using all the above steps. So first keep a count of new feature occurrences, once they cross a threshold, perform feature reduction to test significance of the new features, and then re-learn my model using the $n$ most recent data points, discarding the ones that are "too old".

You can also look into kernel-based methods that deal with infinite featurespaces, essentially allowing the dimensions to grow indefinitely.
Answer: One approach is to use feature hashing, where rather than using the words directly you hash them into some fixed size feature space. When you encounter a new word, you apply a hash function, and mod the result so it fits into your feature space. Of course some words will end up being assigned to the same feature, but in practice it usually doesn't hurt performance too much.


Oracle Hint: Return the the newly created rows in recordset

Question: May I know if there is a HINT to return the most newly created data first?
Example this statement
Select * from table where rownum=>5000

sometimes the newly created rows are not in the result set and
Select * from table where rownum=>5000 order by creation_date

if order by creation_time or last_modified, it will take too long to return.
so may I know if there is a way to HINT ORACLE to at least ensure those newly inserted rows are in the result set?
it doesn't need to be in any order just the newly created rows will be in the result will do.
i have exhausted every way to use order by last_modified or creation_time.
Comment: And you don't have a column `creation_time` or `last_modified`?
Comment: maybe `Select * from table order by rownum desc` ?
Comment: Possible duplicate of http://stackoverflow.com/questions/11656476/mysql-order-by-insertion-order-no-sorting-columns
Comment: I do have but it takes too long to process.
Comment: @seesee takes too long? is your date field not indexed? when you tested with date, can you paste that query in your question? i'd exxpect you did `select * from table where your_date >= to_date('date you last fetched the set, 'format') order by your_date`? or are you interested in a top-n style query based on date (newest first?)
Answer: First, index the date column.
Second, specify an order by in descending order, so the newest dates appear first.
Third, use the first rows hint, as you actually just want the first rows quickly, though if you really want 5000 records, it is hardly just the first rows any more. You can pass a desired number of rows with the first rows hint, which allows oracle to pick the best approach for whatever volume of data you require.
Fourth, ensure you have up to date statistics for the table and index.


How to update an object within an array with immutable.js

Question: I wish to update an objects  in an array using immutable.js
My object looks like this:
{
    location: '',
    cars: [
      {id: 1, color: blue},
      { id: 2, color: red}
    ]
};

In my code I get for example car id: 2 and change the color { id: 2, color: black}
I now wish to update the car which is at  index 1 .
I tried :
const car = { id: 2, color: black}
updateIn([ 'cars'  ], list => list.push(car));

This will only add a new car at the end.
Which function in Immutable JS can I use to update car id # 2.
What's  the correct way to do this with immutable.js
Comment: Can you add some more code for context? For example, what are you calling `updateIn()` on?
Answer: const car = { id: 2, color: black};
const indexToBeUpdated = 1;
updateIn(["cars", indexToBeUpdated], () => car);

whatever the index is, you need to pass it to updateIn methods first argument, which is an array.
Answer: Update element in immutable list by condition:
const data = fromJS({
    location: '',
    cars: [
      {id: 1, color: blue},
      { id: 2, color: red}
    ]
});

const car = { id: 2, color: black}

const index = data.get(['cars']).findIndex(listItem => {
  // condition here
  return listItem.get('id') === car.id;
});

const new_data = data.setIn(['cars', index], car);


How do you derive the backward differentiation formula of 3rd order using interpolating polynomials?

Question: It was my exam question, and I could not answer it. How do you drive the backward differentiation formula of 3rd order (BDF3) using interpolating polynomials?  I only knew how to derive it using the undetermined coefficients method.
BDF3 : $x_{n+1} = \frac{18}{11}x_{n} - \frac{9}{11}x_{n-1} + \frac{2}{11}x_{n-2} + \frac{6}{11}hx_{n+1}$
Comment: Generally, you interpolate and then compute whatever derivative or integral you want from that polynomial. There is not enough context in your question to answer more specifically.
Comment: I think the context is already there, if you know where to do look at. "Interpolating polynomials" and "Drive BDF3". That is all the needed keywords.
Comment: Sure, and the answerer (TroyHaskin) understood what exactly you wanted, right?
Comment: Not really. He has not used "polynomials interpolation". I think we must set all point intervals in respect to $x_1$ and $h$ like $x_2 = 2h$, $x_0 = -h$, $x_{n-1} = -2h$ and so on. Then we must plug those terms with $h$ into "interpolating polynomial formula", and then integrate all terms. (Integration here is just change the summation symbol into the integration. I am not sure though.
Answer: I'll start with the way I think you know and then discuss the method which uses explicit interpolation scheme. I'll note that the differences are mostly semantic and lead to the same answer, but it never hurts to know how to approach a problem from multiple angles.

Let's say we want to find the value of $x_{n+1}$ that balances the differential equation
$$
\frac{\mathrm{d}x}{\mathrm{d}t}=f(t,x)
$$
at $t=t_{n+1}$ given we know $x_{n-2}$, $x_{n-1}$, and $x_{n}$ at times $t_{n-2} = t_{n+1}- 3 \Delta{t}$, $t_{n-1} = t_{n+1}- 2 \Delta{t}$, and $t_{n} = t_{n+1}- \Delta{t}$, respectively, and we have no closed form for $x(t)$.
One way to approach the problem is to express the derivative of $x(t)$ on the left-hand side of the ODE as a linear combination of the three values we know in addition to the value we're searching for:
$$
\frac{\mathrm{d}x}{\mathrm{d}t}\bigg\rvert_{t_{n+1}} = \omega_{n+1} x_{n+1} + \omega_{n} x_{n} + \omega_{n-1} x_{n-1} + \omega_{n-2} x_{n-2}.
$$
In order to determine the coefficients of the combination, we can replace the known values with suitable Taylor Expansions about $t_{n+1}$:
$$
\begin{align}
x_{n}   &= x_{n+1} + (-1\Delta{t}) \frac{\mathrm{d}x}{\mathrm{d}t}\bigg\rvert_{t_{n+1}} + \frac{(-1\Delta{t})^2}{2!} \frac{\mathrm{d}^2 x}{\mathrm{d}t^2}\bigg\rvert_{t_{n+1}} + \frac{(-1\Delta{t})^3}{3!} \frac{\mathrm{d}^3 x}{\mathrm{d}t^3}\bigg\rvert_{t_{n+1}} + \mathcal{O}(\Delta{t}^{4}) \\
x_{n-1} &= x_{n+1} + (-2\Delta{t}) \frac{\mathrm{d}x}{\mathrm{d}t}\bigg\rvert_{t_{n+1}} + \frac{(-2\Delta{t})^2}{2!} \frac{\mathrm{d}^2 x}{\mathrm{d}t^2}\bigg\rvert_{t_{n+1}} + \frac{(-2\Delta{t})^3}{3!} \frac{\mathrm{d}^3 x}{\mathrm{d}t^3}\bigg\rvert_{t_{n+1}} + \mathcal{O}(\Delta{t}^{4}) \\
x_{n-2} &= x_{n+1} + (-3\Delta{t}) \frac{\mathrm{d}x}{\mathrm{d}t}\bigg\rvert_{t_{n+1}} + \frac{(-3\Delta{t})^2}{2!} \frac{\mathrm{d}^2 x}{\mathrm{d}t^2}\bigg\rvert_{t_{n+1}} + \frac{(-3\Delta{t})^3}{3!} \frac{\mathrm{d}^3 x}{\mathrm{d}t^3}\bigg\rvert_{t_{n+1}} + \mathcal{O}(\Delta{t}^{4})
\end{align}.
$$
As will be shown, the expansions were carried out to third-order to create a solvable (square) system.  For other numerical methods, the number of needed Taylor Expansions (in this problem, three) and needed order (in this problem, again, three) may increase or decrease as needed to effect a solution.
As an aside, by examining the pattern from the expansions above, it is easy to see that for this constant $\Delta{t}$ problem, the general form of the Taylor Expansions is
$$
x_{n-k} = x_{n+1} + \sum_{m=1}^p \frac{[-(k+1) \Delta{t}]^m}{m!} \frac{\mathrm{d}^m x}{\mathrm{d}t^m}\bigg\rvert_{t_{n+1}} + \mathcal{O}(\Delta{t}^{p+1}),
$$
where for this problem, $k \in \{0,1,2\}$ (mapping to $x_n$, $x_{n-1}$, and $x_{n-2}$) and $p = 3$ for the third order expansions. This generalization is limited to only constant $\Delta{t}$ methods but can be used in computer algebra systems to create generic code to calculating various other order methods for these systems. Aside over.
Substituting these three Taylor Expansion equalities into the linear combination gives
$$
\begin{align}
\frac{\mathrm{d}x}{\mathrm{d}t}\bigg\rvert_{t_{n+1}} =
    &\left[\omega_{n+1}  + \omega_{n} +                 \omega_{n-1} +                 \omega_{n-2}\right] x_{n+1} \\
    &\left[(-1\Delta{t})   \omega_{n} + (-2\Delta{t})   \omega_{n-1} + (-3\Delta{t})   \omega_{n-2}\right] \frac{\mathrm{d}x}{\mathrm{d}t}\bigg\rvert_{t_{n+1}} \\
    &\left[\frac{(-1\Delta{t})^2}{2!} \omega_{n} + \frac{(-2\Delta{t})^2}{2!} \omega_{n-1} + \frac{(-3\Delta{t})^2}{2!} \omega_{n-2}\right] \frac{\mathrm{d}^2x}{\mathrm{d}t^2}\bigg\rvert_{t_{ n+1}} \\
    &\left[\frac{(-1\Delta{t})^3}{3!} \omega_{n} + \frac{(-2\Delta{t})^3}{3!} \omega_{n-1} + \frac{(-3\Delta{t})^3}{3!} \omega_{n-2}\right] \frac{\mathrm{d}^3x}{\mathrm{d}t^3}\bigg\rvert_{t_{ n+1}}
&+ \mathcal{O}(\Delta{t}^{4})
\end{align}.
$$
In the above expression, there is a first-order derivative at $n+1$ on the left-hand side and several different orders of derivatives on the right-hand side.
In order to give the best approximation, we seek weights that zero the coefficients of the zeroth-, second-, and third-order derivatives and gives a coefficient of one for the first-order derivative:
$$
\begin{align}
\omega_{n+1}  + \omega_{n} +                 \omega_{n-1} +                 \omega_{n-2} &= 0 \\[1em]
(-1\Delta{t})   \omega_{n} + (-2\Delta{t})   \omega_{n-1} + (-3\Delta{t})   \omega_{n-2} &= 1 \\[1em]
\frac{(-1\Delta{t})^2}{2!} \omega_{n} + \frac{(-2\Delta{t})^2}{2!} \omega_{n-1} + \frac{(-3\Delta{t})^2}{2!} \omega_{n-2} &= 0 \\[1em]
\frac{(-1\Delta{t})^3}{3!} \omega_{n} + \frac{(-2\Delta{t})^3}{3!} \omega_{n-1} + \frac{(-3\Delta{t})^3}{3!} \omega_{n-2} &= 0
\end{align}.
$$
This can be expressed as a square, linear system:
$$
\begin{bmatrix}
1& 1&1&1\\
0&-1\Delta{t} & -2\Delta{t} & -3\Delta{t}\\
0& \frac{1}{2!}(-1\Delta{t})^2 &\frac{1}{2!}(-2\Delta{t})^2&\frac{1}{2!}(-3\Delta{t})^2\\
0& \frac{1}{3!}(-1\Delta{t})^3 & \frac{1}{3!}(-2\Delta{t})^3 &  \frac{1}{3!}(-3\Delta{t})^3
\end{bmatrix}
\begin{bmatrix}
\omega_{n+1}\\\omega_{n}\\\omega_{n-1}\\\omega_{n-2}
\end{bmatrix}
=
\begin{bmatrix}
0\\1\\0\\0
\end{bmatrix}.
$$
The solution of this system leads to the linear combination:
$$
\frac{\mathrm{d}x}{\mathrm{d}t}\bigg\rvert_{t_{n+1}} = \frac{1}{\Delta{t}} \left(\frac{11}{6} x_{n+1} - 3 x_{n} + \frac{3}{2} x_{n-1} - \frac{1}{3} x_{n-2}\right) + \mathcal{O}(\Delta{t}^4)
$$
where the equality becomes approximate when the higher-order terms are omitted.

Another approach to take is to use the information we have and the conditions we want to form a local cubic approximation to $x(t)$. Consider the following polynomial
$$
\hat{x}(t) = c_0 + c_1 (t - t_{n+1})  + c_2 (t - t_{n+1})^2  + c_3 (t - t_{n+1})^3.
$$
If $\hat{x}(t)$ exactly matches the three points we know in addition to the value we're searching for at their associated times, it may be a good approximation of $x(t)$ close to $t_{n+1}$.  And if $\hat{x}(t)$ is a good approximation, it's derivative may approximately balance the ODE at $t_{n+1}$:
$$
\frac{\mathrm{d}x}{\mathrm{d}t}\bigg\rvert_{t_{n+1}} \approx \frac{\mathrm{d}\hat{x}}{\mathrm{d}t}\bigg\rvert_{t_{n+1}} = c_1
$$
Note that because we're clever and centered the polynomial on $t_{n+1}$, only the $c_1$ coefficient remains after evaluation of the derivative at that time.
Through the four requirements on the polynomial to match our four points-of-interest, we can form the linear system
$$
\begin{bmatrix}
1 & 0 & 0 & 0 \\
1 & -1 \Delta{t} & (-1 \Delta{t})^2 & (-1 \Delta{t})^3\\
1 & -2 \Delta{t} & (-2 \Delta{t})^2 & (-2 \Delta{t})^3\\
1 & -3 \Delta{t} & (-3 \Delta{t})^2 & (-3 \Delta{t})^3
\end{bmatrix}
\begin{bmatrix}
c_0 \\ c_1 \\ c_2 \\ c_3
\end{bmatrix}
=
\begin{bmatrix}
x_{n+1} \\ x_n \\ x_{n-1} \\ x_{n-2}
\end{bmatrix}
$$
and obtain the value of the needed coefficient
$$
c_1 = \frac{1}{\Delta{t}} \left(\frac{11}{6} x_{n+1} - 3 x_{n} + \frac{3}{2} x_{n-1} - \frac{1}{3} x_{n-2}\right)
$$
This answer agrees with the approximation from the Taylor Expansion method after neglecting higher-order terms.

Both are valid approximations and merely differ in how the problem is approached:

Directly approximate the derivative of an unknown function with values we know and a cubic Taylor approximation about the point-of-interest.
Directly approximate the unknown function with values we know using a cubic polynomial and evaluate its derivative at the point-of-interest.

Would the answer be the same if we used a different basis for the interpolation such as Lagrange versus the above Monomial?  It shouldn't since all one-dimensional interpolating polynomials of degree $n$ are unique given $n+1$ interpolation points.  But let's check.
Consider the following polynomial
$$
\hat{x}(t) = \sum_{j=-2}^{1}\,\prod_{\substack{-2\le m \le 1\\ m \neq j}}
\frac{t - t_{n+m}}{t_{n+j} - t_{n+m}} \,x_{n+j}
$$
It would be rather verbose and tedious to look at all of the terms, so let's focus on the $j = 1$ term of the polynomial:
$$
\frac{(t-t_{n})(t-t_{n-1})(t-t_{n-2})}{(t_{n+1}-t_n)(t_{n+1}-t_{n-1})(t_{n+1}-t_{n-2})} x_{n+1}
$$
We can compress the denominator to $6 \Delta{t}^3$ since the interval $\Delta{t}$ is fixed between all points, and we note that the derivative of the product in the numerator is a summation of lower degree products:
$$
\frac{\mathrm{d}}{\mathrm{d}t}\left[(t-t_{n})(t-t_{n-1})(t-t_{n-2})\right] =
(t-t_{n})(t-t_{n-1}) + (t-t_{n})(t-t_{n-2}) + (t-t_{n-1})(t-t_{n-2})
$$
which is equal to $11 \Delta{t}^2$ when evaluated at $t_{n+1}$.  So the coefficient for the $x_{n+1}$ coefficient is $\tfrac{11}{6 \Delta{t}}$, which is the same as the two above results.
Performing the process for each other value of $j$ will also generate the same terms.
Comment: @Ehsan To be clear, any polynomial formed from a selected set of points is an interpolating polynomial.  However, it appears you are interested in the Lagrange form.  I added a brief description of how to perform the construction using that basis but did not go through each of the terms as it is rather tedious and very mechanical.
Comment: @Samuel Yes. As stated in the comments (though a bit unclear in the answer), the Taylor Series version right-hand side $[0\,1\,0\,0]^{T}$ creates weights that zero out the zeroth, second, and third derivatives while retaining the $1$ for the first derivative. The right-hand side can thus be changed to retain any desired order derivative by changing the placement of the $1$. Also, given an interpolating polynomial, simply take the derivative of the polynomial to your desired order of derivative, assuming the polynomial is not the zero function following the differentiation.
Comment: @Samuel I expanded the Taylor Expansion derivation. To derive expressions for other orders of derivatives comes down to calculating weights that zero all orders of derivative other than the desired order. This particular problem zeros all derivative orders other than one. The $k$ is how many steps behind $n+1$ the Taylor Expansion is. I also expanded this point in the answer update.
Comment: @Samuel There is not citation needed. It's a typical form of the Taylor Expansion with $(x-a) == -(k+1)\Delta{t}$. The equality is simply a result of the $k$-th step being integer multiples of $\Delta{t}$ behind $x_{n+1}$. In fact, I wrote the three Taylor Expansions first by-hand and then created the generalization since that is how I created and solve the equations (in Mathematica, I believe). I have inverted the discussion for more proper framing.
Comment: @Samuel BDF methods above sixth order are not (zero-)stable and can't be used. Further, since BDF methods are also implicit (i.e., the right hand-side function $f(t_{n+1},x_{n+1})$ is evaluated at $x_{n+1}$), they require a nonlinear solve in-general and higher-order BDF methods have more terms in the solve (not a deal breaker, but it does increase complexity). Lastly, BDF methods are not "self-starting" like Runge-Kutta Methods, so some combination of other solvers is needed to generate all needed data (all state values before $x_{n+1}$) before using the high-order BDF scheme.
Comment: Thanks for the lengthy answer, I up-voted it. You used Taylor series and cubic expansion as your tools. I didn't go through the details but it seems your approach is right. I think my instructor wanted me to use  "interpolation polynomial" formula in order to drive the BDF3: $ f(x) \approx \sum_{i=0}^{n} \Big( \prod_{0 \le j \le n, j \neq i} \frac{x-x_j}{x_i - x_j} \Big)f(x_i)$ which I see you still have not used. Anyway thanks for the answer.
Comment: @TroyHaskin How did  you get the RHS column of [0 1 0 0 ] on the taylor series version?
Comment: @MathIsHard Each column represents a truncated Taylor Expansion for $x_{n-k}$. Substituting each of those columns into the previous weight equation for $\mathrm{d}x/\mathrm{d}t|_{t_n+1}$, you have a left hand-side with only a first order derivative, and a right-hand side with many orders of derivative. To make the right-hand side sum to only a single first order derivative, the load vector of the linear system must be $[0\,1\,0\,0]^T$, where each row is an increasing order of derivative (i.e., zero-th, first, second, and third).
Comment: Is it possible to use the same approach to derive a backward differentiation formula for the second and third derivative?
Comment: @TroyHaskin Thank you for the answer! I tried to derive an expression for the second derivative. But I got stuck. In the expression for the Taylor expansion above where does $k$ in $[-(k+1) \Delta{t}]^m$ come from? I'd be very happy if you could give me a hint how to derive the second derivative with the approach above!
Comment: @TroyHaskin Wow, what a great explanation! Thanks!
Comment: @TroyHaskin Can you please explain where $x_{n-k} = x_{n+1} + \sum_{m=1}^p \frac{[-(k+1) \Delta{t}]^m}{m!} \frac{\mathrm{d}^m x}{\mathrm{d}t^m}\bigg\rvert_{t_{n+1}}$ comes from? Is it the discrete Taylor series? Could you please add a link to some source? Thanks!
Comment: Perfect, now I see it. Thank you for the effort!
Comment: @TroyHaskin You mentioned that based on the problem the number of needed Taylor Expansions and the needed order varies. Can I not just use more Taylor Expansions of higher order to get better results? Or does it not work this way?


How to know when can I use proof by contradiction to prove operations with irrational/rational numbers?

Question: I know that the proof by contradiction is useful when we know that there is only one answer and we can pick from two exclusive options. Therefore, if one doesn't work, we know the other is the correct answer. It seems to me that to use the proof I require, beforehand, the knowledge that the result can be only one option or the other.
When proving, that the sum of an irrational number and a rational number is irrational, we can use proof by contradiction to demonstrate that it can't be rational. Doesn't this assume that the sum can't be sometimes rational and other times irrational?
However, with the sum of two irrational numbers, the result can be either rational or irrational based on the chosen numbers. How can I deduce, beforehand, that the proof by contradiction won't be useful in this case?
Comment: The exclusive alternative is usually that a statement is either true or false, so one will not necessarily have a fine-grained dichotomy like rational vs. irrational.  Indeed a "direct" proof could always be recast as a proof by contradiction, so I doubt that one can give a rule for when "the proof by contradiction is useful".
Answer: The trick is to use closure rules we know for rationals: they're closed under addition, subtraction, multiplication, and division if the quotient is nonzero. Let's discuss your examples:

If $i$ is irrational and $r$ is rational, suppose $i+r$ is rational: then $i=(i+r)-r$ is rational, a contradiction.
If $i,\,j$ are irrational, let $k:=i+j$; we can't write any one variable as a function of rationals, so an argument similar to the above is unavailable.
Answer: When we prove that the sum of a rational and an irrational number cannot be rational, we are certainly not assuming that the sum cannot be rational in some cases and irrational in others: we are proving that this is the case. This really has nothing to do specifically with proofs by contradiction; it just happens that in this case we prove that something is true by proving that its negation is false. And in fact it's entirely possible to prove the result without using proof by contradiction.

Proposition: If $x,y\in\Bbb R$, and $x$ is rational, then $x+y$ is rational if and only if $y$ is rational.

Proof: If $y$ is rational, then $x+y$ is the sum of two rational numbers and is therefore rational. If $x+y$ is rational, then $y=(x+y)+(-x)$ is the sum of two rational numbers, so $y$ is rational. $\dashv$

Knowing when a proof by contradiction or a proof of the contrapositive is likely to be a good way to proceed is in large part a matter of experience, but there is at least one moderately useful guide: if the hypotheses available for a proof of the contrapositive or a proof by contradiction seem to give you more to work with — more specific details, for instance — than the hypotheses available for a direct proof, then it's definitely worth your while to consider an indirect proof.
Comment: But using the proof by contradiction to prove it takes the premise that the result can only be rational or irrational for all the cases, right? I mean, could be the case that a proof by contradiction wouldn't work for something because this premise of either one option or the other isn't right?
Comment: @Jon: No, it doesn't. It simply proves that the result **is** irrational in all cases. If there were some cases in which the sum was rational and others in which it was irrational, the result would simply be false, and no attempt to prove it, by any method whatsoever, would work. This has nothing to do with the method of proof that you're attempting to use.