Replicating child elements in JavaScript [Bug]

Question: I am trying to solve a problem for an assignment, where the objective is to build a game. The game loads with smiley faces shown across two columns(divs) laid out side by side. There is one extra smiley on the left div, and if the user clicks on that extra smiley, the next run of the game should:

Increase the number of smileys by 5, for the next run
Generate a whole new set of smileys for the second run(by removing existing smileys from the current run).

The code I have so far has a bug. If I include the code for removing child nodes from the current run of the game, in the next run 5 more smileys are not added. If I remove this code, then in the next run, 5 more smileys are added but the previous smileys are at the same position (meaning they are not removed).
Here's the JS code:
var numberOfFaces = 5;
    var theLeftSide = document.getElementById("leftSide");
    var theRightSide = document.getElementById("rightSide");
    var theBody = document.getElementsByTagName("body")[0];

    function generateFaces() {

        //remove current children
        while(theLeftSide.firstChild){
                theLeftSide.removeChild(theLeftSide.firstChild);
            }

        while(theRightSide.firstChild){
                theRightSide.removeChild(theRightSide.firstChild);
            }

        while(numberOfFaces > 0) {
            var face = document.createElement("img");
            face.src = "http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png";
            var topValue = generateRandomNumber(0,400);
            face.style.top = topValue+"px";
            var leftValue = generateRandomNumber(0,400);
            face.style.left = leftValue+"px";
            theLeftSide.appendChild(face);

            //changes
            var leftSideImages = theLeftSide.cloneNode(true);
            var leftSideLastImage = leftSideImages.lastChild;
            leftSideImages.removeChild(leftSideLastImage);
            theRightSide.appendChild(leftSideImages);

            //4th - generate 5 more faces for correct click in left side
            theLeftSide.lastChild.onclick = function nextLevel(event){
                event.stopPropagation();
                numberOfFaces += 5;
                generateFaces();
            };

            theBody.onclick = function gameOver() {
                alert("Game Over!");
                theBody.onclick = null;
                theLeftSide.lastChild.onclick = null;
            };

            numberOfFaces--;
        }

    }

    function generateRandomNumber(min, max) {
        return Math.floor(Math.random() * (max-min + 1));
    }

And this is the link to the Fiddle.
I also made a Codepen pen that has the same code.
Would appreciate any help.

Update -
My function structure was somewhat different in earlier attempts. I revised it based on some forum comments. Here's the earlier structure-
function generateFaces() {

    //remove current children
    while(theLeftSide.firstChild){
            theLeftSide.removeChild(theLeftSide.firstChild);
        }

    while(theRightSide.firstChild){
            theRightSide.removeChild(theRightSide.firstChild);
        }

    while(numberOfFaces > 0) {
        var face = document.createElement("img");
        face.src = "http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png";
        var topValue = generateRandomNumber(0,400);
        face.style.top = topValue+"px";
        var leftValue = generateRandomNumber(0,400);
        face.style.left = leftValue+"px";
        theLeftSide.appendChild(face);
        numberOfFaces--;
    }

        //changes
        var leftSideImages = theLeftSide.cloneNode(true);
        var leftSideLastImage = leftSideImages.lastChild;
        leftSideImages.removeChild(leftSideLastImage);
        theRightSide.appendChild(leftSideImages);

        //4th - generate 5 more faces for correct click in left side
        theLeftSide.lastChild.onclick = function nextLevel(event){
            event.stopPropagation();
            numberOfFaces += 5;
            generateFaces();
        };

        theBody.onclick = function gameOver() {
            alert("Game Over!");
            theBody.onclick = null;
            theLeftSide.lastChild.onclick = null;
        };

        //numberOfFaces--;

}

Comment: Your fiddle works like a charm for me, as soon as you call the `generateFaces()` function after the definition, look here http://jsfiddle.net/1a91858p/
Comment: I'm not sure I understand what you mean. I had a `` at the top, looks like that wasn't included in JSFiddle somehow. But even without that, if you simply call `generateFaces()` at the end - the problem remains the same. You get 5 new smileys (at different positions) if you click on the smiley correctly - but it should be 5 **more** smileys instead of just 5 smileys! That's what is not working right now.
Comment: ok, got it. and they all (old 5 and new 5) are at new positions?
Comment: My apologies since the question isn't clear. Yes, all smileys(old 5 and new 5) are at new positions. Here's what every correct run of the game would do - redraw the existing set of smileys at new positions and add 5 more smileys to this, such that with the first run you have 5 smileys, for the next correct run you have 10 smileys, and so on. In the second run, the first 5 smileys would be redrawn(at random positions) and another 5 smileys would be added to the set. This continues with every loop.
Answer: You are decrementing numberOfFaces to zero in the while loop and add 5 again, so the value is always 5.
Change the while loop to
for(var n = 0; n < numberOfFaces; n++) {
    var face = document.createElement("img");
    face.src = "http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png";
    var topValue = generateRandomNumber(0,400);
    face.style.top = topValue+"px";
    var leftValue = generateRandomNumber(0,400);
    face.style.left = leftValue+"px";
    theLeftSide.appendChild(face);

    ...
}

and each run adds an extra 5 smiley set. Altered fiddle here
Comment: Many thanks! I wonder why I didn't see that. Every time `numberOfFaces` becomes 5, as it's a global variable, so only 5 faces are shown. Appreciate the help.
Answer: Append your right child after while loop:
    var numberOfFaces = 5;
    var theLeftSide = document.getElementById("leftSide");
    var theRightSide = document.getElementById("rightSide");
    var theBody = document.getElementsByTagName("body")[0];

    function generateFaces() {

        //remove current children
        while(theLeftSide.firstChild){
                theLeftSide.removeChild(theLeftSide.firstChild);
            }

        while(theRightSide.firstChild){
                theRightSide.removeChild(theRightSide.firstChild);
            }

        while(numberOfFaces > 0) {
            var face = document.createElement("img");
      face.setAttribute("src", "http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png");
            //face.src = "http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png";
            var topValue = generateRandomNumber(0,400);
            face.style.top = topValue+"px";
            var leftValue = generateRandomNumber(0,400);
            face.style.left = leftValue+"px";
            theLeftSide.appendChild(face);

            //changes
            var leftSideImages = theLeftSide.cloneNode(true);
            var leftSideLastImage = leftSideImages.lastChild;
            leftSideImages.removeChild(leftSideLastImage);

            //4th - generate 5 more faces for correct click in left side
            theLeftSide.lastChild.onclick = function nextLevel(event){
                event.stopPropagation();
                numberOfFaces += 5;
                generateFaces();
            };

            theBody.onclick = function gameOver() {
                alert("Game Over!");
                theBody.onclick = null;
                theLeftSide.lastChild.onclick = null;
            };

            numberOfFaces--;
        }
        theRightSide.appendChild(leftSideImages);
    }

    function generateRandomNumber(min, max) {
        return Math.floor(Math.random() * (max-min + 1));
    }

Check out codepen
Comment: Umm- that's not exactly what the issue is. Even without the append of right child after while loop, I did get 5 new smileys at every subsequent run. But the bug is that I am required to get 5 **more** smileys with every correct run of the game, thereby making the level of the game harder. I had written this line just for that - `numberOfFaces += 5;` But only 5 new smileys are generated and displayed with every correct run of the game. What should happen is that every correct run of the game should regenerate the old smileys at new positions and generate 5 new smileys as well.


concept behind interchange of column vectors

Question: I came across a problem to find $A^{-1}$ if a matrix $A$ satisfies
$$A\begin{bmatrix}
3 &1  & 2\\
3 & 0 &2 \\
-1 & 1 &0
\end{bmatrix}=\begin{bmatrix}
0 & 1 &0 \\
0 &  0&1 \\
 1&0  &0
\end{bmatrix}$$
I just used simple column vector Interchanging in right hand side matrix as follows
Interchange $C_1$ and $C_2$ and then Interchange $C_2$ and $C_3$ to get Identity matrix.
If we perform same operations to left hand side matrix we get a new matrix $$C=\begin{bmatrix}
1 &2  &3 \\
 0& 2 &3 \\
1 &0  & -1
\end{bmatrix}$$
and magically that is $A^{-1}$.
Can i know what is the concept behind this?
But if i do same interchange to Rows, i am getting a different matrix.
Answer: interchanging columns is equivalent to post-multiplying by a matrix, so you are reducing $AX = Y$ into $AXZ = YZ$ with $YZ=I$, so $A^{-1} = XZ$.
interchanging rows is pre-multiplying but that does not help here since you must do that to $A$, not to $X$...
Comment: I would be a little more precise "equivalent to post-multiplying by **the permutation** matrix **associated with the columns interchange**"
Answer: The reason why this happens is that when changing two row is related to permutation matrix.
For the operation of exchanging $C_1$ and $C_2$, the related matrix is:
$$B = \begin{bmatrix}
0 &1  &0 \\
 1& 0 &0 \\
0 &0  & 1
\end{bmatrix}$$
For the operation of exchanging $C_2$ and $C_3$, the related matrix is:
$$C = \begin{bmatrix}
1 &0  &0 \\
 0& 0 &1 \\
0 &1  & 0
\end{bmatrix}$$
$$A\begin{bmatrix}
3 &1  & 2\\
3 & 0 &2 \\
-1 & 1 &0
\end{bmatrix}BC=\begin{bmatrix}
0 & 1 &0 \\
0 &  0&1 \\
 1&0  &0
\end{bmatrix}BC \implies A \begin{bmatrix}
3 &1  & 2\\
3 & 0 &2 \\
-1 & 1 &0
\end{bmatrix}BC= I $$, where I is identity matrix.
So the inverse of matrix $A$ is the $$\begin{bmatrix}
3 &1  & 2\\
3 & 0 &2 \\
-1 & 1 &0
\end{bmatrix}BC$$
= \begin{bmatrix}
1 &2  &3 \\
 0& 2 &3 \\
1 &0  & -1
\end{bmatrix}


If one rounds digits one by one starting from the end, then is the rounding same as when "cut-offing" around required the precision?

Question: If one rounds digits one by one starting from the end, then is the rounding same as when "cut-offing" around required the precision?
That is does (for $1/10^3$):
$0.84562...4356 \rightarrow 0.8456 \rightarrow 0.846$
produce the same as
$0.84562...4356 \rightarrow 0.84562...436$
$ \rightarrow 0.84562...44 \rightarrow 0.84562...4$
up until: $\rightarrow 0.846$
Comment: How does $44$ become $5$?
Answer: No, you cant't use these methods equivalently.
For example by the sequence of rounding we have:
$$.846 \approx .85 \approx .9$$
while the single rounding (which is the proper method) gives us:
$$.846 \approx .8$$
Comment: So how did they figure out the cutoff method as the valid one? Particularly, what's the property that makes it valid and the other invalid?
Comment: See the approximation error. In "cut off" method it is smaller. In sequentional rounding the approximation error accumulates and then it can be higher (like in the example above).
Comment: In the other words - in each step of the step-by-step rounding, you approximate different number. In most cases it would give you the same result, but when the number is close to the half, the results might differ.


