Adding a Serial (cck field) to the Page Title

Question: I don't really know PHP or how to modify the Drupal API so it would probably have to be a solution in the Drupal interface or rather simple programming.
Is it possible to have a Serial cck field automatically combine with the page/node title? Example, Title: Update Example Task. Serial (job number): 225 -- Automatically combine to make the page/node title: 225 Update Example Task  -- So it can be referenced as a autocomplete term with either the Serial field or the Title
Thanks for any input!
Answer: Checkout auto_nodetitle, you could add the serial field as a token to the node title automatically.
The rest should then just fall into place.
Comment: Excellent Solution, exactly what I was looking to do, thanks!


Java: Extending ArrayList Using Objects

Question: I'm extending ArrayList (not implementing an interface) in Java. I get an error in the function:
 public void push(Object o) {
    add(o);
}

It says "The method add(E) in the type ArrayList< E > is not applicable for the arguments. How can I fix this?
Comment: Please show some more code on how you are extending the arraylist. I hope you  know what you are doing..:P
Comment: `public void push(E o) {` - But you have to ask the question why?
Answer: This works for me, I m not sure what you want to achieve with this.
public class MyList<E> extends ArrayList<E>{
     public void push(E o) {
            add(o);
        }
}

Answer: As pointed out by @shikjohari you need to specify how you extend the ArrayList.
The reason you get this error is because you are trying to push an Object to an ArrayList that is expecting to get something of type E. So you need to explicitly cast the o to type E in order for this to work (of course, you need to ensure that o is indeed of dynamic type E before you perform casting).
Assuming E is a class defined somewhere else (it does not represent a generic type here, otherwise, the compiler should give you another error - "E cannot be resolved to a type" instead of "The method add(E) in the type ArrayList< E > is not applicable for the arguments"), you should have something like the following:
// This class is defined somewhere in your package
class E {

}

public class YourClass extends ArrayList<E> {
    public void push(Object o) {
        if (o instanceof E) {
            add((E) o);
        }
    }
}

Comment: I assume `E` is a class defined somewhere else, it does not represent a generic type in this case. Otherwise, the compiler should give you another error - "E cannot be resolved to a type"
Comment: Thanks for giving it a look. shikjohari solved the problem :)

Java: Extending ArrayList Using Objects


How to work with a single git repo on 2 servers: production and testing?

