Using the Queue class in Python 2.6

Question: Let's assume I'm stuck using Python 2.6, and can't upgrade (even if that would help).  I've written a program that uses the Queue class.  My producer is a simple directory listing.  My consumer threads pull a file from the queue, and do stuff with it.  If the file has already been processed, I skip it. The processed list is generated before all of the threads are started, so it isn't empty.
Here's some pseudo-code.
import Queue, sys, threading

processed = []

def consumer():
    while True:
        file = dirlist.get(block=True)
        if file in processed:
            print "Ignoring %s" % file
        else:
            # do stuff here
        dirlist.task_done()

dirlist = Queue.Queue()

for f in os.listdir("/some/dir"):
    dirlist.put(f)

max_threads = 8

for i in range(max_threads):
    thr = Thread(target=consumer)
    thr.start()

dirlist.join()

The strange behavior I'm getting is that if a thread encounters a file that's already been processed, the thread stalls out and waits until the entire program ends.  I've done a little bit of testing, and the first 7 threads (assuming 8 is the max) stop, while the 8th thread keeps processing, one file at a time.  But, by doing that, I'm losing the entire reason for threading the application.
Am I doing something wrong, or is this the expected behavior of the Queue/threading classes in Python 2.6?
Comment: There must be something wrong -- how could the stalling be _expected_ to depend on a test completely unrelated to the queue?! But I don't think it's in this code despite its defects (threads not-daemon, abuse of builtin name `file`, ...) -- I don't think those could make threads stall!  Rather, how is processed populated, and, is it ever altered in the "..." part (which could be a problem since there's no lock around it)? Can you reproduce this problem with trivial population of processed (e.g. put half the files there) and trivial "...", e.g `print file`?
Answer: I tried running your code, and did not see the behavior you describe.  However, the program never exits.  I recommend changing the .get() call as follows:
    try:
        file = dirlist.get(True, 1)
    except Queue.Empty:
        return

If you want to know which thread is currently executing, you can import the thread module and print thread.get_ident().
I added the following line after the .get():
    print file, thread.get_ident()

and got the following output:
bin 7116328
cygdrive 7116328
 cygwin.bat 7149424
cygwin.ico 7116328
 dev etc7598568
7149424
 fix 7331000
 home 7116328lib
 7598568sbin
 7149424Thumbs.db
 7331000
tmp 7107008
 usr 7116328
var 7598568proc
 7441800

The output is messy because the threads are writing to stdout at the same time.  The variety of thread identifiers further confirms that all of the threads are running.
Perhaps something is wrong in the real code or your test methodology, but not in the code you posted?
Comment: Ah, that explains why I had to keep killing the program.  Thanks for the tip.
Answer: Since this problem only manifests itself when finding a file that's already been processed, it seems like this is something to do with the processed list itself. Have you tried implementing a simple lock? For example:
processed = []
processed_lock = threading.Lock()

def consumer():
    while True:
        with processed_lock.acquire():
            fileInList = file in processed
        if fileInList:
            # ... et cetera

Threading tends to cause the strangest bugs, even if they seem like they "shouldn't" happen. Using locks on shared variables is the first step to make sure you don't end up with some kind of race condition that could cause threads to deadlock.

Of course, if what you're doing under # do stuff here is CPU-intensive, then Python will only run code from one thread at a time anyway, due to the Global Interpreter Lock. In that case, you may want to switch to the multiprocessing module - it's very similar to threading, though you will need to replace shared variables with another solution (see here for details).
Comment: I hadn't thought of putting a lock on the processed list.  Now that I think of it, it definitely makes sense.  And thanks for the recommendation to use the multiprocessing module.

Using the Queue class in Python 2.6


How to test whether a 0-1 series is "Bernoulli or Markov distributed"?

Question: I'm currently studying for a stat exam and in a list of sample questions that came up in previous exams I stumbled upon this problem which I can't solve:

Given a series of 0s and 1s, how to test whether the series is Bernoulli or Marko distributed?

I've never actually heard the name "Markov distribution" before, but I guess the question refers to whether the series consists of independent Bernoulli trials or if there is some dependency - Markov property being the weakest kind.
We've covered nonparametric (e.g. Chi-squared) tests for specific distributions, but if I'm not mistaken, they are based on an i.i.d. assumption.
But the question seems to be exactly whether such an assumption holds, so these are no good.
Is there a common statistical test that does the trick or how should I approach this problem?
Answer: I think your supposition is correct, but the problem is not crisply stated.
In a two-state Markov Chain (MC) let the conditional change probabilities
be $\alpha = P(X_i = 1|X_{i-1} = 0)$ and $\beta = P(X_i = 0|X_{i-1} = 1)$.
With this notation, if $\alpha = \beta = 1/2,$ then the MC is not distinguishable from an independent Bernoulli sequence.
But if the MC is not trivial in this sense, and does have a dependent structure,
then you might be able to detect it using a 'runs test' or by looking at
autocorrelations.
Runs test. In an independent Bernoulli sequence with $P(X_i = 0) = P(X_i = 1) = 1/2,$ runs of $0$'s should average 2 in length, and similarly for runs of $1$'s. So by looking at the average lengths of runs, you can detect dependent structure.
See this page from the NIST Engineering Statistics Handbook. (Before revision, I suggested the Wikipedia page on this topic, but then I read it
carefully, found it cryptic and possibly misleading, and regretted that suggestion.)
Auto-correlation fundtion (ACF). The (estimated) autocorrelation of lag $\ell$ of a
sequence is (roughly) the correlation of the sequence $x_1, x_2, \dots, x_{n-\ell}$ with the sequence $x_{\ell}, x_{\ell+1}, \dots x_n.$ (The quibble is
that the mean and SD of the whole sequence are used in computing the correlation.) The ACF is a sequence of autocorrelations for various lags
$\ell = 0, 1, 2, \dots.$
Here is an example for a sequence from a MC simulated in R statistical software.
n = 1000;  x = numeric(n)
x[1] = 0  # start chain in state 0 at step 1
for (i in 2:n) {
  if (x[i-1]==0) x[i] = rbinom(1, 1, 1/3)  # obs from Bernoulli(1/3)
  else x[i] = rbinom(1, 1, 2/3)
}
acf(x)

Of course, the autocorrelation for lag 0 is unity. The ACF plot shows significant autocorrelations for lags 1 and 2 (bars outside the dotted
blue boundaries).
Autocorrelations for larger lags are not significantly different from 0,
because Markov dependence on a state several steps back "wears away" as the chain reaches steady state.

There are several relevant Wikipedia pages; you could google 'autocorrelation test',
'autocorrelation function', and so on. Autocorrelation methods are widely used in a number of fields, including economics.
Note: These 1000 values from a simulated MC (about half 0's and half 1's) happened to have 350 runs, whereas we would expect to see about 500 runs
in a Bernoulli sequence from tosses of a fair coin; 350 is very much fewer than
expected, and so we would reject the null hypothesis of random trials. See the NIST page linked above for details. (In R, one can count runs with the code
rle(x), where 'rle' stands for 'run length encoding'; type help rle
in the Session window of R for details.)


div draggable not working after reset

Question: I have 4 divs on my page, say div1, div2,div3,div4 which are draggable.
<code>$("#div1").draggable();
$("#div2").draggable();
$("#div3").draggable();
$("#div4").draggable();
</code>
First time when the page loads, I have captured positions of all divs' like mentioned below.
<code>div1Position = $("#div1").position();
div2Position = $("#div2").position();
div3Position = $("#div3").position();
div3Position = $("#div4").position();
</code>
I have a reset button on my page. On the click of this button, I'm reseting the positions of all divs to their original position.
<code>$("#div1").css({ top: div1Position.top, left: div1Position.left, position: 'initial' });
$("#div2").css({ top: div2Position.top, left: div2Position.left, position: 'initial' });
$("#div3").css({ top: div3Position.top, left: div3Position.left, position: 'initial' });
$("#div4").css({ top: div4Position.top, left: div4Position.left, position: 'initial' });
</code>
Till here it is working fine. But after reset , all the divs are no more draggable.Am I doing something wrong??
Comment: can you remove the `position: 'initial'` while reseting and try again.
Answer: You may also need to set the div as draggable again on reset button click.
<code>$("#div1").draggable();
$("#div2").draggable();
$("#div3").draggable();
$("#div4").draggable();
</code>
Comment: https://stackoverflow.com/questions/1040902/jquery-draggable-options

Similar thread

div draggable not working after reset


macOS, Swift 3: How to get data back after segue?

Question: For example: I have two UIViewControllers. The first has a button and a NSTextField, and the second has a NSTextField only.
When I click the button on the first controller — the second controller shows as popover window.
Making a transfer of some data from first controller to second is not big deal — I use a segue. But what I should to do to transfer data back — from the popover window to main window?
For example: we open the popover window, type some text in NSTextField, and I want to get this text in the NSTextField of the first ViewController.
I can't find the answer :-(
Comment: http://stackoverflow.com/documentation/ios/434/passing-data-between-view-controllers/2520/using-the-delegate-pattern-passing-data-back#t=201611080745298224365 (it's in the iOS tag, but it works on macOS, too)
Comment: You can use an unwindSegue method if your view controllers are in a navigation controller, or you can use a protocol.
Answer: In macOS it's pretty easy if you are using storyboard and segues.
NSViewController has a property presenting which contains a reference to the parent view controller.
Obtain the reference, call a method in the parent view controller to pass the data and dismiss the child view controller.
let firstViewController = presenting as! FirstViewController
firstViewController.passDataBack(data)
self.dismiss(self)

Comment: Youtube hasn't got a seal of quality. Anybody can upload stuff ;-)
Comment: Thanks! Other guy told me that the "right" way is to use delegates. That video is pretty good for understanding: https://www.youtube.com/watch?v=KhqXUi1SF4s


Swift submodules - what are they exactly

Question: Coming from the Java world, I cannot still understand exactly what Swift submodules are and are not in terms of Java packages' counterparts.

How Swift modules and submodules relate to jars and packages in the Java
world?
Can you actually import from submodules, e.g. by writing import
Module.Submodule1(...).SubmoduleN.MyClass?
Are submodules bound to directory trees, just like Java packages? If they are not, then how, where are submodules defined?

