Como imprimir apenas números que se repetem entre duas formulas diferentes?

Question: Eu tenho duas formulas de 50 números.
Uma tem múltiplos de 3 e a outra múltiplos de 7.
Código:
public static void main(String[] args) {
    BigInteger start = new BigInteger("1");
    BigInteger limit = new BigInteger("50");
    BigInteger n1 = new BigInteger("3");
    BigInteger n2 = new BigInteger("7");

    for (BigInteger a = start; a.compareTo(limit) <= 0; a = a.add(BigInteger.ONE)) {
        BigInteger copas3 = a.multiply(n1);
        BigInteger copas7 = a.multiply(n2);

        System.out.println( copas3 );
        System.out.println( copas7 );
    }
}

Veja funcionando no repl: https://repl.it/repls/AngryImpureSequences
Preciso que somente os números que se repetem entre as duas formulas sejam impressos.
Sendo assim, apenas:
21
42
63
84
105
126
147

Comment: Você quer apenas com esses códigos ou podemos sugerir aplicações diferentes?
Comment: Pode ser com aplicações diferentes, mas precisa ser BigInteger e sem Listas...
Answer: Uma solução consideravelmente simples passa por utilizar duas variáveis no for uma para o copas3 e outra para o copas7, em vez de ser a variável a para as duas. Assim pode ir aumentando sempre a que é mais baixa para dar a possibilidade de ambas serem iguais:
public static void main(String[] args) {

    BigInteger start = new BigInteger("1");
    BigInteger limit = new BigInteger("50");
    BigInteger n1 = new BigInteger("3");
    BigInteger n2 = new BigInteger("7");

    for (BigInteger a=start,b=start;a.compareTo(limit)<=0 && b.compareTo(limit) <= 0;){
        BigInteger copas3 = a.multiply(n1); //contador "a" apenas para o copas3
        BigInteger copas7 = b.multiply(n2); //contador "b" apenas para o copas7

        int comparacao = copas3.compareTo(copas7);
        if (comparacao == 0) { //escreve apenas se forem iguais
            System.out.println(copas3);
        }

        //aumenta apenas o mais pequeno
        if (comparacao <= 0) {
            a = a.add(BigInteger.ONE);
        }
        else {
            b = b.add(BigInteger.ONE);
        }
    }
}

Veja o resultado que estava a procura no Ideone
Comment: Olá @Isac tudo bem? Então, o código está bom mas infelizmente não funcionou, não tem uma outra maneira? Quando se tenta fazer uma soma progressiva na formula, por exemplo "2+4+6+8+10+12" não funciona.. lembrando que, 2+2+2+2+2+2.. = (2,4,6,8,10,12)
é diferente de 2+4+6+8+10+12 = (2,6,12,20,30,42).
Comment: @William Se apenas precisa de somar os que são iguais nos dois é uma questão de colocar a soma só dentro do `if (comparacao == 0) {`. Se não for isso, infelizmente o seu exemplo ficou muito longe do que precisa e dessa forma acabou de ter uma resposta também longe do que precisa. Sugiro assim que torne mais evidente o que está realmente a tentar fazer
Comment: @William E porque não colocou as fórmulas que precisa de facto? Torne a pergunta o mais clara possível incluindo as fórmulas que realmente está a utilizar e o resultado esperado para essas fórmulas.
Comment: Desculpa é que eu pensei que assim daria certo... vc pode me ajudar Isac?? =/
Comment: @William uma vez que a minha resposta responde a 100÷ a esta questão aconselho a finalizar esta questão e abrir uma nova exatamente com as fórmulas que precisa de utilizar e os resultados que tem que dar. Eu responderei a essa pergunta se for evidente o que pretende.
Comment: Tudo bem Isac, eu finalizei e postei outra pergunta... https://pt.stackoverflow.com/questions/276084/como-imprimir-apenas-n%C3%BAmeros-que-se-repetem-entre-duas-formulas-progressivas-dif


LAMP servers performance: Can I improve them by adding more RAMs?

Question:
Possible Duplicate:
How do you do Load Testing and Capacity Planning for Web Sites

Let me refine my questions:
1. How can I improve MySQL performance?
2. Will adding more RAMs help?
3. Will server 1 (details below) get bogged down even with more RAMs?
My setup:
Server 1 - Dual-core Xeon, 1GB RAM (max 8GB), LAMP (as image cdn server, not really using the M part)
Server 2 - Quad-core Xeon, 8GB (max 24GB), LAMP (as main server, AMP running exclusively on this server)
Current situation:
Server 1 (top command) - rather free, not much happening, CPU utilization is idle most of the time
Server 2 (top command) - heavily loaded with MySQL (where mysqld can take 100-175% CPU utilization easily)
My plan is to do primitive MySQL load balancing between the two servers (edit PHP and channel say 40% of mysql queries to Server 1).
Answer: 100-175% CPU on a 4 core machine isn't necessarily an issue.  Performance improvement has to follow this process,

Baseline - know what your performance stats are with a given workload, in fact, with several well defined workloads (i.e. 0 connections, 100 connections, 10 queries, 5 users, whatever makes sense).
Change - make a change, there are many tuning options for mysql which you need to read up on, mostly around cache sizes.
Re-baseline - after making the changes, rerun your tests to see how it behaves now.

There's no magic bullet with performance tuning, especially when you don't tell us anything about your application.
Comment: My application is my own custom cms and a wordpress blog. Actually your #1 makes sense. I will do a study on my server's baseline performance to see if that is expected (as you mentioned 100-175% on quad-core is not to be alarmed).


shell variable value not storing inside file

Question: I wrote following script, which generate random password and  store it in file
pass1=</dev/urandom tr -dc _A-Z-a-z-0-9|head -c8
echo $pass1
echo "$pass1" >/tmp/a