Question: We have a single git repository on a test server (let's call it test1). The repository is very simple, just master no branches or anything fancy. The goal of this repo is to track changes to /etc/puppet and all of its sub folders. It's usually clean and synced with head.
Now we want to copy everything in test1:/etc/puppet to a production server (let's call it prod1) while maintaining a proper git workflow between to two machines. Whenever changes on test1 are ready for production, we want to use git to push them from test1 to prod1. The goal here is to be able to quickly revert back changes on prod1 if anything breaks.
This is what I have of so far:

Setup a bare git repo (git init --bare) on test1:/opt/puppet.git to act as an intermediate server.
Push data from test1:/etc/puppet to the bare.
On prod1 init a new repo in /etc/puppet
Add test1:/opt/puppet.git bare as remote to prod1:/etc/puppet
Pull master data on from test1 to prod1 whenever we want to apply changes to production.

What are your thoughts? Should we continue to use a single master branch or we need to create a new devel branch? If we create devel branch, how do we use it with the newly created bare repo?
Answer: If I summarize your request, what you want, is to have an architecture with 1 git server which receives all the pushes from users, and automated pushes to prod when it's ready.
This is exactly what is done with architectire based on gerrit review system for instance.
The workflow is as follow :

Git pull from prod server
Do you changes locally
Git push to test server (always in dedicated branch)
Assign reviewer to the commit and add extra hooks to launch for instance smoke test on jenkins (the hakks are added during installation pahse).
When reviewers have accepted the code and the jenkins smoke test are ok, if trivial merge is still possible with master, push is done via gerrit automatically.

So the good point here is that nobody (if properly configured) can push to master without minimal review and basic test. So you master branch from prod is (almost) always clean.
Answer: I've actually sorted out this one by myself. After some testing, I've created an intermediate bare git repo (git init --bare) on test1. This will act kind of as a git server that will receive and push commits to test1 + prod1 repos at will.
To achieve this I did the following:
On test1 machine:
1. mkdir /opt/puppet.git
2. cd /opt/puppet.git
3. git init --bare
4. cd /etc/puppet (where the current git repo with actual code is located)
5. useradd git && passwd git (to allow push/pull from remote hosts)
6. git remote add origin git@test1:/opt/puppet.git
7. git push origin --all
I now have all my code stored in the bare repo at /opt/puppet.git
On prod1
1. cd /etc
2. git clone git@test1:/opt/puppet.git
3. If I want to pull latest changes: git pull


Are N-1 distinct points with two classes Linearly Separable in an N Dimensional Space

Question: I have this conjecture whose verification I haven't been able to find online anywhere. If you have N - 1 distinct points that each have one of two classes, can you find a linear decision boundary in an N dimensional space that perfectly separates the classes?
EDITS
Changed N to N-1. If you are in R^3 with the points (1, 0, 0), (2, 0, 0) (3, 0, 0) and you have the colors Red, Blue, Red respectively, you won't be able to draw a plane that separates them.
Answer: Short answer
In general this is not true. Consider $N-1$ points in $\mathbb{R}^N$ and a line $d$. Place the $N-1$ points any way you want in $d$. Now, the intersection of any affine hyperplane of $\mathbb{R}^N$ with $d$ will either be empty, a point, or the whole line. Any way, if $N-1 \ge 3$, not all classifications will be possible.
Longer answer
This being said, let's get to the interesting part. It is also always possible to place $N-1$ points in $\mathbb{R}^N$ such that they can be classified in any way you want by an affine hyperplane. In fact, you can place up to $N+1$ points in $\mathbb{R}^N$ and still have that property.
To prove this, we'll explicity place $N+1$ points in $\mathbb{R}^N$ in a particular way and count the number of ways we can classify those points. If we get $2^{N+1}$ ways of classifying them, it will mean that we can make any classification we want. (Make sure you see why.)
I say that the points
$$ x_1 = (1^1, \dots, 1^N), x_2 = (2^1, \dots, 2^N), \dots, x_{N+1} = ((N+1)^1, \dots, (N+1)^N) $$
will do the job.

Recall that our classification function is $sign(h(x_i))$, where $h(x_i)$ is the dot product between a weight vector $w=(w_0, w_1, \dots, w_N)$ and our vector $x_i$ augmented with a new coordinate fixed to $1$. That is, for $< , >$ the dot product,
$$
h(x_i) = <(w_0, w_1, \dots, w_N), (1, x_{i_1}, \dots, x_{i_N}) >
$$
If we let $P$ be a polynomial of $\mathbb{R}[X]$ and say $P = w_0 + w_1 X + \dots + w_N X^N$, then we find
$$
h(x_i) = P(i)
$$
Now, fix $h(x_1) = P(1) >0$ (you could also say $P(1) < 0$ ). Choose any $d \le N$ and $u_1, \dots u_d \in \{2, 3, \dots, N+1\}$ such that $u_1 < u_2 < \dots < u_d$.  We can choose $P$ (that is, choose the weights $w_i$ using polynomial interpolation) such that
\begin{array}{l}
P(1) > 0, \dots P(u_1 -1 ) >0\\
P(u_1) < 0, \dots P(u_2 -1) < 0\\
P(u_2) > 0, \dots P(u_3 -1 ) >0\\
\vdots\\
(-1)^d P(u_d) >0 , \dots,  (-1)^d P(u_N) >0
\end{array}
We thus have an alternance in our classification over the intervals $[1, u_1), [u_1, u_2), \dots, [u_d, N]$.

It's now time to count. For two choices of $d$, the classifications are disjoint. We had two choices for the sign of $P(1)$, and ${N \choose d}$ possibilities for the $u_1, \dots u_d$. Therefore, the we can classify our $N+1$ points in (at least)
$$
2\cdot \sum\limits_{i=0}^{N} {N\choose i} = 2^{N+1}
$$
ways. We won!
Generalisation
I went through that long argument in order to get this number : $2\cdot \sum\limits_{i=0}^{N} {N\choose i}$. It turns out that $N$ points in $\mathbb{R}^d$ can be chosen so they can be classified in
$$
m(N) := 2\cdot \sum\limits_{i=0}^{d} {N-1\choose i}
$$ ways. Furthermore, no $N$ points will have more than $m(N)$ classifications.
You can use the proof we made in order to build $N$ points that can be classified in $m(N)$ ways. I found it is harder to prove that $m(N)$ is also a maximum (I'd be interested in a simple proof, if someone has one.)

Note: I think I got carried away.
Answer: VC-dimension and shattering are keywords for this kind of question. The typical expression involved is "A 2-D hyperplane shatters 3 points". Indicating that if there are three points that can have two classes, then a 2-D hyperplane can always separate the classes. So N distinct points can be separated by a N-1 dimensional hyperplane in a N-1 dimensional space.
For more information see slides 8 and 9: https://www.google.si/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCAQFjAA&url=https%3A%2F%2Fwww.cs.toronto.edu%2F~hinton%2Fcsc2515%2Fnotes%2Flec10svm.ppt&ei=X9hNVYmQNMONsAGj54GoCQ&usg=AFQjCNFuwD_e_uNS4e1eLmeY9K4mKTdOWw&sig2=1bwN22vlPsMvovR8Y4e1_w&bvm=bv.92885102,d.bGg
I am not sure how this works out for more than 2 classes.


Views relationship not been added by code

Question: Im trying to make a abandoned cart notification module, trying to relate my notification table with the commerde_order table by its order_id in views. Right now i have this in my hook_views_data
<code>function commerce_abandoned_cart_notification_views_data(){
    $data = array();

    $data['commerce_abandoned_cart_notification']['table']['group']  = t('Commerce abandoned cart notification');

    $data['commerce_abandoned_cart_notification']['table']['join'] = array(
        'commerce_order' => array(
            'left_field' => 'order_id',
            'field' => 'order_id',
        ),
    );

    $data['commerce_abandoned_cart_notification']['table']['base'] = array(
        'field' => 'id',
        'title' => t('Commerce abandoned cart notification'),
        'help' => t('Commerce abandoned cart notification'),
    );

    $data['commerce_abandoned_cart_notification']['id'] = array(
        'title' => t('Notification ID', array(), array('context' => 'A Commerce abandoned cart notification internal ID')),
        'help' => t('The unique internal identifier of the notification.'),
        'field' => array(
            'handler' => 'views_handler_field',
            'click sortable' => TRUE,
        ),
        'filter' => array(
            'handler' => 'views_handler_filter_string',
        ),
        'sort' => array(
            'handler' => 'views_handler_sort',
        ),
    );

    $data['commerce_abandoned_cart_notification']['order_id'] = array(
        'title' => t('Order ID', array(), array('context' => 'A Commerce order ID')),
        'help' => t('The unique internal identifier of the order.'),
        'relationship' => array(
            'base' => 'commerce_order',
            'field' => 'order_id',
            'handler' => 'views_handler_relationship',
            'label' => t('Referencing order'),
        ),
        'field' => array(
            'handler' => 'views_handler_field',
            'click sortable' => TRUE,
        ),
        'filter' => array(
            'handler' => 'views_handler_filter_string',
        ),
        'sort' => array(
            'handler' => 'views_handler_sort',
        ),
    );

    return $data;
}
</code>
The problem is that when creating a view i do not see my relationship to commerce orders or viceversa, (the hook works because i can create a view of type commerce_abandoned_cart_notification and add some of its fields). I know there is a commerce_abandoned_cart_notification or commerce_notification module but they seem incomplete and/or abadoned
Should i use hook_views_data_alter to alter commerce_order relatations and add one to my custom table?
Answer: Have you considered using the module Commerce Notification?


Neural Network and H2O.ai: inputs that have multiple right answers (questions about a previously answered question)

Question: I have two questions related to this previously answered question.

Can someone explain how the NN's backproagation algorithm would not be able to function properly when inputs have multiple right answers?
I have a similar problem where in a data set, there are times when the inputs result in a certain output, and other times when the inputs result in a completely different (but equally valid) output. I am trying to use H2O.ai to train a model on this data, but am new to the platform. Does anyone know how this can be done?

Thanks :)
Answer: Regarding your question 1, I read the linked-to answer as it will work.
And regarding question 2, one of the nice things about H2O is how easy it will be to try it out, but I believe it should just work. (I suspect training in batches might cause problems, but because H2O updates weights after every training sample, i.e. batch size of 1, I think it will be okay.)
E.g. if you have three possible outputs, A, B and C, and two inputs, and if you have a couple of training rows that look like this:
<code>blue,green,A
blue,green,C
</code>
With a bit of luck, when you give your trained network the inputs of "blue" and "green" it will give a probability of 0.5 for A, 0.0 for B, and 0.5 for C. A bit of noise might mean C is 0.501, and A is 0.498, and B is 0.001, in which case it would predict C consistently. But, as I understood the question, that is fine?
Comment: What you say might work IF the output of the NN is a number (between 0 and 1) which can be interpreted as a probability !
Comment: @aginensky At least in H2O, that is what the outputs are for a binomial or multinomial classification: numbers between 0.0 and 1.0, that will total to 1.0. And if you train for logloss (the default) it will be trying to optimize the accuracy of those probabilities.
Comment: Of course, but that is what I was saying.  Out of the box, there is no reason for a NN to return a probability, but it can be given input parameters so that it will return a probability.
Comment: I also think that, while the OP framed this as a question about h20, the question really has nothing to do with h20 and everything to do with how a trained net will react when it has ambiguous data.  I posted a comment on the linked question.  I want to give the 'original answerer' time to respond and then I will, depending on what he says, add more.
Comment: Thanks for the explanation and H2O discussion, this is pretty much what I was looking for and gives me some things to think about when using H2O.


Point to a variable owned by a different class in C#

Question: In the below code, I am hoping to end up with both classA and Main to both thing that Main's name is "GoodBye" However, in the end, classA still things Main's name is 'Hello' while Main know's it's name has been changed to 'GoodBye'. How to I allow classA to see that Main's name has been changed to GoodBye?
Is this not allowed in C# and is deemed as unsafe and therefore you have to wrap the code inside of 'unsafe' blocks?
After testing I understand that using the reference allows classA to modify Main's name but doens't allow classA to see when Main's name has been changed.
class Program
{
    static void Main(string[] args)
    {
        string MyName = "Hello";

        Console.WriteLine($"Main thinks Main's name is: {MyName}");
        Console.WriteLine($"");

        Console.WriteLine($"Creating ClassA");
        ClassA classA = new ClassA(ref MyName);

        Console.WriteLine($"Main thinks Main's name is: {MyName}");
        classA.WriteOutMainName();

        MyName = "GoodBye";
        Console.WriteLine($"");
        Console.WriteLine($"Main changed it's name to: {MyName}");
        Console.WriteLine($"");

        Console.WriteLine($"Main thinks Main's name is: {MyName}");
        classA.WriteOutMainName();

    }
}

public class ClassA
{
    string MainName;
    public ClassA(ref string mainName)
    {
        MainName = mainName;
        mainName = "Dog";
    }

    public void WriteOutMainName()
    {
        Console.WriteLine($"ClassA thinks Main's name is: {MainName}");
    }
}

The output for the above is the following:

Main thinks Main's name is: hello

Creating ClassA
Main thinks Main's name is: hello
ClassA thinks Main's name is: hello

Main changed it's name to: GoodBye

Main thinks Main's name is: GoodBye
ClassA thinks Main's name is: hello
Press any key to continue . . .

Comment: Make a class, give it a `Name` property, then pass an instance of that class to your `ClassA`.  Voila, `ClassA` now has a reference to the owner of the Name, and can get its name at any time.
Comment: Hi Amy, that worked great. If you restate your comment as an answer I can mark this as the solution. Thanks.
Comment: Feel free to answer it. You've already done so in your question, just move that into an answer and you're done.
Comment: Possible duplicate of [How do I assign by "reference" to a class field in c#?](https://stackoverflow.com/questions/2980463/how-do-i-assign-by-reference-to-a-class-field-in-c)
Answer: Update after testing Amy's statement above:
The below works as desired:
class Program
{
    static void Main(string[] args)
    {
        //string MyName = "Hello";

        NameHolder MyName = new NameHolder();
        MyName.Name = "hello";

        Console.WriteLine($"Main thinks Main's name is: {MyName.Name}");
        Console.WriteLine($"");

        Console.WriteLine($"Creating ClassA");
        ClassA classA = new ClassA(MyName);

        Console.WriteLine($"Main thinks Main's name is: {MyName.Name}");
        classA.WriteOutMainName();

        MyName.Name = "GoodBye";
        Console.WriteLine($"");
        Console.WriteLine($"Main changed it's name to: {MyName.Name}");
        Console.WriteLine($"");

        Console.WriteLine($"Main thinks Main's name is: {MyName.Name}");
        classA.WriteOutMainName();

    }
}

public class ClassA
{
    NameHolder MainName;
    public ClassA(NameHolder mainName)
    {
        MainName = mainName;
    }

    public void WriteOutMainName()
    {
        Console.WriteLine($"ClassA thinks Main's name is: {MainName.Name}");
    }
}

public class NameHolder
{
    private string name = "";
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }
}

The output is the following:

Main thinks Main's name is: hello

Creating ClassA

Main thinks Main's name is: hello
ClassA thinks Main's name is: hello

Main changed it's name to: GoodBye

Main thinks Main's name is: GoodBye
ClassA thinks Main's name is: GoodBye
Press any key to continue . . .

EDIT 3:
you can simplify your NameHolder class if you are not doing anything special/extra within the get and set blocks:
public class NameHolder
{
    public string Name {get; set;}
}


Java highscore list

Question: So i have a problem with my final projekt in school. I'm creating a game with a highscore list but i need to have ten people to show up, and i curently only have 5 people and i do not have a single clue how to fix it.
import java.util.*;
    import java.io.*;

    public class HighscoreManager {
        // Sparar alla poäng i filen "scores.dat", filen skapas här
        private static final String HIGHSCORE_FILE = "scores.dat";

        // En arraylista med typen score
        ArrayList<Score> scores;

        // Startar en in och en utström för att skicka och ladda in information från
        // filen
        ObjectOutputStream outputStream = null;
        ObjectInputStream inputStream = null;

        public HighscoreManager() {
            // För att kunna arbeta med listan behövs en variabel dvs. Arraylistan
            // scores
            scores = new ArrayList<Score>();
            // Det är med classen Arraylist<score> getScores() som arrayen skapas
            // forts
        }

        public ArrayList<Score> getScores() {
            // ...forts-> och här används loadScoreFil() och sort(), returnerar
            // värdet scores som används i HighscoreManager
            loadScoreFile();
            sort();
            return scores;
        }

        private void sort() {
            // Här blir det komplicerat, kort och gott så skapas "comparator", ifrån
            // ScoreComparator.java, den sorterar scores med hjälp av just
            // collections(från java).sort(x,y)
            ScoreComparator comparator = new ScoreComparator();
            Collections.sort(scores, comparator);
        }

        public void addScore(String name, int score) {

            loadScoreFile();
            scores.add(new Score(name, score));
            updateScoreFile();
        }

        public void loadScoreFile() {
            //Se updateScoreFile
            try {
                inputStream = new ObjectInputStream(new FileInputStream(
                        HIGHSCORE_FILE));
                scores = (ArrayList<Score>) inputStream.readObject();
            } catch (FileNotFoundException e) {
                System.out.println("");
            } catch (IOException e) {
                System.out.println("io problem: " + e.getMessage());
            } catch (ClassNotFoundException e) {
                System.out.println("cnf problem: " + e.getMessage());
            } finally {
                try {
                    if (outputStream != null) {
                        outputStream.flush();
                        outputStream.close();
                    }
                } catch (IOException e) {
                    System.out.println("io problem: " + e.getMessage());
                }
            }
        }

        public void updateScoreFile() {
            try {
                // Med serializerade Score.java så används outputStream för att
                // skicka till HIGHSCORE_FILE som är scores.dat

                outputStream = new ObjectOutputStream(new FileOutputStream(
                        HIGHSCORE_FILE));
                outputStream.writeObject(scores);
            }
            // resten är catch som kan göras om för att visa felmeddelanden, jag
            // gjorde bara en basic.
            catch (FileNotFoundException e) {
                System.out.println("File Not Found, run the program again to fix.");
            } catch (IOException e) {
                System.out.println("IO problem, run the program again to fix.");
            }

            finally {
                try {
                    if (outputStream != null) {
                        outputStream.flush();
                        outputStream.close();
                        // Detta displayar poängen så länge det finns något att
                        // displaya
                    }
                } catch (IOException e) {
                    System.out
                            .println("Update problem, run the program again to fix.");
                }
            }
        }

        public String getHighscoreString() {
            // Här skapas själva texten som ska visas, nästan slutstation för
            // highscoret

            // Skapar Stringen highscoreString och sätter en maxgräns för hur många
            // highscores som ska vara med.
            String highscoreString = "";
            int max = 10;

            // Sammanställer poängen
            ArrayList<Score> scores;
            scores = getScores();

            // while loop som printar ut ett highscore i taget, när i < max så
            // skrivs scores1,scores2 etc ut och plussar
            // på i och processen repeteras tills i = x, då returneras värdet som är
            // highscoreString och kan användas i tamain.
            int i = 0;
            int x = scores.size();
            if (x > max) {
                x = max;
            }

            while (i < x) {
                highscoreString += (i + 1) + ".\t" + scores.get(i).getNamn() + "  "
                        + scores.get(i).getScore() + "\n";
                i++;
            }

            return highscoreString;
        }
    }

Comment: Where in your code do you have the top 5 scores? A simple algorithm to get the top N scores: Geth the first N scores, sort them. Take the next score, if it's bigger than the smallest one, remove the smallest one and insert the new score at th right place. And loop till the end.
Answer: I made a JUnit test for your code and tried writing ten scores to the scores.dat file. I then tried reading the scores.dat file and the list of scores contained 10 scores. Your code seems to be working.
Do you have more than five scores in the scores.dat file ?
I ran the following test:
public class HighscoreManagerTest {
    @Test
    public void shouldReturnTenHighscores(){
        HighscoreManager highscoreManager = new HighscoreManager();
        highscoreManager.addScore("Player 2", 998);
        highscoreManager.addScore("Player 1", 999);
        highscoreManager.addScore("Player 5", 995);
        highscoreManager.addScore("Player 3", 997);
        highscoreManager.addScore("Player 4", 996);
        highscoreManager.addScore("Player 6", 994);
        highscoreManager.addScore("Player 7", 993);
        highscoreManager.addScore("Player 8", 992);
        highscoreManager.addScore("Player 9", 991);
        highscoreManager.addScore("Player 10", 990);
        ArrayList<Score> scores = highscoreManager.getScores();
        System.out.println( highscoreManager.getHighscoreString() );
        Assert.assertEquals( 10 , scores.size() );
    }
}

Java highscore list


uniform distribution, showing that two variables are uncorrelated and not independent

Question: Let (X,Y) have uniform distribution on the four points(0,1),(0,−1),(1,0),(−1,0). How can I show that X and Y are uncorrelated but not independent? Could someone just point me in the right direction for this problem. I don't know how to start here
Comment: When you say "Uniform", you mean that each variable is Uniform, or that the four points you mention have equal probability? Or both?
Comment: I'm pretty sure it's both, but this is all i was given, so I can't be too sure
Comment: No it is not both, it is a statement abut the four points only. Check my answer, I think I gave you enough to go on.
Comment: You should add the `self-study` tag if this is some sort of homework.
Answer: This is a clever study in contingency tables. Build the contingency table.
Each variable has support $\{-1,0,1,\}$, so you need to draw a $3 \times 3$ table. Put inside the probabilities you are given, and fill the other cells too, so that the whole satisfies the laws of probability. Remember, or read about, how from the contingency table that holds the joint probabilities we can get the marginal probabilities (hint: it is from here that they acquired the adjective "marginal"). Then apply the advise given to you by the other answer, to show what you are asked to show.
Answer: For uncorrelatedness, you just calculate $E[XY]-E[X]E[Y]$, which can be calculated via simple double sum. For example, the first term can be calculated as:
$$E[XY]=\sum_x\sum_y xy p_{XY}(x,y)\rightarrow 0$$
For independence, try to find $x_0,y_0$ such that $p_{XY}(x_0,y_0)\neq p_{X}(x_0)p_Y(y_0)$. For example, $y_0$ can be $0$.
Comment: So E[X] = Sum x to infinity of xP_x(x)-> 0 ?
Comment: Yes, expectations are $0$ as well.
Comment: So, are we saying that E[XY]−E[X]E[Y] just = 0? does that just imply that its uncorrelated in itself? Also, I'm confused about the independence statement... how do set up something to find that?
Comment: It's the definition of covariance, if covariance is $0$ RVs are uncorrelated. For independence, put some values for $x_0,y_0$ (i.e. -1,0,1) and see if the inequality holds for a case.
Comment: i understand that, but how do i just solve for pXY(x0,y0) or pXY(x0,y0)≠pX(x0)pY(y0) by plugging in?
Comment: You'll just see that rhs is not equal to lhs, you'll not solve anything. For example, what is p(x0) when x0 is 0?
Comment: wouldn't both sides be zero here though?
Comment: there are cases when only one side is $0$. Just give it a try, there are 9 cases in total.


Remove drum track from song given the isolated drum track and the song?

Question: I have a song for which I have the song itself, and an isolated drum track from a live performance that fits perfectly in time with the original. Given these 2 things, is there software that can do a decent job at removing the drum track or at least lowering it? I would really like to make a remix of it, but I need the drum track to be lowered.
I do not care if the software is pricy, free, etc,
If there is another way to remove drum tracks from songs I am open to that too.
Thanks
Comment: Music SE site definition: "...Topics include practice & performance, composition, technique, theory, and history." This question doesn't seem to fit in any of the following categories. The Audio-Visual SE site seems best for this question.
Comment: I agree with @Luke - this would be on topic for AVP
Answer: You'll probably get a better answer over at AVP, but an approach that might work -- depending on how similar your drum-only track really is to the drum track in the mix -- is wave cancellation.  The program I use for music editing is Amadeus Pro ($40 last I checked), though I assume Audacity (free) does this too.  You select the pure track, then overlay it on the section you want to edit it out of (correct alignment is essential), and use the wave-cancel function (might be called "cancel" or "filter").  I've used this approach to remove hum from tracks that started out on audio cassettes, and I've used the original track to remove a track from a mix, but I've never tried using an irregular external source.  The results won't be as good, but this might help.
Comment: Wave cancellation was going to be my suggestion; given a drum track and the album cut, you can reverse the waveform of the drum track and apply that to the original cut to cancel out the drums. But exactly as you say, if the drum track isn't the ORIGINAL track, then this will have decidedly less-than-ideal results; even if they sound the same to the listener, the waveform won't match 100%.
Answer: No chance. If you had a clean Drum Track and the mix from that Drum-Track, you could subtract it by mixing it with the inverted signal, but if it is not the exactly same waveform from the mix or has ambience (live…), there is no chance to do that except for perhaps a lowering of really low frequencies at the cost of arbitrary pumping.
Answer: It might be possible if you had the actual drum track used in the mix, but a similar track isn't going to work.  There are other issues but that's the main one in this case.  Your best bet is to use an FFT filter or similar to remove frequencies used by the drums, though this may affect the other sounds as well.  If you have more questions about that I'd encourage you to ask it on the Audio-Video site, as they're the experts in this area.


Terms for 4, 8 and 16 bar sections?

Question: Are there specific musical terms describing time structure like 4 bar sections, 8 bar sections, 16 etc.?
Comment: They are specified as 4 bar sections, 8 bar sections, etc.
Comment: How about "phrases"?
Comment: Perhaps "chorus" .
Answer: You would just refer to it as a section, if it is a complete section, or by the name of the section if you are trying to communicate the part of the song.
For example, "take it from the bridge" (sometimes called the "middle 8" or "middle 16") or "take it from the 4 bars before the 2nd chorus".
Jazz musicians might call the main part of the song the "head".
Someone might refer to a small section of a song by the word "passage".
But in general if you are not referring to a specific section of a song you can say the first 8, 16, 32, bars starting from x.
In a band setting you are almost always going to refer to a named section of the song and if you don't want to start at the top of that section you would say x bars before or after that section starts.
In a orchestral setting you will have section markings as well as bar markings. So rehearsal marking B or bar 112.

Terms for 4, 8 and 16 bar sections?


Not recognising hyphen on split

Question: I'm working with about 24k text files and am splitting some lines on '-'. It works for some files, however it fails to split for some other files.
company_participants is a list with N >= 1 elements, with each element consisting of a name followed by a hyphen ("-"), followed by the job title. To get the names, I use:
names_participants = [name.split('-')[0].strip() for name in company_participants]

On closer inspection, I found that it does not recognise "-" as "-" for some reason.
For example, the first element in company_participants is "robert isom - president"
Calling company_participants[0].split()[2] returns "-" since I've split on whitespace, and the hyphen is the third element (index 2).
When I then run a boolean on whether this is equal to "-", I get False.
company_participants[0].split()[2] == "-"  # Item at index 2 is the hyphen
# Output = False

Any idea what's going on here? Is there something else that looks like a hyphen but isn't one?
Many thanks!
Comment: Could by `binary encoded strings`: `b'-' != '-'`
Comment: `company_participants[0].split()[2] == b"-"` still returns False unfortunately. I also double checked the type (`type(company_participants[0].split()[2])`) and that returns `str`
Comment: I would suggest you to check type of `company_participants_full[0].split()[2]`.
Comment: Checked and that's a str. But I seem to have found the solution; posting now!
Answer: So I found that this has actually been answered elsewhere on StackOverflow.
Apparently I'm dealing with a "dash" and not a "hyphen"; couldn't see the difference with me naked eyes but when I copied the symbol from here, then it recognised it such that company_participants[0].split()[2] == "–" returned True.
#textDataProblems
#didNotSeeThatComing
Thank you StackOverflow!

Not recognising hyphen on split


Give an example in which $(A + B)^2 \neq A^2 + 2AB + B^2$

Question: Other provided information: 'Let A and B be square matrices of the same size.'
The last question for my Linear Algebra assignment involves providing matrices $A$ and $B$ that satisfy the above requirement, as well as 'Give a valid identity for $(A + B)^2$' and I do not see any theorem or example from my text that helps me break this problem down. So far I have been guessing random matrices to no avail and I know there must be a methodical solution to this.
What do I need to know in order to approach this problem?
Comment: Guessing random size $2$ matrices should work. - good strategy is to make matrices out of $1$s and $0$s.
Comment: Essentially anything where multiplication is not commutative,e.g.. matrices.
Comment: You need to include dollar signs (\$) to make MathJax do its work.
Comment: See here:http://math.stackexchange.com/questions/699505/for-all-square-matrices-a-and-b-of-the-same-size-it-is-true-that-ab2?rq=1
Answer: You also ask what a valid identity for $(A+B)^2$ is. Because you asked:

What do I need to know in order to approach this problem?

I will first outline a few basic facts about matrix multiplication, which will help you.

In general, matrix multiplication is not commutative. That means that $AB \neq BA$. They can be equal, but they are not equal in general.
Matrix multiplication is distributive, so $(A+B)C=AC+BC$.

If the last applied three times on $(A+B)^2$, you will get the correct identity.
Answer: take A = $\pmatrix{0 & -1\\-1 & 0}$ and B = $\pmatrix{1 & 0\\0 & 0}$. Basic idea is to provide A and B such that AB $\neq$BA
Comment: `\pmatrix{ a & b \\ c & d }` gives $\pmatrix{a & b \\ c & d}$
Comment: You need to include dollar signs ($) to make MathJax (the formulas) do its work
Comment: yes!...Thanks for the help


Bounds of integration for PDF

Question: Given the PDF $\ f_{XY}(x,y)=8xy $
$\ 0<x<1 $
$\ 0<y<x $
Find $\ f_Z(z) $
Where
$\ Z=\frac{Y}{X}$

I want to calculate $\ F_Z(z) $ so that I can derive it and find $\ f_Z(z) $.
$F_{Z}(z)=  \iint_{\frac{Y}{X}<z} (8xy)dydx = P(Z<z) $
How do I find the bounds for the integration?
Comment: Draw the region described  by the two conditions on x and y, it helps you
Answer: First, by definition, since $X\ge0$:
$$F_Z(z)=P(Z<z)=P(Y<zX)=\iint_{y<zx} 8xy\ dydx.$$
Look at the following figure now:

It is easy to see that
$$\iint_{y<zx} 8xy\ dydx=
\begin{cases}
8\int_0^1x\int_0^{zx}y\ dy\ dx,& \text{ if } z<1\\
1,& \text{ if } z\ge1
\end{cases}=$$
$$=
\begin{cases}
4\int_0^1x[y^2]_0^{zx}\ dx,& \text{ if } z<1\\
1,& \text{ if } z\ge1
\end{cases}
=$$
$$=
\begin{cases}
4z^2\int_0^1x^3\ dx=z^2,& \text{ if } z<1\\
1,& \text{ if } z\ge1
\end{cases}
.$$
(And, of course, the result is $0$ if $z<0$.)

Bounds of integration for PDF


Proof of a simple inequality used in a paper

Question: In a proof I'm trying to understand the following lemma was used:

If $x, y \in \mathbb{C}$, with $|y|<|x|$ and $|\operatorname{arg}(x) - \operatorname{arg}(y)| < \frac{\pi}{3n}$ for a positive integer $n$, then the inequality $|x-y| \leq |x|$ holds.

I tried to apply the cosine law, but didn't get a proof of this little lemma. Maybe one of you has an instant idea that works.
Comment: The appearance of $n$ in the denominator on the right hand side is mysterious- does it appear any where else in the lemma?
Comment: It's the degree of a nonconstant polynomial. I also think that it's value doesn't matter, so maybe $n$ should be $1$.
Comment: It seems to be a geometric property of triangles: If we consider one corner of a triangle, then the (in some cases more) point on the opposite edge of the corner with the largest distance is one of the remaining corners. For our problem, if we take the intersection point of the ray from the origin crossing $y$ and the circle with radius |x| around the origin, then this point forms with the origin and $x$ a triangle. We than have to consider one of the corners differing from $0$. If the angle at the origin is $< \frac{\pi}{3}$, then the two radial edges are the longest and hence, $|x-y|<|x|$.
Comment: This should be a geometric explanation of the proof below.
Answer: If $|y| = 0 $, note that $|x-y| = |y|$. Let $ |y| >0 $ and $\phi = |arg(x) - arg(y)| < \frac{\pi}{3} $.
From Law of cosines, it follows that
$$ \frac{|x|^2 + |y|^2 - |x-y|^2}{2|x||y|} = \cos \phi > \frac{1}{2} \implies |x|^2 - |x-y|^2 >|y|(|x| - |y|). $$
Since $ |x|>|y|$, we conclude that $ |x| > |x-y|$.
Comment: OK, this is a very simple proof. Thank you. :-)


What is "{method 'recv_into' of '_socket.socket' objects}" at Cprofile result? How can I reduce its time consumption?

Question: This is the profiling result of my python code.
As you can see below, method 'recv_into' of '_socket.socket' objects takes too much time ( 17.265 as tottime )
What is it? And is there any way to reduce its time?
When is it called?
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.402    0.402   37.668   37.668 c:\Users\user\Google ����̺�\Business\Project\Jessica Project\jessica-1\simulation\simulatorW.py:239(backtestWithArgumentsList)
        1    0.173    0.173   26.762   26.762 c:\Users\user\Google ����̺�\Business\Project\Jessica Project\jessica-1\simulation\simulatorW.py:110(getPrices)
        1    0.000    0.000   26.588   26.588 c:\Users\user\Google ����̺�\Business\Project\Jessica Project\jessica-1\dto\__init__.py:5(__init__)
        1    1.734    1.734   25.380   25.380 c:\Users\user\Google ����̺�\Business\Project\Jessica Project\jessica-1\dto\__init__.py:21(priceInfoListToDeque)
   815679    2.204    0.000   23.473    0.000 C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\pymongo\cursor.py:1152(next)
       13    0.021    0.002   20.631    1.587 C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\pymongo\cursor.py:1039(_refresh)
       12    0.008    0.001   20.609    1.717 C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\pymongo\cursor.py:937(__send_message)
       12    0.000    0.000   20.601    1.717 C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\pymongo\mongo_client.py:1306(_run_operation_with_response)
       12    0.000    0.000   20.601    1.717 C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\pymongo\mongo_client.py:1437(_retryable_read)
       12    0.000    0.000   20.597    1.716 C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\pymongo\mongo_client.py:1334(_cmd)
       12    0.001    0.000   20.597    1.716 C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\pymongo\server.py:70(run_operation_with_response)
       18    0.001    0.000   17.386    0.966 C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\pymongo\network.py:192(receive_message)
       12    0.013    0.001   17.379    1.448 C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\pymongo\pool.py:637(receive_message)
       36    0.066    0.002   17.331    0.481 C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\pymongo\network.py:249(_receive_data_on_socket)
    19984   17.265    0.001   17.265    0.001 {method 'recv_into' of '_socket.socket' objects}
        1    2.499    2.499    6.522    6.522 c:\Users\user\Google ����̺�\Business\Project\Jessica Project\jessica-1\simulation\simulatorW.py:138(filterIndicesWithTimeCondition)

Comment: Can you share a functional example of your code?
Comment: please share an example of your code
Comment: Reduce calls 19984
Answer: It's a low level networking call. This is the time spent reading whatever you are loading. Take a look at its callers.
p.print_callers("{method 'recv_into' of '_socket.socket' objects}")

Keep going up the callers tree picking the ones that have longer times. Remember that the restriction is a regexp. Use escapes when necessary:
p.sort_stats("tottime").print_callers("api.py:104\(post\)")

The top 4 lines are more interesting than the recv_into one. If you go up the caller tree, you're likely to end up in one of those. There could be many ways to optimize those, since no details are provided. Cacheing, compressing, getting only what you need, and otherwise reducing network footprint.


input from a user list-comprehensions

Question: Why first line create a lists of list and second a normal list? How can I create a normal list using comprehensions?
list45 = [input("first line: ").split()]
words2 = input("second line: ")
list46 = words2.split()
print(list45)
print(list46)

Output:
first line: my first line
second line: my second line
[['my', 'first', 'line']]
['my', 'second', 'line']

Answer: list45 = [input("Second line: ").split()]

This takes an input, splits the string, the output of which is a list, and puts it inside of another list. This is because you have square brackets surrounding it, which denote a list.
list46 = words2.split()

This just returns the result of the split function, which is a list.

input from a user list-comprehensions


Generate 1D bar code based on user defined input

Question: My requirement is to generate 1D bar code based on user defined input in the text box.
 for example if user give 10 as input in the text box then he should able to generate 10 bar code with different serial number after saving the content ....
so far, I have used bar code module to generate the bar code and serial module for different serial number and I am able to generate single bar code on saving the content.

now my problem is how to generate series of bar code based on the number which we give as input.
I am not getting how to map the input text box field to content and call multiple time.can any one help on this.
Comment: @Pierre.Vriens: Is there any way to achieve this I have tried a lot but  I am unable to crack this.
Comment: Thx for the edit, a bit better ... But what's the difference between a "1D" barcode and a "QR code"? Or could a "QR code" also work for you? After I correctly understand the question, I might have a suggestion (answer) for you. PS: please enhance the FORMAT of your question also. E.g. by adding some line breaks in between, and moving the image to a better spot.
Comment: I think more information is needed , like how the content is selected , do you want to edit content too? If you just want a reference to a node maybe reference module would handle the job
Comment: yes I do want to edit the content,actually my requirement is to generate the bar code based on user input but I have tried with all possible way I dint get any single solution so thought of invoking the content to the form and i really don't know will it works or not either.if you know the solution please help me out. thanks in advance
Comment: @  Pierre.Vriens : I have a requirement to generate 1D barcode, QR code is for 2D barcode.
Comment: sorry but still there is not enough info , please explain more so other can make a imagination about your problem so they can help , tell about your content types , tell about the form(is it a content creation or edition form or it is a custom form or ...) , make an omagination of scenario plz
Comment: I have created content type with bar code and serial fields,if i save the content I am able to generate the single bar code with unique id.now i want to generate the multiple bar code based on the number we give as input.
Comment: There is a module [Barcode](https://www.drupal.org/project/barcode) that can help you do that. > The Barcode module provides a text field to generate barcode images.
Comment: Yes but  Iam able to generate only one barcode using this module, my requirement is to generate series of barcode.
Answer: You have content of type A and it contains a field called B , B is your serial fieldneedrst of all your field B must get multiple values , this can be set through Field UI. If you are going to collect more than 10 values per each content you can select unlimited values , but this has some DB overhead , after that drupal itself will create the form with multiple value , if you have chosen a specific number for that field for example 3 values , there will be 3 widget to get values , else if it is unlimited you will have an ajax add element button .
If this is not what you want and you want to enter the number of barcodes at the beggining then you must alter the form using hook_form_alter (you must write a module) , inside that hook add a counter element to your form (it gets the repeatation s count) and then use ajax to add widget to your form .
Each widget must be added to your field in your form , so drupal will understand that all the data inserted in these widgets relates to your field.
If you want to do it by writing a module and need some code ,I can add some code later.
Comment: Sorry if i can't tell you all about drupal module development cause it is too time consuming , there are some good tutorial on that off course , but if you are eager to write a module for that I recommend to learn about [how to create .info file](https://www.drupal.org/node/542202) and [how to alter a form](https://api.drupal.org/api/function/hook_form_alter/7) and finally [Using ajax in drupal](https://www.drupal.org/node/752056) , they are all you need to achieve your goal in this problem .
Comment: Thanks for your response and It Means a lot for me. Problem with me is I am new to this drupal I am not getting What exactly I have to do,and as of now writing a module is very difficult for me. To achieve this requirement Example: assume a user enters 20 as input in a text field and then clicks the save button. It should then generate a 1D barcode for '20'. If however the input gets changed to '50', it should generate a 1D barcode for '50' (with different serial number).is there any way to achieve this please let me know I am trying to figure it out from past many days.
Comment: Thank a lot, I will go through the tutorial which you have suggested.
Answer: I tried Barcode module and there were no problem for having multiple values , so I recommend you to use this module , off course it is not exactly what you wanted but it can do the job ,

first create a test content type using Structure
add a new field using manage fields to this content
add a field of type Barcode
in the first setting screen choose any type of barcode you wish
in second screen down below you can see Number of values
change it to Unlimited
save and try adding new content


There is scrollview in LinearLayout in which I inserted a GridView to LinearLayout

Question: good afternoon,Today i want a gridView scroll in scrollview.
Perhaps like this:
There are LinearLayout in scrollview,Then I inserted a GridView to LinearLayout.
Question gridView is displaying only one item.
My xml:
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
 <ScrollView
    android:id="@+id/record_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="10dip">

    <LinearLayout
        android:id="@+id/record_Layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>

</RelativeLayout>

My java code:
public class MainActivity extends Activity {
private LinearLayout linearLayout = null;
private ScrollView scrollView =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    linearLayout = (LinearLayout)findViewById(R.id.record_Layout);
    scrollView = (ScrollView)findViewById(R.id.record_scrollview);
    GridView gd = new GridView(this);
    List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("name", "huangli1");
    list.add(map);
    map = new HashMap<String, String>();
    map.put("name", "huangli2");
    list.add(map);
    map = new HashMap<String, String>();
    map.put("name", "huangli3");
    list.add(map);
    SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.list_item, new String[]{"name"}, new int[]{R.id.tv}){

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            return super.getView(position, convertView, parent);
        }

    };
    gd.setAdapter(simpleAdapter);
    linearLayout.addView(gd);
}

