Reorder dataframe column that includes specific names in specific order

Question: I have the data below which has 1044 rows. The id contains in the first 87 rows 87 unique names with a specific order. Then from row 88 the same names with same order are repeated until row 175 and so on until row 1044.
Now I want to reorder the dataset by id by gathering all the same names as many times they exist (1044/87=12) but with the same order they exist in the initial dataframe so for example the first 4 rows of my new data will be Name1,Name88,Name175,Name262 and so on..
# Creating a sequence of 87 unique names
unique_names <- paste0("Name", 1:87)

# Repeat the unique names to get 1044 rows
repeated_names <- rep(unique_names, length.out = 1044)

# Generating random numeric values for the second column
random_values <- runif(1044) # Change parameters according to your requirements

# Creating the dataframe
data <- data.frame(id = repeated_names, values = random_values)

Comment: Aren't you just sorting by name then? Like `data[order(with(data, factor(id, unique(id)))),]`
Answer: If I understand you correctly, you want to sort the dataframe by id, but use the order in which id first appears. You can do this with the following one-liner (note the row names are in the specified order):
data[order(with(data, factor(id, unique(id)))),]
#>          id       values
#> 1     Name1 5.263096e-01
#> 88    Name1 8.578252e-01
#> 175   Name1 3.479284e-01
#> 262   Name1 5.296923e-01
#> 349   Name1 1.085899e-01
#> 436   Name1 3.922578e-01
#> 523   Name1 3.078238e-01
#> 610   Name1 6.257815e-01
#> 697   Name1 6.245708e-01
#> 784   Name1 1.373686e-01
#> 871   Name1 7.947614e-01
#> 958   Name1 1.039647e-02
#> 2     Name2 5.335738e-01
#> 89    Name2 3.393713e-01
#> 176   Name2 6.222394e-01
#> 263   Name2 3.016773e-01
#> 350   Name2 8.678417e-01
#> 437   Name2 3.494456e-01
#> 524   Name2 2.297880e-01
#> 611   Name2 4.217584e-01
#> 698   Name2 2.800260e-01
#> 785   Name2 5.557307e-01
#> 872   Name2 4.699509e-01
#> 959   Name2 2.238131e-01
#> 3     Name3 1.496468e-01
#> 90    Name3 2.112112e-01
#> 177   Name3 3.951932e-01
#> 264   Name3 7.924696e-01
#> 351   Name3 5.275981e-01
#> 438   Name3 6.269663e-02
#> 525   Name3 2.767372e-01
#> 612   Name3 1.210715e-01
#> 699   Name3 4.055651e-01
#> 786   Name3 6.611223e-01
#> 873   Name3 7.281214e-01
#> 960   Name3 6.972039e-01
#> 4     Name4 5.093340e-01
#> 91    Name4 6.280333e-02
#> 178   Name4 5.187594e-01
#> 265   Name4 5.429091e-01
#> 352   Name4 7.461857e-01
#> 439   Name4 4.905147e-01
#> 526   Name4 6.319581e-01
#> 613   Name4 2.297919e-03
#> 700   Name4 1.401638e-01
#> 787   Name4 2.014982e-01
#> 874   Name4 2.428456e-01
#> 961   Name4 7.313308e-01
#> 5     Name5 7.550440e-01
#> 92    Name5 5.882545e-01
#> 179   Name5 7.004829e-01
#> 266   Name5 2.107642e-01
#> 353   Name5 3.826765e-01
#> 440   Name5 3.326024e-01
#> 527   Name5 4.256065e-01
#> 614   Name5 4.536161e-01
#> 701   Name5 5.773471e-01
#> 788   Name5 8.345956e-01
#> 875   Name5 9.561155e-01
#> 962   Name5 4.040662e-01
#> 6     Name6 8.341755e-01
#> 93    Name6 6.813080e-02
#> 180   Name6 2.466805e-02
#> 267   Name6 5.896061e-01
#> 354   Name6 7.327837e-01
#> 441   Name6 2.120784e-01
#> 528   Name6 3.720217e-01
#> 615   Name6 8.222597e-01
#> 702   Name6 3.182815e-01
#> 789   Name6 7.549763e-01
#> 876   Name6 4.358885e-01
#> 963   Name6 2.613530e-02
#> 7     Name7 9.598459e-01
#> 94    Name7 7.708241e-01
#> 181   Name7 9.302728e-01
#> 268   Name7 4.958397e-02
#> 355   Name7 4.876716e-01
#> 442   Name7 1.168121e-01
#> 529   Name7 8.276435e-03
#> 616   Name7 7.948343e-01
#> 703   Name7 2.227685e-01
#> 790   Name7 5.726201e-02
#> 877   Name7 8.184423e-01
#> 964   Name7 5.175537e-01
#> 8     Name8 2.331493e-01
#> 95    Name8 3.309616e-01
#> 182   Name8 5.721188e-01
#> 269   Name8 5.770252e-01
#> 356   Name8 4.404109e-01
#> 443   Name8 7.156175e-01
#> 530   Name8 4.182836e-01
#> 617   Name8 5.414753e-01
#> 704   Name8 5.216959e-01
#> 791   Name8 7.068611e-01
#> 878   Name8 3.350511e-01
#> 965   Name8 4.170534e-01
#> 9     Name9 6.851374e-01
#> 96    Name9 5.360740e-01
#> 183   Name9 9.975251e-01
#> 270   Name9 7.584329e-01
#> 357   Name9 1.268428e-01
#> 444   Name9 9.640549e-01
#> 531   Name9 9.471353e-01
#> 618   Name9 5.612151e-01
#> 705   Name9 9.001855e-01
#> 792   Name9 9.447749e-01
#> 879   Name9 8.363992e-01
#> 966   Name9 8.243104e-01
#> 10   Name10 7.763435e-01
#> 97   Name10 9.070740e-01
#> 184  Name10 4.145050e-01
#> 271  Name10 9.382179e-01
#> 358  Name10 4.985626e-01
#> 445  Name10 2.076228e-01
#> 532  Name10 3.329333e-01
#> 619  Name10 2.779036e-01
#> 706  Name10 4.817758e-02
#> 793  Name10 9.687587e-01
#> 880  Name10 2.562227e-01
#> 967  Name10 2.570376e-01
#> 11   Name11 9.118810e-01
#> 98   Name11 2.730810e-01
#> 185  Name11 8.106561e-02
#> 272  Name11 4.455183e-01
#> 359  Name11 4.976100e-01
#> 446  Name11 8.723689e-01
#> 533  Name11 2.148719e-01
#> 620  Name11 9.317240e-01
#> 707  Name11 5.467247e-01
#> 794  Name11 7.658968e-01
#> 881  Name11 1.716942e-02
#> 968  Name11 7.674486e-01
#> 12   Name12 3.024189e-01
#> 99   Name12 3.765192e-01
#> 186  Name12 8.825763e-01
#> 273  Name12 4.756373e-01
#> 360  Name12 2.877821e-01
#> 447  Name12 1.922460e-01
#> 534  Name12 9.172091e-01
#> 621  Name12 7.716123e-01
#> 708  Name12 4.821892e-01
#> 795  Name12 1.592853e-01
#> 882  Name12 7.684508e-01
#> 969  Name12 7.102600e-01
#> 13   Name13 1.321232e-01
#> 100  Name13 2.010518e-01
#> 187  Name13 3.728789e-01
#> 274  Name13 3.621436e-01
#> 361  Name13 8.102981e-01
#> 448  Name13 5.125705e-02
#> 535  Name13 7.476368e-01
#> 622  Name13 5.447666e-01
#> 709  Name13 9.280155e-01
#> 796  Name13 7.221189e-01
#> 883  Name13 5.128152e-01
#> 970  Name13 1.051755e-01
#> 14   Name14 8.609264e-01
#> 101  Name14 1.926538e-01
#> 188  Name14 4.579446e-01
#> 275  Name14 7.701247e-01
#> 362  Name14 9.721063e-01
#> 449  Name14 7.200982e-01
#> 536  Name14 2.648320e-01
#> 623  Name14 2.628744e-01
#> 710  Name14 7.331427e-01
#> 797  Name14 5.759444e-01
#> 884  Name14 8.684383e-01
#> 971  Name14 4.480472e-01
#> 15   Name15 6.021514e-01
#> 102  Name15 8.681113e-01
#> 189  Name15 2.981368e-01
#> 276  Name15 5.467562e-01
#> 363  Name15 4.559119e-01
#> 450  Name15 7.069873e-01
#> 537  Name15 3.087303e-01
#> 624  Name15 1.464224e-01
#> 711  Name15 3.651410e-01
#> 798  Name15 8.863425e-01
#> 885  Name15 5.381808e-01
#> 972  Name15 2.372111e-01
#> 16   Name16 4.144494e-01
#> 103  Name16 1.602388e-01
#> 190  Name16 4.672472e-01
#> 277  Name16 2.816833e-01
#> 364  Name16 2.352374e-01
#> 451  Name16 4.661067e-01
#> 538  Name16 2.160984e-01
#> 625  Name16 7.831634e-01
#> 712  Name16 7.545236e-01
#> 799  Name16 8.047031e-01
#> 886  Name16 6.481207e-01
#> 973  Name16 3.839450e-01
#> 17   Name17 2.892806e-01
#> 104  Name17 2.883199e-01
#> 191  Name17 5.207887e-01
#> 278  Name17 9.798556e-02
#> 365  Name17 5.507378e-01
#> 452  Name17 6.463473e-01
#> 539  Name17 4.036830e-01
#> 626  Name17 9.142881e-01
#> 713  Name17 1.072295e-01
#> 800  Name17 1.859526e-01
#> 887  Name17 1.441799e-02
#> 974  Name17 8.361656e-01
#> 18   Name18 5.554867e-01
#> 105  Name18 8.138222e-01
#> 192  Name18 7.922891e-02
#> 279  Name18 5.890455e-01
#> 366  Name18 9.743380e-01
#> 453  Name18 9.578815e-01
#> 540  Name18 1.108237e-02
#> 627  Name18 1.159907e-01
#> 714  Name18 6.517556e-01
#> 801  Name18 2.788639e-02
#> 888  Name18 4.473165e-01
#> 975  Name18 4.117293e-01
#> 19   Name19 3.502466e-01
#> 106  Name19 1.877713e-02
#> 193  Name19 2.849907e-01
#> 280  Name19 6.290562e-01
#> 367  Name19 2.657069e-01
#> 454  Name19 5.852346e-01
#> 541  Name19 2.652137e-01
#> 628  Name19 7.264965e-01
#> 715  Name19 4.858167e-01
#> 802  Name19 9.396374e-02
#> 889  Name19 8.324111e-01
#> 976  Name19 2.232743e-01
#> 20   Name20 9.637388e-01
#> 107  Name20 9.724821e-01
#> 194  Name20 3.021156e-01
#> 281  Name20 7.370453e-01
#> 368  Name20 6.006101e-01
#> 455  Name20 3.364321e-01
#> 542  Name20 7.470931e-01
#> 629  Name20 5.756818e-01
#> 716  Name20 1.144363e-02
#> 803  Name20 6.223516e-01
#> 890  Name20 5.505761e-01
#> 977  Name20 1.757959e-01
#> ...etc

Answer: Here is a method of turning the first column into factors in the desired order and then using the order() function to sort.
Here is your example, but using a small sample for demonstration purposes.
# Creating a sequence of 87 unique names
unique_names <- paste0("Name", 1:5)

# Repeat the unique names to get 1044 rows
repeated_names <- rep(unique_names, length.out = 20)

# Generating random numeric values for the second column
random_values <- runif(20) # Change parameters according to your requirements

# Creating the dataframe
data <- data.frame(id = repeated_names, values = random_values)

#convert id in factors in the original order of the first rows
data$id <- factor(data$id, levels=data$id[1:5])

answer <-data[order(data$id),]
answer

      id     values
