Como passar argumentos por referência em Python?

Question: Quando eu estudei Pascal lembro que meu professor explicou que para passar um argumento por referência era necessário colocar var na frente da varável, nos parâmetros da função/procedimento.
Exemplo:
function exemplo(var, a: real):real;

Em Python o que é necessário fazer para isso acontecer?
A ideia é ter uma variável definida no inicio do programa, alterar ela dentro da função, e mudar o valor dela fora da função sem usar return.
Comment: Se a ideia é alterar o valor de uma variável externa, use o `return` e atribua novamente à variável. Explícito é melhor que implícito no Python.
Comment: É que para chegar na função que altera esse valor precisa passar por outras 3 ou 4. Aí teria que fazer retiro em todas estas?
Comment: Possivelmente. Se quiser, pode entrar no chat que podemos discutir o código.
Comment: Alguma das respostas resolveu sua dúvida? Acha que pode aceitar uma delas? Veja o [tour] como fazer isso, se ainda não o fez. Você ajudaria a comunidade identificando qual foi a melhor solução para você. Pode aceitar apenas uma delas. Mas pode votar em qualquer pergunta ou resposta que achar útil no site todo (quando tiver pontuação suficiente).
Answer: Python funciona igual a todas as linguagens, passa tudo por valor. O que existe de diferente é que alguns valores são acessados de forma direta e outros através de uma referência, ou seja tem objetos que já são colocados na estrutura interna do Python e tem objetos que são referenciados por um valor na estrutura interna do Python.
Se esta estrutura interna é acessada por uma referência é um detalhe de implementação e não faz diferença para você programador.
Então certos valores que tem semântica por valor não podem ser passados diretamente por referência já que a linguagem não provê sintaxe para isso. Ela oferece tipos mais complexos que são sempre por referência. Os objetos de forma geral são assim, as listas e tuplas também, então encapsule em um objeto mais complexo e está resolvido. Veja:
def value(x):
    x = 1
def ref(x):
    x[0] = 1
x = 0
value(x)
print(x)
x = [0]
ref(x)
print(x[0])

Veja funcionando no ideone. E no repl.it. Também coloquei no GitHub para referência futura.
Comment: É que a ideia era usar uma variável de fora da função dentro, alterar ela na função, e mudar o valor sem usar return.
Comment: Que foi o que eu te demonstrei.
Comment: So adicionando um comentário, nem todas a linguagens passam parametros por valores, por exemplo em PHP objetos são passado por referencias mas tipos primitivos não. Se não me engano Java também é eassim
Answer: Caso você queira alterar variáveis globais com objetos imutáveis dentro do escopo de uma função, você deve utilizar a palavra global para referenciar o objeto global. Exemplo:
def exemplo_imutavel():
    global x
    x = 20

x = 10
exemplo()

print(x)  # 20

Em caso de variáveis com objetos mutáveis (listas, set, dicionários, etc.) a manipulação pode ser feita através de parâmetros de maneira simples e direta sem o uso da palavra reservada global. Exemplo:
def exemplo_mutavel(x):
    x[0] = 10

x = [1, 2, 3]
exemplo(x)

print(x)  # [10, 2, 3]

Comment: Achco que não é bem isso.
Comment: É que tenho uma variável que iniciei com o programa. Em certo ponto, dentro de uma função, eu tento alterar ela mas ela só muda dentro dessa função, e não lá fora dela. Eu gostaria que mudasse como se fosse global.
Answer: O pessoal de python não está entendendo a pergunta. Passar por referência em Harbour por exemplo é:
function funcaoX(x)
    x = x + 10
    return nil

y = 5
funcaoX(y)   // Passando sem referência
print(y)     // Retorna 5

funcaoX(@y)  // Passando por referência
print(y)     // Retorna 15

