Encontrar caractere na string

Question: Como encontrar a posição de uma determinado caractere, sendo que a string possui muitos destes caracteres na mesma string, exemplo: encontrar a 3ª letra X na string Y. alguma forma de realizar isto?
Answer: usa strpos http://php.net/manual/pt_BR/function.strpos.php
sendo que vc pode fazer um loop passando o $offset de onde a função deve começar a buscar

Algo assim
<?php

$texto = "um texto muito longo cheio de letras, numeros, fatos, historias, aventuras e altas confusões na sessao da tarde.";
$achar = "a";
$posicoes = array();
$offset = 0;

while ( ($pos = strpos($texto, $achar, $offset)) !== false) {

    $posicoes[] = $pos;
    $offset = $pos+1;
}

print_r($posicoes);

Update
se você quiser também pode usar expressões regulares, o que na minha opinião é bem mais elegante:
<?php

$re = "/a/";
$str = "um texto muito longo cheio de letras, numeros, fatos, historias, aventuras e altas confusões na sessao da tarde.";

preg_match_all($re, $str, $resultados, PREG_OFFSET_CAPTURE);

print_r($resultados);

Comment: mas como poderia encontrar a 3ª letra X na string Y, sendo que não sei a quantidade e nem a posição da letra X na String?
Answer: Isso pede uma função especializada. Bem, desenvolvi rapidamente a seguinte:
function nthstrpos($haystack, $needle, $nth) {
    $count = 0;
    $pos = -1;
    do {
        $pos = strpos($haystack, $needle, $pos + 1);
        $count++;
    } while ($pos !== false && $count < $nth);
    return $pos;
}

A função nthstrpos() recebe três parâmetros:

Texto a ser pesquisado (palheiro)
Texto a ser procurado (agulha)
Número da ocorrência a ser localizada (n-ésima ocorrência do parâmetro anterior), sendo 1 a primeira ocorrência e assim por diante

Por exemplo, o comando nthstrpos('banana', 'a', 2) vai retornar a segunda ocorrência da letra a no texto banana.
Caso o texto a ser procurado não seja encontrado ou o número da ocorrência solicitada exceda o total de ocorrências existentes o retorno será false.
Execute no Ideone
Comment: Funciona perfeitamente, Obrigado
Answer: Utilize a função strpos. Seus parâmetros são:

$haystack: A string na qual será feita a busca.
$needle: Caracteres a serem buscados.
$offset: Esse parâmetro permite definir a partir de qual caractere em $haystack iniciar a busca.

Podemos usar este último parâmetro para fazer esse tipo de busca, assim:
function strposNth($texto, $procurar, $n){
    switch($n){
        case $n === 0:
            return false;
            break;
        case $n === 1:
            return(strpos($texto, $encontrar) + 1);
            break;
        default:
            return(strpos($texto, $procurar, strposNth($texto, $procurar, $n - 1) +
            strlen($procurar)) + 1);
            break;
    }
}

Para usá-la:
echo strposNth("overflow", "o", 2); // 7

DEMO
Comment: mas como poderia encontrar a 3ª letra X na string Y, sendo que não sei a quantidade e nem a posição da letra X na String?
Comment: @RicardoHenrique Melhorei a resposta.
Answer: $test = "Hi, How are you!";

function strCharFind($needle,$haystack){
    $return = FALSE;
    $arr = str_split($haystack,1);
    foreach($arr as $value){
        if($value==strtolower($needle)||$value==strtoupper($needle)){
            $return = TRUE;
        }
    }
    return $return;
}

var_dump(strCharFind(',',$test));//true
var_dump(strCharFind('h',$test));//true
var_dump(strCharFind('!',$test));//true
var_dump(strCharFind('?',$test));//false

Comment: Saudações, Andre. É legal que queira contribuir com a comunidade, mas seria interessante você adicionar explicações à sua resposta. Um código que pode ser trivial para você pode não ser para os outros usuários, então busque sempre explicar por texto a lógica implementada.

Encontrar caractere na string


Find a function that makes this differential form exact

