one sample t-test ${\rm H_0}:\mu=1$ vs ${\rm H_0}:\mu<1$

Question: Suppose I want to test
\begin{align}
&{\rm H_0}: \mu = 1,\notag\\
&{\rm H_A}: \mu < 1,
\end{align}
and I get a p-value of 0.3, meaning I don't reject the null for the alternative.
In this post
One sided test $H_0:\mu=0$ or $H_0:\mu\leq 0$?
can write this test this way:
\begin{align}
{\rm H_0}: \mu < 1,\notag\\
\end{align}
I think the p-value for this test is just 1 minus the p-value of the above test, so I get a p-value of $0.7$. However I am a little confused about the interpretation. A p-value of 0.7 (>0.05) means that we don't reject the null. But the null here is $\mu < 1$. It seems to me that this contradicts my previous conclusion.
Can anyone help me explain this? Thanks!
Comment: Thanks so much for your comment. I guess my first question is that what's the relationship between the p-value of test1 and test2? with test1 being H0: mu=1; Ha:mu<1 and test2 being H0: mu<=1; Ha:mu>1. Is It p-value for test1 = 1-p-value for test2?
Comment: Maybe $H_0:μ\ge1,$ but **not** $H_0:μ>1,$ The null hypothesis determines the null distribution of the test. So $H_0$ must have an $=$-sign in order precisely to specify the null distribution. // The rest of your question is not at all clear. If you are asking what sample size is necessary in order to reject $H_0:μ\ge1$ in favor of $H_a:μ<1$, then (a) you need to specify the exact value $μ_a<1$ (maybe you have $μ_a=0.5$ in mind), (b) the variance $σ^2$ of the normal population distribution, and (c) the power of the test (that is, the probability of rejection).
Comment: Also, be careful to distinguish between null hypothesis $H_0$ and _alternative_ hypothesis $H_a.$
Comment: You seem to be totally on the right track with this comment. See the Addendum to my Answer for an example.
Answer: In this post, we'll write down rejection regions associated with both tests in OP's question. We'll show that the rejection regions for both tests do not partition space: it's possible that the realized data is in neither rejection region, leading to no rejections at all.
The model
Suppose that $X_i \stackrel{\textrm{ind}}{\sim} \mathcal{N}(\mu, 1)$ are independent draws from a normal distribution with common mean and variance. For convenience, we'll assume that the variance is known and so we're working with a $Z$ test rather than a $t$ test.
Getting the rejection regions
In this section, we will write down the rejection regions for both of the tests defined below. The displayed equations are the only important parts. Notationally, we use "primes" on all quantities associated with the second test.
For the first hypothesis test
$\begin{align*}
 H_0: \mu = 1 \\
 H_1: \mu < 1
\end{align*}$
an optimal rejection region is given by $R=\{\bar{X} < c\}$ for some rejection cutoff $c$. Whenever the sample mean is small, we reject the null hypothesis $H_0$ and conclude that $\mu < 1$. If we wish the rejection region $R$ to have an $\alpha$ type I error rate, then we can choose the cutoff so that: $$R_\alpha = \{\bar{X} < 1+\frac{\Phi^{-1}(\alpha)}{\sqrt{n}}\},$$ where $\Phi$ is the standard normal distribution function. Note, we found this rejection region $R_\alpha$ by solving for $\mathbb{P}_{\mu=0} \left[ \bar{X} \in R_\alpha \right] = \alpha.$
For the second hypothesis test
$\begin{align*}
 H_0': \mu < 1 \\
 H_1': \mu = 1
