What is it called when the same melody is played at different speeds by voices with different timbre?

Question: I'm imagining this situation:

A melody is played by a voice (by a "voice" I mean an instrument or an actual human voice).
The same melody or a very similar melody is played by another voice, with a very different speed and very different timbre.

What is this called? If it doesn't have a name, what musicians explored doing it?
Here's some examples I have in mind:

Yume Nikki Ending by Kikiyama, Aphehad's remix (from 0:44). It feels to me like the human voice is singing a slower version of the main melody. Wyl Ayres' remix (1:53 - 2:46) is also relevant.
Aquarium - Rock'n'Roll Is Dead (first 21 seconds). Both the higher-pitched voice (0:00 - 0:02) and the lower-pitched voice (0:01 - 0:04) play a fainting melody, with different speeds and timbres. By the way, what I perceive as different voices here is probably caused by the same string instrument, but it doesn't matter.
Rush - Losing It (first 24 seconds). It begins with a faster voice, then after 0:05 adds a slower voice (the violin). But they play something similar.
Tiger Hifi - King of my Castle (48 seconds). The song starts with a dull echoing voice, then after 0:33 adds another voice. The second voice is slower. But they play the same melody.
Piknik - Be Forever (first 30 seconds). There's a weird effect added to the violin which I perceive as the second voice. Caveat: the voices can be said to play the melody at the same speed, but one of the voices is slower overall (has a slower fade-out).

The examples above are very obscure and some of them are potentially wrong. That's because I extremely rarely find anything similar to what I've described at the start of the post. I'm scrapping the very bottom of the barrel. That's probably because I'm very inexperienced in music.
Relevant concepts
There's a concept of doubling (a relevant question from where I took the examples below), e.g. using the vocal.

Nirvana - Smells Like Teen Spirit (2:36 - 2:46). Kurt Cobain sings "hey" to match something the guitar plays. But he doesn't sing the guitar's melody with a different speed.
Jimi Hendrix - Crosstown Traffic. Jimi Hendrix sings the guitar's melody with "doo doo doo...". But he doesn't sing it with a different speed.

So either I'm looking for a specific kind of "doubling" or "doubling" is the wrong concept for describing what I'm looking for.
I've also heard (from this video by Woochia, 1:53) that "fractal music" may involve playing the same melody at different speeds. But in Woochia's example different versions of the melody are not differentiated by timbre.
I also searched examples of fugue. But didn't find examples where the voices are given very different timbres.
And as I understand, harmony is more or less just means "multiple sounds together" (where one of the sounds is more important than the other), so it's too vague of a term.
P.S.: Here's a bit of subjectivity. It's not necessary for the question, but may be important for context. First, when I listen to music I focus on the intersection of timbre and melody. Second, I can associate emotions even with very short musical segments (I assume most people do the same). So when I hear a musical segment which has very connected (e.g. playing the same melody), but very contrasting (because of the speed and timbre) voices, I experience it like some exotic combination of contradictory emotions. Like some super-condensed, super-stimulating experience. It's like uncanny valley, but much stronger and much more positive. That's why I remember such music even if it lasts just a dozen of seconds.
Comment: also augmentation and diminution
Comment: Not enough time to write a fully-fledged answer but look up the term heterophony.
Comment: Short version of my answer: There isn't going to be any one word that encapsulates all this, and certain words are relevant to certain contexts. (E.g. augmentation and diminution are mostly in the contexts of fugues or similar contrapuntal writing; in the toolbox along with inversion and retrograde.) Sometimes a phenomenon is best described in whatever ordinary words describe it well.
Comment: You might be interested in the [Missa prolationum](https://en.wikipedia.org/wiki/Missa_prolationum).
Comment: "But didn't find examples where the voices are given very different timbres": they're out there, though.  Look at Beethoven's ninth, or any orchestral or choral/orchestral fugue where voices are played in different sections of the orchestra.  The Credo of Bach's Mass in B minor comes to mind.
Comment: @nuggethead, a type of heterophony has got to be what I'm looking for. What are the simplest examples of heterophony, with the most contrasting versions of the same melody? Is anybody creating heterophonies by using all the available modern tools? I'd assume nowadays a dedicated composer could go really wild with differentiating the same melody by using any sounds and timbres known to man.
Comment: @AndyBonner, augmentation/diminution could be very relevant too.
Answer: Depending on which is the "main" voice, it may be called, "imitation by diminution" or "imitation by augmentation." This was quite popular in the 1300s to 1600s (and used later) in "puzzle" canons.
Comment: I think [Shostakovich's Symphony No. 10, Mvmt. 2](https://youtu.be/bCypb--lv1M?si=CKo09lDMSAAqHVlf&t=2m31s) fits the examples in the OP. I've always thought its "return of the scherzo proper" (at 2:31 in this recording) involved a lot of diminution.
Comment: @Dekkadeci, at least 20 seconds (from 2:31) are extremely close to what I'm talking about. Or 100% close.
Answer: Having listened to the examples, I think the only answer that wraps it all up is: "No, there's no one term that's useful here. And yes, your response to these musical similarities is meaningful. But to be able to talk about it more concretely, keep enhancing your skills at listening to and describing music."
I'm afraid that only the Woochia example does what you describe. In all the other examples, the two voices aren't playing the same sequences of notes, just something similar in spirit. So let's discard these examples and talk about the idea instead.

So when I hear a musical segment which has very connected (e.g. playing the same melody), but very contrasting (because of the speed and timbre) voices, I experience it like some exotic combination of contradictory emotions. Like some super-condensed, super-stimulating experience. It's like uncanny valley, but much stronger and much more positive. That's why I remember such music even if it lasts just a dozen of seconds.

There's no particular word for that "Aha!" moment of recognizing "This is the same material that I heard before!" But it's very powerful, and in fact maybe there's no word for it because it's so fundamental to so much music. The power of recognition is significant; it's the same thing that makes a comedian's "callback" work, in which they make a joke early on, and then merely mentioning it later in the routine gets a big laugh. Perhaps there's no word for "using music that was used earlier" because it's so common. In fact, there's not much music that just goes all the way through using new material all the time ("through-composed"). A lot of music has verses and choruses that repeat. Even if there's a structure that less obviously repeats, there are "themes" or "motifs" that recur, like the four-note opening of Beethoven's 5th; they "recur" so dang much that he pretty much builds a whole movement out of them.

Wagner's operas used these "motifs" to symbolize plot points or characters, and John Williams popularized the same approach with the Star Wars soundtracks, so merely quoting a motif can give the listener an "aha moment" that says something about the plot.
When musical material is repeated immediately by another voice, the word "imitation" can be helpful. This is a very broad word that covers lots of uses. For instance, in "Spring" from Vivaldi's "Four Seasons," three violins imitate birds (literally "imitate," as in do impressions) and repeat their own and each others' material, though not always in the same patterns:

Or "imitation" can describe a much stricter process, like the way canons and rounds are created, each voice doing the same thing but a bit later. Many such genres have names involving words for hunting or chasing, as one voice "chases" after another. Some late-renaissance choral music was "polychoral," in which there would be two choirs, on either side of a church, working together or in imitation, "in stereo." Many works play with "echo" effects, like this one in which Biagio Marini says to have two extra violinists hidden off stage:

One genre built on repetition is the chaconne. Bach has a famous one for solo violin which includes some of the most powerful "aha moments" of recognition for me. It's a hefty solo, with a length worthy of prog rock; the performer here brings it in at a shorter run time than In A Gadda Da Vida only by playing it faster than most, more the speed it's supposed to go:

The whole chaconne is built by just repeating the same chords over and over and building "variations" on them, but the opening material comes back in the middle (5:27) and at the end. And immediately after the middle statement, we get the same theme, transformed from a minor key into major. After "everything we've been through" to get to that point, it's a moment of profound transmutation. Chris Thile in this video loses some of the setup by starting right from that apotheosis, but he has something of the "aha":

These terms so far don't concern differences in instrumentation or timbre, as you're focused on. There aren't a lot of words for changes in timbre; sometimes it gets overlooked in analysis. But it's used often, as a composer might have different instruments imitate each other (Beethoven's 5th, above, is full of it). Broad words like "tone color" or "orchestration" are relevant, but you pretty much just have to describe what happens, like "around 3:30 the strings and winds alternate, taking turns sounding the same tentative chord repeatedly."
A number of artists or works play with the notion of giving the same material to the human voice and to an instrument, sometimes simultaneously. The whole notion of scat singing came from vocals imitating instrumental idioms, and there are a number of jazz guitarists who like to double a played line with simultaneous scatting:

In Different Trains, Steve Reich took snippets of recorded speech and mapped the vocal intonation to musical pitches, then gave these melodic lines to instruments to imitate.
Comment: Also: for the idea of slowing a melody down and stretching it out, see the medieval practice of taking an existing song, stretching it wayyyyyy out, and using it as one of the voices in a multi-voice work: https://youtu.be/WSsutCu8PIo?t=276 . Also, read about John Cage's "[As Slow as Possible](https://en.wikipedia.org/wiki/As_Slow_as_Possible)"—making news just a month or so ago for changing a note!
Comment: "that "Aha!" moment of recognizing 'This is the same material that I heard before!'": this reminds me of the first time I heard Cary John Franklin's moody, modern "Coming from Evening Church," and realizing only at the moment of the final cadence that one of the parts had been singing, very slowly and without words, the Tallis Canon.  (Just one part sings the tune, though; it isn't presented as a canon.)  Somehow the realization reaches back in time and the memory of what has just happened suddenly undergoes some sort of phase shift.
Comment: Will accept your answer if a better one doesn't appear. But I'd like to discuss my examples for a bit too. Could there be anything simple and objective about the way voices relate in them? "Losing It" starts with fast-changing sounds; then adds a violin which becomes similarly fast-changing. I listened to Tiger Hifi on different speeds and it seems that each voice plays a melody with a slower and a faster half (not sure). In "Rock'n'Roll Is Dead" it seems like both voices play a couple of notes before fading out. So could there be any patterns in the structure of melodies?
Comment: @FlowyPoosh Yeah, I'd love to get into it in [the chat room](https://chat.stackexchange.com/rooms/440/the-practice-room), which is better for discussion than comments. (And yes, there are plenty of *other* ways to find similarities between two sets of material other than having the same notes in the same order!)
Answer: Imitation is the broad, general term to describe when one voice plays some melodic idea and then another voice plays that melodic idea again. But there are several additional terms to specify the details of how that imitation happens. There isn't a need to list all of that terminology here, a counterpoint textbook will cover it.
If some rhythm is played and used again but at either with faster of slower note values, while maintaining the same relative proportion of note durations, usually it's done by doubling or halving note values, it is called rhythmic diminution or augmentation.
When I listen to the examples in your question I don't really hear imitation, at least not sustained imitation. A few of the example had very brief segments of melody, something you might call a motif, repeated by imitation. I didn't hear any rhythmic diminution or augmentation. But those device can be difficult to hear and are often clear from reading a score.
With no large scale imitation at play in the examples it would probably be better to just use the broad term "counterpoint" to describe the interaction of the different melodic parts. Two terms come to mind regarding one part moving fast relative to another slow part. Species counterpoint as taught by Fux has five categories and each category is based on the rhythmic relation of the voices. Species 2, 3, and 5 are specifically about counterpoint parts moving faster than the other parts.
The second term is melisma, and while the proper use of that term is about vocal music and the relation of rhythm to syllables of sung text, in a multi-voice setting a melismatic line often has a florid rhythm which moves faster than other harmonizing lines. Melisma has a strong connotation of a fast melodic part moving against some other slower part.
I don't know a term that involves both imitation and contrasting timbre, but there is an idea that comes close. Antiphony is when instrumental groups alternate in a type of call and response meant to contrast the groups spatially or timbrally.  Antiphony does not necessarily involve imitation but it can.
Ritornello is somewhat related to the idea of antiphony but it involves the contrast of a solo instrument or small instrumental group against a full orchestra in the concerto form. Often the ritornello repeats a melodic idea so that hearing it, for example, first in the orchestra then again from the soloist provides a definite timbral contrast. That type of melodic repetition isn't imitation in the contrapuntal sense.
I would not be too concerned about applying a term about contrasting timbre for the music examples you gave. While I did hear a few brief occurrences of imitation nothing gave me the sense that contrasting timbre was integral those passages. If similar passages had occurred between similar instruments, two voices or two guitar, for example, I would not think that an important difference than contrasting instruments. Mostly it seems like a kind of brief, imitation between whatever happens to be the instrumentation of the particular bands.
When you bring up the "uncanny valley" idea (an idea I only know about regarding human-like robots) I think you are suggesting something like a psychedelic or swirling feeling. Echo and reverberation are often used to create that mood, and as they involve time delay there is a relation to contrapuntal imitation. But another music device that I think creates a swirling, ping-pong echo type of feel is hocketing, which is a quick alternating between separate voices. It isn't an imitation technique but a way to use opposing voices. It isn't timbral contrast per se, but you are meant to perceive two distinct voices or vocal groups, and there is at least some timbral difference between the voices involved. IMO, it's an "uncanny" musical effect involving multiple voices.
Finally, I want to add that while I tried to think of music theory terms that match the details in your question, I personally hesitate to use most of them when describing rock music, the music styles in your examples. It is not that I think music theory is anti-rock, far from it, but some of these terms apply to specific historic genres of music. For example, "melisma" makes me think of ancient church music. While melisma might be technically applicable to something like modern R&B singing "riffs and runs" seems to be the contemporary term in use.
Comment: Given that the consensus of responses says there really isn't large scale imitation in the examples, I think the term you want to use is just "counterpoint." Regarding the different speeds of the melodies, I will make an addition to my answer.
Comment: Looks like a great answer! What kind of imitation do you think is present in the examples, how could we call it? I naively called it "playing the same melody", but I've been told it's wrong.
Comment: The short bits I heard would be called simply "imitation". If an imitative melody occurs, and there is no change to the original melody, there is no need for additional labels. If the imitation happens in a different octave, you can say "imitation at the octave", but it's generally assumed "imitation" with no other description, is happening at the unison or octave. I didn't hear any changes in the few bits of imitation in the examples.
Comment: I've updated my description of the examples in [the chat](https://chat.stackexchange.com/rooms/440/the-practice-room). Do you think it's more correct now? Anyway, I accept your answer.


Statistics tests for monitor changes in data distribution (concept drift) without knowing real output

Question: I'm currently struggling with concept drift problem in on-line learing. I read some papers "Ikonomovska Gama - Regression Trees from Data Streams with Drift Detection " and check their implementation of AM Rules Regresor in MOA framework and their concept drift detector based on knowing real output of each sample.
In real-word cases I don't know real output (or know after some time). So is there some statistical test that tells if there is a change in data distribution based only on sample features?
Comment: Take a look at [A Survey on Concept Drift Adaptation][1] and [Learning under Concept Drift: an Overview][2] as overviews.

  [1]: http://www.win.tue.nl/~mpechen/publications/pubs/Gama_ACMCS_AdaptationCD_accepted.pdf
  [2]: https://arxiv.org/pdf/1010.4784.pdf
Answer: Most existing approaches for handling concept drift make the bit unrealistic (for most real world application imo) assumption that the labeled data will be available at no labeling cost shortly after classification. However there are some proposed alternatives, I've come across this one :

Lindstrom, Patrick, Brian Mac Namee, and Sarah Jane Delany. "Drift detection using uncertainty distribution divergence." Evolving Systems 4.1 (2013): 13-25

So maybe it's a good starting point. hth.
Comment: Welcome to our site! Could you complete the citation by adding year and publisher/journal information, as it would appear in a bibliography?


Display the id of an object in many to many relation in Django admin

Question: I have a model(VariationPrice) in my Django app which has many to many relation with other model(Variation). Under Django Admin VariationPrice, one sees the variations field with many Variations inside it. Is it possible to display the Variation id (Variation.id) along with the Variation name in many to many field of django admin?
class VariationPrice(models.Model):
    price = models.IntegerField()
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    variations = models.ManyToManyField(Variation, blank=True)

Comment: Shouldn't this `VariantionPrice` have a `ForeignKey` to a `Variation` (only)? How is the `Variation` modeled towards a `Product`? Perhaps you do not even need a `VariationPrice` model at all.
Answer: The name of the M2M object is rendered by it's __str__()  method. So the easiest way to achieve this is to simply add the ID to your str method of the Variation model:
class Variation(models.Model):
    ...
    def __str__(self):
        return f"{self.name}-{self.id}"


Finding N formula (Backward Difference)

Question: I want to find the N formula of $▽^n f(x)=x^2+x$ Where $h=1$
So far, I reached to this point,
$▽ f(x)=f(x)-f(x-h)$
$ =x^2+x-[(x-h)^2+(x-h)]$
$= x^2+x-[x^2-2xh+h^2+x-h]$
$= x^2+x-x^2+2xh-h^2-x+h$
$= 2xh-h^2+h$
What to do next?
Comment: Welcome to stackexchange. Please edit the question since it's not clear. What is the "$N$ formula"?
Comment: @EthanBolker N formula in Backward difference
Comment: Do you mean the Nth difference?
Comment: @martycohen Yes .
Answer: The binomial theorem
appears in
one of its many guises here.
$▽ ^{(n)} f(x)
=\sum_{k=0}^n (-1)^k\binom{n}{k}f(x-kh)
$.
This can
(and should, as an exercise)
be proved by induction.
Then get
$f(x-kh)$
and substitute.

Finding N formula (Backward Difference)


How to recover from failed Mysql schema update, with replication?

Question: I have two MySQL servers configured with master-slave replication.
Before we deploy a new application version we:
1) STOP SLAVE
2) Take a MySQL dump of the slave.
However, if a mistake is done during the deployment of the new schema version (a table is dropped by mistake, for example), having the slave intact doesn't help. Our service is write-intensive, so we can't turn it back up until we have a master working.
If we now load the mysql dump back into the master, it will take a long time during which our service remains down.
What is the best-practice to recover from such a mistake?
How can I setup the system so I can easily promote the slave, turn on our service and only then tend to the broken database? Mainly, I'm worried with re-syncing the slave and the master after changes are done on the slave.
Comment: This belongs on ServerFault, I think.
Comment: Could be. I'm not sure the DB is the domain of the sysadmin. I asked it here because there's more MySQL activity on StackOverflow.
Comment: @OmerGertel: Well it's definitely not `programming related` , so I agree, should be on SF.
Answer: The method you're using is certainly the safest but there are ways you can cheat to make the process more transparent.

