How and when to use the generic type parameter in impl block in Rust?

Question: I am confused about the generic parameter for the impl keyword in Rust.
To explain what I am confused about, I am currently going through the rustling exercises. I am doing the generics part. The second question which can be seen here is to make the Wrapper struct generic.
Easy, the solution is:
struct Wrapper<T> {
    value: T,
}

impl<T> Wrapper<T> {
    pub fn new(value: T) -> Self {
        Wrapper { value }
    }
}

But I ask myself, why does impl have to also have a type parameter? Why impl<T> Wrapper<T> and not just impl Wrapper<T>?
I could not answer that question but on moving on to the next exercise, which can be seen here there is another generic question, which I solved by having this:
use std::fmt::Display;

pub struct ReportCard<T:Display> {
    pub grade: T,
    pub student_name: String,
    pub student_age: u8,
}

impl<T:Display> ReportCard<T> {
    pub fn print(&self) -> String {
        format!("{} ({}) - achieved a grade of {}",
            &self.student_name, &self.student_age, &self.grade.to_string())
    }
}

Now I am wondering, why is this right, and the following wrong:
pub struct ReportCard<T:Display> {
    pub grade: T,
    pub student_name: String,
    pub student_age: u8,
}

impl<T:Display> ReportCard<T:Display> {
    pub fn print(&self) -> String {
        format!("{} ({}) - achieved a grade of {}",
            &self.student_name, &self.student_age, &self.grade.to_string())
    }
}

I think the crux of my problem is, I do not really understand the generics syntax. And where to put the generic type parameter, especially when there is an impl block.
Does anyone have an explanation on how these syntax is supposed to be used?
Comment: Don't forget that you can write `impl ReportCard { fn some_function_here() { ... } }`. Does that help?
Answer: You could think of it the same way a function takes parameters, and can feed back these parameters in a function call, and it will probably make sense.
A generic type Wrapper<T> is "like" a function that takes a type T, and produces an actual, concrete type Wrapper<T> (even though there is no difference in the syntax, just like there is no difference when you define a function and when you call it to get an actual value out of it).
Similarly, an impl block is "like" a function that may take some parameters, and that produces a given implementation for a given type. That is, impl<T> Wrapper<T> { ... } should be read like define a generic implementation that takes a parameter, T, and that produces an  actual, concrete implementation { ... } for the concrete type Wrapper<T>, which, in this case, can be seen as the "function call" rather than the "signature declaration".
But, for instance, you could also choose not to make your implementation generic: impl Wrapper<u32> { ... }, or even to make it generic over several arguments, not use none in the Wrapper<T> "call": impl<T, U> Wrapper<char> { ... } because these type parameters can also be used in the actual implementation.

This being said, we can now look at the syntax more in details. Usually, when you define a "generic block" (that is, a generic impl, or a generic struct, or a generic enum, or ...), the first keyword of that block may also specify some times with the <T, U, ...> syntax, which you may think as in a function definition. Every subsequent usage of these types in the block through the same syntax may be though of as a "function call": you feed these arguments into an other "function".
For this reason, you can only add constraints over these types in the first <...> part of an impl block, not after the type:
impl<T: Display> Wrapper<T> { ... }

is valid, because it means given a type T such that T implements the trait Display, I can provide an implementation for the concrete type Wrapper<T>, but
impl<T: Display> Wrapper<T: Display> { ... }

is not valid, and neither is
impl<T> Wrapper<T: Display> { ... }

because you are putting constrains on a "function call". Just like
fn something(a: usize) {
  something_else(a);
}

is valid, but not
fn something(a: usize) {
  something_else(a: usize)
}

nor
fn something(a) {
  something_else(a: usize)
}


When is a relation a function?

Question: Can you explain the difference between a function and a relation and how a function is a subset of a relation, and when a relation is a function and when not?
Also, what is the domain, co-domain and range of a function?
All I've found on the internet and books has been a bunch of definitions which I can't make sense out of. An intuitive explanation or any understandable illustration would be much appreciated.
Comment: google it......
Comment: **Consider $$\{(1,2),(1,3)\}$$as a relation not a function and $$\{(1,2),(3,4)\}$$ as both a relation and a function. Can you point out the difference?**
Comment: @Mostafa Ayaz There's no number paired twice in the second one?
Comment: Not necessarily. Also $\{(2,1),(3,1)\}$ is a function. In fact if $(x,y)$ and $(a,b)$ are included in a function, then $x=a$ implies $y=b$.
Comment: @Mostafa Ayaz So for a set of ordered pairs to be a function, the first element of each can't be paired with multiple elements? x = a would then imply y = b because it has to otherwise the set wouldn't be a function?
Comment: That's right and followed by the definition of the functions...
Comment: "and followed by the definition of the functions..."?
Answer: A relation R is a function when for all x,y,z, (xRy and xRz implies y = z).
The domain of a function f is the 1st projection of f.
{ x : exists y with (x,y) in f }
The range of a function f is the 2nd projection of f.
{ y : exists x with (x,y) in f }
The codomain of a function is arbitray as long as the range is a subset of it.

When is a relation a function?


How do I count ordered tuples in this inequality?

Question: I have trouble seeing how the total number of k-tuples $(\phi_1 , \phi_2 ,..., \phi_k)$ satisfying the inequality
$1 \leq \phi_1 < \phi_2 < ... < \phi_k \leq n \qquad $  with $k \leq n$
is $\displaystyle \binom{n}{k}$.
Can someone shed some light on this by suggesting a combinatorial argument or some other suitable alternative? Many thanks.
Answer: There is only 1 way to order a k-tuple in increasing order
Comment: Thanks, annoyingly that was all that kept me back from understanding.
Answer: Identify such a sequence $(\phi_1,\phi_2,\dots,\phi_n)$ with a sequence $(a_1,a_2,\dots,a_n)$ of $n-k$ zeroes and $k$ ones with the $a_{\phi_i} = 1$ and the others $0$.


What should I use instead of windows.h in Linux?

Question: I am trying to port a Windows program to Linux. I have never programmed in Linux and have very little idea what I am doing. I have managed to eliminate a lot of the errors I got in the G++ compiler on Linux and have traced most of the remaining ones to non existent variable types.
I took out the windows.h file but I do know know what to do with the CALLBACK, HANDLE, DWORD and HHOOK variables. Apparently there is no equivalent to HANDLE in Linux so I think I may have to rewrite some of the structure.
Can anyone explain what I should be doing or point me to some tutorials that teach me how to do these things in Linux?
My program runs a polling loop on an RS-485 network if that helps.
Comment: You haven't said what your code does, so no. it may surprise you, but describing **all** the differences in programming Windows vs Linux requires more space than a SO answer typically contains. There isn't a magical `ConvertWindowsToLinux()` function. They're different OS'es with different APIs. In order to do something using the Linux APIs (POSIX, really), you'll need to start by defining *what you want to do*.
Comment: If you want GUI programming in Linux, it's called "X" programming, and it's nowhere related to Win32.
Answer: The missing typedefs (HANDLE etc.) aren't your problem. Your problem is that Linux and Windows have completely different APIs, you cannot simply hope to port one to the other by replacing a few type definitions.
The complete platform-dependent portion of your code has to be replaced. Your first step is therefore to learn the Linux API. The best way of doing this is getting a book on Linux programming.
Furthermore, Linux doesn't provide a default API for window management as does Windows so if you are programming a graphical application then you need to choose a windowing library as well.
Answer: There's no "equivalent", so to speak, for windows.h in Linux, you need to fix your errors case by case, or better, rewrite your code for linux (if it's not too complicated).
However, if we ignore windows specific APIs, you may be able to fix typedef errors (DWORD, HANDLE, ...):
#ifndef DWORD
#define WINAPI
typedef unsigned long DWORD;
typedef short WCHAR;
typedef void * HANDLE;
#define MAX_PATH    PATH_MAX
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int BOOL;
#endif

typedef source code
Answer: Having windows.h means that your application uses the API of the Windows operating system, there is no 1-to-1 mapping to libraries on Linux.
You can consider running your application under Wine if you don't want to port the application.
Answer: You have 2 options.

Typedef your missing types to types that are suitable for your new OS.
Rewrite the code not to use those types.

I doubt that anyone will be able to tell you what you should do with the port without knowing the specifics of each case. The thing about windows.h is that it relies on the windows OS... you'll have to change the functions that you are calling from it. In that case you will no longer be using the types that you do not have.
Basically you'll have to understand what the windows API is being used for and find the equivalent functions for your target OS.
Answer: Linux has quite a different programming model to Windows. If you are interest in programming a serial port (RS485 is similar to RS232) here is a tutorial that should show you the basics.


Como garantir que uma SESSION expire caso o usuário acesse uma cópia da aplicação que está em um outro diretório

Question: Tenho uma aplicação em php onde utilizo sessions para controle do login.
A aplicação está em meusite.com/minhaApp (vou chamar de App1)
Ocorre que para testes eu criei uma instância da minha aplicação em outro diretório.
meusite.com/outroDir/minhaApp  (vou chamar de App2)
Depois que fiz está modificação, verifiquei que uma vez criado um acesso e armazenado uma SESSION, caso o usuário acesse App1 e depois mude a url para acessar App2.  A mesma SESSION permanece ativa. Fazendo com que meu controle de login falhe.
Alguém teria uma dica para lidar com isso? Teria que fazer alguma verificação de variável de ambiente do servidor como HTTP_REFERER?
Comment: Seria muito melhor separar as "hospedagens" locais pra faciltar a vida. Inclusive pq esse tipo de "organização" em pastas geralmente não organiza muito as coisas se na hospedagem definitiva a aplicação for ficar na raiz do site (geralmente as pessoas usam caminhos relativos onde deveria ser absoluto justamente por causa das pastas). Duas soluções mais óbvias pra fazer testes são a "hospedagem" em portas diferentes, ou melhor ainda, criar URLs de teste no "hosts" do sistema apontando pro 127.0.0.1 (ex: app1.bruno e app2.bruno).
Answer: Em vez de mudar a lógica do sistema, uma solução simples é você mudar o nome do cookie de sessão de acordo com a pasta, antes do session_start().
Fazendo isso, você tem sessões totalmente independentes, mas simultâneas:
 <?php
    // inicio do bloco de teste
    $independentes = array( 'app1', 'app2' );

    $caminho = explode( '/', $_SERVER['PATH_INFO'] );
    $appnumber = array_search( $caminho[1], $independentes );
    session_name( 'PHPSID_'.( $appnumber === false ? 0 : $appnumber + 1 ) );
    // fim do bloco de teste

    session_start();

Basicamente estamos pegando o segundo ítem do caminho dividido pelas barras (o primeiro é vazio, pois o PATH_INFO começa com /), localizando a posição dele num array com o nome das pastas, e adicionando sua posição ao nome do cookie de sessão, fazendo com que cada situação tenha uma sessão totalmente independente.
PS: Se não estiver usando CGI nem Apache, troque o PATH_INFO por REQUEST_URI.
No caso, compensa criar um include com as linhas do bloco de teste, e dar um require_once() nas suas páginas que usam sessão. Fazendo isso, você pode testar quantas pastas diferentes quiser com sessões independentes, simultaneamente. Basta colocar o nome da pasta raiz de cada uma das aplicações no lugar de app1 e app2 no array.
Exemplo:
aplicação 0 em   http://127.0.0.1/...
aplicação 1 em   http://127.0.0.1/teste_a/...
aplicação 2 em   http://127.0.0.1/teste_b/...
aplicação 3 em   http://127.0.0.1/teste_c/...

