Como executar query em C# para controle de solicitação

Question: Sou novo em programação e preciso acrescentar algumas funcionalidade em uma aplicação C# asp.net. Uma delas é um controle que impede o usuário de prosseguir com a solicitação caso ele tenha alguma pendência no sistema. Vou descrever o passo a passo.
Minha view
@using Integracao.ImpressoRequests.Model.Entities.ImpressoRequest
@model Request
@{
  ViewBag.Title = "Requisição";
  Layout = "~/Views/Shared/_Layout.cshtml";
}

    ...

    @foreach (RequestItem item in Model.Items)
    {

                @Html.DisplayFor(x => item.Usuario.Nome)

                @Html.DisplayFor(x => item.Date)

                @Html.DisplayFor(x => item.Unidade.Nome)

                @Html.DisplayFor(x => item.ResultCenter.Nome)

    }

    <input type="button" value="Gerar pedido" onclick="window.open('@Url.Action("ProcessRequest", new { requestId = Model.Id })','_blank');firstname.lastname@example.com("Index", "Home")'" />

Controller
    [HttpGet]
    public ActionResult ProcessRequest(int requestId)
    {
        Request request = _data.Requests.GetByID(requestId);
        if (request.Processed)
            return RedirectToAction("PrintRequest", new { requestId = request });
        foreach (RequestItem item in request.Items.OrderBy(x => x.Usuario_Id))
        {
            AssignImpressos assignIItems = new AssignImpressos(item.Id);
            assignIItems.Assign();
        }
        request.Processed = true;
        _data.Save();
        return RedirectToAction("PrintRequest", new { requestId = request.Id });
    }

Model
    public Request()
    {
        Date = DateTime.Now;
        UsedItems = new List<Impresso>();
        RequestsHistory = new List<RequestHistory>();
        Items = new List<RequestItem>();
    }

    public int Id { get; set; }

    public DateTime? Date { get; set; }

    public string Justification { get; set; }

    public string Usuario_Id { get; set; }

    public virtual Usuario Usuario { get; set; }
    public virtual ICollection<Impresso> UsedItems { get; set; }

    Model Impresso

        public class Impresso : IEntity
{
    public Impresso()
    {
        RequestsHistory = new List<RequestHistory>();
    }

    public int Id { get; set; }

    public int Code { get; set; }

    public string Purpose { get; set; }

    public DateTime? Data_Controle { get; set; }

    public decimal AdjustedValue { get; set; }

    public int Status { get; set; }
    public ItemStatus EnumeratedStatus
    {
        get { return (ItemStatus)Status; }
        set { Status = (int)value; }
    }
    public string Note { get; set; }

    public string Usuario_Id { get; set; }
    public virtual Usuario Usuario { get; set; }

    public int? Request_Id { get; set; }
    public virtual Request Request { get; set; }

    public string Unidade_Id { get; set; }
    public virtual Unidade Unidade { get; set; }

    public string ResultCenter_Id { get; set; }
    public virtual ResultCenter ResultCenter { get; set; }

    public virtual ICollection<RequestHistory> RequestsHistory { get; set; }

    object IEntity.Id
    {
        get { return Id; }
        set { Id = (int)value; }
    }
}

O que eu preciso é o seguinte: Ao usuário clicar em 'Gerar Pedido' na view, na minha classe controller preciso varrer a tabela Pedido no banco procurando se aquele usuário já autenticado pelo sistema possui na tabela Impresso o campo 'Data_Controle' null ou o Status = 1. Caso positivo, o sistema deve impedir o prosseguimento disparando um alerta com a informação. Caso seja falso, o programa continua e gera a solicitação normalmente.
A pergunta é: Eu sei como fazer um select no banco para buscar isso mas qual a sintaxe no c# de acordo com o meu código para fazer esse controle? Pensei em query porque mexo com banco de dados e a lógica é mais clara pra mim, ou se não por query, qual seria a melhor maneira de conseguir isso em lógica de programação mesmo? Obrigado
Edit
        private string GetUser()
    {
        string userId = string.Empty;
        try
        {
            string id = User.Identity.Name.Split('\\')[1];
            Usuario _currentUsuario = _data.Usuario.GetByID(id);
            userId = _currentUsuario.Id;
            ViewBag.UserName = _currentUsuario.Name;
        }
        catch (Exception ex)
        {
            ModelState.AddModelError("UserName", ex.Message);
        }
        return userId;
    }

Comment: Você quer que esse aviso apareça na tela como?
Comment: @CiganoMorrisonMendez pode ser um alert ou messagebox simples... O importante é travar o usuário e voltar a tela inicial quando ele der o ok de ciente de não poder solicitar...
Answer: Você pode criar um ActionFilterAttribute para isto:
public class VerificarPendencia : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        // Assumindo que você possui um ICollection<Impresso> Impressos para a entidade Usuario, e que esta coleção está populada ao obter o Usuario.
        if (_data.Usuarios.GetByID(GetUser()).Impressos.Any(x => x.Data_Controlle == null && x.Status == 1))
        {
           // Açoes...
        }
    }
}

Pesquise um pouco sobre o ActionFilterAttribute para saber os métodos que você pode utilizar. E então você só precisa decorar a Action com este atributo:
[VerificarPendencia]
[HttpGet]
public ActionResult ProcessRequest(int requestId)

Todas as Actions decoradas com este atributo (se preferir, pode decorar o Controller inteiro) agora passam pelos métodos nela criados, como o OnResultExecuting, OnActionExecuted, etc...
Comment: Acredito que a resposta seria mais útil se a solução fornecesse os passos para a pesquisa, não pedir pro autor da pergunta pesquisar.
Comment: @PFVictor entendo. Pelo que vi nas respostas, você pode usar "_data.Usuarios.GetByID(GetUser()).Impressos.Any(x => x.Data_Controlle == null && x.Status == 1", eu alterei a resposta.
Comment: @EuripedesLeal dei uma pesquisada sobre esse método e pelo que vi ele pode ser uma alternativa sim. Porém minha dificuldade maior está justamente no "// aqui você faz o processo".
Comment: @CiganoMorrisonMendez pode me dar um exemplo de implementação dessa resposta do Euripedes para eu me basear ou outra resposta que também julgue que me atenda?
Comment: Olha, não sou muito fã dessa abordagem de `ActionFilters` porque a validação de dados deve ser feita 1) No Model, ou 2) No Controller, quando envolve dados entre dois ou mais Models, o que é seu caso, mas é uma questão mais pessoal. Vou colocar duas maneiras de fazer, aí você elege qual a melhor.
Comment: ótimo :) obrigado
Comment: Obrigado @EuripedesLeal!
Answer: O código está bem próximo do que você precisa. Imagino que ProcessRequest seja aquele código mais pra cima, então eu faria o seguinte:
[HttpGet]
public ActionResult ProcessRequest(int requestId)
{
    // Estou supondo que aqui você carregue seu usuário dentro de um objeto 'Usuario'
    // var usuario = contexto.Usuarios.SingleOrDefault(u => u.Nome == User.Identity.UserName);
    var usuario = _data.Usuarios.GetByID(GetUser());

    if (contexto.Impresso.Any(i => i.Usuario_Id == usuario.Usuario_Id && (i.Data_Controle == null || i.Status == 1))) {
        ModelState.AddModelError("", "Coloque aqui a mensagem que explica porque o sistema não pode continuar");
        return View();
    }

    Request request = _data.Requests.GetByID(requestId);

    if (request.Processed)
        return RedirectToAction("PrintRequest", new { requestId = request });

    foreach (RequestItem item in request.Items.OrderBy(x => x.Usuario_Id))
    {
        AssignImpressos assignIItems = new AssignImpressos(item.Id);
        assignIItems.Assign();
    }

    request.Processed = true;
    _data.Save();
    return RedirectToAction("PrintRequest", new { requestId = request.Id });
}

Comment: eu tenho dentro do mesmo controller do ProcessRequest uma classe GetUser que identifca o usuário, a qual editei na pergunta. Nesse caso, a primeira linha eu trocaria por `var usuario = _data.Usuarios.GetByID(GetUser()); ` certo?
Comment: @PFVictor Isso. Aliás, fique à vontade pra editar minha resposta, se quiser.
Comment: Deu certo @CiganoMorrisonMendez. Obrigado mais uma vez.


How find this equation all solution $2a^2-1=b^{2013}$

Question: let $a,b\in \mathbb Z$, and such $$2a^2-1=b^{2013}$$
find all value $a,b$
I think $(a,b)=(0,-1),(1,1),(-1,1)$ is solution, and have other solution.  Thank you everyone
Comment: As $3\mid2013$ I would try to prove non-existence of other solutions of $2a^2-1=c^3$. An elliptic curve, but probably has some trick method.
Comment: If it is from an ongoing competition (as seems possible given the appearance of 2013), you should tell us (and wait until deadline has passed). Otherwise it may get ugly, allegations of cheating and such.
Comment: Thank you all same, @JyrkiLahtonen
Comment: According to me, we can prove that $\frac{b^{2013}}2 + \frac12$ is a perfect square only for the solutions you gave above then I think we might get something.
Comment: this problem How to prove $$\dfrac{b^{2013}}{2}+\dfrac{1}{2}$$ is a  perfect.
Comment: And where do you get these questions from? These questions are so interesting..... You just made me curious...
Comment: @Rohinb97, This problem is my frend ask me,I think this problem is from compition
Comment: Obviously b cannot be even, it must be odd. Also the trick in questions like this is basically same:logically prove that the above term is not a perfect square for large values of b. Completing the square according to me might help...
Comment: This problem is very interesting,Hope someone can use nice methods
Comment: yes,I'm a math teacher and  it's deadline
Comment: Try showing it's congruent to 0mod4 and 1mod8 to see if it's only for odd or even perfect squares or both.
Answer: Following Jyrki's suggestion, set $c=b^{671}$ and the equation becomes $$2a^2=c^3+1$$
Multiply both sides by 8 to get $$(4a)^2=(2c)^3+8$$
This is a Mordell curve, which have finitely many solutions.  In this case the constant is $8$, and here we can find the only integral solutions $$(4a,2c)\in \{(0,-2),(3,1),(-3,1), (4,2), (-4,2), (312,46), (-312,46)\}$$
This gives $$(a,c)\in \{(0,-1),(1,1),(-1,1),(78,23),(-78,23)\}$$
However only the first three give $c$ that is a 671st power.  This confirms that there are three solutions in the integers.

