Salvar no localStorage a posição para mante-la quando recarregar a página

Question: Quando o usuário clica em um determinado botão a posição scroll de um elemento é alterado. Gostaria que essa ação ficasse armazenada no LocalStorage, porém não sei fazer... podem me ajudar?
Aqui meu script:
<code>$('.btn.parteum').click(function(){
      $('#textarea').animate({ scrollLeft: 0 }, 400);
      return false;
});
$('.btn.parte2').click(function(){
      $('#textarea').animate({ scrollLeft: 140 }, 400);
      return false;
});
</code>
exemplo: https://jsfiddle.net/g43rmesL/
Answer: Você pode adicionar um callback que será chamado ao fim da animação:
<code>//recupera o valor salvo ao recarregar a pagina
if(localStorage.getItem('scrollposition')){
     $('#textarea').scrollLeft(localStorage.getItem('scrollposition'));
}

$('.btn.parteum').click(function(){
      $('#textarea').animate({ scrollLeft: 0 }, 400, function(){
          localStorage.setItem('scrollposition', 0);
      });
      return false;
});
$('.btn.parte2').click(function(){
      $('#textarea').animate({ scrollLeft: 140 }, 400, function(){
          localStorage.setItem('scrollposition', 140);
      });
      return false;
});
</code>
Exemplo funcionando: https://jsfiddle.net/g43rmesL/1/

http://api.jquery.com/animate/
Comment: obrigado pela resposta! aqui não funcionou... vc testou? ou será que eu não estou sabendo colocar lá?... coloquei exatamente como está aí.
Comment: Seu código na pergunta é insuficiente para testar. Adiciona seu html à pergunta que eu testo e tendo achar o erro.
Comment: Aqui, fiz um exemplo no fiddle: https://jsfiddle.net/g43rmesL/
Comment: Está salvando no localStorage normalmente
Comment: ué, aqui quando eu recarrego a página não fica na posição do scroll do botão 2, volta pro primeiro
Comment: Sua resposta não foi muito clara... Você quer que ao recarregar recupere esse valor e repita a ação né, irei editar minha resposta adicionando essa função
Comment: agora que percebi mesmo... devia ter dito que era "memorizar" e não "armazenar", vou editar
Comment: Acho talvez seria melhor dizer "Salvar no localStorage a posição para mante-la quando recarregar a página" Mas ok, está certo agora. Da uma olhada
Comment: Obrigado por tudo!!!!!
Comment: Tranquilo cara, até mais.


Git syncing branches from branches

Question: Let's say I've created a branch "B" off of an old tag from "A". I make some changes on "B" and open a pull request back against "A". I then make a "C" branch off of "B", which I make changes to and open a pull request, etc, until I reach branch "E".
Once I finish with my changes on "E", let's say all of these branches still have open, unmerged pull requests against "A". Since these could then be merged in any order, and "E" may have made some important changes to my original changes in"B", how do I ensure "B" has the changes from "C, D, E"; "C" has the changes from "D, E"; and "D" has the changes from "E".
Let's assume I needed to go back and make changes to A prior to any pull requests being merged, but it relied on certain changes from E.
A -----
 \
  B -----
   \
    C -----
     \
      D -----
       \
        E -----

Comment: @TimBiegeleisen done
Comment: Wait, do you want all branches – B, C, D, and E – to point to the same thing/have the same state? Or just to make sure that when all of them have been merged to A, all of their changes will have been applied?
Comment: Yeah I may have worded it weird.  Let's say I want my A to have Es changes prior to the PRs getting merged so I can continue development with the latest changes?
Answer: The way you have drawn it, C has all B plus some, D has all C plus some, and E has all D plus some, so when you merge E into A, you get all of D, C, and B into A, so A will have it all.
If you say that you can merge any of B, C, D or E, in any order and ask "How will Git know what commits to merge each time"? Well, that's one of the the big ideas of git merge - git will go back and realize what has already been merged in and won't try to remerge those changes. For instance, if you merge B, which is composed of B1, B2, and B3 and then merge C (which is composed of C1, C2, C3 + B(B1, B2, B3)), then git looks at the merge history and finds all the commits that are already reached from the merge point and finds the ones that are not reached and merges those that are not reachable - that's how it knows what to merge (in this case, it would be C1, C2, C3 since it finds that B1, B2, B3 are reachable). So, internally, for instance, if you have already merged B into A and then tell it to merge C into A, git identifies the un-merged commits by doing something like git rev-list C --not A and merges those commits.The key to understanding this is to think about it like a graph and realize that git is simply trying to figure out which parts of the graph are not reachable from the merge point and the branch to be merged and those are the changes that it then wants to merge in.
If you continue on B (with B4) after the merge of B, that's fine, you can simply remerge branch B and it will take in just the new changes from B onto A because it does that graph traversal that I just described and realizes that B1, B2, and B3 have already been merged, and now its just this new B4 that it needs to merge.
Answer: The typical lifecycle of a branch created from a parent branch is that the former ends up getting merged back into the parent at some point.  This concept is not limited to Git, but to development workflow in general.
In your situation, if you follow this principle it immediately resolves your problem.  Here is your diagram, redrawn to be a bit more palatable:
A -----
 \
  B -----
   \
    C -----
     \
      D -----
       \
        E -----

Following the principle of merging a branch back into its parent, the order of merging should be E into D, D into C, C into B, and finally B into A.  In each of these merges, there should ideally be no conflicts.  If there are conflicts, they should not be due to the parent and branch stepping on each other's toes, i.e. both branches having worked on the same exact areas of a feature.
If your development process might require that you need to bring in the changes from E into A before rolling up all the branches in between, then you should reconsider how you create branches.  In this case, you would have been better of creating branches A through E directly from the parent branch.
Comment: Cool, now let's say I've branched new branches sporadically off of each other, but they all need to end back up into A... is there a way to tell which branch the parent is?
Comment: This is a non-trivial thing to accomplish.  Why would you not know whence a branch is derived?  A better way to deal with this is to give your feature branches names which identify them as coming from a certain parent.  E.g. if the parent is called `product`, then the feature branches in the September sprint might be called `product_09_2015_A`, `product_09_2015_B`, etc.
Comment: If I have 20 local feature branches that all sort of touch similar services and I'm in a rush to get stuff done, I might not be giving enough up front thought to my branching/merging strategies.  If and when this occurs, I'm wondering if there's some better way to visualize a branches ancestry. Our branches have strict naming conventions.
Comment: Any one of the visual Git tools out there can easily and quickly show you whence a branch is derived.  But to do this on the command line is involved, which is why I suggested an alternative.

Git syncing branches from branches


Proof by induction of summation inequality: $1+\frac{1}{2}+\frac{1}{3}+\frac{1}{4}+\dots+\frac1{2^n}\ge 1+\frac{n}2$

Question: Prove by induction the summation of $\frac1{2^n}$ is greater than or equal to $1+\frac{n}2$.
We start with $$1+\frac{1}{2}+\frac{1}{3}+\frac{1}{4}+\dots+\frac1{2^n}\ge 1+\frac{n}2$$ for all positive integers.
I have resolved that the following attempt to prove this inequality is false, but I will leave it here to show you my progress. In my proof, I need to define P(n), work out the base case for n=1, and then follow through with the induction step. Strong mathematical induction may be used.
This is equivalent to $$\sum_{k=0}^n\frac1{2^k}\ge 1+\frac{n}2\;.$$
Let $P(n)$ be summation shown above.
Base case for $n=1$, the first positive integer,
$$\sum_{k=0}^1\frac1{2^k}=\frac1{2^0}+\frac1{2^1}=1+\frac12=\frac32\ge 1+\frac12=\frac32\;,$$
so base case is true.
Induction step: Assume $P(n)$ is true and implies $P(n+1)$. Thus
$$\sum_{k=0}^{n+1}\frac1{2^k}\ge\frac1{2^{n+1}}+\sum_{k=0}^n\frac1{2^k}\ge 1+\frac{n+1}2\;.$$
This can be written as
$$\sum_{k=0}^{n+1}\frac1{2^k}\ge \frac1{2^{n+1}}+1+\frac{n}2\ge 1+\frac{n+1}2\;.$$
I work the math out but I get stuck contradicting my statement. Please show your steps hereafter so I can correct my mistakes.
Comment: Please check that I didn't make any inadvertent changes when I added $\LaTeX$ to your post.
Comment: You're not trying to reach a contradiction: you're trying to show that **if** $P(n)$ is true, **then** $P(n+1)$ is true.
Comment: Thanks for adding the LaTex. I know I should be proving the summation is true but my math always results in a contradiction. I guess I'm not manipulating the terms correctly. Got any tips?
Comment: 1+1/2+1/4=7/4 but 1+2/2 = 2 <7/4, thus, what you are trying to prove is false for n=2.
Comment: @Pax: $2 < 7/4$? Indeed, it is false, but not because $2 < 7/4$.
Comment: @TMM Oh, I assumed that summation of 1/2^n was what any mathematician would think: 1+1/2+1/4+...+1/2^n.
Answer: I think that your notation is rather badly confused: I strongly suspect that you're supposed to be showing that $$\sum_{k=1}^{2^n}\frac1k\ge 1+\frac{n}2\;,\tag{1}$$ from which one can conclude that the harmonic series diverges. The basis step for your induction should then be to check that $(1)$ is true for $n=0$, which it is: $$\sum_{k=1}^{2^n}\frac1k=\frac11\ge 1+\frac02\;.$$
Now your induction hypothesis, $P(n)$, should be equation $(1)$, and you want to show that this implies $P(n+1)$, which is the inequality $$\sum_{k=1}^{2^{n+1}}\frac1k\ge 1+\frac{n+1}2\tag{2}\;.$$ You had the right idea when you broke up the bigger sum into the old part and the new part, but the details are way off:
$$\begin{align*}\sum_{k=1}^{2^{n+1}}\frac1k&=\sum_{k=1}^{2^n}\frac1k+\sum_{k=2^n+1}^{2^{n+1}}\frac1k\\
&\ge 1+\frac{n}2+\sum_{k=2^n+1}^{2^{n+1}}\frac1k\tag{3}
\end{align*}$$
by the induction hypothesis $P(n)$. Now look at that last summation in $(3)$: it has $2^{n+1}-2^n=2^n$ terms, and the smallest of those terms is $\dfrac1{2^{n+1}}$, so $$\sum_{k=2^n+1}^{2^{n+1}}\frac1k\ge 2^n\cdot\frac1{2^{n+1}}=\frac12\;.$$ If you plug this into $(3)$, you find that $$\sum_{k=1}^{2^{n+1}}\frac1k\ge 1+\frac{n}2+\frac12=1+\frac{n+1}2\;,$$ which is exactly $P(n+1)$, the statement that you were trying to prove.
You've now checked the basis step and carried out the induction step, so you can conclude that $(1)$ is true for all $n\ge 0$.
Comment: I understand now that my summation notation was incorrect. However, the base case here is invalid because 0 is not a positive integer. Therefore the base case should start at n=1.
Comment: @Izzy: It doesn't really matter: the result is true for $n=0$, so there's no harm starting there.
Comment: What does the "smallest of those terms" mean? Where does 2^(n+1) - 2^n = 2^n come from?
Answer: The base case looks okay. For the inductive step, you want to assume $P(n)$ is true, and you need to show that $P(n) \rightarrow P(n+1)$. Your wording suggests that you are assuming that implication.
So, you assume for all $k \geq 1$
$$\sum\limits_{i=0}^{k} \frac{1}{2^i} \geq 1 + \frac{k}{2}.$$
Then we have the following when $n = k + 1$
$$\sum\limits_{i=0}^{k+1} \frac{1}{2^i} = \frac{1}{2^{k+1}} + \sum\limits_{i=0}^{k} \frac{1}{2^i} \geq \frac{1}{2^{k+1}} + 1 + \frac{k}{2}.$$
We know that $\frac{1}{2^{k+1}} > \frac{1}{2}$, so we have
$$\sum\limits_{i=0}^{k+1} \frac{1}{2^i} \geq \frac{1}{2^{k+1}} + 1 + \frac{k}{2} > 1 + \frac{k+1}{2}.$$
Thus, for all $n \geq 1$, $P(n) \rightarrow P(n+1)$, so the hypothesis holds.
Comment: $\sum_{i\ge 0}\frac1{2^i}=2$, and the stated result is false for all sufficiently large $k$.
Comment: Oh I don't know where my mind went. I made a terrible mistake in saying that $\frac{1}{2^{k+1}} > \frac{1}{2}$. Just a stupid error.
Answer: Hint:
$$\sum_{k=0}^n\frac1{2^k}=\frac{2^{n+1}-1}{2^n}$$
Comment: True, but it won't help the OP, since the result that he stated is false (and almost certainly isn't the one that he was actually supposed to prove).


