How ValidatesOnExceptions works

Question: based on this example
http://msdn.microsoft.com/en-us/library/system.windows.data.binding.validatesonexceptions.aspx
It seems that ValidatesOnExceptions is responsable to catch the custom exception and add it to Validation.Errors collection.
The problem is I have the same behavior even with ValidatesOnExceptions set to false
Could someone explain what I am missing?
Thanks
Answer: ValidatesOnExceptions is for custom exception display. But if you have TextBox bound to an int property then before the binding happen a conversion occurs which could cause "red border".
To try it out
<TextBox Text="{Binding IntField}"/>
<TextBox Text="{Binding StringField, ValidatesOnExceptions=False}"/>
<TextBox Text="{Binding StringField, ValidatesOnExceptions=True}"/>

public int IntField { get; set; }

private string stringField;
public string StringField
{
    get { return stringField; }
    set
    {
        throw new Exception();  text = stringField; }
    }
}

Type a number to each textbox:

Red border because of data conversion
No red border becauseValidatesOnExceptions false
Red border because ValidatesOnExceptions
true

I hope it helped.
Comment: Yes what you wrote works, I don't understand, but I was doing pretty the same and I was always getting red border, the only different was I use the textbox in a DataGridTemplateColumn. Probably I did something wrong, thanks
Answer: It depends on the Version of the Framework you use.
In .NET 4 and 4.5 the answer of nemesv is correct. Still it differs form the MSDN documentation.
In .NET 3.5 when ValidatesOnExceptions was introduced, there was no default "red boarder" when the the DataConversion failed. If you want to have that you needed to explicitly add the ExceptionValidationRule into the ValidationRules of the Binding. The MSDN (3.5-4.5) says that you could simply set ValidatiosOnExceptions to true if you want a shorthand for that.
The problem is that the documentation in the version 4.0 and 4.5 is just wrong as it refers to the  behaviour of the .NET 3.5

How ValidatesOnExceptions works


Find an example of an even function such that $f'(0) = 0$ and $f(0)$ is not a local maximum / minimum of the function

Question:
Find an example of an even function such that $f'(0) = 0$ and $f(0)$
  is not a local maximum / minimum of the function

I have been thinking about it for a while and I do not think that such function does exist. If we want the function not to have an extremum at $x = 0$, then the only possibility we are left with is that $(0,f(0))$ is an inflection point of the function. It means that the $n$th derivative ($n$ - odd) does not equal zero and all of its previous derivatives do. But then, since $n$ is odd, then the function, too, is odd - and so this does not work.
Can you think of an example of such function?
Answer: $$ f(x) = x^2 \sin \left( \frac{1}{x^2} \right)  $$
when $x \neq 0,$ also $f(0) = 0$
Comment: @Aemilius given your comment, I think you ought to write down, **very carefully,** a proof that this thing is continuous,  has a derivative at the origin, and the derivative at the origin does turn out to be zero.
Comment: @Aemilius Set $f(0)=0$ then $|f(x)-f(0)|\le x^2\to 0$ and $|f'(0)|=\lim |f(x)/x|\le x\to 0$ so $f'(0)=0$.
Comment: How did you come to this solution? You simply guessed it or used some kind of an alogithm to find it?
Comment: @Aemilius Well it's part of the famously known counter examples in mathematics. Also notice that if $f$ is $C^2$ then $f''(-x)=f''(x)$ and locally $f(x)=f(0)+x^2f''(0)/2$ is a local extrema, thus a counter example would exploit $f''$ does not exists.
Comment: One more question - why can we use this function? It is not differentiable at $x = 0$ because it is not continuous, thus why are we allowed to use its derivative at $x=0$?
Comment: @zwim, no, the function is bounded between $x^2$ and $- x^2$ at the origin. The value at the origin is $0.$ We then write the fraction that defines the derivative at the origin...
Comment: There are plenty of examples which are $C^2$ - such as $f(x) = x^{8} \sin(1/x^2)$.  It just happens in those cases that $f''(0) = 0$.
Comment: In fact, I think $e^{-1/x^2} \sin(1/x^2)$ would even be $C^\infty$.
Comment: @WillJagy
The problem is that I do not feel comfortable with taking the derivative of a function at point $x = 0$ which is not differentiable at $x = 0$, since it is not continuous at $0$. I still cannot comprehend why we can do this..


Filesystem walking iterator from the terminal

Question: Is there a simple bash executable program (SIMPLE!!! in the repositories!) to iterate the file tree in a specified order (infix, prefix, postfix whatever) while executing a function on the files?
Specifically i want to make a large list of the names of files on the root of one or more dirs. I could do this on java easy (in fact i already have the function) but i want to do it in bash for some reason).
I'm not interested in code. I want to use a program that takes care of that for me.
Answer: You could use
find . -type f -exec cat {} \;

...to find all files -type f in the current directory ., and show the contents of each one cat {} \;.
Or you could use tree if you want a tree-like output (this one is not installed by default though).
Comment: @Adrian. Yeah, those are sensible concerns, but easily overcome using sound aliases, i.e.: 'xargs -r -d"\n"' (I call that xargsn). That's it, 1) by default don't run empty commands and 2) feed by lines, not by words (granted, \n is also valid in filenames, but not really used in practice).
Comment: Generally, you're better off using the "+" suffix for the -exec command rather than ";" because it runs the command far fewer times with a large argument list instead of running it once per file with a single argument.  When you're dealing with 10,000s or 100,000s of files, this can make quite a difference in execution speed.  (Some old, non-GNU find commands don't support "+")
Comment: @Adrian Pronk: wow, thanks, did not know about that :) Do I still need to escape that?
Comment: No, the `+` does not need to be escaped.
Comment: I somehow prefer to separate the find process from the execution, so I usually end up using find+xargs.
Comment: @tokland: Then you have to worry about file names containing spaces and whether your command should run if no files are found. Spaces can be dealt with using find ... -print0 | xargs -0 ... and not running the command for an empty list with xargs -r
Comment: @tokland: +1: I hadn't seen the -d"\n" option to xargs before. I like it!


Azure : Custom VM blocked on "provisioning" state

Question: I created a custom Windows 10 VM on my local PC, converted it in .VHD format then uploaded it on Azure using Azure Powershell.
After that, I created an image using the VHD file on Azure cloud, but when I try to launch a VM created with my custom vm's image on Azure, it stays in "starting (provisioning)" state and I can't reach the remote VM by RDP or ping the VM's IP...
How can I solve this problem?
I have no firewall and no Antivirus.
Comment: Instead of creating VM from the image, can you try create an OS disk from the VHD you uploaded and then create VM from the OS disk.
Comment: Thanks for your comment. I'll try this today!
Answer: Without technical support which is the important option here, you should start from the VM access troubleshooting guidance. Likely, your VM was stucked because of the misconfiguration. I encountered that issue because of one of three reasons:
1) There was a problem in the datacenter (that happens). To re-deploy the custom VM is the solution. Maybe even in the different region.
2) I made a mistake when i did the sysprep and my server launched after i did my sysprep. So, check if that is the issue. The server should be sysprepped again.
3) Awhile ago, i tried to do that with the domain controller and it did not work. As i understood, it was not suppose to work.
4) UPD. As i missed that you are trying to deploy Windows 10, please be aware that client VM in the cloud is tricky. As i see in the Images Gallery, there is only one Windows 10 Enterprise version. See that article - it is already supported, but it seems like you should check if you are uploading the Enterprise version.
Please check if you did everything exactly as official documentation states.
Comment: I tried to redeploy the custom VM 3 times, I tried to re-create a custom VM with correct Sysprep options so... I don't know what to do...
Comment: @Rhood check my upd answer. Do you try to upload Windows 10 Enterprise or different edition?
Comment: It's a Windows 10 Enterprise.
Comment: Hm. Is it fixed VHD? Do you see anything in the troubleshooting console on the VM dashboard?
Answer: If the state is "starting (provisioning)", it means the VM has been created and started on the Hyper-V host, but the guest agent has not notified the health state to the platform yet.  At this moment, the platform (or fabric controller) does not know if the guest OS receives DHCP offer and finishes boot process or not.  Ensure DHCP is turned on, firewall is configured properly, VHD is sysprepped, better to have guest agent installed (optional), then follow the official document and retry the deployment (stop and start VM on the portal).  If it still does not work, file a support ticket.  You may read How Does VM Boot Up in Azure to understand the technical details.


Isolated points of the closure of a set

Question: Let $X$ be a topological space and $A \subset X$. By $cl(A)$, I mean the closure of $A$.
I know that any isolated point of $cl(A)$ is an isolated point of $A$. A proof is here : https://proofwiki.org/wiki/Isolated_Point_of_Closure_is_Isolated_Point_of_Subspace
Now, is it also true that any isolated point of $A$ is an isolated point of $cl(A)$ ? If not in general, is it at least true in some spaces $X$ ?
Answer: It's true in all $T_1$ spaces. Suppose that $x$ is an isolated point of $A$; then there is an open $U\subseteq X$ such that $U\cap A=\{x\}$. Let $y\in U\setminus\{x\}$; if $X$ is $T_1$, $U\setminus\{x\}$ is an open nbhd of $y$ disjoint from $A$, so $y\notin\operatorname{cl}A$. Thus, $U\cap\operatorname{cl}A=\{x\}$, and $x$ is still isolated in $\operatorname{cl}A$.
It can fail if $X$ is not $T_1$. Let
$$\tau=\{\varnothing\}\cup\{U\subseteq\Bbb N:0\in U\text{ and }\Bbb N\setminus U\text{ is finite}\}\;.$$
$\tau$ is a $T_0$ topology on $\Bbb N$ that is not $T_1$. Let $A=\{0\}$; certainly $0$ is an isolated point of $A$, but $\operatorname{cl}A=\Bbb N$, and $0$ is not isolated in $\Bbb N$.
Comment: Isn't 0 isolated in $\mathbb{N}$ with this topology since $\{0 \}$ is open ?
Comment: @Vandrin: Yes, and I don't know what I was thinking. I've fixed it now, barring another mental hiccup. Thanks!

Isolated points of the closure of a set


Xamarin - Back stack does not clear

Question: I have a LoginActivity (this is the main activity) and after the user logs in it redirects him to another activity, howerver when he presses the back button it goes back to the login activity. I tried using
this:
StartActivity(typeof(FragmentRendererActivity));
Finish();

and this:
StartActivity(new Intent(this, typeof(FragmentRendererActivity)));
Finish();

and this:
Intent intent = new Intent(this, typeof(FragmentRendererActivity));
intent.AddFlags(ActivityFlags.NoHistory);
StartActivity(intent);

and this:
Intent intent = new Intent(this, typeof(FragmentRendererActivity));
intent.AddFlags(ActivityFlags.ClearTop);
intent.AddFlags(ActivityFlags.ClearTask);
intent.AddFlags(ActivityFlags.NewTask);
StartActivity(intent);
Finish();

and this:
<application android:label="Homecheck" android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar">
    <activity android:name="LoginActivity" android:noHistory="true" android:launchMode="singleTask" />
</application>

...but nothing worked. The closest thing I was able to get to was that I log in, I go to Activity B, I press the back button and the app exits(Paused state). I resume it and, you guessed it, I'm back at the Login Activity.
So is there a way to properly get rid of an activity in the back stack ?
P.S. Maybe the problem is that my LoginActivity is the main activity ?
Answer: If Login is your MainActivity, everytime the app closes and opens it will be the first screen the users sees. Use a splash screen that can check if your user has already been authenticated and redirect them from there. The behaviour you talk about is the expected behaviour
Adding NoHistory = true in the attributes ontop of the class will remove it from the back stack
 [Activity(Label = "SampleApp", NoHistory = true)]
