Does shifting vary on a fretted instrument?

Question: As a beginning cellist, it struck me that non-fretted instruments seem to have a much greater focus on shifting mechanics than fretted.  With fretted instruments, it seems to my novice eye that shifting may not even be discussed much until the person becomes advanced, if then.  One is simply expected to move their hand from point A to point B and there's not much discussion of the technique to doing so.
On a non-fretted instrument however, we place a lot of emphasis on the technique of the shift.  We prepare for a shift, we cast (or throw) a shift, we take great pains to ensure that our fingers always remain in contact with the string and that they're just barely touching.  Etc, etc.
Is my perception incorrect?  Does a beginning guitar or electric bass instructor spend a great deal of time working with their student on shifts?  If not, any opinions on why it appears easier on fretted instruments?
Answer: On a fretted instrument shifting is much easier for a beginner as they don't need to be that accurate- as long as they are close behind the fret the note will be in tune.
Also, on a guitar you wouldn't want your fingers touching the string (except behind the fret) unless you were damping the note.
Having said that, a more experienced player will spend lots of time practicing moves and shifts as it does make a difference to tone and clarity, it's just not a core focus in the same way.
Comment: I would even say that the lack of emphasis on such shifting, and the find-as-you go attitude probably contributes a large part to that thing we call style.
Answer: There are two reasons why we need to be so careful about shifting technique: the first applies to all fretless instruments, the second is quite specific to cello or at least to strings.
First. This is quite obvious: after shifting, we need not just be somewhere in range of the correct frets, but exactly at the right position. There is no way to find this position just by eye (which is quite sufficient at the guitar) without ruining the first notes in the new position with ugly trial-and-error pitch corrections. Instead, we need to "burn" the paths needed to go into our brain cells, and that's only possible by having a very well-defined technique so that the shifts are actually reproducable.
This aspect is most important on higher-register instruments. But even on fretless electric bass, it's quite necessary to be capable of doing the shifts "by heart", though you have position marks or even fret-lines.
Second. Shifts are a very important tool in particular on the cello. Not only do we simply need to change positions unusually often (due to the long scale combined with the fifths-tuning, and the large used tonal range), they also provide a very expressive effect. That is when we don't simply stop one motif that was played in, say, the first position, pause, shift to fourth pos., and start another motif there, but play a continuous melody line through multiple positions, especially if it's legato. This seems, if you think about it, a physically impossible task: you need to be at one position, play a note that's sustained until the next note starts. But this note is in another position, for which to go to you have had zero time!
To still be able to play legato over shifts, these need to be an integral part of the melody rather than a seperate process. A badly played shift will sound horrible in this context, but one with well-developed technique can not only fit subtly into any melody but does, when varied accordingly to retain a just-right amount of tonal imperfectness, also provide an essential contribution to what makes the cello such an incredibly expressive instrument.
Answer: Another factor is that the guitar is tuned in fourths, while the cello is tuned in fifths, and the guitar has six strings, while the cello has four. Shifts are a bit difference because of the different distances that need to be covered to go horizontally across the strings to reach different intervals. The other factor is that on the guitar (depending on the design) the neck joins the body at the 12th, 14th, 16th position, or even higher with some electric guitars. So guitarists are not concerned with playing in left-hand "thumb position" because they can keep their thumb around the back of the neck for higher notes. The cello fingerboard is suspended high above the top of the body, at a sharp angle, whereas on the guitar, the upper range of the fingerboard is in solid contact with the top of the body and very close to it, with almost zero angle. Because of the design of acoustic and classical guitars, guitarists rarely ever play the highest notes on the fingerboard because they are inconvenient to reach with conventional technique.
In short, the differences are because the two instruments are tuned differently, strung differently, and designed and built differently.
Answer: Extending on leftaroundabout's second point, the more prepared you are to make a shift the better shift you will make and thus you will more often avoid the pitfalls mentioned.  Being prepared mentally for the shift (through practice, knowledge of the piece you're playing, etc) allows you to set your hand and arm up to actually perform the shift.  While maintaining everything about the motif you're currently playing you must:

Know the target position on the neck of the instrument
Know the hand position you need to achieve from the shift
Put your hand and fingers in a position that will bridge your current position to that of the target position
Prepare your arm to get your hand up or down the neck

Details of the mechanics that support that process vary between instruments but general mechanics apply to both instruments (and strings in general) and are vital to successful shift:

Correct finger position
Proper angle(s) of your arm
The way you are holding your instrument
Balance

In addition to physical body mechanics you also need to consider proper maintenance of your instrument, especially in terms of action height.  That is, the height of your strings above the fingerboard / fretboard.  The higher the strings the harder it is to press down and make a solid tone, and thus the harder it is to make a successful shift.  Body tension in the instrument will greatly affect action height.  Many guitars have a tension rod in the neck that will help you to (carefully!) adjust the instrument's body tension.  Bridge height is something you can adjust on a cello.  Proper humidity control will help maintain a more consistent body tension.
Being off in any of the above lessens your chance for achieving the perfect shift.
Fretless instruments are certainly a bit more forgiving than others in terms of intonation.  Given the lesser expense and greater popularity of guitars I think someone is much more likely to pick up a guitar on their own than they are a cello or other classical stringed instrument.  As such they will not be as likely to enlist a teacher until they are more advanced, and unfortunately more founded in improper technique.  A quality string teacher will teach shift mechanics from the beginning, regardless of the instrument in question.  It may be disguised as one of the items in my second bullet list, but all of them together contribute to proper shift technique.


If $X$ is not countably compact, then there exists a countable subset without accumulation points

Question: I want to prove that if $X$ is not countably compact, then there exists a countable subset $\{x_n:n\in\mathbb{N}\}$ and has no accumulation points.
If $X$ is not CC, then there exists an open cover of $X$, $\{A_i:i\in\mathbb{N}\}$ without a finite subcover of $X$. If we choose $x_i\in A_i$, can we say that $\{x_i:i\in\mathbb{N}\}$ has no accumulation points?
Thanks.
Comment: It's not _so_ easy. You need to choose your $x_i$ appropriately, arbitrary choice of $x_i$ may produce a set with accumulation points.
Answer: You should start by choosing a point $x_1\in A_1$. Then choose a point $x_2\in A_{m_2}-A_1$ where $m_2$ is the minimal number so that $A_{m_2}\not\subseteq A_1$ (This exists because there is no finite subcover). By induction we can choose a sequence $x_1,x_2,...$ such that $x_n\in A_{m_n}-\bigcup_{i<m_n} A_{i}$. Assume that this sequence has a cluster point $c$. Can you get a contradiction by using that fact that $(A_i)_i$ is an open cover and every neighborhood of $c$ contains infinitely many $x_j$ ?


How to show that the conditional distribution of p|X is a beta distribution if X|p is binomial and the prior for p is uniformly distributed

Question: Firstly, I apologize if this question is trivial and if my use of notation/terminology is not on point. I am not a statistician/mathematician but have been taken a statistics course to be able to carry out a project. In the course, we define a lot of different distributions, but there is no rationale for why a distribution is expressed the way it is so I find it difficult to understand. Currently, I am trying to understand the derivation of the Beta distribution as a distribution modeling the unknown probability in data following (drawn from?) binomial distribution.
I attempted to show myself that if X~Binomial(p,n), and p~U(0,1), then P(p|X)~Beta but turns out I don't have the skill set to do this derivation. I am familiar with the Bayes rule. In this case, I have the likelihood for the data and the prior for the probabilities (since I don't know anything about it, except that it's a probability and therefore needs to be between 0 and 1, Im assuming a uniform distribution). Then, following Bayes rule, what I would do to get to the posterior for the probability is to multiply the likelihood with the prior and divide it by the marginalization of this multiplication.
So I started like this:
The likelihood:
$$f_{(X|p)}(X) = {n \choose x} p^{x} (1-p) ^{(n-x)}$$
The prior:
$$f_{P}(p) = \frac{1}{(max - min)} = \frac{1}{1-0} = 1$$
Likelihood * prior (the joint distribution):
$$f_{XP}(x,p)= f_{X|P=p}(x) f_{P}(p) $$
$$f_{XP}(x,p)= {n \choose x} p^{x} (1-p) ^{(n-x)} \times 1 $$
$$f_{XP}(x,p)= {n \choose x} p^{x} (1-p) ^{(n-x)}$$
marginalization of the joint distribution:
$$ \int {n \choose x} p^{x} (1-p) ^{(n-x)} \,dx $$
Plugging into Bayes rule to get the posterior for p:
$$f_{P|X}(p)=  \frac{{n \choose x} p^{x} (1-p) ^{(n-x)}}{\int {n \choose x} p^{x} (1-p) ^{(n-x)} \,dx} $$
How do I get to Beta distribution from here? I don't see how this yields the Beta distribution. If this is not the way to go, could someone help me figure out how to derive Beta in the context of Binomial distribution and help me prove to myself that the posterior of p would indeed be Beta distributed if X is binomially distributed and I assume a uniform prior for p?
I have found plenty of questions here asking about the relationship between binomial and beta distributions and I get the relationship. Binomial distribution models the number of "successes" while the Beta distribution models a probability distribution over the success probability.However, I feel uncomfortable just accepting the expression + definition of Beta and moving on without proving this relationship to myself and the previous questions I found here have not answered this specific question. So, while there seem to be a lot of questions here about Beta, I do not think this is a duplicate.
Thank you for your help in advance!
Comment: Perhaps the key to completing this is to recognize that the uniform distribution is a beta distribution and to use Bayes' Theorem. See my Answer for an outline.
Comment: Thomas Bayes famously illustrated this in 1764 with a description of rolling balls on a pool table: search for it!
Answer: On the parameter $p$ you have the prior distribution
$\mathsf{Unif}(0,1) \equiv \mathsf{Beta}(\alpha_0=1, \beta_0=1)$
with density function
$$f_p(p) = p^0(1-p)^0 = p^{\alpha_0-1}(1-p)^{\beta_0-1},$$
for $0 < p < 1,$ where the symbol $\propto$ (pronounced 'proportional to')
indicates that we have omitted the norming constant factor that makes
$f_p(p)$ integrate to $1$ over $(0,1).$
Based on $n$ Bernoulli trials with $x$ Successes among them, we have
the likelihood function
$$f_{x|p}(x|p) \propto p^x(1-p)^{n-x}.$$
Then by Bayes' Theorem that POSTERIOR $\propto$ PRIOR $\times$ LIKELIHOOD.
we have the posterior distribution with density function
$$f_{p|x}(p|x) \propto p^{\alpha_0-1}(1-p)^{\beta_0-1} \times p^x(1-p)^{n-x}\\
\propto p^{\alpha_0+x-1}(1-p)^{\beta_0+n-x-1}\\
\propto p^{\alpha_n -1}(1-p)^{\beta_n - 1},$$
where $\alpha_n = \alpha_0 + x = 1+x$ and $\beta_n = \beta_0+n-x = 1+n-x.$
Because the beta prior and the binomial likelihood are conjugate (mathematically compatible), we can recognize the final term of the
relationship above as the kernel (density without norming constant) of
the distribution $\mathsf{Beta}(\alpha_n, \beta_n).$
There is only one possible norming constant for this posterior beta
kernel, so we can be sure we have the correct posterior distribution.
If you want to supply the norming constants (with $Gamma$ functions and binomial coefficients) throughout, then you can express factorials as $Gamma$ functions to
find that the integral in the denominator of the final displayed
relationship of your Question simplifies to the norming constant of
$\mathsf{Beta}(\alpha_n, \beta_n).$
Notes: (1) In Bayesian statistics one of the attractions of using
a conjugate prior and likelihood is that conjugacy often makes
the posterior distribution obvious.
(2) Even in frequentist arguments, likelihoods are often defined only
'up to a constant multiple'. The norming constant is irrelevant
if the task is to maximize the likelihood.