do_ypcall: clnt_call: RPC: Unable to receive; errno = Connection refused

Question: I had been running a time-consuming background program via a bash script on a linux server. In the same bash script, I set the notification by "set -o notify" so I could know when the background job was done.
Probably yesterday night the notification popped out to say the background job was done, and after that there were following error messages, which I am not sure if occured immediately after the background job finished or a while later, since I was not at my terminal at that time:

do_ypcall: clnt_call: RPC: Unable to receive; errno = Connection refused
do_ypcall: clnt_call: RPC: Unable to receive; errno = Connection refused
do_ypcall: clnt_call: RPC: Unable to receive; errno = Connection refused

I also redirected the stdout output of my background job to a log file which seems to say the program was not finished as expected but terminated midway.
Could you explain the meaning of the error message? If possible,what kinds of problem could I possibly met? Could it be that the administrator placed some restriction on the resources that I can use on that server?
Thanks and regards!

UPDATE:
the same hard drive is mounted via NFS over several servers including the one mentioned above. I just found a similar but different error occur on the other server:

do_ypcall: clnt_call: RPC: Timed out

This error and the previous error seem not affect the programs that are running. BTW my program has IO operations.
Comment: is your script using sudo by any chance?
Comment: @stsquad It is really a long time ago. I forgot.
Comment: that's a shame. We have a build script that uses sudo and I've noticed it throws these errors while the sudo commands are running. Obviously they trigger NIS queries when checking the user ID for sudo.
Answer: Sounds like you might have NIS running and it has failed, pretty normal with NIS in my experience :-).  If it is still happening, maybe try <code>/etc/init.d/ypbind restart</code> ?  Perhaps this box is bound to Active directory and the connect failed ...
Comment: Thanks! Yes it is still happening. I tried */etc/init.d/ypbind restart* but "bash: /etc/init.d/ypbind: No such file or directory
".
Answer: had this issue on a machine where the /var filesystem had been offlined due to disk faults -
ypbind would restart, but couldn't update it's 'binding' file, and so triggered errors
after reboot / fsck and resolving the filesystem problem, the issue was resolved.
possibly permissions or filesystem isues in /var related here?


Function returns None when it should return the last index

Question: I'm trying to get this function to return the index of the smallest value in a list. It works for every case besides if the smallest value is the last element of the list. Can anyone spot my mistake?
def rotation_point(rotated_list):
  length = len(rotated_list)
  for i in range (0,length-1):
    if rotated_list[i]<rotated_list[(i+1)%length] and rotated_list[i]<rotated_list[i-1]:
      return i
    else:
      pass

I know it has something to do with the first part of the if statement, but I can't see why it wouldnt work.
Comment: You're missing a `return` for that scenario. add `return length-1` after the `for loop`
Comment: Also it's important to know that the list has been sorted, but rotated by some amount (i.e. [2,3,4,5,1])
Answer: The if statement itself works just fine but you never actually check the last element because you use: range(0,length-1) for the range of the for loop. The range function already stops at length - 1. You should use: range(0,length).
Note that you also don't really need the 0 since range already starts at zero by default, and there is no need for the else: pass since the use of an else statement is not required, removing it would change nothing.
This is what a working (and cleaner) version of your function would look like:
def rotation_point(rotated_list):
  length = len(rotated_list)
  for i in range (length):
    if rotated_list[i]<rotated_list[(i+1)%length] and rotated_list[i]<rotated_list[i-1]:
      return i

Here are some more examples of the range function:
>>> range(0, 5-1)
[0, 1, 2, 3]

>>> range(0, 5)
[0, 1, 2, 3, 4]

>>> range(5)
[0, 1, 2, 3, 4]

Answer: Note that range(i, j) goes from i to j - 1. Since you are using range(0, length - 1), you are not traversing the entire array, just from the start to the second last index. A super easy fix is to add return length - 1 to the end.
def rotation_point(rotated_list):
  length = len(rotated_list)
  for i in range (0,length-1):
    if rotated_list[i]<rotated_list[(i+1)%length] and rotated_list[i]<rotated_list[i-1]:
      return i
    else:
      pass
  return length - 1


Python - Estimativa da raiz quadrada

Question: Boas, eu estava aqui a resolver um exercicio que o programa pede ao utilizador um número positivo e calcula a sua raiz quadrada utilizando:
+=(+ú/)/
 é uma estimativa para a raiz quadrada do número;
+ é a estimativa seguinte baseada na estimativa anterior.
A primeira estimativa da raiz quadrada deve ser  = ú/ .
O cálculo da próxima estimativa deve prosseguir enquanto a diferença |+−|>.
O código que tenho é:
# coding: iso-8859-1 -*-
import math
print('Programa que calcula a raiz quadrada')
print()
while True:
    numero=eval(input('Introduza um número positivo: '))
    if 0<numero:
        break
    else:
        print('O número introduzido tem de ser positivo')
        print()
Cont=0
Ri1=1
Ri=0
while abs(Ri1-Ri)>0.0001:
    if Cont==0:
        Ri=numero/2
        Cont=Cont+1
    else:
        Ri1=(Ri+numero/Ri)/2
        Ri=Ri1
        Cont=Cont+1
print(numero)
print(Cont)
print(Ri1)

Como ainda estou no inicio da programação só posso usar if e while.
Supostamente a Estimativa 1 = Ri=numero/2
Estimativa 2 = =(+ú/)/
Estimativa 3 = +=(+ú/)/
...
um dos resultados que deveria dar seria: Numero=2;Numero de Estimativas =5; Resultado=1.414214
Comment: Qual o erro que você está encontrando?
Comment: não me está a dar erro, mas o resultado está a dar errado
Answer: Você deve salvar a última aproximação antes de calcular a próxima:
[...]
    else:
        Ri1=(Ri+numero/Ri)/2
        Ri=Ri1
        Cont=Cont+1
[...]

Na segunda linha, o comando Ri=Ri1 faz os dois números serem iguais, ou seja, assim que sair do loop, a diferença entre eles vai ser 0.
Experimente inverter a ordem das linhas:
[...]
    else:
        Ri=Ri1
        Ri1=(Ri+numero/Ri)/2
        Cont=Cont+1
[...]

Rodando, encontramos o resultado desejado:
Programa que calcula a raiz quadrada

Introduza um número positivo: 81
81
8
9.000000000007091

Outro problema encontrado, é que seu chute inicial de Ri1 já é 1. Isto é, caso numero seja 2, a primeira aproximação vai ser 1 e vai sair do laço logo na primeira iteração. Uma dica, seria dar o valor inicial das variáveis correto desde o começo:
Cont=1
Ri1=numero/2
Ri = 0
while abs(Ri-Ri1)>0.0001:
    Ri=Ri1
    Ri1= (Ri1+numero/Ri1)/2
    Cont=Cont+1

Comment: Acabei de testar com o seu número introduzido (81) e o resultado dá o pretendido mas quando coloco numero=2 o resultado já dá errado visto que com numero=2 os resultados deveriam ser: numero=2, cont=5, Ri1=1.414214... e o resultado que me está a dar é numero=2, cont=1, Ri1=1
Comment: Sim. É um erro pois o primeiro chute é ```R = numero/2```. Como você deu o valor inicial da variável ```Ri1``` de 1, coincide com o primeiro chute e o while já retorna falso. Editei a minha resposta para incluir uma solução para isso também.

Python - Estimativa da raiz quadrada


How much do we know about how ancient Greek and Roman music sounded?

Question: Specifically for music composed earlier than the third or fourth century A.D.
I have heard several reports about deciphering examples of ancient greek musical notation. And you can find CDs of "reconstituted" ancient music although I never had an opportunity to hear one.

