Why Student's t-test doesn't require normality of population?

Question: this PSU master's degree course says t-test can be used if either of the conditions is true:

population is normal.
sample size n>30.

However, I read that t test is applicable when $X_{i}\stackrel{iid}{\sim}N(\mu,\sigma^{2})$, because this leads to the independence between $a=\frac{\bar{X}-\mu_{0}}{\sigma/\sqrt{n}}$ and $b=\frac{(n-1)S^{2}}{\sigma^{2}}$, which is one necessary condition of the ratio between $a$ and $\frac{b}{n-1}$ to be t-distributed.
Is the condition #2 an empirical rule to overwrite the theoretical conditions?
Comment: This seems to be a FAQ: https://stats.stackexchange.com/questions/493349/can-we-always-assume-normal-distribution-if-n-30, https://stats.stackexchange.com/questions/9573/t-test-for-non-normal-when-n50, https://stats.stackexchange.com/questions/78961/t-test-for-more-than-30-samples, https://stats.stackexchange.com/questions/2541/what-references-should-be-cited-to-support-using-30-as-a-large-enough-sample-siz,
Comment: Thank you @kjetilbhalvorsen, I am still bit new in this community. These archives are super helpful.
Answer:
This is the ideal case.  When the population distribution is normal, then the sample mean is independent of the sample variance.  Additionally, the numerator of the test statistic is a normal random variable while the denominator is a chi-square divided by its degrees of freedom; the definition of a t statistic.  However, even when the population distribution is not normal the t test can be very useful.  The question is "how far from normal is far enough for the t test to be ineffective".

This is a commonly misunderstood claim.  When $n>30$ the relative difference between a Student t and a normal distribution is sufficiently small so that someone decided they didn't have to print Student t tables past $\nu=30$ and could instead use normal tables with little consequence.  Let me be clear: this is bad advice.  Thirty is not a magic number and using this particular line in the sand as justification for using the t test is poor statistical practice.
Comment: The $t$-test is **not** robust to non-normality in general.  The power of the test can be arbitrarily bad and the tail coverage for at least one of the two confidence limits can be arbitrarily inaccurate.  Mild tail heaviness or moderate to severe skewness can really mess with the $t$-test.  These problems are reasons that methods such as the bootstrap and the Wilcoxon test and its associated Hodges-Lehmann central tendency estimator exist.  A good way to understand the severe limitations of the $t$-test is to understand that the SD can be an awful measure of dispersion.
Comment: Don't know of the best reference but studying Gini's mean difference will shed light on this.  SD is not a good measure if the distribution is asymmetric.
Comment: ... On the other hand saying things like "moderately robust" (as I would) is arguably hardly any better, because while not obviously false, it offers no really useful guidance. However, that can be a deliberate strategy if one aims to lead people into actually investigating properties in a range of situations in the vicinity of their specific circumstances rather than relying on potentially quite misleading rules of thumb that make themselves out to be completely general.
Comment: Thank you Demetri for the answer. For your 2nd point, I think the misconception may have CLT to blame? Yes CLT dictates the sampling distribution is normal, but that is with known $\sigma$, which is almost never the case.
Comment: @Demetri that page you link to only looks at quite mild deviations from normality. Of course everything works great then, because they avoided any cases where it didn't.
Comment: @Glen_b The problem with this entire thread is that I, nor anyone else, has defined what "robust" and "departure from normality" means specifically.  Before I defend approaches or acquiesce to objections I'd like to know:  What properties of the t test (be they false positive rate, power, CI coverage or otherwise) do we find important in this application, and what is the context in which we are working.  In light of Frank's comments and other considerations, I find discussing further arguments moot.
Comment: We are faced with a claim that is not accompanied by any qualifying condition (i.e. it appears to be a quite general claim, of the form "if ... then") in the question and we can easily evaluate whether it stands up or not without necessarily making a specific counter claim. Similarly, I'd say the assertion that the t-test is "incredibly robust" (since it is also unqualified) is not adequately supported by the very modest investigation in the information at the link. ... ctd
Comment: @lambda  I think there are a variety of reasons for the confusion in 2. No one thing is to blame.
Comment: so @Demetri Pananos, back to the original online course that I had question on, did you mean the t-test is indeed robust against non-normality, it's just the n>30 is too blank check and we need to investigate the data on a case by case basis? Thanks!
Comment: @lambda the t test is incredibly robust to departures from normality.  See https://people.wikimedia.org/~bearloga/notes/comparing-groups/
Comment: @FrankHarrell, thanks, I will look into those alternatives. Could you elaborate a bit on the SD being not a good choice? I didn't know this. Any article that you recommend? Thanks!
Comment: I do have a follow up question though. If CLT gives $\frac{\bar{X}-\mu}{\sigma/\sqrt{n}}\sim N(0,1)$, Slutsky's theorem further gives $\frac{\bar{X}-\mu}{S/\sqrt{n}}\sim N(0,1)$ as long as $\lim_{n\rightarrow\infty}Var(S^{2}_{n})=0$, which seems a quite weak condition, then why don't we skip the t-test with the ambiguous assumptions in real use, and directly use the z-test? I am self-learning, and from what I read in blogs etc, t-test is still more dominant.
Answer: The significance level of the t-test is somewhat robust against mild to moderate deviations from normality, but we must take care not to claim too much.
By being overly general, the second recommendation (re $n>30$) is potentially dangerous.
Many posts on site discuss this or similar "sample size $30$" rules (I encourage you to search out some) but let me give an explicit, simple example that shows how easily we can strike a problem, since it seems this particular claim can be found even in high-level courses.
Angela and Bart are planning to use a pair of ordinary dice in a game, but because they're taking different roles in the game they have different concerns about possible bias. Chaina is an impartial person who will supervise the game for them.
Before the game, the players want to be confident that the dice are not badly biased against them, so they ask Chaina to roll the pair of dice 100 times to help them check the dice are not going to place either of them at a severe disadvantage. The outcome "double-six" (which is an important roll in the game) is of particular concern for them both; one benefits from it occurring, and the other from it not occurring.
Consequently Chaina rolls the two six-sided dice and records "1" every time they come up double-six, for each of 100 attempts. If the dice are fair and rolled properly, we should have to a good approximation a series of 100 independent Bernoulli trials (0/1 values), where the probability of 1 on each trial is 1/36.
After extensive investigation and copious application of slide rules, abacuses, calculators and counting on fingers and toes, I eventually managed to convince myself that the resulting sample of 0's and 1's meets the second condition in your post. That is, the sample size of 100 appears to exceed 30, by at least several more toes than I had in my possession. As an additional check, I asked my six year old neighbor for his thoughts on the matter, and he gladly lent his expert opinion at no charge, stating that 100 was indeed "lots" larger than 30, which more than made up for his unwillingness to contribute additional hands and feet to the task of computing relative sample sizes.
Consequently, according to this rule, we can proceed with our hypothesis tests with complete confidence.
Angela wishes to conduct a test of whether the mean - i.e. the proportion of recorded 1's (representing having got "double six") in 100 attempts is too low -- her alternative hypothesis -- compared to that for a pair of fair dice (100/36) at the $1\%$ level. She does a (one tailed, one sample) t-test which she believes should be fine, per your information.
Bart wishes to conduct a test of whether the mean - the proportion of 1's in 100 attempts is too high compared to that for a pair of fair dice (100/36) at the $5\%$ level. He does a (one tailed, one sample) t-test, which he feels  assured is fine, again per your information.
Chaina also wants to check the dice are fair. She decides to follow the recommendations of a number of researchers in recent years, such as Benjamin et al. (2018)[1] and Ioannidis (2018)[2] to do a two-tailed one-sample t-test at the 0.005 ($\frac12\%$) level.
If the dice are fair (i.e. $H_0$ is true), what's the rejection rate (i.e. the true significance level, $\alpha$) for each of these three tests?
Try a simulation and see for yourself. Would any of them be satisfied with the rule you quote?
If our protagonists were to use R(*) to check the properties of their test, they will see approximately $6\%$ rejection rate on the $1\%$ test, roughly $0.07\%$ on the $5\%$ test and approximately $6\%$ on the two-tailed test that was to be conducted at $\frac12\%$.
How is that adequate?
Yet this is a pretty simple, "everyday" sort of situation.
If the "30" rule only applies sometimes, shouldn't they tell us when it does work? We seem to be missing important guidance here.
Clearly a more sophisticated rule is required; this one will not do.
Further, the common claim that the t-test is very robust obviously needs modification. It's not like we have some bizarre edge-case here. It's clearly not level-robust in this example --- and we haven't yet touched on its power behaviour, which is even more easily impacted.

* I made a point of this because it matters how your software treats a sample of all one value in the one sample t-test, which will happen occasionally. I agree with R's treatment of this situation; e.g. it gives a p-value of $0$ when the t-statistic has a non-zero numerator and a zero denominator in a two tailed test (where the hypothesized value is infinitely-many sample standard deviations from the sample mean)
Any potential software differences are in any case easily avoided by considering an analogous continuous distribution (which also avoids some other potential arguments).
Now our response is no longer a set of 0/1 outcomes but a continuous bimodal distribution with two very narrow peaks (i.e. where the spread around each mode is much smaller than the distance between the modes), with the lower-outcome mode having most of the probability (about 35/36 of it) and the higher-outcome peak having the remaining probability (1/36).
Imagine, perhaps, that we're recording (say) energy of individual photons where the energy is quantized (so we measure an extremely narrow range around a specific value for each of two values) and we're seeing one of the energies about 35 times as frequently as the other. Or imagine any other outcome that is bimodal with very narrow peaks and one much more frequent; other example circumstances that could fit this situation are not difficult to come up with.
With such a setup we will see similar results to those I present above, just from a simple continuous bimodal distribution.

Some example R code:
<code>    pp = replicate(100000, t.test(rbinom(100, 1, 1/36), mu=1/36,
                    alternative="less")$p.value)
    mean(pp<.01)   #  We need this to be close to 0.01
    pp = replicate(100000, t.test(rbinom(100, 1, 1/36), mu=1/36,
                    alternative="greater")$p.value)
    mean(pp<.05)   #  We need this to be close to 0.05
    pp = replicate(100000, t.test(rbinom(100, 1, 1/36),
                    mu=1/36)$p.value)
    mean(pp<.005)  #  We need this to be close to 0.005
</code>
To conduct the corresponding continuous data versions of the tests you could add a random (scaled and centered) symmetric beta value to each observation, with center 0 and range of something like (say) $10^{-4}$ times the distance between the modes.

