Causal inference in DAGs and resulting structural equations

Question: I am trying to understand the difference between the two modelling approaches described below that stems from the causal graphs.
Our goal is to causally measure the total treatment effect of our expenditure of two investment options denoted $X_1, X_2$ on the final outcome sales $Y$. To our help we also have measurements of mediators denoted $end\_exog\_1, end\_exog\_2$.
We know that the investment options causally affect sales in the following way:

Note that all variables are observed: yellow ovals denote exogenous variables and blue ovals denote endogenous variables.
To model this we set up the following system of equations(just as an example, note that the graph does not explicitly state any concrete computations, just relations between variables):
\begin{align}
Y &= b_0 + b_1 * end\_exog\_2 + b_2 * x_1 + b_3 * x_2 + \epsilon\\
end\_exog\_2 &= b_4 + b_5 * end\_exog\_1 + x_1 * b_6 + x_2 * b_7 + \epsilon\\
end\_exog\_1 &= b_8 + b_9 * x_1 + b_{10} * x_2 + \epsilon
\end{align}

We could also get rid of our mediator variables $end\_exog\_1, end\_exog\_2$ resulting in a graph with solely two edges $X_1 -> Y, X_2 -> Y$ which we could consider e.g as a single multiple regression equation(just as an example, note that the graph does not explicitly state any concrete computations, just relations between variables): $Y = b_0 + b_1 * X_1 + b_2 * X_2$.

My understanding of why choose one modelling approach over the other one: since we have this extra information in $end\_exog\_1, end\_exog\_2$ and we know the causal structure it should be smart to utilize it.
Given the simple structure with just 2 edges as in case 2 the effect is indeed identifiable, as can be shown by the backdoor criterion as well as the frontdoor criterion. For situation number 1 it is a bit different: the backdoor criterion is violated due to the mediator variables being descendants of our exogenous variables $X_1, X_2$. However, the frontdoor criterion is still valid.

Which is the preferred modelling way given that we want to understand the total treatment effect of $X_1, X_2$ on $Y$ and why is that?
Answer: Since you talk about identification, I will focus on the causal considerations of your problem. At this level, which modelling approach to choose is not a matter of whether we have "extra information", but rather what is our question of interest. In technical terms, it is a matter of defining our estimand$^1$. You should answer whether you care about the total effect of $X_1$ and $X_2$ on $Y$, or their direct effect on $Y$, or their indirect effect that is mediated through mediators $M_1$ and $M_2$.
Once you have chosen an estimand, you will see that they limit which models you can use to estimate it. For instance, a decomposition of the total effect into direct and indirect components will force you to include mediators in the model (as well as making stronger assumptions).
However, if your estimand is the total treatment effect, you could use both models to estimate this quantity. Note however that a DAG encodes non-parametric relationships: it is not making any assumptions about the functional form of the model for $Y$ for the different covariates. Both models you have suggested assume that there is no interaction between $X_1$ and $X_2$, i.e. no $X_1X_2$ term, as well as that the effects are linear and additive. Without further justification for these assumptions, neither of these models might help you "understand the effect of $X_1, X_2$ on $Y$", which starts to involve estimation and thus statistical considerations, which go beyond mere identification.

Note also that you say that

the backdoor criteria [sic] is violated due to the mediator variables being descendents of our exogenous variables X1,X2

, however there is no backdoor path from $X_1$ or $X_2$ to $Y$---no arrow is entering $X_1$ or $X_2$, so perhaps you would want to re-check your definition.

On the fundamental importance of defining your estimand, you can see "What is your estimand? Defining the target quantity connects statistical evidence to theory"
Comment: Thanks for your response. I apologize for any lack of clarity in my previous question. My intention was to inquire whether there are discernible advantages to adopting one of the two proposed scenarios, considering my goal of estimating the total treatment effect. Additionally, I understand that the graphs I provided earlier are of a conceptual nature and do not reflect concrete computations. Therefore, I intend to augment my question with more detailed information. Rest assured, I will make the necessary edits to clarify these points. Your assistance in this matter is greatly appreciated..
Comment: As for the backdoor criterion, i might have misinterpreted it, but to my understanding, the first of the two criterions making up the backdoor criterion does not consider arrows into our treatments: "Backdoor Criterion — Given an ordered pair of variables (X, Y) in a directed acyclic graph G, a set of variables Z satisfies the backdoor criterion relative to (X, Y) 1; if no node in Z is a descendant of X, and 2; Z blocks every path between X and Y that contains an arrow into X." see criterion 1
Comment: I.e are there any theoretical benefit of structuring the problem as in case 1 or is it just an question of which question we are asking ourselves? (given that we want to estimate the total treatment effect - which in case one i would have done by path analysis)
Comment: @jack Relative to your backdoor criterion point, the set of variables $Z$ refers to the adjustment set. The second condition states that the backdoor criterion is not satisfied if there are (open) paths between X and Y that **contain an arrow into X**. You do not have any (open) paths between X and Y that "contain an arrow into X", thus there is nothing to block. Condition 1 is satisfied then because $Z$, the adjustment set, is empty: $Z = \{ \}$. You are assuming that the mediators are those variables $Z$, but they are not used to block any *backdoor* paths, hence they can be descendants.
Comment: @jack I feel the question is not stated in such a clear manner that allows for such a clear answer. How would one evaluate what are the "theoretical benefits" of a certain approach if not relative to a specific question or goal we have? In the particular case the goal is to estimate the total treatment effect, any model would work from the causal perspective. From the statistical perspective, we would need more information as problem-specific issues such as measurement error could favor one model over the other.


é possivel usar query nativa + spring data para fazer paginação?

Question: Preciso fazer uma paginação usando querys dinamicas porque tenho que fazer muitos joins, estou tentando fazer somente um select * para testar o uso da query nativa + spring data, mas a mensagem de erro é a seguinte:
Cannot use native queries with dynamic sorting and/or pagination in method public abstract

Segue o código:
@Query(value = "select i.* " + "from VRS.TB_CLIE_INVS i ",nativeQuery = true)
Page<TbClieInv> testePaginacaoQuery(Pageable pageable);

Comment: Banco de dados Oracle, a  minha aplicação é Restfull usando spring boot, no nosso back, e front é Angular, estou usando o JpaRepository, quando uso o findAll do JpaRepository funciona, mas quando tento usar com @query ele não sobe o projeto de maneira nenhuma, não é possível que eu só consiga paginar com os métodos pré definidos do JpaRepository, tenho Joins enormes entre muitas tabelas que só fazendo com query dinamica mesmo, por enquanto estou buscando informações na internet mas ainda não achei uma saída.
Answer: Tente o seguinte:
@Query(value = "SELECT * FROM VRS.TB_CLIE_INVS /*#pageable*/",
       countQuery = "SELECT count(*) FROM VRS.TB_CLIE_INVS",
       nativeQuery = true)
Page<TbClieInv> testePaginacaoQuery(Pageable pageable);

Fonte: SOen - Spring Data and Native Query with pagination
Comment: código correto executou com sucesso, Obrigado.
Answer: Funcionou !!!!!
funcionou exatamente desse jeito sem o order by, o intuito era simular o Page findAll do PagingAndSorting e o teste foi executado com sucesso, eu já tinha tentado fazer com o count assim como vc me passou mas a chave dos meus problemas realmente era colocar esse comando /*#pageable*/", acho que agora com esse vou conseguir converter as outras querys dinamicas que tenho para com paginação, muito obrigado pela ajuda, segue o código que funcionou:
@Query(value = "SELECT * FROM tabela1 /*#pageable*/",
       countQuery = "SELECT count(*) FROM tabela1",
       nativeQuery = true)
Page<ClassemodeloTabela> testePaginacaoQuery(Pageable pageable);