public class LoginActivty : Activity
{
}

Also another thing you can try and clear top activity
        var intent = new Intent(this, typeof(SampleActivity));
        intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.ClearTask | ActivityFlags.NewTask);

        StartActivity(intent);

        Finish();

Comment: Since I am using the Trial version of Xamarin I cannot make my own Splash screen. When I get a license next month I am going to try it and if it works, I will mark it as an answer. Thank you.
Comment: @Milen if this helped you fix your problem please accept the answer
Comment: A Splash screen is a standard activity. it behaves like a normal screen. its just the first one that is loaded when the application loads first. you would have [Activity(Label = "Spash", MainActivity=true)]
Comment: The trial version of Xamarin has its own Splash screen that says "This build only works for 24 hours" When I tried adding my own splash it threw an error that another splash screen already exists. Can't remove it while it's in Trial.

Xamarin - Back stack does not clear


Why is L2 regression good for handling multicollinearity?

Question: Looking for an intuitive explanation, thanks.
Comment: Possible duplicate of [Why is ridge regression called "ridge", why is it needed, and what happens when $\lambda$ goes to infinity?](https://stats.stackexchange.com/questions/151304/why-is-ridge-regression-called-ridge-why-is-it-needed-and-what-happens-when) or https://stats.stackexchange.com/questions/118712/why-does-ridge-estimate-become-better-than-ols-by-adding-a-constant-to-the-diago/119708#119708
Answer: take the case of two perfectly correlated independent variables, x1 and x2
then the corresponding coefficients w1, w2 can go to +/- infty (by adjusting the other appropriately), and we have an infinite number of solutions.
adding L2 regularisation, means that of all these solutions (with same mean square error), there is a best solution - namely the one with smallest l2 norm.  assuming we normalise the variables, this will have w1=w2, ie we take the average x1 and x2.


Does the scale of C major with a half-sharp D have a name?

Question:
I am using the same melody for 2 instruments and I half-sharpened every D note on the second one, and left the first as C major. Does this have a name? Or is it used anywhere?
Comment: @Tim it looks to me like the standard half-sharp sign (like a normal sharp sign but with only one vertical stroke) that was cropped from a full staff, so it retains short segments of the horizontal staff lines.  Those should have been removed.
Comment: That sign is unusual. Is it supposed to mean a *semitone* sharp, or *half a semitone* (*quarter tone*) sharp? Can understand D# being played, but not the other.
Comment: It's a cropped standard half-sharp. First instrument is piano, and second is violin where micro tonalities are easy to play
Answer: A scale that comes close to it is mentioned as a vertex tetrachord but it is not exactly a D with 50 cents but a D+33 cents and an additional A+33 cents: https://mizzan.de/archive/src/scala/vertex_diat2.html. If you are interested in microtonality you could drill here https://mizzan.de/scales
Comment: Both, expressed in cents: 0, 233, 400, 500, 700, 933, 1100. However, you are free to do whatever you like and choose the material that fits best your musical ideas!
Comment: Should it be D half-sharp or both D and A half-sharp?
Answer: I don't think there's a particular context for it... it seems like a scale base on microtonal ancient music.
Anyway, you could find something like that in ethnic music, Indian music in particular where you can find hundreds of scales like that.
You can hear a lot of this also in Arabic music.


Change class from not to print, to print by check box in HTML

Question: I have a lot of checkboxes and I only want to print the ones thats been checked. I've tried toggle, it works until I check it.
Here is my code:
CSS

HTML

 <input type="checkbox"id="coms">  \\Private\Computer Services

    <select class="form-control">
        <option >READ</option>
        <option >EDIT</option>
    </select>

JS
$("#coms").click(function(){
    $(".dontprint").toggle();
});

Comment: how do you click anything when nothing is displayed, your css sets the display to none
Answer: Don't toggle the visibility of the class, toggle the class itself.
$("#coms").click(function() {
    $(this).parent().toggleClass("dontprint", !$(this).is(":checked"));
});

And to avoid having to repeat this code for each ID, you should give the checkboxes a class as well, instead of binding the click handler to a specific ID. Or if they're all inside a particular DIV, you can do:

$("#container :checkbox").click(function() {
  $(this).closest(".hidable").toggleClass("dontprint", !this.checked);
});
.dontprint {
  background-color: grey;
}
@media print {
  .dontprint {
    display: none;
  }
}

        <input type="checkbox" id="coms1">Checkbox 1

      <select class="form-control">
        <option>READ</option>
        <option>EDIT</option>
      </select>

        <input type="checkbox" id="coms2">Checkbox 2

      <select class="form-control">
        <option>READ</option>
        <option>EDIT</option>
      </select>

        <input type="checkbox" id="coms3">Checkbox 3

      <select class="form-control">
        <option>READ</option>
        <option>EDIT</option>
      </select>

I don't know how to make Stack Snippets open in a separate window so you can print it, but I made a jsfiddle, and it shows that the media query works.
http://jsfiddle.net/barmar/tyufjv6g/show/
Comment: And since he has _"a lot of checkboxes"_, he also needs to make sure to use a class instead of an ID, something like `$('.coms')`.
Comment: I get this error when I check the checkbox: Error: Syntax error, unrecognized expression: unsupported pseudo: clicked

throw new Error( "Syntax error, unrecognized expression: " + msg );
Comment: Sorry, `:clicked` should have been `:checked`.
Comment: `.parent()` was wrong, I didn't realize how deeply nested the checkbox was. I've changed it to use a `hidable` class on the DIV that should be hidden during printing, and use `.closest()` to find it.
Comment: Great example but for some reason, it will not work for me. But this does the trick: `$(".dontprint").css("display", "block");` . Now only if i could get it to toggle when I uncheck :)
Comment: That will change the  CSS for non-printing, too. My solution works for me, see the jsfiddle.


Convexity of $f(x,y)=\frac{x}{y^2}$