\end{align*}$
an optimal rejection region is given by $R'=\{\bar{X} > c'\}$ for some rejection cutoff $c'$. Whenever the sample mean is large, we reject the null hypothesis $H_0'$ and conclude that $\mu = 1$. If we wish the rejection region $R'$ to have an $\alpha$ type I error rate, then we can choose the cutoff so that: $${R'}_\alpha = \{\bar{X} > 1-\frac{\Phi^{-1}(\alpha)}{\sqrt{n}}\},$$ where $\Phi$ is the standard normal distribution function. Note, we found this rejection region ${R'}_\alpha$ by solving for $\sup_{\mu < 1} \mathbb{P}_{\mu} \left[ \bar{X} \in {R'}_\alpha \right] = \alpha.$
Interpreting the rejection regions
Here's where we are now: by writing down the rejection regions, we've found the sample means which would lead us to reject either test. Now we will interpret the tests for a particular choice of the sample size $n$ and the chosen Type I error rate $\alpha$.
Suppose that $\alpha = 0.05$ and $n = 100$. Then $$R_{0.05} = \{\bar{X} < 0.8355\}$$ and $${R'}_{0.05} = \{ \bar{X} > 1.1645 \}.$$ Therefore, when the sample mean is smaller than $0.8355$, we would reject $H_0$ and conclude that $H_1$ is true, i.e. $\mu < 1$. Further, when the sample mean is larger than $1.1645$, we would reject $H_0'$ and conclude that $H_1'$ is true, i.e. $\mu=1$.
What happens if $\bar{X}$ is not "small" or "large"? In this case, neither $H_0$ nor $H_0'$ would be rejected, and we cannot conclude anything from these particular tests. One way to make some conclusion would be making $\alpha$ larger until one of the tests reject---then concluding the alternative of that test. In some sense, this is choosing the "most likely" parameter region. This would correspond to choosing the alternative of the test with the smallest $p$ value.
Comment: Excellent explanation!
Answer: Comment continued with sample size computations from Minitab
statistical software. Testing $H_0: \mu = 1$ vs. specific alternative
$H_a: \mu = 0.5,$ with data from a population with $\sigma^2 = 1.$
Power and Sample Size

1-Sample t Test

Testing mean = null (versus < null)
Calculating power for mean = null + difference
α = 0.05  Assumed standard deviation = 1

            Sample  Target
Difference    Size   Power  Actual Power
      -0.5      27    0.80      0.811832
      -0.5      36    0.90      0.902575
      -0.5      45    0.95      0.951240

So, for example, if you want the probability of rejection to be $0.9$ (power 90%) in these circumstances, you would need a sample of size $n = 36.$

Addendum per Comment: Suppose you have $n = 40$ observations from a population distributed as $\mathsf{Norm}(\mu = 10.2, \sigma = 1),$ as follows:
set.seed(121)
x = rnorm(40, 10.2, 1)
summary(x);  sd(x)
    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
   8.731   9.655  10.318  10.268  10.861  11.815
[1] 0.8135376

The stripchart below shows the $n=40$ observations. The dotted
red line is at $\mu_0 = 10$ and the solid black line is at
$\bar X = 10.268.$

R code for figure:
stripchart(x, pch="|")
 abline(v = 10, col="red", lwd=2, lty="dotted")
 abline(v = mean(x), lwd=2)

First, consider $H_0: \mu \le 10,$ vs $H_a: \mu > 10.$
Because $\bar X = 10.268,$ we may have evidence that $\bar X$
is significantly greater than $10,$ rejecting $H_0$ in favor
of $H_a.$  In R, a one-sample t test gives the following results:
t.test(x, mu = 10, alte = "greater")

        One Sample t-test

data:  x
t = 2.0809, df = 39, p-value = 0.02203
alternative hypothesis: true mean is greater than 10
95 percent confidence interval:
 10.05094      Inf
sample estimates:
mean of x
 10.26767

It turns out that $\bar X = 10.318$ is significantly larger
than $10$ at the 5% level of significance because the
P-value $= 0.02203 < 0.05 = 5\%.$
By contrast, for the same data, suppose we want to test
$H_0: \mu \ge 10,$ against $H_a: \mu < 10.$ Because $\bar X = 10.268,$ we have no evidence that $H_a < 10.$ If this is the
hypothesis we had planned to test before collecting data, we
might go ahead with the formality of a t test, but we already
know we cannot reject $H_0$ in favor of $H_a.$
t.test(x, mu = 10, alt="less")
        One Sample t-test

data:  x
t = 2.0809, df = 39, p-value = 0.978
alternative hypothesis: true mean is less than 10
95 percent confidence interval:
     -Inf 10.48439
sample estimates:
mean of x
10.26767

The P-value is greater than 5%, so we can't reject $H_0$ at the 5% level. In particular, as you suspected, the P-value of this test looking in the
"wrong" tail is $1$ minus the P-value for the previous t test (looking in the "correct" tail):  $0.022 + 0.978 = 1.$
Note: A two-sided test of $H_0: \mu=10$ against $H_a: \mu \ne 10$
has P=value $= 0.044 = 2(0.022),$ where $0.022$ is the P-value of
the first one-sided t test (the one looking in the "correct" tail).
t.test(x, mu=10)$p.val
[1] 0.04406042


Inverse of an algebraic function is irrational under a condition

Question: I've recently been looking to Function Theory/Real Analysis and I've encountered a bit of a problem I'm not entirely sure how to approach. This isn't from a textbook, rather something I have noticed that may be the case or may not be. Here's the problem:
Let $f: \mathbb{R} \to \mathbb{R}$ be an algebraic function with an existent inverse over $\mathbb{R}$. If $\alpha \in \mathbb{R}$ is such that $f(\alpha) \in \mathbb{Q}$, is $f^{-1}(\alpha) \not\in \mathbb{Q}$? Is the converse, inverse and contrapositive of this statement true?
EDIT: As pointed out, $f(x) = x$ is a counterexample to this, but since this is obvious, I'll elect to exclude this as a function to be considered.
I've been tempted to generalise everything and brute force from there, although I highly doubt that is the correct approach, let alone a reasonable way to approach this. I feel like I could be missing something really obvious here however I have not been able to even get a proper foothold. Any help or guidance would be greatly appreciated!
EDIT 2: Found a simple counterexample again, so this question isn't really that troubling anymore.
Answer: Focus on examples. Take the counterexample of $f(x) = x = f^{-1}(x)$.

For the forward direction, try $\alpha = 1$, which is rational.
For the converse, try $\alpha=\pi$, which is irrational.
For the inverse, try $\alpha=\pi$.
For the contrapositive, try $\alpha=1$.

Each of these give a counterexample to the relevant statement. Remember that, for the statement $P \implies Q$, we have

the converse being $Q \implies P$
the inverse being $\neg P \implies \neg Q$
the contrapositive being $\neg Q \implies \neg P$
Comment: Oh, I completely forgot about the identity function. That's a good point. I'll edit the question to exclude that function .
Comment: In what sense is the negation $\neg P \implies \neg Q$ (which is logically equivalent to the converse)? Surely the negation of $P \implies Q$ is simply $P \land \neg Q$.
Comment: I had some wires crossed, that's my bad. OP meant the inverse, and that's what $\neg P \implies \neg Q$ is.


What's this drum symbol (X with line in the middle without tail)

Question: Can someone please help me identify this drum symbol?

It's from the second bar here:
Comment: Could you maybe provide a complete example where it is used exactly that way?
Comment: Hi Matthias, I can post the full purcussion line where this occurs, e.g.: 4 bar stave.
Comment: Hmm, I looked for references but I didn't find the particular symbol anywhere. Perhaps it is just a crash cymbal, but it's strange because you would expect a normal x like you see it before. Is this from a song or an excercise. Where did you get it?
Comment: It comes directly from the 'Futurama Theme' which I purchased from sheet music plus - http://www.sheetmusicplus.com/title/futurama-theme-digital-sheet-music/20067830. It contains an audio representation of the sheet music, so you should be able to hear the questioned cymbal at the end.
Answer: I have never seen something like this before and couldn't find anything either, so my guess would be that it's a crash cymbal that lasts a whole note (semibreve). I'm guessing it's shaped like that to resemble the whole note.
Comment: I'd be inclined to agree you, with one note: I think the note is just an empty X without stem.  The line is the ledger line. You can see the note head here: https://music.stackexchange.com/questions/27453/different-note-head-uses-in-musescore
Answer: Some of the references say that its a Ride cymbal which is similar to the hi-hats which is played on the imaginary line above the measure. Actually the 'x' sits on the line and that is the reason it looks like a star.
Comment: Hi Shwetha, I'm trying to work out how long the length of the cymbal is. I presume it is the same length as a piano sembibreve, but I'm not sure.


Determine seasonal frequency from the values of the time series alone

Question: What are some algorithms for determining the seasonal frequency (or equivalently, the length of the seasonal period) from the values of the time series alone? That is, the values of the time series are given, but the time stamps are missing. However, we know the data has been sampled at equal time intervals.
(Further nuance could be added to the question, e.g. how this should be approached if our goal is to detect the "true" seasonal frequency vs. if our goal is to forecast the time series optimally under a given evaluation loss function. A motivating example for the latter case can be found here. A reference to an R implementation would be a bonus.)
Comment: I see your point, and I agree. I will post that answer here, because that paper does address the question, and I will also hope for a better and more comprehensive answer.
Comment: @SextusEmpiricus, that is exactly the thread that motivated my question. We can eyeball things, and we may get good at it with experience. However, do we have any sensible algorithms to yield answers in a principled way? (I bet we do, perhaps in the signal processing literature.)
Comment: I reopened [this thread](https://stats.stackexchange.com/q/500796/1352) and am kind of wondering whether this one is not simply a duplicate of that one. I would VTC, but then my gold-plated dupe hammer would be single-vote-enough. What do you think?
Comment: @StephanKolassa, I read the other thread before posting my question, so I do not think it is a duplicate; otherwise I would not have posted it. Johanna seems to be interested in testing for presence of seasonality, while I am interested in determining the seasonal period. These are two distinct problems. The paper you reference in your answer is relevant, but it does not go into any depth of the matter; I hope I can solicit better references.
Comment: *"but the time stamps are missing"* in what *way* is this relevant? Are we supposed to recover the time steps by figuring out daily, weekly, yearly patterns which have ratio's 1:7:365? Or is it relevant in the sense that we are not simply using the time stamps to figure out the patterns, like using default dayly/weekly/yearly patterns without observing the values and without figuring out whether it makes sense.
Comment: @SextusEmpiricus, a time series is a sequence of pairs (time, value). Let us say we do not see the time but we see the value. Additionally, we know the subsequent times have equal time intervals between them. I did not mean anything more complicated than that.
Comment: In this [question](https://stats.stackexchange.com/questions/609234/is-it-possible-to-have-seasonality-at-24-12-8-periods-in-hourly-based-wind-pow) the computation of a power spectrum worked well to extract components that indicate a seasonal period of 24 hrs. But other situations might be less clear so it may not always work.
Answer: (cross-posted from here)
Section 3.2 in the following paper offers a possibility for determining the length of the seasonal cycle:
 Wang, X, Smith, KA, Hyndman, RJ (2006) "Characteristic-based
 clustering for time series data", _Data Mining and Knowledge
 Discovery_, *13*(3), 335-364.

However, this is only one aspect in a paper that is more comprehensive in its aims, so the specific issue of determining a seasonal length is not treated at great depth.
Also note that this was never included in the forecast::auto.arima() function (whose author is Hyndman), although this function does use other methods from that paper (for instance, auto.arima() decides whether to apply seasonal differencing for known seasonal cycle length based on an estimate of seasonal strength as also given in Wang et al.).
I do not now why this was never included. It may have been because it was unstable, varying and hard to automate. After all, you need to identify peaks and troughs in the ACF, and what constitutes a "peak" or a "trough" in a noisy ACF series would need to be operationalized.
Alternatively, perhaps there simply never was any demand for it, since users presumably know their seasonal cycle length.
So if you want to use the cycle length determination per Wang et al., you would need to code it yourself.
Comment: +1, even if the treatment in the paper is very brief. Together with you, I will be looking for other references.


Reliability of research tool with Yes or No answer?

Question: I am doing research on school security. Questionnaire pertains to questions regarding availability or non availability of certain items so as to reach on conclusion that what is available and what is to be provided to complete the security package.
There are 57 questions and almost all are "yes" or "no" with few exceptions of answers as "not applicable" or some "range of amount in currency" like "less than $5000" and "Less than $ 10000". Now, how do I check the reliability. Someone said that since it is quantitative so "Cronbach's Alpha". But once I apply that it discard many questions and index is never more than 0.5. Please help and guide.
In addition, I have consulted few statisticians and they said that the questionnaire I have is just like a checklist which is conformation of the instructions given to schools by the department. So the answer is just yes and no. In this case reliability is not required to be checked.
Comment: At the same time I have another questionnaire in which I am asking the security experts that which item is immediately required, which is later required and which is not required. So they tick appropriate column in front of item name. What do I have to do with this to check reliability?
Comment: Afaq, it would help if you told us more about what type of person would answer your questionnaire. Are you hoping to give it to *security experts* in order to assemble a consensus on what schools should do to become more secure? Or do you already know what a complete 'security package' consists of, and plan to give the questionnaire to *school administrators* to find out what their security gaps are? Understanding your intent better would enable CV users to answer your question most helpfully.
Comment: First of all as per the state given policy, we already know what is to be provided. So I will go about in a following way: (1) I will ask school heads that what all they already have out of the list given by the state and also what funds they have got or not got. (2) Since Schools have very less equipment and less funds as well so I will ask few security specialists the priority of procurement to give a package for Minimum, Acceptable and Ideal....... Now I have a sample of almost 300 School Heads and 6 Security Specialists. I hope I have made it clear. If not you may ask more.
Comment: School Head questionnaires have two options i.e. Yes and No. In the questions where state of funds was asked they were asked about amount they have.
Comment: That does clarify your intent to some extent, thanks. But now I wonder what you mean by 'reliability'. Do you mean *correctness* of the answers? For example, are you looking for ways to detect that a school actually has a lockable gate, even when the questionnaire response is (incorrectly) given as 'NO'?
Comment: Questionnaires were Validated by the subject specialists. So after Pilot testing on 31 Respondents, data was entered in SPSS. To check the reliability of instrument I need to Run some test. Some one said that you have to run Cronbach's Alpha or Coefficient Alpha Test. I did that but since the answers are not in Likert  Scale so the the result was 0.5 and less. Then I asked few again and they said that you dont have to check the reliability as its purely yes / no answers. So is it correct that I should go for data collection without checking reliability?
Comment: Let me suggest that you review [reliability](https://en.wikipedia.org/wiki/Reliability_(statistics)) as discussed on Wikipedia. I am beginning to guess that your main concern is that there might be some standard set of 'reliability checks' that are part of 'good statistical practice', and you want to be sure you're not omitting them. Is that a fair way to state your concern?
Comment: Yes as my supervisor said that check the reliability of instrument. But My question is that in the case of the type of questionnaire I have mentioned above, is it necessary to check the reliability? I have visited each and every school and cross checked the answered so we cannot say that it will have some uniformity as each school is different than one.... In my opinion there is no requirement of reliability test of this type of questionnaire? What do you say?
Comment: I see you now have 3 very different answers to choose from. Please consider 'accepting' one of them by clicking the green check-mark on the left. On the other hand, if none of these answers helps, please consider editing your question to elicit an answer that will help you.
Answer: I would encourage you to maintain your focus on your scientific questions, and select your statistical techniques appropriately to those questions. Issues like the 'reliability' of questionnaire items should be explored with particular reference to your realistic concerns about the information they yield.
It sounds to me as if you believe your questionnaire items ask for straightforward factual information from competent people who are in a position to know the correct answers. But maybe your supervisor has thought of something you've missed; it would pay to ask. Unless you (or your supervisor) can tell a reasonable story about how one or more of the items might give unreliable information, then you have no basis for choosing any statistical technique for assessing 'reliability'.
The higher your ratio of thinking to statistics, the more useful your work is likely to be. There are few things that more clearly flag poor social science research than the desultory, mechanical application of 'obligatory' statistical tests (or other procedures) in the absence of genuine scientific motivation. As one example of where a low $thinking:statistics$ ratio can lead, consider this cautionary tale.
If your site visits showed that there were any items that were not answered as reliably as you hoped, and if you have questions about how to present or analyze your data about those specific reliability problems, then you may wish to ask a new CV question.
Comment: You might consider refining your question in that case, Afaq, so that it elicits answers that are more useful to you and directed toward your needs. One thing that might help is searching for related Q&A's here on CV. Seeing how some successfully answered questions were asked might help you rephrase. Also, consider doing some research on your own, and including links to what you've read in the text of your question.
Comment: I think you are getting my point. My questionnaire is based on a document and answers can only be yes or no. So I think if the answer is to be in this way like a fact that, "Is the wall 8 feet High", "Do you have an Armed Guard", "Do you get special grant for Security", "Do you have CCTV System". I suppose in such questions reliability check is not needed. Its descriptive not infrential. What do you say?
Comment: I'm willing to go out on a limb at this point, and guess that your supervisor was hoping that some statistical 'Validation Fairy' might flutter in to sprinkle magic 'validation dust' on your survey. The short answer is, *there is no Validation Fairy*. No Santa Claus, no Easter Bunny, no Validation Fairy. Please don't forget to 'accept' an answer if it has worked for you.
Comment: None of the answer has solved my query, so how can I select an answer.
Answer: There is no need to check the reliability of demographic type data like (range of amount etc..).
While You can check the reliability of rest of items. It looks like you have a dichotomous (Yes/No, True False) data. In this case, You can either use Cronbach Alpha or KR-20 test both will produce identical results.
And If Index is not more than .50, it does not mean that there is issue in process. There may be issues in items.
Firstly, check the demographics of all items (minimum and maximum values), check weather the range of each item's values are correct.
Secondly, check weather there is any item with negative wordings.
Thirdly, generate the correlation matrix of all items, You must have some very low or negative correlated items.
Lastly, If I were you I would closely observe each items and group them into categories (factor) then check the reliability of factor (subscale).
Answer: You might want to think about the possibility that your questionnaire is assessing different latent constructs. I suggest you factor analyze your data to determine what those constructs are. This would fall more in the realm of validity but you may want to consider this to see how well your items work together (broadly speaking, of course).
With regard to reliability- have you considered all the factors that influence a measure's reliability? For example... 1) test length, 2) time given to participants to complete the measure, 3) item difficulty or clarity, 4) intervals between tests (if you are looking at test-retest reliability, and 5) the environment in which your participants are answering your questionnaire. Something else to consider would be to examine how your participants' characteristics are influencing their responses. Participant homogeneity is another major factor that influences a measure's reliability.


Possible to remove item in adapter onBindViewHolder belong?

Question: Hi to all stackoverflow family.
I begin to learn android OS system for development some project and I can do it.
I have an example project and I'm fetching some data. Everything is okay but I want to do this if it's possible.
I'm using sqlite and I'm adding some items in. I want to remove if sqlite items will equal to arraylist onBindViewHolder belong.
My ArrayAdapter:
public void onBindViewHolder(@NonNull final UserViewHolder holder, final int i) {
    //Setting user profile pic with picasso
    holder.username.setText(users.get(i).getUsername());
    Picasso.with(holder.imageView.getContext())
            .load(users.get(i).getProfile_pic_url())
            .placeholder(R.drawable.ic_profile)
            .error(R.drawable.ic_profile)
            .into(holder.imageView);

    //Remove item if button cliked
    holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final int position = holder.getAdapterPosition();
            removeItem(position);
        }
    });
}

I tried this but it doesn't work:
public void onBindViewHolder(@NonNull final UserViewHolder holder, final int i) {

    //Removing user which in white list
    settings = holder.layout.getContext().getSharedPreferences("INSTAGRAM_UNFOLLOWERS", Context.MODE_PRIVATE);
    settings.edit();
    String current_user = settings.getString("username", "");

    DBWhiteHelper dbWhiteHelper = new DBWhiteHelper(holder.layout.getContext());
    if(users.get(i).getUsername().contains(dbWhiteHelper.WhiteList(current_user).get(i).getUsername())){
        users.remove(i);
        notifyDataSetChanged();
    }

    //Setting user profile pic with picasso
    holder.username.setText(users.get(i).getUsername());
    Picasso.with(holder.imageView.getContext())
            .load(users.get(i).getProfile_pic_url())
            .placeholder(R.drawable.ic_profile)
            .error(R.drawable.ic_profile)
            .into(holder.imageView);

    //Remove item if button clicked
    holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            final int position = holder.getAdapterPosition();
            removeItem(position);
        }
    });
}

