Taking many uniform random samples from very skewed distribution

Question: I have a dataset which has a very skewed distribution across a parameter S, which is in the range [0,1). There are many, many values close to 0, and very few above 0.05. My goal is to draw a random sample from this distribution which has a roughly uniform distribution across S.
I start by binning the points into 1000 linearly-spaced bins, and then calculate a weight parameter w_i = 1/N_i, where N_i is the number of points in bin i. The figure below shows the number of points per bin. Some bins have more than 100M points, others have as few as 500.

Then I perform a weighted random sample on this parameter. To perform the weighted random sampling, I sort by a parameter weight_order = -log(rand())/w_i and take the first N rows. (I originally saw this suggested on this Stack Overflow answer, and I have not thought through the math carefully.) Note that this is sampling without replacement.
This approach results in a uniform distribution as long as N is sufficiently small. See for example the histogram for N=1000:

The histogram is somewhat sparse because I have included all 1000 bins so the average number per bin N_i=1, but aggregating into fewer larger bins visually confirms that the sample is reasonably uniform.
However, for larger N, the resulting sample starts to become skewed. See the same but for N = 10000.

I want 10 samples per bin, but already the resulting sample is quite skewed towards the thicker parts of the source distribution.
Thus, my question: is there a better way to sample my original dataset such that I get an approximately uniform distribution even as N becomes larger? Ultimately I would like to sample 100000 points, which would ideally result in about 100 samples per bin, which is still quite a bit less than the smallest number of points in a bin in the original dataset (~500).
Comment: I'm not sure about "goal is to draw a random sample from this distribution which has a roughly uniform distribution". It's clearly _not_ uniform - so why? What am I missing?
Comment: Sorry but it is still unclear... Why can't you just sample from uniform distribution within the given interval? You'll end up with a perfectly uniform sample...
Comment: Sorry, still makes no sense, as you can't draw a _uniform_ subsample from non-uniform distribution. It's like trying to get a `U(0,1)` subsample from a `N(0,1)` distribution. Perhaps someone else understands what you're trying to do.
Comment: I think it is perfectly clear what OP is asking, and also it makes no sense. Suggest trying a beta distribution, not a uniform one. If OP wants to map a beta distribution into a uniform one, then the x-axis becomes non-linear. One can do this by having unequal histogram category widths, but whatever for? Why bother? Provide some motivation, please.
Comment: The original distribution is not uniform. I wish to draw a random subsample which is roughly uniform. The question of *why* I want to do this seems like an irrelevant detail, unless I am misunderstanding your question.
Comment: To convert your theoretical example into an empirical one, I would draw `N` samples from the original normal distribution and bin them into `i` bins. For each bin I would calculate a weight parameter `w_i = exp(x_i^2)` and then draw `N_s` samples without replacement. I expect that the resulting subsample would be approximately uniform (at least in the `N_s << N` limit). It wouldn't be literally uniform, but it would be effectively uniform to the degree of granularity I care about.
Comment: By the way, I am assuming implicitly that the range of possible values is the same. For your hypothetical this implies that the original normal distribution would have to be defined only over the range `[0,1)`. (I belatedly realized that your notation describes the mean and standard deviation, rather than the range...)
Comment: And since the probability distributions for both `N(0,1)` and `U(0,1)` have analytic expressions, the binning step is unnecessary. That's just to make it more directly analogous to my original question.
Comment: Maybe looking at a cumulative empirical distribution plot of your resulting sample will give you a better feeling whether the distribution is approximately uniform.
Comment: What you're doing doesn't make any sense. One reason is that some details are missing, particularly, an intent. If you clarify the purpose, then help will arrive. Otherwise it just looks nonsensical
Comment: The underlying dataset is not one-dimensional -- it's a table where the feature `S` is just one of many features/dimensions/columns. I want to take a random subsample of rows in the table, but constraining the subsample to be approximately uniformly distributed across `S`. The resulting subsample would keep only a tiny proportion of the rows with `S < 0.05`, but a large fraction of the rows with `S` between 0.4 and 0.95. But I can tell I'm not explaining myself well!
Comment: @abeboparebop still, why can't you simply sample from a multivariate uniform distribution?
Answer: I believe the difficulty you observe may simply be due to attempting to sample without replacement -- as you deplete the sparser bins, you can't avoid starting to sample the high-density bins more heavily.
If you weight the observations according to the inverse of the density but keep the weights to each observation unchanged you have a problem almost as soon as you start - consider the interval (0,1) split into two bins (split at $\frac12$), the first one with 98 observations and the second one with 2. We can calculate the weights to the observations in the second bin will be 49 times as large as the first bin. So far everything is fine, but within the first few draws you expect to take an observation from the second bin. Now unless you adjust the weights at this point, you're now on average twice as likely to get an observation from the first bin as the second, so the first bin starts to be more heavily sampled.
However, the problem you observe will happen even if you keep readjusting the sampling probabilities to observations (equivalently, if you weight the bins rather than the observations) as you sample because once you empty the smallest bins you can no longer sample anywhere else but the ones with lots of observations.
Again consider just two bins as above, one with 98 observations in it, and one with 2. We sample from each bin with a 50-50 chance (so that after we sample one observation from the second bin the weight to the remaining point roughly doubles). On average, after we have sampled only a few observations, the second bin will be empty.