[1] Benjamin, D.J., Berger, J., Johannesson, M., Nosek, B., Wagenmakers, E., Berk, R., et al., "Redefine statistical significance", Nat. Hum. Behav., 2018, no. 2, pp. 6–10.
[2] Ioannidis JPA., "The proposal to lower P-value thresholds to .005", JAMA. 2018; 319:1429–30.
Comment: ... So *how do they know that they're not in one of those "the approximation very poor" situations*? People weirdly write "academic" as if they imagined that academics never solve real problems when many do regularly. I have been an academic, but I work in industry and have done so in at least some capacity continually for more than 30 years. Being in industry does not mean you can't be careful, or thoughtful. It doesn't mean you can't make sure your claims hold water. It doesn't mean you can't do some actual checking that things have the properties you want them to.
Comment: Thank you @glen_b for the excellent counter example. I don't know about the backgrounds of the discussion participants in this thread, but I do see everybody is stressing that we need to be cautious about over-optimistism of the normality violation.

I don't know if this is just a difference in industry vs. academia. I do see A/B testing practitioners in industry embrace the no-normality-is-ok more enthusiastically. Some books even say: no normality is OK, because the normality assumption is only on the sampling distribution of $\bar{Y}$,not on the ${Y_{i}}$.
Comment: They say that a lot ... but you have to ask *is that actually the case*? (The answer is *not if they want a t-statistic to actually have a t-distribution* -- note that a t-statistic is *not just a numerator*. If you ignore that, at best we're dealing in approximations, and in that case how do we tell if it's a good approximation? We could do simulations like the above ... where we have already seen that sometimes the answer is that the approximation is very poor)
Comment: That's true @glen_b; dividing the scenarios into industry and academia is too coarse.


Inner .each()-loop with .append()

