Split de palavras com find_if

Question: Como é montada essa string(i, j)? a primeira string inserida no vector é "Robison" e está correta, porém para ter esse resulta o valor de i deveria ser 0 e j = 7, não?
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using std::string;
using std::vector;
using std::cout;
using std::endl;

// predicate (split)
bool space(char c)
    { return isspace(c); }

// preticate (split)
bool not_space(char c)
    { return !isspace(c); }

vector<string> split(const string& str)
{
    typedef string::const_iterator iter;
    vector<string> ret;

    iter i = str.begin();
    while (i != str.end())
    {
        // verifica se o caracter e espaço
        i = find_if(i, str.end(), not_space);

        // procura um novo carácter
        iter j = find_if(i, str.end(), space);

        // faz a cópia dos caracteres encontrados
        if (i != str.end())
            ret.push_back(string(i, j));
        i = j;
    }
    return ret;
}

int main ()
{
    string str = "Robison Aleixo";
    split(str);

    return 0;
}

/*
 *  Basicamente as declarações acima encontram o range de uma string
    armazena as strings em um vetor "removendo" os espaços em branco
 *
 *  Exemplo: str= "Robison Aleixo"
 *
 *  iter i = str.begin();  ::::> i  será = 0
 *  while (i != str.end()) ::::> será executado até o último caracter da string
 *  i = find_if(i, str.end(), not_space);  ::::> i = 6
 *  iter j = find_if(i, str.end(), space); ::::> j = 7
 *  if i != str.end())  ::::> Verifica se não é o último caracter
 *  ret.push_back(string(i, j));  ::::> i = 6 ; j = 7
 *
 *  R o b i s o n   A l e  i  x  o
 *  0 1 2 3 4 5 6 7 8 9 10 11 12 13
 *              ^ ^
 *              i j
 *
 */

Answer:
i deveria ser 0 e j = 7, não?

São de facto esses os valores. Se olharmos para como é obtido o i:
i = find_if(i, str.end(), not_space /*<--not space aqui!*/);

Atente no not_space, logo mal começa apanha o primeiro caractere que não é espaço, e por isso a primeira letra, a da posição 0 e não a posição 6 como tinha indicado.
Logo de seguida apanhamos o espaço a seguir para o j com:
iter j = find_if(i, str.end(), space);

Agora sim utilizamos o predicado de space para apanhar o espaço. Se ainda não chegamos ao fim utilizamos o construtor da string que recebe dois iteradores e inicia a string com o texto entre eles:
string(i, j)

E dado na documentação como:

template <class InputIterator> string (InputIterator first, InputIterator last);
(7) range constructor Copies the sequence of characters in the range
  [first,last), in the same order.

É importante de notar que o ultimo elemento, o last, não é incluído, o que calha bem no algoritmo pois faz com que a string construída não contenha o espaço que estava em j
Comment: Verdade na própria documentação diz: _Returns an iterator to the first element in the range [first,last) for which pred returns true. If no such element is found, the function returns last._

Eu me confundi, estava pensando que ele ia testando o _predicate_ até retornar falso. Obrigado pela ajuda!

Split de palavras com find_if


Causal inference: is it possible to estimate the actual users who did X because of Y?

Question: Suppose we have a dataset with many (~100) features and a binary outcome.  I am interested in not only assessing whether any given feature is causally related to the outcome, but in actually being able to say that users X,Y,and Z got the outcome specifically because of some specific feature(s).  Is this even possible?  If so, could someone please give me some pointers as to go about it (math, codes, packages, etc.)?
Comment: Welcome to CV. There is no math or code to determine causality. You should have an intervention study to make any claims of a causal relationship. The closest you could get is probably estimating an undirected graph of partial correlations, and then using a PC algorithm to determine direction here and there, but this still does not demonstrate causality, that is up to the study design.
Comment: Here is a math pointer: Pearl's work on causality.  https://ftp.cs.ucla.edu/pub/stat_ser/r350.pdf. Page 136 and onward discusses whether and when questions like "did Z cause Y?" can be answered.
Answer: Yes, this is the problem of "probability of causation." It is not a major topic in the causal inference literature, but there has been some written on it. A Google search led me to this CDC page on interpreting probability of causation and this paper (Dawid, Musio, Murtas, 2017) on contemporary perspectives in probability of causation. The concept is used when attempting assign blame or responsibility for an event to some causal actor.


Alternativa para padding-nevative em tabela

Question: Tenho uma tabela, onde preciso diminuir o padding das linhas da mesma.
Se eu pudesse utilizar um padding-bottom: -XXpx; e padding-top: -XXpx; resolveria meu problema. Porém, não é possível utilizar valores negativos, até onde minha pesquisa informou.
Gostaria de saber se realmente não é possível utilizar um valor negativo? Se sim, qual a alternativa para diminuir o espaçamento entre as linhas de uma tabela.
Um pequeno exemplo da tabela seria esse:

 .tblCompleta tr td {
        padding-top: 0px;
        padding-bottom: 0px;
    }

Entidade

1

2

3

4

5

6

7

8

9

10

11

12

Pendência

Observação

                        C.M. Areal

<input checked="checked" data-val="true" data-val-required="O campo bJaneiro é obrigatório." name="[0].EnvioPrestacao.bJaneiro" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bJaneiro" type="hidden" value="false" />

<input checked="checked" data-val="true" data-val-required="O campo bFevereiro é obrigatório." name="[0].EnvioPrestacao.bFevereiro" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bFevereiro" type="hidden" value="false" />

<input checked="checked" data-val="true" data-val-required="O campo bMarco é obrigatório." name="[0].EnvioPrestacao.bMarco" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bMarco" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bAbril é obrigatório." name="[0].EnvioPrestacao.bAbril" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bAbril" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bMaio é obrigatório." name="[0].EnvioPrestacao.bMaio" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bMaio" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bJunho é obrigatório." name="[0].EnvioPrestacao.bJunho" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bJunho" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bJulho é obrigatório." name="[0].EnvioPrestacao.bJulho" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bJulho" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bAgosto é obrigatório." name="[0].EnvioPrestacao.bAgosto" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bAgosto" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bSetembro é obrigatório." name="[0].EnvioPrestacao.bSetembro" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bSetembro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bOutubro é obrigatório." name="[0].EnvioPrestacao.bOutubro" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bOutubro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bNovembro é obrigatório." name="[0].EnvioPrestacao.bNovembro" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bNovembro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bDezembro é obrigatório." name="[0].EnvioPrestacao.bDezembro" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bDezembro" type="hidden" value="false" />

    <select class="form-control" data-val="true" data-val-number="The field TipoPendencia must be a number." name="[0].EnvioPrestacao.TipoPendencia"><option value="1">Cliente</option>
    <option value="2">Produto</option>
    <option selected="selected" value="3">N/A</option>
    </select>

N/A

                        P.M. Guapimirim

<input data-val="true" data-val-required="O campo bJaneiro é obrigatório." name="[1].EnvioPrestacao.bJaneiro" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bJaneiro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bFevereiro é obrigatório." name="[1].EnvioPrestacao.bFevereiro" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bFevereiro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bMarco é obrigatório." name="[1].EnvioPrestacao.bMarco" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bMarco" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bAbril é obrigatório." name="[1].EnvioPrestacao.bAbril" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bAbril" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bMaio é obrigatório." name="[1].EnvioPrestacao.bMaio" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bMaio" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bJunho é obrigatório." name="[1].EnvioPrestacao.bJunho" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bJunho" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bJulho é obrigatório." name="[1].EnvioPrestacao.bJulho" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bJulho" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bAgosto é obrigatório." name="[1].EnvioPrestacao.bAgosto" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bAgosto" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bSetembro é obrigatório." name="[1].EnvioPrestacao.bSetembro" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bSetembro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bOutubro é obrigatório." name="[1].EnvioPrestacao.bOutubro" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bOutubro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bNovembro é obrigatório." name="[1].EnvioPrestacao.bNovembro" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bNovembro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bDezembro é obrigatório." name="[1].EnvioPrestacao.bDezembro" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bDezembro" type="hidden" value="false" />

    <select class="form-control" data-val="true" data-val-number="The field TipoPendencia must be a number." name="[1].EnvioPrestacao.TipoPendencia"><option selected="selected" value="1">Cliente</option>
    <option value="2">Produto</option>
    <option value="3">Serviço</option>
    </select>

N/A....

                        C.M. Guapimirim

<input data-val="true" data-val-required="O campo bJaneiro é obrigatório." name="[2].EnvioPrestacao.bJaneiro" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bJaneiro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bFevereiro é obrigatório." name="[2].EnvioPrestacao.bFevereiro" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bFevereiro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bMarco é obrigatório." name="[2].EnvioPrestacao.bMarco" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bMarco" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bAbril é obrigatório." name="[2].EnvioPrestacao.bAbril" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bAbril" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bMaio é obrigatório." name="[2].EnvioPrestacao.bMaio" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bMaio" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bJunho é obrigatório." name="[2].EnvioPrestacao.bJunho" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bJunho" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bJulho é obrigatório." name="[2].EnvioPrestacao.bJulho" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bJulho" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bAgosto é obrigatório." name="[2].EnvioPrestacao.bAgosto" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bAgosto" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bSetembro é obrigatório." name="[2].EnvioPrestacao.bSetembro" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bSetembro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bOutubro é obrigatório." name="[2].EnvioPrestacao.bOutubro" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bOutubro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bNovembro é obrigatório." name="[2].EnvioPrestacao.bNovembro" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bNovembro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bDezembro é obrigatório." name="[2].EnvioPrestacao.bDezembro" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bDezembro" type="hidden" value="false" />

    <select class="form-control" data-val="true" data-val-number="The field TipoPendencia must be a number." name="[2].EnvioPrestacao.TipoPendencia"><option value="1">Cliente</option>
    <option value="2">Produto</option>
    <option selected="selected" value="3">Serviço</option>
    </select>

N/A ....

                        Inst. Previdência Iguaba Grande

<input checked="checked" data-val="true" data-val-required="O campo bJaneiro é obrigatório." name="[3].EnvioPrestacao.bJaneiro" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bJaneiro" type="hidden" value="false" />

