Sampling from bivariate distribution with known density using MCMC

Question: I tried to simulate from a bivariate density $p(x,y)$ using Metropolis algorithms in R and had no luck. The density can be expressed as  $p(y|x)p(x)$, where $p(x)$ is Singh-Maddala distribution
$p(x)=\dfrac{aq x^{a-1}}{b^a (1 + (\frac{x}{b})^a)^{1+q}}$
with parameters $a$, $q$, $b$, and $p(y|x)$ is log-normal with log-mean as a fraction of $x$, and log-sd a constant. To test whether my sample is the one I want, I looked at the marginal density of $x$, which should be $p(x)$. I tried different Metropolis algorithms from R packages MCMCpack, mcmc and dream. I discarded burn-in, used thinning, used samples with size up to million, but the resulting marginal density was never the one I supplied.
Here is the final edition of my code I used:
logvrls <- function(x,el,sdlog,a,scl,q.arg) {
    if(x[2]>0) {
         dlnorm(x[1],meanlog=el*log(x[2]),sdlog=sdlog,log=TRUE)+
         dsinmad(x[2],a=a,scale=scl,q.arg=q.arg,log=TRUE)
    }
    else -Inf
}

a <- 1.35
q <- 3.3
scale <- 10/gamma(1 + 1/a)/gamma(q - 1/a)*  gamma(q)

Initvrls <- function(pars,nseq,meanlog,sdlog,a,scale,q) {
    cbind(rlnorm(nseq,meanlog,sdlog),rsinmad(nseq,a,scale,q))
}

library(dream)
aa <- dream(logvrls,
        func.type="logposterior.density",
        pars=list(c(0,Inf),c(0,Inf)),
        FUN.pars=list(el=0.2,sdlog=0.2,a=a,scl=scale,q.arg=q),
        INIT=Initvrls,
        INIT.pars=list(meanlog=1,sdlog=0.1,a=a,scale=scale,q=q),
        control=list(nseq=3,thin.t=10)
        )

I've settled on dream package, since it samples until the convergence. I've tested whether I have the correct results in three ways. Using KS statistic, comparing quantiles, and estimating the parameters of Singh-Maddala distribution with maximum likelihood from the resulting sample:
ks.test(as.numeric(aa$Seq[[2]][,2]),psinmad,a=a,scale=scale,q.arg=q)

lsinmad <- function(x,sample)
    sum(dsinmad(sample,a=x[1],scale=x[2],q.arg=x[3],log=TRUE))
 optim(c(2,20,2),lsinmad,method="BFGS",sample=aa$Seq[[1]][,2])

 qq <- eq(0.025,.975,by=0.025)
 tst <- cbind(qq,
              sapply(aa$Seq,function(l)round(quantile(l[,2],qq),3)),
              round(qsinmad(qq,a,scale,q),3))
 colnames(tst) <- c("Quantile","S1","S2","S3","True")

 library(ggplot2)
 qplot(x=Quantile,y=value,
       data=melt(data.frame(tst),id=1),
       colour=variable,group=variable,geom="line")

When I look at the results of these comparisons, KS statistic almost always rejects the null hypothesis that sample is from Singh-Maddala distribution with supplied parameters. Maximum likelihood estimated parameters sometimes comes close to its true values, but usually too far out of comfort zone, to accept that sampling procedure was succesfull. Ditto for the quantiles, empirical quantiles are not too far, but too far away.
My question is what I am doing wrong? My own hypotheses:

MCMC is not appropriate for this type of sampling
MCMC cannot converge, due to theoretical reasons (the distribution function does not satisfy required properties, whatever they are)
I do not use the Metropolis algorithm correctly
My distribution tests are not correct, since I do not have independent sample.
Comment: In the [Singh-Maddala distribution](http://en.wikipedia.org/wiki/Singh-Maddala_distribution) link, the pdf has two parameters - {c, k}, yet the R function ``dsinmad`` takes three parameters or am I missing something.
Comment: Sorry, the wikipedia link cites the wrong formula, it looked ok at first glance, when I was composing the question. I did not find a ready link, so I just put the formula in the question.
Answer: In fact, you should not do MCMC, since your problem is so much simpler. Try this algorithm:
Step 1: Generate a X from Log Normal
Step 2: Keeping this X fixed, generate a Y from the Singh Maddala.
Voilà! Sample Ready!!!
Comment: You would use Gibbs sampling when you know $p(y|x)$ and $p(x|y)$, but not if you know the marginal $p(x)$
Comment: No, i meant steps 1 and 2 in the order I wrote. After all, the distribution of y is specified conditional on X, so you must generate an X before Y. As for Gibbs sampling, that is a more complicated solution meant for more complicated problems. Yours, as you describe it, is pretty straighforward, IMHO.
Comment: I assume that you meant the steps reversed. But if this is so simple why do we need Gibbs sampling?
Answer: I think the order is correct, but the labels assigned to p(x) and p(y|x) were wrong. The original problem states p(y|x) is log-normal and p(x) is Singh-Maddala. So, it's

Generate an X from a Singh-Maddala, and
generate a Y from a log-normal having a mean which is a fraction of the generated X.


Given Joint PDF, find conditional probability

Question: 1) So I have this joint PDF:
$$
f(x,y)=
\begin{cases}
e^\frac{-x}{y}e^{-y} & \text{ for } x>0 , y>0\\
0 & \text{ otherwise}
\end{cases}
$$
and I found marginal density of y
$$
f_Y(y)=
\begin{cases}
ye^{-y} & \text{ for }y \leq 0\\
0 & \text{ for }y> 0\\
\end{cases}
$$
and now I have to find $P(2 < x < 3 | Y=y)$
$$
\int_2^3 f_{xy}(X|Y)/F_Y(y)  dx
$$
and find a value, I get like $e^{-3y}+e^{-2y}$, am I doing this question right?
2) Let (X,Y) have joint pdf
$$
f(x,y)=
\begin{cases}
\frac{12}{5}x(2-x-y) & \text{ for } 0<x<1, 0<y<1\\
0 & \text{ otherwise}
\end{cases}
$$
and I have to find $F_{x|y}(x|y)$ for $0 < y < 1$
but first I found marginal density of $Y$
$$
f_Y(y)=
\begin{cases}
\frac{12}{5}[\frac{2-y}{2}-\frac{1}{3}] & \text{ for } 0<y<1\\
0 & \text{ otherwise}\\
\end{cases}
$$
what do I have to do next? my professor told me I have to find marginal density in terms of $0 < x < 1$ but I don't know how to pursue that.
my professor did:
$$
f_Y(y)=
\begin{cases}
\frac{x(2-x-y)}{4-3y}x6 & \text{ for } 0<x<1\\
0 & \text{ otherwise} \\
\end{cases}
$$
but how did he find such that answer?
Answer: Your marginal density for $Y$ is correct except the ranges are the wrong way around. It should be:
$$f_Y(y) =
\begin{cases}
ye^{-y} & \text{ for }y \gt 0\\
0 & \text{ for }y\leq 0.\\
\end{cases}
$$
\begin{align}
P(2\lt X\lt 3\mid Y=y) &= \int_{x=2}^{3} f_{X|Y}(x|y)\;dx \\
&= \int_{x=2}^{3} \dfrac{f_{X|Y}(x|y)}{f_Y(y)} \;dx \\
&= \int_{x=2}^{3} \dfrac{e^{-x/y}e^{-y}}{ye^{-y}} \;dx \\
&= \int_{x=2}^{3} \dfrac{e^{-x/y}}{y} \;dx \\
&= \left[ -e^{-x/y}\right]_{x=2}^{3} \\
&= e^{-2/y}-e^{-3/y}.
\end{align}
2.
Your marginal density for $Y$ is correct but you could simplify it to
$$f_Y(y) = \dfrac{2}{5}\left(4-3y\right),\quad\text{for } 0\lt y\lt 1.$$
Then, for $0\lt x\lt 1,\;0\lt y\lt 1$,
$$f_{X|Y}(x|y) = \dfrac{f_{X,Y}(x,y)}{f_Y(y)} = \dfrac{\dfrac{12}{5}x\left(2-x-y\right)}{\dfrac{2}{5}\left(4-3y\right)} = \dfrac{6x\left(2-x-y\right)}{4-3y}.$$


confidence interval and success rate

Question: 1.000 people attempted a task and 30 succeeded. Given this information, we can say that the success rate was 3%.
But what if I want to estimate from this sample a confidence interval for the success rate in the population? For example, given that previous data, the success rate is between let's say 1% and 5% in 95% of cases.
How can I calculate that interval with a 95% certainty? is there any tool online?
Thank you,
Luca
Comment: Look for a binomial distribution calculator.
Comment: Or the formula for the standard error of a proportion.  E.g., the first equation at https://stats.stackexchange.com/questions/266442/standard-error-for-proportion-with-small-sample-size
Answer: If you have a binomial with $n=1000$ and $p=0.03$ the outcome of one sampling of this distribution is $k$. You're looking for the interval from $k-\Delta$ to $k+\Delta$ which contains 95% of the probability of this distribution. Defining $F(x) = P(X \leq x)$ then you need to solve the following equation for $\Delta$:
$$F(k+\Delta) - F(k-\Delta) = 0.95$$
Given that the expression for $F$ is awkward, I'm imagining this that to be done numerically.
Comment: On the contrary, the entire point to a CI is that you do *not* know the underlying distribution.  If you know $F$ then you know $p$ and there's nothing to estimate.
Comment: The problem isn't awkwardness: it's that you don't even know $F$!
Comment: What do you mean? If the PMF is binomial, the CDF is just a finite sum. Awkward, but totally known.
Comment: There's at least two ways to interpret the original question. One is "1000 people attempted task and 30 succeeded, what's the underlying distribution?". That's not how interpreted it. I interpreted it as: "we know we have a binomial with p=0.03, what's the interval of likely outcomes." If you look at the original LucaP question, he says "But what if I want to know what the success rate actually is?", which is a lot more ambiguous than rolando2's edited version.
Comment: In other words, if you take rolando2's edited version as ground truth, then I agree with you that this is an inference problem, the distribution isn't known, and therefore my response doesn't apply. If you take LucaP's original version as ground truth, I would say it's ambiguous, but I can also see how you can read it as being a question about inference.

confidence interval and success rate


Kendo UI for jquery drawer side not forwarding to href

Question:

    <title></title>
    <link rel="stylesheet" href="styles/kendo.common.min.css" />
    <link rel="stylesheet" href="styles/kendo.default.min.css" />
    <link rel="stylesheet" href="styles/kendo.default.mobile.min.css" />

Monday meeting

Regarding org chart changes

Meeting with Cliff

Global Marketing Meeting

Out tonight with collegues?

11/5 Monday

Martha Birthday

15/6 Sunday

Job interview for internal position

Build enterprise apps

Fw: Regarding Multiline textbox

Away next week

Fw: Your Costume is ready

Update completed

90% Discount!

90% Discount!

One time offer!

This block here ^^
I'm trying to forward the menu item onclick to a link
Why does this href not forward the page to google.com on click of the menu item?
Seems like this should be a simple fix, we are not using router. I also tried adding onclick="location.href='requisitions'"
It looks like your post is mostly code; please add some more details. Not sure how many more details I can add.
Answer: @NigelK is correct on both counts. Perhaps you didn't understand his explanation. I'll supplement the "add more code to your itemClick() event bit. With this solution, you won't need  inside the div Inbox.
On the event handler, you check if the Inbox was clicked by finding the CSS class and if it was open the URL. Like so:
itemClick: function (e) {
    if(!e.item.hasClass("k-drawer-separator")){
        e.sender.drawerContainer.find("#drawer-content > div").addClass("hidden");
        e.sender.drawerContainer.find("#drawer-content").find("#" + e.item.find(".k-item-text").attr("data-id")).removeClass("hidden");
        let $li = $(e.item[0]);
        let $inbox = $li.find(".k-i-inbox");
        if ($inbox.length > 0) {
            window.open("https://google.com", "_self");
        }
    }
},

