WebClient - dealing with XSRF/CSRF tokens

Question: I'm trying to connect to API with CSRF protection using WebClient. How ExchangeFilterFunction could look like to handle XSRF-TOKEN cookie and include X-XSRF-TOKEN header?
Answer: That's the solution I came up with:
public class CsrfClientExchangeFilterFunction implements ExchangeFilterFunction
{
    @Override
    public Mono<ClientResponse> filter( ClientRequest request, ExchangeFunction next)   {
        return next.exchange( request )
            .flatMap( response -> {
                if( response.statusCode().is4xxClientError() )
                {
                    ResponseCookie csrfCookie = response.cookies().getFirst( "XSRF-TOKEN" );
                    if( csrfCookie != null )
                    {
                        ClientRequest retryRequest = ClientRequest.from( request )
                                .headers( httpHeaders -> httpHeaders.set( "X-XSRF-TOKEN", csrfCookie.getValue() ) )
                                .cookies( cookies -> cookies.add(  "XSRF-TOKEN", csrfCookie.getValue() ) )
                                .build();

                        return next.exchange( retryRequest );
                    }
                }
                return Mono.just( response );
            } );
    }
}

For reference, it doesn't work if ClientRequest.Builder#cookie(String name, String... values) is used instead of ClientRequest.Builder#cookies(Consumer<MultiValueMap<String, String>> cookiesConsumer). I would love to know why though!
Edit:
Different behaviour of ClientRequest.Builder#cookies and ClientRequest.Builder#cookie methods has been reported as an issue for Spring Framework project - you can find it here
Comment: Your reported issue is closed as Invalid. You will need to create a sample Spring project for them to debug.


Php problemas ao escrever variavel em arquivo de texto

Question: Eu estou desenvolvendo uma pagina de registro para um micro jogo de que vamos testar na sala de aula e como não sei muito de php estou com problemas, eu criei um arquivo que pega os valores dos campos de texto e depois salva em arquivos txt em uma pasta com mesmo nome de usuário e id, porém tenho três problemas.
1 - Ele gera os arquivos mesmo os campos input estando vazios.
2 - Ele não escreve dentro dos arquivos o conteúdo dos campos.
3 - Não está criando o arquivo na pasta ids \ onde ficaria o id de todos os usuários separadamente e seriam comparados dentro da pasta afim de ver se encontra um id existente.
Oque é essa pagina ?
è um registrador de usuário php onde registra o usuário e pode alterar os dados se for repetido o nome de usuário e id permitindo assim atualizar os dados de um usuário já existente.
por que não um banco de dados ?
Por que não é um jogo grande e a ideia é não precisar de chamadar sql ou algo do tipo mostrando um jeito alternativo, de fazer a função.
Observações: Os arquivos progresso1.txt até progresso7.txt gravam dados, mas o progresso8.txt e os demais arquivos de texto não recebem os dados dos campos.
<!DOCTYPE html>

  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="wnameth=device-wnameth, initial-scale=1.0">
  <title>REGISRY</title>

  <link rel="stylesheet" type="text/css" href="./css/popcss.css">

  <link rel="shortcut icon" href="./img/svg/logo.svg" type="image/x-icon">

  <link rel="stylesheet" href="./css/style.min.css">

<link rel="stylesheet" href="./css/blockstyle.css">

Skip to content

      <button class="snameebar-toggle transparent-btn" title="Menu" type="button">
        Toggle menu

      </button>

   Salvando dados da conta

        <button class="gray-circle-btn dropdown-btn" title="To messages" type="button" style="display:none;">
          To messages

        </button>

            <?php

 //Registra o Usuario
 if (isset($_POST['submit'])) {

     /*Php 5.6.2  Code By : Michael S. author*/

            //globais
            $username = $_POST['usernamesave'];
            $userxp = $_POST['userxp'];
            $userid = $_POST['userid'];
            $userlevel = $_POST['userlevel'];
            $usermail = $_POST['usermail'];
            $progresso1 = $_POST['userprog1'];
            $progresso2 = $_POST['userprog2'];
            $progresso3 = $_POST['userprog3'];
            $progresso4 = $_POST['userprog4'];
            $progresso5 = $_POST['userprog5'];
            $progresso6 = $_POST['userprog6'];
            $progresso7 = $_POST['userprog7'];
            $progresso8 = $_POST['userprog8'];
            $proceed = $_POST['proceed'];

            //variaveis concatenadas nome com email e id do campo digitado
            $namenamef = $username  . '_' . $userid;
            $namemail = $username . $usermail;

            //Cria pasta do usuario
            if (!file_exists("usersaccounts/$namenamef/")) {
            mkdir('usersaccounts/'.$namenamef, 0777, true);
            }

            //generate a rand value
            $randger= rand() % 9000 + 1000;
            $randfix = $randger;
            //verify into folder if existe into any file the name , if yes then generete new random
        //  $tokens = ['_twitter_sess', 'ct0', 'auth_token'];
            $tokens = $randger;

            //verifica se o id exite em algum  arquivo da pasta ele gera o arquivo se sim ele gera outro numero.
            foreach (glob(getcwd() . './usersaccounts/ids/*.txt') as $file) {
            $pointer = fopen($file, 'r');
            while (!feof($pointer)) {
            $line = fgets($pointer);

            foreach ($tokens as $token) {
            if (strpos($line, $token) !== false) {
                //$row[] = $line;
                $randger= rand() % 9000 + 1000;
                $randfix = $randger;
                break;
                }else{
                $fp = fopen('/usersaccounts/ids/' . $namemail. '.txt' , 'w');
                fwrite($fp, $randfix);
                fclose($fp);
                }

                }
            }}

            // não está registrando valor
            if ($username = null){
                echo "";
            }else{

                $fp = fopen('usersaccounts/' . $namenamef . '/username.txt' , 'w');
                if ($fp == false) die('Não foi possível salvar o registro.'); //a
                fwrite($fp, $username);
                fclose($fp);
            if ($userxp = null){
                echo "";
            }else{
                $fp = fopen('usersaccounts/' . $namenamef . '/xp.txt' , 'w');
                 if ($fp == false) die('Não foi possível salvar o registro.'); //a
                fwrite($fp, $userxp);
                fclose($fp);

            if ($username = null){
                echo "";
            }else{
                $fp = fopen('usersaccounts/' . $namenamef . '/name.txt' , 'w');
                if ($fp == false) die('Não foi possível salvar o registro.'); //a
                fwrite($fp, $username);
                fclose($fp);
            if ($userlevel = null){
                echo "";
            }else{
                $fp = fopen('usersaccounts/' . $namenamef . '/level.txt' , 'w');
                if ($fp == false) die('Não foi possível salvar o registro.'); //a
                fwrite($fp, $userlevel);
                fclose($fp);
            if ($usermail = null){
                echo "";
            }else{
                $fp = fopen('usersaccounts/' . $namenamef . '/email.txt' , 'w');
                if ($fp == false) die('Não foi possível salvar o registro.'); //a
                fwrite($fp, $usermail);
                fclose($fp);

                if ($progresso1 && $progresso2 && $progresso3 && $progresso4 && $progresso5 && $progresso6 && $progresso7 && $progresso8 = null){
                echo "";
            }else{

                //cria a pasta progress
                if (!file_exists("usersaccounts/$namenamef/progress/")) {
                mkdir('usersaccounts/'. $namenamef . '/progress/', 0777, true);
                }

                $fp = fopen('usersaccounts/' . $namenamef . '/progress/progresso1.txt' , 'w');
                if ($fp == false) die('Não foi possível salvar o registro.'); //a
                fwrite($fp, $progresso1);
                fclose($fp);

                $fp = fopen('usersaccounts/' . $namenamef . '/progress/progresso2.txt' , 'w');
                if ($fp == false) die('Não foi possível salvar o registro.'); //a
                fwrite($fp, $progresso2);
                fclose($fp);

                $fp = fopen('usersaccounts/' . $namenamef . '/progress/progresso3.txt' , 'w');
                if ($fp == false) die('Não foi possível salvar o registro.'); //a
                fwrite($fp, $progresso3);
                fclose($fp);

                $fp = fopen('usersaccounts/' . $namenamef . '/progress/progresso4.txt' , 'w');
                if ($fp == false) die('Não foi possível salvar o registro.'); //a
                fwrite($fp, $progresso4);
                fclose($fp);

                $fp = fopen('usersaccounts/' . $namenamef . '/progress/progresso5.txt' , 'w');
                if ($fp == false) die('Não foi possível salvar o registro.'); //a
                fwrite($fp, $progresso5);
                fclose($fp);

                $fp = fopen('usersaccounts/' . $namenamef . '/progress/progresso6.txt' , 'w');
                if ($fp == false) die('Não foi possível salvar o registro.'); //a
                fwrite($fp, $progresso6);
                fclose($fp);

                $fp = fopen('usersaccounts/' . $namenamef . '/progress/progresso7.txt' , 'w');
                if ($fp == false) die('Não foi possível salvar o registro.'); //a
                fwrite($fp, $progresso7);
                fclose($fp);
                // não está registrando valor
                $fp = fopen('usersaccounts/' . $namenamef . '/progress/progresso8.txt' , 'w');
                if ($fp == false) die('Não foi possível salvar o registro.'); //a
                fwrite($fp, $progresso8);
                fclose($fp);

            }
            }
            }
            }
            }
            }

}

            ?>

        <form  method="post">

        Nome de usuario:
        <input name="usernamesave" type="text"></input>

        Minha Experiêcia:
        <input name="userxp" type="text"></input>

        Meu Tagname User:
        <input name="userid" type="text"></input>

        Level da Minha Conta:
        <input name="userlevel" type="text"></input>

        Meu email pessoal:
        <input name="usermail" type="text"></input>

        Meu uso Progress01:
        <input name="userprog1" type="text"></input>

        Meu uso Progress02:
        <input name="userprog2" type="text"></input>

        Meu uso Progress03:
        <input name="userprog3" type="text"></input>

        Meu uso Progress04:
        <input name="userprog4" type="text"></input>

        Meu uso Progress05:
        <input name="userprog5" type="text"></input>

        Meu uso Progress06:
        <input name="userprog6" type="text"></input>

        Meu uso Progress07:
        <input name="userprog7" type="text"></input>

        Meu uso Progress08:
        <input name="userprog8" type="text"></input>

        Proseeguir pra sav
        <input name="proceed" type="text"></input>

<input name="submit"  type="submit" />

</form>

Answer: O seu problema provavelmente está ocorrendo por conta da forma na qual o seu if foi implementado. Você está utilizando o operador de atribuição '=' no lugar do operador de comparação '==' ou '==='
Veja o trecho de código abaixo como exemplo:
if ($username = null){
    echo "";
}else

Dentro do IF, você está atribuindo o valor 'null' a variável $username, eliminando o valor original da variável $username e deixando o valor 'NULL' a ser interpretado pelo IF, fazendo com que o código sempre entre no else, sempre gravando valores sem dados.
alterar os operadores pelo seu código deve resolver o problema.


Dúvida em query no SQL Server 2012

Question: Preciso que nessa consulta venha somente os registros em que a coluna CURVA ABC seja diferente da coluna MODELO.
Por exemplo, esse cliente a curva ABC dele é prata e o modelo é diamante/ouro, então, percebe-se que o nome prata não encontra-se no campo modelo.
SELECT
C.UsuID [Código],
    U.UsuNome [Cliente],
    P.ProNome [Produto],
    M.MCDescricao [Modelo],
    M.MCId [ID_Modelo],
    A.Descricao [Curva ABC],
    A.CurvaID [ID_Curva]