Prepping to conduct an incomplete score

Question: When I studied conducting at the conservatory, we always had, well, scores to work with.  I got very good at reading, understanding, and working with full scores for many sorts of ensembles.  And when I work with a band, orchestra, or chorus, these skills still serve me quite well.
The ways that I have historically prepped a score primarily involves taking the time to go through each part, and mentally re-building them on top of each other until I can hear every part clearly, and then I make my nuanced decisions and my rough plan of attack for the rehearsals themselves.  By the time I arrive at my first rehearsal, I am prepped and ready to go.
However, lately, I've been getting gigs in the realm of musical theater, and the so-called "conductor's scores" are really just piano reductions with cues indicated (ie. w.w. for "wood-winds", etc).  While I can get the tune, the rhythms, and the harmonic motion from this information, I can't construct the sort of mental model that I usually depend on, and my rehearsals have involved a fair amount of nonsense like "wait, you have that written in your part? Well, then what do you have in this other measure?"
This is not a good way to run a rehearsal, but I am missing a lot of data. However, I know that there are many MDs who work with these kinds of scores for a living.  Do any conductors out there have tips for how to prep for an efficient rehearsal with such a score?  There are so few details, it is hard for me to know where to begin.
Comment: Good question, but I'm afraid you may be overthinking this. Musical theater is not expected to have the same amount of expressional depth and detail-awareness as symphonic works. Those scores seem to be tailored more towards _quickly getting the general shape right_, even with little rehearsal time. It's honourable that you try to take it a step further, but practically speaking it's probably futile; your preparation time may be better invested with focusing on how the music is supposed to _support the actors_. — This assumes of course that _each player plays their part right_ by themselves...
Answer: Conduct the music, let the players tag along.  And conduct it in the same tempo as you did at the piano rehearsals. And if you changed anything at this stage, make sure the changes are marked in everyone's part BEFORE the orchestra rehearsal.   You don't get to be expressive, you get to do it exactly the way you trained the singers to do it!
Answer: In Musical Theatre, the orchestra isn't really meant to be rehearsed in that kind of detail. In high-level professional situations (Broadway, and also the top tier of regional theaters), they don't even have orchestra rehearsals, they just call the musicians in at sitzprobe at the eariest. Maybe there's one orchestra-only rehearsal before that, if the music is insanely complicated. Because of this, they only hire amazing musicians who don't need much rehearsal to get up to speed.
Most shows are also piano-conducted, and that solves a lot of the problem. The MD has been accompanying all of the cast rehearsals, so adding an orchestra is just a matter of adding more people playing along, rather than a shift from rehearsal piano to live orchestra. If a woodwind player gets lost and misses his entrance, the MD can simply add the line himself, and fix the confusing cue later.
If you're baton-conducting with a piano-conductor's score, then yeah, you're going to have the issues you described, for most shows. I recommend syncing up with your rhythm section (maybe even just Keys 1, it depends on the show) and primarily conducting to them. Everyone has to follow them anyway. You really shouldn't have to worry about cueing every little entrance. Trust your musicians to be competent.
And sometimes there will be discrepancies between the score and the parts. What happened was that the score got compiled at one point, and was never updated to reflect subsequent changes to the orchestration. Another reason to not sweat small things and focus only on large structural direction with your rhythm section.

Prepping to conduct an incomplete score


How to fix: Unrecognized command error in Linux

