R - Clustering and Multidimensional Scaling

Question: I am trying to get to grips with Clustering and Visualisation.
I have a set of data (a matrix) that I want to cluster (using R) and then visualise (HTML5 Canvas).
So, I can use MDS to get the coordinates of a matrix, for example:
<code>cells <- c(1, 1, 2, 1, 4, 3, 5, 4)
rnames <- c("A", "B", "C", "D")
cnames <- c("X", "Y")
x <- matrix(cells, nrow=4, ncol=2, byrow=TRUE, dimnames=list(rnames, cnames))
d <-dist(x)
m <- cmdscale(d,eig=TRUE, k=3)
m <- cmdscale(d,eig=TRUE, k=2)
print(m)
</code>
But, should I first cluster this data (using something like k-means) or should I cluster the output of MDS?  Or can I just cluster (and get the coordinates using some other method) and ignore MDS?
What is the relationship between MDS and K-means, if any?
I am unsure what is the best way to approach?
Any suggestions?
Thanks,
s
Comment: Why do you think you need MDS - your data matrix are already pairwise distances rather than original cases X variables?
Comment: Thanks for the comment.  I think this is what I am trying to figure out?  So, if I cluster (suing kmeans for example) I should get a pairwise matrix in return, how do I access the coordinates (or points) of that matrix.  This is probably a very rudimentary question but I just want to make sure I understand it correctly. Thanks again.
Answer: Basically, with MDS you create a 2D map for your data with one icon for one data point. With clustering algorithm you color icons with different colors (or cluster labels). MDS and clustering algorithm should be used independently from each other. It does not make much sense to apply clustering algorithm on the X,Ys created with MDS.


How to kill a single request in IIS?

Question: I'm using IISPeek and can see that there's a single request that's hanging on a buggy page. I've fixed the bug so that others who open the page won't experience the problem, but the page is still active after almost an hour.
I'd rather not stop the whole application as there are some important processes currently running.
Via IISPeek I have the request number (9f0002008001238e) and clientIP. Could any of that be used to stop the ongoing request?
Answer: You can use Process Explorer to do this. Go into the Properties of the problematic w3wp.exe process, choose the Threads tab, then select the long running thread and use the Kill button to kill the thread. Note too that you can also use the Stack button to get the stacktrace of the thread, to hopefully help determine where in code it is getting hung up.
Answer: You can't stop an individual request that's hung. You need to recycle the App Pool to do that.
Alternatively, break in with a debugger and terminate the request thread. If the thread's hung, there's no way for it to voluntarily self-terminate.
Keep in mind that when the debugger breaks in, the whole process is paused. And if you don't .detach before quitting, the process will quit, which functionally resembles a recycle anyway.
Extra step: Hope nothing breaks when you do that.
Answer: Some solution: kill network connection of the server, no need to restart IIS.
Comment: That's a possibility, but it would still disrupt the currently running important requests.

How to kill a single request in IIS?


Some users can't load my website. Possible DNS or routing issue?

Question: I have a website that serves people around the US. I host the web site from a single web server.
Today, some people have claimed they cannot load my website, and I am wondering how likely this is some sort of internet routing issue or DNS issue.
How would you go about figuring out exactly what the issue is? Preferably, I do not want to ask the users to use ping or nslookup since they mostly run Windows boxes and I don't believe those are installed by default.
Comment: `Preferably, I do not want to ask the users to use ping or nslookup since they mostly run Windows boxes and I don't believe those are installed by default.` Those two tools are installed on every current version of Windows that I can think of. Perhaps you should do a bit more basic troubleshooting?
Comment: @MarkM I'd love to do more basic trouble shooting, what would you suggest?
Comment: It would help to know what their browser reports when the website doesn't load (e.g. _Connection timeout_, _Connection reset_, etc). This requires very little effort from your users and you might notice a pattern there. This could help you a lot in isolating the problem to a component (routing, dns, firewall, ...).
Comment: You could start [here](http://www.thewebsiteisdown.com/)
Answer: A word of caution: Users will report that the "website is down" for all manner of issues not actually related to your service at all. Independently confirm that your site is up (ask a friend, use another server you own), then immediately suspect their own networks. I'll second the DNS check with a link of my own: What's My DNS? Who runs your DNS? Make sure the server is authoritative for your domain and double-check the nameservers.
Ping and nslookup are definitely available on Windows boxes. Ask them to ping you and perform an nslookup on your hostname and something almost guaranteed to be accessible via a correctly-configured connection, like google.com. Try to find a pattern between their responses.
Comment: Hell, I've had someone report the website being down when they meant that it looked different due to a minor redesign.
Comment: +1; I've had reports of 'I can't get to my email', and after wasting time checking that their mailbox is okay, I find out that what they actually meant was that their PC that wouldn't boot! Having said that, the question states 'some people', could 'some people' be two people at the same office, or thousands of users in different locations?
Answer: Check your DNS with an online tool like http://www.intodns.com/
Comment: Website does not open for me
Answer: Some things I do to check on potential site issues when reported in rough order:

Test Load Pages Myself: While this is far from guaranteeing everything is working it is still a quick check, especially if the issue reports are for a specific page. Try logged in and logged out, dynamic/static, public/admin pages if applicable.
Check Monitoring: If you have a service of any importance some sort of monitoring service is incredibly useful for a variety of reasons. I can check server loads, memory, disk usage, traffic, etc... across multiple servers at a glance and, with practice, notice small issues before they become big issues. I use Zabbix but there are many others depending on your needs. Chances are, if there is a problem I'll already have several monitoring e-mails before anyone notices.
Check Load: In lieu of monitoring, especially with just a single server, you can check the basic status your server using top. Check for a high load, high CPU usage, high IO Wait, and any VM usage. A "high" load depends on the server/application but generally anything over 10 is probably too high and 2-10 something to be checked. As you become more familiar with your server and traffic you'll begin to know what is good/bad.
Check Memory: Use top, free -m or vmstat to check your VM usage. Any significant VM usage is a bad thing and indicates something is using more memory than it should be.
Check Disk: Check disk usage with df. Sometimes a full disk or tmp partition can manifest itself in strange ways. Disk errors/failures can be checked with smartctl or in the system log files.
Check Traffic: With Apache I usually just check the server-status page and see what is being served. netstat can also be used to see the number/type of connections but takes some experience to know what to look for and what is normal and not.
Check Logs: Especially with intermittent issues by the time you check the server the issue may no longer exist. Even with monitoring checking the various log files is essential in tracking down issues. Check /var/log/messages and various other logs in /var/log along with any application specific logs (Apache, database, etc...) and look for relevant error messages.

When ever I get someone saying "the site is down" how quickly I react/panic depends on the content and number of the report. I'll probably ignore some just saying "the site is down" but someone who posts a detail report with error code/message will get me moving faster, as will multiple reports.
Answer:
Ping and nslookup (and tracert) are available on every Windows OS by default.
Because the problem might be isolated to a specific customer or geographic region I would suggest that you do have the affected customer(s) run nslookup (first) to make sure your web site resolves correctly from their location and then run tracert to the ip address of your web site or network ingress (firewall/router).
Answer: If you cannot recreate the issue then you absolutely have to ask users to help debug the problem. They are usually very happy (if they have the time) to help. Approach them with a customer service hat on.
Tell them about www.downforeveryoneorjustme.com and guide them through basic pings and traceroutes. Find out if it's DNS, routing, or server problem.
Without this you'll be taking random stabs in the dark without knowing if the issue is fixed at all - it's frustrating for you and for your users. Bite the bullet and get in touch with them, your question will be answered in minutes.


Use Spark when function without otherwise but keep column values

Question: I frequently find myself replacing values in columns using
when($"myCol".isNull,myCrazyFunction).otherwise($"myCol")

To me the .otherwise($"myCol") feels kind of redundant.
Is there a better way to replace some values under some condition and otherwise just leave everything as it is without using the otherwise?
Answer: I think you can use coalesce() for that.
select(coalesce($"myCol", myCrazyFunction))

Just remember that myCrazyFunction should return a Column type.


can't start tomcat on vps - java VM error "could not reserve enough space for object heap"

Question: I can't start tomcat6 on my VPS (which has 256 MB, 512 MB burst and uses currently only 20MB of it). the log entry shows:
<code>Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.
</code>
i read that i can set the min and max heapsizes for the java with the -Xms and -Xmx
what do i need to do that tomcat will pass these parameters to java when trying to start the service?
Comment: ps -ef | grep tomcat should tell you if it's up, otherwise have a look in the /usr/share/tomcat6/logs files for anything obvious.
Comment: thank you. `[root@mabulous bin]# ps -ef | grep tomcat
root     13739 32480  0 17:44 pts/1    00:00:00 grep tomcat
` does that mean it's running?
Comment: you was right - there was an error, the java VM doesn't want to start - see my edited problem description
Comment: Mat, I think your question is now well-defined enough to be used in a Google search. Good luck!
Answer: edit /etc/default/tomcat6 and add in -Xms and -Xmx values to java opts there.
eg:-
    JAVA_OPTS=" -Xmx256m"
Comment: perfect - thanks. the actual path was `/etc/tomcat6/tomcat6.conf`


Confusion in model representation

Question: Will the functional form of a Moving Average model having coefficients h = [1 0.2 -0.5] be
y(t) = 0.2e(t-1) - 0.5e(t-2)

Or
y(t) = 1+ 0.2e(t-1) - 0.5e(t-2)

Answer: There is also the current error term,
so
$$y_t = e_t + 0.2e_{t-1}-0.5e_{t-2}$$
Comment: While this is flagged because of its brevity, I think a short answer covers it in this case.
Comment: @Glen_b I left the question for a day, believing that someone would type this obvious answer. No one did, and I didn't want yet another question left in the unanswered pile just due to its triviality, so I posted this as an answer rather than as a comment.
Comment: I think you're right to do so. You answer was flagged (presumably automatically by a script), I voted it as okay, and left that comment so other people contemplating the issue would see my reasoning (and hopefully agree that it is okay as it is).
Comment: @AlecosPapadopoulos: Thank you for your answer. But I have a question which is that the 1 value in h stands for $e_t$ or is it just a convention to write 1? What if h =[0.5 0.2 -0.5] instead of having 1?
Comment: The values in the $h$ vector are the values of the coefficients of the corresponding error term.

Confusion in model representation


Google Cloud Run - Container failed to start workarounds

Question: Similarly to Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable I cannot start my container because it does not (need to) listen on a port. It is a Discord bot that just needs outbound connections for the APIs.
Is there a way I can get around this error? I've tried listening on port 0.0.0.0:8080 using socket module with
import socket

s = socket.socket()
s.bind(("0.0.0.0", 8080))
s.listen()

Answer: Cloud Run is oriented to request-driven tasks and this explains Cloud Run's listen requirement.
Generally (!) clients make requests to your Cloud Run service endpoint triggering the creation of instances to process the requests and generate responses.
Generally (!) if there are no outstanding responses to be sent, the service scales down to zero instances.
Running a bot, you will need to configure Cloud Run artificially to:

Always run (and pay for) at least one instance (so that the bot isn't terminated)
Respond (!) to incoming requests (on one thread|process)
Run your bot (on one thread|process)

To do both #2 and #3 you'll need to consider Python multithreading|multiprocessing.
For #2, the code in your question is insufficient. You can use low-level sockets, but it will need to respond to incoming requests and so you will need to implement a server. It would be simpler to use e.g. Flask which gives you an HTTP server with very little code.
And this server code only exists to satisfy the Cloud Run requirement, it is not required for your bot.
If I were you, I'd run the bot on a Compute Engine VM. You can do this for Free.
If your bot is already packaged using a container, you can deploy the container directly to a VM.
Comment: I was trying to set up a VM but even if I've set up the configuration as the [free tier](https://cloud.google.com/free/docs/free-cloud-features#free-tier-usage-limits) suggests, it still shows that the VM will be charging a monthly amount. I am worried that when the doc says "Only available in the specified region" it means "Your Google's account region" rather than "Your VM region"
Comment: For Google Cloud resources "Regions" always applies to resources. I think (!?) there isn't a concept of regions for Google accounts. There *may* be costs associated with running resources on the Free Tier, it is good to be diligent. You will be able to run a VM for free just ensure you meet the [requirements](https://cloud.google.com/free/docs/free-cloud-features#compute)


I want my homepage to be replaced when I click on a link to another page, but instead, the other page is added to my home

Question:
I am trying to add a second page to my web app, but instead, the second page's HTML is added to my homepage. Why?
app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { OtherPage } from './app.other-page';

const routes: Routes = [
  { path: 'otherpage', component: OtherPage },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

app.component.html

It's true!

Go to Other Page

<router-outlet></router-outlet>

app.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  title = 'angular-test';
}

app.other-page.html

This is the other page

app.other-page.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.other-page.html',
})
export class OtherPage {
  title = 'angular-test';
}

