Métodos HTTP na prática

Question: Já vi a documentação do W3C algumas perguntas aqui no SOpt:

Quais são os métodos de requisição HTTP, e qual é a diferença entre eles?
Quais as vantagens de se utilizar os métodos HTTP corretos?
Por que os navegadores não implementam os protocolos PUT e DELETE do HTTP?
Qual é a diferença entre o método PUT e o POST?

Porém achei muito conceitual, ainda tenho algumas dúvidas de uso na prática, por exemplo, se eu tenho um sistema simples de usuários, eu possuo basicamente 5 operações: inserção (POST), busca (GET), alteração (PUT), exclusão (DELETE) e autenticação (a autenticação verifica os dados de um usuário e a busca pega os dados de todos usuário, sem necessitar dos seus dados de acesso). Se eu estiver errado me corrijam por favor

Qual método eu vou usar para autenticação?
Considerando que tenho 5 ações e que, de acordo com as respostas das perguntas mencionadas, existem 9 métodos "oficiais", quando eu vou utilizar os demais?
Se eu utilizar o método GET para pegar algo muito importante, e, por isso, os dados do usuário devem ser novamente informados (usuário e senha), devo passar isso pela url (examplo.com/dado/usuario/senha)?
Comment: Resposta rápida: conceitualmente a autenticação não faz parte do recurso de usuários. Seria melhor você separar as responsabilidades e criar um serviço próprio, assim podendo utilizar todos os métodos que for necessário para a autenticação.
Comment: @AndersonCarlosWoss não entendi essa resposta rápida, pode explicar melhor?
Answer: Com o grande aumento do uso do protocolo HTTP em API esses métodos vieram mais a tona.
Em transações diretas você pode usar o método que você quiser, porém alguns tem suas restrições, como o GET e o DELETE, por exemplo, não pode ter um corpo na requisição. Porém algumas linguagens essas restrições podem serem feridas. A importância dos diferentes métodos é um protocolo de compreensão do objetivo da requisição. E isso irá ajudar na compreensão dos recursos da sua aplicação. Vamos aos métodos do protocolo:
Métodos padrões do protocolo HTTP

GET: Método de requisição de informação. Esse método é útil para buscar alguma informação requisitada tanto pelo caminho http://meusite/api/usuarios ou http://meusite/api/usuarios/235 por um filtro http://meusite/api/usuarios?status=ativo. Como o objetivo é retornar alguma informação, então esse método não deve esperar um corpo na requisição. Assim o método terá uma transação mais rápida na requisição.
POST: Método de inserção de informação. É um método de extrema utilidade, pois é um método que irá executar uma ação. O método não é obrigatório um corpo nem na requisição e nem na resposta. Deve entender esse método como terá uma execução no servidor. Exemplos:
1 - Inserir valores: Executando um POST em http://meusite/api/usuarios sendo no corpo os valores do novo usuário. Dessa forma sabemos que será inserido a informação do corpo na listagem de usuários.
2 - Atualizar valores: Por mais que temos o método PUT, não é considerado quebra de protocolo usar o POST para fazer uma atualização de dados. Sendo por isso que os browser não utilizam o método PUT, pois no início do protocolo HTTP só existia os métodos GET e POST.
3 - Executar alguma funcionalidade: Caso haja alguma execução de cálculo ou de funcionalidade mais especifica (não sendo uma consulta, pois isso é o GET) utiliza o protocolo POST para essa execução. Um exemplo seria http://meusite/api/usuarios/parabenizar-aniversariantes. Isso não quer dizer que essa funcionalidade haja um corpo tanto na requisição quanto na resposta.
PUT: É um método específico para atualizar valores. Não seria correto utilizar o PUT para execução de funcionalidade, como vimos no POST. Então terá como padrão um corpo na requisição e o caminho terá, por segurança, a chave referência que dos dados que estão sendo atualizado http://meusite/api/usuarios/234. Dessa forma o protocolo recusará as requisições que vierem sem uma chave.
PATCH: Um método de auxílio do PUT é o PATCH. Também tem como objetivo atualizar valores de uma referência chaveada no caminho http://meusite/api/usuarios/234, porém o corpo não terá o objeto completo. Sendo assim a atualização dos valores são parciais. Um exemplo é no processo de atualizar senha, não precisa mandar o corpo todo para a mudança de senha. Assim terá uma segurança e uma validação melhor em caso de mudança de senha e evita de incluir if no PUT.

Um exemplo em comparação entre o PUT e o PATCH teremos um objeto Usuário em que terá a seguinte corpo completo:
{
"Id":"7e8f2ae6-a5b1-41e7-b9fd-2ae8863fa39d",
"Nome":"Joãozinho",
"Email":"email@example.com",
"Login":"joaozinho",
"DataNascimento":"01/01/1990"
}

Para executar uma mudança de dado completa você utilizaria o PUT. Sendo assim a requisição seria (Não considerarei nenhum protocolo de Authorization):

Método: PUT
Url: http://meusite/api/usuarios/7e8f2ae6-a5b1-41e7-b9fd-2ae8863fa39d
Heard: ["Content-Type":"application/json"]
Body:
{
    "Id":"7e8f2ae6-a5b1-41e7-b9fd-2ae8863fa39d",
    "Nome":"Joãozinho Sauro",
    "Email":"firstname.lastname@example.com",
    "Login":"joaozinho",
    "DataNascimento":"01/01/1990"
}

Já o PATCH seria uma compreensão que o processo utilizado nesse método é para uma alteração específica. Como exemplo farei um processo de alteração de login, pois iremos compreender que no lado do servidor ele terá um validador de login disponível, por exemplo. Então a requisição ficaria da seguinte forma:

Método: PATCH
Url: http://meusite/api/usuarios/7e8f2ae6-a5b1-41e7-b9fd-2ae8863fa39d/mudar-login
Heard: ["Content-Type":"application/json"]
Body:
{
    "Login":"joaozinho30"
}

Como já disse, o protocolo não exija que você tenha que fazer dessa forma. Ele é útil para compreensões melhores de cada funcionalidade que seu serviço está oferecendo. Mas caso queira fazer esses dois exemplos no POST você pode. Onde a diferença de incluir um novo Usuário e atualizar os dados do Usuário estará na URL. Então isso fará parte da decisão arquitetural do projeto em qual padrão a equipe irá seguir. Mas caso o seu projeto será de uso público, será muito bem aceito se o serviço respeitar fielmente o protocolo.
DELETE: Apesar de ser um método novo para os primórdios do HTTP, mas, como o GET, é um método que não se deve passar um corpo. Dessa forma o processo ganha uma velocidade maior na requisição, já que o objetivo é simplesmente uma limpeza. Dessa forma o seu processo terá uma organização melhor, pois saberá que as exclusões estarão somente no método DELETE.
OPTIONS: Um método de uso maior dos browser. Quando utiliza o protocolo HTTPS por padrão os browser irão requisitar quais os métodos ele poderá utilizar com a url do momento. Dessa forma o processo irá retornar somente a lista de métodos permitidos executar, por exemplo POST, GET, PUT, DELETE.
HEAD: Não bastando a melhoria do tempo de transação do POST para o GET, o método HEAD terá um ganho a mais. Pois o objetivo é a informação que contenha no HEAD da resposta. Isso é muito útil para atualização de Token, por exemplo.
CONNECT: É um recurso do protocolo HTTP para que abra um túnel de conexão direta. Isso é útil para caso de processo de extrema segurança que irá abrir um espécie de VNP para comunicação.
TRACE: Um processo simples para testar a comunicação. Ele é útil para caso de uma execução muito demorada sendo processada por outros métodos e assincronamente o TRACE irá validade se ainda há comunicação com o servidor ou a demora é devido a uma queda de comunicação.