Keep the synchronisation turned off while you perform the next few steps.
Make the slave the master (easier if you have master/master replication instead of master/slave) and switch your application over to it.
Restore the old master from the dump file.
Re-enable synchronisation.
Ensure the the old master is synced from the former slave.
Switch the application back if so desired.
Comment: I had the hunch the answer will be something like this. I need the log-position of the slave, before I turn the application back on, so I can correctly configure the old-master as a slave on the new-master. Anything other gotchas I need to be careful about?
Comment: Nothing I can think of, although it's been a while since I've been through those steps, so don't take my word for it. ;)


Functional equation of divisibility: $m^2+f(n)\mid mf(m)+n$

Question: I am struggling with the following issue:

Find all functions $f:\mathbb{N}_{+}\to\mathbb{N}_{+} $, such that for all positive integers $m$ and $n$, there is the divisibility
$$m^2+f(n)\mid mf(m)+n\text.$$
$\mathbb{N}_{+}$ stands for the set of positive integers.

I've tried various substitutions but I  dont know how to solve functional equations of this form therefore I couldn't manage to find any $f$. I think this is a interesting problem so I'd like to know the answer.
Comment: Well, as a start, I can show that $f(1)=1$.  Proof:  setting $n=1$ we see that $m^2+f(1)$ divides $mf(m)+1$ for all $m$.  But if there were a prime $p\,|\,f(1)$ then taking $m=p$ gives a contradiction, hence there is no such $p$ which implies that $f(1)=1$.
Comment: $f(n)=n$ is a solution.
Comment: Thanks @lulu, that's just what I needed to solve the problem! :)
Comment: Can you prove there are No others?
Comment: How do we see that $m^2+f(1)$ divides $mf(m)+1$?
Comment: Look at what you are given and take $n=1$.
Answer: Thanks @lulu for showing that $f(1)=1$.  Now I can finish the problem by proving that the identity is the only possibility for $f$.

Suppose that there exists $m$ such that $f(m)<m$.  Then taking $n=1$ we have
$$m^2+1=m^2+f(1)\le mf(m)+1<m^2+1\ ,$$
a contradiction.  Now suppose there exists $n$ such that $f(n)>n$.  Taking $m=1$, we have
$$1+n<1+f(n)\le f(1)+n=1+n\ ,$$
another contradiction.  Thus $f(n)=n$ for all $n$.

Addendum.  Proof that $f(1)=1$.  This proof was provided in a comment by Lulu, I'm copying it verbatim here in case the comment disappears in the future.
Setting $n=1$ we see that $m^2+f(1)$ divides $mf(m)+1$ for all $m$.  But if there were a prime $p\mid f(1)$ then taking $m=p$ gives a contradiction, hence there is no such $p$ which implies that $f(1)=1$.


If the roots of the quadratic equation $2kx^{2}+(4k-1)x+2k-3=0$ are rational and k is an integer, how many values can k take which are less that 50?

Question: If the roots of the quadratic equation $2kx^{2}+(4k-1)x+2k-3=0$ are rational and k is an integer, how many values can k take which are less that 50 ?
The discriminant = $16k+1$
For a rational number the discriminant = $p^{2}$
i.e. $16k=p^{2}-1$
$16k=(p-1)(p+1)$
$k=\frac{(p-1)(p+1)}{16}$
$16=16*1=8*2=4*4=2*8=1*16$
Answer: So far so good.  Notice that the equation $16k=p^2-1$ shows that $p^2$ is congruent to $1$ modulo $16$.  You can then show that $p$ has to be congruent to $1,7,9,$ or $15$ modulo $16$.  This gives a list of possible $p$'s: $1,7,9,15,17,23,25,31,\ldots$.  You'll find that $p=31$ gives $k=60$, but everything smaller on the list should work.  It follows that there are $7$ such values of $k$ (if you include $k=0$).
Answer: Yes, but you should also consider multiples of $16$. For example, $p^2-1=48$ has solutions $p=+7$  and $p=-7$, so in  your case  $16*k=48$ and $k=3$. This is just one solution.


Maximize practice time on electronic drum kit