FROM Usuario U
INNER JOIN ParametroProdutoCliente PPC ON U.UsuID = PPC.UsuID
INNER JOIN ComplementoColunas C ON U.UsuID = C.UsuID AND C.LinhaID  = 1
                                                     AND C.CompID       = 32
                                                     AND C.ColunaID = 12
LEFT JOIN  CurvaABC     A     ON A.CurvaID = U.CurvaID
INNER JOIN Usuario            AU ON AU.UsuID = c.ColunaValor
JOIN ProdutoUsuario PU ON U.UsuID = PU.UsuID
    JOIN Produto P ON PU.ProID = P.ProID
    JOIN ModeloCriticidade M ON PU.MCId = M.MCId AND PU.ProID = M.ProID
WHERE U.EmpLiberada = 1 AND U.UsuTipo = 'C' AND PPC.ProID in(2,5,12)
AND U.UsuID = 316

O resultado é dessa maneira:
Cliente           Produto             Modelo     ID_Modelo  Curva ABC
ChilliBeans Conciliador e-Extrato   Diamante|Ouro   23       PRATA

Comment: `Where CurvaABC <> Modelo`?
Comment: Seria mais como se fosse um like. Na coluna Curva ABC, tem o nome prata, porém, esse nome prata nao está na coluna modelo. Entendeu?
Comment: Not exists https://technet.microsoft.com/pt-br/library/ms184297(v=sql.105).aspx , creio que resolva
Comment: Comparações usando cadeia de caracteres podem não ser práticas. Por acaso não existe forma de estabelecer correspondência entre as tabelas `CurvaABC` e `ModeloCriticidade`, usando as colunas `CurvaID` e `McID`?
Answer: Pelo o que entendi, você deseja obter os registros cujo valor da coluna CURVA ABC não esteja contido na coluna MODELO
Para isso, basta utilizar uma das seguintes condições:
Opção 1: CHARINDEX(CurvaABC.Descricao,ModeloCriticidade.MCDescricao) = 0
Opção 2: NOT ModeloCriticidade.MCDescricao LIKE '%' + CurvaABC.Descricao + '%'
Caso deseje que a query retorne os registros que não tenham correspondência na tabela CurvaABC (Descricao IS NULL), acrescente nas opções acima a função ISNULL(CurvaABC.Descricao,'xxx')
A query poderia ficar assim:
SELECT
    C.UsuID [Código],
    U.UsuNome [Cliente],
    P.ProNome [Produto],
    M.MCDescricao [Modelo],
    M.MCId [ID_Modelo],
    A.Descricao [Curva ABC],
    A.CurvaID [ID_Curva]
FROM Usuario U
JOIN ParametroProdutoCliente PPC ON U.UsuID = PPC.UsuID
JOIN ComplementoColunas C ON U.UsuID = C.UsuID A
                             AND C.LinhaID = 1
                             AND C.CompID = 32
                             AND C.ColunaID = 12
JOIN Usuario AU ON AU.UsuID = c.ColunaValor
JOIN ProdutoUsuario PU ON U.UsuID = PU.UsuID
JOIN Produto P ON PU.ProID = P.ProID
JOIN ModeloCriticidade M ON PU.MCId = M.MCId AND PU.ProID = M.ProID
LEFT JOIN CurvaABC A  ON A.CurvaID = U.CurvaID
WHERE U.EmpLiberada = 1
      AND U.UsuTipo = 'C'
      AND PPC.ProID in(2,5,12)
      AND CHARINDEX( ISNULL(A.Descricao,'xxx') , M.MCDescricao ) = 0

Comment: isso mesmo @Fernando, obrigadão cara

Dúvida em query no SQL Server 2012


SQL Query to remove cyclic redundancy

Question: I have a table that looks like this:
  Column A   |    Column   B    |     Counter
---------------------------------------------
      A      |       B          |       53
      B      |       C          |       23
      A      |       D          |       11
      C      |       B          |       22

I need to remove the last row because it's cyclic to the second row.  Can't seem to figure out how to do it.
EDIT
There is an indexed date field. This is for Sankey diagram. The data in the sample table is actually the result of a query. The underlying table has:
date   | source node | target node | path count

The query to build the table is:
SELECT source_node, target_node, COUNT(1)
FROM sankey_table
WHERE TO_CHAR(data_date, 'yyyy-mm-dd')='2013-08-19'
GROUP BY source_node, target_node

In the sample, the last row C to B is going backwards and I need to ignore it or the Sankey won't display. I need to only show forward path.
Comment: You're missing the point. How do you determine which way is 'forward' - why is it `B->C` and not `C->B`? All you seem to have are two rows which have target and source both ways around, and no obvious way to decide between them. It isn't clear if you want to use the date order (assuming they have different times recorded), or to pick one arbitrarily, or something else?
Comment: How do you know which row to remove, why not remove the 2nd row instead? unless you include a `order by` the database has no real concept of "last row" (ok, if the table is indexed it will likely always return the rows in order of the index but I would be hesitant to rely on that without doing a `order by` on the index columns)
Comment: The rows happen to be ordered accordingly.  I can ignore the second instance.
Comment: Ordered accordingly by what? You can't rely on the order they were add
Comment: Ordered by what, you can't rely on insert order. If you don't explicitly set an order, the DBMS will choose what ever is convenient for it.
Comment: There is an indexed date field.  This is for Sankey diagram.  The data in the sample table is actually the result of a query.  The underlying table has:

date  |  source node |  target node  |  path count

The query to build the table is:
SELECT
source_node, target_node, COUNT(1)
FROM sankey_table
WHERE TO_CHAR(data_date, 'yyyy-mm-dd')='2013-08-19'
GROUP BY source_node, target_node

In the sample, the last row C to B is going backwards and I need to ignore it or the Sankey won't display.  I need to only show forward path.
Comment: Yes I want to use the date order.  The first one is the correct one. Users select the status manually.  It shouldn't have been done that way but often they select the wrong one.  Like if an order is in "working" status it'll suddenly go to "new."  The date has a timestamp which I use to tell the "valid" state.
Comment: But the date field os not available in this table, only in the original table that was used to populate this one, right? Do you have access to that as well, and can you query against that instead; or can you adjust the process that populates the new table?
Comment: I can query against the original table. I've implemented procedures to adjust the process as well but still not perfect.
Answer: Removing all edges from your graph where the tuple (source_node, target_node) is not ordered alphabetically and the symmetric row exists should give you what you want:
DELETE
FROM sankey_table t1
WHERE source_node > target_node
AND EXISTS (
  SELECT NULL from sankey_table t2
  WHERE t2.source_node = t1.target_node
    AND t2.target_node = t1.source_node)

If you don't want to DELETE them, just use this WHERE clause in your query for generating the input for the diagram.
Answer: DELETE FROM yourTable
where [Column A]='C'

given that these are all your rows
EDIT
I would recommend that you clean up your source data if you can, i.e. delete the rows that you call backwards, if those rows are incorrect as you state in your comments.
Comment: It must be early!  I totally didn't think of deleting the rows because I really don't need them but this isn't going to do what I need.
Comment: then you must clarify what you need
Answer: If you can adjust how your table is populated, you can change the query you're using to only retrieve the values for the first direction (for that date) in the first place, with a little bit an analytic manipulation:
SELECT source_node, target_node, counter FROM (
  SELECT source_node,
    target_node,
    COUNT(*) OVER (PARTITION BY source_node, target_node) AS counter,
    RANK () OVER (PARTITION BY GREATEST(source_node, target_node),
      LEAST(source_node, target_node), TRUNC(data_date)
        ORDER BY data_date) AS rnk
  FROM sankey_table
  WHERE TO_CHAR(data_date, 'yyyy-mm-dd')='2013-08-19'
)
WHERE rnk = 1;

The inner query gets the same data you collect now but adds a ranking column, which will be 1 for the first row for any source/target pair in any order for a given day. The outer query then just ignores everything else.
This might be a candidate for a materialised view if you're truncating and repopulating it daily.
If you can't change your intermediate table but can still see the underlying table you could join back to it using the same kind of idea; assuming the table you're querying from is called sankey_agg_table:
SELECT sat.source_node, sat.target_node, sat.counter
FROM sankey_agg_table sat
JOIN (SELECT source_node, target_node,
    RANK () OVER (PARTITION BY GREATEST(source_node, target_node),
      LEAST(source_node, target_node), TRUNC(data_date)
        ORDER BY data_date) AS rnk
  FROM sankey_table) st
ON st.source_node = sat.source_node
AND st.target_node = sat.target_node
AND st.rnk = 1;

SQL Fiddle demos.

SQL Query to remove cyclic redundancy


Limit of $(x+1+\sqrt{(x+1)^2 +1})$ as $x\to-\infty$

Question: This seemed easy at first, but then I confronted my result with the one I got from WolframAlpha and they are different:
$$
\lim_{x\to-\infty}(x+1+\sqrt{(x+1)^2 +1})
$$
WolframAlpha says the limit is equal to 0, though for me it seems to be negative infinity. I tried some simple algebraic manipulation, namely multiplying each factor by 1:
$$
\frac{x\sqrt{(x+1)^2+1}}{\sqrt{(x+1)^2+1}}+\frac{\sqrt{(x+1)^2+1}}{\sqrt{(x+1)^2+1}}+[(x+1)^2+1]*\frac{1}{\sqrt{(x+1)^2+1}}
$$
The last part is 0, because the fraction goes to 0:
$$
\lim_{x\to-\infty}([(x+1)^2+1]*\frac{1}{\sqrt{(x+1)^2+1}})=0
$$
The middle part is 1 and the first part should be negative infinity, as the only factor left standing is x and it goes to negative infinity. What am I missing?
Comment: The last part does not converge to 0. $(x+1)^2$ converges to infinity and the fraction is just equal to $\sqrt{(x+1)^2 + 1}$, which therefore goes to infinity. The expansion of the fractions is pointless, I think.
Comment: hint: let (x+1) be "a".
Comment: @LEO, damn, so the +1 factor under the square root becomes irrelevant and the whole limit effectively becomes $a-a = 0$. That's what you're sayin'? :D
Comment: @MarekM. my hint is just for becoming multiplication with conjugate easy
Answer: Most of the time when you want to get rid of a root expression you use the 3rd binomial formula. So you would expand into a fraction with $(x+1-\sqrt{(x+1)^2+1})$
So we get:
$\frac{(x+1)^2-(x+1)^2-1}{x+1-\sqrt{(x+1)^2+1}}=\frac{-1}{x+1-\sqrt{(x+1)^2+1}}=\frac{1}{\sqrt{(x+1)^2+1}-(x+1)}$
We have $\sqrt{(x+1)^2+1}-(x+1)\to\infty$ as $x\to -\infty$. So the fraction goes to $0$.
Comment: @MarekM. it is multiplied by conjugate .
Comment: @MarekM. The main point of the solution is to use the 3rd binomial formula.
Namely $a^2-b^2=(a+b)(a-b)$, so we get rid of the root. It is not that different from your approach. We just have $\frac{(x+1+\sqrt{(x+1)^2+1})(x+1-\sqrt{(x+1)^2+1})}{(x+1)-\sqrt{(x+1)^2+1}}$
Comment: There's actually a plus sign between the $x+1$ and the square root. But anyway, I don't think I get your solution :(
Comment: After a good night sleep, this looks right. Thank you :)
Answer: I don't like limits at $-\infty$. I'd rewrite it as
$$\lim_{x\to\infty}\left(1-x+\sqrt{1+(x-1)^2}\right)
=\lim_{y\to\infty}\left(-y+\sqrt{1+y^2}\right)$$
(setting $y=x-1$). Then
$$-y+\sqrt{1+y^2}=\frac{-y^2+(1+y^2)}{y+\sqrt{1+y^2}}
=\frac{1}{y+\sqrt{1+y^2}}\to0$$
as $y\to\infty$.
Answer: Your mistake was in writing $\lim_{x\to -\infty}\frac{(x+1)^2+1}{\sqrt{(x+1)^2+1}}=0$. The expression $\frac{(x+1)^2+1}{\sqrt{(x+1)^2+1}}$ equals $\sqrt{(x+1)^2+1}$, which tends to $\infty$ as $x\rightarrow-\infty$.
You can attack this problem using other techniques. One approach is to multiply and divide by the conjugate of $x+1+\sqrt{(x+1)^2+1}$, namely $x+1-\sqrt{(x+1)^2+1}$, then use the difference of squares formula to simplify the result. Alternatively, if you are comfortable with hyperbolic functions and the exponential function $e^x$, you could write
$$x+1+\sqrt{(x+1)^2+1}=e^{\sinh ^{-1}(x+1)}$$
and use the fact that $\lim_{x\to -\infty}\sinh ^{-1}(x)=-\infty$ (this limit can be proven without using the logarithmic form of $\sinh ^{-1}x$). Since $\lim_{x\to -\infty}e^x=0$, we have that
$$\lim_{x\to -\infty}e^{\sinh ^{-1}(x+1)}=0$$
Thus,
$$\lim_{x\to -\infty}\left(x+1+\sqrt{(x+1)^2+1}\right)=0$$.
Answer: $$
\lim_{x\to-\infty}\left(x+1+\sqrt{(x+1)^2 +1}\right)
$$
$$=\lim_{x\to-\infty}\left(x+1+|x+1|\sqrt{1+\dfrac1{(x+1)^2}}\right)$$
$$=\lim_{x\to-\infty}\left(x+1+|x+1|\left[1+\frac1{2(x+1)^2}\cdots\right]\right)$$
$$=\lim_{x\to-\infty}\left(x+1-(x+1)-(x+1)\left[\frac1{2(x+1)^2}\cdots\right]\right)=0.$$
Comment: I don't understand where the second "line" in your solution comes from, namely the 1/2.
Comment: Binomial expansion $(1+y)^n=1+ny\cdots$ with $n=1/2$


