How to insert ® value in database

Question: I am inserting users from drupal6 to drupal7 using a custom script. I am using db_insert function.
In one of the D6 entry I am getting ® mark in a field and because  of this that particular value become blank in D7 database.
D6:  imatter®    ->   D7:
Anyone has any idea about how to insert this value in database?
Collation is same in both database: utf8_general_ci
And in phpmyadmin its look like same as above, means no html code for ®
Comment: what is your query statement?
Answer: In the case that your insertion script is not capable of handling UTF-8 encoding (it sounds like this is the case), you can get around this by inserting this character as 7-bit HTML entities (i.e. ® or ®) provided the field where it appears is going to be rendered as HTML (and not as plain text).  Just do a search and replace and replace all occurences of ® with ® in the source text file.
If this workaround doesn't work (because the field is supposed to be plain text, or for some other reason), you must do two things:

Make sure your file containing these characters is UTF-8 (not ISO-8859-1 or some other encoding incompatible with UTF-8).
Make sure your insertion script is capable of handling UTF-8 (db_insert is, but I don't know what other components you use).
Answer: Try utf8_encode before store you variable
$username=$olduser.'®';
$newusername =  utf8_encode($username);

or Drupali method
$username=$olduser.'®';
$newusername =   drupal_convert_to_utf8($username,'utf-8');

Comment: @GisleHannemyr,tnx, I update my answer
Comment: Your first suggestion (call to PHP `utf8_encode`) will *only* work if the source file is in ISO-8859-1 encoding. Re: [utf8_encode](http://www.php.net/manual/en/function.utf8-encode.php) Yes, it may work, or it may not, depending on the the source data format.  Your second suggestion (call Drupal API `drupal_convert_to_utf8`) is a no-op, and will not work here.

How to insert ® value in database


Analysis of Limits using Contour Maps for the function $f(x,y)=xy/|xy|$ as $(x,y)$ $\rightarrow$ $(0,0)$

Question:
I'm trying to evaluate the limit as (x,y)$\rightarrow$ (0,0) of the function $z=xy/|xy|$. I have the contour plot for the function that I use to find which line I should use when deducing the limit. I know the 3d graph of z looks like

I'm not sure along what path to check for the existence of the limit. The contour diagram doesn't show any recognizable function to me.
In addition to the commonly used paths, y=kx and $y=kx^2$, what are some other paths I can try to find the multivariable limit? The answer keys says that I should use the path y=kx and I don't see why I should use the path y=kx rather than some other path such as $y=kx^2$ or y=1/x? or y=sin(x) or y=e^x or y=tan(x).
Side question, if appropriate, can we use any function as a path that we use in the two-path test for the existence of a limit?

If I had the two contour plots shown above for some function, what path would I use to test for the existence of a limit? I don't think y=kx or y=kx^2 fit the sequence shown in the contour plot.
Answer: If you find two different limits, following two different paths respactively, then the limit does not exist.
It is proved that if a limit of a function exists at a given point then it is unique.
Thus uniqueness of the limit is a necessary condition for the existence of the limit.
For $\frac{xy}{|xy|}$ if you take the path $(x,x)$ i.e $x=y$ then you have that
$\frac{xy}{|xy|}=\frac{x^2}{|x^2|}=1 \rightarrow 0$ as $x \rightarrow 0$
If you take the path $(x,-x)$ ,  then $$\frac{xy}{|xy|}=\frac{-x^2}{|x^2|}= -1 \rightarrow -1$$  as $x \rightarrow 0$
Thus we found two paths that go to zero and the limit in the fisrt path is $1$
and in the second path is $-1$
If the limit existed at $(0,0)$ then the value of it, would independent of our choices of paths.
In simpler words it would have tha same value on all paths we choose to approximate it.
If you want a second way the you can do this:
Take polar coordinates: $$x=r \cos{t}$$ $$y=r \sin{t}$$
If you substitute you will have the expression $$\frac{\cos{t} \sin{t}}{|\cos{t} \sin{t}|}$$
If thelimit exists then its value would be independent of $t$ as $r \rightarrow 0$
But for $t_1= \frac{\pi}{3}$ and $t_2=\frac{\pi}{4}$ you have differend values for the above expression.Thus the limit does not exist.


Propagation of errors for a sum of fractions of random variables

Question: Say I have a random variable $Z$ that is itself a function of two independent random variables $g(X,Y)$, where
$$Z = g(X,Y) = \frac{X}{Y}$$
I know that the propagation of uncertainty gives a good approximation of $Var[g(X,Y)]$ using Taylor expansions around the means $\mu_X,\mu_Y$, such that, when $X, Y$ are independent,
$$Var[Z] \approx
\Bigg(\frac{\delta g(x,y)}{\delta x}\Bigg)^2 \sigma_x^2 + \Bigg(\frac{\delta g(x,y)}{\delta y}\Bigg)^2 \sigma_y^2 =
\frac{\sigma_x^2}{\mu_Y^2}  + \frac{\sigma_y^2 \mu_X^2}{\mu_Y^4} $$
Suppose now that $Z$ is a function of two series of independent random variables, $g(X_1,...,X_k,Y_1,...Y_k)$, such that
$$Z = g(X_1,...,X_k,Y_1,...Y_k) = \frac{X_1}{Y_1} + \dotsb + \frac{X_k}{Y_k}$$
If each of the ratios are independent, I am wondering if the variance of $Z$ can be calculated by taking the sum of propagation of uncertainty for each of the fractions, i.e.,
$$Var[Z] = \sum_{i=1}^k Var\Bigg[\frac{X_i}{Y_i}\Bigg]
\\ \approx \sum_{i=1}^k \Bigg[\Bigg(\frac{\delta g(x_i,y_i)}{\delta x_i}\Bigg)^2 \sigma_{x_i}^2 + \Bigg(\frac{\delta g(x_i,y_i)}{\delta y_i}\Bigg)^2 \sigma_{y_i}^2 \Bigg]$$
where for the $j^{th}$ term, I calculate partial derivatives with respect to $x_j$ and $y_j$ as in the basic case presented above and ignore the other $i \neq j$ terms? Or do I need to do out each partial derivative with respect to the complete series of $\{x_i\},\{y_i\}$ ?
Answer: Yes, the variance of the sum is the sum of the variances, in this case.
By the law of the unconscious statistician, for $i\neq j$:
\begin{align*}
E\left[\frac{X_i}{Y_i}\frac{X_j}{Y_j} \right] &= \iiiint \frac{X_i}{Y_i}\frac{X_j}{Y_j} f(x_i)f(x_j)f(y_i)f(y_j)dx_i dx_j dy_i dy_j \\
&= E\left[\frac{X_i}{Y_i}\right] E\left[\frac{X_j}{Y_j} \right].
\end{align*}
So $\text{Cov}\left(\frac{X_i}{Y_i},\frac{X_j}{Y_j}\right)=0$.
Edit:
Responding to your comment:
\begin{align*}
\text{Var}\left(\sum_i \frac{X_i}{Y_i} \right) &= \text{Cov}\left(\sum_i \frac{X_i}{Y_i},\sum_i \frac{X_i}{Y_i} \right) \tag{defn}\\
&= \sum_i \sum_j \text{Cov}\left(\frac{X_i}{Y_i} ,\frac{X_j}{Y_j}  \right) \tag{bilinearity of cov}\\
&= \sum_{i=j}\text{Cov}\left(\frac{X_i}{Y_i} ,\frac{X_j}{Y_j}  \right) \tag{previous}\\
&= \sum_i \text{Var}\left( \frac{X_i}{Y_i} \right) \tag{defn}
\end{align*}
Comment: Thanks! I don't totally follow how we get to variance still -- I'm just trying to tighten my intuition on this and perhaps you could provide a bit more detail in your answer or in a comment? In the above example, is it true that my variance estimate using propagation of uncertainty can just be swapped in for $Var(X_j/Y_j), j \in \{i\}$ in the more complex example (even though its not a proper analytic solution but an estimate)? You seem to be suggesting that I've just got a generalization equivalent to $Var(\sum_i X_j) = \sum_i Var(X_j)$ in other words? (Sorry for sloppy or unorthodox usage!)
Comment: @isosceleswheel I haven't checked your math for the approximation to the variance of a single ratio. However, I just added some stuff to my answer to show you why you can interchange the order of the sum and the variance operator. Let me know if this helps.
Comment: Thanks, I think I can see why I'm allowed to separate the Variances in this case, given the conditions. I will do some additional editing for a fuller version of my exact problem, since there are some other conditions that might break down the independence (but I don't think so because the details are just additional constants for the most part...). Short of a typographical error, the math should be OK for the propagation of uncertainty, as it is just straight from a book/online examples. Anyway, if you happen to stop by in a day or so maybe you can weigh in again. ;)


Condition variable usage pattern in C/C++ and other languages

Question: If you look at documentation describing the usage of condition variables (cv), you'll see that e.g. in PThreads and C++ you don't need to hold the mutex of a cv to call notify on this cv. Whereas e.g. in Java and Python, you must lock the mutex to do the same thing.
Is there some deep reason why things are implemented this way (I'm about the latter case), given that an implementation of a language like Java eventually uses some native threading tools?
Comment: Probably it's not implemented on top of just pthreads .. but that is just an assumption. I feel this question might get better (or even *any*) answers on http://cs.stackexchange.com/ ...
Comment: Obviously the JVM could implement it like that, but it makes a common mistake that I've seen many, many people do in c++ much less likely at a reasonably low cost (you need to hold a lock a bit longer but that's it)
Comment: @Voo The mistake being that  the mutex is not held while modifying the data the condition depends on?
Comment: @vehsakul Checking the *condition* (which usually depends on the data being modified) without holding the lock. Yes there are situations where this is perfectly valid, but it creates a big opportunity for bugs. Managed languages are in the end all about trading performance *options* for safety. Personally I find the tradeoff reasonable here, I don't think there are many real life examples where it'd make a big difference.
Comment: @Voo Ok, your point about safety vs. performance is clear.
Comment: Also if using the PThreads-API from C/C++ it is good practice to lock the condition's mutex before signalling the condition as if not (citing POSIX ) "*The thread(s) that are unblocked shall contend for the mutex according*" and "*if predictable scheduling behavior is required, then that mutex shall be locked by the thread calling pthread_cond_broadcast() or pthread_cond_signal().*". http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_broadcast.html
Answer: The Java notify and notifyAll basic synchronization tools both require you to synchronize on the object before calling them. This is for a simple safety point since it also requires you to synchronize on them before waiting.
For example if you have two threads. One thread reads data from a buffer and one thread writes data into the buffer.
The reading data thread needs to wait until the writing data thread has finished writing a block of data into the buffer and then it can read the block.
If wait(), notify(), and notifyAll() methods can be called without synchronization then you can get a race condition where:

The reading thread calls wait() and the thread is added to waiting queue.
At the same time, the writing thread calls notify() to signal it has added data.
The reading thread misses the change and waits forever since the notify() was processed before the wait() was.

By forcing the wait and notify to happen within a synchronized block this race condition is removed.


How to obtain contrasts for constrained longitudinal data analysis?

Question: I would like to apply a constrained longitudinal data analysis (cLDA) with a linear response variable of the form, y ~ Time + Treatment:Time + (1 | ID). I am using "constrained" whereby the main effect of Treatment is removed, i.e. treatment levels are constrained to have the same baseline mean of y. I found out from preliminary analysis that y is significantly different in both Treatment levels at baseline (i.e. first time point of Time).
I tried the following code using {emmeans} package but it does not work:
emmeans(lmer(y ~ Time + Treatment:Time + (1 | ID), data = df),
        specs = trt.vs.ctrl ~ Time|Treatment)$contrasts