Answer: It is because the link is rendered to the right of the Inbox icon. The element has no content though so you can't see it. Change it to something like this and you'll see what I mean:
Go to mailbox

To do what you want, add more code to your itemClick() event handler to determine if the Inbox item was clicked and open a new tab or redirect the page from there.
Comment: The content is rendered in
```
            $("#drawer").kendoDrawer({
                template: " \
```
its a side bar nav menu


How do I create radio button for options on a Drupal Commerce product?

Question: I just need some radio buttons for adding on components in a drupal commerce product. The best example is Apple's build a customized MAC or laptop where you add on specific elements to the product but start with a base. I also need to know how I can do this with ajax.
Answer: Sounds like you're after either the Commerce Product Option module:

This module adds customizable product options to the "add to cart" form. The approach is generic, so all field types / widget can be used. This module is created only for the commerce module. For other systems you need different systems / approaches.
The module provides a field called option set. You can assign to your commerce product. Each option set consists of a list of fields. If a product is connected to a option set the add to cart form will be extended by the fields of the option sets.

Or the Commerce Product Attribute module:

This module provides some enhancements to the Drupal Commerce attribute handling.
The improvement contains:
Attribute field for line itemEdit option for attributes

I'd advise installing both of those and seeing which fits your use case better. You'll have to handle the AJAX functionality yourself as I don't think it's built in to either.
Comment: any clues on how to create a module for the ajax... I'm brand new with it...


Won a set without dropping a point

Question: Has anyone ever won a set in a grand slam without dropping a single point?
Comment: Then you are asking about [Golden set](http://en.wikipedia.org/wiki/Golden_set). We already had [a discussion](http://sports.stackexchange.com/questions/1056/what-is-a-golden-set-and-what-is-the-difference-with-bagel-set) about this.
Comment: While it was a legitimate question, I found the answer in less than 15 seconds under the first result. Do your fair share of research *before* you ask the question, and also check other questions like @SportsFan mentioned. No need to have an attitude here.
Comment: @user1672694 you're asking a rather...low effort question and then insulting someone asking you to clarify. That's not the kind of behavior we expect around here. Sports Fan pointed you to a question with an answer to this already, this one is getting closed.
Comment: You have a point, results can be limited unless you have that *exact* term, then, poof: results. Just don't be insulting, eh? It's a sports QA meant to be fun, and expert driven -- not overly intense :-)
Comment: I think it could be happened many times. Be more specific.
Comment: @SportsFan you must be thinking games not points because it would be incredibly difficult to achieve. Hence why I am asking
Comment: @SportsFan then you should know the answer. That 'discussion' also happened to be a different question.
Comment: @waxeagle I ask a legitimate question and am treated with contempt. That's not the kind of behaviour I expect around here. No-one around here knows how to give a straight answer
Comment: @NicholasV. I did research but I got nothing. That's why I asked. If I had known what it was called it would have helped, but I didn't.
Comment: @NicholasV. there seems to be a problem with people here giving a straight 'A' from the QA. Hence my increasing frustrations
Answer: A golden set has been seen 5 times (1). But as to "in a grand slam", it looks like only one time it has happened in Grand Slam. Yaroslava Shvedova won a golden set in 2012 Wimbledon while playing against Sara Errani (2).

The Grand Slam itinerary consists of the 1. Australian Open in mid January, the 2. French Open in May/June, 3. Wimbledon in June/July, and the 4. US Open in August/September.

The above quote is from this Wiki article, and offers a lot more information on the history of Grand Slams in particular.

Won a set without dropping a point


How to find a möbius transformation mapping $B(p, r)$ to $B(0, 1)$?

Question: I have an open connected $\Omega$ of the complex plane and for a point $p \in \Omega$ and $r > 0$ we have a ball $B(p, r) \subset \Omega$. Is there a general möbius transformation to map this to B(0, 1)?
I know I can find a möbius transformation between two sets circles if I have three distinct points from each, but in this case I don't have any points to work with from $B(p, r)$. Or could I go with something like $p_1 = (a+r) + p$, $p_2 = (a-r) + p$, and $p_3 = (a+ir) + p$? If yes, how would I proceed?
I am stuck.
Thank you for your time,
Isak
Answer: You can do that even with a linear transformation: We have
$$
 z \in B(p, r) \iff |z-p| < r \iff | \frac {z-p}r  - 0 | < 1 \iff  \frac {z-p}r \in B(0, 1)
$$
so that $f(z) =  \frac {z-p}r$ maps $B(p, r)$ conformally onto $B(0, 1)$.
All Möbius transformations mapping $B(p, r)$ onto $B(0, 1)$ are given by $T \circ f$, where $T$ is an automorphism of the unit disk.


Do the methods in Apache's FileUtils peform synchronous (blocking) i/o?

Question: Do the methods in Apache's FileUtils perform synchronous (blocking) i/o?
I am making a call to FileUtils.copyDirectoryToDirectory. In my next line, I want to delete the directory that I copied.
Example:
<code>FileUtils.copyDirectoryToDirectory(source, destination);
FileUtils.deleteDirectory(source);
</code>
Just want to make sure this is "safe" and asynchronous (non-blocking) i/o isn't happening.
Thanks.
Answer: Two things:

FileUtils is not part of the standard JDK, it a class in the Apache Commons IO library.
The operations you mentioned do not use non-blocking IO.

So to answer your question, yes, your overall operation is safe.
Comment: Thanks so much. The reason I asked is because we are sometimes getting this error after the second line is excecuted: java.io.IOException: Unable to delete directory . Not sure why yet.
Comment: That should read: Unable to delete directory . Not a very helpful message. We're thinking it may be due to file(s) inside the directory being locked. The files/directories are stored on a Mac OS X system. After we upgraded the OS from 10.4 to 10.6, our users occasionally get this error when they use our system to try and delete. At this point, we're not sure how to debug this, since we're not able to duplicate the issue on our local systems.
Comment: You might try putting a slight delay between copying and deleting, in the case that file handles are still open at the OS level. Also, do you have a guarantee that the user will not have any files in the `source` directory opened in another program?
Comment: No guarantees, no. We actually don't care if the files are open - the content is static (served up by Apache). If we could somehow force delete, that would be great, but there doesn't seem to be a way using FileUtils.
Comment: You may want to try using `FileUtils.forceDelete`, it makes a more concerted effort to remove files and directories. It will also return you a useable error if it could not delete the directory for any reason.
Comment: Would `FileUtils.forceDelete(source)` still fail if there is a lock on a file inside the directory? I also considered using `FileUtils.move(source)`, then later deleting, but wasn't sure if moving would be handled the same way if a file lock existed.
Comment: Since file locks (and permissions) are an OS level semantic its impossible to force deletion of files with a pure Java solution. The same applied for moving the directory.
Comment: I'm going to try the `FileUtils.move(source)` into a backup directory and later delete...by later, I mean the next time the user attempts a delete, I will wipe out the backup directory on the next delete. Hopefully file locks will not be an issue with the move. Thanks for your help, Perception. I'll report back if we don't hear anything from our users in awhile :).


Mandar erro para login duplicado C#

Question: Estou implementando um sistema de login, mas não consigo mandar uma error message para quando tento criar um login duplicado.
Declarei o login da seguinte forma:
[Index("User_UserName_Index", IsUnique = true)]
public string Login { get; set; }

Mas não consigo retornar um erro falando que o login ja existe
Pensei em usar um try, mas não sei como eu poderia fazer isso.
Para a verificação do login eu fiz da seguinte forma
public class LoginController : Controller
{

    Contexto db = new Contexto();
    // GET: Login
    public ActionResult Index()
    {
        if (TempData["ViewData"] != null)
        {
            ViewData = (ViewDataDictionary)TempData["ViewData"];
        }
        return View();
    }