Validação de um Select Option Dinamico

Question: Galera preciso de uma super ajuda!
Preciso validar um SELECT OPTION que é gerado dinamicamente:

                                <?php $i=1; foreach ($ImportFields as $EachField): ?>

                                        <?php echo $EachField; ?>

                                            <select name="MatchedFields<?php echo $i; ?>" id="MatchedFields<?php echo $i; ?>">
                                                <option value="0"><?php InterfaceLanguage('Screen', '1183'); ?></option>
                                                <?php foreach ($CustomFields as $EachCustomField): ?>
                                                    <option value="CustomField<?php echo $EachCustomField['CustomFieldID']; ?>"><?php echo $EachCustomField['FieldName']; print_r($EachCustomField['CustomFieldID']); ?></option>
                                                <?php endforeach; ?>
                                            </select>

                                <?php $i++; endforeach; ?>

O conteúdo do SELECT OPTION é igual em todos os campos.
O que eu preciso: fazer uma validação para que o usuario não selecione o mesmo item duas vezes, exibindo alguma mensagem ou um alerta avisando que o item "Email"(EXEMPLO) já foi selecionado no SELECT OPTION anterior.
Alguém pode me ajudar com alguma sugestão?
Comment: E quando mostra o aviso o que acontece com as opções duplicadas? muda a primeira, a ultima ou as duas?
Comment: Sergio, ele deve continuar na mesma pagina, ate que preencha todos os campos sem que nenhum se repita, deu para entender?
Comment: @Vitor Você conseguiu resolver o problema?
Answer: Eu faria da seguinte forma:
Defina a função count dentro do escopo de um objeto do tipo Array
    // Definição de função no escopo de Array para contar
    // quantas vezes um elemento aparece no conjunto.
    // Uso: var x = ['a','a','b','c'];
    //      x.count('a'); // retorna 2
    Array.prototype.count = function (what) {
        var count = 0;
        for (var i = 0; i < this.length; i++) {
            if (this[i] === what) {
                count++;
            }
        }
        return count;
    }

Crie um array para guardar as opções selecionadas
var opts = [];

Coloque um evento de change nos seus selects
$('select[id*="MatchedFields"]').on('change', function (){
    if (!(opts.count($(this).val()) > 0)) {
        opts.push($(this).val());
    }else{
      // exibe uma mensagem de campo inválido
    }
});

Validação de um Select Option Dinamico


Need help at aligning text image and buttons

Question: I am in need of help. I am currently developing my new webstore for my website, and I cannot figure out ho to properly align my text, images and buttons. I have tried many things in order to align them, but I cannot seem to figure out how to do it myself. I'll provide more info if needed. Thanks in advance!
Here is my code and below you can see the result:
Comment: I've put your code into a JSfiddle here: https://jsfiddle.net/qLfusemy/
Answer: Assuming that you just want to align all the items vertically, you can simply give the container a value of text-align:center while giving the containing block a width. So for the purpose of your code it would be something like this. Not that I've changed your markup as you were using inline styling.
The wrapper I've set as display:flex to help it align nicely. You can read about flexbox here
HTML:

MVP

<button>helpme</button>

MVP

<button>helpme</button>

MVP

<button>helpme</button>

<

CSS:
.wrapper section {
    text-align:center;
    justify-content:center;
    width:200px;
 }

 .wrapper {
   display:flex;
 }

Fiddle
Comment: It's kind of working, but the buttons and the alignment of the entire thing is wierd. https://ibb.co/jaC4RF
Comment: Let me add another comment, to show you what it looked like before i wanted to change it: https://ibb.co/iPF8mF :)
Comment: If you want the columns to remain in the center, you can use `justify-content:center` on the `wrapper`
Comment: @Marius if you want the items to align vertically, you can use on the `wrapper`, `flex-direction:column`
Comment: Alright, it's working on centering everything, but the buttons are still really wierd ? https://ibb.co/jZKJmF
Comment: Is there some other CSS overriding the buttons? Do they have a width? It looks like they are getting clipped. Without your code I don't think I can help further.
Comment: Here is the entire code + the page: Page: fireflake.namelessmc.com/nettbutikk Code: http://pastebin.com/LaimeSg2


Looping no request

Question: Como eu poderia fazer um looping para trazer informações do request de uma lista de coordenadas de um csv e levar o resultado para outro csv?
import requests
import pandas as pd

parametro = dict(latitude=-23.512294, longitude=-46.667259, status=1, lista=1, limite=96, acessibilidade='')

r = requests.post('https://www.banco24horas.com.br/index/busca-json-terminal', data=parametro)

df = r.json()
display(df)

Comment: Olha -  dizer como se grava um CSV a partir do grosso desses dados é relativamente fácil - , mas se você (ou vocês) tiverem qualquer outra necessidade de programação por ai, eu sugiro estudar a linguagem Python de forma mais sistemática por alguns dias
Answer: O valor devolvido após feit a a requisição não é um "CSV" - (CSVs é um nome genérico para um tipo de arquivo, nem é um formato de dados que exista na memória).
De qualquer forma, uma vez que você já está usando Pandas, gravar um arquivo CSV a partir da chave detalhes dessa resposta é trivial. (A resposta não retorna apenas uma lista com informações sobre as ATMs, há outros campos também):
import requests
import pandas as pd

parametro = dict(latitude=-23.512294, longitude=-46.667259, status=1, lista=1, limite=96, acessibilidade='')

r = requests.post('https://www.banco24horas.com.br/index/busca-json-terminal', data=parametro)

