Matrix and regression model

Question:
Hi, can someone explain the second matrix? and how to use K in this problem? Thanks!
Comment: Please add the `[self-study]` tag & read its
[wiki](http://stats.stackexchange.com/tags/self-study/info). Then tell us
what you understand thus far, what you've tried & where you're stuck. We'll
provide hints to help you get unstuck.
Comment: I am about to write an answer for you. Please let me know if you are no longer interested.
Comment: @AntoniParellada I am still interested. Thanks! I think I've solved part A, not sure if it is correct tho.
Comment: I just posted an addition to my answer. I can show you with a code example if you are interested.
Comment: @AntoniParellada And I have another question, if I have SSE1 for 107 samples and SSE2 for another 98 samples. How to find the combined SSE?
Answer: The standard error (SE) of the estimate of a given parameter is given by the corresponding value along the diagonal of the estimated covariance matrix. So, for example, in an linear model such as $\hat y= \hat \beta_0 + \hat \beta_1\,x_1 + \hat \beta_2\,x_2$, the variance-covariance matrix will be of the form:
$$\hat{\text{Var}}(\hat\beta)=
\begin{bmatrix}
&\color{blue}{\text{intercept}}& \color{blue}{X_1} & \color{blue}{X_2}\\
\color{blue}{\text{intercept}}&&&\\
\color{blue}{X_1}&&\color{red}{\Huge \cdot}&\\
\color{blue}{X_2}
\end{bmatrix}
$$
and the square root of the entry in the red dot is the SE of the estimated coefficient of the variable $X_1,$ i.e. $\hat\beta_1$, let's label it $\text{SE}_{\hat\beta_1}.$
Now we need to build a confidence interval around the SE, which will entail calculating the degrees of freedom if the t distribution is used: ($df = \text{no. observations - no. variables + 1}$ (the $1$ comes in because of the intercept). Otherwise, with a large number of observations the normal distribution can be used, and at a $5\%$ significant level, we will use the normal quantile for $0.975=1.96$:
$$\text{CI}_{\hat\beta_1}=\hat\beta_1\pm1.96\times \text{SE}_{\hat\beta_1}$$
and, the key is that this confidence interval shouldn't include $0$ if it is to be considered statistically significant.
The rest should probably follow, and I will leave out given the self-study nature of the OP.

As for your less immediate question,

can someone explain the second matrix?

referring to the covariance matrix, it is the result of the equation:
$$\text{Var}(\hat\beta)= \sigma^2\,(X^\top X)^{-1}\tag{*}$$
with $X$ corresponding to the model matrix (including a column of $1$'s), and $\sigma^2$ the variance of the errors around the mean of the predicted points on the OLS line (the noise), which is calculated as:
$$\sigma^2= \frac{1}{df}\sum(\text{residuals})^2$$

Notice, that this is not the covariance matrix of the variables, but of the estimated parameters. The covariance matrix of the variables is calculated as:
$\Large \sigma(A) = \frac{G^TG}{n-1}$ with $G$ being the mean-centered observations and $n-1$ corresponding to the number of observations minus $1$.

Here is an intuition: $X^\top X$ is essential in the orthogonal projection of the dependent variable $Y$ onto the column space of the dataset $X$, encapsulating the geometry of the dataset. The fact that this latter statement is true is exactly the intuition of the variance-covariance matrix between the variables! So we project... with $(X^{\top}X)^{-1}$, orthogonally,... and we amplify (or make imprecise) that projection on either side of the projection by multiplying it times the variance of the noise ($\sigma^2$) in Eq. $*$.

EXAMPLE:
<code>> d = mtcars  # mtcars data set as an example (different car models and specs)
> names(d)    # names of the variables
 [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"

> fit = lm(mpg ~ qsec + wt + drat, d)  # OLS model

> X = model.matrix(fit)  # Model matrix X
> XtrX = t(X)  %*% X  # X transpose X
> inv_XtrX = solve(XtrX) # Its inverse
> df = nrow(d) - 4 # Degrees of freedom
> noise_variance = (1/df)  *  sum(residuals(fit)^2) # Variance of the residuals (sigma)
> (cov_matrix = noise_variance * inv_XtrX) # COVARIANCE MATRIX ESTIMATES

            (Intercept)        qsec          wt        drat
(Intercept)   65.106939 -1.36710241 -4.10219392 -7.59148854
qsec          -1.367102  0.06845697  0.02786122  0.01545768
wt            -4.102194  0.02786122  0.45981653  0.59099800
drat          -7.591489  0.01545768  0.59099800  1.50538184

# Take sqrt of one of diagonal elements (e.g. variable "drat"):

> (SE_drat = sqrt(cov_matrix[4,4]))
[1] 1.22694

> (summary(fit)) # Compare to its Std. Error for "drat" value:

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)  11.3945     8.0689   1.412  0.16892
qsec          0.9462     0.2616   3.616  0.00116 **
wt           -4.3978     0.6781  -6.485 5.01e-07 ***
drat          1.6561     1.2269   1.350  0.18789

> (CI_drat = coef(fit)[4] + c(1,-1) * qt(0.975,df) * SE_drat) # Its CI
[1]  4.1694177 -0.8571278

> matrix=cbind(d$qsec,d$wt,d$drat) # Compare now to the covariance matrix of the i.v.'s:
> (cov(matrix))
            [,1]       [,2]        [,3]
[1,]  3.19316613 -0.3054816  0.08714073
[2,] -0.30548161  0.9573790 -0.37272073
[3,]  0.08714073 -0.3727207  0.28588135

> var(matrix[,3]) # And you can match with the variance of "drat".
[1] 0.2858814
</code>
Comment: Isn't the diagonal the variance of these three variables?
Comment: @J.doe Are you comfortable with R? Let me append the answer.
Comment: I've inserted a pic I found in the question. Is the pic correct? I used that to construct a t test and then get the answer.
Comment: So I'll be able to find a new one and the diagonal of the new matrix is the variance of those three variables?
Comment: @J.doe Take a look at the output of the matrices in my code (posted in the answer), and the correspondence to the output of the summary for the regression model. Good luck!
Answer: First, the $K$ in this question refers to the number of $\beta$ coefficients ($\beta_1, \beta_2$), not including the intercept ($\beta_0$).
The second matrix is the so called Sample Covariance matrix, of $\hat{{\bf \beta}}$. It's elements are $Cov(\hat{\beta_i}, \hat{\beta_j})$ for all i, j.
Notes:

For $i=j$, $Cov(\hat{\beta_i}, \hat{\beta_i}) = Var(\hat{\beta_i})$
For $i \ne j$, $Cov(\hat{\beta_i}, \hat{\beta_j}) = Cov(\hat{\beta_j}, \hat{\beta_i})$ which is why its symmetric.
See here for more detail

Think about how you can use the covariance matrix in estimating the standard error for $\hat{\beta_i}$ in your confidence intervals.

Matrix and regression model


How to prevent cpanel from doing graceful restart every time an add on domain is added

Question: I want to add 300-400 add on domains and wild card subdomains.
Every time I add an add on domain, cpanel does a graceful restart.
The result is the whole server is down for 5-10minutes.
If I did:
tail -f /usr/local/apache/logs/error_log

    [Sun Dec 30 23:11:18 2012] [notice] Graceful restart requested, doing restart
[Sun Dec 30 23:18:01 2012] [notice] SSL FIPS mode disabled
[Sun Dec 30 23:18:01 2012] [notice] Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/1.0.0-fips mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/22.214.171.12435 configured -- resuming normal operations

If I do:
tail -f /usr/local/cpanel/logs/error_log
[2012-12-30 23:11:17 -0600] info [parkadmin] Creating Addon domain 'massadult.com' on 'massadult.freemarketforever.com'.
[2012-12-30 23:11:23 -0600] info [parkadmin] 'local' datastore in use (/var/cpanel/conf/apache/local)
/usr/local/cpanel/bin/ftpadmin : Syncing Ftp Databases....
[2012-12-30 23:13:42 -0600] info [cpmysqladmin] Creating MySQL database freemark_massadult for user freemark

Sometimes, [2012-12-30 22:37:36 -0600] info [ensure_vhost_includes] 'local' datastore in use (/var/cpanel/conf/apache/local)  shows up in cpanel error_log which could be the problem of why it took so long to graceful restart httpd.
So basically I want to add tons of add on domains and then do apache restart once. I've heard we can modify a script slightly to prevent this from happening. But which script and how?
Comment: This is reason number 14872582 to **not** use cPanel.
Comment: Honestly? [Vim](http://www.vim.org/). Or perhaps something like [Puppet](http://puppetlabs.com/).
Comment: Why the downvote if I may know?
Comment: The downvote wasn't from me. If I were to guess, though, it was due to the involvement of cPanel, which isn't looked upon well here. It tends to do things in non-standard ways and as such, makes troubleshooting quite difficult. My recommendation would be to ditch cPanel and commit to learning real linux sysadmin.
Comment: And how do anyone manage hundreds of domains without cpanel?
Comment: Again, I'd like to point out that many of us are *forced* to use cPanel. Often we're sysadmins at hosting companies. Neither `vim` nor Puppet provide *end users* with ways to manage their email addresses, create forwarders, edit `.htaccess` files using a GUI, view and manage log files and stats, track quota and bandwidth usage, install web software... the list goes on. Just because our companies make us use cPanel doesn't mean we're bad sysadmins :-) That being said, an Apache graceful restart should not bring Apache down and should not take 5-10 minutes. *That's* the issue here, not cPanel.
Comment: This question appears to be off-topic because it is about [`working with a service provider's management interface, such as cPanel`](http://serverfault.com/help/on-topic).
Answer: Please note that graceful restart is done any time cPanel makes an update to the Apache configuration. It is not the same as a full Apache restart. After a graceful restart, the uptime reported by /usr/local/apache/bin/apachectl status stays the same. A graceful restart is really a reloading of the configuration values, and should not affect the functioning of your sites.
In your case, the graceful restart is taking a long time, it means there must be an issue with you cPanel server, in this case you need to contact your serever admin and recheck the issue completely. Also if you want you can open ticket in cPanel support ticket.
Comment: 4 more reputation and I can upvote :)
Comment: I am the server admin. I guess I'll make tickets. It tends to happen when server load is high.
Answer: After add a domain cpanel invokes /scripts/restartsrv_apache internally. If you see the source code it invokes to /usr/local/cpanel/scripts/restartsrv_httpd script.
In that script, looking at source code, you can see interesting file flags for disable the apache restart like:

/etc/httpddisable
/etc/apachedisable
/etc/httpdisable
/var/cpanel/mgmt_queue/apache_update_no_restart

This last flag it is poorly documented in cPanel EasyApache docs.
Disabling that should help to mitigate the effect of restart/graceful apache each time that you add a domain. Remember restart Apache when you finish enable again the flag, because until apache will be restarted it would not notice the new domains for attend the requests.


Trying to use drupal_http_request to get an admin page

Question: I would like to render an admin page in a popup, I thought I'd use the ctools api to render the popup and drupal_http_request to get the data for the page, however even when logged in all I get is an 'unauthorized' error.
Looking deeper, I think there might be a way, as the function is reputed to support basic authentication, but I can't figure out how to make it work.
This is for Drupal 7.x
Or if there's a different way to render an admin page in a popup that'd be nice too. It's for a client who doesn't want to go back and forth between admin pages so the popup would solve the problem.
Answer: The Overlay module in the core is built to do exactly this, to open admin pages in an overlay popup.  You can enable it directly from <code>/admin/modules</code>.
Comment: Thanks, but actually the request was to render an admin page on a popup that takes place within the context of a different admin page. Like a popup that took place on the node edit page for example, so there would be like an option to do something on a different admin page within a popup on the node edit page itself.


why when creating e2-small it shows it has 2 vCPU and when checking configuration it shows it has only 1 vCPU

Question:
This is why I get when creating a  e2-small machine

This is what I got then checking the machine after creation:

This is what is shown on the page https://cloud.google.com/compute/vm-instance-pricing:

This is what I get when using cat /proc/cpuinfo:

Which one is right?
Answer: In the first screenshot, it shows e2-small (2 vCPU, 2 GB memory), and 1 shared core under vCPU. One CPU core is 2 vCPUs.
In the second screenshot, it shows one shared core, which is mentioned the same
in the first screenshot. Both the first and second screenshots are referring to the same thing.
The E2 machine series also contains shared-core machine types that use context- switching to share a physical core between vCPUs for multitasking. Different shared-core machine types sustain different amounts of time on a physical core.
In general, shared-core machine types can be more cost-effective for running small, non-resource intensive applications than standard, high-memory, or high-CPU machine types.
A vCPU is a multi-thread. Multithreading is a form of parallelization or dividing up work for simultaneous processing. Instead of giving a large workload to a single core, threaded programs split the work into multiple software threads. These threads are processed in parallel by different CPU cores to save time.
Each CPU core consists of two hyper-threads.
Refer to the documentation for more information on E2 family categories.


"exception in thread main java.lang.arrayindexoutofboundsexception" when using an array + for loop

Question: when I wrote this code to prompt the user for how many numbers are going to be entered, I got an error saying: "exception in thread main java.lang.arrayindexoutofboundsexception"
PS: note that I used an array of int + for loop to write it.

import java.util.*;
public class Pr8{
  public static void main(String[] args){
    Scanner scan = new Scanner (System.in);

    //Prompt the user for how many numbers are going to be entered.

    System.out.print("*Please write how many numbers are going to be entered: ");
    int a = scan.nextInt();
    int[] n = new int[a];

    for (int i = 0; i < a; i++){
      System.out.print("*Please enter an enteger: ");
      n[a] = scan.nextInt();
    }//for

  }//main
}//Pr8
Comment: that for loob will get you every time ;)
Answer: Change
n[a] = scan.nextInt();
to
n[i] = scan.nextInt();
a is not a valid index in an array that only has a elements. The valid indices are 0 to a-1.
Comment: @Bader ,`n` is the array containing `a` elements with valid indexes of `n` 0 to a-1 and not `a`. This is because array indexes start from 0 not 1
Comment: @Bader You are looping with the `i` variable, so I'm assuming you want to store in each iteration of the loop the new input in the i'th position of the array. You can't store it in the `a`'s position, since there's no such position in the array you created.
Comment: If you want to be able to do n.getElementWithValue(...) use arraylist ( or other list implementation ) then you can just do(with n Arraylist of Strings) n.get("This string")
Comment: @Eran can you please look at my question here: http://stackoverflow.com/questions/27181778/java-lang-arrayindexoutofboundsexception-50-handling-big-integers-on-java ///// the program I did is the 9's complement to deal with addition and subtraction in java, but there is some problems with it!!


Nova network DHCP not releasing ip with force_dhcp_release=True

Question: The problem I am seeing is that when nova-network calls dhcprelease upon
instance termination (due to forcedhcp_release=True) the address is not
always released(syslog are not showing DHCPRELEASE request). Then later on if nova assigned a new instance the same ip address that was not released the DHCP request is ignored and an error in the syslog will show that dnsmasq saw the request and refused because the ip address was already leased to a different MAC address (the one belonging to the old, terminated, VM).
Some details about my setup:

Juno release
legacy (nova-) network
Ubuntu 14.04
DHCP handled by DNSMASQ.

When hosts are able to get their ip address from the DHCP server everything
appears to be working perfectly fine. It seems as though the error is only
when an ip fails to be released and blocks subsequent use of it for future
VM's.
I checked for any errors in my nova-* logs and don't see any. The only
errors are in my syslog when dnsmasq refuses to lease the ip address due to
the conflicting MAC addresses.
Any info or suggestions would be much appreciated.
Answer: Still I am not able to find perfect solution for this issue but I have found the problem area and have some workaround.
1. Problem Area: Problem lays in dnsmasq not in OpenStack. I have observed that OpenStack is executing "dhcprelease" function after terminating instance each time but dnsmasq is responding to only few dhcprelease requests.
2.  Workaround: Default lease time of any lP is 24 Hrs (86400 Seconds) which means each instance must renew its lease after each 24 hrs. If instance do not renew its lease of IP then dnsmasq will consider that lease invalid and release the IP obtained by that lease.
I have reduced that lease to 3 minutes(180 Seconds). So any lease will not be able to hold the IP more than 3 minutes after terminating the instance.
Steps to reduce lease time to 3 minutes:
Perform following steps on all compute nodes one by one.

Open a file /etc/nova/nova.conf

vi /etc/nova/nova.conf

In [DEFAULT] section configure dhcp_lease_time.
It is in seconds.

[DEFAULT] ... dhcp_lease_time = 180

Save and quit file.
Kill dnsmasq process for each bridge on server. (OR you can perform killall also if dnsmasq is not used for any other purposes)

kill  (OR killall dnsmasq)

Restart services.

restart nova-api-metadata restart nova-compute restart nova-network


Maths-Physics question, can I solve this situation for $x$?

Question: So Let's say I have an object going at velocity $V$, initially. Each second, the current velocity $v$ is reduced by $v/x$ . After $250$ (arbitrary) seconds the velocity has been reduced to below/equal $0.01$ (arbitrary small amount). What is $x$?
The only formula I can really come up with describes only a single time-interval-step.
vnew = vold - (vold/x)
But I would like to have a formula where I can plug in:
-Initial velocity, Vinitial
-Number of time-steps, n
-Final velocity, Vfinal
...and get $x$.
(I hope this isn't too Physics-y for y'all. I'm not even sure if this can be done, but I thought that coming up with abstract formulas like this is more a mathematically-inclined thing so that's why I'm posting here.)
Comment: Need some clarification here.  Are you looking for a calculus-based solution?  And this is a one-dimensional problem, with $x$ being position and $v$ being velocity?  So essentially your rate of change of velocity (acceleration) depends on position?
Comment: No sorry, x is just 'unknown factor', not position.
Answer: Assuming that you mean that each step down in speed happens exactly each second which is strange.
$$V_{n+1} = V_{n}(1-\frac{1}{x})$$
Therefore:
$$V_{n+k} = V_{n}(1-\frac{1}{x})^k$$
and
$$V_{f} = V_{i}(1-\frac{1}{x})^n$$
Comment: And hence to complete the problem, $$1-\frac{1}{x} = \sqrt[n]{\frac{V_f}{V_i}} \implies x = -\frac{1}{\sqrt[n]{\frac{V_f}{V_i}}-1}.$$
Comment: That's it! Brilliant! Thank you!
Comment: Out of curiosity though, what exactly is happening with the introduction and reduction of k in lines 2 and 3?
Comment: Never mind I see it now. That was actually a crucial part of my problem- I didn't notice that for each time increment it was just multiplying the RHS by (1 - 1/x). Thanks again.


How can I use og:title and og:description for Twitter cards in Docusaurus 2?

Question: I'm wondering how I can reuse the title and description metadata for Twitter cards title and description, respectively. Docusaurus uses these values to populate og:title and og:description already so perhaps there is a way to pass these values as argument for Twitter cards generation.
Answer: If the og: tags are present, Twitter cards will use them. You will need a twitter:card tag to specify the card type, though - the rest can be og: tags equivalent to the Twitter Cards markup.


covering of a connected genus?

Question: Let $\Sigma_n$ be a connected sum of $n$ tori for $n \in \mathbb{N}$. So basically, $\Sigma_0$ is the sphere, $\Sigma_1$ is the torus and then it's basically connected tori and the whole thing has $n$ holes. (I hope you get the picture.) I want to know for which values of $n$ does the sphere $S^2$ cover $\Sigma_n$.
I think the $\Sigma_n$ can be written as $S^2\times S^2\times \cdots \times S^2$ with $n$ many $S^2$'s, but does it mean that $S^2$ can cover the $\Sigma_n$ for all values of $n$?
Answer: If $X$ is an $n$-fold cover of $Y$ then (under mild hypotheses which are satisfied here, say, for finite CW complexes) we have $\chi(X) = n \chi(Y)$ where $\chi$ is the Euler characteristic. The Euler characteristic of $\Sigma_g$ is $2 - 2g$, so it follows that if $\Sigma_g$ covers $\Sigma_h$ (any such covering map is necessarily finite, because $\Sigma_g$ is compact) then $1 - h$ must divide $1 - g$.
Setting $g = 0$ gives that $1 - h$ must divide $1$ (and the quotient must be positive, since it must be the degree of the cover) which gives $h = 0$. In other words, the only closed orientable surface that $S^2$ covers is itself.
An alternate argument is thinking about universal covers. $S^2 = \Sigma_0$ is simply connected, $T^2 \cong \Sigma_1$ has universal cover $\mathbb{R}^2$, and for $g \ge 2$, $\Sigma_g$ has universal cover the upper half plane $\mathbb{H} \cong \mathbb{R}^2$ (e.g. by the uniformization theorem although one can also write down a covering map explicitly). In particular the universal cover of $\Sigma_g$ is noncompact for $g \ge 1$ (this is is equivalent to the fundamental group being infinite) and so cannot be $S^2$.
Answer: Firstly, $\Sigma_n$ cannot be written as a product $S^2 \times \cdots \times S^2$.
Secondly, using the fact that the Euler characteristic of a covering is the product of the Euler characteristic of the base space and the degree of the cover, we see that $S^2$ can only cover $\Sigma_0$ (i.e., itself) since $\chi(\Sigma_n) = 2 - 2n$.

covering of a connected genus?


Obtaining higher order terms of the Baker-Campbell-Hausdorff formula as higher order commutators

Question: Let $G$ be a Lie group, then $(X,Y) \mapsto \log(\exp(X)\exp(Y))$ locally defines a group law on a neighbourhood of  $0 \in T_eG$. This local group law is analytic, and the first term is given by $X + Y$ and the second by $\frac{1}{2}[X,Y]$. According to Tits' textbook on Lie groups the Lie bracket may be obtained as the second order derivative of $G \times G \to G, \; (x,y) \mapsto xyx^{-1}y^{-1}$. (This is well-defined, because the first derivative of $(x,y) \mapsto xyx^{-1}y^{-1}$ vanishes.) My question is then:

Are there maps $\pi_r: G \times G \to G$ for every $r \in \mathbb{N}$ such that $d^k \pi_r$ vanishes for all $k < r$ (so that we obtain a well defined $r$-linear form $d^r \pi_r: T_eG \times \underbrace{\dots}_{2 r\, \times} \times T_eG \to T_eG$), such that the $r$-th term in the Baker-Campbell-Hausdorff formula is given by $\frac{1}{r!}d^r \pi_r$?
  Furthermore, can the maps $\pi_r$ be constructed from the multiplication and inverse maps on $G$?
Comment: You can obtain iterated brackets from higher order derivatives of the group commutators, e.g.  $[X,[X,Y]]$ from $(x,(x,y))$. See Thm 3.16 in Peter Michor's book ["Topics in differential geometry"](http://www.mat.univie.ac.at/~michor/dgbook.pdf).
Comment: @EeroHakavuori Your comment answers my question. If you post it as an answer I can accept it.
Answer: Thm 3.16 in Peter Michor's book "Topics in differential geometry" describes how on any smooth manifold iterated Lie brackets of vector fields can be computed from higher order derivatives of commutators of flows of the vector fields on the manifold.
In the case of Lie groups, the flow $\Phi_X:G\times\mathbb{R}\to G$ of a left-invariant vector field starting from a point $p\in G$ is given simply by $\Phi_X(p,t)=p\exp(tX)$. So in this case the commutators of flows from the identity $e\in G$ can be written using the group commutators $(x,y)=xyx^{-1}y^{-1}$ as
$$\Phi_Y^{-1}\circ\Phi_X^{-1}\circ\Phi_Y\circ\Phi_X(e,t) = \exp(tX)\exp(tY)\exp(-tX)\exp(-tY) = (\exp(tX),\exp(tY)).$$
The individual iterated brackets involved in the Baker-Campbell-Hausdorff formula are then given by higher order derivatives of these. For example a bracket of length three can be obtained from
\begin{align}
[X,[X,Y]] &= \frac{1}{6}\frac{d^3}{dt^3}(x,(x,y))\Big\vert_{t=0}\\
&=\frac{1}6\frac{d^3}{dt^3}x(x,y)x^{-1}(x,y)^{-1}\Big\vert_{t=0}\\
&=\frac{1}6\frac{d^3}{dt^3}xxyx^{-1}y^{-1}x^{-1}yxy^{-1}x^{-1}\Big\vert_{t=0},
\end{align}
where $x=\exp(tX)$ and $y=\exp(tY)$.
In order to get the entire $r$-th term of the BCH formula from a single map, you can then take products of these group commutators. For instance if $\alpha(t)$ and $\beta(t)$ are commutators with $\frac{d^3}{dt^3}\alpha(t)\Big\vert_{t=0}=[X,[X,Y]]$ and $\frac{d^3}{dt^3}\beta(t)\Big\vert_{t=0}=[Y,[Y,X]]$ and all their lower order derivatives vanish, then also all the lower order derivatives of $\alpha\beta$ vanish, and
$$\frac{d^3}{dt^3}\alpha(t)\beta(t)\Big\vert_{t=0} = \frac{d^3}{dt^3}\alpha(t)\Big\vert_{t=0}+\frac{d^3}{dt^3}\beta(t)\Big\vert_{t=0} = [X,[X,Y]]+[Y,[Y,X]].$$


Why is the speed of fully connected neural network very fast no matter how large the prediction dataset is?

Question: I used tensorflow(GPU and CPU) to train a 5-layers fully connected DNN with few parameters (Less than 300 parameters). And then I use it to predict two datasets with data sizes of 1000 and 1 million, respectively.  I found that not only the dataset with size of 1000 completed the prediction in 0.05 seconds, but also the dataset with size of 1 million completed the prediction in 0.05 seconds. Why?
Why can DNN predict such a huge dataset quickly?  Why does the size of the data set affect the speed of DNN so little ?
Comment: In what environment are  you running this?  Is it possible you are using a clustered computing system such as AWS where additional resources are spun up for larger problems and the work processed in parallel?
Comment: I just used a laptop, tensorflow_GPU and tensorflow_CPU processe datasets with sizes of 1 million very quickly. My model is very small (less than 20 neurons per layer) .
Answer: Your network is simply too small to be a real computational load. As a back of the envelope calculation, if you have a modern CPU with about $3.2$GHz, then you can do $3.2*10^9$ calculations per second. Your network only has $500$ parameters, so for an input of $1000$ elements, you do in each epoch about $500*1000=5*10^5$ operations which is orders of magnitude less than what your CPU can do in one second. If you now increase your data set to $10^6$ elements, then you still only have $500*10^6 = 5*10^8$ operations.
On the other hand, if you would add more layers, then your computation time would increase drastically. For an input layer of size $m$ and output layer of size $n$, you have(without bias) $m*n$ parameters. If you repeat this a few times, then you have very easily millions of parameters.
The calculation above is of course not realistic. There is a lot more to consider to estimate the performance of a network, such as memory transfer and usage.


How can I tell a control to NOT redraw itself?

Question: I've tried something like:
horizontalPictureScroller1.SuspendLayout();
horizontalPictureScroller1.DeleteSelectedImages();
horizontalPictureScroller1.ResumeLayout();

But it's still lagging visually when I run the DeleteSelectedImages() method.
Is there some way to manually tell a control to NOT redraw itself until I tell it to start drawing itself again?
Using Windows Forms and .NET 4
Comment: What do you mean it's lagging visually?  What are you doing after ResumeLayout()?
Comment: Have you tried turning on doublebuffering to remove the flickering?
Comment: Can you override OnRender while you're doing your stuff?
Answer: I suppose that your problem is that your screen updates during the delete operation resulting in flickering.
The SuspendLayout and ResumeLayour methods only suspend the layout of the control. It does not stop the control from redrawing or resizing.
You should enable DoubleBuffering on the control.
You should create a new control class derived from the WinForms control you are using here. In the constructor of this class enable the double buffering using SetStyle method.
Assuming that your control horizontalPictureScroller1 is a PictureBox -
class MyControl : System.Windows.Forms.PictureBox
    {
        public MyControl()
        {
            this.SetStyle(ControlStyles.DoubleBuffer |
                ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint,
                true);
        }
    }


Sum of a random number of r.v.'s

Question: A fair coin is flipped independently until the first Heads is observed. Let the random variable  K  be the number of tosses until the first Heads is observed plus 1. For example, if we see TTTHTH, then K=5. For  k=1,2,…,K , let  $X_k$   be a continuous random variable that is uniform over the interval  [0,5]. The  $X_k$  are independent of one another and of the coin flips.
Let
X=$\sum_{k=1}^k X_k$
 . Find the mean and variance of  X . You may use the fact that the mean and variance of a geometric random variable with parameter  p  are  $\frac 1p$and  ${(1−p)\over(p^2)}$ respectively.

What is $E[X]$?
What is $Var[X]$?

My solution:
$E[N] = \frac{1}{p} = 2$
$E[X_k] =\frac{1}{2}\times 5=\frac{5}{2}$
$E[X] = E[N]\times E[X_k]=2\times\frac{5}{2} = 5$
$Var[N]=\frac{(1−p)}{p^2} = 2$
$Var[X_k] = \frac{1}{12}\times 5^2 = \frac{25}{12}$
Comment: Avoid cross-posting: https://math.stackexchange.com/q/3291498/321264. Better to choose where you want to ask.
Comment: I saw this question on this site (https://stats.stackexchange.com/) hours ago.
Comment: Please decide which site you want to ask your question on, & delete the other version. Cross-posting is against SE policy & it wastes a lot of people's time.
Comment: I'm voting to close this question as off-topic because it is cross-posted on [math.SE].
Answer: Firstly, I assume $N=K$ in your solutions. The expected value and variance of $X$ can be found via Law of Iterated Expectation (LIE) and Law of Total Variance (LTV):
$$E[X]=E[E[X|K]], \ \ \ \ \operatorname{var}(X)=E[\operatorname{var(X|K)}]+\operatorname{var}(E[X|K])$$
For the expectation, your approach is correct, but it can be found via LIE:
$$E[X|K]=KE[X_k]\rightarrow E[KE[X_k]]=E[K]E[X_k]$$
You just need to correct your expectation for $K$: $E[K]=1/p+1$, since it is of the form $1+Y$, where $Y$ is a geometric RV with parameter $p$. Also, note that $\operatorname{var}(K)=\operatorname{var}(1+Y)=\operatorname{var}(Y)=(1-p)/p^2$ as yours.
For the variance, we need $\operatorname{var}(X|K)=\operatorname{var}(\sum X_k|K)=K \operatorname{var}(X_k)$, and by LTV:
$$\begin{align}\operatorname{var}(X)&=E[K\operatorname{var}(X_k)]+\operatorname{var}(KE[X_k])\\ &=\operatorname{var}(X_k)E[K]+E[X_k]^2\operatorname{var}(K)\end{align}$$
The rest is substitution.
Answer: Formulas for the mean and variance of $X$ are stated and derived here.
Notice that there are two components to $V(X).$
If you're interested in distribution of $X$ you can do a simulation. In R, the a geometric random variable counts the number of failures before the first success;
this is the second version in Wikipedia. With a million iterations one can expect
about 3 significant digits of accuracy.
<code>set.seed(712)
x = replicate(10^6,  sum(runif(rgeom(1,.5)+2,0,5)))
mean(x);  var(x)
[1] 7.497338     # aprx E(X) = 15/2
[1] 18.73797     # aprx V(X) = 75/4
mean(x < 10)
[1] 0.787989     # aprx P(X<10) = 0.788 +/- 0.001
2*sd(x < 10)/10^3
[1] 0.0008174656 # aprx 95% marg of sim err

hist(x, prob=T, br=30, col="skyblue2", main="")
</code>

Sum of a random number of r.v.'s


find the fundamental group with van kampen.

Question: how I must prove that the fundamental group of this shape with base point which is heavily shaded is $\left \langle a^{2},b^{2},aba^{-1},bab^{-1} \right \rangle$,and I want to use van kampen theorem.very naturally I think to pick the eight figure each side of the base point and do the van kampen theorem but it didn't go well,it will be great if you help me with some hint or explanation.
Comment: The $\pi_0(\infty, c)$ is generated by $a^2$ and $b$. The isomorphism $\pi_0(\infty, c)\cong\pi_0(\infty, s)$ sends a loop $\gamma$ to $a\gamma a^{-1}$.
Comment: The right side has fundamental group $\langle a^2, aba^{-1}\rangle$. You can see this by applying van Kampen to the figure eight with a base point in the middle, call it $c$. Then you can take the isomorphism $\beta_a:\pi_0(\infty,c)\cong\pi_0(\infty,s)$ (where $s$ is the shaded point) given by the upper edge $a$.
Comment: my problem is exactly here :why it is $\langle a^2, aba^{-1}\rangle$?
Answer: Applying van Kampen to the open cover formed by small enough open neighborhoods around the two figure eight $\Large∞$ which meat at the heavily shaded dot $\bullet$, you see that
$$π_0(\Large∞\hspace{-5pt}\small\bullet\hspace{-5pt}\Large∞, \small\bullet)\approx
π_0(\Large∞\hspace{-3pt}\small\bullet,\bullet)
*π_0(\small\bullet\hspace{-3pt}\Large∞,\small\bullet)$$
Now $π_0(\Large∞\hspace{-9pt}\small\bullet\hspace{6pt},\bullet)$ is generated by $a^2$ and $b$, and there is an isomorphism
$$\beta_a:π_0(\Large∞\hspace{-9pt}\small\bullet\hspace{6pt},\bullet)\to
π_0(\small\bullet\hspace{-3pt}\Large∞\small,\bullet)$$ given by sending the class of a loop $\gamma$ to the class of the loop $a\gamma a^{-1}$, which means that
$π_0(\small\bullet\hspace{-3pt}\Large∞\small,\bullet)$ is generated by $a^2$ and $aba^{-1}$.


Can you connect to a VPN through an SSH reverse tunnel?

Question: Say I have two machines, both running Linux;
Machine A is running a VPN server and is behind a NAT.
Machine B is the VPN client.
Machine A creates a reverse SSH tunnel from itself to Machine B, exposing a port on Machine B that connects back to the VPN running on Machine A.
How can I connect to Machine A's VPN server from Machine B through the SSH reverse tunnel?
Comment: I've seen something similar done with this using iptables; askubuntu.com/questions/324685/how-to-route-all-internet-traffic-through-tor-the-onion-router
Answer: A SSH tunnel only works with TCP, so your VPN server must be configured to use TCP for connections. You connect the the tunnel endpoint on Machine B just as you would connect to Machine A. In other words, just replace the ip address and destination port in your client configuration.
Answer: Suppose we have this setup:

Machine B has public domain machine.b and port 22 will access SSH server of Machine B.
Machine A has local IP address 192.168.10.2 and its VPN server uses port 1194.

Steps to setup a VPN connection through reverse SSH connection:

Machine A establishes an SSH connection to Machine B with this command: ssh -i "/path/to/key/file" -R 2500:192.168.10.2:1194 firstname.lastname@example.com -p 22
Machine B establishes a VPN connection to 127.0.0.1:2500

As the result, connection from Machine B to port 2500 of localhost (127.0.0.1) will be redirected through the SSH tunnel and go to port 1194 of 192.168.10.20 on Machine A, which is the VPN server of Machine A.
I always use this setup as my DIY VPN server in case the company's VPN is in maintenance.


Android: ViewPager Gallery from sdcard

Question: I have some code for a ViewPager Gallery, however the images are taken from the drawable folder, whereas I need them from a folder on the sdcard.
protected Integer[] mImageIds = {
        R.drawable.sample_1,
        R.drawable.sample_2,
        R.drawable.sample_3,
        R.drawable.sample_4,
        R.drawable.sample_5,
        R.drawable.sample_6,
        R.drawable.sample_7
};

This is the code that needs changing, how do I replace these from drawable folder with images from sdcard?
Full Code
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

public class Main extends FragmentActivity implements AdapterView.OnItemSelectedListener, ViewPager.OnPageChangeListener {

    private ViewPager viewPager;
    private Gallery gallery;

    protected Integer[] mImageIds = {
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3,
            R.drawable.sample_4,
            R.drawable.sample_5,
            R.drawable.sample_6,
            R.drawable.sample_7
    };
    private TextView textView;
    private Handler handler;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        viewPager = (ViewPager) findViewById(R.id.view_pager);
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        viewPager.setOnPageChangeListener(this);

        gallery = (Gallery) findViewById(R.id.gallery);
        gallery.setAdapter(new ImageAdapter());
        gallery.setOnItemSelectedListener(this);

        textView = (TextView) findViewById(R.id.title);

        handler = new Handler();

    }

    // gallery item selected
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        updateUI(i);
    }

    // view pager item selected
    public void onPageSelected(int i) {
        updateUI(i);
    }

    private void updateUI(final int i) {
        handler.post(new Runnable() {
            public void run() {
                gallery.setSelection(i);
                viewPager.setCurrentItem(i);
                textView.setText("Photo #" + i);
            }
        });
    }

    public void onNothingSelected(AdapterView<?> adapterView) {    }

    public void onPageScrolled(int i, float v, int i1) {    }

    public void onPageScrollStateChanged(int i) {    }

    public class ImageAdapter extends BaseAdapter {

        public int getCount() {
            return mImageIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            ImageView imageView = new ImageView(Main.this);

            imageView.setImageResource(mImageIds[position]);
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);

            return imageView;
        }
    }

    public class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mImageIds.length;
        }

        @Override
        public Fragment getItem(final int position) {
            return new Fragment() {
                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                    ImageView imageView = new ImageView(Main.this);
                    imageView.setImageResource(mImageIds[position]);
                    return imageView;
                }
            };
        }
    }

}