    public ActionResult Autenticar(string login, string senha)
    {

        try
        {
            var usuario = db.Usuarios.FirstOrDefault(p => p.Login == login && p.Senha == senha);

            if (usuario.Admim == true)
            {
                Session["admLogado"] = usuario;
                return RedirectToAction("Index", "Usuarios");
            }
            else
            {
                Session["usuarioLogado"] = usuario;
                return RedirectToAction("Login", "Home");
            }
        }
        catch(Exception ex)
        {
            ModelState.AddModelError(string.Empty, "Usuario ou senha invalidos");
            TempData["ViewData"] = ViewData;
            return RedirectToAction("Index");
        }

    }

Comment: Por que o Login estaria duplicado? Não valida se já existe um usuário com os mesmos dados no momento do cadastro?
Answer: Amigo com o try-catch você não vai conseguir validar que o login já existe.
Veja:

FirstOrDefault(IEnumerable)
Retorna o primeiro elemento de uma sequência ou um valor padrão se a
  sequência não contém elementos

Ou seja, ele não lança uma exeception caso o valor não esteja na coleção, sendo assim não irá cair no seu catch.
Para isso você pode utilizar a função First() no lugar de FirstOrDefault(), esta sim lança o exeception.
Veja esses links para saber mais sobre as funções FirstOrDefault e First
Answer: Sempre que buscar alguma informação de usuário e não encontrar, iria cair no catch ao tentar acessar a informação de "Admin", retornando um NullPointer.
Tente assim:

public ActionResult Autenticar(string login, string senha)
{
 var usuario = db.Usuarios.FirstOrDefault(p => p.Login == login && p.Senha == senha);

 if (usuario != null)
 {
  if (usuario.Admim)
  {
   Session["admLogado"] = usuario;
   return RedirectToAction("Index", "Usuarios");
  }
  else
  {
   Session["usuarioLogado"] = usuario;
   return RedirectToAction("Login", "Home");
  }
 }
 else
 {
  ModelState.AddModelError(string.Empty, "Usuario ou senha invalidos");
  TempData["ViewData"] = ViewData;
  return RedirectToAction("Index");
 }
}
Answer: public ActionResult Autenticar(string login, string senha)
{

    try
    {
        var usuarios = db.Usuarios.Where(p => p.Login == login && p.Senha == senha);

      if(usuarios.Count==0)
      {
         ModelState.AddModelError(string.Empty, "Usuario ou senha invalidos");
         TempData["ViewData"] = ViewData;
         return RedirectToAction("Index");
      }
     else if(usuarios.Count==1)
     {
        var usuario=usuarios.First();
        if (usuario.Admim == true)
        {
            Session["admLogado"] = usuario;
            return RedirectToAction("Index", "Usuarios");
        }
        else
        {
            Session["usuarioLogado"] = usuario;
            return RedirectToAction("Login", "Home");
        }
      }
    else
    {
      //Login Duplicado
    }

    }
    catch(Exception ex)
    {
        //Erro inesperado
    }

}

Acredito que desse jeito funcione, porém eu acho que deverias verificar o login duplicado ao fazer o cadastro.
A Exception não serve para esses tipos de tratamento como mensagem de: usuario ou senha invalidos, se por ventura ocorrer uma Exception qualquer a mensagem será sempre a mesma.

Mandar erro para login duplicado C#


How can I force PDB to quit when the signal is being caught?

Question: PDB's quit command works by raising an exception (Bdb.BdbQuit). If that exception gets caught, I cannot figure out a way to kill the program short of killing the entire shell. CTRL+C works by raising a KeyboardInterrupt exception, which can also be caught.
You can recreate this problem with this simple script.
foo = 0
while True:
    try:
        import pdb; pdb.set_trace()
        foo += 1
    except:
        pass

This script cannot be stopped from within PDB with the quit command or CTRL+C.
I'm aware this is bad programming and you should never use an except without an exception type. I ask because I ran into this issue while debugging and a third-party library trapped me in the loop.
Comment: This is an unsolvable problem, of course.  The whole reason we have SIGINT, SIGTERM, SIGQUIT and SIGKILL is that, every time someone invented a way to block a kill signal, someone else invented an unkillable signal.  If you are on Linux, you can try Ctrl-\ .  Otherwise, you'll need to Ctrl-Z and kill the job.
Answer: You can try killing the python process with os._exit.
import os
try:
    print("Exiting")
    os._exit(1)
except:
    print("Caught!")

Output:
Exiting

Answer: Why not re-raise the exception:
import bdb

try:
   something_i_might_want_to_debug_with_pdb()
except bdb.BdbQuit as exc:
   raise exc
except:
    print("Caught!")


HttpClient provide not truly async operations?

Question: I'm confused about async IO operations. In this article Stephen Cleary explains that we should not use Task.Run(() => SomeIoMethod()) because truly async operations should use

standard P/Invoke asynchronous I/O system in .NET

http://blog.stephencleary.com/2013/11/there-is-no-thread.html

However, avoid "fake asynchrony" in libraries. Fake asynchrony is when
  a component has an async-ready API, but it's implemented by just
  wrapping the synchronous API within a thread pool thread. That is
  counterproductive to scalability on ASP.NET. One prominent example of
  fake asynchrony is Newtonsoft JSON.NET, an otherwise excellent
  library. It's best to not call the (fake) asynchronous versions for
  serializing JSON; just call the synchronous versions instead. A
  trickier example of fake asynchrony is the BCL file streams. When a
  file stream is opened, it must be explicitly opened for asynchronous
  access; otherwise, it will use fake asynchrony, synchronously blocking
  a thread pool thread on the file reads and writes.

And he advises to use HttpClient but internaly it use Task.Factory.StartNew()

Does this mean that HttpClient provides not truly async operations?
Answer:
Does this mean that HttpClient provides not truly async operations?

Sort of. HttpClient is in an unusual position, since it's primary implementation uses HttpWebRequest, which is only partially asynchronous.
In particular, the DNS lookup is synchronous, and I think maybe the proxy resolution, too. After that, it's all asynchronous. So, for most scenarios, the DNS is fast (usually cached) and there isn't a proxy, so it acts asynchronously. Unfortunately, there are enough scenarios (particularly from within corporate networks) where the synchronous operations can cause significant lag.
So, when the team was writing HttpClient, they had three options:

Fix HttpWebRequest (and friends) allowing for fully-asynchronous operations. Unfortunately, this would have broken a fair amount of code. Due to the way inheritance is used as extension points in these objects, adding asynchronous methods would be backwards-incompatible.
Write their own HttpWebRequest equivalent. Unfortunately, this would take a lot of work and they'd lose all the interoperability with existing WebRequest-related code.
Queue requests to the thread pool to avoid the worst-case scenario (blocking synchronous code on the UI thread). Unfortunately, this has the side effects of degrading scalability on ASP.NET, being dependent on a free thread pool thread, and incurring the worst-case scenario cost even for best-case scenarios.

In an ideal world (i.e., when we have infinite developer and tester time), I would prefer (2), but I understand why they chose (3).
On a side note, the code you posted shows a dangerous use of StartNew, which has actually caused problems due to its use of TaskScheduler.Current. This has been fixed in .NET Core - not sure when the fix will roll back into .NET Framework proper.
Comment: Thank you for the information, Stephen. Each time I think that I understand `async/await` property, here you comes and I understand that I know nothing.
Answer: No, your assumptions are wrong.

StartNew isn't equal to the Run method.
This code is from HttpClientHandler, not the HttpClient, and you didn't examine the this.startRequest code from this class. The code you're inspecting is a prepare method, which starts a task in new thread pool, and inside call actual code to start an http request.
HTTP-connection is created not on the .NET level of abstraction, and I'm sure that inside startRequest you'LL find some P/Invoke method, which will do actual work for:

DNS lookup
Socket connection
Sending the request
waiting for the answer
etc.

As you can see, all above are logic which really should be called in async manner, because it is outside the .NET framework, and some operation can be very time-consuming. This is exactly logic that should be called asynchroniously, and during the waiting for it .NET thread is being released in ThreadPool to process other tasks.


login:1 GET http://localhost:3000/manifest.webmanifest 404 (Not Found)

Question: i created a todo list app using nodejs and wanted to add PWA (add to home screen feature) , on adding manifest.js file the browser console shows the following errors.

login:1 GET http://localhost:3000/manifest.webmanifest 404 (Not Found)
Failed to load resource: the server responded with a status of 404 (Not Found)

inside header.ejs
<link rel="manifest" crossorigin="use-credentials" href="/manifest.webmanifest" >

inside manifest.json
{
    "short_name":"Slate",
    "name":"SLATE (The Online Scheduler)",
    "description":"A simple To-Do list application.",
    "icons":[

      {
        "src":"images/logophone.png",
        "type":"image/png",
        "sizes":"187x91"
      },
      {
        "src":"images/logo.png",
        "type":"image/png",
        "sizes":"519x151"
      }
    ],
    "start_url":"home.ejs",
    "display":"standalone",
    "background_color":"#45ff61",
    "theme_color":"#45ff61",
    "scope":"/"
}

struggling hard to solve it ..kindly help
Comment: Why do you expect the server to provide the content of that JSON file when the client asks for that URL?
Comment: If m not wrong you are asking me to add app.get("/manifest ") ?? Is it?
Comment: You haven't provided the server side code you think should perform a response other than 404 for that URL so … maybe? Except the URL is `/manifest.webmanifest` not `/manifest `
Answer: It looks like the <link rel="manifest" tag is pointing to the wrong file. Your code points to manifest.webmanifest, but your actual filename is manifest.json.
Change the line in header.ejs to:
<link rel="manifest" crossorigin="use-credentials" href="/manifest.json">

Comment: thanku for replying...but I tried this this too ...gives the error as no resource found ...
Comment: If you try to make a request for the file on its own, does it work?
Comment: actually it don't  .. I added  app.get("/",function(req,res){ res.sendFile(__dirname+"c:\users\pallavi\....")});      again it gives me same error  line1,column1,syntax error   and manifest. Json not found
Answer: I had the same error, but fixed it with a few changes.
First of all I have change my file FROM manifest.webmanifest TO manifest.json and I have done the same in href, but it was not enough I still received an error (404).
Then I added my top root folder in and it worked.
My index.html was in task folder
My manifest.json file in task/favicon folder.
Link in index.html looked like this:
<link rel="manifest" href="/task/favicon/manifest.json" >

Or try to add ./ at the beggining, it means "on the same level", that worked for me too.
<link rel="manifest" href="./favicon/manifest.json" >

I hope it will help someone out.


OpenCV: Load multiple images

Question: I am updating some older OpenCV code that was written in (I guess) an OpenCV 1.1 manner (i.e. using IplImages).
What I want to accomplish right now is to simply load a series of images (passed as command line arguments) as Mats. This is part of a larger task. The first code sample below is the old code's image loading method. It loads 5 images from the command line and displays them in sequence, pausing for a key hit after each, then exits.
The second code sample is my updated version using Mat. It works fine so far, but is this the best way to do this? I've used an array of Mats. Should I use an array of pointers to Mats instead? And is there a way to do this such that the number of images is determined at run time from argc and does not need to be set ahead of time with IMAGE_NUM.
Basically, I'd like to be able to pass any number (within reason) of images as command line arguments, and have them loaded into some convenient array or other similar storage for later reference.
Thanks.
Old code:
#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
using namespace cv;

// the number of input images
#define IMAGE_NUM 5

int main(int argc, char **argv)
{
    uchar **imgdata;
    IplImage **img;
    int index = 0;
    char *img_file[IMAGE_NUM];

    cout << "Loading files" << endl;
    while(++index < argc)
        if (index <= IMAGE_NUM)
            img_file[index-1] = argv[index];

    // malloc memory for images
    img = (IplImage **)malloc(IMAGE_NUM * sizeof(IplImage *)); // Allocates memory to store just an IplImage pointer for each image loaded
    imgdata = (uchar **)malloc(IMAGE_NUM * sizeof(uchar *));

    // load images. Note: cvLoadImage actually allocates the memory for the images
    for (index = 0; index < IMAGE_NUM; index++) {
        img[index] = cvLoadImage(img_file[index], 1);
        if (!img[index]->imageData){
            cout << "Image data not loaded properly" << endl;
            return -1;
        }
        imgdata[index] = (uchar *)img[index]->imageData;
    }

    for (index = 0; index < IMAGE_NUM; index++){
        imshow("myWin", img[index]);
        waitKey(0);
    }

    cvDestroyWindow("myWin");
    cvReleaseImage(img);

    return 0;
}

New code:
#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <time.h>
using namespace std;
using namespace cv;

// the number of input images
#define IMAGE_NUM 5

int main(int argc, char **argv)
{
    Mat img[IMAGE_NUM];
    int index = 0;

    for (index = 0; index < IMAGE_NUM; index++) {
        img[index] = imread(argv[index+1]);
        if (!img[index].data){
            cout << "Image data not loaded properly" << endl;
            cin.get();
            return -1;
        }
    }

    for (index = 0; index < IMAGE_NUM; index++) {
        imshow("myWin", img[index]);
        waitKey(0);
    }
    cvDestroyWindow("myWin");
    return 0;
}

Answer: you can use a vector instead of an array:
for example
#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <time.h>
#include <vector>
using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
    vector<Mat> img;
    //Mat img[IMAGE_NUM];
    int index = 0;

    for (index = 0; index < IMAGE_NUM; index++) {

        //img[index] = imread(argv[index+1]);
        img.push_back(imread(argy[index+1]));

        if (!img[index].data){
            cout << "Image data not loaded properly" << endl;
            cin.get();
            return -1;
        }
    }

    vector<Mat>::iterator it;

    for (it = img.begin(); it != img.end() ; it++) {
        imshow("myWin", (*it));
        waitKey(0);
    }
    cvDestroyWindow("myWin");
    return 0;
}

Answer: It took me a while to get back around to this, but what I've ended up doing is as follows, which is probably functionally the same as Gootik's suggestion. This has worked well for me. Notice that for functions that take Mat& (i.e. a single cv::Mat), you can just de-ref the array of Mats and pass that, which is a notation I'm more comfortable with after doing a lot of image processing work in Matlab.
#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
    if (argc==1){
        cout << "No images to load!" << endl;
        cin.get();
        return 0;
    }

    int index = 0;
    int image_num = argc-1;

    Mat *img = new Mat[image_num]; // allocates table on heap instead of stack

    // Load the images from command line:
    for (index = 0; index < image_num; index++) {
        img[index] = imread(argv[index+1]);
        if (!img[index].data){
            cout << "Image data not loaded properly" << endl;
            cin.get();
            return -1;
        }
    }

    for (index = 0; index < image_num; index++) {
        imshow("myWin", img[index]);
        waitKey(0);
    }
    cvDestroyWindow("myWin");

    delete [] img; // notice the [] when deleting an array.
    return 0;
}

Comment: Quick update: at the time that Gootik replied, I was unfamiliar with the Standad Template Library's std::vector, and didn't see the value in his suggestion. Now that I'm more familiar with vectors, I like his suggestion more, although mine did work fine for my purposes.

OpenCV: Load multiple images


Extreme difficulty matching a piece to the metronome (piano)