In the example code: the "Go to other page" link is still on the page when clicked, while I'd rather the other page consist of only app.other-page.html.
Comment: Working as expected, it only renders where you have `router-outlet` on your `app.component.html`
Comment: @penleychan How would I go about making them seperate? (new to stackoverflow so if I am breaking conventions let me know).
Comment: *Also* in the tutorial [I suggested](https://stackoverflow.com/questions/57045997/i-want-to-use-ngif-with-javascript-variables-is-this-possible-how-so#comment100620058_57045997) you read: https://angular.io/tutorial/toh-pt5
Answer: In your example, When you navigate to "/otherpage", the content of the component OtherPage will be displayed instead of router-outlet.
That means that everything that is around router-outlet will be always displayed. It makes sense to put the header before router-outlet.
For more informations, you could read the angular router documentation. https://angular.io/guide/router


Passar dados de um DataGridView para TextBoxes no C#

Question: Olá, estou tendo um problema e já até fiz uma pergunta parecida anteriormente, mas agora estou sendo mais específico.
Tenho dois forms, um form possui um DataGridView conectado a um Banco de Dados e o outro form servirá como edição para os dados dos registros mostrados naquele DataGridView.
Para que isso ocorra, o usuário vai selecionar uma linha com um registro do DataGridView e clicar no botão Editar, que abrirá o outro form.
Não sei como fazer a seguinte parte: preciso passar os dados dos registros que estão no dataGridView no form principal para as textBoxes do form de edição, para que seja possível a alteração desses dados.
Se alguém puder ajudar, ficarei agradecido =D
Estou usando Windows Form.
Answer: <code>    //ESTE É O FORM DA EDIÇÃO
    public partial class frm_edit : Form
     {
         public string Nome;
         public int Idade;
     }

       // este é o evento load do form da edição
    private void frm_edit_Load(object sender, EventArgs e)
    {
         txtNome.Text=Nome;
         txtIdade.Text=Idade+"";
    }

    // AQUI NO FORM ONDE ESTA A DataGridView1  NO CLICK DO BOTÃO
     {

       frm_edit  FE=new frm_edit();

      FE.Nome= Convert.ToString(DataGridView1[0,DataGridView.CurrentRow.Index].Value);
      FE.Idade= Convert.ToInt32(DataGridView1[1,    DataGridView.CurrentRow.Index].Value);

      FE.Show();
</code>
os números 0 e 1 são as colunas, neste caso:
0-Nome
 1-Idade
}


Simplificar Função de probabilidades com prioridade

Question: Tenho esta função que funciona como eu quero.
Ela recebe um valor entre -2 a 2 para definir a prioridade para escolher entre duas strings.
A minha questão é: Como posso simplificar a função?
function random_str($val){
   if($var == '-2') { $out = array('str1','str2','str2','str2'); }else
   if($var == '-1') { $out = array('str1','str2','str2'); }else
   if($var ==  '0') { $out = array('str1','str2',); }else
   if($var ==  '1') { $out = array('str1','str1','str2'); }else
   if($var ==  '2') { $out = array('str1','str1','str1','str2'); }
   return $out[array_rand($out)];
}

Answer: Analisando seus if independente do valor ele sempre vai ter 2 elementos que são o str1 e str2, depois vendo a diferença dos outros significa que quando for menor que 0 então precisa ter str2 e quando for mair que 0 precisa ter str1.
function random_str($val){
    // padrão
    $out = array('str1', 'str2');

    while ($var != 0) {

        // menor que zero
        if ($var < 0) {
            array_push($out, 'str2');
            $var++;
        } else { // maior que zero
            array_push($out, 'str1');
            $var--;
        }
    }
    return $out[array_rand($out)];
}

Dessa forma funciona até se informar valores menor que -2 e maior que 2 seguindo a mesma lógica, caso não queira adicione um if/return.
Comment: Boa, gostei desta solução! ;)


Linux does not get IPv6 default gateway from Cisco ASA5510 on autoconf

Question: My ASA 5510 has the following configuration for an interface. My Ubuntu box (2.6.35) connected to this network will correctly autoconf an IPv6 address, but it will not set a default route.
interface Ethernet0/0.10
 vlan 10
 no shutdown
 nameif inside
 security-level 100
 ip address 172.18.0.1 255.255.254.0
 ipv6 address REMOVED:1::1/64
 ipv6 nd prefix REMOVED:1::/64
 ipv6 nd ra-interval 120
 ipv6 enable

Thus, ping6 REMOVED:1::1 works fine and if I manually add a default route for IPv6 it works fine.
The resulting router advertisement looks like this:
01:06:42.253895 IP6 (class 0xe0, hlim 255, next-header ICMPv6 (58) payload length: 64) fe80::21c:58ff:fed3:ea36 > ff02::1: [icmp6 sum ok] ICMP6, router advertisement, length 64
        hop limit 64, Flags [none], pref medium, router lifetime 1800s, reachable time 0s, retrans time 1000s
          source link-address option (1), length 8 (1): 00:1c:58:d3:ea:36
            0x0000:  001c 58d3 ea36
          mtu option (5), length 8 (1):  1500
            0x0000:  0000 0000 05dc
          prefix info option (3), length 32 (4): REMOVED:1::/64, Flags [onlink, auto], valid time 2592000s, pref. time 604800s
            0x0000:  40c0 0027 8d00 0009 3a80 0000 0000 XXXX
            0x0010:  XXXX XXXX 0001 0000 0000 0000 0000

How come I do not get a default gateway set?
Answer: You can debug the process on the ubuntu box (a bit) with:
rdisc6 -v eth0

See if the advertisment matches what you expect.
Answer: For unclear reasons, this problem no longer persists. It may be that it disappeared on Ubuntu upgrade.


What's the relationship between geometric multiplicity of eigenvalues and dim ker(T)?

Question: Let $T: \mathbb{R}^7 \rightarrow \mathbb{R}^7$ be a diagonalizable linear operator with characteristic polynomial give by $p(t) = t(t-1)^2(t+2)^3(t -3)$.
Calculate $\dim(\ker(T - Id), \dim(Im(T + 2Id)), \dim(Im(T))$
I'm thinking whats the relationship between knowing that the algebraic multiplicity of each eigenvalue equals the geometric multiplicity and the dimension of the kernel and the image.
Comment: The geometric multiplicity of the eigenvalue $\lambda$ is  the dimension of$\ker(-\lambda I)$. So, if $0$ is an eigenvalue, (which means $ker T$ is non-trivial), its geometric multiplicity is the dimension of $\ker T$.
Answer: Since $T$ is diagonalizable, it has an eigenbasis (in other words, you can find a linearly independent collection of eigenvectors which span $\mathbb{R}^7$).  Let $p(t)$ be the characteristic polynomial of $T$ (or the matrix associated to $T$).  Then,

$\ker(T-\lambda I)$ is a linear space of dimension equal to the multiplicity of $\lambda$ as a root of $p(t)$.  The idea is that if $A$ is the matrix corresponding to $T$, then diagonalizability means that there is an invertible matrix such that $P^{-1}AP=D$ is diagonal.  Then, $A-tI$ can be diagonalized as $PDP^{-1}-tPIP^{-1}=P(D-tI)P^{-1}$.  If $\lambda$ is an eigenvalue then $D-\lambda I$ will have as many zeros on the diagonal as the multiplicity of the root of $p(t)$ at $\lambda$.  Moreover, if the $i$-th position is zero, then $Pe_i$ will be in the kernel (and be an eigenvector).  All $Pe_j$'s where the $j$-th position is nonzero are not eigenvectors.
The eigenbasis vectors which correspond to $\lambda$ are a basis for $\ker(T-\lambda I)$.  The construction above describes how to get these.
The dimension of the images can be found through the rank-nullity theorem.

The first two facts fail when $T$ is not diagonalizable.


Excel VBA - select to end of column minus two rows

Question: I'm in a situation where I want to start from a cell, E9 say, and I want to select down to the end of the column EXCEPT I don't want to include the last 2 cells in the column.
So for example
A
B
C
D
E

I want to select the range A:C in the column but NOT D and E.
Have tried
ActiveSheet.Range("E9", ActiveSheet.Range("E9").End(xlUp)).Select

but don't know how to lop off the last two cells...
Comment: @Jean-FrançoisCorbett - sorry, you're right. Here is the correct syntax I had intended: `Range(Range("A1"), Range("A1").End(xlDown).Offset(-2, 0)).Select`
Comment: select(range(A1).end(xldown).offset(-2,0))
Comment: Please show us what have you tried? Questions asking for code must demonstrate a minimal understanding of the problem being solved.Include attempted solutions, why they didn't work, and the expected results. See also: [Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist)
Answer: Range("E9", Cells(Rows.Count, "E").End(xlUp).Offset(-2)).Select

Though as a rule of thumb, you should avoid Select statements.
Answer: Dim r as range

set r = activesheet.range(activesheet.range("A1"), _
                        activesheet.cells(rows.count,1).end(xlup))

if r.rows.count>2 then set r=r.resize(r.rows.count-2,1)


Convergence of continuous functions on compact Hausdorff spaces to a continuous function on product topology

Question: Let $(X,T_x), (Y,T_y)$ be compact Hausdorff spaces.
Let $f \in C_{\Re}(X \times Y)$ and $\epsilon>0$. Prove that there is $n \in \mathbb{N}$ and functions {$g_j \in C_{\Re}(X):1 \leq j \leq n$} and {$h_j \in C_{Re}(Y): 1 \leq j \leq n$} such that
$|f(x,y)- \sum _{j=1}^{n}g_j(x)h_j(y)| < \epsilon$ for all $x \in X, y\in Y$.
I have shown that $(X \times Y,T)$ is compact. But I'm not sure if that helps. Not sure how to proceed from here, please help.
Answer: Show that the set of all functions on $X \times Y$ of the form
$$\mathcal{A}:= \{f(x,y) = \sum_{i=1}^n f_i(x) g_i(y): n \in \Bbb N, f_1,\ldots, f_n \in C(X), g_1, \ldots g_n \in C(Y)\}$$
forms a subalgebra on $C(X \times Y)$. This is not hard. Show also that it separates points.
Use the Stone-Weierstraß theorem to conclude $\mathcal{A}$ is uniformly dense, which is what you had to show.


Upload de imagem automático sem refresh

Question: Colegas.
Estou querendo implementar no meu sistema, uma atualização de imagem como ocorre no facebook, onde clicamos sobre a imagem existente e selecionamos do nosso computador a imagem desejada e a imagem é atualizada automaticamente, sem a necessidade de apertar um botão.
Encontrei o código abaixo, do qual consigo clicar em um link para fazer o upload, mas não estou conseguindo terminar. Alguém poderia me ajudar?
HTML

<input id="upload" type="file">
Alterar foto

Separei o h ref, pois na visualização aqui interpretava como html
CSS
 #upload_link{
text-decoration:none;
}
#upload{
display:none
}