Guilherme, sendo assim você pode colocar a autenticação no método que você quiser, mas por padrão de protocolo e para facilitar a manutenção para os novos membros da equipe é melhor usar o método POST.
Bom, espero que eu atendi a sua expectativa. Mas foi de grande treino responder essa questão.
OBS: Já houve uma resposta para essa questão em outra postagem https://pt.stackoverflow.com/a/9421/114084, então complementei um pouco mais a minha compreensão para que possa compartilhar o meu entendimento.
Comment: O link que colocou está na pergunta e foi um dos que achei muito conceitual, gostei da resposta já que explica o que e mostra um exemplo prático, porém ainda estou com dúvida em relação ao **PATCH** poderia explicar um pouco mais? Como eu o uso e qual a diferença para o **PUT**?
Comment: fiz uma [pergunta](https://pt.stackoverflow.com/questions/301436) relacionada ao corpo das requisições, acredito que possa te interessar, ou a outros que estão pesquisando sobre o HTTP
Comment: Guilherme, eu fiz uma edição colocando um exemplo entre o **PUT** e o **PATCH**. Veja se ficou melhor.
Comment: Quanto a questão do corpo, no protocolo HTTP a requisição você pode fazer da forma que você quiser. Ninguém poderá te dizer que não poderá ter corpo, ou que a URL está errada, ou qualquer outra coisa. O que acontece que em algumas linguagens (*Java* ou *.Net*, por exemplo) não lhe entregará o corpo em caso de uso do método **GET**. Você poderá burlar isso, mas será um custo de programação muito cara ao invés de simplesmente aceitar o protocolo normal.
É meio chatinho as exigências do protocolo, mas quando acostuma fica até mais fácil de trabalhar. rs
Comment: Entendi melhor, porém no exemplo surgiu uma última dúvida (espero), se eu quero que, quando um usuário for cadastrar ou alterar o campo username, fazer uma chamada pra api de acordo com o que ele digita para verificar se aquele usuário está disponível. Essa chamada é GET ou PATCH ou outro?
Comment: A consulta de login disponível, como o exemplo, por ser um *consulta* o método deveria ser o **GET**. O login consultado seria informado por Query String (parâmetro na URL). Pois o que está fazendo é somente uma consulta no banco. Agora, se uma consulta necessitar de uma execução de um calculo ou uma mudança de dados, pelo lado do servidor, assim seria um conceito para o **POST**.
Fique a vontade com as dúvidas!!
Answer: TL;DR: não é que precise existir um recurso para autenticação, porém precisa de autorização para que você manipule um recurso.

O primeiro conceito a se entender do REST é o recurso. Ele é qualquer coisa a ser transferida na web.

De forma metafórica, pense em uma biblioteca. Ela possui diversos recursos: livros, pessoas, funcionários.
GET
É muito simples pensar no REST dessa forma. Se quero saber sobre um livro, eu posso fazer:
GET api.biblioteca.com/livros/iliada

{
  "autor": "Homero",
  "título": "Ilíada",
  "descrição": "A Ilíada (em grego antigo: Ἰλιάς, IPA: [iːliás]) é um dos dois principais poemas épicos da Grécia Antiga, de autoria atribuída ao poeta Homero, que narra os acontecimentos decorridos no período de 50 dias durante o décimo e último ano da Guerra de Troia, conflito empreendido para a conquista de Ílion ou Troia, cuja gênese radica na ira (μῆνις, mênis) de Aquiles."
}

Se quero todos os livros de Homero, faço parecido, mas com uma query string:
GET api.biblioteca.com/livros?autor=Homero

[ { ... }, { ...} ]

Ou dependendo do design da sua API, poderia ser também: api.biblioteca.com/autor/homero/livros
Pode-se concluir que o método HTTP GET, em REST, é utilizando para retornar um ou mais recursos.
DELETE
Se você precisa tirar um livro da biblioteca, é só utilizar o DELETE.
DELETE api.biblioteca.com/livros/iliada

POST
Esse método envia dados para o servidor. No REST, utilizamos para criar um novo recurso.
Por não ser idempotente, não se pode cachear a resposta de uma requisição como esta.
POST api.biblioteca.com/livros

{
  "autor": "William Shakespeare",
  "título": "Macbeth",
  "descrição": "Macbeth é uma tragédia do dramaturgo inglês William Shakespeare, sobre um regicídio e suas consequências. É a tragédia shakespeariana mais curta, e acredita-se que tenha sido escrita entre 1603 e 1607. O primeiro relato de uma performance da peça é de abril de 1611, quando Simon Forman registrou tê-la visto no Globe Theatre, em Londres. A obra foi publicada pela primeira vez no Folio, de 1623, possivelmente a partir de uma transcrição de alguma performance específica."
}

PUT
Assim como o POST, é utilizado para enviar dados ao servidor. No REST, é utilizado para atualizar recursos únicos, como um livro em específico.
Por ser idempotente, a resposta das requisições podem ser cacheadas.
PUT api.biblioteca.com/livros/arte-da-guerra

{
  "autor": "Sun Tzu",
  "título": "Arte da Guerra"
}

PATCH
Assim como o PUT, é utilizado para enviar dados ao servidor. No REST, é utilizado para atualização de recursos parciais. Como por exemplo, adicionar ou atualizar um só campo em uma entidade.
Pode ou não ser idempotente, por isso pode ser um método um pouco confuso.
PATCH api.biblioteca.com/livros/arte-da-guerra

{
  "descrição": "A Arte da Guerra (chinês: 孫子兵法; pinyin: sūn zĭ bīng fǎ, literalmente "Estratégia Militar de Sun Tzu"), é um tratado militar escrito durante o século IV a.C. pelo estrategista conhecido como Sun Tzu. O tratado é composto por treze capítulos, cada qual abordando um aspecto da estratégia de guerra, de modo a compor um panorama de todos os eventos e estratégias que devem ser abordados em um combate racional. Acredita-se que o livro tenha sido usado por diversos estrategistas militares através da história como Napoleão, Zhuge Liang, Cao Cao, Takeda Shingen, Vo Nguyen Giap e Mao Tse Tung."
}

POST vs PUT vs PATCH
Existem inúmeras discussões comparando os dois métodos e seu uso. É importante ressaltar que o REST é somente um padrão, e que não resolve todos os casos.

Use POST para criação de novos recursos em endpoints de coleção, como api.biblioteca.com/livros.
Use PUT para atualização de recursos em endpoints de entidade, como api.biblioteca.com/livros/iliada.
Use PATCH para atualização parcial de recursos, também em endpoints de entidade.

Em resumo...

GET
POST
PUT
PATCH
DELETE

Requisição aceita body?
Não
Sim
Sim
Sim
Sim

Resposta aceita body?
Sim
Sim
Sim
Sim
Sim

Altera estado dos recursos?
Não
Sim
Sim
Sim
Sim

É idempotente?
Sim
Não
Sim
Não
Sim

É cacheável?
Sim
Não*
Não
Não
Não

* depende do que vier nos headers
Ok, mas e a autenticação?
Como comentei, o REST não resolve tudo, e nem deve. Nem tudo são recursos ou podem ser representados como um. Aí vai de implementação para implementação.
Existem diversas formas de autenticar utilizando HTTP, e a grande maioria não tem nada a ver com método HTTP, e sim sessões, cookies ou cabeçalhos HTTP.
O ponto é: não é que precise existir um recurso para autenticação, porém precisa de autorização para que você delete um livro, por exemplo.
Autenticação básica com HTTP

A dica de ouro, primeiramente, é estar utilizando HTTPS. A cada requisição feita, um cabeçalho HTTP indicando um token de autenticação.
DELETE api.biblioteca.com/livros/arte-da-guerra
Authorization: Basic QWxhZGRpxjpvcGVuIHNlc2FtZS==

Recomendo ler mais sobre esse método de autenticação na MDN.
Comment: Sua resposta virou referência para REST, vou por ela nas leituras obrigatórias daqui da empresa.
Comment: Fico feliz de ouvir isso, @JeffersonQuesado!
Answer:
Qual método eu vou usar para autenticação?

Para responder aquilo que comentei e não foi abordado nas outras respostas.
Recurso de usuário é diferente de recurso de autenticação. Imagine que você possua o recurso de usuário sob a URI /user, então:

GET /user busca informações de usuário;
POST /user cria um novo usuário;
PUT /user atualiza/cria dados do usuário;
DELETE /user exclui um usuário;
PATCH /user atualiza um usuário;

Como autentica um usuário? Com outro recurso, por exemplo, /auth.

GET /auth busca informações de uma autenticação;
POST /auth cria uma nova autenticação;
etc;

Se você deseja autenticar um usuário cadastrado, basta efetuar um POST para o recurso de autenticação. Normalmente esta requisição irá gerar um token que identifica qual é a autenticação do usuário e, normalmente, esse token é armazenado nos cookies do cliente.

Considerando que tenho 5 ações e que, de acordo com as respostas das perguntas mencionadas, existem 9 métodos "oficiais", quando eu vou utilizar os demais?

As outras respostas abordam esse assunto.

Se eu utilizar o método GET para pegar algo muito importante, e, por isso, os dados do usuário devem ser novamente informados (usuário e senha), devo passar isso pela URL (exemplo.com/dado/usuario/senha)?

Não, os dados do usuário para autenticação devem ser passados exclusivamente para o recurso de autenticação. Ter que passar isso a outro recurso seria redundância e prejudicaria a atomicidade dos seus recursos. Se outro recurso precisa de autenticação para acessá-lo, você deve passar apenas o token de autenticação, através dos cabeçalhos da requisição, e validá-lo no lado do servidor. A resposta do vnbrs exemplifica muito bem isso na parte que fala da autenticação.
Comment: Não é o caso, mas estaria semanticamente incorreto usar `user|client|worker/auth`, se por exemplo eu tenho autenticação de usuários, funcionários e clientes e cada uma tem uma forma diferente?
Comment: Não, perfeitamente aceitável. A URI é opaca, ela não reflete nenhuma lógica implementada no servidor, então apenas por possuir `user` no caminho não significa, necessariamente, que pertence ao recurso de usuários.
Answer: Olá,
Vou tentar ir direto ao ponto, respondendo apenas as suas perguntas.

Qual método eu vou usar para autenticação?

Geralmente usa-se o método POST. Com certeza a autenticação é um conceito que não parece ser encaixar muito bem em qualquer método HTTP, mas normalmente adota-se o POST nestes casos :)

Considerando que tenho 5 ações e que, de acordo com as respostas das perguntas mencionadas, existem 9 métodos "oficiais", quando eu vou utilizar os demais?

Infelizmente você não vai conseguir sempre achar um método HTTP adequado para todas as ações que deseja fazer no sistema. Inclusive, tome cuidado ao utilizar alguns dos métodos HTTP mais raros, pois você pode ter problemas inesperados com firewall entre outras surpresas de ambiente.
Normalmente o pessoal se prende aos mais clássicos: GET, POST, PUT, DELETE e (mais raro) PATCH. O PATCH é usado quando o usuário quer atualizar apenas informações específicas da entidade HTTP e não quer enviar todas as informações a cada atualização, como é feito no PUT.

Se eu utilizar o método GET para pegar algo muito importante, e, por isso, os dados do usuário devem ser novamente informados (usuário e senha), devo passar isso pela url (examplo.com/dado/usuario/senha)?

Passar o usuário e senha pela URL é algo que nunca deve ser feito. Se determinado endpoint de consulta precisa do usuário e senha, esta informação deve ser passada no corpo da mensagem (body). Como o GET não aceita um body (ou não deveria aceitar..), usa-se POST mesmo. Exemplo:
POST /api/consultar-dado-importante/123

Body:
{
    "usuario": "user", "senha": "123456"
}

Usar POST ao invés de GET nestes casos é uma exceção a regra.
No mais, fica a minha dica: você não vai encontrar uma resposta pronta de como usar o método ou código HTTP correto para cada problema no seu projeto. Tente apenas escolher um padrão na sua API e seguir ele o tempo todo, criando um padrão interno.
Se tiver mais alguma dúvida ou algo que não ficou claro, use a área de comentários que posso esclarecer melhor alguns pontos.
Answer: geralmente tem sim, sempre várias operações que você pode fazer nesse caso.
Mas as mais utilizadas é o GET e o POST conforme você ja deve ter lido, provável que já tenha ciência do que faz cada uma:
GET = Mais RAPIDO, porém MENOS SEGURO.
POST = Mais LENTO, porem MAIS SEGURO.
ou seja, use get para passar alguma informação que não seja relevante para o servidor atraves da url. e use o post para passar alguma informação como senha, ou numero de cartão de credito, algo que seja mais confidencial que voce nao pode deixar visivel pela url.
Acredito que você agora entenda melhor qual deve usar.
Comment: Eu também fiquei curioso com essas afirmações. Por quais motivos afirva que um é mais rápido que outro e porquê um é mais seguro? Ambos enviam a informação através de texto, então qual seria a diferença na segurança?
Comment: Sua resposta está um pouco vaga e responde apenas o que não foi perguntado (GET vs POST), acrescente mais dados, por exemplo, porque é mais ou menos rápido e seguro? (desculpe se pareço ingrato não é a intenção :) )

Métodos HTTP na prática


How to get sqlite3 into Samsung Galaxy Tab

Question: I successfully rooted my Galaxy Tab and now I'd like to debug a SQLite database of my application using sqlite3 command on the device but it is not available there.
Is there any way how to install/inject this tool to the device, please?
Thank you for your answers.
Comment: You might check out this link:http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
Comment: Thank you. Although this blog is quite unrelated to the question, the link in the end of discussion pointed me to an idea that I can download the database to PC, modify it there and then upload it back to the device. It's not perfect but I can live with that.
Answer: If you are rooted on your galaxy tab, you can install Busybox, Connectbot and Terminal IDE from the Samsung Market, then you can install linux (i.e. google "sourceforge linux on android") which is packaged with python. I am running unbuntu 12 precise from sourceforge on my galaxy tab 10.1. Ubuntu12.04 beta comes with both python 2.7 and python 3.2.2. Python can implement sqlite3.


Method or property?

Question: I have a class, which I'll refer to as SpiderNest, that has a property of type List<Spider>, where Spider is a type of object with a property that is of type int; we'll call this property NumberOfLegs. I want either a method or property on the SpiderNest class to get the sum of the number of legs for all the spiders in my nest.
Would it be preferred to use a property like this (forgive poor object naming):
class SpiderNest {
    // Our example property.
    public List<Spider> Spiders { get; set; }

    public int TotalLegNumber
    {
        get { return Spiders.Sum(spider => spider.NumberOfLegs); }
    }

Or a method?
class SpiderNest {
    public List<Spider> Spiders { get; set; }

