Modular functionality with ASP.NET vNext Core CLR

Question: With ASP.NET 4.5 it is possible to use Assembly.Load() or AppDomain.CurrentDomain.Load() to dynamically load an assembly at runtime. This can be used to add new functionality to a running web application in the form of modules without having to rebuild or even restart the app.
I would like to know how this can be done with the new ASP.NET vNext (5.0?) targeting the Core framework. I know this can be done with the full framework as System.AppDomain is available along with an overloaded Assembly.Load(). When I change to target the Core framework, I no longer have System.AppDomain available and Assembly.Load() becomes limited.
Is there a way to get this dynamic modular functionality in the ASP.NET 5.0 Core framework?
Comment: Regardless of vNext, you may want to take a look at MEF (http://msdn.microsoft.com/en-us/library/dd460648%28v=vs.110%29.aspx).
Comment: Thanks. I have already considered MEF and it doesn't appear to be available in the core framework. Neither `System.ComponentModel.Composition` or `System.Compostion` appear to be available.
Comment: I have the same task now. Do you have any ideas ho to do that? How do you think, maybe we can use dynamic compilation instead of features like MEF?
Answer: I'm not sure what a good answer would be, because Asp.Net 5 is so new, and doesn't have a lot of full documentation.
It should theoretically be possible, it'll just be different than what you're used to.
Looking at the source there is an interface IAssemblyLoadContext which will allow you to get the assembly.   There is also IAssemblyLoader.
These are used by the "kre" host, which is the underlying piece of Asp.Net 5 that basically boostraps your application.  So you would need to add your IAssemblyLoader to the underlying host, so that the kre.
Unfortunately I'm not seeing very many extension points in the code as of yet.   Will we be getting those extension points?  I do not know.  You can log an issue on the github page.   It's also possible there is an extension point I'm not seeing currently.
To come back to the question, can you add that kind of extensiblity to Asp.Net 5 Core?  Yes, you could create your own host, similar to the default host, that would then include your custom loader, and then start your application with that.
Comment: Thanks David, this is a very different approach! I'll have a dig in the source and see what I can find.
Comment: I think there is already an [issue logged for this](https://github.com/aspnet/KRuntime/issues/500), so I guess it is  still to come
Answer: You can try ExtCore  framework
It allows to have modular and extendable ASP.NET 5 applications out of the box.
In short

It allows to use both modules from dlls and from nuget packages at the same time.
It allows all modules to work with storage in the single context and to have their own models and repositories.
Every extension can have controllers, views, static content etc.


How can I let others install my Google Apps Script?

Question: I've made a simple script in Google Apps Script. The script builds forms based on a spreadsheet, the script itself is part of the spreadsheet. It adds a menu item. People would like to use it. They are non-technical people, so I would like to make the installation of my script as easy as possible. With 'installation' I mean a way to 'import' my script into their spreadsheet and automatically set up the needed triggers so the menu item becomes visible. It should be possible for anyone to install it.
I've been thinking of the following solutions:

Publishing as an add-on: my script wouldn't qualify, and even then I can't wait for Google's approval.
Copy-paste. I don't like this, it's way too complicated.

I've looked at this question: https://stackoverflow.com/questions/5334751/how-do-i-share-a-script-i-wrote-with-my-co-workers-on-the-same-google-apps-accou, but the people wanting to install it are not on the same domain in my case. The question is also 3 years old.
EDIT: As it turns out, there doesn't seem to be a solution. I've filed a feature request  here: https://code.google.com/p/google-apps-script-issues/issues/detail?id=4122&thanks=4122&ts=1403949074
Comment: Re the add-on route, last I heard add-on's don't support programmatically adding triggers anyhow. Re by copy/paste I guess you mean copying the script itself. You can also offer a link to the spreadsheet to use as a master copy and they could copy that.
Answer: There is no way to import a script in an existing spreadsheet , the "normal" workflow is , in a way, the reverse process, that's to say create a sheet from an existing template that already has the script in it and start to work with that copy.
From your description I'm not sure this method could be useable but I'm afraid there is no other way if you want people who will be using it should not go into the script editor at all.
There would be a couple of functions to write that should run at install time and would create the triggers (if needed) and trigger the authorization process.
I made such a SS some time ago that needed authorizations and I added an "install" menu that made its use quite simple (example here on a friendly hosting site).
I know this is probably not the answer you were expecting but it was definitely too long to fit in a comment anyway.
Comment: This is problematic. My script is part of a spreadsheet automatically generated by a form. The workflow of people wanting to use my script would be to first create a form, and then the spreadsheet is created automatically. This way it is impossible to put my script in a template. Also,  I've made this script in order to fix a particular problem raised after the data was collected, so the spreadsheets are already there.
Comment: As I said: " I know this is probably not the answer you were expecting" couldn't you create the sheet using a template with script?
Comment: I know that this question will probably remain unanswered for some time, until Google fixes it. But no, it's not possible in my situation. My script is part of a spreadsheet, and the spreadsheet is automatically created by the form. Unless you could tell the form to use a certain template when creating a spreadsheet, the form will never include any scripts in the spreadsheets it creates. The form is always created first in this workflow.
Comment: It should be possible to tell the form to use a spreadsheet created from a template... I didn't try but I'm pretty sure you can. Could you provide a schematic structure of your workflow in pseudo code (or in real code) ? so I could provide a more concrete answer ... thx
Comment: You're right, it's possible. While that doesn't solve the problem for newly created spreadsheets, it does partly solve the problem. It is quite a lot of work though.
Scheme: http://i.imgur.com/YNrqRP0.png
Comment: I meant to say that would *not* solve the problem for already created spreadsheets.
Answer: If your Google Account administrator allows you, you can share scripts/spreadsheets outside your organisation either by sharing directly with another person with a Google Account (Gmail or Google Apps) or by making it public (share with a link or published online).
https://support.google.com/drive/answer/2494822?hl=en
Keep in mind that if you use the Script Properties to store values, each time someone uses the shared script it will overwrite the values. In a shared context it is best to use User Properties instead. This way each user can store their own values.
https://developers.google.com/apps-script/guides/properties
Comment: Also if the script is made available online (Web App), make sure you set it up to run as the users. https://developers.google.com/apps-script/execution_web_apps
Comment: I don't think this answers my question. I've edited my question to clarify.


Eclipse Code review for android

Question: I have installed plug-ins, checkstyle and PMD, its working fine for the sample project, "/SampleTestProject/src/TestPackage/TestClass.java" , But same plug-ins not at all showing any errors , not highlighting for project " /SampleProject/SampleDemo/Sample_src/SampleTest/src/com/Company/BusinessLogic/BusinessLogic.java"
is it because of folders loop ? Please suggest me the solution.
Comment: what  you have tried So far . its unclear
Comment: I have created a sample project , path looks like this:
"/SampleTestProject/src/TestPackage/TestClass.java",  here I can see warnings in "problems" window.

Same file " TestClass.java", I copied and pasted in another project , path is : "Test1/Test_loop/TestProject/SampleTestProject/src/TestPackage/TestClass.java"
here it will not show errors /warnings also.

(I just created folders loop and put this project)
Answer: Try this ,In eclipse right click the project ->checkstyle->activate checkstyle
Comment: checkstyle activation also doesnt make any sense.

Eclipse Code review for android


How to send formatted Rich Text Box as mail signature to some one in c# winform

Question: I have created an email signature using Rich Text Box. I want to sent this to my client through smtp protocol. I know the code to send smtp mail, but how to send this formatted text to the client as mail body?
Thanks in advance.
Answer: What you want to do is sending an email in html format:
MailMessage myMail = new MailMessage();
myMail.IsBodyHtml = true;
myMail.Body = "here your html text";

Use the System.Net.Mail namespace.
Comment: Thanks for the reply. It won't work that because I want to send formatted text which is on the Rich Text Box.
Answer: In order for the body to be formatted like your richtextbox text is formatted, you must write some logic in order to convert the text into html. when i say logic i mean that you must locate new lines, bold text, text color etc.. inside your richtextbox and convert it to html.
for example lets say that is the text inside your textbox:

Hello I AM ROBOT

you must write the logic in order to convert it to that string:
 Hello
 I AM ROBOT

after the text is embedded as html pass it to the .Body property of MailMessage class, also .IsBodyHtml property must be set to TRUE.
here is a a recommended tool for that task: Writing Your Own RTF Converter if you dont want to do it by yourself.
Comment: hi....i am facing a issue by using this method.its not allowing to convert and send embedded image
Comment: this is a completely different question. please publish a new question about you problem, maybe i can help you with this.
Comment: Sir I have already posted a question regarding this issue. but still no help. Please refer [This](https://stackoverflow.com/questions/45052216/send-embedded-image-in-html-body-by-using-rich-textboxs-rtf-to-html-converter-m)


Coordinate ring of general linear group

Question: Let $n$ be a positive integer and let $k$ be an algebraically closed field. What is the coordinate ring of $GL(n,k)$ (the set of all $n \times n$ matrices with entries in $k$)?  Here we identify this set as a subset of $k^{n^{2}}$.
Would it suffice to say that the coordinate ring is the localization of $k[x_{11},x_{12},..,x_{nn}]$ at the determinant function? Is there a way to "simplify" this?
Comment: Yes, that is precisely what the coordinate ring is. I think one can argue that this is the most simple presentation as follows: the dimension of $\textrm{GL}(n, k)$ is $n^2$, but it is not affine space, so it must have at least $n^2 + 1$ generators and at least one relation...
Answer: Maybe you already know this, but I think the most natural way to simplify it (if you think taking quotient is simpler than taking localization) is to view $GL(n,k)$ as a subvariety in $\mathbb{A}^{n^2+1}$: Now view $det$ as a polynomial $D(t_1,...,t_{n^2})$ of $n^2$ variables, then the coordinate ring is $k[t_1,...,t_{n^2},y]/(yD-1)$.
Comment: $D-y$ is wrong.  $y$ should be a multiplicative inverse of the determinant, not equal to it.
Comment: Why did you write $yD-1$ rather than $D-y$?

Coordinate ring of general linear group


Checking for overdispersion in a Poisson model

Question: I've run the following multilevel poisson regression:
> ambiglme4 <- glmer(AmbigCount ~ Posn.c*Valence.c + (Valence.c|mood.c/Chain), data = FinalData_forpoisson, family = poisson(link = "log"), control = glmerControl(optimizer = "bobyqa", check.conv.grad = .makeCC("warning", 0.05)))
> summary(ambiglme4)
Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']
 Family: poisson  ( log )
Formula: AmbigCount ~ Posn.c * Valence.c + (Valence.c | mood.c/Chain)
   Data: FinalData_forpoisson
Control: glmerControl(optimizer = "bobyqa", check.conv.grad = .makeCC("warning",      0.05))

     AIC      BIC   logLik deviance df.resid
   943.1    989.2   -461.6    923.1      726

Scaled residuals:
    Min      1Q  Median      3Q     Max
-1.0106 -0.4791 -0.2481 -0.0211  7.6527

Random effects:
 Groups       Name        Variance  Std.Dev.  Corr
 Chain:mood.c (Intercept) 0.000e+00 0.000e+00
              Valence.c   1.967e-12 1.402e-06  NaN
 mood.c       (Intercept) 0.000e+00 0.000e+00
              Valence.c   6.576e-13 8.109e-07  NaN
Number of obs: 736, groups:  Chain:mood.c, 92; mood.c, 2

Fixed effects:
                 Estimate Std. Error z value Pr(>|z|)
(Intercept)      -1.68128    0.11397 -14.752  < 2e-16 ***
Posn.c           -1.07585    0.08775 -12.260  < 2e-16 ***
Valence.c         0.89698    0.22795   3.935 8.32e-05 ***
Posn.c:Valence.c  0.47985    0.17550   2.734  0.00625 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation of Fixed Effects:
            (Intr) Posn.c Vlnc.c
Posn.c       0.831
Valence.c   -0.508 -0.461
Psn.c:Vlnc. -0.461 -0.454  0.831

I want to check for overdispersion in the model and came across the aods3 package and the gof function, which I used:
> gof(ambiglme4)
D  = 481.9449, df = 726, P(>D) = 1
X2 = 825.0443, df = 726, P(>X2) = 0.006089041

I'm just not sure exactly what the output represents and am having a hard time finding an answer. Is anyone able to help me understand how to interpret this? Also, how might I then report the dispersion in the model in a paper.
Answer: I am not familiar with those packages, nor the outputs. However, if you can identify the residual deviance and the residual degrees of freedom in the output, the two should be roughly equal or else over- or under-dispersion may exist.
The only function I am familiar with that tests dispersion is dispersiontest in the AER package. The output is minimal and very clear e.g. gives you what the dispersion value is at what p-value.
Answer: D is the residual degrees of freedom, X2 is the sum of squared Pearson residuals. The ratio of X2 to D (so, X2/D) should be near 1. In your caase, the ratio is near 2. So your model is clearly overdispersed.
You can also use:
library(glmmADMB)  ## 0.7.7
m2 <- glmmadmb(y~x+(1|f),data=d,family="poisson")
overdisp_fun(m2)

Which will give output like this:
    chisq        ratio    rdf             p
1026.7585    1.0298480    997     0.2499024

In this case, chisq = X2, rdf = D. But this output conveniently gives you the ratio.
Source: https://bbolker.github.io/mixedmodels-misc/glmmFAQ.html
Comment: Why should it be near one?
Comment: Recommenting on this because my previous (now deleted) answer was garbage.

So yes, >1 = overdispersed, <1 = underdispersed. Poisson distribution is defined by having one parameter that describes both the mean and variance. Thus, mean and variance must be equivalent, which of course is rarely true for any real data. If the ratio is >1, then it means the data are overdispersed and the variance is greater than the mean. If the ratio is <1, then it means the data are underdispersed, and the variance is less than the mean.

Is the ratio = 1, then it means the mean = variance.


Java DataStreams and Sockets

Question: I'm coding a distributed application in Java using .net libraries using sockets and I'm having trouble with the server and the client.
It's a very concrete case. I have a method that reads a String (airport code) and returns the Airport Data.
First I write the code of the operation and the parameters in a DataInputStream and then I send it to the server. On the server I get the parametres and call the method.
Here is the problem; The method can return null if the airport is not found.
How can I send back the null value to the client?
I have tried with:
If the value is null
then
   inputStream.writeUtf("Null")
   inputStream.flush()
endif

but when I tried to read the value in the client, sometimes I got EOFException
Here are both parts of the code, first the server part:
case com.codAeropuerto:  //aeropuerto(codigo : String): DatoAeropuerto

    String codigoAeropuerto = InStr.readUTF();

    DatoAeropuerto aeropuerto = espacioAereo.aeropuerto(codigoAeropuerto);

    if(aeropuerto==null){  //if the value returned is null
            OutStr.writeUTF("NULL");
            OutStr.flush();
    } else {
            OutStr.writeDouble(aeropuerto.coordX);
            OutStr.writeDouble(aeropuerto.coordY);
            OutStr.writeDouble(aeropuerto.coordZ);
            OutStr.flush();
    }

and the client part:
    try {
        sock=new Socket(server_dir,puerto_server);

        //send petition to server
        DataOutputStream outstr = new DataOutputStream(sock.getOutputStream());
        outstr.writeByte(com.codAeropuerto);
        outstr.writeUTF(codigo);
        outstr.flush();

        //return information
        DataInputStream instr = new DataInputStream(sock.getInputStream());

        if(instr.readUTF().equals("NULL")){
            sock.close()
            return salida;
        }

        double coordX = instr.readDouble();
        double coordY = instr.readDouble();
        double coordZ = instr.readDouble();

        salida = new DatoAeropuerto(coordX,coordY,coordZ);
        sock.close();

    } catch (IOException e) {
        System.out.println("Error EspacioAereo_Proxy aeropuerto : socket no se ha podido cerrar");

    } return salida;

Thank you in advance.
Comment: `return salida; sock.close();` — This one doesn't compile. `sock.close()` is an unreachable statement, and the compiler will complain about it.
Comment: @MCEmperor sorry, i adapted it on the go, its fixed now and I also added the sock.close() in the client part (if the value is null)
Answer: You need to send back a success/failure code, and only send the name next in the success case.

Java DataStreams and Sockets


Android Widget click doesn't do anything after few hours

Question: I have a widget that has a refresh button and a textview. Refresh updates the content and when user clicks on textview it starts a new activity.
Problem is it works fine for a few hours and then onclick and refresh button doesn't do anything. Nothing is captured in logcat. Also If user deletes widget and put a new one it starts working for a few hours and then the same story :(...what am I doing wrong!
Broadcast receiver.
onUpdate
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        long interval = getrefresInterval();

        final Intent intent = new Intent(context, UpdateService.class);
        final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
        final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarm.cancel(pending);

        alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending);

        // Build the intent to call the service
           RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget);

        // To react to a click we have to use a pending intent as the
            // onClickListener is excecuted by the homescreen application
            Intent ClickIntent = new Intent(context.getApplicationContext(),widgetHadith.class);
            Intent UpdateIntent = new Intent(context.getApplicationContext(),UpdateService.class);

            PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, ClickIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            PendingIntent pendingIntentUpdate = PendingIntent.getService(context.getApplicationContext(), 0, UpdateIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT); //use this to update text on widget. if use this put UpdateService.class to intent

            remoteViews.setOnClickPendingIntent(R.id.widget_textview, pendingIntent);
            remoteViews.setOnClickPendingIntent(R.id.widget_refresh, pendingIntentUpdate);

            // Finally update all widgets with the information about the click listener
            appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

            // Update the widgets via the service
            context.startService(intent);

    }

