Can small SGD batch size lead to faster overfitting?

Question: I have feedforward neural net, trained on cca 34k samples and tested on 8k samples. There is 139 features in dataset. The ANN does classification between two labels, 0 and 1, so I am using sigmoid function in last layer and two hidden layers, both with 400 units. NN is created using following Keras code:
model = Sequential()
model.add(Dense(units=139, input_dim=len(list(X))))

model.add(Dense(units=400))
model.add(Activation('relu'))
model.add(Dropout(0.2))

model.add(Dense(units=400))
model.add(Activation('relu'))
model.add(Dropout(0.2))

model.add(Dense(units=1))
model.add(Activation('sigmoid'))

model.compile(optimizer='sgd',
          loss='binary_crossentropy',
          metrics=['accuracy'])

checkpoint = ModelCheckpoint('tempmodelKeras.h5',period=1)
custom = LambdaCallback(on_epoch_end=lambda epoch,logs: test_callback_wrapper())
model.fit(X, y, epochs=500, batch_size=128, callbacks=[checkpoint,custom])

test_callback_wrapper() is just for testing model after each epoch on test dataset and then calculating average precision score for different thresholds.
Now the part I need to help with:
Following images shows epoch number on X axis, and average precision on Y axis, for test set.

I tried to three different batch sizes (32, 128, 256), but if you look at the plot, it looks that smaller batch sizes are 'faster' in terms of number of epoch needed to reach maximum average precision, but are more prone to overfitting. However, I read several articles, where was written that larger batches usually leads to overfitting and smaller batches are better. How is it possible that in my image, it looks vice versa, that larger batches perform better?
Comment: Welcome to the site! I have to admit this is something new to me, intuitively I could not think of a reason why larger batches would result in overfitting.  However there seems to be literature, as you point out, that reports this phenomenon. Have you seen this [thread](https://stats.stackexchange.com/questions/164876/tradeoff-batch-size-vs-number-of-iterations-to-train-a-neural-network)? Also you are on the verge of answering your own question, why not plot the in-sample results alongside out-of-sample and then you can update the question?
Answer: Using "epoch" on the x-axis to compare "speed" of convergence while comparing different batch sizes makes no sense since the number of weight updates per epoch depends on the batch size. When using smaller batches, the learning algorithm performs more weight updates per epoch and it naturally it seems to converge faster.
In your image, the gray curve (batch size = 32) starts decreasing (overfitting) around epoch 40. The orange curve (batch size = 128, four times more) seems to peak around epoch 160—four times later, which is exactly after the same number of weight updates. Possibly you might see similar peak on the blue curve around epoch 320.
Generally, smaller batches lead to noisier gradient estimates and are better capable to escape poor local minima and prevent overfitting. On the other hand, tiny batches may be too noisy for good learning. In the end, it is just another hyperparameter that one needs to tune on the specific dataset. In your case, it does not seem to play a big role, since all curves peak around the same accuracy, and early stopping should lead to the same performance regardless of the batch size.
Comment: Thanks. This helps me a lot. I understood that smaller batch sizes can be noisier, but this explains me why they reach peak faster and then starts overfitting.
Comment: Great answer. Note that when the dataset is small enough or homogenous enough, it won't matter if the batch size is 32 or 128, from the aspect of accurately approximating the loss and gradient (they will both be very accurate), you'll just need to wait x4 more epochs with b=128, as @Jan said in his answer, to have the same number of weight updates.


Using the Python API is there anyway to get the Test Run's Status?

Question: I currently have a python program which exports Test Runs, Test Plans, and Test Cases to CSV.
I am using the TestRun Model but I cannot get the information highlighted in the status column seen here. Is there anyway to get this information?
Thanks!
Answer:
I currently have a python program which exports Test Runs, Test Plans, and Test Cases to CSV.

This is great. Please consider contributing your script under https://github.com/kiwitcms/api-scripts

I am using the TestRun Model but I cannot get the information highlighted in the status column seen here. Is there anyway to get this information?

Note: these screenshots are old!
There is no TestRun.status field, the status is only a visual property which is calculated by the UI based on the presence or absence of TestRun.stop_date field. If this field is null/None then the status is "Running", otherwise it is "Stopped".
In the current TR search page for example we query the TestRun.filter API with a
stop_date__isnull=True parameter.
Comment: Thanks! I am already exporting the stop_date field from the TestRun model. Ill run this by our documentation folks and let them know to check that field. Unfortunately this script was developed on contract so I cannot share it externally. Appreciate the help though!
Comment: Ask your customer if you can open source. Maybe they wouldn't mind. They will retain copyright and all.


Contour integral $\int_{-\infty}^{\infty}e^{-iax}/(-b+\cos(x))\mathrm dx$ with $a>0$ and $0<b<1$

Question: The integral is
$$\text{PV}\int_{-\infty}^{\infty}\frac{e^{-iax}}{(-b+\cos(x))}\, dx$$
 with $a>0$ and $0<b<1$.
This integral stems from the Fourier transform of a Green's function in energy-momentum domain to Energy-position domain. The poles in the integrand are the eigenvalues of the system (tight binding model). How to deal with these poles? I know that since $a>0$, so we can choose the contour which goes to the lower half complex plane. But I have no idea how to deal with this kind of poles which are periodic on the real axis. If anybody has any idea, please help. Thanks.
Comment: For $0 < b < 1$, this is the Fourier transform (with "frequency" $a$) of the function $f(x) = 1/(\cos(x) - b)$. The function $f(x)$ is periodic in $x$ with period $2\pi$, and it has an infinite number of divergences where $\cos(x) = b$. Assuming we can take the principal value of the integral across each divergence, then a "value"  for this integral can be obtained in the sense of distributions as an infinite sum of $\delta$-functions in $a$.
Comment: Thanks, John. Do you mean $\sum_{x_{poles}}\delta(a-x_{poles})$? Is it true that the principal value of a general integral across each divergence can be treated as a sum of delta function?
Comment: I added a principal value, otherwise the request is meaningless (it is a divergent integral).
Answer: Let $f(x)$ be any function that is periodic with period $L$, so that $f(x+L) = f(x)$. Then the continuous Fourier transform of $f(x)$ is:
\begin{align}
\hat{f}(a)
&= \int_{-\infty}^{+\infty} dx\, f(x)\, e^{-i a x}\\
&= \sum_{n = -\infty}^{+\infty}\int_{nL}^{(n+1)L}dx\, f(x)\, e^{-i a x}\\
&= \int_0^L dx\, f(x)\, e^{-ia x}\sum_{n = -\infty}^{+\infty} e^{-i n a L}\\
&= \sum_{n = -\infty}^{+\infty}\delta\left(a - \frac{2\pi n}{L}\right)
\; \frac{2\pi}{L} \int_0^L dx\, f(x)\, e^{-i\frac{2\pi n x}{L}}\, .
\end{align}
In going from the second to the third line above, I have made a change of variables $x \rightarrow x+nL$, and used the fact that $f(x+nL) = f(x)$. In going from the third to fourth line, I've made use of the $\delta$-function identity
$$
\sum_{n = -\infty}^{+\infty}e^{\pm i n x} = 2\pi\sum_{n = -\infty}^{+\infty}\delta(x - 2\pi n)\, .
$$
For this problem, $L = 2\pi$ and $f(x) = 1/(\cos(x) - b)$, and so the integral in question becomes:
$$
I(a,b) = \sum_{n = -\infty}^{+\infty} C_n(b)\, \delta(a - n)\, ,
$$
where
$$
C_n(b) = PV \int_0^{2\pi}dx\, \frac{e^{-i n x}}{\cos(x) - b}\, .
$$
This expression for the coefficients $C_n(b)$ can be evaluated by contour methods. Make a change of variables $z = e^{-i x}$, so that $dz = -i z\, dx$ and $\cos(x) = (z + 1/z)/2$. Then:
$$
C_n(b) = 2i\, PV\oint dz\, \frac{z^n}{z^2 - 2 b z + 1}
$$
Here the contour runs around the unit circle in the complex plane in the clockwise direction (because we defined $z = e^{-i x}$). The integrand has two poles, located at
$$
z_{\pm} = b \pm i\sqrt{1 - b^2}\, .
$$
Note that $|z_{\pm}| = 1$, so the contour passes through both of these poles. Since this is a principal value integration, we can use standard contour integration techniques and each pole on the contour will contribute half its residue to the integral. (Perhaps a real mathematician, which I am not, can correct me on the generality of that assertion.) Then:
\begin{align}
C_n(b)
&= 2i\times (-2\pi i) \times \frac{1}{2} \times
\left[
\frac{{(z_+)}^n}{z_+ - z_{-}} +
\frac{{(z_{-})}^n}{z_{-} - z_{+}}
\right]\\
&= \frac{2\pi}{\sqrt{1 - b^2}}\;\mathrm{Im}\left\{{\left(b + i\sqrt{1-b^2}\right)}^n\right\}
\end{align}
Finally, by using the fact that $b + i\sqrt{1-b^2}$ has modulus unity, this becomes:
$$
C_n(b) = \frac{2\pi}{\sqrt{1 - b^2}}\, \sin(n\phi)\, ,
$$
where
$$
\phi = \tan^{-1}\left(\frac{\sqrt{1-b^2}}{b}\right)\, .
$$
Together these expressions for $I(a,b)$ and $C_n(b)$ should answer the question.
Comment: +1. No idea how this went so long without any upvotes !!!


Lendo e Comparando login e senha do arquivo em C

Question: Estou programando minha tela de login do sistema com a ajuda de arquivos, ou seja, salvar/ler os dados dentro de um arquivo.txt.
Até agora eu consegui fazer o cadastro do login e senha do usuário, porém estou lidando com vários erros na hora de ler o texto que está dentro do .txt, "pegar" o login e a senha digitadas anteriormente no cadastro e comparar com o login e a senha que o usuário digitou no momento quando tentou fazer o login.
Código abaixo é iniciando a parte da escrita no login.txt, que é o nome do meu arquivo:
FILE * fp; //Está em cima do main

int main () {

 fp = fopen("login.txt", "a+");

    if (fp == NULL)
    {

        printf("\nErro na criação da abertura do arquivo!");

    } else {

        /*Aqui consta o menu, para levar o usuário a outras telas, caso ele queira "Cadastrar" ou "Logar" no sistema.*/

    }
    fclose(fp);
}

Abaixo a tela de cadastro:
void tela_cadastro(void)
{
    printf("\n\tLOGIN........: ");
    fgets(pessoas[i].login, 100, stdin);
    __fpurge(stdin);

    printf("\n\tSENHA........: ");
    fgets(pessoas[i].senha, 100, stdin);
    __fpurge(stdin);

    //fprintf(fp, "\n************LOGIN************SENHA************\n");

    fprintf(fp, "%s", pessoas[i].login);

    fprintf(fp, "%s", pessoas[i].senha);

   // printf("\n\tLogin: %s", pessoas[i].login); // teste só pra ver se ta mostrando login
    //printf("\n\tSenha: %s", pessoas[i].senha);//teste só pra ver se ta mostrando senha

}

OBS: Na tela acima do cadastro, eu consigo cadastrar no login.txt todos os logins e senhas.
Abaixo o código da tela de login, que é onde estou tendo problema:
void tela_login()
{

    FILE *fp;
    fp = fopen("login.txt", "r"); //"r" para ler o que tem no arquivo

    if (fp == NULL) {

        printf("\nO arquivo não foi aberto!");
        exit(0);

    }

    char login[100], senha[100], str[100]; //str = linha
    int tamanho=100;

    printf("\n\tLOGIN........: ");
    fgets(login, 100, stdin);
    __fpurge(stdin);

    printf("\n\tSENHA........: ");
    fgets(senha, 100, stdin);
    __fpurge(stdin);

   // printf("\nLogin: %s", login); teste p ver o que estava imprimindo
   // printf("\nSenha: %s", senha);teste p ver o que estava imprimindo

   while(fgets(str, tamanho, stdin) != EOF) {

       if ( (strcmp(str, login) == 0) && (strcmp(str, senha) == 0) ) {

            printf("\nTESTE");

        } else {

            printf("\nERRO");

        }

    }//Fim while

    fclose(fp);

}

Se alguém puder me mostrar a direção. Tipo, o que devo pesquisar para solucionar esse problema. Já tentei de tudo, mas eu acredito que não to conseguindo entender como a função funciona, mas já pesquisei e ainda preciso de uma explicação mais específica. Alguém pode me ajudar?
Comment: Qual erro está acontecendo?
Comment: Vc quer gravar o login e a senha no arquivo isso?
Comment: Gravar eu já gravei. O erro está no login. Não to entendendo como comparar um valor do login.txt com o valor digitado pelo usuário no login.
Comment: O erro ta no while da tela tela_login();
Comment: na linha 146, da um erro: comparison between pointer and integer
Answer: Você disse a linha, mas não sei qual é a linha 146 (não tem numeração de linhas...).
Lendo seu código, acredito que o problema esteja na linha:
while(fgets(str, tamanho, stdin) != EOF) {

O comando fgets retorna um char*, não um int. Esta linha deve ser reescrita substituindo EOF por NULL, pois quando não há mais dados a serem lidos do arquivo, este comando retorna NULL. Do contrário, ele retorna o mesmo passado no primeiro parâmetro (no caso, str).
Segue o código:
while(fgets(str, tamanho, stdin) != NULL) {

Ou simplesmente:
while(fgets(str, tamanho, stdin)) {

Gostaria apenas de observar que a linha após este comando não faz sentido, uma vez que você compara str tanto com login quanto com senha.
Comment: Ah vdd. Desculpa, nao reparei na questão da linha. Mas nessa onde você apontou mesmo que estava o erro. Muito obrigada, eu suspeitava mas procurava como solucionar o problema e não encontrava nada que eu conseguisse entender. Sua explicação me ajudou a entender aa lógica ali também. Ah sim, essa linha eu coloquei mais porque nada tava dando certo daí resolvi ir testando tudo que eu achava que dava pra colocar. Mas obrigada. Me ajudou muito. ;)


Why are Clavichords Keys Diagonally Shaped?

Question: Why are the keys on clavichords (as shown below) not all parallel?

In fact, only 1 or 2 of the keys go straight back, the others all bend diagonally and then go straight at the end.
Why is this? I would assume it has something to do with how the keys hit the strings but I'm not exactly sure.
Comment: Lots of information about clavichords that confirms user26889's answer and more is readily available on the Wikipedia page: https://en.wikipedia.org/wiki/Clavichord
Comment: I am not familiar with clavichords but it looks like it's mapping notes, which get closer and closer together as they get higher (like a guitar), to fixed-width keys.  There's only one going straight back which is exactly what you'd expect in such a case.
Answer: The reason this clavichord's key arms are cranked (as they say) is because it is a fretted clavichord- that is, most of the strings play more than one note, depending on where they're struck.  And as the strings get higher and shorter, the places they are struck that will produce a half tone of difference (say, between Eb and E) get closer together.
In designing a fretted clavichord, the positions of the strings and the keys are juggled to try to reduce this cranking as much as possible, because it's mechanically not ideal.  Unfretted clavichords, which have one pair (usually) of strings to each note don't have this problem and can be laid out with straight key arms. However, even unfretted clavichords usually crank the arms a bit at the top of the range to prevent the bridge from having to be radically curved back.
Answer: The OP's picture is a half-complete instrument. The tangents (the vertical metal pieces that press the strings) and the strings themselves are not yet in place, so it's hard for a "non-expert" to understand how the finished instrument will "work".
The pictures in the Wikipedia link are of complete instruments, but the back of the keys is partly covered by the felt dampers which stop the "unused" part of the strings from vibrating.
The tangents will be at the end of the dog-leg part of the keys, i.e. in a diagonal straight line running across the instrument. The "working length" of the key levers is thus shorter in the bass than in the treble, which applies more force to the bass strings for a constant finger-pressure across the whole keyboard.
The strings run in a parallel band at an angle to that line, with the bass strings at the front and the treble strings furthest form the player.
The geometry of where the strings must be "touched" depends on the shape of the curved bridge (on the soundboard at the right) and where the left hand end of the strings are fixed. The bass strings extend to the left hand side of the instrument but you can see the pins for the treble strings along the back of the case. (The positions of the tuning pegs for the strings, on the right hand side of the soundboard, have been marked out, but the pegs are not yet fitted). Any intentional variation in string tension between bass and treble, also affects the geometrical layout.
The second part of the dog-leg "behind" where the tangents will be, is just a matter of fitting the keys into the case. The back of each key is slides up and down on a metal pin fixed into the back rail of the case, to stop the keys wobbling sideways. Therefore the back of the keys need to be wide enough to contain the slots for the pins without the risk of the wood cracking, and more or less evenly spaced along the back of the instrument.
All these geometrical compromises apply to both fretted and unfretted clavichords. The main difference in design is that fretted instruments have fewer strings (since two notes share the same string and can not be played together) which reduces the "depth" of the instrument from front to back.
Incidentally, keyboard-making was considered to be the most skilful woodworking task in building these early keyboard instruments. To avoid problems caused by the wood warping, the traditional way was to cut the entire set of keys out of one solid plank of wood, so that after cutting (using nothing but hand tools, of course) any changes in temperature and humidity would warp them all in the same direction, and minimize any problems with adjacent keys jamming against each other. Make one mistake, and the whole keyboard was firewood!
Comment: Agreed, except for the point about unfretted clavichords having "all" the geometrical compromises fretted ones do.  It's quite possible to build an unfretted clavichord, just as it's possible to build a harpsichord or a piano, with perfectly straight key arms, by simply laying out the bridge accordingly.
Answer: Clavichords work by shortening strings according to pitch.  They keys all have the same distance, but the distances of shortening are spaced logarithmically like on a guitar fretboard.  While this is somewhat masked by the overall distribution of notes across the strings (and the geometrical arrangement of the strings themselves), the higher pitches per string still need to be accessed with shorter distances.
Answer: On a piano, there is a one-to-one correspondence between a key and the strings that play a note. One group of strings (a single string, a pair, or three) are used to produce one single note. But on a clavichord, one group of strings (a single string or a pair of strings) is used to produce one, two, or three different notes. This is in the same manner that one frets a guitar string at different positions to produce multiple pitches from a single string.
The angles of the key mechanisms on the clavichord enable positioning the tangents which strike the strings along different points of the length of the string, resulting in the ability to play different notes on one group of strings.

Here is a clavichord with three notes per pair of strings. Click on the original image to see it in much larger detail.
Comment: Yep.  I'd just add that some extremely fretted clavichords have as many as four notes from one course of strings.  I have plans for an early German clavichord that has D♯, E, F, and F♯ on the same course, meaning that you can't play, for instance, a B major triad in close position.  But I guess you don't need a B major triad for Renaissance music much...


software to determine the tuning (temperament) of a recording?

Question: Case in point: Glenn Gould's recording of the WTC. The liner notes mention nothing about the tuning (in fact, I doubt the author understood the difference between equal and well temperament). This is one of many recordings about which I would like to know what tuning they used. I can sort-of tell quarter-comma-mean-tone by listening, but that's about it.
Thanks!
Comment: I don't think Glenn was big on "historical" tunings, and the lack of any special mention of the tuning used would imply "standard tuning", I would say. Just because it's the "Well-Tempered Clavier" doesn't mean it has to be played on one!
Answer: This should be possible in theory, but I doubt you will find any "free" software that can do it, or any software at all that doesn't require some specialist knowledge.
The basic problem is the accuracy required. The difference between a just intonation fifth and an equal-tempered fifth is about 2 cents (1 cent = 1/100 of a semitone). So let's make the plausible assumption that to accurately "measure" a temperament you need to measure the pitch of individual notes to within 1 cent.
For A = 440 Hz, the note 1 cent higher has a frequency of about 440.25Hz, so in the mid range of the piano you need to measure frequencies to that degree of accuracy.
If you use the simplest approach to this based on Fourier analysis, you would need a "constant amplitude" note with a duration of about 4 seconds to measure the frequency to within 0.25 Hz. It's not very likely that you will find single notes of that length in the recording, even given Gould's rather eclectic approach to choosing tempos!
Of course there are more sophisticated methods of determining frequency than the above, but any reasonably straightforward method needs a single "note" that is long enough to analyse.
If you can find single notes that are sufficiently long, you could try using a guitar tuner to measure the frequencies - but I don't have any practical experience of whether that would be successful.
A good piano tuner "measures" frequencies to this level of accuracy by counting the beats between two notes played simultaneously - but in the mid range of a piano that depends on listening to just two notes for long enough to accurately measure beat frequencies of about 1 beat per second. Once again, you need notes that last for a few seconds to do that by ear.
I expect if you wanted to pay a signal-processing specialist enough money, he/she would find this an interesting challenge - but "enough money" might be more than you want to spend!
Comment: OTOH, there are notes in the scale with larger frequency deviations than the fifth.   In addition, the precision of a Fourier transform depends on the number of samples, not just the temporal sample length. If you were to sample at, say 1MHz, you should be able to distinguish the fifths.
Comment: @CarlWitthoft No. If you sample a one-second signal at 1 kHz, you can resolve frequencies up to 0.5kHz with a resolution of 1 Hz. If you sample the same 1-second signal at 1 MHz, you can resolve frequencies up to 0.5 MHz *with exactly the same resolution of 1 Hz*. For the OP's application, the extra *frequency range* (1000 time bigger) doesn't help. Also, the resolution you need depends on the *smallest* deviation from some nominal reference point, not the average or the largest.
Comment: @leftaroundabout " IMO the deviations of the fifths in 12-edo vs just intonation are neglectable, but others like the 15ct sharp major thirds are very much not neglectable" - that doesn't make any sense. Your 15ct sharp major third is simply the sum of 5 tempered fifths! Sure, an ET major third *sounds* more "unjust" than an ET perfect fifth, but that's irrelevant. You can easily hear the difference between a just 5th and a meantone 5th, and that is only about twice as big as the difference between just 5th and an ET 5th.
Comment: Right. IMO the deviations of the fifths in 12-edo vs just intonation are neglectable, but others like the 15ct sharp major thirds are very much not neglectable. I'd suggest something like 5ct as the required precision, which makes it all substantially more feasible.
Comment: Well... stacking up five neglectable things can add up to something non-neglectable! And the "only" twice-as-big (actually a bit more) dicrepancy to Pythagorean of the ¼-comma meantone fifth makes it just flat enough to actually sound off in a musical context (to my perception), whereas I'd not be able to tell the 12-edo fifth apart from Pythagorean. The quarter-comma fifth is in fact 5.4 cents flat, which is why I think 5ct would be a reasonable resolution for such a program.
Comment: @alephzero  you are exactly correct and I apologize for confusing range with resolution.  I should be more awake when I comment.
Comment: @leftaroundabout 5ct is tpo coarse. Take two well documented historical temperaments (relevant to Bach) like Werckmeister I and III. In WI the interval between C and E is 392.18 cents. In WIII it is 390.22 cents. The difference between them (1.96 cents) is exactly the same as the difference between a ET and a just 5th (and that is by design, not a coincidence). To find what Gould's temperament was, IMO you need to measure to that level of accuracy. See https://en.wikipedia.org/wiki/Werckmeister_temperament for the "exact" definition of the two temperaments.
Comment: Re the accuracy required, pipe organ tunings (which are often based on historical temperaments rather than ET) are typically defined, and set, with a nominal accuracy of 0.1ct, which is the difference between counting 9 or 10 beats in 10 seconds, in the octave around middle C where the temperament is set.
Comment: @alephzero You only need 4s worth of sound, not a continuous note. True, every time a hammer strikes the note the frequency itself varies a bit, but I still think it should be possible to stitch multiple different parts and only then analyze them (doesn't sound that complicated).
Comment: @dtldarek: for that to work you'd need to stitch them together with exactly reproducable phase. I.e. you'd need to know the time when the hammer striked the string down to a millisecond.
Comment: @alephzero: you're right of course if you want to know it that exactly. I personally don't consider it meaningful to make a distinction between Werckmeister I and III — musically: it certainly is important to know the difference as an organ tuner, in the sense it's probably important to know the difference between different wood files if you're a luthier. But for all that's _musically_ relevant, if the difference is _in principle_ too small to detect it with a computer then it's also to small to matter for the player or listener.
Comment: If you really wanted to do this, you should probably look at "Bayesian Spectrum Analysis and Parameter Estimation" G. L. Bretthorst: http://bayes.wustl.edu/glb/book.pdf  , the two points relevant to these comments are that at some point, SNR drives the accuracy more than sampling, and that you should be able to combine the estimated frequencies from different notes w/o having to time align the signals themselves.
Answer: This is too long for a comment, so I'm posting it as an answer.
I though it should be possible to recover tuning of the instrument using harmonics, after all due to how Fourier transform works, higher frequencies have higher precision. I have opened a recording of the last chord of C major prelude (cut out and copied several times from both left and right channels) in spectral imaging software and the harmonics I recovered form the recording do not make sense to me.
For a FFT of size 65536 and 44.1 KHz sampling (which gives accuracy of 0.67 Hz) I got the following buckets (manually picking those with high values):
<code>89, 97, 194, 244, 268, 292, 357, 388, 446, 486, 491, 714, 780, 889, 974, 1072, 1169, 1268, 1365, 1368, 1475, 1563, 1665, 1756, 1765, 1956, 1970, 2065, 2154, 2166, 2267, 2467, 2552, 2751, 2778, 2880, 2941, 2968, 3473, 3543, 3768, 3946, 3983, 4149, 4497, 4761, 5016, 5543, 6618
</code>
I know that harmonics of acoustic instruments deviate from the ideal series, and that it can vary in time, but I guessed the mean should be good approximation, and I hoped these differences among different strings will be similar.
Unfortunately my approach didn't work. The differences between harmonics and their ideal counterparts were just too big and unpredictable. Also, there were some larger clusters of frequencies, too large even if you were to count colliding harmonics of different notes. Sometimes good piano tuners introduce slight differences between different strings of the same note to make the sound more rich, but these are usually very small. Also, I didn't have the recording in a lossless format, so perhaps the encoding messed the sound (although I doubt it).
In other words, I have no idea why that didn't work, but the problem of recovering actual tuning seems to me much more difficult. Although the general issue is accuracy, if I were to guess the actual cause looks more like the general complexity of sound of the piano, including interference of all the other strings that were not hit, but still play.
Finally, even if we were able to recover note heights with reasonable accuracy, that might not be any known tuning (e.g. the high frequencies might be a bit more equal-tuned than the rest, while the low frequencies might be just stretched). In fact, if we go into such detail, then each acoustic piano is slightly different (in particular how they resonate), and I would expect that tuners tune pianos so that they sound good rather than just use some particular scheme, even if they try to achieve effect similar to, say, Werckmeister III tuning.


CKEditor configuration is not working

Question: In my site it was not required to add color to text or change its size, but now we want to add formatting to our posts.
So I tried to configure CKEditor's Toolbar to add text formatting options in Drupal administration page, and after I added that, I see no changes in my CKEditor, new options weren't added.
How could I add that options?
Thanks in advance for your help
Answer: A few things you can try:

Make sure that when you are editing or creating new content that the "Text Format" selection box at the bottom of the WYSIWYG editor shows the same profile that you edited. On my site an advanced user can be allowed to choose from Filtered or Full HTML, and by default the Filtered HTML profile is loaded (thus they wouldn't immediately see changes to the "Full HTML" profile).
You can try clearing your cache if you haven't already.
I understood your question as not seeing the new options in the Toolbar, but if your issue is that you are not seeing something like bolded or larger font upon saving, it may be that you need to create an exception to a specific HTML tag in your "Advanced Content Filter" and/or the "Cleanup and Output" settings under your CKEditor profile options (/admin/config/content/ckeditor/ in Drupal 7).

CKEditor configuration is not working


Quicksort Partition Algorithm

Question: I am trying to learn Quick Sort. To do so, I followed the logic for quick sort in this article where the last element is picked as the pivot and you work through the array from both ends swapping elements as needed. Now after a long time of trying to come up with my own algorithm based on this, here is what I have so far:
<code>using namespace std;

int a[] = {10,4,3,2,8,5};
int j;

int partition(int left, int right,  int array[]){
    int pivot = array[right];

    while(1){
        while(array[left]<pivot){
            left = left+1;

        }

        while(array[right]>pivot){
            right=right-1;

        }
        if(left>=right){

            return right;
        }

        int temp1 = array[left];
        int temp2 = array[right];
        array[left] = temp2;
        array[right] = temp1;
    }
}

void quicksort(int left, int right, int array[]){
    if(left<right){
        int p = partition(left, right, array);
        quicksort(left, p-1, array);

        quicksort(p+1, right, array);
    }
}

int main(){
    quicksort(0, sizeof(a)/sizeof(a[0])-1, a);

    for(int i=0; i<(sizeof(a)/sizeof(a[0])); i++){
            cout << a[i] << endl;
        }

}
</code>
So after testing this with various arrays of different sizes and elements, it does output a correctly sorted array however in the algorithms that I have found online (ex. Hoare's) they always decrement right and increment left at the start of the partition. Also the pivot does not move in most algorithms I have found, but I move it.
What I am wondering is, am I doing this wrong? Why does it work? It's been a lot of trial and error so that it be understandable if I did it wrong, I am also slightly new to algorithms and data structures so I wouldn't be surprised if I made some mistakes.
Comment: Sounds like your "question" would be more appropriate for http://codereview.stackexchange.com/
Comment: As you're asking to improve working code, this question is probably better placed at [SE Code Review](http://codereview.stackexchange.com/).
Comment: If we accepted `Why does it work?` questions, there would be too many :)
Answer: Regarding the pivot for Qsort, there's no fixed rule for selecting the pivot. Choosing the pivot really depends on which strategy you want to impose on your input data, and that largely depends on the properties of the input data - range, size etc.
Generally, we choose the pivot so that it splits the array into two equal halves (as much as possible). This is done so that we don't end up pushing all the elements on to one-side of the Qsort. If this happens your algorithm will become O(n^2) - think of skew trees if you're not able to see why.

Quicksort Partition Algorithm


Trying to add dependency json schema using loopback mongodb

Question: I wanted a dependency schema using mongodb loopback.
My Post requests:
REQUEST TYPE 1
<code>{
    "url": "google.com"
    "type": "redirect"
}
</code>
REQUEST TYPE 2
<code>{
    "url": "yahoo.com"
    "type": "anchor"
}
</code>
My model function looks like
<code> @property({
    type: 'string',
    required: true,
  })
  type: string;

  @property({
    type: 'string',
    required: true,
    jsonSchema: {
      maxLength: 2000,
      pattern: '^[\\]*$', //using some regex
    },
  })
  url: string;
</code>
My problem is I want this url pattern to be checked only if type in the payload is redirect.
I read about json schema dependencies. But it is not working. Nor giving any error.
I am trying something like:
<code>@property({
    type: 'string',
    required: true,
    jsonSchema: {
    maxLength: 2000,
    "dependencies": {
    if(type == "redirect){
       pattern: '^[\\]*$', //using some regex }
     }
    },
  })
  url: string;
</code>
Answer: You can use conditions in jsonSchema using dependancies.
Link : https://react-jsonschema-form.readthedocs.io/en/latest/usage/dependencies/
In your case it seems you can use oneOf.
Your schema can look like:
<code>@property({
  type: 'string',
  required: true,
  oneOf: [
    {pattern: 'your 1st pattern'},
    {pattern: 'your 2nd pattern'},
  ],
  jsonSchema: {
    maxLength: 2000,
  },
})
url: string;
</code>
This schema with oneOf is valid if exactly one of the subschemas is valid.
Comment: You are suggesting like if else ?
Comment: I am suggesting one of the pattern should mandatorily match else will throw error.


Blackberry return text from a website

Question: I was wondering if it is possible to return text from a website to the blackberry device.
So for example you have a plain text website that says "Hello World" on the page.
Is it possible to create a connection to that website and then return the contents of the page, in this case "Hello World"
I just want to save it as a string.
Should mention that i want this through an application not a blackberry browser
Answer: Of course you can. Just open an HttpConnection, which gives you an InputStream.
To convert an InputStream to a String, just do that:
byte[] response = IOUtilities.streamToBytes(stream);
String text =new String(response,"UTF-8");

Blackberry return text from a website


Como capturar uma foto através da webcam do usuário e enviar via POST?

Question: Como realizar o procedimento de captura de imagem da webcam do usuário em um formulário de cadastro para enviar via POST? Busco por uma solução que seja compatível com a maioria dos navegadores e que seja de simples implementação.
Comment: De uma olhada nesse tutorial: [http://www.phpclasses.org/blog/post/228-How-to-Use-a-Webcam-to-take-Pictures-in-PHP-Application.html](http://www.phpclasses.org/blog/post/228-How-to-Use-a-Webcam-to-take-Pictures-in-PHP-Application.html)
Comment: Este link tem um boa explicação em portugues:
Comment: Do meu ponto de vista a única maneira de ser o mais compatível com vários Browsers seria usar as soluções em html5 já demonstradas e acaso o browser não de suporte, usar uma solução alternativa em Flash (apenas para capturar a imagem).
Comment: Conseguistes resolver tua questão? Não encontrou a resposta?
Comment: Um adendo: Agora seu site precisa ter [conexão https para para poder acessar a Webcam via HTML5](http://pt.stackoverflow.com/questions/95282/getusermedia-agora-ser%C3%A1-depreciado-em-conex%C3%B5es-n%C3%A3o-https?noredirect=1&lq=1)
Answer:
Exemplo Mínimo
PHP
camera.php
<!DOCTYPE HTML>

<meta charset="utf-8">
<title>Tirar Fotos</title>

<video id="video" width="640" height="480" autoplay></video>

<button id="snap">Tirar Foto</button>

<button id="save">Salvar Foto</button>

<canvas id="canvas" width="640" height="480"></canvas>

fotossalvar.php
<?php
    function base64_to_jpeg( $base64_string, $output_file ) {
        $ifp = fopen( $output_file, "wb" );
        fwrite( $ifp, base64_decode( $base64_string) );
        fclose( $ifp );
        return( $output_file );
    }
    $imagem = str_replace('data:image/png;base64,','',$_POST['imagem']);
    base64_to_jpeg($imagem, "pasta1/foto1.png");
    echo json_encode(array('imagem' => 1));

Todo o código se resume nesses dois arquivos, siga esse modelo como fator de teste. Configure a sua aplicação com jQuery, e uma pasta com o nome de pasta1, sendo tudo feito em HTML5 com PHP.
Em relação aos navegadores, o Google Chrome e o Firefox funcionaram perfeitamente, já o IE não tive sucesso.
Fontes:

Camera and Video Control with HTML5
HTML5 Canvas Save Drawing as an Image
Answer: Existe um projeto muito interessante no GitHub com vista a fornecer uma solução atual para o uso da webcam do cliente, fazendo uso de fallback para Flash em casos de navegadores mais antigos:
WebcamJS

WebcamJS é uma pequena (~3K minificado e compactado) biblioteca standalone em JavaScript para capturar fotos a partir da câmera do seu computador, e entregá-los a você como JPEG ou PNG Data URIs. As imagens podem ser exibidas em sua página web, rendido em uma tela, ou submetido ao seu servidor. WebcamJS usa HTML5 getUserMedia, mas proporciona um fallback automático e invisível através de Adobe Flash.

Demonstração

Webcam.set({
  width: 320,
  height: 240,
  image_format: 'jpeg',
  jpeg_quality: 90
});
Webcam.attach('#my_camera');

function take_snapshot() {
  // take snapshot and get image data
  Webcam.snap(function(data_uri) {
    // display results in page
    document.getElementById('results').innerHTML =
      '
Here is your image:
' +
      '';
  });
}
body {
  font-family: Helvetica, sans-serif;
}
h2,
h3 {
  margin-top: 0;
}
form {
  margin-top: 15px;
}
form > input {
  margin-right: 15px;
}
#results {
  float: right;
  margin: 20px;
  padding: 20px;
  border: 1px solid;
  background: #ccc;
}

A imagem capturada aparece aqui...

WebcamJS Página de teste

Demonstração de uma captura e apresentação em 320x240

<form>
  <input type=button value="Tirar Snapshot" onClick="take_snapshot()">
</form>

Enviar para o servidor
A função Webcam.snap() fornece a imagem por meio de um URI Dados via JavaScript, conforme vimos no exemplo em cima.
Contudo, também dispomos de uma forma de decodificar e enviar esses dados da imagem para o servidor através de uma chamada AJAX:
var data_uri = Webcam.snap();

Webcam.upload( data_uri, 'meuScript.php', function(code, text) {
    // Upload concluído
    // 'code' contém o código da resposta HTTP enviado pelo servidor, ex. 200
    // 'text' contem o que o servidor enviou
});

No lado do servidor, os dados chegam tal como se tivessemos submetido um formulário com um campo <input type="file" name="webcam">, pelo que podemos simplesmente receber a imagem da seguinte forma:
// verificações de segurança e outros
// ...
// mover o ficheiro recebido para o local pretendido
move_uploaded_file($_FILES['webcam']['tmp_name'], 'webcam.jpg');

Se for necessário enviar mais dados para o servidor junto com a imagem, podemos faze-lo adicionando os mesmos na forma de queryString:
var username = 'Bubu';
var image_fmt = 'jpeg';
var url = 'meuScript.php?username=' + username + '&format=' + image_fmt;
Webcam.upload( data_uri, url, function(code, text) {...} );

Nota: Existem muitas opções fornecidas pelo projeto WebcamJS e o mesmo está muito bem detalhado e recheado de exemplos de utilização no link do mesmo no GitHub.


Probability of long identical substring

Question: You have a string of $20,000$ consecutive bits. Each bit is either a $1$ or a $0$ and has a $0.5$ chance of being either.
Calculate the probability that there is at least one substring of at least $34$ consecutive $1$'s or $0$'s.
its not a binomial so i cant normally approximate it.
Comment: yes, it can be answered.
Comment: The probability of $2$ or more such strings is very small in comparison to the probability of $1$, which is already quite small. So a good estimate is not hard to find.
Comment: okay???? but can you go into more detail please. as how to solve it?
Comment: there are a number of different ways to solve this question. what is the context.
Comment: what do you mean by context?
Comment: is this for a class? in a company? what discipline, etc...
Comment: its for my own knowledge.
Comment: can anyone answer this or no?
Answer: Hints
Here is an outline of one approach.

What if the substring contains exactly 34 1's, can you find it's likelihood?
What would it be for 34 0's? For either 34 1's, or 34 0's?
What if it were 35, how would this change your answer?
Can you generalize 34 and 35 above to $n$?
Sum from $n = 34$ to $n = 20000$?

Update
Here is an approach.
Pick string of 0's or 1's -- 2 ways to do that
Pick the starting place for your string (any one of $n=20,000$ bits except for the last 33) -- $20,000-33$ ways to do that
Pick the elements in the string, leaving others free -- 2^{n-34}
Total possible strings = 2^n
$$
Prob = \dfrac{2 \cdot (n-33) \cdot 2^{n-34}}{2^n}
     = \dfrac{n-33}{2^{33}} = p_{34}
$$
Notice this overcounts the 35-long strings, subtract $p_{35}$ and now you didn't count the 36-strings, so use inclusion exclusion:
$$
p_{34} - p_{35} + p_{36} \pm + p_{20,000}
$$
Comment: you don't have to state the obvious. how do you solve this problem so that  the calculator doesnt overflow. I cant use normal approximation cause its not binomial. my intuition tells me  its something like: 1-(((0.5)^33)(0.5^19967)(19967))+(0.5)^32)(0.5^19968)(19968).....+(0.5^20000))
Comment: @mitzy lots of people come to this site, it would not be obvious for everyone. Please update the question with a generalized version of your approach and I will be glad to help you to compute the sum.
Comment: my question is in its purest form. I'm the one who's asking you for help. I'm not sure how to solve this. Can you solve it or not? If you can... just give me a straightforward explanation with an approximation formula or something.
Comment: @mitzy added the details for you

Probability of long identical substring


Proof that $e^x$ is a transcendental function of $x$?

Question: Let a function $f(x)$ be algebraic if it satisfies an equation of the form $$c_n(x)(f(x))^n + c_{n-1}(x)(f(x))^{n-1} + \cdots + c_0(x)=0,$$ for $c_k(x)$ rational functions of $x$, and let $f$ be called transcendental if it is not algebraic. Is it possible to use this definition directly to show that $e^x$ is transcendental?
One way I have been considering for any complex number is this:
Let $x_0\in\mathbb{C}$ and $x_n=x_0+2\pi i n$, where $n\in\mathbb{Z}$. Hence $x_n\neq x_m$ for all $n\neq m$, but we do have $e^{x_n}=e^{x_m}$ for all $n,m\in\mathbb{Z}$ (since $e^{2\pi i n} = 1$ for all $n\in\mathbb{Z}$). But since the Implicit Function Theorem suggests there exists an exact algebraic formula for $x$ using the above definition of an algebraic function, then $e^x$ can not be algebraic since there are an infinite number of representations $x_n$ of $x$.
Comment: Does $f^n$ mean $f(x)^n$ or $\underbrace{f(f(\cdots f}_n (x)))$ here?
Comment: A slight nitpick: $f$ is algebraic if it satisfies an equation *of the given form*, not just one particular equation.
Comment: No, it just means $(f(x))^n$ in this case.
Answer: One could use the growth at infinity of the function $f:x\mapsto\mathrm e^x$.
Assume that $f$ is algebraic and choose a real number $x\geqslant0$. Then $|f(x)|\geqslant1$ and
$$
|c_n(x)|\,|f(x)|^n\leqslant b(x)|f(x)|^{n-1},\qquad b(x)=\sum\limits_{k=0}^{n-1}|c_k(x)|.
$$
Hence, for every real number $x\geqslant0$ such that $c_n(x)\ne0$, $|f(x)|\leqslant b(x)/|c_n(x)|$. But indeed, $c_n(x)\ne0$ for every real number $x$ large enough and $b(x)/|c_n(x)|$ can grow at most polynomially when the real number $x$ goes to $+\infty$ while $|f(x)|=\mathrm e^x$ grows... well, exponentially. This is a contradiction.
Answer: Here is a purely algebraic proof using only the fact that if $y=e^x$, then $y'=y$, but no analytic facts about the exponential function. In other words it should hold in any differential field.
Let $K$ be a differential field, such that $K=k(x)$ where $k$ is the field of constants of $K$ (ie those numbers whose derivative is 0) and with $x'=1$ (think field of rational functions in $x$). And $K(y)$ is a differential extension with $y'=y.$
First observe that $y\notin K$. For elements of $K$ are of the form $c p/q$ with $p,q$ monic polynomials in $x$ over $k$ and $c$ a constant. If $(p/q)'=p/q,$ then $p'q-pq'=pq.$ But the polynomial on the right has degree $\deg p+\deg q$, while the polynomial on the left has degree at most $\deg p+\deg q-1,$ and the right-hand side is monic, so they are not equal.
Now suppose $y$ were algebraic over $K$, then let the minimal polynomial of $y$ be $y^n+a_{n-1}y^{n-1}+\dotsb+a_0=0.$ Taking the derivative, we have $ny^n+(n-1)a_{n-1}y^{n-1}+a_{n-1}'y^{n-1}+\dotsb+a_0'=0.$ Subtracting from $n$ times the first equation gives $(a_{n-1}-a_{n-1}')y^{n-1}+\dotsb+na_0-a_0'=0.$ We already observed that $y'=y$ has no non-zero solutions in $K$, hence $a_{n-1}-a_{n-1}'\neq 0,$ so we have a monic polynomial of degree $n-1$ annihilating $y$, which contradicts the minimality of our polynomial.
Answer: Let's use your idea: assume that we have $F(y,x) = \sum_{k=0}^N A_k(y) x^k$ such that $F(f(x), x) = 0$. Since $f(\cdot)$ is periodic with period $d$.  we have
$$\sum_{k=0}^N A_k(f(x)) t^k = 0$$
for all $t = x+ m d$, $m \in \mathbb{Z}$, and so the polynomial in $t$
$$\sum_{k=0}^N A_k(f(x)) t^k =0$$
is the zero polynomial, that is $A_k(f(x)) = 0$ for all $x$. Say one of the $A_k(\cdot)$ is analytic and non-zero. Then the set of zeros of $A_k$ is discrete. Moreover, it  contains the image of $f$. We conclude $f$ is constant.
Comment: What are the $A_k$ here, and where have the $(f(x))^n$ for $n=0,1,2,\ldots$ gone? Have they been combined into the $A_k$? So you've rearrange the equation?
Comment: The presumed relation $F(y,x) = 0$ between $x$ and $y = f(x)$ can be considered polynomial. Now rearrange in terms of powers of $x$ as $\sum A_k(y) x^k = 0$ ( recall  $y = f(x)$). So yes, we rearrange.  You might as well consider $x$ as algebraic in terms of $f(x)$.  I am reading again your solution and you pretty much said that.


BES 5.0 and MAPI calls to exchange system

Question: We have been using BES 4.1(5) for a while now and it has been a resource hog on exchange due to high number of MAPI calls. I have heard that BES 5.0 is even worse. the comparison i heard is that BES 4.1 is makes MAPI calls equal to 5 outlook clients per BB user and BES 5.0 makes MAPI calls equal to 10 outlook clients per BB user.
can someone confirm if it is true? is BES 5.0 is really that bad in MAPI calls and for exchange performance. ?
thanks
Answer: With BES 5.0 and Exchange 2010 BES is similar to one Outlook 2007 client in online mode per user.
RIM has a guide that shows the load requirements under different environments:
http://docs.blackberry.com/en/admin/deliverables/8864/BlackBerry_Enterprise_Server_for_Microsoft_Exchange-5.0-US.pdf


Complete ordered space

Question: In their paper A Method for Constructing Ordered Continua , Hart and van Mill give the following definition of ordered continuum:

An ordered continuum is a compact, connected linearly ordered topological space, equivalently, a complete and densely linearly ordered set equipped with the order topology.

My question is a about the word complete.
I know that a linearly ordered space is connected if and only if it is densely ordered and Dedekind complete (every non empty subset that is bounded above admits a supremum). But that is not enough for this definition.
So I think that complete here means complete lattice (every subset has both supremum and infimum), as complete lattices are compact. Is that correct?
If it is, I have another question. Is completeness as a lattice equivalent to completeness in the sense of uniformity (in case the space is indeed uniformizable)?
Thank you!
Answer: Yes, "complete" here means "complete as a lattice".  Alternatively, it just means the variant of Dedekind-complete where you require that any set has a supremum (without requiring the set to be nonempty or bounded).
Completeness in this sense is not equivalent to completeness with respect to a uniformity.  For instance, $\mathbb{R}$ is complete with its standard uniformity, but is not complete as an ordered set in this sense.

Complete ordered space


Desktop sharing where multiple users can share and viewer can view all desktops at once

Question: I was looking for the following feature:
I am interacting with my clients (let's say they are 10 in number). I would like them to share their screens and I would like to view those from my desktop.
Is this possible? If yes, can you please let me know the software that supports this?
Answer: You don't mention budget, so I'll suggest WebEX.  There's multiple flavors, at multiple price-levels, but for ease of use/robustness/feature set, it's pretty tough to beat.
Some other options that may work:

VNC/TightVNC (open source/free, but requires firewall ports to be open)
LogMeIn (lots of options here; no firewall changes required)
Teamviewer (no firewall changes required)
Comment: I can speak from experience that TeamViewer works great. TV is free for personal use, but will limit you by logging your session off and requiring a wait time if you try to connect to domain computers if you do not have a commercial license, which start at about $700. With that license, you can have an infinite number of client computers you connect to. That is a big advantage in that it does not cost more as you add clients. I use it and have such a license.
Comment: One more thing: There are also versions, which run, rather than have to be installed, so even if the users are locked down, you can still help them.
Comment: Thanks a lot. But using TeamViewer will I able to view multiple shared screens(users desktops) at once i.e., 5 windows showing 5 shared screens.
Comment: Doubtful, unless there's a conferencing flavor of it. That's where you'd typically find that feature set, under "web conferencing".
Comment: Thanks a lot for all the helpful suggestions. From what I came across, I couldn't find any solution where I can see multiple sharers at once. 1-many (one sharer - many viewers) is common, but many-1 or many-many (many sharers-many viewers) doesn't seem to be around.
Answer: Microsoft Live meeting will do this as well as WebEx and others as mentioned.  If teh cinet sue Windows, they can also send a Remote Assitance request that would provide access.  However, it requires ports to be opened.  It is free and in Windows XP and later.
Comment: Thanks a lot. Does Live Meeting allow me to see multiple shared screens at once.?


use property value in thymeleaf th:if condition

Question: Basically I have configuration in property file
data.enabled = true

and I have added a POJO class for that
@Configuration
@PropertySource(value = {"classpath:dataconfig.properties"})
public class DataProperties {
    private Boolean enabled;
}

and I want to check the enabled property on html tag using thymeleaf.

Hello

Comment: What is the error?
Comment: i'm getting enabled property null
Comment: Show your Controller Code please... Or at least how you populate the Model.
Answer: First you should add @ConfigurationProperties(prefix="data") to your configuration class.
@Configuration
@PropertySource("classpath:dataconfig.properties")
@ConfigurationProperties(prefix="data")
public class DataProperties {
    private Boolean enabled;

This activates that your variable is directly binded to the property value without using @Value annotation. In your property file you have data.enabled. This means that your prefix is data. So you have to set this, too.
To use the bean in thymeleaf directly you need to use a special command. In your case it should look like that: (see point 5 in the docs)

Hello

Addition 1:
To use the same property in other spring beans like controllers you have to autowire your DataProperties
@Controller
public class IndexController {

    @Autowired
    private DataProperties dataProperties;

    @GetMapping("/")
    public String index() {
        dataProperties.getEnabled();
        return "index";
    }

Just to mention it autowire on a field is bad practice. But its your choice to use it like that or autowire on constructor or setter.
Comment: and how can i use the same property in controllers??


How can I use ExtendedFloationgActionButton with Action Menus

Question: I want to show an action menu when I Press the ExtendedFloatingActionButton as in the image. My menu has 4 items, but I can't figure out how to easily make the FAb expand to display the actions.
I know ExtendedFloatingActionButton is a child class of MaterialButton, rather than FloatingActionButton, therefore working with it slightly differs from using the normal FAB
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        app:backgroundTint="@color/colorAccent"
        android:contentDescription="Menu"
        android:text="Menu"
        app:iconTint="@color/card_background"
        android:textColor="@color/background_card"
        android:textStyle="bold"
        app:icon="@drawable/ic_menu_black_24dp" />

I want to have a result like this:
Answer: I have found an easy implementation for a FloatingActionButton, but not for an ExtendedFloatingActionButton.

FAB transformation | animaiton-samples by android at GitHub


Force insertion point on a textarea

Question: This is a textarea,
<textarea placeholder="what's new..." id="messageID" type="text" ng-model="content"></textarea>

Please how can I trigger insertion point or cursor on this textarea.
Comment: what you mean by insertion point?
Comment: Correct, that is a textarea. Can you explain what you mean by *"Trigger insertion point"* Thank you.
Comment: He means the *caret* or *cursor*, as in this question/answer: http://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area  [I'm assuming - could just mean *focus*] [update: or even something completely different... who knows]
Comment: If I click on the textarea I can not insert text.
Comment: You don't need `textarea` and `type=text` - but that makes no difference.  There's nothing wrong with the code you've provided so must be caused by some other code.  You've not provided enough information to reproduce your issue - try your own code in the question in a *new* project/page.  Read this [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)
Answer:
    <form name="myform">
       <textarea name="mytextfield" placeholder="what's new..." id="messageID" type="text" ng-model="content"></textarea>
    </form>

Comment: @JnG that is plain javascript, the only difference is it's inline. If you want to run something when the DOM is ready you can replace the `onload` attribute method for `window.onload=function(){//Do something here}` in the script tags.
Comment: please post an answer with this idea
Comment: @JnG I would post an answer but I'm not 100% sure what it is you are trying to ask. Also the fact you haven't shown any attempt of fixing the problem yourself, I haven nothing to debug/fix and explain the reason(s) for it not functioning as intended.
Answer: function setSelection(id, start, end) {
    var input = document.getElementById(id);
    input.focus();
    input.setSelectionRange(start, end);
}

with this function you can set the selection begining and end in number of characters from the start of the string (eg. setSelection('messageID', 0, 10) would select the first 10 characters.
If you are using angular you should use other methods to access the DOM input element, bu the setSelectionRange call would be the same.

Force insertion point on a textarea


Anti SQL Injection (Tentei de tudo mas o Havij ainda pega)

Question: Olá!
Estou passando por maus bocados, já tentei TODAS as técnicas que encontrei na internet que possam impedir o SQL Injection, e mesmo assim o Havij consegue pegar meus dados.
Este é o código da página que estou fazendo o ataque:
<code>$id = mysql_real_escape_string($_GET['id']);

$sql = $id;
$sql = preg_replace(sql_regcase("/(from|select|insert|delete|where|drop table|show tables|or|=|#|\*|--|\\\\)/"),"",$sql);
$sql = trim($sql);
$sql = strip_tags($sql);
$sql = addslashes($sql);
$id = $sql;
$id = intval($id);

// Connect to the database
$mysqli     = new mysqli($MySQL_HOST, $MySQL_USER, $MySQL_USER_PASS, $MySQL_DB) or die("Erro ao conectar ao bando de dados");

// Prepare the query
$sql        = $mysqli->prepare('SELECT autor, noticia, data, titulo, tipo, logo FROM noticias WHERE id = ?') or die("erro ao preparar consulta");

// Bind the parameter, i --> int, datatype of column
$sql->bind_param('i', $id);

// Execute SQL
$sql->execute() or die("erro ao executar consulta");

$sql->bind_result($autor, $noticia, $data, $titulo, $tipo, $imagem);

$sql->fetch();

$sql->close();
</code>
Já tentei até tirar o parâmetro direto da consulta do sql, trocando ele por um número fixo. Já tentei tirar a linha de pesquisa, tirar a linha de conexão, e até AMBAS! O site nem sequer funciona mas o havij ataca igual um louco! Não tem lógica que explique!
E pra piorar, ativei os logs do sql achando que ele poderia estar pegando a conexão sozinho e mandando os prompts sozinho ou por outra página, e toda a resposta que tenho é que ele faz meia duzia de pesquisas na tabela com o id 999999, nada diferente!. Agora como um programa pega todo o meu banco de dados com meia duzia de SELECTs em um id inexistente?
Comment: Se você tira a linha da conexão, não tem como a ferramenta injetar SQL por aquela página. Ela pode ter guardado seus dados de acesso de uma tentativa anterior que deu certo.
Comment: Seu log de SQL nao deve estar pegando todas as consultas. Tente usar um monitor de SQL diferente e monitore! Tente alterar a senha do banco, pro caso de ele ter os dados da sua conexão.
Answer: Se você quiser tentar evitar mais ainda uma sql injection, tente utilizar uma "stored procedure", assim o mysql aceitará qualquer código sql como uma variável, independentemente do que for digitado.
Minha especialidade é oracle, e pelo menos no meu caso as vulnerabilidades caem muito ao utilizar uma stored procedure. No caso do Oracle, utilizo um parâmetro NUMERIC, equivalente ao INT para entrada e um parâmetro SYS_REFCURSOR, que seria a consulta como saída. Como a procedure espera um inteiro como parâmetro, ele dará erro na procedure caso seja inserido um sql e a função retornará uma falha. Parece-me que o MySql não precisa desse SYS_REFCURSOR e retorna a consulta como resultado sem precisar da variável de saída


Decoupled AppWidget Intents after Home process restart

Question: My Android app Transdroid offers several home screen widgets. Every AppWidget has 2 'buttons' (ImageButton), one starts the app and one starts some activity that refreshes the AppWidget content. Pretty simple. Here is a screenshot. The widget code is at my Google Code website, but most importantly:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_15);
views.setOnClickPendingIntent(R.id.widget_action, PendingIntent.getActivity(context, 0, new Intent(context, Transdroid.class), 0));
appWidgetManager.updateAppWidget(id, views);

The problem is: the widget's onUpdate is not called after the Home process is restarted, and hence the PendingIntents used to attach functionality to the buttons is lost.
It's fairly easy to reproduce.

Start an emulator
Add a widget (that uses a PendingIntent to, say, start an activity)
Click the button to see it actually works
Force kill the home proces ('adb -e shell kill 96' where 96 is the PID of android.process.acore)
The widget's button doesn't work any more.

More precise: no onReceive and thus no onUpdate is called when the android.process.acore Home process is restarted. In turn, no Intent is attached.
Anyone experienced the same problem and knows how to circumvent this problem?
Comment: In what real-world situation would you expect the Home process to be restarted, without also causing a reboot of the device?
Comment: It seem to happen when the device is low on memory. It will kill the home process to free up space, but when later on returning to the home screen (and thus automatically starting the home process again) my widget isn't updated.
Answer: I've just tested a couple of my own appwidgets, and their click events work fine after killing acore. Here's my relevant code:
    final RemoteViews views = new RemoteViews(context.getPackageName(), layoutId);
    views.setOnClickPendingIntent(R.id.widget_view, PendingIntent.getActivity(
            context,
            0,
            new Intent(context, MyActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
            PendingIntent.FLAG_UPDATE_CURRENT));

The major difference I can see is the intent-related flags; I'd say they are definitely worth trying. [The layoutId parameter in my code is set elsewere; this same code is used for several widgets with different layouts].
Comment: Okay. so I'm not 100% sure what actually happened and why, but the problem was 'fixed' when I started setting the PendingIntents from the background service (dedicated to the widget) as well, as seen in http://code.google.com/p/transdroid/source/browse/trunk/src/org/transdroid/widget/WidgetService.java


Showing Uncaught TypeError: Cannot set property 'selected' of null

Question:
Showing Uncaught TypeError: Cannot set property 'selected' of null

    <select id="sorter" class="sorter-options">
    <option value="name">Product Name </option>
    <option value="price">Price</option>
    <option value="sort_by">Scale</option>
    </select>

Comment: That's reasonable, considering there's no option with empty `value`. And what do you actually want to do with this?
Answer: You don't have an option is val='' and hence the selector returns null which is expected.
You could add a default empty option if you wish

<select id="sorter" class="sorter-options">
    <option value="">Select options</option>
    <option value="name">Product Name </option>
    <option value="price">Price</option>
    <option value="sort_by">Scale</option>
    </select>

or else set the  value to an available option
 var val = 'price';
 document.querySelector('#sorter [value="' + val + '"]').selected =  true;

or else provide a check for existence before checking the selected value.
 var val = '';
 document.querySelector('#sorter [value="' + val + '"]') !== null? document.querySelector('#sorter [value="' + val + '"]').selected =  true: null;

or if you just wish to set the default value use defaultValue on select
<select id="sorter" class="sorter-options" defaultValue="price">

Answer: val is an empty string whereas no option in this snippet have an empty string as value. Either create an option with value as empty string or set a predefined  value of option to val

var val = 'price';
document.querySelector('#sorter [value="' + val + '"]').selected = true;
<select id="sorter" class="sorter-options">
    <option value="name">Product Name </option>
    <option value="price">Price</option>
    <option value="sort_by">Scale</option>
    </select>


Examine whether the identity map is continuous or NOT.

Question: Consider the space $C[0,1]$. Consider the following metrics
$$d_1(f,g)=\int_0^1|f(t)-g(t)|\,dt.$$
$$d_2(f,g)=\left(\int_0^1|f(t)-g(t)|^2\,dt\right)^{1/2}.$$
$$d_3(f,g)=\sup_{0\le t\le 1}|f(t)-g(t)|.$$
Then which are correct ?
(a) $id:(X,d_2)\to (X,d_1)$ is continuous.
(b) $id:(X,d_1)\to (X,d_3)$ is continuous.
(c) $id:(X,d_3)\to (X,d_2)$ is continuous.
I only know that for a Topological space $id:(X,\tau_1)\to (X,\tau_2)$ is continuous if $\tau_1$ is finer than $\tau_2$.
Can we apply this for this problem ?
Or any other technique to test the continuity ?
Update:
According to   Fardad Pouran's comment ,
$$d_2(f,g)=\left(\int_0^1|f(t)-g(t)^2\,dt\right)^{1/2}$$
$$\le \left(\int_0^1\left(\sup_{0\le t \le 1} |f(t)-g(t)|\right)^2\,dt\right)^{1/2}$$
$$=\left(\int_0^1\left( d_3(f,g)\right)^2\,dt\right)^{1/2}$$
$$=d_3(f,g).$$
So,(c) is TRUE.
Again,$$d_1(f,g)=\int_0^1|f(t)-g(t)|\,dt$$
$$\le \int_0^1\sup_{0\le t\le 1}|f(t)-g(t)|\,dt=d_3(f,g).$$
So, (b) is FALSE.
Again, $$d_1(f,g)=\int_0^1|f(t)-g(t)|\,dt$$
$$\le \left(\int_0^1|f(t)-g(t)|^2\,dt\right)^{1/2}=d_3(f,g)$$ , using Cauchy-Schwarz inequlity.
So, (a) is TRUE.
Check whether I am right or wrong ?
Comment: You're right with respect to a) and c) including the proofs. While you are correct with your answer to b), the inequality $d_1(f,g) \leqslant d_3(f,g)$ does not imply that the identity is not continuous from the $d_1$-endowed space to the $d_3$-endowed. Consider $d_4(f,g) = 2\cdot d_1(f,g)$ to see that.
Comment: It seems you should find constants, say $C_{ij}$, such that :
$$d_i(f,g)\leq C_{ij}d_j(f,g)$$
Then you've proved that identity functions are even *Lipschitz*
Comment: Hints: Because each metric comes from a norm, you may as well assume $g = 0$. Now as yourself questions such as, "If $\int |f_{n}| \to 0$, does it follow that $\sup |f_{n}| \to 0$?
Comment: @  Fardad Pouran): Please check my thoughts about my question in UPDATE....
Comment: Well...We know that, " A Topological space $id:(X,\tau_1)\to (X,\tau_2)$ is continuous if $\tau_1$ is finer than $\tau_2$." Again every Topological space is a metric space. So how this result related in metric space?...@ Daniel Fischer
Answer: As mentioned by Daniel Fisher, we cannot conclude that b) is false by the argument given. Instead consider the functions :  $f_n(x) = x^n$ on $[0,1]$.Then $lim_{n\to\infty}||x^n-0||=0$  on $(X,d_1)$ whereas  $lim_{n\to\infty}||x^n-0||=1$ on $(X,d_3)$ ( where the norm is induced by the metric ).
Comment: How $x^n$ is identity function ?
Comment: $x^n$ is not the identity map. $({x^n})$ are elements of $(C[0,1],d_1)$ as well as $(C[0,1],d_3)$.
Comment: Ok.Without creating example , is it possible to check using any theorem that the identity function in **NOT** continuous ? Because for these type of problems in examination hall creating example is very difficult ...
Comment: That's a very good question. I also have the same doubt. will try to find if there is any other way out.
Comment: Ok. I will also try..If you find out then please suggest me here.


Universal generalization proof

Question:
Assume $\Gamma$  is a set of formulas, $P(x)$ a formula, and $\Gamma\vdash P(c)$ has been derived.
The generalization rule states that $\Gamma \vdash \forall x\,P (x)$
can be derived if $c$ does not occur in $\Gamma$.

This is an intuitive rule, since if we can deduce $P(c)$ having no information about the constant $c$, that means $c$ could have any value, and therefore P would be true for any interpretation, that is $\forall x\,P (x)$.
However, I have tried to demonstrate this rule using first order logic, but I think it is impossible since there is no way to directly translate "c does not occur in $\Gamma$" in FOL as far as I know.
What logic system do I need to use in order to demonstrate this?
Where can I find a formal proof for this rule?
Comment: In some proof system, like [Natural Deduction](https://www.iep.utm.edu/nat-ded/#H7) it is a basic rule.
Comment: In different proof systems (the so-called Hilbert-style) it is proved using quantifiers axioms. See e.g [Enderton](https://books.google.it/books?id=dVncCl_EtUkC&pg=PA117), page 117.
Comment: The statement of your quote is kind of problematic, the x should not occur free in P(c).
Comment: That can be inferred from $P(x)$, which means $x$ is the only free variable in $P$.
Comment: You cannot say that a constant like $c$ occurs free. Only a variable can do that.
Comment: That mean 'not occurs', which is different from 'not occurs free'. In that wiki page, it's about the alphabetic variance stuff.
Comment: @Michael Thanks! I have corrected it. But, feel free to edit the post. I took the statement from: https://en.wikipedia.org/wiki/Universal_generalization
Comment: @Michael In your edit, why did you remove the "does not occur in" part?
Comment: @Michael, Ok i get it. But, your edit is just a rephrasing, or the current quote is wrong?
Comment: @Michael, And what about "$c$ is not mentioned in $\Gamma$", like the Wikipedia's definition?
Comment: @Michael I still don't understand why you didn't state in your edit that "c does not occur in Γ".
Comment: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/140563/discussion-between-michael-and-mateleco).
Answer: If we are talking about Hilbert-style proof system, this rule is not an inference rule within the theory. A proof of "if $\Gamma \vdash P(c)$ then $\Gamma \vdash (\forall x)P(x)$" would be a meta-proof, i.e. a proof of the sentence about the first-order theory, but not a sentence in the theory.
So an informal proof is as follows. Since $\Gamma \vdash P(c)$, there exists a proof, i.e. a finite sequence of sentences each of which is either an axiom, or a sentence of $\Gamma$, or a sentence derived by inference rule of the theory. Now we pick a new variabe $y$ such that $y$ does not appear in the proof. This is possible because (1) the proof must be of a finite sequence, hence contains a finite number of variables; (2) in a first-order theory, there are infinitely (though countable) many variables. After we pick such a $y$, we replace $c$ by $y$ in the proof, yielding a new finite sequence of sentences. It is not difficult to verify that this new sequence is indeed a proof in the theory. That is, one has to verify that every sentence in this new sequence is either an axiom, or a sentence of $\Gamma$, or dervied by inference rules. Note $\Gamma$ does not contain the constant symbol $c$. So the above verification is possible. Finally, in this new proof, the last sentence must be $P(y)$. Now applying Gen rule, we can obtain $(\forall y)P(y)$. Deriving $(\forall x)P(x)$ from $(\forall y)P(y)$ is straightforward.
The above meta-proof is "informal" because it is stated in English. I think it is possible to formulate the above proof in a first-order set theory, although I'm not certeain. Nevertheless, I think it suffices to accept such an informal proof, just like other meta-proofs and meta-statements that discuss various properties about the subject-matter first-order theory, such as models, consistency, completeness, etc.
Answer: I don't know exactly what it should mean to 'demonstrate this rule using first order logic'. This is a rule of inference... it is part of the metatheory... it is a statement about first order logic, not in it.
There are a couple adjacent things that make sense.

One could prove that it is an admissible inference rule in a given system of first order logic. This would consist of proving that if a deduction of $P(c)$ exists from a $\Gamma$ where $c$ doesn't appear, then a deduction of $\forall x P(x)$ exists. Oftentimes universal generalization taken as a primitive rule, so this is trivial, but sometimes it is not. In any case one would need to know what the rules and axioms of the system are in order to produce such an argument.
One could prove that the rule is sound semantically, i.e. if $P(c)$ is a logical consequence of $\Gamma$, then $\forall xP(x)$ is as well. So let $M$ be an interpretation where $\Gamma$ holds and $y$ in the domain of $M.$ We will show $P(y)$ holds in $M.$ Consider the interpretation $M'$ which is the same as $M$ except $c$ is interpreted as $y$ instead of whatever $M$ had it as. Since $c$ does not appear in $\Gamma,$ $M'$ still satisfies $\Gamma,$ and so by assumption, $P(c)$ holds in $M',$ and thus so does $P(y).$ And $P(y)$ does not contain $c,$ so $P(y)$ holds in $M.$
Comment: @mateleco It's a proof that if $\Gamma\models P(c)$ then $\Gamma\models \forall x P(x).$
Comment: @mateleco I was thinking of $c$ as a constant, not a variable. But I think you have the right idea (it should just be the intuitive argument, but with attention to the precise definition of satisfaction).
Comment: @mateleco No, $\Gamma \models P(c)$ would not hold here. For that to be the case, $P(c)$ would need to be true for any interpretation that satisfies $\Gamma,$ not just the one you selected where $P(c)$ is true but $P(k)$ (for some other constant symbol $k$) is false. In particular you could choose an interpretation where $c$ is interpreted as whatever $k$ was interpreted as and $P(c)$ would be false there (and it'd still satisfy $\Gamma$ since $c$ doesn't appear so the change has no impact on $\Gamma$).
Comment: @mateleco It varies from treatment to treatment. Way I learned it (and what I think is fairly typical nowadays) is that interpretation and variable assignment are kept separate. There are constant, function, and relation symbols in your language whose meaning is defined by the interpretation. This decides the truth value of any sentence (i.e. something without free variables). Satisfaction/validity of a formula with free variables is not defined. But you can add a variable assignment and talk about satisfaction of a formula in reference to a particular variable assignment.
Comment: @mateleco Now, this doesn't quite play nicely with proof systems that let you derive formulas as well as sentences. But restricted to sentences, the proof system agrees with validity.
Comment: Proving this rule semantically is a nice approach. However, I still don't get why (2) demonstrates that the rule is semantically valid.
Comment: Let me check if I understood: $\Gamma \vDash P(c)$ can be rewritten into "for every possible interpretation (with $c$ in the domain), $\Gamma$ implies $P(c)$. We know that $c$ does not occur in $\Gamma$, so if we start by assigning values to all the variables of the domain except $c$, we will get the truth-value of $\Gamma$ and the $c$ variable will remain free. Assuming $\Gamma \vDash P(c)$, if $\Gamma$ is true, then $P(c)$ must be true for all $c$. This is exactly what the consequent says: $\Gamma \vDash \forall xP(x)$. Therefore, this argument is valid semantically.
Comment: But if $c$ was a constant wouldn't this be a counterexample? Let $\Gamma$ be true, $P(c)$ be true, and $P(k)$ be false. Both $c$ and $k$ are constants. $\Gamma \vDash P(c)$ would be true, but $\Gamma \vDash \forall x(P(x))$ would be false because $P(k)$ is false
Comment: Sorry for asking too much questions but... then, what is the difference in this specific case (if $\Gamma \vDash P(c)$ then $\Gamma \vDash \forall x P(x)$) between setting $c$ as a constant, and setting $c$ as a variable? I mean, I thought only variables could change upon interpretation.
Comment: Hi any idea how to prove this semantically i.e. if Γ⊨() then Γ⊨∀().
Comment: @CherryBlossomBomb If $M\models\Gamma$ and $M\models\lnot\forall xP(x),$ then  $M\models\lnot P(x)$ for some $x\in M.$ Let $M'$ be the same as $M,$ except it interprets $c$ as $x$ rather than whatever $M$ interprets it as. Since $c$ doesn't appear in $\Gamma,$ we still have $M'\models \Gamma,$ but $M'\models \lnot P(c).$

Universal generalization proof


Encadeamento de Promise/Observable em IONIC/Angular

Question: Pessoal sou novo em IONIC/Angular e não entendi ainda muito bem Promise/Observable e gostaria de uma ajuda !
Tenho a seguinte situação:

Usuário clica em um botão que irá buscar informações no SQLite do celular;
É feito um foreach no retorno e chamado uma WebApi (http post) passando para cada registro a API;
Ao terminar o foreach irei executar 2 comandos para deletar as tabelas do SQLite;
E finalmente dar uma mensagem ao usuário

Porém não consigo definir essa sequencia, no meu código ele deleta, da a mensagem ao usuário para depois chamar a API para cada registro. Segue abaixo trecho do código
    SincronizarSistema() {
  let loading: Loading = this.showLoading('Sincronizando...');

  this.voucherService.sincronizarPedidoDetalheSistema()

  this.pedidosDetalhe = [];
  this.pedidosDetalheBase = [];
  this.carregarEventos();
  loading.dismiss();

  this.showAlert('Sincronização realizada com sucesso !')}

sincronizarPedidoDetalheSistema() {
 this.getDB().then(() => {
  let listaPedidosDetalhe: PedidoDetalhe[];

  this.retornaPedidosDetalheBaixados()
    .then((pedidosDetalhe: PedidoDetalhe[]) => {

      listaPedidosDetalhe = pedidosDetalhe

      listaPedidosDetalhe.forEach(element => {
        this.baixaPedidoDetalheSistema(element.idPedidoDetalhe)
          .subscribe(pedidoDetalhe => {
            console.log(`Pedido ${element.idPedidoDetalhe} baixado com sucesso !`)
          }, error => {
            console.log('Erro ao baixar pedido ', error)
          })
      });

      console.log("chegou aqui")
      this.db.executeSql('DELETE FROM tblPedidoDetalhe', [])
      this.db.executeSql('DELETE FROM tblEventoSincronismo', [])

    })
});}

Answer: Amigo, boa noite.
tente assim:
listaPedidosDetalhe.forEach(element => {
    this.baixaPedidoDetalheSistema(element.idPedidoDetalhe)
      .subscribe(pedidoDetalhe => {
        console.log(`Pedido ${element.idPedidoDetalhe} baixado com sucesso !`)
        this.db.executeSql('DELETE FROM tblPedidoDetalhe', []);
        this.db.executeSql('DELETE FROM tblEventoSincronismo', [])!`);
      }, error => {
        console.log('Erro ao baixar pedido ', error)
      })
  });

Comment: Este caso até funcionaria, porém eu iria deletar as bases mais de uma vez sem necessidade (processamento sem necessidade)
Answer: Consegui fazer utilizando forkJoin, porém fiz um teste em um emulador desligando a internet (que neste caso daria erro por não conseguir acessar a API) e não caiu no catch
 this.voucherService.retornaPedidosDetalheBaixados()
    .then((pedidosDetalhe: PedidoDetalhe[]) => {
      this.pedidosDetalheBaixados = pedidosDetalhe
    })
    .then(() => {
      if (this.pedidosDetalheBaixados.length > 0) {
        const calls = [];

        this.pedidosDetalheBaixados.forEach((pedidoDetalhe: PedidoDetalhe) => {
          calls.push(this.voucherService.baixaPedidoDetalheSistema(pedidoDetalhe.idPedidoDetalhe))
        })
        return forkJoin(calls)
          .subscribe((sucess) => {
            this.voucherService.deletaBases();
            this.pedidosDetalhe = [];
            this.pedidosDetalheBase = [];
            this.carregarEventos();
            loading.dismiss();

            this.messageService.showToast('Sincronização realizada com sucesso !', '3000', 'bottom').subscribe(toast => { });
          },(erro) =>{
            this.messageService.showAlert('Não foi possível realizar e sincronização com o sistema')
          })
      } else {
        this.voucherService.deletaBases();
        this.pedidosDetalhe = [];
        this.pedidosDetalheBase = [];
        this.carregarEventos();
        loading.dismiss();

        this.messageService.showToast('Sincronização realizada com sucesso !', '3000', 'bottom').subscribe(toast => { });;
      }
    })
    .catch(() => {
      this.messageService.showAlert('Não foi possível realizar e sincronização com o sistema')
    })


Adding instance-level public SSH keys to GCE instance

Question: Per Managing SSH keys in Metadata  |  Compute Engine Documentation  |  Google Cloud, I edited instance-level public SSH key metadata by adding my public key (in following format):

ssh-rsa [KEY_VALUE] [USERNAME]

yet cannot connect.
/var/log/auth.log:
Apr 18 15:24:19 web0 sshd[5080]: Invalid user staging from X.X.X.X port 52403
Apr 18 15:24:19 web0 sshd[5080]: input_userauth_request: invalid user XXX [preauth]
Apr 18 15:24:19 web0 sshd[5080]: error: Received disconnect from X.X.X.X port 52403:14: No supported authentication methods available [preauth]
Apr 18 15:24:19 web0 sshd[5080]: Disconnected from X.X.X.X port 52403 [preauth]

Please advise.
Comment: Were you able to resolve this? I'm seeing identical behavior
Comment: @BrianTheLion re-created instance(
Comment: I'm seeing this behavior on a fresh instance.
Comment: easier to reinstall) mine wasn't fresh so it was kinda pain..
Answer: When you create your private/public key pair using a command such as the below:
ssh-keygen -t rsa -C "firstname.lastname@example.com"

Your email address should already be present in the generated public key file (generally named id_rsa.pub by default).
You should therefore only need to copy the contents of this file and paste it straight into the instance ssh-keys section in the Console. So yes, the format you've written is correct, but the most reliable way to ensure you are inputting it correctly is to copy it straight from the public key file.
One method to validate you have entered the public key into the instance SSH keys section is to check your username appears to the left of the box you are pasting the public key into, as displayed in your linked article.
If it doesn't appear, this may be a clue that the key format is wrong. If this is the case, one thing I would suggest is checking there are no new lines in the pasted key. You should be able to place the cursor at the start of the pasted key in the box and press the right arrow key from start to finish. If there are any new lines remove them so that the key is continuous.
If none of the above is relevant some additional information would be useful. From where are you trying to connect? Also, what command are you using to connect to the instance?
EDIT:
As the user from the SSH key isn't appearing in the /etc/passwd file, this suggests that it may be possible that relevant services of the Linux Guest Environment are not running, which in turn means that metadata is not propagating to the instance. I believe the Compute Engine Accounts Daemon within the Linux Guest Environment may be responsible for this issue.
You can try running the following command to see if it is active:
sudo systemctl list-units --all | grep google

If it is not active/installed there is some useful information here about how to start the services, or install the Linux Guest Environment if it is not already installed on the instance.
Comment: The username appears on left of the pasted key inside of my instance. I don't think issue with connecting, I believe it's more with instance as I don't even see my username inside of `/etc/passwd` and that explains `invalid user` message.


Interpretation of Baseline Reference Group

Question: I have more of a theoretical question regarding the use of dummy regression. I know that one level of a categorical variable is used as a reference group, and the means of all subsequent levels are compared to the mean of the reference group, and that these differences give different p-values.
I understand how each level in a categorical variable is interpreted for a categorical variable, but I am not sure how the p-value in the reference group should be interpreted. This may be too simplistic, but I thought the interpretation of a reference group was not meaningful because I believe the mean is being compared with itself, and thus not useful. Is this an inappropriate way of conceptually understanding my question?
Answer: You can think of the coefficient for the reference group as comparing the reference group mean to 0. So the t-statistic and p-value for that coefficient reflect the null hypothesis that the mean of the reference group = 0.


$T$ automorphism of a finitely generated $A$-module, then $T^{-1} \in A[T]$

Question:
Let $M$ be a finitely generated $A$-module. Let $T: M \to M$ be an isomorphism. Then $T^{-1}$ is a polynomial in $T$ with coefficients in $A$.

This seems to me as a direct application of proposition 2.4 in Atiyah-Macdonald, which says that $T$ satisfies an equation of the form $T^n+a_1T^{n-1}+\cdots+a_n=0$. Indeed, we just multiply by $T^{-1}$ and get the required identity.
However, I was told that a solution exists using integral extensions, to show that $T^{-1} \in A[T]$ by looking at $A \leq A[T] \leq A[T,T^{-1}]$ and using the correspondence of prime ideals between integral extensions (5.10 in Atiyah's book). This feels like an unrelated fact which I cannot utilize here.
So, is the 'easy proof' correct? And how can it be proven using the guidance?
Comment: How does multiplying by $T^{-1}$ help? It would work if $a_n$ is a unit.
Comment: @MattSamuel: I think Leullame's argument rather shows that $T$ is a polynomial in $T^{-1}$. But this can be applied to $T^{-1}$ instead of $T$, and we get the claim.
Answer: As mentioned in the comments, your easy proof isn't quite right because $a_n$ might not be a unit.  However, you can multiply the equation by $T^{1-n}$ and get that $T$ is a polynomial in $T^{-1}$, and then swapping the roles of $T$ and $T^{-1}$ gives the desired result.
For the suggested proof, suppose $T^{-1}$ could not be expressed as a polynomial in $T$.  This means $T$ is not a unit in $A[T]$, so there is some prime $P\subset A[T]$ containing $T$.  By Proposition 2.4 applied to $T^{-1}$, $T^{-1}$ is integral over $A$, so $A[T,T^{-1}]$ is integral over $A[T]$.  Thus there exists a prime $Q\subset A[T,T^{-1}]$ such that $Q\cap A[T]=P$.  But since $T$ is a unit in $A[T,T^{-1}]$, no prime can contain $T$, so no such $Q$ can exist.
Comment: If you know that $T^{-1}$ is integral over $A$, then you already have the result by the OPs "easy" proof. It wouldn't make sense to use the big machinery that comes with integrality.
Comment: I agree, OP's proof is easier.  But OP asked for a proof in terms of the properties of prime ideals between integral extensions, and this is that proof.  And it seems rather likely to me that this is what whoever told them that such a proof exists had in mind.  After all, they were told to look at $A\leq A[T]\leq A[T,T^{-1}]$, so presumably they need to know that these extensions are integral.


Argument of type 'Http' is not assignable to parameter of type 'Http'

Question: I have following error:

[ts] Argument of type 'Http' is not assignable to parameter of type
  'Http'.   Property '_backend' is protected but type 'Http' is not a
  class derived from 'Http'. (parameter) http: Http

This error is in file auth.module.ts
import { NgModule } from '@angular/core';
import { Http, RequestOptions } from '@angular/http';

import { AuthHttp, AuthConfig } from 'angular2-jwt'

import { AuthService } from './auth.service';
import { AuthGuard } from './auth.guard';
import { RoleGuard } from './role.guard';
import { GuestGuard } from './guest.guard';

export function authHttpServiceFactory( http: Http, options: RequestOptions) {
  return new AuthHttp(new AuthConfig(), http, options);
}

@NgModule({
  providers: [
    AuthService,
    AuthGuard,
    RoleGuard,
    GuestGuard,
    {
      provide: AuthHttp,
      useFactory: authHttpServiceFactory,
      deps: [Http, RequestOptions]
    }
  ]
})
export class AuthModule {}

What is wrong?
Comment: What versions of angular and angular2-jwt are you using?
Comment: "angular2-jwt": "^0.2.0" package.json https://pastebin.com/gYiqMV5m
Comment: I can't see any obvious errors in your code, but I can't see the versions of your angular modules in the package.json you posted. Is your angular modules from before version `2.4.2`?
Comment: How can I check version?
Comment: Try running this in the root directory of your application: `cat node_modules/@angular/*/package.json|grep -E "((^ {2}\"name\")|(^ {2}\"version\"))"` to get the version per angular module and paste the result
Answer: Check if you don't have any library referenced with npm link. There is issue with linked dependencies.
Would love to help more but I'm currently struggling with the same issue in different setup.


How to retrieve and set the parameters of LSTMCell?

Question: I would like to retrive the parameters of BasicLSTMCell, and to initialize an object with given parameters with python.
I already looked to Tensorflow Github and BasicLSTMCell guide, but I'm not able to understand how to do it.
Answer: This question has already been answered here
<code>cell = rnn_cell.BasicLSTMCell(num_nodes)

with tf.variable_scope("LSTM") as vs:
# Execute the LSTM cell here in any way, for example:
for i in range(num_steps):
    output[i], state = cell(input_data[i], state)

# Retrieve just the LSTM variables.
lstm_variables = [v for v in tf.all_variables()
                if v.name.startswith(vs.name)]

# [..]
# Initialize the LSTM variables.
tf.initialize_variables(lstm_variables)
</code>


Removing a string inside a comma delimited string

Question: I want to remove a string inside a comma delimited string and I want to do this via regex.
   var strToReplace = 'bbb';
   var str = 'values["aaa","bbb","ccc"]';

The problem I encountered was the commas.
Replacing only the desired string leaves the commas behind so I added removing also the following comma if present.
   var replaceRegexp = new RegExp('"'+ strToReplace +'",?');
   str = str.replace(replaceRegexp,'');

However it leaves a trailing comma if it was the last item. Adding it to the front will have the same problem if removing the first item and adding to both sides will remove the commas when removing from the the middle items.
I just added another replace string to remove the trailing one after removing the item.
   str = str.replace('",]','"]');

Can someone point me to a more elegant way to handle this in just one regExp. Thanks
EDIT:
   var str = 'values["aaa","bbb","ccc"]';
   var str = 'values["bbb"]';
   var str = 'values["aaa","bbb","ccc","bbb"]';

Comment: What about these: `values['aaa','bbb','ccc']`, `values["aaa",  "bbb"  ,"ccc"]`, `values["bbb"]`, `values['"aaa","bbb","ccc"']` ?
Comment: Thanks Kobi, for my sample it will only use double quotes but values["bbb"] is valid.
Answer: You need to use alternation operator |.
String you want to replace is at the middle
> var strToReplace = 'bbb';
undefined
> var str = 'values["aaa","bbb","ccc"]';
undefined
> var replaceRegexp = new RegExp('"'+ strToReplace +'",|,"'+ strToReplace +'"', 'g');
undefined
> str.replace(replaceRegexp,'');
'values["aaa","ccc"]'

String you want to replace is at the last
> var str = 'values["aaa","ccc","bbb"]';
undefined
> var strToReplace = 'bbb';
undefined
> var replaceRegexp = new RegExp('"'+ strToReplace +'",|,"'+ strToReplace +'"', 'g');
undefined
> str.replace(replaceRegexp,'');
'values["aaa","ccc"]'

String you want to replace is at the first
> var strToReplace = 'bbb';
undefined
> var str = 'values["bbb","aaa","ccc"]';
undefined
> var replaceRegexp = new RegExp('"'+ strToReplace +'",|,"'+ strToReplace +'"', 'g');
undefined
> str.replace(replaceRegexp,'');
'values["aaa","ccc"]'

| called alternation or logical or operator. But the logical OR operator is a must needed one.
At first, the regex engine would try to match all the chars using the pattern before the | symbol. After that, it tries to match the remaining characters (in this case) using the  pattern which was next to the | operator.
So the first part of the above regex "bbb", matches all the bbb, strings but it fails to match the last bbb because it isn't have a following comma. Then the next pattern ",bbb"comes into attack and matches the last ,bbb string.
OR
YOu could use the below regex if you want to remove "bbb" if the list has only "bbb"
> var str = 'values["bbb"]';
undefined
> var replaceRegexp = new RegExp('"'+ strToReplace +'",|,?"'+ strToReplace +'"', 'g');
undefined
> str.replace(replaceRegexp,'');
'values[]'

Comment: Add a 'g' flag to make it replace multiple instances and remove the unecessary lookahead and this is the correct answer.
Comment: Thanks. Hitting myself in the head now. how could I forget about |. The positive assertion is new to me. I don't think it is really needed but can you explain this further. Thanks
Comment: Kobi just pointed out in the commment a valid scenario which is values["bbb"] but this does not cover it.
Comment: I think @Makaze post was more on the spot but he deleted it.
Comment: Mine didn't cover a case where bbb was in the middle.

I'll go ahead and make one that catches all of the cases then.
Comment: Yours does. Mine doesn't.
Comment: The explanation was much appreciated Avinash. Lookahead was new to me. Vks just covered all scenarios including the one i left out.
Comment: Done. I made use of the regex function capability to detect if there were any commas surrounding it and replace them with a single comma if there were.
Answer: "bbb",|,?"bbb"

You can try something like this which will fulfll all conditions.See demo.
https://regex101.com/r/vN3sH3/40
Comment: Thanks. I think this one covers most of the scenarios so far.
Comment: @DRTauli is there anyone left?
Answer: This is optimimal in my opinion.

var strToReplace = 'bbb',
  re = new RegExp('(,?)"' + strToReplace + '"(,?)', 'g'),
  str = 'values["bbb","aaa","ccc","bbb","ccc","aaa","bbb"]';

str = str.replace(re, function(match, sub1, sub2) {
    if (!sub1.length || !sub2.length) {
        return '';
    } else {
        return ',';
    }
});

document.getElementById('result').textContent = str;
console.log(str);

Answer: This is the regex used in my javascript code and it works for me, given finding string is "findme":
/^findme,|,findme$|,findme(?=,)/

And code snippet like this:
var find = form1.find.value;
var source = form1.source.value;
var regex = new RegExp("^"+find+",|,"+find+"$|,"+find+"(?=,)");
var result = source.replace(regex, "");


Python - video processing. How to: 1) change pixels value in videos (ie pixelated effect), and then 2) retrieve every single pixel's information

Question: This question is related to an actual science project.
1. The video will be done whit the medium of a white moving object against a black background surface. The final results will be close to a black/white output but I also need to render the video into an actual dual-color channel video (only displaying black and white pixels). I guess it should also be useful to delete/reduce noise (unrelated white pixels caused by light interference)
2. After that, I need to modify the pixels values in order to get a pixelated effect
3. at this very point, I need to obtain all the pixel values from all the frames, maybe arranged in a matrix fashion because I will need to perform some computational math on these matrices
let me show you an example:
<code> frame 1. (0 for black pixel values, 1 for white pixel values)
              01110000
              01111001
              01100011
              11101111
              01100000
              10000000

frame 2.      00111000
              01011100
              11100000
              10010100
              10000101
              01010101
</code>
and so on...
Comment: you should post example data (images/video). unaltered data if possible. something that shows the problem and is usable in potential answers to demonstrate a solution. that ascii art hardly shows anything and you aren't even pointing out what it's supposed to show.
Answer: <code># You could store it in a list like this:

frame1 = [.01110000, .01111001] # and so on. Because an int can't start on zero, I made these decilams

# To change the first item in this list do the following

frame1[0] = .01110011 # This will be the new value and the current list looks like this:
# frame1 = [.01110011, .01111001]

# To retrieve information about every element in the list simply use a for loop
n = -1
for element in frame1:
    n += 1
    element = frame1[n]
    print(element)
</code>
Comment: I really don't get what you're asking, so I did my best with the information at hand. How can I improve my answer to help you with your question?
Comment: I'm not the one who asked the question. that would be @Enzo. I'm just here because I look for interesting questions in the opencv tag. I'll repeat my point: this answer doesn't even address the question.
Comment: @ChristophRackwitz you are right. Samuel was very kind but failed in giving back a useful answer.
Comment: @Samuel could you please tell me what you don't get in the question? try to be as specific as possible and I will do the same in return


Translation of Menu items

Question: HI I'm trying to work out how to translate my menu items, weirdly Log Out appears to be the only one which translated from the onset. But I can't see where this was implemented. If I search for the menu name as a string in transate interface/transalte they can't be found. and editing a menu offers no extra fields for translate options??
any body know?
Answer: First of all, when you search for translations they are case sensitive. don't include aditional spaces, it has to be exact, but it can be partial.
If one translation was previously made and the same word appears in many places it will list it, but what applies is the first translation made (sorted by time)
You have to make sure that your Log Out is really a tranlation
in my case it appears in chain: /admin/settings/language as Log out @username, so if I translate this, it works in the other languages
there is another Log out in the chain with the same name...so be careful
Comment: Thanks for the tip on case sensitivity, I've used this but still I cannot pick out my menu items? Its a bit weird really, I managed to find a menu item which is listed under my User Menu and is created there from a View. But my other items just arn't available? I can see no settings under the Structure Admin menu area so I'm totally at a loss here!?
Answer: OK Its another Module thats required. The Module is called Internationalization this allows you to customise menus/taxonomy and lots of other translation features.

Translation of Menu items