NOTE: A nesting structure was detected in the fitted model:
Treatment %in% Time
Error in .nested_contrast(rgobj = object, method = method, interaction
= interaction,  :    There are no factor levels left to contrast. Try taking nested factors out of 'by'.

When I tried to use the argument nesting = NULL, it surprisingly works.
emmeans(lmer(y ~ Time + Treatment:Time + (1 | ID), data = df),
        specs = trt.vs.ctrl ~ Time|Treatment, nesting = NULL)$contrasts
# Treatment = Sedation:
#   contrast                            estimate    SE  df t.ratio p.value
# OR - (30-min Pre-OR)                  0.0667 0.248 224   0.269  0.9841
# (30-min Post-OR) - (30-min Pre-OR)    0.0000 0.248 224   0.000  1.0000
# (60-min Post-OR) - (30-min Pre-OR)    0.0000 0.248 224   0.000  1.0000
# (120-min Post-OR) - (30-min Pre-OR)   0.0000 0.248 224   0.000  1.0000
#
# Treatment = Acupuncture:
#   contrast                            estimate    SE  df t.ratio p.value
# OR - (30-min Pre-OR)                 -0.6429 0.257 224  -2.505  0.0458
# (30-min Post-OR) - (30-min Pre-OR)   -0.9643 0.257 224  -3.758  0.0009
# (60-min Post-OR) - (30-min Pre-OR)   -1.1071 0.257 224  -4.314  0.0001
# (120-min Post-OR) - (30-min Pre-OR)  -1.4643 0.257 224  -5.706  <.0001
#
# Degrees-of-freedom method: kenward-roger
# P value adjustment: dunnettx method for 4 tests

However, I am confused why the results are similar when I included the main effect of Treatment in.
emmeans(lmer(y ~ Treatment*Time + (1 | ID), data = df),
        specs = trt.vs.ctrl ~ Time|Treatment)$contrasts
# Treatment = Sedation:
#   contrast                            estimate    SE  df t.ratio p.value
# OR - (30-min Pre-OR)                  0.0667 0.248 224   0.269  0.9841
# (30-min Post-OR) - (30-min Pre-OR)    0.0000 0.248 224   0.000  1.0000
# (60-min Post-OR) - (30-min Pre-OR)    0.0000 0.248 224   0.000  1.0000
# (120-min Post-OR) - (30-min Pre-OR)   0.0000 0.248 224   0.000  1.0000
#
# Treatment = Acupuncture:
#   contrast                            estimate    SE  df t.ratio p.value
# OR - (30-min Pre-OR)                 -0.6429 0.257 224  -2.505  0.0458
# (30-min Post-OR) - (30-min Pre-OR)   -0.9643 0.257 224  -3.758  0.0009
# (60-min Post-OR) - (30-min Pre-OR)   -1.1071 0.257 224  -4.314  0.0001
# (120-min Post-OR) - (30-min Pre-OR)  -1.4643 0.257 224  -5.706  <.0001
#
# Degrees-of-freedom method: kenward-roger
# P value adjustment: dunnettx method for 4 tests

Furthermore, this is the same when I tried to find the contrasts within each Time level. For the first time point, 30-min Pre-OR, the contrast is significant though I tried to adjust it.
emmeans(lmer(y ~ Time + Treatment:Time + (1 | ID), data = df),
        specs = trt.vs.ctrl ~ Treatment|Time, nesting = NULL)$contrasts
# Time = 30-min Pre-OR:
#   contrast               estimate    SE  df t.ratio p.value
# Acupuncture - Sedation   1.5381 0.266 269   5.776  <.0001
#
# Time = OR:
#   contrast               estimate    SE  df t.ratio p.value
# Acupuncture - Sedation   0.8286 0.266 269   3.111  0.0021
#
# Time = 30-min Post-OR:
#   contrast               estimate    SE  df t.ratio p.value
# Acupuncture - Sedation   0.5738 0.266 269   2.155  0.0321
#
# Time = 60-min Post-OR:
#   contrast               estimate    SE  df t.ratio p.value
# Acupuncture - Sedation   0.4310 0.266 269   1.618  0.1068
#
# Time = 120-min Post-OR:
#   contrast               estimate    SE  df t.ratio p.value
# Acupuncture - Sedation   0.0738 0.266 269   0.277  0.7819
#
# Degrees-of-freedom method: kenward-roger

What should I do, if I need to adjust for the baseline value of y (which I understand cLDA is one way) and obtain the contrasts too?
Comment: The idea of treating the baseline measurement of Y as the first follow-up measurement is very controversial.  I go into great detail about this in the longitudinal modeling chapter of [RMS](https://hbiostat.org/rms).  Often the baseline Y is manipulated (e.g., serves as a study entry criterion) resulting in the need for a different distribution for Y for the first time.  No one has developed a parametric longitudinal analysis method for different distributions over time.  Why suffer such complexities as opposed to adjusted for a baseline as baseline?
Comment: The model specification Time + Treatment:Time explicitly specifies that Treatment is nested in Time. So you shouldn't be surprised that by default you can't compare times conditional on treatment. Specifying nesting = NULL bypasses the detection of nesting structures. So decide what you want. If you don't think Treatment is nested in Time, then don't fit a model that specifies that it is.
Comment: It also seems very suspicious that you have exact equality of means over time  in the sedation group. Are you including the same measurements more than once? If so, that is a serious flaw in your analysis.
Answer: Let me tell you that this is not the cLDA.
You didn't remove the intercept from the model matrix. You don't have to believe me - please fit the model and compare the baseline values for both treatments. Unless a very lucky coincidence I can guarantee they will be different, which invalidates the cLDA. In cLDA they must be equal due to the constraint.
You have to either adjust the model.matrix or assign appropriate factor levels to the treatment and fit only with treatment.
Take this one:
https://rdoodles.rbind.io/2020/03/analyzing-longitudinal-data-a-simple-pre-post-design/
or this one: https://datascienceplus.com/taking-the-baseline-measurement-into-account-constrained-lda-in-r/


Tabela de configuração

Question: Como eu poderia fazer para usar uma tabela de configuração no nHibernate? Ou seja, uma tabela de apenas um registro e que ele só controlasse esse registro, sei que tem como fazer isso via aplicação, mas queria saber se o nHibernate já tem algum recurso de trata isso automaticamente.
Comment: Sabe que pode votar em tudo no site também?
Answer: Posso estar enganado porque faz tempo que não mexo com isto ou por não ter entendido a pergunta mas acho que não existe. Nem teria motivos para existir nada específico. Não é função do ORM ficar tratando desses detalhes. Claro que tem algumas coisas normais do framework que você pode usar para facilitar o acesso a esta tabela.  Em geral uma tabela assim é lida uma vez e colocada no cache.
Talvez seja melhor fazer uma tabela com pares de propriedade e valor. Pelo menos dá mais flexibilidade. É só uma ideia.

Tabela de configuração


An Interesting riddle on Rational's and Primes

Question: We are supposed to find all rational numbers $\frac{m}{n}$  such that the following set of conditions are true:
$$1)0<\frac{m}{n}<1$$
$$2)H.C.F [m, n] =1$$
$$3)m⋅n = 30!$$
Here is my attempt at it:

I used Legendre's formula to find out powers of prime factors involved so I arrived at the following: $2^{26}, 3^{14},5^{7},7^{4},11^{2},13^1,17^1,19^1,23^1,29^1$ now to make m, n and to make them coprime I decided to make groups of these factors(prime numbers) without distributing the powers of these primes i.e. I am taking whole of $2^{26}$ in a group not taking $2^{13}$ in one and $2^{13}$ in other,as that will result in H.C.F being 2 not 1. hence we need to distribute these 10 factors in groups of 2. We can make these many groups by the following scheme : $\binom{10}0+\binom{10}9+\binom{10}8+\binom{10}7+\binom{10}6+\binom{10}5$ this is because we can choose 10 factors at once and call it n (here m being 1) also we can choose 9 factors and in each choice 1 of the 2 pairs will result in greater number so accordingly for each $\binom{10}9$ 1 possibility would be chosen so we can keep doing this till we reach $\binom{10}5$ now furthur choosing will not result in new cases being formed just same groups will be made again. Hence this should be the answer.
Comment: Shouldn't it be $\frac{2^{\text{no. of prime divisors of }30!}}2?$ Let $x_i=p_i^{\alpha_i}$. It can be in either of the divisors. You don't want ordered pairs since you would both overcount and you need $m<n$.
Comment: Kindly let me know if there are any errors in my answer
Comment: Yes I got my mistake
Answer: (1) In your list of prime powers in the first paragraph you left out $7^4$ (though you seemed to have counted it in your total of $10$ primes).
(2) The ${{10}\choose{5}}$ term should be divided by $2$, otherwise you count the answers with five prime factors in each part twice. (For instance once you select $2, 3, 5, 7, 11$ as the primes, and once you select $13, 17, 19, 23, 29$ as the primes, but that gives the same values for $m$ and $n$. (With this correction, you should get an answer consistent with the comment of Summer Child.)
Comment: Yeah that was a typo, (my bad) corrected!, Yeah thankyou so much I got it brother.
Answer: I don't get the method by which you calculate the number of solutions.  There are exactly $2^{10-1} = 512$ solutions:  $30!$ factors into 10 prime powers:
$$30!=\prod_{k=1}^{10}p_k^{m_k}$$
where $p_k$ are primes and $m_k\in\Bbb N^+$. In order to enumerate solutions, consider all bit strings $S\in\{0,1\}^9$ of length 9 and set
$$ a = a(S) = p_1^{m_1} \prod_{k=2}^{10}p_k^{m_k\cdot b_k}$$
where $b_k\in\{0,1\}$ is the $k-1$-th bit of $S$. Then define
$$\begin{align}
m &= \min(a, 30!/a) \\
n &= \max(a, 30!/a) \\
\end{align}$$
For example, when we order the primes such that $p_k < p_{k+1}$:
$$\begin{align}
a((0,0,0,0,0,0,0,0,0)) &= 2^{26} \\
a((1,0,0,0,0,0,0,0,0)) &= 2^{26} \cdot 3^{14} \\
a((0,1,0,0,0,0,0,0,0)) &= 2^{26} \cdot 5^{7} \\
a((1,1,0,0,0,0,0,0,0)) &= 2^{26} \cdot 3^{14} \cdot 5^{7} \\
&\ \ \vdots \\
a((1,1,1,1,1,1,1,1,1)) &= 2^{26} \cdot \underbrace{3^{14} \cdot 5^{7} \cdot 7^4\cdots 29}_{\textstyle 9\text{ factors}} =30!\\
\end{align}$$
Then we have that $\gcd(m,n) = 1$ and $0<m<n$ and $m\cdot n=30!$ by construction. $m\neq n$ because $30!$ is not a perfect square, and $m(S_1)\neq m(S_2)$ whenever $S_1\neq S_2$ because prime factorization is unique (the order of the primes is fixed).

You can also calculate the numer of solutions as follows:  $30!$ factors into 10 prime powers. You have $2^{10}$ ways to select whether a prime power shall be a factor of $m$ or not, and the other way rounf for $n$, i.e. $n=30!/m$.  Exactly half of all the $m$'s will satisfy $m>n$ and hence are no valid solutions.  The other half of the $m$'s satisfy $m<n$ and are valid.  Thus, there are $2^{10}/2$ valid pairs $(m,n)$.
Comment: Thankyou so much emacs, I am still working on developing my combinatorics thankyou


How to use RaVis Library? Some ideas?

Question: I'va been monkeing with this lib : Ravis.
It a librari on .swc, someone know how to use or configure this library on NetBeans 6.5?
I use Windows XP and Vista!
Thanks to all
Comment: Which Netbeans plugin do you use for Flex development? If it is Flexbeans, go into project properties and all additional library/source folders.
Comment: Another option, of course, is to checkout the Ravis source code into your project src folder and take it from there.. :)
Answer: I'm using Ravis with flex builder and I can use it just by dropping the .swc into my libs folder. Are you using any other libs with netbeans? If so, what's the error you get with the swc?
Also, it's open source, so you can compile it into your project as well if you want. Mine is at src/com/org/un/cava/birdeye.
Comment: I want to develop a web app, that create in a page a graph from xml (if it's ok).Can You help me Glenn?
Comment: Try using their examples and then adapt them to your needs. I won't be able to be much more help than that unless you have specific problems.

How to use RaVis Library? Some ideas?


Given two mixed independent random variables X,Y and Z=X+Y. Find the pdf of Z.

Question: Given that  X,Y,Z are  all random variables with $Z=X+Y$ and $X,Y$ independent variables,where  $X\sim N(0,1)$ and $Y\sim Bernoulli(p)$ .then,find pdf of $Z$.
By total probability laws we get the cdf of Z as folows: $F_z(t)=F_x(t)(1-p)+F_x(t-1)(p)$.
Now how do i find the pdf of Z? My attempt is that we diferentiate both RHS and LHS with respect to t since X is continous variable, and $Z=X+Y$ then Z is  a continous variable to. Is this argument valid? But then, how do i find domain of f(z) ,where f(z) is pdf of z ?Is the domain  every possible values of x+y thus $-\infty \le z \le \infty$?
Answer: Notice that if we condition $Z$ on $Y$ we have a normal random variable of the form
$$
Z|Y=y\sim\mathcal N(y,1).
$$
Thus, denoting $\phi(t)=\frac{1}{\sqrt{2\pi}}e^{-t^2/2}$ as the standard normal density and $\Phi(t)=\int_{-\infty}^t\phi(u)\,\mathrm du$ as the standard normal distribution, we have according to the law of total probability
$$
\begin{aligned}
\mathsf P(Z\leq z)
&=\mathsf E(\mathsf P(Z\leq z|Y))\\
&=\mathsf E(\Phi(z-Y))\\
&=\sum_{y=0}^1\Phi(z-y)p^y(1-p)^{1-y}\\
&=\Phi(z)(1-p)+\Phi(z-1)p.
\end{aligned}
$$
Differentiating w.r.t. $z$ gives the final result
$$
f_Z(z)=\phi(z)(1-p)+\phi(z-1)p.
$$
As for the support of $f_Z$, recall that $Z$ is the sum of a normal random variable, which can take on any real value, plus an integer. Therefore, the sum can be any real number, i.e., $f_Z$ has support for all $z\in\Bbb R$.

Here is a plot of $f_Z$ for $p=0.25$:
Answer: Yes, your argument is valid, though your notation is maybe a little confusing, since it's not clear what $F(Z=t)$ means.  If you write $F_Z(z)=P(Z\leq z)$ and $F_X(x)=P(X\leq x)$ for the CDFs of $Z$ and $X$ respectively, then the total probability law gives:
$$F_Z(t) = F_X(t)(1-p) + F_X(t-1)p, \quad t\in\mathbb{R}$$
This cdf is continuous (in fact, differentiable!), so that implies that $Z$ is a continuous random variable.  The pdf $f_z(t)$ of $Z$ for all $t\in\mathbb{R}$ can be calculated by differentiating both sides, as you have guessed.  There is no special issue with the domain; it is all of $t\in\mathbb{R}$.  (You are correct that the domain should include all of the possible values of $x+y$ which is all of $\mathbb{R}$.)
The  domain would only become a concern if $F_X$ had a restricted domain.  For example, if $X$ had an exponential distribution with parameter $\lambda=1$, then the CDF could be written:
$$F_X(x)=1-e^{-x}, \quad x\geq 0$$
If you tried to differentiate both sides of the above equation while ignoring the domain, you might get the answer:
$$f_Z(t) = e^{-t}(1-p)+e^{-(t-1)}p$$
but this is incorrect/incomplete.
Instead, you'd have to pay careful attention to the terms $F_X(t)$ and $F_X(t-1)$ and realize that since the complete CDF of $X$ is:
$$F_X(x)=
\begin{cases}
0, & x < 0 \\
1-e^{-x}, & x \geq 0\\
\end{cases}$$
the complete CDF for $Z$ based on the law of total probability:
$$F_Z(t) = F_X(t)(1-p) + F_X(t-1)p, \quad t\in\mathbb{R}$$
needs to be broken down into multiple cases:
$$F_Z(t)=
\begin{cases}
0, & t < 0 \\
(1-e^{-t})(1-p), &0 \leq t < 1 \\
(1-e^{-t})(1-p) + (1-e^{-(t-1)})p, &t \geq 1
\end{cases}$$
and this piecewise differentiable function gives density:
$$f_Z(t)=
\begin{cases}
0, & t < 0 \\
e^{-t}(1-p), &0 \leq t < 1 \\
e^{-t}(1-p) + e^{-(t-1)}p, &t \geq 1
\end{cases}$$
Again, none of this is an issue for your problem, because the domain is all of $\mathbb{R}$.


Kendo Grid - Not showing certain sub grids

Question: I have a kendo grid within an mvc asp.net core application. When I expand the grid into a sub grid, for the most part, it works as expected, showing the sub grid and then making the call to the controller and returning the json data required.
When I expand the root grid to select a row to expand. On specific rows, when the sub grid is created, the root report is almost collapsed, the columns disappear and all the data rows. Subsequently, the call to the controller is still executed as normal and the data is returned. However, the data is not shown on screen as it appears the sub report is not displayed.
Why would the code that works to generate a sub report for one row, not work for another?

This is repeatable on my setup. i.e - I can refresh and restart my browser/application and the same rows cause this issue.

Clicking the second row causes the report to collapse, as if its datasource and columns were removed.

Please note that the root report I speak of, is in actual fact a sub report.
Root Report:

Sub Report:

Comment: I think I may have figured out what is causing it. Not sure how to fix at this stage. The report fails to display when I click the row index corresponding to the parent row index. I.e on my first report. If I click row 1. The sub report will fail when expanding it's row 1. If I click the row 2 on the root report, when expanding row 2 on the sub report. It will fail. and so on. I'll see what I can do to fix this.
Answer: I noticed that the rows which would fail had a pattern.
If I expanded the root report on row 1. The sub report, on expanding it's row 1, would fail.
If I expanded the root report on row 3. The sub report, on expanding it's row 3, would fail.
This made me look into the Name properties of my grids & tabs. I was including an ID on the end as a way of uniquely identifying them. I removed the IDs from the tabs. For the grids I had to further uniquely Identify them, using a combination of model values and Ids.
If anyone understands this behavior further than I do, please comment.


Usar Retrofit para popular um RecyclerView

Question: Preciso trazer uma lista de cidades em um RecyclerView de um WebService usando o Retrofit. Mas a minha List esta vindo null.
Tenho uma Class cidades com os dados que preciso pegar do WebService, a Class DadosCidades onde configuro o Retrofit.
A minha Interface DataServer:
public interface DataServer {
@GET("cidade-nome.php?nome=rio")
Call<List<Cidades>> getCidades();
}

O Adapter CityListAdapter:
public class CityListAdapter extends RecyclerView.Adapter<CityListAdapter.CityViewHolder> {

Context context;
List<Cidades> cidadesList;

public CityListAdapter(Context context, List<Cidades> cidadesList) {
    this.context = context;
    this.cidadesList = cidadesList;
}

@Override
public CityListAdapter.CityViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

   View view = LayoutInflater.from(context).inflate(R.layout.single_row_view, viewGroup, false);

    return new CityViewHolder(view);
}

@Override
public void onBindViewHolder(CityListAdapter.CityViewHolder cityViewHolder, int i) {

    cityViewHolder.txtNome.setText(cidadesList.get(i).getNome());
    cityViewHolder.txtSigla.setText(cidadesList.get(i).getSigla_estado());
    cityViewHolder.txtLatitude.setText(cidadesList.get(i).getLatitude());
    cityViewHolder.txtLongitude.setText(cidadesList.get(i).getLongitude());

}

@Override
public int getItemCount() {

    return cidadesList.size();
}

public class CityViewHolder extends RecyclerView.ViewHolder {

    TextView txtNome;
    TextView txtSigla;
    TextView txtLatitude;
    TextView txtLongitude;

    public CityViewHolder(View itemView) {
        super(itemView);

        txtNome = itemView.findViewById(R.id.txtNome);
        txtSigla = itemView.findViewById(R.id.txtSigla);
        txtLatitude = itemView.findViewById(R.id.txtLatitude);
        txtLongitude = itemView.findViewById(R.id.txtLongitude);
    }
}
}

Um xml single_row_view, com o TextView para o RecyclerView e a minha Activity onde quero exibir a lista de cidades.
Activity SearchActivity :
public class SearchActivity extends AppCompatActivity{

    RecyclerView recyclerView;
    CityListAdapter adapter;
    List<Cidades> cidadesList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        cidadesList = new ArrayList<>();

        recyclerView = findViewById(R.id.recycler_view_notice_list);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        DataServer dataServer = DadosCidades.getDadosCidades().create(DataServer.class);

        Call<List<Cidades>> call = dataServer.getCidades();

        call.enqueue(new Callback<List<Cidades>>() {
            @Override
            public void onResponse(Call<List<Cidades>> call, Response<List<Cidades>> response) {

                cidadesList = response.body();

                Log.d("SearchActivity", cidadesList.toString());

                adapter = new CityListAdapter(getApplicationContext(), cidadesList);
                recyclerView.setAdapter(adapter);

            }

            @Override
            public void onFailure(Call<List<Cidades>> call, Throwable t) {

            }
        });

    }

}

E por fim o xml activity_search:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.activity.SearchActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view_notice_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

No entanto a minha cidadesList = new ArrayList<>(); vem size = 0
Já vi vários tutoriais e até agora não consegui resolver.
Comment: Já verificou se no endpoint está vindo os dados?
Comment: Sim, os dados não chegam, está tudo null. E não entendo o motivo.
Comment: Então, talvez o problema seja na API, tente "bater" nesse endpoint com o postman para recuperar as infos. Se lá vir o problema é no app, se não, provavelmente, na API
Answer: O metódo onResponse te dá apenas um retorno, contudo pode não ter sido o de código 200 (como se esperaria). Tenta jogar no log response.code() pra ver.
Além disso, você pode usar um if com response.isSuccessful() pra ter certeza do retorno e tratar as exceções.


How to compare two array of object and if value are same add new key: value on second object in Javascript

Question: Let's take an example. I've two array of objects
let paymentType = [
  {id: "settle", set: "dis_10"},
  {id: "settle1", set: "dis_20"},
  {id: "settle2", set: "dis_30"},
]

let result = [
  {id: "settle", type: 40},
  {id: "settle4", type: 50},
  {id: "settle2", type: 60},
]

If id from both array matches I want to add the the set value to the result array of object.
Final result will be
let result = [
  {id: "settle", type: 40, set: "dis_10"},
  {id: "settle4", type: 50},
  {id: "settle2", type: 60},
]

I've tried using filter and some method, but didn't work as expected
Answer: I'd reduce() everything to a single accumulator object keyed by id, and then get the values from that:

const paymentType = [
  {id: "settle", set: "dis_10"},
  {id: "settle1", set: "dis_20"},
  {id: "settle2", set: "dis_30"},
];

const result = [
  {id: "settle", type: 40},
  {id: "settle4", type: 50},
  {id: "settle2", type: 60},
];

const merge = (...arrays) => Object.values(
  arrays.reduce((a, array) => array.reduce((a, v) => {
    a[v.id] = { ...(a[v.id] || {}), ...v };
    return a;
  }, a), {})
);

console.log(merge(paymentType, result));

This has a linear runtime complexity (O(n)), as opposed to solutions that invoke find() or findIndex() on every iteration which are O(n log n) at best.
Answer: Hope it helps:

const paymentType = [
  {id: "settle", set: "dis_10"},
  {id: "settle1", set: "dis_20"},
  {id: "settle2", set: "dis_30"},
];

let result = [
  {id: "settle", type: 40},
  {id: "settle4", type: 50},
  {id: "settle2", type: 60},
];

result.forEach((resultItem, index) => {
  const id = resultItem.id;
  // Chech if the id exists in the paymentType array
  const paymentTypeIndex = paymentType.findIndex((paymentTypeItem) => paymentTypeItem.id === id);
  if(paymentTypeIndex > -1) {
    // If the id exists, we add the paymentType item values to the result item.
    resultItem.set = paymentType[paymentTypeIndex].set;
  }
});

console.log(result);
Answer: this might be something that you are looking for if I understood your question well
 result.forEach((item) => {
      if (paymentType.find((el) => el.id == item.id))
        item.set = paymentType.find((el) => el.id == item.id).set;
    });