(I have not been able to find answers to these questions neither on Apple's online specification of Swift nor in the free book distributed by them. I've asked on some dev lists but the info I got there is in contradiction with Apple's documentation for the Package Manager, especially regarding the third point.)
Comment: @Martijn Pieters - I have added a pair of words.

I mostly disagree with your critics. This question is asking, basically, how the Swift module system maps (conceptually) into the Java packages/jar model.

Logical mappings between models need several sub-questions to pose useful, complete theories. This is not an opinion of mine but a basic fact.

I can delete some of the sub-questions but then my inquiry will lose completeness.
Comment: Furthermore: just as you can judge from the (accepted by me as an OP) answer, it is not only specific and consistent enough, but also not "too broad".

I suggest you to re-think your critics a bit deeper.
Answer:
Swift module equivalent to Java package, Swift framework is like Java jar. Submodule is just module inside of other module.
Yes you can, but it if you want to import just a class you have to use import class (import struct for struct, import enum for enum). Simple import can be used only with modules.
No, they're not bound to the folder trees. They're defined using Clang module system, Xcode and SPM perform most of the boilerplate for you.
Comment: In Xcode source files can be anywhere, file belongs to module if it's in the target defining module. In SPM it's somewhat stricter, like source must be in subfolder of the folder with package description, and each target in own folder(though file-system aliases work too). So: for Xcode both are true, for SPM: not really.
Comment: OK, thanks for your answer. To make sure I have understood you well, please confirm that the following is true: 1 - neither modules or submodules are bound to the source folder tree, so I can refactor the source folders without breaking my clients' imports; and 2 - submodules are defined not implicitely in any way by the source layout, but by additional tooling and meta-files. Is all this correct?
Comment: OK. Thanks a lot!


Caso o usuário acesse a home antes de efetuar login ele deve ser redirecionado para o index utilizando jsp

Question: Qual verificação devo fazer se o usuário acessar diretamente a página home.jsp sem ter feito o login? Ressaltando que não estou utilizando servlet para esta situação, apenas páginas jsp.
Código da página home.jsp:
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Bem vindo</title>

        <%
            if ((session.getAttribute("login").equals(null)) || (session.getAttribute("login").equals(""))) {
        %>
        Você não esta logado. Por favor, entre com a sua conta.
        <%
            } else {
        %>
        Seja bem vindo,
        <%=session.getAttribute("login")%>

            Listar -
                Servlet
 Listar - JSP
 Log out

        <%
            }
        %>

Código da página login.jsp:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Login</title>

        <form  action="checklogin.jsp" method="post">

                            Login

                            Login
                            <input type="text" name="login"/>

                            Senha
                            <input type="password" name="senha"/>

                            <input type="submit" value="Login" />
                            <input type="reset" value="Reset" />

                            Ainda não esta cadastrado? Cadastra-se

        </form>

Elucidando ainda mais o contexto desta aplicação segue uma imagem da aplicação, que deve implementar a figura abaixo (Apenas falta a parte que está destacada).
Comment: Vitor Oliveira, acredito que a resposta: [aqui](https://pt.stackoverflow.com/a/256625/6789) pode ajudar no entendimento da sua pergunta.
Answer: Existem várias formas de se fazer isso.
UTILIZANDO FILTROS
Você pode criar um filtro através da implementação da interface javax.servlet.Filter:
@WebFilter(filterName = "MyFilter", urlPatterns = {"/principal.jsp"}, dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ERROR, DispatcherType.INCLUDE})
public class MyFilter implements Filter {
   public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain)
      throws IOException, ServletException {

     HttpServletRequest req = (HttpServletRequest) request;

     if(req.getSession().getAttribute("login") == null) {
       response.sendRedirect("/login.jsp");
     } else {
       chain.doFilter(request, response);
     }

    }
}

ENTENDENDO O FILTRO MyFilter
Utilizamos a annotation @WebFilter que está presente na especificação Servlet 3.1 para indicar que a classe criada será um filtro.
O atributo urlPatterns que é um Array de String, indica quais URL's esse filtro vai interceptar. Ou seja, o código do método doFilter() será executado antes mesmo da request chegar na Servlet, mas apenas quando a URL requisitada for principal.jsp.
A verificação feita dentro do método doFilter foi baseada no código que você postou na pergunta, mas poderia ser qualquer outra coisa. (Assume-se que depois do login, você utiliza o método setAttribute com a chave "login", salvando algum objeto Java indicando que o usuário está logado).
UTILIZANDO JSP PURO
Essa forma é fortemente não recomendada. Use os filtros para uma melhor legibilidade e elegância do código.
Também é possível utilizar apenas JSP com uma verificação do atributo "login" da sessão, no começo da página.
Caso dê negativo, você redireciona para login.
Ficaria algo assim:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% if(request.getSession().getAttribute("login") == null) {
  resp.sendRedirect("/login.jsp");
}
%>
<!DOCTYPE html>

Acho que o código na JSP está bem claro e não necessita de maiores explicações.
Enfim, espero ter ajudado.
Qualquer dúvida, pode comentar na resposta. Abraços.
Comment: Olá amigo, bastante grato por ter elucidado este cenário. Então, não tinha conhecimentos sobre o uso do Filter, mas até então ficou claro a real funcionalidade do uso do Filter. O código da página home.jsp funcionou, apenas reescrivi e dei um clean no projeto antes de startar; mas é como você falou q não é fortemente recomendado. Pra galera que esta aprendendo a como fazer um login e um registro, em JSP, de um usuário segue o site que peguei como exemplo: http://www.javaknowledge.info/login-and-registration-example-in-jsp-with-session/


A mathematical statement

Question: Is it technically correct to write "Let $A$ and $B$ be two sets"? or we should write "Let $A,B$ be sets"? Actually I am confused whether by mentioning "two" we are ruling out the possibility of $A=B$. Please suggest!
Comment: You can always say: "Let $A$ and $B$ be two sets, not necessarily distinct", to avoid confusion.
Comment: I do not think the presence of 'two' affects the implied meaning in any way.
Comment: I think the logical statement is true irrespective of the language used here; you are not discounting the notion that possibly $A=B$ with the definition of two sets. Remeber that the statement $A=B$ is mealry a consequence of the fact that for two sets $A, B$ we have $A \subseteq B$ *and* $B \subseteq A$.
Comment: You can also say "a couple of sets"
Answer: As a reader, one should not make assumptions about mathematical statements which have not been stated. Thus I feel that you are not losing anything by saying "Let $A$ and $B$ be two sets".
If you have any extra conditions which you feel are non trivial for the reader, then you should mention them.
Answer: As for me, I think saying $A$ and $B$ are two sets does not rule out the possibility of $A=B$. It just saying these two sets are not always equal to each other, but they can sometimes.
Answer: If you wish to define two sets, $A$ and $B$, saying "let $A$ and $B$ be two sets" does not exclude the possibility that they will equal each other. You can continue the statement as you like, such as (if they are equal) "let $A$ and $B$ be two sets that are equal to each other" (or, and I think this is more broad but possibly more correct) "let $A$ and $B$ be two sets, which are elements of each other."
I hope this helped.

A mathematical statement


How to get ip address of azure functions

Question: How can i get the IP address of the azure function.
I would like to be able to send a request to the function and have the ip returned
I have tried this code, which hang and never completed / returned anything
import { AzureFunction, Context, HttpRequest } from "@azure/functions";

const httpTrigger: AzureFunction = function (context: Context, req: HttpRequest): void {
    console.log("HTTP trigger function processed a request.");

    const ip = req.headers["x-forwarded-for"] as string;

    context.res = {
        body: { ip },
        status: 200
    };
};

export default httpTrigger;

Answer: As you're not using an async Azure function you need to call context.done() after setting context.res in order to return.
Microsoft does recommend you use async functions.
import { AzureFunction, Context, HttpRequest } from "@azure/functions";

const httpTrigger: AzureFunction = function (context: Context, req: HttpRequest): void {
    console.log("HTTP trigger function processed a request.");

    const ip = req.headers["x-forwarded-for"] as string;

    context.res = {
        body: { ip },
        status: 200
    };
    context.done();
};

export default httpTrigger;

Comment: Didn't know about async vs non async, thank you
Comment: Using this code you provided, `ip` is undefined.
Comment: @GMN - are you running the function locally? The X-Forwarded-For header is likely only set when the function app is running behind a reverse proxy (e.g., deployed to Azure).
Comment: Ah good point i havent' tried it deployed yet.


How do I use a DNS service to change hosts with zero downtime?

Question: I'm moving DomainX  from OldHost-dotcom to NewHost-dotcom. I'm using a DNS service, in an attempt to
make the move with zero or near-zero downtime, but I don't know which of two approaches would be
better.
I really don't know what I'm doing. Can you tell me which of the two approaches below is better?
Thanks!
Given--
DNS setting at registrar:
ns1.OldHost-dotcom
DNS address for current host:
ns1.OldHost-dotcom
DNS address for new host:
ns1.NewHost-dotcom
DNS address for DNS service:
ns1.NewDynamicDNS-dotcom
HostingSpace for OldHost-dotcom
oldhost-dotcom/myspace/
HostingSpace for NewHost-dotcom
newhost-dotcom/myspace/
The content of DomainX-dotcom will be available at both OldHost-dotcom and NewHost-dotcom during the transition process,
so overlap is fine.
Approach_1
Pointing the New DNS Back at the Old DNS.
Within the DNS service interface, I set the DNS address to the OLD host address...
then at my registrar, give the DNS address of my new DNS service.
Wait three days.
After the address of my DNS service successfully propagates through the host--
after DomainX seamlessly goes to the DNS service--
then have the DNS address inside my DNS service point to the new webhosting.
Specifically:
1) Inside the DNS service interface, change the DNS address to the OLD host address, namely,
ns1.OldHost-dotcom
2) At the domain registrar, change the DNS address to the dynamic service, namely,
ns1.NewDynamicDNS-dotcom
3) Wait three days for the domain registrar to fully link DomainX to ns1.NewDynamicDNS-dotcom .
4) Inside the DNS service interface, change the DNS address to the NEW host address, namely,
ns1.NewHost-dotcom
Approach_2
Redirecting the URL to Old Web Space
Within the DNS service interface, I set a Web Redirect to the OLD hosting space...
then at my registrar, give the DNS address of my new DNS service.
Wait three days.
After the address of my DNS service successfully propagates through the host--
after DomainX seamlessly goes to the DNS service--
then have the DNS address inside my DNS service point to the new hosting space.
Specifically:
1) Inside the DNS service interface, set the Web Redirect address to the OLD hosting space, namely,
oldhost-dotcom/myspace/
2) At the domain registrar, change the DNS address to the dynamic service, namely,
ns1.NewDynamicDNS-dotcom
3) Wait three days for the domain registrar to fully link DomainX to ns1.NewDynamicDNS-dotcom .
4) Inside the DNS service interface, set the Web Redirect address to the NEW hosting space, namely,
newhost-dotcom/myspace/
Again, I've got no idea which of these approaches would work better for my zero-downtime goal,
or if either of these approaches is correct, or
if there's some third approach that would work even better.
I'd really appreciate it if you could show me a good approach!
Thanks! :)
Answer: You're making this much more complex than it needs to be. Simply update the A records in your current DNS hosting service to point at the new host.
Your DNS hosting service will often be your domain registrar. If your web old host is providing DNS services move your DNS service back to your registrar in advance of your server move, a free DNS provider like CloudFlare, or a paid DNS provider like AWS Route 53.
Comment: >Simply update the A records in your current DNS hosting service to point at the new host.
Thanks-- would that preserve paths? DomainX currently has attached to it some/long/paths/with/scripts/and/stuff... would these still work when DomainX is re-pointed, via the domain registrar, from nnn1.nnn.nnn to nn2.nnn.nnn?
Comment: For 99.9% of cases Cloudflare with get you there. The least possible TTL is 60 secs over there. Its important to lower the TTL before transition and wait till the previous TTL value to let that propagate.
Comment: @ziff paths are irrelevant to DNS. DNS is how domain names are mappy to servers. I use CloudFlare for this, I've done it many times, with no issues. I suggest you read up on what DNS is and how it works.
Comment: [_italic]>For 99.9% of cases Cloudflare with get you there. >I use CloudFlare for this[_italic] I've already purchased use of a DNS service, but have not applied it yet; the registrar currently points to the OldHost's DNS. I would like to have the DNS more flexibly controlled, via the DNS service, with zero downtime. Given this, should I repoint the DNS address or the IP numbers/hosting space?
Comment: Like I said, you really need to do some reading on how DNS works. Your steps are 1) Migrate DNS to CloudFlare 2) Update domain registrar to point at CloudFlare DNS servers 3) Change your A records when the new host is ready
Comment: By "migrate DNS," do you mean (a) copy the existing DNS records (as stored at OldHost) to CloudFlare; or (b) within CloudFlare, point to the NewHost IP; (c) within CloudFlare, point to the NewHost webspace; (d) something else?
Comment: Re "migrate DNS", when you set up CloudFlare it copies all your existing DNS records. This gives it the capability to replace your current DNS server. Once you change your name server settings it starts actually doing the job. Suggest you initially turn off caching, ie change orange cloud to grey, in the DNS section. If you want caching / CDN turn it back on after some testing, but default settings are fairly safe.


