Itext: Show paragraph at absolute position

Question: Is there a way to position a paragraph absolutely, in a way that also works when a list is added to the paragraph?
A google search shows that I should use ColumnText, but I can't get that to work if there is a list in the paragraph. It simply shows the list items next to each other on the same linie. Here is my test program:
<code>        PdfWriter writer=PdfWriter.getInstance(document,new FileOutputStream("/tmp/output.pdf"));
        document.open();
        ColumnText ct = new ColumnText(writer.getDirectContent());
        ct.setSimpleColumn(0,0,300,300);
        Paragraph p=new Paragraph();
        List list=new List();
        list.add(new ListItem("First item"));
        list.add(new ListItem("second item"));
        list.add(new ListItem("third item"));
        p.add(list);
        ct.addElement(p);
        ct.go();
        document.close();
        writer.close();
</code>
Answer: I looked in the changelogs of iText and I discovered that this was fixed in iText 5.2.1, released on March 31st, 2012. That's over a year ago. Please upgrade to the latest version and the problem will disappear.
Note that all 5.2.x versions were removed from SourceForge because they contained a bug that occasionally produced PDFs that weren't compliant with ISO-32000-1. Based on the description of your problem, I know that you're using a version of iText that is even older than the 5.2.x series, so you definitely need to upgrade.
Comment: Sorry, I can't. We currently don't have the 1.615€ needed for a server license -(
Comment: Will you be profitable in the future? If so, mail sales at iText and ask for Garry. Refer to this post and he can make you a start-up deal.
Answer: A different way to organize you content in PDF file is that you can use PdfPTable . first write list content into the table using loop instruction and then define the position of that PdfPTable in your output pdf file.
Answer: You can use this function:
<code>private void PlaceChunck(String text, int x, int y) {
    PdfContentByte cb = writer.DirectContent;
    BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    cb.SaveState();
    cb.BeginText();
    cb.MoveText(x, y);
    cb.SetFontAndSize(bf, 12);
    cb.ShowText(text);
    cb.EndText();
    cb.RestoreState();
}
</code>
see also: itext positioning text absolutely;


as3 air and screen size dection issue on devices

Question: I have worked so hard on an app that displays perfectly on my Galaxy Note 3. However, it does not display right on iPhone and also one other Droid I tested on. My issue is with addChild() and then resizing it to fit the screen. For some reason when I add the Background (addBG(); The screen size works but if I load addChild to the BG, this works great on my Note 3 but not on the iPhone or another android device.
My issue is with the screenX, screenY var I created. They seem to be different for devices. Or it is a "rendering order issue" I am not sure. Would be so greatful for the help to fix this issue so it looks great on each phone. I have read some tuts on this subject but they are confusing. I think my code is close, I hope, and maybe it just needs a tweak. !
Here is a shot of the about screen on an IPhone. See the white does not fit the whole screen.
and Here is a shot from my Droid Note 3.
Declared Vars in a package:
This is not my full code, of course but only that which I believe is relevant.
public var screenX:int;
public var screenY:int;

   public function Main()
    {
        if (stage)
        {

            setStage();
            addBG();
        }
    }

    public function setStage()
    {
        stage.scaleMode = StageScaleMode.NO_SCALE;
        stage.align = StageAlign.TOP_LEFT;

        if (flash.system.Capabilities.screenResolutionX > stage.stageWidth)
        {
            screenX = stage.stageWidth;
            screenY = stage.stageHeight;
        }
        else
        {
            screenX = flash.system.Capabilities.screenResolutionX;
            screenY = flash.system.Capabilities.screenResolutionY;
        }
    }

This works: addBG();
            public function addBG()
    {
        theBG = new BG();
        addChild(theBG);
        theBG.width = screenX;
        theBG.height = screenY;
    }

This does not: addAbout();
public function addAbout()
        {

            About = new viewAbout();
            About.width = screenX;
            About.height = screenY;
            theBG.addChild(About);
            TweenMax.fromTo(About,1, {alpha:0}, {alpha:1, ease:Expo.easeOut}  );

        }

UPDATE: And yet another more complex load is also called from a button and having the same issue. I hope you see the logic of what I am trying to do. First set the BG to the device then load and resize content to fit proportionally into the BG I loaded. The reason being, the BG will be distorted to different proportions and that's ok, but the content can not. So here is the example that loads content into the BG and then content into that container.
public function addRosaryApp()
        {
            Rosary = new viewRosaryApp();
            Rosary.width = screenX;
            Rosary.height = screenY;
            theBG.addChild(Rosary);
            TweenMax.fromTo(Rosary,1, {alpha:0}, {alpha:1, ease:Expo.easeOut}  );

            contentRosary = new contentRosaryApp();
            theBG.addChild(contentRosary);
            contentRosary.width = screenX;
            contentRosary.scaleY = contentRosary.scaleX;
            contentRosary.x = screenX/2 - contentRosary.width/2;
            contentRosary.y = menuBanner.height;
        }

Comment: Are you getting the correct values for iphone if you trace your screenX/screenY?
Comment: Yes, its like 600 by 900 or something I forget. Its an iphone 4s. It is working for the BG and fits it niecly to screen. So maybe its a loading issue I am thinking or the iPhone is not always seeing the values of screenX and ScreenY
Comment: You should use stage.fullscreenWidth, fullscreenHeight. Does it make any difference?
Comment: I will try. :) And get back to you.
Comment: The load about worked but I added more examples above because I also need to load content on the page and this can not be stretched. It seems the screenX is still not working. :(
Answer: Have you tried adding the child to stage first, and then setting the size? That's the only difference I can see between addBG and addAbout
        About = new viewAbout();
        theBG.addChild(About); // do this first
        About.width = screenX; // then set width
        About.height = screenY; // and height

Comment: iF it is the same variable, it will 'move' it... meaning, it will "reparent" it. However, if it is still attached to another parent, it will throw an exception so you will need to handle it safely
Comment: you could do something along the lines of; `if(menuBanner.parent != null){menuBanner.parent.removeChild(menuBanner);} xxxxxx.addChild(menuBanner);`
Comment: I think you are on to something. I noticed that it is being scaled because the content is being scaled. So if its squeezed, the newly added content will also be squeezed. To fix it I think I can simply do a addChild(menuBanner); over the top, and not add the content inside the BG, which is what I really need. My question is, if I do that will I be adding a new instance or will I simply be moving it to the top, which is what I want?
Comment: ok that should work then. However, if I did need it I don't know how to handle it safely.
Answer: I think your problem may have to do with either of several things.
My findings so far from my own devices (an Ipad Air, an iphone 4S, an LG G2 and an Acer Iconia A500) is that the only device size that's being reported correctly at all times is the stage.fullScreenWidth and the stage.fullScreenHeight
1st, Android reports Capabilities.screenResolutionX as the LONG side of your Android device.
IOS devices however report the SHORT side of your device to Capabilities.screenResolutionX.
2nd, stage.stageWidth and stage.stageHeight seem to report the wrong size on both Android and IOS.
And in fact if you check these before and after setting stage.scaleMode and stage.align then the values will differ completly (although Android will show it almost correctly).
My suggestion is that you set a base size (width and height) for your app and then compare it with the actual stage.fullScreenWidth and stage.fullScreenHeight reported by the device. That way you can get some nice scaleratios to use to scale your displayobjects. That's what I'm currently on my apps and it scales just fine :-)


AIC Calculation using log likelihood

Question: I have a dataset that has 40 experimental observations of cells' activity, $n=40$, I tested several models using each of these samples. The model can only explain one cell at a time due to variability between the cells. Hence, for each model I have 40 values of log-likelihood and 40 parameter sets for each model.
I thought that this is how I should calculate the AIC:
$AIC = -2\times \sum_{i=1}^n log\mathcal{L}_i + 2\times k $
where $k$ is a number of model parameters.
But because the models do not explain all the data with one set of parameters, and
 each cell ends up with their own best parameter set, I was wondering I should be dividing the log likelihood by $n$? I.e.:
$AIC = -2\times \frac{1}{n} \sum_{i=1}^n log\mathcal{L}_i + 2\times k $
Comment: That's not the same as the formula in your question though.
Comment: How do you get the second formula?
Comment: Not sure if I understand the question? If you are asking what made me think it's the second formula it was this: AIC = n*log(residual sum of squares/n) + 2K which is written here: https://pypi.org/project/RegscorePy/
Comment: I was wondering if you could explain why? I thought 'residual sum of squares' in the formula I linked would be equivalent to the sum of the log likelihood values of all my samples?
Comment: Ok, sorry, I only just noticed that the formula in the link is different to my second formula... But I am still confused which one of the ones I wrote I should be using, if I should be dividing the sum of log(L) by sample number or not, I was wondering if you have any suggestions?
Comment: If you edit your question so that it matches your reference, it's possible that it may not turn out to be such a mystery.
Comment: I rephrased the question, hopefully that describes my problem bit better... Thanks for your input.
Comment: It's unclear what the motive would be for dividing that specific term by n. Where did that come from?
Comment: I think my reasoning behind it is because the model does not describe data of the population, it can only be fit to one sample at a time, hence I fit the model separately to each one of my 40 sample so in a sense I end up with n*k parameters. So I thought by dividing the log likelihood by n, I get an idea of the 'marginal' performance of the model. Do you think this is somehow flawed?
Comment: Ah I think I see; I will edit a little more into my answer.
Answer: The AIC is given explicitly in (for example) Akaike, 1974[1] (including in the abstract) as:

$^{-2 \log(\text{maximum likelihood}) + 2(\text{number of independently adjusted parameters within the model})}$

when you have independence of observations, this becomes your first form.
If you adjust AIC by shifting it, nothing of consequence changes (as long as the same shift is applied to every such term that is compared).
If you scale the entire AIC, that still allows less than or greater than comparisons but it's no longer an adjusted likelihood. People sometimes divide the entire AIC by $n$, which is like an average adjusted likelihood (and there can sometimes be good reasons to do this but it's not strictly AIC)
I see no justification whatever for dividing the first term by $n$ but leaving the second term alone; that changes the relative impact of the two terms and you are no longer doing what Akaike was doing.
In your case you're fitting multiple models to different samples. If you treat the samples as independent of each other, the original Akaike formulation works as is for this collection of models as long as you add the log-likelihoods and the parameters for each model. i.e. it's a model with as many observations as the total number of observations (assuming no overlap; these are supposed to be independent) and the number of parameters is the total number of parameters.
If you then decide to scale to some kind of average, you can do so (but as I mentioned earlier, it's no longer strictly AIC)
[1] Akaike, H. (1974),
"A new look at the statistical model identification",
IEEE Transactions on Automatic Control, 19 (6): 716–723,  doi:10.1109/TAC.1974.1100705, MR 0423716.
Comment: Thank you so much for taking the time to write this. I was just wondering the bit where you say "and the number of parameters is the total number of parameters", do you mean the second term in the equation should be $-2\times k \times n$ ?
Comment: Your notation is a little confusing to me so I have avoided it. You would add the number of free parameters in each model as long as they're all separate (no common parameters across those models). If you have a parameter $\theta_1$ in model A and another in model B, that's actually two parameters for the combined data $\theta_{1A}$ and $\theta_{1B}$. For example if I collect data for males and data for females and fit a pair of simple regression models, I have six free parameters ($\beta_{0F},\beta_{1F},\sigma_F,\beta_{0M},\beta_{1M},\sigma_M$).
Comment: But if I combine them into a single model (using indicators / dummies for sex and the interaction of that with the predictor / IV), I still have the four parameters for the two lines but only one $\sigma$ parameter, making 5 in all. In each case you just count how many "independently adjusted" parameters you estimate, like Akaike said in that little bit I quoted.
Comment: Ok, thank you so much, I think I might be slowly getting it. I'm just going to write this again just so that I am sure I am understanding what you're saying:
Comment: In my case I am recording data from 40 cells, in the end I would like to determine which model is the best at describing all the cells. So let's say I want to test two models using AIC, let's say both models have 5 parameters. I fit Model1 and Model2 to each of my 40 samples, so each sample has unique parameters, so I end up independently adjusting 40x5 parameters? So the penalising term becomes 2x40x5? and the first term in the equation will be the sum of the log likelihoods of all 40 unique parameter sets?
Comment: I just wanted to say thank you again for your help, made me think a lot - I think my comment from yday is still maybe off but your help has made me actually realise what my problem is, i.e. I am somehow trying to take an average of AIC to compare models and decide which one is the best to account for a population of cells. Really appreciate it!

AIC Calculation using log likelihood


Qual é a finalidade do simbolo :: no Java?

Question: Implementei um método que soma todos o números de uma lista do tipo List<Integer> da seguinte forma:
int soma = 0;

for (Integer num : numeros)
    soma += num;
return soma;

Porém, a IDE NetBeans sugeriu para que eu substitui-se a rotina acima pela seguinte rotina abaixo:
int soma = 0;
return numeros.stream().map((numero) -> numero).reduce(soma, Integer::sum);

Sei que a rotina acima utiliza novas características do Java 8, contudo, fiquei em duvida a respeito do simbolo :: usado no método reduce(), não sei se é algum tipo de operador, eu gostaria de saber qual é a sua finalidade?
Comment: Até que consegui entender a chamada em lambda, só não sei te explicar, senão respondia :/
Comment: @DiegoF se quiser, pode complementar minha resposta. Eu estou pelo celular agora e não vou conseguir melhorá-la. Até porque eu desconheço o método `reduce`.
Comment: @jbueno é um método da classe Stream, que por sinal ainda é obscura pra mim, já que é recente.
Comment: Aparentemente é uma boa explicação do uso, se meu inglês fosse bom, eu até resumiria o conteudo: http://netjs.blogspot.com.br/2015/06/method-reference-in-java-8.html
Answer: O operador :: foi adicionado ao Java 8 e faz parte de expressões que referenciam métodos (Method Reference Expressions).
As referências a métodos (Method References) funcionam como complemento aos lambdas.
Um lambda é como um trecho de código que você pode passar como argumento para um método. Só que às vezes esse trecho de código se repete ou já está implementado em algum método, portanto, você pode simplesmente referenciar um método qualquer.
Segundo a documentação, existem basicamente quatro tipo de referências a métodos:
1. Método estático
public static void medirTempo(Runnable rotinaDemorada, Supplier<Long> timestampSupplier) {
    long inicio = timestampSupplier.get();
    rotinaDemorada.run();
    System.out.format("Tempo: %d ms", timestampSupplier.get() - inicio);
}

Runnable é uma interface funcional que abstrai uma tarefa a ser executada. Ela não recebe nem retorna valor. Supplier é uma interface funcional cuja execução retorna um valor de um certo tipo, Long neste caso.
Dada uma rotina que demora:
Runnable rotinaDemorada = () -> {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
};

Podemos chamar o método assim:
medirTempo(rotinaDemorada, System::currentTimeMillis);

Coloquei o lambda numa variável separada para fins didáticos, mas poderíamos passar ele diretamente como parâmetro.
Assim, passamos um lambda e depois o método estático currentTimeMillis da classe System como argumentos. Quando a rotina de medição executa, timestampSupplier.get() é exatamente o mesmo que System.currentTimeMillis(). O método estático é chamado duas vezes, no início e no fim, onde a diferença de tempo é então impressa no console.
Note que System.currentTimeMillis() não recebe parâmetros e retorna um número longo, portanto é compatível com a interface funcional. O mesmo serve para o lambda que é atribuído para a variável Runnable, isto é, ele não tem parâmetros e não retorna valor.
2. Método de uma instância específica
Exemplo:
public int tamanho(Supplier<Integer> s) {
    return s.get();
}

Supplier é uma interface funcional cuja execução retorna um valor de um certo tipo, Integer neste caso.
Podemos chamar o método assim:
String s = "Hello, Instance Method!";
int t = tamanho(s::length);

Aqui passamos o método length do objeto s como argumento. Quando a rotina executa, s.get() é exatamente o mesmo que s.length(). O tamanho da String é retornada.
Note que s.length() não recebe parâmetros e retorna um inteiro, portanto é compatível com a interface funcional.
Outro ponto interessante é que você poderia passar List.size () para a rotina com a interface funcional. Portanto, o mecanismo de compatibilidade de interfaces permite um outro nível de programação genérica.
3. Método de uma classe aplicado a qualquer instância
Exemplo:
public int tamanho(Function<String, Integer> f) {
    return f.apply("Hello, Class Method!");
}

Function é uma interface funcional que recebe um valor do tipo T (String) e retorna um valor do tipo R (Integer).
Podemos chamar o método assim:
int t = tamanho(String::length);

Aqui passamos o método length da classe String como argumento. Quando a rotina executa, f.apply("...") é exatamente o mesmo que chamar o método "...".length(), ou seja, na String passada como argumento para apply.
Note que no exemplo anterior, a interface funcional não sabe a qual objeto o método pertence. Neste exemplo, podemos chamar o método em qualquer instância do tipo.
4. Referência a um construtor
Exemplo:
public String newHello(Function<String, String> f) {
    return f.apply("Hello, Constructor!");
}

Function é uma interface funcional que recebe um valor do tipo T (String) e retorna um valor do tipo R (também String).
Podemos chamar o método assim:
String s = newHello(String::new);

Aqui passamos o construtor String(String) como argumento. Quando a rotina executa, f.apply("...") é exatamente o mesmo que instanciar a String assim: new String("...").
Na prática, não existe muita diferença entre esta abordagem e a anterior, exceto que usar construtores facilita a utilização de objetos imutáveis como String, BigDecimal, Integer e outros ao usar a API funcional.
Sobre a transformação do NetBeans
O código gerado pela IDE não é bom:
int soma = 0;
return numeros.stream().map((numero) -> numero).reduce(soma, Integer::sum);

Mapeamento desnecessário
Não sei porque a IDE gerou uma chamada ao método map. Ele serve basicamente quando você quer transformar todos os valores de um stream em alguma outra coisa, gerando um novo stream. Neste caso é totalmente desnecessário.
O código abaixo gera o mesmo resultado:
int soma = 0;
return numeros.stream().reduce(soma, Integer::sum);

Variável desnecessária
A variável soma não é necessária, já que define apenas um valor inicial.
O código abaixo gera o mesmo resultado:
return numeros.stream().reduce(0, Integer::sum);

Alternativa: IntStream.sum()
A API de Streams do Java 8 possibilita executar uma determinada operação numa sequência de valores e agregar o resultado de alguma forma. Isto é análogo às operações de agregação do SQL como SUM e AVG.
Um dos exemplos é o método reduce que possui os parâmetros:

Valor inicial
Método de agregação

Entretanto, existem atalhos para as operações comuns em números tais como na classe IntStream que implementa sum, min, max, average e count.
Na documentação de sum, lê-se que que ela é equivalente a reduce(0, Integer::sum). Portanto, poderíamos reescrever o código assim:
return numeros.stream().mapToInt(Integer::intValue).sum();

Alternativa: array de primitivos
O matpToInt no exemplo anterior apmente é necessário porque o stream inicial não era do tipo IntStream.
Isso não seria necessário se ao invés de List<Integer> você tivesse um array primitivo int[].
Exemplo:
int[] numerosArray = new int[] { 1, 2, 3 };

Então o código para usar o sum seria apenas:
return IntStream.of(numerosArray).sum();

Comment: Eu estava esperando uma resposta sua *=)*
Comment: Só por curiosidade, por que `Supplier` no lugar de `IntSupplier`?
Answer: É uma nova característica da linguagem na versão 8. Chamasse Method Reference ou Referência de Método. O recurso fornece uma maneira de referenciarmos um método sem executá-lo. Além disso, está relacionado às expressões lambda porque, como elas, requer um contexto de tipo de destino composto por uma interface funcional compatível. Quando um Method Reference é avaliado, uma instância de uma interface funcional é criada. Essa definição bonita tirei do livro do Schildt
Um exemplo:
@Test
public void remove_null_from_list_java8_lambda () {
    List<String> strings = Lists.newArrayList(null, "www", null, "leveluplunch", "com", null);

    List<String> filterStrings = strings
        .stream()
        .filter(p -> p != null)
        .collect(Collectors.toList());

    assertEquals(3, filterStrings.size());

    // or
    List<String> filterStrings2 = strings
        .stream()
        .filter(Objects::nonNull)
        .collect(Collectors.toList());

    assertEquals(3, filterStrings2.size());
}