Up to this point we will have sampled uniformly, but we can't sample the empty bin ever again and from now on things will start to look more and more like the original distribution until eventually we've sampled the entire original data set and the distribution shape is identical.
[If you try sampling with replacement you should see that you don't get this problem; that should serve to confirm my explanation is what you're seeing in what you're doing.]
Comment: I think this is exactly the correct explanation of the phenomenon -- indeed, sampling with replacement gives the expected uniform distribution. Unfortunately, sampling with replacement doesn't work for my use case. My hope is that there is a principled way to overweight the sparser bins based on the number of draws I will be making, given that I still expect to be drawing substantially fewer than the total number from each bin.
Comment: If the bins are equal width (you say "linearly spaced" but that's not quite unambiguous; I presume that's equal width) then my answer already responds to that. Simply continue to sample each bin with equal probability no matter how many (or how few) points it contains. If the bins are not equal width you'd need to sample points from the bins in proportion to their width.
Comment: Yes, equal width bins. So to make it explicit, your proposed sampling procedure would to first choose a bin (by e.g. uniform sampling) and then randomly choose an item within that bin? I did end up implementing essentially this solution and it has exactly the features that I wanted.
Comment: Yes, that's it. You can do it without choosing a bin -- by choosing points directly -- though then there's additional overhead since each time you choose a point you have to distributed the weight to that point across the rest of the points in its bin to keep the bin probability constant


$\overline{B}_{1}(\textbf{0})$ complete $\iff (X,\|\cdot\|)$ Banach

Question: Prior to this, I have shown that $\overline{B_{1}}(\textbf{0}) = \overline{B_{1}(\textbf{0})}.$ Now,

Let $(X,\|\cdot\|)$ be a normed linear space. Show $\overline{B_{1}}(\textbf{0})$ is complete $\iff (X, \|\cdot\|)$ is a Banach space.

The reverse case I have as very simply:

Suppose $(X,\|\cdot\|)$ is a Banach space. Thus, $X$ is complete. Since $\overline{B_{1}}(\textbf{0})$ is closed in $X$, it is complete.

However, I am unsure of the reverse direction. I know that I want to show that $(X,\|\cdot\|)$ is complete. Take an arbitrary Cauchy sequence in $X$ and show that it converges in $X$ under $\|\cdot\|$. I am not sure how to continue.
Comment: try to scale things
Comment: Step 2 is recalling that Cauchy sequences are bounded.
Answer: Let $\{x_n\}$ is Cauchy in $X$ and $\overline{B_1}(0)$ is complete. Because all Cauchy sequences are bounded it follows $\exists M>0:\,\sup\limits_{n\in\mathbb N}{\|x_n\|}\leq M$ (take $M\ge 1$). Denote $y_n=\frac{x_n}{M}$ and note that $\|y_n\|\leq 1\Rightarrow y_n\in \overline{B_1}(0)$. Now we show that $\{y_n\}$ is also Cauchy: Let $\epsilon>0$ is arbitrary and find $N\in\mathbb N$ such that $\|x_n-x_m\|\leq \epsilon,\,\forall n,m\ge N$
$$\|y_n-y_m\|=\|\frac{x_n}{M}-\frac{x_m}{M}\|=\frac{1}{M}\|x_n-x_m\|\leq \epsilon,\,\forall n,m\ge N$$
So $\{y_n\}$ is a Cauchy sequence in $\overline{B_1}(0)$ and therefore $\exists y\in \overline{B_1}(0):\,y_n\to y$ which means $\frac{x_n}{M}\to y$ or equivalently $x_n\to My\in X$.
Boundedness of $\{x_n\}$:
Let $\epsilon=1$ (arbitrarily taken) and find $N\in\mathbb N:\,\|x_n-x_m\|\leq 1,\,\forall n,m\ge N$. This implies $\|x_n-x_N\|\leq 1,\,\forall n\ge N$ and from triangle inequality it follows $\|x_n\|\leq \|x_N\|+1,\,\forall n\ge N$. So $\|x_n\|\leq \max\{\|x_1\|,\|x_2\|,...,\|x_{N-1}\|,\|x_N\|+1\},\,\forall n\ge 1$.
Comment: No, it is just a verification that every Cauchy sequence is bounded.
Comment: May I ask, is the boundedness part you showed part of the proof, or just a verification of the claim in line 1.


Finding the span of $AB=0 $ if I have the basis of $Ax=0$

Question: I have the basis of a kernel of a matrix $A\in M_{4\times 4}(\mathbb R)$, or the basis of $Ax=0$, suppose the given basis of the kernel is $v_1,v_2$. How can I find the span of $W$ such that $W= \{ B\in M_{4\times 2}(\mathbb R)|AB=0\} $ ?
(I have an example with numbers for the given basis and the matrix $A$ but I would prefer a general solution if it's not a problem)
Answer: $\mathrm{Im}(B)\subset \mathrm{span}(v1,v2)$ So every column of $B$ must be of the form $av_1+bv_2$.
A Basis of the subspace is therfore given by the matrices $(v_1~0~0~0),(v_2~0~0~0),(0~v_1~0~0),\dots$
Comment: Every column of $B\neq be$. why is the image of $B$ the span of the kernel ?
Comment: indeed it should have been subset to make sense
Comment: You have a typo there, you wrote be instead of $B$ and OK still why is that ?
Comment: $AB=0$ so if there is an element $x$ which $B$ doesnt map to $\mathrm{span}(v_1,v_2)$, then  $A(Bx)\neq 0$ since $Bx$ is not in the kernel, and thus $AB \neq 0$
Comment: I don't understand why this implies that the image of $B$ is a subset of the kernel.
Comment: the asumtion that there exists an element $x\in \mathrm{Im}(B) $ and $x\not\in \mathrm{span}(v_1,v_2)=\ker(A)$ lead to a contradiction because $Ax=0$ but $x$ is not in the kernel and thus $\mathrm{Im}(B)\subset\mathrm{span}(v_1,v_2)$
Answer: Fix the matrix $A$, noting $\ker(A) = \text{span}(v_1,v_2)$.  Let $B$ be a matrix such that $AB = 0$.
It follows that for all $x$, $A(Bx) = (AB)x = 0$.  So, $Bx \in \ker(A)$ for every $x$, so the image of $B$ is a subset of the kernel of $A$ if $B \in W$.
Now, suppose $B$ is an arbitrary matrix such that the image of $B$ is in the kernel of $A$.  It follows that for all $x$, we have $(AB)x = A(Bx) = 0$.  Since $(AB)x = 0$ for all $x$, $AB$ must be the zero matrix, so that $B \in W$ if its image is in the kernel of $A$.
So, $W$ is exactly the set of matrices whose image is contained in the kernel of $A$.  That is, $W$ is the set of matrices whose columns are of the form $\alpha v_1 + \beta v_2$ for $\alpha,\beta \in \mathbb{R}$.


Matlab code would not plot part of the function

Question: I tried recently to plot some velocicty fields for a tutorial problem in my fluids problem set. I wrote the following Matlab code
clear;

h_theta_multiple=0.01;
h_theta=h_theta_multiple*2*pi;
h_rho=0.1;

[theta,rho] = meshgrid(0:h_theta:2*pi,0:h_rho:5);
[x1,x2] = pol2cart(theta,rho);

N=[1 2 3 -1 -2 -3];

figure
for i=1:length(N)
n=N(i);
u1 = rho.^n .* cos(n.*theta);
u2 = rho.^n .* sin(n.*theta);
subplot(2,3,i);
quiver(x1,x2,u1,u2);
end

figure
for i=1:length(N)
n=N(i);
u1 = -rho.^n .* sin(n.*theta);
u2 = rho.^n .* cos(n.*theta);
subplot(2,3,i);
quiver(x1,x2,u1,u2);
end

It gives me the following

for the first and second functions respectively. I can't quite figure out why it doesn't plot the ones for n is negative... I tried to isolate everything but couldn't quite debug it in the end.
Comment: Interesting, the data is indeed there, but you can not see it. `quiver(x1,x2,u1,u2,'o')` will show it.
Comment: @AnderBiguri Yeah I just tried that and it works but I do want the vector form. It is so weird.
Answer: The problem is that for negative n the matrices u1 and u2 contain infinite values in some entries. quiver auto-scales the values, so everything gets compressed down to zero and thus does not show in the graph.
A solution is to replace infinite values by NaN:
clear;

h_theta_multiple=0.01;
h_theta=h_theta_multiple*2*pi;
h_rho=0.1;

[theta,rho] = meshgrid(0:h_theta:2*pi,0:h_rho:5);
[x1,x2] = pol2cart(theta,rho);

N=[1 2 3 -1 -2 -3];

figure
for i=1:length(N)
n=N(i);
u1 = rho.^n .* cos(n.*theta);
u2 = rho.^n .* sin(n.*theta);
u1(isinf(u1)) = NaN; % replace infinite values by NaN
u2(isinf(u2)) = NaN;
subplot(2,3,i);
quiver(x1,x2,u1,u2);
end

figure
for i=1:length(N)
n=N(i);
u1 = -rho.^n .* sin(n.*theta);
u2 = rho.^n .* cos(n.*theta);
u1(isinf(u1)) = NaN; % replace infinite values by NaN
u2(isinf(u2)) = NaN;
subplot(2,3,i);
quiver(x1,x2,u1,u2);
end

This gives
Comment: Oh yes of course, for the first iteration it is finding 1/0^n and hence the Inf values. Don't know why I didn't just have a look at the u1 and u2 arrays. Thanks a lot!


Angular-Cli from NodeJS

Question: Building my project using Angular-Cli works when I use it directly from my terminal window like this:
$ ng build
Now I'd like to run it from nodejs using spawn. However,
const spawn = require('child_process').spawn;
const bld = spawn("ng build");

throws:
child process has an error with code Error: spawn ng build ENOENT
child process exited with code -2

Why is this happening? (I am running everything from inside the same folder.)
Answer: Spawn doesn't behave like a command line.
You send the command and args separately like:
spawn('ng', ['build'])

In your case it tried to do ng\ build and can not find that file.

Angular-Cli from NodeJS


conditional defining static variables

Question: Looking for a way to make conditional static declaration.
I want to make some variables in my program static when compiling in release and global for testing while compiling in debug.
Since I don't want all my static variables to turn global when debugging so I thought maybe I could do a macro that defines a variable static according to a compiling flag. I want something like this:
#define STATIC(def) (def) ? : static

Can something like this be done?
Using C standard 89
Comment: isn't the conditional operator is run-time one?
Answer: How about using a preprocessor #if? You could do something like this:
#if YOUR_CONDITION
#define STATIC(def) static def
#else
#define STATIC(def) def
#endif

You could, in your case, also use #ifdef and #ifndef:
#ifdef DEBUG_MODE /*Define as static when DEBUG_MODE is defined*/
#define STATIC(def) static def
#else
#define STATIC(def) def
#endif

conditional defining static variables


How do I play this ornament/slur? (Guitar Tab)

Question: (As I have 2 questions about this specific tab and as they're both (probably) easy to answer, I figured I'd just ask them at the same time)

How do I play the ... ?

Ornament (Green)
I guess the ornament in combination with the 'H' means: Play 5th fret, play 5th fret again and shortly after hammer on the 7th. Is this right?
Slur (Red)
No Idea. Is this some kind of slide? But the two notes are on different frets! And what does the 'g.' above the arc mean?

PS: If you're interested in the whole piece, it's 'Wind Song' from Kotaro Oshio, available here.
Answer: You're correct about the ornament in green: the 5th fret note (an E) just before the 7th fret note (an F#) is called a grace note, and it is to meant to be a very short note played in anticipation of the main note (the F#).  How short is "very short"? That's up to you.
As for the indication in red: the 'g.' indicates a glissando, meaning a slide from the first note to the second one, with all the notes in between included.  In this particular piece, what you should do is slide from the 7th fret on the G-string (a D) down the G-string to the 4th fret---but just as you get to that 4th fret, you'll do a few things simultaneously:

play the E-string, 3rd fret (which is a G). Unlike almost every other note, the music doesn't suggest a left-hand fingering---I'd suggest using your middle finger (2).
let go of the G-string
play the open B-string

You could both let go of the G-string and play the open B-string in one movement by flicking your ring finger (the one sliding down the G-string) off the neck in such a way that it plucks the B-string on its way off the G-string.  But that would also probably cause the G-string to ring out, so you'd have to mute it with a free finger---I'd use my left-hand index finger.
This all sounds very complicated, but it's really not so hard with a little practice.  Best of luck!
Comment: For glissandi, it is better to use a straight line with 'gliss.' on it instead of a slur with a 'g.' in it.
Answer: As noted in the music, this looks like Bm7 played at the 7th fret, and then a slide and lift movement to G in the "open position."
While this makes sense for the 2nd ornament, this makes the first ornament appear more difficult, but the fifth fret on the B string is the "same note" as the open E string. This means that instead of a hammer on from 5 to 7 on the B string, you leave the your finger on the B string at the 7th fret and merely pluck the open E.
In this way you can execute both ornaments without a lot of hand motion.
(There is an indication of an open A string there which complicates things a little, but may indicate a whole hand movement to Am7 rather than a hammer etc.: imagine you are sliding from Bm7 to Am7 to Bm7 but plucking slightly "out of phase" with the hand motion.)
Comment: -1 This answer doesn't make sense to me. The included tablature indicates that the E is to be played on the B string, not the open E, and it doesn't address the OP's question about the 'g.' written above the slur. Finally, there is nothing in the music to indicate that the player should be holding down whole chords such as Bm7 or (especially) Am7.
Comment: I should clarify: Yes, there are chord indications above the staff. But those are there to help the player understand the harmony of the piece, not to tell the player to hold down a whole Bm7 chord or a whole G chord. The left-hand fingering given over the notes of the staff clearly indicate what the player should be holding down, and it's not a whole chord.
Comment: You are correct: I called the "g." a slide and lift movement, rather than a glissando. However I must take issue with the idea that the transcription addresses in any way what your *other* fingers are doing.
Comment: The standard notation includes explicit left-hand fingering on every note of the first half of the bar. For example, it indicates that the guitarist should hold the low B with the 3rd finger while playing the D, E, and F# with the 4th, 1st, and 4th fingers, respectively.


Index was outside the bounds of the array C# in unit test

Question: I input my QuaterDisplay as dummy data, but I get the error as below:

Index was outside the bounds of the array.

Code:
private string GetQuarterDisplay(DateTime dateKey)
{
    return ((AvailabilityDS.IntelTimeRow[])mAvailabilityDS.Time.Select("DateKey = '"
            + dateKey + "'"))[0].QuarterDisplay; //error occur here
}

internal void PropagateModelStartQuarter()
{
    object[] args = new object[0];
    m_privateObject.Invoke("PropagateModelStartQuarter", new System.Type[0], args);
}

Comment: So what's your question, and clean up your code please, it looks like it's been through war
Comment: It's giving you that error because you're trying to access a index in your array that isn't there. If you had a length of 5 indexes, `[0], [1], [2], [3], [4]` and tried to access `[5]` then this would give you the error `Index was outside the bounds of the array.`. Make sure there is actually data in your `AvailabilityDS.IntelTimeRow[]` array
Comment: what is `mAvailabilityDS.Time.Select("DateKey = '"dateKey"'"))`? Does it have any data in it?
Comment: You may want to use LINQ: `return mAvailabilityDS.Time.FirstOrDefault(x => x.DateKey == dateKey)`
Comment: there are number of row in Time table with DateKey, e.g.12/31/2006 12:00:00 AM 4/1/2007 12:00:00 AM 7/1/2007 12:00:00 AM 9/30/2007 12:00:00 AM 12/30/2007 12:00:00 AM 3/30/2008 12:00:00 AM 6/29/2008 12:00:00 AM 9/28/2008 12:00:00 AM 12/28/2008 12:00:00 AM 3/29/2009 12:00:00 AM 3/29/2009 12:00:00 AM
Comment: column of QuarterDisplay data:Q1 2007
Q2 2007
Q3 2007
Q4 2007
Q1 2008
Q2 2008
Q3 2008
Q4 2008
Q1 2009
Q2 2009
Q2 2009
Answer: This means that there is no row in mAvailabilityDS table Time with specified DateKey string representation.
I would replace this with strongly typed LINQ solution:
private string GetQuarterDisplay(DateTime dateKey)
{
    return ((AvailabilityDS.IntelTimeRow[])mAvailabilityDS.Time.Select()
        .Where(x => x.DateKey == dateKey)
        .Select(x => x.QuarterDisplay)
        .FirstOrDefault();
}

EDIT
The main problem is that you are searching your rows by string representation of date/time object which is dependant on the current culture used.
This string representation does not match standard ISO format datetime (like 2009-11-03 00:00:00)  that is used in DataTable internals to filter values.
If you really want to use not-strongly-typed approach, convert datet/time object to string using invariant culture.
private string GetQuarterDisplay(DateTime dateKey)
{
    return ((AvailabilityDS.IntelTimeRow[])mAvailabilityDS.Time.Select("DateKey = '"
            + dateKey.ToString(DateTimeFormatInfo.InvariantInfo) + "'"))[0].QuarterDisplay;
}

Comment: there are number of row in Time table with DateKey, e.g.12/31/2006 12:00:00 AM
4/1/2007 12:00:00 AM
7/1/2007 12:00:00 AM
9/30/2007 12:00:00 AM
12/30/2007 12:00:00 AM
3/30/2008 12:00:00 AM
6/29/2008 12:00:00 AM
9/28/2008 12:00:00 AM
12/28/2008 12:00:00 AM
3/29/2009 12:00:00 AM
3/29/2009 12:00:00 AM
Comment: So... What is your point? @nodisplay. Did you query those exact times?
Comment: column of QuarterDisplay data:Q1 2007 Q2 2007 Q3 2007 Q4 2007 Q1 2008 Q2 2008 Q3 2008 Q4 2008 Q1 2009 Q2 2009 Q2 2009
Comment: I get the error "Error 2 'Intel.ATMSS.MetricSystem.DataObject.AvailabilityDS.IntelTimeRow[]' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'Intel.ATMSS.MetricSystem.DataObject.AvailabilityDS.IntelTimeRow[]' could be found (are you missing a using directive or an assembly reference?)"  at LINE (.Where(x=>x.DateKey == dateKey))
Comment: You need to add a `using System.Linq;` directive in the file.
Comment: there is an error in "DateTimeFormatInfo", sorry I am first time user.
Answer: I am pretty sure this is the line in error:
((AvailabilityDS.IntelTimeRow[])mAvailabilityDS.Time.Select("DateKey = '"
        + dateKey + "'"))[0]

I guess there is no item on that time stamp, resulting in an empty array. If you try to access the first item in an empty array, that is the error you get.
If you use LINQ, you could use FirstOrDefault, which will not fail when there is no item. Instead, it will return the default value. In this case null:
var availability = ((AvailabilityDS.IntelTimeRow[])mAvailabilityDS.Time.Select("DateKey = '"
        + dateKey + "'")).FirstOrDefault();

and then:
if (availability != null)
{
    return availability.QuarterDisplay;
}
else
{
    // return a default value, or throw an exception
    return null;
}

Comment: I get the error at FirstOrDefault , Error 2 'System.Array' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
Comment: You should include `using System.Linq;` on top of your code file.
Answer: why don't you check for null?
private string GetQuarterDisplay(DateTime dateKey)
{
    var availabilityDS = (AvailabilityDS.IntelTimeRow[])mAvailabilityDS
    if(availabilityDS != null)
    {
        var time = availabilityDS.Time;
        if(time != null)
        {
            var elements = availabilityDS.Select('"+ dateKey + "'");
            if(elements.Any())
            {
                return elements[0].QuarterDisplay;
            }
        }
    }
    return null;
}

Answer: You are assuming that the Select class returns an item. If it's an empty array then you are indexing into an empty array. Use something like:
private string GetQuarterDisplay(DateTime dateKey)
{
    var times =((AvailabilityDS.IntelTimeRow[])mAvailabilityDS.Time.Select("DateKey = '"
        + dateKey + "'"));

    if (times.Length > 0)
    {
        return times[0].QuarterDisplay;
    }

    return null; // Or whatever
}

Alternatively use if (!times.Any())...
Answer: DataTable.Select didn't return any rows, therefore you get the mentioned exception if you try to access a non-existing row.
You may want to use LINQ instead which makes it much more readable:
var timeRow = mAvailabilityDS.Time.FirstOrDefault(x => x.DateKey == dateKey);
if(timeRow != null)
    return timeRow.QuarterDisplay;
else
    return null;

Maybe you don't find any rows because there is a time portion that you want to ignore. Then you can use the Date property of DateTime:
var timeRow = mAvailabilityDS.Time.FirstOrDefault(x => x.DateKey.Date == dateKey.Date);


Test if JavaScript is enabled and/or running correctly

Question: I've taken different approaches to find out whether JS is enabled / running correctly.
Usually I just do something like this:
$('.jstest').html('JavaScript works.');

So, I can see whether I accidentally disabled JS, or I have a syntax error and JS is not running correctly without consulting firebug.
Now, this requires me to modify every single page. With Greasemonkey I thought this problem could be solved, without interfering with the site. But so far I couldn't find a way. I've tried:

inserting a  right after the opening body
inserting a script tag with a src to a remote server

Both ways still work, even if the JS on the site is not executed because of script / syntax errors.
Now, I've tried to insert a noscript container with Greasemonkey and disable js to find out whether the JS on the site reacts while Greasemonkey is not affected. No, GM does not work without JS :P
It seems like GM is running independently from all other scripts on that site. Anybody know a solution?
Answer: Greasemonkey will run, even if the target page has JS disabled, because GM runs in its own, privileged sandbox.
You can test if JS is available on the target page by injecting a JS function and then using GM to see if that function actually ran.
Here's a script that does that:
// ==UserScript==
// @name            _JS active test
// @include         http://stackoverflow.com/questions/*
// @include         http://www.nextdaypets.com/*
// ==/UserScript==

if (window.top != window.self)  //-- Don't run on frames or iframes (normally).
    return;

//--- This function will be injected to see if JS is active on the target page.
function TestPagesJS () {
    var testNode        = document.createElement ("span");
    testNode.setAttribute ("style", "display: none;");
    testNode.setAttribute ('id',    'SpecialNodeToTestIfPageJS_IsActive');
    document.body.appendChild (testNode);
}

//--- Inject the test JS and try to run it.
var scriptNode          = document.createElement ("script");
scriptNode.textContent  = TestPagesJS.toString () + '\nTestPagesJS ();';
document.body.appendChild (scriptNode);

/*--- Do not call this function from within a timer.  If you do, and the page's JS
    is disabled (NoScript, etc.), the alerts will not fire.
*/
function CheckIsNodePresent () {
    var testNode        = document.getElementById ("SpecialNodeToTestIfPageJS_IsActive");
    if (testNode)
        alert ('Javascript works fine on the target page.');
    else
        alert ('Oops! Javascript is off or broken on the target page.');
}

CheckIsNodePresent ();

Note that the two @include directives are for a site where I allow and disallow JS, respectively (using the NoScript add-on).  (Script tests pass.)
Comment: Wow, that is a great solution. Plus the check for frames is great!
Comment: Glad you like it, Chris.  Did it answer the question for you?
Comment: Definitely did. I tried to translate that into jQuery, but did not get it to work. Read somewhere that jQuery is not able to insert script-tags properly...


Where to store oauth2 access/refresh tokens using a native (windows) desktop client?

Question: When implementing oauth2 authorization code grant flow, what would be the best practices for storing the access and refresh tokens between sessions? The client is a native windows desktop application.
My initial thought was storing the tokens in the windows registry after encoding them using the windows Data Protection API, with a hard-coded secret (entropy in DPAPI). This is rather simple to implement although I'm not sure if it's a good idea or not.
Answer: Use built in operating system secure storage, which will use storage private to your app and user. For Windows use the Windows Credential Manager - see the screenshots in my blog post.
My example desktop app is coded in Node / Electron and uses the Keytar Component to interact with WCM.
The Keytar Home Page provides more info - and you can then follow the same pattern if using different tech.


Finding $\lim_{t\to\infty}y(t)$, where $y$ is a solution to $\frac{dy}{dt}=e^yy^2(y-1)(y-2)^3(y-3)^5$

Question:
Let $y(t)$ be solution to the boundary value problem
  $$\frac{dy}{dt}=e^yy^2(y-1)(y-2)^3(y-3)^5,\qquad y(3)=\frac{1}{2}$$
  Find $$\lim_{t\to+\infty}y(t)$$

My Try: For $y\lt0$, $\frac{dy}{dt}\lt0$. For $0\lt y\lt1$, $\frac{dy}{dt}\lt0$. For $1\lt y\lt2$, $\frac{dy}{dt}\gt0$. For $2\lt y\lt3$, $\frac{dy}{dt}\lt0$. For $y\gt3$, $\frac{dy}{dt}\gt0$.   Hence for $y=\frac{1}{2}$, $\frac{dy}{dt}\lt0$, then $y(t)$ decrease as $t$ increase. But I don't know how to determine if $y(t)$ will decrease below $0$. Also if $y(t) $ go below $0$, then still have $\frac{dy}{dt}\lt0$. Now whether $y(t) $ will go to negative infinity.
Answer: $y=0$ is a solution and the right side is differentiable, so the uniqueness theorem applies. Thus crossing the value zero is not possible. The same goes for the other fixed points $y=1,2,3$.
Comment: Ok, I see. Can we conclude that $\lim_{t \to \infty}y(t)=0$ since $\frac{dy}{dt}\lt 0$. Still get trouble in this step.
Comment: Yes, because you know that $y(t)$ is monotonous, bounded on both sides, and at the limit point you get $0=y'(\infty)=f(y(\infty))$, thus the limit point has to be a root of $f$, the solution can not stall at some intermediate point.


Performance of TreeViewer.expandAll method

Question: In my application, an action is to expand all elements of a certain tree.
each time I execute this action, it takes longer time than before.
<code>long startTime = System.currentTimeMillis();
getTreeViewer().expandAll();
long endTime = System.currentTimeMillis()-startTime;
System.out.println("expandAll time :  " + endTime );
</code>
and this is a sample from the output.
expandAll time : 200
expandAll time : 800
expandAll time : 1800
expandAll time : 3200
-- This slowdown my plugin so much. Is this an issue with the method ?
Answer: Do you have a filter in place?  Filters and other TreeViewer collaborators are invoked on expand actions.  Also check the implementation of the LabelProvider to see if there's anything that could take a while when calculating the text label and image for each of the newly displayed tree items.
Comment: Yes, I have a filter. but even when I disable it, I go the same performance.
I use eclipse EMF generated providers for the label and content provider
Comment: OK. How many tree items are being exposed and how expensive is the getChildren() method on the ContentProvider?
Comment: about 100 item.

getChildren takes some milliseconds.
ex:

    getChildren takes 7 ms when expandAll takes: 1926 ms
Comment: You could also take a look at the getInput() method that is called by getRoot() in expandAll().
Comment: getInput also takes no time compared to createchildren in expandAll


Given $a_n$ converges to $a$ how to find this limit?

Question:
If the terms of a sequence $a_n$ converge to $a$, show that $\frac{a_1+a_2+...+a_n}{n}$ also converges to $a$. Also show that the converse isn't necessarily true.

Please help me with this- I am unable to approach this problem, and don't even know where to begin looking for a solution.
Comment: For the second part $a_k=(-1)^k$ works
Comment: See [this](https://math.stackexchange.com/questions/155839/on-cesàro-convergence-if-x-n-to-x-then-z-n-fracx-1-dots-x-nn).
Answer: Hint:
Just remind that if $a_n$ converges to $a$, as of a certain $N$, the terms $a_n$ remain at distance at most $\epsilon$ of $a$. Now if you take the average, you will have a finite number of terms with arbitrary values, but an infinity of them at distance $\epsilon$ or less. So with sufficiently many terms, the average will stay close to $a$.


Update previously pushed view controllers via delegation IOS6

Question: I'm still getting the hang of IOS delegation, so I hope this question makes sense as I explain what I want to do...
What I want to do is download some JSON Data on a background thread as soon as my app first runs.  The data will then be parsed and then update a global variable.  Once that happens I want all previously pushed view controllers to update their content based on the data that has been downloaded and parsed.
My proposed way of solving this problem would be to have either my app delegate or my custom Navigation Controller subclass be a delegate for a custom JSON object that will parse the data.  The delegate will run a protocol method that updates a global variable once the parsing is complete.
Now once this variable has been set, it will be available to any view controller that is pushed on to the navigation stack. I also want to update the view controllers that have been previously pushed so that their content can be updated.
I know I can make the top view controller an active delegate that will run a protocol but what about the previously pushed view controllers?  Is this even possible or is there another way to make previously pushed view controller update their content?
Answer: In this case you don't really want to use a delegate. You want several objects to listen to a specific event, so use NSNotificationCenter instead.
When your parser finished parsing the JSON do the following:
[[NSNotificationCenter defaultCenter] postNotificationName: @"FinishedDataParsing" object:self userInfo:nil;

This way you also won't need a global variable. You could either make your parsed data accessable in your parser object or use the userInfo dictionary to pass some information to the notification receiver.
Everywhere you want to do something when your parsing finishes you first have to register as an observer (you could do that in viewDidLoad):
 [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(dataParsed:)
    name:@"FinishedDataParsing"
    object:nil];

And obviously you have to implement your callback method to do what ever you want to do with your parsed data.
- (void)dataParsed:(NSNotification *)notification {
      // Do this to access the user info.
      NSDictionary *userInfo = notification.userInfo;

      // Or access your data parser object.
      DataParser *parser = (DataParser *)notification.object;
}

Also you should deregister as observer when you no longer need to get notified (e.g. in dealloc)
- (void) dealloc
{
   [[NSNotificationCenter defaultCenter] removeObserver:self];
   [super dealloc];
}

Comment: Wow, this sounds like a great alternative. I'll definitely give this a try. One question though, how important is it to deregister as an observer?  Reason being is because I think I'll have to add the ability for the user to update the app content by redownloading the JSON data on an as needed basis.
Comment: Yes, you definitely have to deregister or your app might crash. You will be save if you register in `viewDidLoad` and deregister in `dealloc`. That way you will always receive the notification when your view controller is alive.


Let $\bar{x_0}\in D$ if $f$ is continually differentiable in $\bar{x_0}$ then $f$ is differentiable in $\bar{x_0}$

Question: Let $D\subset\mathbb{R}$ a open set, $f:D\rightarrow\mathbb{R}^m$ a function and $\bar{x_0}\in D$ if $f$ is continually differentiable in $\bar{x_0}$ then $f$ is differentiable in $\bar{x_0}$
Here is the proof of the theorem, but i don't understand the red parts.

Questions:
1- How i know $f$ fulfill with the hyptohesis of mean value theorem (I tried to see this but i cannot) and i dont understand very well how he apply mean value theorem here.
2- Why $lim_{\bar{}h\rightarrow \bar{0}}\bar{v_k}(\bar{h})=\bar{x_0} $
3- Why that two symbols are equal (arrows)?
4-I don't see very clear the inequality.
Thanks for his help.
Answer: I manage to say something about number three and four, for one and two the notations are messy and I am not sure neither:
3) $\left<\nabla f(x_{0}),h\right>=\left<\displaystyle\sum_{k=1}^{n}\dfrac{\partial f}{\partial x_{k}}(x_{0})e_{k},\sum_{k=1}^{n}h_{k}e_{k}\right>=\displaystyle\sum_{k=1}^{n}h_{k}\dfrac{\partial f}{\partial x_{k}}(x_{0})$ by the orthogonality of $\{e_{k}\}_{k=1}^{n}$.
4) We have $\left|\dfrac{h_{k}}{\|h\|}\right|\leq\dfrac{\|h\|}{\|h\|}=1$.
Comment: Thanks man, very grateful.


Expectation of a random variable $X$

Question: 9 people are assigned to one of 17 posts uniformly at random and more than one person can be at one post. Letting $X$ be the random variable representing the number of occupied posts, I was tasked with computing the expectation.
For my sample space $\Omega$, I was thinking of doing tuples where a person is mapped to one of the 17 posts with $|\Omega|=17^{9}$ (by stars and bars).
From here on I'm always confused with expectation. Would it be beneficial to utilize indicator random variables or do I have all I need to solve this?
Comment: You cannot use stars and bars for this problem.  Stars and bars are good for counting how to assign indistinguishable balls into distinguishable boxes.  But here the people are distinguishable, and the same space has size $17^9$ (and every sample point is equally likely).
Comment: Thanks for the clarification
Answer: Yes, it is simplest to use indicator random variables and the linearity of expectation.
Let $X_i$ equal one if post $i$ is occupied and zero otherwise.  $X$, the count of occupied posts is then the sum of the seventeen indicators. $$\mathsf E(X)=\mathsf E(\sum_{i=1}^{17}X_i)$$

Expectation of a random variable $X$


Does this residual plot look bad?

Question: I am not sure about this plot if it looks good as the dots appear around the line. So I just want to ask if it looks okay. My data is quite large (almost 3000 observations) and maybe that is why it looks like this.
Comment: No, it looks fine. When you say 'why it looks like this', which particular features are you concerned about? What is it you think it should look like?
Comment: I wish my residual plots looked as good as this.
Comment: Are you talking about the 'clumping' of the fitted values at low values (below 10000, say)? That's really not an issue.
Comment: I'd add to @Penguin_Knight one more - to plot studentized residuals either.
Comment: Thanks Glen_b. You reply relieves me. I thought it might look bad because everytime I see a good-looking residual plot, the dot would go around the line and not clustered like this, so I am not sure if mine is okay. Maybe because the size of data in the examples is less than mine.
Comment: Also, consider plotting standardized residual (in z-score) rather than in their original unit in the y-axis. That way you can see how many of them got beyond +/- 3 or +/- 4 SDs. And I agree that it looks fine.
Comment: Never give up Hong Ooi. I usually find people here are willing to help.
Answer: Have you tried plotting this using ggplot2 in R? It has a nice semi transparency feature with the Cairo package which makes guesstimating the mean for such residual plots easy. For example you could have each point semi transparent and you could visually check if they are centered around 0. But overall by looking at the image you posted, no reason to think otherwise.
Answer: The plot of the residual looks good. To be very sure get the mean it should be very close to zero. In fact it should approximate to zero when you approximate to 1 decimal place

Does this residual plot look bad?


Exibir String completa no modal

Question: estou usando Java,JPA, Wildfly e Primefaces.Eu tenho uma tabela que exibe um texto,mas esse texto esta sendo cortado na exibição pelo tamano da tabela.Resolvi criar um boão que chama um modal para exibir o texto completo.Cada linha da tabela tem o seu botão para exibir o seu detalhe.
O problema é que em qualquer botão que eu aciono ele me mostra a ultima string, o ultimo texto.
Segue o código em XHTML da coluna:

<code> <p:column headerText="Detalhes">
       <p:commandButton value="Visualizar detalhes" type="button"
        onclick="PF('dlg1').show();" update="idDescricao" />
       <p:dialog header="Modal Detalhes" widgetVar="dlg1" minHeight="40"
        width="700" id="idDescricao">
         <p:outputPanel id="multiCarDetail" style="text-align:center;">
           <ui:repeat value="#{cadastroMB.listaCadastro}"  var="cad">
        <h:outputText value="#{cad.descricao}" />
        </ui:repeat>
        </p:outputPanel>
       </p:dialog>
      </p:column></code>

A Imagem abaixo mostra que acionei o segundo botão mas esta me exibindo a da primeira linha
Answer: O problema esta que no seu teste não existe espaço entre as palavras "Teste", o que a torna uma palavra muito grande.
O PrimeFaces quebra a linha como é o caso da primeira linha da imagem.
Experimente dar espaços entre as palavras isso tornará o teste mais realista.
Comment: Exatamente, antes eu utilizava um textArea, resolvi mudar para o painel de edição, funcionou normalmente pq digiando na mão funciona só que eu esava copiando e colando um texto, erro bobo mas que nao prestava atenção na hora de desenvolver, Obrigado Marcelo

Exibir String completa no modal


Why do a 1, 3, and sharp 4 work well in this piece?

Question: I recently came across a short piece which I decided to try and analyze, but I have some questions about it. I am still a bit new to music theory but if I'm correct, most instances of non-diatonic notes/chords are instances of secondary dominants or borrowing from the parallel minor key.
However, measure 21 confuses me. In the measures leading up to it, E and C are played with a "bass note" which steps down chromatically from A to Ab (or is it G#) then to G then to Gb. The way I interpret this is using a vi chord, then the Ab is borrowed from the minor key in the context of the very common iv chord, then we have the I chord, then this mysterious Gb/F# shows up.
Thus my question: Why does this sharp 4, then followed by a 3, sharp 4, and a 1 work well? Is it just the chromaticism set up by the previous measures, or is there more going on? Maybe something lydian related?
Also, on more than one occasion, the song uses the notes Bb, E, and G to form what I believe is a diminished chord. It is then followed by the V/ii, as in measures 5-6 and 12-14. Again, chromaticism supports the Bb in the previous measures.
In short: What is "going on" with 1-3-#4 and 3-5-b7 chords in a major key? Why do they work well? Also, do you know of any other pieces which have these?
Answer: First you should be aware that there is a glaring error in this transcription. Either there is a beat missing in bar 10 or bar 10 should be a 3/4 bar because beat 4 of bar 10 and every other bar after that is actually a downbeat. It is apparent by the phrasing of the melody. Play the midi file with the play button and you will see that every 4th beat starting at bar 10 sounds like a downbeat.
Taking that into account beat 4 of bar 21 and the first 3 beats of bar 22 are actually the same bar with the F# being the downbeat. This is an F#m7b5 chord (with an added 9th) which transitions beautifully to a C/G because of the half step motion up in the bass and the E common tone in both chords. The #IVm7b5 is a very colorful chord often used by jazz players as a substitute for a ii or IV chord and usually to start a descending pattern but in this case it is used to ascend to the I chord with the 5th in the bass.
Leading up to that I agree with your analysivi on bar 18/19 but the following 2 bars I hear as G#+ then C/G and finally the F#m7b5, a fairly common and effective chromatic descending pattern.
The other thing you mentioned the Bb E G does have the makeup of a diminished chord. It is part of a progression with a chromatic descending bass line under a common tone of  G: C G/B Eo/Bb A7 then Dm.
As a postscript I would like to add the the G#'s in bars 9 and 10 should be Ab's since this is clearly a IVm or Fm chord.
Comment: You're right! The sheet music I originally looked at is actually different than this version. Also, thanks for identifying the F#m7b5. I'm definitely going to go and mess around with this to see what sounds I can get.
Comment: Great to hear, I found a recording on YouTube and bar 10 is a 3/4 bar. It was enjoyable to listen to and figure this one out. I like the harmonic style of using moving bass against fairly stationary upper harmony. It's sort of the opposite of a bass pedal point.
Comment: Worth mentioning that F#m7b5 (half diminished - why call it that?!) is a reincarnation of Am6 - which is easily found in key C, as a derivative of the vi chord - although here with a bass note of F# the former name fits better. I sometimes wonder whether a missing (or added) beat is actually on purpose, or just an omission (or addition) unknowingly left out (or put in), having worked with singers who had the propensity to do that, and not at will... +1.
Comment: Thanks @Tim, I remember reading years ago that Thelonious Monk referred to that chord as a minor 6th with the 6th in the bass, an inversion like you mentioned. As far as the dropped beat (?) goes, I listened to a few different versions on YouTube and the 3/4 bar is in most of them so in this case it's not a singer's lack of counting skills, lol.
Comment: Just thought - it's called *half diminished seventh* which is still somewhat of a misnomer. What have I missed?
Comment: @Tim I agree, the naming of this chord as half diminished is odd, guess we just have to accept it since they already printed it in a book somewhere. Even dim7 can imply a m7 instead of a o7 since every other chord that has 7 has a b7. I think m7b5 more accurately describes it. Check out paragraphs 2 and 3 under "Chord Symbols and Terminology" here: https://en.wikipedia.org/wiki/Half-diminished_seventh_chord I like Milton Babbitt's quote at the end.


How do I parse my own HTML files in Flask?

Question: I am trying to create a sitemap for my website instead of having to run it through a website that will make it for me. This is because the website changes quite often.
I found code online that achieves part of it:
@app.route('/sitemap.xml', methods=['GET'])
def sitemap():
    try:
      """Generate sitemap.xml. Makes a list of urls and date modified."""
      pages = []
      seven_days_ago = (datetime.datetime.now() - datetime.timedelta(days=7)).date().isoformat()
      for rule in app.url_map.iter_rules():
          if "GET" in rule.methods and len(rule.arguments) == 0:
               pages.append( ["..." + str(rule.rule), seven_days_ago])

      sitemap_xml = render_template('pages/sitemap_template.xml', pages=pages)
      response = make_response(sitemap_xml)
      response.headers["Content-Type"] = "application/xml"

      return response
    except Exception as e:
        return(str(e))

It works to create a basic sitemap. Okay, easy enough.
I want to add a priority in the meta tags of each page and then build the sitemap off of that. This SO question/answer covers that but it is using beautifulsoup and urllib, and is geared more toward the web, not a local instance.
So, I figure I need to render_template for each route (in this case, rule.rule) then parse that, maybe with BeautifulSoup and get the priority. I have no idea how to do this. Is there a way to GET each template per its route so I can parse it?
Comment: [https://www.flaskapi.org/api-guide/parsers/] hope it helps
Answer: I ended up using beautifulsoup4 on those routes. Shame that flask does not seem to offer a more direct way of this.


How to pass an entire map

Question: got another issue.
Im trying to send an entire Map of objects and their associated keys so that it can be accessed from another class. The method I am using does not work, as I tried to use the same method that is used to pass an array list, but it does not work for a Map.
what would be the correct way of doing it?
this is what I have tried
public Map <Integer, Employee> getAllEmps()
{
     return (Map <Integer, Employee>) ;
}

and this is how I have declared it (the actual map itself)
private static  Map <Integer, Employee> employeeMap = new TreeMap<Integer,Employee>();

Comment: you should be returning `employeeMap` instead of returning an empty typecast
Comment: Doesn't the compiler tell you what is wrong? What error message is it giving you?
Comment: I'm curious, what book/resources are you using to study Java, the language itself?
Answer: You have to return the map instance, not the type.
public Class SomeClass{
     private static Map<Integer,Employee> employeeMap=...;

    //Other methods

   public Map <Integer, Employee> getAllEmps()
  {
      return employeeMap ;
  }
}

If you want only the employees (with no keys) you can add another method
 public Collection<Employee> getEmployees(){

     return employeeMap.values();
  }

Answer: You can return the variable itself:
public Map <Integer, Employee> getAllEmps()
{
     return employeeMap;
}

Which would allow anyone to modify the contnent of the map.
To prevent modification, you can return an unmodifiableMap:
public Map <Integer, Employee> getAllEmps()
{
     return Collections.unmodifiableMap(employeeMap);
}

Alternatively, you can return a copy:
public Map <Integer, Employee> getAllEmps()
{
     return new TreeMap<Integer,Employee>(employeeMap);
}

Comment: I have made the method in the MainDriver class and I want to access it from the other class. this line of code does not seem to work, how would I do it correctly

theEmployeeMap = MainDriver.getAllEmps();
Answer: Why don't you try passing the variable itself and have the map as a member variable?
private Map <Integer, Employee> employeeMap = new Map <Integer, Employee>();
public Map <Integer, Employee> getAllEmps()
{
     return employeeMap;
}

Comment: new TreeMap()
Answer: Your getAllMaps () function needs to actually return a map; do you mean it to return the static employeeMap?
Comment: Oh hello again Dave
Nah, I just want to return a map.
Comment: I have made the method in the MainDriver class and I want to access it from the other class. this line of code does not seem to work, how would I do it correctly. I have obviously declared theEmployeeMap

theEmployeeMap = MainDriver.getAllEmps();
Comment: @Binyomin That syntax would imply a static method, not an instance method.
Comment: Thanks for getting back to me.
its okay I declared it in the constructor rather then in the program itself so it wanted to be declared    as in: int i;
Im as little over tired:)

How to pass an entire map


Is converting audio to MIDI or sheet music difficult (for pro musicians or software)?

Question: I watched this Youtube video about converting audio to MIDI, and the comments mostly point out that it's a scam or hoax.
Is it really difficult for software to convert audio to MIDI, and if so why?
Also, do pro musicians find this hard too?
Comment: I've read about some software tools that have made large advances in converting audio to MIDI, but several converter tools transcribe unpitched percussion as pitched notes, with disastrous results.
Answer: I had a quick listen to part of the YouTube video - they guy is confusing audio & MIDI a bit, really. He's done the multi-pitch analysis, & is then dragging notes up & down... fine, but that's not MIDI, that's Melodyne live-shifting one single note from the chord as audio, in realtime [which is one hell of a feat anyway]
To the uninitiated - Melodyne is "Auto Tune" on steroids.
It is neither scam nor hoax. I own the software & have done for a decade [or more] The manufacturer, Celemony, have a scant entry on Wikipedia but you can investigate Melodyne on their own site.
Melodyne is about the only commercially-available software than can analyse chords & separate the pitches. With audio, you can then shift pitches within the file without changing the entire song.
Whether it can convert this, without error, into MIDI would be a whole different level of task, & one I have never actually needed to test. I use it entirely as an audio medium.
What it can do is such as this...
REM - Losing My Religion in a major key

Yes, it sounds truly weird :\ but if you didn't know the original, I doubt you would be able to tell what had been done to it.
Pitch tracking itself has come a long way over the years.
Back in the late 90's I worked for Yamaha R&D & we were truly proud at that time to be able to play midi drums & piano, whilst live-tracking a real bass guitar, audio to midi, & sync that all to a streamed vocal recording, with a video of the entire performance, as a kind of 'simulcast'.
It's too old to have ever made it to YouTube, so I can't demonstrate how it did actually work - even in a rather primitive way.
Do pro musicians find this hard too?
Some do, some don't. I know many people who can transcribe accurately with little repetition, though mostly they listen to one instrument at a time.
I do know one guy who can transcribe an orchestra, vertically, down the page, in 2-bar segments, pausing the audio only long enough for him to physically write all the notes.
That is an extremely rate talent.
I realised I have one of his transcriptions already on Soundcloud - this is a MIDI file, a "one box demo" i.e. all played at once into a single 16-channel MIDI device (Yamaha XG MU100, circa 1997), the stereo output recorded untouched. I did the mix, he did the transcription from audio to MIDI.
Answer: Yes, it's hard.  And it gets harder as the music gets more complex.  I think there would be general agreement that, at the present state of the art, a skilled and experienced musician is way ahead of any computer program.  But computers have a habit of catching up....


Is the Below Theorem true:

Question:
Given a $n{\times}n$ matrix $A$. $A$ can be partitioned into cells in $2^{2n-2}$ ways.

Proof
A $n{\times}n$ matrix has $n^2$ elements. The number of columns is $n$, and the number of rows is $n$. A partitioning of the matrix into cells is done by drawing lines to divide the matrix.
The number of horizontal lines is $n-1$.
The number of vertical lines is also $n-1$.
The total number of lines is $2(n-1) = 2n -2$ lines.
There is a partition that involves none of these lines, which is $A$ itself.
All the other partitions are formed by selecting any $k$ lines.
Number of partitions $N$:
$$N = \sum_{i=0}^{2n-2} {(2n-2)\choose{i}} \tag{1}$$
Now, the cardinality of a power set $P(S)$ of $S$, where ${\#}S = m$ is given by the below formula:
$${\#}P(S) = 2^m \tag{2}$$
${\#}P(S)$ can alternatively be calculated as:
$${\#}P(S) = \sum_{i=0}^{m} {m\choose{i}} \tag{3}$$
Equivocating $(2)$ and $(3)$:
$$\sum_{i=0}^{m} {m\choose{i}} = 2^m \tag{4}$$
Substituting $(4)$ into $(1)$:
$$N = 2^{2n -2}$$.
Q.E.D
Answer: Yes, I think you're correct. Alternatively, once you have established that there are $2n-2$ possible lines, you could argue that each line can either be drawn or not ($2$ choices), and each set of choices of which lines to draw results in a unique partitioning. Thus, there are $2^{2n-2}$ partitions.

Is the Below Theorem true:


Can you perform a UNION without a subquery in SQLAlchemy?

Question: Another question shows how to do a union in SQLAlchemy.
Is it possible to perform a union in SQLAlchemy without using a subquery?
For example, in the linked question SQLAlchemy produces SQL of the form:
SELECT * FROM (SELECT column_a FROM table_a UNION SELECT column_b FROM table_b)
But I would like SQLAlchemy to produce SQL like:
SELECT column_a FROM table_a UNION SELECT column_b FROM table_b
The latter SQL is shorter, doesn't use a subquery, and does the same thing. Can SQLAlchemy produces a union like the latter query?
Comment: `session.execute(s1.union(s2))`?
Comment: @univerio No. If you print s1.union(s2) you'll see it's using a subquery.
Answer: Per the SQLAlchemy SQL Expression Language Tutorial:
You can use sqlalchemy.sql.expression.union:
from sqlalchemy.sql import union
u = union(
    addresses.select().where(addresses.c.email_address == 'email@example.com'),
    addresses.select().where(addresses.c.email_address.like('%@yahoo.com')),
).order_by(addresses.c.email_address)
u.compile()

This produces:
SELECT
    addresses.id,
    addresses.user_id,
    addresses.email_address
FROM addresses WHERE addresses.email_address = ?

UNION

SELECT
    addresses.id,
    addresses.user_id,
    addresses.email_address
FROM addresses
WHERE addresses.email_address LIKE ?
ORDER BY addresses.email_address

Params: ('email@example.com', '%@yahoo.com')


Tensorflow Contrib module will not be included in TensorFlow 2.0

Question: I am training my own model using Python on Windows 10 with TensorFlow 2.0 and Python 3.6, Anaconda 3. When I run the commands:
python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/faster_rcnn_inception_v2_pets.config

to train my images, it shows that The Tensorflow Contrib module will not be included in TensorFlow 2.0. What should I do with TensorFlow now? Do I need to upgrade or downgrade TensorFlow? Which Anaconda Promt commands should I run?
Comment: Right now only TF 2.0 alpha is available, you shouldn't be using it to run existing code as it breaks API.
Comment: Tensorflow 2.0 is now officially available. Please check https://www.tensorflow.org/
Answer: tf.contrib.* will indeed disappear from tensorflow, and will not even make it to the tf.compat* namespace.
It is one of the breaking change in TF 2.0. There are many, many others, as it is not only a major revision of TF but a complete change of mindset IMHO. Generally speaking, you cannot really expect a TF 1.x script to run smoothly in TF 2.0, even after a run of their automatic conversion script. You will need to rewrite your code, and before that, rethink your way of working with TF, especially if you were ignoring the Keras API.
Answer: In the Anaconda environment, execute:
conda install tensorflow=1.15

or
conda install tensorflow-gpu=1.15

But the warning will appear, It is the last stable version of Tensorflow 1.


Não consigo carregar dados em um mat-select angular 6

Question: Esse é meu html

  <form [formGroup]="createForm" (ngSubmit)="onPostCreateTypeFields()" style="width:400px; margin: 0 auto;">

Fields

        <input formControlName="name" type="text" placeholder="Fields" class="form-control">

*

        <input formControlName="name" type="text" placeholder="Nome" class="form-control">

*

    <mat-form-field>
        <mat-select placeholder="Type Fields" [(ngModel)]="dataSource" name="field">
          <mat-option *ngFor="let field of dataSource">
            {{field.name}}
          </mat-option>
        </mat-select>
      </mat-form-field>

    <button type="submit" class="btn btn-primary" >Criar</button>
</form>

esse é meu componente
import { Component, OnInit } from '@angular/core';
import { FieldsService } from '../Services/Fields/fields.service';
import { setTheme } from '../../../node_modules/ngx-bootstrap/utils';

@Component({
  selector: 'app-fields',
  templateUrl: './fields.component.html',
  styleUrls: ['./fields.component.css'],
  providers: [FieldsService]
})
export class FieldsComponent implements OnInit {

  selectedValue: string;
  private operUrl: 'api/Fields';
  public dataSource: Model.Itens[];

  constructor(private _fieldService: FieldsService) {
    setTheme('bs3');
  }

  ngOnInit() {

    this._fieldService
      .getAll<Model.Result>()
      .subscribe((data: Model.Result) => {
        debugger;
        this.dataSource = data.itens;
      });
  }
}

meu Model.Result
declare namespace Model{
    export interface Result{
        error: boolean;
        itens: Array<Itens>;
        message: string;
    }

    export interface Itens{
        operatorId: string;
        name: string;
    }
}

esse é o erro

compiler.js:215 Uncaught Error: Template parse errors: 'mat-option' is
  not a known element:
  1. If 'mat-option' is an Angular component, then verify that it is part of this module.
  2. If 'mat-option' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component
  to suppress this message. ("eld>

            [ERROR ->]
              {{field.name}}
             "): ng:///AppModule/FieldsComponent.html@31:10 'mat-select' is not a known element:

Como resolvo isso?
Answer: Nao esqueca de importar o MatSelectModule no module que vc declara seu componente
import {MatSelectModule} from '@angular/material/select';

imports: [..., MatSelectModule]


Showing that the flow value is well defined.

Question: Let $G$ be a directed graph with edge set $E$ and $u:E \to \mathbb{R}_{\geq 0}$ a capacity function, as well as $x: E \to \mathbb{R}_{\geq 0}$ be a function. Then $x$ is called an $s-t$ flow if the following properties are satisfied:
$(a)$ $x(e) \leq u(e)$ for all $e \in E$,
$(b)$ for all $v \in V - \{s,t\}$ it holds that: $\sum_{e \in \delta^{+}(v)} x(e)-\sum_{e \in \delta^{-}(v)} x(e)=0,$ where $\delta^+(v)$ denotes the edges that are starting in $v$ and $\delta^{-}(v)$ are the edges that are ending in $v$.
The flow value $v(x)$ is now defined as $v(x):=\sum_{e \in \delta^{+}(s)} x(e)-\sum_{e \in \delta^{-}(s)} x(e)$, or equivalently $v(x):=\sum_{e \in \delta^{+}(t)} x(e)-\sum_{e \in \delta^{-}(t)} x(e)$. My question is, why are those equivalent? My idea was to sum the equation in $(b)$ over all $v \in V - \{s,t\}$, giving $$0= \sum_{v \in V -\{s,t\}}(\sum_{e \in \delta^{+}(v)} x(e)-\sum_{e \in \delta^{-}(v)} x(e)) \iff v(x)=\sum_{v \in V -\{t\}}(\sum_{e \in \delta^{+}(v)} x(e)-\sum_{e \in \delta^{-}(v)} x(e)),$$ where I am using the first definition for $v(x)$. However, I don't know how to proceed, is this last expression equal to the other definition of $v(x)$?
Answer: You should sum over all vertices.
For simplicity, I adopt the notation $x(\delta^+(v))=\sum_{e\in \delta^+(v)}x(e)$ and analogously for $\delta^-(v)$ for any $v\in V$. We have
$$\sum_{v\in V}x(\delta^+(v))=\sum_{v\in V}x(\delta^-(v))$$
because every edge is ingoing exactly once and outgoing exactly once (i.e. every edge is counted exactly once on both sides).
Then essentially subtract what you have calculated:
$$\sum_{v\in V}x(\delta^+(v))-\sum_{v\neq s,t}(\delta^+(v))=\sum_{v\in V}x(\delta^-(v))-\sum_{v\neq s,t}(\delta^-(v))$$
This holds because $\sum_{v\neq s,t}(\delta^+(v))=\sum_{v\neq s,t}(\delta^-(v))$ by the second property. But this just means
$$\delta^+(s)+\delta^+(t)=\delta^-(s)+\delta^-(t)$$
which yields the result
$$\delta^+(s)-\delta^-(s)=\delta^-(t)-\delta^+(t)$$
by a simple arrangement.
Comment: Thank you for your answer. Could you explain where the first centered equality $\sum x(\delta^+(v))=\sum x(\delta^-(v))$ is coming from? I don't see why this should hold.
Comment: For every edge $e=(v,w)\in E$, the value $x(e)$ appears exactly once on the left hand side (in the sum $x(\delta^+(v))$) and exactly once on the right hand side (in the sum $x(\delta^-(w))$). These sums are just sums over edge flow values, so the equality must hold.
Comment: You should re-check your definition as it is not right. Take a path with unit flow, with endpoints $s$ and $t$ for example. Then $x(\delta^+(s))-x(\delta^-(s))=1$ but $x(\delta^+(t))-x(\delta^-(t))=-1$. It doesn't make sense for both source and sink to have more outgoing than ingoing (or vice versa), if you think about it.
Comment: You are right, which is what I just noticed. I will adapt the post. Thank you!


cpufreq directory not present. How to change governor for P states in such a case

Question: One node in my cluster is using ondemand governor which is specified in the directory:
/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ..

Scaling_governor allows us to choose the governor of our choice.
But this sub-directory is absent in the other node of the cluster.
How do I change the governor for Pstates in such case ?
Answer: Verify in your BIOS that you have something akin to "OS level power management" enabled.
Answer: We have to recompile the kernel in this case after choosing acpi Pstates driver in menuconfig


custom handle post error - duplicate entry

Question: Is it possible to handle a duplicate entry error in a post request so that I can return details of the existing entry rather then an error message?
Answer: Yes it is. You'll have to override the obj_create() definition. Here's an example
def obj_create(self, bundle, request=None, **kwargs):
    bundle = self.full_hydrate(bundle)
    attempt = Attempt.objects.filter(**kwargs)
    if attempt.count() < 1:
        bundle.obj = Attempt(*kwargs)
    else:
        logger.info("Updating attempt")
        bundle.obj = attempt[0]
    return bundle


Finding a Function That Approaches Another Function

Question: One of my math professors gave me the following challenge. It isn't graded, it's just for fun.
Consider the function:
\begin{equation*}
f_n(x)=x+3^3x^3+5^3x^5+...+(2n-1)^3x^{2n-1},~x \in (0, 1).
\end{equation*}
I want to find which of the following functions $f_n$ is getting close to as $n$ gets larger:
$ \displaystyle a)\frac{x(x+1)(x^4+22x^2+1)}{(x-1)^4(x+1)^4}$ $\displaystyle b) \frac{x(x^2+1)(x^4+22x^2+1)}{(x-1)^4(x+1)^4}$
$\displaystyle c) \frac{x^2(x+1)(x^4+22x^2+1)}{(x-1)^4(x+1)^4}$ $\displaystyle d) \frac{x^2(x^2+1)(x^4+22x^2+1)}{(x-1)^4(x+1)^4}$
Based on some tests i ran in mathematica by giving $n$ and $x$ values, it looks like $b)$ is the answer, but I am not sure. Can anyone confirm or deny this, and show how one might find the right answer, either with pen and paper or by using mathematica or maple or some other software?
Answer: Yes looks like it must be b)
Consider the following:
There is only one $x$ factor, so this eliminates choices c) and d).
The function is odd, i.e $f(-x) = -f(-x)$, so this eliminates a).
Thus b) must be the answer.
To find the answer by hand you can do the following:
Start with
$$f(x) = x + x^3 + x^5 + \cdots = \frac{x}{1-x^2} $$
Now differentiate
$$f'(x)  = 1 + 3x^2 + 5x^4 + \cdots $$
Multiply by $x$
$$f'(x)x = x + 3x^3 + 5x^5 + \cdots $$
Differentiate again
$$(f'(x)x)' = 1 + 3^2 x^2 + 5^2 x^4 + \cdots $$
Multiply by $x$, differentiate again and then multiply by $x$ to give the answer.
Comment: This is a nice solution!  To anyone checking this, you should know there's one small error: after the third differentiation, you need to multiply by x one more time.
Comment: @Michael: You are right. I will edit the answer. Thanks!
Answer: Yes, b).
In Maple, if you do
<code> map(factor, sum((2*i-1)^3*x^(2*i-1),i=1..n));
</code>
The result will be 2 terms, one which is shape $(x^2)^{(n+1)}*r(n,x)$ with $r(n,x)$ polynomial in $n$ and rational in $x$, and the second term is (exactly) (b).
What this is is actually writing
$$\sum_{i=1}^{n} (2i-1)^3x^{2i-1} = \sum_{i=1}^{\infty} (2i-1)^3x^{2i-1} - \sum_{i=N-1}^{\infty} (2i-1)^3x^{2i-1}$$
(and the term order gets flipped in Maple's output).  Both the resulting sums are 'easy' to do by hand.
Answer: Here's yet another way to verify that it's (b). Let Mathematica do partial fractions decomposition:
<code>Apart[x(x^2+1)(x^4+22x^2+1)/(x^2-1)^4]
</code>
This gives $\frac{1}{2 (x+1)}-\frac{7}{2(x+1)^2}+\frac{6}{(x+1)^3}-\frac{3}{(x+1)^4}+\frac{1}{2 (x-1)}+\frac{7}{2(x-1)^2}+\frac{6}{(x-1)^3}+\frac{3}{(x-1)^4}$, which can be expanded into power series using the geometric series and its derivatives.
Of course this is ugly, and requires you to know the answer in advance. Moron's answer is much better. ;-)
Answer: This one might be another way of approaching the problem:
$f_n$ is a polynomial, so it is the Taylor expansion of itself at every point; then, we can pick any point in $[0,1]$, say 0, and see what the Taylor expansions of your four possibilities are. In this case you'll see that $b)$ has a Taylor expansion at $x =0$ which is exactly the same as you $f_n$, so that's your answer.
On the other hand, since I did the calculations on my computer, I don't really know how messy it is to find the answer this way. (As Moron pointed out, also a quick check of the simmetry of the functions gives the right answer)


Prove that $d_{1}$ and $d_{2}$ induce the same topology

Question:
Suppose $d_{1}(x,y) = |x-y|$, $d_{2}(x, y) = |\phi(x) - \phi(y)|$, where $\phi(x) = \frac{x}{1 + |x|}$. Prove that $d_{1}$ and $d_{2}$ are metrics on $\mathbb{R}$ which induce the same topology.

It's easy to prove that $d_{1}$ and $d_{2}$ are metrics. Call the topologies induced by them $\tau_{1}$ and $\tau_{2}$. I can prove that $\tau_{1}$ is finer than $\tau_{2}$, but I can't prove the inverse. Can any body please help me. Thanks
Comment: Consider the map $$\phi \colon \mathbb{R} \to (-1,1);\quad \phi(x) = \frac{x}{1+\lvert x\rvert}.$$ Show it is a homeomorphism.
Comment: I can prove your map is a homeomorphism, and $d_{2}(x,y) = d(\phi(x), \phi(y))$, but then why $\tau_{2}$ is finer than $\tau_{1}$???
Answer: Let's break it down like this:

Given a metric space $(Y,d)$, a set $X$, and a bijection $f\colon X \to Y$, the pull-back of $d$ via $f$, $f^\ast d \colon X\times X \to [0,\infty);\; f^\ast d(a,b) = d(f(a),f(b))$ is a metric on $X$. Thus $f\colon (X,f^\ast d) \to (Y,d)$ is an isometry, in particular a homeomorphism.
If, in the situation above, $X$ carries a prior topology $\tau$, we have two topologies on $X$, the original $\tau$, and the one induced by the pull-back of $d$, let's call it $\tau_{f^\ast d}$. We have $\tau = \tau_{f^\ast d}$ if and only if $\operatorname{id} \colon (X,\tau) \to (X,\tau_{f^\ast d})$ is a homeomorphism. Since the composition of homeomorphisms is a homeomorphism, and we know that $f \colon (X,\tau_{f^\ast d}) \to (Y,d)$ is a homeomorphism, that is the case if and only if $f\circ \operatorname{id} \colon (X,\tau) \to (Y,d)$ is a homeomorphism.

It remains to identify $X$, $Y$, $f$, $d$, $\tau$, $\tau_{f^\ast d}$.
Comment: perfect. Thanks so much Daniel.
Answer: Or you can show that if $d_1(x_n,x)\to 0\iff d_2(x_n,x)\to 0$. It is more easy sometimes to use convergence,besides topology was invented for convergence. To give you a hint i'll show the more easy part ($\implies$).
Let $d_1(x_n,x)=|x_n-x|\to 0;$ then \begin{align}d_2(x_n,x)&=\frac {x_n}{1+|x_n|}-\frac {x}{1+|x|}\\&=\frac {(x_n-x)+(|x|x_n-|x_n|x)}{(1+|x_n|)(1+|x|)}.\end{align} We have that $|x_n-x|\to 0$ and $x_n\to x\implies |x_n|\to |x|$ and thus $d_2(x_n,x)\to 0.$
Comment: Can you explain why if $d_{1}(x_{n}, x) \rightarrow 0 <=> d_{2}(x_{n}, x) \rightarrow 0$, then 2 topologies are the same. This statement seems quite helpful in many case? Thanks
Comment: Yes $d1(x_n,x)→0<=>d2(x_n,x)→0$ which means that they produce the same open sets. to prove this,is not difficult but you need to write down some things( i cannot think any faster way),but think of it that they produce the same structure (this is algebraic term). Or each open set can be described equivalenlty by these two metrics.
Comment: I don't think it's right Dimitris. With your condition, I think we can only state that 2 topologies have the same Cauchy sequence, so if one is complete, then the other is complete, too. But how can they be the same?
Comment: No,it's quite right my friend:). Use it in these problems and you will remember me:)


Multiple Field , same ajax callback

Question: I created a custom field. The hook_field_widget_form has an submit element with an ajax callback:
  //Add more
  $element['add_zone'] = array(
    '#type' => 'submit',
    '#name' => $field_name . '-add_more-' .'name',
    '#value' => t('+'),
    '#limit_validation_errors' => array(),
    '#submit' => array('dms_polygone_add_more_add_one'),
    '#ajax' => array(
      'callback' => 'dms_polygone_add_more_callback',
      'wrapper' => 'zones-fieldset-wrapper-'. $field_name,
    ),
  );

I add 2 fields of my custom field type to a node. Ex: field_polygone (field type: polygone) and field_polygone_2 (field type: polygone).
When i press the 'add_more' button of my first field_polygone, my callback function 'dms_polygone_add_more_add_one' thinks the trigger element is field_polygone_2.
Callback function:
function dms_polygone_add_more_callback($form, $form_state) {
  //@todo: $i gebruiken voor veld als er meerdere waardes zijn toegestaan.
  $parents = $form_state['triggering_element']['#parents'];
  return $form[$parents[0]][$parents[1]][$parents[2]]['zones'];
}

$parents[0] is the field_name => When i press 'add_more' , the $parents[0] variable in the callback function should be 'field_polygone', but its field 'field_polygone_2'.
How can is solve this problem? Why does the callback function think the trigger element is field_polygone_2 instead of field_polygone.
Answer: I had a similar issue in Drupal 8. I had one form with multiple entities (not drupal entities) that had an identical subset of fields and each had their own button. My $form_state->getTriggeringElement() would always show the last of these buttons, no matter which I clicked. My issue was that I was not setting #name on the buttons and they were both defaulting to op (they had the same #name value). Explicitly setting a unique #name for each button resolved my issue.
Answer: The following link helped me to change 2 elements from a form through AJAX callback, whitout having to return the whole $form :
Is it possible to replace more than one form element (wrappers) triggered by only one #ajax trigger element?
Comment: This is not the answer of question.

Multiple Field , same ajax callback


CentOS54 server gone, no ssh, no ping, console hung. nothing in logs

Question: One of my nfs servers disappeared this morning from monitoring. I checked it out and the console was hung and non-responsive, and it was apparently crashed.
I power-cycled and checked out the syslog, and it very much appears to have just crashed with no indication of why.
Is there any kernel or debug settings that I can apply, to try and trap any future re-occurence or this problem. (or any recommendation on how to proceed)
Answer: If it completely hard-crashed, nothing in the logs, I'd strongly suspect it was hardware related. I'd reseat memory, check that the fans are running properly to cool the server, and if it's a server-grade system, use the diagnostics to check the equipment out (I know that Dell servers usually have a series of tests that can be run, but it depends on the model whether it's in BIOS or a startup partition or a bootable CD)
Rarely, rarely, rarely have I had Linux crash completely in an unresponsive manner without a kernel dump or something in the logs. I have had systems go nuts due to a dying controller, memory creeping, or something else hardware-related, which can easily do what you're describing.
Comment: yeah. I have rarely seen such a complete cessation of activity
Answer: Check your hardware, as Bart said. Also, sometimes an unresponsive machine may be on that state because of a stupidly large load. I saw some mailservers do that. Check your network, NFS can crash badly if the networks goes away when it is doing something.
If you ever need to do that to a machine again, remember the Magic Sysrq key and the Raising Elephants Is So Utterly Boring phrase. The ALT+SysRQ+ can do wonders on a linux box that is to all aspects dead. The prase is to remember the commands to use with ALT+SysRQ:
<code>R: take control of the keyboard
E: sends SIGTERM to all processes
I: sends SIGKILL to all processes
S: Sync (flush caches to disk, very important)
U: remount all FS read only
B: reboot!
</code>
Comment: thats really interesting, I didn't know about the sysrq key. I am looking forward to something crashing again so I can try it ;-)