Definition of transitivity.

Question: Definition :
A relation $\rm R$ in a set $\rm A$ called transitive, if $(a_1, a_2) \in \mathrm R$ and $(a_2, a_3) \in \mathrm R  \implies (a_1, a_3) \in \mathrm R \quad  \forall a_1, a_2, a_3 \in \mathrm A$
Problem : (source)
Let $\mathrm A$ be finite set of human beings.
Let $\mathrm R$ be a relation on the set $\mathrm A$ defined as $$\mathrm R = \{ (x,y) : \text{$x$ is wife of $y$}\}$$
Determine whether it is transitive or not.

I would say it is not transitive because if $x$ is wife of $y$ then $y$ can't be wife of $z$ and certainly $x$ can't be wife of $z$ assuming no same sex marriage or extramarital affairs by the people of set $\mathrm A$.
Here if we define $p : (x,y) \in \mathrm R \  \land \ (y,z) \in \mathrm R $   and $q : (x, z) \in \mathrm R$,
Then clearly both $p,q$ is false here and so $p \implies q$ should be false.
By the definition of transitivity ,$\text{if $(p \implies q)$ then transitive}$, the relation $\mathrm R$ is not transitive because $p \implies q$ is false.

Here is the part I don't understand, in the source of this problem the answers suggest that the relation is transitive and it is so because $p \implies q$ is false, provided I understand them properly.
I don't understand why if both $p,q$ is false then the relation is transitive and how does this follows from the definition of transitivity ?
Comment: This is why I *hate* most "real world exercises" about basic set theory. There is so much left ambiguous, and mathematics is exactly about clarify. So can only women be wives? Can women marry other women? Can men be the wives of other men, or better yet, of other women? Do you assume monogamy? Do you at least assume that every woman is married at most to one person, if monogamy is not assumed?
Comment: The relation is transitive because it's not non-transitive (there are no counterexample $x, y, z$ to the statement "$R$ is transitive"), and we assume the law of excluded middle.
Comment: @AsafKaragila For the sake of an answer assume monogamy and no homosexuality.
Answer: I write $xRy$ for $(x,y)\in R$.
A relation $R$ is transitive if and only if, for all $x$, $y$, and $z$, if $xRy$ and $yRz$, then $xRz$.
Note that a statement of the form `if $p$, then $q$' is true if $p$ is false.
The point of the exercises is to notice that there are no $x$, $y$, and $z$ such that $xRy$ and $yRz$. So, it is (trivially) true that, for all $x$, $y$, and $z$, if $xRy$ and $yRz$, then $xRz$. So, the relation $R$ with which you are dealing in this exercise is transitive.
Comment: Why `"if $p$ then $q$" is true if $p$ is false` ?
Comment: There is a long literature on this, since it is actually a complicated issue. In short, I say the following. The negation of "if $p$, then $q$" is: "$p$ and non-$q$".  Therefore, the negation of the negation of "if $p$, then $q$" is: "either non-$p$ or $q$". But the negation of the negation of "if $p$, then $q$" is true exactly when "if $p$, then $q$" is true. So, "if $p$, then $q$" is true when non-$p$ is true, i.e. when $p$ is false.
Comment: So, if we let $r : \text{if $p$ then $q$}$ then $r$ is true when $p$ and $q$ is true, if $p$ is false and if $q$ is true, right ?
Comment: "If $p$, then $q$" is false exactly in one case: when $p$ is true and $q$ is false. It is true in all the other cases, i.e. when $p$ is false or $q$ is true
Answer: A relation is transitive if whenever $(x,y)\in R$ and $(y,z)\in R$, then also $(x,z)\in R$.
In the case of "$x$ is the wife of $y$", it is necessarily the case that $x$ is a female, $y$ is a male, and that $y$ is not married to anyone else than $x$ (assuming the conditions in the comments).
In particular, only women can be wives, so if $(x,y)\in R$, it automatically means that there is no $z$ such that $(y,z)\in R$. To paraphrase Anouk, $y$ is nobody's wife.
So it is never the case that you have both $(x,y)\in R$ and $(y,z)\in R$. So now we fall back to the truth definition of an implication, $p\implies q$. And the truth definition says that if $p$ is false, then the implication as a whole is true.
In our case, $p$ is the conjunction $(x,y)\in R$ and $(y,z)\in R$. And as remarked above, it is necessarily false. So the implication is true, and therefore the relation is transitive.
Comment: You highlight the wrong IF. You highlight the IF joining the definiendum (R is transitive) and the definiens (for all x, y, z, if xRy and yRz, then xRz). The IF-THEN statement whose truth value is relevant to the solution of the exercise is the second one (which you start with WHENEVER).
Comment: @123: https://math.stackexchange.com/questions/70736/in-classical-logic-why-is-p-rightarrow-q-true-if-p-is-false-and-q-is-tr and many other threads on the site covered this fairly extensively.
Comment: In the definiens, you should also add: for all x, y, z.
Comment: `So now we fall back to the truth definition of an implication, $p\implies q$. And the truth definition says that if $p$ is false, then the implication as a whole is true.` Why ?
Comment: @123: I explain that in the sentence.
Comment: @AsafKaragila I mean to ask the reason behind the truth table.
Comment: Admittedly, I am confused about this downvote. Especially since the other answer is not significantly different from a mathematical point if view...
Comment: "To paraphrase Anouk, $y$ is nobody's wife." - Actually, $y$ is $x$'s wife... and I'm not sure "nobody is $y$'s wife" is a song. The whole thing is a stone hanging 'round 123's neck though :)
Comment: @CompuChip: As far as I understand it, you're absolutely wrong. $y$ is $x$'s husband. And again we return to me hating these kind of "real world examples" which are full of the inherent ambiguity of natural language.

Definition of transitivity.


Using Elmer to execute Python in TCL; how do I handle keyword arguments?

Question: I have a Python class that looks like this:
class Mine:
    def __init__ (self, param1=None, param2=None, param3=None):
        self.param1 = param1
        self.param2 = param2
        self.param3 = param3

What should my Elmer glue file look like for this class?  If they were all strings, I would guess this:
class Mine {

    Mine __init__ ( string, string, string ) -> create

But what if param3 is an object?  Or a dictionary?
And is there any chance that Elmer supports **kwarg:
class Mine2:
    def __init__ (self, param1=None, param2=None, **kwargs):
        self.param1 = param1
        self.param2 = param2
        self.kwargs = kwargs

Thanks.
Answer: Reading the elmer docs, I'd guess that you'd be best off using the guess type as it usually "does the right thing" (and you'll have to specify them all when doing the call).
I don't know how it handles defaulted arguments or keyword lists; the documentation doesn't say at all (but looking at the code, I'd say that it doesn't handle keyword lists well). You might want to contact the author of elmer for more advice on this…


Change the parameters in text with fields

Question: I want to change these items in my context with dynamic fields.
parameters: "Dear {prefix} {name} {family}, thank you for accepting....".
I mean when a user write {family} in his text box, the program must detect it as a key word and replace it with a dynamic fields. In summery i want to edit a text with dynamic fields by user form his panel.
can any body help me?
Answer: Consider using String.Format:
string yourString = "Dear {prefix} {name} {family}, thank you for accepting....";
yourString = yourString.Replace("{prefix}","{0}");
yourString = yourString.Replace("{name}","{1}");
yourString = yourString.Replace("{family}","{2}");
string formatedString = String.Format(yourString,prefixVariable,nameVariable,familyVariable);


Laravel 5: How to saveMany() based on value

Question: I need a little help with the following situation.
I am willing to saveMany based on the input value. Let me give code example.
I was experimenting with the following.
       $data = [
      'id' => 1,
      'name' => 'example',
      'number_of_slots' => 5,
      'material' => 'Colo',
      'equipment_status_code_id' => 1,
    ];

    $platecontainer = PlateContainer::create($data);

    foreach ($data as $key => $value) {
      $platecontainer->containerSlots()->saveMany([
          new ContainerSlot([
            'plate_container_id' => $data['id'],
            'slot' =>  $data['number_of_slots'],
            'occuiped' => false,
            ])
        ]);
    }

until $platecontainer everything works just great. What I want is when a PlateContainer is created using the data array, I want to create slots as well, but this one is based on number_of_slots
So for instance number_of_slots in the example is 5 so I want to save 5 records in (descending order) in the ContainerSlot table
so the containerslots table will end up looking something like this.
Answer: The save many method accepts an array of models, so just use a for loop for the $plateContainer's number_of_slots
$plateContainer = PlateContainer::create($data);

$containers = [];

// Need to add one to our number of slots because
// the $i count starts at one instead of zero.
$slots = $plateContainer->number_of_slots + 1;

for($i = 1; $i < $slots; $i++) {
    $containers[] =  new ContainerSlot([
        'plate_container_id' => $plateContainer->getKey(),
        'slot' =>  $i,
        'occuiped' => false,
    ]);
}

$plateContainer->containerSlots()->saveMany($containers);

Comment: does assigning id to `palate_container_id` is necessary?it would be assigned automatically when using `saveMany()`
Comment: @alex You're right, but I'm also not aware of his model setup. For explanation purposes I've left it there.
Answer: You can use createMany (array of arrays of attributes) instead of saveMany (array of models new or existing). Useful for API calls.
Answer: An alternative, if you want to insert data based on value, you can prepare your array and let the model insert in bulk.
$itemRebates = array_map(function ($rebateId) use ($payoutItem) {
    return [
        'rebate_id' => $rebateId,
        'payout_item_id' => $payoutItem->id
    ];
}, $rebateIds);

PayoutItemRebate::insert($itemRebates);

This way you don't need to wrap your data in multiple model instances.
Additional details
I tried to use the insert method from $payoutItem->historyRebates() but it didn't take the payout_item_id from the relationship. So it's better to use the model itself.
My first attempt was to use saveMany, but even preparing the data, it doesn't work with arrays, only with model instances. I wanted to avoid that.


Combine date and time is very slow, how to make it efficient?

Question: This is my code
concat(CONVERT(varchar(10), cast(cast(cast(DATE1 as int) as char(8)) as date), 101),

Above part is creating date format and then concatenating below with:
', ',
convert(varchar(5), (convert(time, left(RIGHT('000000' + CONVERT(varchar(6), TIME1), 6), 2)
+ ':' + substring(RIGHT('000000' + CONVERT(varchar(6), TIME1), 6), 3, 2)
+ ':' + substring(RIGHT('000000' + CONVERT(varchar(6), TIME1), 6), 5, 2))), 108))

This returns the time.
Combining the two in same line outputs as mm/dd/yyy, 13:00
I am converting original fields where date is written as yyyymmdd and time is written as 5-6 digits, 71200 (7:12, but seconds always 00).
This takes significantly long, so I would like a way to make the code more efficient.
A note, I am concatenating date with 4 different time columns, so maybe that's why it is taking long?
Comment: Doing string manipulations are always going to be slow.  Try using DATEADD instead.
Answer: Try using DATEADD instead of string manipulations:
DATEADD(MINUTE, TIME1%10000/100,
  DATEADD(HOUR,TIME1/10000,
    CAST(CAST(CAST(DATE1 as int) AS char(8)) AS datetime))

TIME1 / 10000 should give you the number of hours.  71200 -> 7
TIME1 % 10000 should give you the number of minutes * 100. 71200 -> 1200
TIME1 % 10000 / 100 will give you the number of minutes.  71200 -> 12

If you are querying this data often (more often than you are inserting/updating it), or you care more about query performance than updating/insert performance, then you should consider making a persisted computed column on this table so that you don't have to do these calculations at query time.  You can then also index the column which will help immensely if you regularly filter based on the datetime.
Comment: getting this error:

`Msg 9810, Level 16, State 1, Line 1
The datepart hour is not supported by date function dateadd for data type date.`
Comment: `DATEADD(MINUTE, TIME1%10000/100, DATEADD(HOUR,TIME1/10000, CAST(CAST(CAST(DATE1 as int) AS char(8)) AS date)))` is the code
Comment: Yes, try casting the original date to a datetime.


Use composition of functions to derive the function that relates oral dosage to the amount of drug that reaches the sinus cavity.

Question: https://i.stack.imgur.com/KAEJA.png
How does the book go from $$\frac{2x}{x+8}-1 \Rightarrow \frac{2x-(x+8)}{x+8}$$
Comment: 1 = $x+8\over x+8$.
Answer: The trick is that $$1=\frac{x+8}{x+8}$$ if $x\neq -8$.
Comment: So essentially, one needs to subtract the one from the rest of the expression. You do this by getting a common denominator of x+8 by allowing 1 to equal x+8/x+8 you can combine the parts of the expression to end up with 2x-(x+8) / x+8 ? Thank you for the quick reply.


How to unmarshal json to struct with uint8

Question: How do you unmarshal json into struct that contains a uint8? I am getting the error message json: cannot unmarshal object into Go struct field A.test of type uint8
In my struct I have
type A struct {
    Test  uint8  `json:"test omitempty" bson:"test"`
}

I inserted struct A into mongo and then I successfully do a mongo find and print out the collection struct A corresponds to. I can do a bson.MarshalExtJSON to convert the bson to json and then when I do a json.Unmarshal to convert json to struct A that is where I am failing.
Here is a sample golang playground that recreates the issue. I don't understand why this fails? How do I fix it?
https://play.golang.org/p/0HOAxsu166j
I see that unmarshal uses "float64, for JSON numbers" but I can't get float64 rather than uint8 to work either
Comment: Sorry you are correct (just noted it was not plain old JSON and you used the BSON package) its extended JSON which means you are trying to decode the object ```{"$numberInt":"52"}``` to a uint8.
Comment: Unmarshalling JSON to int8 [works fine](https://play.golang.org/p/xs_zVPyPNzU) but you seem to be trying to unmarshall a BSON string using the JSON parser?
Comment: Ok so the issue is that I have extendedJson and not plain JSON? I don't think calling it a BSON string is accurate
Comment: apparently related, based on this `$numberInt` thing: https://stackoverflow.com/q/30703171/1256452
Answer: Thanks to @Brits comment, I found that when I called bson.MarshalExtJSON I got extendedJson. json.Unmarshal() cannot read extendedJson like {"$numberInt":"52"} so that is why it was failing.
so to solve it I am using bson.UnmarshalExtJSON() rather than json.Unmarshal() to be able to unmarshal the extendedJSON into the struct


Prove that a NxN grid can be colored using n colors such that each color appear once each row and column

Question: Just like a simpler Sudoku game, given n, show that nxn grid can be colored using n colors so that each color appears once for each row/column.
I see that each row/column forms a complete graph so that we need at least n colors for each row/column. But I'm stuck at bringing those together and show that n is also the maximum color needed to color the grid.
Any hint? Thanks!
EDIT: I forgot to add 1 more constraint that I would need to prove also for the case that k (k less than n) row is already given. I think in that case the shifting answer would not be correct right?
Comment: Pick something for first row, and then simply shift colors by one e.g. to the left on each row.
Comment: You could consider the multiplication table for a group of order $n$ (the cyclic group always exists)
Comment: Welcome to Mathematics Stack Exchange! A quick [tour](https://math.stackexchange.com/tour) will enhance your experience. Here are helpful tips to [write a good question](https://math.meta.stackexchange.com/questions/9959/how-to-ask-a-good-question/9960#9960) and [write a good answer](https://stackoverflow.com/help/how-to-answer).  For equations, please use [MathJax](https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference).
Comment: Order your N colors in a sequence $a_1, \dots a_N$ color the first row this way and cyclically start each row i with the color $a_i$.
Answer: Give the squares of the grid coordinates $(x,y)$ where $0 \leq x, y \leq n-1$. Give the square $(x,y)$ colour $i$ where $0\leq i \leq n-1$, and $x+y \equiv i \pmod n$. This colouring satisfies the conditions required.
Answer: What about:
$$\begin{matrix}
 1 & 2 & \cdots & n-1 & n\\
2 & 3 & \cdots & n & 1\\
3 & 4 & \cdots & 1 & 2\\
\vdots & \vdots & \cdots & \vdots & \vdots\\
n & 1 & \cdots & n-2 & n-1\\
\end{matrix}$$
Answer: Find solutions for $n = 2$ and $3$. Then try for $n=4$. (Starting with simple cases is always a good strategy.)
Hint in general. Fill in the first row any way you like. Then think about a shift to the right.


Multilevel analysis when the predictor varies with one of the levels in the data hierarchy

Question: I'm doing mediational analysis on clustered (AKA nested/multilevel) data. The question I have is not specific to mediational analyses but applies to any kind of multilevel analysis.
I'm assessing whether the effect of SES (socioeconomic status) on an outcome is mediated by another variable. SES is a continuous individual predictor and the other two variables are continuous (Likert scores transformed to percentages).  This data comes from schools so the nested structure is children within classes within schools.  Meaning that the analysis needs to account for the possible similarities in responses of children who are in the same school and in the same class.
The problem is that since the schools were selected on the basis of the SES status of the neighbourhood,  the school and the individual SES predictor are correlated. This means that modeling the effect of school does not seem to make sense in this case (as it would presumably dramatically reduce the effect of the predictor).  Would it be safe here to simply ignore the school level of the hierarchy and model the effect of class membership? Any advice would be greatly appreciated!
Answer: I would say that this sounds like an additional variable on the causal path.
NSES -> SchoolSES -> M -> Y
(Where NSES is neighorhood SES, M is mediator, Y is outcome). So I agree with you that you should not add NSES as a predictor to the model, but you might think about analyzing SchoolSES as a mediator of the effect of NSES (although that adds a lot of complexity to the model, and I probably wouldn't.)
Comment: Good idea. Done.
Comment: Maybe I can rephrase and explain my problem more clearly. As I understand it,  hierarchy potentially introduces dependency in the data but the levels are not typically directly relevant to the predictor(s) or the hypothesis. But in this case I would expect the order of one of the levels (school) to correlate strongly with the predictor (the individual SES) and thus both of these to correlate in the same way with the outcome. So it seems that modeling the effect of school by allowing the intercept to vary would incorrectly weaken an expected correlation between the predictor and the outcome.
Comment: Yes, that's what I was trying to say.


Laravel 5.1 - test database with SQLite gives 'no such index' error on migration down()

Question: The following schema builder code works perfectly when running php artisan migrate and php artisan migrate:rollback on local and staging (production-like) environment. This will run an alter table statement to modify an existing table.
Note: since rows are empty, there is no need to set nullable or default value to the category_id:
// up
$table->integer('category_id')->unsigned()->index();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');

// down
$table->dropForeign('stores_category_id_foreign');
$table->dropColumn('category_id');

I'm running my functional tests with SQLite using :memory: configuration, and I'm getting the following error when the database rolls back (thanks to the DatabaseMigrations trait)
General error: 1 no such index: stores_category_id_index (SQL: DROP INDEX stores_category_id_index)

Why is this happening, is there something I have to configure on SQLite that I don't know of?
Answer: By default SQLite has foreign key support disabled.
You'll need to enabled it manually or use a different DB.
Laravel 5.1: Enable SQLite foreign key constraints
Link above talks about how to do this but essentially you need to find a way to run 'PRAGMA foreign_keys=1' on your functional test environment before tests.
Disclaimer: I've only tried this on Laravel 5


jQuery show() on specific drop-down selection

Question: When a certain value from a drop-down box is selected, I want another section of the form to be shown using jQuery's show() function. Currently, my code above the header looks like this:

And, the form is coded like this:

    Location

<select name="location" id="location">
    <option value="Clarksville">Clarksville</option>

</select>

For some reason, nothing happens when I select "On Rental" from the drop-down field, and I can't for the life of me figure out what I'm doing wrong. Can anyone see what I'm doing wrong?
Comment: FYI you can just do `$(#dpo)`
Answer: trim the value and try as below , another thing is if you are using id than no need to specify tage type in the selector
$("#location").change(function(){
      if($.trim($(this).val()) == 'On Rental'){
         $('#dpo').show("slow");
       }
     }

Comment: Why the need to trim?  It's a hardcoded value inside an option isn't it?
Answer:

Answer: Wrap it in a document.ready function.  Also you can just use ID selectors.  And your select isn't an input type..
$(function(){
    $("#location").change(function() {
        if ($(this).val() === 'On Rental') {
            $('#dpo').show("slow");
        }
    }
})

http://jsfiddle.net/wirey00/FYwPv/
Answer: I tried all of the suggestions above (including wrapping it in the document.ready function), but I couldn't get it to work . I eventually got the code to work (as I originally had it) when I placed at the bottom of the document, rather than in the header. I don't know enough about JavaScript and jQuery to know why that would be...?


"A WebService annotation is not present" exception

Question: I'm trying to call a web service. I used wsimport to create stub classes based on the WSDL. I created a small console app to test them in Eclipse, and this apps works correctly. I then moved these classes into a CMS I'm using (Day CQ 5.3), modified the package name accordingly, and created a small JSP page to test them. When I attempt to view the page, I get an error that says "A WebService annotation is not present on class package.name.ProductsSoap".
However, 1) ProductsSoap is an interface, not a class. 2) ProductsSoap does indeed have a WebService annotation. 3) There were no generated classes that implement the ProductsSoap interface.
What would cause this?
Answer: Do you use spring framework to consume your web service? You should have spring-ws-core and spring-ws-core-tiger in your classpath.