Python é uma liguagem script, por isso não é possível fazer isso, mas deve haver no Python uma maneira de passar por referência quando não se sabe o nome da variável dentro da função e precisa que a variável passada por referência reflita a alteração da variável dentro da função. Lembrando que array (lista), tupla, dicionário e objetos já são por natureza passados por referência na maioria das linguagem.
Comment: Se desconhece a linguagem ou não domina o assunto evite responder com suposições baseado em outras linguagens pois isso só gera confusão. Em Python o modelo de passagem de argumentos chama-se [passagem por atribuição](https://docs.python.org/pt-br/3/faq/programming.html#how-do-i-write-a-function-with-output-parameters-call-by-reference) que é modelo hibrido onde tipos primitivos são passados por valor e tipos abstratos são passados por referência, não havendo sintaxe ou notação especial para gerir esse controle. O que implica que esse seu exemplo não é cabível para a linguagem em questão.
Comment: Outra coisa, o Stack Overflow [não é um fórum](https://pt.meta.stackoverflow.com/q/2068/137387), nós somos um [site de perguntas e resposta](https://pt.meta.stackoverflow.com/q/7256/137387) e sua publicação além de não responder a pergunta feita na página e ainda lança uma dúvida *...Deve haver no Python uma maneira de passar por referência...*. Use esse espaço apenas para responder a pergunta da página. Para mais informações veja [answer] e [help].
Comment: Harbour também é uma linguagem de script. Ser de script ou não nada tem a ver com o fato de ter uma sintaxe ou implicitamente passar por referência.


My function somehow undo an object property change

Question: To calculate the winner of Tic Tac Toe game (I followed the react's tutorial), I created/modified the calculateWinner function. This function accepts an array of objects, whereas each of these objects has 2 properties:

value (which can be set to 'X' or 'O', null by default); and
isWinner (boolean, false by default).

What this function does is to check if a certain combination of 3 squares (distinguished by their index in the array) has the same value, and change these squares' isWinner value to true.
The problem is, on certain winning combinations, one or more isWinner property is somehow still set to false when you inspect the whole array itself. To make the problem weirder, this only occurs (so far) on codepen, but not here in StackOverflow's snippet. (I'll try locally soon.) Also erroneous on local.

doTheTest();

function calculateWinner(squares) {
  const lines = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];

  let winner = null;

  for (let i = 0; i < lines.length; i++) {
    const [a, b, c] = lines[i];

    let squareA = squares[a];
    let squareB = squares[b];
    let squareC = squares[c];

    if (squareA.value && squareA.value === squareB.value && squareA.value === squareC.value) {
      squareA.isWinner = true;
      squareB.isWinner = true;
      squareC.isWinner = true;

      winner = squareA.value;

      console.log('squares to check:', a, b, c);

      // this returns three trues as expected
      console.log('The three winning values just after setting them:', squares[a].isWinner, squares[b].isWinner, squares[c].isWinner);

      // but if you'll check the three winning square objects in the array,
      // one or more isWinner property is still set to false
      // and for some reason, this only happens in certain winning combinations
      // e.g. it works just fine with [2, 4, 6] but produces this issue with [0, 4, 8]
      console.log('But the squares array itself still just after setting them', squares);
    } else {
      squareA.isWinner = false;
      squareB.isWinner = false;
      squareC.isWinner = false;
    }
  }

  return {
    winner: winner,
    squares: squares
  };
}

function doTheTest() {
 const squaresJSONString = '[{"index":0,"value":"X","coordinates":"1, 1","isWinner":false},{"index":1,"value":"O","coordinates":"2, 1","isWinner":false},{"index":2,"value":"X","coordinates":"3, 1","isWinner":false},{"index":3,"value":"O","coordinates":"1, 2","isWinner":false},{"index":4,"value":"X","coordinates":"2, 2","isWinner":false},{"index":5,"value":"O","coordinates":"3, 2","isWinner":false},{"index":6,"value":null,"coordinates":"1, 3","isWinner":false},{"index":7,"value":null,"coordinates":"2, 3","isWinner":false},{"index":8,"value":"X","coordinates":"3, 3","isWinner":false}]';

 /* The JSON string above in more readable format:
[
 {"value":"X","coordinates":"1, 1","isWinner":false},
 {"value":"O","coordinates":"2, 1","isWinner":false},
 {"value":"X","coordinates":"3, 1","isWinner":false},
 {"value":"O","coordinates":"1, 2","isWinner":false},
 {"value":"X","coordinates":"2, 2","isWinner":false},
 {"value":"O","coordinates":"3, 2","isWinner":false},
 {"value":null,"coordinates":"1, 3","isWinner":false},
 {"value":null,"coordinates":"2, 3","isWinner":false},
 {"value":"X","coordinates":"3, 3","isWinner":false}
]
 */

 const squaresParsed = JSON.parse(squaresJSONString);

 calculateWinner(squaresParsed);
}

Comment: I see the same result on CodePen and here. They both have `isWinner: true` for indexes 0, 4, and 8.
Comment: You have the same squares in multiple elements of `lines`. So if a square is a winner for one set, but a loser for another set, its final `isWinner` value will be from the last set that included it.
Comment: You should break out of the loop as soon as you find a winning line.
Comment: That is so weird. I just checked mine again, still not working. Check this [screenshot](http://imgur.com/a/nOGfT), do you have any suggestion?
Comment: I didn't break out of the loop immediately because I have to undo any other winners that are previously set.
Comment: I just understood what you said on the second comment. I'll reconstruct my code so that it prevents that issue. Thanks a lot!
Comment: I certainly overlooked that. How can I vote you up or something for this? @Barmar
Answer: You need to break out of the loop when you find a winning line. Otherwise, you'll keep testing other lines, and since they don't match it will set isWinner = false; for those squares, which undoes the assignment when the test succeeded. So put a break statement at the end of the if block that finds a winning line.
Or if you want to be able to find all winning lines, just leave out the else block that sets isWinner = false. That should be the initial state of all the objects, you don't need to do it every time through the loop. When the loop is done, all the squares that are part of winning lines will have isWinner: true.


Amazon EC2/ELB and AD FS Proxy Not Working in HTTPS Mode

Question: We run a AWS ELB for two AD FS Proxy servers (also hosted in AWS).  For some reason, when I set it up as HTTPS mode it doesn't work.  When I set it up as TCP mode, it works with no issues, however it can't determine downtimes.  Whenever I set the link to check adfs in HTTPS mode, it reports instances unavailable.  I can hit each server by putting the IP and address into my Hosts file.  For some reason the https simply does not work.  Any ideas on how to make HTTPS mode work?
Comment: Did you verify the health checks being received on the AD FS servers? Are you forwarding or offloading SSL at the ELB? If you are using HTTPS checking, how are you able to use the IP and not the domain name?
Answer: The issue is that when the ELB is connecting to the host using HTTPS it isn't connecting to a particular hostname but instead is using the IP. The AD FS Proxy is only responding to connections to that hostname.
For the ELB to monitor the health of the AD FS Proxies you need to install IIS on the AD FS Proxy servers.

Install IIS
Configure with an SSL Certificate (Self signed is fine here as ELB's don't check the SSL certificate validity)
Configure the Default website to listen on port 443 with the SSL certificate you configured.

You should now find that the HTTPS health check works correctly.
This also applies to AD FS Servers behind an ELB.


Doubt in Example realted to dual of $L^{\infty}$ is strictly bigger than $L^1$

Question: I was reading Brezis I stuck at following Example

I do not know how contradication occur. First we take functional of compact supported space then we extend than function to whole $L^{\infty}$ this I understand
But I do not understand why to take f(0)=0 and proceed .
Please can any one give me idea about proof
Any Help will be appreciated
Answer: Of course when it is assumed that $\displaystyle\int uf=\left<\phi,f\right>=f(0)$ and $f(0)=0$, then $\displaystyle\int uf=0$ for all such $f$.
The statement that $\left<\phi,f\right>=0$ for all $f\in L^{\infty}$ entails that $\left<\phi,f\right>=f(0)=0$ for $f\in C_{c}$ by the extension, this is a contradiction since we can always find $f\in C_{c}$ such that $f(0)\ne 0$, say, a triangle splines with $f(0)=1$.


How to apply a point cut on constructor that has around advice?

Question: Hello everyone i am new to aspectj. In my program i need to apply apply a pointcut on cunstructor invocation i.e. new Myclass(); .
I have a point cut like:
package pak;
//import java.io.PrintWriter;
import java.io.*;

public aspect HookYepmeAspect {

    pointcut constructorPkt(int paramInt, String paramString1):
        call(public  MyClass.new(int , String ))  && args( paramInt,  paramString1);

    void around(int paramInt, String paramString1):constructorPkt(paramInt,  paramString1) && within(com.yepme.*)
    {
        proceed(paramInt,  paramString1);
    }
}

but it gives me worning that :
applying to join point that doesn't return void: constructor-call(void com.yepme.Contact.<init>(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)) HookYepmeAspect.aj  /TmpHookAspectProject/src/pak   line 63 Java Problem

i also tried int as a return type of advice.So please can anyone tell me what i am making mistake. what should i do in this case .
Answer: The error says that you are intercepting a function that doesn't return void.
Around advice should not return void, but capture the output from the call and return that.
public Object around(int paramInt, String paramString1):constructorPkt(paramInt,  paramString1) && within(com.yepme.*)
    {
        return proceed(paramInt,  paramString1);
    }

Use Object so that it will work for all the object types that the matching adviced functions(constructors) might return.


Is this function infinitely differentiable?

Question:
Let
  $$f(x)=\begin{cases}\exp\left(1-\frac{1}{1-|x|}\right),\,\,&|x|<1\\0,&\text{otherwise}.\end{cases}$$
  and show that $f$ is continuously differentiable.

I took the derivative and obtained
$$f'(x)=\begin{cases}\frac{\text{sgn}(x)}{(1-|x|)^2}\exp\left(1-\frac{1}{1-|x|}\right),\,\,&|x|<1\\0,&\text{otherwise}.\end{cases}$$
which does not look to be continous at $x=0$ and thus is not continuously differentiable. Did I do something wrong?
Answer: From what you've written here, I can't see anything wrong, except a misssing negative sign in front of everything. The only thing I can think of is that you might have copied it wrong. If not, then the problem itself is wrong (presumably there is a typo somewhere).


Logistic regression interaction only significant when main effects are removed

Question: I'm interested in understanding if the interaction of a categorical variable (group A or B) and a continuous one (X1) can predict reaction time.
I started by testing the main effects plus the interaction using fitglm in Matlab.
modelspec = 'RT ~ group*X1';          % test interaction and individual factors

% Logistic regression model
mdl1 = fitglm(data,modelspec,'Distribution','binomial','CategoricalVars',[1])

With this model the interaction term is not significant, although when I plot the data it seem like there is an interaction. Model output:
Estimated Coefficients:
                   Estimate       SE        tStat      pValue
                   ________    ________    _______    _________
    (Intercept)    -0.80295     0.15055    -5.3336    9.631e-08
    group           0.31        0.23212     1.3355    0.18171
    X1              0.1202      0.056139    2.1412    0.032261
    group:X1        0.13312     0.085264    1.5613    0.11846

1369 observations, 1365 error degrees of freedom
Dispersion: 1
Chi^2-statistic vs. constant model: 58.7, p-value = 1.12e-12

Next I looked at the interaction only and the interaction is significant:
modelspec = 'RT ~ group:X1';          % test interaction only

% Logistic regression model
mdl2 = fitglm(data,modelspec,'Distribution','binomial','CategoricalVars',[1])

Estimated Coefficients:
                   Estimate       SE       tStat       pValue
                   ________    ________    ______    __________
    (Intercept)    -0.52461    0.071806    -7.306    2.7524e-13
    group:X1        0.26365    0.036835    7.1576    8.2125e-13

1369 observations, 1367 error degrees of freedom
Dispersion: 1
Chi^2-statistic vs. constant model: 54.1, p-value = 1.94e-13

I'm a bit lost and don't really know how to interpret these results, particularly from the first model (mdl1 = main effects and interactions). As I said, my main interest is the interaction. Should I report the model with the interaction only? What would be the correct way forward?
Any input is welcome!
Answer: Interactions in nonlinear models can get very tricky since these models can allow much more heterogeneity/flexibility in response. I always try to leave my linear intuition behind at home when I go out into the wild, nonlinear world, and do some math. So here we go.
In a logit with two main effects and an interaction,
$$Pr[y = 1 \vert x,z] = \frac{\exp (\alpha + \beta x + \gamma z + \delta z \cdot x)}{1+\exp (\alpha + \beta  x + \gamma z + \delta z \cdot x)}=p. $$
After some tedious calculus and simplification, the partial of that with respect to $x$ becomes
$$ \frac{\partial Pr[y=1 \vert x,z]}{\partial x} = (\beta + \delta \cdot z) \cdot p \cdot (1-p).$$
This tells you the change in probability associated with a small increase in $x$. The term $p \cdot (1-p)$ is just a product of two probabilities, so it's a random number in $[0,0.25]$, so we can focus on the first term.
Note that the whole marginal effect (ME) depends on $z$ (as well as $x$), and that even the sign could be ambiguous, and that this can vary from observation to observation depending on covariates. Now there are two ways to proceed from here.
To determine if $z$ modifies the effect of $x$ you can calculate the MEs for a range of plausible values of $z$, use the delta-method to get variance-covariance matrix for those MEs, and then construct a hypothesis test that the MEs are all equal. If you reject that null, you can say that the data is inconsistent with "no interaction".
You could also try to differentiate the ME with respect to $z$ and use delta-method to calculate the SE of that. The cross-partial derivative is
$$ \frac{\partial Pr[y=1 \vert x,z]}{\partial x \partial z} = \delta \cdot p \cdot (1-p) +(\beta + \delta \cdot z) \cdot \left[ p \cdot (1-p)^2 - p^2 \cdot (1-p) \right].$$
This is even harder to untangle, but corresponds to a simpler counterfactual of bumping everyone's observed $z$ by an epsilon rather than resetting to a entirely different value. This hypothesis would be easier to test since it's just one number and you don't have to pick counterfactual values of $z$ (how many, which values, etc).
Your $x$ is binary, so it might make sense to replace differentiation with finite differences in the first step.
Also, all these derivatives can vary across observations, so it often makes sense to average them and do the hypothesis test on those AMEs.
All this is just a mathy, long-winded way to say that it is not sufficient to consider the statistical significance of the coefficient on the interaction.
For contrast, the linear case is much more straightforward. The ME of $x$ is $\beta + \delta \cdot z$, and the cross-partial of that is easy to calculate. It's just $\delta$, and it tells you everything you need to know about how the effect of $x$ depends on $z$ (sign, economic and statistical significance). But there's no heterogeneity whatsoever, which is the price you pay for the simplicity.
I suppose you can also try to interpret the index function coefficients as effects on the log odds (rather than on the probabilities), since that is still linear, but I find those harder to explain, both to myself and my colleagues, so I abstain from that.
Comment: I must confess I don't understand the whole post but I think I got most of it. Thank you.
Comment: @Oiko Please feel free to ask any clarifying questions you have.
Answer: Most of the time you should not include the interaction without the main effects. Check out here why. I also found this comment from @Thomas Levine under the same question that might help you:

If the interactions are only significant when the main effects are not in the model, it may be that the main effects are significant and the interactions not. Consider one highly significant main effect with variance on the order of 100 and another insignificant main effect for which all values are approximately one with very low variance. Their interaction is not significant, but the interaction effect will appear to be significant if the main effects are removed from the model
Comment: Thank you, from this I understand that as a rule of thumb, removing main effects is incorrect.


Find the radius of the circle??

Question:
$|AP|=12cm,$ $|BP|=4cm,$ $|OP|=11cm.$ Find the radius. Help me.
enter image description here
Answer: We know that $$|AP|\cdot |PB|=|DP|\cdot |PC|$$
but letting the radius be $r$,
$$|DP|=r+|OP|=r+11$$ and $$|PC|=|OC|-|OP|=r-11$$
thus
$$12\times4=(r+11)(r-11)=r^2-121$$
or $$r^2=169$$ and $$r=13$$

Find the radius of the circle??


Receiving a Struct and Writing to file in socket programming

Question: How to receive a struct over tcp socket and output to a file? I have a struct, which is a packet and I'm trying to send this over socket and receive it on the client side and write to the file. I'm using a void pointer as the buffer to store the file data
So far I have
struct packet {
    int id;
    void* data;};

I'm able to access the id, the int on the client side but when i write the data out to file. the file seems to be corrupted.
UPDATE:
The Server has a file open and will read from the file and make it into a packet and send the packet to the client.
Client will also have a file open for write, as it receives the packets it will write it out to the file.
Server Side:
current_packet = 0;
While(1)
{
struct packet *current = (packet *)malloc(packet_size+sizeof(int));
current->id = current_packet;
int read = fread(&current->data, 1, packet_size, file);
//end of file
if(read == 0){
break;
}
int byte_write = write(conn, current,read + sizeof(int));
current_packet++;
}

Client Side
current_packet = 0;
While(1)
{
struct packet *current = (packet *)malloc(packet_size+sizeof(int));
int read = read(conn, current, packet_size + sizeof(int));
//end of file
if(read == 0){
break;
}
int byte_write = fwrite(&current->data, 1, read - sizeof(int), file);
fflush(file);
}

Comment: How do you manage to know the size of `data` when you receive it at the client site?
Comment: the void pointer data will be assigned  a size in bytes. So the total size of the struct will be size of the data + the size of the int. the client and the server both know the data size. When i write to the client i write the bytes read from file, maximum can be the the set size of data. On the client side i receive the struct then when i fwrite to file, I write the number of bytes read - the size of the int. which will be the size of the data
Comment: So you mean `id` actually contains the size of the file? Are you considering network byte order transmission correctly? You code sample is too narrow to give some concise diagnosis for your problem. Provide a [MCVE](http://stackoverflow.com/help/mcve) please.
Comment: id is the sequence number of the packet. The client will open a file to write and receive packets in order and write it out to the file. the void* contains the data. The Server does fread on the file an reads it to the `void * data`. maximum read can be the packet size.
Comment: And `packet_size` is fixed? `void*` needs to point at some allocated memory.
Comment: yes. maximum fread will read is `packet_size` unless its reaching end of the file it will be less than `packet_size`
Comment: And the `void* data;` pointer is set to something like `char buf[packet_size];`? Man, post a minimal code sample as I already mentioned please!
Comment: no i dont set it to anything i just malloc a `struct packet` and fread the file to the `void * data`. I've updated the question with more code
Comment: A `struct packet` doesn't provide any buffer that should be connected to `void* data;` You're writing to unallocated memory, and I have no clue what you're actually doing at the sender side. The only clue I have is: You're doing it wrong.
Answer:
struct packet *current = (packet *)malloc(packet_size+sizeof(int));

Supposed packet_size is something like
 const size_t packet_size = 2048;

The code cited above might allocate something big enough to contain a packet, but the data pointer isn't initialized, and also not considered correctly in the allocation.
What I would do in your case:
Have a fixed size buffer allocated per packet like this:
struct packet {
    const size_t packet_size = 2048;
    int id;
    std::array<unsigned char,packet_size> data;
};


Invertibility in Reinsch form Derivation (Smoothing Splines)

Question: In Element of Statistical Learning II, in the context of smoothong splines, $\pmb{S_{\lambda}}$ is defined as
$$
\pmb{S_{\lambda}} = \pmb{N}(\pmb{N}^T\pmb{N} + \lambda \pmb{\Omega_N})^{-1}\pmb{N}^T
$$
$\pmb{N}$ is a $N$x$N$ square matrix defined as $\pmb{N}_{i,j} = N_j(x_i)$ where the $N_j$ are defined as
$$\left\{\begin{array}{ll}
        N_1(X) = 1\\
        N_2(X) = X \\
        N_{j+2}(X) = d_j(X)-d_{N-1}(X)
    \end{array}
\right.
$$
where the knots $\xi_{j}$ are the $x_j$ and
$$
d_j(X)=\frac{(X-\xi_{j})_+^3-(X-\xi_{N})_+^3}{\xi_{N}-\xi_{j}}
$$
Assuming that $\pmb{N}$ is invertible the Reinsch form can be obtained as
\begin{align}
  \begin{aligned}
\pmb{S_{\lambda}}
& =  \pmb{N}(\pmb{N}^T\pmb{N} + \lambda \pmb{\Omega_N})^{-1}\pmb{N}^T   \\
& =  \pmb{N}(\pmb{N}^T\pmb{N} + \lambda\pmb{N}^T\pmb{N}^{-T}\pmb{\Omega_N} \pmb{N}^{-1}\pmb{N})^{-1}\pmb{N}^T \\
& =  \pmb{N}[\pmb{N}^T(\pmb{I} + \lambda \pmb{N}^{-T} \pmb{\Omega_N} \pmb{N}^{-1})\pmb{N}]^{-1}\pmb{N}^T \\
& =  \pmb{N}\pmb{N}^{-1}(\pmb{I} + \lambda \pmb{N}^{-T} \pmb{\Omega_N} \pmb{N}^{-1})^{-1}\pmb{N}^{-T}\pmb{N}^T \\
& =  (\pmb{I} + \lambda \pmb{K})^{-1}
 \end{aligned}
\end{align}
where $\pmb{K} =  \pmb{N}^{-T} \pmb{\Omega_N} \pmb{N}^{-1}$.
My question is:
How to show that $\pmb{N}$ is invertible?
Answer: I was hoping to find a more intuitive proof, but the matrix can be shown to be invertible by directly showing that its determinant is nonzero.
First note that the knots are located at the $K$ unique values of $\mathbf{x}$. Let $\boldsymbol{\xi}$ represent these knots, in ascending order.
Also, $d_j(\xi_i) = 0$ if $i \le j$, and so $N_{i,j} = 0$ if $i > j + 1$ and so $\mathbf{N}$ is "almost lower diagonal". In fact, it's a lower diagonal matrix with a column of 1's appended to the front of it.
We can take advantage of this structure to obtain a simple expression for the determinant. First we transform $\mathbf{N}$ by subtracting the first row from all others. Call this matrix $\mathbf{M}$. (These row operations do not affect the determinant, so $\text{det}(\mathbf{M}) = \text{det}(\mathbf{N})$.)
We have:
$$
\begin{aligned}
\mathbf{M} &=
\begin{pmatrix}
1 & \xi_1 & 0 & 0 &  \cdots & 0 \\
0 & \xi_2 - \xi_1 & 0 & 0 & \cdots & 0 \\
0 & \xi_3 - \xi_1 & N_3(\xi_3) & 0 & \cdots & 0 \\
0 & \xi_4 - \xi_1 & N_3(\xi_4) & N_4(\xi_4) & \cdots & 0 \\
\vdots & \vdots & \vdots & \vdots & \ddots & \vdots \\
0 & \xi_K - \xi_1 & N_3(\xi_K) & N_4(\xi_K) & \cdots & N_K(\xi_K)
\end{pmatrix} \\
\end{aligned}
$$
By expanding along the first column, we see that the determinant of $\mathbf{M}$ is equal to that of the sub-matrix formed by deleting the first column and the first row. Because this sub-matrix is lower diagonal, the determinant is the product of its diagonal entries:
$$
\text{det}(\mathbf{M}) = (\xi_2 - \xi_1) \prod_{i=3}^K N_i(\xi_i)
$$
We know that $\xi_2 - \xi_1 \ne 0$. Thus it suffices to show that $N_i(\xi_i) \ne 0$ for all $i \in \{3, \dots, K\}$, as follows:
\begin{aligned}
N_i(\xi_i) &= d_{i-2}(\xi_i) - d_{K-1}(\xi_i) \\
&= \frac{(\xi_i - \xi_{i-2})_+^3 - (\xi_i-\xi_{K})_+^3}{\xi_{K}-\xi_{i-2}}
-
\frac{(\xi_i-\xi_{K-1})_+^3 - (\xi_i-\xi_{K})_+^3}{\xi_{K}-\xi_{K-1}}
\\
&= \frac{(\xi_i - \xi_{i-2})_+^3}{\xi_{K}-\xi_{i-2}}
-
\frac{(\xi_i-\xi_{K-1})_+^3}{\xi_{K}-\xi_{K-1}} \\
\end{aligned}
If $i < K$, then we have
$$
N_i(x_i) = \frac{(\xi_i - \xi_{i-2})_+^3}{\xi_{K}-\xi_{i-2}} \ne 0
$$
by the fact that the $\xi_i$ are strictly increasing. If $i = K$, then we have
\begin{aligned}
N_K(x_K)
&= (\xi_K - \xi_{K-2})^2 - (\xi_K - \xi_{K-1})^2 \\
&> (\xi_K - \xi_{K-2})^2 - (\xi_K - \xi_{K-2})^2 = 0
\end{aligned}
So $N_K(x_K) \ne 0$ and the proof is complete.
Comment: Hello, I Just noticed that you answered this question and so wanted to thank you.
I also tried to compute the determinant to show that it is invertible but I couldn't succeed.
In your proof, you say that $N$ is "almost lower diagonal". However I don't see why.
Indeed you wrote that $N_{2,3}=0$ but it seems that $N_{2,3} = N_3(ξ_2) = d_1(ξ_2) = denom * (ξ_2−ξ_1)^3 > 0$.
Comment: Little update: you said that $d_j(ξ_i)=0$ if $i \leq j$, and so $N_{i,j}=0 $ if $ i>j+1$.
But it seems to me that then it is true for $i < j - 1$


Are the way that substitutions are utilised in soccer optimal?

Question: From empirical observation (I have no statistics), it seems fairly common that 3 substitutions are not often made in football (soccer) games and if they are the final one is usually late after the 85th minute.
But with top flight teams and national teams the depth of squad is often such that the quality of players means you won't necessarily have simply 11 top players and then some fillers, and even if you have some world class players you probably don't have more than 8 meaning there are potential substitution outgoers and incomers.
I think there might be a number of good cases made for the value of substitutions from experience. I would also like to know more about the actual statistics of substitutions used and their timing distributions. I recognise holding something back incase of injury is a valid counter argument but again, statistically, this might have actually very little value and I would welcome the same type of statistics.
Measured in terms of number of substitutions made and the timing of each substitution, and the general effect of each substitution, are coaches in general making optimal decisions?
Answer: Your question is somewhat subjective and the answers (or at least this one) might be found to be opinion based. But for me there are a few factors to consider:

You only get 3 substitutions
You want to utilize your staring line up
The reason behind substitutions

Using these points I'll argue that the value of substitutions are not under utilized the way they happen in most professional soccer games.
1. You only get 3 substitutions
If the coach makes 2 or 3 quick substitutions early in the game, they can end up regretting it later in the match. Coaches want to leave themselves with the leeway to make changes  based on their own team's or the opposing team's performance.
Imagine making all your substitutions in the first 60 minutes. If one of your players gets injured, gets a yellow or red card, or your team goes down a goal you can't bring in new players to make adjustments.
So the substitutions are made while keeping it in mind how much time there's left in the game. When you substitute late in the game, you minimize the odds of having to make another substitute for readjusting team strategy. Late substitutions can be linked to replacing tired players and other reasons we'll cover in the third point.
Similarly if coaches leave their substitutions all towards the last few minutes of the game they lose the opportunity to make in game adjustments throughout the game. So doing substitutions too early or too late under utilizes their values.
If teams had more than 3 substitutions, you'd then naturally see more substitutions. But also earlier ones, purely because you'd have more substitutions later on to adjust the game plan.
As argued, more substitutions has leads to earlier substitutions in 2020 post the COVID-19 shutdowns. FIFA allowed for up to 5 substitutions for these matches. These extra 2 line up changes a side have made the half time substitutions far more common.
2. You want to utilize your staring line up
Replacing players in the first half is very uncommon as they still have the energy to play for many more minutes. Most football players are used to playing full match so substituting them before half time is a waste of a substitution in most cases. Players also get a 15 minute break at half time so no need to make substitutions in first half to preserve player energy. Furthermore the coaching staff has days between matches to decide which players give the best chance to win based on their level of fitness, skills, injuries, bookings, and etc. So the starting line ups are perceived to give the maximum utility and reduce the need for substitutions.
The only times you see first half substitution is when players get injured, when the coach needs to desperately make an early adjustment because of coming to the game with the wrong tactics (assuming that the coach is willing to accept having made wrong tactics), or to punish players for not following team tactics. An example of the last point is Mario Balotelli substituted for making a ridiculous back heel shot in a friendly game.
So in general substitutions are all done in the second half to maximize the overall utility of all players.
3. The reason behind substitutions
The time of substitutions are dependent on the reason behind the substitutions.
Substitutions to replace injured players can happen at any minute of the game. On the other end of the spectrum you see the "last minute" substitutions when a team wants to kill time as they're happy with the outcome of the game and want to drag the outcome to the end of the game.
Another common type of substitution is at the half time. This usually signals that the coach is either not happy with the player performance or is in need of a change of tactics.
Lastly, you see substitutions between the 70th to 80th minutes where an older player comes in. One of the reasons behind substitutions are to bring in players with fresh legs. Older players tend to get tired faster and so substituting them earlier means they can't play at a high level till the end of the game. Or if they start the game they're likely to be substituted in the second half for the same reason.
There are also times that there is no need to do all 3 substitutions. For example if your team is up by 2 goals and you have control of the game, have made one substitution and there's 10 minutes left to go, then there's no need to do all 3 substitutions.
So in summary substitutions are made precisely to increase the utility of the players on pitch, hence substitutions are not under utilized.
Comment: @Attack68, please include this clarification comment into your question. But note that this sentence: "*Let me put it a different way, do you think the way that coaches, on average, use substitutions currently is optimal?*" brings a lot of subjectivity into the underlined question.
Comment: @Attack68: … unless you can come up with a precise, unambiguous, objectively measurable definition of optimality.
Comment: Thanks for the detailed response. I fear I may have phrased my question poorly though. Let me put it a different way, do you think the way that coaches, on average, use substitutions currently is optimal? From your point 1, I think you would also welcome statistics on the timing distribution of each substitution. Furthermore do you think you could predict it based on what you believe to be optimal? For example what is the percentage number of games in the UK-PL where both teams used 3 substitutions. btw I agree all subs should be done in 2nd half (pt 2), simple maths dictates that as you say
Comment: @jorgwmittag or perhaps that is part of the question. Arguably if I could do that I doubt I would ask the question. There might indeed be someone who has already studied this scientifically and can contribute, such as the author of the links I post below.
Comment: @Attack68 there is no "optimal" way. Coaches do that depending on the situation on the pitch and could still be wrong. You never know when you need to re-adjust your game (e.g. when the other coach changes theirs / late goals) or because of an injury. You just can't study that.. it's simply an important part of coaching and requires experience.
Comment: I disagree. 'Optimality' can be contextual and relative to a constructed model. It is possible to form a theory and propose a method of measuring the effectiveness, statistically across data. I don't care about a single game, but in general for example I think everyone would agree 3 subs on 45mins and no subs at all are both poor strategies and in mathematical terms that creates lower and upper bounds, with something better in between. I think those who have downvoted this question have not invested any thought beyond first impression, into a valid question that might actually create an edge.
Answer: After some research I stumbled onto a soccer metrics page: http://www.soccermetrics.net/team-performance/substitutions-and-their-impact-at-euro-2012 and http://www.soccermetrics.net/team-performance/sudden-impact-substitution-at-the-2010-africa-cup-of-nations.
The data both confirms and disproves some of my observational comments. In these games there was 1 unused substitution slot every two games, i.e. 1 in 2 games used all 6. Also since these were tournament games the end time was not necessarily always known so that may bias the stats for subs held out until extra time which never came.
The distribution seems fairly natural, but the author questions the optimal time for making substitutions as well, without giving any firm conclusions.
I think it would be interesting to compare this with more data on league games, which unfortunately I can't find.
I agree with all points made by @alamooot except I'm still unsure on the frequency of injury meaning I still question whether saving a sub in case of this is better than bringing on a fresh pair of legs.


output same form buttone on same page - drupal_render($form

Question: I try to duplicate form button on the same page. The button is printed like this
drupal_render($form['continue_shopping']);

The problem is that once I render the button, the second one is not rendered again later in DOM. How to overcome this? Thank you
Answer: From the top of my head, I would just duplicate the button in hook_form_alter() and render it as two separate buttons.
Some code examples here:
https://www.drupal.org/node/1415334
And here:
How to add extra save buttons


Does second countable implies countably many components?

Question: I was proving that a manifold has countably many connected components and this question came up. The way I did the original question is to show that components in manifolds are open and then use the fact that second countability implies separable to show that there's only countably many components. The latter argument follows from the fact that, if there's uncountably many components, then there must be some component "left out" but since that component is open, we must get a contradiction.
However, in general, components are not open (in fact, they are always closed). So in the general case, I do not know how to prove that separability implies having a countable number of components or if second countability would imply such (since second countability is a stronger condition). Are those implications true and if not, what are the counter-examples?
Comment: In general it does not: the Cantor set is second countable and is totally disconnected and so has $2^\omega$ components.
Comment: @bof Sorry for the confusion, I had no trouble showing components are open in manifolds or the fact that a manifold has countably many connected components. I'm mainly interested in the second part of my post, I'll edit for clarity
Comment: The product space $\{0,1\}^\mathbb R$ is not second countable but it is separable and totally disconnected so it has $2^{2^{\aleph_0}}$ components.
Comment: In a locally connected space the components are open. Hence a separable, locally connected space has countably many components. Of course, manifolds are locally connected.
Answer: Let $\mathbb{R}$ have the usual topology and let $X$ be the set of irrational numbers with the subspace topology inherited from $\mathbb{R}$.

Since $\mathbb{R}$ is second countable, so is $X$.

But the components of $X$ are the singleton subsets of $X$, so $X$ has uncountably many components.


QT Quick Designer Custom Components are Blank?

Question: I'm doing some testing of QT Quick to see if i can use it as a GUI replacement for the old Ui files. I noticed in some of the examples that custom components will populate the library view. I managed to do that (apparently they must be in a sub dir of the qml file that uses them?). However these components do not render in the Qt Quick design window. There is actually nothing to grab or manipulate. Upon running the program, they render correctly.
Does anyone have a solution? My source is below
import QtQuick 1.0
import Chips 1.0

Item {
    width: 100
    height: 62
    Chip
    {

    }
}

chip.cpp
#include "Chip.h"

#include <QtGui>

Chip::Chip(QDeclarativeItem *parent)
    : QDeclarativeItem(parent)
{
    x = 0;
    y = 0;
    color = QColor(0, 200, 0);
    setFlags(ItemIsSelectable | ItemIsMovable);
    setFlag(QGraphicsItem::ItemHasNoContents, false);
    setAcceptsHoverEvents(true);
}

//Chip::Chip(const QColor &color, int x, int y, QDeclarativeItem *parent)
//    : QDeclarativeItem(parent)
//{
//    this->x = x;
//    this->y = y;
//    this->color = color;
//    setZValue((x + y) % 2);

//    setFlags(ItemIsSelectable | ItemIsMovable);
//    setFlag(QGraphicsItem::ItemHasNoContents, false);
// setAcceptsHoverEvents(true);
//}

QRectF Chip::boundingRect() const
{
    return QRectF(0, 0, 110, 70);
}

QPainterPath Chip::shape() const
{
    QPainterPath path;
    path.addRect(14, 14, 82, 42);
    return path;
}

void Chip::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(widget);

    QColor fillColor = (option->state & QStyle::State_Selected) ? color.dark(150) : color;
    if (option->state & QStyle::State_MouseOver)
        fillColor = fillColor.light(125);

    const qreal lod = option->levelOfDetailFromTransform(painter->worldTransform());
    if (lod < 0.2) {
        if (lod < 0.125) {
            painter->fillRect(QRectF(0, 0, 110, 70), fillColor);
            return;
        }

        QBrush b = painter->brush();
        painter->setBrush(fillColor);
        painter->drawRect(13, 13, 97, 57);
        painter->setBrush(b);
        return;
    }

    QPen oldPen = painter->pen();
    QPen pen = oldPen;
    int width = 0;
    if (option->state & QStyle::State_Selected)
        width += 2;

    pen.setWidth(width);
    QBrush b = painter->brush();
    painter->setBrush(QBrush(fillColor.dark(option->state & QStyle::State_Sunken ? 120 : 100)));

    painter->drawRect(QRect(14, 14, 79, 39));
    painter->setBrush(b);

    if (lod >= 1) {
        painter->setPen(QPen(Qt::gray, 1));
        painter->drawLine(15, 54, 94, 54);
        painter->drawLine(94, 53, 94, 15);
        painter->setPen(QPen(Qt::black, 0));
    }

    // Draw text
    if (lod >= 2) {
        QFont font("Times", 10);
        font.setStyleStrategy(QFont::ForceOutline);
        painter->setFont(font);
        painter->save();
        painter->scale(0.1, 0.1);
        painter->drawText(170, 180, QString("Model: VSC-2000 (Very Small Chip) at %1x%2").arg(x).arg(y));
        painter->drawText(170, 200, QString("Serial number: DLWR-WEER-123L-ZZ33-SDSJ"));
        painter->drawText(170, 220, QString("Manufacturer: Chip Manufacturer"));
        painter->restore();
    }

    // Draw lines
    QVarLengthArray<QLineF, 36> lines;
    if (lod >= 0.5) {
        for (int i = 0; i <= 10; i += (lod > 0.5 ? 1 : 2)) {
            lines.append(QLineF(18 + 7 * i, 13, 18 + 7 * i, 5));
            lines.append(QLineF(18 + 7 * i, 54, 18 + 7 * i, 62));
        }
        for (int i = 0; i <= 6; i += (lod > 0.5 ? 1 : 2)) {
            lines.append(QLineF(5, 18 + i * 5, 13, 18 + i * 5));
            lines.append(QLineF(94, 18 + i * 5, 102, 18 + i * 5));
        }
    }
    if (lod >= 0.4) {
        const QLineF lineData[] = {
            QLineF(25, 35, 35, 35),
            QLineF(35, 30, 35, 40),
            QLineF(35, 30, 45, 35),
            QLineF(35, 40, 45, 35),
            QLineF(45, 30, 45, 40),
            QLineF(45, 35, 55, 35)
        };
        lines.append(lineData, 6);
    }
    painter->drawLines(lines.data(), lines.size());

    // Draw red ink
    if (stuff.size() > 1) {
        QPen p = painter->pen();
        painter->setPen(QPen(Qt::red, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
        painter->setBrush(Qt::NoBrush);
        QPainterPath path;
        path.moveTo(stuff.first());
        for (int i = 1; i < stuff.size(); ++i)
            path.lineTo(stuff.at(i));
        painter->drawPath(path);
        painter->setPen(p);
    }
}

void Chip::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mousePressEvent(event);
    update();
}

void Chip::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (event->modifiers() & Qt::ShiftModifier) {
        stuff << event->pos();
        update();
        return;
    }
    QGraphicsItem::mouseMoveEvent(event);
}

void Chip::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseReleaseEvent(event);
    update();
}

QColor Chip::getColor() const
{
    return color;
}

int Chip::getX() const
{
    return x;
}

int Chip::getY() const
{
    return y;
}

void Chip::setColor(const QColor &color)
{
    this->color = color;
}

void Chip::setX(const int &x)
{
    this->x = x;
}

void Chip::setY(const int &y)
{
    this->y = y;
}

chip.h
#ifndef CHIP_H
#define CHIP_H

#include <QtGui/QColor>
#include <QDeclarativeItem>

class Chip : public QDeclarativeItem
{
    Q_OBJECT
    Q_PROPERTY(int x READ getX WRITE setX)
    Q_PROPERTY(int y READ getY WRITE setY)
    Q_PROPERTY(QColor color READ getColor WRITE setColor)

public:
    Chip(QDeclarativeItem *parent = 0);
    Chip(const QColor &color, int x, int y);

    QRectF boundingRect() const;

    QColor getColor() const;
    int getX() const;
    int getY() const;

    void setColor(const QColor &color);
    void setX(const int &x);
    void setY(const int &y);

    QPainterPath shape() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget);

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

private:
    int x, y;
    QColor color;
    QList<QPointF> stuff;
};

#endif

In my investigation of this issue, i learned that you can add custom widgets to the QT Designer. I might have to check that out as well before I make my decision. Any help will be appreciated, thanks.
Answer: UPDATE: Dec/2015
You have to mark it as 'supported'
Documentation clearly states that you have to explicitely mark it as supported, otherwise you will get blank boxes.

The items of an unsupported plugin are not painted in the Qt Quick
  Designer, but they are still available as empty boxes and the
  properties can be edited.

In order to do this you must build it as a plugin and then include the keyword designersupported into a qmldir file in the same folder your plugin shared object/dll is placed. This is a whitelist and Qt Creator's puppet will trust your code, be sure not to make long operations or crash, or you will crash the puppet and make the designer useless.

Old and outdated answer below
Same thing happens to me. This seems to be a bug in QtCreator. I digged into the code of the Rectangle QML item and found no special instructions. So QtCreator must have it hard coded or something.
See this: http://developer.qt.nokia.com/forums/viewthread/2555
Answer: They are blank, custom components created with C++ cant be show, but they work fine when running your app. There is a bug placed into Qt for allowing that, but seems to be low priority now as it requires internal Qt changes.
Update:
Just recently, after some discussion, a task was created to implement a mechanism for shadowing custom components, and it was marked P1, nice!. See this bug report, they have even added some work in progress patch you can test. That's a patch for Qt, so we might (or not) be close to having it inside Qt 5.4 and then Qt Creator will have that support. Remember Qt has a release cycle of 6 months now, so it might not be that far. Please register and vote for it, if you need it.


Java IBM MQ Client connected but not getting messages from queue

Question: I have a Camel/Spring-based Java MQ client, which should connect to MQ and get some messages from queue. The connection is estabilished, but the client is not downloading any messages. Wireshark network analysis shows that after some initial processing an MQCLOSE is generated and only further only heartbeat traffic appears.

The IBM MQ server version is 188.8.131.52 while the client JARs are in version 184.108.40.206. But I believe this combination worked on other environments.
I have checked the application on some other environment, where it works properly and after the MQCLOSE_REPLY there comes a number of other messages, starting with what Wireshark recognizes as SOCKET_ACTION. But this does not happen on this environment, why??
Comment: Is the app getting any type of error message from the queue manager?  MQ v7.1 went out of support from IBM on April 30 2017, you would be better to move to the IBM MQ v8 or v9 jar files.
Comment: I answered the question - there was no errors, simply nothing. And it turned out the Camel route was not started properly and the app was simply not querying for the messages
Answer: Your application isn't requesting any messages, that's why it is not receiving any.
Based on the snippet of network trace from your non-working environment, it is only connecting (opening, inquiring and closing the queue manager - which it does during connection processing and after the MQCONN).  The connection that was created is then not used - there are no MQGET or SPI flows, just heartbeats.
Why are you trying to debug this using network packet trace anyway?!
Look at the application and work out what it does after connecting and determine why it isn't requesting any messages.
Answer: a_cornish_pasty was right - the application was not requesting any messages. And that's because the Camel route has not been started properly due to some startup error I missed.


How to automate cleanup of config files from past pkgs as reported by "dpkg -l | grep ^rc"

Question: There are a number of pkgs that weren't removed with "apt-get purge" and I would like to automate cleaning them up.
You can't apt-get purge apache2.2-common because the pkg is already removed.
You can still list the files that are in the pkg with dpkg -L apache2.2-common.
This means I could just remove the list of files in the pkg, but how would dpkg know this?  How do I remove the config files left behind AND remove it from the following report?
Example output:
$ dpkg -l | grep ^rc
rc  apache2.2-common                  2.2.14-5ubuntu8.10                Apache HTTP Server common files
rc  libapache2-mod-php5filter         5.3.10-1ubuntu2ppa6~lucid         server-side, HTML-embedded scripting languag
rc  libapr1                           1.3.8-1ubuntu0.3                  The Apache Portable Runtime Library
rc  libaprutil1                       1.3.9+dfsg-3ubuntu0.10.04.1       The Apache Portable Runtime Utility Library
rc  libgd2-xpm                        2.0.36~rc1~dfsg-3.1ubuntu1        GD Graphics Library version 2
rc  libt1-5                           5.1.2-3ubuntu0.10.04.2            Type 1 font rasterizer library - runtime
rc  php5-gd                           5.3.10-1ubuntu2ppa6~lucid         GD module for php5
rc  ssl-cert                          1.0.23ubuntu2                     simple debconf wrapper for OpenSSL

Comment: I probably do /not/ want to remove ssl-cert. It is just an example.
Answer: Something like this will find the package names:
dpkg --list | grep ^rc | awk '{print $2}'

Or to remove the number of pipes you can use this:
dpkg --list | awk '/^rc/ {print $2}'

So the next step is to feed one of those lists into dpkg --purge.  Using a subshell you can do that easily:
dpkg --purge $(dpkg --list | grep ^rc | awk '{print $2}')

Simple!
Comment: Ah, I tried various iterations of `dpkg | awk | xargs apt-get purge` which was failing because the pkg was not installed.  I never tried `dpkg | awk | xargs dpkg --purge`.  To be honest, I didn't know it had a purge option.  Thank you!


How to change Document Root for Apache2 in Mac OS X

Question: The default location of Document root as per  httpd.conf is /Library/WebServer/Documents. I want this location to be /webcontent. So to do that, I created a webcontent folder in root(/).
Then in the httpd.conf:

Changed the Document root line to DocumentRoot /webcontent
Changed the Directory tag to <Directory "/webcontent">;

After restarting the Apache I'm getting the following page:

Forbidden
You don't have permission to access / on this server.

Could anyone please tell me whether I need to change any permissions anywhere else to change the document root?
Comment: Do you have an `index.html` stored in `/webcontent/`?
Comment: No, but I have a test.html placed inside the webcontent folder. When I  access it I'm getting this forbidden error. Is it mandatory to add index.html?
Answer: The httpd.conf file provided with OS X has a default deny that locks down every directory from every client.  It then allows access to the DocumentRoot directory — that would be the default of /Library/WebServer/Documents.  Page down some in that file and you'll see:
<Directory "/Library/WebServer/Documents">
    # [...]
    Options Indexes FollowSymLinks MultiViews

    # [...]
    AllowOverride None

    # [...]
    Order allow,deny
    Allow from all

</Directory>

Change the "/Library/WebServer/Documents" bit to "/webcontent" and you're good.
Answer: To follow on from @Bred Ackerman answer if your using apache vhost you will need to add: private/etc/apache2/extra/httpd-vhosts.conf
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot "/Users/fred/Sites"
    ServerName 127.0.0.1
    ServerAlias localhost
    ErrorLog "/private/var/log/apache2/localhost-error_log"
    CustomLog "/private/var/log/apache2/localhost-access_log" common
</VirtualHost>


Calculate $ E(X) $ where $f(x,y) = \frac{1}{y}e^{-(y+\frac{x}{y})}$

Question: I am trying to solve the following probability problem:

Please calculate $ E(X) $ where the joint density function of x and y is:
$$f(x,y) =
 \frac{1}{y}e^{-(y+\frac{x}{y})} \text{ for } x,y > 0$$

It is well known that the expected value of a continuous random variable is:
$$
E(X) = \int_{-\infty}^\infty{xf_X(x)dx}
$$
That means that we have to find $f_X(x)$ first by doing:
$$
f_X(x)=\int_{0}^\infty{\frac{1}{y}e^{-(y+\frac{x}{y})}}dy
$$
Note: The zero lower bound is due to the given inequality $ y > 0$.
However, this integral turned out to be a handful (I tried substitution and integration by parts to no avail). Then, I tried this on WolframAlpha instead. However, it gave me an answer which I did not understand (link).
Am I even supposed to integrate this integral in the first place? I do not know much about the bessel function, which was present in the answer given by WolframAlpha. Furthermore, I am only undergoing an introductory probability course so it is not possible for the integral to be so complex (I was not taught bessel function anyway).
Could someone please advise me on how I could calculate the value of $E(X)$?
Comment: "That means that we have to find $f_X(x)$ first" Well, no. $$E(X) = \int_{-\infty}^\infty\int_{-\infty}^\infty x\,f_{X,Y}(x,y)\,dx\,dy$$
Comment: @FrankMoses Not wrong (and I do not see what it is that I wrote that could lead to believe it is), simply the method in my comment is always at least as easy to implement, and in some cases, such as the present one, it is spectacularly more so.
Comment: Just a special case of the formula in your notes saying that, for every suitable $g$, $$E(g(X,Y))=\iint g(x,y)f_{X,Y}(x,y)\,dx\,dy$$
Comment: @Did do you think his approach is generally wrong or for this particular case it is not a good choice? I am asking because I am unable to see anything wrong with the definitions he provided in second and third equations. I will be thankful if you could clarify why his approach is wrong (if it is). Thank you.
Comment: @Did Thank you for your help! I couldn't find this formula in my textbook. I'll add this formula to my notes.
Answer: Make the change of variable $(x,y)\rightarrow (u,v)=(x/y,y)$ and obtain from $(x,y)=(uv,v)$ that $dxdy=v\,dudv$ and
$$\mathbb{E}(X)=\int_0^{\infty}\int_0^{\infty}x\frac{1}{y}e^{-y-\frac{x}{y}}dx dy=\int_0^{\infty}ue^{-u}du\int_0^{\infty}ve^{-v}dv=1\times 1.$$
Answer: It is simple.
First find $f_Y(y)=\int_{0}^{\infty}\frac{1}{y} e^{-(y+\frac{x}{y})} dx= e^{-y}$
Thus find expectation of $Y$ ,$E(Y)=\int_{0}^{\infty} ye^{-y} dy=1$
Now, find the distribution of $X|Y$ , which is $f_{Y|X}(y)=\frac{1}{y}e^{-\frac{x}{y}}$.Thus $X|Y \sim \text{Exponential}(y)$,with mean $y$.
Now, use law of conditional expectation,
$E(X)=E(E(X|Y))=E(Y)=1$


Rotas dos controllers e actions em minúsculo

Question: Como fazer com que as rotas dos controllers e actions sejam todas em minúsculo?
Por exemplo: ao invés de Noticias/Details ficar noticias/details.
Answer: É possível usar uma biblioteca (Nuget) para fazer isto. Lá tem as indicações de como instalar e usá-la. È possível que o projeto tenha sido abandonado. Alternativa.
Mas se estiver usando o .NET 4.5 ou maior tem como configurar sem bibliotecas:
public static void RegisterRoutes(RouteCollection routes) {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.LowercaseUrls = true;
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

Coloquei no GitHub para referência futura.
Retirado da resposta no SO. Na mesma pergunta tem outras formas de fazer isto.
Comment: Essa biblioteca é show. Valeu mano!


javaScript Source maps not working

Question: I have been using sourcemaps generated via gulp to debug for a while without problems. I have recently updated my chrome and they stopped working.
So, I know they should be working and I also can see the source maps inside of the js file generated.
var bundler = browserify({
    // Required watchify args
    cache: {}, packageCache: {}, fullPaths: true,
    // Specify the entry point of your app
    entries: ['./js/init.js'],
    // Add file extentions to make optional in your requires
    extensions: ['.js'],
    // Enable source maps!
    debug: true
});

This is my bundler to generate my js file.
//#
    sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlc1xcYnJvd3NlcmlmeVx
    ......
    HVybiBfO1xuICAgIH0pO1xuICB9XG59LmNhbGwodGhpcykpO1xuIl19

Point being, I know that the source maps are currently being built... But for some reason chrome cannot interact them.
I obviously want the sourcemaps to work. How can I get them working? Maybe a setting in chrome?
Comment: In the settings for the DevTools, click the box marked 'Enable Javascript Sourcemaps'
Comment: Ye, this is actually already checked. But thanks for the headsup :)
Answer: Took me a while and I hate answering my own questions. But who knows somebody else might have the same problem...
My script include had async this little guy was the cause of all evil!!
Wrong

Correct

javaScript Source maps not working


Why doesn't my form submission handler work?

Question: I am new to drupal and require help from Gurus.
I am adding some form elements in the status share form by using hook_form_alter(). These elements are getting displayed in the form, but my submission handler is not working. I am using $form['#submit'][] = 'my_module_form_submit'; to add it to the form.
function my_module_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == "statuses_box") {
    $form['tech2'] = array(
      '#type'=>'select',
      '#title' => '',
      '#attributes' => array('class' => array('tech2')),
      '#options' => array('All'=>'All','New'=>'New',),
      '#multiple' => false,
    );

    $form['#submit'][] = 'my_module_form_submit';
  }
}