Erro de variável privada em C++

Question: Não estou conseguindo entender o porquê de estar dando esse erro ao compilar meu código:

In file included from test_GenericArray.cpp:2:0: GenericArray.h: In
  function 'std::ostream& operator<<(std::ostream&, const
  GenericArray&)': GenericArray.h:43:7: error: 'int
  GenericArray::length' is private    int length;

Tenho 3 arquivos, que estão abaixo:
GenericArray.h:
 #include <iostream>

const int MAX_ITEMS = 6;

class GenericArray {
 public:

  // Constructor
  GenericArray(); // Inicializa o nosso objeto com uma lista vazia.

  // Observers
  bool isFull() const;
  int  getLength() const;

  // Recebe um ponteiro para um elemento do tipo int. Esse
  // elemento int que recebemos por parametro tem uma
  // chave. Usamos essa chave para buscar um elemento em nossa lista e
  // retornar esse elemento, caso ele exista.

  // Se o elemento nao estiver na lista, entao nao precisa alterar o
  // parametro item, basta avisar na variavel found que o elemento nao
  // foi achado.
  void retrieveItem(int& item, bool& found);

  // Insere o elemento na lista em uma posicao arbitraria no caso do
  // Unsorted e na posicao correta no caso do Sorted.
  void insertItem(int item);

  // Remove uma ocorrencia do elemento na lista.
  void deleteItem(int item); // Nos nao sabemos qual a chave da
                  // classe int, entao precisamos
                  // de um objeto para saber se os
                  // elementos listados sao iguais ao
                  // nosso.

  //friend std::ostream& operator<<(std::ostream& os, const GenericArray& st);

 private:
  int length;
  int info[MAX_ITEMS];
};

test_Generic_Array.cpp (arquivo que exibe na tela os dados):
#include <iostream>
#include "GenericArray.h"

using namespace std;

ostream& operator<<(ostream& os, const GenericArray& array){
  for (int i = 0; i < array.length; i++) {
    os << array.info[i];
  }
  os << endl;
  return os;
}

int main(){
  GenericArray unsorted;

  int elements[4] = {5, 7, 6, 9};

  for (int i = 0; i <= 3; i++) {
    int item(elements[i]);
    unsorted.insertItem(item);
  }

  cout << unsorted;

  int item(7);
  bool     found = false;

  unsorted.retrieveItem(item, found);
  cout << item << endl;

  unsorted.deleteItem(item);
  cout << unsorted;
  cout << "Fim" << endl;

}

GenericArray.cpp:
#include "GenericArray.h"

GenericArray::GenericArray(){
    length = 0;
}

bool GenericArray::isFull() const { //8:22
    return(length == MAX_ITEMS);
}

int GenericArray::getLength() const {
    return length;
}

void GenericArray::retrieveItem(int& item, bool& found){
    int location = 0;
    found = false;

    while((location < length) && !found ){
        if(item == info[location]){
            found = true;
            item = info[location];
        }
        location++;
    }
}

void GenericArray::insertItem(int item){
    info[length] = item;
    length++;
}
void GenericArray::deleteItem(int item){
    int location = 0;

    while(item != info[location]){
        location++;
    }

    info[location] = info[length-1];
    length--;
}

Answer: A linha
for (int i = 0; i < array.length; i++) {

está acessando uma propriedade chamada length na classe, mas ela está declarada como privada e portanto não pode ser acessada. A solução poderia ser deixá-la pública, mas acho que pelo código, a intenção indicada supõe a solução que seria mudar para:
for (int i = 0; i < array.getLength(); i++) {

Coloquei no GitHub para referência futura.

Erro de variável privada em C++


Facebook SDK share always returns sharerDidCancel

Question: I'm trying to share a post with the Facebook SDK, but sharerDidCancel is always called, either if I share or cancel the post.
Here is my code
<code>- (void)shareFacebook {
    FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
    content.contentURL = [NSURL URLWithString:@"http://www.google.com"];
    [FBSDKShareDialog showFromViewController:view
                                 withContent:content
                                    delegate:self];
}

- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults :(NSDictionary*)results {
    NSLog(@"FB: SHARE RESULTS=%@\n",[results debugDescription]);
}

- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error {
    NSLog(@"FB: ERROR=%@\n",[error debugDescription]);
}

- (void)sharerDidCancel:(id<FBSDKSharing>)sharer {
    NSLog(@"FB: CANCELED SHARER=%@\n",[sharer debugDescription]);
}
</code>
This class implements the FBSDKSharingDelegate, in my Info.plist I already entered the FacebookAppID and FacebookDisplayName.
In my AppDelegate I have this method:
<code>- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
}
</code>
What I'm doing wrong? I exactly followed the Facebook SDK guide.
Thanks
Answer: Solved, my error was in the AppDelegate method:
here is a link with the solution http://jitu1990.blogspot.it/2015/05/share-with-facebook-from-ios-app.html
And here is the code
<code>- (BOOL)application:(UIApplication *)application openURL:(NSURL* )url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                      openURL:url sourceApplication:sourceApplication annotation:annotation];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [FBSDKAppEvents activateApp];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    return [ [FBSDKApplicationDelegate sharedInstance] application :application
             didFinishLaunchingWithOptions:launchOptions]
  }
</code>
Comment: I also have implemented the same but still shareDidCancel is called always...


Emulate cortex-a53 (arm64) in qemu

Question: I would like to emulate with qemu an aarch64 system. The system has been tested on the board and works perfectly. The board is based on the allwinner A64. I started using qemu from this repo : git://git.qemu.org/qemu.git but the command I run just doesn't start it, I get a black screen :
<code>./qemu-system-aarch64 \
  -machine type=virt \
  -cpu cortex-a53 \
  -nographic \
  -smp 1 \
  -m 2048 \
  -kernel my_image
  -serial stdio
</code>
I'm not sure about the need of pointing to the kernel file and initrd one and there's no -v flag for qemu to debug
Comment: @DaniëlW.Crompton Unfortunately not
Comment: Did you ever get a satisfactory answer?
Answer:
First you have to make sure that you have compiled your kernel file according to aarch64. You can check this by running the "file my_image" command in the terminal.

Second, make sure that the qemu repo for "qemu-system-aarch64" is working properly. You can find a simple qemu example and run it.

Thirdly, you should check if the qemu version you selected has cortex-a53 in the cpu list. You can see this simply with the command "qemu-system-aarch64 -M ?"
Answer: You can use gdb for debug kernel. Just start qemu as gdb server:  need add another two options in the end: " -S -s" when you run qemu. After you could use gdb client from aarch64 cross tools for debugging your kernel. Also looks like '-dtb' options is also required.
Answer: I will say I've never used qemu, but I see that you're using nographic and not setting up a console.   You can open a console connection to qemu, which will let you halt the emulated arm processor, and observe the registers and processor state.  Try checking out this link, and search for "QEMU WITHOUT X":
https://www.aurel32.net/info/debian_arm_qemu.php
Edit: Full documenation of console commands is here:
http://wiki.qemu.org/download/qemu-doc.html#pcsys_005fmonitor

Emulate cortex-a53 (arm64) in qemu


modulating by a tritone