Followup: We may replace $2013$ with $3(2k+1)$ for any integral $k\ge 2$ and get the same result.
Comment: Hello, @vadim123,I can't Open the Mordell curve,can you send to me?
Comment: http://tnt.math.se.tmu.ac.jp/simath/MORDELL/MORDELL+ it's a text file
Comment: Thank you @vadim123, where are this proof? with this $$c^3+1=2a^2$$ has only $$(a,c)\in\{(0,-1),(1,1),(-1,1),(78,23),(-78,23)\}$$?
Comment: Mordell curves are of the form $y^2=x^3+n$, and there are only finitely many integral solutions for any fixed $n$.  For more details, see the wikipedia link I posted.  The second link I posted (see http://tnt.math.se.tmu.ac.jp/simath/MORDELL/) gives the specific solutions for all $n$ with $|n|<10000$.  They were computed using computer software.
Comment: Thank you, I hope see this equation $x^3+1=2y^3$ integer solution methods? But I can't find it,can you link ? Thank you very much
Comment: I've uploaded the file (and a pdf of the first 10 pages) for you.  Here's the [link](http://www.sendspace.com/filegroup/QbnOP1uprFAtpnXQkjEn4g).
Comment: The methods to solve these are difficult, as they are elliptic curves.  See also http://mathworld.wolfram.com/MordellCurve.html
Comment: Thank you, if you let $b^{667}=x$ this solution Become less?


How to calculate skewness of data from histogram in R?

Question: In R, the function skewness from package moments allows one to calculate the skewness of the distribution from a given sample. Does anybody know if there is a ready-to-use function to calculate the skewness of the distribution from a given histogram?
Comment: @dominic Frequently--especially when reading papers--a histogram is just about the most detailed presentation of the data available.  This situation is not new: older statistical texts spend a lot of time on making estimates from histograms and binned data.  For instance, see the [MathWorld article on Sheppard's Corrections](http://mathworld.wolfram.com/SheppardsCorrection.html) and note the dates of the references.
Comment: Not sure I understand the question... If you have an histogram object, you should normally have access to the data from which this object was constructed. This is given by the $xname attribute of the histogram object.
Answer: Sounds like you're looking at a published histogram and don't have the actual data?
If that is the case you could calculate a rough skewness figure by doing something like pub_his <- c(rep(10,16),rep(15,18),rep(20,27)...), picking the mid point of each bar and reading the frequency off the graph, then you'd have the data and could use the skewness function.
Comment: @nz You're right.  This method is great for estimating the third moment when the skewness is small.  However, because it tends to overestimate the variance, it will typically *underestimate* the absolute value of the skewness.  The correction depends on the size of the histogram bin width relative to the standard deviation (as well as on the shape of the distribution): it becomes substantial when the bin widths exceed 1 SD (which can happen for coarse histograms of highly skewed datasets).
Comment: Correct, without the xis you can't do any calculations that include the xis. You could substitute your 'middle of the bin' position and get an approximate result, but personally I would never do that and I wouldn't recommend it to you either. As whuber notes, there is lots at play here, bin with etc. If this is a histgram in a journal article try contacting the authors, you may be pleasantly surprised.
Comment: This would be approximate though, but probably just as useful as an accurate skewness value....
Comment: @nzcoops: Yes, I can do it this way. So, no way to calculate $\sum (x_i - \bar{x})^2$ and $\sum (x_i - \bar{x})^3$ knowing bar positions and heights without "re-generating" the data from the histogram?
Answer: In the case of variance (and indeed for the fourth central moment), the "taking the bin center" approach carries a bias.
Such bias can be corrected, via what are known as Sheppard's correction e.g. see here, which in the case of variance would subtract $\frac{1}{12}$ of the square of the bin-width from the binned estimate of the second moment. However, in the case of the third moment, no such adjustment is needed.
On the other hand, the third-moment-skewness, if just calculated directly by dividing the binned third-moment by the unadjusted binned-variance to the power $\frac{3}{2}$ will yield too large a denominator, and so the skewness will be on average slightly underestimated.
This suggests that the binned variance be adjusted by Sheppard's correction before computing the skewness. So:
1) calculate the binned moments (first, second, third) as needed (i.e. by taking every observation at its bin center and if possible using the weighted/grouped-data formulas)
2) correct the variance for the grouping bias
3) compute the skewness by dividing the grouped third moment by the corrected grouped second moment.
However, you may want to ponder the circumstances under which the corrections apply. For example, you might see the discussion of the corrections in Kendall and Stuart, or maybe consult
M. G. Kendall (1938),
The Conditions under which Sheppard's Corrections are Valid
Journal of the Royal Statistical Society,
Vol. 101, No. 3, p592-605
[Even so, dividing an unbiased numerator by an unbiased denominator doesn't itself lead to an unbiased ratio, even when they're independent, so if an actually unbiased ratio is of interest, further analysis would be needed -- but we don't generally concern ourselves with that when dealing with ordinary sample skewness either.]


Erro write failed: No space left on device

Question: Ao reinstalar o servidor Apache e PHP na minha máquina estou recebendo este erro:

Warning: Unknown(): write failed: No space left on device (28) in Unknown on line 0

E se tento executar algum script com sessão, além deste erro, recebo o seguinte erro:

Warning: Unknown(): Failed to write session data (files).
  Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
Comment: qual distro e qual comando vc está usando para instalar?
Comment: @AdirKuhn http://pt.stackoverflow.com/help/self-answer
Comment: Antes de dar um voto negativo entenda o modelo da comunidade, leia: http://pt.stackoverflow.com/help/self-answer e http://blog.stackoverflow.com/2012/05/encyclopedia-stack-exchange e se tiver algum outro motivo pro downvote justifique.
Answer: Este problema costuma ocorrer por motivos como:

O diretório configurado (no caso /tmp) não tem permissão para escrita.
O diretório não permite acesso do usuário atual (o servidor pode ter acesso do usuário principal, mas o diretório só tem acesso por root).
Pode ser um diretório que não existe.

O diretório /tmp é apenas um exemplo, veja outra situação do problema:

Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/opt/lampp/tmp/session) in Unknown on line 0

Neste caso foi instalado o servidor lampp em uma máquina like-unix e é provável que a pasta /opt/lampp/tmp/session não seja acessível (por um dos motivos já descritos).
No caso do problema é provável que também retorne este erro:

Warning: Unknown(): Failed to write session data (files).

Possíveis soluções:

Se o diretório não existir devemos editar o arquivo php.ini e configurar para um diretório válido:
Procure no arquivo php.ini está linha e altere-a:
session.save_path = /home/user/tmp;

Se for um servidor/maquina Windows seria algo como:
session.save_path = c:/wamp/tmp;

Se você estiver tiver feito a modificação php.ini e mesmo assim o problema ocorre, pode ser que você esteja em um "servidor virtual", portanto alteração tem que ser no tal servidor.
Se tiver certeza que não é um servidor virtual ou que alterou o arquivo certo, então é por que você necessita reiniciar o servidor (não precisa reiniciar a máquina inteira):
Se for Apache, no terminal use o comando:
apache2 restart ou com SU use sudo apache2 restart
Se tiver reiniciado (mesmo que a máquina), o problema pode ser nas permissões da pasta.
Para alterar as permissões, use o seguinte comando:
chmod 1777 /tmp

Nota: Em sistemas like-unix não é necessário permitir o acesso da pasta que ficará a sessão para todos usuário (usuário do sistema operacional), somente o usuário que o serviço está rodando.

Outra solução é configurar usando (no caso do problema ser com session_start) o session_save_path:
session_save_path('/home/user/tmp');

Agora se por algum motivo você estiver usando um serviço de hospedagem e você não tiver acesso ao php.ini e o session_save_path estiver desabilitado só existe uma solução, procurar o suporte técnico da empresa que está lhe prestando o serviço, através de ticket, telefone ou email (em alguns casos chat).


Allow Anonymous Access to ForgotPassword Page

Question: I have a link to /ForgotPassword on my Login page. When it's clicked, I'm just redirected to /Login?ReturnUrl=%2fForgotPassword. This is an MVC5 app using Form-based authentication. I'm attempting to use AllowAnonymous, but it isn't working.
Web Config:
<code>  <location path=".">
  <system.web>
  <authorization>
    <deny users="?"/>
  </authorization>
  </system.web>
  </location>
  <location path="Content">
  <system.web>
  <authorization>
    <allow users="*"/>
  </authorization>
  </system.web>
  </location>
  <location path="Scripts">
  <system.web>
  <authorization>
    <allow users="*"/>
  </authorization>
  </system.web>
  </location>
  <location path="App_Themes">
  <system.web>
  <authorization>
    <allow users="*"/>
  </authorization>
  </system.web>
 </location>

<authentication mode="Forms">
  <forms loginUrl="~/Login" timeout="15"/>
</authentication>
</code>
ForgotPassword Controller:
<code> [AllowAnonymous]
 public class ForgotPasswordController : Controller
 {
    [AllowAnonymous]
    public ActionResult Index()
    {
        return View();
    }
 }
</code>
No such luck. Aside from a _ViewStart, I can't find anything that is being used that should be decorated with AllowAnonymous.
What I'm a missing here? Thanks!
Answer: don't use authorization tags in we.config, use only authorization attributes in controllers and actions.
authorization tags in web.config are useful to allow/restrict Access to files and it Works well with web forms and static content.
But in MVC there is routes which is mapped to controllers and actions, and you may have different actions in the same controller with different authorization tags, e.g.: you can use [Authorize] for the whole controller, and allow only one action for anonymouse users by using [AllowAnonymous] attribute to the desired action only.
read more here: https://weblogs.asp.net/jongalloway/asp-net-mvc-authentication-global-authentication-and-allow-anonymous


want to access sharepoint remotely for using sharepoint api