A linha .filter(Objects::nonNull) está chamando o método Objects.nonNull(Object) passando como parâmetro um objeto (o atual) da lista. Ou seja, declarou o método sem invocá-lo explicitamente.
O exemplo eu tirei daqui.
Mais informações no Tutorial java (em inglês).
Comment: Se eu lembrar algum exemplo melhor/mais didático eu atualizo a resposta.
Answer: Esse operador é chamado de Method Reference.
Em poucas palavras ele serve para referenciar o método sum da classe Integer. Neste caso, você está passando o método sum como segundo parâmetro do reduce.


Objeto Date() não funciona no Mozilla Firefox

Question: O meu código em Javascript têm um objeto Date, contudo este script só funciona no Chrome e no IE, sendo que no Firefox não aparece nada, porque isso acontece?
Código:

function relogio(elemento){

 var target = document.querySelector(elemento);
 var rel = new Date();
 var hora   = rel.getHours();
 var minuto   = rel.getMinutes();
 var segundo  = rel.getSeconds();

 if(hora < 10){
  hora = "0"+hora;
 }
 if(minuto < 10){
  minuto = "0"+minuto;
 }
 if(segundo < 10){
  segundo = "0"+segundo;
 }
 target.innerText = hora+":"+minuto+":"+segundo;
}
window.setInterval("relogio('#relogio')", 1000);