How to redefine a message type for drupal_set_message?

Question: By default, there are 3 (status / warning and error). I will wish to add the info type, with a blue background for example. What to do?
Answer: The status is arbitrary, so:
drupal_set_message('A message', 'custom_status');

Will produce

A message

Notice the "custom_status" class, which you can target with CSS.


Invasion of lane in swimming

Question: If does it exists, how is regulated the invasion of lane in swimming?
It is allowed to "invade" the next lane during the stroke under water?
Answer: I'll go with the USA Swimming 2015 Rulebook (available on line at usaswimming.org under the Officials resources). These rules are in line with FINA international rules, so if not in the USA, your national Rulebook should still contain similar language.
USA Swimming rule 102.22.4 states:

A swimmer must start and finish the race in the same lane.

So, should they end up in a different land during the race, they must get back in to their lane for it to be a legal swim.
Furthermore, USA Swimming 102.22.6 states:

Obstructing or otherwise interfering with another swimmer shall disqualify the
  offender, subject to the discretion of the Referee.

So, if the lane you find yourself in has another swimmer in it, you can't get in their way. The 'discretion of the Referee' part is primarily there to give the Referee discretion as to who interfered with whom in less clear-cut cases, e.g. two backstrokers in adjacent lands tangling up with each other under the lane line.)
Now, suppose you ended up in the wrong lane, following the other swimmer - to this point you have not interfered with them (until they turn and come back your way). Or, suppose it was on a 50 long course race, where they won't come back your way. To avoid being disqualified under 102.22.6, you need to get back in to your lane, under the lane line, before you run into them. The trick is how to do that legally.
In breaststroke (USA Swimming rules 101.2) it would be fairly straightforward as part of the normal, legal stroke - your head goes under the lane line, you kick, and your head breaks the surface back in your lane.
In butterfly (USA Swimming rules 101.3) it is harder to see how - 101.3.2 includes the words

