How to setup for really deep convolution model

Question: I'm rather new to deep learning and convolution network and got some basic models to run. However, when I tried to build a deep CNN model (i.e., more than 14, 15 layers) the error rate does not seem to decrease even after several epochs. So I wonder what could be the problem and how to overcome it?
Comment: 15 layers is *really* *really* deep. Some of the most powerful networks around (like the one that breaks Captcha) are <10 layers.
Comment: Visualize the weights and see what they learned. If convolution + pooling layer is assumed as one layer, 14-15 layers seem to me too much. Posterior convolution layers may try to learn super-abstract features which are probably doesn't exist in the input images [open to discussion].
Answer: First, try an optimization method like Adagrad or Adadelta that are more robust to learning rates. Also, make sure your gradient updated are correct (e.g. scipy has a check_grad method).
Given that you have these two correct, I would then try to do greedy layer-wise pre-training in the first layers for some epochs. The reason is that your network is very deep and you will need proper initialisation to start training.
If you are using Torch7 you can have a look at this and use the ConvNet example as pre-training https://github.com/torch/demos/blob/master/train-autoencoder/train-autoencoder.lua.
Comment: Why only the first layer?
Comment: @Oxinabox not only one but, in general the first layers. You are dealing with a very high-dimensional optimisation problem. The intuition behind that is that you start discovering features in a greedy unsupervised way and then you fine-tune everything with supervised training. This helps you avoid many local minima, starting from your optimization from a better than random point.
Comment: I mean why not on all the layers? (except the final classification layer)
Comment: It's your choice, you want a reasonable initialisation, I would say just the first layers for speeding up the process, but both would work.


Negative R squared contradicts ssa/sst?

Question: I understand from this question -
When is R squared negative?
that the R squared value of a linear regression model can be negative if the intercept is constrained. And this makes sense if you define R squared as -
$$R^2 = 1-\frac{SSE}{SST}$$
One says $SSE>SST$. But then,
$$SST = SSA + SSE $$
Total Sum of squares  = Sum of squared errors + Sum of squared residual.
And with this we get -
$$R^2 = \frac{SSA}{SST}$$
And now it is hard to imagine how $R^2$ can be negative. Aren't SSA and SST >0 always?
Answer: $$
\begin{eqnarray*}
SST&=&\sum_{i=1}^n (y_i-\bar y)^2\\&=&\sum_{i=1}^n (y_i-\hat y_i+\hat y_i-\bar y)^2\\&=&\sum_{i=1}^n (y_i-\hat y_i)^2+2\sum_{i=1}^n(y_i-\hat y_i)(\hat y_i-\bar y)+\sum_{i=1}^n(\hat y_i-\bar y)^2\\&=&SSE+SSR+2\sum_{i=1}^n(y_i-\hat y_i)(\hat y_i-\bar y)
\end{eqnarray*}
$$
When an intercept($\beta_0$) is included in linear regression,
$$\hat{y}_i = \beta_0 + \beta_1x_{i,1} + \beta_2x_{i,2} +…+ \beta_px_{i,p}$$
It can be proved that $\sum_{i=1}^n(y_i-\hat y_i)(\hat y_i-\bar y)=0$ here
So
$$
SST=SSE+SSR+2\sum_{i=1}^n(y_i-\hat y_i)(\hat y_i-\bar y)=SSE+SSR
$$
$$
R^2 = 1-\frac{SSE}{SST}=\frac{SSR}{SST}\geqslant0
$$
However, when an intercept is not included in linear regression, $\sum_{i=1}^n(y_i-\hat y_i)(\hat y_i-\bar y)\neq0$
If $\sum_{i=1}^n(y_i-\hat y_i)(\hat y_i-\bar y)<0$, $SST=SSE+SSR+2\sum_{i=1}^n(y_i-\hat y_i)(\hat y_i-\bar y)$may be less than $SSE$
In this case,
$$
\frac{SSE}{SST}>1
$$
$$
R^2 = 1-\frac{SSE}{SST}<0
$$
Answer: I found a proof for SST=SSE+SSR in a different thread (link below). The proof relies on two equations derived from least square regression. If your regression was not not based on LS, those conditions would be violated and the equation SST=SSE+SSR wouldn't hold, which removes the contradiction of a negative R-squared.
https://math.stackexchange.com/questions/709419/prove-sst-ssessr
Comment: This is a good find, but reproducing the result here (with credit to the author) would probably garner you many upvotes.
Comment: Oh well, there are more important things in life than upvotes.
Answer: The point in the accepted answer to your linked question illustrates the answer to this.
If the regression model is so bad (for example because of a foolishly constrained intercept, but there are other potential causes) that a constant predictor (i.e. a horizontal line, with an intercept equal to the mean of the observed data) would fit the observed data better, then the sum of squares of the errors will exceed the sum of squares calculated from the original data (of the difference from the mean), and you will find $R^2$ going negative.
In such a case, you might want to think about whether the expression $SST = SSA + SSE$ is meaningful when the model is so bad that $SSE \gt SST$ and $SST$ represents the sum of squares calculated from the original data.
Unconstrained simple linear regression avoids this problem, since it just minimises $SSE$, and so $SST$ is an upper bound on $SSE$
Comment: SST = SSA + SSE should hold irrespective of the way linear regression has been fit and it should hold for all lines (not the best fitting one) shouldn't it?
Comment: @ryu576: Not if you can calculate SST before you do the regression (it is related to the variance of the dependent variable in the data).

Negative R squared contradicts ssa/sst?


pagination broken on custom theme's search system page

Question: I do not have pagination on the system search page .../search*.
When I toggle off my custom theme I have pagination on the search-result.tpl.php page, I can see the $pager variable. But on my custom theme I cannot find that variable, it doesn't show up. I have checked hook_query_alter() throughout all of my site and I do not see any conflict. I checked the template.php file and there is no issue there. I will begin to remove everything, piece by piece to find out if I can find a solution.
No real code to show, and I would send you a stock search-results.tpl.php page, but the code block doesn't want to show it because <?php ?> is interspersed with html tags. It is something like:
if ($search_results) {  print t('search results');   print $pager; }

I simplified it because of the aformentioned issue, but this is essentially it - it is so minimal it is laughable and it works for any other theme, but mine. So I just copy the search-results.tpl.php file over to bartik, then do drush cc -v all and recheck and I can use xdebugger to see the $pager variable, but the same file with my custom theme I never get the $pager variable.
Comment: So what exactly do you expect from us? Without code, or specific question there is not much for us to help with :)
Answer: So i found out that a view on the search page had pagination and it caused a conflict with the search results pagination! I removed pagination in the View's configuration screen.

Removed the pagination of the pager settings.


Find area of unit square using outer Hausdorff-measure

Question: We put $$\eta_{\delta}(E) = \inf\left\{ \sum_{i \in \mathbb{N}} \text{diam }U_i : E \subset \bigcup_{i\in \mathbb{N}} U_i \text{, and diam }U_i\in(0,\delta] \right\}$$
and for $E\subset \mathbb{R}^2$ we define the outer Hausdorff-measure on $\mathbb{R}^2$ as $\eta = \lim_{\delta \to 0} \eta_{\delta} = \sup_{\delta>0} \eta_{\delta}$.
Find the value of $\eta( \{ (0,0) \} ), \eta( [0,1] \times \{0\})$ and $\eta([0,1]^2).$
So far I've proved that $\eta( \{ (0,0) \} )=0$ by choosing the covering $(U_i)_{i \in \mathbb{N}}$ where $U_i = [0, 2^{-i} \delta) \times \{0\}$. Then
$$\begin{eqnarray}
\eta( \{ (0,0) \} ) &=& \lim_{\delta \to 0} \eta_{\delta}( \{ (0,0) \} )
\\&\leq& \lim_{\delta \to 0} \sum_{i \in \mathbb{N}} 2^{-i} \delta
\\&=& \lim_{\delta \to 0} \delta
\\&=& 0.
\end{eqnarray}$$
But so far I have failed for the other two. I'm very grateful for any type of hint.
Comment: For the unit square, relate $\eta_\delta([0,1]^2)$ and $\eta_{\delta/2}([0,1/2]^2)$.
Comment: Use the translation-invariance of $\eta_\rho$ to obtain a relation between $\eta_\delta([0,1]^2)$ and $\eta_{\delta/2}([0,1]^2)$.
Comment: Thank you! We should have $\frac{1}{2} \eta_\delta([0,1]^2) = \eta_{\delta/2}([0,1/2]^2)$, right? I haven't been able to figure out how to use this though.
Answer: Consider $E:=[0,1]\times\{0\}$. We claim that $\eta(E)=1$.
For $\delta>0$ let $N=\lfloor\frac1\delta\rfloor+1$, so that $\frac 1N<\delta\le\frac1{N-1}$.
For $1\le i\le N$ let $U_i=B\left((\frac {2i-1}{2N},0),\frac12\delta\right)$. Then $\operatorname{diam}(U_i)=\delta$ and $\sum \operatorname{diam}(U_i)=N\delta<1+\delta$. This shows $\eta_\delta(E)\le 1+\delta$ and $\eta(E)\le 1$.
On the other hand, let $\{U_i\}_{i\in\mathbb N}$ be any open cover of $E$
and assume $\sum\operatorname{diam}(U_i)<1$.
By compactness of $E$, a finite subcover $\{U_i\}_{1\le i\le N}$ suffices and wlog. all $U_i\cap E$ are nonempty.
Recall that $U\cap V\ne \emptyset$ implies $\operatorname{diam}(U\cup V)\le \operatorname{diam}(U)+\operatorname{diam}(V)$.
Therefore, we may repeatedly replace two overlapping elements of our finite cover with their union, until after finitely many steps we arrive at a finite open pairwise disjoint cover of $E$ that still has diameter sum $<1$. Since  $E$ is connected, the only possibility is that the remainig cover consists of a dingle open set. But then its diameter must be at least $1$ as it contains $(0,0)$ and $(1,0)$. Hence $\sum\operatorname{diam}(U_i)\ge 1$ for any open cover of $E$, so that we ultimattely find $\eta(E)=1$.

Now consider $E=[0,1]\times[0,1]$.
For $n\in\mathbb N$ and $\delta<\frac1n$, observe that in an open cover $\{U_i\}$ of $[0,1]\times[0,1]$, any $U_i$ intersects at most one of the horizontal lines $[0,1]\times\{\frac kn\}$, $0\le k\le n$, because its diameter is too small to touch two such lines. Using the previous result, we find
$$\sum_{i\in\mathbb N}\operatorname{diam}(U_i)\ge \sum_{k=0}^n\sum_{i\in \mathbb N\atop U_i\cap [0,1]\times\{\frac kn\}\ne\emptyset}\operatorname{diam}(U_i) \ge \sum_{k=0}^n1=n+1.$$
As we let $\delta\to0$ this shows $\eta([0,1]\times[0,1])=\infty$.


Bounded linear operators on a Hilbert space

Question: Currently I'm self studying functional analysis, namely Hilbert spaces. In the text, the author gives the following remark:

Remark: If $\mathcal{H}$ is a Hilbert space, then for every bounded linear operator
$A\colon\mathcal{H}\to\mathcal{H}$ we have that the operator norm (or rather dual norm) is given by
$$
||A||_\text{op}=\sup\{|\langle Ax,y \rangle|: ||x||\leq 1,||y||\leq 1\}.\tag{1}
$$