JQUERY
$(function(){
            $("#upload_link").on('click', function(e){
            e.preventDefault();
            $("#upload:hidden").trigger('click');
            // Não sei cmo passar a foto para a página upload.php
             $.ajax({url: "upload.php?foto=", success: function(result){
           // Não sei como faria para que a imagem atualizasse sem refresh
            $("#atualiza").html(result);
          }});
         });
        });

Comment: Duplicada de http://pt.stackoverflow.com/q/5581/3635
Comment: Olha esse [exemplo aqui](https://jsfiddle.net/ivanferrer/9gqaqaoL/)
Answer: primeiro passo, para se enviar um arquivo via ajax, você precisa desativar o comportamento padrão do jQuery (application/x-www-form-urlencoded) e usar o padrão da Web (multipart/form-data).
$.ajax({
  url: form.action,
  data: data,
  contentType: false, // evitar que o jQuery realize o set do 'content-type' para 'application/x-www-form-urlencoded'.
  processData: false, // evitar que o jQuery tente serialzar o conteudo usando o $.params().
  type: form.method,
  success: function(data){
    console.log(data);
  }
});

agora vamos aos formulario.:

var form = $("#formUpload")[0];
var enviar = $("#Enviar");
enviar.on("click", function (event) {
  var data = new FormData(form);
  $.ajax({
    url: form.action,
    data: data,
    contentType: false,
    processData: false,
    type: form.method,
    success: function(data){
      console.log(data);
    }
  });
  return false;
});
<form id="formUpload">

      Descrição:
      <input type="text" id="Descricao" name="Descricao" value="" />

      Arquivo
      <input type="file" id="Arquivo" name="Arquivo" value="" />

    <input type="submit" id="Enviar" name="Enviar" value="Enviar" />

</form>

agora caso não tenha um form e queira fazer o upload assim que o usuario selecionar um arquivo.:

var arquivo = $("#Arquivo");
arquivo.on("change", function (event) {
  if (arquivo[0].files.length == 0)
    return false;

  var data = new FormData();
  data.append('Arquivo', arquivo[0].files[0]);
  $.ajax({
    url: "minha url",
    data: data,
    contentType: false,
    processData: false,
    type: "POST",
    success: function(data){
      console.log(data);
    }
  });
  return false;
});
<input type="file" id="Arquivo" name="Arquivo" value="" />

e por fim, uma informação extra que pode lhe ser util, você pode criar um link para objetos em memoria, então você pode criar um link para a imagem selecionada e já atualizar a sua imagem sem fazer uma requisição AJAX.
Esta tecnica pode ser util para que o usuario tenha uma previa da imagem antes de realizar o upload.

var arquivo = $("#arquivo");
var imagem = $("#imagem");

arquivo.on("change", function () {
  if (arquivo[0].files.length == 0)
    return false;

  var file = arquivo[0].files[0];
  var url = URL.createObjectURL(file);
  imagem.attr("src", url);
  imagem.attr("title", file.name);
  console.log(arquivo[0].files[0]);
});
#imagem {
  width: 480px;
}

<input type="file" id="arquivo" name="arquivo" />

Comment: Olá Toby. Na verdade gostaria que fosse automático, sem ter o botão de Enviar. Selecionar a imagem e automaticamente a foto ser trocada
Comment: @Fox.11 ja estava editando a resposta para incluir este cenario.
Comment: Boa resposta Toby, mas se puder dar vantagem as respostas existentes marcando a pergunta como duplicada seria melhor ;) (acredito que seja desta pergunta http://pt.stackoverflow.com/q/5581/3635)
Comment: Olá Guilherme. Vi o tópico. Não sei se seria a mesma coisa, pois pelo que vi, ele adiciona até 02 fotos. No meu caso seria apenas uma atualização. Desculpe se eu estiver errado.

Upload de imagem automático sem refresh


Which topology belongs with which set? A question about weak convergence

Question: I am reading a statement of Goldstein's Theorem that reads (here $J$ is the embedding of $X$ into $X^{**}$):

Let $X$ be a normed linear space, $B$ the closed unit ball of
  $X$, and $B^{**}$ the closed unit ball of $X^{**}$. Then the the weak-* closure of $J(B)$ is $B^{**}$.

A statement in the proof reads:

We leave it as an exercise to show $B^{**}$ is weak-* closed.

I'm am confused by this statement, we assumed $B^{**}$ to be the closed unit ball in $X^{**}$. Doesn't this mean $B^{**}$ is closed with respect to the topology on $X^{**}$ (which $\textit{is}$ the weak-* topology)?
Answer: When we say "the closed unit ball in $X^{**}$" without any specification of the topology, we usually mean the closure in the norm of $X^{**}$. In other words "the topology on $X^{**}$" is that inudced by the norm, not the weak-star topology.
Comment: The weak-star topology is defined on a dual space. Since $X^{**}$ is the dual of $X^{*}$, the weak-star topology on $X^{**}$ is the one generated by elements of $X^*$ acting as linear functionals on $X^{**}$.  In other words, a (balanced) basic-neighborhood of zero in the weak-star topology on $X^{**}$ has the form:
$\{x^{**}\in X^{**}: |x_i^{*}(x^{**})|<\epsilon,\ \ 1\leq 1\leq n\}$ for some choice of $x_i^*\in X^*$. A sequence $x^{**}_j$ in $X^{**}$ converges to $x^{**}$ in the weak-star topology if and only if $x_j^{**}(x^*)\to x^{**}(x^*)$ for every $x^*\in X^*$.
Comment: okay what is the weak-* topology on $X^{**}$ then? the weak topology will be induced by functionals on $X^{**}$? then the weak-* topology given by pointwise evaluation of those functionals?
Comment: My understanding was that the dual space provides linear functionals on the orginal space. If $X^{**}$ is dual to $X^*$ then shouldn't $x^{**}$ be a functional defined over  $x^* \in X^*$? Then the neighborhood at zero would look like  (see below)
Comment: https://imgur.com/a/mIP0W6f
Comment: Sorry the interpreter was thinking my ** was bolding
Comment: Oh sorry, you are defining the weak-* topology. Nvm -- ok I think I get it. I am still confused by this definition of weak-* base I think. Perhaps I'll go back to my book and formulate another question about it.


Expected squared distance between order statistics?

Question: Suppose $p(\cdot)$ is a smooth probability distribution over $\mathbb R$.  Suppose we draw two collections of $k$ i.i.d. samples from $p(\cdot)$, yielding random variables $(X_1,\ldots,X_k)$ and $(Y_1,\ldots,Y_k)$.  Use $X_{(i)}, Y_{(i)}$ to denote the $i$-th order statistic of $(X_1,\ldots,X_k), (Y_1,\ldots,Y_k)$, resp.; that is, $X_{(i)}$ is the $i$-th value obtained when sorting the sample $X_1,\ldots,X_k$.
Is it possible to compute in closed-form the following expectation?
$$
\mathbb E_{\substack{X_1,\ldots,X_k\sim p(\cdot)\\Y_1,\ldots,Y_k\sim p(\cdot)}}\left[
\sum_{i=1}^k (X_{(i)}-Y_{(i)})^2
\right]
$$
That is, is there an expression for the expected squared L2 distance between $\vec X$ and $\vec Y$ after sorting?
This answer gets part of the way, showing roughly that the expected value tends to zero as $k\to\infty$.  This document (and many others) gives the distribution function for each $X_{(i)}$.  If this value isn't known in closed-form, anything about its properties as a function of $k$ would be helpful as well!
Comment: The two expectations are identical thus should cancel one another.
Comment: In most cases this calculation can only be done numerically.  (Normal distributions with $k\gt 6$ are a notable example of that.) You could probably succeed with a uniform distribution and an exponential distribution, but almost anything else is likely to be intractable.  It's pretty difficult for discrete distributions, too, because of the need to account for ties.
Comment: Possibly useful: this can be rewritten as $\sum_i \left\{ \left( \mathbb{E} X_{(i)} - \mathbb{E} Y_{(i)} \right)^2 + \text{Var} X_{(i)} + \text{Var} Y_{(i)} \right\}$
Comment: True! So I guess it's equivalent to knowing the variance and expectation of the order statistic...hmm!
Comment: Haha, true.  So really this question is just asking for the variance of the order statistics summed.
Comment: Interesting.  I guess it might be possible to just use quadrature to get some idea of what this function looks like.  I wonder if it's possible to prove any interesting properties of it.
Comment: What is the intended application for this calculation? With that information we might be able to find a suggestive example for which we could answer the question.
Comment: this post was written while we were working on 1d calculations for this paper:  https://arxiv.org/pdf/2012.06958.pdf
Answer: The terms of this sum are known exactly for some distributions, since
$$E[(X_{(i)}-Y_{(i)})^2]=E[X_{(i)}^2]-2E[X_{(i)}]E[Y_{(i)}]+E[Y_{(i)}^2]=2\text{Var}[X_{(i)}].$$
For those distributions, we can get some nice expressions for the limiting behavior.
In what follows, $\psi_1(z)$ is the trigamma function $d^2\log \Gamma(z)/dz^2$, appearing in the calculation for the logistic distribution in N. Balakrishnan and A. Clifford Cohen, Order Statistics and Inference (1991), p. 40. The approximations given here with $\sim$ are exact in the highest-order terms.
For a uniform distribution bounded by $0,1$:
\begin{align}
\text{Var}[X_{(i:k)}]&=\frac{i(k+1-i)}{(k+1)^2(k+2)}\phantom{+123456}\\
\\
2\sum_i\text{Var}[X_{(i:k)}]&=\frac{k}{3(k+1)}\sim\frac13
\end{align}
For an exponential distribution with mean $1$:
\begin{align}
\text{Var}[X_{(i:k)}]&=\psi_1(k+1-i)-\psi_1(k+1)\\
\\
2\sum_i\text{Var}[X_{(i:k)}]&\sim 2\log k+1
\end{align}
For a logistic distribution with parameters 0,1:
\begin{align}
\text{Var}[X_{(i:k)}]&=\psi_1(k+1-i)+\psi_1(k)\phantom{+12}\\
\\
2\sum_i\text{Var}[X_{(i:k)}]&\sim 4\log k+8
\end{align}
For the unbounded distributions, these are growing functions: the answer linked in the question shows $\frac{1}{k}\|X-Y\|_1\to 0$, while this is showing $\|X-Y\|_2^2\to \infty$.


Question about differentiation and equalities and integrals

Question: Question ;Let's suppose I have function f(x) and function u(x)
Now if $\frac{d}{dx}f(x) = q$ and $\frac{d}{dx} u(x)=q$ then this means $u(x)=f(x)$?"
But if I do integral of q would I get $f(x)$ or $u(x)$?
God Bless You.
New question; Let's suppose I have function f(x) and function u(x) with same constant (ex: f(x)=5x+c u(x)=5x+c)
Now if $\frac{d}{dx}f(x) = q$ and $\frac{d}{dx} u(x)=q$ then this means $u(x)=f(x)$?"
But if I do integral of q would I get $f(x)$ or $u(x)$?
God Bless You.
ALSO I WANT TO ACCEPT ANSWER OF AMYWHY BUT I CANT
Comment: Welcome to MSE! For some basic information about writing math at this site see e.g.
[here](http://meta.math.stackexchange.com/questions/5020/),
[here](http://meta.stackexchange.com/a/70559/155238),
[here](http://meta.math.stackexchange.com/questions/1773/) and
[here](http://math.stackexchange.com/help/notation).
Comment: Please don't ask new questions, or change a question, once you've received answers. That makes answers look off target. If you want to clarify what you mean, do so below your original question, clearly marking it as an edit: e.g. "Question.... (then) Edit: ...."
Comment: Christian, with respect to your most recent edit: to accept the answer, just click on the grey check mark $\large \checkmark$ to the left of the answer you want to accept: it turns green when you click on it. ;-)
Answer: It is not true that if $\frac{d}{dx}f(x) = q(x)$ and $\frac{d}{dx} u(x) = q(x)$ then $f(x) = u(x)$.
For example, suppose $f(x) = x^2 + 3$, and $u(x) = x^2 - 7$. Then $f'(x) = u'(x) = 2x$, but $f(x)\neq u(x).\;$
What we can say is that if $f'(x) = u'(x),\,$ then $f(x)$ and $u(x)$ differ by at most a constant.
Note that if you find the indefinite integral of, say $2x$, you'll obtain a family of functions $$\int 2x \,dx = x^2 + \text{any constant}$$ which, again, differ by at most a constant of integration.
So to answer your updated (new) question: In your polynomial example, only if we know that the two functions have exactly the same constant term, then yes, of course, $$f'(x) = u'(x) = 5\;\;\implies f(x) = u(x)$$ In other words, in your example, we essentially already have that $f(x) = u(x) = 5x + c $, so clearly, we'd necessarily have that $f'(x) = u'(x) = 5$, and we'd necessarily have $f(x) = u(x)$ not because their derivatives are the same, but because we started with the exact same functions $f(x) = u(x)$.
With integration, you'd get neither (or both) $f(x)$ and $u(x)$: You'd integrate $\int 5\,dx$ to obtain $5x + \text{any constant}$, which is a specific form of a function to denote that the integral represents the family of all functions that differ only in their constant term, and so both $f(x)$ and $u(x)$ belong the the family of function given by $5x + \text{constant}$.
You might want to read/review what is highly relevant to your question:
The Fundamental Theorem of Calculus.
Answer: if $\frac{d}{dx} f(x) = \frac{d}{dx} u(x)$ does not mean $u(x) = f(x)$, it only means they have the same rate of change, e.g.: $f(x) = x$ and $u(x) = x+100$
also if you integrate $q$, you will get both, because you will get $x + c$, where $c = 0$ for $f(x)$ and $c = 100$ for $u(x)$ in my example
see here for more detail
Answer: I think that the correct answer depends on the intersection between the domain of $f$ and the domain of $u$. If $E$ is such intersection, then you can deduce that, on every interval $I \subset E$, $f$ and $u$ differ by a constant possibly depending on $I$.
Finally, the indefinite integral of $q$ cosists, by definition, of all the primitives of $q$. In particular, $\int q(x)\, dx$ gives you *both $f$ and $u$. And it gives you any other function that differs from $f$ and $u$ by a constant.


Combinatorics or Permutations for 2 letters into a five letter strings

Question: On Binary Island, the locals have only two letters in their alphabet: A and B. Sequences of these letters are called strings. The number of letters in a string is called its length. If a string has length n, then we call it an n-string. For example, ABAABABAABAA is a 12-string.
A block of consecutive letters in a string is called a substring. A substring may appear more than once. For example, ABAABABAABAA contains the substring AA three times, the substrings AB and BA four times each, but does not contain the substring BB.
A string that reads the same forwards and backwards is called palindromic. Every 3-string starting with A has exactly three different palindromic substrings, as shown in this table.
as shown in this image(please open)
a)  Explain why it follows from the table above that every 3-string starting with B has exactly 3 palindromic substrings.
b)  Show that every 4-string starting with A has exactly 4 palindromic substrings.
c)   Show that every 5-string has exactly 5 palindromic substrings.
It is also true that every 6-string has exactly 6 palindromic substrings, and every 7-string has exactly 7 palindromic substrings. However, this pattern does not continue.
d Find an 8-string that starts with AABBA and has only 7 palindromic substrings.
d)   Find an 8-string that starts with AABBA and has only 7 palindromic substrings.
Comment: What have you tried? Where are you stuck?
Comment: I am only stuck on question d but it would be great if someone could explain questions a, b and c because I cannot clearly find a reason why. Thank You.
Answer: a) Flip all the letters in the table (change A to B and change B to A). Then you will get a list of 3-strings starting with B, and their palindromic substrings.
b) and c) can be done by inspection
d) AABBA already has A, B, AA, BB, ABBA. Extending to AABBABA adds ABA and BAB. Finally extending to AABBABAA adds no more palindromic substrings.
Comment: Thanks and one more thing what do you mean by inspection
Comment: @0Ketrav0 Just check each one manually. You can use the trick in part a) to save some time.
Comment: OH YEAH, I get it thanks angryavian.


