How to unlearn incorrect muscle memory

Question: So I've been learning to play piano for a few months now. And there's this one, very easy piece, which I have absolutely no difficulties playing, except for. one. freaking. transition. which I somehow struggle with, even though it's extremely simple.
What do I mean with "struggle"? I can play the whole piece on a steady 76 bpm (as it's meant to be played,) without thinking about fingering or anything. Except in one spot, where either I play the left hand wrong, or I need to think about it and drop out of the rhythm. The problem is only with the left hand.
Here's the malevolent bit:

Seriously. And the thing is, I even know what goes wrong, I just don't know how to fix it. Somehow my brain ALWAYS mixes it up with this measure:

As you can see it's similar; in m7 your l.h. goes from Bb to the dyad F-D, and in m11 it goes from Ab to Eb-C.
For some reason my brain got wired to go to Eb-C in m7 too and I always trip over it. Every time.
So, how can I unlearn this? How can I reset my idiotic brain in not doing that anymore?
I've tried repeating just that transition for dozens of times, hoping to burn it into my synapses, but nope. During the repetition no problem at all. The moment I start playing the full song, my brain trips again.
Please help a desperate man out! :)
The full score can be found here: https://musescore.com/user/24361621/scores/5833365
Comment: I wonder if a visual cue might help.  I notice that the score doesn't use courtesy accidentals.  Try pencilling in a flat sign before the bass note in bar 7.
Comment: From a personal experience with guitar, I'd recommend playing it correctly at a slower bpm. It takes only a few days to get back into tempo after that. Also I'd recommend to read a bit about [spaced repetition](https://en.wikipedia.org/wiki/Spaced_repetition), because it's very applicable to development of any fine motor skills.
Answer: You may be interested in checking out some of the pedagogy of Arnold Jacobs, the long-time tuba player of the Chicago Symphony Orchestra. I know this seems unrelated to your question, but he discussed at length the role of muscle memory in musical performance and how to overcome issues related to it.
One of his big concepts was that you can't really unlearn something; rather, you just have to learn something new such that it replaces the old habit.

Herein lays one of the great paradoxes of the Jacobs philosophy. It is incredibly simple, but not easy to change long-held habits that hold a player back. Playing a musical instrument, to use Jacobs's lexicon, is a series of conditioned responses developed through a trial-and-error process in response to various stimuli. Most players came to Jacobs with conditioned responses that were getting in their way. They substituted tension and pressure for wind (air in motion). They played by pushing valves vs. conceiving of a clear pitch and sending that into the brass instrument. They substituted moving their body for taking a full, seamless breath. Some people could play despite these bad habits, but none reached their potential until new habits were developed over time — new conditioned responses developed to a new set of stimuli. Jacobs's concept of teaching not by breaking old habits but replacing them with new, better habits was an early musical application of what we know today as neuroplasticity.
...
Through careful practice and repetition, the new way would eventually replace the old habit and create a new, conditioned response and a new pattern of neuronal activity. One of Jacobs' frequent comments was, "Don't correct what's wrong; go for what's right," and this was reinforced by urging students to rehearse success and not failure in practice. Dr. Frank Diaz, a music educator who has written numerous scholarly articles on the psychology of music, said, "Jacobs' notions on creating new habits rather than erasing old ones, and on using top-down approaches (mind controls meat) as a way of creating these new neural maps were insightful."

The above quote is from a celebration page from the Chicago Symphony Orchestra, which continues on at some length about Jacobs's approach to this. You may also be interested in this blog post, and I bet you could find multiple other sources by searching "arnold jacobs habit" or something along those lines.
In any event, consider taking some of "Jake"'s advice and simplify matters. Maybe just play the left hand, starting in m. 1 and stopping on beat 2 of m. 7. Then perhaps from m. 6 into m. 11, and then connect the two. Once your left hand has learned its new habit, add in the right hand and see how things go.
This isn't a foolproof answer, of course, but I thought it would be worthwhile to share a new way of thinking about problems like this.
Comment: I was going to say something similar, but without a solid reference. Btw replacement of the wrong with the right is a good mental and physical strategy in many areas besides music.
Comment: I like the concept. No wonder 'unlearning' something is 10x more difficult than 'learning' it (wrongly) initially. It's apparently not eradicated from memory, but has to imprint itself better than the original did! No better reason for not getting it right first, is there? +1.
Comment: I feel a bit bad about not marking this as the correct answer, but I'll explain why: though the part about Jacob's approach is no doubt the most useful, to me it remains a bit abstract. And I tried the concrete approach Richard himself gave, but it didn't really help me further. The marked answer (basically, go slow again) however did.
Answer: There's a practice technique called "building a bridge".  I bet you can play m7 on its own just fine.  The challenge is to bridge over that measure: to get from m6 to m8 without falling in a hole.  What you need is secure foundations on either side.  The problem isn't m7 itself, it's something in your state of mind as you approach it.
Try this:

Start playing from m8.  When you can do this without a mistake,
repeat it three more times to reinforce it.
Now start playing from m7. Get used to transitioning from m7 to m8.
Once you've got it, repeat at least another three times.
Now start playing from m6. Once you've done this a few times, you've
got your bridge.  But you're not done yet: you need to get used to
approaching the bridge and using it.
Finally, play from a few measures earlier (or from the very beginning
if you like), and remember that you've got a new bridge.

At some point you'll probably trip up, and then it's worth repeating all the steps from the beginning, probably every day for a few days.  You need to be patient with yourself when making this kind of change.
The most important step is actually the first one.  You'll be surprised how much it helps to have a secure "landing pad", more than practicing the transition itself.
Comment: **This**. I refer to this as 'practising backwards' but it's the same thing. It's a very powerful technique. The most important thing is not to rush it. Make sure you can do each stage _without fail_ before you move onto the next. If you make a mistake, back off a little to the point where you were not making a mistake. The key is never to move on faster than you can manage _without_ making mistakes. By the time you have finished, you will have played it correctly _many, many_ more times than you ever played it incorrectly.
Answer: Very down-to-earth and an answer from experience, play the piece really slowly and correctly. It might be really irritating and boring. But do it at least twenty times correctly. Then slowly increase the tempo little by little. Your muscles will automatically overwrite its memory
A common misconception is that practicing a piece at a high tempo will make you learn faster. That is false. Once you learn to play a piece fast, you will find it tough to play it slowly, but vice versa is not true. If you practice the piece really slowly, you will never learn the wrong things, and playing fast soon becomes really really easy.
As I said, practicing slowly is really irritating and you might even, sometimes lose your marbles and hate the piece itself. (Experience: Like how I hated Moonlight Sonata while practicing it but even if you ask me to play it with my eyes closed, I can:D)
"I hated every minute of training, but I said, 'Don't quit. Suffer now and live the rest of your life as a champion."- Muhammed Ali
Answer: Richard's answer is excellent; I just wanted to add a small note to expand on it. I encourage you to think about the physicality of the passage. To mistakenly jump to the Eb requires you move your left hand several notes down the keyboard, while the correct change from B to Bb is only a half step. Surely by now you know the passage from memory. For a bit, do what you're always told not to: look at your hands. Watch them move or not move. Feel the muscular realities of the two.
Keep in mind that the "dots on the page" are just an abstraction. For music to happen in actual time, the only realities are our bodies and the vibrations of sound waves. So often we read from the printed page and think in terms of "B" or "E," but the mechanical reality is the difference between moving an inch more than the previous measure or of moving your hand four inches. Since this problem is about so-called "muscle memory," bring your muscles into the conversation.
Comment: "…the correct change […] is probably an extension"  I can't speak for OP, but as a pianist with small-to-medium hands, the left hand pattern in this excerpt needs at least a wide rocking motion to go between the bass note and the upper notes of the chord; so some hand movement is happening anyway, and it doesn't feel like just an "extension".
Comment: Good point. That's what happens when a violinist is too lazy to walk over to the piano actually try it. I'll revise…
Answer: The ideas presented here are based on the same principle as presented in How to avoid stopping when I play a wrong note on the piano. I recommend reading that answer in addition to this one, possibly before reading this one.

The core of the problem
Problems like this one are often attributed to muscle memory, and physical solutions are the typical prescription. However, in the absence of a genuine physical limitation (for example, an injury that somehow prevents accurately playing certain notes), there is near always a deeper problem that, when solved, allows the muscle memory to take care of itself or at least be much easier to fix. That deeper problem lies in one's emotional understanding of the music. Emotions drive the movements we make to play the piano. (If not, then we're just robots mechanically following instructions.) Confusing two notes/measures/sections after having learned the mechanics of them means we associate or confuse them on a felt level.
To most efficiently fix the problem requires understanding the "meaning", "feel", "story", "picture", "color", or other emotional association(s) of the two measures. What makes the two measures feel different from each other?

CAVEAT: What follows is based on my interpretation of the piece. Yours may be different. The goal here is to demonstrate an approach, not to dictate an interpretation. The key is that you practice hearing/feeling the interpretation you find best fits for you; this is how you will fix the problem.

How do we find the difference between measures 7 and 11?
What is the larger context?
The whole piece
The title of the piece is "Lamentation for a Lost Life", and the feel of the music bears this this out: slow, sad, nostalgic, but also, in seeming contradiction, dance-like.
Within the piece, we can hear three main sections: a dance-like, nostalgic melody that feels somehow incomplete (measures 3–18); a section that feels similar, but very hollowed out (measures 19–34); and a return to the first section (measures 35–50). It's worth noting that while all three section share the identical left hand part, the middle section right hand is a stripped down ("hollowed out") variation of the melody of the first section.
The emotional progression of the piece is: bittersweet, hollow, bittersweet.
The first section
Since measures 7 and 11 are both part of the first, bittersweet section, we'll focus on that.
As with the piece as a whole, we can hear three sub-sections: a sad little dance that trails off (measures 3–6); a slightly happier repetition of the little dance which again trails off — perhaps an attempt to feel a greater presence of whomever was lost, but ending in just feeling the loss again (measures 7–10), and an attempt to revive the dance (feel the presence of the lost life) that keeps sinking lower and finally peters out (in a sense of loss) (measures 11–18).
The first section is sad, tries to be a bit happier by feeling the presence of the lost life, and then tries even harder to be happier (feel more presence), failing every time (experiencing loss).
What does this mean for measure 7?
Measure 7 is the beginning of a happier attempt at the little dance. Since it's trying for "happier", I would add some energy. I would do this by making measure 7 just a bit louder than measure 3 (note the shift from minor [measure 3] to major [measure 7]). I also might press the tempo very, very slightly in measure 7. Speeding up and playing louder both add energy — greater musical "presence" — and happiness (felt presence) is more energetic than sadness (loss).
Measure 7 is has a bit more energy/presence than measure 3.
What does this mean for measure 11?
Measure 11 not only kicks off another attempt at a happier memory, but it's a more earnest attempt: 8 measures instead of 4. And it tries to use only the dance part (presence) of the initial musical idea (measures 3-4 and 7-8), avoiding the "trailing off" (absence/loss) part (measure 5-6 and 9–10). The measure 11–18 section repeats the dance idea three times, but each time lower and lower in pitch. There's a tension between the effort of trying again and again, and the "defeat" of sinking lower and lower.
Measure 11 needs more energy than measure 7, because its the beginning of the most substantial emotional effort so far.
What is the musical difference between measures 7 and 11?
Measure 7 needs enough energy to support a four-measure idea that starts off a little bit happier or warmer than measure 3, but ends by fading away.
Measure 11 needs enough energy to sustain an eighth-measure idea that makes a substantial effort at happiness or warmth.
I would play measure 7 a slight bit louder than measure 3, and measure 11 would be louder than measure 7. I would also consider subtly pushing the tempo in measures 7 and 11 to add some momentum at the beginning of their respective phrases. Again, measure 11 needs a little more momentum than measure 7.
How to practice this?