Também disponível no jsfiddle.
Por favor apresentem só soluções em Javascript.
Answer: A razão para o teu código não funcionar no jsFiddle é porque essa função relogio está a ser declarada dentro do onload do jsFiddle, e por isso não disponível no escopo global em que o eval do setInterval corre. Há outra pergunta com um problema parecido aqui: https://pt.stackoverflow.com/a/35268/129
Corrigindo isso fiacaria assim: https://jsfiddle.net/qkssh4s1/5/
Sugestão de melhoramanto:
function relogio(id) {
    var target = document.querySelector(id);
    function pad(nr) {
        return nr < 10 ? '0' + nr : nr;
    }
    return function() {
        var rel = new Date();
        var hora = rel.getHours();
        var minuto = rel.getMinutes();
        var segundo = rel.getSeconds();
        target.innerText = [hora, minuto, segundo].map(pad).join(':');
    }
}
window.setInterval(relogio('#relogio'), 1000);

jsFiddle: https://jsfiddle.net/qkssh4s1/4/


Service Reference and List/Array Types

Question: I have a problem that I really need solved in any way (changing data types is an option)
Assumptions:

Both Program and Service done in VS2008
I have complete control over both Program and Service
I have a program that is passing a "list" of info to the Service Reference

Example:
Program snippet:
IList<string> XMLList = null;
XMLList = new List<string>();

// fill my list up here...  mostly just an "array" of xml-based strings

string[] arrayToSend = new string[XMLList.Count];
arrayToSend = XMLList.ToArray<string>();

string results = DataFeedService.ProcessXMLBatch(arrayToSend);

Service Snippet:
[WebMethod]
public string ProcessXMLBatch(string[] XMLBatch)
{
       // process the xml here...
       return "done";
}

Problem:
It does not consider the string[] that I create in the service to be the same kind of
string array.  It converts it to a type of #namespace.#servicename.ArrayOfString
this code will currently not compile. I have looked around for alternate ways to pass a list or a generic and I keep hitting roadblocks.
Answer: Consider changing your code to use a List<string> rather than an IList<string> (I know that's what you're actually using, but declare your variable as such). The List<T> class provides an explicit ToArray() function that will be faster than the Enumerable.ToArray<T> extension method you're employing in your code. This might clear up your issues too, but I'm not certain about that.
Also, you do not need
string[] arrayToSend = new string[XMLList.Count];

Just declare it as
string[] arrayToSend;

You're already assigning it with the ToArray() call; no need to create another one beforehand.
Edit Why not instantiate and populate the ArrayOfString class that's created in your service reference namespace? It will work just like a List<string> (and, in fact, I believe it inherits from it). Use this as the type for your XMLList, and just send the XMLList variable directly to the function.
Comment: I have implemented your suggestions, and am basically receiving the same error:

string[] arrayToSend;
arrayToSend = XMLList.ToArray();
string results = DataFeedService.ProcessXMLBatch(arrayToSend);

yields:
Error 4 Argument '1': cannot convert from 'string[]' to 'FileFeedParser.DataFeedService.ArrayOfString' E:\tlatourelle-pc-external\dev\AmazonDataFeed\FeedFileParser\FeedFileParser\Program.cs 223 86 FeedFileParser
Comment: Didn't see your answer, Jeff. You've moved to the top, not sure what the issue is.
Comment: Actually I ended up doing the suggestion in the Edit.  I populated the ArrayOfStrings object in the Service Reference.  I then passed this to the Service and it accepted it.

Thanks, Tom
Answer: If you change your WebMethod to accept a List<string> rather than string[] and pass an appropriate type, it should work. ASP.NET services seem to prefer this in my experience.
Also, don't forget to make sure your Service References are up-to-date so that the service wrapper types are correctly created for your code to interact with.
Update
Have you tried actually creating the ArrayOfString type and populating it, rather than using string[] when calling your WebMethod?
Comment: I have also tried this methodology and also receive the ArrayOfString error as described above with the string[] example.  I have also made sure that my references are up-to-date each time I make a change to the service.

Service Reference and List/Array Types


SSH Tunnel can't connect - connection times out, even though regular login works

Question: I am trying to set up an SSH tunnel with a SOCKS proxy in Firefox, so I can surf the internet securely from public wifi. Both client and server are running Ubuntu 12.04 LTS.
After setting up port forwarding on my router (port 8888 forwards to port 22 on my server at home, at least for this example), I am able to log in via ssh normally using the command:
ssh -p 8888 user@hostname

However, when I follow the "standard" directions for setting up the tunnel, I cannot connect. The directions tell me to use this command:
ssh -D 8888 user@hostname

But all I get is a connection timeout:
ssh: connect to host hostname port 22: Connection timed out

I haven't found any other helpful posts on here (or elsewhere). Any suggestions or ideas? I already added the following line to /etc/ssh/sshd_config on the server and restarted sshd:
AllowTcpForwarding yes

Same result. Suggestions are greatly appreciated.
Answer: You seem to have mixed up what exactly happens here. ssh -p will tell the ssh client on which port the server is listening, and -D will tell it on which port it should listen locally as SOCKS proxy.
You should use it like this:
ssh -p 8888 -D 5115 user@host

Test it with curl like this:
curl --proxy socks5h://localhost:5115 http://private.example/contacts.html

Source: http://blog.sanctum.geek.nz/ssh-socks-and-curl/


Use teamviewer for running web server

Question: I have a PC behind NAT. I want to host a web server on it which is accessible from the internet. I cannot open any blocked ports nor do i have admin rights on the computer. But it has a teamviewer client running on it 24*7 to which i do have access(no vpn installed). I can accesss this client from anywhere using teamviewer. I want to know if somehow i can use teamviewer to get a ip/hostname that is accessible from the internet and host a webserver.
Also is it possible to get an ip if i get admin access but without opening ports.
Comment: We're not here to help you circumvent network policy. This is a site for professionals.
Answer: No, that's not possible. Further, you're asking us to help you violate network policy, like the policies we implement... That's like trying to buy marijuana from a uniformed police officer.

Use teamviewer for running web server


Binary Classification in Imbalanced Data; Oversampling and Imputation

Question: Together with two friends I participate in a university course about data mining in R and we chose the topic of bankruptcy prediction. We started with some "clean" data found on an "In class" kaggle competition and comparing to the leaderboard our classification seemed to perform very good.
Now we've gone on to use the public dataset about Polish companies from UCI, that has some missing values; apart from that, it's basically the same data. We get AUC scores of 0.96-0.98 depending on the chosen seed, so the classification seems to perform pretty good. The score is better than in the paper by Zieba et al. (2016) that gathered and first worked with the data.
We apply k-fold-cross validation and use the Extreme Gradient Boosting method "xgbLinear". Now dealing with this data we discovered a few strange facts about the performance:
For the kaggle data without missing values, SMOTE Oversampling definitely improved the classification performance. Now for the UCI data, running it without Oversampling performs slightly better. Any ideas how that can be?
Next, we experimented with imputation for the missing values. The best scoring method is to put all of them equal to "-99999" (as Zieba et al. have done as far as I can tell). This makes sense as the distribution of missing values is different for the two classes in our data and so has some predictive power.
We thought we might be able to slightly improve on that method by using KNN imputation for the missing values and then add a dummy variable for every predictor that is 1 for a missing value (prior to imputation) and 0 if there was no missing value. But it turns out this method performs worse than the simpler (and more arbitrary?) method of making all of them equal to "-99999". My intuition is that the dummies should capture the information stored within the distribution of missing values, and additionally to that the KNN imputation should definitely provide a more realistic picture of missing values than the -99999 does. But apparently my intuition is wrong here;)
So in short:
Why does Oversampling lead to worse performance here? Is it connected to the occurrence of missing values in our data?
Why does the "-99999" method perform better than KNN imputation + dummies?
I'd be incredibly thankful for any ideas and input on this! If helpful I can supply our code.
Answer: The fact that you are having to consider balance in the Y distribution means that you don't understand the need to predict tendencies rather than make arbitrary classifications (premature decisions).  This is discussed in detail here.  If you have to sample from your data there is a fundamental logic flaw in the approach.
Comment: Thank you very much for your feedback, we will look into this in detail the next days.
From my current understanding I can just say this:

I believe I understand the point about predicting tendencies instead of a binary class, because predicting whether an active company will default within the next year is essentially a probabilistic question.

So classifying based on some probability threshold obviously involves a utility trade-off, which needs to be made by the respective decision maker.

I will add a comment as soon as I have figured out whether/how this changes our approach.
Comment: The "xgbLinear" algorithm we used so far predicts probabilities for new data points to show Y=1 (companies that will go bankrupt). In a next step we performed the classification based on some threshold.

I've read your blog posts and completely get the point about classification being context-/preference-dependent, thank you for that, we will emphasize that.

My question now is:

Are the probability outputs of "xgbLinear" ready to interpret as such, or do they need any transformation/calibration? And what scoring rule is proper for our task, is AUC correct?
Comment: Others will have to answer the xgbLinear output question.  As for scoring rules, take a look at the Brier score, pseudo $R^2$ (a version of the logarithmic proper scoring rule).  Use the c-index (concordance probability; AUROC) as a descriptive additional measure.
Comment: My plan is now to compare the results for different models using (1) AUROC and (2) Brier Score as performance metrics; being especially interested in whether/how the ranking of models changes depending on the metric. But as far as I know, Brier Score (or log loss) will give misleading results when working with imbalanced data.