Probability of getting three zeros when sampling a set

Question: Say we have a set of $12$ numbers - $(0,1,2,0,3,0,4,0,5,6,0).$
We go thru this set one number at a time, and choose a number with a probability $1/3$; we sample. What is the probability we end up with $3$ $0$'s out of the $4$ numbers we will likely choose(on average)?
Is there a closed-form solution for this?
Comment: @probability_noob: Your list is of length 11.
Comment: That's sampling without replacement. You should read up on the [hypergeometric distribution](http://en.wikipedia.org/wiki/Hypergeometric_distribution)
Comment: @kahen: no it isn't, since they go through the list one number at a time.
Comment: Also, it's not clear what is the event whose probability you're trying to calculate (Nate and me offer conflicting interpretations).
Comment: Sorry about the confusion, and thanks for answering. To rephrase:
Comment: Consider a stream of values with different frequencies(in the example above, 0 has more frequency) and you are sampling with a probability p. I want to know what is the probability that I sample high frequency events and the probability that I sample low frequency events (say the number 1 or 2 or 3 etc).
Comment: This is still not very clear, can you write a C function (any other language will do) that returns "true" if your event happens, given a Boolean array of size 12?
Comment: Apologize again. Let me take a more concrete example. Say I'm looking at a stream of web page visits by the entire human population. Some web pages are visited more than others (fb.com, cnn.com, etc) and some are relatively rare (e.g. stackechange.com). Now I do not collect ALL pages I see, rather I sample with a probability p. It is a stream. What I want to know is - after I've sampled for some time, what is the probability of sampled highly popular pages (cnn) vs rare pages (stackexchange). Say I know the popularity distribution of the pages. Thanks much again for your time
Comment: I think it is pretty clear what you want. Nate got it right (imho).
Comment: In my opinion, Ralth got it right, or at least is close to it (my answer is a cumbersome version of Ralth's).
Answer: If I understand the problem, you have a list of 12 objects, 5 of which are special (the 0s).  (I use the word "list" instead of "set" since the latter implies there is no repetition among the objects).  Each of the 12 objects will be independently considered, and "chosen" with probability 1/3.  You want to know the probability of 3 of the special objects being chosen.
In that case, the 7 non-special objects are irrelevant.  You just want to know the probability that exactly 3 out of 5 independent events occur, when each one occurs with probability 1/3.  This is the probability of a binomial random variable with $n=5$ and $p=1/3$ taking on the value $3$.  The linked Wikipedia page has a formula for computing this.
Comment: Thanks for the answer. Yes, a set is a bit misleading here. Consider a stream of values with different frequencies(in the example above, 0 has more frequency) and you are sampling with a probability p. I want to know what is the probability that I sample high frequency events and the probability that I sample low frequency events (say the number 1 or 2 or 3 etc).
Answer: In your original question it sounded like you wanted the conditional probability $P\{\text{we obtain 3 0's}|\text{4 events are sampled}\}$ which is the number of size 4 subsets that contain 3 0's divided by the total number of size 4 subsets which would be, if $k$ is the number of 0's total, $$\frac{{k \choose 3}{12-k \choose 1}}{{12 \choose 4}}$$
In general it sounds like what you really want is the probability that any single sample is either a high frequency event or a low frequency event. Say you have an event that occurs with a high frequency, but that each trial is independent, we can model this by giving it a probability, say $p_h=\frac{n}{100}$ where n is the average # that occur in any 100 trials.
Now say you sample each trial independently with probability $p_s$. Then the probability that any single sample is one of the high frequency events is the conditional probability $$P\{\text{event is the high frequency event}| \text{event is sampled}\} = p_h $$
This is because, presumably, your sampling and your event occurrences are independent
It also sounds like you would like the expected value of the number of high frequency events sampled given a certain number of total events, $n$. Consider any single event. In order for it to be a "success" it has to be both a high frequency event and be sampled. $$P\{\text{an event is high frequency and is sampled}\} = p_h p_s$$
The probability to have $k$ high frequency events sampled when you have $n$ total events is a binomial distribution with parameter $p_hp_s$.
$$P\{k \text{ of our sampled events are high frequency events}\} = {n \choose k} (p_sp_h)^k(1-p_sp_h)^{n-k}$$
The expected number of sampled events that are high frequency is then just $np_sp_h$
Everything works the same with a low frequency event, just use an appropriate $p_l$ instead of $p_h$.
Answer: If I understand the problem, you have a sequence of independent and identically distributed random variables $X_1,X_2,\ldots,X_n$ (in your example $n=11$, as noted by Yuval), such that ${\rm P}(X_i = j) = p_j$ (in your example $j \in \lbrace 0,1,\ldots,6 \rbrace $); you sample each $X_i$ with probability $p$ (so in average you pick $pn$ numbers; in your example $p=1/3$), and wants to find the probability $P(n,j,m)$ of obtaining $0 \leq m \leq n$ occurrences of the number $j$. Then, this probability can be computed, using the law of total probability, as follows:
$$
P(n,j,m) = \sum\limits_{k = m}^n {{n \choose k}p^k (1 - p)^{n - k} {k \choose m} p_j^m (1 - p_j )^{k - m} }.
$$
The term ${n \choose k}p^k (1 - p)^{n - k}$ corresponds to sampling $k$ $X_i$'s out of $n$, and the term
${k \choose m} p_j^m (1 - p_j )^{k - m} $ corresponds to obtaining $m$ occurrences of the number $j$ given that $k (\geq m)$ $X_i$'s were sampled. The rest is left for you.
Answer: The probability that you want is $(1/3)^4(2/3)^8 N$, where $N$ is the number of subsets of length $4$ containing exactly $3$ zeroes.
Comment: This would be correct if we chose four out of twelve, but we do not.
Comment: @Raphael: Rather, it's the probability to choose four out of twelve in such a way that there are three zeroes.


Prove that for all integers $a$ and $b$ there is an integer $c$ such that $a | c$ and $b | c$.

Question: This is Velleman's exercise 3.4.25:
Prove that for all integers a and b there is an integer c such that a | c and b | c.
A proof in Velleman's expository style would be highly appreciated.
Thanks in advance.
Edits:
I know that c = ab is the requirement! The first chapter of the book is about sentential logic, the second one is about quantificational logic and in the third one he progresses to proofs!
Doesn't the proof statement mean ∀a∀b∃c(a|c ∧ b|c)? How from there we could justifiably deduce c = ab?
For you to get familiar with his expository style I'll copy and paste one example from the book in here to see what I mean:
Prove for every integer n, 6 | n iff 2 | n and 3 | n.
Proof. Let n be an arbitrary integer.
(→) Suppose 6 | n. Then we can choose an integer k such that 6k = n.
Therefore n = 6k = 2(3k), so 2 | n, and similarly n = 6k = 3(2k), so 3 | n.
(←) Suppose 2 | n and 3 | n. Then we can choose integers j and k such
that n = 2 j and n = 3k. Therefore 6( j − k) = 6 j − 6k = 3(2 j) − 2(3k) =
3n − 2n = n, so 6 | n.
Comment: Please provide further context: what number theory did you learn before this, and what is "Velleman's expository style"?
Comment: You don't "deduce that $c=ab$." You reason that $c=ab$ is one example, but there could be many such $c$ - indeed there are. That's different from the example of $2,3\mid n\iff 6\mid n$ because now you are trying to classify *all* such $n$ with $2,3\mid n$.
Comment: what products divide both a and b.
Comment: Yeah... that's right! Thanks a lot.
Answer: I don't know his style, but $c=ab$ satisfies the requirement.
Comment: @BillDubuque How do you motivate that answer?
Comment: @JoséCarlosSantos That will depend on the OP further clarifying their  context  (namely "Velleman's style" and what  they learned before this). It's truly unfortunate that FGITW answerers always spoil questions like this. But such are the defects of gamified platforms.
Comment: Presumably the OP wants some *motivation* or *intuition* for discovering the answer, not simply pulling it out of a hat like above (which *spoils* any potential learning experience).
Answer: Hint $\, a\mid c\iff c = na,\,$ so we need to find $n$ such that $\,b\mid c = na.\,$ It suffices that $\,b\mid n\ $ (why?)
Comment: Note: this answer (and the 2 others) were posted before the OP added context (everything after "Edits:...").
Answer: Expository, hm.
What integers does an integer $a$ divide? Clearly any such number is a multiple of $a$, so we have $a \mid ka, k\in \Bbb Z$ and when we undertake the division $\dfrac{ka}{a}$ we retrieve $k$ as the result and thus every integer that $a$ divides is of this form.
Similarly every integer that $b$ divides is of the form $jb, j\in \Bbb Z$.
So we need to find whether these two characterizations can coincide: can we have $c=ak$ and also $c=jb$? Since both $k$ and $j$ can be any integer, there is an easy choice available; we can set $k=b$ and $j=a$ to find that $c=ab$ is divisible by both $a$ and $b$.


Get the newest file based on timestamp in Java

Question: I have a directory which contains files in the following format. The files are in a diretory called /incoming/external/data
ABC_20100806.csv
ABC_20100807.csv
ABC_20100808.csv
ABC_20100809.csv
ABC_20100810.csv
ABC_20100811.csv
ABC_20100812.csv

As you can see the filename of the file includes a timestamp. i.e. [RANGE]_[YYYYMMDD].csv
What i need to do is find out which of these files has the newest date using the timestamp on the filename not the system timestamp and store the filename in a variable and move it to another directory and move the rest to a different directory in java.
Comment: Looks like you should be able to sort those names lexically. What issue(s) are you having?
Answer: You can read the filenames into an array using:
File directory = new File("/incoming/external/data");
String[] fileNames = directory.list(new FilenameFilter() {
    public boolean accept(File dir, String fileName) {
         return fileName.endsWith(".csv");
    }
});

And from there simply sort the array if your files always have the same prefix:
Arrays.sort(fileNames);

One way you can remove the prefix and suffix of each fileName to extract the date is:
int underline = fileName.indexOf("_");
int dot = fileName.indexOf(".");
String datePart = fileName.substring(underline, dot);

And then you can add that string to an array and sort (lexically).
If for some other reason you want to convert the dates into Java Dates, you can use:
SimpleDateFormat dt = new SimpleDateFormat("yyyymmdd");
Date date = dt.parse(datepart);

And you'll have a Java date, which you can also sort in an array or list.


Daily data regression - setting up dummies

Question: In order do make a regression for daily sales data i need to set up different dummies (e.g. day of the week, monthly, yearly, week of the year, moving holidays...)
<code>dummies <- cbind(model.matrix(~template$Weekday)[,2:7],
                 model.matrix(~as.factor(template$Month))[,2:12],
                 model.matrix(~as.factor(template$Year))[,2:5],
                 model.matrix(~as.factor(template$CalendarWeek))[,2:53])
colnames(dummies) <- c('Tue','Wed','Thu','Fri','Sat','Sun',
                   'Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec',
                   paste0('y',rep(2:5)),   #5 years of data in total
                   paste0('w',rep(2:53)))   #calendar weeks
</code>
"template" is a data.table which includes all the information (e.g. Sales, Date, Weekday...)
Right now i am missing holiday dummies (including lead & lag dummies). In order to catch the moving holidays i have to create one dummy per holiday.
How do i figure out how many lead and lag dummies i need for different holidays? Is there a way to create them "on the fly" meaning i start adding lead/lag variables step by step (e.g. first: Easter-1, second: Easter-2...) and check each time if my regression model improves (e.g. AIC goes down)?
How do i deal with not-moving holidays? Simply creating dummy variables for "day of the year"? E.g. 366 dummy variables
Are there any kind of dummy variables missing so far? E.g. day of the month?
Thanks for your support!
Update:
Sample data
Comment: Hi...the link to the data doesn't work anymore...can you fix and I will post the whole model and results to dropbox.
Comment: Post your data to dropbox.com and specify the beginning date and what country the data is from.
Comment: Hi @TomReilly, i added the data under Update. Main country is certainly the US but it is possible that there are influences of other countries (talking about specific holidays) in the data - it is coming from an international supply chain...
Answer: This is not the full model. There are a total of 74 variables. It looks like you have already have been drinking at the trough of daily data. What is there to be learned differently from your question here that wasn't answered here? Decomposition of daily time series (several years) with multiple seasonal patterns
Comment: As you can see above, day of the week, month of the year were important, but not all of them.  Yes, certain holidays and their lead and lags are important.  Why don't you download a 30 day trial from autobox.com and use that as well?  Day of the year is not something I have ever seen before.  What could that be?  Day of the month is something we have seen.
Comment: No, there is no sense to think that a given year would be different unless there was some marketing or policy change.  "Dummies for different years" is like a level shift variable or multiple ones.
Comment: Already thought about using autobox, as soon as my model works i will download it so i can compare performance and also show that it makes sense to invest in such a software solution.
Comment: Any progress here?
Comment: Thanks for your answer. Well i already accepted that there is no "simple" way to forecast daily data + there is a reason why software solutions like autobox exist. But for the sake of my thesis i have to come up with some model to forecast daily data. So i am comparing different approaches available in R (e.g. tbats, regression,...) and compare the results of different datasets that i have to forecast in order to suggest the "least worst" model.
Comment: So i am trying to improve the regression approach as much as i can. I set up dummy variables and afterwards use variable selection to find a final regression model. Would you suggest any other dummy variables that i can/should implement: Day of the week, Month, Calendar Week, Day of the year, Moving holidays (including lead and lag)? Don't think i can implement Level Shift or Pulse, at least i am not aware of any method.
Comment: Does it make sense to create dummies for the different Years?
Comment: Maybe i am using the wrong name for it or it just doesn't make sense?! Day of the year: 366 different dummies to represent each date of the year. For example: January has 31 days, so i need 30 dummies, February has 29 days (sometimes 28) so i need 28 dummies..., December has 31 days so i need 30 dummies. I thought in that way i could catch all holidays that are related to a specific date (4th July, Christmas) as well as their lead and lag effects.
Day of the month is not month specific right? So there are 30 dummies for 31 possible days of the month (1st to 31. of a month)?
Comment: Take a look at slides 45-55  http://www.autobox.com/cms/index.php/afs-university/intro-to-forecasting/doc_download/53-capabilities-presentation For day of the month, it is trying to measure the same impact on a given day of the month.  For days of the week, you create 6 dummies.  For months of the year, 11 dummies, and the holidays.
Comment: If you ignore the outliers, level shifts, changes in seasonality, level shifts then you don't get a good read on the main variables.


Using and interpreting $k$-fold cross validation for regression

Question: I have created a model using using regression and was trying to check if it is an underfit or overfit by running CV on top of it using Python (sklearn). I have used a higher degree relation between $x$, $y$ using regression model on this data set.
Below is my code:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from sklearn import linear_model, cross_validation
import math
data = pd.read_csv("location")
from sklearn import metrics

x = data.temp.values
y = data.cnt.values

x = x.reshape(len(x), 1)
y = y.reshape(len(y), 1)

train_x = x[:482]
test_x = x[482:]

train_y = y[:482]
test_y = y[482:]

regr = linear_model.LinearRegression()
clf = regr.fit(train_x**4, train_y)
scores = cross_validation.cross_val_score(clf, x, y, cv=10)

print('Coefficients: \n', regr.coef_)
print("Residual sum of squares: %.2f"
    % np.mean((regr.predict(test_x) - test_y) ** 2))
#Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % regr.score(test_x, test_y))
print(scores)

Below is the o/p
Residual sum of squares: 1906498.12
Variance score: 0.26
score (o/p of CV):[-14.82381293 -0.29423447 -13.56067979 -1.6288903 -0.31632439
0.53459687 -1.34069996 -1.61042692 -4.03220519 -0.24332097]

Questions:

What do these different values mean?
In all the codes online everyone has taken the mean of it, why?
Is this the right approach to validating whether the model is appropriate or not?
Comment: Note that in statistics ML is the common acronym for maximum likelihood, so you could rather spell out what ML stands for in your question.
Comment: @RichardHardy thank you.. I will keep that in mind
Answer:
For regression, sklearn by default uses the 'Explained Variance Score' for cross validation in regression. Please read sec 184.108.40.206 of Model Evaluation in sklearn.
The cross_val_score function computes the variance score for each of the 10 folds as shown in this link.
Since you have 10 different variance scores for each of the 10 folds of the data, the variance score that you would expect to see in the data can be computed by taking the mean of 10 scores. Hence, it is the mean value which is usually reported.
To figure out the appropriateness of the model, you need to have some comparison involved. In your case, there is just one model. Cross validation is normally used to figure out the optimal value of a parameter. In your case, the power of the independent variable could be optimized using cross validation. A suggestion would be to compute mean value of cross validation scores for each of the models with different power values and pick the model with the best mean score. In this way, you can figure out the model which has the best bias-variance combination.You can control what type of score to be outputted by using the scoring parameter. For eg. To obtain the mean squared error, use

scores_mse = cross_validation.cross_val_score(clf, x, y, scoring = 'mean_squared_error' ,cv=10)
Also, note that the mse score values will be shown as negative. The reason is that the API was built to maximize score values. Hence, the MSE is negated to pick the model with the maximum MSE score.
Answer:
Im not fimiliar with CV in Python. My guess is it would be the Means Squared Error.
You take to mean to get the overall MSE.
I don't think so. CV works as follows:
You divide your data in 10 (for example, this number can be different pieces. You train your model on 9 of them, and test the model on the last piece. You repeat this for all pieces (so 10 times). You end up with 10 different measures of MSE and you take the mean of them to get a measure of overall MSE (if all pieces of the data have the same size).

You do this because this is a more appropriate estimate of how your model would perform on external data (more appropriate than looking at the residual sum of squares).
Like I said, Im not fimiliar with Python CV but it looks like you have built one model (in my opinions, R is a better/easier program for doing this) and tested it on different parts. Dividing your data set once in training and test data would not be sufficient for a 10 fold CV. But most likely, dividing it is not necessary as the CV function will do that automatically.


If $A$ is normal and upper triangular then it is diagonal

Question:
Let $A$ be a normal matrix in Mat$_{n\times n}(\mathbb C)$, if $A$ is upper triangular then it is diagonal

(Normal means $AA^*=A^*A$, where $A^*$ is the conjugate transpose of $A$)
If I consider the diagonal of $AA^*$,  let denote $(a_{ij})=A$ and $(â_{ij})_{i,j}=AA^*$ then, since $AA^*=A^*A$
$â_{ii}=\sum\limits_{k=1}^na_{ik}\overline{a}_{ik}=\sum\limits_{k=1}^n\overline{a_{ki}}{a}_{ki}$
$\implies\sum\limits_{k=1}^n|a_{ik}|^2=\sum\limits_{k=1}^n|a_{ki}|^2$.
If I take $i=n$ then it follows that $a_{in}=0, \forall 1\le i\le n-1$ and continuing in this manner the upper diagonal entries are zero, Is this correct ?
Can I show it in another way, because in a previous exercise I had to show that ''If A is normal and nilpotent then $A=0$'' so using this can I decompose $A$ into diagonal and nilpotent matrix, then show that the nilpotent part is zero ?
Comment: Your approach is good, but is essentially sketchy induction. You can prove this much more cleanly with induction.
Answer: Denote $A=\begin{pmatrix}a_{11}&a_{12}&\cdots&a_{1n}\\
&a_{22}&\cdots&a_{2n}\\&&\ddots&\vdots\\&&&a_{nn}\end{pmatrix}$.
Observe that the $(1,1)$-entries of $A^\ast A$ and $AA^\ast$ are
$$\vert a_{11}\vert^2\quad\mbox{and}\quad \sum_{i=1}^n\vert a_{1i}\vert^2,$$
respectively. Since $A$ is normal,
$$
\sum_{i=1}^n\vert a_{1i}\vert^2=\vert a_{11}\vert^2\quad\Rightarrow\quad
\sum_{i=2}^n\vert a_{1i}\vert^2=0\quad\Rightarrow\quad a_{12}=a_{13}=\cdots=a_{1n}=0.$$
Now, from preceding result, the $(2,2)$-entry of $A^\ast A$ is
$$\vert a_{12}\vert^2+\vert a_{22}\vert^2=\vert a_{22}\vert^2,$$
and the $(2,2)$-entry of $AA^\ast$ is
$\displaystyle\sum_{i=2}^n\vert a_{2i}\vert^2$. Again, since $A$ is normal,
$$
\sum_{i=2}^n\vert a_{2i}\vert^2=\vert a_{22}\vert^2\quad\Rightarrow\quad
\sum_{i=3}^n\vert a_{2i}\vert^2=0
\quad\Rightarrow\quad a_{23}=a_{24}=\cdots=a_{2n}=0.$$
Continue this process, we may conclude that the upper off-diagonal entries of $A$ are all zero. Hence, $A$ is a diagonal matrix.
Comment: A* is conjugate transpose? In other words did you assume standard(or orthonormal basic)  ? Is this results still hold without assumption of orthonormal basis?
Answer: Let's prove this by induction. Suppose $A$ is an $n\times n$ upper-triangular, normal matrix.
If $n=1$, this is trivial.
If $n=2$, we have
$$ A = \begin{pmatrix}
a & b\\
0 & c
\end{pmatrix}$$
So
\begin{align*}
0 = AA^* - A^*A &= \begin{pmatrix}
a & b\\
0 & c
\end{pmatrix}\begin{pmatrix}
\overline{a} & 0\\
\overline{b} & \overline{c}
\end{pmatrix}- \begin{pmatrix}
\overline{a} & 0\\
\overline{b} & \overline{c}
\end{pmatrix}\begin{pmatrix}
a & b\\
0 & c
\end{pmatrix} \newline
&= \begin{pmatrix}
|a|^2 + |b|^2 & b\overline{c}\\
\overline{b}c & |c|^2
\end{pmatrix} - \begin{pmatrix}
|a|^2 & \overline{a}b \\
a\overline{b} & |b|^2+|c|^2
\end{pmatrix} \newline
&= \begin{pmatrix}
|b|^2 & b\overline{c}-\overline{a}b\\
\overline{b}c -a\overline{b} & |b|^2
\end{pmatrix}_.
\end{align*}
Therefore $|b|^2 = 0$. Thus $b = 0$ and $A$ is diagonal.
Now assume the result holds for $n-1$.
Then $$A = \begin{pmatrix}
a & B\\
0 & C
\end{pmatrix}$$
where $B$ is an $1\times (n-1)$ matrix and $C$ is an $(n-1)\times(n-1)$ upper-triangular matrix.
Since $A$ is normal,
\begin{align*}
0 = A^*A-AA^* &= \begin{pmatrix}
\overline{a} & 0\\
B^* & C^*
\end{pmatrix}\begin{pmatrix}
a & B\\
0 & C
\end{pmatrix} - \begin{pmatrix}
a & B\\
0 & C
\end{pmatrix}\begin{pmatrix}
\overline{a} & 0\\
B^* & C^*
\end{pmatrix} \newline
&= \begin{pmatrix}
|a|^2 & \overline{a}B\\
aB^* & B^*B + C^*C
\end{pmatrix} - \begin{pmatrix}
|a|^2 + BB^* & BC^*\\
CB^* & CC^*
\end{pmatrix} \newline
&= \begin{pmatrix}
-|B|^2 & \overline{a}B-BC^*\\
aB^*-CB^* & B^*B + C^*C-CC^*
\end{pmatrix}_.
\end{align*}
Thus $|B| = 0$ and so $B=0$. Hence $C^*C-CC^* = 0$, i.e. $C$ is normal. By the hypothesis of induction, $C$ is diagonal. Finally,
$$ A = \begin{pmatrix}
a & 0\\
0 & C
\end{pmatrix}_.$$
So $A$ is diagonal.
Comment: I think this answer is better than the accepted answer because it fully provides the steps of the induction.


Mail server run well but shutting down soon when i try a not root user in CentOS7

Question: I want turn on the mail server(apache jmail) by normal user(not root user),
so i tried the following step for under 1024 ports.
It runs well when root turns it on.
But it shutting down few second later when normal user turn it on, and it isn't remain any log on server.
I don't under stand why it is happened, and i can't find any solution by googling.
cd /usr/local/java/bin
setcap 'cap_net_bind_service=+ep' java
cd /etc/ld.so.conf.d
vi java.conf  --->     /usr/local/java/lib/amd64/jli
ldconfig
cd ~userId
chown –R userId:group projectfolder
cd /volumes
chown –R userId:group shared
vi /etc/security/limits.conf --->
userId             soft    nofile           65535
userId             hard    nofile           65535
vi .bash_profile --->
export JAVA_HOME=/usr/local/java
export PATH=$JAVA_HOME/bin:$PATH
source .bash_profile
Comment: Check your mail server's logs.
Answer: At some point, you are going to have to start something as root.  You must be root to listen on port 25.  Most mail servers run as root, or they start as root and then fork child processes as an unprivileged user.
Most likely the reason it's failing as a non-root user is that it's getting a "permission denied" on port 25.
If you want to run it as a non-root user, change the configuration to listen to a port above 1024... say 9025 (for example).  Then have your firewall portmap incoming connections to port 25 to port 9025 on your mail host.


Artificial Intelligence project idea

Question: I am looking for a graduation project idea in AI and machine learning field...
The idea may require front-end user interface to attract users...
I am thinking of how AI and machine learning can help you in daily life..?
Any help/hint about new interesting ideas ?
Thanks
Edit:
I am talking about practical ideas that may be used in real life... Not an idea to prove theoretical things...  Something like a OS (or an add on in existing one) that adapt with your way of work... or a word processor that helps you collecting information about what you are writing..
Comment: This is an undergraduate thesis, right?  "Something like a OS (or an add on in existing one) that adapt with your way of work" seems wildly impractical to me.  The second idea seems better.  Why aren't you talking to an adviser and working out a doable topic with them?
Comment: I like the second idea as well. It's pragmatic and also scalable: you could start small, and even a simple implementation might be helpful, but the potential scope is almost unlimited.  You might want to pick a specific domain to give assistance on, possibly something you're already familiar with, such as a favorite history topic, etc.
Answer: What about an project that uses Markov-Chain text generation to generate answers for Stack Overflow questions? (^__^)
Answer: Two ideas:

Write a current, robust version of SHRDLU with understandable source code.
Write a SHRDLU-like program that manipulates actual code instead of imaginary blocks.  Such a tool could be used for manipulating extremely large, complicated programs, including its own code!

Imagine giving commands like the following...
(a) Scan web site X and list any sentences you failed to parse.
(b) Scan document Y and list any grammar rules you didn't need.
(c) Instead of iterating over every element of "proplist" in your "search" function, only process the cdr of "proplist" if the initial call to "lookup" returns nil.  After you make the modification, confirm the sentence "pick up a very very big block" will succeed and the sentence "pick up a very and very big block" will fail.
(d) Your "conjoin" grammar currently requires a coordinator word like "and", but that requirement is wrong.  Split your "coordination" grammar into "syndetic coordination" and "asyndetic coordination" as follows:  conjoins using "and", as in "quickly and quietly, he walked into the bank" are called "syndetic coordinations".  Conjoins without a coordinator, as in "quickly, quietly, he walked into the bank" are "asyndetic coordinations".  Now scan corpus Z to see if fewer sentences fail to parse.
Answer: One component of intelligence is imagination.
It wouldn't take much to Google for "artificial intelligence research projects" and see what other people are doing at other schools.  Since it's not a Ph.D., there's no uniqueness requirement for you.
You could also look at Peter Norvig's text to see what's been done before and adapt it.
I'd also recommend doing something with the reams of data that's available to you on the web.  Try thinking about "Programming Collective Intelligence" and "Beautiful Data" to see how you could use information to teach a program how to adapt its behavior based on new information (neural nets, genetic algorithms, ant colony algorithms, etc.)
Answer: What interests you?
AI is used in a great many areas, so find something you are passionate about and then see how to use AI for it.
For example, if you are interested in games, then you could find an interesting algorithm for the ghosts in Pac-man to chase, and use some more interesting mazes. You may find someone that is interested in doing a 3D project and they could write a 3D version and your algorithm could be more interesting.
Or, you may be interested in robotics. Again, it would be ideal if you could find someone with an interest in making a robot and you could write the AI part.  So, for example, you could see if you can figure out how to get a robot to determine the difference between a farm crop and a weed/grass.
Basically, your starting point should be on what really interests you.
Answer: Perform a clustering on documents, as done by ex-clusty search engine. This clearly is an attractive application.

Artificial Intelligence project idea


Is it posible to delete a row from an underlying table of a view?

Question: i have a database with 2 tables and 1 view. One of the tables is a lookup table and i am using the view to present the data in one single table.
CREATE TABLE places (
 place_id INT NOT NULL AUTO_INCREMENT,
 place VARCHAR(20) NOT NULL,
 PRIMARY KEY (place_id)
);

CREATE TABLE tasks (
 task_id INT NOT NULL AUTO_INCREMENT,
 task VARCHAR(20) NOT NULL,
 place_id INT NOT NULL,
 PRIMARY KEY (task_id),
 FOREIGN KEY (place_id) REFERENCES places(place_id)
);

In order to reduce the size of the select querys, i've created a view that presents the information of both tables.
CREATE VIEW view_planning AS
 SELECT tasks.task_id, tasks.task, places.place
 FROM tasks
 LEFT JOIN places ON places.place_id = tasks.place_id;

If i want to eliminate a row from the tasks table i have to do something like:
DELETE FROM tasks WHERE
 task_id = (SELECT task_id FROM tasks WHERE
             task="some task" AND
             place_id= (SELECT place_id FROM places WHERE place = "some place"));

I would like to use the view like this so the querys are shorter:
DELETE FROM tasks WHERE
 task_id = (SELECT task_id FROM view_planning WHERE
             task="some task" AND place= "some place");

However i get

Error Code: 1443. The definition of the table "view_planning" prevents operation DELETE on table "tasks".

Is there any way around this error?
Thank you.
Comment: MySQL has lots of strange restrictions on how views can be used.
Comment: In general deleting from views is a terrible idea. see mysql reference for rules on updateable view. If your view violates any of these rules you will not be able to delete from the view directly. https://dev.mysql.com/doc/refman/5.7/en/view-updatability.html
Comment: @HLGEM He's not trying to delete from the view, he's just using the view in the query that selects which rows to remove from a table the view uses.
Comment: One of the rules is: Reference to nonupdatable view in the FROM clause which I took to mean if the view is not updateable it can't be used in a delete. The error message supports that. However, see https://stackoverflow.com/questions/33133234/mysql-error-1443-workaround
Answer: Use a JOIN rather than a subquery in the WHERE clause.
DELETE t FROM tasks AS t
JOIN view_planning AS vp ON t.task_id = vp.task_id
WHERE vp.task = "some_task" AND vp.place = "some_place";

Comment: Thank you @Barmar for your answer.

I am getting: Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect. What is "safe update mode"?
Comment: That's a MySQL Workbench issue, not a problem with the query.
Comment: [Mysql Safe Update Explained](http://www.xpertdeveloper.com/2011/10/mysql-safe-update/)


Post Method in using request

Question: I am trying to simulate a click button on a page using request and python
My code is the following.
As usual, it is very simple: on this web page, just click on the button "Valider" and the file is download
But I am doing something wrong
Perhaps someone can help me?
Thanks for your help
<code>import requests

url = "https://www.doyoubuzz.com/denis-colliot/cv/download"
response = requests.post(url)
</code>
Thais
Comment: the website has download security
Comment: http://doc.doyoubuzz.com/ try using thier api
Answer: Thanks for your help.
I know their api, but I just want to learn how to do this and how to use de post functionnality of request.
I perform it with selenium for example but not with requests that's why my SOS :)
Thais

Post Method in using request


Testing nested models with clustered errors

Question: I have a simple linear model. $$Y_i = \beta_1X _{1i}+ \beta_2X_{2i}+\beta_3X_{3i}+\epsilon_i$$
I'd like to test if some of my model parameters ($\beta_2$ and $\beta_3$) are jointly different from zero.
This post gives a nice overview of how the different tests (Wald, Likelihood Ratio and Score) can be used to determine the significance of such nested models. However, it's not clear to me that the Likelihood ratio test is still appropriate in the case that I need to cluster my standard errors.
In the case of clustering with $G$ groups, my covariance matrix is will be different and the Wald Test incorporates this, both because the calculation uses the covariance matrix and critical value is now determined from an F-distribution with reduced denominator degrees of freedom ($G-1$) as opposed to the number of observations. However, since my parameter estimates remain unchanged, the residual sum of squares remains unchanged, so the Likehood Ratio will remain unchanged. Stata and python's statsmodels will report the same Log-Likelihood for the models regardless of clustering, which is still based on the number of observations, not the number of groups.
So is it completely wrong to use the Likelihood Ratio in this case?
Answer:
So is it completely wrong to use the Likelihood Ratio in this case?

Saying "completely wrong" is too strong, because it might still have in some cases better small sample properties than a cluster robust Wald test. Cluster robust Wald test overrejects in many cases in small samples.
However, the Likelihood Ratio test is not valid if there is cluster correlation or other correlation or heteroscedasticity that is not included in the model.
In the linear exponential family, including the linear gaussian model, the parameter estimates for the mean function are consistently estimated even if variance and correlation are misspecified.
Wald test can still be based on asymptotically correct standard errors if a Goddambe-Eicker-Huber-White sandwich covariance estimate is used.
There is also a corrected version of the score test, that is the analogue of a sandwich based Wald test and takes unspecified correlation or heteroscedasticity into account. (A preliminary version of this score_test is in statsmodels master.)
However, the likelihood ratio test assumes that the likelihood function is correctly specified. If that is not the case, then the distribution of the test statistic is not the usual chi-square distribution. Consequently the standard likelihood ratio test is invalid.
The distribution of the LR test statistic in many cases is the same as a weighted sum of chisquare distribution. The weights and an approximation to this distribution can be computed in some special cases, where the weights are model specific.
There are a few special cases where the LR test still has a chisquare distribution in misspecified case, a main example is quasi-likelihood in generalized linear models like Poisson with simple over or under dispersion.
Instead of proper references, some links to statsmodels issues which include many of the references for related questions that I looked, e.g. Wooldridge, Cameron and Trivedi and Boos and Stefanski.
https://github.com/statsmodels/statsmodels/issues/3363
https://github.com/statsmodels/statsmodels/issues/1755
https://github.com/statsmodels/statsmodels/issues/1163#issuecomment-48189874
https://github.com/statsmodels/statsmodels/issues/2054


Sourcing (just-some-lines) from .vim file

Question: The title is pretty much self-explanatory; is there a way to just source a couple of selected lines from a .vim file, instead of copying it to a new buffer, saving it, ... etc.
I don't want to (after writing it in a file) rewriting it in a command line, but just select it and source it. Unfortunatelly, "source" doesn't work on a range.
Bright ideas welcomed :)
Answer: You could use a custom command combined with g//
command! -range=% RSource <line1>,<line2>g/./exe getline('.')

Then select you're code, hit : and type RSource (so the command line should look like :'<,'>RSource).


For all positive integers $a, b$, if $a$ is composite and $a$ divides $b$, then $b$ is composite.

Question: This question is in my practice midterm and it does not have solutions, I want to make sure my proof is correct.
Proof:
Suppose integers $a$ and $b$, and suppose $a$ is composite therefore we can write $a$ as the product of integers $r$ and $s$ where $1 < r < a$ and $1 < s < a$. since $a$ divides $b$, we can write that $rs$ divides $b$. therefore we can write $b = rsk$ where $k$ is an integer. so $b$ is written as a product of integers where one of them is not $1$ or $b$ so $b$ is composite.
Would this be correct?
Comment: edit: i assumed the statement to be true
Answer: It's good! You have exactly the right idea, and you've presented it reasonably well. I think there's one thing that could be expanded a little bit. When you say,

so b is written as a product of integers where one of them is not 1 or b so b is composite.

I think you could justify a little more completely why none of $r$, $s$, or $k$ is $1$ or $b$. Obviously $r$ and $s$ are not $1$, by assumption, but why couldn't $k$ be $1$? Why couldn't $k$ be $b$? Also, couldn't $r$ and/or $s$ be $b$?
Of course, none of the above possibilities are possible, and it's not hard to justify this. For example, if we had $r = b$, then we'd have
$$a = rs = bs \ge b = rsk = ak \ge a.$$
Hence, we have $a \ge b$ and $b \ge a$, which implies $a = b$, which would mean $r = b = a$, even though $r < a$ by assumption.
Comment: thank you for the explanation


TomEE lookup errors

Question: I've a WebApp that uses tomcat and openejb in separate installations. I'm trying to move this to a TomEE (web profile / plus) environment and I'm running into lookup errors. JNDIContext is throwing a java.lang.NullPointerException.
I tried w/ TomEE web profile first. My webapp also uses web services and google search lead me to try TomEE plus because the web profile doesn't support web services. No luck w/ this version either.
One difference between the two version is that in the TomEE web profile, I see all the Remote objects when I access the openejb webapp. However I don't see them w/ TomEE plus version.
http://localhost:8080/openejb/viewjndi.jsp
Any suggestions of how I could resolve this? I'm guessing it is configuration issue and I don't know which one.
Here is the complete stack trace:
<code>java.lang.NullPointerException
    org.apache.openejb.client.JNDIContext.createBusinessObject(JNDIContext.java:203)
    org.apache.openejb.client.JNDIContext.lookup(JNDIContext.java:247)
    javax.naming.InitialContext.lookup(InitialContext.java:392)
    org.bluebox.customer.client.CustomerConnection.getCustomerAccountRemote(CustomerConnection.java:67)
    org.bluebox.customer.client.CustomerLogin.prepare(CustomerLogin.java:65)
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:167)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)
</code>
Answer: TomEE Plus is definitely the right distro to use if migrating from an OpenEJB/Tomcat install.  They match identically in features with the difference that the stack now supports full EJB 3.1, JAP 2.0 and JAX-RS.  The Web Profile version of TomEE is quite a bit leaner and lacks WebServices, JMS and any non-WebProfile requirement.  Here's a brief comparison of APIs supported.
That looks like a bug that was fixed with TomEE 1.0.0-beta-2 released in January.  If you can give it a try, it'd be wonderful.  We will absolutely want to make sure any issues you might face are cleared up before the 1.0.0 GA.

TomEE lookup errors


Manupulating byte code generated from ASM

Question: I want to generate byte code for a java class only with the public or protected fields, constructors, and methods.
I am trying with the below code, but I don't know is it the correct approach?

Client code:

String sourceFileName = file.getName();
ClassReader reader = new ClassReader(file.getContents());
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
JavaStubClassAdapter adapter = new JavaStubClassAdapter(writer, sourceFileName);
reader.accept(adapter, 0);
byte[] content = writer.toByteArray();
// we can use content to print in .class file

Adapator code:

private class JavaStubClassAdapter extends ClassVisitor {
    private final String sourceFileName;

    /**
     * @param writer
     * @param sourceFileName
     */
    public JavaStubClassAdapter(ClassWriter writer, String sourceFileName) {
        super(Opcodes.ASM7, writer);
        this.sourceFileName = sourceFileName;
    }

    @Override
    public void visitSource(String source, String debug) {
        super.visitSource(this.sourceFileName, null);
    }

    @Override
    public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
        if (access == Opcodes.ACC_PUBLIC || access == Opcodes.ACC_PROTECTED) {
            return super.visitField(access, name, descriptor, signature, value);
        }
        return null;
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature,
            String[] exceptions) {
        if (access == Opcodes.ACC_PUBLIC || access == Opcodes.ACC_PROTECTED) {
            return super.visitMethod(access, name, descriptor, signature, exceptions);
        }
        return null;
    }
}

Comment: Looks good to me, Except that I would declare the first parameter of the constructor as ClassVisitor instead of ClassWriter.
Answer: Below code work for me

Client code this method has org.eclipse.core.resources.IFile

        ClassReader reader = new ClassReader(file.getContents());
        if (!isAccessPermited(reader.getAccess())) {
            return new byte[0];
        }
        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        JavaStubClassAdapter adapter = new JavaStubClassAdapter(writer, sourceFileName);
        reader.accept(adapter, 0);
        return writer.toByteArray();

Helper methods

    private static boolean isAccessPermited(final int access) {
        boolean isEnumAcc = (access & Opcodes.ACC_ENUM) == 0 ? false : true;
        boolean isPublicAcc = (access & Opcodes.ACC_PUBLIC) == 0 ? false : true;
        boolean  isProtectedAcc = (access & Opcodes.ACC_PROTECTED) == 0 ? false : true;
        if (isPublicAcc || isProtectedAcc || isEnumAcc) {
            return true;
        }
        return false;
    }

    private static boolean isFinalAccess(final int access) {
        return (access & Opcodes.ACC_FINAL) != 0;
    }

Adapator code

    private static class JavaStubClassAdapter extends ClassVisitor {
        private final String sourceFileName;

        /**
         * @param writer
         *            ClassVisitor
         * @param sourceFileName
         *            String
         */
        public JavaStubClassAdapter(ClassVisitor writer, String sourceFileName) {
            super(Opcodes.ASM7, writer);
            this.sourceFileName = sourceFileName;
        }

        @Override
        public void visitSource(String source, String debug) {
            super.visitSource(this.sourceFileName, null);
        }

        @Override
        public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
            if (isAccessPermited(access)) {
                switch (descriptor) {
                case "Ljava/lang/String;":  //$NON-NLS-1$
                    if (isFinalAccess(access)) {
                        return super.visitField(access, name, descriptor, signature, "");
                    }
                    return super.visitField(access, name, descriptor, signature, null);
                case "Z":                   //$NON-NLS-1$
                    return super.visitField(access, name, descriptor, signature, Boolean.FALSE);
                case "C":                   //$NON-NLS-1$
                case "B":                   //$NON-NLS-1$
                case "S":                   //$NON-NLS-1$
                case "I":                   //$NON-NLS-1$
                case "J":                   //$NON-NLS-1$
                case "D":                   //$NON-NLS-1$
                case "F":                   //$NON-NLS-1$
                    if (isFinalAccess(access)) {
                        return super.visitField(access, name, descriptor, signature, Integer.valueOf(0));
                    }
                    return super.visitField(access, name, descriptor, signature, null);
                case "Ljava/lang/Object":   //$NON-NLS-1$
                    return super.visitField(access, name, descriptor, signature, null);

                default:
                    return super.visitField(access, name, descriptor, signature, null);
                }
            }
            return null;
        }

        @Override
        public MethodVisitor visitMethod(int access, String name, String descriptor, String signature,
                String[] exceptions) {
            if (isAccessPermited(access)) {
                MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
                return new JavaClassEmptyMethodVistor(mv, Type.getReturnType(descriptor));
            }
            return null;
        }

        @Override
        public void visitInnerClass(String name, String outerName, String innerName, int access) {
            if (isAccessPermited(access)) {
                super.visitInnerClass(name, outerName, innerName, access);
            }
        }
    }

    private static class JavaClassEmptyMethodVistor extends MethodVisitor {
        private final MethodVisitor visitor;

        private final Type returnType;

        public JavaClassEmptyMethodVistor(MethodVisitor methodVisitor, Type type) {
            super(Opcodes.ASM7, methodVisitor);
            this.visitor = methodVisitor;
            this.returnType = type;
        }

        @Override
        public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {
            // Do nothing
        }

        @Override
        public void visitFieldInsn(int opcode, String owner, String name, String descriptor) {
            // Do nothing
        }

        @Override
        public void visitInsn(int opcode) {
            if (returnType == Type.INT_TYPE || returnType == Type.FLOAT_TYPE
                    || returnType == Type.LONG_TYPE || returnType == Type.DOUBLE_TYPE
                    || returnType == Type.CHAR_TYPE || returnType == Type.BYTE_TYPE ) {
                visitor.visitIntInsn(Opcodes.BIPUSH, 0);
                visitor.visitInsn(Opcodes.IRETURN);
            } else if (returnType == Type.BOOLEAN_TYPE) {
                visitor.visitInsn(Opcodes.ICONST_0);
                visitor.visitInsn(Opcodes.IRETURN);
            } else if (returnType == Type.VOID_TYPE) {
                visitor.visitInsn(Opcodes.RETURN);
            } else {
                visitor.visitInsn(Opcodes.ACONST_NULL);
                visitor.visitInsn(Opcodes.ARETURN);
            }
        }

        @Override
        public void visitIincInsn(int var, int increment) {
            // Do nothing
        }

        @Override
        public void visitLineNumber(int line, Label start) {
            // Do nothing
        }

        @Override
        public void visitLabel(Label label) {
            // Do nothing
        }

        @Override
        public void visitTypeInsn(int opcode, String type) {
            // Do nothing
        }

        @Override
        public void visitJumpInsn(int opcode, Label label) {
            // Do nothing
        }

        @Override
        public void visitLdcInsn(Object value) {
            // Do nothing
        }

        @Override
        public void visitVarInsn(int opcode, int var) {
            // Do nothing
        }

        @Override
        public void visitMaxs(int maxStack, int maxLocals) {
            visitor.visitMaxs(0, 0);
        }

        @Override
        public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
            // Do nothing
        }

        @Override
        public void visitTableSwitchInsn(int min, int max, Label dflt, Label... labels) {
            // Do nothing
        }

        @Override
        public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
            // Do nothing
        }

        @Override
        public void visitFrame(int type, int numLocal, Object[] local, int numStack, Object[] stack) {
            // Do nothing
        }
    }