Question: Hi everyone!
I wrote a script that worked in the past, but suddenly it does not work and I have no idea why.
Here is my script:
#!/bin/bash
#This script gets damage seq bed files of 4 nucleotides and filters only those with dipyrimidins in 2-3 position.
_pwd=$PWD
#hg-19 ref genome.
genome_path="/hg_19_ref_genome.fa"
#Scans all the damage seq given bed files.
for bedFile in $_pwd/*.bed
do
  #Removes duplicates lines.
  sort -u -o $bedFile  $bedFile
  #Sorts the files according to chromosome and coordinates.
  sort -k1,1 -k2,2n -o $bedFile  $bedFile
  name=`basename $bedFile`
  #Adjusts the fasta file name.
  faName="${name%.bed*}.fa"
  bedtools getfasta -s -fi $genome_path -bed $bedFile -name -fo $_pwd/fasta_files/$faName #Gets the sequence itself.
done

#Scans all the obtained fasta files.
for faFile in $_pwd/fasta_files/*.fa
do
  name=`basename $faFile`
  faOutput="${name}"
  bedOutput="${name%.fa*}.bed"
  #Filters the files for getting only those which contain dypirimidne in 2-3 columns.
  cat $faFile  |\
  paste - -  |\
  awk 'toupper(substr($2,2,2)) ~ /^(TT|TC|CT|CC)$/' |\
  tr "\t" "\n" |\
  tee $_pwd/filtered_fasta/$faOutput |\
  awk -F '[>():-]' '/^>/ {printf("%s\t%s\t%s\t%s\t%s\t%s\n",$4,$5,$6,$2,$2,$7);}' > $_pwd/filtered_bed/$bedOutput
done

When I run it, I get this error:
line 30:  : command not found

where line 30 is this line:
tee $_pwd/filtered_fasta/$faOutput |\

I have no idea why this error occurs, it worked fine before.
I really hope someone can help me with that issue.
UPDATE: I deleted all the '' and it works fine now.
Comment: FWIW, the backslashes after the pipe characters are not necessary. As long as the pipe character is the last character on a line, the shell knows to use the next line as the next pipe segment. But .. try running your script through http://shellcheck.net. You'll get tips for many improvements which may make debugging easier.
Comment: The error is consistent with that line ending with a carriage return (`\r`).
Comment: Also see [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc.
Answer: If I should believe that you've pasted your script correctly, there is a character in line 30 after the backslash \.
The Bash itself is telling you name of the unknown command:
line 30:  : command not found

The thing between : is name of command that Bash was unable to recognize and this thing is  - a white space.
As suggested in comments, remove the backslashes \ after pipe | and make sure that the pipe character is the last one in line. This will improve readability.
Also, good editor can show white spaces (example for Sublime Text). Enabling it will show you such mistakes.
Comment: Good catch. I didn't see that. :-)


How do you determine the commands a Windows App will accept?

Question: As an example the OneNote Windows App will accept.
onenote-cmd://
This will activate the app if it's open, or open it if it is closed.
It also accepts
onenote-cmd://quicknote/?onOpen=typing
This creates a new note.
How do I determine the other commands, if any, that OneNote (or any other Windows App)?
Comment: You ask the people who wrote the program what commands they support.
Comment: It's considered polite to mark the correct answer.
Comment: Thanks Jerry.  Was just waiting to see if any other answers came along.
Know anyone on the OneNote team I could reach out to? :-P
Answer: There is no way to "probe" into an app and know which commands will work.
For each application, you will have to look at the documentation.
There may also be even more additional undocumented commands. But, by the nature of being undocumented, someone would have to tell you about their existance in order to find out.


Setting a field readonly rather than invisible from hook_field_access()?

Question: I have a field that should not be visible when editing, in certain cases (depending on node state and user).
It works fine using hook_field_access():
function mymodule_field_access($op, $field, $entity_type, $entity, $account) {
  if ( ...conditions on $entity_type, $entity->type, $field['field_name'], $op...) {
    // → $op=='edit' && isset($entity->nid) so only when modifying existing content
    return FALSE;
  }
  return TRUE;
}

As said it works fine, and the field is not displayed at all when access is not granted.
But in some particular conditions I would like to let the field visible but readonly (grey-ed, or anything preventing to modify it) rather than hiding it.
I tried to return TRUE and before to set according attributes to the field, but until now I failed to make it works as expected. What I tested:
$tmp = mode_load($entity->nid);
$tmp->my_field['#attributes'] = array('readonly' => 'readonly'); // also tried to use 'disabled')
node_save($tmp);

I also tried:
$tmp->my_field['#disabled'] = true;

Thanks for any help.
Regards.
Comment: You need to alter this field at display or on edit form ?
Comment: On edit. My conditions to hide the field is for preventing some users in some conditions to edit this field. But in some other cases I would like them to be able to see the field value when editing, but not to modify it.
Answer: Try something like that :
Replace FORM_ID with the real form ID.
function MYMODULE_form_FORM_ID_alter(&$form, &$form_state) {

  if(!user_access(ADMIN_USER)){
      $form['your_field_name']['#disabled'] = TRUE;
    }

    $form['your_field_name']['#access'] = TRUE;
}

Comment: Thank you. This example does (part of) the trick. But in my particular case I allow the field with more complex criteria. For example the field is writable at creation (`($op=='edit') && isset($entity->nid)` to restrict only on existing content). Is it possible from `form-alter` to get similar informations than from `field_access`?
Comment: Hmm… Yes I can: `$node = $form_state['node']; if (!isset($node->nid) or isset($node->is_new)) { ...this is a node creation... }`.
Comment: Maybe you can clarify for me: is it whatever possible to alter a field from `field_access()`?
Comment: If you want to alter the form on node/add/article use : `function MYMODULE_node_article_form_alter(&$form, &$form_state)` and if you want to alter the form on node/{id}/edit use : `function MYMODULE_node_article_edit_form_alter(&$form, &$form_state)`
Comment: `field_access` can't alter your field, return TRUE if the operation is allowed, and FALSE if the operation is denied.


MySql clips TIME values

Question: Background
We have a column of TIME data type to indicate elapsed time.
Issue
When we try to insert a number of hours greater than 24, for example '25:00:00', MySQL clips this value down to '01:00:00', which is not what we want.
Solution Attempted
The documentation suggests that we can use 'HHH:MM:SS' instead of 'HH:MM:SS' format (https://dev.mysql.com/doc/refman/5.0/en/time.htm). That would probably solve our problem, but the documentation doesn't say how to do this.
Comment: I've never tried to do that, but my first guess would be maybe it has to do with "strict mode" preventing "invalid" dates.
Comment: Even HHH will exhibit the same behaviour ~840 hours; Why not store [int seconds]
Comment: @AlexK.The 840-hour limit should be OK.
Answer: Use INT UNSIGNED to measure duration instead. For example, represent seconds as INT UNSIGNED if that is the "grain" for the time dimension.
Reasons:

INT calculations are way simpler!
My impression is that TIME is meant to indicate time of day, not duration.
JDBC can't understand TIME values larger than 24 hours.
Getting TIME to format as HHH instead of HH is undocumented and we couldn't figure it out.

MySql clips TIME values


Splitting list in two new lists python

Question: in Python I have a list with 96 entries, some are positiv, some are negative. I now want to new lists with 96 entries each. The first one should include all positiv values and instead of the negative values it should be 0 at this place in the list. The same the other way round for the second one. I think I have to use list-comprehension but don´t know how...
Thanks for help!
Comment: hint: you **never** *have* to use a comprehension
Comment: A list comprehension can only create one new list. Use a `for` loop and then `append` each element to the appropriate result list.
Comment: and how I will get 0 instead of the negativ value in this list...so i don´t want to short the list....the length of each list should be equal to the orginal one
Comment: Please share with us what you've tried and where do you get stuck?
Answer: Here's one way to do it, using list comprehensions and ... if ... else ...:
lst = range(-5, 5)

pos = [x if x > 0 else 0 for x in lst]
neg = [x if x < 0 else 0 for x in lst]

print(pos) # [0, 0, 0, 0, 0, 0, 1, 2, 3, 4]
print(neg) # [-5, -4, -3, -2, -1, 0, 0, 0, 0, 0]

Alternatively, using for loop:
pos, neg = [], []
for x in lst:
    if x > 0:
        pos.append(x)
        neg.append(0)
    else:
        neg.append(x)
        pos.append(0)

Or, shorter:
pos, neg = [], []
for x in lst:
    pos.append(max(x, 0))
    neg.append(min(x, 0))

Splitting list in two new lists python


Component does not remount when route parameters change

Question: I'm working on a react application using react-router. I have a project page that has a url as follows:
myapplication.com/project/unique-project-id

When the project component loads, I trigger a data request for that project from the componentDidMount event. I'm now running into an issue where if I switch directly between two projects so only the id changes like this...
myapplication.com/project/982378632
myapplication.com/project/782387223
myapplication.com/project/198731289

componentDidMount is not triggered again so the data is not refreshed. Is there another lifecycle event I should be using to trigger my data request or a different strategy to tackle this issue?
Comment: Could you post your components code ?
Comment: Good article might relates to question https://www.nikgraf.com/blog/using-reacts-key-attribute-to-remount-a-component
Answer: If you do need a component remount when route changes, you can pass a unique key to your component's key attribute (the key is associated with your path/route). So every time the route changes, the key will also change which triggers React component to unmount/remount. I got the idea from this answer
Comment: @AlpitAnand this is a broad question. Re-mounting is definitely slower than re-rendering as it triggers more lifecycle methods. Here I'm just answering how to make remount happen when route changes.
Comment: @AlpitAnand Your question is too broad and application specific. For some applications, yes, it would be a performance hit, in which case you should watch props changing yourself and update only the elements that should update. For most though, the above is a good solution
Comment: Just asking, won't this be affecting the performance of the app?
Comment: But its not a huge impact ? Whats your advice can we use it safely without much effect ?
Comment: this does not work, produce render loop infinite with erros, maybe for very light componentes works, it's not my case
Answer: Here is my answer, similar to some of the above but with code.
<Route path="/page/:pageid" render={(props) => (
  <Page key={props.match.params.pageid} {...props} />)
} />

Comment: The key here plays the biggest role ,because image that you have Link to in a profile page ,upon click it just redirect to the same route ,but with different parameters ,BUT the component doesnt update ,because React can't find a difference ,because there must be a KEY to compare the old result
Comment: This is a graet answer - has saved me from componentDidUpdate hell ;)
Comment: The best answer, because update the comp only when changes :pageid param.
Answer: If the link is directing to the same route with just a different param, it's not remounting, but instead receiving new props. So, you could use the componentWillReceiveProps(newProps) function and look for newProps.params.projectId.
If you're trying to load data, I would recommend fetching the data on before the router handles the match using static methods on the component. Check out this example. React Router Mega Demo. That way, the component would load the data and automatically update when the route params change without needing to rely on componentWillReceiveProps.
Comment: FYI: "componentWillReceiveProps" considered legacy
Comment: You should use ComponentDidUpdate with React 16 since componentWillReceiveProps is deprecated
Comment: Thank you. I was able to get this working using componentWillReceiveProps. I still don't really understand the React Router Mega Demo data flow. I see the static fetchData defined on some components. How are these statics getting called from the router client side?
Comment: Great question. Check out the client.js file and the server.js file. During the router's runcycle they call the fetchData.js file and pass the `state` to it. It's a little confusing at first but then becomes a little clearer.
Comment: This works but has the problem that you need to add componentWillReceiveProps() or componentDidUpdate() for handling re-renders to every component that is loading data. For example think about a page that has several components that are loading data based on same path parameter.
Comment: Coming to this some months later, and being very inexperienced, I am having difficulty updating the examples with the changes to react-router with release of 1.0 - is it still possible now that Router.run has been replaced with render({routes}}, el) - can we still do the fetching before the router handles the match?
Comment: Yes, you would do it inside the match. I made an example once here: https://github.com/bradbumbalough/react-0.14-iso-test. Check out the `src/server.jsx` file.
Comment: @PatoLoco that's exactly what I needed - I didn't need to remount or push a new route after all, I just had to check the `:parameter` value in `ComponentDidUpdate`
Answer: You have to be clear, that a route change will not cause a page refresh, you have to handle it yourself.
import theThingsYouNeed from './whereYouFindThem'

export default class Project extends React.Component {

    componentWillMount() {

        this.state = {
            id: this.props.router.params.id
        }

        // fire action to update redux project store
        this.props.dispatch(fetchProject(this.props.router.params.id))
    }

    componentDidUpdate(prevProps, prevState) {
         /**
         * this is the initial render
         * without a previous prop change
         */
        if(prevProps == undefined) {
            return false
        }

        /**
         * new Project in town ?
         */
        if (this.state.id != this.props.router.params.id) {
           this.props.dispatch(fetchProject(this.props.router.params.id))
           this.setState({id: this.props.router.params.id})
        }

    }

    render() { <Project .../> }
}

Comment: Thanks, this solution works for me. But my eslint setting is complaining about this: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-did-update-set-state.md
What do you think about this?
Comment: Yeah that's actually not best practice, sorry about that, after dispatching the the "fetchProject" your reducer will update your props anyways, I just used this to make my point without putting redux into place
Comment: Hi chickenchilli,
I'm Actually still stuck in this... do you have other suggestions or recommended tutorials? Or I'll stick to your solution for now. Thanks for your help!
Answer: If you have:
<Route
   render={(props) => <Component {...props} />}
   path="/project/:projectId/"
/>

In React 16.8 and above, using hooks, you can do:
import React, { useEffect } from "react";
const Component = (props) => {
  useEffect(() => {
    props.fetchResource();
  }, [props.match.params.projectId]);

  return (
Layout
);
}
export default Component;

In there, you are only triggering a new fetchResource call whenever props.match.params.id changes.
Comment: This is the better answer given that most of the other answers rely on the now deprecated and insecure `componentWillReceiveProps`
Answer: Based on answers by @wei, @Breakpoint25 and @PaulusLimma I made this replacement component for the <Route>. This will remount the page when the URL changes, forcing all the components in the page to be created and mounted again, not just re-rendered. All componentDidMount() and all other startup hooks are executed also on the URL change.
The idea is to change components key property when the URL changes and this forces React to re-mount the component.
You can use it as a drop-in replacement for <Route>, for example like this:
<Router>
  <Switch>
    <RemountingRoute path="/item/:id" exact={true} component={ItemPage} />
    <RemountingRoute path="/stuff/:id" exact={true} component={StuffPage} />
  </Switch>
</Router>

The <RemountingRoute> component is defined like this:
export const RemountingRoute = (props) => {
  const {component, ...other} = props
  const Component = component
  return (
    <Route {...other} render={p => <Component key={p.location.pathname + p.location.search}
                                              history={p.history}
                                              location={p.location}
                                              match={p.match} />}
    />)
}

RemountingRoute.propsType = {
  component: PropTypes.object.isRequired
}

This has been tested with React-Router 4.3.
Answer: @wei's answer works great, but in some situations I find it better to not set a key of inner component, but route itself. Also, if path to component is static, but you just want component to remount each time user navigates to it (perhaps to make an api-call at componentDidMount()), it is handy to set location.pathname to key of route. With it route and all it's content gets remounted when location changes.
const MainContent = ({location}) => (
    <Switch>
        <Route exact path='/projects' component={Tasks} key={location.pathname}/>
        <Route exact path='/tasks' component={Projects} key={location.pathname}/>
    </Switch>
);

export default withRouter(MainContent)

Comment: Adding `key` directly to `` is more simple fix that also works for react with *Typescript* without additional codes (as other similar fixes requires some extra work on types).
Answer: This is how I solved the problem:
This method gets the individual item from the API:
loadConstruction( id ) {
    axios.get('/construction/' + id)
      .then( construction => {
        this.setState({ construction: construction.data })
      })
      .catch( error => {
        console.log('error: ', error);
      })
  }

I call this method from componentDidMount, this method will be called just once, when I load this route for the first time:
componentDidMount() {
    const id = this.props.match.params.id;
    this.loadConstruction( id )
  }

And from componentWillReceiveProps which will be called since the second time we load same route but different ID, and I call the first method to reload the state and then component will load the new item.
componentWillReceiveProps(nextProps) {
    if (nextProps.match.params.id !== this.props.match.params.id) {
      const id = nextProps.match.params.id
      this.loadConstruction( id );
    }
  }