Question: I have heard that modulating by diminished fifth/tritone is the hardest of all modulations. Interestingly my non-musician husband has just composed a tune with a smooth tritone modulation (ie.  from C to F sharp below). Apparently the key to his success was: the last chord of his tune before modulation happened to be G major, which continued to F# major (tonic of the new key). In other words, root movement by a semitone/minor second.
Does anyone have other strategies for a successful tritone modulation, especially if the tune does not lead to such a "semitone/minor second" chord progression?
Comment: In Queen's [*Lazing on a Sunday afternoon*](https://www.youtube.com/watch?v=OU6EyXcFBxA), they do a direct modulation from E♭ major to A major right before the guitar solo at the end. No idea how they get away with it...
Comment: In other words, if i want to modulate from the key of F major to the key of B major, and the tune around the key change does not allow a "C major to B major" chord progression, what strategy should i use/ what pivot chord(s) should i choose?
Comment: There is such thing as "direct modulation," where you just plop into a new key without pivoting or preparing.
Answer: A very traditional way would be to pivot from a Neapolitan sixth chord (bII6) in your first key to V6 in your second key.  For instance, in C major, you'd have a first inversion Db major (this is your Neapolitan) which becomes the dominant of Gb, and resolves to Gb, modulating a tritone.
If this is too abrupt for you, you may want to smooth out this modulation with a brief transition to the parallel minor (C minor in this case) and arrive at your Db chord through chromatic alteration of of a iv or ii°.  For instance, CMaj - Fmin - DbMaj - GbMaj.
Comment: Thanks @Casey-rule. So in the above example i could replace Fm with D dim. Is first inversion OK?
Comment: yes and yes.  First inversion would work very nicely.
Comment: In addition, you can think the `V` chord of the first scale as the Neapolitan sixth chord of the second scale. For example, in C maj./min., the `V` chord is G major, i.e. the Neapolitan sixth chord of F#/Gb maj./min. The "problem", though, is that the `V` (= Neapolitan) should be written in first inversion, so: you will either have to double one of the other notes (in case you don't want to double the leading-tone), or, in case you are going to F# major, you  will have to deal with augmented seconds (for example, D to E#, or G to A#).
Answer: One way to see and realize a modulation by a tritone is in two steps of a minor third. A modulation by a minor third corresponds to a change from major to minor (or vice versa). From F major to B major this could look like this (these are keys, not chords):
F major => F minor / Ab major => Ab/G# minor / B major)
One example of a realization of that idea is the following progression:
||  F   |  Bb  Bbm  |  Db (C#)  |  C#m  F#7  |  B    ||
Another important thing to notice is that due to the tritone interval between the roots of the two keys, also the V7 chords of both keys are a tritone apart. This means that the V7 of the first key is the tritone substitution of the V7 of the new key. Example: C7 is the V7 of F major, and F#7 is the V7 of B major. Note that C7 and F#7 share (enharmonically) the two most important chord tones: the E (3rd of C7, 7th of F#7) and the Bb (A#) (7th of C7 and 3rd of F#7). This is why these chords can replace each other, which can of course be used for modulating between the two keys. The tritone interval of both V7 chords (E - Bb/A#) can resolve in two ways:
E -> F
Bb -> A (corresponding to C7 -> F)
or
E -> D#
A# -> B (corresponding to F#7 -> B)
Comment: @mey: Yes, that could work, but you need to confirm B major as the new tonic by some cadence (e.g. II-V-I), otherwise it may sound a bit ambiguous.
Comment: @mey: I've added something on tritone substitution, which I think shouldn't be missing when considering modulating by a tritone.
Comment: Thanks to your answer @MattL. , i tried a shortened version of this progression to be used when i am "in a hurry" to modulate. I went from F maj triad straight to Ab maj triad and again straight to B maj triad. Which also sounds nice to me ☺
Comment: Oh yes. Tritone substitution for tritone modulation,  sounds great :) @MattL.
Answer: There are many different ways to approach this, but I think the simplest way would be to take advantage of chromatic movement similar to the one in your examples.
There are two simple ways that I can think of to modulate that way off the top of my head. That use chromatic movement and dominants to pull you to the new key.
The first one is rather simple. Using your example of going from the key of B major to the key of F major you would start on the tonic chord  B (I of B major) then go to C7 (V7 of F major) chromatically then go to F (I of F major). It's very simple and gets the job done.
The second one is a little more complex, but personally I think sound better. Using your example of going from the key of F major to the key of B major you would start on the tonic chord  F (I of F major) then go to BbMaj7 (A#Maj) (IV7 of F major) then go to Bbø7 (A#ø7) (viio7 of B major) chromatically then finally  go to B (I of B major).
Both of these need to be voiced correctly to sound right, but should get the job done. If you are interested in modulating to distant key I recommend studying impressionist pieces as they tend to modulate to distant keys pretty often in creative ways.
Comment: Thanks @Dom, apparently half-diminished and diminished chords are very versatile for modulation... So when googling for impressionist pieces (i assume they have free sheet musics, free of charge?), which keywords should i enter to help me find the relevant pieces faster than of reading line after line? (I mean, is there any?)
Comment: As for voicing correctly, do you recommend certain inversions, or perhaps i only need the root, 3rd and 7th?
Answer: Another way to modulate by a tritone is by using the French augmented sixth chord of the original key which contains the same notes as the... French augmented sixth chord of the destination key.
For example, the French augmented sixth chord in the key of C is D F# Ab C, and, in the key of F#, G# B# D F#.
If you write the French sixth of C in root position, you'd have the French sixth of F# in second inversion, and afterwards you can resolve smoothly to the V or the I 6/4 of F#.
Answer: I actually just composed a piece that modulated a tritone up! My methods were... unconventional, but hey, it worked!
So basically, I had this basic chord progression:
| Em | B | C | Adim Fdim |
And I wanted to modulate up to Bb minor. So, what I did, was resolved to E diminished instead of Em. Then, I had the lead voice play a Bb over it. Then, after a measure, I played the Bb minor and had the lead voice continue playing Bb so that the transition was smoother.
When you do this, you do need to establish the chord progression again because if you don't, it feels offset (because it was).
Basically, my point here is, a lot of things are possible. And, the only way to find out what is possible is by expirementing.
TL;DR I used the diminished 1 chord to modulate up a tritone
Comment: What you did is called a "common-tone modulation".

modulating by a tritone


Why is menu item for custom module not showing?

Question: I'm new to Drupal and about to write my first custom module ("configurator") with help of the drupal documentation.
I've implemented the menu hook:
function configurator_menu()
{
    $items = array();
    $items['configurator'] = array(
        'title' => 'Configurator',
        'page callback' => 'configurator_init',
        'access callback' => TRUE,
        'type' => MENU_CALLBACK,
     );
     return $items;
}

function configurator_init()
{
    return "
 Hello World
";
}

At "Administer > Site Building > Modules" the module is shown and activated. Cache has been cleared, but somehow the menu item for my "configurator" module is not showing up.
Why?
Answer: You've either forgotten to set the menu_name key for the menu you want to add the link to:
$items['configurator'] = array(
  'title' => 'Configurator',
  'page callback' => 'configurator_init',
  'access callback' => TRUE,
  'type' => MENU_CALLBACK,
  'menu_name' => 'primary-links',
);

Or you want to use the default ('navigation'), but you're using the wrong type. It should be
$items['configurator'] = array(
  'title' => 'Configurator',
  'page callback' => 'configurator_init',
  'access callback' => TRUE,
  'type' => MENU_NORMAL_ITEM,
);

MENU_CALLBACK is:

Callbacks simply register a path so that the correct function is fired when the URL is accessed. They are not shown in the menu.

Emphasis mine.


AWS Opsworks Chef deployment using S3 Archive without making archive public

Question: I'm trying to use the S3 Archive method to do a Chef deployment in Opsworks. I put the Chef archive in S3 and copied the address of https://s3.us-east-2.amazonaws.com/redacted/redacted.tar.gz. I gave the Instance Profile used by the instance full read/write access to the "redacted" bucket. However, during a deployment I get "setup_failed" and the logs show a 403 Forbidden while trying to access the S3 archive.
I found some AWS docs that suggested making the archive public. However I do not want my Chef code to be public to the world.
Why am I getting a 403 Forbidden even though the Instance Profile has the correct permissions to the archive?
Answer: How do you access the files in S3 bucket?

Over HTTPS it won't work without making the files public. That's the case if in your Chef script you're doing for example:
wget https://s3.us-east-2.amazonaws.com/redacted/redacted.tar.gz

To make use of the IAM Role you'll have to download the files using AWS-CLI. That will get the correct permissions and should be able to download the file. For example:
aws s3 cp s3://redacted/redacted.tar.gz .

This should work.

Hope that helps :)
Comment: Thanks for the response! Option 1 is out of the question because I can't make it public. Option 2 just flat out doesn't work, which is odd, because it absolutely should work. I've verified the IAM permissions are correct and that I'm using the correct role in the AWS CLI. I'm starting to wonder if there's a bug.
Comment: Something to note about option 2: the address of the bucket has to be a URL (starting with https:..), NOT a s3 resource (starting with s3://...). And we know that IAM permissions don't work when accessing resources over HTTPS. So the options given seem to contradict each other. It makes no sense that it asks for a HTTPS link AND IAM credentials when the two aren't compatible.
Answer: I drove myself nuts trying to get this to work before. Trying to access the bucket during initial boot would always fail. I think it had something to do with boot timing, and Chef asking for the file before the instance had rights to do so.
Sadly I moved on and gave up on the Opsworks chef solution. (This is even after talking to one of their Opsworks techs!) I've moved on the testing the  Opsworks Chef Automate solution.


Compartilhar PDF

Question: Tenho uma aplicação que gera um pdf a partir de um conteúdo interno.
Preciso compartilhar este pdf, e para isso, faço da seguinte forma:
 final String filePath = file.getAbsolutePath();
 final Uri arquivo = Uri.parse( filePath);
 final Intent _intent = new Intent();
            _intent.setAction(Intent.ACTION_SEND);
            _intent.putExtra(Intent.EXTRA_STREAM,  arquivo);
            _intent.putExtra(Intent.EXTRA_TEXT,  "Meu  PDF");
            _intent.putExtra(Intent.EXTRA_TITLE,   "Meu pdf");

            _intent.setType("application/pdf");

            startActivity(Intent.createChooser(_intent, "Compartilhar"));

Sendo a variável arquivo uma Uri, com o caminho que do arquivo, que é gerado ao abrir a tela.
A intent é chamada após o click do usuário.
Quando tento enviar por email, o arquivo não é anexado.
Exibe uma Toast dizendo que não é possível anexar um arquivo vazio.
Mas quando vou até a pasta, ele abre normalmente!
Como faço para conseguir compartilhar por email este pdf?
Answer: O erro ocorre devido a forma em que se pega a Uri do arquivo:
 final String filePath = file.getAbsolutePath();
 final Uri arquivo = Uri.parse( filePath);

Para arquivos internos deve se usar Uri.fromFile.
Exemplo:
 final Uri arquivo = Uri.fromFile(file);

Compartilhar PDF


What tools do you recommend for getting data out of SQLServer 2000 databases?

Question: This organization has data in various places including MySQL 5 databases and SQLServer 2000 (developer edition) on Win2k Server. For the data in MySQL I have used PHPMyAdmin, mysqldump, and so on to get data out and transform it. I've also written some simple Ruby Rails code to work with the MySQL data and used SQLYog (pro) to transfer data from SQLServer 2000 to MySQL 5. Now I need to get some data from the SQL Server 2000 databases and I'd like to do it without having to write code (or make changes to legacy ColdFusion app's codebase). This doesn't seem unreasonable ...
SQL Server's Query Analyzer can probably save query output somehow. I'm pretty sure I could use the SQL Server console as well and pipe that into a file in a console and then do transforms on that. What's a better tool? Is there a tool like PHPMyAdmin for SQLServer 2000 or some other quick way to get CSV from SQLServer 2000  with little or no code?
Thanks,
adric
Comment: SQL Server 8? Judging by the reference to Query Analyzer, I assume you mean SQL Server 2000? Or possibly 7?
Comment: Yes, they are SQL Server 2000 Developer edition. I'll use that if it's more clear and feel free to retag. Thanks!
Comment: Nobody uses numbers since SQL Server 2000 anymore. Before that there's been 6.5, 7... But after it was 2000, 2005, 2008, 2010. So please replace 8 with 2000. It will de-confuse people.
Comment: be confused no more!  yes, even though sql server 2000 is sql server version 8, there's something very unusual about calling it sql server 8, possibly it sounds too much like sybase 8 ?
Answer: SQL Server 2000 came with Data Transformation Services (DTS)1 which allows data to be moved from one database to another,2 potentially including transformations.
This is designed to move bulk data, Query Analyzer isn't.

1 Replaced with SQL Server Intergration Services (SSIS) in newer versions.
2 Or anything with an ADO driver. I've used it to move data to from Excel files in the past.
Answer: The closet tool you'll find in SQL Server 2000 to mysqldump is the BCP utility  You can export tables out of the database and into csv without writing any code, although it will require at least some batch files.
eg.
<code>BCP northwind.dbo.customers out c:\customers.txt -c -t, -T –S<servername>
</code>
If you want to pipe output from queries, then the command line version of query analyzer is called osql.  You can run this from the command prompt & do things like pipe output, or specify an output file.
IMHO the least painful way of getting sql server data into mysql is by using linked servers.  I answered a similar question about getting mysql data into sql server, but the same method can be applied.  This will of course, require more code writing.
Comment: If you want to run queries you should still be using bcp, not osql.  osql isn't designed to output files correctly like that.  Change the object name "northwind.dbo.customers" to a query, then change "out" to "queryout".  BCP is installed on all SQL servers from at least 6.5 and up.
Comment: BCP manpage you linked looks promising. I'll see if it's available on one of our servers next time I'm on site.

Re: linked server Yes, I can use SQLYog to push the tables into a MySQL database local or remote, but the users are updating the SQL server database so I'd have to do that regularly/before every data pull. I'm hoping not to have to do all that for this.

Thanks!
Comment: BCP works well. I may have a followup question on it (field names in first row)in a few minutes. Thanks!
Answer: There is as mentioned previously the DTS option, or I believe the simpler GUI incarnation of it via right click and export. You can do this to excel, CSV or other file formats but I believe that you will need to recreate the table structure manually.
One tool I have used before for migrating SQL Server databases (structure as well) was Apex software's SQL diff, as far as moving to another platform, well I don't know exactly how that would work, it may be clever enough to "do what you Mean"
Comment: You don't need to recreate the schema manually - Rt-Click (on database) > Tasks > Generate Scripts (as well as a host of 3rd Party tools) can be used to script the structure in seconds.
Comment: I know I can right click in Enterprise Manager and do some things. I'll see what my export options are in there. Thanks!


Design matrix contrast coding for model selection and 'main effects' vs. 'simple main effects' interpretation in linear mixed effects model (R/Matlab)

Question: My question is about contrast coding and planned contrasts in three-way interactions for a linear mixed effects model. Sample code is provided for R and Matlab as I can work in either one, but prefer Matlab.
I have an experiment with three categorical variables:

Group (2 levels, between subjects)
Condition A (2 levels, within subjects)
Condition B (3 levels, within subjects)

The design is fully crossed (i.e. each subject is exposed to each level of B within each level of A) and the groups are balanced.
+---------+------+-------+----+----+
| Subject | Item | Group | A  | B  |
+---------+------+-------+----+----+
|       1 |    1 |     1 | A1 | B1 |
|       1 |    2 |     1 | A1 | B2 |
|       1 |    3 |     1 | A1 | B3 |
|       1 |    4 |     1 | A2 | B1 |
|       1 |    5 |     1 | A2 | B2 |
|       1 |    6 |     1 | A2 | B3 |
|       2 |    1 |     2 | A1 | B1 |
|       2 |    2 |     2 | A1 | B2 |
|       2 |    3 |     2 | A1 | B3 |
|       2 |    4 |     2 | A2 | B1 |
|       2 |    5 |     2 | A2 | B2 |
|       2 |    6 |     2 | A2 | B3 |
+---------+------+-------+----+----+

All predictor variables are coded as factors/categorical variables and ordered according to a priori hypotheses. I want to test the three-way interaction between Group, A, and B, and would like to compare B1, B2, and B3 at each level of A for each group. I fit the following model in Matlab:
lme = fitlme(data, 'respVar ~ 1 + Group*A*B + (1|Subject) + (1|Item)', 'FitMethod', 'REML', 'DummyVarCoding', 'effects', 'CheckHessian', true);

R equivalent:
library(lme4)
contrasts(data$Group) = c(-0.5, 0.5)
    contrasts(data$A) = c(-0.5, 0.5)
    contrasts(data$B) <- cbind(c(1/2,0,-1/2), c(1/2, -1/2,0))

    lme = lmer(respVar ~ 1 + Group*A*B + (1|Subject) + (1|Item), control=lmerControl(optCtrl=list(maxfun=100000)), data=data)

This gives me the main effects of each parameter. However, I also want to see the simple main effects (i.e. the effect of each level of B within a fixed level of A for each group). Does it make sense to re-fit the model with treatment/dummy coding (the default in R and Matlab)? Do I then need to apply a Bonferroni correction for multiple comparisons?
Also, I am specifying a random effects structure using model selection with AIC, and the model selected differs (by one term) depending on whether I use effects coding or treatment coding. (The difference in AIC between both models with either coding method is ~2). If I want to report the results of both models, which type of coding should I use for model selection?
Answer: I thought I would explain what I ended up doing here in case it's helpful to anyone else.

Step 1: Fit the lme with effects coding
library(MASS)
library(lme4)
library(psycholing)
library(lmerTest)
contrasts(data$Group) = contr.sum(2)
    contrasts(data$A) = contr.sum(2)
    contrasts(data$B) = contr.sum(3)

lme = lmer(respVar ~ 1 + Group*A*B + (1|Subject) + (1|Item), control=lmerControl(optCtrl=list(maxfun=100000)), data=data)

I performed model selection using sum coding and then tested the overall significance of each coefficient using anova from the lmerTest package:
lmerTest::anova(lme)

This gave me a significant Group x A x B three-way interaction.
Step 2: Switch to dummy coding and fit three models, with each level of B as the intercept.
contrasts(data$Group) = contr.treatment(2)
    contrasts(data$A) = contr.treatment(2)
contrasts(data$B) = contr.treatment(3)

# N.b. these are the default contrasts in R. contrasts(data$B)
#       B2    B3
# B1     0     0
# B2     1     0
# B3     0     1

lmeB1 = lmer(respVar ~ 1 + Group*A*B + (1|Subject) + (1|Item), control=lmerControl(optCtrl=list(maxfun=100000)), data=data)
b1sum = lmerTest::summary(lmeB1)

relevel(data$B, "B2")
lmeB2 = lmer(respVar ~ 1 + Group*A*B + (1|Subject) + (1|Item), control=lmerControl(optCtrl=list(maxfun=100000)), data=data)
b2sum = lmerTest::summary(lmeB2)

relevel(data$B, "B3")
lmeB3 = lmer(respVar ~ 1 + Group*A*B + (1|Subject) + (1|Item), control=lmerControl(optCtrl=list(maxfun=100000)), data=data)
b3sum = lmerTest::summary(lmeB3)

Step 3: Extract the contrasts of interest and apply a Bonferroni-Holm correction for multiple comparisons.
# Test the contrasts:
#  1) Group1 A1 B1 vs. Group1 A1 B2/B3
#  2) Group1 A1 B1/B2/B3 vs. Group2 A1 B1/B2/B3
#  3) Group1 A1 B1/B2/B3 vs. Group1 A2 B1/B2/B3

pvals = cbind("B1"=p.adjust(b1sum$coefficients[c(12, 11, 8, 3), 5], "holm"),
              "B2"=c(9,9,p.adjust(b2sum$coefficients[c(8, 3), 5],"holm")),
          "B3"=c(9,9,p.adjust(b3sum$coefficients[c(8, 3), 5],"holm")))

# Numbers correspond to the rows with the coefficients of interest in model$coefficients, column 5 contains the p-values.

# Reference Level=Group1
#                        B1           B2        B3
# 1a) B2:A1     0.001707473
# 1b) B3:A1     0.027679733
# 2)  Group2:A2 0.016903682 0.0328017681 0.9451504
# 3)  A2        0.127490731 0.0008424514 0.1002219

Note that I did this in R because I also included a fixed effect for participant gender, which I coded as c(0.5, -0.5) to centre the estimates on the mean of both (effectively "controlling for" gender). This is easier to do in R with the contrasts function: in MATLAB, it seems you have to specify the entire design matrix manually if you want to use something other than effects or dummy coding.
If you don't need custom contrasts, this whole process can be done much more easily in MATLAB by fitting the model with the default (dummy) variable coding:
lme = fitlme(data, 'respVar ~ 1 + Group*A*B + (1|Subject) + (1|Item)', 'FitMethod', 'REML', 'CheckHessian', true);

Then use coefTest to specific contrast matrices for your coefficients. The following gives me an F test for the contrast between my second and third coefficients---B2 and B3 in this case---with a Satterthwaite approximation for degrees of freedom. (See this reference for a discussion of significance testing for LMEs: https://doi.org/10.3758/s13428-016-0809-y)
[pval,F,DF1,DF2]=coefTest(lme, [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0; 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'DFMethod', 'Satterthwaite')


Polymeter vs Polyrhythm

Question: What is the difference between a polymeter and a polyrhythm? Do these words mean anything different for different instruments?
PS: I'm a drummer.
Answer: Hopefully these examples of 5/4:4/4 polymeter and 5:4 polyrhythm clears it up.

Polymeter
Here is a simple example of 5/4 over 4/4 polymeter notated in 4/4 time. Notice how voice A's meter is five beats (the accents illustrates the starts), while voice B's meter is four beats, and they are sort of modulating over each other. After 20 beats their accented beats will coincide again. Important is that the beat tempo is the same for both voices!

Here is the same example but notated in 5/4 time:

You could also notate voices A and B each with a different time signatures (explicit polymeter notation), but it might look confusing if you're not used to it when the bar lines don't line up inbetween the staves.

Polyrhythm
Here is a simple example of 5:4 polyrhythm notated in 4/4 time. Notice how both voices coincide and have emphasis on the first beat of each measure (or rather each complete beat ratio). Important is that the beat tempo is different between the two rhythms of voice A and B!

Here is the same example but notated in 5/4 time:
Answer: Polymeter: different voices/instruments that play different meters that de-synchronize themselves (a 9/8 piano part against a 4/4 drum part, or 7/8 on a 3/4.
Polyrhythms: different subdivisions that fit in the same bar. The classic Christmas tune "Carol of the bells" is an example of 2 against 3. Traditional Cuban rumba, and lots of West African drum rhythms use 6 against 4.
Source : Meters, polymeters, polyrhythms, compound meters. What are the differences?
Comment: Are you sure about Carol of the Bells?  Sounds to me like it's just regular old 3 all the way through.
Comment: Mykola Leontovich.Old trad.Ukranian (the song...)Some copies are written 3/4 some 3/8, but the said version had a slow 2 feel amongst the quick 3s, so probably the choir sang with that feel on purpose - musical licence !
Comment: Just listened to Carol of the Bells (who was she ?....)and it's in 6/8.So, compound duple.
Comment: It's not a duple.  You would hear dotted quarters all over the place if it were in 2.  6/8 can represent duples and triples.  Here it is most definitely a triple.
Comment: I've heard the version Tim's talking about. It also has a weird modulation to major that I haven't figured out. Can't remember the artist's name either. :(
Comment: The main poly rhythm that Chips mention is the main theme of Carol of the Bells. It is a 2 against 3 polyrhythm but it can be written simply in 3/4 as a quarter note, two eighths, quarter. If you use both hands to play this rhythm you can experience the polyrhythm first hand. Play beat one with both hands then play the rest of the notes alternating hands. For example, B RL R (both right left right) repeat. Try and keep the rhythm even and steady and you will notice that one hand is playing two notes evenly while the other is playing three notes evenly. Magic!
Answer: Polyrhythms are multi-rhythms as in a bar of ,say, 8 quavers played against 12 quaver triplets (in the same bar).They don't necessarily fit properly,but they are playable.So, the bar length stays the same, but the divisions in it are varied simultaneously against another rhythm pattern in the same bar.                                                              Polymeters  are changing lengths of bars in the same piece,e.g. 4/4 followed by 7/4 followed by 3/4, all the crotchets being equal in time to each other.An example would be 'Closest Thing to Crazy' by Katie Melua, or lots of stuff by Stravinsky.
                           Polymeters are also instruments used to measure differing values, e.g. temperature and voltage!!Not much use to drummers.
Answer: The first answer by Ulf is how I would use the terms also.  I'll just point out that there are of course more complex ways of playing polymeters and polyrhythms than just using equally long beats for each of the two (or more) lines.  In fact, polymeter and polyrhythm are not two different things, but two ends of a spectrum.  For instance, if you take Ulf's example of a five against four polymeter but just play the first note of every group of five and of every group of four, you have a five against four polyrhythm.
If anyone's interested, here's an example of a polymetric piece in 12 against 47:

cheers from rainy Vienna, Scott
Comment: That's a nice thought! I've often felt that the reason I find it so hard to separate polyrhythm and polymeter conceptually is because they feel the same to me in a strange way. Thanks for articulating why - polymeter creates polyrhythm from its downbeats.
Comment: @yaxu - My pleasure. I find polymeters and polyrhythms and their combinations endlessly fascinating, like multifaceted crystals.  They seem to lend themselves to meditation, right on the fringes of music.
Answer: Polyrhythm and polymeter demonstrate the outer limits of written music. In truth if I play a seven note repeating phrase over a four note phrase at the same speed they will synchronise every 28 notes. I can notate this within one bar or I can fit one phrase in a bar and have the other run over the bar lines. It should sound the same either way. Music notation has not yet matured sufficiently to cover a full range of polyrhythms. Personally, as I am very interested in polyrhythm (I have named nearly 40 million of them) I tend not to use bar lines, and avoid using the word polymeter. I suppose if I was improvising in 7s over 4s (varying the 7 and 4 patterns) I might technically call it polymeter but I would expect to have to explain the word to most folks, most people I meet have some understanding of the word polyrhythm.
Comment: The simplest polyrhythm that does not include straight Beats is 'Geera Ak Hap Djeb'. 'Geera' is 1110 in integer notation and 'Djeb' is 110. 'Hap Djeb' means that the Djeb is stretched twice as long (101000) 'Ak' means that the two rhythms start together in normal form. 'Geera Ak Hap Djeb' runs B1B011B0B110 (Both hands play B) 'Geera El Hap Djeb' runs 1B1211121B10 (Where the Geera and the Hap Djeb start out of phase)
Answer: Note that they are only conceptually different.  Any polymeter can be expressed as a polyrhythm and vice versa.  To take the written example already provided, the top line is the polymeter and the bottom line is exactly the same thing written as a polyrhythm:
Comment: No no, that is not true. In the polymeter example the notes coincide, but the repetitions do not.
In the polyrhythm example the repetitions coincide, but the notes do not.
Comment: Both your examples are polymeters, just differently notated.

Polymeter vs Polyrhythm


True or false? $ \dim_{\mathbb{K}}(U) - \dim_{\mathbb{K}}(\bigcap_{\varphi\in U}$Kernel$ (\varphi)) = 6$.

Question: How can I check if this statement is true or false?
Does there exist a subspace $ U$ of $ (\mathbb{K}^{15})^*$, so that $ \dim_{\mathbb{K}}(U) - \dim_{\mathbb{K}}(\bigcap_{\varphi\in U}$Kernel$ (\varphi)) = 6$.
Answer: I believe this is false. Hopefully I have not blundered, here's my reasoning:
Let $V=k^{15}$. Any $U$ subspace of $V^*$ is isomorphic to a the image of a linear operator $T:V \to V$. Think about the map $T(\vec{v})=\begin{bmatrix}\varphi_1(\vec{v})\\ \varphi_2(\vec{v})\\ \vdots \\ \varphi_{15}(\vec{v})\end{bmatrix}$.
Now the rank of $T$ is equal to $\dim \left< \varphi_i \mid i=1,\ldots, 15 \right>=\dim U$
Also the nullity of $T$ is $\dim\ker T=\dim (\cap \ker \varphi_i)$
If the statement you said is true then
$\textrm{Rank}(T)-\dim\ker(T)=6$
add $2\dim\ker T$ to both sides
$\textrm{Rank}(T)+\dim\ker(T)=6+2\dim\ker T$
Then the left is equal to 15 by the Rank-Nulity theorem so you have
$6+2\dim\ker T=15$ or $\dim\ker T=\frac{9}{2}$ which is not possible
Comment: You argument seems to be correct. Thank you very much!


Problem with building mlogit model (with no alternative specific variables)

Question: I am confused with using mlogit package to build a multinomial logit model. In my data the only variables I have are the individual specific variables, to be consistent with terms from the mformula() method description (from the package documentation).
Here is the minimal example presenting the steps I am taking to build a model:
# load data
library(RCurl)
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))

url <- getURL("https://dl.dropboxusercontent.com/u/73455291/StackOverflow/student_state_data.csv")
student.data <- read.csv(text = url, sep=",", header=TRUE)
for(name in names(student.data)) student.data[, name] <- as.factor(student.data[, name])

# build model
length(levels(student.data[, "result_state"]))   # [1] 3

library(mlogit)
student.data.m <- mlogit.data(student.data, shape = "wide", choice = "result_state")
model.m <- mlogit(result_state ~ 0 | var_1 + var_2 + var_3 + var_4 + var_5 + var_6 | 0,
              data = student.data.m[-c(1:3), ])

# predict
predict(model.m, newdata = student.data.m[c(1:3), ])

I receive the following error:
> predict(model.m, newdata = student.data.m[c(1:3), ])
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels

What am I doing wrong? Here is the console output to take a glimpse at the data:
> head(student.data)
  student_id var_1 var_2 var_3 var_4 var_5 var_6 result_state
1     216524     0     0    10     3  <NA>     1      state_3
2     245787     0     0    11     6  <NA>     0      state_3
3     120747     0     1     9     3  <NA>     1      state_3
4     130874     0     0     5     3  <NA>     0      state_1
5     156898     0     0     7     3  <NA>     0      state_3
6     241517     0     0     5     3  <NA>     1      state_1
> head(student.data.m)
          student_id var_1 var_2 var_3 var_4 var_5 var_6 result_state chid     alt
1.state_1     216524     0     0    10     3  <NA>     1        FALSE    1 state_1
1.state_2     216524     0     0    10     3  <NA>     1        FALSE    1 state_2
1.state_3     216524     0     0    10     3  <NA>     1         TRUE    1 state_3
2.state_1     245787     0     0    11     6  <NA>     0        FALSE    2 state_1
2.state_2     245787     0     0    11     6  <NA>     0        FALSE    2 state_2
2.state_3     245787     0     0    11     6  <NA>     0         TRUE    2 state_3

> sapply(names(student.data), function(name) length(levels(student.data[, name])))
  student_id        var_1        var_2        var_3        var_4        var_5        var_6 result_state
        1000            2            2            8            8           12            2            3

Answer: The <NA>s in var_5 are killing you. Student 30135 has a complete row of data, so this works:
> new.data <- student.data.m[student.data.m$student_id == 30135, ]
> predict(model.m, newdata = new.data)
state_1      state_2      state_3
3.980402e-01 5.812384e-09 6.019598e-01

Consider using the mice package (on CRAN) for imputation of those missing values, or dropping var_5 if it's not that theoretically important.
Comment: Ben, thank you very much for that answer! I wouldn't suppose this to be a reason, so your suggestion is of a great help for me!


HTTP status code always returning 200 from HttpServletResponse

Question: I'm using Spring AOP Aspect class to log request and response.
Below is my code
@AfterReturning(pointcut = "execution(* com.tr.dco.fp.ipam.controller..*.*(..))", returning = "retVal")
public void logAfter(JoinPoint joinPoint, Object retVal) {
    HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
}

Here, on successful response, I am always getting 200 status code even though the status code is 201 or any other.
Answer: Adding plain AspectJ aspect afer controller method is a bad idea. There are other pieces of Spring Web framework that can change the web response e.g. result code might be set with @ResponseStatus annotation.
You have to become part of the Spring Web processing logic e.g. by registering a HandlerInterceptorAdapter. Take a look at Spring - Modify response headers after controller processing to see how to do it.
Alternatively you can write a JavaEE filter before Spring's DispatcherServlet.
Answer: You can use it this way:
  @AfterReturning(pointcut = "execution(* com.tr.dco.fp.ipam.controller..*.*(..))", returning = "result")
  public void afterReturning(final JoinPoint joinPoint, final ResponseEntity<?> result) {

    data.put("status_code", result.getStatusCodeValue());
  }


perl calling 'ssh -f -N' .. should I use Net::Expect, system or exec?

Question: I'm trying to automate a process with perl that will basically telnet into one system, "ssh -f -N" into two more and finally open a screen/tmux session and run sshuttle, then disconnect from screen/tmux.
If I am going to be running ssh with the "-f -N" options - is this best handled through Net::SSH::Expect, Net::Expect, system or exec?
I'm concerned with Net::SSH or Net::Expect as I'm not sure of a few things, first, how can I get Net::Expect/SSH to recognize that the SSH session is successful?  That is to say, when I program Net::Expect and spawn the ssh session with those parameters, and subsequently login, there is no more output for 'expect' to see.  I'd like to rely upon something other than the expect timeout.
Does that make sense?  I'm not sure I'm phrasing my question clearly.
I'm also not sure how perl will behave if I spawn the ssh session that goes into the background.  Will my perl program never exit?  Waiting for the backgrounded SSH sessions to end?
It is because of this last question that I ask the additional questions of whether I should be considering the use of system or exec calls over Net::Expect.
Comment: Does the perl code do any other processing aside from running the external commands? This sounds better suited to a shell script than a perl script.
Comment: The code does not do much other than run external commands.  I would typically use a shell script for this, but needed something like expect to pass the passwords to ssh.  That, along with trying to learn more perl, is why I am trying to use Net::Expect.
Comment: and these particular hosts do not allow me to use rsa keys for passwordless login.
Answer: You could use Net::OpenSSH, which can integrate with Expect if needed.
Edit, Ugh, I missed that you were relaying it through a telnet session. Ignore the above.
Comment: So how could I automate a couple of SSH sessions w/o using RSA keys that would also somehow survive the initial script upon exit?

I can't use shell scripting w/o RSA keys, as far as I'm aware, as I don't know how to pass the passwords to the SSH login prompts.

I can't seem to use perl Expect as it hard_closes the ssh sessions when the perl script ends.  I suspect that the real TCL based expect would do  the same.


How to get Sun X4100 Firmware updates from Oracle?

Question: This document directs how to get there, but it is for the old Sun website. http://download.oracle.com/docs/cd/E19569-01/820-1188-12/core_ilom_firmware.html#50413605_67571

Download the flash image .ima file using these steps:

Naviage to http://www.sun.com/download/
Locate the Hardware Drivers section.
Click the X64 Servers and Workstations.
Click the link for the Integrated Lights Out Manager (ILOM) Server
  software release version that you want.

...

Under the new Oracle site, how do I get to the X4100 firmware updates?
Answer: Oracle insists on a "support identifier" and matching company name before downloading anything from them.  There is no apparent way to get this, so it seems that they effectively block further support for old Sun hardware unless you're willing to pay to be an Oracle hardware reseller.  :-(
The easiest way to get past this problem is to ask around for someone that does have access to the Oracle's support site.  Technicians or Engineers working for corporations that have support contracts for Sun/Oracle servers should be able to help you (if they want to!)
From the page

To find your download on My Oracle Support:

List item
Sign in to My Oracle Support.
Click the "Patches & Updates" tab.
In the "Patch Search" box on the left side, select "Product or Family (Advanced)" search.
Enter a partial product name for "Product is" - a list of matches will be displayed. Select the product of interest.
Select one or more "releases" in the "Release is" drop down and close the pop-up window.
At the bottom of that window click on the Search button and a list of product downloads (listed as patches) will be displayed. Select the
  Patch Name of interest. This will take you to the Download Page.

If, on the Download Information Page, you get the message "You do not
  have permissions to download this Patch...", see How Patches and
  Updates Entitlement Works to help you determine the reason.
Answer: Here is the Oracle firmware page: http://www.oracle.com/technetwork/systems/patches/firmware/release-history-jsp-138416.html


Qt: QMenu with translucent background

Question: I use Windows and I want to set a style sheet to a QMenu to give it a translucent background. In order for that to work, I first set the FramelessWindowHint, then I set the WA_TranslucentBackground attribute. Then I set my style sheet and display the menu with the popup method. It is drawn correctly, but it behaves strangely: As soon as it has the FramelessWindowHint, it is always visible (even before calling the popup() method). It does not hide itself anymore after one of its entries has been clicked.
Here is a minimalistic example:
#include <QApplication>
#include <QMenu>
#include <QPoint>
#include <QCursor>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMenu menu;
    menu.addAction("about", &a, SLOT(aboutQt()));
    menu.addAction("exit", &a, SLOT(quit()));
    menu.setWindowFlags(Qt::FramelessWindowHint);
    menu.setAttribute(Qt::WA_TranslucentBackground);
    menu.setStyleSheet("QMenu{background:rgba(255, 0, 0, 50%);}");
    menu.popup(QCursor::pos());
    return a.exec();
}

Answer: menu.setWindowFlags(menu.windowFlags() | Qt::FramelessWindowHint);

should solve your problem. Now you are clearing all flags already set by Qt.

Qt: QMenu with translucent background


Maximization using partial derivatives?

Question: A trough is to be formed by bending up two sides of a long metal rectangle so that the cross-section of the trough is an isosceles trapezoid. If the width of the metal sheet is 2 meters, how should it be bent to maximize the volume of the trough
I honestly do not know how to start. I know that I have to do partials but I do not know...
Comment: Your question is phrased as an isolated problem, without any further information or context. This does not match [many users' quality standards](http://goo.gl/mLWc8), so it may attract downvotes, or be put on hold. To prevent that, please [edit] the question. [This](http://goo.gl/PlJyVQ) will help you recognise and resolve the issues. Concretely: please provide context, and include your work and thoughts on the problem. These changes can help in formulating more appropriate answers.
Answer: You only have to maximise the cross sectional area not the volume.
If the sides of the trapezoid are $x$ and they are bent to an angle $\alpha$ from the vertical then the cross-sectional area is given by $A(x,\alpha)=x^2 \cos (\alpha)\sin(\alpha)+(2-2 x)x \cos(\alpha)$.
From here just calculate the partials w.r.t. $x$ and $\alpha$ and solve.
I found it easiest to use the partial wrt $x$ to get an expression for $\sin(\alpha)$ in terms of $x$, and then substitute this back into the partial wrt $\alpha$ which gives you an equation in $x$ alone.

Maximization using partial derivatives?


Skip regularization for some features for generalized linear models in SkLearn

Question: I'm working on a time-series forecasting project, where we found regularized linear models to work well. But we have a feature that has small but important impact for some period. We want to be sure this column is not regularized to 0.
Is there any way to skip regularization for generalized linear models, such as Lasso, Ridge, ElasticNet in the SkLearn library? I can't find the option in the documentation. What would be a good workaround if this option is not available?
Comment: I don't know if it's possible in sklearn, but it definitely is in the R library `glmnet`.
Answer: You can completely skip regularization for all features using LinearRegression, or setting alpha=0 in Ridge, Lasso or ElasticNet.
You can't skip regularization for only a subset of features with scikit-learn's API.

Note that using Lasso leads to many coefficients to exactly zero, whereas Ridge leads to small values, but with almost no zeros, so more features are kept in the game. ElasticNet is a mix of both regularization, balanced with l1_ratio parameter. So try using Ridge, or decrease the regularization parameter alpha.
As an experimental workaround, you may also try to balance regularization between features by scaling the features of interest (and obviously use normalize=False):
Multiplying a feature by 0.1 should increase the corresponding coefficient by 10 (with no regularization), so the regularization should be stronger on this feature. I am curious if it yields the desired effect.


Existence of a bijection $f:ω_1×ω_1→ω_1$ for the first uncountable ordinal $ω_1$?

Question: May I ask why "Let $ω_1$ be the first uncountable ordinal. There's a bijection $f:ω_1×ω_1→ω_1$. " Thanks!
Comment: Because "[the following relation well-orders $\omega_1\times\omega_1$ in type $\omega_1$ and thus indirectly provides the desired bijection. For $\langle\alpha,\beta\rangle,\langle\gamma,\delta\rangle\in\omega_1\times\omega_1$ define $\langle \alpha,\beta\rangle \preceq\langle\gamma,\delta\rangle$ iff one of the following holds: (1) $\max\{\alpha,\beta\}\lt\max\{\gamma,\delta\}$; (2) $\alpha=\gamma$ and $\beta\le\delta\lt\alpha$; (3) $\beta\lt\alpha=\delta\ge\gamma$; or (4) $\beta=\delta$ and $\alpha\le\gamma\le\beta$](http://math.stackexchange.com/a/85424)."
Comment: See my answer here: http://math.stackexchange.com/questions/54892/about-a-paper-of-zermelo/54904#54904
Comment: @t.b.: This might be a stupid question. But, does that implies $\omega_1\times\omega_1=\omega_1$ ?
Answer: In http://caicedoteaching.wordpress.com/2009/01/27/580-some-choiceless-results-3/ (see section 6) I show more generally that there is a canonical bijection between $\alpha$ and $\alpha\times\alpha$ for any infinite ordinal $\alpha$. This does not use choice, and does not require that $\alpha$ is a cardinal or even a limit ordinal, so in particular is more general than the argument using Goedel pairings (The ordering indicated by t.b.).
(I believe this can be found in a book by Levy, but don't know who it is due to.)
Some additional details are in the link above, but the idea is to use Cantor's normal form: Any ordinal $\alpha$ can be written in the form $\omega^{\beta_1}n_1+\dots+\omega^{\beta_k}n_k$ where the $n_i$ are positive integers and the $\beta_i$ are strictly decreasing, with $\beta_1>0$ as long as $\alpha$ is infinite.
Fixing a bijection $p:\omega\times\omega\to\omega$ with $p(0,0)=0$ (and $p$ recursive, if you want), we use the normal form to define an injection $G:ORD\times ORD\to ORD$, where $ORD$ is the class of all ordinals:
Given $\alpha,\beta$, write them as $$ \alpha=\omega^{a_1}m_1+\dots+\omega^{a_k}m_k $$ and $$ \beta=\omega^{a_1}l_1+\dots+\omega^{a_k}l_k $$ where the $a_i$ are strictly decreasing and the $m_i$ and $l_i$ are natural numbers (with 0 allowed). Then set $$G(\alpha,\beta)=\omega^{a_1}p(m_1,l_1)+\dots+\omega^{a_k}p(m_k,l_k).$$
Note that $G$ restricts to an injection of $\alpha\times\alpha$ to $\alpha$ whenever $\alpha$ is indecomposable, i.e., of the form $\omega^\gamma$ for some $\gamma$.
The Schroeder-Bernstein theorem has an explicit proof: From bijections $f:A\to B$ and $g:B\to A$ we can exhibit a bijection between $A$ and $B$. In particular, since obviously there is an injection from $\alpha$ into $\alpha\times\alpha$, we now have an explicit bijection between any indecomposable $\alpha$ and $\alpha\times\alpha$.
For an arbitrary infinite ordinal $\beta$, note that $\beta$ can be uniquely written as $\omega^\gamma\cdot n+\delta$ for some positive integer $n$ and ordinals $\delta<\omega^\gamma$.
From the above (and induction on $n$), there is an explicit bijection between $\omega^\gamma$ and $\omega^\gamma\cdot n$, and there is also an obvious bijection between $\omega^\gamma +\delta$ and $\delta+\omega^\gamma=\omega^\gamma$.
From this, we can easily get a bijection betwen $\beta$ and $\beta\times\beta$.


Where does the extra "nscanned" comes from in this .explain()?

Question: I have a collection of bookings. Every document has these fields:
_id: ObjectID,
client: ObjectID // A reference to the "owner" of the service.
start: nr,
end nr

I insert 15 dummy document (with the same id on client). the first five start at 1 and ends at 2, the next five starts at 3and ends at 4, the last five starts at 5and ends at 6.
I then create the following index: db.bookings.ensureIndex({client: 1, start: 1, end: 1})
For these queries:
db.bookings.find({client: anID, start: {$gte: 1}}).explain()
db.bookings.find({client: anID, start: {$gte: 3}}).explain()
db.bookings.find({client: anID, start: {$gte: 5}}).explain()

I get the these expected results in order:
"n": 15, "nscannedObjects": 15, "scanned": 15
"n": 10, "nscannedObjects": 10, "scanned": 10
"n": 5, "nscannedObjects": 5, "scanned": 5

Which is expected and totaly fine.
But when doing these queries:
db.bookings.find({client: anID, start: {$gte: 1}, end: {$lte: 2}}).explain()
db.bookings.find({client: anID, start: {$gte: 3}, end: {$lte: 4}}).explain()
db.bookings.find({client: anID, start: {$gte: 5}, end: {$lte: 6}}).explain()

I get these confusing results in order:
"n": 5, "nscannedObjects": 5, "scanned": 7
"n": 5, "nscannedObjects": 5, "scanned": 6
"n": 5, "nscannedObjects": 5, "scanned": 5

Why do I get extra (5 is expected) scanned documents in the first two queries? Is there something wrong in my index or query?
Answer: The nscanned count can be higher than the nscannedObjects count because MongoDB scanned more than one index entry for a particular object/document and then de-duplicated.  This is actually covered as part of the explain example in the docs.  With compound keys, it can also be the case that the index entry is scanned because it matches on the leftmost field (for example) but because it doesn't match on one of the other fields, the document is skipped and not scanned.
There is an excellent post here with visuals of the btree traversal to help understand how this can happen.
As an aside, the new version of explain in version 3.0 will replace these fields with totalKeysExamined and totalDocsExamined.  They represent the same thing, but the names are a little more indicative of what they actually represent.
Comment: well, it's a very basic measure of the efficiency of the query - if it diverges then you may need to rethink the order of the compound index, or use a different index, but as a rule of thumb I wouldn't worry about 10% differences (given larger values, a 1 doc difference for 5 `nscanned` is not something to worry about usually).  I updated the answer with a post that it took me a while to find that does an excellent job explaining this.
Comment: Ok. It there some kind of rule of thumb when `nscanned` are "to much" in comparison with `nscannedObjects`? Or can this differ alot in certain queries and still be correct?
Answer: In the case of querying on two ranges without relation, like I do above. (start & end) I found this great tip about "closing" the first range in the query. A query will perform potetially much better with a closed first range.


Using different transformations and regressions in the same analysis

Question: I am carrying out linear regressions with quite different independent variables and I am unsure how to treat each appropriately without compromising my ability to compare the final $R^2$ values.
I have 150 individuals in the analysis, each with 6 proxy measures for body size, as well as the actual body size (my DV). All individuals will later be analysed altogether, but they are made up of 3 species (n=50 each) and the focus of my analysis is to look at them separately (to see if there is one proxy for body size that suits them all, or if each species has its own that's most suitable).
Within each species, some of my variables show a normal distribution and some of them don't, and some of them show clear heteroscedasticity while others are homoscedastic.
I understand that to carry out a linear regression I need normally distributed residuals, but $\frac{5}{18}$ of my independent variables are not normal (or even approximately normal) and there is no one transformation that is suitable for them all. I have been told by a colleague that each variable should be considered independently and transformed to normality accordingly.
Ultimately I am concerned that having a combination of both transformed (with various transformations) and non-transformed data as well as using a traditional regression and a weighted regression, will make it difficult to compare my final $R^2$ values.
Comment: Why do you want to compare the final R2 values?  R2 do not only depend on the quality of fit, but also on the distribution of the predictors, so is not a measure of quality of fit. It is probably more natural to compare residual variance.  Transforming predictors should not be necessary, maybe transforming the response ....   Comparison of different models will be easier if the response have a common scale, so maybe search for a compromise transformation ...
Answer: There are no distributional assumptions on your independent variables in linear regression. Only the dependent variable is assumed to be normally distributed (with constant variance, homoscedastic), conditional on the values of the covariates in the model.
Based on the information you give I would fit separate models for every species and compare them to see if there is a common best proxy.
Comment: Thank you for your help. Are the R2 values generated through OLS and WLS then comparable?
Comment: @Joanne Which weights would you use? The $R^2$ are comparable as long as you use the same dependent variable, but better use an adjusted version to account for different numbers of independent variables.
Comment: I used my unstandardised predictor values to calculate my weights. I am using the same dependent variable throughout.
Comment: @Joanne I need more explanation with respect to these weights, they are not self-evident to me.


Javascript read a text file being uploaded through a form

Question: I want to do some basic calculations based on a text file. However since it's for a client it needs to be straight forward. Is it possible to do these calculations purely in the client. If somebody selects a file to upload through a form. Can I instead capture that through Javascript and process it that way?
Answer: Matti is correct, with HTML 4 you're out of luck.
However, with HTML 5 this is made possible by using the FileReader API. Currently support for this feature is limited to very recent versions of Chrome, Firefox and Opera. A similar feature exists in older versions of Firefox and this can also be done in older versions of Internet Explorer by using ActiveX. I would imagine that Google Gears also would allow this although I haven't looked into it.
The Javascript to do this would look like:
<code>$( "#files" ).change( function( event )
{
    var reader = new FileReader();
    reader.onload( e ) { /* handle a file being loaded. contents in event.result */ };
    reader.onerror( e ) { /* handle an error during the file reading */ };

    var files = event.target.files;

    for( var i = 0; i < files.length; i++ )
        console.log(reader.readAsText( files[i] ));
});
</code>
Where #files is simply:
<code><input type="file" id="files" multiple />
</code>
Comment: To update this, HTML 5 File Upload it is supported in IE 10 and above .
Comment: I don't know if things have changed since this was written but I see results in event.target.result rather than event.result.
Comment: Where is the text u have read from file?Is it stored in some file? while iterating where are you using 'i' ?


Comando para abrir o terminal em pasta determinada

Question: Estou produzindo alguns scripts para preparar o ambiente de trabalho, e queria já deixar o terminal em uma pasta pré-definida. Como fazer isso?
Já tentei usando cd ./pasta_selecionada e cd pasta_selecionada, mas ambos voltam para a pasta original após o script ser executado.
Estou usando OSX e zsh, mas o script, até o momento, é compatível com Bash também.
Answer: Supondo que seu script seja arquivo.sh, se você rodar
$ ./arquivo.sh

o conteúdo será executado numa nova shell, na qual existe, também, uma variável $PWD com o diretório corrente do script. Ao terminar, essa variável é extinta com a shell.
Já se você usar o comando builtin (na shell atual) source ou ., o conteúdo é executado na shell atual, permanecendo o diretório corrente do script na variável $PWD:
$ source ./arquivo.sh


A maximal ring wrt Domination is a valuation ring.

Question: Let $K$ be a field, and let $A$ be local subring of $K$, which is maximal wrt domination, ie for any local ring $B\subset K$, with $\mathfrak{m}_B\cap A=\mathfrak{m}_A$, we have $A=B. \ $ Then $A$ is a valuation ring.
My idea: assume there's $x \in K$ such that $x,x^{-1}$ are not in $A$. Consider $A[[x]]$, the ring of formal power series in $x$. It has unique max ideal $\mathfrak{m}_A+(x)$, so is local. Then taking $B=A[[x]]\cap K$ contradicts the maximality of $A$. I'm not $100\%$ sure that $B$ is a local ring, though.
Answer: $\newcommand{\mm}{\mathfrak{m}}$Unless you have some notion of ring being "complete", it doesn't make sense to talk about the power series ring $A[\![x]\!]$ sitting inside $K$. (How do you make sense of $1 + x + x^2 + \cdots$?)

Here's how we can do it instead.
Claim. $A$ is normal (i.e., if $x \in K$ is integral over $A$, then $x \in A$).
Proof. Let $x \in K$ be integral over $A$. Then, $R = A[x]$ is an integral extension of $A$. Thus, there exists a maximal ideal $\mm_R \subset R$ that contracts to $\mm_A$. But then, $B = R_{\mm_R}$ is a local ring whose maximal ideal contracts to $\mm_A$. By maximality, we must have $A = R = B$. In particular, $x \in A$. $\Box$
Now we are ready to show that $A$ is a valuation ring. Let $x \in K \setminus A$. We wish to show $x^{-1} \in A$.
By hypothesis, the subring $R = A[x]$ is larger than $A$. This implies that $\mm_A R = R$. (Why?)
Thus, we can write
$$1 = a_0 + a_1 x + \cdots + a_n x^n$$
for some $a_i \in \mm_A$.
Now, note that $1 - a_0$ is a unit (in $A$) since it is outside $\mm_A$. Thus, we can rearrange the above to get
$$\frac{1}{x^n} = \frac{1}{1 - a_0}\left(\frac{a_1}{x^{n - 1}} + \cdots + a_n\right).$$
This shows that $x^{-1}$ is integral over $A$ and hence, $x^{-1} \in A$.

Here is how we can answer the "(Why?)": Note that we have the containment $\mm_A R \subset (1)$. This is containment as ideals of $R$.
Now, given any maximal ideal $\mm_R \subset R$, we see that $R_{\mm_R}$ is a strictly larger local ring than $A$. Thus, the maximal ideal of $R_{\mm_R}$ cannot contract to $\mm_A$. This means that $$\mm_A R_{\mm_R} = 1 R_{\mm_R}.$$
Thus, we have shown that the ideals $\mm_A R$ and $(1)$ agree at all localisations at maximal ideals. This is enough to show that they are equal to begin with.