Answer: Straight from the ASM FAQ at https://asm.ow2.io/faq.html#Q11

If you want to know how to generate a synchronized block, a try catch
block, a finally statement, or any other Java construct, write the
Java code you want to generate in a temporary class, compile it with
javac, and then use the ASMifier to get the ASM code that will
generate this class.


Retrieve values from data structure holding variant data types without specifying the return type

Question: I am currently using a map with string data type as key and a variant value. I am relying on the boost::variant library for defining the map data type. I am currently following a few tutorial on boost::variant for extracting the values from the map without having to specifying the exact data type value I want to retrieve while iterating through it.
source: https://theboostcpplibraries.com/boost.variant
I want something like the last example for generic data type but instead of printing the values I was to return them.
The following below does not seem to be working correctly.
struct output : public boost::static_visitor<>
{
  template <typename T>
  T operator()(T t) const { return t }
};

I also instantiate a map while populating it with a string data type as keys and boost::variant type as values; and later on iterate through the map and process the values.
std::map<std::string,boost::variant<int, double, long long, std::string>> _keyPairValue;

for(auto &x: _keyPairValue)
{
  // appending a key pair value to a bson object which is part of mongo sdk
  b << x.first << boost::apply_visitor(output{},x.second);
}

Comment: Small note: From C++17 onwards, you should also be able to use [`std::variant`](https://en.cppreference.com/w/cpp/utility/variant) instead.
Comment: Please expand on _"...does not seem to be working correctly..."_ so we know what the problem is.
Comment: Could you provide a [mcve]? I find the question rather difficult to understand...
Answer:
I want something like the last example for generic data type but instead of printing the values I was to return them.

You probably want to have operator() overloads, something like:
struct process : public boost::static_visitor<>
{
  void operator()(double d)
  {
      // process d ...
  }
  void operator()(char c)
  {
       // process c ...
  }
  void operator()(const string& s)
  {
      // process s ...
  }
};

int main()
{
  boost::variant<double, char, std::string> v;
  v = 3.14;
  boost::apply_visitor(process{}, v);
  v = 'A';
  boost::apply_visitor(process{}, v);
  v = "Boost";
  boost::apply_visitor(process{}, v);
}


Complex logarithm and argument

Question: I have this question that I faced during my work in complex analysis. Is the following equation is correct where $x>0$
$\log \left(1+e^{-i \pi  x}\right)+\log \left(1+e^{i \pi  x}\right)=\log (2 \cos (\pi  x)+2)$
But If this is true, then why, when I evaluated the argument, I get $ i \pi $ not zero?
Can anyone explain?
Comment: $\text{Log} z=\text{Log}|z| + i {Arg}  z,-\pi <\text{Arg} z\leq \pi$
Comment: How do you define $\log$?
Comment: The natural Complex logarithm
Comment: That's just a name. Again, how do you define it?
Comment: a single-valued complex function, Ln z
Answer: Yes, that is correct. You are working with the principal value of the logarithm, and so $\log(z)+\log(w)=\log(zw)$ holds when both $z$ and $w$ have positive real part. And that's the case here, since $1+e^{\pm i\pi x}$ has positive real part, since it's $1$ plus a complex number with absolute value $1$. Besides, $(1+e^{-\pi ix})(1+e^{\pi ix})=2+2\cos(\pi x)$.
Comment: Thank you, but If I tried to simplify each term individually then I get the argument of $i \pi $ ?
Comment: I could I know? You did not tell us *how* you have evaluated it. By the way, I used Mathematica to evaluate the difference, and I got that it is always equal to $0$.
Comment: okay thank you, maybe I have made some mistakes in evaluating the argument

Complex logarithm and argument


ansible main.yml if else conditionals

Question: So I'm running an ansible role which has a file main.yml in the defaults/ role folder. The content of that file is this:
---
api_secrets:
  'API_PROFILE': "{{ api_profile }}"
  'SERVER_ADDRESS': "{{ server_address }}"
  'MGMT_SERVER_ADDRESS': "{{ management_server_address }}"

Now I want to include to the api_secrets block, after the MGMT_SERVER_ADDRESS something like this:
{% if '"port" in mgmt_ports' %}
'MGMT_SERVER_PORT': "{{ management_server_port1 }}"
'MGMT_SERVER_USER': "{{ user1 }}"
{% else %}
'MGMT_SERVER_PORT': "{{ management_server_port2 }}"
'MGMT_SERVER_USER': "{{ user2 }}"
{% endif %}

With everything from here, a file is created on the server having the content above and of course replacing the variables with their actual values.
No matter how I try, it always results in different errors. I tried with "{% if ... endif %}", also with ''
The error would be this:
ERROR! Syntax Error while loading YAML.
  found character that cannot start any token

The error appears to be in '/opt/ansible/roles/api/defaults/main.yml': line 55, column 2, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

{% if '"port" in mgmt_ports' %}
 ^ here

I've also tried like this:
   "{% if (port in mgmt_ports) %}
   'MGMT_SERVER_PORT': "{{ management_server_port1 }}"
   {% else %}
   'MGMT_SERVER_PORT': "{{ management_server_port2 }}"
   {% endif %}"

In this case the error is:
ERROR! Syntax Error while loading YAML.
  could not find expected ':'

The error appears to be in '/opt/ansible/roles/api/defaults/main.yml': line 56, column 24, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  "{% if (port in mgmt_ports) %}
  'MGMT_SERVER_PORT': "{{ management_server_port1 }}"
                       ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

What's the proper way to do this?
I know it would have been easier by using a jinja2 template but the playbooks are created like this and I have to stick to this method.
Answer: Ansible is not meant to treat If-Else-Statements.
As you mentioned in the bottom of your question, with jinja2 templates it would be easier, but it would not just be easier, but it would make it the correct way to do it.
So instead of trying to mess up your yaml file with if-else statements, create a single jinja2 file with the required template parameters (looks like you pretty much got jinja2 template structure anyways!) and create a configuration file using that.
Then you can do a template command to include the template in the correct location with the correct parameters.
"Playbooks are created like this and I have to stick to this method" yells that whoever came up with that policy is either insecure or just a child.
Answer: Templating of variables happens well after the YAML parsing step, so you cannot use it to template YAML in this way.
The easiest approach is to move the condition into the individual Jinja expressions:
api_secrets:
  API_PROFILE: "{{ api_profile }}"
  SERVER_ADDRESS: "{{ server_address }}"
  MGMT_SERVER_ADDRESS: "{{ management_server_address }}"
  MGMT_SERVER_PORT: "{{ management_server_port1 if 'port' in mgmt_ports else management_server_port2 }}"
  MGMT_SERVER_USER: "{{ user1 if 'port' in mgmt_ports else user2 }}"

You can also use Jinja statements, though that makes the value a bit lengthier for the same result.
api_secrets:
  API_PROFILE: "{{ api_profile }}"
  SERVER_ADDRESS: "{{ server_address }}"
  MGMT_SERVER_ADDRESS: "{{ management_server_address }}"
  MGMT_SERVER_PORT: "{% if 'port' in mgmt_ports %}{{ management_server_port1 }}{% else %}{{ management_server_port2 }}{% endif %}"
  MGMT_SERVER_USER: "{% if 'port' in mgmt_ports %}{{ user1 }}{% else %}{{ user2 }}{% endif %}"

ansible main.yml if else conditionals


Is there a way to extend a ThreeJS object?

Question: I'm using ThreeJS to create an interaction where people can click cubes. However these cubes behave differently when clicked (different color animations, to keep the idea simple).
My idea was to create extension classes of the THREE.Mesh object and add my custom functions and attributes. This would help isolate the different behaviors of the cubes and provide a cleaner code.
I tried using John Resigs' function to extend classes, but it seems to work just for classes that ultimately extend his "Class" class.
Is there a way to do this?
Comment: Have you tried doing it the regular way, like: `THREE.Mesh.prototype.your_method = function { }`?
Comment: @Hassan I was doing that but then by code was getting confusing.
Answer: There are several ways to make class-based systems in Javascript. John Resig's "Class" is a great one, but it is not the type of inheritance that Three.js uses.
Notice in the Cube class file the line:
THREE.Geometry.call( this );

Javascript does not provide a built-in model for class inheritance, so unless you are using a library (like John Resig's) that bakes inheritance into class construction, you have to call the super method explicitly.
Your class would inherit from CubeGeometry if, inside your class, you call:
THREE.CubeGeometry.call( this );

You will also likely want to set CubeGeometry to be the prototype:
THREE.MyCubeGeometry.prototype = new THREE.CubeGeometry();
THREE.MyCubeGeometry.prototype.constructor = THREE.MyCubeGeometry;

Comment: Ok, I managed to make it work using your idea. Can we say that the call function behaves like the "super" in other languages?
Comment: @PCoelho `call` is just a way to execute a function from a given context
Comment: there is no built-in "super" in JS. "THREE.Geometry" is the super method, and call() is calling it and injecting the local scope "this". Class-based stuff seems a bit strange in JS because it is not like other languages, but it makes JS much more flexible than other inherently class-based languages.