Decision tree for output prediction

Question: I have satellite data that provides radiance which I use to compute the Flux (using surface and cloud info). Now using a regression method, I can develop a mathematical model directly relating radiance and flux and can be used to predict the flux for new radiance values.
Is it possible to do same using decision trees or regression trees? In a regression there is mathematical equation connecting dependent and independent variable? Using decision trees, how  could you develop such a model?
Comment: Is it possible to refine your question somewhat, @Bijoy? Are you just looking for a tutorial on decision tree methods? Note that such a tutorial would be beyond the scope of CV.
Comment: @Bijoy if you feel sashkello post below answered your question, please mark it as 'accepted' by clicking the green check mark. If not, please add a comment to clarify. Thanks in advance.
Answer: You can do it with decision trees for sure or any other regression model for that matter. Use any of the packages which have this method available (you can find lots of info about how to do it in R or Python or various statistical software programs). They all work exactly the same - have some input x, have some output y, train it mymodel.train(x, y), and you have the model. Do proper cross-validation and you're done. I'm not sure how you are building your regression right now, but I'm sure this is not much different from it.
Comment: This is kind of what i want to know.. in a regression, there is a equation connecting x and y.. Is there a similar mathematical equation in decision trees also.?? if it is there,how can i build it?
Comment: Decision tree is your equation. You can print it out and see it. It will look like a set of conditions. (as @Wake2Sleep duly mentions this is not the best way for continuous variable prediction though). BUT do you really need to see it? If all you need is function connecting two variables all you care is having f(x) and an ability to enter any x in there and find y. All of it can be inside computer, you don't need to understand the equation to use it.
Comment: I.e., most popular method is Random Forest which would be extremely hard to visualize and interpret because of its often huge size. Do you care to see 100 parameters? I wouldn't.
Comment: thanks for giving a more detailed answers. For long i thought that there could exist a mathematical equation in decision trees. now another doubt i am having is regarding using the decision trees in a Random forest. Random forest contain many decision tress.. how do you define so many different decision trees for a dataset eg. for three input and one output ? Do you just change splitting condition at nodes?how exactly do you make random forest of trees?
Comment: mymodel.train(x, y) - this is all you need to do. At least for starters. It is a machine learning method, so let machine do all the job for you. If you want to do it manually, you are to have lots of read on how it works etc. All the standard methods are already encoded in many libraries, don't redo it again, there is no need...
Answer: In algorithmic modeling, as opposed to parametric modeling, there is no explicit equation relating input and output variables (see this paper by Breiman). The assumption is that the phenomenon being modeled is complex and unknown, and rather than imposing a formal model (which comes with a suite of assumptions and limitations), algos directly learn the links between predictors and predictand from the data. In the case of a single tree, this is not so much of an issue because the tree explains its predictions in a very visual and intuitive manner, but with ensemble of trees (Random Forests, Boosting), interpretability is definitely traded off for accuracy.
Comment: Thanks to all contributors for their answers..It was really an educational experience...
Answer: To predict on a continuous variable the decision tree must essentially bin the variable and you are left with terminal nodes each with an average value for your prediction.  Some programs like RapidMiner force you to bin the value yourself before predicting and some do it on the fly, behind the scenes.  But you could always do something like use your tree to score a dataset that contains X values across the full range of X and derive some kind of equation from the prediction results, Y.
But the problem with this is decision trees are not phased by non-linear relationships in your data.  So in your scored dataset you might see several different "strata".
A better solution might be to think of the decision tree as a tool to identify these "linear strata" beforehand and then build a regression model for each, or the most "important", depending on your domain for example.
Hope this helps!
Comment: i already binned the data based on the various  variable bin sizes. for example for x1=10-20, x2=1-5, x3=0.1-0.5, the output mean value of y=1.5. So i have many bins for the input variables and corresponding mean output variable. Basically there is three input variables binned at different size and output value (mean) for each bin. if i build a tree using this binned data, is it possible to extract some type of mathematical relationship between the x1,x2,x3 and y (output)   which can be used for prediction of y using new input varibles?
Comment: Each terminal node (aka leaf) will have a different mean and equation.  You could use something like R's caret() package and just use your tree (i.e. your model) to predict() for your new variables.
Comment: Decision trees does not force you to bin numerical data. After training, decision trees will describe some hypercubes/regions, but this is not binning, since each region is determined during learning, while binning variables is done before learning.

Decision tree for output prediction


Calculating SVD by hand: resolving sign ambiguities in the range vectors.

Question: When calculating the SVD of the matrix
$$A = \begin{bmatrix}3&1&1\\-1&3&1\end{bmatrix}$$
I followed these steps
$$A A^{T} = \begin{bmatrix}3&1&1\\-1&3&1\end{bmatrix} \begin{bmatrix}3&-1\\1&3\\1&1\end{bmatrix} = \begin{bmatrix}11&1\\1&11\end{bmatrix}$$
$$\det(A A^{T} - \lambda I) = (11-\lambda)^{2} - 1 = 0$$
Hence, the eigenvalues are $\lambda_{1} = 12$ and $\lambda_{2} = 10$.
When $\lambda_{1} = 12$:
$$ \begin{bmatrix}11-\lambda_{1}&1\\1&11-\lambda_{1}\end{bmatrix} \begin{bmatrix}x_{1}\\x_{2}\end{bmatrix} = \begin{bmatrix}0\\0\end{bmatrix}$$
$$x_{1} = x_{2} \implies u_{1} = \begin{bmatrix}t\\t\end{bmatrix}$$
And for $\lambda_{2} = 10$:
$$x_{1} = -x_{2} \implies u_{2} = \begin {bmatrix}t\\-t\end{bmatrix}$$
Now
$$U = \begin {bmatrix} u_{1}&u_{2} \end{bmatrix}$$
$u_{1}$ and $u_{2}$ are orthonormal.
So the for $u_{1} = \begin{bmatrix}t\\t\end{bmatrix}$ , $u_{2} = \begin{bmatrix}t\\-t\end{bmatrix}$ I know $\left| t  \right| = \frac{1}{\sqrt{2}}$ and $u_{1}.u_{2}=0$.
My question how can we decide about the sign?
For example I think both $U=  \begin{bmatrix}\frac{1}{\sqrt{2}} &\frac{1}{\sqrt{2}} \\\frac{1}{\sqrt{2}} &\frac{-1}{\sqrt{2}} \end{bmatrix}$ and $U=\begin{bmatrix}\frac{-1}{\sqrt{2}} &\frac{1}{\sqrt{2}} \\\frac{1}{\sqrt{2}} &\frac{1}{\sqrt{2}} \end{bmatrix}$ could be answers. Then Which one should I choose?
======
Update1:
Based on answers posted I rewrite:
$u_{1} = sgn (t_1) \begin{bmatrix} \frac{1}{\sqrt{2}}\\ \frac{1}{\sqrt{2}}\end{bmatrix}$
$u_{2} = sgn (t_2) \begin{bmatrix} \frac{1}{\sqrt{2}}\\ \frac{-1}{\sqrt{2}}\end{bmatrix}$
$$U= \begin{bmatrix} \frac{1}{\sqrt{2}}& \frac{-1}{\sqrt{2}} \\  \frac{1}{\sqrt{2}}& \frac{1}{\sqrt{2}}\end{bmatrix} \begin{bmatrix} \operatorname{sgn} (t_1)&0 \\ 0& \operatorname{sgn} (t_2) \end{bmatrix}$$
======
Update2:
I continued by calculation of $V$ as follow:
$ A^{T} A = \begin{bmatrix}3&-1\\1&\\1&1\end{bmatrix} \begin{bmatrix}3&1&1\\-1&3&1\end{bmatrix}  = \begin{bmatrix}10&0&2\\0&10&4\\2&4&2\end{bmatrix}$
$det( A^{T} A- \lambda I)=0$
$\lambda_{1} = 12 , v_1 =  sgn(t_3) \begin{bmatrix}t_{3}\\ 2t_{3} \\ t_{3} \end{bmatrix}$
$\lambda_{2} = 10 ,  V_{2} = sgn(t_4) \begin{bmatrix}t_{4}\\ -0.5t_{4} \\ 0 \end{bmatrix}$
$\lambda_{3} = 0 ,  V_{3} = sgn(t_5) \begin{bmatrix}t_{5}\\ 2t_{5} \\ -5t_{5} \end{bmatrix}$
$V= \begin{bmatrix}\frac{1}{\sqrt{6}} &\frac{2}{\sqrt{5}} &\frac{1}{\sqrt{30}}\\ \frac{2}{\sqrt{6}}&\frac{-1}{\sqrt{5}}&\frac{2}{\sqrt{30}}\\ \frac{1}{\sqrt{6}}& 0& \frac{-5}{\sqrt{30}}\end{bmatrix}\begin{bmatrix} \operatorname{sgn} (t_3)&0&0 \\ 0& \operatorname{sgn} (t_4)&0\\ 0&0& \operatorname{sgn} (t_5) \end{bmatrix}$
I try to check if all possible answers for U and V are valid :
$A = U\Sigma V^{*}$
 $A = \begin{bmatrix} \frac{1}{\sqrt{2}}& \frac{-1}{\sqrt{2}} \\  \frac{1}{\sqrt{2}}& \frac{1}{\sqrt{2}}\end{bmatrix} \begin{bmatrix} \operatorname{sgn} (t_1)&0 \\ 0& \operatorname{sgn} (t_2) \end{bmatrix} \Sigma  (\begin{bmatrix}\frac{1}{\sqrt{6}} &\frac{2}{\sqrt{5}} &\frac{1}{\sqrt{30}}\\ \frac{2}{\sqrt{6}}&\frac{-1}{\sqrt{5}}&\frac{2}{\sqrt{30}}\\ \frac{1}{\sqrt{6}}& 0& \frac{-5}{\sqrt{30}}\end{bmatrix} \begin{bmatrix} \operatorname{sgn} (t_3)&0&0 \\ 0& \operatorname{sgn} (t_4)&0\\ 0&0& \operatorname{sgn} (t_5) \end{bmatrix} )^{*} $
 $A = \begin{bmatrix} \frac{1}{\sqrt{2}}& \frac{-1}{\sqrt{2}} \\  \frac{1}{\sqrt{2}}& \frac{1}{\sqrt{2}}\end{bmatrix} \begin{bmatrix} \operatorname{sgn} (t_1)&0 \\ 0& \operatorname{sgn} (t_2) \end{bmatrix} \Sigma  \begin{bmatrix} \operatorname{sgn} (t_3)&0&0 \\ 0& \operatorname{sgn} (t_4)&0\\ 0&0& \operatorname{sgn} (t_5) \end{bmatrix} \begin{bmatrix}\frac{1}{\sqrt{6}} &\frac{2}{\sqrt{5}} &\frac{1}{\sqrt{30}}\\ \frac{2}{\sqrt{6}}&\frac{-1}{\sqrt{5}}&\frac{2}{\sqrt{30}}\\ \frac{1}{\sqrt{6}}& 0& \frac{-5}{\sqrt{30}}\end{bmatrix}^{*} $