Configuração:
$independentes = array( 'teste_a', 'teste_b', 'teste_c' );

Tudo que estiver fora dos caminhos teste_a, teste_b  e teste_c, ou em caminhos que não estejam na lista, vai ser considerado como parte da aplicação padrão (0).
Reaproveitando em várias páginas:
Pra aplicar a solução em várias páginas, pode salvar esse arquivo como session_start.php, por exemplo:
<?php
   $independentes = array( 'app1', 'app2' );

   $caminho = explode( '/', $_SERVER['PATH_INFO'] );
   $appnumber = array_search( $caminho[1], $independentes );
   session_name( 'PHPSID_'.( $appnumber === false ? 0 : $appnumber + 1 ) );

   session_start();

E simplesmente usar com require_once() em todas as páginas, no lugar do session_start() original:
<?php
   require_once( 'session_start.php' );

   // ... resto do seu código  ... //

Answer: Sobre o HTTP_REFERER:
O HTTP_REFERER é extremamente vulnerável neste caso. Uma vez que ele pode ser editado e apagado com tranquilidade no lado do cliente, portanto não acredite nele, você pode usar o HTTP_REFERER como um complemento, mas não unicamente ele.

Nota: Se o usuário copiar o link e abrir uma nova página o Referer irá deixar de existir, apenas como complemento existem plugins para eliminar Referer, por motivos de privacidade.

Correção do problema:
Supondo que exista:
meusite.com/App1/index.php
meusite.com/App2/index.php
meusite.com/OutroDir/App3/index.php

A forma mais fácil seria comparar se a sessão tem acesso ao App que deseja.
Por exemplo:
// Restringe acesso ao App1:
   $acesso = array('App1');
   $_SESSION['acesso'] = $acesso;

// Restringe acesso ao App1 e App2:
   $acesso = array('App1', 'App2');
   $_SESSION['acesso'] = $acesso;

Dessa forma determina que o usuário terá acesso somente ao App1 e no outro caso o usuário teria acesso ao App1 e também ao App2.
Dessa forma, os seus aplicativos (App1, App2 e App3) teriam que verificar se o usuário está ou não autorizado a fazer isso.
Você pode usar algo desta forma:
function VerificarSessao($FSession, $FApp){

   return in_array($FApp, $FSession);

}

Então chame por:
$Autorizado = VerificarSessao( $_SESSION['acesso'] , basename(__DIR__) );

if($Autorizado){
  echo 'Você pode acessar!";
}else{
  echo 'Você não pode acessar!";
}

Dessa forma você irá verificar se o diretorio (no caso /App1, /App2, /App3) estão autorizados na sua sessão.

Nota: Eu preferir por criar a função exigindo passar os dois parâmetros para que compreendesse o funcionamento de forma mais clara, por isso está extremamente simples, porém poderá colocar um header('location: /erro.php'); exit; dentro da função ao invés de retornar true/false, por exemplo. Enfim.


QR decomposition and a change of basis

Question: Consider a square matrix $A$ and its $QR$ decomposition $A = QR$, where $Q$ is orthogonal and $R$ is upper triangular matrix. Now consider a change of basis. Let
$A' = PAP^T$, where $P$ is an orthogonal matrix. Is there a way how to compute a $QR$ decomposition of $A'$ directly from $Q, R$ and $P$, without actually computing $A'$? In other words, if I know $Q$ and $R$, is there a way how to obtain $Q'$ and $R'$ without actually computing $A'$ and doing $QR$ decomposition?
Answer: If $A=QR,$ then $A'=PAP^T=PQRP^T=(PQP^T)(PRP^T).$
The matrix $PQP^T$ is orthogonal. Once you find a decomposition of
$PRP^T=Q'R',$ where $Q'$ is orthogonal and $R'$ is upper triangular, you get
$PAP^T=(PQP^T)Q'R'.$ The matrix $(PQP^T)Q'$ is orthogonal as the product of two orthogonal matrices. In this way knowing $Q$ and $R$ is sufficient for determining
the decomposition of $A'.$
Comment: Thank you for your answer, however I was hoping for a way that would not require to compute a QR decomposition.

QR decomposition and a change of basis


ajax_command_invoke addClass changes lost after form validation

Question: I am currently trying to build a form which has a series of select elements, each of which triggers display of a textarea specific to that select element which is initially hidden.
Currently the select element has a callback to a function which is using ajax_command_css (to toggle the view of the textbox) and also ajax_command_invoke to add a class to the textbox so that it is still shown after validation errors are triggered. Currently after a validation event, the classes added using this method are lost so the textboxes remain hidden.
Select and textareas are created like this:
$form['2og_select'] = array(
    '#prefix' => $blurb,
    '#title' => t('2OG'),
    '#type' => 'select',
    '#options' => array(
      t(' '),t('1'),t('2'),t('3'),t('4'),t('5'),
    ),
    '#ajax' => array(
      'callback' => 'show_comments_js',
      'event' => 'change',
      'effect' => 'fade',
      'progress' => array(
        'message' => '',
        'type' => 'throbber',
      ),
    ),
  );
  $form['2og_textarea'] = array(
    '#type' => 'textfield',
    '#maxlength' => 512,
    '#suffix' => ('

'),
    '#attributes' => array(
      'class' => array('comments'),
    ),
  );

Callback function looks like this:
function show_comments_js($form, &$form_state) {

  $commands = array();

  $triggerName = $form_state['triggering_element']['#name'];
  $triggerValue = $form_state['triggering_element']['#value'];

  $name_bits = explode('_',$triggerName);
  if ($triggerValue != '0') {
    $commands[] = ajax_command_css('#edit-' . $name_bits[0]  . '-textarea', array('display' => 'inline'));
    $commands[] = ajax_command_invoke('#edit-' . $name_bits[0]  . '-textarea', addClass, array('show-comments'));
  }
  else {
    $commands[] = ajax_command_css('#edit-' . $name_bits[0]  . '-textarea', array('display' => 'none'));
    $commands[] = ajax_command_invoke('#edit-' . $name_bits[0]  . '-textarea', removeClass, array('show-comments'));
  }

  return array('#type' => 'ajax', '#commands' => $commands);
}

I have also noticed that the id of the form has '--2' appended to it after submitting, could this be related?
Would using an ajax validation tied to the submit button have the same effect?
Answer: Yes, the IDs of an element change when it's being reloaded using AJAX. That's supposed to be a feature. :/ You might try using a class to target your textareas instead.
Comment: Cheers, that's confirmed what I suspected. The only problem is, the form isn't retaining the classes added between submitting and validating.
Answer: What I did to get round this was to use the highly useful #states property in the Form API - set the textarea display to be dependent on the select box having a value other than 0 selected. Did the trick without too much hassle!
For an example of this go to
form_example_states_form documentation


What does it mean if Nemenyi test does not return that the performance of two methods are significantly different while the Wilcoxon test does?

Question: I want to compare the performance of $N$ methods on $m$ datasets. I performed the Friedman test and after that the Nemenyi test.
The Nemenyi test is though not powerful enough to conclude that there is a significant difference between the performance of the best performing method and the second performing method.
I then used the Wilcoxon method to compare the performance of these two models, and Wilcoxon method indeed returns that the difference in their performance is significant.
What can I conclude? Can I conclude that the difference in performance of the two models is statistically significant?
Answer: Per Wikipedia on Wilcoxon signed-rank test:

Assumptions

Data are paired and come from the same population.

Each pair is chosen randomly and independently[citation needed].

The data are measured on at least an interval scale when, as is usual, within-pair differences are calculated to perform the test (though it does suffice that within-pair comparisons are on an ordinal scale).

Now, the Friedman test, a commonly employed non-parametric test for complete block designs is related to the non-parametric Durbin test as it  reduces to the Friedman test for complete block design scenarios. The cited underlying assumptions per a source are:

The b blocks are mutually independent. That means the results within one block do not affect the results within other blocks.

The data can be meaningfully ranked (i.e., the data have at least an ordinal scale).

The Nemenyi post-hoc test, however, has been discussed previously on this forum here, and the appropriate underlying assumptions are those that apply for the use of the studentized range distribution. Per Wikipedia, these are:

Assumptions

The observations being tested are independent within and among the groups.

The groups associated with each mean in the test are normally distributed.

There is equal within-group variance across the groups associated with each mean in the test (homogeneity of variance).

Note, the Nemenyi test has an implicit assumption normality of means. This assumption is absent elsewhere, as such, I would more likely suspect, that there is a non-normality issue. Also, per the references cited, be mindful of the 'family of test' significance level adjustment.
Comment: couldn't it be that the Nemenyi test is not powerful enough? I obtain CD (critical distance) equal to 1.64. Thus, even though the best performing algorithm is always ranked first and the second is always ranked second I get that there is no significant difference
Comment: Note, there is also a homogeneity of variance assumption in addition to normality of means. With these assumptions, I would hope for more power and not less.
Comment: Here's another source (specifically for Friedman) stating that the blocks ("rows") must be independent: https://onlinelibrary.wiley.com/doi/epdf/10.1002/9781118445112.stat06549
Comment: To clarify, this should be interpreted as the answerer describes above, that each of the rows is independent from each of the other rows.


Distributional derivative of $\sin { (\pi|x|/2))} \chi_{\{|x|<2\}}$

Question: I have to find the  first and the second distributional  derivative of the function :
$$f(x) = \begin{cases} \sin { (\pi|x|/2))}  \quad & |x| <2 \\ 0 \quad & |x| \geq 2 \end{cases}$$
  but I am not sure  if I am doing it  in the right way.
The first derivative should be
 $$ g'(x) = \cos { (\pi|x|/2))}\operatorname{sgn}(x) \pi/2 $$
because  $f(x)$ is a continuous function  with no jumps in any point so the delta does not appear.
The second derivative should be :
$$ g''(x) = - \sin { (\pi|x|/2))}[\operatorname{ sgn}(x)]^2 (\pi/2)^2 +    \delta _2   -\delta _{(-2)}   $$
I have never seen a distribution  with jumps in different points before, am I correct if I apply the delta in each of this points? Also I am not sure if the jump is $+1$ or $-1$.
Answer: The distributional derivative of a piecewise smooth function consists of two terms:

pointwise derivative, found as in calculus
singular part: if $f$ is discontinuous at $a$, add $(f(a+)-f(a-))\delta_a$; this is done for each discontinuity.

You correctly found that $f'$ has no singular part. However, note that the formula for $f'$  should also say that it's zero when $|x|>2$.
The function $f'$ is discontinuous at three points: $-2,0,2$.

at $-2$  the left limit is $0$, the right limit is $\pi/2$
at $0$  the left limit is $-\pi/2$, the right limit is $\pi/2$
at $ 2$  the left limit is $-\pi/2$, the right limit is $0$