function my_module_form_submit($form, &$form_state) {
  // Do something here.
}

The issue I am getting is my_module_form_submit is not getting called. The debugger break-point is not getting hit in this function
I am not getting error message neither on page nor in admin/reports/dblog
What I want is to add a new row in my custom data table, which this code is supposed to create.
Default form handling is running perfectly.
Comment: I'd *try* adding your submit handler BEFORE any current ones. For example: `$form['#submit'] = array_unshift($form['#submit'], my_module_form_submit)`. I'm sure I've had this before, where SOMETHING a submit function does would prevent any more from firing.  I might be thinking of something else though, which is why this is a comment ;-)
Comment: The default submit handler is sometimes (often even) on a specific element in `$form['actions']`. Does the form you're altering have any submit buttons under `$form['actions']`?
Answer: Check your form id. I doubt your form id is not correct. Even you can try this also -
function my_module_form_submit($form, &$form_state) {
  // Do something here.
  echo "
";
  print_r($form);
  exit;
}


remove duplicates from arraylist

Question: I'm trying to remove duplicates from an ArrayList. Every element is repeated not more than 5 times. Why is it always leaving 2 of each duplicate? (0,0,1,2,2,3,3,4,4,5).
void quitarRepetidos_lista(ArrayList* lista)
{
    eNumero* auxNumero;
    eNumero* auxNumero2;
    int i,j;

    if ( lista != NULL  )
    {
        for (i = 0; i<lista->len(lista); i++)
        {
            auxNumero = lista->get(lista,i);
            for ( j = i+1; j<lista->len(lista); j++ )
            {
                auxNumero2 = lista->get(lista,j);
                if ( numero_compare(auxNumero,auxNumero2) == 0)
                {
                    lista->remove(lista,j);
                }
            }
        }
    }
}
int numero_compare(eNumero* numeroAlfa, eNumero* numeroBeta)
{
    if ( ((eNumero*)numeroAlfa)->numero >((eNumero*)numeroBeta)->numero )
    {
        return 1;
    }
    if ( ((eNumero*)numeroAlfa)->numero < ((eNumero*)numeroBeta)->numero )
    {
        return -1;
    }
    return 0;
}