This will result in
[
    {
        "id": "settle",
        "type": 40,
        "set": "dis_10"
    },
    {
        "id": "settle4",
        "type": 50
    },
    {
        "id": "settle2",
        "type": 60,
        "set": "dis_30"
    }
]


R, linear regression with duplicate data

Question: So I have an data set with

customer ID
Type Of customer service
customer income.

I like to do linear regression, but a single customer ID can have multiple customer service. A costumer with 2 customer service will be put in two rows. How can I perform this type of linear regression?
Comment: You should expect to have dependence within ID; you should perhaps consider  hierarchical / mixed models of some type. Which variable is your response?
Comment: I have search for duplicate and unique command but i don't think it will solve the problem
Comment: 1. Will the same customers at different customer service types have the same income? 2. What are you trying to explore (what do you mean by type of customer service)?
Comment: If your question is specifically about R, then it is off topic here. But if it is about statistics, it's on topic.  So, I will vote to close, but if you remove the reference to R it can be reopened.
Answer: You need to reformat your data.
Say, F is your data, where you have n+1 different Types of customer service. You then make a dummy variable corresponding to using a particular type of service. The number of such dummy variables should be n, if the number of different Types of customer service is n+1. This is to avoid multicollinearity.
<code>Customer_ID TypeOfService_1 TypeOfService_2 TypeOfService_n Customer_Income
1           1               0               0               1000.8
2           0               1               1               6000.9
</code>


Key combinations with 'j' like "rj" doesn't work correctly on VSCode Vim

Question: I'm using Vim for VSCode on macOS.
When I type "rj" to replace a character under the cursor to 'j', it doesn't work and my cursor moves down.
Key combinations with 'j' or 'k' like "rj", "fj", "rk", etc., doesn't work correctly  but with 'h' or 'l', there is no problem.
I'm using latest version of VSCode and Vim for VSCode.
I tried deleting everything on settings.json, but nothing has changed.
I also tried the same thing on vim from terminal.app, it works as I expect.
Answer: I found the cause by myself.
I don't know why, but the wrong key bindings had been set.
I removed the following lines from Keybindings.json, and the problem was solved.
<code>{
    "key": "k",
    "command": "cursorUp",
    "when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode == 'Normal' && !suggestWidgetMultipleSuggestions && !suggestWidgetVisible"
  },
  {
    "key": "j",
    "command": "cursorDown",
    "when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode == 'Normal' && !suggestWidgetMultipleSuggestions && !suggestWidgetVisible"
  }
</code>


Proving that the Bernoulli self similar measure is doubling

Question:
Let $\mu_p$ a measure which is the push forward of the bernouli product measure $(p,1-p)^\mathbb N$. Let S=$\{f_1,\dots f_m\}$ an IFS, a system of functions with attractor $K$, means $$K=\bigcup_{i=1}^nf_n(K).$$
  a. Prove that for $K=[0,1],S=\{\frac{x}{2},\frac{x+1}{2}\}$ and for any $p\neq \frac 1 2$, the measure $\mu_p$ is not doubling its support.
b. Now consider the same $K$ but $S=\{\frac{x}{3},\frac{x+1}{3},\frac{x+2}{3}\}$. The mass is now divided according to probability vector $(p,1-p-q,q)$. Prove tat $\mu_p$ is doubling if and only if $p=q$

I don't really know from where to start. I understand that each singleton on this interval gets weight according to the probability $p,q$ or $1-p-q$ but I can't connect it to terms of balls and doubling measures. Can I get any hint for both parts?
Comment: I deleted the probability tag to make room for the fractals tag. Not that the question is unrelated to probability theory but I see it as very solidly in the fractal category.
Answer: The general topic you are studying is called self-similar measures.  Such a measure can be approximated by a sequence of simpler measures in much the way that a self-similar set can be approximated by a sequence of simpler sets.  Let's examine how the self-similar measure $\mu$ with probability list $\{2/3,1/3\}$ is generated by the IFS
$$S=\{f_1(x),f_2(x)\}=\{x/2,(x+1)/2\}.$$
The invariant set of $S$ is the unit interval, which will also be the support of $\mu$. As a zeroth approximation $\mu_0$ to $\mu$, we'll choose Lebesgue measure restricted to the unit interval.  The sequence of measures will be defined recursively.  Given a Borel set $A$, $\mu_{n+1}(A)$ will be defined in terms of $\mu_{n}$ by
$$\mu_{n+1}(A) = \frac{2}{3}\mu_n(f_1^{-1}(A)) + \frac{1}{3}\mu_n(f_2^{-1}(A)).$$
For example, $\mu_1([0,1/2])$ satisfies
\begin{align}
\mu_{1}([0,1/2]) &= \frac{2}{3}\mu_0(f_1^{-1}([0,1/2])) + \frac{1}{3}\mu_0(f_2^{-1}([0,1/2])) \\
 &= \frac{2}{3}\mu_0([0,1]) + \frac{1}{3}\mu_0([1,2]) \\
 &= \frac{2}{3}\cdot1+\frac{1}{3}\cdot0=\frac{2}{3}.
\end{align}
The other half, similarly satisfies $\mu_1([1/2,1])=1/3$.  Thus, application of the IFS has the effect of redistributing the measure in a sense.  We can visualize the process using a sequence of histograms.  Here's what Lebesgue measure looks like together with the first five steps in the process.

And, here's a higher order approximation:

Of course, there is a limit measure (in the weak-$*$ topology) that satisfies
$$\mu(A) = \frac{2}{3}\mu(f_1^{-1}(A)) + \frac{1}{3}\mu(f_2^{-1}(A))$$
for all Borel sets $A$.
Now, look at what is happening near $1/2$.  The measure just to the right of $1/2$ is much heavier than the measure just to the left of $1/2$.  Specifically, they can be determined as the terms in the binomial expansion of
$$\left(\frac{2}{3}+\frac{1}{3}\right)^n.$$
I think this should give you more than enough to do part (a). Just imagine applying the doubling criterion at a point like $2^{-1}-2^{-n}$.

Part (b) should rely on very similar ideas.  You should understand that these ideas generalize quite a lot.  Thus, you can have IFS with $m$ functions $\{f_1,f_2,\ldots,f_m\}$ and you can define a recursive process like
$$\mu_{n+1}(A) = \sum_{i=1}^m p_i \, \mu_n(f_i^{-1}(A))$$
to get a measure $\mu$ satisfying
$$\mu(A) = \sum_{i=1}^m p_i \, \mu(f_i^{-1}(A)).$$
You should think about how the measure accumulates on either side of the $1/3$ or $2/3$, perhaps using a trinomial expansion.
Comment: I start to understand. thanks. about the specific questions:
a. after taking $x=B(2^{-1}+2^{-n},2^{-n})$ as the ball, the criterion gives a restriction on C rather then getting that C doesn't exist. Maybe it mustn't depend on $n$?
b. I still don't understand how to prove $p=q\Leftrightarrow\mu_{p,q}$ is doubling. Can you provide additional guidance? thanks again.
Comment: (a) we get a lower bound for C and since $p<1$, it means that C holds even for smaller intervals, which we get after enlarging the n. So it's sufficient to choose large n and then say that for $n-k\quad(k\in\mathbb{N})$ the C is too small? that seems too abstract.
(b) Why when $p=q$, the measure is Lebesgue? for  $p=q=\frac 1 3$, yes but for other values, I can't find any reason why should it hold. and about the another direction I'll rethink about it.
Comment: (a). fine. (b) $p+q+1-p-q=p+1-2p+p=1$ so the sum is 1 anyway. Still I don't understand why $p=q=\frac 1 3$? Sorry for being that tedious.
Comment: Do you give me another hint for this direction please?
Comment: @DanisFischer I made a little edit, which might help. It might not, but I'm done at the moment.


Inequality of small two numbers with power

Question: Let $0<x,y<1$ are two real numbers and $n\in\mathbb N$. Is the following inequality true $$x^n-y^n\leq x^{n+1}-y^{n+1}?$$
I split into two cases:
Case1: when $y<x$.
Case2:when $x<y.$
But in both the cases, I can't conclude anything.
Comment: It's enough to assume that $x \geq y$, say.  Have you considered some values of $x$ and $y$ to test if it's true?  In what context did this question arise?  What tools are available for use here?
Comment: I estimate some integration and on this, I face this problem.
Answer: If its true for all $x,y$ with $0<x,y<1$, then it must also be true for all $x,y$ with $0 \leq x, y \leq 1$, since both $x^n-y^n$ and $x^{n+1}-y^{n+1}$ are continuous functions.
What happens if you let $y=0?$
Comment: foy y=0 and y=1 it's not true as $x<1$
Comment: Exactly, so if you prove the assertion that it also must hold for $x,y$ with $0 \leq x,y \leq 1$ then you are done.
Comment: Can you tell more?
Comment: I claim that if $f(x,y)$ and $g(x,y)$ are continuous functions and you a sequences $(x_n,y_n)$ which converges to $(x,y)$ and $f(x_n,y_n) \leq g(x_n, y_n)$ for all $n$ then you must have $f(x,y) \leq g(x,y)$. If not then $f(x,y)-g(x,y)> \epsilon >0$ for some $\epsilon$. You should be able to use that and the continuity of $f$ and $g$ (hence the fact that $f(x_n, y_n) $ converges to $f(x,y)$ and likewise for $g$) to get a contradiction.


Nginx ingress 504 gateway timeout on EKS with NLB connected to Nginx ingress

Question: We are using a NLB in AWS connected to our EKS cluster via a nginx ingress controller. Some of our requests get a random 504 gateway timeout.
We think we debugged the problem to our nginx ingress.
Based on some Stackoverflow recommendations we played around with Connection headers.
1) We set Connection "close" this had no effect
2) We set Connection "keep-alive" again no effect
We also noticed another behavior with our proxy_read_timeout when it was 60seconds our request from the browser would be fulfilled at 60.xx seconds. When we reduced it to 30 it became 30.xx, 20 became 20.xx.
We went to 1 but still get random 504 gateway timeouts and do not understand why proxy_read_timeout has this behavior in our environment.
We want to understand what is the effect of proxy_read_timeout and why do we get above behavior? Also is there a way to set Connection "" on our nginx ingress (we are not able to do this via nginx.ingress.kubernetes.io/connection-proxy-header: "")
Thanks in advance!
Comment: StackOverflow isn't a general tech support site. Maybe try [sf]?
Comment: Good job. Now you can delete this question.
Comment: Now I added in server fault as well - https://serverfault.com/questions/963116/nginx-ingress-504-timeout-eks-with-elb-connected-to-nginx-ingress
Answer: We think our issue was related to this:
https://docs.aws.amazon.com/elasticloadbalancing/latest/network/load-balancer-troubleshooting.html#loopback-timeout
We're using an internal nlb with our nginx ingress controller, with targets registered by instance ID. We found that the 504 timeouts and the X second waits were only occurring on applications that were sharing a node with one of our ingress controller replicas. We used a combination of nodeSelectors, labels, taints, and tolerations to force the ingress controllers onto their own node, and it appears to have eliminated the timeouts.
We also changed our externalTrafficPolicy setting to Local.


Finding an isometry that maps one circle to another.

Question: I have a problem goes as follows:
Consider the unit speed curve $$\boldsymbol{r}(s)=\left(\frac{4}{5}\cos(s),1-\sin(s),-\frac{3}{5}\cos(s)\right).$$
Find an isometry $f$ such that $f(\boldsymbol{r}(s))$ is the circle $$(\cos(s),\sin(s),0).$$

I am not sure how to approach this problem. I understand that I have to find a function (more specifically an isometry) that maps each ordinate to another, but I do not know how to find it. Nor do I know any methodical approach to help me.
Thank you to all in advanced for your help.
Comment: You might like to start by finding the normal to the plane that the circle lies in. Then recall that (orientation preserving) isometries of $\mathbb{R}^3$ are compositions of translations and rotations about the origin.
Answer: Your first parametrization can be written
\begin{align}
   \boldsymbol{r}(s) &= \cos(s)\,\langle 4/5,0,-3/5 \rangle +\sin(s)\,\langle 0,-1,0 \rangle +\langle 0,1,0 \rangle \\
 &= \cos(s)\,\boldsymbol{u} + \sin(s)\,\boldsymbol{v} +\boldsymbol{p}