<input checked="checked" data-val="true" data-val-required="O campo bFevereiro é obrigatório." name="[3].EnvioPrestacao.bFevereiro" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bFevereiro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bMarco é obrigatório." name="[3].EnvioPrestacao.bMarco" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bMarco" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bAbril é obrigatório." name="[3].EnvioPrestacao.bAbril" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bAbril" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bMaio é obrigatório." name="[3].EnvioPrestacao.bMaio" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bMaio" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bJunho é obrigatório." name="[3].EnvioPrestacao.bJunho" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bJunho" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bJulho é obrigatório." name="[3].EnvioPrestacao.bJulho" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bJulho" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bAgosto é obrigatório." name="[3].EnvioPrestacao.bAgosto" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bAgosto" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bSetembro é obrigatório." name="[3].EnvioPrestacao.bSetembro" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bSetembro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bOutubro é obrigatório." name="[3].EnvioPrestacao.bOutubro" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bOutubro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bNovembro é obrigatório." name="[3].EnvioPrestacao.bNovembro" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bNovembro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bDezembro é obrigatório." name="[3].EnvioPrestacao.bDezembro" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bDezembro" type="hidden" value="false" />

    <select class="form-control" data-val="true" data-val-number="The field TipoPendencia must be a number." name="[3].EnvioPrestacao.TipoPendencia"><option value="1">Cliente</option>
    <option value="2">Produto</option>
    <option selected="selected" value="3">Serviço</option>
    </select>

N/A

Resumindo: Como diminuir o espaço entre a texto e as margens da linha em uma tabela?
Segue um pequeno exemplo, porém que ilustra bem o que estou tentando dizer.
Exemplo JSFiddle
Answer: O que você precisa zerar os elementos herdeiros dentro das tags.
Aconselho você estudar mais CSS isso são coisas básicas.
Veja o exemplo abaixo:

tr,
td,
p {
  padding: 0 !important;
  margin: 0 !important;
  border: 0 !important;
}
select {
  height: 20px !important;
}

Entidade

1

2

3

4

5

6

7

8

9

10

11

12

Pendência

Observação

                        C.M. Areal

<input checked="checked" data-val="true" data-val-required="O campo bJaneiro é obrigatório." name="[0].EnvioPrestacao.bJaneiro" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bJaneiro" type="hidden" value="false" />

<input checked="checked" data-val="true" data-val-required="O campo bFevereiro é obrigatório." name="[0].EnvioPrestacao.bFevereiro" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bFevereiro" type="hidden" value="false" />

<input checked="checked" data-val="true" data-val-required="O campo bMarco é obrigatório." name="[0].EnvioPrestacao.bMarco" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bMarco" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bAbril é obrigatório." name="[0].EnvioPrestacao.bAbril" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bAbril" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bMaio é obrigatório." name="[0].EnvioPrestacao.bMaio" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bMaio" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bJunho é obrigatório." name="[0].EnvioPrestacao.bJunho" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bJunho" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bJulho é obrigatório." name="[0].EnvioPrestacao.bJulho" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bJulho" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bAgosto é obrigatório." name="[0].EnvioPrestacao.bAgosto" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bAgosto" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bSetembro é obrigatório." name="[0].EnvioPrestacao.bSetembro" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bSetembro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bOutubro é obrigatório." name="[0].EnvioPrestacao.bOutubro" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bOutubro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bNovembro é obrigatório." name="[0].EnvioPrestacao.bNovembro" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bNovembro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bDezembro é obrigatório." name="[0].EnvioPrestacao.bDezembro" type="checkbox" value="true" /><input name="[0].EnvioPrestacao.bDezembro" type="hidden" value="false" />

        <select class="form-control" data-val="true" data-val-number="The field TipoPendencia must be a number." name="[0].EnvioPrestacao.TipoPendencia">
          <option value="1">Cliente</option>
          <option value="2">Produto</option>
          <option selected="selected" value="3">N/A</option>
        </select>

N/A

                        P.M. Guapimirim

<input data-val="true" data-val-required="O campo bJaneiro é obrigatório." name="[1].EnvioPrestacao.bJaneiro" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bJaneiro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bFevereiro é obrigatório." name="[1].EnvioPrestacao.bFevereiro" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bFevereiro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bMarco é obrigatório." name="[1].EnvioPrestacao.bMarco" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bMarco" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bAbril é obrigatório." name="[1].EnvioPrestacao.bAbril" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bAbril" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bMaio é obrigatório." name="[1].EnvioPrestacao.bMaio" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bMaio" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bJunho é obrigatório." name="[1].EnvioPrestacao.bJunho" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bJunho" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bJulho é obrigatório." name="[1].EnvioPrestacao.bJulho" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bJulho" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bAgosto é obrigatório." name="[1].EnvioPrestacao.bAgosto" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bAgosto" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bSetembro é obrigatório." name="[1].EnvioPrestacao.bSetembro" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bSetembro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bOutubro é obrigatório." name="[1].EnvioPrestacao.bOutubro" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bOutubro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bNovembro é obrigatório." name="[1].EnvioPrestacao.bNovembro" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bNovembro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bDezembro é obrigatório." name="[1].EnvioPrestacao.bDezembro" type="checkbox" value="true" /><input name="[1].EnvioPrestacao.bDezembro" type="hidden" value="false" />

        <select class="form-control" data-val="true" data-val-number="The field TipoPendencia must be a number." name="[1].EnvioPrestacao.TipoPendencia">
          <option selected="selected" value="1">Cliente</option>
          <option value="2">Produto</option>
          <option value="3">Serviço</option>
        </select>

N/A....

                        C.M. Guapimirim

<input data-val="true" data-val-required="O campo bJaneiro é obrigatório." name="[2].EnvioPrestacao.bJaneiro" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bJaneiro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bFevereiro é obrigatório." name="[2].EnvioPrestacao.bFevereiro" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bFevereiro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bMarco é obrigatório." name="[2].EnvioPrestacao.bMarco" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bMarco" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bAbril é obrigatório." name="[2].EnvioPrestacao.bAbril" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bAbril" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bMaio é obrigatório." name="[2].EnvioPrestacao.bMaio" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bMaio" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bJunho é obrigatório." name="[2].EnvioPrestacao.bJunho" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bJunho" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bJulho é obrigatório." name="[2].EnvioPrestacao.bJulho" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bJulho" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bAgosto é obrigatório." name="[2].EnvioPrestacao.bAgosto" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bAgosto" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bSetembro é obrigatório." name="[2].EnvioPrestacao.bSetembro" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bSetembro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bOutubro é obrigatório." name="[2].EnvioPrestacao.bOutubro" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bOutubro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bNovembro é obrigatório." name="[2].EnvioPrestacao.bNovembro" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bNovembro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bDezembro é obrigatório." name="[2].EnvioPrestacao.bDezembro" type="checkbox" value="true" /><input name="[2].EnvioPrestacao.bDezembro" type="hidden" value="false" />

        <select class="form-control" data-val="true" data-val-number="The field TipoPendencia must be a number." name="[2].EnvioPrestacao.TipoPendencia">
          <option value="1">Cliente</option>
          <option value="2">Produto</option>
          <option selected="selected" value="3">Serviço</option>
        </select>

N/A ....

                        Inst. Previdência Iguaba Grande

<input checked="checked" data-val="true" data-val-required="O campo bJaneiro é obrigatório." name="[3].EnvioPrestacao.bJaneiro" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bJaneiro" type="hidden" value="false" />

<input checked="checked" data-val="true" data-val-required="O campo bFevereiro é obrigatório." name="[3].EnvioPrestacao.bFevereiro" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bFevereiro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bMarco é obrigatório." name="[3].EnvioPrestacao.bMarco" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bMarco" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bAbril é obrigatório." name="[3].EnvioPrestacao.bAbril" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bAbril" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bMaio é obrigatório." name="[3].EnvioPrestacao.bMaio" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bMaio" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bJunho é obrigatório." name="[3].EnvioPrestacao.bJunho" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bJunho" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bJulho é obrigatório." name="[3].EnvioPrestacao.bJulho" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bJulho" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bAgosto é obrigatório." name="[3].EnvioPrestacao.bAgosto" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bAgosto" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bSetembro é obrigatório." name="[3].EnvioPrestacao.bSetembro" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bSetembro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bOutubro é obrigatório." name="[3].EnvioPrestacao.bOutubro" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bOutubro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bNovembro é obrigatório." name="[3].EnvioPrestacao.bNovembro" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bNovembro" type="hidden" value="false" />

<input data-val="true" data-val-required="O campo bDezembro é obrigatório." name="[3].EnvioPrestacao.bDezembro" type="checkbox" value="true" /><input name="[3].EnvioPrestacao.bDezembro" type="hidden" value="false" />

        <select class="form-control" data-val="true" data-val-number="The field TipoPendencia must be a number." name="[3].EnvioPrestacao.TipoPendencia">
          <option value="1">Cliente</option>
          <option value="2">Produto</option>
          <option selected="selected" value="3">Serviço</option>
        </select>

N/A

Comment: Era isso mesmo. Pode deixar que estou estudando. Porém não havia encontrado nada sobre isso. Muito obrigado pela ajuda.


Regression equation

Question: What would be my regression equation if I have a three level categorical value and a continuous variable?
This is how I re-coded my variables:City_d Capital_d Town_d.(recoded Town_d = 0)
RECODE Area (0=1) (ELSE=0) INTO Town_d.
VARIABLE LABELS  Town_d 'Town_d'.
EXECUTE.
RECODE Area (1=1) (ELSE=0) INTO City_d.
VARIABLE LABELS  City_d 'City_d'.
EXECUTE.
RECODE Area (2=1) (ELSE=0) INTO Capital_d.
VARIABLE LABELS  Capital_d 'Capital_d'.
EXECUTE.
And this is what I got in the Coefficients Table
constant:            3.843
City_d  :            -.900
Capital_d:           -1.498
High PoorH:           .425
High_PoorH_City_D:   -.316    interaction term
High_PoorH_Capital:  -.817    interaction term
The mean of PoorH is :.229 and the
SD for at high levels of PoorH is: 892
What would my regression equation be here?
Thank you
Comment: You've already listed your coefficients, why is the equation not known to you? (y=mx+c)?
Comment: I am just not just how to plug the numbers. I am new to Stats. We have a formula Ypred= b1X1 + b2X2 + b3X1X2 + but I don't know how to create my own.
Answer: Im going to assume you built a linear regression model as follows:
Pred_ Target ~ City_d + Capital_d + High PoorH + High_PoorH_City_D + High_PoorH_Capital