The swimmer must remain
  on the surface until the next turn or finish.

As an official, if a butterflier ducked under the lane line this would disqualify them.
In backstroke (USA Swimming rules 101.4), there is also wording (101.4.2) of

Some part of the swimmer must break the surface of
  the water throughout the race, except it is permissible for the swimmer to be completely
  submerged during the turn and for a distance of not more than 15 meters
  (16.4 yards) after the start and after each turn.

Freestyle (USA Swimming 101.5.2) has similar language to the backstroke.
Except perhaps for breaststroke, once you are swimming in the wrong lane, your only chance to get back in to your appropriate lane is on a turn, where the 15 meter rule applies.
Plausible scenario: On the start, within the 15 meter rule, you dolphin kick out in front of the adjacent swimmer, coming up well ahead. You realize your mistake. At the turn, you get back in to your lane, avoiding any contact with the other swimmer. However, at best, you are leaving it up to the Referee to determine whether or not your wake is interfering with the other swimmer (102.22.6). As an official I would call it in as a possible infraction, which would then lead to discussion with the Referee. Except under highly unusual circumstances, it would likely be a disqualification.
One of those unusual circumstances would be if there were no swimmer in the adjacent lane - then you have no possibility of interference, you would switch back to the correct lane on the turn, and life would go on. But then, would it really be an 'invasion'?