Answer: Instead of calling setImageResource, call setImageDrawable with a bitmap you loaded from the sdcard. For example, create a Bitmap from a sdcard path and then setImageDrawable:
Bitmap bitmap = BitmapFactory.decodeFile(path_of_file);
imageView.setImageDrawable(bitmap);

Note, image decoding should not be done of the UI thread. The android documentation discuss efficiently loading images off the ui thread. The AsyncTask discussed can be modified to meet your needs with a one line or two line change.

Android: ViewPager Gallery from sdcard


Expected value of Combined random variable with function E(X^2Y^3)

Question: ok, so the question goes like this. see embedded link enter image description here
I have no problem finding E[X] and E[Y] using iterative expectation. Unfortunately, X and f(Y|X) is not independent therefore simply multiplying E[X] and E[Y] will not give the final outcome. Any suggestion on how to do this? or Must we use rigorous method of integration etc?
Answer: I'll start you with a hint to give you chance to finish it yourself (and because I'm on my phone)
$$ E[Y^2X^3] = E[E[Y^2X^3 | X]] = E[X^3E[Y^2 | X]] $$
Comment: Thanks, thats more than enough :) i think i tried from bottoms up approach.


How to join tables with hook_views_data

Question: I have two tables activity and activity_map. I need to join both the tables by aid. Below is my code for hook_views_data. Base table is working fine including custom text handler. But I do not see the joined table(activity_map) fields (entity_id, entity_type)  either in fields section nor in arguments or filters. Am I missing anything?
  <?php

    /**
 * Implements hook_views_data
 */
