Filtro com chekbox mysql php

Question: Poderia me ajudar como eu faria a concatenação de strings nessa pesquisa, passando qual a tabela certa se seria a i ou c?
select i.nome from imovel i join cidade c
where c.nome in ("Campo Grande","Paranaíba")
and i.tipo in ("Casa","Apartamento")
and i.dormitorio in ("4","5")

function parseMysqlQuery($array) {

    $output = '';
    foreach ($array as $key => $value) {
        $output .= !$output ? " WHERE $table.$key " : " AND $table.$key";
        $output .= ' IN ("' . implode('","', $value) . '")';
    }
    echo "Key: $key; Value: $value
\n";
    return $output;
}

$array = (array)filter_input_array(INPUT_POST,
        array( 'num' =>
                 array( 'filter' => FILTER_SANITIZE_STRING,
                        'flags' => FILTER_FORCE_ARRAY),
                        'grupo'  => array(
                            'filter' => FILTER_SANITIZE_STRING,
                            'flags'  => FILTER_FORCE_ARRAY,
                        ),
                        'dt_de_inducao' => array(
                            'filter' => FILTER_SANITIZE_STRING,
                            'flags'  => FILTER_FORCE_ARRAY,
                        ),
                )
        );

//SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = 'folha_tarefa' if ($_SERVER['REQUEST_METHOD'] == "POST") { $array = array_filter($array);

$in = parseMysqlQuery($array);

$query = "SELECT * FROM contratos " . $in;.