    public int GetTotalNumberOfLegs()
    {
        return Spiders.Sum(spider => spider.NumberOfLegs);
    }
}

And why would you choose that way? I know the question may be finicky but whenever I'm presented with two ways of doing something, there's usually benefits to each way of doing things. Thanks SO!
Answer: The method has no side effects, however could be computationaly expensive if Spiders is large.  If Spiders is large then it should be a method, otherwise make it a property.
Comment: To add a bit of clarification to Justin's answer: When you implement something as a property, you're hinting to your classes consumer that this is a relatively cheap and straightforward operation. This isn't set in stone, but when seeing a `NumberOfLegs` property or a `GetNumberOfLegs()` method, my instinct for the latter tells me it's an expensive operation whose results should be cached, while the former should be trivially repeatable. Be aware of those expectations.
Comment: That seems to be a very good rule of thumb. Thanks!
Answer: Have a look at Choosing Between Properties and Methods on MSDN

Properties are meant to be used like fields, meaning that properties should not be computationally complex or produce side effects.
Do use a method, rather than a property [if] ...the operation is orders of magnitude slower than a field set would be [or] ...the operation has a significant and observable side effect.

Let's consider an example - if your List is populated from a database with lazy loading, so when you make a call to your list it is populated from the database - this means that when you ask for the total number of legs you could be waiting for the information to be populated - which is not an instant operation. In this case you would be better off using a method.
Additionally I always feel that saying GetXXX() implies that the method could return a different value at different points in execution whereas a property feels like it should be more of a static number. On this basis I would err towards GetXXX() if you plan to be adding spiders to your nest after initiation.
Answer: Another hint would be if it would produce side effects (like changing another property or some internal state). If yes, make it a method even if it is cheap.
In your specific case i would make it a property. If the calculation could take long (due to the fact the list constains int.MaxValue items) i would create within the property itself a cache that will calculate this value once when it will be called the first time or when the list has been changed since the last calculation of it. Or maybe calculate beforehand every time the sum when the list changes and within your property you simply return the latest calculated value. But all these possibilities are depending on the design of the list elements (e.g. are they immutable or implementing INotifyPropertyChanged?) and how often the list changes vs. how often will the sum be needed by a client.

Method or property?


How to join same table to get parent ID data in Sequelize-Typescript?

Question: I'm using Sequelize with the Sequelize-Typecript decorators, and I want to join data from the same table but getting the data from a parent ID.
This a test model called "Element"
<code>@Table({
    timestamps:false,
    tableName: "element"
});

export class Element extends Model {

    @Column({
        type: DataType.INTEGER,
        primaryKey:true,
        allowNull:false,
        autoIncrement:true
    })
    id!:number;

    @Column({
        type: DataType.STRING,
        allowNull:false,
    })
    name!:string;

    @ForeignKey(() => Element)
    @Column({
        type: DataType.INTEGER,
        allowNull:false,
    })
    parent_id!:number;

    @BelongsTo(() => Element)
    parent_element!: Element;
}
</code>
By using Element.findAll(), I want to get this example result (as a JSON):
<code>"element": [
        {
            "id": 1,
            "name": "Car",
            "parent_id": null,
            "parent_element": {}
        },
        {
            "id": 2,
            "name": "Engine",
            "parent_id": 1,
            "parent_element": {
                 "id": 1,
                 "name": "Car"
                 "parent_id": null
            }
        }
]
</code>
But instead, "parent_element" data gets ignored, like that:
<code>"element": [
        {
            "id": 1,
            "name": "Car",
            "parent_id": null
        },
        {
            "id": 2,
            "name": "Engine",
            "parent_id": 1
            }
        }
]
</code>
So, I wonder how to get the data of the parent_id from the same table. Any help is welcome.
Answer: You have to include the element again inside your findAll call. It will return the data of the parent element.
<code>const data = await Element.findAll({
     include:[{model:Element}],
   });
</code>


Is there a unique solution for this equation..?

Question: I need to solve the below mentioned equation and try to find a unique solution for $\epsilon$ for the range between (-1,1) in terms of $n$.
$$\begin{equation}
\sum_{j=1}^{n-1} \frac{(2{\alpha_j} + 1)( \alpha_j -1)}{{\bigg[(\alpha_j - 1) \bigg((2{\alpha_j}+3) - \epsilon(2{\alpha_j}+1)\bigg) \bigg]\,\,\,}^{2}}  = 0
\end{equation}
$$
where
$\alpha_j=\cos\frac{2{\pi}j}{n}$.
With simulations, I can see that there's a unique solution for $\epsilon$ when $\epsilon$ is in (-1,1) and I need to find that as an expression in terms of $n$. Any suggestions
Comment: I removed the (soft-question) tag, which is for questions that don't admit a definitive answer. Please read the tag summaries that are offered in the tag selection menu when selecting tags.
Answer: Your sum has following closed form:
$$\frac{\left( n-1 \right)  \left( 2\,\cosh \left( 2\,{\frac {\pi }{n}}
 \right) +1 \right)}  {\left( \cosh \left( 2\,{\frac {\pi }{n}} \right)
-1 \right)  \left( 2\,\cosh \left( 2\,{\frac {\pi }{n}} \right) +
3-\epsilon \, \left( 2\,\cosh \left( 2\,{\frac {\pi }{n}} \right) +1
 \right)  \right) ^{2}}$$
So when this expression is equal to $0$ ,  $\epsilon$ may be an arbitrary number.
Comment: Since $j$ is not an imaginary unit here, can we use the above mentioned closed form for this problem ? Is there any other way to solve this problem?


ssl svn user login problem

Question: I've a subversion server, on Apache (on WindowsXP), which is accessible over the net using http. I wanted to use ssl/https to access it. I haven't messed around with forcing http -> https yet, and can login using http:// fine. However, if I use https://, the login fails, and I get an "Authentication required!" 401 error message.
I guess it's a configuration issue on my server(?), but haven't found a solution yet.
Any ideas?
edit
I can connect and login via https://localhost ok, but not over the net via the ip address. I did put the ip address in when creating the certificate (via makecert).
snippet of conf file (I can add more if necessary):
httpd-ssl:
`VirtualHost _default_:443>
        #   General setup for the virtual host    t
        DocumentRoot "C:/xampp/htdocs"
        ServerName localhost:443
        ServerAdmin webmaster@localhost
        ErrorLog "logs/error.log"
        <IfModule log_config_module>
            CustomLog "logs/access.log" combined
        </IfModule>

        SSLEngine on

        SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
        #SSLCertificateFile "conf/ssl.crt/server-dsa.crt"
        SSLCertificateFile "conf/ssl.crt/server.crt"`

httpd.conf excerpt:
<IfModule ssl_module>
    SSLRandomSeed startup builtin
    SSLRandomSeed connect builtin
    SSLSessionCache "shmcb:logs/ssl.scache(512000)"
    SSLSessionCacheTimeout 300
</IfModule>

<Location /svn>
  DAV svn
  SVNListParentPath on
  SVNParentPath C:\SVN
  #SVNIndexXSLT "/svnindex.xsl"
  AuthType Basic
  AuthName "Subversion repositories"
  AuthUserFile passwd
  #AuthzSVNAccessFile svnaccessfile
  Require valid-user
</Location>

Comment: Can you provide the apache virtual host configs for the http, and https sites?
Comment: Update: it's all working fine now, but I can't pinpoint exactly what caused it to work (which is a bit annoying, but anyway...).
Answer: Try adding this under ServerName:
ServerAlias 127.0.0.1:443

Comment: I just tried that (in the httpd-ssl.conf), but it hasn't worked. It's still getting past the warning message with the certificate not being trusted, but when I get to the subversion login, it fails that. It's still working fine on my local machine.
Comment: So everything is working the way you want it to when you go to "localhost", but not when you go to "127.0.0.1"?
Comment: no, sorry, it's when I go to my actual ip address from an external pc. That still works fine via "http :// 82.xxx.xxx.xxx", but not "https :// 82.xxx.xxx.xxx". If I try the https://localhost etc. on the actual server machine, that works fine, after the cert. warning. (can't work out how to tag https, hence the spaces!)

ssl svn user login problem


Displaying and selecting data from a database in a module

Question: I have a database table (gpx_api) with the following fields:
runID   userID  score   distance    pace    speed   gpx_path    missionID   startTime   duration

I want to display all of these fields from within a module.
I have searched a lot on stack and I got until here:
   $query = db_select('gpx_api', 'run')
     ->fields('run')
     ->condition('runID', $firstparameter, '=')
     ->execute()
     ->fetchAssoc();

Do I only select the row where runID = $firstparameter like this?
This should only return one result. Now how can I display the fields one by one?
$query->distance

will not work.
I also tried
foreach($query as $result2){
 $result .="DISTANCE";
$result .= $result2->distance;
  }

This strangely enough gave a lot of results and errors.
I am sure there is a very simple way, but I am not familiar with the drupal API.
--Update
a var_dump of query gives me:
array(10) { ["runID"]=> string(1) "2" ["userID"]=> string(1) "1" ["score"]=> string(2) "10" ["distance"]=> string(3) "200" ["pace"]=> string(1) "7" ["speed"]=> string(1) "8" ["gpx_path"]=> string(0) "" ["missionID"]=> NULL ["startTime"]=> NULL ["duration"]=> NULL }

so it is just about displaying the array. Shouldn't $query->distance  work then? Yet if I try it, I get this error:
Notice: Trying to get property of non-object in epicrun_mymenu_page_callback() (line 62 of /home/suneesto/public_html/addons/epicrunapp.com/sites/all/modules/epicrun/epicrun.module).

Comment: please read about Result Sets (and working with them): https://drupal.org/node/1251174
Answer: Basic php error, it's $query['distance']


Playing background sound loop

Question: So I am developing a game and I want it to have background sound and sound in reaction to touch events. But taking it one step at a time I would really like to know how to play my mp3 in the background of my game?
Answer: You will either want to use MediaPlayer or SoundPool or both! With MediaPlayer you will want to use it for longer sounds such as background music that is played throughout your game. SoundPool is used more for things such as sound effects (shorter sounds that are played when something in your game occurs). There are many examples in the Android documentation on how to use both of these.

Playing background sound loop


Monit: 2 checks, one action

Question: Is something like this possible with monit:
check process foo with bar
check process foo2 with bar2
  start program = "baz start"
  stop program = "baz stop"

Basically I'm looking for an "OR" condition.
Answer: It's not exactly OR condition you asked for but take a look at check process <name> matching <regex> syntax.
CHECK PROCESS <unique name> MATCHING <regex>

<regex> is alternative process specification using pattern matching
to process name (command line) from process table instead of pidfile.

Example:
check process foo_foo2 matching "foo_name|foo2_name"
  start program = "baz start"
  stop program  = "baz stop"

Comment: Thanks, ideally I'd want to match the pidfile though, but "pidfile matching" doesn't seem to be valid.

Monit: 2 checks, one action


Correlation of a calculated variable