Comment: trying to add gridview inside a scrollview?
Comment: You have to specify a layout width rather then match_parent. like  300dp or else in your child layout of scrollview
Answer: All you need to do is just make a custom scroll view as shown below: (The name of this class is MitsScrollView.java)
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class MitsScrollView extends ScrollView
{

public MitsScrollView(Context context)
{
    super(context);
}

public MitsScrollView(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public MitsScrollView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
    final int action = ev.getAction();
    switch (action)
    {
        case MotionEvent.ACTION_DOWN:
            Log.i("VerticalScrollview", "onInterceptTouchEvent: DOWN super false");
            super.onTouchEvent(ev);
            break;

        case MotionEvent.ACTION_MOVE:
            return false; // redirect MotionEvents to ourself

        case MotionEvent.ACTION_CANCEL:
            Log.i("VerticalScrollview", "onInterceptTouchEvent: CANCEL super false");
            super.onTouchEvent(ev);
            break;

        case MotionEvent.ACTION_UP:
            Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false");
            return false;

        default:
            Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action);
            break;
    }

    return false;
}

@Override
public boolean onTouchEvent(MotionEvent ev)
{
    super.onTouchEvent(ev);
    Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction());
    return true;
}
}

And in you xml you need to put the linear layout and grid view inside this custom scroll view:
<your package name.MitsScrollView
    android:id="@+id/scrollViewOther"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#c4c4c4"
    android:descendantFocusability="blocksDescendants"
    android:fadeScrollbars="false"
    android:fadingEdge="horizontal"
    tools:ignore="ScrollViewCount" >

    here you can put your linear layout or grid view or any views