That means I'd need to implement Brier Skill Score for example, correct?
As I'm having trouble implementing this metric in "caret", I'm probably starting to use simple Brier Score (or log loss) after balancing the data with SMOTE, which is not my favorite solution..
Comment: No, the Brier score works fine with imbalanced data.  Also look at the measures in http://fharrell.com/post/addvalue
Comment: Thanks again, interesting article.
Unfortunately I'm struggling to implement the Brier Score within the "caret" framework so far. I will keep on working on that, but is logarithmic loss also fine as a metric for training and evaluating the models? As far as I understand it's also a proper scoring rule like the Brier Score, but puts more weight on bigger deviations between observed values and predicted probabilities.

So the structure would be:

- Train and compare ranking of different models based on (1) AUROC and (2) Log-Loss
- Calibrate predicted probabilities for the best model
Comment: Train using log loss which is log likelihood (the gold standard).  As an accuracy score log loss is also a gold standard; it's just not always used, because a single "mistake" gives a score of minus infinity.  Never use AUROC to train.  Brier should be trivial to compute in any context that produces probabilities.
Comment: Okay, will do! I will separately also use AUROC to train just for the sake of comparing both versions (as a lot of papers about bankruptcy prediction afaik use AUROC for training and evaluation; for example Zieba et. al (2016), which has been the starting point of our analysis), but then we will critically discuss this approach based on the information from your articles and other sources.
The computation of Brier is clear mathematically, but as a beginner in R I'm having trouble to implement it within "caret" (there is a problem with transforming the class variable from factor to numeric).
Comment: Once you have the predicted probability, Brier is easy.  Suppose that the outcome variable has a value "yes" whose probability is being predicted.  Then use `mean(((y=='yes') - p)^2)`.
Comment: I'm slowly getting there; one more question: Why is AUROC a bad metric to train models with? Is the only reason that it doesn't prioritize goodness of fit as Log-Loss does (which is a good enough reason of course)? Or is there more to it, especially connected to the problem of doing binary classification instead of prediction where the latter is more fitting to the problem at hand?
Comment: AUROC is for continuous risk prediction, not classification.  It is an excellent supplemental index of predictive discrimination.  It is the concordance probability between predicted risk and observed Y=0/1, and is a simple translation of Somers' Dxy rank correlation.  Using only the ranks of predicted risk it is not sensitive enough for comparing models.
Answer: SMOTE stands for "Synthetic Minority Over-sampling Technique". As you can deduct from the word synthetic - it creates additional minority instances. (which are similar to the original minority instances). That way it reaches class balance.
This creating of new minority instances follows some algorithm (just google for SMOTE). If this new instances do not resemble the actual ground truth your performance will be worse. Meaning you tried to create new instances for "bankruptcy" - but these synthetically created instances are not representative for "bankruptcy". These new - but wrong "bankruptcy" instances mislead your algorithm.
Try another over- or undersampling technique. (maybe one that does not create synthetic instances). This does not mean synthetic instances are bad - it just did not work in this case.
Might also be affected by the missing data - but I would rather think the two datasets are just different. Just because both are about bankruptcy does not mean they behave the same or the same set of algorithms will be best.
About the KNN imputation:
This can also mislead your algorithm. Especially KNN imputation is prone to this. KNN - as the name says just looks for the nearest neighbors  - the most similar instances. So what might happen e.g for knn (k=1) is the following: variable x is missing for instance 3, knn finds instance 5 which is exactly the same but with variable x. Then the x value of instance 3 gets imputed with the x value of variable 5. The imputation just repeats what is already there in the dataset.
Instance 5 is now basically overrepresented in the dataset. Which might lead to a worse performance.
You could try other imputation methods. Generally the binary missingness indicator dummy variable is a good idea. The problem probably rather lies with the imputation method you've chosen. Sometimes also no imputation is the best solution.
Comment: Both datasets are actually based on identical data, but they are different "subsamples" from this Polish Companies Bankruptcy Data; one is very neat and clean, the other contains observations with missing values. But I can see how there might be systematic differences, if the clean data has been constructed by only sampling observations with no missing values for example.

We will look into the imputation problem in more detail, thank you! But what do you mean by no imputation? Just omitting those observations would lead to significant bias I suppose.
Answer: There are multiple things to consider.

SMOTE operates in the Euclidean space implied by the data, thus, the range of the attributes matters when SMOTE determines which are the closest neighbors of a sample. Extreme values for missing values will definitely ruin this. I would recommend the standardization/normalization of all attributes, and the use of means/medians/modes for data imputation.
Classification after SMOTE is highly sensitive to the number of samples being generated. Balancing the dataset might work, but in many cases not. Consider two classes sampled from two manifolds, the manifold of the minority class having half the size of the manifold of the majority. In this case, balancing the dataset to have the same number of samples will result a situation where the density of minority samples is two times the density of the majority ones near the decision boundary, which clearly deteriorates the results, but in the reverse way. Thus, it is highly important to carry out model selection for the number of samples to generate using SMOTE.
You could test various oversampling techniques to find the best one for the data. Implementations can be found in packages like imblearn or smote-variants (the latter is my own).


Insert multiple rows in a table based on ranges between multiple columns in another table

Question: I have four columns YEAR1, MONTH1, YEAR2, MONTH2 of INT in a table named TABEL1. I want to insert multiple rows into table TABLE2 for each row in TABLE1 which have YEAR and MONTH values between YEAR and MONTH columns values in TABLE1.
Table1 is like:
ID   YEAR1   MONTH1   YEAR2   MONTH2
------------------------------------
1    2010      11     2011      2
2    2012      10     2012      12

Table2 should be like:
ID   YEAR   MONTH
-----------------
1    2010    11
1    2010    12
1    2011     1
1    2011     2
2    2012    10
2    2012    11
2    2012    12

I don't know if I should use while loop or just use select statement.
I wrote a function for inserting but I found out I cannot use CRUD in user defined function.
SELECT Calc(ID,YEAR1,YEAR2,MONTH1,MONTH2) FROM Table1

CREATE FUNCTION Calc(@ID INT, @YEAR1 INT, @YEAR2 INT, @PERIOD1 INT, @PERIOD2 INT)
RETURNS TINYINT
AS BEGIN
    DECLARE @ID INT
    DECLARE @YEAR INT
    DECLARE @MONTH INT

    SET @YEAR = @YEAR1
    SET @MONTH = @MONTH1
    WHILE (@YEAR <= @YEAR2)
        WHILE (@MONTH <= @MONTH2)
            INSERT INTO TABLE2 (ID,YEAR,MONTH)
                        VALUES (@ID,@YEAR,@MONTH)
            SET @MONTH = @MONTH + 1
        END
        SET @YEAR = @YEAR + 1
    END
    RETURN 1
END

UPDATE:
MONTH here is not exactly month. It's actually SEASON with the values of 1,2,3,4.
Another issue is that YEAR and MONTH are extracted from Persian dates not Gregorian dates and persian dates are saved as NVARCHAR in database.
So, it seems I cannot use DATE functions here.
Answer: First, create a table-valued function which returns the dates for you according to the @ID passed as
CREATE FUNCTION dbo.GetDates
(
  @ID INT
)
RETURNS TABLE
AS
RETURN
(
  WITH CTE AS
  (
  SELECT DATEFROMPARTS(YEAR1, MONTH1, 1) Dates
  FROM T
  WHERE ID = @ID
  UNION ALL
  SELECT DATEADD(Month, 1, Dates)
  FROM CTE
  WHERE DATEADD(Month, 1, Dates) <
    (SELECT DATEFROMPARTS(YEAR2, MONTH2, 2) FROM T WHERE ID = @ID)
  )
  SELECT *
  FROM CTE
)

Then simply use it as (Use CROSS APPLY to access the ID column)
SELECT T.ID,
       YEAR(Dates) [YEAR],
       MONTH(Dates) [MONTH]
FROM T CROSS APPLY dbo.GetDates(ID) TT

Returns:
+----+------+-------+
| ID | YEAR | MONTH |
+----+------+-------+
|  1 | 2010 |    11 |
|  1 | 2010 |    12 |
|  1 | 2011 |     1 |
|  1 | 2011 |     2 |
|  2 | 2012 |    10 |
|  2 | 2012 |    11 |
|  2 | 2012 |    12 |
+----+------+-------+

Demo
Comment: You used DATE functions in your answer. What if I want to use seasons instead of months? I mean I have `SEASON` column with the values of `1,2,3,4`
Comment: I think I give you the full function, and you can easy make some changes if needed.
Answer: Lots of good options already mentioned. I would also lean towards a periodics table and a join. One approach might be the following one:
USE TEMPDB

CREATE TABLE #Y (YY INT)
INSERT INTO #Y VALUES (2000), (2001), (2002), (2003), (2004),
                  (2005), (2006), (2007), (2008), (2009),
                  (2010), (2011), (2012), (2013), (2014),
                  (2015), (2016), (2017), (2018), (2019)

CREATE Table #M (Mnth INT)
INSERT INTO #M VALUES (1), (2), (3), (4), (5), (6),
                  (7), (8), (9), (10), (11), (12)

SELECT *, CASE WHEN Mnth < 10 THEN CAST (YY AS CHAR (4)) + '0' + CAST (Mnth AS CHAR (2))
        ELSE CAST (YY AS CHAR (4)) + CAST (Mnth AS CHAR (2)) END AS Combined
INTO #Period
FROM #Y
CROSS JOIN #M

SELECT * FROM #Period

CREATE TABLE TABLE1 (ID INT, YEAR1 INT, MONTH1 INT, Combined1 CHAR (6), YEAR2 INT, MONTH2 INT, Combined2 CHAR (6))
INSERT INTO TABLE1 VALUES (1, 2010, 11, '201011', 2011, 2, '201102'), (2, 2012, 10, '201210', 2012, 12, '201212')

SELECT * FROM TABLE1

SELECT T.ID, P.YY, P.Mnth
FROM #Period AS P
INNER JOIN TABLE1 AS T ON P.Combined BETWEEN Combined1 AND Combined2
ORDER BY 1, 2

--DROP TABLE #M, #Period, #Y, TABLE1

Comment: this is my accepted answer since other answers rely on Date functions which is not working in my case
Answer: The query becomes trivial if you use a Calendar table. Calendar tables contain one row per date for eg 100 years in the future with separate fields for year, month, day, day of year, week number and whatever else you may need. Indexing individual fields allows very fast lookups by year, month etc.
Once you have such a table you only need to return the distinct year and month Calendar values that lie between the From and To values. If the range values were dates, one could write :
select distinct id,Calendar.year,Calendar.Month
from #table1 inner join DimDate as Calendar
    on Calendar.Date between #table1.StartDate and #table1.EndDate