function activity_teamie_views_data() {
  $data['activity_teamie']['table']['group'] = t('Activity');
  $data['activity_teamie']['table']['base'] = array(
    'title' => t('Activity'),
    'field' => 'aid',
    'help' => t('Activity feed item')
  );
  $data['activity_teamie']['aid'] = array(
    'title' => t('Activity ID (aid)'),
    'help' => t('Activity ID'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click sortable' => TRUE
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_numeric'
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric'
    ),
    'sort' => array(
      'handler' => 'views_handler_sort'
    )
  );
  $data['activity_teamie']['template'] = array(
      'real field' => 'template',
      'title' => 'Activity',
      'help' => 'Text describing the activity',
      'field' => array(
        'handler' => 'activity_teamie_handler_field_activity_text',
      ),

  );
  $data['activity_teamie']['data'] = array(
    'title' => 'Data',
    'help' => 'Data array',
    'field' => array(
      'handler' => 'views_handler_field',
    ),
  );
  $data['activity_teamie']['created'] = array(
    'title' => t('Created'),
    'help' => t('Created timestamp'),
    'sort' => array(
      'handler' => 'views_handler_sort_date'
    )
  );
  $data['activity_teamie']['updated'] = array(
    'title' => t('Updated'),
    'help' => t('Updated timestamp'),
    'field' => array(
      'handler' => 'views_handler_field_date',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort_date'
    )
  );

  /* ACTIVITY MAP */
  $data['activity_teamie_map']['table']['group'] = t('Activity');

  // Join with the {activity} table
  $data['activity_teamie']['table']['join'] = array(
    'activity_teamie_map' => array(
      'left_field' => 'aid',
      'field' => 'aid'
    )
  );
  $data['activity_teamie_map']['aid'] = array(
      'title' => t('Aid Map'),
      'help' => t('Aid Map'),
    'field' => array(
          'handler' => 'views_handler_field',
  ),
      'filter' => array(
        'handler' => 'views_handler_filter_numeric'
  ),
      'argument' => array(
        'handler' => 'views_handler_argument_numeric'
  )
  );
  $data['activity_teamie']['entity_type'] = array(
    'title' => t('Entity type'),
    'help' => t('Entity type of the associated entity'),
  'field' => array(
        'handler' => 'views_handler_field',
  ),
    'filter' => array(
      'handler' => 'views_handler_filter_string'
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string'
    )
  );
  $data['activity_teamie']['entity_id'] = array(
    'title' => t('Entity ID'),
    'help' => t('Entity ID of the associated entity'),
  'field' => array(
        'handler' => 'views_handler_field',
  ),
    'argument' => array(
      'handler' => 'views_handler_argument_numeric'
    )
  );
  return $data;
}