onReceive
public void onReceive(Context context, Intent intent) {

    // v1.5 fix that doesn't call onDelete Action
    final String action = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
        final int appWidgetId = intent.getExtras().getInt(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            this.onDeleted(context, new int[] { appWidgetId });
        }
    } else {
        super.onReceive(context, intent);
    }
}

onDelete
public void onDeleted(Context context, int[] appWidgetIds) {
    //  Toast.makeText(context, "onDelete", Toast.LENGTH_SHORT).show();
        super.onDeleted(context, appWidgetIds);
    }

Service onstart where I am updating
    @Override
    public void onStart(Intent intent, int startId) {
        RemoteViews updateViews = new RemoteViews(this.getPackageName(),R.layout.widget);

        processDatabase();
        Spanned text = LoadHadith();
        String hadith = text.toString();
        Log.d("BR", "service---> ");
        // set the text of component TextView with id 'message'
        updateViews.setTextViewText(R.id.widget_textview, text);

        //Push update for this widget to the home screen
        ComponentName thisWidget = new ComponentName(this, HelloWidget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, updateViews);
 }

Comment: Please submit code of the service which updates the widgets...
Answer: The problem is that you can't do a partiall update for a widget, you must set all the widget features, such as the set of PendingIntent's every time you push a new remoteView.
(Partiall updates are only available for API14 and up...).
The reason your widgets are loosing their pendingIntents is that the android system saves the remoteView, and rebuilds your widget with it, in case it resets the widget (shortage of memmory, TaskManager/taskKiller in use, etc...), so you must set all the update code for the widget in the remoteView in your updateService.
Otherwise, it's just won't set the pendingIntents again.
So just add the code setting the pendingIntents to the service and your problem will be solved =]
Comment: Thanks for the reply...I am trying solution and will update this post soon.


Mais de um verbo entre conceitos - Diagrama de Conceitos UML

Question: Como representar a situação em que um caso de uso apresenta mais de um verbo entre conceitos? Por exemplo:

O usuário acessa o sistema e pode ver as postagens de outros usuários e também pode criar uma nova postagem.

No caso os conceitos seriam: Usuário e Postagem; e os verbos seriam: Ver e criar.
Como representar isso no diagrama de conceitos UML?
Comment: _"Um caso de uso representa uma unidade discreta da interação entre um ator (humano, dispositivo ou outro software) e o sistema. Um caso de uso é uma unidade de um trabalho significante. Por exemplo: o "login para o sistema", "registrar no sistema" e "criar pedidos" são todos casos de uso."_  https://pt.wikipedia.org/wiki/Diagrama_de_caso_de_uso. Então, ver postagens e criar postagem são casos de uso distintos.
Answer: Olá,
Desculpa a minha dúvida mas só tenho trabalhado com os nomes em inglês e não conheço bem a terminologia em português: "diagrama de conceitos" corresponde ao "use case diagram"?
Se for esse o caso, tens de ter em conta que cada uso corresponde a um e um só verbo.
Isto permite-te que separar cada uso em módulos de código separados e independentes, com inúmeras vantagens para a análise (e desenvolvimento, se for esse o caso).
No exemplo apresentado tens três casos (adicionei o requisito em que o acesso é requerido para a leitura e criação): exemplo no PlantUML
<code>@startuml
    actor usuário as u

    usecase acessa as "acessa o sistema"
    usecase vê     as "vê postagem"
    usecase cria   as "cria postagem"

    u --> (acessa)  #line:red;line.bold;text:red  : requerido
    u --> (vê)
    u --> (cria)
@enduml
</code>
Comment: Diagrama de conceitos no meu entendimento mostra somente as classes e associações, formando o modelo conceitual do domínio, que depois evolui para diagrama de classes.
Comment: Estaremos a falar deste tipo de análise (USP) https://edisciplinas.usp.br/pluginfile.php/3720765/course/section/857605/Aula04_ModeloConceitual.pdf e https://www.devmedia.com.br/introducao-a-modelagem-conceitual/10793 ?

Se assim for, estaremos a falar de outros tipo de diagramas UML.
Comment: Entendo que o AP quis mapear um caso de uso "com mais de um verbo" para o modelo conceitual, por isso citou diagrama de conceitos. Mas caberia ter falado do diagrama de casos de uso, que é esse que você mostrou, com as setas. Confesso que não conhecia esse termo "diagrama de conceitos", não lembro de ser citado no Larman.
Comment: Segundo percebi (mas não conhecia este tipo de diagrama) das informações da USP acima, é um diagrama de classes simplificado.

Compreendo a ideia mas não sei se traz mais informações que o diagrama de classes normal.

Mesmo nesse caso, cada verbo (acção) distinto representa uma associação entre objectos (sujeitos).

Uma representação alternativa criaria dois actores "usuário" e "sistema de postagens", com três casos de uso : "acesso", "vê" e "cria".

Não sei estas ideias ajudam de algum modo?


Multiple of a Triangular Number is another Triangular Number