Comment: This works but has the problem that you need to add componentWillReceiveProps() for handling re-renders to every component that is loading data.
Comment: Use componentDidUpdate(prevProps). componentWillUpdate()
componentWillReceiveProps() are deprecated.
Answer: React Router v6+
You need to specify a key. In earlier versions of React Router you could do the following:
<Route path="/project/:pid"
       render={(props) => (
           <Page key={props.match.params.pid} {...props} />)}
/>

Since render can't be used with <Route> anymore (starting from React Router v6), one of the simplest solutions is to create an auxiliary component:
const PageProxy = (props) =>
{
  const { pid } = useParams();
  return <Page key={pid} {...props} />;
}

And your <Route> becomes simply:
<Route path="/project/:pid" element={<PageProxy />} />

Comment: shouldn't it be } /> I can't find documentation on element prop anywhere
Answer: You can use the method provided:
useEffect(() => {
  // fetch something
}, [props.match.params.id])

when component is guaranteed to re-render after route change then you can pass
props as dependency
Not the best way but good enough to handle what you are looking for
according to Kent C Dodds:
Consider more what you want to have happened.
Answer: If you are using Class Component, you can use componentDidUpdate
componentDidMount() {
    const { projectId } = this.props.match.params
    this.GetProject(id); // Get the project when Component gets Mounted
 }

componentDidUpdate(prevProps, prevState) {
    const { projectId } = this.props.match.params

    if (prevState.projetct) { //Ensuring This is not the first call to the server
      if(projectId !== prevProps.match.params.projectId ) {
        this.GetProject(projectId); // Get the new Project when project id Change on Url
      }
    }
 }

Comment: Are you sure about that? the ComponentDidUpdate is not and is not going to be deprecated in coming version, can you attach some link? the functions to be deprecated as follows: componentWillMount — UNSAFE_componentWillMount
componentWillReceiveProps — UNSAFE_componentWillReceiveProps
componentWillUpdate — UNSAFE_componentWillUpdate
Answer: Here is a pretty simple solution: do a location check in componentDidUpdate, and have a getData function that has the data fetching part with setState:
componentDidUpdate (prevProps) {
    if (prevProps.location.key !== this.props.location.key) {
        this.getData();
    }
}

getData = () => {
    CallSomeAsyncronousServiceToFetchData
        .then(
            response => {
                this.setState({whatever: response.data})
            }
        )
}

Answer: I will try to give the updated answer.
So basically what you want is the Component to get remounted whenever we find a change in params.
Now react-router>6.0 has been released and this answer will be for React Router ^V6
So suppose I have a path
/user/<userId>
Here <userId> will be replaced by the actual userId
So the initialization of this path will be like this
<BrowserRouter>
    <Routes>
        <Route
           path="/user/:userid"
              element={<YourComponent/>}
         />
     </Routes>
</BrowserRouter>

Now in <YourComponent/> receiving end
import { useParams } from "react-router-dom";
import { useEffect } from "react";

export default function YourComponent(){
    const {userid} = useParams();

    useEffect(() => {
        // Some Logic Here
    },[userid])

   return (<Component/>)
}

So this useEffect() will mount itself everytime there is a change in the params (userid in this case)
Thus the outcome is whenever you hit the URL with same params (It is not equal to reloading the page. I mean to navigate to the same URL) the component will not remount itself but there will be remounting whenever there is a change in the params which I think solves your problem.
Answer: react-router is broken because it must remount components on any location change.
I managed to find a fix for this bug though:
https://github.com/ReactTraining/react-router/issues/1982#issuecomment-275346314
In short (see the link above for full info)
<Router createElement={ (component, props) =>
{
  const { location } = props
  const key = `${location.pathname}${location.search}`
  props = { ...props, key }
  return React.createElement(component, props)
} }/>

This will make it remount on any URL change


Haskell Replace a value in a list with another value

Question: I am pretty new to Haskell. I am trying to write a program that takes two values and a list and replaces every instance of the first value in the list with the second. E.g. repOcc 'n' 'i' "pink" would return "piik".
The following is my code:
repOcc :: t -> t -> [t] -> [t]
repOcc x y (z:zs) = if z == x
                      then z = y
                      subst x y zs
                      else subst x y zs

The error I am receiving at compile time is:
rev.hs:3 :32: error:
   parse error on input '='
   Perhaps you need a 'let' in a 'do' block?
   e.g. 'let x = 5' instead of 'x = 5'
Failed, modules loaded: none.

Comment: Please indicate how the code deviates from your expectation. Is there a compiler error? What is that error? Does the program give incorrect output? What is an example of an input and incorrect output?
Comment: A good first step into Haskell is forgetting that you ever heard of assignments.
Comment: Start simpler: can you write a function `f from to x` that evaluates to `to` if `x` is equal to `from` and `x` unchanged otherwise?
Comment: On compilation, I am getting a parse error and the compiler is suggesting me to add 'let' in the 'do block'
Comment: @RNee please edit your question to include the exact text of the parse error.
Comment: @RNee And also the code the error points to. There is no do block in the function above, for instance.
Comment: Note that single quotes delimit a character whereas double quotes delimit a list of characters. I made this small correction for sake of clarity to other beginners with a similar question.
Comment: Not specific to Haskell but in every functional language you have a `map` function for this task. Such as `map (\c -> if c == 'n' then 'i' else c) "pink"
` will give you `"piik"`
Answer: Your program looks rather "imperative" whereas Haskell aims to be more "declarative". So you can not set a variable z in a list: once a list is constructed, you can not alter it anymore. So you have to construct a new list where elements that are equal to x are set to y.
Next you make use of the (==) function. That function is defined in the Eq typeclass, so you need to add Eq t as a type constraint to the signature.
So now we can start to construct such function. Usually when working with a list, we make use of recursion. The base case of the recursion is usually the empty list. In case we encounter the empty list, we should return an empty list, regardless what x and y are. So we use underscores as "don't care" patterns, and use [] as the list pattern, and write:
repOcc _ _ [] = []

The recursive case is when the list contains a head h and a tail t in a (h:t) pattern. In that case we check whether h is equal to x. In case it is, we construct a list with y as head, otherwise h is still the head.
repOcc x y (h:t) | x == h = y : tl
                 | otherwise = h : tl

Now the question remains what the tail of the result list tl should be. Here we use recursion, so we call repOcc with x y t:
    where tl = repOcc x y t

Or putting it together:
repOcc :: Eq t => t -> t -> [t] -> [t]
repOcc _ _ [] = []
repOcc x y (h:t) | x == h = y : tl
                 | otherwise = h : tl
    where tl = repOcc x y t

We can write such recursive functions, but the above is actually a special case of a map function: we map every character in such way that we check whether it is equal to x and if it is, we return y, otherwise we return h. So we can rewrite the above as:
repOcc :: Eq t => t -> t -> [t] -> [t]
repOcc x y ls = map (\h -> if h == x then y else h) ls

We can further improve the code by making use of eta-reduction:
repOcc :: Eq t => t -> t -> [t] -> [t]
repOcc x y = map (\h -> if h == x then y else h)


Repository pattern for use in Reports only?

Question: I am creating an MVC application where I'm going to be creating quite a few RDLC based reports.  Looking at some of the examples out there on the web, it seems like the best way to use the report data source is through a repository.
My application will have to work with several databases. I already use the pattern to read/write to the database through POCO objects.
1)  Rather than extend my current repository classes which are in my domain project, I was going to add separate repository classes to my Web project that deals only with the reports requirements.
2) Would it make sense to use the Repository pattern to only return different IQueryable/IEnumerable result sets that I could then use within my reports?  Or would this be the wrong use of the REpository pattern?
I'd really like to hear your views on how you would go about with the above.
Thanks
Answer: It's better to use queries for reports because you need nothing but "read" operations there. Take a look at CQS(CQRS) pattern.
Comment: Hi Dima.  Thank you for your response. I will look up the CQS pattern as recommended


SyncAdapter - detect when to synchronize server with the client (mobile)

Question: I have a form inside my mobileApp to add a new item.
When there are no internet connection, and that an item is added to sqlite localdb, I want this data to be uploaded to the server (synced), once device is connected to the internet.
I have implemented a SyncAdapter, coupled with a simple ContentProvider, but I dont know how to detect that a row need to be created/updated/deleted on the server.
Do I need to add a flag column to the table (having values created/updated/deleted/none)
and if that's the case, is the uploading to the server done with just a call to server api methods or with ContentResolver.
My last question is where do I place this code? inside performSync() method? or I have to add a receiver which react when mobileapp is connected to internet.
Answer: The content provider does notify sync adapter when a modification occured on local db.


In Python, 0 == (1 or 0), returns False. Why doesn't it return True?

Question: I'm trying to see if a function returns an integer who's value should be either a 1 or 0.
0 == (1 or 0)
0 is equal to 1 or 0, this sounds like it should be true, but it's not.
Why? And how to do what I'm looking to do correctly?
Answer: 0 == (1 or 0) is parsed as this tree:
  ==
 /  \
0    or
    /  \
   1    0

1 or 0 results in 1, because or returns the first truthy value of the two operands (and 1 is truthy).
Afterwards, we're left with 0 == 1, which is obviously False.
What you want to do, is check whether 0 is one of those values which you can do through a sequence check: 0 in (0, 1)
Answer: 1 or 0 evaluates to 1, and since 0 is not equal to 1, the expression is false.
I suspect what you are trying to do is something like 0 == 0 or 1 == 0
Answer: You are using the brackets. Which means you are forcing precedence on it. so python will first evaluate the expression 1 or 0  which is 1.  and then it will evaluate the next part,  0 == 1   which is false.


Transfer 15TB of tiny files