1  Name1 0.91407438
6  Name1 0.88835351
11 Name1 0.66129609
16 Name1 0.31937027
2  Name2 0.87281847
7  Name2 0.01204243
12 Name2 0.29501657
17 Name2 0.66134983
3  Name3 0.46970367
8  Name3 0.62974563
13 Name3 0.29550490
18 Name3 0.90043110
4  Name4 0.62597839
9  Name4 0.64886806
14 Name4 0.61533939
19 Name4 0.52197769
5  Name5 0.03909647
10 Name5 0.03930761
15 Name5 0.58545074
20 Name5 0.37939068

Answer: Probably you can try order like this
idx <- order(as.integer(gsub("\\D+", "", data$id)))
out <- transform(
    data[idx, ],
    newId = paste0("Name", idx)
)

and you will see the first 24 rows as below for example
> head(out, 24)
       id     values   newId
1   Name1 0.89669720   Name1
88  Name1 0.71112122  Name88
175 Name1 0.39135928 Name175
262 Name1 0.52398608 Name262
349 Name1 0.17180008 Name349
436 Name1 0.10505014 Name436
523 Name1 0.70897520 Name523
610 Name1 0.95095101 Name610
697 Name1 0.55931027 Name697
784 Name1 0.73093288 Name784
871 Name1 0.07676579 Name871
958 Name1 0.88375226 Name958
2   Name2 0.26550866   Name2
89  Name2 0.12169192  Name89
176 Name2 0.38049389 Name176
263 Name2 0.31714467 Name263
350 Name2 0.36918946 Name350
437 Name2 0.80168771 Name437
524 Name2 0.34855035 Name524
611 Name2 0.10664832 Name611
698 Name2 0.78909447 Name698
785 Name2 0.47248792 Name785
872 Name2 0.42144608 Name872
959 Name2 0.18768935 Name959

Comment: "Name" is just for the rep. example the actual values are like Paul,Bob Jack etc


find out the lcm of 2 numbers using Python