\end{align}
Note that $\boldsymbol{u}$ and $\boldsymbol{v}$ are already orthogonal with unit length so
you simply need to shift back by $\boldsymbol{p}$ and then find a linear transformation mapping $\boldsymbol{u}$ and $\boldsymbol{v}$ to $\boldsymbol{i}$ and $\boldsymbol{j}$ respectively.
Comment: Wow, that was a very nice observation! Thank you. I will work on it from here.
Comment: @GustavoMontano Cool! I was hoping that would help.


What is a definition of "oscillator" that is suitable for all musical instruments?

Question: I understand from my music theory class that for reed and brass instruments, the "primary oscillator" is the modulation of an airstream. Comparatively, for wood winds, the "primary oscillator" is the air column.
I also understand that "oscillatIon" is a repeating waveform with a f0 and peak amplitude.
Fine, but what is a "primary oscillator" that is common to reed, brass, and woodwinds in the most generic sense of the word?
Thanks!
Comment: An oscillator is not a waveform. An oscillator generates a waveform.
Comment: I think your terminology is a bit confused. In any musical instrument that produces a definite pitch, you have (1) something that oscillates, and (2) something that supplies energy to make it oscillate. For reed, brass, and wind instrument, the main thing that oscillates is the air inside the instrument. (The reed itself also oscillates.) The thing that supplies the energy is either the player's breath, or a mechanical system in instruments like bagpipes and pipe organs.
Comment: Thanks! This is probably a naive question since I'm no physicist, but why is it necessary for instruments that produce a definite pitch to have an oscillator? Is there not some other mechanism through which pitch can be produced?
Answer: The common factor in all wind instruments is that sound is produced from a vibrating column of air, set into oscillation by a player's breath.  But the air column isn't the "oscillator", it's the thing that is made to oscillate.  So I think your theory class has got it a bit wrong!  There are three ways in which a player may be the "oscillator".  In a brass instrument he "buzzes" his lips into the mouthpiece.  In a reed instrument the reed(s) take over the function of the lips.  In a flute it's rather different, oscillation is the result of an airstream splitting in two when it hits an edge.   So three different ways of being an oscillator, all with the result of getting an air column vibrating, ready to be shaped by the rest of the instrument.
The flute family produce a waveform reasonably like the classic sine-wave picture, with an easily discernable frequency and amplitude.  Brass and woodwind have much more complex waveforms. And a large part of the sound's characteristic, for all of them, is defined by the attack, where the waveform looks a LOT less tidy.  As so often in musical topics I think you'll be better off learning how individual instruments do work, rather than searching for a Unified Theory.  Maybe your teachers are simply looking for you to say "wind instruments work by the player setting an enclosed air column into vibration".


What is the expected value of the number of randomly chosen real numbers between $0$ and $1$ needed to reach a sum of $1$?

Question: My friend told me that the answer to this question was $e$, which intrigued me, but he refused to tell me why.
My initial intuition was completely wrong. I thought that since the expected value of any one of these numbers (uniformly distributed on $[0,1]$) was $1/2$, the expected number of them you'd need to get a sum of $1$ would be two. I realized this was rather stupid.