With this model and the coefficients you've shown, your final equation will become:
Pred_target = (-.9 * City_d) + (-1.498 * Capital_d) + (.425 * High PoorH) + (-.316 * High_PoorH_City_D) + (-.817 * High_PoorH_Capital) + 3.843

Replace all the variable names, with the actual values from a record in your data.
Comment: Thank you very much Arun Jose! I wish I could email you. This is definitely helping.
Comment: Please mark as answered if this solves your problem

Regression equation


Can a neural network work with support datawhich was not there while training?

Question: I am giving a hypothetical example to convey my question. Suppose I want to train a neural network that abbreviates strings with a preset list of words that are likely to be present in full form of the word. So
database = db
because
data and base are words. We can definitely train a network that can be trained to learn this kind of mapping given large training set with enough instances for each word. But can I train a general neural network which can work with a novel set of words with pre-trained network
In my example, I should be able to train a network which once trained can accept a new set of words that might appear in the examples without a need for retraining with those words.
I am thinking something along the lines of NN working with support data which could be changed as per needs.
Answer: I think what you're saying is, you want your network inputs to be independent of the words in your training set. So you don't want to rely on having "data" and "base" in your dictionary and then splitting "database" into "data" + "base".
One approach would be a character-level LSTM, which ingests your input word one character at a time, and then after each character, makes a prediction of "(blank)", or "letter", so that the final abbreviation is the concatenation of the predictions after removing (blank)s. For example with "database" it would potentially predict "d(blank)(blank)(blank)(blank)(blank)(blank)b" which becomes "db".
Comment: I am asking if it is possible to have a trained network change its behavior without training it with new examples? So your LSTM would work fine for database to db but will it work if I just know some new list of words but I don't want to retrain the network for those words? AFAIK it is not possible with normal NNs but maybe NNs could be made to work like turing machines.
Comment: It's a character level lstm. So as long as you have examples of letters "a-z" in your training set, it will work on arbitrary words.
Answer: The whole point of neural networks is to be able to generalize. In other words, neural networks would be useless if they could only ever be used on your training data and will completely fail on test (real world) data. The training of a neural network is supervised training so presumably you already know all the answers for your training data. Hence, to your general question of "can a neural network work with (support) data which was not there while training" the answer is of course yes - depending on what you mean by "support" data.
The million dollar question is not can neural networks generalize (they certainly can), but will my neural network generalize to the extent that I want it to? There is no general answer to this question, it depends on the neural network and how you train it. There's a lot of issues that arise with this question including issues of overfitting and covariate shift. You would have to experiment to see if you can get a neural network to make predictions up to your standards.


Can summary effect sizes from two separate meta-analyses be directly compared?

Question: I have run two separate random effects meta-analyses to estimate summary effect sizes (risk ratios). Both analyses are based on randomized controlled trials examining same treatment for identical populations. The only difference between the two meta-analyses are that the first model includes studies that follow up participants for 12 months or less (17 studies), while the second model includes studies that follow up participants for 18 months or more (20 studies).
I am interested in whether the treatment effect diminishes with increased follow-up time. The second model gives a lower summary effect size than the first, however the difference is small and the confidence intervals overlap. Furthermore, I am uncertain how to approach a comparison in this scenario. Are there any formalized statistical tests? Is meta-regression preferable? I have searched for literature on comparisons in this case without finding satisfying answers, so input on this would be much appreciated.
Answer: It is perfectly possible to do this in a number of different ways. You can either directly compare your two summary estimates or you can do a meta-regression with follow-up time as a moderator variables. There is an extended discussion of this with code in R on Wolfgang Viechtbauer's pages here.
Note that I am assuming the two sets of trials are independent, that is to say they are not the same trial but reporting twice at the two different time points. If my assumption is false then life becomes more difficult.


Large assets in play framework 2.1