Question: I'm having extreme difficulty following a rhythm using a metronome. Granted, I can't sightread yet so that may be the problem. Trying to play a piece I know very well by ear and matching it with the metronome beats (in any tempo) is incredibly hard for me, the offbeat notes keep escaping me, as if I was concentrating more on the actual metronome than the piece.
Am I using the metronome in an incorrect manner?
Comment: We might need more information, but one part of your question clicks with me: "...as if I was (sic) concentrating more on the actual metronome than the piece." I find playing to a metronome to be a tricky balance between ignoring the metronome and obsessing over it. By that, I mean that focusing too much on the metronome seems to make it *harder* for me to keep time with it. I have to kind of get the groove of the click and then only half-listen to the metronome. I also want to point out that playing to a metronome is harder than counting or playing with others.
Comment: The end of my first comment made me think of another comment: **Are you counting?** If you can count time while playing with or without a metronome, it should help. Try playing without the metronome and learning to count an entire piece and then going back to the metronome and counting with just the metronome and then playing and counting with the metronome - assuming you're not already doing that. **Always count out loud** when working on timing because the motion of your lips and mouth is important for synchronizing your body. Counting in your head isn't very effective.
Comment: No, I am not counting. That makes sense. I also can partly understand what you meant in your first comment, when I play the piece without focusing too much on the metronome, I find that I can much better synchronize with it (though not perfectly, and I end up rushing and slowing down some parts), if I try to measure every note with the metronome I fail tremendously.
Comment: Even if you don't count, can you feel where the first beat of the bar is when you play the piece?
Comment: I agree with Todd that counting is important.  One thing to try would be to sync the *fastest* note in the piece to your metronome beat, and hold and count beats for all other notes.  For example, if the shortest note in the piece is a 16th then play each 16th in time with the metronome tocks, hold and 8th for two tocks, a quarter for 4 tocks, etc.  This will be very slow but will help you get used to it.  The next step would be to set the metronome to match an 8th -- and you'll have to practice playing 16ths on and halfway between two tocks.
Answer: Playing with a metronome can be a challenge but can help tremendously with achieving proper timing when playing a musical piece as well as refining your ability to maintain the overall tempo throughout the song.
I find that the digital metronomes that allow you to choose from a large number of different beats where you can have the accent beat where it needs to be to establish the proper "groove" for the music - is much easier to adapt to than the old wind up metronomes that only tick tock like a grandfather clock.
What I personally find I must do on a complex timing piece when trying to learn it by using a metronome, is to slow the metronome down to a ridiculously slow tempo.  This give me time to think about the next note before the metronome clicks and makes it easy to land my finger on the note at the same time the metronome clicks.  Sometimes I will even double the tempo but play at half that speed so I have an in between phantom (not supposed to be there) reference click in between the real clicks.  This trick is especially helpful if some notes get played on an off beat.
It also helps to break out the song into small segments - even as small as two measures at a time and gradually add the others until I can play an entire verse.
After you are able to play it at the ridiculously slow tempo, start speeding it up gradually until you are able to play it accurately at the new faster tempo.  Repeat this process until you can play full tempo (or faster).
As others have said, counting aloud can be extremely helpful because it helps you connect to the beat in a manner that reflects where you are in each measure and it gives you another auditory cue.  And vocalizing aloud helps anchor everything in your brain (similar to the way thinking out loud sometimes helps you process your thoughts).  If it's a complex beat where some note occur on an off beat you might have to count "one and uh two and uh three ...." instead of just "one - two - three ....".
Playing with a metronome or click track comes easier to some musicians than others.  But with slow deliberate practice, you should be able to master it eventually. If a particular piece proves exceedingly difficult, don't be afraid to play it with a beat that is easier for you.  Consider this as "creating your own interpretation" of the piece.
Good luck and have fun with your music!
Answer: Play an easier piece.
Think of playing with a metronome as a skill to be learned. You'll be frustrated if you try to use a metronome and learn something else at the same time, so start with music that's so simple that it requires almost no conscious attention to play. If you have a lesson book, try the metronome with one of the early lessons. Playing the music will be very easy, but that's the point -- you want to be able to devote your attention to staying exactly in sync with the metronome's beat. It's the same idea as slowing down (also a good idea), but I think playing a simpler song makes it even easier to devote more attention to the beat.
If you don't have a lesson book, this alone would be a good reason to get one. (I use Accelerated Piano Adventures for the Older Beginner: Lesson Book 1, but any introductory book will have simple tunes that'll work.) I know you said you don't sight read, but neither do new piano students working on their first lessons. You'll be able to read this music just fine.
Answer: Since you said that you're having trouble with the off beats, you can set the metronome to count the off beats (instead of quarter notes) to make sure you are playing them correctly. For example, if you are playing a passage with 16th notes, then have the metronome click 16th notes so that each click matches up with a note you are playing.
If your piece switches subdivisions (for example between 16th notes and 16th note triplets) then you can either practice each part separately changing the metronome to the correct subdivision or you could look into a digital programmable metronome/sequencer that will let you program the subdivision changes and you can make sure you are making the change from one subdivision to the other correctly.
And as others have said, slow the metronome down so that you can play it comfortably and in time. Playing to a metronome is very difficult at first, but the more you practice it the better you will get at it.
Answer: Another thing to practice with a metronome is to clap your hands or tap your feet to the beat of the metronome while singing or speaking the rhythm of the music. If this is difficult try just practicing speaking the subdivisions of the beat, like eighth notes then sixteenth notes then triplets, etc. really focus on keeping your clapping with the metronome first and then focus on keeping the subdivisions even. For variation try leaving out the downbeat and just saying the subdivision. For example, for eighth notes you would clap and then say "uh" for each eighth note. You can also practice keeping time with recorded music.


Having trouble understanding hook_theme()

Question: I'm going through the Drupalize.me tutorials on module development, but I've come across something that has me a little stumped. In the module, called "spy_glass", we created a basic theme hook like so:
function spy_glass_theme() {
  return array(
   'spy_glass_item' => array(
       'arguments' => array('title' => NULL, 'data' => NULL),
       'template' => 'spy-glass-item',
    ),
  );
}

Which is all well and good. It eventually gets called by spy_glass_page() which uses the theme hook to create to loop through an $information array, which is formed elsewhere and consists of 2 keys: title, and data.
function spy_glass_page($node) {
    $information = _spy_glass_serious_spying_business($node);

    $content = array();
    foreach ($information as $item) {
        $content[] = array(
            '#type' => 'markup',
            '#markup' => theme('spy_glass_item', $item),
        );
    }
    return $content;
 }

All good. Everything works as expected later when its passed to a spy_glass_preprocess_spy_glass_item() function and printed to a template. But here's what I don't understand. What the heck is this line of code doing in spy_glass_theme():
 'arguments' => array('title' => NULL, 'data' => NULL),

I've removed it and it literally changed nothing at all. Everything still works perfectly. What is the purpose of this?
Answer: I think this is a typo on your part or Drupalize.me.  You should supply a variables array that defines the default values of the variables passed to your theme function if the calling function does not supply them.  It's not necessary, but seems to be good practice to NULL at least title and data.
From the api:

variables: (see above) Each array key is the name of the variable, and
  the value given is used as the default value if the function calling
  theme() does not supply it. Template implementations receive each
  array key as a variable in the template file (so they must be legal
  PHP variable names). Function implementations are passed the variables
  in a single $variables function argument.

function spy_glass_theme() {
  return array(
   'spy_glass_item' => array(
     'variables' => array('title' => NULL, 'data' => NULL),
     'template' => 'spy-glass-item',
    ),
  );
}

And then your theme function:
function theme_spy_glass_item($variables) {
  $title = $variables['title'];
  $data = $variables['data'];
}

Or, if you're using .tpl.php, you simply have access to the variables $title and $data.
Comment: @John 'arguments' is from Drupal 6, they may have re-used some old code without porting it properly
Comment: Hmm I didn't even think of a possible typo! Drupalize.me definitely wrote arguments and not variables, but variables definitely makes much more sense in this context. Thanks for the help.
Comment: @Clive thanks for that clarification man. Makes more sense, I'm guessing they copied/pasted from an older tutorial or the narrater had a brain-fart.


Appending to XML in ASP

Question: I'm trying to append to an XML in ASP Classic and the code I've found online doesn't seem to do the trick for me. Here is the code I'm using to test:
Set objxml2 = Server.CreateObject("MSXML2.DOMDocument.3.0")
objxml2.async = False
objxml2.load (server.mappath("organizations.xml"))

Set node = objxml2.createElement("organization")
objxml2.documentElement.AppendChild node

Set typeEl = objxml2.createElement("type")
typeEl.Text = "TheType"
node.AppendChild typeEl

Set objxml2 = Nothing
node = Nothing
Set typeEl = Nothing

The XML file organizations.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Data>
</Data>

And when I run the code I don't get any errors on the browser but the code doesn't show up in the file. The reason I'm appending rather than creating a new file is because this will be an ongoing list that will always be updating. Am I approaching this correctly?
Answer: You have to save the Document after appending and before setting objects to nothing :
objxml2.save(server.mappath("organizations.xml"))

Appending to XML in ASP


Questions on Proof of Intermediate Value Theorem

Question: I am trying to follow a proof of the Intermediate Value Theorem in Ross's real-analysis textbook, but do not understand several of the steps. I'm going to replicate the proof as much as I can (sometimes adding additional detail, so if I state something incorrectly without knowing, please tell me) and pause at these questions.

Theorem. If $f$ is a continuous real-valued function on an interval $I$, then $f$ has the intermediate value property on $I$: Whenever $a, b \in I$, $a < b$, and $y$ lies between $f(a)$ and $f(b)$ [i.e., $f(a) < y < f(b)$ or $f(b) < y < f(a)$], there exists at least one $x$ in $(a,b)$ such that $f(x) = y$.

--

Proof. Suppose that $f(a) < y < f(b)$. We define a set $S = \{x \in [a,b]: f(x) < y\}$. Since $f(a) < y$ by assumption, $a \in S$, so $S$ is nonempty. Furthermore, $[a,b] = \{x \in \mathbb{R} : a \leq x \leq b\}$ by definition, so it follows, it follows that $x \leq b, \forall x \in S$, so $S$ is bounded above by $b$. By the completeness axiom, $S$ has a supremum,  $x_0$. For any $n \in \mathbb{N}$, $x_0 - \frac{1}{n} < x_0$, so $x_0 - \frac{1}{n}$ is not an upper bound for the set of $S$, by the definition of the supremum.

The only thing I am confused on here is whether we are able to assert that $x < b$. We know $f(b) > y$, so $b$ is not in $S$, which suggests that we can make this greater assertion.

Because $x_0 - \frac{1}{n}$ is not an upper bound of $S$, there exists some element in $S$ greater than it. So, for every $n \in \mathbb{N}$, there exists some $s_n \in S$ such that $x_0 - \frac{1}{n} < s_n \leq x_0$.

Here, I am not completely sure on the strategy. He seems to take $s_n$ as not a particular element of $S$, but to treat $S$ as a sequence (we take the limit in the next step) and then take $s_n$ to be a subsequence where we create a value for each natural number. Is this the correct way to think about it?

Therefore, $\lim s_n = x_0$.

Does this follow from the squeeze lemma? Limits preserve $\leq$ inequalities, so $x_0 - \frac{1}{n} < s_n \leq x_0$ implies $\lim \left(x_0 - \frac{1}{n} \right) \leq \lim s_n \leq \lim \left(x_0 \right)$ and that $x_0 \leq \lim s_n \leq x_0$, so by the squeeze lemma $\lim s_n = x_n$.

That $f(s_n) < y$ for all $n \in \mathbb{N}$, and since $f$ is continuous, it follows that $f(x_0) = \lim f(s_n) \leq y$.

Assuming that I am correct in thinking that each $s_n$ is an element of the set that we are treating as a subsequence of $S$ (meaning that when we talk about the limit of $s_n$, we are are talking about the sequence, but when we say $f(s_n) < y$, we are talking an individual element), then I believe i understand this.

Define $t_n = \min\left(b, x_0 + \frac{1}{n}\right)$.

I am assuming, again, that this is a subsequence of $S$.

Observe that $x_0 \leq t_n \leq x_0 + \frac{1}{n}$.

I do not understand completely the second of these inequalities. If $t_n = b$, since $b$ is an upper bound of $S$ and $x_0$ is its supremum, it follows that $x_0 \leq t_n$, so I understand this. But $n$ is strictly positive, obviously, so $x_0 + \frac{1}{n} > x_0$. If $x_0 = t_n$, then $x_0 + \frac{1}{n} > t_n$. I suppose we have $t_n = x_0 + \frac{1}{n}$ is $x_0 + \frac{1}{n} < b$ and thus $t_n = x_0 + \frac{1}{n}$. Is this correct?

Therefore, $\lim t_n = x_0$.

I assume this is also a result of the squeeze lemma, correct?

Observe that each $t_n$ belongs to $[a,b]$ but not to $S$.