Question: I would like to ask the convexity of function
$$f(x,y)=\frac{x}{y^2}$$
where $x\geqslant0, y>0$.
Since $f(x,y)$ is differentiable but not twice differentiable, I used the first order condition and have
$$\frac{x_2(y_1^2-y_2^2)}{y_1^2y_2^2}-\frac{2x_1(y_1-y_2)}{y_1^3}$$
Assume $x_1>>x_2, y_1>y_2$, the expression above is less than 0.
So is it correct that $f(x,y)$ is nonconvex?
Thank you.
Dylan
Comment: You should note that your function **is** twice differentiable on $\{(x,y) \in \mathbb{R}^2 : y > 0 \}$.
Comment: Thank you for your notice.
Answer: If you look at the characterizations of convexity in the following link, at page 11 ( http://www2.econ.iastate.edu/classes/econ500/hallam/documents/Convex_Opt_000.pdf ) you will see that convexity requires
$$
\frac{\partial^2 f(x,y)}{\partial x^2}\frac{\partial^2 f(x,y)}{\partial y^2} - \left(\frac{\partial^2 f(x,y)}{\partial x\partial y}\right)^2\geq 0.
$$
However, we have
$$
\frac{\partial^2 f(x,y)}{\partial x^2} = 0,
$$
and
$$
\left(\frac{\partial^2 f(x,y)}{\partial x\partial y}\right)^2 = \frac{2^2}{(y^3)^2}>0.
$$
Thus, we can never satisfy the first inequality, hence the function cannot be convex.
Comment: Is the requirement a necessary condition or sufficient and necessary condition?
Comment: For twice differentiable functions, it is both necessary and sufficient.
Comment: But the function $f(x,y)=\frac{x}{y^2}$ is not twice differentiable.

Convexity of $f(x,y)=\frac{x}{y^2}$


Terminology for constructing a vector or matrix of the coefficients of an expression

Question: I have a vector of variables as follows:
$$
X=
\begin{bmatrix}
    x_{1} & x_{2}  & \dots  & x_{n}
\end{bmatrix}
$$
I also have a function $f$ which returns a linear combination of the values of $X$. For example, it might return:
$$
f(X) = 22.6 x_{1} + 3.2 x_{2} + \dots + 14.4 x_{n}
$$
(The function also takes other parameters to determine those coefficients, which aren't relevant here.)
What I need is the correct mathematical terminology or notation for constructing a vector from $f$ which contains the coefficients of the corresponding values of $X$. For example, if
$$
f(X) = c_{1} x_{1} + c_{2} x_{2} + \dots + c_{n} x_{n}
$$
then I am looking for the correct way to describe how to get $C$ where:
$$
C=\begin{bmatrix}
    c_{1} & c_{2} & \dots & c_{n}
\end{bmatrix}
$$
How can I express this? For example, simple division such as
$$C=\frac{f(X)}{X}$$
obviously isn't right, but is vaguely close to what I'm trying to do. Perhaps there's some operation I don't know about. I don't necessarily expect there to be a way to represent this symbolically, but a sentence would also be fine. For example, "Let C represent the coefficients of something something something." I'm having trouble with the wording and making it any more concise than what I have above.

Edit
This is the best I have so far, based on @Cat's suggestion:
Let $\hat{e}_i$ denote a vector with a 1 in the $i$th coordinate and 0's elsewhere. Then
$$C = \begin{bmatrix} c_{1} & c_{2} & \dots & c_{n}  \end{bmatrix} \text{ where }c_i=f(\hat{e}_i)
$$
Answer: Look at what happens when you plug in the points
$$X_1 = \begin{bmatrix}1 & 0 & 0 & \dots & 0 \end{bmatrix},$$
$$X_2 = \begin{bmatrix}0 & 1 & 0 & \dots & 0 \end{bmatrix},$$
$$
\vdots
$$
$$X_n = \begin{bmatrix}0 & 0 & 0 & \dots & 1 \end{bmatrix},$$
Comment: It is exactly right what your wrote, and that is a perfectly fine way to phrase it.
Comment: I have added an edit with the best language I could come up with, based on this idea. Do you see any better way to phrase it?
Answer: Just define a map $[c_1x_1, ... , c_nx_n] \mapsto [c_1,...,c_n]$ and this will be okay for your purposes by the sounds of it.
Comment: This could definitely work, and is simple, although I'm having trouble writing it concisely. I would prefer to avoid having to write an explanation using both the "f(X) = c1x1 + ... cnxn" line (to define where those ci values came from) as well as this one to define the map.


Get camera matrix from object transformation matrix

Question: I have a transformation matrix of 3d object in world space with a fixed camera position. I would like to derive the camera matrix (position, look up vector, right vector) if the object wasn't transformed and the camera was transformed instead. How could I compute that? Hope my question makes sense
Comment: Rotating and then translating the object is done by `M1= T*R`. Transforming the camera instead is done with `M2`. The point is that if the object is seen from the camera *same for both options* that means `M1=M2`
Comment: This [lookAt link](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluLookAt.xml) shows how to derive the matrix when the camera is transformed.
Comment: see [Understanding 4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) so for example you just translate behind the object |(relative to it) and do an inverse to obtain the camera matrix with follow mode ... but of coarse you have to take your conventions ...
Answer: Premise. Let's represent coordinate transformations with 4x4 matrices. Specifically, the 4x4 matrix Qab representing the coordinate transform of frame a from frame b is such that:

Its 3x3 upper-left submatrix is the rotation matrix Rab, i.e. the 3x3 orthonormal matrix whose columns are the ordinately the components of the x_b, y_b, z_b unit vectors of frame b, decomposed in frame a.
Its 3x1 upper-right submatrix is the translation vector t_ab from the origin of frame a to the origin of frame b, decomposed in frame a.
Its 4th row is [0, 0, 0, 1].
If p is a point whose coordinates in frame b are p_b= [px_b, py_b, pz_b, 1], then the coordinates p_a= [px_a, py_a, pz_a, 1] of the same point in frame in frame a are given by p_a.T=Qab * p_b.T, where x.T means the transposed of vector x. Note that we append a 1 as a dummy fourth coordinate in order to be able to multiply 3D points by 4x4 matrices.

Now, to your question. Let Qcw be the 4x4 matrix representing the rotation and translation of the camera from the world reference frame, and Qow the  analogous transformation of the object from the world.
Then your answer is the camera-from-object transform Qco. We can compute it by noting that we can go from frame o to frame c by first going from o to w, and then from w to c. Therefore it is Qco=Qcw * Qwo, where Qwo=inv(Qow) is the inverse of Qow, and represents the world frame as seen from the object.
Comment: A little bit complicated but I got it. Thanks


Weird MLE Problem based on Dirichlet Function

Question: This question was on a HW in my Statistical Theory class and I find the professor's answer and explanation to be unsatisfactory. Please give me some guidance as to why

$\bar{x}$ is the MLE if this is the case, or
Let me know if I am correct to think that both $\bar{x}$ and $1-\bar{x}$ maximize the likelihood function and therefore the MLE is not unique, or
If the problem is even well defined as it is posed.  I feel like this could be the case also.

I understand the definition of the MLE in more regular circumstances, as detailed in the Principles section of http://en.wikipedia.org/wiki/Maximum_likelihood, but the weird form of the PDF brings up issues with supremums that I am not used to dealing with.
Question: Let $X_1,...,X_n$ be an i.i.d. sequence of 0-1 valued RV's with the probabilities
$$
P(X_1=1)=\begin{cases}
\theta, & \theta\in\mathbb{Q}\\1-\theta, & \theta\notin\mathbb{Q}
\end{cases}
$$
where $\theta\in(0,1)$. Does the MLE of $\theta$ exist?
Outline of Professor's solution: This is the main idea of my professors solution. The likelihood function is
$$
L(\theta|x_1,...,x_n)=\{\theta^{\sum{x_j}}(1-\theta)^{n-\sum{x_j}}\chi_{\theta\in\mathbb{Q}} +\theta^{n-\sum{x_j}}(1-\theta)^{\sum{x_j}}\chi_{\theta\notin\mathbb{Q}}\}
$$
where $\chi_A$ is the indicator function of the set $A$.
We have
$$
\begin{eqnarray}
\underset{\theta\in[0,1]}{\sup} L(\theta|x_1,...,x_n)&=&\underset{\theta\in[0,1]}{\sup} {\{\theta^{\sum{x_j}}(1-\theta)^{n-\sum{x_j}}\chi_{\theta\in\mathbb{Q}} +\theta^{n-\sum{x_j}}(1-\theta)^{\sum{x_j}}\chi_{\theta\notin\mathbb{Q}}\}}
\\&=& \max\{\underset{\theta\in\mathbb{Q}}{\sup} \{\theta^{\sum{x_j}}(1-\theta)^{n-\sum{x_j}}\},\underset{\theta\notin\mathbb{Q}}{\sup} \{\theta^{n-\sum{x_j}}(1-\theta)^{\sum{x_j}}\}\}
\\&=& \max\{\bar{x}^{\sum{x_j}}(1-\bar{x})^{n-\sum{x_j}},(1-\bar{x})^{n-\sum{x_j}}\bar{x}^{\sum{x_j}}\}
\\&=& \bar{x}^{\sum{x_j}}(1-\bar{x})^{n-\sum{x_j}}
\end{eqnarray}
$$
Professor: At this point, the professor argues that the supremum in the second term,$\underset{\theta\notin\mathbb{Q}}{\sup} \{\theta^{n-\sum{x_j}}(1-\theta)^{\sum{x_j}}\}\}$ is not attained since $\bar{x}$ is a rational number.  Since the data consists of rational numbers, the supremum of the first term , $\underset{\theta\in\mathbb{Q}}{\sup} \{\theta^{\sum{x_j}}(1-\theta)^{n-\sum{x_j}}\}$ is attained at $\hat{\theta}_1=\bar{x}$ and that this is the MLE of $\theta$.
Me: It seems like we should consider the supremum over the closure of the sets $\mathbb{Q}$ and $\mathbb{R}\backslash\mathbb{Q}$, which would be $[0,1]$, in which case $\sup L(\theta|x_1,...,x_n)$ is achieved at both $\hat{\theta}_1=\bar{x}$ and $\hat{\theta}_2=1-\bar{x}$. Otherwise, we are essentially assuming that $\theta$ is rational and ignoring irrational $\theta$.  Is this the case?  If so, is this an undesirable property of the Likelihood Principle in weird cases like this? Is there any plausible situation where issues like this occur? Should I stop worrying about weird problems like this?
As an aside, considering $[0,1]\backslash\mathbb{Q}$ has Lebesgue measure 1 and $[0,1]\cap\mathbb{Q}$ has Lebesgue measure 0, it seems like $\bar{x}$ is a bad estimator, since it is an estimate of $\theta$ if it is in a very small set.  Also, if $\theta\in\mathbb{R}\backslash\mathbb{Q}$, $\hat{\theta}_2=1-\bar{x}$ is consistent, so I can't think of a good reason why $\bar{x}$ is better.
Edit As @cardinal pointed out, the $x_i$ are obviously rational, so this is not an issue. This addressed my first (silly) misunderstanding, which involved assuming the estimator $\bar{x}$ could be irrational or rational.
Comment: The $X_i \in \{0,1\}$, so, irrespective of everything else, $\bar X$ is certainly rational!
Comment: There is a good book that I think has the solution to this particular problem. It's called The Cauchy-Schwartz Master Class. I cannot recall their solution off of the top of my head.
Answer: I am not a professor of Statistics, and so most probably my approach will be naive.
Since we want to consider cases where the parameter characterizing the distribution belongs to disjoint sets of numbers, it follows that they will be different numbers. So the notion "the MLE of $\theta$" appears to be wrong or at least confusing/misleading, since it will not be the same $\theta$... Perhaps a more transparent definition of the probability mass function would be
$$P(X_1=1)=p=\begin{cases}
\theta_1, & p\in\mathbb{Q}\\1-\theta_2, & p\notin\mathbb{Q}
\end{cases}$$
with $0< \theta_1\neq \theta_2<1$.
In the one scenario where $p$ cannot be rational, while at the same time the MLE can only take rational values, we are effectively forcing the range of the estimator to have no common elements with the parameter space where $p$ lives.  But of course, this is a special case of "empty intersection", since the values that the MLE takes are "very near-by" the values $p$ can take, and this is why the MLE is a consistent estimator of $p$, a property that is probabilistic and does not require the deterministic limit of the MLE to converge to the true $p$. Note that the MLE cannot be strongly consistent, since the event $\{\hat p_{MLE} = p\}$ is impossible by construction. So it only converges in probability, not almost surely.
Now, if $p$ could be any real number in the interval $(0,1)$, then the likelihood $L(p|\mathbf x)$, as a function of $p$ would be maximized at $p=\bar x$ (second-order conditions for a maximum are also satisfied). This means that
$$ L(p=\bar x) > L(p\neq \bar x)$$
In other words, when $p$ cannot be rational, then the MLE $\hat p = \bar x$ makes the likelihood acquire a value that is higher than any value it can take for any non-rational value of $p$, and for any conceivable sample from a $\{0,1\}$ random variable. By continuity, I believe that this means that $\hat p= \bar x$ gives us the supremum of the function "Likelihood given $p$ is not rational" (although it is not its maximum, since the function "Likelihood given $p$ is not rational" is not defined for a rational number).
So, if the OP accurately conveyed the argument of the professor, ("in the $p$- cannot-be-rational case, the supremum is not attained since $\bar x$ is a rational number"), it appears that I have just laid down an argument to the exact opposite effect.
When the likelihood is the function "Likelihood given $p$ is rational", then the sample mean is the argmax of this function, and so it gives us its supremum=maximum also.
So in both cases, we are good in obtaining the maximum likelihood estimator of $p$ as the sample mean of the data.
Then, if it is revealed to us that we are in the scenario "$p$ is rational", we have already obtained the MLE of $\theta_1$. If it is revealed to us that we are in the scenario "$p$ is not rational" then, by using the invariance property of the MLE we have $\hat \theta_2 = 1-\hat p_{MLE} = 1-\bar x$.
Comment: Thanks for looking at this! First, I think it is important that in the definition of the PMF that $\theta_1 = \theta_2$. That way, the supremum for $p\in \mathbb{Q}$ is $\bar{x}$, the supremum for $p\in \mathbb{R} \ \ \mathbb{Q}$ is $1 - \bar{x}$, and the suprema are equal. Then the only difference is whether the supremum over the set is attained, which occurs for rational $p$, but not irrational $p$. Since we have no knowledge of what set $p$ is in, we look at both suprema, see that it is only obtained for rational $p$, ignore irrational $p$, and set $\hat{p}=\bar{x}$.
Comment: Basically, the problem becomes an uninteresting exercise in analysis because we have to restrict the domain of the estimator to $\mathbb{Q}$, even though "most" parameter values are in $\mathbb{R} \\\mathbb{Q}$.  It is about as interesting as saying the recursion $a_{n+1} = \frac{1}{a_n} + \frac{a_n}{2}$ has no limit over $\mathbb{Q}$, even though it converges to $\sqrt(2)$ over $\mathbb{R}$.  The main lesson is that probabilists should not teach statistics.
Comment: I agree with your comments, although I still cannot understand how we are allowed to state that the two parameters are equal while at the same time they belong to disjoint sets.


ccnet and unit testing

Question: is it possible for ccnet to say that the build has failed in the ccnet tray and the web site if a unit test fails for the project?
Anyone know of a tutorial for this?
Comment: You might wish to try TeamCity - this is also a build server, its just much easier to configure. I believe small installations are free.
Comment: look at http://www.cruisecontrolnet.org/projects/ccnet/wiki/Scenarios that should get you started
Answer: Of course ! And it is the goal for continuous integration !
Just add a task to run your unit test after compilation. It depends heavily on the programming language you use, but you can consider running the EXE for unit tests.
So to summarize, sample task order :

Get source from version control
Compile source
Compile tests
Run tests
Report

You ask for a tutorial, I can give you an example :
<code>  <!-- SVN implementation -->
  <sourcecontrol type="svn">
    <trunkUrl>http://dephicodetodoc.svn.sourceforge.net/svnroot/dephicodetodoc/trunk/DelphiCodeToDoc/</trunkUrl>
    <workingDirectory>$(WorkingBaseDir)\DelphiCodeToDoc</workingDirectory>
  </sourcecontrol>

  <!-- Build tasks to implement -->
  <tasks>
    <!-- Compile main application -->
    <msbuild>
      <executable>$(MSBuildPath)\MSBuild.exe</executable>
      <workingDirectory>$(WorkingBaseDir)\DelphiCodeToDoc\Source</workingDirectory>
      <projectFile>DelphiCodeToDoc.dproj</projectFile>
      <buildArgs>/target:Build /p:Config=Debug</buildArgs>
    </msbuild>

    <!-- Compile tests -->
    <msbuild>
      <executable>$(MSBuildPath)\MSBuild.exe</executable>
      <workingDirectory>$(WorkingBaseDir)\DelphiCodeToDoc\Test</workingDirectory>
      <projectFile>DelphiCodeToDoc_Tests.dproj</projectFile>
      <buildArgs>/target:Build /p:Config=XmlOutput</buildArgs>
    </msbuild>

    <!-- Execute unit tests -->
    <exec>
      <executable>$(WorkingBaseDir)\DelphiCodeToDoc\Exe\DelphiCodeToDoc_Tests.exe</executable>
      <baseDirectory>$(WorkingBaseDir)\DelphiCodeToDoc\Exe\</baseDirectory>
    </exec>
</code>
Comment: Why the 2 **negative** votes ? You should really explain instead of only throwing down vote !
Answer: This is the good tutorial how to set up CCNET with NUnit:
http://ilmatte.wordpress.com/2008/06/01/cruisecontrolnet-tutorial-part-1/
Continuous integration allows to do even more interesting things. For example check code style with StyleCop, look for obvious bugs with FxCop, track dependencies with NDepend, automatically deploy application on staging server for manual testing, perform acceptance testing through UI, do performance testing etc

ccnet and unit testing


Is there actually an "undertone series"?

Question: We know there is an overtone series, or harmonic series, i.e. if a note A of 440 Hz is being played on a musical instrument, there will also be different amplitudes of sound of 880 Hz (440 Hz * 2), 1320 Hz (440 Hz * 3) etc. emitted at the same time.
But will there be a series of undertones (i.e. 220 Hz, 110 Hz...) emitted too?
I'm asking this question because:

I can sometimes hear a note which is an octave lower than what's actually being played on a piano. This can be very significant if this note is in a chord, which can even make me think it's an inversion;
In a discussion of the mechanism of the new Taylor V-Class bracing, an explanation which was claimed to be made by Andy Powers, the designer of the bracing, seems to believe the existence of undertones;
However, the concept "undertone" is rarely mentioned elsewhere. The wikipedia page believes undertone series can only be produced with unconventional methods.
Comment: What you hear can be misleading as the ear is NON LINEAR, and there is a phenomenon called fundamental tracking that can cause people to think they hear notes lower than those being played.  There are so many psycho-acoustical phenomenon at play that you cannot rely on your ears to determine the physics of a musical instrument.  I see a lot of incorrect and illogical responses to this.  Please see my answer for more on this.  If you disagree please provide evidence (not belief).  At least from the POV of linear response it is not possible.
Comment: I also disagree with the conclusion Tim has drawn and have done the same experiment with slightly different techniques and can demonstrate fairly well that no true sub harmonics are created by sympathetic resonance in an acoustic guitar.
Comment: That does NOT mean it can never happen but as far as I am aware it is a non-linear effect that is weak at best or not present relative to the noise floor of equipment I have used.
Answer: The answer is 'yes,'  with a big "if".  If there is something in the instrument which is capable of vibrating at some simple multiple of the tone's wavelength, then it'll happen.  As Tim's test showed, playing a note on a piano will produce a little bit of sound at sub8va and other subharmonics.
In general, if you play, say a 440-A,  the 110, 220, 880, 1760 A-strings will vibrate in sympathy.  Now, the higher pitched strings don't support 440, so they will "dump" energy into their own fundamental.  However, the lower-pitched strings do support 440 (as the 2nd, 4th, etc harmonics), so their principle resonance will be at 440.  Due to mathematical magic :-), there will be some energy at each string's fundamental, but that's a weak secondary effect.
BTW, lower-brass players can, with a little embouchure hacking, produce subharmonics.  It's a matter of forcing a quarter-wave stability on a tube which prefers to be stable at half-wave.
Comment: My test was on a guitar. Tried it on my studio piano, but vertical strings made it impossible. Must get another grand...
Comment: This is not true.  Unless I've misunderstood what you are trying yo say there is an extreme flaw in the logic.  The fact that lower pitch strings vibrate when a higher pitch string is played in not proof that the lower frequency is present!  Only that the longer strings are actually vibrating at the higher harmonic.  What proof do you have that the fundamental is present?  In physical reality, not by the trick of fundamental tracking which I've read that John Petrucci uses.
Comment: Your example of the brass player is a red herring. It isn't the case that a true sub harmonic was generated. It means that different physics was excited in the system. The double open and single open tube models are very simple and cannot completely describe the horn. With all the bends in the material it makes sense that for some excitation a different set of harmonics is generated. But this is still a harmonic relation based on linear theory. Just a different one that was assumed.
Answer: You have some good answers already but here's a test that I just performed on my upright acoustic piano.
I pressed A3 (A below middle C) gently so that the damper was lifted but no sound was made.  If you cannot manage this then play as softly as you can and wait for the note to end.  Assuming that my piano is tuned well, the fundamental of this string is 220Hz.
I stuck A2 (octave below) hard and then released it.  So, this is 110Hz.  The A3 string resonated afterwards but at its normal frequency 220Hz.  To check that the note is coming from this string, let go of the key and you will find that the note stops.
Now, I stuck A4 (octave above) similarly.  So, 440Hz.  Again, the A3 string resonated but not at its normal frequency; it resonated at the frequency of the higher A4 key.
It also works two octaves away e.g. A1 and A5 but in the case of A1 the resonance from A3 is very brief.  Again, A1 caused A3 to resonate at its normal frequency but A5 caused it to resonate at A5's frequency.
I also tried a twelveth away which is approximately the third harmonic.  So, D2 and E5 with similar results to the two octave case.  So, the match does not need to mathematically exact since assuming that my piano is tuned normally, these twelveths won't be exactly on the third harmonic.
However, even though I can induce the resonance with notes above or below, this can all be explained by overtones without undertones.  My experiment does not disprove undertones but suggests that, if they exist, they are much weaker than overtones.
Finally, note that if you really wanted to do this experiment scientifically then you should not trust your ear as the measuring device.  It is quite conceivable that the ear may falsely detect 110Hz from a 220Hz stimulus.  Tim's piece of paper on the guitar string test is more objective but just proves that resonance can occur above or below and does not tell us the frequency of that resonance.  You would need some good lab equipment to be sure.
Even more finally, harmonics are not always so simple.  Look up stretch tuning for pianos and why most drums don't have a distinct pitch.
Comment: Thanks for this experiment.  The sympathetic resonance occurs at the *overtone* of a string with a lower fundamental.  And you're right: resonance doesn't require an exact match to the driving frequency, just a somewhat close one.  You'll find if you repeat your experiment for example holding down a C major chord (C4-E4-G4) and strike C2 very loudly, the whole C chord will resonate a bit, including the E4 that will be tuned ~14 cents off from the harmonic of the C2.
Comment: In fact, I did exactly that a little while ago.  See this older question of mine: https://music.stackexchange.com/questions/95570/notation-for-depressing-piano-keys-without-sounding-them.
Comment: @badjohn, did you capture wave files and look at a spectrum?  Do you know what is present in the physics rather than based on assumption or hearing?  Just curious.  I am concerned about the misinformation present in this Q and the A's.  Yours is the only one that attempts to really explain the situation.
Comment: @ggcg I have considered trying that but my day job is computers so, when not working, I tend to avoid computers as toys.  This is why I like to play a real piano rather than an electronic one.  It is less like work.  When I retire, I may attempt projects such as this.
Comment: @ggcg My answer was based on theoretical understanding and hearing.  I think that I have been modest in my claims e.g. I don't claim to have disproved undertones and I warn that ears are not a reliable scientific measuring device.  Nonetheless, I think that my ears are good enough to be able to state that undertones are a minor feature at most.
Comment: I agree.  To be clear I think your answer is a better one.    Based on a theoretical understanding of linear systems the under tones couldn't be there.  And with all due respect no one's ear is good enough.  The ear is nonlinear and coupled with psycho acoustic effects cannot be trusted to measure the physics of the instrument.
Comment: @ggcg No disagreement on the ear at all.  However, this is a music group rather than a physics group.  So, what we hear is paramount.  If we heard an undertone but we could prove that it was not really present, it would not matter. If the OP was interested, he / she could post a related question to the physics group.
Comment: That doesn't mean we should accept or propagate false information.
Comment: Again, I agree.  However, if some process created a false impression of an undertone then I think that it would be valid to use it in music.  Similarly, many of Escher's drawing are valid art despite being impossible.
Answer: The "test" described is being used incorrectly to support a potentially false claim.  In linear system you will NEVER excite an undertone.  This is simply not possible.  It could be done by some non-linear coupling that cause sub harmonics to be generated.  In Tim's experiment there is a false conclusion being drawn from the fact that plucking the high e string caused the low e string to vibrate.  Of course it will vibrate as Newton's law says that a force applied to the object will cause acceleration and hence motion.
What is really happening is that the high e string's fundamental is a natural harmonic of the low e string.  Hence when the high e string is plucked the low e string vibrates in response to that frequency and the n = 4 harmonic (with n = 1 being the fundamental) is excited.  This does NOT mean that there is a sub harmonic or under tone being generated.  For that to be the case you would need to somehow prove that the lower octaves were excited by this.
EDIT:  I had a mistake in my example.  The high open e string is the n = 4 harmonic of the low E string and the n = 3 harmonic of the A string.  This is not the E on the D string.
This is the case with all sympathetic resonance phenomenon and I think it is grossly misunderstood.  E is also the 5th of the A string and hence equal to the n = 3 harmonic, and n = 6 as well.  If you were to play the high e string you would "SEE" literally the low E and A strings vibrate.  However this does not mean that the note A was produced, in fact it is the E that is the 7th fret harmonic that is excited in the A string.  A is NOT a natural harmonic of E.
When you pluck a string (or excite vibration by some other means) the spectrum of that string is comprised of its harmonics as the motion has to obey the boundary conditions imposed (approximately) by the nut and bridge.  There will NOT be any  lower frequencies in that spectrum other than the fundamental of that string, or fretted note.  Once those frequencies are excited and begin propagating through the instrument they will act as a driving force for the other strings but they will not drive them at the natural frequency of the string, only at the frequency present in the original vibration spectrum.  If any of the pieces of the system have a natural harmonic that matches one of the harmonics in the original spectrum then that will respond on resonance.  It should be noted that everything in the guitar vibrates at whatever driving force is present.  So in a sense the E string will, if forced to, move at the frequency of an A, but it will not "resonate", it will not grow in amplitude on its own (a feature of the resonance phenomenon).
The only way that I can think of to prove that a sub harmonic is present is to capture the sound with a high quality mic (broad band flat response) and DAC and FFT it.  If the sub harmonic is there then its there but that has never been my experience.  As I mentioned in the beginning a non-linear coupling that caused frequency splitting or some other phenomenon could be at work but the linear models of instruments do a pretty good job describing what we see and hear.
Answer: Just done a mini-experiment. On a guitar, when a tiny strip of paper is rested on an open string - let's say the top e, and the bottom E is plucked, the paper gets vibrated off the string. Well-known, as the bottom E produces overtones, one of which is the top e pitch. Worth mentioning that if a bottom F, G etc., is played the paper stays put.
Now, turning the idea on its head, and the paper resting on the bottom, thick E, and plucking the top thin e, that paper falls off!
So yes, undertones must be there - maybe not as strong, as the basic top e is making the bottom E vibrate in sympathy, probably due to the opposite effect.
You can most likely only hear 'undertones' on a piano when the dampers aren't stopping the strings.
Comment: "So yes, undertones must be there" - not quite following the logic there - The top e would be able to create sympathetic resonances in the lower E without having any undertones, because the two strings have harmonics in common.
Comment: I'm not rubbishing the idea of undertones by the way - the linked article seems to be about what happens when a vibrating system with one set of resonant frequencies (a string) is coupled to another system with another set of frequencies (braced instrument body) - which does seem like it could exhibit complex behaviour.
Comment: @topomorto - moot point. Back to the drawing board - or as I call it - a guitar...
Comment: NO!!!!!  This is NOT proof that undertones were created.  Only proof that the low E string vibrated at the overtone that equals the high e string.  I have done the same test but with the addition of touching the low e string at the node with another piece of string and it did NOT vibrate.  This illustrates that the low e string was vibrating at a higher harmonic.  This is really bad from a physics perspective.
Comment: @topoReinstateMonica, no the undertones are not there based on this evidence.
Answer: I mostly agree with Carl Witthoft's answer, though I'd say the procedure suggested there likely falls under what the question described as "unconventional methods."  Basically, if a sound is created by one sounding body, and another body is present with a resonant frequency among its harmonics that matches the first sounding body, then you can get a special effect.  For example, a string may sympathetically vibrate when one of its harmonics is sounding.
Note the first string doesn't respond sympathetically at its own fundamental (which might indicate the presence of true undertones).  Instead, some rather small part of the vibrating energy will "leak" (due to the imperfect nature of real vibrating bodies) into the fundamental mode (as well as other modes/frequencies).  If anything, this effect is further proof of the pervasive nature of the overtone series, which causes this all to happen.  If the second body didn't have overtones at the right frequency, there would be no sympathetic vibration and no leakage to the fundamental.
This could easily be confirmed experimentally with some audio software capable of doing an FFT or even by damping lower vibrations on the second body to see what's really going on.  For example, suppose you created a sound of 300 Hz and put it in the presence of a string with a fundamental frequency of 100 Hz.  The string will resonate at 300 Hz sympathetically.  But assuming the stimulus is strong enough, some energy will eventually leak into the other modes of the string, including 100 Hz.  If this were truly evidence of undertones, you'd only see vibration at 100 Hz.  However, a real-world string will begin to have some limited vibrations (due to similar leakage of energy) at other modes too, e.g., 200 Hz or 400 Hz, which have no relationship to the first string's frequency in terms of overtone or undertone series.  (And no, you can't blame 200 Hz as sympathetically vibrating with the 600 Hz harmonic of the first sound, particularly if a 300 Hz sine wave is your stimulus.)
In sum, a true "undertone series" like the overtone series is not produced naturally by normal harmonic sounding bodies.  Under certain circumstances, some sounding bodies can be manipulated to vibrate at unusual modes (including undertones), and other sympathetic bodies in the area could leak energy to other modes of vibration.
From the question:

I can sometimes hear a note which is an octave lower than what's actually being played on a piano. This can be very significant if this note is in a chord, which can even make me think it's an inversion.

There are a variety of effects that can create that sensation.  Without knowing the exact circumstances, I can't say for certain, but my guess is that what you're hearing in a situation like that is the psychoacoustical phenomenon known as a missing fundamental.
This is a well-known cognitive effect where the brain creates the sensation of a fundamental that matches the harmonics in a stimulus even if that fundamental frequency isn't present in the real sound.  The reason for this is presumably because the brain spends so much time processing harmonic stimuli and condensing all of those various overtone frequencies into a single fundamental "pitch" that we hear.  So, when we hear a bunch of frequencies and no fundamental, the brain inserts it anyway.  (Note that this effect is exploited all the time, e.g., in telephone speakers which are often incapable of producing the fundamental pitches of low male voices.  Instead, the speaker transmits the higher partials, and your brain adds in the fundamental so you perceive the voice at the correct pitch.)
Anyhow, by listening carefully and playing notes of, say, a major triad, you can sometimes have a sensation of hearing the fundamental that would theoretically lie under those pitches, generally an octave or two lower (depending on chord voicing).  This effect becomes much more pronounced when playing the triad in just intonation.  And if played with pure tones (sine waves) in a 4:5:6 frequency ratio, many people would simply hear a fundamental pitch two octaves lower and no individual frequencies at all.  (Piano tuners and other musicians who train themselves to listen to harmonics may not hear this "ghost" effect sometimes, however.)
I should note, in closing, that psychoacoustical phenomena such as the "missing fundamental" effect are likely one of the primary reasons why an "undertone series" was first postulated.  There are many situations where the brain can seem to "hear" lower pitches than are actually present in a stimulus.  Nowadays, with access to audio analysis software, it's easy to verify which frequencies are and aren't present in a given stimulus.
Answer: There are certain techniques on a Violin where if you bow the string with extreme pressure it will vibrate at one-half the fundamental frequency, although the tone produced is so scratchy you would only want use it in very select situations.  The same technique can be used to vibrate the string at one-third and one-quarter of the fundamental frequency, so in this case Sub Harmonics are possible.