I also tried many things from StackOverflow but nothing worked.
What can I do, anymore ideas?
Comment: https://stackoverflow.com/a/26310638/6925888
Answer: Use this method
public void removeItem(int pos) {
mDataset.remove(pos);
notifyItemRemoved(pos);
notifyItemRangeChanged(pos, users.size());
}

I think it will be helpful.
Comment: Thanks but I tired this gives error when I put onBindViewHolder belong.


Max value for compareTo() method

Question: I want to implement merge sort but I need in this case max value for guard. I tired use null as max value but I get null pointer exception.
<code> private static <T extends Comparable<? super T>> void merge(T[] A, int p, int q, int r) {

        T[] L = Arrays.copyOfRange(A, p, q + 1);
        T[] R = Arrays.copyOfRange(A, q, r + 1);

        L[L.length - 1] = null; //guard
        R[R.length - 1] = null; //guard

        int i = 0;
        int j = 0;
        // i+j < r - k
        for (int k = p; k < r; k++) {
            if (L[i].compareTo(R[j]) <= 0) {
                A[k] = L[i];
                i++;
            }
            else {
                A[k] = R[j];
                j++;
            }
        }
    }
</code>
so I defined
<code>Comparable<T> guard = new Comparable<T>(){

    @Override
    public int compareTo(T o) {
        return 1; //always max
    }};

    L[L.length - 1] = (T) guard;
    R[R.length - 1] = (T) guard;
</code>
but if I use it as guard instead of null I can rewrite the code but I get always ArrayStoreException
how to do in right way?
So question is:
  how to define object which is comparability with T type without knowing what is T.
Comment: "how to do in right way?" Well, the right way is to implement an algorithm which doesn't need such a guard.
Comment: I think you need to provide an SSCCE so people can run your code.
Answer: You cant use null, instead use -1 if you are sorting positive values or very large values
Instead of
<code>    L[L.length - 1] = null; //guard
    R[R.length - 1] = null; //guard
</code>
Use
<code>    L[L.length - 1] = -1; //guard
    R[R.length - 1] = -1; //guard
</code>
Comment: In case when I have a numbers I can do that but when I will have any type of object with implements Comparable interface?

I cannot put -1 in to T type.
Answer: Defining the comparator which always returns 1 is bad idea, it doesn't confirm to comparator requirements.
You always know where the guard is so you may check indices or you may check for null. So just add special logic which will bypass comparator if element is a guard.
Something like this:
<code> for (int k = p; k < r; k++) {
      if (R[j] == null || (L[i] != null && L[i].compareTo(R[j]) <= 0)) {
          A[k] = L[i];
          i++;
      }
      else {
          A[k] = R[j];
          j++;
      }
 }
</code>
Comment: This is not a comparator!
Comment: Way do you think that definition of object which is always bigger (compareTo returns 1) is a bad idea?
Comment: Because it breaks comparatTo contract. This should always be true `yourFakeComparator.compareTo(yourFakeComparator) == 0`.

Max value for compareTo() method


How can I upload multiple images all at once?

Question: Is there a way to upload multiple images all at the same time in Drupal 7?
Answer: See the Plupload integration module. From the project page:

Provides integration between for the Plupload widget to upload
  multiple files and Drupal. Plupload is a GPL licensed multiple file
  uploading tool that can present widgets in Flash, Gears, HTML 5,
  Silverlight, BrowserPlus, and HTML4 depending on the capabilities of
  the client computer.
Comment: plupload required node_gallery? http://drupal.org/project/node_gallery beacause node_gallery is not present for drupal 7!
Comment: the dev version can create a stand alone field.
Answer: You can use the Plup module.