Question: I have an array of projects. Projects can have subprojects, hence the field parent_id.
I want to show a list of subprojects and for each subproject i want to "preview" the prio1 subprojects of that subproject.
So i have two .each()-loops and use .append() to add the HTML (INNER AND OUTER).
For some reason the preview subprojects come out on top of the list instead of inside the project-div.
Any ideas what i do wrong?
        var filterdata = this.get_subprojects(project_id);
      // **** OUTER LOOP *******
        filterdata.forEach(function(project) {
            var subprojects = projectList.get_subprojects(project.project_id, 1);

        $("#projects>.list").append(
        '
'

            +'
'
                +''
                +''
            +'
'

            +'
'
                +'
'+project.title+'
'
                +'
');

            //*** INNER LOOP LOOP FOR PREVIEW *****
                subprojects.forEach(function(subproject,1) {
                    $("#projects>.list").append(
                    +''+subproject.title+'');

                });

            $("#projects>.list").append(
                '
'
            +'
'
             +'

'
        +'
 ');
        return $("#projects>.list").html();
        });

Comment: Would you mind making a [jsfiddle](http://jsfiddle.net/) out of this? It would make it a lot easier for us to test, and change the code for you! :)
Answer: You can't .append sections of HTML like that. The HTMLstring .append() accepts expects proper HTLM content. Tags in that string automatically get closed.
Try this instead:

$('#container').append('
Outer div
');
$("#outer").append("
Inner div
"); // As example why your code doesn't work:
$("#outer").append("
Broken div (Check the rendered HTML, the tag will be fixed)");
div{
  border: solid 1px black;
  padding: 2px;
}

Comment: Okay, I guess i put it all in a string first then. That should solve it, right?

Inner .each()-loop with .append()


Properties of the rank function on independence systems

Question: Let $\mathcal{M}=(X,\mathcal{I})$ a matroid on $X$ (here $\mathcal{I}$ is the collection of independent subsets of $\mathcal{M}$). If $\rho$ denotes the corresponding rank function, namely the function
$$
\rho(A)=\max \{|B|: B \in \mathcal{I} \cap \mathfrak{P}(A)\}
$$
(here $\mathfrak{P}(A)$ stands for the powerset of $A$), we may define the set operator
$$
\sigma_\rho(A)=\{x \in X: \, \rho(A)=\rho(A \cup \{x\})\},
$$
which turns out to be a closure operator. We will say that $x$ depends on $A$ if $\rho(A)=\rho(A \cup \{x\})$. In order to prove that $\sigma$ is increasing, it suffices to see that if $x$ depends on $A$ and $A \subseteq B$, then $x$ depends on $B$.
Well, my question is: if $\mathcal{I}$ is only an independence system (a nonempty collection of subsets which is closed under taking subsets), is $\sigma_\rho$ increasing? Can you help me, please?
P.S. Probably $\sigma_\rho$ is not idempotent and, hence, in general, it is not a closure operator. But it should be a pre-closure operator.
Answer: Let's take the simplest counterexample to the exchange property. We want independent sets $A$ and $B$ such that $|A|>|B|$, but there is no $x\in A\setminus B$ such that $B\cup \{x\}$ is independent. So let $X = \{1,2,3\}$, $A = \{1,2\}$ and $B = \{3\}$, and $\mathcal{I} = \{\emptyset, \{1\}, \{2\}, \{3\}, \{1,2\}\}$.
This gives an example where $\sigma_\rho$ is not increasing.
Let $C = \{3\}$ and $C' = \{1,3\}$. We have $\rho(C) = \rho(C\cup \{1\}) =  \rho(C\cup \{2\}) = 1$, so $\sigma_\rho(C) = \{1,2,3\}$. But $\rho(C') = 1$, while $\rho(C'\cup \{2\}) = 2$, so $\sigma_\rho(C') = C'  = \{1,3\}$. Thus $\sigma_\rho$ is not increasing.


How to hide keyboard when focus in input in i pad

Question:
How to hide keyboard when focus in input in i pad
Answer: If you need a pure javascript solution, use this line :
<code>document.activeElement.blur();
</code>
This line removes the focus on the active element and hiding the keyboard.
Answer: You can use readonly attribute or blur() right after the input is focused.
Disable setting of focus for jQuery UI datepicker
Comment: $('#ArrivalDate,#DepartureDate').on('focus', function () { $(this).blur(); });i wrote this code but keyboard show for a second and disappear in IOS but in android it hide completely
Comment: Have you tried `event.preventDefault()` method? Ex: `$('#ArrivalDate,#DepartureDate').on('focus', function (e)  { e.preventDefault();  $(this).blur(); });`


Mathematical Induction: how do we know what applies to one thing also applies to another?

Question: Background:
I'm new to math. I'm learning about set theory. The book I'm reading (Schaum's Outline of Set Theory) covers mathematical induction. The book uses a common illustration and a common example (I could only find the same illustration and example when I searched on-line) to illustrate how mathematical induction works.
Illustration: "...it is like a row of standing dominoes. First, demonstrate that one standing domino falls after it is pushed. Second, demonstrate that pushing over one standing domino will knock down the second. Essentially, that proves that a row of standing dominoes will fall if the first is pushed." (taken from decodedscience.com)
Example:
"(1) 1 + 3 + 5 + ... + (2n - 1) = n^2
(2) If x_1, x_2, ..., x_n > 0 then (x_1 + x_2 + ... + x_n)/n ≥ (x1·x2·...·xn)^1/n
etc.
n here is an "arbitrary" integer.
It's convenient to talk about a statement P(n). For (1), P(1) says that 1 = 12 which is incidently true. P(2) says that 1 + 3 = 22, P(3) means that 1 + 3 + 5 = 32. And so on. These particular cases are obtained by substituting specific values 1, 2, 3 for n into P(n)."
(Taken from cut-the-knot.org)
What I Understand:
I know that Mathematical Induction and The Other Induction are different.  I know that they are both about drawing conclusions about the general by examining the specific.
Intuitively, I can see that in the universe of math, where we have the advantage of a 'bottom-up' understanding of the laws of the universe, we can employ induction reliably
I understand that the above equations tell us that the value of a set of odd integers is equal to the square of the set's cardinal. E.g., 1+3+5 = 9.  Cardinal of 3. 3^2=9.
I can see that you could apply this along the whole continuum of odd integers.
What I Don't Understand:
I don't see why those equations were used to illustrate mathematical induction. I know little about proofs, but I believe those equations are examples and not proofs. In that case, would it not have been sufficient, but trivial, to use an example like,  'an integer is always one integer greater than the integer that preceded it' n ≺ n+1
Those equations are used everywhere to illustrate mathematical induction. Nobody uses the example I just provided. So I'm assuming there is something more valuable in those equations than in my example. What is it?
I thought of some scenarios where (my understanding of) mathematical induction would be misleading. For instance, if I said that 'a prime number will be one integer greater than the prime that preceded it.' That works for the first two prime numbers (2,3), So haven't we illustrated that if you push one domino you will cause it to fall which will cause the next domino to fall...? (But in this case, the second domino will not cause the third domino to fall)
As far as I (wrongly) understand: Mathematical induction is saying that, 'you can know the infinitely applicable relationship between an input and an output, so long as you have two sets of input and output from the same continuum.'
What am I misunderstanding? Thank you for your help.
Added

P(1) is true
If P(*n*) is true for any n, then P(n+1)

I think that I see what I missed.

"P(1) is true," is A
"P(n) is true, for any n" is B
"P(n +1) is true," is C

I thought that what you wrote meant:
(A&B)->C
In words (I may have mucked up the notation):
If P(1) is true; and any P(n) is true, for any n; then P(n +1). In other words: That so long as you have P(1), then for any n for which P(n) is true, you also know that the next n is true (i.e., P(n + 1)).  Since the 'next n' (i.e., P(n + 1)) is true, you have another 'any n for which P(n) is true';and you can know that the n following it (i.e., P(n + 2))is also true... and you could continue this forever; thus replicating the 'domino' effect.
However, it seemed to me that it would also be possible where one could find some properties of the natural numbers for which P(1) is true, and then check that property on any other number, and by coincidence, that property may appear to be true. One would then have satisfied "P(1) is true,", and "P(n) is true, for any n"; in which case, mathematical induction would mislead one to believe that P(n) was true for all natural numbers.
I think that I should have understood that it meant:
(A&(B->C))<-> 'P(n) is true for all natural numbers'.
In words:

'If P(1)' is true.
And if it is also true that P(n + 1) is always true whenever 'P(n) is true, for any n'
Only then and always then, can we say for certain that P(n) is true for all natural numbers.

I'm still curious, how "P(n + 1) is always true whenever 'P(n) is true, for any n'", without having to rely on 'evidence' (since now amount of evidence forms a proof). Is there something about mathematical induction that allows one to prove that, - or is another technique used to prove that it is true that "P(n + 1) is always true whenever 'P(n) is true, for any n'"?
Comment: Welcome to MSE! It really helps readability to format questions using MathJax (see FAQ). Regards
Answer: You seem to have a general confusion regarding induction, so I'll say a couple of things in the hope that somewhere in my answer is what you're looking for.
I'll put the definition a bit formally. Let's say that we want to prove some property $P(n)$ is true for all natural numbers $n$. Mathematical induction tells us that we will have proved that if we prove that:

$P(1)$ is true
If $P(n)$ is true for any $n$, then $P(n+1)$

You are correct that the formulas you mention are examples, not proofs. While I don't know much about the axiomatic foundations of mathematics, as far as I know you don't really prove induction. Rather, it is taken as an axiom of the natural numbers (see Peano axioms).
The problem with your statement that any integer is always one more than the one that preceded is that it's pretty much the definition of natural numbers and induction. It's not a very useful example, because you need to know that in the first place to talk about induction.
You mention that it would be sufficient, but trivial, to use your example. That's the point: why would we mention it if it's trivial? Most explanations of induction use more elaborate examples because that way they show how induction can be used to prove useful things. There is indeed something valuable in statements like "the sum of the first $n$ odd numbers is $n^2$". Namely, they are new knowledge. If we're working rigorously with natural numbers and induction, we already know that $n+1$ is one greater than $n$, because that's the basis for everything we're doing.
Let's take a look at what's wrong with your prime numbers example. First, we need to write it a bit more formally, because to use induction we need to specify $P(n)$. We could say this: $P(n)$ is the assertion that the $(n+1)$th prime number is the $n$th prime number plus one. Let's call the $n$th prime number $p_n$. Then $p_1 = 2$, $p_2 = 3$, $p_3 = 5$, etc. $P(n)$ can now be written as $p_{n+1} - p_n = 1$.
To prove that $P(n)$ is true for all $n$, induction asks that first we prove that $P(1)$ is true. And sure enough, it's true for $n = 1$: $p_{1+1} - p_1 = 3 - 2 = 1$. So $P(1)$ is true. But the next step is where induction fails. It's not true that $P(n)$ implies $P(n+1)$. Let's say by chance we find two consecutive primes whose difference is $1$. This does not necessarily imply that it will also be true for the next pair of primes.
In terms of your analogy: we've proved that if we push the first domino it will fall, but we have not proved that if one domino falls then the next one will fall too, because in this case it's false. We can't say that if we push the first domino then all of them will fall, because we haven't proved that each domino will make the next one fall. All we know is that the first one will fall.
Regarding your definition of induction as "you can know the infinitely applicable relationship between an input and an output, so long as you have two sets of input and output from the same continuum", I must admit I don't understand it. Could you clarify what you mean with this?
Added: You seem to be confusing mathematical induction with scientific induction. In science, we can never really "prove" anything. How do we know that Einstein's relativity is true? Countless experiments and observations have been made, and every one of them agrees with relativity, so we're pretty sure it's true. But could there, in principle, be a circumstance which we haven't tested for which the law fails? Sure, and it has happened many times in the past.
Mathematical induction doesn't work this way. Say we want to prove that the sum of the first $n$ odd numbers is $n^2$. We can check for $n = 1$, and for $n = 2$, and we can check and check all day long until our calculator breaks, but we can never be sure that the statement is true for every natural number because we can't try them all. They're infinite.
Instead, (mathematical) induction provides us with a way to prove something is true for all natural numbers without individually checking all of them, something that is impossible. This is a very important point: it doesn't mean that we know something is true for a lot of numbers, so it must be true for all of them. That is false in mathematics.
When people say that there is evidence but not proof of something, that's exactly what that means. Take the Collatz conjecture, for example. Since it has been checked for a lot of numbers, most people think that it's probably right. But, and this is important again, they don't know that it's true. No one says "Well, we know the Collatz conjecture is true for $n$ up to $5000000000$, so by now we're sure it's true in general". No one says that because as long as you don't have a rigorous proof, there's always the possibility that there is an extremely high number for which the proposition fails.
Comment: Thank you! So if P(n) is  P_n+1 − P_n=1, that is true for primes 2 and 3. So we have two examples. But as you said. We haven't proved anything; because our induction implies that the next number is prime, and it is not.  But in order to determine that our P(N) is incorrect, we have to check it against the third prime. Rhetorically: how many instances must we verify to prove that the induction succeeds.  I have heard of patterns in numbers for which we have 'evidence' into the billions, but no 'proof'.
Comment: @Hal: We don't have two examples. $P(2)$ fails, because $p_{2+1} - p_2 = p_3 - p_2 = 5- 3 = 2 \ne 1$. The good thing about induction is that we don't have to check all the natural numbers, because we can't: There are infinitely many of them! We don't prove a statement by checking enough special cases. Give me a while and I'll add a bit of clarification to my answer.
Comment: The fact that there is 'evidence' into the billions, but no 'proof' leads me to believe that it must be known that it is possible for an identified pattern to apply to a series of integers (for example), but not apply to all integers. So if that's the case, and if we have to check each relevant number to ensure our P(n) applies, then at what point would we become 'satisfied' that our induction is correct? And wouldn't we then still only have a pile of evidence - and not proof? and if we don't have proof then it's still possible (even if its unlikely) that our induction is wrong? Thank you.
Comment: Thank you for the addition. I did understand the distinction between SI and MI. That said, I'm definitely closer to explicitly identifying the root of my confusion about MI. I've added an addition to my question.
Comment: Great answer, that made more clear the idea of Mathematical induction, thanks!
Answer:
I know that Mathematical Induction and The Other Induction are different. I know that they are both about drawing conclusions about the general by examining the specific.

It is important to see that this is absolutely wrong. Unlike empirical induction, mathematical induction is not, repeat not, about drawing conclusions about the general from specific cases. It is a way of drawing a universally generalized conclusion from a universally generalized  premiss (together with an additional singular premiss). The simplest pattern for induction over natural numbers, for example, is this:

(1) For all $n$, if $P(n)$ then $P(n+1)$.
(2) $P(0)$.
Hence (3) For all $n$, $P(n)$.

Note that the crucial premiss (1) is already universally generalized. That is what enables us, with the aid of (2), to draw a universally generalized conclusion.
And the premiss (1) can't be established by looking a cases (a couple of cases or a couple of billion). It will itself have to be established by another argument from general truths (maybe by a further argument by induction, but ultimately by appeal to some basic general axioms about numbers).
Comment: Contra a suggested edited, I do prefer the British spelling "premiss" for the logical sense (cf. Latin *praemissa*).
Comment: Thank you. That's very helpful.
Answer: It should also be stressed that unlike the other axioms, the principle of mathematical induction refers not just to variables, but also to properties. Thus, it would be more proper to refer to it as an axiom schema rather than an axiom - as it is a template for producing an (infinite) number of axioms, rather than being a single axiom in its own right.
Comment: It can be made into a single axiom when formulating a second-order theory, though.
Answer: The principle of mathematical induction (PMI) is just one of the axioms for the natural numbers. (See Peano's Axioms). In the language of set theory, it can be stated as follows:
For all subsets $P$ of $N$, if
(a) $1\in P$, and
(b) for all $x\in P$, we have $S(x)\in P$,
then $P=N$
where $S$ is the so-called successor function defined on the set of natural numbers $N$.
Peano's axioms can be described informally as follows:

$S$ is an injective (1-to-1) function on set $N$.
$1$ is an element of $N$ with no pre-image under $S$. (Guarantees the existence within $N$ of an infinite chain of succession starting at $1$)
The infinite chain of succession starting at $1$ includes every element of $N$. (Equivalent to PMI)


Bayesian optimization - One run with 100 iterations vs ten runs with ten iterations

Question: I was wondering whether it makes more sense to run Bayesian Optimization let's say ones with 100 iterations or ten times with ten iterations each. Which one should I favor?
I would assume that the first is better since the probabilistic model of the objective function gets better?
I'm using Bayesian Optimization to tune multiple hyperparameters of a Random Forest model - max_depth, max_features, min_samples_leaf, and min_samples_split. Currently, I'm using the library pyGPGO with a Gaussian Process and expected improvement as an acquisition function.
Answer: We can't say without knowing your specific problem.
I would suggest you first run $10\times 10$. Then assess how variable your 10 results are (e.g., in terms of the variability of predictions on a holdout dataset). If this variability is "low", you can presumably trust 10 iterations. If this variability is "high", then I would not trust any one of the 10 iterations (I would probably trust an ensemble of the 10 instances more), and I would repeat with $10\times 100$, or whatever number is feasible, and iterate until the results stabilize.
You will need to consult your domain knowledge to operationalize "low" and "high".
Comment: On the one hand, I would recommend trying this on a smaller sample or simulated data, so you get a feeling for how things stand. I would definitely trust that more than random strangers on the internet. On the other hand, if the only cost associated is computing time, you can either run it overnight, or parallelize it by renting a little cloud capacity.
Comment: Thanks for the answer! The only problem, I'm asking about before actually running the experiments since each run is expensive - around 45 minutes of computation time. So I have to choose which way to go, before actually running the experiments :(.


How long does it take DynamoDB to stop throttling?

Question: Suppose I make a very large number of write requests to a DDB table, so that its provisioned write capacity is far exceeded and it starts to throttle me. I eventually stop sending requests. How long do I then have to wait before it's guaranteed that DDB will not throttle a new write request?
Answer: DynamoDB throttled on a per-second basis. If you have no retries enabled on your client, and you stop writing, you would be able to succeed in the following second.
However, when you have retries, those are still consuming capacity. So ultimately the answer is variable, depending on your retry and back-off strategy.
Retries would not surpass 1-2 minutes usually, if using the default configuration.
To further elaborate, capacity in DynamoDB is provisioned on a per-second basis. If you provision 100 RCU, you can consume 100 RCU per second. If you exceed 100 RCU in a given second, you get throttled. However, the following second still has 100 RCU available to you.
This is a simplified example, in reality DynamoDB is an extremely large distributed system, and counting every tables request rate synchronously would impact its performance. For that, we introduce the aspect of token buckets controlled by a PID controller. If you would like to learn more of how this works in-depth, then I suggest this video from Jaso's re:Invent talk 2018
Comment: Interesting answer! 'DynamoDB throttles on a per-second basis.' - do you have any confirmation of this in the official docs? I had the same question years ago and was never able to find an official confirmation.
Comment: None that explicitly states it. But by reading the docs, it's implicitly stated. Your capacity is provisioned by WCU or RCU. That's a limit of how much you can consume per second. So if you exceed the capacity in second 1, and you stop processing, in second 2 you will have the capacity that you provisioned. Throttling is simply an exception to state you've exceeded that seconds provisioned capacity.
Comment: Thank you! It'd be good to add what's in your comment to the answer to provide extra context.
Comment: Updated, if you're interested please give the entire re:Invent video a watch. It goes into great depths on what was previously regarded as implementation detail.


Identificar elemento em uma imagem com PHP?

Question: Existe alguma ferramenta ou forma para verificar se existe um elemento dentro de uma imagem em PHP? O sistema precisa identificar que o elemento da primeira imagem também está na segunda imagem.
Exemplo:
Answer: Existe uma biblioteca javascript de face detection, adaptando ela creio que sirva a sua necessidade:
http://facedetection.jaysalvat.com/


How to assign frontend submitted forms automatically to backend users, if backend users are 3-4 or may increase

Question: How can I assign frontend submitted form to the backend users? Backend users are 3-4 in count and may increase any time, we have a special table and CRUD functionality to create users.
I tried the following way, but it's not working.
I created a table named auto_assign and inserted the submitted form count for each form submitted in that table, after that I took count of the backend users, now I have a count of both:
1. the total form submitted today
2. total count of backend users.
now I wanted to assign the form in following way
if we suppose that we have 5 forms and 3 backend users
form 1 must be assigned to user 1,
form 2 must be assigned to user 2,
form 3 must be assigned to user 3,
form 4 must be assigned to user 1,
form 5 must be assigned to user 2
and the form 6 whenever is submitted must be assigned to user 3 again and the flow must go on,
How can I proceed?
Answer: When you inserting form data fetch users id and calculate to whom you should assign current form. you should also fetch latest assigned form.
Pseudo code:
<code>$myNewForm = some data

$myLatestInsertedForm = [assigned_user_id: 7]

$myUsers = [1,2,3,6,7,8] : 6

new_form_id = insert into form ();

//calculating

if (assigned_user_id < max($myUsers)) {

    user_id = next(assigned_user_id, $myUsers)
} else {
    user_id = min($myUsers)
}

assing (new_form_id, user_id)
</code>


Under what condition, $n(>2)$ non-zero vectors of equal length forms a regular n-gon in Euclidean plane

Question: Suppose we have a unit circle in $R^{2}$ and $f_{1}, f_{2}, \cdots ,f_{n}$ be n vectors s.t. $\|f_{i}\|=1~\forall~i=1,2,\ldots,n$.\
Also we assume that $f_{1}=(1,0)$.\
Under what condition the vectors will form a regular n-gon ?\
At least try to give some hints when $n=3,4$ or $5$.
Answer: You can take $\mathbb{R}^2$ as 1-dimensional vector space over $\mathbb{C}$, then $f_1=1$ and $f_k=e^{\frac{i\pi}{n}(k-1)}$.


How do i make mysql fail to insert a row when some column is not set?

Question: I'm running into a problem where it is possible to insert a row into a table whithout specifically setting an INT NOT NULL column (defaults to 0 which is invalid for my purposes)
I create the table this way:
CREATE TABLE inventorycontrol (
     ID                         INT NOT NULL UNIQUE AUTO_INCREMENT
    ,nodeID                     INT NOT NULL CHECK (nodeID > 0)
    ,custom1                    VARCHAR(128) NOT NULL DEFAULT ''
    ,custom2                    VARCHAR(128) NOT NULL DEFAULT ''
    ,custom3                    VARCHAR(128) NOT NULL DEFAULT ''
    ,custom4                    VARCHAR(128) NOT NULL DEFAULT ''
    ,articlekey                 VARCHAR(32) NOT NULL
    ,amount                     INT NOT NULL
    ,orderID                    INT NULL
    ,supplierID                 INT NULL
    ,customerID                 INT NULL
    ,datecreated                DATETIME NOT NULL
    ,expectedcompletion         DATETIME NOT NULL
    ,datecompleted              DATETIME NULL

    ,PRIMARY KEY (articlekey, datecreated)
)
GO

CREATE INDEX ix_InventoryArticle ON inventorycontrol ( articlekey )
GO

CREATE INDEX ix_InventoryArticle_Custom ON inventorycontrol ( nodeID, custom1, custom2, custom3, custom4 )
GO

CREATE INDEX ix_InventoryAmount ON inventorycontrol ( amount )
GO

CREATE INDEX ix_InventoryCreated ON inventorycontrol ( datecreated )
GO

CREATE INDEX ix_InventoryCompleted  ON inventorycontrol ( datecompleted )
GO

ALTER TABLE inventorycontrol
    ADD FOREIGN KEY fk_nodeID (nodeID) REFERENCES node(ID)
GO

I would expect this insert to fail:
INSERT INTO inventorycontrol ( articlekey, amount, datecreated, expectedcompletion, datecompleted )
VALUES
     ( 'abc', -10, '2009-01-27', '2009-01-27', NULL )
GO

Unfortunately, this just works. (there is no node WHERE ID=0)
Is there any way i can force this insert to fail short of creating a trigger for inserts?
Thanks in advance,
Kris
P.S. i am using mysql Ver 14.12 Distrib 5.0.45, for redhat-linux-gnu (x86_64) using readline 5.0
Answer: You need to put MySQL into strict mode
In your configuration file:
sql-mode="STRICT_ALL_TABLES"

EDIT:
You can also set it locally (and put it back) using this query:
SET @mode = @@SESSION.sql_mode;
SET sql_mode = "STRICT_ALL_TABLES";

INSERT...;

SET sql_mode = @mode;

Comment: That looks good but is not an option, there are many databases on the server and we cannot risk breaking anything else.
Comment: The edit seems good, but unfortunately I will probably not be the guy doing the actual INSERTs. I am reading up on the concept, perhaps I can rewrite the specifications to allow for triggers. (triggers are problematic to copy from db to db)
Comment: I think if you can't change the SQL or the server configuration then triggers are all that's left
Answer: Set the values NOT NULL, if this dont work and you cant check the values by code before inserting the row you should put new triggers that looks for values when there is an insert or a modify call on your database.
hope this helps!
Comment: It is set NOT NULL and there is even a check constraint that should make it not be 0 as well (doesn't seem to work though)


recovering raid5 from 3 working drives, one has a read error

Question: i had 4 drives, lost one drive before, and now another drive was reported by the qnap web gui as having a read error (i did a bad block scan).
now i want to insert a new drive, in the lost drive's place, and get data on it from the remaining 3 drives.
however, one of the 3 drives has a read error.
specifically, this read error occured when the bad block scan was 98 or 99%, so i assume it wasnt alot of bad blocks?
so with this given, even if those blocks cannot be read, i assume the unreadable blocks represent a small fraction of the data that is to be lost, if we do not recover the data on the bad blocks.
how can i do this, i've read that a raid rebuild will terminate on even a single read failure?
have in mind that i see the folder and file structure intact, as before, except the filesystem is on read-only now of course, having one of the 3 drives give a read error
Comment: Apparently QNAP is Linux md based and can have a text console. Please edit your question to add detailed status of the array and the drives with the commands from https://raid.wiki.kernel.org/index.php/Asking_for_help   substituting your device names.  (Whatever type of array, we would need a similar level of detail to  help with specifics)
Answer: QNAP should be Linux based, using md and mdadm. This means it may be possible to replace the broken drive and let md reconstruct the array, skipping the unreadable parts.
However, I strongly suggest to first clone the disk with unreadable sectors via ddrescue and then to use the cloned disk (with zeroed but no unreadable sectors) in place of the first one. You will end with some corrupted stripes that will hopefully affect free space / unneeded data only.
Comment: this seems like a good idea, and in fact i am on my way to do it right now,
the only question being: how do i make ddrescue do exactly what you said here? ive read that ddrescue by default does not zero-out the bad block parts, but also that there is a fill-mode for ddrescue to make it do it, but also that that fill-mode makes the rescue-mode not work at all, so then i dont know if it will make geniune attempts to rescue my data
Comment: While `ddrescue` will not zero-fill unreadable sectors by default, in this case it should not matter because a) a new disk should already be zero-filled (or you can do that very easily) and b) zero-filled or garbage-filled, the stripe with unreadable sector will surely be corrupted anyway (so let's hope it only affect free space)


Print node tags in html header

Question: I am trying to print the articles tags in the meta area of the html. I am able to print them, however, they contain all the markup, where as I just need the plain text, comma separated list, much like it prints when using the meta module.
Here is what I am using.
function mytheme_preprocess_html(&$variables) {
if ($node = menu_get_object()) {
  if ($node && isset($node->nid)) {
  $node = node_load($node->nid);
   node_build_content($node);
   $variables['field_tags'] = render($node->content['field_tags']);
   }
 }
}

Comment: what is the content type of field_tags? do you know what the node structure looks like?
Comment: Just article for the content type.
As far as the structure, it's just title, body, tags, author, etc. Standard stuff. Hope that is what you meant.
Comment: Sorry, I meant field type (text, long text, etc), multi valued?
Answer: The reason your getting html in your output is because your using render(), which is just a wrapper for drupal_render. Per that function's documentation, it "Renders HTML given a structured array tree."
If you want just something like:
Cats, Dogs, Otters
it would probably be better to just pull out those values, ether by looping through the field_tags array and pulling out the value into an array and then imploding your array, or simply looping through and creating a string. So instead of render(), use something like:
$tags = array();
foreach($node->field_tags['und'] as $tag){
  $tags[] = $tag['value'];
}
$variables['field_tags'] = implode(',', $tags);

Comment: Thanks. Yes, I was thinking of this as well, however, I am getting a white screen of death when trying the above or a variation of it. Silly question, but can you do a foreach loop in the preprocess function?
Comment: You can. It generally helps if I don't put typos in my code though :) Sorry about that. $node['field_tags'] should be $node->field_tags. I'll update my answer.
Comment: Thanks. No more white screen of death :)
Only thing is it's not printing the tags, just
`"tags": ",,"`
Seems to be not getting the data? Here is what I am using so far:
`function mytheme_preprocess_html(&$variables) {
if ($node = menu_get_object()) {
if ($node && isset($node->nid)) {
    $node = node_load($node->nid);
    node_build_content($node);
    $tags = array();
        foreach($node->field_tags['und'] as $tag){
        $tags[] = $tag['value'];
        }
        $variables['field_tags'] = implode(',', $tags);

      }
  }
}
}`
Comment: Sounds like your looping though ok, it's just the value of $tag['value'] isn't set. You might wanna look into installing devel on your site, then trying dsm ($tag) from inside the for loop to see what the values in $tag are. Then switch  ['value'] with the correct index name (it's probably name or something like that if the field is a term reference)
Comment: Thanks, I was trying to add more to my comment, but ran out. I was thinking the same, need to find the proper `'value'`.
Comment: @Jance i think you can do it in one line maybe ? does this work ? `$variables['field_tags'] = implode(',', array_column($node->field_tags[LANGUAGE_NONE], 'value'));
`
Comment: Thanks Jance. Got it working by using devel.
Basically, i needed to convert the tid to use the name. So, I used the taxonomy term load function.
For anyone following, this is what I used.
`$tags[] = taxonomy_term_load($tag['tid'])->name;`

Print node tags in html header


Estou aprendendo a usar funções

Question: Estou apenas aprendendo e treinando o uso de funções. Vou tentar explicar qual era o objetivo desse código.
Fiz esse código para criar um verificador de idade. Então eu chamo a função teste() crio uma variável b para armazenar a idade digitada. Em seguida b deixa de ser um numero e passa a ser uma string.
o objetivo é criar uma pequena lista para guardar b.
Se alguém puder me dizer se oque fiz de errado eu fico grato.
function teste(a){
    b=a
    if(a>=18){
        b=`voce digitou ${b},pode entrar`
    }else{
        b=`voce digitou ${b},não pode entrar`
    }
    incluirNaLista()
}

teste(prompt())

function incluirNaLista(){
    let lista=[]
    for(i=0;i<=5;i++){
        if(i<5){
            lista[i]=b
            teste(prompt())
        }else{
            lista[i]=b
        }
        console.log(lista[i])
    }
}

Answer: Seu código possui uma recursão indireta, o que gera um loop infinito. Parece  que você até tentou colocar uma condição de parada baseada no i do for, mas não adianta, o for é irrelevante e o loop seria infinito mesmo sem ele.
Pense bem, toda vez que você chama teste, ele chama incluirNaLista, que chama teste, que chama incluirNaLista, e assim por diante... Se não estiver claro, faça um teste de mesa para entender melhor.
Já que você está estudando funções, é importante entender que elas servem basicamente para evitar repetição de código. E, uma vez que esteja usando funções, é importante que cada uma tenha uma responsabilidade clara, um nome claro, e que haja o mínimo possível de dependências entre elas. Parece simples, mas na verdade é algo que você só aprende mesmo quebrando a cara muitas vezes, na prática, aprendendo passo a passo como não cair em armadilhas que já caiu anteriormente. E, em cada projeto, reescrevendo o código várias vezes.
Também é importante entender que não repetir código (trechos idênticos de código) não significa necessariamente fazer menos operações. Muitas vezes é o contrário disso. Então, no mundo real, é preciso encontrar um equilíbrio entre as duas coisas.
Como esse seu exemplo é bem simples e sem muita utilidade prática, fica difícil sugerir qualquer código. Basicamente, a melhor forma (a mais simples) de fazer essa operação seria sem nenhuma função, pois é uma situação bem simples que você resolve sem repetir código nem operações.

Estou aprendendo a usar funções


Out of sample MASE

Question: When calculating the MASE, the original paper suggests using the in-sample naive forecast error for scaling of the out of sample forecast error.
When i use the the MAE generated by a naive forecast on the out of sample dataset however, I get a MASE which correlates more with the actual performance of the forecast in the tested period.
My understanding is that one limitation with using the out of sample naive MAE is that if the out of sample set is small, it is not reliable. This is however not the case in my application.
intuitively it seems more relevant to scale the predictions errors with the naive forecast errors of the same timeperiod.
My question:
Is it a good idea to use the MAE of the out of sample data to scale the forecast error?
ie.
$$MASE=\frac{MAE}{MAE_{out-sample, \, naive}}$$
Answer: You can use whatever benchmark you are most comfortable with in the denominator. (A frequent choice is a naive seasonal forecast in-sample.) Your approach has the advantage of comparing your forecasts to the benchmark out-of-sample, which is probably more relevant than comparing it to the benchmark in-sample, as the "normal" MASE does.
It would just be a good idea to note explicitly what denominator you are using, so other people (or you yourself in half a year) don't get confused that their calculated "plain vanilla" MASE does not match your calculation.

Out of sample MASE


Silverlight TextBlock - get text with ellipse

Question: In silverlight TextBlock, TextTrimming="WordEllipsis" is used to display ellipsis when the text is exceeding the textblock width.
Is there any way to get the display text (what is displayed in the screen) with ellipsis?
I need to place image instead of ellipsis.
Thanks
Suyambu
Answer: To set an image as background of a Ellipse, we can set an image brush as the Fill of the Ellipse.
<Ellipse.Fill >
    <ImageBrush ImageSource="dock.jpg" />
</Ellipse.Fill >

try it and reply..


building datetime from 3 integer/float columns in pandas

Question: After loading a DataFrame with Panda (from a csv) with this structure:
            startmonth  startday  startyear  endmonth  endday  endyear
caseid
1945121601        12.0      16.0       1945       5.0    27.0   1947.0
1946031101         3.0      11.0       1946      10.0     9.0   1993.0
1946110101        11.0       1.0       1946       2.0     4.0   1947.0

I am thinking how to efficiently use the first 3 & last 3 columns to generate 2 datetime columns, say 'startdate' and 'enddate.' Since there are missing values that need to be dealt with, the parse_dates & date_parser arguments in read_csv seem a bit unwieldy, so I wrote the function below.
First, I fill the NaN value so as to cast month and date from float to integer, and then string them together for parsing.
def dateparser(y=df.startyear,m=df.startmonth,d=df.startday):
    m = m.fillna(1).astype(int)
    d = d.fillna(1).astype(int)
    x = str(y) + " " + str(m) + " " + str(d)
    return pd.datetime.strptime(x, '%Y %m %d')

The resulted error message is a bit confusing, as the string format should be exactly the same with what strptime expect.
n [338]: dateparser()
Traceback (most recent call last):

  File "<ipython-input-338-917257f547ca>", line 1, in <module>
    dateparser()

  File "<ipython-input-337-41aa89124ae6>", line 5, in dateparser
    return pd.datetime.strptime(x, '%Y %m %d')

  File "/Users/Username/anaconda/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)

  File "/Users/Username/anaconda/lib/python3.5/_strptime.py", line 343, in _strptime
    (data_string, format))

    ValueError: time data 'caseid\n1945121601    1945\n1946031101
1946\n1946110101    1946\n1947022401    1947\n1947053101
        1947\n1947111001    1947\n1947120501    1947\n1947120502
1947\n1947120503    1947\n1947120504    1947\n1947120505
1947\n1947120506    1947\n1947120507    1947\n1947122001
1947\n1948032501    1948\n1948032502    1948\n1948070101
6\n2005100601    10\n

    Name: startmonth, dtype: int64 caseid\n1945121601    16\n1946031101
6\nName: startday, dtype: int64' does not match format '%Y %m %d'`

I also tried another parsing package that turns most of the datetime string in English language into datetime variable without issue:
from dateutil.parser import parse
def dateparser():
    (same function as above)
    return parse(x)

And it also results in error (ValueError: Unknown string format)...
Any thoughts on how to improve the function are much appreciated. It is also a bit strange for me that most package functions only converts string to datetime, and one needs to make integers/floats into string even if it shouldn't be that hard to directly convert numerical data into datetime formats... did I miss some obvious solutions?
Comment: Put a ```try/except``` block around the offending line and catch the ```ValueError```, in the ```except``` suite print out the function arguments and your date string - ```x```.  That should give you an idea of what is happening.  Re raise the error in the ```except``` suite if you want it to stop at that point.https://docs.python.org/3/tutorial/errors.html#handling-exceptions
Comment: thanks so much for the hint for how to debug this! I have successfully found the issue and will answer it below for reference.
Answer: Although not absolutely sure, the issue seems to be that I am trying to feed the parser a pandas Series while they expect only to take string.
In this cae, Panda's own to_datetime function can do the work.
def dateparser(y=t4.startyear,m=t4.startmonth,d=t4.startday):
    y = y.astype(str)
    m = m.fillna(1).astype(int).astype(str)
    d = d.fillna(1).astype(int).astype(str)
    x = y +' '+ m +' '+ d
    return pd.to_datetime(x)


Recuperar gateway da rede?

Question: Alguém conhece ou sabe alguma maneira de encontrar o endereço ip do gateway da rede ? O ip local eu sei que consigo recuperar com a classe InetAddress: InetAddress.getLocalHost().getHostAddress();
Answer: Tem uma classe chamada DhcpInfo dentro do pacote android.net, ele tem algumas variáveis com parâmetros da rede. Mas o problema que estes valores devem ser convertidos.
Imagem para descrever o cenário:

Código java:
package com.schogini.dhcp;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import android.net.*;
import android.net.wifi.WifiManager;

public class dhcpInfo extends Activity {
    public String   s_dns1 ;
    public String   s_dns2;
    public String   s_gateway;
    public String   s_ipAddress;
    public String   s_leaseDuration;
    public String   s_netmask;
    public String   s_serverAddress;
    TextView info;
    DhcpInfo d;
    WifiManager wifii;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        wifii= (WifiManager) getSystemService(Context.WIFI_SERVICE);
        d=wifii.getDhcpInfo();

        s_dns1="DNS 1: "+String.valueOf(d.dns1);
        s_dns2="DNS 2: "+String.valueOf(d.dns2);
        s_gateway="Default Gateway: "+String.valueOf(d.gateway);
        s_ipAddress="IP Address: "+String.valueOf(d.ipAddress);
        s_leaseDuration="Lease Time: "+String.valueOf(d.leaseDuration);
        s_netmask="Subnet Mask: "+String.valueOf(d.netmask);
        s_serverAddress="Server IP: "+String.valueOf(d.serverAddress);

        //dispaly them
        info= (TextView) findViewById(R.id.infolbl);
        info.setText("Network Info\n"+s_dns1+"\n"+s_dns2+"\n"+s_gateway+"\n"+s_ipAddress+"\n"+s_leaseDuration+"\n"+s_netmask+"\n"+s_serverAddress);
    }
}

XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.schogini.dhcp"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".dhcpInfo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
</manifest>

Para fazer a conversão:
public String intToIp(int i) {
   return ((i >> 24 ) & 0xFF ) + "." +
               ((i >> 16 ) & 0xFF) + "." +
               ((i >> 8 ) & 0xFF) + "." +
               ( i & 0xFF) ;
}

Or
public String intToIp(int addr) {
    return  ((addr & 0xFF) + "." +
            ((addr >>>= 8) & 0xFF) + "." +
            ((addr >>>= 8) & 0xFF) + "." +
            ((addr >>>= 8) & 0xFF));
}

Fonte: Programmatically getting the gateway and subnet mask details
Comment: obrigado, vou tentar realizar as modificações hoje ainda !
Comment: Wictor obrigado pela ajuda, tenho apenas uma dúvida, a linha que cria a instancia da classe WifiManager, está acusando o erro : Cannot resolve method getSystemService(java.lang.String)
Comment: Tente substituir wifii= (WifiManager) getSystemService(Context.WIFI_SERVICE); por wifii =  (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
Comment: Obrigado Wictor, seu código está funcionando. Será que você poderia me explicar esse método intoIP ? pois estou conseguindo pegar o ip do gateway, porém ele está retornando o ip de trás para frente. exemplo 184.108.40.206
Comment: Ele pega aquela numeração e converte para o formato ip, aquela tabela demonstra como isso funciona, de qualquer forma eu fiz outro método(O terceiro) invertendo a numeração, já que esta com a saída investida, e coloquei um segundo método do link que coloquei como referencia.

Recuperar gateway da rede?


What are the eigenspaces and the minimal polynomial of the "transposing about the anti-diagonal" endomorphism?

Question: Given a field $K$ of characteristics zero, let the endomorphism $f:M_2(K)\to M_2(K)$ be defined by $f\left(\begin{bmatrix}a&b\\c&d\end{bmatrix}\right)=\begin{bmatrix}d&b\\c&a\end{bmatrix}$ for every $\begin{bmatrix}a&b\\c&d\end{bmatrix}\in M_2(K).$
How can I find the eigenspaces and the minimal polynomial of $f$?
And why is $\operatorname{char} K=0$ important?

What I noticed is that $f$ is invertible (hence its eigenvalues are non-zero) and $f^{-1}=f$. But I couldn't go further than this
Answer: Let$$E_1=\begin{bmatrix}1&0\\0&0\end{bmatrix},\ E_2=\begin{bmatrix}0&1\\0&0\end{bmatrix},\ E_3=\begin{bmatrix}0&0\\1&0\end{bmatrix},\text{ and }E_4=\begin{bmatrix}0&0\\0&1\end{bmatrix}.$$Then $\{E_1,E_2,E_3,E_4\}$ is a basis of $M_2(\mathbb{R})$ and the matrix of $f$ with respect to this basis is$$\begin{bmatrix}0&0&0&1\\0&1&0&0\\0&0&1&0\\1&0&0&0\end{bmatrix}.$$Can you take it from here?
Comment: Yes, it's all correct.
Comment: The minimal polynomial of your matrix is $(\lambda+1)(\lambda-1)$ - then it's also the minimal polynomial of $f$, because it's of degree $2$, hence equal to $\chi_f$. And, the eigenspaces of your matrix are the ones of $f$. All correct? Also, one thing: $K$ is isomorphic to a subset of $\Bbb C$, provided that $|K|$ isn't larger than the continuum; still, I can see cardinality here doesn't make a difference, but is it enough (in an exam) to say "without loss of generality we may take $K=\Bbb R$"? Thanks for your help, this cleared my thoughts!
Answer: Since as you noted $f^2=1$, and $f\neq\pm1$, it follows that $X^2-1$ is indeed the minimum polynomial of $f$. The eigenvalues are $+1$ an $-1$. We have $f(A)=1\cdot A$ for $A\in$Mat$_2(K)$ if and only if $$\begin{pmatrix}d & b \\ c & a\end{pmatrix}=\begin{pmatrix}a & b \\ c & d\end{pmatrix},$$
if and only if $a=d$, so the eigenspace for $\lambda=+1$ is
$$E_1=\left\{\begin{pmatrix}a & b \\ c & a\end{pmatrix} \ | \ a,b,c\in K\right\}.$$
Similarly, the eigenspace for $\lambda=-1$ is found by studying $f(A)=-A$, which gives $d=-a$, $b=-b$, $c=-c$, hence $d=-a$, $b=0=c$ when char$(K)\neq2$:
$$E_{-1}=\left\{\begin{pmatrix}a & 0 \\ 0 & -a\end{pmatrix} \ | \ a\in K\right\}.$$
By the way, $f$ is the symplectic involution, and satisfies $f(AB)=f(B)f(A)$ in addition. Moreover $E_1$ is the set of symmetric elements with respect to the involution and $E_{-1}$ is the set of skew-symmetric elements. You can define a symplectic involution on $n\times n$ matrices whenever $n$ is even.
Comment: Very instructive and exhaustive answer, thank you!


Read Complex XML

Question: I have difficulties to read from that xml!  I think XMLDocument would help but i don't know how to get value from each element in childNode!
Especially, the childnode which has any childNode Inside like IdList.
XML format :
 <sdnEntry>

<programList>
  <program>SDNT</program>
</programList>

<idList>
  <id>
    <uid>6028</uid>
    <idType>NIT #</idType>
    <idNumber>900106267-0</idNumber>
    <idCountry>Colombia</idCountry>
  </id>
  <id>
    <uid>6029</uid>
    <idType>N0T #</idType>
    <idNumber>900106267-1</idNumber>
    <idCountry>Colombian</idCountry>
  </id>
</idList>
</sdnEntry>

Code :
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\SDN1.xml");

XmlElement root = doc.DocumentElement;

XmlNodeList sdnEntryNodeList = root.GetElementsByTagName("sdnEntry");

foreach (XmlNode sdnNode in sdnEntryNodeList)
{
    for (int row = 0; row < sdnEntryNodeList.Count; row++)
    {
        XmlNodeList programListNodeList = sdnNode["programList"].GetElementsByTagName("program");

        foreach (XmlNode programNode in programListNodeList)
        {
            program = programNode.InnerText;
        }

        XmlNodeList idListNodeList = element["idList"].GetElementsByTagName("id");

        foreach (....)
        {

        }
     }

}

The above code , is it good ? otherwise , i will take all your advice, please comment ....
How to read programList and idList in That complex XML ?
Comment: Your xml is not complex.. google for **linq to xml**
Comment: Can you post the code you are trying to use so that people can better understand your question. Also please don't use so many !!!! ;)
Comment: Isn't this the same XML I told you once before is not complex? Please show the code you're trying and tell us what specific problem you're having.
Comment: @parapurarajkumar : **ling to xml** , is it support for C# 2.0 ?
Comment: @FrancisUpton : `Sorry for "!!!" , next time i will paste code on the question , but i already comfirm that i used `XMLDocument`
Comment: @JohnSaunders : previous question , i was waiting you to answer but you didn't answer. i hope you try for me , please do me a fever. thank you.
Comment: "Favor", not "Fever". And LINQ to XML is not supported in .NET 2.0.
Answer: If you want to use XMLDocument , here is a sample for you
var programList = doc.SelectNodes("/sdnEntry/programList/program");
var idList = doc.SelectNodes("/sdnEntry/idList/id");

Comment: `var` is not supported in C# 2.0 (VS2005) .
What about the my code (above code) , am i good ? or i need to change to your code . thank you....
Comment: `programList` and `idList` got nothing after test .
Comment: It runs in my test. I think you have namespace .
Comment: if you delete yours namespace , it could run.  so ,you must add yours namespace in the code .
Answer: Use linq to xml.
http://msdn.microsoft.com/en-us/library/bb397976.aspx
Sample:
foreach(var node in doc.Elements("idList").Elements("id").Elements("uid"))

Comment: `Use linq to xml.` , is it support for C# 2.0 , VS2005
Comment: It is not .net 2.0. But, there are workaround (rather) hacks available to make ling run in .net 2.0. see this link http://stackoverflow.com/questions/2138/linq-on-the-net-2-0-runtime or you can use http://www.albahari.com/nutshell/linqbridge.aspx
Comment: DO you have another way ? hacks is not good for me . because i developer software for commercial ....
Answer: I would simplify you code with the following linq-to-xml queries.
var programs = from program in
               doc.Root.Element("programList").Descendants("program")
               select program.Value;

var ids = from id in doc.Root.Element("idList").Descendants("id")
          select new
          {
               uid = (string)id.Element("uid"),
               idType = (string)id.Element("idType"),
               idNumber = (string)id.Element("idNumber"),
               idCountry = (string)id.Element("idCountry")
          };

Read Complex XML


JFrame: how to make Transparent Scollbar

Question: I was working on some code for the last couple hours trying to make the scrollbar transparent.  I ' been able to make it transparent when i grab the scrollbar object from the scroll-pane and change its value.
any other way to do this?
JFrame Code Example
Comment: Don't put code in an image! Paste it as text in an [edit] to the question. Further tips: 1) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output.  To do that, select the text and click the `{}` button at the top of the message posting/editing form. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/).
Comment: I don't think they are transparent, but are simply the same color as the editor
Answer: Extend javax.swing.JViewPort. You can paint anything inside it
class TransparentViewPort extends JViewport
{

    public TransparentViewPort() {
        setOpaque(false);
    }

}

Then set an instance of TransparentViewPort as your JScrollPane's view port:
scrollPane.setViewport(new TransparentViewPort())

And if you want to make JScrollBar transparent look at Java JScrollBar Design
Comment: I Tried this and it just makes my Scrollpane disappear, am i doing something wrong?


How to list nodes in a view as checkboxes?

Question: For my drupal 8 website I need to create a view page with nodes of particular content type. I need to dispaly them as a list of checkboxes, and further implement a PDF manual, composed out of the "checked" nodes. Is it possible to implement it using standard Drupal tools?
Answer: You'll have to test that particular combination yourself, but there are modules that seem the best fit to try first.
To be able to select individual views results and perform actions on them you need Views Bulk Operations (VBO). You don't need to look further than that module for this part.
The tricky part is the PDF. Views PDF and Printer, email and PDF versions might have helped in D7 but they don't seem to be ready in D8.
In D8 Entity Print seems to be the rightful heir so try that one. Also I don't know how far you can get without custom code. You will probably need to use Rules to create the action that would be executed with VBO.


Non-parametric alternative to simple t-test

Question: I have five numeric variables of two populations (each of them with 60 individuals) and for each of those five variables I want to know if there is difference in the means.
I was trying to use a simple t-test for this (the t.test R function), but let me explain my concerns to see if it's possible.
One variable of one population do not pass the Shapiro normality test.
Any of the five variables passed Levene's test for 0.05, only one for 0.01 (but is the one containing the not normal distribution in one variable).
Even with all that, would it be a good choice to use the t-test to evaluate the means? What could be a non-parametric alternative that suits my problem?.
Comment: 1. Even if you had purely Gaussian data, you'd expect 5% of your samples to reject on a 5% test - if you're testing 5 variables for each of two groups, you're looking at about 40% chance of at least one rejection  2. Take a look at [Is normality testing essentially useless?](https://stats.stackexchange.com/questions/2492/is-normality-testing-essentially-useless), especially [Harvey's answer](https://stats.stackexchange.com/questions/2492/is-normality-testing-essentially-useless/2501#2501). 3. Similar comments would apply to testing equality of variance. ... ctd
Comment: ctd.. 4.  An easy way to test equality of means with a nonparametric test would be to do a permutation test but if you have heteroskedasticity you should probably be thinking more about a suitable distributional model... indeed such considerations should be entered into well before you collect your data. 5. On the other hand, if your sample sizes are the same the t-test isn't sensitive to heteroskedasticity, and in any case you really only need assume equality of variance under the null; if a variance difference is caused by the same thing causing the mean-difference, it may be a non-issue.
Answer:
The t-test does not assume normality of the dependent variable; it assumes normality conditional on the predictor. (See this thread: Where does the misconception that Y must be normally distributed come from?). A simple way to condition on your grouping variable is to look at a histogram of the dependent variable, splitting the data on your grouping variable.
Normality tests, like the Shapiro-Wilk test, may not be that informative. Small deviations from normality may come up as significant (see this thread: Is normality testing 'essentially useless'?). However, given your small sample size, this probably is not an issue. Nonetheless, it does not really matter (practically speaking) if normality is violated, but the extent to which it is violated.
Depending on how non-normal your data might be, you probably do not have much to worry about. The general linear model (of which the t-test is a part) is more robust to violations of the normality assumption than to other assumptions (such as independence of observations). Just how robust it is has been discussed on this website before, such as in this thread: How robust is the independent samples t-test when the distributions of the samples are non-normal?. There are many papers looking at how robust this method is to violations of normality, as well (as evidenced by this quick Scholar search: https://scholar.google.com/scholar?hl=en&as_sdt=0%2C33&q=t-test+robust+to+nonnormality&btnG=).
You might as well run a nonparametric test and see if your conclusions differ; it costs essentially nothing to do so. The most popular alternative is the Mann-Whitney test, which is done with the stats::wilcox.test function in R (http://stat.ethz.ch/R-manual/R-devel/library/stats/html/wilcox.test.html). There are a lot of good introductions to this test on the internet—I would Google around until a description of it clicks with you. I find this video, calculating the test statistic by hand, to be very intuitive: https://www.youtube.com/watch?v=BT1FKd1Qzjw. Of course, you use a computer to calculate this, but I like knowing what's going on underneath.
Comment: Your Answer (+1) appeared while I was typing mine. I will check out some of your references, and you may want to note my reference to a paper in the issue of TAS which arrived yesterday.
Comment: +1 Very nice summary. In your first point, you state: "The t-test does not assume normality of the independent variable; [...]". Did you mean "dependent variable"? I have never seen a statement that the independent variable, being a group indicator should be normally distributed.
Comment: @BruceET +1 on your answer, as well. Good reference in TAS, too. I'll have to check that out.
Comment: @COOLSerdash thank you for catching my mistake; I updated my answer to say "dependent"
Comment: @MarkWhite thanks a lot for your help, Now I have a much better idea. I totally agree with you, first learn how to do it by hand is really important to actually understand why and what we are doing.
Answer: Let's look at one variable at a time. As I understand it you have $n_1 =60$ observations from Population 1 which is distributed $\mathsf{Norm}(\mu_1, \sigma_1)$ and
$n_2 =60$ observations from Population 2 which is distributed $\mathsf{Norm}(\mu_2, \sigma_2).$
You want to test $H_0: \mu_1 = \mu_2$ against $H_a: \mu_1 \ne \mu_2.$
You could use a 2-sample t test. Unless you have prior experience with
such data indicating that $\sigma_1 = \sigma_2,$ it is considered good
practice to use the Welch (separate-variances) t test, which does not
require $\sigma_1 = \sigma_2.$
Specifically, suppose you have the following data:
sort(x1); summary(x1); sd(x1)
 [1]  78.0  78.5  80.1  80.9  87.2  88.8  89.0  90.1  90.7  92.6  92.9  93.7  94.5  97.3  98.3
[16]  98.3  98.6 100.5 100.9 101.1 101.8 101.9 103.2 103.4 104.0 104.1 104.6 104.9 105.1 105.4
[31] 105.8 107.2 107.6 108.1 108.1 108.2 108.7 109.6 109.6 112.0 112.2 112.7 114.0 114.1 114.7
[46] 114.8 116.6 117.0 118.0 118.4 118.6 119.2 123.1 124.1 124.7 125.5 127.4 127.7 136.4 138.2

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
   78.0    98.3   105.6   106.2   114.7   138.2
[1] 13.55809

.
sort(x2);  summary(x2);  sd(x2)
  [1]  65.3  70.1  76.1  76.8  80.9  81.3  82.4  82.5  84.9  85.0  85.6  86.6  87.7  88.6  89.4
 [16]  89.7  90.3  91.9  92.2  92.5  93.0  93.0  93.5  94.0  94.4  96.1  96.4  96.9  97.3  97.6
 [31]  98.5  98.9  99.7  99.9 100.2 101.3 101.5 101.7 103.3 103.4 103.5 103.6 104.5 104.7 106.0
 [46] 106.2 107.2 107.7 109.2 109.3 110.5 110.7 110.9 111.1 111.3 113.8 114.9 115.2 118.1 118.9

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  65.30   89.62   98.05   97.30  106.05  118.90
[1] 11.89914

boxplot(x1, x2, notch=T, col="skyblue2", pch=19)

There are no outliers in either sample and samples seem roughly symmetrical.
The notches in the sides of the boxplots are approximate nonparametric
confidence intervals, here indicating that the population medians differ.

The Welch 2-sample t test shows a significant difference. [A pooled t test would have had df = 118; because of a slight difference in sample standard deviations, the Welch test has only about df = 116.]
t.test(x1, x2)

        Welch Two Sample t-test

data:  x1 and x2
t = 3.8288, df = 116.05, p-value = 0.0002092
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
   4.304113 13.529220
sample estimates:
mean of x mean of y
 106.2117   97.2950

Now for your specific concerns:
(1) For sample sizes of 60, you should not worry about a slight departure from
normality. If you feel nonnormality may be a problem you can look at all 120
'residuals' in this model together in one normality test. (The residuals
are $X_{1i} - \bar X_1, X_{2i} - \bar X_2.$ for $i=1, 2, \dots, 60.)$
(2) Any difference in variances is taken care of by doing the Welch 2-sample t test.
(3) The nonparametric two-sample Wilcoxon (signed-rank) test could be used if you really feel data are far from normality. This is a test to see if one population is shifted from the other. (Some authors frame this as testing for a difference
in medians, but a paper in this month's The American Statistician objects
to that interpretation and takes a broader view of the test: Dixon et al. (2018), Vol. 72, Nr. 3, "The Wilcoxon-Mann-Whitney procedure fails as a test of medians.")  For my example, this test finds a significant difference between the two
populations, without assuming either population is normal.
wilcox.test(x1, x2)

    Wilcoxon rank sum test with continuity correction

data:  x1 and x2
W = 2465, p-value = 0.0004871
alternative hypothesis: true location shift is not equal to 0

(4) Addendum: A Comment and a linked Q&A mention permutation tests, so we include one possible permutation test. [For an elementary discussion of permutation tests, perhaps see Eudey et al. (2010), especially Sect. 3.]
Below is R code for
a permutation test using the pooled t statistic as 'metric'. If the two groups
are the same it should not matter if we randomly scramble the 120 observations
into two groups of 60. We recognize the pooled t statistic as a reasonable
way to measure the distance between two samples, but do not assume that statistic has Student's t distribution.
The code assumes data x1 and x2 are present, does the scrambling with the function sample(gp), and (conveniently, but somewhat inefficiently) uses t.test()$stat to get the t statistics of the permuted samples. The P-value 0.0003 indicates rejection
of the null hypothesis. (Results may vary slightly from one run to the next.)
all = c(x1, x2);  gp = rep(1:2, each=60)
t.obs = t.test(all ~ gp, var.eq=T)$stat
t.prm = replicate( 10^5,  t.test(all ~ sample(gp), var.eq=T)$stat )
mean(abs(t.prm) > abs(t.obs))
[1] 0.00026

The figure below shows a histogram of the simulated permutation distribution. [It happens
to match the density curve (black) of Student's t distribution with 118 degrees of freedom rather well, because data were simulated as normal with nearly equal SDs.] The
P-value is the proportion of permuted t statistics outside the vertical dotted lines.

Note: My data were generated in R as follows:
set.seed(818)
x1 = round(rnorm(60, 107, 15), 1); x2 = round(rnorm(60, 100, 14), 1)

Comment: The permutation test example makes this really helpful. In R, permutation/randomization tests are really easy to do (even without using extra features in add on packages that make it even easier); it surprises me that people don't do them more often.
Comment: thanks a lot, I've a concern about the homogeneity of variance.
Comment: @Glen-b: Thanks. Yes, I'm a big fan of permutation tests. One precaution, especially for small $n,$ is to use something like `length(unique(t.perm))` at the end to make sure there are meaningfully many values.
Answer: One thing to keep in mind- outside of some contexts in physics, no process in nature will generate purely normally distributed data (or data with any particular nicely behaved distribution).  What does this mean in practice?  It means that if you possessed an omnipotent test for normality, the test would reject 100% of the time, because your data will essentially always only be, at best, approximately normal. This is why learning to ascertain the extent of approximate normality and its possible effects on inference is so important for researchers, rather than relying on tests.


On opening a Google Sheet, how can I activate the 1st empty cell in column1?

Question: Volunteers sign in for work by scanning a QR code on their ID cards. The scanned data is entered into a Google Sheet. When the sheet is opened each morning, the first volunteer has to remember to click on the first empty cell in column 1, before scanning his ID card.
How can I automate this, so when the sheet is opened the first empty cell in column 1 is activated/selected and ready for the first scan of the day?
I am very inexperienced with script for google apps. I tried modifying code I found online, but it won't run.
var sheet
var data

function onOpen(){
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.DataRange().getValues();
  for (var i = 0;i < data.length;i++) {
    if (data[i][1] === "") {
       break;
    }
  }
  sheet.getrange(i+1, 2).activate();
}

The script doesn't run, but returns this error message.

TypeError: Cannot find function DataRange in object Sheet. (line 9,
  file "1st Open Cell, Column1"
Comment: It should be `getDataRange()`. However, Cooper's answer will be faster.
Answer: Here is the code I tested and worked as you want. I used getNextDataCell function [1] to find the last cell with data for column A, and then I used offset function [2] to get the next cell in the column A (empty cell).
function onOpen(){
  var sheet = SpreadsheetApp.getActiveSheet();
  //Find the last cell with data in that specific column (A in this case)
  var lastCell = sheet.getRange("A1").getNextDataCell(SpreadsheetApp.Direction.DOWN);
  //Activate the next cell
  lastCell.offset(1, 0).activate();
}

[1] https://developers.google.com/apps-script/reference/spreadsheet/range#getnextdatacelldirection
[2] https://developers.google.com/apps-script/reference/spreadsheet/range#offsetrowoffset-columnoffset
Comment: Thanks Andres! This is very close. I had to change "A" to 'A' to make it run. However, it activates the first non-hidden cell in column A, rather than the first empty cell. Any ideas to fix that?
Comment: You mean you want to activate the first hidden empty cell?
Comment: No. Whenever the sheet is opened, the first visible cell below the Column A header is activated, regardless of whether that cell contains data. So if rows 4-20 are hidden, the script activates cell A21. Does it matter that the data is text?
Comment: I just tried your script on another sheet and it works perfectly there. Now looking for differences between the sheets.
Comment: I don't understand. May be if you share an example of the spreadsheet and explain the case, editing the question. Apps Script functions don't take hidden cells into account.
Comment: Andres, thanks for your help. Your script worked perfectly on other spreadsheets, but not on the one where I planned to use it. So, I recreated that spreadsheet from scratch and now the script is working there too.
Answer: Try:
function onOpen() {
  var sheet=SpreadsheetApp.getActiveSheet();
  sheet.getRange(1,sheet.getLastRow()+1,1,sheet.getLastColumn()).activate();
}

Comment: Thanks, Cooper. I got the other one working after recreating the sheet. Not sure where the bug was, but everything pointed to that particular sheet. I'll keep a copy of your script in case the problem reappears.


User course listings with contact forms

Question: I'm very new to drupal and trying to get my head around how things are setup.
I'm attempting to create a training course listings website where a user can join as a course provider and add listings for upcoming courses. I have this ability sorted but, I'm struggling to find a way for visitors to fill in a contact form attached to the course listing node, which will go into an admin panel for the main administrator. Once approved by the admin an email alert will be sent to the listing's author/course provider and they can go into their own panel to view the message with a link to the course where it was submitted. I have played around with webforms, panels but as far as I can tell all messages get attached to the form and not the node itself. Hope this makes sense :)
If you know of any guides, tutorials on this type of setup or if you could advice in any way that would be great, thanks
Answer: FYI - asking for guides and tutorials is off-topic here.  If I was to attempt this, I would first try something like this:

create a content type with the relevant fields you want a prospective user to fill out
create a hidden field that includes the email address of the course provider
include another "hidden" field using the Field Permissions module something along the lines of "approved" so that only Admin can access or view this field. This would be a boolean type field
create a custom View visible only to Admin that shows ONLY that content type, filtered by those where the approved field is false

The Admin can then go through each item, check and set the 'approved' field to true if appropriate

Create a Rule, based on that content being saved that checks the value of the approved field - if it has changed and is now true, send an email using the hidden email field to the course provider

It's pretty involved, but should work.
Field Permissions

The Field Permissions module allows site administrators to set
  field-level permissions to edit, view and create fields on any entity.

Views

You need Views if
You like the default front page view, but you find you want to sort it
  differently. You like the default taxonomy/term view, but you find you
  want to sort it differently; for example, alphabetically. You use
  /tracker, but you want to restrict it to posts of a certain type. You
  like the idea of the 'article' module, but it doesn't display articles
  the way you like. You want a way to display a block with the 5 most
  recent posts of some particular type. You want to provide 'unread
  forum posts'. You want a monthly archive similar to the typical
  Movable Type/Wordpress archives that displays a link to the in the
  form of "Month, YYYY (X)" where X is the number of posts that month,
  and displays them in a block. The links lead to a simple list of posts
  for that month. Views can do a lot more than that, but those are some
  of the obvious uses of Views.

Rules

The Rules module allows site administrators to define conditionally
  executed actions based on occurring events (known as reactive or ECA
  rules). It's a replacement with more features for the trigger module
  in core and the successor of the Drupal 5 workflow-ng module.
Comment: thanks Geoff you have given me a great head start and a lot to digest, will keep me busy for a while :) and from now on I'll be more specific with any additional questions, thanks for the heads up.

User course listings with contact forms


How do I show a custom module settings page as tab of another existing settings page?

Question: I am developing a custom module for Drupal 8.0.1 which shows a custom form in admin/config/people/ban/settings.
admin/config/people/ban is the routes used by the Ban module. admin/config/people/ban/settings should be shown as Settings tab in that route, while the settings for the Ban module are shown in the Default tab.
How can I achieve this in my module?
Answer: You need a local tasks menu link yml file, see example in node module: node.links.task.yml
Comment: This is my module.links.menu.yml file
Comment: Can you provide a working example?
Answer: For this you need to create a route in the module.routing.yml file
route_name:
  path: 'admin/config/people/ban/settings'
  defaults:
    _form: '\Drupal\modulename\Form\FormController'
    -title: Form Title
  requirements:
    _permission: 'your permission string'

then you need to create another yml file with the name mymodule.links.menu.yml
add the following code
form_config_link:
  title: Configure Form Title
  description: 'Configuration Form description'
  parent: system.admin_config_people
  route_name: route_name //defined above

You need to check system module yml file for parent name
Answer: The trick is that always need at least two local tasks for local tasks to show up. They don't do if there's only one.
So, define a yourmodule.menu.task.yml and put two definitions in there. Default, which has the existing ban route for both base_route and route_name and yours, which has your route_name and the same base_route as the the default task.
What used to be the default task in Drupal 7 is now simply the task that has base_route == route_name.


Advagg with S3fs relative url() references inside of CSS files - broken image URL

Question: Background Images Paths are Changed when CSS Aggregation is On
s3fs module with AdvAgg to store CSS/JS aggregates on the S3 stream wrapper
Without Aggregation (Works well). fetch from domain base url.
background-image: url(/themes/THEME/images/test.jpg);

Src of the image in CSS, which is pointing to the CNAME for S3
With Aggregation isn't working. Because, my CNAME is different from my DOMAIN name.
background-image: url(http://CNAME_DOMAIN/themes/THEME/images/test.jpg);

I disabled it all OBSCURE options(admin/config/development/performance/advagg). But No luck.
Everytime, the CSS file content url() fetching from the CNAME url.
Answer: I found the same issue and it was the absolute path that broke things https://www.drupal.org/project/s3fs/issues/3241554
Had a very similar structure:
background-image: url(/themes/THEME/images/test.jpg);
Convert this to:
background-image: url(/../images/test.jpg);
(Or whatever relative path that makes sense) and it should work (thanks to @cmlara for the pointer!)
Comment: Still, i have same issue.


Null space of A and Null space of A Transpose

Question: Let $A$ be $m \times n$ matrix and after we gone through some elementary row operations we get $U$ which is Reduced row echelon form of $A$. Then can we say that $\text{Null}(A^T) = \text{Null}(U^T$)?
Comment: or should i first take A to transpose of it then find rref. As i tried on some matrices some of them gave me the same result but it seems it was lucky. I first need to take transpose.
Comment: Elementary row operations do not change the nullspace. Elementary column operations do not generally preserve the nullspace (that is, they will usually change it). Since $U^T$ is obtained from $A^T$ via elementary column operations, you simply cannot expect them to have the same nullspace.
Answer: Let $A=\left(\begin{array}{cr}1&1\\1&1\end{array}\right)$. Its row echelon form is $U=\left(\begin{array}{cc}1&1\\0&0\end{array}\right)$.
The nullspace of $A^T=A$ is $\{(x,-x)^T\mid x\in\mathbb{R}\}$.
The nullspace of $U^T=\left(\begin{array}{cc}1&0\\1&0\end{array}\right)$ is the set $\{(0,y)^T\mid y\in\mathbb{R}\}$.


Use Webform to email users with query parameter or argument?

Question: I want to be able to have a contact form that lets visitors send email to different users (like a staff directory) without exposing the email address or user id. I am going to come up with my own way of identifying users to not expose Drupal specific information. Anyway.
However, I need to:

Pass that value in the URL to identify the user to locate, to know where to send the mail
Load and check the user from that passed value before showing the form
If the user is not a certain role, serve a 404 instead of a form (only certain roles can be contacted).
Preferably have a url like /staff/{FOO}/contact where {FOO} is my custom value.

Would it be easier to just implement this myself, or rely on Webform? I wanted to take advantage of Webform in case the client wanted to add another field later or create other forms (more basic ones), throttle repeat submissions and the dozens of other things Webform can do.
Comment: I think there needs to be a webform_contact.module which mirrors how the contact.module adds a contact form to every user profile.
Comment: All custom module Vs custom webform integrated module. As far as "what is easier" depends on your Drupal 8 knowledge.
Answer:

Pass that value in the URL to identify the user to locate, to know where to send the mail
Load and check the user from that passed value before showing the form
If the user is not a certain role, serve a 404 instead of a form (only certain roles can be contacted).
Preferably have a url like /staff/{FOO}/contact where {FOO} is my custom value.

If you want to make a webform integrated module, you can achieve all of the above by altering certain webform's route(s) & maybe service(s).


Aparecer um gif de loading ao clicar em um link para gerar um PDF

Question: Tenho um sistema do qual o usuário ao clicar em um link conforme abaixo abre um arquivo PDF:
<?php
  while($listar = mysqli_fetch_object($sql)){
?>
    <a href="#!" data-id="<?php echo $listar->IdCodCarteiras; ?>" id="link" style="color: #000">
<?php } ?>

Esse link é direcionado para uma página  em PHP onde gera o arquivo PDF através das informações passadas pela variável global $_REQUEST["key"], porém gostaria de saber como faço para que enquanto o arquivo PDF é gerado, apareça um gif de loading ao lado do link clicado pelo usuário.
Pensei em fazer algo assim, mas não tenho muito conhecimento com jquery:
$('#link').click(function(){
  $('#loading').html('');
  var id = $(this).attr('data-id');
  $.post('processar-pdf.php', {key: id}, function(retorno){
  });
});

Mas não sei se está correto.
Comment: Sua pergunta parece ter alguns problemas e sua experiência aqui no Stack Overflow em Português poderá não ser a que melhor por causa disto. Queremos que você se dê bem aqui e consiga o que quer, mas para isso precisamos que você faça sua parte. Veja algumas orientações que vão te ajudar: [Guia de sobrevivência do Stack Overflow em Português](https://pt.meta.stackoverflow.com/q/8045/101).
Comment: Olá Maniero. Ajustei minha pergunta. Acredito que agora ficou mais claro minha dúvida.
Answer: Existem várias formas de resolver isso, mas vou separar duas opções..

A primeira seria aparecer o loading por um determinado tempo, geralmente o tempo medio que demora essa contrução de carteira. (o que não é tão recomendado, mas é simples de resolver)
A segunda seria enviando uma informação de callback com o tempo de duração do processo (semelhante a quando você vai apagar ou mover uma pasta no seu computador, geralmente aparece, por exemplo: 4 minutos restandtes 130kb de 1000kb.

No primeiro caso você pode fazer isso utilizando javascript:

$( document ).ready(function() {
    $("#build").click((e)=>{
     e.preventDefault();
     $("#build").hide();
      $("#build-loading").show();
      setTimeout(()=> {
       $("#build").show();
        $("#build-loading").hide();
      }, 5000)
    })
});
.container {
  width: 100%;
  height: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.container-img {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

p, img {
  margin: 0;
}

img {
  margin-bottom: -50px
}

label {
  z-index: 1;
  margin-left: 5px;
}

#build-loading{
  width: 100px;
  display: none;
}

<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css" rel="stylesheet" />

    <a id="build" href="../../../carteiras/?key=<?php echo $listar->IdCodCarteiras; ?>" style="color: #000">

    Construindo...

Basicamente foi feito a ocultação e a desolcutação de itens no html, o qual foi retomado após um tempo que seria o tempo de construição.
Repare que o parametro do setTimeout é o tempo que dura a contrução em milisegundos.No exemplo foi colocado que o tempo de construção demoraria 5 segundos, use essa ferramenta para descobrir o tempo de segundos em milisegundos.
Agora para simular um callback do servidor, vou usar este json

var continuar = 0;
$( document ).ready(function() {
    $("#build").click((e)=>{
     e.preventDefault();
     $("#build").hide();
      $("#build-loading").show();

      simulandoCallbackApi();
    })
});

const simulandoCallbackApi = () => {
  setTimeout(() => {
    $.ajax({
      method: "GET",
      url: "http://www.json-generator.com/api/json/get/cfeUYUyUEi?indent=2",
    }).done(function(data) {
       console.log(data);
       if(data.success){
        $("#build").show();
        $("#build-loading").hide();
       }
    });
  }, 1000);
}
.container {
  width: 100%;
  height: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.container-img {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

p, img {
  margin: 0;
}

img {
  margin-bottom: -50px
}

label {
  z-index: 1;
  margin-left: 5px;
}

#build-loading{
  width: 100px;
  display: none;
}

<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css" rel="stylesheet" />

    <a id="build" href="../../../carteiras/?key=<?php echo $listar->IdCodCarteiras; ?>" style="color: #000">

    Construindo...

Logo a função simulandoCallbackApi representa o processo sendo feito dentro do seu php, e assim que for finalizado é apresentado o botão novamente.
Comment: Muito obrigado Júlio. Usei a segunda opção!