</your package name.MitsScrollView>

Please note your package name indicates the package in which you have put the above custom scroll view class.
Answer: It is not good practice to put GridView in ScrollView. But you can do it though it is bit painful.
Now, here is how you can do this. Check the answer here. It is an expandable height GridView, which you will want to import / create in your project. What that basically means is that as more items are added to the GridView, it will just expand its height, as opposed to keeping its height set and using scrolling. This is exactly what you want.
Once you have the ExpandableHeightGridView in your project, go to your XML layout where you want the GridView to be. You can then do something like this (paraphrasing):
<ScrollView ...>
    <RelativeLayout ...>
        <com.example.ExpandableHeightGridView ... />
        <other view items />
    </RelativeLayout>
</ScrollView>

Then, in your activity where you set the GridView's adapter, you want to make sure you set it to expand. So:
ExpandableHeightGridView gridView = (ExpandableHeightGridView) findViewById(R.id.myId);
gridView.setAdapter(yourAdapter);
gridView.setExpanded(true);

The reason you want this expandable GridView is because, the fact that a standard GridView doesn't expand is what causes it to scroll. It sticks to a certain height, and then as more items fill it past its view bounds, it becomes scrollable. Now, with this, your GridView will always expand its height to fit the content within it, thus never allowing it to enter its scrolling mode. This enables you to use it inside of the ScrollView and use other view elements above or below it within the ScrollView, and have them all scroll.