Above script printing generated password through "echo $pass1" command. But nothing getting stored inside created /tmp/a file .
Please let me know whats wrong i am doing.
Comment: Are you missing a pipe between `urandom` and `tr`?
Comment: @jscott, this is a [unique usage of the `<` operator](http://unix.stackexchange.com/questions/40837/bash-linux-random-cli-password-generator-and)
Comment: @MikePennington Thanks for that, I was trying to work through the syntax and what the expected output is... Both the OP's and your examples produce `tr` errors for me.
Answer: It's not necessary to store the password in an intermediate variable.  You only need this line in your shell script
</dev/urandom tr -dc _A-Z-a-z-0-9|head -c8 > /tmp/a

I saved as foo.sh, made permissions 755, and executed it under linux...
[mpenning@chestnut ~]$ uname -a
Linux chestnut.he.net 18.104.22.168-1-grsec #1 SMP Fri Sep 2 12:42:23 PDT 2011 x86_64 GNU/Linux
[mpenning@chestnut ~]$ ./foo.sh
[mpenning@chestnut ~]$ cat /tmp/a
qAUezN0-[mpenning@chestnut ~]$

Comment: This gives me:
`tr: write error: Broken pipe`
`tr: write error`
Comment: hmm works in cygwin and linux... see my edit
Comment: Thanks , this worked for me  /tmp/a
Comment: Oh my, I'm still getting the `tr` errors, yet `/tmp/a` is created and contains the expected data.  Odd, must be something screwy in my shell. +1
Comment: @vnix27, FYI... there is also the `mkpasswd` command on most linux systems, but I like the control `tr` gives me


How to make HTML5 color picker to return color name instead of color code?

Question: Choose a color for background :
<input id="background-color" type="color" />

I want this to return the color name instead of color code
e.g. "Blue" instead of "#0000ff".
Comment: what will happen if the color selected don't have a friendly name?
Comment: You need to read [Name that color article and use ntc.js library](http://chir.ag/projects/name-that-color/). Otherwise, you will need to define your own color set with each hex color code.
Comment: If you want to allow only a set of specific colors, use a drop down...
Comment: What you have tried so far? Where is source code which return color code?
Comment: The OP want to use the HTML5 color picker but want it to return color names instead of codes.
Answer:

$(document).ready(function(){
$("#showPaletteOnly").spectrum({
    showPaletteOnly: true,
    showPalette:true,
    hideAfterPaletteSelect:true,
    color: 'blanchedalmond',
change: function(color) {
        printColor(color);
    },
    palette:["red", "green", "blue"],
});
});

function printColor(color) {
alert(color.toName());
   //var text = "You chose... " + color.toHexString();
   //$(".label").text(text);

}
</script>

<link rel="stylesheet" type="text/css" href="https://bgrins.github.io/spectrum/spectrum.css">

Palette Only

<input type='text' id="showPaletteOnly"/>

Choose a color
Comment: Thanks that helped me
Answer: You can use a java script library "Name that color".


What is may be reason for charles(http snoffer) does not catch htmlUnit requests?

Question: I have test in which I get page (with help of the htmlUnit) and I may print content of that page, i.e. htmlUnit works correct and fine load my page, but I need to see how that page load all resources (css, js and other resources).
My HTTP sniff(charles) does not catch loading of page.
What may be wrong in this case?
Comment: i forget set up proxy in webClient
Answer:
My http sniff(charles) does not catch loading of page. What is may be wrong in this case?

This may happen when you're trying to sniff on local traffic. See Localhost traffic doesn't appear in Charles for workarounds.
Answer: This is desicion
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_10, "myproxyserver", myProxyPort);
Answer: HtmlUnit requires his own proxy setup.

When using Charles the proxy configuration of reals browsers (IE, FF) will be adapted automatically from Charles. But Charles does not offer this convenience for HtmlUnit.
Keep in mind that HtmlUnit does not use the java proxy settings. You have to configure this when starting your WebClient; e.g. new WebClient(BrowserVersion.FIREFOX_31, "localhost", 8888);
Like real browsers, you have additional options to configure hosts to bypass. For doing this you can create a new ProxyConfig; addHostsToProxBypass and than us webClient.getOptions.setProxyConfig to activate your new ProxyConfig.
And finally there is another option. HtmlUnit internally uses HttpClient. This implies, that you can enable HttpClient wire log (via 'org.apache.http.wire') to see all transferred info in your log.


Deploy React + Express on AWS

Question: I'm quite new to AWS and web development so please bear me if my questions look silly.

I have an Express service deployed via AWS codestar. the service address is myservice.ap-southeast-2.elasticbeanstalk.com
A react app is deployed on S3 bucket.

My question is, how can I access (like make ajax call) to Express service inside React app in production enviroment? Just append any ajax call with the service address?
The s3 bucket React app is in was created by AWS codestar, does that mean the React and Express are on the same machine? I mean if they are on same machine the ajax call will be faster, right?
Another option is, since Codestar instantiated an EC2, I can deploy the React into the EC2. So that Express on EC2 serves all API requests but for static files and index.html Express directly returns the deployed React assets.
Anyone knows which solution is better?
Answer: Well, the best solution is the one that fits better your needs. For example, if you would need server side rendering, it would be better to add your React application to the EC2 that is currently holding your express app; aside from that, I would continue using S3 bucket for your compiled React application. If your concern is about performance/latency, you can use AWS Cloudfront to improve those timings.
To access your Express app, you call your application's IP (under AWS Console > EC2 > your instance > IPv4 Public IP or Public DNS (IPv4) as your host in the ajax call.
Just to clarify, your EC2 and S3 bucket are in different machines.
Comment: Thanks Rodrigo, I'll give a try for Ajax call from React on S3 to Express.

Deploy React + Express on AWS


Not able to write document in Elastic search

Question: I am using Elastic search services from AWS. I am facing a strange issue.
It seems like elastic search node has become read only. I went through logs but there is not a single error in code. I am getting success.
I an using elastic client for nodejs.
<code>var elasticClient = new elasticsearch.Client(this.options);
elasticClient.bulk(data);
</code>
and data looks like :
<code>[
  {
    "update": {
      "_index": "elastic_index",
      "_type": "user",
      "_id": 22224689
    }
  },
  {
    "doc": {
      "id": 22224689,
      "first_name" : "John",
      "last_name" : "Deo",
      "email" : "firstname.lastname@example.com",
      "dob" : "1985-12-20",
      "company_name" : "MPLLC",
      "city" : "NY",
      "state" : "Cal",
      "country" : "USA",
      "events" : [
        {
          "event_name" : "E1",
          "date" : "2018-08-15",
          "venue" : "Some custom venue"
        }

      ]
    },
    "doc_as_upsert": true
  }
]
</code>
From last 2 year it was working fine. Suddenly stopped recording users in index.
Here is exception i am getting while writing in ES :
<code>{"error":{"root_cause":[{"type":"cluster_block_exception","reason":"blocked by: [FORBIDDEN/8/index write (api)];"}],"type":"cluster_block_exception","reason":"blocked by: [FORBIDDEN/8/index write (api)]
</code>
Any idea what's the issue.
Comment: what about the logs from ES, do you have access to it? Maybe your disk is full? possible?
Comment: See it on aws service domain. On your domain dashboard you can see the no of indexed documents along with used and free storage space. If you've storage issue then just tweak the configuration of your ES domain and scale it to another machine with large storage
Comment: I do not have access to ES logs. I'll check them as well.
Comment: once i saw minimum space was 9 GB and available space was like 18 GB. any possibility of over flow ?
Comment: What do you get when running `GET _nodes/stats/fs` ?
Comment: I do not have ES access to my current IP. Will post you shortly
Answer: I had exactly the same issue. In my case, the problem was that the Index Lifecycle Policies. I had a policy that changed the index mode to "warm" after a certain amount of hours. For some reason, the messages started to get piled in my queues and reached the Elasticsearch cluster after that time. That causes the rejection because the index mode "warm" is a read-only status. It needs to be in a "hot" to allows writing.
The solution was to extend the time to pass to the "warm" mode.
Another possible cause is if the cluster nodes start to get run out of memory or space.


Identifying the Sample space

Question: I always have doubt about identifying sample space for events. Do they vary with the events or are they always fixed?
For eg:Tossing two coins one after another
Sample Space: $\{HH,HT,TH,TT\}$.
This was fairly easy because I could kinda relate this but how do I identify sample space for difficult questions.
For e.g., $3$ persons work independently on a problem.first person has a probability of $1/5$ to solve it,second $1/4$ and third $1/5$. What is the probability that they solve the question independently.
Sample Space-The Sample space $c$ be an the combination of no of elements of the  persons solving and not solving and all solving. Had to think for $2$ - $3$ minutes, is it right? Also does the question play any role in deciding sample space or is it fixed?
My point is that identifying sample space becomes difficult whenever the situation becomes complex so is there any guidelines to follow for identifying in Sample space.Like I had to think hard for the previous question about including the combination of elements of solving and not solving of the three person.Also how do we choose these abstract elements e.g. "solved " or "not solved".
Thanks in advance
Comment: Do you know roughly what a random variable is, and what it means for two random variables to be independent?
Comment: dK about that infact I am seeing this word for first time
Comment: Seeing **what** word "for the first time"?  "Random variable" or "independent"?  You used the word "independently" in your original post.
Comment: random variable
Comment: Searched in the wiki,says it's a  variable whose  possible values are numerical outcomes of a random phenomenon and is measurable.Can u tell me the random variable in my question?
Answer: In order to apply Sample Space Theory to word problems concerning probabilities/odds, you have to do two things:

Describe the problem as a random experiment
Select a probability model

There may be more than one valid way to do this, so you can't just plug into a formula or apply a simple technique - you have to understand what you are doing. One might set things up with a 'collapsed' sample space, while another approach may give a larger space, requiring more 'counting up stuff', but they both give the same answers.

For your specific problem, I suspect you are looking for the probability that the problem gets solved by at least one person.
This is a problem that can be solved in more than one way. Below we solve it in the most complicated way. But it doesn't matter, you simply 'turn the math crank' to get the answer.
$\quad A_1 = \{1,F_1,F_2,F_3,F_4\}$
$\quad A_2 = \{1,F_1,F_2,F_3\}$
$\quad A_3 = \{1,F_1,F_2,F_3,F_4\}$
Experiment: Ask the first person if he has the solution, and he responds with an outcome listed in $A_1$. Ask the second person .... Ask the third person .... Record the answers given by the problem solvers.
Sample Space $S = A_1 \times A_2 \times A_3 $
Model: All outcomes are equally likely.
$S$ has $100$ outcomes. Let $E$ be the event that at least one of the three coordinates in $S$ is equal to $1$ (for example, $(1,F_2,F_2)$ or $(1,F_1,1)$). The set $E$ has $52$ elements so the probability that the problem gets solved is $0.52$.
Of course it looks weird to have the first person say that the outcome of his work is $F_1$ - the failures can be lumped together as one outcome. But I set this up using the discrete uniform distribution.
Exercise 1: Try using a different model with a smaller sample space. Also, try using the 'complement trick' -  What is the probability that nobody solves the problem?
Exercise 2: Get the same answer by multiplying the probabilities of independent events.

The OP writes

I always have doubt about identifying sample space for events. Do they
  vary with the events or are they always fixed?

You identify a sample space to describe all the outcomes of the random experiment. The sample space is the event that occurs with probability = 1. All other events are subsets of the (universal) sample space.

Identifying the Sample space


Predict a vector of values with constraints?

Question: I am aware of a variety of methods for simultaneously predicting multiple outcomes known sometimes as multivariate regression/analysis. However, my situation is a little more special. I am trying to predict a vector of values and where each part ranges from 0-1, and the sum of the vector must be equal to 1. A typical example of this would be population fractions of exclusive groups.
The most simple approach to this I can think of is to use OLS and rescale the predictions so they do not violate data structure. Here's an example using R and US state population data.
> #packages
> suppressPackageStartupMessages(library(tidyverse))

> suppressPackageStartupMessages(library(poliscidata))

> suppressPackageStartupMessages(library(rms))

> suppressPackageStartupMessages(library(magrittr))

> d = states

> #recode
> d$black = d$blkpct10 / 100

> d$hispanic = d$hispanic10 / 100

> d$white = 1 - d$black - d$hispanic

> #test sums
> rowSums(d %>% select(black, hispanic, white))
 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
46 47 48 49 50
 1  1  1  1  1

> #regression
> #using 4 variables
> #OLS
> (ols_black = ols(black ~ abortion_rank12 + ba_or_more + cig_tax12 + conserv_advantage, data = d))
Linear Regression Model

 ols(formula = black ~ abortion_rank12 + ba_or_more + cig_tax12 +
     conserv_advantage, data = d)

                Model Likelihood     Discrimination
                   Ratio Test           Indexes
 Obs      50    LR chi2      4.81    R2       0.092
 sigma0.0958    d.f.            4    R2 adj   0.011
 d.f.     45    Pr(> chi2) 0.3068    g        0.033

 Residuals

      Min       1Q   Median       3Q      Max
 -0.11768 -0.06635 -0.03056  0.05120  0.25075

                   Coef    S.E.   t     Pr(>|t|)
 Intercept          0.2077 0.1505  1.38 0.1744
 abortion_rank12   -0.0016 0.0013 -1.28 0.2063
 ba_or_more         0.0000 0.0042  0.00 0.9970
 cig_tax12         -0.0209 0.0211 -0.99 0.3272
 conserv_advantage -0.0014 0.0024 -0.56 0.5803

> (ols_hispanic = ols(hispanic ~ abortion_rank12 + ba_or_more + cig_tax12 + conserv_advantage, data = d))
Linear Regression Model

 ols(formula = hispanic ~ abortion_rank12 + ba_or_more + cig_tax12 +
     conserv_advantage, data = d)

                Model Likelihood     Discrimination
                   Ratio Test           Indexes
 Obs      50    LR chi2      3.11    R2       0.060
 sigma0.1010    d.f.            4    R2 adj  -0.023
 d.f.     45    Pr(> chi2) 0.5400    g        0.028

 Residuals

      Min       1Q   Median       3Q      Max
 -0.13098 -0.05541 -0.02833  0.02041  0.34087

                   Coef    S.E.   t     Pr(>|t|)
 Intercept          0.1112 0.1586  0.70 0.4869
 abortion_rank12    0.0011 0.0013  0.87 0.3904
 ba_or_more         0.0001 0.0044  0.01 0.9899
 cig_tax12         -0.0069 0.0222 -0.31 0.7558
 conserv_advantage -0.0014 0.0026 -0.56 0.5764

> (ols_white = ols(white ~ abortion_rank12 + ba_or_more + cig_tax12 + conserv_advantage, data = d))
Linear Regression Model

 ols(formula = white ~ abortion_rank12 + ba_or_more + cig_tax12 +
     conserv_advantage, data = d)

                Model Likelihood     Discrimination
                   Ratio Test           Indexes
 Obs      50    LR chi2      1.29    R2       0.025
 sigma0.1344    d.f.            4    R2 adj  -0.061
 d.f.     45    Pr(> chi2) 0.8635    g        0.024

 Residuals

      Min       1Q   Median       3Q      Max
 -0.29197 -0.11366  0.02988  0.08802  0.19163

                   Coef    S.E.   t     Pr(>|t|)
 Intercept          0.6811 0.2111  3.23 0.0023
 abortion_rank12    0.0005 0.0018  0.26 0.7938
 ba_or_more        -0.0001 0.0059 -0.01 0.9903
 cig_tax12          0.0278 0.0296  0.94 0.3517
 conserv_advantage  0.0028 0.0034  0.82 0.4167

> #get predictions
> d$ols_black = predict(ols_black)

> d$ols_hispanic = predict(ols_hispanic)

> d$ols_white = predict(ols_white)

> #inspect predictions
> d %>% select(starts_with("ols")) %>% mutate(sum = rowSums(.))
    ols_black ols_hispanic ols_white sum
1  0.08129250   0.10802760 0.8106799   1
2  0.11823321   0.08031043 0.8014564   1
3  0.14136889   0.07023172 0.7883994   1
4  0.13189772   0.07624972 0.7918526   1
5  0.10280396   0.15374533 0.7434507   1
6  0.12930893   0.11325306 0.7574380   1
7  0.06261715   0.13842026 0.7989626   1
8  0.11806119   0.12687147 0.7550673   1
9  0.11508647   0.10817106 0.7767425   1
10 0.15097533   0.08315063 0.7658740   1
11 0.06825851   0.13257364 0.7991678   1
12 0.09290831   0.11626503 0.7908267   1
13 0.12003177   0.09022122 0.7897470   1
14 0.09482377   0.12513629 0.7800399   1
15 0.14432113   0.07970917 0.7759697   1
16 0.14472842   0.08870156 0.7665700   1
17 0.14109725   0.09872039 0.7601824   1
18 0.15769332   0.06708056 0.7752261   1
19 0.09469234   0.14480432 0.7605033   1
20 0.09043738   0.14094802 0.7686146   1
21 0.10129247   0.11791403 0.7807935   1
22 0.12064080   0.10132318 0.7780360   1
23 0.11602388   0.11910634 0.7648698   1
24 0.16377700   0.09077838 0.7454446   1
25 0.12524587   0.07730750 0.7974466   1
26 0.07060243   0.10921933 0.8201782   1
27 0.12703322   0.11021081 0.7627560   1
28 0.13367696   0.07430030 0.7920227   1
29 0.15029178   0.07798233 0.7717259   1
30 0.10685246   0.12199943 0.7711481   1
31 0.07058313   0.13901865 0.7903982   1
32 0.09142246   0.12212662 0.7864509   1
33 0.10757628   0.12890212 0.7635216   1
34 0.03493501   0.13189804 0.8331670   1
35 0.13176324   0.09598102 0.7722557   1
36 0.14334473   0.06494498 0.7917103   1
37 0.10788090   0.14958898 0.7425301   1
38 0.14930675   0.08299583 0.7676974   1
39 0.09010154   0.12273524 0.7871632   1
40 0.12960586   0.09186749 0.7785267   1
41 0.12586666   0.08545994 0.7886734   1
42 0.12252536   0.09645709 0.7810176   1
43 0.12473591   0.08529285 0.7899712   1
44 0.09099896   0.08246437 0.8265367   1
45 0.16097493   0.09583502 0.7431901   1
46 0.07564659   0.14598479 0.7783686   1
47 0.05854270   0.14244206 0.7990152   1
48 0.09670796   0.09239404 0.8108980   1
49 0.10933095   0.10965351 0.7810155   1
50 0.09307565   0.09622426 0.8107001   1

> #fit accuracies
> d %>% select(starts_with("ols"), black, hispanic, white) %>% cor() %>% .[1:3, 4:6]
                  black    hispanic       white
ols_black     0.3029909 -0.17497676 -0.08992821
ols_hispanic -0.2159748  0.24547482 -0.02824165
ols_white    -0.1708902 -0.04347992  0.15944403

> #multivariate OLS
> (ols_joint = ols(cbind(black, hispanic, white) ~ abortion_rank12 + ba_or_more, data = d))
Linear Regression Model

 ols(formula = cbind(black, hispanic, white) ~ abortion_rank12 +
     ba_or_more, data = d)

                Model Likelihood     Discrimination
                   Ratio Test           Indexes
 Obs     150    LR chi2    342.38    R2       0.898
 sigma0.1911    d.f.            8    R2 adj   0.892
 d.f.    141    Pr(> chi2) 0.0000    g        0.314

 Residuals

           black hispanic    white
 Min    -0.12063 -0.12726 -0.28038
 1Q     -0.06388 -0.05599 -0.10255
 Median -0.03327 -0.02423  0.04949
 3Q      0.05821  0.01528  0.09565
 Max     0.24543  0.34242  0.18706

       Coef    S.E.   t     Pr(>|t|)
  [1,]  0.1552 0.1636  0.95 0.3442
  [2,] -0.0018 0.0022 -0.82 0.4133
  [3,]  0.0001 0.0067  0.02 0.9868
  [4,]  0.0384 0.1636  0.23 0.8146
  [5,]  0.0013 0.0022  0.62 0.5392
  [6,]  0.0012 0.0067  0.18 0.8549
  [7,]  0.8063 0.1636  4.93 <0.0001
  [8,]  0.0004 0.0022  0.21 0.8378
  [9,] -0.0013 0.0067 -0.20 0.8419

> #predictions
> d$joint_ols_black = predict(ols_joint)[, 1]

> d$joint_ols_hispanic = predict(ols_joint)[, 2]

> d$joint_ols_white = predict(ols_joint)[, 3]

> #inspect predictions
> d %>% select(starts_with("joint_ols")) %>% mutate(sum = rowSums(.))
   joint_ols_black joint_ols_hispanic joint_ols_white sum
1       0.09555303         0.11815034       0.7862966   1
2       0.12188779         0.09234947       0.7857627   1
3       0.15017948         0.06705244       0.7827681   1
4       0.14913609         0.07664226       0.7742216   1
5       0.07086325         0.14100841       0.7881283   1
6       0.11448727         0.11617229       0.7693404   1
7       0.07865753         0.14265444       0.7786880   1
8       0.10473606         0.11402244       0.7812415   1
9       0.11151654         0.10446698       0.7840165   1
10      0.14218850         0.08435132       0.7734602   1
11      0.08335854         0.13124113       0.7854003   1
12      0.09180628         0.11898908       0.7892046   1
13      0.11851983         0.09737339       0.7841068   1
14      0.09420884         0.12441664       0.7813745   1
15      0.14521110         0.07551151       0.7792774   1
16      0.13883168         0.08949833       0.7716700   1
17      0.12714583         0.08709083       0.7857633   1
18      0.15582745         0.06610206       0.7780705   1
19      0.08789627         0.13914201       0.7729617   1
20      0.08224830         0.14009239       0.7776593   1
21      0.10274571         0.11314933       0.7841050   1
22      0.12575708         0.09286476       0.7813782   1
23      0.10862763         0.11478390       0.7765885   1
24      0.14372208         0.08017760       0.7761003   1
25      0.13056949         0.08268238       0.7867481   1
26      0.08490326         0.12719051       0.7879062   1
27      0.10986041         0.10728667       0.7828529   1
28      0.13662966         0.08628645       0.7770839   1
29      0.14754681         0.08020051       0.7722527   1
30      0.10152407         0.12076966       0.7777063   1
31      0.07674517         0.14264299       0.7806118   1
32      0.09003875         0.12057784       0.7893834   1
33      0.08785901         0.11761214       0.7945288   1
34      0.07293158         0.14274317       0.7843252   1
35      0.12928101         0.08956415       0.7811548   1
36      0.15418246         0.06904484       0.7767727   1
37      0.07973434         0.13343390       0.7868318   1
38      0.15280485         0.07494186       0.7722533   1
39      0.10672641         0.11489554       0.7783781   1
40      0.12393384         0.09383805       0.7822281   1
41      0.13476186         0.08676737       0.7784708   1
42      0.11662975         0.09760812       0.7857621   1
43      0.13301661         0.08860231       0.7783811   1
44      0.11545267         0.10572082       0.7788265   1
45      0.14112283         0.09369495       0.7651822   1
46      0.07479938         0.14226225       0.7829384   1
47      0.06919598         0.14370501       0.7870990   1
48      0.12051018         0.09824650       0.7812433   1
49      0.09809657         0.10401752       0.7978859   1
50      0.09703090         0.11336115       0.7896079   1

> #accuracies
> d %>% select(starts_with("joint_ols"), black, hispanic, white) %>% cor() %>% .[1:3, 4:6]
                        black    hispanic       white
joint_ols_black     0.2679728 -0.22508994 -0.02574528
joint_ols_hispanic -0.2605606  0.23149306  0.01537504
joint_ols_white    -0.1416356  0.07306988  0.04870974

Thus, in my case, as far as I understand, the OLS does not actually know the values must range from 0-1, and sum to 1, yet this somehow happens with the data when doing regressions one by one, as well as in the joint OLS (i.e. multivariate regression).
My questions are:

How does one generally model outcomes that have a specified range, such as 0-1? This is somewhat different from modeling binary data because while in that case the predictions must be within 0-1 (when transformed from logits), the training data is not binary in this case.
In general, how does one force outcome constraints such as the sum=1 in the above case?
How does OLS know in the above code not to output inappropriate values?

I imagine the above issues can be approached with explicit Bayesian approaches where the priors reflect the outcome ranges, though not sure how one would handle the sum constraint. Is there a frequentist approach as well?
ETA
Stephan Kolassa points to this prior question, which also concerns the issue of proportional data, also called compositional data. However, it differs from the present question in that the question concerns time series data, whereas the present does not, and that I'm explicitly interested in retaining modeling on the proportional data, whereas the approach in the answer question is to transform the data. I am essentially looking for e.g. links to R packages (or maybe Python) that enable modeling on proportional data and predictions on the same scale.
Comment: I do not think you will be successful in terms of finding a way to model the original proportional data, but having predictions respect the compositional constraints. Could you explain just why you would prefer to model the original proportions? Modeling transformed data is very common, e.g., in logistic regression. Yes, interpretation becomes more difficult, but it's better to have a model that is hard to interpret, rather than no model at all...
Comment: Well, that paper is explicitly about forecasting, and of course on the original scale. You need to back-transform the predictions from the transformed scale. (You may need some bias-correction in the back-transformation, [similar to what you need in Box-Cox transformations](https://otexts.com/fpp2/transformations.html), and I don't recall whether that paper goes into that.)
Comment: [The paper](https://doi.org/10.1016/j.ijforecast.2016.11.008) I cite in [my answer](https://stats.stackexchange.com/a/380293/1352) to the proposed duplicate is concerned with time series forecasting, but the transformation it uses is applicable for more general [tag:compositional-data]
Comment: Thanks, Stephan. That's not entirely what I was looking for, but good to know it's called compositional data. I'll be googling around for that term.
Comment: Sometimes the models become uninterpretable. For instance, I would want to keep things in original metric so that I can make predictions in that metric. Let's say we have a dataset with missing compositional data, then it would be useful to be able to impute these, but that requires a model that can output appropriate predictions. I did skim the paper you posted, but it doesn't seem to be able to predict values in proper scale (I may be wrong).
Comment: For clarity: Your R code shows the multivariate regression performed with *different predictors* -- only two of the four variables that are used in the separate regressions. This is why you get different results. If you performed multivariate regression with the same predictors, you'd get the same fit.
Comment: nanoman, yes, but that's irrelevant for this question. The reason I did not use all 4 in the second case is that this made the model malfunction. Try yourself and see.
Answer: In response to you showing and asking about the results of OLS models, in general, OLS is not appropriate for truncated data, as it will not adjust the estimates of the coefficients to take into account the effect of truncation, resulting in biased coefficients.  It is similar to estimating a logistic regression using OLS.  But just as with logistic regression, a linear model will sometimes fit just as well, or almost as well, as a non-linear model.  (That's why there is something called a linear probability model that models continuous probability values between 0 and 1 using OLS and that is sometimes used by practitioners instead of logistic regression.)  The fact that you obtained "appropriate" predicted values is simply due to your data allowing for a good linear approximation of the data generating process.
You'll probably find good examples of modeling compositional data, but for reference, since you asked for general information and are using R, the censReg package allows for estimation of a censored regression model with both lower and upper limits of the dependent variable that can be any numbers, which is a generalization of the the standard Tobit model with a dependent variable left-censored at zero:
https://cran.r-project.org/web/packages/censReg/vignettes/censReg.pdf
The model will be estimated using Maximum Likelihood, as is usually done for censored/truncated/limited dependent variable models, not using OLS.
Comment: But the values are not approximate in the above case, that's what's so odd. They are exactly summed to 1 (7 digits), but I don't see why that would be the case. In fact, when I wrote the example code, I wanted it to illustrate the problem with using the linear probability model approach, as you also allude to.
Comment: Also, if you decide to use separate models, I recommend you check out Seemingly Unrelated Regression.  It's appropriate (and not equivalent to OLS) when your separate regressions have correlated error terms.  With this method, the equations are treated as independent from one another, except that the errors are modeled as jointly normally distributed.  All equations are estimated simultaneously.
Comment: [Note](https://en.wikipedia.org/wiki/Seemingly_unrelated_regressions): "SUR is in fact equivalent to OLS ... when each equation contains exactly the same set of regressors on the right-hand-side."
Comment: @nanoman, sure, with one small caveat, as described in the comment to this [answer](https://stats.stackexchange.com/a/103498/241093)
Answer: First, your OLS models automatically respect the sum-to-1 constraint (up to roundoff error) because both the models and the constraint are linear in the observations. That is, the sum of the predictions for each input is equivalent to the prediction of an OLS model fit to the sum of observations (a constant of 1). What is not guaranteed is that the OLS predictions will be between 0 and 1, although they happen to be for your data.
The multinomial logit model provides one natural way to transform between unconstrained linear-regression predictions and "probabilities" (positive fractions that sum to 1). This is the transformation $y_{it}\mapsto \log(y_{it}/y_{0t})$ in the linked answer (where here you would take $t$ as a sample index, not time).
To make a proper fit systematically, you would need to decide on your error model, which determines the likelihood function. For example, do you imagine that the model can precisely predict probabilities from which the individuals are sampled, and the only error in the population fractions comes from the multinomial distribution based on the population size? If so, you could directly use the likelihood from the multinomial logit model, casting your observations as counts of individuals. But this is implausible for the US state example, because factors not in the model have a much greater effect than the tiny multinomial fluctuations in a state population of millions.
More likely, you just want a reasonable error model with a tractable likelihood, which could be a Gaussian error with unknown variance on the linear predictor functions (log-odds-ratios) of the multinomial logit -- i.e., OLS applied to the transformed observations.


subtração de meses com erros em 31 dias (strtotime)

Question: Estou desenvolvendo um gráfico utilizando a biblioteca chart.js. Está funcionando perfeitamente.
Acontece que hoje, mês de Maio com 31 dias, as subtrações dos meses acontece de forma errada.
$mhoje = date("M");

$m1 = date('M', strtotime(' - 1 month'));
$em1 = ucfirst(strftime('%b', strtotime($m1)));

echo "$mhoje / $m1 / $em1"; // May / May / Mai

Ou seja, hoje menos 1 month ainda é Maio.
Utilizo apenas date("M") pois preciso apenas do nome do mês e não das datas, mas já testei completo, como date("Y-M-d").
Nos outros dias isso funciona perfeitamente.
Preciso obter os últimos 12 meses a partir da data de hoje.
Answer: Conforme a documentação: https://www.php.net/manual/en/datetime.formats.relative.php
Para obter o ultimo dia de um mês deve-se usar last day, você pode ver no link da documentação aonde esta escrito "Day-based Notations", ou abaixo:

Formato
Descrição
Examplos

yesterday
Meia-noite de ontem
yesterday 14:00

midnight
A hora é configurada para 00:00:00

today
A hora é configurada para 00:00:00

now
Agora - isto é simplesmente ignorado

noon
A hora é configurada para 12:00:00
yesterday noon

tomorrow
Meia-noite de amanhã

back of hour
15 minutos após a hora informada
back of 7pm, back of 15

front of hour
15 minutos antes da hora informada
front of 5am, front of 23

first day of
Define o dia para o primeiro dia do mês atual. Essa frase é usada de melhor forma, junto com um nome de mês.
first day of January 2008

last day of
Define o dia para o último do mês corrente. Essa frase é usada de melhor forma, junto com um nome de mês.
last day of next month

ordinal space dayname space of
Calcula o número ordinal x-th da semana do mês corrente.
first sat of July 2008

last space dayname space of
Calcular o último dia da semana do mês corrente.
last sat of July 2008

number space? (unit | 'week')
Manipula porções de tempo relativas onde o valor seja um número.
+5 weeks, 12 day, -7 weekdays

ordinal space unit
Manipula porções de tempo relativas onde o valor seja um texto.
fifth day, second month

ago
Nega todos os valores das porções de tempo relativas anteriores.
2 days ago, 8 days ago 14:00, 2 months 5 days ago, 2 months ago 5 days, 2 days ago

dayname
Move para o próximo dia deste nome.
Monday

reltext space week
Manipula o formato especial weekday + last/this/next week.
Monday next week

E para obter o mês anterior deve usar previous month, na documentação localiza aonde esta escrito Used Symbols, ou abaixo:

Descrição
Formato

dayname
sunday | monday | tuesday | wednesday | thursday | friday | saturday | sun | mon | tue | wed | thu | fri | sat

daytext
weekday | weekdays

number
[+-]?[0-9]+

ordinal
first | second | third | fourth | fifth | sixth | seventh | eighth | ninth | tenth | eleventh | twelfth | next | last | previous | this

reltext
next | last | previous | this

space
[ \t]+

unit
(('sec' | 'second' | 'min' | 'minute' | 'hour' | 'day' | 'fortnight' | 'forthnight' | 'month' | 'year') 's'?) | weeks | daytext

Logo você pode combinar ambos em:
last day of previous month

Traduzindo, "o ultimo dia do mês anterior". O uso de -1 month deve ser devido a considerar 30 dias como a unidade de valor, veja o teste online: https://ideone.com/CwMD39
Comment: @JuniorLike faça um loop, se o mês atual é 5 você vai fazer 4 loops executando o tempo relativo obtido e pegando todos ultimos dias de todos 4 meses anteriores, dá para pegar o mês atual em formato de numero usando `$mesatual = date('n')`, então salva o valor em uma variavel e faz -1 e joga em um loop `for ($i = $mesatual - 1; $i > 0; $i--) { ... }`
Comment: Eu testei e funcionou. Acredito que eu falhei na formulação da pergunta. Eu preciso obter dos últimos 12 meses. `last day of previous month` eu obtenho o mês anterior corretamente. Mas quando preciso obter de todo o ano?
Comment: Obrigado pela ampla e rápida resposta!
Comment: @JuniorLike no strtotime no segundo parametro vá passando o valor da ultima data obtida do loop anterior, e pode salvar em um array a cada loop.
Comment: Sim, vi seu código mas pensei em montar um array, acho que vai ser melhor pra eu trabalhar com a biblioteca.
Comment: no seu `for` de exemplo ele sempre para no mês 1. Não volta para o mes 12, 11 e assim por diante. Você poderia me ajudar, por favor? Ou abro uma nova pergunta?
Comment: @JuniorLike como eu disse no comment anterior, tem que salvar o valor retornado do strtotime em uma variavel, que vai ser um "Unix timestamp", e colocar no segundo parametro do proximo strtotime.


Django Forms UpdateView: How to alert user after successful update?

Question: I'm using a django UpdateView and cripsy forms to handle my form processing. I'd like to show an alert or popup on the page after the model has successfully updated, i.e. after validation and save.
If I use the onSubmit parameter to show an alert, this will just show after the submit button is pressed, but I only want it to show after the valid model has been updated.
Is there an easy way to do this without re-writing a lot of the class methods?
Thank you.
Comment: Please add front end and backend code. Only then i can suggest you.
Answer: You need to use messages framework . Here is an example:
# in views.py:

from myapp.forms import ContactForm
from django.views.generic.edit import FormView

class ContactView(FormView):
    template_name = 'contact.html'
    form_class = ContactForm
    success_url = '/thanks/'

    def form_valid(self, form):
        # This method is called when valid form data has been POSTed.
        # It should return an HttpResponse.
        form.send_email()
        success_message = 'Success! We just sent you an email to confirm'
        messages.success(request, success_message)
        return super().form_valid(form)

Then, in your template (again from the docs):
{% if messages %}

    {% for message in messages %}

{{ message }}

     {% endfor %}

{% endif %}


Add content type machine name to an attribute in the body tag?

Question: I figured this would be easily done in the template_preprocess_html function, but I haven't found a way to do it... yet.
I need to add the content type machine name to a "data-init" attribute in the body tag.  How can this be done exactly?
End result:
Answer: template_preprocess_html should be the right function to start with.
So your code should look like the following:
function mytheme_preprocess_html(&$variables) {
  if ($node = menu_get_object()) {
    $variables['attributes_array']['data-init'][] = drupal_html_class('node-type-' . $node->type);
  }
}

Make sure to replace mytheme with your custom theme and clear Drupal cache.


Does $\bigcup\emptyset$ equal $\bigcup\{\emptyset\}$?

Question: For any collection of sets $A = \{A_i\ : i \in I \}$, define
$$\bigcup A = \bigcup_{i \in I} A_i$$
Question: Is the following true?
$$\bigcup \emptyset = \bigcup \{\emptyset \}$$
The right-hand side, using the definition, is simply the empty set.  However, I'm not sure how I'm supposed to interpret the left - hand side.  I'm guessing the fact that the left-hand side is the empty set follows "vacuously", but I'm not too certain.
Can somebody help me understand?
Comment: @thyde641 The symbol on RHS denotes taking the union of all elements in the set, so it would be just the empty set, not the set containing the empty set.
Comment: Related: http://math.stackexchange.com/questions/370188/empty-intersection-and-empty-union
Comment: I think the RHS is $\{\emptyset\}$, not $\emptyset$; while the LHS is just $\emptyset$. Remember, $\cup A_i = \{x : x\in A_i$ for some $i.\}$, hence $\emptyset$ is an element of the RHS.
Comment: I have changed the title, so that it better describes the question. If needed, edit/improve it again. It is useful to make the titles of your questions more informative. E.g., *Why does $a<b$ imply $a+c<b+c$?* is much more useful for other users than *A question about inequality.* From [How can I ask a good question?](http://meta.math.stackexchange.com/a/589/): *Make your title as descriptive as possible. In many cases one can actually phrase the title as the question, at least in such a way so as to be comprehensible to an expert reader.*
Answer: The union $\bigcup\varnothing$ is empty vacuously (the union of no sets).
The union $\bigcup\{\varnothing\}$ is empty because $\bigcup\{x\}=x$, and in this case $x$ is the empty set.
So yes, they are equal.
Note that this is not a trait unique to the empty set. Take $\omega$ for example, the first infinite ordinal. $\omega$ has the property $\omega=\bigcup\omega$, so we also have $\bigcup\omega=\bigcup\{\omega\}$. The same is true for any limit ordinal.
Answer: The union on the RHS is the empty set: if $x$ is in the union, then $x\in \emptyset$, a contradiction, thus there are no elements in the union.
The union on the LHS, as a union over an empty indexed collection of sets $\{A_i\}_{i\in I}$ with $I=\emptyset$ is also the empty set: if $x$ is in the union, then there must be some index $i\in I$ with $x\in A_i$. Since no such $i$ exists, there are no elements in the union.
Answer: Recall that (by definition, see, for example, Wikipedia)
$$x\in\bigcup \mathcal S \Leftrightarrow (\exists S\in \mathcal S)x\in S$$
So $\bigcup\emptyset=\emptyset$, since for no element $x$ there exists $S\in\emptyset$ fulfilling $x\in S$. (Simply because there is no $S$ in $\emptyset$.)
Answer: $
\newcommand{\calc}{\begin{align} \quad &}
\newcommand{\calcop}[2]{\\ #1 \quad & \quad \text{"#2"} \\ \quad & }
\newcommand{\endcalc}{\end{align}}
$To expand on the other answers, here is a slightly more detailed proof that both sets are empty, but for different reasons.
For the first set, we calculate its members $\;x\;$ as follows:
$$\calc
x \in \bigcup \emptyset
\calcop{\equiv}{definition of $\;\bigcup\;$}
\langle \exists V : V \in \emptyset : x \in V \rangle
\calcop{\equiv}{definition of $\;\emptyset\;$}
\langle \exists V : \text{false} : x \in V \rangle
\calcop{\equiv}{logic: simplify}
\text{false}
\endcalc$$
For the second set, we similarly calculate for all $\;x\;$:
$$\calc
x \in \bigcup \{\emptyset\}
\calcop{\equiv}{definition of $\;\bigcup\;$}
\langle \exists V : V \in \{\emptyset\} : x \in V \rangle
\calcop{\equiv}{definition of $\;\{\ldots\}\;$}
\langle \exists V : V = \emptyset : x \in V \rangle
\calcop{\equiv}{logic: one-point rule}
x \in \emptyset
\calcop{\equiv}{definition of $\;\emptyset\;$}
\text{false}
\endcalc$$
Therefore both sets are empty, and therefore they are equal.


How to parse XML with namespaces in tags using BeautifulSoup?

Question: I have an xml link (http://api.worldbank.org/v2/countries) with the following data:
<wb:countries xmlns:wb="http://www.worldbank.org" page="1" pages="6" per_page="50" total="299">
<wb:country id="ABW">
<wb:iso2Code>AW</wb:iso2Code>
<wb:name>Aruba</wb:name>
<wb:region id="LCN" iso2code="ZJ">Latin America & Caribbean </wb:region>
<wb:adminregion id="" iso2code=""/>
<wb:incomeLevel id="HIC" iso2code="XD">High income</wb:incomeLevel>
<wb:lendingType id="LNX" iso2code="XX">Not classified</wb:lendingType>
<wb:capitalCity>Oranjestad</wb:capitalCity>
<wb:longitude>-70.0167</wb:longitude>
<wb:latitude>12.5167</wb:latitude>
</wb:country>
<wb:country id="AFE">
<wb:iso2Code>ZH</wb:iso2Code>
<wb:name>Africa Eastern and Southern</wb:name>
<wb:region id="NA" iso2code="NA">Aggregates</wb:region>
<wb:adminregion id="" iso2code=""/>
<wb:incomeLevel id="NA" iso2code="NA">Aggregates</wb:incomeLevel>
<wb:lendingType id="" iso2code="">Aggregates</wb:lendingType>
<wb:capitalCity/>
<wb:longitude/>
<wb:latitude/>
</wb:country>
</wb:countries>

I tried to parse the incomeLevel but it returns (None)
how to reach the text (ex: High income) in the xml text using BeautifulSoup?
I tried this code but it does not work as it should!
import requests
#import re
from bs4 import BeautifulSoup
url = 'http://api.worldbank.org/v2/countries'

response = requests.get(url)
soup = BeautifulSoup(response.content, 'lxml')
countries= soup.findAll('wb:country')
for country in countries:
    name = country.find("wb:name").text
    code = country.find('wb:iso2code').text
    incomeLevel = country.find('wb:incomeLevel', {"iso2code":"XD"})
    print(f"{name}, {code}, {incomeLevel}")

Answer: Thanks for posting the question. I think you have a few things wrong in the code. This will help you rectify your errors.

Instead of findAll you should use the new method i.e. find_all from the bs4 API. Please refer this link https://www.crummy.com/software/BeautifulSoup/bs4/doc/#method-names
In the second argument of BeautifulSoup please specify "lxml-xml" or simply "xml" as it instructs beautifulsoup to generate a XML document otherwise it'll just generate a plain HTML document and in your question you wish to parse and extract stuff from a XML document. Please refer the following link https://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser

Please refer the following code snippet :)
import requests
from bs4 import BeautifulSoup

url = 'http://api.worldbank.org/v2/countries'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'xml')
countries = soup.find_all('wb:country')
for country in countries:
    name = country.find('wb:name').text
    code = country.find('wb:iso2Code').text
    income_level = country.find('wb:incomeLevel').text
    print(f'Name: {name} Code: {code} Income Level: {income_level}')

Comment: There is no need to decode using "utf-8-sig". Why do you say that this is the most important point?
Comment: I have also tried it out and `.decode('utf-8-sig')` was not needed.
Comment: Try to use the response content as is. I can guarantee it won't be parsed. I've tried it out :)
Comment: '<?xml version="1.0". if you make a request to the url and check the text you see a wierd character which suggests it is utf-8-sig. I guess you are right, it's not needed. bs4 auto detects the encoding somehow. But it's better to be explicit about it, wouldn't result in any surprises down the line :)
Comment: The weird characters are the Byte Order Mark. It can be present in an UTF-8 file, but that is not something that you should have to worry about. Byte order has no meaning in UTF-8. Why would there be surprises down the line?
Comment: You are right. I thought i needed to decode it in utf-8-sig because when i was trying to pass response.text to the `BeautifulSoup` object it turned out to be empty. Hence i thought those characters needs to be removed and a direct way to do that is `response.content.decode("utf-8-sig")` but yeah `response.content` works as well.
Answer: What happens?
The parser has problems to parse the BeautifulSoup object with xml namespaces well, cause it is created as HTML, thats why you get a None and you won't get the text while you not adding .text method. So it is a combination of both.
How to achieve?
Pass xml as parser to BeautifulSoup to handle namespaces properly and as valid xml not as html:
soup = BeautifulSoup(response.content, 'xml')

Add the .text to your result:
incomeLevel = country.find('incomeLevel').text

If you like to get only the countries with incomeLevel and iso2code="XD", change your selector and use css selectors instead of find_all():
countries = soup.select('country:has(incomeLevel[iso2code="XD"])')
for country in countries:
    name = country.find("name").text
    code = country.find('iso2Code').text
    incomeLevel = country.find('incomeLevel').text
    print(f"{name}, {code}, {incomeLevel}")

NOTE: xml parser works case sensitive find('iso2code') won't work, you have to change to find('iso2Code')
Example
NOTE: In new code better use actually syntax find_all() instead of outdated findAll()
import requests
#import re
from bs4 import BeautifulSoup
url = 'http://api.worldbank.org/v2/countries'

response = requests.get(url)
soup = BeautifulSoup(response.content, 'xml')
countries= soup.find_all('country')
for country in countries:
    name = country.find("name").text
    code = country.find('iso2Code').text
    incomeLevel = country.find('incomeLevel').text
    print(f"{name}, {code}, {incomeLevel}")

Output
Aruba, AW, High income
Africa Eastern and Southern, ZH, Aggregates
Afghanistan, AF, Low income
Africa, A9, Aggregates
Africa Western and Central, ZI, Aggregates
Angola, AO, Lower middle income
Albania, AL, Upper middle income
Andorra, AD, High income
Arab World, 1A, Aggregates
United Arab Emirates, AE, High income
...

Answer: Here's how to use the ElementTree class from the xml module, and especially take into account the namespace:
from xml.etree import ElementTree as ET

ns = {'wb': 'http://www.worldbank.org'}

countries = ET.parse('input.xml').getroot()

for country in countries.findall('wb:country', namespaces=ns):
    name = country.find("wb:name", namespaces=ns).text
    code = country.find('wb:iso2Code', namespaces=ns).text

    incomeLevel = None
    for x in country.findall('wb:incomeLevel', namespaces=ns):
        if x.get('iso2code') == 'XD':
            incomeLevel = x.text
            break

    print(f"{name}, {code}, {incomeLevel}")

When I run that on the sample you provided, input.xml, I get:
Aruba, AW, High income
Africa Eastern and Southern, ZH, None


Simple question about Fourier transformations on $L^1$

Question: I was looking at my definition of Fourier transformation of a function $u \in L^1(\mathbb{R}^N)$, and the following question came up.
Question: Why is it defined for $ u \in L^1(\mathbb{R}^N)$? Why not for $u \in L^2(\mathbb{R}^N)$... $L^p(\mathbb{R}^N)$ with $p \in [1,\infty]$? What is the intuition of letting $u$ be in $L^1(\mathbb{R}^N)$?
Thanks for any possible explanation.
Comment: The integral defining the Fourier transform need  not exist if $f \in L^{p}$ with $p >1$.
Comment: You can extend the Fourier transform to all $L^p$ spaces. But you have to be careful because, as @KaviRamaMurthy pointed out, the usual integral definition does not work.
Answer: The first and simplest reason might be that the Fourier transform $\widehat{u}$ exists if $u  \in L^{1}(\mathbb{R}^{N})$ and - this should be the second reason -  has some nice properties which can be easily proven. For instance if $u \in L^{1}(\mathbb{R}^{N})$ then $\widehat{u} \in C_{0}(\mathbb{R}^{N})$ - so it defines a continuous bounded function that vanishes at infinity.
On the other hand, the Fourier transform $\widehat{\cdot} : L^{1}(\mathbb{R}^{N}) \to C_{0}(\mathbb{R}^{N})$ has also 'bad' properties. For example it is not bijective  as a mapping defined on $L^{1}(\mathbb{R}^{N})$ to $C_{0}(\mathbb{R})$.
By using the results on $L^{1}(\mathbb{R}^{N})$ it can be shown that for all $u \in L^{1}(\mathbb{R}^{N}) \cap L^{2}(\mathbb{R}^{N})$ we have $\widehat{u} \in L^{2}(\mathbb{R}^{N})$ with $\left\|u\right\|_{L^{2}} = \left\| \widehat{u} \right\|_{L^{2}}$. Therefore it can be extended in a unique way to an unitary operator on $L^{2}(\mathbb{R}^{N})$.
So in this sense the Fourier transform can be defined on $L^{2}(\mathbb{R}^{N})$, but have in mind that this actually is an extension of the 'original' Fourier transform on $L^{1}(\mathbb{R}^{N})$.
Comment: Really nice answer, thanks!
Comment: Sir. about your 3-th line. "if $u \in L^1(\mathbb{R}^N)$ then $\hat{u}\in C_0(\mathbb{R}^N)"$... I think that this may not be true... I know that if $u \in C_c ^{\infty}(\mathbb{R}^N)$ then this is true since $\hat{u} \in C_0(\mathbb{R}^N) \cap L^1(\mathbb{R}^N)$, but this part by just letting be $u \in L^1(\mathbb{R}^N)$. I'm not sure if this is sufficient. Please correct me if I'm wrong. Or does it follow from $\overline{C_c ^{\infty}(\mathbb{R}^N)} = L^1(\mathbb{R}^N)$?
Comment: It is a popular result called "Riemann-Lebesgue" lemma. Indeed it it easy to see, that $\widehat{u}$ is a continuous bounded function for $u \in L^{1}(\mathbb{R}^{N})$. The "vanishes at infinity"-part needs some more care.


Getting drush to install missing dependencies automatically into libraries

Question: How to get drush to automatically install libraries or ignore dependencies?
Trying to automate enabling modules in batch with Drush
c:\2016\sites\all\modules>drush en views_slideshow_cycle -y
Module views_slideshow_cycle doesn't meet the requirements to be         [error]

enabled.
You need to install the jQuery cycle plugin. Create a directory in       [error]

sites/all/libraries called jquery.cycle, and then copy
jquery.cycle.all.min.js or jquery.cycle.all.js into it. You can find
the plugin at http://malsup.com/jquery/cycle. (Currently using jQuery
Cycle Library Library required for Views Slideshow Cycle)

c:\2016\sites\all\modules>

Answer: It's the module that handles "libraries" dependencies - not Drush. AFAIK you can't force Drush to ignore hook_requirements implemented by the module in (the install file). Some modules, for example Colorbox, provides a Drush command to download the missing libraries dependencies, but it's not automated.
Answer: I'm guessing that this Drupal 7, since you're installing to sites/all/modules. My guess is that you've installed Views Slideshow 7.x-3.1.
In Drupal 7, drush en typically will download and enable required modules automatically. But other kinds of libraries, like jQuery libraries, won't get installed automatically for you. In this kind of case, you definitely want to see if the module has a README file.  And for Views Slideshow, indeed it does.
From README.txt:
Installation
=============

1) Assure that you are using Views 3.x. http://drupal.org/project/views
2) Upload/install the Libraries API module. http://drupal.org/project/libraries
3) Upload/install the ctools library. http://drupal.org/project/ctools
4) Create a sites/all/libraries directory on your server.
5) Create a directory within sites/all/libraries named jquery.cycle.
6) Locate/download the jQuery cycle plugin. http://malsup.com/jquery/cycle
7) Upload/install the jQuery cycle plugin: place it inside the jquery.cycle
   directory.