direção de abertura do select

Question: Tenho um select que está abrindo seus options acima do mesmo. Creio que seja por conta da quantidade de resultados, tendo em vista que o mesmo vem de uma consulta do banco.
<select class="custom-select" id="insc_cnpj" name="insc_cnpj">
   <option value="" disabled selected>Selecionar CNPJ</option>
   <?php
        $arg = array("select"=>"distinct(insc_cnpj)","tabela"=>"cliente","where"=>"order by insc_cnpj");
        $obj_cnpj = $obj_pdo->getConsultaDI($arg);
        $tl_cnpj = 0;
        if(!empty($obj_cnpj)){
            $tl_cnpj = count($obj_cnpj);
            for($i=0; $tl_cnpj > $i; $i++){
                if(strlen($obj_cnpj[$i]->INSC_CNPJ) > 17){
                    print "<option value='{$obj_cnpj[$i]->INSC_CNPJ}'>{$obj_cnpj[$i]->INSC_CNPJ}</option>";
                }
            }
        }
    ?>
</select>

Gostaria de saber: há como eu definir em que direção o meu select deve abrir?
Answer: É possivel fazer com que o tamanho do select seja alterado assim exibindo os itens (deve-se especificar a quantidade), para isto deve usar o atributo size="", assim por exemplo:

<select name="setor" size="2">
    <option value="a">setor a</option>
    <option value="b">setor b</option>
    <option value="c">setor c</option>
    <option value="d">setor d</option>
</select>

Você não terá problemas com a diversidade de options com isso sempre abrirá pra baixo, caso isso não ajude recomendo que escolha algum plugin do jQuery UI, são muito eficientes e você pode alterar o select por um auto-complete.
Link recomendado: Select-Menu
Auto-complete: Auto-Complete
Exemplo auto-complete:

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Autocomplete - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">

  Tags:
  <input id="tags">

direção de abertura do select


What type of data is needed for offset in a Poisson regression model - R

Question: I am trying to do a Poisson regression using the following data, where infant deaths are shown per year for both North and South England.
ageband agecat midage year deaths population Divide Gender percentage
2965    <01 <01 0.5 1965    5033    214400  North   1   2.3474813
989     <01 <01 0.5 1965    3952    199000  South   1   1.9859296
2984    <01 <01 0.5 1966    4999    210900  North   1   2.3703177
1008    <01 <01 0.5 1966    3850    196900  South   1   1.9553073
3003    <01 <01 0.5 1967    4663    208700  North   1   2.2343076
1027    <01 <01 0.5 1967    3525    194200  South   1   1.8151390
3022    <01 <01 0.5 1968    4603    204400  North   1   2.2519569
1046    <01 <01 0.5 1968    3616    188400  South   1   1.9193206
3041    <01 <01 0.5 1969    4507    204100  North   1   2.2082313

This is what I am running in R: (nsmaleMerge is my data), am I correctly using the offset parameter or should I not be enclosing it in a log function?
poissonM <- glm(deaths~Divide, nsmaleMerge, offset(log(population)), family = poisson(link = "log"))

Deaths is the count variable as seen from the data, and divide (north/south) is the covariate, exposure would be population.
When I try doing 'offset = population' without the log function I get an error about not including start values, but when I do it with the log function as seen above it works fine and this is the output:
    Call:
glm(formula = deaths ~ Divide, family = poisson(link = "log"),
    data = nsmaleMerge, weights = offset(log(population)))

Deviance Residuals:
    Min       1Q   Median       3Q      Max
-108.82   -77.21   -28.82    32.27   203.75

Coefficients:
              Estimate Std. Error z value Pr(>|z|)
(Intercept)  7.5750418  0.0009075    8347   <2e-16 ***
DivideSouth -0.1843193  0.0013457    -137   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 808081  on 103  degrees of freedom
Residual deviance: 789242  on 102  degrees of freedom
AIC: 800662

Number of Fisher Scoring iterations: 5

Do I need to do the log function, or do I have an error in my data when trying to use population by itself under offset?
Answer: With the offset, you are saying that you look at expected infant deaths per member of the population per year with the theoretical possibility of more than one infant death per year per member of the population. Your model for the mean rate of the Poisson distribution is:
$$\log E Y_i = {\bf x_i\beta_i} + \log \text{population},$$
which you can rewrite as
$$\log E (Y_i/\text{population}) = {\bf x_i\beta_i}.$$
You may be able to judge whether that is a sensible model and/or whether another denominator (e.g. I do not even know what population means, is it the total population of all ages or of infants?) or distribution makes more sense. E.g. if "population" is a population of infants, you could also look at it as a binomial outcome (each infant either dies or does not die), or you could try to take into account partial years at risk (by counting total years of infants at risk, in which case a Poisson model makes a lot of sense) etc.
Comment: Thanks for the explanation, yes population is a population of infants for that specific year, I have the data from 1965-2016.

What do you mean by "you could try to take into account partial years at risk (by counting total years of infants at risk"?

The model currently is for all years, are you saying to do e.g. every 10 years? Also do you have any information or references that could help me interpret the output of the model and what it means, thanks.
Comment: What  meant is if some infants only live in the area for half the year (=0.5 years at risk), or die half way through the year (=only 0.5 years at risk) or are born after 3 months (=0.75 years at risk in the year), move away after 3/4 of the year (=0.75 y at risk)  etc. (an infant that is there for the whole year counts as 1y at risk), you can sum up the time at risk and use the logarithm of that as an offset. You may not have the necessary data though. Any good reference that explains generalized linear models would be a good starting point, there are plenty pitched at different audiences.
Answer: It is a programming error.
The fourth argument of glm is weights, not offset. So either use named arguments or add the offset to the formula like + offset(log(population)).


How to implement operator*= for big integers without instantiating new vector?

Question: I'm trying to implement a class for working with big integers (e.g. > 2^64, which are given by their string representation) in C++.
I use 2^32 as the base of the number system, that is, I have a vector that stores digits as integers from 0 to 2^32-1 (uint32_t is used for this). I would like to implement the operator* and the operator*=, in my case the operator* via the operator*=, while I would like to avoid  instantiating new vector (e.g. result) in the implementation of the operator*=. On the Internet, I could only find implementations of the operator*= via the operator* (as this one) or just implementations of the operator* for two big integers, such as this one. How can I do that?
Below is my current implementation of the operator*=:
// BASE = 1 << 32
// num: std::vector<uint32_t> num;
bignum& operator*=(const bignum &bn) {
    std::vector<uint32_t> result(size() + bn.size() + 1, 0);

    for (size_t i = 0; i < size(); ++i) {
        uint64_t carry = 0;

        for (size_t j = 0; j < bn.size(); ++j) {
            // static_cast is used to avoid overflow
            uint64_t tmp = static_cast<uint64_t>(num[i]) * bn.num[j] + result[i + j] + carry;
            result[i + j] = tmp % BASE;
            carry = tmp / BASE;
        }

        size_t index = bn.size();
        while (carry > 0) {
           uint64_t tmp = carry + result[i + index];
           result[i + index] = tmp;
           carry = tmp / BASE;
           ++index;
        }
    }

    num = result;

    return *this;
}