Question: <code>def calculate_lcm(x, y):
    if x > y:
        greater = x
    else:
        greater = y
    while (True):
        if ((greater == x == 0) and (greater == y == 0)):
            lcm = greater
            break
        greater += 1

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
# printing the result for the users
print("The L.C.M. of", num1, "and", num2, "is", calculate_lcm(num1, num2))
</code>
I am getting error please help me.
Comment: Not related to the question but in many cases, finding HCF and using the `a*b=LCM*HCF` formula would be better. This is because of the LCM is much larger than the numbers, It will take a lot of time. Also, Try this out [Built-in module to calculate the least common multiple](https://stackoverflow.com/questions/51716916/built-in-module-to-calculate-the-least-common-multiple)
Comment: I think this question should be closed as a duplicate.
Comment: What is the error?
Comment: he have used == sign where he have to use % sign according to me
Answer: <code># defining a function to calculate LCM
def calculate_lcm(x, y):
    # selecting the greater number
    if x > y:
        greater = x
    else:
        greater = y
    while(True):
        if((greater % x == 0) and (greater % y == 0)):
            lcm = greater
            break
        greater += 1
    return lcm

# taking input from users
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
# printing the result for the users
print("The L.C.M. of", num1,"and", num2,"is", calculate_lcm(num1, num2))
</code>
Not sure but it might help and this question is repeated many times.
Comment: as I was able to find this code easily and checked it before posting in on stackoverflow
Answer: Please Try My Code and If Get Any Error Then Comment
<code># defining a function to calculate LCM
def calculate_lcm(x, y):
    l = [x,y]
    lesser = min(l)
    greater = max(l)
    if greater % lesser ==0:
        return(greater)
    else:
        i = 1
        while True:
            if (greater*i)%lesser == 0:
                return(greater*i)
            elif i > 100:
                return(greater*lesser)
            i+=1

# taking input from users
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
# printing the result for the users
print("The L.C.M. of", num1,"and", num2,"is", calculate_lcm(num1, num2))
</code>
Comment: your code also worked
Answer: Here is another approach using GCD by implementing Euclidean algorithm.
<code># Function for calculating GCD
def calc_gcd(x, y):

   while(y):
       x, y = y, x % y
   return x

# Function for calculating LCM
def calc_lcm(x, y):
   lcm = (x*y)//calc_gcd(x,y)
   return lcm

# taking input from users
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
# printing the result for the users
print("The L.C.M. of", num1,"and", num2,"is", calc_lcm(num1, num2))
</code>


Using the Chain Rule

Question: Find $dy/dx$. $y=x/\sqrt{x^2+1}$. I started with the quotient rule and got $y'=(x^2+1)^{1/2}-x^2(x^2+1)^{1/2 }$all over $((x^2+1)^{1/2})^2$. I can not find my mistake.
Comment: Check your computation of the derivative of $(x^2 + 1)^{1/2}$.
Answer: Quotient rule:  $\left(\dfrac{f}{g}\right)'=\dfrac{f'g-fg'}{g^2}$.
In this case: $f=x$, $f'=1$, $g=(x^2+1)^{1/2}$.  Use the chain rule to find $g'$, which is $g'=\frac12(x^2+1)^{-1/2}(x^2+1)'=\frac12(x^2+1)^{-1/2}(2x).$  Now put this all together to determine
$\left(\dfrac{f}{g}\right)'.$
Answer: Another way:
Product rule
\begin{align*}
y' = [x(x^2+1)^\frac{-1}{2}]' &= x\frac{-1}{2}(x^2+1)^\frac{-3}{2}(2x) + 1(x^2+1)^\frac{-1}{2}\\
&= -x^2(x^2+1)^\frac{-3}{2} + (x^2+1)^\frac{-1}{2}\\
&= \frac{-x^2}{(x^2+1)^{\frac{3}{2}}}+ \frac{1}{(x^2+1)^\frac{1}{2}}\\
&= \frac{-x^2}{(x^2+1)^{\frac{3}{2}}}+ \frac{1}{(x^2+1)^\frac{1}{2}}\frac{x^2+1}{x^2+1}\\
&= \frac{1}{(x^2+1)^\frac{3}{2}}
\end{align*}

Using the Chain Rule


Getting value and index of array matlab

Question: I have an array in Matlab, let say of (256, 256). Now i need to build a new array of dimensions (3, 256*256) containing in each row the value, and the index of the value in the original array. I.e:
test = [1,2,3;4,5,6;7,8,9]

test =

 1     2     3
 4     5     6
 7     8     9

I need as result:
[1, 1, 1; 2, 1, 2; 3, 1, 3; 4, 2, 1; 5, 2, 2; and so on]

Any ideas?
Thanks in advance!
Answer: What you want is the output of meshgrid
[C,B]=meshgrid(1:size(test,1),1:size(test,2))
M=test;
M(:,:,2)=B;
M(:,:,3)=C;

Comment: Sorry, I am new at matlab and I think I messed up with the question. Let me rephrase, I have an image in a matrix of 256x256 and need to get a "list" of the images coordinates along with the value in the coordinate. So, if position 1,1 of the matrix contains the value 145, then first row of the resulting array will be [145, 1, 1] and so on.
Answer: here's what i came up with
test = [1,2,3;4,5,6;7,8,9]; % orig matrix

[m, n] = size(test); % example 1, breaks with value zero elems
o = find(test);
test1 = [o, reshape(test, m*n, 1), o]

Elapsed time is 0.004104 seconds.
% one liner from above
% (depending on data size might want to avoid dual find calls)
test2=[ find(test) reshape(test, size(test,1)*size(test,2), 1 ) find(test)]

Elapsed time is 0.008121 seconds.
[r, c, v] = find(test); % just another way to write above, still breaks on zeros
test3 = [r, v, c]

Elapsed time is 0.009516 seconds.
[i, j] =ind2sub([m n],[1:m*n]); % use ind2sub to build tables of indicies
                                % and reshape to build col vector
test4 = [i', reshape(test, m*n, 1), j']

Elapsed time is 0.011579 seconds.
test0 = [1,2,3;0,5,6;0,8,9]; % testing find with zeros.....breaks
% test5=[ find(test0) reshape(test0, size(test0,1)*size(test0,2), 1 ) find(test0)] % error in horzcat

[i, j] =ind2sub([m n],[1:m*n]); % testing ind2sub with zeros.... winner
test6 = [i', reshape(test0, m*n, 1), j']

Elapsed time is 0.014166 seconds.
Using meshgrid from above:
Elapsed time is 0.048007 seconds.
Comment: So, if you dont have zeros, I'd recommend using find. Otherwise ind2sub? Keep in mind this for a realllly small dataset. You should `tic toc` to find out how it scales with your data.

Getting value and index of array matlab


Converting xml to json using jackson

Question: I want to convert an xml to json.
The format of of xml is as follows -
 <default>
      <column>
        <title>Title 1</title>
    <id>id1</id>
    <value>val1</value>
  </column>
  <column>
    <title>Title 2</title>
    <id>id2</id>
    <value>val2</value>
  </column>
  <column>
    <title>Title 3</title>
    <id>id3</id>
    <value>val3</value>
  </column>
  </default>

And after the conversion i am expecting following json -
{
    "column": [
        {
            "title": "Title 1",
            "id": "id1",
            "value": "val1"
        },
        {
            "title": "Title 2",
            "id": "id2",
            "value": "val2"
        },
        {
            "title": "Title 3",
            "id": "id3",
            "value": "val3"
        }
    ]
}

But when i use jackson for this purpose it gives me following json -
{
    "column": {
        "title": "Title 3",
        "id": "id3",
        "value": "val3"
    }
}

I have tried using jackson 1.9 and jackson 2.1, but it didnt gave me the expected output.
Can some one please let me know that whether it is possible or i need to change my xml format ?
Following is the code that i have written to acheive the above scenario -
    try {
            XmlMapper xmlMapper = new XmlMapper();
            Map entries = xmlMapper.readValue(new File("xmlPath"), Map.class);

            ObjectMapper jsonMapper = new ObjectMapper();
            String json = jsonMapper.writeValueAsString(entries);
            System.out.println(json);

        } catch (Exception e) {
            e.printStackTrace();
        }

Thanks
Comment: It looks like just the last element is being converted. You should add an [SSCCE](http://sscce.org/) to your question, illustrating how you are performing this conversion.
Comment: What _exactly_ are you doing? Your description only talks about expected input, output, but not what you are doing to (try to) get that.
Answer: I was able to get the solution to this problem by using org.json API to convert source XML to JSONObject and then to JSON by Jackson API.
Code
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import org.json.XML;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

...
...

try (InputStream inputStream = new FileInputStream(new File(
                "source.xml"))) {
    String xml = IOUtils.toString(inputStream);
    JSONObject jObject = XML.toJSONObject(xml);
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    Object json = mapper.readValue(jObject.toString(), Object.class);
    String output = mapper.writeValueAsString(json);
    System.out.println(output);
}

...
...

Answer: While working on a group project I ran into the same issue you have described. The solution we used to get this to work was to change our Maven dependency from Jackson to json-lib. We used this site as a guide: http://answers.oreilly.com/topic/278-how-to-convert-xml-to-json-in-java/
<dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
    <type>jar</type>
    <classifier>jdk15</classifier>
    <scope>compile</scope>
</dependency>

--$pli7
Comment: link is broken .
Answer: I also ran into this issue, using Jackson 2.5. My solution was to replace the implementation of javax.json.JsonObjectBuilder in use with my own, which when a new object is added behaves like this :
public class CustomJsonObjectBuilder implements JsonObjectBuilder {

  private final Map<String, JsonValue> jsonFragments;

  // other methods omitted for brevity

   public JsonObjectBuilder add(String name, JsonValue value) {
     if(jsonFragments.containsKey(name)) {
        JsonValue oldValue = jsonFragments.get(name);
        JsonArrayBuilder newBuilder = new JsonArrayBuilderImpl();
        if(oldValue instanceof JsonArray) {
            JsonArray theArray = (JsonArray) oldValue;
            for (JsonObject oldValues : theArray.getValuesAs(JsonObject.class)) {
                newBuilder.add(oldValues);
            }
        } else {
            newBuilder.add(oldValue);
        }
        newBuilder.add(value);
        jsonFragments.put(name, newBuilder.build());
     } else {
        jsonFragments.put(name, value);
     }
     return this;
   }

Converting xml to json using jackson


Phillips-Perron Unit Root Test with exogenous breaks

Question: I would need to do a Philips-Perron test in R, while controlling for an exogenous break.
I found the Philips-Perron test in the package tseries (PP.test), but can't find (contrary to the CADF test) how to extend this test to control for an exogenous break. I'm not a programming expert, is there a way to easily include this in this test in R?
I had a look here and here, but this only offers alternative approaches to my problem, where I would need a perron test for my article..
Comment: Could you give a reference for the PP test with an exogenous break?
Comment: Equation 4 on p.6

http://ro.uow.edu.au/cgi/viewcontent.cgi?article=1488&context=commpapers
Answer: I think what your a looking for is a test like performed in Perron, P. (1989), "The great crash, the oil price shock, and the unit root hypothesis",
Econometrica , 57, pp.1361-1401 allowing for one structural break. I know that it is implemented in Eviews but I do not know if there is some R package that performs this test.
José


Let $A = \{0\} \cup [1,2] \cup \{3\}$ and $ B = [0,1] \cup \{2\} \cup \{3\}$ proof there is no homeomorphism

Question:
Let $A = \{0\} \cup [1,2] \cup \{3\}$ and $ B = [0,1] \cup \{2\} \cup \{3\}$ be subspaces of $\mathbb{R}$ in the usual topology.
Prove that there does not exist a homeomorphism $h:\mathbb{R}\to\mathbb{R}$ taking $A$ to $B$.

I was thinking to use the fact that a side of the interval has 2 points and in the other has one point on both sides.  Maybe then I can use a connected components result, but I can't seem to write a proof.
Thanks for any hints.
Comment: Try proving a lemma that shows for all $x,y\in \mathbb{R}$ with $xf(y)$.  In words, it will be orientation preserving or reversing.
Comment: @Francisco: Yes.
Comment: Thank you very much @N.Owad .. using the hint you gave with  intermediate value theorem. the result follows Right ?
Comment: @BrianM.Scott thank you very much..
Answer: Here is a proof from more-or-less basic principles you might have seen.
Lemma 1: If $h:[x,y]\to[\alpha,\beta]$ is a homeomorphism and $h(x)<h(y)$, then for all $x\leq a<b\leq y$ we have $h(a)<h(b)$.
Proof of Lemma 1: Assume to the contrary that $h(b)<h(a)$.  Note that $[x,a]\sqcup[b,y]$ has exactly two components, so by a simple result on homeomorphisms we have that $h([x,a]\sqcup[b,y])=h[x,a]\sqcup h[b,y]$ has exactly two components, namely $h[x,a]$ and $h[b,a]$.  Now
\begin{align*}
[\min\{h(x),h(a)\},\max\{h(x),h(a)\}]&\subseteq h[x,a]&\text{and}\\
[\min\{h(b),h(y)\},\max\{h(b),h(y)\}]&\subseteq h[b,y]
\end{align*}
By our note on the components of $h([x,a]\sqcup[b,y])$ we have that either $\max\{h(b),h(y)\}<\min\{h(x),h(a)\}$ or $\max\{h(x),h(a)\}<\min\{h(b),h(y)\}$.  In the former case
$$h(y)\leq\max\{h(b),h(y)\}<\min\{h(x),h(a)\}\leq h(x)<h(y)$$
which is absurd.  In the latter case we have a similar absurdity:
$$h(a)\leq\max\{h(x),h(a)\}<\min\{h(b),h(y)\}\leq h(b)<h(a)$$
In particular $h(a)\leq h(b)$.  As $h$ is injective we have $h(a)<h(b)$, completing the proof.
Analogously if $h(y)<h(x)$, then for all $x\leq a<b\leq y$ we have $h(b)<h(a)$.
Lemma 2: If $h:[x,y]\to[\alpha,\beta]$ is a homeomorphism and $h(x)\leq h(y)$, then $h(x)=\alpha$ and $h(y)=\beta$.
Proof of Lemma 2: By Lemma 1 we have that $\alpha\leq h(x)\leq h(a)$ for all $x\leq a\leq y$.  As $\alpha=h(c)$ for some $x\leq c\leq y$ by surjectivity, we must have $\alpha\leq h(x)\leq h(c)=\alpha$, so $h(x)=\alpha$.  A similar argument yields $h(y)=\beta$, which concludes the proof.
Analogously if $h(y)\leq h(x)$, then $h(y)=\alpha$ and $h(x)=\beta$.
Proof of Result: Let $h:\mathbb{R}\to\mathbb{R}$ be a homeomorphism.  By a simple result on homeomorphisms we have that $h|_{[0,3]}:[0,3]\to h[0,3]$ is a homeomorphism.  Assume to the contrary that $h(A)=B$. Thus we have $[0,3]\subseteq h[0,3]$, and necessarily $h[1,2]=[0,1]$ and $h(0),h(3)\in\{2,3\}$ as $h$ is bijective and takes path components to path components.  By Lemma 1 either $h(0)<h(1)<h(2)$ or $h(3)<h(2)<h(1)$.  In the first case $h(1)=0$ and $h(2)=1$ by Lemma 2, so either $2=h(0)<h(1)=0$ or $3=h(0)<h(1)=0$; as both of these are absurd, the first case is impossible.  In the second case $h(1)=1$ and $h(2)=0$ by Lemma 2, so either $2=h(3)<h(2)=0$ or $3=h(3)<h(2)=0$; as both of these are absurd, the second case is impossible.  As both cases lead to an absurdity we have that $h(A)\neq B$ by necessity, completing the proof that no such homeomorphism can exist.
Comment: Holy crap. I have to prove this theorem too,is there any other way to do it. This example is due to Kuratowski and shown up in Dugungji's book. I don't have that theorem in my book.

I do have Let X. & Y be topological spaces and let h:X$\mapsto$Y be a Homeomorphism of X onto Y. Then for each x$\in$X h($C_x$)=$C_{h(x)}$ and for each y$\in$ Y, $h^{-1}(C_y)$=$C_{h^{-1}(y)}$
Comment: @Eudoxus what does $C_x$ mean in the context of the theorem you cited?  Is it the path component of $x$?
Comment: $C_x$=$\bigcup ${K $\subset$ X ,K connected ,x$ \in $ K }
Comment: @Eudoxus I added an answer which is much simpler technically, but also needs the Intermediate Value Theorem.
Comment: It much nicer thanks @User12345
Answer: In a comment on my other answer, I was asked to give a simpler proof using the following lemma.
Lemma A: Every homeomorphism carries path components to path components.
My proof below will also make use of the Intermediate Value Theorem.
Proof of result: Let $X = \{0\} \cup [1, 2] \cup \{3\}$ and $Y = [0, 1] \cup \{2\} \cup \{3\}$.  Assume to the contrary that there is a homeomorphism $f \colon \mathbb{R} \to \mathbb{R}$ such that $f(X) = Y$.  Now $f^{-1}$ is also a homeomorphism, and restricts to homeomorphisms on the path components of $Y$ by Lemma A and the fact that homeomorphisms restrict to homeomorphisms.  In particular, we have $f^{-1}([0, 1]) = [1, 2]$ as these are the only infinite path components of $Y$ and $X$ respectively.  Now $f^{-1}\{2, 3\} = \{0, 3\}$, so by the Intermediate Value Theorem there is a $2 \leq x \leq 3$ such that $f^{-1}(x) = 1$ because $0 \leq 1 \leq 3$.  On the other hand, there is an $x' \in [0, 1]$ with $f^{-1}(x') = 1$ by our previous remark.  Hence $f^{-1}(x) = f^{-1}(x')$ for $x \geq 2 > 1 \geq x'$, so $f^{-1}$ is not injective.  But this implies $f^{-1}$ is not a homeomorphism, which is absurd.  Hence there is no such homeomorphism $f$.
Answer: If exist a homeomorphism f from $\Bbb{R} to \Bbb{R}$ such that $f(A)=B$ then $A\cong B$ now it is not true because $(A-\{2\})\not\cong (B-\{2\})$ because $A-\{2\}$ has three components connect and $B-\{2\}$ has two components connect and it is contradiction.
Comment: There is no reason the homeomorphism must map 2 to 2.  What you have written here is false.  Maybe you meant something else?
Comment: 2 is in A and 2 is in B ,we say ``taking A to B``
Comment: What is BbbRtoBbbR


How to store api request count to a PHP script for fast reply to the client?

Question: I recently asked here : How to know which website ask for an external image served by php?
A third party website request an image like:

I need to limit the access for a specific api key on a hourly and daily basis.
Now I stored some data on a mysql DB:

id, apikey, limit_ip, limit_referer, limit_hour, limit_day
1, JFKHJSDLGHUFIE, 127.0.0.1, *,     250,    10000

<?php

//Verify Referer
if(
    !isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == "" ||
    !isset($_GET['apik']) || $_GET['apik'] == "" ||
    !isset($_SERVER['REMOTE_ADDR']) || $_SERVER['REMOTE_ADDR'] == "") {
        exit("API error, your referer informations aren't set");
} else {
    //Site referer
    $site = $_SERVER['HTTP_REFERER'];
    //Ip referer
    $ip = $_SERVER['REMOTE_ADDR'];
    //Get api key
    $apik = htmlentities($_GET['apik']);

    try
    {
        $options = array(
            PDO::MYSQL_ATTR_INIT_COMMAND    => "SET NAMES utf8"
        );
        //MySQL connection
        $bdd = new PDO('mysql:host=localhost;dbname=apitest', 'root', '', $options);
    }
    catch(Exception $e)
    {
        //If error, die
        die('Error: '.$e->getMessage());
    }

    //Lookup for the apikey in the database
    $answer = $bdd->query('SELECT * FROM apiCode WHERE apikey = "'.$apik.'" LIMIT 0,1');
    //Fetch settings for this api key
    while ($data = $answer->fetch()) {
        $limit_ip = $data['limit_ip'];
        $limit_referer = $data['limit_referer'];
        $limit_hour = $data['limit_hour'];
        $limit_day = $data['limit_day'];
    }

    //Verify API limits
    //Cookie, session, database?

    //Return content
    echo "Api : Success!";

}

I think the best way is to create a table in the database like:
id, access_hour, access_day
Reset the access_hour field each hour with cron job, etc. but it will take much more time to serve the content because MySQL is so much sloooooww and it's better if it'S quick :).
Cookies will be stored on the remote computer so it is not reliable.
Session's duration are 15 minutes... so it's too short to store and limit access.
All statements from the above are from my point of view only.
My question is: how to store and limit the number of access for a specific API key hourly and daily without* affecting the serving speed?

without: it just need to be fast and reliable.
Answer: I wouldn't put this data in mysql.  You don't need durability, or persistance for this kind of information.  It would be much better to use memcached, and use an algorithm like the following:
http://en.wikipedia.org/wiki/Leaky_bucket
Comment: Thanks, I'll take a look at this. I just found a PHP implementation on Github :)


Angular2 Universal Server side rendering NgbTypeaheadWindow Did you add it to @NgModule.entryComponents

Question: i'm moving my project to angular universal due an SEO improvments.
i followed this guide:
(im using angular version 5)
 https://github.com/angular/angular-cli/wiki/stories-universal-rendering
and finally, after rendering the server and the client side,
when i try hit node dist/server.js
its failed , and pop me up the msg below:
image here click
i tried almsot everything, move the NgbTypeaheadWindow to a SharedModule.
first tried:
adding to app.module.ts like the error suggested
import { NgbTypeaheadWindow } from '@ng-bootstrap/ng-bootstrap/typeahead/typeahead-window';

@NgModule({
    declarations: [
        AppComponent,
    ],
    imports: [
         /** others modules/**
        SharedModule,
        NgbTypeaheadWindow
    ],

    entryComponents: [NgbTypeaheadWindow]

but still not working.
getting this error:

ERROR in : Unexpected directive 'NgbTypeaheadWindow in
  C:/Users/benn/Documents/dev/angular_universal_without_e2e/client/node_modules/@ng-bootstrap/ng-bootstrap/typeahead/typeahead-window.d.ts'
  imported by the module 'AppModule in
  C:/Users/benn/Documents/dev/angular_universal_without_e2e/client/src/app/app.module.ts'.
  Please add a @NgModule annotation.

So, i tried add it also to declarations and then export it like this post suggest:
Angular 4: no component factory found,did you add it to @NgModule.entryComponents?:
@NgModule({
    declarations: [
        AppComponent,
        NgbTypeaheadWindow
    ],
    imports: [
        SharedModule,
    ]

    entryComponents: [NgbTypeaheadWindow],
    exports:[NgbTypeaheadWindow]

and get this error:

ERROR in : Type NgbTypeaheadWindow in C:/Users/benn/Documents/dev/angular_universal_without_e2e/client/node_modules/@ng-bootstrap/ng-bootstrap/typeahead/typeahead-window.d.ts is part of the declarations of
  2 modules: NgbTypeaheadModule in C:/Users/benn/Documents/dev/angular_universal_without_e2e/client/node_modules/@ng-bootstrap/ng-bootstrap/typeahead/typeahead.module.d.ts and AppModule in C:/Users/benn/Documents/dev/angular_universal_without_e2e/client/src/app/app.module.ts! Please consider moving NgbTypeaheadWindow in C:/Users/benn/Documents/dev/angular_universal_without_e2e/client/node_modules/@ng-bootstrap/ng-bootstrap/typeahead/typeahead-window.d.ts to a higher module that imports NgbTypeaheadModule in C:/Users/benn/Documents/dev/angular_universal_without_e2e/client/node_modules/@ng-bootstrap/ng-bootstrap/typeahead/typeahead.module.d.ts and AppModule in C:/Users/benn/Documents/dev/angular_universal_without_e2e/client/src/app/app.module.ts. You can also create a new NgModule that exports and includes NgbTypeaheadWindow in C:/Users/benn/Documents/dev/angular_universal_without_e2e/client/node_modules/@ng-bootstrap/ng-bootstrap/typeahead/typeahead-window.d.ts then import that NgModule in NgbTypeaheadModule in C:/Users/benn/Documents/dev/angular_universal_without_e2e/client/node_modules/@ng-bootstrap/ng-bootstrap/typeahead/typeahead.module.d.ts and AppModule in C:/Users/benn/Documents/dev/angular_universal_without_e2e/client/src/app/app.module.ts.

after that, i tried to move it to my SharedModule but still.
here is my SharedModule :
import { NgbTypeaheadWindow } from '@ng-bootstrap/ng-bootstrap/typeahead/typeahead-window';

        @NgModule({
      imports: [CommonModule,NgbTypeaheadWindow],
      declarations: [CaruselComponent],
      exports: [CommonModule, FormsModule, CaruselComponent,NgbTypeaheadWindow],

    })
    export class SharedModule { }

ERROR in : Unexpected directive 'NgbTypeaheadWindow in
  C:/Users/benn/Documents/dev/angular_universal_without_e2e/client/node_modules/@ng-bootstrap/ng-bootstrap/typeahead/typeahead-window.d.ts'
  imported by the module 'SharedModule in
  C:/Users/benn/Documents/dev/angular_universal_without_e2e/client/src/app/shared/shared.module.ts'.
  Please add a @NgModule annotation.

i tried it to app.server.module.ts but still.
Hope you can help
thanks
Comment: why are you trying to import it at the first place? its a directive and it comes as a part of the typeahead module.
Comment: which  version of ng boostrap are you using?
Answer: Import the NgbTypeaheadModule instead of the directives/components related to it.
import { NgbTypeaheadModule } from '@ng-bootstrap/ng-bootstrap/typeahead';
@NgModule({
    declarations: [
        AppComponent,
    ],
    imports: [
         /** others modules/**
        SharedModule,
        NgbTypeaheadModule
    ],
    entryComponents: []
})

Comment: added to app.module.ts , import NgbTypeaheadModule from '@ng-bootstrap/ng-bootstrap' , and added to imports: [    NgbTypeaheadModule,..] still got the same Error: No component factory found for NgbTypeaheadWindow. Did you add it to @NgModule.entryComponents?
Comment: dont add it to your imports, if its in your app.module.ts
Answer: I fixed it by not choose to render it on serverside with IsPlatFormServer and isPlatFormBrowser


Is $\operatorname{Iso}(E)= E \setminus\operatorname{Der}(E)$ or $\operatorname{Iso}(E)=\operatorname{Cl}(E) \setminus\operatorname{Der}(E)$?

Question: In my lecture notes I have the following property:
$\operatorname{Iso}(E)= E \setminus\operatorname{Der}(E)$ ...(1)
but then I saw in other section, they used
$\operatorname{Iso}(E)=\operatorname{Cl}(E) \setminus\operatorname{Der}(E)$ ...(2) (Cl=closure)
Besides I know, $\operatorname{Cl}(E)=$ disjoint union of $\operatorname{Iso}(E)$ and $\operatorname{Der}(E) $.
So from this, I would say (2) should be the correct one.
Which one is correct and why?
Answer: Both are correct.
Importantly $E \subset \operatorname{Cl}(E)$ and $\operatorname{Cl}(E) \setminus E \subset \operatorname{Der}(E)$, so any point in $\operatorname{Cl}(E)$ that wasn't already in $E$ itself is removed when we remove $\operatorname{Der}(E)$.  The result is the same either way, whether we start with $E$ or $\operatorname{Cl}(E)$.

As a simple example that encompasses the entire situation, just note that $$\{1,2\} \setminus \{2,3\} = \{1,2,3\} \setminus \{2,3\}$$
Comment: How is {1,2,3 } the closure of {1,2}?
Comment: @J.C.VegaO Topology doesn't actually matter for the example, only the basic set operations.  If you want it to be topology more explicitly, we would need to define a topological space. To that end, consider the topology $\tau = \{\emptyset, \{1\}, \{1,2,3\}\}$ on the set $\{1,2,3\}$.  If you set $E = \{1,2\}$, then $\operatorname{Der}(E) = \{2,3\}$ and $\operatorname{Cl}(E) = \{1,2,3\}$
Comment: jeez, you are right, I was looking at {2,3} when I thought that, my bad


Xvfb on debian not installing properly from apt-get

Question: I try to install xfvb for headless firefox and selenium tests, but after installing by:
apt-get install xvfb
I got only those files on server (search by xvfb):
/usr/bin/xvfb-run
/usr/share/doc/xvfb
/usr/share/man/man1/xvfb-run.1.gz
/var/lib/dpkg/info/xvfb.list
/var/lib/dpkg/info/xvfb.md5sums
/var/cache/apt/archives/xvfb_2%3a1.7.7-14_i386.deb

As you can see, no xvfb executable in /usr/bin/
Using Debian 2.6.32-042stab055.12

Comment: `dpkg -L xvfb` would give you the full list of files provided by the package. You would probably come to DerfK's conclusion then.
Answer: The actual executable name is /usr/bin/Xvfb with the capital X.
Comment: The command worked for years with a lower case X and suddenly stopped working. Thanks for saving me a few hair-pulling hours debugging to find out what's wrong ;-)
Answer: If you are using Debian stable and apt-get reports a successful install then you can be almost 99.99% sure it was successful. In other words, a mistake is most likely something you forgot to check, or overlooked or another end user error. In fact i can't remember a problem ever with a Debian stable package installing incorrectly.
This is not gripe about the user, but a compliment towards Debian's excellent packaging system and stability.


How to enable TLS with port 587 with Secure Authentication on Postfix?

Question: How to make my Postfix server send mail only on port 587, and also enable TLS with port 587 with Secure authentication (which uses system linux users)?
First of all, this question might seem too broad, but I couldn't find a single solution for this on the internet. Many articles do give a solution, but they skip some parts.
I want to force authentication and force TLS connections.
I can post the config if anyone asks me.
Thanks in advance. I am new to postfix. Also, I could take a look at http://www.postfix.org/documentation.html, but before you tell me about it, I am unable to understand it properly, hence, I am sure I would mess up a config, and make my server unusable. Also, postfix here was installed using the apt package manager. But I'm sure this is not particularly related to ubuntu. I was using Exim4 before using postfix, but there is so little support for Exim, because people mostly have it in cPanel, so I don't get vanilla Exim support.
This needs urgent help because our server's security is in danger.
Answer: To enable port 587, edit the file /etc/postfix/master.cf
vi /etc/postfix/master.cf

and remove the # in front of the line (uncomment the line):
#submission inet n - n - - smtpd

so that it looks like this:
submission inet n - n - - smtpd

You may want to uncomment additional lines enabling SASL authentication right after this line. Each new line should start with space!
 -o syslog_name=postfix/submission
 -o smtpd_tls_security_level=encrypt
 -o smtpd_sasl_auth_enable=yes
 -o smtpd_client_restrictions=permit_sasl_authenticated,reject

All the other settings to be configured in /etc/postfix/main.cf based on your environment.
Additionally you may need to configure TLS settings:
# TLS parameters
# you need to specify a real certificate location
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert.key
smtpd_use_tls=yes
smtpd_tls_received_header = yes
smtpd_tls_session_cache_timeout = 3600s
smtp_tls_session_cache_database = btree:$data_directory/smtp_tls_session_cache
smtpd_sasl_tls_security_options=noanonymous
smtpd_tls_auth_only=yes

Depending if you use dovecot to receive emails you may need to add:
smtpd_sasl_security_options = noanonymous
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/dovecot-auth
broken_sasl_auth_clients=yes

Additional security restrictions:
smtpd_relay_restrictions=permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject_non_fqdn_helo_hostname,reject_non_fqdn_sender,reject_unknown_sender_domain,reject_non_fqdn_recipient,reject_unknown_recipient_domain

check and restart postfix:
postfix check
systemctl restart postfix

You can make sure that postfix is now listening on both ports 25 and 587:
netstat -na | grep LISTEN | grep 25
netstat -na | grep LISTEN | grep 587

Don't forget to allow port 587 in your firewall.
Creation of postfix users is another story. As was mentioned in comments you should use SQL to store mailboxes (mail users). If you don't want to do so, you can use Linux users are described in details here.
P.S. I don't think it's feasible to provide the complete config here because it always very dependent on the specific case requirements and your environment.
Comment: So, uhh... If dovecot uses linux users. So, if I enable user auth in dovecot, and connect postfix to it, then will it start working, the auth? I will do those things myself unless I have a major issue.
Comment: Dovecot is used only to receive emails via IMAP or POP3, you may delegate postfix to do authentication instead of it when receiving emails. But postfix is able to do SASL auth itself without dovecot when using another protocols.
Answer: This will only allow secure connections:
smtpd_tls_auth_only = yes

Then you have the other needed options:
smtpd_tls_security_level = may

smtp_sasl_auth_enable = yes

smtp_use_tls = yes

To use 587, edit master.cf and uncomment the line:
submission inet n - n - - smtpd

The restart postfix.
Comment: Thanks for that. But Does that also enable authentication, if so, what users? I want to use system users, with their systme username and password. What about port 587 and stuff? I am a newbie. And just in case, my configuration is full default, with Internet site conf.
Comment: Mail users should not be linux users. There is no reason for that. You have MariaDB or alternates to handle e-mail client users. If you're config is default there's a lot of work on multiple aspects; you can't just make a setting and call it all good.
Comment: help me about that. Maybe tell me how to use port 587, virtual users without Database,
Comment: I have added the 587 usage option to the answer.


Highlight autocomplete suggestions Drupal 7

Question: I have a text field in a form, with autocomplete on. It works very well, but I cannot manage to find a way to highlight the letters in the suggested works. I found this hack in the autocomplete library (general, no Drupal related), but don't know if it applies or how to apply it. I would like the same effect:
http://output.jsbin.com/ezifi/4
Answer: Try Autocomplete Deluxe Module. May b this help you
Comment: Thanks a lot, it works! However, I had to do the following, hacking the code, to make it work as I wanted: 1) delete the "the @term termi will be added" because it tried to add new non-existing content. 2) hack the code of *.js so it didn't trigger when mouse down or mouse up, just when I hit some key. 3) Add t() to the not found message. What I don't understand is that such a basic feature is not incorporated into the core autocomplete.js, and also don't understand why this module Autocomplete Deluxe is abandoned.
Answer: I have found this tutorial. It is a small gem in which I have found the solution to this problem and this another one.
Basically, I use the hook callback function mymodule_formName_autocomplete ($type, $text) and there I can do drupal_json_output($output_string) where $output_string, based on the string query $text accepts any HTML label, so I can highlight at convenience by just using PHP, which is very powerful.


Unable to parse a stringified array containing HTML

Question: This may be a silly question but I invested more than half an hour to understand why it is not working. I have a 2D javascript array. Some of the elements in array are HTMl having anchor tag with href attributes.
I am trying to used JSON.parse("stringified2D array here") but it give me error as shown in this screenshot.
var cd = JSON.parse('[["header","This is some header"],["footer","
This addon is brough to you by Accemy and SW gApps
This is universal and appended to all add-on content"],["nslookup","NS Lookup allows to fetch DNS records from public DNS servers"]]');

It gives me error
Uncaught SyntaxError: Unexpected token h in JSON at position 88
    at JSON.parse (<anonymous>)
    at <anonymous>:1:15

Comment: that isn't a proper json format
Comment: this is a stringified array. And yes, an array can be be stringified and then parsed. Check here https://imgur.com/PnU1oUB
Comment: Do not generate JSON code manually. Use the language's built-in functions (e.g. [`json_encode()`](http://php.net/manual/en/function.json-encode.php) in PHP or [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) in JS) or corresponding ready-to-use libraries.
Answer: You should escape the quotes twice \\"
var s = '[' +
  '["header","This is some header"],' +
  '["footer","
This addon is brough to you by ' +
  'Accemy and ' + //here
  'SW gApps
' + //and here
  'This is universal and appended to all add-on content"],' +
  '["nslookup","NS Lookup allows to fetch DNS records from public DNS servers"]]';

var cd = JSON.parse(s);

console.log(cd.length); //3

Answer: You need another \ to escape double quotes in string:
var cd = JSON.parse('[["header","This is some header"],["footer","
This addon is brough to you by Accemy and SW gApps
This is universal and appended to all add-on content"],["nslookup","NS Lookup allows to fetch DNS records from public DNS servers"]]');


What is the origin of the up bow and down bow symbols for bowed string instruments?

Question: My brother is a beginning cellist and I was helping him annotate "up bow" and "down bow". I find it particularly strange that the "up bow" symbol is similar to a V. Intuitively, I would have thought it would be ^ or an up arrow.
I thought I would find the answer to this for a quick Google search, but haven't yet turned up any information. Is there any known historical reason for the use of these two symbols?
Comment: It's not just bowed instruments.  Similar, or the same symbols are also used for [organ pedaling](http://www.arkkra.com/doc/uguide/heeltoe.html) and [guitar picking](http://en.wikipedia.org/wiki/Strum).  (The latter isn't so much of a stretch, given that it's still a string instrument.)
Comment: Is it the word "vilaine"? I'm doing my best to find a source linking this with string bowing...
Comment: Hello @Lou. Don't know if you're still using Music.SE. I just returned to this page today - there are two answers about the terms "nobilis" and "vilis" that seem really useful. Maybe one of them might be worth accepting as an answer...
Comment: The V is for the French word "Vileine" (not sure about the spelling), because up bow naturally sounds less nice (nowadays, people tend to practice until up and down sounds equal). There is a symbol with a French origin for down bow as well.
Answer: This is one of the best questions I've seen for ages. I've looked for a while, but can't find a definitive answer, but hopefully I can give some useful information.
Two things: the up bow mark doesn't really describe an upward direction, but instead that the bow is pushed, rather than pulled as in a down bow. So, this helps to explain why this doesn't use an upward pointing arrow. The orchestration book on my desk, Walter Piston's Orchestration, points out that these signs are,

more clearly expressed by the French tire (drawn) for down-bow, and pousse (pushed) for up-bow.

I figured that trying to find out when bowing marks were first used might yield the reasoning behind the symbols used. This webpage has some detailed information about the early uses of bow markings in string music. Interestingly, it mentions that the violinist Ferdinand Franzl used bowing marks, but that they were,

inverted versions of the modern signs

At the bottom of the webpage there is a link to another webpage describing the continuing development of string bowing markings - but unfortunately the webpage linked to no longer exists, oh well!
Just one observation of my own, the upbow on string instruments is more suited to creating a crescendo (getting louder), with the downbow more easily producing the opposite; to my eyes this makes the upbow symbol "look right" (it is similar to an upward crescendo symbol). Equally, the downbow on strings is more effective at creating a strongly accented "heavy" articulation, and the downbow symbol somewhat resembles the marking for marcato.
Sorry, to not be able to give you a definitive answer - I hope this post gives you some useful information though.
EDIT: @tohuwawohu points out here that the upbow marking resembles the tip of the bow (where you start when executing an up bow), this "pointy" marking also being appropriate as the tip is also called the point of the bow (punta d'arco in Italian, pointe in French). And, the downbow marking resembles the frog (or heel) of the bow, where you start when executing a downbow.
As I say, @tohuwawohu deserves credit for this additional information.
UPDATE: the search continues, especially considering @11684's comment above (below the OP's question) - I'm trying my hardest to find a source to back that comment up.
In the meantime, I have found an example which uses t (tirer, to pull) for down-bow and p (pousser, to push) for up-bow, as mentioned above. It can be found in this book. See extract below from Principes de violon (1718) by French violinist and dancing master Pierre Dupont:
Comment: You might be able to draw the information from [the archived version](http://web.archive.org/web/20130224020703/http://chase.leeds.ac.uk/article/bowing-fingering-and-performance-instructions-in-string-music-from-the-early-19th-to-early-20th-century-the-evolution-of-annotated-string-editions-clive-brown) - hope this helps!
Comment: I'm a viola da gamba player - it's common for us to use **t** and **p** for 'down-bow' and 'up-bow', which we call 'pull' and 'push', respectively. And actually, because of the inverted bow hold for a viol, the 'strength' of a stroke is inverted, i.e. our push (equivalent to an up-bow) behaves more like a down-bow on a violin. I've always thought the modern bowing symbols were derived from the shape of the bow, but that's pure speculation on my part. Some viol players do use the modern symbols, preserving their meaning in terms of bow direction (and inverting the effect).
Comment: I can't find it online, and it was so many years ago I don't remember the book exactly, but I remember in one of my music history books a section that detailed the marks and how they were indeed pictures of the bow tip and frog.  It was a revelation for me as I had never made the connection before, mind blown..  So that theory has been around for a while at least.
Comment: Thank you for trying, it's a good start, and there's some interesting ideas here :). This is a real stab in the dark, I don't even play cello, but perhaps up-bow was inspired by the V-shape of the thumb and index finger as you push the bow up the cello. I'm interested that there's not an easily found answer to this question.
Comment: No problem. I'm sure a string player will respond with a far less conjectural answer than mine!
Comment: That sounds like a compelling theory! I'll share that with my brother if he's interested (I think this question was of more interest to me, though.)
Comment: @Bergi Thanks! I had a good look through this, but still no extra info specific to this question...
Answer: At first sight, i would suppose the symbols are inspired by the different shapes of the bow's frog and tip respectively. But that's a mere guess, without corroboration by sources.
EDIT:
Here's an illustration showing a bow's frog (above - notice the orthogonal shape) and tip (below - showing the characteristic "pointy" shape):

Image: By Henry Saint-George (1866-1917) [Public domain], via Wikimedia Commons
Comment: Might I recommend a picture to enhance this answer?
Comment: +1 I think you're probably right here. I've edited my answer to acknowledge your information and answer - I hope that's okay. If not, I'll take the information out...
Comment: @kojiro: Very good idea - fortunately, Wikimedia Commons has an illustration showing frog and tip as well.
Comment: @BobBroadley: no problem - feel free just to use the info!
Comment: That's what I always though as well, but I don't have any sources either.
Comment: Depending on when the notation symbols were first adopted, it may be relevant that the points of bows were even pointier in the Baroque era (as the bows bowed outward rather than inward, and were terminated with a "pike" or "swan-bill" shape). http://en.wikipedia.org/wiki/Baroque_violin#Bows
Comment: I was told by the concertmaster of a major orchestra that this explanation (tohuwawohu's answer, above) is spot on.
Answer: Last year I was told by a Baroque and Renaissance expert cellist/gambist that the ∏ and ⋁ signs originate from abbreviations of the latin nobilis and vilis. This blog post also says something along those lines.
Apparently, french composers liked to write n above important notes, demanding that they be played in a particularly beautiful, noble fashion. To the players, this was generally a prompt to play those notes from the frog for best control. v was the opposite, indicating that these notes didn't need to be payed to much attention to, which was then generally used as an opportunity to bring back the bow. The composers probably didn't really care about that, only about the tone, hence didn't directly write p and t which would have literally expressed pull or push. But instead of elaborating on these tonal qualities, I suspect many teachers just taught the simple rule "n means down-, v upstroke", so the original meaning got lost and the n symbol became a symbol most today wouldn't recognise as such.
Answer: n and v (standing for Latin "nobilis" and "vilis") seem to be first used in print in Georg Muffat's 1698 "Florilegium Secundum", a collection of music for string orchestra with a preface in which Muffat explains the French style of bowing (associated with Lully's orchestra) for the benefit of musicians elsewhere in Europe.
They are definitely used by Muffat as indications for the direction of the bow. The terminology of "good" and "bad" notes respectively for notes on the strong and weak parts of the beat was, however, common in the 17th century, so the two ideas are connected.
Answer: An older German encyclopedia (Brockhaus, 4th edition, 1885-1892, see "Bogenführung", attention: not only German but also black letter) gives also the upside-down variants with the inverted meaning for both bow-up and bow-down marks. I found no reference for it, but could imagine, that these were abandoned to ensure orientation-independence. Note, that quite a few ornamentation marks are turned upside down and are written to the other side of the note system depending on the direction of the note stem, like staccato-dots, marcatos, fermata etc, a practice I found also mentioned as possible (even if not recommended) for bow markings in "Behind the Bars". So having not only the direction, but also the form of the sign, might have been quite an advantage before electric note stand lamps.
And obviously any symbol is more international than terms in whatever language.
Answer: Old thread I know, but still there seems to be no answer.
I have a theory
\/ is used for Guitar also to mean strum up.
both for Violin up bow and Guitar up strum the bow or plectrum move from high strings to low strings so \/ points across the stave in the direction you arpeggiate the notes.
Admittedly this makes more sense for strumming than bowing.
Comment: Well, the ∏ and ⋁ symbols are definitely older than the guitar, so if anything this could only have originated from _lute_ notation. But I don't think it was used as such, or do you have any references for that?
Comment: Welcome to Music Stack Exchange, Stuart.  The site always welcomes cogent answers (such as yours) no matter how old the thread is.  Your answer is clear and should be helpful to new browsers even if the OP never comes back to look.  Again, welcome.
Answer: The use of n (nobilis) and v (vilis) by Muffat seems like a good theory for the shape of down and up bows. Reference "Performing Baroque Music" by Mary Cyr.
Answer: I've been teaching strings for years and always tell my students that it's a theory, but seems plausible.  My vote goes for the shape of the tip and frog ends of the bow.  Whether it was originally t and p is irrelevant since neither resembles the currently used symbols.


How to position  tag to the right inside  tag

Question: Align the  tag inside
 tag to the right.
Issue :  tag aligns immediately after the content of
 tag . I want it to be placed evenly on to right on each li
example on jsfiddle :
link to question
Comment: You mean like that? http://jsfiddle.net/8XSar/4/
Comment: So you want your anchor tag to be right justified?
Comment: Float it to the right: http://jsfiddle.net/NZg8m/
Answer: Use float:right on the anchor element.
http://jsfiddle.net/8XSar/3/
Answer: #cssmenu li a{
    /*background: #2580a2 url('hover.gif') left center no-repeat;
    color: #fff;
    cursor : pointer;
    padding-bottom: 8px;*/
    margin-right: 2px;
    padding-left: 5px;
    float:right;
}

jsfiddle: http://jsfiddle.net/8XSar/5/


Synchronize modification between SWT table and TextEditor

Question: I'm facing a problem and want to ask for a solution.
I'm working on an eclipse plugin project, in which an editor for a type of resource file is required. The resource file has similar structure like CSV file. My idea is to provide user the option to edit this type of file both in plain text format and also in an SWT table. Plain text is required for examining data and table provides more flexibility to editing such as sorting by column.
I have been able to create a MultiPageEditorPart, with one page of org.eclipse.ui.editors.text.TextEditor, and another page with a org.eclipse.swt.widgets.Table and several other widgets like search bar. The content of the resource file can be shown in the TextEditor, can also be edited and saved. On the other hand, the content can be loaded in the table too, sorting and searching all work good.
The problem is: when I edit a cell in the table, I want the change also reflected in the TextEditor, and vice versa. Since the resource file can be very large, I want the saving action happen only on the TextEditor, i.e. I don't want any modification in the table directly stored to resource file, but to mark the file dirty, but I can't figure out how. How can I for example get the content of EditorInput, check it line by line, and modify it outside TextEditor?
Or, are there more efficient ways to do this? Can anyone give any hints?
Answer: The IDocument used by the TextEditor gives you access to the document contents. Get this with something like:
IDocumentProvider provider = editor.getDocumentProvider();

IEditorInput input = editor.getEditorInput();

IDocument document = provider.getDocument(input);

IDocument has many methods for accessing lines such as:
int getLineOffset(int line);

int getLineLength(int line);

and methods for modify the text:
void replace(int offset, int length, String text);


Hough Line: Detect ticks on the image

Question: I am trying the detect ticks on the following image using hough line transformation:

I am using the following simple open CV code:
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imwrite('original.jpg',img)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
lines = cv2.HoughLines(edges,1,np.pi/180,200)
for rho,theta in lines[0]:
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))

    cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)

I am getting the following output:

I wanted to detect the ticks, but instead it detected the lines. How can I solve it? Any help or suggestion will be appreciated.
Comment: Can you please output "edges" image too? And what is that "200" parameter in houghLines?
Answer: I'm not sure what you mean by "ticks", I guess the green and red lines?!?
Using C++ API and HoughLinesP:
function call:
cv::HoughLinesP(edges, lines, 1, CV_PI/720.0, 30, 10 /* min-length */, 1 /* max gap */);

canny:
cv::Mat edges;
cv::Canny(gray, edges, 50, 150, 3);

I get this result for canny:
edges look like this

that's why the result looks like:

but using edges from thresholds:
edges = gray > 50;

edge image:

result:
Comment: you can probably increase result quality if you know that those lines are always either red or green, so thresholding might give even much better results.
Comment: Hi, yes i meant the green and red shapes.
Comment: Is the "threshold" version a help/solution or what are you missing?
Comment: Hi Micka, this is the one what I am expecting. Is it possible to get the lines which intersects each other 90 degree?
Comment: Hi Micka, can you explain me what does 'edges = gray > 50;' mean? I meant what does it do.
Comment: Gray > 50 results in the Mat where each pixel that has a value of bigger than 50 in the source Mat "Gray" is set to 255 in the destination Mat. So its just a simple thresholding. If you use R and G channel instead grayscale image it might become even better.
Comment: Thank you Micka. Which threshold value produced the result image? I used cv2.Canny(gray,100,150,apertureSize = 3) (till 100) but i did not get like yours.
Comment: dont use the canny, remove canny and use `edges = gray > 50` instead

Hough Line: Detect ticks on the image


Implementing countries to spinner in activity

Question: I am trying to implement: https://github.com/hbb20/CountryCodePickerProject to my project to show a list of all countries in a spinner.
I added code to my build.gradle (app):
implementation 'com.hbb20:ccp:2.3.4'

After that I added it to XML file (layout) as it state on https://github.com/hbb20/CountryCodePickerProject/wiki/Use-as-a-Country-Selector:
<com.hbb20.CountryCodePicker
        android:id="@+id/country"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        app:ccp_showFullName="true"
        app:ccp_showNameCode="false"
        app:ccp_showPhoneCode="false"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="0dp" />

When I run my application it looks like:

But when I click on the spinner to select country application crash and in Logcat I get an error:
2019-12-27 14:47:00.619 13536-13536/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.cryptowallet, PID: 13536
    android.view.InflateException: Binary XML file line #78: Binary XML file line #78: Error inflating class com.futuremind.recyclerviewfastscroll.FastScroller
    Caused by: android.view.InflateException: Binary XML file line #78: Error inflating class com.futuremind.recyclerviewfastscroll.FastScroller
    Caused by: java.lang.reflect.InvocationTargetException

Comment: Shame, I guess I'll try to found another for countries as this one crashes on 8, 9, and 10 for me. Thank You.
Comment: Maybe this issue about the same problem https://github.com/hbb20/CountryCodePickerProject/issues/362

I can recommend you also to raise another one. That really looks like a bug in this library.
Answer: Add below 2 lines in gradle.properties file.
android.useAndroidX=true
android.enableJetifier=true


How do I model the volatility of an arithmetic mean?

Question: Suppose I have a model of stock prices developed using Brownian motion.  I have a second time series derived from the first.  At each price point in the first time series, I take the arithmetic mean up to that point and that is the data point for my second time series.  The volatility of the second time series is lower because the arithmetic mean has a dampening effect.  Are there any statistical tools to model the volatility of the second time series?
Comment: did you try standard volatility models? Since you take arithmetic means and do not estimate anything, the end result is again time series, so I do not see why standard models cannot be applied. Whether they will be appropriate is another matter, but that applies for any time series data.
Comment: Is your model actually a scaled Brownian motion with drift? If so, averaging the price process over $n$ observations to get a second series yields another Gaussian process and is determined by its mean and covariance, which you can readily calculate via the mean and covariance of the underlying price process. If your model is some (nonlinear) *function* of a Brownian motion, then things get more complicated.
Answer: Let's say your stock price series is $S_t$ so that your arithmetic average series is $X_t = \frac{1}{t}\sum_{j=1}^t S_j$. The series of returns that you use to calculate the volatility can be defined in several ways, the most popular being the log-returns series
$R_t = \frac{1}{\delta t} \log \frac{S_t}{S_{t-1}}$
and the simple returns series
$R_t = \frac{1}{\delta t} \left( \frac{S_t}{S_{t-1}} - 1 \right)$
where $\delta t$ is the time between observations (measured as a fraction of a year). Both of these involve the ratio $S_i / S_{i-1}$, so to look at the volatility of the series $X$ we should calculate this ratio:
$\frac{X_t}{X_{t-1}} = \frac{t-1}{t} \left( 1 + \frac{S_t}{S_1+\cdots+S_{t-1}} \right)$
Either way you cut it, the returns series for $X$ is not going to be pretty. The most straightforward approach is to look at the simple returns series for $X$, let's call it $Q$:
$Q_t = \frac{1}{\delta t} \frac{t-1}{t} \left( \frac{S_t}{S_1+\cdots+S_{t-1}} - \frac{1}{t-1} \right)$
The volatility of the series $X$ is the standard deviation of this series. Note that even if the volatility of $S$ is constant, the volatility of $X$ won't be constant, because of the dependence of the series on $t$. In fact, under some mild assumptions on the nature of the series $S$ (eg bounded returns) you should be able to show that the volatility of $X$ decreases to zero as $t\to\infty$.
This doesn't fully answer your question, but it points out some of the difficulties involved in what you're trying to do.
Perhaps a better idea (depending on what you're trying to achieve) is to take a moving average of your stock price series, taking the arithmetic average of the previous $T$ observations. At least this way your averaged series won't have an explicit dependence on the time index $t$.
You might also consider taking the geometric mean rather than the arithmetic mean. Although conceptually less intuitive, it has the advantage that it is equivalent to taking the arithmetic average of the log-returns series, which greatly simplifies the algebra later.


Gerador de chaves primárias em Django/Python com baixo risco de colisão e boa performance

Question: GERANDO CHAVES PRIMÁRIAS EXCLUSIVAS NA APLICAÇÃO
Eu gostaria de obter dicas de como gerar chaves primárias com baixo risco de colisão e que não reduza a performance das operações em PostgreSQL.
Explicando o problema:
Supondo que eu tenha 3 modelos: Cliente, Produto e Usuário
Em uma determinada aplicação eu gostaria de juntar todos esses modelos em uma mesma tabela de dados e poder fazer buscas através da chave primária, mas para isso cada registro deve possuir um identificador exclusivo. Neste caso, eu já havia pensado em utilizar UUID, mas são 128 bits e isso acaba prejudicando a performance do SGDB PostgreSQL. Por esta razão é que estou aqui em busca de alguma ideia que seja melhor do que as que eu já tive. Eu não preciso de um identificador exclusico universal, mas que seja exclusivo apenas dentro da aplicação.
Eu pensei em usar, com baixa possibilidade de colisão:
import uuid
import base64

def get_pk():
    pk = str(base64.b64encode(uuid.uuid4().bytes))[2:-3]
    return pk

Desde já eu agradeço por qualquer dica!
Comment: Qual a versão do postgres que está utilizando?
Comment: O pseudo tipo [SERIAL](https://www.postgresql.org/docs/8.1/datatype.html#DATATYPE-SERIAL) satisfaz o seu requisito?
Comment: @Danizavtz eu uso a versão 12
Comment: @AugustoVasques não satisfaz, porque eu já uso o SERIAL nas tabelas separadamente. Quando eu faço a junção das tabelas ocorrem colisões. É lógico que eu poderia definir faixas de valores diferentes para cada tabela, mas estou justamente buscando algum método mais Pitônico para resolver o problema.
Comment: Veja esse exemplo se o ajuda: http://sqlfiddle.com/#!17/163f5/2 no caso é utilizado um [gerado de sequencias](https://www.postgresql.org/docs/current/sql-createsequence.html) para criar as chaves, a cada inserção, independentemente de qual for a tabela, é gerado uma nova chave.
Comment: Boa alternativa, @AugustoVasques. Eu vou avaliar as alternativas, considerando segurança, performance e probabilidade de colisões, mas agradeço muito pela colaboração de todos.
Answer: Uma alternativa é solucionar o problema em SQL definindo um novo gerador de sequência com CREATE SEQUENCE e compartilhá-lo entre as tabelas criando assim a cada inserção uma nova chave primária exclusiva com nextval() seja qual for a tabela assim evitando a colisão de chaves.
--Inicializa um no gerador de sequência.
CREATE SEQUENCE chave_geral START 1;

CREATE TABLE Cliente (
    /*A cada inserção na tabela Cliente o gerador de sequência chave_geral será
    incrementado e utilizado como valor para o campo pKey.*/
    pKey integer DEFAULT nextval('chave_geral') NOT NULL,
    nome CHARACTER(10)
);

CREATE TABLE Produto (
    /*A cada inserção na tabela Produto o gerador de sequência chave_geral será
    incrementado e utilizado como valor para o campo pKey.*/
    pKey integer DEFAULT nextval('chave_geral') NOT NULL,
    nome CHARACTER(10)
);

CREATE TABLE Usuario (
    /*A cada inserção na tabela Usuario o gerador de sequência chave_geral será
    incrementado e utilizado como valor para o campo pKey.*/
    pKey integer DEFAULT nextval('chave_geral') NOT NULL,
    nome CHARACTER(10)

INSERT INTO Produto(nome) VALUES('Produto1');
INSERT INTO Usuario(nome) VALUES('Usuário1');
INSERT INTO Cliente(nome) VALUES('Cliente1');

SELECT C.pkey AS ChaveCliente,
       P.pkey AS ChaveProduto,
       U.pkey AS ChaveUsuário
from Cliente C, Produto P, Usuario U;

teste o exemplo no SQL Fiddle
Quanto ao desempenho dessa abordagem é equivalente ao uso do pseudo tipo SERIAL:
CREATE TABLE tablename (
    colname SERIAL
);

Já que pseudo tipo SERIAL é açúcar sintático específico do PostgreeSQL usado para a criação de gerador de sequência e atribuí-lo ao coluna especificada:
CREATE SEQUENCE tablename_colname_seq AS integer;
CREATE TABLE tablename (
    colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;

Answer: Hashing o uuid vai gerar uma chave bem menor e com enorme possibilidade de não conflitar
import uuid
import base64

def get_pk():
    pk = hash(uuid.uuid4())
    return pk

Exemplo de saída:
>>> print(get_pk())

749691330465060769

Nota
Differença de tamanho na memória
>>> import sys

>>> sys.getsizeof(get_pk())
32

>>> sys.getsizeof(uuid.uuid4())
56

>>> sys.getsizeof(str(base64.b64encode(uuid.uuid4().bytes))[2:-3])
71

Comment: Para quem votou negativo, poderia dizer o motivo para que eu possa melhorar a resposta?
Comment: Obrigado pela dica, Paulo Marques! Eu vou aguardar para ver se alguém apresenta uma solução melhor, mas este método é bastante interessante. O meu objetivo é simples de resolver, mas não custa nada pesquisar por novas soluções.


Propensity score matching with time dependent covariates in R or SAS

Question: I'm trying to use propensity score matching (PSM) to find matched controls. The data is not cross-sectional and it has time dependent information (monthly, quarterly, etc.). Let us say I have information from t_0 to t_N for both control units and treated units, and the event (treatment) for the treated units can happen at any time between t_0 and t_N. Is there an easy way to use propensity score matching using R or SAS to find the matched controls for each of the treated units?
Comment: I don't know about easy, but if you search at Mayo Clinic website they have SAS macros for propensity score matching.
Comment: I understand a typical propensity score matching, but I'm looking for one in the context where we have time-dependent covariates.
Answer: Not matching, but you might want to look up marginal structural models, which essentially use propensity score based weights to balance treated/untreated cases on time invariant and time varying confounders.
For reference you can see:
Robins, Hernan, Brumback (2000) Marginal structural models and causal inference in epidemiology, Epidemiology, 11 (5): 550-560


NodeJS Filesytem sync and performance

Question: I've run into an issue with NodeJS where, due to some middleware, I need to directly return a value which requires knowing the last modified time of a file.  Obviously the correct way would be to do
getFilename: function(filename, next) {
    fs.stat(filename, function(err, stats) {
        // Do error checking, etc...
        next('', filename + '?' + new Date(stats.mtime).getTime());
    });
}

however, due to the middleware I am using, getFilename must return a value, so I am doing:
getFilename: function(filename) {
    stats = fs.statSync(filename);
    return filename + '?' + new Date(stats.mtime).getTime());
}

I don't completely understand the nature of the NodeJS event loop, so what I was wondering is if statSync had any special sauce in it that somehow pumped the event loop (or whatever it is called in node, the stack of instructions waiting to be performed) while the filenode information was loading or is it really blocking and that this code is going to cause performance nightmares down the road and I should rewrite the middleware I am using to use a callback?  If it does have special sauce to allow for the event loop to continue while it is waiting on the disk, is that available anywhere else (though some promise library or something)?
Comment: It's strange that the middleware does not allow for async execution because without IO you cannot get *anything* done.
Answer: Nope, there is no magic here.  If you block in the middle of the function, everything is blocked.
If performance becomes an issue, I think your only option is to rewrite that part of the middleware, or get creative with how it is used.

NodeJS Filesytem sync and performance


One router and one modem+router. Can I pass a static IP range from one to the other?

Question: I had been running a Huawei HG612 connected to a DD-WRT router for internet services using PPPoE as authentication.
Due to an office move, I need to have a DG834 modem+router which will be used for connecting to a PPPoA network. After that, the router running DD-WRT will take over for the NATing and firewall rules.
There is 1 dynamic IP and 5 static IPs available to us. Is there a configuration I can implement where the modem takes the dynamic IP and the DD-WRT router is passed traffic for the 5 static IPs? I've been looking at static routes but I'm a bit out of my depth!
Answer: Looking at the manal for the dg834gt, you should be able to do this as follows -

Set up the router "as normal" so that the ADSL (PPPoA) connection works.
Set the LAN range as 192.168.0.x - make sure both your DG834 and DD-WRT router are
in the same range AND HAVE STATIC IP ADDRESSES ASSIGNED.
Set up a static route with your subnet with the gateway being the 192.18.x.x address of
the DD-WRT router.  You can do this in advanced -> Static Routes.  (See page 102 of this
manual).

Depending on your model (according to this link), you might also be able to use "Half Bridge" mode, which pushes the external IP address to the DD-WRT using DHCP.   (Although I don't have the luxury of a subnet routed to my ADSL connection, I also have a PPPoA connection and use a cheap dynalink modem to do half bridging to my Linux box - this is probably a better solution) as it makes your ADSL router seemless to the wider Internet and is probably simpler to configure.
Comment: Thanks for your help! This works inbound, but doesn't appear to work outbound as I now have 2 NATs operating on the network. Is there a way I can safely disable the NAT on the DG834?
Comment: Have a look at http://support.zen.co.uk/kb/KnowledgebaseArticle.aspx?articleid=10048 - Specifically there appears to be a setting to disable NAT.  As you have valid IP's you should be able to disable this OK.  REMEMBER THAT IF YOU DONT HAVE NAT YOU NEED TO SET UP FIREWALLING - POSSIBLY ON EACH DEVICE, OR THEY WILL BE DIRECTLY ACCESSIBLE AND MORE VULNERABLE TO HACKING.


Responsive Image Gallery

Question: I want to create a responsive Image gallery block. I have used Views Slideshow Liquid Carousel module for this but that is not working properly, It breaks when the screen is too small. I want a carousel like display of images with responsive skin like http://tympanus.net/Development/Elastislide/, I have also tried this plugin but this plugin is not working properly can't find why. So is there any other way or plugin for this.
Comment: I solved this using flexslider plugin
Answer: Trying to use these modules may help you (quotes included are from their project pages):

Responsive Images:

... integrates the Responsive Images library created by Filament Group.
The purpose of this library is to load images in different sizes based upon
  the resolution of the browser loading the page. This allows for mobile-optimized images.
... provides a "Responsive Images" image field formatter for which you can select
  both a small (mobile-optimized) image style as well as a large (for normal screens) image style. The image style configuration options are provided by Drupal core.

Responsive images and styles:

... solves the problems with images and responsive themes, it allows you to define multiple image style suffixes and their corresponding maximum width.
Answer: Drupal 8 module for responsive images will be in the picture module, a Drupal 7 Version exists today: Picture
Using the picture module, instead of the other available alternatives, will probably help in upgrading pages from 7 to 8.

Responsive Image Gallery


How do I make it so only people with a role in my server can use the bot?

Question: I want my bot to only run a command if they have a role in my server. My bot is in many servers and I want the command to only work if they have a role in a certain server.
I have tried many ways such as:
let guildcheck = client.guilds.get('496780648351006720')

if (guildcheck.member.roles.has(role => role.name === 'Role')) {

message.channel.send("works")

}else{

message.channel.send("doesn't work")

}

I expected this to make it so if I have "Role" in the guld "496780648351006720" it would reply with "works".
Comment: There doesn't seem to be a member property on the guild object? checkout: https://discordapp.com/developers/docs/resources/guild#guild-object. From the following endpoint: https://discordapp.com/developers/docs/resources/guild#get-guild
Comment: What is currently wrong with the code; What action it is actually doing right now?
Comment: @Kaynn Cannot read property 'has' of undefined
Answer: if (guildcheck.member.roles.has(role => role.name === 'Role'))
You can't directly get a guild member just by doing guildcheck.member, since there are so many members in the guild; The code would be confused as to which member to fetch anyway.
What you were trying to use was probably a member(User) function, not a property. Following the documentation, you would need to give it a user as an argument, like so:
if (guildcheck.member(message.author).rol...)
This way, you can get the message's sender as a guild member.(Note that guildcheck.member(message.author) will be null if the user isn't in the target guild.)
Alternatively...
You can do message.member to simply get the message's sender as a guild member It would just return null/default if the message wasn't in a guild (Or the user isn't in the server.)
The only issue would be that it now allows any user in a guild to use your command.You can avoid this by checking against the guild id of where this message was sent in, like so:
// Ignore if this is not the desired guild.
if (message.guild.id !== "Your desired guild ID") { return; }

if (message.member.roles.has(role => role.name === 'Role')) {
  message.channel.send("works");
}else{
  message.channel.send("doesn't work");
}

Comment: I done some tweaking to this to make it suit me, thanks!


extrema of a polynomial

Question: Say I'm given a general sextic equation
$$ax^6+bx^5+cx^4+dx^3+ex^2+fx+g =0$$
I want to know how I can tell where the critical points (or least say how many there will be) will be since the Abel-Ruffini theorem tells me that it is not possible to express the solutions via radicals, because the derivative of the above functions will be a $5^{th}$ order polynomial. I'd also like to know if there is away to determine how the extrema will change if I vary the parameters. Anyone knows some methods that make this possible? I know about Descartes rule of signs but that doesn't give me a very clear answer just a range. I also know that the roots of an arbitrary sextic equation can be expressed as the soluution of a hypergeometric differential equation but I don't want something that deep.
Comment: The best I can come up with: There are either $1,3$ or $5$ critical points using the conjugate root theorem (assuming that the polynomial have real coefficients.
Answer: As you say, the derivative is a polynomial of degree $5$ with symbolic coefficients.  The discriminant of that derivative is, according to Maple,
$$4050000\,{a}^{4}{f}^{4}-5400000\,{a}^{3}be{f}^{3}-9720000\,{a}^{3}cd{f
}^{3}+6912000\,{a}^{3}c{e}^{2}{f}^{2}+8748000\,{a}^{3}{d}^{2}e{f}^{2}-
8294400\,{a}^{3}d{e}^{3}f+1769472\,{a}^{3}{e}^{5}+5400000\,{a}^{2}{b}^
{2}d{f}^{3}-180000\,{a}^{2}{b}^{2}{e}^{2}{f}^{2}+6480000\,{a}^{2}b{c}^
{2}{f}^{3}-8856000\,{a}^{2}bcde{f}^{2}+921600\,{a}^{2}bc{e}^{3}f-
4374000\,{a}^{2}b{d}^{3}{f}^{2}+6609600\,{a}^{2}b{d}^{2}{e}^{2}f-
1658880\,{a}^{2}bd{e}^{4}-4147200\,{a}^{2}{c}^{3}e{f}^{2}+4276800\,{a}
^{2}{c}^{2}{d}^{2}{f}^{2}+3870720\,{a}^{2}{c}^{2}d{e}^{2}f-1179648\,{a
}^{2}{c}^{2}{e}^{4}-4898880\,{a}^{2}c{d}^{3}ef+1492992\,{a}^{2}c{d}^{2
}{e}^{3}+944784\,{a}^{2}{d}^{5}f-314928\,{a}^{2}{d}^{4}{e}^{2}-4800000
\,a{b}^{3}c{f}^{3}+720000\,a{b}^{3}de{f}^{2}-216000\,a{b}^{3}{e}^{3}f+
4896000\,a{b}^{2}{c}^{2}e{f}^{2}+3024000\,a{b}^{2}c{d}^{2}{f}^{2}-
5371200\,a{b}^{2}cd{e}^{2}f+1382400\,a{b}^{2}c{e}^{4}+194400\,a{b}^{2}
{d}^{3}ef-64800\,a{b}^{2}{d}^{2}{e}^{3}-3628800\,ab{c}^{3}d{f}^{2}+
184320\,ab{c}^{3}{e}^{2}f+3075840\,ab{c}^{2}{d}^{2}ef-921600\,ab{c}^{2
}d{e}^{3}-699840\,abc{d}^{4}f+233280\,abc{d}^{3}{e}^{2}+663552\,a{c}^{
5}{f}^{2}-663552\,a{c}^{4}def+196608\,a{c}^{4}{e}^{3}+165888\,a{c}^{3}
{d}^{3}f-55296\,a{c}^{3}{d}^{2}{e}^{2}+800000\,{b}^{5}{f}^{3}-960000\,
{b}^{4}ce{f}^{2}-720000\,{b}^{4}{d}^{2}{f}^{2}+1080000\,{b}^{4}d{e}^{2
}f-270000\,{b}^{4}{e}^{4}+864000\,{b}^{3}{c}^{2}d{f}^{2}-48000\,{b}^{3
}{c}^{2}{e}^{2}f-720000\,{b}^{3}c{d}^{2}ef+216000\,{b}^{3}cd{e}^{3}+
162000\,{b}^{3}{d}^{4}f-54000\,{b}^{3}{d}^{3}{e}^{2}-172800\,{b}^{2}{c
}^{4}{f}^{2}+172800\,{b}^{2}{c}^{3}def-51200\,{b}^{2}{c}^{3}{e}^{3}-
43200\,{b}^{2}{c}^{2}{d}^{3}f+14400\,{b}^{2}{c}^{2}{d}^{2}{e}^{2}
$$
As you vary the coefficients, the points where the number of real critical points changes will be points where that discriminant is $0$.
You can also use a Sturm sequence to determine the number of real critical points, or the number in a given interval,  for given values of the coefficients.
Comment: Wow... that's hideous lol. What command in MAPLE did you use to find the discriminant?
Comment: discrim(diff(p,x),x);

extrema of a polynomial


Effect of Variable transformation on predictive model

Question: Two questions here :

Should transformation of skewed distribution is neccessary for classification problem ?
And consider a case where I have already transformed variables , so the equation will work for the transformed variables.
Now when I use this model on a new and never seen data where the variables are not transformed , will it hold good ?
Answer:
From my experience it's not necessary, but transforming variables can be very beneficial. It's also always judgement call depending on the variable. It's also dependent on the type of model you're using; for logistic regression it's a good idea and one recommendation would be to use Weight of Evidence (WOE) binning.

For other tree based algorithms, such as Random Forest, you don't need to as the algorithm will make the cuts itself.

The answer is No. For the sake of argument, let's say you have create a Logistic Regression, with one transformed variable.

That transformed variable is actually a NEW variable, so once you get a new dataset, you should apply the same transformations and then make your predictions.
If I haven't been clear in any of my points just let me know and I will be happy to elaborate. :)
Comment: You don't actually need to transform your data. Even if you are doing a Linear Regression, and if you are you would need to check that your errors are following a normal distribution.

Since you're doing a classification algorithm, there is no need to get rid of the skewed data. You can bin variables using WOE to increase prediction power and robustness, but doing a log transformation on skewed data is unnecessary, specially since you're not using a Linear Regression.
Comment: Thank you Ehsan. I was wondering about Kaggle Data science competitions . Many competitions have test set with them. So if I apply transformations on my training and validation set, it won't rather can't be applied to test set as they are not accessible. Any idea or suggestion ?
Comment: I think I understand what you mean. So in this case, your code structure should be: 1) Transform variables, 2) Create model. Then for your prediction stage (on new inaccessible dataset), write a code that will first transform any new dataset and then make a prediction. Does this make sense?
Comment: Oops, I confused my self. You are correct Ehsan. Thank you. And Can you please tell me how does transformation basically affect predictive power ? I have data which is right skewed, i have read log transformations works best for right skewed data . I want to know how will it increase the predictive power .


How to pull a record from Salesforce and immediately push the node ID back to Salesforce

Question: I'm using the Salesforce Suite module.
What I'm trying to accomplish:
When a record is created in Salesforce, that record is pulled into Drupal and the new node content ID is pushed back to the Salesforce record (for reference in Salesforce).
I have a mapping set up as such:
Triggers

Drupal create (push)
Drupal update (push)
Salesforce create (pull)
Salesforce update (pull)

Fields

Title - Salesforce to Drupal
Content ID - Drupal to Salesforce

With this setup, it pulls the record into Drupal but the Content ID doesn't get pushed back to Salesforce. It will, however push when I update (just save) the node but we don't expect these nodes to be saved anytime soon after it's pulled in.
I've tried a few different ways leveraging the events dispatched by the Salesforce module but it always leads me to a race condition or a circle.
Comment: Isn't there some setting in the module on Drupal that lets you expose a certain URL you can call via cron to have the sync triggered? Do that. Another alternative would probably be to trigger resaving nodes on cron. Or maybe there's some hook that can be triggered right after the node got stored to then resave it to trigger the sync. Can you have a look around, maybe hook_entity_insert or similar?
Comment: If I enable the standalone url, it disables core cron processing for that mapping, forcing me to leverage that url for everything... not ideal. I can save the node on cron run but this leaves me having to wait until next cron for the node ID to be pushed to salesforce. I'm looking for something simultaneous.
Comment: I took a second glance at your advice. See answer below. Thanks @leymannx
Answer: Here's how I did it...
Since content is set to push (immediately) on update, I added a hook_entity_insert() that responded not to the content insert but the "salesforce_mapped_object" entity.
This ensures that not only was the node/user created but a mapping was created.
<code>/**
 * Implements hook_entity_insert().
 */
function MYMODULE_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {
  if ($entity->bundle() == 'salesforce_mapped_object') {
    $entity_info = $entity->get('drupal_entity')->first()->getValue();
    $type = $entity_info['target_type'];
    $id = $entity_info['target_id'];
    switch ($type) {
      case 'node':
        $node = \Drupal\node\Entity\Node::load($id);
        $node->save();
        break;
      case 'user':
        $user = \Drupal\user\Entity\User::load($id);
        $user->save();
        break;
    }
  }
}
</code>
Thanks to @leymannx in the above comment for the guidance.


How to save form data (table inside form) in many rows in MySQL database

Question: I am using a table inside HTML form and when i enter the details in form I want the data to be saved in two rows. Currently when i input data the row 1 gets overwritten by row 2 and only information submitted in row two gets displayed.
<form name="contact-form" action="" method="post" id="contact-form">

                Name
                Email ID
                Phone No
                Comments

             <input type="text" class="form-control" name="your_name[]" placeholder="Name" required>

             <input type="text" class="form-control" name="your_email[]" placeholder="Email" required>

            <input type="text" class="form-control" name="your_phone[]" placeholder="Phone" required>

            <input type="text" class="form-control" name="comments[]" placeholder="Comments" required>

             <input type="text" class="form-control" name="your_name[]" placeholder="Name" required>

             <input type="text" class="form-control" name="your_email[]" placeholder="Email" required>

            <input type="text" class="form-control" name="your_phone[]" placeholder="Phone" required>

            <input type="text" class="form-control" name="comments[]" placeholder="Comments" required>

        <button type="submit" class="btn btn-primary" name="submit" value="Submit" id="submit_form">Submit</button>

</form>

on the response side my code looks like this :
<?php
require_once("database_connection.php");
if((isset($_POST['your_name'])&& $_POST['your_name'] !='') && (isset($_POST['your_email'])&& $_POST['your_email'] !=''))
{
 require_once("contact_mail.php");

for ($i = 0; $i<count($_POST['your_name']); $i++){
$yourName = $conn->real_escape_string($_POST['your_name'][$i]);
$yourEmail = $conn->real_escape_string($_POST['your_email'][$i]);
$yourPhone = $conn->real_escape_string($_POST['your_phone'][$i]);
$comments = $conn->real_escape_string($_POST['comments'][$i]);

$sql="INSERT INTO contact_form_info (name, email, phone, comments) VALUES ('".$yourName."','".$yourEmail."', '".$yourPhone."', '".$comments."')";

}
if(!$result = $conn->query($sql)){
die('There was an error running the query [' . $conn->error . ']');
}
else
{
echo "Thank you! We will contact you soon";
}
}
else
{
echo "Please fill Name and Email";
}
?>

Answer: Your brackets were misplaced, check my code. Disclaimer - have'nt tried executing it.
<?php
require_once("database_connection.php");
require_once("contact_mail.php");
if((isset($_POST['your_name'])&& $_POST['your_name'] !='') && (isset($_POST['your_email'])&& $_POST['your_email'] !=''))
{
    for ($i = 0; $i<count($_POST['your_name']); $i++) {
        $yourName = $conn->real_escape_string($_POST['your_name'][$i]);
        $yourEmail = $conn->real_escape_string($_POST['your_email'][$i]);
        $yourPhone = $conn->real_escape_string($_POST['your_phone'][$i]);
        $comments = $conn->real_escape_string($_POST['comments'][$i]);
        $sql="INSERT INTO contact_form_info (name, email, phone, comments) VALUES ('".$yourName."','".$yourEmail."', '".$yourPhone."', '".$comments."')";
        if(!$result = $conn->query($sql)){
            die('There was an error running the query [' . $conn->error . ']');
        }
    }
    echo "Thank you! We will contact you soon";
}
else
{
    echo "Please fill Name and Email";
}

?>

Just a suggestion - try using MySQLi with prepared statements
Comment: thank you! works perfect. just misplaced the brackets. Can I ask why you suggested using MySQLi with prepared statements?


Ajuda para fazer operação matemática de divisão de dois selects

Question: Estou precisando calcular a porcentagem(%) com esses dois selects, já fiz a multiplicação do primeiro por 100, preciso agora pegar esse resultado e dividir pelo segundo select para ter o valor 95,34.
Tentei fazer com esse Union mas ele traz uma coluna com os dois resultados.
Resultado
With total as(
select COUNT(*)*100 as valor from glpi_tickets where solvedate >= DATE_SUB( DATE( NOW() ), INTERVAL DAY( NOW() ) -1 DAY) AND DATE > DATE_SUB( DATE( NOW() ), INTERVAL DAY( NOW() ) -1 DAY)
union
select COUNT(*) as valor from glpi_tickets where DATE > DATE_SUB( DATE( NOW() ), INTERVAL DAY( NOW() ) -1 DAY)
)
Select * from total;

Answer: Para calcular a porcentagem com base nos resultados dos dois selects, você pode usar uma subconsulta dentro da sua query principal. Aqui está como você pode fazer isso:
WITH total AS (
    SELECT
        (COUNT(*) * 100) AS total_solved,
        (SELECT COUNT(*) FROM glpi_tickets WHERE DATE > DATE_SUB(DATE(NOW()), INTERVAL DAY(NOW()) - 1 DAY)) AS total_tickets
    FROM
        glpi_tickets
    WHERE
        solvedate >= DATE_SUB(DATE(NOW()), INTERVAL DAY(NOW()) - 1 DAY)
        AND DATE > DATE_SUB(DATE(NOW()), INTERVAL DAY(NOW()) - 1 DAY)
)
SELECT
    total_solved / total_tickets AS percentage
FROM
    total;

Esta consulta calcula o número total de tickets resolvidos e o número total de tickets, depois divide o número total de tickets resolvidos pelo número total de tickets para obter a porcentagem. Isso deve dar o resultado que você está procurando.