Comment: Oi Leandro, formatei seu código e atualizei minha resposta para refletir seu comentário. Você pode marcar uma resposta como correta aceitando-a e indicar que ela é boa com um voto positivo ([faça um tour](https://pt.stackoverflow.com/tour)) .  Você realmente quis escrever uma resposta para sua pergunta ou sua intenção era apenas comentar na minha resposta? No segundo caso você poderá fazer isso assim que tiver 50 pontos de reputação, devido ao formato do site respostas e comentários são coisas distintas e não devemos usar respostas para comentar.
Comment: não consegui colar como resposta porque deu comprimento excedido, apesar de ler faz anos, mas é a primeira vez que posto aqui, tentei responder seu comentário mas tem limitações de linhas, por isso coloquei abaixo para visualização geral, muito obrigado.
Comment: Que bom que você quebrou a barreira do primeiro post ;). Vou sinalizar para os moderadores para ver se eles conseguem converter a resposta em comentário (como o código ficou o mesmo acho que o resto cabe).
Comment: eu estou com grandes duvidas de sintaxe para fazer esse select com paginação e usando o join, isso é possível ?


Abrir links de iframe

Question: Como fazer para impedir que links de Iframe que tem a propriedade target="_blank" e abrir esses links em outro iframe só que na mesma página?
Exemplo:
Conteúdo do iframe1: MEU VIDEO
Conteúdo do iframe2: Aqui será o link que foi clicado no iframe1
Comment: Esses iFrames estão no mesmo domínio?
Answer: Seria isso... (mas terá de trocar http://www.google.com.br pois ele não vai carregar dentro do IFrame.). Consegui achar uma URL do Google que funciona dentro do IFrame, só pra você poder conferir o funcionamento.

$("body").on("click", "a", function(e) {
  var target = $(this).attr("target");
  if (target == '_blank') {
    $("#meuIFrame").attr('src', $(this).attr("href"));
    return false;
  }
});

  Google

Tal como a URL http://google.com.br não funciona, qualquer URL cuja resposta contenha o cabeçalho X-Frame-Options: SAMEORIGIN também não vai abrir dentro do IFrame, a não ser que você tenha como colocar este HTML na mesma origem da URL.
Comment: Nesse caso é só substituir o `"body"` por outro seletor que envolva apenas o widget desejado.
Comment: sim os 2 iframe estão localizados na mesma página
Comment: tipo eu estou utilizando isso para executar um widget do twitter mas quando é clicado para Retwittar ou Curtir ele abre em nova página. por isso quero ter esses 2 iframe 1 com o twitter do usuário e o outro para abrir quando clicar nos links da postagem!
Comment: Mas só uma coisa... eu não quero abrir todos os links de target="_blank" não, apenas o que estão dentro do widget do twitter.

Abrir links de iframe


Is it possible to run mpiexec as a subprocess of an mpiexec'd program?

Question: I have a program A that is run with mpiexec. This program normally runs in parallel, but in one of the execution patterns, it may invoke an external program B which is also MPI enabled. Of course, every instance of A will end up invoking one instance of B if B is invoked as serial. If invoked parallel, every instance of A will invoke a parallel B.
One possible solution is to have A's master node invoke a parallel B, leaving the other A's processes idle, but of course I need to carry over the mpiexec options from the user command line invocation of A (including any quirks, e.g. PBS interaction) to the inner B invocation. This is as far as I see not trivial, nor dealing nicely with different MPI implementation (e.g. MPICH vs Open MPI).
Does anyone have experience with this kind of problem?
Comment: Take a look at the [Process Creation and Management](http://www.mpi-forum.org/docs/mpi22-report/node201.htm#Node201) chapter of the MPI standard. There are some examples floating around Stack Overflow - just search for `MPI_Comm_spawn`.
Comment: How about the built-in process management in MPI? The nice thing is that the DRM interaction is automatically being taken care of since the initial A's `mpiexec` is the one that would spawn the processes in B.
Comment: @HristoIliev: You might be on to something, could you detail a bit more about process management in MPI?
Comment: @HristoIliev: this is _really_ interesting. Thanks. I will explore it further and if it works, please come back to add it as an answer. I'll keep you posted.
Answer: This is not possible without using the dynamic process management of MPI 2 and above. You should use MPI call like MPI_COMM_SPAWN or MPI_COMM_SPAWN_MULTIPLE to create new MPI processes. If you use a fork and exec or similar function to start a new mpiexec command, your new process will not be MPI enable and any call to MPI_Init will fails.
I have experiences that in a project.
Answer: It doesn't matter if it is possible - it is a terrible idea.  Use MPI_Comm_spawn(_multiple) as noted by others.
And I believe there are at least some networks where this usage will not work and thus it is not portable.
Comment: Terrible or not, that's what I need to provide. Besides, I don't see a lot of alternative when you have to run a script that spawns a process.


How important are the following undergrad courses when trying to pursue studies in chaos theory/dynamical systems?

Question: I'm currently a physics major with a year left, and deciding whether to switch into  mathematical physics, mathematics or applied mathematics. I'm definitely switching into one of them, as I can meet the requirements for either in my last year and all of them align better with my interests. Speaking of which, at this point I think I want to pursue graduate studies related to chaos theory/dynamical systems. I did a bit of searching and this seems a field that is sometimes studied from an (applied) mathematical viewpoint and at other times from a physics viewpoint.
Not knowing how exactly I want to go about studying this, i.e. I don't know which side interests me more, since it kind of depends on the actual topic, not the approach per se, here's where my quandary comes in. Namely, which of the following courses do you find most crucial if I do indeed want to study the aforementioned fields:
A) Quantum mechanics II, Electromagnetism II [mathematics/math physics/applied maths]
B) Continuum mechanics [mathematics]
C) Intermediate PDE's (a second course in PDE's), Numerical methods [applied maths]
I've grouped this into three separate categories, because if I go with either of the courses in C), then I can't take B). In square brackets I've also listed the programs that I'd need to be in if I want to fit them into my schedule next term.
Personally, I'd like to get a taste of continuum mechanics, but then I definitely can't take either of the courses in C, and what worries me is that those would perhaps be required if I was to approach the subject from a mathematical standpoint in grad school. On the other hand, I'm not sure I could get away with not taking the courses in A if wanted to approach things from the physics side.
What would you recommend?
Answer: I am physicist. My advice: 1) forget about A, it will make you hit your head against a wall to try understand it but will not learn anything about chaos. 2)it would not be a waste of time, but it depends on what specific topics it touches. 3) the most important if you wanna learn chaos seriously.Chaos is a mathematical issue, even if there are many physicists working on the subject (because the equations they use in many branches happen to be chaotic). But you can always learn some basic physics if you need or feel about it. for instance analytical mechanics and then a specific course on chaos theory (but you will be able to do it by yourself once you are good a PDE's.
Comment: Thanks for the reply. The reason I was considering A is that, for example, at my university, I've been told that those courses would probably be required for entry into Physics grad school. So I'm worried that if I apply to approach chaos and dynamical systems from a physics standpoint, I could be rejected for not having those courses. I have done both first parts of the respective QM and EM sequences, though. As for C, I wasn't sure whether more advanced PDE knowledge is as needed in those areas as ODEs, or whether it would be enough to have the basics down.
Comment: well, that changes the picture around, if you plan to enter physics grad school, you should take not only both A) and B), but make sure to ask your adviser or mentor what else. Grad school in physics requires to learn a lot of phyiscs! Your will learn enough about PDE's in advanced course of physics, at least enough of what you need if you decide to work in chaos theory.
Comment: Well, like I said, that's the problem. I don't know which approach I'll take, since looking online sometimes the research groups are located in the Physics department, and sometimes in the Maths department. So I need to choose an option that will give me the most leeway in making my decision in the coming months. Unfortunately, I namely only have a couple of days to make the decision about which program to switch to, though. Any further input during with these constraints?
Comment: it is a difficult question, and the key to the answer is only you. My advice. If you love physics, go for the physics phD in chaos dynamics. If you dont really love it, go for math, you will learn much more and you will always find a place in a physics group studying chaos, because you will know more math than them, and they are aware of that! ;)
Comment: Hmm, I see. So do you think the focus should be on whether I want to get a PhD in Maths or in Physics, not on the department in which groups dealing with chaos and dynamical systems are located?


Mocking in jest and express

Question: I have some code in an Express route which talks to AWS Cognito and am having trouble working out how to mock it in tests.
cognitoExpress.validate(accessTokenFromClient, (err, response) => {
    if (err) return res.status(401).json({ error: err });

    res.json({ data: `Hello ${response.username}!` });
  });

Then in my test I want to say cognitoExpress.validate should be called once and return {username: 'test user'} so that it doesnt hit the network and doesnt actually call AWS Cognito
it('It should returns 200 with a valid token', async done => {
    const { cognitoExpress } = require('../helpers/cognitoExpress');

    // I have tried
    jest.mock('../helpers/cognitoExpress');
    // and this
    jest.mock('../helpers/cognitoExpress', () => ({
      validate: jest.fn()
    }));

    const token = 'sfsfdsfsdfsd';
    const response = await request.get('/').set('Authorization', token);
    expect(cognitoExpress.validate).toHaveBeenCalledWith(token);
    expect(response.body).toEqual({ data: 'Hello test user' });
    done();
  });

Thanks in advance....
Answer: Create file ../helpers/__mocks__/cognitoExpress.js with mocked function you want to use. It is essential to call the folder __mocks__. You can modify a functions and return any data you want.
example
module.exports = {
    validate: () => { username: 'test user' }
}

Now you can use jest.mock('../helpers/cognitoExpress'), but I recommend you to place it to some global or test setup file, not to separate tests.
Jest Manual Mocks
Answer: let spyInstance = undefined;

beforeAll(() => {
  spyInstance = jest.spyOn(cognitoExpress.prototype, "validate").mockImplementation(() => {
    // Replace the body of 'validate' here, ensure it sets
    // response body to {username: 'test user'} without calling AWS
    ...
  });
});

afterAll(() => {
  expect(spyInstance).toBeDefined();
  expect(spyInstance).toHaveBeenCalledTimes(1);
  jest.restoreAllMocks();
});

it("It should call mocked cognitoExpress.validate once", async done => {
 ...
});

A similar and working test in my project. Instead of cognitoExpress.validate it mocks and tests SampleModel.getData

Mocking in jest and express


Interchange of sum and limit in sequence algebra

Question: As you may know,
Let $ {a_n} $ and $ {b_n} $ be convergent sequence with limit L, M respectively, then the following is true
$ \lim_{n\to \infty} (a_n + b_n) = \lim_{n\to \infty} a_n +\lim_{n\to \infty} b_n = L+M$
In the following case
$$ \lim \frac{\sum_{k=1}^n k}{n^2} = \lim \frac{\frac{n(n+1)}{2}}{n^2} = \frac{1}{2}$$
is true but
$$ \lim \frac{\sum_{k=1}^n k}{n^2} = \lim \frac{1+2+3+\cdots+n}{n^2} = \lim \frac{1}{n^2} + \lim \frac{2}{n^2} + \cdots + \lim \frac{n}{n^2} = 0$$
I think this cannot be true because there are infinitely many terms.
However, let $ \alpha $ be a positive real number, then the following can be true
$$ \lim \frac{\sum_{k=1}^n k}{n^{2+\alpha}} = \lim \frac{1+2+3+\cdots+n}{n^{2+\alpha}} = \lim \frac{1}{n^{2+\alpha}} + \lim \frac{2}{n^{2+\alpha}} + \cdots + \lim \frac{n}{n^{2+\alpha}} = 0$$
Does this have something to do with uniform convergence?
When can I interchange the limit and sigma in limit operation of sequence?
Answer: $\lim_{n\to\infty}\frac1{n^2}(1+2+\cdots+n)\ne\lim\frac1{n^2}+\cdots+\lim\frac n{n^2}$ becuase the number of terms varies with $n$.  You can only apply the sum of limits rule, if the number of terms stay fixed.
Answer: You can't interchange limit and sum when evaluating a limit of the form $\lim_{n\to \infty} \sum_{k = 1}^n a_k$ because the number of summands of the sequence $\sum_{k = 1}^n a_k$ depends on $n$.
Comment: @AllenJung your first example already proves that.
Comment: Can you show me mathematically why the interchange is not true if the number of summands depends on n?
Answer: Unfortunately what you've written makes little sense. You aren't actually interchanging sum and limit but rather somewhat abusing the definition of a sum as Tim implies in his answer.
However, you can in some sense make an interpretation of what you've written as a Riemann integral. For example:
$$\frac{1}{2}=\int_0^1 xdx = \lim_{n\rightarrow\infty} \frac{1}{n} \sum_{k=1}^n k/n,$$
which comes from:
$$\int_0^1 f(x)dx = \lim_{n\rightarrow\infty} \sum_{k=1}^n f(k/n)\frac{1}{n}.$$
So if you squint, your interpretation is basically that of a Riemann Integral: you are adding up pieces of height $f(k/n)$ and width $1/n$ which have smaller and smaller area. It is wrong to say that you can pass the limit to each term because then you really do get zero, which is wrong.


PROC LOGISTIC, GENMOD

Question: How can I get the standard error for odds ratios.
I know how to obtain ORs, either by exp the estimate or by using LSMEANS or SLICE statements.
Unfortunately wasnt able to find the options to have SE for ORs in the output datasets. Only the estimate SE is in there.
Answer: You can use the delta method for that. Lets call the raw coefficient or log odds ratio, $\beta$, and its standard error $se_\beta$, the odds ratio $OR$, and its standard error $se_{OR}$.
You have $\beta$ and $se_\beta$ and you know that $OR=\exp(\beta)$.
Per the delta method, $se_{OR} \approx \frac{\partial \exp(\beta)}{\partial \beta}se_\beta = \exp(\beta)se_\beta$
Comment: Thank you so much! This did it. Same approach for rel risk out of GENMOD with poisson and bin

PROC LOGISTIC, GENMOD


How to secure ajax on automatically save field data

Question: My custom module make ajax request on _node_edit form after input changed by user, and menu callback save this field's data (only this field, not all node object). Menu item is as "field-save/NID/FIELD_NAME", and the input val is on $_POST['field_data']. Menu item have own permission per role. Additionally each user have own randomly generated secure string and it is on the same ajax $_POST['secure_phrase']. Menu callback checking for permission per role and this secure string per user, if have access going forward to save field data.
The question: Is this enough security to prevent for unauthorised access to save field through this ajax request?
edit:
additional for this on "access_callback" i have
<code>if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  // checking permissions here
}
</code>
Answer: It seems pretty secure. Also check the content type permissions for the role ;)
Comment: Good suggestion, I will, tnx @rteijeiro
Comment: Can you expand a little on why you this is secure?
Comment: The ajax callback is triggered with the menu item: field-save/NID/FIELD_NAME that is secured with Drupal permissions. Only a privileged user could update the field. You should also could send the field value to the javascript through Drupal settings instead $_POST. And also add some validation in the javascript.


How to send mail using php by insert html into mail content?

Question: How to send mail using php by insert html into mail content ?
I tried to insert html code inner $message, When i test it's show error
like this  Parse error: syntax error, unexpected 'margin' (T_STRING)
How can i do ?
<?PHP
include("connect.php");
$email = "email@example.com";
$to = $email;
$subject = "test subject";
$message = "

                test text

";

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: EXAMPLE <firstname.lastname@example.com>' . "\r\n";
$headers .= 'Return-Path: email@example.com' . "\r\n";
mail($to, $subject, $message, $headers, '-firstname.lastname@example.com');
?>