Question: I have played on an acoustic drum kit all my life but now I have unfortunately switched to an electronic drum set because I live in an apartment.
How can I maximize my practice time on an electronic drum set without loosing too much technique from acoustic training?
Is there anything else I can do to keep my acoustic technique by using an electronic drum kit?
Comment: Tony Royster Jr. extensively plays electronic Rolands and see if he looses anything. There is nothing unfortunate about electronic drumset playing. It's just different. I would suggest playing intensively for a week on the electronic drums and then rent a rehearsal place with acoustic ones to see where it feels awkward. Other than that, it's a matter of a few days to adapt to one or the other. There will be no technique loss.
Comment: I know drummers who practice on acoustics and perform on electrics. They both use the same technique. The only difference is some electrics aren't as responsive as an acoustic.
Answer: I have played both acoustic and electric drum and have never noticed any technique loss between the two. It's like learning guitar and then hoping that you don't lose your guitar technique when you learn piano. They are different instruments so in reality learning electric drums will only make you better at the other one.
Although, just like anything if you stop practicing you may forget how to do some things (including technique), but nothing you can't pick back up again.  When you go back to acoustic drums I would make sure you have plenty of time to get familiar with the acoustic kit, before performing or recording.
Comment: I would argue it's more like acoustic vs. electric guitar (though i'm not a fabulous guitarist, and barely a drummer). If you can play an acoustic, you can play an electric, and vice-versa, though they feel different due to different size strings, and so respond differently, and there are a few things you can do on one (e.g. volume swells on electric) that you can't do on other. But still close enough it's not a complete loss.


If $a+b|ab, a+c|ac, b+c|bc$ then there is a prime which divides $a,b,c$.