it should say 012345
Comment: What's an ArrayList? Never heard of it. Consult the documentation for the third-part library you're using.
Comment: @GMP_47  This condition  ilen(lista) already does not make sense. Why not just  ilen()?
Comment: Where is `ArrayList` defined?
Comment: @GMP_47 It seems the problem in the loops. For example if in this loop  for ( j = i+1; jlen(lista); j++ ) an item was deleted then j shall not be increased.
Answer: Ignoring the fact that your code is not correct in C (you are calling methods on pointers, eg lista->remove(...) but C doesn't have objects).
This is just a guess since we can't know the implementation or the interface of ArrayList class (which curiously mimics the name of the one from Java SDK) but when you do
lista->remove(lista,j);

usually this makes all elements after the one at j shift back by one position (since you are removing j-th then (j+1)-th becomes j-th, filling the hole).
          j
+---+---+---+---+---+---+
| A | B | C | D | E | F |
+---+---+---+---+---+---+

erase element at j

          j
+---+---+---+---+---+
| A | B | D | E | F |
+---+---+---+---+---+

for loop body ends, j is incremented

              j
+---+---+---+---+---+
| A | B | D | E | F |
+---+---+---+---+---+

D has been skipped

But since you are inside a loop, j is then incremented by the for afterthought, effectively skipping one element. You should do something like:
for ( j = i+1; j < lista->len(); /* no after thought */) {
  if (are equal)
    lista->remove(j);
  else
    ++j;

So that you don't increment j if you just removed an element, since the next element will be placed still at j.
Comment: It crashes.
Forgot to say that al_remove() has a contract() function that rezises down the arraylist
EDIT: seems like it iterates 5 times before crashing.
Comment: This shouldn't be relevant as long as you are not caching the size of the list since at each loop iteration `j` is checked against again `lista->len()` (which will be lesser)
Answer: After removing an element from your list, you shouldn't increment j, otherwise you'd end up skipping the very next element (which takes on the index of the element that was just removed).
Instead of:
if ( numero_compare(auxNumero,auxNumero2) == 0)
{
    lista->remove(lista,j);
}

You should decrement j only when an element is removed:
if ( numero_compare(auxNumero,auxNumero2) == 0)
{
    lista->remove(lista,j--);
}

Comment: https://github.com/aflorio91/Modelo_P1_Laboratorio_1/tree/master/1F_SP_FlorioAndres here you'll find all the arraylist funcions in ArrayList.c.
Comment: It crashes with that.
Comment: @GMP_47 Show us your code for `len()`, `get()`, and `remove()`
Answer:
It crashes…

In addition to the error treated by Jack and frslm, there are errors in ArrayList.c, function resizeDown().

newLen = (pList->reservedSize - pList->size);
Since the reserved size of the list is to be shrinked when there are more than AL_INCREMENT unused elements (see contract()), reservedSize has to be reduced by AL_INCREMENT rather than by pList->size, which latter lead to a way to small newLen. Correct:
newLen = pList->reservedSize - AL_INCREMENT;
pList->size = newLen;
Since function resizeDown() is only to shrink the allocated space for the list, not remove an element from it (what contract() already did), newLen has to be assigned to reservedSize rather than to pList->size. Correct:
pList->reservedSize = newLen;

remove duplicates from arraylist


Having my server ping google.com prevents it from freezing a few hours after logging out

Question: I have an Ubuntu Godaddy server I use to host mail and webapps. It started having problems a couple months ago. It would lock up and stop responding to anything. I couldn't ssh into it, so I'd have godaddy power cycle the server.
I have never seen anything that looked suspicious in the var logs (although I'm no expert at reading them). An fsck turned up no problems. Godaddy replaced the ram, but found no hardware problems. I started logging the output from "top" to a log file and found that even that stops running when the server freezes.
Now, here is the crazy part: It got so bad that it would actually go down every few hours, but then it stopped going down. I eventually realized I had left an ssh terminal logged into the machine running top. This seemed unlikely to be a reason, but after the server was up with no problems for a full week (remember, it had been going down after just a few hours), I disconnected from the ssh session. Lo and behold, within a few hours the server froze again!
I had them power cycle again and then left another ssh session open with top. It has been going without problems for 8 days now.
I told others about this and they hardly believe me. I simply can't imagine what is going on. I don't know what else to try other than to just get a new server and reinstall everything.
Does anyone have any ideas about what I can look for to determine what the cause is? Is it possible there's some sort of exploit on the server which only runs if everyone is logged out of the system?
EDIT:
The power management gone haywire sounds plausible, so I've modified the /boot/grub/menu.lst to boot with acpi=off and apm=off. It appears to have prevented kacpid and kacpid_notify from being in the process list, so I assume I did that right. I've disconnected all my sessions from the server. I'll check later tonight to see if it's still up. If it goes down then I'll try the pinging process idea.
EDIT:
It went down again. It lasted about a day. I've had them reboot, so now I'll try running "nohup ping -i 5 google.com &" and then disconnect. If it goes down again I'll come back. Hopefully someone will have some more ideas.
EDIT:
Huh. It hasn't gone down in 5 days. I have only briefly been logged in. The "nohup ping -i 5 google.com &" appears to do the job. Anyone have any ideas why? I'm not exactly excited about leaving my machine pinging google ever 5 seconds indefinitely, but if I can't figure out anything else I'll probably add a startup script that runs this when the server is rebooted.
Comment: What happens if you leave a process running, pinging another host periodically?   That will keep the networking stack ticking over without leaving a network connection open.   The idea here is to narrow things down by changing only some of the variables.  Also, you could just try asking the hosting folks to move your disks to some entirely different hardware instance to see if the problem follows the hardware or follows the OS install.
Comment: This smells of a hardware problem.
Comment: I've had flaky hardware that did the same thing, ping "fixed" it just the same. Replacing the hardware with non-broken stuff worked better.
Answer: Some kind of power saving mechanism, going into sleep mode perhaps.  Maybe disable the ACPI modules in /etc/modules.conf to test the hypothesis.


Showing a diffeomorphism extends to the neighborhood of a submanifold

Question: Does anyone have a proof of problem 14, on page 56 of Guillemin and Pollack? I meant to do it as an exercise (I'm teaching myself the subject) but I'm struggling with the last step. Suggestions?
Suppose that the derivative of $f: X \rightarrow Y$ is an isomorphism whenever $x$ lies in the submanifold $Z \subset X$, and assume that $f$ maps $Z$ diffeomorphically onto $f(Z)$. Prove that $f$ maps a neighborhood of $Z$ diffeomorphically onto a neighborhood of $f(Z)$. Essentially, this is the inverse function theorem but without assumptions on the compactness of $Z$.
So far I have local inverses $g_i : U_i \rightarrow X$ where $\{U_i\}$ is a locally finite collection of open subsets of $Y$ covering $f(Z)$. Define $W = \{y \in U_i : g_i(y) = g_j(y)\mbox{ whenever }y \in U_i \cap U_j\}$. We then have a smooth inverse $g: W \rightarrow X$.
How do I show that $W$ then contains an open neighborhood of $f(Z)$ (using local finiteness of $\{U_i\}$, presumably)?
Comment: Could you post the problem statement?
Comment: Suppose that the derivative of $f: X \rightarrow Y$ is an isomorphism whenever $x$ lies in the submanifold $Z \subset X$, and assume that $f$ maps $Z$ diffeomorphically onto $f(Z)$. Prove that $f$ maps a neighborhood of $Z$ diffeomorphically onto a neighborhood of $f(Z)$. Essentially, this is the inverse function theorem but without assumptions on the compactness of $Z$
Comment: @Eric Please edit your question and add it there.
Answer: Eric: Here's the idea. A good example I've found helpful when presenting this is the spiral $f\colon\mathbb R^2\to\mathbb R^2$, $f(t) = (e^x\cos y,e^x\sin y)$, with $Z$ the diagonal. So $f(Z)$ is the spiral $\{(e^t\cos t,e^t\sin t): t\in\mathbb R\}$. You can imagine overlapping but non-adjacent little boxes on which $f$ is a local diffeomorphism, so that the local inverses don't quite glue.
As G&P suggest, cover $f(Z)$ with open sets $U_i$ on which we have a local inverse $g_i$ of $f$. The key point is that we may assume this is a locally finite open cover. (In our example, note that it's crucial that $0\notin f(Z)$.) Let $V_{ij} = \overline{\{v\in U_i\cap U_j: g_i(v)\ne g_j(v)\}}$. Then let $\tilde U_i = U_i - \cup_j V_{ij}$, and let $W=\cup_i \tilde U_i$. Check that $W$ works.
Let me know if this works better than what G&P have.
Comment: When is a (perhaps) infinite union of closed sets closed?
Comment: We should discuss this in person in 2 weeks! :) $W$ is open and contains $f(Z)$. And $f$ has a well-defined inverse on $W$. (I still like my spiral example to understand what's going on.) What am I missing?
Comment: That seems reasonable, but my problem is with how to check that $W$ in fact works. How exactly do we employ the local finiteness of the open cover? Perhaps I haven't thought about it long enough?
Comment: Is the point just that $W$ is a nonempty open set, and so therefore contains some open neighborhood of $f(Z)$?
Comment: But to argue that $W$ is open requires the local finiteness. The rest is easy, yes, as the $g_i$'s glue to make a well-defined smooth map.
Comment: Hi, Ted, I was working this problem and I came across your old answer here. How can we be sure that $V_{ij}$ does not meet $f(Z)$? Suppose that $y_i$ are a sequence are points in $U_i \cap U_j$ such that $g_i(y_i) \neq g_j(y_i)$ and $y_i \to y \in f(Z)$. This is impossible if $y$ is in $U_i \cap U_j$, by continuity of the inverses. But what if it is not?
Comment: Eric: I'm two years rusty on this. But isn't it the point that $f|_Z$ has an inverse, so on points of $f(Z)$ the various $g_i$ (where they're defined) must of course agree?... Or am I missing something?
Comment: Ted: definitely the $g_i$ must agree on $f(Z)$. But when we remove the $V_{ij}$, which are the closures of the sets where the $g_i$ disagree, we want to end up with an open set containing $f(Z)$. Definitely $\{g_i \neq g_j\}$ does not meet $f(Z)$, but how can we be sure its closure does not meet $f(Z)$? In particular, what if a sequence of points in $\{g_i \neq g_j\}$ converges to a point in $f(Z) \setminus (U_i \cap U_j)$?
Comment: Eric, the point is that because of local finiteness, $\cup V_{ij}$ is closed, so you're left with an open set!
Comment: Ted, I agree it's open, but why does it not meet $f(Z)$? Certainly $\{g_i \neq g_j\}$ does not meet $f(Z)$, but why does its closure not meet $f(Z)$?
Comment: Great, would be lovely to meet you. There is something I'm not getting, because I can't see why $W$ contains $f(Z)$ (although certainly $g_i = g_j$ on $f(Z)$). I think I'll ask it as a separate question.
Comment: Great, would be lovely to meet you. There is something I'm not getting, because I can't see why $W$ contains $f(Z)$ (although certainly $g_i = g_j$ on $f(Z)$). I asked it as a separate question here: http://math.stackexchange.com/questions/1413022/generalization-of-inverse-function-theorem-to-noncompact-submanifolds


32-bit fractional multiplication with cross-multiplication method (no 64-bit intermediate result)

Question: I am programming a fixed-point speech enhancement algorithm on a 16-bit processor. At some point I need to do 32-bit fractional multiplication. I have read other posts about doing 32-bit multiplication byte by byte and I see why this works for Q0.31 formats. But I use different Q formats with varying number of fractional bits.
So I have found out that for fractional bits less than 16, this works:
(low*low >> N) + low*high + high*low + (high*high << N)

where N is the number of fractional bits. I have read that the low*low result should be unsigned as well as the low bytes themselves. In general this gives exactly the result I want in any Q format with less than 16 fractional bits.
Now it gets tricky when the fractional bits are more than 16. I have tried out several numbers of shifts, different shifts for low*low and high*high I have tried to put it on paper, but I can't figure it out.
I know it may be very simple but the whole idea eludes me and I would be grateful for some comments or guidelines!
Answer: It's the same formula.  For N > 16, the shifts just mean you throw out a whole 16-bit word which would have over- or underflowed.   low*low >> N means just shift N-16 bit in the high word of the 32-bit result of the multiply and add to the low word of the result.  high * high << N means just use the low word of the multiply result shifted left N-16 and add to the high word of the result.
Answer: There are a few ideas at play.
First, multiplication of 2 shorter integers to produce a longer product. Consider unsigned multiplication of 2 32-bit integers via multiplications of their 16-bit "halves", each of which produces a 32-bit product and the total product is 64-bit:
  a * b = (a_hi * 216 + a_lo) * (b_hi * 216 + b_lo) =
  a_hi * b_hi * 232 + (a_hi * b_lo + a_lo * b_hi) * 216 + a_lo * b_lo.
Now, if you need a signed multiplication, you can construct it from unsigned multiplication (e.g. from the above).
Supposing a < 0 and b >= 0, a *signed b must be equal
  264 - ((-a) *unsigned b), where
  -a = 232 - a (because this is 2's complement)
IOW,
  a *signed b =
  264 - ((232 - a) *unsigned b) =
  264 + (a *unsigned b) - (b * 232), where 264 can be discarded since we're using 64 bits only.
In exactly the same way you can calculate a *signed b for a >= 0 and b < 0 and must get a symmetric result:
  (a *unsigned b) - (a * 232)
You can similarly show that for a < 0 and b < 0 the signed multiplication can be built on top of the unsigned multiplication this way:
  (a *unsigned b) - ((a + b) * 232)
So, you multiply a and b as unsigned first, then if a < 0, you subtract b from the top 32 bits of the product and if b < 0, you subtract a from the top 32 bits of the product, done.

Now that we can multiply 32-bit signed integers and get 64-bit signed products, we can finally turn to the fractional stuff.
Suppose now that out of those 32 bits in a and b N bits are used for the fractional part. That means that if you look at a and b as at plain integers, they are going to be 2N times greater than what they really represent, e.g. 1.0 is going to look like 2N (or 1 << N).
So, if you multiply two such integers the product is going to be 2N*2N = 22*N times greater than what it should represent, e.g. 1.0 * 1.0 is going to look like 22*N (or 1 << (2*N)). IOW, plain integer multiplication is going to double the number of fractional bits. If you want the product to
have the same number of fractional bits as in the multiplicands, what do you do? You divide the product by 2N (or shift it arithmetically N positions right). Simple.

A few words of caution, just in case...
In C (and C++) you cannot legally shift a variable left or right by the same or greater number of bits contained in the variable. The code will compile, but not work as you may expect it to. So, if you want to shift a 32-bit variable, you can shift it by 0 through 31 positions left or right (31 is the max, not 32).
If you shift signed integers left, you cannot overflow the result legally. All signed overflows result in undefined behavior. So, you may want to stick to unsigned.
Right shifts of negative signed integers are implementation-specific. They can either do an arithmetic shift or a logical shift. Which one, it depends on the compiler. So, if you need one of the two you need to either ensure that your compiler just supports it directly
or implement it in some other ways.


Handling windows events in a tight loop?

Question: I have written a compiler and interpreter for a scripting language.   The interpreter is a DLL ('The Engine') which runs in a single thread and can load many 100s or 1000s of compiled byte-code applications and excecute them as a set of internal processes.     There is a main loop that excecutes a few instructions from each of the loaded app processes before moving one to the next process.
The byte code instruction in the compiled apps can either be a low level instructions (pop, push, add, sub etc) or a call to an external function library (which is where most of the work is done).     These external libararies can call back to the engine to put the internal processes into a sleep state waiting for a particular event upon which the external function (probably after receiving an event) will wake up the internal process again.         If all internal processes are in a sleep state (which the are most of the time) then I can put the Engine to sleep as well thus handing off the CPU to other threads.
However there is nothing to prevent someone writing a script which just does a tight loop like this:
while(1)
   x=1;
endwhile
Which means my main loop will never enter a sleep state and so the CPU goes up to 100% and locks up the system.     I want my engine to run as fast as possibly, whilst still handling windows events so that other applications are still responsive when a tight loop similar to the above is encountered.
So my first question is how to add code to my main loop to ensure windows events are handled without slowing down the main engine which should run at the fastest speed possible..
Also it would be nice to be able to set the maximum CPU usage my engine can use and throttle down the CPU usage by calling the occasional Sleep(1)..
So my second question is how can I throttle down then CPU usage to the required level?
The engine is written in Borland C++ and makes calls to the win32 API.
Thanks in advance
Comment: If you're making a cooperative multitasking scheme, and a script doesn't cooperate - should you really force it? Make a preemptive mechanism if that's what you want.
Comment: IMHO you're going a step backward (ala windows 3.1).  You have a full multi-process and multi-thread capable operating system.  Why not just let each script run in it's own thread and manage that threads.  The operating system will be responsible to perform the context switches and assign processor time for each of the scripts.  You can then control thread priorities and manage thread lifetime.  For infinite loops as the shown you can take some steps on your engine to terminate the problematic threads and even if you don't, this will never stops other scripts from running.
Comment: Sean, you seem to be new to SO: welcome :)  It's been a few days since the last answer was posted.  You should upvote answers you find useful (click the triangle above the number on the top left of an answer) and if any answer your question well, mark it as the accepted answer (click the tick.)  We go to a lot of effort to answer questions here, and it's altruistic, but it's nice to get the 'reward' of points.
Answer: 1. Running a message loop at the same time as running your script

I want my engine to run as fast as
  possibly, whilst still handling
  windows events so that other
  applications are still responsive when
  a tight loop similar to the above is
  encountered.

The best way to continue running a message loop while performing another operation is to move that other operation to another thread.  In other words, move your script interpreter to a second thread and communicate with it from your main UI thread, which runs the message loop.
When you say Borland C++, I assume you're using C++ Builder?  In this situation, the main thread is the only one that interacts with the UI, and its message loop is run via Application->Run.  If you're periodically calling Application->ProcessMessages in your library callbacks, that's reentrant and can cause problems.  Don't do it.
One comment to your question suggested moving each script instance to a separate thread.  This would be ideal.  However, beware of issues with the DLLs the scripts call if they keep state - DLLs are loaded per-process, not per-thread, so if they keep state you may encounter threading issues.  For the moment purely to address your current question, I'd suggest moving all your script execution to a single other thread.
You can communicate between threads many ways, such as by posting messages between them using PostMessage or PostThreadMessage.  Since you're using Borland C++, you should have access to the VCL.  It has a good thread wrapper class called TThread.  Derive from this and put your script loop in Execute.  You can use Synchronize (blocks waiting) or Queue (doesn't block; method may be run at any time, when the target thread processes its message loop) to run methods in the context of another thread.
As a side note:

so that other
  applications are still responsive when
  a tight loop similar to the above is
  encountered.

This is odd.  In a modern, preemptively multitasked version of Windows other applications should still be responsive even when your program is very busy.  Are you doing anything odd with your thread priorities, or are you using a lot of memory so that other applications are paged out?
2. Handling an infinite loop in a script
You write:

there is nothing to prevent someone
  writing a script which just does a
  tight loop like this:
while(1) x=1; endwhile
Which means my main loop will never
  enter a sleep state and so the CPU
  goes up to 100% and locks up the
  system.

but phrase how to handle this as:

Also it would be nice to be able to
  set the maximum CPU usage my engine
  can use and throttle down the CPU
  usage by calling the occasional
  Sleep(1)..
So my second question is how can I
  throttle down then CPU usage to the
  required level?

I think you're taking the wrong approach.  An infinite loop like while(1) x=1; endwhile is a bug in the script, but it should not take down your host application.  Just throttling the CPU won't make your application able to handle the situation.  (And using lots of CPU isn't necessarily a problem: if it the work is available for the CPU to run, do it!  There's nothing holy about using only a bit of your computer's CPU.  It's there to use after all.)  What (I think) you really want is to be able to continue to have your application able to respond when running this script (solved by a second thread) and then:

Detect when a script is 'not responding', or not calling into your callbacks
Be able to take action, such as asking the user if they want to terminate the script

An example of another program that does this is Firefox.  If you go to a page with a misbehaving script, eventually you'll get a dialog asking if you want to stop the script running.
Without knowing more about how your script is actually interpreted or run, I can't give a detailed answer to these two.  But I can suggest an approach, which is:

Your interpreter probably runs a loop, getting the next instruction and executing it.  Your interactivity is currently provided by a callback running from one of those instructions being executed.  I'd suggest making use of that by having your callback simply log the time it was last called.  Then in your processing thread, every instruction (or every ten or a hundred) check the current time against the last callback time.  If a long time has passed, say fifteen or thirty seconds, it may be an indication that the script is stuck.  Notify the main thread but keep processing.
For "time", something like GetTickCount is probably sufficient.
Next step: Your main UI thread can react to this by asking the user what to do.  If they want to terminate the script, communicate with the script thread to set a flag.  In your script processing loop, again every instruction (or hundred) check for this flag, and if it's set, stop.
When you move to having one thread per script interpreter, you TThread's Terminated flag for this.  Idiomatically for something that runs infinitely in a thread, you run in a while (!Terminated && [any other conditions]) loop in your Execute function.

To actually answer your question about using less CPU, the best approach is probably to change your thread's priority using SetThreadPriority to a lower priority, such as THREAD_PRIORITY_BELOW_NORMAL.  It will still run if nothing else needs to run.  This will affect your script's performance.  Another approach is to use Sleep as you suggest, but this really is artificial.  Perhaps SwitchToThread is slightly better - it yields to another thread the OS chooses.  Personally, I think the CPU is there to use, and if you solve the problem of an interactive UI and handling out-of-control scripts then there should be no problem with using all CPU if your script needs it.  If you're using "too much" CPU, perhaps the interpreter itself could be optimised.  You'll need to run a profiler and find out where the CPU time is being spent.
Comment: Thanks for that comprehensive response.. My interpreter DLL is run in it's own thread, but it will lock up the GUI front end and slow down the whole machine if this thread enters a tight loop.  I think the answer is to call ProcessMessages via Synchronise to ensure windows events are still processed.            There are a number of reasons why I didn't run scripts in separate threads,  one of them being that there might be many 1000's of scripts running simulataneously and I was not keen on the idea of 5000 OS threads being created...
Comment: You could create a few and have each thread run several script instances.  `ProcessMessages` is generally not a good idea, it can be re-entrant.  If you main thread is not doing anything else, it should be processing messages already!
Answer: Although a badly designed script might put you in a do-nothing loop, don't worry about it. Windows is designed to handle this kind of thing, and won't let your program take more than its fair share of the CPU. If it does manage to get 100%, it's only because nothing else wants to run.


MassAssignmentException in Model.php line 444: users_id

Question: estou fazendo um listerner para salvar quando o usuário loga, e imprime o erro do título no models do User.
public function accesses()
    {
        // Não esqueça de usar a classe Access: use App\Models\Access;
        return $this->hasMany(Access::class);
    }

public function registerAccess()
    {
        // Cadastra na tabela accesses um novo registro com as informações do usuário logado + data e hora
        return $this->accesses()->create([
            'user_id'   => $this->id,
            'datetime'  => date('Y-m-d H:i:s')
        ]);
    }

Answer: Erros de MassAssignment geralmente ocorrem porque os campo inserido não está na propriedade $fillable do Model ou está protegido pela propriedade $guarded do Model.
No teu caso é preciso checar se user_id e datetime estão como fillable no model Access. Ou então usar o método relationship->save() para criar o registro.
Exemplo utilizando save():
public function registerAccess()
{
    return $this->accesses()->save(new Access([
        'user_id'   => $this->id,
        'datetime'  => date('Y-m-d H:i:s')
    ]);
}

Comment: O create é usado para *mass assignment*, como por exemplo adicionar um array de arrays com vários `Access` a ser criados. O save é usado para salvar um único model.
Então o create exige que o model saiba que campos são permitidos serem inseridos em lote. Por isso o save não checa o campo `$fillable`
Comment: Tem um detalhe que está me incomodando. Na exception diz que o erro é por causa de um campo `users_id` mas na pergunta está `user_id`.
Checa se não tem um *typo* na tua classe.
Comment: Se tu alterar o valor da propriedade `$timestamps` no teu model `Access` ele não faz isso. Olha [aqui nos docs](https://laravel.com/docs/5.6/eloquent#defining-models)
Comment: adicionei dessa forma, protected $fillable = [
        'name', 'email', 'password', 'user_id', 'datetime'
    ]; @fernandosavio o model->save() substitui o create()?
Comment: No model `Access::class` ?
Comment: mudei para return $this->hasMany('App\Access'); continua a mesma coisa.
Comment: testei com o save também, o erro continua.
Comment: Consegui esste fillable pertence ao model da Access, e não do users. valeu @fernandosavio agora tenho outro erro para tratar. é regra ele colocar o updated_at`, `created_at` no insert?


Equivalent inductive step to $P(n) \implies P(n+1)$

Question: In a usual proof by induction the inductive step involves assuming $P(n)$ is true and then showing that $P(n+1)$ follows.
Are there any other valid inductive steps? That is, can we define an equivalent inductive step $P(q) \implies P(k)$, where $q \neq n$ and $k \neq n+1$ such that proving the implication in the inductive step proves that the given proposition holds true for all $n$
EDIT: This question comes from a problem that I had in my Discrete math class:
Assume we have the statement $P(n)$ which is defined over the set of natural numbers. Is this sufficient to conclude that
$P(n)$ is true for all $n \in N$ :
\begin{align*}
&P(2) \ \text{is true} \\
&\forall n \in N. P(n) \implies P(2n) \\
&\forall n \in N. P(n) \ \wedge n>1 \implies P(n-1)
\end{align*}
Comment: Other such would almost certainly be able to be reworded as traditional induction.  Strong induction for instance might have been worded $P(1)\wedge P(2)\wedge \cdots \wedge P(k)\implies P(k+1)$ but it could have been reworded as $Q(k)\implies Q(k+1)$ where $Q(k)\equiv P(1)\wedge P(2)\wedge \cdots \wedge P(k)$
Comment: Not sure what you mean.  It's standard, for example, to use "strong" induction, that is $P(≤n)\implies P(n+1)$.  But one could imagine something using, say, $P(n-1)≤P(n+1)$, though your base case would need to cover both even and odd numbers.
Comment: Typo:  in my first comment I meant to write $P(n-1)\implies P(n+1)$.  Thus, your induction separates even $n$ from odd.
Comment: Maybe look up transfinite induction
Comment: @Shinrin-Yoku What do you mean by "finite in the theory but not the meta theory"? And why would it contradict JMoravitz's observation
Comment: That Edit is useful!  So note: now you don;t have a single conditional that potentially 'replaces' the normal step, but several conditionals.  And yes, if you have several conditionals, then there are many ways to make it work.
Comment: With these two properties it should not be too difficult that you indeed have $P(k) \Rightarrow P(k+1)$.
Comment: One semi-common case shows $P(0)$ and $P(n)\implies P(2n)\land P(2n+1).$
Comment: See [here](https://math.stackexchange.com/a/190543/242) for the induction method used in your problem.
Answer: If you can prove multiple conditionals, then yes, there are many (infinitely, really) ways to 'cover' all numbers.
For example, suppose I can show all of:
$P(1)$
$P(2)$
$P(n) \to P(n+2)$
Well, then $P(n)$ should be true for all $n$: Since we have $P(1)$, and since we have $P(n) \to P(n+2)$, we now also have $P(3)$. Applying $P(n) \to P(n+2)$ again, that gives us $P(5)$ .. and then $P(7), P(9)$, ... etc. So, every odd number has then property $P$
But $P(n)$ will also be true for any even number: we have $P(2)$, and therefore $P(4)$, and thus $P(6), P(8)$, ... etc. So, all even numbers have property $P$ as well. Butif all even numbers and all odd numbers have property $P$, then all numbers have property $P$.
So, collectively, these three claims can be used to prove that all numbers have property $P$. And to prove those 3 claims, you are doing some kind of induction: $P(1)$ and $P(2)$ are now two base cases, and $P(n) \to P(n+2)$ is a kind of inductive step to 'get to' all other numbers.
You were probably given the domino metaphor for induction. 'Standard' induction will place all 'dominoes' in one long line:
$1 \to 2 \to 3 \to 4 \to 5 ...$
However, there are many other ways to place all the dominoes so that, when you knock over a initial bunch, they eventually all get knocked over. What I just described above corresponds to having two lines:
$1 \to 3 \to 5 \to ....$
$2 \to 4 \to 6 \to ....$
So, if I knock over $1$ and $2$, will they all get knocked over? Yes.
For your specific case, ask yourself this: can I 'get to' any number $n$, given that I start at number $2$, and from there, either can go to a number twice as large ('double'), or (assuming I am at any number greater than $1$) get to a previous number ('previous')?
For example, can you get to 13?  Sure!  From $2$ we can 'double' to go to $4$, and then 'double' again to get to $8$. From $8$ we can do 'previous' to get to $7$, then 'double' to get to $14$, and finally 'previous' to get to $13$.
Note that this is not the only way to get to $13$ .. I could do $2 \to 4 \to 8 \to 16 \to 15 \to 14 \to 13$.  But being able to get to some number in multiple ways does not take away from the fact that I can get to that number, and that's all you are about.
So again, the question is: can we 'get to' every number by applying the 'double' or 'previous' operation some number of times?  And if you think the answer is yes, how would you mathematically prove that?
Comment: Note that for this example you could also set an other property: $P'(n) = P(n)$ and $P(n+1)$. Then it is trivial to prove $P'(k) \rightarrow P'(k+1)$, so $P'(n)$ is true $\forall n$. Then you just recall that $P(n)$ is true as well, being implied by $P'(n)$.
Answer: Ok lets try to generalize a bit. We could replace the map $n\mapsto n+1$ with some map $f:\mathbb N \to \mathbb N$. Then we could pose conditions on $f$ so that
$$
0\in N\text{ and } (n\in N \implies f(n)\in N)
$$
implies that $N=\mathbb N$ for every subset $N\subset \mathbb N$. The condition on $f$ would be
$$
\{f(0),f(f(0)),\dots,f^k(0),\dots\}=\mathbb N.
$$
Lets go a step further. Let $S:\mathscr P(\mathbb N)\to\mathscr P(\mathbb N)$. We shall call $S$ inductive if
$$
0\in N\text{ and } (X\subset N\implies S(X)\subset N)
$$
implies that $N=\mathbb N$ for every $N\subset \mathbb N$. Then we could have some kind of generalized induction. Show that $0$ staisfies your proposition $P(0)$. Then the induction step would be: If $P$ is satisfied on $X\subset \mathbb N$ then it is also satisfied on $P(S(X))$. This would then imply that the proposition is true on $\mathbb N$, provided that $S$ is inductive.
"Strong induction" would then be given with $S(\{0,\dots,n\})=\{n+1\}$ (and $S=id$ on all other sets)
Now one could try to describe which $S$ are inductive.
Is that what you are looking for?