Question: I'm archiving data from one server to another. Initially I started a rsync job. It took 2 weeks for it to build the file list just for 5 TB of data and another week to transfer 1 TB of data.
Then I had to kill the job as we need some down time on the new server.
It's been agreed that we will tar it up since we probably won't need to access it again. I was thinking of breaking it into 500 GB chunks. After I tar it then I was going to copy it across through ssh. I was using tar and pigz but it is still too slow.
Is there a better way to do it? I think both servers are on Redhat. Old server is Ext4 and the new one is XFS.
File sizes range from few kb to few mb and there are 24 million jpegs in 5TB. So I'm guessing around 60-80 million for 15TB.
edit: After playing with rsync, nc, tar, mbuffer and pigz for a couple of days. The bottleneck is going to be the disk IO. As the data is striped across 500 SAS disks and around 250 million jpegs. However, now I learnt about all these nice tools that I can use in future.
Comment: @TessellatingHeckler: so you suggest OP migrates from Redhat to Windows before archiving?
Comment: @ThomasWeller They asked "is there a better way?", and there is. I make no recommendation that they use the better way. They're free to use commands in a pipe which can't recover from interruption, won't verify the file content, can't report copy status, can't use previously copied blocks to avoid copying parts of files, has no implicit support low-priority copying, can't be paused, has no mention of copying ACLs, and needs someone to stay logged in to run it. Anyone else following along, however, might be interested - or prompted to say "x does that on Linux".
Comment: *Is there a better way to do it?* - Yeah, Windows Server 2012 R2 DFS replication [would prepare that in about 10 hours](http://blogs.technet.com/b/filecab/archive/2013/11/15/using-dfs-replication-clone-feature-to-prepare-100tb-of-data-in-3-days-a-test-perspective.aspx). And it would sync changes, and pick up where it left off after reboots.
Comment: One option is creating the compressed tar files on an external drive and moving that to the new system. The extra disk will speed up creating the tar files (won't be writing to existing disks in the system, possibly while trying to read 15TB from them) and doesn't tie up the new server.
Comment: possible duplicate of [linux to linux, 10TB transfer?](http://serverfault.com/questions/149045/linux-to-linux-10tb-transfer)
Comment: @TessellatingHeckler: That sounds a bit like BTRFS send/receive.  https://en.wikipedia.org/wiki/Btrfs#Send.2Freceive.  I think that can work as a dump/restore but with incremental capability.  Some other Linux filesystems also have dump/restore tools that read the data in disk order, not logical directory order (e.g. `xfsdump`).  The problem here is that the OP is going from ext4 to XFS, so this isn't an option.  (BTW, OP, I'd suggest evaluating BTRFS for use on your server.  XFS can handle being used as an object store for zillions of small files, but BTRFS may be better at it.)
Comment: @lbanz: ssh encryption, or rsync's gzip compression, might be bottlenecking you.  Discussion in comments on http://unix.stackexchange.com/a/228048/79808 has some numbers for compression.
Comment: If you're not going to access it again*, what if you simply removed the drive itself and stored it in an airtight container (Lock&Lock) together with a packet of desiccant and maybe a bit of bubble wrap or padding? If you needed to transfer it, use snail mail or other physical methods. It's usually faster than 17 weeks. I am assuming that the files are in a different drive than the OS.
Comment: @TessellatingHeckler The example (and the link) you reported clearly state that the preseed phase (read: file upload to the new server) is done by DFSR via robocopy. While robocopy **is** very useful, rsync is a better alternative from almost any point of view.
Comment: It's a little offtopic, but: @PeterCordes I'd be very careful recommending btrfs for production use, yet. Lately I had some data corruption issues related to btrfs and bcache on Ubuntu 14.04.
Comment: @TessellatingHeckler It is true that these are free commands and doesn't have any reporting status if there are corruptions. Now that you mention it, I think I'm going back to rsync. Because I know there might be corruption in our old system when the temperature threshold was breached.
Comment: @Fox: From what I've read, if you use BTRFS, it's a good idea to use the latest kernel.  They usually fix more bugs than they introduce, and it's still new and improving, so a years-old stable-distro kernel version of BTRFS is not ideal.
Comment: @PeterCordes that is why I recommend being careful. Myself being rather fan of bleeding edge, I quite understand why some people like long term support distros, which tend to stick to an older kernel. So sure, btrfs is maturing at a pretty good pace, but it's not an universal answer. Sure not without buts.
Comment: @Fox: I haven't used BTRFS myself, since XFS is good at what I mostly do.  Any comment on whether it's good for a workload like the OP's, where it's *all* small to medium-size files?  I know the XFS devs sometimes say on the mailing list that XFS isn't designed to be an object-store, and my impression was BTRFS was designed with that workload as a potential use-case.  (And in practice may handle it better than XFS.)  Reiserfs is a bad choice for a new FS these day, but it was explicitly designed for using the filesystem as a database.
Comment: Speaking of FS-as-object-store, I did some digging when this came up recently, since I was curious.  http://unix.stackexchange.com/a/222640/79808 has most of what I found.  Traditional-filesystem on RAID5 is a bad choice.  One object-store system I looked at did redundancy at an object level, and wanted a separate XFS filesystem on each disk.  The difference is subtle but huge.  Metadata ops improve, because each CPU can be searching a separate small free-inode map, instead of one giant one, for example.  Taking RAID5 out of the picture for small object writes is also huge.
Comment: Sounds like a great little use case for BitTorrent Sync to me. https://www.getsync.com/
Comment: @TessellatingHeckler LOL, the OP asked for a *better* way, not Windows... nobody genuinely *wants* Windows.
Comment: I'm so glad this is tagged [tag:Linux] and not [tag:windows]. I would probably die.
Comment: You might want to stick ice packs on the drives during the transfer as well, to help prevent heat degradation.
Comment: @lbanz so how much time it took ?
Comment: @RahulPatil small files is around 6mb/s and large files are at 150mb/s. I'm expecting 1-2 months to transfer 15TB of small files.
Answer: I have had very good results using tar, pigz (parallel gzip) and nc.
Source machine:
tar -cf - -C /path/of/small/files . | pigz | nc -l 9876

Destination machine:
To extract:
nc source_machine_ip 9876 | pigz -d | tar -xf - -C /put/stuff/here

To keep archive:
nc source_machine_ip 9876 > smallstuff.tar.gz

If you want to see the transfer rate just pipe through pv after pigz -d!
Comment: How can this be accepted if OP has already tried `tar` and `pigz`? I don't understand...
Comment: @ThomasWeller where did you get that he's tried `pigz`? From the question it looks like he's only tried `rsync` so far, and was *considering* using `tar` to split and bundle the data. Especially if he hasn't used the `-z`/`--compress` option on rsync, `pigz` could theoretically help significantly.
Comment: FYI, you can replace `pigz` with `gzip` or remove it altogether, but the speed will be significantly slower.
Comment: @ThomasWeller yes indeed I already tried tar and pigz but not nc. I was using ssh so it added a lot more overhead.
Comment: @lbanz that simply means that `tar` isn't producing data fast enough for `pigz` to use much CPU for compression. Reading lots of small files involves many more syscalls, many more disk seeks, and a lot more kernel overhead than reading the same number of bytes of larger files, and it looks like you're simply bottlenecking at a fundamental level.
Comment: For checking you could use _tee_ to divert a copy of the tar to _sha256sum_ (or other checksum/CRC tool) on both source and destination. And then compare the resulting checksum values.
Comment: http://intermediatesql.com/linux/scrap-the-scp-how-to-copy-data-fast-using-pigz-and-nc/
Using nc/pigz seems to score the highest on benchmark too. I was piping it through ssh so it was incredibly slow.
Comment: @h0tw1r3 Just to let you know that it is insanely fast. After pressing enter, and doing ls. It has already done 1GB. With rsync or piping it over ssh usually takes 20-30 mins just for 1GB. The bit I'm worried is how to verify the data once it has completed the transfer.
Comment: To verify the data run the compression step on both sides and compare the result on one side or the other. You'd need to make sure that the files are all in the archive in the same order which might not be possible. In that case you could (assuming enough space is available repeat the transfer in reverse to a different location and compare the result using a standard file compare utility. Or if space is short, transfer to a third (spacious) location from both source and target servers, and do the compare there.
Comment: I would propose using [`mbuffer`](http://www.maier-komor.de/mbuffer.html) instead of `nc`. The advantage is the ability to define a local buffer for the transfer. Plus, you have some additional stats. It is being widely used in [zfs dataset transfers](http://everycity.co.uk/alasdair/2010/07/using-mbuffer-to-speed-up-slow-zfs-send-zfs-receive/) for years.
Comment: @h0tw1r3 looks like this doesn't help either when there are so many tiny jpegs. It's around 24 million of jpegs in the folder and pigz is just using 1 core. Whereas if the files are larger, it uses the default 8 cores and is insanely fast.
Comment: @lbanz the speed that `tar` is able to collect small files is likely a disk or filesystem IO problem. The size of files should not make a difference to `pigz` because the data it receives is a `tar` stream, not the individual files.
Answer: I'd stick to the rsync solution. Modern (3.0.0+) rsync uses incremental file list, so it does not have to build full list before transfer. So restarting it won't require you to do whole transfer again in case of trouble. Splitting the transfer per top or second level directory will optimize this even further. (I'd use rsync -a -P and add --compress if your network is slower than your drives.)
Comment: Find (or build) a statically-linked rsync binary and just run it from your home. Hopefully that won't ruin no warranty.
Comment: I'm using rsync 2.6.8 on the old server. As it is one of those boxes where we're not allowed to install/update anything as stated by the vendor or it voids the warranty. I might update it and see if it is any quicker.
Comment: How about `unison`? How does it compare to `rsync`?
Answer: Set up a VPN (if its internet), create a virtual drive of some format on the remote server (make it ext4), mount it on the remote server, then mount that on the local server (using a block-level protocol like iSCSI), and use dd or another block-level tool to do the transfer. You can then copy the files off the virtual drive to the real (XFS) drive at your own convenience.
Two reasons:

No filesystem overhead, which is the main performance culprit
No seeking, you're looking at sequential read/write on both sides
Comment: If the server is using LVM, one could do a read-only snapshot of the filesystem and copy it instead. Space overhead only for the changes in the filesystem that happen while the snapshot is read.
Comment: Bypassing the filesystem is good. Copying block-level of a read-write mounted filesystem is a really bad idea. Unmount or mount read-only first.
Comment: Having a 15TB copy sucks, too. It means the new server needs minimum 30.
Answer: If the old server is being decommissioned and the files can be offline for a few minutes then it is often fastest to just pull the drives out the old box and cable them into the new server, mount them (back online now) and copy the files to the new servers native disks.
Comment: It's about 1PB of 2TB drives so it is way too much.
Answer: Use mbuffer and if it is on a secure network you can avoid the encryption step.
Answer: (Many different answers can work. Here is another one.)
Generate the file list with find -type f (this should finish in a couple of hours), split it to small chunks, and transfer each chunk using rsync --files-from=....
Answer: Have you considered sneakernet? With that, I mean transfering everything onto the same drive, then physically moving that drive over.
about a month ago, Samsung unveiled a 16 TB drive (technically, it's 15.36 TB), which is also an SSD: http://www.theverge.com/2015/8/14/9153083/samsung-worlds-largest-hard-drive-16tb
I think this drive would just about do for this. You'd still have to copy all the files, but since you don't have network latency and probably can use SATA or a similarly fast technique, it should be quite a lot faster.
Answer: If there is any chance to get high success ratio when deduplication, I would use something like borgbackup or Attic.
If not, check the netcat+tar+pbzip2 solution, adapt the compression options according to your hardware - check what is the bottleneck (CPU? network? IO?). The pbzip2 would nicely span across all CPUs, giving better performance.
Comment: lzma (`xz`) decompresses faster than bzip2, and does well on most input.  Unfortunately, `xz`'s multithread option isn't implemented yet.
Comment: Usually the compression stage needs more horsepower than decompression, so if the CPU is the limiting factor, pbzip2 would result in better overall performance. Decompression shouldn't affect the process, if both machines are similar.
Comment: Yes, my point was it's a shame that there isn't a single-stream multi-thread lzma.  Although for this use-case, of transferring whole filesystems of data, `pigz` would prob. be the slowest compressor you'd want to use.  Or even `lz4`.  (There's a `lz4mt` multi-threaded-for-a-single-stream available.  It doesn't thread very efficiently (spawns new threads extremely often), but it does get a solid speedup)
Answer: You are using RedHat Linux, so this wouldn't apply, but as another option:
I've had great success using ZFS to hold millions of files as inodes aren't an issue.
If that was an option for you, you could then take snapshots and use zfs to send incremental updates.  I've had a lot of success using this method to transfer as well as archive data.
ZFS is primarily a Solaris filesystem, but can be found in the illumos (open source fork of Sun's OpenSolaris).  I know there has also been some luck at using ZFS under BSD and Linux (using FUSE?)--but I have no experience on trying that.
Comment: There has been a non-FUSE native Linux port of ZFS for quite a while now: http://zfsonlinux.org/
Answer: Start an rsync daemon on the target machine.
This will speedup the transfer process a lot.
Answer: Try juicesync?
juicesync local/path user@host:port:path --threads=50

you can also use --worker and --manager mode to start more jobs.
Also you can try rclone(sftp).
Answer: You can do this with just tar and ssh, like this:
tar zcf - <your files> | ssh <destination host> "cat > <your_file>.tar.gz"
Or, if you want to keep individual files:
tar zcf - <your files> | ssh <destination host> "tar zxf -"
Comment: It will not deduplicate, no way to resume, compressing using only one CPU.

Transfer 15TB of tiny files


Offensive foul: why doesn't the basket count?

Question: The FIBA Interpretations contain example 33-2:

Example: A1 is in the air on the shot for a 3-point field goal. A1 extends his leg which contacts his defensive player B1.

The interpretation is as follows:

Interpretation: This is a foul by A1 for moving his leg outside the boundaries of his cylinder and contacting his defensive player B1.

Suppose the contact between the leg of A1 and the body of B1 occurs after the ball leaves the hand of A1 for the shot.
Let's further suppose that the ball enters the basket.
Does the basket count?
The interpretations don't say whether it counts or not.
Let's look at the rules.
Rule 16.1.1 says that:

A goal is made when a live ball enters the basket from above and remains within or
passes through the basket entirely.

So the question becomes: was the ball live when it entered the basket?
Let's apply Rule 10.4:

The ball does not become dead and the goal counts if made when:

The ball is in flight on a shot for a field goal and:

A referee blows his whistle.

In this case, when the contact occurred, the ball was in flight on a shot for a field goal.
Then a referee blew his whistle (for the offensive foul).
Then it looks like "The ball does not become dead and the goal counts".
So it seems like the goal counts?
(I know the goal doesn't count, but I would like to know why, where it is written, and how the fact the the goal doesn't count squares with rule 10.4 and the concept of live and dead ball)
I also accept answers according to the NBA rules, for completeness, although this question is specifically focused on FIBA rules.
Answer: For NBA rules, this is trivial to answer.  From the rulebook:

Section VII—Offensive Fouls

A personal foul assessed against an offensive player which is neither punching or flagrant shall be penalized in the following manner:

No points can be scored by the offensive team
The offending player is charged with a personal foul
The offending team is not charged with a team foul

EXCEPTION: Rule 3—Section I—a. No penalty free throws are awarded.
4. The ball is awarded to the offended team out-of-bounds on the sideline at the nearest spot where play was interrupted but no nearer the baseline than the free throw line

If the offensive player commits a penalty, no points can be scored, explicitly.
FIBA's rules are not clear on this in my mind, but I have never considered FIBA's rules to be clear on much of anything, despite their detail, so that doesn't surprise me.


Very odd Mouse Listener and If statements

Question: Before you read this I just want to say that any help would help me very much, right now. I'm very desperate. I've spent at least a week trying to get this work in Eclipse, I think I'm missing something obvious, I really need your help.
Right now my mouse listener contains if-statements for each 'room' and a double for-loop within those if-statements that tells the JFrame if the mouse is clicked within a certain region of the JFrame to repaint the corresponding 'room'.
Now let's say room #4 can lead to either #5 or #6. Going from #4 > #6 shows no issues. Now from #5 > #4 there is a problem. For some reason the event regions for room #4 show up in #5 (it should not) so now I can go to either #4 or #6 when I click on previous event regions.
I've tried this for other 'rooms' and the issue doesn't appear in them. I've concluded that it might have to do something with going back and forward between rooms that are connected with more than one path. I've attached a visual pathway and the isolated code to make things easier (The numbers are just the room #'s).
MouseAdapter mouseHandler = new MouseAdapter()
      {
          public void mouseClicked(MouseEvent e)
          {
             //System.out.println(e.getPoint());
            if(n==6)//R6
             {
                for(int i = 116;i<132;i++)//3a
              {
                  if(e.getX() == i)
                  {
                      for(int j = 388;j<404;j++)
                      {
                          if(e.getY() == j)
                          {
                            n = 7;//3b
                              return;
                          }
                      }
                  }
              }
                 for(int i = 116;i<132;i++)//2b
                  {
                      if(e.getX() == i)
                      {
                          for(int j = 308;j<324;j++)
                          {
                              if(e.getY() == j)
                              {
                                 n = 4;//2a
                                  return;
                              }
                          }
                      }
                  }
                 for(int i = 580;i<596;i++)//8a
              {
                  if(e.getX() == i)
                  {
                      for(int j = 372;j<388;j++)
                      {
                          if(e.getY() == j)
                          {
                             n = 2;//8b
                              return;
                          }
                      }
                  }
              }
             }
            if(n==5)//R5
             {
                 for(int i = 220;i<268;i++)//1b
                  {
                      if(e.getX() == i)
                      {
                          for(int j = 437;j<485;j++)
                          {
                              if(e.getY() == j)
                              {
                                  n = 4;//1a
                                  return;
                              }
                          }
                      }
                  }
             }
            if(n==4)//R4
             {
                 for(int i = 179;i<244;i++)//2a
                  {
                      if(e.getX() == i)
                      {
                          for(int j = 403;j<468;j++)
                          {
                              if(e.getY() == j)
                              {
                                 n = 6;//2b
                                  return;
                              }
                          }
                      }
                  }
                 for(int i = 436;i<500;i++)//1a
                      {
                          if(e.getX() == i)
                          {
                              for(int j = 403;j<468;j++)
                              {
                                  if(e.getY() == j)
                                  {
                                     n = 5;//1b
                                      return;
                                  }
                              }
                          }
                      }
                 for(int i = 274;i<406;i++)//A2
                  {
                      if(e.getX() == i)
                      {
                          for(int j = 193;j<276;j++)
                          {
                              if(e.getY() == j)
                              {
                                  n = 0;//A1
                                  return;
                              }
                          }
                      }
                  }
             }
            if(n==0)//R0
             {
                 for(int i = 459;i<493;i++)//A1
              {
                  if(e.getX() == i)
                  {
                      for(int j = 110;j<133;j++)
                      {
                          if(e.getY() == j)
                          {
                              n = 4;//A2
                              return;
                          }
                      }
                  }
              }
             }
            repaint();
             //http://stackoverflow.com/questions/5654208/making-a-jbutton-invisible-but-clickable
          }
          public void mouseMoved(MouseEvent e)
          {
              // = e.getPoint();
              //repaint();
          }
      };

      addMouseMotionListener(mouseHandler);
      addMouseListener(mouseHandler);

Comment: Let me introduce you to my friend [`Rectangle`](http://docs.oracle.com/javase/7/docs/api/java/awt/Rectangle.html) and it's wounderful method [`contains`](http://docs.oracle.com/javase/7/docs/api/java/awt/Rectangle.html#contains%28java.awt.Point%29) which would reduce your loops and ifs down to a few lines...
Comment: You've got a bug in your code. How we can help you is beyond me since you don't show any code. I know we're smart, but not that smart.
Comment: Before posting code you should do some *serious* debugging. I would either use a debugger or `println(...)` statements that show the state of your program's variables in an attempt to try to find out why things are not working right. Else you could be posting code that has no relevance to your problem.
Comment: Give me a second, I'll add the code quickly. EDIT: Okay, I've added, it would help me a lot if you could see what I'm doing wrong.
Comment: I've isolated the problem manually, I'm 98% sure that the problem relies in the mouse listener as this is where all the connections between the images rely in.
Comment: @MadProgrammer: you posted this as I was typing... arg! beaten by a comment!
Answer: This is designed to demonstrate the use of Rectangle and a self contained concept of a Room.
Each Room can have 1 or more exists, each exit leads to another room.  This is essentially a type of linked list.
The Rectangle for position for each is calculated dynamically, making easier to render and control.  This example basically will only show the active room and it's connected rooms.  Each time you click on a room, that room becomes the active room (in the center) and renders it's linked exists.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Rooms {

    public static void main(String[] args) {
        new Rooms();
    }

    public Rooms() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new RoomsPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class RoomsPane extends JPanel {

        private Room currentRoom;

        public RoomsPane() {

            currentRoom = new Room("0", new Dimension(100, 100), null, null);
            currentRoom.addExit(ExitPoint.East, createRoom(1, currentRoom, ExitPoint.West));

            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (getBounds(currentRoom).contains(e.getPoint())) {
                        // current room
                    } else {
                        for (Room room : currentRoom.getRooms()) {
                            if (getBounds(room).contains(e.getPoint())) {
                                currentRoom = room;
                                repaint();
                                break;
                            }
                        }
                    }
                }
            });

        }

        protected boolean chance() {
            return Math.round(Math.random() * 1) == 0 ? false : true;
        }

        public Room createRoom(int count, Room parent, ExitPoint entryPoint) {
            int width = (int) Math.round(Math.random() * 90) + 10;
            int height = (int) Math.round(Math.random() * 90) + 10;
            Room room = new Room(String.valueOf(count), new Dimension(width, height), parent, entryPoint);
            if (count < 10) {
                count++;

                if (chance() && !room.hasExit(ExitPoint.North)) {
                    room.addExit(ExitPoint.North, createRoom(count, room, ExitPoint.South));
                }
                if (chance() && !room.hasExit(ExitPoint.East)) {
                    room.addExit(ExitPoint.East, createRoom(count, room, ExitPoint.West));
                }
                if (chance() && !room.hasExit(ExitPoint.South)) {
                    room.addExit(ExitPoint.South, createRoom(count, room, ExitPoint.North));
                }
                if (chance() && !room.hasExit(ExitPoint.West)) {
                    room.addExit(ExitPoint.West, createRoom(count, room, ExitPoint.West));
                }

            }
            return room;
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        public Rectangle getBounds(Room room) {

            Rectangle bounds = null;
            // Can't get the bounds for a room not linked to current room
            if (currentRoom.isLinkedTo(room) || currentRoom == room) {
                Dimension size = currentRoom.getSize();
                int width = getWidth() - 1;
                int height = getHeight() - 1;
                int x = (width - size.width) / 2;
                int y = (height - size.height) / 2;
                if (currentRoom != room) {

                    switch (currentRoom.getExit(room)) {
                        case North:
                            y -= room.getSize().height;
                            x = (width - room.getSize().width) / 2;
                            break;
                        case South:
                            y += size.height;
                            x = (width - room.getSize().width) / 2;
                            break;
                        case East:
                            x += size.width;
                            y = (height - room.getSize().height) / 2;
                            break;
                        case West:
                            x -= room.getSize().width;
                            y = (height - room.getSize().height) / 2;
                            break;
                    }

                    size = room.getSize();

                }
                bounds = new Rectangle(x, y, size.width, size.height);
            }
            return bounds;

        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            FontMetrics fm = g2d.getFontMetrics();

            g2d.setColor(Color.GRAY);
            for (Room room : currentRoom.getRooms()) {
                Rectangle bounds = getBounds(room);
                g2d.draw(bounds);
                g2d.drawString(
                        room.getName(),
                        bounds.x + ((bounds.width - fm.stringWidth(room.getName())) / 2),
                        bounds.y + ((bounds.height - fm.getHeight()) / 2) + fm.getAscent());
            }

            Rectangle bounds = getBounds(currentRoom);
            g2d.setColor(Color.BLUE);
            g2d.draw(bounds);
            g2d.drawString(
                    currentRoom.getName(),
                    bounds.x + ((bounds.width - fm.stringWidth(currentRoom.getName())) / 2),
                    bounds.y + ((bounds.height - fm.getHeight()) / 2) + fm.getAscent());

            g2d.dispose();
        }
    }

    public enum ExitPoint {

        North,
        South,
        East,
        West
    }

    public class Room {

        private Dimension size;
        private Map<ExitPoint, Room> exists;
        private String name;

        public Room(String name, Dimension dim, Room parent, ExitPoint entryPoint) {
            size = new Dimension(dim);
            exists = new HashMap<>(4);
            this.name = name;
            if (parent != null) {
                addExit(entryPoint, parent);
            }
        }

        public String getName() {
            return name;
        }

        public Dimension getSize() {
            return size;
        }

        public void addExit(ExitPoint point, Room room) {
            exists.put(point, room);
        }

        public boolean isLinkedTo(Room room) {
            return exists.values().contains(room);
        }

        public Room[] getRooms() {
            return exists.values().toArray(new Room[exists.size()]);
        }

        public boolean hasExit(ExitPoint ep) {
            return exists.containsKey(ep);
        }

        public ExitPoint getExit(Room room) {
            ExitPoint ep = null;
            for (ExitPoint exit : exists.keySet()) {
                if (exists.get(exit) == room) {
                    ep = exit;
                    break;
                }
            }
            return ep;
        }
    }
}

Answer: You appear to be trying to see if the mouse has clicked inside of a region and you appear to be trying to do this with nested for loops. Don't use for loops for this purpose. Instead use if blocks. Simplest would be to create Rectangles and see if the Mouse has clicked inside of it using its contains(Point p) method.
Comment: @MadProgrammer: yeah, but you'll probably create and post some beautiful code. And that's a good thing.
Comment: I took one look at the if/loops and quickly closed the tab ;)
Comment: Is it possible to 'disable' the Rectangles after a new image has been repainted? This way there won't be overlapping Rectangles that would lead the user to different rooms.
Comment: @SelfDeceit That would be up to you.  You could maintain the `Rectangle` as part of a room class that had the ability to "hide" each one as required
Comment: @SelfDeceit I'd basically create a `Room` class that had 4 slots (one for each edge) which had links to the next room (if available).  That way you would be able to easily check in what direction the user can move...
Comment: I'm fairly new to Swing, that's why I was using for-loops because they only showed up upon a repaint and the program knows that. Is there a simpler way to hide it rather than adding more classes? EDIT: The game is just mouse input so I don't think direction would matter. What the user clicks are these portals that are drawn in the image. Are these rectangles translucent btw?


Programmatically check the checkbox of an exposed filter

Question: I need to check a checkbox on an exposed filter for a view based on the url. I can't seem to figure out how to do that. If x is detected in the url, I want to set one of the fields of an exposed filter to have the status checked="checked." It seems like that would be pretty easy, but I can't seem to figure it out.
I have tried various ways through:

hook_views_pre_view()
hook_views_pre_build()
hook_form_views_exposed_form_alter() (I feel like this should be the best way.)

I've found that I can effectively alter the results through hook_views_pre_view(), but I can't figure out how to check that freaking checkbox on the exposed filter.
Varioush failed attempts via hook_form_alter():

$form["field_intended_use_target_id"]["#default_value"] = ['357'];
$form_state->input["field_intended_use_target_id"][356] = '356';
$form['field_intended_use_target_id']['widget'][0]['value']['#default_value'] = ['357'];

Can anyone please help?
Comment: try `['#default_value'] = 357;` since value is not an array.
Comment: btw don't add single `''` nor `""` double quotes around numbers, because when you do that, you're making them "strings" type rather than "numeric".
Comment: No luck. Using xdebug I can see that it does in fact set the default value. But it still doesn't render with the checkbox checked. I checked source to confirm. I turned off my theme and used Bartik. Still no dice.
Comment: Also tried removing quotes. Just can't seem to get that checked attribute for anything.
Comment: I suggest you add a default value on your view's exposed filter settings, then check how the structure looks like, find where exactly the `['#default_value']` gets added inside `$form` array. Then you'll know where you need to put it.
Comment: Well, that was a good idea to at least rule out some stuff. `$form["field_intended_use_target_id"]["#default_value"][357] = 357` is the right way to set it. But It must be too late by the time it hits hook_form_views_exposed_form_alter. Because even though I can see that I'm resetting it from 358 (the test deafult) to 357 (the id I want) perfectly via xdebug, the other value (358) is still the one that shows up checked. I can't alter it. I guess this needs to be done in some other hook?
Comment: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/100890/discussion-between-no-sssweat-and-xpersonas).
Answer: I was finally able to achieve what I need via the hook_views_pre_build() hook.
Please let me know if this is wrong or if there is a better way, but from what I can tell this is the best way to achieve this.
function module_views_pre_build(ViewExecutable $view) {
  $termId = 357;
  $view->setExposedInput(['field_name' => [$termId => $termId]]);
}

Answer: You can use hook_form_alter() by adding view id condition.
The use the code below, replacing FILTER_NAME with the name of your filter and set the necessary options.
$request = \Drupal::request();
if (!$request->get(FILTER_NAME, NULL)) {
  $input = &$form_state->getUserInput();
  $input[FILTER_NAME] = [
    'opt1' => 'opt1',
    'opt2' => 'opt2',
  ];
}


Can you calculate the mean of a multivariate normal distribution for the cosine similarity?

Question: Let ${\displaystyle \mathbf {X} =(X_{1},\ldots ,X_{k})^{\mathrm {T} }}$ be a sample from  a multivariate normal distribution ${\displaystyle \mathbf {X} \ \sim \ {\mathcal {N}}({\boldsymbol {\mu }},\,{\boldsymbol {\Sigma }})}$.
Can you calculate the mean of the output distribution after transforming with the following formula (which is the cosine similarity in vector space), assuming that ${\displaystyle \mathbf {w}}=(w_{1},\ldots ,w_{k})^{\mathrm {T} }$ is a fixed vector?
$$
f(\mathbf{X}) =
\frac
{\sum_{i}{X_{i} w_i}}
{\sqrt{\sum_i{X_i}^2}\sqrt{\sum_i{w_i}^2}}
$$
Comment: @Zhanxiong yes, by calculation of the mean I meant the expectation [()]
Comment: It helps to simplify questions like these.  For instance, a suitable invertible linear transformation will map $w$ to $(1,0,\ldots, 0)$ while changing the parameters of $X$ in a definite way, giving another multivariate Normal distribution.  This simplifies the function to $f(X)=X_1/\sqrt{X_1^2+\cdots+X_k^2}.$  Now you can apply another invertible linear transformation to $X,$ reducing the function to $f(X)=X_1/\sqrt{X_1^2+X_2^2}.$
Comment: Is calculating $E[f(\mathbf{X})]$ your question?
Comment: @whuber Thank you for pointing this out! I suppose my follow up question is how to calculate the mean of the resulting function then?
Answer: You may find the answer quite intuitive. Consider the sketch below. As cosine similarity deals with the angle between $\bf{X}$ and $\bf{w}$, the question boils down to projecting $\bf{X}$ on a sphere passing through the center of the distribution of $\bf{X}$, that is, through $E[\bf{X}]$.
From the symmetry of the distribution of $\bf{X}$, the mean of the projected $\bf{X}$ is on the chord between $E[\bf{X}]$ and the origin.
Therefore, your answer is the cosine similarity between $\bf{w}$ and $E[\bf{X}]$.
Comment: Could you provide a little more detail about "the symmetry of the distribution"?  I don't believe this argument works, because that distribution (along with the fixed vector $w$) does not necessarily share the same similarities as the unit sphere.
Comment: I'd appreciate if you could please explain what similarities is in the context of the symmetry of a multivariate normal distribution about its mean.
Comment: The information in the problem consists of the matrix $\Sigma,$ which describes a collection of concentric ellipsoids oriented in its (arbitrary) eigendirections; the (arbitrary) mean $\mu,$ and the (arbitrary) vector $w.$  Anything deserving of the term "symmetry" would be a transformation of these data that preserve some property of interest, which here is the cosine similarity.  I am simply asking you to describe the symmetry to which you appeal in this post.  I cannot find any in general.
Comment: Not sure that i understand your comment, but "a multivariate normal distribution" is spherical symmetric about its mean point.
If you want to ask about the symmetry on the sphere, you may, for example, consider an infinitesimal thick sphere on which p(X) is constant, and consider the intersection between this sphere and  the sphere passing through the center of the distribution of X.
Please also consider this: [link](https://math.stackexchange.com/questions/1038847/is-the-intersection-between-two-n-spheres-an-n-1-sphere)
Comment: Your point of departure is incorrect: $\Sigma$ is not necessarily spherical.   The only symmetries a general multivariate Normal distribution enjoys are those generated by the reflections through the mean $\mu$ determined by the (generically) distinct eigendirections.
Comment: Correct. You are right about the distribution not being spherical. My bad. Yet, there is as symmetry which justifies the answer. The explanation is more complex though. Can you help?
Comment: I can't help, because I believe your answer is incorrect.
Comment: Thank you very much for the proposition and discussion! I may be able to help out: I forgot to mention that ${\boldsymbol {\Sigma }}$ is diagonal, i.e. we assume independence of $X_i$.


MathJax basic tutorial and quick reference

Question:
To see how any formula was written in any question or answer, including this one, right-click on the expression it and choose "Show Math As > TeX Commands".
For inline formulas, enclose the formula in $...$.  For displayed formulas, use $$...$$.  These render differently: $\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}$ (inline) or $$\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}\tag{displayed}$$
For Greek letters, use \alpha, \beta, …, \omega: $\alpha, \beta, … \omega$.  For uppercase, use \Gamma, \Delta, …, \Omega: $\Gamma, \Delta, …, \Omega$.
For superscripts and subscripts, use ^ and _.  For example, x_i^2: $x_i^2$.
Groups. Superscripts, subscripts, and other operations apply only to the next "group". A "group" is either a single symbol, or any formula surrounded by curly braces {…}.  If you do 10^10, you will get a surprise: $10^10$. But 10^{10} gives what you probably wanted: $10^{10}$. Use curly braces to delimit a formula to which a superscript or subscript applies: x^5^6 is an error;  {x^y}^z is ${x^y}^z$, and x^{y^z} is $x^{y^z}$. Observe the difference between x_i^2 $x_i^2$ and x_{i^2} $x_{i^2}$.
Parentheses Ordinary symbols ()[] make parentheses and brackets $(2+3)[4+4]$. Use \{ and \} for curly braces $\{\}$.
These do not scale with the formula in between, so if you write (\frac{\sqrt x}{y^3}) the parentheses will be too small: $(\frac{\sqrt x}{y^3})$.    Using \left(…\right) will make the sizes adjust automatically to the formula they enclose: \left(\frac{\sqrt x}{y^3}\right) is $\left(\frac{\sqrt x}{y^3}\right)$.
\left and\right apply to all the following sorts of parentheses: ( and ) $(x)$, [ and ] $[x]$, \{ and \} $\lbrace x \rbrace$, | $|x|$, \langle and \rangle $\langle x \rangle$,  \lceil and \rceil $\lceil x \rceil$, and \lfloor and \rfloor $\lfloor x \rfloor$. There are also invisible parentheses, denoted by .: \left.\frac12\right\rbrace is $\left.\frac12\right\rbrace$.
Sums and integrals \sum and \int; the subscript is the lower limit and the superscript is the upper limit, so for example \sum_1^n $\sum_1^n$. Don't forget {…} if the limits are more than a single symbol.  For example, \sum_{i=0}^\infty i^2 is $\sum_{i=0}^\infty i^2$. Similarly, \prod $\prod$, \int $\int$, \bigcup $\bigcup$, \bigcap $\bigcap$, \iint $\iint$.
Fractions There are two ways to make these. \frac ab applies to the next two groups, and produces $\frac ab$; for more complicated numerators and denominators use {…}: \frac{a+1}{b+1} is $\frac{a+1}{b+1}$. If the numerator and denominator are complicated, you may prefer \over, which splits up the group that it is in: {a+1\over b+1} is ${a+1\over b+1}$.
Fonts

Use \mathbb or \Bbb for "blackboard bold": $\mathbb{CHNQRZ}$.
Use \mathbf for boldface: $\mathbf{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$  $\mathbf{abcdefghijklmnopqrstuvwxyz}$.
Use \mathtt for "typewriter" font: $\mathtt{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$ $\mathtt{abcdefghijklmnopqrstuvwxyz}$.
Use \mathrm for roman font: $\mathrm{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$  $\mathrm{abcdefghijklmnopqrstuvwxyz}$.
Use \mathsf for sans-serif font: $\mathsf{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$  $\mathsf{abcdefghijklmnopqrstuvwxyz}$.
Use \mathcal for "calligraphic" letters: $\mathcal{ ABCDEFGHIJKLMNOPQRSTUVWXYZ}$
Use \mathscr for script letters: $\mathscr{ABCDEFGHIJKLMNOPQRSTUVWXYZ}$
Use \mathfrak for "Fraktur" (old German style) letters: $\mathfrak{ABCDEFGHIJKLMNOPQRSTUVWXYZ} \mathfrak{abcdefghijklmnopqrstuvwxyz}$.

Radical signs Use sqrt, which adjusts to the size of its argument: \sqrt{x^3} $\sqrt{x^3}$; \sqrt[3]{\frac xy} $\sqrt[3]{\frac xy}$. For complicated expressions, consider using {...}^{1/2} instead.
Some special functions such as "lim", "sin", "max", "ln", and so on are normally set in roman font instead of italic font. Use \lim, \sin, etc. to make these: \sin x $\sin x$, not sin x $sin x$. Use subscripts to attach a notation to \lim: \lim_{x\to 0} $$\lim_{x\to 0}$$
There are a very large number of special symbols and notations, too many to list here; see this shorter listing, or this exhaustive listing. Some of the most common include:

\lt \gt \le \ge \neq $\lt\, \gt\, \le\, \ge\, \neq$.  You can use \not to put a slash through almost anything: \not\lt $\not\lt$ but it often looks bad.
\times \div \pm \mp $\times\, \div\, \pm\, \mp$. \cdot is a centered dot: $x\cdot y$
\cup \cap \setminus \subset \subseteq \subsetneq \supset \in \notin \emptyset \varnothing $\cup\, \cap\, \setminus\, \subset\, \subseteq \,\subsetneq \,\supset\, \in\, \notin\, \emptyset\, \varnothing$
{n+1 \choose 2k} or \binom{n+1}{2k} ${n+1 \choose 2k}$
\to \rightarrow \leftarrow \Rightarrow \Leftarrow \mapsto $\to\, \rightarrow\, \leftarrow\, \Rightarrow\, \Leftarrow\, \mapsto$
\land \lor \lnot \forall \exists \top \bot \vdash \vDash $\land\, \lor\, \lnot\, \forall\, \exists\, \top\, \bot\, \vdash\, \vDash$
\star \ast \oplus \circ \bullet $\star\, \ast\, \oplus\, \circ\, \bullet$
\approx \sim \simeq \cong \equiv \prec $\approx\, \sim \, \simeq\, \cong\, \equiv\, \prec$.
\infty \aleph_0 $\infty\, \aleph_0$ \nabla \partial $\nabla\, \partial$ \Im \Re $\Im\, \Re$
For modular equivalence, use \pmod like this: a\equiv b\pmod n $a\equiv b\pmod n$.
\ldots is the dots in $a_1, a_2, \ldots ,a_n$ \cdots is the dots in  $a_1+a_2+\cdots+a_n$
Some Greek letters have variant forms:
\epsilon \varepsilon $\epsilon\, \varepsilon$, \phi \varphi $\phi\, \varphi$, and others. Script lowercase l is \ell $\ell$.

Detexify lets you draw a symbol on a web page and then lists the $\TeX$ symbols that seem to resemble it.  These are not guaranteed to work in MathJax but are a good place to start.  To check that a command is supported, note that MathJax.org maintains a list of currently supported $\LaTeX$ commands, and one can also check Dr. Carol JVF Burns's page of $\TeX$ Commands Available in MathJax.
Spaces MathJax usually decides for itself how to space formulas, using a complex set of rules. Putting extra literal spaces into formulas  will not change the amount of space MathJax puts in: a␣b and a␣␣␣␣b are  both $a    b$. To add more space, use \, for a thin space $a\,b$; \; for a wider space $a\;b$.  \quad and \qquad are large spaces: $a\quad b$, $a\qquad b$.
To set plain text, use \text{…}: $\{x\in s\mid x\text{ is extra large}\}$. You can nest $…$ inside of \text{…}.
Accents and diacritical marks Use \hat for a single symbol $\hat x$, \widehat for a larger formula $\widehat{xy}$. If you make it too wide, it will look silly. Similarly, there are \bar $\bar x$ and \overline $\overline{xyz}$, and \vec $\vec x$ and \overrightarrow $\overrightarrow{xy}$ and \overleftrightarrow $\overleftrightarrow{xy}$. For dots, as in $\frac d{dx}x\dot x =  \dot x^2 +  x\ddot x$,  use \dot and \ddot.
Special characters used for MathJax interpreting can be escaped using the \ character: \$ $\$$, \{ $\{$, \_ $\_$, etc.
If you want \ itself, you should use \backslash $\backslash$, because \\ is for a new line.

(Tutorial ends here.)

It is important that this note be reasonably short and not suffer from too much bloat. To include more topics, please create short addenda and post them as answers instead of inserting them into this post.
Comment: As stolen from http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference?rq=1 . Perhaps a mod can add the "FAQ" label. I think we really need a quick link, as it recurrently happens that new visitors dont know how to insert maths properly.
Comment: Thanks for posting this; added the FAQ tag
Answer: Looked into tables. A few times for me it would have been useful:
$$\begin{array}{c|a|c}
 & \text{Column A} & \text{Column B}& \\ \hline
\text{Row 1} & \alpha & \lambda \\ \hline
\text{Row 2} & \alpha-1 & 8 \\ \hline
\end{array}$$
$$\begin{array}{c|a|c}
 & \text{Column A} & \text{Column B}& \\ \hline
\text{Row 1} & \alpha & \lambda \\ \hline
\text{Row 2} & \alpha-1 & 8 \\ \hline
\end{array}$$
{c|a|c} refers to the number of columns, \hline indicates a new row. & refers to the next element in a row.