Question: This might be a very basic question, but I've failed in finding an explanation on the web ...
How is it possible that a variable (Var1) that significantly correlates with two different variables (Var2 and Var3), would not correlated also with a fourth variable (Var4) which is the result of dividing the later two variables (Var4 = Var2/Var3)?
For example:
I have an image in which one of the properties is "Quality".
I use this image to obtain two measurements: $M_1$ and $M_2$. From $M_1$ and $M_2$ I calculate $F_1$ (which is my endpoint) by dividing $M_1$ by $M_2$ ($F_1 = M_1/M_2$).
Although I get a significant correlation between "Quality" to both $M_1$ and $M_2$ (Spearman's -0.625, P<0.0001 and -0.636, P<0.0001 respectively), the correlation between $F_1$ and "Quality" is non-significant (-0.095, p=0.565).
It should also be noted that $M_1$ and $M_2$ strongly correlate with each other (0.692, P<0.0001).
Comment: Since correlation measures a degree of *linearity* in a relationship and division can be arbitrarily *non-linear,* we should be surprised if there were any general relationship among correlations involving fractions.  If you can bound the values of the numerator and denominator (such as when the denominator is large and positive), then perhaps something can be said--but only because in that circumstance division is *approximately* linear. These considerations provide hints concerning how to construct illustrative examples.
Comment: Thank you very much. Just to be clear, if I can say for certain that both numerator and denominator are positive (but I can't know which is bigger) would it be considered to be _approximately linear_?
Answer: Well, the most simple (and extreme) example of this would be if all those variables are the same.  If $X_1 = X_2 = X_3$ then we have $X_2/X_3 = 1$, which is constant, so that:
$$\begin{equation} \begin{aligned}
\mathbb{Corr}(X_1,X_2) &= 1, \\[6pt]
\mathbb{Corr}(X_1,X_3) &= 1, \\[6pt]
\mathbb{Corr}(X_1,X_2/X_3) &= 0. \\[6pt]
\end{aligned} \end{equation}$$
As you can see, in this extreme case the value $X_1$ has perfect correlation with $X_2$ and $X_3$, but zero correlation with their ratio $X_2/X_3$.  So this kind of phenomenon is certainly possible in the mathematical sense described here.  In this case you can see that the correlation with the individual variables is "cancelled out" in the correlation with the ratio variable.
Practically speaking, in your particular analysis I notice that $\widehat{\mathbb{Corr}}(X_1,X_2) \approx \widehat{\mathbb{Corr}}(X_1,X_3)$ and you also find no significant evidence of correlation with the ratio of these two latter variables.  It is possible that the correlation in the individual variables is "cancelling out" in the ratio in a manner roughly analogous to the above extreme example.  You can probably see exactly what is going on by generating a scatterplot matrix of your data, including the ratio variable.
Answer: The clue is in your last sentence "It should also be noted that $M_1$ and $M_2$ strongly correlate with each other". In the limit case where that correlation is 1, $F_1=M_1/M_2$ will be a constant, and the correlation of a constant with anything is zero. When it is only strong, $F_1$ will be close to constant, so with low variance and the correlation close to zero.
Let us look at that with some simulation. I will denote the Quality variable by $Q$. Simulation in R with correlations similar to yours:
<code>library(MASS)
set.seed(7*11*13) # my public seed

n  <-  100
mu <-  c(100,20,20)
sd1 <- 7
sd2 <- sd3 <- 3
rho1 <- -0.5
rho2 <- 0.7
Sigma <- matrix( c(sd1^2, sd1*sd2*rho1, sd1*sd2*rho1, sd1*sd2*rho1, sd2^2, sd2*sd2*rho2, sd1*sd2*rho1, sd2*sd2*rho2, sd2^2), 3, 3)

mydata     <- mvrnorm(n, mu, Sigma) # returns samples in rows
mydata  <- cbind(mydata, mydata[,2] / mydata[,3] )
colnames(mydata)  <-  c("Q","M_1","M_2","F_1")

cor(mydata)
              Q        M_1        M_2         F_1
Q    1.00000000 -0.6777416 -0.6635441 -0.02183251
M_1 -0.67774159  1.0000000  0.6928511  0.39089309
M_2 -0.66354405  0.6928511  1.0000000 -0.37940807
F_1 -0.02183251  0.3908931 -0.3794081  1.00000000
</code>
Note the very small correlation (about -0.02) between $F_1$ and $Q$. You can repeat the simulation yourself with other parameters.
Some other views was given in a comment by whuber: "Since correlation measures a degree of linearity in a relationship and division can be arbitrarily non-linear, we should be surprised if there were any general relationship among correlations involving fractions. If you can bound the values of the numerator and denominator (such as when the denominator is large and positive), then perhaps something can be said--but only because in that circumstance division is approximately linear. These considerations provide hints concerning how to construct illustrative examples. "

Correlation of a calculated variable


Show that the area set is measurable

Question: This is a problem from Billingsley's text Probability and measure.

Suppose that $f$ is nonnegative on a $\sigma$-finite measure space $(\Omega, \mathscr{F}, \mu)$. Show that
  $$\int_\Omega f d \mu = (\mu \times \lambda)[(\omega, y) \in \Omega \times \mathbb{R}^1: 0 \leq y \leq f(\omega)]. \tag{1}$$
  Prove that the set on the right is measurable.

My Attempt:
Denote the set on the right by $G$. First, by condition, the product measure $\mu \times \lambda$ exists on the $\sigma$-field $\mathscr{F} \times \mathscr{R}^1$. I am able to show that $G$ is measurable and $(1)$ holds if $f$ are simple functions. It looks naturally that for general nonnegative $f$, let $\{f_n\}$ be a sequence of simple functions such that
$f_n \uparrow f$. Then define $G_n = [(\omega, y) \in \Omega \times \mathbb{R}^1: 0 \leq y \leq f_n(\omega)]$ accordingly for every $n$. It is expected that $G_n \uparrow G$ so that the measurability of $G$ follows from the measurability of $G_n$. However, this seems unjustified, since in fact
$$G_n \uparrow [(\omega, y) \in \Omega \times \mathbb{R}^1: 0 \leq y \color{red}{<} f(\omega)].$$
So we have to show that
$$[(\omega, y) \in \Omega \times \mathbb{R}^1: f(\omega) = y, y \geq 0] = [(\omega, f(\omega)): f(\omega) \geq 0] \tag{2}$$
is measurable. And here is where I got stuck. Can someone show me why $(2)$ is measurable or to show the assertion through other ways? Thank you very much.
Answer: First of all you need to assume that $f$ is $(\cal F, B)$-measurable, where $\cal B$ is the Borel sigma algebra on $\Bbb R$.
Define a map $g: \Omega \times \Bbb R \to \Bbb R$ as $(\omega, y) \mapsto f(\omega)-y$. Note that $g$ is $(\cal F \otimes B, B)$-measurable, since it is the difference of two $(\cal F \otimes B, B)$-measurable functions, namely $h_1(\omega, y) := f(\omega)$ and $h_2(\omega, y) := y$.
It follows that the set $A:=g^{-1}[0,\infty)$ is measurable. Hence $A \cap B$ is measurable, where $B=\Omega \times [0,\infty) \in \cal F \otimes B$. But $A \cap B$ is precisely the set you are trying to prove is measurable.
Comment: This looks good to me, thank you. But in your way, is it also easy to show that $(1)$ also holds?
Comment: I got it, $(1)$ then follows from the Fubini's theorem.
Comment: Exactly, just let $D$ be that set. And then notice that $f(\omega) = \int_{\Bbb R} 1_D(\omega,y) dy$ and integrate both sides w.r.t $\mu$. Then apply Fubini.

Show that the area set is measurable


Derive the following expression:

Question: Given the function $(x_1^2+x_2^2)^2-x_1^2+x_2^2=0$, where $r^4=x_1^2-x_2^2$ and $r^2=x_1^2+x_2^2$ for $-1\leq r \leq 1$ show that
$x_1=\frac{1}{\sqrt{2}}r\sqrt{1+r^2}$ and $x_2= \pm \frac{1}{\sqrt{2}}\sqrt{1-r^2}$.
Any help is much appreciated!
Comment: Your first expression is not a function. It is an equation.
Answer: Hint: We have $x_{1}^2 = \frac{r^4 + r^2}{2}$ and $x_{2}^2 = \frac{r^2 - r^4}{2}$
Comment: Cheers! That's the one I needed. I guess it's kind of late and I just didn't see it. Anyhow, thanks!

Derive the following expression:


Como diferentes linguagens de programação se comunicam?

Question: Uma dúvida que sempre tive e que me traz curiosidade é como diferentes linguagens de programação se comunicam entre si.
Não é tão raro ver diferentes projetos que usam linguagens diferentes em um mesmo software, como por exemplo um jogo utilizou as tecnologias de C/C++ com Python ou algo do tipo.
Existe um API que faz essa comunicação entre linguagens? Gostaria de saber mais.
Answer: Sim, existe uma API de comunicação, pelo menos nas linguagens mais abstratas como Python. As mais concretas em geral só se conformam com a linguagem mais universal.
Toda linguagem que se preze é capaz de chamar C, seja de forma natural através da ABI ou de uma API criada para fazer a conversão de dados de forma a garantir a comunicação. C++ mesmo se comunica com C por ABI. C# tem um misto de ABI e API. ABI é mais eficiente que a API.
Linguagens de script podem um modelo de memória muito diferente e precisa de funções que pegam os parâmetros de forma diferente das linguagens mais nativas (quase sempre só usa-se C - ou C++/Rust de forma compatível com C). E para retornar o resultado também precisa de alguma conversão. Também de cuidar da forma de gerenciar as alocações de memória que são bem diferentes.
Geralmente isto é chamado de Foreign Function Interface ou FFI. Em Python tem documentação sobre (não achei na oficial). Leia mais.
Em Lua é bem mais fácil entender a API para conversar com C, por ser mais simples, mas com mesmo poder.
Softwares específicos podem ter APIs específicas de comunicação.
Além disso é possível se comunicar sem usar ABI ou API. Por exemplo, com técnicas de RPC ou IPC. Em geral são extremamente ineficientes, mas hoje muita gente não liga para isso, chega, até fazer um RPC via web. Em geral pode ser feito até mesmo sem conhecimento do que está do outro lado. É mais flexível, apesar da ineficiência. Em geral o pessoal evita até ser muito necessário.
Veja mais.
Coloquei no GitHub para referência futura.
Comment: Obrigado pela resposta Maniero! Nunca tinha ouvido falar de ABI até agora. Vou me aprofundar no assunto.
Comment: Gostaria de acrescentar que em sistemas que se utilizam de múltiplas linguagens muitas vezes tem módulos específicos onde, por exemplo, o módulo desenvolvido em C coleta dados e salva em um `banco de dados`, já o módulo desenvolvido em Python analisa os dados que estão no banco de dados. Outra possibilidade é que os módulos (ou micro serviços dependendo do "tamanho") se comuniquem por meio de um sistema de mensagens como RabidMQ, Kafka ou outro. Estes tipos de arquitetura são agnósticos à linguagem, ou seja os módulos/micro serviços poderiam até ter sido desenvolvidos na mesma linguagem.


Cron running by itself?

Question: My Drupal 7 instance was getting slow. I realised that the problem was a cron job running. However, why is cron running when I'm browing the site? It shouldn't be, surely? It should only run when I run cron.php.
I don't have poormanscron running or anything else that I am aware of that could be kicking off cron. Any ideas of what could be causing this on Drupal 7?
Answer: In Drupal 7 cron is run by Drupal by default.
You can configure it at: admin/config/system/cron
It is preferable to disable that cron and run it automatically from the server in some way.
That method is really only preferable if for any reason you have no other choice.
That said, you really should be automatically running cron regularly.
Not necessarily every hour but at least once a day.

Cron running by itself?


I want to use READ_NOS to read a file from S3 and get all rows to return. But it only returns some rows

Question: I  want to use READ_NOS to  read a file  from S3  and get all rows to return.  But  it  only returns  some  rows .
I create a  foreign table for  a parquet file.
but result:  https://i.stack.imgur.com/K9jmA.jpg
use studio still the same result:  https://i.stack.imgur.com/J5EBO.jpg
how to get the all rows  return ?
Comment: So it appears one file doesn't match the FOREIGN TABLE structurally, and for most rows there are column values that fail conversion to the data types stated in the FOREIGN TABLE definition.
Comment: yes   data content has some problem.   I  fixed  the content finally get all rows return.
Comment: And  this takes me a lot of time to debug.  Thanks  Fred.  :)
Answer: the first SQL (COUNT*) show the number of records. The second one the number of Parquet Files. So in average each files holds 6.470 records.
There is a Teradata Orange Book dedicated to Use of NOS with some backgroud but as well some example SQL. Chapter 5 of this is focussed on Parquet Files.
It looks like RETURNTYPE ('NOSREAD_PARQUET_SCHEMA') is important in the combination os READ_NOS and Parquet.
Comment: yes,  But the return  message  got me confused. It doesn't  let me think about the  data content problem.


Page rank MATLAB

Question: trying to fix my code that does page rank. Not sure why the power method and the backslash are coming up with different answers and which is wrong and how to fix it.
<code>    p=rand(length(M),1);
    pnorm=norm(p,1);
    p=p/pnorm;
    n=length(M);

    d=0;
    e=ones(n,1);
    a=1/n;
    Z=a*(e*e');
    Mtilda=d*M+(1-d)*Z;

    for k=1:n
        tic;
        prev_p = p;
        z = Mtilda*p;
        p = z/norm(z,2);
        dl_p = norm(p-prev_p, 2);
        t(k)=toc;
        str=sprintf('for d=%g , iteration %d: time=%11.4g',d,k,t(k));
        disp(str);
    if dl_p < 1e-6; break; end
    end

    ReportPageRank(p,U,0.005)
    d=1;
    p2 =(eye(n)-d*M)\((((1-d)/n)*e));
</code>
Comment: it seems to be out by a factor of 2 so i cant be completely wrong.
Comment: Could you give an example of what the array `M` represents? Also, what is the definition of `ReportPageRank(p,U,0,005)` function? What are the parameters `p`, `U`, and `0.005`? This way I may run it on my machine. What was the intention of the last two lines of code? They don't seem to have any purpose.
Comment: I had to compare the backslash operator. I now have it working luckily. I had to set up the power method differently and it for some reason worked.
Comment: Can you add an edit to your question where you explain what was the issue with the pseudo-inverse?
Answer: You've figured it out, but I'll post my work anyways in case someone's interested in the implementation.
<code>M = ...
[  0.09091   0.00000   0.00000   0.50000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000;
   0.09091   0.00000   1.00000   0.50000   0.33333   0.50000   0.50000   0.50000   0.50000   0.00000   0.00000;
   0.09091   1.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000;
   0.09091   0.00000   0.00000   0.00000   0.33333   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000;
   0.09091   0.00000   0.00000   0.00000   0.00000   0.50000   0.50000   0.50000   0.50000   1.00000   1.00000;
   0.09091   0.00000   0.00000   0.00000   0.33333   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000;
   0.09091   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000;
   0.09091   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000;
   0.09091   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000;
   0.09091   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000;
   0.09091   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000];
N = length(M);
PR = (1/N)*ones(length(M),1);   %define PageRank vector for t = 0
d = 0.85;                       %define damping rate
iter = 1;
delta_PR = Inf;                 %set initial error to infinity
while delta_PR > 1e-6           %iterate until error is less than 1e-6
    tic;

    prev_PR = PR;               %save previous PageRank vector (t-1)
    PR = d*M*PR + ((1-d)/N)*ones(N,1);  %calculate new PageRank (t)

    delta_PR = norm(PR-prev_PR);%calculate new error
    t(iter)=toc;
    str=sprintf('for d=%g , iteration %d: time=%11.4g',delta_PR,iter,t(iter));
    disp(str);
    iter = iter + 1;
end

powerRank = pinv((eye(length(M)) - d*M))*(((1-d)/N)*ones(length(M),1));

PR = powerRank =

    0.0328
    0.3844
    0.3429
    0.0391
    0.0809
    0.0391
    0.0162
    0.0162
    0.0162
    0.0162
    0.0162
</code>

Page rank MATLAB


Does RandomAccess have any value in modern day Java?

Question: There is that peculiar interface which is documented as such (extracts only):

Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists.

The listed implementations are (as of JDK 8):

ArrayList, AttributeList, CopyOnWriteArrayList, RoleList, RoleUnresolvedList, Stack, Vector

Two of which (the latter two, in fact) are obsolete in any code you would produce today.
Now, as a word of warning, my research has been so NOT extensive that I am asking this question out of a whim. Is there any use case at this point in time where one would see the need to use this interface, or even implement it?
Answer: This marker interface could be useful for algorithm implementors. Lists which implement this interface can be handled differently from the lists without random access.
For example, "if the list is random-access, use binary search, otherwise use linear search."
One example is Guava's Lists.transform:
public static <F, T> List<T> transform(
        List<F> fromList, Function<? super F, ? extends T> function) {
   return (fromList instanceof RandomAccess)
       ? new TransformingRandomAccessList<F, T>(fromList, function)
       : new TransformingSequentialList<F, T>(fromList, function);
}

Answer: Having a quick look at the OpenJDK's java.util.Collections code this is still used and useful.
For example the Collections.binarySearch method uses this marking interface to chose between two implementations.
public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key) {
     if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)
         return Collections.indexedBinarySearch(list, key);
     else
         return Collections.iteratorBinarySearch(list, key);
 }