Question: I wish to show that if $a+b|ab, a+c|ac, b+c|bc$ then there is a prime which divides all of $a,b,c$, where $a,b,c$ are positive integers.
First, I rule out the case that any pair are coprime: in this case, $(a,b)=(ab,a+b)=1$, but now since $a+b|ab$, we must have that $a+b=1$, which forces one of $a$ or $b=0$, contradicting the positivity of $a,b$. This argument works for any pair, so we've ruled out pairwise co-primality.
Thus, we suppose that $(a,b)=d_{ab}$, $(a,c)=d_{ac}$, $(b,c)=d_{bc}$ and WLOG suppose that
$$1<d_{ab}<d_{ac}<d_{bc}$$
(if two are equal, then there is a prime factor of $a,b,c$ and we're done).
I'm having trouble finishing, though. Where can I go from here?
Comment: This is very hard to read. Please use parentheses: $(a+b)|ab$, etc.
Comment: Sorry I mistyped.
Comment: FYI, using [Approach0](https://approach0.xyz/search/?q=OR%20content%3A%24a%2Bb%20%5Cmid%20ab%24%2C%20OR%20content%3A%24a%2Bc%20%5Cmid%20ac%24&p=1), I found the AoPS [$(a,b,c)>1$](https://artofproblemsolving.com/community/c6h1785188p11783970) with your problem, and also [$x$, $y$, $z$ are positive integers such that $x + y \mid xy$ and $y + z \mid yz$. Prove that $\gcd(x, y, z) > 1$.](https://math.stackexchange.com/q/3482470/602049) which shows you only actually require $2$ of the $3$ divisibility requirements to reach the stated conclusion.
Answer: Suppose there is no common factor of all of $a,b,c$. As noted by the OP, each pair of these numbers does have a non-trivial gcd.
Let $a=ZU$ and $b=ZV$, where $U$ and $V$ are coprime. Then $Z(U+V)$ divides $Z^2UV$ and so $U+V$ divides $Z=$ gcd$(a,b)$.
The gcd of $b$ and $c$ is coprime to $Z$ and so divides $V$. Similarly,  the gcd of $a$ and $c$ is coprime to $Z$ and so divides $U$.
Hence gcd$(a,b)$ is larger than both gcd$(b,c)$ and gcd$(c,a)$.
We could have started with a different gcd and obtained a different larger gcd. This contradiction proves that there is a common factor of all of $a,b,c$.
Comment: Since U and V are coprime U+V and UV are necessarily coprime.
Comment: How did you obtain $U+V | Z$? I'm not seeing how that follows from $Z(U+V)|Z^2UV$.
Comment: ah, yes, I see! Thank you!


Why $\sum_{d|n} \mu(d)q^{n/d}= \sum_{d|n} \mu(n/d)q^d$

Question: I was working on proving Gauss' formula for the number of monic irreducible polynomials of degree d over a finite field of order q. However, me (and Serge Lang himself in his book) came up with the formula (where N(d) is number of irreducible monic polynomials of degree d): $$N(n)=(1/n)\sum_{d|n}  \mu(d)q^{n/d},$$ however it seems that the common result is
$$N(n)=(1/n)\sum_{d|n}  \mu(n/d)q^d.$$ Could someone please show me how it is that the two results are equal to each other? Thank you.
Comment: This equivalence is used in a proof by Apostol, where he notes that if $d$ runs over all the factors of $n$, so does $\frac nd$.
Comment: $$\sum_{d \mid 10} f(d) g(10/d) = f(1) g(10) + f(2) g(5) + f(5) g(2) + f(10) g(1) \\ = f(10) g(1) + f(5) g(2) + f(2) g(5) + f(1) g(10) = \sum_{d \mid 10} f(10/d) g(d)$$
Comment: I suppose you mean $N(n)$ rather than $N(d)$?
Comment: @EricWofsey Yes I meant N(n) sorry about that
Answer: Just make a change of variables in the summation.  If you let $e=n/d$, then $d=n/e$ and $e$ will range over all divisors of $n$ as $d$ ranges over all divisors of $n$.  Thus $$(1/n)\sum_{d|n}  \mu(d)q^{n/d}=(1/n)\sum_{e|n}  \mu(n/e)q^{e}.$$ Renaming the dummy variable back to $d$, this gives equality of your two expressions.


Factoring Trinomials

Question: I've been working with Trinomials recently, but I ran across a problem like this: Factor the polynomial: $m^2 - mv - 56v^2$.
Can someone point me in the right direction, please?
Answer: Hint:
Observe:
\begin{align*}
m^2-mv-56v^2&=m^2-8mv+7mv-56v^2\\
&=m(m-8v)+7v(m-8v)
\end{align*}

Factoring Trinomials


Common trend assumption

Question: I am using a basic diff-in-diff strategy and have a couple of questions regarding the common trends assumption.

I use panel level data at the individual level and graph the average of the outcome between the two groups across the years. Does this make sense? My diff-in-diff regression will be an model of within player estimates, whilst the graph will show between player estimates. So can I really visually inspect for the common trends assumption? Further, my model includes time dummies and covariates, so it is harder to say that even though the pre-treatment trends don't look similar, it might be that after controlling for time dummies and covariates, the pre-treatment trends should be similar. I can't really 'graph' this arguement though.
I'm trying to test the assumption. I am using the approach given in this stackexchange post "Difference in Difference method: how to test for assumption of common trend between treatment and control group?" however that model does not make much sense to me as it has time dummies  plus a treatment dummy interacting with a time dummy which would seem to indicate colinearity problems. In terms of other tests, I have seen online that I should delete all my post treatment data, and just keep my pre-treatment data. Then, using just my pre-treatment data I should put in 'placebo years'. That is, run exactly the same model (with time dummies + covariates) but with a different treatment year, and see whether the DiD coefficient is 0 and significant. Is this the correct understanding?

My model at the moment is:
$y_{it}= u_i(\text{individual fixed effect}) +\beta_1* \text{treatment group} + \beta_2*\text{treatment period} + \text{time dummies} + \text{covariates} + e_{it}$
Thank you
Answer: For your first point, plotting the average of the outcome for the treatment and control group over time would be the right thing to do in order to see the unconditional evolution of the outcomes in both groups over time. Your statement that you are essentially using between player variation is not correct though. When using difference-in-differences (DiD) you are essentially comparing the group averages. In a simple setting with one pre- and one post-treatment period you can compute the DiD coefficient as
$$
\delta_{did} = \left[ E(y_{it}|g=1,t=1)-E(y_{it}|g=1,t=0) \right] - \left[ E(y_{it}|g=0,t=1)-E(y_{it}|g=0,t=0) \right]
$$
where $g=1$ is the treatment group, and $t=1$ is the post-treatment period (see here for further explanation). So given that the computation of the treatment effect ultimately happens at the group level, plotting the group averages over time is a good indication for whether the parallel trends assumption holds.
For your second question, doing the procedure in the answer you linked is actually very similar to including placebo treatments. Suppose you have time periods $t = 1, 2,...,k,...,T$ periods where the treatment happens between $k$ and $k+1$ (so time $k$ is your last pre-treatment period). In your setting, you could run the following regression:
$$
y_{it} = \text{individual Fe}_i + \text{time FE}_t + \sum_{j\neq k} \delta_j \left( \text{treatmentgroup}_i \cdot I(t=j) \right) + X'\gamma + \epsilon_t
$$
i.e. you are interacting the treatment group indicator with time dummies for all periods except for period $k$ (because you need to leave one interaction out as otherwise there will be perfect multicollinearity) which are the $I(\cdot)$ terms in the regression equation.
Then all the $\delta_j$ with $j<k$ are placebo tests for whether the treatment had an effect on the outcome between the two groups. This should not happen because if the treatment has an effect before it even occurs, then this casts doubts on the parallel trends assumption. Plotting these coefficients is basically the "conditional" outcome distribution plot as compared to plotting the unconditional outcome evolution over time as discussed for point 1.
The nice thing is that the $\delta_j$ coefficients with $j>k$ then show you how the treatment effect evolves over time, i.e. how long it takes to fade away or whether it is persistent. An example of how such a coefficient plot would look like is shown below. There is a nice command available in Stata (and I think also in R) called coefplot which does this for you.

Here the omitted time period is 1940 which is the last pre-treatment period). None of the coefficients before are statistically significant and afterwards you see a permanent effect of the treatment (in this particular case).
Comment: nice graph, actually I'm trying to replicate it: can you provide me the do file? thanks
Answer: But what about the first pre-treatment period (1900 in your example)? Assuming a placebo treatment (1 for observations that are in treatment group and year is 1900), every observation in the treatment group is coded with one and then there is multicollinearity, leading the first pre-treatment interaction variable to be dropped.

Common trend assumption


Baseline rate of missing values. Can missing values be MAR and MCAR?

Question: Define $\pi_i$ as the probability that person $i$ will be missing from your sample and $Y_i = 1$ denotes that a subject is missing. Say we're in a missing at random (MAR) scenario where $\pi_i$ depends on two known continuous variables $X_1$ and $X_2$:
$$logit(\pi_i) = \beta_1 x_1 + \beta_2 x_2$$
Let's say that I introduce a "baseline" rate of missing values, $\beta_0$, that is independent of all covariates. For a specific example, let's say there is a 5% chance that an observation is missing. In this case, $\pi_i$ is described by:
$$\text{logit}(\pi_i) = \beta_0 + \beta_1 x_1 + \beta_2 x_2$$.
In our specific example of 5% baseline missing, this would be:
$$\text{logit}(\pi_i) = \text{logit}^{-1}(0.05) + \beta_1 x_1 + \beta_2 x_2$$
In this scenario, there parts of $\pi$ that depend on known covariates, i.e. $\beta_1$ and $\beta_2$. There is also a part of $\pi$ that doesn't depend on any covariates: $\beta_0$.
If we observe missing values through this second scenario, is this considered MAR? My guess it that it is because the distribution of $\pi$ depends on $X_1$ and $X_2$ even if not all parts of $\pi$ do.
Answer: Yes, the second scenario would be considered MAR. The usual definition of MCAR is something like $P(Y=1|data) = P(Y=1)$; the probability of missingness is independent of the data. That doesn't apply in this case unless $\beta_1, \beta_2 = 0$. Therefore this scenario is not MCAR.
I suppose technically one could imagine the other direction. If we have MCAR data, can we say it is also MAR? I think the answer is yes, depending on how exactly MAR is defined.


Number of die rolls needed for average to converge to roll expectancy?

Question: I'm aware there are similar questions, but I haven't been able to find what I'm asking.
Say we have an $n$-sided die, labeled with 1 thru $n$ and roll it $N$ times.
We take the average, call it $m$.
The die is fair, so the expectancy for the die roll is $E=\frac{n+1}{2}$.
How large must $N$ be for the average to be within $\epsilon$ from $E$ with a probability, say, $p$?
For example, 20-sided die:
$E=10.5$, choose $\epsilon = 0.01$, and $p=0.99$.
So how many times do I have to roll the 20-sided die for the average to lie in the interval $[10.49, 10.51]$ with 99% probability?
Comment: You can approximately answer such questions using the central limit theorem.
Comment: @Qiaochu Yuan, How does  one apply the central limit theorem as an approximation for large N rolls? Thank you.
Answer: The variance of a single roll is $\frac{n^2-1}{12}$ so the standard deviation of the average of $N$ rolls is $\sqrt{\frac{n^2-1}{12N}}$.
For a normal distribution, the probability of being within $\Phi^{-1}\left(\frac{p +1}{2}\right)$ standard deviations of the mean is $p$, where  $\Phi^{-1}$ is the inverse of the cumulative distribution of a standard normal.
For large $N$ you can use the central limit theorem as an approximation, so you want $\sqrt{\frac{n^2-1}{12N}}\Phi^{-1}\left(\frac{p +1}{2}\right) \le \epsilon$, i.e. $$N \ge \left(\frac{n^2-1}{12}\right) \left(\frac{\Phi^{-1}\left(\frac{p +1}{2}\right)}{\epsilon}\right)^2. $$
So in your numerical example $\left(\frac{p +1}{2}\right)=0.995$, $\Phi^{-1}\left(\frac{p +1}{2}\right) \approx 2.5758 $, $\epsilon=0.01$ and $n=20$ so $$N \ge 2206103.1$$ which is certainly large.
Comment: How did you apply the central limit theorem as an approximation for large N rolls? Thank you.
Comment: @Frank: The distribution of the sample mean multiplied by $\sqrt{N}$ is, thanks to the Central Limit Theorem, approximately normal with mean of the population $\mu$ and standard deviation $\sigma$.  So the distribution of the mean (or the sum) is approximately normal for large $N$ with mean $\mu$ (or $N\mu$ ) and standard deviation $\sigma/\sqrt{N}$ (or $\sigma \sqrt{N}$)
Comment: Thank you for your answer. May I ask the analytical expression which shows  the convergence of a 6 sided fair dice's expected value to 3.5 as  a function of number of rolls.? I realize that there may need to a confidence interval. Thank you.
Comment: Try asking a new question.  The Central limit theorem can be demonstrated with characteristic functions.  That for a discrete uniform distribution such as a $6$-sided die is at https://en.wikipedia.org/wiki/Discrete_uniform_distribution
Comment: I just followed your advice and asked a new question, http://math.stackexchange.com/questions/1933507/what-is-the-analytical-expression-which-shows-the-convergence-of-a-6-sided-fair-dice. Please take a look at it when you have time. Thank you. Frank


Cannot find a submodule imported inside a module installed from npm

Question: I would like to use a node.js module from https://github.com/asbjornenge/react-datalist using browserify.
I did install the module locally at my working directory.
In that directory, I created a javascript file, main.jsx
var React = require('react');
var ReactDatalist = require('react-datalist');

var options = ['apple','orange','pear','pineapple','melon'];
React.render(<ReactDatalist list="fruit" options={options} />, document.body);

Then, I reactify like this:
browserify -t reactify main.jsx > main.js

So far so good, but the problem is when I want to browserify:
browserify main.js > bundle.js

I got an error:
Error: Cannot find module './components/DataList' from '...my working directory...'
at /usr/local/lib/node_modules/browserify/node_modules/resolve/lib/async.js:55:21
at load (/usr/local/lib/node_modules/browserify/node_modules/resolve/lib/async.js:69:43)
at onex (/usr/local/lib/node_modules/browserify/node_modules/resolve/lib/async.js:92:31)
at /usr/local/lib/node_modules/browserify/node_modules/resolve/lib/async.js:22:47
at FSReqWrap.oncomplete (fs.js:95:15)

In ...my working directory../node_modules/react-datalist/src/ReactDataList.js, this is defined:
import React          from 'react'
import DataList       from './components/DataList'
import DataListOption from './components/DataListOption'
import layout         from './styles/react-datalist.styl'

This is unclear to me about the scope of import. I thought node.js import mechanism should work locally, but why does it try to find './components/DataList' right from my working directory?
There must be some concept about importing modules I don't know yet. So I would appreciate if you can point out to some references.
Answer: I feel embarrassed now. The problem is I didn't realize the transformation step and the bundling step are already combined into a single command line. Hence, instead of
browserify -t reactify main.jsx > main.js
browserify main.js > bundle.js

Do this
browserify -t reactify main.jsx > bundle.js


Why is softmax considered counter-intuitive for multi-label classification?

Question: In the FB paper on Instagram multi-label classification (Exploring the Limits of Weakly Supervised Pretraining), the authors characterize as "counter-intuitive" their finding that softmax + multinomial cross-entropy worked much better than sigmoid + binary cross-entropy:

Our model computes probabilities over all hashtags in the vocabulary using a softmax activation and is trained to minimize the cross-entropy between the predicted softmax distribution and the target distribution of each image. The target is a vector with $k$ non-zero entries each set to $1/k$ corresponding to the $k ≥ 1$ hashtags for the image.
We have also experimented with per-hashtag sigmoid outputs and binary logistic loss, but obtained significantly worse results. While counter-intuitive given the multi-label data [...]

Why is this counterintuitive?
The sigmoid binary loss would encourage the true label logits to be much higher than 0, and the other logits to be much smaller than 0. I think everyone agrees that this is intuitive.
The softmax multinomial loss would encourage the true label logits to be roughly the same, and much higher than the other logits. The only real difference vs. the sigmoid binary is that the true label logits are now pushed to be close to each other. It does constrain the logits behavior  (the model now needs to keep all the true label logits in some small-ish range, except for an arbitrary per-image shift). I guess this constraint may help or harm depending on much it helps with regularization, but I don't think it would be considered counter-intuitive?
Is there anything else I'm missing?
(There's another difference: with softmax multinomial loss, only relative, not absolute, values of logits matter -- but that seems to be a very minor point.)
Answer: It is actually the opposite of what you said "The sigmoid binary loss would encourage the true label logits to be much higher than 0, and the other logits to be much smaller than 0. I think everyone agrees that this is intuitive."
When applying the logit and taking binary cross-entropy loss, we encourage each output component to be independent of each other: one logit is higher doesn't push the other logits to be smaller. This is exactly the opposite of softmax, which encourages one logit to be higher while all others to be smaller. This is why it's counterintuitive, as indicated in the paper.
Answer: This was also confusing for me too! And I didn't quite think that the accepted answer or well, the only answer covered the source of confusion completely. The issue here isn't just with softmax, because even though you want to minimize the other logits, the loss will not converge unless the prediction and target vectors are nearly the same. If the target labels contained 1 for each present class, then because of softmax your loss will never converge -- its an impossible problem. But, in the mentioned paper the target labels are divided by k where k is the number of non-zero labels in the target vector. Then our loss function can converge.
However, now think about what this target distribution is saying. Given your input, X, it is equally likely that the classification should be any of the k-assigned labels. This is not true since this is not an either-or scenario rather AND! This is where lack of independence comes in and is covered by the first sentence of the second paragraph of the answer above, and I've reposted below.

When applying the logit and taking binary cross-entropy loss, we encourage each output component to be independent of each other


What Linux command do I type to see my MySQL server load in real time?

Question: What Linux command can I type where I can watch MySQL server load in real time?
BACKGROUND:
I can type 'top' and watch overall server load (just look at inverse of Idle % and consider it over a period of time). But I need to know how to see when MySQL is running into critical load and may run out of process handles or something like that unless I do something about it.
Comment: Collect metrics with Cacti or Munin.
Answer: Try innotop, a top clone for MySQL. It is probably already in your package repositories.
Answer: I can recommend you mytop, a top clone for MySQL. It is also available in repositories of most common Linux distributions.
Comment: Hm. That doesn't seem to have been maintained in years.


How to disable symfony validation of a non mapped field

Question: I have an error message "This value is not valid." on a non mapped field. This field is a dropdown with a maximum of 20 items. The user can load more results using ajax. The error happens only when I choose a value loaded using ajax. If I use an element from the original list that shows when the page loads, I don't have this error. I understand that symfony considers it a non valid value because it is not in the original query result, but that's not what I want.
Here is the field definition in the form type:
<code>    ->add('clientcustomer', EntityType::class, array(
        'label' => 'Customer',
        'class' => Customer::class,
        'choices' => $this->customerRepository->findClientCustomersForSelect2(),
        'placeholder' => 'New customer',
        'required' => false,
        'mapped' => false,
    ))
</code>
I would like to disable validation for this field.
Answer: You can use FORM EVENTS see documentation Form-Events
Use PRE_SUBMIT event to update your dropdown choices based on form submitted values


Proposal distribution for a generalised normal distribution

Question: I am modelling plant dispersal using a generalised normal distribution (wikipedia entry), which has the probability density function:
$$ \frac{b}{2a\Gamma(1/b)} e^{-(\frac{d}{a})^b} $$
where $d$ is distance travelled, $a$ is a scale parameter, and $b$ is the shape parameter. Mean distance travelled is given by the standard deviation of this distribution:
$$ \sqrt{\frac{a^2 \Gamma(3/b)}{\Gamma(1/b)}} $$
This is convenient because it allows for an exponential shape when $b=1$, a Gaussian shape when $b=2$, and for a leptokurtic distribution when $b<1$. This distribution crops up regularly in the plant dispersal literature, even though it is pretty rare in general, and hence difficult to find information about.
The most interesting parameters are $b$ and mean dispersal distance.
I am trying to estimate $a$ and $b$ using MCMC, but I am struggling to come up with an efficient way to sample proposal values. So far, I have used Metropolis-Hastings, and drawn from uniform distributions $0 < a < 400 $ and $ 0 < b<3$, and I get posterior mean dispersal distances of about 200-400 metres, which does make biological sense. However, convergence is really slow, and I am not convinced it is exploring the full parameter space.
Its tricky to come up with a better proposal distribution for $a$ and $b$, because they depend on one another, without having much meaning on their own. The mean dispersal distance does have a clear biological meaning, but a given mean dispersal distance could be explained by infinitely many combinations of $a$ and $b$. As such $a$ and $b$ are correlated in the posterior.
So far I have used Metropolis Hastings, but I am open to any other algorithm that would work here.
Question: Can anyone suggest a more efficient way to draw proposal values for $a$ and $b$?
Edit: Additional information about the system: I'm studying a population of plants along a valley. The aim is to determine the distribution of distances travelled between by pollen between donor plants and the plants they pollinate. The data I have are:

The location and DNA for every possible pollen donor
Seeds collected from a sample of 60 maternal plants (i.e. pollen receivers) that have been grown and genotyped.
The location and DNA for each maternal plant.

I do not know the identity of the donor plants, but this can be inferred from the genetic data by determining which donors are the fathers of each seedling. Let's say this information is contained in a matrix of probabilities G with a row for each offspring and a column for each candidate donor, that gives the probability of each candidate being the father of each offspring based on genetic data only. G takes about 3 seconds to compute, and needs to be recalculated at every iteration, which slows things down considerably.
Since we generally expect closer candidate donors to be more likely to be fathers, paternity inference is more accurate if you jointly infer paternity and dispersal. Matrix D has the same dimensions as G, and contains probabilities of paternity based only a function of distances between mother and candidate and some vector of parameters. Multiplying elements in D and G gives the joint probability of paternity given genetic and spatial data. The product of multiplied values gives the likelihood of dispersal model.
As described above I have been using the GND to model dispersal. In fact I actually used a mixture of a GND and a uniform distribution to allow for the possibility of very distant candidates having a higher likelihood of paternity due to chance alone (genetics is messy) which would inflate the apparent tail of the GND if ignored. So the probability of dispersal distance $d$ is:
$$ c \Pr(d|a,b) + \frac{(1-c)}{N}$$
where $\Pr(d|a,b)$ is the probability of dispersal distance from the GND, N is the number of candidates, and $c$ ($0< c <1$) determines how much contribution the GND makes to dispersal.
There are therefore two additional considerations that increase computational burden:

Dispersal distance is not known but must be inferred at each iteration, and creating G to do this is expensive.
There is a third parameter, $c$, to integrate over.

For these reasons it seemed to me to be ever so slightly too complex to perform grid interpolation, but I am happy to be convinced otherwise.
Example
Here is a simplified example of the python code I have used. I have simplified estimation of paternity from genetic data, since this would involve a lot of extra code, and replaced it with a matrix of values between 0 and 1.
First, define functions to calculate the GND:
import numpy as np
from scipy.special import gamma

def generalised_normal_PDF(x, a, b, gamma_b=None):
    """
    Calculate the PDF of the generalised normal distribution.

    Parameters
    ----------
    x: vector
        Vector of deviates from the mean.
    a: float
        Scale parameter.
    b: float
        Shape parameter
    gamma_b: float, optional
        To speed up calculations, values for Euler's gamma for 1/b
        can be calculated ahead of time and included as a vector.
    """
    xv = np.copy(x)
    if gamma_b:
        return (b/(2 * a * gamma_b ))      * np.exp(-(xv/a)**b)
    else:
        return (b/(2 * a * gamma(1.0/b) )) * np.exp(-(xv/a)**b)

def dispersal_GND(x, a, b, c):
    """
    Calculate a probability that each candidate is a sire
    assuming assuming he is either drawn at random form the
    population, or from a generalised normal function of his
    distance from each mother. The relative contribution of the
    two distributions is controlled by mixture parameter c.

    Parameters
    ----------
    x: vector
        Vector of deviates from the mean.
    a: float
        Scale parameter.
    b: float
        Shape parameter
    c: float between 0 and 1.
        The proportion of probability mass assigned to the
        generalised normal function.
    """
    prob_GND = generalised_normal_PDF(x, a, b)
    prob_GND = prob_GND / prob_GND.sum(axis=1)[:, np.newaxis]

    prob_drawn = (prob_GND * c) + ((1-c) / x.shape[1])
    prob_drawn = np.log(prob_drawn)

    return prob_drawn

Next simulate 2000 candidates, and 800 offspring. Also simulate a list of distances between the mothers of the offspring and the candidate fathers, and a dummy G matrix.
n_candidates = 2000 # Number of candidates in the population
n_offspring  = 800 # Number of offspring sampled.
# Create (log) matrix G.
# These are just random values between 0 and 1 as an example, but must be inferred in reality.
g_matrix  = np.random.uniform(0,1, size=n_candidates*n_offspring)
g_matrix  = g_matrix.reshape([n_offspring, n_candidates])
g_matrix  = np.log(g_matrix)
# simulate distances to ecah candidate father
distances = np.random.uniform(0,1000, 2000)[np.newaxis]

Set initial parameter values:
# number of iterations to run
niter= 100
# set intitial values for a, b, and c.
a_current = np.random.uniform(0.001,500, 1)
b_current = np.random.uniform(0.01,  3, 1)
c_current = np.random.uniform(0.001,  1, 1)
# set initial likelihood to a very small number
lik_current = -10e12

Update a, b, and c in turn, and compute the Metropolis ratio.
# number of iterations to run
niter= 100
# set intitial values for a, b, and c.
# When values are very small, this can cause the Gamma function to break, so the limit is set to >0.
a_current = np.random.uniform(0.001,500, 1)
b_current = np.random.uniform(0.01,  3, 1)
c_current = np.random.uniform(0.001,  1, 1)
# set initial likelihood to a very small number
lik_current = -10e12
# empty array to store parameters
store_params = np.zeros([niter, 3])

for i in range(niter):
    a_proposed = np.random.uniform(0.001,500, 1)
    b_proposed = np.random.uniform(0.01,3, 1)
    c_proposed = np.random.uniform(0.001,1, 1)

    # Update likelihood with new value for a
    prob_dispersal = dispersal_GND(distances, a=a_proposed, b=b_current, c=c_current)
    lik_proposed = (g_matrix + prob_dispersal).sum() # lg likelihood of the proposed value
    # Metropolis acceptance ration for a
    accept = bool(np.random.binomial(1, np.min([1, np.exp(lik_proposed - lik_current)])))
    if accept:
        a_current = a_proposed
        lik_current = lik_proposed
    store_params[i,0] = a_current

    # Update likelihood with new value for b
    prob_dispersal = dispersal_GND(distances, a=a_current, b=b_proposed, c=c_current)
    lik_proposed = (g_matrix + prob_dispersal).sum() # log likelihood of the proposed value
    # Metropolis acceptance ratio for b
    accept = bool(np.random.binomial(1, np.min([1, np.exp(lik_proposed - lik_current)])))
    if accept:
        b_current = b_proposed
        lik_current = lik_proposed
    store_params[i,1] = b_current

    # Update likelihood with new value for c
    prob_dispersal = dispersal_GND(distances, a=a_current, b=b_current, c=c_proposed)
    lik_proposed = (g_matrix + prob_dispersal).sum() # lg likelihood of the proposed value
    # Metropolis acceptance ratio for c
    accept = bool(np.random.binomial(1, np.min([1, np.exp(lik_proposed - lik_current)])))
    if accept:
        c_current = c_proposed
        lik_current = lik_proposed
    store_params[i,2] = c_current

Comment: Are you looking for a prior on a and b, or for a proposal distribution in a Metropolis-Hastings algorithm? You seem to have used both terms interchangeably.
Comment: You are right - sorry for not being clear. I am most interested in a proposal distribution for MH. I have changed the title where I mentioned priors accordingly.
Comment: Under a flat or Jeffreys prior on $a$, i.e., $\pi(a)\propto 1$ or  $\pi(a)\propto 1/a$ I believe that a change of variable to $\alpha=a^{-b}$ produces a closed-form conditional $\pi(a|b,\text{data})$.
Comment: See https://en.wikipedia.org/wiki/Generalized_normal_distribution
Comment: It remains quite unclear whether or not you are interested in setting a prior that helps or in running Metropolis-Hastings more efficiently.
Comment: When the likelihood is a computational burden, ABC techniques may come to the rescue.
Comment: @tellis could you explain a bit more about this 'additional parameter' and also about the observations that you make instead of the (direct) observation of the distance.
Comment: Thanks for the comments: I have expanded the question to give some details of the rest of the analysis. I have also highlighted the question; I am looking for a more efficient way to draw proposal values for a and b.
Comment: Also, although I have been using Metropolis Hastings so far, I am entirely open to a solution using another algorithm, such as Gibbs sampling.
Answer: You do not need to use the Markov Chain Monte Carlo (MCMC) method.
If you are using uniform prior distributions then you are doing something very similar as maximum likelihood estimation on a restricted space for the parameters $a$ and $b$.
$$P(a,b;d) = P(d;a,b) \frac{P(a,b)}{P(d)} =  \mathcal{L}(a,b;d) \times const $$
where $\frac{P(a,b)}{P(d)}$ is a constant (independent from $a$ and $b$) and it can be found by scaling the likelihood function such that it integrates to 1.
The log likelihood function, for $n$ iid variables $d_i \sim GN(0,a,b)$  is:
$$\log \mathcal{L}(a,b;d) = -n \log(2a) - n \log\left(\frac{\Gamma(1/b)}{b} \right) - \frac{1}{a^b} \sum_{i=1}^n \left( d_i \right)^b $$
For this function it should not be too difficult to plot it and find a maximum.
Comment: I have added an example using simulated data.
Comment: This gird interpolation would work for two parameters and observed distances, and might be what I end up doing. I am in fact doing joint estimation of dispersal distance and paternity inference, which involves at least one other parameter to integrate over, and an extra likelihood term which is really slow (~3 seconds per iteration) that really slows down the chain. I think I would need about 10-fold more iterations than I currently used for the markov chain.
Comment: @tellis those terms 'dispersal distance' and 'paternity inference' I do not really understand. Maybe you could provide some more concrete information by adding a dataset or a piece of it. While doing that you might as well speak more about the 'one other parameter'. So, what *data* is it that you *do* have?
Answer: I don't quite understand how you are setting up the model: in particular, it seems to me that for a given seed, the possible pollen dispersal distances are a finite set, and thus your "dispersal probability" might be better termed a "dispersal rate" (as it would need to be normalized by summing over putative fathers to be a probability). Thus the parameters may not quite have the meaning (as in, plausible values) that you expect.
I have worked on a couple of similar problems in the past and so I'll try to fill in the gaps in my understanding, as a way of suggesting a possible approach/critical look. Apologies if I completely miss the point your original question. The treatment below basically follows Hadfield et al (2006), one of the better papers about of this kind of model.
Let $X_{l,k}$ denote the genotype at locus $l$ for some individual $k$. For offspring $i$ with known mother $m_i$ and putative father $f$, let the probability of the observed offspring genotypes be $$G_{i,f} = \prod_l \mathrm{Pr}(X_{l,i}|X_{l,m_i}, X_{l,f}, \theta)$$ -- in the simplest case this is simply a product of Mendelian inheritance probabilities, but in more complicated cases may include some model of genotyping error or missing parental genotypes, so I include nuisance parameter(s) $\theta$.
Let $\delta_i$ be the pollen dispersal distance for offspring $i$, and let $d_{m_i,f}$ be the distance between known mother $m_i$ and putative father $f$, and let $D_{i,f} = q(d_{m_i,f}|a,b,c)$ be the dispersal rate (e.g a weighted combination of generalized normal and uniform pdfs as in your question). To express the dispersal rate as a probability, normalize w.r.t to the finite state space: the (finite) set of possible dispersal distances induced by the (finite) number of putative fathers in your study area, so that
$$\tilde{D}_{i,f} = \mathrm{Pr}(\delta_i = d_{m_i,f}|a,b,c) = \frac{D_{i,f}}{\sum_k D_{i,k}}$$
Let $P_i$ be the paternal assignment of seed $i$, that is $P_i = f$ if plant $f$ is the father of offspring $i$. Assuming a uniform prior on paternity assignments,
$$\mathrm{Pr}(P_i = f|a,b,c,\theta,X) = \frac{G_{i,f}\tilde{D}_{i,f}}{\sum_k G_{i,k}\tilde{D}_{i,k}} = \frac{G_{i,f}D_{i,f}}{\sum_k G_{i,k}D_{i,k}}$$ In other words, conditional on other parameters and genotypes, paternal assignment is a discrete r.v. with finite support, that is normalized by integrating across said support (possible fathers).
So a reasonable way to write a simple sampler for this problem is Metropolis-within-Gibbs:

Conditional on $\{a,b,c,\theta\}$, update paternity assignments $P_i$ for all $i$. This is a discrete r.v. with finite support so you can easily draw an exact sample
Conditional on $\{P_i,\theta\}$, update $a,b,c$ with a Metropolis-Hastings update. To form the target, only the $D$ values in the equations above need to be updated, so this isn't costly
Conditional on $\{P_i,a,b,c\}$, update $\theta$ with a MH update. To form the target, the $G$ values need to be updated, which is costly, but the $D$ do not.

To decrease the cost of drawing samples of $\{a,b,c\}$, you could perform steps 1-2 multiple times before 3. To tune the proposal distributions in steps 2-3, you could use samples from a preliminary run to estimate the covariance of the joint posterior distribution for $\{a,b,c,\theta\}$. Then use this covariance estimate within a multivariate Gaussian proposal. I'm sure this isn't the most efficient approach, but it is easy to implement.
Now, this scheme may be close to what you are already doing (I can't tell how you are modelling paternity from your question). But beyond computational concerns, my larger point is that the parameters $a,b,c$ may not have the meaning you think they do, with regards to mean dispersal distance. This is because, in the context of the paternity model $\mathrm{Pr}(P_i|\cdot)$ I described above, $a,b,c$ enter into both numerator and denominator (normalizing constant): thus, the spatial arrangement of plants will have a potentially strong effect on which values of $a,b,c$ have a high likelihood or posterior probability. This is especially true when the spatial distribution of the plants is uneven.
Finally, I suggest you take a look at that Hadfield paper linked to above and the accompanying R package ("MasterBayes"), if you haven't already. At the least it may provide ideas.
Comment: My approach is indeed modelled on Hadfield's, with two major changes:
(1) seeds from a mother may be full siblings, and therefore not independent. The problem is therefore one of jointly inferring dispersal, paternity, bat also sibship structure as well.
(2) I am using a fractional paternity approach to consider all candidates simultaneously in proportion to their likelihood of paternity, rather than update paternity assignments sequentially, because there is a large space of possible fathers to explore.
Comment: I am using the package [FAPS](https://onlinelibrary.wiley.com/doi/abs/10.1111/1755-0998.12782) to do those things.
Comment: My question is essentially asking about an efficient proposal distribution for do your point 2. The rest of your answer describes something very close to what I have already done, including the formulation of the product of G and D (but thanks for this - I wasn't sure I had done it correctly, so it is useful to know a second pair of eyes agree!).
Comment: I don't have a canned solution w.r.t proposal distribution, sorry. But I have a few observations:
**(1)** Steps 1-2 are very cheap, and can be iterated many times with little cost before moving to step 3. Even with a shoddy proposal in step 2, a lot of iterations should suffice to "make large moves" in the state space of $a,b,c$.
Comment: **(2)** The conditional distribution in step 2 is 3-dimensional. As in: easy to visualize. What does the unnormalized target of $a,b,c$ look like at a MAP estimate of the paternity assignments for a fixed $G$? Visualizing the unnormalized target across different paternities should give you a sense of if it's multimodal, flat in areas, etc.
Comment: **(3)** On what basis do you suspect the sampler is not mixing well, aside from thinking that the parameter estimates are biologically implausible? Is the chain getting stuck, is the rejection rate really high, etc. If it is ONLY that you think the parameter estimates are implausible, then your time is better spent thinking critically about the model than chasing different computational methods ...for example, conditional on a paternity assignment, how does the normalizing constant (denominator) of $\mathrm{Pr}(P_i|a,b,c)$ contribute to the conditional posterior density of $a,b,c$? Good luck!


How can I change the routes for Taxons in sylius-standard?

Question: I am trying to change the routes for my Taxons but cannot find a way to do it.

Current: {domain}/taxons/{taxon name}
Desired: {domain}/categories/{taxon name}

After searching Google/Stackoverflow I only could find a solution from 2018 that isn't working anymore.
I am using Sylius version 1.11.5.
Answer: Found the answer after some messing with the Symfony Debug bar. Just put the following piece of code in config/routes/sylius_shop.yaml.
sylius_shop_product_index:
    path: /categories/{slug}
    methods: [ GET ]
    defaults:
        _controller: sylius.controller.product:indexAction
        _sylius:
            template: "@SyliusShop/Product/index.html.twig"
            grid: sylius_shop_product
    requirements:
        slug: .+(?<!/)

This overrides the Route defined in vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/Resources/config/routing/product.yml.


How to rebuild everything that can be rebuild during module development?

Question: During module development, Drupal does not use all relevant information provided by a module on every HTTP request. Examples:

Changes to an implementation of hook_menu() are only used after calling menu_rebuild().
Changes to implementations of hook_node_access_records() and hook_node_grants() are only used after calling node_access_rebuild().
Classes referenced in the files[] array of the .info file are only rebuild after enabling the module. During module development, this means it has to be disabled first, along with all other modules that depend on it.

This list might not be exhaustive.
Is there a way to update all these information in the database in one go. Preferably via a Drush command?
Answer: drush cc all

Catches most of it. Registry/files[] changes can be tricky, but should work as well in most cases.
Node access records are not rebuilt by that, that wouldn't make sense to me. Unless working on a node access module, you don't want to have that rebuilt, as there could possibly be many nodes which means that this process could take a very long time.
Answer: Disable the Drupal cache
D6: place this in settings.php $conf['cache_inc'] = 'includes/cache-install.inc';
D7: http://drupal.org/node/797346#comment-3681298


Right triangle $\triangle ABC$, $D$ lies on $AB$, inscribed in a circle of radius $9$. Find the measure of $CD$

Question: I saw this mathematical puzzle on an Instagram post today. As title suggests, we have a right angled triangle inscribed in a circle with radius $D$ and some angles. The goal is to find the length of $CD$. I'll share my approach as an answer below. I'm not quite sure if my answer is correct, so please feel free to point out any faults in my approach and/or post your own approaches too!
Comment: In which you invent an equilateral triangle in the figure...
Comment: What do you mean by a 'canonical answer'?
Answer: This is going to be my approach to the problem:

Here's my approach for the problem:
1.) Extend $CD$ to meet $E$ that lies on the circumference. Join the center of the circle $O$ with $E$ via segment $OE$. Since $OE$ is radius, we know that $OE=9$. Notice that $\angle ECB$ is the inscribed angle of $\angle EOC$, therefore we can conclude that $\angle EOB=2\alpha$
2.) Notice that $\angle EOA=\angle EDB=180-2\alpha$, therefore $OE=ED=9$. Now we can use the intersecting chords theorem (which can easily be proven via similarity of triangles in any general cyclic quadrilateral) and conclude that:
$$9\cdot CD=6\cdot12=72$$
$$\Rightarrow CD=8$$
Comment: Nice proof, I like it.
Comment: The problem is misplaced. The triangle $\Delta$ABC is rectangle in B. Therefore point D is located on a cathetum and not on the hypotenuse.
Answer: Your solution is much nicer than what I write in the following. However, the solution below is almost straightforward.
Let's assume: $AC=y, BC=x$. Then:
$$\frac{x}{12}=\frac{\sin 2\alpha}{\sin \alpha} \implies \frac{x}{24}=\cos \alpha \\\frac{y}{6}=\frac{\sin (\pi - 2\alpha)}{\sin (\frac{\pi}{2}-\alpha)} \implies \frac{y}{12}=\sin \alpha.$$
Hence, $x^2+4y^2=24^2$. On the other hand, we already have :$x^2+y^2=18^2.$So,
$$y^2=84, x^2=240.$$
By the law of cosines in $\triangle ADC $, we obtain:
$$AD^2=36=y^2+CD^2-2CD\times y\cos (\frac{\pi}{2}-\alpha)=\\y^2+CD^2-2CD\times y\times \frac{y}{12}\implies \\ 48=14CD-CD^2\implies CD=8 \ or\ 6.$$
But $CD=6$ is not acceptable because that will imply that $\alpha= 45^{\circ}$, which is impossible.
Answer: Trig identity: $\displaystyle \;\frac{\sin 3α}{\sin α} - \frac{\cos 3α}{\cosα}
= \frac{\sin 2α}{(\sin α)(\cos α)} = 2$
Law of Sine, ΔBCD:
$\displaystyle\frac{x}{\sin (\pi-3α)} = \frac{BC}{\sin 2α} = \frac{12}{\sin α}$
$→ \;\;\displaystyle \frac{x}{\left(\frac{\sin 3α}{\sin α}\right)} = \frac{BC}{2 \cos α} = 12$
Right triangle ΔABC:
$\;BC = 18\cos(\pi-3α) = -18\cos(3α)$
$\displaystyle \frac{BC}{2 \cos α}
= -9 \left(\frac{\sin 3α}{\sin α} - 2\right)$
$\displaystyle → \quad 12 = -9 \left(\frac{x}{12} - 2\right) \qquad
→ \large\;CD = x = 8$
Comment: For ΔBCD, side x match with angle (π-3α). For ΔEAD, side y match with angle α. , Using Law of Sine, solve for y, then to x, is simpler.  My preference is still going straight for x though, as it "get out" of geometry quickly (no adding lines/points, fill in angles), into algebra problem.
Answer: The fantastic power of a point wrt. a circle solution is already found by Goku but let me show my try:
By the law of cosines on $\triangle CDO$, $OC^2=CD^2+DO^2-2CDDO\cos2\alpha$. Putting $CD=x$, $DO=3$, $OC=9$ in this equation we have
$$4x^2-24x-288+48x\sin^2\alpha=0\tag1$$
On the other hand, by the law of sines on $\triangle CDB$, $\frac{x}{12}=\frac{\sin3\alpha}{\sin\alpha}$. By the identity $\sin3\alpha=3\sin\alpha-4\sin^3\alpha$,
$$48x\sin^2\alpha=36x-x^2\tag 2$$
Combining $(1)$ and $(2)$, we have $x^2+4x-96=0$ and $x=8$ or $x=-12$. Since $x$ is positive, $x=8$.
Answer: A novel way, by calculating area 2 different ways.
Extend CD to intersect the circle at point E.  Let $\,x=CD\,,\, y=DE$
Area(ΔAEB), with base = EB
$\displaystyle \frac{(18\sin α)\,(18\cos α)}{2} = 81\,(\sin 2α)$
Area(ΔAEB), with base = AB
$\displaystyle \frac{18 × y\,\sin(\pi-2α)}{2} = 9y\,(\sin 2α)$
Area, done in 2 ways, must match: $\quad→y = 9$
$ΔCDB \sim ΔADE\;\;→ \displaystyle\frac{x}{6}=\frac{12}{y}\quad → x = 8$

Even better, Law of Sine on ΔADE:
$\displaystyle \frac{y}{\sin α} = \frac{18 \cos α}{\sin 2α}\;\; → y=9$
Answer: Law of Tangents, let $t = \tan α$
$\displaystyle \frac{x+6}{x-6} =
\frac{\tan \frac{(3α-90°)+(90°-α)}{2}}{\tan \frac{(3α-90°)-(90°-α)}{2}}
= \frac{\tan α}{\tan (2α-90°)} = -(\tan α)(\tan 2α) = \frac{-2t^2}{1-t^2}$
$\displaystyle \frac{x+12}{x-12} =
\frac{\tan \frac{(180°-3α)+(α)}{2}}{\tan \frac{(180°-3α)-(α)}{2}}
= \frac{\tan (90°-α)}{\tan (90°-2α)} = \frac{\tan 2α}{\tan α} = \frac{2}{1-t^2}$
$→ \displaystyle \frac{x+6}{x-6} + \frac{x+12}{x-12} = 2$
$→ x^2-6×12 = (x-6)(x-12) $
$\displaystyle → x = \frac{6×12}{(6+12)÷2} = \frac{72}{9} = 8$
Comment: Final calculations look like solving by geometry!


How to disable status messages?

Question: I am creating/updating terms and nodes dynamically using code. It shows a lot of status message as usual. But I need to hide those message only for this task. That means if any user creates/updates terms or nodes it will show the status message.
How can I do that? Please note that the task will run by cron, so a lot of status massage will be stacked for display. Obviously nobody wants to be the poor person to see the huge list of status messages.
Answer: take a look to Disable Messages module http://drupal.org/project/disable_messages I think that could be useful for your needs.

Features
<code>* Filter out messages that match a full text string exactly.
* Filter out messages that match a regular expression.
* Permissions to specifically hide all messages of a given type from any role.
* Disable all filtering for specific users.
* Disable all filtering for specific paths.
* Apply filtering only for specific paths.
* Debug system to get messages in the HTML without showing it to the end users.
</code>
Comment: That a good one. I think looking into it's code will be helpful. May be it can be use with corn job without altering this module. Thank you :)

How to disable status messages?


Does the IP address of a mobile device change as it connects to different cell towers?

Question: Does the IP address change as a phone moves area and connects to different cell towers?
Comment: This question doesn't fit [so].
Answer: Mobile devices and cell towers communicate with each other with Global System for Mobile Communications (GSM - see wikipedia article), which does not provide internet protocol, and thus not an internet protocol (IP) address.
So  the best would be to ask your cell provider that question. However, see https://gamedev.stackexchange.com/questions/11738/how-does-the-3g-network-assign-ip-to-smartphones


Passing variable from parent paragraph to children without using getParentEntity

Question: I have a paragraph called Boxes which is a parent paragraph referencing to many paragraphs of type Box.
I have written the preprocess function of paragraph Boxes which contains some logic that can't be done elsewhere. The result of that code is stocked in a variable called custom_color_weight.
I wanna access this variable in the box paragraph template but I can't move the code that yielded that variable's value outside the preprocess boxes function.
I was searching for a way to pass the variable from preprocess boxes to the preprocess box and hence to the template of the box paragraph Is there any way to do it?
Ps : I can't use getParentEntity in the preprocess Box function to get each time the parent Boxes entity and calculate the value of custom_color right there because that way I will be loading the parent paragarph entity multiple times as I have multiple nested paragraphs of type Box inside the paragraph Boxes.
Answer: getParentEntity() is not the problem, it always returns a reference to the same instance. There you could use a bundle class to calculate and store the value. See this change record:
https://www.drupal.org/node/3191609
Then you can replace both preprocess hooks by calling the bundle class method directly from the child Twig template
<code>{{ paragraph.getparententity.getcustomcolor }}
</code>
where getCustomColor() is a method of the parent bundle class.
Comment: Entity objects are cached in memory and loading the entity again returns the cached instance. This is based on PHP passing objects always by reference.
Comment: But if i use getParententity it doesn't load the parent paragraph again and again ? I didn't understand how it just returns a reference :) Thankk you
Comment: In drupal 9 and 10 getParentEntity loads the parent entity before returning it


Mudando a orientação da tela

Question: Meu website PWA tem a seguinte diretriz no manifest.json:
"display": "standalone",
"orientation": "portrait"

Garantindo assim que seja visualizado em tela cheia e sempre em orientação retrato.
Uma certa parte, em específico, precisa ter a orientação mudada para paisagem.
Porém como o manifest definiu a orientação de início, não sei como altera-la.
Tentei incluir um novo manifest com "orientation": "landscape" porém sem sucesso.
Alguma sugestão?
Comment: É gambiarra, mas as vezes dependendo do que for vc consegue resolver se rotacionar o container 90° :), já vi em alguns casos essa técnica sendo aplicada, mas foi junto de @media etc... mas lembro que pra situação resolveu
Comment: Dentro do manifest tem uma propriedade scope, se não me engano serve para aplicar aquele manifest a uma parte (URL) específica. Pode ter faltado isso no seu
Answer: Para setar a orientação de tela do PWA você precisa que sua aplicação esteja no modo de tela cheia. Pelo menos é a solução que eu encontrei.
https://hearthero.medium.com/locking-orientation-for-ionic-pwas-7c75c5bb3639

Technically a locked screen is just a fullscreen view with a forced orientation.

Mudando a orientação da tela


Como criptografar StringConnection para um banco remoto MySql em C#

Question: Estou desenvolvendo uma aplicação que pretendo vender, e por isso criei um sistema de verificação de serial, que verifica se aquele código de ativação já está associado ao MAC do PC. Eu faço isso me conectando a um banco de dados remoto. Se alguém tiver uma ideia melhor e mais fácil de como posso criar esse tipo de serial de ativação, estou aceitando sugestões hehehe
Minha dúvida é, como eu faço pra proteger as informações de login dispostas da String de Conexão.
Tenho lá
connString "SERVER=xxx;PORT=3306;DATABASE=xxx;UID=xxx;PASSWORD=**xxx**;";

Acredito que deixa isso disposto no código permita que possam roubar os dados de acesso ao meu banco de dados. Há alguma maneira de criptografar isso? Ou teria que fazer isso direto no servidor do banco.
Answer: Se a ConnectionString estiver armazenada no app.config pode sempre seguir este tutorial bastante detalhado no CodeProject: https://www.codeproject.com/Articles/15392/Implementing-Protected-Configuration-With-Windows
Se apenas pretende encriptar/decriptar uma string, da mesma forma que faria com uma password, existe a possibilidade de utilizar uma encriptação AES: https://msdn.microsoft.com/en-us/library/system.security.cryptography.aes(v=vs.110).aspx


PHP Fatal error: Maximum execution time of -1 seconds exceeded in

Question: Been getting this error on my server all of a sudden -
Fatal error: Maximum execution time of -1 seconds exceeded in

This is happening inside my wordpress site. It shows up across many files so it's not isolated to one in particular.
Googling hasn't given me any results for the "-1 seconds" issue which is rather odd. I've tried increasing the time limit on the scripts but it doesn't resolve the issue.
I've found that refreshing the page many times will eventually give me the page I want.
I ended up reinstalling Wordpress, and I get the same issue again. I'm starting to think it's not a Wordpress issue, but have no idea where to start diagnosing the issue.
Comment: Refer to [How to Fix Fatal Error: Maximum Execution Time Exceeded in WordPress](https://www.wpbeginner.com/wp-tutorials/how-to-fix-fatal-error-maximum-execution-time-exceeded-in-wordpress/)
Answer: Try this in your php code(add top)
set_time_limit(0);
ini_set('max_execution_time', 0);

if you are getting error again and again increase the time limit and see


Why should I keep all data when reading external sources?

Question: I've been told that I should try to use as raw data as possible and not filter or transform the data when reading them into the hub. Why is this so?
If I only need a subset of the inputdata, it seems much easier to just read that subset.
Answer: The article on Raw Input lists some pros and cons.


How to get own process pid from the command prompt in Windows

Question: I'm trying to find a way to get my own PID from a command prompt (for later use in bat scripts).
So far the only useful way I found was to use getpids.exe from here : http://www.scheibli.com/projects/getpids/index.html, but I'm looking for a command that's "built in" to Windows.
I'm looking for a "bullet proof" way. No assumptions about my process being the only cmd.exe or anything.
Answer: Since none of the other solutions are bulletproof and built in, I figured I'd offer the following solution, but note that you'll need to parse/save the results somehow:
title mycmd
tasklist /v /fo csv | findstr /i "mycmd"

Comment: Don't use %random% + %time% if you have a scheduler call your batch script twice at the same time. I had the issue that the same script was called with different parameters at the same time. The seed for random was the same and I ran into collisions when creating a temporary directory. There was no way around then to handle the error when creating the temporary directory. Long story short, if you can assume that your script does not get fired of twice at the same time, this trick should work.
Comment: thats really neat. im pretty sure the current time + some randomly-generated number can be set as the title to get near bulletproof
Comment: Might be (not deeply tested) that getting a more precise timestamp (including `microseconds`, rather than 1/100 seconds like in %time%), would be more reliable in this case: `wmic os get LocalDateTime` (YearMonthDayHourMinuteSecond.Microsecond+Timezone). Unless the scheduler is able to run two things in the exact same microsecond...
Comment: This won't work if the process is running in another session on the computer as the title is shown as N/A
Comment: This gives the PID of the parent process.
Comment: The problem is, `tasklist /v` executes very slow. My Win10.21H2 has 365 running processes, `tasklist` costs 0.5 seconds, while `tasklist /v` costs almost 30 seconds.
Comment: This does not work in Windows 11 with the Windows Terminal being the default emulator.
Answer: Expanding upon Tony Roth's answer:
title uniqueTitle
for /f "tokens=2 USEBACKQ" %f IN (`tasklist /NH /FI "WINDOWTITLE eq uniqueTitle*"`) Do echo %f

Using the WINDOWTITLE filter avoids the pipe so you can put it in a for loop and assign it to a variable with SET if you like:
title uniqueTitle
for /f "tokens=2 USEBACKQ" %f IN (`tasklist /NH /FI "WINDOWTITLE eq uniqueTitle*"`) Do set ourPID=%f

Removing the /v makes it quicker, and the /NH gets rid of the header line. You need the wildcard after "uniqueTitle" because the window title actually contains the current command (thus it would go on and on if you tried to match it fully).
Answer: Using PowerShell + WMI :
powershell (Get-WmiObject Win32_Process -Filter ProcessId=$PID).ParentProcessId

Using WMIC :
for /f %a in ('wmic os get LocalDateTime ^| findstr [0-9]') do set NOW=%a
wmic process where "Name='wmic.exe' and CreationDate > '%NOW%'" get ParentProcessId | findstr [0-9]

(as always, double the % sign with for in batch files)
Comment: Nice; if you have PowerShell _Core_, it gets even easier (though neither command will be _fast_): `pwsh -noprofile -c "(Get-Process -Id $PID).Parent.Id"`
Comment: Your "Using PowerShell + WMI" -solution is the best! However I noticed that you cannot use it in a FOR -statement, because then the the parent process id the FOR-loop. My suggestion uses tempfile with GUID as the tempfile name:
`FOR /F "tokens=*" %%G in ('powershell -ExecutionPolicy Unrestricted  -Command "(New-Guid).guid"') do Set my_guid=%%~G`
`powershell -ExecutionPolicy Unrestricted  -Command "(Get-WmiObject Win32_Process -Filter ProcessId=$PID).ParentProcessId>%temp%\%my_guid%.pid`
`FOR /F "tokens=*" %%P in ('type %temp%\%my_guid%.pid') do set my_pid=%%~P`
Comment: @Ciove, you can also reach for the parent's parent : `for /F %%a in ('powershell -ex bypass "(gwmi Win32_Process -f ProcessID=$((gwmi Win32_Process -f ProcessId=$PID).ParentProcessID)).ParentProcessID"') do set my_pid=%%a`

(or even `for /F %%a in ('powershell -ex bypass "gwmi Win32_Process -f ProcessId=$PID | foreach {gwmi Win32_Process -f ProcessId=$($_.ParentProcessID)} | foreach ParentProcessID"') do set my_pid=%%a`, which might be more readable)
Comment: As you might have noticed, the powershell approach gets the parent process of the powershell subprocess just invoked, which is me, the current process. Who am I? I am my child's parent.
Answer: I believe the following is bulletproof, provided the user has access to WMIC and TEMP points to a valid path where the user has write privileges. This is the end result of some collaborative work at http://www.dostips.com/forum/viewtopic.php?f=3&t=6133.
@echo off

:getPID  [RtnVar]
::
:: Store the Process ID (PID) of the currently running script in environment variable RtnVar.
:: If called without any argument, then simply write the PID to stdout.
::
setlocal disableDelayedExpansion
:getLock
set "lock=%temp%\%~nx0.%time::=.%.lock"
set "uid=%lock:\=:b%"
set "uid=%uid:,=:c%"
set "uid=%uid:'=:q%"
set "uid=%uid:_=:u%"
setlocal enableDelayedExpansion
set "uid=!uid:%%=:p!"
endlocal & set "uid=%uid%"
2>nul ( 9>"%lock%" (
  for /f "skip=1" %%A in (
    'wmic process where "name='cmd.exe' and CommandLine like '%%<%uid%>%%'" get ParentProcessID'
  ) do for %%B in (%%A) do set "PID=%%B"
  (call )
))||goto :getLock
del "%lock%" 2>nul
endlocal & if "%~1" equ "" (echo(%PID%) else set "%~1=%PID%"
exit /b

The script establishes an exclusive lock on a temporary file that incorporates the current time into the name. There can only be a collision if two like named batch processes attempt to get the PID within the same 0.01 second time interval, in which case only one will succeed.
Any process that fails will repeatedly loop back and try again with a new lock file path until it succeeds.
The full path to the lock file is transformed into a unique ID that can be used in the WMIC query. WMIC is run within a FOR /F command, which means it is running in a child cmd.exe process. That is why the ParentProcessID of the cmd.exe process is retrieved.
Comment: I love looking for a CMD.EXE trick and coming up with an answer from dbenham. I know the code is going to be well-written, meticulous in its handling of edge-cases, and problems I didn't even think of will have been considered and solved. (I know, I know... I shouldn't be writing scripts in CMD.EXE in 2023 either... >sigh<  Old man yells at PowerShell, I guess. I have too many battle-hardened scripts from the last 27-ish years that I can't let go of.)
Answer:
Windows Task Manager, you will need to go to View -> Select Columns.. and select PID.
"tasklist /v" to get verbose task information in command prompt.
Process Explorer from live.sysinternals.com.
Comment: @Vivek, from a command prompt... and you are duping answers
Comment: i just gave all possible options.. and no duplicating.. i could have also mentioned PowerShell but i restrained myself as that does not comes by default in XP and 2003. If this is for Vista onwards OS, Yes PowerShell would have been my preference.
Comment: The question contains "... from the command prompt...", and your answers are "Windows Task Manager" (GUI, not command prompt) and "Process Explorer" (GUI, not command prompt). Did you even bother to read the question?
Answer: if you know there's only one cmd.exe running, you can get the PID this way:
for /F "tokens=1,2" %%i in ('tasklist /FI "IMAGENAME eq cmd.exe" /fo table /nh') do set pid=%%j

echo %pid%

Comment: That sounds like an improbable assumption, your code could be flaky
Comment: Raul, I understand what you're saying.  It's unsafe in many cases, but is safe in some -- such as a kiosk system or a VM where you're in total control of what processes are starting and not
Comment: Even if you know there's only one `cmd.exe` running the given code supposedly returns wrong value as the  `tasklist` runs in a child `cmd` instance invoked from `FOR` command. Proof: `for /F "tokens=*" %G in ('tasklist /FI "IMAGENAME eq cmd.exe" /fo table /nh') do @echo %G`. I'd guess that this child process is listed down to end so this could help: `set "pid="&for /F "tokens=1,2" %%i in ('tasklist /FI "IMAGENAME eq cmd.exe" /fo table /nh') do if not defined pid set pid=%%j`
Answer: This should do the trick:

tasklist /v
Answer: If you want to find the PID of imagename "notepad.exe", then the following code will work for you:
for /F "tokens=1,2" %i in ('tasklist') do (
 if "%i" equ "notepad.exe" (set x=%j)
)
echo %x%

Comment: there's no need to parse the process name yourself like that when `tasklist` already has the same capability
Answer: If you've got the Windows 2003 Resource kit push it through qgrep to get just the line you want. You could then extract the pid from here (this assumes you've only got one cmd running at a time),
tasklist /v | qgrep cmd

cmd.exe 2040 RDP-Tcp#447 0 1,804 K Running MACHINE\Administrator  0:00:00 Command Prompt

Answer: Take a look at this little batch trick.  It sets the title of cmd to a special value then uses tasklist to find it.  Inventive
\\Greg
Answer: THIS IS SHORT WAY TO GET PROCESS ID FOR OPEN CMD
tasklist /v /fi "imagename EQ cmd.exe" /FO LIST | FIND "PID:"

Answer: you can try with getcmdPid.bat - this is a selfcompiled .net application embedded in a bat script and returns the PID as the errorlevel:
call getcmdPid
echo %errorlevel%

Comment: This works well for me but it leaves an exe file after retrieving the PID, and it makes me feel a bit of 'virusly', if you know what I mean. Still, a nice trick!
Answer: This answer will give you ONLY the process ID, and none of the extra stuff the top answer includes.
title mycmd
tasklist /v /fo csv | findstr /i "mycmd" > PIDinfo.txt

set /p PIDinfo=<PIDinfo.txt
set PID1=%PIDinfo:~11,5%
set PID2=%PIDinfo:~11,4%

if %PID2% gtr 8100 (
    set PID=%PID2%
) else (
    set PID=%PID1%
)

echo %PID%

Explanation:
-there will not be a PID for cmd.exe that is greater than 18100 so check if PID2 is greater than 8100 so we know if it's a 4 digit or 5 digit number
case 1: a 5 digit PID like 17504 has a PID1 val 17504 and a PID2 val of 1750, so we use PID1
case 2: a 4 digit PID like 8205 has a PID1 val of 8205" and a PID2 val of 8205, so we use PID2
case 3: a 4 digit PID like 4352 has a PID1 val of 4352" and a PID2 val of 4352, so we use PID2
Answer: To get the PID for the current user, see my example below for restarting the explorer process:
@echo off
for /F "tokens=1,2" %%i in ('tasklist /FI "IMAGENAME eq explorer.exe" /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FO table /NH') do taskkill /F /PID %%j
start explorer.exe

Answer: From the above I used this to get current process id in a batch file:
for /f %a in ('powershell "$proc=Get-WmiObject Win32_Process -Filter ProcessId=$PID; $proc.ParentProcessId"') do set PID=%a
Comment: This gives me a different PID every time I run it and it's not 'this' `cmd`'s... The `for` uses an extra `cmd` it seems.
Answer: I found the easiest solution in a comment by Nicolas Melay:
for /F %%a in ('PowerShell -ex bypass "(gwmi Win32_Process -f ProcessID=$((gwmi Win32_Process -f ProcessId=$PID).ParentProcessID)).ParentProcessID"') do set my_pid=%%a

It uses PowerShell to get the PID of the grandparent process, as the for introduces an extra cmd.exe.


Interpolation not working properly inside mat-form-field for mat-error

Question: I am validating username with custom validation in angular material
Following is my code
<code> <mat-form-field>
          <input id="Username" required name="Username" [(ngModel)]="employee.Username" type="text" matInput
            #Username="ngModel" (focusout)="ValidateUsername($event.target.value)" placeholder="Username">
            <mat-error *ngIf="isNotValidUsername">Username already exist</mat-error>
          <mat-error *ngIf="Username.errors?.required && Username.touched">Required</mat-error>
        </mat-form-field>
</code>
Problem is "Username already exist" error not showing.
If i write this error line code outside mat-form-field then it worked. And also if Required error fired then it also show "Username already exist" error. But it alone inside mat-form-field tag not working. What i am doing wrong? How can i show it?
Comment: Pls provide your validation code!
Comment: ValidateUsername(username) {
    this.employeeService.ValidateUsername(username).subscribe((res:boolean) => {
      this.isNotValidUsername = res;
    })
  }
Comment: When you debug your code will the *ValidateUsername* function get called?
Comment: yes it called and also saved its response in "this.isNotValidUsername" variable. When i placed " " tag outside mat-form-field tag then it shows error. Otherwise its not visible.
Answer: mat-error won't show errors if the control is valid. You can use the error state matcher to set an invalid state to the input. The following post includes how it is implemented in a password validation form group.
https://stackblitz.com/angular/brleoyyorkap?file=app%2Finput-error-state-matcher-example.ts


Prove $\hom_R(R/I,N)\cong \{n\in N\mid\forall a\in I, an=0\}$

Question:
Let $R$ a ring, $I$ a left ideal and $N$ an $R$-module. Prove
  $$\hom_R(R/I,N)\cong \{n\in N \mid \forall a\in I, an=0\}$$
  using the fact that there is an exact sequence
  $$
0\to \hom_R(R/I,N)\to\hom_R(R,N)\to\hom_R(I,N) \, .
$$

The only way I can think it could work is if the set $\{n\in N \mid \forall a\in I, an=0\}$ was a subgroup of $\hom_R(R,N)$ and it is exactly the image of the second homomorphism in the sequence.
Comment: It says the $R$-module homomorphism $\overline{\phi} : R/I \to N$ has to come from $\phi : R \to N, \phi(a) = a \bar{\phi}(1_{R/I})$ and $I \in \ker(\phi)$
Comment: @reuns sorry but that doesn't explain anything to me
Comment: It is the injection $hom_R(R/I,N) \to \{ \phi \in hom_R(R,N), I \in \ker(\phi)\} $ $\simeq \{  \phi \in hom_R(R,N), \phi(1_R) = n \in N, \phi(I) = nI = 0\}$
Comment: Ok, I get it now, thanks
Answer: Hint:
Consider the short exact sequence:
$$0\longrightarrow I \overset{i}\longrightarrow R\overset{p}\longrightarrow R/I\longrightarrow 0$$
where  $i$ is the canonical injection of $I$  into $R$, and $p$ the canonical surjection from $R$ onto $R/I$.
Transform it by the left exact functor $\;\DeclareMathOperator{\Hom}{Hom}\Hom(-,N)$ and remember the isomorphism:
\begin{align}\Hom(R,N)&\overset{\sim}\longrightarrow N\\\varphi &\longmapsto \varphi(1)\end{align}
Comment: Functors are out of my league, so to speak
Comment: It's not a problem of category  theory. I guess you've seen what happens to a short exact sequence with Hom and tensor product?
Comment: Not at all. This question is just after a very short section on complexes and homology going over the definitions, with just some basic knowledge of modules. Regardless, the comment by reuns made me realise the answer.
Comment: Well, you get a left exact sequence $0\longrightarrow \operatorname{Hom}(R/I,N)\longrightarrow \operatorname{Hom}(R,N)\longrightarrow \operatorname{Hom}(I,N)$. Just interpret what it means concretely.
Comment: The only thing I can discern that it tells me is that the second homomorphism is injective and hence if the set in question is the image of the homomorphism then it must be an isomorphism
Comment: It also says the image of the second homomorphism is the kernel of the third.
Comment: I am doing a similar exercise and thank you for your answer! It's very elegant. @Bernard