Question: I have a huge directory of data (~30GB) which contains files i'd like to serve in my Play application. When I place the directory in the public folder, the application wont finish compiling and will just hang on compilation
I've already tried something like:
GET  /liabilities/*file          controllers.Assets.at("/foo", file)

and in the Build.scala file
// Add your own project settings here
playAssetsDirectories <+= baseDirectory / "foo"

However, I get the same issue as above.
I've also tried ExternalAssets
Some of the files being served are used in javascript so I cannot have them on a separate local ftp server or javascript will complain.
Ideas?
Comment: Where are you gonna to publish it ? some cloud, custom VPS, other?
Answer: Actually you don't need (and as you can see in your case) you don't even should place assets in apps directory, instead consider two possibilities:

Frontend HTTP server, for an example nginx, light and fast will allow you to create a reverse proxy and/or aliases, so you can just serve files directly from disk within address http://domain.tld/public - in such case you need to write paths manually instead of using Assets.at(...). Actually using this approach you can also serve common assets, such as layout's CSS'es, images etc.
Use some professional CDN or cloud storage, anyway in this case you will need to rebuild your scripts a little.
Answer: I've found a quick and dirty solution. When setting up a simple http webserver (e.g python simple webserver), you need to enable cross origin resource sharing which will allow javascript to access files from different domains.
CORS is built into in several webservers, but not pythons. I found a script that that will do this for python. See here
Cheers

Large assets in play framework 2.1


Scrollable UITextField

Question: Is there any way to make the UITextField (horizontally) scrollable?
Thanks,
Mihai
Answer: Do you mean a multi-line UITextField? If that is the case you need UITextView. By default it is multi-lines.
Comment: Not necessarily multi-line, as one single line that scrolls horizontally. Thanks.
Comment: Why would that be required? To display the white space beneath the text?
Comment: No, I just have a text that is not small enough to fit in a text field. Since all other text fields are one line, I wanted to have the same. I will end up using text views instead of text fields everywhere on that particular screen, so to keep the uniformity intact. The problem with text views is that they do not have rounded edges like text fields do. I will end up setting the opacity to 0 to only display the text.

Scrollable UITextField


How can you logout with Realm javascript?

Question: I'm using the Realm self hosted platform for javascript for a react native App.  The login code works fine, as show below...
Realm.Sync.User.login('http://0.0.0.0:9080', creds)
.then(result => resolve(result))
.catch(error => reject(error))
...

However, when I try to logout, I get an error that says Realm.Sync.User.logout is not a function.   Here is the logout code...
logout = () => {
    Realm.Sync.User.logout()
}

I've gone through the docs a few times, but it seems this function simply doesn't exist.  https://realm.io/docs/javascript/latest/api/Realm.Sync.User.html
I'm using realm 2.18.0.
Any ideas?
Answer: The issue is that you are trying to use logout as a static method, but it is an instance method. You need to store a reference to your logged in User instance and call logout on that instance.
Realm.Sync.User.login('http://0.0.0.0:9080', creds)
.then(user => resolve(user))
.catch(error => reject(error))
...

Make sure that you define user to be in scope for logout and also make sure that you only call logout once login is resolved.
logout = () => {
    user.logout()
}


From OLS to semi-parametric GAM: parametric vs no-parametric

Question: I am quite new to this kind of topic, but for my master thesis i built an multiple linear regression with OLS. Now I want to control for non-linear relationships using a semi-parametric GAM. My Question is: What is the right way to decide which variables are parametric and which are non-parametric? I would've included a smooth funktion s() for every variable and the looking at the edf's and if they're equal to 1 this is a linear relationship and if they're higher, then i would've said there's a non-linear relationship. Is that the right procedure?
Answer: You could use an exploratory approach to guide your choice as to what (continuous/discrete) predictor variables should be allowed to have non-linear effects.
Simply construct component plus residual plots (also known as partial residual plots) for each of those variables after fitting your multiple linear regression model with OLS. If any of the component plus residual plots suggest nonlinearity, then you could model the corresponding effects as nonlinear in your GAM model.
R has a function called crPlots in the car package which helps construct component plus residual plots: https://www.rdocumentation.org/packages/car/versions/3.0-0/topics/crPlots.
Comment: Thank you for your help. I tried it and plotted the  explainatory variable "age" against the log of the transaction price, because from my advisor i know that there has to be a non-linear relationship... but I don't know how to see that because of that plot. Maybe you could explain it a little bit for me :) The link to the plot is: https://picload.org/view/dlllcwcl/rplot01.jpg.html
Comment: Is the outcome variable in your model log(kaufpreis)? If the plot you linked to is the component plus residual plot for the explanatory variable age, then that plot doesn't show any evidence of nonlinearity of the effect of age. While the theory predicts a linear relationship between log(kaufpreis) and age controlling for the other explanatory variables in your model, your data do not seem to support that.
Comment: Ok, I get that point. But when I run the GAM with these variables the Output shows a nonlinear relationship. Or maybe I didn't understand it right :) It would be nice if you could take alook at the output and the graphs of the splines: https://picload.org/view/dlllwllw/output1.jpg.html and https://picload.org/view/dlllwlcr/splines.jpg.html .... Thank you very much for your help :)
Comment: Were your data collected over time? Also, can you share a plot of the estimated nonlinear effects?
Comment: It's pooled cross-section collected over time, transaction prices from different houses in different months, because of that the time dummies. Here are the plots: https://picload.org/view/dllidarl/nonlin.jpg.html


differentiating sinx in relation to time

Question: So I am trying to figure this out, and using chain rule,I get 1(sinx)^0 x cos dx/dt, but somehow the answer is cos x dx/dt. I guess I can't figure out where the extra x is coming from because while I get that it is cos x normally, in this case doesn't the x become dx/dt? If anyone can explain this to me, I would appreciate it greatly.
Comment: You wrote x cos.  Do you mean cos x?
Answer: Recall the chain rule:  $\frac{d}{dx}f(g(x)) = f'(g(x)) g'(x)$.
I presume that you are trying to differentiate $\sin(x(t))$ with respect to time.  Let $f=\sin$ and let $g=x$, and let $x=t$.  Then use the chain rule as stated above.
$\frac{d}{dt}\sin(x(t))=\sin'(x(t))\cdot x'(t)=\cos(x(t)) \cdot x'(t)$, which is the stated answer.


How to extract standard error from coefficients in stan_glm

Question: I have a model that looks like this:
fit <- stan_glm(switch ~ arsenic + dist100,
                family=binomial(link="logit"),
                data=wells,
                refresh=0)

with an output that looks like this:
stan_glm
 family:       binomial [logit]
 formula:      switch ~ arsenic + dist100
 observations: 3020
 predictors:   3
------
            Median  MAD_SD
(Intercept)  0.0054  0.0810
arsenic      0.4618  0.0415
dist100     -0.9025  0.1040

------

and I want the MAD_SD coefficients from the resulting fit. I usually use se(fit) and that gives me the coefficients, but for some reason I get the error
Error in se(fit) : argument "predicted" is missing, with no default

How do I remedy this error?
Answer: In case anybody wants to know the answer to this question, stan_glm objects are basically like big tibbles that map a name to an element. In this case, the element is named "ses" and it's a list. So you can do
fit$ses[0]

to index into it
EDIT: Turns out, the reason that I needed to do this at all was because I imported a package AFTER stan_glm that also has a function called "se" and it got masked.
Comment: Why use stan_glm and not glm?
Comment: not entirely sure, this was for a class. What's the difference between glm and stan_glm?
Comment: Oh I looked at documentation and the stan version is for Bayesian stats
Answer: Another option would be to use broom.mixed::tidy().
library(rstanarm)
library(broom.mixed)
fit <- stan_glm(am ~ mpg + disp, family = binomial, data =mtcars)
tidy(fit)

# A tibble: 3 × 3
  term        estimate std.error
  <chr>          <dbl>     <dbl>
1 (Intercept) -2.57      4.25
2 mpg          0.188     0.153
3 disp        -0.00817   0.00698


Scala - Convert Map to Json avoiding key/value keywords

Question: Consider a simple map below:
val myTable:Map[String,Any] = Map(
"Table1" -> Array(
    Map("date" -> "2019"),
    Map("FilePath" -> "C:/Test")
))

I try to write it into a JSON file using the commands below:
import java.io.{File,FileWriter,Writer}
import org.apache.spark.sql.SparkSession
import com.google.gson.Gson
import com.google.gson.GsonBuilder

val gson: Gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create()
val json =  gson.toJson(myTable)
val w: Writer = new FileWriter("C:/Test/output.json")
w.write(json)
w.close()

What I get in the json file is as following:
{
"key1": "Table1",
"value1": [
{
  "key1": "date",
  "value1": "2019"
},
{
  "key1": "FilePath",
  "value1": "C:/Test"
}
]
}

I am wondering if it is possible to exclude "key1", "value1" words and instead print their actual values into the file (Please see the structure below):
{
"Table1": {
"date": "2019",
"FilePath": "C:/Test"
}
}

If it is relevant, I am using maven not sbt.
Answer: Try this:
import play.api.libs.json.Json

object JsonToString extends App {
  val myTable = Map(
    "Table1" -> Array(
      Map("date" -> "2019"),
      Map("FilePath" -> "C:/Test")
))

val jsonObject = Json.toJson(myTable)
println(jsonObject)

}
It should remove the key1, value1 words and instead use their actual value. Below are the list of maven dependency you need to add.
<dependency>
    <groupId>com.typesafe.play</groupId>
    <artifactId>play-json_2.10</artifactId>
    <version>2.4.0-M1</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.10.0</version>
</dependency>

Comment: For example, if you wanted to make it pretty you'd do something like this: https://scastie.scala-lang.org/872ipw3QToKvQJIuwbAdeQ
Comment: Thanks Madstuffs! I am new to scala and appreciate it if you can help me with the following issues that I face when trying to implement your solution: 1) For "import play.api.libs.json.Json", it says "cannot resolve symbol play"; why? 2) For "val jsonObject = Json.toJson(myTable)", it says "cannot resolve symbol Json" and asks me to add either "import org.apache.avro.data.Json" or "import org.json4s.jackson.{Json, Serialization}" 3) After adding it, now it doesn't like ".toJason"; why? 4) You used "println", if I need to write it to a file, should I simply use FileWriter as I mentioned above?
Comment: You need to add a maven dependency. I'll add it in the answer.
Comment: Ah, you beat me to it! Was going to suggest using Play JSON. @Mahmoud, the println was just to give an example printing out the JSON. If you actually want to do something with the JSON (like use a FileWriter), yes you'd replace this line.
Comment: Thanks James! With your additional suggestions it now works perfectly fine!


OATH TOTP with Windows NPS Server?

Question: I'm trying to set up multi-factor authentication for AWS WorkSpaces using AD and OATH TOTP (e.g. Google Authenticator).  In the AWS AD Connector config you can set up the RADIUS server's IP, port and shared code.  If I understand correctly the RADIUS server is what would then connect to Google Authenticator or any other provider and those details are abstracted away behind RADIUS.
Is it possible to connect NPS to an OATH TOTP provider or do you require another RADIUS server?  Did I misunderstand how this works and the provider would have additional software to install?  I've searched online but haven't found a very clear answer.
Answer: Looking around a bit I think you might want to approach this a bit differently.  Let Amazon services do the OTP heavy lifting and only reach back to your AD for that small part of things.
Method seems to be first set up Amazon Directory Services to use your AD:  http://docs.aws.amazon.com/directoryservice/latest/ad-connector/what_is.html
Enable/configure multi-factor authentication on that:  http://docs.aws.amazon.com/directoryservice/latest/ad-connector/connect_mfa.html
Then come back and point your Amazon Workspaces at the Amazon Directory Services instance you just set up:  http://docs.aws.amazon.com/workspaces/latest/adminguide/registration.html
I've done none of this, but on paper this looks like it might be easier than what you're contemplating, instead.
Comment: We are currently using simple AD but would have to change to AD Connector.  I've done this before so this part's easy.  In your second link when you enable MFA you have to specify the RADIUS server IP(s).  This is the part I'm having trouble with.  It's possible that WorkSpaces client uses AD Connector for the AD authentication and RADIUS is used only for the second factor.  Let's assume that's the case; how do I configure Windows NPS to use virtual MFA such as OATH TOTP?  Do I need a 3rd party, do I need to go Linux, etc.?  If I can answer this last part I think I can figure out the rest.

OATH TOTP with Windows NPS Server?


Session Replication and clustering using Spring Session and HazelCast IMDG on WildFly 18

Question: As per the requirement, I have to make the WebApp sessions to be clustred on WildFly 18.
I'm using Spring session to manage the clustering and filtering along with Spring Security. The WebApp is using JSF 2.3 as fronted, and I'm using HazelCast to replicate and store the session.
The problem is, when I try to run my application on WildFly, once logged in, if I try to refresh the page or navigate to other page I'm getting logged out instantly. The same app is working fine on Tomcat, hence there must be something going on with WildFly configuration.
What I have done till now is:

I'm using standalone.xml(with default configuration) instead of full
or full-ha.
I have removed <distributable/> from web.xml (although the result is same either I add it or not to web.xml).
I'm Using hazelcast-all.jar in my project.

So, should I have to do anything in WildFly to stop using its inbuilt infispan clustering?
I've searched over and found this:
https://developer.jboss.org/thread/266186?_sscc=t
I'm sure I'm missing something while configuring the app with wildfly.
Any help would be appreciated.
Answer: Removing  from web.xml will cause WildFly to use a ConcurrentHashMap-based session manager (i.e. no more Infinispan).
Spring Session is supposed to hijack the request such that no HttpSessions are ever created (in WF's session manager).  This means you can't use WildFly to manage security (i.e. standard JEE authentication/authorization), as everything will need to be managed by Spring.


Análise de Complexidade de Algoritmo

Question: Um professor da minha faculdade passou um exercício para analisar a complexidade temporal desse algoritmo, mas eu não estou conseguindo de jeito nenhum. Alguém sabe me dar uma luz de como separar o algoritmo?
 int Algoritmo(int A[], int n) {
    int k=0, i=1, j=0;
    int h;
    while (i<n && k+j+1<n ) {
        if (A[k+j] == A[(i+j)%n]) {
            j = j+1;
        }
        else if (A[k+j]  A[(i+j)%n]) {
            h = max(i, k+j+1);
            k = h;
            i = h+1;
            j = 0;
        }
    }
    return k;
}

Tipo, quando é com uma variável no while, fica bem fácil,
Mas nesse caso, o i, j e k vão mudar de formas diferentes de acordo com cada else...
O que eu queria saber é, existe algum meio de descobrir essa complexidade de modo fácil ou eu teria que ficar testando e achar a hora em que o algoritmo começa a repetir?
Obrigado pela ajuda!
Answer: Primeiro, dá para eliminar o h do algoritmo para simplificar um pouco mais:
int Algoritmo(int A[], int n) {
    int k=0, i=1, j=0;
    while (i<n && k+j+1<n ) {
        if (A[k+j] == A[(i+j)%n]) {
            j = j+1;
        }
        else if (A[k+j]  A[(i+j)%n]) {
            k = max(i, k+j+1);
            i = k+1;
            j = 0;
        }
    }
    return k;
}

n me parece ser o tamanho do array. Pois o array é acessado nas seguintes posições:

(i+j)%n o que vai dar alguma posição entre 0 e n-1.
k+j, mas a condição do while garante que k+j+1<n e isso também significa que k+j<n e portanto é alguma coisa entre 0 e n-2.

Dentro do while há três condições, comparando o A[k+j] com o A[(i+j)%n]. Ou estes dois elementos são iguais, ou o primeiro é maior, ou o segundo elemento é maior. Logo, sempre haverá uma e apenas uma das condições sendo verdadeira em cada iteração do while.
Vamos ver como j evolui ao longo do tempo:
j=0;     // Inicial
j = j+1; // Primeira situação (==)
j = 0;   // Segunda situação (<)
j = 0;   // Terceira situação (>)

Ou seja, j começa em zero, vai incrementando de um em um de vez em quando ou então volta para zero. Para simplificar a análise, vamos por ora assumir que a primeira condição (do ==) nunca será verdadeira e que portanto j seria sempre zero. Sabemos que isso não é correto, mas faremos apenas para entender melhor o que se passa no algoritmo. Desta forma, ele ficaria assim:
int AlgoritmoErrado(int A[], int n) {
    int k=0, i=1;
    while (i<n && k+1<n) {
        if (A[k]  A[i%n]) {
            k = max(i, k+1);
            i = k+1;
        }
    }
    return k;
}

Uma vez que pela definição do while, temos que i<n, podemos eliminar o %n:
int AlgoritmoErrado(int A[], int n) {
    int k=0, i=1;
    while (i<n && k+1<n) {
        if (A[k]  A[i]) {
            k = max(i, k+1);
            i = k+1;
        }
    }
    return k;
}

Vejamos o que este algoritmo faz. Sabemos que ele funciona apenas quando todas as posições do array têm valores diferentes (afinal, eliminamos o caso do == que precisaria do j para funcionar).
Começamos com k=0 e i=1. Os elementos A[0] e A[1] são comparados, se o A[0] for menor que o A[1], o i será incrementado, e então vamos comparar o A[0] com o A[2] e se a condição se manter, vamos comparar o A[0] com o A[3]. Ou seja, a condição do < se mantém quando o elemento A[k] é menor do que os elementos que o sucedem. Se tivermos o caso aonde o primeiro elemento do array for o menor de todos, zero será retornado pelo algoritmo, o que parece indicar que ele descobre aonde está o elemento de menor valor.
Vamos ver o que ocorre se cairmos na condição >. Começamos comparando A[0] com A[1] e o menor elemento é o A[1]. Então comparamos A[1] com A[2], depois A[2] com A[3], e o valor do k é sempre o valor do índice do array com o menor elemento.
Concluímos que este algoritmo retorna a posição no array que tem o menor valor encontrado.
Sabendo deste comportamento, temos que neste caso aonde não há j, o k sempre será menor (e nunca igual) ao i. Portanto podemos simplificar o algoritmo para o seguinte:
int AlgoritmoErrado(int A[], int n) {
    int k=0, i=1;
    while (i<n && k+1<n) {
        if (A[k]  A[i]) {
            k = i;
            i = k+1;
        }
    }
    return k;
}

Que é equivalente a isso:
int AlgoritmoErrado(int A[], int n) {
    int k=0, i=1;
    while (i<n && k+1<n) {
        if (A[k] > A[i]) {
            k = i;
        }
        i = i+1;
    }
    return k;
}

Uma vez que temos sempre que k<i, então a condição k+1<n é redundante, pois só será verdadeira se i<n também for. Então o algoritmo fica assim:
int AlgoritmoErrado(int A[], int n) {
    int k=0, i=1;
    while (i<n) {
        if (A[k] > A[i]) {
            k = i;
        }
        i = i+1;
    }
    return k;
}

Que por sua vez é equivalente a isso:
int AlgoritmoErrado(int A[], int n) {
    int k=0;
    for (int i = 1; i<n; i++) {
        if (A[k] > A[i]) k = i;
    }
    return k;
}

E neste ponto já está óbvio que ele vai iterar exatamente n-1 vezes, o que seria Θ(n-1), mas como o n domina sobre o 1 temos Θ(n).
E quanto ao j? Será que o j destroi o Θ(n) que o algoritmo seria?
Os elementos são acessados nas posições k+j e (i+j)%n. Na primeira condição, o k+j cresce, aproximando o fim do algoritmo um passo na direção da condição k+j+1<n. Na terceira condição, o k aumenta e o j volta a zero, mas o valor de k+j também aumenta porque k = max(i, k+j+1), o que garante que o novo valor de k+j será maior ou igual ao k+j+1 anterior. O k+j só pode diminuir na segunda condição, mas essa redução só vai ocorrer após uma sequência de entradas na primeira condição, o que fará o i aumentar em lugar pulando por cima de todas as posições k+j e aproximando o fim do algoritmo da condição i<n enquanto que o k+j só retrocede até o ponto antes da entrada na condição do == pela primeira vez.
Assim conclui-se que a cada iteração ou chega-se um passo mais perto da condição de parada k+j+1, ou então retrocede-se alguns passos desta condição, mas avança-se este mesmo número de passos mais um em direção a condição de parada i<n.
Se a for o número de passos realizados na primeira e na terceira situação se aproximando de se atingir a condição de parada do k+j+1 e b for o número de passos realizados na segunda situação se aproximando para atingir a condição de parada do i<n, então em no máximo a+b passos, pelo menos uma das condições será atingida. Dado que sabemos que a<=n e b<=n então a+b<=2n.
Isso significa que garantidamente, em não mais que 2n iterações o algoritmo termina e em não menos que n-1 iterações ele termina. Assim o algoritmo é Ω(n-1) e O(2n). Como os fatores constantes não importam e o n domina sobre o 1, então Ω(n-1) é o mesmo que Ω(n) e O(2n) é o mesmo que O(n). E se o algoritmo é Ω(n) e O(n) ao mesmo tempo, então ele é Θ(n).

Análise de Complexidade de Algoritmo


Adding validation to widget settings form

Question: I'm creating a new field in Drupal 8. I've created a class for the widget, extending WidgetBase and implementing WidgetInterface, and added the functions defaultSettings(), settingsForm() and settingsSummary().
I want/need to validate the element I'm adding in settingsForm(), but I can't seem to figure out how to add validation to this. I tried adding a function settingsFormValidate(), but that doesn't seem to be called. I also tried adding #element_validate to the element I'm adding in settingsForm(), but that also does not seem to be called.
Note that I'm not looking to validate the widget (that is shown to the user), I'm looking to validate the element added in settingsForm() for the widget, that is on the Manage Form Display tab, when adding the field to Entities/content types.
Does anyone know how or whether it's even possible to add validation to the widget settings form?
Answer: Once again, I've found the solution to my issue. Although I said that I had tried #element_validate in my original post, I must have messed something up, because that turned out to be the solution. Here is the code:
class SomeWidget extends WidgetBase implements WidgetInterface
{
  public function settingsForm(array $form, FormStateInterface $form_state)
  {
    $element['my_field'] = [
      '#type' => 'textfield',
      '#title' => t('My Field'),
      '#default_value' => $this->getSetting('my_field'),
      '#required' => TRUE,
      // Add your validation here
      '#element_validate' => [
        // settingsFormMyFieldValidate() is arbitrary. Choose something descriptive
        [$this, 'settingsFormMyFieldValidate'],
      ],
    ];

    return $element;
  }

  public function settingsFormMyFieldValidate($element, FormStateInterface $form_state)
  {
    $submitted_value = $form_state->getValue($element['#parents']);
    if($submitted_value != 'something it should equal')
    {
      $form_state->setError($element, t('You submitted a bad value'));
    }
  }
}

Comment: I think the validator is executed within an asynchronous callback when submitting the widget settings form. As a result, use `'#element_validate' => [[static::class, 'validate']],` and `public static function validate`
Comment: Also, be aware of "A field widget that fails validation in settingsForm() does not display an error message without inline_form_errors" issue @ https://www.drupal.org/project/drupal/issues/3407134
Answer: There is a field_example module in the examples module. It includes a TextWidget and Text3Widget, both of which have a validation function on their formElement.
/**
 * Plugin implementation of the 'field_example_text' widget.
 *
 * @FieldWidget(
 *   id = "field_example_text",
 *   module = "field_example",
 *   label = @Translation("RGB value as #ffffff"),
 *   field_types = {
 *     "field_example_rgb"
 *   }
 * )
 */