Como modificar CSS inline após carregamento da página?

Question: Boas,
Estou fazendo uma consulta à base de dados, onde existe um campo que possui conteúdo em html. Dentro desse conteúdo existe uma table, que possui algum CSS aplicado inline com uma width definida em px.
Preciso de alterar a propriedade width da tabela para 100%;
O problema é que estou a mostrar esse código por php uma vez que este vem da base de dados.
Ou seja, na realidade o que esotu a fazer é apenas isto:
echo $dados;

Todo o código html está dentro de $dados.
Tentei explicar de forma mais clara...
Agradeço a ajuda.
Cumps.
Comment: Você pode usar jQuery se a tabela tiver algum tipo de classe que você possa pegar
Comment: A sua tabela provavelmente será impressa dentro de algum elemento que você tem uma identificação certo ? pesquise uma tabela dentro desse elemento em particular: `$( "#minhaDiv" ).find( "table" ).css( "width", "100%" );
`
Comment: @LuísAlmeida sim. Se a tabela que você quer for a primeira, pode fazer `$('table:first-of-type')`
Comment: @LuísAlmeida se for a terceira, o seletor é `$('table:nth-child(3)')`
Comment: Você dispara esse "evento" como ? a lógica está no PHP ?
Comment: @KhaosDoctor a tabela não tem nenhuma class associada nem nenhum id e não consigo atribuir nenhum.
Comment: `$('table').css('width', '100%');` ?
Comment: @LucasQueirozRibeiro o PHP está a ser usado apenas para mostrar os dados da bd.
Comment: @CaioFelipePereira lembrei disso.. mas isso vai alterar em todas as tabelas existentes na página certo ?
Comment: @CaioFelipePereira hummm... vou tentar fazer assim.. se for a terceira posso fazer $('table:third-of-type') ????????
Comment: @LucasQueirozRibeiro boa dica... acho que peguei a solução... obrigado.
Comment: O ideal seria que você editasse a sua pergunta e colocasse o HTML gerado pra que a gente tenha uma ideia melhor de como te ajudar
Comment: Com vossas resposta eu já consegui modificar a width da table. :)
não coloquei o html, porque é muito grande mesmo. Mas já cheguei à solução.
Obrigado.
Comment: @LuísAlmeida vou postar minha sugestão como resposta, se ela resolveu seu problema, aceite como resposta. :)
Answer: Caso sua tabela seja impressa dentro de um outro elemento que você tenha identificado, pode utilizar o método find() do Jquery para buscar somente neste elemento.
Exemplo:
$( "#minhaDiv" ).find( "table" ).css( "width", "100%" );


numpy array append is not working

Question: i am trying to append in numpy array.it does not showing any error but not appending the values in the array. i can't  understand what's the reason.
ind=[]
ind=np.array(ind)
ind

out:array([], dtype=float64)
rand_num
out:0.2581020651429914

for i in T_Yk:
    print i,rand_num,i>=rand_num
    if i>=rand_num:
        np.append(ind,i)

0.841407208505 0.258102065143 True
0.544017164891 0.258102065143 True
0.847014100035 0.258102065143 True
0.837888398913 0.258102065143 True
0.602345432651 0.258102065143 True
0.758088894007 0.258102065143 True
0.875552313712 0.258102065143 True
0.566129640396 0.258102065143 True
0.398095901072 0.258102065143 True
0.708554596955 0.258102065143 True
0.308165627166 0.258102065143 True
0.716732072072 0.258102065143 True
0.760848001298 0.258102065143 True
0.307696603977 0.258102065143 True
0.574524448748 0.258102065143 True
0.608537650411 0.258102065143 True
0.661614576393 0.258102065143 True
0.358783413082 0.258102065143 True
0.396823316883 0.258102065143 True
0.867563492221 0.258102065143 True
0.520237352281 0.258102065143 True
0.866000916749 0.258102065143 True
0.851035162881 0.258102065143 True
0.566755675099 0.258102065143 True
0.687814928058 0.258102065143 True
0.787882814547 0.258102065143 True
0.8790451058 0.258102065143 True
0.538294379248 0.258102065143 True
0.543694673875 0.258102065143 True

but when i print the array ind,it shows
array([], dtype=float64)

Comment: You did not read the np.append docs.  It is better use list append, and do one catenated at end
Comment: Duplicate of: https://stackoverflow.com/questions/19646726/unsuccessful-append-to-an-empty-numpy-array  (and many other questions); https://stackoverflow.com/questions/40961972/numpy-append-array-isnt-working
Answer: np.append, unlike list.append, is not an in-place operation.
Therefore, assign back to original pointer:
ind = np.append(ind, i)

numpy array append is not working


Lacunary polynomials, sparse interpolation and uniqueness in $F_q$