Answer: The documentation gives a good example of how to use this interface: as an indicator. I could imagine such a usecase: You have a list and don't know if it behaves faster by iterating or by randomly accessing:
void process(List<E> items) {
    if (items instanceof RandomAccess)
        for (int i = 0; i < items.size(); i++) System.out.println(items.get(i));
    else
        for (E item : items) System.out.println(item);
}

Comment: This is just restating the first quote in code. I'd be interested to know of concrete, non-toy examples.


Calclulating and creating x matrix using set.seed function

Question:
I have been asked to evaluate the sum of xi's? what could be wrong with the following code? How to correct it to work?

set . seet (1) #set random seed
x = rnorm (50)
for ( j in 1:50){ s = s + x [ i ] }

Also, if you are calculating the sum of xi's and xj's using seed function for i = 1...20 and j = 1,2. what could be wrong with the following code?

set . seed (1) #set random seed
create x matrix
x = matrix ( rnorm (40) , nrow =20 , ncol =2)
S = NULL
for ( i in 1:2)
{
for ( j in 1:20)
{
S <- s + x[i , j ]
}# end of j loop
}# end of i loop
(c) Calculate the row means
Comment: Welcome to Cross Validated! // Why is a seed coming up at all? Why are you using loops instead of built-in functions like "sum"? // Please format your code so it can be read and copied verbatim into R to be run.
Comment: It seems you are to _generate_ the $X_i$.s and then to sum them. What is the distribution of the $X_I$s? What is the the purpose for finding the sum?
Answer: If you want the sum of two standard normal random variables, and to repeat this task 40 times, then here
are two methods that give identical results for the same seed:
set.seed(1)
x = rnorm(80)
MAT = matrix(x, nrow=40)
head(MAT)  # first six rows of matrix
           [,1]       [,2]
[1,] -0.6264538 -0.1645236
[2,]  0.1836433 -0.2533617
[3,] -0.8356286  0.6969634
[4,]  1.5952808  0.5566632
[5,]  0.3295078 -0.6887557
[6,] -0.8204684 -0.7074952

s = rowSums(MAT)
summary(s)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-3.3441 -0.3653 -0.0317  0.2123  1.0424  3.3206
 s
 [1] -0.790977407 -0.069718356 -0.138665237  2.151944001
 [5] -0.359247923 -1.527963541  0.852011015  1.506857630
 [9]  0.463435140  0.575719339  1.909887049 -0.222183157
[13] -0.280120889 -3.344062983  2.557954620  1.935466289
[17] -0.383411740 -0.100298416  1.390940823  0.458846717
[21]  3.320595132  0.742896298  0.764304346 -1.961349537
[25] -0.123447461  0.132663560 -1.960754136 -0.005197522
[29] -0.324896717  2.590553231  1.834189080 -0.812734158
[33]  0.998397965 -0.987902672 -2.630692957 -0.123548328
[37] -0.837581827 -0.058208045  1.174366696  0.173654802

.
set.seed(1)
x1 = rnorm(40); x2 = rnorm(40)
head(x1)
[1] -0.6264538  0.1836433 -0.8356286  1.5952808
[5]  0.3295078 -0.8204684
head(x2)
[1] -0.1645236 -0.2533617  0.6969634  0.5566632
[5] -0.6887557 -0.7074952

s = x1 + x2
summary(s)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-3.3441 -0.3653 -0.0317  0.2123  1.0424  3.3206
head(s)  # same forty sums as above; only six shown
[1] -0.79097741 -0.06971836 -0.13866524  2.15194400
[5] -0.35924792 -1.52796354

Here is another method that uses the random variables
is a different order, and so gives a different simulated
result starting with the same seed.
set.seed(1)
t = replicate(40, sum(rnorm(2)))
summary(t)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
-2.8359 -0.4970  0.1051  0.2123  1.1563  3.4134
length(t)
[1] 40

Notice that the 'Mean` is the same as before because
the same 80 observations are involved, even if in a different order.
head(t)
[1] -0.4428105  0.7596522 -0.4909606  1.2257538
[5]  0.2703930  1.9016244

Notes: (1) You can show code and output here by beginning
each line with four blank spaces.
(2) Your question is about R, not really about statistics, so I suppose it may be closed or 'migrated' to another site. If I have not correctly guessed what you're trying
to do, please clarify the question.
Comment: This is really what I wanted and thanks for your help. Indeed R programming is demanding task and at the same time highly rewarding.  Many thanks BruceET for your help.


drawing circle starts at wrong position

Question: I wrote a function which allows me to draw circles on a canvas. Everything works fine, but the start position of my canvas is not the same position as my mouse position.
const stopDrawingCircle = () => {
    setIsDrawing(false);
    console.log(currentImage);
}
//Setting start position
const startDrawingCircle = (event:any) => {
    currentImage.circle.x = event.clientX;
    currentImage.circle.y = event.clientY;
    currentImage.circle.radius = 0;
    setIsDrawing(true);
};
const drawCircle = function(event:any){
    if(!isDrawing)
      return;
    let canvas = canvasRef.current as HTMLCanvasElement | null;
    let ctx = canvas?.getContext('2d')
    //seems to be the problem console log at start says the result of this is zero
    var currentX = currentImage.circle.x - event.clientX;
    var currentY = currentImage.circle.y - event.clientY;
    currentImage.circle.radius = Math.sqrt(currentX * currentX + currentY * currentY)
    if(canvas != null && ctx != null){
        ctx.beginPath();
        ctx.arc(currentImage.circle.x, currentImage.circle.y, currentImage.circle.radius, 0, Math.PI*2);
        ctx.fill();
    }
}
return(

      <canvas id="canvas"
        onMouseDown={startDrawingCircle}
        onMouseUp={stopDrawingCircle}
        onMouseMove={drawCircle}
        ref={canvasRef}> </canvas>

)

The result of drawing a circle is like this:

I started with my mouse in the center of the image and he draws it in the "lower right corner"
Answer: It's hard to run your example because it does not seem to be a complete one.
I suspect what your error is and I think this simple example will be enough for you to fix your issue.

const canvas = document.getElementById('canvas');

const drawCircle = function(event){
  const ctx = canvas.getContext('2d');
  const canvasClientRect = canvas.getBoundingClientRect();

  const { clientX, clientY } = event; // absolute click pos

  const x = clientX - canvasClientRect.left; // calculate relative x
  const y = clientY - canvasClientRect.top; // calculate relative y

  ctx.beginPath();
  ctx.arc(x, y, 20, 0, Math.PI * 2);
  ctx.fill();
}

canvas.addEventListener('click', drawCircle);

    Example

      <canvas id="canvas" width="500px" height="500px" style="border: 1px solid red;"></canvas>

So as you can see getting the clicked X and Y coordinates is not enough and it's not relative to the canvas element and to draw on the canvas you need its relative coordinates.
If your canvas started at the very top left corner of your page it would not matter but if it's not there then it's important to get the canvas coordinates so you can calculate the click position properly.
So let's assume your canvas starts 50px from the top and 100px from the left. To each click on the canvas you need to subtract 50px for the X coordinate and 100px to the Y coordinate. With these relative values you can draw on canvas and the circle will be in the middle of your cursor every time.
Comment: Thanks for you solution, it was a problem with the canvas offset. See Solution below :)
Answer: event.clientX and event.clientY are relative to the current screen, not element being clicked.
MDN MouseEvent.clientX docs:

clientX
A number, defaulting to 0, that is the horizontal position of the mouse event on the client window of user's screen;

Use event.offsetX and event.offsetY to get the mouse position relative to the event target.
However, since you're using React, you need to use event.nativeEvent.offsetX and event.nativeEvent.offsetY because react doesn't copy the offset properties onto their event proxy object.

Full code example:
import {useState, useRef} from "react";

const currentImage = {
  circle: {
    x: 0,
    y: 0,
    radius: 10,
  }
};