Comment: surround your message with single quote instead of double quote
Comment: or escape them `\"string\"` - it's longer, but is valid syntax.
Comment: The answer given below, answers the question  for what you posted and should be marked as correct.
Answer: You have an issue with wrapping your $body string try this
 <?PHP
    include("connect.php");
    $email = "email@example.com";
    $to = $email;
    $subject = "test subject";
    $message = "

                    test text

    ";

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: EXAMPLE <email@example.com>' . "\r\n";
    $headers .= 'Return-Path: firstname.lastname@example.com' . "\r\n";
    mail($to, $subject, $message, $headers, '-firstname.lastname@example.com');
    ?>

Comment: I don't know why you went through all that trouble to change the quotes. All that was needed to do was `$message = '` and `';` - *Done like dinner* ;-)
Comment: Oh well, it has been done ;-) *Cheers*
Comment: @Fred-ii- hahahahahaha indeed didn't notice!!!! +1


Learning hat matrix

Question: I'm stuck learning the hat matrix and wondered if someone could help with a question.  If I have the model
$$Y_i =\beta_0+\beta_1X_i+\epsilon_i,i = 1,2,3 \dots n,$$ how can I calculate the hat matrix as
$$H = X(X^\prime X)^{-1}X^\prime$$
And what would the $h_{ij}$ element be?
Comment: Assuming that your linear model is `mod` in R you would write something like: `X = model.matrix(mod); (H = (X %*% solve(crossprod(X)) %*% t(X)))`. See this thread [here](http://stats.stackexchange.com/questions/125886/linear-model-trace-of-the-hat-matrix-in-r) for more details.
Comment: It's unclear what you are asking.  You can calculate the hat matrix using the formula that you wrote down.  Is there something particular about that formula that you do not understand?
Comment: https://en.wikipedia.org/wiki/Hat_matrix  This looks like it will help.
Comment: (1) That minus sign does not belong in the expression for $H$.  I guess you intend it to mean an inverse or a pseudo-inverse of the quantity within parentheses.  (2) Are you perhaps asking what a matrix is and how to multiply matrices?
Comment: Sorry I have corrected it now. Yes please whatever is relevant to solve the question
Comment: do you know what element hij would be ?
Comment: The notation $h_{ij}$ denotes the entry in the matrix $H$ in the $i$th column and $j$th row.  The matrix $H$ can be calculated as in your formula.  Are you looking for a formula that gives $h_{ij}$ directly?  Any such formula would just be expanding the matrix formula you gave, and would not be particularly useful or insightful.
Comment: what would that formula be ?
Comment: Using the definition of matrix multiplication, $h_{ij} = \sum_{km} x_{ik} z_{km} x_{jm}$, where $Z = (X' X)^{-1}$.  There is no legitimate closed form for $z_{km}$ without giving in to absurdity.
Comment: I have merged the two copies of your question so that all comments appear in one place. If you would like to make changes to this question, then please edit it rather than creating a new one.
Answer: We can write the model in matrix notation as
$$ Y = \beta X + \epsilon $$
The OLS solution for the vector of regression coefficients $\beta$ is:
$$ \hat{\beta} = (X'X)^{-1} X'Y $$
The hat matrix is the projection matrix that maps the response vector $Y$ to the vector of fitted values $\hat{Y}$ (hence the name "hat" matrix). That is:
$$ \hat{Y} = HY$$
Now, since
$$ \hat{Y} = X \hat{\beta} = X(X'X)^{-1} X'Y $$
it immediately follows that
$$ H =  X(X'X)^{-1} X'$$
as required.

Learning hat matrix


Knockout effect/clip path for multiple objects using CSS

Question: I was wondering how you would achieve a clip path effect/knockout effect in css for multiple divs (created as rectangles) for a single background image (similar to clipping paths in photoshop?)
Is there a CSS tool for this? I have included a sample image within code.

example: https://i.stack.imgur.com/ZuJvK.jpg
codepen: https://codepen.io/lucasenz/pen/MWYaZBJ
HTML:
<!DOCTYPE html>

    <meta charset="utf-8">
    <title>HELLO.</title>
    <link href="https://fonts.googleapis.com/css?family=Lato:300,400,700,900&display=swap" rel="stylesheet">

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

HELLO

BOX ONE

BOX TWO

BOX THREE

4 of 6

5 of 6

6 of 6

CSS:
*{
  font-family: lato;
}
body{
  height: 100%;
}

h1{
  font-weight:900;
  letter-spacing: 0.2em;
  text-align: center;
  font-size: 4vh;

  margin-top: 4vh;
}

.content{
  width: 50%;
  height: 80vh;
align-items: center;
margin: 0 auto;
display: flex;
align-items: center;
justify-content: center;

}

.rectangle{
  height: 18vh;
  width: 15vw;
  margin: 1.5vh 0.7vw;
  text-align: center;
  background-color: tomato;
  display: flex;
  align-items: center;
  justify-content: center;
}

Answer: I would go for a simple solution. Create a container with your image as background and draw separator lines by creating transparent divs inside your container with white border.

.cont{
  display: flex;
  flex-wrap: wrap;
  width: 560px;
  height: 300px;
  background-image: url("https://cnet4.cbsistatic.com/img/pI-Oq4fGqthDVMMMuyL2ZMnaC5I=/2019/11/01/1e902743-2ee4-4c22-9b66-0b396596b13e/20190701-154228.jpg");
  background-size: cover;
}

.box{
  flex-basis: 28%;
  flex-grow: 1;
  flex-shrink: 1;
  height: 46%;
  border: 10px solid white;
}


psql asking for user password and even tho i provide correct password it returns error

Question: I am taking CS50 Web Programming course and after installing PostgreSQL i had issues with setting path for psql, after overcoming that i tried to run psql and i am being asked for password which i figured is the one i set when installing PostgreSQL. I was looking for an answer for a long time now and haven't found anything that worked. I am using Windows 10 and PostgreSQL 12.
Command line paste after trying to run it in admin mode:

C:\WINDOWS\system32>psql
  Password for user Korisnik:
  psql: error: could not connect to server: FATAL:  password authentication failed for user "Korisnik"
Comment: Thank you very much kind sir. It did work.
Comment: The default user for a postgres installation is usually 'postgres' - I'd suggest trying that user with the password you set at installation.
Comment: How do i do that ?
Comment: When you open up psql, should be prompted for a username, enter 'postgres'. If you're not then running psql using "psql -U postgres" should do it
Answer: Answering so this can be marked as answered:
Default username for PostgreSQL installations is typically postgres.
Therefore when prompted to enter a username enter 'postgres' or if there is no prompt to enter a username run psql using the following command to specify the username.
<code>psql -U postgres
</code>


Magma say the size of my group is $0$

Question: I am trying to define a group in terms of generators and relations in Magma and check its size, but Magma says the size of my group is $0$. The same code works for smaller presentaions of groups. What am I doing wrong?
Here's the code:
F<x000_0,x000_1,x000_2,x001_0,x001_1,x001_2,x010_0,x010_1,x010_2,x011_0,x011_1,x011_2,x100_0,x100_1,x100_2,x101_0,x101_1,x101_2,x110_0,x110_1,x110_2,x111_0,x111_1,x111_2> := FreeGroup(24);
G<x000_0,x000_1,x000_2,x001_0,x001_1,x001_2,x010_0,x010_1,x010_2,x011_0,x011_1,x011_2,x100_0,x100_1,x100_2,x101_0,x101_1,x101_2,x110_0,x110_1,x110_2,x111_0,x111_1,x111_2>, phi := quo<F | x000_0=x100_0,x000_1=x010_1,x000_2=x001_2,x001_0=x101_0,x001_1=x011_1,x001_2=x000_2,x010_0=x110_0,x010_1=x000_1,x010_2=x011_2,x011_0=x111_0,x011_1=x001_1,x011_2=x010_2,x100_0=x000_0,x100_1=x110_1,x100_2=x101_2,x101_0=x001_0,x101_1=x111_1,x101_2=x100_2,x110_0=x010_0,x110_1=x100_1,x110_2=x111_2,x111_0=x011_0,x111_1=x101_1,x111_2=x110_2,x000_0 * x000_1^-1 = x000_1 * x000_2^-1,x000_1 * x000_2^-1 = x000_2 * x000_0^-1,x000_2 * x000_0^-1 = x001_0 * x001_1^-1,x001_0 * x001_1^-1 = x001_1 * x001_2^-1,x001_1 * x001_2^-1 = x001_2 * x001_0^-1,x001_2 * x001_0^-1 = x010_0 * x010_1^-1,x010_0 * x010_1^-1 = x010_1 * x010_2^-1,x010_1 * x010_2^-1 = x010_2 * x010_0^-1,x010_2 * x010_0^-1 = x011_0 * x011_1^-1,x011_0 * x011_1^-1 = x011_1 * x011_2^-1,x011_1 * x011_2^-1 = x011_2 * x011_0^-1,x011_2 * x011_0^-1 = x100_0 * x100_1^-1,x100_0 * x100_1^-1 = x100_1 * x100_2^-1,x100_1 * x100_2^-1 = x100_2 * x100_0^-1,x100_2 * x100_0^-1 = x101_0 * x101_1^-1,x101_0 * x101_1^-1 = x101_1 * x101_2^-1,x101_1 * x101_2^-1 = x101_2 * x101_0^-1,x101_2 * x101_0^-1 = x110_0 * x110_1^-1,x110_0 * x110_1^-1 = x110_1 * x110_2^-1,x110_1 * x110_2^-1 = x110_2 * x110_0^-1,x110_2 * x110_0^-1 = x111_0 * x111_1^-1,x111_0 * x111_1^-1 = x111_1 * x111_2^-1,x111_1 * x111_2^-1 = x111_2 * x111_0^-1,x000_0 * x000_2^-1 = x000_1 * x000_0^-1,x000_1 * x000_0^-1 = x000_2 * x000_1^-1,x000_2 * x000_1^-1 = x001_0 * x001_2^-1,x001_0 * x001_2^-1 = x001_1 * x001_0^-1,x001_1 * x001_0^-1 = x001_2 * x001_1^-1,x001_2 * x001_1^-1 = x010_0 * x010_2^-1,x010_0 * x010_2^-1 = x010_1 * x010_0^-1,x010_1 * x010_0^-1 = x010_2 * x010_1^-1,x010_2 * x010_1^-1 = x011_0 * x011_2^-1,x011_0 * x011_2^-1 = x011_1 * x011_0^-1,x011_1 * x011_0^-1 = x011_2 * x011_1^-1,x011_2 * x011_1^-1 = x100_0 * x100_2^-1,x100_0 * x100_2^-1 = x100_1 * x100_0^-1,x100_1 * x100_0^-1 = x100_2 * x100_1^-1,x100_2 * x100_1^-1 = x101_0 * x101_2^-1,x101_0 * x101_2^-1 = x101_1 * x101_0^-1,x101_1 * x101_0^-1 = x101_2 * x101_1^-1,x101_2 * x101_1^-1 = x110_0 * x110_2^-1,x110_0 * x110_2^-1 = x110_1 * x110_0^-1,x110_1 * x110_0^-1 = x110_2 * x110_1^-1,x110_2 * x110_1^-1 = x111_0 * x111_2^-1,x111_0 * x111_2^-1 = x111_1 * x111_0^-1,x111_1 * x111_0^-1 = x111_2 * x111_1^-1>;

#G;

Explanation of these generators and relations:
As a set, the generators can be identified with $\mathbb{Z}_2^3\times\mathbb{Z}_3$.
The relations are of two types:
The first type:
For each $i\in\mathbb{Z}_3$, the generators $(a,b,c,i)$ and $(d,f,e,i)$ are the same group element even if they differ in the $i$-th coordinate (counting from $0$). This is expressed by relations like x000_2=x001_2.
The second type:
For each $i\in\mathbb{Z}_3\setminus\{0\}$, I want to say that the ratio between $(a,b,c,j)$ and $(a,b,c,i+j)$ does not depend on $(a,b,c,j)$, but only on $i$.
This is expressed by relations like x100_1 * x100_0^-1 = x100_2 * x100_1^-1.
EDIT: If I kill one generator, for example, $x000_0$, then Magma says I get a group of order $3$.
Comment: @Alex If you're interested in _how_ the generators give $A_4$, I'm not sure that Magma is going to be a great help to you.  The fact that you write 24 generators but know that you actually have 12 is already obscuring much of the structure - I would briskly encourage parsing your relations quite a bit 'by hand' before you hand them over.
Comment: I'd also appreciate if anyone can check if it happens when you run my code on your system. I'm using the [Online Magma Calculator](http://magma.maths.usyd.edu.au/calc/).
Comment: Are you looking for a free group (s)?
Comment: @BabakS.: I'm looking for finite groups. I want to get $A_4$, and I want to check if I added enough relations. I know I can get $A_4$ by adding (possibly zero) relations to the ones specified there, and I would like to look at the multiplication table in terms of my generators.
Comment: @Alex It seems that $0=\infty$ for Magma (?) E.g. "G:=Group;
#G;" gives $0$ (your presentation has the same type of relations), whereas "G:=Group;
#G;" gives $2$.
Comment: Maybe I can give you $A_4$ and its generators and its elements in GAP. Moreover, nice to know $A_4=$
Comment: @O.L.: Maybe, but that's an odd behavior. It does know to write 'Infinity' sometimes.
Comment: @Alex I agree, I am puzzled as well.
Comment: @BabakS.: Thanks. I've just learned this presentation from the answer to my previous question today :) But I am interested in seeing how my generators give $A_4$. In a sense I'm studying those generators and not the group $A_4$.
Comment: @Alex: May I know what are that generators? I can't find them from above codes.:(
Comment: Yes, I will edit the post now to explain. It will take a few minutes.
Answer: The group you've given is still infinite, at least at first glance.  You've written the relations that specify the invariance of cross-ratios, but nothing that describes powers of individual generators — as far as I can see (and presumably, as far as Magma can see), even $x_{000,0}$ by itself is enough to give an infinite cyclic group because none of the relations you've provided suggest any means of reducing, e.g., $x_{000,0}^5$.
As I mentioned in a comment, if you're interested in how this (presumably 3d cubic of some nature) symmetry group actually generates $A_4$, I would strongly recommend using the knowledge you already have of the generators (e.g., the fact that there are only 12, not 24) to determine more relations among the group 'by hand' before you hand it over to the software, because Magma will likely have a hard time in explaining to you precisely how it derives its result, so all you'll have is the result itself.
Comment: Ok. Thanks. I will ask another, more specific question.
Comment: More specific question here: http://math.stackexchange.com/questions/418572/line-graph-of-cayley-graph-of-mathbbz-23-is-a-4
Answer: GAP confirms that the group is infinite. As @StevenStadnicki already mentioned, orders of generators are missing, and they can not be inferred from other relations. The GAP code to check this is
F:=FreeGroup("a","b","c","d","e","f","g","h","i","j","k","l",
"m","n","o","p","q","r","s","t","u","v","w","x");
AssignGeneratorVariables(F);
rels:="a=m,b=h,c=f,d=p,e=k,f=c,g=s,h=b,i=l,j=v,k=e,l=i,m=a,n=t,
o=r,p=d,q=w,r=o,s=g,t=n,u=x,v=j,w=q,x=u,a * b^-1 = b * c^-1,
b * c^-1 = c * a^-1,c * a^-1 = d * e^-1,d * e^-1 = e * f^-1,
e * f^-1 = f * d^-1,f * d^-1 = g * h^-1,g * h^-1 = h * i^-1,
h * i^-1 = i * g^-1,i * g^-1 = j * k^-1,j * k^-1 = k * l^-1,
k * l^-1 = l * j^-1,l * j^-1 = m * n^-1,m * n^-1 = n * o^-1,
n * o^-1 = o * m^-1,o * m^-1 = p * q^-1,p * q^-1 = q * r^-1,
q * r^-1 = r * p^-1,r * p^-1 = s * t^-1,s * t^-1 = t * u^-1,
t * u^-1 = u * s^-1,u * s^-1 = v * w^-1,v * w^-1 = w * x^-1,
w * x^-1 = x * v^-1,a * c^-1 = b * a^-1,b * a^-1 = c * b^-1,
c * b^-1 = d * f^-1,d * f^-1 = e * d^-1,e * d^-1 = f * e^-1,
f * e^-1 = g * i^-1,g * i^-1 = h * g^-1,h * g^-1 = i * h^-1,
i * h^-1 = j * l^-1,j * l^-1 = k * j^-1,k * j^-1 = l * k^-1,
l * k^-1 = m * o^-1,m * o^-1 = n * m^-1,n * m^-1 = o * n^-1,
o * n^-1 = p * r^-1,p * r^-1 = q * p^-1,q * p^-1 = r * q^-1,
r * q^-1 = s * u^-1,s * u^-1 = t * s^-1,t * s^-1 = u * t^-1,
u * t^-1 = v * x^-1,v * x^-1 = w * v^-1,w * v^-1 = x * w^-1";
r:=ParseRelators(GeneratorsOfGroup(F),rels);
G:=F/r;
Size(G);

and I am posting it here as an answer, since I can not fit it into a comment. Actually, the code above is not how I would normally create such a group - I've used ParseRelators only to simplify the task of converting your Magma code into GAP, and ParseRelators requires one-letter generator names. Otherwise, there would be no problem to support names of generators like in your example:
gap> F:=FreeGroup("x000_0","x000_1");
<free group on the generators [ x000_0, x000_1 ]>
gap> AssignGeneratorVariables(F);
#I  Assigned the global variables [ x000_0, x000_1 ]
gap> G:=F/[x000_0^2,x000_1^2,Comm(x000_0,x000_1)];
<fp group on the generators [ x000_0, x000_1 ]>
gap> Size(G);
4

Magma say the size of my group is $0$


ODE and absolute value

Question:
$$y'=\frac{y}{x}$$

Using separation of variables we get $\frac{dy}{y}=\frac{dx}{x}$ which is $ |y|=|x|+c$ can we drop the absoulte value?
Comment: You should get $|y|=A|x|$ for some constant $A$. So either $y=Ax$ or $y=-Ax$ and both satisfy the original DE
Answer: I'm ignoring domains of definition in the following. You rather get
$$\ln|y|=\ln|x|+k$$
so that
$$|y|=C|x|$$
where $C=e^k>0$. This gives
$$y=\pm Cx, C>0$$
which can be rewritten as
$$y=Cx, C\not=0$$
Since the function $y=0$ is trivially a solution, we actually may take $C$ to be any real number.

ODE and absolute value


Best way of having Pathauto based off node title, but not tied to it?

Question: Sorry for the poorly worded question, I'll try to better explain myself.
I'm swapping (not migrating) from WP to Drupal and I'm trying to replicate WP's URL feature. I want it to initially be based off the title of the node ("My First Blog Post" => "my-first-blog-post")
What I'm not wanting is Pathauto to be directly linked to the title, so I can update the title to something like "My First Blog Post (Updated 5/4/2017)" without having to worry about the URL changing. I want the URL to be easily editable if the need rises, I just don't want it tied to the title.
Would the best way just be to add an extra field, and manually enter a 'URL title' in there?
Answer: The contributed module Pathauto will provide you a setting to set your own alias urls in the add content page.
Suppose you are adding a content of type basic page (using node/add/page), at the bottom there will be an url path settings tab where you can uncheck the 'Generate automatic URL alias' option and set your own path alias for the page.
Comment: Not true @p3tch, that's exactly the functionality you've described as your requirement: set a pattern for a node type, create a node, that node now has an automatic initial alias based on the title. Then untick the "generate automatically" option, save the node, and pathauto will no longer affect it. That satisfies 1: _I want it to initially be based off the title of the node_, 2: _I can update the title to something like "My First Blog Post (Updated 5/4/2017)" without having to worry about the URL changing_ and 3: _I want the URL to be easily editable if the need rises_.
Comment: I still want it automated for the most part becausse I want the content type to be automatic. I'm currently using `[node:content-type:name]/[node:title]`
Comment: You can do it by adding the pattern in the url aliases settings for the contents which is available in **admin/config/search/path/patterns**.
Comment: Yes, that's where I've done it. But if I follow your answer and uncheck the automatic URL alias, I won't get the functionality I want anymore
Comment: Oh, *then* uncheck it - I'm following now. Thanks!
Comment: Actually I think there's also a global pathauto setting to stop subsequent saves from updating the URL automatically, so you might be able to automate the whole process. Have a look through the pathauto settings pages, I think you'll find it in there @p3tch


Data Structure - Avoid Duplicate in List and Map

Question: I have a million Employee Objects in a List. The Employee Objects get modified and also sorted many times while processing. Also, while processing, there is a need to fetch Employee objects by Department. Means, I have to maintain a Map with Department as Key and List as value.
While processing, the RAM shoots up to 100+ GB. Whereas, the Employess per se on a file is about 2 GB.
List is the master, while Map is there for convenience (to fetch by Department).
Now, my question is: How can I avoid duplication of Employee objects in List & Map? Whenever a getByDept is called, if I loop through List it's expensive & time consuming.
I need a Map with List as  backing data. Any changes to the Employee object in List should reflect in the Map too.
Any ideas to build a data structure without duplicating Employee objects in Map?
Thanks in advance.
Comment: I'd say you need to elaborate a bit and show some code. Maps and lists only contain _references_ to objects that are stored on the heap so if you have a `Map` and `List` both structures can easily reference the same `Employee` instances and any changes to such an instance are "reflected" in both.
Comment: Also note that a 2GB file can result in much higher memory requirements when loaded into objects so it would be necessary to analyze that. If you load the entire file into _one_ list only and the heap usage is much lower than when you're using the map this could indicate you're creating multiple instances of the same employee - if so try to eliminate that. Additionally you might want to acquire and analyze a heap dump to see where the memory goes - maps can be quite costly if used wrong.
Comment: Firstly you should have a valid equals() and hashCode() method for the Emploee class. Given that you should check to use something different than just a List. For example SQLite. So you can enforce constraint and handle the data efficiently in memory.
Comment: You should have a reference on the Emploee about the department they belong, or you have a different source for this information? In this last case you're in trouble as you have to cover the case an Emploee cannot belong to multiple department.
Answer: Try to use a Set instead of a List:

A collection that contains no duplicate elements

Adding two times the same element in the set doesn't change the set.
Remember to define the methods equals (and hashcode) to use correctly a Set because they are used internally by the Set methods.
Comment: I'm not sure the OP means duplicate entries but rather avoiding that there are 2 instances - one in the list and one in the map. That's at least how I understand the question.
Comment: Yes go for set instead of list only maintains the unique data
Comment: When I "add, modify or delete" an Employee object to the List, I need it to be reflected in the Map also.
Answer: Add your list to a empty set, that will remove all the duplicate elements of your employee list. The you can again convert back the set to list like this. The following removes the duplicate elements from List using set. Then using the non duplicate employees, your search will become faster.
      Map<Integer, List> employeeMap = new HashMap<Integer, List>();
        Set<String> set = new HashSet<>();
        List<String> list = new ArrayList<>();
        list.add("Allen");
        list.add("Alder");
        list.add("Allen");
        set.addAll(list);
        //This removes duplicate
        List<String> employeeList = new ArrayList<>();
        employeeList.addAll(set);
        //Add to the Map
        employeeMap.put(1, employeeList);
        //the way to edit the list directly, but you cannot check for duplicate elemets here
        employeeMap.get(1).add("werner");
        // Instead of List use Set
        Map<Integer, Set> employeeMapUsingSet = new HashMap<Integer, Set>();
        Set<String> employeeSet = new TreeSet<>();
        List<String> employeeLists = new ArrayList<>();
        list.add("Allen");
        list.add("Alder");
        list.add("Allen");
        employeeSet.addAll(list);
        //Add to the set to Map
        employeeMapUsingSet.put(1, employeeSet);
        //the way to edit the list directly, but you cannot check for duplicate elemets here
        employeeMapUsingSet.get(1).add("werner");
        //the following Duplicate will not be allowed and Map also gets updated, so no need for reference variable
        employeeMapUsingSet.get(1).add("Alder");
        //Again add Nancy to the set added in map and print
        employeeSet.add("Nancy");
        //This will print Nancy with the result too, because we are stroing a copy of pointer in the map, so the changes get reflected
        System.out.println(employeeMapUsingSet.get(1));

Try this because we are storing a pointer for employee set in the map, any changes made in the employee set will reflect on the Map too. TreeSet has been used for sorting purpose. Untill you assign employeeSet again to new TreeSet, the address will be the same, so any changes made will reflect in the map too. Hope this helps
Comment: The problem is not about Employee objects in a List. The Employee objects are in a List. However, since I need to fetch Employee objects frequently (about 100 from a total of 1 million) by Department Id, I am forced to use a map with Department Id as Key and List as value.
Comment: Updated my answer, It may help you.
Comment: Thanks Venkat. I got you. Let me rephrase my question. I have a million Employee objects created. Each Employee gets added to the List. While Employees are being created, I also need to have Employees grouped by Department Id - so that, I can fetch list of employees efficiently. Now the contents of this grouped Map should always be in sync with the List. Should I go with a Listener on the List?
Comment: Again updated the code with some more explanation. In the bottom of the solution find explanation about map using the reference of the set.
Answer: "sorted many times" is possibly the source of the memory usage, depending on how you go about it.
e.g. List.sort creates a new array containing all list elements, each time you sort.
Answer:
How can I avoid duplication of Employee objects in List & Map?
  Whenever a getByDept is called, if I loop through List it's expensive
  & time consuming.

Maps and list contains references of the object in heap memory created by JVM. So, as soon as you add or remove some employee from list, since map is pointing to same list, you will get the updated list from map. Below is example to illustrate this fact:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class Demo {
    public static void main(String args[]) {
        HashMap<Integer, List<Employee>> hashMap = new HashMap<>();
        List<Employee> empList1 = new ArrayList<Employee>();
        empList1.add(new Employee(3,"c",13));
        empList1.add(new Employee(2, "b", 12));
        empList1.add(new Employee(1, "a", 11));

        List<Employee> empList2 = new ArrayList<Employee>();
        empList2.add(new Employee(6,"f",16));
        empList2.add(new Employee(5, "e", 15));
        empList2.add(new Employee(4, "d", 14));

        hashMap.put(101, empList1);
        hashMap.put(102, empList2);

        System.out.println("Before::::::::::");
        hashMap.forEach((x, y)-> System.out.println(x + " " + y));

        List<Employee> list = hashMap.get(101);
        list.add(new Employee(10, "z", 18));

        System.out.println("After::::::::::");
        hashMap.forEach((x, y)-> System.out.println(x + " " + y));
    }
}

class Employee {
    int id;
    String name;
    int age;
    public Employee(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return id + " : " + name + " : " + age;
    }
}

Any ideas to build a data structure without duplicating Employee
  objects in Map?

Since you need to remove duplicates and also sort the list multiple times, better approach would be to use TreeSet.
Advantage of using TreeSet is that, you will get distinct employees and also you will have employees in sorted order.
Answer: sorted many times while processing
You haven't mentioned based on what condition you are sorting. To solve your problem you can sort based on department, so that all the objects having same department are grouped together.
To fetch list of objects based on department, you can use binary search to find the start and end index of the department range. time complexity O(log(n)). This way you don't need to maintain map and makes the logic simpler.
class HelloWorld {
    public static void main(String[] args) {
        search(List.of(1, 2, 4, 4, 4, 5, 5, 5, 5, 7, 8), 4);
    }

    private static void search(List<Integer> list, int searchKey) {
        int left = 0, right = list.size() - 1;
        int firstOccurrence = findFirstOccurrence(list, left, right, searchKey);
        int lastOccurrence = findLastOccurrence(list, left, right, searchKey);
        System.out.println("left: " + firstOccurrence + " right: " + lastOccurrence);
    }

    private static int findFirstOccurrence(List<Integer> list, int left, int right, int searchKey) {
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (list.get(mid) < searchKey) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return left;
    }

    private static int findLastOccurrence(List<Integer> list, int left, int right, int searchKey) {
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (list.get(mid) <= searchKey) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return right;
    }

When adding any object or updating department for an object you need to re-sort using https://stackoverflow.com/a/4903642/9160306 which takes O(log(n)) time and O(1) space.
Overall time complexity add/remove/update - O(log(n))
Overall Space complexity add/remove/update - O(1)


EWS Powershell Exchange 2013 FindFolders returns 0 results

Question: I am currently trying to make this script run on exchange 2013 to convert folder types from IPF.IMAP to IPF.NOTE as the folders are not showing on mobile devices after being imported from Imap. This script returns 0 results after running and multiple Doesnt Exist. If I output the folder names they are coming through, so i am not sure why the FindFolders is not returning any results.
I tried turning on impersonation (commented out here) but get an error saying I do not have permissions to impersonate even though I am logged in as administrator and running on powershell as admin. I am not sure if this is even necessary as the script works fine and returns the folder names for both $mbxfolder.Name and $SfSearchFilter, but only until it hits the FindFolders line, then the TotalCount is always 0.
<code>Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"
$exchService = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$exchService.UseDefaultCredentials = $true
$exchService.AutodiscoverUrl('firstname.lastname@example.com', {$true})

$MBXID = "email@example.com" #Define mailboxID
foreach ($MailboxIdentity in $MBXID) {
    Write-Host "Searching for $MailboxIdentity"
    $mailbox = (Get-Mailbox -Identity $MailboxIdentity)
    $MailboxName = (Get-Mailbox -Identity $MailboxIdentity).PrimarySmtpAddress.ToString()
    $MailboxRootid = new-object  Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$MailboxName) #MsgFolderRoot selection and creation of new root
    $MailboxRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchService,$MailboxRootid)
    #$exchService.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId -ArgumentList ([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress),$MailboxName #Define impersonation
    $folderid = $MailboxRootid
    $f1 = $MailboxRoot
    $fold = get-mailboxfolderstatistics $MailboxIdentity #Getting complete list of selected mailbox

foreach ($mbxfolder in $fold){
        #Define Folder View Really only want to return one object
        $fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(100) #page size for displayed folders
        $fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep; #Search traversal selection Deep = recursively
        #Define a Search folder that is going to do a search based on the DisplayName of the folder
        $SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::Displayname,$MBXFolder.name) #for each folder in mailbox define search
        $findFolderResults = $MailboxRoot.FindFolders($SfSearchFilter,$fvFolderView) #for each folder in mailbox define folder view (this is online task for store.exe) and perform search

        if ($findFolderResults.TotalCount -eq 0){ "Folder Doesn't Exist"  } #Info if folder still exist
        else {"Folder Exist"
        ForEach ($Folder in $findFolderResults.Folders) { #for each folder in folder results perform check of folder class
            $folder.folderclass #Info about folder class
            if ($Folder.folderclass -eq "IPF.Imap"){ #If folder class is target type, do change and update
                    $Folder.folderclass = "IPF.Note" #Folder class change in variable
                    Write-Host "Updating folder $folder.name to correct type IPF.Note. Folder will  start to be visible in OWA"
                    $Folder.update() #Folder class update in mailbox via EWS
                }
        }
        }
        }
</code>
}
Answer: It doesn't really make much sense to enumerate the the folders using Get-MailboxFolderStatistics and then search for each folder in EWS. This is going to really slow and unnecessary (you have the folderId anyway from Get-MailboxFolderStatistics so you can just convert that and bind to it). However I would
Get rid of Get-MailboxFolderStatistics altogether and just use straight EWS to enumerate the Folders in the Mailbox and do your fixes as this will be much quicker eg
<code>    ## Get the Mailbox to Access from the 1st commandline argument

$MailboxName = $args[0]

## Load Managed API dll
###CHECK FOR EWS MANAGED API, IF PRESENT IMPORT THE HIGHEST VERSION EWS DLL, ELSE EXIT
$EWSDLL = (($(Get-ItemProperty -ErrorAction SilentlyContinue -Path Registry::$(Get-ChildItem -ErrorAction SilentlyContinue -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Exchange\Web Services'|Sort-Object Name -Descending| Select-Object -First 1 -ExpandProperty Name)).'Install Directory') + "Microsoft.Exchange.WebServices.dll")
if (Test-Path $EWSDLL)
    {
    Import-Module $EWSDLL
    }
else
    {
    "$(get-date -format yyyyMMddHHmmss):"
    "This script requires the EWS Managed API 1.2 or later."
    "Please download and install the current version of the EWS Managed API from"
    "http://go.microsoft.com/fwlink/?LinkId=255472"
    ""
    "Exiting Script."
    exit
    }

## Set Exchange Version
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1

## Create Exchange Service Object
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)

## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials

#Credentials Option 1 using UPN for the windows Account
$psCred = Get-Credential
$creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
$service.Credentials = $creds
#$service.TraceEnabled = $true
#Credentials Option 2
#service.UseDefaultCredentials = $true

## Choose to ignore any SSL Warning issues caused by Self Signed Certificates

## Code From http://poshcode.org/624
## Create a compilation environment
$Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler=$Provider.CreateCompiler()
$Params=New-Object System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable=$False
$Params.GenerateInMemory=$True
$Params.IncludeDebugInformation=$False
$Params.ReferencedAssemblies.Add("System.DLL") | Out-Null

$TASource=@'
  namespace Local.ToolkitExtensions.Net.CertificatePolicy{
    public class TrustAll : System.Net.ICertificatePolicy {
      public TrustAll() {
      }
      public bool CheckValidationResult(System.Net.ServicePoint sp,
        System.Security.Cryptography.X509Certificates.X509Certificate cert,
        System.Net.WebRequest req, int problem) {
        return true;
      }
    }
  }
'@
$TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly

## We now create an instance of the TrustAll and attach it to the ServicePointManager
$TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy=$TrustAll

## end code from http://poshcode.org/624

## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use

#CAS URL Option 1 Autodiscover
$service.AutodiscoverUrl($MailboxName,{$true})
"Using CAS Server : " + $Service.url

#CAS URL Option 2 Hardcoded

#$uri=[system.URI] "https://casservername/ews/exchange.asmx"
#$service.Url = $uri

## Optional section for Exchange Impersonation

#$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)

#Define Function to convert String to FolderPath
function ConvertToString($ipInputString){
    $Val1Text = ""
    for ($clInt=0;$clInt -lt $ipInputString.length;$clInt++){
            $Val1Text = $Val1Text + [Convert]::ToString([Convert]::ToChar([Convert]::ToInt32($ipInputString.Substring($clInt,2),16)))
            $clInt++
    }
    return $Val1Text
}

#Define Extended properties
$PR_FOLDER_TYPE = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(13825,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);
$folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)
#Define the FolderView used for Export should not be any larger then 1000 folders due to throttling
$fvFolderView =  New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
#Deep Transval will ensure all folders in the search path are returned
$fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep;
$psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$PR_Folder_Path = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(26293, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);
#Add Properties to the  Property Set
$psPropertySet.Add($PR_Folder_Path);
$fvFolderView.PropertySet = $psPropertySet;
#The Search filter will exclude any Search Folders
$sfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($PR_FOLDER_TYPE,"1")
$fiResult = $null
#The Do loop will handle any paging that is required if there are more the 1000 folders in a mailbox
do {
    $fiResult = $Service.FindFolders($folderidcnt,$sfSearchFilter,$fvFolderView)
    foreach($ffFolder in $fiResult.Folders){
        $foldpathval = $null
        #Try to get the FolderPath Value and then covert it to a usable String
        if ($ffFolder.TryGetProperty($PR_Folder_Path,[ref] $foldpathval))
        {
            $binarry = [Text.Encoding]::UTF8.GetBytes($foldpathval)
            $hexArr = $binarry | ForEach-Object { $_.ToString("X2") }
            $hexString = $hexArr -join ''
            $hexString = $hexString.Replace("FEFF", "5C00")
            $fpath = ConvertToString($hexString)
        }
        "FolderPath : " + $fpath
        "Folder Class : " + $ffFolder.FolderClass
    }
    $fvFolderView.Offset += $fiResult.Folders.Count
}while($fiResult.MoreAvailable -eq $true)
</code>
Cheers
Glen
Comment: You will need Full Access to the Mailbox your trying to run the script against eg add this via Add-MailboxPermission or you could set-up EWS Impersonation https://msdn.microsoft.com/en-us/library/office/bb204095%28v=exchg.140%29.aspx which would require a change to script
Comment: When running this it came up and asked for credentials I entered administrator and password and it returns this error after

[Exception calling "FindFolders" with "3" argument(s): "Access is denied. Check credentials and try again."]
Comment: This works if I use the credentials of the mailbox I am trying to change I am marking this the answer because I can still do it that way. Thank you so much!  I am assuming the access denied on admin accounts is a configuration problem on my end.


how to compare slope of two lines and find the angles in 3d

Question: I have three points $A(x_1,y_1,z_1)$ ,$ B(x_2,y_2,z_2)$ and $C(x_3,y_3,z_3)$ and I want to compare the slopes of AB and BC and check the angles between them.
What should I do ?
Thanks in advance .
Comment: Welcome to MSE. Please use [MathJax](https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference).
Comment: How do you define "slope" in 3-D?
Answer: Hint 1: Find the direction cosines of $AB$ and $BC$. Then take their dot product. This will give the cosine of the angle between $AB$ and $BC$.
$$$$
Hint 2: The Direction Cosines of $PQ$, $P(x_1,y_1,z_1)$ and $Q(x_2,y_2,z_2)$, are $\left(\pm\frac{x_2-x_1}{\sqrt{(x_2-x_1)^2+(y_2-y_1)^2+(z_2-z_1)^2}},\pm\frac{y_2-y_1}{\sqrt{(x_2-x_1)^2+(y_2-y_1)^2+(z_2-z_1)^2}},\pm\frac{z_2-z_1}{\sqrt{(x_2-x_1)^2+(y_2-y_1)^2+(z_2-z_1)^2}}\right)$
$$$$
Edit: The Dot Product works iff the lines intersect.
Comment: B is the intersect point , doesn't matter if it's in 2D or 3D
Answer: Lines in dimension three do not have to intersect. We find their direction vectors to see if they are parallel.
Note that $$ \vec {AB} = < x_2-x_1, y_2 - y_1, z_2-z_1>$$ and  $$ \vec {BC} = < x_3-x_2, y_3 - y_2, z_3-z_2>$$
These vectors are the direction vectors are your lines.
The dot product  $$ \vec {AB}.\vec {BC}$$ along with the norms of these vectors determine the angle between the lines if they intersect at all. $$ \cos\theta = \frac { \vec {AB}.\vec {BC}}{||AB||.||BC||}$$
Comment: B is the intersect point , doesn't matter if it's in 2D or 3D
Comment: I think it's better to say : cosθ=AB.BC/(||AB||.||BC||)
Comment: Thanks, I fixed my error . Check the new version.


Arrange formatted fields in Views row using a template

Question: I want to render the rows of my "Recent content" Views page using a template in order to

subdivide the rows into multiple divs,
freely move around the rendered fields within these divs, and
apply some custom PHP where needed.

Idea A (fails): I could achieve this by creating a new Display Suite layout for nodes and using this layout to render my view set to display format "Content". However, I need to group the rows by a specific field, and grouping by fields is not available in the "Content" display format – for this, I would need to chose the display format "Fields".
Idea B (fails): When displaying fields (say, as an unformatted list), I can make use a copy of

views-view.tpl.php to adjust the coarse layout of the view, e.g., put an additional div around the whole content block;
views-view-fields.tpl.php to adjust how a single field should be rendered;
views-view-unformatted.tpl.php to adjust the coarse layout of rows, e.g. put
an additional div around the whole row. However, here the $row variable is not an array of rendered fields but rather a string containing the full html markup of the row, so I cannot access single fields and move them around within the row as I wish.

Idea C (works, but isn't it extremely inefficient?): I could display an unformatted list of fields, but render only the Node-ID-field. Then, in views-view-unformatted.tpl.php I can load the whole node corresponding to the current row via $node = node_load($row); and access all fields attached to it via $node->field_whatever. These I can then move around as I wish, wrap in divs, and apply custom PHP.
Idea C seems to work, but it also seems highly inefficient, as I will have to load the full node for every row. Also, I want to leverage the many views display formatters that my fields come equipped with, which I would have to re-build manually.
Question: Is there a way to specify formats for specific fields within the Views UI, submit only these to a template (and not the whole node), and move them around freely within the row as explained above?
Answer: I had a similar issue recently and this is what I did.
Let's say for the sake of argument that we have 6 fields: A to F. I wanted to wrap A-C in a layer eg "col-1" and D-F in a second layer eg "col-2". I overrided the field templates (views-view-fields--field-*.tpl.php) for A, C, D, F as such:

Added a
 element in A, without a matching closing tag
.
Added a
 element in D, without a matching closing tag
.
Added the respective closing tags
 i n C and F.

This may show some error or warning in your IDE, but the rendered HTML should be ok as long as the order of the fields in the views allows it (eg don't re-arrange D to be above C).


Returning list of lists

Question: I'm having a tough time figuring out why my function buildList-aux does not return the correct contents of the list Z.
When I run debug mode, and pause on the if statement above return Z, I can see that the value in racket is correct and as expected, however the actual contents that gets returned and printed have some sort of error (overflow? or something) and prints (shared ((-1- (list 'abc))) (list -1- -1-)).
I'm running DrRacket 6.11, Advanced Student language.
(define buildList-aux
  (lambda (E Z count)
    (if (<= count 0)
        Z
        (if (not (list? E))
            (buildList-aux E (append (list E) Z) (- count 1))
            (buildList-aux E (cons E Z) (- count 1))))))

(define buildList
  (lambda (N E)
    (buildList-aux E '() N)))

(buildList 5 '())
(buildList 3 'A)
(buildList 2 '(abc))
(buildList 3 '(A))

Expected Output:
(list '() '() '() '() '())
(list 'A 'A 'A)
(list (list 'abc) (list 'abc))
(list (list 'A) (list 'A) (list 'A))

Actual Output:
(list '() '() '() '() '())
(list 'A 'A 'A)
(shared ((-1- (list 'abc))) (list -1- -1-))
(shared ((-1- (list 'A))) (list -1- -1- -1-))

Answer: Your code is correct, it's just the way a list gets displayed in "Advanced Student". To see the correct output, do as @JohnClements suggests and go to "Language Details", and uncheck "Show Sharing in Values" box. Alternatively, switch to "Detect language from source" and specify #lang racket at the top of the file. Either way, the output will be as expected:
'(() () () () ())
'(A A A)
'((abc) (abc))
'((A) (A) (A))

But let's take a look at that weird output:
(shared ((-1- (list 'abc))) (list -1- -1-))

The above is stating that the result is:
(list -1- -1-)

Think of the -1- as a "variable" representing shared data, with a value of (list 'abc). If we substitute the "variable" with its value we get the more familiar:
(list (list 'abc) (list 'abc))

Comment: No need to switch language levels... go to Language>Choose Language..., click on "Language Details", and uncheck "Show Sharing in Values" box. Changing language levels is likely to have other effects that you may not expect or want.
Comment: The output _is_ already correct, it's just the way that's displayed that's weird - but that's not your fault. To avoid this inconvenience, better switch to regular racket.
Comment: I'm supposed to use "Advanced Student", is there a way to get the desired output using it, or must I switch to regular racket language?

Returning list of lists


Groups of numbers that equal a specific sum

Question: Find the number of order quadruples of integers $(a(1), a(2), a(3), a(4))$, with $a(n) \geq -4$ such that $a(1)+a(2)+a(3)+a(4)=4$.
I have no clue where to start. I am familiar with stars and bars, but I don't think I can apply that here.
Comment: Did you mean "ordered quadruples", as in $a(1) \leq a(2) \leq a(3) \leq a(4)$?
Answer: If you take an

"ordered quadruple whose entries are each $\ge -4$ summing to $4$"

and add four to each entry, you get an

"ordered quadruple of nonnegative integers summing to $20$".

This correspondence is bijective, so you can instead count the second object, which can be done with stars and bars.
Comment: That is very smart. Thanks!
Answer: First, we note that bars and stars solves the equation for the constraint of $a_n >=0$. Let us examine the common positive constraint, and then extend to the negative constraint.
Consider first the familiar case, where we have $a1 +a2+a3+a4=k $ and take $an>=q$ where $q$ is a positive number. Here, we need to "reserve" $q \times 4$ from k, so each of the $a_n$ has at least $q$. We then subtract $4*q$ from $k$, and solve the new equation $a1 +a2+a3+a4=k-(4\times q)$, without any constraints. We can now apply bars and stars. The intuition behind this is that for every assignment that satisfies the new equation, we can simply add 4 to each of the terms, and end up with a unique assignment of numbers such that $a_n >=q$, and their sum is $k-(4 \times q) +(4\times q)=k$
Turns out, for negative numbers, it is the exact same thing. Let $q=-4$ and solve for $a1 +a2+a3+a4=4+4*4=20$. The intuition here, is that for every solution we obtain such that we have an ordered quadruple of positive integers summing to 20, we can subtract 4 from each of these terms, and now we will have an ordered quadruple summing to 4, where each integer is greater than or equal to -4. This simple translation allows us to "shift" the bars and stars approach to any range we deem appropriate.
Comment: Thanks that was really helpful


What do you work on to get more confident, stay relaxed and play consistently?

Question: I've been a self taught drummer for the past 18 years and only a few months ago started getting into drum notation and actually practicing rudiments.
I'm realizing how important these both are to developing great (not only good) drumming skills, however I still believe that if you are not able to transmit your passion and excitement (through confident and relaxed, dynamic playing) to an audience, you will never go far in your drumming career, even if you are a monster in technique.
So I'm curious on what do you practice to develop confidence, relaxation, flow, groove etc?
Comment: Practice. Then I practice some more. Followed by daily practice. Also, karaoke. When you get on stage (sober!) to do karaoke in front of a bunch of drunk strangers who will often be super judgemental about your singing, it becomes a real good stage fright learning experience. Sitting behind a drum kit after that will feel much more manageable, emotionally speaking. I'm assuming you practice with a click/metronome at least part of the time you practice. If not, definitely do that.
Comment: @Tim As a guitarist, karaoke helped me a huge amount with confidence, stage presence, stage fright, etc. It's like practicing everything about being on stage without having to do load-in, sound check, tuning your guitar, etc. It's focused practice on an important aspect of the gig. Even (or maybe especially) if you can't sing. Again, if you can brave doing the bad singing in front of a bunch of strangers several times in a night, sitting behind the drums at the back of the stage and playing really well will seem *easy*.
Comment: @ToddWilcox - where did the OP as a vocalist appear from?
Answer: Apart from the obvious practice for and by yourself; get out and play with as many different musos as possible - different instruments, different genres, different styles, different experiences (beginners to seasoned players), different venues (from small cosy clubs to arenas- if you make the chance). You maybe only play one or two styles, so doing these things will broaden the outlook and experience. O.k., you may be the best muso on one gig, the worst on another, but you'll learn a heck of a lot from both ends. And if you're part of quite a rare breed - drummers who can read - the opportunity for gigs other drummers get passed by on will start to come to you. Good luck!
Answer: I play the violin an piano (no drums), but I find that the better I know a piece, the easier it is to play it well in front of people.

If I have practised a piece a great amount, I will generally be less stressed when playing in front of audiences as I know that it probably won't go wrong; there is no reason for it to do so since I have already played it through successfully so many times. Another confidence booster is playing in front of people more often, at small concerts, in front of family etc.
So, first be confident about the piece, both in terms of knowing it well and in terms of feeling good about it. This will allow you to play it well on the day, and once you are over the stress of there being an audience and the chance of it going wrong, you will be able to relax and show your passion for the music itself.
Comment: I feel it's somewhat different for drummers. They ought to be able to fit in more easily with people playing songs they are unfamiliar with, mainly as they don't have to worry about notes, chords and keys. So any particular piece should be easier for a drummer to perform than other musicians, unless of course, it's rather complex, but that goes for any piece on any instrument. Keeping time and playing sympathetically would be enough to start with.
Comment: Yep, you're probably right - I have done no drumming
Answer: Sorry for the late answer - I hope you don't mind. I wholeheartedly agree with Tim's answer; playing with other musicians is undoubtedly the best way to improve. Since you mentioned you are expanding your technical repertoire, I would advice practising the rudiments in a musical context. Try playing beats around the rudiments (for example a simple paradiddle with one hand on ride or hi hat and the other on the snare). I know a lot of drummers who can play great rudiments, but until you are comfortable with getting in and out of a rudiment it has no real use. Ultimately your aim is to incorporate anything new you learn into your musical vocabulary so that you play it as naturally as you play singles etc.
The best way to do this is of course to practice with other musicians as Tim said!
Answer: Practice often and with click-track! Familiarity with the song it's general pattern, fill spots...helps confidence immensly. With confidence anxieties disappear. don't drink to relax, beside all that goes with bad habits I found it destroyed my memory on stage over the ages.


Payment Gateway integration on selecting user profiles

Question: I am working on a project where human resource agents can view a list of job applicants. On each job applicant profiles theres a Confirm job applicant link visible only to human resource agents.
Once the HR agent clicks on Confirm job applicant link, the control should go to a payment gateway and after the HR successfully does the payment, the job applicant should get confirmed by that HR agent.
I was thinking to use ubercart module to handle all the payment transaction, but is ubercart the best choice for this type of workflow as theres no product shopping cart model here.
Is there any other module which can be used for the above workflow?
Answer: Drupal Commerce would probably be a better choice. I've used it on two non-standard ecommerce systems and it works well. It's very flexible and the rules integration would allow you to do things like comfirm and email applicant on order completion.
Comment: No, you'd need to add products. But you can add them to the cart programmatically. Sounds like a custom module would be your best option if you only wanna collect payment. What payment gateway are you using?
Comment: Yea, so programmatically I am adding the product and taking user to checkout form. I am using adyen payment gateway. Its integrated and working well :) [\n] Then only doubt I had was will it be possible to create an order without adding any product to the order.
Comment: Is it possible to integrate payment gateway integration without creating products in the site?


Como criar uma função para somar em php e salvar no mysql?

Question: Boa tarde.
Primeiramente, quero criar uma função no PHP que sempre que executada adicione +1 no MySQL.
No meu MySQL vai ter uma coluna chamada numeros, sempre que a função por executada, ele ira remover o que tem nessa coluna e vai estar adicionando +1
Exemplo:
Tenho na minha coluna o número 67, ai eu executo a função e na coluna vai passar de 67 pra 68
Espero que tenham entendi kkk
abraço!
Comment: Bem vindo so StackOverflow em Português! Qual a sua dúvida exatamente? Dê uma olhada nesse link de [Como fazer uma boa pergunta?](https://pt.stackoverflow.com/help/how-to-ask) para melhorar a pergunta para que ela tenha mais chance de ser respondida.
Answer: Você pode fazer isso direto no MySQL
Se você tiver uma tabela "numeros" e ela tiver um campo "numero", você pode primeiro adicionar um registro com o valor zero
INSERT INTO numeros (numero) values (0)

Desta forma, você criou um registro na tabela. Para sempre atualizar este registro, você pode fazer rodar o seguinte comando:
UPDATE numeros SET numero = numero + 1

Desta forma, ao rodar o UPDATE, sempre vai incrementar o campo número em mais 1.
Comment: a como faço para usar esse UPDATE?

$somar = 'UPDATE numeros SET numero = numero + 1';
$result = mysqli_query($conexao, $somar);

OBS: $conexao é o que está conectando o MySQL
Comment: Edit: criei uma nova tabela como você falou.


CSS Diamond shape centric website issues

Question: I've got an audacious project and would like some advice. I want to have a site that uses CSS Diamonds, I've attached an image to give a better idea of what I am looking at doing. Here are the issues I have run into so far:

The triangles are perfectly aligned until a submenu is added, then the right three shift to the right (this was working at one point). Also the li in which the submenu is under loses its diamond
The text on the submenu diamonds are not centered.
I cannot have the background image, then diamond then title. I have tried different z-indexs and it hasn't worked. (To see the menu titles, the background image needs to be removed, and the diamonds need to be -1 z-indexed)
Because I need to use spans to create the triangle text wrapping for the sidebar I cannot just make the sidebar div a diamond, so I still need to add the diamond in below it.
It would be great to have both the right text body and the sidebar text body wrap to the diamond, however I think this is not possible, therefore I reverted to just having the sidebar wrap.

Here is my HTML so far: I have commented out the submenu and my sidebar diamond attempt. http://jsfiddle.net/s4XXE/

 /* reset.css */
 html {
   margin: 0;
   padding: 0;
   border: 0;
 }
 body,
 div,
 span,
 object,
 iframe,
 h1,
 h2,
 h3,
 h4,
 h5,
 h6,
 p,
 blockquote,
 pre,
 a,
 abbr,
 acronym,
 address,
 code,
 del,
 dfn,
 em,
 img,
 q,
 dl,
 dt,
 dd,
 ol,
 ul,
 li,
 fieldset,
 form,
 label,
 legend,
 table,
 caption,
 tbody,
 tfoot,
 thead,
 tr,
 th,
 td,
 article,
 aside,
 dialog,
 figure,
 footer,
 header,
 hgroup,
 nav,
 section {
   margin: 0;
   padding: 0;
   border: 0;
   font-size: 100%;
   font: inherit;
   vertical-align: baseline;
 }
 article,
 aside,
 details,
 figcaption,
 figure,
 dialog,
 footer,
 header,
 hgroup,
 menu,
 nav,
 section {
   display: block;
 }
 body {
   line-height: 1.5;
   background: white;
 }
 table {
   border-collapse: separate;
   border-spacing: 0;
 }
 caption,
 th,
 td {
   text-align: left;
   font-weight: normal;
   float: none !important;
 }
 table,
 th,
 td {
   vertical-align: middle;
 }
 blockquote:before,
 blockquote:after,
 q:before,
 q:after {
   content: '';
 }
 blockquote,
 q {
   quotes: """";
 }
 a img {
   border: none;
 }
 :focus {
   outline: 0;
 }
 /* typography.css */
 html {
   font-size: 100.01%;
 }
 body {
   font-size: 75%;
   color: #222;
   background: #fff;
   font-family: "Helvetica Neue", Arial, Helvetica, sans-serif;
 }
 h1,
 h2,
 h3,
 h4,
 h5,
 h6 {
   font-weight: normal;
   color: #d10000;
 }
 h1 {
   font-size: 3em;
   line-height: 1;
   margin-bottom: 0.5em;
 }
 h2 {
   font-size: 2em;
   margin-bottom: 0.75em;
 }
 h3 {
   font-size: 1.5em;
   line-height: 1;
   margin-bottom: 1em;
 }
 h4 {
   font-size: 1.2em;
   line-height: 1.25;
   margin-bottom: 1.25em;
 }
 h5 {
   font-size: 1em;
   font-weight: bold;
   margin-bottom: 1.5em;
 }
 h6 {
   font-size: 1em;
   font-weight: bold;
 }
 h1 img,
 h2 img,
 h3 img,
 h4 img,
 h5 img,
 h6 img {
   margin: 0;
 }
 p {
   margin: 0 0 1.5em;
 }
 .left {
   float: left !important;
 }
 p .left {
   margin: 1.5em 1.5em 1.5em 0;
   padding: 0;
 }
 .right {
   float: right !important;
 }
 p .right {
   margin: 1.5em 0 1.5em 1.5em;
   padding: 0;
 }
 a:focus,
 a:hover {
   color: #09f;
 }
 a {
   color: #06c;
   text-decoration: underline;
 }
 blockquote {
   margin: 1.5em;
   color: #666;
   font-style: italic;
 }
 strong,
 dfn {
   font-weight: bold;
 }
 em,
 dfn {
   font-style: italic;
 }
 sup,
 sub {
   line-height: 0;
 }
 abbr,
 acronym {
   border-bottom: 1px dotted #666;
 }
 address {
   margin: 0 0 1.5em;
   font-style: italic;
 }
 del {
   color: #666;
 }
 pre {
   margin: 1.5em 0;
   white-space: pre;
 }
 pre,
 code,
 tt {
   font: 1em'andale mono', 'lucida console', monospace;
   line-height: 1.5;
 }
 li ul,
 li ol {
   margin: 0;
 }
 ul,
 ol {
   margin: 0 1.5em 1.5em 0;
   padding-left: 1.5em;
 }
 ul {
   list-style-type: disc;
 }
 ol {
   list-style-type: decimal;
 }
 dl {
   margin: 0 0 1.5em 0;
 }
 dl dt {
   font-weight: bold;
 }
 dd {
   margin-left: 1.5em;
 }
 table {
   margin-bottom: 1.4em;
   width: 100%;
 }
 th {
   font-weight: bold;
 }
 thead th {
   background: #c3d9ff;
 }
 th,
 td,
 caption {
   padding: 4px 10px 4px 5px;
 }
 tbody tr:nth-child(even) td,
 tbody tr.even td {
   background: #e5ecf9;
 }
 tfoot {
   font-style: italic;
 }
 caption {
   background: #eee;
 }
 .small {
   font-size: .8em;
   margin-bottom: 1.875em;
   line-height: 1.875em;
 }
 .large {
   font-size: 1.2em;
   line-height: 2.5em;
   margin-bottom: 1.25em;
 }
 .hide {
   display: none;
 }
 .quiet {
   color: #666;
 }
 .loud {
   color: #000;
 }
 .highlight {
   background: #ff0;
 }
 .added {
   background: #060;
   color: #fff;
 }
 .removed {
   background: #900;
   color: #fff;
 }
 .first {
   margin-left: 0;
   padding-left: 0;
 }
 .last {
   margin-right: 0;
   padding-right: 0;
 }
 .top {
   margin-top: 0;
   padding-top: 0;
 }
 .bottom {
   margin-bottom: 0;
   padding-bottom: 0;
 }
 /* Customization */
 body {
   background: #efefef;
   margin: 0px auto;
   width: 1320px;
 }
 #bodydiv {
   width: 100%;
   background-image: url('https://i.stack.imgur.com/QVfwy.png');
   background-repeat: repeat;
   overflow: hidden;
 }
 ul {
   list-style: none;
   position: relative;
   width: 100%;
   text-align: center;
   padding: 0px 0px 0px 28.6em;
   margin-bottom: 0px;
   display: block;
 }
 li {
   float: left;
   margin: 0em -1.45em;
   font-weight: bold;
   font-size: 2em;
 }
 ul li a {
   color: red;
   text-decoration: none;
 }
 ul li a:hover {
   color: #eee;
   text-decoration: none;
 }
 .triangle:after {
   content: '';
   position: relative;
   top: 105px;
   margin-left: -50%;
   border: 5em solid transparent;
   border-top-color: red;
   z-index: 1;
 }
 li:nth-child(1).triangle:after {
   border-top-color: blue;
 }
 li:nth-child(2).triangle:after {
   border-top-color: green;
 }
 li:nth-child(3).triangle:after {
   border-top-color: purple;
 }
 ul li ul {
   font-size: .5em;
 }
 ul li ul a {
   z-index: 1;
 }
 .diamond,
 .diamond:after {
   width: 0;
   height: 0;
   border: 4em solid transparent;
   content: '';
   z-index: 1;
 }
 .diamond {
   border-bottom-color: red;
   position: absolute;
   top: -1em;
 }
 .diamond:after {
   position: absolute;
   margin-left: -4em;
   top: 4em;
   border-top-color: red;
 }
 .diamond.big,
 .diamond.big:after {
   width: 0;
   height: 0;
   border: 9em solid transparent;
   content: '';
   z-index: 1;
 }
 .diamond.big {
   border-bottom-color: red;
   position: absolute;
 }
 .diamond.big:after {
   margin-left: -9em;
   top: 9em;
   border-top-color: red;
 }
 .diamond.huge,
 .diamond.huge:after {
   width: 0;
   height: 0;
   border: 500px solid transparent;
   content: '';
   z-index: 1;
 }
 .diamond.huge {
   top: -500px;
   border-bottom-color: red;
   position: absolute;
 }
 .diamond.huge:after {
   margin-left: -500px;
   top: 500px;
   border-top-color: red;
 }
 li:hover > ul li {
   display: none;
 }
 ul ul li {
   font-size: 1em;
   display: block;
   position: relative;
   left: 1em;
   margin-left: 1em;
   margin-top: 1em;
 }
 ul ul li:nth-child(1).diamond {
   border-bottom-color: orange;
   top: 4em;
   left: 6em;
 }
 ul ul li:nth-child(1).diamond:after {
   border-top-color: orange;
 }
 ul ul li:nth-child(2).diamond {
   border-bottom-color: aqua;
 }
 ul ul li:nth-child(2).diamond:after {
   border-top-color: aqua;
 }
 */

  <title>Triangles</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta charset="utf-8">
  <meta name="viewport" content="width=479;">
  <link href="style.css" rel="stylesheet" type="text/css">

Home

Home

Home

Home

Heading for the page

        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur at tellus ipsum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nunc et erat magna. Aenean volutpat ultrices quam nec vestibulum. Nullam
        quis ante vel tellus mattis laoreet. Duis vehicula est id lorem consequat pretium. Suspendisse potenti.

        Donec ut nunc ante, vel elementum ante. Vivamus mattis ornare fermentum. Quisque vitae justo quam. Cras nunc odio, sodales a condimentum id, laoreet sed neque. Integer adipiscing placerat sollicitudin. Etiam posuere, lacus ut bibendum mattis, nibh urna
        ullamcorper nibh, vitae blandit massa nunc ac augue. Aliquam mollis aliquam magna eget rutrum. Suspendisse iaculis placerat dolor, at venenatis eros rutrum non. Phasellus pretium risus ac nisi sollicitudin sit amet adipiscing nisl malesuada. Cras
        nisi nunc, hendrerit at elementum in, auctor a ligula. Maecenas pharetra condimentum aliquam. Nulla et ultricies nisi. Donec vitae neque ac augue iaculis pretium nec sed ipsum. Sed gravida facilisis congue. Nullam lobortis interdum augue ac venenatis.
        Sed luctus turpis sed urna sollicitudin imperdiet.

        Nam lectus lacus, ultricies non placerat in, consectetur lacinia urna. Duis orci eros, porta nec tempus vitae, sollicitudin ac massa. Nam porta dignissim bibendum. Aenean hendrerit lacinia nisi vel viverra. Maecenas eu mi orci, vel laoreet massa. Fusce
        rhoncus, tellus eu consectetur eleifend, lacus enim ultricies diam, ut rutrum mauris dolor nec nibh. Suspendisse tincidunt neque vitae ligula adipiscing consectetur quis et nisl. Fusce eu sem diam. Aenean iaculis laoreet lacus, a vulputate augue
        egestas ut. Donec fringilla nisi sit amet arcu convallis in egestas nisl imperdiet. Nam interdum dolor nec nibh auctor eget sollicitudin orci iaculis. Proin turpis sem, ultricies facilisis euismod in, mattis in justo. Morbi feugiat semper blandit.
        Quisque tincidunt semper sodales. Nullam suscipit tempor gravida.

        Vivamus eu diam id libero rhoncus ultrices ut eu lectus. Praesent diam elit, luctus a lobortis ac, faucibus in nisl. Duis id est purus, a condimentum leo. Quisque gravida metus sed arcu placerat nec convallis quam blandit. Cras laoreet vulputate sem et
        consectetur. Curabitur felis erat, auctor vitae eleifend id, suscipit eget sapien. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam porttitor ultricies tortor in tincidunt.

        In hac habitasse platea dictumst. Sed erat leo, cursus vel pellentesque quis, pretium quis mi. Integer interdum neque non nisi luctus sollicitudin pellentesque nunc consectetur. Fusce auctor bibendum porta. Donec in est lacus, in luctus purus. Proin consectetur
        molestie urna, eu laoreet ligula pellentesque eget. Cras at magna sit amet lacus consectetur viverra. Mauris sed faucibus nisl.

What is ACM?

      In hac habitasse platea dictumst. Sed erat leo, cursus vel pellentesque quis, pretium quis mi. Integer interdum neque non nisi luctus sollicitudin pellentesque nunc consectetur. Fusce auctor bibendum porta. Donec in est lacus, in luctus purus. Proin consectetur
      molestie urna, eu laoreet ligula pellentesque eget. Cras at magna sit amet lacus consectetur viverra. Mauris sed faucibus nisl.

Answer: Good news! I have solved most of my issues. Here is my updated jsfiddle: http://jsfiddle.net/s4XXE/1/ I will continue to work on fixing all issues. Any assistance would be helpful.

FIXED: I used the submenu class to correct the offset that was caused by the addition of a submenu.
FIXED: The titles do show up now. I think I added display:relative; to the CSS so that it accepted the z-index
FIXED: Changed the triangles on the menu to before instead of after so the triangles still show up on the menus with submenus.
BUG: The submenus are shown when the mouse gets close to the menu item. It should just be showing the submenus when the mouse is on the triangle.
BUG: On the menu items a link is only 'valid' when the mouse is over the text. It would be nice to have it 'valid' when it is over the shape.
BUG: The titles on the submenus and sidebar title are still not centered.
BUG: Changing the "Home" titles messes everything up. (need to adjust for changing content)


Any element of a field may be expressed as the sum of non-zero two elements of the field.

Question: Am I correct in this conclusion?
Any element of a field may be expressed as the sum of some two non-zero elements of the field.
I.e. For any $z \in F$ there is $x, y \neq 0 \in F$ such that $z = x + y$
Proof:
$z  + (-y) = x$
Certainly. (Any $z$ added to the additive inverse of some $y$ is certainly equal to some $x$ in $F$)
Hence
$z + (-y) + y = x + y$
$z = x + y$
Comment: Absolutely correct! It hold for any additive group, by the way. Not just fields.
Comment: Sorry, I meant two non-zero elements. Does my conclusion still hold?
Comment: "$z+(-y)=x$" is not a proof, because you don't say what the elements are. And you have no warrant for asserting you have the equality. In short the equality doesn't mean anything until you tell us who $z$, $x$, and $y$ are.
Answer: There is but one exception: the field of two elements, $\mathbb{F}_2$. In this field, the element $1$ cannot be expressed as the sum of two nonzero elements, since the only nonzero element is $1$ but $1+1=0$.
If the field has more than two elements, though, then the result holds. In fact, it holds in any group (commutative or not): Let $G$ be a group with at least three elements, let $e$ denote its identity element, and let $z\in G$.
Let $x$ be any element of $G$ that is different from $e$ and from $z$ (possible since $G$ has at least three elements); (if $z=e$, then just pick any $x\neq e$). Let $y=x^{-1}z$. Since $x\neq z$, then $y\neq e$. And we have $xy = xx^{-1}z = z$.
Interpreting this additively in a field, given $z$, pick $x$ to be any element of the field different from $0$ and from $z$, and take $y=z-x$. Since $x\neq z$, then $y\neq 0$, and we have $x+y = x+(z-x) = z$.
Answer: First off, we may assume without loss of generality that $z\ne 0$, as $0 = 1 + (-1)$ in every field.
The proof fails because you do not know that $y \ne z$, and thus it could be the case that $x = 0$, meaning you have not found your desired pair of nonzero elements. Your proof thus makes the implicit assumption that there are two distinct nonzero elements $y$ and $z$, a statement true in every field except $\mathbb F_2$. And sure enough, the statement is false in $\mathbb F_2$: $1 = 0 + 1$ and $1 = 1+0$ are the only ways to express $1$ as the sum of two elements of that field.


SYN Cookies on a LAN server - Ok / advisable to disable?

Question: We are looking at tuning our linux tcp stack on one of our lan boxes.
We were wondering if it is ok / advisable to disable SYN cookies on a server that doesnt have any sort of external (eg: "internet") access.
Is it ok to disable syn cookies on a LAN only server via net.ipv4.tcp_syncookies?
Answer: SYN cookies are disabled by default.  That should be a reasonable indication of what you should be doing, absent specific knowledge to the contrary.  More specifically, tcp(7) says:

The syncookies feature attempts to protect a
               socket from a SYN flood attack.  This should be used as a last resort,
               if at all.  This is a violation of the TCP protocol, and conflicts with
               other areas of TCP such as TCP extensions.  It can cause problems for
               clients and relays.  It is not recommended as a tuning mechanism for
               heavily loaded servers to help with overloaded or misconfigured
               conditions.
Comment: many linux distributions, including centos which we are using, default to net.ipv4.tcp_syncookies=1 via /etc/sysctl.conf. default as in, on fresh install net.ipv4.tcp_syncookies=1 exists in /etc/sysctl.conf.
Comment: So your distro overrides the default.  That doesn't change the fact that it *is* the default in the Linux kernel.
Comment: Syncookies are enabled by default to prevent system crash in case of excessive syn requests, if you are expecting a very high rate of connections for e.g. test machine then you can disable it no problem via sysctl
Comment: @AndrewSmith: You can't crash a Linux server with a SYN flood.  You can only starve the server from being able to accept legitimate TCP connection requests.
Answer: I am not sure if you are doing it the right way.
If you have some need or some problems (then say it :) then tweak the tcp stack otherwise leave it as it is.
Why would you disable syncookies what is the benefit you are expecting?