Transparently replace file mapping with anonymous

Question: I am doing a checkpoint-and restore using CRIU; in turn after restore, my application wakes with some threads that have their stack mmaped into files on disk (CRIU doesn't do it by default, this is a custom optimization). Later on, I want to transparently replace this mapping with anonymous memory - allocating new one, copying it over and finally calling mremap to the original address.
However, there's a glitch in this approach - if the threads start mutating the stack while I copy it over I could break the application. Ideally, I would trap it using userfaultfd but it's not possible to register on a file-mapped memory region. Even if I introduced some mutex to those threads there's no way to tell that the thread is really parked and won't mutate its stack until I wake it up.
I am thinking of mprotect to read-only and handling SIGSEGV. Or is there a better approach? PTrace self?
Comment: Are you saying that it is the only after the restore, as a function of custom modifications you've made to CRIU, that these threads have their stacks mmaped to a file?
Comment: Program behavior is undefined in the event that a signal handler is called for a `SIGSEGV` , and that call returns normally.  A handler for `SIGSEGV` needs to terminate the program -- for example, by calling `_exit()` or `abort()`.
Comment: Yes, normally the thread stack wouldn't be allocated on file-mapped memory. Program state is really undefined by POSIX (as man sigaction says), but handling SIGSEGV is used in JVMs on all major platforms so in practice it's workable. IIUC it continues on the same instruction again when I manage to fix the memory.
Comment: Not just POSIX.  The C language spec says that program behavior is undefined if a signal handler is called to handle a `SIGSEGV`, and it returns normally.  I can imagine that some applications might use such an approach anyway in carefully controlled, carefully tested ways, in conjunction with specific C implementations.  That does not generalize.
Answer: The only alternative I have come up with that I would trust is for the main thread to use ptrace to force the others to stop, and then to resume them when that is safe.  You seem to already be aware of this option, so I will not go into details.  The main objective here is to preemptively suspend the activity of the affected threads while their stacks are being copied, which seems far less risky than approaches that do otherwise.
The alternative presented in the question is to use mprotect to trap the threads' attempts to modify data on their stacks while the copy is being made.  I guess the idea is to have a lighter touch, allowing threads to proceed as long as they can do so without modifying their stacks, but I don't think that's plausible or viable.  Among other things:

it seems unlikely in general that any thread will be able to do much meaningful work without modifying its stack, so it seems doubtful that there is much gain available in practice.

as I observed in comments, both C and POSIX specify that a program has undefined behavior if a signal handler for SIGSEGV returns normally.    Usually, program termination is the only viable alternative, but a sufficiently prepared program might in some cases longjmp() or siglongjmp() out of the handler instead.  That could give you a vector for recovery, but only to whatever extent you are prepared to mediate it with special tooling, and only to the extent supported by such tooling.
It is not safe to assume that the trap handler installed by the kernel will  have the effect of retrying the failed instruction of your userspace program in the event that a handler for a segfault returns normally.  That ranks very high among the implications of the userspace behavior being undefined.  If you did observe that effect with a particular combination of hardware and software then that would be no basis for relying on the same thing for different combinations.
Comment: Thanks for the elaborate answer; right now I have a working (ofc to the tested extend) solution with SIGSEGV, I'll see the feedback in context of that project. I agree that PTrace is probably a more sound solution, but it requires more privileges, therefore probably another binary with suid. My goal is not really to let other threads continue, but rather have a simple way to block them.
Answer: That premise seems a bit weird to me, I don't really get why you'd have the stacks file-mapped after such a CRIU operation... but anyway:
First off: There is one type of file mapping that userfaultfd does work with, which is shmem/tmpfs. But I don't know whether that helps in your case. If not:
You can't register the file mapping with userfaultfd, but you can register the new anonymous mapping with userfaultfd. This means that one thing you could do would be to first replace the stack with the new mapping, then copy the data over from the file when you know the old mapping is no longer used.
You probably don't want to do exactly this, because then you'd have to block for as long as it takes to copy the entire stack. There are two optimizations you could consider:

You could try to stop the thread and figure out the thread's current stack pointer; any memory that is sufficiently far below the stack pointer based on the ABI (e.g. 128 bytes on amd64) doesn't need to be copied at all, you only have to register the currently used part of the stack with userfaultfd. (Probably a good way to do this would be to send a signal to the thread and let the signal handler take care of this.)
If your threads typically have relatively little stack usage and only use lots of stack memory for short moments, this is probably all you need?
You could copy the file contents into anonymous memory area A ahead of time while letting the kernel monitor which of the file mapping pages have been written to. Then after you replace the file mapping with a new anonymous mapping B with userfaultfd, you can ask the kernel which parts of the file mapping have been written to, copy all those parts into mapping A again, and then mremap() mapping A over the file mapping. This probably only makes sense if your stacks are typically pretty big. To figure out which parts of a file mapping have changed, you can use the kernel's Soft-Dirty interface, using bit 55 in /proc/[pid]/pagemap and /proc/[pid]/clear_refs.
Comment: The mmapping is an optimalization in a forked version of CRIU - regular CRIU just loads the files, but here we make the loading lazy. Probably the best option is really to not load it in the first place, and use userfaultfd to implement the laziness.


STATA: Interpreting sigma_u, sigma_e in Random Effects Modells, Difference betwenn sigma_u in Random- and Fixed-Effects-Modells

Question: I am struggling with a pretty pragmatic problem. Like the heading is indicating I am doing longitudinal analysis in STATA. Therefore I need to model a fixed-effects-modell and a random-effects-modell an compare them.
You can find the results listet below.The depend Variable is the job prestige of italian and turkish migrants in germany and the independent variables are the speaking and the writing skills (correlation >.60) in another modell I also add the age at immigration as a timeinvariant variable.
Now I have to compare these two modells, which is okay, but there is point which is overhelming me: the sigma values in the random-effects-modell and the comparisn of sigma_u between random- and fixed-effects.
I know that rho in context of the random-effects-modell indicates the estimated proportion of the between-Variance at the total variance. It is calculated like this: sigma_u/sigma_u+ sigma_e
So sigma_u in the random-effects-modell has to be the between variance. But what is sigma_u in the fixed-effects-modell? And what is sigma_e in the random effects modell? Or how do I interpret them?

Another smaller problem is, that the Fu test, the corr(u_i,Xb) and the Hausman test indicate that I have to use the fixed-effects-modell. But actually its not significant. Could a conclusion be that there are timeinvariant variables missing in the modell, so that the fixed-effects-modell is not significant but reliable while the random-effects-modell is significant but distorted?
Best wishes,
Marcel
Answer: The online Stata documentation provides some answers to your questions:
http://www.stata.com/manuals13/xtxtreg.pdf
: p.8 introduces the notations, p.13 an example for the fixed-effect (FE) model, and p.16 for the random-effect (RE) model.
If the model is:
$$y_{it} = X_{it}\beta + \eta_i + \varepsilon_{it}$$
Whether in the FE or RE model, $\sigma_u$ is the standard deviation of the time-invariant individual-specific term $\eta_i$, and $\sigma_e$ the standard deviation of the error term $\varepsilon_{it}$. In both case, your results suggest that a substantial part of the variance of the outcome (or, more precisely, what is left after you control for the observables) is due to unobserved heterogeneity across migrants.
About your second question, while I'm 100% sure to have understood you, I would say that, indeed, it seems preferable in your case to grant more trust to the FE model, especially if the tests you mention indicate to do so. Note that the FE model requires, in general, milder assumptions than the RE model, in which you have to specify the distribution for the $\eta$ as well as the way it co-varies with the observables. No such assumption in the FE model but this might come at the cost of precision.
Comment: Nice answer, but I think the penultimate sentence needs some attention.
Comment: Thanks for the kind answer Roland! But I have one more question according to  σu in FE and RE model.
I found something in literature:
- sigma_u in FE: mean variation of person-specific means. It's constant. In this case the mean difference of the unit specific mean from the total mean of the job prestige is 12.114 units.
- sigma_u in RE: variation of the residual unit effects (=the not through independent variables explained difference between the unit specific means).
So  I can not really compare them between the models, but sigma_u in the RE should get smaller when variables are added?
Comment: In FE models, the covariates that are not varying over time are simply not identified. In RE models, it must be true that sigma_u is non-increasing when covariates are added, pretty much like the sum of squares of residuals.
Comment: @Roland I think the second FE should be RE.


Webform submit button does not work

Question: I have created a webform block. After I enter all the info in the webform, I clicked the Submit button. Nothing happens. Do you know why the Submmit button is not working?
This webform block's submit button works: http://godrupal.info/?q=contact-us
Actual webform node's submit button works : http://godrupal.info/?q=node/16. Then I created block of the node/16 but then this webform blocks's submit button does not work: http://godrupal.info/?q=plan-an-event
Answer: if you look into your console log you will see the following errors

this happens when you hide a required field.
I had a look at your markup and found that you have the above 4 fields display set as none; and at the same time also has attribute required.
So remove the required attribute or apply novalidate on submitting form.
Comment: Thank you. After i display the fields, submit button is working. I did make the address field not required. I still have the same issue. I do need to hide the fields. I did look at the form settings of the webform and i did not see where to select no validate on submitting form?
Comment: Can you remove the required attribute from the hided field?
Comment:  like this.
Comment: I did make the address date field not required now. It is working. I cant make the month/day/year not required bc I used date field. But the problem is that people can submit the form without entering the date. Option1- I can make the field required and display all fields Option2- I can make the field not required then hide the fields then I dont know where to enter form action = "" novalidate. Is it need to be entered in tpl file? I am still new. My two requirements: date must be required and     month/day/year field must be hidden.
Comment: try this in your template.php
function THEMENAME_preprocess_webform_datefieldname(&$variables) {
  $variables['element']['#attributes']['novalidate'] = TRUE;
}
Comment: alternatively, remove html5 required attribute and add custom php or js validation. that would work best

Webform submit button does not work


SVG stroke-dashoffset not working on safari

Question: Stroke-dashoffset is not working on safari even if I add -webkit- prefix. please help me. Thanks!....
Here is my sample code....
#path1 {
  stroke-dasharray: 500;
  -webkit-animation: dash 2s ease;
  animation: dash 2s ease;
  display:inline-block;
}
.path2 {
  stroke-dasharray: 500;
  -webkit-animation: dash 2s ease;
  animation: dash 2s ease;
  display:inline-block;
}

@keyframes dash {
  from {
  stroke-dashoffset: -500;
  }
}

@-webkit-keyframes dash {
  from {
  stroke-dashoffset: -500;
  }
}