When I assigned $U=  \begin{bmatrix}\frac{-1}{\sqrt{2}} &\frac{-1}{\sqrt{2}} \\\frac{-1}{\sqrt{2}} &\frac{1}{\sqrt{2}} \end{bmatrix}$
and $V= \begin{bmatrix}\frac{-1}{\sqrt{6}} &\frac{-2}{\sqrt{5}} &\frac{-1}{\sqrt{30}}\\ \frac{-2}{\sqrt{6}}&\frac{1}{\sqrt{5}} & \frac{-2}{\sqrt{30}} \\ \frac{-1}{\sqrt{6}}& 0& \frac{5}{\sqrt{30}}\end{bmatrix}$ and $\Sigma = \begin{bmatrix}\sqrt{20}&0&0\\ 0&\sqrt{10}&0\end{bmatrix}  $in $A = U\Sigma V^{*}$
I got the A.
But when I updated U as $U = \begin{bmatrix}\frac{1}{\sqrt{2}} &\frac{1}{\sqrt{2}} \\\frac{1}{\sqrt{2}} &\frac{-1}{\sqrt{2}} \end{bmatrix}$, it produced -A.
This probably means certain version of $U$ and $V$ will reproduce A. I haven't  figured how should I choose them.
Comment: BTW note that in your first step you wrote $A^TA$ but you calculated $AA^T$.
Comment: Each singular vector is only uniquely determined up to sign, indeed in more generality it is only defined up to *complex* sign (i.e. up to multiplication by a complex number of modulus 1). However, in your case, the minus sign can move from the bottom right to the top right but *not* to the top left or bottom left. Doing that would switch the role of the two singular vectors, which would require you to reverse the order of the singular values and the right singular vectors to be consistent.
Comment: @Zereshki Most of the properties of the SVD follow from the following three facts: $Av_i=\sigma_i u_i$, and $A^T u_i = \sigma_i v_i$, and $v_i,u_i$ are unit vectors. (To be specific, *all* properties follow in the case when $A$ is square and all singular values are distinct; some more stuff can be required otherwise.) These properties are preserved if you multiply $u_i$ and $v_i$ (together) by $-1$, or more generally by a "complex sign" (i.e. a complex number with modulus $1$).
Comment: @Zereshki Let $U = \hat{U} S_U$ and  $V = \hat{V} S_V$, where $S_U$ and $S_V$ are the diagonal matrices whose entries on the main diagonal are the signs $\operatorname{sgn} (t_i)$. Hence, $$A = U \Sigma V^T =  \hat{U} S_U \Sigma  S_V \hat{V}^T$$The diagonal entries of $S_U \Sigma  S_V$ must be positive, which constrains the signs of the parameters.
Comment: @Moo I have not calculated it, since I have the same problem for finding V.
Comment: @David Thanks. I edited it.
Comment: You should not have used the same parameter $t$ to parameterize both left singular vectors. You should have used two parameters, say, $t_1$ and $t_2$.
Comment: @Rodrigo I just considered t as a free variable for each case separately.
Comment: @Lan Thanks for your explanation. I actually used the SVD calculator on the Internet. It produced $$U=\begin{bmatrix}  -0.7071 &-0.7071  \\-0.7071 &0.7071\end{bmatrix} $$ as an answer. I don't quite understand when you wrote each singular vector is only uniquely determined up to sign (I also have read it in the uniqueness proof).
 Would you please point me to (background) topic/subject that I need to better understand the details of what you explained?
Comment: @Lan Thanks for your explanation. I continued calculating V and updated my post. When I was watching the videos on calculating SVD by hand (like the ones from MIT-open-course-ware), they did not show how they decided on the signs. It made me think if there is a quick way for guessing the correct sign of U and V's columns.
Comment: @ Rodrigo Thanks for your detailed explanation. I updated my partial answer based on what you wrote. I understand there should be certain constraints on $t_{i}$ so I continued simplifying the expression. Yet, I haven't got a clue about the constraints that I should consider for choosing the sign.
Comment: @Zereshki  The signs of $t_1$ and $t_3$ must be the same. If they're not, you get $-A$. The signs of $t_2$ and $t_4$ must also be the same. The sign of $t_5$ does not matter, as it multiplies a column of zeros (the 3rd column of $\Sigma$).
Comment: I tried some of the alternatives (using MATLAB). So far when $t_{1},t_{3} > 0$ and  $t_{2},t_{4} < 0$ or when $t_{1},t_{3} < 0$ and  $t_{2},t_{4} > 0$ it reproduced $A$. For $t_{1},t_{2}, t_{3} ,t_{4} < 0$ or $t_{1} , t_{2}, t_{3} ,t_{4} > 0$  , it reproduced $-A$.
Answer: This is a very nice post with an illuminating discussionon the confusion between sign choices and how they propagate.
Define the singular value decomposition of a rank $\rho$ matrix as
$$
\mathbf{A} = \mathbf{U} \, \Sigma \, \mathbf{V}^{*}.
$$
Connecting vectors between row and column spaces
The sign conventions can be understood by looking at the relationship between the $u$ and $v$ vectors
$$
 u_{k} = \sigma^{-1}_{k} \mathbf{A} v_{k}, \qquad
 v_{k} = \sigma^{-1}_{k} u^{*}_{k} \mathbf{A}, \qquad k = 1, \rho
\tag{1}
$$
Changing the sign of a $u$ vector induces a sign change in the corresponding $v$ vector; changing the sign of $v$ vector induces a sign change the corresponding $u$ vector.
Example matrix
We certainly agree the singular values are $\sigma = \left\{ 2\sqrt{3}, \sqrt{10} \right\}$. Let's nominate a set of eigenvectors.
$$
 \mathbf{A}\,\mathbf{A}^{*}: \quad
%
u_{1} =
%
\frac{1}{\sqrt{2}}
\left[
\begin{array}{r}
 1 \\
 1 \\
\end{array}
\right], \quad
%
u_{2} =
%
\frac{1}{\sqrt{2}}
\left[
\begin{array}{r}
-1 \\
 1 \\
\end{array}
\right]
$$
$$
 \mathbf{A}^{*} \mathbf{A}: \quad
%
v_{1} =
%
\frac{1}{\sqrt{6}}
\left[
\begin{array}{r}
 1 \\
 2 \\
 1
\end{array}
\right], \quad
%
v_{2} =
%
\frac{1}{\sqrt{5}}
\left[
\begin{array}{r}
-2 \\
 1 \\
 0
\end{array}
\right]
%
v_{3} =
%
\frac{1}{\sqrt{30}}
\left[
\begin{array}{r}
-1 \\
-2 \\
 5
\end{array}
\right]
$$
How did we decide to distribute the minus signs in these vectors? Use the convention of making the first entries negative.
Stepping back, the eigenvectors have an inherent global sign ambiguity. For example
$$
\begin{align}
%
 \mathbf{A}^{*} \mathbf{A} v_{1} &= \mathbf{0} \\
%
 \mathbf{A}^{*} \mathbf{A} \left(-v_{1}\right) &= \left(- \mathbf{0} \right) = \mathbf{0}
%
\end{align}
$$
Using this arbitrary set of column vectors, construct the default SVD:
$$
 \mathbf{A} =
\left[
\begin{array}{cc}
 u_{1} & u_{2}
\end{array}
\right]
%
\Sigma \,
%
\left[
\begin{array}{ccc}
 v_{1} & v_{2} & v_{3}
\end{array}
\right]^{*}
$$
If we select the sign convention $\color{blue}{\pm}$ on the $k-$th column vector, we induce the sign convention $\color{red}{\pm}$ on the $k-$th column vector in the  complimentary range space. The $\color{blue}{choice}$ induces the $\color{red}{consequence}$.
Choices and consequences
For example, find the SVD by resolving the column space to produce the vectors $u$. The vectors $v$ will be constructed using the second equality in $(1)$. Flipping the global sign on $u_{1}$ flips the global sign on $v_{1}$.
$$
 \mathbf{A} =
\left[
\begin{array}{cc}
 \color{blue}{\pm}u_{1} & u_{2}
\end{array}
\right]
%
\Sigma \,
%
\left[
\begin{array}{ccc}
 \color{red}{\pm}v_{1} & v_{2} & v_{3}
\end{array}
\right]^{*}
$$
A nice feature of @Crimson's post is the demonstration that where can choose which range space to compute and which to construct. Compute $\mathbf{V}$ and construct $\mathbf{U}$, or compute $\mathbf{U}$ and construct $\mathbf{V}$.
The other path is to resolve the row space vectors $v$ and use the first equality in $(1)$ to construct the vectors $u$. The point is that flipping the global sign on $v_{1}$ flips the global sign on $u_{1}$
$$
 \mathbf{A} =
\left[
\begin{array}{cc}
 \color{red}{\pm}u_{1} & u_{2}
\end{array}
\right]
%
\Sigma \,
%
\left[
\begin{array}{ccc}
 \color{blue}{\pm}v_{1} & v_{2} & v_{3}
\end{array}
\right]^{*}
$$
Choice and consequence swap places. The process for the first vector extends to all $\rho$ vectors in the range space.
Counting choices
The number of unique sign choices is $2^\rho$, two choices for each range space vector.
Here the matrix rank is $\rho = 2$ and there are $2^{2} = 4$ unique vector sets. If $\mathbf{A}\mathbf{A}^{*}$ is resolved, then:
$$
\begin{array}{cc|cc}
 u_{1} & u_{2} & v_{1} & v_{2} \\\hline
 \color{blue}{+} & \color{blue}{+} & \color{red}{+} & \color{red}{+} \\
 \color{blue}{+} & \color{blue}{-} & \color{red}{+} & \color{red}{-} \\
 \color{blue}{-} & \color{blue}{+} & \color{red}{+} & \color{red}{+} \\
 \color{blue}{-} & \color{blue}{-} & \color{red}{-} & \color{red}{-} \\
\end{array}
$$
If instead $\mathbf{A}^{*}\mathbf{A}$ is resolved, then:
$$
\begin{array}{cc|cc}
 v_{1} & v_{2} & u_{1} & u_{2} \\\hline
 \color{blue}{+} & \color{blue}{+} & \color{red}{+} & \color{red}{+} \\
 \color{blue}{+} & \color{blue}{-} & \color{red}{+} & \color{red}{-} \\
 \color{blue}{-} & \color{blue}{+} & \color{red}{+} & \color{red}{+} \\
 \color{blue}{-} & \color{blue}{-} & \color{red}{-} & \color{red}{-} \\