Invasion of lane in swimming


javascript innerhtml is not responding

Question: I am trying to capitalize everything i type inside the text-input the caps function on js works fine which i can see on console but the innerhtml function is not changing the html file

function clr() {
  document.getElementById("box").value = '';
}

function caps() {
  var str = document.getElementById("box").value;
  console.log(str);
  var res = str.toUpperCase();
  console.log(res);
  document.getElementById("box").innerHTML = res;
}
<!DOCTYPE html>

  <meta charset="utf-8">
  <title>UI</title>
  <link rel="stylesheet" href="css.css">

      <input id="box">

        <button onclick="clr()">Clear It</button>
        <button onclick="caps()">Capitalize</button>

here i can see caps function is working fine on console but not working in HTML file
Comment: inputs use value, not innerHTML
Comment: `box` is an input, it has a `value` not an `innerHTML`
Comment: It solves my problem thank you
Answer: input elements don't have an .innerHTML property. They have a .value property.
Only elements that have an opening and closing tag can contain any HTML within them, so only elements like:
, ,
, etc. can utilize .innerHTML.
You accessed the value properly in your clr function.

function clr() {
  document.getElementById("box").value = '';
}

function caps() {
  var str = document.getElementById("box").value;
  console.log(str);
  var res = str.toUpperCase();
  console.log(res);
  document.getElementById("box").value = res;
}

      <input id="box">

        <button onclick="clr()">Clear It</button>
        <button onclick="caps()">Capitalize</button>