Esse é um filtro com checkbox, só que eu tenho uma pesquisa que faço um join left e nao estou conseguindo fazer com que meu php ele entenda em qual table deve pesquisar, ex nometable.nomecoluna
Comment: Poderia ser mais específico na sua questão?
Comment: Por que [abriu duas perguntas relativas do mesmo assunto](http://pt.stackoverflow.com/questions/143876/filtro-php-chebox), sendo uma delas mais incompleta?
Comment: poxa estou apredendo a usar o site e nao tinha visto o botão de edição, por isso que abri 2
Comment: Você precisa realmente adotar esse método mal escrito? Uma observação, "concatenização" não existe, o correto é "concatenação".
Pelo que parece `$table.$key` seria `nome_tabela.campo` e `$value` o valor da tabela, então o correto seria passar uma estrutura de array que tivessem cada um desses parâmetros. Sendo que `$table`, não faz parte da função, mas uma variável externa. `$array = array(array('campo'=>'valor'),array('campo'=>'valor'), ...);`
Comment: Obrigado pela correção Ivan Ferrer, já corrigi. bem eu preciso fazer um filtro e fazendo uma pesquisa achei esse método, (eu sou nova em programação php), comecei a estudar esse código e me deparei com esse problema de como colocar o nome da tabela concatenado com o campo, se você tiver algum link de estudo sobre o assunto de filtros para que eu possa estudar agradeço, vou tentar usar essa forma que você escreveu pra mim.
Answer: Pelo que parece $table.$key seria nome_tabela.campo e $value o valor da tabela, então o correto seria passar uma estrutura de array similar a este formato:
$table = 'imovel';

$arrIn = array(
           array('c.nome'=> array('Campo Grande','Paranaíba')),
           array('i.tipo'=> array('Casa','Apartamento')),
           array('i.dormitorio'=> array('4','5'))
        );
$in = parseMysqlQuery($arrIn);

$query = "select i.nome from {$table} i join cidade c on(c.id_cidade=i.id_fk_cidade)
where c.nome " . $in;

Filtro com chekbox mysql php


Clarify an old answer: Why we can let $\vert x \vert = \frac{1}{1+t}$ for $\vert x \vert <1$

Question: I am looking for a clarification on this answer about why $\lim_{n\to\infty}nx^n=0$.
Specifically, the answer has two aspects I am concerned about: First, the answer begins by saying

We prove the result under the slightly weaker condition $|x|\lt 1$.
Let $|x|=\dfrac{1}{1+t}$. Then $t\gt 0$.

Why can we let $\vert x\vert = \frac{1}{1+t}$?,

And $t>0$ because $\vert x\vert$ must be positive, correct?

Second, the answer claims

By the Binomial Theorem, if $n \ge 2$, then
$$(1+t)^n \ge 1+nt +\frac{n(n-1)}{2}t^2 \gt \frac{n(n-1)}{2}t^2.$$

Why is the first inequality weak? We are leaving off terms from the binomial formula, and $t>0$, so shouldn't it be $>$ (strict)?
Note: The original answerer has not be active in over a year, which is why I post here rather than leave a comment.
Comment: In that first case you can actually just solve for $t$, but even if you couldn't do this algebraically you can still make such a substitution for a monotone function by using the intermediate value theorem. As for the second thing, since $t>0$ the inequality is strict as soon as $n>2$. This is really nitpicking though because the strictness isn't needed for the argument at all.
Comment: "Why can we let |x|=1/1+t?".  Because if $0< |x| < 1$ then  $\frac 1{|x|} > 1$.  And $\frac 1{|x|} - 1 > 0$.  So let $t = \frac 1{|x|} - 1; t > 0$ then $|x| = \frac 1{1+t}; t> 0$.
Comment: @user106860 Yes, that's right, and $t''$ is unique as a solution to the equation.
Comment: "We are leaving off terms from the binomial formula, and t>0, so shouldn't it be > (strict)?"  Only if $n > 2$ if $n = 2$ it's an equality.
Comment: "And t>0 because |x| must be positive, correct?"  Not really.  $t > 0$ because we are assuming $|x| < 1$.
Comment: @Ian Sorry, you lost me when talking about substituting for monotone function. You are just saying that since $\frac{1}{1+t}$ is monotone, I can find values of $t$ such that $\frac{1}{1+t} <\vert x\vert$ and another value $t'\not=t$ such that $\frac{1}{1+t}>\vert x\vert$ and then by IVT there is a $t''$ such that $\frac{1}{1+t} =\vert x\vert$?
Comment: @Ian Thank you c
Answer:
$t$ has to be non-negative because $\;0<|x|=\dfrac1{1+t}<1$, so the denominator has to be  $>1$.
The first inequality is weak because it's an equality if these are the only terms of the binomial expansion, which happens if $n=2$.


Minimum Least Squares Solution Using Pseudo Inverse (Derived from SVD) Is The Minimum Norm Solution - Extension from Vectors to Matrices

Question:
Given $A \in \mathbb{R}^{m \times n}$, $B \in \mathbb{R}^{k \times \ell}$, and $C\in \mathbb{R}^{m \times \ell}$. Show that for $X \in \mathbb{R}^{n \times k}$
$$ {A}^{\dagger} C {B}^{\dagger} = \arg \min_{X} {\left\| C - A X B \right\|}_{F} $$
is the unique solution.
Note: This is an extension of the minimum $2$-norm least squares problem.
Hint: Use the SVDs of $A$ and $B$.

This is a homework problem, but I got stuck. After I do SVD for both $A$ and $B$, I discard the $0$ terms to get a slim SVD. Then I can factor out some things, but still don't see an explicit expression formulating. Could somebody guide me in the right direction?
Edit:
1."F norm": Frobenius norm
2. $A^+$ or $A^\dagger$: conjugate transpose, Moore–Penrose pseudoinverse
Comment: Hopefully the Frobenius norm, not a criminal norm?
Comment: Frobenius norm, yes.
Comment: Not your question, but one can also do this with a single SVD:
flatten $A X B$ to $\sum_{ij}$ vec $( A_{\bullet i} \otimes B_{j \bullet} ) \, X_{ij} $ .
Answer: First, I am not exactly sure what you are asking. I am assuming that you know that the minimal norm solution to the least squares problem $\min_x {1 \over 2} \|Ax-y\|^2$ is given by $x=A^{\dagger} y$ and that the issue to show that the solution to problem
$\min_X {1 \over 2} \| C-AXB\|_F^2$ is given by $A^{\dagger} C B^{\dagger}$.
Second, note that $A^{\dagger} C B^{\dagger}$ is not necessarily a unique solution, however, of all solutions (and is at least one solution), it is the one of minimal norm.
For example, if $A=0$ then any $X$ is a solution, but clearly not unique.
One approach is to determine the SVD with respect to the basis $E_{ij} = e_i e_j^T$ and translate this into the above form.
Note that if we can write $A = \sum_k \sigma_k u_k v_k^T$ (or equivalently, $Ax = \sum_k \sigma_k u_k \langle v_k, x \rangle$ for all $x$), where $\sigma_k \ge 0$, $u_k$ & $v_k$ are orthonormal, then we can extract the SVD from this formulation in the usual sort of way. Then
$A^{\dagger} = \sum_{\sigma_k \neq 0} {1 \over  \sigma_k } v_k u_k^T$ (or
equivalently, $A^{\dagger} x = \sum_{\sigma_k \neq 0} {1 \over  \sigma_k } v_k \langle u_k, x \rangle$ for all $x$).
So, we look for a similar expansion for
the operator $L(X) = AXB$.
\begin{eqnarray}
AXB &=& (\sum_k \sigma_k(A) u_k(A)v_k(A)^T ) X (\sum_k \sigma_k(B) u_k(B)v_k(B)^T ) \\
&=& \sum_{k,l} \sigma_k(A) \sigma_l(B) u_k(A)v_k(A)^T X u_l(B)v_l(B)^T \\
&=& \sum_{k,l} \sigma_k(A) \sigma_l(B) u_k(A) v_l(B)^T v_k(A)^T X u_l(B) \\
&=& \sum_{k,l} \sigma_k(A) \sigma_l(B)  u_k(A) v_l(B)^T \operatorname{tr} (v_k(A)^T X u_l(B)) \\
&=& \sum_{k,l} \sigma_k(A) \sigma_l(B)  u_k(A) v_l(B)^T \operatorname{tr} ( u_l(B) v_k(A)^T X ) \\
&=& \sum_{k,l} \sigma_k(A) \sigma_l(B)  u_k(A) v_l(B)^T \langle  v_k(A) u_l(B)^T , X \rangle  \\
\end{eqnarray}
Hence $L$ has singular values $\sigma_k(A) \sigma_l(B) $ with left singular vectors $u_k(A) v_l(B)^T$ and right singular vectors $v_k(A) u_l(B)^T$.
A quick check shows that the singular vectors are orthonormal.
Hence $L^{\dagger}$ has the representation
\begin{eqnarray}
L^{\dagger}(X) &=& \sum_{\sigma_k(A) \sigma_l(B) \neq 0} {1 \over \sigma_k(A) \sigma_l(B)}  u_k(A) v_l(B)^T \langle  v_k(A) u_l(B)^T , X \rangle  \\
&=& \sum_{\sigma_k(A) \sigma_l(B) \neq 0} {1 \over \sigma_k(A) \sigma_l(B)}  u_k(A) v_l(B)^T \operatorname{tr} ( u_l(B) v_k(A)^T X ) \\
&=& \sum_{\sigma_k(A) \sigma_l(B) \neq 0} {1 \over \sigma_k(A) \sigma_l(B)}  u_k(A) v_l(B)^T   v_k(A)^T X u_l(B) \\
&=& \sum_{\sigma_k(A) \sigma_l(B) \neq 0} {1 \over \sigma_k(A) \sigma_l(B)}  u_k(A) v_k(A)^T X u_l(B)  v_l(B)^T   \\
&=& ( \sum_{\sigma_k(A) \neq 0} {1 \over \sigma_k(A)}  u_k(A) v_k(A)^T ) X ( \sum_{\sigma_l(B) \neq 0} {1 \over \sigma_l(B)} u_l(B)  v_l(B)^T  ) \\
&=& A^{\dagger} X B^{\dagger}
\end{eqnarray}
Hence the minimal norm solution to the least squares problem is given by
$L^{\dagger}(C) = A^{\dagger} C B^{\dagger}$.
Answer: You don't need singular value decomposition here. Observe that $\pi_1:C\mapsto AA^+CB^+B$ and $\pi_2:X\mapsto A^+AXBB^+$ are orthogonal projections on their respective matrix spaces with respect to Frobenius inner products. Also,
$$
(C-\pi_1(C))=(\operatorname{id}-\pi_1)(C)\perp \pi_1(AXB)=AXB=A\pi_2(X)B.
$$
It follows that
$$
\|C-AXB\|_F^2 = \|C-\pi_1(C)\|_F^2 + \|\pi_1(C)-A\pi_2(X)B\|_F^2
$$
and the problem boils down to minimising $\|\pi_1(C)-A\pi_2(X)B\|_F$. As this quantity involves only the projected component $\pi_2(X)$ of $X$ but not its orthogonal component, in a least-norm solution, we must have $X=\pi_2(X)$.
Clearly, when $X=A^+CB^+$, we have $X=\pi_2(X)$ and $\|\pi_1(C)-A\pi_2(X)B\|_F=0$. Therefore the minimum possible value (i.e. zero) of $\|\pi_1(C)-A\pi_2(X)B\|_F$ is attained and $A^+CB^+$ is indeed a least-norm minimiser.
If there is another least-norm minimiser $X$, we must have $X=\pi_2(X)$ and $\|\pi_1(C)-A\pi_2(X)B\|_F=0$. Thus we get
\begin{align}
A\pi_2(X)B &= \pi_1(C),\\
A^+A\pi_2(X)BB^+ &= A^+\pi_1(C)B^+,\\
\pi_2(\pi_2(X)) &= A^+AA^+CB^+BB^+,\\
X &= A^+CB^+
\end{align}
and we conclude that the least-norm solution is unique.
Answer: Deriving the Least Squares Solution
Since:
$$ \arg \min_{X} {\left\| C - A X B \right\|}_{F} = \arg \min_{X} \frac{1}{2} {\left\| C - A X B \right\|}_{F}^{2} $$
One could use the second term which is easier to work with.
Defining $ f \left( X \right) = \frac{1}{2} {\left\| C - A X B \right\|}_{F}^{2} $ then:
$$ \frac{d}{dX} f \left( X \right) = -{A}^{T} \left( C - A X B \right) {B}^{T} $$
Finding when the Derivative vanishes:
$$\begin{align*}
\frac{d}{dX} f \left( X \right) = 0 & \iff -{A}^{T} \left( C - A X B \right) {B}^{T} = 0 \\
& \iff {A}^{T} A X B {B}^{T} = {A}^{T} C {B}^{T} \\
& \Rightarrow X = {\left( {A}^{T} A \right)}^{-1} {A}^{T} C {B}^{T} {\left( B {B}^{T} \right)}^{-1} = {A}^{\dagger} C {B}^{\dagger}
\end{align*}$$
Deriving the Least Norm Solution
We're after the Least Norm solution of all Least Squares solutions:
$$\begin{align*}
\hat{X} = \arg \min_{X} \: & \: {\left\| X \right\|}_{F} \\
\text{subject to} \: & \: X \in \left\{ Z \mid {\left\| A Z B - C \right\|}_{F} = \min_{Z} {\left\| A Z B - C \right\|}_{F} \right\}
\end{align*}$$
The Singular Value Decomposition (SVD) of $ A = {U}_{A} {\Sigma}_{A} {V}_{A}^{T} $ and $ B = {U}_{B} {\Sigma}_{B} {V}_{B}^{T} $ and reminder that for any Unitary Matrix $ U $ the following holds $ {\left\| X \right\|}_{F} = {\left\| U X \right\|}_{F} $.
Then:
$$\begin{align*}
\min_{Z} {\left\| A Z B - C \right\|}_{F} & = \min_{Z} {\left\| {U}_{A} {\Sigma}_{A} {V}_{A}^{T} Z {U}_{B} {\Sigma}_{B} {V}_{B}^{T} - C \right\|}_{F} \\
& = \min_{Z} {\left\| {\Sigma}_{A} {V}_{A}^{T} Z {U}_{B} {\Sigma}_{B} - {U}_{A}^{T} C {V}_{B} \right\|}_{F} \\
& = \min_{Y = {V}_{A}^{T} Z {U}_{B} } {\left\| {\Sigma}_{A} Y {\Sigma}_{B} - {U}_{A}^{T} C {V}_{B} \right\|}_{F}
\end{align*}$$
Since both $ {\Sigma}_{A} $ and $ {\Sigma}_{B} $ are diagonal matrices the solution $ \hat{Y} $ is unique and is given by $ \hat{Y} = {{\Sigma}_{A}}^{\dagger} {U}_{A}^{T} C {V}_{B} {{\Sigma}_{B}}^{\dagger} $.
Since it is unique it is also the Least Norm solution.
This implies $ \hat{Z} = {V}_{A} {{\Sigma}_{A}}^{\dagger} {U}_{A}^{T} C {V}_{B} {{\Sigma}_{B}}^{\dagger} {U}_{B}^{T} = {A}^{\dagger} C {B}^{\dagger} $ is the least Norm Solution.
Comment: This does not show that $X$ is the minimal norm solution.
Comment: @copper.hat, Edited to answer this directly as well.


Custom SIP Header In Kamailio

Question: I need to set a custom header in Kamailio 3.3.1 from a Lua script.
I can theoretically set one in the config script like this -
append_hf("X-MyHeader: myvalue\r\n");

but I cannot work out how to call it from a Lua script, which is my preferred place to do this from. I have the following in my CFG file -
modparam("app_lua", "load", "/usr/local/etc/kamailio/lua/kamailio.lua")
modparam("app_lua", "register", "sl")
modparam("app_lua", "register", "rr")
modparam("app_lua", "register", "tm")

and my Lua script works fine as far as it goes. Documentation seems to suggest that append_hf() is part of the textops module, but I can see no way to expose that to the Lua script.
Please can anyone help me understand this or maybe suggest an alternative way to achieve my goal?
Answer: Done it.
You need to use -
sr.hdr.append("header: value\r\n");

I've spent 2 days looking for that. And 5 mins after posting I find it. Here's the URL where i got the answer from -
http://www.kamailio.org/wiki/embeddedapi/3.2.x/lua

Custom SIP Header In Kamailio


Passing AWS role to the application that uses default boto3 configs

Question: I have an aws setup that requires me to assume role and get corresponding credentials in order to write to s3. For example, to write with aws cli, I need to use --profile readwrite flag. If I write code myself with boot, I'd assume role via sts, get credentials, and create new session.
However, there is a bunch of applications and packages relying on boto3's configuration, e.g. internal code runs like this:
s3 = boto3.resource('s3')
result_s3 = s3.Object(bucket, s3_object_key)
result_s3.put(
                Body=value.encode(content_encoding),
                ContentEncoding=content_encoding,
                ContentType=content_type,
        )

From documentation, boto3 can be set to use default profile using (among others) AWS_PROFILE env variable, and it clearly "works" in terms that boto3.Session().profile_name does match the variable - but the applications still won't write to s3.
What would be the cleanest/correct way to set them properly? I tried to pull credentials from sts, and write them as AWS_SECRET_TOKEN etc, but that didn't work for me...
Answer: Have a look at the answer here:
How to choose an AWS profile when using boto3 to connect to CloudFront
You can get boto3 to use the other profile like so:
rw = boto3.session.Session(profile_name='readwrite')
s3 = rw.resource('s3')

Comment: Maybe something like this can help:
https://ripon-banik.medium.com/aws-assume-role-script-4e7d3b548f20
Look at example 1, put that in a bash script and source it before you call your application
(You will need JQ installed, and you can ignore the last 3 lines of the script if you only need it for this one use case)
Comment: Thanks Nathan. I am afraid I cannot do that because boto3 is used within the application I use (great expectations) which I ran via terminal, and I hope I won't need to maintain custom copy
Answer: I think the correct answer to my question is one shared by Nathan Williams in the comment.
In my specific case, given that I had to initiate code from python, and was a bit worried about setting AWS settings that might spill into other operations, I used
the fact that boto3 has DEFAULT_SESSION singleton, used each time, and just overwrote this with a session that assumed the proper role:
hook = S3Hook(aws_conn_id=aws_conn_id)
boto3.DEFAULT_SESSION = hook.get_session()

(here, S3Hook is airflow's s3 handling object). After that (in the same runtime) everything worked perfectly


Should I use a new core version that doesn't yet have its first stable release for production?

Question: I want to start using Drupal 8.
Can I use it in its current state for a simple blog site built with  views?
Comment: I can't remember where I saw the following (paraphrased) quote (I think it was chx either on here or D.o) - but I think it's something like the following:  *"If you have to ask this question, then you shouldn't use it."*
Comment: The answer to this question would also be irrelevant, since you should use it just for testing, debugging, or providing patches. If you are developing modules, you could be interested in checking how Drupal core code is going to change, but using it in production servers is quite different.
Answer: The real question is: what's the Drupal core lifecycle look like?
First, we have feature thaw. During this time it's really a bad idea to even try to build anything on it. It's likely to be broken, buggy, and so on. (Although now we have extensive test coverage so it's less broken than you'd think.)
Then there's a feature freeze. People with some knowledge, patience and not too important sites -- say, a blog :) -- could start testing. It'd be great if this happened. You need to understand that there is still no guarantee that your data can stay intact from one git pull to the next. (This is where we are now.)
The next step is beta. From beta and on we support upgrades. So while it's not the quality you'd expect from a released product, it's time to begin serious testing. If you start testing when release happened, your problems will take a lot longer to be fixed.
Answer: We should always prefer the latest stable release. You may know that the alpha, beta versions are not completed so after all the issues are fixed & testing everything the author will update the stable release. Once we got the stable release then we can update our site later.


Alternative fingering for high B-flat to D, on the oboe

Question: Does anybody know of an alternative way to finger high B-flat to D on the oboe?
In Malcom Arnold's Divertimento for Flute, Oboe, and Clarinet, there is a vivace 16th note section alternating between B-flat and D.  I cannot this play cleanly with normal fingerings.  Suggestions?
Answer: The standard B flat fingering along with the third D fingering on this list http://www.wfg.woodwind.org/oboe/ob_alt_3.html might work for you, it at least makes it so you only have to move one finger on each hand.
You've probably already looked there though, but if not, you might find some other workable combinations.
Comment: Every instrument has some ugly note transitions somewhere :-( .  A lot of practice techniques such as "galumph" , aka playing first as dotted eighth-sixteenth and then sixteenth-dotted-eighth thru the note sequence, will help.


Get Total of Grid and Show on Label in DataList both Grid and Label are Inside DataList

Question: I have a DataList. Label and Grid are Inside ItemTemplate of DataList i have Bound the Grid noe i want to show Total Of Grid Column in Label . Which is Placed in Parent control of Grid
Comment: Can you provide your code and please be specific what exactly u want.
Comment: Please be clear. Do you want to show the total no. of columns or the total of a column? Also post your code. It will give a picture of what you have tried.
Answer: You can Count the giridview columns by using this code:
<code>int colCount = GridView1.Items[0].Cells.Count;
</code>


Retornar valores de duas colunas de tabelas diferentes

Question: Tenho a tabela ALUNOS e a tabela PROFESSORES, há mais professores que alunos, queria realizar uma consulta que traga alunos em uma coluna e professores em outra e não todos em uma só.
Todos em uma coluna ficou assim:
SELECT PK_PROFESSORES AS CÓDIGO_ALUNO_PROFESSOR, NOME, IF(PK_PROFESSORES != NULL, 'ALUNO', 'PROFESSOR') AS VÍNCULO
FROM PROFESSORES
UNION
SELECT PK_ALUNO, NOME, IF(PK_ALUNO != NULL, 'PROFESSOR', 'ALUNO') AS VÍNCULO
FROM ALUNOS

Há a possibilidade de trazer cada um em uma coluna?
Comment: Só por curiosidade por que você faz isso dos `IF`?
Comment: Como é o relacionamento das suas tabelas?
Comment: Porque como retornou tudo em uma coluna, eu queria diferenciar os alunos de professores, o IF é uma nova coluna que diz se é professor ou aluno. hehehe
Comment: Professores e alunos não estão relacionados.
Comment: E por que não faz um produto cartesiano? Exemplo `SELECT * FROM ALUNOS, PROFESSORES`
Comment: Retornou dados repetidos, mesmo usando o distinct.
Answer: Consegui fazer aqui, veja se é o que você precisa
SELECT PROFESSOR.NOME, ALUNO.NOME FROM
(
  SELECT @rownum_p:=@rownum_p+1 AS ROW_NUM, PROFESSORES.NOME FROM PROFESSORES, (SELECT @rownum_p:=0) r
)  PROFESSOR
LEFT JOIN
(
   SELECT @rownum_a:=@rownum_a+1 AS ROW_NUM, ALUNOS.NOME FROM ALUNOS, (SELECT @rownum_a:=0) r
) ALUNO ON PROFESSOR.ROW_NUM=ALUNO.ROW_NUM

Basicamente criei um "ID FALSO" e vinculei eles.
SqlFiddle DEMO


Views - sticky items per page paginated using several displays (Hooks or UI way)

Question: Hi and thanks for take the time to read this post.
(I want to know if exist some hook or User Interface way to make this)
I have two content types (content type X and content type Y), and I want to build one view that have the nodes of content types X and Y, but that shows always sticky items for X content type per each paginated page.
For example: the view will show 10 items per page.
to the first page --> the first 3 items will have positions 1,2,3 to X content type nodes, and the last positions 4,5,6,7,8,9,10 will have Y content type nodes.
to the second page --> the first 3 items will have positions 11,12,13 to X content type nodes, and the last positions 14,15,16,17,18,19,20 will have Y content type nodes.
I am reviewing modules like Nodequeue or Weight but I can not understand how to reach this using this modules (i think that this is a useful way with nodequeue: http://www.absolutecross.com/tutorials/drupal/custom-re-ordered-front-page-teasers but my question about how to sort automatically in the required way still is pending)
I must say that I have some aproximation to resolve this using a php snippet in one page: I use one pagination control that handle a SQL query with all results (all content types needed), and the other two SQL queries will call the respective quantity of data to each kind of content type (the sum of results of all individual queries, must be the number in the respective pagination parameter)...it works, but, like I already indicated, I want to know if exist some hooks way, or User Interface way, to make this because I will use several exposed filters that will change SQL queries.
Thanks in advance.
Comment: Does every page show X nodes 1-3? Or does page two show X nodes 4-6, page three shows X nodes 7-9, etc.
Comment: hi rocketeerbkw, the answer to your question is yes. first page shows X nodes 1-3, second page X nodes 4-6, three X nodes 7-9. thanks for your question, to be more clear.
Comment: more information: I am using Drupal 6 and views 2.
Answer: First off, this can all be done in views, no custom code required (yay!). This is accomplished by creating an attachment view which will inherit the pager settings.
First you create a view with a page display that shows 7 Y nodes only.
Second, on the same view, add an attachment display. For the attachment, set it to show 3 X nodes only. In the attachment settings, make sure that Inherit arguments, Inherit exposed filters, and Inherit pager are set to yes. Set Render pager to no. Set Position to before and set Attach To to your page display.
Comment: awesome! thanks. this is the way!. I detected that if a expose a filter to select Content Types in Default display, the attached content type items will not change according the filter (may be is related with that are forced a content type in each display) so it's necessary expose the filter in the attached display (It can be infered of your answer).
Comment: I have a related question (restrict items listed in exposed filter (to list only 'default display' and 'attached display' filters in the exposed filter)
http://drupal.stackexchange.com/questions/8926/views-restrict-items-listed-in-exposed-filter-to-list-only-default-display-a


Is there a word for the phenomenon that the old are generally less affected by risk factors?

Question: In epidemiology, this occurs often: Old people are less prone to the influence of risk factors. For example, the Framingham risk score, which tries to estimate cardiovascular risk, gives 8 or 9 points to smokers in their twenties and thirties, but only 1 point to those in their seventies (more points correspond to higher risk).
While several effects, including some selection effects, may contribute to this, the main reason is that the old have less to lose. Their baseline risk of most diseases and death is higher due to their age, and there is only so much "risk substrate" or "potential" or "residual disease-free life" left for any other risk factor to occupy.
Is there a word for this phenomenon?
Comment: "tough old bird" is idiomatic. Some sources claim that it implies females, but I've heard it applied to all genders.
Answer: Survival bias and competing risks.  Also, elderly having a high value of a risk factor who have not been affected by that risk factor have demonstrated a robustness to that particular factor in general.  This is why age $\times$ risk factor interactions can be important to pre-specify in a model.
Answer: Age $\times$ risk factor interaction are, as mentioned, important. Some risk factors may have a different form of association in later life. Low values of some risk factors can be associated with mortality due to them being possible indicators of poor health in later life, leading to a u-shaped relationship. There is an interesting article by Abdelhafiz et al that discusses this phenomena:
The U-shaped Relationship of Traditional Cardiovascular Risk Factors and Adverse Outcomes in Later Life


Cannot get exim4 & gmail working

Question: I've been following various guides on how to send email from an ubuntu server via exim and my gmail account.  I think that i've been around the block so many times on this that i've cocked it up somehow.  Can anyone see what's not right?
I don't want to include any real email addresses or domain names in this forum post, so whenever they appear in config files etc i'm going to swap them out as follows.  I'm going to use silly names instead of descriptive names so they don't get mixed up with system variables etc.
domain name for the server:                bigspanners.com
username i'm logged into the server as:    jimmy
hostname on the server:                    spanners
"long" hostname (result of `hostname -f`): spanners.bigspanners.com
the gmail address i want to send from:     email@example.com
gmail password:                            abcdefgh

My exim4 config (/etc/exim4/update-exim4.conf.conf) looks like this:
#/etc/exim4/update-exim4.conf.conf

dc_eximconfig_configtype='smarthost'
dc_other_hostnames=''
dc_local_interfaces='127.0.0.1 ; ::1'
dc_readhost='bigspanners.com'
dc_relay_domains=''
dc_minimaldns='false'
dc_relay_nets=''
dc_smarthost='smtp.gmail.com::587'
CFILEMODE='644'
dc_use_split_config='true'
dc_hide_mailname='false'
dc_mailname_in_oh='true'
dc_localdelivery='mail_spool'

I have this in /etc/exim4/passwd.client file:
# password file used when the local exim is authenticating to a remote
# host as a client.
#
# see exim4_passwd_client(5) for more documentation
#
# Example:
### target.mail.server.example:login:password
gmail-smtp.l.google.com:email@example.com:abcdefgh
*.google.com:email@example.com:abcdefgh
smtp.gmail.com:email@example.com:abcdefgh

And i have this in /etc/email-addresses
# This is /etc/email-addresses. It is part of the exim package
#
# This file contains email addresses to use for outgoing mail. Any local
# part not in here will be qualified by the system domain as normal.
#
# It should contain lines of the form:
#
#user: email@example.com
#otheruser: firstname.lastname@example.com
jimmy: firstname.lastname@example.com
jimmy@localhost: email@example.com
jimmy@hostname1: email@example.com
firstname.lastname@example.com: firstname.lastname@example.com

I'm trying to send an email like so:
echo "This is a test." | mail -s Testing firstname.lastname@example.com

And i see this in my /var/log/exim4/mainlog file:
2014-04-16 13:57:50 1WaQLW-0002dC-9k <= firstname.lastname@example.com U=jimmy P=local S=445
2014-04-16 13:57:51 1WaQLW-0002dC-9k plain authenticator failed H=gmail-smtp-msa.l.google.com [126.96.36.199] 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbsuj
534-5.7.14 B3VqEmiHZ1icpT_O185MznNwotW78A3DdsElZPGAkfsIWQdd5de1lNbXoR-xdqK5Ko_Rs5
534-5.7.14 8zXBl2vzfUDPSF08dULZVaF8z999DpQNslK02DfEG0YcTUOxLqoosEzuxHA1vG-_bIxi9L
534-5.7.14 hMsYKcMkWJ7g4wf4xG-LK6c1sKQhFx-u1zcsUyLykkzI1qtGXYF5NC-ZpdnALCdQZ3hzUh
534-5.7.14 aAoXTLg> Please log in via your web browser and then try again.
534-5.7.14 Learn more at https://support.google.com/mail/bin/answer.py?answer=787
534 5.7.14 54 tz6sm39939105obc.10 - gsmtp
2014-04-16 13:57:51 1WaQLW-0002dC-9k login authenticator failed H=gmail-smtp-msa.l.google.com [220.127.116.11] 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvT4
534-5.7.14 CHLiBFGLWWXIojohtPd9cIM1vLOt1MMyjSUzcFdXOarqE18FzPPxmU9e32_CveJOax5nnB
534-5.7.14 _7-adgi6NtHxbChOOxjRpd8xZCLgNnSjiHFWcbYeLLCZbv-7l6zylE7poh8nFe7SgTEINk
534-5.7.14 Lx1XvFQH1RuA1HJCwsC6nC-jG_4HQF7TY8ma9PBJWv4gz9fNna7wZFX4eeWtFTgpg7dKrX
534-5.7.14 wGYmwwQ> Please log in via your web browser and then try again.
534-5.7.14 Learn more at https://support.google.com/mail/bin/answer.py?answer=787
534 5.7.14 54 tz6sm39939105obc.10 - gsmtp
2014-04-16 13:57:51 1WaQLW-0002dC-9k ** email@example.com R=smarthost T=remote_smtp_smarthost: SMTP error from remote mail server after MAIL FROM:<email@example.com> SIZE=1480: host gmail-smtp-msa.l.google.com [184.108.40.206]: 530-5.5.1 Authentication Required. Learn more at\n530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 tz6sm39939105obc.10 - gsmtp
2014-04-16 13:57:51 1WaQLX-0002dT-QM <= <> R=1WaQLW-0002dC-9k U=Debian-exim P=local S=1595
2014-04-16 13:57:51 1WaQLW-0002dC-9k Completed
2014-04-16 13:57:52 1WaQLX-0002dT-QM plain authenticator failed H=gmail-smtp-msa.l.google.com [184.108.40.206] 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbt1l
534-5.7.14 LT1ysO4nisev9cFYPlQZ0Lk_Y0VvwVmgOWTJOEbZzYo5JOP9vMCoh-wwEXhq-8pUkhKejG
534-5.7.14 KRUNECT5VykeH6TSNQ8MR8ZoXe36ZZhpiZXCr0Smbjq512Lq_D5tozz9A9o1_JQePR05w6
534-5.7.14 RjyOK481V2Th6RhKwKqlBsvxfWKbs7twgxyklxBTRihFUEkJpFXDzvWNfUNJhkQq0MJnHv
534-5.7.14 53T4KeQ> Please log in via your web browser and then try again.
534-5.7.14 Learn more at https://support.google.com/mail/bin/answer.py?answer=787
534 5.7.14 54 sm4sm23756569obc.3 - gsmtp
2014-04-16 13:57:53 1WaQLX-0002dT-QM login authenticator failed H=gmail-smtp-msa.l.google.com [126.96.36.199] 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbt09
534-5.7.14 0DeijxwxzvClKfwY3oc1IhhkdSt_3suNtEsij4ibdikNtCMCiogd6mOma3obSAu2J5PlVh
534-5.7.14 QnZAN8CalbSYOX7mzuQyVqErqsY9uv5yuObmXk8771WBGrOzIxaOTnzChpd_ZVVeSGfmnh
534-5.7.14 qU5KHUrFo-qvHuYuqNZ1IsnbCkDKpov095uSs9wsbqA2WDEIKMI97EScrbjSNTJ35gdzOx
534-5.7.14 D_2ZhLA> Please log in via your web browser and then try again.
534-5.7.14 Learn more at https://support.google.com/mail/bin/answer.py?answer=787
534 5.7.14 54 sm4sm23756569obc.3 - gsmtp
2014-04-16 13:57:53 1WaQLX-0002dT-QM ** email@example.com R=smarthost T=remote_smtp_smarthost: SMTP error from remote mail server after MAIL FROM:<> SIZE=2655: host gmail-smtp-msa.l.google.com [22.214.171.124]: 530-5.5.1 Authentication Required. Learn more at\n530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 sm4sm23756569obc.3 - gsmtp
2014-04-16 13:57:53 1WaQLX-0002dT-QM Frozen (delivery error message)
2014-04-16 14:00:13 1WaQNg-0002eG-NX <= email@example.com U=jimmy P=local S=2381
2014-04-16 14:00:13 1WaQNg-0002eG-NX == jimmy@localhost R=local_user T=mail_spool defer (13): Permission denied: creating lock file hitching post /var/mail/jimmy.lock.spanners.bigspanners.com.534e8ced.000027cc (euid=1000 egid=8)

Sorry for the longness of this post but i'm trying to provide any info i think might be relevant.  Can anyone see what's wrong?
EDIT:  i just thought of something else that may be relevant: i have iptables set up to allow all outgoing requests but to block all incoming requests expect for those on a whitelist.  I don't know how gmail auth works:  could it be that part of the auth process is being blocked because it's coming in on a non-whitelisted port, and that's why it's failing?  here's my iptables rules:
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
REJECT     all  --  anywhere             127.0.0.0/8         reject-with icmp-port-unreachable
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:www
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:https
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:24367
ACCEPT     icmp --  anywhere             anywhere            icmp echo-request
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:2812
LOG        all  --  anywhere             anywhere            limit: avg 5/min burst 5 LOG level debug prefix `iptables denied: '
REJECT     all  --  anywhere             anywhere            reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  anywhere             anywhere            reject-with icmp-port-unreachable

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:2812

EDIT 2:  since posting this i've had notifications from gmail saying "Someone has tried to access your email account, was it you?" (or words to that effect), listing the ip address of my server.  I clicked the "Yes, that was me" box, and tried again, but it's still not working.
Comment: @bennettp123 - are you sure?  Every guide i looked at has two colons, eg https://wiki.debian.org/GmailAndExim4 http://tompurl.com/2011/09/17/exim-gmail-on-ubuntu/ http://islandlinux.org/howto/configure-exim-use-gmail-smtp-server
Comment: Well I can see one error immediately: your `/etc/exim4/update-exim4.conf.conf` has one too many colons: `dc_smarthost='smtp.gmail.com::587'`. However, I'm not sure if that's what's causing your errors.
Comment: not as sure as I was 7 hours ago. I assumed that the correct syntax was `server:port`, but your examples all show `server::port`. And my own server runs on port 25, so it's just `server`. :)
Comment: It seems more to be like a problem with the credentials. Have you double-checked them? Do you have any special chars in it?
Comment: @german_guy the credentials are definitely correct:  i just double-checked by pasting them from the passwd.client file into the login box in gmail in my web browser.  There are no special chars.
Comment: Is this mailaccount configured with 2-factor authentication? And, did you try logging in on this Gmail-account using a browser -from this particular machine- before trying?
Comment: @Martijn - you mean the 2-step verification where you have to enter a code from your phone?  If so, then no.  I can't log in using a browser from the machine in question as it's a cloud-hosted VPS - i only have shell access.
Answer: Had the same issue for a few hours now, but it's not the configuration it's google which blocks the account access, because you try to login from an other IP then normally.
Open http://www.google.com/accounts/DisplayUnlockCaptcha and then try to send an e-mail using for example: echo 'My Test Text' | mail -s Testmail firstname.lastname@example.com
/etc/exim4/passwd.client :
# password file used when the local exim is authenticating to a remote
# host as a client.
#
# see exim4_passwd_client(5) for more documentation
#
# Example:
### target.mail.server.example:login:password

gmail-smtp.l.google.com:firstname.lastname@example.com:yourpassword
*.google.com:firstname.lastname@example.com:yourpassword
smtp.gmail.com:firstname.lastname@example.com:yourpassword

/etc/exim4/update-exim4.conf.conf :
# /etc/exim4/update-exim4.conf.conf
# (...)
# This is a Debian specific file

dc_eximconfig_configtype='satellite'
dc_other_hostnames='ServerName'
dc_local_interfaces='127.0.0.1 ; ::1'
dc_readhost='yourdomain_which_points_to_server.com'
dc_relay_domains=''
dc_minimaldns='false'
dc_relay_nets=''
dc_smarthost='smtp.gmail.com::587'
CFILEMODE='644'
dc_use_split_config='false'
dc_hide_mailname='true'
dc_mailname_in_oh='true'
dc_localdelivery='mail_spool'

Comment: That's worked!  Halleflippinglujah, thanks @FireEmerald!
Comment: It looks like you have to turn on "Less Secure Apps" on in the Google account: https://support.google.com/accounts/answer/6010255?hl=en
Answer: Gmail authentication will fail if tls negotiation does not complete properly . Check you're indeed using starttls when talking to gmail.
You can run exim with -d-all+tls+transport.
That should give more infos
Comment: Thanks @alxgomz.  Can you tell me how i should start exim in this way?  I tried doing `sudo /etc/init.d/exim4 stop; sudo /etc/init.d/exim4 start -d-all+tls+transport` but i'm not sure if that was the right way to do it: it didn't make any difference.
Comment: Actually it depends on your distro, but if you use a Debian like version of Linux, you can set the debug flags in /etc/default/exim4. Be aware that this will make the process run in foreground so you don't want to s stay like this for too long. Particularly not when you reboot!!
Comment: really sorry but do you mind explaining what `-d-all+tls+transport` actually does?  Is it to do with debugging as your last comment implies?  Also, what would i look out for in the debug output to see if starttls is being used properly?
Comment: this switch will indeed activate debug for tls negotiation and exim transports (including the smtp one that is supposed to deliver mail to gmail). It is quite difficult to forecast what will be the error and hance the need for debugging :). But for example seraching for the gmail address you're trying to reach or the remote server name can be a starting point.

Cannot get exim4 & gmail working


Pegar data hora dispositivo phonegap

Question: Como eu faço para pegar a data hora do dispositivo usando o phonegap?
Answer: Me parece que uma possível solução seria a seguinte:
<code>var date = new Date();
var hour = date.getHours();
var min = date.getMinutes();
var seconds = date.getSeconds();
</code>
Comment: Com certeza ajudou! muito obrigado.

Pegar data hora dispositivo phonegap


Optimize mysql query with subquery in values list

Question: I am trying to optimize queries to my database. I have the following query:
select date, (
    select count(user_id)
    from myTable
    where logdate = date
) as value
from myTable;

As far as I can see, the second value is computed efficiently. However, is there any common practice to optimize this kind of query in MySQL?
Answer: I believe you can avoid writing a subquery and preform the same query using aggregation, which may run faster:
SELECT date, COUNT(user_id) AS numRecords
FROM myTable
GROUP BY date;

Here is a reference on aggregate functions.
Answer: you do not have to put group functions in a separate select.  Just do
select date, count(user_id) from myTable group by date;

There is no hard and fast.  In this query, it was a matter of one select being more efficient than 2.  But here is some tips for beginners on optimizing queries.


detecting a click inside a hexagon drawn using canvas?

Question: I'm working on a certain layout where I need to draw a hexagon which needs to be clickable. I'm using the Path2D construct and isPointInPath function. I'm constructing an animation where a number of hexagons is created and then each moved to a certain position. After the movement is done, I am attaching onclick event handlers to certain hexagons. However there is weird behaviour.
Some initialized variables
const COLOR_DARK = "#73b6c6";
const COLOR_LIGHT = "#c3dadd";
const COLOR_PRIMARY = "#39a4c9";

const TYPE_PRIMARY = 'primary';

let hexagons = [];

Below is the function which draws the hexagons.
function drawHex(ctx, x, y, hexProps, stroke, color) {

    let myPath = new Path2D();

    myPath.moveTo(x + hexProps.width*0.5, y);
    myPath.lineTo(x, y + hexProps.height*hexProps.facShort);
    myPath.lineTo(x, y + hexProps.height*hexProps.facLong);
    myPath.lineTo(x + hexProps.width*0.5, y + hexProps.height);
    myPath.lineTo(x + hexProps.width, y + hexProps.height*hexProps.facLong);
    myPath.lineTo(x + hexProps.width, y + hexProps.height*hexProps.facShort);
    myPath.lineTo(x + hexProps.width*0.5, y);
    myPath.closePath();

    if (stroke){
        ctx.strokeStyle = color;
        ctx.stroke(myPath);
    } else {
        ctx.fillStyle = color;
        ctx.fill(myPath);
    }

    return myPath;
}

This function populates the hexagon array
function populateLeftHex(canvasWidth, canvasHeight, hexProps) {

const startX = canvasWidth / 2;
const startY = canvasHeight / 2;
const baseLeft = canvasWidth * 0.05;

for(let i = 0; i < 5; i++){
    let hexNumber = (i % 4 == 0)? 2: 1;

    for(let j = 0; j < hexNumber; j++){
        hexagons.push({
            startX: startX,
            startY: startY,
            endX: baseLeft + (2 * j) + ((i % 2 == 0)? (hexProps.width * j) : (hexProps.width/2)),
            endY: ((i + 1) * hexProps.height) - ((i) * hexProps.height * hexProps.facShort)  + (i* 2),
            stroke: true,
            color: ( i % 2 == 0 && j % 2 == 0)? COLOR_DARK : COLOR_LIGHT,
            type: TYPE_PRIMARY
        });
    }
}

}
And here is where Im calling the isPointInPath function.
   window.onload = function (){
const c = document.getElementById('canvas');

const canvasWidth = c.width = window.innerWidth,
    canvasHeight = c.height = window.innerHeight,
    ctx = c.getContext('2d');

window.requestAnimFrame = (function (callback) {
    return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
            window.setTimeout(callback, 1000 / 60);
        };
})();

console.log(canvasWidth);

let hexProps = {
  width: canvasWidth * 0.075,
    get height () {
      return this.width/Math.sqrt(3) + (1.5)*(this.width/Math.sqrt(2)/2);
    } ,
    facShort: 0.225,
    get facLong () {
      return 1 - this.facShort;
    }
};

populateLeftHex(canvasWidth, canvasHeight, hexProps);

let pct = 0;
const fps = 200;

animate();

function animate () {

    setTimeout(function () {
    // increment pct towards 100%
    pct += .03;

    // if we're not done, request another animation frame
    if (pct < 1.00) {
        requestAnimFrame(animate);
    } else { //if pct is no longer less than 1.00, then the movement animation is over.
        hexagons.forEach(function (hex) {
            if(hex.type === TYPE_PRIMARY) {

                console.info(hex.path);

                c.onclick = function(e) {

                    let x = e.clientX - c.offsetLeft,
                        y = e.clientY - c.offsetTop;
                    console.info(ctx.isPointInPath(hex.path, (e.clientX - c.offsetLeft), (e.clientY - c.offsetTop) ));
                };
            }
        })
    }

    ctx.clearRect(0, 0, c.width, c.height);

        // draw all hexagons
        for ( let i = 0; i < hexagons.length; i++) {

            // get reference to next shape
            let hex = hexagons[i];

            // note: dx/dy are fixed values
            // they could be put in the shape object for efficiency
            let dx = hex.endX - hex.startX;
            let dy = hex.endY - hex.startY;
            let nextX = hex.startX + dx * pct;
            let nextY = hex.startY + dy * pct;
            hex = hexagons[i];
            ctx.fillStyle = hex.color;
            hex.path = drawHex(ctx, nextX, nextY, hexProps, hex.stroke, hex.color);

        }

    }, 1000 / fps);
}

Can you help me figure out what I'm doing wrong? Maybe I misunderstood how Path2D works? Thanks in advance.
Comment: You are drawing multiple paths (hexagons) correct?  I don't think this is the best approach.  `isPointInPath()` will only tell you if a point is in the current path.  You'll have to check the point while you are drawing.  The way your code works now, it will only check if the click was in the last hexagon drawn.  At least, that is how I am understanding the documentation.
Comment: It's true, the drawHex function draws one hexagon. But im calling it many times. There is a weird behavior happening now... I'm constructing a complex animation .. I'll update the question
Answer: Had to do a bit of work to build a test page as your example is incomplete, but this is working for me - though my hexagon is concave...

var myCanvas = document.getElementById("myCanvas");
var ctx = myCanvas.getContext("2d");

var hexProps = {width:100, height:100, facShort:-2, facLong:10};

var hexagons = [];

 function drawHex(ctx, x, y, hexProps, stroke, color) {

     let myPath = new Path2D();

     myPath.moveTo(x + hexProps.width*0.5, y);
     myPath.lineTo(x, y + hexProps.height*hexProps.facShort);
     myPath.lineTo(x, y + hexProps.height*hexProps.facLong);
     myPath.lineTo(x + hexProps.width*0.5, y + hexProps.height);
     myPath.lineTo(x + hexProps.width, y + hexProps.height*hexProps.facLong);
     myPath.lineTo(x + hexProps.width, y + hexProps.height*hexProps.facShort);
     myPath.lineTo(x + hexProps.width*0.5, y);
     myPath.closePath();

     if (stroke){
         ctx.strokeStyle = color;
         ctx.stroke(myPath);
     } else {
         ctx.fillStyle = color;
         ctx.fill(myPath);
     }

     return myPath;
 }

hexagons.push({type:0, path:drawHex(ctx,100,100,hexProps,false,"#0f0")});

   hexagons.forEach(function (hex) {
            if(hex.type === 0) {

                console.info(hex.path);

                myCanvas.onclick = function(e) {

                    let x = e.clientX - myCanvas.offsetLeft,
                        y = e.clientY - myCanvas.offsetTop;
                    console.info(x,y);
                    console.info(ctx.isPointInPath(hex.path, (e.clientX -
 myCanvas.offsetLeft), (e.clientY - myCanvas.offsetTop) ));

                };
            }
        })
<canvas width=500 height=500 id=myCanvas style='border:1px solid red'></canvas>

Test clicks give true and false where expected:
test.htm:48 165 168
test.htm:49 true
test.htm:48 151 336
test.htm:49 false
test.htm:48 124 314
test.htm:49 true
test.htm:48 87 311
test.htm:49 false


Exibir uma imagem que esta fora do projeto JSF

Question: Olá tenho uma tela que faz um upload de um arquivo e salva em uma pasta em um diretorio do computador até e salva no banco o caminho completo da imagem, agora quero exibir essa imagem em uma tabela por exemplo fiz o exemplo do primefaces porem nao conisgo exibir a imagem.

          <h:outputLink value="#{item.imagem}" title="Nature 1">
           <h:graphicImage name="#{salaBean.exibirImagem(item)}" id="nature1" style="height: 77px; width: 100px" />
           </h:outputLink>

no meu bean eu trago o caminho completo exemplo:
D:\dados\sar\uploads\Chrysanthemum.jpg
Comment: Porquê usar o `p:lightBox`? Tenta sem ele, e vê no que dá.
Answer: Esse caminho D:\dados\sar\uploads\ precisa ser servido por um Servlet, ou seja, se eu acessar http://localhost:8080/app/uploads, deverá ter esse diretório mapeado.
Após request do browser, ler o caminho do disco e retornar no response.getOutputStream(), sem esquecer de settar o Content-Type para image\jpeg.
Para ler imagem do disco use ImageIO ou Apache Commons-IO.
Comment: Olá Giuliano eu fiz assim
Comment: File file = new File("D:\\dados\\sar\\uploads\\");
        InputStream inputStream = new FileInputStream(file);
        StreamedContent content = new DefaultStreamedContent(inputStream, "image/jpeg", "Chrysanthemum.jpg");
Comment: mas ta dando acesso negado
Comment: SEVERE: Error Rendering View[/restrito/sistema/sala.xhtml]
javax.faces.FacesException: javax.el.ELException: /restrito/sistema/sala.xhtml @97,157 name="#{salaBean.exibirImagem(item)}": java.io.FileNotFoundException: D:\dados\sar\uploads (Acesso negado)
Comment: Pelo que entendi, `uploads` é um diretório, certo? Então deve concatenar algum parâmetro passado pela URL que forme o nome de um arquivo válido, por exemplo o que você citou **Chrysanthemum.jpg**, então você leria de `D:\dados\sar\uploads\Chrysanthemum.jpg`. Se o erro persistir, verifique se tens acesso a esse diretório.
Answer: Você pode criar um StreamedContent para trafegar sua foto.

public StreamedContent getFoto(){
    File foto=new File("suafoto.jpg");
    DefaultStreamedContent content=null;
    try{
        BufferedInputStream in=new BufferedInputStream(new FileInputStream(foto));
        byte[] bytes=new byte[in.available()];
        in.read(bytes);
        in.close();
        content=new DefaultStreamedContent(new ByteArrayInputStream(bytes),"image/jpeg");
    }catch(Exception e){
        log.error(e);
    }
    return content;
}

Comment: quem é fotoPerfil
Comment: Falha na hora de simplificar o código para postagem. Corrigido!
Comment: @NilsonUehara, pela minha experiência usando o `StreamedContent` dentro da tabela não funciona.
Comment: @Cold funciona sim, tenho isso em datatable e picklist. Acho que sem a propriedade cache="disable" talvez dê errado mesmo.


High kurtosis and bad skewness

Question: Is it necessary to have data that looks normal if you want to apply a dynamic correlation coeffient (dcc)?
Should the explanatory variables in the dcc method also be normalized?
My dataset has a high kurtosis and bad skewness. I used the log10 to try to make it more normal, but remarkably the kurtosis is much higher after taking the log... Very strange.
What other options do I have to make my data more normal and how do I apply that in Stata?
Comment: Welcome to the site. You don't need to sign your name as the site does it for you automatically.
Answer: I would imagine the DCC suffers the same limitations as the regular correlation with non-normal data. That is, there isn't an assumption of normality, but non-normal data can cause odd findings; see the Anscombe quartet, for example.
As for kurtosis, taking the log can certainly make it worse. Take this example of the uniform distribution:
set.seed(2810101)
x <- runif(100)
logx <- log(x)
library(moments)
kurtosis(x)
kurtosis(logx)

where a Normally distributed variable has kurtosis of 3.
on the other hand, in this example
set.seed(2829101)
z <- c(rnorm(1000, 10, 1), rnorm(1000, 10, .01))
kurtosis(z)
kurtosis(log(z))

However, you mention skewed data with kurtosis. Was your data right skew or left skew? Since the former is more common, I'll guess that.
set.seed(1919110)
x <- c(rnorm(1000, 10, 1), rnorm(300, 30, 2), runif(10, 500, 600))
skewness(x)
kurtosis(x)
skewness(log(x))
kurtosis(log(x))

Here, taking the log improves kurtosis and skewness.
Taking the log had almost no effect on kurtosis.
As always, try plotting the data to see what is going on in your correlation.

High kurtosis and bad skewness


Google Maps not working HTML and Javascript

Question: I registered the key for google maps in my website. Everything was working until recently it stopped showing with an error, but for some reason its works fine on mobile view.
Here's an image of what's happening
Here's the code
<code>    <script>
        function initialize() {
            var LatLng = {lat: 25.09112766, lng: 55.1534003};
          var mapProp = {

            center:new google.maps.LatLng(25.09112766,55.1534003),
            zoom:16,
                scrollwheel: false,
                navigationControl: false,
                mapTypeControl: false,
                scaleControl: false,
                draggable: false,
            mapTypeId:google.maps.MapTypeId.ROADMAP
          };
          var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
            var marker = new google.maps.Marker({
                position: LatLng,
                map: map,
              });
        }
        google.maps.event.addDomListener(window, 'load', initialize);
    </script>
</code>
Answer: These errors are usually generated from an ad blocking plugin, such as Adblock Plus. To test this use either a different browser or uninstall the ad blocking plugin (right clicking the extension by the URL bar and clicking "Remove from Chrome...").
There is an easier way to temporarily disable an extension. In Chrome, opening an Incognito tab will usually stop extensions running (unless you have specifically told Chrome which ones to run in Incognito).
Comment: Thank you! worked like a charm in Incognito!
Cheers!
Answer: It's common for these error to appear if you have Ad Blocker turned on. Try turning it off and see if it persists.
Comment: Worked! Thank you!


SignalR and NServiceBus in a WebFarm

Question: I am trying to understand if/how this works.
Can SignalR and NServiceBus be used together to support full duplex on a web farm where the request could be sent from a server then later the response would come back but the user may be on another server.
The use case for this would be a user submits a form which then takes a while to process say 3-5 minutes.  During that time the user is doing other things and could be switching between servers due to load balancing.  Once the process is complete it would send a response back which would show a notification to the user.
If there are some articles on this that would be great.
Answer: Allow me to refer you to Roy Cornelissen's great blog post on this very topic:
http://roycornelissen.wordpress.com/2013/03/11/an-nservicebus-backplane-for-signalr/
Comment: Thanks a lot Udi, for your kind words and the link to my post. I have just now updated it to SignalR 1.1.2, which contains the final release of the ScaleoutMessageBus architecture. It has changed quite a bit since the preview.
Answer: Yes,
What you need to do is configure a scaleout-backplane for signalr so that messages can propagate between the nodes back to the user.
Here is a document that describes how to do it: http://www.asp.net/signalr/overview/performance-and-scaling/scaleout-with-windows-azure-service-bus
PS: there are other scaleout options besides the one used in this document, it's up to you.

SignalR and NServiceBus in a WebFarm


SQL Server : filter on dates from another table

Question: I'm pretty new to SQL Server so this may be a pretty easy question
I have a log table view that looks like the following:

And a table that logs the start and stop of a run that looks like the following:

I would like to filter data from the first table between the start and stop times in the second table. Is there any way to do that?
Comment: Table names are always useful when discussing SQL queries.
Comment: Have you tried to solve this problem yourself?  If so, please include the SQL in the question.
Answer: You can use a regular join to match a date-range:
select  *
from    Entries e
join    Runs r
on      r.StartTime <= e.EntryTime
        and (
          e.EntryTime <= r.StopTime
          or r.StopTime is null -- For a run that has not yet stopped
        )

But your example data suggests there is a foreign key, which would eliminate the need for date-range matching:
select  *
from    Entries e
join    Runs r
on      e.ProjectRunId = r.ProjectRunId


Does the infinite series $\sum_{n=1}^\infty \frac{\ln n}{n}$ converge?

Question: Does the series $\sum_{n=1}^\infty \frac{\ln n}{n}$ converge?
Is there a way to determine this besides a comparison test?
Comment: You can use the integral test if you don't like the comparison test
Answer: Hint. The given series is divergent

by comparison with the harmonic series: $$ \frac1n<\frac{\log n}n ,
   \qquad n \geq 3, $$
by the integral test: $$ \frac{\log^2 N}2=\int_1^N \frac{\log
   x}x\,dx\le\sum_{n=1}^N\frac{\log n}n $$ letting $N \to \infty$.
Comment: Why is that a hint?
Comment: Thank you. I was looking for a way besides the comparison test so this is perfect.
Comment: This is more than a hint. This is nearly a complete answer. Nearly two complete answers for that matter.


New workflow can't select content type

Question: I'm trying to create a new workflow in Drupal. When I click "Content Types" beneath the "This workflow applies to:" section, it loads an empty modal. I have 6 content types but they fail to show.
There is an existing workflow that has the correct content types set. However, if I uncheck one of them, it won't be available if I try to add it back later.
I made a new, test content type. It will show as an option for workflows but the older, existing options will not.
Does anyone have any ideas as to why the content types won't load inside of the workflow editor?
Comment: Not sure what's gone on there. You could try doing into edit one of the existing content types and clicking save to see if that sorts it. It could be you've updated core and a setting isn't populated somewhere that should be
Comment: Content types don't really have a "save" button. I can save a change to a field, though. I tried that and it didn't work. I'll try other, similar ideas and will report back.
Comment: I was referring to the edit screen for the content type .e.g /admin/structure/types/manage/page for the basic page content type
Comment: Sorry, my mistake... I tried it but it didn't work. They still don't appear.
Comment: I've tried everything I can think of in the Content Types and they still won't appear.
Answer: It seems each content type can only be assigned to one workflow at a time.
Try removing removing a content type from the other working workflow, save it, and then adding it to the new workflow.

New workflow can't select content type


Why is a union mysql query over three tables faster than join on impossible where clauses?

Question: My schema is set up such that I have three tables player coach and manager where all three have a foreign key to a table employee which contains not more than a single auto-incrementing id and a string type representing the type of employee they are ie. 'PLAYER' or 'COACH' or 'MANAGER'.
However, different employees have different ids based on the team they work for. So I have a lookup table team_x_lookup for each team (where x is the team number or name) which maps a team id to a global id. Also, I have a generated column within each player coach and manager table for each team which contains the team id, this column is indexed. This column will obviously be null if the employee is not on that team.
To fetch an employee from their team id I currently have two select statements
SELECT * FROM employee e
    LEFT JOIN player p ON (e.`type` = 'PLAYER' AND p.employee_id = e.id)
    LEFT JOIN coach c ON (e.`type` = 'COACH' AND c.employee_id = e.id)
    LEFT JOIN manager m ON (e.`type` = 'MANAGER' AND m.employee_id = e.id)
WHERE e.id = (
    SELECT employee_id FROM team_x_lookup t WHERE t.team_id = 6
);

SELECT * FROM (
    SELECT * FROM player WHERE team_id = 6
    UNION
    SELECT * FROM coach WHERE team_id = 6
    UNION
    SELECT * FROM manager WHERE team_id = 6
) as emp;

I have indexes on everything I do a SELECT on. Running explain gives me these respectively:
1   PRIMARY e       const   PRIMARY,id_UNIQUE           PRIMARY 4   const   1   100.00
1   PRIMARY p       const   PRIMARY,employee_id _UNIQUE PRIMARY 4   const   1   100.00
1   PRIMARY c       const   PRIMARY,employee_id _UNIQUE PRIMARY 4   const   0   0.00    unique row not found
1   PRIMARY m       const   PRIMARY,employee_id _UNIQUE PRIMARY 4   const   0   0.00    unique row not found
2   SUBQUERY t      const   PRIMARY,team_id_UNIQUE      PRIMARY 4   const   1   100.00

1   PRIMARY         <derived2>      ALL                                 6               100.00
2   DERIVED         player          ref team_x_id_INDEX team_x_id_INDEX 5   const   1   100.00  Using index
3   UNION           coach           ref team_x_id_INDEX team_x_id_INDEX 5   const   1   100.00  Using index
4   UNION           manager         ref team_x_id_INDEX team_x_id_INDEX 5   const   1   100.00  Using index
N   UNION RESULT    <union2,3,4>    ALL                                                         Using temporary

I don't like the second way of doing this because it requires me to have potentially 20 more columns for each table. But, I don't understand how the union query is up to 50% faster than the lookup table query! Doesn't the union have to select on all three tables, whereas the cross reference query will notice that one of the three joins is possible and discard the other two?
I would like to know how to make the cross reference table faster, additionally because union requires me to select the same number of columns from each table in the union while each of my tables have a different number of columns that all should be returned.
EDIT
I have about 30,000 elements in the database and I am doing my timing by querying the first half of my data via the union and the second half via the join. My time comes out to roughly 3.9s for the union and 7.0s for the join.
Comment: Please give DDL. Read about and act on [mcve]. Search [so] re bad idea  "smart keys", encoding info in key values. team_x_lookup is also a bad idea, you are encoding info in table names. Just start with a straightforward design where all info is recorded as values on rows of tables. Each table has an associated *predicate* (sentence template) and holds rows that say something about the situation when column values are substituted for column names. PS Your use of NULL seems like it might be making your table way larger than necessary, with every row requiring an unnecessary calculation.
Answer: You can probably make the first query more efficient by removing the subquery, like this:
SELECT e.* FROM employee e
INNER JOIN team_x_lookup t ON e.id = t.employee_id
LEFT JOIN player p ON (e.`type` = 'PLAYER' AND p.employee_id = e.id)
LEFT JOIN coach c ON (e.`type` = 'COACH' AND c.employee_id = e.id)
LEFT JOIN manager m ON (e.`type` = 'MANAGER' AND m.employee_id = e.id)
WHERE t.team_id = 6

Comment: Thanks, this makes the query simpler but doesn't change the execution time
Comment: ok fair enough. often subqueries can be an issue but clearly not in this case.


Finite fields are isomorphic

Question: This is from A Course in Arithmetic by JP Serre

Theorem 1
ii) Let $p$ be a prime number and let $q = p^f(f \geq 1)$ be a power of $p$. Let
     be an algebraically closed field $\Omega$ of characteristic $p$. There exists a  unique subfield $F_q$ of $\Omega$ which has $q$ elements. It is the set of roots of the polynomial$X^q-X$.
iii) All finite fields with $p^f$ elements are isomorphic to $F_q$.

The proof of the last part says

Assertion iii) follows from ii) and from the fact that all fields with $p^f$
    elements can be embedded in $\Omega$, since $\Omega$ is algebraically closed.

Can someone explain me how iii) follows fom ii)?
Comment: If we had two fields $L,K$ which have cardinality $p^f$, then both are algebraic over $\mathbb{F}_p$, and so they both embed into $\overline{\mathbb{F}_p}$, but by (ii) they have the same image, and in particular are both isomorphic to that image and thus to each other.
Answer: Let $E$ be a field with $q$ elements. By (iii), it is isomorphic to a subfield of $\Omega$. By (ii), there's only one such subfield, namely $F_q$.
Comment: You are using what you want to prove.
Answer: You can also use a little Galois Theory: by (ii) any field $\,\Bbb F_q\,$ is the splitting field of $\,x^q-x\,$ over $\,\Bbb F_p\,$ , and "the" comes from the uniqueness, up to isomorphism, of the splitting field...

Finite fields are isomorphic


Three fold covering of $\Bbb RP^2\lor \Bbb S^1$.

Question: I am trying to find $3$-fold covering of $\Bbb RP^2\lor\Bbb S^1$. So what I guess is the following:---
Choose three distinct points $a,b,c$ on a cricle, then the three open arcs $ab,bc,ca$ is our three one cells and the points $a,b,c$ are three $0$-cells. Now wedge three copies of $\Bbb RP^2$ at the points $a,b,c$. And this will give us the required $3$-fold covering. Am I right?
My next question is, can I find a $3$-fold covering by performing different operations like wedge, just on the spaces $\Bbb S^2$ and $\Bbb S^1$. Any help will be appreciated.
Comment: Your second question is unclear.
Answer: If you have coverings $p_i : X_i \to Y_i$, then you get a covering $p_1 \times p_2 : X_1 \times X_2 \to Y_1 \times Y_2$. The wedge $Y_1 \vee Y_2$ depends on basepoints $\eta_i \in Y_i$ and can be defined as
$$Y_1 \vee Y_2 = (Y_1,\eta_1) \vee (Y_2,\eta_2) = \{(y_1,y_2) \in Y_1 \times Y_2 \mid y_1 = \eta_1 \text{ or } y_2 = \eta_2 \} .$$
By restricton of $p_1 \times p_2$ we then get a covering
$$\pi : (p_1 \times p_2)^{-1}(Y_1 \vee Y_2) \to Y_1 \vee Y_2$$
which has $n_1 \cdot n_2$ sheets if the $p_i$ have $n_i$ sheets. We have
$$(p_1 \times p_2)^{-1}(Y_1 \vee Y_2) = \{(x_1,x_2) \in X_1 \times X_2 \mid x_1 \in p_1^{-1}(\eta_1) \text{ or } x_2 \in p_2^{-1}(\eta_2) \}  \\ = X_1 \times p_2^{-1}(\eta_2) \cup p_1^{-1}(\eta_1) \times X_2 .$$
It is therefore obtained as a "multiple wedge" by taking the disjoint union $\bigsqcup _{x_2 \in p_2^{-1}(\eta_2)} X_1(x_2) \sqcup \bigsqcup _{x_1 \in p_1^{-1}(\eta_1)} X_2(x_1)$, where the  $X_1(x_2)$ and $X_2(x_1)$ are copies of $X_1$ and $X_2$, respectively, and identifying for all pairs $(x_1,x_2) \in p_1^{-1}(\eta_1) \times p_2^{-1}(\eta_2)$ the points $x_1 \in X_1(x_2)$ and $x_2 \in X_2(x_1)$.
Apply this gneral result to $p_1 = id : \Bbb RP^2 \to \Bbb RP^2$ and $p_2 : S^1 \to S^1, p_2(z) = z^3$. We get a covering with $3$ sheets and it is precisely the map you described.
Comment: So what I have done regarding $3$-fold covering is correct???.
Comment: Yes, it is correct.
Comment: I have guess that, in order to find odd-fold covering space of $X=\Bbb RP^2\lor \Bbb S^1$, I have to use $\Bbb RP^2$. I mean as we do for finding two fold covering $X$ we have $\Bbb S^1\lor\Bbb S^2\lor\Bbb S^1$ as two fold cover of $X$, we can't do for when we trying to find three fold cover.
Comment: Take $p_1 = id : \Bbb RP^2 \to \Bbb RP^2$ and $p_2 : S^1 \to S^1, p_2(z) = z^n$. We get a covering with $n$ sheets. For $n = 2m$ you can also take  $p_1 : S^2 \to \Bbb RP^2$ the qoutient map which is covering with two sheets and $p_2(z) = z^m$.


Keeping existing capital letters in string

Question: I am using the following code to change the first letter of each word to a capital except for some trivial ones (a, of etc.)
f = open('/Users/student/Desktop/Harry.txt').readlines()[2]
new_string = f.title()
print (new_string)

What i would also like to do is have those exception words not capitalised as stated above but also have that any word that already has capitals letters (For e.g. CHINA, NSW) that those letters will be retained.
Comment: Even though your question is clear enough, the code you are showing is not helpful. The first line is not related to the question at all. What would be nice is an example string, the desired `new_string`, and the current `new_string`.
Comment: so you want to check if your string is already all uppercase...?
Comment: What about words like `'aBCDe'`?
Answer: Something like this:
Using str.capitalize:
Why?
>>> "CAN'T".title()
"Can'T"

>>> "CAN'T".capitalize()
"Can't"

Code:
>>> strs = """What i would also like to do is have those exception words not capitalised as
stated above but also have that any word that already has capitals letters
( For e.g. CHINA, NSW etc. ) that those letters will be retained."""
>>> words = {'a','of','etc.','e.g.'}  #set of words that shouldn't be changed
>>> lis = []
for word in strs.split():
    if word not in words and not word.isupper():
        lis.append(word.capitalize())
    else:
        lis.append(word)
...
>>> print " ".join(lis)
What I Would Also Like To Do Is Have Those Exception Words Not Capitalised As Stated Above But Also Have That Any Word That Already Has Capitals Letters ( For e.g. CHINA, NSW etc. ) That Those Letters Will Be Retained.

Answer: For the first requirement you can create a list containing the exception words:
e_list = ['a', 'of', 'the'] # for example

Then you can run something like this, using isupper() to check if the string is already all capitalized:
new = lambda x: ' '.join([a.title() if (not a in e_list and not a.isupper()) else a for a in x.split()])

Testing:
f = 'Testing if one of this will work EVERYWHERE, also in CHINA, in the run.'

print new(f)
#Testing If One of This Will Work EVERYWHERE, Also In CHINA, In the Run.

Comment: This will remove words that don't need to be capitalized, instead of keeping them as is.
Comment: @interjay Thank you for the feedback, it was missing an else statement in the list comprehension...


human singing voice shows impulse-like high frequency component in spectrogram

Question:
I have recording of singing voice recorded in studio with high quality devices.
When I open it as spectrogram (using ocenaudio), the periodic impulse like
high frequency components are shown as in Figure. (vertical lines)
There is no instrument and only exist singing voice of a male adult.
Is this normal and negligible?
or there might be a problem in recording process?
What might cause this?
Answer: From this post on the same topic over on Digital Signal Processing Stack Exchange:
Vertical lines noise in spectrogram

Window functions that are not zero-ended produce these vertical lines. For example, Rectangular Window, Hamming Window, Gaussian Window (with low sigma) produce these lines, and Barlett Window, Hanning Window, Blackmann Window, Welch Window, don't.
Answer:
The figure shows the spectrogram of the same voice with the Blackman window for spectrogram.
It turned out that the custom config to make spectrogram(windows size and type in this case) produced the artifact. Preset config does not show the vertical lines. And spectrogram from Adobe Audition also is okay.
Comment: windowing is used all over the place in signal processing and in some contexts is also called "apodizing" (e.g. in Fourier optics and FTIR) See [Is it "common practice in Fourier transform spectroscopy to multiply the measured interferogram by an apodizing function"? If so, why?](https://chemistry.stackexchange.com/q/111982/16035) I use it in [STM](https://en.wikipedia.org/wiki/Scanning_tunneling_microscope). But somehow our ears manage to avoid the need to do it. *viva analog!* :-)
Answer: The problem probably lies in noise-shaping DSP techniques used by sigma-delta ADCs and DACs, which I personally strive to avoid. @uhoh, you'll never get virtually close even to 20-bit dynamic range (using real, read R2R temperature-compensated no-oversampling DACs) with any analog gear (2^20 is much much higher dynamic range than ~140dB which is about the limit of any analog gear).
If you want real, as colloquially called "analog" sound feel, use R2R DACs with no oversampling as well as so-called flash ADCs. Would be expensive as hell, but beats any analog gear in every possible way. Sigma-delta DACs, currently 90% of pro-audio market and close to 100% of consumer audio market are actually 1-bit DACs which run at "insane" frequencies like tens of MHz followed by a reconstruction filter (which is usually also quite poor quality) so they're obviously doing extreme oversampling to "make up" their 1-bit dynamic range with high "sampling rate". If anything 1-bit can be called "sampling", of course.
To get this close as we know them nowadays to real DACs they need some DSP processing, of which the crucial part is aggressive noise-shaping techniques moving most of the quantization noise (which obviously is extremely high here) to 30-40+ kHz range, which is supposed to being cut-off by the (low-pass) reconstruction filter.
But filters as well as switching elements (transistors) aren't ideal, and that's the actual problem. The delta-part of this kind of DACs/ADCs is briefly a feedback for the DSP processor. Everything above is a great simplification, but I hope it's understandable for people without advanced DSP background.
Even further, often those DACs are using just like 6-10 most significant bits of every sample of your PCM recording, they aren't able to reproduce more dynamic range on 1-bit output, even with their DSP processing. That's why noise-shaping gets even more important. Similar things are happening with sigma-delta ADCs, but it's also a huge simplification. Also speaking briefly, sigma-delta ADCs are much more difficult to replace than equivalent DACs.
Summarizing, some part of your (recording) chain is probably doing some crude noise-shaping, that's could be why you see some high-amplitude noise on the borderline of audible frequencies.
Comment: I do have some DSP background and you lost me at "2^16 is much much higher dynamic range than ~140dB"
Comment: @ojs well, I've been thinking about a 20-bit R2R DAC like the one I have in my studio. It has 120dB "native" dynamic range and about 130dB with some dithering applied (which is still much better that anything 1-bit based), and I doubt you can get close to such values with any real analog setup (not speaking of a single device, rather more like a whole chain). Thanks for pointing this out, just corrected the bit depth.
Comment: Can you explain why changing window function for the spectrogram makes the artefact disappear?
Comment: @ojs, I'm not the author of both programs used by the op to create a spectrogram, but shooting blind, maybe some Gaussian functions are applied by the other function before passing it through FFT (that's the first thing on my mind that would cause outcome like that). Or could be a change in FFT parameters, making it less precise, but faster, that could also cause such difference.
Comment: So you're absolutely certain the pulsing is in the signal and hidden by choice of window function, not the other way round? Could you add some detail that explains the periodicity of the noise?
Comment: Explaining periodicity would be quite hard (and lenghty), but it definitely fits the rest of the case. And to be absolutely certain I'd need to take a look at the PCM data (the recording) by myself, using various, more advanced analysis tools, those screenshots are all stubs - when speaking about any level of certainty, first I would need certain input data.
Comment: Yet the answer reads like you would be certain. The explanation can be lengthy, just explain like you would answer in an exam, not like a lie-to-children.


How to assign output from SOLVE function in PROC FCMP (SAS) to global variable/table?

Question: I'm trying to use PROC FCMP with the function SOLVE to solve non-linear equations and insert solutions into the table automatically.
For instance
proc fcmp;
      /* define the function */
   function inversesqrt(x);
      return(1/sqrt(x));
   endsub;

   y = 20;
   x = solve("inversesqrt", {.}, y, .);
   put x;
run;

After running above code x will be shown in results but i cannot utilize it in further code.
I tried to save x as macro variable or as table but nothing works for me.
Can somebody help me, please?
Answer: Create a second fcmp function to return the solve.
options cmplib=work.funcs;

proc fcmp outlib=work.funcs.sandbox;
      /* define the function */
   function InverseSqrt(x);
      return(1/sqrt(x));
   endsub;

   function SolveInverseSqrt(arg);
     return (solve('InverseSqrt', {.}, arg, .));
   endsub;
run;

%let x = %sysfunc(SolveInverseSqrt(20));
%put &=x;


Sendmail wont send to same domain

Question: I'm using sendmail on my webserver, which so far is working fine. However, when I send emails to the same domain I'm sending them from, they never get delivered. I temporarily changed the email information in my application to another domain, at which point it send to my domain mail accounts no problem.
<code>// does not work
$from = 'email@example.com'
$to = 'email@example.com'
// works fine
$from = 'email@example.com'
$to = 'email@example.com'
</code>
Furthermore, the server this is run on is a separate server to the one that has the mail accounts & domain set up. (+ I'm on linux)
sendmail is using the default configuration.
I've tried testing with the following command, however I seem to get a bunch of unverified sender errors. When I do this with my gmail account, it goes through OK.
<code>echo -e "To: email@example.com\nSubject: Test\nTest\n" | sendmail -bm -t -v
</code>
Edit
It turns out that it just requires the email address its coming from to exist. Is there any way to bypass this?
Comment: This is a very common question. See this answer http://serverfault.com/questions/344105/outgoing-mail-from-linux-not-being-delivered/344189#344189
Answer: As you've already discovered yourself, it's because the sending address is invalid.
Most mail servers will check a domain is valid when accepting an email, but usually won't try and verify the actual address. (Some mail servers will pretend an address exists if you ask them, even if it doesn't to stop automated systems building up a list of valid addresses)
However, your own mail server knows that noreply@ is not a valid address and so will reject the email.
The easiest way round this is to set up noreply@ as a valid address and just discard emails sent to it. On UNIX/Linux you can usually just point the address at an entry in aliases that gets delivered to |/dev/null.
Answer: Without any further information, I'm guessing your local smtp daemon is misconfigured and is delivering mail to @mydomain.com locally instead of forwarding them upstream. Add your full mailserver config to your post if you want a more detailed answer.
Comment: What information do you need? I'm new to sendmail.
Comment: Your configuration. And why the hell you're using sendmail instead of something more reasonable, like postfix or exim :)
Comment: All I need this for is sending out mail from my app - surely sendmail is perfectly reasonable for that? My config is default but I'll add the sendmail.mc to my post.
Comment: Added the config to post
Answer: Your smtp service is using your internal dns which dosen't send the @mydomain.com to the web NS which got the MX record on. If you ping your mydomain.com from that webserver it probably show your IIS internal address. I add the same issue and in my case since it was a stand alone server with no other services I put a web static DNS like 220.127.116.11 to test that theory and it worked. (assuming you're on a windows server)
Comment: I'm on Linux, and furthermore the mailserver for all the accounts + the domain itself is on a different server entirely. I'll add this to the main post.

Sendmail wont send to same domain


The miracle of the Lanczos/conjugate gradient algorithm

Question: Lanczos/Arnoldi/Rietz/CG-like algorithm share the same core strategy... In each, a little miracle appears, most of the Gram-Schmidt inner products are zeroes ! In others words, new direction need only get orthonormalized with the one previous updated search direction (or two in lanczos case), no need to check other previous directions (because zeros). Rather a big miracle when A is sparse and n = 10^6.
In case of non-linear inverse or eigen problem, this property is not true, but depending of the "nonlinearity rate", the values could be small and easily neglected or threshold since the iterative nature of the algorithm assumed that the residual must decrease in each step. So this property remains useful.
Any suggestions are welcome for understand why the new direction is "already" orthogonal to the set of all previous+1 (or +2) directions. Math is ok, but there is a deep principle behind this thing. It look like a hidden bi or tridiagonalization process that happen at the same time. Is there an easy way to handle the trick ?
Answer: The Lanczos iteration builds an orthogonal basis $\left(q_i\right)$ for the nested Krylov subspaces, given some matrix $A$ and vector $b$. For $i > j + 1$ you get: $$q_i^T A q_j = 0$$
since $q_j \in \mathcal{K}_j$, so $Aq_j \in \mathcal{K}_{j+1}$ and $q_i$ is orthogonal to $\mathcal{K}_{j+1}$ since $i > j + 1$. So the matrix $$Q_k^T A Q_k = H_k$$ is lower-Hessenberg, where $Q_k = \left(q_1, \ldots, q_k \right)$. When $A$ is symmetric, then so is $H_k$ which is therefore tridiagonal. Summarizing, what makes it work is:

the fact that we're building an orthogonal basis for Krylov subspaces, which are all related by applying $A$
the fact that $A$ is symmetric


Is drawing a random vector from normal distribution is the same as sampling every coordinate from normal distribution?

Question: So say I have a matrix where each row is sampled from the normal distribution with mean 0 and identity covariance matrix. Is it equivalent to simply sampling every matrix entry from standard normal distribution with mean 0 and std 1?
E.g is sampling vectors from multi dimensional normal distribution is equivalent to sampling every vector coordinate according to standard normal.
Why or why not?
Thanks!
Answer: Yes, if you sample the coordinates independently.  You can see this by writing out the joint pdf for the vector coordinates, and noting that it is the product of the individual pdfs for the coordinates.  Basically because $$\exp(-\sum_i x_i^2/2) = \prod_i \exp(-x_i^2/2).$$
Comment: "pdf" means probability distribution function.  Does this answer your question?  If not, what does "normal distribution" mean to you, in your own terms.  (It's sometimes hard to understand how much background knowledge a questioner here has:  assume to much and an answer might be meaningless, assume too little, and an answer might be belittling.)


Best practice to store screen dimension for reuse in Spritekit game

Question: I read out the screen dimensions of the current device in the following way:
<code>[UIScreen mainScreen].bounds.size.height;
[UIScreen mainScreen].bounds.size.width;
</code>
I need this value very often in different classes of the game, so I want to avoid calculating these values again and again. What is the best practice to store and reuse these values in the game?
Comment: Anywhere you can access them is fine.
Comment: But how in particular? As a global variable? How do you do it?
Answer: You cand store them in AppDelegate(.h), then call them wherever you need.
<code>   @interface AppDelegate :UIResponder <UIApplicationDelegate> {
    ...
   }
   @property (nonatomic, assign) int MYSCREENHEIGHT;
   @property (nonatomic, assign) int MYSCREENWIDHT;
</code>
In AppDelegate(.m) you should synthesize them, then:
<code>    MYSCREENHEIGHT = [UIScreen mainScreen].bounds.size.height;
    MYSCREENWIDHT = [UIScreen mainScreen].bounds.size.width;
</code>
In other places just call them:
<code>    AppDelegate *appDelGlobal = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    int myScreenHeight = appDelGlobal.MYSCREENHEIGHT;
    int myScreenwidth = appDelGlobal.MYSCREENWIDHT;
</code>
You can store them in NSUserDefaults also, but this is not the case for you here.


Has anyone publicly shared an implementation of RUSBoost in R?

Question: There's no package available on CRAN, so I was hoping someone in the community had written their own function/package. I see it's been done in MATLAB, so I may just have to start with that and write the code myself...
Algorithm description - "RUSBoost: Improving Classification Performance when Training Data is Skewed"
Example of MATLAB code (which includes code for boosting in addition to the undersampling)
Answer: Well, I did it. If anyone is looking for this, you can find it at http://steveohh.github.io/RUSBoost.


How can I build a specific data structure?

Question: I have a Course content type which has several "modules." (A module is just a name.) There are also several "units," which may or not belong to a module. A unit has several fields, for example Location, Description.
I have created a Course unit paragraph type, and a Units field in the "course" content type, which references this paragraph type. That way I can create units that belong to the course.
However, I'm not sure how to deal with modules. I could create a Course module paragraph type, but I don't think it would enable me to attach a unit to a module, since there's no way to select an existing paragraph.
Basically I need to create several modules inside a node, then several units, and optionally attach these units to one of the modules I've created.
Comment: Are modules unique to a course? Can units belong to multiple modules? Can multiple units belong to the same module? What's different about a unit if it's attached to a module vs not?
Comment: Yes, modules are unique to a course. Units cannot belong to multiple modules, but multiple units can belong to the same module. There is no difference between a unit that belong to a module vs not. In a way units are tagged with a module, so I've ended up setting up modules as a taxonomy vocabulary (with a content reference field that point to a course)
Answer: Given what you have done so far I would recommend:

Create a "Modules" taxonomy.
Make this a hierarchical taxonomy that includes course names (as if courses were top level modules) and then add the modules under those.
Add a taxonomy reference field to your Unit paragraph
This will be flexible in a couple of ways:

It will let modules receive descriptive fields as needed later.
It will allow site search and filter by module in a hierarchy that includes courses.
It will allow for modules within modules if that becomes a later requirement.

Optional improvements

Make the module selector field on the Unit paragraph form use a view that filters by the current course name as parent (with depth)
Comment: Thank you, that's a great idea! I've ended up setting up a "course module" taxonomy (although I kept it simple, with just a name and course reference), and tagged each course unit with a term.


How to apply custom pandoc code-block syntax-highlighting to integer constants (e.g., 3L) in an rmarkdown::render()ed R script?

Question: In applying a custom highlight theme (for HTML output) to R code blocks in a .R script, the pandoc syntax-highlighter fails to identify R integer constants (e.g., 3L) and applies no highlighting.  String constants (e.g., "howdy"); unmarked integer, floating point, or scientific notation values (e.g., 3, 17.22, 5.3e27); and logical constants (e.g., TRUE) are all parsed correctly and can thus be custom-formatted.
Here's a simple demo R script:
<code>#' ---
#' title:  inttest.R
#' author: ""
#' date: ""
#' output:
#'   html_document:
#'     highlight: pygments
#' ---
#'
#' ## a heading
#'
data.dir <- "./Data/"  # comment comment(yes)
hortense <- 13 + 4     ## documentation
clarence <- TRUE
beverley <- 3L
maryanne <- c( g6 = 15.5, g7 = 534.333, g8 = NA )

if ( hortense > 16 ) { "YO" } else { "NOYO" }
ifelse( clarence, 1.23e5, -17 )

#' ## END ##
</code>
Using rmarkdown::render() with this script as input (and no other arguments specified) yields an HTML file in which all of the R code is appropriately highlighted, except for 3L: The individual R code components are marked with appropriate  classes and formatting is applied on a class-by-class basis (from the specified custom highlight theme); but integer constants are not placed within a  class at all.
Replacing pygments in the YAML header with other built-in pandoc highlighting options, or with a custom .theme file, yields the same results.
On the other hand, if no highlight option is specified in the YAML header (output: html_document replaces the output: line and the following two), pandoc appears to use an entirely different syntax highlighter for code blocks ("highlightjs", perhaps, based on the R console's reported call to pandoc?):  All numeric constants (integers, fp, decimal, etc.) are tagged as  class "hljs-number" and highlighted correspondingly.  However, this highlighter appears not to tag a variety of other classes (e.g., function names), so it doesn't solve the general problem.
Note also that while the example here uses html_document as the output format, I'd actually like to use html_vignette (file sizes are <50KB, vs >500KB w/html_document); but html_vignette yields the same original problem, whether or not a highlight option is specified.
The underlying problem (bug, omission?) presumably lies in the code parser(s) for R syntax w/in pandoc, but I have no idea how to mess with that -- or whether that's even possible in pandoc when installed within R Studio (for MacOS), which is what I'm using (latest versions of R, RStudio, MacOS).  Based on this, the relevant R highlighter specification might be this, and there's a version of that file in my macOS RStudio installation at:  /Applications/RStudio.app/Contents/Resources/app/quarto/share/pandoc/syntax-definitions/r.xml.
Solutions would include a workaround for the problem, or instructions on how to replace the existing pandoc code parser specification file(s?) for R with an improved version.
Answer: After a lot of digging through SO and the Pandoc, KDE syntax-highlighting, and rmarkdown documentation, I came up with the following solution:

Edited a copy of the latest r.xml syntax-highlighting definition file packaged with Pandoc (from here; file format documentation), to detect the marked integer type:  I added a RegExpr rule, just before the Float and Int rules in the CommonRules context, to find and label marked integers as "MarkedInt"; and I added an ItemData spec for the MarkedInt attribute to assign the "dsDataType" style.

Pandoc syntax-highlighting definition files are apparently compiled in (at least for Pandoc in RStudio for macOS), so for Pandoc to use the edited version, Pandoc needs to be called with its "--syntax-definition" option (see here & here). B/c I'm calling pandoc indirectly by way of rmarkdown::render() with an R script as input, I added the pandoc option to the R script's YAML header:
<code> #' output:
 #'   html_vignette:
 #'     highlight: <custom.theme>
 #'     pandoc_args: [
 #'       "--syntax-definition", "<custom.xml>"
 #'     ]
</code>
This works with either html_vignette or html_document, with     <custom.xml> replaced by the edited syntax-definition xml file path, and with <custom.theme> replaced by a syntax-highlighting .theme file path (the latter assigns the desired formatting to the DataType default style -- see here and here).
Comment: Excellent summary.  I too had trouble with something similar in Quarto and also found this blog post helpful:  https://hamel.dev/notes/quarto/highlighting.html


Getting Google Contacts in Rails 3 app after successful oauth2 authentication with devise

Question: I've searched around a lot and seen various answers. Some seem like they may be outdated, and others use gems (omnicontacts) that I can't seem to get working with devise.
In my situation, I already get an oauth token from Google successfully. Now I'm trying to send a get request to the url given by Google to get all my contacts. I'm able to do this in Google's oauth2 playground, but not from my rails app.
Here is the relevant code:
require 'net/http'
require 'json'

class User < ActiveRecord::Base

 def get_google_contacts(auth_token)
  uri = URI.parse("https://www.google.com/m8/feeds/contacts/default/full?oauth_token=#{auth_token}&max-results=50000&alt=json")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE # You should use VERIFY_PEER in production
  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)
  contacts = JSON.parse(response.body)

  puts contacts
 end
end

Notebooks Controller: this is where I want a user to be able to access their contacts from Google.
def show
 @contacts = current_user.get_google_contacts(current_user.oauth_token)
end

Here is the error log I get in my local:
JSON::ParserError in NotebooksController#show

746: unexpected token at '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Error
401
(Client Error)!!1</title>

401.
That's an error.

There was an error in your request.
That's all we know.
'

I think I may be using incompatible techniques to parse the xml I get from Google into json, but that is basically what I want to do. The only Rails related documentation I found in Google API documentation was dated 2009, but it mentioned the gdata gem. Should I be using that?
Thank you so much for any help on this.
EDIT
This is the response I get from Google Oauth2 Playground on a 200 ok (https://developers.google.com/oauthplayground/):
Content-type: application/atom+xml; charset=UTF-8
-content-encoding: gzip
<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom'
xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'
xmlns:gContact='http://schemas.google.com/contact/2008'
xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005'>
<id>email@example.com</id><updated>2014-01-13T18:34:22.842Z</updated><category
scheme='http://schemas.google.com/g/2005#kind'    term='http://schemas.google.com/contact/2008#contact'/>
<title type='text'>Kaustubh Bhardwaj's Contacts</title>

EDIT #2
Ok, I've isolated my problem. I'm getting Net::HTTPUnauthorized on my get request. Here is the updated code for the get request I'm making to Google Contacts API:
uri = URI.parse("https://www.google.com/m8/feeds/contacts/default/full?max-results=50000")
# headers = { "access_token" => "#{auth_token}" }

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
request.initialize_http_header({ "access_token" => "#{auth_token}" })

response = http.request(request)
puts response.code
puts response

That is the same url I am using in Google Oauth2 playground.
Answer: I suspect you're setting the token incorrectly. It is more normal to set it in an http header
Authorization: Bearer 0b79bab50daca910b000d4f1a2b675d604257e42

If you set it in the URL, I believe it's access_token=, rather than your oauth_token= but since I never use it this way, I might be wrong.
Comment: Thanks for your help, I'll try to set a header and see how it goes. Also, am I correctly converting the xml from Google to json? Because it doesn't seem to be giving me json data like I expect? Should I just give up on getting json data and figure out how to parse the xml? I get the feeling this is part of my problem as well.
Comment: What XML? The response you got is not XML, it's HTML and is meant to be a human displayable error message. You will only receive JSON for a successful 200 response
Comment: I edited the question to show you the 200 response I get when I make the get request using Google's Oauth2 Playground (https://developers.google.com/oauthplayground/). When I don't get a 200 response then the format is HTML.
Comment: Since you have this working in the Oauth Playground, I suggest you simply compare the URL from OP with the URL sent by your app and see what is different. Feel free to paste the two URLs side-by-side in your question.
Comment: Sorry for the late reply, and thanks for your patience. I edited the question again with my Net:HTTP get request that's giving me an 'unauthorized' error.


What is the stability condition of a vector error correction model (VECM)?

Question: As written in the title, I am looking for the stability condition of a vector error correction model (VECM).
I have found this phrase:

the companion matrix of a VECM with  endogenous variables and  cointegrating equations has  −
  unit eigenvalues. If the process is stable, the moduli of the remaining  eigenvalues are strictly less than one.

Source: http://www.stata.com/manuals13/tsvecintro.pdf

Is this the correct condition? If so, could anyone tell me how we define "the companion matrix of a VECM"? Is it the coefficient matrix of X_(t-1) ?
In the end, how could I test this condition in R? Is there a package to do this?
Answer: I maybe have an answer which I'm not sure about : I think that I should transfer my VECM to a VAR form, and then I could analyse the Eigenvalues of the compagnon matrix of VAR. There should be r unit roots where r is the number of cointegration relationships, and the module of the rest Eigenvalue should be strictly inferior to 1.
The definition of a compagnon matrix could be found at page 7 and 8 of this link : http://economia.unipv.it/pagp/pagine_personali/erossi/rossi_VAR_PhD.pdf
Please confirm if my idea is correct. Also, if my question is not clear enough, please tell me.