\end{array}
$$
Another example is in SVD and the columns — I did this wrong but it seems that it still works, why?
Answer: The left singular vectors are
$$\mathrm{u}_1 \in \left\{ t_1 \begin{bmatrix} 1\\ 1\end{bmatrix} : t_1 \in \mathbb R \right\}$$
$$\mathrm{u}_2 \in \left\{ t_2 \begin{bmatrix} 1\\ -1\end{bmatrix} : t_2 \in \mathbb R \right\}$$
We want the left singular vectors to be orthonormal. They are already orthogonal. Normalizing,
$$\mathrm{u}_1 = \frac{t_1}{\sqrt{2 t_1^2}} \begin{bmatrix} 1\\ 1\end{bmatrix} = \operatorname{sgn} (t_1) \begin{bmatrix} \frac{1}{\sqrt{2}}\\ \frac{1}{\sqrt{2}}\end{bmatrix}$$
$$\mathrm{u}_2 = \frac{t_2}{\sqrt{2 t_2^2}} \begin{bmatrix} 1\\ -1\end{bmatrix} = \operatorname{sgn} (t_2) \begin{bmatrix} \frac{1}{\sqrt{2}}\\ -\frac{1}{\sqrt{2}}\end{bmatrix}$$
where $\operatorname{sgn}$ denotes the signum function. Hence,
$$\mathrm U = \begin{bmatrix} | & |\\ \mathrm{u}_{1} & \mathrm{u}_{2}\\ | & |\end{bmatrix} = \begin{bmatrix} \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}}\\ \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}}\end{bmatrix} \begin{bmatrix} \operatorname{sgn} (t_1) & 0\\ 0 & \operatorname{sgn} (t_2)\end{bmatrix}$$
There are $2^2 = 4$ possible choices.
Comment: @Zereshki Note that whenever you change $U$ you must correspondingly change $V$.
Comment: Thank you. From your explanation, I concluded that $U$ can be any of these 4 matrices.
$$ \begin{bmatrix}\frac{-1}{\sqrt{2}} &\frac{-1}{\sqrt{2}} \\ \frac{-1}{\sqrt{2}} &\frac{1}{\sqrt{2}} \end{bmatrix}$$
$$ \begin{bmatrix}\frac{-1}{\sqrt{2}} &\frac{1}{\sqrt{2}} \\ \frac{-1}{\sqrt{2}} &\frac{-1}{\sqrt{2}} \end{bmatrix}$$
$$ \begin{bmatrix}\frac{1}{\sqrt{2}} &\frac{-1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}} &\frac{1}{\sqrt{2}} \end{bmatrix}$$
$$ \begin{bmatrix}\frac{1}{\sqrt{2}} &\frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}} &\frac{-1}{\sqrt{2}} \end{bmatrix}$$
Answer: For $12$, $u_1=\begin{bmatrix} 1/\sqrt{2}\\ 1/\sqrt{2}\end{bmatrix}$ is a normalised  eigenvector.   Now, compute the first column of $V$, $$v_1=\frac{A^T}{\sqrt{12}}u_1= \begin{bmatrix} 2/\sqrt{24}\\ 4/\sqrt{24}\\ 2/\sqrt{24}\end{bmatrix}.$$Corresponding to $10$, a nomalised eigenvector is $u_2=\begin{bmatrix}1/\sqrt{2}\\ -1/\sqrt{2}\end{bmatrix}$. Calculate the second column of $V$, $$v_2=\frac{A^T}{\sqrt{10}}u_2=\begin{bmatrix}4/\sqrt{20}\\ -2/\sqrt{20}\\ 0\end{bmatrix}.$$
For $v_3$, choose any eigenvector corresponding to $0$.  The sign doesn't matter. You can choose $v_3=\begin{bmatrix}1/\sqrt{30}\\ 2/\sqrt{30}\\ -5/\sqrt{30}\end{bmatrix}$
Take $U=[u_1 u_2]$, $V=[v_1 v_2 v_3]$, $S=\begin{bmatrix}\sqrt{12}&0&0\\ 0&\sqrt{10}&0\end{bmatrix}$.  Then $A=USV^T$.
You don't have to worry about the sign of the eigenvectors for $AA^T$.  Choose any eigenvector, but make sure that things match nicely by choosing the columns of $V$ as I indicated.
Note further that the non-zero eigenvalues of $AA^T$ and $A^TA$ are the same.  So, after finding the eigenvalues of $AA^T$, you need not find the eigenvalues of $A^TA$ again.  Also, you get the normalised eigenvectors for the nonzero eigenvlaues of $A^TA$ virtually `for free' from the eigenvectors of $AA^T$.


Understanding picture handling in Intel XDK

Question: I've used this code to use my phone's photo library
// event listener for camera
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
        pictureSource=navigator.camera.PictureSourceType;
        destinationType=navigator.camera.DestinationType;
}

and that works OK.
I invoke the photo library using:
    navigator.camera.getPicture(onGetPicSuccess, onGetPicFail, {
        quality: 50,
        destinationType: destinationType.FILE_URI,
        sourceType: pictureSource.PHOTOLIBRARY
    });

then the function called after successful picture "get"
function onGetPicSuccess(imageData){

The variable "imageData" looks like this:
blob:http%3A//localhost%3A57492/ac65226c-5698-4345-b301-81205b1403fa

Now, my question.
I'm assuming that is not the actual image data. It's a URL that points to the image on the camera.
Is that correct?
If so, how do I get the actual image data?
My end goal is to format the data into the same as what would be used in a HTML file upload form.
Answer: Yes, the variable "imageData" does contain an URI to the picture on the camera.
I needed to create a FileTransfer() object and give it the URI and it takes care of sending the raw image data.
     var ft = new FileTransfer();
     ft.upload(imageData, "https://somewhere.com/cgi/mPictureUpload.exe", function(result){
         // success
     }, function(error){
         // fail
     }, options);


Has there ever been an orchestra without a conductor?

Question: I don't think I have ever seen an orchestra where there wasn't someone conducting.
Has there ever been such an example of an orchestra?
Edit: By conductor, I mean the guy swinging the baton:
Comment: I think if you read this question in a charitable manner the OP means an orchestra that has no person acting specifically as conductor.
Comment: Not all conductors use a baton (says the wise-ass pedant).  And there's a short story somewhere about a conductor who uses his tail.
Comment: If an orchestra is "led" by a concertmaster or soloist (e.g., when performing a concerto), does that count as having a conductor or not, in terms of your question? See: https://en.wikipedia.org/wiki/Conductorless_orchestra
Comment: Guidot's answer speaks to my uncertainty about what the asker considers to be a "conductor", and since there are many aspects to a conductor's job, it's not even clear what is meant by "acting specifically as a conductor". That being said, regardless of what one thinks of as a "conductor", it seems like some orchestras do not have any one person performing *any* of those tasks, as I quote in my answer. So while I still think the question isn't 100% clear, it doesn't actually have to be 100% clear to be answerable.
Comment: @ToddWilcox Neil is correct; I updated my question to specify what I meant
Answer: It looks like yes, regardless of your answer to my comment.
From https://en.wikipedia.org/wiki/Conductorless_orchestra

The East Coast Chamber Orchestra (ECCO) was envisioned in 2001, when a group of young string players sought to form a conductorless chamber orchestra, based upon democratic principles. The members are soloists, orchestral musicians, and chamber musicians all mainly trained at Marlboro Music School and Festival. ECCO is a string orchestra made up of 17 instrumentalists.
The Advent Chamber Orchestra started in 2003 when Roxana Pavel and
  Elias Goldstein worked together in chamber music ensembles. The
  ensemble functioned primarily as a chamber music collaborative until
  2005 performed their first concert as the Advent Chamber Orchestra.
  The ensemble is generally led by concertmaster and music director
  Roxana Pavel Goldstein, but artistic decisions are achieved
  democratically.
A Far Cry, formed in early 2007, is a Boston based chamber orchestra
  made up of 18 string players. A Far Cry describes itself as
  self-conducted, and operates with rotating leadership and no
  conductor. All artistic decisions are made by vote, and the orchestra
  members handle all the artistic management and promotion of the
  ensemble. A Far Cry has close ties to the New England Conservatory
  (NEC) as well as the Isabella Stewart Gardner Museum, where it is
  ensemble-in-residence.
The orchestra Spira Mirabilis, formed in 2007, is a European classical
  orchestra functioning without a conductor.
The Lyra Vivace Chamber Orchestra, founded in 2010, is a musician-run
  collective based in Augusta, GA and the SouthEastern United States.
  Lyra Vivace is an ensemble of www.InPraiseofMusic.org, a resource of
  and for professional musicians.
Arizona Chamber Orchestra is the most recently formed conductorless
  ensemble. Based in Phoenix, the Arizona Chamber Orchestra presents its
  inaugural season in 2011 with a nucleus of 15 string players,
  performing throughout Arizona.
Ars Nova Chamber Orchestra, formed in late 2010, is a Washington,
  D.C.-based chamber orchestra made up of 29 musicians. It is
  self-conducted, and operates with rotating leadership in all sections.
  Ars Nova's artistic decisions are guided by the musicians, and they
  all participate in marketing and administering the ensemble. The
  ensemble's members include D.C.-based musicians, and musicians based
  in other parts of the United States.

And those are just the contemporary examples.
Comment: `regardless of your answer to my comment.` I didn't answer your comment until now :P
Comment: Right, I meant that regardless of whatever your answer might be to my comment. Probably I should have just deleted my comment after I discovered it didn't matter. Oh well.
Comment: I was going to mention A Far Cry :-).   Further,  IIRC on occasion Bernstein would leave the podium empty to play a piano concerto with the NY Phil.
Comment: Actually, those are just the examples of groups that were *founded in the 21st century.*  [Orpheus](https://en.wikipedia.org/wiki/Orpheus_Chamber_Orchestra), a pioneer in the field, is mentioned under "1970s" because it was founded in 1972, but it is alive and well.
Answer: Unfortunately neither term is strictly defined.
Orchestra may be anything from a string ensemble (starting at about 12 people), to a late-romantic crowd (easily exceeding 60 instrumentalists).
Conductor may be
1) the name for the person, swinging the baton
2) the person having the concept for interpretation and rehearsing it with the colleagues
Smaller, professional players may have no type-1 conductor, to form a coherent interpretation for lacking type-2 conductor they must be very synchronized.
As soon as soloists (as in solo concerto, or vocal soloists as in an opera) are involved, or a full orchestra is playing I don't believe in existence of an orchestra without type-1 conductor. The area the players need, the remoteness of far players, speed of sound delays, free capacity of mind to detect and correct any sort of delay wherever...
Comment: I updated the question to specify what kind of conductor I refer to
Answer: Yes it is sometimes the case with smaller baroque and ancient music type orchestras although other eras of music also had it sporadically. Often it is chamber music with only strings.
I know the Irish choir group Anuna sometimes have a small conductorless string ensemble. Something which fits it with the type of music they perform. IF the video is anything to go by it looks like the lead of the 1st violins takes on the role of conductor.

You can read more about it here.
Comment: Video link is dead.
Answer: Taking the question at its most literal: yes, there have been many conductor-less orchestras. The conductor, in the sense of the person in front of the orchestra with a skinny baton, is a relatively recent innovation.
Wikipedia offers a history of conducting, which includes this statement:

By the early 19th century (ca. 1820), it became the norm to have a dedicated conductor, who did not also play an instrument during the performance.

In the general sense, conductors of some sort go back at least to the middle ages (see Wikipedia), but the modern conception of the conductor can be traced to, roughly, 1820.
A 2018 article in the South China Morning Post also offers some brief history.

It was 1820 when the word "conductor" appeared on a concert programme for the very first time, according to the book A History of Orchestral Conducting.

Brittanica has a brief and useful history of conducting, which, among other things, contains this tidbit.

Conducting became a specialized form of musical activity only in the early 19th century. As early as the 15th century, performances by the Sistine Choir in the Vatican were kept together by slapping a roll of paper (or in other cases, a lengthy pole, or baton) to maintain an audible beat. This practice continued until it became an actual intrusion on the performance and was of necessity abandoned.


Java String Split/Manipulation

Question: I need to write a method in Java that can take a String text parameter and an array of String "regexes" parameter that returns an array of Strings.
I want the returned Array to basically do what String.split does, but with multiple String regexes and instead of stripping the regex, keep it in the array.
Example:
Input: "int a=10; a++;"
Regexes: "int", ";", "++", "="
Output: "int", " a", "=", "10", ";", " a", "++", ";"
And I tried a few things including the following, none worked
public static String[] splitIntoBits(String in, String[] regex)
{
    List<String> bitList = new ArrayList<String>();

    for(int i = 0; i < in.length(); i++)
    {
        int lastIndex=0;
        //Check that character against all regexes
        for(int j = 0; j < regex.length; j++)
        {
            if(in.substring(i).startsWith(regex[j]))
            {
                bitList.add(in.substring(lastIndex, i));
                lastIndex=i;
            }
        }
    }

    return bitList.toArray(new String[0]);
}

Comment: You could create a new ArrayList after each split, capture the String[]s and feed them into the list. then use the List to feed strings into the next phase.
Comment: How do you handle overlapping patterns (in other words, where the match is ambiguous)?
Comment: @durron597 - I would probably handle that with more specific checking for what comes before or after the regex.
Comment: If you are really trying to parse code you will need more than split().
Comment: @peter.murray.rust - what would I need?
Comment: You may also look at Scanner class (http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#method%5Fsummary) but @peter.murray.rust looks resonable
Comment: For code you need a parser such as ANTLR (you'll find it on SO). But writing a parser is complex. There is already an ANTLR parser that parses Java.
Answer: You can construct a single regex to match all the regexes in the argument, then use Matcher.find() tracking the end index of previous matches with end().
Note: in my code below, I show Pattern.quote(r) to handle '++' (which is an invalid regex pattern).  If you are indeed supporting regexes as the argument, just remove the call.
    String[] regex = { "int", ";", "++", "=" };
    StringBuilder pattern = new StringBuilder();
    for (String r : regex) {
        pattern.append('|').append(Pattern.quote(r));
    }

    String input = "int a=10; a++;";
    Matcher m = Pattern.compile(pattern.substring(1)).matcher(input);
    // e.g. m = Pattern.compile("int|;|\\+\\+|=").matcher(input);
    List<String> result = new ArrayList<String>();
    for (int i=0 ; m.find() ; i = m.end()) {
        if (i != m.start()) {
            result.add(input.substring(i, m.start()));
        }
        result.add(m.group(0));
    }
    String[] array = result.toArray(new String[result.size()]);

Java String Split/Manipulation


how to print data from table relation using with() funtion in laravel

Question: Here is the generated array from table relation:
{"id":1,"shelf":{"id":1,"name":"Computer Related","description":"Lorem ipsum dolor somet.","created_at":"2017-01-15 00:00:00","updated_at":"2017-01-15 00:00:00","deleted_at":null},"book_no":"001","title":"Programming","author":"Rasmus Lerdorf","publisher":"Micheal Holmes","description":"The planning, scheduling, or performing of a program.","book_img":null,"no_of_books":20,"created_at":"2017-01-11 00:00:00","updated_at":"2017-01-11 00:00:00","deleted_at":null}

Just want to know how can I get value of name from shelf array value.
Comment: where to you want to do it in javascript or mysql?
Comment: I want it to print using curly braces {{ }} in laravel blade.
Something like this:
{{ $variable->field }}
Answer: Your output is in json format.so use json_decode() to convert into array.As below.
$json = '{"id":1,"shelf":{"id":1,"name":"Computer Related","description":"Lorem ipsum dolor somet.","created_at":"2017-01-15 00:00:00","updated_at":"2017-01-15 00:00:00","deleted_at":null},"book_no":"001","title":"Programming","author":"Rasmus Lerdorf","publisher":"Micheal Holmes","description":"The planning, scheduling, or performing of a program.","book_img":null,"no_of_books":20,"created_at":"2017-01-11 00:00:00","updated_at":"2017-01-11 00:00:00","deleted_at":null}';

$array = json_decode($json,true);
print_r($array);

Output:
Array ( [id] => 1 [shelf] => Array ( [id] => 1 [name] => Computer Related [description] => Lorem ipsum dolor somet. [created_at] => 2017-01-15 00:00:00 [updated_at] => 2017-01-15 00:00:00 [deleted_at] => ) [book_no] => 001 [title] => Programming [author] => Rasmus Lerdorf [publisher] => Micheal Holmes [description] => The planning, scheduling, or performing of a program. [book_img] => [no_of_books] => 20 [created_at] => 2017-01-11 00:00:00 [updated_at] => 2017-01-11 00:00:00 [deleted_at] => )

Get value of name using echo $array['shelf']['name']; because your array is multidimensional.
Comment: yeah got it.. Thanks


pushed to heroku but no changes to app?

Question: I used the following three commands to push the changes to an already existing app but the changes are not being reflected
$ git add .
$ git commit -m "changes"
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
$ git push heroku master

And I get:
To firstname.lastname@example.com:sleepy-oasis-7771.git ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'firstname.lastname@example.com:sleepy-oasis-7771.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

and no changes are reflected in the app
Comment: Check this as well: http://stackoverflow.com/questions/9831129/failed-to-push-some-refs-to-githeroku-commyapp-git
Answer: Your push was ! [rejected]. That's why no changes take effect.
As the message indicates:
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

This refers you to further documentation, visible on your local machine or online. Again, as the message indicates, a git pull (and its resulting merge) will fix this issue.
One way to avoid this problem is by using a rebase workflow instead of a merge workflow. Do your development in a feature branch, and when you're ready to merge, pull master, rebase the feature branch, re-run your test suite, and then merge.
Also, please read your error messages.

pushed to heroku but no changes to app?


Why did the Buffalo Bills score a touchdown without ball breaking the plane of the goal line?

Question: On January 8 2023, the Buffalo Bills scored a touchdown on kickoff return. Nyheim Hines returned the ball, then ran very near the sideline. Shortly before reaching the end zone, he spread his arms so that the ball was outside the field. After entering the end zone with the ball still being outside the field, he threw the ball away. From my perspective, it looked like the ball only crossed the goal line after Nyheim Hines threw it away.
I am no football expert, but my question is: Why was a Touchdown called or why wasn't there at least a thorough review?
Answer: I'm not sure if you're asking if the ball remained in possession past the goal line, or if the ball has to be inside the sidelines.  I'll address both.
All scoring plays are reviewed by the booth official.  If they had a concern with the review, they would have signaled to the on-field referee to wait if more time was necessary.  Presumably they were comfortable with seeing that the ball broke the plane of the goal while still in possession.  That's all done behind the scenes, so it's not possible to know how much concern or what was seen.

From my viewing, the ball remains in his outstretched hand until after he reaches the end zone and the ball appears to have crossed the goal line.  It looks quite precarious in this image, but it looked that way for some steps before the end zone as well.  I don't see anything to suggest that he lost possession before scoring.
You mentioned the ball was "outside the field".  As long as the ball crosses the plane in possession, and the possessing player is in the end zone, then the ball can be beyond the sideline but still a valid touchdown.
From NFL Rule 11-2-1, subpart (a) was met:

ARTICLE 1. TOUCHDOWN PLAYS. A touchdown is scored when:
(a) the ball
is on, above, or behind the plane of the opponents' goal line
(extended) and is in possession of a runner who has advanced from the
field of play into the end zone

It is only if the ball crosses the plane but if the runner does not reach the end zone that the position of the ball around the pylon is required.
This play is similar to approved ruling 3.16 (c):

A.R. 3.16 PYLON—GOAL LINE EXTENDED
Second-and-goal on B5. A2, running
to his right, is contacted by B2, and as he is going down at the B1,
he dives toward the pylon. The ball is held in his right hand extended
over the out of bounds area but beyond the extended goal line, when
his chest hits: ... (c) the
ground in the end zone. The ball crossed the sideline at the B½-yard
line, but it never went over the top of the pylon.
Rulings:
...
(c) Touchdown, as the ball broke the plane of the extended goal line, and the runner was not down until he was in
the end zone. (3-39, 11-2-1)
Comment: Thank you! To clarify: My question was if the ball has to be inside the sidelines.


count third dimension array segments

Question: I have the following array
Array
(
    [01] => Array
        (
            [03] => Array
                (
                [0] => 38
                [1] => 30
                )

        )
)

How do I count the segments in the third dimension?
So $arrayname[01][03] has 2 elements.
Answer: Use count to count the number of elements in an array
count($arrayname["01"]["03"]);

Also keep in mind that your array keys are actually strings (not numbers)
Comment: Yep. It's *that* simple.

count third dimension array segments


What assumptions about probability do different models make?

Question: If we throw a dice n times and get an empirical, discrete probability distribution, one could say that it approximates the real underlying distribution.
When we build a machine learning model like neural networks or logistic regression, libraries often offer predicted probabilities.
Empirically, we can evaluate them with, e.g., RMSE or other metrics compared to known cases.
However, what are the model assumptions regarding probability?
For example, has the logistic function any specific property that makes it more suitable to predict probabilities than another function? Have neural networks any specific link to probability?
To sum up: In which case can I use predicted probabilities from ML models for probability-related computations (e.g. expected value)?
Answer: These are great questions, and their answers can be found in the paper Why the logistic function? A tutorial discussion on probabilities and neural networks by Michael I. Jordan, 1995. I highly recommend you read it all. However, I will provide a short summary here. Note that some of what I write will not necessarily be found in the paper.
In the context of supervised machine learning, our overall objective is to classify a given signal, whether that is a 1-dimensional (audio, time-series) or 2-dimensional (image) signal, represented by $\mathbf{x}$. Suppose that $\mathbf{x}$ may belong to one of two classes: $c_1$ or $c_2$. We can then classify $\mathbf{x}$ using the following decision rule:
$$
\mathbf{x} \in c_1 \ \ \text{if} \ \ p(c_1|\mathbf{x}) > p(c_2|\mathbf{x}) \\
\mathbf{x} \in c_2 \ \ \text{if} \ \ p(c_1|\mathbf{x}) < p(c_2|\mathbf{x})
$$
This decision rule is also known as the Bayes classifier, which turns out to be the most optimal classifier. In other words, the Bayes classifier will maximize the probability of correctly classifying $\mathbf{x}$. The proof of this statement can be found on the same Wikipedia page of the link above.
Therefore, to classify $\mathbf{x}$, our objective now is to compute both $p(c_1|\mathbf{x})$ and $p(c_2|\mathbf{x})$ and compare them. Notice that:
$$
\begin{align}
p(c_1|\mathbf{x})
&= \frac{p(\mathbf{x}|c_1)p(c_1)}{p(\mathbf{x})} \\
&= \frac{p(\mathbf{x}|c_1)p(c_1)}{p(\mathbf{x}|c_1)p(c_1) + p(\mathbf{x}|c_2)p(c_2)}
\end{align}
$$
Dividing the numerator and denominator by $p(\mathbf{x}|c_1)p(c_1)$ yields:
$$
p(c_1|\mathbf{x}) = \frac{1}{1 + \frac{p(\mathbf{x}|c_2)p(c_2)}{p(\mathbf{x}|c_1)p(c_1)}}
$$
Since:
$$
\frac{p(\mathbf{x}|c_2)p(c_2)}{p(\mathbf{x}|c_1)p(c_1)} = \exp\left(\text{ln}\left(\frac{p(\mathbf{x}|c_2)p(c_2)}{p(\mathbf{x}|c_1)p(c_1)}\right)\right)
$$
Then:
$$
\begin{align}
p(c_1|\mathbf{x})
&= \frac{1}{1 + \exp\left(\text{ln}\left(\frac{p(\mathbf{x}|c_2)p(c_2)}{p(\mathbf{x}|c_1)p(c_1)}\right)\right)} \\
&= \sigma\left(\text{ln}\left(\frac{p(\mathbf{x}|c_2)p(c_2)}{p(\mathbf{x}|c_1)p(c_1)}\right)\right)
\end{align}
$$
Where $\sigma(\cdot)$ is the logistic function. Therefore, our new objective is to estimate $p(\mathbf{x}|c_1),p(c_1),p(\mathbf{x}|c_2),$ and $p(c_2)$, and since:
$$
\begin{align}
p(c_1|\mathbf{x}) + p(c_2|\mathbf{x}) &= 1 \\
p(c_2|\mathbf{x}) &= 1 - p(c_1|\mathbf{x})
\end{align}
$$
Then we do not need to estimate any other probabilities to perform Bayesian classification. However, in practice, it is very difficult to accurately estimate $p(\mathbf{x}|c_1)$ and $p(\mathbf{x}|c_2)$. Here's a better idea: why not estimate $p(c_1|\mathbf{x})$ directly? This idea is attributed to Vladimir Vapnik, and it is the basic idea behind discriminative models.
Notice that in the equation:
$$
p(c_1|\mathbf{x}) = \sigma\left(\text{ln}\left(\frac{p(\mathbf{x}|c_2)p(c_2)}{p(\mathbf{x}|c_1)p(c_1)}\right)\right)
$$
For any given problem, the right-hand side of this equation is only a function of $\mathbf{x}$. Therefore, let:
$$
p(c_1|\mathbf{x}) = \sigma(f(\mathbf{x};\theta))
$$
Where $f$ is a function that is parameterized by $\theta$. Our final objective then is to choose the function $f$ and estimate the parameters $\theta$ to perform Bayesian classification. There are many different choices for $f$. One example is a neural network, where its parameters are represented by $\theta$.
Given that $f$ is a neural network, the next step is to estimate its parameters $\theta$. In practice, given a training dataset:
$$
\mathcal{D} = \{(\mathbf{x}_1,c_1),(\mathbf{x}_2,c_1),...,(\mathbf{x}_N,c_1),(\mathbf{x}_1,c_2),(\mathbf{x}_2,c_2),...,(\mathbf{x}_M,c_2)\}
$$
This is usually done using Maximum Likelihood estimation. More details can be found here.
Comment: Wow, this is a great answer, too! Thank you for the summary and for pointing to the paper. Will read it thoroughly.
Comment: By the way, I did not mention this in the answer, but the paper I linked to by Michael I. Jordan also discussed how to maximize the likelihood function in great detail. Overall, I think it is a wonderful paper that I wish more people knew about.


Convolution of two identical functions and Fourier transform

Question: I have the following problem to solve:

Express the integral $$\int_{-\infty}^\infty \frac{1}{1+x^2}\frac{1}{1+(x-t)^2}dx$$ in terms of $t$.

My solution is as follows:
We quickly realize that we have that the integral is just the convolution $f * f$ where
$$f := \frac{1}{1+x^2}$$
Furthermore, if we define the integral as $I(t)$, we have that $\mathcal{F}(I) = \mathcal{F}(f * f) = (\mathcal{F}(f))^2 $. So,
$$I(t) = \mathcal{F}^{-1}((\mathcal{F}(f))^2)$$
Now $\mathcal{F}(f) = \pi e^{-|x|}$, so
$$(\mathcal{F}(f))^2 = \pi^2 e^{-|2x|} = 2\pi \cdot \pi/2e^{-|2x|} = \mathcal{F}\left(2\pi \frac{1}{1+(2t)^2}\right)$$
And so by the property of uniqueness for Fourier transform, we have that: $$I = 2\pi \frac{1}{1+4t^2}$$
Is this correct? And also, is there any other easier way to solve it? Also, I'm struggling oftenly to keep $x$ and $t$'s separated which makes me confused whether I should do the inverse fourier or the regular Fourier transform of a function. Is there any easy trick to distinguish these variables?
Comment: It seems like you could try partial fractions. Write $\frac{1}{1+x^2}\frac{1}{1+(x-t)^2}=\frac{a+bx}{1+x^2}+\frac{c+dx}{1+(x-t)^2}$ where $a,b,c,d$ are functions of $t.$ This should work for all $t\neq 0.$
Comment: This is a problem that is really well-suited to Fourier-transform methods, as you observed. Your method is fine, and assuming you kept careful track of where the factors of $\pi,\sqrt\pi,\dots$, etc. all go, your solution should be correct. If you want someone to verify for you if it's 100% correct, it may be helpful to edit your post and recall your convention for the Fourier transform.
Comment: @ThomasAndrews: What's the significance of considering $t\ne0$? As a function of $t$, $I(t)$ is well-defined for every $t$.
Comment: @AlexOrtiz $t\neq 0$ just because this partial fraction decomposition don't work there, not because it is undefined. The value when $t=0$ can be computed with the limit as $t\to 0.$ Essentially, $a,b,c,d$ will end up undefined at $t=0,$ so this approach fails there.
Comment: [WolframAlpha](https://www.wolframalpha.com/input?i=Integrate%5B1%2F%281%2Bx%5E2%29*1%2F%281%2B%28x-t%29%5E2%29%2C%7Bx%2C-Infinity%2C+Infinity%7D%2CAssumptions-%3Et%3E0%5D) certainly seems to think it's correct (the $t>0$ is just so it doesn't think $t$ might be complex). One suggestion though: use a different variable, such as $k$, for the Fourier transformed functions.
Comment: The error is assuming the transform of $f(ax)$ is $\hat f(ax).$ So the approach is right, you just has the wrong last step.
Comment: If $\hat f(x)$ is the transform of $f$ then $\frac1{|a|}\hat f(x/a)$ is the transform of $f(ax).$
Answer: As suggested by @Thomas Andrews. Do the partial fractions as:
$$\frac{1}{(1+x^2)(1+(t-x)^2)}= \frac{Ax+B}{1+x^2}+\frac{Cx+D}{1+(t-x)^2}.....(1)$$
One gets $$A=\frac{2}{t(4+t^2)}, B=\frac{1}{4+t^2}, C=\frac{-2}{t(4+t^2)}, D=\frac{3}{(4+t^2)}.$$
While integrating (1) from $x=-\infty$ to $x=\infty$ the integral multiplied to $A$ vanishes and we get
$$\implies I=\frac{1}{4+t^2} \left(\int_{-\infty}^{\infty} \frac{dx}{1+x^2}dx+ \int_{-\infty}^{\infty}\frac{dx}{1+(t-x)^,}+\int_{-\infty}^{\infty}\frac{2(t-x)dx}{1+(t-x)^2}\right).$$
Let $t-x=u$, then
$$I=\frac{1}{4+t^2} \left(\int_{-\infty}^{\infty} \frac{dx}{1+x^2}dx+ \int_{-\infty}^{\infty}\frac{dx}{1+u^2}+\int_{-\infty}^{\infty}\frac{2udu}{1+u^2}\right).$$
The first two integrals give $\pi+\pi$ and the third one vaishes. Finally
$$I=\frac{2\pi}{4+t^2}.$$
Comment: You forgot the $\frac{2x}{x^2+1}$ in the integrals. There should be four. Technically $$\int_{-\infty}^{\infty}\frac x{x^2+1}\,dx$$ and the opposite term don't converge. We have $\lim_{R\to\infty}\int_{-R}^{R}$ converges, but if the two bounds go to infinity separately, they don't. But since the starting integral definitely converges, we can show the $[-R,R]$ case is enough.
Comment: That is what  is with $A$ and it vanises the integrabd being odd.
Comment: Except the limit doesn't even converge. Just because the function is odd doesn't mean the limit converges to $0.$
Answer: Sorry, too long for a comment
As @eyeballfrog provided the link in the comment, WolframAlpha gives $\displaystyle I(t)=\frac{2\pi}{4+t^2}$ (please, pay attention that not $\displaystyle \frac{2\pi}{1+4t^2}$).
I may suppose thay you define FT as $\displaystyle \hat f(k)=\int_{-\infty}^\infty f(x)e^{ikx}dx$. In this case you do get $\displaystyle\int_{-\infty}^\infty \frac{e^{ikx}}{1+x^2}dx=\pi e^{-|k|}$.
To find an answer, you could also use the Plancherel' theorem (here), which states (for the above chosen FT) that
$$\int_{-\infty}^\infty f(x)g^*(x)dx=\frac{1}{2\pi}\int_{-\infty}^\infty \hat f(k)\hat g^*(k)dk$$
Taking $\,\displaystyle g(x)=\frac{1}{1+x^2},\,\, f(x)=\frac{1}{1+(x-t)^2}$, you get $\,\displaystyle \hat g(k)=\pi e^{-|k|}; \hat f(k)=\pi e^{-|k|}e^{ikt}$
$$I=\frac{\pi^2}{2\pi}\int_{-\infty}^\infty e^{-2|k|}e^{ikx}dk=\pi\, \Re\,\int_0^\infty e^{-2k}e^{ikx}dk=\pi\, \Re\,\frac{1}{2-it}=\frac{2\pi}{4+t^2}$$
Comment: Nice answer, I'll look into that theorem. Have never seen it before so it should be fun!


Uniform Integrability and Conditional Expectations

Question: Let $X=\{X_n:n\in \mathbb{N_0}\}$ be a supermartingale or a submartingale on a filtered space $(\Omega,\mathcal{F},\{\mathcal{F}_{n}\},\mathbf{P}).$ Suppose the sequence $\{X_n\}_{n\in\mathbb{N_0}}$ is uniformly integrable $\left( \text{i.e.} \lim \limits_{\lambda \rightarrow \infty} \sup _{n \in \mathbb{N}_0} E\left(\left|X_{n}\right| \mathbf{1}_{\left\{\left|X_{n}\right|>\lambda\right\}}\right)=0\right)$,then  we have $\sup _{n \in \mathbb{N}_0} E\left(\left|X_{n}\right|\right)<\infty$.By Doob's Martingale Convergence Theorem (discrete case),there exits a random variable $X_{\infty}$ on $(\Omega,\mathcal{F},\mathbf{P})$,such that $\lim \limits_{n \rightarrow \infty} X_{n}=X_{\infty}\text{ a.s.}$.Since uniformly integrability of $\{X_n\}_{n\in\mathbb{N_0}}$,then $\lim \limits_{n \rightarrow \infty}\left\|X_{n}-X_{\infty}\right\|_{1}=0$, that is, the sequence $\{X_n\}_{n\in\mathbb{N_0}}$ converges to $X_{\infty}$ in the $L^{1}$ norm.
For any fixed $k\in\mathbb{N_0},$can we have $$
\boxed{\mathbf{E}(X_\infty\big|\mathcal{F_{k}})=\lim \limits_{n \rightarrow \infty}\mathbf{E}(X_n\big|\mathcal{F_{k}})\text{ a.s.}\quad?}
$$
I doubt the validity of this equation.Surely,$X_n \to X_{\infty}$ in $L^1$ implies $\mathbf{E}(X_n\big|\mathcal{F_{k}})\to\mathbf{E}(X_{\infty}|\mathcal{F_{k}})$ in $L^1$.But from this we only know that there exists a subsequence $\left\{\mathbf{E}(X_{n_{\ell}}\big|\mathcal{F_{k}}): \ell \in \mathbb{N}_{0}\right\}$ such that $$\mathbf{E}(X_\infty\big|\mathcal{F_{k}})=\lim \limits_{\ell \rightarrow \infty}\mathbf{E}(X_{n_{\ell}}\big|\mathcal{F_{k}})\text{ a.s.}\quad$$
I feel the condition $\lim \limits_{n \rightarrow \infty} X_{n}=X_{\infty}\text{ a.s.}$ doesn't contribute to deducing $$\mathbf{E}(X_\infty\big|\mathcal{F_{k}})=\lim \limits_{n \rightarrow \infty}\mathbf{E}(X_n\big|\mathcal{F_{k}})\text{ a.s.}$$
How to construct a counterexample to show its invalidity, or prove its correctness ?
Answer: The sequence $\left(\mathbb E\left[X_n\mid\mathcal F_k\right]\right)_{n\geqslant k}$ is monotonic (non-increasing in the case of a supermartingale, non-decreasing in the case of a submartingale). Moreover, $\left(\mathbb E\left[X_n\mid\mathcal F_k\right]\right)_{n\geqslant k}$ converges in probability to $\mathbb E\left[X_\infty\mid\mathcal F_k\right]$  hence the convergence takes place in the almost sure sense.
Comment: Opps ! I completely forgot about the condition of monotonicity. THX !
Answer: Hint: It is easy to check that  $(E(X_n|\mathcal F_k))$ is a sub/super-martingale.
$P(E(X_n|\mathcal F_k)|>\Delta)\le \frac 1 {\Delta} E|X_n|$. Use this  to show that $(E(X_n|\mathcal F_k)) $ is uniformly integrable (which gives a.s. convergence). [Recall that  family $(X_i)$ is uniformly integrable if and only if

$\sup E|X_i| <\infty$ and

For any $\epsilon >0$ there exists $\delta>0$ such that $\int_{A} |X_i|dP<\epsilon$ for all $i$ whenever $P(A) <\delta$].
Comment: You already said you have $L^{1}$ convergence to the right limit. You cannot have different r.v.'s $Y$ and $Z$ such that $X_n \to Y$ in $L^{1}$ and $X_n \to Z$ a.s. @user250236
Comment: Oh,both $L^1$ and $\text{a.s.}$ convergence imply convergence in probability.So the limits, if they exist, must to be the same. THX!
Comment: Yes, that is right. My answer is not the shortest, but I think it contains additional information about $(E(X_n|\mathcal F_k))$. @user250236
Comment: @geetha290krm I might have missed something, but how do you check that $(\mathbf E(X_n\mid\mathcal F_k))$ is a martingale? (It surely is as a process in $k$, but I don't see it as a process in $n$.)
Comment: @nejimban I meant to say it is a sub/super-martingale according as $(X_n)$ is a  sub/super-martingale.
Comment: Based on my understanding,$$(1).\sup _{n \in \mathbb{N}_0} E\left(\left|X_{n}\right|\right)<\infty + (2).\mathbf{P}(|\mathbf{E}(X_n\Big|\mathcal F_k)|>\Delta)\le \frac 1 {\Delta} \mathbf{E}|X_n|(\text{the Markov's inequality})\Rightarrow$$   the $\left\{E(X_n|\mathcal F_k)\right\}_{n\in\mathbb{N}_0} $ is uniformly integrable. And then,using *Doob's Martingale Convergence Theorem (discrete case)* again, it a.s.convergence.  Those as you said.
Comment: Until now,we only know that $\left\{\mathbf{E}(X_n|\mathcal F_k)\right\}_{n\in\mathbb{N}_0} $ converges almost surely to some random variable  $Y,$so next step we need further explain $\mathbf{E}(X_\infty\big|\mathcal{F_{k}})=Y\text{ a.s.}$ But to explaining that,we will back to  the method given by @Davide Giraudo.
Comment: Just a side note: $(\mathbf E(X_n\mid\mathcal F_k))$ is uniformly integrable because it converges in $L^1$ (as OP said: "$X_n\to X_\infty$ in $L^1$ implies $\mathbf E(X_n\mid\mathcal{F_{k}})\to\mathbf{E}(X_{\infty}\mid\mathcal{F_{k}})$ in $L^1$").
Comment: $k$ is fixed. It is a martingale in $n$.  @nejimban
Comment: Restrict to $n \geq k$. @nejimban
Comment: @geetha290krm So I suppose you assumed that $(X_n)$ were itself a martingale to start with? Otherwise, consider the submartingale (bounded in $L^2$) $X_n:=B_1+\cdots+B_n$ with independent $B_k\sim\text{Bernoulli}(k^{-2})$ and the natural filtration. Then $Y_n:=\mathbf E[X_n\mid\mathcal F_1]=B_1+\sum_{k=2}^nk^{-2}$ (for $n\ge1$) cannot be a martingale because $Y_{n+1}-Y_n={(n+1)}^{-2}$.


Forward *.Company.com to an IP address

Question: I want to forward all *.company.com URLs to my company.com IP address. Is this possible with Godaddy DNS? The reason I want this is so that I can deliver different logos based on the company using my application (eg. ibm.company.com, bell.company.com etc. will see a respective IBM and Bell logo at the top). I need the prefix so that my application will remain branded even without a user session on the system, such as on the logon, registration and password recovery screen. So I am also open to a criticism of this idea for as a branding solution, and a discussion of a better solution... [using ASP.NET]
Comment: Move to ServerFault?
Comment: Its certainly nothing to do with html or css... might be possible with c# / .net though but not that I know of...
Comment: @ClarkeyBoy: Shouldn't be - .net wouldn't get any request if the dns isn't pointing in the right direction...
Answer: You want to add a cname record in the total dns settings
Host
use *
Points to
company.com IP address
Then just check the host with asp.net
Answer: http://scottelkin.com/programming/how-to-properly-configure-godaddy-dns-settings/

Forward *.Company.com to an IP address


Selenium no Node.js, WebDriverError como resolver?

Question: Estou usando o windows, usando o node.js com o selenium para fazer automação de um site. meu problema e que eles esta dando erro na função click().
Código abaixo e o que vem de exemplo ja pronto quando digita npm install selenium-webdriver.

const webdrive = require('selenium-webdriver'), By = webdrive.By, until = webdrive.until;

var driver = new webdrive.Builder().forBrowser('chrome').build();

driver.get('http://www.google.com/ncr')
    .then(_ =>
        driver.findElement(By.name('q')).sendKeys('wiki'))
    .then(_ =>
        driver.findElement(By.name('btnK')).click())
    .then(_ => driver.wait(check_title, 1000));

function check_title() {
    var promise = driver.getTitle().then(function (title) {
        if (title === 'wiki - Google Search') {
            console.log('success');
            return true;
        } else {
            console.log('fail -- ' + title);
        }
    })
    return promise;
}

Segue agora o erro que me retornou...

    DevTools listening on ws://127.0.0.1:54874/devtools/browser/78c2868b-41a2-4d72-8da9-95f6a3fd05ec //Aqui ele abriu o navegador e escreveu no bampo de busca.
(node:924) UnhandledPromiseRejectionWarning: WebDriverError: element not interactable // aqui ele deu erro na parte do click() e saiu dando erro no resto...
  (Session info: chrome=73.0.3683.75)
  (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT
10.0.17134 x86_64)
    at Object.checkLegacyResponse (D:\Automacao site - Selenium\node_modules\selenium-webdriver\lib\error.js:585:15)
    at parseHttpResponse (D:\Automacao site - Selenium\node_modules\selenium-webdriver\lib\http.js:533:13)
    at Executor.execute (D:\Automacao site - Selenium\node_modules\selenium-webdriver\lib\http.js:468:26)    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:924) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:924) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
PS D:\Automacao site - Selenium>
Answer: Consegui resolver usando:
<code>const { Builder, By, Key, until } = require('selenium-webdriver');

var driver = new Builder().forBrowser('chrome').build();

driver.get('http://www.google.com/ncr')
    .then(_ =>
        driver.findElement(By.name('q')).sendKeys('wiki', Key.RETURN))
    .then(_ => driver.wait(check_title, 1000));

function check_title() {
    var promise = driver.getTitle().then(function (title) {
        if (title === 'wiki - Google Search') {
            console.log('success');
            return true;
        } else {
            console.log('fail -- ' + title);
        }
    })
    return promise;
}

</code>


Local String-variable initialization in Loop

Question: I'm facing a strange behavior with vb.net (framework 4.8,VS2019, Option Explicit and Strict On) and a string variable inside a For Each loop.
Why the tmpVal variable is not reinitialized on each loop, or may I've missed something?
Here is my code :
Sub Main()
    Dim Props() As String = {"one", "two", "three"}
    For Each cProp As String In Props
        Dim tmpVal As String
        If cProp = "two" Then
            If String.IsNullOrEmpty(tmpVal) Then
                tmpVal = cProp
            Else
                tmpVal = "Nope"
            End If
        End If
        Console.WriteLine("cProp " & cProp & " : " & tmpVal)
    Next
    Console.ReadKey()
End Sub

and the output I get :
cProp one :
cProp two : two
cProp three : two

I was expecting the third row to be "empty"
Thank you
Comment: You only assign `tempVal` once when `cProp = "two" And String.IsNullOrEmpty(tmpVal)`. Otherwise it's never updated.
Answer: Declaring a variable inside a loop does not mean it will be initialized on each iteration.
It just limits the scope to the loop.
Means it will be initialized once to Nothing in your example.
You can change the initialization as follows to get the expected result:
Dim tmpVal As String = ""

Comment: Thank you  you're right.  the related info at Microsoft if it can help: https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/declared-elements/scope
Comment: I believe the String, being a reference type, is initialized to Nothing.
Comment: @Mary You're right. I got confused because of this: https://stackoverflow.com/questions/2633166/nothing-string-empty-why-are-these-equal


How to move a robot in a box using python?

Question: I have a box of 8x8 dimension.
It has some lines that indicate the ideal path that the robot should follow.
create a bounding box around the area so the robot cannot escape.
Pseudo Code
import math
import random
import pygame
direction = {'up': (0, -1), 'down': (0, 1), 'left': (-1, 0), 'right': (1, 0)}
direction = direction['up']                   # set a direction

Comment: Could you give more information about the problem? you want to apply the path to a matrix or what? You aren't telling us anything about your problem
Comment: What have you tried so far? Where do you stuck? What is your basic idea? What are your data structures (`box`, lines, path)? What is a robot in this context?
Comment: This question can be improved in a number of ways. Most importantly, it needs a clearer explanation of the problem. To be a _good_ SO question, it should also clearly describe what the asker has already tried, and the ways in which that attempt(s) failed.
Comment: is it a matrxi user? if so please comment so that we can start working on it
Answer: You found a fun area to explore!  Your question leaves out a lot of details, so I will try to give some advice about solving these general problems.

You are using a character grid. Use '.' to signify an empty position; using a space leads to longer debugging times.
You should think of your grid variable like grid[line][col].  If you try to use [y][x], [x][y], [row][col] then you will get confused and create bugs for yourself.
Make this program in lots of small steps.  At each step you should be able to run it and see some output.  You might do these steps:

Write make_blank_grid and print_grid.  You should be able to print an empty grid.
Write make_grid_from_string and get_string_from_grid so that you can pass a long string to make different grids.  Put all this inside a Grid class.
Start your Robot class so that your robot knows its own line and column.  Write dump_robot to just print these out.
Start a World class that has a Robot and a Grid.  This class should have a way to print the robot on the grid (dump_world).
Add a stupid (no error checking) move(delta_line, delta_row) function, make a simple loop to make your robot move around (but destroying walls).
Now, implement walls in the Grid and add can_move() to your robot. By re-running the loop, the Robot should refuse to move if there are walls in his path.
Once you have all this, you can work on adding mazes, maze solving, finding paths, etc.  If any step starts getting frustrating, break it down.

Get the logic to work first.  I recommend using simple print and input from I/O until you get your Robot, Grid, and World classes working.
Initialize your grid correctly.  That is: grid = [['.' for col in range(COLS)] for line in range(LINES)] not grid = [' ' * COLS]*LINES

Keep working at it.  Keep hacking!  Keep notes.


Describe the non-trivial periodic orbit of a system of ode's

Question: Consider the system
$\frac{dx}{dt}=y+ax-x(x^2+y^2),\;x(0)=x_0$
$\frac{dy}{dt}=-x+ay-y(x^2+y^2),\;y(0)=y_0$
The question is describe for $a> 0$ the non-trivial periodic orbit and determine its period.
My ideas:
Where, the fixed point is $(x,y)=(0,0)$ and using the polar coordinates, I obtained the following system of ode's
$\frac{dr}{dt}=ar-r^3$
$\frac{d\theta}{dt}=-1$.
If $\frac{dr}{dt}<0$ then $r\geq \sqrt{a}$ and if $\frac{dr}{dt}>0$ then $0<r\leq \sqrt{a}$.
From here I don't know how to continue to find the orbit.
Thanks for your help.
Answer: Hint: You almost have it.  $dr/dt = 0$ if $r = \sqrt{a}$.
Comment: From here, I can find $x(t)$ and $y(t)$ in function of $r=\sqrt{a}$?
Comment: In polar coordinates, $r = \sqrt{a}$ is constant, $\theta = const - t$.  Convert back to cartesian.
Answer: From
$$
\cases{
\frac{dr}{dt}=ar-r^3\\
\frac{d\theta}{dt}=-1
}
$$
with $\theta = t_0-t$ we have after integration
$$
\frac{\ln \left(r_1^2-a\right)-\ln \left(r_2^2-a\right)-2 \ln (r_1)+2 \ln (r_2)}{2
   a} = t_2-t_1 = -2\pi
$$
and solving for $r_1$
$$
r_1 = \frac{\sqrt{a} e^{2 \pi  a} r_2}{\sqrt{e^{4 \pi  a} r_2^2+a-r_2^2}}
$$
and $r_1 = r_2 = \sqrt a$
NOTE
From
$$
\cases{
x = r(t)\cos(\theta(t))\\
y = r(t)\sin(\theta(t))\\
\theta(t) = t_0 -t
}
$$
we have $T = -2\pi$ so along any orbit we have
$$
\frac{\ln \left(r_1^2-a\right)-\ln \left(r_2^2-a\right)-2 \ln (r_1)+2 \ln (r_2)}{2
   a} = t_2-t_1 = T
$$
and solving for $r_1$
$$
r_1 = \frac{\sqrt{a} r_2}{\sqrt{\left(a-r_2^2\right) e^{2 a T}+r_2^2}}
$$
so along a limit cycle, after a period $T$ we have $r_1 = r_2\Rightarrow r_1 = r_2 = \sqrt{a}$
characterizing a circle with radius $\sqrt a$ and period $T$
Comment: I'm sorry. I can't understand your explanation.
Comment: @AlexPozo Please. See note attached.


Difference between model_weights and optimizer_weights in keras

Question: What is difference between model_weights and optimizer_weights in keras. after running following code model.summary shows total 9 parameters, which shows in model_weight in 1.h5 file. But optimizer_weight shows total 18 parameters. I used only 1 epoch. Code is following:
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
import numpy as np
from sklearn.model_selection import train_test_split
import tensorflow as tf
batch_size = 128
num_classes = 2
epochs = 1

# input image dimensions
img_rows, img_cols = 28, 28
(x_train, y_train), (x_test, y_test) = mnist.load_data()

#Redistributing data for only two classes
x1_train=x_train[y_train==0]; y1_train=y_train[y_train==0]
x1_test=x_test[y_test==0];y1_test=y_test[y_test==0]
x2_train=x_train[y_train==1];y2_train=y_train[y_train==1]
x2_test=x_test[y_test==1];y2_test=y_test[y_test==1]
X=np.concatenate((x1_train,x2_train,x1_test,x2_test),axis=0)
Y=np.concatenate((y1_train,y2_train,y1_test,y2_test),axis=0)
# the data, shuffled and split between train and test sets
x_train, x_test, y_train, y_test = train_test_split(X,Y)

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Conv2D(1, kernel_size=(2, 2),
                 activation='relu',
                 input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(16,16)))
model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])
model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
model.summary()
model.save('1.h5')

Answer: Model weights are weights that act on the actual data.
They will affect the output.
A model alone (without an optimizer) is enough to take an input and produce (predict) an output. The better the model's weights, the better the output.
The whole purpose of training a model is to adjust its weights so it can make good predictions.
An optimizer, on the other hand, has no influence on data and predictions.
The role of the optimizer is decide how to change the model's weights during training. I purely for training purposes. The optimizer gets the gradients and decide how to apply these gradients to the model. (Considering learning rates, momentum, etc.)
The optimizer weights are just helpers to improve the adjustment of the model's weights. Once you consider that your model is doing a good job, you can throw the optimizer away.
Comment: But why model_weights are 9 and optimizer_weights are 18 in number in above example. (i am using epoch=1). And suppose i am not using Gradient Based optimizers (using swarm based) then which weight i have to update, model_weights or optimizer_weights??
Comment: I don't know how swarm optimizers work... But I think it takes cares of it's own weights... You can try to increase your model and see what happens to the optimizer weights.
Comment: I understand. The only confusion is, why no of parameters for models_weights and optimizer_weight are different (for above example 9 and 18, i got from model.save)
Comment: Why should they be equal?? It looks like this optimizer has 2 per model's weight. (But check this info).
Comment: from keras documentation i think 9 parameters for loss and 9 for optimizers. line number 69 in


Co-variance of two random variables

Question: Let $X$ and $Y$ be jointly distributed random variables such that $Y|X=x \in Bin(n,x)$ with $X \in U(0,1)$. We must find $E(Y)$, $Var(Y)$ and $Cov(X,Y)$.
I was able to complete the first two tasks, but I ran into trouble on the co-variance.
I used the fact: $ Cov(X,Y) = E[ (Y-E(Y))(X-E(X)) ] = E(XY) - E(X)E(Y) $
I know $E(Y) = \frac{n}{2}$ and $E(X) = \frac{1}{2}$ so the second term must be $\frac{n}{4}$. I know the answer is $\frac{n}{12}$ so the first term must come out to be $\frac{n}{3}$. I've tried many eclectic strategies, but nothing worked.
Thank you for your input!
Comment: what is "eclectic strategies"?
Comment: https://en.wikipedia.org/wiki/Law_of_total_expectation
Answer: Using law of total expectation,
\begin{align}
E[XY] &= E[E[XY|X]] \\
&= E[XE[Y|X]] \\
&= E[X(nX)] \\
&= n E[X^2] \\
&= n \int_0^1 x^2 dx \\
&=\frac{n}{3}
\end{align}
Comment: $Y|X$ follows binomial distribution with parameter $n$ and $X$ hence the mean is $nX$.
Comment: That makes sense! Though it's not obvious to me how we go from $E[Y|X] \to (nX)$
Answer: $E(XY)=EE(XY|X)=E(XE(Y|X))=E(X(nX))=nE(X^2)=\dfrac{n}{3}$

Co-variance of two random variables


How to Get a List of Logged in Users?

Question: I don't know how to narrow down this question further: We are using Auth0 and WebApi 2. I need to display a list of all users who are currently logged in. More specifically, Auth0 issues a token, this token is then sent with every request to our WebApi. I imagine, I need to write the token and the associated id to the database every time a request is made to any controller with the specific token? Can someone give me a general idea of whether or not I am on the right path here or what I should be reading?
Auth0 docs are of little help, since this seems to be an unusual requirement. Same with: http://www.asp.net/web-api/overview/security
I am not concerned about clientside. Just need the way to go on the server.
Edit: I added another part to this question in the comments: I would also like to know how to revoke tokens, effective immediately. To understand this, I believe I need to understand exactly how the backend verifies the token sent by the front end, and whether or not Auth0's server gets called for this at all.
Comment: Usually you use an authentication module which handles that aspect and places the pertinent details into a shared location where your logic can access them.
Comment: https://auth0.com/docs/sequence-diagrams this is the chain of events - from what I understand, it doesn't get saved anywhere, it just verifies if the user is authorized to perform the action, and that's that. (API isn't hit at all until Auth0 has issued a token and user makes some API call).
Comment: So, I would like to clarify - the answer is "There is some magical magic that Auth0 should probably provide, maybe does or doesn't?" I am not being sarcastic and appreciate any help, but it seems to me that there should be some way to react to the API being hit and get the token from there.
Comment: See if Auth0 supports OAuth and if so there are a lot of modules available that link up MVC and OAuth.
Answer: I'm a Developer Advocate at Auth0. Let me see if I can help you out :).
The whole idea of having an API that just checks the JWT is to have a Stateless API. Being "logged in" doesn't really exist in concept. All JWTs expire at some point in time, and that's what checked when you call an API.
Therefore, what I suggest is the following:
Every time you get an API call you save the JWT somewhere on memory. Then, we need to get logged in users, you just grab all the JWTs that you have on those list, and show the not expired ones as "logged in". Also, you should have a cron that goes every 5 minutes over the list and which cleans expires JWTs. It's not really "logged in" users, but I think it's close enough.
Would that work?
Thanks
Comment: Hey, you're right. The JWT is yours. In order to log them out, you'd have to store in your DB one ID per JWT that you receive so that you can blacklist them, and then before letting the user in with the middleware, you have to check that the JWT isn't blacklisted on that DB.
Comment: Thanks for the reply mgonto. I considered something similar as a potential solution, and it's good to see you confirm it. However, there is another issue - the project has a bit of a weird requirement: we need to be able to get a list of all users with current access (let's consider this solved) and force them out of the system (individually), effective immediately. From what I understand (and this is critical), once Auth0 ISSUES the token, Auth0 is NOT involved in checking it. I.e. the client has the token (after getting it from Auth0) and sends it with requests...
Comment: ...Our WebAPI/OWIN/whatever backend receives the request, checks the token for validity (WITHOUT calling Auth0's server) and decided whether or not the token is valid. If that's the case, I don't understand what blacklisting a token does (https://auth0.com/blog/2015/03/10/blacklist-json-web-token-api-keys/). If I am wrong, and Auth0 is involved in CHECKING the token, then this whole thing just got a lot easier since I can just make an API call to blacklist it. Thanks again for any help, I will check this throughout the day tomorrow.

How to Get a List of Logged in Users?


How to call User::load with dependency injection

Question: I have code that worked, but I wanted to clean it up and use dependency injection. I'm working in a BlockBase that implements ContainerFactoryPluginInterface.
class DefaultBlock extends BlockBase implements ContainerFactoryPluginInterface {

I need to access some information from the user object. Right now I'm using Drupal\Core\Session\AccountProxyInterface. This works fine, but doesn't give me all the information I was expecting. I have a contrib module adding a field to the user. I can access that field if I use User::load($id) however, I get the warning that...

User::load calls should be avoided in Drupal classes, use dependency
  injection instead

So I'm trying. And failing.
This code works...
$userIdx = \Drupal::currentUser()->id();
$userx = \Drupal\user\Entity\User::load($userIdx);
$countryx = $userx->country_iso_code_2;

But when I clean that up and do it the right way, I can't get that country_iso_code_2 information.
Here is the code I'm "trying" to work with...
<?php

namespace Drupal\country_selector\Plugin\Block;

use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a 'DefaultBlock' block.
 *
 * @Block(
 *  id = "default_block",
 *  admin_label = @Translation("Country switcher"),
 * )
 */
class DefaultBlock extends BlockBase implements ContainerFactoryPluginInterface {

  protected $account;

  /**
   * Class constructor.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $account) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->account = $account;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('current_user')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $build = [];

    $userIdx = \Drupal::currentUser()->id();
    $userx = \Drupal\user\Entity\User::load($userIdx);
    $countryx = $userx->country_iso_code_2;

    $userId = $this->account->id();
    $country = $this->account->country_iso_code_2;
    .....

When I debug, I see that:

$userx is Drupal\user\Entity\User
$user is Drupal\core\Session\AccountProxy

So I get that I'm working with two different things. I just don't understand how I'm supposed to call something like User::load with dependency injection.
I hope that makes sense. Would love some advice!
Answer: You should use entity_type_manage to get the user storage. Something like this:
<?php

namespace Drupal\country_selector\Plugin\Block;

use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\user\UserStorageInterface;
/**
 * Provides a 'DefaultBlock' block.
 *
 * @Block(
 *  id = "default_block",
 *  admin_label = @Translation("Country switcher"),
 * )
 */
class DefaultBlock extends BlockBase implements ContainerFactoryPluginInterface {

  protected $account;
  protected $userStorage;

  /**
   * Class constructor.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $account, UserStorageInterface $user_storage) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->account = $account;
    $this->userStorage = $user_storage;
  }

  /**
   * {@inheritdoc}
  */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('current_user'),
      $container->get('entity_type.manager')->getStorage('user')
    );
  }

  /**
   * {@inheritdoc}
  */
  public function build() {
    $build = [];

    $userIdx = $this->$acount->id();
    $userx = $this->userStorage->load($userIdx);
    $countryx = $userx->country_iso_code_2;
    .....

Hope this helps.
Comment: Line ```$userIdx = $this->$acount->id();``` should be ```$userIdx = $this->account->id();```
Comment: That definitely helps. Thank you. I owe you a beer!


Erro registered the JDBC driver [oracle.jdbc.driver.OracleDriver] but failed to unregister

Question: Estou fazendo um sistema JavaWeb usando jsp que está nesse tópico: Dúvida em consulta banco de dados, erro na consulta, e em uma consulta com o banco de dados, me deparei com um erro de driver que está logo abaixo. Uso o IDE netbeans com servidor glassfish e o banco de dados Oracle.
Erro:

Advertência: The web application [/rodoviario] registered the JDBC driver [oracle.jdbc.driver.OracleDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

Classe de conexão:
public class Conexao {

private Connection conexao;

public Conexao() {
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
        conexao = DriverManager.getConnection(url,"rafilds","1234");
  } catch (Exception e) {
        e.printStackTrace();
    }
}

public Connection getConexao() {
    return conexao;
}
}

Comment: relacionado a http://stackoverflow.com/questions/3320400/to-prevent-a-memory-leak-the-jdbc-driver-has-been-forcibly-unregistered
Comment: parece ser apenas um warning, nao um erro
Answer: Problema resolvido, isso não era erro como dito pelo Pedro, é um warning, e o problema era no insert do banco de dados.
Comment: Será que poderia complementar sua resposta. Ela poderá ajudar mais pessoas no futuro. Lembrando também que você pode aceitar sua própria resposta.


computing the second derivative of $F(t)=f(a+t(x-a))$

Question: Let $t\in \mathbb{R}$ and $F(t)=f(a+t(x-a))$, where $x\not = a$ and $x,a\in \mathbb{R}^n$. I need computing $F''(t)$.
Well by chain rule, $F'(t)=(x-a) \cdot \nabla f(a+t(x-a))$ but what is $F''(t)$
Answer: Define a curve $\varphi: \mathbb{R} \rightarrow \mathbb{R}^n$ by
$$
\varphi(t) := a + t(x-a).
$$
You already showed that
$$
F'(t) = Df(\varphi(t))\cdot \varphi'(t).
$$
$\varphi'(t)$ is constant and can thus be ignored when differentiating. Just apply the chain rule again:
$$
F''(t) = (D^2 f(\varphi(t) \cdot \varphi'(t))) \cdot \varphi'(t) = (x-a)^\top D^2(a+t(x-a)) (x-a)
$$
Answer: Let $\mathbf{y}=\mathbf{a}+t(\mathbf{x-a})$.
The differential writes
$d\mathbf{y}=(\mathbf{x-a}) dt$
Then
$F'(t)=\mathbf{g}^T(\mathbf{x-a})$ where
$\mathbf{g}=\frac{\partial f}{\partial \mathbf{y}}$.
From
$d\mathbf{g}
=\mathbf{H}_f d\mathbf{y}
=\mathbf{H}_f (\mathbf{x-a}) dt$
it holds
$F''(t)
=(\mathbf{x-a})^T \mathbf{H}_f (\mathbf{x-a})$.
Answer: Suppose we are in Euclidean coordinates so $x=(x_i)_{i=1}^n$ so that $\nabla f = \left(\frac{\partial f}{\partial x_i}\right)_{i=1}^n$. Then
$$\frac{d}{dt} (\nabla f) ( a + t(x-a)) = \left( \frac{d}{dt} \frac{\partial f}{\partial x_i}(a + t(x-a))\right)_{i=1}^n= \left(\sum_{j=1}^n\frac{\partial^2f}{\partial  x_i\partial x_j}(x-a)_j\right)_{i=1}^n=H(f) (x-a) $$ where $H(f) = \left(\frac{\partial^2f}{\partial  x_i\partial x_j}\right)_{i,j=1}^n$ is the Hessian matrix of $f$. Therefore we have
$$F''(t) = (x-a)\cdot \frac{d}{dt}\nabla f(a+t(x-a)) =(x-a)^T \frac{d}{dt}\nabla f(a+t(x-a)) = (x-a)^T H(f) (x-a) $$
So $$F''(t) = (x-a)^T H(f) (x-a)$$
Answer: $F$ is $f$ along the slice given by $a+t(x-a)$. As such the dericative to $F$ in $t_0$ is the derivative to $f$ in the point $a+t_0(x-a)$ and the direction $(x-a)$, which is an interpretation of your chain rule thing. Similarly the second derivative to $F'$ will be the second derivative of $f$ in $a+t_0(x-a)$ twice in the direction of $(x-a)$. If $f$ has a second total derivative $D_2$ (that is a bilinear map that maps $v,w$ to the second order derivative of $f$ in directions $v,w$) we can write this as $D_2(x-a)(x-a)$.
Similar to $D_1$ being given by $\nabla f$ we can also under the assumption that our functions are nice take $D_2$ to be the hessian $\mathrm Hf$ to $f$.
Comment: What is your explicit answer about $F''(t)$?
Comment: @TheMartian Well, as long $f$ is nice enough it is $\mathrm H_f(a+t_0(x-a))(x-a)(x-a)$.
Comment: @TheMartian Which is the quadratic form of $\mathrm H_f(a+t_0(x-a))$ in $(x-a)$.
Comment: @TheMartian What I don't get is why anyone would vote this down and all later answers up, when my answer just states the same, just more generalised. I just do not understand the dynamics of this site, sometimes people are getting tons of upvotes for total shit answers that do not in fact answer the point behind the whole thing, just because the answer sounds good. I think I'll just quit SE at some point, it's just so frustrating!


JSON retorna null ao tentar pegar informaçoes no banco via PHP

Question: Estou tentado pegar informaçoes do banco com php e passar para JSON, porem ele sempre vem vazio retorna null e sem nenhum erro.
[
    {
        "current_field": null,
        "field_count": null,
        "lengths": null,
        "num_rows": null,
        "type": null
    }
]

Abaixo o código PHP:
<?php

require 'conexao.php';

$temperatura = "SELECT * FROM estufas";

$resultado_temperatura = mysqli_query($conexao,$temperatura)or
die(mysqli_error($conexao));

if (mysqli_num_rows($resultado_temperatura) > 0) {

    $dados[] = $resultado_temperatura;

    echo json_encode($dados, JSON_PRETTY_PRINT);
}

?>

Answer: Consegui resolver, o código ficou assim:
<?php

require 'conexao.php';

$array = array();

$resultado = "SELECT Temperatura_Estufa,Umidade_Estufa FROM estufas";

$res = mysqli_query($conexao,$resultado)or die(mysqli_error($conexao));

while ($rows = mysqli_fetch_assoc($res)) {
$dados[] = array('Temperatura_Estufa' => $rows['Temperatura_Estufa'],
                 'Umidade_Estufa' => $rows['Umidade_Estufa'],);
}
echo json_encode($dados);
?>


Accessing a static variablefrom a class instance

Question: I would like to use this line of code:
$this->ClassInstance::$StaticVariable;

Where this is the basic setup:
Class foo{
   public static $StaticVariable;
}

$this->ClassInstance = new foo();

What is the reasoning behind this being a parse error?
When this:
$ClassInstance = $this->ClassInstance;
$ClassInstance::$StaticVariable;

Is perfectly valid.
Answer: Assuming you meant
Class Foo{
    public static $StaticVariable = 42;
}

class Bar {
    private $ClassInstance;

    public function fn() {
        $this->ClassInstance = new Foo();
        return $this->ClassInstance::$StaticVariable;
    }
}

$bar = new Bar;
echo $bar->fn();

Then this works as of PHP7. Prior to that, the parser could simply not dereference this. See the wiki page linked in the comments to your question for details.
You could use get_class($this->ClassInstance)::$StaticVariable; though.
Answer: In PHP docs

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

source http://php.net/manual/en/language.oop5.static.php
Answer: You can access it with self::$staticVariable or foo::$staticVariable. No need to make use of the instance.


TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str

Question: Estou tentando traduzir um codigo de Python 2.7 para 3.6
O codigo 2.7 funciona perfeitamente:
from urllib2 import Request, urlopen

values = """
   {
    "exchange_code": "PLNX",
    "exchange_market": "BTC/USDT"
   }
"""

headers = {
'Content-Type': 'application/json',
'X-API-KEY': 'xxxxxx',
'X-API-SECRET': 'yyyyyy'
}

request = Request('https://api.coinigy.com/api/v1/ticker', data=values, headers=headers)
response_body = urlopen(request).read()
print response_body

O codigo para 3.6 :
from urllib.request import Request, urlopen

values = """
   {
    "exchange_code": "PLNX",
    "exchange_market": "BTC/USDT"
   }
"""

headers = {
'Content-Type': 'application/json',
'X-API-KEY': 'xxxxxx',
'X-API-SECRET': 'yyyyyy'
}

request = Request('https://api.coinigy.com/api/v1/ticker', data=values, headers=headers)
response_body = urlopen(request).read()
print(response_body)

Retorna o seguinte erro :

TypeError                                 Traceback (most recent call last)
   in ()
        1 request = Request('https://api.coinigy.com/api/v1/markets', values, headers=headers)
  ----> 2 response_body = urlopen(request)
        3 print(response_body)
C:\Users\Marcelo\Anaconda3\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
      221     else:
      222         opener = _opener
  --> 223     return opener.open(url, data, timeout)
      224
      225 def install_opener(opener):
C:\Users\Marcelo\Anaconda3\lib\urllib\request.py in open(self, fullurl, data, timeout)
      522         for processor in self.process_request.get(protocol, []):
      523             meth = getattr(processor, meth_name)
  --> 524             req = meth(req)
      525
      526         response = self._open(req, data)
C:\Users\Marcelo\Anaconda3\lib\urllib\request.py in do_request_(self, request)
     1246                 msg = "POST data should be bytes, an iterable of bytes, " \
     1247                       "or a file object. It cannot be of type str."
  -> 1248                 raise TypeError(msg)
     1249             if not request.has_header('Content-type'):
     1250                 request.add_unredirected_header(
TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.

Pelo que eu entendi eu tenho que passar os parametors para urllib.request no formato bytes e nao no formato str, mas nao sei qual a forma correta de converter os valores.
Obrigado pela ajuda.
Answer: Tente esse código:
from urllib.request import Request, urlopen

values = """
   {
    "exchange_code": "PLNX",
    "exchange_market": "BTC/USDT"
   }
"""

headers = {
    'Content-Type': 'application/json',
    'X-API-KEY': 'xxxxx',
    'X-API-SECRET': 'yyyyyy'
}

request = Request('https://api.coinigy.com/api/v1/ticker', data=values.encode('utf-8'), headers=headers)
response_body = urlopen(request).read()
print(response_body)

O problem aparentemente estava no jeito que você estava lidando com o dicionário. Era necessário codificar os valores antes.
Da documentação (inglês):

For an HTTP POST request method, data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.parse.urlencode() function takes a mapping or sequence of 2-tuples and returns an ASCII string in this format. It should be encoded to bytes before being used as the data parameter.

Tradução do trecho destacado:

O parâmetro deve ser convertido para bytes antes de ser usado como
  parâmetro da variável data.
Comment: poderia traduzir esse trecho destacado em ingles?
Comment: @diegofm pronto. Melhor?
Comment: A msg de erro não aparece mais no entanto a resposata da chamada é :

b'{"err_num":"1057-32-01","err_msg":"Missing or empty parameters:"}'
Comment: @RiskTech Acabei de testar no Python 3.6.0 e obtive essa resposta: b'{"data":[{"exchange":"PLNX","market":"USDT\\/BTC","last_trade":"1115.0200000000","high_trade":"1123.4000000000","low_trade":"1026.1450002000","current_volume":"8157.2646000000","timestamp":"201
7-03-21 20:23:53","ask":"1115.9999999900","bid":"1115.0100000000"}],"notifications":[]}'
Pode testar novamente?
Comment: Funcionou realmente, pode ser que o feed estava derrubado quando testei. Muito obrigado


Print a png file however can't seem to print

Question: Good day! I was trying to print a png file using a Picturebox as my button.
However, I can't seem to print. Could you please help me? or Give me a link that will guide me on how to print on a default printer using C# VS 2010
private void pictureBox2_Click(object sender, EventArgs e)
{
    using (PrintDocument pd = new PrintDocument())
    {
        using (PrintDialog printDialog = new PrintDialog())
        {
            if (printDialog.ShowDialog() == DialogResult.Yes)
            {
                pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                pd.Print();
            }
        }
    }
}

private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

    System.Drawing.Image img = Image.FromFile(@"C:\Coke\res10.png");
    e.Graphics.DrawImage(img, 0,0);
}

Comment: What is error you are getting?
Comment: Have you resolved your problems?
Answer: Found a solution for this.
 PrintDocument pd = new PrintDocument();
 pd.PrintPage += (sender, args) =>
 {
       Image i = Image.FromFile("C://tesimage.PNG");
       args.Graphics.DrawImage(i, args.PageBounds);

 };
 pd.Print();

Answer: This is a simple one, but easy to miss..
You are doing just about everything right. Just one tiny thing is not 'OK':
You need to change
if (printDialog.ShowDialog() == DialogResult.Yes)

to
if (printDialog.ShowDialog() == DialogResult.OK)

It is not a question, after all, so it shows 'OK' and 'Cancel' not 'Yes' and 'No'.


XML cannot be the whole program

Question: I am using jquery to make an $.ajax() call to a REST web service. Based on the documentation I found, I need to use jsonp as the datatype in order to make this cross domain call (an XML document is what is actually returned). When running my code, however, I receive the error "XML cannot be the whole program".
Does anyone know what this error means and how to fix it (if at all)? Google searches have not provided much information and the other stack overflow post on this subject was not helpful to me either.
If you have additional questions, please let me know.
Comment: Could you elaborate on where the error is coming from?  Is it the response from the webservice?
Comment: i dont think that you need jsonp if the response type is xml
Comment: Firebug is what is showing me this error.
Comment: If I change the response type to anything but jsonp, i get a "Access to restricted URI denied" error as my response.
Answer: Looks to me like you included a *.js file in your web app that contains  around the code...which is not needed. Those tags are making your code look like one big XML document to the parser.
Comment: I apologize if I am not getting this, but can you clarify your answer a bit? I don't think I understand what you mean by the js include being contained around the code.

Thanks.
Comment: here's my code:

JavaScript Practice

Click to Test

my scripts.js file does not contain any
Comment: Can you post the relevant piece of your script.js file so we have an idea of what's going on there?
Answer: If you need to return XML over a connection that requires JSON, you will have to wrap your xml.  So, for example, if your document looks like this:
<magic8ball>
   <outcome_looks_doubtful/>
</magic8ball>

Then you will need to do something like this:
{"value": "<magic8ball>
    </outcome_looks_doubtful/>
</magic8ball"}

Then you are passing around JSON, like your jsonp datatype requires.  All you have to do is extract your xml, and you're off and running.

XML cannot be the whole program


Additional brackets in Java8 stream map() function

Question: I'm working/testing streams in Java8 and come across very frustrating issue.
I've got the code which compiles well:
        List<String> words = Arrays.asList("Oracle", "Java", "Magazine");
    List<String> wordLengths = words.stream().map((x) -> x.toUpperCase())
        .collect(Collectors.toList());

And second one (nearly the same) which throw a warnings:
        List<String> words = Arrays.asList("Oracle", "Java", "Magazine");
    List<String> wordLengths = words.stream().map((x) -> {
        x.toUpperCase();
    }).collect(Collectors.toList());

Warning:
The method map(Function<? super String,? extends R>) in the type Stream<String> is not applicable for the arguments ((<no type> x) -> {})

What does this additional brackets have changed?
Comment: If you are using the brackets and the return type of the lambda is not void, you have you to supply the `return` keyword in the lambda body (see also https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-LambdaBody)
Comment: that's not a `warning` but a compilation problem, *entirely* different things
Answer: Your lambda expression returns a value. If you use brackets you need to add a return statement to your lambda function:
List<String> words = Arrays.asList("Oracle", "Java", "Magazine");
List<String> wordLengths = words.stream().map((x) -> {
    return x.toUpperCase();
}).collect(Collectors.toList());

Answer: According to the official Oracle tutorial

A lambda expression consists of the following:
A comma-separated list of formal parameters enclosed in parentheses.
  The CheckPerson.test method contains one parameter, p, which
  represents an instance of the Person class.
Note: You can omit the data type of the parameters in a lambda
  expression. In addition, you can omit the parentheses if there is only
  one parameter. For example, the following lambda expression is also
  valid:
p -> p.getGender() == Person.Sex.MALE
    && p.getAge() >= 18
    && p.getAge() <= 25

The arrow token, ->
A body, which consists of a single expression or a statement block.
  This example uses the following expression:
p.getGender() == Person.Sex.MALE
    && p.getAge() >= 18
    && p.getAge() <= 25

If you specify a single expression, then the Java runtime evaluates
  the expression and then returns its value. Alternatively, you can use
  a return statement:
p -> {
    return p.getGender() == Person.Sex.MALE
        && p.getAge() >= 18
        && p.getAge() <= 25;
}

A return statement is not an expression; in a lambda expression, you
  must enclose statements in braces ({}). However, you do not have to
  enclose a void method invocation in braces. For example, the following
  is a valid lambda expression:
email -> System.out.println(email)

Since there is only one parameter in the provided lambda expression (x) -> x.toUpperCase() we can omit the parentheses: x -> x.toUpperCase(). String#toUpperCase returns a new String so there is no need to use return statement and braces. If instead we had a complex block with return statements we would have to enclose it into braces. Moreover in this case it is better to use Method Reference String::toUpperCase
List<String> wordLengths = words.stream().map(String::toUpperCase).collect(Collectors.toList());