Play measure 7 with the emotion you want behind it. If this takes more than 3–5 tries, you likely need to look deeper for what else might be mission at the emotional/interpretive level.
Play measure 11 with the emotion you want behind it. If this takes more than 3–5 tries, you likely need to look deeper for what else might be mission at the emotional/interpretive level.
Play measure 7 and then measure 11 (back to back, but with a pause in between), making sure you can feel/hear the difference between them. If this takes more than 3–5 tries, ....
Play the first phrase with the overall musical "shape" you want. If this....

A warning
Invariably, fixing problems in this way "creates" other problems. That is, deepening one's emotional understanding of a note, a chord, a measure, etc. will automatically reveal other notes/chords/measures/... that haven't yet achieved the same depth of understanding.
Comment: What a great, in-depth answer. Thank you Aaron to take the time to delve into this. And I have no doubt you're right. But (you knew there was a "but" right?) I'm not really dealing with expression yet, since I always try to practice a piece until I know it blindly before even thinking about emotion yet. And this hiccup was preventing me from going further. But yeah maybe you're right and that's exactly what I should try for now.
Comment: @Creynders I'm gratified to know my post was helpful. I hear you loud and clear about learning the notes first, then adding expression. Perhaps this will be a transition for you, having encountered and understood an example of where the expression is required first in order to learn the notes.
Answer: I can probably relate this to something more "native" to stackexchange.  This sounds like it is not all that different from switching from Vi text editor to Emacs text editor, for a person who uses both.  I can speak from some experience on that topic.
Both editors are effectively controlled exclusively by use of keyboard shortcuts, which often become learned muscle memory for common tasks.
A person using one editor exclusively for a prolonged period of time, when switching to the other one, inevitably presses the shortcuts common in the previous one.  Initial period of switching requires a conscious effort for a period of a few days to a few weeks of exclusive use to re-adjust (relearn) the muscle memory.
Even in the medium term the mistakes occasionally continue to happen.  They disappear virtually entirely after enough time has passed that enough of the more esoteric shortcuts, used in the old editor, become forgotten.  Which is not to say the forgotten shortcuts do not get used.  But rather, after the uncommon shortcuts are forgotten, the common ones stop creeping in as mistakes.  This takes a period of a few months of exclusive use.
If the new use is not exclusive, then the effort may have to continue to be conscious.  Occasional mistakes are inevitable. But they become less common over time simply because the conscious effort required to distinguish which editor is being used becomes part of the learned process.
Comment: And while we're discussing EMACS, ... one of my all-time favorites: https://www.gnu.org/fun/jokes/ed-msg.en.html
Comment: This is a nice analogy, but doesn't seem to point to a solution.
Comment: @Aaron I thought it would.  It seems like practicing until it becomes natural is the solution.  It may be frustrating at first, but knowing that, with time, practice does solve the problem justifies hoping that it does.
Comment: "Practice and time" doesn't answer the question; *how to practice* is what the OP is looking for.
Comment: @Aaron co-incidentally, I, too, use a 110 baud modem when I browse stackexchange.com.  The parts are a little difficult to get nowadays, but replacing them is half the fun.
Answer: Muscle memory is forever but you can overwrite it.  However, it is always there waiting to resurface especially when we are cold or nervous.  That is why things can go haywire during a performance because we let the old hardwired movements take over and anarchy reigns.  This is also where injury can occur because as you improve your technique, the old muscles that you don't need anymore atrophy and are no longer there to support improper movement. You may wake up the next morning with stiff arms or hand tendons.  Something to be cognizant of is playing old repertoire.  If you learned a work with improper movement, that movement will be hardwired into that song forever.  Unless you take the time to relearn a piece, steer away from former repertoire works.
Comment: Sorry, this doesn't match my experience.  Neither the injury risk nor the "muscle memory is forever" make sense to me. I can think of many occasions when I've returned to old repertoire and been able to play it better because my technique has improved in the intervening years, and this works without having to relearn the piece.
Comment: @Dekkadeci, there might be an element of self-fulfilling prophecy there.  If you talk yourself into playing something badly, you will indeed play it badly.  Certainly "muscle memory is forever" was received wisdom amongst piano teachers when I was a student.  But since then, there's decades of research in sports psychology and brain plasticity, and nowadays we know that adults are capable of learning and adapting.  Believing that you can actually do it is the first step.
Comment: @AlexanderHanysz - I guess your experience is different from Malcolm Kogut's, then. Kogut consistently says in his answers that "muscle memory is forever", then occasionally backs this up with an anecdote from his own experience. It's somewhat likely that some people's old muscle memory must indeed be continuously suppressed, and you're lucky you're not one of them.
Comment: This doesn't address the question of how to go about fixing the problem OP has encountered.

How to unlearn incorrect muscle memory


Why does XGBoost with cross-validation perform worse on test holdout than unvalidated model?

Question: I have an XGBoost model that I fit on some X data directly out of the box:
xgb_outofbox = XGBClassifier(random_state=0).fit(X_tr, y_tr)

This model yields an F1-score of 0.626 on my test holdout data.
Then I build another model:
cv_params = {'learning_rate': [0.1, 0.2, 0.3],
             'max_depth': [2,3,4,5],
             'min_child_weight': [1,2,3,4]
             'n_estimators': [50,75,100,125]
            }
xbg_cv = GridSearchCV(XGBClassifier(random_state=0), cv_params, scoring='f1', cv=5)
xgb_cv.fit(X_tr, y_tr)

This model gives an F1-score of 0.614.
Note: the train_test_split was performed with stratify=y and the entire dataset was shuffled prior to modeling.
I have 2 questions:

Why would the cross-validated model perform worse than the original when it was explicitly set to optimize F1-score?

The parameters in cv_params include the exact parameters of xgb_outofbox for each item. When I call xgb_cv.best_estimator_, I get different hyperparameters than were used for the out-of-box model. Why wouldn't the cross-validated model settle on the same hyperparameters used by the out-of-box model if they yield a higher F1 score?