My question is fairly straightforward: why is this remark true? I know one way of writing the operator norm for such a map is the following:
$$
||A||_\text{op}=\sup\{||Ax||:||x||\leq 1\}.\tag{2}
$$
But how does $(2)$ turn into $(1)$?
Comment: Exercise for you: $\lVert x\rVert=\sup_{\lVert y\rVert\leq1}\lvert\langle x,y\rangle\rvert$ for all $x\in\mathcal{H}$.
Answer: There are two norms. The first norm is the vector norm, which is written $\|x\|$.  For a Hilbert space, this is also the same as $\sqrt{\langle x, x, \rangle}$. The second norm is the operator norm. For a linear operator $f: Y \rightarrow Z$, where $Y,Z$ are both normed spaces, the norm of $f$ is written as
$$
\|f\|_{op} := \sup_{\|y\| \le 1} \|f(y)\|.
$$
The crucial thing here is that these two norms are one and the same on a Hilbert space when $x$ is viewed as a linear map $\mathcal{H} \rightarrow \mathbb{C}$ given by $f_x(y) = \langle x, y \rangle$; that is, the vector norm of $x$ is the same as the operator norm of $f_x$, and thus:
$$\sup_{\|y\| \le 1} |\langle x, y \rangle| = \|x\|$$
This is a crucial property of how vector space duality works, and it is worthwhile for you to understand why this property holds.
Therefore, replacing $x$ with $Ax$, we similarly obtain $\sup_{\|y\| \le 1} |\langle Ax, y \rangle| = \|Ax\|$, and the equality that you wanted to show follows.
Comment: It is related, but technically not necessary. The Riesz representation theorem is a uniqueness result that says that every linear functional on a Hilbert space can be expressed as an inner product against a unique vector. This is in some sense the reverse of what we need for your question. For your question, we only need that the map $y \mapsto \langle x, y\rangle$ is a linear functional and its operator norm is $\|x\|$, and this does not depend on uniqueness.
Comment: "is both the Hilbert space norm, but also the norm of $x$ when viewed as a linear functional over the dual; that is:$$\sup_{\|y\| \le 1} |\langle x, y \rangle| = \|x\|."$$ Can you explain this a little more?
Comment: I have expanded my answer further.
Comment: Great! Shouldn't it be mentioned $x$ (being viewed as a bounded linear functional on a Hilbert space) can be rewritten as such do due the Riesz representation theorem? Or is this not needed?


How to search non-Android Java projects on GitHub?

Question: There are always so many Android libs/repos
when we want to search for Java libs/repos.
GitHub docs tells we could use filters
but there is no out-of-the-box examples.
Answer: Use NOT filter:
{lib-name} NOT android language:java

Example:
audio player NOT android language:java
More filter usages:

Understand the search syntex - GitHub Docs (中文)


Sending a file via CURL in PHP

Question: Im trying to send a file using CURL and no matter what I keep on failing...
$imgfile = realpath("../../vozila/".$glavna_slika);

$ch1 = curl_init();

$ext = pathinfo($imgfile, PATHINFO_EXTENSION);
$di['image'] = curl_file_create($imgfile,'image/'.$ext ,'test_img');
$di['access_token'] = (string) $ad['access_token'];

curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data'));
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_POSTFIELDS, http_build_query($di));
curl_setopt($ch1, CURLOPT_POST, true);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);

$data1 = curl_exec($ch1);

curl_close($ch1);

The result I am getting is that no file is sent. It is sending a string instead of a file
Comment: You set the file in `$di` but then send `$ad` in `CURLOPT_POSTFIELDS`. Typo?
Comment: @ishegg yeah that's an old typo, I forgot to edit that one with the latest code, ill edit the OP
Comment: what is the output you get ?
Comment: @motto this is the output
["access_token"]=> string(40) "a526ac6febd446c11dd" ["id"]=> string(8) "1195" ["image"]=> string(82) "@/home/website/public_html/vozila/5387617666.jpg;filename=test_img;type=image/jpg"
Comment: @OhHiDer any updates?
Answer: A couple of problems in your code. First, you don't need http_build_query(). CURLOPT_POSTFIELDS will take an array, so just pass your $di array to it. Second, CURLOPT_POST needs to be in the code before CURLOPT_POSTFIELDS. So your working code would look like this:
<?php
$imgfile = realpath("../../vozila/".$glavna_slika);

$ch1 = curl_init();

$ext = pathinfo($imgfile, PATHINFO_EXTENSION);
$di['image'] = curl_file_create($imgfile,'image/'.$ext ,'test_img');
$di['access_token'] = (string) $ad['access_token'];

curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data'));
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_POST, true);
curl_setopt($ch1, CURLOPT_POSTFIELDS, ($di));
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);

$data1 = curl_exec($ch1);

curl_close($ch1);

Sending a file via CURL in PHP


Should I use the Rules module to implement custom code when performing state transition?

Question: I need to run some custom code when I go from state A to state B (using the workflow module).
Should I implement this with the Rules module and call my function from there or implement all of it in hook_workflow from a custom module?
In short, what is the Drupal approach?
Answer: Using the Rules module for this is indeed an option, whereas you should be able to implement your "custom code" using the Rules Action to "Execute custom PHP Code". If you do, make sure to not forget the PHP filter (otherwise you'll run into "Rules "Execute custom PHP Code" option not available ...").
Of course, that required 'PHP filter' might be your show-stopper. BUT: are you sure you need "custom code" at all? There may be alternative (site building only) techniques to even not need that "custom code" (and hence you'd avoid the need for that PHP filter also). And/or as a variation, you could write your own (custom) Rules Actions also ...
Not to forget these benefits also:

Less custom code (= easier to maintain and/or upgrade to future Drupal versions).
Possible out-of-the-box integrations with other modules, such as Flag, VBO etc.
Answer: If I understand your question correctly, both are Drupal approaches. Here are the main differences:

If you do this through Rules, you will allow site builders / administrators to change, enable and disable this without touching the code. Also, adding new actions later on will be much easier, because you can just use the admin panel.
If you do this through code, you will be able to track the changes through CVS, you will probably have no limitations on what you want to achieve, and there will be less chances that whoever manages the site does something wrong.

Ultimately, it's up to you and your client. If you don't see this changing in the future, or if it's something complex, do it in the code. Otherwise, Rules might be an easier option.
Answer: Thank you both for you answers. I now see that there are pros and cons between the both approaches.
I used the workflow approach with the motivation that the triggers and conditions I set up will be easier to migrate to other Drupal versions (I also tried to implement it with a custom module but the result was all but satisfying)


Show that $n(1-\sqrt[n]{n})\to-\infty$

Question: Show that $\lim \limits_{n \rightarrow \infty} n(1 -\sqrt[n]{n}) = -\infty$, using the following bounds if necessary:
(a) $n(n+1)^{1/n} < n+s_n$ for $n>1$
(b) $(n-1)(n)^{\frac{-1}{n+1}} < n - s_n$ for $n>2$
EDIT: Here $s_n$ is the harmonic sum.
My ideas were: If the sequence is bounded below by constant multiple of $s_n$, then it is divergent.
I used (b) which gives,
$s_n < n^{\frac{-1}{n-1}}(n^{\frac{n}{n-1}} - n + 1 )$
and as we limit $n \rightarrow \infty$, the term $n^{\frac{-1}{n+1}}$ limits to $1$. We also have
$ \frac{n^{\frac{n}{n-1}} - n + 1}{n(\sqrt[n]{n} -1 )} = \frac{n^{\frac{1}{n-1}} - 1 + \frac{1}{n}}{\sqrt[n]{n} -1} \rightarrow \frac{n^{\frac{1}{n-1}} - 1}{n^\frac{1}{n} -1}$
It suffices to show the latter limits to a constant. Which I somehow could not find.
Any ideas?
Comment: Your "bounds" make no sense because "$s_n$" is not defined.
Comment: @Peter "The limit is $-\infty$ because $n^{1/n}>1$ for $n\ge 2$." This argument is false, please do not send the OP into deadends or into wrong solutions, even in comments.
Comment: OP: 9 months on the site, 15 questions, please show some work.
Comment: @Did, you mean I should post some of my workings?
Comment: Exactly. Didn't you read some howtoask page before starting to dump your questions on the site?
Comment: @Did I wanted to show a typo and formulated this unlucky.
Answer: Hint:
A simpler solution:
$$n^{\tfrac1n}=\mathrm e^{\tfrac{\ln n}n}=1+\frac{\ln n}n+o\biggl(\frac{\ln n}n\biggr), \quad\text{hence}\quad1-n^{\tfrac1n}\sim_\infty -\frac{\ln n}n.$$

Show that $n(1-\sqrt[n]{n})\to-\infty$


Constant $C$ in differential equation

Question: I have differential equations:
$$  dy + x(y+1)dx = 0.$$
I'm dividing boths sides by $(y+1)$:
$$ \frac{dy}{y+1} + xdx = 0.$$
The solution is:
$$ \ln|y+1| + \frac{x^{2}}{2}  = \ln |C|.$$
As we can see, I've assumed that arbitrary constant is $ \ln|C|.$
Next:
$$ \ln \left|\frac{y+1}{c}\right| = -\frac{x^{2}}{2} $$
$$ e^{\frac{-x^{2}}{2}} = \left|\frac{y+1}{c}\right| $$
$$ \pm e^{\frac{-x^{2}}{2}}  = \frac{y+1}{c}.$$
Letting $d = \pm c,$
$$ d e^ \frac{-x^{2}}{2} = y+1 $$
$$ y = de^{-\frac{-x^2}{2}} -1 $$
Could I write additional note that  $C \neq  0$? Because $ \ln 0 $ doesn't exist right?
Comment: @Chinny84 I suggest you submit your comment as an answer.
Comment: You can directly proceed from the third equation to $y+1=C·e^{-\frac{x^2}2}$, there is no need to introduce the extra sign variations. $C=0$ gives the equally valid and regular constant solution $y=-1$ that had to be excluded for the separation approach.
Comment: @LutzL you are absolutely correct - Compounding one error with an arguably larger one! In any case cheers for the catch. +1
Answer: If you want to apply the method of separation of variables, you need to reduce the domain be the zero set of the divisor. This might lose some solutions so first check if curves in the zero set are solutions, which in this case the constant function $y\equiv -1$ is.
Then proceed as you did to find that $y=-1+Ce^{-x^2/2}$ where per construction $C\ne 0$ but $C=0$ reproduces the excluded constant solution, so no restrictions on $C$ are necessary.
If you want to confirm that in a more unified manner, use the method of the integrating factor which in this case is $e^{x^2/2}$ and from
$$
\left(e^{x^2/2}(y+1)\right)'=e^{x^2/2}(y'+x(y+1))=0
$$
you get again $e^{x^2/2}(y+1)=C$ without further restrictions.
Answer: I have a similiar example here:
differential equation
Could I exclude in similiar way solution $ y \neq 1 $, because $ln(0)$ dosen't exist in solution $ y = e^{c sin(x)} $ ?

Constant $C$ in differential equation


How can I filter which entities are available in the admin form for an Entity Reference Field?

Question: I've got a Field which is called "Primary Article" which is an Entity Reference Field. I've configured this field setting the "Reference Type" to be "article", so that only articles show up in the Form when editing this field.
I would want to add two customizations to the current autocomplete field:
)
1) only return some articles based on some logic ( like I only want to return articles which are not selected as primary articles for another taxonomy )
2) change the text of the autocomplete field, so in this case, instead of "Primary Article" I would want to return "Primary article - XYZ" where XYZ is a custom field
Answer: 1.) You can create the limited list of Articles with Views, then use that Views in the Entity reference Field settings as the source.
2.) This should have probably gone into a separate question. Try researching into modules related to autocomplete, like: Search Autocomplete, Autocomplete Search Suggestions.
Comment: For point number 1), I would prefer a "code only" solution, I don't want to install new modules.
Comment: Views module is in D8 core ;)