order by Calendar.Year,Calendar.Month

Things become a little more complicated since we need to compare years and months :
CREATE TABLE #Table1 (
    ID int,
    YEAR1 int,
    MONTH1 int,
    YEAR2 int,
    MONTH2 int
)
INSERT INTO #Table1
    (ID, YEAR1, MONTH1, YEAR2, MONTH2)
VALUES
    (1, 2010, 11, 2011, 2),
    (2, 2012, 10, 2012, 12)

select distinct id,Calendar.year,Calendar.Month
from #table1 inner join DimDate as Calendar on
    (#table1.YEAR1=Calendar.Year and #Table1.Month1<=Calendar.Month
                                 and (#table1.YEAR2>Calendar.Year or #Table1.Month2>=Calendar.Month)
    )
    or
    (#table1.YEAR1<Calendar.Year and (#table1.YEAR2>Calendar.Year
                                       or (#table1.YEAR2=Calendar.Year
                                           and #Table1.Month2>=Calendar.Month)))
order by Calendar.Year,Calendar.Month

The query could be simplified a lot if the source and Calendar tables had a Year-Month column, eg in YYYYMM format. This could be an actual column or a computed and persisted column that's covered by an index :
CREATE TABLE #Table1 (
    ID int,
    YEAR1 int,
    MONTH1 int,
    YEAR2 int,
    MONTH2 int ,
    YearMonth1 AS YEAR1*100+Month1 persisted,
    YearMonth2 as Year2*100+Month2 persisted
)

create index IX_Table1_YearMonth1 on #Table1 (YearMonth1) Include (Year1,Month1)
create index IX_Table1_YearMonth2 on #Table1 (YearMonth2) include (Year2,Month2)

After that, the query becomes a simple :
select distinct id,Calendar.year,Calendar.Month
from #table1 inner join DimDate as Calendar on
    Calendar.YearMonth between #table1.YearMonth1 and #Table1.YearMonth2
order by Calendar.Year,Calendar.Month

The execution plan is simple and fast :
Answer: Can you try this query
DECLARE @minYear  VARCHAR(10),
        @minMonth VARCHAR(10),
        @maxYear  VARCHAR(10),
        @maxMonth VARCHAR(10)

SELECT *
INTO   #table1
FROM   (SELECT 1    id,
               2010 year1,
               11   month1,
               2011 year2,
               2    month2
        UNION ALL
        SELECT 2    id,
               2012 year1,
               10   month1,
               2012 year2,
               12   month2) Table1

SELECT @minYear = Min(year1),
       @minMonth = Min(month1),
       @maxYear = Max(year2),
       @maxMonth = Max(month2)
FROM   #table1;

WITH cte
     AS (SELECT CONVERT(DATE, @minYear + '-' + @minMonth + '-01') AS dt
         UNION ALL
         SELECT Dateadd (month, 1, dt) dt
         FROM   cte
         WHERE  dt < CONVERT(DATE, @maxYear + '-' + @maxMonth + '-01'))
SELECT id,
       Year(dt)[year],
       Month(dt) [month]
FROM   #table1 Table1
       INNER JOIN cte
               ON cte.dt BETWEEN CONVERT(DATE, CONVERT (VARCHAR, year1) + '-'
                                               + CONVERT(VARCHAR, month1) +
                                               '-01') AND
                                 CONVERT(
                                            DATE,
                                 CONVERT(VARCHAR, year2) +
                                 '-'
                                 + CONVERT(VARCHAR, month2) +
                                 '-01')

Answer: You can perfectly use UNPIVOT, here my proposal solution
DECLARE @Table1 AS TABLE
(
  ID INT,
  YEAR1 INT,
  MONTH1 INT,
  YEAR2 INT,
  MONTH2 INT
)

INSERT INTO @Table1  VALUES (1,2010,11,2011,2),(2,2012,10,2012,12)

SELECT ID, [YEAR],[MONTH]
FROM
(
  SELECT ID, YEAR1,YEAR2,MONTH1,MONTH2
  FROM @Table1
) AS t
UNPIVOT
(
  [YEAR] FOR YEARS IN (YEAR1, YEAR2)
) AS up
UNPIVOT
  (
    [MONTH] FOR MONTHS IN (MONTH1,MONTH2)
  ) AS um

Answer: One possible approach is to define one table-valued function and then use this function:
Function:
CREATE FUNCTION [dbo].[udf_GetMonths] (@year1 int, @month1 int, @year2 int, @month2 int)
RETURNS TABLE
AS
RETURN
(
    WITH Months AS
    (
        SELECT DATEFROMPARTS(@year1, @month1, 1) AS SomeDate
        UNION ALL
        SELECT DATEADD(month, 1, SomeDate) AS SomeDate
        FROM Months
        WHERE
            (DATEPART(year, SomeDate) < @year2) OR
            (DATEPART(month, SomeDate) < @month2)
    )
    SELECT YEAR(SomeDate) AS [YEAR], MONTH(SomeDate) AS [MONTH]
    FROM Months
)

INSERT Statement:
-- Table 1
CREATE TABLE #Table1 (
    ID int,
    YEAR1 int,
    MONTH1 int,
    YEAR2 int,
    MONTH2 int
)
INSERT INTO #Table1
    (ID, YEAR1, MONTH1, YEAR2, MONTH2)
VALUES
    (1, 2010, 11, 2011, 2),
    (2, 2012, 10, 2012, 12)

-- Table 2
CREATE TABLE #Table2 (
    ID int,
    [YEAR] int,
    [MONTH] int
)

-- Insert
INSERT INTO #Table2 (ID, [YEAR], [MONTH])
SELECT t.ID, f.[YEAR], f.[MONTH]
FROM #Table1 t
CROSS APPLY (SELECT * FROM dbo.udf_GetMonths(t.YEAR1, t.MONTH1, t.YEAR2, t.MONTH2)) f
OPTION (MAXRECURSION 0)

Inserted rows:
ID  YEAR    MONTH
1   2010    11
1   2010    12
1   2011    1
1   2011    2
2   2012    10
2   2012    11
2   2012    12


Calculate the volume of the solid

Question: Which is generated from the common part of $$ y =x^2$$ and $$y^2=8x$$ as it rotates around $0y$. Limits of the integral should be from $0$ to $4$ right? Shouldn't the integral be $$π\int_0^4(x^2 - \sqrt{8x})^2dx$$
Comment: The intersection of the two curves is at $(0,0)$ and $(1,1)$
Comment: see my answer${}$
Answer:
Scale misleading

Answer
At $y$ distance from origin we take a thin vertical ring of thickness $dy$. Now volume of this ring is:
$$dV=\pi(x_o^2-x_i^2)dy$$
where $r_i$ and $r_o$ are inner and outer radii.
Now inner radii will be on blue curve such that:
$$8x_i=y^2$$
And the outer radii:
$$x_o^2=y\implies x_o=+\sqrt y$$
Note the "+" because we're for the time being taking the Ist quadrant in consideration.
Now $$dV=\pi(y-y^4/64)dy$$
They intersect at $k=h^2,k^2=8h\implies h=2,k=4$
And:
$$V=\int_0^4\pi(y-y^4/64)dy=\frac{24\pi}5$$
Comment: That is not a plot of $y^2=8x$ :)
Comment: Sorry I made a mistake with one function and forgot to include the $8$. Thanks for the answer.
Comment: @user3601507 Does that mean your'e going to accept this answer and upvote it?

Calculate the volume of the solid


Conjugation of a direct product

Question: Suppose that $H \times K \leq G = A\times B$ and let $g\in G$. Is it true that $g( H\times K)g^{-1} = gHg^{-1} \times gKg^{-1}$ holds in general?
Comment: The fact that $G = A \times B$ is irrelevant and distracting. You could just delete "$=A \times B$", and the answer would still be yes.
Answer: We have $$g(H\times K)g^{-1}=g\{(h, k)\in H\times K\}g^{-1}=\{g(h, k)g^{-1}|(h,k)\in H\times K\}=\{(ghg^{-1}, gkg^{-1})|(h, k)\in H\times K\}=gHg^{-1}\times gK^{-1} $$
Comment: It might be worth mentioning that, in this answer as well as the question, elements $h\in H$ (or $k\in K$) are tacitly identified with elements $(h,1)$ (or $(1,k)$) of $G$, so that $ghg^{-1}$ (or $gkg^{-1}$) makes sense.

Conjugation of a direct product


Coordinates in basis

Question: linear algebra is quite confusing at the beginng... So: Let
$V=lin  \left  ( (1,1,3,2),(4,5,2,5),(2,3,-4,1),(1,2,-5,5)   \right ) \subset \mathbb{R}^4$a) Find base and dimension of $V$
b)For what values of $r\in\mathbb{R} $ exists base $\alpha_1, \alpha_2,\alpha_3,\alpha_4 $ in $\mathbb{R}^4$ which satisfies: $\alpha_1, \alpha_2 \in V$ and vector $\beta=(1,1,3,3)$ has in that base coordinates $0,1,r,0$? For every such $r$ give example of base.