Comment: Since I don't have enough time to try it, the best I can offer is point you to http://drupal.stackexchange.com/questions/3275/how-do-i-display-an-external-database-data/3321#3321 . Those steps allow you to create a views integration for an arbitrary schema, and then you can dump the code and check it against yours to find the problem.
Comment: Did you try defining the join in the base table?
Comment: Yes. and edited my question with latest code. can you please help
Answer: $data['activity_teamie']['table']['join'] = array(
  'activity_teamie_map' => array(
    'left_field' => 'aid',
    'field' => 'aid'
  )
);

You actually want to join the other way round.kk
A general tip, try to think of some kind of similar join relation in Drupal core and look it up in views/modules/$module.views.inc
$data['activity_teamie']['entity_id'] = array(

Isn't entity_id part of activity_teamie_map?

How to join tables with hook_views_data


Is there ever a time when computing the limit of Riemann sums is the best way to compute a definite integral?

Question: Is there ever a time when computing the limit of Riemann sums is the best way to compute a definite integral?
The use of Riemann sums is a great way to approximate the area under the curve, and the area under the curve is exactly equal to the limit as the number of rectangles goes to infinity (or as the maximum width of the rectangle goes to zero).
I feel like a common stumbling block in calculus instruction is how it is typical to introduce Riemann sums, do some approximations, express a given area as the limit as the number of rectangles goes to infinity, and then start computing integrals. Students get the idea that evaluating definite integrals is about computing the limit of Riemann sums rather than using antiderivatives and the fundamental theorem of calculus.
So I was wondering... Is there ever a time when a closed form antiderivative is not available, but the limit of Riemann sums is computable without too strenuous of effort? I don't think I've ever seen an example of this kind before.
Comment: Well, this is essentially how Archimedes computed his integrals, and he did not know of antiderivatives...
Comment: The definite integral is the result of evaluating a limit which represents the converging value of the ever more precise determination of the area. Because of being a limit is never reached, despite our best efforts. Even when the antiderivative s help us calculate it, we still found the limit. Even the area of a rectangle is the result of evaluating a limit.
Comment: There are very extremely many functions without closed form antiderivatives ...
Answer: This isn't quite what you're asking for, because there is no limit taken.  But sometimes we don't even know the function.  I used to work for a power company and we bought electricity off the grid.  This electricity is not measured like the electricity to your house (in kilowatthours.)  Instead, once per hour the "load" was measured.  The amount of electricity is the integral of the load.  All we know about the load function is its value every hour on the hour.  The Trapezoid rule was used to estimate the area and that's what we paid for the power we used.
So the point is: You have to use Riemann sums (or something similar) when you don't know what the actual function is.


Compute the DateTime of an upcoming weekday

Question: How can I get the date of next Tuesday?
In PHP, it's as simple as strtotime('next tuesday');.
How can I achieve something similar in .NET
Comment: ASP.NET is a set of web technologies. C# is a language. You really need to think of this in terms of plain .NET. Now, for "next Tuesday" - is that "the first Tuesday after today"? If it were Monday and someone said "see you next Tuesday" I'd expect that to mean 8 days time rather than 1. What about if today is a Tuesday? What time of day do you need?
Comment: @brenjtL: And if it's already Tuesday?
Comment: If today is tuesday, you want to find the date when is next tuesday? Or today is monday, you want to find the 2nd tuesday from monday?
Comment: The closest tuesday going forward to which ever particular day it is.
Comment: If already Tuesday then that same day
Answer: As I've mentioned in the comments, there are various things you could mean by "next Tuesday", but this code gives you "the next Tuesday to occur, or today if it's already Tuesday":
DateTime today = DateTime.Today;
// The (... + 7) % 7 ensures we end up with a value in the range [0, 6]
int daysUntilTuesday = ((int) DayOfWeek.Tuesday - (int) today.DayOfWeek + 7) % 7;
DateTime nextTuesday = today.AddDays(daysUntilTuesday);

If you want to give "a week's time" if it's already Tuesday, you can use:
// This finds the next Monday (or today if it's Monday) and then adds a day... so the
// result is in the range [1-7]
int daysUntilTuesday = (((int) DayOfWeek.Monday - (int) today.DayOfWeek + 7) % 7) + 1;

... or you could use the original formula, but from tomorrow:
DateTime tomorrow = DateTime.Today.AddDays(1);
// The (... + 7) % 7 ensures we end up with a value in the range [0, 6]
int daysUntilTuesday = ((int) DayOfWeek.Tuesday - (int) tomorrow.DayOfWeek + 7) % 7;
DateTime nextTuesday = tomorrow.AddDays(daysUntilTuesday);

EDIT: Just to make this nice and versatile:
public static DateTime GetNextWeekday(DateTime start, DayOfWeek day)
{
    // The (... + 7) % 7 ensures we end up with a value in the range [0, 6]
    int daysToAdd = ((int) day - (int) start.DayOfWeek + 7) % 7;
    return start.AddDays(daysToAdd);
}

So to get the value for "today or in the next 6 days":
DateTime nextTuesday = GetNextWeekday(DateTime.Today, DayOfWeek.Tuesday);

To get the value for "the next Tuesday excluding today":
DateTime nextTuesday = GetNextWeekday(DateTime.Today.AddDays(1), DayOfWeek.Tuesday);

Comment: The `+7)%7` solution is pretty nice though. Although the reason I didn't use that is because it's a bit of a micro-optimization and too easy to get wrong (as well as sacrificing some readability), imho of course.
Comment: @brenjt: Actually I'd say that Sven's is more versatile, as you can specify the day of week, but it's your call :) (I've now edited mine to give a more generalized version.)
Comment: Wow, I was just wondering on how I could get nth days til the next tuesday and you then updated your answer with an example Nice. Thanks
Comment: It was hard to choose the correct answer. But yours seems to be the most versatile and you made it easy to understand. Thanks for you help.
Comment: A unit test:        [TestMethod]
        public void ShouldGetNextSaturday()
        {
            var now = DateTime.Now;
            var test = GetNextWeekday(DateTime.Today, DayOfWeek.Saturday);

            Assert.IsTrue(now.Day < test.Day, "The expected month day is not here.");

            Assert.IsTrue(test.DayOfWeek == DayOfWeek.Saturday, "The expected week day is not here.");

            Assert.IsTrue((test.Day - now.Day) < 7, "The expected day interval is not here.");
        }