I will be glad to receive any advice. Have a nice day!
Comment: fwiw, implementing the compound operator in terms of the "normal" operator is not good. Typically it is done the other way around. If the operation cannot be done in place then imho better do not provide the compound operator
Comment: what are the elements of `num` ? digits?
Comment: In my case no, but if `BASE` was 10, then `num`, for example for 54 would store digits or in other words `{4, 5}`.
Comment: But it doesn't really matter while speaking about `operator*=`, isn't it?
Comment: of course it matters what you want to multiply, unless you assume the answer is already obvious ;)
Comment: @MuhammadjonHakimov *while I would like to avoid copying* -- Is this a premature optimization on your part?  Have you measured if copying really is expensive?  What exactly are the members of the `bignum` class?
Comment: @idclev463035818, for `145634049368338284`, which binary representation is `1000000101011001010110010101110011110011010010111101101100`, `num` would store last 32 bits and other bits as it's members (`10000001010110010101100101` and `01110011110011010010111101101100`, or in other words `33908069` and `1942826860`, which will be stored as `{1942826860, 33908069}`.
Comment: @PaulMcKenzie, No I didn't really measured. `bignum` has only one private member `num`, which is a vector and constructors from `uint32_t` and `std::string`, implementations of some operators, such as `operator+, operator*, operator+=, operator*=, operator=` and others.
Comment: `num = result` --  If `num` is a vector, then the compiler may already simply do a `std::move`, and no copy is done.  Or you could explicitly do a `num = std::move(result);`
Comment: @PaulMcKenzie, yeah that makes sense, but I want to get rid of `result` too, ideally.
Comment: you cannot do it without additional memory. Try with pen and paper. If you find a solution I'll be glad to be proven wrong ;)
Comment: @MuhammadjonHakimov Also, you should really try to optimize the possible combination of values that could be multiplied.  For example, what if `bignum` is 1?  Why do anything?  What if `bignum` is 0?  Also, what if `bignum` is not really that big?  You could get away with multiplying two "regular" numbers and just creating a `bignum` from the result.  There are a lot of better optimizations you could do, rather than trying to beat the compiler at the optimization game by avoiding copies.
Answer: You ought to implement the operator *= at first as you already did. But you should optimize the code to let it reuse the container num of this to avoid allocation of new memory. Then implementing operator * (that does not change its arguments) would be very simple:
bignum operator*(bignum left, const bignum &right) const {
    return left *= right;
}

Such an approach has 2 advantages:

There is no copy-paste inside * and *= operators.
There is no need to copy the left argument and allocate memory for it if you use *=.


Should I warmup for every small session I play?