class TextWidget extends WidgetBase {
  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $value = isset($items[$delta]->value) ? $items[$delta]->value : '';
    $element += array(
      '#type' => 'textfield',
      '#default_value' => $value,
      '#size' => 7,
      '#maxlength' => 7,
      '#element_validate' => array(
        array($this, 'validate'),
      ),
    );
    return array('value' => $element);
  }

  /**
   * Validate the color text field.
   */
  public function validate($element, FormStateInterface $form_state) {
    $value = $element['#value'];
    if (strlen($value) == 0) {
      $form_state->setValueForElement($element, '');
      return;
    }
    if (!preg_match('/^#([a-f0-9]{6})$/iD', strtolower($value))) {
      $form_state->setError($element, t("Color must be a 6-digit hexadecimal value, suitable for CSS."));
    }
  }
}

I hope this points you to the right direction.
Comment: Thanks, but that's for the form element itself. I need to validate the settings form of the Field - I've already handled the element level validation.
Comment: I think the validator is executed within an asynchronous callback when submitting the widget settings form. As a result, use `'#element_validate' => [[static::class, 'validate']],` and `public static function validate`
Comment: Also, be aware of "A field widget that fails validation in settingsForm() does not display an error message without inline_form_errors" issue @ https://www.drupal.org/project/drupal/issues/3407134


Requisição CUrl não funciona dependendo do tamanho do JSON

Question: Meu problema é que dependendo do tamanho do JSON enviado, a requisição CUrl não funciona, ou seja caso eu envie 2 fotos no JSON, da certo, mas 4 já é motivo para um TimeOut.
O problema não está na API pois no Postman funciona independentemente do tamanho do JSON, ou seja, creio que seja alguma configuração que eu não esteja setando, alguém tem alguma ideia?
Estrutura do CUrl...
$ch = curl_init();

curl_setopt_array($ch, array(
            CURLOPT_URL => trim($this->vg['url'] . $url1 . $url2),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => $method,
            CURLOPT_POSTFIELDS => $json,
            CURLOPT_HTTPHEADER => array(
              "Cache-control: no-cache",
              "Accept: application/json",
              "Content-type: application/json",
            ),
          ));

Answer: O timeout do cURL está em 30s, troque a linha abaixo:
CURLOPT_TIMEOUT => 30

para (1800 = 30min):
CURLOPT_TIMEOUT => 1800

Fonte: http://php.net/manual/pt_BR/function.curl-setopt.php
Coloca também o código abaixo antes de tudo. Note: 30*60 = 1800s = 30min máximo de timeout (tempo de execução máxima). E o post_max_size tb tem influência no upload de arquivos, segundo a documentação do php.
ini_set('max_execution_time', 30*60);
ini_set('post_max_size','256M');