So, the singular part of $f''$ is
$$\frac{\pi}{2} (\delta_{-2} +2\delta_0 +\delta_2)$$
You found the continuous part of $f''$ correctly, but it can be simplified to
$$- \sin { (\pi|x|/2))}  (\pi/2)^2$$
And again, $f''(x)$ is zero when $|x|>2$.
Comment: if I compute the fourier transform of $f''$ as a distribution do I have  to sum the fourier transform of the regular part  with the fourier transforms of those $\delta$?  initially  I thought it was  just $\hat f(k) (i2\pi k)^2 $ but now I think it is wrong
Comment: Yes, if you want to do it directly. But it may be easier to do what you thought initially: use a formula for $\widehat{f''}$ in terms of $\widehat{f}$.
Comment: Yes, they should both give the same result if your computation is correct. A nice thing about distributions is that some things that work for test functions automatically  work for distributions too, because one can pass both the derivatives and the Fourier hat onto the test function.
Comment: does the formula $\hat( f^m) = (i2 \pi k ) ^m \hat f $ imply the derivative  are  in the sense of distribution? because i thought  that formula was valid only for classic pointwise derivative. edit: ok i thing i got it, ty.
Answer: $\newcommand{\angles}[1]{\left\langle\, #1 \,\right\rangle}
 \newcommand{\braces}[1]{\left\lbrace\, #1 \,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\, #1 \,\right\rbrack}
 \newcommand{\ceil}[1]{\,\left\lceil\, #1 \,\right\rceil\,}
 \newcommand{\dd}{{\rm d}}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\dsc}[1]{\displaystyle{\color{red}{#1}}}
 \newcommand{\expo}[1]{\,{\rm e}^{#1}\,}
 \newcommand{\fermi}{\,{\rm f}}
 \newcommand{\floor}[1]{\,\left\lfloor #1 \right\rfloor\,}
 \newcommand{\half}{{1 \over 2}}
 \newcommand{\ic}{{\rm i}}
 \newcommand{\iff}{\Longleftrightarrow}
 \newcommand{\imp}{\Longrightarrow}
 \newcommand{\Li}[1]{\,{\rm Li}_{#1}}
 \newcommand{\norm}[1]{\left\vert\left\vert\, #1\,\right\vert\right\vert}
 \newcommand{\pars}[1]{\left(\, #1 \,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\pp}{{\cal P}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\vphantom{\large A}\,#2\,}\,}
 \newcommand{\sech}{\,{\rm sech}}
 \newcommand{\sgn}{\,{\rm sgn}}
 \newcommand{\totald}[3][]{\frac{{\rm d}^{#1} #2}{{\rm d} #3^{#1}}}
 \newcommand{\ul}[1]{\underline{#1}}
 \newcommand{\verts}[1]{\left\vert\, #1 \,\right\vert}$
$\ds{\fermi\pars{x} \equiv
     \Theta\pars{2 - \verts{x}}\sin\pars{{\pi \over 2}\,\verts{x}}}$

\begin{align}
\color{#66f}{\large\fermi'\pars{x}}&
=\delta\pars{2 - \verts{x}}\bracks{-\sgn\pars{x}}
\sin\pars{{\pi \over 2}\,\verts{x}}
+\Theta\pars{2 - \verts{x}}\cos\pars{{\pi \over 2}\,\verts{x}}
\,{\pi \over 2}\,\sgn\pars{x}
\\[5mm]&=\color{#66f}{\large{\pi \over 2}\,\Theta\pars{2 - \verts{x}}
\cos\pars{{\pi \over 2}\,\verts{x}}\sgn\pars{x}}
\\[1cm]\color{#66f}{\large\fermi''\pars{x}}&
={\pi \over 2}\,\delta\pars{2 - \verts{x}}\bracks{-\sgn\pars{x}}
\cos\pars{{\pi \over 2}\,\verts{x}}\sgn\pars{x}
\\[5mm]&+{\pi \over 2}\,\Theta\pars{2 - \verts{x}}
\bracks{-\sin\pars{{\pi \over 2}\,\verts{x}}
\,{\pi \over 2}\,\sgn\pars{x}}\sgn\pars{x}
\\[5mm]&+{\pi \over 2}\,\Theta\pars{2 - \verts{x}}
\cos\pars{{\pi \over 2}\,\verts{x}}\bracks{2\,\delta\pars{x}}
\\[5mm]&={\pi \over 2}\,\delta\pars{\verts{x} - 2}
-{\pi^{2} \over 4}\,\Theta\pars{2 - \verts{x}}\sin\pars{{\pi \over 2}\,\verts{x}}
+\pi\,\delta\pars{x}
\\[5mm]&=\color{#66f}{\large\pi\,\delta\pars{x} + {\pi \over 2}\,\delta\pars{x + 2}
+ {\pi \over 2}\,\delta\pars{x - 2}}
\\[5mm]&
\color{#66f}{\large\phantom{=}-{\pi^{2} \over 4}\,\Theta\pars{2 - \verts{x}}\sin\pars{{\pi \over 2}\,\verts{x}}}
\end{align}
Comment: What is that $\Theta$ ?
Comment: @Alucard $\Theta\left(\,x\,\right)$ is the [Heaviside Step Function](http://en.wikipedia.org/wiki/Heaviside_step_function). Physicists usually use $\Theta$ instead of ${\rm H}$ to avoid confusion with 'Magnetic Field', 'Harmonic Number', 'Hamiltonian', etc...
Comment: i cannot understand in the last line why there is  $\pi/2 \delta(x+2) + \pi/2 \delta(x-2)$
Comment: @Alucard I use:
$$
\delta\left(\,{\rm g}\left(\,x\,\right)\,\right)
=\sum_{n}{\delta\left(\,x - x_{n}\,\right)\over
\left\vert\,{\rm g}'\left(\,x_{n}\,\right)\,\right\vert}
$$
where $\,{\rm g}\left(\,x_{n}\,\right) = 0$. With $\,{\rm g}\left(\,x\,\right)=\delta\left(\,2 - \left\vert\,x\right\vert\,\right)$ it becomes:
$$
\delta\left(\,2 - \left\vert\,x\right\vert\,\right)
={\delta\left(\,x + 2\,\right) \over \left\vert\,{\rm sgn}\left(\,x\,\right)\,\right\vert}
+
{\delta\left(\,x - 2\,\right) \over \left\vert\,{\rm sgn}\left(\,x\,\right)\,\right\vert}
$$
Comment: @Alucard ...which yields
$$
\delta\left(\,x + 2\,\right) + \delta\left(\,x - 2\,\right)
$$
Comment: do you know where   i can  find a step-by-step  proof  of this rule ?
Comment: @Alucard [Just here](http://en.wikipedia.org/wiki/Dirac_delta_function).


Index and normal subgroups

Question: I want to show the following.
For an infinite group G with only two normal subgroups (G and {e}) holds:
There does not exist a non-trivial subgroup of G with finite index.
I think i should prove this by contradiction. So there exists a partition of G:
Let U be a non-trivial subgroup of G, it holds:
G=$g_1U\cup g_2U\cup...\cup g_nU$ for some $g_1,...g_n$.
Maybe there is a possibility to show, that the gU are finite?
Answer: Hint: Suppose $G$ has a subgroup $H$ of finite index, then let $G$ act on the left cosets of $H$ by left translation. Look at the kernel of this action.
Comment: Okay. I have done this so far. The kernel of the action is the set $Core_G(H):=\cap_{g\in G}(gHg^{-1})$. This is a normal subgroup of G. So by assumption this is {e} or G, right? H cant be {e}, because then it would be H={e}, a contradiction (Because we choose H as a no-trivial subgroup). So G=$Core_G(U)$ now we want to conclude in some way that G must be finite, right?
Comment: Edit: If my way is right so far, then let me continue:We want to show that G=$Core_G(U)$ is impossible. Lets assume that this is true. Then we chose a g with $g \in G$ and $g\notin H$. Then it must be g $\in$ $gHg^{-1}$ which is equivalent to $g^{-1} \in H$. Thats impossible. So the assertion follows.
Answer: I just want to complete this prove for others and for completeness: As stated from me above there are only two possibilities for $Core_G(U)$ First:  $Core_G(U)=\{e\}$ and second:  $Core_G(U)=G$, because G has only two normal subgroups (G and $\{e\}$) and $Core_G(U)$ is clearly a normal subgroup of G. We want now get a contradiction:
1) If $Core_G(U)=\{e\}$ We get a contradiction because then G is finite by lagrange's theorem.
2) If $Core_G(U)=G$ then we want to show that $U=G$ which would be a contradiction to the chose of U and we where finish (We chose U as non-trivial)
Prove by contradiction: If $U\neq G$ then there is an element $h$ with $h \in G$ and $h\notin U$. By definition of the Core it holds $h\in gUg^{-1}$ $\forall g \in G$, so especially for $g=e$. But then $h\in U$ with is a contradiction.

Index and normal subgroups


Retornar E-mail de amigos Facebook friend_list

Question: Utilizando a graph-API do Facebook eu consigo obter vários dados do usuário que está logado em meu sistema (a lista completa pode ser vista na doc do Facebook). Porém, gostaria de saber se existe uma forma de obter os dados de meus amigos, para ser mais específico, o e-mail.
Sei que posso utilizar a taggable_friends para marcar amigos que não utilizam minha aplicação, e a invitable_friends para poder convidar amigos (caso utilize canvas). Porém, em nenhuma dessas formas eu consigo recuperar o e-mail do amigo?
Grandes empresas, como o Yahoo conseguem realizar essa importação de emails. É possível realizar isso através da API do facebook?
Answer: Infelizmente o Facebook restringiu algumas funcionalidades a fim de mater a privacidade dos usuários. Com isso, não é possível obter o e-mail da forma desejada.
Grandes empresas possuem um "acordo de senhores" para obterem esses dados, o que é normal em qualquer grande API no mercado.


Martingale regularization with right continuous filtration

Question: The standard textbook presentation of the Doob Regularization Theorem for a martingale $(X_t, \mathcal{F}_t)$ assumes that the filtration satisfies the usual conditions.  It is clear that the assumption of right continuity of the filtration is critical (both in showing the adaptedness of the modification $X^+_t = \lim_{q \to t^+} X_t$ as well as showing the martingale property of $X^+$ using the Levy Downward Theorem).  On the other hand, it is clear that the assumption of completeness is stronger than necessary for all we really need to assume is that $\mathcal{F}_0$ contains the null event where the limits $\lim_{q \to t^+} X_q$ and $\lim_{q \to t^-}X_q$ fail to exist: this assumption being used to conclude that $X^+$ is $\mathcal{F}$-adapted.
The question is that I occasionally run into scenarios in which the existence of a cadlag version is asserted with only an assumption of right continuity in the filtration.  This is not uncommon in Girsanov Theory in which the usual conditions cause subtle problems.  For example on pg. 325 of Revuz and Yor we have the statement "These (classes of) random variables form a $(\mathcal{F}^0_t, P)$-martingale and since $(\mathcal{F}^0_t)$ is right continuous, we may choose $D_t$ within its $P$-equivalence class so that the resulting process $D$ is a $(\mathcal{F}^0_t)$-adapted martingale almost every path of which is cadlag...".  There are other examples that I can quote (even Kallenberg is a bit slippery on this point).
Without some a priori knowledge that right and left limits exist prior to modification I don't see how this can be true; we can certainly modify to a cadlag process within the $P$-equivalence class but without further assumptions on the filtration we destroy $\mathcal{F}$-adaptedness.  Is there something that I am missing here?
Comment: @ David Blair : On the other hand for Markovian process (even Feller if not mistaken) it suffices to have the completion of the initial sigma algebra to imply the right continuity of the filtration (You can look at P. Protter's book where this point is proved). Best regards.
Answer: I think I have the result that you are looking for. It is called "Föllmer's lemma" (theorem 2.44 chapter II section 5 in the book "Semimartingale Theory and Stochastic Calculus" by Gang Wang, Jia-An Yan and here is how it goes :
(of course you are given a stochastic basis with no particular assumptions regarding the regularity of the filtration)
Let's be given $(X_t)_{t\in \mathbb{R}^+}$ a supermartingale (resp. martingale) and $D$ a dense subset of $\mathbb{R}^+$.
Then there exists a process $\bar{X}_t$ such that the following four points hold :
1-$(\bar{X}_t)_{t\in \mathbb{R}^+}$ is right continuous for almost all $\omega$, for any $t\in \mathbb{R}^+$ ; $\bar{X_t}(\omega)=\lim_{(s\in D,s \searrow \searrow t )}X_s(\omega)$.
2-for almost all $\omega$, for any $t>0$,
$\bar{X}_{t_-}(\omega)=\lim_{(s\in \mathbb{R}^+,s \nearrow \nearrow t )}\bar{X}_s(\omega)$ exists and is finite. And we have moreover :
$\bar{X}_{t_-}(\omega)=\lim_{(s\in D,s \nearrow \nearrow t )}X_s(\omega)$
3- For all $t\in\mathbb{R}^+$ :
$X_t\geq \mathbb{E}[\bar{X}_t|\mathcal{F}_t]$ (resp. equality for the martingale case)
4- $(\bar{X}_t)_{t\in \mathbb{R}^+}$ is a $\mathcal{F}_+$-supermartingale (resp. martingale). Where $\mathcal{F}_+$ is the right continuous filtration generated by the filtration $\mathcal{F}$.
As you can notice there is no completion of the filtration at time 0 by the negligible sets.
Comment: I think this works.  Just define $A_t$ to be the event that the limits $\lim_{q \to t^+} X_q$ exist.  Let $\overline{X}_t = 1_{A_t} \lim_{q \to t^+} X_q$ and note that $\overline{X}$ is $\mathcal{F}^+$ adapted.  If we let $A = \cap_t A_t$ then $\overline{X}$ agrees on $A$ with the everywhere cadlag version $\tilde{X}_t = 1_{A} \lim_{q \to t^+} X_q$ which is a $\overline{\mathcal{F}}^+$ (sub)martingale (from the "standard textbook" proof).  Since $A$ has probability 1 it follows that $\overline{X}$ is a version of $X$, is almost surely cadlag and is a $\mathcal{F}^+$ (sub)martingale.  Thanks!
Comment: This is part of the Doob regularization theorem.  The germ of the modification to the filtration lurks here in the almost sure modifiers on the right continuity and existence of limits assertions.  In most presentations that I've seen one goes a step further to get an everywhere cadlag process by defining it to be zero where the limits don't exist; this requires the addition of a null set to the filtration.  I now see that Revuz and Yor only ask for cadlag paths almost surely so it appears addition of null set is not required.  I'll check details and that we get a version in this case.
Comment: Off the top of my head I am failing to see how to define $\overline{X}$ on the event where the limits don't exist while keeping it adapted to a general $\mathcal{F}$.  I don't have access to the book (and Google books conveniently elides exactly the pages where this is proved!); it'd be great if you could help me see how this is done. Otherwise off to the library...
Comment: @David Blair : I'll try to give the proof of the theorem later (I dont have the book with right now). But those authors are sometimes hard to understand (the translation from chinese is not always very good I believe). Although there is a reference at the page of the theorem it leads to no address in the reference part of the book so I don't know which paper of Föllmer is really the source of the theorem, as they give the proof this is not too serious problem but if their proof lacks rigour in some points I won't be able to check with the source. Best regards
Comment: @ David Blair : Your argument seems too work but I'll edit my answer to include the proof given by the authors. Best regards
Comment: This might be off topic, but why is everyone refering to $ \bar{X}$ as a modification of $X$? The value of X at some t might not be equal to its limit from any side. Hence $X\ne \bar{X}$ a.s for such $ t$ and thus is not a modification. If we assume that $\mathbb{E}X(t)$ is right-continous it is a modification tho imo. Or is there something that I am missing?


Let $A$ be a non-empty, proper subset of the reals $\mathbb{R}$. What "obvious" set relationship can be proven about $A$, $\bar{A}$, and/or $A^c$.

Question: I'm given a list of test-prep problems, which involve unstated theorems which we are to guess the statement of and then prove. I'm able to work out what most expect me to show, however the following is unclear.
Problem. Suppose that $A$ is a proper, non-empty subset of $\mathbb{R}$. What can be proven about $A$? Prove it. Hint: note that $A \subseteq \bar{A}$, and $A^c \subseteq \bar{A^c}$.
(Here, $A^c$ denotes the complement of $A$ with respect to the reals, and $\bar{A}$ denotes the closure of $A$ (that is, the union of $A$ and all its limit points))

Is there some obvious property of $A$, $\bar{A}$, or $A^c$ that can be proven here? My best guess is that I am to prove some set relationship (given the hints), however it is unclear to me what that might be (perhaps some subset relationship between $\bar{A}$ and $A^c$?).
I suspect that this is some "standard" problem, however I'm unable to turn anything up with simple Google/Stack Exchange searches.
Comment: Besides the fact that $~A \cup A^c~ = \Bbb{R},~$ you have a problem expressing $~\overline{A}~$ (i.e. the *closure* of $~A~$ in terms of either $~A~$ or $~A^c.~$  If $~A~$ is closed then $~\overline{A} = A.~$  However, in the stated problem, you are not given any information about whether $~A~$ or $~A^c~$ is closed.  For example, it could be that neither $~A~$ nor $~A^c~$ is closed.
Comment: What you can say is that $~A \subseteq \overline{A}.$  This implies that $~\left[ ~\overline{A} ~\right]^c \subseteq A^c.$
Comment: @user2661923 Sorry, could you clarify the meaning of the bracket notation ( I assume that $[\bar{A}]^c \not =  \bar{A}^c$)?
Comment: I am only using the bracket notation to represent order of operations.  You start with the set $~A.~$  Then you compute the *closure of* $~A.~$ Then you compute the complement of (the closure of $~A$).
Comment: There are (literally) infinitely many theorems that can be proven about $A$. For a trivial example, you can prove "$1 \in A\text{ or } 1\notin A$". Without further guidance, the question is unanswerable. And calling a necessary part of the question a "hint" beggars the meaning of that word. So presumably that further guidance should have come from the context of the problem that you haven't given here. If you could indicate the various things that were defined in the material being covered by the test, then we could probably figure out what they want, but as is, no. We can only guess.
Comment: This is like a proofs version of a "find the next number in the sequence" question.
Answer: Although the question is open-ended, I would argue that the following identity is the natural 'answer':
$$
\overline{\left(\overline{\left(\overline{\left(\overline{A}\right)^c}\right)^c}\right)^c}
=
\overline{\left(\overline{A}\right)^c}
$$
It is universal in the sense that not only is it true for all $A\subseteq \mathbb{R}$, but any other equality of sets (obtained through successive closure and complement of a subset $A\subseteq \mathbb{R}$) that is true for all $A\subseteq \mathbb{R}$, is a consequence of this identity (in a sense that we make formal below).
Formally, let $L$ denote the closure operation on subsets of the reals, and let $X$ denote the complement operation on subsets of the reals.  Then $X,L$ generate a monoid $M$, which is partially ordered by: $U\leq V$ if and only if $U(A)\subseteq V(A)$ for all $A\subseteq \mathbb{R}$.
It is easy to see that the following hold in $M$:\begin{eqnarray}&1)& X^2=1\\&2)&L^2=L\\&3)& L\geq1
\\&4)& \forall U,V,W \in M, \,\, U\leq V \implies UW\leq VW
\\&5)& \forall U,V \in M, \,\, U\leq V \implies LU\leq LV
\\&6)& \forall U,V \in M, \,\, U\leq V \implies XU\geq XV
\end{eqnarray}
Using just $(1)-(6)$ above, one can deduce the identity $$LXLXLXL=LXL\qquad (*)$$
Further $M$ is the monoid freely generated by $X,L$ subject to the relations $$*,\qquad L^2=L,\qquad X^2=1.$$
To see this, note that any element of $M$ is a word in $X,L$, and from the second and third relation above, we may choose that word to consist of alternating $X$'s and $L$'s.  From $(*)$ we may choose that word to not contain $LXLXLXL$.  This only leaves $14$ words.
Thus any identity between words in $X,L$ that is not implied by the three relations above, must equate two of these $14$ elements of $M$. However this is not possible, as we may find a single set $A\subseteq\mathbb{R}$ such that applying these $14$ elements of $M$ to $A$ results in fourteen distinct sets. Thus $$|M|=14.$$
Comment: I left the proof of $(*)$ and the construction of $A$ (such that $|MA|=14$) as exercises, as it looked like you wanted the statements to prove yourself.  If you want any help with the proof\construction just let me know.


How to prevent login if a condition is not met

Question: I am working on a Drupal 6 site (which I did not make and am not in charge of the general IT architecture at this company).
The site is on an intranet and they regularly import all users from an external source to Drupal users. The requirement is to restrict access to a subset of the imported users.
So:
When a user logs in if they are on the authorized list then proceed else deny access
I realize this isn't the most effective way of doing things and I would have done in differently, personally. But I am stuck having to do it this way.
What I have working so far:

I have feeds and data modules to import a CSV of authorized users
I am using hook_user() in my module to check if the user is on the authorized list

the problem:
I have all the above working but I can't figure out a clean way to log the user out or terminate the session when they are not found on the list. When I use this it causes errors:
function mymodule_user($op, &$edit, &$account, $category = NULL) {

  switch ($op) {
    case 'login':
        $strID = $account->uid;
        $strName = $account->name;
        if ($strID <> 1) {
            if (!mymodule_checkACL($strName)) {
                global $user;
                $user = array();
            }
        }
    break;
  }

Any better way than this? Sorry if the question seems a little basic, I'm new to this.
Thanks
Comment: I can't post a definitive answer on this as I have never actually had to do this, but there is a decent thread on drupal.org that talks about some of these issues: http://drupal.org/node/16217
Answer: You can use sess_destroy_uid($account->uid).
You might also consider blocking users during the import process; this is done by setting the "status" column to 0.


Como separar os dígitos de um número inteiro e somá - los entre si? - Python

Question: Como separar os dígitos de um número e somá-los, por exemplo: 123 -> 1 + 2 + 3 = 6
Como fazer isso, porém, fazer isso usando operadores aritméticos. Vi um caso em que foi separado usando operações com "//" e "%", mas não entendi muito bem como se faz.
Answer: Você pode fazer dessa forma inicia com uma variavel para a soma usando um laço para intera os digitos do numero informado no exemplo 123 a cada passo e feito a divisão retirando o resto da divisão do numero por 10 com isso se extrai o ultimo numero numero % 10 resto da divisão 3 depois dividi o numero por 10 com resultado 12 então reinicia o laço 12 % 10 resto da divisão 2 no caso já somando com o numero 3 e assim por diante.
soma = 0
numero = 123
while(numero > 0):
    soma += numero % 10
    numero = int(numero /10)
print(soma)

Comment: Obrigado, era exatamente oque eu tava procurando. Infelizmente não posso votar, não tenho 15 de reputação ainda.
Answer: Tente assim:
soma = 0
a = 123
b = str(a) #Transformo em str para poder usar posição. Ex: "b[1] = 2"
for i in range(len(b)):
        soma += int(b[i])
print(soma)

Comment: Olá Antony, acho que o AP não quer desta maneira. De quaquer forma, mostro-te uma maneira alternativa à tua: http://ideone.com/IR0jH5


Entity Embed module not working as expected

Question: I am wanting to use the Entity Embed module, and have installed this an it's dependent, Embed module, in Drupal 8.
There are no errors in the log, nor any javascript errors.
When I go to create the Entity Embed button, I get the follow UI feedback:

Is there something I need to do to allow entities to be embedable?
Installed Modules:
Here is a list of all modules I have installed.
Answer: You only have the Embed module enabled, but not Entity Embed itself.
Embed is just the underlying API/framework on which Entity Embed and Url Embed build.


SailsJS uniqueness scope multiple columns

Question: For single uniqueness check:
title: {
      type: 'string',
      required: true,
      unique: true
    }

dateTime: {
      type: 'string',
      required: true,
      unique: true
    }

Is there a way to limit model creation/update based on uniqueness spanning more than 1 column? Let's say now I need the table to be unique by using string + dateTime. Something like validates :title, uniqueness: { scope: :dateTime, :case_sensitive => false } in Rails.
Answer: You could use afterValidate to add your own validation spanning across more than one columns.
afterValidate: function (values, cb) {
    Foo.findOne({
        title: values.title,
        dateTime: values.dateTime
    }).exec(function(err, foo){
        if(err) return cb(err);
        if(foo) return cb('Not unique');
        cb(null, values);
    });
}

Comment: Not nearly :) I've been using sails for about a year now so I'm pretty familiar with a bunch of concepts. I got a bunch of help here so I was just giving back. Also the [issues](https://github.com/balderdashy/sails/issues) are in a really good shape if you want someone to have a look at a particularly nasty problem.
Comment: Wow, that's exactly what I did, but I thought there should be a better way. I guess I'll just settle for this. I just wished I didn't had to do so many unnecessary queries. You seem to have replied to quite a lot of my questions. Are you a contributor to sails?
Comment: I'd rather not raise issues if I could not contribute to the repo. Actually I know how this whole thing works. There can be hacks to making it work, but coming from Rails, I felt that many common problems are still not addressed in sails. But the last time I checked it had over 10k stars. Seems like one of the more popular nodejs frameworks out there.
Answer: I was looking for the answer to this problem when I found this trick could work.
For this Model
title: {
  type: 'string',
  required: true,
}
dateTime: {
  type: 'string',
  required: true,
}

We can use
Model.findOrCreate({title:"Your Title",dateTime:"DateTime"}).exec(console.log)

Here unique=true is removed from the two attributes. Here are the effects of using unique=true as originally asked in the question.
a) When not using unique=true
Data 1: title="Google", dateTime="2015-05-25T10:26:20.428Z" | will get accepeted
Data 2: title="Facebook", dateTime="2015-05-25T10:26:20.428Z" | will get accepeted
Data 3: title="Google", dateTime="2015-05-25T10:26:20.428Z" | will get rejected (because same as 1)

b) When using unique=true
Data 1: title="Google", dateTime="2015-05-25T10:26:20.428Z" | will get accepeted
Data 2: title="Facebook", dateTime="2015-05-25T10:26:20.428Z" | will get rejected (because dateTime is same as 1)
Data 3: title="Facebook", dateTime="2016-06-26T10:26:20.428Z" | will get rejected (because title is same as 2)


Interpreting an interaction effect with a logged term

Question: I am researching what effect transport infrastructure quality has on political stability. I would like to add an interactive term  - population density. I wish to investigate the effect that improving transport infrastructure by 1 point has on political stability in countries with differing population densities.
I computed an interactive variable by multiplying population per square mile with quality of transport infrastructure. I included this term in my regression, along with the the original population density variable and the quality of transport variable and obtained the following output.

My question is, how do I interpret the resulting model?
The population density variable has been log transformed and this is what is causing me a headache.
(I know that the interactive term is insignificant - but I must include an interpretation regardless for my university paper)
Comment: The interpretation of the main effects is complicated by the cross product term so, in your case in which there is no evidence of an interaction, I would suggest leaving the cross product term out of the model. Although many people refer to a cross product term as an interaction, it contains main effects and interactions. Only when the main effects are partialled out (as they are in computing the regression coefficient) is the term an interaction. With the cross product term in the model, what looks like a main effect is actually a simple effect at the point at which the other variable is 0.
Comment: You have an interpretation for the $\mathit{lnpopdens}$ variable correct? The interaction term would pickup how that effect varies linearly with transport infrastructure quality. It's a second derivative.
Comment: Matthew, thank you for your reply. Sorry to say though that it has gone completely over my head! If you are able to start from the beginning with your explanation it would be most appreciated, though I understand if you don't have the time. I am very new to all of this and I don't know what a second derivative is. I just wish to know how to interpret the effect that population density has on the relationship between infrastructure quality and political stability,
Comment: Have you done calculus? Eg. if we have $f(x) = a + bx$ then $\frac{df}{dx} = b$, the derivative of $f$ with respect to $x$ is $b$; the slope of the line is $b$?
Comment: I have not studied calculus no - thank you for what you have written below but once again that is way beyond me. I have purely been taught how to enter variables into spss and how to interpret the very basic outputs. I have not however been taught how to interpret them when variables have been log transformed (as they were skewed). I really appreciate you taking the time to help though :)
Comment: Hehe, my last try! With linear regression, you're estimating the [slope of a line](https://en.wikipedia.org/wiki/Slope). The interaction term shows you how the slope of the line changes as another variable changes.
Answer: Linear regression as a conditional expectation function
One interpretation of what you're doing with linear regression is that you're estimating a linear conditional expectation function. You're approximating the conditional expectation of $y$ given $x$ with a line.
Let's say you run the regression:
$$ y_i = a + b x_i + \epsilon_i$$
Because of the assumption $\operatorname{E}[\epsilon \mid X] = 0$, the expectation of $y$ given $x$ is then:
$$ \operatorname{E}\left[y \mid x \right] = a + b x$$
You're approximating the conditional expectation of $y$ given $x$ with a line. Your effect $b$ is the slope of the line. Let's call $f(x) = a  + bx$. The slope of your line is $\frac{df}{dx} = b$.
(Note that estimating a conditional expectation function can be useful for forecasting with minimal assumptions, but if you want to say $b$ is a causal effect, this requires more assumptions on the data generating process.)
With interaction terms:
Let's say you add another variable $z$, add an interaction term $xz$, and run the regression:
$$ y_i = b_0 + b_1 x_i + b_2 z_i + b_3 x_i z_i + \epsilon _i $$
Your conditional expectation of $y$ given $x$ and $z$ would be given by:
$$ f(x, z) = \operatorname{E}[y \mid x, z] = b_0 + b_1 x_i + b_2 z_i + b_3 x_i z_i$$
What is the effect of $x$? the derivative with respect to $x$?
$$ \frac{\partial f}{\partial x} = b_1 + b_3 z_i $$
The effect of $x$ varies linearly with $z$. If you take the 2nd derivative:
$$\frac{\partial ^2f}{\partial x\,\partial z} = b_3$$


The set $W^{⊥⊥}$ in a Hermitian space

Question:
Problem Statement: Let $W$ be a subspace of a Hermitian space $V$. Prove that $W^{⊥⊥}=W$

I am trying to figure out a good strategy for this proof.
I know that:

$W$ is a subspace of $V$
$W^{⊥}=\{\mathbf{v}\in V\ \ |\ \ \langle \mathbf{v}, \mathbf{w}\rangle=0\ \ \  \forall\mathbf{w}\in W\}$
$W^{⊥⊥}=\{\mathbf{v}'\in V\ \ |\ \ \langle \mathbf{v'}, \mathbf{w'}\rangle=0\ \ \  \forall\mathbf{w}'\in W^{⊥}\}$

It makes sense to me that the set of all vectors perpendicular to the vectors in $W$ are in $W^{⊥}$ and that the set of all vectors perpendicular to those in $W^{⊥}$ would indeed be $W$. Because we had to consider all vectors in $W$ to get $W^{⊥}$, so the only vectors perpendicular to $W^{⊥}$ would be $W$. I feel like I am just going in circles... any tips on how to approach this proof?
Edit: I also have the fact that $W$ being a $G$-invariant subspace implies that $W^{⊥}$ is $G$-invariant, and that $V=W\oplus W^{⊥}$. Are these facts useful for this proof? Like maybe show that $V=W^{⊥⊥}\oplus W^{⊥}$, which would imply that $W^{⊥⊥}=W$?
Answer: $W^{⊥⊥}$ contains $W$ is trivial by definition.
Equality follows from dimensionality since taking perp gives a new dimension of $n$ minus the old one.
This doesn't work in infinite dimensions but that's good because it's not true.
Comment: Ah, yes. I see how showing $W^{⊥⊥}$ containing $W$ is trivial now. I think I was mostly confused about how to show it doesn't contain any elements in $V\setminus W$. So we know $$dim(W)+dim(W^{⊥})=dim(V)$$. Then we have $$dim(W^{⊥⊥})+dim(W^{⊥})=[dim(V)-dim(W^{⊥})]+dim(W^{⊥})=dim(V)$$ thus, $$dim(W^{⊥⊥})+dim(W^{⊥})=dim(V)$$? Is this good enough to claim that $W^{⊥⊥}=W$ since they have the same dimension, and since $W$ is a subspace contained in  $W^{⊥⊥}=W$?
Comment: if a subspace contains another and they have the same dimension, they are equal. Proof: a basis for the smaller one must also be a basis for the bigger one.
Comment: Yes, because a subspace of a vector space that has the same dimension of the vector space will span the entire space. Awesome!
Comment: Is it enough to just claim that $W^{\perp\perp}\oplus W^{\perp} =V$ by the given theorem ($W\oplus W^{\perp} =V$) which would give the result of $\dim {W^{\perp\perp}}=\dim{W}$
Comment: no, you can have different subspaces adding to the same thing. this is easy to see even in R^2
Comment: Well I am confused how I would get $$dim W^{\perp\perp}=dim W - dim W^{\perp}$$ without that fact.

The set $W^{⊥⊥}$ in a Hermitian space


Initializing from an array of property pointers

Question: I'm trying to write a shortcut for initializing my custom UIView's properties, rather than write out each one line by line, but unfortunately it's not working the way I expected.
    // .h file
    @property (nonatomic, strong) UIView *view1;
    @property (nonatomic, strong) UIView *view2;
    @property (nonatomic, strong) UIView *view3;
    @property (nonatomic, strong) UIView *view4;

    // .m file
    UIView *views[] = { self.view1, self.view2, self.view3, self.view4 };
    for ( int i = 0; i < sizeof(views) / sizeof(views[0]); i++ ) {
        views[i] = [[UIView alloc] initWithFrame:self.frame];
    }

    NSLog( @"%@", self.view1 ); // prints null

Is this just not allowed with C-style arrays?
Answer: Your initialization of views[] looks fine, but as soon as you do
views[i] = ...

you are merely overwriting the contents of the array. You are, however, not initializing self.view1, self.view2, etc. Try
NSLog(@"%@", views[0]);

to see what I mean.
I would not do it like follows, but you could probably do (untested):
UIView **views[] = { &self.view1, &self.view2, &self.view3, &self.view4 };
for (int i = 0; i < (sizeof(views) / sizeof(views[0]); ++i)
{
    *views[i] = [[UIView alloc] initWithFrame: self.frame];
}

NSLog(@"%@", self.view1);

I would rather initialize them one by one, without the array. I don't see any advantage in using an array and a loop.
Comment: I agree with Rudy. Alternatively, if they're all so similar, perhaps you could just have `@property (nonatomic, strong) NSMutableArray *arrayOfSimilarViews;` Then just iterate from `0` to `n` and add an extra view to the array each time.
Comment: @AaronBrager: agreed.
Comment: This approach seems fragile and depends on how the property is implemented. A solution that actually sets the property would be better.
Comment: Sure, a solution that actually sets the property would certainly be better. I said it was untested, and that I would not do it like this, but if it must be done in a loop, this seems to answer the question.
Answer: You are just changing the pointer inside the array, the self.view1 pointer will still point to nil.
You could have an array of views as property:
@property(nonatomic,strong) UIView **views;

Or maybe you can use Key Value Coding like
for(int i=1;i<5;i++){
    NSString* key = [NSString stringWithFormat:@"view%i",i];
    UIView *view = //initialize view
    [self setValue:view forKey:key];
}

Answer: Calling e.g. self.view1 at the beginning will give you nil before you have initialized it. So the line
UIView *views[] = { self.view1, self.view2, self.view3, self.view4 };

is effectively creating an array of 4 nils.
Inserting the new instances of UIView to the array afterwards has no effect on the properties. In fact, a property is only a pair of selectors (e.g. view1 and setView1:) and by itself cannot be used reliably with C-style pointers.
You could use the backing ivars with an array of UIView ** pointers, but you're most likely better off rethinking the whole approach.


How valid is the use of bootstrapping in my case?

Question: I am currently working on the analysis of measurement data and have the problem of obvious non-normal data. Since most methods I want to use require normality (especially statistical process control and process capability) I first tried to transform my data using a power transform, however this did not work.
The data I have consists of means of ~1500 samples each. Of these means I have around 500-700 samples for multiple days and different measurements. Here is an example of how such a measurement might be distributed. Depending on the day and the type of measurements, the different distributions differ greatly.

Obviously this is only a sample and does not fully represent the underlying process. That's why I wanted to attempt to use bootstrapping to estimate the true population parameters of the underlying process which could then be used to define statistical limits. However I am not completely sure how valid such an approach is for my usecase. I know that due to the central limit theorem the means of my bootstrapped groups are going to follow a normal distribution which I was able to observe.

I am not a statistician and relatively new to everything in the field of data science and statistics so I definitely lack some of the fundamentals and intuition needed to evaluate such things. When looking at the bootstrapped distribution, the mean is consistent with the total mean of all samples, but the distribution just does not seem wide enough. In my samples I have many values which are below 0.45 and many more which are above 0.47 but to my understanding, according to the bootstrapped distribution such values would be highly unlikely. I am more than sure that there is something I do not understand correctly about bootstrapping and the relation to population parameters so I hope someone is able to explain the error in my train of thought.
Comment: By bootstrapping your sample (which is composed of means) and then taking the average of these means, and repeating this n times, you will get the bootstrap distribution of the mean of means, which is not what you want. You want to get the distribution of your population (from you sample).
Comment: Your bootstrapped results shown in the second chart are for the mean.  It is no surprise at all that the bootstrapped means have almost exactly the same mean as the original sample (that is inevitably their expectation given the original sample from which the bootstrap is taken), or that their distribution is closer to a normal distribution than the original sample (related to the central limit theorem) or that their standard deviation is about $\frac{1}{\sqrt n}$ times the original sample standard deviation.
Comment: @user2974951 Sadly I don't have access to the raw data, only the means. But I would also be more than happy to have the distribution parameter for the means. Does that mean however that without the raw data, even an estimation of the means is not possible?
Answer: Bootstrap is used for estimating the uncertainty of some estimator. In your case, you seem to have calculated the mean of means and used bootstrap to find the distribution of the mean of means. This is not the distribution of the means, you already approximated this distribution by using the histogram. This is why the uncertainty does not range over the whole range of the means. Recall that if the standard deviation of the means is $\sigma^2$, then the standard error would be $\sqrt n$ times smaller and this is what you observed.
Bootstrap is not a method for estimating the true distribution or true mean of the data. If you want to estimate the raw data given the sample means $m_i$, you additionally need to know the sample sizes for each sample, in such a case, the mean of the raw data can be obtained using weighted mean of means, where the sample sizes $n_i$ are used as weights
$$
\tilde m = \frac{\sum_i m_i n_i}{\sum_i n_i}
$$
if the sample sizes are the same, $n_1 = n_2 = \dots$, this is the same as using the arithmetic average of the means. If the sample sizes are not the same, the larger samples would have less impact on the final estimate than they need to, leading to a biased estimate.
See also a similar thread that we had recently: Can any quantity be bootstrapped?
Comment: It doesn't make sense to discuss this problem until the context for the problem is clearly stated.  What is the purpose of caring about what distribution the raw data have?    Are you using a parametric method on the data?  Which one?  If you seek data transformations how do you incorportate the proper amount of transformation uncertainty in the final analysis?  Why not use a nonparametric or semiparametric method?  Start with the nonparametric CDF estimate (empirical cumulative distribution function estimator).  Why do your data consist of means?  Analyze the raw data, usually.
Comment: Thank you for the great answer! It definitely seems like I misunderstood the usage of bootstrapping to find population parameters.


What's the fastest way to evaluate a custom function over a range of integers?

Question: What is my problem?
In my project, the end-user is providing me with the set of custom functions with one integer parameter written in Python.
I can enforce on user to use decorators.
The functions can call themselves and each other, for example:
QUX = 100
QUUX = 250

def foo(x):
    if x == 0:
        return QUX
    return foo(x - 1) - bar(x)

def bar(x):
    if x == 0:
        return 0
    return QUUX - baz(x)

def baz(x):
    if x == 0:
        return 0
    return foo(x - 1) * 0.01

The job of the program - that I'm supposed to write - is to:

calculate the results of the functions (foo(), far(), baz()) on range 0 to 1000
repeat the calculation with slightly changed constants (QUX, QUUX) many, many times (say 200_000).

So the job of the program is to provide the user with the output of that sort:
import pandas as pd
import random

for _ in range(200_000):
    QUX = random.randint(100, 500)
    QUUX = random.randint(150, 750)

    result = pd.DataFrame({
        "foo": [foo(x) for x in range(1_000)],
        "bar": [bar(x) for x in range(1_000)],
        "baz": [baz(x) for x in range(1_000)]
    })

My question:
What is the fastest way to evaluate these custom functions?
What did I try?

Caching

I can enforce on user to use a decorator so I can use caching.
from functools import lru_cache

@lru_cache()
def foo(x):
   ...

@lru_cache()
def bar(x):
   ...

@lru_cache()
def baz(x):
   ...

for _ in range(200_000):
    ...

    foo.cache_clear()
    bar.cache_clear()
    baz.cache_clear()

This one is a must. (~45 seconds on 10_000 iterations (instead of 200_000 iterations))

Map

Using map() inside of list comprehension:
    result = pd.DataFrame({
        "foo": [*map(foo, range(1_000))],
        "bar": [*map(bar, range(1_000))],
        "baz": [*map(baz, range(1_000))],
    })

Minor runtime improvement. (~42 seconds)

Numpy vectorize

Vectorizing with np.vectorize():
r = np.arange(1000)

vec_foo = np.vectorize(foo)
vec_bar = np.vectorize(bar)
vec_baz = np.vectorize(baz)

for _ in range(10_000):
    QUX = random.randint(100, 500)
    QUUX = random.randint(150, 750)

    result = pd.DataFrame({
        "foo": vec_foo(r),
        "bar": vec_bar(r),
        "baz": vec_baz(r),
    })

Minor improvement (~41 seconds)

Pybind11 (C++)

I've tried to write the code in C++ and expose it using Pybind11
cpp
array<double, 1000> calculate_cpp(function<double(int)> func) {
    array<double, 1000> result;
    for (int t=0; t<1000; t++) {
        result[t] = func(t);
    }
    return result;
}

PYBIND11_MODULE(cmake_example, m) {
    m.def("calculate_cpp", &calculate_cpp);
}

python
for _ in range(10_000):
    QUX = random.randint(100, 500)
    QUUX = random.randint(150, 750)

    result = pd.DataFrame({
        "foo": calculate_cpp(foo),
        "bar": calculate_cpp(bar),
        "baz": calculate_cpp(baz)
    })

No improvement at all.
I still want to try out cython and numba, but given very poor effect of Pybind11 (C++), I'm losing the hope of any solution that would bring me at least a little bit closer to C/C++ speed.
What do I expect?
I expect that given that the problem is fairly specified (function always with integer parameter, always applied over a range) an approach exist that is much closer to C/++ speed than Python's speed.
Comment: numba may help.
Comment: You main issue here is recursion, each function call other ones in a circular fashion, this is what takes most time. I'm not sure whether you can combine that and numba, it looks like [caching on recursive functions is currently not supported](https://github.com/numba/numba/issues/4062)
Comment: You have to pay attention to redefine the function for each step of the loop, or to include the constants as parameters. Else, you will run the same functions over and over with the initial constants ([example](https://tio.run/##TYwxCsMwDAB3vUKjPbbZCoa@oHO2EhyZGGLJKDI0@bzrqfS2u@HqaZvw1HtSKZgaRxPZD8ylihru2t5xiRsBzBjwBvD8JVeWz5EvCi9h8rBSwuT8A3CgZE0ZZ6ia2dzofhzC/c97/wI))
Comment: For each iteration in the 10k, are you concatenating the result or overwriting the result?
Comment: @JonSG, I will be either summing or concatenating, but that's not relevant for the question asked... or is it?
Comment: I believe it is. If I take your code with just the cache decorator and each outer iteration I append the results of the inner as a tuple, the whole thing runs in 8 seconds on my crappy laptop.
Comment: @mozway, oh... I see... I'll edit the snippet and I'll add `.cache_clear()`, thanks.
Comment: `@lru_cache()` has a default size of 128.  Given that you are iterating over 1000 distinct values, you may want to be specific on the size (or just let is grow as large as needed with `@lru_cache(None)`).  Your example functions only go back by one but actual user functions may not be so kind.
Answer: I tried to replicate what you are getting with the exception that I will work with lists of lists of tuples as the accumulator rather than concatenating DataFrames as my intuition tells me that might be a pinch point.
Here is what I tried:
import functools
import random
import time

start = time.time()
results = []
for _ in range(10_000):
    QUX = random.randint(100, 500)
    QUUX = random.randint(150, 750)

    @functools.cache
    def foo(x):
        return QUX if x == 0 else foo(x - 1) - bar(x)

    @functools.cache
    def bar(x):
        return 0 if x == 0 else QUUX - baz(x)

    @functools.cache
    def baz(x):
        return 0 if x == 0 else foo(x - 1) * 0.01

    results.append([(foo(x), bar(x), baz(x)) for x in range(1_000)])

print(f"{round(time.time() - start, 4)}s for {len(results)} iterations")

That gives me results like:
8.7771s for 10000 iterations

and if you are seeing results in the 40s range for 10k iteration, then this might be a path forward.
Comment: Looks like pandas dataframe initialization is indeed time-consuming, thanks.
Answer: You might want to save the results of previous (QUX,QUUX) combinations (in a dictionary for example). With the numbers in your example, there will be roughly 136K distinct pairs out of the 200K samples. Reusing the result (whole dictionary of lists) of already seen pairs should save 30% of the execution time.
for example:
import pandas as pd
import random

seen = dict()
for _ in range(200_000):
    QUX = random.randint(100, 500)
    QUUX = random.randint(150, 750)

    if (QUX,QUUX) in seen:
        data = seen[QUX,QUUX]
    else:
        data = { "foo": [foo(x) for x in range(1_000)],
                 "bar": [bar(x) for x in range(1_000)],
                 "baz": [baz(x) for x in range(1_000)]
                }
        seen[QUX,QUUX] = data
    result = pd.DataFrame(data)

Comment: If you pass those parameters to the functions and use a cache this should do the same seamlessly ;)
Comment: @mozway, true but only if you cache everything (with distinct entries for QUX,QUUX values) and don't clear caches between runs.  Also, reusing whole lists, and thus not going through the comprehension/function calls, should make this a bit faster as well.


Could someone explain this results section from a study? (very elementary question)

Question: Sorry for the beginner level question. Could someone explain what the below means?

On average, more than 4 weights per week were entered into the CTM program. A significant difference in mean within subject weight loss was found between the control group ($n = 65$; $0.5 ± 4.4$ kilograms (kg)) and the CTM intervention group ($n = 70$; $2.6 ± 5.9$ kg) over the first year ($p = 0.019$). Nonparametric tests (Mann-Whitney $U$) revealed very similar results ($p = 0.02$).

I think that this section ($n = 65$; $0.5 ± 4.4$ kilograms (kg)) means:
*there were 65 people in the group
*the average weight lost was .5 kg
*4.4 is the standard deviation of the weights within that group?
Is that correct?
(It's the section entitled "3.2. Year 1 Results" in this paper: https://www.hindawi.com/journals/jobe/2015/763680/)
thanks for any info!
Comment: Dunno, I had no problem copy.pasting the missing parts (but I nevertheless added LaTeX-dollars around them)
Answer: I know little about statistics, but from what I can gather, the Mann-Whitley U test is similar (and in some cases, more robust) than the Student's T test.  You can read more about the Mann-Whitley test here: Mann-Whitley U Test


Not able to render/retain graph on button click HighChart

Question: I am trying to plot a graph on click of a button. In my code below when I click on button 'click me' I am able to see graph displaying on my screen but it quickly disappers from screen. I thing the graph is not being rendered. I am not sure where am I going wrong. I need the graph to persist on my screen. I shall be populating the values for data and categories dynamically once I get this issue solved.
I need to call jquery function from javascript function only due to structure of my exisiting code.
Can anyone please let me know why the graph is not just persisted or retained on screen.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

  <title>
  </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  <form name="contact" action="" method="post">
      <button id="control" onclick="js_fun()">Click me</button>
  </form>

Answer: It only persists for a short time because you have placed your button within a form here:
<form name="contact" action="" method="post">
    <button id="control" onclick="js_fun()">Click me</button>
</form>

When clicking the button the form is submitted. Since action="" it submits the form to the same page, and you once again have to call js_fun() to render the graph. The graph is only shown the short time between clicking the button and the new page being loaded.
You could make your button code like this:
    <button id="control" onclick="js_fun(); return false">Click me</button>

Or just remove the form tag if you're not actually planing on submitting a form at any time.
Answer: If you does not give any type for the button tag element, it will point as submit. when you click the button, the form is submitted first after that only JavaScript calls.You did not mention any action for the form tag, so form is submitted to the same page.So button JavaScript click event does not call because the page is refreshed each button click.
You have 3 ways to resolved this type issues
1.If you does not post any form values then remove the form tag, just put only the button tag.
2.include the return false in the button on-click event.
     <button id="control" onclick="js_fun(); return false">Click me</button>

3.Include button type="button" in the button tag element.
      <button id="control" type="button" onclick="js_fun();">Click me</button>


How to find deceleration of ball when deceleration causes speed to decrease which causes deceleration to decrease etc.

Question: For my IB extended essay, I am modelling the trajectory of a tennis ball, however I later realised that the deceleration of the ball is not constant. Even if I found the initial speed and its initial deceleration, moments later that deceleration will cause the balls speed to decrease and thus causing the drag force decrease as well in a never ending loop until the ball has finally reached rest. I was wondering if there was a way I could solve this problem without making too many assumptions (I heard that maybe I should look into multi-variable calculus?).
Comment: Multivariable calculus is the wrong extension of calculus for this kind of problem. You want (ordinary) differential equations.
Comment: do you know the formula in newtonian physics for distance under constant acceleration/deceleration ?
Comment: Is the trajectory also influenced by gravity? I don't think there's a convenient closed-form solution in that case. You might have to look up numerical solution of differential equations.
Answer: If you want to model acceleration as a function of velocity, then your problem is
$$
\frac {dv} {dt}  = f(v)
$$
Which is an ordinary differential equation and can be rearranged using seperation of variables to
$$
\int \frac{dv} {f(v)} = \int dt
$$
which you should be able to solve using 'normal calculus' provided you can integrate the LHS.
If you don't want to make too many assumptions about $f$ you could use asymptomatic analysis to, for example, find the low speed behaviour. You could do this by finding an expression that is asymptotically equal to $f$ as $v \rightarrow 0$ which can be though of as a low speed approximation to $f$, and then solving the problem for your approximation to $f$.
Answer: If you want to model acceleration as a function of velocity, then your problem is
$$
\frac {dv} {dt}  = f(v)
$$
Which can be rearranged using seperation of variables to
$$
\int \frac{dv} {f(v)} = \int dt
$$
which you should be able to solve using 'normal calculus'.


how to cover the entire screen with a UIView using swift programatically

Question: I'm trying to put a UIview() to cover the full screen so when i press a button i can put a dark mode theme on it. it works ok the only problem is that it does not cover the nav bar which is something i want to do i have looked up a few things  i found this code it works on the tutorial i have seen but does not work now think swift have updated the language making  that solution old
    if let window = UIApplication.shared.keyWindow {
        let blackView = UIView()
        blackView.backgroundColor = .black
        view.addSubview(blackView)
               blackView.frame = window.frame

    }

Answer: Swift 4.X
You can add extension in your project.
extension UIView {

    func addToWindow()  {
        let window = UIApplication.shared.keyWindow!
        self.frame = window.bounds
        window.addSubview(self)
    }
}

Use:
bgView = UIView(frame: UIScreen.main.bounds)
bgView.backgroundColor = UIColor(white: 0, alpha: 0.4)
bgView.addToWindow()

Comment: Xcode says "'keyWindow' was deprecated in iOS 13.0. Should not be used for applications that support multiple scenes as it returns a key window across all connected scenes"
Comment: @ChuckZHB Solution already available https://stackoverflow.com/questions/57134259/how-to-resolve-keywindow-was-deprecated-in-ios-13-0
Comment: Thanks for it. After trying some code way, didn't work for me. Then In IB, I change holder view's top and bottom anchor to constraint on its superview(view) instead of safe area, that lets holder view fill the whole screen.


having a backup of 'sites' folder and database backup is all I need?

Question: I'd to be sure that having a backup of 'sites' folder and database backup is all I need to restore a site?
Answer: So long as you know which version of core you have and it is un-patched then yes.
If you are backing up sites, make sure you back up any other databases. You may have multiple sites under there.
One good way to test is to do a restore on another server and check things work, after all unless you know your restore works a backup is worthless.
I have found Drush make scripts a handy way to make sure that the core and modules that you use are correct. That way you only need to back up the make script, the site directory itself sites/yoursite.com and the database.
Comment: thanks, I know nothing about Drush, I'll go into drush as soon as I can.


Cant kill session even after restarting browser

Question: I am having an issue right now with sessions. I use a log in script to do initial authentication, and then i authenticate at the begining of each protected page, and finally i try to log out (code below).
The files exist on my server using PHP 5.6. I am using Win10 and Chrome on my pc.
SYMPTOMS:

Although session vars are destroyed, i can still use the 'back' button in my browser to view a page that does authentication. When i dump $_SESSION vars on that page (which i browsed back to using the back button of the browser) all $_SESSION var are non existent - but the page still loads.
COOKIES are still there. I have set cookie lifetime in my php ini to 1 (one second) for testing... they are still there after i supposedly delete them. Even when set to 0, they are still there after i restart the browser.
After reading symptom 1 above, i think many of you will guess, correctly, that the session is still alive and well - even after i close the browser, restart it and type the url of one of the protected pages directly in the address bar i am still able to view the page, even though the $_SESSION var that checks for authentication does not exist.

Would really appreciate advice.
LOG IN SCRIPT
//this page is called (using require_once) by the page
    //that captures username and password
    session_start();

    //requirements
    require_once "../php/path.php";       //sets the server search path
    require_once "constants.php";         //does all the DEFINE stuff
    require_once HTML_HEADER;             //loads HTML code - doc type, head etc
    require_once DATABASE;                //does the dB connecting

    //collect the POST
    $uName = $_POST[uName];
    $uPsswd = $_POST[uPsswd];

    //build & execute sql query
    **SQL to retreive uName and password here if match then...**
        $_SESSION['approved'] = 'true';
        require_once MAIN_CONTENTS;  //main page after authentic log in
        exit;

AUTHENTICATE CODE CALLED USING require_once BY EACH PROTECTED PAGE
    if (session_status() == PHP_SESSION_NONE) {
    session_start();
  }//end if

  //what's the time Mr Wolf?!!!
  $now = time();

  //although session exists, are we logged in and approved? If not kill session & eixt.
  if (isset($_SESSION['approved']) && $_SESSION['approved'] != 'true'){
    require_once "killSession.php";
    require_once "notAuthorised.php";
    exit;
  }//end if

  if (!isset($_SESSION['approved'])){
    require_once "killSession.php";
    require_once "notAuthorised.php";
    exit;
  }

  //if session exists, how old is it? If older than 'discard_after' then kill session.
  if (isset($_SESSION['discard_after']) && $now > $_SESSION['discard_after']) {
    require_once "killSession.php";
    require_once "notAuthorised.php";
    exit;

  }//end if
  else {
    //logged in and approved so set timeout for 15 mins
    $_SESSION['discard_after'] = $now + 30;
  }//end else

THE killSession FILE
//check to make sure session exists - start if not
  if (session_status() == PHP_SESSION_NONE) {
    session_start();
    $_SESSION['approved']='false';
  }//end if

  //following code caused 'headers already sent...'
  //if (isset($_COOKIE[session_name()])) {
    //$params = session_get_cookie_params();
    //setcookie(session_name(),'',time() - 172800, '/', $params['domain'], $params['secure'], isset($params['httponly']));
  //}

session_unset();
session_destroy();
session_write_close();
setcookie(session_name(),'',0,'/');
session_regenerate_id(true);
?>

Answer: Your issue is most probably caused by the POST data still being stored in your browser. By clicking the back button, your user is authenticated again and a session is created.
To resolve this issue you can use the POST-Redirect-GET method.
<?php

// Has the session been set?
if (!isset($_SESSION)) {
    session_start();
}

// If a form is submitted, add the POSt data to a session
// and redirect
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $_SESSION['login_data'] = $_POST;
    unset($_POST);
    header("Location: ".$_SERVER['PHP_SELF']);
    exit;
}

// Check if user wants to login, authenticate and unset login data
if (isset($_SESSION['login_data'])) {
    // Authenticate
    unset($_SESSION['login_data']);
}
?>

<form action="" method="POST" role="form" action="<?= $_SERVER['PHP_SELF']; ?>">

    Username
    <input type="text" class="form-control" id="username" placeholder="Username"

    Password
    <input type="password" class="form-control" id="password" placeholder="Password">

    <button type="submit" class="btn btn-primary">Authenticate</button>
</form>

Comment: Thank you so much for your rapid response and attempt to resolve this issues. Although your code didn't resolve my issue, your comment about BROWSER CACHE was SPOT ON! So for now i have used  and 'header()' to disable page caching (although still some odd behaviour in Chrome... a separate issue)!


Trabalhar com session é seguro?

Question: Exemplo, se eu criar uma sessão
session_start();
$_SESSION['nome'] = $nome; #valor pegado anteriormente.

O usuário final pode ver e(ou) editar os dados contidos nela?
Comment: Sua pergunta é respondida aqui: http://pt.stackoverflow.com/questions/38920/o-que-guardar-em-uma-sess%C3%A3o-de-login/38970#38970
Answer: Falando sobre a API nativa do PHP, os dados da sessão ficam em um arquivo do lado do servidor, que geralmente fica em uma pasta fora, public_html ou www, e isto torna o acesso para o usuário final impossível.
No entanto, quem desenvolve o sistema pode sim acabar expondo os dados de alguma maneira, então podemos dizer que depende da maneira que foi programado.
Expondo os dados da pasta
Às vezes configuramos as pasta de sessão em outro local, que é acessível publicamente:
session_save_path('/etc/www/sessions');

Você também pode acidentalmente expor os dados de $_SESSION, mas é provável que os dados expostos sejam do próprio usuário, no entanto, é possível compartilhar a sessão, e isto pode ser uma dor de cabeça.
Concluindo, sessões no PHP não são "inseguras", a maneira que você programa é que pode lhe expor.
Comment: Entendi! Obrigado, Guilherme :)
Answer: As variáveis de sessão no PHP são cookies server-side, ou seja, são cookies salvos no servidor.
Usuários comuns sem acesso administrativo ao servidor não tem acesso aos arquivos, desde que estejam num diretório privado e, por padrão, o PHP salva os cookies num diretório privado.
Contudo, é preciso ter cuidado onde os cookies são salvos no servidor pois o PHP permite configurar o local dos cookies até mesmo em tempo de execução:
Se o programador for um jumento, definirá os cookies numa pasta de acesso público:
session_save_path('/var/www/website.foo/public/sessions');

Com isso, deixa tudo vulnerável.
Mas essa é uma situação muito difícil de acontecer, mesmo para "jumentos".
Uma precaução mais importante é, quando salvar dados sensíveis em variáveis de sessão, como login e senha, sempre codifique os dados pois pode existir um administrador malicioso com acesso ao servidor ou mesmo um hacker pode ter acesso físico aos arquivos.
Sequestro de sessões
As variáveis de sessão podem ser sequestradas. Isso é chamado de session hijacking: https://en.wikipedia.org/wiki/Session_hijacking
Como é possível sequestrar o session_id do PHP?
Não é muito difícil. Basta ter acesso ao computador de uma pessoa que esteja com a sessão aberta.
Apesar dos dados das variáveis de sessão serem gravados no servidor, as variáveis de sessão também precisam de um cookie client-side. Nesse cookie contém o session_id, ou, o ID da sessão. Esse ID é o que identifica qual é o cookie no servidor.
Aí está a jogada! Se vc tem o ID, consegue obter os dados do servidor referente ao mesmo.
Então basta transportar o cookie client-side que contém o ID de sessão para outro computador. Com isso, estará, por exemplo, logado no seu computador como se fosse outra pessoa.
Como evitar o sequestro?
Uma técnica básica para evitar o sequestro de sessões é gerar um novo ID de sessão periodicamente. Recomenda-se gerar um novo ID a cada 5 minutos.
Alguns acham exagero e definem como 1 hora ou mais. Porém, uma hora é mais que o suficiente para roubar um session id. Por isso, defina um período de tempo mais curto.
No PHP existe a função session_regenerate_id(), que pode ser usada para gerar novos IDs de sessão. http://php.net/session_regenerate_id
Dedo duro
No PHP, há uma diretiva de configuração do ambiente chamada session.use-trans-sid. http://php.net/manual/en/session.configuration.php#ini.session.use-trans-sid
Essa diretiva, quando ativada, transporta o ID da sessão de forma transparente em requisições $_GET e $_POST de forma automática.
Recomenda-se que desative essa opção pois apenas facilita que um hacker obtenha o session id de alguém, mesmo sem ter acesso físico ao computador da vítima.
Por quê o PHP possui essa configuração "nociva"?
O motivo é que é uma configuração de tempos obscuros, dos primórdios da internet onde tínhamos muitos problemas para lidar com cookies nos browsers da época entre 1996 ~ 2003. Atualmente não há necessidade em se preocupar com isso.
Session name
Por definição padrão, o nome do parâmetro que contém o ID de sessão no cookie client-side é PHPSESSID.
É recomendável que modifique para outro nome que não dê pistas sobre qual tecnologia utiliza no servidor. Isso dificulta o trabalho de um hacker em obter informações de um servidor no qual deseja atacar ou explorar vulnerabilidades.
No PHP, temos a função session_name(), onde é possível definir um nome customizado.
Defina como ASPSESSIONID, por exemplo. Com isso, um hacker pensará que o site utiliza ASP e ficará perdendo tempo procurando vulnerabilidades do ASP ao invés do PHP.
Um hacker mais experiente pode perceber isso verificando outros dados no cabeçalho de requisções ao servidor. Por exemplo, o Apache envia dados que podem expor que o servidor utiliza Apache e PHP. Portanto, procure também como omitir tais dados.
Para servidor Apache, na diretiva  <Directory> do <Virtualhost>, adicione a linha php_value expose_php Off.
Dependendo das configurações do servidor, isso só é permitido alterar no php.ini.
Há diversos outros detalhes que podem expor informações do servidor. Caso queira saber mais, pesquise sobre o assunto.
Algumas palavras-chave para pesquisar:
hide apache server info
php expose off
hide php info

Comment: +1 pelos detalhes adicionais sobre sequestro. Mas o PHP não usa exatamente "cookies no servidor", que eu saiba. Pode usar sim arquivos em disco (não sei como ele serializa), BD ou mesmo a memória RAM. Depende de como o PHP está configurado.
Comment: O termo "cookie server side" é um termo popular. Obviamente que não são cookies como num browser client-side. Mas podemos definir/apelidar os arquivos de variáveis de sessão do php como cookies também. Por isso o termo "cookie server side".
Comment: Eu nunca tinha ouvido isso, pelo visto tô por fora :P
Comment: é um termo bem antigo.. pensei que todo programador php soubesse.

Trabalhar com session é seguro?


ibatis domain modelling

Question: I am working on the domain model for a project. I have a class named user that has a class named UserType as one of the properties. I know when I want to select all users, I will use joins to pick up all corresponding usertypes. How do I do inserts? Do I have to write a handler for userType? Or can I do something like
INSERT INTO users(... usertype_id ...) VALUES(... #{usertype.usertype_id}...)

Please help;
I have spent the whole day trying to figure this out. I am using ibatis 3.0 and I am new to ibatis.
Answer: Ibatis is not a full ORM framework, so it doesnt know about objects relationships.
So yes, you must write something like your INSERT if you want to work directly with domain objects that do not fully correspond to a record in your table; that is, if the User object you are mapping in Ibatis does not have a getUsertypeId() method (that returns the value corresponding to the table column usertype_id  ) but instead a getUserType() method.
(Of course you can also write a  getUsertypeId() method which internally calls getUserType().getId()... but just stop here and don't pretend to make also a setUserTypeId(int id) which internally tries to load the UsertypeId from the Db, etc, etc... that calls for trouble. You'll end reinventing JPA/Hibernate.)
I don't think a TypeHandler is the right approach, that feature is more oriented to converting non-trivial types, not so much for handling relationships.
Another valid approach is to have a layer of relatively low-level-dumb POJOs, roughly one for each table, with properties that map directly to your table columns (say, a UserDb object with a userTypeId property and no getUserType() method, and no bussiness intelligence, dependencies on upper layers or persistence knowledge), and then, on top of that, a layer of richer "real" domain objects, each of which wraps a (usually small) graph of those "dumb" POJOs, and has the intelligence necessary for calling the persistence layer (eg DAO) to load/save the graph (perhaps lazily).
One advantage of this approach is that the core of actual ibatis mappings (the SQL coding) can be done fairly automatically with Ibator - it even creates the code of the POJOs from the DB schema.
For massive reads of data which involves many tables (reports), you can create other trviial ad-hoc POJOs (which corresponds directly to the columns of your SELECT, and perhaps have some rudimentary intelligence to display values - something like a 'ViewModel') ... or even HashMaps.
PS: You might want to read about DDD (and concepts like "Entities", "Value objects", "Views", "contexts" "rich domain object" vs "anemic domain objects" eg). Ibatis gives you much flexibility to learn and implement along this lines.
Comment: some links for people passing thru: http://en.wikipedia.org/wiki/Domain-driven_design DDD and http://code.google.com/p/mybatis/wiki/Generator for ibator replacement.

ibatis domain modelling