Answer: For <input type="text">there is no innerHTML, they have the value attribute for that (you actually used it in the caps function).
This should work:

function clr() {
  document.getElementById("box").value = '';
}

function caps() {
  var str = document.getElementById("box").value;
  console.log(str);
  var res = str.toUpperCase();
  console.log(res);
  document.getElementById("box").value = res;
}
<!DOCTYPE html>

  <meta charset="utf-8">
  <title>UI</title>
  <link rel="stylesheet" href="css.css">

      <input id="box">

        <button onclick="clr()">Clear It</button>
        <button onclick="caps()">Capitalize</button>

javascript innerhtml is not responding


What is the easiest way to get into the groove of holding the guitar neck correctly?

Question: I've been playing guitar every since I was 9 yrs old and now I'm 14 and have found that the way i hold the guitar is giving me Tennis Elbow. What is the best way to get back to playing the guitar properly
Comment: See also: http://music.stackexchange.com/questions/40845/how-to-play-power-chords-without-fretting-hand-getting-tired/40867
Comment: Also see also: http://music.stackexchange.com/questions/44754/what-are-some-tips-for-battling-forearm-fatigue-for-performing-long-passages-of
Comment: @ToddWilcox   Very apropo info in your links.  ;-)
Comment: How were you holding the guitar?
Comment: I was holding it with my thumb draped over the top
Answer: Well since there is no "trick" to correcting wrong technique from one day to another (especially if you have been doing it for 5 years) I'd say this is going to be a matter of a lot of practice and concentration.
Every time you pick up the guitar and play you should actively focus on how you are holding it and only play with the correct technique from now on. This also applies to when you are in the middle of playing: If you catch yourself falling back to your old habit, immediately stop, hold the guitar correctly and then continue playing.
If you do this properly you will eventually get used to holding the guitar correctly and you won't have to concentrate on it as much anymore.
Note: As I said this eventually does not mean in a couple of days. The amount of time this takes entirely depends on your dedication and how much you practise your guitar. But even if it takes longer than expected you should never give up, since learning the correct technique does not only affect your playing but also your health - as you have already figured out yourself.
Comment: OK that helps alot, i don't ever plan on giving up guitar is the only thing that keeps me going