df = pd.DataFrame(r.json()['detalhes']
df.to_csv('meu_arquivo.csv')

A chama .to_csv do Pandas já cria o arquivo e grava todos os dados serializados de forma correta.

Looping no request


Propensity score matching: bias adjustment

Question: I'm using propensity score matching to match similar individuals. I.e., I first estimate a propensity score (the probability of treatment conditional on some set of variables) and then match on the estimated probabilities. Because this matching uses only one covariate (i.e. the propensity score) I had assumed that there's no need to use bias adjustment?!
Apparently I'm wrong about this, but I've not received a convincing argument why. Can anybody out there give me a compelling reason why I'd need bias adjustment in this case?
It was my understanding that one only needs to use bias adjustment when matching on more than one covariate (for example, nearest neighbour matching uses a bias correction term when matching on more than one covariate to speed up convergence).
Thanks (and sorry for the long question).
Answer: Bias adjustment after matching is only needed if you have imperfect matches, as typically occurs with more than one covariate. In that case an additional regression adjustment is helpful to reduce residual bias after matching. If you have one covariate only, and conditional on this one covariate the data generating mechanism is unconfounded, and you have overlap in your covariate distribution between treatment and control groups, you might find a perfect match for all cases, and there is no potential bias. This situation is, however, not common in practice. Likewise uncommon in practice is that you find perfect matches for all units on a multivariate covariate distribution. In general, the higher the dimensionality, the less likely it is that you have overlap and can find perfect matches. Hence there is usually bias after matching in practice.


Does complex analysis generalize to analysis of $2\times2$ invertible matrices?

Question: We can encode the complex numbers as matrices by letting $1$ be the identity matrix and $i = \left( \begin{array}{clcr} 0 & -1\\1 & 0 \end{array} \right)$. From this fact, this means that complex analysis is equivalent to the analysis of matrices of the form $ \left( \begin{array}{clcr} a & -b\\b & a \end{array} \right)$.
If we removed the restriction on the form of the matrices, and instead studied all invertible $2\times2$ matrices, how much of the theorems from complex analysis would generalize to this new system? Is there any place where I could read more about this?
Comment: Not much, I think, because invertible $2\times 2$ matrices do not form a field.
Comment: Note that the set of invertible $2\times 2$ matrices is not even closed under addition.
Comment: If you use general $2\times2$ (real) matrices, you just get (two-dimensional) multivariable calculus.
Answer: The set of real matrices of the form $\left(\begin{smallmatrix}a & -b \\ b & a\end{smallmatrix}\right)$ together with matrix addition and matrix multiplication form a field, that is, the field of complex numbers.
The set of all real $2\times 2$ matrices with the same addition and multiplication forms a unitary ring that is not commutative, does not admit inverses for all non-zero elements and hence misses a lot of properties that a field has.
Now the analysis for complex functions and such gadgets heavily depends on $\mathbb C$ being a field, so most of it will fail for functions over the ring of all real $2\times 2$ matrices.


http request via iptables --to-destination ip redirect results in no response

Question: I have two Ubuntu servers with each having their own ip addresses.
Let's call them server1 and server2, having respectively ip 220.127.116.11 and 126.96.36.199
I have a nginx running on server2. The sole purpose I want server1 to have is to redirect all incoming http (so port 80) requests to server2 without clients noticing that their request is being redirected.
I tried the following command on server1:
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 126.96.36.199
But when I enter 18.104.22.168 in my browser I get no respond: the page keeps trying to load without giving any message or error message (I get a time-out after 2-3 mins).
But when I do remove the above iptables rule I immediately do get a "page not found error" when I enter 188.8.131.52 in my browser; so something is working but not as it should: when I enter 184.108.40.206 I want the html page to load that is hosted on 220.127.116.11
Because when i enter 184.108.40.206 in my browser I do see the webpage loaded.
Could anyone please help me with this? I am searching quite some time (on severfault & Google) on this now so that's why I ask.
Many thanks for reading my question!
Update:
Thank you all for you information.
Unfortunately I still get no response
I have the following iptables configuration:

root@ip-10-48-238-216:/home/ubuntu# sudo iptables -L
  Chain INPUT (policy ACCEPT)
  target     prot opt source               destination
Chain FORWARD (policy ACCEPT)
  target     prot opt source               destination
Chain OUTPUT (policy ACCEPT)
  target     prot opt source               destination
  root@ip-10-48-238-216:/home/ubuntu# sudo iptables -t nat -L
  Chain PREROUTING (policy ACCEPT)
  target     prot opt source               destination
  DNAT       tcp  --  anywhere             anywhere            tcp dpt:www to:188.8.131.52
Chain OUTPUT (policy ACCEPT)
  target     prot opt source               destination
Chain POSTROUTING (policy ACCEPT)
  target     prot opt source               destination

When i run tcpdump and do request via chrome to 18.104.22.168 i get the following

root@ip-10-48-238-216:/home/ubuntu# sudo tcpdump -i eth0 port 80 -vv
  tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
  13:56:18.346625 IP (tos 0x0, ttl 52, id 12055, offset 0, flags [DF], proto TCP (6), length 60)
     212-123-161-112.ip.telfort.nl.16386 > ip-10-48-238-216.eu-west-1.compute.internal.www: Flags [S], cksum 0xb398 (correct), seq 2639758575, win 5840, options [mss 1460,sackOK,TS val 1223672 ecr 0,nop,wscale 6], length 0
  13:56:18.346662 IP (tos 0x0, ttl 51, id 12055, offset 0, flags [DF], proto TCP (6), length 60)
     212-123-161-112.ip.telfort.nl.16386 > ww1dc1.shopreme.com.www: Flags [S], cksum 0x9ee0 (correct), seq 2639758575, win 5840, options [mss 1460,sackOK,TS val 1223672 ecr 0,nop,wscale 6], length 0
  13:56:18.598747 IP (tos 0x0, ttl 52, id 10138, offset 0, flags [DF], proto TCP (6), length 60)
     212-123-161-112.ip.telfort.nl.16387 > ip-10-48-238-216.eu-west-1.compute.internal.www: Flags [S], cksum 0xac40 (correct), seq 2645658541, win 5840, options [mss 1460,sackOK,TS val 1223735 ecr 0,nop,wscale 6], length 0
  13:56:18.598777 IP (tos 0x0, ttl 51, id 10138, offset 0, flags [DF], proto TCP (6), length 60)
     212-123-161-112.ip.telfort.nl.16387 > ww1dc1.shopreme.com.www: Flags [S], cksum 0x9788 (correct), seq 2645658541, win 5840, options [mss 1460,sackOK,TS val 1223735 ecr 0,nop,wscale 6], length 0
  ^C
  4 packets captured
  4 packets received by filter
  0 packets dropped by kernel

the mentioned address relate to the following
212-123-161-112.ip.telfort.nl.16386 : my personal computer
ww1dc1.shopreme.com.www : dns of server2 (184.108.40.206)
ip-10-48-238-216.eu-west-1.compute.internal.www : amazon web services ec2 internal address of server1 (126.96.36.199)
However, the tcpdump log on server2 (220.127.116.11) stays empty and I get no response back in my browser.
I am able to ping from server1 to server2.
And net.ipv4.ip_forward is set to 1 and so is /proc/sys/net/ipv4/ip_forward
Could there be anything else that is missing?
Update2:
Server1 is an aws ec2 micro instance, it is attached to a elastic ip which I called 18.104.22.168. all the time. The micro server used to be a bigger ec2 server, but due to its high costs we are switching to dedicated hosting. But in our iPhone client we used a fixed ip address, namely the elastic ip mentioned above. The iPhone client uses REST services on the server. So we now use a micro server on that elastic IP that should redirect all requests to a vps from an other company (www.xlshosting.nl) which has a different subnet. I do not know functionality that can redirect an aws elastic ip to an external ip address, so that's why I try it with a micro instance + iptables. That vps is what I called server2 all the time with ip 220.127.116.11. So server1 and server2 are on a different subnet. Does this help you?
Answer: It sounds like you could have a routing problem. Check using tcpdump on both servers and pointing tcpdump with the -i option to the correct ethX device to check if packets are being sent through the right network device on server1 and these packets also arrive at server2.
tcpdump -i ethX host 126.96.36.199 and port 80

Could it be that the packets arrive at server2 but server2 doesn't know where to send the replies to? Does server2 have a correct default gateway configured?
Check it with
route -n

Answer: run
echo 1 > /proc/sys/net/ipv4/ip_forward

To make the change permanent, put the following in /etc/sysctl.conf
net.ipv4.ip_forward = 1

Answer: Your problem can be:
1- Routing problem: You need to check the following:
1.1- Routing is enabled on Server1 as suggested by Patrick. File /proc/sys/net/ipv4/ip_forward is set to 1.
1.2- Server2 is able to reach the requesting client. You can try pinging server2 22.214.171.124 from your client. You can add static route using route add command.
2- Firewall problem: You need to check that your firewall allows you to access the IP 126.96.36.199. You can for simplicity set firewall policy to accept all traffic (INPUT, OUTPUT, FORWARD).
Comment: Thank you all for your information. Unfortunately it is still not working. Could you please be so kind to have a look at my updated question?
Comment: It seems that you have a problem setting up your network! Can you please tell us the IPs you are using and how you are connecting your servers and client? If you are using public IPs, you can convert them to private IPs. The IPs in the form 184.108.40.206 and 184.108.40.206 are not helpful. Use private IPs like: 192.168.0.1/24 and 188.8.131.52.2/24. This way I can know that they are in the same subnet.
Comment: Thanks for your reply again! I have updated my question in which I try to explain it. Does that help you?
Answer: I have a few questions:
What is the purpose of your setup? Is server1 meant to be a firewall that proxies to server2 because you don't want server2 to be directly accessible by the net?
How are the two machines connected? Directly via an crossover or though a router or something else?
Have you tried running tcpdump on both interfaces of server1 as well as the interface on server2? Running all three at the same time during a request will give you a pretty good idea of where things are getting mucked up.


Start Activity from a ListView

Question: I am French but I'll try to explain my problem I have a ListView and in this ListView I'd like to start DIFFERENT activity, I explained :
The first two Items must launch a PDF but the third " Sur Facebook" must to launch a new activity, specifically a new view (facebook.java), I can do it with a button but I can not since my ListView , here is my code :
    package com.androiddev.tab;

import java.io.FileNotFoundException;
...

public class Tab5 extends ListActivity implements OnClickListener {
private ListView maListViewPerso25;

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

                Button button = (Button) findViewById(R.id.imageButtonSelector2);
                button.setOnClickListener(this);

                maListViewPerso25 = (ListView) findViewById(R.id.listView25);
                ArrayList<HashMap<String, String>> listItem25 = new ArrayList<HashMap<String, String>>();
                HashMap<String, String> map;
                map = new HashMap<String, String>();
                map.put("titre", "1 - Qui sommes-nous ?");
                map.put("img", String.valueOf(R.drawable.fleche));
                map.put("file", "QSN.pdf");
                listItem25.add(map);
                map = new HashMap<String, String>();
                map.put("titre", "2 - A propos de l'application");
                map.put("img", String.valueOf(R.drawable.fleche));
                map.put("file", "Apropos.pdf");
                listItem25.add(map);
                map = new HashMap<String, String>();
                map.put("titre", "3 - Sur Facebook");
                map.put("img", String.valueOf(R.drawable.fleche));
                listItem25.add(map);
                SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem25, R.layout.afichageitem,
                  new String[] {"img", "titre" }, new int[] {R.id.fleche, R.id.titre});
                maListViewPerso25.setAdapter(mSchedule);

                //LISTENER

                maListViewPerso25.setOnItemClickListener(new OnItemClickListener() {
                   public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                         HashMap<String, String> map = (HashMap<String, String>) maListViewPerso25.getItemAtPosition(position);
                         String nomFichierDansAsset = map.get("file");
                         String nomFichierTemp = "list25.pdf";

                         if (copyAssetToTempFile(nomFichierDansAsset, nomFichierTemp)) {
                          try {
                                String name = getFileStreamPath(nomFichierTemp).getAbsolutePath();
                                Uri uri = Uri.parse("file://" + name);
                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                intent.setDataAndType(uri, "application/pdf");
                                startActivity(intent);
                          } catch (ActivityNotFoundException e) {
                                // Cas d'erreur si pas de lecteur PDF installé
                                Log.d("xx", "Erreur affichage PDF", e);
                          }
                        }
                  }
                 });

                 };
  private boolean copyAssetToTempFile(String nomFichierAsset,
           String nomFichierTemp) {
           boolean result = true;
           try {
                 byte[] buffer = new byte[512];
                 FileOutputStream fos = openFileOutput(nomFichierTemp, MODE_WORLD_READABLE);
                 InputStream is = getAssets().open(nomFichierAsset);
                 int bytesRead = is.read(buffer);
                 while (bytesRead > 0) {
                   fos.write(buffer, 0, bytesRead);
                   bytesRead = is.read(buffer);
                 }
                 fos.close();
                 is.close();
           } catch (FileNotFoundException e) {
                 // Cas d'erreur de création de fichier
                 Log.d("xx", "Erreur creation fichier ", e);
                 result = false;
           } catch (IOException e) {
                 // Cas d'erreur de lecture de fichier
                 Log.d("xx", "Erreur lecture fichier", e);
                 result = false;
           }
           return result;
         }
// POUR LE BOUTON
  public void onClick(View src) {
         Intent i = new Intent(this, TabAndroidActivity.class);
         startActivity(i);
  };
  }