Have we been able to reconstruct ancient instruments of these times?
Do we know if some melody we still play come from ancient greek or roman folklore?
Comment: Interesting question. I believe the Ancient Greeks used a primitive form of notation which the Romans inherited. Not a lot of music has survived from either culture, though undoubtedly they did later influence classical music.
Comment: I talked about this a few days ago. We agreed they used hexachords and the modi, though not with absolute certainty.
Answer: There's actually quite a lot of surviving information on how the Greeks tuned their scales.  But as far as I know there's only one complete piece of music, the so-called "Seikilos Hymnus", a short epitaph:
https://en.wikipedia.org/wiki/Seikilos_epitaph
It's a very nice little song, and I've used it in concerts.
About Greek instruments- aside from pictures, there's not much: a few fragments of kithera tailpieces, some fragmentary auloi, and that's about it.  I've built two kitherae (the classical lyre) and have a third one underway, but how they are built is mostly guesswork.  Here's the last one I built:
http://i578.photobucket.com/albums/ss221/therealzilch/kithara.jpg
Comment: Yes an answer with actual real citations
Comment: @NeilMeyer- yes, if only to Wikipedia.  I didn't have time to look up more scholarly information, but the wiki on Seikilos seems pretty good.
Answer: To paraphrase and oversimplify 'Mastering the Scales and Arpeggios' by James Francis Cooke, the answer (for Greek) is, 'not very much.' There is a very large problem with ancient Greek writing on the subject. We don't really know what the words mean. Much ink has been spilled by writers purporting to know what the words mean, but in the end, there's perishing little evidence to support their theories.


Find $k$ in $\mathbb{K}=\mathbb{R},\mathbb{C},\mathbb{Z/11Z}$ such that this matrix is diagonalizable

Question: Find $k$ in $\mathbb{K}=\mathbb{R},\mathbb{C},\mathbb{Z/11Z}$  such that $$B:= \begin{bmatrix}
    1 & k & 1  \\
    0 & 1 & k+1 \\
    -1 & 0 & 2
\end{bmatrix}$$ is diagonalizable.
I tried taking the characteristic polynomial and I got $-t^{3}+4t^{2}-6{t}-k^{2}-k+3$ which is where I got stuck because I'm not sure how to proceed. Presumably I would want to find $k$ such that I can factor this polynomial on the fields that were given but I'm not sure how to go about that either. Any help would be appreciated.
Comment: sorry about that, I meant $k$, fixed it, thank you
Answer: To ease the computation a little, consider $C=B-I$ (so it has two more zero entries), which is diagonalizable iff $B$ is.
We start the calculation by looking for the minimal polynomial of $C$.  We have
$$
C=\begin{bmatrix}0&k&1\\0&0&k+1\\-1&0&1\end{bmatrix},
C^2=\begin{bmatrix}-1 & 0 & k^2 + k + 1\\
-k - 1 & 0 & k + 1\\
-1 & -k & 0\end{bmatrix}
$$
So for $k\neq 0$ we see by the middle column there are no nontrivial quadratics satisfied by $C$, hence the characteristic polynomial of $C$ is the minimal polynomial of $C$
$$
m_C(t)=\chi_C(t)=t^3-t^2+t+k(k+1)
$$
For $k=0$, if $C$ satisfies a nontrivial quadratic, the $(2,1)$-entry gives no quadratic term.  Since $C$ is not a scalar multiple of the identity, this is impossible.
So $m_C=\chi_C$ for all $k\in\mathbb{K}$.
Now remember $C$ is diagonalisable iff $m_C$ is a product of distinct linears, i.e., $C$ has three distinct eigenvalues.
Over $\mathbb{K}=\mathbb{R}$, note that $\chi_C'(t)=3t^2-2t+1>0$ for all $t$, so there can only be one simple eigenvalue, so $C$ is never diagonalisable.
I'll leave the details of $\mathbb{C}$ for you to tackle, and just remark that, except for finitely many $k$s the matrix $C$ has three distinct eigenvalues and hence diagonalisable.
Over $\mathbb{Z}/11\mathbb{Z}=\mathbb{F}_{11}$, the simplest is to list all possible values:
$$
\begin{array}{c|c}
\hline t & t^3-t^2+t\\\hline
 0& 0\\
 1& 1\\
 2& 6\\
 3&10\\
 4& 8\\
 5& 6\\
 6&10\\
 7& 4\\
 8& 5\\
 9& 8\\
10& 8\\\hline
\end{array}
$$
So the only way to get three distinct eigenvalues is $-k(k+1)=8$, but that has no solutions $k\in\mathbb{F}_{11}$.  So $C$ is never diagonalisable over $\mathbb{F}_{11}$.
Comment: I'm sorry for the late response. I'm afraid I'm a little underprepared for this question it seems. We haven't covered minimal polynomial and it doesn't seem like we will. If it's not too much trouble do you have a hint as to how would one go about this without knowing about minimal polynomial, with just the characteristic polynomial. Regardless, I will have go have a read about this, because it seems like a big gap in my linear algebra tool-kit, so thank you for your answer! I'll come back to read it when I patch this up.


Reading Avro into spark using spark-avro

Question: I'm not being able to read spark files using the spark-avro library. Here are the steps I took:

Got the jar from: http://mvnrepository.com/artifact/com.databricks/spark-avro_2.10/0.1
Invoked spark-shell using spark-shell  --jars avro/spark-avro_2.10-0.1.jar
Executed commands as given in the git readme:
import com.databricks.spark.avro._
import org.apache.spark.sql.SQLContext
val sqlContext = new SQLContext(sc)
val episodes = sqlContext.avroFile("episodes.avro")

The action sqlContext.avroFile("episodes.avro") fails with the following error:
scala> val episodes = sqlContext.avroFile("episodes.avro")
java.lang.IncompatibleClassChangeError: class com.databricks.spark.avro.AvroRelation has interface org.apache.spark.sql.sources.TableScan as super class
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)

Comment: Instead of pulling down the .jar file yourself you can just reference the package and the shell will pull down the package and any of its dependencies for you, using: spark-shell --packages com.databricks:spark-avro_2.10:1.1.0
Answer: My bad. The readme clearly says:
Versions

Spark changed how it reads / writes data in 1.4, so please use the correct version of this dedicated for your spark version

1.3 -> 1.0.0

1.4+ -> 1.1.0-SNAPSHOT

I used spark:1.3.1 and spark-avro: 1.1.0. When I used spark-avro: 1.0.0, it worked.
Answer: Since spark-avro module is external, there is no .avro API in DataFrameReader or DataFrameWriter.
To load/save data in Avro format, you need to specify the data source option format as avro.
Example:
val usersDF = spark.read.format("avro").load("examples/src/main/resources/users.avro")
usersDF.select("name", "favorite_color").write.format("avro").save("namesAndFavColors.avro")

Answer: import org.apache.spark.sql.SparkSession

val spark = SparkSession.builder()
            .appName(appName).master(master).getOrCreate()

val sqlContext = spark.sqlContext
val episodes = sqlContext.read.format("com.databricks.spark.avro")
                .option("header","true")
                .option("inferSchema","true")
                .load("episodes.avro")

episodes.show(10)


Quais aplicações Ruby tem além da web?

Question: Tenho vontade de me aprofundar em Ruby, pela facilidade da linguagem, assim como Python, que por exemplo, na minha Universidade utilizam muito para aplicações matemáticas e científicas.
Vejo muito a utilização de Ruby com Rails para Web, mas gostaria de saber se Ruby está sendo utilizado em outras áreas/aplicações.
Quais frameworks e bibliotecas que não são para web?
Realmente falou em Ruby, falou em web? Ou o cenário é diferente?
Comment: Eu só uso Ruby para criar programas desktop, com interface grafica e tudo.
Tipo se você baixar o Ruby e instalar, pode já começar a programar pra desktop. Ele executa no prompt de comando. Já pra criar sites, acho que você precisa fazer algo mais.
Answer: Qualquer linguagem de programação pode ser usada para qualquer atividade. É claro que algumas são melhores para um tipo de aplicação do que outras.
Existem algumas linguagens que optaram por ser de nicho mesmo e são muito boas nisso. Um exemplo é R que é adequada para estatísticas.
Mas a imensa maioria das linguagens optam por ser de uso geral por atingir um público maior. É claro que elas não costumam ser melhores que as outras em nada, são apenas razoavelmente boas em tudo, é o caso de Ruby.
O que muitas vezes determina um uso um pouco maior da linguagem não é a linguagem em si, mas as plataformas onde ela começa ser usada ou as bibliotecas existentes para ela.
Exemplos claros do primeiro caso são JavaScript, que foi usada em todos os navegadores e até padronizado, e Lua, que foi escolhida por muitos jogos como linguagem de personalização.
No segundo caso Ruby é o melhor exemplo onde a biblioteca determinou o sucesso e nicho da linguagem. Tem gente que acha que a linguagem se chama Ruby on Rails.
Mas a realidade é que Ruby pode ser usada para qualquer coisa que não exija performance ao extremo. Ruby não é conhecida por ser uma linguagem rápida. E por mais que surjam ferramentas para torná-la mais rápida, sempre haverá limitações, nunca poderá competir com linguagens de tipagem estática, só para ficar no exemplo mais óbvio.
Não sei como está o suporte de Ruby nos dispositivos móveis. Acredito que seja fraco ou inexistente. Isso pode mudar, mas duvido que ainda tenha espaço para Ruby ser forte nisto. Note que mesmo que suporte completamente será por responsabilidade de bibliotecas e não da linguagem.
Quer fazer aplicações GUI/Desktop? Ok. Ruby suporta através de bibliotecas existentes, tais como: Shoes, FXRuby, QtRuby, etc.
Quer mais? Pesquise em Ruby Toolbox e Ruby Gems. Tem literalmente milhares de bibliotecas, a maioria não é para web.
Coloquei no GitHub para referência futura.
Comment: Exatamente o que eu estava procurando, obrigado pela resposta :D
Comment: Só um ponto que faltou, ruby vem sendo muito utilizado para escrever testes, diversas ferramentas utilizam Ruby como linguagem, um exemplo seria o calabash que foi escrito em Ruby. Abraço.

Quais aplicações Ruby tem além da web?


What does it mean for an integer to be congruent to 1 $\pmod {n}$?