Question: I am currently studying lacunary polynomials in finite fields and trying to understand a basic concept.
When we do dense interpolation using a classical method like Lagrange interpolation or Newton's interpolation, we may expect a dense polynomial of degree $d \le n$ for a set of $n+1$ points i.e., a polynomial of the form:
$$f(x) = \sum_{k=0}^{d} a_k x^{k}, x_d \ne 0$$
In other words, we expect $n$ or less non-constant terms in $f(x)$. This is true of dense interpolation over finite fields such as $\mathbb{F}_q$.
A lacunary polynomial (also called sparse polynomial or fewnomial) is a polynomial where
the number of its terms is considered fixed, while the degrees and coefficients of its terms
may vary. For example we can write
$$f(x) = a_1 x^{d_1} + . . . + a_l x^{d_{l}}$$
for a lacunary polynomial with at most $l$ terms, with no control whatsoever on the actual value of the degrees $d_i$ and the coefficients $a_i$. (Sparse interpolation methods are given in the linked article and references within).

My understanding is that by using a sparse interpolation algorithm for lacunary polynomials, we can obtain an interpolation of $f(x)$ with fewer than $n$ non-constant terms. Then, by using Fermat's Little Theorem, we can reduce degree $d_i$ of terms with $d_i > q$. Therefore, $\forall i, d_i < q$.
Questions:

Is it true that if we built a blackbox for $n+1$ datapoints using Lagrange or Newton interpolation over in $\mathbb{F}_q$ with $n < q$ and then used that as a blackbox for sparse interpolation of a lacunary polynomial (as many of the sparse interpolation methods seem to require one), we would get a different sparse polynomial?
Does this mean that the sparse polynomials obtained are not unique in $\mathbb{F}_q$? How does this reconcile with the Theorem on Uniqueness of Interpolating Polynomial (See Theorem 4.1 in linked notes)?
Comment: Can you unaccept the answer? The argument I had in mind doesn't actually work. Weird sparse interpolating polynomials do not contradict uniqueness of the interpolating polynomial in the sense of your second link, because they could have degree larger than $n$ in general.
Comment: @QiaochuYuan, the field $\mathbb{F}_q$ has $q$ elements. Degree $d$ of $f$ interpolated from $n$ points where $n < q$. There must be polynomials of degree in the range $[n+1, q-1]$ that interpolate the same points as $f$, some of which may be lacunary. That is what prompted the question.
Comment: Yes, I think we agree on that. I don't think that's what the linked paper is doing, though.
Comment: I think I got it. The flaw in my thinking is that $d_i >q$ for some lacunary polynomial obtained through sparse interpolation. While that may be true, a Fermat reduction would send $d_i$ less than $q$. Even if this $d_i > n$, it still doesn't violate the polynomial uniqueness theorem, because that is only about uniqueness of polynomials of degree $\le n$ for a set of $n+1$ points.
Comment: Yep. ${}{}{}{}$
Answer:
My understanding is that by using a sparse interpolation algorithm for lacunary polynomials, we can obtain an interpolation of $f(x)$ with fewer than $n$ non-constant terms.

This is impossible; you cannot expect to do better than $n$ non-constant terms in general. This is because for $n + 1$ points the vector space of possible inputs is $(n+1)$-dimensional, so the vector space of interpolating polynomials must also be at least $(n+1)$-dimensional to be able to match it.
Edit: Ah, sorry, there's a mistake in this argument. Here I am assuming that the degrees are fixed ahead of time. This argument doesn't apply if the degrees are allowed to depend on the points.

Is it true that if we built a blackbox for $n+1$ datapoints using Lagrange or Newton interpolation over in $\mathbb{F}_q$ with $n < q$ and then used that as a blackbox for sparse interpolation of a lacunary polynomial (as many of the sparse interpolation methods seem to require one), we would get a different sparse polynomial?

I don't know what sparse interpolation method you have in mind, and I also don't know what you mean by "a different sparse polynomial" - different from what?
It is true, over any field including finite fields, that there is a unique interpolating polynomial of degree $\le n$ if we interpolate at $n + 1$ distinct points. If instead you consider sparse interpolating polynomials of degree $> n$ there is no reason to expect uniqueness, again over any field.
(At least, that was my original interpretation of your question. But I don't think the linked paper is doing this; it looks like the point is to write down an algorithm for interpolation which has the property that it finds a sparse representation when one exists, with a runtime depending on the size of such a sparse representation. But the degree is still $n$, I think.)


How to list nodes that share one or more of the same entity references as the current node

Question: I have three content types Projects, Campaigns and Blog Posts. Campaigns and Blog Posts share an entity reference field that allows for selection of one or more related Projects.
I currently have a View with block that is displayed on the individual Project nodes and lists Blog Posts that are related to that Project. There is a contextual filter to compare the array of related (Project) nodes within the Blog Posts with the ID of the current Project node. I want to add the same type of list to the individual Campaign nodes.
The Campaign nodes will have one or more referenced Projects and the Blog Posts must be compared to see if they share any of the referenced Projects in common.
For example Campaign 1 with references to:

(Project 1, Project 2, Project 3)

should show related Blog Posts with either:

(Project 1)
(Project 2)
(Project 3)
(Project 2, Project 3)
(Project 1, Project 3)
(Project 1, Project 2)
(Project 1, Project 2, Project 3)

I have read and tried a couple of possible solutions to configure the contextual filters and relationships, but nothing is working; the results are blank. I know for a fact that there is a Blog Post and a Campaign that share at least one common Project reference. So the results should show at least that one Blog Post.
Any ideas? Clearly defined configuration options would be appreciated.
Answer: You could get the blog posts related to projects refernced by the campaign displayed
through:
1- create a view  to get project id for projects have relation with that campaign.
2- create a new view listing blog posts that have relationships with projects taking project id as a contexctual filter.
3- create a .tpl file displaying the output of the view that gets projects from theme information
In that file embed blog posts view using views_embed_view() function giving the projects ids as a parameter for the view but you will need to
Configure views to accept multiple parameters you should set this option: View → Advanced → Your Contextual Filter → More → Allow Multiple Values
you will loop through fields to extract and form the ids array and pass it as the code sample below
<code>$array = array('32+50+32');
$view = views_get_view('myview');
$view->set_display("myblock");
$view->set_arguments($array);
$view->pre_execute();
$view->execute();
$content = $view->render();
</code>
By that you will be able to get blog posts from another separated view


MySQL error 1054 (42S22) when using UPDATE

Question: When using the MySQL command line interface, I enter the following command:
UPDATE movies SET Title=Full WHERE ID=6;

I get the following error:
ERROR 1054 (42S22): Unknown column 'Full' in 'field list'

I have verified that I am using the table 'movies' and the column 'Title' exist.  Also, there does indeed exist an entry with an ID=6.
What am I doing wrong?
Comment: Full is not quoted, so it's being treated as if it were a field.
Answer: UPDATE movies SET Title="Full" WHERE ID=6; ???
Comment: That worked.  Thank you!  Does that have to be done for every field I change?


fast way to create a date based lookup R

Question: I have a pretty large data set with users and their membership start and end dates. For each membership period there is one entry.
I have another dataset, which is coming from the support system, and it has records of user id's along with the dates of each system usage. This dataset is even larger, as there is one record for each usage.
I need to aggregate the second and combine with the first one, based on each user and membership period.
I tried a function for a for loop but for an extremeley large dataset (her we are talking about some few millions of rows) this will take ages.
Edit: The join or merge will not work, because here there are several ranges (between start and end dates) for each ID in the first frame. Each range has been assigned a number. (Period of membership) The second data frame has dates and IDs and the problem is finding the membership period for each ID & date by comparing it to the date ranges in the first frame.
Here is the code, along with mock datasets and what I want to achieve at the end:
ids <- c(rep("id1", 5), rep("id2", 5), rep("id3", 5))
#
stdates <- c("2015-08-01", "2016-08-01", "2017-08-01", "2018-08-01", "2019-08-01",
             "2013-05-07", "2014-05-07", "2015-05-07", "2016-05-07", "2017-05-07",
             "2011-02-13", "2013-02-13", "2015-02-13", "2016-02-13", "2017-02-13")
#
endates <- c("2016-07-31", "2017-07-31", "2018-07-31", "2019-07-31", "2020-07-31",
             "2014-05-06", "2015-05-06", "2016-05-06", "2017-05-06", "2018-05-06",
             "2013-02-12", "2015-02-12", "2016-02-12", "2017-02-12", "2018-02-12")
#
# First dataset:
df <- data.table(id = ids,
                 stdate = stdates,
                 endate = endates)
#
df <- df %>%
  arrange(id, desc(endate))
#
# Add the membership period number for each user:
setDT(df)
df[, counter := rowid(id)]
#
# Second dataset:
ids2 <- sample(df$id, 1000, replace = TRUE)
dates2 <- sample(seq(Sys.Date() - 7*365, Sys.Date() - 365,  1), 1000)

#
df2 <- data.table(id = ids2,
                  dateticket = dates2)
#
# Function
counterFunc <- function(d2, d1) {
  d2$groupCounter <- NA
  for (i in 1:nrow(d2)) {
    crdate <- d2$dateticket[i]
    idtemp <- d2$id[i]
    dtemp <- d1 %>%
      filter(id == idtemp) %>%
      data.table()
    dtemp[, drcode := ifelse(crdate >= stdate & crdate <= endate, 1, 0)]
    if (length(unique(dtemp$drcode)) == 2) {
      dtempgc <- dtemp[drcode == 1]$counter
      d2$groupCounter[i] <- dtempgc
    }
    if (length(unique(dtemp$drcode)) != 2) {
      d2$groupCounter[i] <- 0
    }
    print(i)
  }
  return(d2)
}
#
# The result I want to get without a for loop:
df2gc <- counterFunc(df2, df)
#

Comment: Use a data.table non-equi join ...
Comment: Possible duplicate of [How to join (merge) data frames (inner, outer, left, right)](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right)
Comment: it is not a join problem. It is a look-up problem.
Comment: You solve lookup problems with a join ...
Comment: The look-up here is for a range, and not a definite value that can be used by 'merge'. I cannot really see, how one can apply a join in this case. @Zeiram has provided an answer which works fine but not for a dataset with 5M rows. The problem is similar to an IP lookup within range of IPs.
Answer: The operation you want to do is called "joining", so depending on the direction and completion of that "joining" there are some options.
Here is a simple example:
df1<-data.frame("ID"=c("1","2","3","1","2"),"First_Name"=c("A","B","C","D","E"))

df2<-data.frame("ID"=c("1","2","3"),"Last_Name"=c("Ko","Lo","To"))

left_join(df1,df2,by = "ID")

The result looks like this:

 ID First_Name Last_Name
 1          A        Ko
 2          B        Lo
 3          C        To
 1          A        Ko
 2          B        Lo

left_joinfrom the dplyrpackage simply looked up the relevant values in the look-up table (df2) and added them to the original table (df1, the left table) based on a "key" (by = "ID" in this case).
There are other operations that specify the terms of the joining more but left_joinshould be helpful in your case.
EDIT:
I have better understood your problem now. Please check if this solves it:
library(tidyverse)
df %>%
  mutate(stdate = as.Date(stdate), endate = as.Date(endate)) %>%
  left_join(df2, by = "id") %>%
  mutate(check = case_when(dateticket >= stdate & dateticket <= endate ~ "TRUE", TRUE ~ "FALSE")) %>%
  filter(check == "TRUE")

Edit:
For the problem the error "Cannot allocate vector of size" with join please refer to this:
Left_join error cannot allocate vector of size
Comment: thanks for the answer, but join (or merge, or left_join) does not solve the problem. My problem is not joining two data frames (data tables). In the second frame, there are many entries matching the date periods of the first frame. The problem is to get the right counter for each entry of the second frame. Second frame will be merged after it is aggregated.
Comment: @maop joing is performing a look-up, see my example. The left table "looks" for the right value to add from the look-up/right table based on the provided key. If it is not a look-up problem, what else do you want to do? Can you provide a simple input/output example like I did?
Comment: The example data frames are in my code. The key is not enough in this case. You should find for each id and date, which counter it corresponds to, by checking which start-end date period that the date from the second table corresponds.
Comment: for example, id1 has membership period from 2018-02-08 to 2019-02-17 and this is id1's 3rd membership period, hence the counter is 3. ID1 has two tickets in the other data frame, one is from 2018-03-15 and the other is from 2019-05-16. The problem is assigning a counter in the second frame, for the ticket from 2018-03-15 as 3. This date would be corresponding to another period for another ID. For each ID and date, this lookup should be done.
Comment: @maop I assume the membership period vary by individual and can not be added easily as groups to the second frame? Look-up/join can act on two keys, so your real problem is just creating the second key, correct? Maybe you can edit your question a bit in that direction. Also I cannot see any mock data, so it is hard to improve my answer.
Comment: correct. If you run the code I gave within my question, you will see the data. (sorry, ids and dates were somehow not copied. Added them now.) The function I wrote does what I want. But it is not efficient for large datasets. I am looking for an efficient way
Comment: @maop I edited my answer, check the second code, it did work for me.
Comment: hey @Zeiram yes this worked! Thanks a lot! I'll try it on my larger datasets and let you know.
Comment: unfortunately it doesn't work with the actual dataset. I get 'cannot allocate a vector of size...' error, although I reduced the number of columns in both data tables.
Comment: @maop Without knowing the real data there is no way to know why my function does not work but the error indicates size of the data set. This function isn't too resource heavy but if your dataset is too big there is a different problem. I suggest looking up sparse matrix functions, cleaning memory in R, etc.
Comment: Your suggested code piece worked on an EC2 instance. I will accept your answer but will keep searching for an efficient way.


Put two buttons on left side of ActionBar in Android

Question: I'm trying to build an ActionBar with two icons on left side of it, and the rest of them on the right side. These two items would be a logo, that when is clicked it redirects the user to the home page, and a back button, that when is clicked it redirects the user to the "Page Up". Logo should appear at the most left place, and the default back arrow should appear after.
Currently, I already achieved the "Page Up" functionality (with the default back arrow), and the item appears on left side, but I've been unsuccessful on putting the logo there too.
Here is what I have until now (code is simplified to focus on what matters to the question):
layout_action_bar.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="@dimen/action_bar_text_padding_right"
        android:id="@+id/txt_action_bar_title"
        android:singleLine="true"
        android:ellipsize="end"
        style="@style/Widget.ActionBar.Text"/>

</LinearLayout>

menu_base.xml (Not needed, but just to give context about what I have on the right side of ActionBar):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      tools:context=".BaseActivity">

     <item
        android:id="@+id/action_menu"
        android:icon="@drawable/ic_menu"
        android:orderInCategory="1"
        android:title="@string/menu_drawer_open_accessibility"
        app:showAsAction="always"/>

     <item android:id="@+id/search"
          android:title="@string/actionbar_search_button_tooltip"
          android:icon="@drawable/ic_navigation_search"
          app:showAsAction="always|collapseActionView"
          app:actionViewClass="androidx.appcompat.widget.SearchView"
        />

</menu>

menu_base is inflated onCreateOptionsMenu() and on that side of the ActionBar everything is ok.
BaseActivityMenu.java:
protected final void createMenu(CharSequence actionBarText) {

     ActionBar actionBar = getSupportActionBar();
     if(actionBar != null) {
          actionBar.setCustomView(R.layout.layout_action_bar);
          TextView title = actionBar.getCustomView().findViewById(R.id.txt_action_bar_title);
          title.setText(actionBarText);

          actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
          actionBar.setDisplayHomeAsUpEnabled(true);
          actionBar.setHomeButtonEnabled(false);
     }
}

Visually, this is what I have at the moment: My ActionBar
How can I achieve this?
Answer: Inside your layout_action_bar.xml, add the following code in the LinearLayout:
<ImageButton
    android:id="@+id/homeBtn"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:background="@android:color/transparent"
    android:src="@drawable/ic_home"/>

<ImageButton
    android:id="@+id/topBtn"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:background="@android:color/transparent"
    android:src="@drawable/ic_back_up"/>

And then in your activity, add the following lines of code:
ImageButton toHome = actionBar.findViewById(R.id.homeBtn);
ImageButton toTop = actionBar.findViewById(R.id.topBtn);
toHome.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        //do what you want here
    }
});
toTop.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        //do what you want here
    }
});