This is my attempt at calculating the expected value term-by term. Let $N$ be the number of numbers between $0$ and $1$ necessary for their sum to exceed $1$, and let $P(N=k)$ be the probability that it takes $k$ such numbers. Then the expected value of $N$ is $$E(N) = P(N=2) \cdot 2 + P(N=3) \cdot 3 + P(N=4) \cdot 4 + \cdots$$
I then calculated the first term:
$$P(N=2) \cdot 2 = \frac{1}{2} \cdot 2 = 1$$
Unfortunately, the second and higher terms aren't as easy ...
Comment: http://math.stackexchange.com/questions/111314/choose-a-random-number-between-0-and-1-and-record-its-value-and-keep-doing-it-u
Comment: First of all, it can't be $2$, because the probability that you reach $1$ in one choice is $0$, so $2$ is minimum value for the number, so the average value of the number would have to be more than $2$ unless it was almost always $2$...
Answer: It might be easier to write this as $P(N>0)+P(N>1) + P(N>2)\dots$.
Then you need to see that $P(N>k)=\frac{1}{k!}$, which is essentially a geometric argument - the ($k$-dimensional) volume of $\{(x_1,\dots,x_k)\in[0,1]^k:x_1+\dots+x_k<1\}$ is $\frac{1}{k!}$.
For the first line:
$$\begin{align}P(N>0) &= P(N=1)+&P(N=2)+&P(N=3)+\dots\\
P(N>1)&=&P(N=2)+&P(N=3)+\dots\\
P(N>2)&=&&P(N=3)+\dots
\end{align}$$
Add them up and you get $\sum_{k=0}^\infty P(N>k) = \sum_{k=1}^\infty kP(N=k) = E(N)$.
Comment: A justification of [the volume statement](https://en.wikipedia.org/wiki/Simplex#Volume), for the curious
Comment: By $N$, do you mean they same $N$ as I do in the question? It doesn't seem to make sense with the greater-thans...
Comment: Same $N$ as your question. You'll have to be more specific - I've definitely left out steps... @ZubinMukerjee
Comment: I guess I don't see how you got the very first line as the expected value.
Comment: I've added a note on the first line, above.


How do I derive this coefficient formula in a regression with non-linear conditional expectation function

Question: I have the following model:
$$ y_{i} = \alpha + \beta{d_{i}} + X^{'}_{i}\gamma + \epsilon_{i} $$
Where $d_{i}$ is a dummy variable and $X^{'}_{i}$ is a vector of control variables. I want to estimate the the causal effect of $d_{i}$. Suppose that the regression is saturated in $X_{i}$ so that $E(d_{i}|X_{i})$ is linear. But suppose also that the model above is not actually the conditional expectation function (which is non-linear).
I want to find how $\beta$ is related to $E(y_{1,i}-y_{0,i})$ (the causal effect on $y_{i}$ of switching the dummy $d_{i}$ on and off).
Now since the regression is saturated in $X_{i}$ I can write $\beta$ as $\frac{Cov(y_{i},\hat{d_{i}})}{Var(\hat{d_{i}})}$ where $d_{i}$ is the residual from regressing $d_{i}$ on the covariates $X^{'}_{i}$. And I know $\hat{d_{i}} = d_{i} - E(d_{i}|X_{i})$ so therefore:
$$ \beta = \frac{E(y_{i}(d_{i}-E(d_{i}|X_{i})))}{E((d_{i}-E(d_{i}|X_{i}))^{2})} $$
So here's my question: in the notes I am working from, the next step is written as:
$$ \beta = \frac{E(E(y_{i}|d_{i},X_{i})(d_{i}-E(d_{i}|X_{i})))}{E((d_{i}-E(d_{i}|X_{i}))^{2})} $$
I am confused because if this step has been reached using the Law of Iterated Expectations, then why hasn't the expectation sign also been applied to the second term inside the bracket (I am assuming they can be separated due to Conditional Independence)? And wouldn't that term equal zero?
Answer: The regression CEF theorem states that the function $X_{i}^{'}\beta$ provides the best (minimum-mean-square-error) linear approximation to $E[Y_{i}|X_{i}]$. That is:
$$ \beta = arg \min_{b}E[[E[Y_{i}|X_{i}] - X_{i}^{'}\beta]^{2}]   $$
In other words, regression is the best linear approximation to the CEF (even if the CEF is non-linear). A regression line will fit a non-linear CEF as if we were trying to estimate $E[Y_{i}|X_{i}]$ instead of $Y_{i}$. But this is precisely what is going on. An implication of the regression CEF theorem is that we can obtain regression coefficients by using $E[Y_{i}|X_{i}]$ as a dependent variable instead of $Y_{i}$.
Therefore - in our question above - we can replace the dependent variable $y_{i}$ with $E(y_{i}|d_{i},X_{i})$.
Source: Mostly Harmless Econometrics
Comment: We would normally look for answers on this site to be more extensive that one line. Would you mind editing to expand into a more complete answer, perhaps one designed to help a future user that's more confused than you. Could you also expand CEF to (what I presume is intended to be) *conditional expectation function* on first use, please?
Comment: Is this an answer to your own question? Or is it additional information that might help someone else answer? If the former, please elaborate. If the latter, it should be a comment.
Comment: Yes - it's the answer. I will expand it as you have suggested .


Necessary condition for selecting a New coordinate system (as tangent and normal vectors)

Question: I wanted to understand intuitvily, how it is possible to make tangent and normal vectors as a coordinate system for a planar motion because my concern is that what I have read about coordinate system is that once it is fixed along a particular direction after that those directions will remain same regardless of  curve for example Cartesian coordinate
but
My concern with tangent and normal as coordinate system is that it keep changing for every new curve because tangent and normal for every other curve will be somewhat different which is quite unintuitive to what I have read earlier about coordinate system .
Can anyone clear my doubt ,what is basic criteria for selecting a coordinate system ?
Answer: <code>my concern is that what I have read about coordinate system is that once it is
fixed along a particular direction after that those directions will remain
same regardless of curve for example Cartesian coordinate
</code>
That paragraph is where things go wrong, or at least where your confusion is revealed.
Let's think about a curve $\gamma$ in the plane. We have, for each $t$, the point
$$
\gamma(t) = (x(t), y(t))
$$
For instance, if $x(t) =  t$ and $y(t) = t^2$, the curve described by $\gamma$ is a parabola. It's made up of many different points, one for each possible $t$-value. The fact that $\gamma(0) = (0,0)$ doesn't prevent $\gamma(2)$ from being $(2, 4)$, right? If we think of $t$ as "time", then you could say that we have a "continuously changing point" moving along the curve, but in reality, we have many many points, each attached to a particular time-value. $\gamma(0) = (0,0)$, and it never changes, but $\gamma(t)$, as $t$ varies, does change.
The same thing is true for coordinate systems: the cartesian coordinate system is fixed once and for all. For our curve, the tangent vector at time $0$ is $[1, 0]$, and the normal is $[0, 1]$. That tangent-normal pair is fixed for all time. But at time $t = 1$, the tangent is $[1, 2]$ and the normal is something like $[-2c, 1c]$ for some value of $c$ that I'm not going to compute. We can use either of those to measure things like "the position of $\gamma(2)$".
The clever idea of the "moving frame" is that in measuring things associated with $\gamma(t)$, for some particular $t$, it may be useful not to use $T(0), N(0)$, or $T(1), N(1)$, but instead to express things in the coordinate system $T(t), N(t)$.
We do this all the time in real life. Imaging you're driving a car in the US; your friend Robin is in the passenger seat to the right of you. Your child is in a car-seat right behind you. You start out driving North, so Robin is to the East of you, but soon the road curves and you're driving East, and Robin is to the south of you. But in your mind, we ignore the global N/S/E/W coordinates, and you say "the whole time I was driving, Robin was to my right, and my child was behind me". In this case, you're using, at each moment, a coordinate system based on the car's orientate at that moment, i.e., a constantly moving frame-of-reference.


EOF occurred in violation of protocol for ansible

Question: I am installing ansible on a newly formatted machine(Windows 10). I have enabled WSL and I installed ansible using the following set of commands.

sudo apt-get-y install python-pip python-dev libffi-dev libssl-dev
pip install ansible --user
echo `PATH=$HOME/.local/bin:$PATH` >> ~/.bashrc
pip install "pywinrm>=0.2.2"

I wrote a simple win_ping module and it is returning this error with the following verbose.

fatal: [192.168.6.1]: UNREACHABLE! => {
     "changed": false,
     "msg": "ssl: HTTPSConnectionPool(host='192.168.6.1', port=5986): Max retries exceeded with url: /wsman (Caused by SSLError(SSLEOFError(8, u'EOF occurred in violation of protocol (_ssl.c:590)'),))",
     "unreachable": true
  }

  TASK [Gathering Facts] **********************************************************************************************
  task path: /mnt/c/ansible/test.yml:1
  Using module file /home/xxxxxx/.local/lib/python2.7/site-packages/ansible/modules/windows/setup.ps1
  <192.168.6.1> ESTABLISH WINRM CONNECTION FOR USER: xxxx on PORT 5986 TO 192.168.6.1
  checking if winrm_host 192.168.6.1 is an IPv6 address
  <192.168.6.1> WINRM CONNECT: transport=ssl endpoint=https://192.168.6.1:5986/wsman
  <192.168.6.1> WINRM CONNECTION ERROR: HTTPSConnectionPool(host='192.168.6.1', port=5986): Max retries exceeded with url: /wsman (Caused by SSLError(SSLEOFError(8, u'EOF occurred in violation of protocol (_ssl.c:590)'),))
  Traceback (most recent call last):
    File "/home/chanondw/.local/lib/python2.7/site-packages/ansible/plugins/connection/winrm.py", line 345, in _winrm_connect
      self.shell_id = protocol.open_shell(codepage=65001)  # UTF-8
    File "/home/chanondw/.local/lib/python2.7/site-packages/winrm/protocol.py", line 157, in open_shell
      res = self.send_message(xmltodict.unparse(req))
    File "/home/chanondw/.local/lib/python2.7/site-packages/winrm/protocol.py", line 234, in send_message
      resp = self.transport.send_message(message)
    File "/home/chanondw/.local/lib/python2.7/site-packages/winrm/transport.py", line 256, in send_message
      response = self._send_message_request(prepared_request, message)
    File "/home/chanondw/.local/lib/python2.7/site-packages/winrm/transport.py", line 261, in _send_message_request
      response = self.session.send(prepared_request, timeout=self.read_timeout_sec)
    File "/home/chanondw/.local/lib/python2.7/site-packages/requests/sessions.py", line 618, in send
      r = adapter.send(request, **kwargs)
    File "/home/chanondw/.local/lib/python2.7/site-packages/requests/adapters.py", line 506, in send
      raise SSLError(e, request=request)
  SSLError: HTTPSConnectionPool(host='192.168.6.1', port=5986): Max retries exceeded with url: /wsman (Caused by SSLError(SSLEOFError(8, u'EOF occurred in violation of protocol (_ssl.c:590)'),))

I am also using ansible for the same user credentials and using the same playbook. My old machine works fine, but my new machine refuses to cooperate. I am not sure what I'm doing wrong here.
Answer: Maybe there is no certificate on the host?
Try running the host preparation script with the -ForceNewSSLCert option.
ConfigureRemotingForAnsible.ps1 -ForceNewSSLCert


how to search for html tag in solr?

Question: i crawled site with apache nutch and indexed it to Apache Solr.i don't know how search strings between  and  html tags in a site with solr?
Thanks
Answer: The easiest way is to extract data from the HTML and index extracted data. You can use the HTMLStripCharFilterFactory to strip HTML from input stream.
<code><analyzer>
  <charFilter class="solr.HTMLStripCharFilterFactory"/>
  <tokenizer class="solr.StandardTokenizerFactory"/>
</analyzer>
</code>

how to search for html tag in solr?


Repeat line x to y every few second in php

Question: how i can repeat  code from line x to y in php By Ajax.
for example:
mycode:
$info_2=('
');
echo $chat_2;
$info_query=mysql_query("SELECT * FROM info WHERE fuid='$uid' or luid='$uid' ");
$info_num=mysql_num_rows($info_query);
for($i=0;$i<$info_num;$i++)
{
$info_fetch=mysql_fetch_array($info_query);
echo($info_fetch['username'].':'.$info_fetch['text'].'
');
}

$info_3=('');
echo $info_3;
now i need repeat this code every x second.
Comment: Where do you `echo` the results to?
Comment: jquery ajax or simple xml httprequest..?
Comment: Similarly if I want to be repeated every few seconds, what should I do?
Answer: One way you can resolve this, is by executing setInterval with an Ajax call.
Somethine like :

However, I don't like settings intervals because it does not take into account the server time.
So this is how I would do it (pseudo code):

Now for "my_content.php".
 ....

This way this piece of code is "self contained" and only requires it to be wrapped with "my_container" element.


How to choose what distribution to use?

Question: I'm trying to get around a specific problem.
I have a dataset with observations of a specific event, for Northern and Southern states, each with an associated timestamp.
How could I calculate a probability to whether the Northern states have a higher probability of that event occurring during night time than the Southern states?
I can plot the data so that it follows a normal distribution (if I set a starting hour to 4am), but I feel I shouldn't be doing this.
Can I use a Binomial distribution? (the event either occurs at night, or not).
Answer: Aside from the distribution, you could create a variable called $\texttt{nightevent}$, which is set to 1 if an event happened at night and zero otherwise.  Next, create a variable $\texttt{southern}$, which is set to 1 if the record is for a southern state, or zero if from a northern state.  Then regress $\texttt{nightevent}$ as the dependent variable on $\texttt{southern}$, which is the predictor variable.  If the regression coefficient is negative for $\texttt{southern}$ it means southern states are more associated with daily events and northern are more associated with night events.  Otherwise a positive coefficient would imply that southern states are more associated with night events and northern associated with day events.  If the coefficient for $\texttt{southern}$ is significant, it means the test is significant.
Regarding the distribution, yes, binomial is appropriate, so you could use logistic regression to meet this assumption of a binomially-distributed outcome, i.e. $y=0$ or $y=1$.
The above is not appropriate for evaluating "what time" the events occurred, since that would entail use of longitudinal (time-dependent) regression.
Comment: Yes, you can use a test of two proportions, p1, and p2, where p1 is proportion of night events in northern states, and p2 is the proportion of night events in southern states.  The p-value (if less than 0.05) from the test will reveal if north vs south night events are significantly different.  Which ever set of states has the largest proportion (i.e. p1 vs p2) will be the one with significantly more night events.
Comment: Thank you for your answer. But instead of a regression, I am doing simple hypothesis testing. How could the p-value be calculated in this case?

How to choose what distribution to use?


Show that series is convergent and less than 1

Question: Show that the series
$$ \sum_{k=1}^{\infty} \dfrac{2}{(k+1)(2k+1)} $$
is convergent and converges to a number less than $1$.
I tried the quotient test for series, but it didn't work. I don't know what else to do. Any hint would be very appreciated.
Thanks!
Answer: Note that $\frac{2}{(k+1)(2k+1)}<\frac{1}{k(k+1)} = \frac{1}{k}-\frac {1}{k+1}$. But if we sum this from $1$ to $\infty$, all terms telescope except $1$ so the final sum is $1$. So we conclude that the original sum has a convergent value less than $1$.
Answer: $$\dfrac S4=-1+\dfrac12\sum_{r=0}\left(\dfrac1{2r+1}-\dfrac1{2r+2}\right)$$
$$=\ln(1+1)-\dfrac12$$
Answer: Using comparsong test with $a_k=1/k^2$ you get that $$\lim_{k\to\infty} \frac{\frac{2}{(k+1)(2k+1)}}{1/k^2} = \lim_{k\to\infty}\frac{2k^2}{2k^2+3k+1}=1.$$ So both series are convergent.
Now, taking into account that $2k+1\ge k+1$ for all $k\ge 1$, you have
$$\sum_{k=1}^\infty\frac{2}{(k+1)(2k+1)} \le \sum_{k=1}^\infty\frac{1}{(k+1)^2} =\sum_{k=1}^\infty\frac{1}{k^2}-1=\frac{\pi^2}{6}-1<1$$


Console.log shows array but can't be returned

Question: I'm new to working with promises (I'm using 'co' in node) so I'm not entirely sure what's failing with this code:
function* excelToJSON(excelFileNames) {
  var jsonData = [];

  for (let index = 0; index < excelFileNames.length; index++) {
    parseXlsx(excelFilesNames[index], function (err, data) {

      jsonData.push(data);
      console.log(jsonData); //***Shows data correctly
    });
  }

  console.log(jsonData); //***Empty array
  return yield jsonData;
}

It reads the file, converts it and, at least within the loop, it shows everything correctly, but once we get out of the loop the data seems to disappear. I've also tried to return one of the values from within the loop but that doesn't work either.
EDIT:
parseXlsx is from the 'excel' module here: https://github.com/trevordixon/excel.js
I'm not entirely sure if it's async or sync, to be honest. This seems to be its code, and I know 'extractFiles' returns a promise but since it then goes through 'parseXlsx' I'm not sure what happens afterwards:
function parseXlsx(path, sheet, cb) {
  if (typeof cb === 'undefined') {
    cb = sheet;
    sheet = '1';
  }
  extractFiles(path, sheet).then(function(files) {
    cb(null, extractData(files));
  },
  function(err) {
    cb(err);
  });
};

EDIT2:
What I used to solve it is a combination of several answers, thanks to all of you.

function* excelToJSON(excelFileNames) {
  return new Promise(function(resolve, reject) {
    var jsonData = [];

    if (excelFilesNames === null || excelFilesNames.length === 0) {
      reject();
    }

    for (let index = 0; index < excelFilesNames.length; index++) {
      parseXlsx(excelFilesNames[index], function(err, data) {
        if (err) {
          throw err;
        }

        jsonData.push(data);

        if (jsonData.length === excelFilesNames.length) {
          resolve(jsonData);
        }
      });
    }
  });
}
Comment: Is `parseXlsx` an async function ?
Comment: Nice, but what is the purpose of making a generator function here? Does this behave as an async function if a generator returns a promise?
Comment: It seems to be so. Check 'co':
https://www.npmjs.com/package/co
Answer: Use just a counter & When to return, try something like this.
function* excelToJSON(excelFileNames) {
var jsonData = [];
var count=0;
  for (let index = 0; index < excelFileNames.length; index++) {
    parseXlsx(excelFilesNames[index], function (err, data) {

      jsonData.push(data);
      console.log(jsonData); //***Shows data correctly
          if(count==excelFileNames.length){
                console.log(jsonData);
                return yield jsonData;
           }
        count++
    });
  }
}

Answer: Reason:
parseXlsx is an Asynchronous call, so you won't get data back immediately.

How to fix:
do things in callback.
function* excelToJSON(excelFileNames, callback) {
  var jsonData = [];

  for (let index = 0; index < excelFileNames.length; index++) {
    parseXlsx(excelFilesNames[index], function (err, data) {

      jsonData.push(data);
      console.log(jsonData); //***Shows data correctly

      callback(jsonData);    // do what you want with the jsonData here.
    });
  }

  // console.log(jsonData); //***Empty array
  // return yield jsonData;
}

Answer: So what's happening here is that your code just runs through that for block, invoking parseXlsx a few times, but never actually waits for it to finish.
So this is why your empty array log comes first, and then there come the logs with the 'correct data'. Look up javascript event loop to get a better understanding of how asynchronous functions work.
What you essentially need is either a promise that you resolve when you're done, or get a callback function that you'll call when you're done.
And you'll know when you're done when your jsonData.push(data); has been called as many times as long as your excelFileNames array is.
For example:
function excelToJSON(excelFileNames) {
  var deferred = Promise.defer();
  var jsonData = [];

  for (let index = 0; index < excelFileNames.length; index++) {
    parseXlsx(excelFilesNames[index], function (err, data) {

      jsonData.push(data);
      console.log(jsonData); //***Shows data correctly
      if (jsonData.length === excelFileNames.length) {
        deferred.resolve(jsonData);
      }
    });
  }

  return deferred.promise;
}

// And use it as a promise:

var exelToJsonPromise = excelToJSON(["apples.xlsx", "pears.xlsx]);

exelToJsonPromise.then(function(jsonData){
  console.log(jsonData); // Now this will have everything in it.
});

Answer: Node.js is a asynchronous framework. What is happening in your case is that the console.log(jsonData) outside the parseXlsx is getting called before the one inside. You can try the async waterfall method like this.
var pushData = function (err, data) {
    jsonData.push(data);
    console.log(jsonData);
};

function* excelToJSON(excelFileNames) {
    var jsonData = [];
    async.waterfall([
        function(){
            for (let index = 0; index < excelFileNames.length; index++) {
                parseXlsx(excelFilesNames[index], pushData);
            }
        }
    ], function() {
           console.log(jsonData);
           return yield jsonData;
    });
}

You can read more about it here.
PS. It is also not a good practice to define a function inside of a loop.


graph axis calibration in c++

Question: I am plotting some float data in a 2D graph and I need to calibrate the axis of graph into small units that looks neat. Obviously this unit varies based on magnitude of the data. I am trying to find out a good way to divide the axis into nice looking number. for example if my data runs from
-1.3345 to +5.882 may be divide in units of 1.0 or 0.5. if my data us from -100 to 800 divide the axes into units of 100 or 50. (I hope that makes sense) right now I am dividing the range (largest value - lowest value) by some fixed integer and getting the units but that gives me  ugly looking number with long trailing digits. Is there any smart way of doing this?
Comment: Co-incidentally, I just read this tweet: https://twitter.com/headinthebox/status/303588840180944896
Comment: Have a look at how [gnuplot](http://www.gnuplot.info) does it. [Asymptote](http://asymptote.sourceforge.net) also does plots, that might be easier to understand.
Answer: One way to calculate a good step would be to find the value of the most significant digit of the range's length (i.e. the diff = maxVlaue - minValue), and use that as your step. To calculate the value of the most significant digit use this simple formula:
pow(10, floor(log10(diff)))

This takes a decimal logarithm of the difference, discards the fractional part, if any, and raises ten to the power of that logarithm. For a difference of 7.2165, the calculation would return 1; for 721.65, it would return 100, and so on.
One shortcoming of this calculation is that the grid step for the diff of 9.99 and the diff of 1.001 would be the same. One way to address this would be to calculate the number of grid lines you'd get for the step, and decrease the step ten times if the number of lines is insufficient (say, less than three).
Comment: I tried the method you suggested. I ran into the problem you pointed in. The suggestion was very clear and easy to follow thanks for that..  Guess I should try to create my own function for the to overcome the problem thanks
Answer: ACM Algorithm 463 provides three simple functions to produce good axis scales with outputs xminp, xmaxp and dist for the minimum and maximum values on the scale and the distance between tick marks on the scale, given a request for n intervals that include the data points xmin and xmax:

Scale1() gives a linear scale with approximately n intervals and dist being an integer power of 10 times 1, 2 or 5.
Scale2() gives a linear scale with exactly n intervals (the gap between xminp and xmaxp tends to be larger than the gap produced by Scale1()).
Scale3() gives a logarithmic scale.

The code is in Fortran but it is very straightforward to interpret and convert into other languages. There are more complicated functions that give prettier scales (e.g. the ones in gnuplot), but Scale1 would likely do the job for you with minimal code.
(EDIT)
I found the text of the original 1973 paper online here, which provides more explanation than the code linked to above.
Comment: The article was good however I do not know fortan. Guess I have to find someone who can translate. Thanks.
Comment: @da_elysian_fields. The free translator [f2c](www.netlib.org/f2c/) is excellent and can translate Fortran of any vintage into C or C++.

graph axis calibration in c++


Assign web.config settings based on URL

Question: I have an asp.net/c# website that runs on a dev server and a prod server. We keep the code identical between both servers. Is it possible, based on the URL to assign different settings in the web.config (e.g. for error pages etc)
Ideally I want an IF statement in the web.config e.g.
<code>if (url.contains "http://dev") {
            web.config += @'<customErrors mode="Off" />'
} else {
            web.config += @'<customErrors mode="On" defaultRedirect="~/web/error.aspx" />'
}
</code>
Answer: Application configuration files have no direct templating nor scripting support so the approach outlined in the question is not possible. Building configuration files per environment is better placed as part of a build process.
If you are using Visual Studio 2012+ (or 2010 with the Visual Studio Web Publish Update) then you can use web.config transformations to manage the difference configurations across your environments.
This can be made to work for Web Site projects as per the question using some creative thinking. E.g. http://andrewtwest.com/2010/02/25/using-web-config-transformations-in-web-site-projects/. Although this involves creating a separate project for configuration management, it is a considerably less complex approach to implementing a full build process outside of the IDE.
Using this approach, you have a master web.config which contains your base config that is common across environments. In addition, you have a web.config transform file per build configuration that applies transformations specific to that environment. In your case, this would be to turn off customErrors in dev.
More information available here: http://msdn.microsoft.com/en-us/library/vstudio/dd465318(v=vs.100).aspx
But the basic idea is make sure you have all the build configurations you need, then right click on your web.config and select add transforms. These are then applied when the site is deployed using the respective build configuration.
Comment: As this is a website project and not a web application project I don't have the same build functionality.
Comment: There are workarounds to using it with a website project. Ive used this one before: http://andrewtwest.com/2010/02/25/using-web-config-transformations-in-web-site-projects/

Assign web.config settings based on URL


Integration using "method of judicious guessing"

Question: My calculus-book gives an example of integration using the method of judicious guessing. But I do not intuit the method very well.
QUESTION: How does the derivative of $f_{mn}(x)$ "suggest that we try" $I=Px^4\left(\log {x}\right)^2 +Qx^4\log{x}+Rx^4+C$? Where does this trial formula come from?
The example goes as follows:

Find the derivative of $f_{mn}(x)=x^m\left(\log {x}\right)^n$ and use the result to suggest a trial formula for $I=\int x^3\left(\log {x}\right)^2dx$. Thus evaluate this integral.
Solution: We have
  $$f'_{mn}(x)=mx^{m-1}\left(\log {x}\right)^n+nx^{m-1}\left(\log {x}\right)^{n-1}.$$
  This suggests that we try
  $$I=Px^4\left(\log {x}\right)^2+Qx^4\log{x}+Rx^4+C$$
  for constants $P$, $Q$, $R$ and $C$. Differentiating we get
  $$\frac{dI}{dx} = 4Px^3\left(\log {x}\right)^2 + 2Px^3\log{x} + 4Qx^3\log{x} + Qx^3 + 4Rx^3 = x^3\left(\log {x}\right)^2,$$
  solving for $P$, $Q$ and $R$ we arrive at the right answer:
  $$\int x^3\left(\log {x}\right)^2dx=\frac{1}{4}x^4\left(\log {x}\right)^2-\frac{1}{8}x^4\log{x}+\frac{1}{32}x^4+C.$$

Please note my level of mathematics is still "in development": I am learning without a teacher.

BACKGROUND: In my efforts I did notice the following, which also results in the right answer:
$$\frac{d}{dx}x^m\left(\log {x}\right)^n=mx^{m-1}\left(\log {x}\right)^n+nx^{m-1}\left(\log {x}\right)^{n-1}.$$
Integrating both sides we get:
$$x^m\left(\log {x}\right)^n=m\int x^{m-1}\left(\log {x}\right)^n dx+n\int x^{m-1}\left(\log {x}\right)^{n-1}dx.$$
Now we can define $g_{mn}(x)$ as follows:
$$g_{mn}\left(x\right)=\int x^{m-1}\left(\log {x}\right)^n dx=\frac{1}{m}x^m\left(\log {x}\right)^n-\frac{n}{m}\int x^{m-1}\left(\log {x}\right)^{n-1}dx.$$
Taking $m=4$ and $n=2$ we get:
\begin{align}
I&=\int x^3\left(\log {x}\right)^2dx=g_{42}(x)=\frac{1}{4}x^4\left(\log {x}\right)^2-\frac{1}{2}\int x^3\log{x}\,dx\\
&=\frac{1}{4}x^4\left(\log {x}\right)^2-\frac{1}{2}g_{41}(x)=\frac{1}{4}x^4\left(\log {x}\right)^2-\frac{1}{8}x^4\log{x}+\frac{1}{32}x^4+C.
\end{align}
Answer: Of course one can say: The expertise of the author led him to this "Ansatz". Note that you cannot make a mistake by trying it. If the true solution is not in the bag of proposed functions the computation will reveal this.
Your own efforts resulted in the recursion formula
$$\int x^m\log^n x\>dx=\langle{1\over m+1}x^{m+1}\log^n x\rangle-{n\over m+1}\int x^m \log^{n-1} x\>dx\qquad(n\geq1)\ .$$
Here $m$ is fixed, and $n$ is the recursion variable, and the $\langle\ldots\rangle$ notation means "up to an additive constant". It is then pretty obvious, and can be verified by a simple induction proof, that the complete solution will be of the form
$$\int x^m\log^n x\>dx=\bigl\langle x^{m+1}\sum_{k=0}^n c_k\log^{n-k}x\bigr\rangle\tag{1}$$
with certain rational coefficients $c_k$, e.g., $c_0={1\over m+1}$. Knowing this it is perhaps simpler to start right away with the "Ansatz" $(1)$ with indetermined coefficients $c_k$ than going through an $n$ steps of the recursion with all its integral signs.
But there is also an " abstract nonsense" proof that the proposed approach works: The result shows that it is not sufficient to consider "monomials" of the form $x^m\log^n x$ and integrate them in a single step. For a general theory we have to deal with the full space of functions $f$ of the form
$$f(x)=x^m p(\log x)\qquad(x>0)\ ,\tag{2}$$
where $p(t)$ is a polynomial in an "indeterminate" $t$.
Denote the vector space of all polynomials $p(t)$ of degree $\leq n$ by $P_n$, and denote the vector space of all functions $(2)$ with $p\in P_n$ by $V_{m,n}$. For an $f\in V_{m,n}$ one computes
$$f'(x)=x^{m-1}\bigl(m p(\log x)+p'(\log x)\bigr)\ ,\tag{3}$$
which implies that $f'\in V_{m-1,\, n}\>$.
This shows that the derivation $D:\>f\mapsto f'$ maps $V_{m,n}$ to $V_{m-1,\, n}$. The problem at hand is about the converse direction: We are given a $g\in V_{m-1,\,n}$ and have to show that  $g$ is the $G'$ of some $G\in V_{m,n}$, in other words: that $$D:\>V_{m,n}\to V_{m-1,\, n}\ ,\qquad f\mapsto f'\tag{4}$$ is surjective.
In $(3)$ we see a linear map $\psi:\>P_n\to P_n$ at work, namely
$$\psi:\quad P_n\to P_n,\qquad p\mapsto mp +p'\ .$$
When $m>0$ this map is injective, because $\psi(p)=0$, i.e., $p'=-mp$, implies $p=0$, or $p$ would be an exponential function. As $P_n$ is finite dimensional we can conclude that $\psi$ is also surjective, hence $(4)$ is surjective as well. This is saying that any $g\in V_{m-1, \,n}$ is the derivative of some $G\in V_{m,\,n}$. In particular this is the case for the function $g(x):=x^{m-1}\,(\log x)^n$.
Comment: I don't doubt this answer is correct; it's just that my level of math is not sufficient yet to understand it. For example, I don't no what a vector space is, nor do I know what a linear map is, nor injective or surjective... further explanation or simplification would be much appreciated!
Comment: That's o.k. But you cannot expect a conceptual explanation of this "method" without bringing the concepts into the game.
Comment: Fair enough... it's just that my calculus book (calculus a complete course) hasn't covered these concepts while still expecting that the trial formula is kind of obvious. Therefore I was wondering what "obvious" pattern I was missing here...
Answer: More or less, when the author of the textbook makes their guess of the form of the integral, they are eliding exactly the work that you did to calculate the integral.

Essentially, there is an inductive argument at work here. So, the critical observation that the textbook asks you to make is the following one:
 $$f_{m+1,n}'=m\cdot f_{m,n}+n\cdot f_{m,n-1}.$$
The idea is then that if you want to integrate $f_{m,n}$, what you need to do is the following:

Calculate an anti-derivative for $f_{m,n-1}$.
Subtract $n$ times that anti-derivative from $f_{m+1,n}$ and divide by $m$.

The idea then is that, if you repeat this argument until you get to calculating an anti-derivative for $f_{m,0}$ - which is just some multiple of $f_{m+1,0}$ - you see that the answer should be some weighted sum of the following functions $$f_{m+1,n},\,f_{m+1,n-1},\,f_{m+1,n-2},\ldots,f_{m+1,2},\,f_{m+1,1},\,f_{m+1,0}.$$
This is just what happens when you "unroll" the process to figure out the antiderivative of $f_{m,n}$ by using the antiderivative for $f_{m,n-1}$, which can be determined by knowing the antiderivative for $f_{m,n-2}$ and so on.
Basically, the textbook runs this argument by figuring out what the terms will be, ignoring the factors of $n$ and $m$ that appear, then figuring out what the coefficients have to be at the end. Your method does the same idea, but you explicitly keep track of the constants at each step.

The clever step here is in seeing that $f_{m+1,n}'=m\cdot f_{m,n} + n\cdot f_{m,n-1}$. Once you have that, you see that you can relate your desired anti-derivative to a simpler one, and then proceed. I would say that the "judicious guess" is this step. As you discovered, there are multiple ways to proceed once you get to this step - and whatever you do, it's a somewhat straightforward calculation from that point on.
Answer: Let me try to give a little insight into my thinking process when I see problems like that.

Objective: Compute explicitly integral $\,I=\int x^3\log^2 x\,dx$ by "trial and error".

Interpretation: Authors want you to guess "general form" of solution (meaning they want you to plug in some expression that you think might be solution, but without necessarily knowing exact coefficients, which can be adjusted later).

Hint: find the derivative of $\,f_{mn}\left(x\right)=x^m\log^nx$ and guess general solution of integral $\,I=\int x^3\log^2 x\,dx$ from there.

Interpretation: We need to guess a function whose derivative is $\, x^3\log^2 x$.
Since the result is product of powers of $x$ and $\log x$, it is reasonable to assume that the integrand has similar form, with maybe more terms and higher powers, but it should still have product of powers of $x$ and $\log x$ in order for its derivative to have required form.

Approach to Solution
Following the hint, we write down derivative of $\,f_{mn}\left(x\right)$ as $f'_{mn}(x)=mx^{m-1}\log^n{x}+nx^{m-1}\log^{n-1}{x}$.
Thus if we want the derivative of function to be $x^3\log^2 x$ we have
$ m-1 = 3$ as well as $n = 2$ or $n-1=2$.
Moreover, the derivative of $f_{mn}(x)$ has more than one term, thus if we seek for expression of whose derivative is $x^3\log^2 x$ we need to take linear combination of functions of the form $f_{mn}$ hoping that redundunt terms will cancel out.
More specifically, we see that in the integrand power of $\log x$ is smaller by $1$ than the power of $ x$, thus we conclude that we want to get rid of the term
$x^{m-1}\log^{n-1}{x}$ since it has the same powers.
From here on you can use trial and error to try to match and cancel redundant terms.
Answer: Note that$$
f_{m, n}'(x) = mx^{m - 1} (\ln x)^n + nx^{m - 1} (\ln x)^{n - 1} = P_{m, n}(x, \ln x),
$$
where $P_{m, n}(x, y) = mx^{m - 1} y^n + nx^{m - 1} y^{n - 1}$ is a polynomial of $x$ and $y$, and $\deg_x P_{m, n} = m - 1$, $\deg_y P_{m, n} = n$.
First, the primitive function of $f_{m_0, n_0}(x) = x^{m_0} (\ln x)^{n_0}$ can be guessed as a linear combination of some $f_{m, n}$'s plus a constant $C$. Next, note that $P_{m, n}$ is a monomial of degree $m - 1$ with respect to $x$, i.e. $P_{m, n}(x, y) = x^{m - 1} Q_{m, n}(y)$, thus the combination should be of $f_{m, n}$'s all with $m = m_0 + 1$. Now, since $\deg_y P_{m, n} = n$, then the $f_{m_0 + 1, n}$'s in the combination should satisfy $0 \leqslant n \leqslant n_0$. Therefore, the guessed form is$$
F_{m_0, n_0}(x) = \sum_{k = 0}^{n_0} a_k f_{m_0 + 1, n}(x) + C.
$$
Comment: @GambitSquared Some given positive integers. In your case, $(m_0,n_0)=(3,2)$.
Comment: What is the meaning of $m_0$ and $n_0$?


Reboot Docker container from inside

Question: I'm working with a Docker container with Debian 11 inside and a server.
I need to update this server and do other things on regular manne. I've written several scripts that can do it, but I encountered serious proble.
If I want to update the server and other packages I need to reboot the container.
I'm obviously able to do so from the computer Docker is installed on (in my case Docker Desktop running with WSL2 on Windows 10), I can reboot the container easily, but I need to automate it.
The simplest way will be to add the shutdown command to the scripts I've written. I was reading about it, but found nothing. Is there any way to reboot this container from the Debian inside it? If no, how can it be achieved and how complicated is it?
I was trying to invoke standard Linux commands to shutdown or reboot system on Debian inside container.
I expect a guide if it's possible and worth efforts.
Comment: _Reboot_ a container? A container doesn't have its own kernel. Yes, it has a Debian 11 userland, but your container does not have a Debian 11 kernel, and so you can't reboot that. Also, you wouldn't update the container. Instead, you update the  _image_, and create a new container.
Comment: Docker containers shouldn't be responsible for orchestrating themselves. If you need to reboot the host, just shut it down. Docker will restart the containers when it comes back up. More here: https://docs.docker.com/config/containers/start-containers-automatically/
Answer: The only way to trigger a restart of a container from within the container is to first set a restart policy on the container such as <code>--restart=on-failure</code> and then simply stop the container, i.e., let the main process terminate itself. The Docker engine would then restart the container.
This, however, is not the way Docker is intended to be used! Docker containers are not VMs and instead are meant to be ephemeral:

By "ephemeral", we mean that the container can be stopped and destroyed, then rebuilt and replaced with an absolute minimum set up and configuration.

This means, you shouldn't be updating the server within a running container but instead should update/rebuild the image and start a new container from it!
Comment: Ok, thanks for help I'm new to docker. I will update image instead.

Reboot Docker container from inside


How to see the result of macro processing of a .cpp file?

Question: I'm reading some c++ library code and there are lots of macros.
I'm wondering how can I substitute all the macros to final code of the whole .cpp file?
I'm using g++ and msvc, if it's compiler related, either should be fine.
Comment: `g++` and `clang++` have `-E`.
Comment: Most compilers have options to run code through the preprocessor and stop there. Look at the documentation for your compiler.
Comment: which compiler are you using?
Comment: For msvc it's `/E` IIRC, but `-E` should be fine as well.
Comment: Note: _"I'm wondering if there is a tool/method which can substitute all the macros to final code of the whole .cpp file?"_ Asking for tools or any other 3rd party resources is _off-topic_ here.
Comment: @πάνταῥεῖ if g++ is a tool, it happens that I ask about that tool.
Comment: @FelixFXu but it won't help you with the step of _"substitution"_. There's further processing needed of these sources generated.
Answer: thanks for the comments above, -E is the correct answer and I extend it to be a full one here.
The answer is:
g++ -E ./global/tagging.cpp -B /mnt/d/Test

-E is to preprocess the file as suggested in the comment above.
Usually a .cpp file will include .h files and -B is to set where to find the include files (otherwise it's very likely that g++ can't find .h files)
-o is optional to set the output file but when it's not used, the output will be in the terminal where I run g++.
When the cpp file includes lots of header files (recursively) the output is very long and is like: (search the file name of "tagging.cpp")
# 95 "./global/tagging.cpp"
 extern __attribute__((alias("_" "ccc"))) const char ....;

which means tagging.cpp file's line number 95 is preprocess to be the code following.
Comment: Not so sure how this answers your question? Did you mean to edit your question to add additional information about your concrete problem?


makov chains and property

Question: let $X_t$ for $t \in \{1,2,...,10\}$ be a sequence of independent tosses of a fair coin. We denote heads with 1 and tails with 0. Define the random variable $S_n=\sum_{t=1}^n X_t$ for $n \in \{1,2,...,10\}$. let event $A=S_{10}=8$.
What is the distribution of $X_3|A$ ?
(that is the distribution of $X_3$ conditionally on the event $A$)
(b) What is the distribution of $X_4|A$ ?
(c) Conditionally on the event $A$; are $X_3$ and $X_4$ still independent?
(d) What is the distribution of $S_5$ ? (without any conditioning this time).
(e) What is the distribution of $S_5|A$ ?
I Have no idea how to answer this question.any help is appreciated. I know it is to be done using Markov chains but don't understand how. What i attempted was $P(X_{t+1}=1|X_t=1)=a$ and $P(X_{T+1}=0|X_t=0)=b$ then i formed my transition matrix to be $$\begin{pmatrix}  a & 1-a \\ b & 1-b\end{pmatrix}.$$
Answer: Except in question (d) (which is quite obvious on its own), everything is conditioned on $A$ hence this is as if one had $n=10$ balls in a bag, $w=8$ of them white and $n-w=2$ black, which were drawn one by one, $X_k$ being $1$ if the $k$th ball drawn is white and $0$ otherwise. Thus:

$X_i=1$ means the ball number $i$ is white. By symmetry this happens with probability $w/n$. Thus, $P[X_i=1\mid A]=w/n$ and $P[X_i=0\mid A]=1-(w/n)$. Likewise for $X_j$ for every $j\ne i$.
$X_i=X_j=1$ means one draws a white ball, which happens with probability $w/n$, then another white ball, which happens with probability $(w-1)/(n-1)$, hence $P[X_i=X_j=1\mid A]=w(w-1)/(n(n-1))\ne(w/n)^2=P[X_i=1\mid A]P[X_j=1\mid A]$. Conditionally on $A$, $X_i$ and $X_j$ are not independent (and in fact they are negatively associated).
Unconditionally, $S_n$ is binomial $(n,\frac12)$ and $S_{2n}=S_n+S'_n$ where $(S_n,S'_n)$ is i.i.d. Thus, using $A=[S_{2n}=k]$, one sees that $P[S_n=i\mid A]$ is proportional to $P[S_n=i]P[S_n=k-i]$, that is, to ${n\choose i}{n\choose k-i}$. One recognizes the hypergeometric distribution with parameters $(n,n,k)$, hence, for every $0\leqslant i\leqslant k$,
$$
P[S_n=i\mid A]=\frac{{n\choose i}{n\choose k-i}}{{2n\choose k}}.
$$
Comment: Two remarks: (1) not a whiff of Markov chain in here; (2) the level of the last question is significantly higher than the rest, to the point that asking these together is debatable.
Comment: ...To which only the silence of the Abysses responded.

makov chains and property


Node.js - what are the consequences of an async function not calling back?

Question: What area the consequences of an async function simply returning without calling back?
I know you shouldn't do this. My question is hypothetical - I'm just interested to know what the consequences of doing this might be.
Is it bad for the stack?  Could it lead to memory leaks?
For example:
<code>function whatAreMyConsequences(value, callback) {
     value++;
     if(value > 1000) {
         return;
     }

     callback(null, value);
}
</code>
Comment: Nothing? Not calling the callback is a path not taken. It's not like node js examines your function arguments and requires every possible callback there in to be called, otherwise, you couldn't have a different 'success' or 'error' callback as is often the case.
Answer: As long as the calling code knows that the callback might not get called (and doesn't depend upon it for something in the logic flow), then there are no consequences.  There is no more or less chance of a memory leak if the callback is not called.  It has no net impact on the stack whether it is called or not.
A function passed as an argument is just an argument to the receiving function.  The receiving function can decide to do whatever it wants with that function.  It can call it always, never or sometimes.
Of course, the caller will be expecting some sort of behavior so it is up to the receiving function to hold up its end of the contract or the caller's code will not work correctly, but that's just correct programming logic.
In fact, it would be perfectly valid programming to have a function that accepts four callbacks and it decides which of the four to call based on something that happens or some other arguments that are called.
Look at promises:
<code> $.ajax(...).then(mySuccessHandler, myErrorHandler);
</code>
Only one of those two callbacks will be called and this is by design.
Comment: Heh, that was quick.  Thank you.