8) Enable the Views slideshow and Views Slideshow: Cycle modules
9) You should now see the new views style option called "Slideshow"

These aren't the simplest instructions, but at least they're clear.
The good news is that in Drupal 8, drush uses the Composer installation system, and this kind of requirement will get auto-installed for you. But for Drupal 7, sometimes you'll need to track down the manual instructions.
Comment: Can I use composer with drupal 7?
Comment: You can, using the Composer Manager module.  I do this a lot with Drupal 7.  But that assumes that the Views Slideshow module (or whatever module has the dependencies) defines itself to composer (using a composer.json file).  My sense is that it doesn't.


Comparision between dates

Question: In my application i have to retrieve date from database which lies between some dates.In my database i am storing date as string.I also wrote the query.
This is my sample table:
id    item  price  startdate    enddate
1     ass    78    04/08/2012   14/09/2012
2     err    89    04/08/2012   10/09/2012.
3     dffg   44    04/08/2012   18/08/2012

My query is :
public Cursor getIncometitle(String grpsdb,String date) throws SQLException
{
    Cursor mCursor = db.rawQuery(
             "SELECT * FROM incomexpense WHERE date like '"+date+"' OR enddate > date  AND category='Income' AND groups='"+grpsdb +"'   ORDER BY date DESC" , null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}

In the above condition i am checking enddate>date. In this what is coming is,i think its comparing the dates (04 and 14) and(04and 10),it is greater so all the data is coming .But i don't need that,if enddate month is greater that startdate means i have to get the data.
Thanks in advance..
Comment: Could you explain this a little further, I can't understand which is the question? Do you want SQL to only compare the months?
Comment: luanjoit ..I will explain my scenario clearly.in my above table start date and end date is there..Some of the end dates lies in next month and some in this month.Now if i am going to display data for next month means i have to show the item which end date lies in next month.So i checked the condition if end date is gretaer than current date i have to retrieve that data..as i said in my question it is comparng the date and gives all item.Because the enddate(14,18,10) all is coming,.But my query should return (data which month lies in next month)(i.e)item 1 nd 2 should come.
Comment: you can check this for sqlite http://sqlite.awardspace.info/syntax/sqlitepg08.htm
Answer: Leaving aside the SQL injection attack vulnerability (use parameterized queries instead of inserting values directly into your SQL) you should store your dates in a sortable format, e.g. yyyy-MM-dd. At that point, a lexicographic ordering will also give you the right chronological ordering, so your comparisons will work.
If you can, it would actually be better if you could store your dates as dates in the database - it depends on whether your database supports that.

Comparision between dates


Why is he using .every method in that line of code?

Question: I watch a tutorial where the author makes a e-commerce website.
Now he iterates through the products and returns only the products that match the filters
The filters structure is {"color": "red", "size": "M"} , or only one of the key/value pair.
const [filteredProducts, setFilteredProducts] = useState([]);

useEffect(() => {
    setFilteredProducts(
        products.filter((item) =>
            Object.entries(filters).every(([key, value]) =>
                item[key].includes(value)
            )
        )
    );
}, [products, filters]);

This is what he wrote. I don't understand why he added .every method.
Comment: They are selecting all the products that match `every` filter (since there are multiple of them) ^^
Answer: Not sure what you mean by saying "I don't understand why he added .every method.", but here's a explanation of the every method and the code.

The every() method of Array instances tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

Array.prototype.every iterates & calls the callback function through array itself, and returns true only if every callback func returns true.
so, if you take a look at the code
products.filter((item) =>
    Object.entries(filters).every(([key, value]) =>
        item[key].includes(value)
    )
)

it iterates through all the products, and for each product it checks if the product satisfies all the filters by using every method and the filters object to filter the products that only match all current filters. By using Object.entries & every method, you don't have to change your code even if other filters are added to the filters object.
Comment: `every()` does return a boolean, and that boolean is being fed to the `filter()` function.
Comment: I thought that every method return a Boolean and therefore is not suitable for our purpose in the example. But from what you said I understand that every is used only to iterate through the array and not for the Boolean.
Comment: As @RoryMcCrossan said, yes `every()` does return a boolean and it is used by [`filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) function, to judge if the current product should be excluded or not
Comment: If any one of the filters were not included in the item, it would return false. Not a great tutorial if that's not explained.


Magento StackExchange Role in Magento Developer Career

Question: I have few questions related to Magento StackExchage Platform.

How much important is this platform in a Magento developer career?
Is it must to join to become expert at Magento?
Does it help developer in getting good job in well reputed company?
Do you think a Magento developer can become expert without joining this platform?

Answers to these questions will be really appreciated.
Answer:
How much important is this platform in a magento developer career?

For me this platform has been a big help from time to time. Some weird issue I ran into which had a ready to go solution posted here. In general monitoring questions coming by gives you a good idea of what is going on with the platform

Is it must to join to become expert at magento?

Nope, you can definately become an expert without. It's just another tool or place to learn sstuff.

Does it help developer in getting good job in well reputed company?

The company I'm with takes reputation online into account since it says something about that person. You still need to be a good developer but if someone has a high rank it means he can at least explain stuff well and solves issues.

Do you think a magento developer can become expert without joining this platform?

You can, for sure. But this platform helps
Answer:
How much important is this platform in a magento developer career?

Not sure about this. For sure it is important in your everyday job if you work with magento intensively. You get a lot of answers and ideas on this website for problems or impediments you may have in your project. So this platform certainly helps.

Is it must to join to become expert at magento?

No. But it may help along the way.

Does it help developer in getting good job in well reputed company?

Indirectly, yes, but it's not enough. It helps you build your skills that may help you get a good job.
Directly...maybe. I've seen some job posts that under the section "Nice to have" ask for reputation on the stackexchange network. This won't get you the job but it may get you priority on an interview.

Do you think a magento developer can become expert without joining this platform?

Yes, it's possible.  I know a few.
Comment: Thanks for sharing detailed answer. Really appreacited
Answer: Here is my opinion on your questions.
How much important is this platform in a Magento developer career?
I think MageStackExchangeange is the best and very useful platform for Magento developers. This platform is very useful for newbies also very helpful for the experienced developer of Magento to find solutions in a very quick way.
Is it must join to become expert at Magento?
No, But if you join MSE. You can learn Magento development very fastly by other answers and also by solving other problems.
Does it help a developer in getting a good job in the well-reputed company?
Yes, If you have a good profile in MSE. It definitely helps to get a job in the well-reputed company.
Do you think a Magento developer can become expert without joining this platform?
Yes, But as said before if you join MSE then you can learn Magento very quickly by posting your problems and by solving other problems.
Comment: Nice explanation +1 from me!


SQL query taking a long time to execute

Question: USE Pooja
GO
----Create TestTable
CREATE TABLE TestTable(RtJobCode VARCHAR(20), RtProfCode smallint,RtTestCode smallint,ProfCode smallint,TestCode smallint)
----INSERT INTO TestTable using SELECT
INSERT INTO TestTable (RtJobCode, RtProfCode,RtTestCode,ProfCode,TestCode)
SELECT RtJobCode,RtTestCode,TestCode,RtProfCode,ProfCode
FROM dbo.ResultTest,dbo.Test,dbo.Profiles
WHERE RtTestCode=ANY(Select TestCode from dbo.Test)

----Verify that Data in TestTable
SELECT *
FROM TestTable

GO

The above code tries to take out entries from a table called resutltest and profiles and test,
The problem was during creation of a cube i was encountering some data which was not consistent in all the tables,
So i tried a join on the tables but as the tables contained a huge number of columns it was'nt feasible so tried making this code which just keeps on executing without stopping
and not displaying any data
Resulttest's Rttestcode is foreign key from testcode
Comment: It's pretty easy -- there's a "code" link to click.  Even easier, just add four spaces before each line of code.
Comment: If you highlight the code, and click the "{ }" button, it will try to format the code for you.  You can also use back ticks (`...`) around the code, or indent it 4 spaces.
Comment: sry i dont know how to post code in the forum!!!
Comment: ill remember this next time!!!
Answer: Your query is very slow because it is making a cartesian product between ResultTest, Test and Profiles. you need to provide "join" conditions to link the tables together.
SELECT RtJobCode
     , RtTestCode
     , TestCode
     , RtProfCode
     , ProfCode
FROM dbo.ResultTest r
JOIN dbo.Test t
  ON r.RtTestCode = t.TestCode
JOIN dbo.Profiles p
  ON r.RtProfCode = p.ProfCode

I speculate that this is the query you are looking for. Note the conditions that link ResultTest and Test together and the condition that links ResultTest and Profiles together.
Comment: can u tell if i wanna bring in more columns from the individual tables along with this view and insert it into a new table!!!
Answer: USE Pooja
GO
----Create TestTable
CREATE TABLE TestTable(RtJobCode VARCHAR(20), RtProfCode smallint,RtTestCode smallint,RtCenCode smallint,LabNo int,ProfCode smallint,ProfRate money,ProfName varchar(100),TestCode smallint,TestRate money,TestName varchar(100),TestCategory varchar(50),Cost money)
----INSERT INTO TestTable using SELECT
INSERT INTO TestTable (RtJobCode, RtProfCode,RtTestCode,RtCenCode,LabNo,ProfCode,ProfRate,ProfName,TestCode,TestRate,TestName,TestCategory,Cost)
SELECT RtJobCode
     , RtProfCode
     , RtTestCode
     , RtCenCode
     , LabNo
     , ProfCode
     , ProfRate
     , ProfName
     , TestCode
     , TestRate
     , TestName
     , TestCategory
     , Cost
FROM dbo.ResultTest

JOIN dbo.Test
  ON ResultTest.RtTestCode = Test.TestCode
JOIN dbo.Profiles
  ON ResultTest.RtProfCode = Profiles.ProfCode

Comment: If you post code, XML or data samples, **please** highlight those lines in the text editor and click on the "code samples" button ( `{  }` ) on the editor toolbar to nicely format and syntax highlight it!

SQL query taking a long time to execute


Extract ZIP file to memory (not disk)

Question: I need to extract a zip file to memory (not to the disk). I cannot save it to a directory, even temporarily.
Is there a way to extract a zip file just to memory, and perform "File" functions there?
I can't open the file as a file stream because this doesn't allow me to read the metadata (last write time, attributes, etc). Some but not all the file attributes can be read from zip entry itself but this is insufficient for my purposes.
I've been using:
using (ZipArchive archive = ZipFile.OpenRead(openFileDialog.FileName))  // Read files from the zip file
{
    foreach (ZipArchiveEntry entry in archive.Entries)
    {
        if(entry.Name.EndsWith(".txt", StringComparison.InvariantCultureIgnoreCase)) // get .txt file
        {
            FileStream fs = entry.Open() as FileStream;
        }
    }
}

Thanks.
Comment: [`ZipArchiveEntry.ExternalAttributes`](https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.ziparchiveentry.externalattributes)? What other attributes do you need to access?
Comment: You can assume all attributes.
Comment: Use MemoryStream instead of FileStream.  After filling MemoryStream you need to set the position to zero before reading.
Comment: I'm surprised that your cast to FileStream succeeds. I don't see how that could be. Open returns a Stream from which you can read.
Answer: The code below presents a way to get the file into memory as an array of strings, but it is unclear as to what file functions you are asking for. Other commenters have mentioned ExternalAttributes, which is OS dependent therefore it is relevant to have more information as to the problem space.
using System;
using System.IO;
using System.IO.Compression;

namespace StackOverflowSampleCode
{
    class Program
    {
        ///
        /// Validate the extension is correct
        ///
        /// <param name="entry"></param>
        /// <param name="ext"></param>
        /// <returns></returns>
        static bool validateExtension(ZipArchiveEntry entry, string ext)
        {
            return entry.Name.EndsWith(
                        ext,
                        StringComparison.InvariantCultureIgnoreCase);
        }

        ///
        /// Convert the entry into an array of strings
        ///
        /// <param name="entry"></param>
        /// <returns></returns>
        static string[] extractFileStrings(ZipArchiveEntry entry)
        {
            string[] file;
            // Store into MemoryStream
            using (var ms = entry.Open() as MemoryStream)
            {
                // Verify we are at the start of the stream
                ms.Seek(0, SeekOrigin.Begin);

                // Handle the bytes of the memory stream
                // by converting to array of strings
                file = ms.ToString().Split(
                    Environment.NewLine, // OS agnostic
                    StringSplitOptions.RemoveEmptyEntries);
            }

            return file;
        }

        static void Main(string[] args)
        {
            string fileName = "";

            using (ZipArchive archive = ZipFile.OpenRead(fileName))
            {
                foreach (var entry in archive.Entries)
                {
                    // Limit results to files with ".txt" extension
                    if (validateExtension(entry, ".txt"))
                    {
                        var file = extractFileStrings(entry);

                        foreach (var line in file)
                        {
                            Console.WriteLine(line);
                        }

                        Console.WriteLine($"Last Write Time: {entry.LastWriteTime}");
                        Console.WriteLine($"External Attributes: {entry.ExternalAttributes}");
                    }
                }
            }
        }
    }
}

Extract ZIP file to memory (not disk)


Homotopy equivalence of Topologist's sine curve

Question: I'm currently studying algebraic topology and I have the following question.
There are two similar topological spaces as below.
Below is a union of two Topologist's sine curves, and a square is added in the above.

I guess they are not homotopy equivalent, but I cannot find the reasoning.
Can anybody give some explanation? Thank you in advance.
(Added)
Let $T = \{(x,sin(\frac{1}{x})):x \in (0,1)\} \cup \{0\} \times [-1,1]$ be the Topologist's sine curve in $\mathbb{R}^2$.
Similarly, $T' = \{(x,sin(\frac{-1}{x})):x \in (-1,0)\} \cup \{0\} \times [-1,1]$
Below : $T \cup T'$
Above : $\{0\} \times [-1,1]$ is replaced by $[-1,1] \times [-1,1]$, and sine curve is translated accordingly.
Comment: Why do you think they are not homotopy equivalent? Also, it would be nice if you could write down your description of the spaces in unambiguous notation (e.g. what does "with a square added" mean?), rather than a picture. It it possible someone might give an answer based on a reading of your picture that is not what you intended.
Comment: @William Thank you for the suggestion! I will write down the description of the spaces as well :)
Comment: My bad! @PaulFrost => +1 and +1
Answer: They are not homotopy equivalent.
Let
$$L = \{0\} \times [-1,1], \\ R = [-1,1]^2 , \\  S_\pm = \{(\pm x,\sin \frac 1 x) \mid x \in (0,1) \}, \\ S'_\pm = \{(\pm (x +1),\sin \frac 1 x) \mid x \in (0,1) \}.$$
We have to compare
$$T = S_+ \cup S_- \cup L$$
and
$$T' = S'_+ \cup S'_- \cup R .$$
Both spaces have three path components. Let $f : T \to T'$ be any map. It maps each of the path components $S_+, S_-,L$ of $T$ into one of the path components  $S'_+,S'_-,R$ of $T'$.
Let $f^* : P \to P'$ the associated map between the set of path components. In order that $f$ be a homotopy equivalence, a necessary condition is that $f^*$ is a bijection. We shall show the assumption that $f^*$ is a bijection leads to a contradiction.
Case 1. $f(L) \subset S'_+$.
Since $f^*$ is bijective, we must have $f(S_+ \cup S_-) \subset S'_- \cup R$. The closure of $S_+ \cup S_-$ contains $L$, thus  $f(L)$ must be contained in the closure of $f(S_+ \cup S_-)$ and thus contained in $S'_- \cup R$. This contradicts the assumption in case 1.
Case 2. $f(L) \subset S'_-$.
This is treated similarly.
Case 3. $f(L) \subset R$.
Then $f(S_+)$ must be contained in one of $S'_\pm$. W.l.o.g. we may assume $f(S_+) \subset S'_+$. It follows that $f(S_-) \subset S'_-$.
Since $L$ is contained in the closure of both $S_\pm$, we see that $f(L)$ must be contained in the closure of both $f(S_\pm)$ and thus in the closure of both $S'_\pm$. But these closures are disjoint, a contradiction.
Comment: Wow, this is a beautiful argument using the basic property of continuity. Thanks Paul!


Mascara em campo html não funciona

Question: Boa tarde,
Tenho uma pagina em minha aplicação para cadastrar clientes , nada fora do normal.
Nesta pagina , tenho alguns campos ( cif ,rg , telefone,telefone_2 ,etc) que ao digitar são formatados com javascript com a seguinte função:
*Transformando o telefone : 1199999999 em (11)9999-9999
 // Mascaras Javascript
function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}

function id( el ){
    return document.getElementById( el );
}

// aqui começa as mascaras

function mtel(v){ //MASCARA PARA TELEFONE

    v=v.replace(/\D/g,"");             //Remove tudo o que não é dígito
    v=v.replace(/^(\d{2})(\d)/g,"($1) $2"); //Coloca parênteses em volta dos dois primeiros dígitos
    v=v.replace(/(\d)(\d{4})$/,"$1-$2");    //Coloca hífen entre o quarto e o quinto dígitos
    return v;
}

window.onload = function atribuiMascaras(){ // FUNCAO QUE É ACIONADO AO CARREGAR A PAGINA ( WINDOW.ONLOAD )

    id('txtCel').onkeyup = function(){ //ATRIBUI O CAMPO COM ID txtCel A MASCARA DE TELEFONE
        mascara( this, mtel );
    }

Acontece que, eu posso acessar essa pagina e ja ter parcialmente os dados desse contato que esta virando um cliente.
Caso eu ja tenha esses dados parciais , ao acessar a pagina  , pego o ID do contato na Url e faço uma chamada num controller para pegar os dados desse cliente.
$(document).ready(function selectDadosContato(){

var id_contato = <?=$dados[0];?>;
var tipo = '<?=$dados[1];?>';

jQuery.ajax
    ({

        url: "/admin2/controllers/contatos/Contatos.php?id="+id_contato+"&tipo="+tipo, //URL de destino
        dataType: "json", //Tipo de Retorno
        success:
        function(data) {

                var d = data.data_nascimento;
                var d = d.split('-');
                var data_nasc = d[2] + '/' + d[1] + '/' + d[0];

                document.getElementById('txtNome').value = data.nome;
                document.getElementById('txtCel').value = data.telefone;

             } //function(data);
        });
});

Eis que surge meu problema , quando faço a requisição e pego os dados do contato e atribuo o valor recebido nos campos html , eu precisaria que as mascaras dos campos fossem ativadas, mas os campos são preenchidos da maneira que eles vem do banco.
Desde ja, grato
Answer: Basta atribuir as mascara no retorno do seu ajax
mascara( document.getElementById('txtCel'), mtel);

jQuery.ajax
    ({

        url: "/admin2/controllers/contatos/Contatos.php?id="+id_contato+"&tipo="+tipo, //URL de destino
        dataType: "json", //Tipo de Retorno
        success:
        function(data) {

                var d = data.data_nascimento;
                var d = d.split('-');
                var data_nasc = d[2] + '/' + d[1] + '/' + d[0];

                document.getElementById('txtNome').value = data.nome;
                document.getElementById('txtCel').value = data.telefone;

                //Chame a sua função de mascara
                mascara( document.getElementById('txtCel'), mtel);

             } //function(data);
        });
});

Comment: Voce não pode aceitar uma resposta em 6 minutos....  Deu certinho amigo , tinha quebrado a cabeça a tarde tentando resolver isso  hehe . Obrigado !
Comment: @jefeson Assis , uma duvida , tentei chamar a mascara para outro campo : mascara( document.getElementById('txtFixo'), tel); . Ficando assim com a chamada para 2 campos distintos. Mas assim , só funcionou a mascara do campo 'txtFixo' e a do campo 'txtCel' não funcionou .Por que ?
Comment: Não deveria chamar a variável `mtel` - `mascara( document.getElementById('txtCel'), mtel);`? caso não funcione, verifique o console do browser para ver se aparece algum erro
Comment: Bom dia @Jeferson  , erro meu ao digitar. Ainda sim , tentei colocando todas as mascaras ( de cpf,rg , telefone, email , etc )  e somente a ultima mascara chamada é que funciona. No console , ele não apresenta nem um erro ou aviso
Answer: Usa https://plugins.jquery.com/jquery.inputmask
$(document).ready(
   $("#Cep").inputmask("99999-999");
   $('#telefone').inputmask("(99) 9999-9999[9]");
});

Comment: Bom dia Alexandre , realmente não conhecia o plugin. Na doc tem muito material sobre e parece ser bem completo. Deu certinho as mascaras. Obrigada !

Mascara em campo html não funciona


db_affected_rows gives fatal error in drupal 7

Question: How can i get the number of affected rows in drupal 7? db_affected_rows which i was used in d6 is not working in d7. It shows an error
 $bool = db_query("SELECT * FROM {template_config} WHERE user_role = ':d'",array(':d'=> $id));
        $num = db_affected_rows();
        if ($num != 0) {
            $result = db_fetch_array($bool);
            $custom_theme = $result['theme_name'];
        }

i want to check if there is any affected rows.db_num_rows also is not working in d7
How can i do this in d7?
Answer: There is no db_affected_rows() in Drupal 7 I'm afraid. Also you don't need include the quotes in your variable placeholder. Your code might look a little more like this:
$results = db_query("SELECT * FROM {template_config} WHERE user_role = :role", array(':role' => 'the_role'))->fetchAll();

if (count($results) > 0) {
  $first_result = $results[0];

  $custom_theme = $first_result->theme_name;
}

It could be even simpler though:
if ($result = db_query("SELECT * FROM {template_config} WHERE user_role = :role", array(':role' => 'the_role'))->fetchObject()) {
  $custom_theme = $result->theme_name;
}

Answer: Drupal 7 is based on PDO as database abstraction layer. That's why a lot of functions have changed compared to D6. To get the number of affected rows, you can use the rowCount method.
By the way, in Drupal 7 you can simply foreach() over a database query result. When using foreach, you don't even need to check if the result has more than 0 rows. See db_query documentation for more info. The following example comes from that page:
<?php
$uid = 1;
$result = db_query('SELECT n.nid, n.title, n.created FROM {node} n WHERE n.uid = :uid', array(':uid' => $uid));
// Result is returned as a iterable object that returns a stdClass object on each iteration
foreach ($result as $record) {
  // Perform operations on $record->title, etc. here.
  // in this example the available data would be mapped to object properties:
  // $record->nid, $record->title, $record->created
}
?>

Answer: $bool = db_query("SELECT * FROM {template_config} WHERE user_role = ':d'",array(':d'=> $id));
        $num = db_affected_rows($bool);

OR
$num = db_num_rows($bool);

    if ($num != 0) {
        $result = db_fetch_array($bool);
        $custom_theme = $result['theme_name'];
    }

Try this you have forgot passing the argument.
Answer: In drupal 7 use $pdos->rowCount(); to get the affected rows count.
$result = db_query('<your query>');
$affected_rows = $result->rowCount();


Error creating a VM in Terraform for GCP with KMS key (Error creating instance: googleapi: Error 503)

Question: i can't create a VM on GCP using terraform, i want to attach a kms key in the attribute "kms_key_self_link", but when the machine is being created, time goes and after 2 minutes waiting (in every case) the error 503 appears. I'm going to share my script, is worthly to say that with the attribute "kms_key_self_link" dissabled, the script runs ok.
data "google_compute_image" "tomcat_centos" {
  name = var.vm_img_name
}

data "google_kms_key_ring" "keyring" {
  name     = "keyring-example"
  location = "global"
}

data "google_kms_crypto_key" "cmek-key" {
  name     = "crypto-key-example"
  key_ring = data.google_kms_key_ring.keyring.self_link
}

data "google_project" "project" {}

resource "google_kms_crypto_key_iam_member" "key_user" {
  crypto_key_id = data.google_kms_crypto_key.cmek-key.id
  role          = "roles/owner"
  member        = "serviceAccount:email@example.com"
}

resource "google_compute_instance" "vm-hsbc" {
  name         = var.vm_name
  machine_type = var.vm_machine_type
  zone         = var.zone

  allow_stopping_for_update = true
  can_ip_forward            = false
  deletion_protection       = false

  boot_disk {
    kms_key_self_link = data.google_kms_crypto_key.cmek-key.self_link
    initialize_params {
      type = var.disk_type
      #GCP-CE-CTRL-22
      image = data.google_compute_image.tomcat_centos.self_link
    }
  }

  network_interface {
    network = var.network
  }

  #GCP-CE-CTRL-2-...-5, 7, 8
  service_account {
    email  = var.service_account_email
    scopes = var.scopes
  }

  #GCP-CE-CTRL-31
  shielded_instance_config {
    enable_secure_boot          = true
    enable_vtpm                 = true
    enable_integrity_monitoring = true
  }
}

And this is the complete error:
Error creating instance: googleapi: Error 503: Internal error. Please try again or contact Google Support. (Code: '5C54C97EB5265.AA25590.F4046F68'), backendError

Comment: Try creating a new service account and use it on your deployment or try it on a new project.
Comment: I can´t do that, but I have resolved it in another way. Thx for your response :)
Answer: I solved this issue granting to my compute service account the role of encrypter/decripter through this resource:
resource "google_kms_crypto_key_iam_binding" "key_iam_binding" {
  crypto_key_id = data.google_kms_crypto_key.cmek-key.id
  role          = "roles/cloudkms.cryptoKeyEncrypter"

  members = [
    "serviceAccount:email@example.com",

  ]
}


What does "92 in 43 in hundred" mean?

Question: I installed glance app in my phone. It gives me wallpapers on home.

What does "92 in 43 in hundred" mean?
Answer: The 'Hundred' part is reference to the new competition in England called The Hundred.
Specifically, this is showing Jemimah Rodrigues of the Northern Superchargers, who was not out with 92 runs, from 43 balls faced, in their match against Welsh Fire.

What does "92 in 43 in hundred" mean?


How to send mail when node is published

Question: I have a content type "Properties". By default, content type is set to "Not published"
When users add a node of the content type, node is not published.
Another role gets a notification and then publishes the node.
Once the node is published, an email should be sent out to multiple roles.
I used Rules module to do that but emails are not being send out once nodes are published. I also created other rules and emails are being send out just fine. I am not sure why this specific rule does not send emails.
Answer: From your scenario it seems that your event is wrong. The content is already saved before. So you should use an event like after updating existing content, of type "Properties". If you then also replace your condition by 2 conditions to check that (a) the unsaved node is unpublished and (b) the saved node is published, you should be close to what you want.
Try it, and see what happens ...the bets are open ...

How to send mail when node is published


After AndroidStudio update,Gradle file not getting downloaded

Question: I am working on Android Studio 1.5.1. Using this version of Studio, I can create new projects, there are no issues for creating new projects. But when I try to import other projects (existing in my Studio sample projects or Git Projects) into my work space, the gradle try to download the files, but it takes a long time and, unfortunately, I still can't open any single  existing project in my Studio 1.5.1:
Comment: What is your Android Studio error log/event log?
Comment: You have to change the Gradle versions of old project to new one.
Comment: 1.Try to use graldew assemble -info in terminal to get more info.2.And can you try to import a project with less depends?3.Go to the build.gradle in old project and change gradle version to new project's  gradle version.5.Try to upgrade to android studio 2.0,it's faster.
Comment: Still i didint get any updation for studio 2.0
Comment: Hey i dowloaded the studio 2.0 and still facing the same issue
Answer: I was facing the same problem after gradually upgrading from Android Studio 1.3 to 1.4 & now to 1.5.
I fixed the problem by performing a clean install of the latest Android Studio after completely uninstalling the existing version.
Further, I also downlaoded API 22 & 21 in addition to most current API 23.
Building the gradle still takes upto 20 minutes for certain older projects of Eclipse, but it's at least opening now.


How can I include one variable if another specific variable is included in LASSO?

Question: People often ask if it is appropriate when interaction effect is included when the corresponding main effect is not. When I do LASSO in R or Python, can I force one variable should be included if some other specific variable is included?
I'd like to know how to do this in the computer or theoretically.
(So if it is not implemented in the existing package, I could write a code for it)
Answer: You can use the "group lasso", which forces groups of variables to be taken as a whole into the regression. There's two good packages for the group lasso in R, grplasso and gglasso.
(That is, you assign one group to your specific main effect variable and its interaction term, and all of the other variables are in their own groups of size 1.)
Comment: I appreciate your valuable advice, but it does not seem to solve my problem. In the 'group lasso' you mentioned, a group of variables must be included together or excluded together. In my problem, however, there is priority. For example, main effects have the highest priority and interaction terms and quadratic terms are the second. If X2^2 should be included, then X2 must be included. But including X2 and excluding X2^2 is okay. This is the difference from the group lasso you mentioned.


How to find the number of differentially expressed genes due to treatment from a dataset whose p-value is already adjusted?

Question: I have a dataset with gene Id, gene name, log2 fold change and raw p-value. How to find the BH  adjusted p-value for each gene? and  How can I find how many have expression was upregulated by treatment by at least 2-fold from the adjusted p-value.The p-value is in column 4 and log2 fold change is in column 3.
Comment: The title says the p-values are already adjusted, but the question says the dataset includes the raw p-value. Do you mean that the p-values in the dataset have already been adjusted?
Answer: Assuming you are dealing with raw p-values like your question states, you can use the p.adjust function in R, which can compute adjusted p-values for a wide variety of correction measures.
I would create a new column called padj using p.adjust and then filter to only include genes with a log2FC greater than 2 and padj below your desired alpha threshold.
Using R, here is code that computes BH adjusted p-values and then filters based on your criteria. Note that df would be your dataframe
library(dplyr)
alpha <- .05

diffexp <- df %>%
   mutate(padj = p.adjust(pvalue, method="BH")) %>%
   filter(log2FC > 0, padj < alpha)
```

Comment: Can you please explain the purpose of choosing log2 fold change greater than 2?
Comment: I interpreted your question as 2 fold from log2 fold change although now I see you said from adjusted p-value. I'm not sure exactly what you mean by "at least 2-fold from the adjusted p-value". Could you elaborate? In my field it is common to filter for genes with `padj` < .05 and absolute value of `log2FC` > 2 to prioritize genes with large differential expression and significant evidence to back up the fold change
Comment: Sorry for the confusion. I have a dataset which has the values for the log2 fold change from that dataset I want to see the genes those were upregulated due to treatment. Which I want to see after adjusting the p-values. I am not being able to figure out how to do that in R. Thank you.
Comment: I've updated my code to now include **any** genes that are upregulated and considered significant at `padj` < .05. Note this code assumes the non-treatment group was coded as the reference level so that the log2FoldChange values are for the treatment group compared to non-treatment.


Multinomial theorem with imposed conditions

Question:
The number of ways in which 12 identical balls can be grouped in four marked non-empty sets $P, Q, R, S$ such that $n(P) < n(Q)$ is?

The answer to the above problem is the number of positive integral solutions of the expression $$P + Q + R + S = 12$$
when $P<Q$. I know that with no conditions imposed, the number of solutions is $^{12-1}C_{4-1} = ^{11}C_3$. How would one account for the condition when $P<Q$? Please explain the concept in detail in addition to providing a solution to the problem (which is given just as an example).
Answer: So first of all, the solution without that condition is $\binom{12-1}{4-1}$. Since the sets are all non - empty.
Notice that $Q-P$ is a positive integer as well. The number of solutions with condition imposed is the number of positive integer solutions of
$2P+(Q-P)+R+S=12$
Since the number is small, You can count solutions for $2P=2,4,6,8$.
$2P=2,\ \ (Q-P)+R+S=10$ there are $\binom{10-1}{3-1}=36$ solutions
$2P=4,\ \ (Q-P)+R+S=8$ there are $\binom{8-1}{3-1}=21$ solutions
$2P=6,\ \ (Q-P)+R+S=6$ there are $\binom{6-1}{3-1}=10$ solutions
$2P=8,\ \ (Q-P)+R+S=4$ there are $\binom{4-1}{3-1}=3$ solutions
Total $70$ solutions
Answer: Let $a,b,c,d\ge0$. Then set $|P|=a+1$, $|Q|=a+b+2$, $|R|=c+1$, $|S|=d+1$ where $2a+b+c+d+5=12$. We want to count the non-negative solutions to
$$
2a+b+c+d=7\tag1
$$
Considering the coefficients of $x^n$ in
$$
\overbrace{\left(1+x^2+x^4+x^6+\dots\right)\vphantom{{x^2}^3}}^a\overbrace{\left(1+x+x^2+x^3+\dots\right)^3}^{b,c,d}\tag2
$$
$(1)$ gives a generating function of
$$
\begin{align}
&\frac1{1-x^2}\left(\frac1{1-x}\right)^3\\
&=\frac1{1+x}\left(\frac1{1-x}\right)^4\\
&=\frac12\left(\frac1{1-x}+\frac1{1+x}\right)\left(\frac1{1-x}\right)^3\\
&=\frac12\left(\frac1{1-x}\right)^4+\frac14\left(\frac1{1-x}+\frac1{1+x}\right)\left(\frac1{1-x}\right)^2\\
&=\frac12\left(\frac1{1-x}\right)^4+\frac14\left(\frac1{1-x}\right)^3+\frac18\left(\frac1{1-x}+\frac1{1+x}\right)\frac1{1-x}\\
&=\frac12\left(\frac1{1-x}\right)^4+\frac14\left(\frac1{1-x}\right)^3+\frac18\left(\frac1{1-x}\right)^2+\frac1{16}\left(\frac1{1-x}+\frac1{1+x}\right)\tag3
\end{align}
$$
Therefore,
$$
\begin{align}
&\left[x^n\right]\frac1{1-x^2}\left(\frac1{1-x}\right)^3\\[3pt]
&=\textstyle\frac12(-1)^n\binom{-4}{n}+\frac14(-1)^n\binom{-3}{n}+\frac18(-1)^n\binom{-2}{n}+\frac1{16}(-1)^n\binom{-1}{n}+\frac1{16}\binom{-1}{n}\\[3pt]
&=\frac12\binom{n+3}{n}+\frac14\binom{n+2}{n}+\frac18\binom{n+1}{n}+\frac1{16}+\frac1{16}(-1)^n\\
&=\frac{4(n+3)(n+2)(n+1)+6(n+2)(n+1)+6(n+1)+3+3(-1)^n}{48}\\
&=\frac{(4n+14)(n+3)(n+1)+3\left(1+(-1)^n\right)}{48}\tag4
\end{align}
$$
For $n=7$, we get
$$
\frac{42\cdot10\cdot8}{48}=70\tag5
$$
Comment: Perfect! This is what I was looking for
Comment: Is there something wrong with this answer, or is the downvote simply spiteful?
Comment: I have expanded the explanation of the generating function in case that was the problem.
Answer: $\newcommand{\bbx}[1]{\,\bbox[15px,border:1px groove navy]{\displaystyle{#1}}\,}
 \newcommand{\braces}[1]{\left\lbrace\,{#1}\,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\,{#1}\,\right\rbrack}
 \newcommand{\dd}{\mathrm{d}}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\expo}[1]{\,\mathrm{e}^{#1}\,}
 \newcommand{\ic}{\mathrm{i}}
 \newcommand{\mc}[1]{\mathcal{#1}}
 \newcommand{\mrm}[1]{\mathrm{#1}}
 \newcommand{\pars}[1]{\left(\,{#1}\,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\,{#2}\,}\,}
 \newcommand{\totald}[3][]{\frac{\mathrm{d}^{#1} #2}{\mathrm{d} #3^{#1}}}
 \newcommand{\verts}[1]{\left\vert\,{#1}\,\right\vert}$
\begin{align}
&\bbox[5px,#ffd]{\sum_{P = 1}^{\infty}\sum_{Q = 1}^{\infty}
\sum_{R = 1}^{\infty}\sum_{S = 1}^{\infty}\bracks{P < Q}
\bracks{z^{12}}z^{P + Q + R + S}}
\\[5mm] = &\
\bracks{z^{8}}\sum_{P = 0}^{\infty}z^{P}
\sum_{Q = 0}^{\infty}z^{Q}\bracks{Q > P}
\sum_{R = 0}^{\infty}z^{R}\sum_{S = 0}^{\infty}z^{S}
\\[5mm] = &\
\bracks{z^{8}}\pars{1 - z}^{-2}\sum_{P = 0}^{\infty}z^{P}
\sum_{Q = P + 1}^{\infty}z^{Q} =
\bracks{z^{8}}\pars{1 - z}^{-2}\sum_{P = 0}^{\infty}z^{P}\,
{z^{P + 1} \over 1 - z}
\\[5mm] = &\
\bracks{z^{7}}\pars{1 - z}^{-3}\sum_{P = 0}^{\infty}z^{2P} =
\bracks{z^{7}}\pars{1 - z}^{-3}\,{1 \over 1 - z^{2}}
\\[5mm] = &\
\bracks{z^{7}}\pars{1 - z}^{-4}\pars{1 + z}^{-1} =
\sum_{k = 0}^{7}\bracks{z^{7}}\pars{1 - z}^{-4}\pars{-1}^{k}z^{k}
\\[5mm] = &\
\sum_{k = 0}^{7}\pars{-1}^{k}\bracks{z^{7 - k}}\pars{1 - z}^{-4} =
-\sum_{k = 0}^{7}\pars{-1}^{k}\bracks{z^{k}}\pars{1 - z}^{-4}
\\[5mm] = &\
-\sum_{k = 0}^{7}\pars{-1}^{k}{-4 \choose k}\pars{-1}^{k} =
-\sum_{k = 0}^{7}\pars{-1}^{k}{k + 3 \choose 3}
= \bbx{\Large 70} \\ &
\end{align}