Comment: @rasx: Bad unit test - would fail towards the end of the month, when it could be (say) the 30th and the next Saturday is the 2nd. (I don't generally like using "today's date" in unit tests anyway - I like tests to be repeatable.) Also assumes the date doesn't roll over between evaluating `DateTime.Now` and `DateTime.Today`.
Comment: @Jon: great observations! ...what .NET Unit Testing framework do you prefer by the way? Also, do you recommend any books or writs on Unit Testing?
Comment: @rasx: I personally use NUnit, just because it's always done what I needed, occasionally with a bit of help. Doesn't mean it's the best :) I haven't read many books about unit testing, to be honest.
Answer: This should do the trick:
static DateTime GetNextWeekday(DayOfWeek day)
{
    DateTime result = DateTime.Now.AddDays(1);
    while( result.DayOfWeek != day )
        result = result.AddDays(1);
    return result;
}

Comment: This will return the next Tuesday. If you want it to return today, just remove the `.AddDays(1)` from the first line, that way it will also check `DateTime.Now` itself.
Comment: Great response, if today is Tuesday (which it is ha) will this return today or the next tuesday?
Answer: There are less verbose and more clever/elegant solutions to this problem, but the following C# function works really well for a number of situations.
///
/// Find the closest weekday to the given date
///
/// <param name="includeStartDate">if the supplied date is on the specified day of the week, return that date or continue to the next date</param>
/// <param name="searchForward">search forward or backward from the supplied date. if a null parameter is given, the closest weekday (ie in either direction) is returned</param>
public static DateTime ClosestWeekDay(this DateTime date, DayOfWeek weekday, bool includeStartDate = true, bool? searchForward=true)
{
    if (!searchForward.HasValue && !includeStartDate)
    {
        throw new ArgumentException("if searching in both directions, start date must be a valid result");
    }
    var day = date.DayOfWeek;
    int add = ((int)weekday - (int)day);
    if (searchForward.HasValue)
    {
        if (add < 0 && searchForward.Value)
        {
            add += 7;
        }
        else if (add > 0 && !searchForward.Value)
        {
            add -= 7;
        }
        else if (add == 0 && !includeStartDate)
        {
            add = searchForward.Value ? 7 : -7;
        }
    }
    else if (add < -3)
    {
        add += 7;
    }
    else if (add > 3)
    {
        add -= 7;
    }
    return date.AddDays(add);
}

Comment: The only answer that implements as extension to DateTime.  While the other solutions all work, having it as an extension method produces the easiest to use code.
Answer: @Jon Skeet good answer.
For previous Day:
private DateTime GetPrevWeekday(DateTime start, DayOfWeek day) {
    // The (... - 7) % 7 ensures we end up with a value in the range [0, 6]
    int daysToRemove = ((int) day - (int) start.DayOfWeek - 7) % 7;
    return start.AddDays(daysToRemove);
}

Thanks!!
Comment: Note that this solution involves negative numbers handed to the modulo operator. The [Wikipedia article about the modulo operator](https://en.wikipedia.org/wiki/Modulo_operation) says that _"When either a or n is negative, the naive definition breaks down and programming languages differ in how these values are defined."_ While this probably works in C#, a mathematically more 'solid' solution to get the same result would be to swap the `DayOfWeek` values like this: `int daysToSubtract = -(((int)dateTime.DayOfWeek - (int)day + 7) % 7);`
Answer: DateTime nextTuesday = DateTime.Today.AddDays(((int)DateTime.Today.DayOfWeek - (int)DayOfWeek.Tuesday) + 7);

Comment: If today is Monday, the answer that you provided would yield a week from Tuesday, rather than tomorrow.
Answer: Very simple sample to include or exclude current date, you specify the date and the day the week you are interested in.
public static class DateTimeExtensions
{
    ///
    /// Gets the next date.
    ///
    /// <param name="date">The date to inspected.</param>
    /// <param name="dayOfWeek">The day of week you want to get.</param>
    /// <param name="exclDate">if set to <c>true</c> the current date will be excluded and include next occurrence.</param>
    /// <returns></returns>
    public static DateTime GetNextDate(this DateTime date, DayOfWeek dayOfWeek, bool exclDate = true)
    {
        //note: first we need to check if the date wants to move back by date - Today, + diff might move it forward or backwards to Today
        //eg: date - Today = 0 - 1 = -1, so have to move it forward
        var diff = dayOfWeek - date.DayOfWeek;
        var ddiff = date.Date.Subtract(DateTime.Today).Days + diff;

        //note: ddiff < 0 : date calculates to past, so move forward, even if the date is really old, it will just move 7 days from date passed in
        //note: ddiff >= (exclDate ? 6 : 7) && diff < 0 : date is into the future, so calculated future weekday, based on date
        if (ddiff < 0 || ddiff >= (exclDate ? 6 : 7) && diff < 0)
            diff += 7;

        //note: now we can get safe values between 0 - 6, especially if past dates is being used
        diff = diff % 7;

        //note: if diff is 0 and we are excluding the date passed, we will add 7 days, eg: 1 week
        diff += diff == 0 & exclDate ? 7 : 0;

        return date.AddDays(diff);
    }
}

some test cases
[TestMethod]
    public void TestNextDate()
    {
        var date = new DateTime(2013, 7, 15);
        var start = date;
        //testing same month - forwardOnly
        Assert.AreEqual(start = start.AddDays(1), date.GetNextDate(DayOfWeek.Tuesday)); //16
        Assert.AreEqual(start = start.AddDays(1), date.GetNextDate(DayOfWeek.Wednesday)); //17
        Assert.AreEqual(start = start.AddDays(1), date.GetNextDate(DayOfWeek.Thursday)); //18
        Assert.AreEqual(start = start.AddDays(1), date.GetNextDate(DayOfWeek.Friday)); //19
        Assert.AreEqual(start = start.AddDays(1), date.GetNextDate(DayOfWeek.Saturday)); //20
        Assert.AreEqual(start = start.AddDays(1), date.GetNextDate(DayOfWeek.Sunday)); //21
        Assert.AreEqual(start.AddDays(1), date.GetNextDate(DayOfWeek.Monday)); //22

        //testing same month - include date
        Assert.AreEqual(start = date, date.GetNextDate(DayOfWeek.Monday, false)); //15
        Assert.AreEqual(start = start.AddDays(1), date.GetNextDate(DayOfWeek.Tuesday, false)); //16
        Assert.AreEqual(start.AddDays(1), date.GetNextDate(DayOfWeek.Wednesday, false)); //17

        //testing month change - forwardOnly
        date = new DateTime(2013, 7, 29);
        start = date;
        Assert.AreEqual(start = start.AddDays(1), date.GetNextDate(DayOfWeek.Tuesday)); //30
        Assert.AreEqual(start = start.AddDays(1), date.GetNextDate(DayOfWeek.Wednesday)); //31
        Assert.AreEqual(start = start.AddDays(1), date.GetNextDate(DayOfWeek.Thursday)); //2013/09/01-month increased
        Assert.AreEqual(start.AddDays(1), date.GetNextDate(DayOfWeek.Friday)); //02

        //testing year change
        date = new DateTime(2013, 12, 30);
        start = date;
        Assert.AreEqual(start = start.AddDays(1), date.GetNextDate(DayOfWeek.Tuesday)); //31
        Assert.AreEqual(start = start.AddDays(1), date.GetNextDate(DayOfWeek.Wednesday)); //2014/01/01 - year increased
        Assert.AreEqual(start = start.AddDays(1), date.GetNextDate(DayOfWeek.Thursday)); //02
    }

Comment: I have made additional changes from the original answer after some extensive testing.  This will now safely calculate the next day based on the date used, past, present and future.
All the previous examples where great, but failed under certain conditions.
I did not make it a one-liner statement, so that additional comments could be made to what the calculations are doing.
The positive case by Jon Skeet was great, although the case I had was to move 1 day back from a date, but still greater than Today, and what if it moves to today or to yesterday ... this solved it.
Answer: DateTime nexttuesday=DateTime.Today.AddDays(1);

while(nexttuesday.DayOfWeek!=DayOfWeek.Tuesday)
   nexttuesday = nexttuesday.AddDays(1);

Answer: Now in oneliner flavor - in case you need to pass it as parameter into some mechanism.
DateTime.Now.AddDays(((int)yourDate.DayOfWeek - (int)DateTime.Now.DayOfWeek + 7) % 7).Day

In this specific case:
DateTime.Now.AddDays(((int)DayOfWeek.Tuesday - (int)DateTime.Now.DayOfWeek + 7) % 7).Day

Answer: It could be an extension also, it all depends
public static class DateTimeExtensions
{
    public static IEnumerable<DateTime> Next(this DateTime date, DayOfWeek day)
    {
        // This loop feels expensive and useless, but the point is IEnumerable
        while(true)
        {
            if (date.DayOfWeek == day)
            {
                yield return date;
            }
            date = date.AddDays(1);
        }
    }
}

Usage
    var today = DateTime.Today;
    foreach(var monday in today.Next(DayOfWeek.Monday))
    {
        Console.WriteLine(monday);
        Console.ReadKey();
    }

Comment: A yield return in a loop without an exit clause seems a little risky.
Answer: I want to get next day from this time include today
also I want it to be at 12:00 AM
  public static DateTime GetNextWeekday(DateTime start, DayOfWeek day , bool includetoday = false) {
        if (includetoday && start.DayOfWeek == day) return start.Date;
        int daysToAdd = ((int)day - (int)start.DayOfWeek + 7) % 7;
        return start.Date.AddDays(daysToAdd);
    }

Comment: But this would also return the same day even if ``includeToday = false``
Comment: Yes but this makes the first line `if (includetoday && ...` obsolete
Comment: i use this code in my app... for sure ... u can check last line and if date was the same add 7 days more
Comment: if it is Monday and u want next Monday `includetoday`  should be `false` ...
if it is Monday and u want nearest Monday `includetoday`  should be `true` ...
like now it is 2:00 Am And  want to get 9:00 Am in next Monday ( Today )
Comment: there is should be check of Clock too .. if time was pass .. add 7 day more
... and this can be added in first line
Answer: Objective C Version:
+(NSInteger) daysUntilNextWeekday: (NSDate*)startDate withTargetWeekday: (NSInteger) targetWeekday
{
    NSInteger startWeekday = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate:startDate];
    return (targetWeekday - startWeekday + 7) % 7;
}

Comment: Cool answer, but the original question asked about .NET.


OpenMP. Parallelization of the loop in N threads

Question: I am trying to parallelize a cycle of 50 million iterations with several threads - first by 1, then by 4, 8 and 16. Below is the code for implementing this functionality.
#include <iostream>
#include <omp.h>

using namespace std;

void someFoo();

int main() {
    someFoo();
}

void someFoo() {
    long sum = 0;
    int numOfThreads[] = {1, 4, 8, 16};

    for(int j = 0; j < sizeof(numOfThreads) / sizeof(int); j++) {
        omp_set_num_threads(numOfThreads[j]);
        start = omp_get_wtime();
        #pragma omp parallel for
        for(int i = 0; i<50000000; i++) {
            sum += i * 10;
        }
        #pragma omp end parallel

        end = omp_get_wtime();

        cout << "Result: " << sum << ". Spent time: " << (end - start) << "\n";
    }
}

It is expected that in 4 threads the program will run faster than in 1, in 8 threads faster than in 4 threads, and in 16 threads faster than in 8 threads, but in practice this is not the case - everything is executed at a chaotic speed and there is almost no difference. Also, it is not visible in the task manager that the program is parallelized. I have a computer with 8 logical processors and 4 cores.
Please tell me where I made a mistake and how to properly parallelize the loop in N threads.
Answer: There is a race condition in your code because sum is read/written from multiple threads at the same time. This should cause wrong results. You can fix this using a reduction with the directive #pragma omp parallel for reduction(+:sum). Note that OpenMP does not check if your loop can be parallelized, it is your responsibility.
Additionally, the parallel computation might be slower than the the sequential one since a clever compiler can see that sum = 50000000*(50000000-1)/2*10 = 12499999750000000 (AFAIK, Clang does that). As a result, the benchmark is certainly flawed. Note that this is certainly bigger than what the type long can contain so there is certainly an overflow in your code.
Moreover, AFAIK, there is no such thing as the directive #pragma omp end parallel.
Finally, note that you can control the number of thread using the OMP_NUM_THREADS environment variable which is generally more convenient than setting it in your application (Hardwiring a given number of thread in the application code is generally not a good idea, even for benchmarks).
Comment: *AFAIK, there is no such thing as the directive #pragma omp end parallel*. Well, there is, but it's required when programming in Fortran, not C or C++.
Answer:
Please tell me where I made a mistake and how to properly parallelize
the loop in N threads.

First you need to fix some of the compiler issues on your code example. Like removing pragmas like #pragma omp end parallel, declaring the variables correctly and so on. Second you need to fix the race condition during the update of the variable sum. That variable is shared among threads an updated concurrently. The easiest way would be to use the reduction clause of OpenMP, you code would look like the following:
#include <stdio.h>
#include <omp.h>

int main() {
    someFoo();
}

void someFoo() {
    int numOfThreads[] = {1, 4, 8, 16};

    for(int j = 0; j < sizeof(numOfThreads) / sizeof(int); j++) {
        omp_set_num_threads(numOfThreads[j]);
        double start = omp_get_wtime();
        double sum = 0;
        #pragma omp parallel for reduction(+:sum)
        for(int i = 0; i<50000000; i++) {
            sum += i * 10;
        }
        double end = omp_get_wtime();
        printf("Result: '%d' : '%f'\n", sum, (end - start));
    }
}

With that you should get some speedups when running with multi-cores.
NOTE: To solve the overflow mentioned first by @Jérôme Richard, I change the 'sum' variable from long to a double.
Comment: Note that if `sum` is a `double`, then the type barely provide sufficient precision for the computation to give a correct result. If works in this case on 64-bit IEEE-754 compliant machines, but if we just try `sum += 1;` at the end, then the result would be wrong. In fact, theoretically, the C++ standard does not specify sufficient requirement to make it works (the IEEE-754 standard is not a requirement of the C++ specification). `int64_t` should be safer and C++ compliant ;) .
Comment: @JérômeRichard Thanks for the information always a pleasure to come to SO and learn from experts in the field :)