Question: I've had this on my mind for a long time and it's regarding whether I should properly warm-up for every small practice sessions I play (30-45 minutes) or if I should just warm-up the on my first session and continue the rest with none.
I know that the difference between playing with warmed up hands and non-warmed up gives very different results, but is this something I should worry about? Could anyone give any advice regarding this issue and what I can do about it?
Personally, I think every practice session, no matter how long or short is important and I'd like to make the most out of it without wasting too much time.
P.S My warm-up routine normally consists of ~10 Hanon excercises (x2)
Comment: Are your playing sessions happening multiple times per day, or on different days? My recommendation would be to do a daily warmup routine before your first session of the day; then consider yourself "warm" for any other sessions you have that day.
Comment: I play multiple session a day (around 3-5 sessions a day).. and by saying that I should "consider" myself "warm" for other sessions after the first meaning I should ignore the fact that I haven't warmed up for **those** sessions?
Comment: You've warmed up for the day. And if you continue playing throughout the day, you're going to stay warm. You "cool off" when you go to sleep for the night, and need to warm up again in the morning. It's a pretty reliable metaphor--think about the engine in a car. You might need to let the engine warm up before it's at peak performance, but if you drive to the grocery store and do some shopping, when you come back, it will not have bled off so much heat in a short amount of time. The difference with piano playing is that you're talking about neural pathways and muscles.
Answer: What about a pre-warm-up, and a pre-pre-warm-up! Unless you're going to be playing frantic tunes, or very complex stretchy chords, etc., then a gentle warm-up will get you into most.There are many other 'exercises' that will warm-up apart from Hanon. Try playing scales/arpeggios/chromatics/block chords up and down the 'board. Try playing some known pieces, faster and slower than usual.Use the 'warm-up' time to actually practise some of the stuff you're learning. As I say, if you're going crazy, then make it a longer warm-up, but don't worry unduly about harming yourself due to insufficient warm-up.
Comment: Thanks! The tips really helped and I decided to play pieces faster/slower and turned out it helped a bit!
Answer: The simplest answer is "just try not doing it, and see what happens". You aren't going to injure yourself permanently by not going through your lengthy warming up procedure for a few days. If you don't discover any downside, you just gained a few more hours in each day, either to practice longer or do something else.
Personally, I would consider spending hours a day on something as mind-numbingly boring as Hanon exercises a complete waste of time. If you really need to spend hours "warming up" before you can start "practising", at least do something that exercises your mind, and not just your fingers.
There are plenty of ways to warm up that don't involve actually playing your instrument - for example a 5 or 10 minute jog around the block might be better for your overall fitness and stamina levels than 30 or 45 minutes of only exercising your fingers.
Comment: Thanks! Well I dont usually spend hours on Hanon but at least run it twice a day in order to keep building finger strenght (I've been playing for 9 months). I'll definitely consider that running around the block thing, I've heard similar advice when it comes to studying and that doing something that makes your body and mind work will help other things in general.


Python urlopen IOError: [Errno socket error] [Errno 10060]

Question: I'm looking to get more information about IOError: [Errno socket error] [Errno 10060] when using urlopen in Python 2.7. I am using my personal 35MB/s Internet connection (no proxy).
I've been opening multiple webpages from various websites using a Python script and randomly get this error message from time to time:
webpage = urlopen('http://www.thewebpage.com')

IOError: [Errno socket error] [Errno 10060] A connection attempt
failed because the connected party did not properly respond after a
period of time, or established connection failed because connected
host has failed to respond

This error appeared after trying to open pages from different websites. Therefore, it doesn't seem to be related exclusively to the opening of pages from one particular website. I also got this error using mechanize.
My questions are :

Is this error related to the fact that I am sending multiple requests to the same server within a short amount of time? Would a time-out reduces the chance of getting this error?
Is there any way to prevent it? Could I use a conditional statement to prevent the script from crashing?

My script takes around an hour to run and having to rerun it due to this error is fairly unpleasant.
Comment: Can you access the website in question through other means? (say, with `curl`?)
Comment: I haven't try `curl`. I am just curious about the fact that for the same URL, I would get this error on a certain day and not on another day. It arises approximately once every 2000 times I open a URL. My script was built for a web scraping purpose.
Comment: @LaGuille Can you please provide exact url ?
Comment: Like mentioned in the question, I've been getting this error message while opening pages from **various** websites. This is not related to a URL in particular. I was just looking to get more info about what might have been causing the error and if there was any way to avoid it since it seems to occur randomly.
Answer: Sending multiple requests to the same server in short succession could very well cause the server not to respond, since your requests might look like a ddos attack.  You can catch the exception with a try-except clause,  and try again.


Erro ao gravar as informações de uma página ASP.NET no banco de dados

Question: Tenho uma aplicação desenvolvida em ASP.NET (VB.NET) com dois RadioButtonList e uma TextBox onde essas informações são retornadas pelo VB e gravadas no banco de dados, ao clicar em gravar o sistema apresenta o erro descrito abaixo e as informações não são inseridas no banco.
Contudo ao realizar o mesmo procedimento em modo de depuração o erro não ocorre.

Invalid postback or callback argument.  Event validation is enabled using  in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Invalid postback or callback argument.  Event validation is enabled using  in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

PesquisaAtendimento.aspx

                    Por favor, atribua uma nota ao atendimento:

                <asp:RadioButtonList ID="rblVotos" runat="server" Style="font-family: Tahoma; font-size: 0.8em; color: #043668;"
                    RepeatDirection="Horizontal">
                    <asp:ListItem>1</asp:ListItem>
                    <asp:ListItem>2</asp:ListItem>
                    <asp:ListItem>3</asp:ListItem>
                    <asp:ListItem>4</asp:ListItem>
                    <asp:ListItem>5</asp:ListItem>
                    <asp:ListItem>6</asp:ListItem>
                    <asp:ListItem>7</asp:ListItem>
                    <asp:ListItem>8</asp:ListItem>
                    <asp:ListItem>9</asp:ListItem>
                    <asp:ListItem>10</asp:ListItem>
                </asp:RadioButtonList>

                    Observações:

                    <asp:TextBox ID="txtObs" runat="server" Columns="65" Rows="2" TextMode="MultiLine"
                        Font-Names="Tahoma" Style="font-family: Tahoma; font-size: 0.8em; color: #043668; border: none;"
                        CssClass="txtObs" Width="590" Height="120"></asp:TextBox>

                    Todos os problemas foram resolvidos?
                    <asp:RadioButtonList ID="rblProblemasResolvidos" runat="server" Style="margin-left: 3px; font-size: 0.8em; font-family: Tahoma; color: #043668;"
                        RepeatDirection="Horizontal">
                        <asp:ListItem Value="S">Sim</asp:ListItem>
                        <asp:ListItem Value="N">Não</asp:ListItem>
                    </asp:RadioButtonList>

                    <asp:Button ID="btnGravar" runat="server" CssClass="btnGravar" Text="Gravar"
                        Height="32px" Width="71px" />
                    <asp:Label ID="lblInformacao" runat="server" ForeColor="blue"></asp:Label>

</form>

Answer: Conheço duas soluções para você:

Desabilitar o EventValidation, colocando o código <%@ Page EnableEventValidation="false" %>  no inicio do seu código (não recomendo, pois perde muito em segurança, mas dependendo do seu caso...)
Utilize o UpdatePanel, você ira encontrar mais informações:
aqui e aqui


FQL console throw "Ops" error

Question: I have this FQL query
<code>select uid, name, website
from user
where website != "" and uid in (
    select uid2
    from friend
    where uid1 = me()
)
</code>
I tested this query in the FQL console a couple of months ago, and it worked fine. Now I try it again and I have this error:

that can be translated in english like "An error occurred. We are working to solve it soon". I tried with different queries and no one work, all throw the same error. Someone have any suggestions?
Answer: Since an unspecified version, Facebook doesn't allow to query some private parameters (like the websites). So my old query is not valid now.

FQL console throw "Ops" error


Contracting elements from two different lists

Question: I have two different lists list1 = ['A','B'] and list2 = ['C','D','E']. I would like to be able to find all possible contractions between the elements of the two lists. For the present case I would like to have a code (preferably Python, Mathematica or MATLAB) that takes the lists above and returns:
AC,BD , AC,BE , AD,BC , AD,BE , AE,BC , AE,BD
which are all the possible contractions. I would like to be able to do this for lists of variable size (but always 2 of them). I've played a lot with Python's itertools but I can't get the hang of how it works with two lists. Any help would be much appreciated.
Comment: You mean all permutations of list1 concatenated with list2 for a total word length of each permutation as 2? EDIT: Second question... are you looking for word 2 being dependent of what was selected in word 1?
Answer: Here is my version:
import itertools

l1 = 'AB'
l2 = 'CDE'

n = min(len(l1),len(l2))
print('; '.join(
    ','.join(a+b for a,b in zip(s1,s2))
    for s1,s2 in itertools.product(
        itertools.permutations(l1,n),
        itertools.combinations(l2,n),
    )
))

This will output:
AC,BD; AC,BE; AD,BE; BC,AD; BC,AE; BD,AE

Note that for shortness, I did not build a list of the items, but directly iterated the strings. It does not matter which of the two lists gets permutations and which gets combinations, that just changes the order of the output. permutations takes all possible orderings, while combinations returns sorted orderings. This way, you get each contraction exactly once.
For each contraction, you will get two sequences s1 and s2, the contraction is between elements of like index in each sequence. ','.join(a+b for a,b in zip(s1,s2)) makes a nice string for such a contraction.
Comment: Thank you very much. This does it. The mathematica question below doesnt do what I want when I add more elements...
Answer: listA = {"A", "B"};
listB = {"C", "D", "E"};
f[x_, y_] := If[StringMatchQ[StringTake[x, {2}], StringTake[y, {2}]],
                    Sequence @@ {}, List[x, y]];
z = Outer[StringJoin, listA, listB];
Flatten[Outer[f, First@z, Last@z], 1]

Answer: If you're asking about how to build all permutations of the items contained within both lists, with no repetitions, with each result of length two, you could use itertools.permutation:
combined_list = []
for i in list1 + list2:
    if i not in combined_list:
        combined_list.append(i)

for perm in itertools.permutations(combined_list, 2):
    print(perm)

For the inputs list1 = ['a', 'b'] and list2 = ['c', 'd', 'e'], this outputs:
('a', 'b') ('a', 'c') ('a', 'd') ('a', 'e') ('b', 'a') ('b', 'c') ('b', 'd') ('b', 'e') ('c', 'a') ('c', 'b') ('c', 'd') ('c', 'e')  ('d', 'a') ('d', 'b') ('d', 'c') ('d', 'e') ('e', 'a') ('e', 'b') ('e', 'c') ('e', 'd')

Comment: @burnpanck Am I totally confused about the purpose of extend? I could've sworn, based on usage, documentation, and SO answers I've seen, that it concatenated lists.
Comment: It does, but it does so in-place. You would have to write first `list1.extend(list2)` which will add list2's element to list1, but return nothing. Then you can use `list1`.
Comment: @burnpanck Well, that's nowhere near as useful here as +=. Thanks.
Answer: In [2]: list1 = ['A','B']

In [3]: list2 = ['C','D','E']

In [4]: list(itertools.product(list1, list2))
Out[4]: [('A', 'C'), ('A', 'D'), ('A', 'E'), ('B', 'C'), ('B', 'D'), ('B', 'E')]

In [5]: [''.join(p) for p in itertools.product(list1, list2)]
Out[5]: ['AC', 'AD', 'AE', 'BC', 'BD', 'BE']


Strongswan to Cisco ASA with multiple right subnet

Question: I've got an ikev2 tunnel up, initiated on the left from an ubuntu box with strongswan going to a cisco asa. Using ASA to ASA multiple networks work but I can not get it with strongswan.
#config setup
    # strictcrlpolicy=yes
    # uniqueids = no
config setup
    charondebug="all"
    uniqueids=yes
    strictcrlpolicy=yes
# Add connections here.
conn host2
    left=18.104.22.168 #Changed from real for this example
    leftsubnet=172.27.1.0/20
    right=126.96.36.199 # changed
    rightsubnet=192.168.1.0/24,192.168.2.0/24
    ike=aes256-sha512-modp2048s256
    esp=aes256-sha512
    keyingtries=0
    ikelifetime=1h
    lifetime=8h
    dpddelay=30
    dpdtimeout=120
    dpdaction=restart
    authby=secret
    auto=add
    keyexchange=ikev2
    type=tunnel

after connecting:
ip xfm policy
src 192.168.1.0/24 dst 22.214.171.124/32
    dir fwd priority 185664
    tmpl src 126.96.36.199 dst 18.104.22.168
        proto esp reqid 1 mode tunnel
src 192.168.1.0/24 dst 18.104.22.168/32
    dir in priority 185664
    tmpl src 220.127.116.11 dst 188.8.131.52
        proto esp reqid 1 mode tunnel
src 220.127.116.11/32 dst 192.168.1.0/24
    dir out priority 185664
    tmpl src 188.8.131.52 dst 220.127.116.11
        proto esp reqid 1 mode tunnel
src 0.0.0.0/0 dst 0.0.0.0/0
    socket in priority 0
src 0.0.0.0/0 dst 0.0.0.0/0
    socket out priority 0
src 0.0.0.0/0 dst 0.0.0.0/0
    socket in priority 0
src 0.0.0.0/0 dst 0.0.0.0/0
    socket out priority 0
src ::/0 dst ::/0
    socket in priority 0
src ::/0 dst ::/0
    socket out priority 0
src ::/0 dst ::/0
    socket in priority 0
src ::/0 dst ::/0
    socket out priority 0

$ipsec statusall
Status of IKE charon daemon (strongSwan 5.5.1, Linux 4.13.0-37-generic, x86_64):
  uptime: 119 seconds, since Mar 23 09:08:33 2018
  malloc: sbrk 2297856, mmap 532480, used 1224384, free 1073472
  worker threads: 11 of 16 idle, 5/0/0/0 working, job queue: 0/0/0/0, scheduled: 2
  loaded plugins: charon test-vectors unbound ldap pkcs11 aes rc2 sha2 sha1 md4 md5 mgf1 rdrand random nonce x509 revocation constraints acert pubkey pkcs1 pkcs7 pkcs8 pkcs12 pgp dnskey sshkey dnscert ipseckey pem gcrypt af-alg fips-prf gmp chapoly xcbc cmac hmac ctr ccm ntru bliss curl soup mysql sqlite attr kernel-netlink resolve socket-default farp stroke updown eap-identity eap-sim eap-sim-pcsc eap-aka eap-aka-3gpp2 eap-simaka-pseudonym eap-simaka-reauth eap-md5 eap-gtc eap-dynamic eap-radius eap-tls eap-ttls eap-peap eap-tnc xauth-eap xauth-pam xauth-noauth tnc-tnccs tnccs-20 tnccs-11 tnccs-dynamic dhcp whitelist lookip error-notify certexpire led radattr addrblock unity
Listening IP addresses:
  188.8.131.52
  172.27.1.3
Connections:
       host2:  18.104.22.168...22.214.171.124  IKEv2
       host2:   local:  [184.108.40.206] uses pre-shared key authentication
       host2:   remote: [188.8.131.52] uses pre-shared key authentication
       host2:   child:  184.108.40.206/32 === 192.168.1.0/24 192.168.2.0/24
TUNNEL
Security Associations (1 up, 0 connecting):
       host2[1]: ESTABLISHED 115 seconds ago, 220.127.116.11[126.96.36.199]...188.8.131.52[18.104.22.168]
   host2[1]: IKEv2 SPIs: 1f4ce050e3e482ac_i* 35d68fd4c114ef25_r, pre-shared key reauthentication in 2 hours
   host2[1]: IKE proposal: AES_CBC_256/HMAC_SHA2_512_256/PRF_HMAC_SHA2_512/MODP_2048_256
   host2{1}:  INSTALLED, TUNNEL, reqid 1, ESP SPIs: ca77e9b9_i a6993362_o
   host2{1}:  AES_CBC_256/HMAC_SHA1_96, 0 bytes_i, 0 bytes_o, rekeying in 40 minutes
   host2{1}:   18.104.22.168/32 === 192.168.2.0/24

The ASA after successful connection:
asa-fw1# show crypto ipsec sa peer 184.108.40.206 | include ident
  local ident (addr/mask/prot/port): (192.168.2.0/255.255.255.0/0/0)
  remote ident (addr/mask/prot/port): (172.27.1.0/255.255.240.0/0/0)

And a different tunnel between 2 ASAs:
      local ident (addr/mask/prot/port): (10.1.2.0/255.255.255.252/0/0)
      remote ident (addr/mask/prot/port) : (192.168.10.2/255.255.255.255/0/0)
      local ident (addr/mask/prot/port): (192.168.1.0/255.255.255.0/0/0)
      remote ident (addr/mask/prot/port): (192.168.10.2/255.255.255.255/0/0)
      local ident (addr/mask/prot/port): (192.168.2.0/255.255.255.0/0/0)
      remote ident (addr/mask/prot/port): (192.168.10.2/255.255.255.255/0/0)
      local ident (addr/mask/prot/port): (192.168.4.0/255.255.255.0/0/0)
      remote ident (addr/mask/prot/port): (192.168.10.2/255.255.255.255/0/0)

I have also tried putting additional right subnets on their own, such as follows:
conn host2
    left=22.214.171.124 #Changed from real for this example
    leftsubnet=172.27.1.0/20
    right=184.108.40.206 # changed
    rightsubnet=192.168.1.0/24
    ike=aes256-sha512-modp2048s256
    esp=aes256-sha512
    keyingtries=0
    ikelifetime=1h
    lifetime=8h
    dpddelay=30
    dpdtimeout=120
    dpdaction=restart
    authby=secret
    auto=add
    keyexchange=ikev2
    type=tunnel

conn host2-2
    also=host2
    rightsubnet=192.168.2.0/24

after connection:
$ipsec statusall
Status of IKE charon daemon (strongSwan 5.5.1, Linux 4.13.0-37-generic, x86_64):
  uptime: 52 seconds, since Mar 23 09:02:22 2018
  malloc: sbrk 2293760, mmap 532480, used 1203936, free 1089824
  worker threads: 11 of 16 idle, 5/0/0/0 working, job queue: 0/0/0/0,
scheduled: 2
  loaded plugins: charon test-vectors unbound ldap pkcs11 aes rc2 sha2 sha1 md4 md5 mgf1 rdrand random nonce x509 revocation constraints acert pubkey pkcs1 pkcs7 pkcs8 pkcs12 pgp dnskey sshkey dnscert ipseckey pem gcrypt af-alg fips-prf gmp chapoly xcbc cmac hmac ctr ccm ntru bliss curl soup mysql sqlite attr kernel-netlink resolve socket-default farp stroke updown eap-identity eap-sim eap-sim-pcsc eap-aka eap-aka-3gpp2 eap-simaka-pseudonym eap-simaka-reauth eap-md5 eap-gtc eap-dynamic eap-radius eap-tls eap-ttls eap-peap eap-tnc xauth-eap xauth-pam xauth-noauth tnc-tnccs tnccs-20 tnccs-11 tnccs-dynamic dhcp whitelist lookip error-notify certexpire led radattr addrblock unity
Listening IP addresses:
  126.96.36.199
  172.27.1.3
Connections:
       host2:  18.104.22.168...184.108.40.206  IKEv2
       host2:   local:  [220.127.116.11] uses pre-shared key authentication
       host2:   remote: [220.127.116.11] uses pre-shared key authentication
       host2:   child:  22.214.171.124/32 === 192.168.1.0/24 TUNNEL
      host2-2:   child:  18.104.22.168/32 === 192.168.2.0/24 TUNNEL
Security Associations (1 up, 0 connecting):
       host2[1]: ESTABLISHED 50 seconds ago,
184.108.40.206[126.96.36.199]...22.214.171.124[18.104.22.168]
   host2[1]: IKEv2 SPIs: 98cf15596219b433_i* 8177dc2c8f0605d8_r, pre-shared key reauthentication in 2 hours
   host2[1]: IKE proposal: AES_CBC_256/HMAC_SHA2_512_256/PRF_HMAC_SHA2_512/MODP_2048_256
  host2-2{1}:  INSTALLED, TUNNEL, reqid 1, ESP SPIs: ccc8bf25_i 1cec9278_o
  host2-2{1}:  AES_CBC_256/HMAC_SHA1_96, 336 bytes_i (4 pkts, 40s ago), 336 bytes_o (4 pkts, 40s ago), rekeying in 41 minutes
  host2-2{1}:   22.214.171.124/32 === 192.168.2.0/24

and the ip xfrm policy:
src 192.168.2.0/24 dst 184.108.40.206/32
    dir fwd priority 185664
    tmpl src 220.127.116.11 dst 220.127.116.11
        proto esp reqid 1 mode tunnel
src 192.168.2.0/24 dst 220.127.116.11/32
    dir in priority 185664
    tmpl src 220.127.116.11 dst 18.104.22.168
        proto esp reqid 1 mode tunnel
 src 18.104.22.168/32 dst 192.168.2.0/24
    dir out priority 185664
    tmpl src 184.108.40.206 dst 188.8.131.52
        proto esp reqid 1 mode tunnel
src 0.0.0.0/0 dst 0.0.0.0/0
    socket in priority 0
src 0.0.0.0/0 dst 0.0.0.0/0
    socket out priority 0
src 0.0.0.0/0 dst 0.0.0.0/0
    socket in priority 0
src 0.0.0.0/0 dst 0.0.0.0/0
    socket out priority 0
src ::/0 dst ::/0
    socket in priority 0
src ::/0 dst ::/0
    socket out priority 0
src ::/0 dst ::/0
    socket in priority 0
 src ::/0 dst ::/0
    socket out priority 0

When using the above method, I can access 192.168.2.0 but no longer 192.168.1.0
Regardless, the xfrm policy is the same, and i can only access 192.168.2.0/24
I've tried generating traffic to the subnets without success. Can anyone help determine why I can only access ONE remote subnet on the right side?
Comment: The output of `ip xfrm policy` is not complete (or invalid, as there is a mix of remote subnets). The output of `ipsec statusall` is also very incomplete. And where does that 188.8.131.52/32 address come from? (I mean you configured _leftsubnet=10.10.0.0/20_, so that doesn't match at all).
Comment: Ah, i apologize for the confusion. I made mistakes find/replacing IPs to cover up actual IP. I've made corrections and it should all match fine now. 172.27.1.0/20 is the inside network on the LEFT side, 192.168.[1-2].0/24 inside subnet on the right side
Comment: You only have one CHILD_SA established. What happens if you also do `ipsec up host2`? You might also want to try _auto=route_ to trigger CHILD_SAs based on matching traffic.
Answer: The answer is simple, Cisco ASAs don't support multiple traffic selectors per CHILD_SA. So you have to negotiate individual CHILD_SAs for each combination of local and remote subnet you want to tunnel. That is, something like:
conn host2
    ...
    rightsubnet=192.168.1.0/24
    ...

conn host2-2
    also=host2
    rightsubnet=192.168.2.0/24

conn host2-3
    also=host2
    rightsubnet=192.168.3.0/24

Comment: I've tried this previously as well, and the result is the same. after making changes, i can bring only one up, for example, host2-2 then is the only subnet i can access


Conexão Persistente ao banco de dados Mysql

Question: Olá gostaria de saber se eu mantiver uma conexão com banco de dados aberta(conexão persistente) é melhor do que eu fechar sempre após uma consulta?
Answer: Isso depende muito. Várias instâncias (Manter a conexão aberta) podem ser úteis caso haja uma grande demanda contínua, ou caso o controle de acesso ao banco e operações suportadas (insert, alter, delete) seja feito por usuários do banco, ou mesmo se as requisições ao banco forem distribuídas. Em contrapartida, uma única instância, além de ser mais fácil de gerenciar, atende bem uma quantidade razoável de usuários com requisições esporádicas (o que é o caso em muitos sistemas).
Mas você fechar e abrir a conexão com o banco pode ter um custo notável em desempenho, pois os recursos gastos (tempo e processamento) para carregar o driver e acessar o banco diversas vezes pode causar uma lentidão indesejada, ainda mais se muitas requisições consecutivas sejam feitas.
Você poderia fazer um Singleton para solucionar o seu caso. Siga esse guia aqui que é muito bom:
http://www.devmedia.com.br/php-singleton-aplicando-o-padrao-de-projeto-na-pratica/28469


Calculating percentage basics

Question: I am new to this website, so please forgive my mistakes if there is any.
I'm not quite good standing with mathematics and I have trouble finding the solution for this problem:
60% of the number 50 equals 30. I know this from my own logic, but how would I solve the issue mathematically?
Thanks.
Answer: Hint: $60\%$ is the same as $\frac{60}{100}=\frac{6}{10}$.
So you just need to compute the product $$\frac{6}{10}\times 50.$$
Comment: Thank you. This is something I definitely won't ever forget anymore.
Comment: If you want to compute how much $p\%$ is you compute as  above *but* if you want to add $p\%$ to a number $x$, just compute the product $(100+p)/100\times x$ to obtain $x+p/100\times x$.

Calculating percentage basics


C++: Initialization within switch case is not consistently giving out error

Question: I read that initialization within cases of switch is supposed to give compiler error.
But when I tried two different version. One is not giving out the error (Ex1), I can't understand why.
Ex 1:
switch (i)
{
case 1:
    int k;
    break;
case 2:
    int j=3;
    break;
}

Ex2:
switch (i)
{
case 1:
    int k=3;
    break;
case 2:
    int j;
    break;
}

Answer: In a switch block, every declaration that comes before a case is available at that case. The error occurs when there is a case to which the switch can jump to which is located after the initialization of a variable that you could access there. If that case was jumped to, you could use the variable despite its initialization never occuring.
In the first example, there is no case which would skip an initialization. The only initialization is int j=3; which comes after every case so it can't be skipped. k is not initialized so its initialization can't be skipped.
In the second example,if i == 2 then k's initialization would be skipped.
One solution is to define a scope for each case. Then, the variables defined for a case is only accessible from that case and the problem can't occur. For example :
void foo(int i)
{
    switch (i)
    {
    case 1:
    {
        int k=3;
        break;
    }
    case 2:
    {
        // k is not accessible here
        int j;
        break;
    }
    }
}


How to copy dataframe in pandas

Question: I have a python script which does the following:
final_df = main_df
final_df['new_column'] = final_df['b']*0.05

Basically I don't want to disturb main_df dataframe and work on a copy of it. But when run the above script, both final_df and main_df affect in similar ways. Why does this happen?
If I want to achieve the required way, how do I proceed?
Comment: ``final_df = main_df.copy()`` that way main_df is not affected
Comment: `final_df = main_df` *never makes a copy*
Answer: use copy method:
final_df = main_df.copy()

Answer: You have to deep copy it other wise it would be pointing to the same object.
So it would be
final_df = main_df.copy() # by default deep copy is done

Here is the link to the documentation.
Answer: The reason is when you do final_df = main_df, a copy of main_df is not made but just a new reference final_df is created thereby both final_df and main_df referring to the same memory location. So if one is changed like final_df in your case, the changes are also reflected into main_df as both are pointing to same memory location.
Example:
main_df = ['final', 'df']
final_df = main_df
print (f'Location of final_df: {id(final_df)}')
print (f'Location of main_df: {id(main_df)}')

Both the above print statements will print the same memory location.
Here is the nice writeup to understand this behavior.
If you don't want your main_df not to be affected, create a deepcopy of it as below:
final_df = main_df.copy()

with copy() a complete new copy is created and can be verified with below code:
from copy import copy
main_df = ['final', 'df']
final_df = main_df.copy()
print (f'Location of final_df: {id(final_df)}')
print (f'Location of main_df: {id(main_df)}')

Now both the print statements will print 2 different memory locations.

How to copy dataframe in pandas


Multiple ng-app directives on a page

Question: I want to be able to use multiple ng-app="{angular.module}" directives on one page. I want to do this to make the bits of my app more modular. I figure, if I can create angular modules and plug several of them into one document, I could take those modules and plug them into other projects easily.
I have seen people say that you can only use one ng-app directive on your page... is this true? Is it most accurate to say, "one ng-app directive per view"?
I hope this is not the case, or if it is the case that there is still a best way to achieve a high degree of abstract modularity.
Here are my modules/apps and their controllers...
var searchModj = angular.module('searchModule', []);

var controllers = {};

controllers.SearchList = function ($scope){

    $scope.coworkers = [
            {name: 'Joe Bob', city: 'Ukrainia'},
            {name: 'Adam Blobnovski', city: 'Logan' },
            {name: 'Carlos Sanchez', city: 'Deerbushle'},
            {name: 'Martin Kellerweller', city: 'Uptown'},
            {name: 'John Doe',  city: 'New York City'}
        ];
};

searchModj.controller(controllers);

var fruitModj = angular.module('fruiter', []);

controllers.SomeFruit = function ($scope) {

    $scope.fruits = ['apple', 'banana', 'pear'];

};

fruitModj.controller(controllers);

Ok, now here is the relevant part of my markup...

        Name:

        <input type="text" ng-model="name" />

{{ coworker.name }} - {{ coworker.city }}

You are searching for {{ name }}

{{ fruits }}

I think because it comes first in the document, my "searchModule" app works and the second app does not. When I comment out the first app, the second works. So it looks like I'm confirming my most unfortunate suspicions. Regardless... if this is the case, then what is the best approach I can bear in mind to make the functionality on my projects as modular as possible?
Comment: you only want one `ng-app` on a page, but you can insert your other modules as dependencies of the main `ng-app` module
Comment: You require only one `ng-app` in your application. If you want to decide your application module at run time you should use `angular.bootstrap`. Refer http://docs.angularjs.org/guide/bootstrap for details.
Answer: you only want one ng-app on a page, but you can insert your other modules as dependencies of the main ng-app module.
var app=angular.module('myNgAppName', ['searchModule']);

This will expose any directives , controllers etc you have in your 'searchModule'
Comment: This is the correct answer. Ideally, you should break up your app into multiple modules as per your requirements and inject those modules as dependencies into your app.
Answer: The limitations of the ngApp directive is just that, limitations of the directive, not AngularJS itself. Angular allow you to associate modules with multiple elements in a page, it even allows you to associate more than one module with each element.
Referencing other modules from you module will work. Another approach that will work is using the angular.bootstrap() method. See: https://stackoverflow.com/a/18583329/984780
Finally you can create a directive that works like ngApp without it's limitations. It would work exactly the way it does in your markup code. That's what I did you can get the code here:
http://www.simplygoodcode.com/2014/04/angularjs-getting-around-ngapp-limitations-with-ngmodule/
The directive is called ngModule. Here's a code sample:
<!DOCTYPE html>

Module A, B

                {{name}}

                {{name}}

Just Module B

                {{name}}

Comment: another great post luis
Answer: Yes you only want one ng-app per page, but you can create other modules and declare them as dependencies of your main app.
var app=angular.module('appModule'); //resuable module

var app=angular.module('myApp', ['appModule']); //in the HTML ng-app="myApp"

So you can put re-usable stuff in appModule and use it in other projects. For example, I like to put my routing logic (i.e. re-routing users depending on their access levels) in a module that I reuse in other projects.
Note: You might want to look into $provider http://docs.angularjs.org/api/AUTO.$provide if you want to use data from your re-usable ("appModule") module inside the config method of your ng-app ("myApp") module.

Multiple ng-app directives on a page