Question: I need to know if there are any necessary and/or sufficient condition(s) on the natural numbers $m$ and $n$ such that

$2T_n=T_{n+m}$, where $T_a$ is the $a$-th triangular number.

I have been able only to apply the formula $T_{a+b}=T_a+T_b+ab$ to the RHS to get $2T_n=T_n+T_m+mn \iff T_n=T_m+mn$. I've no more progress.

Any help will be appreciated.

This generalization is also much welcome:

$kT_n=T_{n+m}$, with $k\geq2$ is a natural number.
Answer: Here are some ways of making progress
If we simply use $2T_n=T_m$ we find that $$n(n+1)=\frac {m(m+1)}2$$ and if we multiply by $8$ this becomes $$2(4n^2+4n+1)=4m^2+4m+1+1$$ or, with $N=2n+1, M=2m+1$ $$2N^2=M^2+1$$ which is a classic Pell's equation - $N=5, M=7$ will do, for example.
With $kT_n=T_m$ we get $$k(4n^2+4n+1)=(4m^2+4m+1)+(k-1)$$ or $$kN^2=M^2+(k-1)$$
Note that there will always be multiples of any triangle number, because $n=2T_m$ gives $T_n=T_m(2T_m+1)$ and $T_{n-1}=T_m(2T_m-1)$
Answer: We have, $$2T_n = T_{n+m} \Rightarrow 2\frac{n(n+1)}{2} = \frac{(n+m)(n+m+1)}{2}$$ $$\Rightarrow 2n(n+1) = (n+m)(n+m+1)$$ $$\Rightarrow 2n^2 + 2n = n^2 + 2nm + m^2 + m + n$$ $$\Rightarrow m^2 + m+2nm-n^2-n=0$$ That gives us, $$m=\frac{-(1+2n)\pm \sqrt{(1+2n)^2-4(1)(-n^2-n)}}{2} = \frac{-(1+2n)\pm \sqrt{4n^2+4n+1+4n^2+4n}}{2} =\frac{-(1+2n)\pm \sqrt{8n^2 +8n+1}}{2}$$ We have to find the condition for which $8n^2+8n+1$ is a perfect square. Those values of $n$ satisfying this condition are given in the sequence $A029549$ in the OEIS.

Similarly, the sequence $A076140$ in the OEIS gives a list of those numbers which are $3$ times another triangular number. You can also see here for more information. Hope it helps.
Comment: Thanks. I tried this way. But I was stuck for a condition for $(8n^2+8n+1)$ to be a perfect square.


Can several suites of the same version of Visual Studio be installed Side by Side?

Question: I would like to know if I can install say Visual Studio 2008 Pro and Visual Studio 2008 standard on the same machine. I have tried to install the pro (trial edition available on MSDN) after the express, and it seems to require them to be installed in the same directory. Is this specific to the express edition ?
The reason why I am asking this is to support automatic detection of VS compilers on a given machine for a build tool (scons), and I am not so familiar with non express versions of VS. IOW, I don't want to run them side by side, I only need to know if I should handle side by side installation in my detection scheme.
Answer: I wouldn't recommend installing multiple editions of the same version side-by-side. I can imagine it playing havoc with the registry. If you just need to have all the different editions available for testing, I'd use VMs.
You can, however, install multiple editions (e.g. 2003, 2005 and 2008) side-by-side.
Comment: "I'd use VMs" - thanks for solving my 'Duh!' moment there. Obvious solution.
Answer: I know I have installed the express version and the professional version on the same machine.
You could also use Microsoft Virtual PC and install other versions on virtual machines.
Answer: You can install Visual Studio and each of the Express Editions side by side, but not a bunch of Visual Studios.
If you need to have them close, I would agree with the Virtual Machine idea.


Reference is calling base class methods?

Question: What's a reason that a child reference will call its parent's methods instead of its own? I have the parent and child both implementing virtual methods.
Problem --> In this sample of code initial.setup is calling State's setup. <-- Problem
void StateMachine::initialState(State& initial)
{
            initial.setup();
            currentStates.add(&initial);
}

void StateMachine::setup(Actor& actor, HashMap<State>& allStates)
        {
            _actor = &actor;
            _allStates = &allStates;
        }

Here is my Controller that gives the statemachine an initial state:
HashMap<State> _states; //This is defined in Controller
void PlayerController::setup(Actor& actor)
{
        //Factory* actorFactory = new Factory();
        //actorFactory->setup();

        _actor = &actor;
        _stateMachine.setup(actor, _states);

        DrinkState drinkState(actor, _stateMachine);
        MineState mineState(actor, _stateMachine);
        SingState singState(actor, _stateMachine);
        BrawlState brawlState(actor, _stateMachine);
        SleepState sleepState(actor, _stateMachine);
        IdleState idleState(actor, _stateMachine);
        CombatState combatState(actor, _stateMachine);

        _states.add(idleState.to_string(), idleState);
        _states.add(mineState.to_string(), mineState);
        _states.add(drinkState.to_string(), drinkState);
        _states.add(singState.to_string(), singState);
        _states.add(brawlState.to_string(), brawlState);
        _states.add(sleepState.to_string(), sleepState);
        _states.add(combatState.to_string(), combatState);

        //Put actor in idle state
        _stateMachine.initialState(_states.getValue(idleState.to_string())); //_states.getValue returns a reference
}

Relationship between States.
class State
    {
        protected:
            Actor* _actor;
            IStateCallback* _statemachine;
        public:
            State();
            State(Actor& actor);
            State(Actor& actor, IStateCallback& stateMachine);
            virtual void setup();
            virtual void breakdown();
            virtual void tick(float dTime);
            virtual void registerTransitionCallback(IStateCallback& statemachine);
            virtual void switchState(string nextState);
            virtual string to_string();
            virtual bool operator==(State& s);
            virtual ~State();
    };
class MineState : public State
    {
        private:
            int successChance;
            int maxGold;
        public:
            ~MineState();
            MineState(Actor& actor);
            MineState(Actor& actor, IStateCallback& statemachine);
            virtual void setup();
            void breakdown();
            virtual void tick(float dTime);
            void switchState(string nextState);
            string to_string();
    };

Hashmap's getValue function:
template<class T>
    T& HashMap<T>::getValue(string key)
    {
        int bucket = hashString(key);
        HashNode<T>* temporary = hashMap[bucket];

        while (temporary != NULL)
        {
            if (key.compare(temporary->getKey()) == 0)
            {
                return temporary->getValue();
            }

            temporary = &(temporary->getNextNode());
        }

        T *ret = NULL;
        return *ret;
        //return (T) NULL;
    }