How to connect a file_field to the upload action in a rails app

Question: I'm trying to add to a very simple data entry form, a button that allows the user to upload a file to the servers file system so that the url to that file can be placed in a field of the database.
So far, I've worked out that in the _form...erb file, I can add the file_field helper:
<%= form_for(@business) do |f| %>
<% if @business.errors.any? %>

<%= pluralize(@business.errors.count, "error") %> prohibited this business from being saved:

<% @business.errors.full_messages.each do |msg| %>

<%= msg %>

<% end %>

<% end %>

<%= f.label :deletedFlag %>

<%= f.check_box :deletedFlag %>

<%= f.label :name %>

<%= f.text_field :name %>

...

<%= f.label :logoURL %>

<%= f.text_field :logoURL %>

    <%= f.file_field :imagefile %>

<%= f.label :streetAddress %>

<%= f.text_area :streetAddress %>

<%= f.submit %>

<% end %>

This gives me a form with a "Choose file" button.  Clicking on that allows the selection of the file.  Great.
Now, from the ruby guide at: http://guides.rubyonrails.org/form_helpers.html#uploading-files I'm told to add something like:
def upload
  uploaded_io = params[:business][:imagefile]
  File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'w') do |file|
    file.write(uploaded_io.read)
  end
end

to my controller.
What I haven't worked out is how that file_field gets connected to that 'upload' action.  I can see in the logs that the button press accepts the filename, etc, but there's no indication that the file upload is started or attempted.
I've read a number of references, and they all seem to say that you need these two things, but (for me) they don't connect the dots.  Yes I'm very new to Ruby/Rails, so I'm obviously missing something fundamental.
Comment: can you post your complete _form, model and controller file? It will make it easier to help you rather than guessing.
Comment: I've added the form almost entirely now.  Just left out some fields.
Answer: Did you forget to specify multipart in your form_for like this?
<%= form_for(@uploadfile, :html => {:multipart =>true}) do |f| %>