Comment: This is a large chunk of code without any context of the error - you could explain what isn't working, or post a [SSCCE](http://sscce.org/).
Comment: Trace, log or debug to determine what's happening. Btw, don't do long operations in the UI thread of android. This thread is used to paint stuff on screen and react to user inputs, if you use it, as you do to copy files, or other somewhat lenthy operations, you will get ANR on many devices. Learn async tasks
Comment: Sorry for this long code I just want when I click on "3 - Sur Facebook" launches a new view , new activity (facebook.java).
So I want to know how i can do that
Answer: try this code:
if(map.get("titre").equals("YourTile") {
    Intent mainIntent = new Intent(Tab5.this, "Your Facebook Activity Here".class);
    startActivity(mainIntent);
}

Answer: You can do first thing in onItemClick
if(map.get("titre").equals("3 - Sur Facebook") {
    startActivity(new Intent(this, TabAndroidActivity.class));
    return;
}

This is ugly but if it is what you're looking for then you will be able to do it better

Start Activity from a ListView


assert_select and assert_template test. What is it doing?

Question: This is from the Hartl Tutorial:
Here is my test:
test "index including pagination" do
    log_in_as(@user)
    get users_path
    assert_template 'users/index'
    assert_select 'div.pagination'
    User.paginate(page: 1).each do |user|
      assert_select 'a[href=?]', user_path(user), text: user.name
  end
end

Here is my code which is the index.html.erb. I am not showing the application.html.erb.
<% provide(:title, 'All users') %>

All users

<%= will_paginate %>

  <% @users.each do |user| %>

      <%= gravatar_for user, size: 50 %>
      <%= link_to user.name, user %>

  <% end %>

<%= will_paginate %>

What is the assert_template doing?
This is from the docs:

Asserts that the request was rendered with the appropriate template
  file or partials.

Is it checking the name of the view or partial? Or is it checking the URL?
What is assert_select doing here? This is from the docs:

An assertion that selects elements and makes one or more equality
  tests.

Is it checking that there exists a
 on the page? What is it doing? What is it asserting?
Answer: First send a get request to the users path
get users_path

After this check that the rendered template is the right one.
assert_template 'users/index'

It is not checking directly the url, but if you have any problems with routing, you won't get the right template.
Then, for testing the pagination, it checks that in the template exists a div element with class="pagination", this element is added by the paginations gems used.
assert_select 'div.pagination'

assert_select gets the html elements of the template and check if a given element is present.
Finally, it gets the users of the first page of the pagination (the ones should be on the index page) and for each one checks that in the page there is an a element linking to the url of the user and enclosing the text corresponding to the user name.
User.paginate(page: 1).each do |user|
  assert_select 'a[href=?]', user_path(user), text: user.name


Group (mathematics) Conjecture

Question: Given $(G,•)$ as a Group with finite set $G$, operator •.
Define: subset $S \subset G$ is called the core of the Group if and only if
$$ \{ x•y ~|~  x \in S, y \in S \} = G \setminus S$$
Conjecture 1: if $G$ has at least two elements,  core exists.
Conjecture 2: if core exists, any two cores have the same cardinality.
Conjecture 2.1: given prime $p \ge 3$, for $G = \{1,2,\ldots,p-1\}$ and operator • is 'multiplication modulo p', then any core $S \subset G$ has cardinality $(p-1)/2$.
Example:
$p = 7$, $S = \{3,5,6\}$
$1 \equiv 3*5 \equiv 6*6\pmod{7}$
$2 \equiv 3*3 \equiv 5*6\pmod{7}$
$4 \equiv 3*6 \equiv 5*5 \pmod{7}$
Question: anyone can find any (related) reference of the definition of core?
Comment: Can we have some motivation as to why we care about this construction? That might help in finding references.
Comment: It looks like in the context of example $\mathbb Z_p^*$, the multiplicative group modulo a prime, that your subset $S$ consists of the quadratic nonresidues mod $p$.  Searching on "quadratic residue" will turn up a lot of links to this classic number theory topic.
Comment: Are you asking for help with proving the conjectures?  Or are you just searching for corroborating references?
Comment: @Don Thousand
I came up this when studying the sequences containing no 3-term arithmetic progressions (Check Szekeres's sequence, A003278).
For any two numbers $a$ and $b$ from the sequence, $2a-b$, $2b-a$, $(a+b)/2$ must not appear in the sequence.
Comment: @Dohleman
I am just searching for corroborating references.
Comment: @hardmath
You are right, thanks. But also needs to prove that $a*b$ does not appear in the set.
Comment: @Kevin: That is a well-known property of quadratic (non)residues.
Comment: First, there's a standard unrelated definition of core of a subgroup in a group. Let's forget this. Second, you shouldn't say "the core of the group $G$", but "a core in the group $G$". "The" would mean you define some subset, as we can define "the center of $G$", while you define "a proper subgroup of $G$", etc.
Answer: Note. Core already has a meaning in Group Theory: given a group $G$, the "core of a subgroup $H$" is the largest normal subgroup of $G$ contained in $H$. Since your set $S$ can never contain the identity, it can never be a subgroup, so this meaning of "core" is antithetical to the standard meaning. Please consider changing the name.
Conjecture 1 is false. Consider the cyclic group of order $3$, generated by $x$. If $S$ were a "core" of $G$, then it cannot contain $e$ (since $e\bullet e = e\notin G-S$). Now, if $x\in S$, then $x^2=x\bullet x\notin S$; and if $x^2\in S$, then $x=x^2\bullet x^2\notin S$. Thus, $S$ would have to be either empty (in which case it cannot satisfy your condition) or else $S$ contains exactly one element, in which case $\{s\bullet t\mid s,t\in S\}$ contains exactly one element, and cannot be equal to $G-S$.
It is true that the quadratic nonresidues form a set as given. More generally, if $G$ has a subgroup $H$ of index $2$, then $G-H$ works as a set $S$, since $H$ is normal and $G/H$ is cyclic of order $2$; thus the product of two elements not in $H$ is always in $H$, and every element of $H$ can be expressed that way: take $h\in H$, $x\notin H$; then $x^{-1}(xh) = h$ is a product of two things in $G-H$.
Conjecture 2 is also false. Consider $G=S_3$. As noted above, if we take $H=\{e, (1,2,3), (1,3,2)\}$ which is a subgroup of index $2$, then $S=G-H = \{(1,2), (1,3), (2,3)\}$ will satisfy $SS=G-H$. However, so does $S'=\{(1,2), (1,2,3)\}$: indeed, we obtain the other four elements as the products (I compose permutations right-to-left):
$$\begin{align*}
(1,2)(1,2) &= e\\
(1,2)(1,2,3) &= (2,3)\\
(1,2,3)(1,2) &= (1,3)\\
(1,2,3)(1,2,3) &= (1,3,2).
\end{align*}$$
That is, $(S')(S') = G-S'$. However, $S$ has three elements and $S'$ has two.

Group (mathematics) Conjecture


Select max em nested queries

Question: Tenho de selecionar o candidado que tem mais publicações que qualquer outro dos candidatos.
select candidato.nome, Count(1) as nr_publicacoes
from candidato, cv,ficha_inscricao, publicacao_cv
where candidato.bi = ficha_inscricao.bi and
      candidato.bi = cv.bi and cv.cod_cv = publicacao_cv.cod_cv
group by candidato.nome;

Porém quando faço o max, o Orale SQL Developer não dá resultados nem mensagens de erro.
Não quero usar o rownum, o meu objetivo é resolver usando o max.
Answer: Em conversa pelo chat, a @DanielaMaia disse que não poderia ser usado ROWNUM, apenas o MAX. Portanto, a resposta original foi substituída por essa agora.
Para evitar o uso do ROWNUM, uma CTE (Common Table Expression) pode ser usada, como mostrado abaixo:
with publicacao_cte (nome,nr_publicacoes)
AS
(
    select
        candidato.nome,
        Count(1) as nr_publicacoes
    from
        candidato,
        cv,
        ficha_inscricao,
        publicacao_cv
    where
        candidato.bi = ficha_inscricao.bi and
        candidato.bi = cv.bi and
        cv.cod_cv = publicacao_cv.cod_cv
    group by
        candidato.nome
)

select nome
from publicacao_cte
where publicacao_cte.nr_publicacoes = (select max(nr_publicacoes) from publicacao_cte);

Comment: Atualizei a pergunta, veja se é isso, por favor.
Comment: Resolve, mas o objetivo era usar entender como usar o `max` em vez do `rownum`
Comment: Na linha 16 o identificador `nr_publicacoes` é inválido, não existe.
Comment: `nr_publicacoes` era uma variável que eu estava a tentar criar usado o `as`
Comment: @DanielaMaia, não sei como é a estrutura do seu banco de dados. Certamente é um erro pelo meu desconhecimento do seu modelo. O campo nr_publicacoes pertence ao qual tabela?
Comment: não pertence a nenhuma tabela, como poderá ver na minha pergunta, na 2ª linha estou a tentar criar essa nova variável com o comando `as`
Comment: Entendi, dessa maneira você não resolverá esse problema. Você conta o numero de publicacoes por candidato usando a tabela publicacao_cv?
Comment: Vamos [continuar esta discussão no chat](http://chat.stackexchange.com/rooms/31870/discussion-between-daniela-maia-and-cantoni).

Select max em nested queries


Tips for a New Teacher

Question: I'm an intermediate guitar player. I've been playing for a few years, mostly teaching myself with a few months of lessons in the middle. I'm not a great player, or even particularly good; but I can read tab, read music, strum chords, and play some songs. I'm not fast, but that's OK, my internal blues is slow. I know enough theory to get into trouble.
Now, someone has asked me to help him learn to play.
I'm happy to help, and I've been clear about my limitations, so he doesn't think he's getting some virtuoso instructor.
But I was wondering where to start and how fast to go. I thought I could start with the open position chords. But I think that maybe, I should really start at the major scale, because everything really comes from that.
I thought maybe:

First Lesson

Getting to know your guitar
Holding, fretting, picking, strumming
The major scale

Second Lesson

Review first lesson
Chords
Major Chords
Three Little Birds

Does that seem like too much? Too little? If I could have been playing a song after my second attempt at playing, I'd have been thrilled. What would you have wanted to learn in your first lesson or two?
EDIT: With the help of the answers below, I've decided that I should have my student be able to play a song at the end of the first lesson. So I think I'll reverse the order of the lessons - to get to the good stuff early, and then come back and talk about the major scale and how everything derives from it. So the revised lesson plan looks more like this:

First Lesson

Getting to know your guitar
Holding, fretting, picking, strumming
Open chords: A, D, and E
Three Little Birds (or another easy A, D, E tune)

Second Lesson

Review first lesson
The Major Scale
Chord construction
Minors and 7ths of A, D, and E

Does this look like the first two guitar lessons you wish you'd gotten?
Comment: Great question - you might also find some additional advice in this question.  http://music.stackexchange.com/questions/2144/what-should-a-guitarist-learn-first-rhythm-or-lead
Comment: Consider D, G, A -- with the one-fingered version of G.
Comment: Wish I had gotten that, my guitar teacher probably noticed that the group I was in and I couldn't have learnt that fast though
Answer: For the first few lessons, that actually looks good. For everything you want him to play, just make sure it is written out somewhere. Just try not to rush him along. Learning songs is good - it's usually what people want to work towards after all, but it'd be nice to keep them very simple and try to have each one you teach him go along with what techniques you've been teaching him.
Also, you should really teach him how to read music and not just tabs. I know there are guitar players out there who think it's unimportant, but it's really essential to getting a grasp on more advanced playing. It allows you to really study more advanced theory when the time arrives for that and improvising relies on how well you know your theory.
Also, think back to when you were first learning. I bet you struggled a lot and got real frustrated. Make sure you're patient with him and make sure he knows to take his practicing slow and be patient. The guitar can be very difficult to get a feel for at first, but it'll come before he knows it.
Oh, and cliché, I know, but make sure he's having fun!
Answer: I started learning with a few pointers from my dad. he showed me 3 chords that happened to fall nicely into a 1, 4, 5 progression then he went to the pub. I proceeded to spend a good while improving my changes between em, and finding that no matter what order i strummed them in, it sounded good :)
Quickly getting to the point of playing something cool is extremely important for keeping up motivation so getting that simple foundation of rock and blues music is a handy starting point imho. Learning your scales is obviously important but won't be something that can be usefully applied and played with straight away, so I'd try to get those first 3 chords into the first session. It could be full open chords or just simple power chords to simplify the required fingering, but it'll give something that can be played with rather than simply practiced between lessons.
Answer: I think your ideas look good. What length of sessions do you plan on providing - remember not to make it too long, both for attention span and pain in fingertips reasons.
For teaching, I always tried to make sure that they could play a basic blues progression by end of lesson one, usually A, D E to keep things simple, and end with a quick demo of a range of songs which used it (including some that just use power chords) to show how you can play a basic version of a tune very quickly and improve it as you get more experience.
Providing some quick wins like that keeps motivation and excitement high.
Answer: Good lessons depend on the student and the setting.
I think  you method is quite organized. However, I think there might need to be an emphasis on fun for your friend. If it is your friend, and not a paid student you can be much more informal with him. Try and get to the bottom of his guitar interest, and find simple exercises that speak to his interest.
When I did professional lessons, I would use examples like you used. When I would teach my friends I would start with smoke on the water.
Answer: That sounds like a good path to success! I will add just a couple things that were very helpful when I started, as I can honestly say I knew absolutely nothing. Maybe ask your student what their goals are, such as what do you want to play? Lead? Rhythm? Electric/Bass? What kinds of music do you want to play?
In addition to learning a song, I'd start with basics. Are you focused on teaching the core foundations of music? Like chords, scales, rhythm, a very basic song (or two). Also, I had to be taught about guitar care, string changing, clipping my nails, everything :-D
I really didn't know much about what my teacher asked me, but he was extremely patient and has remained that way. I'm never afraid to ask any question. That is what makes me happy to come back month after month, year after year.
Even though just as I feel I conquered something he gave me to do, there is something more challenging presented. I'm sure you will be a
 fantastic teacher too! Good luck! - Catz
Answer: There's lots of good advice here, and two things I'd like to add. First is making sure his instrument is comfortable to play. I believe a properly set up guitar is important for beginners. Also, stress the importance of being able to play with others, working with others can give a person added incentive to learn what they need to know. These points get lost in the mix and excitement of beginning to learn which can lead to physical pain in the fingers and lack of a sense of purpose as the learning adventure moves forward.

Tips for a New Teacher


How to convert existing taxonomy terms (to ucwords) with Termcase module?

Question: I'm using the Termcase module to make all the taxonomy terms ucwords in a specific vocabulary.

It also has the option: Convert the existing terms in this vocabulary immediately
It works for making the term UPPERCASE, lowercase but doesn't work for ucwords although it has the ucwords (propercase) option (first character of each word uppercase).
And if needed this is the related code part in termcase.module:
<code>case TERMCASE_PROPERCASE;
  $words = explode(' ', $original_string);
  foreach ($words as $key => $word) {
    $words[$key] = drupal_ucfirst($word);
  }
  $converted_string = implode(' ', $words);
  break;
</code>

What should I do to make all the existing terms ucwords?
Comment: Directly applying the ucwords option didn't work and I found a way to do what I wanted. At first, I converted all the terms to lowercase and then to ucwords and it worked. If there isn't any better solution to this, I will use that as a solution.
Answer: Not a perfect solution but this is the way I used to solve the problem while converting terms to ucwords:
If all the letters of terms are lowercase then applying ucwords is working correctly.

Go to the vocabulary page you wanted to convert terms (admin/structure/taxonomy/VOCABULARY_NAME).
Select the "Convert all characters to lowercase" option.
Enable the "Convert the existing terms in this vocabulary immediately."
Save and then it will updates (converts) all the terms to lowercase.
Then select the "Convert the first character of each word to uppercase" option.
Save and then it will updates (converts) all the terms to ucwords.

Done.

original -> step 1-4 -> step 5-6

my first term -> my first term -> My First Term
MY SECOND TERM -> my second term -> My Second Term
My Third Term -> my third term -> My Third Term
mY FourTH TErm -> my fourth term -> My Fourth Term

It works correctly.

(If you directly apply "Convert the first character of each word to uppercase" option to these terms above, the results would become:
original -> ucwords

my first term -> My First Term
MY SECOND TERM -> MY SECOND TERM
My Third Term -> My Third Term
mY FourTH TErm -> MY FourTH TErm

Doesn't work correctly if the words has uppercase letters. So do as I mentioned above (steps 1-6).


Setting the value of one column to a list of shape (1, 2), where each of the 2 nested lists is the value of another column

Question: Take for example that I have a DataFrame of two columns A and B, each consisting of a single number. I would now like to add another column, C which is a nested list, each internal list containing the single value from A and B.
           A      B                 C
index
    0    2.3    6.4    [[2.3], [6.4]]
    1    5.6    2.8    [[5.6], [2.8]]
    2    8.2    9.0    [[8.2], [9.0]]

I have tried
df['C'] = [[x] for x in df[['A', 'B']].values.tolist()]

but this results in
           A      B               C
index
    0    2.3    6.4    [[2.3, 6.4]]
    1    5.6    2.8    [[5.6, 2.8]]
    2    8.2    9.0    [[8.2, 9.0]]

Comment: `df['C'] = [ [[x[0]], [x[1]]] for x in df[['A', 'B']].values.tolist()]` ?
did this work?
Answer: Use the agg i.e
df['C'] = df[['A','B']].agg(lambda x : ([x[0]],[x[1]]),1).apply(list)

   index    A    B               C
0      0  2.3  6.4  [[2.3], [6.4]]
1      1  5.6  2.8  [[5.6], [2.8]]
2      2  8.2  9.0  [[8.2], [9.0]]

In case you need speed use numpy's expand dims i.e
df['C'] = (df[['A','B']].values[:,:,None]).tolist()

df['C']
0    [[2.3], [6.4]]
1    [[5.6], [2.8]]
2    [[8.2], [9.0]]
Name: C, dtype: object

Comment: Nice to see u back :-)
Comment: @KOB no probs. If the solution was helpful you can vote for it.
Answer: df['C']=df[['A','B']].apply(lambda x : [[x[0]],[x[1]]],1).values.tolist()
df
Out[1051]:
   A  B           C
0  1  4  [[1], [4]]
1  2  5  [[2], [5]]
2  3  6  [[3], [6]]


Not able to overwrite old value in Drupal.settings

Question: I am facing Problems in overwriting old value in Drupal.setting.
Problem Statement
I am passing FID value from PHP to JS. At first it is getting correct value in JS.
Ex. When PHP is passing FID=59 to JS for first time then it is correctly received by JS.
Next time when it is passing FID=60 then it is showing FID=59 in JS always.
Drupal.setting is not refreshing with current value.
My sample php code is like this
<code> function TraverseSnap($nextFID)
 {
    $thm_slideshowimg .= '<input type="image" src="nxt.jpg" class="clsNextSnap" width="25" height="25">';

    $nxtSnapSetting = array(
            'fid' => $nextFID,
     );

    drupal_add_js(array('nxtSnp' => $nxtSnapSetting), 'setting');
 }
</code>
JS Code
<code>(function($) {
 Drupal.behaviors.VidsImgsContent=
 {
      attach: function (context, settings)
      {
          var fileId = Drupal.settings.nxtSnp.fid;
          var dataNextSnap = 'fid=' +fileId;
          $.ajax({
                  url: 'nextsnap',
                  type: 'POST',
                  async : false,
                  dataType : 'json',
                  data : dataNextSnap,
                  success : updateMainNextDiv,
                  error : function(jqXHR, textStatus, errorThrown){alert("Some Error occured");},
                  beforeSend: function()
                  {
                  },
               });
               return false;
       }
  }
  })(jQuery);
</code>
My hook_menu call
<code>$items['nextsnap']=array(
   'title'=>t(''),
   'type'=> MENU_CALLBACK,
   'page callback' => 'nxt_snap_post',
   'access arguments' => array('access content'),
);
</code>
Page Callback
<code>function nxt_snap_post()
{
    $fid=$_POST['fid'];

    $htmlResult = TraverseSnaps(60); //An arbitary Value for sake of this example
    drupal_json_output($htmlResult);
    exit();
}
</code>
So I believe when the button is clicked next time it will take FID=60 in JS but it is taking old value instead.
Please help here as my crucial logic depends on this.
Comment: The missing " in your js at line error : function(jqXHR, textStatus, errorThrown){alert("Some Error occured);}, is just a copy and paste error?
Comment: Yes. You are right. It is just a copy and paste error. I have edited the code now.
Answer: I think you can not do it this way. Data Drupal.settings only run in first load. This data store in tag head of page.

When you call ajax your function was execute, but tag head don't reaload. So Data.settings don't change.
Comment: Then what will be the best way to achieve this functionality?
Comment: do you want create slider with ajax, after click button ajax will loaded and you get file fid for next slider? (sorry my english is not good). You can say what do you want?
Comment: Yes. That is correct. I have custom buttons which actually move to next FID on clicking and also load comments regarding those images. Can you help me out? I don't want to try any contributed modules. I want to write PHP & JS code for this.
Comment: I need more info. I want to know structure of image and comment of images. is It content type and comment, and structure of your html slider. Final, i think better you should create new question, say what do you want your slide, and structure of it. I and others will help you better.
Comment: As per your suggestion I have create new question related to this. Please see the link:- [link](
http://drupal.stackexchange.com/questions/165086/how-to-create-custom-slider-using-drupalphp-jquery)
Comment: I already see your new question. And see that your problem is just how can derive the value of the next FID. Why do not stores this value into and updated after each button click. Implementing call ajax, ajax complete when you update 2 values in 2 button.
Comment: I didn't get the main point of your comment.Can you please tell again where do you want to store next FID value into?
Comment: This is my way http://tinypic.com/r/zu4n4w/8
Comment: I get your point. But will it be secure to pass sensitive data like FID as part of HTML control? This is because embedding FID in HTML control means anyone can press F12 on firefox browser and get the value by inspecting the button.
Comment: if you data is private, you need process it in server. You can save data to variable global $use. And return html after ajax done.
Comment: Can you please share code example of such kind to get your concept.


dataformat for multiple lines in bokehjs

Question: I am using just the BokehJS part of Bokeh since i am building a more production oriented system.
Unfortunately it seems that the actual BokehJS part of Bokeh is not documented that much, which makes it difficult to find the needed information, such as how to format data for the bokehJS object.
What I am trying to do is to make a simple line graph, however instead of having just one line i would like to have multiple lines, and the possibility of making a legend describing each line. Its a very basica plot, however i did not find any way to do this in bokehJS.
In order to make a plot with a single line i execute the following javascript:
    Bokeh.Plotting.show(
        Bokeh.Plotting.make_plot({type:'line'}, {x:[1,2],y:[4,5]}, {})
        ,'.mydivcontainer');

How do i alter this so that i can have 5 lines in the same plot as well as a legend, basically similar to this which is written in standard bokeh:
from collections import OrderedDict
    import pandas as pd
AAPL = pd.read_csv("aapl.csv", parse_dates=["Date"])
MSFT = pd.read_csv( "msft.csv", parse_dates=["Date"])
IBM = pd.read_csv( "ibm.csv", parse_dates=["Date"])

xyvalues = OrderedDict(
    AAPL = AAPL[("Date", "Adj Close")],
    MSFT = MSFT[("Date", "Adj Close")],
    IBM  = IBM[("Date", "Adj Close")],
)
df = pd.concat(xyvalues, axis=1, names=["l0", "l1"])

from bokeh.charts import TimeSeries
ts = TimeSeries(
    df, title="timeseries, pd_input",
    filename="stocks_timeseries.html")
ts.legend("top_left").show()

(Taken from the release note: http://continuum.io/blog/bokeh-0.6 )
Thank you very much in advance for your help
Answer: it's definitely true that the developing and documenting the JS interface has lagged behind the other interfaces (python mostly, but also scala and Julia and soon R). We plan to improve this, but as you can imagine there are lots of competing priorities.
But I will mention another option, in case it is useful to you. It is possible to create the plot JS from python, and then use the JS directly. That is you only use python to set things up, then you can throw the python away. You can use functions in bokeh.embed to turn your python plot object graph into JS that you can embed however you like.
With more recent version of Bokeh, you can also easily grab ahold of the plot objects (for instance data sources) to update the plot directly from JS. See, for instance:
https://github.com/bokeh/bokeh/blob/master/examples/embed/spectrogram/spectrogram.coffee#L187
Comment: hi,
sorry for the late reply.
First of all - i didnt get to say thank you for bokeh. Its a great product, and I have not been able to find anything like it on the market - so bokeh is really good and I believe there is a gap in the available plotting tools out there which bokeh may cover.

I have been looking at creating a python-bokeh library as you suggested, but i am not much of a coffee script guy... and would be really great to actually do this in bokehJS.

So is there no way to change the js code above to contain multiple lines in any way? eg. pass multiple lines in the data array?
Answer: ahhh now i have seemed to figure this one out.
To enable multiple lines, it seems i can do like this:

Bokeh.Plotting.show(
      Bokeh.Plotting.make_plot([{type:'line'},{type:'line'}], [{x:[1,2],y:[4,5]},{x:[1,4],y:[2,5]}], {})
      ,'.mydivcontainer');

Great :)
Comment: FYI here is a GH issue you can track (or add to the discussion) about the JS interface: https://github.com/bokeh/bokeh/issues/1515


$\mathbb E[|X_n|^r]<\infty$ and $\mathbb E[|X_n|^r]\to \mathbb E[|X|^r]$ as $n\to \infty$

Question:
Let $\{X_n\}\xrightarrow{d}X$ and for some $p>0$, we have
$$\sup_{n\ge 1} \mathbb E[|X_n|^p]<\infty$$
Show that for any $r\in (0,p)$, we have
a. $\mathbb E[|X|^r]<\infty$
b. $\mathbb E[|X_n|^r]\to \mathbb E[|X|^r]$ as $n\to \infty$
[Note: You must not use (b) to prove (a)]

I am pretty sure we need to use Skorohod Representation Theorem. Maybe, we also need to use the fact that
$$\{X_n\}\xrightarrow{d}X \iff \mathbb E[f(X_n)]\to \mathbb E[f(X)]\;\;\forall f\in \mathcal C_B(\mathbb R)$$
but I can't figure out how to do that.
The actual question had $\mathbb E[|X|^r]<\infty$ instead of $\mathbb E[|X_n|^r]<\infty$ which was wrongly written in the first question. So, now I have doubts in part (a) as well. The $1\le r\le p$ case can be tackled using some theorems done in class, but I can't find any argument for the $0<r\le 1$ case.
Comment: Are you interested only in part b?  It seems like it, but you should make that clear either way.
Comment: For (a), see https://stats.stackexchange.com/questions/244202.
Comment: Cts mapping + skorohod + uniform integrability convergence theorem
Comment: Use Fatou's lemma and Skorohod theorem for part (a).
Comment: @jbowman yes, I'll place an edit
Comment: @whuber that was a nice answer! Do you have any insights on part (b)?
Comment: @Zhanxiong please elaborate how
Comment: @SayanDutta See my updated answer. There is no need to distinguish $r \in (0, 1]$ or $[1, p)$.
Answer: Part (a)
I assume you already know how to prove $\sup_n E[|X_n|^r] < \infty$.
By continuous mapping theorem, $X_n \overset{d}{\to} X$ implies $|X_n|^r \overset{d}{\to} |X|^r$. It then follows by Skorohod's representation theorem that there exist $\{Y_n\}$ and $Y$ such that $Y_n \overset{d}{=} |X_n|^r$, $Y \overset{d}{=} |X|^r$, and $Y_n \to Y$ with probability $1$. Therefore, by Fatou's lemma,
\begin{align}
E[|X|^r] = E[Y] = E[\liminf_n Y_n] \leq \liminf_n E[Y_n] =
\liminf_n E[|X_n|^r] \leq \sup_n E[|X_n|^r] < \infty.
\end{align}
Part (b)
By continuous mapping theorem, $X_n \overset{d}{\to} X$ implies $|X_n|^r \overset{d}{\to} |X|^r$.  In view of Theorem 25.12$^\dagger$ (the proof to this theorem indeed uses Skorohod's theorem) in Probability and Measure by Patrick Billingsley, to show $E[|X_n|^r] \to E[|X|^r]$, it suffices to prove $\{|X_n|^r\}$ is uniformly integrable.  Indeed, suppose by condition $\sup_n E[|X_n|^p] = M < \infty$, then for any $\alpha > 0$, we have
\begin{align}
& E[|X_n|^rI_{[|X_n|^r \geq \alpha]}] \\
=& E\left[|X_n|^p\frac{1}{|X_n|^{p - r}}I_{[|X_n| \geq \alpha^{1/r}]}\right] \\
\leq & \frac{1}{\alpha^{(p - r)/r}}E[|X_n|^p]
\leq \frac{1}{\alpha^{(p - r)/r}}M \to 0
\end{align}
as $\alpha \to \infty$.  This completes the proof.

$\dagger$

Theorem 25.12.  If $X_n \overset{d}{\to} X$ and the $X_n$ are uniformly integrable, then $X$ is integrable and
\begin{align}
E[X_n] \to E[X].
\end{align}
Comment: Excellent answer (+1)!


Significance of Difference between Two Extremes (Maxima/Minima)

Question: I am comparing two different populations (with different sizes) of models, for each individual I have a score. I'd like to compare not only the difference of the two means but the maximum value. I've learnt in the past to perform significance test when comparing averages between groups but this is the first time that I'm trying to do the same with an extreme value (maximum in this case). To summarise, my question is: what is the right way to know whether the difference  $max(P_1) - max(P_2)$ is significant?
Comment: Also note that extreme values are the least robust statistic. They are maximally sensitive to outliers. [wikipedia](http://en.wikipedia.org/wiki/Sample_maximum_and_minimum). First of think of why you want to use them. A much robust alternative are deciles or some more general quantile of interest that can be as close to the extrema.
Comment: another derivative will tell you if the difference is linear or at least polynomial
Comment: populations ... of models? What kind of models are these?
Comment: Do you know anything about the distributions of the original observations?
Comment: Inside the population there are all the possible combinations among two sets of models. But it's not important, at least for me now, this is a general question.
Comment: @Matteo De Felice:  You could edit the question, to add that changed question about quantiles.
Answer: There may be a little confusion about the intricacies of inferential statistics and significance testing.
Inferential statistics do not check whether averages of samples differ between groups. We test whether the underlying distributions are different, at least as far as we can tell based on the samples.
It just happens that for the normal distribution, the sample mean is a good estimator of one of the distributional parameters. If we assume, e.g., equal variances, then we can use the sample means to make inferences
However, the normal distribution (which I assume you are assuming) is unbounded. It has no maximum. Therefore, it really makes no sense in an inferential framework to ask whether maxima of samples from two groups differ differ significantly as maxima.
You could in principle work along the lines of "the maximum of this sample of size 100 is 1000, and the maximum of that sample of size 200 is 0.3, so it is unlikely they come from the same normal distribution". This page lists properties of maxima of finite samples of the normal distribution, e.g., the standard deviation of the sample maximum. However, this does not address the problem of estimating the parameters of the normal distribution from the data. (Note that bootstrapping will not work for maxima without additional effort.)
Comment: Ok, what if instead of comparing the maxima I'll try to compare their quantiles (e.g. 0.99)?


Quadratic terms in logistic regression

Question: I am looking at the results of a logistic regression model (i dont have the data) and the person who has developed the model has included quadratic terms in the model.
I understand the use of such polynomial terms in a linear model where one can look at the relationship between the response and the predictor. But in case of a binary outcome, is there a way to identify such a trend before hand i.e. without including it in the model and then checking if the variable is significant or not?
Comment: @DWin I believe the OP means a quadratic expansion of a continuous covariate in logistic regression. In others, if $x$ is a predictor of $y$, include $x$ and $x^2$ as two covariates.
Comment: There is no way to separately interpret a coefficient for a "quadratic term". It is essentially an interaction and requires that you make a prediction across the range of possible values using all terms that contribute to the variation of the outcome. The notion of a quadratic terms for a binary variable isn't making a lot of sense to me.
Comment: That is what I interpreted the question as requesting. But I hope you agree that it remains unclear what a "quadratic trend" might mean. There's no way to decide whether a positive sign for a squared term actually means a concave upward "trend" in the region of domain of interest. My main point is that one needs to _predict_ using the (Intercep, linear and quadratic coefficients together over the range of interest. Attempting to assign meaning to individual coefficients is foolish.
Comment: @DWin:  I agree the word "trend" is a little misleading, but like Ciff has mentioned what i meant was how does one figure out whether to add a x^2 or X^n predictor in the model if we can't get a good feel of the relationship between the predictor and outcome. I get your point of trial and check but my doubt is what would prompt someone to try a polynomial covariate?
Answer: During EDA, you can take the (continuous) predictor and discretize it by either creating equal-sized or equal-spaced bins. Then you can plot the event rates across all bins to visually detect a linear or quadratic relationship (if it exists). E.g., an inverted U-shaped curve would suggest the presence of a quadratic relationship. Another way to create such bins is by using CHAID (or other) decision tree algorithm to split your sample into statistically-derived bins.
Answer: After building a model based on general linear model (as you would typically be doing when you have a binary outcome), you have several methods available for checking for violations of the assumptions supporting statistical validity. The assumption that would be violated when the prediction relationship was polynomial is linearity of residuals (or equivalently the prediction-vs-predictor on the fitted scale, logistic in the case of binary outcomes). The details will vary depending on your computing platform, but you should be thinking of residual (or fitted values) versus predictor plots. The test for needing a polynomial would be "eyeball"-driven. If you get a "smile" or a "frown" then a squared term might be appropriate. If you get a minus-plus-minus-plus sort of pattern then a higher order polynomial might be needed. You should be thinking about the underlying scientific implications during this process of model building. Cubic polynomials should have a higher degree of skepticism. You need to balance the degree of fit against complexity. The other approach is to use regression splines which allow an automatic penalty to be imposed. Frank Harrell's "Regression Modeling Strategies" has many worked examples using the S/R platform.

Quadratic terms in logistic regression


Upload a file using FS and NodeJS

Question: I get an error when I try to upload a file, it works in local but it doesn't work on my remote server...
My error :
    [sbaf.fr.3005-53 (out) 2014-03-05T20:19:59] { [Error: ENOENT, rename '/tmp/1e426309d298d9ab1d099e1017584058']
[sbaf.fr.3005-53 (out) 2014-03-05T20:19:59]   errno: 34,
[sbaf.fr.3005-53 (out) 2014-03-05T20:19:59]   code: 'ENOENT',
[sbaf.fr.3005-53 (out) 2014-03-05T20:19:59]   path: '/tmp/1e426309d298d9ab1d099e1017584058' }

My controller :
photoDAL.prototype.save = function(photo, file, callback) {
    photo.file = file.name;
    var photo = dbContext.photo.build(photo);

    var file_tmp = file.path;
    var file_name = file.name;
    var file_type = file.type;
    var file = './public/images/photo/'+file_name;

    fs.rename(file_tmp, file, function(err){
        if( err ) console.log(err);
    });

    photo.save().success(function(photo) {
        callback(photo);
    }).error(function(error) {
        callback({message: error});
    });
};

EDIT #1 :
Screenshots of my ExpressJS app...
Screenshot 1 : http://glui.me/?i=eweyq4ovennej50/2014-03-05_at_20.34_2x.png/
Screenshot 2 : http://glui.me/?i=1n2cjv57jd2fmwq/2014-03-05_at_20.33_2x.png/
EDIT #2 :
My code :
console.log(process.cwd());
console.log(__dirname);

The console :
[sbaf.fr.3005-71 (out) 2014-03-05T21:55:48] /home/anthoc/apps
[sbaf.fr.3005-71 (out) 2014-03-05T21:55:48] /home/anthoc/apps/sbaf.fr/app/dal

Comment: Does public `./public/images/photo/` folder exists ?
Comment: Yes, see edit #1 above...
Answer: So this: var file = './public/images/photo/'+file_name; is a relative path based on process.cwd(). Presumably if your server process was started with your app repo root as the cwd, all should be well, but probably that is not the case. It's more robust to not rely on the cwd but use __dirname and construct paths relative to the location of the current javascript file. So give that a try and see if it fixes it. You can confirm one way or another with: console.log(process.cwd()) in your controller module to debug this.
Answer: You can try  using __dirname. Here is how I would do it:
photoDAL.prototype.save = function(photo, file, callback) {
photo.file = file.name;
var photo = dbContext.photo.build(photo);

var file_tmp = file.path;
var file_name = file.name;
var file_type = file.type;
var file = __dirname + '/public/images/photo/'+file_name;

fs.rename(file_tmp, file, function(err){
    if( err ) console.log(err);
});

photo.save().success(function(photo) {
    callback(photo);
}).error(function(error) {
    callback({message: error});
});

};
Now, if you're code is in a folder and your target is in the parent, like me:
-root
  - public
     -photos
  -server
     -upload.js

You can add a function to the string proto.
String.prototype.getParent = function () {
        // Be cross-platform
    var replaced = this.replace(new RegExp("\\\\", "g"), '/');
    var index = replaced.lastIndexOf('/');
    return replaced.substring(0, index);
};

Now call __dirname.getParent() as many time as needed (__dirname.getParent().getParent()...).
Comment: Ok but is there a solution working in local and also on a remote server?
Comment: I don't really understand. This will work on heroku or nodejitsu.
Answer: You can use formidable module. easy to use
https://www.npmjs.org/package/formidable

Upload a file using FS and NodeJS


Sequência de datas a partir de um intervalo no R

Question: Tenho uma base com ID, data de inicio e data fim.
Queria gerar uma base que cada linha seja um dia dentro do intervalo com uma coluna do ID.
Base:
 base <- data.frame( ID = c("A","B","C"),
                    data_inicio = c("2010-01-05", "2010-01-07", "2020-11-30"),
                    data_fim = c("2010-01-07", "2010-01-09", "2020-12-03"))

A saída seria:

ID     DATA
A     2010-01-05
A     2020-01-06
A     2020-01-07
B     2020-01-07
B     2020-01-08

etc...
Answer: Usando o dplyr e o lubridate você pode utilizar o rowwise que te possibilita trabalhar linha a linha:
library(dplyr)
library(lubridate)

nova_base <- base %>%
 rowwise() %>%
 do(data.frame(ID = .$ID, DATA = seq(ymd(.$data_inicio), ymd(.$data_fim), by = '1 day')))

A função seq() pega um valor inicial e um final, calculando assim os valores intermediários.
Saída:
  ID       DATA
  A     2010-01-05
  A     2010-01-06
  A     2010-01-07
  B     2010-01-07
  B     2010-01-08
  B     2010-01-09
  C     2020-11-30
  C     2020-12-01
  C     2020-12-02
  C     2020-12-03

Comment: Preciso de uma linha para cada data. A sugestão que colocou me da a diferença de dias entre as variáveis data_inicio e data_fim.
Comment: Fala Rubens, tudo bom? Adicionei uma nova resposta. Realmente eu não havia olhado a saída que você esperava. Abraço!!
Answer: Semelhante à resposta do @lmonferrari, mas usando data.table:
library(data.table)

setDT(base)  # estabelece como data.table

> base[, .(DATA = seq(as.IDate(data_inicio), as.IDate(data_fim), by = "1 day")), ID]
    ID       DATA
 1:  A 2010-01-05
 2:  A 2010-01-06
 3:  A 2010-01-07
 4:  B 2010-01-07
 5:  B 2010-01-08
 6:  B 2010-01-09
 7:  C 2020-11-30
 8:  C 2020-12-01
 9:  C 2020-12-02
10:  C 2020-12-03

Alternativamente, converter primeiro as colunas relevantes:
base[, 2:3 := lapply(.SD, as.IDate), .SDcols = 2:3]

base[, .(DATA = seq(data_inicio, data_fim, by = "1 day")), ID]

A classe IDateTime faz parte do pacote data.table; seu uso é similar aos métodos usados pelo lubridate.
Veja a ajuda para data.table::fread para ver como carregar seus dados diretamente como data.table e com as colunas relevantes como IDate.
Answer: Uma solução em R base poderá ser a seguinte.
saida <- apply(base, 1, function(x) {
  x <- unname(x)
  cbind.data.frame(
    ID = x[1],
    DATA = seq(as.Date(x[2]), as.Date(x[3]), by = "1 day")
  )
})
saida <- do.call(rbind, saida)

saida
#   ID       DATA
#1   A 2010-01-05
#2   A 2010-01-06
#3   A 2010-01-07
#4   B 2010-01-07
#5   B 2010-01-08
#6   B 2010-01-09
#7   C 2020-11-30
#8   C 2020-12-01
#9   C 2020-12-02
#10  C 2020-12-03


Rails app will not load file from lib directory

Question: I use a mixin method called to_bool for class String in my Rails applications:
class String
  def to_bool
    return true if self == true || self =~ (/(true|t|yes|y|1|byapplication)$/i)
    return false if self == false || self.blank? || self =~ (/(false|f|no|n|0|disabled)$/i)
    raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
  end
end

I just put it in a file called <app_root>/lib/string.rb. Works fine in one application.
In a new application I'm working on, it refuses to work. I can make it work in the console if I load it first, but it absolutely will not load on its own. It's in the lib directory!!! And I have this in my application.rb:
# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(
  #{config.root}/lib
  #{config.root}/app/workers
  #{config.root}/app/inputs
  #{config.root}/lib/mixins
  #{config.root}/lib/classes
  #{config.root}/lib/database
  #{config.root}/app/api
  #{config.root}/app/datatables)

It's right there at the top. No matter what I do, I get NoMethodError in the console until I:
load 'string.rb'

What simple thing am I overlooking?
Answer: The way autoload works is that if a constant is not already defined then the autoload_paths are checked for a file matching that constant, and if it exists then that file is loaded.
String (obviously :) already exists, so the autoload of your lib/string.rb will never be triggered.
You should put it into config/initializers/core_ext.rb or something like that.
Comment: You are both a gentleman and a scholar. I did exactly as you said and it worked immediately.


Is there any way to show that $\sum_{n=2}^{\infty}\frac{1}{n^2-1}$ converges without integral test?

Question: In my real analysis class, we are trying to show that
$$\left(\frac{2 \times 2}{1 \times 3}\right)\left(\frac{4 \times 4}{3 \times 5}\right) \left(\frac{6 \times 6}{5 \times 7}\right)...$$
converges. I re-expressed this into the infinite product $(1 - \frac{1}{n^{2}-1})$.  If I can show that the series of $\frac{1}{n^2 -1}$ converges then the proof will be complete, however we have yet to develop integration, and as a result is there any way to prove that this series goes to $0$.
Other methods to show that this product converges are also well appreciated!
Edit: Thanks so much! I completely forgot about the limit comparison test, been a while since I took calculus.
Comment: That sum doesn't exist from 1 to infinity.
Comment: Has your question been answered? If yes, you should accept an answer.
Answer: Since$$\lim_{n\to\infty}\frac{\frac1{n(n-1)}}{\frac1{n^2-1}}=1\neq0,$$and since the series$$\sum_{n=2}^\infty\frac1{n(n-1)}\left(=\sum_{n=2}^\infty\frac1{n-1}-\frac1n\right)$$converges, the series$$\sum_{n=2}^\infty\frac1{n^2-1}$$converges too.
Comment: Actually, for $n>1$, $n^2-1>n(n-1)$, so the limit is not even necessary.
Comment: Indeed. Nice observation.
Answer: You can even calculate the exact value of $\sum_{n=2}^\infty \frac{1}{n^2 - 1}$.  Namely,
$$\frac{1}{n^2 - 1} = \frac{1}{2} \left( \frac{1}{n-1} - \frac{1}{n+1} \right) = \frac{1}{2} \left( \frac{1}{n-1} + \frac{1}{n} \right) - \frac{1}{2} \left( \frac{1}{n} + \frac{1}{n+1} \right).$$
Therefore, the series is a telescoping series, and
$$\sum_{n=2}^N \frac{1}{n^2-1} = \frac{3}{4} - \frac{1}{2} \left( \frac{1}{N} + \frac{1}{N+1} \right).$$
Taking the limit as $N \to \infty$, it is then easy to see that $\sum_{n=2}^\infty \frac{1}{n^2-1} = \frac{3}{4}$.
Answer: We have $\frac{1}{n^2-1} \leq \frac{2}{n^2}$ for $n\geq2$. This can easily be verified. It follows that if $\sum_{n=1}^\infty\frac{1}{n^2}$ converges, then so does $\sum_{n=2}^\infty\frac{1}{n^2-1}$.
We can write
$$\sum\limits_{n=1}^\infty \frac{1}{n^2} = 1 + \frac{1}{2^2} + \frac{1}{3^2} + \frac{1}{4^2} + \cdots$$
Also, we have
\begin{eqnarray*}
  1 &\leq& 1 \\
  \frac{1}{2^2} + \frac{1}{3^2} &\leq& \frac{1}{2^2} + \frac{1}{2^2} = \frac{1}{2} \\
  \frac{1}{4^2} + \frac{1}{5^2} + \frac{1}{6^2} + \frac{1}{7^2} &\leq& \frac{4}{4^2} = \frac{1}{4}
\end{eqnarray*}
and so on.
Adding up all the terms on the left and the right we get
$$\sum\limits_{n=1}^\infty\frac{1}{n^2} \leq 1 + \frac{1}{2} + \frac{1}{4} + \frac{1}{8} + \cdots = \sum\limits_{n=0}^\infty \left(\frac{1}{2}\right)^n$$
This is a geometric series and it converges. Hence, $\sum_{n=1}^\infty\frac{1}{n^2}$ converges and, by implication, so does the starting series.
Comment: Alternatively use $\sum_{n=1}^\infty\frac{1}{n^2} = π^2/6$.
Answer: You can see that it increases.
On the other hand, when you group them so that a typical factor is $2\times4\over3\times3$, that sequence decreases.
So two sequences, one increasing, one decreasing.  They are within $n\over n+1$ of each other, so yours is increasing and bounded above.


Singing the third instead of the root/melody

Question: I play the guitar and sing for fun. I have some understanding of music theory but not much. I've been told a couple of times that I was singing the third. I also noticed that when I play around transposing chords (usually with a capo) to find the best vocal range for me I tend to sing the third (or some other note instead of the root/melody?). I wonder why I do that. I usually try to find a key that's good for me to sing, but then sometimes I realize I wasn't singing the right notes, nonetheless I think it sounds good.
Is it wrong? Why do I tend to do that? Any suggestions of how to find the right melody line on a different key?
As a reference, I'm a baritone.
Answer: It depends quite a lot on the context:

The third above a melody is two notes above it in the scale, it could just be you're not completely clear on where the melody goes next.
If you're singing with someone, the third is quite a good place to be to find harmonies.

Remember, too, that a key is not a range of pitches, it's a selection of notes. Which key a piece is in doesn't actually effect which pitches you're singing unless you're changing the key, in which case you would sing it so many steps up or down from the original melody.
I do find that changing key to make a song fit your voice better is a bit of a trap, try singing along to a record to get the shape of the melody into your head before making changes to it.
Answer: Sounds like you're singing a harmony. But - to do that, there is usually a voice already singing the main tune. Harmonies often work in thirds - think Everly Brothers, et al.It's not wrong, but something inside is making you sing another part apart from the main tune.Some people do this naturally, others work for ages to be able to do it.Not all tunes lend themselves to this phenomenon, but maybe the tunes you do it on do.You may have a small tessitura, which governs the range of singable notes, and it just happens that the ones you sing are contained within this.Sing along to anything and everything. There are songs that it's not good to sing harmony to, and you'll probably find that you're in unison on those.Particularly, of course, when they're within your range.