export default function App() {
  const [isDrawing, setIsDrawing] = useState(false);
  const canvasRef = useRef<HTMLCanvasElement>(null);

  const stopDrawingCircle = () => {
      setIsDrawing(false);
      // console.log(currentImage);
  }
  //Setting start position
  const startDrawingCircle = (event:any) => {
      currentImage.circle.x = event.nativeEvent.offsetX;
      currentImage.circle.y = event.nativeEvent.offsetY;
      currentImage.circle.radius = 0;
      setIsDrawing(true);
  };
  const drawCircle = function(event:any){
      if(!isDrawing)
        return;
      let canvas = canvasRef.current as HTMLCanvasElement | null;
      let ctx = canvas?.getContext('2d')
      //seems to be the problem console log at start says the result of this is zero
      var currentX = currentImage.circle.x - event.nativeEvent.offsetX;
      var currentY = currentImage.circle.y - event.nativeEvent.offsetY;

      currentImage.circle.radius = Math.sqrt(currentX * currentX + currentY * currentY)
      if(canvas != null && ctx != null){
          ctx.beginPath();
          ctx.arc(currentImage.circle.x, currentImage.circle.y, currentImage.circle.radius, 0, Math.PI*2);
          ctx.fill();
      }
  }
  return(

        <canvas id="canvas"
          onMouseDown={startDrawingCircle}
          onMouseUp={stopDrawingCircle}
          onMouseMove={drawCircle}
          ref={canvasRef}> </canvas>

  )
}

Comment: Ahhhh okay, that makes sense! Im new at react and its really hard to work with canvas sometimes. Thx for your solution.

drawing circle starts at wrong position


A conjecture on $\phi(n)$

Question: Let $\phi(n)$ denote the Euler  totient function of $n $.
Then let $N$ be a number such that $\phi(N)$ divides $N$ . Also let $I_1= \frac{N}{\phi(N)}$ which is defined as the "Second order Index of Beauty of $N$ ".

Then prove that - For every number $I_1$ there exists a number $N$ such that $I_1$ is the second order index of beauty of $N$.
Comment: Hang on, is this a conjecture or you want a proof?
Comment: Why do people want to close this? Sure, the OP should have asked "Prove or disprove", but it is reasonable to ask this question after checking for $10000$ values, and there is a definite answer.
Comment: How large a value of $I_1$ have you tried?  Anything larger than $3$?
Comment: @IanColey I want a proof of this conjecture.
Comment: @ShivamPatel Most conjecturers would be happy to see a proof or a disproof.  What's your evidence that it's true?
Comment: I wrote a script in Python to check this -- up to $n=10000$, the only (integral) values are 1,2, and 3. Working on $n=100000$ now, but the code is sloppy.
Comment: @IanColey can you please show me that code
Comment: Just to clarify: You don't know yet, whether that is true, right?
Comment: @Bernd yes , I don't know.
Comment: I'm sure this problem has been considered before. You should check "Unsolved Problems in Number Theory". (By the way, "Index of Beauty" isn't a particularly apt name for this quantity, in my opinion.)
Comment: @GregMartin I read something like this named this way , so I gave it this name....
Comment: Am I wrong? I think there is an explicit formular: Product over (1-p/N) where p must be a prime dividing N
Comment: correction: The formular yields Phi(N)/N, that is the reverse value
Comment: @Phira that's true and the way it was , I didn't check for $10000$ but I did till $3000$.
Answer: I am sorry, but the conjecture is really false: In fact, there are no other integer values for $N/\varphi(N)$ other then $1,2,3$. Reason:
The function $\varphi(N)$ is a multiplicative number-theoretic function. Let $N = \prod p_i^{k_i}$ with pairwise distinct primes $p_i$ dividing $N$, then $$\frac{N}{\varphi(N)} = \frac{\prod p_i^{k_i}}{\prod (p_i-1)p_i^{k_i-1}}  = \frac{\prod p_i }{ \prod (p_i-1)}.$$
Now compare prime factors of the numerator and the denominator:

If $N$ is divisible by $3$, then there is a $2$ in the denominator, canceling the only remaining $2$ in the numerator. This means that there cannot be another prime factor of $N$ other than $2,3$. So $N/\varphi(N)=3$ is the only integer value in this case, obtained by any $N$ of the form $2^k3^j$.
Suppose $N$ is divisible by a prime $>3$ and let $p$ be the lowest such prime. Then there is $p-1$ in the denominator which must be canceled by primes in the numerator lower than $p$. But as we have already ruled out $3$, there is only a single $2$ in the numerator remaining as a possible factor of $p-1$, a contradiction.
The only remaining value is $N/\varphi(N) = 1$ for $n=1$ and $N/\varphi(N) = 2$ for $N = 2^k$.

EDIT
By the way, as this was discussed alongside: Here is my recommendation for calculation of many values of $\varphi(N)$: use Sieves.
N = 10**8
BeautyIndex = set([1])
PHI = numpy.arange(N)

for i in xrange(2,N):
   if PHI[i]==i:#i is prime
     PHI[i::i]*=i-1
     PHI[i::i]/=i
   if i%PHI[i]==0:
     BeautyIndex.add(i)
print BeautyIndex

Comment: ... no other prime factors $p_i$ and of course $q / ((q-1)/2)$ is not an integer. Thus $k \neq 1$ and there is some other prime factor $p_i$. But then the denominator is even and the numerator is odd, the resulting fraction can not be an integer.
Comment: can someone explain the second dot (N was divided by a prime p > 3 ) in more detail?
Comment: @Bernd: If there is a prime $q \mid N$ with $q > 3$, then the corresponding factor in the denominator is $q-1$, which is even. Now this must be canceled by some term in the numerator, and hence after this cancellation the numerator is now odd. Our fraction now looks like $(q \cdot \prod p_i) / ((q-1)/2 \cdot k)$ where $k \in \mathbb{Z}$ and the product runs over all primes except for $2$ and $q$. Note that $k = \prod (p_i-1)$ is even as long as $k \neq 1$. I claim that if $k = 1$, then the fraction is not an integer. This is clear since in this case there are...
Comment: @rghthndsd: Thank you very much
Comment: I really understand what you are saying and get YOUR proof. Is this the same as in the answer under dot 2? I still don't understand the sentence "But as we have already ruled out 3, there is only a 2 remaining in the numerator, a contradiction." because the only 2 remaining would be in the denominator, wouldn't it?
Comment: @Bernd: I believe so. Indeed, everything I wrote was inspired by benh and not my own idea, although I did not get it at first either. I think the sentence you quoted should read "But as we have already ruled out 3, and there is only a (single) 2 in the numerator, a contradiction." In other words, if you remove the word "remaining" and note that $p \neq 3$ implies there are two factors of 2 in the denominator, it does make sense.
Comment: Thank you very much rghthndsd for your comments! And you are absolutely right, I meant "a **single** $2$ in the numerator". I will change that in my post. @Bernd My initial reasoning is as follows: we consider the prime factors of $p-1$. As $p$ is the lowest prime $>3$ dividing $N$, the only two factors in the numerator that could divide $p-1$ are $2$ and $3$. But if there was a factor $3$ of $N$, we were again in the first case, which we have already considered. So there is only **a single** $2$ left as a possible factor of $p-1$, forcing $p=3$ which contradicts our assumption $p>3$.
Comment: ok thanks to both of you. Then I have two further suggestion to make it even clearer:
Write "N can be divided by 3" rather than "was divided" and add the fact that you pick p as the **lowest** prime > 3 that divides N. By the way: Great proof (now that I got it :-) )
Comment: @Bernd Thank you very much. For some reason I was sure that I had written something about $p$ being the lowest prime... I edited the post, sorry for the lack of precision in the earlier version.
Answer: Not an answer, but this won't work as a comment.  Here's the requested code to have a look-see in a slow, brute force way in Python.
def totient(n):
  if n == 1: return 1
  return sum(d * mobius(n / d) for d in range(1, n+1) if n % d == 0)
def mobius(n):
  result, i = 1, 2
  while n >= i:
    if n % i == 0:
      n = n / i
        if n % i == 0:
          return 0
      result = -result
    i = i + 1
  return result

results = set() #sets store only unique items, so the output is cleaner

for n in range(1,[large number]):
  if float(n)/float(totient(n))==float(n/totient(n)): #integer check
    results.add(n/totient(n))

for item in results:
  print str(item) #shows what we got

Update: Up to $100000$, we get $\{1,2,3\}$.

A conjecture on $\phi(n)$


Nginx+PHP scalability on Windows

Question: I'm trying to understand Nginx+PHP scalability running Windows with, lets say, 100 requests  doing a medium-term operations.
Analyzing source code I saw:

Nginx start a spawns itself several times depending on
configuration and/or server processors count.
Altough it uses I/O completion ports, each worker only creates
one thread to handle requests.
When a PHP request is done, Nginx communicates with PHP using FastCGI

At this point, although NGinx can continue scaling, I cannot see in standard PHP fastcgi sapi code that scales using multiple threads/processes and completion ports.
For unix/linux, PHP-FPM come to solve the problem because it forks child processes to complete tasks altough not sure about the performance.
But back into Windows, still in PHP-FPM, I don't see specific code to mantain the whole webserver performance high.
Is there an alternative for Windows? Is something wrong in my research?
Answer: The scalability of nginx on Windows is limited, so it's not a recommended platform for a production web site.
From the nginx web site:

Version of nginx for Windows uses the native Win32 API (not the Cygwin emulation layer). Only the select() connection processing method is currently used, so high performance and scalability should not be expected.
Although several workers can be started, only one of them actually does any work.
A worker can handle no more than 1024 simultaneous connections.
Comment: Here is a non-offical Nginx build optimized for Windows, with multi-worker support and lots of modules added. http://nginx-win.ecsds.eu/
Comment: Thanks so much for this link. I had no it existed.  I wish I had known sooner.

Nginx+PHP scalability on Windows


Hypercube Maximum Distance, Graph Theory