Comment: This seems to do the work, thanks! However, now when I click on search menu item and it expands, logo disappears with the action bar title, and I'd like to maintain the logo.
Do you know how can this be achieved?
Comment: Add: I'm using SearchView from AppCompat
Comment: @Uppa If you're using SearchView from AppCompat as a menu item, then I don't think you can. In the `LinearLayout` code that I have, add another Search View with the attribute `iconified="true"` and place it on the right side of the menu bar, I think that should work. If you want I'll edit my answer to upload the same change
Comment: Worked like a charm! Now logo is always visible, and I can hide/unhide title bar when search view expands or collapses :)
Thanks for your help!


How to change submit button name in HAML?

Question: I am new to Haml and have a form with two submit buttons. To differentiate and handle them in the Controller every single button needs a name.
An example from HAML:
= program_form.submit I18n.t('texts.6OA')

appears in HTML as:
<input type="submit" name="commit" value="Save Template">

but I want something like this:
<input type="submit" name="Button2" value="Save Template">

Any hints?
Answer: Have you tried this?
program_form.submit I18n.t('texts.60A'), name: 'Button2'

Comment: Thank you, forgot the ',' before name :). Now it's working!


How to logout from stackexchange.com?

Question: How to logout from stackexchange.com?
My question is straightforward, but I thought I'd format my question properly to meet question quality standards.
I made reasonable searches but I found nowhere how to logout without clearing cache manually.
Any tips?
Comment: You can check out but you can never leave
Answer:
Answer: Follow this screenshot steps:

Hope it helps!

How to logout from stackexchange.com?


How read() method of BufferedReader works?

Question: public class Test{
    public static void main(String [] args){

        BufferedReader reader = new BufferedReader(new
        InputStreamReader(System.in));
        System.out.println("Enter number?");
        int num = br.read();
        System.out.println("You  entered " + num);
    }
}

If i give 97 as input then it gives 57 as output.Why? I mean that read method returns int then why it returns another thing? And suppose that if we replace br.read() with (char)reader.read() and if we give input as 97 then output remains same or not i mean that will 97 be converted into character that is 'a'.Kindly make my concept clear .I am confused about when int is represented in ascii or in unicode and further.
Answer: BufferedReader's read() method reads a single character only, so it is actually printing the Unicode value of number 9 (taken first digit from 97) as shown below:
char c = '9';
int num = c; //reader.read();

Output: 57
To solve the issue, you can use readLine() to read the input as a line and convert it to int as below:
int num =  Integer.parseInt(reader.readLine());

Answer: You are using the read method, which just reads a single character and then returns the ascii or unicode value of it. hence when you enter 97 it just reads it as character 9 and then print its ascii value which is 57.
Please refer https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#read() oracle doc , to get more info of read method.
Apart from readLine method of BufferedReader , you can use scanner class object which comes with option to read int,long,string etc. below sample shows how to read integer.
Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        System.out.println("a"+a);

Answer: read() return one character. String "9" int value is 57.
If input 97 and output 97.
int num = Integer.valueOf(reader.readLine()); // num is 97

If input 97 and output A.
int num = Integer.valueOf(reader.readLine()); // num is 97
char ch = Character.valueOf((char)num); // ch is A


Accessing data frame columns via names using regex

Question: I have a huge data.frame [350000, 431] and I need some cleaning to do on it.
One of the cleaning operation is to turn all boolean columns c(0,1) to c(FALSE, TRUE).
I can identify these columns by the condition that the column name contains the string "include".
So I need to do something like that:
data.clean[,4:272] <-  ifelse(data.clean[,4:275] == 1,"TRUE", "FALSE")