This module integrates a modern Plupload library with file field in Drupal 7. It provides a field widget for image field that allow user to upload multiple images at once using Plupload library. It depends on Libraries module.
Comment: You need to use the [Plupload](http://www.plupload.com/ "Plupload") library for using the Plup module.
Answer: You may use the Multiupload Imagefield Widget module.

As an extension to the Multiupload Filefield Widget module, this module presents the same widget to Image fields.
Answer: For an Image field, you can set the number of images you wish the user to upload.

(Image module must be enabled)
Comment: No, i request a multiple upload but the images must uploading all together and then they must see in the gallery, for example if i use media gallery it's impossible uploading most of one image and that is a difficult thing if i must create a gallery with 300 images for example!! Thank you for now


Redirecting via 301 subdomain to URI/URL configuration error in NGINX

Question: Trying to get a simple subdomain redirect in NGINX to a URI/URL path. At this point I want to get nginx.somedomain.com to redirect permanently to nginx.somedomain.com/sample/redirection.html. The config below I have in the nginx.conf file but isn't working. I've restarted the server, but still no go. Specifically it errors out and shows this as invalid in the nginx.conf file. Where should this go and also is this even correct?
server {
  server_name  nginx.somedomain.com;
  rewrite ^(.*) http://nginx.somedomain.com/sample/redirection.html permanent;
}

Thanks for any assist!
Comment: Show full error message. And this config is useless, it willl create infinite loop,
Comment: Yeah, I found that out the hard way.  :-/  The solution as shown below however works fine, no validation error.
Answer: Follow best practices and use the return directive:
server {
        server_name nginx.somedomain.com;
        return 301 http://nginx.somedomain.com/sample/redirection.html;
}


size_t portability concept

Question: I was analyzing on how the size_t offers portability across platforms, in the reference http://www.embedded.com/electronics-blogs/programming-pointers/4026076/Why-size-t-matters. The following are some few points that I derived out of it.

size_t is an typedef of an unsigned integral type.
The datatype which  is used for type defining size_t ensures that, it is sufficient enough for accessing the size of the largest data object but not larger.

I am unable to understand what do they mean by the largest data object?  Also consider the following example that I took from that reference.
For an I16LP32 they claim the size of the largest data object can be 2^(32)-1. Hence, in that case if we use an unsigned int as a substitute of size_t in memcpy function. We are limiting the processor capability to 2^(16)-1.
So what determines the size of largest data object?
Answer: The maximum possible size of an object is determined by the memory map of the architecture that the C implementation targets (in principle, and/or other implementation details, but in practice that's what does it).
On a so-called "flat" memory model, which includes most systems in use today, this means that objects can be almost the size of the whole address space (resources allowing), and so you'd expect size_t to be the size of a pointer. If your address space is 32 bits, then since each byte of the object has a different address you clearly cannot have an object larger than 2^32-1 bytes (-1 because the null pointer value "uses up" one address, which is guaranteed not to be the address of any object).
On a segmented memory architecture, it could be that your C implementation can address 2^32 bytes of space, but that a single object is not permitted to span multiple 16-bit segments, and so in principle size_t could be a 16 bit type.
For that matter, a C implementer is permitted to place arbitrary limit on malloc, out out sheer mischief, that it will never return a block bigger than 2^24-1 bytes. With the same restriction in the compiler to prevent static or automatic objects of that size, size_t could then be a 24 bit type even if pointers are bigger.
You'd hope that no implementer would do this solely for the fun of it, but there might be practical reasons for that 24 bit limit to exist. For example if the implementation uses a maximum of 16MB RAM for dynamic allocations and a separate limit of 16MB for static objects, then no object could be bigger than 16MB but a pointer has to be at least 25 bits. Even in that case, I doubt that an implementer would bother making size_t a 24-bit type, it would most likely still be 32 bits. But the standard allows the implementer to choose what's best.
You say, "it is sufficient enough for accessing the size of the largest data object but not larger". It's not true that it must not be any larger. For example, if some technical restriction of the platform means that no object can be larger than 2^31-1 bytes, it doesn't follow that size_t must be a 31-bit type. It is permitted to be a 32-bit type, because all that's required is that it's large enough.
Answer: As size_t is used for memory measurements it needs to be same same size as a pointer.
32 bit pointers --> 32 bit unsigned int
64 bit pointers --> 64 bit unsigned int

size_t portability concept


IPv6 são vulneraveis a ataques DDoS?

Question: Existe como atacar um endereço IPV6 utilizando DDoS, isto é, inundar o trafego de um servidor que utiliza IPv6?
Exemplo: WebSite.com [xxxx.xxxx.xxx.xxx::x], gostaria de saber se existe alguma forma de derrubar websites que não possuem protocolo IPv4 somente IPv6.
Estava pensando em talvez utilizar somente IPv6 nos meus servidores em quesito de segurança.
Comment: E por que não seria? Não encontrou nada porque não são assuntos relacionados.
Comment: Se está online, está passivel de ataque, independente do protocolo utilizado.
Comment: Uma simples pesquisa no Google me retornou umas coisas interessantes, dá uma olhada: http://fenainfo.org.br/info_ler.php?id=40215
Comment: Não encontrei nada a respeito sobre DDoS em WebSites IPV6, então pensei que não tinha como :C
Comment: Sim, eu tambem encontrei isso, mas creio que se refira a rede interna [LAN]. e estou me referindo a WAN [IP Dedicado]. Pois já encontrei varias ferramentas para inundação ddos em redes IPv6 [Somente interna]
Comment: Não existe relação, sua aplicação vai continuar suscetível aos mesmos problemas uma vez que independente do protocolo de "identificação" o servidor persistem idêntico.
Answer: Sei que a pergunta é antiga, mas vamos lá!
IPv6 nada mais é do que o IPv4 com uma quantidade de 128 bits, em resumo.
Ele inclui letras, números e resolve nossa demanda atual por endereços de IPs.
Um servidor APENAS sob IPv6, pode diminuir um ataque DDoS, já que somente os IPv6 irão acessa-lo, mas não significa que seja imune.
Administro um servidor de hospedagem que hospeda alguns sites grandes e já recebemos ataques DDoS que incluíam tanto IPv4 quanto IPv6. Ter um ou outro desativado só vai diminuir a gama de IPs que acessariam seu servidor.
E não recomendo que você desative o IPv4, especialmente no Brasil, muitas operadoras ainda não aderiram o IPv6 em todos os estados ou atualizaram seus roteadores, a maioria dos acessos ainda são por IPv4.

IPv6 são vulneraveis a ataques DDoS?


Informação em várias tabelas ou apenas em uma?

Question: Tenho muita informação que preciso de guardar na base de dados.
Alguma dessa informação é algo muito idêntica:
Exemplo: hora1, hora2, hora3, ... hora10.
A minha dúvida é se devo guardar tudo na mesma tabela ou criar uma tabela para a informação e outra específica para guardar as horas, colocando uma foreign key da primeira tabela na tabela das horas.
Posteriormente criar uma vista com tudo.
O que será melhor?
Answer: Cria uma outra tabela para cadastrar as horas, já que pode se levar em conta que a quantidade de campos seria hora1 .. horaN, assim se num futuro você precisar criar outra "hora" será só adicionar uma linha..
E criar outra outra tabela fazendo a relação entre sua tabela a tabela de horas.
Por exemplo
<code>Tabela nome

Id |  Nome
01 | XXXXXX
02 | YYYYYY
03 | WWWWWW

Tabela horas

Id |  Hora
01 | Hora1
02 | Hora2
03 | Hora3

Tabela Relacao NomeHoras

Id | IdNome | IdHora | Valor
01 |   01   |   01   | '00:00'
02 |   01   |   02   | '01:00'
03 |   01   |   03   | '02:00'
04 |   02   |   01   | '03:00'
05 |   03   |   01   | '04:00'
</code>
Espero ter ajudado!


JAX-RS Client vs MicroProfile Rest Client

Question: Where is the difference between a JAX-RS Client implementation like Apache CXF and the MicroProfile Rest Client?
Answer: The different clients implement different standards. The JAX-RS Client is implemented according to JSR 370 https://www.jcp.org/en/jsr/detail?id=370 while the other one implements the MicroProfile specification http://microprofile.io/project/eclipse/microprofile-rest-client.


How to update Twig - does it need to be updated?

Question: Does Twig will be updated automatically? Or does an updated version comes with a new Drupal 8.x release? Or does Drupal just stuck on the current version?
I'm just wondering because Twig will be further developed. Thanks.
Answer: Drupal core may update that dependency if it complies with the Allowed changes during Drupal 8 release cycle documentation page. A library may be updated to a more recent version in a minor release of Drupal (such as 8.1.0 or 8.2.0). Any feature requests or bug reports should be discussed in the issue queue for Drupal core.
Composer dependencies do not update automatically, but are based on running the composer command on the composer.json (or lock file).
However a contrib module may add its own Composer dependencies, which could alter dependencies added by Drupal core outside of a release. Composer looks through the entire dependency graph to choose a version to install, and this might update Symfony or Twig or Behat for instance. You can learn more about how Composer deals with semantic versions, which is similar to npm (node.js).
It is probably not safe to update to an arbitrary version of Twig because that may break core or contrib, but it is possible to update to a compatible version without waiting for an update in Drupal core.


Uniqueness of solution of nonlinear PDE

Question: Let $\Omega \subseteq \mathbb{R}^n$ be bounded open set and $\partial \Omega \in C^1$. Consider the problem
$$  \begin{cases} -\Delta u =  \lambda u^2 (1-u ) \text{ in } \Omega, \\ u=0 \text{ on } \partial \Omega, \end{cases} $$ where $\lambda >0$ is a constant.
I want to show that $u \equiv 0 $ is the unique solution as $\lambda \to 0$. The hint of the exercise is to use Poincaré's Inequality.
My attempt:
Multiplying by $u$ in $-\Delta u =  \lambda u^2 (1-u )$, we have $-u\,\Delta u =  \lambda u^3 (1-u )$. Integrating over $\Omega$, using the Green's Identity and $u=0$ on $\partial \Omega$, we have: $$ - \int_{\Omega} \lambda u^3 (1-u ) \, dx= \int_\Omega u \, \Delta u \, dx = \int_\Omega |\nabla u |^2 \, dx.$$
Now by Poincaré's Inequality $$\int_\Omega |\nabla u |^2 \, dx \ge C \int_\Omega  u^2 \, dx,$$
for some contant $C$, so that
$$ C \int_\Omega u^2 \, dx \le - \lambda \int_{\Omega} u^3 (1-u ) \, dx.$$
It is not hard to show that $ 0 \le u(x) \le 1$ for all $ x \in \bar{\Omega}$.
Since $ 0 \le u(x) \le 1$ for all $ x \in \Omega$, we have $u^3(1-u) \ge 0$. So $$ 0 \le C \int_\Omega u^2 \, dx \le -\lambda \int_\Omega u^3 (1-u ) \, dx \le 0.$$ Therefore, the unique solution is $u \equiv 0$ when $\lambda \to 0$ since $u \equiv 1$ is not a solution for the problem.
I'm not sure it's right especially in the last step. That's right?
Thanks!
Comment: I think there is a problem with your equality $ \int_\Omega u \, \Delta u \, dx = \int_\Omega |\nabla u |^2 \, dx$, since normally (because we are integrating by part and saying u=0 on the boundary) it would be $- \int_\Omega u \, \Delta u \, dx = \int_\Omega |\nabla u |^2 \, dx$
Comment: There are a couple of issues. I think you want $-\Delta u = \lambda u^2(1-u)$ instead of $\Delta u = \lambda u^2(1-u)$. If $\Delta u = \lambda u^2(1-u)$ and $0\leqslant u \leqslant 1$ then the maximum principle along with your boundary condition imply that $u=0$ which is what you want to prove.
Comment: @JackT, of course is $-\Delta u = \lambda u^2(1-u)$.
Comment: To conclude from $0 \le C \int_\Omega u_{\lambda}^2 \, dx \le \lambda \int_\Omega u_{\lambda}^3 (1-u_{\lambda} ) \, dx$, think you just need to use Lp inclusion (since $\Omega$ is bounded) to say that $0 \le C \int_\Omega u_\lambda^2 \, dx \le \lambda C'\int_\Omega u_\lambda^2 \, dx .$, and conclude that when $\lambda C'0,\forall w \in L^2(\Omega), \int_\Omega w^3 \, dx \le C'\int_\Omega w^2 \, dx .$$ and   $$\exists C''>0,\forall w \in L^2(\Omega), \int_\Omega w^4 \, dx \le C''\int_\Omega w^2 \, dx .$$ see [https://en.wikipedia.org/wiki/Lp_space#Embeddings]
Comment: @pFRAUX, Thanks! I understand now, but I didn't know these inequalities or if I've ever seen them.
Comment: I think i did something wrong, since i forgot to take the power in the inequalities... sorry about that. It should be $$\exists C'>0,\forall w \in L^2(\Omega), (\int_\Omega abs(w)^3 \, dx)^{\frac{1}{3}} \le C'(\int_\Omega w^2 \, dx )^{\frac{1}{2}}.$$ and i am not sure if the argument stil hold
Comment: @pFRAUX is there an easier way to solve this question?
Comment: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/127540/discussion-between-p-fraux-and-mary-e).
Answer: Ok, I will sums up what we said in comment and discussion here
Let u be a solution for some $\lambda$.
So we multiply by $u$ in $-\Delta u =  \lambda u^2 (1-u )$, and get  by integrating over $\Omega$, using the Green's Identity and =0 on $\partial\Omega$, we have:
$$\int_{\Omega} \lambda u^3 (1-u ) \, dx= -\int_\Omega u \, \Delta u \, dx = \int_\Omega |\nabla u |^2 \, dx.$$
Now by Poincaré's Inequality, there is some constant C>0 such that :
$$\int_\Omega |\nabla u |^2 \, dx \ge C \int_\Omega  u^2 \, dx,$$
So here we have that :
$$C \int_\Omega u^2 \, dx \le  \lambda \int_{\Omega} u^3 (1-u ) \, dx \le \lambda (\int_{\Omega}u^3\, dx-\int_{\Omega}u^4\, dx). $$
Finally, $u(x) \le 1$ follows analysing the sign of $\Delta u (x_0)$ for $x_0$ such that $u(x_0) > 1$ assuming (WLOG) that $x_0$ is a maximum of $u$.
So by integrating the relation $u^3 \le u^2$, and using that $u^4\geq0$ we finally get that
$$C \int_\Omega u^2 \, dx \le \lambda (\int_{\Omega}u^3\, dx-\int_{\Omega}u^4\, dx).\le \lambda \int_{\Omega}u^2\, dx $$
So for $\lambda<C$, such an u must have an $L^2$ norm nul, and so must be the null fonction $0$.

Uniqueness of solution of nonlinear PDE


Finding the odds of games in the past

Question: I'm interested in finding the odds (aka spread or latest line) of NFL games that were played in the past, as far back as the 1960's.  In general, the odds that were printed in newspapers.
Where can I find this information?
I am specifically not looking for gambling websites.
Comment: I don't understand your requirement about gambling websites. Wouldn't those be the most likely sources of such information?
Comment: @MichaelMyers Maybe I'll strike that.  Questions about gambling and fantasy football can easily wander into off-topic territory, and I'm trying to stay on-topic.  That said, almost all gambling websites that I've found hide or delete their line information as soon as the game ends.  But there were odds on games long before the internet was really used, before these gambling websites existed, and that information is just really hard to find.
Answer: If you are looking for odds that were printed in newspapers, you already know where to look... newspapers.  Take a trip down to your local library, where they probably have 50-100 years of newspaper archives on microfilm.
Comment: http://en.wikipedia.org/wiki/Wikipedia:List_of_online_newspaper_archives
Comment: Are any newspaper microfilms on the internet?  I thought Google was trying to do something like that but none of the publishers were cooperating.

Finding the odds of games in the past


Path breadrumb crumb displaying just first letter of title

Question: I am using the module path breadcrumb, is it possible to create a crumb which displays just the first letter of the title?
Comment: I think there is no way. But custom breadcrumbs module have the php option of setting breadcrumb path and title. https://drupal.org/project/custom_breadcrumbs
Answer: Maybe it helps someone so I will respond my own question: It resulted to be very easy using the module custom tokens with the following code:
<code>    <?php
$node = node_load(arg(1));
$title = $node->title;
$rest = substr($title, 0, 1);
return $rest;
?>
</code>


Existence of a strictly decreasing sequence

Question: If $S$ is an uncountable subset of $\mathbb{R},$ then does there always exists a strictly decreasing sequence $ (s_n) \in S,$ such that it converges to some point of $\mathbb{R}$
I divided $\mathbb{R}$ into union of closed sets $[n,n+1]   n\in  \mathbb{N}$ and atleast one such interval must have uncountable number of points. Hence it has a limit point, now how do I show the existence of a strictly decreasing sequence that converges to some point of $\mathbb{R}$?
Answer: Consider the set of points $s \in S$ such that $(s,s+\epsilon) \cap S$ is empty for some $\epsilon >0$. For each such $s$ we can consider a maximal interval $(s,s+\epsilon)$ with this property. These intervals are pairwise disjoint.  It follows that the set of such points is at most countable. Now remove these ponits from $S$. Call this uncountable set $T$. There exists $n$ such that $T \cap [n,n+1]$ is uncountable. Now consider the infimum of the set $T \cap [n,n+1]$. Can you see that there is  sequence of distinct points of $T$ which is decreasing to the infimum?
Comment: Yes Sir, I can complete it now


Does the product of two constants get computed every time it is executed?

Question: For example, if I have:
if(x < 2*0.025) { ... }
Does the 2*0.025 get computed every time? Or does a 0.05 get substituted in so that the multiplication operation doesn't have to run every time?
In other words, is it more efficient to use 0.05 instead of 2*0.025?
Answer: Every compiler I know implements constant folding, i.e. calculates constant expressions at compile time, so there is no difference. The standard, however, does not mandate it:

A constant expression can be evaluated during translation rather than runtime, and accordingly may be used in any place that a constant may be.

You can explicitly disable this optimization with some compilers. For example, -frounding-math disables constant folding for floating point expressions in gcc.
Answer: Constant expressions are precomputed.


Sort related nodes in block views based on a score calculated with common vocabulary

Question: Is it possible in drupal8 with Views to show related nodes in a block based on a score calculated with common vocabulary (each common tag adds some  affinity points depending on vocabulary type).
The sorting in views is ordered by:
1- Editor manual selection.
2- Best related nodes ordered by max affinity with the current node
3- Nodes sorted by date
Regards,
Thanks for your help
Answer: To place the php code to calculate the score there are several approaches:

Add an extra field for the score to your content type and calculate it in submit or
presave.
Add a custom views field plugin:
http://enzolutions.com/articles/2015/10/11/how-to-include-php-code-in-drupal-8-views/ or a custom sort plugin: https://chromatichq.com/blog/create-custom-views-sort-plugin-drupal-8
Use the module Computed Field.

With (1) you save the value in the database. This is best for calculations that don't change after saving the content.
A custom views plugin (2) is more dynamic and needs no space in the database, but it has to be recalculated each time.
The extra module (3) can do both, but it is not ready for production, still in alpha. If you read the linked how-to for views field plugins in (2) there are some good points that in D8 plugins are often more powerful than contrib modules to extend Drupal with custom php code.
Comment: Can you please help with more details about how to deal with the relation between the current node and the 'affinity score' to every other nodes.
Comment: I don't know exactly how you calculate the affinity score, but you can use as example the module https://www.drupal.org/project/similarterms. This is using both views plugins mentioned, a custom views field and a custom views sort.
Comment: Well, the score is the total sum of points calculated by giving 10 points to a common tag in a certain vocabulary and 20 points to another one.
Comment: The score is the total sum of points calculated by adding 10 points to each common tag in a  vocabulary and 20 points and 30 points to the 2 others vocabulary.

Also with similarterms. i get "No query was run" when Contextual Filter is added.
Any idea why ?
Thanks
Comment: That custom sort plugin in (2) uses a DB field - does anyone have one that doesn't? e.g I've some code that loops through a load of comments and does some computational logic on them to generate a score, but it's also unique to each user, so storing it as an extra field wouldn't be practical.


Frobenius norm of a product of Gaussian matrices

Question: Suppose $$C_n=X_1 X_2\cdots X_n$$ where $X_i$ is $d\times d$ matrix with IID entries sampled with normal centered at 0 and variance $1/d$.
The following appears to be true for large $d$, why?
$$\|C_n C_n^T\|_F^2\approx d(n+1)$$
Here are some numbers from a 4 samples with $d=1000$
$$
\begin{array}{c|ccccc}
  & \text{n} & \text{sample1} & \text{sample2} & \text{sample3} & \text{sample4} \\
\hline
  & 1 & 2003.99 & 1998.66 & 1999.51 & 1998.14 \\
  & 2 & 3029.97 & 2990.12 & 3008.21 & 2999.13 \\
  & 3 & 3967.81 & 3995.46 & 4022.33 & 4005.2 \\
  & 4 & 5027.41 & 5075.39 & 4941.94 & 5057.4 \\
  & 5 & 6143.21 & 5964.35 & 5844.76 & 6015.08 \\
\end{array}
$$
Notebook
It also appears to hold if I use the same matrix for all $X_i$, ie, $$\|X^n (X^T)^n\|^2_F \approx d(n+1)$$
Dividing entries of table by $d$ below we get near perfect agreement in table below with $d=4000$ using either $X^n$ (fixed) or $C_n$ (resampled)

(crossposted on stats.SE)
Comment: In the second image,  you need to change $s$ to $n$, and to replace $n=4000$ with $d=4000$.
Comment: I do not understand what you mean by " the random variable$ \|CC^T\|^2$ is equivalent to $d(n+1)"$ Do you mean $ \|CC^T\|^2/n$  converges almost surely to $d?$
Comment: @GérardLetac I meant that numerically this gives a very good approximation, possibly because there's a.s. convergence, hence I'm looking for a simple way to explain this
Comment: Surprised it doesn't grow exponentially. After all, it is a Lyapunov exponent. Maybe the Lyapunov is precisely zero?
Comment: @user619894 Formula 1.3 of Forrester [paper](https://arxiv.org/pdf/1206.2001.pdf) gives Lyapunov exponent and it's positive for these matrices
Comment: @user619894 ok found the discrepancy, it's an issue of normalization -- in my experiments I take Gaussian $d\times d$ matrices and dividing by square root of $d$. Any deviation from this normalization makes the norm either explode or decay
Answer: First I will give a rigorous proof for the case $n=1$.
For convenience $\|C_n^\mathsf TC_n\|^2_F$ instead of $\|C_nC_n^\mathsf T\|_F^2$ will be calculated. This is obviously equivalent. Suppose $\mathrm X_1=(\mathbf x_1,\dots,\mathbf x_d)$, then $\mathbf x_i\sim N_d(\mathbf 0,\frac1dI_d)$. The element of $\mathrm{X}_1^\mathsf T\mathrm X_1$ in $(i,j)$ is $\mathbf x_i^\mathsf T\mathbf x_j$.
For $i=j$,
$$
\mathbb E[(\mathbf x_i^\mathsf T\mathbf x_i)^2]=\frac1{d^2}\mathbb E[((\sqrt d\mathbf x_i)^\mathsf T(\sqrt d\mathbf x_i))^2]=\frac1{d^2}(2d+d^2)=1+\frac2d.
$$
For $i\neq j$,
$$
\begin{aligned}\mathbb E[(\mathbf x_i^\mathsf T\mathbf x_j)^2]&=\mathbb E[\mathrm{tr}(\mathbf x_j\mathbf x_j^\mathsf T\mathbf x_i\mathbf x_i^\mathsf T)]=\mathrm{tr}(\mathbb E[\mathbf x_j\mathbf x_j^\mathsf T]\mathbb E[\mathbf x_i\mathbf x_i^\mathsf T])\\&=\mathrm{tr}\Big(\frac1{d^2}I_d\Big)=\frac1d.\end{aligned}
$$
Hence
$$
\mathbb E[\|\mathrm X_1^\mathsf T\mathrm X_1\|_{\mathrm F}^2]=d\Big(1+\frac2d\Big)+(d^2-d)\frac1d=2d+1.
$$

For the case $n>1$, here only some approximation is given. Note that if $n$ is large the approximation may be bad since the approximation of the case $n+1$ is based on that of $n$.
For simplicity only $\|\mathrm C_2^\mathsf T\mathrm C_2\|_\mathrm F^2$ will be calculated based on $\mathrm C_1=\mathrm X_2$. The $(i,j)$-th element of $\mathrm C_2^\mathsf T\mathrm C_2$ is $\mathbf x_i^\mathsf T\mathrm C_1^\mathsf T\mathrm C_1\mathbf x_j$. Denote the $(p,q)$-th element of $\mathrm C_1^\mathsf T\mathrm C_1$ as $\mathrm c_{pq}$, then the $\mathbf x_i^\mathsf T\mathrm C_1^\mathsf T\mathrm C_1\mathbf x_j=\sum_{p,q}\mathrm x_{ip}\mathrm c_{pq}\mathrm x_{jq}$. For $i=j$, it is the diagonal element, and its approximation (by taking expectation) is
$$
\mathbb E[\mathrm {tr}(\mathbf x_i^\mathsf T\mathrm C_1^\mathsf T\mathrm C_1\mathbf x_i)]=\mathrm{tr}(\mathbb E[\mathbf x_i\mathbf x_i^\mathsf T]\mathbb E[\mathrm C_1^\mathsf T\mathrm C_1])=\frac1d\mathbb E[\mathrm {tr}(\mathrm C_1^\mathsf T\mathrm C_1)]=1.
$$
Now we consider the non-diagonal element, i.e., $i\neq j$. Taking square and expectation, we obtain
$$
\begin{aligned}\mathbb E\Big(\sum_{p,q}\mathrm x_{ip}\mathrm c_{pq}\mathrm x_{jq}\Big)^2&=\mathbb E\Big[\sum_{p,q,r,s}\mathrm x_{ip}\mathrm x_{jq}\mathrm x_{ir}\mathrm x_{js}\mathrm c_{pq}\mathrm c_{rs} \Big]\\&\stackrel{(?)}=\sum_{p,q}\mathbb E[(\mathrm x_{ip}\mathrm x_{jq})^2]\mathbb E[\mathrm c_{pq}^2]\\&=\sum_{p=1}^d\mathbb E[(\mathrm x_{ip}\mathrm x_{jp})^2]\mathbb E[\mathrm c_{pp}^2]+\sum_{p\neq q}\mathbb E[(\mathrm x_{ip}\mathrm x_{jq})^2]\mathbb E[\mathrm c_{pq}^2]\\&\approx d\cdot\frac1{d^2}\cdot1+(d^2-d)\cdot\frac1{d^2}\cdot\frac1d\\&\approx\frac2d.\end{aligned}
$$
Note I haven't check the second equality carefully. Thus $\|\mathrm C_2^\mathsf T\mathrm C_2\|_\mathrm F^2\approx d+(d^2-d)\frac2d\approx 3d$.
The approximation is based on: The diagonal elements of $\mathrm C^\mathsf T\mathrm C$ are about $1$, non-diagonal elements have zero mean but have $n/d$ squared expectation. And the above analysis indicates that these remains true as $n$ increases. Thus, for any $n$, the squared norm has approximation $(n+1)d$.
Answer: I was working on something similar to the (seemingly correct) answer you got on stats.SE. My approach was basically identical, with the difference that I was using the fact that
$$
||M||^2_{F} = Tr(MM^T)
$$
But the calculations are even more tedious in that case.

EDIT: I think that combining this property of the Frobenius norm with the answer you got to this question you should be able to obtain the result.


Why do we get particular solution when solving non homogeneous differential equation and not general solution

Question: What I don't understand is that why can't we find the general solution of non homogeneous differential equation from the non homogeneous one itself. Currently we use the homogeneous equation also.
Why isn't it that general solution is not available from the non-homogeneous equation itself?
Comment: Why do you want this?  If you have two linear ODE's that have the same homogeneous part and different forcing functions, isn't it better to be able to reuse the solution of the homogeneous equation?
Comment: Because there would be no way to find it without using the homogeneous equation first. Any particular solution of a **linear** inhomogeneous ODE added to the general solution of its homogeneous part gives the general solution to the original equation. It's how it works
Comment: In principle we can try to directly solve the inhomogeneous ODE, for example by a series method, but it would need to be done from scratch for each equation
Comment: Note that you should mention in the question that this method only works for linear ODEs
Answer: The whole point is that if $x_1$ and $x_2$ solve $L(x)=y$, where $L$ is a linear differential operator, then $$0=y-y=L(x_1)-L(x_2)=L(x_1-x_2)$$ so $x_1$ and $x_2$ differ by a homogeneous solution. Note that linearity is crucial here.
(You seem to think the inhomogeneous equation has a unique particular solution. But this is not true. When you solve an inhomogeneous equation, you find a particular solution of the infinitely many that are available. The other solutions to the equation differ from the one you find by a homogeneous solution, that is, by an element of the kernel of the linear operator.)
Comment: I would add to this that from the practical standpoint, knowing the homogeneous solution, we can use it to find a particular solution for the inhomogeneous equation as well. Which is generally better than trying to guess
Comment: Sure, and agreed (e.g. variation of parameters), but that's not what the OP asked. The general solution of any linear equation arises from translates of the kernel of the homogeneous equation.
Comment: @symplectomorphic I still don't understand why it is so that the solution is particular solution + general solution of homo. Is it some fundamental nature of diff equations that we stumbled upon?
Comment: Also what I am asking is that why is it that we get a particular solution and not a general solution when we solve?
Comment: It is not the fundamental nature of differential equations, but rather the fundamental nature of **linear** equations, whether differential or not. The idea doesn't work for nonlinear differential equations. The answer to your second question depends on what you mean by "when we solve." There are a variety of methods for finding *one* solution to an inhomogeneous equation, but as my answer shows, there are really infinitely many answers: once you find *one* particular solution, you get others by adding a homogeneous solution.
Comment: By analogy, think about how you would solve the equation $$\begin{pmatrix} 1 & 0 \\ 0 & 0\end{pmatrix}\mathbf{x}=\begin{pmatrix}3\\ 0\end{pmatrix}$$
Comment: @symplectomorphic What if we add the arbitrary constant when doing integrations using differential operators for finding particular solution. Wouldn't we get a general solution then?
Answer: What you are observing here is a fundamental principle valid in the "linear world". You are given an equation or system of equations
$$Ax=b\ ,\tag{1}$$
whereby $A$ operates linearly on the input vector $x$, and $b$ is a given constant vector. Such an equation may have no solutions. If it has solutions then they are of the form
$$x=y_{\rm hom} +x_p\ ,$$
whereby $x_p$ is a particular solution of the original equation $(1)$ (maybe found by guessing), and $y_{\rm hom}$ is the general solution of the associated homogeneous equation
$$Ay=0\ .\tag{2}$$
Note that the set of solutions of $(2)$ is a vector space, which means that any linear combination of "special solutions" of $(2)$ found by "special methods" is again a solution of $(2)$. One last thing: If the RHS of $(1)$ is the sum of two "simpler" vectors: $b=b_1+b_2$, and we can find "particular solutions" $x_p^{(1)}$, $x_p^{(2)}$ of $(1)$ for these "simpler" RHSs then $x_p=x_p^{(1)}+x_p^{(2)}$ is a solution of $(1)$ for the given $b$.
What I have described here applies to  inhomogeneous linear systems of ordinary equations, to inhomogeneous linear systems of ODEs, and to inhomogeneous linear PDEs with homogeneous and inhomogeneous boundary conditions. It is a truly fundamental principle: The set of all solutions of $(1)$ is an affine space, i.e., a linear space of functions $V$, albeit translated away from the origin by some (not uniquely defined) vector $x_p$.
Comment: So what you are saying is that this is a fundamental nature of every linear equation that difference of two solution is a solution of homogeneous equation.
Comment: Also what I am asking is that why is it that we get a particular solution and not a general solution when we solve?


Reference for cross-validation and Lasso

Question: Is there a main reference/publication suggesting to use cross-validation in order to choose the regularization parameter in Lasso?
Comment: The [original Lasso paper](http://statweb.stanford.edu/~tibs/lasso/lasso.pdf) by Tibshirani discusses using cross-validation to choose the regularisation parameter in Section 4.
Answer: You can try sec. 6.2.3 of James/Witten/Hastie/Tibshirani, Introduction to Statistical Learning

...
  implementing ridge regression and the lasso requires a method for selecting
  a value for the tuning parameter λ ... Cross-validation provides a simple way to tackle this problem. We choose a grid of λ values, and compute
  the cross-validation error for each value of λ ... We
  then select the tuning parameter value for which the cross-validation error
  is smallest. (p. 227)

The canonical text for Lasso is probably Hastie/Tibshirani/Friedman, Elements of Statistical Learning, but ISLR is a good place to start for non-PhD level.


Query para pegar informações de uma chave estrangeira

Question: Eu possuo uma tabela que faz relacionamento entre duas chaves estrangeiras

Gostaria de fazer uma query onde iria fazer uma verificação do fk_user_1 e pegar as informações do fk_user_2
Fiz essa QUERY:
SELECT users.username, users.name, users.user_id FROM friendship AS f INNER JOIN users WHERE f.fk_user_1 = 1
Porém esta retorna dados duplicados de USER e traz também o USER de chave estrangeira 1, que foi o que foi feito a comparação fk_user_1 = 1
Answer: Conseguir pegar somente as informações do fk_user_2 sem estar duplicado os dados com essa QUERY:
SELECT users.username, users.name, users.user_id FROM friendship AS f INNER JOIN users WHERE fk_user_1 = 1 AND f.fk_user_2 = user_id


Probability of future survival for extended cox model

Question: I have fit an extended cox model in R (i.e., some of the covariates change with time), and am now interested in predicting, for the censored observations, the probability that they will survive an additional year. I am fine with kaplan-meier survival curve estimates, and if needed, smoothed estimates for the hazard.
How would I go about getting these probability predictions? Tools for doing this and/or a theoretical understanding of how to do it would be helpful to me.
Answer: Perhaps the Cox-predicted survival curve could help you. Example follows. It does work with the extended Cox model.
install.packages("survMisc"); library(survMisc); library(survival)

# Fit a model, stratified by the variable you wish to predict upon
fits <- coxph(Surv(time, status==2) ~ age + strata(sex), data=lung)

# What is the age-adjusted survival for each sex? A prediction...
autoplot(autoplot(survfit(fits, newdata=data.frame(age=mean(lung$age, na.rm=T), legTextSize = 14, legLabSize = 14, legTitle='', axisLabSize = 14, tabTitle = "Number at risk", title="Adjusted survival by sex", xlab="Time (years)", ylab="Probability of survival", nRiskSize=5, censSize=0))))

Answer: Say a person you want to predict for is censored at time $c_1$. You are interested in the predicted survival from that point on, so on  $S(w) = P(T>c_1+w| T>c_1)$. Then denote $c_1$ as a landmark time and:

create a landmark data set, where time point $c_1$ becomes the new time $0$. In this data set you want to keep only the persons who are still at risk at $c_1$, and discard all information on the past.
Fit the same (Cox) model on this data set. The predicted survival curve from this data set, for the subject that you are interested in (the one censored at $c_1$), is the quantity of interest (you can select the value at 1 year after that). Having a time-dependent covariate should not be a problem. For this subject you can use the last observed value.
Then repeat the procedure for all the censoring time points.

There are two packages in R which can do the splitting of the data set (dynpred and mstate) but it wouldn't be very complicated to implement a function like that yourself.
For more theoretical explanations about this, a reference book is Dynamic Prediction in Clinical Survival Analysis by Hans van Houwelingen and Hein Putter, in this paper or there are a bunch of presentations that I just found online with Google, such as this one.


How to add ui frameworks to a c# form

Question: overflow, im having trouble trying to add a ui framework , for exmple metro framework, to my c# form.
I know that i have to include the reference and include it with
using MetroFramework.Forms;

but im not sure what else to do here and when ever i try and google it, i just get websites that try and sell me a framework package.
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework.Forms;

namespace FrameworkTest
{
public partial class Form1
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
}

I know this may be a silly question but any help would be much appreciated.
Comment: Did you try the documentation? https://thielj.github.io/MetroFramework/#GettingStarted
Comment: @Crowcoder No i haven`t I shall have a look... thank you
Comment: @Crowcoder I see where i went wrong, i should have put `public partial classs Form1 : MetroForm` Thank you for the help.
Answer: Thanks to Crowcoder I found out that I was supposed to add
MetroForm after Form1 like:
public partial class Form1 : MetroForm {
   //Code goes here
}

How to add ui frameworks to a c# form


Juniper SSG5 find bandwidth eater

Question: I looking for a way to get the list of the top bandwidth eater on my Juniper ssg5. On my old OpenBSD Box I had a command line version of ntop. It was just perfect.
How can I do the same thing whith the SSG?
Thank you !
Answer: Nope, you get to set up a port mirror on the switch the SSG is connected to and from there you can sniff, ntop, tcpdump, wireshark, or nfsen as you'd like.
Answer: You can use Junipers session analyzer. You can find it at: https://tools.juniper.net/fsa/
You're suppose to make a session dump first and load it then into the analyzer. Instructions can be found here: http://kb.juniper.net/InfoCenter/index?page=content&id=KB8690
Answer: configuring j-flow might be able to help to you.
http://www.juniper.net/techpubs/software/erx/junose90/swconfig-ip-services/html/ip-jflow-stats-config.html
Comment: J-Flow isn't available on devices running ScreenOS: http://kb.juniper.net/index?page=content&id=KB12911
Comment: so , what your saying is ScreenOS is a piece of **** ?
Comment: Also, if you've got bandwidth problems, enabling *flow can further destroy network performance.
Comment: Nope, just your response. :) And your mastery of your/you're.

But seriously, few companies will port the latest technology back to all former products. Port mirroring is probably the best option for the lower end SSG; if you expect to need JunOS functionality you should be getting a newer box (or a SSG-520M/550M).

Juniper SSG5 find bandwidth eater


Usando Array.Foreach para modificar a coleção

Question: Eu quero remover espaços do início e fim da string (Trim) em todas posições do array, mas nenhuma das formas abaixo funcionaram.
O que estou fazendo de errado?
var optionArray = new string[] { "in the jungle ", " the mighty jungle", " the lion sleeps tonight " };

Array.ForEach(optionArray, x => x.Trim()); // nada acontece
Array.ForEach(optionArray, x => x = x.Trim()); // nada acontece
optionArray = Array.ForEach(optionArray, x => x = x.Trim()); // Cannot implicitly convert type 'void' to 'string[]'

var optionList = optionArray.ToList();

optionList.ForEach(x => x.Trim()); // nada acontece
optionList.ForEach(x => x = x.Trim()); // nada acontece
optionList = optionList.ForEach(x => x = x.Trim()); // Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<string>'

Comment: Veja esta [resposta](http://pt.stackoverflow.com/a/112092/2541) para perceber porque o *ForEach* não funciona quando se pretende alterar um valor da lista. Ela se refere a Java mas o mesmo se aplica ao C#.
Answer: Daniel complementando a resposta do Maniero, o Eric Lippert em 2009 escreveu um bom artigo em seu blog, nele ele explica os malefícios de se usar o List<T>.ForEach no lugar do foreach. Você pode ler o artigo na integra no seguinte link.: "foreach" vs "ForEach"
Um outro argumento, seria que os próprios arquitetos da Micrsoft decidiram não incluir um método ForEach no Linq, você pode ler a respeito do assunto em.: Why no ForEach method on IEnumerable interfaces
mas em resumo, você não deve usar o List<T>.ForEach, por que este viola os princípios da programação funcional, podendo causar alguns efeitos colaterais. além de que, o List<T>.ForEach é menos legível e isto pode implicar em uma manutenção mais difícil (tente debuggar uma iteração dentro de um List<T>.ForEach).
Ainda assim, este um cenário onde este tipo de implementação pode vir a ser útil, paralelismo... e mesmo assim existe todo um Namespace para tratar disto.: https://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel(v=vs.110).aspx
var lista = Enumerable.Range(1, 10).ToList();
Parallel.ForEach(lista, (item, state, indice) => {
    Console.WriteLine(string.Format("indice: {0}, value: {1}", indice, item));
});

mas a implementação ainda vai sofrer do mesmo problema, você não vai conseguir modificar a lista, neste caso você precisar fazer uso do Parallel.For e acessar o elemento pelo índice.:
var lista = Enumerable.Range(1, 10).ToList();
Parallel.For(0, lista.Count, (indice, state) => {
    lista[indice] *= 2;
});

foreach(var item in lista) {
    Console.WriteLine(string.Format("value: {0}", item));
}

Coloquei no GitHub para referência futura.
E por fim, um bom motivo para não usar o .Select(s => s.Trim()).ToArray(), você não está modificando os valores do array. Você está criando um novo array com os novos valores, neste ponto você terá dois arrays em memoria, então estará apenas desperdiçando memoria, ciclos da CPU e forçado que o garbage collector seja chamado mais vezes.
Answer: Pode usar o Select() do LINQ, mas precisa criar um novo array:
var newArray = optionArray.Select(s => s.Trim()).ToArray();

A não ser que queira fazer "manual" (provavelmente o que eu faria):
for (var i = 0; i < optionArray.Length; i++) {
    optionArray[i] = optionArray[i].Trim();
}

Veja funcionando no ideone. E no .NET Fiddle. Também coloquei no GitHub para referência futura.
Comment: Não é possível alterar coleção com `ForEach`? Parece que isso é feito em um exemplo do [DotNetPearls](http://www.dotnetperls.com/array-foreach#d). Por que você faria da maneira "manual"?
Comment: Daniel, ele não está alterando o valor dos itens da coleção, está simplesmente "logando no console" os valores no momento em que percorre cada item. Funcionaria para você caso você quisesse apenas logar este valor, como por exemplo:
optionList.ForEach(x => { Console.WriteLine(x.Trim()) });
Isso logaria os valores sem espaço, mas não modificaria o array.
Comment: Não é possível, o elemento não é passado por referência, então não é alterado no *array*. Até poderia, mas teria que criar outro *array* também. Se quiser mesmo usar um método e *lambda* prefira usar o LINQ, é mais moderno e faz o que quer. Eu iria até de `foreach` mesmo.


Backup de todas as tabelas com exceção de uma

Question: Em MySQL podemos realizar backups via terminal da seguinte forma:
Base de dados
mysqldump nomeBaseDados > meuFicheiroBackup.sql

Tabela
mysqldump nomeBaseDados nomeTabela > meuFicheiroBackupDaTabelaX.sql

Como posso fazer um backup de todas as tabelas da minha base de dados nomeBaseDados com excepção de uma chamada tabelaCorrupta ?
Answer:
O mysqldumptem uma flag exatamente para isso:
--ignore-table

Exemplo de uso:
mysqldump -u username -p database --ignore-table=meubanco.tabelaCorrupta > database.sql

Lembrando que, se necessário, você pode usar o --ignore-table mais de uma vez:
mysqldump -u username -p database --ignore-table=db.tab1 --ignore-table=db.tab2 ...

Veja mais detalhes no manual.
Comment: No MariaDB fiz da seguinte forma:  mysqldump -u root -p  --ignore-table=.{tabela1,tabela2,tabela3} > backup.sql
Comment: @dark777 isso é uma característica do shell usado (expansão de parâmetros), e não um recurso do MariaDB. Se você der um `echo a.{1,2,3}` no mesmo shell o resultado também será `a.1 a.2 a.3`, pois a expansão ocorre antes da execução. Na prática você gerou uma linha com `--ignore-table=` várias vezes, conforme mencionado na resposta.
Comment: sim fiz isso pra nao ter que ficar escrevendo --ignore-table varias vezes.


Proof: $\sin \mathrm{A}+\sin \mathrm{B}+\sin \mathrm{C} \leq \cos \frac{\mathrm{A}}{2}+\cos \frac{\mathrm{B}}{2}+\cos \frac{\mathrm{C}}{2}$

Question: Let $\mathrm{I} \subseteq \mathbf{R}$ be an interval and
$\mathrm{f}: \mathrm{I} \rightarrow \mathbf{R}$.
We have this inequality for any  $x, y, z \in I$,
$$
f\left(\frac{x+y}{2}\right)+f\left(\frac{y+z}{2}\right)+f\left(\frac{z+x}{2}\right) \geq f(x)+f(y)+f(z) .
$$
If A, B, C are the measures of the angles of a triangle $ \mathrm {ABC} $, expressed in radians, show that:
$$\sin \mathrm{A}+\sin \mathrm{B}+\sin \mathrm{C} \leq \cos \frac{\mathrm{A}}{2}+\cos \frac{\mathrm{B}}{2}+\cos \frac{\mathrm{C}}{2}$$
I have to prove the inequality by using f.
I don't know what "function" to take in order to prove inequality.
Comment: Just choose $f(x) = \sin x$. Use the fact that $A + B + C = \pi$.
Comment: I have not tried about this, but if I was solving this, I would like to think about Jensen-inequality.
Comment: @Zerox A better hint could be that $\,\sin \frac {(A+B)+(A+C)}{2} = \cos \frac{A}{2}\,$.
Answer: Following up on my comment, let $\,f(t) = \sin(t)\,$ and $\,x = B+C\,$, $\,y = C+A\,$, $\,z = A+B\,$.

$f(x) = \sin \left(B+C\right) = \sin \left(\pi - A\right) = \sin \left(A\right)$

$f\left(\frac{y+z}{2}\right) = f\left(\frac{(C+A)+(A+B)}{2}\right) =  f\left(\frac{(A+B+C) + A}{2}\right) = f\left(\frac{\pi}{2} + \frac{A}{2}\right) = \cos\left(\frac{A}{2}\right)$

Then: $\quad\quad\quad\quad f\left(\frac{x+y}{2}\right)+f\left(\frac{y+z}{2}\right)+f\left(\frac{z+x}{2}\right) \geq f(x)+f(y)+f(z) \quad\quad\quad\quad\quad\text{(1)} \\ \iff\quad \cos \frac{A}{2} + \cos \frac{B}{2} + \cos \frac{C}{2} \ge \sin A + \sin B + \sin C$
What remains to be proved is inequality $\,(1)\,$ itself. That follows because $\,\sin(t)\,$ is concave on $\,[0, \pi]\,$, so $\,f\left(\frac{x+y}{2}\right) \ge \frac{f(x)+f(y)}{2}\,$ by Jensen's inequality. Writing it also for $\,(y,z)\,$ and $\,(z,x)\,$ then adding the three inequalities gives $\,(1)\,$.


In which step in the checkout process is an user account is created?

Question: I'm using Commerce Kickstart (Drupal 7.23, Kickstart 7.x-2.9). New commerce orders are linked to user accounts. If an account with the same email address does not exist yet, a new account is created. This is the normal flow.
I would like to know in which part of the checkout process, that account is created or linked to, because I've found some cases where the commerce order is linked to anonymous user with no reason.
Comment: If you're really using Drupal 7.23, then forget whatever you're doing ... Hurry to get your Drupal core version updated. Even if it was only because of the security issue in 7.32 ... Actually upgrading may not be sufficient ... Hopefully you still have an "ap^propriate" backup to restore ... PS: I realize this is NOT a real answer to the question, but hopefully this comment doesn't get flagged for deletion.(until the OPer has indicated the 7.23 'issue' is resolved).
Answer: This happens in Commerce Checkout through Rules.

Rule commerce_checkout_order_convert will check the order's given mail against existing users and automatically assign it.
Rule commerce_checkout_new_account creates a new account if the mail given doesn't match a user.

See the rule "Create a new account for an anonymous order". It creates a new user based off of the mail provided by the anonymous user during checkout.


Commerce Kickstart Product: URI links to admin

Question: When I try to use a Commerce Product Reference field in any view with a link to content, it links to the -admin- for the product, not the content.
This was discussed for the images here: https://www.drupal.org/node/1970482
But I'm finding that this will happen if you try to create a link wrapper to -any- Commerce Product field. (SKU, Title, etc.)
So.. can someone point me to the place in the Commerce code where that URI is generated so I can override it?
I thought it was in commerce_product_reference.module but I can't figure it out.
Comment: To link the content page, don't use refrence field. Instead use content field directly like content path or link.
Answer: Please see my answer at https://www.drupal.org/node/1970482#comment-9252269 and try to avoid posting the same support request in multiple support forums.
Answer: I was facing this problem also in the latest Kickstart 2 version, so after hours of investigating I was able to prepare those detailed steps how to solve it:

In Views under 'Advanced' and the 'Relationship' heading choose
'Add', and find and select 'Content: Referenced Product' (if
adjusting a cart form view, then select 'Commerce Line item:
Product'). Check the 'Require this relationship' checkbox.
Now, under 'Fields' heading add a field 'Content: Path' (in case
of the cart form view, add 'Commerce Line item: Display path').
'Exclude from display' this field and arrange it to be the first
field (it is important to have this field above the image field in
order to be able to use its [path] token (see point 3)). If you have
a multilingual page, check 'Use absolute link' under 'Rewrite
results' (matters for the 'Content: Path field' only).
Finally, add the 'Product: Images' field. Link it to Nothing.
Under 'Rewrite results' choose 'Output this field as a link' and use
the token [path]. If you are facing the problem also with other
fields, apply this solution also to them.

Source: Me and https://drupalcommerce.org/discussions/556/product-image-views-link-product-admin-rather-product-display-page
Answer: You can add the path alias of the products display to your view, check the box to hide the field. Then rewrite your product reference field to link to path.


Theming user_profile_form (the user edit form)

Question: I am looking for easy ways to theme the user profile form that will allow me to setup a two-column layout for the form and exclude certain elements.
How can I achieve it?
Comment: I created this question for two reasons: First, because it took me a while to figure out my personal method for solving this problem and wanted to save others grief. Second, to see if anybody else has a better way of theming not only the user_profile_form but forms in general. I'll post links to good questions about form theming as I see them; others are welcome to do the same.
Comment: The Drupal documentation on theming node forms is a good place to start for general instruction on form theming: http://drupal.org/node/1092122
Answer: Aside from using the Display Suite module, most methods for theming forms require a bit of code to be written. In the following method I define variables from the form that can be outputted in a template file to fit whatever HTML structure you'd like. I put all of my code in template.php, but you can just as easily make a custom module to do this.

Add a hook_theme() implementation
function YOURTHEME_theme($existing, $type, $theme, $path){
  return array(
    'user_profile_form' => array(
      'render element' => 'form',
      'template' => 'user-profile-form',
      'path' => drupal_get_path('theme', 'YOURTHEME') . '/templates',
      //add '/templates' only if you store template files in an additional folder
    ),

  );
}

This tells the Drupal theming system that there's a template file waiting for it in YOURTHEME/templates.
Next we'll define some variables to pass into that template in a hook_preprocess_HOOK function. We can find the structured array of the form in $variables['form'] and assign the form elements to the variables we'd like to throw around our tpl.php file.
function YOURTHEME_preprocess_user_profile_form(&$variables) {
  $form_inputs = array(
    'account' => $variables['form']['account'],
    'picture' => $variables['form']['picture'],
    'actions' => $variables['form']['actions'],
  );
  $variables['rendered'] = _YOURTHEME_form_variables_render_all($form_inputs);
}

function _YOURTHEME_form_variables_render_all($elements) {
    //Create array to return, with element name as key and element as value
    $elements_array = array();
    //For each element, render it and add it to the array
    foreach ($elements as $key => $element) {
        $elements_array[$key] = render($element);
    }
    //Return array
    return $elements_array;
}

This example will put the general account info fields, the user picture upload field (if pictures are enabled), and the submit actions into a variable called "rendered."
Now that we have defined these variables, we can output them in a template file. Create a template file named user-profile-form.tpl.php in 'YOURTHEME/templates'. This is a demo example:

  <?php echo $rendered['picture']; ?>
  <?php echo $rendered['account']; ?>
  <?php echo $rendered['actions']; ?>
  <input type="hidden" name="form_id" value="<?php print $form['#form_id']; ?>" />
  <input type="hidden" name="form_build_id" value="<?php print $form['#build_id']; ?>" />
  <input type="hidden" name="form_token" value="<?php print $form['form_token']['#default_value']; ?>" />

Notice the hidden form fields at the end; these include vital Drupal tokens that help secure forms from attack. Your form won't work without them.
When you visit your user edit page, you should now see only some general account info fields and the picture fields. Create different variables in the preprocess function to separate out form elements and place them wherever you wish in the user-profile-form.tpl.php file, adding whatever markup you like.
Comment: It's been so long I honestly don't remember. I do think that the ability to output forms in a template file (while still having the power of the Forms API to generate powerful, secure forms in backend code) is a big win for flexibility and code separation; much closer to the MVC model Drupal should aspire to.
Comment: True that, saying that it "should aspire to" an MVC paradigm is definitely just an opinion (and I like the article, by the way). I guess my point isn't that Drupal should follow strict MVC guidelines, but rather that template files often provide themers with little Drupal experience more flexibility than learning another abstraction layer (DS) or a Drupal API. I think that viewpoint is gaining credence; I'm looking forward to a much simpler presentational layer in D8 with the Twig initiative. Code like this is basically just a silly hack to get there.
Comment: Is there a reason you're avoiding Display Suite Forms?
Comment: Come on, Drupal is not MVC nor should it be. Check http://www.garfieldtech.com/blog/mvc-vs-pac (although that post is quite old, it still helps to clear up things).
Answer: Display suites can only be used for the display, not for editing. It were great, if it could handle both.

http://drupal.org/node/1087520
http://drupal.org/node/798720
Comment: Display suite 7.x *can* be used to edit user edit forms. In a limited way.
Answer: Some fixes for areynolds's code:
Underscore fix in 'render element
function YOURTHEME_theme($existing, $type, $theme, $path){
  return array(
    'user_profile_form' => array(
      'render element' => 'form',
      'template' => 'user-profile-form',
      'path' => drupal_get_path('theme', 'YOURTHEME') . '/templates',
      //add '/templates' only if you store template files in an additional folder
    ),

  );
}

Fix preprocess function from data overriding (account->picture) and adding action elements (form button).
function YOURTHEME_preprocess_user_profile_form(&$variables) {
  $required_elements[]=$variables['form']['account'];
  $required_elements[]=$variables['form']['picture'];
  $required_elements[]=$variables['form']['actions'];
  $variables['rendered'] = drupal_render($required_elements);
}

But all this manipulations give only appearance of the form. But the form won't work properly as it requires other elements such as validate, submit handlers, etc. So I think the best way to hide unrequired elements in $variables['form'] array leaving all necessary form metadata.
Comment: Thanks @ACD for getting me back here to review my old noob code. I've made the corrections you've suggested and added a couple more refinements (namely, making sure that the form tokens are actually included). Not quite sure how or if this code ever did work; it might have displayed a form, but shouldn't have been able to submit.
Answer: Panels can fully take over the Profile edit form and with a handful of patches (currently) you can pull in Profile2 info as a Panels relationship.


Internal Server Error when running Gunicorn as a service

Question: I'm trying to deploy my Django app for the first time on a Digital Ocean VPS.  I've used their Django droplet which setup things like this except I'm not using a virtual environment.
After adding my code and changing a few things, if I run gunicorn directly from the command line, the site loads.
However, if I run gunicorn as a service sudo service gunicorn start, I just get an 500 "Internal Service Error".  There is no error in the nginx log file.  The gunicorn log file does not exist.
Here is the command I run that works (run from the /home/django directory):
gunicorn --pythonpath=myproject --bind=0.0.0.0:9000 --config /etc/gunicorn.d/gunicorn.py myproject.wsgi:application

Here is the /etc/init/gunicorn.conf file:
description "Gunicorn daemon for Django project"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

# If the process quits unexpectedly trigger a respawn
respawn

setuid django
setgid django
chdir /home/django

exec gunicorn \
    --name=myproject \
    --pythonpath=myproject \
    --bind=0.0.0.0:9000 \
    --config /etc/gunicorn.d/gunicorn.py \
    myproject.wsgi:application

I'm new to gunicorn and nginx so this is really stumping me.
Answer: Internal Server Error (HTTP 500) usually indicates that there is a problem with your code, not the Web Server application.
Have you set up logging?
You should log all 500 errors to a file in your django project, so that you can see why the error is occurring.
You can also try switching on Debug mode, which should tell you why the application is failing.
Comment: perfect! in this way: vim settings and change DEBUG = True


A Conway quadratic (form) equation in four variables

Question: In the Sensual Quadratic Form page 44, J.H. Conway is discussing isospectral lattices of dimension 4 when he states the following:

''... we find all solutions of the equation(s) $x^2+7y^2+13z^2+19w^2=48$ ... to find possible vectors of norm 4 ... in $L^+(1,7,13,19)$ and $L^-(1,7,13,19)$.''

Here $L^+$ and $L^-$ are the isospectral lattices in question. I know that 'all solutions' in this case refers to integer solutions, but I wonder how one goes about finding these. The statement is written in such a way that it would seem easy to do, yet I have no idea where to start.
Comment: positive definite. For $x^2 + 7y^2 + 13 z^2 + 19 w^2 < 100$ we have $|x|<10,$ $|y| \leq 3, $ $|z| \leq 2, $  $|w| \leq 2.$  Finite search if you wish to use a computer to do it.
Comment: 48       1   2   0   1;
  48       3   1   1   1;
  48       4   0   1   1;
  96       0   1   1   2;
  96       1   3   1   1;
  96       4   2   2   0;
  96       5   0   2   1;
  96       6   2   1   1;
  96       7   2   0   1;
  96       8   0   1   1;
Comment: Ahh I see, thank you! The inequalities for the variables are sort of worst case scenarios for each, given that their combination is less than 100. I would gladly accept your comments as an answer
Answer: It suffices to find all solutions with $x$, $y$, $z$ and $w$ nonnegative. Because the quadratic form is positive definite, we see that
$$y^2\leq\frac{48}{7},\qquad z^2\leq\frac{48}{13},\qquad w^2\leq\frac{48}{19},$$
and so $y\leq2$, $z\leq1$, $w\leq1$, and for each choice of $(y,z,w)$ there is at most one (nonnegative) value of $x$ that satisfies the equation. So this leaves just $12$ cases to check, which isn't too much work to do by hand even.
Comment: I think you are missing absolute values in your inequalities for y,z etc
Comment: I've restricted to nonnegative values, as that leaves fewer cases to check.
Comment: Ah, I was being careless. Excuse me!


Java generic utility method to convert between two types

Question: I have a method to convert between objects of two different classes.
These are DTO objects and hibernate entity classes.
public static DomainObject1 convertToDomain(PersistedObject1 pObj) {
        if (pObj == null)
          return null;
        DomainObject1 dObj = new DomainObject1();
        BeanUtils.copyProperties(pObj,dObj); //Copy the property values of the given source bean into the target bean.
        return dObj;
      }

Instead of having same method with DomainObject2 and PersistedObject2 and so on .. Is it possible to have a generic method with below signature? (without having to pass source and target class)
 public static U convertToDomain(V pObj) {
    ...}

PS: (A different topic that it is wasteful to use DTO when entities have same structure which some people don't agree to despite hibernate documentation and other sources)
Answer: To achieve this you would need to pass the class of the Domain object you are looking for. Something like the following would work:
public static <T> T convert(Object source, Class<T> targetType)
        throws InstantiationException,
        IllegalAccessException,
        InvocationTargetException {
    if (source == null)
        return null;
    T target = targetType.newInstance();
    BeanUtils.copyProperties(source, target);
    return target;
}

With that being said, as it appears, you are already using Spring. You could try registering a special converter with Spring'sConversionService (Auto wire conversion service) and you can use the convert method to achieve the same result).
Please note that you should add some checks to make sure that each the entity and the domain objects are compatible otherwise you'd end up with a big mess and your code would be error prone.
Comment: I think it should be: BeanUtils.copyProperties(target, source) if you are using Apache Commons (perhaps correct for Spring ...)


Convergence of $\int_0^{\infty}\left(\frac{x}{\ln x}\right)^c\frac{1}{1+x^{4c}}\ dx$

Question: For which values of parameter $c\in\mathbb{R}$ is $$\int_0^{\infty}\left(\frac{x}{\ln x}\right)^c\frac{1}{1+x^{4c}}\ dx < \infty$$
Using the inequality $\ln x > \frac{x}{1+x}$ it can be shown that $$\int_0^{a}\left(\frac{x}{\ln x}\right)^c\frac{1}{1+x^{4c}}\ dx < \int_0^{a}\left(\frac{x}{\frac{x}{1+x}}\right)^c\frac{1}{1+x^{4c}}\ dx = \int_0^{a}\frac{(1+x)^c}{1+x^{4c}}\ dx < \infty \ \ \forall c\in\mathbb{R}$$
But I don't know what next, how to nicely check convergence of $$\int_a^{\infty}\left(\frac{x}{\ln x}\right)^c\frac{1}{1+x^{4c}}\ dx$$ is respect to $c$. Got any tips?
Comment: $\ln x > \frac{x}{1+x}$ is true for $x >1.93295$
Comment: You're right, it was $ln(1+x)>\frac{x}{x+1}$ for $x>-1$ without $x=0$
Comment: Fot he integral between $0$ and $\infty$, you have convergence if $c<-2$.
Answer: Around $+\infty$, the integrand behaves like $\frac{x^{c-
4c}}{\ln^{c} x}$ then we need $-3c<-1$ (the P-test) meaning $c>1/3$.
The above condition is necessary, it remains to prove that if $c>1/3$ the integral converges for $a>1$ which is easy.
If $a<1$, we must have $c<1$ due to the singularity around zero.
Conclusion : the integral mentioned in the title converge iff $c\in (1/3,1)$.
Comment: Just by curiosity, did you compute the integral for any value of $c$ ? Please let me know. Thanks.
Comment: @ClaudeLeibovici I tried with $c=1$ (wich should be the easiest) and got nothing elementary.
Comment: What did you get as a result for the integral ?
Comment: @ClaudeLeibovici Seems like I have done some errors and got a Lerch Phi results, if I get something I'll tell you.
Comment: What did you type nto arrive to something involving Lerch Phi ? Be sure it is just by curiosity. I have the feeling that we need $c<-2$ but I am just unable to prove.
Comment: No, wait, I don't see how the condition for $c$ is necessary here. What about this logarithm expression?
Comment: $\ln x =o(x^{\epsilon})$ for any $\epsilon >0$, so it does not matter.
By the way, for the last integral you need $a>1$ or $c<1$ or else you get $+\infty$, I forgot to mention this.
Comment: @Michael yes but we don't need that, we have stronger condition $c>1/3$.
Comment: Where does $c<1$ stem from?
Comment: @Jules Near $1$ we have $\ln x\sim (x-1)$.


physical significance of latent variables in machine learning

Question: Just curious to know the meaning contained in the latent variables used in the machine learning applications ?
Basically, I am confused if latent variables present the same essence as the model parameters ? In Parameters vs latent variables, @alberto suggested that parameters are fixed while latent variables are essentially random variables that are not observed directly.
My point is that even a parameter is a random variable (its just that we do not know the value of this parameter). Surely, we know the distribution that it is coming from and using this distribution as a prior information, we try to estimate the value that fits the training data well. Am I going well with this concept ?
Comment: Parameters are constant across all data points while latent variables can vary. Yes, parameters can be treated as random variables too, which was pointed out in the other thread.
Comment: @shimao I've copied your comment as an answer. If you would prefer to write your own answer, please let me know and I'll delete mine.
Answer: Parameters are constant across all data points while latent variables can vary. Yes, parameters can be treated as random variables too, which was pointed out in the other thread.

I've copied @shimao's comment as an answer because the comment is, more or less, an answer to this question. We have a dramatic gap between answers and questions. At least part of the problem is that some questions are answered in comments: if comments which answered the question were answers instead, we would have fewer unanswered questions.

Are we seeing a dramatic drop in answers per question?

Comments that are actually answers

We have a very large & widening gap between questions and answers. How do we fix it?