Question: I'm dealing with some hypercube questions here.  The one I'm currently on states:
Find the maximum distance between pairs of vertices in $Q_8$.  Give an example of one such pair that achieves this.
I've got the first part of the question down.  I drew out $Q_1, Q_2, Q_3, Q_4$, realized that the maximum distance between vertices in $Q_n$ is $n$.  After realizing this, it began to make sense to me.  $Q_{n+1}$ is created by duplicating $Q_n$ and connecting corresponding vertices.  Each vertex from $Q_n$ is connected to exactly one more vertex, and so the maximum distance between vertices will increase by exactly one.
That was the easy part.
How would I possibly show this?  I mean.  I could spend hours figuring out how to draw $Q_8$, then label all the vertices, and finally present a sequence of eight edges.. But that doesn't sound like the way a mathematician would solve such a problem so it can't possibly be the correct way...
Any help is greatly appreciated!
Answer: Think of it this way. If you have a cube in $3$ dimensions then we can embed the cube into $\mathbb{R}^3$. We label the vertices of the cube as the set of the binary sequences of length $3$. For example $(0,\ 0,\ 0)$ will represent the vertex at the origin while $(1,\ 1,\ 1)$ will represent the opposite corner. The vertices which are adjacent are precisely the sequences which differ by $1$ and only $1$ component (corresponding to moving one step in that dimension).
It is perhaps more intuitive to see how we can traverse the cube now. The distance between two vertices will be the number of different entries between the binary representations of the vertices.
You can verify that in general, the hypercube graph in $n$ dimensions $Q_n$ is given similarly in terms of binary sequences of length $n$ and that the distance property still holds. Now we have $n$ entries representing a vertex, so given any vertex, the furthest we can be away from it is for every entry to be different for a total of $n$ different entries. Therefore the furthest we can traverse from any vertex is $n$ steps.
Notice that this also explicitly gives you which vertices are distance $n$ away from each other, we simply take the complement of the vertex's binary representation (change $0$s to $1$s and vice versa).
Comment: Ahhh.. I forgot that $Q_n$ already has labels. So my example of a path of length $8$ in $Q_8$ would be a sequence of 9 binary strings that represent vertices, each differing by exactly 1 digit.  Correct?
Comment: Yes, that would be it.
Answer: You can think of $Q_n$ as a graph whose vertices are the $2^n$ $n$-tuples $\langle b_1,\dots,b_n\rangle$ with each $b_k\in\{0,1\}$. Two vertices are adjacent if they differ in exactly one coordinate. The distance between two vertices vertices is the number of coordinates in which they differ. (This requires proof, but it's not too hard.) Now, what vertex of $Q_n$ is maximally distant from $\langle\underbrace{0,0,\dots,0}_n\rangle$?
Answer: You have basically already sketched a proof by induction. The base case $Q_1$ is obvious. Given that the maximum distance in $Q_n$ is $n$, consider $Q_{n+1}$ as the Cartesian product $Q_n\square K_2$. The maximal distance between vertices in one copy of $Q_n$ is $n$, and the maximal distance between points in different copies is at least $n+1$, since you have to switch copies at least once, and also at most $n+1$, since you can reach each point by switching copies only once, so it's $n+1$. Thus by induction the maximal distance in $Q_n$ is $n$.


What intervals does the "consecutive interval" comprehend?

Question: I'm studying 16th century counterpoint.
I'm reading 3part counterpoint with 2 species and 3 species mixed chapter.
I read that consecutive intervals must be avoided in certain situations.
Ok I get that but I don't know if the consecutive intervals also implies 3,4,6.
(I know I should avoid perfect 5th and 8.)
Comment: It would be helpful to have the full quotation from the book or website you're reading.
Answer: "Consecutive intervals" in this case means any intervals. If one has several consecutive sixths, for example, the voices will start to lose their independence.
Leaving aside parallel fifths and octaves, it's generally okay to have three of the same interval in a row, but more than that is where the voices lose too much independence.
Answer:
Ok I get that but I don't know if the consecutive intervals also implies 3,4,6.

I think with numerals 3,4,6 you meant the intervals third, fourth, and sixth.
I think of it this way:
Consecutive fifths and octaves are forbidden through a relative motion rule. The relative motion involved there is perfect consonance intervals in parallel relative motion. You can flip that around into a positive statement: imperfect consonances are permitted to move by parallel motion.
No that we have an idea of what relative motions are allowed for different types of intervals we can move are more aesthetic based rule: create variety, avoid monotony.  This "rule" applies to consecutive intervals. Fux's species counterpoint basically puts a limit of three on consecutive intervals. This limit applies regardless of the previous relative motion rule, because this second rule is not concerned with relative motion, but rather about monotony. So, even though parallel thirds and sixths are OK by relative motion rules, you still want to limit them to only three consecutive occurrences.
Three can be regarded as a rule of thumb, a general guide line. A piece of music won't fall apart over four consecutive thirds. And, if my memory serves, Fux has some examples in his treatise where there are four consecutive, imperfect consonances (I think thirds.)
Answer: Simply it means don't use the same interval twice (or more!). So, if you've written voices that move in 5ths, the next move should be anything but. Using, as you say, 3,4,6 isn't repeating the interval, thus is o.k.


Supremum of expectation of absolute value

Question: Consider that $$X(x)=\begin{cases} \frac{x^2}{2}+\frac{1}{2}, &\text{ with prob. } \frac{1}{4},\\ \frac{5x}{6}, &\text{ with prob. } \frac{3}{4},\end{cases}$$
where $x\in\left<0,1\right>$ is fix and $$X(y)=\begin{cases} \frac{y^2}{2}+\frac{1}{2}, &\text{ with prob. } \frac{1}{4},\\ \frac{5y}{6}, &\text{ with prob. } \frac{3}{4},\end{cases}$$
where $y\in\left<0,1\right>$ is fix.
I wolud like to find out if $$\sup\limits_{x\neq y}\left[\mathrm{E}\left(\frac{\left|X(x)-X(y)\right|}{|x-y|}\right)\right]<1.$$
I have made some editting, i.e.
$$\sup\limits_{x\neq y}\left[\frac{1}{|x-y|}\mathrm{E}\left(\left|X(x)-X(y)\right|\right)\right]<1.$$
But I do not have idea how to copmute $\mathrm{E}\left(\left|X(x)-X(y)\right|\right)$. I have been trying some ways but then I think I cannot compute $\sup$ easily.
Any help will be appreciated. Thanks.
Comment: Hint: what's the probability that $X(x)=\frac{5x}6$ **and** $X(y)=\frac{y^2}2+\frac12$?
Comment: $X(x)$ and $X(y)$ are idependent, it means that probaility is $\frac{3}{4}\frac{1}{4}$ (I think). That also means that $P(X(x)-X(y)=\frac{5x}{6}-\frac{y^2}{2}-\frac{1}{2})=\frac{3}{4}\frac{1}{4}$
Comment: Correct. You have 4 cases, with probabilities $1/16$, $3/16$, $3/16$, $9/16$. Now calculate the expectation.
Answer: Then $$X(x)-X(y)=\begin{cases} (\frac{x^2}{2}+\frac{1}{2})-(\frac{y^2}{2}+\frac{1}{2}), &\text{ with prob. } \frac{1}{4}\cdot\frac{1}{4},\\ \frac{5x}{6}-\frac{5y}{6}, &\text{ with prob. } \frac{3}{4}\cdot \frac{3}{4},\\ \frac{5x}{6}-(\frac{y^2}{2}+\frac{1}{2}), &\text{ with prob. } \frac{3}{4}\cdot \frac{1}{4},\\ (\frac{x^2}{2}+\frac{1}{2})-\frac{5y}{6}, &\text{ with prob. } \frac{1}{4}\cdot \frac{3}{4}\end{cases}$$
$$X(x)-X(y)=\begin{cases} \frac{x^2-y^2}{2}, &\text{ with prob. } \frac{1}{16},\\ \frac{5(x-y)}{6}, &\text{ with prob. } \frac{9}{16},\\ \frac{5x-3y^2-3}{6}, &\text{ with prob. } \frac{3}{16},\\ \frac{3x^2+3-5y}{6}, &\text{ with prob. } \frac{3}{16}\end{cases}$$
$$|X(x)-X(y)|=\begin{cases} \frac{|x^2-y^2|}{2}, &\text{ with prob. } \frac{1}{16},\\ \frac{5\cdot |x-y|}{6}, &\text{ with prob. } \frac{9}{16},\\ \frac{|5x-3y^2-3|}{6}, &\text{ with prob. } \frac{3}{16},\\ \frac{|3x^2+3-5y|}{6}, &\text{ with prob. } \frac{3}{16}\end{cases}$$
$$E(|X(x)-X(y)|)=\frac{|x^2-y^2|}{2}\cdot \frac{1}{16}+\frac{5\cdot |x-y|}{6}\cdot\frac{9}{16}+\frac{|5x-3y^2-3|}{6}\cdot \frac{3}{16}+\frac{|3x^2+3-5y|}{6}\cdot \frac{3}{16}$$
$$E(|X(x)-X(y)|)=\frac{|x^2-y^2|}{32}+\frac{15\cdot |x-y|}{32}+\frac{|5x-3y^2-3|}{32}+\frac{|3x^2+3-5y|}{32}$$
$$\frac{E(|X(x)-X(y)|)}{|x-y|}=\frac{x+y}{32}+\frac{15}{32}+\frac{|5x-3y^2-3|}{32\cdot|x-y|}+\frac{|3x^2+3-5y|}{32\cdot|x-y|}$$
This expression is not limited. Take $x=\frac{1}{2}$, $y=x+\delta$, $0<\delta<\frac{1}{4}$ then
$$\frac{E(|X(x)-X(y)|)}{|x-y|}=\frac{7}{16}+\frac{\delta}{8}+\frac{5}{64\delta}$$
At $\delta\to 0$ this expression tends to infinity. So supremum is infinite.


Clarification Needed on Selecting the Farthest Right Point 'c' in Lemma 3.5 of Chapter 3 from Stein's Real Analysis

Question: I'm reading chapter 3 of Stein's Real Analysis and have come across Lemma 3.5, which states:

Lemma 3.5
Suppose $G$ is real-valued and continuous on $\mathbb R$.
Let $E$ be the set of points $x$ such that
$$ G(x+h) > G(x) \quad \text{ for some } h = h_x > 0. $$
If $E$ is non-empty, then it must be open, and hence can be written as a countable disjoint union of open intervals $E = \bigcup \left( a_k, b_k \right)$.
If $\left( a_k, b_k \right)$ is a finite interval in this union, then
$$ G(b_k) - G(a_k) = 0. $$

While reading the proof of the lemma, I'm having difficulty with an argument, and I've stuck at that part.
In the proof, the author claims that if $G(b_k) < G(a_k)$, then due to continuity, there exists a point $c$ in the interval $(a_k, b_k)$ where $G(c) = \frac{G(a_k) + G(b_k)}{2}$.
Furthermore, the author says that this $c$ can be chosen to be the farthest to the right in the interval.
However, I'm concerned that the set of such $c$'s could resemble $\left\{ b_k - 1/n \mid n \in \mathbb Z^+ \right\}$, suggesting that we might not be able to definitively choose the farthest right $c$ in the interval.
This possibility seems to challenge the author's assertion.
Could anyone provide clarification on why it is valid to select $c$ as the farthest right in the interval $(a_k, b_k)$?
I'm eager to understand this part of the proof and would appreciate any insights or explanations.
Comment: This is a pretty standard property of continuous functions. If $f:[a, b] \to\mathbb{R} $ is continuous and $p\in f([a, b]) $ then there is a largest value of $c\in[a, b] $ for which $f(c) =p$. Prove this by considering the set $A=\{x\mid x\in[a, b], f(x) =p\} $ and showing $A$ as closed.
Comment: Also if you think that $c$ could be like $b_k-1/n$ then by continuity $f(c) =f(b_k) $ which is a contradiction. Thats exactly what we mean when we say that set $A$ in previous comment is closed (ie contains its limit points). Here $b_k$ would be limit point of set $\{b_k-1/n\mid n\in \mathbb{N}, n\geq 1\} $.
Comment: @ParamanandSingh Oh, my brain might have been left from my head for a while back then. Thank you so much to pick up my mistake!
Answer: Visualizing will help with this proof.
Think about the conditions of the set $E$. If you visualize it, then you will understand it's the set of points $x$ on the real line where there is (at least) one point to the right, $x + h$, such that $G(x + h)$ is higher than $G(x)$.
Now, think about what is NOT in $E$. If $y \not\in E$, then for every point to the right of $y$, $y + h$, we must have $G(y + h) \leq G(y)$. The way I'm visualizing this is if $y \not\in E$, then if $G$ was my altitude and I was walking (from right to left), $y$ is point at which $G(y)$ is the highest altitude I have reached so far.
By definition, $a_k < b_k$, we know $a_k, b_k \not\in E$, and $G(b_k) < G(a_k)$ (all laid out in the proof). Further we know that $G$ is monotone decreasing in the interval $(a_k, b_k)$, since otherwise it would contain points that are not in $E$.

$$ \\ $$
The core continuity idea is that the function $G$ always has to tend back toward $G(b_k)$ eventually, which means it has to tend away from $\frac{G(a_k) + G(b_k)}{2}$, since by definition $G(b_k) < \frac{G(a_k) + G(b_k)}{2}$.
$$ \\ $$

Hope this helps!


Covariance between exponential and gamma distribution

Question: Define $W_{r}=\min\{t:N_{t}\geq r\}(r=1,2,...)$ as the waiting time until the $r$th arrival in a Poisson process $\{N_{t}:t \geq 0\}$ with rate $\lambda$. The question is to find $Cov(W_{1},W_{r})(r\geq 2)$.
Here is my attempt.
Since $W_{1}\sim Exp(1/\lambda)$ and $W_{r}\sim Gamma(r,1/\lambda)$, I will separate $W_{r}$ as
$$W_{r}=X_{1}+...+X_{r}$$
, where $X_{1}=W_{1}$, $X_{i}\sim Exp(1/\lambda)$, and the distributions are independent.
Then since $Cov(X,Y+Z)=Cov(X,Y)+Cov(X,Z)$,
$$Cov(W_{1},W_{r})=Cov(X_{1},X_{1})+...+Cov(X_{1},X_{r})=Var(W_{1})=1/\lambda^{2}$$
.
My question is, can I guarantee the existence of such $X_{i}$'s from the beginning?
Answer: Your work is fine. The $X_i$ have a very tangible meaning in the context of a Poisson process: they are the "inter-arrival times" between consecutive arrivals, and are independent. In fact, this is how one typically proves the distribution of $W_r$ is gamma in the first place.


Evaluation on Binary Outcome data

Question: Let us say that we have an experiment where people are shown images of tyres from car/bike and have to tell what it really belongs to.
The objective of the experiment is to validate the hypothesis that people from age group 25-35 are much better at recognizing that the tyre images are from car or bikes.
So the final data that is obtained is as follows
Sample data for the same would be : Bike = 0 Car = 1
-------------------------------------
AGE    Actual_Image  Predicted_Image
-------------------------------------
15         0              1
27         1              1
34         0              0
55         1              0

What is the correct way to see if the hypothesis is correct or not ?
Since I am a beginner in this field some links would be appreciated. I have looked at chi-square test for the same but am unable to understand how to apply it in this problem.
As a secondary question I would also like to know if d-prime value can help in this ? For example if I calculate d prime value for each group and say that the group having the highest d-prime value is the best at recognizing the images ?
P.S : Not any homework/test. Looking to learn more about validating hypothesis/ significance value etc.
Comment: Are you showing equal numbers of cars and bikes?  Do the subjects answer equal numbers of cars and bikes (unlikely)?  And are both groups shown the same set of images?
Comment: Hey Corone....If you are talking about the database, then yes it contains equal number of cars and bikes images. If the experiment is designed in terms of 10 groups : each group having unique images of bikes and cars, people across groups are not unique ie they can answer different groups also. Thus equal numbers of cars and bikes is not true per subject.
Comment: I think the research hypothesis is not well formulated. You have a well-defined group (age 25-35) and you suppose they are better at recognizing the tyres. But better than what? The average performance of all other people? This might be rather misleading. Or better than any other age group of similar size? Or better than any other non-overlapping age groupf of similar size? Or better than any set of people consisting of people of an age in a arbitary age set not containing 25-35?
Comment: @Erik I can always change the age groups of users to be as 1,2,3,4 where in 1 determines the age-group 0-5, 2 determines the age group 5-15 etc. The hypothesis would be that people in age group 4 (25-35) perform better than people in other age group ?
Answer: The first thing I would do is make a graph with "age" on the x-axis and "percent correct" on the y-axis. Depending on how much data you have, different graphs could be appropriate, but I would want to add a smooth line to this.
Then, since your hypothesis is that the relationship between age and percent correct is non-monotonic, I would look at a logistic regression where "percent correct" is the dependent variable and a spline of age is the independent variable (since you seem to have age in years, not in groups). Another alternative is to add a transform of age such as "distance from ideal group" which could be whichever is greater of a) the absolute value of age - 15 or b) the absolute value of age - 25.
Comment: Please correct me if I am wrong but isn't logistic regression used to predict the outcome of a test ? Can it be used in hypothesis testing ( to determine with some confidence that this hypothesis is more correct ). That is why I had asked about `d-prime`
Comment: Logistic regression can certainly be used to test hypotheses.