If $t_n = b$, then it surely doesn't belong to $S$ since $f(b) > y$. Is the additional argument then that since $x_0$ is the supremum of $S$, then if $x_0 + \frac{1}{n}$ were in $S$, then $x_0$ couldn't be its supremum (a contradiction), then $x_0 + \frac{1}{n}$ cannot be in the set? I think I understand why $t_n$ is not in $S$, but not why they are still in $[a,b]$. Surely $b$ is in $[a,b]$, and I suppose in the case where $x_0 + \frac{1}{n}$ is greater than $b$, then $t_n = b$, so we stay in $[a,b]$. Is that correct?

Therefore, $f(t_n) \geq y$ for all $n$. Therefore, since $f$ is continuous, $f(x_0) = \lim f(t_n) \geq y$. Since $f(x_0) \leq y$ and $f(x_0) \geq y$, it follows that $f(x_0) = y$. Therefore, $\exists x \in (a,b)$ such that $f(x) = y$.

I understand everything here.
I would greatly appreciate any insights on the above points or on something I missed. Thanks in advance.
Comment: Members of $S$ do not form a sequence, rather we choose some specific members of $S$ one by one and form a sequence $s_n$. Another point is that $t_n\leq x_0+1/n$ is obvious from definition of $t_n$.
Comment: Also the fact that $t_n\notin S$ is not clear at least not from the proof given. That it lies in $[a, b] $ is trivial. For example it may be the case than $t_n=x_0$ for all $n$ and it is not clear why $x_0$ not in $S$.
Comment: To be frank the proof is needlessly long and confusing. Just note that $x_0=\sup S$ exists and show that $f(x_0)=y$ by showing that both cases $f(x_0)y$ lead to contradiction.
Answer: Here are my comments on your arguments:

The only thing I am confused on here is whether we are able to assert
  that $x < b$. We know $f(b) > y$, so $b$ is not in $S$, which suggests
  that we can make this greater assertion.

If $b$ were in the set $S$, then it would be that $f(b)<y$ which contradicts our assumption. So yes, your conclusion is true.

Here, I am not completely sure on the strategy. He seems to take as
  not a particular element of , but to treat $S$ as a sequence (we take the
  limit in the next step) and then take  to be a subsequence where we
  create a value for each natural number. Is this the correct way to
  think about it?

No, we are trying to construct a sequence which converges to $x_0$. How do we do it? Since for each $n \in \mathbb{N}$, $x_0 - 1/n$ is no longer an upperbound for $S$, we can choose $s_n \in S$ such that $x_0 - 1/n  < s_n \le x_0$. Can you see that we constructed a sequence $(s_n)$? And btw, $S$ is a set, it isn't a sequence.

Does this follow from the squeeze lemma? Limits preserve $\leq$
  inequalities, so $x_0 - \frac{1}{n} < s_n \leq x_0$ implies $\lim
 \left(x_0 - \frac{1}{n} \right) \leq \lim s_n \leq \lim \left(x_0
 \right)$ and that $x_0 \leq \lim s_n \leq x_0$, so by the squeeze
  lemma $\lim s_n = x_n$.

You are right. But there's a typo at the end of previous paragraph:  $\lim s_n = x_0$.

Assuming that I am correct in thinking that each $s_n$ is an element
  of the set that we are treating as a subsequence of $S$ (meaning that
  when we talk about the limit of $s_n$, we are are talking about the
  sequence, but when we say $f(s_n) < y$, we are talking an individual
  element), then I believe i understand this.

Yes, $s_n \in S$ for all $n \in N$, so by definition of $S$, we have $f(s_n) < y$ for all $n \in \mathbb{N}$. To conclude that $f(x_0) = \lim f(s_n) \leq y$, we first notice that $f(s_n) < y$ for all $n \in \mathbb{N}$ then it must be that $\lim f(s_n) \le \lim y = y$ (since $f(s_n)$ is a sequence too!) and by the continuity of $f$, we have $f(s_n) = f(x_0)$

I am assuming, again, that this is a subsequence of $S$.

Yet again, $S$ is a set and $(t_n)$ is a sequence.

I do not understand completely the second of these inequalities. If
  $t_n = b$, since $b$ is an upper bound of $S$ and $x_0$ is its
  supremum, it follows that $x_0 \leq t_n$, so I understand this. But
  $n$ is strictly positive, obviously, so $x_0 + \frac{1}{n} > x_0$. If
  $x_0 = t_n$, then $x_0 + \frac{1}{n} > t_n$. I suppose we have $t_n =
 x_0 + \frac{1}{n}$ is $x_0 + \frac{1}{n} < b$ and thus $t_n = x_0 +
\frac{1}{n}$. Is this correct?

No, you are incorrect here. Let's first discuss about the first inequality. Since $x_0$ is the lub for $S$ and $b$ is an upperbound for $S$, we have that $x_0 \le b$. Since $x_0 \le b$ and $x_0 < x_0 + 1/n$ for all $n \in \mathbb{N}$, it must be that $x_0 \le \min \{ b, x_0 + 1/n \} = t_n$. The second inequality follows immediately from the definition of minimum, $t_n = \min \{ b, x_0 + 1/n \} \le x_0 + 1/n$.

If $t_n = b$, then it surely doesn't belong to $S$ since $f(b) > y$.
  Is the additional argument then that since $x_0$ is the supremum of
  $S$, then if $x_0 + \frac{1}{n}$ were in $S$, then $x_0$ couldn't be
  its supremum (a contradiction), then $x_0 + \frac{1}{n}$ cannot be in
  the set? I think I understand why $t_n$ is not in $S$, but not why
  they are still in $[a,b]$. Surely $b$ is in $[a,b]$, and I suppose in
  the case where $x_0 + \frac{1}{n}$ is greater than $b$, then $t_n =
 b$, so we stay in $[a,b]$. Is that correct?

Yes, you are correct if $x_0 + 1/n$ was in $S$, then $x_0$ would no longer be an upperbound for $S$. The sequence $(t_n)$ is in the set $[a,b]$ because $a \le x_0 \le t_n = \min \{ b, x_0 + 1/n \} \le b$.


How to check the number of CPUs and cores from host with virsh?

Question: How to check the number of CPUs and cores available in the host machine using virsh?
Answer: Use virsh nodeinfo. The output will be something like:
CPU model                    x86_64
CPU (s)                      8
CPU frequency                2895 Mhz
CPU socket(s)                2
Core(s) per socket           2
Threads per core:            2
Numa cell(s)                 1
Memory size:                 1046528 kb

Source: https://www.centos.org/docs/5/html/5.2/Virtualization/chap-Virtualization-Managing_guests_with_virsh.html
Alternatively, you can run the lscpu command.
Comment: Is there a simple command similar to `virsh nodeinfo` which shows the exact same layout, but currently allocated CPU and memory count?
Answer: to see the full set of what the host can offer, run virsh capabilities. The <cpus num...> field will hold the number, followed by the listing of cores/sockets/threads etc.


Folding Paper and Angles Question

Question: We are trying to solve this problem asking us to look for a certain angle involving a paper being folded.
Here's the problem:
On a square-shaped paper ABCD, folds the paper according to line EF to make an equal half.  Again, generates ab equal half ti each side of the paper, where A and B meet each other.  As shown in the image below, what is X?

X° in our solution results in 45° but in the answer key it's 75° and we can't seem to find the reason why. Thanks in advance!
Comment: how did you arrive at $45^{\circ}$? (You most likely carried out calculations by accidentally treating FD as CD, but you might have done something else.) You should explain your working. You will then receive more helpful answers.
Comment: Hi, thanks for your reply.  We began our solution by assuming AD = CD, and this makes angle GDC 60°. Let's say that midpoint between A and E is H.  Isn't angle EHG also 60° ?  Then, we also got angle ADH as 15° so angle AHD is 75°.  X° is then 180° - 60° - 75° = 45°.  Hopefully our solution is not too confusing
Answer: Here you can see, The answer is $75^°$.
Answer: Let's analyse your working:
"Hi, thanks for your reply. We began our solution by assuming AD = CD, and this makes angle GDC 60°. Let's say that midpoint between A and E is H. Isn't angle EHG also 60° ? Then, we also got angle ADH as 15° so angle AHD is 75°. X° is then 180° - 60° - 75° = 45°."

Because $AC$ = $CD$, after folding the corners, you obtain an equilateral triangle, so $\angle GDC$ = $60^\circ$ as you said.
We denote the midpoint of $AE$ by $H$. What is $\angle EHG$? First, from 1., we see that $\angle GDF = 30^\circ$. We also know that $\angle GDH = 90^\circ$, as the original paper is square. Using "angles on a line add to $180^\circ$", we have $\angle EGH = 180^\circ-90^\circ-30^\circ = 60^\circ$. The sum of angles in a triangle add to $180^\circ$, so $\angle EHG = 180^\circ - 60^\circ - 90^\circ = 30^\circ \neq 60^\circ$.
The rest of your argument is fine.

In summary, you need to calculate $\angle EHG$ more carefully. You'll see that everything works out now.

Folding Paper and Angles Question


How can I connect my Azure App Service web app to a private virtual network subnet (via a virtual network gateway) using PowerShell?

Question: I'm trying to provision an App Service web app via PowerShell and an ARM template and connect the web app to a virtual network via a virtual network gateway (both also provisioned via ARM).
The template is reasonably straightforward. I have a single network security group to cut the vnet off from the public Internet.
<code>{
            "comments": "NSG - DENY Internet",
            "type": "Microsoft.Network/networkSecurityGroups",
            "name": "[parameters('private_nsg')]",
            "apiVersion": "2016-03-30",
            "location": "[parameters('location')]",
            "properties": {
                "securityRules": [
                    {
                        "name": "allow-ssh",
                        "properties": {
                            "protocol": "TCP",
                            "sourcePortRange": "*",
                            "destinationPortRange": "22",
                            "sourceAddressPrefix": "Internet",
                            "destinationAddressPrefix": "*",
                            "access": "Allow",
                            "priority": 100,
                            "direction": "Inbound"
                        }
                    },
                    {
                        "name": "deny-internet",
                        "properties": {
                            "protocol": "TCP",
                            "sourcePortRange": "*",
                            "destinationPortRange": "*",
                            "sourceAddressPrefix": "Internet",
                            "destinationAddressPrefix": "*",
                            "access": "Deny",
                            "priority": 200,
                            "direction": "Inbound"
                        }
                    }
                ]
            },
            "resources": [],
            "dependsOn": []
        },
</code>
and then the virtual network itself with a subnet belonging to the above nsg and also a GateWay subnet.
<code>{
            "comments": "VNet 1",
            "type": "Microsoft.Network/virtualNetworks",
            "name": "[parameters('vnet1')]",
            "apiVersion": "2016-03-30",
            "location": "[parameters('location')]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "10.0.0.0/16"
                    ]
                },
                "subnets": [
                    {
                        "name": "private1",
                        "properties": {
                            "addressPrefix": "10.0.1.0/24",
                            "networkSecurityGroup": {
                                "id": "[resourceId('Microsoft.Network/networkSecurityGroups', parameters('private_nsg'))]"
                            }
                        }
                    },
                    {
                        "name": "GatewaySubnet",
                        "properties": {
                            "addressPrefix": "10.0.100.0/24"
                        }
                    }
                ]
            },
            "resources": [],
            "dependsOn": [
                "[resourceId('Microsoft.Network/networkSecurityGroups', parameters('private_nsg'))]"
            ]
        },
</code>
I then create an IP address for a virtual network gateway
<code>{
            "comments": "Gateway 1 IP",
            "type": "Microsoft.Network/publicIPAddresses",
            "name": "[parameters('gateway1_ip')]",
            "apiVersion": "2016-03-30",
            "location": "[parameters('location')]",
            "properties": {
                "publicIPAllocationMethod": "Dynamic",
                "idleTimeoutInMinutes": 4
            },
            "resources": [],
            "dependsOn": []
        },