all edits and additions must be approved by designated administrators/managers

Question: ive a project in which a user can add record but that can published in the front end end only after it is verified and approved by admin. Please help me with the workflow and php code for this
Comment: i havent tried nything yet just got the project and im analysing whether it can be done or not.Can u help me?
Answer: here is a thought , because this is what i have implemented in my system
we have bunch of data entry guys , they add data . Then asks administrator to review once it done administrator publishes.
i have created one extra field flag and by default flag is 0 for newly entaied data.
once admin review the work he then updates the table setting flag to 1
Comment: i was also thinking the same :-) and there is another functionality in this that All changes to content in the database should be traceable to the user who made the change. means whoever made any changes or gives approval name of that user should be shown
Comment: you are thinking in right way , my db stores created by, create date, updated by , update date
Comment: how can i show the name of the user whoever edit or delete record
Comment: in my system i am storing his/her `login name` in variable and when ever he updates or inserts data i add his name in query
Comment: im new to php can u help me with the query n database table plz.
Comment: I also dont know PHP :D
Comment: I can help you with queries can you let me know which action queries you need? like add, edit, delete, view etc Also provide me the conditions, db table structure. So that i can give the exact working queries..Thanks..
Comment: thanks ill let u know becoz ive just started my project
Answer: That is not a problem. What you need to do is very simple.
First of all

You need to create a table in database which store all the data you want.
You need to create 1 more field which store any variable. Example ('0' or 'pending' as default ). Or you can store by using INSERT sql. What I mean is when you doing your form using php and then you insert the particular field as 0 or pending.

INSERT INTO table ('name', 'contact', 'field')VALUES ('$name','$contact','0/pending')

Create an admin site which use to verify and approve manually for the new user.
Do a transaction which can change the status of the field.
Update the field if admin approve to 1 or approved.

PS* if do not approve just let the field remain 0 or change the value to rejected.

Select the result by calling the specific value which according to the value you set in the field. For example you set the value is approved. Then what you should do is :

SELECT * FROM table WHERE field = approve
This should be what you want.
Comment: thanks sir. ill start working in this project. I might need ur help in future. Is there any way I can contact u in future.
Comment: you no need to worry about that. any question you can ask here when you face bottleneck on your code. stackoverflow have a lot of talents might help you out. but make sure when you ask something, you fulfill stackoverflow terms and conditions. good luck have fun.
Answer: I can give you some raw steps.

Create a flag (column) is_published in you users table.
Set this flag default value as 0
Provide an option in the admin panel with approve or publish button.
On click of the above mentioned buttons run an ajax request update that particular column with value 1.
In the front end part list only those records where is_published = 1 or is_published != 0

Hope this can solve your problem.


Showing that the limit of $g$ does not exist at any point $x_0$ in $\Bbb{R}$

Question: I have tried to show that the following function has no limit at any point in $\Bbb{R}$ but I haven't been able to get it to work out nicely. I would like to use an epsilon-delta argument to show this if it is possible as I feel this is the most fundamental and rigorous way of doing so. If anyone has a nice epsilon-delta argument for the following I would be very grateful:
$$g(x) = \begin{cases}
1 ,&x \in \Bbb{Q}
\\
0 ,&x \notin \Bbb{Q}
\end{cases}$$
Show that the limit of $g$ does not exist at $x_0$, $\forall$ $x_0 \in \Bbb{R}$.
Thanks,
Lauren
EDIT: When I say the epsilon-delta argument I mean the following.
Let $x_0 \in \Bbb{R}$. We say that $g$ has a limit $L$ at $x_0$ if:
$\forall \epsilon \gt 0, \exists$ $\delta \gt 0$ such that $\forall x \in \Bbb{R}$ such that $0 \lt |x - x_0| \lt \delta$ then $|f(x) - L| \lt \epsilon$.
Answer: Suppose that $g(x)$ has limit $l$ for $x \to x_0 \in \mathbb{R}$. This means that

$\forall \epsilon >0$ there exists $\delta >0$ such that  $\forall x \in (x_0-\delta,x_0+\delta)$ we have: $l-\epsilon < g(x)<l+\epsilon$.

Now suppose $l=0$, since in $(x_0-\delta,x_0+\delta)$ there are infinitely many  rational number for which $g(x)=1$, for these numbers  the statement is not verified since $|1-0| > \epsilon$.
If $l \ne 0$ the statement is not verified because in $(x_0-\delta,x_0+\delta)$ there are infinitely many non rational numbers for which $g(x)=0$, so , for these numbers, $|0-l|>\epsilon$.
Comment: Thank you, this is concise, simple and a very nice proof!
Answer: By classic definition of limits in $\mathbb{R}$, you say that $f(x) \xrightarrow{x \to x_0} l$ if for all radius $\varepsilon > 0$ there is a neighbourhood $I$ of $l$ of radius less than $\varepsilon$ such that you can find a neighbourhood $J$ of $x_0$ (radius $\delta$) that makes $f(J) \subseteq I$ true.
But, as I'm sure you know, $\mathbb{Q}$ is dense in $\mathbb{R}$. Therefore it is impossible for a whole neighbourhood of any point to be sent in a set different from $\{ 0, 1\}$: no matter how small the radius, between $x_0$ and $x_0 + \delta$ there are always a $q \in \mathbb{Q}$ and a  $r$ $\in \mathbb{R} \backslash \mathbb{Q}$.
So choosing any $\varepsilon < 1$ proves that, no matter what $x_0$ is, $f$ cannot have a limit in that point.
Answer: Let $x_{0} \in \Bbb{R}$; without loss of generality, let $0 < l := \lim _{x \to x_{0}}g(x) \leq 1.$ If $x_{0} \in \Bbb{Q}$, then there is some $\delta > 0$ such that $0 < |x-x_{0}| < \delta$ implies $|g(x) - l|  < l/2$, implying that $g(x) > l/2$; but this is false (why?). If $x_{0} \in \Bbb{R}\setminus \Bbb{Q}$, then again there is some $\delta > 0$ such that $0 < |x-x_{0}| < \delta$ implies $|g(x) - l| < l/2$, implying that $g(x) > l/2$; but this is false for a likewise reason.
Comment: Are we presuming that the limit at $x_0$ is $g(x_0)$ here? Is this ok to do? Ie. taking $|g(x) - g(x_0)|$ rather than $|g(x) - L|$?
Comment: @LaurenHayes Yes, I mis-read your question. :)


Question about the usage of shared_from_this() in practice

Question: The below code snippet is seen at cppreference.
I am curious about what the intention of Best::getptr() is? When should I use this method in practice? Maybe a simple demo code helps a lot.
struct Best : std::enable_shared_from_this<Best> // note: public inheritance
{
    std::shared_ptr<Best> getptr() {
        return shared_from_this();
    }
    // No public constructor, only a factory function,
    // so there's no way to have getptr return nullptr.
    [[nodiscard]] static std::shared_ptr<Best> create() {
        // Not using std::make_shared<Best> because the c'tor is private.
        return std::shared_ptr<Best>(new Best());
    }
private:
    Best() = default;
};