Question: This keeps coming up in Sylow proofs.
Eg: By Sylow's theorem, the number of subgroups of order 5 must divide 45, and must be congruent to 1 modulo 5; the only possibility is that there is a single group of order 5.
Subgroups of order 5 must divide 45 by Lagrange's theorem, and they need to divide the index of the subgroup in G by Sylow's theorems. That means, we can have 1 or 5 subgroups of order 5, no? So why 1? I assume its something im missing with the "congruent" part.
Comment: It means it's one more than a multiple of $5$ (i.e., it's $1$ or $6$ or $11$ or $16$ or $21$ or ...)
Comment: Integers $a,b$ are congruent mod $n$ if $a-b$ is divisible by $n$. Equivalently, this means that $a$ and $b$ leave the same remainder when we divide them by $n$. The notation is $a\equiv b$(mod $n$)
Comment: @TheSilverDoe Take a look at both the title and the question itself. Also, why would OP ask about how to prove the third Sylow theorem? The proof appears in any book about group theory.
Comment: @Mark Well, yeah. But I would be a little bit surprised to meet someone dealing with Sylow's theorems without knowing what a congruence is...
Comment: @TheSilverDoe Actually, this happens. You don't need to know a lot about mathematics in order to learn basic group theory. If OP learns it from some book then surely the group $\mathbb{Z_n}$ should have appeared as an example, but maybe they just didn't use the term "congruent".
Comment: I think the OP's question is rather "why must the number of subgroups of order $5$ be congruent to $1$ modulo $5$ ?" than "what does to be congruent to $1$ modulo $5$ mean ?".
Answer: Suppose the number of Sylow-$5$ subgroup is $n$. As $n$ is congruent to $1$ mod $5$ , it's should be of the form $5k+1$. Now as $5k+1$ must divide $45= 5×9$ i.e must divide $9$ ( as it's co prime to $5$) , the only possibility of $k$ is $0$, hence therefore $n$ has only the value $1$.
Comment: Welcome to the forum. Please use MathJax when you write here. Here is a tutorial: https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference
Comment: good scoring Sweta!
Answer: Observe that the divisors of $45$ are $1$, $3$, $5$, $9$ and $15$, but $5\nmid (3-1)$, $5\nmid (5-1)$, $5\nmid (9-1)$ and $5\nmid (15-1)$.


How many option are there to divide n people into any number of groups of any size?

Question: I found only something like this:
$$f(0) = 1$$
$$
f(n) = \sum_{i=0}^{n-1} \binom{n-1}{i} * f(i)
$$
Now I wonder if there are other (faster) ways to calculate this.
Answer: This is called the number of partitions of a set, or Bell numbers.


Classification methods for univariate time series

Question: Our team wants to develop a machine learning algorithm for classification of univariate data. Our data is a live feed from a position sensor placed in an injection molding machine. We want to be able to clearly identify the number of cycles this machine makes. The data has a timestamp (every 100 ms approximately) and a 'position' value. We are acquiring the data and processing it with a Raspberry pi 4, so we hoped we could perform all processing on it. In the data series we have primarily two values/positions:

High value: When the injection molding machine is using
its clamping force to close the mold.
Low value: When the mold is opened.

Relative to the interactions of these values, we will have different events. When it goes up a certain time and then down, there will be a cycle. When it stays either up or down for a longer time means that the machine was stopped. When it goes up and down in a short time could mean that the operator of the machine was performing a dry cycle (no injection, just moving the plates of the machine, for example).
I know this simple problem could be solved via a heuristic approach, but we want to be able to develop a method for this in order to learn and put our hands in more complex models. And also, our current heuristic approach fails when there are unidentified cycle movements, so we hoped to make it more robust with better algorithms.
What we've found so far
My research has lead me into thinking LSTMs with sliding windows could help me perform this task:

I could perform a cluster operation on a static window in order to know how long the window size (e.g. number of timestamps to take into account) should be (for example, creating clusters between position data and time elapsed in this position). This could be made once in a while to adjust the window in case someone changes the mold in the machine (this situation would result in a different cycle time because it would be a different molded product).
With the time window known, I could use a sliding window to perform a classification between cycle/no cycle/idle and be able to count this in order to know how many cycles where performed in certain time.

The previous approach is just a possible example of what i believe we could do. I have encountered several possible methods but RNNs and LSTMs are a frequent subject in classification for time series. Maybe this is not the best approach, maybe it could be a good one. That's our doubt and will be our starting point in this learning (human learning!) process. Therefore, today i'm coming to you because i believe i could get your opinions in terms of:

Do you think this sort of algorithm would work properly for our application?
Do you think there might be a simpler approach which we could use?
Relative to the approach (either the one proposed or another one you think would suit better this particular application), do you think a raspberry pi 4 could be enough to perform these classifications in real time?

I have plenty of sample data which i could use for training and labeling every event in different stages of the process. Every opinion of yours is greatly appreciated.
Thank you!
Comment: Excellent, I will follow your advice. Thank you again!
Comment: RNN/LSTM is the obvious first idea. Another is to do a wavelet transform (or similar) of the time series to make it photo-like and run a convolutional neural network. Brandon Rohrer mentions this in his excellent video on CNNs: https://youtube.com/watch?v=FmpDIaiMIeA&t=1321s. This CNN approach can be combined with RNN/LSTM, too.
Comment: Thanks for your answer Dave. That's an interesting approach, I thought about it but I had some doubts whether it could be heavy to run on a raspberry. Another one of my doubts was how to slice the real time data so that I could build these images from similar time/position behaviors. If there are events that could take a longer time than others, that could start at different times relative to the chosen analysis window, how could one isolate this effect? I believe from this approach a fixed size window could be an issue...
Comment: Unequal data size presents a problem, and I'm pretty sure you don't avoid it by doing straight RNN/LSTM instead of CNN-LSTM. Since you have a live feed, I assume you'll only be looking at the past X-many seconds. Perhaps start by training your models on some restricted amount of data (5 seconds, 30 minutes, whatever seems to matter). You can figure out what the correct duration is empirically and with some subject matter expertise. Then your live feed can give that duration to a trained model to make the predictions.
Answer: (From the comments)
RNN/LSTM is the obvious first idea. Another is to do a wavelet transform (or similar) of the time series to make it photo-like and run a convolutional neural network. Brandon Rohrer mentions this in his excellent video on CNNs. This CNN approach can be combined with RNN/LSTM, too.
Unequal data size presents a problem, and I'm pretty sure you don't avoid it by doing straight RNN/LSTM instead of CNN-LSTM. Since you have a live feed, I assume you'll only be looking at the past X-many seconds. Perhaps start by training your models on some restricted amount of data (5 seconds, 30 minutes, whatever seems to matter). You can figure out what the correct duration is empirically and with some subject matter expertise. Then your live feed can give that duration to a trained model to make the predictions.


Alguém pode me explicar a lógica de funcionamento desse padrão

Question: Eu estou escrevendo um compilador Scheme R6RS(trabalho escolar). Ele está funcionando perfeitamente com exceção desse padrão proposto no próprio manual em 11.19 - Macro Transformes: sintaxe-rules.
Eu já li e reli e não consigo entender como que esse padrão de elipses(... ...) pode retornar o elemento 4 da lista (1 2 3 4 )?
Código que não consigo entender o funcionamento:
(define-syntax be-like-begin
  (syntax-rules ()

    ((be-like-begin name)

     (define-syntax name

       (syntax-rules ()

         ((name expr (... ...))

          (begin expr (... ...))))))))

(be-like-begin sequence)

(display (sequence 1 2 3 4))

O resultado é:
4