</code>
And the gateway itself
<code>{
            "comments": "VNet1 Gateway",
            "apiVersion": "2015-05-01-preview",
            "type": "Microsoft.Network/virtualNetworkGateways",
            "name": "[parameters('gateway1')]",
            "location": "[parameters('location')]",
            "properties": {
                "ipConfigurations": [
                    {
                        "properties": {
                            "privateIPAllocationMethod": "Dynamic",
                            "subnet": {
                                "id": ""[concat(variables('vnet1_ref'),'/subnets/','GatewaySubnet')]""
                            },
                            "publicIPAddress": {
                                "id": "[resourceId('Microsoft.Network/publicIPAddresses',parameters('gateway1_ip'))]"
                            }
                        },
                        "name": "vnetGatewayConfig"
                    }
                ],
                "gatewayType": "Vpn",
                "vpnType": "RouteBased",
                "vpnClientConfiguration": {
                    "vpnClientAddressPool": {
                        "addressPrefixes": [ "192.168.2.0/24"]
                    }
                },
                "enableBgp": false
            },
            "dependsOn": [
                "[concat('Microsoft.Network/publicIPAddresses/', parameters('gateway1_ip'))]",
                "[concat('Microsoft.Network/virtualNetworks/', parameters('vnet1'))]"
            ]
        },
</code>
I also create a Linux VM and put it inside the private subnet and then create an App Service web app (and Plan) that knows how to perform a GET request against the Linux VM's private (10...*) IP.
Once the template deploys, I run some PowerShell based on this other Stack Overflow answer.
<code>$app1 = Get-AzureRmWebApp -ResourceGroupName $ResourceGroupName -Name $parameters["appsvc1_name"]
$app1Config = Get-AzureRmResource -ResourceName "$($app1.Name)/web" -ResourceType "Microsoft.Web/sites/config" -ResourceGroupName $resourceGroupName -ApiVersion 2015-08-01
$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $resourceGroupName -Name "$resourceGroupName-Net"
$gateway = (Get-AzureRmVirtualNetworkGateway -ResourceGroupName $ResourceGroupName)[0]
$networkProperties = @{ "vnetResourceId" = $vnet.Id }
$networkConnection = New-AzureRmResource -ResourceGroupName $resourceGroupName -Location $resourceGroupLocation -Properties $networkProperties -ResourceName "$($app1.Name)/$($vnet.Name)" -ResourceType "Microsoft.Web/sites/virtualNetworkConnections" -ApiVersion 2015-08-01 -Force
$null = Add-AzureRmVpnClientRootCertificate -ResourceGroupName $resourceGroupName -VpnClientRootCertificateName "AppServiceCertificate.cer" -PublicCertData $networkConnection.Properties.CertBlob -VirtualNetworkGatewayName $gateway.Name
</code>
At this point, if I look at my app in the portal, it looks like it's connected to the gateway (the portal says "Connected" in green and the "Certificate Status" is "Certificates are synced"). However, my app can't connect to the private VM.
If I remove the network connection in the portal and then reconnect it in the portal, my app can connect to the VM. This leads me to believe that the gateway is configured correctly and it's just the connection that's gone wrong.
The only thing I'm missing from the previous StackOverflow answer is:
<code>$vpnPackageUri = Get-AzureRmVpnClientPackage -ResourceGroupName $resourceGroupName -VirtualNetworkGatewayName $gateway.Name -ProcessorArchitecture Amd64
$vpnPackageUri = $vpnPackageUri.Replace('"', "")
$vpnPackageProperties = @{ "vnetName" = $vnet.Name; "vpnPackageUri" = $vpnPackageUri }
$networkConnectionVPN = New-AzureRmResource -ResourceGroupName $resourceGroupName -Location $resourceGroupLocation -ResourceName "$($app1.Name)/$($vnet.Name)/primary" -ResourceType "Microsoft.Web/sites/virtualNetworkConnections/gateways" -ApiVersion 2015-08-01 -Force
</code>
Unfortunately, the last command errors out with a generic error message:
<code>New-AzureRmResource : {"Message":"An error has occurred."}
At line:1 char:25
+ $networkConnectionVPN = New-AzureRmResource -ResourceGroupName $resourceGroupNam ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzureRmResource], ErrorResponseMessageException
    + FullyQualifiedErrorId : InternalServerError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceCmdlet
</code>
So I can't tell what's actually gone wrong. Running this command doesn't allow my app to connect either; I have to manually remove the connection and re-add it in the portal.
What am I missing?
Comment: try running the cmdlet with -debug or -verbose to see detailed exception
Comment: If I remove the network connection in the portal and then reconnect it in the portal, my app can connect to the VM. - this leads me to believe you are missing something in your powershell.
Comment: Take a look at [this question](http://stackoverflow.com/questions/38257379/how-to-create-a-new-resource-in-azure-arm-templates/38258829#38258829). It might help.
Comment: @itaysk thanks for the suggestion; unfortunately, all it shows is that the command gets back a 500 Internal Server Error with a body of { "message": "An error has occurred." }
Answer: I've done something very similar (except the VM was on Windows). I was following the MSDN article here and ended up in a similar situation. The portal said it the App was connected to the VPN, but there was no connection between the App and the VM. Connecting the App manually from the Portal fixed the problem.
You need to re-sync the network (from the App Service Plan / Networking tile).
There is an open issue on this here. They appear to not want to fix this, they are awaiting new features regarding the App Service - VPN integration.


Problemas atualização de dados com entity Framework com dados relacionais

Question: Tenho uma tabela Consulta que tem duas chaves estrangeiras (Cliente e Procedimento).
para adicionar uma nova consulta eu consigo normalmente, porem na hora de editar ele não muda meu Cliente ou Procedimento os outros campos modifica normalmente.
public static void EditarConsulta(Consulta entrada)
    {
        try
        {
            using (ConsultorioContext ctx = new ConsultorioContext())
            {
                ctx.Entry(entrada).State = EntityState.Modified;
                ctx.SaveChanges();
            }
        }
        catch (Exception)
        {

        }
    }

outras formas que já tentei:
ctx.ChangeTracker.Entries<Consulta>().First(x => x.Entity == entrada).State = EntityState.Modified;

ctx.Entry<Consulta>(entrada).State = System.Data.Entity.EntityState.Modified;

Answer: Lucas, você está iniciando o context dentro do método, nesse caso o Entity não reconhece a entidade original.
Tente buscar a entidade original e setar o valores em cima dela usando o SetValue. Algo como:
var entidadeExistente = ctx.Consulta.Find(entidadeAlterada.Id);
ctb.Entry(entidadeExistente).CurrentValues.SetValues(entidadeAlterada);

Abraços


Split array in n arrays with certain length

Question: I have this:
<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578> <:rrrr:409342761431728139> <:aaa:409342761439854593> <:fff:409342761503031296> <:woah:409342761532391424> <:swon:409342761549037568> <:owoah:409342761595043850> <:sss:409342761662414848>

Convert it to this array:
using .split(' ')
[ '<:cmd:409342761179938838>',
  '<:nobot:409342761246916610>',
  '<:haha:409342761272344578>',
  '<:rrrr:409342761431728139>',
  '<:aaa:409342761439854593>',
  '<:fff:409342761503031296>',
  '<:woah:409342761532391424>',
  '<:swon:409342761549037568>',
  '<:owoah:409342761595043850>',
  '<:sss:409342761662414848>' ]

But i don't know how to split it several arrays so each will have 100 symbols in it
like this:
[ '<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578>' ],
[ '<:rrrr:409342761431728139> <:aaa:409342761439854593> <:fff:409342761503031296>' ],
[ '<:woah:409342761532391424> <:swon:409342761549037568> <:owoah:409342761595043850>' ],
[ '<:sss:409342761662414848>']

p.s. i tried use .match(/.{1,100}/g);
but i got this
[ '<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578> <:rrrr:409342761431 '], ['728139>... etc ']

and i don't want to split <:rrrr:409342761431728139>
Comment: What do you want here?  Do you want _exactly_ 100 characters?  Well, that's probably not possible.  Do you want less/more than 100 characters, but as close as you can get?  Something else?
Comment: @TimBiegeleisen yes i want to get as close as i can to 100
Comment: Start with your original `.split()` array. Loop through the array, concatating elements to the strings in the result array. Whenever the length gets above 100 start a new string.
Comment: Do you really need each string to be in its own nested array? Or just one array of these strings.
Comment: @Barmar one array splitted with ',' will work as well
Answer: Split the string on spaces. Then concatenate each string to a result until it would be over 100 characters long, then you push it onto the result array.

var str = '<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578> <:rrrr:409342761431728139> <:aaa:409342761439854593> <:fff:409342761503031296> <:woah:409342761532391424> <:swon:409342761549037568> <:owoah:409342761595043850> <:sss:409342761662414848>';
var array = str.split(' ');
var result = [];
var curstr = '';
array.forEach(s => {
  var newstr = curstr + (curstr != '' ? ' ' : '') + s;
  if (newstr.length > 100) {
    result.push(curstr);
    curstr = s;
  } else {
    curstr = newstr;
  }
});
if (curstr != '') { // Get the last string, which wouldn't have been pushed in the loop
  result.push(curstr);
}
console.log(result);
Comment: I think instead of minimum 100 chars, OP is asking for nearest char closest to 100 but not exceeding 100. That's why the first element in the array is 80
Comment: @D.Lene My code was making strings >100 characters long. I changed it to push just before it gets to 100, so it produces the same result you wanted.
Comment: @Isaac I realized that and changed it.
Answer: Besides the first suggested approach, there is a functional way, using the reduce function:
const arr = str.split(' ')
  .reduce((acc, item) => {

    const idx = acc.length ? acc.length - 1 : 0

    const tempStr = `${acc[idx] || ''} ${item}`;

    return tempStr.length <= 100
      ? Object.assign(acc, { [idx]: tempStr })
      : [...acc, item];

  }, []);

Comment: Good alternative. Thank you!
Answer: @D.Lene, according to the output provided in the question. I have written the below code which gives youur output.

Note: Please do not forget to look at code executed on Node REPL after the below code. It will give you a clean understanding of each and every line.
And leave comment if the below code does not satify your need by  provide more outputs (that you actuially need for some specific inputs) as I am not counting for 100 characters as your provided output array's elements are not exactly of length 100 (it's 80, 78, 81, ...) which is clear in Node REPL testing.
If you are still looking for 100 characters then based on your provided data input(s) & output(s), I will need to change the code.

var data = "<:cmd:409342761179938838> " +
          "<:nobot:409342761246916610> " +
          "<:haha:409342761272344578> " +
          "<:rrrr:409342761431728139> " +
          "<:aaa:409342761439854593> " +
          "<:fff:409342761503031296> " +
          "<:woah:409342761532391424> " +
          "<:swon:409342761549037568> " +
          "<:owoah:409342761595043850> "+
          "<:sss:409342761662414848>"

var arr = data.split(' ')
console.log(arr)
/*
  [ '<:cmd:409342761179938838>',
    '<:nobot:409342761246916610>',
    '<:haha:409342761272344578>',
    '<:rrrr:409342761431728139>',
    '<:aaa:409342761439854593>',
    '<:fff:409342761503031296>',
    '<:woah:409342761532391424>',
    '<:swon:409342761549037568>',
    '<:owoah:409342761595043850>',
    '<:sss:409342761662414848>' ]
*/

var mainArr = []

while(arr.length) {
      mainArr.push(arr.splice(0, 3).join(' '))
}

// Pretty printing array
// (This is the o/p which is specified in the question, here I forgot everything about 100 characters)
console.log(JSON.stringify(mainArr, null, 4))
/*
  [
      "<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578>",
      "<:rrrr:409342761431728139> <:aaa:409342761439854593> <:fff:409342761503031296>",
      "<:woah:409342761532391424> <:swon:409342761549037568> <:owoah:409342761595043850>",
      "<:sss:409342761662414848>"
  ]
*/