$a_{1}=1$, $a_{n+1}=a_{n}+f(n)$. Prove that $a_{n}$ isn't bounded

Question: I came up with $a_{n}=1+f(1)+f(2)+f(3)+\ldots+f(n-1)>1+(n-1)f(n-1)$
As $n$ approaches infinity $n-1$ goes to infinity but $f(n)$ may approach $0$
So from here I do not get any information
($f(n)$ is decreasing function that's always equal to positive values)
Comment: Do you mean it is not necessarily bounded?
Answer: If $f(n)= \frac{1}{n^2}$, then $(a_n)$ is bounded.
If $f(n)= \frac{1}{n}$, then $(a_n)$ is not bounded.


Local moved to Server all nodes 404

Question: My homepage works perfectly fine on the live server but when I use any of the links they give me a 404 Page not Found.
I tried a couple things which was:

Made sure .htaccess was there
Tried a copy/paste of an .htaccess that someone provided
Tried setting $base_url
Tried setting Rewritebase /

Tried making sure this was in .htaccess
<code> Options +FollowSymLinks
 RewriteEngine on
 RewriteBase /
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]
</code>
I did go from an xampp local testing environment on Windows to a nginx setup on Ubuntu 14.04 so I figure it has something to do with that. I'm just not sure what to try next I've done most everything that googling brings me to.
Comment: module_rewrite is enable?
Comment: I do not see a module_rewrite in the settings at all
Comment: Are you running nginx or apache locally? Have you checked so `AllowedOverride All` is set in you webservers configuration for you local directory?
Comment: #   RewriteBase / try comment it out line
Comment: @Cyclone AllowOverride is set to none     Adi, RewriteBase was commented out, I uncommented it but I tried again still no go. Trying something else I just found we'll see what it does
Comment: Missed part of the question...Locally I'm running Apache on my Live Server its nginx
Answer: Ok so it was a bit weird how it all came together, I restarted the nginx and that didn't do anythig.
I opened the default config file for nginx and just saved it without making changes and restarted the service.
After than I went from a 404 error to a 500 error.
To fix the 500 error I changed a line in my default from this:
<code> try_files $uri $uri/ /index.html;
</code>
To this:
<code> try_files $uri/ $uri /index.php?$query_string;
</code>
I saved the config file and restarted nginx and it worked!

Local moved to Server all nodes 404


Economics.SE traffic update

Question: The last thirteen weeks (Mon Feb 1 to Sun May 1, 2016) appear relatively stationary (with daily seasonality of course):

So sample daily averages are meaningful:

In the last week of April 2015, daily traffic was between $[300, 450]$...
...and we have come a long way from last summer:
Comment: @denesp. Generally, it does not take into account quarterly seasonality indeed. But in our case, traffic increased up to May 2015, then stayed ~stable for the duration of the summer, and then visibly increased as Autumn 2015 set in. So summer 2015 was like ~Spring 2015, and since I  had the graph ready I included it in the post.
Comment: Comparing April averages to summer averages seems strange. I seem to recall there is a huge drop during the summer months.
Answer: Thanks for the update. As a 'softer' metric, here is the current Area 51 data:

We indeed seem to be firmly rooted into the excellent category for traffic-related stats. I guess we now have a fairly health user-base and good search engine visibility.
I am not too worried about % answered and number of answers per question because my impression is that we have quite a lot of low-quality questions on the site and that the good questions tend to get answered fairly promptly.
The thing that worries me a little more is the number of questions per day. This seems to be slowly declining, and is obviously important for the long-run vitality of the site.
Comment: Re: Questions. See additional data in an answer I posted. Re: Questions answered. I also don't worry as long as it stays above 70% (the 90% benchmark is for simpler and less diverse fields of knowledge).
Answer: Following on @Ubiquitous answer, questions per week in the first four months of 2016 seem to also be stable. There was an apparent upward trend in mid-March, but it didn't last.

A different and more general worry could be "What? We have stabilized? So this is it?"
I can understand this "grow or perish" mentality, but in real life growth many times happens in "waves", and after each wave there is a period of stability.
However adventurous it may be to use data from the first months in beta, it may be the case that we will have to "wait it out" (meaning, energetically care-taking for the site in the meantime), until September 2016.

Economics.SE traffic update


CSS and Javascript is not applied to a content rendered from h:outputText with escape="false"

Question: A content rendered from h:outText with escape="false" is not bound to the css or javascript applicable for that page. Actually I am trying to use syntax highlighters to highlight my syntax within a post. The post is stpored in database and displaying it in a JSF page with h:outputText tag by setting escape attribute as false. It renders the page as expected with all html tags being processed but css or javascript applicable to the code blocks within that post is not applied. Below  is my jsf page which retrieves html from database and shows it through h:outputText tag. The retrieved content has syntax it needs to be highlighted.
   <ui:composition xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:p="http://primefaces.org/ui"
        template="/templates/ui.xhtml">

 <ui:define name="head">
    <title>tekBytes-#{tutorialController.tut.title}</title>

<h:outputScript library="primefaces" name="jquery/jquery.js" />
<link href="/rainbow/themes/pastie.css" rel="stylesheet" type="text/css" />

  </ui:define>
  <ui:define name="content">

#{tutorialController.tut.title}

  <h:outputText value="#{tutorialController.tut.contentStr}" escape="false"/>

  </ui:define>
</ui:composition>

Answer: I got it resolved. The actual issue was that the html stored in database was generated through the p:editor which has put so many div tags around every line, so when it is rendered through h:outputText tag  the css or javascript is not able to parse the code blocks that are applicable for syntax highlighting because of surrounded divs and other tags. So I have removed all of these unnecessary tags before storing it in the database. Thanks, everyone for the replies.
Answer:
In JSF 2.0, all your web resources files like css, images or
  JavaScript, should put in "resources" folder, under the root of your
  web application (same folder level with "WEB-INF").

Put your js / css in resources/js and resources/css folder and access them with <h:outputStylesheet and <h:outputScript
Like this
<h:outputScript name="js/rainbow.min.js" />

and
<h:outputStylesheet name="css/language/css.js" />

and so on...

Also read this : Resources (Library) In JSF 2.0
Answer: Potential problem could be in number of places here , verify the following in order.
    1) Check your CSS files are getting loaded by the browser
by monitoring the request in developer tools on Firefox or chrome.

   https://developers.google.com/chrome-developer-tools/docs/overview?hl=fr

    2) check the source of your html to see if that looks
fine and tags are not encoded etc.

    3) verify your CSS to see it works and it doesn't have missing semicolon,
 brackets,quotes etc by adding it to a test html page on your machine.


How do I call legacy PHP files?

Question: I am unable to call PHP files inside the Drupal directory. My task is maintaining staff's old pages on the new platform. With pages written on clear HTML, there is no problem: I place files in a folder I created (domain.org/sites/default/files/share/staff_folder).
In sites/default/files/.htaccess I added DirectoryIndex share and now the link to the domain.org/sites/default/files/share/staff_folder/index.html can be used. When I import pages written in PHP, e.g. calling domain.org/sites/default/files/share/staff2_folder/index.php, I get a 403 error:

Forbidden
You don't have permission to access /sites/default/files/share/staff2_folder/index.php on this server.