Alguém me por favor me explique o por que desse resultado. É o que me falta para fechar um capitulo da minha vida.
Comment: Outra ótima pergunta para https://cs.stackexchange.com
Comment: Eu já testei em alguns compiladores e uns não reconhecem esse padrão, mas os que reconhece o resultado é sempre 4.
Comment: Quem negativou poderia por favor explicar o motivo do porque essa pergunta é ruim pois essa questão é relativa ao minha monografia onde escrevi um compilador da [linguagem SCHEME R6RS](http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-2.html#node_toc_start) em C# e esse padrão de expansão é único que não consegui compreender e é o que me impede concluir o compilador e aqui está o [link na documentação oficial para esse padrão de expansão](http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html#node_sec_11.19)
Answer: A questão está no begin expr:
begin expr (... ...)
As expressões no seu exemplo, no caso os números (1, 2, 3 e 4) são avaliados sequencialmente da esquerda para a direita, e no caso o resultado é o próprio numero em si (... ...)=template(ellipsis ellipsis), sendo que todos são ignorados com a exceção do último.
Você pode alterar sua sequencia como for, o resultado sempre será o último numero.
Comment: Mas a questão não é o exp mas sim o begin, veja: "Scheme begin expressions aren't just code blocks, though, because they are expressions that return a value. A begin returns the value of the last expression in the sequence." https://www.cs.utexas.edu/ftp/garbage/cs345/schintro-v14/schintro_18.html#:~:text=Scheme%20begin%20expressions%20aren't,like%20begin%20s%20as%20well.
Comment: Por exemplo se testar esse código no [Racket](https://www.jdoodle.com/execute-racket-online/) funciona sem problemas mas se testar no [BiwaScheme](https://repl.it/@RonaldoVasques/DeepskyblueUnsungTaskscheduling#main.scm) vai dar erro porque o autor do compilador está com o mesmo problema que eu, que é não compreender a lógica desse padrão de transformador de macro.
Comment: Tranquilo. Talvez fosse interessante então você explicar o que acontece quando tenta compilar esse código no seu compilador. Se nem chega a compilar ou se a execução é que fica errada.
Comment: Eu não quero saber o exp faz eu quero saber o BNF para montar a arvore de sintaxe abstrata que compreenda esse padrão. E exemplo mostra exp mas pode ser qualquer outra função em seu lugar.Veja http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-2.html#node_toc_node_sec_11.19
Comment: Isso não tem nada a ver. Estou escrevendo um compilador eu preciso do [BNF](https://pt.wikipedia.org/wiki/Formalismo_de_Backus-Naur) para usar no [Bison](https://pt.wikipedia.org/wiki/GNU_bison) de forma que eu possa gerar uma [AST](https://pt.wikipedia.org/wiki/%C3%81rvore_sint%C3%A1tica_abstrata) para compilar o código que o usuário digitar. Se o usuário digitar esse transformador meu compilador não entende. Ele entende toda a linguagem menos esse padrão de expansão de macro.
Comment: Essa que citou é definida aqui http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-2.html#node_toc_node_sec_11.4.7 foi um dos primeiro não terminais que fiz.
Comment: Não é que eu seja chato ou implicante. As vezes estou escrevendo outra resposta e não a dou a devida atenção. Obrigado por se interessar pelo meu problema.
Comment: Não cheguei a criar os símbolos não terminais para essa produção, [veja o parser](https://justpaste.it/306n1), pois não faz sentido. Todos os elementos de linguagem estão funcionais menos essa produção o que torna o meu compilador em um compilador de brinquedo pois não cria macros higiênicos.
Comment: Explicando porque sua resposta não responde a pergunta, onde se encontra os "..." ele pode ser expandido na forma `( ...) ou
(  ... . ) ou
( ...    ...) ou
( ...    ... . ) ou
#( ...) ou
#( ...    ...)`  mas no manual diz que é uma abstração sintática mas não há uma explicação pontual do que. O que deixou perdido, eu cheguei sim  a fazer uma tentativa mas os resultados davam valores divergentes do esperado.


How to fit logistic regression to circular data?

Question: I've made a script that can do normal logistic regression with sigmoid(linear model). However, I have data that has a circular decision boundary and looks like this.

My question is how I can modify my script handle these data? I am thinking of including the circular equation, but I am not sure how.
Comment: Convert to polar co-ordinates (https://en.wikipedia.org/wiki/Polar_coordinate_system)
Comment: @DikranMarsupial I think a better option (making fewer assumptions) is to include both linear and quadratic effects of $x_1$ and $x_2$ as well as their interaction (unless it is known a priori that the region is perfectly circular rather than elliptic and centered at the origin).  But it all depends on what fits the data and what is known a priori about the data generating process.
Comment: It looks to me if $x_0^2 + x_1^2 \leq 1$ then $y=1$ and otherwise $y=0$. Could you use this as a feature/decision criteria? I.e. distance of $(x_0, x_1)$ from the origin.
Comment: @DikranMarsupial That looks a lot like an answer!
Comment: @DikranMarsupial Do you want to turn your comment into an answer? If not, I may try.
Comment: @JarleTufto when I am teaching ML, I often use a similar example to motivate radial basis function neural networks and kernel learning methods, so I would probably go that route (with regularisation) to make a general non-linear version.  However the nice thing about 2D data is we can just look at the data and use intuition to find a sensible transformation.
Comment: A worked example for this kind of problem (arising from a different question) is presented in https://stats.stackexchange.com/questions/164048/can-a-random-forest-be-used-for-feature-selection-in-multiple-linear-regression/164068#164068
Comment: @PeterFlom please do go ahead!
Answer: A few ideas.
When I first read this question, I thought "just measure distance from the center." Then I read the comments. Dikran Marsupial suggested converting to polar coordinates, which is quite similar to what I had thought of.
In another comment, Sycorax referenced a thread where he suggests using random forests for a similar problem.
Sycorax's example is very well worked out, so I won't say more about that here. But I will modify a bit of his code code to create data.
Let's try out mine and Dikran's.  First, I'll make some data that looks more or less like what the question has (I increased the sample size and added some noise):
set.seed(1234)

N  <- 1000
x1 <- rnorm(N, sd=1.5)
x2 <- rnorm(N, sd=1.5)

y  <- apply(cbind(x1, x2), 1, function(x) (x%*%x)<1)  + rnorm(1000, 0, .1)

plot(x1, x2, col=ifelse(abs(y) < 0.8, "red", "blue"))

This produces

which seems close.  Now, just to check, let's try regression on this:
m1 <- lm(y~x1+x2)
summary(m1)

and, as expected, the parameter estimates are very close to 0 and not close to significant. So, let's find the  polar coordinates:
r <- sqrt(x1^2 + x2^2)
theta <- atan2(x2, x1)

and regress on r and $\theta$ (I don't expect $\theta$ to be useful).  Lo and behold, it works:
Residuals:
     Min       1Q   Median       3Q      Max
-0.59991 -0.26826 -0.07653  0.27481  0.99404

Coefficients:
              Estimate Std. Error t value Pr(>|t|)
(Intercept)  0.6992665  0.0222995  31.358   <2e-16 ***
r           -0.2606233  0.0106316 -24.514   <2e-16 ***
theta       -0.0003919  0.0057505  -0.068    0.946
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.3344 on 997 degrees of freedom
Multiple R-squared:  0.3762,    Adjusted R-squared:  0.3749
F-statistic: 300.6 on 2 and 997 DF,  p-value: < 2.2e-16

Of course, you could try different distance measures or modify things other ways.
Comment: Although the plot in the question is suggestive, the question itself does not state that the origin is known.  Shouldn't that be a parameter to estimate as well?
Comment: Well, I guess I took the "suggestion" in the plot seriously. You could center the data or you could center them.  That would be more general.
Comment: It would be incorrect, though, because it would use your "center" -- however you compute it -- as an estimate.  Moreover, you wouldn't be able to determine an appropriate standard error for that estimate.


Permission denied using mysql in Mac Terminal

Question: When attempting to do anything with mysql I am met with a

permission denied

For example
<code>adamshaw$ mysql> -u root -p
-bash: -u: Permission denied

(base) Adams-MacBook-Pro:mysql adamshaw$ mysql> --skip-grant-tables
-bash: --skip-grant-tables: Permission denied
</code>
How do I grant myself permission to my own mysql?
Comment: Can you explain a bit more about why you believe `mysql>` with the ending `>` is a valid command? More often than not, `mysql` is the command you'd be looking for, unless you have some aliasing set up. In the context of a `bash` prompt, the `>` token indicates that the output of the preceding command should be redirected to a file.
Comment: I get a 'command not found' when I don't use '>'
Comment: That means that either the mysql client is not installed, or the path to the executable is not in the default paths in your terminal. Try changing to the directory where the mysql executable is installed and then run it again (without the >)
Answer: Try adding chmod 755 to the beginning of the code line.
This helped me with trying to run python scripts via terminal.
May work for MySQL too.


MySQL Left Outer Join Validation?

Question: I have a table listing locations and the acceptable fields for each location:
Location                       Acceptable Fields
----------------------------------------------------------|
Location_A                       Address
Location_A                       Phone
Location_A                       Hours     Location_B                       Address
Location_B                      Phone
Now, I have a table listing the values that I want to validate against the first table.
ID     Location       Address                   Phone               Hours
----------------------------------------------------------------------|
1     Location A     123 Some St.     9999999     8-5
2     Location B     456 Some St.     9999998      8-4   Now, Location A passes the Hours validation, however, Location B has Hours listed but according to the validation table, it shouldn't be. This should be NULL. I want my query to return Location B, 8-4. Here my MySQL statement that is obviously not working:
SELECT Table2.Location, Table2.Hours
FROM
    Table1 LEFT OUTER JOIN Table2
ON
    Table1.Location = Table2.Location

WHERE
    Table2.Hours is not null
    AND Table1.Acceptable_Fields != 'Hours';

Comment: let us know .. what output you need exactly
Comment: are the LOCATION values in table1 and table2 listed as shown in your example, I.e. Location_A vs Location A?
Comment: Hi - 1. that was a typo, both are Location_A, Location_B (in both tables)
Comment: The output I'm trying to get is: Location_B  8-4
Answer: How about this?
SELECT Table1.Location, Table1.Acceptable_Fields
FROM Table1 RIGHT JOIN
    (
        SELECT Location, 'Address' AS col_name, Address AS col_val
        FROM Table2

        UNION ALL

        SELECT Location, "Phone" AS col_name, Phone AS col_val
        FROM Table2

        UNION ALL

        SELECT Location, "Hours" AS col_name, Hours AS col_val
        FROM Table2
    ) T3 ON Table1.Location = Table3.Location
      AND Table3.col_name = Table1.Acceptable_Fields
WHERE Table3.col_name IS NULL AND TAble3.col_val IS NOT NULL;

Answer: There are two ways you can achieve this:
Using LEFT JOIN. You will need to left join your control table three times (once for each of the control parameters (i.e. Acceptable Fields) and check for NULLs, like this:
SELECT t2.ID,
       t2.Location,
       CASE WHEN a.Acceptable_Fields IS NOT NULL THEN t2.Address END Address,
       CASE WHEN p.Acceptable_Fields IS NOT NULL THEN t2.Phone END Phone,
       CASE WHEN h.Acceptable_Fields IS NOT NULL THEN t2.Hours END Hours
FROM
Table2 t2
LEFT OUTER JOIN Table1 a ON
t2.Location = a.Location AND a.Acceptable_Fields = 'Address'
LEFT OUTER JOIN Table1 p ON
t2.Location = p.Location AND p.Acceptable_Fields = 'Phone'
LEFT OUTER JOIN Table1 h ON
t2.Location = h.Location AND h.Acceptable_Fields = 'Hours'

Using a sub-queries:
SELECT t2.ID,
       t2.Location,
       CASE WHEN EXISTS(SELECT 1 FROM Table1 WHERE Location = t2.Location AND Acceptable_Fields = 'Address') THEN t2.Address END Address,
       CASE WHEN EXISTS(SELECT 1 FROM Table1 WHERE Location = t2.Location AND Acceptable_Fields = 'Phone') THEN t2.Phone END Phone,
       CASE WHEN EXISTS(SELECT 1 FROM Table1 WHERE Location = t2.Location AND Acceptable_Fields = 'Hours') THEN t2.Hours END Hours
FROM
Table2 t2

MySQL Left Outer Join Validation?


Guzzle Request : Post body data

Question: I'm trying to POST data to a remote AWS API.
The data should be a JSON on the body part.
Using Postman, I can send the data and everything works correctly :

Now, trying to do so using GuzzleHttp\Psr7\Request, I'm doing :
$request = new \GuzzleHttp\Psr7\Request(
            'POST',
            'AWS API URL',
            ['Host' => 'AWS HOST', 'body' => '{"json": "my JSON"}']
        );
$request = $signer->signRequest($request, $credentials);
$response = $client->send($request);

The request succeed, but there is no data update ! As if the 'body' is not received.
I have no access to the remote API log files.
So my question, is this the correct way to post data in body part of a Guzzle request ?
Thanks.
Comment: Does this answer your question? [How can I use Guzzle to send a POST request in JSON?](https://stackoverflow.com/questions/22244738/how-can-i-use-guzzle-to-send-a-post-request-in-json)
Comment: @Jeto, I've edited my question, the `request` should be signed. So I should provide the data on the request object, not on the `client->send()`. At least, that's my analyses.
Answer: As per the linked answer, you need to pass the following options along with your request:
[GuzzleHttp\RequestOptions::JSON => ['key1' => 'value1', 'key2' => 'val2']]

or:
['json' => ['key1' => 'value1', 'key2' => 'val2']]

But since you need to build your Request object first, you should be able to pass this option as the second parameter of Client::send:
$response = $client->send($request, [
  GuzzleHttp\RequestOptions::JSON => ['key1' => 'value1', 'key2' => 'val2']
];

Guzzle Request : Post body data


Distinguishing called URL for response rewriting for tomcat app via IIS ARR reverse proxy

Question: Hi to the community at Serverfault!
I have an issue while trying to configure my IIS to open the respective manager APP on two Tomcat Servers on the same machine.
When I call the sites directly via http://servername:8081/manager and http://servername:8080/manager (the other Tomcat) respectively, everything is working fine.
The nightmare begins, when I try to redirect the request to http://servername/manager7 to the first Tomcat and http://servername/manager8 to the second server. The links in the HTML Resonse then always contain /manager7/... even if I call the URL http://servername/manager8.
Below are my rewriting rules. I set them up at server level.
<rewrite>
        <globalRules>
            <clear />
            <rule name="Tomcat7" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                <match url="*7*" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                <action type="Rewrite" url="http://wwwts02:8081/{R:1}{R:2}" />
            </rule>
            <rule name="Tomcat8" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                <match url="*8*" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                <action type="Rewrite" url="http://wwwts02:8080/{R:1}{R:2}" />
            </rule>
        </globalRules>
        <outboundRules>
            <clear />
            <rule name="Tomcat8" preCondition="Recognize Tomcat8" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                <match filterByTags="A, Form, Img, Link, Script" pattern="/manager*" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
                </conditions>
                <action type="Rewrite" value="/manager8{R:1}" />
            </rule>
            <rule name="Tomcat7" preCondition="Recognize Tomcat7" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                <match filterByTags="A, Form, Img, Link, Script" pattern="/manager*" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
                </conditions>
                <action type="Rewrite" value="/manager7{R:1}" />
            </rule>
            <preConditions>
                <preCondition name="Recognize Tomcat8">
                    <add input="{REQUEST_URI}" pattern="(.)*/(.*)8/(.*)" />
                </preCondition>
                <preCondition name="Recognize Tomcat7">
                    <add input="{REQUEST_URI}" pattern="(.*)/(.*)7(.*)" />
                </preCondition>
            </preConditions>
        </outboundRules>
    </rewrite>

Can anyone think of other options I have?
I also tried the ISAPIRedirect.dll offered by Microsoft, but it seems it doesn't support the rewriting of the HTTP responses at all.
Please direct me, what to try next.
Any help will be greatly appreciated and feel free to ask for further information you need to answer.
Sorry for my probably bad English, as I am German.
Answer: The pattern match *7* is invalid. It says <nothing><any times> followed by <7><any times>, to be more clear: it matches all things because there are 0 or more 7 characters included.
You may want to match sth. like (.*7.*) -- the brackets for allowing referencing with {R:1}.


R: Frequency Table on Unique Rows in a Dataframe

Question: I have a dataframe that looks like the following, where many are redundant rows. I'd like to collapse the dataframe so that there are only unique rows, but there is another variable weight that shows the number of times a unique row showed up.
A snippet of my dataframe:
from    to
6140690 6141500
6140430 6140428
6141450 6140428
6141450 6140428
6140430 6141450
6140430 6141450

Desired output:
from    to      weight
6140690 6141500  1
6140430 6140428  1
6141450 6140428  2
6140430 6141450  2

Answer: Here's an R base approach
 merge(unique(df), data.frame(table(df)))
     from      to Freq
1 6140430 6140428    1
2 6140430 6141450    2
3 6140690 6141500    1
4 6141450 6140428    2

Here's a dplyr approach using count:
  count(df, from, to, name="weight")
         from      to weight
    1 6140430 6140428      1
    2 6140430 6141450      2
    3 6140690 6141500      1
    4 6141450 6140428      2

Comment: This approach only counts the number of times "from" is unique. I've updated the example dataset a little bit, but ideally I would prefer to tabulate the times "from" AND "to" are unique
Comment: @Itong I edited my answer using your updated data.


Upload image programmatically in user account

Question: I used the below code to upload default picture field. Other than this default picture field, I have another field to upload image (for Example, field name "field_house_image"). How to upload file Programmatically.
$tmp_photo_realpath = "images/".$userimgname;
        $tmp_photo = "images/".$userimgname;
          $file = new stdClass();
          $file->uid      = $uid;
          $file->status   = 0; // mark the file as temporary
          $file->filename = basename($tmp_photo_realpath);
          $file->uri      = $tmp_photo;
         // $file->filemime = image_type_to_mime_type(exif_imagetype($tmp_photo_realpath));
          $file->filesize = filesize($tmp_photo_realpath);
         $infofile = explode("/",$tmp_photo);
        $infoext = explode(".",end($infofile));
        $imgext = end($infoext);

      // The file should be an image
      $errors = array();
      $errors += file_validate_is_image($file);
      $errors += file_validate_image_resolution($file, variable_get('user_picture_dimensions', '100x100'));
      $errors += file_validate_size($file, variable_get('user_picture_file_size', '30') * 1024);

      // Make sure file extension is a valid image
      if (!in_array(strtolower($imgext), array('jpg', 'png', 'gif'))) {
        $errors[] = ' invalid image file extension.';
      }

      if (count($errors)) {
        drupal_set_message(t('Profile Image Import:') . ' ' . $errors[0], 'warning');
        // Clean up (set fid to avoid error messages)
        $file->fid = 0; file_delete($file);
      }
      else {
        // We'll need a valid file id on the file object; file_save() will give us one
        $file = file_save($file);
        // Update user account (fid is not empty, status is temporary -- image
        // will be moved to proper directory and assigned to the user)
        $fields['picture'] = $file;
        $account = user_save($account, $fields);
    }

EntityDrupalWrapper Object
(
    [id:protected] => 26679
    [bundle:protected] =>
    [entityInfo:protected] => Array
        (
            [path] => user/%user
            [metatags] => 1
            [label] => User
            [controller class] => UserController
            [base table] => users
            [uri callback] => user_uri
            [label callback] => format_username
            [fieldable] => 1
            [entity keys] => Array
                (
                    [id] => uid
                    [revision] =>
                    [bundle] =>
                )
        [bundles] => Array
            (
                [user] => Array
                    (
                        [label] => User
                        [admin] => Array
                            (
                                [path] => admin/config/people/accounts
                                [access arguments] => Array
                                    (
                                        [0] => administer users
                                    )

                            )

                        [rdf_mapping] => Array
                            (
                                [rdftype] => Array
                                    (
                                        [0] => sioc:UserAccount
                                    )

                                [name] => Array
                                    (
                                        [predicates] => Array
                                            (
                                                [0] => foaf:name
                                            )

                                    )

                                [homepage] => Array
                                    (
                                        [predicates] => Array
                                            (
                                                [0] => foaf:page
                                            )

                                        [type] => rel
                                    )

                            )

                    )

            )

        [view modes] => Array
            (
                [full] => Array
                    (
                        [label] => Doctor Login
                        [custom settings] =>
                    )

                [token] => Array
                    (
                        [label] => Tokens
                        [custom settings] =>
                    )

            )

        [static cache] => 1
        [field cache] => 1
        [load hook] => user_load
        [translation] => Array
            (
            )

        [schema_fields_sql] => Array
            (
                [base table] => Array
                    (
                        [0] => uid
                        [1] => name
                        [2] => pass
                        [3] => mail
                        [4] => theme
                        [5] => signature
                        [6] => signature_format
                        [7] => created
                        [8] => access
                        [9] => login
                        [10] => status
                        [11] => timezone
                        [12] => language
                        [13] => picture
                        [14] => init
                        [15] => data
                    )

            )

        [token type] => user
        [bundle label] => User
        [xmlsitemap] => Array
            (
                [process callback] => xmlsitemap_user_xmlsitemap_process_user_links
            )

        [plural label] => Users
        [description] => Users who have created accounts on your site.
        [access callback] => entity_metadata_user_access
        [creation callback] => entity_metadata_create_object
        [save callback] => entity_metadata_user_save
        [deletion callback] => user_delete
        [form callback] => entity_metadata_form_user
        [view callback] => entity_metadata_view_single
        [configuration] =>
    )

[propertyInfo:protected] => Array
    (
        [bundles] => Array
            (
                [user] => Array
                    (
                        [properties] => Array
                            (
                                [field_enterprise_blog_firstname] => Array
                                    (
                                        [label] => First Name
                                        [type] => text
                                        [description] => Field "field_enterprise_blog_firstname".
                                        [getter callback] => entity_metadata_field_property_get
                                        [setter callback] => entity_metadata_field_property_set
                                        [access callback] => entity_metadata_field_access_callback
                                        [query callback] => entity_metadata_field_query
                                        [translatable] =>
                                        [field] => 1
                                        [required] => 1
                                    )

                                [field_enterprise_blog_lastname] => Array
                                    (
                                        [label] => Last Name
                                        [type] => text
                                        [description] => Field "field_enterprise_blog_lastname".
                                        [getter callback] => entity_metadata_field_property_get
                                        [setter callback] => entity_metadata_field_property_set
                                        [access callback] => entity_metadata_field_access_callback
                                        [query callback] => entity_metadata_field_query
                                        [translatable] =>
                                        [field] => 1
                                        [required] =>
                                    )

                                [field_enterprise_blog_picture] => Array
                                    (
                                        [label] => Picture
                                        [type] => field_item_image
                                        [description] => Field "field_enterprise_blog_picture".
                                        [getter callback] => entity_metadata_field_verbatim_get
                                        [setter callback] => entity_metadata_field_verbatim_set
                                        [access callback] => entity_metadata_field_access_callback
                                        [translatable] =>
                                        [field] => 1
                                        [required] =>
                                        [auto creation] => entity_metadata_field_file_create_item
                                        [validation callback] => entity_metadata_field_file_validate_item
                                        [property info] => Array
                                            (
                                                [file] => Array
                                                    (
                                                        [type] => file
                                                        [label] => The image file.
                                                        [getter callback] => entity_metadata_field_file_get
                                                        [setter callback] => entity_metadata_field_file_set
                                                        [required] => 1
                                                    )

                                            )

                                    )

Answer: I'm doing that using entity_metadata_wrapper since it's the easiest way. You should have Entity API installed to use this function. A working example from one of my projects (I've changed the variables to fit your question):
$user_wrapper = entity_metadata_wrapper('user', $uid);

$tmp_photo_realpath = "images/" . $userimgname;
$contents = @file_get_contents($tmp_photo_realpath);
$uri = 'public://images/user/' . $userimgname;

$file = file_save_data($contents, $uri);
$file_info = array(
  'fid' => $file->fid,
  'filename' => $file->filename,
  'filemime' => $file->filemime,
  'uid' => 1,
  'uri' => $file->uri,
  'status' => 1,
  'display' => 1,
);

$user_wrapper->field_house_image->set(array($file_info));
$user_wrapper->save();

If your field field_house_image is a multi-value field, you can use a loop like (suppose you have an array of multiple images named $file_infos):
foreach ($file_infos as $key => $file_info) {
  $user_wrapper->field_house_image[$key]->set($file_info);
}
$user_wrapper->save();

Comment: I am getting page error in this line :
$user_wrapper->field_enterprise_blog_picture->set($file_info);
Comment: Is the field_enterprise_blog_picture a multi-value field?
Comment: No. It's only one value field. Below ... I 'll send $user_wrapper array.
Comment: Ok, it seems you need to set $key no matter if the field is multi-value or not. I'll update my answer, and please try it with a key.
Comment: I kept key value .. showing this error.
Fatal error: Cannot use object of type EntityStructureWrapper as array in .....test.php on line 354.

Plz once check the edited question, which I have printed $user_wrapper array.
Comment: I've updated my answer again. I've removed the key and wrapped the $file_info in an array in set() function. I also added $user_wrapper->save(), which at first I forgot to add.
Comment: Getting page error. sorry... my question is wrong.. field_enterprise_blog_picture is default picture field of drupal 7, here machine_name was changed, but my above code is not working in this site. In other site it is worked well. $file = file_save($file); is not working here..


Is the Grassmannian bundle projective?

Question: Let $X$ be a smooth projective complex variety and $E$ a complex vector bundle of rank $n$ on $X$. Write $\mathbb G(k,E)$ for the fiber bundle whose fibers are the Grassmannians $\mathbb G(k,E_x)$ where $E_x$ is a fiber of $E$ at $x$. I managed to show that $\mathbb G(k,E)$ is a smooth complete variety ; to show properness it suffices to show that the morphism $\mathbb G(k,E) \to X$ is proper since $X$ is projective, and since properness is local on the base it suffices to check on a trivializing neighborhood $U$ for $E$, so that
$$
\mathbb G(k,E|_U) \simeq U \times \mathbb G(k, \mathbb C^n)
$$ is an isomorphism of $U$-schemes and $U \times \mathbb G(k, \mathbb C^n) \to U$ is proper since $\mathbb G(k, \mathbb C^n)$ is projective and this is a base extension.
How do I show that $\mathbb G(k,E)$ is projective? It seems to be assumed in a paper I am reading and I didn't believe it at first because I thought that the twists in the bundle $E$ could prevent the existence of an embedding (i.e. the existence of a very ample invertible sheaf). Any ideas?
Comment: Letting $\pi:G\to X$ to be the Grassmannian, it comes equipped with an exact sequence $0\to S\to \pi^*E\to Q\to 0$, where $Q$ is a rank $k$ vector bundle on $G$. Then, one checks that $\pi^*L\otimes \Lambda^k Q$ is an ample bundle on $G$, where $L$ is any ample line bundle on $X$.
Comment: @Mohan : Sorry, but can you elaborate? I have no idea how to derive any of your claims. How do you construct $S$ and $Q$? How does "one check" your last claim? Still, your comment makes me happy because someone believes the Grassmannian bundle is indeed projective! Feel free to elaborate into an answer.
Comment: @Mohan : Is it possible that $S$ is the tautological bundle and $Q$ has rank $n-k$, not $k$? So that I take $\wedge^{n-k}Q$ instead? I don't know how to check ampleness though... I don't know if the results in Hartshorne suffice.
Comment: Grothendieck told us to take $Q$ to be the tautological quotient bundle, its behaviour is superior than taking subbundles. Here is a general fact: Let $f:S\to T$ be a projective morphism, $L$ ample on $T$ and $M$ a relatively ample line bundle on $S$. Then $f^*L\otimes M$ is ample on $S$.
Comment: @Mohan : Good thing he told you, 'cause he didn't tell me yet! (I didn't read much EGA so far). Do you mean that the morphism $\pi : G \to X$ is projective? If I knew this, I wouldn't have to deal with finding an ample sheaf... where would you look up the fact that $\pi^* L \otimes \Lambda^k Q$ is ample?
Comment: EGA of course. Just kidding. Any book which deals with relative Grassmannian should do these, especially, if they do the universal property of Grassmannian, rest is quite easy.
Comment: @Mohan : I'm one of these persons that doesn't mind reading EGA. Thanks =)
Answer: Patrick,
May be without checking all the details, here is how it is done. I always think of rank $k$ quotients. Then, $G(k,E)$ and $G(k,E\otimes L)$ are naturally isomorphic for any line bundle $L$ on $X$. So, we may twist $E$ sufficiently and assume that $E$ is globally generated and so we have a surjection, $F=\mathcal{O}_X^N\to E$. From this, any rank $k$ quotient of $E$ is a rank $k$ quotient of $F$ and thus we get a closed embedding $G(k,E)\subset G(k,F)$. But, $G(k,F)=X\times G(k,N)$ and thus it is projective, if you knew that the usual Grassmannian (over a field) $G(k,N)$ is.
Comment: Thanks! Very useful answer. +1 :)

Is the Grassmannian bundle projective?


Why is $P(\min\{X_1,...,X_n\} ≥ y)=P(X_1≥y,..., X_n≥y)$?

Question: Why is $$P(\min\{X_1,...,X_n\} ≥ y)=P(X_1≥y,..., X_n≥y)$$
and similarly
$$P(\max\{X_1,...,X_n\}≤y)=P(X_1≤y, ..., X_n≤y)$$
I.e. why are $\min$ and $\max$ equivalent to AND (since $P(X_1≥y, X_2≥y)$ means $P(X_1≥y \text{ AND } X_2≥y)$ or $P(X_1≥y \cap X_2≥y)$) probabilities of all the r.v.s?
Comment: If the smallest value is bigger than $y$, then *all* the values are bigger than $y$.
Comment: To whom it may concern: This question is clearly on-topic here because it is about probability. I vote to keep it open.
Comment: Similarly if the largest value is less than y then all values are less than y.
Comment: It perhaps worth adding that comma-separated events are considered to be connected with "and".
Answer: Because the events inside each $P(\cdot)$ are equivalent in both cases.  To see this, let $X_{j} = \min\{X_{1}, \ldots , X_{n}\}$.  Then if $X_{j} \geq y$, then it must also be that $X_{i} \geq X_{j} \geq y$ for all $i \in \{1,\ldots, n\}, i \neq j$.  A similar argument can be used in the $\max$ case.
A standard mathematical statistics textbook will present this result when introducing order statistics.
Answer: Consider four people, Yugi, Yuki, Yusei and Yuma. If we get their ages, observe that the following are equivalent.

Yuma is younger than Yusei, Yuma is younger than Yuki, and Yuma is younger than Yugi (Analogous to: $y \le X_1, y \le X_2, y \le X_3$ where $y$ is the age of Yuma and the $X_i$'s are the ages of the others).
Yuma is younger than the youngest among Yusei, Yuki and Yuma (Analogous to: $y \le \min\{X_1, X_2, X_3\}$).
Comment: Perhaps some additional elaboration could make it clear how this answers OP's question.
Comment: If you define the r.v.'s under consideration carefully, this isn't even an *analogy* -- they will be random variables.
Comment: @user777 what do you mean? For a given $\omega$, whatever is the value of $X_i$ in one side of the equation is the value of $X_i$ on the other side


Abrir Vários Programas de uma vez o CMD

Question: Quero usar o CMD para abrir um programa várias vezes.
Exemplo quero abrir 5 vezes com um só comando.
E não como está nesse exemplo.
Então eu não quero desta forma e sim de alguma que abra com um só comando.
timeout 5
start chrome.exe
timeout 2
start chrome.exe
timeout 2
start chrome.exe
timeout 2
start chrome.exe
timeout 2
start chrome.exe
timeout 5

Answer: 1° Abra o CMD.
2° Digite:
FOR /L %V IN (1 1 5) DO start chrome %V

Ele vai abrir 5 paginas no Chrome.
Comment: Obg. =) Ajudou muito.
Answer: Digite o comando no arquivo batch:
start notepad.exe && calc.exe && mspaint.exe

Basta separar cada nome de processo por um "&&".
Comment: Obrigado, uma forma bem legal.
Answer: Existem opções:
1. Executar o program em segundo plano (background): Start /b
2. Abrir o Chrome em janelas, em abas em ou ambos não em modo privado/ incócgnito:

start "" /b chrome --new-window

for %%i in (pt.stackoverflow,stackoverflow,superuser,windows,google)do start "" /b chrome --new-window %%~i.com & timeout 5

start "" /b chrome --new-tab

for %%i in (pt.stackoverflow,stackoverflow,superuser,windows,google)do start "" /b chrome --new-tab %%~i.com & timeout 5

start "" /b chrome --incognito

for %i in (pt.stackoverflow,stackoverflow,superuser,windows,google)do start "" /b chrome --incognito %~i.com & timeout 5

start "" /b chrome --new-window --incognito

for %%i in (pt.stackoverflow,stackoverflow,superuser,windows,google)do start "" /b chrome --new-window %%~i.com & timeout 5

start "" /b chrome --new-tab --incognito

for %i in (pt.stackoverflow,stackoverflow,superuser,windows,google)do start "" /b chrome --new-tab --incognito %~i.com & timeout 5

Abrir Vários Programas de uma vez o CMD


Can I set command line arguments using the YAML metadata

Question: Pandoc supports a YAML metadata block in markdown documents. This can set the title and author, etc. It can also manipulate the appearance of the PDF output by changing the font size, margin width and the frame sizes given to figures that are included. Lots of details are given here.
I'd like to use the metadata block to remember the command line arguments that I'm supposed to be using, such as --toc and --number-sections. I tried this, adding the following to the top of my markdown:
---
title: My Title
toc: yes
number-sections: yes

---

Then I used the command line:
pandoc -o guide.pdf articheck_guide.md

This did produce a table of contents, but didn't number the sections. I wondered why this was, and if there is a way I can specify this kind of thing from the document so that I don't need to add it on the command line.
Comment: I wrote a small script that sets pandoc command-line options from the YAML metadata: https://github.com/mb21/panrun
Answer: YAML metadata are not passed to pandoc as arguments, but as variables. When you call pandoc on your MWE, it does not produce this :
pandoc -o guide.pdf articheck_guide.md --toc --number-sections

as we think it would. rather, it calls :
pandoc -o guide.pdf articheck_guide.md -V toc:yes -V number-sections:yes

Why, then, does you MWE produces a toc? Because the default latex template makes use of a toc variable :
~$ pandoc -D latex | grep toc

$if(toc)$
\setcounter{tocdepth}{$toc-depth$}

So setting toc to any value should produce a table of contents, at least in latex output. In this template, there is no number-sections variables, so this one doesn't work. However, there is a numbersections variable :
~$ pandoc -D latex | grep number

$if(numbersections)$

Setting numbersections to any value will produce numbering in a latex output with the default template
---
title: My Title
toc: yes
numbersections: yes
---

The trouble with this solution is that it only works with some output format. I thought I had read somewhere on the pandoc mailing-list that we soon would be able to use metadata in YAML blocks as intended (ie. as arguments rather than variables), but I can't find it anymore, so maybe it won't happen very soon.
Comment: Great, thanks. That all makes sense. While not perfect (and a bit brittle if the templates are mushed about), that'll do for me until an improvement comes along.
Answer: Have a look at panzer (GitHub repository).
This was recently announced and released by Mark Sprevak -- a piece of software, that adds the notion of 'styles' to Pandoc.
It's basically a wrapper around Pandoc. It exploits the concept of YAML metadata blocks to the maximum.
The 'styles' provide a way to set all options for a Pandoc document conversion process with one line ("I want this document be an article/CV/notes/letter.").
You can regard this as more general abstraction than Pandoc templates. Styles are combinations of...

...Pandoc command line options,
...metadata settings,
...templates,
...instructions to run filters, and
...instructions to run pre/postprocessors.

These settings can be customized on a per output type as well as a per document basis. Styles can be...

...combined and
...can bear inheritance relations to each other.

panzer styles simplify Makefiles: they bundle everything concerning the look of a document in one place -- the YAML metadata (a block in the Markdown file, or a separate file).
You just add one line of metadata (style: ...) to your document, and it will be treated as a letter/article/CV/notebook or whatever.


Encrypt/Decrypt filesystem container file with smartcard on linux

Question: I want to encrypt and decrypt a file system container file with luks and a smartcard on debian linux.
The smartcard is a Nitrokey Start which is basically a compact smartcard in a usb reader.
I know how to create and mount a container file with dd, cryptsetup and a key file. But I can't figure out how to use a smart card instead of a key file.
I read something about that this is not possible because you can't read the key from the smartcard.
So did I understand this right, I have to:

create a container
generate a key file
use cryptsetup and the key file to encrypt the container
use the smartcard to encrypt the key file
delete the not encrypted key file

And every time I want to decrypt ant mount the container I have to:

decrypt the key file with the smartcard
decrypt the container with the decrypted key file
delete the not encrypted key file

Is this correct?
Are there any better ways to achieve HDD (or file system container) encryption with a smartcard?
Note: I'm not bound to cryptsetup and luks, so if there are any better tools out there to achieve my goal, please tell me.
Comment: It would be insecure to actually write the unencrypted key to your HDD. You would pipe the output directly to luks so that it is only ever decypted in memory: 'pkcs15-crypt --decipher --input /tmp/encryptedkey.pkcs1 --pkcs1 --raw | cryptsetup --key-file=- luksOpen /dev/sdb1'
Comment: Instead of using a key file you could use a "password". This way invoking cryptsetup isn't much different from using an ordinary password. Obviously the password should be long and random and doesn't need to be rememberable. Hence, you could get it from /dev/random.
You would store the encrypted password as you describe storing an encrypted key file.
Answer: Looks like you can load external keys on to the Nitrokey with a GPG command called "keytocard"

generate key file
copy key file to Nitrokey (keytocard)
secure erase key file from HDD
use key file on the Nitrokey to encrypted\decrypt as normal

There are many options for encrypted containers with VeraCrypt being the easiest to use.
Here is the official documentation for Nitrokey:
https://www.nitrokey.com/documentation/applications#p:nitrokey-start&os:linux&a:hard-disk-encryption
If you already have a container that is encrypted with a key, just copy that one, test it and if it works from the Nitrokey just secure erase the copy on the HDD. Now the only copy left is on the key.
(You might want a backup)
Comment: I realy tried, but maybee my question wasn't clear enough.

I have no Problem in putting keys onto the Nitrokey. It is Step4 that I can't follow.
"4. use key file **on the Nitrokey** to encrypted\decrypt **as normal**"

So how do I do that?

With a key file the cryptsetup command looks like this:
`cryptsetup luksOpen --key-file luks-key.txt /dev/loop0 container1`

But there is no such thing like: `cryptsetup luksOpen --smart-card key-slot1 /dev/loop0 container1`
Comment: @soulflyman Ok, I see. There are instructions on how to do this within the Nitrokey documentation. See my edit above.
Comment: I understand the term "key file" in this discussion is inspired by TrueCrypt/VeraCrypt where it describes a file containing a symmetric secret to decrypt an encrypted volume. However, GnuPG's ```keytocard``` command can be used to import RSA and ECC keys to the Nitrokey device. Using "key file" in conjunction with GnuPG's "keytocard" is misleading.
Comment: They are all keys in files. You can put all sorts of key files on a Nitrokey.


Comparing regression coefficients between models with dependent predictors and dependent criterions

Question: I would like to compare regression coefficients between two models as part of a within-subject study setup. The problem is that both the predictors and criterions are likely to be correlated and that my understanding of repeated measurement analysis is thus far limited.
Basically, I know (from theoretical considerations) that there exists a linear relationship:
$$
DV_{ij}=\beta_0 + \beta_{1j}IV_{ij}+\epsilon_{ij}
$$
where $i \in 1,..,400$ (subjects) and $j \in 1,2$ (conditions).
I measure both variables two times (2 conditions): $DV_{i1}, DV_{i2}$ and $IV_{i1}, IV_{i2}$. I expect the variables' values to change.
I want to test whether $\beta_{11} \ge \beta_{12}$.
I was thinking about using dummy coding to describe it as an interaction effect as described in this question and I thought about using SUR as described in this question, yet I believe that both approaches would not acknowledge the correlation between the criterions. Likely, my biggest problem right now is that I have too little knowledge about regression and I do not know what to call this "statistical problem" and which "tools" to look for.
I appreciate any remarks and comments.
Answer: You can use a repeated measures regression model, with an unstructured covariance (2x2) covariance matrix to account for the dependence between (and possible heteroscedasticity  across) the two conditions.  Then use a contrast test provided by the software. Any good software would allow this contrast test, and would automatically incorporate the unstructured covariance matrix in the result.
As always, read "the manual" to be sure that the test is correctly calculated.  If they show a formula for the standard error that involves this estimated covariance matrix, then they are probably doing it right.


Throttling a user across multiple datacenters

Question: I'm trying to implement a throttling feature on nginx, that is shared across multiple servers across multiple datacenters. I would like to know what would be the best practice for building this.
For example, let's say that I have an HTTP API running on two cluster of servers (behind a load balancer) located in two different datacenters. I would like to throttle a developer by his api-key to 1000 requests/hour. The developer has built a mobile application, which means that depending where his final users are, requests will be server by both locations (the closest datacenter).
How would you enforce throttling in this particular scenario?
Comment: Would the throttling not be better suited to being handled by the actual application/backend? Remember you're dealing with a web server, your not implementing complex logic in it.
Answer: The easiest way would be to implement throttling in each of N data centers separately. In your case M=1000 requests/hour and N=2 data centers. So, just use M/N=500 as your throttle value.
See: NGINX - throttle requests to prevent abuse


@PostConstruct method called all the time on Jersey service

Question: Hi I'm using GigaSpaces XAP which basically uses Spring and ApplicationContext to do all it's init and config etc... At some point in time during the loading of the web application a "cache" or what they call a space proxy is instantiated and is made available through the ServletContext. This space proxy is what allows you to write and read to and from the clustered cache.
So what I did initially was to get the space on each REST method. So...
@GET
public String myMethod()
{
space = (GigaSpace)context.getAttribute("mySpace");
space.write(new HelloWorld());
space.read(....);
etc...
return "Hello World!";
}

Since the space itself is thread safe I was instructed by the GigaSpaces guys to lookup the space once on init of my "application" so i can save on the lookup of the space.
So I looked into @PostConstruct where I did...
@PostConstruct
public void init()
{
space = (GigaSpace)context.getAttribute("mySpace");
}

But it seems that this method is being called on every request I make! Does this mean my REST service is being created for each request I make? Is it because I'm using @Scope("request")?
If it Helps the servlet container is Jetty 7.1.4 and I'm using standard WAR to deploy.
Comment: if you have access to logs/console, throw a print in the bean constructor
Answer: But of course! System.out.println("Is your friend!")
And yes it is because of @Scope("request") changing to @Scope("singleton") causes the jersey "bean" to instantiate once instead of per request.