Finally look at the below code executed on Node REPL to understand few of the above basic statements.
H:\RishikeshAgrawani\Projects\Stk>node
>
> var data = "<:cmd:409342761179938838> " +
...           "<:nobot:409342761246916610> " +
...           "<:haha:409342761272344578> " +
...           "<:rrrr:409342761431728139> " +
...           "<:aaa:409342761439854593> " +
...           "<:fff:409342761503031296> " +
...           "<:woah:409342761532391424> " +
...           "<:swon:409342761549037568> " +
...           "<:owoah:409342761595043850> "+
...           "<:sss:409342761662414848>"
undefined
>
> arr
[ '<:cmd:409342761179938838>',
  '<:nobot:409342761246916610>',
  '<:haha:409342761272344578>',
  '<:rrrr:409342761431728139>',
  '<:aaa:409342761439854593>',
  '<:fff:409342761503031296>',
  '<:woah:409342761532391424>',
  '<:swon:409342761549037568>',
  '<:owoah:409342761595043850>',
  '<:sss:409342761662414848>' ]
>
> arr[0]
'<:cmd:409342761179938838>'
> arr[0].length
25
>
> arr[1].length
27
>
> arr[2].length
26
>
> arr[3].length
26
> arr[4].length
25
> arr[5].length
25
> arr[6].length
26
>
> var mainArr = []
undefined
>
> while(arr.length) {
...       mainArr.push(arr.splice(0, 3).join(' '))
... }
4
>
> mainArr
[ '<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578>',
  '<:rrrr:409342761431728139> <:aaa:409342761439854593> <:fff:409342761503031296>',
  '<:woah:409342761532391424> <:swon:409342761549037568> <:owoah:409342761595043850>',
  '<:sss:409342761662414848>' ]
>
> mainArr[0]
'<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578>'
>
> mainArr[1]
'<:rrrr:409342761431728139> <:aaa:409342761439854593> <:fff:409342761503031296>'
>
> mainArr[2]
'<:woah:409342761532391424> <:swon:409342761549037568> <:owoah:409342761595043850>'
>
> mainArr[0].length
80
> mainArr[1].length
78
> mainArr[2].length
81
>

Thanks.


Avoid non-exhaustive pattern match when using para recursion-scheme