Check the generated form html code, do you see this?
<form ... enctype="multipart/form-data" ...>

Comment: I don't think I need it. I'm using rails 3.2, and the guide I linked to stated that.
Comment: @PKCLsoft You do need to specify multipart if you want a browser to upload a file.
Comment: In the guide it says you must specify `multipart: true`
Comment: But not for ruby 3.1+:  'Since Rails 3.1, forms rendered using form_for have their encoding set to multipart/form-data automatically once a file_field is used inside the block. Previous versions required you to set this explicitly.'
Answer: You probably need to setup a route so that Rails can pass the request from your form into your controller, then setting up your form to use that route:
# config/routes.rb
post '/upload' => 'mycontroller#upload'

# app/views/some_model/new.html.erb
<%= form_for something, url: upload_path do |f| %>
...

This is probably the least recommended way of handling a file upload on Rails, especially for a new user, though. You would be much better off setting up Carrierwave, Paperclip, or Dragonfly instead.
Comment: Thanks for that!  Yes I looked at Paperclip, but it seems heavy when all I wanted was a simple way to upload a file to a directory.  I don't want to add more to the database backing the site if I can help it (and I go the impression that Paperclip uses the db), and I don't want to overcomplicate it for the client (or myself for that matter).  The route is the sort of thing I was looking for, but if this is a bad way to go, can you link me to some explanation?  If I have to I'll go with a plugin/gem to do this, but I really want keep it simple.
Comment: @PKCLsoft A database is not heavy. It's better to track files uploaded rather than to have them littering a folder without any references to them from a structured store.
Comment: OK, that's a fair comment.  There would be a reference, but not as well managed.  Thanks.  I'll mark this as the answer and get on with doing it right.  Thanks.


Given two sets having the same cardinality what can we say about the cardinality when they are subtracted from another set

Question: Let $B$ and $C$ be two subsets of $A$ having the same cardinality and cardinality of $A$ is strictly greater than that of the sets $B$ and $C$ then can I conclude that $A \setminus B$ and $A\setminus C$ they both have the same cardinalities?
I constructed this question myself and have found no counterexample but I am also unable to prove it. Any help will the truly appreciated.
Comment: Are you only looking at finite cardinalities? If yes, you can use the fact that if $B \subset A$ and $|A|<\infty$, then $|A \setminus B|=|A|-|B|$. If you are considering infinite cardinalities, then this is false.
Comment: Yes, I am looking for infinite cardinalities. Can you provide one counterexample to prove it false?
Comment: Sorry I didnt realise the cardinality of $A$ is strictly greater than that of the sets $B$ and $C$. Not sure it is false anymore.
Answer: As explained in the comments, normal integer arithmetic proves this in the finite case. In the infinite case, we can use choice and the assumption that $A$ has strictly larger cardinality than $B$ or $C$ to prove it. This assumption on $A$ is critical, lest $A = B = \mathbb Z$ and $C = 2 \mathbb Z$ be a counterexample.
The application of choice here is that for two infinite cardinals $\lambda, \kappa$ we have $\lambda + \kappa = \max(\lambda, \kappa)$. We have $|A| = |A - B| + |B| = \max(|A - B|, |B|)$. As we assume $|B| < |A|$ we must therefore have $|A - B| = |A|$. Similarly, $|A - C| = |A|$, so $|A - B| = |A - C|$. We didn't even need the assumption that $|B| = |C|$.
Comment: I did not get this part, "The application of choice here is that for two infinite cardinals , we have +=max(,)." How do I prove that?
Comment: @Sayantan An equivalent way to say that is that for any $\kappa \leq \lambda$, $\kappa + \lambda = \lambda$. We certainly have $\lambda \leq \kappa + \lambda$. Furthermore, $\kappa + \lambda \leq \lambda + \lambda$. Do you know how to prove $\lambda + \lambda \leq \lambda$ for infinite cardinals? How do you do it for $\aleph_0$? I believe the proof should generalize using a well ordering on $\lambda$. I think transfinite induction on $\kappa$ could also work.


How to add fields wrapper in field preprocess

Question: My code:
function MYTHEME_preprocess_field(&$vars, $hook) {
  $element = $vars['element'];
  if($vars['element']['#field_name'] == 'field_example') {
    foreach($vars['items'] as $key => $item) {
      $vars['items'][$key]['#prefix'] = '
';
      $vars['items'][$key]['#suffix'] = '
' . $item['#item']['alt'] . '
';
      $vars['items'][$key]['#suffix'] .= '
';
    }
  }
}

I want to wrap all items in a div. I should use some code outside the foreach, but I hadn't found any solution. Can you help me?
Answer: Based on the question I'm assuming you are asking for D7 ...
You should use a "container" for this. Something like this:
$element = $vars['element'];
if($vars['element']['#field_name'] == 'field_example') {
  foreach($vars['items'] as $key => $item) {
    // Do whatever you want...
  }
  $vars['items']['#type'] = 'container';
  $vars['items']['#attributes'] = array(
    'id' => "some-id",
    'class' => array(
      'some-class',
    ),
  );
}

See this other question for more details. Also, if you want to get a little more fancy you can look at using theme wrappers.


SBS 2008 Backup Drive Full - Error Code '2147942512'

Question: We are using Windows Backup on SBS 2008 SP2 and backing up to 1TB external hard drives. Recently after switching drives our backup started failing because the backup drive is full and auto-delete isn't automatically deleting older backups/show copies. I'm trying to get more information to help me effectively prevent this problem from reoccurring in the future.
How I can tell that the drive is getting full:
In the event viewer under Windows Logs > Application, I'm seeing Event ID 517 but it fails to show an intelligible description. However, under Applications and Services Logs > Microsoft > Windows > Backup > Operational, I'm seeing an event with the ID of 5 and a description like this: Backup started at '10/4/2011 12:30:12 PM' failed with following error code '2147942512'.
One of the most informative posts I've found on this error is located on Microsoft's Technet Forums here. In that post, a Microsoft representative gives this hazy explanation:
auto-delete feature to ensure that at least some old backup copies are maintained on the disk -- does not automatically delete backups if space utilization by older copies is less than 1/8 of the disk size or in other words, 13% of the disk size. that means if the one full backup copy does not fit in the 7/8 of the disk size, backup may fail with disk full error. auto-delete will not automatically delete older versions to reclaim more older versions of backup.
In the above explanation, I do not understand what is meant by "older copies" except that it appears that anything older than the very last shadow copy would be considered "older copies". I'm going to make the assumption that this problem where auto-delete will not work will affect any hard drive that is large enough to make an effective backup drive, or in other words, any hard drive that is large enough to hold more than one backup/shadow copy at once.
The same MS representative proposes the solution of using a larger backup drive. I can't understand how this will help. It appears to me it will simply delay the problem until a later date.
In order to resolve this problem for now, I did the following:

Assign the backup drive a disk letter under disk management.
Run the command line with Administrative rights.
diskshadow.exe [enter]
delete shadows oldest x: [enter] (where X: is the letter you
assigned your backup drive)
I manually ran the above command some 60 or 80 times to free up about 200 GB of space on my 1 Terrabyte External Hard drive.

However, I do not feel this is a satisfactory solution to prevent the problem from happening again in the future. Does anyone have a solution to prevent your Windows Server backup drive from getting full?
Comment: For completeness and some searchability, 2147942512 = 0x80070070.
Answer: I found a good answer here.  You have change the "use limits" for shadow copies.
Comment: Welcome to Server Fault! Generally we like answers on the site to be able to stand on their own - Links are great, but if that link ever breaks the answer should have enough information to still be helpful. Please consider editing your answer to include more detail. See the [FAQ](http://www.serverfault.com/faq) for more info.