While /sites/default/files/share/staff2_folder/index.html still works.
Comment: If the old pages are just simple html it might be better to just make it as a basic page in Drupal.
Comment: The problem appears with pages written on php only (may be on pearl or python, but I don't have any of them to try). And as I answered to Kevin, I need only link for outside usage. No use inside drupal. It will be staffs' own space to maintain.
Comment: Thanks, kiamlaluno, for higlighting text. Sorry "I can not into tags" :(
Comment: This isn't really a Drupal issue. Drupal bypasses any existing files, and since this is an existing file, and the error you are seeing is a server error and not a Drupal error, it shows that the problem isn't in Drupal, but rather your server configuration.
Comment: Jaypan, I understand this circumstance. It is connected to the structure of .htaccess file in the root of /www. But I don't know how to configure it for correct "portforward". That is why I tried asking at many resources.
Answer: This is the part of .htaccess, which controls the access to php files:
  # For security reasons, deny access to other PHP files on public sites.
  # Note: The following URI conditions are not anchored at the start (^),
  # because Drupal may be located in a subdirectory. To further improve
  # security, you can replace '!/' with '!^/'.
  # Allow access to PHP files in /core (like authorize.php or install.php):
  RewriteCond %{REQUEST_URI} !/core/[^/]*\.php$
  # Allow access to test-specific PHP files:
  RewriteCond %{REQUEST_URI} !/core/modules/system/tests/https?.php
  # Allow access to Statistics module's custom front controller.
  # Copy and adapt this rule to directly execute PHP files in contributed or
  # custom modules or to run another PHP application in the same directory.
  RewriteCond %{REQUEST_URI} !/core/modules/statistics/statistics.php$
  # Deny access to any other PHP files that do not match the rules above.
  # Specifically, disallow autoload.php from being served directly.
  RewriteRule "^(.+/.*|autoload)\.php($|/)" - [F]

You can see, how the access for statistics.php is allowed. You can add your own rules (at your own risk).
Answer: Just create the pages by going to Admin > Content > Add Content > Basic Page (or content type that is most applicable). There is no need to serve static PHP files within Drupal. Drupal is a content management system.
Knowing nothing about your content structure, it is up to you to attach fields to a content type for these pages that would best represent the data within them - use Drupal for its strengths, this is one of them. Revert all the changes to the .htaccess and apache conf files, it is not necessary.
For security reasons, you definitely do not want to have .php files in sites/default/files, a directory which may inadvertently be set to 777 permissions and open up multiple holes.
edit: I thought you said they were 'staff' web pages, but now I see that you might mean different sites the staff maintained. In which case you would want to look into multisite with Drupal, but with the same content management methodology applied.
Comment: In fact, I don't want to implement these pages in Drupal. We have only one hosting, that is why, I just want to give them new links.
e.g. "/sites/default/files/share/staff2_folder/index.php"
these links will not be used in Drupal, just outside.
Comment: Kevin, I just want to "portforward" access to the files inside Drupal directory, like it is done for *.html files.

How do I call legacy PHP files?


Unique ID (Might be Binary or something)

Question: // Okay originally people do their IDs for stuff like this:
var Item1 = 0;
var Item2 = 1;
var Item3 = 2;
var Item4 = 3;
var Item5 = 4;

// ... and so on.
// Then create more variables for their checks like:
var HasItem1 = false;
var HasItem2 = false;
var HasItem3 = false;
var HasItem4 = false;
var HasItem5 = false;

// Then somewhere in their code set the items to true if they want the user or player to // have the item, but that's alot of variables.
// Instead I wanted to do something like this:
var Item1 = 1;
var Item2 = 2;
var Item3 = 4;
var Item4 = 8;
var Item5 = 16;

var HasItems = 0; // 0 = no items added.

// The somewhere in my code:
HasItems = Item1 + Item3 + Item4; // 1 + 4 + 8 = 13

// But can't find a way to check if the player has Item3 for example.
if(HasItems != 0 && (HasItems & Item3) != -1) { // It was something like this.
    // Has the item we're looking for.
}

// Seen the ID system I'm talking about before, I forget where.
// Anyone know about this system of IDs?
// !!!EDIT!!!
http://blog.millermedeiros.com/using-integers-to-store-multiple-boolean-values/
Example:
[code]
var Item1 = (1 << 1);
var Item2 = (1 << 2);
var Item3 = (1 << 3);
var Item4 = (1 << 4);
var Item5 = (1 << 5);

var Items = (Item1 | Item3 | Item4);

if(Items & Item1) {
print("true 1");
}
else {
print("false 1");
}

if(Items & Item2) {
print("true 2");
}
else {
print("false 2");
}

if(Items & Item3) {
print("true 3");
}
else {
print("false 3");
}

if(Items & Item4) {
print("true 4");
}
else {
print("false 4");
}

if(Items & Item5) {
print("true 5");
}
else {
print("false 5");
}
[/code]

Output:
true 1
false 2
true 3
true 4
false 5

Thank you all for your responses, I searched bitwise and walla.
Special thanks to winterblood!
For your concern, I know it only supports a limited amount of values, this is meant for small systems.
Comment: You have the overall premise of a bitwise permission system, but it cannot work with decimal numbers.
Answer: Checking for 1 bit can be done with the following code:
(HasItems & (1 << itemNumber)) != 0
where itemNumber is number of item, starting at 0.
Also, this may be more universal and convenient:
http://msdn.microsoft.com/en-us/library/system.collections.bitarray.aspx
http://msdn.microsoft.com/en-us/library/system.collections.specialized.bitvector32.aspx
Comment: @winterblood from the example, there is only powers of 2.
Comment: Yeah, acted without thinking, thus I deleted my original comment.
Comment: The OP should also know that this system is generally limited to a max ownership of 32 items within the number though (unless this is programmed for a 64 bit system, in which case 64 is the limit).
Comment: winterblood

This system makes it so the IDs can't add into themselves:
Item1 = 1
Item1 + Item1 = 2; // New ID.
Item1 + Item2 = 3;
Item2 + Item2 = 4; // New ID.
Item1 + Item3 = 5;
Item2 + Item3 = 6;
Item1 + Item2 + Item3 = 7;
Item3 + Item3 = 8; // New ID.

// Get it?
Comment: @user2893193 Bitwise systems cannot handle multiples of the same id. You are going to have to resort to a more conventional associative array type thing if you need to handle more than on/off toggling of an id.
Comment: @Sarge Borsch - Anyways that did not work at all, only Item2 came true, and Item2 wasn't even one of the ones I added.
Comment: @user2893193 You need to begin your indexing from 0 for the provided code to work, because `1 << 0 === 1`, `1 << 1 === 2`, `1 << 2 === 4` etc. but as I said above, it can't handle multiples of the same item.


My first iteration is being skipped - MATLAB

Question: I am trying to get an output from my function but the first iteration is being skipped.
I keep getting a ZETA = 1.000 instead of ZETA = -18.7026
Please help!
EIG = [
 -18.7026;
  -4.6179 + 7.4827j;
  -4.6179 - 7.4827j;
  -1.1268 + 4.3335j;
  -1.1268 - 4.3335j;
  -0.3372 ]

for i = 1:6;

    SIG(i) = real(EIG(i));
    OMG(i) = imag(EIG(i));

    if  OMG(i) == 0;
        ZETA(i) = SIG(i);
    else
        ZETA = -SIG ./(sqrt(SIG.^2 + OMG.^2));
    end

end

Answer: You forgot to include indexes in the else clause. Try this instead:
EIG = [
 -18.7026;
  -4.6179 + 7.4827j;
  -4.6179 - 7.4827j;
  -1.1268 + 4.3335j;
  -1.1268 - 4.3335j;
  -0.3372 ]

for i = 1:6;

    SIG(i) = real(EIG(i));
    OMG(i) = imag(EIG(i));

    if  OMG(i) == 0;
        ZETA(i) = SIG(i);
    else
        ZETA(i) = -SIG(i) ./(sqrt(SIG(i).^2 + OMG(i).^2));
    end

end

Comment: Appreciate your help! Still new to matlab.
Answer: This might be a better version in terms of readability and mathematical rigorousness:
EIG = [...
  -18.7026;
  -4.6179 + 7.4827j;
  -4.6179 - 7.4827j;
  -1.1268 + 4.3335j;
  -1.1268 - 4.3335j;
  -0.3372...
 ];

ZETA = nan(size(EIG(:),1),1);
for i = 1:size(EIG(:),1)
   if ~imag(EIG(i))
      ZETA(i) = real(EIG(i));
   else
      ZETA(i) = -real(EIG(i)) /abs(EIG(i));
   end
end

Comment: Indeed much easier to read, makes me wonder whether the calculation actually makes sense mathematically. --- Sidenote: I think `isequal(abs(EIG(i)),abs(real(EIG(i))))` can be reduced to `~image(EIG(i))`
Comment: Nice, it's now in the version I submitted.


Why php's soapCall gives string conversion error?

Question: <code> // Set username and password
    $ih_soap_user= $this->config->item('interUser');
    $ih_soap_pass=$this->config->item('interPass');

    //echo $ih_soap_user.':P:'.$ih_soap_pass;
    // Set soap namespace
    $ih_soap_ns = 'http://www.interhome.com/webservice';
    // Create new instance of SoapClient with the interhome web service URL
    $client = new
    SoapClient($this->config->item('interSoap'),array('trace'=>'1'));
    // Create new soap header and set username and password
    $header = new SoapHeader($ih_soap_ns,
            'ServiceAuthHeader',
    array('Username' => $ih_soap_user,'Password' =>
    $ih_soap_pass),
    true
    );
    // Prepare p// Prepare parameters

  $params = array('inputValue' =>array(
        'Page'=>$page,
        'PageSize'=>'10',
        'OrderDirection'=>'Ascending',//Descending
        'OrderBy'=>'Price',//Favorite,Place
        'LanguageCode'=>'EN',
        'CurrencyCode'=>'EUR',

        'CountryCode'=>trim($ajaxSearchCountryCode),
        'RegionCode'=>trim($ajaxSearchRegionCode),
        'PlaceCode'=>$ajaxSearchPlaceCode,
        'CheckIn'=> $ajaxSearchCheckinDate,
        'Duration'=>'7',
       'ThemeFilter'=>$ajaxSearchTheme,
       'HouseApartmentType'=>array('House'=>'House'),
       'SpecialOffer'=>'AnySpecialOffer',
         'PaxMin'=>'1',
         'PaxMax'=>'',
          'RoomsMin'=>'1',
         'RoomsMax'=>'',

    ) );

    try{
    $result = $client->__soapCall("Search",array('parameters'=> $params),null,$header);
</code>
Hi guys..Any Idea why this call when I pass any not empty array, as for example ,as I made for  'HouseApartmentType', returns  this error

  A PHP Error was encountered

  Severity: Notice Message:
  Array to string conversion
Filename:
  controllers/houses.php Line
  Number: 269

And when it's only empty array or a string the soap call function is working... I need to pass array of options to one parameter....
LINE 269 is
<code>$result = $client->__soapCall("Search",array('parameters'=> $params),null,$header);
</code>
Comment: if your api only support strings, maybe you should serialize the array...
Comment: @Ibu it's not error I get from API server... I get this error during the call generation
Comment: The error said, array to string conversion so the error has something to do with the array being converted, can you show the line #269 of your code
Comment: I already edited the post and showed it @Ibu,it's a line where the call is made
Answer: This is probably solved already, but I also had problems with this and I thought I should post here when I finally found the solution. (The solution is not nusoap, it throws the same error.)
The error only seems to come from the soapCall call generation, it really is the Web Service that causes it. When you run __soapCall, it first asks the server which datatypes the parameters should have and then tries to comply. So if you try to feed an array when the service expects a string, it will do the array to string conversion when the request is generated.
Answer: i think you should look at the manual how a soapcall work:
<code>$params = array(
     'Page'=>$page,
     'PageSize'=>'10',
     'OrderDirection'=>'Ascending',//Descending
     'OrderBy'=>'Price',//Favorite,Place
     'LanguageCode'=>'EN',
     'CurrencyCode'=>'EUR',
     'CountryCode'=>trim($ajaxSearchCountryCode),
     'RegionCode'=>trim($ajaxSearchRegionCode),
     'PlaceCode'=>$ajaxSearchPlaceCode,
     'CheckIn'=> $ajaxSearchCheckinDate,
     'Duration'=>'7',
     'ThemeFilter'=>$ajaxSearchTheme,
     'HouseApartmentType'=>'House', // changed to string instead of array
     'SpecialOffer'=>'AnySpecialOffer',
     'PaxMin'=>'1',
     'PaxMax'=>'',
     'RoomsMin'=>'1',
     'RoomsMax'=>'' );
</code>
i simplified the array and you should test it and look if this is the kind of result you are looking for.
Also look for ordering errors like the example shown in php.net
Comment: I know that with String it's working. I need to pass an array with 3! options House,Appartment and DetachedHouse, so that search will return more results.  :(((
Comment: i don't think it supports what you are trying to do, maybe you can pass the 3 options you want individually
Comment: just to know. how? maybe Nusoap?Any Idea, how could I make the same call with Nusoap? With Nusoap I have problem of passing header... I have put the Header part of code in original post...
Comment: Maybe it is a good idea to add a new question for that, i am not familiar with Nusoap... sorry


Texas holdem meets the game of hearts

Question: The actual issues of my question are complex but can be reduced to the metaphor of a game of texas holdem with a twist.  The holder of the queen of spades always looses.
The actual scenario is that there are two players and all the cards are dealt for one hand only and then the players never see each other ever again. Suddenly one player says "you can have every  card in the deck but in exchange if you have the queen of spades you must turn it over, which means that you lose"
The other player declines the deal.
My question is how to show mathematically that such declination (assuming the imperative of rational and self-interested behavior of winning) is mathematically equivalent to holding the queen of spades
Comment: Then haven't you just answered your own question?
Comment: What does having every card in the deck mean?  Might not that give me the queen of spades?
Comment: I am regrettably not as precise as I should be. The point is that the person declining the deal has prior knowledge of the whether he or she is holding the queen of spades. Therefore the act of rationally declining the deal is equivalent (assuming rational behavior and a one time test) should be able to be shown as mathematically equivalent to possessing the queen of spades. (bayesian prior and likelihood analysis is the only way I am aware of showing this but I am unskilled at using this formulation to show proof of equivalency.
Comment: "you can have every card in the deck".  What is the significance of having every card in the deck?
Comment: The significance is that the declining side has absolute guarantee of winning if they do not have the queen of spades and absolute guarantee of losing if they do.
Comment: @Glen_b I think OP is looking for something in [symbols](https://www.facebook.com/permalink.php?story_fbid=10152323207228375&id=13012333374) or stating such precisely
Answer: Let our probability space be ($\Omega, \mathscr{F}, \mathbb{P}$) where $\Omega$ be the sample space containing the possible hands in this game, $\mathscr{F} = 2^{\Omega}$ and $\mathbb{P}$ is however probabilities are assigned.
Let A be the event of having all the cards in the deck in one's hand, and let B be the event of having the queen of spades in one's hand ($A, B \subseteq \Omega; A, B \in \mathscr{F}$).
Note that $A \subseteq B$
By the monotonicity of probability, $P(A) \leq P(B)$. If P(A) = 1, then P(B) = 1.
Another way to look at it is $P(B|A) = P(A \cap B)/P(A) = P(A)/P(A) = 1$

Texas holdem meets the game of hearts


How to speed up event confirmation email?

Question: I recently setup a Drupal 8 instance to set up a special event website. I am allowing users to build inline contacts to register for an RNG event. Everything seems to be working: The users offer their email and username, and the contact is created; then, they register for the event and the registration is created. Twelve hours later, they get an email message setup using the Courier and Rules integration with RNG.
Is there a way to speed up the confirmation email?  If I send an email from a user contact form on the same website, it sends it immediately. I imagine it is a Courier/Rules issue with how often the email queue is being processed, but I cannot figure out where that is configured.
Comment: I would probably phrase more than a Yes or No question in the future. I think that you have provided enough information for someone to provide an answer based on Courier and Rules. But I think one pending question for you is what your cron interval is setup for?
Answer: Courier message dispatch is all done in cron. Therefore, in order to speed up the delivery of messages, you should run cron as often as possible.
Personally I run it every 30 seconds, but every 1-5 minutes should be fine in most cases.
Disclaimer: I am the maintainer of both the RNG and the Courier modules.
Comment: If you run cron that often, you might want to use a module like Ultimate Cron so you don't have to run every cron job that often. Some can be quite slow and shouldn't be run that often.


Geometric brownian motion - Ito's lemma

Question: I have a question about geometric brownian motion. dS = uSdt + /sigma/SdW and then we do log(S) and we want to found dlog(S). So we use Ito's lemma en I get the dt part of the lemma but I don't see why df/dt*dw is zero.
Can somebody help me?
Answer: If $f(t,S) = \log(S)$ then $\frac{\partial}{\partial t}f(t,S) = 0$, since $f$ does not depend on $t$. Is this what you're asking?
Using Itô's formula, you will get
\begin{align}
d\log(S_t) &= \frac{\partial}{\partial t}f(t,S_t)\,dt +\frac{\partial}{\partial S}f(t,S_t)\,dS_t + \frac{1}{2}\frac{\partial^2}{\partial S^2}f(t,S)\,(dS_t)^2\\
&= 0\,dt + \frac{1}{S_t}\,dS_t - \frac{1}{2S_t^2}\,(dS_t)^2\\
&= \Bigl(\mu - \frac{1}{2}\sigma^2\Bigr)\,dt + \sigma\,dW_t
\end{align}
Comment: But S does depend on t, so don't we use the chain rule?
Comment: @user1200276 Ito's formula *is* the chain rule, appropriately modified for Ito stochastic integration. Indeed the first two terms in the first line are exactly what you would expect from the classical chain rule. The last term is the special term for stochastic equations.
Comment: @cimrg.joe I think you made a whole bunch of calculation errors. In particular I don't think there should be any $t$'s in the denominator, and that the $dW_t$ in the first line should be $dt$.
Comment: I found me error. Thanks for the help Joe.
Comment: @Ian, you are correct.
Comment: @Ian you are absolutely correct. There are just so many errors. Haha. Sorry for that.
Comment: They should be corrected now.

Geometric brownian motion - Ito's lemma


Generating polynomials that are co-prime to their first and second derivatives

Question: Let $f \in \mathbb Q [X]$ and not constant or of the form $(x-a)^n$.
Suppose:
$f_1 := \frac{f}{gcd(f,D^2f)}$ and;
$f_2 := \frac{f_1}{gcd(f_1,Df_1)}$,
where $Df$ stands for the formal derivative.

Is it true that $gcd(f_2,Df_2)=gcd(f_2,D^2f_2)=1$
Comment: Yes, it is true.
Comment: @ Eric ..Can you suggest proof of the statements.
Comment: @Ram: are you and the other one the same Ram? If so you seem to have been logging in using to different Google IDs, which resulted in your two accounts disassociating. E-mail me and I'll merge them.
Comment: If I'm not mistaken, you get $\gcd(f_2,Df_2) = 1$, but the difficulties come with the second derivative. It is easy to characterize polynomials that are relatively prime to their derivative, but relatively prime to their second derivative is not so immediate.
Answer: No, it is not true.  Try
$f(x) = x^2 + x^4 $
$f_1(x) = x^2 + x^4 $
$f_2(x) = x + x^3 $
${\rm gcd}(f_2, D^2 f_2) = x$


Is it possible to pickle a QSettings object?

Question: I realize this sounds horribly redundant, since pickle and QSettings basically achieve the same thing, but let me explain.
I have a program made up of widgets that can be docked, resized, or otherwise modified. The user wants to be able to save various states as layouts and change between these layouts. The layout is saved as a QSettings file.
To save a list of layouts, I want to pickle a dictionary of the format {'layoutName':qSettingsFile, 'anotherName':anotherFile}, but when I try to pickle a QSettings file I get TypeError: the sip.wrapper type cannot be instantiated or sub-classed.
Is there a way to make this work? I could just put the contents of the QSettings file in the dictionary and forget about QSettings, but I'm hoping for an easier/shorter way.
Comment: I've gone with working the contents of the file into the dictionary for now. Turns out it wasn't as bad as I thought it would be.
Answer: Use one settings file, and create separate groups for each layout.
The QSettings syntax supports hierarchical keys, so just use beginGroup and endGroup to create named layout sections:
settings.beginGroup('layouts')
settings.beginGroup('layoutname1')
# set various layout values...
settings.endGroup()
settings.beginGroup('layoutname2')
# set various layout values...
settings.endGroup()
settings.endGroup()

Which would output a file looking like this:
layouts/layoutname1/value1=true
layouts/layoutname1/value2=true
...
layouts/layoutname2/value1=false
layouts/layoutname2/value2=false
...

Alternatively, you could use QSettings arrays to create an of indexed list of layouts.
Comment: Great answer! Unfortunately this won't work in my situation due to legacy code, but under normal circumstances this would be perfect, so I'm marking it as correct.
Comment: Wanted to add also the extra benefit of sticking with a single `QSettings` approach as opposed to writing out your own pickle is that it picks the proper platform-specific preferences location and format. On OSX, for instance, it writes plist files to the preferred user location.


should i use subquery for many to many relationship?

Question: I have one table for many to many relationships but didn't have constraint index for this case, let said that i have table like this
people, clones, types,
clones trigger to people, and types trigger to clones and clones trigger to types,
clones have many types, and types have many clones
when I query like this
select * from clones m
join people p on p.id = m.person_id
join "CloneHasType" c on c.clone_id = m.id
join clone_types t on t.id = c.clone_type_id

CloneHasType is the table the many to many
the result shows all data of that clones with the types
but when in the end of that query i add where t.type_name = "nameType"
it was returned to that query ( or nothing )
what am i missing for this query ?
Comment: it was no problem, the query i said above with `where` not working, it shows that query on Dbeaver, not a table empty @ravioli
Answer: Try this:  UPPER(t.type_name) = 'NAMETYPE'
A couple notes:

Postgres is case-sensitive with strings.
When you check for string literals, it should be done with a single quote (') instead of double quotes (")
Comment: you are correct, but anyway, when i query different types on many to many table and result have same clones because that clones included that types, but i want result just one clone, no duplicate, how do to that ?
Answer: where t.type_name = "nameType"

as you used double quote so engine will find column name of nameType but you want to compare string so you have to use single quote
 where t.type_name = 'nameType'


Spring Security and social SignUp with Rest

Question: My application exposes a REST API for services and uses SpringSecurity to manage login at the private services.
With custom signup and login I don't have any kind of problem, but now I try to implement login/signup with Facebook or Twitter, and I don't know how to do this.
Has anyone had the same problem and solved it?
I tried to use a custom password "very long" for every Facebook and Twitter account but that didn't work.
UPDATE
I try your solution, but get an error. This is my code
public UserDetails loadUserByUsername(String mail) throws UsernameNotFoundException {
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        if (ConstantPWCabinet.SOCIAL_LOGIN_FACEBOOK.equalsIgnoreCase(attr.getRequest().getParameter(ConstantPWCabinet.LOGIN_TYPE))) {
            User facebookInfo = dao.getFacebookInfo(new FacebookTemplate(attr.getRequest().getParameter(ConstantPWCabinet.FACEBOOK_TOKEN)));
            List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
            authorities.add(new SimpleGrantedAuthority(Role.ROLE_USER_FACEBOOK.toString()));
            org.springframework.security.core.userdetails.User user = new org.springframework.security.core.userdetails.User(facebookInfo.getEmail(), null, authorities);
            Authentication auth = new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(auth);

            return user;

        }

        logger.debug("Mail di accesso: " + mail);
        User user = dao.getUserSelectedMail(mail);
        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        String role = user.getRole().toString();
        if (StringUtils.isEmpty(role))
            authorities.add(new SimpleGrantedAuthority(Role.ROLE_USER.toString()));
        else
            authorities.add(new SimpleGrantedAuthority(role));
        return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), authorities);
    }

But i get and "Bad credential" and no get login.
Comment: Have you gone through [the Spring Social documentation](http://projects.spring.io/spring-social/) and examples?  Providing an example of your setup/code may increase your chances of receiving a helpful response.
Comment: i don't have problem to recover info from social. My problem is authentication with social. How i make login if i don't have password? ok i use token, but what's the corresponding of j_spring_security_check? And how i write the method 'public UserDetails loadUserByUsername(String mail) throws UsernameNotFoundException'
Answer:
You have an AuthenticationFilter that listens to url j_spring_security_check
Filter creates an authentication object and sends to Authentication Provider.
AuthenticationProvider calls UserDetailsService to load user by username and authenticate the user.
Filter then checks the authentication object returned by the provider and sends request to success/failure handler.

When you do it through social medium, your user is authenticated by an external source, so you do not need to authenticate user at your end.
You can simple do
// Authenticate the user
UserDetails user = userDetailsService.loadUserByUsername(username);
Authentication auth = new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(auth);

This will authenticate the user without password.


Forward Kolmogorov Equation and Normal Distribution

Question: I read through my textbook and find one interesting statement. The normal pdf function $\frac{1}{\sqrt[]{2\pi\sigma^2}\ t}\exp\left(-\frac{(x-\mu t)^2}{2\sigma^2t}\right)$ satisfies the forward Kolmogorov equation. I find the forward Kolmogorov equation and try to prove it. But I'm confused about how to deal with the derivatives, especially the $\frac{\partial P}{\partial t}$ since I never see the t in other normal pdf. Could someone give me some ideas about how to go forward? Thank you very much.
$$\frac{1}{2}\sigma^2\frac{\partial^2 P}{\partial x^2} - \mu\frac{\partial P}{\partial x}=\frac{\partial P}{\partial t}$$
Comment: You're just asking how to take the partial with respect to $t$?  Have you had multivariable calculus?
Comment: I have taken multivariable calculus. But I don't know how to deal with this derivative.
Comment: Ok, I did your derivative below.  I hope this is the sort of thing you're asking for, rather than some functional analysis proof that it satisfies the Kolmogorov equation.
Answer: Ok, the problem isn't hard if you know what those partials mean, it's just a huge mess haha  To take the partial with respect to $t$ (or any variable) you just treat the other variables like constants.  For example, if $P = xt^2$ then $\partial P/\partial t = 2xt$.  So to compute the partial $\partial P / \partial t$ first use the product rule and take the derivative of the left summand:
$$\partial P / \partial t = \frac{\partial}{\partial t}\left( \frac{1}{t \cdot \sqrt{2\pi\sigma^2}}\right) \cdot \exp\left(-\frac{(x-\mu t)^2}{2\sigma^2t}\right) + \left( \frac{1}{t \cdot \sqrt{2\pi\sigma^2}}\right) \cdot \frac{\partial}{\partial t} \left( \exp\big(-\frac{(x-\mu t)^2}{2\sigma^2t}\big) \right) =$$
$$= \left( \frac{-1}{t^2 \cdot \sqrt{2\pi\sigma^2}}\right) \cdot \exp\left(-\frac{(x-\mu t)^2}{2\sigma^2t}\right) + \left( \frac{1}{t \cdot \sqrt{2\pi\sigma^2}}\right) \cdot \frac{\partial}{\partial t} \left( \exp\big(-\frac{(x-\mu t)^2}{2\sigma^2t}\big) \right)$$
Now to take the derivative on the right, we use the chain rule for $(e^{f(t)})' = f'(t)e^{f(t)}$:
$$\frac{\partial}{\partial t} \left( \exp\big(-\frac{(x-\mu t)^2}{2\sigma^2t}\big) \right) = \frac{\partial}{\partial t} \left(-\frac{(x-\mu t)^2}{2\sigma^2t}\right) \cdot \exp\left(-\frac{(x-\mu t)^2}{2\sigma^2t} \right) =$$
$$ = - \left( \frac{-4\mu \sigma^2 t(x-\mu t) - 2\sigma^2(x-\mu t)^2}{4 \sigma^4 t^2}\right) \cdot \exp\left(-\frac{(x-\mu t)^2}{2\sigma^2t} \right) =$$
$$ = \frac{2\mu t(x-\mu t)+(x-\mu t)^2}{2 \sigma^2 t^2}\cdot \exp\left(-\frac{(x-\mu t)^2}{2\sigma^2t} \right)$$
Where the first equality is just the well-known quotient rule for derivatives, and the second equality is just distributing the minus sign and reducing.  I'd double-check these calculations if I were you, I just woke up.  Putting these together and writing $A = \exp\left(-\frac{(x-\mu t)^2}{2\sigma^2t}\right)$, we get the atrocious:
$$\partial P/\partial t = \left( \frac{-1}{t^2 \cdot \sqrt{2\pi\sigma^2}}\right) \cdot A + \left( \frac{1}{t \cdot \sqrt{2\pi\sigma^2}}\right) \cdot \frac{2\mu t(x-\mu t)+(x-\mu t)^2}{2 \sigma^2 t^2} \cdot A =$$
$$ =\frac{A}{t^2 \cdot \sqrt{2 \pi \sigma^2}} \cdot \left( \frac{2\mu t(x-\mu t)+(x-\mu t)^2}{2 \sigma^2 t} - 1\right) =$$
$$= \frac{A}{t^2 \cdot \sqrt{2 \pi \sigma^2}} \cdot \left( \frac{x^2 - \mu^2t^2}{2 \sigma^2 t} - 1\right)$$
Now you need to check that the functional equation is satisfied, taking the partials wrt $x$.  Hopefully it just falls right out - which it basically always does when something like that is true.  But without some heavy theorems I see no way to prove this without doing the monster calculations.  The partial wrt $x$ is a lot simpler, thankfully.


How was row reduction used to obtain the result in this example?

Question: In the example below, I obtained a different result for $(2)$, and don't know how to reproduce the given one. Here is my result:
$$ \begin{bmatrix}a \\ b \\ c \\d\end{bmatrix} = \begin{bmatrix} -2 + 2r \\ -2 + r \\ -1 \\ r \end{bmatrix}$$
I verified my result with an online row reduction calculator and it seems to be correct.
How do we get from $(1)$ to $(2)$ by row reduction in the example below?
Answer: Note that in your calculation, you use the last variable $d$ as the "free parameter" (you have the equation $d = r$ and then you express $a,b,c$ in terms of $r$). However, in the calculation attached from the book, they use the first variable $a$ as the free parameter. To convert your answer to the book's answer, starting with your representation of the solution, note that
$$ a = -2 + 2r = -2 + 2d \implies d = \frac{a + 2}{2} = \frac{a}{2} + 1,\\
b = -2 + r = -2 + d \implies b = -2 + \frac{a}{2} + 1 = \frac{a}{2} - 1.$$
Hence, if we use $a$ as a free parameter instead of $d$, (setting $a = r$), we get
$$ b = \frac{r}{2} - 1, \,\,\, d = \frac{r}{2} + 1. $$
It is worth emphasizing there is "nothing wrong" with your solution. It is perfectly correct and there is no need to convert it to the book's solution. Plugging your solution into the first equation, we get
$$ \begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix} + (2r - 2) \begin{pmatrix} 1 \\ 1 \\ 0 \end{pmatrix} + (r - 2) \begin{pmatrix} -1 \\ 0 \\ 1 \end{pmatrix} = \begin{pmatrix} 1 \\ 0 \\ 1 \end{pmatrix} + r \begin{pmatrix} 1 \\ 2 \\ 1 \end{pmatrix}$$
which is just a different parametric representation of the same line as the one written in the book's answer (verify this!).
Comment: @Calculemus: I've tried to be be consistent both with your solution and the book's notation but that might have been overly confusing. If you start with your solution, then $d = r$ and (say) $a = -2  + 2r = -2 + 2d$. This expresses $a$ in terms of $d$. However, we can set $a = r'$ and then express $d$ in terms of $r'$. This is the conversion between your representation of the solution and the book's representation, only that it uses the letter $r$ instead of $r'$. Also, I've had a typo which I fixed which might have caused the confusion.
Comment: @Calculemus: I have no idea why they used the first variable as the free parameter. As far as I know, in the standard way Gauss-Jordan elimination is taught, you use the "last variables possible" as free parameters (just like in your solution).
Comment: How is it possible to set $a = r$ and also obtain $a = \frac{r}{2} + 1$? Am I missing something?
Comment: Thank you for your answer and the follow-up! It helps to know that my answer can be converted to the book's solution! But I'm also interested in the procedure by which they arrived at their solution. Do you know how they got to it (assuming they didn't convert from my solution to theirs, because I see no reason to)? The row reduction algorithm we've been taught (Gauss-Jordan elimination) yields my solution.


Como forçar um documento html sempre ser aberto pelo navegador?

Question: Estou desenvolvendo uma aplicação em java para um trabalho em faculdade e tenho uma seção de ajuda para o usuário em que eu abro uma página html no navegador, ou pelo menos deve abrir neste.
A questão é que se os arquivos com extensão .html estiveram por padrão do sistema para serem abertos por um navegador, tudo bem ele será aberto pelo navegador, porém se esta extensão estiver por padrão vinculada, por exemplo, a um editor de texto ele será aberto no editor de texto revelando o código.
Gostaria de saber se existe um modo em java de um documento html somente ser aberto pelo navegador. O código que eu uso é o abaixo:
Desktop desktop = null;
desktop = Desktop.getDesktop();
URI url = null;

File file = new File("src/ajuda/index.html");

try {

    url = new URI(file.toURI().toString());
    desktop.browse(url);

} catch (URISyntaxException | IOException ex) {
    Logger.getLogger(Teste.class.getName()).log(Level.SEVERE, null, ex);
}

Comment: E no caso você iria pressupor que o computador tem um programa default? tipo todos tem o IE? nesse caso você poderia usar o Runtime.getRuntime().exec(""C:\Program Files\Internet Explorer\iexplore.exe" www.google.com.br");
Answer: Não entendo de java, mas programo em outras linguagens. Não sei aonde está executando seu código, tb não sei se é possível...mas...
E se ao invés de você dar um comando deixando o SO decidir aonde abrir o seu HTML, você chamasse o browser diretamente, passando como parametro o caminho do html, tipo isso:
execute("c:\arquivo programas\ie\iexplorer.exe seuhtml.html").
Isso abrir seu HTML sempre no browser. Se tiver um server rodando na maquina, só chamar o caminho padrão do seu html:
execute("c:\arquivo programas\ie\iexplorer.exe http://localhost/seuhtml.html").
É uma idéia.
Answer: Não acho que isso seja possível principalmente porque, fazendo isso, você estaria tirando o poder do usuário de controlar as ações que acontecem no seu próprio computador. Seguindo padrões de usabilidade, o usuário que deve escolher como e com quais programas ele vai abrir/visualizar determinado tipo de arquivo. O que você pode fazer é mostrar mensagens aconselhando a melhor maneira de abrir/visualizar o arquivo;


Comparing upper percentile across time

Question: Is there a test to compare a variable across time When using a cutoff value to select for observations with a particularly high or low value.
I can think of many simple examples for this:

Did the richest persons (99th percentile) get richer between 2000 and 2010?
Do persons with a blood pressure above 140mmHg show an increase in blood pressure when watching their favourite football team?
Did the fastest runners get faster in the Olympics 2004 in comparison to 2000?

My own naive guess is something like a regression to the mean (I hope I am using the term correctly): If the times for running in the Olympics are just randomly correlated across years, then I would expect that those who were fast in 2000 are likely to be slower in 2004.
The other case when the rich become richer from year to year holds rather true for money.
Comment: _thickest_ looks like a typo for _richest_. (In some versions of English, _thickest_ is informal for _most stupid_ and so pejorative, if not offensive, unless used with modest self-reference.)
Comment: These are just special cases of _quantile regression_ which is the key term you seek. As the predictor is time, worry about dependence of errors might be appropriate.
Comment: Thanks a lot for your comments so far. Although I was refering to the waist size, I chose a more suitable example.
Comment: That would be _fattest_ except that circumlocution about circumference using a term like _waist size_ would often be preferred.
Comment: "a blood pressure above 140 mmHg" would usually mean different percentiles in different years.
Answer: I think a t-test would fit here. You can, if we take the rich folks example, compute an independent sample t-test to compare between their value in 2000 and their value in 2010. Perhaps even better, to see they got richer than other folks, you can compute an independent sample t-test between (rich 2010 - rich 2000) and (ordinary 2010 - ordinary 2000). This way you test if the richest gained more.
Comment: No; comparing means is not the question. It's comparing upper (more generally particular) percentiles.
Comment: Not my question. I'm just the interpreter. Flag @NicolasBourbaki to get their attention. (However, it's rare to want curves for each percentile in 1(1)99 unless the data set is **_enormous_**.)
Comment: I see, so you don't want to test if the top is different than the rest. The examples imply so. Do you want to get 100 means, one for each percentile, and see if they fit a linear or some curve?
Comment: Thanks for your comments. Comparing the means would be easy, but this is exactly what I don't wan't. Actually, I would like to do a paired comparison, but as I said, I'm afraid to get a false effect because of the regression to the mean.
Comment: using a percentile divides the observations to groups. I can't see how this helps you unless you compare the groups somehow, like analyzing their means or the variance (ANOVA).

Comparing upper percentile across time


How to get the dynamic image title from wordpress from "get_the_post_thumbnail" function?

Question: I am learning to code and while developing a test website I liked the way below code snippet worked to have more control and print responsive srcset images. The only thing missing is How can I get the dynamic title which I add inside the wordpress media upload to show on the front end as a result of the code.
Please note that the below snippet prints the alt tag from the image correctly, However, title is not showing...
<?php
    echo get_the_post_thumbnail($post->ID, 'large', array('title' => '', 'sizes' => '(max-width: 480px) 480px, (max-width: 640px) 512px, (max-width: 960px) 960px, (max-width: 1280px) 480px, '));
?>

Answer: One way is to use get_post & pass the ID of the thumbnail & get the title from the object.
echo get_post($post->ID)->post_title;
echo get_post(get_post_thumbnail_id())->post_title;


How to despine a matplotlib and seaborn axes

Question: I'm having difficulty turning the axes on and off in plots, and sizing my axes appropriately. I've followed several threads and my method is:
f1=plt.figure(1,(3,3))
ax=Subplot(f1,111)
f1.add_subplot(ax)
ax.scatter(current,backg,label='Background Height')
ax.plot(current,backg)
ax.scatter(current,peak,color = 'red',label='Peak Spot Height')
ax.plot(current,peak,color='red')
ax.plot(current,meanspot,color='green')
ax.scatter(current,meanspot,color = 'green',label='Mean Spot Height')

ax.spines['left'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('center')
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

But my figures still end up with axes on the top and right, and a strange gap due to the sizing of the axes.
Answer: Can you be more specific what's needed?
You can remove the spine with:
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)

But it sounds like you might be referring to ticks on the spine...
Answer: I highly recommend looking at the seaborn library for this sort of manipulation. Removing spines is as easy as sns.despine().
For example, to make a spineless chart with a white background I might write
import pandas as pd
import numpy as np
import seaborn as sns

df2 = pd.Series([np.sqrt(x) for x in range(20)])
sns.set(style="nogrid")
df2.plot()
sns.despine(left=True, bottom=True)

to get

Have a look at the linked documentation for more details. It really does make controlling matplotlib aesthetics dramatically easier than writing all the code out manually.


Why do I get a wrong output in SPSS with anova for a regression with categorical and continuous variables?

Question: I'm going to paste up a sample dataset so that everyone can see it if necessary (I can't post my own data for ethical reasons).  The data are called Survey.sav and can be downloaded via the survey.zip file here. Displaying the data:
<code> id   toptim sex age

415       22   2  24
  9       19   1  39
425       19   2  48
307       26   1  41
440       18   1  23
484       17   2  31
341       10   2  30
300       25   1  23
 61       13   2  18
 24        9   1  23
138       30   1  27
184       28   2  34
183       16   1  35
144       20   2  43
 57       17   1  50
491       14   1  57
 41       19   2  37
  6       21   2  41
157       17   2  19
287       26   2  25
 53       14   1  23
330       13   2  53
379       25   2  47
 59       14   2  36
416       21   1  21
508       24   2  26
116       20   2  48
422       26   2  24
 87       21   1  25
434       12   2  47
264       29   1  18
442        9   2  24
540       22   1  36
470       27   2  49
252       26   2  21
338       16   2  45
517       19   1  18
179       16   2  45
420       21   1  69
 66        .   1  59
  7       28   1  33
404       28   2  24
331       10   1  22
336       18   2  43
  5       30   2  48
291       19   2  30
 79       17   2  46
342       19   1  24
219       26   1  63
 85       14   2  20
 11       10   2  22
494       21   2  21
127       27   2  37
359       10   2  39
459       24   1  32
301       24   1  54
 31       18   2  27
233       21   2  21
159       30   2  65
139       23   2  32
 20       18   2  42
533       19   1  38
236       19   2  26
154       28   2  52
477       15   2  36
495       26   2  60
344       10   2  33
132       20   1  54
119       30   2  68
340       26   2  66
258       28   2  24
439       27   2  38
 22       24   2  57
458       18   1  50
541       22   1  50
370       17   2  24
362       20   2  46
180       30   2  70
345       18   2  30
229       21   2  21
507        8   2  46
546       27   1  58
133       23   2  23
263       16   1  49
530       23   2  25
109       26   1  33
337       18   1  32
509       24   1  42
235       21   1  26
498       26   2  25
304       23   2  36
437       24   1  24
403       27   2  56
311       24   1  40
499       26   1  36
103       23   2  23
 14       23   1  26
316       26   2  60
 28       20   1  27
209       25   1  70
535       24   2  46
518       23   2  39
377       25   2  55
254       27   1  33
369       18   2  38
231       17   1  26
315       24   2  31
489       20   1  27
438       24   2  35
419       22   1  44
313       28   2  46
538       28   2  28
295       26   2  37
120       26   2  74
368       27   2  82
161       21   1  43
456       21   2  24
 40       20   1  36
 60       27   1  45
290       19   1  50
367       16   1  27
467       26   1  21
 32       16   1  31
217       30   2  49
 58       17   1  49
497       20   2  48
453       24   2  20
485       23   1  25
303       29   2  58
216       29   1  31
188       28   2  29
162       20   1  22
 67       15   2  20
478       23   1  21
228       25   2  26
107       16   1  39
177       17   2  24
 27       28   2  38
176       26   2  53
351       15   2  31
143       26   1  21
317       23   2  53
178       21   2  42
421       26   2  68
701       17   1  47
504       24   1  48
329       14   1  18
128       19   2  39
309       27   1  57
406       20   1  34
134       28   2  41
 70       15   2  30
 63       16   2  19
372       17   1  26
181       29   1  46
158       27   2  58
192       29   2  44
227       21   2  30
312       25   1  21
 36       23   2  22
126       25   2  35
326       28   1  46
163       29   2  22
452       22   2  27
 81       18   2  20
455       23   2  42
 78       24   2  25
332       26   2  31
506       20   2  60
 39       22   2  24
156       22   2  36
206       24   1  36
428       17   2  24
466       23   1  47
435       20   2  34
259       20   1  54
405       23   1  20
266       23   1  55
454       22   1  30
354       21   2  41
193       29   2  23
376       20   1  22
700       19   2  44
519       23   2  21
131       19   2  23
201       24   1  66
534       20   1  39
510       16   2  50
457       22   1  51
411       23   2  49
104       24   2  51
115       26   2  38
129       15   1  50
153       27   1  49
318       24   2  25
 93       16   1  47
185       22   2  36
365       20   2  52
378       21   1  25
500       15   1  22
282       30   2  51
451       19   1  42
 95       24   2  41
302       21   1  43
113       24   2  35
205       16   2  28
545       21   2  28
493       25   2  28
488       23   1  41
483       19   1  40
423       25   2  27
321       23   1  23
 13       30   2  45
410       27   2  48
286       28   1  33
208       21   2  27
490       30   2  22
164       17   1  31
 82       16   2  49
293       24   1  34
292       19   2  33
536       26   2  24
214       20   1  56
402       23   2  23
 56       21   2  48
105       20   1  48
101       25   2  25
246       20   1  65
111       23   1  32
 83       18   2  42
191       28   2  49
486       22   1  24
 94       23   2  21
 89       28   1  22
211       22   1  33
 42       27   1  50
515       27   2  26
251       26   1  45
516       19   1  40
476       30   2  61
 86       20   1  22
110       20   2  27
 80       30   2  22
433       26   2  49
220       29   1  36
409       20   1  46
537       21   2  27
529       15   2  41
461       10   2  21
464       25   1  36
357       22   1  36
 44       25   1  40
204       24   1  35
117       23   1  54
 62       22   1  22
358       23   2  28
429       22   1  25
106       18   1  37
503       18   2  63
353       21   2  48
 29       27   1  21
364       18   1  56
335       26   1  36
355       20   2  37
 84       20   1  37
102       23   2  26
238       30   2  41
135       24   2  49
112       25   2  50
374       21   1  35
469       22   2  55
375       18   1  30
531       26   1  26
256       27   1  48
 52       26   1  51
308        7   2  22
463       20   1  19
265       21   1  30
401       24   2  28
 33       29   1  52
142       24   1  23
443       17   2  25
501       29   2  62
363       23   2  35
262       28   2  36
468       26   2  45
462       26   2  44
 30       24   1  37
296       29   2  24
532       27   2  56
417       24   1  31
186       18   2  44
 15       22   1  51
  1       19   1  45
356       27   1  50
427       19   1  50
 55       21   2  19
436       18   2  60
 38       24   1  56
492       17   1  35
130       14   1  33
237       23   1  32
339       25   1  41
 76       25   1  22
 65       24   2  46
502       23   1  28
310       22   1  33
371       22   2  75
118       24   2  36
207       27   1  36
 17       23   1  33
255       24   2  44
 77       21   1  23
240       22   2  44
108       19   2  40
 12       24   2  31
407       23   2  26
285       16   2  21
702       28   1  46
289       21   2  32
230       24   1  20
511       24   1  21
496       23   1  31
212       25   2  35
542       21   1  26
190       21   2  23
481       25   2  39
189       16   1  46
 23       25   2  60
182       24   1  42
210       26   2  35
418       17   2  22
432       22   2  44
165       17   1  46
430       19   2  29
 68       24   1  55
  2       19   2  21
141       28   2  41
513       26   1  67
479       22   2  53
257       22   2  56
242       21   2  18
234       26   2  44
 91       27   1  51
 34       25   1  64
  8       21   2  38
412       25   1  48
 64       26   1  27
327       17   2  46
226       20   2  48
284       28   2  55
280       26   1  34
528       15   1  27
424       18   2  63
544       22   2  23
137       20   2  22
241       27   2  48
213       27   1  22
373       25   1  32
260       19   2  41
380       24   1  38
480       25   1  29
514       21   2  62
203       26   2  41
152       19   2  31
294       20   1  41
414       19   1  24
361       23   1  48
413       24   1  27
 18       27   2  61
 26       27   2  55
 54       29   2  37
136       21   1  22
  4       24   2  47
276       29   1  42
244       21   1  47
114       21   2  50
366       26   1  19
 43       27   1  37
 16       22   2  37
460       27   2  35
505       16   2  33
232       17   2  22
482       19   2  20
512       22   2  18
218       30   2  31
319       18   1  44
151       24   1  40
 92       21   2  45
278       26   2  36
487       17   2  31
333       23   1  32
279       20   2  50
  3       23   2  42
195       23   2  29
187       19   1  31
261       24   2  27
431       21   1  31
281       27   2  78
 51       21   2  27
526       17   2  28
194       27   1  42
543       18   2  42
352       23   2  34
283       27   2  21
 19       22   2  33
305       25   2  28
 69       20   2  23
328       20   2  21
140       21   2  38
288       19   2  21
160       24   1  22
539       22   1  28
 21       21   2  45
 37       18   1  23
215       17   2  23
465       20   2  25
297       27   2  35
306       20   1  21
239       18   2  40
314       29   2  35
 88       14   1  26
343       18   1  48
242       22   1  54
441       24   2  36
253       21   2  48
 35       18   2  35
 10       23   2  67
527       23   2  47
202       22   2  21
 90       21   1  24
408       20   1  32
360       18   1  50
155        .   2  29
277       22   2  34
520        .   2  52
 25       28   2  48
426        .   2  21
245       23   2  74
</code>
To try to mimic what I'm doing in my own data, I've run a general linear model analysis in SPSS with one continuous dependent variable (in this sample dataset I have used "Total Optimism" as a representative variable), one categorical independent variable ("sex" from the sample dataset) and one continuous independent variable ("age" from the sample dataset, along with the interaction between these two independent variables.
The output table I get is below:
If I then run the same analysis via the regression option in SPSS (I created the interaction term by multiplying the two independent variables together via compute to create the new interaction variable) I get the below output:
My confusion is that the outputs are different (in the general linear model output the interaction term is not significant, whilst in the regression output it is marginally significant.  FYI in my own data I have the opposite effect - my interaction term is highly significant in the general linear model output and not significant at all in the regression output).
Can anyone help me understand these differences?  Could it be something to do with the contrasts used?  Strangely, if I run both the same analyses in R (via lm and via Anova in the car package specifying same sums of squares as in SPSS) both R outputs are the same and equivalent to the regression output in SPSS (but not the general linear model output).
Can anyone shed some light on this for me?  And also help me interpret the outputs? Thank you very much in advance and please let me know if I can edit this question to include more information!
Comment: Despite saying you input age in GLM as continuous variable, you actually input it as a factor. Hence df=55. This what Martijn correctly noticed.
Comment: Put age in Covariates field, go to Model button and select custom model with both main effects and the factor-covariate interaction
Comment: Thank you @ttnphns - after reading Martin's comments I understand now.  I now feel very silly!  But at least I know what my mistake was :)
Answer: I am not sure whether this solves everything, but after a quick look, I guess that your age variable in the SPSS GLM model was treated as a categorical variable (it has df 55 instead of 1). (this could explain why the model matches a higher degree of variance 2155 instead of 405)
Comment: It should be possible. But I don't know SPSS to tell you how to do it. In the worst case, if you can not find the option, you can generate your own interaction term by making an extra variable like you did in R. In the end it is all just the same type of linear matrix equation and the options that we have in the software is all front-end stuff.
Comment: I might just stick to R in future to be honest, haha!
Comment: Ah yes, that works!  So if I add the categorical "sex" variable to fixed factors and then the continuous "age" variable to covariates, along with my generated interaction term, it gives me the same output as the regression option with the correct degrees of freedom for age (1).  I feel very silly now!  Thank you so much for clarifying this for me :)
Comment: Thank you so much for your answer!  This makes sense - how stupid of me.  I was putting age into fixed factors along with sex because I wanted to include the interaction between the two, but it seems like SPSS then treats my continuous variable as categorical (I assumed it would treat it as continuous as it was specified as a scale variable).  Is there anyway to specify continuous by categorical interactions in the General Linear Model option in SPSS, or can you only do this via the regression option? Thank you so very much again!!!


Database consistency check using SQL

Question: I have the next problem:
The database in the system has denormalized tables with lets say "CompanyID" field held on almost every table in the database. This was done due to business rules purposes and should be that way. Sometimes the data is inconsistent as the Customer being with CompanyID == 1 has the order with CompanyID == 2.
My suggestion is to write specialized stored procedure which would be fired every once in a while and analyse some basic 'relation chains' on this property (meaning Cusomer with some Company ID should always have only Orders from the same Company ID where the latter has Articles with corresponding Company ID)
Quesqion:
Is there any generic way in SQL to fetch tables having field CompanyID and then checking them on consistency? Any other solutions to this problem?
I get the tables with the given column name using this SQL:
select column_name, c.is_nullable, c.table_schema, c.table_name, t.table_type, t.table_catalog, *
from information_schema.columns c join information_schema.tables t
on c.table_schema = t.table_schema and c.table_name = t.table_name
where column_name = 'CompanyID'
and table_type not in ('VIEW')
and t.table_name not like 'MsMerge%'
order by ordinal_position

After that I have in mind to traverse by foreign keys up the relation tree for record checking the equality of the CompanyID parameter.
Comment: Business rules don't determine denormalisation, since the entire point of a denormalised schema is that it holds the same data as a normalised schema, but in a format which can be accessed more quickly; albeit at the cost of increased difficulty in maintaining data integrity - as seen here.
Comment: what is the rdbms? oracle? sql server? postgresql? mysql?
Answer: example of query:
lets say these are our tables:
create table customer(
id int,
companyID int
)

create table orders (
orderId int,
customerid int,
companyID int --"wrong column"
)

you should tun queries like these:
update orders
set companyID=(select companyID from customer where id=customerid)

to correct the data, but also eliminate all usages of column companyID  on table orders.
If you have loads of places where this is happening and you want to create an automate way of running the above query, you can look for the column companyID on the table sys.columns, get the table name from it and build a loop to generate the queries
EDIT (based on your answer on the comments):
so the logic is pretty much the same.
Loop through the sys.columns table to get the tables  where the column appears and for each table run:
select *
from orders o
where companyID != (select companyID from customer where id=customerid)

Comment: well the thing is I don't want to correct the data, I want only to detect this inconsistency and return table name with record primary key that would spot the problem and will let me decide later what to do with this data.
Answer: I would not do that via a generic sql that runs every view minutes - this is the performance dead for big databases.
Instead you could use a Insert/Update Trigger on every effected table that you would code like that:
CREATE TRIGGER chk_tablename
ON T1 tablename
FOR INSERT,UPDATE
AS
BEGIN

  // Your checks are here
  // Log inconsistent data

END

Comment: yeah. good point. should discuss it with my team. I like it a lot better then having stored procedures do some strange logic.

Database consistency check using SQL


why my code is not work on block body drupal

Question: function confirm_form($form, &$form_state) {
  global $user;
  $c_user=user_load_by_mail($user->mail);
  $query = db_select('import_contact');
  $query->fields('import_contact', array('name', 'email', 'company', 'operator_insert', 'status', 'matches'))
    ->condition('email', $c_user->mail,'=')
    ->condition('status', 'pending', '=');

  $bolean = $query->execute();
  $data = $bolean->fetchAssoc();
  $check = $bolean->rowCount();

  if(!$check = 0) {
    $data = $bolean->fetchAssoc();
    $nama = $data['name'];
    $company = $data['company'];
  }

  echo "you have been invited by " . $company ".to join.";

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  return $form;
}

Comment: Welcome to Drupal Answers :) Dumping a wedge of code into a question and asking us to guess what you're trying to do, guess what's wrong, and provide you with a fix is bad form - we don't allow it here. We'd love to help with your problem, but to do that we'll need to know what your problem actually is. Please add as much detail as necessary to explain the problem domain, what you expect to happen, what actually happens, and what (if any) error messages you receive. Please see [Writing the perfect question](http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx)
Comment: please explain what your trying to do, in your question
Answer: The most evident errors are:

confirm_form() is a Drupal function; you cannot define a function with that name.
There isn't any reason to use user_load_by_mail($user->mail) when you need only $user->mail, and you can use $user->mail directly, which is what your code is already doing.
!$check = 0 is assigning 0 to $check and then checking $check is not zero. What you want is !check.
A form builder should never use echo. echo "you have been invited by " . $company ".to join."; contains an error, as you moved the concatenation operator inside the string to the right, and PHP is complaining about that.


PHP somando e subtraindo errado

Question: Estou com um problema muito estranho aqui.
É um código para fechamento de caixa, tenho o valor total de vendas, o usuário informa vendas no crédito, débito e dinheiro, caso a soma das 3 seja igual ao total de vendas ele deixa fechar o caixa.
O problema é que pro PHP 211.1 - 211.1 = -2.8421709430404E-14

public function isValid($value, $context = null)
{
    foreach ($context as $key => $valor) {
        $valor = str_replace('.', '', $valor);
        $valor = str_replace(',', '.', $valor);
        $context[$key] = (float)number_format($valor, 2);
    }

    $total = $context['total_pedidos'] - ($context['valor_credito'] + $context['valor_debito'] + $context['valor_dinheiro']);

    var_dump($context);
    var_dump($total);
    die();
    $this->setValue($total);
    if(empty($total)){
        return true;
    }else{
        $this->error(self::FLOAT);
        return false;
    }

}

Outra coisa, como eu usei o number_format($valor, 2) ele deveria colocar 2 casas decimais não?
Quando jogo outros valores ele faz a conta correto.
Estou usando o PHP 5.5.12
Comment: Ler também [Melhor tipo de dados para trabalhar com dinheiro?](https://pt.stackoverflow.com/q/5746/5878)
Comment: E também [Qual a forma correta de usar os tipos float, double e decimal?](https://pt.stackoverflow.com/q/219211/5878)
Answer: O problema em si é a operação com números de ponto flutuante. Eles não são um valor exato, mas sim uma aproximação. Isso ocorre também em qualquer linguagem, não só no PHP. Por exemplo no JavaScritpt:

a = 31.8;
b = 49.9;
c = 129.4;

total = 211.1;

alert(total - (a + b + c));

Temos aqui uma pergunta relacionada com esse assunto e com ótimas respostas (recomendo a leitura para um melhor entendimento):
Resultado impreciso em cálculo com números quebrados
Para resolver seu problema, você pode usar as funções de BC Math para fazer cálculos matemáticos com pontos flutuantes de forma precisa:
<?php

$a = '31.8';
$b = '49.9';
$c = '129.4';

$total = '211.1';

bcscale(2);

$subtotal = bcadd($a, $b);
$subtotal = bcadd($subtotal, $c);

var_dump(bcsub($total, $subtotal));

Veja funcionando.
Ou então fazer essas operações utilizando números inteiros, multiplicando por 100 e no final dividindo tudo por 100:
<?php

$context = [
    'total_pedidos' => '211,10',
    'valor_credito' => '31,80',
    'valor_debito' => '49,90',
    'valor_dinheiro' => '129,40'
];

foreach ($context as $key => $valor) {
    $valor = str_replace('.', '', $valor);
    $valor = str_replace(',', '.', $valor);
    $context[$key] = number_format($valor, 2) * 100;
}

$total = $context['total_pedidos'] - ($context['valor_credito'] + $context['valor_debito'] + $context['valor_dinheiro']);
$total = $total / 100;

var_dump($total);

Veja funcionando.
Outras referências:

Números de ponto flutuante - Documentação do PHP
What Every Programmer Should Know About Floating-Point Arithmeticen
Floating-point cheat sheet for PHPen
Arbitrary-precision decimal arithmetic for PHP 7en
Comment: Funcionou com numeros inteiros, obrigado!
Answer: Aviso:
Embora a questão esteja usando float para manipular moeda, nunca armazene ou manipular dinheiro usando float, pois podem ocorrer erros de arredondamento. Trabalhe com números inteiros internamente, em que o número inteiro representa o número total de centavos. Uma alternativa  é usar o módulo de matemática BCMath, que lida com números de precisão arbitrários com 100% de precisão.

Quanto a questão você esqueceu de formatar a saída e fez um cast para um valor já formatado.
<?php
function isValid($context = null)
{
    foreach ($context as $key => $valor) {
        $valor = str_replace('.', '', $valor);
        $valor = str_replace(',', '.', $valor);
        // Sem o cast (float) se o fizer você perde a formatação.
        $context[$key] = number_format($valor, 2);
    }

    // Faltou formatar o resultado
    $total = number_format(
               $context['total_pedidos'] - (
               $context['valor_credito'] +
               $context['valor_debito'] +
               $context['valor_dinheiro']), 2);

    var_dump($context);
    var_dump($total);

}

$arr = [
'total_pedidos' => '211,1',
'valor_credito' => '31,8',
'valor_debito'  => '49,9',
'valor_dinheiro'=> '129,4'
];

isValid($arr);
?>

Resultado:
array(4) {
  ["total_pedidos"]=>
  string(6) "211.10"
  ["valor_credito"]=>
  string(5) "31.80"
  ["valor_debito"]=>
  string(5) "49.90"
  ["valor_dinheiro"]=>
  string(6) "129.40"
}
string(4) "0.00"

Link para o código.

PHP somando e subtraindo errado