Question: Below is some code which I wrote as an exercise for using para from recursion-schemes (I know that this reduced example could also be solved using just cata, but let's ignore this for this question).
While doing this, I noticed that I have to do a non-exhaustive pattern match when I'm using para if I want to access the expression tree of any of the arguments to the Depth constructor.
I found an alternative implementation of gcata' and para' that don't have this issue, and also don't require a Comonad, but just a Functor instance on w. This makes me wonder: Why wasn't this version used in the implementation of recursion-schemes? Is there anything wrong with it, or are there just better ways to achieve what I'm looking for?
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RankNTypes #-}
module Test where

import Data.Functor
import Data.Functor.Foldable

data ExprF a = Depth a a -- ^ Counts the maximum depth of the tree
             | Unit
  deriving Functor
type Expr = Fix ExprF

unit :: Expr
unit = Fix Unit

depth :: Expr -> Expr -> Expr
depth a b = Fix $ Depth a b

evalDepth :: Expr -> Int
evalDepth = cata phi where
  phi Unit = 0
  phi (Depth a b) = max a b + 1

eval :: Expr -> Int
eval = para phi where
  phi Unit = 0
  phi (Depth (Fix (Depth a b), _) _) = evalDepth a + evalDepth b
--            ^^^^^^^^^^^^^^^
-- at this point is the whole *current* expression tree, not just
-- the subtree that was given as first argument to `depth`

--------------------------------------------------------------------------------
-- Is this possible without definining gcata' / para' myself with the current API?

eval' :: Expr -> Int
eval' = para' phi where
  phi Unit = 0
  phi (Depth (a,_) (b,_)) = evalDepth a + evalDepth b
--            ^     ^
-- a and b are just the first and second argument to `depth`. No need
-- to do a pattern match which might go wrong.

gcata' :: forall t w a. (Foldable t, Functor w)
       => (forall b. Base t (w b) -> w (Base t b))
       -> (Base t (w a) -> a)
       -> t -> a
gcata' k g = fst . c where
  c :: t -> (a, w a)
  c y = (g x, g x <$ k x) where
    x :: Base t (w a)
    x = fmap (snd . c) . project $ y

para' :: (Foldable t, Unfoldable t) => (Base t (t,a) -> a) -> t -> a
para' = gcata' distPara

And here is an example how to use it:
eval' (depth (depth unit unit) (depth (depth unit unit) unit)
-- 3
eval (depth (depth unit unit) (depth (depth unit unit) unit))
-- 3

As you can see, both functions do the same, calculating the maximum depth of the tree (without counting the most outer depth call itself)
Answer: para is a very very special case.
Notably it uses (,) (Mu f) as its choice of Comonad.
This Comonad has a lot more structure than most.
Notably, it is left adjoint as (,) e -| (->) e.
Why does this matter? Well (,) e preserves colimits, and thus it only has one a inside of it.
So you can get away with g x <$ k x -- because you're only replacing one thing!
For any more interesting Comonad your gcata' should fail.
When you have more than one a to replace in w a, you're throwing away information, so this won't be a universal recursion scheme.


Understand a matrix expression

Question: Here is something I read and I didn't understand :

Let $n \in \mathbb{N}^{\ast}$ and $\Gamma$ be a $n \times n$ positive definite matrix. Let $X \in \mathbb{R}^{n}$, $X \neq 0$ and $\big(X, \varepsilon_{2}, \ldots, \varepsilon_{n}\big)$ an  orthogonal basis of $\mathbb{R}^{n}$ such that, in this basis, $\Gamma$ writes :
$$\begin{pmatrix} x_{0} & 0 & \ldots & 0 \\ 0 & & & \\ \vdots & & S & \\ 0 & & & & \end{pmatrix}$$
Then, the matrix $\Gamma$ is :
$$ \Gamma = x_{0} \frac{X X^\top}{\Vert X \Vert^{2}} + \Big(\mathrm{I}_{n} - \frac{X X^\top}{\Vert X \Vert^{2}} \Big) \begin{pmatrix} \ast & \ast & \ldots & \ast \\ \ast & & & \\ \vdots & & S & \\ \ast & & & & \end{pmatrix} \Big( \mathrm{I}_{n} - \frac{X X^\top}{\Vert X \Vert^{2}} \Big)^\top \tag{$\clubsuit$}$$
  where $\ast$ represents some real coefficients.

I don't understand why the expression $(\clubsuit)$ is true. Here is what I tried :
Let $B_{\mathrm{can}}$ be the canonical basis of $\mathbb{R}^{n}$ and $B=\big(X,\varepsilon_{2},\ldots,\varepsilon_{n}\big)$. Let $P$ be the matrix of change of basis from $B_{\mathrm{can}}$ to $B$. Then :
$$ \Gamma = P \begin{pmatrix} x_{0} & 0 & \ldots & 0 \\ 0 & & & \\ \vdots & & S & \\ 0 & & & & \end{pmatrix} P^{-1} $$
and :
$$ \frac{X X^\top}{\Vert X \Vert^{2}} = P \begin{pmatrix} 1 & 0 & \ldots & 0 \\ 0 & & & \\ \vdots & & \mathbf{0} & \\ 0 & & & & \end{pmatrix} P^{-1} \quad \mathrm{and} \quad \Big(\mathrm{I}_{n} - \frac{X X^\top}{\Vert X \Vert^{2}} \Big) = P \begin{pmatrix} 0 & 0 & \ldots & 0 \\ 0 & & & \\ \vdots & & \mathrm{I}_{n-1} & \\ 0 & & & & \end{pmatrix} P^{-1} $$
Therefore, $\Big(\mathrm{I}_{n} - \frac{X X^\top}{\Vert X \Vert^{2}} \Big) \begin{pmatrix} \ast & \ast & \ldots & \ast \\ \ast & & & \\ \vdots & & S & \\ \ast & & & & \end{pmatrix} \Big( \mathrm{I}_{n} - \frac{X X^\top}{\Vert X \Vert^{2}} \Big)^\top$ is equal to :
$$
\begin{eqnarray*}
 &  & P \begin{pmatrix} 0 & 0 & \ldots & 0 \\ 0 & & & \\ \vdots & & \mathrm{I}_{n-1} & \\ 0 & & & & \end{pmatrix} P^{-1} \begin{pmatrix} \ast & \ast & \ldots & \ast \\ \ast & & & \\ \vdots & & S & \\ \ast & & & & \end{pmatrix} P \begin{pmatrix} 0 & 0 & \ldots & 0 \\ 0 & & & \\ \vdots & & \mathrm{I}_{n-1} & \\ 0 & & & & \end{pmatrix} P^{-1} \\[3mm]
 & = & P \begin{pmatrix} 0 & 0 & \ldots & 0 \\ 0 & & & \\ \vdots & & \mathrm{I}_{n-1} & \\ 0 & & & & \end{pmatrix} \begin{pmatrix} \diamond & \diamond & \ldots & \diamond \\ \diamond & & & \\ \vdots & & \tilde{S} & \\ \diamond & & & & \end{pmatrix} \begin{pmatrix} 0 & 0 & \ldots & 0 \\ 0 & & & \\ \vdots & & \mathrm{I}_{n-1} & \\ 0 & & & & \end{pmatrix} P^{-1} \\[3mm]
& = & P \begin{pmatrix} 0 & 0 & \ldots & 0 \\ 0 & & & \\ \vdots & & \tilde{S} & \\ 0 & & & & \end{pmatrix} P^{-1}
\end{eqnarray*} $$
So, in the end I get :
$$ \Gamma = P \begin{pmatrix} x_{0} & 0 & \ldots & 0 \\ 0 & & & \\ \vdots & & \tilde{S} & \\ 0 & & & & \end{pmatrix} P^{-1} $$
which is not the expected expression. Where am I wrong ?
Answer: Considering that $(X,\epsilon_2,\dots,\epsilon_n)$ forms an orthogonal basis, we have direct sum decomposition of $V=\mathbb R^n$. We write
$$V=\mathrm{span}\{X\}\oplus\mathrm{span}\{\epsilon_2,\dots,\epsilon_n\}=\mathrm{span}\{X\}\oplus\mathrm{span}\{X\}^\perp$$
It's not hard to write down the projection operator $\mathcal P_X=\frac{XX^T}{||X||^2}$ on subspace $\mathrm{span}\{X\}$ and the rejection operator $\mathcal R_X=I-\mathcal P_X=I-\frac{XX^T}{||X||^2}$. Now the decomposition becomes
$$V=\mathrm{Im}\mathcal P_X\oplus\mathrm{Im}\mathcal R_X$$
Next your question is readily followed by direct computations.
$$\begin{align}&x_0\frac{XX^T}{||X||^2}=x_0\mathcal P_XI_n\\
=&x_0\begin{pmatrix} 1 & 0 \ldots & 0 \\ 0 & & \\ \vdots & \mathbf O_{n-1} \\ 0 & \end{pmatrix}=\begin{pmatrix} x_{0} & 0 & \ldots & 0 \\ 0 & & & \\ \vdots & & \mathbf O_{n-1} \\ 0 & & & \end{pmatrix}\end{align}\\$$
$$\begin{align}&\Big(I_n-\frac{X X^T}{\Vert X \Vert^{2}} \Big) \begin{pmatrix} \ast & \ast & \ldots & \ast \\ \ast & & \\ \vdots & & S \\ \ast & & & \end{pmatrix} \Big(I_n-\frac{X X^\top}{\Vert X \Vert^{2}} \Big)^T\\
=&\mathcal R_X\begin{pmatrix} \ast & \ast & \ldots & \ast \\ \ast & & \\ \vdots & & S \\ \ast & & & \end{pmatrix}\mathcal R_X^T
=\begin{pmatrix} 0 & 0 & \ldots & 0 \\ \ast & & \\ \vdots & & S  \\ \ast & & & \end{pmatrix}\mathcal R_X^T\\
=&\begin{pmatrix} 0 & 0 & \ldots & 0 \\ 0 & &  \\ \vdots & & S \\ 0 & & &  \end{pmatrix}\end{align}$$
Hence
$$\Gamma=x_0\mathcal P_XI_n+\mathcal R_X\tilde S\mathcal R_X^T$$
Comment: Thanks a lot ! I can't figure out where I was wrong. Was it my expression for $\mathrm{I}_{n} - \frac{X X^\top}{\Vert X \Vert^{2}}$ ?
Comment: @jibounet I guess you forget the transpose of that.
Comment: Yes you're right ! I think I made a mistake there with the transpose. Thanks for your explanations.

Understand a matrix expression


Transpose of a 3D Tensor

Question: I'm finding it difficult to wrap my head around how the transpose operation works for Tensors of Rank 3 and above.
Here's an example in PyTorch
I was doing a transpose of tensors of rank 3 and according to transpose rule for rank 2 tensors which follow simple 2D matrix transpose rule. $$ {A_{ij}}^T =A_{ji} $$
But when I transposed a rank 3 tensor I ended up with a different output given below.
Can someone explain to me how is this happening?
a = torch.tensor(
        [[[1., 1., 1.],
         [1., 1., 1.],
         [1., 1., 1.]],

        [[0., 0., 0.],
         [0., 0., 0.],
         [0., 0., 0.]],

        [[1., 1., 1.],
         [1., 1., 1.],
         [1., 1., 1.]]])

Printing the tensor and the shape gives the following:
print(a)
print(a.shape)
tensor([[[1., 1., 1.],
         [1., 1., 1.],
         [1., 1., 1.]],

        [[0., 0., 0.],
         [0., 0., 0.],
         [0., 0., 0.]],

        [[1., 1., 1.],
         [1., 1., 1.],
         [1., 1., 1.]]])
torch.Size([3, 3, 3])

Transposing with a.T we get
tensor([[[1., 0., 1.],
         [1., 0., 1.],
         [1., 0., 1.]],

        [[1., 0., 1.],
         [1., 0., 1.],
         [1., 0., 1.]],

        [[1., 0., 1.],
         [1., 0., 1.],
         [1., 0., 1.]]])

Edit:
As suggested I'm adding another example with unique values for every entry.
Tensor b
tensor([[[ 1.,  2.,  3.],
         [ 4.,  5.,  6.],
         [ 7.,  8.,  9.]],

        [[10., 11., 12.],
         [13., 14., 15.],
         [16., 17., 18.]],

        [[19., 20., 21.],
         [22., 23., 24.],
         [25., 26., 27.]]])

Transpose of Tensor b ( b.T)
tensor([[[ 1., 10., 19.],
         [ 4., 13., 22.],
         [ 7., 16., 25.]],

        [[ 2., 11., 20.],
         [ 5., 14., 23.],
         [ 8., 17., 26.]],

        [[ 3., 12., 21.],
         [ 6., 15., 24.],
         [ 9., 18., 27.]]])
```

Comment: The transpose of a general higher order tensor is a [difficult topic](https://math.stackexchange.com/questions/906254/how-do-you-transpose-tensors?rq=1).
Comment: Let's see a different example where the rows/columns are distinguishable.
Answer: It looks like this is happening.
Suppose the tensor components are $a_{ijk}$ where $i$ tells you which matrix you're talking about, $j$ says which row you're talking about, and $k$ says which column you're talking about. The transpose you presented then has components $a_{kji}$, so transpose seems to reverse the indices, or at least swapp first/last indices.
Of course your $jk$ indices can be swapped without changing $a_{ijk}$ which means $a_{jki}$ is another possibility; you should have made the rows/columns in your choice of $a$ distinguishable so you can better see what the operation is doing.
Comment: So basically this is a programmed behavior specific to this framework, and according to what you are suggesting there can be other outputs to this transpose operation that theoretically are just as valid?
Comment: Yes. ${}{}{}{}$

Transpose of a 3D Tensor


Prove that if $f(x)$ is a polynomial of degree $n$, then $f(x+a)$ is also a polynomial of degree $n$

Question: I am trying to prove an exercise from 'Chapter 1, Calculus Vol. I' by Apostol:
If $f(x)$ is a polynomial of degree $n$, then show that $f(x+a)$ is also a polynomial of degree $n$.
I have outlined two proofs for this:

Assume $f(x) = \sum_{k = 0}^{n}c_{k}x^{k}$, which gives us  $f(x+a) = \sum_{k = 0}^{n}c_{k}(x+a)^{k}$. Using the binomial expansion formula, we can expand each term and show: $$f(x+a)=\sum_{k=0}^{n}\left(\sum_{i=k}^{n}{i \choose k}a^{i-k}c_i \right) x^{k}$$
Argue that $f(x+a)$ is a linear translation of $f(x)$ along the $x$-axis and, graphically, the functions have the same form.

I am looking for a more elegant proof not involving such heavy algebra or arguments involving higher-order objects such as graphs.
Comment: Heavy algebra??? You ain't seen nothing yet!
Comment: (2) isn't a proof at all, really. I think (1) is the canonical proof, and hints at the fact that we should practice this sort of algebraic manipulation with summations until it doesn't seem heavy.
Comment: #1 is the standard argument, and arguably the more general one. #2 is basically just hand-waving it as written, but since this is tagged `real-analysis` and assuming you know the [fundamental theorem of algebra](https://en.wikipedia.org/wiki/Fundamental_theorem_of_algebra) you could perhaps argue that $x_k$ is a (complex) root of $f(x)$ iff $x_k-a$ is a root of $f(x+a)$ and the two have the same multiplicity. The degree of a complex polynomial equals the number of roots counting multiplicities, so $f(x)$ and $f(x+a)$ have the same degree. That said, using FTA here _is_ quite heavy-handed.
Comment: How about that $\frac {d}{dx} f(x+a) = \frac {d}{dx} f(x)$ and $\frac {d^{n+1}}{dx^{n+1}} f(x+a) = \frac {d^{n+1}}{dx^{n+1}} f(x) = 0$
Comment: @DougM that is an interesting approach. The $(n+1)^{\text{th}}$ derivative shows that the degree of the function is $<=n$. However, I don't think we can equate the lower order derivatives.
Comment: @GregMartin thank you for your comment. Yes, in fact, as is evident from that I am reading Apostol, I am very new to this and I need more practice.
Comment: @JyrkiLahtonen Indeed I am just opening up my eyes :D
Comment: I'm not even sure that the FTA argument does the trick: it proves that $f(x+a)$ must have the same degree *if* it's a polynomial, but doesn't prove that it's a polynomial to begin with.
Comment: @GregMartin I would assume that polynomials being closed under composition has been established by that point.
Comment: That seems unlikely, given that the proof of that fact is at least as difficult as the proof (1) in the OP.
Comment: @GregMartin Fair point, but textbook exercises are sometimes exercising the obvious.
Answer: $$f(x) = \sum_{n=0}^{k}c_{n}x^{n}$$
$$f(x+a) = \sum_{n=0}^{k}c_{n}(x+a)^{n}$$
$$(x+a)^n = \sum_{l=0}^{n}b_{l}a^{n-l}x^{l}$$
$$f(x+a) = \sum_{n=0}^{k}c_{n}\sum_{l=0}^{n}{\gamma}_{l}x^{l}$$
gamma is the new constant since $b_{l}a^{n-l}$ is constant
rhs
$$\sum_{n=0}^{k}c_{n}\sum_{l=0}^{n}{\gamma}_{l}x^{l} = \sum_{n=0}^{k-1}c_n\sum_{l=0}^n{\gamma}_{l}x^{l} + c_{k}\sum_{l=0}^{k}{\gamma}_{l}x^{l}$$
$$c_{k}\sum_{l=0}^{k}{\gamma}_{l}x^{l} = \sum_{l=0}^{k}c_{k}{\gamma}_{l}x^{l} = \sum_{l=0}^{k}{\beta}_{l}x^{l}$$
following the same recursive solution
$$f(x+a) = \sum_{l=0}^{k}{\beta}_{l}x^{l} + \sum_{l=0}^{k-1}{\beta}_{l}x^{l} + \sum_{l=0}^{k-2}{\beta}_{l}x^{l}.....$$
$$F(x+a) = \sum_{l=0}^{k}{\beta}_{l}x^{l}$$
which is a ploynomial
Comment: don't hesitate to ask if you still have doubts


How can a covering map (as defined in Hatcher) fail to be a surjection?

Question: Hatcher defined a covering space as follows:
$\textbf{Defn:}$ A covering space of a space $X$ is a space $\tilde{X}$ together with a map $p: \tilde{X} \to X$ satisfying the following conditions: There exists an open cover $\{ U_{\alpha}\}$ of $X$ such that for each $\alpha$, $p^{-1}(U_{\alpha})$is a disjoint union of open sets in $\tilde{X}$ each of which is mapped by $p$ homeomorphically onto $U_{\alpha}$
I don't understand how such a map can fail to be surjective. Any concrete examples??
Answer: For instance, if $\tilde{X}$ is the empty set, and $p$ is the empty map. Then each $p^{-1}(X)$ is empty, so it is an (empty) disjoint union of open sets, "each" of which are mapped homeomorphically onto $X$ (vacuously, as there are no opens to check the condition on).
Comment: And there are intermediate situations: if $X$ is not connected, one component can be completely outside of the image for this reason.
Comment: This is not correct. The definitions asks that $p$ maps each disjoint open set *onto* $U_\alpha$. This cannot happen in your case, since the image of an empty set is empty.
Comment: @PedroTamaroff I think it is correct. The open cover of $\emptyset$ is not the empty set $\{ \emptyset \}$, it is the empty collection of sets $\{ \}$. Each open $U \in \{ \}$ satisfies any condition you want, since there are no opens to check. Do you disagree?
Comment: @area The cover is of the base space, not the covering space.
Comment: @PedroTamaroff I was being imprecise. You take the covering $\{X\}$ of the base space. What I mean is that $p^{-1}(X)$ is an empty disjoint union - it is $\cup_{U \in \emptyset} U$. Since there are no opens in this disjoint union to check, vacuously each maps onto $X$.
Comment: No, they don't map onto $X$. The induced map must be an homeomorphism, and it clearly isn't in this case.
Comment: @PedroTamaroff I think you are misunderstanding me. It is analogous to the following situation: Let A be a collection of some spaces. In this case, X. Then the empty set is always a disjoint union of spaces in A. How? It is the empty disjoint union. Each set in the disjoint union is homeomorphic to X, but this is only because there are no sets in the disjoint union. There are no induced maps that can fail to be a homeo, so all of them are homeomorphisms. This changes if you require that the preimage is a nonempty disjoint union of open sets in ...
Comment: @PedroTamaroff the covering. But really, this is maximal pedantry, and completely a distraction from the beautiful ideas of covering space theory.
Comment: The empty function is a function to $X$, but it is not an homeomorphism, because it is not onto, say.
Comment: @PedroTamaroff I know that the empty function is not a homemorphism. That is not my point.
Comment: @AreaMan OK, I see now what the misunderstanding is. You're allowing to take a union indexed by the empty set, and I wasn't.
Comment: @PedroTamaroff Precisely the difference. I am pretty sure that taking empty unions is allowed under standard set theoretic rules... it is a pretty weird thing to do though. If you know some sheaf theory, you can use this trick to figure out what a sheaf of Abelian groups on the empty set has to be.


Can a Dialog have a view model in android?

Question: I need to do an API call from a Dialog. Do I need to go back to the fragment for doing so, or is there any way to refer to the fragment view model?
Answer: Yes, it is possible and I was able to do that because class DialogFragment extends Fragment. So I added a view model just like any other fragment.

like below where BaseDialog class extends DialogFragment
Comment: this is the answer
Comment: He asked about Dialog so why you answered with DialogFragment
Answer: You can try with this:

Use interface, implement it in fragment so you have callback funtion.

Pass high ordered function, declare it like this in dialog:
var click: (() -> Unit)? = null;
Then you can set it from fragment when you instantiate your dialog.

Use shared view model, for example make view model in your activity and then you can access it from every fragment or dialog like this:

(requireActivity() as MainActivity).viewModel
Like this you can set value in view model variable (liveData often) inside your dialog and observe changes in fragment
Answer: I think you can pass a high ordered function to dialog and handle it in fragment using viewModel inside.