Question: We have a sharepoint server 2010 installed on a machine(say server A) which we can access remotely.
Till now we were using sharepoint webservices to communicate with sharepoint.
But now we want to use sharepoint.dll to code.
i read somewhere that we need to coding on the same machine on which the sharepoint server is installed(Server A in this case).  Is this correct , pls correct me if i am wrong?
Here starts the problem , We can not access the server physically, we have only remote access( using remote desktop means only 2 connection at a time).
We are a team of 5 people needs to code simutaneously, we should i do for this.
Answer: Yes it's correct, the Microsoft.SharePoint.dll is the server object model, which only can be accessed when you are connected to the server.
There is a client object model aswell, which allows your code to be executed remotely. http://msdn.microsoft.com/en-us/library/ee857094.aspx However this is not the solution you are looking for.
Each of your team members needs to install SharePoint in an virtual environment (i'd recommend using VMware) on your developer pc/laptop. (Obviously you will install it only once and then copy the VWware disk).
If you never installed a Windows/SharePoint server yourself, get the admin who installed Server A to do it, because it can be quite tricky. Heres a guide if you want to try yourself: http://mosshowto.blogspot.com/2009/11/installing-sharepoint-2010-windows-2008.html
Answer: This may be getting to you too late, but... just in case.
I would strongly suggest you to think twice before trying this, but you'll see this may get to help you.
I was very concerned about remote development when I first started working with SP, but then I convinced the PM to have local environments and I didn't need to complete my research.
REMOTE CODING:
However, I started having to bring some work to home and needed to connect remotely to my PC at work, so I eventually got sick bored of latency and connected my laptop's VS2010 (ultimate) to my employeer TFS and got latest version. I have to remark I run win 7 64 bits.
Of course when I tried to compile the solution my machine started complaning because it didn't have an idea what that Sharepoint.dll was supposed to be.
That's when I connected remotelly to my work-pc and got from the GAC a that dll. (see here how to extract a dll from GAC: http://metahat.blogspot.com/2008/08/copy-extract-dll-from-gac.html). Then I copied that to my machine and installed it in my own GAC (gacutil -I "C:[PathToBinDirectoryInVSProject]\gac.dll").
I had to do it a few times since our app does some crazy things and had pretty much to depend on. Normally you'll be told what is missing and you can go in msdn and see what assembly contains it.
Ok, after all that I got to compile it and it worked like a charm.
DEPLOY AND TEST
That can help you coding. Now, from my past shared-Websphere-appserver experiences I can bet you sinchronizing a team to deploy in one app server will be a headache. Think that some of you will be able to compile but do not run.
Since SP has dlls inside the GAC, having different versions of your app running in different web apps is something quite complicated. (I prefeer not to discuss if it is possible, but if my idea is possible, then it is at least very complex in terms of configuration management)
Of course you can share a folder at your server, compile, copy the pkg dlls to the shared folder, then use the trick of drag and drop to move (some of) them to the GAC and finally iis reset. (I used to have a script to do it all but the drag and drop)
I simply decided to commit (perhaps in a different branch) and then deploy from the remote machine. But mainly I decided to code from my laptop all but specific SP view components, therefore I was able to simply validate that all by running my Unit tests without the SP
DEBUG
In adition, If this all didn't got you affraid enought of even trying, then you can find in here some instructions about how to get to debug remotelly your SP2010
http://techblog.hk.agenda-asia.com/2010/10/29/create-sharepoint-project-without-install-sharepoint-server/
Well, that's all i know about shrimp. :p (ment I wrote too much)


Let $X$ metric space connected by paths. If $U$ is open then every connected components of $U$ is open in $X$?

Question: Let $X$ metric space connected by paths. If $U$ is open then every connected components of $U$ is open in $X$?
I tried a few things and I did not succeed, would like a tip.
Thank's
Comment: This claim is false; to make it true you also need local connectivity.
Answer: A space $X$ (metric or not) has the property that "for every open set $U$ of $X$, all connected components of $U$ are open", iff $X$ is locally connected.
So the path-connectedness of $X$ is irrelevant here. Any path-connected but not locally connected space (like the Warsaw circle, or other examples) is a counterexample.


Does $2^x \equiv -1$ mod $24n-1$ have a pair $(x,n)$ that gives a solution?

Question: I'm interested in knowing if $2^x \equiv -1$ mod $24n-1$ has a solution. I'm also interested in knowing in general how to find out which elements of the group $(\textbf{Z}/(24n-1)\textbf{Z})^{\times}$ generate $-1$.
One thing that I considered was using Jacobi symbols. $\bigg(\dfrac{2}{24n-1}\bigg) = 1$, but this doesn't help me if $24n - 1 = pq$, $p$ and $q$ prime, and $\bigg(\dfrac{2}{p}\bigg)$ = $\bigg(\dfrac{2}{q}\bigg) = -1$, as is the case in mod 95.
Another thing that I considered was using the Chinese Remainder Theorem to construct an isomorphism between $(\textbf{Z}/(24n-1)\textbf{Z})^{\times}$ and $(\textbf{Z}/p_1^{k_1})^{\times} \times (\textbf{Z}/p_2^{k_2})^{\times} \times \dots (\textbf{Z}/p_r^{k_r})^{\times}$ where $24n-1 = p_1^{k_1}p_2^{k_2}\dots p_r^{k_r}$. I've figured out that by doing this, I can show that $(\textbf{Z}/(24n-1)\textbf{Z})^{\times}$ is isomorphic under some bijection $\phi$ to $\{1,g\} \times A$, where $A$ is an abelian group, and $\{1,g\}$ is $C_2$. I can construct $\phi$ such that $\phi(-1) = (g,e_A)$, but I don't see a way of showing whether or not $(g,e_A) \in <\phi(2)>$.
I would be able to move forward if I could somehow analyze which subgroups of $(\textbf{Z}/(24\textbf{Z}-1))^{\times}$ contain $-1$, but I don't see how to do this.
Any help would be appreciated. Thank you.
Answer: Write $m=24n-1=p_1^{a_1}\cdots p_k^{a_k}$.
We know that $x$ must be odd, since if $2^x+1$ is divisible by $m$ with $x$ even, then $m\not\equiv 3\pmod{4}$.
Since $x$ is odd, we know that for each $p_i$ that $2$ is a square modulo $p_i$ iff $-1$ is a square modulo $p$. This means that either $p_i\equiv 3\pmod{8}$ or $p_i\equiv 1\pmod{8}$.
So you can't even get $m=8n-1$.


Parametric form of ellipse $x^2 + 16y^2 = 4$

Question: The given ellipse is $-$
$x^2 +16y^2 = 4$
I know the standard method of changing the equation of ellipse in parametric form.
Instead of complicated calculation, can we simply put $x= 2 \cos \theta , y= \frac{1}{2} \sin \theta $ to satisfy the equation. Does this still represent the same ellipse?  How do I know if my parametric equation is representing the same ellipse as given ,without using graph?
Comment: You can verify this by looking at the [graph](https://www.desmos.com/calculator/ecrp5w4sku). Your parametric form is the same as the initial one, assuming the range of $t$ is from $0$ to $2\pi$
Comment: @roman Just looking at a graph usually isn't enough to rigourously prove that two things are the same.
Comment: @roman is there any other way to verify this than graph?
Comment: @Arthur I know, but that helps to gain some intuition
Comment: @Mathsaddict That's parametric form, not polar.
Comment: @Arthur, thanks for pointing out. I edited my question.
Answer: In terms of 'intuition' - generally the only thing you need to be careful of this technique is that you may not obtain the whole ellipse. But upon checking the domain of $\theta$. This particular parameterisation that you have used is okay!
A choice of parameterisation is generally from the knowledge that it will satisfy the cartesian equation and that it is sufficiently variable.
For example if I wanted to find a parameterisation for the parabola $x^2=4ay$ I could simply say
$$x=2t^2 \ y=\frac{t^4}{a}$$
Indeed, it satisfies the equation - however this would not 'capture' the parabola for $x<0$. We need to be aware of such things when we choose a parameterisation.
Answer: In the case of an ellipse, the simplest solution is to make a change of variables, in order to get the equation of a circle.
For the equation
$$ x^2 +16y^2 = 4 $$
Just consider the change in variables:
$$X = x; \quad Y = 4y $$
To  get the new equation
$$X^2 + Y^2 = 4 $$
Therefore the equation of a circle of center $(0,0)$ and ray $R=2$.
The corresponding parametric equation is
$$X=R\cos \theta\quad Y=R\sin \theta $$
For $\theta$ varying from $0$ to $2 \pi$
Which corresponds to
$$x=2\cos \theta\quad y=\frac{1}{2}\sin \theta $$
Answer: Well, you can make the substitution in the equation, so you get:
$$(2\cos(\theta))^2+16(\frac 1 2 \sin(\theta))^2=4.$$
Therefore your parametrization is ok.


Como salvar registros de uma consulta mysql em C++

Question: Estou utilizando uma biblioteca em C++ que faz a consulta no banco de dados. Meu objetivo é fazer uma consulta, e salvar os dados em um arquivo txt, talvez até manipulando para ficar no formato json. O problema está que nao consigo recuperar o valor dos registros ao salvar o arquivo. Quando adiciono o valor row[i] , ele salva um valor binário, ao invés de caracteres ASCII. Aparentemente, mesmo se fosse em ASCII, duvido que retornaria o conteúdo do campo, visto que a função mysql_fetch_row provavelmente não faz isso. Não consigui achar alguma função fetchArray se é que exista. Segue o código
#include <windows.h>
#include <iostream>
#include <fstream>
#include <mysql/mysql.h>

using namespace std;

#define HOST "localhost"
#define USER "root"
#define PASS ""
#define DB "estoque"

MYSQL *conn;

int main()
{

    conn = mysql_init(NULL);

    if(mysql_real_connect(conn, HOST, USER, PASS, DB, 0, NULL, 0))
    {
        cout << "conectado \n";

    }
    //Select all table
    mysql_query(conn,"SELECT * FROM estoque");

    snprintf(buf, sizeof buf, "SELECT %s from %s", value, table);

    mysql_real_query(conn, buf, strlen(buf));

    // pointer of result
    MYSQL_RES *result;

    MYSQL_ROW row;
    unsigned int i;

    result = mysql_store_result(conn);
    row = mysql_fetch_row(result);

    while ((row = mysql_fetch_row(result)) != NULL)
    {
    for (i=0; i<mysql_num_fields(result); i++)

        cout << row[i] << endl;

    //  ############### ESCREVER EM ARQUIVO DE TEXTO O RESULTADO ############################

        std::ofstream Hypnos_FILE;
std::string TEXTO = row[i];
Hypnos_FILE.open("resultado.txt", std::ios::app);
if (Hypnos_FILE.is_open())
{
   std::cout << "Arquivo de texto aberto com sucesso!\n";

   Hypnos_FILE << TEXTO;

}
else
   std::cout << "Erro ao abrir arquivo de texto.";

Hypnos_FILE.close();

    //  ############### ESCREVER EM ARQUIVO DE TEXTO O RESULTADO ############################

    }
    // mysql_query(conn,"SELECT varchar1, varchar2 FROM estoque;");
    return 0;
}

Comment: Esse código nem compila.
Comment: Tem que instalar a biblioteca libmysql
Comment: Depois jogar a dll na pasta system32 do windows
Comment: Ele não compila, ele tem erros básicos, o problema não é esse, nem chega nesse ponto.
Answer: Primeiro você precisa verificar os valores de retorno adequadamente, começando pelo mysql_init() e mysql_real_connect(), e abortar o programa caso seja necessário. Se isso não acontecer o resultado pode ser inesperado e pode acarretar na gravação de dados inválidos em seu arquivo assim como já está acontecendo.
Em segundo você está descartando a primeira linha da tabela, pois há uma chamada à função mysql_fetch_row() ao relento.
Por último, e talvez a real razão do seu problema, você está usando o índice (variável i) com um valor inválido, acarretando um buffer overflow. Basta colocar a escrita do arquivo dentro do escopo do seu laço for para resolver este problema.
Segue abaixo um código simples que funciona como você provavelmente deseja:
#include <iostream>
#include <fstream>

#include <string.h>

#include <mysql/mysql.h>

using namespace std;

#define HOST "localhost"
#define USER "root"
#define PASS ""
#define DB "estoque"

MYSQL *conn;

int main()
{
    MYSQL *conn = mysql_init(NULL);
    if (!conn) {
        cout << "falha no mysql_init()" << endl;
        return 1;
    }

    conn = mysql_real_connect(conn, HOST, USER, PASS, DB, 0, NULL, 0);
    if (!conn) {
        cout << "falha no mysql_real_connect()" << endl;
        return 1;
    }

    cout << "conectado" << endl;

    const char *value = "*";
    const char *table = "banana";
    char buf[128];
    snprintf(buf, sizeof buf, "SELECT %s from %s", value, table);
    mysql_real_query(conn, buf, strlen(buf));

    MYSQL_ROW row;
    MYSQL_RES *result = mysql_store_result(conn);

    std::ofstream Hypnos_FILE;
    Hypnos_FILE.open("resultado.txt", std::ios::app);
    if (!Hypnos_FILE.is_open()) {
        std::cout << "Erro ao abrir arquivo de texto.";
        return 1;
    }
    std::cout << "Arquivo de texto aberto com sucesso!\n";

    unsigned int j = 0;
    while (((row = mysql_fetch_row(result)) !=NULL)) {
        cout << j << ":";
        Hypnos_FILE << j << ":";;
        j++;
        for (unsigned int i = 0; i < mysql_num_fields(result); i++) {
            cout << " " << row[i];
            Hypnos_FILE << " " << row[i];
        }
        cout << endl;
        Hypnos_FILE << endl;
    }

    Hypnos_FILE.close();

    mysql_close(conn);

    return 0;
}

Comment: Muito obrigado! Isso solucionou o problema.
Só tive que dar uma adaptada no código. Segue o gist da versão do seu código que solucionou o problema:
https://gist.github.com/paulosergioduff/de93765365eafdaad71c5ea52a82ff0e
Comment: Tome cuidado, pois agora você não está jogando fora apenas o primeiro valor da consulta, mas também o segundo. Recomendo que você dedique bastante a entender como está funcionando os laços do seu código.
Comment: Vou dedicar. Mas curiosamente retornou todos os registros. Mas vou ter cautela!


Application working fine in iOS 8 device but crashing in iOS 7 devices

Question: I have developed application using Xcode 6 which is using auto layout and size classes . It is working fine in iOS 8 devices but there is problem while running application on iOS 7 devices. In one view controller the application is crashing with the following exception :
<code>*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIView 0x7916a9c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key keyPath.'
</code>
I have checked all the connection in connection inspector ,those all are clean. Please suggest some solution to overcome this problem .
Comment: looks like its not related to os or connections. you are setting value to UIView, which I think you might have thought of Dictionary.
Comment: there is nothing to do with the dictionary , this problem lies in storyboard
Comment: I had similar problem when I was copying some buttons from one view to another. They stayed connected in IB but still I needed to reconnect them.
Comment: Remove all IBOutlets and re-connect them.
Comment: @Wongzigii: I am getting same exception after removing all the connections.
Comment: ` setValue:forUndefinedKey:` Also check the class identifier for the view you attached to.
Comment: @V-Xtreme, as others said issue should be with IBOutlet, remove your existing app in  your iOS 8 device and  clean and run in your iOS 8 device, you should get the same error as you see in iOS 7.
Answer: This is normally related to a deleted IBOutlet. Check your Storyboard/Nibs outlets and see if there anything marked with an "!": it means the corresponding outlet was removed.
Comment: I have already checked all the connections . I am not seeing ! mark before any connection. so seems like the connections are clean . also same storyboard working fine in iOS 8 devices


What team has shortest "longest losing streak" within a season in NBA history?

Question: Inspired by this question: What team has shortest longest losing streak within a season in MLB history?
What team has shortest "longest losing streak" within a season in NBA history? Off the top of my head, the 2015-2016 Golden State Warriors and the 1995-1996 Chicago Bulls are the most likely to have achieved short losing streaks given they lost 9 and 10 games throughout their respective seasons.
Answer: The 2015-2016 Golden State Warriors lasted an entire season without losing back-to-back games on their way of posting an all-time regular season best record of 73-9. Therefore, the shortest longest losing streak among NBA teams in one season is 1.
That very same season, the San Antonio Spurs lasted 78 games before losing back-to-back(-to-back) games. Next on the list was the 1984-1985 Celtics who lasted 74 games before losing back-to-back games.
Source
In addition, the 1995-1996 Bulls lasted 45 games before losing back-to-back games. Also, the Warriors went 146 games without losing back-to-back games between 2015-2017.


How to get Remaining Amount of Customer Deposits in Saved Search (includes SuiteScript code)

Question: I need to get the Remaining Balance of all Customer Deposits linked to a specific Sales Order
Remaining Balance = the available/unapplied amount
Below, I will show you how I solved this in a saved search.
As a bonus, I'll also include the SuiteScript 1.0 code to do the same.
Comment: you did not include your code example.
Comment: @lancegerday Yes I did. It's at the bottom of the answer... Am I meant to provide something more?
Answer: To show the Remaining Amount of all Customer Deposits linked to a specific Sales Order record:
Create a Transaction Saved Search as follows:

Criteria (Use Expressions)

( Type IS Deposit Application AND
Created From Fields > Sales Order IS Sales Order #xyz ) OR
( Type IS Customer Deposit AND
Created From IS Sales Order #xyz )

Results

Formula (Numeric) (Summary type SUM): CASE WHEN {type} = 'Customer Deposit' THEN {debitamount} ELSE -{creditamount} END

BONUS, SuiteScript 1.0 code to get Remaining Amount of all Customer Deposits linked to a specific Sales Order record:
function customerDepositsRemainingBalance(salesorder_internalid) {
    var filters = [[["type","anyof","DepAppl"],"AND",["createdfrom.salesorder","anyof",salesorder_internalid]],"OR",[["type","anyof","CustDep"],"AND",["createdfrom","anyof",salesorder_internalid]]];
    var columns = [new nlobjSearchColumn('formulanumeric',null,'SUM')];
    columns[0].setFormula("CASE WHEN {type} = 'Customer Deposit' THEN {debitamount} ELSE -{creditamount} END");
    var search = nlapiSearchRecord('transaction',null,filters,columns);

    if(search == null) return 0;
    return Number(search[0].getValue(columns[0]));
}

// Example Usage
balance = customerDepositsRemainingBalance(3247434); // returns 50
balance = customerDepositsRemainingBalance(3256644); // returns 0

Comment: Thanks for sharing. Please note that the search filter is bloated. You can get the same with Created From = SO# AND Type anyof (Deposit Application, Customer Deposit).


Behavior of async await with new threads

Question: I am trying to understand the precise behavior of async/await and am having a small amount of trouble wrapping my head around it.
Consider this example:
public async void StartThread()
{
    while(true){
        SomeOtherClass.SomeSynchronousStuff();
        var something = await SomeOtherClass.SomeOtherAsyncMethod();
    }
}

public void ConstructorForThisClass()
{
    Thread thread = new Thread(StartThread);
    thread.Start();
}

My understanding of async/await is that what is happening under the covers is that the compiler is essentially turning your code into a bunch of callbacks and storing state objects for each one.
So according to this, my questions are:

Will the newly created thread be running asynchronously? Meaning, while the thread is awaiting the SomeOtherAsyncMethod, will it be freed up to work on other work?
If the preceding is true, will the thread simply end and a thread pool thread take its place when the SomeOtherAsyncMethod returns?
How would I go about issuing the StartThread function on a thread pool thread rather than a managed thread?
When an awaitable method returns to its caller, is it forced to resume on the thread that calls it or can any free thread take its place?
Answer: Bare threads don't work great with async/await.

Will the newly created thread be running asynchronously? Meaning, while the thread is awaiting the SomeOtherAsyncMethod, will it be freed up to work on other work?

Actually, the thread will just exit. When StartThread resumes after the await, it will execute on a thread pool thread.

How would I go about issuing the StartThread function on a thread pool thread rather than a managed thread?

First, you'd want to change the return type of StartThread from void to Task. async void methods are intended for event handlers; using them in other places can cause all sorts of problems.
Then you can just call it via Task.Run:
var backgroundTask = Task.Run(() => StartThread());

When an awaitable method returns to its caller, is it forced to resume on the thread that calls it or can any free thread take its place?

By default, the await operator will capture the "current context" and resume on that context. This "current context" is SynchronizationContext.Current, unless it is null, in which case it is TaskScheduler.Current. Usually, this is either a UI/ASP.NET SynchronizationContext, or else it's the thread pool context (TaskScheduler.Default).
You may find my async intro helpful.
Answer:
Will the newly created thread be running asynchronously?

Your wording is a bit problematic. The creation of the thread is completely synchronous.

Meaning, while the thread is awaiting the SomeOtherAsyncMethod, will it be
  freed up to work on other work?

You're manually creating a thread using the Thread class, not a Threadpool Thread. It isn't shared inside the AppDomain. It will be freed up once it hits the first await keyword, but since you use it in an endless while loop, it won't get used for any other work other than that.

If the preceding is true, will the thread simply end and a thread pool
  thread take its place when the SomeOtherAsyncMethod returns?

Disregarding the former, since you don't use ConfigureAwait(false), then the continuation will run on an arbitrary ThreadPool thread. But that really depends on the context. Since you're running this delegate on a new Thread, that's what happens. But if you were, say, run this from the UI thread, then the continuation would of attempted to marshal itself to the UI message loop via the relevant TaskScheduler and the corresponding SynchronizationContext.

How would I go about issuing the StartThread function on a thread pool
  thread rather than a managed thread?

All threads initiated by the Thread class and the ThreadPool class are managed threads. If you meant "How do i run this delegate on the threadpool", than the answer is via Task.Run or via the ThreadPool static class.

When an awaitable method returns to its caller, is it forced to resume
  on the thread that calls it or can any free thread take its place?

If ran without ConfigureAwait(false), it is forced onto the current TaskScheduler and its underlying SynchronizationContext. This means that if you run inside a UI message loop, and call await there, it will attempt to post the continuation to it. If no custom TaskScheduler is available, it will use the default one, which is the threadpools scheduler.


12 bit strings with more zeros than ones

Question: I'm trying to solve how many twelve bit strings have more zeros than ones. Can someone sanity check my method please?
C(12,5) = 792 +
C(12,4) = 495 +
C(12,3) = 220 +
C(12,2) = 66 +
C(12,1) = 12 +
C(12,0) = 1 =
1586 ?
Comment: Correct. A trick to avoid calculating so many binomials is to see that the result is equal to $\frac{1}{2}\left(2^{12} - {12\choose 6}\right)$ since $2^{12} = \sum_{i=0}^{12}{12\choose i} = 2\sum_{i=0}^5 {12\choose i} + {12\choose 6}$
Comment: Assuming that the zeroes are indistinguishable, then yes, your method is right.
Comment: @Newb: That's surely so obvious as not to be worth stating! But if you _do_ think it worthwhile, then what about the ones? Don't they have to be indistinguishable too?
Answer: Your method is correct (apart from the way you write it down; as written, it reads as $C(12,5)=792+C(12,4)=\ldots=1=1586$, but clearly $C(12,5)\ne 792+C(12,4)$, … and $1\ne1586$), but unnecessarily complicated. A simpler way is as follows:
There are three types of 12 bit strings:

Those with more zeroes than ones.
Those with more ones than zeroes.
Those with the same number of zeroes and ones.

Obviously there are $2^{12}=4096$ 12 bit strings in total. Also, the number of strings of the first two types is equal, as you get one by bit-flipping the other. Finally, the last class has $\binom{12}{6}=924$ strings.
Therefore the first class (more zeroes than ones) has $(4096-924)/2 = 1586$ elements.
Answer: Yes, this method is correct:
$${12 \choose 5} + {12 \choose 4} + {12 \choose 3} + {12 \choose 2}+ {12 \choose 1}+ {12 \choose 0}=1586$$ As I am sure you know, this works because you have found the number of combinations where there are $5$ zeroes and the rest are ones, then the case where there  are four zeroes, three zeros, two zeros, one zero.
In the alternative form, the above can be constructed using standard derangement procedure, where the total number of characters in the string, and all of their permutations is divided by how many of each character is repeated with all of their permutations.
$$\frac{12!}{7!5!}+\frac{12!}{8!4!}+\frac{12!}{9!3!}+\frac{12!}{10!2!}+\frac{12!}{11!1!}+\frac{12!}{12!0!}$$
Which is of course the same as expanding out your expression.


Spectral density estimation for limited data

Question: We need an idea of the frequencies in the time series shown.
What can we expect from nonparametric spectrum estimation? Observing that much power is in frequency 4 years, of which we have only one period.
May we improve the result by proper window design, and what recommendations then?
We have modeled the logseries with an AR1 process. Far from perfect fit, but it serves some practical risk management purposes. How would you decide between nonparametric spectrum estimation or using the model's spectrum?
We are doing our research on spectral methods, but hope for some advice from practitioners. Given the limited data, how best go about obtaining important frequencies?
Answer: When attempting to determine what frequencies contribute the most to the variance of a process my research group relies heavily on multitaper spectral estimation techniques.  This involves estimating multiple spectra with different tapers / windows and using the weighted average as the overall estimate.
Specifically, we use Discrete Prolate Spheriodal Sequences (DPSS's or Slepian sequences (named after their "discoverer")) as our windows because they are doubly orthogonal and provide an optimal trade off between bias and variance (Spectrum Estimation and Harmonic Analysis - DJ Thomson - 1982).
One method to determine which frequencies are important is the harmonic F-test which determines "how sinusoidal" a particular frequency is in the data.  There are other methods that can be used, the harmonic F-test is one of the more simple.
R has a software package "multitaper" and Matlab has the function "pmtm()".  This method of estimation is parametric in the sense that in order to compute the windows, one parameters must be assigned: NW (the time bandwidth parameter) which is normally 3 < NW < 20 (for most purposes I have seen it is defaulted to 4).  This parameter sets the width of the window (W) based on the amount of data that is available (N).  Lastly, you must decide how many windows you would like to use (K) where K <= floor(2*NW-1).  The tapers after 2*NW actually concentrate power outside of -W to W.
If you use the multitaper package in R, the defaults are all set and you can get a general idea of the spectrum with very little tinkering.
Comment: Thanks! You're last phrase is great news for us :) Also, you nowhere say that it's foolish to expect results from such small dataset, am I interpreting you correctly?
Comment: My understanding is that you require at least 2 periods worth of data in order to obtain a reliable result at a frequency of f=1/P (if 'f' is the frequency of interest with period, P).  In other words - the lowest frequency that you would expect to get reliable results would be at a frequency of 1/<2 years> = 0.5 cycles/year.
How many data points do you have out of curiosity?
Comment: Yes sorry I haven't specified much. Time series is 250 weekly observations. Ok now I understand we shouldn't expect spectral info for periods above 2 years. The time series shown is the observed excitation of our system. We want to estimate its spectral content so as to predict spectrum of our system's output H(w)^2 S(w).


Compare this topology with the usual topology

Question: I have to compare the following topology with the usual one. Which of them is finer?
$\tau= \{U\subseteq \mathbb{R}^2:$ for any $(a,b) \in U$ exists  $\epsilon >0 $ where $[a,a+\epsilon] \times [b-\epsilon, b+\epsilon]\subseteq U\}$
By definition, $\tau\subseteq\tau_u $ if and only if for every $U\in \tau$ implies $U\in \tau_u$
However, how can I compare them using open basis?
THANK YOU!
Answer:
If $U \in \tau_u$, then for any $(a,b) \in U$, there is a basic open set
$$
(a-\delta, a+\delta)\times (b-\delta', b+\delta') \subset U
$$
you can take
$$
\epsilon = \min\{\delta/2, \delta'/2\}
$$
then
$$
[a,a+\epsilon]\times [b-\epsilon,b+\epsilon] \subset U
$$
Hence $U \in \tau$. So
$$
\tau_u \subset \tau
$$
The set
$$
[0,1)\times (-1,1) \in \tau\setminus \tau_u
$$
Do you see why?
Comment: @ Prahlad Vaidyanathar But you have to proof that for every $B\in \beta_u,$ for every $(c,d)\in B$, exists $D\in\beta : (c,d)\in D \subseteq B$ Is that epsilon correct? If I choose $(c,d)\in B=(a-\mu, a+\mu) x (b-\mu_1, b+\mu_1)$ which would be D?
Comment: @Vaidynathar In the second point, why do you chose the set $$
[0,1)\times (-1,1) \in \tau\setminus \tau_u
$$ instead of $$
[0,1]\times [-1,1] \in \tau\setminus \tau_u
$$ Could you explain it please? As these sets are in $\tau$ aren't they supposed to be closed?
Comment: @Blanca: Given $U \in \tau_u$ to show that $U \in \tau$ we just need to show that it has the defining property of the collection $\tau$, namely that for all $\langle a,b \rangle \in U$ there is an $\epsilon > 0$ such that $[ a,a+\epsilon ] \times [ b,b+\epsilon ] \subseteq U$.  This is what Prahlad has done.
Comment: @Blanca: As for your second comment, your proposed set $V = [0,1] \times [-1,1]$ does not work because it is not in $\tau$.  Note that $\langle 1,1 \rangle \in V$ but there is no $\epsilon > 0$ such that $[1,1+\epsilon]\times[1,1+\epsilon] \subseteq V$.  Do you see why?
Comment: @ArthurFischer: Thank you, I understand the second.So regarding to my first comment, Prahlad has used this notation?  $\tau\subseteq\tau_u $ if and only if for every $U\in \tau$ implies $U\in \tau_u$
Comment: Therefore, we can conclude that $\tau$ is $T_2$?
Comment: @Blanca: Yes, this is just the usual subset notation.  (Recall that a topology is just some family (set) of sets, and a topology $\tau^\prime$ is _finer_ than a topology $\tau$ (on the same set) if every $\tau$-open set is also $\tau^\prime$-open, which in turn means that $\tau \subseteq \tau^\prime$.  (But Prahlad actually shows that $\tau_u \subseteq \tau$ (the reverse of what you wrote in your comment above).
Comment: @Blanca: It is quite easy to show that if a topology $\tau_1$ is Hausdorff, then so is every topology finer than $\tau_1$.  (So in this case since $\tau$ is finer than the known Hausdorff topology $\tau_u$, then $\tau$ is also Hausdorff.)


Ability to change the attributes of the default selector when uploading through android

Question: We have a website where on mobile and specifically android devices when people want to change their "avatar" image the options that show up when we click on "upload" does not have the "Gallery". Instead it has "Documents" is there a way to define what to show and what not to show?
We are using PHP if that matters at all for development.
Please see below image.
Answer: well it depends on which intent you have used for upload button, in my opinion the most appropriate intent to call when you want user to select an image would be
<code>Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
</code>
it would make the gallery and other photo handling apps visible in the chooser dialog.
for more information you can go here
Hope it helps


Do temp variables slow down my program?

Question: Suppose I have the following C code:
int i = 5;
int j = 10;
int result = i + j;

If I'm looping over this many times, would it be faster to use int result = 5 + 10? I often create temporary variables to make my code more readable, for example, if the two variables were obtained from some array using some long expression to calculate the indices. Is this bad performance-wise in C? What about other languages?
Comment: Optimizing compiler will change that code to become effectively:

    `int result = 15 ;`
Comment: Compiler will optimize your code. It's more productive to focus on matters such as (part of) a calculation repeated within a loop that would be better done before the loop begins.
Comment: @PeteBecker I'm afraid that isn't a productive suggestion.  It is quite easy to try something like this and get the wrong impression because you happen to have picked (or failed to pick) a case which is an exception to the general rule.  Without a clear understanding of how a compiler works, just testing a few cases should not in any way convince you that it is true for all cases; making such generalisations can be very risky and often leads to error.
Comment: I think that he means any temporary variables, i.e.: is using a = b + c; d = a + e; slower than using a = b + c + d + e; it may potentially use more memory if done in ways the compiler can't optimise, but it shouldnt be slower. best focus or work productivity unless it's a commercial and critical performance code.
Comment: @WeatherVane although most compilers would do that as well, at least to some degree.  In general I think it'd be better to focus on code maintainability rather than microoptimisations like that.
Comment: As the several answers suggest, there is rarely a performance penalty from using temporaries in you code.  There is almost never a performance advantage to writing "long chain polymer" code that combines multiple functions into a single statement vs the simpler, easier to understand, easier to debug approach of doing the code in stages with the result of each stage assigned to a temp.
Comment: I'm going to guess no whenever the compiler can "optimize" the values. However, if reading from a file, there may be a difference.
Comment: To add a couple of $0.01: a) an optimizing compiler (like gcc) will identify the relationship of (many of) the values calculated and/or used in a piece of code; irrespective if they're constants or not. b) On the assembler level, operations usually store their result in a register which can then be used as input to the next operation; since there are normally more variables involved than HW registers available the compiler will have to decide after each atomic operation which values to keep in a register and which to store to RAM. This way it kind of introduces its own temporary
Comment: You can compile it both ways and then use time(1) to see time-based performance differences; you probably won't notice any unless you have a LOT of stack allocation and deallocation of little variables like that.
Comment: variable (`registerX`) during calculations. c) While the compiler is mapping variables, intermediates, and constants to physical registers anyway, it usually tries to do so in some optimized way, which will often eliminate any explicit temporary variable assignments present in the high level language code. So, when optimization is enabled when compiling, there will usually be no difference in the assembler/binary code generated.
Answer: A modern optimizing compiler should optimize those variables away, for example if we use the following example in godbolt with gcc using the -std=c99 -O3 flags (see it live):
#include <stdio.h>

void func()
{
  int i = 5;
  int j = 10;
  int result = i + j;

  printf( "%d\n", result ) ;
}

it will result in the following assembly:
movl    $15, %esi

for the calculation of i + j, this is form of constant propagation.
Note, I added the printf so that we have a side effect, otherwise func would have been optimized away to:
func:
  rep ret

These optimizations are allowed under the as-if rule, which only requires the compiler to emulate the observable behavior of a program. This is covered in the draft C99 standard section 126.96.36.199 Program execution which says:

In the abstract machine, all expressions are evaluated as specified by
  the semantics. An actual implementation need not evaluate part of an
  expression if it can deduce that its value is not used and that no
  needed side effects are produced (including any caused by calling a
  function or accessing a volatile object).

Also see: Optimizing C++ Code : Constant-Folding
Comment: @REACHUS, under `gcc` use `gcc -S -o asm_output.s app.c`
Comment: How can I see what assembly my C code will generate?
Comment: I don't think this addresses the question. The example given is simple, but I think the part about "if the two variables were obtained from some array using some long expression to calculate the indices" is more important here, and that can't be optimized away, or can it?
Comment: @ArturoTorresSánchez variables are just a human concept. There are no really variables. What the compiler sees are references to expressions and then the assembly code they are equivalent. When someone asks about variable overhead, really means the memory overhead of the result assembly instructions of that references (such as register/memory read/writes). "Erasing" a variable means if the compiler was smart enough to recognize that there is no reason to reload that "reference" again from memory, since the data is in another reference reachable by it in this context... (1)
Comment: @ArturoTorresSánchez ...  (The array element in this case). And nowadays, compilers are very proficient at global optimizations and expression folding (See the answer bellow about SSA form).
Comment: @REACHUS I added a link to the godbolt example where you can see the assembly
Comment: Thanks but what command line tool can I use, does GCC provide any flag for it?
Comment: @REACHUS under either `gcc` or `clang` you would use the `-S` to generate assembly.
Comment: I think no example is easier for a compiler to optimize.  Perhaps is easier for a human to predict, but no more.
Answer: This is an easy task to optimize for an optimizing compiler. It will delete all variables and replace result with 15.
Constant folding in SSA form is pretty much the most basic optimization there is.
Comment: Yes, however since "result" is unused technically the program would be empty.
Comment: If this is the whole program then indeed it would be empty. If not, 15 is "pasted" into all usages.
Answer: The example you gave is easy for a compiler to optimize.  Using local variables to cache values pulled out of global structures and arrays can actually speed up execution of your code.  If for instance you are fetching something from a complex structure inside a for loop where the compiler can't optimize and you know the value isn't changing, the local variables can save quite a bit of time.
You can use GCC (other compilers too) to generate the intermediate assembly code and see what the compiler is actually doing.
There is discussion of how to turn on the assembly listings here:Using GCC to produce readable assembly?
It can be instructive to examine the generated code and see what a compiler is actually doing.
Comment: This is the more useful answer.  The others went into the constant-folding part of things, rather than the local-copy of a memory location point.  Assigning globals and array elements to locals is often helpful when the compiler can't prove that input arrays don't overlap, or that some unknown function being called doesn't can't make changes to the array.  This often stops the compiler from reloading the same memory location multiple times.
Answer: While all sorts of trivial differences to the code can perturb the compiler's behavior in ways that mildly improve or worsen performance, in principle it it should not make any performance difference whether you use temp variables like this as long as the meaning of the program is not changed. A good compiler should generate the same, or comparable, code either way, unless you're intentionally building with optimization off in order to get machine code that's as close as possible to the source (e.g. for debugging purposes).
Comment: The point about "as long as the meaning of the program is not changed" is key.  There are many cases where two ways of writing a program will have slight semantic differences that might not matter to a programmer, but would require a compiler to generate much less efficient code for one than would be needed for the other.
Comment: Exactly what supercat said: the compiler can't always prove that two pointers / arrays don't overlap, or that a function call can't change the contents of memory.  Thus, it sometimes is forced to generate multiple loads of the same location, but using `int a = arr[i]` would let the compiler keep the value in a register across function calls and writes through other pointers.
Comment: I agree completely with the points made by supercat and Peter Cordes.
Answer: You're suffering the same problem I do when I'm trying to learn what a compiler does--you make a trivial program to demonstrate the problem, and examine the assembly output of the compiler, only to realize that the compiler has optimized everything you tried to get it to do away.  You may find even a rather complex operation in main() reduced to essentially:
push "%i"
push 42
call printf
ret

Your original question is not "what happens with int i = 5; int j = 10...?" but "do temporary variables generally incur a run-time penalty?"
The answer is probably not. But you'd have to look at the assembly output for your particular, non-trivial code. If your CPU has a lot of registers, like an ARM, then i and j are very likely to be in registers, just the same as if those registers were storing the return value of a function directly. For example:
int i = func1();
int j = func2();
int result = i + j;

is almost certainly to be exactly the same machine code as:
int result = func1() + func2();

I suggest you use temporary variables if they make the code easier to understand and maintain, and if you're really trying to tighten a loop, you'll be looking into the assembly output anyway to figure out how to finesse as much performance out as possible. But don't sacrifice readability and maintainability for a few nanoseconds, if that's not necessary.
Comment: The solution is to look at the compiler output for functions that operate on parameters, rather than `main()` operating on compile-time constants.  e.g. here's a simple example of summing a float array, with gcc asm output from godbolt:  http://goo.gl/RxIFEF
Comment: I was trying to say that you can put pretty trivial code in a function, and see the asm for it.  (I think this turned into an argument over the definition of "trivial", which isn't what I intended...).  Your example used a function call to generate `i` and `j`.  My example would be to make `i` and `j` function *parameters*, so they'd be just sitting there in registers at the start of the code for the function.
Comment: I believe that's what I said: "you'd have to look at the assembly output for your particular, non-trivial code". ;)

Do temp variables slow down my program?


How do I pass parameters as JSON format to the HTTP client request?

Question: How can I pass parameters to the http client get request in json format:
$clientFactory = \Drupal::service('http_client_factory');
$client = $clientFactory->fromOptions(['verify' => FALSE]);

$params = '{"data1":"",
"data2":"mydata"
"data3":"mydata",
"data3":"mydata",
"data5":"mydata"}';

$url = 'https://myendpointurl';
try {
  $response = $client->get($url, array(
      'headers' => array('Accept' => 'text/plain'),
    )
  );
  $data = json_decode($response->getBody());
}
catch (RequestException $e) {
  watchdog_exception('my_module', $e->getMessage());
}

Where I need to put $params variable in my code?
Answer: You can add it to the request like so:
use Drupal\Component\Serialization\Json;
...
$response = $client->get($url, [
    'headers' => ['Accept' => 'text/plain'],
    'json' => Json::decode($params),
]);

Comment: what's the advantage of using `Json::decode($params),` over `json_decode`?


How to edit the template of a node teaser that is sent in a newsletter email?

Question: I have a content type for my newsletter, it has an entity reference field that displays the latest articles of my site in a teaser format.
I tried to edit the teaser display in my node.tpl.php like this:
<?php if ($teaser): ?>

  <?php else: ?>

  <?php endif; ?>

in the website all is correct, what i put inside the if, is working but just in the website, but when i send the newsletter, the email arrives with something very different (something like a standard format), could you please tell me were can i edit the way my newsletter send the emails?
Answer: I think it would be easiest to change the teaser in the Manage Display tab under the appropriate content type. That will apply everywhere.
However if you have custom theme stuff you want to accomplish you'll have to make sure you're affecting the correct theme. It sounds like the e-mail generator may be using a different theme to send the e-mail?
A simple way to test for this is putting
HELLO
 in a template that you think it's using.


Hyper parameters tuning: Random search vs Bayesian optimization

Question: So, we know that random search works better than grid search, but a more recent approach is Bayesian optimization (using gaussian processes). I've looked up a comparison between the two, and found nothing. I know that at Stanford's cs231n they mention only random search, but it is possible that they wanted to keep things simple.
My question is: which approach is generally better, and if the answer is "sometimes random search, sometimes Bayesian" when should I prefer one method over another?
Comment: Google is selling their deep learning cloud services now and pushing a feature that automatically tunes your hyperparameters with Bayesian optimization...of course claiming it does the best and is faster as well (searching the hyperspace more efficiently). There are several papers out there that evaluate BO vs RS as well showing BO doing just slightly better. IMO from what I've seen, the diff is something you'd care about more in a Kaggle competition than real life.
Answer: I think that the answer here is the same as everywhere in data science: it depends on the data :-)
It might happen that one method outperforms another (here https://arimo.com/data-science/2016/bayesian-optimization-hyperparameter-tuning/ people compare Bayesian hyperparameter optimization and achieve a better result on the San Francisco crime kaggle challenge than with random search), however I doubt that there is a general rule for that. You can see a nice gif here (http://blog.revolutionanalytics.com/2016/06/bayesian-optimization-of-machine-learning-models.html) where people show the 'path' that Bayesian optimization takes in the landscape of hyperparameters, in particular, it does not seem as if it outperforms random search in general...
I think the reason why people tend to use Bayesian hyperparameter optimization is that it just takes less training steps in order to achieve a comparable result as compared to random search with a sufficiently high number of experiments.
Summarising in one sentence:
*When training time is critical, use Bayesian hyperparameter optimization and if time is not an issue, select one of both... *
Usually I am too lazy to implement the Bayesian stuff with Gaussian Processes if I can achieve the same result with random search... I just train Gradient Bossting ensembles on 'few' data, so for me, time is not an issue...
Answer: Bayesian optimization is better, because it makes smarter decisions. You can check this article in order to learn more: Hyperparameter optimization for neural networks. This articles also has info about pros and cons for both methods + some extra techniques like grid search and Tree-structured parzen estimators. Even though it was written in order to show pros and cons of different methods for neural networks the main knowledge is generalizable for any other machine learning domains
Comment: I just wanted to point out that in your first sentence, the actual reason for why Bayesian optimization is better is not that the decisions being made are _smarter_, it is about decisions being _made at all_.
Answer: Of note, Bayesian hyperparameter optimization is a sequential process, so it may take longer than some other approaches able to search or be conducted in parallel.


Error while running Logistic Regression in pyspark mlib

Question: I am having a dataframe(df_ml_nullable), like this :
<code>+-----+--------------------+
|label|            features|
+-----+--------------------+
|  0.0|[127.0,132.0,123....|
|  0.0|[67.0,67.0,67.0,6...|
|  0.0|[-29.0,-30.0,-28....|
|  4.0|[31.0,31.0,31.0,3...|
|  0.0|[39.0,40.0,42.0,4...|
+-----+--------------------+
</code>
Below is the schema of this data frame:
df_ml_nullable.printSchema()
<code>root
 |-- label: double (nullable = false)
 |-- features: vector (nullable = false)
</code>
I try to run the logistic regression like this :
<code>    from pyspark.ml.linalg import Vectors
    from pyspark.ml.classification import LogisticRegression
    lr = LogisticRegression(maxIter=10, regParam=0.01)
    (train_d,test_d)=df_ml_nullable.randomSplit([0.7, 0.3])
    model1 = lr.fit(train_d)
</code>
When I try to run this I get this error :
IllegalArgumentException: u'requirement failed: Column features must be of type struct,values:array> but was actually struct,values:array>.'
Has anyone faced this issue?
Comment: You should check what vector type you are using. Depending on your Spark version there can be a conflict between spark.mllibs and spark.ml, if you use the regression of one package with the vector type of the other package you get this error.
Comment: @Vincent Thanks a lot for the reply. The feature column was originally a double array ( output of a collect_list operation). I converted it to a vector using the udf below:
`def fn(row):
    ln = len(row)
    idx = range(ln)
    sp_vc = Vectors.sparse(ln,idx,row)
    return(Vectors.dense(row))
    #return(sp_vc)
usr_fn = F.udf(fn,VectorUDT())
df_indexed_vct = df_indexed.withColumn("features",usr_fn(F.col('pump_sequence')))`
So in the return type of udf should i use a different vector type?
A silly question - how do I check the version of spark.ml and spark.mlib in my environment?
Answer: The problem was with the import. Instead of importing from ml I was importing the vectors from mllib. The below correction did the trick:
<code>#from pyspark.mllib.linalg import Vectors, VectorUDT
from pyspark.ml.linalg import Vectors,VectorUDT
</code>
@ Vincent - Thanks for the hint.


What causes duplicate ACK records?

Question: We're reviewing Wireshark captures from a few client machines that are showing multiple duplicate ACK records which then triggers retransmit and out-of-sequence packets.
These are shown in the following screen shot.  .26 is client and .252 is server.

What causes the duplicate ACK records?
More background if it helps:
We're investigating network throughput concerns at one particular client site.  The perceived issue from a user interface perspective is that data is being transmitted slowly despite an underutilized 1gbps WAN connection.
Almost all of the client machines have the same issue, tested at more than 20 machines.  We did find two machines that do not have the problem.  We're in the process of identifying what is different in their configuration.  We did notice that in the two machines that do not have the problem, we only ever saw at most one duplicate ACK record.  The machines that have the problem usually have three duplicate ACK records.  One notable difference is that the machines that work fine all belong to members of the network operations team and all of other machines are for "regular" employees.  The machines are supposed to be standard but the network admins could have made changes on their local systems, which is another aspect we're researching.
We tried changing the TcpMaxDupAcks setting on the server but the value we really need is 5 and the valid range is only 1-3.
Server is Windows Server 2003.  Clients are all enterprise managed Windows XP.  All clients, including the two working ones, have Symantec anti-virus installed.
This is the only client site out of hundreds that has exhibited this problem.
pathping shows 56ms RTT and consistent 0/100 packet loss even from the problem machines.
Thanks,
Sam
Comment: What kind of routing switching hardware is between the two endpoints?
Comment: @SpacemanSpiff, there's a Cisco ASR 1006 router.
Comment: Are the IT staff and clients on the same switching equipment? Can you take one of their machines to the IT area and see the problem go away?
Answer: Note: I'm assuming that this capture was taken on the client machine.
A brief summary on TCP sequencing: TCP reliably delivers streams of bytes between two applications. "Reliably" in this case means that, among other things, TCP guarantees to never deliver out of order data to a listening application.
In-order, reliable delivery is implemented through the use of sequence numbers. Every packet in each stream is assigned a 32 bit sequence number (remember that TCP is effectively two independent streams of data, A->B and B->A). If A sends an ACK to B, the value in the ACK field is the next sequence number A expects to see from B.
From the above, it appears that at least one TCP segment being sent from the server to the client was lost. The three duplicate ACKs in sequence are an attempt by the client to trigger a fast retransmit. When a TCP sender receives 3 duplicate acknowledgements for the same piece of data (i.e. 4 ACKs for the same segment, which is not the most recently sent piece of data), it can reasonably assume that the segment immediately after the segment being ACKed was lost in the network, and results in an immediate re-transmission.
In this case, the re-transmission gets through, and is identified by Wireshark as out-of-order.
As mentioned by joeqwerty, packet loss is most often caused by congestion. It may also be a result of CRC or other errors on a link, due to a bad interface card, loose cable, etc. I'd look at the stats of every link along the path to see if any are highly utilized and/or are experiencing large numbers of errors.
If you can't see any obvious candidates, perform concurrent packet captures at multiple points along the path to try and isolate where the loss is occurring.
What kind of WAN connection is in use here? Is it a dedicated line? MPLS VPN link? IPsec VPN over the public internet? Something else?
Comment: Thanks for your comments.  You're right, the packet capture is from the client.  If I understand what you're saying, the duplicate ACKs are not the client doing anything wrong but are actually a trigger from the client that it didn't receive a different record (the one after the ACKs).  Is that correct?  What things can I look into on the client PC that would cause this?  If it's not a client PC problem why would it consistently show up on some clients and not others?
Comment: The WAN is "two point to point circuits" between three sites on the east coast and mid-west United States.
Comment: That's correct; the DUPACKs are a symptom of packet loss. As to why the issue would occur on some clients and not others, you need to work out what's common to the affected clients. Are they all in the same office? Going through common network infrastructure? (A switch or a link?). One thing that's worth doing is using `mtr` (or `pathping` on Windows) on each of the impacted machines and seeing if there are any common hops along the path to the server which seem to be experiencing packet loss. Do you have a network monitoring system you can use to look at switch port data?
Answer: While you are isolating where the problem is, think of a packet dump as just one of the symptoms... As an analogy,  if someone walks into the doctor's office with chest pains, the doc won't spend three hours investigating the nature of the pain.  He spends about two-minutes on that and then knows that 95% of the causes are either heartburn or angina...  In the same way, if you see duplicate ACKs, don't rat-hole on the weeds of the trace right away.
After the connection establishes, slow TCP performance is not always because of transit network problems; sometimes it comes as the result of server CPU or disk limitations... and occasionally because of some issue on a client PC.  I have chased my tail for weeks digging into the weeds of wireshark traces only to give up and find the problem relatively quickly with mtr, or by looking at other host metrics such as CPU and disk I/O.
Your first task is to prove whether this is a network issue or a host-level issue.  Focus on sending real traffic through your network and prove whether you're queuing / loosing / re-ordering Note 1 it; that always is the bottom-line for a potential network issue like this.
I would do a ping sampling for an extended period of time (typically an hour for me) between the client and server while the throughput problem is happening; you can use mtr or ping plotter freeware for this.  If you're consistently loosing packets at some hop, and all hops afterwards loose as much or more, then you have a potential network suspect.  Keep in mind that device ICMP rate-limiting can cause some hops to appear that they loose packets... that's why you want to look for a trend starting from that hop, and those following.

Note 1 If you are re-ordering traffic, that will show up rather quickly in the expert info field that wireshark provides
Comment: @Sam: Just a point regarding troubleshooting network problems: every network has "issues". The key is determining whether those issues are causing performance and/or connectivity problems. You'll find duplicate ACK's, TCP Retransmits, broadcasts, errant protocols, etc. on every network. You should focus on the volume of duplicate ACK's and the hosts most involved in sending the duplicate ACK's to determine if that's really a symptom of a larger problem or just the natural operation of the network. If I see 5 duplicate ACK's out of 1,000 packets I'm not going to give it a second thought.
Comment: Agree that blaming the network by default isn't a good approach. Instrumenting throughout the stack is always good practice. However in this case, the DUPACKs, out-of-order and retransmitted segments do seem to be indicative of some sort of network loss between the two endpoints.
Comment: @Murali Suriar, let's go with your assertion (which has a decent chance of being right)... then what next?  You have to isolate **why** there is packet loss.  We IT people have mysteriously fallen in love with `wireshark` to the point that we like looking at the microscope far too long.  The point I'm making is take a quick glance at the `pcap`, after that you're better off spending cycles on instrumenting packet loss, CPU cycles, and disk I/O than delving deep into the annals of TCP.  There is a time to do that, but it normally is not at this stage of analysis.
Comment: @Mike agreed, which is why I suggested looking for errors/utilisation information for devices along the path as a first step. I'm not a big fan of ICMP based diagnostics other than for reachability. As you say, rate limiting and incorrectly configured ACLs/firewalls can make it unreliable; though in an enterprise network (which this sounds like), MTR can often point you in the right direction. The other issue with MTR is that it often only points at one problem; it's entirely possible that there are *multiple* faults along the path, which you won't be able to find until you fix the first one.
Comment: We are not disagreeing, ICMP with TTL-stepping is not a panacea and there can be multiple faults.  However, for all it's flaws dealing with firewalls and load-balancers, ICMP is the best remote diagnostic we have unless you can run host-level instrumented TCP/UDP sessions on the specific application ports in question... even then you can only say, this socket is retransmitting a lot... but why?  70% of the time, I'm pulling out `mtr` or it's ilk, and I've been solving problems the same way for the last 15 years.  Once I've focused in on a specific device, then we can look at drop counters
Comment: Thank you for your answer.  In my case I know enough to run Wireshark and see that there are duplicate ACKs and that it is a problem.  I don't know what are the possible causes and what to look into, hence the question.  I'm also not blaming the network.  On the contrary, I think it's almost certainly a client PC issue.  Most standard-issue enterprise managed PC's exhibit the problem but the two PC's that belong to network administrators do not.  My question is what types of things no the client PC can cause duplicate ACK records?
Comment: @joeqwerty, thanks, I'm focusing on the duplicate ACK's because that's the only thing I see different in the clients that work (i.e., can stream video smoothly) and clients that don't work (cannot stream smoothly).  The ones that work have an occasional duplicate ACK and the ones that don't work have much more regular three duplicate ACKs in sequence.  It's what stood out as different.
Comment: @Sam, this whole exchange is symptomatic of what I was saying in my post.  Quit obsessing about TCP ACKs.  You are troubleshooting performance.  There are three network-related causes for IP performance problems: packet loss, packet delay, and packet reordering (if TCP).  Your job is to find out whether you have any of that, or if there are contributions from the individual hosts.
Comment: @Mike Pennington, I wouldn't say I'm obsessing over the ACKs. I see a problem--certain clients can't stream video smoothly. The ACKs is one difference between the clients that work and those that don't. I don't understand what that difference, so I'm asking here. I'm also concurrently investigating five other things that may be totally unrelated to the ACKs that could effect the streaming performance. I still would like to learn more about this particular issue while I investigate everything fully. Murali Suriar's answer actually explained a lot; I'm glad to have learned something new today.
Answer: By seeing lots of [TCP segment of reassembled PDU] without ACKs - I'd say those ACKs are likely shown as [TCP Dup ACK ...] due to Selective Acknowledgement (aka SACK) behavior.
Example:

client sends data parts (...,0,1,2,3,4,5,6,...)
server acked (0), then received (2,4,3), then (5), then (6) and never got (1)

In above scenario - server can legitimately choose to ack (2-4) range first, then (2-5) range, then (2-6) range. While forming the "(A-B) range ack" packet - server has to specify the last-acked part (0) in TCP header. Wireshark marks the range-acks (SACKs) as [TCP Dup ACK ...] because all those range-acks have same last-acked part value in TCP header (Ack=872619 in Your case).
Answer: Duplicate ACK's in combination with slow network performance sounds like a network congestion problem to me. Look at the volume and rate of broadcast traffic on the network. Make sure to look at physical layer and network layer broadcasts as well as multicasts.

What causes duplicate ACK records?


Every principal $G$-bundle over a surface is trivial if $G$ is compact and simply connected: reference?

Question: I'm looking for a reference for the following result:

If $G$ is a compact and simply connected Lie group and $\Sigma$ is a compact orientable surface, then every principal $G$-bundle over $\Sigma$ is trivial.

The proof supposedly uses homotopy theory and classifying spaces. I'm not very familiar with either, so I don't know where to start looking.
Answer: The homotopy theoretic proof is as follows: Let $E \longrightarrow \Sigma$ be a principal $G$-bundle over a surface $\Sigma$. Such a bundle is determined by a homotopy class $[f_E] \in [\Sigma, BG]$ by classifying space theory. Since $G$ is simply connected (and presumably connected), the classifying space $BG$ is $2$-connected (i.e. connected, simply connected, and $\pi_2(BG) = 0$). A surface $\Sigma$ has the homotopy type of a $2$-dimensional CW-complex. Hence $[\Sigma, BG] = 0$ and it must be that $[f_E] = 0$, so that $E$ is the trivial bundle.
We have the following elementary fact in general:

Fact 1. If $X$ is an $n$-dimensional CW complex and $Y$ is $n$-connected, then $[X, Y] = 0$.

Furthermore, a basic property of classifying spaces is the following:

Fact 2. If $G$ is $(n-1)$-connected, then $BG$ is $n$-connected.

By the same argument as above, these two facts imply the following general fact:

Fact 3. If $G$ is an $(n-1)$-connected Lie group and $X$ is an $n$-dimensional manifold, then every principal $G$-bundle over $X$ is trivial.

For example, every $\mathrm{SU}(2)$-bundle over a $3$-manifold is trivial.

I'll describe a few references here. First of all, if you want to know exactly how the classifying space $BG$ is constructed and that the principal $G$-bundle $EG \longrightarrow BG$ is universal, you can read sections 4.11-4.13 of Husemöller's Fibre Bundles. The main idea is the following:

For a topological group $G$, there is a topological space $BG$ and a principal $G$-bundle $EG \longrightarrow BG$ with $EG$ contractible such that for any paracompact space $X$, isomorphism classes of principal $G$-bundles over $X$ are in one-to-one correspondence with homotopy classes of maps from $X$ to $BG$ as follows:
  $$[X, BG] \leftrightarrow \text{Prin}_G(X),$$
  $$[f] \leftrightarrow f^\ast EG.$$

Here $f^\ast EG$ is the pullback of $EG \longrightarrow BG$ by any map $f$ representing to homotopy class $[f] \in [X, BG]$. So to classify principal $G$-bundles over a paracompact space $X$ (e.g. a manifold), we can look at homotopy classes of maps from $X$ to $BG$. If we're lucky, some properties of $X$ and $BG$ can let us find $[X, BG]$ without doing much work.
I don't know of a place where Fact 1 appears as a theorem, but it is Exercise 117 in Davis and Kirk's Lecture Notes in Algebraic Topology. You can solve by just working cell-by-cell.
Fact 2 follows from the long exact sequence of a fibration in homotopy and the fact that $EG$ is contractible. The long exact sequence of a fibration goes as follows: if $F \hookrightarrow E \to B$ is a fibration, then we have a long exact sequence
$$\cdots \to \pi_{k+1}(B) \to \pi_k(F) \to \pi_k(E) \to \pi_k(B) \to \pi_{k-1}(F) \to \cdots.$$
If we apply this to $G \hookrightarrow EG \to BG$ and use the fact that $EG$ is contractible, we find that $\pi_{k+1}(BG) \cong \pi_k(G)$, so if $G$ is $n$-connected, $BG$ is $(n+1)$-connected.
Finally, Fact 3 is just a corollary of Facts 1 and 2 as well as the basic properties of classifying spaces.
Comment: It may be worth pointing out that the "following general fact" isn't actually that much more general.  Every compact Lie group either has $\pi_1$ or $\pi_3$ nontrivial.  Further, every noncompact (connected?) Lie group deformation retracts onto its maximal compact subgroup, so the only noncompact 3-connected Lie groups are diffeomorphic to $\mathbb{R}^n$ and this then implies $BG$ is contractible, so $[X,BG] = \{\ast\}$.
Comment: I am familiar with the fundamental group and with singular and de Rham (co)homology. I can define the higher homotopy groups, but I cannot state any relevant "big theorems" by heart. I think I know what classifying spaces are about, meaning that in particular I more or less know the correspondence between homotopy classes of maps $\Sigma\to BG$ and $G$-bundles over $\Sigma$. I have never really worked with higher homotopy groups. (And I'm aware it may be hard to give a good answer to someone lacking this knowledge.)
Comment: @DaanMichiels: The main thing about classifying spaces to know is that the universal $G$-bundle $EG$ over $BG$ is contractible.  Then the long exact sequence in homotopy associated to the fibration $G \to EG \to BG$ tells you that $\pi_n(BG) = \pi_{n-1}(G)$.
Comment: @EricO.Korman - Thank you, that helps (though I have to think about it a little bit).
Comment: Thanks for the answer. Any reference I can take a look at?


How to prevent file from being overridden when reading and processing it with Java?

Question: I'd need to read and process somewhat large file with Java and I'd like to know, if there is some sensible way to protect the file that it wouldn't be overwritten by other processes while I'm reading & processing it?
That is, some way to make it read-only, keep it "open" or something...
This would be done in Windows environment.
br,
Touko
Answer: you want a FileLock:
<code>FileChannel channel = new RandomAccessFile("C:\\foo", "rw").getChannel();

// Try acquiring the lock without blocking. This method returns
// null or throws an exception if the file is already locked.
FileLock lock = channel.tryLock();

// ...

// release it
lock.release();
</code>
for simplicity's sake I've omitted an enclosng try/finally block but you shouldn't in your production code
Comment: Hi still, is there some way to prevent the file being edited?

If I acquire exlusive lock under Windows and open it with text editor (Scite for example), it is opened with empty content but if I write something and save, the file ends up emptied in the end?

In contrast, if I try to open it with Eclipse, it tells me that the file can't be opened because it's locked by another process...

This probably ends up to Windows, but somebody got some ideas?
Answer: Working mostly fine, got some issues : as the locking doesn't seem to be totally absolute.


Is there a special case of the EM algorithm for exponential family distributions?

Question: According to Wikipedia, the formal definition of the EM algorithm is

The EM algorithm seeks to find the MLE of the marginal likelihood by iteratively applying these two steps:
Expectation step (E step): Define $Q(\boldsymbol\theta\mid\boldsymbol\theta^{(t)})$ as the expected value of the log likelihood function of $\boldsymbol\theta$, with respect to the current conditional distribution of $\mathbf{Z}$ given $\mathbf{X}$ and the current estimates of the parameters $\boldsymbol\theta^{(t)}$:
$$
Q(\boldsymbol\theta\mid\boldsymbol\theta^{(t)}) = \operatorname{E}_{\mathbf{Z}\mid\mathbf{X},\boldsymbol\theta^{(t)}}\left[ \log L (\boldsymbol\theta; \mathbf{X},\mathbf{Z})  \right] \,
$$
Maximization step (M step): Find the parameters that maximize this quantity:
$$
\boldsymbol\theta^{(t+1)} = \underset{\boldsymbol\theta}{\operatorname{arg\,max}} \ Q(\boldsymbol\theta\mid\boldsymbol\theta^{(t)}) \,
$$

In instances where $p(\mathbf{x},\mathbf{z})$ is an exponential family distribution, I have found that authors used
$$
Q(\boldsymbol\theta\mid\boldsymbol\theta^{(t)}) = \log L (\boldsymbol\theta; \mathbf{X},\operatorname{E}_{\mathbf{Z}\mid\mathbf{X},\boldsymbol\theta^{(t)}}\left[\mathbf{Z}\right]) \,
$$
instead of
$$
Q(\boldsymbol\theta\mid\boldsymbol\theta^{(t)}) = \operatorname{E}_{\mathbf{Z}\mid\mathbf{X},\boldsymbol\theta^{(t)}}\left[ \log L (\boldsymbol\theta; \mathbf{X},\mathbf{Z})  \right] \,
$$
where $\operatorname{E}_{\mathbf{Z}\mid\mathbf{X},\boldsymbol\theta^{(t)}}\left[\mathbf{Z}\right]$ is the estimated data. Are these two equations equivalent?
Comment: @AryaMcCarthy "In the former, you're taking an expectation over all possible Z's. Z can be of any type; e.g. a tree or set. But in the latter, you're taking the expected value." not sure what the difference between "expectation over all possible Z's" and "expected value" is here, could you explain? As for your second comment, yes you're right. The longer version of this question was accidentally submitted. Not sure why. I deleted it.
Comment: This seems like a type error to me. In the former, you're taking an expectation over all possible Z's. Z can be of any type; e.g. a tree or set. But in the latter, you're taking the expected value. But what's an 'expected tree'? It only makes sense if Z is a continuous value. (And even then, I'm not sure this is what you _want_ to do. If you took the mode value instead of the mean, you'd recover Viterbi EM.)
Comment: Is this an abridged version of your question here? https://stats.stackexchange.com/q/547900/155836
Comment: Oh, there's no distinction. I wrote poorly. You're feeding the expected value into the likelihood function, instead of a given value. But certain types don't have an expected value.
Comment: @AryaMcCarthy what if I assume that $\operatorname{E}_{\mathbf{Z}\mid\mathbf{X},\boldsymbol\theta^{(t)}}\left[\mathbf{Z}\right]$ is always defined? Would the two equations be equivalent then? I'm mainly asking this because I have rarely seen the first equation used in practice, while I've seen the second many times. It seems the first equation is only good for demonstrating that this is the lower-bound for the evidence, but I'm not sure.
Answer: Suppose we want to estimate the parameter $\theta$ of the distribution $p(z;\theta)$ of a random variable $Z$. If we had samples $z_1, z_2, \dots, z_N$, we could easily do this using maximum likelihood estimation. However, suppose instead we are given the samples $x_1,x_2,\dots,x_N$ of another random variable $X$, where $X$ and $Z$ are related via the joint distribution $p(x,z)$. Since $p(x,z) = p(x \mid z) \cdot p(z;\theta)$, then $p(x,z) = p(x,z;\theta)$.
We could then define the complete-data likelihood as
$$
p(x_1,x_2,\dots,x_N,z_1,z_2,\dots,z_N)
$$
We can also assume that each pair $(x_i,z_i)$ is independent of and identically distributed to every other pair for $i \in \{1,2,\dots,N\}$ such that
$$
p(x_1,x_2,\dots,x_N,z_1,z_2,\dots,z_N) = \prod_{i=1}^N p(x_i,z_i;\theta)
$$
and
$$
\log p(x_1,x_2,\dots,x_N,z_1,z_2,\dots,z_N) = \sum_{i=1}^N \log p(x_i,z_i;\theta)
$$
Next, let
$$
p(x_i,z_i;\theta) = h(x_i,z_i) \cdot \exp(\eta (\theta) \cdot T(x_i,z_i) + A(\theta))
$$
such that
$$
\log p(x_i,z_i;\theta) = \log h(x_i,z_i) + \eta (\theta) \cdot T(x_i,z_i) + A(\theta)
$$
Then, note that
\begin{align}
\mathbb{E}\left[\log p(x_i,z_i;\theta) \mid x_i\right] &= \mathbb{E}\left[\log h(x_i,z_i) \mid x_i\right] + \eta (\theta) \cdot \mathbb{E}\left[T(x_i,z_i) \mid x_i\right] + A(\theta) \\
\log p(x_i,\mathbb{E}\left[z_i \mid x_i\right];\theta) &= \log h(x_i,\mathbb{E}\left[z_i \mid x_i\right]) + \eta (\theta) \cdot T(x_i,\mathbb{E}\left[z_i \mid x_i\right]) + A(\theta)
\end{align}
and so, in general,
$$
\mathbb{E}\left[\log p(x_i,z_i;\theta) \mid x_i\right] \neq \log p(x_i,\mathbb{E}\left[z_i \mid x_i\right];\theta)
$$


postgresql function execution taking too long

Question: Below is my Postgres funtion
CREATE OR REPLACE FUNCTION public.test_me(test_id _int4)
  RETURNS TEXT
AS $BODY$
DECLARE
  tempIds INT[];
  returnValue TEXT := 'Success';
BEGIN

  RAISE NOTICE ' selecting  abc_id for  : %', test_id;

  tempIds := ARRAY ( SELECT DISTINCT abc_id
                     FROM test
                     WHERE test_id IN ( SELECT unnest(test_id)) );

  RAISE NOTICE ' selected abc_id are : %', tempIds;

  RETURN returnValue;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;

and I am executing it like this
select test_me('{123}'::int[]);

It takes forever to run and I observed that only the first notice is printed during the execution and it never reaches to the second notice statement. when I run the select query inside ARRAY method it executes quickly.
Edit 1: When I tried running the select query like this
SELECT DISTINCT abc_id
FROM test
WHERE test_id IN (123)

it executed very fast as test_id is an indexed column.
But when I tried it like this
SELECT DISTINCT abc_id
FROM test
WHERE test_id IN ( SELECT unnest('{123}'::int[])

It took lot of time since due to unnest inside IN clause query was using a sequential scan on test_id column and taking a lot of time to run.
So the main culprit is the unnest inside IN clause.
Any substitute to use an array inside an in clause with indexed search?
Comment: No need for `unnest`. Try: `test_id = ANY ( test_id )`
Comment: Did you run `EXPLAIN` on that query?
Comment: How long does the query take when you run it manually outside of the function. What is the execution plan of the query? How many rows does the query aggregate (i.e. how many elements will the final array contain)
Comment: @a_horse_with_no_name it takes around 20 ms.
Comment: `_int4` is a custom type? is it array? what is `ARRAY` method?
Comment: @VaoTsun in postgresql its shorthand for int[]
Comment: @sumit cool - thank you. and the `ARRAY method`?
Comment: @unkmas can you please check question again. I have updated it !
Comment: @a_horse_with_no_name please check now !
Comment: @a_horse_with_no_name Thanks
 it worked ! its using indexed search with any
Answer: As suggested by a_horse_with_no_name instead of unnest inside IN clause using
test_id = ANY( test_id )
fixed the issue !
Comment: Accept this answer :)