All other settings should be the same. I didn't specify anything differently except what I mentioned above.
Comment: 0. Welcome to CV.SE. 1. Nice question (+1), you are correct to question this behaviour. Please see my answer below for more details.
Answer: The cross-validated model performs worse than the "out-of-the-box" model likely because by default max_depth is 6. So when the classifier is fitted "out-of-the-box", we have more expressive base learners. In addition to that, please note that the cross-validated model is not necessarily optimal for a single hold-out test-set. It might be the case that the "out-of-the-box/depth-6" model overfits the training folds during cross-validation so it has a lower CV score than some of the less expressive models (e.g. with "depth-4") but appears better in the hold-out set.
Comment: OK, I am a bit surprised because as mentioned the default is `6` at the time of writing this (see https://xgboost.readthedocs.io/en/stable/parameter.html) but I appreciate that this is likely not what happened then. Yes, it is understood that it is fitted "once" and not via CV. As mentioned in my answer the fact that one model outperforms another model in a single hold-out set doesn't mean that is generally better, overfitting our test set can be an issue. The performance of the two models is actually quite close: $F_1$ score of 0.626 vs 0.614. Can you check the variability of the CV estimate?
Comment: Thanks for the response! The thing is, when I check these params for my out-of-box model, I see:

learning rate:  0.1
max_depth:  3
min_child_weight:  1
n_estimaters:  100

This out-of-box model doesn't have any training folds during CV, because I don't perform CV on it. It's just fit to my training data and then used to predict on the test. I thought a model that overfit the training data would perform worse on the test data. This one isn't. It's better than a cross-validated model. I can't figure it out.
Comment: Apologies, for the delay. Unfortunately, I can't check the variability because I have since changed everything and it would be a bit of work to go back. I'm guessing what was happening was that there was just a combination of a lucky test split and the fact that CV is training on less data with each fold validation so it arrives at slightly different hyperparameters that don't happen to perform as well on that particular test data.


A Blues Major Scale and A Blues Minor Scale meet at one point?

Question: I am fairly new to playing the guitar now backed with some theory. I was exercising the A Blues Major scale for a while now and had some very cool insights. The latest is the assumption that the A Blues Major and the A Blues Minor meet at some point.
This is the A Blues Minor Scale in the mode of the 5th fred.

It is the same pattern as the A Blues Major I learned when being in the box of 2nd to 5th fred.
Is this some kind of pivot point to give me a way to switch to another scale?
Comment: What you've shown are Am blues and Cmaj blues notes. They're *relative* to each other, there's no pivot point here, but if you take the whole pattern *down* 3 frets, you're at the A maj blues spot. Pivot point, 'A', surprisingly!
Comment: ... and then they both go into a bar...
Comment: Yep. It's very handy to to be able to play this shape in the fifth position for minor/blues and in the second position for a more major/country sound. Not sure what you mean by 'pivot point'. since one can just move to another scale at will.
Answer: They meet because they are the same "scale", just different modes. If you're not familiar with modes you might want to explore that. But put simply, both have the same notes they just start on different notes. For example, A natural minor (A B C D E F G) is just the aeolian mode of C major (C D E F G A B).


Regex por posição PHP

Question: Tenho a seguinte string:
'QD 10 LT 20 PA 30'

Usando o preg_replace() no PHP consigo manipular apenas os números.
Por exemplo, pegar o primeiro grupo de números:
$string = 'QD 10 LT 20 PA 30'
preg_replace('/\D+(\d+)\D+(\d+)\D+(\d+)/', '$1', $string)
//return 10

Há alguma forma de fazer o padrão ser recursivo?
Por exemplo o regex /\D+(\d+)/ separar todos números em grupos de acordo com a quantidade de grupos de números. Ou seja, caso minha string fosse maior ('QD 10 LT 20 PA 30 SC 40'), eu não precisaria aumentar o meu regex.
Answer: Na pergunta é mencionado que você quer pegar apenas o primeiro número, mas depois diz que também quer pegar todos. Então deixei uma solução para cada caso.

Pegar apenas o primeiro número
Se você só quer extrair um trecho da string que corresponde à regex, pode usar preg_match, passando a sua string e uma variável que conterá os matches:
$string = 'QD 10 LT 20 PA 30';
preg_match('/^\D+(\d+)\D+/', $string, $matches);
echo($matches[1]);

No caso, usei o marcador ^, que indica o início da string. Em seguida, eu verifico até o primeiro grupo de dígitos ((\d+)). Como você só quer saber do primeiro grupo, não precisa verificar o restante da string.
A variável $matches conterá os trechos que a regex capturar. E como \d+ está dentro de parênteses, será formado um grupo de captura. E como é o primeiro par de parênteses, ele estará disponível em $matches[1]. Por isso esse código imprime 10.

Há um detalhe na regex. Logo depois do ^ há o \D+ (um ou mais caracteres que não sejam números). Isso quer dizer que a string não pode começar com números. Ou seja, este código não imprime nada, pois como a string começa com números, o preg_match retorna false:
$string = '11 QD 10 LT 20 PA 30'; // começa com números (não entra no if)
if (preg_match('/^\D+(\d+)\D+/', $string, $matches)) {
    echo ($matches[1]);
}

Sendo assim, uma alternativa é trocar o quantificador + (uma ou mais ocorrências) por * (zero ou mais ocorrências):
$string = '11 QD 10 LT 20 PA 30'; // começa com números
if (preg_match('/^\D*(\d+)\D+/', $string, $matches)) { // troquei o primeiro \D+ por \D*
    echo ($matches[1]); // agora entra no if
}

Mas há outro porém. Pelo que entendi, você também quer validar se a string está no formato "duas letras, dois números, duas letras, dois números, etc".
Vamos assumir que o formato é exatamente esse (sempre duas letras e dois números, separados por espaço, e este padrão pode se repetir várias vezes). Neste caso, podemos trocar o + por {2}, que significa "exatamente duas ocorrências":
$string = 'QD 10 LT 20 PA 30 SC 40';
if (preg_match('/^[A-Z]{2} (\d{2})( [A-Z]{2} \d{2})*$/', $string, $matches)) {
    echo ($matches[1]); // 10
}

Primeiro temos o início da string (^), e em seguida [A-Z]{2} (\d{2}):

[A-Z]{2} significa "2 letras de A a Z" (somente maiúsculas). Se quiser somente letras minúsculas, troque por [a-z]{2}, e se tanto faz maiúsculas quanto minúsculas, use [A-Za-z]{2}
depois temos um espaço em branco (já que os campos são separados por um espaço)
depois temos (\d{2}): dois dígitos dentro de um grupo de captura (para que esteja em $matches[1])

Depois temos ( [A-Z]{2} \d{2})*. Dentro dos parênteses temos um espaço (repare no espaço logo depois do (), seguido de duas letras, outro espaço e dois dígitos. Fora dos parênteses temos *, o que significa que toda essa sequência (espaço, 2 letras, espaço, 2 dígitos) pode se repetir zero ou mais vezes. Isso garante que a sua string pode ser tanto QD 10 LT 20 PA 30 SC 40 quanto QD 10.
Por fim, temos $, que significa "final da string". Com isso eu garanto que a string toda possui o formato que preciso, e ainda pego o primeiro grupo de dígitos em $matches[1].
Se a quantidade de letras e dígitos pode variar, basta mudar o quantificador. Exemplos:

\d{1,5} - entre 1 e 5 dígitos
\d{2,} - 2 ou mais dígitos (sem limite máximo)

Ajuste para os valores que você precisar.

Pegar todos os números
Neste caso, use preg_match_all:
$string = 'QD 10 LT 20 PA 30 SC 40';

if (preg_match_all('/\b(\d{2})\b/', $string, $matches, PREG_SET_ORDER)) {
    foreach ($matches as $m) {
        echo $m[1] . "\n";
    }
}

Neste caso, estou usando \b como delimitador, pois ele representa uma "fronteira de palavras", que na prática verifica se há algum caractere não alfa-numérico antes e depois do (\d{2}). A diferença é que agora $matches conterá um array com todas as ocorrências de 2 dígitos da string. A saída é:

10
  20
  30
  40

A diferença é que o preg_match_all só está pegando os dígitos, mas não valida de fato o formato da string. Ou seja, se a string for abcdef 10, xyz!20, ainda sim ela pegará os números.
Mas você pode usar a regex anterior somente para validar o formato, e em seguida usar preg_match_all para extrair os números:
$string = 'QD 10 LT 20 PA 30 SC 40';
// verifica o formato da string
if (preg_match('/^[A-Z]{2} (\d{2})( [A-Z]{2} \d{2})*$/', $string)) {
    // só extraio os números se o formato estiver OK
    if (preg_match_all('/\b(\d{2})\b/', $string, $matches, PREG_SET_ORDER)) {
        foreach ($matches as $m) {
            echo $m[1] . "\n";
        }
    }
}

Comment: Isso já me ajuda muito.
Eu queria fazer com o replace pois a posição e o regex é o cliente que define. Então em vez de usar o match_all e digitar as posições de cada número de 0 à 2 , por exemplo, ele digitaria de 1 à 3 apenas à fim de ser visualmente mais claro.
Eu cheguei a ver alguns comandos de recursividade no regex mas não consegui chegar no que eu queria.

Regex por posição PHP


Multisites with different database share rules

Question: I create two sites with same code and different databases, but when I activate rules in the second site I can see the first site's rules.
I can disable the rules in second site and keep them active in the first, but rules with automatic name (ex. "Email on User Restriction") only appear one time.
Its a problem of my configuration? registry_rebuild.php does not fix.
Answer: SOLVED: Problem appear when a sigle site (default) is moved to a multisite (sites/XXX).
SOLUTION: erase rules in the other site don´t erase in the original site.


Monitor application.log in Play Framework

Question: We have a standard application.log file in our Play Framework installation. To this file, all searches that are performed in the application are logged, giving the log file a new entry approx every second.
But sometimes the application will throw an error, logging it with a id in the form of @andthensomecharacters.
Now, when looking for these errors, I look through the file manually, using / to search (the application.log file gets very large). But is there a better way to monitor these errors? Can I somehow be notified if an error is thrown, or maybe have these errors also written to another file?
Any suggestions are appreciated.
The Play version is 1.2.3, and it is running in Ubuntu.
Answer: You can annotate a method in your controller with @Catch annotation. This will cause the method to be run every time an error happens in the controller. In this method, you can save the error to a database or file.
If you want to make a common catch method for your entire application, you can implement the catch method in its own controller, and use the @With annotation to add the functionality to your other controllers.


When is Locrian appropriate to use over a diminished triad?

Question: In my experience and training, I'm under the impression that a diminished triad isn't listed in a chord chart unless it is considered the Diminished 7th chord function. While there may be different functions for diminished chords, in my experience, the Locrian mode is used for the m7(b5) chord, not the diminished chord.
I say that the min7(b5) chord isn't to be hidden behind the "Diminished Triad" label. The diminished chord has a bb7, and the min7(b5) doesn't include that. If you're using Locrian over the diminished triad, it's a mislabeled min7(b5) chord, no?
Answer: When using the entire seven notes of Locrian, then yes, the half-diminished seventh chord is implicitly involved. However, it's perfectly plausible to use Locrian with a diminished triad without implying a half-diminished seventh chord -- just avoid playing the seventh. The first five notes encompass a diminished triad, so will work fine over that chord. The only issue would be whether the second scale degree fits the larger musical context.
Answer: It's pretty straightforward that m7♭5 and a diminished triad aren't the same. Take Co - the triad  - C E♭ G♭. Add the diminished 7, B♭♭ (enharmonic with A). Not the same notes as Cm7♭5 - C E♭ G♭ B♭. Although the basic triad is the same, the extensions are different.
C Locrian contains the notes C, D♭ E♭ F, G♭, A♭ B♭. Thus, the better match from the two above chords will inevitably be Cm7♭5, as the B♭♭ note will certainly clash from the C fully diminished 7th.
However - it would really depend where certain notes were played and there's not a lot wrong with passing notes, on non-stressed parts of bars. Or even notes which seemingly, technically don't/won't fit, but when played, add some tension to that part of the piece.
Comment: "m7♭5 and a diminished triad aren't the same."... do you mean a dimished tetrad? The first 3 notes (triad) are the same, aren't they?
Comment: @mkorman - no, that's what I meant - they can't be the same, one has 3 notes, one has 4. Read on...
Answer: A dimished chord (with the ♭♭7) is not part of any diatonic scale. Thus, no diatonic mode will really fit it 100%. When playing over a dimished chord you need to think outside the modes and use an altered scale.
As mentioned above, the issue is the ♭♭7 vs. the ♭7 of a diatonic semi-diminished chord. You could start with the Locrian mode and add a ♭♭ alteration to the 7th.
Answer: "If you're using Locrian over the diminished triad, it's a mislabeled min7(b5) chord, no?"
No, because one is a triad.  That would be like saying that a major tried is a mislabeled dominant 7th chord, or a mislabeled major 7th chord.


getattr() without 'class object'

Question: How can I use getattr without "Class" per se ?
So I have this situation: I have 'columns' that are asking mysql for specific data in a specific order. data is printed via flask/apache so that user has ability to manipulate this data. Now, From flask, POST methdd, I'm receiving changed(?) values and I am storing them in python attributes.I need to check if values within those attributes are same as in original data. Sure, I could hardcore it but I would like have possibility of change columns dynamically.
columns = ["username", "email", "admin"]
data = ("john", "email@example.com", "True")

username = "john"
email = "firstname.lastname@example.com"
admin = False

Not sure how can I approach it ?
for i in data:
  if i == getattr(???, 'username'):
    print("it's the same")

or something like this?:
for i in data:
   if i == getattr(data, '?????'):
     print("it's the same")

Everything is within flask, I cannot embed it into the Class per se. So I don't have 'self' etc.
If I could create class I would probably make something like
class Myclass:
    def __init__(self):
        self.columns = ["username", "email", "admin"]
        self.data = ("john", "firstname.lastname@example.com", "True")
        self.result = []
        self.username = "john"
        self.email = "email@example.com"
        self.admin = False

    def test(self):
        for i in self.data:
            if i == getattr(self, self.columns[self.data.index(i)]):
                self.result.append("same")
            else:
                self.result.append("different")
        return self.result

Myclass().test()

['same', 'different', 'different']
Comment: Leave out the `"self." + `. The attribute is called `username`, not `self.username`.
Comment: thanks! I will edit question for clarity :)
Comment: If you can do something like `x = thing.attribute`, then you can also do something like `attribute_name = "attribute"; x = getattr(thing, attribute_name)`. ***That's all there is to it.***
Comment: @AlexHall, sounds great, but what would be the 'thing' ?
Comment: Literally anything for which it's possible to get `thing.attribute`. Just read the comment over and over again. I emphasised the last sentence heavily for a reason.
Comment: @AlexHall, I think i might be lacking of some basic python knowledge here. 'x = data.username' will not work as data has no username attribute, so still i am not sure what do u mean by 'thing'. Do you mind writing working example that would solve the very top situation in my post? much appreciated!
Comment: If "'x = data.username' will not work as data has no username attribute" then `getattr` is not going to help you. Again, as far as `getattr` is concerned, the comment above is all the information needed.
Comment: @AlexHall, thanks! you are probably right, but still, I don't know what to do with that information to solve my problem :)
Comment: Ask a new question that has nothing to do with attributes and `getattr`, because this is a mess.
Answer: It turned out that I was looking for simple eval(). getattr() is designed for different purposes.
so simple:
for i in data:
  if i == eval(cols[data.index(i)]):
    print("it's the same")

did the trick
Answer: Flask is just Python code. You can create a class and use that if that fits your use-case. Or, if you used Flask-SQLAlchemy to manage database-backed data you'd have classes and instances anyway (and get easier data updates to boot).
And classes and instances are not the only objects with attributes; modules and functions have attributes too (although you wouldn't store your data as attributes on either of those), and when you look up methods on anything, you are looking up attributes too.
Pick a storage, then either wrap that storage with an instance of a class, and use getattr(), or pick a different data structure and use the methods for that data structure to get at the different fields. A dictionary, for instance, would make it trivial to get the current value for a given name.
If you do stick to instances, then note that in your loop you'd want to zip your columns and data values together:
for name, value in zip(columns, data):
    if getattr(self, name) == value:
        self.result.append("same")
    else:
        self.result.append("different")

Note that you do not have to add "self." in front, the whole point of getattr() is do the same work the . syntax does.
You probably want to put your columns and data lists together as a dictionary:
self.data = {'username': 'john', 'email': 'firstname.lastname@example.com', 'admin': 'True'}

because that's how you'd process POST data from a form anyway; that way you can iterate over the dict.items() pairs, or use just the columns list to access values:
for name, value in self.data.items():
    # ...

or use dict.get() to retrieve values, allowing for missing entries:
for name in self.columns:
    if getattr(self, name) == self.data.get(name):
        # ...

Comment: well, but I don't have a class so 'self' is undefined
Comment: @tmdag: you *do* have a class, did you mean *instance* perhaps? And if you don't have an *instance*, what data are you going to compare this with? If there is no instance, then you *know already* that all data is new.
Comment: @tmdag: and if you don't have a class at all, what are you storing the existing data in to compare with? If you have a dictionary, then just use `current_data.get(name)` to get the current value (or `None` if not set). But you used a class and instance in your question.
Comment: Peters everything is within flask. data is comming from mysql, going to www, and then i am getting data back. I want to update mysql ONLY if data from www (post) is different then what it originaly was. Ok, I don't have instance, I don't have 'self'. And all I want is just to compare those values, like in my example on the very top. How can I approach it ?
Comment: I've used class only as example (I've edited question just to be clear).
Comment: @tmdag: Flask is *just Python*. You can create a class. If you don't have a class, you have another Python data structure. Just use that data structure. And Python classes and instances are not the only objects with attributes.
Comment: ok, so here is my question - do I have to create class just to use getattr? is there any other cleaner way ? as it sounds over complicated just to make 'simple' task that i would like to achieve :)
Comment: @tmdag: you are rapidly veering into  *too broad* here. It depends on your data; if you have data in a list, use `zip(names, original, new)` to pair up the names with the original data *and* the new values. If you have a dictionary, just use `dict.get()`. If you have instances, use `getattr()`. Etc.
Comment: @martjin my problem is that i don't know which position of data list corresponds to which variable. Sure, I can pair those strings, but still i need to compare it with variable which is same as string. data is exacly as given in first example. Do you mind writing example code solving it? much appreciated!