where instead of 4:272 (index of columns that contains "include", I want to use a more robust regex for that purpose.
Answer: Mimic your data (only three columns, two columns meeting the "include" condition"):
data.clean <- data.frame(exclude = sample(LETTERS, 3.5e5, replace=TRUE), this.include = sample(c(0,1), 3.5e5, replace=TRUE), this.include2=sample(c(0,1), 3.5e5, replace=TRUE))

> head(data.clean)
  exclude this.include this.include2
1       W            1             1
2       Y            1             0
3       F            0             0
4       W            0             1
5       B            1             0
6       M            1             1

Use names and grep to identify the correct columns, and use a direct logical comparison to return the values to TRUE and FALSE:
data.clean[,names(data.clean)[grep("include", names(data.clean))]] <- data.clean[,names(data.clean)[grep("include", names(data.clean))]] == 1

> head(data.clean)
  exclude this.include this.include2
1       W         TRUE          TRUE
2       Y         TRUE         FALSE
3       F        FALSE         FALSE
4       W        FALSE          TRUE
5       B         TRUE         FALSE
6       M         TRUE          TRUE

Comment: `data.clean[, grep("include", names(data.clean))]` should be enough.


Criação de dois projetos em único repositório Git

Question: Estou criando um projeto em Java web e quero criar um outro projeto em Android.
Mas gostaria de criar apenas um repositório e dentro do mesmo possuir estes dois projetos com suas respectivas árvores de commits, como se fossem componentes separados.
É possível criar algo assim no Git?
Comment: você pode separar os projetos em pastas... mas não sei se é a maneira correta
Answer: Não é possível. O Git tem suas vantagens, mas uma das desvantagens é justamente essa. O Git trabalha com o repositório como um todo. Na verdade, é mais comum quando precisa de melhor organização e controle de acesso ter uma separação de projetos em partes e cada uma ter seu próprio repositório.
É evidente que pode, ainda que não recomendado, colocar ambos no mesmo repositório, mas será uma coisa só do ponto de vista do Git. Dá para fazer, mas qualquer tentativa de administrar como se fossem coisas separadas vai trazer mais confusão.
Separe em dois repositórios e seja feliz. Não tem por que não fazer, é o que todo mundo faz. Inclusive criam projetos auxiliares que nem são códigos.
Se quiser insistir, tem uma solução no SO. Não pense que é simples lidar corretamente. Não sei se terá tudo o que deseja e principalmente se compensa o esforço, especialmente quando algo sair errado.
No seu caso, me parece que os projetos são tão distintos que não valeria nem tentar colocar tudo em um repositório só.
E é claro que se quiser tratar como uma coisa só, não tem segredo.
Coloquei no GitHub para referência futura.
Answer: Eu realmente desconheço a configuração que possibita a inclusão de mais de um projeto no mesmo repositório, da forma que você sugeriu.
Mas, uma alternativa seria você utilizar uma função do Git subtree que permite a possibilidade de estruturar diferentes repositórios, de tal modo que passe a impressão que está tudo em um mesmo.
Esse recurso do git permite gerenciar multiplos repositórios de tal forma:
Repositorio
   |--- Reposotorio_1.0
   |    |--- index.html
   |    |--- README.md
   |    |
   |    |--- Repositorio_1.1
   |         |--- index.html
   |         |--- README.md
   |
   |--- Reposotorio_2.0
   |    |--- index.html
   |    |--- README.md
   |
   |--- README.md

Os passos em base consistem em:

Criar os repósitorios que serão utilizados
Criar os Remotes para cada um dos repositórios
git remote add repositorio-1_0 https://github.com/usergit/Repositorio-1_0.git
git remote add repositorio-1_0 https://github.com/usergit/Repositorio-2_0.git

Adicionar os "repositórios-filhos" a partir do repositório principal
git subtree add --prefix=Repositorio-1_0/ Repositorio-1_0 master
git subtree add --prefix=Repositorio-2_0/ Repositorio-2_0 master

Obs.: Nesse modelo deverá ficar atento com o fluxo de atualização dos repositórios, podendo ser atualizado  a partir do repositório principal para os repositórios filhos
Exemplo com 'push'
    git push ---> No principal
    git subtree add --prefix=Repositorio-1_0/ Repositorio-1_0 master
    git subtree add --prefix=Repositorio-2_0/ Repositorio-2_0 master

Abaixo tem uma referência para um artigo que trará mais detalhes:

Artigo de "ROBERTO ACHAR - Multiplos repositórios"
Comment: Olá, Luiz! Consegue detalhar algumas coisas descritas no link na sua resposta? Isto é importante pois este site pode sair do ar um dia. No SO, é importante que as respostas não dependam muito de links externos para ajudarem os usuários.
Comment: Fala @Dherik, realmente faz todo sentido!
Answer: É possível, embora seja utilizado em casos especificos e com nivel de programação avançado.
Atualmente pode se escolher duas formas.
1 - Branch (como uma versão)
2 - subtree (já mencionado nesta questão)
Que devem ser implementadas separadamente.
Ambas as formas você encontra na documentação do GitHub.

Os motivos que deve ser desencorajado tais praticas

Ambas as opções são complexas e com grande probabilidade de erros...
Vou detalhar:
Em desenvolvimento e até mesmo em produção...
Acaba sendo uma confusão...
Certamente algo do branch anterior possa interferir no branch atual...
Na Organização ao longo tempo. Fica um desastre. Embora seja separado no Git por Brach, ou ramificado como subtree...
Quando você faz a implementação final.
Sem notar previamente as devidas configurações (Que podem ser milhares) os arquivos se misturam... Causando conflito de dependências.
Mesmo que seja separada por pastas, mas existem arquivos ocultos que podem não ser substituídos.
Creio que essa função não deverá ser implementada.
O melhor é um repositório único. Com suas dependências, sendo atualizadas...
Vale lembrar que as dependências são essenciais para o funcionamento do repositório.
Não complique algo que é simples.


Regular expression to get Valid HTTP Characters

Question: As per [RFC 1738][1]
[1]: http://www.ietf.org/rfc/rfc1738.txt there are unsafe characters in http headers, query parameters and path parameters which should be filtered out. I am trying to get a regular expression for all the characters that can be allowed or whitelisted
Answer: Take a look at "Character Classes" in https://docs.oracle.com/javase/9/docs/api/java/util/regex/Pattern.html
Excerpt:
<code>Character classes
[abc]   a, b, or c (simple class)
[^abc]  Any character except a, b, or c (negation)
[a-zA-Z]    a through z or A through Z, inclusive (range)
[a-d[m-p]]  a through d, or m through p: [a-dm-p] (union)
[a-z&&[def]]    d, e, or f (intersection)
[a-z&&[^bc]]    a through z, except for b and c: [ad-z] (subtraction)
[a-z&&[^m-p]]   a through z, and not m through p: [a-lq-z](subtraction)
</code>


How can I get R to use more CPU usage?

Question: I noticed that R doesn't use all of my CPU, and I want to increase that tremendously (upwards to 100%). I don't want it to just parallelize a few functions; I want R to use more of my CPU resources. I am trying to run a pure IP set packing program using the lp() function. Currently, I run windows and I have 4 cores on my computer.
I have tried to experiment with snow, doParallel, and foreach (though I do not know what I am doing with them really).
In my code I have this...
library(foreach)
library(doParallel)
library(snowfall)

cl <- makeCluster(4)
registerDoParallel(cl)

sfInit(parallel = TRUE, cpus = 4)

#code that is taking a while to run but does not involve simulations/iterations

lp (......, all.int = TRUE)

sfStop()

R gets stuck and runs lp() for a very long time. My CPU is around 25%, but how can I increase that?
Comment: If you need to run 4 integer programs in parallel, then the packages you are using would be useful. If you need to run a single integer program using all 4 cores, you need to find a package that does that for you (and I don't think the `lpSolve` package provides that option).
Comment: Is there a package that allows me to run programs using all my cores? It doesn't necessarily have to be specific to IP, but more so can I increase my R's CPU usage?
Comment: fwiw, this sounds quite similar to [this recent question](http://stackoverflow.com/questions/31868370/parallel-multicore-processing-in-r-for-an-integer-program); you may find some of the suggested packages in the comments helpful.
Comment: Not to my knowledge -- parallelizing functions often requires a pretty deep understanding of how they work. For instance, parallelizing MIPs is quite intricate and an area of active research in operations research and related communities. You can read more about the many R packages for parallel processing [here](https://cran.r-project.org/web/views/HighPerformanceComputing.html).
Comment: Easiest solution is to use Revolution R's distribution. It uses the CPU's SIMD instructions to achieve 4x speedup just on a single core. It also uses multiple cores a lot more efficiently than R itself. Running `svd` on i7 was 7x times faster on RRO than plain R.  Parallelization solutions should be tried *after* you ensure R itself is running at its maximum.
Answer: Posting this as an answer because there's not enough space in a comment.
This is not an answer directly towards your question but more to the performance.

R uses slow statistical libraries by default which also can only use single core by default. Improved libraries are OPENBLAS/ATLAS. These however, can be a pain to install.
Personally I eventually got it working using this guide.
I ended up using Revolution R open(RRO) + MKL which has both improved BLAS libraries and multi-cpu support. It is an alternative  R distribution which is supposed to have up to 20x the speed of regular R (I cannot confirm this, but it is alot faster).
Furthermore, you could check the CRAN HPC packages to see if there is any improved packages which support the lp function.
There is also packages to explore multi cpu usage.
This answer by Gavin, as well as @user3293236's answer above show several possibilities for packages allowing multi CPU usage.
Answer: If you are trying to run 4 different LPs in parallel, here's how to do it in  snowfall.
sfInit(parallel=TRUE, cpus=4)
sfSource(code.R) #if you have your function in a separate file
sfExport(list=c("variable1","variable2",
            "functionname1")) #export your variables and function to cluster
results<-sfClusterApplyLB(parameters, functionname) #this starts the function on the clusters

E.g. The function in the sfClusterApply could contain your LP.
Otherwise see comments in regard to your question

How can I get R to use more CPU usage?


Test features simply being a superset of train features

Question: I am working on a two-class prediction task. A rather informative field in the dataset is user_id that has ~4m unique values which all occur in test set but it is a proper superset of user_ids available inside the training set (~2.5m). Now for my poly-2 regression based model it doesn't make sense to have categorical feature values that it hasn't seen before, so I plan to replace all user_ids in test set, which are missing in train set, with a single value (-999 if you may). But then the problem is that my model wouldn't have seen any sample with -999 for its user_id. So what's the best course of action here? I'm thinking about iterating over each train sample twice during the training, once with the original user_id and once with the special value -999; as if telling the model to rely on other features if user_id was missing. I know about imputation techniques but here I don't have any samples with missing values.
Edit: I should mention that user_id is one-hot encoded.
Comment: This is a conceptual failure. `user_id` has no generalizability and hence no external validity and should not be considered as a predictor.
Comment: Can you please explain a bit more about user id, is it akin to say roll number of a student in a class?
Comment: @saharudra Yes, you can say that. Although my model is only considering them as nominal features identifying distinct users.
Comment: This is a local competition and the problem is very similar to CTR prediction. The test set has two features (i.e. `user_id` and `item_id`) and while there is no overlap between items in two sets (item feature are given in another table) and there is no point in actually giving `item_ids` to the model, the train `user_ids` are a subset of test `user_ids`. Although the features are highly anonymized, the time users have signed up in the system can be probably inferred from this feature, but at the moment I'm only treating them as categorical features.
Comment: @AdamO This is actually common practice in CTR prediction challenges to use `user_id`. They usually use *hashing trick* to combine several features (`device_id`, `platform_id`, etc.) into a `user_id` and one-hot encode them into a space of several million distinct values to distinguish users. The evidence to the importance of `user_id` is that the choice of a proper hashing function leading to less collisions can vividly impact the performance. The troubling part is that the model doesn't see all these `user_ids` whose user's estimated affinity toward the item we seek, during training phase.
Answer: Unless 'user_id' has some information more than just being a unique identifier for a specific person...then you are creating a couple of issues in your model. If there is other information to be gathered (other than just a unique number) then it is best to try to capture that information in some other way. This could happen because your user_id is actually correlated to another variable and is acting as a stand-in for that variable.
For example: if user_id is actually representing how long the user joined the system -- then you translate the number to an ordinal or approximate time variable. However, you have to determine whether there are issues with with using user_id as a substitute for time. Did only 10 people join the first year and 100 the second and 1000 the third? Generally it is better to see if there is a sign-up date elsewhere in the data. In the time example you may be able to simply replace everyone who doesn't have an account with the shortest amount of time possible because they don't have an account.
Otherwise, if the variable can't be translated into something more meaningful:
1) You are over-fitting by using a variable that only applies to individuals in order to find a result. Example: It's great to know that Bob is more likely to make a claim than Judy is...but it has nothing to do with a model predicting whether John will. Your model will be tweaked to intentionally be better at the training set (even more than it normally would be).
2) You may not be capturing the actual information that is impacting your results. Does any individual user_id in the data occur more than once? If it doesn't occur more than once, and is having a meaningful effect on your model, then it is likely that the variable correlates to something else that is really having the impact. Try identifying what the correlation might be. If user_id does occur more than once then some models will simply weight the user_id based on how many times it occurs which is a frequency and not related the the actual user_id itself (and you can transform the data to represent this). Determine what the impact is of having multiple instances of a user id and how to deal with that.
It sounds like there is some data exploration still needed unless user_id means something different in your data set than it normally does.
Comment: Thanks. I need to read your answer a couple more times. The train and test are ~90m and ~10m samples respectively, so we have more than one sample for each user (about 38 samples on average). My model only relies on `user_id` distinctly distinguishing users and I haven't made any assumption about it being ordinal. Excluding this field makes the accuracy worse though. Even tried to hash the values of `user_id` into smaller spaces (1m and 200k) but it worsen the performance both for the test set and my validation set (which I have my reservations about it being representative of the test data)
Comment: My model doesn't take them as ordinal, but if it did, I guess hashing to a space of identical cardinality would set me straight.
Comment: You run into the conceptual issue that you don't need to predict a known user's performance -- you already know that user's performance. If you try to break down into "known user" and "unknown user" you should just go ahead and split this into two models: One that tells you what you already know and one that predicts about things you don't know. There are a lot of reasons that including this could improve some metric you are reviewing.
Comment: Also, it's not clear how you are determining "performance". Are you checking how well your model finds the current information? Or how well it predicts future values? Or some other measure? These are not all the same.
Comment: Thanks. That's actually a very good idea. I didn't want to succumb to ensemble classifiers yet and wanted to work on my single model as much as I could. But this is actually seems inevitable that my rudimentary model needs to be split into two models for known and unknown users.
Comment: Oh, and as for the performance, this is a data science student challenge held in Iran and I can submit predictions on a daily basis. So I know that including the `user_ids` does indeed help with predicting future values and I also know that it's not because of the ordering, since I OHE'd the feature values.


I am getting a missing keyword error in this case expression

Question: I am getting a missing keyword error in this case expression.
What is the reason?
SELECT (CASE WHEN (MM.MAKE_DESCRIPTION = 'TOY')
       THEN
          CASE
             WHEN MM.MODEL_DESCRIPTION = 'SCION' THEN MM.MAKE_DESCRIPTION = 'SCION'
             WHEN MM.MODEL_DESCRIPTION <> 'SCION' THEN MM.MAKE_DESCRIPTION
          END
    ELSE MM.MAKE_DESCRIPTION END) AS MAKE
FROM table_make MM where value ='test';

Comment: Give us some sample table data and the expected result - all as formatted text, not images. Take a look at https://stackoverflow.com/help/mcve.
Answer: It looks like in one of your when clauses you have put another Boolean expression as your THEN output so instead of saying then show 'SCION', you put then show Boolean expression which doesn't make sense.
SELECT (CASE WHEN (MM.MAKE_DESCRIPTION = 'TOY')
   THEN
      CASE
         WHEN MM.MODEL_DESCRIPTION = 'SCION' THEN 'SCION' --edit here
         WHEN MM.MODEL_DESCRIPTION <> 'SCION' THEN MM.MAKE_DESCRIPTION
      END
ELSE MM.MAKE_DESCRIPTION END) AS MAKE

FROM table_make MM where value ='test';

Answer: your case statement missed two points
1.end will be after else
2.and another else will come for 1st case when
    SELECT CASE WHEN MM.MAKE_DESCRIPTION = 'TOY'
               THEN
                  CASE
                     WHEN MM.MODEL_DESCRIPTION = 'SCION' then
                     'SCION'
                     WHEN MM.MODEL_DESCRIPTION <> 'SCION' THEN MM.MAKE_DESCRIPTION

            ELSE MM.MAKE_DESCRIPTION END
else null end AS MAKE -- this else for 1st case
  FROM table_make MM where value ='test';

Answer: Simplify it with an AND
CASE
    WHEN MM.MAKE_DESCRIPTION = 'TOY' AND MM.MODEL_DESCRIPTION = 'SCION'
        THEN 'SCION'
    ELSE MM.MAKE_DESCRIPTION
END


Angular Cordova push data gone after reload

Question: im new with angular, and i want create apps that store cart data to sesssion using push,
but when i reloading the page, the data session was gone, not like in the php $_SESSION,
heres my code
cartObj.cart.push( { "cart_item_id": id , "cart_item_image": image , "cart_item_name": name , "cart_item_price": price , "cart_item_qty": qty } );

how to make the cart data is store in session,
i'l try using local storage but it not work either
localStorage.setItem('cart_item_name', name);

in my controller
$scope.name= localStorage.getItem('cart_item_name');

im new here, hope someone care to answer and explain slowly,
thanks
edit
this my controller
.controller('keranjangBelanjaCtrl', ['$scope', '$stateParams','sharedCartService','$ionicPopup','$state', // TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $stateParams,sharedCartService,$ionicPopup,$state) {

    //onload event-- to set the values
    $scope.$on('$stateChangeSuccess', function () {
    $scope.name= localStorage.getItem("cart_item_name");

    });

this my view

{{ name }}

solved with
.controller('keranjangBelanjaCtrl', ['$scope', '$stateParams','sharedCartService','$ionicPopup','$state', // TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $stateParams,sharedCartService,$ionicPopup,$state) {

    $scope.name= localStorage.getItem("cart_item_name");

    );

Answer: You don't need to store the whole object in localstorage just Id of your cart item and get your cart item later by that Id. Currently you are storing just name. Item will be stored in localstorage until someone not remove it.
localStorage.setItem("cartId", id)

And get
localStorage.getItem("id") || 'null'

Comment: its gone when i reload the page, seem not storing in session,
Comment: it can't be. it's not session. how's your code look like? you can test in dev tools console by typing localStorage.setItem("id",1) and get it by localStorage.getItem("id") and you will have 1. it can depends on your browser also.
Comment: yeah, its work in console, so the problem is in mu controller right?
Comment: has to be share your code so i can try to help you.
Comment: yeah, i update the answer above, so other reader may read my post and found the solution,


java.lang.InstantiationException: class com.e has no zero argument constructor

Question: I am getting this error
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.e.www.i/com.e.www.i.Search}: java.lang.InstantiationException: class com.e.www.i.Search has no zero argument constructor

This is the below class :
public class Search extends Activity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;

    private RecyclerView mRecyclerView1;
    private RecyclerView.Adapter mAdapter1;
    Context mContext;

    public Search(Context context) {

        mContext = context; // I guess the error is here, but I need to define context for below MyAdapter
    }
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(
                new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false));
        mAdapter = new MyAdapter(getDataSet(),mContext);
        mRecyclerView.setAdapter(mAdapter);

      }

private ArrayList<String> getDataSet() {
        ArrayList results = new ArrayList<DataObject>();
        for (int index = 0; index < 5; index++) {
            String obj = new String("User " + index);
            results.add(index, obj);
        }
        return results;
    }

Comment: try creating a zero argument constructor
Answer: You need to define a no-args constructor, just as the error says:
public Search() {
    // No args constructor
}

The context that you need for your adapter is the Activity itself, you don't need to get it via the constructor. Just use this, since you are already in the context of an activity:
mAdapter = new MyAdapter(getDataSet(), this);

And then you can drop the overloaded constructor that you defined for your custom activity.
Comment: **You need to use** `ClassName.this` to refer to the activity's context if you were working inside of an anonymous class (like from within a `View.OnClickListener` e.g, when handling a button click), because using `this` in this case would refere to the anonymous class, not to the container activity. Check this: http://stackoverflow.com/questions/10102151/whats-the-difference-between-this-and-activity-this
Comment: how to define the context to past it to myAdapter ?
Comment: ah .. I was doing it `(getDataset(), this.Search)` i though `this,search` is the same as `this`
Comment: euh... what ? you need to use `this` (or `Search.this`), not `this.Search`.
Comment: do you have link explain the difference between both ? I am still doing my first app , so many things I need to learn
Answer: In search, you created the constructor
public Search(Context context) {
    mContext = context;
}

Now, since you have a user defined constructor, your compiler does not provide you with any default constructors, so you need to define a parameter-less constructor yourself too.
public Search() {
    // Constructor body
}

Answer: remove Search(..) constructor and just pass conetext to adapter like
 mAdapter = new MyAdapter(getDataSet(),Search.this);

As you are already in Search Activity
Answer: If Argument is a must for example IntentService
public BaseIntentService() {
    super("IntentService");
}

Otherwise
public Search() {
    // Constructor body
}

Answer: If any one is facing this problem with dependency injection. please add the annotation for activity . @AndroidEntryPoint . Also check if you added the required annotations for viewmodel/fragment/application etc.


Facebook app requesting personal data

Question: I have to create a Facebook app for an online contest, and I need to collect users personal data through a form.
I have a checkbox for privacy with the link to the privacy agreement, but I heard that Facebook needs to approve your app if you're requesting personal data.
Is it true? What's the process?
Answer: If you need more info than the public_profile, user_friends, email permissions, you have to submit your app for review. See https://developers.facebook.com/docs/apps/review/login for a process description.
And, first of all, you'll have to implement the OAuth Login Flow (https://developers.facebook.com/docs/facebook-login/overview/v2.0) for your app, because otherwise you'll not be able to request any non-public data from your users.
Comment: Ok, I read it and it talks about Facebook data ("Extended Profile Properties"), but what I have in my app is a form when people directly insert their data (Name, Surname, Date of birth, etc), ticking a checkbox that allows us to manage their data (just in order to choose the winner of the contest).
So, do I still need to send an approval request?
Comment: Why would you have to do that if the data isn't pulled from Facebook? Besides that, I somehow don't understand why people need to fill in the data manually, when you also could use FB functionality to do that. It's a bit strange...
Comment: A lot of people don't put their address or date of birth on Facebook, some don't even have their real name; but we want to let them participate in this contest, as long as they put real data in the form.
Comment: True, but a lot of such applications use the FB API to pre-populate the form with the given FB data. The user then can overwrite the pieces he wants to change.
Comment: And if your app is a canvas app, you could use the `signed_request` (https://developers.facebook.com/docs/reference/login/signed-request/) to get the user_id and then query the public_profile data, such as first and last name
Comment: Thank you for your help, and you are right about pre-populating data; but if I don't want to use FB data and I want to directly ask the users, I still don't know if I need that approval.
Comment: As I wrote: No. You don't pull any data from FB.

Facebook app requesting personal data


Replace a specific number in file on ssh

Question: Need to replace a line in a specific file on multiple hosts using pssh:
The file is located at a/b/c.txt and contians
myPackage /version/21 install

I need to replace this line with
myPackage /version/22 install

Answer: Use sed the stream editor. -i will replace the contents back to the file:
$ cat test.txt
blah blah blah blah blah myPackage /version/21 install blah blah blah
blah blah myPackage /version/21 install
myPackage /version/21 install blah blah
myPackage /version/21 install

$ sed -i "s/21 install/22 install/" test.txt

$ cat test.txt
blah blah blah blah blah myPackage /version/22 install blah blah blah
blah blah myPackage /version/22 install
myPackage /version/22 install blah blah
myPackage /version/22 install

Comment: and since he want to use it with pssh: `parallel-ssh -h hostslist 'sed -i "s/21 install/22 install/" a/b/c.txt'`
Answer: If the file is identical on all machines, you could just push the corrected copy of the file.
Something like:
for h in $(<hosts); do
  scp c-updated.txt $h:a/b/c.txt
done