Fonte: https://secure.php.net/manual/pt_BR/ini.core.php
Comment: já tinha tentado isso não resolveu
Comment: Seu servidor está habilitado para vc reconfigurar em execução pelo php? Qual o erro exibido?
Comment: Cara... o timeout do cURL está em 30segundos (CURLOPT_TIMEOUT => 30), já tentou aumentar ele também para CURLOPT_TIMEOUT => 1800?
Comment: Sim kkk, aumentei ao máximo, simplesmente fica em um looping infinito, basta eu aumentar um pouco o json e já era
Comment: Estranho... já usei o cURL do php para enviar até 4GB e deu certo.


SVM confusion matrix whose dimensions are more than two

Question: While I am using SVM, I train it with a train data and then I try to predict a sample if its label is -1 or +1. However, I see some confusion matrice for SVM like below. Mine are 2x2 matrice but their dimesions are larger, e.g. 15x15. Do they have more than one SVM? How people do such things? It is easy to write one diagonal but how they decide other values? In the figure below, for 2, it is easy to write 38 but how they write 2?
BTW, I am new to these topics.
Comment: There are multi-class SVMs as well ( http://svmlight.joachims.org/svm_multiclass.html ). For that matter, from the name of the file, it seems they are using one vs all multiclass SVMs - http://stats.stackexchange.com/questions/21465/best-way-to-perform-multiclass-svm
Comment: I understand if there are 15 svm models. While we are evaluating a sample if its model is correctly classifies it then we increment the value, e.g. 38 for 2. And if it is correctly classified by another model (this is a false positive), e.g. 10's model, we increment the value, e.g. 2 for 2. However, what if there are more than one model which says it is a valid (+1) sample for that model? What would we do in this case? How can we sure which one is correct?
Comment: Confusion tables belong to classification (used in the sense that the outcome is nominal or maybe ordinal), not to  regression - I removed the "regression" tag.
Answer: Based on only the context we can get from the URL name, they are most likely using 15 SVMs, each of which tries to classify one class "versus" the rest (one-vs-all). Thus you need 15, and thus 15x15 confusion matrix.
You make a good point - more than one of these might declare a new point as their own. What then? A simple way is to use the hyperplane that is output as the model for the SVM and compare the distance to the plane in all +1 class labelings and accept the class for which the distance from the point to the plane is the greatest (ie, was most confidently classified).
The off-trace (incorrect) labelings the classifier makes are still done in the same way, ie,
C_ij = count(we declared i, but was actually j)


How to sample from a probability distribution on the unit sphere?

Question: I'm dealing with the following problem:
"Given a set of $N$ points on the unit sphere, sample a new set of $M$ points having the same probability distribution."
I know that the simple 1D version can be solved e.g. with the inverse transform sampling method, but I couldn't find any reference on how to tackle this 3D equivalent on the unit sphere. Does anyone have good resources or hints about this?
Thank you in advance.
Comment: A relevant concept is [Von Mises-Fisher distribution](https://en.wikipedia.org/wiki/Von_Mises–Fisher_distribution)
Comment: Does the support of your distribution include only the discrete points that were sampled or do you want an approximate continuous distribution based on the points that you have?
Comment: I think this problem is a bit underspecified to have a precise answer. If you can narrow it down to a single family of distributions, then you can do MLE on it or something. If you're looking for piecewise *constant*, you could use the points to derive a Voronoi diagram and have the density inversely proportional to the area of each zone. I'm not sure what piecewise linear would even look like on the sphere, to be honest. What's the local criterion for piecewise linear?
Comment: The simplest solution is to just sample from your N points as if they are the only points in the world. If you want to be able to get 'new' points then a bit more information about the probability distribution is needed
Comment: @Vincent the PDF is discrete and defined by those points. I would like to find an equivalent of the 1D case where you just have discrete points and approximate the PDF as piecewise linear, then sample from it using inverse transform.
Comment: @BrianBorchers ideally I would like to approximate the underlying continuous distribution, in order to sample from it. Not sure how to approximate it though, what I'm actually looking for is the spherical equivalent of using a piecewise linear distribution.
Answer:
Continuous Uniform Distribution.
The total surface of the unit sphere is
$$
S_2=\int_0^{2\pi}\int_0^{\pi}\sin\theta\,d\theta\,d\phi=4\pi\,,
$$
where $\theta,\phi$ are polar angles latitude and longitude (aka meridian). Unlike on earth, the convention here is that the north pole is at $\theta=0\,.$ One could now sample the random angles $\Theta,\Phi$ such that
$$
\mathbb P(\Theta\le \theta,\Phi\le\phi)=\frac{1}{4\pi}\int_0^\phi\int_0^\theta\sin t\,dt\,dp=\frac{1}{4\pi}\phi\,(1-\cos\theta)\,.
$$
The joint distribution of $\Theta$ and $\Phi$ is obviously the product of the marginal distributions
$$
\mathbb P(\Theta\le\theta)=\frac{1}{2}(1-\cos\theta)\quad\text{ and }\quad\mathbb P(\Phi\le\phi)=\frac{\phi}{2\pi}\,.
$$
Clearly, the meridian $\Phi$ has a uniform distribution on $[0,2\pi]$ and the latitude $\Theta$ can be sampled by drawing a uniform $U\in{\cal U}(0,1)$ and inverting the marginal distribution of $\Theta:$
$$
\Theta=\arccos(1-2U)\,.
$$

Discrete Uniform Distribution. A finite number of $N$ points can be put relatively homogeneously onto the sphere when the differences in their longitudes $\phi_i$ and latitudes $\theta_i$ satisfy
$$
\phi_i-\phi_{i-1}=\frac{2\sqrt{\pi}}{\sqrt{N}\sin\theta_i}\,,\quad\theta_i-\theta_{i-1}=\frac{2\sqrt{\pi}}{\sqrt{N}}\,,\quad i=1,...,N\,.
$$
Clearly,
\begin{align}
&\mathbb P(\theta_{i-1}\le\Theta\le\theta_i,\phi_{i-1}\le\Phi\le\phi_i)
\\[3mm]
&=\mathbb P(\Theta\le\theta_i,\Phi\le\phi_i)
-\mathbb P(\Theta\le\theta_i,\Phi\le\phi_{i-1})\\[3mm]
&\quad-\mathbb P(\Theta\le\theta_{i-1},\Phi\le\phi_i)
+\mathbb P(\Theta\le\theta_{i-1},\Phi\le \phi_{i-1})\\[3mm]
&=\frac{\phi_i(1-\cos\theta_i)-\phi_{i-1}(1-\cos\theta_i)-\phi_i(1-\cos\theta_{i-1})+\phi_{i-1}(1-\cos\theta_{i-1})}{4\pi}\\
&=\frac{(\cos\theta_{i-1}-\cos\theta_i)(\phi_i-\phi_{i-1})}{4\pi}\,.
\end{align}
For large $N$ the difference $\theta_i-\theta_{i-1}$ small so that this probability is approximately equal to
\begin{align}
\frac{\sin\theta_i(\theta_i-\theta_{i-1})(\phi_i-\phi_{i-1})}{4\pi}=\frac{1}{N}
\end{align}
which is equal for all cells $[\phi_{i-1},\phi_i]\times[\theta_{i-1},\theta_i]\,.$
Comment: Hi, thanks for the answer, but I don't see how this is connected to having a set of points defining the PDF.
Comment: How many points? An arbitrary but finite number? If so: what have they to do with the surface of the sphere?
Comment: Yes, an arbitrary but finite number that are defined to lie on the surface of the sphere. It's simply the domain they belong to.
Answer: You are given $N$ points on a sphere, $X=\{x_1,\dots,x_N\}$. In order to generate a new point $y$ with a similar distribution to $X$,

Choose $x\in X$ uniformly at random.

Choose an angle $\theta\in [0,2\pi)$ uniformly at random.

Choose a positive number $r$ using some continuous distribution on $[0,\pi)$. The distribution should be tightly clustered near $0$. For example, the continuous uniform distribution on $[0,\pi/20]$ would work.

Place a vector at $x$, pointing to the north pole. Rotate that vector by $\theta$, clockwise. Walk $r$ units along the direction of the rotated vector. Where you end up is the new point $y$.

Repeat this process $M$ times to generate $M$ points. Note that every point $y$ will tend to cluster where $X$ clusters, but will never be exactly equal to any of the points in $X$.
Comment: Hi, thanks for your answer. Unfortunately, the set of points to sample must be different from the original points.
Comment: @morsingher I've edited to accommodate this new constraint.
Comment: One issue not discussed in this answer is what distribution to use for $r$.  Kernel density estimation can be used to help select the parameters of a distribution for $r$.
Answer: You should consider using a kernel density estimator to estimate the pdf and then sample from that estimated pdf as in the answer from Mike Earnest.
Although kernel density estimation with Gaussian kernels is widely used for distributions in $R^{n}$ and discussed in many textbooks, you need a version customized to work on the sphere. The von-Mises-Fisher distribution is often used instead of a Gaussian kernel.
A google search on "spherical kernel density estimation"  quickly leads to many research papers on the topic and at least one software package written in Python.
See for example:
Hall, Peter, G. S. Watson, and Javier Cabrera. "Kernel density estimation with spherical data." Biometrika 74, no. 4 (1987): 751-762.
Pelletier, Bruno. "Kernel density estimation on Riemannian manifolds." Statistics & probability letters 73, no. 3 (2005): 297-304.
Gray, Alexander G., and Andrew W. Moore. "Nonparametric density estimation: Toward computational tractability." In Proceedings of the 2003 SIAM International Conference on Data Mining, pp. 203-211. Society for Industrial and Applied Mathematics, 2003.
Software:
https://github.com/williamjameshandley/spherical_kde


Find DE given its solution

Question: If anyone could give me a hint from where to start because I can't think by reversing solution...
$$
    x^{3} y^{3} = 2^x - 6x + C \qquad\text{ }\quad
$$
Comment: do you mean a differential equation?
Comment: How about differentiating both sides to get rid of the $C$... Is that what they mean?
Comment: usually when we have a diff. equation, we derive a part from it partially in terms of x and so in terms of y, and if the partial derivative for both are equal we get an exact equation and solve it normally.. but the question is giving the solution and asking for the initial diff. equation
Comment: I'm sure what you are asking about is somewhat clearer to you than it will be to your Readers.  Part of the problem is that the body of the Question starts "I dont know how to solve it" before you've presented a statement of the problem.  Relying on the title alone to bear this burden invites confusion.  The body text gives more space to setup and explain what you've tried or considered, and what the difficulty is from your point of view.
Answer: Since you have edited the tag 'differential-equations' I am going that way.(Well ..I don't have any other solution :P)
$$d(x^3y^3-2^x+6x)=d(C) $$
$$d(x^3y^3)-d(2^x)+d(6x)=0$$
$$3x^2y^3 ~dx+3y^2x^3~ dy-2^x \ln 2~dx+6~dx=0$$
$$-3y^2x^3~ dy=(3x^2y^3-2^x \ln 2+6)~dx$$
$$\implies \frac{dy}{dx}=\frac{3x^2y^3-2^x \ln 2+6}{-3y^2x^3}$$

Find DE given its solution


How to find an irreducible polynomial of degree $p$ over finite field $F_p$ where $p$ is prime?

Question: I have learnt a theorem that there always exists an irreducible polynomial of an arbitrary degree $m$ over a finite field. This theorem, of course, ensures the existence of an irreducible polynomial
of degree $p$, but how to actually find such a polynomial? Any help would be appreciated!
Comment: Called https://en.wikipedia.org/wiki/Artin-Schreier_theory
Comment: Now found a [question](https://math.stackexchange.com/q/81583) about that.
Comment: @metamorphy That's correct. How do you figure it out?
Answer: An irreducible polynomial $f(x)$ of degree $n$ over $\mathbb{F}_p$ is necessarily a divisor of $x^{p^n}-x$.
Which gives a known criterion: $f(x)\in\mathbb{F}_p[x]$ is irreducible if and only if $f(x)\mid x^{p^n}-x$, where $n=\deg f$, and for each prime $d\mid n$ we have $\gcd\big(f(x),x^{p^{n/d}}-x\big)=1$. In terms of roots of $f$ (in an algebraic closure of $\mathbb{F}_p$), these must be distinct (this is checked by $\gcd(f,f')=1$), and any root $\alpha$ must satisfy $\alpha^{p^n}=\alpha$ but $\alpha^{p^{n/d}}\neq\alpha$ for each prime $d\mid n$.
This gives an easy way to check that $g(x):=x^p-x-1\in\mathbb{F}_p[x]$ is irreducible. Since $g'=-1$, the condition $\gcd(g,g')=1$ holds trivially. Let $\alpha$ be a root of $g(x)$. Then, by induction, $\alpha^{p^k}=\alpha+k$ for any natural $k$, hence $\alpha^{p^p}=\alpha$. Since $d=p$ is the only possibility here, and $\alpha^{p^1}=\alpha+1\neq\alpha$, we're done.


Chords derived from uncommon scales

Question: I came across an unusual chord progression in an A minor track: iv-I.
Correct me if I'm wrong, but my first guess is that the I is borrowed from the major key. However, after thinking a while, I started to wonder why most chord progressions assume chords from either the major or natural minor scales (I would also like an explanation why that is).
Is it possible that the chords could be derived from different scales as well? For example, jazz musicians know a lot of scales under their belt. Could each of those scales form a different type of chord progressions?
I may be asking this question in a strange way, considering that I only know basic music theory. So please take that into account as there may be a better way to ask this.
Answer: You will find that most chords can be derived from more than one scale - usually this will change the chord's function.
Consider the humble C major chord. It can be the Tonic (I) chord in C, the Subdominant (IV) in G and the Dominant (V) in F. The same is true for other major and minor chords. In other words, it's not enough to look at the root and type of the chord, we must also consider the surrounding chords to determine which scale/key the chord is derived from.
You specifically ask why chord progression assume major or minor scales. This isn't necessarily true. For example, the I-IV-bVII-I progression commonly found in rock songs is actually based on the Mixolydian mode, so G Mixolydian would give you G-C-F-G. Except, that G Mixolydian is a mode of C major, so if you really wanted to, you could bring it all back around to a major scale (it's not the sensible thing to do, because your "tonic" note in this case is G).
When we start looking at extended chords, we can see how they derive from particular modes. For example Cmaj7 can be viewed as a chord in either C major or C Lydian (G major), but G7 is unique to G Mixolydian (a C major mode) as are all further chords in the Dominant series.
Finally, it is possible to create highly clustered harmonies (usually very dissonant) implying a scale outside the minor/major system and its modes - possibly even pure chromaticism. It's not often done, because the results sound bad to our ears, but that is a cultural issue as much as a question of actual physics of sound (pretty much anything outside major/minor triads is dissonant to some extent).
To briefly address the resolution that led to this question, it is not that uncommon to introduce harmonic substitutions in order to achieve the desired effect and resolutions to a major tonic are often seen as more "satisfying" than resolutions to minor. Other things you're likely to encounter are V in a minor key (harmonic minor), IV and V in minor (melodic minor), the aforementioned substitution of bVII for V in major keys, and a whole host of others that may either change the quality of the key (as with the minor variants), imply multiple tonalities or even modulate to other keys. At the end of the day, what sounds "right" to the composer is what goes - theory may only catalogue such substitutions and attempt to explain them.
Major and minor keys form the basis of the Western musical system, so it's to be expected that most music will exploit these to a lesser or greater extent. At the same time, we've outgrown a lot of the once inviolable rules of harmony and quite a lot of modern music has departed from the old diatonic system entirely. Popular music styles like rock and jazz may largely work within the old system, but often bend it in interesting ways - if only because a lot of the songwriters never bothered to learn what they (supposedly) shouldn't be doing.
Answer: If that is the cadence the piece ends on then it very well may be A Picardy third, Picardy cadence or Tierce Picarde. That is to say a Cadence in a minor key that ends on a Major Tonic chord.
It traditional harmony this is only done at the very end of a piece but it could be that some popular music just liked the sound and went with it.
Read more here.
Comment: More commonly - tierce de Picardie.
Comment: I was wondering about the 'de' myself.

Chords derived from uncommon scales


How to define an array of objects conforming to a protocol?

Question: Given:
protocol MyProtocol {
    typealias T
    var abc: T { get }
}

And a class that implements MyProtocol:
class XYZ: MyProtocol {
    typealias T = SomeObject
    var abc: T { /* Implementation */ }
}

How can I define an array of objects conforming to MyProtocol?
var list = [MyProtocol]()

Gives (together with a ton of SourceKit crashes) the following error:
Protocol 'MyProtocol' can only be used as a generic constraint because it has Self or associated type requirements

Even though the typealias is in fact defined in MyProtocol.
Is there a way to have a list of object conforming to a protocol AND having a generic constraint?
Comment: possible duplicate of [Usage of protocols as array types and function parameters in swift](http://stackoverflow.com/questions/24888560/usage-of-protocols-as-array-types-and-function-parameters-in-swift)
Comment: @Paulw11 My bad, I completely missed that one.
Answer: The problem is about using the generics counterpart for protocols, type aliases.
It sounds weird, but if you define a type alias, you cannot use the protocol as a type, which means you cannot declare a variable of that protocol type, a function parameter, etc. And you cannot use it as the generic object of an array.
As the error say, the only usage you can make of it is as a generic constraint (like in class Test<T:ProtocolWithAlias>).
To prove that, just remove the typealias from your protocol (note, this is just to prove, it's not a solution):
protocol MyProtocol {
    var abc: Int { get }
}

and modify the rest of your sample code accordingly:
class XYZ: MyProtocol {
    var abc: Int { return  32 }
}

var list = [MyProtocol]()

You'll notice that it works.
You are probably more interested in how to solve this problem. I can't think of any elegant solution, just the following 2:

remove the typealias from the protocol and replace T with AnyObject (ugly solution!!)
turn the protocol into a class (but that's not a solution that works in all cases)

but as you may argue, I don't like any of them. The only suggestion I can provide is to rethink of your design and figure out if you can use a different way (i.e. not using typealiased protocol) to achieve the same result.
Comment: @David: I do not agree with that. Struct and classes with generics have the same problem. Other languages (C#, java, just to name a few) allow protocols with generics. And however an array of `MyProtocol` **do have** the same functionality - what is different is that each element can have the functionality implemented in a different way - but the same concept applies to using a base class as array element type and put instances of objects inherited from it.
Comment: This actually makes sense if you think about it.  Since the return type of abc isn't known at compile time, you can't reliably use the results in a type safe fashion.  An array of MyProtocol's makes no sense because they don't all have the same functionality.
Comment: @Antonio Thanks, as I see it casting is probably the only solution here. In my previous attempt I was indeed using a class one generic parameter, but Swift does not allow (yet) to have a non-generic type that inherits from a generic one – `XYZ: ABC` is not allowed.
Comment: @erudel Yeah that's another thing of swift that I miss... although not as important as having **usable** generic protocols.


HashMap doesn't copy object

Question: in Java I have a ResultSet object which I use to fill an HashMap Object, in this way:
<code>        rs = db.executeQuery(query);
        rs.last();
        rows = rs.getRow();
        rs.beforeFirst();
        String current;
        String[] current_columns = new String[rows];
        int x;
        for(int i=0; i<n_col; i++){ //scroll columns
            x=0;
            while(rs.next()){ // scroll rows
                current = rs.getString(columns[i]);
                current_columns[x++] = current;
            }

            //a little output debug
            System.out.print("hm.put("+columns[i]+", {");
            for(int j=0; j<current_columns.length; j++)
                System.out.print(current_columns[j]+" ");
            System.out.println("});");
            //end of output debug

            hm.put(columns[i], current_columns);

            rs.beforeFirst();

        }
</code>
The output debug prints:

hm.put(a, {a1 a2} );
hm.put(b, {b1 b2} );

So, if I write the instruction hm.get("a") it should return me the array string {"a1", "a2"}, while if I write hm.get("b") it should return me the array string {"b1", "b2"}.
But, in practice, when I try to get the values, the hashmap returns me always the last array that I put, regardless of the key.
So if I write the instructions:
<code>System.out.println(hm.get("a")[0]);
System.out.println(hm.get("b")[0]);
</code>
it prints:

b1
b1

Why does the hashmap have this behavior ? Where is the problem ?
Comment: What if you print the _actual `HashMap`_, rather than your debug output? I think you have a bug elsewhere in your code.
Answer: You are always using the same array so all the keys are mapped to the same object, you should declare it inside the loop and not outside.
<code>    int x;
    for(int i=0; i<n_col; i++){
        // This time you declare a NEW array each time
        String[] current_columns = new String[rows];
        x=0;
        while(rs.next()){
            current = rs.getString(columns[i]);
            current_columns[x++] = current;
        }

        System.out.print("hm.put("+columns[i]+", {");
        for(int j=0; j<current_columns.length; j++)
            System.out.print(current_columns[j]+" ");
        System.out.println("});");

        hm.put(columns[i], current_columns);

        rs.beforeFirst();
    }
</code>
Comment: That's precisely the problem : he is writing several times into a single array, thus overriding the previously written values
Comment: Yes it works, thank you very much.
I thought that when I put an object into a hashmap it would be copied, instead pretty much the pointer is passed.

HashMap doesn't copy object


Find two points on two lines in the plane where the line between the two points go through a third point and are equidistant from that point

Question: I have the following situation (see pic below). I have two lines $B$, $C$, in the plane, the intersection point $a$, and a point $p$. I need to find the points $b$ and $c$ along $B$ and $C$ such that the line $A$ goes through $p$ and the two segments on each side of $p$ ($pb$ and $pc$) are of equal length, i.e., so that $l_1 = l_2$.

I have tried setting up various equation systems with three unknowns (the lengths $|bc|$, $|ac|$, and $|ab|$), using the Cosine Law but I only get really complicated algebraic equations when trying to solve them.
Any input would be helpful.
Answer: Reflect $C$ in $p$, obtaining reflected line $C'$.  Let $c$ be the intersection of $C'$ and $B$.
Answer: I tried using analytic geometry, exploiting the fact that one can always situate $a$ at the origin and $p$ at $(0,1)$ in cartesian coordinates.  Then the two lines $B$ and $C$ are simply of the form  $y = \alpha_b x$ and $y = \alpha_c x$ and line $A$ is $y = \alpha_a x + 1$; we have solved the problem if we determine $\alpha_a$ in terms of $\alpha_b$ and $\alpha_c$ since we can alwyys rotate and rescale our original problem to fit this pattern.
Well,
$$
A \cap B = \left( \frac{1}{\alpha_b - \alpha_a},\frac{\alpha_b}{\alpha_b - \alpha_a} \right)  \\
 A \cap C = \left( \frac{1}{\alpha_d - \alpha_a},\frac{\alpha_c}{\alpha_d - \alpha_a} \right)
$$
Now we write down the expressions for $|pa|^2$ and $|pb|^2$, and cross multiply by the product of the denominators, to get
$$ (\alpha_b-\alpha_a)^2 (1+\alpha_a^2) = (\alpha_c-\alpha_a)^2 (1+\alpha_a^2)$$
Then
$$(\alpha_b-\alpha_a)^2 = (\alpha_c-\alpha_a)^2 \\
\alpha_a = \frac{\alpha_b+\alpha_c}{2}$$
Well, well, that is a pleasant surprise.  THe geometric construction of line $A$ given point $p$ and lines $B$ and $C$ passing through point $a$ is now clear:

Construct line $ad \perp pa$; $d$ can be any arbitrary point on this line other than $a$.
Construct line $fd \perp ad$.  Label the intersection of $df$ with line $B$ as $e$, and the intersection of $fd$ with line $C$ as $f$.
Construct point $g$ as the midpoint of line segment $ef$.
Draw line $ag$.
Construct a line through $p$ which is outside line $ag$, parallel to line $ag$. Label that line $A$ because it is the desired line.
Answer: You can ensure equal distances by considering circles around point $P$ and then looking for the intersection points $B$ and $C$ with the lines $c$ and $b$.
(Note: I use upper case letters for points, lower case letters for lines).
Here are some of these:

The interesting question is if we have a circle radius $R$ for which the vectors $u = PB$ and $v = PC$ are antiparallel $u = -v$.
The good news for this image is that there should be one, as the green and light blue cases show, there should be a feasible circle in between.
I do not know a good constructive way how to find that circle, in the above scenario one would try a circle and see if the lines along $PB$ and $PC$ are identical. Or one might look, if the line through $BC$ intersects with $P$.
Another analytical criteria would be that the area of the triangle $PBC$ vanishes.
Comment: Sorry for not mentioning it in the question but I have considered this. It solves the problem but it requires an iterative solution and I was looking for a solution in constant time. Thanks anyway for your input.
Comment: I prepared an analytic solution, but stopped after I saw Steven's solution which looked very simple. It seems to be constructable.


Storing Excel templates in my Access database

Question: What I want:
I am working with an MS Access database to produce a report for the Ontario Professional Engineering University Accreditation board. The PEO have their templates in MS Excel and I MUST use their exact format.
I want to output the data from the Access DB to the Excel sheet (easy enough), but I want to copy their formatting when producing a new file. (I do NOT want to keep an empty template file and copy it each time).
So essentially I'm looking to somehow store the template in code. (Other suggestions are welcome!)
What I've tried:
Some of you will read this and think i'm an idiot. But what i tried was to get the data from excel on the MS Clipboard through the API Code and store the DataObject as (i was hoping) some sort of string.
But i could not preserve the formatting or cell shading from the original MS Excel template.
Any suggestions?...
Comment: What version of Access are you using?
Answer: You could store the Excel template(s) within the database by creating a table that includes an Attachment field

creating a record and saving the Excel template as an attachment to that record

and then using VBA code like this to save a new copy of the Excel document to disk whenever you need one:
Option Compare Database
Option Explicit

Public Sub SaveReportTemplateToFile()
    Dim cdb As DAO.Database, rowRst As DAO.Recordset, attachRst As DAO.Recordset2, attachField As DAO.Field2
    Set cdb = CurrentDb
    Set rowRst = cdb.OpenRecordset("SELECT TemplateFile FROM ReportTemplates WHERE ID=1")
    Set attachRst = rowRst.Fields("TemplateFile").Value
    Set attachField = attachRst.Fields("FileData")
    attachField.SaveToFile "C:\Users\Gord\Desktop\" & attachRst.Fields("FileName").Value
    Set attachField = Nothing
    attachRst.Close
    Set attachRst = Nothing
    rowRst.Close
    Set rowRst = Nothing
    Set cdb = Nothing
End Sub

Comment: This looks FANTASTIC! So storing an attachment in a table means the DB has a hard copy? So i do not need to keep the .xls file with the Access DB?
Comment: @user3163380 Yes, the attachment file is saved as an object within the database.
Comment: Thank you this is a great answer to my problem! And thank you for the screenshot images they were EXTREMELY helpful
Comment: Gord, you have just made my life a hell of a lot easier! Thank you
Answer: What you are going to have to do is first export all the data into a new sheet of an empty template, then link all the data to the correct place on the template and then remove the sheet with all the data, keeping the data on the template.
As far as I know there is no quicker way to do this, but here is an example: http://www.rogersaccesslibrary.com/forum/topic350.html
It will be quite a job, but doable, if the template is well setup.
Comment: Thank you for your answer, but as i mentioned; I want to somehow avoid keeping the Excel template all together.

I would like to replicate the templates format programatically, that way if i move the Access DB i do not have to keep it tied with an external Excel file.

(Also this will avoid the song-and-dance of copying into a new sheet of an empty template then linking to the other sheet)
Comment: Why not store the template in the Access DB ?  Replicating the template entirely via code seems like a lot of work for no obvious benefit.
Comment: @TimWilliams Yes, I did not know you could do this. I accepted this as the best answer (from Gord T). Thank you for the suggestion!


Verificar se existe um arquivo em determinada pasta rendered jsf

Question: Pessoal estou salvando imagem em uma determinada pasta do projeto e preciso verificar se esse arquivo existe na pasta. Estava usando um comando conforme abaixo:
rendered="Fotos/#{consultaFuncionariosBean.pessoaModel.codigo != null}.png"

Mas não funcionou. Alguém sabe como posso resolver isso?
Resolvido conforme Abaixo.
Metodo.
public boolean existeArquivo(String file) {
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    String nomeSaida = externalContext.getRealPath("") + "resources" + File.separator + "Fotos" + File.separator + file + ".png";
    File Arqs = new File(nomeSaida);
    boolean success = Arqs.exists();
    return success;
}

Form.

Answer: O atributo rendered é Booleano.

Faça algo do tipo:
@ManagedBean
public class TesteMB {

    private File arquivo;
    private boolean arquivoExiste;

    public boolean getArquivoExiste() {
        return arquivo != null;
    }
}

E então:
rendered="#{testeMB.arquivoExiste}"

Ou para ficar mais fácil, pode fazer a verificação direto na view:
@ManagedBean
public class TesteMB {

    private File arquivo;

    public File getArquivo() {
        return arquivo;
    }
}

E então:
rendered="#{testeMB.arquivo != null}"

Comment: Obrigado Igor Resolvi conforme postado acima e de acordo com a sua ajuda grato.


No. of rows 1 less than expected in a jagged array

Question: While printing this jagged array I am getting the no of rows to be 1 less than expected. It should start with 0th index to (h-1)th index creating a total of h rows. What am I doing wrong?
h is the no. of rows.
        int h = int.Parse(Console.ReadLine());

        int[][] arr = new int[h][];

        for(int i = 0; i < h; ++i)
        {
            arr[i] = new int[i+1];
        }

        for(int i = 0; i < h; i++)
        {
            Console.WriteLine();

            for(int j = 0; j < i; j++)
            {
                Console.Write(arr[i][j] + " ");
            }
        }

Comment: How many rows did you expect and how many rows did it actually print?
Answer: That's because your inner for-loop has the condition j < i. If i is 0 in the first pass, the inner for-loop will not be passed.
Try it with
for(int j = 0; j < arr[i].Length; j++)
{
  Console.Write(arr[i][j] + " ");
}

Comment: Yup, this is it!
Answer: The arrays have a growing list of elements, starting with 1 so if you want to scan all the items:
for(int i = 0; i < h; i++)
{
    Console.WriteLine();

    for(int j = 0; j < (i + 1); j++)
    {
        Console.Write(arr[i][j] + " ");
    }
}

Comment: Again, another correct answer, sorry guys I don't seem to be able now to delete my answers without flagging.
Answer: Shouldn't it be:
new int[i];

Instead of:
new int[i+1]

Or is it h - 1? Just change that index of the array.
Or you need j < arr[i].Length