I wrote a simple code snippet to use shared_from_this(), but I still can't see anything meaningful.
If I want to create a new instance, I just call Foo::Create(). If I have not called Foo::Create, Foo::getPtr() is meaningless (i.e. it could not be invoked at all) since there is no valid instance yet.
If I miss something, please let me know.
#include <iostream>
#include <memory>

class Foo : public std::enable_shared_from_this<Foo> {
private:     //the user should not construct an instance through the constructor below.
    Foo(int num):num_(num) { std::cout << "Foo::Foo\n"; }

public:
    ~Foo() { std::cout << "Foo::~Foo\n"; }

    int DoSth(){std::cout << "hello world" << std::endl; return 0;}

    std::shared_ptr<Foo> getPtr() { return shared_from_this();}

    static std::shared_ptr<Foo> Create() {
        Foo* foo = new Foo(5);
        return std::shared_ptr<Foo>(foo);
    }

private:
    int num_;

};

int main()
{
    auto sp = Foo::Create();
    sp->DoSth();

    Foo& foo = *sp.get();
    auto sp1 = foo.getPtr();
    std::cout << sp.use_count() << std::endl;
}

Comment: https://godbolt.org/z/EE3fr3cve
Comment: when you create an lambda expression, bind a shared_ptr and pass it to others in member function.
Comment: @macomphy Sorry, I can't get your idea. Could you please show me a simple code or a link to make it clear?
Comment: @macomphy It deserves to be treated as an excellent answer! Thank you so much!
Answer: shared_from_this() is intended to be used from within the shared class itself (hence its name), not so much by an external entity, which can always have access to a shared_ptr to that class.
For example, you can have a class Database that hands out Views, which have to keep a back-pointer to the Database:
struct View;

struct Database : enable_shared_from_this<Database>
{
    auto GetView() { return make_unique<View>(shared_from_this()); }
};

struct View
{
    shared_ptr<Database> db;
    View(shared_ptr<Database> db) : db(move(db)) {}
};

Comment: Is there anything wrong with `auto GetView() { return make_unique(shared_from_this()); }`?  `shared_from_this()` invoked in the `Database::GetView()` returns a `std::shared_ptr` ***other than*** `std::shared_ptr`. So it seems wrong to call `make_unique` with parameter whose type is `std::shared_ptr` . How do you think about it?
Comment: Did you miss that the `View` constructor takes a `shared_ptr`?
Comment: I see. Thank you. After I run the [code snippet](https://godbolt.org/z/9cM18v7PG), I almost fully understand what you say. Is it the ***right way*** to use your demo code?
Comment: By using a private constructor in `Database` and a `Create()` function that returns a `share_ptr` you ensure that clients will hold the object in a `shared_ptr`; that sounds about right, but is certainly not the only way to handle the situation.


Construct a std::mem_fn by type

Question: Consider this contrived example:
template<typename Fn, typename Iter, typename T>
void funny_transform(Iter first, Iter last, vector<T>& v)
{
    transform(first, last,
              back_inserter(v),
              Fn());
}

Normally we would pass in the functor object directly, but this example is contrived to simulate the behavior of many STL containers. For example, std::map can take the type of the comparator and default-construct it, instead of taking a comparator object directly. (My actual use case is with some stateless functor class. Since the class is stateless, I can only pass in types.)
The problem is with std::mem_fn. If I try to do something like:
auto get_size = mem_fn(&string::size);

vector<string> v1 = { ... };
vector<string::size_type> v2;

funny_transform<decltype(get_size)>(v1.cbegin(), v1.cend(), v2);  // oops

This won't work, because decltype(get_size) (whatever it is) does not have a default constructor. Though strangely it has copy and move constructors. Switching to a simple lambda won't work either, for exactly the same reason.
Do I have to write my own functor struct to work around this problem? Is there any standard library tool that I can use?
Comment: And the thing `mem_fn` returns is not stateless by definition.
Comment: @T.C. Why is it not stateless? I think a purely stateless functor `struct` can be used to implement it. I could be wrong though.
Comment: Why don't you try writing it?
Comment: "std::map takes the type of the comparator, instead of a comparator object. " Wat?
Comment: @T.C. I should've said "`std::map` can take the type of the comparator and  try to default-construct it."
Comment: @T.C Oh now I see why. Pointers to different member functions can have the same type.
Comment: Even c++14 generic stateless lambda's default ctor gets deleted. It seems you need to define your own functor. This could do http://ideone.com/ErYxeZ
Comment: This is easier in C++17 with `auto` template value arguments.  `my_mem_fun<&string::size>`.
Comment: Also note that it's forbidden to take the address of standard library member functions.
Comment: @T.C. Didn't know that! Can you point me to where the standard says this?
Comment: http://eel.is/c++draft/member.functions#2. Implementations are allowed to mutilate their signatures as they see fit.
Answer: template<class F>
F& magic_store(F* f) {
  static const F r=std::move(*f);
  return r;
}
template<class F>
struct stateless_t{
  stateless_t(F&&f){ magic_store<F>(&f); }
  stateless_t()=default;

  template<class...Args>
  decltype(auto) operator()(Args&&...args)const{
    return magic_store<F>(nullptr)(std::forward<F>(f));
  }
};
template<class F>
stateless_t<F> make_stateless(F f){ return std::move(f); }

Now you can do
auto get_size = make_stateless([](auto&s){return s.size();});

vector<string> v1 = { ... };
vector<string::size_type> v2;

funny_transform<decltype(get_size)>(v1.cbegin(), v1.cend(), v2);  // works

Note that this is a horrible hack, but standard legal.  It is easily abused and easy to screw up non-locally.  Only use it with a lambda that is already stateless.  Never anything else.

In C++17 you could write:
template<auto F>
struct stateless_invoker{
  template<class...Args>
  decltype(auto) operator()(Args&&...args) const{
    return std::invoke( F, std::forward<Args>(args)... );
  }
};

which gives you:
using get_size = stateless_invoker<&std::string::size>;

vector<string> v1 = { ... };
vector<string::size_type> v2;

funny_transform<get_size>(v1.cbegin(), v1.cend(), v2);  // works

which is less hacky.  This can be reproduced in C++11 but is ugly.
Comment: @ziz check out the C++17 version, which is not nearly as evil.
Comment: Hmm the C++17 version looks much more reasonable. Can any type bind to the `auto F`? Does it have the same constraints as current non-type template parameters?
Comment: @ziz it is sort of like `template` but just `template`.  If it has further quirks, I would not know, I have not used it yet.  ;)
Comment: Do you know if there is a compiler able to actually compile this? I tried gcc 6.1.0 with `-std=c++1z` but gcc couldn't recognize the syntax.
Comment: @ziz nope.  See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0127r1.html for the (accepted to feature complete draft) proposal I based the C++17 version off of.
Comment: Thanks! I've added some update to the question based on your idea; can you take a look to see if there are any caveats?
Comment: @ziz write answers as answers, do not edit it into the question.
Comment: I think it should be possible to check if it is stateless via `std::is_empty`, `std::is_pointer` and `std::is_member_pointer`
Answer: Based on @Yakk 's idea, a simple workaround in C++14 can look something like this:
template<typename PMF, PMF pmf>
struct my_mem_fn_t {
    template<typename... Args>
    constexpr decltype(auto) operator()(Args&&... args) const
        noexcept(noexcept(std::mem_fn(pmf)(std::forward<Args>(args)...)))
    {
        return std::mem_fn(pmf)(std::forward<Args>(args)...);
    }
};

// well, if you really want to use macro
#define my_mem_fn(pmf) my_mem_fn_t<decltype(pmf), pmf>()

Then:
auto get_size = my_mem_fn_t<decltype(&string::size), &string::size>;
//          or: my_mem_fn(&string::size);

string s("hello");
cout << decltype(get_size)()(s) << '\n';  // prints 5

Construct a std::mem_fn by type


Lista retorna apenas ultimo elemento

Question: Eae galera, depois de muito trabalho consegui pegar várias tags de um xml para jogar em um relatório Excel. Porém quando populo a lista com meu objeto e passo para o relatório apenas o ultimo elemento é listado. Ja fiz alguns System.out.println para mostrar os elementos da lista mas da na mesma, apenas o ultimo elemento aparece. Alguem pode ajudar? Fiz outra lista para testar com o nome de beneficiariosLote e nao tirei ela ainda, com ela eu consigo listar tudo quando faço um FOR, porém não sei como passar uma lista que não tem vinculo com um objeto para o Excel. Obrigado

RESUMO:
Quero pegar os dados da lista e preencher um Objeto chamado UnimedLote;
minha listaLote retorna o ultimo elemento todo repedido, ou seja, acho que os valores da minha lista estão sendo substituidos a cada iteração do FOR que pega os dados das tags no xml. Quando faço um debug line com SysOut pegando o Objeto contato ele lista normal:

Porém quando eu tento exibir a lista é isso que aparece:

Essa imagem mostra apenas um trecho pois a lista é extensa, porém o mesmo cliente é repetido por toda a lista.
public List<UnimedLote> realizaLeituraXML(String arquivoXML) throws ParserConfigurationException, SAXException, IOException {
    //fazer o parse do arquivo e criar o documento XML
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(arquivoXML);

    Element elem = doc.getDocumentElement();
    NodeList tagdadosLote = elem.getElementsByTagName("unimed:dadosLote");
    List<UnimedLote> listaLote = new ArrayList<>();
    System.out.printf("\n tagdadosLote %s ", tagdadosLote.getLength());
    UnimedLote contato = new UnimedLote();
    for (int i = 0; i < tagdadosLote.getLength(); i++) {
        String lote = "";

        Element elementoLote = (Element) tagdadosLote.item(i);
        lote = pegaTag(elementoLote, "unimed:numeroLote");
        contato.setNumLote(lote);

        NodeList tagGuia = (NodeList) elementoLote.getElementsByTagName("unimed:guia");
        // Como sabemos pela estrutura que só tem 1 elemento não necessitamos de um for podendo fixar o indice.
        NodeList tagdadosGuia = ((Element) tagGuia.item(0)).getElementsByTagName("unimed:dadosGuia");
        System.err.println("LOTE: " + lote);
        for (int y = 0; y < tagdadosGuia.getLength(); y++) {
            NodeList tagProcedimentos = ((Element) tagdadosGuia.item(y)).getElementsByTagName("unimed:procedimentos");
            NodeList tagBeneficiario0 = ((Element) tagdadosGuia.item(y)).getElementsByTagName("unimed:beneficiario");
            NodeList tagProcedimentos1 = ((Element) tagProcedimentos.item(0)).getElementsByTagName("unimed:dadosProcedimento");
            NodeList tagProcedimento = ((Element) tagProcedimentos1.item(0)).getElementsByTagName("unimed:procedimento");
            /*Beneficiarios*/
            Element elementoBeneficiario0 = (Element) tagBeneficiario0.item(0);
            beneficiariosLote.add(pegaTag(elementoBeneficiario0, "unimed:numeroCarteira"));
            contato.setCodigo(pegaTag(elementoBeneficiario0, "unimed:numeroCarteira"));

            Element elementoBeneficiario = (Element) tagBeneficiario0.item(0);
            beneficiariosLote.add(pegaTag(elementoBeneficiario, "unimed:nomeBeneficiario"));
            contato.setNomeBeneficiario(pegaTag(elementoBeneficiario, "unimed:nomeBeneficiario"));
            /*Beneficiarios*/

            /*Dados das Guias*/
            Element elementoBeneficiario1 = (Element) tagdadosGuia.item(y);
            beneficiariosLote.add(pegaTag(elementoBeneficiario1, "unimed:numeroGuiaOperadora"));
            contato.setNumeroDocumento(pegaTag(elementoBeneficiario1, "unimed:numeroGuiaOperadora"));

            Element elementoBeneficiario2 = (Element) tagdadosGuia.item(y);
            beneficiariosLote.add(pegaTag(elementoBeneficiario2, "unimed:valorProcessadoGuia"));
            Element elementoBeneficiario3 = (Element) tagdadosGuia.item(y);
            beneficiariosLote.add(pegaTag(elementoBeneficiario3, "unimed:valorLiberadoGuia"));
            /*Dados das Guias*/

            /*Procedimentos*/
            Element elementoBeneficiario4 = (Element) tagProcedimentos1.item(0);
            beneficiariosLote.add(pegaTag(elementoBeneficiario4, "unimed:valorProcessado"));
            Element elementoBeneficiario5 = (Element) tagProcedimentos1.item(0);
            beneficiariosLote.add(pegaTag(elementoBeneficiario5, "unimed:dataProcedimento"));
            contato.setData(pegaTag(elementoBeneficiario5, "unimed:dataProcedimento"));

            //Procedimento(tag que fica dentro de procedimentos)!!
            Element elementoBeneficiario6 = (Element) tagProcedimento.item(0);
            beneficiariosLote.add(pegaTag(elementoBeneficiario6, "unimed:codigo"));
            contato.setProcedimento(pegaTag(elementoBeneficiario6, "unimed:codigo"));

            Element elementoBeneficiario7 = (Element) tagProcedimento.item(0);
            beneficiariosLote.add(pegaTag(elementoBeneficiario7, "unimed:descricao"));
            contato.setDescricaoProced(pegaTag(elementoBeneficiario7, "unimed:descricao"));
            //Procedimento(tag que fica dentro de procedimentos)!!

            Element elementoBeneficiario8 = (Element) tagProcedimentos1.item(0);
            beneficiariosLote.add(pegaTag(elementoBeneficiario8, "unimed:quantidadeExecutada"));
            contato.setQtdExecutada(pegaTag(elementoBeneficiario8, "unimed:quantidadeExecutada"));

            Element elementoBeneficiario9 = (Element) tagProcedimentos1.item(0);

            String filme = pegaTag(elementoBeneficiario9, "unimed:valorFilme");
            if (filme
                    == null) {
                filme = "0";
            }
            BigDecimal vp = new BigDecimal(filme).setScale(2);
            contato.setCustoFilme(vp);
            beneficiariosLote.add(vp);

            Element elementoBeneficiario10 = (Element) tagProcedimentos1.item(0);
            String custo = pegaTag(elementoBeneficiario10, "unimed:valorServico");
            if (custo
                    == null) {
                custo = "0";
            }
            BigDecimal vc = new BigDecimal(custo).setScale(2);
            contato.setCustoServico(vc);
            beneficiariosLote.add(vc);

            Element elementoBeneficiario11 = (Element) tagProcedimentos1.item(0);

            String hon = pegaTag(elementoBeneficiario11, "unimed:valorHonorario");
            if (hon
                    == null) {
                hon = "0";
            }
            BigDecimal vh = new BigDecimal(hon).setScale(2);
            contato.setHonorario(vh);
            beneficiariosLote.add(vh);
            //Dados Guias
            Element elementoBeneficiario12 = (Element) tagdadosGuia.item(y);

            String tot = pegaTag(elementoBeneficiario12, "unimed:valorInformado");
            if (tot
                    == null) {
                tot = "0";
            }
            BigDecimal vt = new BigDecimal(tot).setScale(2);
            contato.setValorTotalInformado(vt);
            beneficiariosLote.add(vt);
            /*Dados das Guias*/
            System.err.println("Contatos: " + contato);
            listaLote.add(y,contato);

        }

    }

    System.err.println("Lista lote: "+listaLote);
    return listaLote;

}

Trecho aonde preencho as colunas no Excel:
 //Percorre a lista e preenche as celulas
        int cont2=0;
        for (int i = 0; i < listaContatoslt.size(); i++) {

            XSSFRow cabRowAnalitico7 = sheet1.createRow(i + 6);
            for (int x = 0; x <= 12; x++) {
                cabRowAnalitico7.createCell(x);
            }

            XSSFCell cellAnaA5 = cabRowAnalitico7.getCell(0);
            cellAnaA5.setCellStyle(estiloS2);
            cellAnaA5.setCellValue(listaContatoslt.get(i).getNumeroDocumento());

            XSSFCell cellAnaB5 = cabRowAnalitico7.getCell(1);
            cellAnaB5.setCellStyle(estiloS2);
            cellAnaB5.setCellValue(listaContatoslt.get(i).getCodigo());

            XSSFCell cellAnaC5 = cabRowAnalitico7.getCell(2);
            cellAnaC5.setCellStyle(estiloS2);
            cellAnaC5.setCellValue(listaContatoslt.get(i).getNomeBeneficiario());
            cont2++;
            }

Comment: Qual objeto?, qual parte, especifica  limitando ao máximo, ou seria preciso muito tempo para tentar ajudar você
Comment: Editei a pergunta, não sei se ficou melhor mas está difícil até pra explicar
Answer: O erro está em você criar um único objeto do tipo UnimedLote antes do for
Esta linha
UnimedLote contato = new UnimedLote();

Deve ficar dentro do seu for.
Comment: Poxa viajei legal, muito obrigado por me ajudar.

Lista retorna apenas ultimo elemento


Adding Values to Header Drupal 7 + .htaccess

Question: Well, I'm new to Drupal, I was wondering if I can add values to header?
Note: this is the code for my mysite.com
I got this piece of code in mysite.com
<?php

    if (isset($_GET['code'])) {
    echo $_GET['code'];
    }

?>

So for example,
Drupal URL:
mysite.com/U2pVnWK
If I run that URL, it redirects me to PAGE 404
So I tried this, URL:
mysite.com/?code=U2pVnWK
Which it eventually works.
So I tried using RewriteRule thing in .htaccess and added this code
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)$ index.php?code=$1

Even change it into
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)$ ?code=$1

Also
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)$ http://www.mysite.com/?code=$1

None of it works.
Plus I don't know where to put that piece of code in existing .htaccess that Drupal made it.
So to simplify, what I want is:
If I access mysite.com/U2pVnWK I would be able to retrieve and echo U2pVnWK instead of redirecting to PAGE 404 PAGE
Any help would be appreciated. :)
Answer: There is something you can try with Drupal itself.
hook_url_inbound_alter

Alters inbound URL requests.

The idea is to drupal_lookup_path and if nothing found you can do whatever with $path variable in  hook_url_inbound_alter


Generate Variables from Causal Structure for Simulation with Binary/Indicators

Question: I would like to generate a data set of variables from a specific causal structural (a stylized world) for simulation, similar to this answer, but most of the key variables are binary/indicators.
Specifically, my example wants to capture the relationship between example variables surgery, clinical variables, and patient race from the following structural model:

I will then use the created data to demonstrate confounding applied to this setting. However, each variable, except 'appropriate', is an indicator variable i.e. 1 if the true and 0 if false, which makes setting up the systems of equations tricky to make into simple linear combinations.
How should I create these equations?
To show an attempt in R, I tried to create the structural equations for this stylized world as such:
Black <- rbinom(reps, 1, .5)
U <- rnorm(reps)
Appropriate <- (1/(1+exp(-U)))
ST_Elev <- as.numeric(2*Black + 3*Appropriate + rnorm(reps) > 2)
LowSES <- as.numeric(3*Black+rnorm(reps)>2.5)
Surgery <- as.numeric(5*Appropriate+LowSES+2*ST_Elev+rnorm(reps) > 3.5)

world <- data.frame(Black ,
                    ST_Elev,
                    LowSES,
                    Appropriate,
                    Surgery)

by just creating indicators with the right proportions based on being greater than some value. But this formation doesn't nicely follow with my (limited) understanding of structural causal models and I can't figure out how to control the relationships as well directly.
Answer:
How should I create these equations?

There is no single right way to simulate the equations: any parametric form you choose for the structural equations is a valid model, as long as they satisfy the assumptions encoded in the DAG: exclusions restrictions of what is a direct cause of what, and independence restrictions of which error terms are independent. In fact, your approach is fine, you are simulating the structural equations with the latent variable representation of probits.

and I can't figure out how to control the relationships as well
  directly

Since all variables are binary, you can also easily simulate a linear probability model or a logit.   Here I will show an example using a linear probability model, maybe it will be easier for you to control exactly the conditional probabilities you want in the simulation:
reps <- 1e3
Black <- rbinom(reps, 1, .5)
Appropriate <- rbinom(reps, 1, .5)
ST_Elev <- rbinom(reps, 1, .1 + .1*Black + .2*Appropriate)
LowSES <- rbinom(reps, 1, .15+ .3*Black)
Surgery <- rbinom(reps, 1, .2*Appropriate + .1*LowSES + .2*ST_Elev)


Cisco ASA 8.2 ACL For NAT

Question: Sadly I have gone back in time to ASA 8.2(5)33 which I am not so familiar with. I have configured NAT between two interfaces but traffic isn't passing becasue I can't get the ACL to work;
(The full config which isn't very big is here but to keep this post tidy I have just pasted the important parts below);
interface Ethernet0/0
 switchport access vlan 108
!
interface Ethernet0/6
 switchport access vlan 104
!
interface Ethernet0/7
 switchport access vlan 105
!
interface Vlan104
 description BUILDING2
 nameif BUILDING2
 security-level 0
 ip address 10.104.0.1 255.255.255.0
!
interface Vlan105
 description BUILDING1
 nameif BUILDING1
 security-level 0
 ip address 10.105.0.1 255.255.255.0
!
interface Vlan108
 description Main LAN VLAN
 nameif lan
 security-level 0
 ip address 172.22.0.215 255.255.255.0
!
object-group network obj_net_Remote_Hosts
 network-object host 188.8.131.52
 network-object host 220.127.116.11
object-group network obj_host_pc1_eth1
 network-object host 10.104.0.111
object-group network obj_host_pc2_eth1
 network-object host 10.104.0.112
object-group network obj_host_pc3_eth1
 network-object host 10.104.0.106
object-group network obj_host_pc4_eth1
 network-object host 10.104.0.107
object-group network obj_net_PCs
 description IPs of PCs
 group-object obj_host_pc1_eth1
 group-object obj_host_pc2_eth1
 group-object obj_host_pc3_eth1
 group-object obj_host_pc4_eth1
access-list acl_NAT_pc1_91 extended permit tcp host 10.104.0.111 host 126.96.36.199 eq 8101
access-list acl_Permit_PCs extended permit tcp object-group obj_net_PCs object-group obj_net_Remote_Hosts eq 8101
!
global (BUILDING1) 11 126.96.36.199 netmask 255.255.255.255
nat (BUILDING2) 11 access-list acl_NAT_pc1_91
access-group acl_Permit_PCs in interface BUILDING2
route BUILDING1 188.8.131.52 255.255.255.255 10.105.0.2 1
route BUILDING1 184.108.40.206 255.255.255.255 10.105.0.2 1

When I try and connect from PC1 to ip 18.104.22.168 I see the following error logged on the ASA console;
%ASA-2-106001: Inbound TCP connection denied from 10.104.0.111/38495 to 220.127.116.11/8101 flags SYN  on interface blades

What the duce!
Answer: You need to enable same-security-traffic.
same-security-traffic enable inter-interface

Or change one of the interfaces security level
Comment: Oh man, that seems obvious now, either way I needed anothers opinion, thank you :)

Cisco ASA 8.2 ACL For NAT