Comment: Help us to help you. Please provide a [MCVE](http://stackoverflow.com/help/mcve).
Answer: Safari does not support negative stroke-dashoffset......
Answer: It's not that "Safari doesn't support negative `stroke-dashoffset", per the accepted answer. Safari is actually following the spec. Chrome, Firefox, etc are breaking with the spec, possibly because they realized the spec was poorly defined.
Here is the SVG spec's definition of stroke-dashoffset. Some things to note:

Positive stroke-dashoffset values do not march the dash line forward, from the start of the path to the end of the path: they move it backwards. This is because it's defined as "the distance into the dash pattern where the path starts", rather than "how far along the path the pattern starts" as you would think.
Negative stroke-dashoffset values do not march the dash line backward, exactly. That is, crossing over from > 0 to < 0 does not always continue the smooth march of dashes. The true behavior is peculiar✝ and appears unpredictable on the surface.

In short, dashoffset works nothing like you'd expect. And you should probably avoid negative values if at all possible to avoid confusion.
Solution
The common way to get around this is to just remove the negative sign and reverse the animation, which usually (possibly always?) should result in the desired effect:
#path {
  stroke-dasharray: 500;
  animation: dash 2s ease reverse;
}

@keyframes dash {
  from {
    stroke-dashoffset: 500;
  }
}

Discussion✝
I believe the confusion ultimately stems from ambiguity in this line in the spec:
where s is the sum of the dash array values.

Does "dash array values" mean the values provided by the user in the attribute? Or does it refer to the array of values after being processed, with odd-length arrays being repeated to yield even-length arrays (eg stroke-dasharray="10" becomes stroke-dasharray="10 10"). Safari seems to interpret it as the former, Chrome and Firefox the latter.
For example, with stroke-dasharray="10", the spec says stroke-dashoffset="-1" will look the same as stroke-dashoffset="9" per the spec. But in Chrome and Firefox, it will look the same as stroke-dashoffset="19". However, if you set stroke-dasharray="10 10", it seems to behave as you would hope.
Clarifying the spec
Here's an issue I created on the spec regarding negative dash offsets:
https://github.com/w3c/svgwg/issues/795
If you found this frustrating, give the issue some comments or 's and maybe they'll fix it.
Comment: The SVG 1.1 spec did not contain this text. If you feel things can be improved, take it up with w3c and get the spec changed further. If Chrome and Firefox agree, it's probably easiest to get Safari to fix their implementation.
Answer: As above, negative values don't work for stroke-dashoffset. If you shift your values to positive numbers this should work (you'll need to shift your initial value too):
.path2 {
  stroke-dasharray: 1500;
}

@keyframes dash {
  from {
  stroke-dashoffset: 500;
  }
}

@-webkit-keyframes dash {
  from {
  stroke-dashoffset: 500;
  }
}

Answer: I was able to get the desired result by just animating the stroke-dashoffset to the doubled value of stroke-dasharray
Step one:

Step two:
Answer: I had the same issue with negative stroke-dashoffet. Hence, I used positive value instead and flipped the SVG.
In my particular case, I resolved it by flipping the SVG by using transform:scaleX(-1).
Depending on your requirement, flipping the SVG might work.
Answer: SVG Animate tag seems to work without css

  <path stroke-dasharray="500" other attr>
    <animate
      attributeName="stroke-dashoffset"
      from="-500"
      to="500"
      dur="3s" // duration
      begin="2s" // delay
      fill="freeze"/>
  </path>
Comment: Welcome to SO. Please ensure, your **snippets are rendering a visible result.** Your example is missing a parent `` element. Besides, you're right stroke related svg attributes like `stroke-offset` or `stroke-dasharray` can also be defined or animated via native SMIL animation or svg attributes.


Can I account for an unknown starting period while estimating reliability?

Question: I have been going through this article on how to estimate the reliability of a device using Weibull Analysis. The article clearly states that in order to do this, I need to have information in the following form:
#   Age_A   Age_B
1    26      16
2    31      24
3    35      30
4    38      36
5    41      42
6    44      48
7    47      55
8    51      63
9    55      76
10   70      126
Avg. 44      52

From my understanding, this age must have been measured from some starting period that is used as the experiment starting time. Now, if I am not really sure of the experiment starting point and have observations of failures across time, is there any way I can still carry out the Weibull Analysis to draw some conclusions?
To put it clearly, let us say that in the middle of nowhere I decide to see which type is more reliable while both types have been operational for an unknown period of time. My idea was to fix a time period t and then start calculating the age relative to this t. Of course, the serious drawback is for devices that have been operational for a long time that fail right after t in which case the age is miscalculated at a small number. Any suggestions?
Answer: You have what's called left-censored data. Survival likelihoods are still computable for it, although are even more complicated than for right-censored data, so I am not going to attempt to reproduce them here. Klein and Moeschberger's book covers Weibull regression in a couple of sections, and NADA package provides some of the tools of the analysis of left-censored data.
Comment: Paul Allison's book *Survival Analysis Using the SAS® System: A Practical Guide* covers how to analyze data like this in SAS as well. I would try to see if you can try and find the starting time (origin) of your measurements before you go with this approach, as it's considerably more complex.
Comment: @EpiGrad: Thank you for your suggestions. My problem is that even if I do determine the starting time for the measurements, they will be different for different devices and missing for most of them.
Comment: @Legend You go with the data you have then - its harder to model censored observations, but far from impossible. Good luck!
Comment: @EpiGrad: I am currently bound to R so am not really sure how much of this is really far from impossible :) Are you aware of any material that discusses this using R?
Comment: @Legend: have you taken a look at NADA package I suggested? This is an R package alright.


Biggest opening game wins by Stanley Cup defending Champions

Question: The Washington Capitals dominantly beat the Boston Bruins 7 to 0 on their opening game of 2018 season. The Capitals won the Stanley Cup the previous season for the first time in their team history.
What are the top 5 biggest opening game wins by the defending NHL champions?
Comment: Perhaps [this tweet](https://twitter.com/PR_NHL/status/1047818671098400768) (posted [also in chat](https://chat.stackexchange.com/transcript/29021/2018/10/8)) might help you get an answer. Although it would be nice to find a more reliable source.
Answer: Shamelessly stealing from NHL's Public Relations department (which is a plenty reliable source) plus using Pro Hockey Reference game logs for verification and goal difference, here are the top 5 biggest opening game wins by defending Stanley Cup Champions (the year being the calendar year the respective team won the Stanley Cup):

1976 Montreal Canadiens: 9 goals (10-1)
1965 Montreal Canadiens: 7 goals (8-1)
2018 Washington Capitals: 7 goals (7-0)
1942 Toronto Maple Leafs: 5 goals (7-2)
1938 Chicago Black Hawks: 5 goals (6-1)
Comment: Please add a link to the  NHL PR source you mentioned, also note that this does not match the tweet mentioned in the question's comments
Comment: @alamoot Link added. What doesn't match the tweet? I don't follow.
Comment: The list you provided is not the same list as the one in the tweet
Comment: @alamoot You asked for biggest opening game wins by defending Stanley Cup champions. The tweet provides most goals scored in the opening game by defending Stanley Cup champions.
Comment: perfect, that's what I want


Orable DB e MVC 4 - Erro de conexão (ORA-06413)

Question: Tenho uma dll que é responsável por realizar as consultas no banco. Nessa dll, na classe de conexão, possuo um método que abre a conexão com o banco e me retorna a conexão, conforme por der visto abaixo:
private OracleConnection _IniciarConexao()
{
    ///Limpa as variáveis de erro
    this.UltimaExcecao = null;
    this.Erros         = new List<Exception>();

    OracleConnection conexao = null;

    try
    {
        conexao = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=***)(PORT=***)))(CONNECT_DATA=(SERVICE_NAME=****)(SERVER = DEDICATED))); User Id=***; Password=***;");

        conexao.Open();
    }
    catch (Exception ex)
    {
        this.UltimaExcecao = ex;
        this.Erros.Add(ex);
    }

    return conexao;
}

A dll utilizada para as referências do Oracle é a System.Data.OracleClient.dll Versão 2.0.50727.
Essa dll é consumida por uma aplicativo Web utilizando MVC 4 com C# e o .Net Framework 4.0.
Quando utilizo essa dll em um projeto Windows Forms consigo conexão com o banco de dados sem problemas, porém quando utilizo em um sistema MVC ou WebForms ocorre um erro de conexão com o banco de dados.
O erro que ocorre é o ORA-06413. A imagem abaixo pode ilustrar o ocorrido:

Já estou no segundo dia de pesquisa sem sucesso sobre como resolver este problema.
Ao publicar o sistema no servidor de produção ele consegue conexão normalmente, porém no local, para utilizar o modo debug, não consigo conexão com o banco.
Alguém sabe como corrigir este erro, ou alguma fonte que possa me indicar para resolver este problema?
Comment: A opção para usar a versão 64bit do IIS Express fica em `TOOLS > Options > Projects And Solutions > Web Projects > [x] Use the 64 bit version of IIS Express for web sites and projects`
Comment: No servidor de banco Oracle tem os logs, verifique se essa conexão está chegando, ou verifique se está passando pelo firewall !!! Também tem o View Details na Exception verifique o dados mais detalhado do erro !!!
Comment: [Você está usando este pacote](https://www.nuget.org/packages/odp.net.x64/)? Ou ainda, [este outro](https://www.nuget.org/packages/Oracle.ManagedDataAccess/)?
Comment: Está usando o IIS completo ou o Express? O seu erro pode estar sendo causado por ter um client 32bit do Oracle e estar rodando uma aplicação 64bit, ou vice-versa. Tente alternar entre 32 e 64bit na sua aplicação (direto no pool se for IIS ou no Visual Studio se for o Express).
Comment: @Cezar vou verificar os logos do banco e a questão do firewall. Os detalhes da exceção não tem nada, já olhei diversas vezes.
Comment: @CiganoMorrisonMendez não estou usando nenhum pacote Nuget, apenas a própria dll que é utilizada nos projetos da empresa.
Comment: Então é isto. Vou responder.
Comment: @MarcusVinicius Estou usando o IIS Express, que vem com o VS 2013 Express For Web. Vou verificar essa questão que você falou
Comment: @MarcusVinicius a sua solução funcionou. Se quiser postar uma resposta eu aceito ela.
Comment: @RichardDias publiquei a resposta com uma explicação de como configurar 32/64bit em apps Web .Net (http://pt.stackoverflow.com/a/92072/23593)
Answer: Esse erro é comumente causado por incompatibilidade de arquiteturas entre a DLL do Oracle e a aplicação que está rodando. Se estiver usando a versão 64bit da DLL por exemplo, em projetos Web, deve-se configurar o Pool de aplicativos do IIS para usar 64bit ou ativar a configuração do IIS Express no Visual Studio, se for o caso:

Se estiver usando o IIS, por padrão ele roda como 64bit. Se precisar forçar 32bit, Dentro do Gerenciado do IIS, expanda "Application Pools", localize o Pool que sua aplicação está usando, clique direito > Advanced Settings:

Se a configuração "Enable 32-Bit Applications" estiver como True, o pool rodará como 32bit, caso contrário, como 64bit.

Se estiver usando o IIS Express, dentro do Visual Studio, Vá em TOOLS > Options > Projects And Solutions > Web Projects > e marque o checkbox "Use the 64 bit version of IIS Express for web sites and projects":
Answer: Adicionar a DLL diretamente pode ser um problema, sobretudo considerando que pode haver uma mudança de ambientes na hora de publicar sua aplicação.
Ao invés de adicionar a DLL diretamente, utilize o pacote NuGet oficial dos drivers da Oracle:
PM> Install-Package Oracle.ManagedDataAccess

Comment: Na dll que faz o acesso ao Banco a versão do .Net é a 3.5. Esse pacote não dá para essa versão. Sei que referenciar a DLL diretamente não é a melhor opção, mas é o costume utilizado até o momento na empresa. Em breve iremos mudar isso, mas até lá temos que usar assim.
Comment: Aí complica. O jeito é forçar 64 bits, conforme o @MarcusVinicius colocou. Não tem como usar um *framework* mais recente?
Comment: Por enquanto não, pois existem vários sistemas que são antigos e rodam sobre o 3.5. Consegui criar um novo utilizando o 4.0 para mostrar algumas vantagens, mas ainda assim não é possível mudar todos os aplicativos. Por isso a dll que é compartilhada deve manter essa versão.
Answer: Creio que achei o problema.
Eu estava testando um projeto que era uma aplicação de console, utilizando uma dll de acesso à banco de dados Oracle idêntica à do problema.
O arquivo .csproj estava configurado como abaixo:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
 <DebugSymbols>true</DebugSymbols>
 <DebugType>full</DebugType>
 <Optimize>false</Optimize>
 <OutputPath>bin\Debug\</OutputPath>
 <DefineConstants>TRACE;DEBUG</DefineConstants>
 <ErrorReport>prompt</ErrorReport>
 <WarningLevel>4</WarningLevel>
 <PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>

Entretanto, quando fui pesquisar como estava o arquivo .csproj da minha aplicação console, ela estava da forma abaixo:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
 <DebugType>pdbonly</DebugType>
 <Optimize>true</Optimize>
 <OutputPath>bin\Release\</OutputPath>
 <DefineConstants>TRACE</DefineConstants>
 <ErrorReport>prompt</ErrorReport>
 <WarningLevel>4</WarningLevel>
 <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>

A solução foi copiar essa seção do .csproj da dll, substituindo o trecho do .csproj da minha aplicação console pelo trecho da dll.
Após a substituição o problema estava resolvido. Creio que o VS2015 estava se perdendo quando salvava as dlls a serem utilizadas, salvando uma parte na pasta bin/Release e outra na pasta bin/Debug.
Answer: A resposta do @MarcusVinicius resolveu o meu problema para projetos web, porém continuei com um problema para aplicativos desktop, como windows forms e console aplication.
Observando a resposta do @HendrigWernner percebi a tag <Prefer32Bit>false</Prefer32Bit>. Olhei as propriedades do projeto e desmarquei esta opção nas propriedades do projeto, na aba build.

Fazendo esta modificação o projeto funcionou corretamente e o erro de conexão não apareceu mais.


Probability of getting between

Question: ...2 to 5 questions answered correctly, out of 20 of them? Each question has 5 choices. Probability of getting one right is 1/5. Probability of getting exactly 1 right is ${20 \choose 1} p^1  q^{19}$, with $p=P(\mathrm{right})$ and $q=P(\mathrm{wrong})$  (which I managed to understand and calculate). However how do I calculate for the problem above?
Comment: your are welcome. Note that right clicking on formula will present you pop-up menu with a choice to see the source.
Comment: @mpiktas: Nice to know that it is possible to see the source.
Comment: If you feel @mpiktas's answer helped you to solve your problem, then the best way to thank him is probably to upvote his response.
Answer: Hint: sum up  the probabilities. The probability that exactly $k$ answers are answered correctly is $${20 \choose k}\left(\frac{1}{5}\right)^k\left(\frac{4}{5}\right)^{20-k}.$$ In your case you have $k=2,3,4,5$.
Comment: You confirm the answer is aprox. 0.735 ? Seems the problem solution might be wrong.
Comment: @Queops, R code `pbinom(5,20,1/5)-pbinom(1,20,1/5)` gives 0.7350325, so yes.
Comment: @Queops, maybe the problem is to calculate the probability that exactly questions numbered 2 to 5 are answered correctly? Then probability should be calculated differently.
Comment: No no, it's just the number, doesn't matter which ones. And here I was trying to figure out what was wrong. Thanks so much for the help.

Probability of getting between


Advantages of dual formulation

Question: Why do we solve the dual form of the SVM in practice to obtain a classifier instead of the primal?
Answer: The primal formulation of SVM can be solved by a generic QP solver, but the dual form can be solved using SMO, which runs much faster.

Advantages of dual formulation


Por que Button dentro da Label não funciona. O Button não funciona como o esperado não ativa o Checkbox

Question: Eu estava fazendo um Menu que só aparece quando um checkbox está checado. E para alterar o estado desse checkbox eu preciso clicar na label dela, já que o input:checkbox está na verdade oculto.
O que acontece é que dentro da label eu coloquei um button, e dessa forma não consigo ativar o checkbox... Veja que segundo a documentação dentro da label são permitidos todos os Phrasing_contenthttps://developer.mozilla.org/pt-BR/docs/Web/HTML/Element/label
Sobre os: Phrasing Content
Repare que aqui tenho dos códigos, um que não funciona com o button dentro, e outro que funciona sem o button.
Então, porque a label como o button dentro não ativa o checkbox?

li{
  display: inline-block;
  background-color: red;
}
nav {
  display: none;
}
[type="checkbox"] {
  display: none;
}
[type="checkbox"]:checked + nav {
  display: block;
}
label {
  cursor:pointer;
}
<button>button</button>
<input type="checkbox" name="" id="btnx">

item 01

item 02

item 03

label
<input type="checkbox" name="" id="btny">

item 04

item 05

item 06

Answer: O problema é que o  possui apenas um filho interativo e, por padrão, a interação com esses elementos não devem ativar o . Veja o trecho da HTML Standard:

The label element's exact default presentation and behavior, in particular what its activation behavior might be, if anything, should match the platform's label behavior. The activation behavior of a label element for events targeted at interactive content descendants of a label element, and any descendants of those interactive content descendants, must be to do nothing.

Em tradução livre, "a apresentação e o comportamento padrão exatos do elemento , em especial qual deve ser seu comportamento de ativação, deve corresponder ao comportamento do  da plataforma. O comportamento de ativação de um elemento de  para eventos destinados a descendentes de conteúdo interativo de um elemento de rótulo e quaisquer descendentes desses descendentes de conteúdo interativo deve ser o de não fazer nada".
Ou seja, o clique no botão dentro do  não ativa o clique do , bem como em nenhum elemento interativo:

Alvo: <input type="checkbox" id="check">

Teste com o campo de texto:

    <input type="text">

Teste com o campo de checkbox:

    <input type="checkbox">

Teste com o campo de radio:

    <input type="radio">

Perceba que nenhuma interação com os elementos dentro do  ativa a seleção do checkbox.
Curiosamente isso não é um problema com o alvo do , pois se verificarmos o valor do atributo control no JavaScript, que possui uma referência ao elemento alvo, veremos que será o próprio <input type="checkbox"> como definimos com o atributo for.

const label = document.querySelector('label[for="btnx"]');

console.log(label.control);
li{
  display: inline-block;
  background-color: red;
}
nav {
  display: none;
}
[type="checkbox"] {
  display: none;
}
[type="checkbox"]:checked + nav {
  display: block;
}
label {
  cursor:pointer;
}
<button>button</button>
<input type="checkbox" name="" id="btnx">

item 01

item 02

item 03

label
<input type="checkbox" name="" id="btny">

item 04

item 05

item 06

Comment: Acredito que deva ser isso mesmo. Muito completa a resposta, []´s
Comment: Legal, a explicação técnica é exatamente essa. Fui ler a documentação e lá diz que o `tabindex` transforma qualquer elemento em "conteúdo interativo", mas se você colocar um `foo` dentro do label e clicar nele, irá ativar o label. Vai entender! rs...
Comment: @Sam, interativo acho que apenas no sentido de entrar na lista de navegação pelo teclado, talvez para outros fins não. Onde viu esta informação?
Comment: Nesta página: https://www.w3.org/TR/html5/dom.html#interactive-content, no item 126.96.36.199.7


Virtualization with xHCI-IOV

Question: I need some documents about it (must be detailed) and,is KVM support it????
If yes, how to enable it and use it?
My goal is to virtualize intel usb controller(onboard 0000:00:14.0) and distribute it to 2 guests.
Passthrough can only be used by 1 guest.
Comment: Concerning the documentation, the specification is available [here](https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf). Chapter 8 details the virtualization.
Answer: xHCI-IOV is an xHCI-specific extension to SR-IOV (Single Root I/O Virtualization), the PCI specification that lets a single PCI device expose multiple functions.
Of the functions, one is a physical function and the others are virtual functions. Virtual functions have some limitations regarding the features they can support, but the spec says that no function cannot interfere with the functioning of the others.
However, I don't think Linux supports it.  The XHCI_EXT_CAPS_VIRT symbol only appears in debug code, and there should probably be a separate driver for virtual xHCI functions as well.

Virtualization with xHCI-IOV


haproxy multihost with ssl acl

Question: how can I use ACL rules in haproxy (1.5dev19) for server multiple hosts with own ssl certificate for each??
I have 3 backends with multiple domains all on one IP address.
nodejs (http/https/ws/wss)
nginx (http/https) (reverse proxy for apache now)
apache (http)
I want to put haproxy before them and make it serve ssl connections.
is it possible with haproxy acl or I have to use something like stud in front of haproxy?
something like:
frontend http-in
    bind *:80

    acl host_domain1 hdr(host) -i domain1.lt
    use_backend nginx_web_http if host_domain1

frontend http-in
    bind *:443

    acl host_domain1 hdr(host) -i domain1.lt
    use_backend nginx_web_https if host_domain1

backend nginx_web_https
    mode http
    ssl crt /etc/ssl/domain1/ crt ./certs/ prefer-server-cipher
    option httplog
    option httpclose
    server nginx 192.168.2.101:8080 check

backend nginx_web_http
    mode http
    option httplog
    option httpclose
    server nginx 192.168.2.101:8080 check

Comment: SNI is not supported by legacy browsers. Save the trouble and use 3 IPs with your haproxy.
Answer: You can actually do this with your version of haproxy.  I've blogged about it here
Here's the example:
global
   log 127.0.0.1  local0
   log 127.0.0.1  local1 notice
   #log loghost   local0 info
   maxconn 4096
   # chroot /usr/share/haproxy
   user haproxy
   group haproxy
   daemon
   #debug
   #quiet

defaults
   log   global
   mode  http
   option   httplog
   option   dontlognull
   retries  3
   option redispatch
   maxconn  2000
   contimeout  5000
   clitimeout  50000
   srvtimeout  50000

# Host HA-Proxy web stats on Port 3306 (that will confuse those script kiddies)
listen HAProxy-Statistics *:3306
    mode http
    option httplog
    option httpclose
    stats enable
    stats uri /haproxy?stats
    stats refresh 20s
    stats show-node
    stats show-legends
    stats show-desc Workaround haproxy for SSL
    stats auth admin:ifIruledTheWorld
    stats admin if TRUE

frontend ssl_relay 192.168.128.21:443
    # this only works with 1.5 haproxy
    mode tcp
    option tcplog
    option socket-stats
    # option nolinger
    maxconn  300

    # use tcp content accepts to detects ssl client and server hello.
    # acl clienthello req_ssl_hello_type 1 -> seems to not work

    tcp-request inspect-delay 5s
    tcp-request content accept if { req_ssl_hello_type 1 }

    use_backend ssl_testdomain_prod if { req_ssl_sni -i www.testdomain.nl }
    use_backend ssl_testdomain_stag if { req_ssl_sni -i test.testdomain.nl }

    default_backend ssl_testdomain_stag

backend ssl_testdomain_stag
   mode tcp
   #option nolinger
   option tcplog
   balance roundrobin
   hash-type consistent
   option srvtcpka

    # maximum SSL session ID length is 32 bytes.
    stick-table type binary len 32 size 30k expire 30m

    # make sure we cover type 1 (fallback)
    acl clienthello req_ssl_hello_type 1
    acl serverhello rep_ssl_hello_type 2

    # use tcp content accepts to detects ssl client and server hello.
    tcp-request inspect-delay 5s
    tcp-request content accept if clienthello

    # no timeout on response inspect delay by default.
    tcp-response content accept if serverhello

    # SSL session ID (SSLID) may be present on a client or server hello.
    # Its length is coded on 1 byte at offset 43 and its value starts
    # at offset 44.
    # Match and learn on request if client hello.
    stick on payload_lv(43,1) if clienthello

    # Learn on response if server hello.
    stick store-response payload_lv(43,1) if serverhello

    #option ssl-hello-chk

    server x_testdomain_stag 22.214.171.124:443

backend ssl_testdomain_prod
   mode tcp
   #option nolinger
   option tcplog
   balance roundrobin
   hash-type consistent
   option srvtcpka

    # maximum SSL session ID length is 32 bytes.
    stick-table type binary len 32 size 30k expire 30m

    # make sure we cover type 1 (fallback)
    acl clienthello req_ssl_hello_type 1
    acl serverhello rep_ssl_hello_type 2

    # use tcp content accepts to detects ssl client and server hello.
    tcp-request inspect-delay 5s
    tcp-request content accept if clienthello

    # no timeout on response inspect delay by default.
    tcp-response content accept if serverhello

    # SSL session ID (SSLID) may be present on a client or server hello.
    # Its length is coded on 1 byte at offset 43 and its value starts
    # at offset 44.
    # Match and learn on request if client hello.
    stick on payload_lv(43,1) if clienthello

    # Learn on response if server hello.
    stick store-response payload_lv(43,1) if serverhello

    #option ssl-hello-chk

    server x_testdomain_prod 188.8.131.52:443

This example implies that you terminate your SSL on the webserver backends, I haven't tried to do this with haproxy ssl termination yet.
If that is what you want, maybe this example helps getting it to work.
there is another example that uses use_server instead of use_backend here
Comment: Thanks, this finally worked for me, after hours of searching online
Answer: I don't think haproxy will allow you to specify a per-backend SSL certificate for each incoming request, rather you'd have to have a combined certificate that allows for multiple domain names (SNI).
Here's a guide on using SNI with haproxy, where all the certificates are actually hosted by the haproxy server, not the backend instances: https://trick77.com/haproxy-and-sni-based-ssl-offloading-with-intermediate-ca/
Also see the example at the end of this section: http://cbonte.github.io/haproxy-dconv/configuration-1.5.html#4.2-use-server
Comment: Option 1 will half-work, but you won't be able to decide which backend to send to from haproxy, since the packet is encrypted.
Comment: SNI is good choice, but I want to use certificates that I already have.
So i see 3 options.
1) use haproxy in tcp mode and server ssl from apache or nginx (i dont know if it's possible)
2) use nginx in front of haproxy, which is not really cool.
3) use another reverse proxy like pound which I don't know if can handle my requirements :D
Comment: From the article, it seems like you can place all your existing certs in /etc/haproxy/certs.d, and use them via HAPRoxy to decrypt the traffic, and then send to the correct backend.
Comment: And how is this solve by apache? Because in apache i can use for each virtualhost different certificate. Is apache using SNI for this? As I'm reading articles all I here are possible problem but this is working fine on apache.
Comment: I'm not 100% sure on Apache, but here's an article on how to do the same with nginx: https://www.digitalocean.com/community/articles/how-to-set-up-multiple-ssl-certificates-on-one-ip-with-nginx-on-ubuntu-12-04 It also uses SNI, as do most other multi-ssl certificate guides will.
Comment: Mike, why will option 1 half-work? Isn't the exact purpose of SNI to peek into SSL-encrypted packets and forward them to the actual HTTPS backend?
Comment: @ErikAllik Option 1 is the one referred to in http://serverfault.com/questions/561479/haproxy-multihost-with-ssl-acl/561493?noredirect=1#comment652153_561493 - "use haproxy in tcp mode and server ssl from apache/nginx" - if haproxy is balancing in TCP mode, and has no notion of the contents of SSL (or SNI) then it cannot use rules that are hostname-based to route traffic as it is unable to terminate the SSL connection and inspect the packets.
Comment: haproxy 1.5 (compiled against up-to-date ssl) now supports termination with multiple certificates stored in 1 directory.

haproxy multihost with ssl acl


Find $x$ and $y$ - Why is there no answer?

Question: I need to find $x$ and $y$ from the following equations:
\begin{eqnarray*}
7x-3y &=& 8 \\
14x-6y &=& 21
\end{eqnarray*}
I my book it says there's "no answer". Can someone explain to me - why?
Thanks
Comment: Multiply the first equation by 2, and you'll see why.
Comment: Please improve the question by includes your thoughts on the problem and any attempts you have made to solve it. This helps others identify where you have difficulties and helps them write answers appropriate to your experience level.
Comment: An instructive exercise would be to put these two equations in point-slope or slope-intercept form and graph them on graph paper. That might give you a little insight into why the equations can't both be satisfied.
Comment: Because this amounts to $2\cdot8=21$ !
Comment: @Sahar Avr You can draw graphs and see
Answer: Say we assume that $x$ and $y$ satisfy the first equation. Then $7x -3y =8$ so if we multiply by 2 we get $14x -6y =16$ which contradicts the second equation.
Answer: Multilpyng the first e quation by $-2$ and summing the two equations you find: $0=5$ that is impossible.
In other words: any couple $x,y$ that satisfies the first equation, cannot satisfies the second.
Answer: Assume that both equations are true (for the moment). Using the first equation, we find $x=(8+3y)/7$. Take this solution, and plug it in to the second equation. The second equation becomes $2(8+3y)-6y=21$. The terms with y cancel, and we are left with 16=21 which is obviously wrong. But we didn't make a math error. In this scenario either: a.) one of the two equations we started with must be incorrect (which isn't the case since it was given at the start or b.) no set of {x=..., y=...} will solve the two equations simultaneously.
Answer: If you put your two equations in $y=mx+b$ form. You will see that both of there slopes or "$m$" values are the same. Two lines with the same slope are guaranteed parallel if they are not the same lines. Therefore because parallel lines never intersect, there were will not be a point $(x,y)$ that satisfies both equations. The point $(x,y)$ is what you are looking for.
Answer: First of all, multiplying the first equation gives $14x - 6y = 16$, it should not be 21.
Secondly, if you can, please check whether $(8,21)'$ belongs to the column space of the matrix $A = \begin{pmatrix}7 & -3\\ 14 & -6\end{pmatrix}$ or not. I think it must not belong to the column space of $A$, which means that the system is not consistent. Hence it has no solution.
Answer: Double both sides of the first equation and you get
\begin{eqnarray*}
14x-6y &=& 16 \\
14x-6y &=& 21
\end{eqnarray*}
Both lines have a slope of $\frac{14}{6} = \frac 73$, so they are parallel to each other. Their y-intercepts are $-\frac{16}{6} = -\frac 83$ and $-\frac{21}{6} = -\frac 72.$ since they are parallel and have different y-intercepts, they never intersect.
Or, much more simply, this implies that $16 = 21$.
So there is no solution.


Evento onchange no Input type="text" em uma aplicação Blazor

Question: Estou desenvolvendo uma aplicação em Blazor e consegui fazer um InputText juntamente com um button realizar uma pesquisa em uma api criada por intermédio de scafolding com Entity Frameworks.
Segue simples formulário abaixo.
Hello @motorista !!
<input type="text" @bind-value="motorista" />
<button class="btn btn-primary" @onclick="PesquisaMotorista">Click me</button>

@code {

    private string motorista = null;

    Veiculo[] veiculos { get; set; }

    protected async Task PesquisaMotorista()
    {
        veiculos = await http.GetJsonAsync<Veiculo[]>($"api/veiculos/motorista/{motorista}");
    }
}

Gostaria de utilizar o evento onchange no input, para não mais necessitar do button do formulário acima.
Abaixo segue o mesmo formulário sem o button e com o evento oninput que não esta funcionando conforme o evento onchange.
Como faço para utilizar no exemplo abaixo o evento onchange?
 Hello @motorista !!
 <input type="text" @bind-value="motorista" @oninput="PesquisaMotorista" />
 @code {

    private string motorista = null;

    Veiculo[] veiculos { get; set; }

    protected async Task PesquisaMotorista()
    {
        veiculos = await http.GetJsonAsync<Veiculo[]>($"api/veiculos/motorista/{motorista}");
    }
 }

Alguns testes realizados até o momento
Com onclick digito o texto e dou um clique no input ai a pesquisa é realizada
<input type="text" @bind-value="motorista" @bind-value:event="oninput" @onclick="PesquisaMotorista" />

Com onchange digito o texto e dou um enter, ai a pesquisa é realizada
<input type="text" @bind-value="motorista" @bind-value:event="oninput" @onchange="PesquisaMotorista" />

Solução obtida com o evento onkeyup
<input type="text" @bind-value="motorista" @bind-value:event="oninput" @onkeyup="PesquisaMotorista" />

Comment: coloca sua solução em uma resposta, e marca a sua própria resposta como a solução.
Comment: Você está utilizando algum framework js? tipo jquery?
Comment: Não estou utilizando nenhum tipo de frameworks js e jquery - estou procurando usar os conceitos do Blazor, tem que utilizar alguma coisa ?
Comment:  - neste exemplo o evento onchange esta funcionando mas modo estranho, temos que digitar primeiro um texto e dar enter, ai realiza a pesquisa
Comment: Solução obtida com o evento onkeyup, veja exemplo na atualização do post de perguntas
Answer: Obtive resultado com o evento onkeyup
<input type="text"
       @bind-value="motorista"
       @bind-value:event="oninput"
       @onkeyup="PesquisaMotorista" />

@code {

private string motorista = null;

Veiculo[] veiculos { get; set; }

protected async Task PesquisaMotorista()
{
   //Acessa a rota personalizada na VeiculosController
    veiculos = await http.GetJsonAsync<Veiculo[]>($"api/veiculos/motorista/{motorista}");
}

}
Api da VeiculosController
// GET: api/Veiculos
[HttpGet("motorista/{motorista}")]//Rota personalizada
public async Task<ActionResult<IEnumerable<Veiculo>>> GetVeiculos(string motorista)
{
      return await _context.Veiculos
                   .Where(v => v.Motorista.Contains(motorista))
                   .ToListAsync();
}


CSS selector for element before another element/remove list-style-type of nested uls

Question: I have nested uls like this:

1...

2...

3.1...

4...

li_3 displays a bullet but I don't want it to since its children li already do (there are 2 bullets side to side near li_3_1).
So I wanted to select all the li having a ul as a direct child, something like this:
ul < li {
  list-style-type: none;
}

But of course the selector < doesn't exist. What solutions do I have?
Answer: If you want to remove list-style in nested ul, this selector may be helpful.
ul > ul {
    list-style: none;
}

Comment: Nah I do want the li in the nested list to have bullets, if I do this it removes all the bullets in the nested lists.
Answer: You could just call the li directly using it's ID in CSS using this code:
#li_3 {
  list-style-type: none;
}

Or if there are multiple you could just add a class to every li you want the bullet removed from and use the same code.
working:

#li_3 {
  list-style-type: none;
}

1...

2...

3.1...

4...

Comment: I would like a generic solution if possible.
Answer: Found it:
li:has(ul) {list-style-type: none;}


How to evaluate $3^{123}\!\bmod 3\,$ and $5^{123}\! \bmod 2$ and similar?

Question: I hope someone can explain me how to solve modulo -equations ,e.g.
$5^{123} \mod 2$
It was a little bit overwhelming for me to get a feeling for solving such equations.I don't how and when i have to use  one of these great theorems like, Fermat's little ,Euler totient function,Chinese remainder theorem a.s.o.So I decide to  ask you guys for helping  me with my stacks.
Comment: In this case , the result is obvious : $0$
Comment: Ok,you are right.It is a bit trivial example.
Comment: @Gui Since this one is closed, I posted in another similar one, vote if you like https://math.stackexchange.com/a/3364841/399263
Comment: Tip for the general case : Use the chinese remainder theorem and the Euler theorem (sometimes Fermat's little theorem is already sufficient)
Comment: Maybe 5^123 mod 2 should be better.
Comment: I heard in this context about this three( not so trivial ) theorems but  hoping I get the algorithm  from someone  who explaining it to me by an example
Comment: @Gui A few points. This question will be "closed" soon, and (somewhat later) deleted. Why? Partly because the particular example you picked initially was so trivial. But mainly because you tried to modify it in the comments instead of in the question. Comments are NOT for modifying the question. They are for others to get you to clarify the question. If the question needs changing, you should edit it, not make comments on it!
Comment: $3$ is easy if $k|3$ then $3^n\equiv 0$.  If $k\equiv 1 \pmod 3$ then $k^n\equiv 1 \pmod 3$ and if $k\equiv -1 \pmod 3$ then $k^n\equiv (-1)^n\pmod 3$.  ANd for $5^{123}\pmod  2$ then $5\equiv 1 \pmod 5$.  But in general, Euler's Th. and C.R.Theorem can do it.
Comment: This shouldn't be on hold but it is a duplicate (although I'm too lazy to find it).  But 1) If $p$ is prime and $k$ is not a multiple of $p$ then Fermat's little Theorem says $k^{p-1}\equiv 1 \pmod p$ and so if $n \equiv m \pmod{p-1}$ you have $k^n\equiv k^m \pmod p$. so example $7^{5342}\pmod {11}\equiv 7^{534*10 + 2}\equiv (7^{10})^{534}*7^2\equiv 1^{534}*7^2\equiv 1*7^2\equiv 7^2\equiv 49\equiv 5\pmod{11}$.
Comment: 2) If $n$ isn't prime but $\gcd(a,n)=1$ then Eulers theorem says that $a^{\phi(n)}\equiv 1\pmod n$ where $\phi(n)=$ number of integers less than $n$ that are relatively prime to $n=\prod_{p_i^{a_i}|m} (p_i-1)p_i^{a_i-1}$. So for example $7^{5342}\pmod {40}$ we have $\phi(40=2^3*5)=2^2*4=16$ and $5342\equiv 14\pmod{16}$ so $7^{5342}\equiv 7^{14}\pmod 40$. And well, $7^2\equiv 49\equiv 9\pmod {40}$ and $9*9\equiv 81\equiv 1\pmod {40}$ and $7^{14}7^{2}\equiv 1\pmod {40}$ we have $7^{14}\equiv 9\pmod {40}$.
Comment: But google 1) Euler's theorem, 2) Fermat's Little Theorem and 3) Chinese remainder Th.  and see how this in combination can solve all.
Comment: firstly ,I must thank you all for your amazing help.!!But know I must go offline to work  on your specific  comments .
Comment: If you want to solve, say $6^{5342}\pmod {33}$ then solve $6^{5342}\pmod{3}$ and $6^{5342}\pmod {11}$.  $6^{5342}\equiv 0 \pmod {3}$ and $11$ is prime so by FLT $6^{10}\equiv 1\pmod 11$ and $6^{5342}\equiv 6^{2}\equiv 36\equiv 3\pmod {11}$.  So by Chinese Remainer The there is one solution to $3\pmod{11}$ and $0\pmod 3$ mod $33$ and that is $3\pmod {33}$ so $6^{5342}\equiv 3\pmod{33}$.
Answer: The answer is pretty obvious: it is $0$ ($3^n$ where $n$ is a positive integer obviously divides $3$ and thus has a remainder of $0$). If you were asking for$\pmod{10}$, then we would apply Euler's Totient's Function, $\phi(n)$ and then simplify from there. For your next question, since $5\equiv 1\pmod 2$, we have that $$5^{123}\equiv 1^{123}\equiv 1\pmod{2}.$$
Comment: I will try it for $5^{123}$ mod 10:
Comment: 5 is the answer then. As any odd multiple of 5 ends in a 5 in base 10.


Handle categorical class labels for scikit-learn MLPClassifier

Question: I have a handwritten dataset for classification purpose where the classes are from a-z. If I want to use MLPClassifier, I think I cannot use such categorical classes directly because MLP implementation in scikit-learn only handles numerical classes. Thus, what is the appropriate action to do here? How about converting these classes to be numbered from 1-28, does it make sense? If not, does scikit-learn provide special encoding mechanism for class labels to handle this case (I guess one-hot encoding is not the option here)?
Thank you
Answer: Using MLPClassifier you can do exactly what you suggested, that is represent classes as integers from 0 to 27 (in the case of 28 classes). Here is an example with MLPClassifier and MNIST dataset.
You can use sklearn to transform data to such format with Label Encoder.

Although the above will solve your problem, I believe MLPClassifier actually transforms the numerical labels to one-hot vectors for the neural network. Using lower level neural network libraries you would have to do it yourself.  This is because in multi-class classification the last layer's activation is softmax, which outputs a vector of n (number of classes) elements with continuous (0, 1) values. This makes sense as an indication of probability of observing a given class. To transform numerical labels to one-hot vectors with sklearn you can use Label Binarizer. When we expect a neural network to predict a numerical value we're really talking about a regression, not classification.


Exact timing of piano pedal re-engagement

Question:

In this example, do I release pedal BEFORE beat 1 of the second measure, or ON beat 1 of the second measure?
Comment: I hate this pedal notation precisely because of the ambiguity. I much prefer the modern notation with a ____^____ mark for pedal pumps. (The caret shouldn't be italicized or bolded.)
Answer: The pedal should be both released and re-engaged in the moment after you play beat 1: Play note + quickly release and re-engage pedal.
Releasing before, or even on, beat 1 will leave a gap of silence that interrupts the melody.
Comment: I don't know whether the gap between the previous pedalling line and the "Ped." following it means that you let go of the pedal on Beat 3.5 or the following Beat 1. It looks like Beat 3.5 to me, which is why I never use "Ped." notation in my scores.
Comment: @Dekkadeci - the graphic way it's printed is certainly not intuitive. It's pretty poor, as that's how NOT to pedal. Just like a broken pencil - pointless...
Answer: You 'change' pedal on the new note.   Up-down as you play it.
Actually, pedalling would ideally be be rather more subtle than is printed here.  I'd do a light 'change' on a lot of the non-chord melody notes.  Especially on the G♯ in bar 6.  Don't want that smeared with the surrounding A's.   There's even a lot to be said for sustaining the bass notes with the fingers and using no pedal
at all.
Answer: The concept of pedalling in situations such as these is to sustain notes that can't be held with separate fingers.
With very basic triads such as these, there's pretty well no reason to use the pedal at all. What it will do is richen the sound, as it allows the strings to vibrate in sympathy - those which match harmonics of the played notes. It also allows playing to get sloppy, when notes are released too soon, because you can!
The pedalling here will be such that as you press the first note in a new bar, the pedal is quickly let go, and re-pressed before you let go of that new first note. So the change will happen all on that first note. Play, up pedal, down pedal, play next note.
I guess this is actually a pedalling exercise. Half of it (most of it) is unnecessary, as all notes in a bar can be sustained by simply holding them down - there are no big stretches with triads. And most bars have their notes which make up a particular chord. That's an important factor to bear in mind when pedalling. Notes which don't belong with a particular harmony are best unpedalled. As in bar 6, pedalling the A and G♯ together bleeds one into the other. Not a good technique here.
And that F in bar - maybe F♯?
Comment: I'm guessing the pedalling in this is to generate a bit of a 'harp vibe' with plenty of over-ring & harmonic going on.
Comment: @Tetsujin - maybe, but well over the top. Every two bars would suffice.
Comment: I've seen scores with this much pedal and this much pedal pumping, but all of them were either "con pedale" or could definitely have used that direction.
Comment: @Dekkadeci - I'd have thought that unless it was a very special piece, any half-decent player would be able to work out pedalling , certainly for run-of-the-mill pieces.
Comment: @Tim - No, the rules I learned in piano lessons are to never use pedal unless directed, and to arpeggiate and not pedal any chords that are too wide (i.e. the way to "work out pedalling" is to not pedal by default).
Comment: @Dekkadeci - o.k., we''ll agree to differ! To be honest, it happens automatically - or not - when I lay. There again, a lot of what I do isn't off dots. And when it is, like big band orchs, pedal marks are never there, so I have to guess, and when it happens, it happens - half pedalling, catch pedalling, whatever.


Change admin menu item

Question: How would I unset this menu item: Content > Add Content > Article
I would like to programmatically disable that path-page (node/add/article), or even better use that path for another page. I tried using hook_menu_alter(&$items). Does not work for that path, while on others custom menu items that I created using hook_menu it does work.
Update:
This is simple code that I tried (unset the $item key):
<code>mymodule_hook_menu_alter(&$items) {
     unset($items['node/add/article'])
}
</code>
Comment: hook_menu_alter should do the trick -- could you show us the code you tried?
Comment: Hi anschauung, I thought so as well... I updated the question with code example
Answer: Admin menu is an yet another menu, which builds programmatically. So you can modify items using hooks registered for any menus.
If you would like to disallow atricle creation by specified role, just remove appropriate permission.
If this does not helps, look at admin_menu_menu_alter() and theme_admin_menu_links().

Change admin menu item


Explain me this SELECT dbo.TableName(@variable) statement

Question: I'm working on one program that I need to modify a little. There's one SQL statement I don't understand what it does (or basically how it does it).
string query = "SELECT dbo.BusinessMinutes(@start,@end,@priorityid)";

SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add("@start", SqlDbType.DateTime).Value = start;
cmd.Parameters.Add("@end", SqlDbType.DateTime).Value = end;
cmd.Parameters.Add("@priorityid", SqlDbType.UniqueIdentifier).Value = priorityId;

SqlDataAdapter READER = new SqlDataAdapter();
READER.SelectCommand = cmd;

DataTable table = new DataTable();
READER.Fill(table);

if (table.Rows.Count == 1)
{
    minutes = (int)table.Rows[0][0];
}

So can someone explain me the SELECT statement there. The end result (minutes) is as expected so it works but that syntax confuses me. Is this somehow equal to SELECT * FROM dbo.BusinessMinutes WHERE...
Is this commonly used and does this syntax has some special name so I could name my question better? Thank you in advance.
Comment: `dbo.BusinessMinutes` is a *User defined function*
Answer: It is not a table name. I think you call into a FUNCTION.
how to create and call scalar function in sql server 2008
has more explanations and examples.
Comment: This is a Scalar function, not a table function.
Comment: Corrected. Overlooked the return in the call.
Comment: Yeah. and now i even have a stackoverflow internal link ;)
Answer: dbo.BusinessMinutes has to be a UDF (User Defined Function) that returns a simple scalar value based on a starting date, an ending date and a priority indicator.
Scalar functions, be it UDF or native, can be used in a SELECT statement to produce a field in the returned resultset. Thus, the code you have is perfectly legal.
For more information about scalar UDF, read this MSDN article.
As a side note, a better implementation for that code would be this:
string query = "SELECT dbo.BusinessMinutes(@start,@end,@priorityid)";

using (SqlCommand cmd = new SqlCommand(query, con))
{
    cmd.Parameters.Add("@start", SqlDbType.DateTime).Value = start;
    cmd.Parameters.Add("@end", SqlDbType.DateTime).Value = end;
    cmd.Parameters.Add("@priorityid", SqlDbType.UniqueIdentifier).Value = priorityId;

    // assuming connection is already open
    using (SqlDataReader reader = cmd.ExecuteReader())
    {
        if (reader.Read()) minutes = reader.GetInt32(0);
    }
}

If all you want is to init the minutes variable, using SqlDataReader will be more efficient and performant than creating a SqlDataAdapter and a DataTable. The using statements will also make sure your SqlCommand and SqlDataReader objects gets disposed of properly.


Formatting time in JSON to display in ag-grid

Question: I am using ag-grid with angular 5.
My goal is to have a grid with some columns where one of the columns is a "Time" column and shows the time in this format "hh:mm".
All the data I am displaying in the grid comes from a JSON object. Right now the time I am displaying looks like this "2018-04-28T08:16:07.632Z".
I have looked a bit on Angular Pipes but I can't figure how to use it for a JSON object where I need one column formatted, but it needs to be "bound" to the correct cells in the other columns. I think all the formatting have to be done in the typescript code.
Have any of you done something similar or have an idea have to do this?
Answer: I have not found an identical question on this and it was very simple so I will explain what I did:
In the coulmDefs I added valueFormatter: HomePage.timeFormatter
columnDefs = [
    {headerName: 'Time', field: 'time', valueFormatter: HomePage.timeFormatter}]

Then I added this function to the same file:
// I take the substring of this '2018-04-28T08:16:07.632Z' and I get '08:16'
static timeFormatter(params) {return params.value.substring(11, 16);}

Keep in mind that this only works when you know the DateTime comes in the same format every time.
Answer: In Ag-Grid you have the grid-options that you define at some point:
this.gridOptions = {
  // ...
  columnDefs: [    'DATE': {
     headerName: 'Datum',
     field: 'myJson.date',
     cellRenderer: (params: ICellRendererParams) => params.value ?
        `<div class="my-awesome-date-style">${this._customDatePipe.transform(params.value)}</div>` : ''
  }],
  // ...
}

To use a pipe (be it your own CustomDatePipe or Angular's native DatePipe) inside a typescript-file (e.g. outside of a template), you have to inject it:
constructor(private readonly _customDatePipe: CustomDatePipe) {}

and include CustomDatePipe (or CommonModule for native DatePipe) in your module's providers.
The pipe's transform() method will format the part of the JSON (in your case the date-property) you want to display as soon as AgGrid tries to render it.
PS: If you are using an AgGrid CellRenderer-Component, you can just use the pipe directly in it's template without injection (need to put in providers still though):

${params.date | customDatePipe}

Comment: This could probably work, but because of your answer I stumbled upon this doc https://www.ag-grid.com/javascript-grid-cell-rendering/ that says "If you just want to do simple formatting of the data (eg currency or date formatting) then you can use colDef.valueFormatter", so I decided to go for the valueFormatter.
Answer: This is for Angular2+ version. If you are using moment library in your application. Then the job is really simple
In your .ts file:
    import * as moment from 'moment';

{
    headerName: 'End Date',
    field: 'endDateUTC',
    minWidth: 80,
    maxWidth: 100,
    valueFormatter: function (params) {
        return moment(params.value).format('hh:mm');
    },
},

And the output you will get is:
End date:
10:55
Using moment library you can configure many date formats including locale support.
Hope this will be helpful to some one.
Answer: Maybe you will create a Pipe and also mommentjs within this Pipe.
Comment: From my experience Angular's Date-pipe can do most of what people use moment.js for. I would recommend trying to use the DatePipe either directly or as an injected service in a `CustomDatePipe` before integrating an additional framework.
Comment: Excelent cooment, I have present for others


ImageMagick: How to not save background color information?

Question: How - if at all - is it possible to create a fully transparent image with ImageMagick?
The problem: It's always getting a background color, white by default (as per docs)
More specifically, looking for: Not even a single background color stored in the file, so that every viewer that supports PNG will see it as a fully transparent image - just like the input
My commandline is:
magick convert in.png -trim out.png
Comment: See also the example at the end of this answer... https://stackoverflow.com/a/32417234/2836621
Answer: Standard "transparent" is a zero valued alpha channel over a black background, so rgba(0,0,0,0). If you want to ensure such, then make the background black under the transparent areas. So for example from your command assuming in.png has some transparency:
magick in.png -background black -alpha background -trim +repage out.png

Note that in ImageMagick 7, use magick, not magick convert.
Comment: Hm, that is already a good hint incase there's nothing better, but if I open that in Picasa Photo Viewer (for example) it shows a black background and no transparent (like it does in image editors)

If I save it with [this feature](http://justpic.info/images4/29b3/2020032415_13_18ExportImageasPNG.png) in Gimp, it's displayed correctly (though the output is then also more than 3 times larger):
Comment: When you use -trim and save to PNG, you should add +repage to remove the virtual canvas. Otherwise the image will still display with the part you trimmed off. I have added that to may command above. See the command above and try that again with the +repage added before saving to PNG


How to automatically delete the key line from known_hosts when a session is closed?

Question: I'm working on automating a whole environment by using AWS CloudFormation and during the testing period I'm required to kill and re-create the servers very often.
The servers are configured to set their IP addresses from a dedicated pool which means that on the first time I connect to them I have to accept the host key checking (the yes/no question) and then when I delete the server and re-create it, I have to delete the server's key from the known_hosts file each time.
My question is:
Is it possible to delete the key from known_hosts automatically when I close the session? is there such a setting which I've missed?
Comment: Just as a pointer: The answer of @ptman was posted earlier and contains the same content as the one you've accepted.
Answer: You can configure ssh to use multiple files for the known_hosts and new entries only get added to the first, by setting that to /dev/null effectively they won't get saved:
UserKnownHostsFile /dev/null ~/.ssh/known_hosts

Combine it with your personal favourite of asking or automatically accepting unknown keys:
StrictHostKeyChecking no

You probably want to those options restricted to only (specific hosts in) your DEV environment rather than global defaults, as they will take away quite a bit of the security key checking provides so in your ~/.ssh/config:
Host *.dev.example.com
    IdentityFile ~/.ssh/id_rsa.dev.example.com
    UseKeychain yes
    User hbruijn-adm
    StrictHostKeyChecking ask
    UserKnownHostsFile /dev/null ~/.ssh/known_hosts

Comment: Please write one or two lines regarding the risks / disadvantages this is introducing
Comment: @gf_ You're of course right that doing so is insecure, but you can mitigate the impact somewhat as I updated above.
Answer: You could do:
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -l user host

StrictHostKeyChecking=no is of course a bad idea, since it will automatically accept all hosts keys
UserKnownHostsFile=/dev/null will not save anything to your normal known_hosts file.
Comment: Please write one or two lines regarding the risks / disadvantages this is introducing.
Answer: The proper way would be to put the SSHFP (automatically) into the DNS, sign the zone via DNSSEC and let SSH handle the rest, especially, if you need to do this very often and the keys are changing constantly.
Comment: Will ssh(1) actually verify the DNSSEC signature in order to trust the SSHFP record or will it blindly trust the system resolver?
Comment: @ptman I think (but don't take this for granted) it will rely on the system resolver. The response needs to have the `ad` bit set.
Comment: Sure, but the interface to the resolver (res_nquery(3)?) doesn't tell ssh(1) if the ad bit was set or not, or does it?
Comment: @ptman I can't explain the inner workings: I use this myself, but didn't dig into the code. All I can say is, that it makes a difference, it the `ad` bit is set or not. Without `ad`, `SSH` will print `Matching host key fingerprint found in DNS.` and will still ask you to manually accept or decline the fingerprint.


Caching images for a Middleman site

Question: I have a Middleman app deployed to GitHub pages. The app hits the Twitter API (GET users/show) every time a user visits the homepage to display our team. The Twitter API is rate limited (and I'm sure this is poor practice anyway) so, if there is a large number of visitors in an hour, the team comes up empty.
If this were a Rails app, caching the images to the server regularly would be fairly straightforward but how would you do this for a static site? Could you cache the images to an external service once per day or something?
Answer: You can have some cronjob for this task which will fetch data from twitter and, for example, generate html or some other kind of media and upload it to github.
Actually, you might want to check IronWorker for that job, so you'll don't need your own server and with few-fetches-per-day load it'll be free for you.
Comment: Cool, just had a read, that lloks like it will work. Thanks @iced
Comment: If you like IronWorker, you may want to check out IronCache for storing the data, too. ;)

Caching images for a Middleman site


How do you manage multiple similar Apache configurations?

Question: Right now I'm using Mercurial to synchronize similar Apache configurations between the production and development servers. A patch queue on top of that adds in the changes and extra stuff required for the development server.
The scheme works but it could be better. I always get merge conflicts when I reapply the patch queue after pulling changes from the deployment configs. I feel like I am always making identical changes to the separate http:// and https:// config files.
How should I factor out the identical bits between my :80 and :443 Apache configs and more cleanly add in the "extra bits" to the development config?
Answer: You might want to consider using the Include feature of Apache's config system. Then separate out the elements of your config that are specific to an individual machine into a _local.conf that should make the merging simpler
Answer: Apache has an  option to selectively enable certain configurations. This lets you use the same config file for development and production. Start apache with -DDEVELOPMENT or -DPROD, and you can use  tags for configuration that is specific to the dev server. This way you won't get any merge conflicts (unless you change both dev/prod config files before merging of course)
Comment: +1 Useful. Here is an example:

   #Anything you want in here

Then run "apache -DCustom" or "httpd -DCustom" depending on linux flavour.
Answer: This may be old school, but my stage and production apache configs were identical, except that some of the parameters within each were different (ie: IP address).  I used a perl wrapper around a start script to 'fill in the blanks' in a methodical way.  The actual base config 'templates' were indeed identical.
This insured that the stage apache was functionally identical to the production apache.
Sharing this production config with development enabled them to test as close to production as they wanted (and they wanted to, because they would not get into production unless their stuff 'worked' in stage.  We supplied help occasionally (for mod_rewrite rules for example) and left the rest up to the developer.
Difference between this particular setup and other places I've worked are:
developement staff were encouraged to dabble in areas not directly related to the programs they worked on... ie: they may work on back-end applications (thus wouldn't need to know much about apache) yet they configured their own apache servers (in fact built their own desktops, but that's a line crossed IMnsHO).
operations actually drove a lot of the infrastructure, rather than just running it.


Why is the component of a differentiable function also differentiable?

Question: I'm self studying with the Spivak book, and I'm trying to see a result that I know to be true but I'm having some time seeing how the maths pans out.  Supposing a function $f$ is differentiable.  Then given the projection operator $\pi^i$ where $\pi^i(f)=f^i$, $f^i$ must also be differentiable.
I've been trying to work it out by applying the chain rule
$$Df^i(a)=D(\pi^i\circ f)(a)=D\pi^i(f(a))\circ Df(a)$$
since the projection operator is linear, it's derivative is simply itself
$$Df^i(a)=\pi^i(f(a))\circ Df(a)$$
$$Df^i(a)=f^i(a)\circ Df(a)$$
I'm fairly sure the steps leading up to this point are correct, however it doesn't seem to make sense.  For example, let $f:\mathbb{R}\rightarrow\mathbb{R}^2$
$$f=\begin{bmatrix}x^2\\x\end{bmatrix}$$
then
$$Df=\begin{bmatrix}2x\\1\end{bmatrix}$$
So if we let $i=1$,
$$\begin{align}
Df^1(a)&=f^1(a)\circ Df(a) \\
&=a^2\circ\begin{bmatrix}2a\\1\end{bmatrix}
\end{align}$$
Which I am certain is wrong, since $Df:\mathbb{R}\rightarrow\mathbb{R}^2$, but $f^1:\mathbb{R}\rightarrow\mathbb{R}$ so it doesn't match up.  I think I have misunderstood either the derivative of the projection operator or how $f^i(a)$ forms a composition with $Df(a)$.  Could someone point me in the right direction?  Thanks!
Answer: You're misusing some notation, and probably misunderstanding what it means to say "derivative of a linear transformtion at every point is itself". In the beginning you write $\pi^i(f)=f^i$. This is not at all what Spivak writes. $\pi^i$ is a function $\Bbb{R}^n\to\Bbb{R}$, so its input is a tuple of numbers $(a^1,\dots, a^n)$ and its output is $a^i$. By definition if we have a function $f:\Bbb{R}^k\to\Bbb{R}^n$, then by $f^i$, we mean the function $\pi^i\circ f$. Now,
\begin{align}
(Df^i)(a)&=D(\pi^i\circ f)(a)=(D\pi^i)(f(a))\circ Df(a)=\pi^i\circ Df(a),
\end{align}
where the last equality is because $\pi^i:\Bbb{R}^n\to\Bbb{R}$ is a linear transformation, so at any point (for example $f(a)$) its derivative is itself. This equality makes sense, because $f^i:\Bbb{R}^k\to\Bbb{R}$, so $Df^i(a):\Bbb{R}^k\to\Bbb{R}$ is linear. On the RHS we have a composition $Df(a):\Bbb{R}^k\to\Bbb{R}^n$ with $\pi^i:\Bbb{R}^n\to\Bbb{R}$.
For your example below, Spivak uses the $D$ notation to mean the linear transformation and the prime notation to refer to the corresponding matrix representations relative to the standard bases. So, if $f:\Bbb{R}\to\Bbb{R}^2$ is given as $f(x)=(x^2,x)$, then $Df(a):\Bbb{R}\to\Bbb{R}^2$ is the linear transformation
\begin{align}
Df(x)[h]&=(2xh,h)
\end{align}
And so $Df^1(a)[h]=(\pi^1\circ Df(a))[h]=\pi^1(2ah,h)=2ah$.
Or if you prefer matrices, then
\begin{align}
(f^1)'(a)&=(\pi^1)'(f(a))\cdot f'(a)\\
&=[\pi^1]_{\text{standard bases}}\cdot f'(a)\\
&=
\begin{pmatrix}
1&0
\end{pmatrix}
\cdot
\begin{pmatrix}
2a\\
1
\end{pmatrix}\\
&=2a,
\end{align}
where $[\pi^1]$ is the matrix representation of the linear map $\pi^1$ with respect to the standard basis $\{(1,0),(0,1)\}$ on the domain $\Bbb{R}^2$ and $\{1\}$ on the target space $\Bbb{R}$. This should of course make sense because $f^1(x)=(\pi^1\circ f)(x)=\pi^1(x^2,x)=x^2$, so the derivative at a point $a$ is $2a$.
Comment: so $f^i=\pi^i\circ f$ is obvious. $\pi^i$ is linear so it is differentiable at $f(a)$ and $f$ is differentiable at $a$ by assumption, so $f^i$ is differentiable at $a$ by the chain rule, with $Df^i(a)=D\pi^i(f(a))\circ Df(a)=\pi^i\circ Df(a)$.
Comment: I have a few questions

1.  When Spivak writes "$\pi^i(x)=x^i$" does that mean that it only applies to tuples of numbers?  So if I have a function $f=(f^1,\cdots,f^n)$ then $\pi^i(f)$ is just not defined?

2.  Doesn't $(D\pi^i)_{f(a)}$ have to be $f(a)$ "substituted into" $D\pi^i$?  For example if it's $f=(x^2+a)^2$ the corresponding term would be the $2(x^2+a)$ term.  It seems to me that the $f(a)$ has simply vanished?
Comment: @Redcrazyguy of course, $\pi^i$ is defined only on tuples of numbers, so $\pi^i(x)=\pi^i(x^1,\dots, x^n)=x^i$ for $x=(x^1,\dots, x^n)\in\Bbb{R}^n$. $\pi^i(f)$ just makes no sense in this scenario. Of course, one can define a new object $P^i:(\text{functions $\Bbb{R}^k\to\Bbb{R}^n$})\to(\text{functions $\Bbb{R}^k\to\Bbb{R}$})$ defined as $P^i(f)=\pi^i\circ f$, but why bother with this extra layer of obscurity.
Comment: Next, $(D\pi^i)_{f(a)}$ means the derivative of the function $\pi^i$ at the point $f(a)$. Look at Spivak's theorem 2-3(2). We have $D(\text{linear transformation})_{\text{any point in the domain}}=\text{that same linear transformation}$. THis equal sign is one of linear transformations. So here, $(D\pi^i)_{f(a)}=\pi^i$, for ANY function $f$ mapping into the domain of $\pi^i$, and ANY point $a$ in the domain of $f$, simply because $\pi^i$ is a linear transformation.
Comment: But be careful, this is a very special (but important) result, and it's only true for linear transformations! Seems like you need to spend some time appreciating that $D(\text{function})_{\text{point}}$ is a linear transformation and that $(\text{function})'(\text{point})$ is a corresponding matrix. You need to understand what type of object each is, and how they relate to each other (which is basic linear algebra). Otherwise, alot of what Spivak says afterwards will become confusing.
Comment: I think I understand the first part, but I want to check that I'm understanding the second part.  Basically, we have a linear function $Df(h)$ called the derivative which we use to approximate $f$.  We can evaluate this at a point $a$, denoted by $Df_a(h)$ which would be an approximation for points near $a$.  However this would not be a function of $x$, but of the "small step" $h$ used in the approximation.  If the mapping from domain to range is linear, we can use the same mapping to approximate it, i.e. the differential
Comment: so when we have some function like $y=ax$ the mapping is multiplying by $a$, which is the derivative and which can be used to approximate the points near $x$.  How this applies is when we say $D\pi^i(f(a))$ what we are really saying is $D\pi^i$ evaluated at $f(a)$, which (because it is linear) is just $\pi^i$.  Therefore, in the chain rule the mapping is just $D\pi^i(f(a))\rightarrow \pi^i$
Comment: Wait I'm a bit confused then about the matrix representation, because when you write $(\pi^i)'(f(a))$ wouldn't this be the mapping $\pi^i$ taking an input of $f(a)$?
Comment: @Redcrazyguy take a look at my answer again. I have modified the notation to correspond exactly with what Spivak writes (what Spivak writes $Df(a)$, I have originally written as $Df_a$ simply because it is easier to read. because if I want to evaluate on a "displacement" $h$ then $Df_a(h)$ is much easier to read than $Df(a)[h]$ and of course, $D(g\circ f)_a=Dg_{f(a)}\circ Df_a$ is much easier to read than $Dg(f(a))\circ Df(a)$).
Comment: @Redcrazyguy so yes $D\pi^i(f(a))=\pi^i$. This is an actual equal sign (not an arrow $\to$ as you write) asserting the equality of two linear transformations (which again, I previously wrote as $(D\pi^i)_{f(a)}=\pi^i$ simply because I think it's easier to read). Apologies for the mixup in notation.
Comment: I have a quick follow up on the notation, but I just re-read the proof and Spivak uses $f^i=\pi^i\circ f$.  Is the "evaluated at a" basically implied here?
Comment: @Redcrazyguy  which page/sentence are you referring to? I mean $f^i=\pi^i\circ f$ is obvious/true by definition.
Comment: p.21 (3), sorry I checked what I wrote again and it is different, no problem.


db_select condition with subquery causes PDOexception

Question: I want to build a relative simple select query with a subquery in its condition. What I want to achieve is to get the list of users updated after a given timestamp.
The working SQL-Query which I want to build as dynamic query is:
SELECT uid
FROM `users`
WHERE `vid` > (
  SELECT vid
  FROM `user_revision`
  WHERE `timestamp` > 1379494886
  ORDER BY `timestamp` ASC
  LIMIT 1
);

I've written the following code to build above query with Drupal's Database API:
$subquery = db_select('user_revision', 'ur')
  ->fields('ur', array('vid'))
  ->where('ur.timestamp > 1379494886')
  ->orderBy('ur.timestamp', 'ASC')
  ->range(0, 1);

$query = db_select('users', 'u')
  ->fields('u', array('uid'))
  ->condition('u.vid', $subquery, '>');

$result = $query->execute()->fetchAssoc();

But, I'm getting the following PDOexception error:

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in
      your SQL syntax; check the manual that corresponds to your MySQL server version for the right
      syntax to use near 'SELECT ur.vid AS vid FROM user_revision ur WHERE (ur.timestamp >
      137949488' at line 1: SELECT u.uid AS uid FROM {users} u WHERE (u.vid > SELECT ur.vid AS vid
      FROM {user_revision} ur WHERE (ur.timestamp > 1379494886) ORDER BY ur.timestamp ASC LIMIT 1
      OFFSET 0) ; Array ( ) in MYMODULE_page_callback() (line 22 of
      /path/to/the/module/includes/page_callback_file.inc).

What am I doing wrong building the subquery?
Comment: [SelectQuery::condition()](https://api.drupal.org/api/drupal/includes!database!select.inc/function/SelectQuery%3A%3Acondition/7) doesn't mention that it supports sub queries for the value parameter (in fact it only mentions scalars and array). That's probably the problem
Comment: Yes, I would not expect subqueries to be supported by the PDO, from my experience of how queries are built. You will need to use db_query instead of db_select.
Comment: Thanks for the comments. I was sure that I did read about subqueries could be used in db_select.
Comment: https://drupal.org/node/310086 On this page, under **Subselects** section, I've read about that, but seemingly it doesn't work well.
Answer: Thanks to @Clive's comment, I checked the documentation about the subqueries on https://drupal.org/node/310086 and I noticed the note about:

Note: Currently it is only possible to use subselect conditions with the IN operator because with the other operators the sub-query is not wrapped in parentheses and so results in a syntax error. See #1267508: Subselects don't work in DBTNG conditions, except when used as value for IN.

I've changed my code to use IN instead of > and it worked like a charm. Here's the working code for future reference if someone falls into the same situation:
$subquery = db_select('user_revision', 'ur')
  ->fields('ur', array('vid'))
  ->where('ur.timestamp > 1379494886')
  ->orderBy('ur.timestamp', 'ASC');

$query = db_select('users', 'u')
  ->fields('u', array('uid', 'vid'))
  ->condition('u.vid', $subquery, 'IN');

$result = $query->execute()->fetchAll();

Comment: Ok, it was not directly relevant. To test it I just added the `LIMIT` back and got another error this time: `PDOException: SQLSTATE[42000]: Syntax error or access violation: 1235 This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery':...`
Comment: Not sure how relevant this is, but I noticed you remove the `LIMIT` from the subquery. Interestingly, the [Restrictions on Subqueries](http://dev.mysql.com/doc/refman/5.0/en/subquery-restrictions.html) would've caused that to fail even if PDO had got the syntax right. Not something I've come across before
Comment: I removed the `LIMIT` to be able to use `IN` instead of `>`, since I needed all values returned by the subquery, not only the smallest one.
Answer: You could separate your subquery into another query to return just the value, see below:
$vid = db_select('user_revision', 'ur')
  ->fields('ur', array('vid'))
  ->where('ur.timestamp > 1379494886')
  ->orderBy('ur.timestamp', 'ASC')
  ->range(0, 1)
  ->execute()
  ->fetchField();

$query = db_select('users', 'u')
  ->fields('u', array('uid'))
  ->condition('u.vid', $vid, '>');

$result = $query->execute()->fetchAssoc();

Comment: This way is no better than the solution I posted. It is almost the same, but using 2 queries instead of just one, so it is worse for performance. I see no point why did you post almost the same answer, splitting it into two queries.


Entity - Loop infinito em conversão JSON

Question: O que acontece
Estou tendo um problema ao utilizar o Entity com minha aplicação MVC. O que acontece é que ao converter uma classe pra JSON o programa entra em um loop infinito e acaba retornando no-content
Exemplo
Tomando como exemplo o relacionamento (1-n):
Um Autor tem vários Posts.
O que eu percebi fazendo debug é que com uma Conversão de JSON vai ocorrer a seguinte interpretação:
Um autor tem vários posts. Um post tem um autor. Um autor tem vários posts. Um post tem um autor...
Claro que não é bem essa a minha situação de classes, mas o que ocorre é a mesma ideia. Quando vou ver na minha API a resposta é no-content. Quando eu insiro [JsonIgnore] nas minhas propriedades isso é ajustado, mas aí perco um dos lados da relação, o que é um preço que não posso pagar para o funcionamento da aplicação.
Gostaria de saber como proceder e fazer o Entity não entrar nesse loop infinito.
EDIÇÃO: Código
Models
Team.cs
public class Team : TEntity
{
  public int ID {get; set;}

  public string Function {get; set;}

  public int LeaderID {get; set;}

  public User Leader {get; set;}

  public List<TeamUser> Users {get; set;}
}

TeamUser.cs
public class TeamUser : TEntity
{
  public int ID {get; set;}

  public int TeamID { get; set; }

  public int UserID { get; set; }

  //Aqui, sem essa anotação já retornaria "no-content" (acredito eu, devido ao bug do loop)
  [JsonIgnore]
  public virtual Team Team { get; set; }

  public virtual User User { get; set; }
}

User.cs
public class User : TEntity
{
  public int ID {get; set;}

  ...

  // o mesmo aqui
  [JsonIgnore]
  public virtual IList<TeamUser> Teams {get; set;}

  // e aqui
  [JsonIgnore]
  public virtual IList<Team> TeamsLeadered {get; set;}

Arquivos Configuration do Entity
TeamConfiguration.cs
...
builder
  .Property(t => t.LeaderID)
  .HasColumnName("ID_LIDER");

builder
  .HasOne(t => t.Leader)
  .WithMany(u => u.LeaderedTeams)
  .HasForeignKey(t => t.LeaderID);

TeamUserConfiguration.cs
builder
  .Property(tu => tu.TeamID)
  .HasColumnName("ID_EQUIPE");

builder
  .Property(tu => tu.UserID)
  .HasColumnName("ID_USUARIO");

builder
  .HasOne(tu => tu.Team)
  .WithMany(t => t.Users)
  .HasForeignKey(tu => tu.TeamID);

builder
  .HasOne(tu => tu.User)
  .WithMany(u => u.Teams)
  .HasForeignKey(tu => tu.UserID);

GetAll()
public IEnumerable<Team> GetAll()
{

    var teams = _context.Teams
       .Include(t => t.Leader)
       .Include(t => t.Users)
       //Aqui eu precisaria de um ThenInclude(t => t.User) mas retorna no-content
       .ToList();

    return teams;
 }

Comment: Poste o código que você está usando para tal rotina amigo
Comment: Adicione seu código para que possamos lhe ajudar.
Comment: Pergunta editada, com o código adicionado. Ali eu explico que, da forma que está, funciona, mas pra fazer uma busca dos usuarios de um Team por exemplo, já não consigo.
Comment: Como você está convertendo seu objeto em JSON?
Answer: Quando for realizar a serialização do seu objeto adicione a propriedade JsonSerializerSettings e configure para ignorar os loops quando for realizar a serialização.
Se você estiver usando o Newtonsoft.Json para realizar a serialização ficaria da seguinte forma:
JsonConvert.SerializeObject(objeto1, Formatting.Indented, new JsonSerializerSettings
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});

Caso esteja utilizando ASP.NET Core Web Api, no seu WebApiConfig.cs adicione essa configuração:
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

Referência do ASP.NET Core: https://code.msdn.microsoft.com/Loop-Reference-handling-in-caaffaf7


Swift Can't Import Sqlite3 iOS

Question: I added libsqlite3.0.dylib to my project, and then I tried to import using the following code:
import UIKit
import sqlite3

class Dataware: NSObject
{

}

But it's giving me this error:
No Such Module 'sqlite3'
Answer: Add it to your Bridging-Header.h file:
#import <sqlite3.h>

This is the primary mechanism for importing any C-language libraries.
If you don't yet have a Bridging-Header.h file:

Add a file Bridging-Header.h (or more typically (ProjectName)-Bridging-Header.h
Go to the build settings tab for your project
Find "Objective-C Bridging Header".  The easiest way is to search for bridging.
Enter the name and path for the file you created in step one.  It's probably (ProjectName)/(ProjectName)-Bridging-Header.h
Comment: Where I Add this to My Project?
Comment: I Added this File and i imported sqlite3.h but for .swift i use var dbsql=sqlite3() this line. but use of unresolved identifier 'sqite3' error is arrived
Answer: when one want to add sqlite to framework target, module.map is needed
since sqlite is not mapped, and to do so just:
1. create file in your project 'module/module.map'
2. create the module from the umbrella header:
    module sqlite3 [system] {
       header "/Applications/Xcode6-Beta5.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.0.sdk/usr/include/sqlite3.h"
       link "sqlite3"
       export *
    }

*change the Xcode6-Beta5.app in the path to right one
3. connect the map file to you project, search for 'import paths' in 'Build Settings'and put the full path to the module file
Comment: Hi @RatikantaPatra, to add new module map, you need to: [1]create file called module.map [2]add the wanted module info (like in my answer) [3]in Xcode Go to Build Settings->set Swift Compiler->Search Paths->Import Paths to the directory that you put the module.map file in
Comment: @StefanArentz Module map files do not, but you can use an xcconfig file to reference different `MODULEMAP_FILE` paths per platform. See [my answer to another question](http://stackoverflow.com/a/29189873/86910), or my [SQLite.swift](https://github.com/stephencelis/SQLite.swift) framework (on the `swift-1-2` branch) for examples.
Comment: this solution worked for me when tried to add sqlite to my swift framework
Comment: Hi @ben. Could you please elaborate your answer. I am not getting how to add a new module
Comment: Is it possible to change the absolute path in that file to something like $(SDKROOt)? Do you know if module.map files support something like that?
Answer: We need to import the header files for SQLite3 into the view controller so that the compiler can see the function and other definitions that make up the API.
There is no way to directly import the header file into Swift code, because the SQLite3 library is not packaged as a module.
The easiest way to deal with this is to add a bridging header to the project. Once you have a bridging header, you can add other header files to it, and those header files will be read by the Swift compiler. There are a couple of ways to add a bridging file. We'll use the simpler of the two, which is to temporarily add an Objective-C class to the project. Let's do that now.
File ➤ New ➤ File.... In the iOS section of the dialog, choose
Cocoa Touch Class and press Next. Name the class Temporary, make it a subclass of NSObject, change the language to Objective-C, and press Next. In the next screen, press the Create button.
When you do this, Xcode will pop up a window asking whether you want to create a bridging header. Press Yes.
Now, in the Project Navigator, you'll see the files for the new class (Temporary.m and Temporary.h) and the bridging header, which is called SQLite Persistence-Bridging-Header.h. Delete the Temporary.m and Temporary.h files—you don't need them anymore. Select the bridging header to open it in the editor, and then add the following line to it:
#import < sqlite3.h>
Now that the compiler can see the SQLite3 library and header files, we can write some more code in ViewController.swift
That's it!
Answer: Hi Please follow these steps
In xcode 8.3.3 using swift 3

Go to Build Phases tab
Go to Link Binary with Libraries sub tab.
(a) Click + button to add sqlite framework then search for sqlite then you can see libsqlite3.0.tbd and libsqlite3.tbd
(b) Then select only libsqlite3.tbd(Don't add both because the compiler can not find sqlite3 stuct when you declare in viewController)
Then Add Bridging-Header.h file (because sqlite is not written in swift)
Bridging name should be your Projectname-Bridging-Header.h file (Just for naming convention, not mandatory)
Write #import <sqlite3.h> in your Bridging-Header file
Go to build settings tab
(a) Under the build settings tab search for Swift Compiler - General option and set   YES to Install Objective-C compatibility Header
(b) Set your name and path for the header file in Objective-C Bridging Header option (Or you can simply drag the bridging header file)

Swift Can't Import Sqlite3 iOS


Firebase Test Lab can't record page scroll event in script recording

Question: I'am trying to record a script. Every things is ok but when I try to scroll page, recorder is not responding and record nothing. I tried that in page with RecyclerView and ScrollView.
When I looked at test result in firebase console and watched the video, when the script is over and start the random test, pages can scrolling. So I thing there is a way but I can't find what it is. Please help.
Answer: Page scrolling is not recorded as a separate event. For example, if you scroll a RecyclerView and then click on an item, your click will be recorded and then, during replay of the script, both your scroll and your click will be replayed.


How can I center a div with a fixed width so that it scrolls horizontally and stays centered responsively when the div is wider than the browser?

Question: I have this working the way I want it to, except that I need it to stay centered, so that the green rectangle is in the center of the browser (until the user scrolls) when the browser width is between 835px - 320px.
I'd like to get this figured out with CSS, but I'm not opposed to a JavaScript solution.
I'm pretty new to posting questions on stackoverflow, so I apologize in advance if I'm not clear enough or if I've provided too much or not enough information.
Thank you in advance.
<!DOCTYPE html>

Comment: Here is the code on jsfiddle: http://jsfiddle.net/andrewmowder/k5NKT/
Comment: You can use @media to detect the screen width and adapt the div width.
Answer: You can do that:
@media screen and(max-width:835px) and (min-width:320px) {
    .product_map_image_mobile {
        width: new_widthpx;
    }
}

Comment: Thank you, Codel96. I'm using media queries in the site itself, but what I'm needing here is for the width of .product_map_image_mobile to always remain at a width of 835px, but to have it center within the browser window when the browser is less than 835px wide.
Comment: Mmmh, sorry if I misunderstood. But margin: auto doesn't work? I saw that you are using it so I think it is not what you're looking for but I want to be sure.
Comment: no worries. yeah, I can't get margin: auto to work in this situation. The div sticks to the left edge of the browser window upon resizing. I thought this could be pulled off with CSS, but maybe I need to look into a JavaScript solution.
Comment: Do you have to use position:absolute; and position:relative; or could you use simple "float:left" with margin?
Comment: I can't seem to get it to work that way either, unfortunately.
Comment: My friend Cliff figured out a JavaScript solution that's going to work. I just need to tweak the styling a bit so that the green square hits dead center. http://jsfiddle.net/k5NKT/1/