Evaluation on Binary Outcome data


Integration: Why does the integration yield $R/\Psi$?

Question: Let $\Psi=(x,t)$ and $R=R(x,t)$ and consider
$$
\Psi_xR-\Psi R_x=0.
$$
Now, it is said that integrating over $x$, gives
$$
\frac{R}{\Psi}=h(t)
$$
for some function $h(t)$.
Don't see that!
$$
\int_{-\infty}^\infty\Psi_x R-\Psi R_x\, dx=h(t)
$$
is clear, but why is the left-hand side $R/\Psi$?
Do you have an idea?
I only see that
$$
\frac{\partial}{\partial x}\frac{R}{\Psi}=\frac{R_x\Psi - R\Psi_x}{\Psi^2}
$$
so that I can instead consider the equation
$$
\int_{-\infty}^\infty R_x\Psi-R\Psi_x\, dx=g(t):=-h(t)
$$
and
$$
\int_{-\infty}^\infty R_x\Psi-R\Psi_x\, dx=\int_{-\infty}^\infty\Psi^2\frac{\partial}{\partial x}\left(\frac{R}{\Psi}\right)\, dx
$$
Answer: Assuming $\Psi$ is nonzero, you can divide through by $-\Psi^2$ to get
$$
\frac{\Psi R_x - R\Psi_x}{\Psi^2}= \frac{\partial}{\partial x}\left(\frac{R}{\Psi}\right) = 0.
$$
Then do an indefinite integration with respect to $x$ to get
$$
\frac{R}{\Psi} = h,
$$
where $h$ is some function that does not depend on $x$ (analogous to the constant of integration in single-variable indefinite integrals). Since $t$ is the only other variable it can depend on, we must have
$$
\frac{R}{\Psi} = h(t)
$$
for some single-variable function $h$.


What do I need in a home recording setup to eliminate a problem with recording and playback happening at different speeds?

Question: I'm looking to record myself playing one part at a time, but I always run into the problem that my playback slows down over time, leading me to change tempo when I shouldn't.
By way of example, if I generate a click track on my computer, and then record the click track via a microphone, the two tracks start aligned but are wildly divergent by minute three or so.
I'm pretty sure the cause has been insufficient computing power in the past; I now have the resources to get what I need to fix it, but I'm unsure what I should get. I'm currently using a Behringer Xenyx mixer for recording, but any advice I can get on software and/or hardware would be great.
Comment: No your language is clear... It's just a very odd phenomenon so i wanted to double check!
Comment: If you've generated a click track on a computer, why do you then need to record it via a microphone?
Comment: The recording of the click track was just to prove what I suspected with my ears - that the playback while recording slowed down over time. In reality I want to eliminate lag so that I can perform multiple parts of a song, one at a time, listening to the playback as I go.
Comment: So just to confirm... If you play back a click track from one track, recording it onto another track, then pay back the two tracks together, you can slowly hear them going out of sync?
Comment: Yes, that is exactly the case! Thanks for helping make my language clear.
Comment: BTW I have changed the title slightly as 'lag' or latency, while a common problem, is not the problem you are describing, and isn't usually a problem at all while recording one track at a time (unless you want to monitor through the computer). Have you tried any different buffer settings? And have you tried playing back audio that is more continuous in nature than a click track to check that it's not glitching?
Comment: @topomorto Thanks for the updates. I only experience when recording this while using computer playback, and have had it across multiple computers. I typically play back a track that I just recorded in order to record another. For example, I might lay down a percussion track, and then go back and want to play a different instrumental track over it. And then another, etc. I am using computer playback, but an answer that specifies hardware that doesn't use a computer would be fine. I haven't played with buffer settings, but I'm certainly open to trying.
Comment: BTW - are you using PC (rather than mac), and are you using an ASIO driver for your Xenyx?
Comment: @topomorto I was using PCs when this would happen, and I remember seeing "ASIO" when I tried to experiment with various settings in the past. I've been thinking of switching to a Mac for my home computer, but would be willing to buy whichever system is more likely to give me a better product. Computer minimums would also be helpful, as in the past I tended to only buy very low-end machines, but I am finally in a place where I can afford to buy better products. (Also, let me know if that's better as a separate question).
Comment: As mentioned in my answer, computer minimums aren't really likely to be the issue when it comes to this specific problem. Even a very basic computer from 15 years ago was able to do what you want to do without problems. It probably will be that upgrading your whole setup will see this particular problem disappear, because it's really not a common problem - but do see if you can acquire things in a way that allows you to try before you commit the money.
Answer: What you describe sounds like an error in the product. Take it back to where you bought it and demand the problem to be fixed, or get your money back.
It might be better to buy another audio interface. I recommend the Steinberg UR series of USB audio interfaces. Very high quality build, good sound, good solid drivers, low latency and costs almost nothing. Works on Mac, Windows and iOS/class-compliant. The UR22 has MIDI I/O, separate volume controls for headphones and stereo line outputs, two mic preamps and channel 2 has an optional Hi-Z input mode for recording guitar/bass directly into the box.
To clarify: a computer audio recording system is very complex, and most of the relevant components are not mentioned here. The problem might be in many different places, and troubleshooting it through a site like this is impossible. That's why I recommend narrowing down the search and replacing the audio interface and its drivers with known good parts that "just work". I'm not saying that the Behringer Xenyx thing couldn't be made to work with any effort, but I'm suggesting it might not be worth it. I have a Xenyx 302USB, and I've used it as a simple line mixer and adjusting levels and EQ when digitizing old analog video recordings. It did seem to work for USB audio I/O at least so I could record something with it, but it's in every way a cheap device. It's a bit noisy, and IIRC there's no way to monitor only the computer's USB signal when playing live, which is a must to use computer based guitar amp simulators - and you can't get the USB audio to work with very low latencies either. Very cheap little thing, and if you are able to do something with it, it's a nice bonus.
Comment: Thanks for this answer; I'm very open to another (better) interface. I see computer minimums as well for these products online, which are helpful. If you also had suggestions as to computer minimums I would take them, but otherwise this is very helpful - thanks!
Answer: This sounds very odd! Normally, playing back one audio track while recording another shouldn't result in any obvious timing issues; there obviously are delays (latency) in getting the audio into and out of the system, but an interesting characteristic of ASIO drivers is that they are aware of these delays, which allows your recording software to compensate for them so that each newly-recorded track lines up properly with the tracks previously laid down. This has usually worked properly for me even on the much lower-powered systems of 15 years ago; the only time it didn't work was on a version of Adobe Audition that genuinely hadn't been coded to work properly for this scenario, and even then, it didn't give me issues with audio getting progressively further out of sync.
The only explanation I can think of is that your played-back audio is suffering from buffer underruns, which is causing very small pauses in the played-back audio, which over time might cause things to go out of sync. I don't even think that explanation is likely though, as AFAIR ASIO drivers deal with recording and playback in the same callback, which should mean that they are always dealing with sets of samples that correspond properly.
So apologies for a non-answer, but I agree with piiperi that you might just need to try to do some component swapping to narrow down the problem. A different interface would be a good place to start (I'm currently happily using Behringer's own UMC204HD), but perhaps try a different computer, with a trial version of a different DAW too, to try to get away fully from the ghosts in the machine...
Comment: We don't know if it's an ASIO setup or something else, and even then there might be hidden automatic samplerate conversions or something happening behind the scenes. Some "multimedia" drivers do that.
Comment: @piiperi good point, I was assuming ASIO - have asked for clarification under the question.
Comment: @piiperi Echoing my comment in the question, what do you recommend in terms of a computer (Mac or PC)? I've always had fairly low-end machines, but am finally in a place where I can purchase something decent.
Comment: @mkdir both are usable. With Windows PCs you might get a bit more CPU power for the same money if you build it yourself, but on Macs, things tend to "just work" better. Plug and play devices on the fly, low latency and smooth operation by default even with built-in audio. With PCs it's more hit and miss and you better build the whole thing yourself. If something is integrated or pre-installed on a PC, it means trouble. You have to be somewhat of a computer expert to use PCs. If you only want to get music done, get a Mac. It works for audio out of the box.