First part of quest is easy, just enough to pout everything in matrix and calculate.
Second part is not easy. I don't really understand it. I calculated that $\dim V=2$, so I have three vectors to choose from, but I have to choose only two, so can I choose two most suitable? Further, when I choose two and add $\beta$ I still have only three, but i need for. Can I add one more unique vector to those three (as I am adding to $\mathbb{R}^4)$, e.g. $(0,0,0,1)$? And what with $r$? It really confuses me, any help and hints will be appreciated, thanks in advance.
Comment: Matlab tells me that $\mathbb{dim} V = 3$.
Answer: b) Let's say you are given a real number $ r $. Now, you have to check if it possible to find 4 linearly independent vectors such that at least two of them are in the space V. No condition on the remaining two vectors except linear independence. You take these 4 vectors $ a_1, a_2, a_3, a_4 $ and try to express the vector $ \beta $ in terms of them. It means, that you find 4 numbers $ c_1, c_2, c_3, c_4 $ such that $ \beta = c_1a_1 + c_2a_2 + c_3a_3 + c_4a_4$. If, as in the question, the numbers are 0, 1, r and 0, these $ a_i $ are "ok".
Finally, what you are supposed to do in the exercise - you have to find for which numbers $ r $ it is possible to find these vectors $ a_1, a_2, a_3, a_4 $ so that they are "ok"(when you express $ \beta $ in terms of them, the coefficients are 0, 1, r, 0).
Hope that helps :)
Answer: a) you just need to see is if the vector given are independent. they are not as $(4,5,2,5)$ is an addition of $2 \cdot (1,1,3,2)$ and $(2,3,-4,1)$ then check the rest are independent by computing the rank of the matrix:
1 1 3 2
2 3 -4 1
1 2 -5 5
thus you get a degree of 3, and those vectors are also Basis.
Now for 2, you simply got $dim(V)$ wrong, it is 3 (as explained above). try now
Comment: Because you need to know what you are choosing from. For example after fixing the first section you can always just take $\alpha_2=(1,1,3,2) , \alpha_1=(1,2,-5,5),\alpha_3=(0,0,0,1)$ and $\alpha_4=(0,0,1,0)$ and then you have the base coordinates 0 1 1 0.
Comment: But i have to choose two vectors from V, does it matter which?
Comment: Alright, i think i got it, thanks. One more question, is a base of space somehow related to the base of subspace of system of solutions? In particular: are they equal?
Comment: what do you mean by equal? the two basis $(2,-3) (1,1)$ and $(1,0),(0,1)$ are both basis of the same space $\mathbb{R}^2$ but they are not "equal" they simply span (produce) the same space.
Also, what system of solutions are you referring to? can give an example?
Comment: umm.. system of solutions of, e.g. 3 equations $x_1,x_2,x_3,x_4$, by equal i mean that the spaces are equivalent. I ask if base of that equations span the same space as base of subspace of solutions of this equations
Comment: First, you do not say "equal base". What you mean is "equal space" you are asking if the space of the solutions is equal to the space of the equations. not sure what "space of the equations" mean but i will try to answer in the following comment
Comment: Again, i am having difficulty understanding exactly what you mean. I never heard of "base of equation span"; Lets take a specific example: if we take the matrix A to 2x2 with the first raw 1 0 and the second 0 0. Then the solution space for the system of equations Ax=0 is {(0,1)}. "The base of equations" however is {(1,0)} (again not sure if i understood you correctly).
Comment: By base of subspace of solutions (its directly translated form Polish) i mean something like $(x_1,x_2,x_3,x_3)=(3x_3 -x_4, x_3+2x_4,x_3,x_4)=x_3(3,1,1,0) + x_4 (-1, 2,0,1)$ (just an example) So "base of solutions of..." here is $(3,1,1,0), (-1,2,0,1)$. Is it complementary with the base of main space, i mean base, which i get directly from matrix?

Coordinates in basis


rails routes question - how to take all parameters to end of URL?

Question: In the rails config/routes.rb file, how do I ensure that the first route here takes all characters after the initial /site and uses this for this route (assigning it to :path).
<code>  map.connect 'sites/:path', :controller => 'xxx', :action => 'yyy'
  map.connect ':controller/:action/:id'
</code>
In other words how to avoid any URL that goes http://mysite/sites/, to be missed by the first route and then picked up by the second route?
thanks
Answer: Put the second route first or..
use Route Globbing (Section 4.9)?


Does a I V bVII V (F C Eb C) work? And if so, why?