getattr() without 'class object'


iPhone + UITableView with multiple columns

Question: I want to display a UITableView with Multiple Columns.
The first column will have some text and another column will have a button, click of button will navigate to some another view.
Answer: I had a similar requirement; and did with the help of this..
http://dewpoint.snagdata.com/2008/10/31/drawing-a-grid-in-a-uitableview/
This would probably help you..


Does $K_{15,15}$ decompose into $K_{5,5}-C_{10}$ and $K_{5,5}-(C_6 \cup C_4)$ subgraphs?

Question: Following on from this question:

Q: Does $K_{15,15}$ decompose into $K_{5,5}-C_{10}$ and $K_{5,5}-(C_6 \cup C_4)$ subgraphs?

or equivalently

Q: Does there exist a $15 \times 15$ matrix containing $15$ copies of each symbol in $\{1,2,\ldots,15\}$ such that each row and each column containing a copy of symbol $i$ contains exactly $3$ copies of $i$?

The usual checks hold (a) the number edges in $K_{5,5}-C_{10}$ and $K_{5,5}-(C_6 \cup C_4)$ is $5^2-10=15$ which divides $15^2$, the number of edges in $K_{15,15}$, and (b) the degrees of the vertices in $K_{5,5}-C_{10}$ and $K_{5,5}-(C_6 \cup C_4)$ are $3$ which divides $15$, the degrees of the vertices in $K_{15,15}$.
The answers to the earlier question do not seem usable here.
I managed to fit $10$ in with the computer, but the computer seems woefully inadequate at solving this problem:
Answer: Answering my own question: it is possible (see below).  How I found it: I just started filling in the matrix by hand until I became better at avoiding "painting myself in the corner".

Unfortunately, this process was not too intelligent, so there's no hope of generalizing it.  I'd still be happy to see a better method (if possible).


Gravar cookie e ler seu valor ao aceder ao web site para verificar se expirado

Question: Eu preciso fazer um script em JavaScript que siga a seguinte lógica:

Grave um cookie com um id, que vai de 1 a 3. Juntamente com o cookie, o dia/horario OU eu quero que ele expire em 8 horas.
O usuário, toda vez que entrar no site, deverá ler esse cookie.
Se ele estiver com o tempo expirado, deverá ler qual id parou (1, 2 ou 3), deverá somar 1 ao id (caso seja 3 voltar pro 1), e porfim, abrir um popup automática com uma URL, que será definida desta maneira:

Se for id 1, abrir dominio.com.br/id1
Se for id 2, abrir dominio.com.br/id2
Se for id 3, abrir dominio.com.br/id3
Comment: Dá uma olhada a esta pergunta (http://pt.stackoverflow.com/q/43889/129), é o mesmo problema que queres resolver?
Answer: Você deve usar document.cookie para criar os cookies conforme a necessidade e o script deve rodar em um servidor (como Apache por exemplo), acesso local (protocolo file://) geralmente não funciona com cookies.

note que alguns navegadores bloqueiam cookies gerados pelo http://localhost, para contornar o problema use o endereço http://127.0.0.1

Para criar um cookie devemos usar os parâmetros como abaixo:
document.cookie="chave=valor; expires=DATA PARA EXPIRAR; path=CAMINHO";

Note que talvez ao invés de cookies você pode usar localStorage e sessionStorage (https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage)
Por exemplo com sessionStorage:
// Salva dados na sessão
sessionStorage.setItem("username", "John");

// Pega os dados
alert( "username = " + sessionStorage.getItem("username"));

Por exemplo com localStorage:
// Salva dados na sessão
localStorage.setItem("username", "John");

// Pega os dados
alert( "username = " + localStorage.getItem("username"));

Mas como a questão sobre cookies, irei mostrar um exemplo com tal, iremos precisar de dois métodos para isso:
function getCookie(k) {
    var cookies = " " + document.cookie;
    var key = " " + k + "=";
    var start = cookies.indexOf(key);

    if (start === -1) return null;

    var pos = start + key.length;
    var last = cookies.indexOf(";", pos);

    if (last !== -1) return cookies.substring(pos, last);

    return cookies.substring(pos);
}

function setCookie(k, v, expira, path) {
    if (!path) path = "/";

    var d = new Date();
    d.setTime(d.getTime() + (expira * 1000));

    document.cookie = encodeURIComponent(k) + "=" + encodeURIComponent(v) + "; expires=" + d.toUTCString() + "; path=" + path;
}

Para definir o seu cookie de ID, faça isto:
if (getCookie(id) === null) {//Se o cookie não existir
   var tempodevida = new Date();
   tempodevida.setTime(tempodevida + (1000 * 60 * 60 * 24 * 2));

   setCookie("id", "1|" + String(new Date().getTime()), tempodevida);
} else {
   ...
}

A string irá formar algo como 1|1419862250858 é o primeiro valor do ID e o numero longo é o que usaremos para comparar as 8 horas.

String(new Date().getTime()) pega o horário que o cookie foi criado

Está linha 60 * 60 * 24 * 2 é um calculo para você entender e modifica-lo conforme a necessidade, ele diz que o cookie deve expirar em dois dias (não tem haver com as suas 8 horas), pois todo cookie deve ter um limite de vida.
O 1000 é por que o Date trabalha com milesegundos, o primeiro 60 seria segundos, o segundo 60 seria minutos, o 24 é o tanto de horas que temos no dia e o 2 seria dois dias.
Recomendo que você altere apenas o "2", por exemplo que você quiser que o tempo de vida seja de 15 dias, faça isto: 1000 * 60 * 60 * 24 * 15

Agora iremos trabalhar o else do exemplo. Para verificar devemos usar getCookie:
var meuCookie = getCookie("id");

ele irá retornar como um destes possíveis valores 1|1419862250858, 2|1419862250858 e 3|1419862250858 (note que o 1419862250858 é apenas um exemplo de tempo). Devemos "cortar" a string usando String.split:
meuCookie.split("|");

var id    = parseInt(meuCookie[0]);
var tempo = parseInt(meuCookie[1]);

Teremos as variáveis id  e tempo para compara conforme a sua necessidade, que deve ficar algo como:

Note que 1000 * 60 * 60 * 8 equivale a 8 horas

var tempoAtual = new Date();
tempoAtual.setTime(tempoAtual - (1000 * 60 * 60 * 8));

if (tempo < tempoAtual) {//Se tempo for menor que o limite das 8 horas, então significa que expirou
    id++;
    if (id >= 3) {//Se id for igual a 3 volta para o 1
        id = 1;
    }
}

Após isto devemos usar window.location para redirecionar, seria algo como:
window.location = "http://dominio.com/id" + id;

O código todo deve ficar assim:
function getCookie(k) {
    var cookies = " " + document.cookie;
    var key = " " + k + "=";
    var start = cookies.indexOf(key);

    if (start === -1) return null;

    var pos = start + key.length;
    var last = cookies.indexOf(";", pos);

    if (last !== -1) return cookies.substring(pos, last);

    return cookies.substring(pos);
}

function setCookie(k, v, expira, path) {
    if (!path) path = "/";

    var d = new Date();
    d.setTime(d.getTime() + (expira * 1000));

    document.cookie = encodeURIComponent(k) + "=" + encodeURIComponent(v) + "; expires=" + d.toUTCString() + "; path=" + path;
}

(function () {
    if (getCookie(id) === null) {
        var tempodevida = new Date();
        tempodevida.setTime(tempodevida + (1000 * 60 * 60 * 24 * 2));

        setCookie("id", "1|" + String(new Date().getTime()), tempodevida);
    } else {
        var meuCookie = getCookie("id");

        meuCookie.split("|");

        var id    = parseInt(meuCookie[0]);
        var tempo = parseInt(meuCookie[1]);
        var tempoAtual = new Date();
        tempoAtual.setTime(tempoAtual - (1000 * 60 * 60 * 8));

        if (tempo < tempoAtual) {
            id++;
            if (id >= 3) {
                id = 1;
            }
        }

        window.location = "http://dominio.com/id" + id;
    }
})();

Answer: Não sou Muito bom com isso, mas creio que isso poderá lhe ajudar!
<?php

// Determinada um nome pro seu cookie
$cookie = "4567500";

// Verifica se o cookie nao existe
if(!isset($_COOKIE[''.$cookie.''])){

    //Resposta
    echo "Cookie Não existe";

    // Cria um cookie
    $nome = $cookie;
    $valor = "Valor Dentro do Cookie";
    $expira = time() + (8 * 3600);
    setcookie($nome, $valor, $expira);

    //Insere o poup no corpo da pagina
    include_once ("poup.php");

                                    }

elseif(isset($_COOKIE[''.$cookie.'']))
{
    //Resposta do servidor
    echo 'Cookie já existe';

    //Como o cookie existe ele não faz nenhum include
}

?>

Esse pequeno código pode ser colocado no começo da pagina, assim sempre que a pessoa entrar no seu site esse código sera processado, e o poup só ira aparecer se ja tiver passado 8hrs ou caso ela nunca tenha entrado
Comment: Isso é PHP, o Fabio pediu em Javascript. :\
Comment: Se for pra usar em um site comum da pra ele usar o php normalmente. Até porque em javascript tudo fica mais complicado e complexo. E é claro isso que eu postei é apenas uma alternativa.


Adwords scripts not writing to google sheet

Question: I recently made a script to get info from the Adwords API report KEYWORDS_PERFORMANCE_REPORT that outputed the information to a google sheet and it worked fine.
I decided to make another report for extensions all i basically did was copy and past the old code changing the URL for the google sheet and which report it should use PLACEHOLDER_REPORT instead of the keyword one, as well as some of the metrics.
The script runs without saying that anything is wrong but it doesn't output the values to where its supposed to, or at all for that matter.
function processAccount()
{

  var report = AdWordsApp.report(
    "SELECT AccountDescriptiveName, CampaignName, ExtensionPlaceholderCreativeId, ExtensionPlaceholderType " +
    "FROM   PLACEHOLDER_REPORT " +
    "WHERE  CampaignStatus = ENABLED " +
    "DURING LAST_7_DAYS",
    {apiVersion: 'v201705'});

  var rows = report.rows();

  if (rows.hasNext())
  {
        writeReport(rows);
      }
    }

function writeReport(rows)
{
    var spreadsheet = validateAndGetSpreadsheet(URL);

      // Clear all rows in the Details tab of the spreadsheet below the header row.
      var clearRange = spreadsheet.getRangeByName('Headers')
        .offset(1, 0, spreadsheet.getSheetByName('Details')
            .getDataRange().getLastRow())
        .clearContent();

      // Build each row of output values in the order of the Report tab columns.
      var outputValues = [];
      while (rows.hasNext())
      {
        var row = rows.next();

    outputValues.push([
      row["AccountDescriptiveName"],
      row["CampaignName"],
      row["ExtensionPlaceholderCreativeId"],
      row["ExtensionPlaceholderType"],
      ]);
   }
  // Find the first open row on the Report tab below the headers and create a
  // range large enough to hold all of the failures, one per row.
  var lastRow = spreadsheet.getSheetByName('Details')
    .getDataRange().getLastRow();
  var headers = spreadsheet.getRangeByName('Headers');
  var outputRange = headers
    .offset(lastRow - headers.getRow() + 1, 0, outputValues.length);

  Logger.log("outputValue length is "+outputValues.length);
  outputRange.setValues(outputValues);
  Logger.log(outputValues[0]);

  spreadsheet.getRangeByName('Date').setValue(new Date());

}

I know its finding the sheet because it clears the rows below the header like its supposed to and puts the date and time when it is run.
The logger shows that outputValue has a length of 293 so its definitly getting stuff and also logs the stuff at outputValue[0] so i dont know why it wont write to the sheet.
I called the adwords helpline but they said thearen't't trained in the api stuff so they don't know whats up, and i've googled it with no success
Answer: Everytime it runs you just want the new values, so I suggest using
spreadsheet.getRange(1, 1, data.length, 4).setValues(outputValues);
Replace your last line with this one, the last arguments is how many columns you will write, so in your case is 4, also change your code's logic so it will write the values in the end, not at every iteration!
Answer: Turns out i had messed up the output range it was outputting it about 120 rows lower than it was supposed toi do if id just scrolled down i would of seen it. fixed that and now all is fine.


What is this scale? Possibly an Indian Raga

Question: I have come across a scale which to me sounds very Indian. I thought perhaps it was some sort of Raga, however I can't identify which one. Its "key signature" is two sharps and two flats:

I've been playing with this scale for a few years, and can't remember where I got it from.
Comment: @jdjazz Yep, it's a duplicate. In my defence, it's difficult to find which scale you're looking for when the questions are all titled "what is this scale?" and may not necessarily be in the same key. Shall I close it?
Answer: This scale is refered to by many names, but the preferred term is double harmonic major. It is the near neighbor of a lot of commonly-used scales, like the phrygian dominant mode (of the harmonic minor scale), the h-w diminished scale, and the altered scale. Unlike those scales, the double harmonic major has the b2, the nat5, and the nat7, making it particularly well-adapted to root motions typical of functional harmony.
Comment: To map to Raga, a note must be identified as reference note, and there are several other characteristics that are needed. However, if it's only the question of scale, and since all seven notes are present, it happens to be the "Bhairav" scale, if  D is taken to be the reference frequency ("Shadaj").
Comment: Why accept the answer but not upvote it?
Comment: A great Lando Calrissian quote comes to mind: "I don't like it. I don't agree with it. But I accept it"


Should I install ruby as root or user?

Question: I'm not sure which option to take. I'm installing ruby on VPS.
My options:

Install ruby from ubuntu repository
Install RVM
Install RVM as user
Install RVM as root

I've 1 user VPS, that handles apache/php/python projects. I'm learning ruby, so I will add ruby system tools (I made plugin for munin) and rails apps obviously.
I would like to make informed decision, and keep my VPS (production) in shape and correctly configured.
I could go with either choice and end up with working system. But I would like to make informed decision and learn a thing or two about system administration.
Thanks!.
Comment: It is always a good idea to install from the repository unless the repository doesn't have what you need.
Answer: This really depends on your requirements.
Do you have apps that require different versions of ruby?  Will you have a need to run different ruby version in the future?  If, so then you will probably want to use RVM.  I would install it as root as each user you must add to the RVM group and I believe even if installed as root, each user gets their own folder.
If you want simplicity and only plan on running one version of Ruby then, go with the repository.  In my opinion, that route is much easier.  You can do it with RVM and you'll be just fine, but it may be a little harder and more time-consuming.

Should I install ruby as root or user?


How do I Bulk Edit Fields or Taxonomy of Nodes?

Question: I'm working on a site that needs bulk tagging on nodes. The D8 core View Bulk Operations Port is "simple" (https://www.drupal.org/node/1823572) and it doesn't have "modify entity values" which I used for this user-case in D7. There isn't a D8 tagged version of contrib VBO I can find.
My specific use-case is I need to bulk tag groups of blog posts with single terms. I have bulk imported around 2000 nodes from wordpress and need to apply new tags to them. Once the backlog id tagged I don't really need bulk editing any more. (But I'm sure many folks have similar use cases, like bulk editing fields or tags that aren't one-off tasks)
Actions is part of Core so it might be possible to write custom actions for my use case (Tag node with taxonomy term X, Y or Z). This documentation seems the most promising. https://www.drupal.org/node/2020549
Can any confirm this is best way to bulk-edit nodes or is there a different approach that I should consider?
Answer: Drupal core doesn't provide this functionality, so yes, you need to implement it yourself.
Yes, you can implement your own action plugin to do this. You can also have a form to select which tag you want to apply. Have a look at the node delete action plugin, which uses a confirmation form, but you can implement whatever functionality you want in that form.
Keep in mind that Drupal core is missing another important feature for your use case. The ability to apply a bulk action to more than one page. The best you can do until there is a port of the contrib module to provide this to increase the amount of rows per page, so that you need to repeat it fewer times.
Comment: I ported the contrib module a few months back - https://www.drupal.org/project/views_bulk_edit
Answer: Berdir's answer is accurate, you must write your own action.
I found this article extremely useful to quickly achieve this.
Basically when you create and registering a new action you can specify a 'confirm form', eg 'Are you sure you want to do this?'. You can provide a form for this stage that accepts more information and then uses that information to perform a batch operation on the nodes/entities who's IDs have been passed through.
Note that, as Berdir stated, the action to perform a bulk operation across multiple pages is still (to my knowledge) missing.
Answer: views bulk operations (https://www.drupal.org/project/views_bulk_operations)
gives a special views field that will allow you to bulk change fields
bulk_update_fields also promises exactly that. Its a new module for drupal 8 and seems to work with some warnings (in my case)
you can install it stand alone and go to content/node and use it as action bulk update fields to another value or if you have view bulk operations installed overriding content/node path you can edit the view and there will be a new action available (which will need to be enabled)
Answer: Today, for taxonomy, it's possible via Taxonomy Bulk Actions (TBA) contrib module.


Doubling notes in orchestration - inversions and extensions

Question: Just a very quick question, because I've found similar answers about doubling notes (for instance, when arranging for four- or five-part harmony), but nothing specifically involving inversions.
So, suppose that I have a C major chord. I know that I can safely double the C multiple times, quite safely double the G (or the fifth), and that I should avoid doubling the E note (the third).
For example, I can play the chord, say, in the fourth octave, and have a high pitched instrument play C on the fifth octave and a bass instrument play C on the third octave.
The first question is: what about extensions? If I don't have C major but C7 or Cmaj7, how safe is it to double the Bb or B note (i.e., the seventh)?
The second question: how about inversions? If I have C/G (second inversion), does the "safe note to double" become G since it's acting now as the root note?
Comment: I guess the short answer is 'you have to balance them out so the voicing says what you want it to say'. Imagine a 60-piece orchestra with only a triad between them… who plays what? Add a maj7th & what does it say? If the basses are playing it you could be playing the intro to Whiter Shade of Pale'...
Comment: C/G is second inversion.  First inversion is C/E.
Comment: In C/G the root note is C, not G (unless what you really mean is G6sus4).
Answer: The "rules" for doubling stem from the desire in polyphony to keep voices independent from each other. Since the third of a major chord is often a leading tone, we don't double it, because then two voices would both move together to the tonic in the following chord.
This same principle applies to all "tendency" tones. For example, we wouldn't double the seventh of a dominant seventh chord, because both voices would resolve together, creating a parallel octave (or unison).
These "rules" apply regardless the inversion a chord is in. It's fine to double the fifth, even if that fifth is the lowest pitch (i.e., in a second inversion chord.
However, these rules are designed to apply to the polyphony of Palestrina (or Bach, with some modifications that don't relate to doublings). If you're not writing polyphony, or if you're not writing in an "ancient" style, then it's fine to double as you please. For example, if you have trumpets and violins playing in unison, then they very well might double the third of a major chord -- in that scenario, they are being treated as a single instrument, not separate, independent voices.
Answer: There's a principle that says the harmony should be complete within each section of the orchestra - strings, wind and brass.
There's also a quoted example (but I forget where) of a massive tutti chord including the major 3rd just once, in a 3rd trumpet!
What you DON'T have to do is follow the rules of Bach-style 4-voice harmony.
Comment: 'Orchestration' by Walter Piston.  That's not a firm 'rule' though.  Just a way to get a certain type of 'fullness' in a block chord.  (It's a bit more of a 'rule' when writing for big band - saxes vs trumpets vs trombones.)  In real life I hope you'll be scoring counterpoint more often than chorales.
Comment: +! for the "complete harmony within each section". Makes a lot of sense. Do you know where that comes from?
Comment: From every orchestration textbook ever!
Comment: That's not entirely helpful. Could you recommend one? I've never taken an orchestration course.
Answer: The rules you're citing are the general rules for harmonizing, which are generally applied for classical 4 voices harmonization - and usually for the same family of instruments.
When dealing with an ensamble, those rules still apply, but that mostly depends on two aspects:

the instruments (type and amount) that are going to play a specific voice, and the octave they're playing it;
the harmonic/musical result you want to achieve;

For instance, consider a simple string ensemble: you could have 8 first violins and just one doublebass, this doesn't mean that those violins can't play the same note.
Doubling voices (even for the same type of instrument) changes the "colour" of the harmony. It's not just about the dynamic each voice reaches, but the harmonics generated by that amount, also counting the harmonics each instrument has.
So, unless we're talking about a melodic movement (melody or counterpoint), doubling rules still apply according to the result you want to achieve. If you need a more balanced result (close to what you'd do with the same harmony using 4 voices), then you should also consider balancing the doubling in the same way, considering how those instruments's sound results and is perceived and possibly the amount of players for that section/voice.
BUT. That also depends on the style you want to use. For instance, in classical/early romantic style, it was common to have some instruments to double notes that are generally not doubled; a classic example is the usage of timpani and trumpets, which often doubled leading notes or thirds just because those were the only notes they could play[1], but since they added a more "triumphant" result, the doubling wasn't a problem.
Then again, doubling notes that are normally not doubled is not that an issue, especially if their voice has some importance (consider the E/D# bass/cello "pedale" in the finale of Beethoven's 7th), and in more modern styles those rules (like almost any other rule) are obviously much less strict, so the choice depends on the musical result you require.
[1] Historically, timpani and trumpets only played the tonic and dominant of the whole composition tonality; this was due to technical and practical reasons: natural trumpets were common even in early 19th century, mostly because the more advanced valve trumpets were more expensive and difficult to build, and it was also rare to find virtuoso players, so composers didn't "push" their limits on those instruments by using basic root/fifth notes and specifying in which key the instrument was (coincidentally, the habit was kept for long time exactly because of this: since the repertoire didn't require high skilled players or advanced instruments, players rarely became skilled). The same for timpani, but mostly due to technical issues: modern foot-activated timpani was created only in mid-1800, before that they could only be tuned by turning each screw of the head, so it was almost impossible to change tuning while playing (even between movements).


Understanding Dickey-Fuller-Result

Question: I am currently looking into the dickey-fuller test but seem to not understand something fundamental. To understand the test better, I created 4 time series: cons, lin, r1 and r2. How these are generated can be seen in the code below. While r1 pulls samples from N(0,1), r2 follows the definition of a random walk. These are displayed as Figure 1.

I would expect to obtain Z(t) > 0.05 for r2 and fail to reject H0 that it is a random walk. On the other hand, I would expect Z(t) < 0.05 for lin, con and r1 and reject H0 since these are certainly no random walks.
UPDATE
The results however are not as clear as I would hope, for these very basic examples. While I fail to reject H0 for r2 (as anticipated), the same applies for lin and con with much higher certainty. Only drawing random samples (r1) is correctly identified not to be a random walk with certainty.
. do "dickey-fuller-example.do"

. set obs 80
Number of observations (_N) was 0, now 80.

. gen points = _n

. gen cons = 0

. gen lin = 0.02 * _n

. gen r1 = 0

. replace r1 = rnormal() if _n>1
(79 real changes made)

. gen r2  = 0

. replace r2 = r2[_n-1] + rnormal() if _n>1
(79 real changes made)

.
. two con cons lin r1 r2 points, msize(small...)

.
. tsset points

Time variable: points, 1 to 80
        Delta: 1 unit

.
. dfuller cons

DickeyFuller test for unit root           Number of obs  = 79
Variable: cons                             Number of lags =  0

H0: Random walk without drift, d = 0

                                       DickeyFuller
                   Test      -------- critical value ---------
              statistic           1%           5%          10%
--------------------------------------------------------------
 Z(t)                 .       -3.539       -2.907       -2.588
--------------------------------------------------------------
MacKinnon approximate p-value for Z(t) = 1.0000.

. dfuller lin

DickeyFuller test for unit root           Number of obs  = 79
Variable: lin                              Number of lags =  0

H0: Random walk without drift, d = 0

                                       DickeyFuller
                   Test      -------- critical value ---------
              statistic           1%           5%          10%
--------------------------------------------------------------
 Z(t)             0.151       -3.539       -2.907       -2.588
--------------------------------------------------------------
MacKinnon approximate p-value for Z(t) = 0.9694.

. dfuller r1

DickeyFuller test for unit root           Number of obs  = 79
Variable: r1                               Number of lags =  0

H0: Random walk without drift, d = 0

                                       DickeyFuller
                   Test      -------- critical value ---------
              statistic           1%           5%          10%
--------------------------------------------------------------
 Z(t)            -7.259       -3.539       -2.907       -2.588
--------------------------------------------------------------
MacKinnon approximate p-value for Z(t) = 0.0000.

. dfuller r2

DickeyFuller test for unit root           Number of obs  = 79
Variable: r2                               Number of lags =  0

H0: Random walk without drift, d = 0

                                       DickeyFuller
                   Test      -------- critical value ---------
              statistic           1%           5%          10%
--------------------------------------------------------------
 Z(t)            -2.186       -3.539       -2.907       -2.588
--------------------------------------------------------------
MacKinnon approximate p-value for Z(t) = 0.2112.

.
end of do-file

Can somebody explain me what happens here? These are my main questions?

Why do I fail to reject H0 for r2 with comparatively low confidence?
Why do I fail to reject H0 for lin and cons?
Is there an easy example like this to confirm that the test works correctly?
Comment: May I please ask you how exactly you generated this random time series `rand`?
Comment: What @Alex said. In what sense is `rand` a random variable? It simply looks like a constant value repeated across time. Also you appear to contradict yourself following your graph: you expect to *fail* to reject $H_0$ for something that is not a random walk (`rand`), then you expect to reject $H_0$ for two things that are *also* not random walks. There's an inconsistency there.
Comment: @Alex Hi Alex, I updated the body to include the code that generates the time series.
Comment: @Alexis Hi Alexis, I updated the plot to show the time series more clearly. Now, I expect to fail to reject H0 for r2 only.
Answer: The Dickey-Fuller test has the null hypothesis $$\text{H}_{0}\text{: The time series has unit root},$$with the alternative hypothesis $$\text{H}_{\text{A}}\text{: The time series is stationary.}$$Caveat: evidence in favor of 'stationary' in the alternative hypothesis is agnostic regarding weak or strong stationarity.
You fail to reject $\text{H}_{0}$ of the Dickey-Fuller test for r2 because r2 is a random walk and has a unit root (i.e. was generated by a process conforming to $\text{H}_{0}$).
You fail to reject $\text{H}_{0}$ of the Dickey-Fuller test for lin because each observation of lin at time $t$ 'looks like' the value of the observation at $t-1$ plus a perturbation. The Dickey-Fuller test as you have specified it tests whether the estimated value of $\beta=0$ under the null in:
$$\Delta y_{t} = \alpha +\beta y_{t-1} + \varepsilon _{t}$$
As the value of $\Delta y_{t}$ in lin is uncorrelated with the value of $y_{t-1}$, there is no evidence to reject a hypothesized value of $\beta=0$.
You fail to reject $\text{H}_{0}$ of the Dickey-Fuller test for cons because there is no variation in the outcome. Here $\Delta y_t = 0$ at every point in time, and while it does not matter whether $\alpha = 0$ or not, $\beta=0$ for sure. The missing test statistic results from not having any variance estimate ($\varepsilon=0$ also).
These results, (along with the test rejecting the null for r1) are consistent with Stata's dfuller working correctly.

Understanding Dickey-Fuller-Result


Basic Visual Basic Program Error - Probably Variable Error

Question: I am writing this program for visual basic that will determine a bill based off water usage. My problem is that all values I input come back as zero in the command prompt. Can anyone explain what's wrong with this code?

Option Explicit On
Option Strict On

Imports System

module eurekawatercompany

  Sub Main ()
    ' Declare variables of problem
    Dim waterusage as double
    Dim totalcharge as double

    ' Prompts for user to enter their water usage.
    Console.write ("Please enter your current water usage (cubic feet): ")
    waterusage = convert.toint32(console.readline())
      If (waterusage < 1000) then
      totalcharge = 15

      End If

      If (1000 > waterusage) and (waterusage < 2000) then
      totalcharge = 0.0175 * waterusage + 15

      End If

      else if (2000 < waterusage) and (waterusage > 3000) then
      totalcharge = 0.02 * waterusage + 32.5

      End If
      ' 32.5 is the price of exactly 2000cm^(3) of water

      else if (waterusage > 3000) then
      totalcharge = 70

      End If

    Console.out.writeline ("Total charge is: ")
      Console.out.writeline (totalcharge)

  End sub

End Module

Answer: First up, your statement:
If (1000 > waterusage) and (waterusage < 2000) then

is equivalent to:
If (waterusage < 1000) and (waterusage < 2000) then

meaning that it's testing that waterusage is both less than 1000 and less than 2000 (ie, just that it's less than 1000). I think you may have meant something along the lines of:
If (waterusage > 1000) and (waterusage <= 2000) then

You'll notice I've used <= as well since your if statements don't handle the edge cases at all (2000 is neither less than, nor greater than, 2000 so entering 2000 would cause none of your original if statements to fire).
You'll also need to make similar changes to the 0 to 1000 and 2000 to 3000 cases.

I'm also not entirely certain that the:
:
End If
else if ...

construct is correct (unless VB.NET has changed drastically at the lower levels since the VB6 days (I know that there were a lot of changes but changing such a low-level thing as the workings of if would be unlikely). An end if closes off the entire if statement as far as I'm aware, so the else should be between the if and end if.
So I'd be looking at something like:
Option Explicit On
Option Strict On
Imports System
Module EurekaWaterCompany

Sub Main ()
    Dim WaterUsage as double
    Dim TotalCharge as double

    Console.Out.Write ("Please enter your current water usage (cubic feet): ")
    WaterUsage = Convert.ToInt32 (Console.In.ReadLine())

    If (WaterUsage <= 1000) then
        TotalCharge = 15
    ElseIf (WaterUsage > 1000) and (WaterUsage <= 2000) then
        TotalCharge = 0.0175 * WaterUsage + 15
    ElseIf (Waterusage > 2000) and (WaterUsage <= 3000) then
        TotalCharge = 0.02 * WaterUsage + 32.5
    Else
        TotalCharge = 70
    End If

    Console.Out.WriteLine ("Total charge is: ")
    Console.Out.WriteLine (TotalCharge)
End sub

End Module

That code also has some minor fixes like properly specifying Out and In for the I/O, and using "proper" capitalisation, although it's not fully tested and may still have some syntax errors. The idea behind the code (basically the if statement) is still sound.

However, you may want to check your specifications.
When utilities charge for their resource, they tend to levy higher rates on the excess beyond a certain level, not the entire amount. In other words, I'd expect to see a charge of $15 on the first 1000 cubic feet and then 1.75 cents on each cubic foot beyond that, which would make your statements look more like:
ElseIf (WaterUsage > 1000) and (WaterUsage <= 2000) then
    TotalCharge = 0.0175 * (WaterUsage - 1000) + 15

That would make sense in this situation since you'd be charged 1.5c/ft3 for the first thousand, 1.75c/ft3 for the second thousand, and 2c/ft3 for the third thousand, with a lower limit of $15 (you get charged for the first thousand no matter how much you actually use) and a fixed charge of $70 for anyone who uses more than three thousand cubic feet (sort of a penalty rate).
But, that's supposition on my part, from experience. It may be that your specifications say otherwise, in which case feel free to ignore this section.
Comment: Thanks, I'll give this a try in the morning to see if it works.
Comment: This worked perfectly, thanks for explaining so well. Also, you were correct about the math error.


Calculo em python

Question: Como faço isso?
<code>VAL[0] = 9.999839999244

VAL[1] = 349.0

VALUE = VAL[0]) * 10.0 ** float(VAL[1])
#Error (34, 'Numerical result out of range')
</code>
Comment: olá este é o site do SO em português, as perguntas aqui devem ser feitas nesse idioma
Comment: Bem-vindo ao Stack Overflow **em português**. Tal como o nome sugere, o idioma oficial usado aqui é o português. Assim sendo, você poderia por favor traduzir sua pergunta? Além disso sua pergunta parece ter alguns problemas e sua experiência aqui no Stack Overflow em Português poderá não ser a melhor por causa disso. Queremos que você se dê bem aqui e consiga o que quer, mas para isso precisamos que você faça a sua parte. Veja algumas orientações que vão te ajudar: [Guia de sobrevivência do Stack Overflow em Português](https://pt.meta.stackoverflow.com/q/8045), [ask], [mcve] e [help].
Comment: Especificamente no seu caso use uma biblioteca externa que trabalhe com aritmética arbitrária.
Answer: Boa noite, dei uma estuda aqui neste código e fiz algumas alterações. ver se é oque deseja.
Código alterado:
VAL0 = 9.999839999244
VAL1 = 34
VALUE = (VAL0 * 10.0) ** float(VAL1)
print(VALUE)
Codigo Original:
VAL[0] = 9.999839999244
VAL[1] = 349.0
VALUE = VAL[0]) * 10.0 ** float(VAL[1])
Removi os [] que tinha no código e também o float no código
Comment: Você eliminou o erro do problema original ao elevar ~100 a 34 em vez de 349. O erro mostrado na pergunta ocorre porque o Python não consegue lidar com números tão grandes, e na sua versão você está gerando um resultado muito menor, com que o python consegue lidar. Por isso sua resposta está sendo negativada...

Calculo em python


Iterations of a multivariable function

Question: How do you define iterations of multivariable functions?
To be clear(example):

If $f: \mathbb R^2 \to \mathbb R$
How do you define
$f \circ f$, or $f \circ \cdots \circ f$?

I admit that this question sounds very odd, but I think I need to define or learn of this. (Why? I want to generalize this(Carleman matrix) to multivariable functions to solve this(Multivariable carleman matrix) or this(same but different sites) question!)
And I think this concept may be quite reasonable because there is a something like multiplication of matrices that have different dimensions.
My assumtion is that $f \circ f \cdots \circ f : \text{also  } \mathbb R^2 \to \mathbb R$.
Any suggestions are appreciated.
Comment: There are definitely many possible versions... $F(x,y) =(0,f(x,y)); (f(x,y),f(x,y));$ or even $(f(x,y),f(y,x))$ (which is sort of a more 'symmetric' version of $f$). Which one you should consider depends highly on the property you want them to satisfy
Comment: I suppose you could define $\tilde f:\mathbb R^2\to\mathbb R^2$ by $\tilde f(x,y) = (f(x,y),f(x,y))$, then investigate $f_n := f\circ \tilde f\circ \tilde f\circ\dots \circ \tilde f$.
Comment: @MikeEarnest Worth trying!
Answer: The composition is undefined as $$f \circ f=\mathbb{R}^2\xrightarrow{f}\mathbb{R}\xrightarrow{?}\underline{?}.$$ However if you have a function $\mathbb{R}^2\xrightarrow{F}\mathbb{R}^2,$ then we can easily form the composition.
Comment: You're definitely right! But I know that too... I want to "define" it. Anyway, it seems that and your answer is saying that it has not been defined formally.
Comment: @KYHSGeekCode: How could we define something that cannot exist? The only way to get-rid of the problem is change the co-domain (naturally or artificially).

Iterations of a multivariable function


Bypass Google Load Balancer for dynamic pages

Question: I'm just testing on the Google platform and have a a couple Nginx VM's behind an LB set up which caches to GCDN globally. I want to bypass the cache for users that are logged in (with a wordpress cookie).
What is the best strategy for this? Up until now I've been using VM's -> Varnish -> Cloudflare and bypassed Varnish's cache based on the cookie. I wrote some workers code to do the same for Cloudflare. However, with GCDN/GLB there seems to only be functionality for sticky sessions/cookies which isn't applicable for what I need to do.
I can obviously use nginx to detect the cookie and set Cache-Control: no cache headers to prevent caching but if the pages are already in the cache that boat has sailed. Any advice is appreciated here, cheers.
Answer: In this case, when there is a Set-Cookie header, Google CDN will not cache the response. This being said, you're right that if the URL is already on Cloud CDN, the request most likely won't hit your Backend and the response will be given by Cloud CDN. I say most likely as there are other values that affect Cacheability in Cloud CDN.
Currently, if the URL is already in cache, you need to invalidate the cache first.
For different scenarios, you can double check the options to prevent Cloud CDN caching here.
All this being said,  if you really want to have a Feature, which will allow you to take in considerations users already logged in. I may suggest you to open a Feature Request using the following link.
Thanks


Interpretation of split point in getTree(randomForest) for factor when split point is 0

Question: How to interpret spli_point in getTree(randomForest) when the value of split for factor is 0 ? Here is the picture, look at row 11:
Picture:
Comment: Give some toy code to make the problem. If I can see the problem myself then I can probably attack it more effectively.
Answer: Doesn't make any sense to me.
To figure things out, I suggest to change the factor-levels and then compare the result.
Ok, then you could also prepare some test-data and track how it is classified by this tree. Set this factor-level one time to 0 and then copy the row and set it to 1. Then take a look if this changes the classification.
However this should be easy for a decision-tree ... I'm not sure how to track this in a forest.
Comment: That "0" for factors occurs in many cases which I have tried.
Answer: It's a leaf node; there is no split variable (as designated by <NA>) because no split is done at the leaf node.  You can tell this various ways; from the <NA>, from the fact that it has no daughter nodes (daughter nodes $= 0$), and from the status = -1 values.


How to calculate logistic regression coefficients manually?

Question: For two independent variables, what is the method to calculate the coefficients for any dataset in logistic regression? The equation we know is => logit = ln(P/1-P) = B0 + B1 * X1 + B2 * X2
On the below dataset, how do we calculate the above X1 and X2 values
<code>Y X1 X2
0 2 30
0 6 50
1 8 60
1 10 80
</code>
Comment: Presumably you want to estimate $B_1$ and so on? You already have $X_1$.
Answer: Unlike linear regression, where you can use matrix algebra and ordinary least squares to get the results in a closed form, for logistic regression you need to use some kind of optimization algorithm to find the solution with smallest loss, or greatest likelihood. For this, logistic regression most commonly uses the iteratively reweighted least squares, but if you really want to compute it by hand, then it would be probably easier to use gradient descent. You can find nice introduction to gradient descent in the lecture Lecture 6.5 — Logistic Regression | Simplified Cost Function And Gradient Descent by Andrew Ng (if it is unclear, see earlier lectures, also available on YouTube or on Coursera). Using his notation, the iteration step is
$ \mathtt{repeat} \, \{\\ \qquad\theta_j := \theta_j - \alpha\,
(h_\theta(x^{(i)}) - y^{(i)}) \,x_j^{(i)}\\ \}$
where $\theta_j$ is the $j$-th parameter from the vector $(\theta_0, \theta_1, \dots, \theta_k)$, $x^{(i)}$ is the vector of variables for the $i$-th observation $(1, x_1^{(i)},\dots, x_k^{(i)})$, where $1$ comes from the column of ones for the intercept, and the inverse of the logistic link function is $h_\theta(x) = \tfrac{1}{1+\exp(\theta^T x)}$ and $\alpha$ is the learning rate. You iterate until convergence.


How can I generate Ant targets?

Question: I want to be able to generate a number of Ant targets something like this:
<property name="grunt_tasks" value="jsp,css,js,img" />
<foreach list="${grunt_tasks}" param="task">
    <target name="${task}">
        <exec executable="grunt" failonerror="true">
            <arg line="${task}" />
        </exec>
    </target>
</foreach>

allowing me to run ant jsp or ant js.
However, this code fails because a target tag cannot be placed in a foreach tag.
How can I accomplish this?
Answer: There's a number of ways you might add targets on the fly.  Here's one suggestion:
<property name="mybuild" value="mybuild.xml" />

<property name="grunt_tasks" value="jsp,css,js,img" />

<echo message="<project>" file="${mybuild}" />
<for list="${grunt_tasks}" param="task">
    <sequential>
    <echo file="${mybuild}" append="yes"><![CDATA[
    <target name="@{task}">
        <exec executable="grunt" failonerror="true">
            <arg line="@{task}" />
        </exec>
    </target>
    ]]></echo>
    </sequential>
</for>
<echo message="</project>" file="${mybuild}" append="yes"/>

<import file="${mybuild}" />

Explanation:

Use the antcontrib <for> task in preference to <foreach>, else you have to have a separate target for the body of the loop.
Create a second buildfile, here called mybuild.xml, to contain your targets.
The buildfile content has to be within a <project> element.
Import the buildfile.

You can then invoke the on-the-fly targets in the way you wish.
You might alternatively use a
    </sequential>
</for>

Comment: I'm not so crazy about generating another xml file to accomplish this. I think I'd rather just suffer some repetition in my build.xml.

i've taken a look at the script task but have failed to find any documentation that would indicate how i'd go about creating targets.
Comment: @Matthew Added a script approach.  The documentation for using the API is the source itself.  [grepcode](http://grepcode.com/file/repo1.maven.org/maven2/org.apache.ant/ant/1.9.4/org/apache/tools/ant/taskdefs/Execute.java?av=f) is one way to peruse.
Comment: A couple of things I learned along the way -

load('nashorn:mozilla_compat.js'); // since i'm on Java 8 and using importClass, in other words Rhino compatibility

exec.createArg( ).setValue( "--target_env=" + target_env ); // for some reason ant properties are available to js directly although the task param isn't.

ug, formatting isn't my strong suit

How can I generate Ant targets?


Discovering "templates" in a given text?

Question: If I have significant amounts of text and am trying to discover templates that occur most frequently, I was thinking of solving it using the N-Gram approach and in fact it was suggested as a solution in this question as well but my requirement is slightly different. Just to clarify, I have some text like this:
I wake up every day morning and read the newspaper and then go to work
I wake up every day morning and eat my breakfast and then go to work
I am not sure that this is the solution but I will try
I am not sure that this is the answer but I will try
I am not feeling well today but I will get the work done and deliver it tomorrow
I was not feeling well yesterday but I will get the work done and let you know by tomorrow

and am trying to extract "templates" like this:
I wake up every day morning and ... and then go to work
I am not sure that this is the ... but I will try
I ... not feeling well ... but I will get the work done and ... tomorrow

I am looking for an approach that can scale to million of lines of text so I was just wondering if I can adapt the same N-gram approach to solve this problem or are there any alternatives?
Answer: Millions of lines of text isn't a really big number :)
What you're looking for is at least similar to collocation finding. You could try to compute pointwise mutual information on n-grams. See Manning & Schütze (1999) for this and other approaches to the problem.
Comment: Thank you for your suggestions. I finally got the book today :)


GLMM or LME? Repeated measures help?

Question: I am having trouble using the correct test and r code for my experiment. Essentially I measured insect emergence daily from artificial streams with 3 treatments.:
CONTROL - With 5 replicate streams
TREAT 1 - With 5 Replicate streams
TREAT 2 - With 5 Rep streams.
TREAT 2- With 5 rep streams

I think what I basically want to do is this: Emergence = Treatment + Day + Treatment*Day
Looking for an effect of treatment on insect emergence over time (day)
Update, I have just ran the model below, but it seems to be dropping a treatment group?
Ran this model:
> model6 <- lmer(Emerg ~ Day + Treatment + Day:Treatment + (Day | Stream), insect)
> summary(model6)

Linear mixed model fit by REML ['lmerMod']
Formula:
Emerg ~ Day + Treatment + Day:Treatment + (Day | Stream)
   Data: insect

REML criterion at convergence: 2070.8

Scaled residuals:
    Min      1Q  Median      3Q     Max
-3.0321 -0.4694 -0.0445  0.3883  4.8618

Random effects:
 Groups   Name        Variance  Std.Dev. Corr
 Stream   (Intercept) 672.73598 25.9372
          Day           0.01573  0.1254  1.00
 Residual             594.41059 24.3805
Number of obs: 224, groups:  Stream, 16

Fixed effects:
                        Estimate Std. Error t value
(Intercept)              68.7060    14.6813   4.680
Day                      -0.7632     0.8106  -0.941
TreatmentControl        -26.5467    20.7625  -1.279
TreatmentFluoxetine     -14.0357    20.7625  -0.676
TreatmentMix            -15.0879    20.7625  -0.727
Day:TreatmentControl      0.6181     1.1464   0.539
Day:TreatmentFluoxetine   1.5500     1.1464   1.352
Day:TreatmentMix          1.3808     1.1464   1.204

Correlation of Fixed Effects:
            (Intr) Day    TrtmnC TrtmnF TrtmnM Dy:TrC Dy:TrF
Day         -0.343
TrtmntCntrl -0.707  0.243
TrtmntFlxtn -0.707  0.243  0.500
TreatmentMx -0.707  0.243  0.500  0.500
Dy:TrtmntCn  0.243 -0.707 -0.343 -0.172 -0.172
Dy:TrtmntFl  0.243 -0.707 -0.172 -0.343 -0.172  0.500
Dy:TrtmntMx  0.243 -0.707 -0.172 -0.172 -0.343  0.500  0.500
>

Comment: what is emergence?
Comment: Insect emergence- emergence of the flying adult from the stream channel
Comment: so it is a continuous measure?
Comment: I don't see where the repeated measures comes in.  You are using the same insects for each treatment?
Comment: also what are streams?
Comment: Insects are measured daily and collected, day is the repeated measure
Answer: If you measured emergence as a continuous measure I would start with a mixed-effects model, but if the measure is dichotomous (e.g., yes or no) then generalized LMM is the way to start.
I will assume your response variable is continuous. I would use the lmer() function from the lme4 package in R. Your base model could look something like this:
Model <- lmer(Emergence ~ Treat + Day + Treat*Day + (1|Stream_id), data = Data_Frame)
summary(Model)

You have to tell the model that individual streams have multiple measures by treating it as a random factor (i.e, (1|Stream_id)). The 1|... indicates the model will include a random intercept for individual streams. It looks like you are trying to include a random intercept and slope for the random effect Stream, as a function of the effect of Day, which you would code as (1+Day|Stream) in package lem4.
The reason you are missing a treatment is because it is incorporated into the (Intercept). All of your visible treatments are being compared to the "missing" treatment in the intercept. So, your output does not compare the effect of TreatmentControl and the effect of TreatmentFluoxetine, and so on
I think you are interested in the interaction between Day and Treatment. That is, does the effect of Day on Emergence change depending on the type of Treatment used. If so, a simpler approach may be to use the repeated measures of Day to calculate one response variable, such as rate of emergence or total emergence. Then, a simple anova will tell you if the Treatment type affects the rate of emergence, total emergence, or what have you.
Hope this helps you get started. Good luck.
Comment: Thanks! My measure of emergence is contentious. Is a mixed effect model the correct way to go, or should it simply be a repeated measures? With day being the repeated measure? Also when I run the model, it seems to drop one of my treatment from the comparisons?
Comment: Your measure of emergence is likely to inspire debate?

GLMM or LME? Repeated measures help?


prove inequality $x-y<\tan x-\tan y<4(x-y)$

Question: $x-y < \tan x - \tan y < 4(x-y)$. How to prove that inequality? If $\frac{-\pi}{3} < y < x < \frac{\pi}{3}$. Do I need to use Lagrange theorem? If yes how it should look like? I ve tried but I don't understand logic of that prove and theorem
Answer: $$x-y<\tan(x)-\tan(y)<4(x-y)\iff1<\frac{\tan(x)-\tan(y)}{x-y}<4.$$By the Mean Value Theorem, $$\frac{\tan(x)-\tan(y)}{x-y}=1+\tan^2(c),$$for some $c\in\left(-\frac\pi3,\frac\pi3\right)$. Since in that interval one always has $1\leqslant\tan^2<4$, this almost proves what you want to prove. Can you take it from here?
Answer: The right inequality.
Since $\sin(x-y)\leq x-y$, $\cos{x}\geq\frac{1}{2}$ and $\cos{y}\geq\frac{1}{2},$we obtain:
$$\frac{\tan{x}-\tan{y}}{x-y}=\frac{\sin(x-y)}{(x-y)\cos{x}\cos{y}}\leq\frac{1}{\frac{1}{2}\cdot\frac{1}{2}}=4.$$
The left inequality.
Let $f(x)=\tan{x}-x,$ where $x\in\left(-\frac{\pi}{3},\frac{\pi}{3}\right)$.
Thus, $$f'(x)=\frac{1}{\cos^2x}-1=\frac{\sin^2x}{\cos^2x}\geq0,$$
which says $f(x)>f(y)$ and we are done!


Lattice teory: joins of vectors containing lattice elements?

Question: I would like ask you help with a formal proof concerning lattices.
Given a generic lattice $L$, there is a general partial order between the elements ($\sqsubseteq$) and a join (lub) operation $\sqcup$ that, given two elements, always returns the lub between the two elements.
My question concerns vectors that contain the elements of this generic lattice $L$.
Given two vector ($V$ and $W$) s.t. both vectors have length $n$. Suppose we have $\forall i$ $V[i]$ $\sqsubseteq$ $W[i]$ (we could say that $V$ $\dot{\sqsubseteq}$ $W$ in component-wise notation).
If I apply the join operator ($\sqcup$) to all the elements contained in $V$ I obtain a value of the lattice ($x$). I can do the same thing by joining all the elements inside $W$ (I obtain the value $y$ of the lattice).
My question is: is always true that $x$ $\sqsubseteq$ $y$ ? If yes, how can I prove it formally?
Answer: What you're asking is equivalent to the following question:

If $x_1,\ldots,x_n$ and $y_1,\ldots, y_n$ are elements of a lattice such that $x_i\leq y_i$, for $1\leq i \leq n$, then is it true that $\bigvee_{i=1}^n x_i \leq \bigvee_{i=1}^n y_i$?

The answer is yes, it it very clear that it is so.
To prove it, you can argue by stating that
$$x_1 \vee x_2 \leq x_1 \vee y_2 \leq y_1 \vee y_2$$
and proceed in the obvious way for bigger values of $n$.
Formally, you can use an induction process in a straightforward way.
More generally, if $A$ and $B$ are subsets of a lattice such that $\bigvee A,\bigvee B$ exist in that lattice, and if for every $a \in A$ there exist $b \in B$ with $a \leq b$, then $\bigvee A \leq \bigvee B$.
This is just because every upper bound of $B$ is also an upper bound of $A$.
Indeed, if $u$ is an upper bound of $B$ and $a \in A$, then there exists $b \in B$ such that $a \leq b$, whence $a \leq u$.


A B and C in Completing the square?

Question: When I complete the square, and I get this for example:
$$-(x+3)^2 - 13$$
What would $A, B$ and $C$ be?
For example, when using $C - B^2 / 4A$
Thanks!
Comment: Your notation is not entirely clear. Are the capital $A,$ $B$ and $C$ supposed to mean something different from the lowercase letters? Do you simply want the second-, first- and zero-th degree coefficients of a quadratic polynomial?
Comment: @Justpassingby yes :)  Sorry, I wrote them in capitals!
Answer: $$-(x+3)^2 - 13=\color{red}{-1}x\color{blue}{-6}x\color{green}{-22}$$
so $A=\color{red}{-1}, B=\color{blue}{-6},C=\color{green}{-22}$ this is what you're looking for?
Answer: $-(x+3)^2-13=-(x^2+6x+9)-13=-x^2-6x-22=\color\red{(-1)}x^2+\color\red{(-6)}x+\color\red{(-22)}$
Hence:

$a=-1$
$b=-6$
$c=-22$
Answer: Let $(x+a)^2+b=Ax^2+Bx+C$
Then $A=1, B=2a, C=a^2+b$.
In your case we have to watch the signs:
Let $-(x+a)^2-b=Ax^2+Bx+C$
Then $A=-1, B=-2a, C=-a^2-b$.
$A=-1, B=-6, C=-22$

A B and C in Completing the square?