Comment: Can you give us a complete, compilable, minimal example that illustrates the problem? It's quite hard to figure out what you're asking. There's no parent/child relationship in the code you pasted.
Comment: @user1932934 You made a `map` of `Parent`s, and then you tried to put `Child`s in them. You can't do that. That causes [slicing](http://en.wikipedia.org/wiki/Object_slicing), as I warned.
Comment: Sure I'll add that. Also made the problem a bit more visible.
Comment: You're giving us lots of irrelevant code and not the relevant code. There's probably some slicing going on somewhere, possible in `getValue`. We need complete code. The [basic idea works](http://codepad.org/DFJgV6Sc), of course.
Comment: Can you show us the function which is supposed to be called, namely `StateMachine::setup()`, and how it is declared in `StateMachine?
Comment: I added both StateMachine::setup(), and HashMap::getValue(). Hopefully this will be enough code.
Comment: @David Schwartz are you sure this even works in general? I tried with std:map, and it didn't work. http://codepad.org/xOPMOZwi
Answer: As far as I can see, the problem is that HashMap<State> contains, well, States. When you call:
_states.add(idleState.to_string(), idleState);

this is invoking the State copy constructor, and storing a base State in the hash table -- all the information about the derived class has been lost. You're a victim of slicing.
Since you can't store references in a container, you'll need to do something else instead. It's a bit ugly, but if you change the hash table to:
HashMap<State*> _states;

and then insert State pointers, you'll get the polymorphic behaviour you expect. (Of course, a smart pointer like std::unique_ptr could be used instead of raw pointers.)
Comment: That's how I originally had it, but my professor said it can be done without heap memory.
Comment: In this case, you could allocate your various `State` subclass instances on the stack somewhere where they will live at least as long as the `_states` hash table; as member variables of your `PlayerController` class would be a good option, if that is where `_states` itself lives. Then you still define `_states` as a `HashMap`, and add the addresses of your stack-allocated instances.
Comment: Hm, that would involve making them members of the class right? My professor doesn't want us to do that either :/.


I get Killed by signal 15. when I'm using svn

Question: I'm using svn+ssh and I see a number of:
Killed by signal 15.
during a svn up.
Any ideas way?
Comment: There are a large number of hits for http://www.google.com/search?q=svn+signal+15 looking through them there does seem to be a known bug for older versions of subversion that require you to update your .subversion/config http://old.nabble.com/svn%2Bssh----Killed-by-signal-15.-td26226917.html
Answer: The message you are seeing is printed by ssh as a result of the fix for svn-issue #2580.
This is expected. You need to add -q to the ssh command invoked by svn, which happens by default as of 1.6.6.
Put this in ~/.subversion/config under the [tunnels] section:
ssh = $SVN_SSH ssh -q

Comment: To make the solution work, you should put the line into [tunnels] section
Comment: Note that the fix applied to Subversion somewhere from about 1.6.5 onwards is only to the default `ssh` setting in your config `[tunnels]` section. If you have an existing installation with that set, e.g. `ssh = $SVN_SSH ssh -o ControlMaster=no`, (sans `-q`) then you will need to add that setting there, as the updated default will not apply.
Answer: The correct answer is:
Add the -q parameter after  "$SVN_SSH ssh" in ~/.subversion/config

In plain English:
If your ~/.subversion/config already has such a line, then edit the line. Else add it.
So, if you are sure there is no occurrence of ssh = $SVN_SSH ssh then add a new line:
ssh = $SVN_SSH ssh -q

If the line already exists, typically as ssh = $SVN_SSH ssh -o ControlMaster=no  then edit it to read ssh = $SVN_SSH ssh -q -o ControlMaster=no
Warning: The order of the parameters seems to matter. ssh = $SVN_SSH ssh -q -o ControlMaster=no works, but ssh = $SVN_SSH ssh -o -q ControlMaster=no fails with the message command-line: line 0: Bad configuration option: -q.
Comment: The order matters because 'ControlMaster=no' is a parameter of -o. -o is not a stand-alone flag
Answer:  ~/.subversion/config: ssh = $SVN_SSH ssh -q

This doesn't work - I think this is the fix for the previous bug.
[n@g ~]$ svn up /opt/
At revision 1492.
Killed by signal 15.
[n@g ~]$ grep "ssh =" ~/.subversion/config
ssh = $SVN_SSH ssh -q

Answer: If it isnt working perhaps you are invoking ssh using something other than the $SVN_SSH variable.  http://www.freebsdonline.com/content/view/764/528/ has an example of that.
regarding the warning:

"Warning: The order of the parameters seems to matter. ssh = $SVN_SSH ssh -q -o ControlMaster=no works, but ssh = $SVN_SSH ssh -o -q ControlMaster=no fails with the message command-line: line 0: Bad configuration option: -q."

that is because you placed the -q after the -o which is looking for the option (ControlMaster=no) specified by -o.  if you placed the -q after the option, it will work. ie
ssh = $SVN_SSH ssh -o ControlMaster=no -q


Binding variables in dynamic PL/SQL

Question: I have a dynamic PL/SQL that will construct the SELECT statement based on what the searching criteria input from the users,likes:
     l_sql := 'SELECT *  INTO  FROM TABLEA WHERE 1=1 ';

      IF in_param1 IS NOT NULL THEN
        l_sql := l_sql || 'AND column1 = in_param1 ';
      END IF;

      IF in_param2 IS NOT NULL THEN
        l_sql := l_sql || 'AND column2 = in_param2 ';
      END IF;
      ...................................

     IF in_paramXX IS NOT NULL THEN
        l_sql := l_sql || 'AND columnXX = in_paramXX ';
      END IF;

To reduce the hard parse overhead , I consider to use the binding variables. However , it is difficult to manage when supplying the actual values to the binding variables as there are so many binding variables and combination of the generated SELECT statement . I cannot use the method of DBMS_SESSION.set_context() introduced at http://www.dba-oracle.com/plsql/t_plsql_dynamic_binds.htm because my account has no right to use this package. Besides , I want the generated SQL only contains the conditions on the fields that the user did not leave empty. So I cannot change the dynamic SQL to something likes
SELECT *  INTO  FROM TABLEA WHERE 1=1
and ( in_param1 is NULL or  column1 = in_param1)
and ( in_param2 is NULL or  column2 = in_param2)
...............................................
and ( in_paramXX is NULL or  columnXX = in_paramXX)

So , I want to try  to use the DBMS_SQL method .Can anyone give an example about how to use DBMS_SQL to call dynamic SQL with binding variables? Especially , how can I get the result executed from DBMS_SQL.execute() to the SYS_REFCURSOR , something like :
open refcursor for select .... from

The oracle version that I use is 10g and it seems that the oracle 10g does not have DBMS_Sql.To_Refcursor()
Answer: In your Oracle version you can apply some tricks to your query in order to do this. The idea is to use a query in the following form:
select *
from
(select
 :possibleParam1 as param1
 -- do the same for every possible param in your query
 :possibleParamN as paramN
 from dual
 where rownum > 0) params
 inner join
-- join your tables here
on
-- concatenate your filters here
where
-- fixed conditions

then execute it with:
open c for query using param1, ..., paramN;

It works by using DUAL to generate a fake row with every single param, then inner joining this fake row to your real query (without any filters) using only the filters you want to apply. This way, you have a fixed list of bind variables in the SELECT list of the params subquery, but can control which filters are applied by modifying the join condition between params and your real query.
So, if you have something like, say:
create table people (
    first_name varchar2(20)
    last_name varchar2(20)
);

you can construct the following query if you just want to filter on first name
select *
from
(select
 :first_name as first_name,
 :last_name as last_name
 from dual
 where rownum > 0) params
 inner join
people
on
people.first_name = params.first_name;

and this if you want to filter on both first_name and last_name
select *
from
(select
 :first_name as first_name,
 :last_name as last_name
 from dual
 where rownum > 0) params
 inner join
people
on
people.first_name = params.first_name and
people.last_name = params.last_name;

and in every case you would execute with
open c for query using filterFirstName, filterLastName;

It is important for performance to use the where rownum > 0 with DUAL as it forces Oracle to "materialize" the subquery. This usually makes DUAL stop interfering with the rest of the query. Anyway, you should check the execution plans to be sure Oracle is not doing anything wrong.
Answer: In 10g a DBMS_SQL cursor can't be changed into a Ref Cursor. Going through a result set through DBMS_SQL is tortuous since, as well as looping through the rows, you also have to loop through the columns in a row.

I want the generated SQL only contains
  the conditions on the fields that the
  user did not leave empty

Is that purely for performance reasons ? If so, I suggest you work out what the practical execution plans are and use separate queries for them.
For example, say I'm searching on people and the parameters are first_name, last_name. gender, date_of_birth. The table has indexes on (last_name,first_name) and (date_of_birth), so I only want to allow a query if it specifies either last_name or date_of_birth.
IF :p_firstname IS NOT NULL and :p_lastname IS NOT NULL THEN
  OPEN cur FOR
    'SELECT * FROM PEOPLE WHERE last_name=:a AND first_name=:b AND
     (date_of_birth = :c or :c is NULL) AND (gender = :d or :d IS NULL)' USING ....
ELSIF :p_lastname IS NOT NULL THEN
  OPEN cur FOR
    'SELECT * FROM PEOPLE WHERE last_name=:a AND
     (date_of_birth = :c or :c is NULL) AND (gender = :d or :d IS NULL)' USING ....
ELSIF :p_dateofbirth IS NOT NULL THEN
  OPEN cur FOR
    'SELECT * FROM PEOPLE WHERE date_of_birth=:a AND
     (first_name=:b OR :b IS NULL) AND (gender = :d or :d IS NULL)' USING ....
ELSE
  RAISE_APPLICATION_ERROR(-20001,'Last Name or Date of Birth MUST be supplied);
END IF;

Binding variables in dynamic PL/SQL


Quando devo usar ou não o new?

Question: O que ocorre aqui? Por que se cria uma instancia de Date nesse exemplo:
String dataFormatada = new Date().format("dd/MM/yyyy")

Já nesse exemplo não se usou o new:
def data = Date.parse('dd/MM/yyyy', '31/12/1980')

Quando devo usar ou não o new?
Answer: Você precisa usar o new sempre for instanciar uma classe, ou seja, criar uma nova instância de determinado objeto. Nesse caso, quando você cria uma nova instância de Date ela já vem com a data atual.
No exemplo
def data = Date.parse('dd/MM/yyyy', '31/12/1980')

Você não está criando uma nova instância de Date e é por isso que não precisas usar o new. O parse é um método estático, que retorna um objeto do tipo Date. Nesse caso, ele recebe uma String e o formato da mesma e devolve (retorna) o Date referente.
Answer: @Aline, não se usa new em def data = Date.parse('dd/MM/yyyy', '31/12/1980'), pois Date#parse(String, String) é um método estático não um construtor, e, olhando a classe Date, esse método retorna uma instancia de Date retirada de um objeto Calendar pelo método Calendar#getTime.
Comment: O jbueno já editou a dele :D
Comment: Acho que se fizesse um merge dessa resposta com a do durtto ficaria bem completo :)
Answer:
String dataFormatada = new Date().format("dd/MM/yyyy")

A String dataFormatada está recebendo a data de hoje no formato "dd/MM/yyyy";

def data = Date.parse('dd/MM/yyyy', '31/12/1980')

data está recebendo o valor '31/12/1980'. Como já tem a data, não precisa do new.
new Date() vai retornar a data de hoje para você, e o format é para formatar do jeito que você quiser.
Comment: Métodos estáticos podem ser acessados diretamente da classe, sem ser de um objeto, @Aline.
Comment: aH, é só por isso?
Comment: Sim. New Date vai retornar a data de hoje para você. e o format é para formatar do jeito que você quiser.
Comment: entendi. Obrigada!
Comment: por nada. é um  prazer ajudar.
Comment: okay. Valeu. Isso vale para outros exemplos também? Como setar um nome de uma pessoa etc?
Comment: tem outra forma para eu falar com vc? durtto
Comment: @AlineGonz http://chat.stackexchange.com/rooms/11910/estouro-de-pilha
Comment: Aline, aguarde um momento. Não escolha ainda. Talvez lhe passamos um  contexto errado.
Comment: Eu acho que ficou um pouco superficial, talvez se você aprofundasse um pouco mais seria melhor
Comment: o segundo não usa new pois é um método estático da classe(static). Foi uma resposta que obtive sobre
 def data = Date.parse('dd/MM/yyyy', '31/12/1980')
por isso que não foi utilizado o new. e aí?
Comment: okay Gustavo, entendi.

Quando devo usar ou não o new?


Infinite scroll pager in Drupal 7

Question: I want to add infinite scroll page in search result pages. I am using Drupal 7.
To achieve it i followed this link.
When i do exactly as per the step given on this page, but i am not able to get it working.
Here is the HTML generated on my search result page.

Now to apply the pager in this page i made the following changes in the scripts given in the above link.

Remove .autopager class as i will have to add .pager class
Remove the imagesLoaded function as there are no images in the result

So after removal js will be like this. In his code i have added the .pager class. I am not able to get the pager and what should i write instead of viewsInfiniteScroll
   (function ($) {
  Drupal.behaviors.viewsInfiniteScroll = {
    attach: function (context, settings) {
      $(function(){
        var $container = $('div.pager');
      });
    }
  };
})(jQuery);

Answer: After lots of brainstorming and googling one user Marc from drupal.org suggested me the working solutions. Thanks to Marc for the solution.
Here is the solution:
To set it up, you will need a couple of steps:
REM: This method don't use the "Infinite Scroll Module", it's a rewrite of the stuff. In this method I choosed to integrate it in the theme, you could also create your own module (wish would be probably better .... )
1) download https://github.com/desandro/imagesloaded (jquery.imagesloaded.min.js) place it in your theme folder YOURTHEME/js
This will make sure all images are loaded before activate the infinite scroll
2) download https://github.com/paulirish/infinite-scroll (jquery.infinitescroll.min.js) place it in your theme folder YOURTHEME/js
In order to make it work with Drupal search modify it as follow: Search match(/^(.*?page=)2(\/.*|$)/ replace it by match(/^(.*?page=)2(&.*|$)
3) Create a new js (ex:infinitescroll.js) with the content of the below jquery & place it in your theme folder YOURTHEME/js
Rem: You will probably have to change the img path
4) modify YOUR_THEME.info and add

scripts[] = js/jquery.imagesloaded.min.js
scripts[] = js/jquery.infinitescroll.min.js
scripts[] = js/infinitescroll.js

5) flush the cache
6) cross finger & try .... it should work if not ... it's time to debug with firebug ...
Jquery code which you should place in infinitescroll.js
(function($){
    Drupal.behaviors.viewsInfiniteScroll = {
            attach: function (context, settings) {
              $(function(){
                if (this.processed) return;
                this.processed=true;
                if ($('.search-results').length==0) return;
                var $container = $('.search-results');
                $container.imagesLoaded( function(){
                  $container.infinitescroll({
                    navSelector  : 'ul.pager',    // selector for the paged navigation
                    nextSelector : '.pager-next a',  // selector for the NEXT link (to page 2)
                    itemSelector : '.search-results',     // selector for all items you'll retrieve
                    animate      : true,
                    msgText  : Drupal.t("Loading new results..."),
                    img: '/sites/default/themes/bartik/images/ajax-loader.gif',
                    donetext:Drupal.t('No more results to load.'),
                },function(arrayOfNewElems,state){
                    //YOUR CALLBACK STUFF .. To re-attach behaviour if needed

                    })
            })
          });
          }
        };
})(jQuery);

Comment: Helps a lot but I get an error : 'TypeError: EventEmitter is not a constructor
ImagesLoaded.prototype = new EventEmitter();'. If you had any idea about this it would be wonderful.
Comment: Something was messed up with imagesloaded.js. Made it work with this version : https://gist.github.com/mohit-rocks/5660610
Comment: Great !!! Thanks a lot brother..It's my GIT account :)
Comment: You're welcome. I still have a problem though : the last page keeps being loaded over and over again when I reach the end of the dataset. I never get the 'No more results found' message. Any idea ?
Comment: Don't have idea why it happens. Infact i was doing my free time, nowadays i am busy so get little time. Let's fix it together
Answer: Why not use Infinite Scroll or Views Infinite Scroll?
Answer: You can easily use Views Infinite Scroll module.
Or combine Views Load More with Waypoints modules.
Answer: You can use the GD Infinite Scroll module to add infinite scroll or load more button on non-view paged list, can also work with views.

Provide an administration to use the infinite scroll jQuery plugin : auto-pager on custom pages using drupal pager.

Infinite scroll pager in Drupal 7


Working out the number of automorphisms of a graph

Question: Take the complete graph with n vertices, where one edge has been removed. How can you work out the number of automorphisms that this graph has?
Answer: HINT: Let $G$ be the graph remaining after you remove the edge between vertices $u$ and $v$ in $K_n$, and let $V'=V\setminus\{u,v\}$, where $V$ is the vertex set of $K_n$. In $G$ the vertices $u$ and $v$ 'look alike' in every respect, and all of the vertices in $V'$ also 'look alike' in every respect. Thus, if $h$ is an automorphism of $G$, then $h$ must permute the set $\{u,v\}$ and the set $V'$. Conversely, any bijection from $V$ to $V$ that permutes both $\{u,v\}$ and $V'$ is an automorphism of $G$.
Can you take it from there?
Answer: Let the graph be $G=(V,E)$ with vertex set $$V=\{v_1,v_2,\ldots,v_{n-2}\} \cup \{u_1,u_2\}$$ and edge set $$E=\{v_i v_j : i,j \in \{1,2,\ldots,n-2\} \text{ and } i \neq j\} \cup \{v_i u_j: i \in \{1,2,\ldots,n-2\} \text{ and } j \in \{1,2\}\}.$$
The vertices $u_1$ and $u_2$ have degree $n-2$ whereas the vertices $v_i$ have degree $n-1$.  The degree of a vertex is an example of a vertex invariant; vertex invariants are preserved under isomorphism.  In this case, any automorphism must map a vertex of degree $d$ to another vertex of degree $d$.
Hence $\{v_1,v_2,\ldots,v_{n-1}\}$ and $\{u_1,u_2\}$ are preserved setwise by any automorphism.

Claim: Every $\alpha \in \mathrm{Sym}(\{v_1,v_2,\ldots,v_{n-1}\})$ is an automorphism.
Applying the isomorphism $\alpha$ gives a graph with edge set $$\{v_{\alpha(i)} v_{\alpha(j)} : i,j \in \{1,2,\ldots,n-2\} \text{ and } i \neq j\} \cup \{v_{\alpha(i)} u_{\alpha(j)}: i \in \{1,2,\ldots,n-2\} \text{ and } j \in \{1,2\}\}$$ which equals $E$ (since $\{1,2,\ldots,n-2\}$ is fixed setwise by $\alpha$).

Claim: The transposition $\beta:=(u_1,u_2)$ is an automorphism.
Applying the isomorphism $\beta$ gives a graph with edge set $$\{v_i v_j : i,j \in \{1,2,\ldots,n-2\} \text{ and } i \neq j\} \cup \{v_i u_{\beta(j)}: i \in \{1,2,\ldots,n-2\} \text{ and } j \in \{1,2\}\}$$ which equals $E$ (since $\{1,2\}$ is fixed setwise by $\beta$).

We conclude that any isomorphism which preserves $\{v_1,v_2,\ldots,v_{n-1}\}$ and $\{u_1,u_2\}$ setwise is an automorphism.
So the automorphism group of $G$ is isomorphic to $$\mathrm{Sym}(\{v_1,v_2,\ldots,v_{n-1}\}) \times \mathrm{Sym}(\{u_1,u_2\})$$ which has size $2\, (n-2)!$.


Why SOM is better than clustering technique(e.g. hierarchical)?

Question: I am using SOM for dimension reduction and visualization purpose (to put the same observations together). I am using kohonen r-package for the same.
https://cran.r-project.org/web/packages/kohonen/kohonen.pdf
For experimental purpose I took a 2-dimensional data (original):

Applied hierarchical clustering on original  data.
Applied SOM on original data and then applied hierarchical clustering on top of that.

I got similar results.
So my question is: Is preserving the topological distances only advantage behind using SOM over clustering?
If yes, then why this is important?
If no, what are other advantages of SOM over clustering?
Answer: You took a much too simple example.
If your input data is 2d, you don't need to use a SOM at all.
The purpose of a SOM is to put a 2d map over your data, even if that data is of higher dimensionality.
Apart from that, your question does not make much sense. You are comparing apples and oranges (SOM cannot be "better than clustering" because it solves a different problem). Furthermore, who says that SOMs are supposed to be better than anything?
Better comparisons may be SOM vs. tSNE, for example.
Comment: SOM itself doesn't do clustering. You can use kmeans somewhat on the mapped data. But that isn't very sound or reliable, do is ask but popular to use SOMs with clustering. It's more of a visualization thing.
Comment: I completely agree, it is a bad example. No need to use SOM for 2D data. As the SOM also used for clustering/segmentation, so I think I am comparing  apples vs apples. By clustering I meant clustering techniques.


Refactor and code placement

Question: My edit action has a series of radio buttons in the view.  I want to fill in the value of the currently selected field. I managed to get this to work, though I feel the code could be better and also perhaps should be in the model.
controller:
<code>      def edit
           @rating = Rating.find(params[:id])

       @a,@b,@c,@d,@e,@f,@g,@h,@i,@j = false
          if @rating.environ == 1
         @a = true
          elsif @rating.environ == 2
         @b = true
          elsif @rating.environ == 3
         @c = true
           elsif @rating.environ == 4
         @d = true
        .
        .
        .
        etc.
</code>
view:
<code>         1<%= f.radio_button :environ, 1, :checked => @a %>
         2<%= f.radio_button :environ, 2, :checked => @b %>
         3<%= f.radio_button :environ, 3, :checked => @c %>
         .
         .
        etc..
</code>
Comment: This should probably be on [Code Review](http://codereview.stackexchange.com/).
Answer: What is your model code? I am guessing a rating has many environs?
In any case, you can just loop through all of them in the view and make your :checked argument a boolean.
something like
<code> <% @environs.each do |env| -%>
   <%= f.radio_button :environ, env.id, :checked => (@rating.environ == env) %>
 <% end -%>
</code>
Comment: Environ is an attribute of rating.
Comment: Well my main concern is, I plan to have 10 attributes like this for @rating each with 10 instance variables and 10 elsif statements.  All this will mean 100 instance variables and 100 elsif statements in the edit method of the controller!
Comment: what do you need the instance variables and elsif statements for? i mean if they're all predicatble you can do a loop for that

Refactor and code placement


Print Options Do Not Open

Question: I have an html file in google script with JS, trying to print using the following code. It opens the document without the print options, I still have to do the cmd+P to print. Any idea, please?
function printBadge() {
        var attendee = document.getElementById("info").value;
        var fullName = "xxx";
        var snb = "xxx";
        var printFonts = "fonts";
        var printStyle = "style";
        var printArea = "badge info";

          w = window.open();
          w.document.write(printFonts + printStyle + printArea).html();
          w.document.close();
          w.focus();
          w.print();
          w.close();
         return false;
      }

Answer: The reason is this line w.document.write(printFonts + printStyle + printArea).html();. There is no function .html() associated to document.write and when you try to execute it, it fails there and so window gets stuck.
It needs to be w.document.write(printFonts + printStyle + printArea) without .html() function.
Hope it helps. Revert for any doubts.
Comment: Hi @Moustafa, if this answer has solved your question please consider [accepting](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. More here: [What should I do when someone answers my question](https://stackoverflow.com/help/someone-answers)

Print Options Do Not Open


Open explorer window and wait for it to close

Question: I have a program that is opening an explorer window to a certain folder but i want to perform an action right after the explorer window is closed, but if I use the following code:
Process proc = Process.Start("explorer.exe", "D:\\");
proc.WaitForExit();

It is opening the explorer window as desired but the WaitForExit command has no effect and it just goes right past it.
Is there a different way of opening the explorer window that will be able to let me know when it is closed by the user?
Comment: The problem is that there is one explorer.exe for all windows and the main desktop. Not sure how to fix it, but maybe you can enumerate the windows and find the right title, then wait for that window to close? Pretty error-prone though.
Comment: See this thread [from msdn](http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/8adbf55a-cae3-4168-88a9-56153c540e16). It looks like you can't if you are running explorer as your shell.
Comment: I can tell you that the wait for exit part isn't working because it's not really starting a new process. There's a setting somewhere to 'Open windows in a new process', or there used to be. Good luck.
Answer: The problem is explained pretty well at The Old New Thing:

The reason that WaitForSingleObject returns immediately is that Explorer is a single-instance program (well, limited-instance). When you open an Explorer window, the request is handed off to a running copy of Explorer, and the copy of Explorer you launched exits. That's why your WaitForSingleObject returns immediately.

He offers a couple solutions you could probably use (with some heavy use of PInvoke), like using something like this.
In the end it might just be easier for you to use some other type of file browser maybe from a C# library somewhere that you have more control over, rather than explorer.
Comment: +1 The real problem is that you need to be using a component that is designed to be under your control and not trying to piggyback the system wide shell process.
Answer: Cannot regenerate the error. Just tried this:
Process.Start("explorer.exe", "D:\\").WaitForExit();

and it blocks the current thread and waits until I close the explorer windows. Make sure that you're not executing the command on another thread than the one you want to block. Also make sure that you set every instance of a window to start a new instance of explorer.exe via importing below .reg file:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer]
"DesktopProcess"=dword:00000001
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer]
"DesktopProcess"=dword:00000001
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\BrowseNewProcess]
"BrowseNewProcess"="Yes"
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\BrowseNewProcess]
"BrowseNewProcess"="Yes"

You'll need to restart your computer for this to take effect.
Comment: The registry change did the trick. Thank you very very much for the info and for taking time to help me!
Comment: This solution sounds very costly to the system.


Discrete math induction problem.

Question: I am stuck at this step in the inductive process and I was wondering if someone can help me out from where I am stuck.
Question:
if $n$ is a positive integer, prove that,
$$1\cdot2+2\cdot3+3\cdot4+\cdots+n\cdot(n+1) = \frac{n(n+1)(n+2)}3$$
Basic step is $1$, then assume $k$ is true and we get,
$$1\cdot2+2\cdot3+3\cdot4+\cdots+k\cdot(k+1) = \frac{k(k+1)(k+2)}3$$
After that I am stuck because the answer key is telling me that the next step is to add $(k+1)(k+2)$, but I don't know why we do that. Aren't we supposed to just plugin $k+1$ into all $k$ variables. Why are we adding $(k+1)(k+2)$ to both sides?
Answer: Hint $\: $ It is very easy to inductively prove the following basic theorem on additive telescopy
$$\rm\ g(n)\ =\, \sum_{i\: =\: 1}^n\:\ f(i)\   \iff\  \  g(n) - g(n\!-\!1)\ =\ f(n)\ \ for\ \ n> 1,\,\ \ g(1)=f(1)$$
Your is special case $\rm\ f(n) = n(n\!+\!1),\ \ g(n) = n(n\!+\!1)(n\!+\!2)/3,\, $ which is easily checked to satisfy the above. Indeed, $\rm\,g(n)-g(n\!-\!1) = n(n\!+\!1)((n\!+\!2)-(n\!-\!1))/3 = n(n\!+\!1)\ $ and $\rm\ g(1) = f(1) $
The inductive proof of the general theorem is easier than that for your special case because the cancellation that occurs is much more obvious at this level of generality, whereas it is usually obfuscated by details of specific instances. Namely, the proof of the general theorem is just a rigorous inductive proof of the following telescopic cancellation
$$\rm \underbrace{\overbrace{g(1)}^{\large f(1)}\phantom{-g(1)}}_{\large =\  0}\!\!\!\!\!\!\!\!\!\!\!\!\!\overbrace{-\,g(1)\!+\!\phantom{g(2)}}^{\large f(2)}\!\!\!\!\!\!\!\!\! \underbrace{g(2) -g(2)}_{\large =\ 0}\!\!\!\!\!\!\!\!\!\!\!\!\!\!\overbrace{\phantom{-g(2)}+ g(3)}^{\large f(3)}\!\!\!\!\!\!\!\!\!\!\underbrace{\phantom{g(3)}-g(3)}_{\large =\ 0}\!+\:\cdots +\!g(n)\ =\ g(n) $$
This theorem reduces the inductive proof to simply verifying the RHS equalities, which is trivial polynomial arithmetic when $f(n),g(n)$ are polynomials in $n$. The above theorem is an example of telescopy, also known as the Fundamental Theorem of Difference Calculus,   depending on context. You can find many more examples of telescopy and related results in other answers here.
Comment: Interesting! Do you happen to know why this is called 'telescoping'?
Comment: @drhab The cancellation is analogous to collapsing the sections of a telescope down into a single section. As displayed above, when you rebracket the summands (sections) $\,f(i) = g(i) - g(i\!-\!1),$ the entire sum collapses to $\ 0 + \cdots + 0 + g(n) = g(n),\,$ the last "section". $\ \ $
Answer: I see lot's of good math here, but your repeated question of "Why are we adding (k+1)(k+2)?" leads me to believe you're looking for something a little different. (Of course, I could be completely wrong. If so, then pardon my rambing here.)
Try this:
Induction proofs often start with a formula that looks like
EQ $1.$ $\ $ term$_1$ + term$_2$ + ... + term$_n$ = $f(n)$
where $f(n)$ is a relatively simple function of $n$. We prove true for $1$, which is usually trivial, then assume it holds for $k$, so we have
EQ $2.$ $\ $ term$_1$ + term$_2$ + ... + term$_k$ = $f(k)$
Then we're supposed to prove it's true for $k+1$. OK, here's what no one is explicitly telling you. When you want to prove it's true for $k+1$, we take advantage of the fact that we know what those terms that make up the left-hand side of EQ $1$  look like, and that if we add the $k+1$ term to both sides of EQ $2$, then the new left-hand side looks like the first half of what we're trying to prove:
$\ $ term$_1$ + term$_2$ + ... + term$_k$ + term$_{k+1}$  = $f(k)$ + term$_{k+1}$
So, if we can do the algebra and rearrange that right hand side so it looks like $f(k+1)$ is supposed to , then we've got our proof. Here's where you plug $k+1$ into $f()$, to see what you're trying to manipulate that right-hand side to look like. If we can use algebra (or some other method) to show that
$f(k)$ + term$_{k+1} = f(k+1)$
then we've shown that adding the $k+1$ term makes the sequence equal to $f(k+1)$, which is what it means for the formula to be "true" for $k+1$.
Once you understand this, then the real trick to any of these is finding the right cool algebra trick to rearrange $f(k)$ + term$_{k+1}$ to get what you want, which is why so many of the answers you're getting focus on that aspect. It's the common question implied when someone says they don't get a particular induction proof.
(Note: This is a simple model with additive terms. Sometimes the pattern on the left is more complex and you have to figure out what you have to do to both sides to get from the $k$ to the $k+1$ case.)
(Also Note: This is just a verbose and somewhat less generalized case of what some of the above answers already say. However, I find that often students don't translate the symbology as readily as we "experts" (heh), especially in the early stages of learning a new concept, so I thought one more exposition from another tact might help.)
Answer: So we want to prove that
$$1\cdot2+2\cdot3+3\cdot4+\cdots+n(n+1)=\sum_{i=1}^{n}{i(i+1)}=\frac{n(n+1)(n+2)}{3}\forall n\in\mathbb{N}$$
The base case ($n=1$):
Note that $1\cdot2=2$ and that $\frac{1\cdot2\cdot3}{3}=2$. Thus the base case holds.
Suppose that $\exists k\in\mathbb{N}$ such that $\sum_{i=1}^{k}{i(i+1)}=\frac{k(k+1)(k+2)}{3}$. Now consider the case that $n=k+1$:
$$\sum_{i=1}^{k+1}{i(i+1)}=(n+1)(n+2)+\sum_{i=1}^{k}{i(i+1)}$$.
By the induction hypothesis (we claimed that for $n=k$ the formula held) we have:
$$\sum_{i=1}^{k+1}{i(i+1)}=(k+1)(k+2)+\frac{k(k+1)(k+2)}{3}=\frac{k^3+6k^2+11k+6}{3}=\frac{(k+1)(k+2)(k+3)}{3}$$
Therefore, by induction, the formula is true $\forall n\in\mathbb{N}$
Comment: How come you add ∑i=1ki(i+1) at the k+1 part?
Comment: I split up the sum to $k+1$. I split it into the sum up to $k$ and then the $(k+1)^{\text{st}}$ term, which is $(k+1)(k+2)$. I split the sum up so I can apply the induction hypothesis, that the sum up to $k$ is equal to $\frac{k(k+1)(k+2)}{3}$. Does that make sense?
Comment: Yup thanks for clarifying
Answer: Finishing up the induction proof,\begin{align}
1 \cdot 2 + 2 \cdot 3 + 3 \cdot 4 + \cdots + (k+1)(k+2) &= [1 \cdot 2 + 2 \cdot 3 + 3 \cdot 4 + \cdots + k(k+1)]+(k+1)(k+2) \\
&= \frac{k(k+1)(k+2)}{3}+(k+1)(k+2) \\
&= \frac{k(k+1)(k+2)}{3}+\frac{3(k+1)(k+2)}{3} \\
&= \frac{k(k+1)(k+2)+3(k+1)(k+2)}{3} \\
&= \frac{(k+1)(k+2)(k+3)}{3}
\end{align}
where the last equality is by hypothesis. Now simplify the last expression accordingly and you will get your result.
Comment: Why are we adding (k+1)(k+2)?
Comment: If you combine the last equality in my answer, you'll actually factor out $(k+1)(k+2)$ as it appears on both terms. You will get $$\frac{(k+3)[(k+1)(k+2)]}{3}$$ which is your desired result.
Answer: Based on hypothesis: $$\sum_{i=1}^{k}i\left(i+1\right)=\frac{1}{3}k\left(k+1\right)\left(k+2\right)$$
it must be shown that: $$\sum_{i=1}^{k+1}i\left(i+1\right)=\frac{1}{3}\left(k+1\right)\left(k+2\right)\left(k+3\right)$$
Here $k+1$ is 'plugged in' for $k$.
That is the inductionstep. How to do that? Well, $$\sum_{i=1}^{k+1}i\left(i+1\right)=\sum_{i=1}^{k}i\left(i+1\right)+\left(k+1\right)\left(k+2\right)=\frac{1}{3}k\left(k+1\right)\left(k+2\right)+\left(k+1\right)\left(k+2\right)$$
(in the last equality you are using the hypothesis)
So if you can show that the RHS of this indeed equals: $$\frac{1}{3}\left(k+1\right)\left(k+2\right)\left(k+3\right)$$
then you are ready. Working out the RHS comes to adding $(k+1)(k+2)$ to $\frac{1}{3}k\left(k+1\right)\left(k+2\right)$.
Answer: You suppose that your formula is true for $k$ and prove that it is true for $(k+1)$. That is, suppose that:
$$
1\times2+\cdots+k\times(k+1)=\dfrac{k(k+1)(k+2)}{3},
$$
and prove for $(k+1)$:
$$
1\times2+\cdots+k\times(k+1)+(k+1)\times(k+2)=\dfrac{(k+1)(k+2)(k+3)}{3}.
$$
Note that by assumption you have:
$$
1\times2+\cdots+k\times(k+1)=\dfrac{k(k+1)(k+2)}{3}.
$$
Hence:
$$
\begin{equation}
\begin{split}
1\times2+\cdots+(k+1)\times(k+2)&=\dfrac{k(k+1)(k+2)}{3}+(k+1)\times(k+2)\\&=\dfrac{k\color{red}{(k+1)(k+2)}+3\color{red}{(k+1)(k+2)}}{3}\\&=\dfrac{\color{red}{(k+1)(k+2)}(k+3)}{3}.\mathrm{QED}
\end{split}
\end{equation}
$$

Discrete math induction problem.


Non-parametric test if two samples are drawn from the same distribution

Question: I would like to test the hypothesis that two samples are drawn from the same population, without making any assumptions about the distributions of the samples or the population. How should I do this?
From Wikipedia my impression is that the Mann Whitney U test should be suitable, but it does not seem to work for me in practice.
For concreteness I have created a dataset with two samples (a, b) that are large (n=10000) and drawn from two populations that are non-normal (bimodal), are similar (same mean), but are different (standard deviation around the "humps.") I am looking for a test that will recognize that these samples are not from the same population.
Histogram view:

R code:
a <- tibble(group = "a",
            n = c(rnorm(1e4, mean=50, sd=10),
                  rnorm(1e4, mean=100, sd=10)))
b <- tibble(group = "b",
            n = c(rnorm(1e4, mean=50, sd=3),
                  rnorm(1e4, mean=100, sd=3)))
ggplot(rbind(a,b), aes(x=n, fill=group)) +
  geom_histogram(position='dodge', bins=100)

Here is the Mann Whitney test surprisingly (?) failing to reject the null hypothesis that the samples are from the same population:
> wilcox.test(n ~ group, rbind(a,b))

        Wilcoxon rank sum test with continuity correction

data:  n by group
W = 199990000, p-value = 0.9932
alternative hypothesis: true location shift is not equal to 0

Help! How should I update the code to detect the different distributions? (I would especially like a method based on generic randomization/resampling if available.)
EDIT:
Thank you everybody for the answers! I am excitedly learning more about the Kolmogorov–Smirnov which seems very suitable for my purposes.
I understand that the KS test is comparing these ECDFs of the two samples:

Here I can visually see three interesting features. (1) The samples are from different distributions. (2) A is clearly above B at certain points. (3) A is clearly below B at certain other points.
The KS test seems to be able to hypothesis-check each of these features:
> ks.test(a$n, b$n)

        Two-sample Kolmogorov-Smirnov test

data:  a$n and b$n
D = 0.1364, p-value < 2.2e-16
alternative hypothesis: two-sided

> ks.test(a$n, b$n, alternative="greater")

        Two-sample Kolmogorov-Smirnov test

data:  a$n and b$n
D^+ = 0.1364, p-value < 2.2e-16
alternative hypothesis: the CDF of x lies above that of y

> ks.test(a$n, b$n, alternative="less")

        Two-sample Kolmogorov-Smirnov test

data:  a$n and b$n
D^- = 0.1322, p-value < 2.2e-16
alternative hypothesis: the CDF of x lies below that of y

That is really neat! I have a practical interest in each of these features and so it is great that the KS test can check each of them.
Comment: This is a good example of the confusion we sow when we are not clear about the hypothesis for a test.  Unfortunately, people are taught to use a _t_-test to compare two groups, without really thinking that this test compares two _means_, whereas there's a median test to compare two _medians_, Mann-Whitney that compares something else, quantile regression to compare other percentiles, tests to compare variances, Kolmogorov-Smirnov to compare the distributions, and so on...  We sometimes just say we want to compare two "populations" without being clear which hypothesis we really want to test.
Comment: The hypothesis for Mann-Whitney is sometimes said to concern "location" of the two groups, or something along the lines of systematic stochastic difference.  In the case of your data, both groups are symmetrically distributed around 75, so M-W should definitely not find a difference.
Comment: It is not too surprising that MW does not reject. For a one-sided test it tests whether Pr(a>b) < 0.05 where a and b are randomly chosen members of your populations.
Comment: On reflection it seems like the Wikipedia page for the M-W test states the hypothesis very clearly and it was a misunderstanding (unfounded leap) on my part to think that this hypothesis also implies that the samples come from the same distribution. Indeed, the problem becomes obvious when comparing two different distributions that are symmetric around the same central point.
Answer: The Kolmogorov-Smirnov test is the most common way to do this, but there are also some other options.
The tests are based on the empirical cumulative distribution functions. The basic procedure is:

Choose a way to measure the distance between the ECDFs. Since ECDFs are functions, the obvious candidates are the $L^p$ norms, which measure distance in function spaces. This distance is our test statistic.
Figure out the distribution of the test statistic under the null hypothesis that the samples come from the same distribution (luckily people have done this already for the most common distances!)
Choose a threshold, $\alpha$, for your hypothesis and reject the null if the computed test statistic is in the $\alpha \%$ tails of the distribution from point 2.

For the Kolmogorov-Smirnov test, the test statistic is the maximum distance between the two empirical CDFs (or if you want to be more technical the $L^\infty$ norm). This is super easy to implement in R:
ks.test(a,b)

If the $p$-value is smaller than your chosen threshold, we reject the null hypothesis that the samples are drawn from the same distribution.
Another option is the Cramer-von Mises test, which uses the squared $L^2$ norm as the test statistic and is implemented in the dgof package as cvm.test(). The CVM test is 'better' in the sense that the distance metric takes into account the whole of the two ECDFs, rather than just picking out the largest distance.
EDIT:
Suppose we have samples of size $n$ and $m$, which we want to apply our hypothesis test to.
To turn this into a sampling-type procedure, we can do the following:

Generate samples of size $n$ and $m$ from identical distributions. For the KS test (remarkably, IMO) it doesn't even matter if the distribution changes at each iteration as long as $n$ and $m$ stay the same.
Calculate your distance metric for the samples. For the KS test, this is just the max. difference between the empirical CDFs.
Store the result and go back to step 1.

Eventually you will build up lots of samples from the distribution of the test statistic under the null hypothesis, whose quantiles you can use to conduct your hypothesis test at whichever level of significance you want. For the KS test statistic, this distribution is called the Kolmogorov distribution.
Note that for the KS test, this is just a waste of computational effort because the quantiles are very simply characterised theoretically, but the procedure is generally applicable to any hypothesis test.
Comment: @JamesAdamCampbell could you expand on that a bit in a different answer?
Comment: @LukeGorrie, how to implement [the Kolmogorov-Smirnov test](https://en.wikipedia.org/wiki/Kolmogorov%E2%80%93Smirnov_test) is a separate question -- and one that might be better posted elsewhere (e.g., on Stack Overflow).  The basic recipe is: (a) understand how the test works, (b) implement that.  In this case it seems particularly straightforward: 1. compute the ECDFs (easy, by just taking prefix sums), 2. compute the $L_\infty$ norm (easy, by just taking the max difference).  It looks pretty straightforward.
Comment: I don't see any problem with that. I'd be interested to see the results if you try some of this stuff! Would be cool to see whether the CI approach and straight up KS test always give you the same answer. I suspect they do :)
Comment: Thank you! The Kolmogorov-Smirnov test does indeed reject the null hypothesis that these samples are from the same population. And intuitively it makes sense to compare the ECDFs because that is more-or-less what I am doing visually with the histogram.

Question: Suppose that I needed to implement this test from scratch without any tools like R. Is there a simple method that would suffice? (Perhaps based on bootstrapping?) I ask because my background is computer programming and I find simulation-based methods much easier to really understand.
Comment: You should look into randomization or permutations.  I prefer these for non-normal tests.  They also meet ur criteria of being simulation rather than statistics
Comment: @Will: I'll try to post a full answer later tonight!
Comment: D.W. Really I want to understand if this test can be treated as a special case of some general randomization/resampling method. Sounds like the answer is "yes" if we add step 3. to bootstrap a confidence interval and reject the null hypothesis if that doesn't span zero?
Comment: @Luke Gorrie see my edit, is that the sort of thing you're after?
Comment: @Will Thank you. This helps me to understand the way the test works. I am still wondering if there is a formulation based on bootstrapping though. Could we alternatively pass the statistic function (e.g. Kolmogorov distance) to a standard bootstrapping procedure like `boot.ci` to repeatedly check the distance between resampled copies of `a` and `b` to see how often the result is zero? Or is that simply invalid for some reason? (Maybe zero is the wrong reference since it would be rare for random chance to place the _whole_ ecdf for one sample underneath the other from the same population?)
Comment: I made an edit to show the nifty things I am discovering about hypothesis testing based on ECDFs. Thanks for putting me onto this track!
Comment: @Will I don't see how you can say the KS test can be used to test if two samples are from the same distribution. If the p-value is less than the threshold, we can reject the null hypothesis and say the samples are from different distributions. But if the p-value is above the threshold we can't 'accept the null hypothesis' that the samples are from the same distribution. So it seems the KS test can be used to test if samples are from different distributions, but not the same distribution, unless I've overlooked something.
Answer: The Kolmogorov-Smirnov test is a good start, but building on the same intuition there exist now more powerfull tests.
I would you refere to this, which gives a overview over tests for exactly your purpose.
It also has nice ilustrations and explainations whow they are calculated.
The DTS Test is now recomanded.
Also see the companion paper at arXiv:2007.01360 or here for details of this test statistic.
Comment: Hi, welcome to CV. Please provide references for the links in case they die in the future.


Adding values to defined byte (DB)

Question: I want to do something like this:
a db ?

Then, I want to add values to it, let's for example say it'd end up like this:
a db 1,2,3,4,5,6

Is that possible?
Answer: If you know the maximum number of bytes you'll write, yes:
.data?
a db 6 dup(?)

.code
mov byte ptr [a],1
mov byte ptr [a+1],2
...
mov byte ptr [a+5],6

If you don't know that number (or if it's really large) you'll have to use one of the memory allocation functions provided by the target operating system.
Comment: I don't exactly know the number of bytes I'm going to insert, but it shouldn't be more than 4-5. I thought of doing by saving to a defined memory address like this: `address dw 3000h` `mov si, dw` `mov ds:[si], (value)`. Will it work?
Comment: Using hardcoded addresses is typically a bad idea. There's no guarantee that your program even has permissions to write to memory at the address you pick.
Comment: Then I will use what you have suggested `a db 6 dup(?)`. Thanks a bunch!

Adding values to defined byte (DB)


How to delete file that is created using Uri?

Question: My app takes photo from camera and saves it in a file whose Uri is stored in SQL database. Initializing bitmap using Uri from database works flawlessly. However, when i try to initialize a file using Uri from database and then delete using imagefile.delete()it does not work. I have tried few methods given in other posts but none worked.
This i how i save file.
Intent for Camera:
Intent startCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if (startCamera.resolveActivity(getPackageManager())!=null)
                    {
                        photoFile= null;
                        try{
                            photoFile = createImageFile();
                        }
                        catch (IOException ex)
                        {
                            Log.e("File Creation","error");
                        }
                        if (photoFile!=null)
                        {
                            photoURI = FileProvider.getUriForFile(context,"lcukerd.com.android.fileprovider",photoFile);
                            startCamera.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
                            camerastarted=true;
                            startActivityForResult(startCamera,CAMERA_REQUEST);
                        }
                    }

Declared method:
private File createImageFile() throws IOException
{
    String EName = "Stuff";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(EName,".jpg",storageDir);
    return image;
}

Compressing image:
photoFile.delete();
FileOutputStream out = null;
                    try {
                        File image = createImageFile();
                        photoURI = FileProvider.getUriForFile(context, "lcukerd.com.android.fileprovider", image);
                        out = new FileOutputStream(image);
                        photo.compress(Bitmap.CompressFormat.PNG, 100, out);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if (out != null) {
                                out.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

saving Uri to database is simple conversion of Uri to string and then saving in TEXT column of table.
Help me delete image file .
Edit:
I tried following.
private void deleteimage(String imageloc)
    {
        File imagefile = new File(getRealPathFromURI(Uri.parse(imageloc)));
        Log.d("file deletion",String.valueOf(imagefile.delete())+" "+imageloc);
    }
    public String getRealPathFromURI( Uri contentUri) {
        String result;
        Cursor cursor = getContentResolver().query(contentUri, null, null, null, null);
        if (cursor == null) { // Source is Dropbox or other similar local file path
            result = contentUri.getPath();
        } else {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            result = cursor.getString(idx);
            cursor.close();
        }
        return result;
    }

and..
private void deleteimage(String imageloc)
    {
        File imagefile = new File(Uri.parse(imageloc).getpath());
        Log.d("file deletion",String.valueOf(imagefile.delete())+" "+imageloc);
    }

None worked.
Comment: `delete using imagefile.delete()it does not work.` doesn't tell us much.
Comment: check edit @Rotwang
Comment: Yes, but why do you get? A run-time error? Permissions? The file simply stays there? Or is it just still listed in the database because you forgot to remove the entry from the db table?
Comment: imagefile.delete(); returns false . nothing else shown .File is still there @Rotwang
Answer: Since this is a Uri that you are getting from FileProvider, call delete() on a ContentResolver, passing in the Uri (plus null for the remaining parameters). That will delete the underlying file, at least according to the FileProvider documentation.
Or, store the file path, rather than the Uri, in your database, since this is your file. You can recreate the Uri later as needed using FileProvider.getUriForFile(), and you can use delete() on File to delete the file, given its path.
Comment: I don't understand . What should i initialize FileProvider variable with? This doesn't work.   FileProvider imagefile =  new FileProvider();
imagefile.delete(Uri.parse(imageloc),null,null);
Comment: @lcukerd: The first paragraph of my answer says to call `delete()` **on a `ContentResolver`**. You get a `ContentResolver` by calling `getContentResolver()` on some `Context` (e.g., an `Activity`).
Comment: Thanks again, it worked . I almost forgot about ContentResolver even though I used it in another activity :(


TypeError: data type 'datetime' not understood

Question: I am trying to upload a huge csv file using this script below but getting error
header = ["SKU","STORAGE_AREA","MOVE_TYPE","ORDER_NO","ORDER_ITEM","PICK_VOL","M_UNIT","DATE"]
d_type = {"SKU":"str","STORAGE_AREA":"str","MOVE_TYPE":"str","ORDER_NO":"category","ORDER_ITEM":"str","PICK_VOL":"int","M_UNIT":"str","DATE":"datetime"}
product = pd.read_csv('pick_data.csv', encoding='latin-1', sep=',', index_col=False, header=None, names=header, dtype=d_type)

error:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [68], in <cell line: 3>()
      1 header = ["SKU","STORAGE_AREA","MOVE_TYPE","ORDER_NO","ORDER_ITEM","PICK_VOL","M_UNIT","DATE"]
      2 d_type = {"SKU":"str","STORAGE_AREA":"str","MOVE_TYPE":"str","ORDER_NO":"category","ORDER_ITEM":"str","PICK_VOL":"int","M_UNIT":"str","DATE":"datetime"}
----> 3 product = pd.read_csv('pick_data.csv', encoding='latin-1', sep=',', index_col=False, header=None, names=header, dtype=d_type)

File C:\ProgramData\Anaconda3\lib\site-packages\pandas\util\_decorators.py:311, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs)
    305 if len(args) > num_allow_args:
    306     warnings.warn(
    307         msg.format(arguments=arguments),
    308         FutureWarning,
    309         stacklevel=stacklevel,
    310     )
--> 311 return func(*args, **kwargs)

File C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers\readers.py:680, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options)
    665 kwds_defaults = _refine_defaults_read(
    666     dialect,
    667     delimiter,
   (...)
    676     defaults={"delimiter": ","},
    677 )
    678 kwds.update(kwds_defaults)
--> 680 return _read(filepath_or_buffer, kwds)

File C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers\readers.py:575, in _read(filepath_or_buffer, kwds)
    572 _validate_names(kwds.get("names", None))
    574 # Create the parser.
--> 575 parser = TextFileReader(filepath_or_buffer, **kwds)
    577 if chunksize or iterator:
    578     return parser

File C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers\readers.py:933, in TextFileReader.__init__(self, f, engine, **kwds)
    930     self.options["has_index_names"] = kwds["has_index_names"]
    932 self.handles: IOHandles | None = None
--> 933 self._engine = self._make_engine(f, self.engine)

File C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers\readers.py:1235, in TextFileReader._make_engine(self, f, engine)
   1232     raise ValueError(msg)
   1234 try:
-> 1235     return mapping[engine](f, **self.options)
   1236 except Exception:
   1237     if self.handles is not None:

File C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py:74, in CParserWrapper.__init__(self, src, **kwds)
     64 for key in (
     65     "storage_options",
     66     "encoding",
   (...)
     70     "warn_bad_lines",
     71 ):
     72     kwds.pop(key, None)
---> 74 kwds["dtype"] = ensure_dtype_objs(kwds.get("dtype", None))
     75 self._reader = parsers.TextReader(src, **kwds)
     77 self.unnamed_cols = self._reader.unnamed_cols

File C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py:416, in ensure_dtype_objs(dtype)
    411 """
    412 Ensure we have either None, a dtype object, or a dictionary mapping to
    413 dtype objects.
    414 """
    415 if isinstance(dtype, dict):
--> 416     return {k: pandas_dtype(dtype[k]) for k in dtype}
    417 elif dtype is not None:
    418     return pandas_dtype(dtype)

File C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py:416, in <dictcomp>(.0)
    411 """
    412 Ensure we have either None, a dtype object, or a dictionary mapping to
    413 dtype objects.
    414 """
    415 if isinstance(dtype, dict):
--> 416     return {k: pandas_dtype(dtype[k]) for k in dtype}
    417 elif dtype is not None:
    418     return pandas_dtype(dtype)

File C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\dtypes\common.py:1777, in pandas_dtype(dtype)
   1774 # try a numpy dtype
   1775 # raise a consistent TypeError if failed
   1776 try:
-> 1777     npdtype = np.dtype(dtype)
   1778 except SyntaxError as err:
   1779     # np.dtype uses `eval` which can raise SyntaxError
   1780     raise TypeError(f"data type '{dtype}' not understood") from err

TypeError: data type 'datetime' not understood

tried to change data types several times but still error
Answer: Try add parse_dates=['DATE'] into your pd.read_csv like below, and avoid dtype=d_type.
pd.read_csv(r'path', parse_dates=['DATE'])

Or you can add  converters={'DATE': lambda t: pd.to_datetime(t)} to your pd.read_csv and I guess with this you can use dtype=d_type.
Answer: Try to avoid 'datetime' type in the pd.read_csv()
After you read the file, you can try this method:
product['DATE'] = pd.to_datetime(product['DATE'])


Infinite tower of algebraic extensions

Question: For all I know, the following fact should be true: Consider an infinite tower of extensions $L_0 \subset L_1 \subset L_2 \subset \cdots$ such that $L_{i + 1} / L_i$ is algebraic for all $i \in \mathbf{N}_0$. Then $\bigcup_{n \geq 0} L_n$ is algebraic over $L_0$.
However, I'm not sure how one would prove this. I was thinking to proceed by induction: The base case is okay by definition. Now assume that the claim holds for $i - 1$. Any $\alpha \in L_i$ can be written as a root of a polynomial in $L_{i - 1}[X]$. Here comes the point where I don't have any idea how to proceed. Could someone help me out?
Thanks!
Answer: An element of the union is an element of one of the $L_i$, thus is in a finite chain of algebraic extensions, thus is algebraic over $L_0$.

Infinite tower of algebraic extensions


MainWindow.Closing event not always raised in Silverlight 4 OOB app

Question: I've made a rather complex Silverlight 4 out-of-browser application. One of my main view models adds an event handler to the Application.Current.MainWindow.Closing event.
This works fine when the application is initially run. It is able to cancel the close operation.
However, sometimes after performing operations like showing and closing a ChildWindow, the MainWindow's Closing event is no longer calling my handler.
In the debugger, I added a watch to the MainWindow's underlying closing event delegate. It's not null before showing the ChildWindow. Then sometimes after the ChildWindow is closed the delegate is null. This is explains why my handler is not called any more. But why is this delegate getting nulled? And why is it only happening occasionally?
My application is not unbinding my event handler at any point.
This is the delegate I'm watching:
System.Windows.Application.Current.MainWindow.m_closingEvent

Other stuff: I'm using Caliburn Micro
Comment: Sounds like a really good question.  First diagnositic I would use is create a simple OOB app (no caliburn etc) that simply opens and closes ChildWindows and swaps out RootVisuals to see if a simple repro can be created.
Comment: Ok, I have the same problem - as soon as I don't open/close the ChildWindow then the Closing event fires. Sometimes if I have the debugger attached it will also fire but not always. I tried the ApplicationWrapper below with no joy. Any suggestions?!
Answer: I had the exact same problem. We have a large silverlight application running OOB.
For some reason the m_ClosingEvent was nulled after running for a while. I have not been able to find the cause of this issue but I think it may have something to do with us changing the root visual or all the child windows we show.
I´m using a class ApplicationWrapper.
public class ApplicationWrapper : IApplicationWrapper
{
  public void Initialize()
  {
    HookCloseEvent(true);
  }
  private void HookCloseEvent(bool hook)
  {
    if (hook && IsRunningOutOfBrowser)
    {
      Application.Current.MainWindow.Closing += OnClosing;
    }
    else
    {
      if (IsRunningOutOfBrowser)
      {
        Application.Current.MainWindow.Closing -= OnClosing;
      }
    }
  }
  private void OnClosing(object sender, ClosingEventArgs e)
  {
    InvokeClosing(e);
  }

... etc..
}

And the InvokeClosing method was never called. But when I changed it to
public class ApplicationWrapper : IApplicationWrapper
{
  private Window _mainWindow;

  public void Initialize()
  {
    if(IsRunningOutOfBrowser)
    {
      _mainWindow = Application.Current.MainWindow;
    }
    HookCloseEvent(true);
  }

  private void HookCloseEvent(bool hook)
  {
    if (hook && IsRunningOutOfBrowser)
    {
      _mainWindow.Closing += OnClosing;
    }
    else
    {
      if (IsRunningOutOfBrowser)
      {
        _mainWindow.Closing -= OnClosing;
      }
    }
  }

  private void OnClosing(object sender, ClosingEventArgs e)
  {
    InvokeClosing(e);
  }

... etc...
}

The m_ClosingEvent isn´t nulled.
So, try to just store the "initial" MainWindow in a field and check if that solves your problem.
Comment: I just tried this out and it seems to work! I will need to test on a few more machines to be sure, but it looks like you've solved it (and solved it simply!) Thank you you so much! I'll mark this as the answer and award your bounty soon.
Comment: I can't get this to work - where is the IApplicationWrapper class? It does not seem to be in Silverlight 4 and Google returns very little information on it?
Comment: There is not IApplicationWrapper interface in the framework. Its just an interface I wrote to enable dependencyinjection and unittesting for whatever is dependent on the applicationwrapper. But if you dont care about that you could just copy the application wrapper and Initialize it from you mainwindow I guess.
Answer: Instead of hooking to the event, why not register a service instead? Create a class that implements IApplicationService and IApplicationLifetimeAware. The latter gives you an "onexiting" and "onexited" pair of events. You place the service in the application by pointing to it in a section called  in your App.xaml. I've used this for many projects and never had an issue with the exiting methods not being called.
Comment: Thanks for the answer, but I need the ability to cancel the exiting event (after prompting the user). That interface doesn't seem to provide an option for that.
Comment: +1, Not because its an answer to the problem at hand but because its just a tidy thing to do.  Also its just too easy to forget App lifetime objects so its nice to be reminded, you don't have to do everything in App Start and Exit.
Comment: +1, been looking for a neat solution that that problem. Cheers.
Comment: Thanks for sharing, however, I also can't Cancel the Exit service - I can't find the OnExiting events that you mention so there's no way to cancel?
Comment: Ok, I played around with this on the weekend - here is my conclusion: IApplicationWrapper does not exist in my project libraries so that does not help. There's no OnExiting event in the IApplicationService so I can't cancel my exit using this method. My MainWindow closing event never fires. All I want to do is ask the user if they are sure when they click on the Window Close button when running OOB... there seems to be no way to do that still....
Answer: Ok, after pulling out my hair and many false starts I finally found the answer - it seems to be a known bug with the Closing event, OOB and ChildWindows open/closes...
The trick is to store a static reference to the Main Window:
public MainPage()
{
    InitializeComponent();
    Loaded += MainPage_Loaded;
}

private void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
    //you have to store this to work around the bug
    //http://forums.silverlight.net/forums/p/185664/424174.aspx
    _mainWindow = App.GetApp.MainWindow;

    App.GetApp.MainWindow.Closing += (s, e1) =>
    {
        if (UIUtilities.ShowMessage("Would you like to exit AMT Mobile?", "Exit Application", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
        {
            e1.Cancel = true;
        }
    };
}

Comment: Thanks @Rodney but I don''t see where you use _mainWindow? Unfortunately the links to silverlight.net work not anymore.
Comment: Also App.GetApp.MainWindow; GetApp is unknown?