Question: Was wondering if it would work and why.
Comment: Can you elaborate what you mean by "work"? It's a pleasant-sounding chord progression; if you can come up with an interesting melody to go with it, I'm sure no one will call the music police on you... so what exactly is the question?
Comment: Does this answer your question? [Why do many songs in major keys use a bVII chord?](https://music.stackexchange.com/questions/29817/why-do-many-songs-in-major-keys-use-a-bvii-chord)
Comment: Instead of posing the question, your best answer is to play it several times. If it works for you, then, yes, it 'works'. The why part has an answer already.
Comment: To decide it it would "work", look at other songs and you will find that progression.  Then listen to the songs and get the sound in your head and you will be able to use it for your own music possibly.  Give us more detail such as genre or a particular song to understand more of your question.
Answer: Does it work? What does 'work' mean? Were Bach or Mozart to have written a piece using that change of harmony, maybe it wouldn't have 'worked'.
Using it in a piece of modern pop or Blues especially, yes, it will work.
Assuming key F, the E♭ is VII♭7, which is commonplace in a lot of Soul and Blues music. Could be said to be borrowed from the parallel key (Fm). But initially, someone used it and it was heard to be acceptable, so it got used again and again.


How can you create an .msix package in WPF with .NET 5.0.0 when Microsoft.EntityFrameworkCore.SqlServer (5.0.0) is present (app won't launch)

Question: I have found that I can create a WPF app that deploys from an .msix package but won't launch if it contains Microsoft.EntityFrameworkCore.SqlServer package. Here is a repro (and some other quirks):
Create WPF App .NET 5 app with Windows Application Packaging Project. Compiles and runs. Installs (side loaded) and launches.
Cannot add Microsoft.EntityFrameworkCore (or SQLServer or Tools) 5.0.0 to the above Solution from the package manager. Get the following: Could not install package 'Microsoft.EntityFrameworkCore 5.0.0'. You are trying to install this package into a project that targets 'UAP,Version=v10.0.17763', but the package does not contain any assembly references or content files that are compatible with that framework.
Can add all the EF packages (Microsoft.EntityFrameworkCore, Microsoft.EntityFrameworkCore.SqlServer and the Microsoft.EntityFrameworkCore.Tools)  if you first delete the WAPP project. Compiles and runs.
Now able to add WAPP to a project that already has the EF packages and create an .msix. Side Load, with code signing certificate. Installs without visible errors. Does not launch. Icon is installed on Start Menu.
If you remove the "all" value from the Private Assets property in Microsoft.EntityFrameworkCore.Tools (this fix worked in Core 3.1) and rebuild the app and WAPP, the .msix installs without visible errors. Does not launch. Icon is installed on Start Menu.
If you remove the .Tools package and recreate the app, it compiles and runs, the WAPP can be recreated, the .msix installs but the app doesn't launch.
If you remove the .SQLServer package and recreate the app, it compiles and runs, the WAPP can be recreated, the .msix installs but the app launches.
If you remove the WAPP package, add back the .Tools (so now the Microsoft.EntityFrameworkCore and the Microsoft.EntityFrameworkCore.Tools) and then recreate the WAPP the .msix is created, installs and the app launches.
If you now add back Microsoft.EntityFrameworkCore.SqlServer version 3.1.10 it compiles and runs, the WAPP creates an .msix and the app installs and launches. However, you can't scaffold  or execute without runtime errors with the .SqlServer 3.1.1 and EntitityFrameworkCore 5.0.0 together.
So, it seems that there is something about the Microsoft.EntityFrameworkCore.SqlServer package that prevents the installed app from launching.
Comment: Turns out you need to change Private Assets of the Microsoft.EntityFrameworkCore.SqlServer property to "all". That allows the app to install and launch.
Comment: However, any call to the context crashes the app. It will work correctly when debugging. But, after installation with the .msix it crashes on any call to the context
Answer: Some of the micorsoft.entityframeworkcore packages have a Private Assets set. This prevents some of the assemblies from being available after an .msix installation. If you remove the property "All" from the Private Assets the application should install and run correctly


using $GLOBALS['base_path']

Question: A quick one - is it ok to use this? I can't seem to get access to $base_url in my node which is set in my settings.php file.
Comment: Yes it is ok to use this one. In D7 there is base_path() function which return the same value. See here https://api.drupal.org/api/drupal/includes!common.inc/function/base_path/7
Answer: No, it's not OK to use it directly. Always use base_path() function instead. There are two benefits:

You will not risk accidental change in the original variable, so you will not cause other modules to fail if you'll make a mistake.
If, for whatever reason, Drupal will change way it stores base path, your module will continue to work properly.

using $GLOBALS['base_path']


Conference registration form/site with limited places available

Question: I have to build a little conference-site with a registration-form.
The conference is 1 day and has different sessions in the morning and different sessions in the afternoon. Those sessions have limited spaces available.
Users should be able to register for the conference by the registration-form and if there are no more spaces available for a specific session, they should get a message that this is no longer available, so that they have to choose another session.
What is the best solution for this, knowing that my client wants 1 registration form for the entire day. Otherwise I was considering entity_registration. With the "1 registration form" in mind, I was considering to use webform and webform_validation or webform_rules, but there is no standard solution for the limited spaces available.
Is there a good solution for this problem?
Can someone help me?
Comment: are you not considering a programming approach?
Answer: Session = content_type.
One session = one node of type session
Every session has a field "available spaces"
Create a custom form programatically which you pass the session id in a field of type "value".
https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#val
In the validate function of the form check if the field "available spaces" for the session with that id is greater than 0, otherwise decrease it and in the submit function store the user id with this session id.
Regards.
Comment: Thank you for your answer! I will give it a try, because I am not that experienced with forms_api and the programming approach. Is this also possible with webform?
Comment: I've not worked with webform for a while, probably you can but It would be a less clean approach


dynamic id and special characters for serialization with flat buffers

Question: I have Json data like below
{
  "!type": "alarm",
  "$": {
    "12279": {
      "!type": "alarm",
      "title": "Default",
      "$": {
        "5955": {
          "!type": "alarm",
          "name": "Wake",
          "day": "SUN",
          "startTime": "06:00"
        },
        "29323": {
          "!type": "alarm",
          "name": "Away",
          "day": "SUN",
          "startTime": "08:00"
        },
        "2238": {
          "!type": "alarm",
          "name": "Home",
          "day": "SUN",
          "startTime": "18:00"
        }
      }
    }
  }
}

My fbs looks like this

namespace space.alarm;

table Atom{
    !type:string;
    name:string;
    startDay:string;
    startTime:string; }

table AtomShell{
    key:string (required, key);
    value: Atom; }

table Alarm{
    !type:string;
    title:string;
    $:[AtomShell]; }

table AlarmShell{
    key:string (required, key);
    value:Alarm;  }

table Weeklyalarm{
    !type:string;
    $:[AlarmShell]; } root_type Weeklyalarm;

Im trying to implement google flat buffers but I'm getting errors like

alarm.fbs:4:0: error: illegal character: !
alarm.fbs:23:0: error: illegal character: $  (i have removed ! from
!type and changed $ to dollar to test the working of flat buffers
but i can't change the dynamic ids )
Sample.json:25:0: error: unknown field: 12279

Now my question,

Is it possible to use dynamic ids in flat buffers, if possible how
shall i proceed?
Can is use special characters in ids, if possible how to do it?

Thanks in advance.
Answer: You can't have characters like ! and $ in field names. Just use type instead of !type, etc.
Not sure what you mean by dynamic ids. All field names (keys) have to be declared in the schema, so they can't be dynamic. You can still achieve similar results though, if you make your JSON look something like this:
{
  "type": "alarm",
  "data": [
    {
      id: "12279",
      "type": "alarm",
      "title": "Default",
      "data": [
        {
          "id": "5955",
          "type": "alarm",
          "name": "Wake",
          "day": "SUN",
          "startTime": "06:00"
        },
        {
          "id": "29323",
          "type": "alarm",
          "name": "Away",
          "day": "SUN",
          "startTime": "08:00"
        },
        {
          "id": "2238",
          "type": "alarm",
          "name": "Home",
          "day": "SUN",
          "startTime": "18:00"
        }
      ]
    }
  ]
}

And then make the corresponding schema.
Note that I made the "dynamic" list into a vector, and moved the id into the object itself.
Other tip: string values that are not dynamic (like "alarm") will take up way less space if you make them into an enum instead.


Mudar Videos da Tela

Question: Bom Pessoal eu tenho um tela que incorpora vídeos do Youtube

Estou carregando uma lista de videos do banco de dados, que possui o título do vídeo, e o link do vídeo, estou criando um botão pra cada vídeo.
<?php
  foreach ($listaVideo as $videoobj) {
?>
  <a href="<?php echo $videoobj->getLink()?>" data-dismiss="modal" class="btn btn-block btn-primary texto-grande" type="button"> <?php echo $videoobj->getTitulo()?>
<?php
  }
?>

A duvida é a seguinte, como faço para mudar o vídeo do IFRAME quando o botão for clicado ? com javascript ou jquery
Comment: O que você deseja seria alterar o a fonte (`src`) do vídeo ao clicar em um botão?
Comment: eu queria mudar o link do iframe, com o link que to pegando do banco no caso na parte $videoobj->getLink()
Answer: Faça com que os botões alterem o atributo 'src' do iframe, veja (note que eu setei um id ao iframe):
Jquery:
$("#idiframe").attr("src", "linkvideo");

Javascript:
document.getElementById('idiframe').src ='linkvideo';

@Edit: Pra chegar no que você quer, substitua o atributo href do link pra '#' ficando assim

Após fazer isso atribua o código que eu lhe passei à uma função, pra evitar o repetimento de código.
function mudarVideo(url){
    document.getElementById('idiframe').src = url;
}

Nos botões sendo criados pelo laço for, acrescente o seguinte atributo:
onclick="mudarVideo('<?php echo $videoobj->getLink();?>');?>"

Ficando no final desse jeito:
<?php
    foreach ($listaVideo as $videoobj) {
?>
    <a href="#" onclick="mudarVideo('<?php echo $videoobj->getLink();?>');?>"  data-dismiss="modal" class="btn btn-block btn-primary texto-grande" type="button"> <?php echo $videoobj->getTitulo()?>
<?php
    }
?>

Deve resolver seu problema...
Comment: sim certo, mas como eu vou pegar os links dos videos, se estou criando os botões a partir de um for, eles n vão ter id definido
Comment: Não entendi muito bem, você está criando os botões a partir de um for, e eles não vão ter id definido, mas, os botões não precisam de um id, apenas o iframe.
Comment: Certo, mas como vou trocar o src do iframe, se não tenho como capturar o botão
Comment: Editei o post, leia e me fale se resolveu.
Comment: Era isso mesmo valeu
Answer: Neste caso, basta fazer uma função que altere o atributo src do iframe, exemplo:

function changeVideo($a) {
  document.getElementById('video-curso1').src = $a.href;
  return false;
}
Video 1
Video 2

Comment: Certo, mas vc percebeu que os botões estão sendo criado a partir de um for, eu quero saber como vou recuperar cada link desse, sendo do referido botão que foi clicado
Comment: Alterei o exemplo para receber o link do atributo href da tag , como no seu código. Então você só precisa adicionar o evento onclick conforme o exemplo
Comment: Era isso mesmo, agora funcionou

Mudar Videos da Tela


how to call files with createBlock

Question: I am following No_Frills_Magento_Layout by Alan Storm. I have got to a point that I could not make work by following what the book said so I went about it an alternate way
Below is the books way and I get an error
$layout = Mage::getSingleton('core/layout');
$block = $layout->createBlock('ts_nofrills/helloworld', 'root');
echo $block->toHtml();

//Fatal error: Call to a member function createBlock() on a non-object in

below is my way and produces the .phtml text
$main_block = new Ts_Nofrills_Block_Helloworld();
$layout = Mage::getSingleton('core/layout');
$block = $layout->createBlock($main_block, 'root');
echo $block->toHtml();

My problem is as the book continues this example  is expanded on and by deviating from the example and doing it my way I dont feel like I am learning to the full potential.
I guess I have written the first parameter incorrectly for magento to recognise it in the createBlock function what have i done wrong on example one?
I have been reluctant to put this question on for two reasons:
1 - Im sure the answer is in the book (I cant find it though)
2 - Im sure this question has been asked on here (once again I have looked but cant find it)
===EDIT===
<global>
    <blocks>
        <Ts_Nofrills>
            <class>Ts_Nofrills_Block</class>
        </Ts_Nofrills>
    </blocks>
  ....
</global>

file structure

Ts

Nofrills

Block

Template.php

controllers

IndexController.php

etc

config.xml
Answer: Look inside etc/config.xml in the global node you should have:
<blocks>
        <ts_nofrills>
            <class>Ts_Nofrills_Block</class>
        </ts_nofrills>
</blocks>

When you want to create a block using php or layout xml you have to use the value from the node etc/config.xml blocks as namespace followed by / and then the path to your file inside the module delimited by underscores.
Comment: Thanks for the reply to my question. I do have the config setup which is why example two produce my .phtml file. What I dont understand is why example one produces and error and not example two. If it was a config problem surly they would both produce an error?
Comment: No, they would not produce an error since in your example you are creating a new object and in Alan's he is using Magento to create that object. I believe that in your case, Magento doesn't know from where to get the block class because of the config.
Comment: To load your class `ts_nofrills/helloworld` Magento looks in the config.xml for the first part whre are block classes called `ts_nofrills` and should find `Ts_Nofrills_Block` and then `/` becomes `_` and and explode for `_` and `ucfirst` for every element in the explode array so the class becomes `Ts_Nofrills_Block_Helloworld` and shoud be found inside `app/code/local/Ts/Nofrills/Block/Helloworld.php`
Comment: Thank you for the explanation. Maybe i've been staring at this problem for to long and now cant see the problem even though its staring me in the face. I have edit my original question to add my file structure and config to see if it will help with an answer
Comment: Try to not use capital letters, look at my answer.
Comment: Thanks Emi I have looked at your answer and thats how I understood it to work, which still leaves me confused as to why this doesn't work, I have tried making the caps lowercase
Comment: let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35925/discussion-between-emi-and-tony09uk)

how to call files with createBlock


Performing hefty image processing/path planning tasks on Arduino Due

Question: So, I want to make a robot that is able to navigate through an unknown maze. The only information provided to it would be a monochromatic bitmap image of the maze , and the robot has to identify the start and end points from it, the boxes in which it has to pot the balls it carries, and as well plan its path in the maze. It has to do all of it from that bmp image of the maze. Here's a link to a sample Bitmap Image of a Maze.
I will be using arduino due to do all of this processing. For now, I have read this BMP into a 2D character array, Link to the character array, made a connectivity map/graph from it so that a path planning algorithm could be applied on it (Dijkstra or Breadth First Search).
The problem however is the following, the size of this bitmap is 96x56 pixels. That means the size of the connectivity map/ graph would be [5376][5376] which is very large. To declare this large of an array would require memory in MB whereas the SRAM of the arduino due is only 96KB.
What should I do? Please suggest. I am just a first year electrical engineering student and so don't know much about all of this stuff. One thing that I thought of was to delete the rows that are same (please see the 2D array). However then I thought it, itself will require lots of processing power since I will have to compare every row to others, element by element. The problem here basically are the memory and processing constraints.
I would grateful!
Answer: You actually don't have to explicitly store your entire graph in memory. Alternatively, each time the algorithm visits a state, you can query the bitmap to see which neighbors are occupied are not, and generate the valid transitions/costs accordingly. This will take more processing power but will keep the motion model within the memory limitations of the processor.
However, for BFS or Dijkstra's algorithm, you will also have to store a queue of visited states, which could grow exponentially. You might want to consider use of A* instead of Dijkstra's algorithm because A* will explore fewer states than Dijkstra's algorithm to find the optimal solution.


Positive integers relatively prime to all the terms

Question: Determine all positive integers relatively prime to all the terms
of the infinite sequence
$$a_n = 2^n + 3^n + 6^n − 1, n ≥ 1.$$
Comment: @IndritKello Then these are relevant things to take up in the question. Any context and knowledge you have is relevant for us in order to answer it apropriate.
Answer: This is an old International Mathematical Olympiad problem. Show that $p \mid 2^{p-2} + 3^{p-2} + 6^{p-2} - 1$ for all primes $p\ge 5$ by using Fermat's Little Theorem. Hence the only solution is $1$, as $(a_1,2) = 2$ and $(a_2,3) = 3$
Answer: If  p>3 then, $2^{p-2}+3^{p-2}+6^{p-2}\equiv 1\mod p$. Now, multiply both the sides by 6 to get: $$3 · 2^{p−1} + 2 · 3^{p−1} + 6^{p−1} ≡ 6 \mod p$$
this is clearly a consequence of Fermat's little theorem. Therefore p divides $a_{p−2}$. Also, 2|$a_1$ and 3|$a_2$. So, there is no number other than 1
that is relatively prime to all the terms in the sequence.

Second approach similar to the above one.
Trivially, $a_2 = 48$ is divisible by 2 and 3.Therefore, Let p > 3 be a
prime number. Then
$$6a_{p−2} = 3 · 2^{p−1} + 2 · 3^{p−1} + 6^{p−1} − 6 ≡ 3 + 2 + 1 − 6 ≡ 0\mod p$$
And so the only such number is 1. Proved.


Line through two points in $\mathbb{P}^3$; Segre embedding

Question: Let $\Psi:\mathbb{P}^1\times \mathbb{P}^1\longrightarrow \mathbb{P}^3$ be the Segre embedding. If I take points $(A_1,A_2)$, $(B_1, B_2)$ in $\mathbb{P}^1\times \mathbb{P}^1$, I want to know how does the line that goes through $\Psi(A_1,A_2)$ and $\Psi(B_1,B_2)$ look like. Can someone help me with this?
Comment: What do you mean? All lines look the same...
Comment: This is elementary linear algebra.
Comment: @Sasha I would like to know the explicit equations for the lines
Comment: @Sasha perhaps, but could you help me?
Answer: In coordinates, the Segre embedding of $\mathbb{P}^1 \times \mathbb{P}^1$ into $\mathbb{P}^3$ sends a closed point $([x_0 : x_1], [y_0 : y_1]) \in \mathbb{P}^1 \times \mathbb{P}^1$ to the point $[x_0y_0 : x_0y_1 : x_1y_0 : x_1y_1] \in \mathbb{P^3}$. This is the n = m = 1 case of the more general equation given at https://en.wikipedia.org/wiki/Segre_embedding.
Meanwhile, the line connecting two points C and D in $\mathbb{P}^n$ is described set theoretically as $\{sC + tD \: | \: [s:t] \in \mathbb{P}^1\}$.
Careful use of these two equations, which appear in, say, Harris's Algebraic Geometry: A First Course, should give you your answer.


How to move/rotate a line segment intersecting another, just enough so that they become orthogonal?

Question: I'm trying to find a way to move or rotate a line segment intersecting another until they become orthogonal (the angle formed in the intersection is tuned into 90 degrees) while keeping the length of the line and the intersection point intact, a sample problem is displayed in the image below:
Sample problem
### Sample lines:
lineA = ([2, 4], [1, 1])
lineB = ([1, 1], [4, 1])
# Intersection point: (1,1)

In the sample our target would be the point "a1". Keep in mind while the angle in the sample above is less than 90 degrees there may be cases in which the angle may be more than 90. To make the problem simpler, suppose we know from before hand which line we need to modify (for the sample above, line "a").
I know how to calculate the angle between the lines but don't know where to go from here.
Appreciate your help.
P.S: Sorry for not making the image inline! I'm new here and one needs 15 points to be able to paste inline images.
Answer: You could calculate a transformation of your coordinate system that rotates lineA around it second endpoint until it is orthogonal to lineB.
A simpler approach is: the transformation will preserve the length of lineA and the position of one endpoint, but will wipe out other properties, such as the original direction of the line segment.
But the transformation must also add a property, that the transformed segment is orthogonal to lineB.
So instead of computing the transformation, we simply build the result with the known properties it must have:

Identify the common endpoint of lineA and lineB, call it $b_1$.
Identify the line through $b_1$ orthogonal to lineB.
Construct a line segment lying on the line in step 2, with one endpoint at $b_1$, the other endpoint at a distance from $b_1$ equal to the length of lineA.

In coordinates, the direction of lineB is represented by the vector
$[4,1] - [1,1] = [3,0],$ and an orthogonal line has the direction given by a vector orthogonal to $[3,0].$ To find a vector orthogonal to $[x,y]$ we can take $[-y,x]$ or any non-zero multiple of $[-y,x]$, including $[y,-x]$.
So let's take $[0,3]$ as the vector orthogonal to $[3,0].$
We need the vector from $b_1$ to the other endpoint of the new segment to have the same length as lineA, which is $\sqrt{(2-1)^2 + (4-1)^2} = \sqrt{10}.$
So we multiply $[0,3]$ (which has length $3$) by $\frac{\sqrt{10}}{3}$ to obtain a vector of length $\sqrt{10}$ in the same direction: $[0, \sqrt{10}].$
(Actually in this example we could have seen directly that the answer would be
$[0, \sqrt{10}]$ without working out the multiplication factor,
but the method used here will also work in the case where lineB is not parallel to a coordinate axis, when it is not so obvious how to substitute coordinates to obtain the desired length in the same direction.)
Finally we find the other endpoint of the new segment by adding the vector we just found to the coordinates of the known endpoint, $b_1$:
$$ [1,1] + [0, \sqrt{10}] = [1, 1 + \sqrt{10}] . $$
The rotated copy of lineA is therefore $([1,1], [1, 1 + \sqrt{10}]).$
There is one more detail you might like to enforce: to choose which of the two possible answers to produce. There are, after all, two places you could rotate lineA to so that it is orthogonal to lineB, one on each side of lineB.
If you want to minimize the amount of rotation you want to choose the answer on the same side of lineB as the original lineA.
To get the other possible result in this example, instead of simply adding
$[1, 1 + \sqrt{10}]$ to $b_1$ we multiply by $-1$ first and then add;
or equivalently, we simply subtract $[1, 1 + \sqrt{10}]$, with the result
$$ [1,1] - [0, \sqrt{10}] = [1, 1 - \sqrt{10}] . $$
If you are doing this graphically, you can eyeball the directions of lineB, lineA, and the vector you find orthogonal to lineB, and choose to add or subtract that vector according to whichever way produces a segment on the same side of lineB as lineA.
If you are doing this numerically without the aid of graphics and a human eye
(for example writing a simple computer program to solve the problem),
you first find a vector of the correct length orthogonal to lineB,
which vector may be in the correct direction or the opposite direction.
Then you can use the "dot" product of this vector with the vector from $a_2$ to $a_1$ to decide whether to add or subtract.
If the "dot" product is positive then the angle between vectors is less than $90$ degrees and you should add the new vector to $b_1,$
but if the "dot" product is negative than the angle is greater than $90$ degrees and you really want the "other" orthogonal vector, so you should subtract.
Comment: Thank you for a comprehensive answer.


Showing integer roots of a quadratic equation is countable?

Question:
Show that the set of real numbers that are the roots of quadratic equations $ax^2+bx+c=0$ with integer coefficients (i.e. $a, b, c\in\mathbb{Z}$) is countable.

My work so far:
$ax^2 +bx + c = 0$ can be written as $(mx+p)(nx+q) = 0$, where $mn = a$ and $pq = c$ and $pn + mq = b$. Then $x_1 = -p/m$ and $x_2 = -q/n$. $x_1$ and $x_2$, which are the roots of the polynomial, are countable since they essentially rational numbers. And I think we learned in class an infinite set of all rational numbers are countable.
My question: Does this approach make any sense ? If not, how can improve/change it to answer the question ?
                                                   Thanks for your time.
Comment: Hint: How many quadratic equations with integer coefficients are thee? How mayn real solutions are there at most per equation?
Comment: This approach doesn't quite make sense because not all quadratic equations with integer coefficients, such as $x^2 - 2$, can be factored into a product like there where the coefficients are rational (some quadratic equations wit integer coefficients do not have rational roots). Instead, you could try finding an explicit bijection: can you enumerate the polynomials? Then each polynomial has at most two roots....
Comment: $\#25$ of http://math.berkeley.edu/~isammis/55.S08/55PS2.pdf
Comment: @labbhattacharjee It's true, but the solution in that exercise set is IMHO awful - I would much prefer Hagen's answer, as it doesn't rely on the quadratic formula and thus generalizes effortlessly to the entirety of the algebraic numbers.
Answer: Your approach is wrong, not all the real numbers which are roots of such polynomials are rational numbers. For example $\sqrt2$ is the root of $x^2-2$, but is not a rational number.
HINT:

Show that the set of the polynomials of this form is countable,
Show that every polynomial of this form can have at most two roots,
Show that the countable union of finite sets [of real numbers] is countable.


Principal components analysis on nested data

Question: I'm working on a piece of analysis that requires identifying a small set of variables that summarize the variation found in a larger set of principal observations on teacher practice. Given the nature of the question, it seems natural to use PCA in my analysis. My data set however, has a hierarchical structure (observation occasions at the first level, and teachers at the second). So, I have the following questions:

What do I risk by doing PCA without considering this temporal effect?
What methods are there for accounting for the nested structure of data in PCA? Are there any useful tools in R?
Comment: Can you give an example of what you mean with 'nested data'.
Comment: Can you elaborate on what you mean by "temporal effect"
Answer: An important question is to evaluate how consistent the principal axes are across individual teachers. If they are mostly consistent, you can just use PCA on the whole dataset (ignoring teacher segregation) and be happy. If it happens that different teachers have fundamentally different axes of variance, then you likely have to reconsider the entire approach.
So the question I will try to answer is how to test if PCA are consistent across individuals. This is a solution I used some time ago, I have no guarantees that it is in any way optimal.
Step 1: Decide on the relative amount of explained variance $\alpha$ (e.g. 90%) that your PCA should cover
Step 2: For one teacher $i$, find the first $n$ PCA's that explain $\alpha$ variance. This will give you the eigenvalues $e_i$ and eigenvectors $V_i$. Importantly, the eigenvectors form the mapping that projects original data $x_i$ onto PCA space $\psi_{ii}$
$$\psi_{ii} = V_i^T x_i$$
Step 3: What we want to do is to calculate for another teacher $j$, how much of its variance would be explained by using the PCA space of teacher $i$. The prediction for original data given its PCA is
$$\hat{x}_{ii} = V_i\psi_{ii} = V_iV_i^Tx_i$$
and the relative explained variance is then
$$l^2_{ii} = \frac{||\hat{x}_{ii} - x_i||}{||x_i||} $$
where $||\cdot||$ is a 2-norm (aka euclidean distance). The same principle can be used to project the data of teacher $j$ onto basis of $i$
$$\hat{x}_{ij} = V_i \psi_{ij} = V_i V_i^T x_j$$
and the relative explained variance
$$l^2_{ij} = \frac{||\hat{x}_{ij} - x_j||}{||x_j||} $$
Step 4: Finally, we can repeat this procedure to obtain the matrix $l^2_{ij}$ for every pair of teachers $ij$. This matrix can be used to judge the extent to which a common PCA would capture individual variance, and find out detailed information on which clusters of teachers use similar variance axis, and which are outliers.
Answer: If, and only if, you are looking for the time-independent PCAs (meaning that the clustering caused by repeated measures is removed in a statistically appropriate way - using within-cluster resampling), you can try the method described here: DOI:10.1007/978-3-319-77249-3_18


Stub external HTTP requests in Java

Question: We are building a Java application which makes calls to 3rd party APIs like Facebook Graph API, Twitter API and processes data recieved. We have a problem mocking those calls in test cases. The HTTP call invocation happens in a completely different class's object (usually Java SDK provided by facebook or twitter). I come from a ruby-on-rails background, where we use stub_request in webmock to achieve similar functionality. Is there any similar framework in Java ? Any pointers, esp. examples are highly appreciated.
Thanks
Answer: You can use Mockito or any of Mocking framework available.
If you can change url protocol from http://facebook.com to testprotocol://facebook.com then you can also register your own URL handler e.g see http://www.webbasedprogramming.com/Java-Unleashed-Second-Edition/ch24.htm

Stub external HTTP requests in Java