Executing stored procedures in Parallel

Question: I have 35 stored procedures that I need to run in parallel so as to reduce the execution time.
I came across concept of Agent jobs here.
The link suggests using this piece of code to achieve this:
CREATE PROCEDURE ExecuteSQL_ByAgentJob_usp(
    @SqlStatemet            VARCHAR(4000),
    @SPNameOrStmntTitle     VARCHAR(100),
    @JobRunningUser         VARCHAR(100) = NULL,
    @JobIdOut               UNIQUEIDENTIFIER OUTPUT
)
AS
BEGIN

    SET NOCOUNT ON;

    DECLARE @JobId          UNIQUEIDENTIFIER,
            @JobName        VARCHAR(250) = NULL,
            @DBName         VARCHAR(100) = DB_NAME(),
            @ServerName     VARCHAR(100) = @@SERVERNAME

    --Creating Unique Job Name by combining @SPNameOrStmntTitle and a GUID.
    SET @JobName = @SPNameOrStmntTitle + '_' + CONVERT(VARCHAR(64), NEWID())

    --Currently logged user name will be used to execute the job if not provided one.
    IF @JobRunningUser IS NULL
        SET @JobRunningUser = SUSER_NAME()

    --Adds a new job executed by the SQLServerAgent service
    EXECUTE msdb..sp_add_job @job_name = @JobName, @owner_login_name = @JobRunningUser,
    @job_id = @JobId OUTPUT

    --Targets the specified job at the specified server
    EXECUTE msdb..sp_add_jobserver @job_id = @JobId, @server_name = @ServerName

    --Tell job for its about its first step.
    EXECUTE msdb..sp_add_jobstep @job_id = @JobId, @step_name = 'Step1', @command
    = @SqlStatemet,@database_name = @DBName, @on_success_action = 3

    --Preparing the command to delete the job immediately after executing the statements
    DECLARE @sql VARCHAR(250) = 'execute msdb..sp_delete_job @job_name=''' + @JobName + ''''

    EXECUTE msdb..sp_add_jobstep @job_id = @JobId, @step_name = 'Step2', @command = @sql

    --Run the job
    EXECUTE msdb..sp_start_job @job_id = @JobId

    --Return the Job via output param.
    SET @JobIdOut = @JobId
END

Despite of having read this over and over, I still do not understand which part of it will help execute stored procedures in parallel? If possible, please shed some light on it. I am curious as hell to know which part of the script does this magic.
It is called like this :
SET @Itr = 1 --Seeting the initial value.
    SET @RecCount = (
            SELECT COUNT(*)
            FROM   @Scripts
        )

-----------------PART3------------------------------------
    WHILE (@Itr <= @RecCount)
    BEGIN
        SELECT @sql = t.Script
        FROM   @Scripts t
        WHERE  id = @Itr
        --Just o identify the script name getting first 10 char of the SP
        SET @ScriptTitle = LEFT(REPLACE(@sql, 'EXEC ', ''), 10)

        EXEC ExecuteSQL_ByAgentJob_usp
             @SqlStatemet = @sql,
             @SPNameOrStmntTitle = @ScriptTitle,
             @JobRunningUser = 'sa',
             @JobIdOut = @JobId OUTPUT

Is it how it is called in a loop ? But I believe next iteration of loop will start only when last is done executing, so how come it runs in parallel ?
Comment: sp_start_job just starts the job, it does not wait until it finishes, and that's the idea behind that script
Comment: @Simran, You might consider using Service Broker for this task instead of SQL Server agent.  That will provide more control over the number of concurrent proc executions that might otherwise overwhelm the server.
Answer: On a side note why would you want 35 procedures to execute parallel ? to me this requirement sounds a bit unrealistic.
Even if you execute two stored procedures exactly at the same time, It is not guaranteed that they will go parallel.
Parallelism of executions is dependent on other factors like Query Cost, MXDOP(Maximum Degree of Parallelism), Threshold for Parallelism etc.
These properties are manipulated on the server level configuration (except the MAXDOP which can be specified on query.
I cannot go into too much details but my suggestion would, do not depend on Parallelism write your code in a way that it doesnt depend on queries being parallel, Also the somewhat simultaneous execution of queries is handled via Transactions.
Just as a hint to have 35 procedures being executed parallel you would need 35 cores on your Sql Server and MAXDOP Set to 1 and then have them 35 procs being executed at the exact time. Seems a lot of unrealistic requirements to me :)
Comment: If you want to improve performance, running them in parallel is not necessarily going to help.If you have a resource bottleneck due to bad code this is definitely not going to help
Comment: All of them are not going to be executed in parallel. It's just that some of them in chunks will go on that way.
Comment: any mircrosoft docs link regarding parallel sp execution
Answer: You are right. Next iteration will start when the last is done. But here, you are running jobs in those iterations not the actual statements. So imagine it like sp_start_job is called in async manner. It will just start the job and return immediately. The job itself may continue to do it's steps.
Answer: The best, and easiest, way to do it is to create an SSIS project that has 35 Execute SQL tasks in parallel and then execute the job. The learning curve on using SSIS to do this is an hour or two and you can let SQL Server use as much resources as possible to execute the tasks as fast as possible. You don't have to mess around with maxops or anything else - SSIS will do it for you.
To see different ways to execute SSIS jobs try this link:
http://www.mssqltips.com/sqlservertip/1775/different-ways-to-execute-a-sql-server-ssis-package/
Comment: That sounds interesting. Could you please specify the download link to ssis ? I have been looking up for it but no luck.
Comment: Its a free tool (that comes with any paid edition of SQL Server) and its full name is SQL Server Integration Services. To create a package on SQL Server 2012 and above see: https://msdn.microsoft.com/en-us/library/ms169917.aspx
Comment: Oh, Mine is not a paid edition.I hope SQL server Agent services works without it ? I have been trying to use sql server agent but it is not starting either.
Comment: You need a paid edition of SQL Server to use the SQL Server Agent. EXPRESS doesn't let you

Executing stored procedures in Parallel


How to expand $\partial^2(f\circ g)$ symbolically?

Question: I know that if $f,h\in C^\infty(F,\Bbb R)$ and $g\in C^\infty(E,F)$ for arbitrary real Banach spaces $E$ and $F$, then $\partial f^k\in C^\infty(F,\mathcal L^k(F,\Bbb R))$ and $g^k\in C^\infty(E,\mathcal L^k(E,F))$. Also I have the product rule (for real valued-valued functions) and chain rule that, it reads as
$$\partial(fh)=f\partial h+h\partial f,\quad \partial(f\circ g)=(\partial f\circ g)\partial g$$
where the product above means distribution of variables and composition, that is
$$\partial(fg)(x)=f(x)\cdot\partial h(x)+\partial g(x)\cdot f(x),\quad \partial(f\circ g)(x)=(\partial f\circ g)(x)\cdot\partial g(x)$$
where "$\cdot$" is composition of linear maps. However when I tried to express the second derivative of a composition symbollically, following the above product rule (for real-valued vector functions) and the chain rule, I got
$$\partial^2(f\circ g)=\partial((\partial f\circ g)\cdot\partial g)$$
but I dont know exactly how to interpret here the dot to know if I can continue using the chain rule to expand the expression further. If a plug a variable then
$$\partial^2(f\circ g)(x)\overset{?}{=}\partial\big((\partial f\circ g)(x)\partial g(x)\big)$$
but I dont know if the expression on the RHS makes sense.
My question is: there is a way to get a symbolic expression for these derivatives using the chain rule further? Some help will be appreciated, thank you.

I know a generalized product rule that reads
$$\partial\varphi(f,g)=\varphi(\partial f,g)+\varphi(f,\partial g)$$ for any bilinear map $\varphi$, but I dont see how to apply it here.
Comment: You also need the rule for products.
Answer: I would think about this for $f,g: \mathbb{R} -> \mathbb{R}$ first.
If   $\partial ((f \circ g)(x)) = \partial (f'(g(x)) g'(x))$
Then let h = f' to make things easier to see and let's do this again
     $$
    \partial(f'(g(x)) g'(x)) = \partial[h(g(x)) g'(x)]
                            = \partial(h(g(x)) g'(x) + h(g(x)) g''(x)
 $$
where we have used product rule.  Finishing with chain rule
     $$
    \partial(f'(g(x)) g'(x)) = h'(g(x)) g'(x) g'(x) + h(g(x)) g''(x)
 $$
Substituting back in that $h = f'$ and therefore $h' = f''$ we have
     $$
   \partial^2(f(g(x)) = \partial[f'(g(x)) g'(x)] = f''(g(x)) (g'(x))^2 + f'(g(x)) g''(x)
 $$
Hope this helps.
Answer: Ok, I solved it... I didnt noticed that matrix multiplication is a bilinear map, lol, so I can apply the generalized product rule to it. Then we have that
$$\partial^2(f\circ g)(x)=\partial[(\partial f\circ g)(x)\cdot\partial g(x)]=\partial\varphi[(\partial f\circ g)(x),\partial g(x)]=\\=\varphi[(\partial^2 f\circ g)(x)\partial g(x),\partial g(x)]+\varphi[(\partial f\circ g)(x),\partial^2g(x)]=\\=\underbrace{(\partial^2 f\circ g)(x)}_{\in\mathcal L^2(F,\Bbb R)}\cdot\underbrace{[\partial g(x)]^2}_{\in\mathcal L(E, F)}+\underbrace{(\partial f\circ g)(x)}_{\in\mathcal L(F,\Bbb R)}\cdot\underbrace{\partial^2g(x)}_{\in\mathcal L^2(E,F)}$$
where $\varphi$ means "matrix multiplication" (assuming some matrix representation of the linear maps) and the above must be understood as a bilinear map from $E\to\Bbb R$ (with the power, as the dot, as composition of linear maps).
However, we must follow the order of differentiation to make meaningful how to plug values on the bilinear function, that is
$$\partial^2(f\circ g)(x)[v,h]=\partial(\partial(f\circ g)(x)v)h=\partial\big((\partial f\circ g)(x)\partial g(x)v\big)h=\\=\partial\big((\partial f\circ g)(x)D_v g(x)\big)h=(\partial^2 f\circ g)(x)[D_vg(x),D_hg(x)]+(\partial f\circ g)(x)D_hD_vg(x)$$


Initialize a new instance of the class through its method

Question: Got this class with lots of properties. There`s constructor which sets properties to its default values and Clear method. (Clear method here is just an example)
public class Person
{
    public string A;
    public string B;
    public string C;
    ...
    public string Z;

    public Person()
    {
        this.A = "Default value for A";
        this.B = "Default value for B";
        this.C = "Default value for C";
        ...
        this.Z = "Default value for Z";
    }

    public void Clear()
    {
        this = new Person(); // Something like this ???
    }
}

How can I reinitialize class through Clear method?
I mean:
Person p = new Person();

p.A = "Smething goes here for A";
p.B = "Smething goes here for B";
...
// Here do stuff with p
...

p.Clear(); // Here I would like to reinitialize p through the Clear() instead of use p = new Person();

I know I could write a function with all the default values settings and use it in constructor and in Clear method. But... is there a "proper" way instead of workarounds?
Comment: Do you have any specific reason, why are you trying reuse the instance?
Comment: avoid memory allocation I guess : which is quite a noble purpose :)
Answer: I'd rather implement initializer:
public class Person
{
    public string A;
    public string B;
    public string C;
    ...
    public string Z;

    private void Ininialize() {
      this.A = "Default value for A";
      this.B = "Default value for B";
      this.C = "Default value for C";
      ...
      this.Z = "Default value for Z";
    }

    public Person()
    {
       Ininialize();
    }

    public void Clear()
    {
       Ininialize();
    }
}

....
Person p = new Person();
...
p.A = "Something goes here for A";
p.B = "Something goes here for B";
...
p.Clear(); // <- return A, B..Z properties to their default values

Comment: Yes... or put Initialize code into Clear method and directly call Clear from constructor : you would avoid one function (which is not a big deal, I agree)
Comment: @Kek: I'd rather separate Initialize() and Clear(): often you have to do something additional on Clear(): e.g. ask a permission, invoke an event(s), say "Clearing" and "Cleared" etc.
Comment: @Dmitry Thanks for anwser. Basically if I rename Initialize to **SomethingElse** then it`s just what i said earlier :) I could write function and use it in constructor and Clear method. I was just **curious** if there`s "intended way"; like **this** in parameters or so..
Comment: @majk86: In good old times (Delphi, C++), when "this" was just a pointer you could have done some pretty tricks with it; but now in managed .Net environment we can't.
Answer: I don't know what you want , but I would do it like this :
   public class Person
{
    public string A;
    public string B;
    public string C;
    ...
    public string Z;

    public Person()
    {
        ResetToDefault();
    }

    public void ResetToDefault()
    {
        this.A = "Default value for A";
        this.B = "Default value for B";
        this.C = "Default value for C";
        ...
        this.Z = "Default value for Z";
    }
}

well , at some point you must give the parameters their values.
When you want to reset it to default.. just do this :
Person person = new Person();
//do your stuff here .....
//when reset it:
person.ResetToDefault();

Comment: Basically same as Dmitry wrote.. but thanks anyway :)


Find a quotient map $f:(0,1) \rightarrow [0,1]$ where the intervals $(0,1)$ and $[0,1]$ are in $\mathbb{R}$ and endowed with the subspace topology.

Question: Find a quotient map $f:(0,1) \rightarrow  [0,1]$ where the intervals $(0,1)$ and $[0,1]$ are in $\mathbb{R}$ and endowed with the subspace topology.
I am really not to sure where to start. I know that quotient map must be continuous and surjective. So, it makes sense to show the following
$$\forall\ U \in [0,1], f^{-1}(U) \in (0,1)$$ with $f^{-1}(U)$ open.
Case 1:
$U \in (0,1)$ then $f^{-1}(U) \in (0,1)$ and $f(f^{-1}(U))=U$ and $0,1 \notin (0,1)$
Case 2:
Let $f^{-1}(0)=f^{-1}(1)=  \frac{1}{2}$ so that $U_1=[\frac{1}{2}, 1-\epsilon)$ and $U_2=(0+\epsilon,\frac{1}{2}]$ and $f^{-1}(U_1 \cup U_2)$ and then $$f(f^{-1}(U_1 \cup U_2))=(0,1)$$.
Am I on the right track? Also do I need to show what the equivalence class that I created is? If so, how do I show that?
Answer: Define $f: (0,1) \longrightarrow [0,1]$ as follows
$$f(x)= \left\{\begin{matrix}
0 & \mbox{if }  & 0<x < \frac{1}{3} \\
3x-1 & \mbox{if } &\frac{1}{3} \leq x \le \frac{2}{3} \\
1 & \mbox{if} & \frac{2}{3} < x < 1
\end{matrix} \right.
$$
Comment: @Zeta10 This quotient map (you can see that it is surjective and that it is continuous) identifies $(0, \frac{1}{3}]$ with $\{ \frac{1}{3} \}$ and identifies  $[\frac{2}{3}, 1)$ with $\{ \frac{2}{3} \}$. So, $(0,1)$ is somehow shrinked to $[\frac{1}{3}, \frac{2}{3}]$, which is homeomorphic to $[0,1]$.
Comment: Is there any reason why the function is defined as 1 for x greater than two thirds ?
Comment: @Crostul How do I show that the continuous onto function $f(x)$ is indeed a quotient map?
Comment: @morphic It's very easy to see this: take any set of $[0,1]$ whose preimage is open: show that this must be open.


android:layout_anchor did not work properly in recyclerview'item .

Question:
What is the issue?
: In above picture , you can see 3rd item where the floating icon or view doesn't align properly as you can see in other items .
:Every time I open this screen ,some item's anchor view does not align properly.
What I did?
I implemented the Recycler with GridLayout manager .
//setting Grid layout
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, ROW_COUNT, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(gridLayoutManager);

and custom adapter.
Item Layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:padding="1dp"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <ImageView
            android:id="@+id/service_image"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scaleType="centerCrop"
            android:src="@drawable/dummy_image"/>

        <LinearLayout
            android:id="@+id/service_description"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/COLOR.teal_green"
            android:orientation="vertical"
            android:padding="16dp"
            >

            <mychatapplication.view.custom.CustomTextView
                android:id="@+id/service_topic_title"
                style="@style/customCommonFont_regular"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_marginTop="8dp"
                android:maxLines="3"
                android:text="Android tutorial text"
                android:textColor="@color/white"
                android:textSize="16sp"/>

            <mychatapplication.view.custom.CustomTextView
                android:id="@+id/service_topic_author"
                style="@style/customCommonFont_regular"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:gravity="center"
                android:maxLines="3"
                android:text="Jugal kishor joshi"
                android:textColor="@color/color_background_chat_text_view"
                android:textSize="12sp"
                />

            <mychatapplication.view.custom.CustomTextView
                android:id="@+id/service_topic_interest"
                style="@style/customCommonFont_regular"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="left"
                android:layout_weight="1"
                android:gravity="center_horizontal|bottom"
                android:text="Follow"
                android:textAllCaps="true"
                android:textColor="@color/white"
                android:textSize="16sp"
                android:textStyle="bold"
                />
        </LinearLayout>

    </LinearLayout>

    <de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginLeft="16dp"
        android:src="@drawable/user_profile_image_dummy"
        app:civ_border_color="@color/white"
        app:civ_border_width="2dp"
        app:layout_anchor="@id/service_description"
        />
</android.support.design.widget.CoordinatorLayout>

Answer: Try to modify your code as:
<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_marginLeft="16dp"
    android:src="@drawable/user_profile_image_dummy"
    app:civ_border_color="@color/white"
    app:civ_border_width="2dp"
    app:layout_anchor="@id/service_description"
    app:layout_anchorGravity="bottom|start"

    />

to include app:layout_anchorGravity="bottom|start"


How to replicate standard Tobit results in R using mhurdle package

Question: I'm exploring multiple-hurdle models using the mhurdle package in R. To start, I trying to simply replicate the results of a standard Tobit model run using the tobit() function from the AER package or the censReg() function from the censReg package (both produce the same results). Using sample data from mhurdle, these latter two packages yield the same coefficient estimates for a Tobit regression, but I cannot replicate them using mhurdle(). Here is a basic example. I suspect I'm overlooking something rather simple, but I can't figure it out. Any idea where the misspecification lies between the two models?
library(censReg)
library(mhurdle)

### Load sample data from the mhurdle package
data(Interview)

## mhurdle() results for standard Tobit specification
summary(mhurdle(vacations ~ 0 | linc, data=Interview, dist="n", h2=T))

## censReg() results for Tobit (same as tobit() from "AER")
summary(censReg(vacations ~ linc, data=Interview))

The mhurdle package produces the following output:
Call:
mhurdle(formula = vacations ~ 0 | linc, data = Interview, dist = "n",
    h2 = TRUE, method = "bfgs")

Frequency of 0:  0.848

Coefficients :
               Estimate Std. Error  t-value              Pr(>|t|)
h2.(Intercept) -8.97938    0.54647 -16.4315 < 0.00000000000000022 ***
h2.linc         4.98411    0.61031   8.1666  0.000000000000000222 ***
sd.sd           8.68863    0.18009  48.2463 < 0.00000000000000022 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Log-Likelihood: -765.67 on 3 Df

R^2 :
 Coefficient of determination : 0.040817
 Likelihood ratio index       : NA

The censReg and the AER packages produce the following output:
Call:
censReg(formula = vacations ~ linc, data = Interview, method = "bfgs")

Observations:
         Total  Left-censored     Uncensored Right-censored
          1000            848            152              0

Coefficients:
            Estimate Std. error t value              Pr(> t)
(Intercept) -6.33252    0.53559 -11.823 < 0.0000000000000002 ***
linc         3.51493    0.46583   7.545   0.0000000000000451 ***
logSigma     1.81278    0.06231  29.091 < 0.0000000000000002 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

BFGS maximization, 44 iterations
Return code 0: successful convergence
Log-likelihood: -712.5855 on 3 Df

Answer: The mhurdle function has an argument "scaled". If its meaning is TRUE (default), the dependent variable is divided by its geometric mean.  So, in this case estimated coefficients are scaled by the geometric mean of dependent variable. To obtain the standard result (tobit, censreg) just change "scaled" to FALSE.
summary(mhurdle(vacations ~ 0 | linc, data=Interview, dist="n", h2=T, scaled = FALSE))


Simultaneity in causal diagrams

Question: Lets assume we have simultaneity problem. Variable x causes y and y causes x. As an example i would state alcoholism: the more respondent consumes alcohol, the more 'is' alcoholic (measured for example by psychological test score). The more 'is' alcoholic, the more consumes alcohol.
Is it possible discuss such problem in the causal diagram paradigm by Judea Pearl?
My only way of thinking here is to model underlying structure using lagged values, creating structure similar to VAR models, and draw such relations:

$x_{t-1}$ -> $x_{t}$
$y_{t-1}$ -> $y_{t}$
$x_{t-1}$ -> $y_{t}$
$y_{t-1}$ -> $x_{t}$

Is this idea correct? If it is - are there alternatives? Is it possible to model such situation in cross-section way?
Answer: It may not be possible, or at least has not yet been worked out yet. See the discussion in section 4.3 in Guido Imbens's Potential Outcome and Directed Acyclic Graph Approaches to Causality: Relevance for Empirical Practice in Economics working paper.
The context there is supply and demand, which is the canonical example of a simultaneous equilibrium relationship.
Comment: Thank you for this article. This is awesome reference!
Answer: As far as I know, there is no way to model such situations in a cross-sectional way. At least the way around this issue in epidemiology is to impose a time-ordering, like with lagged variables as you mention.
A simple example is antiretroviral therapy (ART) and CD4. Before ART for recommended at diagnosis, ART therapy was dependent on CD4 T-cell count. Therefore, previous CD4 was related to current ART treatment status, which was related to future CD4 counts. By creating a lagged version of CD4 (like previous month), you can establish a clear time-order of CD4 and ART.
Even in the supply & demand example described in the linked paper, I am not sure how supply and demand could occur exactly at the same time (but maybe that's a lack of understanding of economics on my part).
Without imposing a time-order, I have trouble even thinking how you could establish a causal relationship between two variables. If they occur simultaneously, why would you think that don't have a mutual common cause that is the actual source? Furthermore, how would two things that occur at the exact same moment actually cause each other? That seems to break the temporal sequence we think is necessary for causes. I think there are relatively few things that occur exactly simultaneously. While your data may not be fine enough of a time-scale to differentiate, that's more a data issue than a causal diagram issue.
Comment: I still think a corresponding causal diagram would be divided into sections of time. The 'effect' of gravitational attraction needs time to occur on the relative positions of the objects (a single measurement of position is not enough). It is an interesting example though, as the unique part is the units affecting each other. The diagram here would best be one for interference, where the units can affect each other (i.e., units are not independent) https://arxiv.org/abs/1403.1239
Comment: Perhaps a better example of simultaneity is gravitational attraction between two masses in space at position 1 ($p_1$) and position 2 ($p_2$). Each mass simultaneously (more or less) causes change in the position of the other mass $p_1$ -> $p_2$ and $p_2$ -> $p_1$.

Simultaneity in causal diagrams


Regular Expression to exclude a String around the required String

Question: In between a HTML code:
...
I need this String only
...
How do I write Regular Expression (for Rainmeter which uses Perl RegEx) such that:
-required string "I need this String only" is grouped to be extracted,
-the HTML link tag ... might be
absent or present & can be present in between the required string and multiple times as well.
My attempt:
(?siU)
.*[>]{0,1}(.*)[]{0,1}

where:
.*= captures every characters except newline{]{0,1}= accepts 0 or 1 times presence of > {upto >}
(.*)= captures my String
[]{0,1}= accepts 0 or 1 times presence of

this, of course, doesn't work as I want,
This gives output with HTML linking preceding my string
so my question is
How to write a better(and working) RegEx?
Comment: You should use `XPath` or a XML parser instead. Regex isn't a recommanded approach for this kind of content.
Comment: `[]` is a character class that matches any of the characters, not the string ``.
Comment: `[]` means that character set, `<` or `/` or `a` or `>`. Maybe you meant `(){0,1}`? Also, `.*` will try to match as much as it can. `.*?` might work because it would say match as little as possible.
Comment: [Why a regex *really* isn't good enough for HTML and XML, even for "simple" tasks](https://www.perlmonks.org/?node_id=11116478)
Comment: Actually, because everything after the `.*` in `.*` is optional, that first `.*` will match all the way to `` and everything else in between will match to empty strings. I agree with Amessihel and choroba, you really want a proper XML or HTML parser for this, not a regular expression.
Comment: That being said, maybe this would help you: `]*>([^<]*)`. Here I'm relaxing your requirements to just matching something within the `` tag, and assuming that `<` and `>` will not occur elsewhere, since they should be escaped as `<` if they did. The key is using `[^>]*` and `[^<]*` to say match anything but the angle brace I'm next expecting as a delimiter.
Answer: Even though I agree with the advice to use a real parser for this problem, this regular expression should solve your problem:
<div [^.<>]|*>(?:[^<>]*<a [^<>]*>)*([^<>]*)(?:)*

Logic:

require
 at the beginning and
 at the end.
allow and ignore  before the matched text arbitrarily many times
allow and ignore  after the matched text arbitrarily many times
ignore any text before any  with [^<>]* in front of it. Using .* would also work, but then it would skip all text arbitrarily up to the last instance of  in your string.
I use [^<>]* instead of .* to match non-tag text in a protected way, since literal < and > are not allowed.
I use (?:...) to group without capturing. If that is not supported in your programming language, just use (...) instead, and adjust which match you use.

Caveat: this won't be fully general but should work for your problem as described.


What problem or game are variance and standard deviation optimal solutions for?

Question: For a given random variable (or a population, or a stochastic process), mathematical expectation is the answer to a question What point forecast minimizes the expected square loss?. Also, it is the optimal solution to a game Guess the next realization of a random variable (or a new draw from a population), and I will punish you by the squared distance between the value and your guess if you have linear disutility in terms of the punishment. Median is the answer to a corresponding question under absolute loss and mode is the answer under "all or nothing" loss.
Questions: Does variance and standard deviation answer any similar questions? What are they?
The motivation for this question stems from teaching basic measures of central tendency and spread. Whereas the measures of central tendency can be motivated by decision-theoretic problems above, I wonder how one could motivate the measures of spread.
Comment: Very interesting question. My initial approach would be that the "game" is qualitatively the same as what you already describe, except that the question expects (no pun intended) the answer to be about a *range* of values instead of one point, since spread without a point of reference is rather incomplete (if not meaningless) information.
Comment: Note that variance is itself an expectation - if $Y=(X-\mu)^2$ then $\text{Var}(X)=E(Y)$.
Comment: @Glen_b, you are right, and I got that (I should have included that in the question text). "Guess the difference between the next value and the expectation and I will punish you quadratically" would be the game. Is that the best there is? Does not sound very practical or very fun a game, IMHO.
Answer: If I have understood the question as intended, you have in mind a setting in which you can obtain independent realizations of any random variable $X$ with any distribution $F$ (having finite variance $\sigma^2(F)$). The "game" is determined by functions $h$ and $\mathcal L$ to be described.  It consists of the following steps and rules:

Your opponent ("Nature") reveals $F.$
In response you produce a number $t(F),$ your "prediction."

To evaluate the outcome of the game, the following calculations are performed:

A sample of $n$ iid observations $\mathbf{X}=X_1, X_2, \ldots, X_n$ is drawn from $F.$
A predetermined function $h$ is applied to the sample, producing a number $h(\mathbf{X}),$ the "statistic."
The "loss function" $\mathcal{L}$  compares your "prediction" $t(F)$ to the statistic  $h(\mathbf{X}),$ producing a non-negative number $\mathcal{L}(t(F), h(\mathbf{X})).$
The outcome of the game is the expected loss (or "risk") $$R_{(\mathcal{L}, h)}(t, F) = E(\mathcal{L}(t(F), h(\mathbf{X}))).$$

Your objective is to respond to Nature's move by specifying some $t$ that minimizes the risk.
For example, in the game with the function $h(X_1)=X_1$ and any loss of the form $\mathcal{L}(t, h) = \lambda(t-h)^2$ for some positive number $\lambda,$ your optimal move is to pick $t(F)$ to be the expectation of $F.$
The question before us is,

Do there exist $\mathcal{L}$ and $h$ for which the optimal move is to pick $t(F)$ to be the variance $\sigma^2(F)$?

This is readily answered by exhibiting the variance as an expectation.  One way is to stipulate that $$h(X_1,X_2) = \frac{1}{2}(X_1-X_2)^2$$ and continue to use quadratic loss $$\mathcal{L}(t,h) = (t-h)^2.$$  Upon observing that
$$E(h(\mathbf{X})) = \sigma^2(F),$$
the example allows us to conclude that this $h$ and this $\mathcal L$ answer the question about variance.

What about the standard deviation $\sigma(F)$?  Again, we need only exhibit this as the expectation of a sample statistic.  However, that's not possible, because even when we limit $F$ to the family of Bernoulli$(p)$ distributions we can only obtain unbiased estimators of polynomial functions of $p,$ but $\sigma(F) = \sqrt{p(1-p)}$ is not a polynomial function on the domain $p\in (0,1).$  (See For the binomial distribution, why does no unbiased estimator exist for $1/p$? for the general argument about Binomial distributions, to which this question can be reduced after averaging $h$ over all permutations of the $X_i.$)
Comment: There's a standard way to go from $2$ to $n$: compute the statistic for all pairs and average.  Indeed, that produces my characterization of covariance at https://stats.stackexchange.com/a/18200/919.  For the formal theory of this, read about [U statistics](https://en.wikipedia.org/wiki/U-statistic).
Comment: Thank you very much!
Comment: Thank you for a clear articulation of my question and an equally clear answer. Would you also have an example of $h$ that depends on all $n$ sample points, not just two?


When is a given matrix-valued function the Jacobian of something?

Question: Let $F$ be an $n\times m$ matrix of real-valued functions which are defined and smooth on a neighborhood of a point $p\in \mathbb{R}^m$. Under what conditions is it possible to find a smooth function $f\colon U \to \mathbb{R}^n$ defined on a (possibly smaller) neighborhood $U$ of $p$ such such that $F= J_f$ on $U$, where $J_f$ is the Jacobian matrix of $f$?
Comment: $F: U \to \mathbb{R}^n $ is a linear transformation so $F$ is its own derivative.  Hence, $f(x_1,..., x_m) = F(\vec{x}) $ works.
Comment: @MustafaSaid $F$ is a matrix of _functions_, so it's very much not linear.
Answer: The $i^{\text{th}}$ row vector $F_i$ of $F$ needs to be $df_i$ for some smooth function $f_i\colon U\to\Bbb R$, so, viewing $F_i$ as a $1$-form, it is exact on a simply connnected $U$ if and only if it is closed. That is, we need $\dfrac{\partial F_{ij}}{\partial x_k} = \dfrac{\partial F_{ik}}{\partial x_j}$ for all $1\le i\le n$, $1\le j,k\le m$.


tee to a log within a bash script, while preserving stdout as a TTY

Question: Similar to redirect COPY of stdout to log file from within bash script itself, but I'd also like to preserve stdout as a TTY device.
For example, I have the following scripts:
/tmp/teed-off$ cat some-script
#!/usr/bin/env ruby

if $stdout.tty?
  puts "stdout is a TTY"
else
  puts "stdout is NOT a TTY"
end

/tmp/teed-off$ cat wrapper
#!/usr/bin/env bash

exec > >(tee some-script.log)

./some-script

When I run them, the wrapper eats stdout as a TTY device:
/tmp/teed-off$ ./some-script
stdout is a TTY
/tmp/teed-off$ ./wrapper
stdout is NOT a TTY

How can I flip that behavior around so that the script believes that its in a TTY even when executed via the wrapper?
Answer: It won't be trivial, but I think you can do it via pseudo-ttys.  I'm not sure that there's any standard tool, other than perhaps expect, that would do it for you.
It takes a bit of thinking about.  You'd have a control program that would open the pseudo-tty master, then the slave.  The slave would be connected to the output of ./some-script.  The master would be read by the control program, which would copy the data it reads from the master to the file and to standard output.
I've not tried coding that up. I'm not sure whether you could do it with standard shell commands; I can't think of any way.  So, I think there will be some C coding to be done.
Answer: look for dup2 it duplicates a file descriptor

int dup2(int oldfd, int newfd);
Comment: How is a C function call going to help in a question about Ruby and shells?
Comment: I'd like to avoid having to drop to C for the wrapper script (or a third one), to be sure :(


JS: awaiting promises in map

Question: I asked a question that was closed due to similar questions but even after reading those and seeing that map is not Promise aware, I have tried to use await Promise.all() around my mapping.
let foo = [1,2,3];

(async() => {
   await Promise.all(foo.map(x => {
      return new Promise(async function(resolve) {
         await setTimeout(() => {
            console.log("X")
            resolve()
         }, 3000)
      });
   }))
})()

instead of sequentially getting a "X" log every 3 seconds, it's just waiting 3 seconds and then logging them all.  I am not sure what I am doing wrong at this point.
Comment: Consider reading this post: [Is Node.js native Promise.all processing in parallel or sequentially?](https://stackoverflow.com/questions/30823653/is-node-js-native-promise-all-processing-in-parallel-or-sequentially)
Comment: Promise.all is an all or nothing handler, if all the promises in it resolves then it finishes. So the expected behavior would be all the logs at one time. If one of the promises fail then they "all" fail and it will go into the reject of the promise
Comment: All of the promises start at the same time, thus they resolve at the same time.
Answer: You are setting all the timeouts together at once. You need to increase the timeout duration in each iteration, maybe like this:

let foo = [1,2,3];

(async() => {
   await Promise.all(foo.map((x, i) => {
      return new Promise(async function(resolve) {
         await setTimeout(() => {
            console.log("X")
            resolve()
         }, 3000 * (i + 1))
      });
   }))
})()
Comment: I think what he wants is a sequence: *(3 seconds)* "X" *(3 seconds)* "X" *(3 seconds)* "X" without adding time onto the next Promise
Comment: Instead of: *(3 seconds)* *(6 seconds)* *(9 seconds)* at the same time**

JS: awaiting promises in map


Removing div wrapper of textfield element in webform

Question: I would like to remove div element on webform textfield.
Instead of :

  <input type="text" id="edit-submitted-vous-etes-nom" name="submitted[vous_etes][nom]" value="" size="60" maxlength="128" class="form-text">

I would like to have this :
<input type="text" id="edit-submitted-vous-etes-nom" name="submitted[vous_etes][nom]" value="" size="60" maxlength="128" class="form-text">

Answer: You would do this by overriding theme_form_element() in your theme, eg, something along the lines of:
function YOURTHEME_form_element($variables) {

  $element=&$variables['element'];

  if ( /* this is a webform textfield */ ) {

    // create the html without the div wrapper

  } else {

    // do it the standard way

  }

  return /* the html */;

}

A look into includes/form.inc will show you the full theme_form_element() code that you could copy into your function above where necessary.
Comment: Can we custumise the output of the form tag ? : <form id="contact_form" action="" ..."
Comment: most definitely, see `theme_form()` in `includes/form.inc` and override it similarly as `YOURTHEME_form()` in your theme's `template.php` file
Comment: If you're trying to target only one element and have all others flow as normal, return your html for the element you want and have all others hit `return theme_form_element($variables);`
Comment: I try this but when there is `textarea` field it  yet return it with `div` wrapper
Answer: function YOURTHEME_form_element($variables) {
  $element = &$variables['element'];
  if ( isset($element['#remove-div-wrap']) && $element['#remove-div-wrap'] === true ) {
    return preg_replace('/
]*>|<\/div>/is', '', theme_form_element($variables));
  } else {
    return theme_form_element($variables);
  }
}

Use hook_form_alter() to specify the affected fields:
function YOURTHEME_form_FORMID_alter(&$form, &$form_state, $form_id) {
  $form['mail']['#remove-div-wrap'] = true;
}


How to use namespaced components in handlebars templates

Question: In an ember-cli addon project, if I put a file in the addon/components/my-component.js, how do I use then in the consuming applications handlebars files?
Normally you would use the following if it were in the application namespace:
{{my-component}}
Answer: If your addon is called "Ember CLI Some Addon", then:
import DataRouteMixin from "ember-cli-some-addon/mixins/data-route";

See: https://github.com/stefanpenner/ember-cli/pull/1544
In your case, with a component, to make it available to all the templates you would have to inject it into to the container using an initializer. Although, that looks like fighting your way against the Ember CLI workflow.
If you place the addon component in the app/ folder the component will be available to every template.