Question: We have $\Omega=\mathbb{R^3}\backslash \left\{ (0,0,z):z\in \mathbb{R}\right\}$ and $\omega$ the differential form:
$$\omega :=\left(\frac{4x^2+2zx}{x^2+y^2}+2A(x,y)\right)dx+\left(\frac{2y}{x^2+y^2}(2x+z)\right)dy+A(x,y)\,dz,$$
where $A\in C^1(\mathbb{R^2}\backslash(0,0); \mathbb{R})$
Find a function $A$ that makes the differential form exact.
My teacher advised to use the homotopy of the curves of which I make the integral. Namely find two curves on which the integral is $0$, one that surrounds the origin and the other doesn't, and hence obtaining all the other curves through continuous deformation. For the curves that don't include the $z$-axis I can just take a ball that contains the curve, and since the ball is convex, I know that a closed differential form in a open convex set is exact (I'm pretty sure it is closed). I'm stuck on the curves that surround the $z$-axis, any ideas?
Comment: Isn't the task just one of solving a differential equation? For the form to be exact you need some $\beta$ such that $\omega = d \beta$. For this to hold you need a function $\beta$ such that $\beta_{,x} = (4x^2+2zx)/(x^2+y^2) + 2 A(x,y)$, $\beta_{,y} = ...$. I think this means we need $A(x,y) = \log(x^2+y^2) + c(x)$ for some differentable function $c$.
Comment: Makes sense, although I've had the impression that my professor wanted us to first proof that the form is exact and then find the primitive, this way looks interesting. How would you find $c$?
Comment: @John: Arthur is telling you how to choose $A$ so that $\omega$ has a primitive. If you have a primitive for $\omega$, then perforce $\omega$ is exact....
Answer: You must first verify that your form is at least closed, that is $d\omega = 0$. If I did everything well, after a lengthy calculation you'll get $A= \log(x^2+y^2)+C$. Finally check that $\int_C \omega = 0$ for some curve sourronding the $z$-axis, I chose the curve $C = (\cos t,\sin t, 0)$ for $0\le t\le 2\pi$, because the integral seems easy to evaluate. Since the interal is zero, your form is exact.
---- Edit -------------
To answer the question of @JBond007, let me write two proofs.
(i) For a closed 1-form $\omega$ we are tempted to define the solution as $f(p)=c+\int_q^p\omega$, for some fixed point $q$. If two curves $C_1$ and $C_2$ join $q$ and $p$, and one of them can be deformed into the other, then they can be seen as the boundary of a surface $S$ and by Stokes theorem $\int_{C_1-C_2}\omega = \int_S d\omega = 0$. The problem here is that $M=\mathbb{R}^3-\{z \enspace\text{axis}\}$ is not simply-connected; however $C_1-C_2$ can be deformed into some loop $C = (\cos t,\sin t, 0)$, for $t\in[0,2\pi n]$, that turns $n$ times around the $z$ axis (smash $C_1-C_2$ into the plane $\{z=0\}$ and then contract to $S^1$), so by Stokes Theorem again $\int_{C_1-C_2}\omega=\int_C\omega$. Hence, if the integral around $C$ is zero, then there is no obstruction to define $f(p)=c+\int_q^p\omega$, since the definition does not depend on the curve joining $p$ and $q$.
(ii) You can use an exact sequence. Write $M=U_1\cup U_2$, where $U_1:=\mathbb{R}^3-\{x\ge 0 \text{ and } y=0\}$ and $U_2:=\mathbb{R}^3-\{x\le 0 \text{ and } y=0\}$. Both $U_1$ and $U_2$ are simply-connected, so you can find a solution $\omega = df_i$ in $U_i$. In $U_1\cap U_2$ we have that $0=df_1-df_2$, which implies that $f_1-f_2$ is constant in each connected component of $U_1\cap U_2 = \{y>0\}\cup\{y<0\}$, say $c_+$ in $\{y>0\}$ and $c_-$ in $\{y<0\}$. If $c_+=c_-=c$ then the solution $f = f_1$ in $U_1$ and $f = f_2+c$ in $U_2$ is well defined in $M$. The integral of $\omega=df_1$ on the half-loop $C_1=\{(\cos t,\sin t, 0)\mid t\in[\pi/2,3\pi/2]\}\subset U_1$ is $f_1(0,1,0)-f_1(0,-1,0)$, and the integral on the other half-loop inside $U_2$ is $f_2(0,-1,0)-f_2(0,1,0)$. Hence, if $\int_C\omega = 0$ then $0=(f_1(0,1,0)-f_2(0,1,0))-(f_1(0,-1,0)-f_2(0,-1,0))=c_+-c_-$, and $c_+=c_-$.
Comment: Why the fact that $\int_C\omega=0$ assure me that $\omega$ is exact?


What causes this fractal to curl and unwind?

Question: During my regular recreational late night Desmos foolery, I came across this fractal parametric equation:
$$x(t)=\sum_{n=0}^\infty \frac{\cos(2^nt+cn)}{2^n}$$
$$y(t)=\sum_{n=0}^\infty \frac{\sin(2^nt+cn)}{2^n}$$
And decided to animate $c\rightarrow2\pi$ on a loop.

I was fascinated by the series of loops around it's boundary that seemed to curl into existence then proceed to unwind back into thin air. If the gif doesn't do it justice, I highly recommend checking out the link to the graph.
I'm sure this phenomenon is actually just an example of a much simpler process, but I can't seem to wrap my head around these spiraling loops. What are your thoughts?
Comment: The problem is that you should translate this phenomenon into mathematical language, rather than a description such as "curl into existence" or "unwind back into thin air".
Comment: @WhatsUp I agree. Believe me, if I could, I would. Otherwise, who says poetry and math don't overlap ;)
Comment: Have you tried playing with your parameters? E.g. changing some of the $2$'s to $3$'s or change some of the $2^n$ to $n^2$. I'd say there is nothing special about your function. It's just a "circle" which deforms in a cyclic way.
Comment: this is the image of the unit circle under the function $f(z)=\sum_ {n=0}^\infty 2^{-n} z^{2^n+cn}$, which is a lacunary series
Comment: @WhatsUp I have, indeed I'm sure there's nothing special, as per my "I'm sure this phenomenon is actually just an example of a much simpler process". I'm curious to as what the cyclic deformation is actually a result of however.
Answer: This should really be a comment but it's way too long:
As the comments indicate, all we really have so far is a qualitative observation. It's quite interesting, but turning it into a precise question is nontrivial, and of course we have to do that before we can get an answer.
So let me take a stab at precisiating the phenomenon we see - or rather, precisiating one particular question about whether a general pattern we seem to observe actually holds. Specifically, I want to ask: "Do all the loops roll clockwise?"
There are a couple things we need to pay attention to:

We need a precise definition of "loop," and we then need a way to talk about the motion of a loop over time.

Whatever we whip up should be parameterization-invariant: we're really paying attention to the map $$\Gamma:c\mapsto\{(x,y):\exists t(x=\sum_{n=0}^\infty \frac{\cos(2^nt+cn)}{2^n}\quad\mbox{and}\quad y=\sum_{n=0}^\infty \frac{\sin(2^nt+cn)}{2^n})\},$$ or if you prefer "successive horizontal slices" of the surface $$\{(x,y,z):\exists t(x=\sum_{n=0}^\infty \frac{\cos(2^nt+zn)}{2^n}\quad\mbox{and}\quad y=\sum_{n=0}^\infty \frac{\sin(2^nt+zn)}{2^n})\}\subseteq\mathbb{R}^3.$$

So our definition will probably use the function $$F(t,c)=(\sum_{n=0}^\infty \frac{\cos(2^nt+cn)}{2^n},\sum_{n=0}^\infty \frac{\sin(2^nt+cn)}{2^n})$$ but it will ultimately "lose information."

OK, so let's start with the notion of a loop at time $c$. There are a couple things this could mean.
From algebraic topology we have the simple notion of a continuous function $S_1\rightarrow\Gamma(c)$ (or rather, an appropriate equivalence class of such functions). This notion is quite nice; for one thing, it produces a groupoid for each $c$, and so overall a "continuously varying groupoid."
However, that's a bit abstract; more importantly, it ignores details like direction of movement, which we definitely care about here. So while it's a very natural thing to look at, I'm not sure it's quite the right idea here.
Instead, I want to go a bit more concrete:

A loop at time $c$ is just a pair of numbers $a<b$ such that $F(a,c)=F(b,c)$ but for each $z\in (a,b)$ we have $F(z,c)\not=F(a,c)$. (Note that this does refer to the parameterization; that will go away later, however. The key feature here is that the parameterization $F$ is "locally injective in $c$," that is for each $t,c$ there is some $\epsilon>0$ such that $F\upharpoonright (t-\epsilon, t+\epsilon)\times\{c\}$ is injective.)

To talk about the motion of a loop over time, we'll introduce the notion of a bubble. Basically, a bubble is a "continuously varying nontrivial loop" which is maximal in the sense that if we could keep going either forward or backward in $c$ we do so. More precisely, we'll say that a prebubble consists of a tuple $(I,f,g)$ where $I$ is an open interval (which is allowed to extend infinitely in one or both directions) and $f,g$ are injective functions defined on $I$ such that $\langle f(c),g(c)\rangle$ is a loop for all $c\in I$. A prebubble doesn't necessarily "tell the whole story" of a loop's lifetime, and so we further define a bubble to be a prebubble $(I,f,g)$ such that there is no other prebubble $(\hat{I},\hat{f},\hat{g})$ with $I\subsetneq I'$ and $\hat{f}\upharpoonright I=f, \hat{g}\upharpoonright I=g$.

Now suppose I have a bubble $\beta:=(I,f,g)$. At each time $c\in I$ there is a "special point" in this bubble, $p_\beta(c)=F(f(c),c)=F(g(c),c)$. And via some polar coordinate tedium the following question can be phrased precisely:

If $\beta=(I,f,g)$ is a bubble, need $p_\beta(c)$ always be moving clockwise as $c$ increases?

(The tedium is basically that the argument of a point is many-valued. It's easily dealt with, though.)
Note that as desired, at this point everything is parameterization-free - the above works as long as we fix any parameterization which is locally injective in $c$.

Now that we have a precise question, I'm sure finding the answer will be basically trivial so I'll leave that as an exercise to the reader. :P


Confidence interval for distance from center

Question: We have a bivariate normal process where $X \sim N(\mu_x, \sigma), \, Y \sim N(\mu_y, \sigma)$, with no covariance.
$(\mu_x, \mu_y)$ are unknown.
(For convenience we can assert that $\sigma = 1$, or that we have a good estimate for its value.)
We are trying to characterize the distance between our sample center and the true center $(\mu_x, \mu_y)$ as a function of shots sampled n.
Because we don't care about the location of the true center, only our distance from it, we assert that $\mu_x = \mu_y = 0$ and look at the random variable $R(n) =  \sqrt{\overline{x_i}^2 + \overline{y_i}^2}$ -- the distance between sample center and true center.
Question: How can we characterize the confidence interval of R(n)?
Note that $R(n) \ge 0$ and $E[R(n)] \to 0$ as $n \to \infty$
I have Monte Carlo estimates of both the mean and standard deviation of R(n) for small n.
I want to calculate confidence levels and intervals for R(n).  I.e., given n and confidence level 90% what is the confidence interval of a sample R(n) about its population mean?
I don't believe this is amenable to CLT analysis because the values are bounded at 0.
I suppose I could Monte Carlo the edf since I'm only interested in $n \in [2, 30]$, and the edf must scale with $\sigma$ or $\sigma^2$.  But first I want to make sure I'm not missing something obvious or a known closed-form expression.
Comment: Confidence intervals, by definition, apply to *parameters*.  "R(n)" (whatever it might mean) does not appear to be a parameter.  Are you perhaps asking for confidence intervals for $\sigma$ (which appears to be the only parameter in the question)? Please edit your question to clarify this point.
Comment: We're looking at gunshot impacts on a target, assuming they're distributed as an independent bivariate normal.  We want to "sight in" a gun by taking shots to estimate where the center of impact is.  It's common to take only 3 "sighting shots" and use the center of those three points as the estimate of the true center.  We want to characterize how far that is on average from the true center, and how that distance shrinks as we take more sighting shots.  So, we have _n_ shots with an _x_ and _y_ value, and we compute a sample center _C_ from those.  R(n) is the distance of _C_ from the origin.
Comment: Just as a wrap up for statisticians: It the following correct? You want to find a confidence interval for the parameter $\theta := \sqrt{\mu_x^2+\mu_y^2}$, where $(\mu_x, \mu_y)$ is the (unknown) center of spherical normal with known $\sigma$?
Comment: Asserting $E[R(n)]$ goes to zero with large $n$ means, *for certain,* that the gun sights are absolutely dead-on accurate. I do not see how you can possibly know that; in fact, it seems to me that is the principal question you are addressing when you conduct this kind of study. Declaring $(\mu_X,\mu_Y)$ to be the origin doesn't work, *because you do not know these coordinates*! You have to use an origin you do know, such as the target center. The two big questions are (1) accuracy: what is $\sqrt{\mu_X^2+\mu_Y^2}$ and (2) precision: what is $\sigma$? *These* are what you need CIs for.
Comment: Possible duplicate of [Distribution of distance from center of sample group](http://stats.stackexchange.com/questions/95873/distribution-of-distance-from-center-of-sample-group)
Comment: I thought s/he's trying to figure out what's the distribution of R(n)
Comment: R(n) is the distance from the center estimated by _n_ samples of the bivariate random variables {x, y}.  Given _n_ I should be able to say that I have _K_ confidence that R(n) is within _E_ of the origin (which is the true center in this model).  Right?
Comment: R(n) is a statistic based on your data: it is what it is; one does not compute a confidence interval for it. It sounds like you might be in a situation where $X\sim N(\mu_X,\sigma)$ and $Y\sim N(\mu_Y,\sigma)$ with unknown $\mu_X, \mu_Y,$ and $\sigma$. You seek a confidence region for $(\mu_X,\mu_Y)$ based on $n$ independent observations of $(X,Y)$. But I'm only guessing, and since you're struggling with the terminology it would be much better for you to re-ask your question using your own language. Why not tell us what $X$ and $Y$ really are and explain what you want to learn from them?
Comment: Okay! My previous comment describes your situation, *because you do not know beforehand where the shots will tend to land.* You want to estimate that point (that is, the parameter $(\mu_X,\mu_Y)$) and compare it--relative to the standard error of estimation--to the target's origin at $(0,0).$ This is a crucial point because your confidence region needs to be based on the *normal* distribution of $(X,Y)$ and not on the $\chi$ distribution suggested by @Aksakal. But, once again, I could be misunderstanding...
Comment: Could you edit your question to reflect this information? Could you also explain how you know *a priori* that the variance in the X and Y directions is the same?
Comment: Just tried to incorporate the clarifications.  Willing to keep trying until I've got it right!  Regarding the bivariate model: I'm just asserting equal variance and no covariance for this analysis.  That does turn out to be a valid model for a lot of real-world cases I've analyzed.  And once I nail this I try to expand to the more complicated bivariate forms.
Comment: I believe that's it!
Comment: With *great* loss of generality you assume $(\mu_X,\mu_Y)=(0,0)$! Under that assumption there is no need for data or for any confidence interval. It is possible you might be interested in estimating $\sigma$, in which case a confidence interval for it could be useful: that is essentially what @Aksakal's answer does. Please decide, though, what your question really is. According to one of your comments you wish to "estimate where the center of impact is," which is what your first paragraph asks, but the rest of your question casts considerable doubt on that.
Comment: @whuber, $(\mu_X,\mu_Y)=(0,0)$ doesn't imply that $E[R(n)]=0$, so maybe it's a legit question
Comment: @Aksakal Of course! The issue is *what question is actually being asked.* The current version is a mixture of at least two different questions with some stuff that still makes no sense. It is self-contradictory, too: if, as in the first paragraph, we ask for CIs on the $\mu$s, then it is not the case that $E(R(n))\to 0$ as $n$ grows.
Comment: Let me try again: We are trying to characterize the distance between our sample center and the true center $(\mu_x, \mu_y)$, as a function of shots sampled _n_.  Why can't we assert that wherever it is the true center is called the origin?  Then we can compute the sample distance as I defined _R(n)_.  Further, if we compute the center of an infinite number of samples the distance from the true center must go to zero.  My question is, for some finite _n_, what is the confidence interval about that distance from center?  It must shrink and tighten as _n_ increases.  Is this clarifying things?
Comment: @feetwet, maybe you should re-define your problem like this: $R_i(n)=1/n\sum_{j=1}^n\sqrt{x_{ij}^2+y_{ij}^2}$, where $i$ is the sample. this would be a better definition of an average distance then, $E[R_i(n)]$
Comment: Just did that and incorporated more clarifications on the above.  I believe I have now made it clear that _R(n)_ is a random variable that is a function of _n_.  I assume that as a random variable we can talk about its expected value.  If we can relate it to an analytic distribution like $\chi$ then we have closed-form expressions for confidence intervals about the mean for any sample of the variable.  If we can't then I can only get those from simulation, right?
Comment: Now I see the confusion. I've [separately addressed the estimation of precision](http://ballistipedia.com/index.php?title=Closed_Form_Precision), which is why I stipulate we ignore it here. Now we are just interested in accuracy: I.e., determining how far our point of aim is from the true point of impact. So long as the aiming device is fixed relative to the muzzle we can dial out any detected error. That's why we don't care _where_ the center point of impact is. Given unlimited ammo I'd lock a gun in a machine rest, take a million shots, adjust the sights to that sample center, call it a day.
Comment: Since this is a key point let me elaborate: We lock a sight on a gun, note the point of aim, and then try to find the center of the point of impact. **The only error in the sighting-in process is determining the distance from sample center to true center of impact**. That's why we want to know how much error there is and what confidence intervals are for a given number of shots. Once we're satisfied that our estimate of the distance from sample center to true center is small enough we adjust our sights by referencing the original point of aim to that point of impact and we're done sighting-in.
Comment: Stepping back from the accouterments of the scenario: If we could take a million independent shots from the same bivariate and compute the sample center we'd be 99%+ certain that distance from the true center (_R(1000000)_) is within some $\epsilon$ of zero.  On the other extreme, if $\sigma = 1$ the Rayleigh distribution gives us $R(1) = \sqrt{\pi / 2} \approx 1.25$.  _R(n)_ should be strictly decreasing from there because we expect each additional sample point to pull the sample center closer to the true center.
Comment: @FelipeG.Nievinski - Note that I referenced the answer to that question to inform the accepted answer here.
Answer: Look at $\chi$ distribution, it's a square root of $\chi^2$ distribution, which is in turn a sum of squared normals.
CORRECTION:
$R(n)^2=\sum_{i=1}^n x_i+\sum_{i=1}^n y_i = \sum_{i=1}^{2n}z_i$, where $z_i=x_i$ for $i=(1,n)$ and $z_i=y_{i-n}$ for $i=(1+n,2n)$.
Hence, $R(n) = \sigma r(2n)$, where $r(k)\sim \chi(k)$.
$E[r(2n)]=\mu$, where $\mu=\sqrt{2}\Gamma((2n+1)/2)/\Gamma(n)$
The variance $Var[r(2n)]=(2n-\mu^2)$, see $\chi$ distribution. Subsequently, $E[R(n)]=\sigma E[r(2n)]$ and so on.
UPDATE: the CDF is given by the regularized gamma function: $P(n,r(2n)^2/(2))$. To compute the confidence bounds CB you have to solve for CB in $P(2n,CB^2/2)=\alpha$, where $\alpha$ is the confidence, such as 5% or 95%. CB will be in units of $\sigma$. Your math library should have the regularized gamma function, if it doesn't have its inverse then use the solver to find the CBs.
RESTATED problem
I think that it's best to redefine the $R(n)=\frac{1}{n}\sum_{i=1}^n r_i=\frac{1}{n}\sum_{i=1}^n\sqrt{X_i^2+Y_i^2}$. This means that you compute the distance $r_i$ for each pair of $(X_i,Y_i)$ coordinates, then average it acorss $n$ observation to get $R(n)$. Now, it's clear that $r_i^2\sim\chi^2_2$, assuming that X and Y are standardized normals, while  $r_i\sim\chi_2$, i.e. chi distribution with 2 degrees of freedom. I gave the links to this distribution, you should be able to work out the math for non-standard normals.
Next, $R(n)$ is the sum of $\chi_2$ distributed numbers, so CLT should be applicable. For n=30 CLT should work great. I would run Monte Carlo then test it with Jarque-Bera or similar tests of normality for smaller n. If it's normal enough, then do the CLT for R(n), while working with closed-forms of $r_i$.
Example: $(\mu_X,\mu_Y)=(0,0)$, $\sigma_X=\sigma_Y=1$, $\sigma_{X,Y}=0$.
$E[R(1)]=E[r_1]=\mu=\sqrt{2}\frac{\Gamma(\frac{2+1}{2})}{\Gamma(1)}=1.2533$
$Var[R(1)]=Var[r_1]=2\cdot 1-\mu^2= 0.4292$
You can test this with the following Matlab/Octave code:
    m=1e6 % number of samples
n=1 % number of X and Y to compute R
mu = 0*ones(2,1); % set ZERO means
Sig = eye(2); % set unit variance
x = mvnrnd(mu,Sig,m*n)'; % generate X,Y pairs
x = permute(reshape(x,2,n,m),[3,2,1]); % X and Y in 3 dim matrix

r = sqrt(sum(x.^2,3));
R = mean(r,2); % R(n)
hist(R) % show the histogram
jbtest(R) % test normality

[mean(R) std(R) var(R)]

Which outputs:
ans =

    1.2519    0.6551    0.4291

Now you can run the same for higher n=30, and get the output:
ans =

    1.2534    0.1197    0.0143

Applying the CLT approximation you get $Var[R(30)]_{CLT}=Var[R(1)]/30=0.0143$, very good match, and here's the histogram:
Comment: That looks plausible: R(n) is distributed like chi(2n) / sqrt(2n).  If so are there closed-form confidence intervals for samples of that random variable?
Comment: look at the link I gave, it has the CDF as regularized gamma function. I hope it's "closed" enough for you to compute the CIs
Comment: OK, I think I get it.  I haven't done closed-form CIs except with Normals, but I assume the method is the same: For confidence level K the confidence interval is given by the inverse CDF between (1-K)/2 and K+(1-K)/2?  Is the inverse CDF the same as the Inverse Gamma cdf?  Any links I can follow to ensure I don't screw it up?
Comment: Just to be clear, isn't it necessary to use k=2n for the chi distribution to reflect the distribution of R(n), and also necessary to multiply the mean by 1/sqrt(k)?
Comment: @feetwet, you might be right, I overlooked the bar over x and y in your formula.
Comment: Mathematically this looks plausible.  I'm going to Monte Carlo this weekend to try to validate it.
Comment: i figured it's more complicated than that, because you're using squares of means, these will not be $\chi^2$, but generalized $\chi^2$ due to cross terms. you're dealing with quadratic forms of normal r.v.s like $X^TX$. I would think about the form of $R(n)$, why is it the sqrt of means?
Comment: Shoot: Are you sure?  What you have there now is so elegant!  The variable we are analyzing is "distance from sample center to true center.  As long as I can define "true center" as the origin -- and why not?  I don't care where it is, I only care about my sample distance from it -- then it looks to me like there are no cross terms and $r(k) \sim \chi(k)$ as you showed, right?
Comment: $R(n)$ is a multiple of a [non-central $\chi$ distribution](http://en.wikipedia.org/wiki/Noncentral_chi_distribution) with unknown effect-size parameter and $2n$ degrees of freedom.  It is a multiple of a $\chi$ distribution only in the very special case that $(\mu_X,\mu_Y)=(0,0)$; that is, when the gun has *already* been sighted in to perfect accuracy.
Comment: @Aksakal: I don't think it's an average of $\chi(2)$ variables.  _R(n)_ is the distance of the sample center of _n_ shots, not the average of the distance of each of _n_ shots.  I just restated it in the original question.  I was also playing with the $\chi(k)$ model you gave above, which seems correct for the original statement, and something's wrong:  I coded the expression you derived for the _E[R(n)]_ as `=EXP(LN(SQRT(2))+GAMMALN((2*N+1)/2)-GAMMALN(N)) / SQRT(2*N)` and it quickly increases from 0.94 towards 1, instead of decreasing with _N_.
Answer: As shown by @AlecosPapadopoulos here, $R(n) \sim Rayleigh(\sigma / \sqrt{n})$.
From this we can use the closed-form confidence intervals for Rayleigh estimates.


Qual o significado dessa linha em Java?

Question: Não entendi a seguinte linha do código abaixo:
ret += this.elem[i]+(i!=this.ultimo?", ":"");

Qual o significado desses operadores i! , ? e :
{
    String ret = "{";

    for (int i=0; i<=this.ultimo; i++)
        ret += this.elem[i]+(i!=this.ultimo?", ":"");

    return ret+"}";
}

Comment: Não é i! E sim !=, Ali na frente acho q não é um operador mas sim o nome do objeto q foi criado com a ?, E o os : são cocatenados as informações
Comment: "i!" não é um operador, "i" é uma variável (que foi definida dentro do "for"), "!=" é o operador lógico "diferente de"; e "?:" é o operador ternário, é basicamente um "if" simplificado, antes do "?" é um teste lógico, se verdadeiro retorna o valor antes do ":", se falso, retorna o valor após o ":".
Answer: O operador != significa diferente, ou compara se o i é diferente do this.ultimo. Exemplo:
//i = 5
//j = 4

boolean exemplo = (i == j) //false
boolean exemplo = (j != i) //true

o operador ? é um if simplificado, por exemplo este if..
String exemplo = "";
if (i == 1){
  exemplo = "Igual a 1"
} else{
  exemplo = "Diferente de 1"
}

pode ser simplificado por:
String exemplo = (i == 1 ?   "Igual a 1" :  "Diferente de 1")

Ou seja o operador ? Testa a comparação, se for verdadeiro retorna o valor antes dos :, caso seja false, retorna depois..
Answer: O nome deste tipo de condicional é If ternário ( ternary operator )
Simplificando :
? == if

:  == else

( ( i != this.ultimo) ? ", " : "");

Então se i != this.ultimo , concatene " , " , senão concatene ":"

Qual o significado dessa linha em Java?


Why does hook_menu_link not work?

Question: theme_menu_link allegedly "Returns HTML for a menu link and submenu."
In attempting to use this 'hook' I added code to my template.php as such:
function themename_menu_link(array $vars) {
  dpm($vars);
}

It didn't work.  Nothing appears.
I confirmed that the template.php code is executing and cleared caches several times.
Every other hook_ or theme_ function I've tested runs without issue, however this one does not.
I've also tested the alternative method, themename_menu_link__your_menu_name with no success.  The code seems to be ignored.
Comment: What (base) theme are you using? Some don't use `menu_tree_output`, and as such don't invoke that theme function
Comment: Can you confirm it is being hit with either an exit or xdebug breakpoint? Just to clarify, you aren't literally calling it themename_menu_link are you?
Comment: @Kevin no, the theme is foo, and the function is foo_menu_link :)  I think Clive might have hit the nail though.  I'll check back in a few.
Comment: I think you're right, @Clive.  I'll post back in a few.
Comment: @Clive you are correct, the theme_menu_link does not fire on all default menus implemented in a theme.
Changing over to block menus fixed the issue.
Answer: Try print_r instead of dpm --
function theme_menu_link(array $variables) {
  return print_r($variables['element']);
}

or use hook_menu_link_alter --
hook_menu_link_alter($variables){
  var_dump($variables);
}

Comment: The print_r you suggest would require the boolean set to true to actually work; but thank you all the same.

Why does hook_menu_link not work?


What makes parallel/distributed probabilistic inference difficult to implement?

Question: My knowledge of probabilistic inference is severely limited, so coming from a Computer Science background I'm trying to understand what makes probabilistic inference difficult to implement in a parallel/distributed manner.
For MCMC at least I know there has been a lot of work in parallel and distributed implementations with recent work from the labs of Ryan Adams (FlyMC, Predictive Prefetching) and Erik Xing. The approaches also seem to differ in whether they take an asymptotically exact approach, or an exact approach.
Probabilistic programming languages like Stan use parallelism but only across chains (i.e. run one chain per process). For example in this (admittedly old) answer by one of the core devs of Stan he mentions:

There is no explicitly parallel code in Stan or rstan but neither is there any code that prevents the binary from being executed by several processes simultaneously.

So this is my naive question: What makes it difficult to do probabilistic inference in a distributed/parallel manner?
Would it be possible to do probabilist programming in a distributed setting for example, or are there core limitations in the nature of 'universal inference engines' that make them impossible/inefficient to distribute?
Comment: In the case of Stan, there are technical obstacles to performing auto-differentiation in parallel within a chain. As far as I understand it, the expression tree that Stan walks through to evaluate the gradient of the log-posterior with respect to the parameters (currently) has to be handled serially to ensure that it works correctly. There is some movement, or at least interest, in parallelizing some parts of Stan that internally handle the derivatives analytically rather than relying on the auto-differentiations mechanism. The other probabilistic programming languages do not face this issue.
Comment: @BenGoodrich Welcome to CV! You're probably the most qualified CV user, if not the single most-qualified person, to comment on how Stan works, so perhaps this comment could be submitted as an answer?
Comment: I didn't think it answered the heart of the OP's question, but I would change it to an answer if @Bar wanted me to.
Answer: I think your question is overly broad since it indicates "probabilistic inference", but I'll answer the question relative to Markov chain Monte Carlo (MCMC).
Parallelism in MCMC is hard because MCMC is inherently a serial algorithm. That is, given a current value $\theta^{(t)}$ in a Markov chain, an MCMC algorithm determines a set of steps to obtain the next value $\theta^{(t+1)}$. No amount of parallelism can avoid this fundamental nature of the algorithm.
Nonetheless, for some MCMC algorithms, parallelism can play a huge role in reducing the computational costs associated with the steps in each iteration of the algorithm. In the links that you included there is a huge cost with evaluating the likelihood because there is so much data, but the contribution to the likelihood for each datum (or group of data) can be evaluated independently and in parallel. As mentioned in the comments, each iteration of Stan requires evaluating a large number of derivatives that could potentially be calculated in parallel. There are other approaches, but most involve speeding up the steps within an iteration.
I mentioned that your question is too broad because there are other approaches to probabilistic inference, e.g. importance sampling, that are not iterative and therefore could be more amenable to parallelism. These approaches are generally poor for high dimensional target distributions, and these high dimensional target distributions are often the cause for computational bottlenecks that have you considering parallelism in the first place.
Comment: > "Parallelism in MCMC is hard because MCMC is inherently an iterative algorithm." I guess this hits the nail on the head then, although "serial" instead of "iterative" might be a better word here, since in my mind at least, there many iterative algorithms that are straight-forward to  parallelize (like SGD), given certain assumptions (like convexity of the function). Are there relaxations like convexity that allow us to perform MCMC in parallel?
Comment: In my first draft, I had serial and changed to iterative since I was using the term iteration later. Updated the answer to use serial.


Crop confirm jquery

Question: I want to create croping system php and jquery . I did some research and learned a few things. But i have a problem at the moment. In the following code have confirm. I do not want to encounter such a warning. The system works like this: When you apply the process to select the desired area of the image against the following warning turns out to (( Do you want to save image..! ))But I do not want such a warning. Can you help me on this issue.
http://goo.gl/zXhvGG
function getSizes(im, obj) {
    var x_axis = obj.x1;
    var x2_axis = obj.x2;
    var y_axis = obj.y1;
    var y2_axis = obj.y2;
    var thumb_width = obj.width;
    var thumb_height = obj.height;
    if (thumb_width > 0) {
        if (confirm("Do you want to save image..!")) {
            $.ajax({
                type: "GET",
                url: "ajax_image.php?t=ajax&img=" + $("#image_name").val() + "&w=" +
                    thumb_width + "&h=" + thumb_height + "&x1=" + x_axis + "&y1=" + y_axis,
                cache: false,
                success: function (rsponse) {
                    $("#cropimage").hide();
                    $("#thumbs").html("");
                    $("#thumbs").html("");
                }
            });
        }
    } else
        alert("Please select portion..!");
}
$(document).ready(function () {
    $('img#photo').imgAreaSelect({
        aspectRatio: '1:1',
        onSelectEnd: getSizes
    });
});

PHP
<?php

    $valid_formats = array("jpg", "png", "gif", "bmp");
    if(isset($_POST['submit']))
        {
            $name = $_FILES['photoimg']['name'];
            $size = $_FILES['photoimg']['size'];

            if(strlen($name))
                {
                    list($txt, $ext) = explode(".", $name);
                    if(in_array($ext,$valid_formats) && $size<(1024*1024))
                        {
                            $actual_image_name = time().substr($txt, 5).".".$ext;
                            $tmp = $_FILES['photoimg']['tmp_name'];
                            if(move_uploaded_file($tmp, $path.$actual_image_name))
                                {
                                mysql_query("UPDATE users SET profile_image='$actual_image_name' WHERE uid='$session_id'");
                                    $image="
Please drag on the image
";

                                }
                            else
                                echo "failed";
                        }
                    else
                        echo "Invalid file formats..!";
                }
            else
                echo "Please select image..!";
        }
?>

HTML

<?php echo $image; ?>

<form id="cropimage" method="post" enctype="multipart/form-data">
    Upload your image <input type="file" name="photoimg" id="photoimg" />
    <input type="hidden" name="image_name" id="image_name" value="<?php echo($actual_image_name)?>" />
    <input type="submit" name="submit" value="Submit" />
</form>

Ajax.php
<?php
include('db.php');
session_start();
$session_id='1'; // Session_id
$t_width = 100; // Maximum thumbnail width
$t_height = 100;    // Maximum thumbnail height
$new_name = "small".$session_id.".jpg"; // Thumbnail image name
$path = "uploads/";
if(isset($_GET['t']) and $_GET['t'] == "ajax")
    {
        extract($_GET);
        $ratio = ($t_width/$w);
        $nw = ceil($w * $ratio);
        $nh = ceil($h * $ratio);
        $nimg = imagecreatetruecolor($nw,$nh);
        $im_src = imagecreatefromjpeg($path.$img);
        imagecopyresampled($nimg,$im_src,0,0,$x1,$y1,$nw,$nh,$w,$h);
        imagejpeg($nimg,$path.$new_name,90);
        mysql_query("UPDATE users SET profile_image_small='$new_name' WHERE uid='$session_id'");
        echo $new_name."?".time();
        exit;
    }

    ?>

Comment: Why not simply swap out the `confirm(..)` with `1` or `true` so you don't have to worry about modifying the `if` block?
Comment: why don't you just remove the confirm condition?
Comment: @uchamp thanks for reply can you check my question in this link http://goo.gl/zXhvGG
Comment: @MustafaOzturk your link does not contain a button to save the cropped image?
Comment: @Bellash yes dear you are right i learn it now. How can i add save the cropped image button?!
Comment: you need to send it by post! your ajax must be post method... I am editing my answer to help you
Comment: @Bellash thank you i added my ajax code in my question
Answer: You need to remove the confirm from your js code. So, use this code instead

Comment: this code is right thank you. But How can I add the Save button in the Crop Can you help me?

Crop confirm jquery


Find whether there are integer solutions to $7x^2+9y^2=3932$. Is my solution correct?

Question: Find whether there are integer solutions to $7x^2+9y^2=3932$
Here's my attempt, I would like to know if it's correct please:
Lets assume $7x^2+9y^2=3932$ has integer solutions. That mean GCD of $x^2,y^2$ is 3932, or $(x^2,y^2)=3932$.
That means that there are $a,b \in \mathbb Z$ so that $x^2=3932a$, $y^2=3932b$.
And we know that $7x^2+9y^2=3932 \rightarrow 7\cdot 3932a+9\cdot 3932b=3932$
That means $7a+9b=1$, and since $(7,9)=1$, we can find such $a,b \in \mathbb Z$, using Euclid algorithem:
$9=1 \cdot 7 +2$
$7=3 \cdot 2 +1$
$2=2 \cdot 1+0$
Therefore, after calculations, I found:
$1=4 \cdot 7-3\cdot 9$
So $a=4$, $b=-3$. But we defined $y^2=3932b$, so we get $y^2$ to be negative. Therefore contraditon.
Is this a valid contradiction?
Thanks in advance for any assistance!
Comment: Why does the first assumption imply that $x^2$ and $y^2$ have gcd $3932$?
Comment: Personally I would look at this modulo $3$
Comment: @T.Bongers to amplify $7\cdot 2+ 9 \cdot 3=41$. That doesn't mean the GCD of $2$ and $3$ is $41$.
Comment: I see... thank you. When does it mean that though? I wasn't taught this with much rigor...
Comment: One cannot (at least as I see) immediately conclude that $x^2$ and $y^2$ have no common factor: What we *can* conclude, however, is that the $(x^2, y^2)$ is a divisor of $3932$. More generally, any two linear combination of two numbers (in this case, a linear combination of $x^2$ and $y^2$) must be *divisible* by the GCD of the two numbers, but no more is true. @MarkBennet's suggestion of working modulo $3$ will help a lot, since we can immediately ignore the variable $y$ and only look at $x$.
Comment: What it does mean is that anything which is a factor of $x^2$ and $y^2$ is also a factor of $3932$ so $(x^2,y^2)|3932$. The GCD of $a$ and $b$ can be defined as the least positive integer $d$ for which $ax+by=d$ has a solution in integers.
Comment: I see, thank you very much!
Answer: Notice that $7x^2 + 9y^2 = 67$ has a solution $(x,y) = (2,1)$. But this does not give that $\gcd(x^2,y^2) = 67$ here. So unfortunately, your reasoning does not hold.
On the other hand, if you reduce mod 3, you get $x^2 \equiv 2 \pmod 3$. The only squares mod 3 are $0$ and $1$, so there no solution.
Comment: Why look mod $3$? - always useful to try small primes to see what might be going on. $3$ eliminates on term too, so simplifies things. If that didn't work I'd try mod $7$. And if that didn't work I'd have some clues as to how a solution might look. $2$ would be another prime to choose - but you might try mod $4$ or mod $8$.  This doesn't always work, and there are other things to try - but it is cheap and easy if you can't see where else to begin.


User rank algorithm

Question: I would like to implement a kind of user rank system in my forum.
I have 7 ranks, 1-7.
I have four parameters on which the users should be judged (weighing in parentheses):
Number of likes on their comments (50%)
Amount of comments written (30%)
Average length of comments (15%)
Threads created (5%)
Does anyone have a suggestion on how to do this in PHP? Any other comments on the ranking system is also welcomed
Comment: You might want to watch this similar question: http://stackoverflow.com/q/9097919/338803
Comment: Seems like simple enough math... get the individual score components, add/weigh them, done.
Answer: Its straight forward, what you are creating is a weight score per user.  I would calculate it with a cronjob, and keep a static weight for each user in their profile/user table.
It is really an open ended question, as you can do whatever you like, calculate on the fly, schedule it, etc;

User rank algorithm


How to solve a karmarkar's optimisation problem in scilab

Question: i am new to scilab. i have transformed an Lp equation to the karmarka form and now i am trying to solve but it is not giving xopt. please help. the equation is shown jn the image
Comment: Where is your Scilab code ?
Comment: Aeq= [ i am inputting all the coefficients for the variables];
Answer: I presume that you have not mentioned the positivity constraint on the components of y. In that case, you have the trivial solution:
<code>c=zeros(23,1);
c(23)=1;

A=[
0.00359 0.0042 0.0021 0.00419 0.00359 0.00438 1 zeros(1,14) -4332 4332.978
0.89 1.12 1.044 0.86 0.73 0.23 0 1 zeros(1,13) -467012 467008.126
7.552 6.539 7.671 6.822 6.12 4.824 0 0 1 zeros(1,12) -163766 1637621.625
0.0135 0.0133 0.007 0.005 0.0149 0.0156 0 0 0 1 zeros(1,11) -8796 8796.931
zeros(1,10) 0.00359 0.89  7.552 0.0135 -1 0 0 0 0 0 0 -289.45 281.869
zeros(1,10) 0.0042  1.12  6.539 0.0133 0 -1 0 0 0 0 0 -300.49 293.814
zeros(1,10) 0.0021  1.044 7.671 0.007  0 0 -1 0 0 0 0 -287.37  279.646
zeros(1,10) 0.00419 0.86  6.822 0.005  0 0 0 -1 0 0 0 -288.09 274.577
zeros(1,10) 0.00359 0.73  6.12  0.0149 0 0 0 0 -1 0 0 -290.33 284.475
zeros(1,10) 0.00438 0.23  4.824 0.0156 0 0 0 0 0 -1 0 -317.15 313.076
];
b=zeros(10,1);

y=karmarkar(A,b,c,zeros(23,1))

WARNING: karmarkar: The algorithm did not converge (exitflag= -3).
 y  =

   0.
   0.
   0.
   0.
   0.
   0.
   0.
   0.
   0.
   0.
   0.
   0.
   0.
   0.
   0.
   0.
   0.
   0.
   0.
   0.
   0.
   0.
</code>


How to redirect from `/` to `/foo/` using FlowRouter and MeteorJS?

Question: In my scenario, I want everyone that visits our root URL to be auto-redirected to a url containing a document for collaboration and instant gratification.
Here, the router.coffee code is:
FlowRouter.route '/',
  action: ->
    console.log "I'm home!"
    FlowRouter.go 'myProject'
  name: 'myHome'

FlowRouter.route '/my/:projectId',
  subscriptions: (params) ->
    @register 'currentProject', Meteor.subscribe 'project', params.projectId
  action: ->
    BlazeLayout.render 'myBody'
  name: 'myProject'

I want the root URL to redirect to /my/:projectId but I'm unsure of how to retrieve the auto-generated projectId and redirect using with either FlowRouter.go or FlowRouter.redirect.

Is this possible?
If yes, how?

Thanks for your help!
Comment: @ toszter  You got answer for question?
Comment: @MariyaJames --- Not yet. I'll self-answer if I discover a solution on my own.
Comment: @toszter: I think you must specify a router if you are using FlowRouter.go or FlowRouter.redirect
Comment: What should be the project ID for users that try to access the root URL (/)?
Is it a fix one for all or is it based on username/session var/... ?
Comment: @Izack --- I create a baseline project (with a projectId) for each new user. So, when someone registers and they have a persistent session, I would like to redirect them to their project page so they can collaborate with other users.
Answer: Since the data may not be available when the route action execute,
the best is to re-route at the template level.
It might be a good idea to use the Template.[name].onCreated() function
and put inside it something like the following code:
pID = ... // Get the user project ID from wherever you saved it
var params = {projectId: pID};

// Set the project URL including the :projectId parameter and re-route the user
FlowRouter.go("myProject", params);

Comment: I will give this a shot and see how it fits with my goals.
Comment: To your possible disbelief, I have not tried yet. I imagine based on what I've learned about the Template arch so far, that you're on the right path here and using `FlowRouter.getParam('projectId')` to pull the value is the only bit I'd add. Also, my use of route-level subscriptions is unnecessary here since the Template level would suffice.


Multiple Image Upload and Rename - avoiding multiupload image field

Question: I'm looking to provide the following functionality on my Drupal site:
I'd like to make an easy to use content type that allows users to upload multiple photos easily. The uploader needs to allow them to dump in files, rename them and upload them. I'm currently using the multiupload image field which I've found to do the job of uploading well but, I don't like the thought of increasing my php upload limit to allow users to upload a whole load of photos at once. I wondered whether there was an uploader that allows users to drop in their photos and, providing an individual photo isn't over the upload limit, will handle the upload of all of the photos?
An added bonus would be the ability to rename photos, once they've been uploaded.
Thanks in advance for your help!
Answer: Plupload has a variety of upload widgets that should allow you to upload without increasing the php limit. You can view examples of the widgets here. You can pick one of the [modules that integrate pulpload on its project page at https://www.drupal.org/project/plupload.
Comment: Also, [This patch to the Media module](https://www.drupal.org/node/2074625) should allow you to rename files after upload.


image field point to another image field

Question: I have users whom are part of a group, this group has an image field that can be changed by the administrator.
When a user writes a new article, i wan the article to display the image of the group.
To do so, i'm modifying the hook_node_presave(). I'm loading the group of the user and i modify the image of the article to match the one from the group with a simple affection ( $node->field_image = $userGroup->field_image_group; ).
It's working pretty well, however if the group changes its image, the article will still keep the old one.
Is there a way to make the image change automatically ? Same question if I change the author of the article or if the author changes group.
Thank you.
Comment: Did you test your solution in `hook_preprocess_node`?
Comment: I just tried but it looks like it does nothing. I tried to see what's inside $variables with dvm() but it looks like the function is not called.
Answer: What you need is a change in the way the content is displayed, so it is much better to do it in theme layer rather than content level.
I would recommend using template preprocessing for node.
Add this function to the template.php file in your theme folder:
<?php

/**
 * Implements hook_preprocess_node().
 *
 * Replaces the node's image field with anbother node's image field
 */
function YOURTHEME_preprocess_node(&$variables) {
    $node = $variables;
    // Make sure we only alter nodes that we intend to!
    if ($node->type == 'article') { // assiuming your article content-type is 'article'
        // Get the view mode for this node, e.g. full or teaser
        $view_mode = $variables['view_mode'];
        // Get the group node
        $another_node = get_another_node($node); // Somehow!
        // Retrieve the image field of that node using the same view mode of the current node
        $another_image_field = field_view_field('node', $another_node, 'field_image', $view_mode); // assiuming it is called 'field_image'
        // Replacing the other field with the current node's field
        $variables['content']['field_image'] = $another_image_field; // assuming it is called 'field_image'
    }
}

You will need to update the code to retrieve the node object for the group and also make sure that the field names are correct for your case.


Why are triads considered perfect chords and the basis which all extensions are built off of?

Question: Why are triads considered perfect chords and the basis which all extensions are built off of?
I'm sort of posting the question from an ignorant standpoint but I do already have some prior knowledge. I wish I could buy the book From Polychords To Polya. Which kind of touches on my question. I've already read a bunch of it online but a lot of it is above my current level of understanding so I have to take some time to digest the information. Also the book Audacious Euphony seems to touch on the subject with depth but I'm having a lot of trouble understanding most of the information in that book (I ordered a physical copy today).
Why are dyads not the basis or even considered a chord by a lot of music theorists?
Why are tetrads (pentads etc), only considered extensions of the triad?
People say extensions and add chords only provide more "color" or "dissonance".
By color I don't fully understand what they mean, I do understand what added dissonance would mean though, based on the harmonic series. With 3 notes you can have all consonant pitches, (Root, P5, M3/M6) or even (Root, P5, P4 /I like sus chords/). But once you get to 4 notes you have to add the 2nd or 7th which are more dissonant.
Is this main reason Triads are as far as you can go before beginning to add dissonance to a consonant combination of pitches? Basically, are Tetrads the beginning of where we start to add "dissonant" pitches?
Lastly, usually triads are played and accompanied by vocals (a 4th pitch) which is basically extended and added harmony. Meaning, the triads are definitely the harmonic foundation, leaving the voice to play around with consonance and dissonance on top of the triads. Do I have this correct also?
Comment: Welcome! Please read through [this help page](https://music.stackexchange.com/help/on-topic). Your central question is a great one, but note that "Can you recommend a book for ___" questions are off-topic. (Good answers might include some suggestions, though!) Meanwhile, make sure you don't have "multiple questions in one." In my opinion, though there a lot of question marks here, they seem like part and parcel of your central question.
Comment: I'll let others answer, but note that the musical context for triads being "such a big deal" is limited. It's a pretty big context—like, 99% of the music made in the past few centuries—but it's not the only context. There are musics that don't talk about chords at all. So it's not really about value statements like "perfect"; triadic harmony is just used because this idiom has evolved that way.
Comment: @AndyBonner Hey Andy! Ok noted on the book recommendations part. Also I understand on the trying to keep the question condense. Yes the multiple questions are essentially on topic, I'm just not sure how to put all of that into one whole question it wouldn't have the context I'm looking for I think.
Comment: @AndyBonner Also, I don't want to act like I understand exactly what you meant in your second comment! If you don't mind could you elaborate a little bit. What did you mean in the first part about the context for triads being such a big deal is limited? And when you say there are musics that don't talk about chords, are you talking about how there is a lot of music viewed more from the perspective of melody, or what I would personally consider horizontal harmony?
Comment: I'm trying to clean your posts up a bit @Lecifer, as you seem to have some challenges following site guidance. Please only ask one question in a post, and ensure it meets guidance set out in our [about] and [ask] pages.
Answer:
Why are dyads not the basis or even considered a chord by a lot of music theorists?

They certainly used to be.  The modern theory of chords evolved gradually over several hundred years during which the primary theoretical framework was dyadic.  Music in three or more parts that modern ears would recognize as chords had already existed for a few centuries before theorists started talking about chords as such.
That some theorists distinguish dyads from chords is a bit arbitrary, but it does reflect the harmonic ambiguity of dyads.  If the interval is perfect, the harmony could be major or minor (for example, to C and G you could add either E or E♭), and if the interval is major or minor, you can add a perfect interval related to either element (for example, to C and E you can add G or A; to E and G you can add B or C).  The dyad is therefore somewhat problematic in harmonic analysis, and the theorists who exclude it from the definition of "chord" are mostly theorizing about harmonic analysis.

Why are tetrads (pentads etc), only considered extensions of the triad?

The historical perspective is also helpful to consider here.  Until maybe 100 years ago, it was highly irregular to end with anything more than three distinct pitch classes.  Such chords were considered dissonant because they contained at least one second (or seventh).  The dissonance was not only an acoustic property but a contrapuntal one: it had to be resolved, so it couldn't be present in the final chord.  When Rameau developed his theory that equates the chord EGC with CEG, this led to a chordal theory based on the two possible triads that contain a perfect fifth, the major and minor triads.
Much later, people started regarding "extra" tones in chords as color.  If you add a sixth or seventh to a major triad, you no longer need to hear it as tension requiring resolution; it could just be a different sonority that is still restful.  In this context, however, the underlying harmonic relationships can still be described in terms of triads, which leads us to refer to the "extra" tones as "color."
Surely someone will think of an exception to the preceding paragraph, but to illustrate what I mean, consider the chord progression Em7-Am7-Dm7-G7-C7.  Any song using this very tonal circle-of-fifths progression can also be played without any of the sevenths: Em-Am-Dm-G-C.  The harmonic functions of the chords do not change, but the color is very different.

People say extension's and add chords only provide more "color" or "dissonance". By color I don't fully understand what they mean, I do understand what added dissonance would mean though, based on the harmonic series. With 3 notes you can have all consonant pitches, (Root, P5, M3/M6) or even (Root, P5, P4 /I like sus chords/). But once you get to 4 notes you have to add the 2nd or 7th which are more dissonant. Is this main reason Triads are as far as you can go before beginning to add dissonance to a consonant combination of pitches?

This is more or less it.  The thing about "color" is, as I've said above, a change in the role of the acoustic dissonance: where before it provided from contrapuntal tension (also called "dissonance," which is sometimes confusing), it now serves to give the chord a different sonority.
But...

Basically, are Tetrads the beginning of where we start to add "dissonant" pitches?

Remember that the perfect fourth is frequently said to be consonant or dissonant depending on context.  Also consider the diminished fifth/augmented fourth, which is always dissonant whether there is a third tone (or more) added to it.

Lastly, usually triads are played and accompanied by vocals (a 4th pitch) which is basically extended and added harmony. Meaning, the triads are definitely the harmonic foundation, leaving the voice to play around with consonance and dissonance on top of the triads. Do I have this correct also?

In some styles, yes.  But more generally, the solo melody (whether a voice or an instrument) will basically have one of the chord tones, too.  In fact, you will often see songs where a chord symbol seems to have been chosen specifically to reflect the fact that the melody has a certain pitch.  It's fairly rare that the melody's main pitch at any point is not reflected in the chords.  For example, you're not likely to see a G7 with the melody on A; instead, the chord symbol would be some variety of G9.
The melody might have some figures that use non-chord tones ("play around with consonance and dissonance") but in an ensemble context any part can do that.  For example, if the chord is C then the melody can have C-D-E without the chord having to change.  But an inner part could also have E-F-G at the same time.  If the passing tones (D and F) happen quickly enough then we would not analyze that as a change of chord.
Even with a lead sheet, depending on the degree of precision indicated by the given chords, a pianist or guitarist can play around with consonance and dissonance, for example seeing C and playing C-Csus4-C or adding a little scale or some other lick, such as the passing tones mentioned in the previous paragraph.  Similarly, a good bassist can embellish the indicated bass notes with scales and neighbor tones.  The chords just specify the harmonic framework.  They aren't a precise indication of everything anyone should be playing.  A composer or arranger who wants that degree of precision writes a score in staff notation, not a lead sheet.
Answer: There is a general rule for intervals, which could be phrased like:

If a tone intersects a given interval in an arithmetic way (at the arithmetic mean) it will support the base tone, if it intersects in an harmonic way (at the harmonic mean) it will weaken the base tone.

And, since i have probably not explained what I mean very clearly (English is not my first language), let me explain what I mean by that:
Take the most basic interval, the octave. The proportion of the frequencies of the two tones are 1:2. Now, add a third tone, which intersects this interval at the arithmentic mean (in other words: "halfways" in terms of frequency). This intersecting tone will be the fifth of the base tone, with a proportion of 2:3. This fifth will sound supporting to the base, e.g. when you play C-G-c-g-c'-... on a piano your impression will be that "c" is the base tone and that impression is reinforced by the g.
If you instead use a third tone which intersects the octave interval at the harmonic mean, which means the fourth (3:4, which is near the golden ratio), then the tone will weaken the impression of the base tone being the base and try to establish itself as the base. In the above example, playing C-F-c-f-c'-... instead of C-G-c-g-c'-... the "c" will not be heard as undisputed base tone any more, but the f will try to esablish itself as base tone with c as its fifth.
In light of this, what is a major triad? It is the base tone, the fifth (which, as we said above, is the arithmetic mean of the octave interval) and the major third - 4:5 - which itself is is the arithmetic mean of the base-fifth-interval. This is why the major triad sound so "in rest" - it only consists of intervals, which reinforce the base tone in its role.
This also explains why the minor triad is less reinforcing the base tone - C major is in rest, but C minor "directs" or "hints" towards Eb major.
To sum it up: (major) triads are considered "basic" (and also "in rest"), because they only consist of tones supporting the base tone in its role.
Everything added to that will establish some sort of tension - "tension" here means no longer reinforcing the base tone in its role as base but in one way or the other poining away from it. Hear a C-triad major and c is undisputedly the base. Add a Bb to it you get a C7 and it points away from C, probably to F-major. Dyads are not considered to be basic in this sense, because they do not support the base to the maximum possible (by leaving out the major third).
Comment: @JudyN.: I was already busy editing it. I hope it is clearer now what I meant. If you think it is still unclear you are welcome to give pointers on how to improve the answer.
Comment: This "arithmetic" vs "harmonic" distinction looks very interesting, and I have never seen this idea before! What exactly do you mean by these? Are these terms related to arithmetic mean and harmonic mean? If you can't answer that, maybe you could clarify by example — if you divide the interval of a fifth (2:3) into a major and major intervals (4:5 and 5:6), which of these would you call arithmetic and which harmonic? What if you divide into more complex intervals? Or did I misunderstand you entirely, and you only want to divide the octave interval?
Comment: @anatolyg: by "arithmetic" I mean "halfways", by "harmonically" I mean at the "golden ratio". Octave interval is 1:2, so a-a' is 440Hz:880Hz. Intersecting it arithmetically you get 440Hz:660Hz:880:Hz or 2:3:4 or a-e-a'. Same for the major third: 440Hz-660Hz intersected arithmetically gives 440Hz:550Hz:660Hz or 4:5:6 or a-c#'-e'.
Comment: @anatolyg: One could ask the question why not to divide i.e. the interval 440Hz:550Hz any more, i.e. 440Hz:495Hz:550Hz. This is indeed possible but will not lead to a "regular tone" (one of the twelve tones of the usual tonal system). And, yes "arithmetic mean" and "harmonic mean" were the terms i (unsuccessfully) looked for.
Comment: @bakunin Do you think you could edit your answer to clarify you are referring to dividing intervals at the arithmetic mean and harmonic mean? Your answer was extraordinarily unclear on first reading until I found this clarification
Comment: @bakunin Very interesting! At some point in all of my studies I started to contemplate why some people have a different view of whether or not the Major 3rd or Fourth is more consonant based on the overtone/harmonic series. I stopped searching for an answer on that topic, but the furthest I got with it was that some sources would say consonance isn't just about which ratios are simpler, I never really found a concrete conclusion. This is essentially what you mean when you say arithmetic way and harmonic way correct? Arithmetic way being the overtone series and a harmonic way being ratios?
Comment: @bakunin I'm just a little confused, could you elaborate on how we know that a triad only consists of tones supporting the base tone in its role? In other words, how do we know that everything added after the root, 5th, and major 3rd no longer supports the base tone. In A Geometry of Music by Dimitry Tymoczko, there's a section on how the most consonant chords divide the octave nearly evenly. There's C G, C E G,  and then C E G Bflat being C7, Eflat coming much later in partials. So it is exclusively the major triad that is basic and at rest, not the minor triad correct?
Comment: @bakunin Last thing I'm just not sure about is how you have C7 pointing to F Major, I'm actually not sure what it would be but I was thinking maybe G Minor (G, Bflat, D)? Could you explain that for me? I know i'm asking a lot I apologize!
Comment: @Lecifer: by "arithmetic" and "harmonic" I mean that in a mathematical way: given an interval AB, then arithmetic intersection is a point C so that AC=CB, harmonic intersection (also called the "golden ratio") is a point C so that the proportions AC:CB and BC:AB are the same. C7 is a dominant chord and dominants are the fifth of a key. The key in which C is the fifth is? Play C7-F and you will hear the "tension-release".
Comment: @Bakunin ohh ok I understand what you mean by C7 points away from C and to F now. For some reason I thought you were talking about what pitches are in the chord and what was closest to the tones in C7 but I get it. Do you have any books on the arithmetic and harmonic topic, and do you think reading them would help cause that part is over my head I have like no idea what you mean. I don't understand the intersection part at all.
Comment: "since i have probably not explained what I mean very clearly (English is not my first language)": the English is perfectly clear except for "intersect" -- we don't commonly describe G3 as intersecting the octave between C3 and C4, but we don't have another verb that we do use for this, either.
Comment: @Lecifer for musical intervals the harmonic mean basically flips the position of the inner pitch.  The arithmetic mean of 1 and 1.5 is 1.25; multiply everything by 4 to get a 4:5:6 major triad, which has a major third below and a minor third above.  The geometric mean of 1 and 1.5 is 1.2; multiply everything by 10 to get a 10:12:15 minor triad, which has s minor third below and a major third above.
Comment: 440Hz:495Hz:550Hz does yield a normal tone; 495 Hz is a major tone (9:8) above 440 Hz and 550 Hz is a minor tone above 495 Hz (10:9).  We normally call 495 Hz "B" ("H" in German).  The problem is that the harmonic mean, 488.89 Hz, is also B (or H) :-)  But 495 Hz B is much closer to equal-temperament B than 550 Hz C♯ is to equal-temperament C♯.
Comment: The math migtt make more sense if you invert your ratios to the more conventional ("improper fraction") form, e.g. Octave is 2/1; M3 is 5/4; P5 is 3/2.  However, I calculate the harmonic mean of root and P5 to be 1.2 (or 6/5), which is the minor third.
Comment: But 3:4 is NOT near the golden ratio. If this answer hinges upon us accepting that it is, then this answer cannot be considered correct or useful as it is currently written.
Answer: The explanation I know as the usual explanation is Klang or the Chord of Nature. The harmonics of a string, the overtones of a fundamental pitch, outline a root position major triad for the strongest harmonics. This acoustic phenomena is thought to be the origin or many aesthetic preferences regarding harmony.

Why are dyads not the basis or even considered a chord by a lot of music theorists?

To some degree this is just a matter of terminology. When a textbook defines a chord as three tones, then that's your working definition. A broader view is that any number of simultaneous pitches is harmonic material, and that harmonic material can display harmonic function regardless of the exact number of simultaneous pitches. Those harmonic entities are functionally chords.
So, some consider two simultaneous tones to be chord, or partial/incomplete chords, because they can have recognizable harmonic function. In other words, when doing harmonic analysis, if you are able to, for example, label only two pitches as having function V, you have a dominant chord.
But, to be less abstract, the important thing that happens when adding a third chord tone is you can unambiguous know the chord root. Ex. with only tones C E you could have either a partial, root position C major chord, or a partial first inversion Am chord. Depending on the harmonic context one or the other is more strongly implied, but adding a third tone, either G or A will make the two cases clear.

Why are tetrads (pentads etc), only considered extensions of the triad?

One way of explaining this is a historic, contrapuntal approach. Assuming tertian harmony, the tetrad is a seventh chord, the seventh in the historic, contrapuntal context is a dissonance, not a chord tone, and part of some melodic voice movement.
V7 is an example, where some theory explains the origin of that chord being the melodic movement, the passing motion, of scale degrees ^5 of the V chord, through dissonant, non-chord tone, technically non-triadic tone, ^4, to ^3 of the I chord.
In other words the third and fifth above a root, or to be very historic above the bass, are consonant, and therefore actual chord tones, while the higher extension tones of seventh, ninth, and eleventh are dissonances. That leaves the thirteenth (or added sixth) the odd one out with additional history and theory discussing it, but let skip that for now. Historically, dissonances are not chord tones, therefore the fundamental chord ends with a third and fifth above a root/bass. Everything else will be either explained by chord inversions or non-chord tone motion.
I'm glossing over a lot of detail, because when taking a historic view of these topics you must remember that it goes back to a history before the concept of a chord root existed. It's hard to be complete and concise at the same time.
Answer: A fundamental property of a chord is that it has a functional root through which the other notes are viewed, with the note octaves being mostly unimportant.
A dyad is ambiguous: the two notes in a dyad form an interval with both ends of the interval being equally important.  The exception is a (straight) fourth or fifth that are harmonically so close that they define a root note and overtones.  The lower note of a fifth or the higher note of a fourth is the root.
If you now add a third note, this note stands in relation to the root note and thus provides color, defining a chord.
The most important color to fill in is major/minor: that creates a full chord entity in some inversion.  Other colors are seventh/major seventh and suspensions.  When those are missing major/minor, the result is not really satisfactory as a chord: with suspensions, that usually is the idea.
A diminished chord does not actually have the root-defining fourth or fifth.  For that reason, it tends to work in the manner of a suspended chord as well, namely posing a question that only the context can answer.  The difference here is that it does not even nail down a root note.
Jazz chords are more complicated since they tend to add significant more coloring and since the stacking is detailed enough that octave relations start making a significant contribution to the fingerprint of the chord.
One also has to see that the perception of chords differs over musical periods: in figured bass, the lowest note is considered the root note even if you stack a 4 and 6 above it: as a pop chord, this would rather be seen as the lowest note not being the root and the chord being an inversion, or a chord with a replaced base note.
Answer: My theory: Those styles of music for which it is true that triads form the basis of harmony heavily depend on the major-minor-dualism. Which means that you definitely need the (major or minor) third in a chord, to make it a distinct major or minor chord respectively. As to the fifth, in the history of many music cultures this interval has always had the special status of being the purest interval (following the prime and the octave, which are already in the chord according to the octave equivalency of the root). So it is somewhat natural to include also the fifth in the chord. Just look how naturally the fifth is used in drones, in bagpipe music for instance but also in Indian or Persian classical music; whenever one wants to establish a note as the root of something (chord, scale, piece), you often see that it is sounded together with its fifth.
That is my explanation of why it's all about the triads, composed of the root, the third and the fifth; instead of choosing dyads or tetrads composed of other intervals as the basis. Now regarding your other questions:

People say extension's and add chords only provide more "color" or "dissonance". By color I don't fully understand what they mean, I do understand what added dissonance would mean though, based on the harmonic series.

"Color" refers to the idea of musicians in the beginning of the 20th century who had the feeling that the traditional euroclassical music with its three main pillars rhythm, harmony and melody has become hackneyed. So the idea arose to include timbre as a new pillar equitable to the three other ones. That way, styles like jazz and avantgarde music became born. Especially in jazz you add notes to chords to create new timbres, quite similar to additive synthesis of tones to create new timbres.

With 3 notes you can have all consonant pitches, (Root, P5, M3/M6) or even (Root, P5, P4 /I like sus chords/). But once you get to 4 notes you have to add the 2nd or 7th which are more dissonant. Is this main reason Triads are as far as you can go before beginning to add dissonance to a consonant combination of pitches? Basically, are Tetrads the beginning of where we start to add "dissonant" pitches?

Thinking of chords as layered thirds yes. This thinking developed through the establishment of 3rd-5th-triads, which are layered thirds, so people added the 7th and 9th as the next layers, which are commonly perceived as dissonant.

Lastly, usually triads are played and accompanied by vocals (a 4th pitch) which is basically extended and added harmony. Meaning, the triads are definitely the harmonic foundation, leaving the voice to play around with consonance and dissonance on top of the triads. Do I have this correct also?

This is a good way to think about it. Usually, on the hard beats this 4th pitch is one already included in the triad, but on the weak beats it's often a new pitch not in the triad. In this case the chord has a tetrardic character. This not only applies to the vocal strands on top of the chords, but all instruments, including the bass line.
Comment: Thanks I appreciate your answers! I upvoted it but for some reason it won't show on my screen, do you see a 1 or a 0?

Yea songs can be broken down into 1-4 chords, a lot of times just 2. I think on the 1st and 3rd beats of the measure. To me, there's ann intrinsic chord progression, within every song that isn't even necessarily coming from any particular instrument.
Then the melodic lines from vocals and other instruments kind of dance around those chords (harmonic foundation).
Comment: And yep, im aware of the drone instruments such as a Tanpura being tuned to to certain chords, used as a harmonic foundation. Harmony is the most interesting thing about music to me, specifically the fact that it is a combination of rhythm & pitch. Notes played at the same time are like a guideline for the notes not played at the same time.


Flex Cairngorm - How to update the model without losing focus in Datagrid?

Question: In my Flex Project using Cairngorm, I have a model that stores a bindable ArrayCollection displayed by a DataGrid. My problem occurs when the model is updated by the responder (from a Java service, after an "update" event), because the focus located in a cell of the DataGrid is lost (the scroll position il also lost). So, I retrieve the position before the update, with DataGridEvent.ITEM_FOCUS_IN event :
<code>myModel.focusedCell.rowIndex = e.rowIndex;
myModel.focusedCell.columnIndex = e.columnIndex;
</code>
But, from where restore these parameters ? How to trigger this update only when the model is updated ?
Thanks in advance.
Answer: for information, I resolved my problem as follows:
to avoid losing focus (or rather the next cell being edited), one has to update the model without re-instantiating it.
After that, we must resolve a problem with the scroll as explained in the following post:
How to data bind DataGrid component without scrolling up?


Probability that at least one of three exam tickets randomly selected will be hard, if student considers 5 tickets out of 20 difficult.

Question: My interpretation: $$A \text{ - an event, that the ticket is difficult}\\ B \text{ - an event, that the ticket is easy}, n \text{ - the amount of tickets, } m \text{ - the amount of difficult tickets selected } \\ P_{3}(m \ge 1)= C_{3}^{1}\left( \frac{1}{4} \right)^1\cdot\left(\frac{3}{4}\right)^{2}+C_{3}^2 \left(\frac{1}{4}\right)^2\left(\frac{3}{4}\right)^{1}+C_{3}^3\left(\frac{1}{4}\right)^3 \left(\frac{3}{4}\right)^{0}= \\ 3\left(\frac{9}{64}+\frac{3}{64}  \right)+\frac{1}{64}= \frac{37}{64}$$
First, I think it's incorrect, because the probability that the ticket is difficult is not constant, also because the probability to have at least one difficult questions is greater than one half, don't seem likely.
$$P(\text{ "probability that the first pulled ticket is difficult"})=\frac{5}{20}=\frac{1}{4} \\ P(\text{"probability that second ticket is difficult"})=\frac{3}{4}\frac{5}{19}+\frac{1}{4}\frac{4}{19}  \\ P(\text{ "probability that the third ticket is difficult"})=\frac{3 \cdot 5 \cdot 5}{4 \cdot 19 \cdot 18}+\frac{1 \cdot 4 \cdot 3}{4 \cdot 19 \cdot 18}=\frac{29}{4 \cdot 6 \cdot 19}$$
Here it's weird, that no matter whether the difficult ticket is pulled or not the probability that the next ticket is difficult is much smaller, so I assume that the probability I am calculating is not the one I need to. Can anyone help?
Answer: Hint for correct approach: try finding the probability of the complement event (none of the three tickets are difficult).

 The probability that the first ticket is not difficult is $15/20$. Given that event, the probability that the second ticket is not difficult is $14/19$. Given all this, the probability that the third ticket is not difficult is $13/18$. Multiply them together and subtract the result from $1$.

Error in your first approach: this would be the correct approach if the tickets are drawn with replacement (i.e. it is possible to get the same ticket multiple times).
Error in your second approach: the probability that the second ticket is difficult is also $1/4$ (your computation is correct and your expression equals $1/4$, but this approach is unnecessary). The probability that the third ticket is difficult is also $1/4$. Here your computation is incorrect because you forgot some cases... there should be $4$ different cases for the different configurations of the first two tickets. But again this computation is unnecessary; the probability of any individual ticket being difficult is $1/4$. However, computing all these probabilities will not really help you answer the question of finding the probability that at least one is difficult.
Comment: Ok, but would it also be possible to write this probability as $P(A)=3\frac{5(15\cdot 14 + 15 \cdot 4 + 4 \cdot 3)}{20 \cdot 19 \cdot 18}$ Or does this expression excludes some instances?


How to create a synchronize block using a @PathVariable in a Spring MVC controller?

Question: I have the need to create a synchronized block in a Spring MVC controller, in which I want to use a @PathVariable as my synchronizing object.
@RequestMapping(value = "/upload/{providerId}/{contractId}", method = RequestMethod.POST, headers = "content-type!=multipart/form-data")
public void uploadChunked(
        @PathVariable final long providerId,
        @PathVariable final Long contractId ) {

    Attachment attachment = new Attachment();

    // store the attachment
    synchronized( contractId ){
        contractService.addAttachment( providerId, contractId, attachment );
    }
}

I'm auto boxing contractId into a Long and using it for the synchronize object, however synchronizing on auto boxed values could be an issue if I create another synchronize block elsewhere against an auto boxed Long.  According to the JLS 5.1.7

Ideally, boxing a given primitive value p, would always yield an
  identical reference. In practice, this may not be feasible using
  existing implementation techniques.

Consequently, this block may be waiting for a completely unrelated synchronize block on a boxed Long to complete which is unacceptable.
However, given that the controller is essentially a singleton, how can I safely synchronize against the contractId?  To be clear, only calls to this method with the same contractId must be synchronized - calls with different contractIds can run concurrently.
I have been unable to find any existing libraries that provide this simple support.  Short of creating my own util lib/class (ex: static concurrent hashmap to store lock objects by contractId), is there another cleaner solution?
Answer: As you suggest, you cannot depend on the primitive wrapper types to synchronize around such values. One possible solution is to maintain a ConcurrentHashMap where you putIfAbsent a new lock object using your contractId as a key. If the method returns an object, you lock on that. If it doesn't, you lock on the object you created. You can then clear the entry from the map.
This obviously won't work over many deployments of the application.
Comment: Thanks for the suggestion; that's pretty much what I had figured as well.  But in order to prevent memory leaks, as you suggested, I have to clear the entry from the map when I am done using it.  Which means I have to track the number of times it is requested as a lock object and returned.  Which itself (tracking the number of times it is borrowed/returned) has to be in a synchronized block.  So basically, managing the semaphore myself.  I am surprised that there isn't already a library out there that already encompasses all this work for you.  But I couldn't find anything in Guava or other.
Answer: There is a tool called XSync - a thread-safe mutex factory, that provide ability to synchronize by the value of the object (not by the object).
And you can use it for all type of objects which you need.
https://github.com/antkorwin/xsync
    xSync.execute(contractId, () -> {
           contractService.addAttachment( providerId, contractId, attachment );
    });


.NET Framework 2.0 vs 3.5/4.0... any possible advantage to use the older version?

Question: My project leader insists on using Visual Studio 2005 which only targets the .NET 2.0 framework and only supports C# 2.0 syntax.
He insists that the 2.0 framework has higher compatiblity and performance for older computers.  By old, I mean 5-8 year old Windows XP machines.
Assuming we only support a minimum of Windows XP / Server 2003, is there any advantage at all to using .NET 2.0?
I am pretty sure that even targeting .NET 2.0 we can take advantage of C# 3.0 syntax in our code (lambdas, auto-implemented properties, etc.) if we upgrade past VS2005.
Comment: I would opt for at least Visual Studio 2008 and .NET 3.5. There is enough in there to keep you guys busy and happy for some time. C# 3 is just so bloody awesome. I've been faithfully reaping the benefits since 2008.
Comment: I Agree with frank, [linq is the best](http://stackoverflow.com/tags/linq/info) thing since sliced bread! Seriously!
Comment: Hey if the price of Visual Studio is the issue then perhaps you may want to look into SharpDevelop? My feeble brain can't think of a reason to limit oneself to .NET 2.0 other than the price of a nice IDE. Can't beat free, try out SharpDevelop.
Comment: I am just curious if your project leader's thoughts are the same today, in September 2018, or changed?
Answer: .Net 2.0 is considered to be more widely deployed, but 3.5 isn't far behind any more. However, that's no reason to limit yourself to Visual Studio 2005 or C# 2.0.  Visual Studio 2010 can target .Net 2.0, and use C# 3.0 features when it does so to boot.
Comment: C# 3.0 features without .NET 3.5 is like cereal without milk :(
Comment: +1 for VS2010 IDE.  It's hard to use 2008 and even harder to use 2005 after using 2010 for so long now.
Answer: .NET 2 is no longer supported by Microsoft. Well, Microsoft makes the terms tougher to explain. For Windows XP and Windows Server 2003, you have to at least use .NET Framework 3.5 SP1,
http://blogs.technet.com/b/lifecycle/archive/2010/04/30/net-framework-3-5-sp1-and-later-now-supported-as-part-of-microsoft-windows.aspx
so that you can receive assistance from Microsoft support team.
Therefore, I see no reason to stay on .NET 2.
"the 2.0 framework has higher compatiblity and performance for older computers" is a joke for me. When you lose support-ability from Microsoft, you lose everything.
Answer: Normally, people who would like to use the older version is mainly because they think the old one is more stable and better to production.
But .NET 2.0 is quite out of date already, and .NET 3.5 and .NET 4.0 have been proven to be very stable and make things easier. (such as LINQ, you can try functional style programming via it, very fluent and productive)
For the IDE part, Microsoft rebuild the visual studio 2010, it is more stable and productive, and it is also can target .NET2.0 if you want.
So, why not use Visual Studio 2010 instead of 2005, even if your project is purely .NET2.0, the former also has downward compatibility.


Network bonding mode balance-rr / 802.3ad layer3+4 on Linux

Question: What is the difference between balance-rr and 802.3ad with xmit_hash_policy set to layer3+4.
https://www.kernel.org/doc/Documentation/networking/bonding.txt

layer3+4: This algorithm is not fully 802.3ad compliant.  A
single TCP or UDP conversation containing both
fragmented and unfragmented packets will see packets
striped across two interfaces.  This may result in out
of order delivery.  Most traffic types will not meet
this criteria, as TCP rarely fragments traffic, and
most UDP traffic is not involved in extended
conversations.  Other implementations of 802.3ad may
or may not tolerate this noncompliance.
Answer: 802.3ad with xmit_hash_policy set to layer3+4 is a bonding mode that is one of the standard, sensible, and widely used ones. Like many others it doesn't allow >100% throughput for a single TCP connection. In practice the fragmented packet disclaimer you quoted is not relevant as far as I've seen.
balance-rr is a very specific and rarely used balancing mode. It's mainly useful for a dedicated backup-restore subnet. When maintained with the joint network and system admin staff, with the switch specifically set up and all the servers/routers specifically set up, with a little bit of testing, you could achieve >100% throughput for a single TCP connection. Don't expect 200% though. (Remember to test both directions, unless you like your restores very slow). If a link gets down, the remaining one(s) will function.
You've already linked to a detailed technical description, so I've figured out you'd like a high-level answer.
Comment: Not 200% but very close: https://pastebin.com/XD5vmTr5 - balance_rr is a massive improvement for single TCP connections compared to LACP.
Answer: Link aggregation group (LAG) is used not only to increase bandwidth, also it used to link backup. If one link of LAG will down the balance-rr  LAG won't work properly, but the 802.3ad LAG will work good with reduced bandwidth.
The balance-rr  LAG allows to use full LAG's bandwidth for one TCP/UDP connection, but the 802.3ad  LAG allows to use only one link bandwidth for it. The balance-rr  LAG isn't support backup-restore. It perfect works with Cisco Etherchannel LAG. Many Cisco switches support only Etherchannel LAG and don't support 802.3ad LACP.
Comment: "_Many Cisco switches support only Etherchannel LAG and don't support 802.3ad LACP._ As  far as I know, Cisco switches do support both PAgP _and_ LACP. LACP is configured on an interface with the `channel-group  mode [active|passive]` command.
Comment: Only 65xx and some others, but they didn't support `layer3+4` hash.
Comment: That is completely incorrect. All current models support LACP. I think you are working from very, very old information.
Comment: I didn't say about only newest equipment. But new 2960 Catalist can't work with LACP `fast mode` or with `layer3+4` load balance. PAgP works only between Cisco equipments.


combined p values from different hypothesis

Question: I am currently working on a project in which I'm simultaneously identifying and quantifying protein in k samples. Experimentally, for each of the k samples, I have n identified proteins, with a p value that gives me the reliability of that identification, and the quantity of each protein in each sample. I don't have much control on how this p value is computed.
Other than that, samples are independent, treatment and analysis is the same.
For each of the n proteins I compute a combined p value from the individual p values for each protein, using either Fisher's or Stouffer's, to assess the significance of identifying each protein in the k samples, and I get a p value for the identification of each protein, call it p1.
Then, I want to know the average quantity of each protein in the 4 samples, and I'm testing to check where H0: measured_average = goal_average, and I get a p value for this for each protein, call it p2.
I've been reading a lot of research papers both on statistics and on the experiments I'm performing, and to the best of my knowledge and readings, combining p values, and even combining combined p values, can only be performed if the statistic tests for each individual p value are the same, which I guess it is not the case.
My question – can I combine p1 and p2? Is there anyway to have an aggregated p value for this situation? Or I just leave it at that?
Answer: The view that all the tests have to be the same, although widely believed, is not true. I have provided a detailed account in my answer Test for significant excess of significant p-values across multiple comparisons
To summarise from that

The null hypothesis H0 is well defined, that all pi have a uniform distribution on the unit interval. There are two classes of alternative hypothesis

HA: all pi have the same (unknown) non--uniform, non--increasing density,
HB: at least one pi has an (unknown) non--uniform, non--increasing density.

If all the tests being combined come from what are basically replicates then HA is appropriate whereas if they are of different kinds of test or different conditions then HB is appropriate. Note that Birnbaum specifically considers the possibility that the tests being combined may be very different for instance some tests of means, some of variances, and so on.


Are multiple gears allowed on track cycling bicycles?

Question: It seems to me that most track bikes I see have only one gear ratio. For some events I can imagine that having multiple gears would simply be added weight and drag, but on other events, like the short sprints, it seems like a couple of different gears would be of benefit.
Are multiple gears allowed on track cycling bicycles? If so, why are they not used?
Answer: Track bicycles are not permitted to have multiple gears under UCI regulations.
From their document General Organisation of Cycling as a Sport, in Chapter III Equipment, part 1.3.025:

Freewheels, multiple gears and brakes are not permitted for use on the track during competition or training.