If ScrollView only supports one direct child, How to display the whole layout scrollable?

Question: Here i am showing the xml layout of my application.i want to scroll the whole pages using  Scrollview .But i am unable to use.ScrollView only supports one direct child.But i wanna to scroll the whole content.please help me to solve this problem?
<?xml version="1.0" encoding="utf-8"?>

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:orientation="horizontal" >

            <ImageButton
                android:id="@+id/prev"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_gravity="left"
                android:background="@null"
                android:src="@drawable/left_white" />

            <EditText
                android:layout_width="185dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="25dp"
                android:hint=""
                android:visibility="invisible"
                />

            <ImageButton
                android:id="@+id/tour"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="right"
                android:layout_marginLeft="17dp"
                android:background="@null"
                android:src="@drawable/logo" />

            </LinearLayout>
                        <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center"
                >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="What interests you?"
        android:textSize="20dp" >

</TextView>
</LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="fill_horizontal"
                android:orientation="horizontal"
                >

                <com.android.example.CoverFlow
                    android:id="@+id/coverFlow"
                    android:layout_width="match_parent"
                    android:layout_height="150dip"/>

            </LinearLayout>
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="17dp"
    >

</LinearLayout>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

</LinearLayout>

</LinearLayout>

Answer: Put Scroll view before your main LinearLayout.
<LinearLayout>
    <ScrollView>
       <LinearLayout>
             // This Linear Layout contains your entire layout code
       </LinearLayout>
    </Scrollview>
   </LinearLayout>

Comment: if i put scrollview in main layout,the layout couldnt display.
Comment: @rajeshlawrance I edit my answer please check it and show me your code please.
Comment: Ya but where is the scrollview ?
Comment: if i put scrollview before my main linearLayout i got the error java.lang.IllegalStateException: ScrollView can host only one direct child
Comment: Check my answer again and apply like that
Answer: you can do this
<your main layout>
<ScrollView>
<LinearLayout>

 your whole layout code here
</LinearLayout>
</Scrollview>
</your main layout>

Comment: i think i have mentioned clearly that you have to add your scrollview after your main layout
Answer: Agreed. It supports only one child. But if you want the above xml file to be scrollable, Put your main Linearlayout inside scrollView tag. The main LinearLayout will be considered as single child of ScrollView.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent" android:layout_width="match_parent"
    android:orientation="vertical"
    >
    <LinearLayout
    android:layout_height="match_parent" android:layout_width="match_parent"
    android:orientation="vertical"
    >
    ...
</ScrollView>

Comment: i changed.but not yet come.i didnt get the scrollview
Comment: The problem is here that, even if you able to use ScrollView for the parent layout, as you are using a List inside the linearlayout, you shouldn't or can't use ScrollView, since, both ScrollView and list have scrolling capability, you should not inflate a list inside ScrollView. Since you are using a list, you don't need to use the ScrollView here.\
Comment: ya.because i am using listview inside my linearlayout.my list is scrolling.but also i want to scroll whole page.so that i can see the whole content using scrollview
Answer:  <?xml version="1.0" encoding="utf-8"?>

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="fill_parent"
         android:layout_width="fill_parent"
        android:orientation="vertical" >

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

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/prev"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_gravity="left"
                    android:background="@null"
                    android:src="@drawable/left_white" />

                <EditText
                    android:layout_width="185dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="25dp"
                    android:hint=""
                    android:visibility="invisible"
                    />

                <ImageButton
                    android:id="@+id/tour"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_gravity="right"
                    android:layout_marginLeft="17dp"
                    android:background="@null"
                    android:src="@drawable/logo" />

                </LinearLayout>
                            <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_gravity="center"
                    >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="What interests you?"
            android:textSize="20dp" >

    </TextView>
    </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="fill_horizontal"
                    android:orientation="horizontal"
                    >

                    <com.android.example.CoverFlow
                        android:id="@+id/coverFlow"
                        android:layout_width="match_parent"
                        android:layout_height="150dip"/>

                </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="17dp"
        >

    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:divider="#b5b5b5"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/list_selector" />

    </LinearLayout>

    </LinearLayout>

    </ScrollView>

Comment: i got the error in line starts with The processing instruction target matching "[xX][mM][lL]" is not allowed.
Answer: You cannot use ListView inside ScrollView. Many of us got this problem. So here is the solution of this question:

Create a list.xml that contains just ListView
Create a header.xml that contains above the XML code of ListView
Create a footer.xml that contains below the XML code of ListView

At last, from your main class grab the ListView reference and add list Header and Footer. Hope this will work. If you have any problem with this make sure your comment.


Opposite events

Question:
Let be A and B two events from the same sample set. If $\space P(A)+P(B)=1$, can one say that they are opposite events?

In my thought:
$\space P(A)+P(B)=1$
$\space P(A)=1-P(B)$
So they are opposite events. But my book says no! It says that is not necessary true.
Can you explain me, why not?
Thanks
Comment: If the event $A$ had $P(A)=1/2$ would you say that $A$ is opposite to itself?
Answer: Suppose that you roll an ordinary six-sided die. Let $A$ be the event that you roll $1,2$, or $3$, and let $B$ be the event that you roll an even number ($2,4$, or $6$). $P(A)=P(B)=\frac12$, so $P(A)+P(B)=1$; are $A$ and $B$ opposite events? (By the way, a better word is complementary events.)
Answer: If B is the complementary event of A, then they satisfy the property:
$P(A)=1-P(B)$
But the reverse (your claim) is not true. The key thing is to realize that events are not necessarily mutually exclusive (disjoint, $P(A\cap B)=\emptyset$).
If they happen to be disjoint, then your claim would indeed be true.
Comment: Both the three answers were good.But this one explained with the mutually exclusive event concept.
Answer: Doesn't work.  Throw a die. The sample space is $\{1,2,3,4,5,6\}$.
Let $A=\{1,2,3,4\}$ and $B=\{1,2\}$.
Then $P(A)+P(B)=1$ but $A$ and $B$ are not complementary events.

Opposite events


Permanent SSH Connection via Bash Script?

Question: I have two OpenSolaris servers.  The remote server has a MySQL database.  MySQL is configured with skip-networking; so, I can't access that database from the first server by simply using the hostname and port.
I must have an ssh tunnel setup to connect to MySQL from the local server.  I can do this with a simple : ssh -L 3350:localhost:3306 user@server and SSH keys.  It works great.
However, I need the connection to be permanent and preferably run on bootup as well.
I've created a little bash script to create the connection.  However, it simply connects, ends the script, and loses the ssh connection.  If I modify the script to run a command on the remote server and and a sleep command, it will stay connected for X seconds.  I can then connect with MySQL.  However, I will surely lose the connection after X seconds.
Does anyone have suggestions for a more elegant way to do this?  Is there someway to use Solaris' svcadm to create the connection and maintain it at all times?
UPDATE : I've discovered that if I add sleep X and then create the MySQL connection before X expires, the ssh connection stays up indefinitely.  However, that is not a very stable solution.  If I lose the database connection, the ssh connection dies and then I'll be stuck without being able to connect again.
Answer: You want the -N option to OpenSSH.  For example:
ssh -N -L 3350:localhost:3306 user@server &

Comment: A nicer options is to use "ssh -T -f -N -L 3350:localhost:3306 user@server", SSH won't use a tty and immediately go into background. For the options to work you'll need public key authentication as you won't have any chance of entering a password
Comment: LOL!  I never thought of that.  I was looking right at it with the man page.  Thanks for the quick answer
Answer: You want to look at a tool like autossh, which keeps an ssh tunnel working. If it dies, it automatically recreates it.
Comment: You could also add the command to inittab so the init process would start it on boot and restart it if ti stops (which would be due to error).
Comment: I can't install autossh or rstunnel.

Check out http://www.jaisenmathai.com/blog/2008/10/10/secure-mysql-replication-between-colos-over-an-ssh-tunnel/comment-page-1/#comment-590 for a great way to maintain the connection permanently.
Answer: I think you can try ssh-copy-id - permits easy propagation of SSH pub/priv keypair
ssh-copy-id -i ~/.ssh/id_rsa.pub email@example.com

Answer: Or maybe rstunnel, which does almost the same as autossh
-> http://sourceforge.net/projects/rstunnel/
Answer: If you want to avoid the overhead of ssh, encryption, authentication, and such, you can do something very similar with netcat (or nc). There are lot of tutorials on the web. If you start with them and need more specific help, let me know.


My SQL query is returned with error saying invalid column name whereas column exists and it works in Oracle SQL dev IDE

Question: The same query is giving correct results in Oracle SQL Developer whereas in Java, it throws an error.
SELECT
    LOGIN_ID AS GROUPID, FILENAME,
    TO_CHAR (CREATEDDATE,'MM/DD/YYYY HH:MI:SS AM') AS CREATDATE,
    CASE STATUS
        WHEN 2300 THEN 'File downloaded and deleted'
        WHEN 2200 THEN 'File Downloaded but not deleted'
        WHEN 2100 THEN 'File Created' END STATUS,UMI,PAYLOAD
FROM
    CC_CCS_SRD_ADM.TB_MAILBOX_OUTBOUND
WHERE
    LOGIN_ID = 'HCLLIN02'
ORDER BY
    CREATDATE DESC

java.sql.SQLException: Invalid column name
at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3757)
at oracle.jdbc.driver.InsensitiveScrollableResultSet.findColumn(InsensitiveScrollableResultSet.java:264)

The results are fetched fine as below in SQL developer:

I tried to run this query in query as mentioned in
SQL Query Error: Invalid Column Name and it went as - Login_ID Invalid identifier .
My modified query is
SELECT *
FROM
    (SELECT
         LOGIN_ID AS GROUPID, FILENAME,
         TO_CHAR (CREATEDDATE, 'MM/DD/YYYY HH:MI:SS AM') AS CREATDATE,
         CASE STATUS
             WHEN 2300 THEN 'File downloaded and deleted'
             WHEN 2200 THEN 'File Downloaded but not deleted'
             WHEN 2100 THEN 'File Created'
         END STATUS, UMI, PAYLOAD
     FROM
         CC_CCS_SRD_ADM.TB_MAILBOX_OUTBOUND) DT
WHERE
    LOGIN_ID = 'HCLLIN02'
ORDER BY
    CREATDATE DESC;

Ok and Here in this edit , adding my java lines:
query = "   SELECT LOGIN_ID as GROUPID, \n" +
                    "        FILENAME,TO_CHAR (CREATEDDATE,'MM/DD/YYYY HH:MI:SS AM') AS CREATDATE,\n" +
                    "\t\t\t\t\tDECODE (STATUS,2300,'File downloaded and deleted',2200,'File Downloaded but not deleted', 2100,'File Created' ) STATUS,UMI,PAYLOAD \n" +
                    "\t\t\t\t\tFROM CC_CCS_SRD_ADM.TB_MAILBOX_OUTBOUND\n" +
                    "\t\t\t\t\tWHERE LOGIN_ID = 'HCLLIN02' ORDER BY createddate DESC";
                    stringBuilder.append(query);
            logger.info("after Append Query ");
        }

        try
        {
            logger.info("inside try block..");
            stmt = con.prepareStatement(stringBuilder.toString());
            System.out.println(stringBuilder.toString());
            rs = stmt.executeQuery();
            while(rs.next())
            {
                //logger.info("inside select success block.."+ rs.getString(groupId));
                TreeMap<String, String> mapToAdd = new TreeMap<String, String>();
                mapToAdd.put("GroupID",rs.getString("GROUPID"));
                mapToAdd.put("FileName",rs.getString("FILENAME"));
                mapToAdd.put("umi", rs.getString("UMI"));
                mapToAdd.put("status", rs.getString("STATUS"));
                //mapToAdd.put("PAYLOAD", rs.getString("PAYLOAD"));
                mapToAdd.put("CreateDate", rs.getString("CREATEDATE"));

Comment: You put your original query in a subquery where "login_id" is now "groupid"...
Comment: It might be helpful to include your Java code as that is what's causing the error. Your SQL isn't erroring; your attempt to get a value form the result set is. I'd guess you're asking for `"LOGIN_NAME"` but as already pointed out you alias that so you need to ask for `"GROUPID"`. Or possibly it's case sensitive, or you just have a typo.
Comment: No, you're confusing several things. p3consulting's comment was about the modified query at the end of your question. You have changed the original query, and you can't refer to `GROUPID` in the `WHERE` clause. Go back to the original query, and change how you retrieve the column value *in Java*. The original query was OK (ish), the issue seems to be that you're doing something like `rSet.getString("LOGIN_ID")` instead of `rSet.GetString("GROUPID")`. If you included your Java code in your question we'd have a clear idea of exactly what you are doing wrong.
Comment: Change `WHERE LOGIN_ID = 'HCLLIN02'` to `WHERE GROUPID = 'HCLLIN02'`
Comment: 1) what is the DDL for the table?  2) if you removed LOGIN_ID from the query does it run (even if you just select a count from the table) 3) Are you sure you're connecting to the same databzse the SQL developer query hits and in same schema?
Comment: yes I am using the same creds as in sql developer - its an admin login. is it supposed to be logged in only 1 instance at a time ? @xQbert
Comment: it says invalid column name - any ways i tried by giving as GroupID still same error - query = "SELECT \n" +
     "LOGIN_ID as GROUPID,\n" +
     "FILENAME,TO_CHAR (CREATEDDATE,'MM/DD/YYYY HH:MI:SS AM') AS CREATDATE,\n" +
     "DECODE(STATUS,2300,'File downloaded and deleted',2200,'File Downloaded but not deleted', 2100,'File Created'\n" +
     ") STATUS,\n" +
     "UMI,PAYLOAD \n" +
     "FROM CC_CCS_SRD_ADM.TB_MAILBOX_OUTBOUND " +

     "   WHERE GROUPID = 'HCLLIN02' ORDER BY createddate DESC\n";  Error : ORA-00904: GROUPID: Invalid Identifier
Comment: yes you are right, I was getting column names which are not in the result set. Thanks much for this point
Comment: Don't think you need the \n \t in the string.  Not sure how the SQL parser will process those.  SUre it makes it easier to read in the  println; but it might be what's causing issue with the SQL.
Answer: You rename your column name.
SELECT *
FROM
    (SELECT
         LOGIN_ID AS GROUPID, FILENAME,
         TO_CHAR (CREATEDDATE, 'MM/DD/YYYY HH:MI:SS AM') AS CREATDATE,
         CASE STATUS
             WHEN 2300 THEN 'File downloaded and deleted'
             WHEN 2200 THEN 'File Downloaded but not deleted'
             WHEN 2100 THEN 'File Created'
         END STATUS, UMI, PAYLOAD
     FROM
         CC_CCS_SRD_ADM.TB_MAILBOX_OUTBOUND) DT
WHERE
    GROUPID = 'HCLLIN02'
ORDER BY
    CREATDATE DESC;

Comment: A guess: GROUPID LOGIN)ID, FILENAME may be a reserved word in the database server.  Oracle database server noted.
Comment: thanks - it is working - the issue is i was trying to find column name which is not part of result set


Prove that $AG \parallel BE$

Question:

As shown in the picture, $E$ is a point outside the square $ABCD$
Connect $BE,CE,DE$, point $F$ is on line $DE$
Connect $AF$ to intersect line $DB$ at point $G$
Suppose that $DE=DB, CE=CF, AG=EB$
Prove that $AG \parallel BE$.

My thinking:
It seems very difficult and complicated to prove without any analytic method. But I believe there must be a proof through pure geometric methods.Unfortunately, I have no idea about how to use the giving conditions still.
P.S.
This question comes from Zhihu (there no one could solve it), and according to a comment, the original source is the junior high third grade research of the junior part of Nanjing Xuanwu Senior High School.

The original Chinese version:
Comment: Please give more information in the posted questoin on the level of the problem, year, book/contest/competition, author (if known), country region or even city, et caetera, this may be useful for later searches. Else, the present problem - as it stays - is not easy to search for, which key-words could be specific? (Square maybe only...)
Comment: Please share what you tried. Also: where did you find this problem?
Answer: Well, let us solve first analytically, then we see why there is no purely geometric argument for a false problem.
We may and do assume that the vertices of the square are
$$
A = (0, -1)\ ,\
B = (1, -1)\ ,\
C = (1, 0)\ ,\
D = (0, 0)\ .
$$
The point $E=(x,y)$ is constrained to be on the circle centered in $D(0,0)$ with radius $DB=\sqrt 2$, so
$$
x^2+y^2=2\ .
$$
The point $F$ is on the line $DE$, so it has the shape $(ax, ay)$ for some scalar $a$. The condition $CE=CF$ becomes:
$$
(1-x)^2+y^2 = (1-ax)^2+a^2y^2\ .
$$
The point $G$ is on $CB$, so it can be parametrized like $G=(g,-g)$.
The colinearity of $A,G,F$ becomes:
$$
0=
\begin{vmatrix}
1 & 0 & -1\\
1 & g & -g\\
1 & ax & ay
\end{vmatrix}
\ .
$$
We have a final condition, $AG=EB$, which translates as
$$
g^2 + (1-g)^2 = (1-x)^2 + (1+y)^2\ .
$$
We obtain a system with four unknowns, and four equations.
There is one more hidden, tacit condition, namely $a\ne 1$.
So $(a-1)$ is invertible, we denote by $b$ the inverse:
$$
(a-1)b=1\ .
$$
Let us solve it the quick and dirty way, (the dirty part is determined by the problem itself, no alternatives to fit here in space,) am using sage code:
R.<x,y,a,b,g> = PolynomialRing(AA)
eq1 = x^2 + y^2 - 2
eq2 = (1 - x)^2 + y^2 - (1 - a*x)^2 - a^2*y^2
eq3 = matrix(3, 3, [1, 0, -1, 1, g, -g, 1, a*x, a*y]).det()
eq4 = g^2 + (1 - g)^2 - (1 - x)^2 - (1 + y)^2
eq5 = b*(a-1) - 1
J = R.ideal([eq1, eq2, eq3, eq4, eq5])

print('The real algebraic points in the variety defined by J are:\n')
for P in J.variety():
    AG_II_BE = bool(matrix([[P[g]    , -P[g] + 1],
                            [P[x] - 1,  P[y] + 1]]).det() == 0)
    print(f'E = {(P[x], P[y])} with minimal polynomials:')
    print(f'{P[x].minpoly()} and\n{P[y].minpoly()}')
    print(f'Is AG || BE? {AG_II_BE}\n')

And we obtain:
The real algebraic points in the variety defined by J are:

E = (-0.3660254037844386?, 1.366025403784439?) with minimal polynomials:
x^2 - x - 1/2 and
x^2 - x - 1/2
Is AG || BE? True

E = (1.366025403784439?, -0.3660254037844386?) with minimal polynomials:
x^2 - x - 1/2 and
x^2 - x - 1/2
Is AG || BE? True

E = (0.5878272310746277?, 1.286257807131656?) with minimal polynomials:
x^8 - 5*x^7 + 21/2*x^6 - 9*x^5 - 9/4*x^4 + 10*x^3 - 51/8*x^2 + 11/8*x - 1/16 and
x^8 + 3*x^7 - 3/2*x^6 - 14*x^5 - 29/4*x^4 + 41/2*x^3 + 149/8*x^2 - 71/8*x - 193/16
Is AG || BE? False

E = (0.06116270957962576?, -1.412890343571248?) with minimal polynomials:
x^8 - 5*x^7 + 21/2*x^6 - 9*x^5 - 9/4*x^4 + 10*x^3 - 51/8*x^2 + 11/8*x - 1/16 and
x^8 + 3*x^7 - 3/2*x^6 - 14*x^5 - 29/4*x^4 + 41/2*x^3 + 149/8*x^2 - 71/8*x - 193/16
Is AG || BE? False

E = (0.3593190440690645?, 1.367804746507810?) with minimal polynomials:
x^8 - 5*x^7 + 21/2*x^6 - 9*x^5 - 9/4*x^4 + 10*x^3 - 51/8*x^2 + 11/8*x - 1/16 and
x^8 + 3*x^7 - 3/2*x^6 - 14*x^5 - 29/4*x^4 + 41/2*x^3 + 149/8*x^2 - 71/8*x - 193/16
Is AG || BE? False

E = (-0.9292974582794979?, 1.066023561669847?) with minimal polynomials:
x^8 - 5*x^7 + 21/2*x^6 - 9*x^5 - 9/4*x^4 + 10*x^3 - 51/8*x^2 + 11/8*x - 1/16 and
x^8 + 3*x^7 - 3/2*x^6 - 14*x^5 - 29/4*x^4 + 41/2*x^3 + 149/8*x^2 - 71/8*x - 193/16
Is AG || BE? False

In other words the claim is false. In best case, it is true with the probability $1/3$, and Schrödinger's cat had a better chance to survive then the author to dodge around this junior high school contest problem.
(There, where i am coming from, all contest get solutions to all the problems, the authors are named explicitly at least after the contest, and they have to provide a solution. Solutions of the participants are also made available, to show that the youth has under time pressure ingenious ideas.)
In order to still do something more geometrically, we will show it is true, when it it true, and make a picture showing it is false, when it is false. We are assuming that by "non-geometric means" (well, it is basicly algebraic geometry used above) the candidate points were isolated as above.
Below, $A,B,C,D$ are always drawn in a fixed coordinate system as mentioned above, so $D$ is placed in the origin, $\omega$ is the circle centered in $D$ with radius $DB$. In particular, any point $E\ne B$ on this circle is exterior to the square $ABCD$.

(1) The points
$$
\begin{aligned}
E_1
&=
\left(\
\frac 12(1-\sqrt 3)\ ,\
\frac 12(1+\sqrt 3)
\ \right)\ ,
\\
E_2 &=
\left(\
\frac 12(1+\sqrt 3)\ ,\
\frac 12(1-\sqrt 3)
\ \right)\ .
\end{aligned}
$$
We construct these points as follows.
Draw the parallel through $C$ to $DB$. It intersects $\omega$ in two points,
$E_1$, $E_2$, which sit in the plane as in the picture, $E_1$ in the second, $E_2$ in the fourth quadrant.

Let us obtain some more information about $E_2$. By definition $\widehat{DCE_2}=135^\circ$. In $\Delta DCE_2$ we know two sides, $DC=1$, $DE_2=DB=\sqrt 2$, so the third one denoted by $s=CE_2$ is obtained from
$$
-\frac {\sqrt 2}2=
\cos 135^\circ=
\frac{1^2+s^2-\sqrt 2^2}{2\cdot1\cdot s}\ .
$$
The positive solution of the obtained equation of second degree $s^2+s\sqrt 2-1=0$ is
$$
s=\frac 12(\sqrt 6-\sqrt 2)=\frac 1{\sqrt2}(\sqrt 3-1)\ .
$$
Then we compute in $\Delta CDE_2$ the angle in $E_2$,
$$
\cos \widehat{CE_2D}
=\frac{2+s^2-1}{2\sqrt 2 s}=\frac{2-s\sqrt 2}{2\sqrt 2 s}
=\frac{2-(\sqrt 3-1)}{2(\sqrt 3-1)}
=\frac{\sqrt 3}2
\ .
$$
So the angle in $E_2$ is $30^\circ$.
The angles in $\Delta CDE_2$ are thus $135^\circ$, $15^\circ$, $30^\circ$.
We find now the location of $E_2$ on the circle $\omega$, and the position of the points $F_2,G_2$ inside the square.
Let $C'$ be the reflection of $C$ w.r.t. the line $DF_2E_2$. So $CF_2C'E_2$ is a rhombus with angles of $60^\circ$ in $E_2,F_2$, so the triangles $\Delta CC'E_2$ and $\Delta CC'F_2$ are equilateral. Since $CF_2E_2$ isosceles, its angle $\hat F_2$ is the same as $\hat E_2$, $30^\circ$. This $\hat F_2$ is also exterior to $\Delta CDF_2$, so in this triangle $\hat C=\hat D=15^\circ$. So this latter triangle is isosceles.
Construct $\Gamma_2$ no so that $C'F_2D\Gamma_2$ is a rhombus. Here is a picture:

Then the rays $DC$, $DF_2E_2$, $DC'$, $D\Gamma_2$ are building angles of $15^\circ$, so $\Gamma_2$ is on $DB$. Since $C'$ is on the angle bisector of $\widehat{E_2DB}$ we also have $C'E_2=C'B$. In the isosceles triangle $\Delta DBE_2$ the angles in $B$ and $E_2$ are $\frac 12(180^\circ-30^\circ)=75^\circ$ each. So the angles in $B$ and $E_2$ in $\Delta C'BE_2$ are $75^\circ-30^\circ=45^\circ$ each. So the third angle is $\hat C'=90^\circ$. We obtain $\widehat{CC'B}=60^\circ+90^\circ=150^\circ$.
So $\Delta CC'B$ has angles $15^\circ$, $15^\circ$, $150^\circ$, same as $\Delta DF_2C$. Each of them has the the bigger side as a side of the square, so the two triangles are equal, and thus they can be obtained from each other by a symmetry rotation of the square.
Since $C'$ is on the side bisector of $BC$ and/or $AD$, and since $\widehat{CDA}=60^\circ$, the triangle $\Delta C'DA$ is equilateral. By rotational symmetry, $\Delta F_2AB$ is also equilateral. The angles $\widehat{DF_2A}$ and $\widehat{DF_2\Gamma_2}$ turn out to be equal, so $A, \Gamma_2,F_2$ colinear. So $\Gamma_2=G_2$, and then $G_2A\|E_2B$, since both lines make an angle of $60^\circ$ with $AB$.
This shows that $E_2$ "works" in the condition of the problem in question.

(The above argument could have been simplified to a few lines, if the configuration of the equilateral triangle $\Delta F_2AB$ inside the square
$\square ABCD$ is known.)

The point $E_1$ then also "works" by a Galois conjugation argument in the field $\Bbb Q(\sqrt 3)$, which moves $\sqrt 3$ to $-\sqrt3$, i.e. exchanges $E_1$ and $E_2$. Geometrically, here is the picture for this situation:

Proof is skipped.
But picture is given to strengthen the point of the view of the asked question. Yes, is there any geometric argument that covers both cases pictured above, where the geometric constellations happens to show $AG\|BE$?
This is a question to the author of this contest problem (and to those who inserted it also in the list - very probable without trying it any ten seconds).

Now let us illustrate in pictures some cases, where the claimed property is false.

The pictures were drawn using geogebra. The four $E$-points were declared with some numerical white noise, the results also get this white noise.
To have a proof for the existence of the one or the other point $E$, for instance $(-1.41289034357124\dots,  \ 0.0611627095796257\dots)$ one can proceed as above, let $E$ run on a small (or big) arc on the circle $\omega$, construct $F\in DE$ so that $\Delta CEF$ is isosceles, then construct $G$ as the intersection $G=AF\cap DB$, then consider the expression
$$
AG-BE
$$
as a function of $E$. It changes sign near the solutions, so a continuity argument is  needed, which is not available for the level the problem was proposed for. All in all, this is a bad problem for a contest, beyond the fact that it is false, this may happen, there is no way to save the day and have at the given level a chance to find out under time pressure that it is false, and possibly have arguments for this.
Comment: It's worth noting that only one of the six possible configurations has $G$ *between* $B$ and $D$ (and $F$ *between* $D$ and $E$), and this is one of the two cases where the parallelism property holds. If, as sometimes happens, the phrasing "on line [$PQ$]" is intended to mean "on *segment* [$PQ$]", then the question is actually valid. ... That said, it's not clear to me how one might structure an argument in which the other configurations are easily dismissed, especially in a time-(and technology-)constrained setting. So, even in "valid" form, the question may not be appropriate for a contest.
Answer: From AG = BE (the given) and AG || BE (a result if true), we must have ABEG being a parallelogram.
This implies CDGE is also a parallelogram with CE || DG and $\angle DCE = 135^0$.
That further means E is not just any point outside the square ABCD.

Prove that $AG \parallel BE$


new instance variable created by eval do NOT cease when eval exit

Question: Note that eval evaluates its code in a temporary scope. eval can alter the value of
instance variables that already exist. But any new instance variables it defines are local
to the invocation of eval and cease to exist when it returns. (It is as if the evaluated
code is run in the body of a block—variables local to a block do not exist outside the
block.)
-- from "the ruby programming" book , chapter 8.3.1
irb(main):001:0> class Point
irb(main):002:1> def initialize(x,y)
irb(main):003:2> @x,@y=x,y
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> p=Point.new(1,2)
=> #
irb(main):007:0>
irb(main):008:0* def get_bind
irb(main):009:1> binding
irb(main):010:1> end
=> nil
irb(main):011:0> b=p.get_bind
=> #Binding:0x81915b0
irb(main):012:0>
irb(main):013:0* eval("@x",b)
=> 1
irb(main):014:0>
irb(main):015:0* eval("@x=10",b)
=> 10
irb(main):016:0> eval("@x",b)

=> 10
irb(main):017:0>
irb(main):018:0* eval("@z=2",b)
=> 2
irb(main):019:0> eval("@z",b)

=> 2
irb(main):020:0> p.instance_variables
=> [:@x, :@y, :@z]
irb(main):021:0>
irb(main):022:0*
irb(main):023:0*

the instance variable "@z" does NOT cease , why ?
Answer: I don't have the book at hand, but that must be a typo. What the author describes is true for local variables in Ruby >=1.9.
1.9.3:
irb(main):001:0> eval("a=1")
=> 1
irb(main):002:0> a
NameError: undefined local variable or method `a' for main:Object
    from (irb):2
    from /Users/apple/.rbenv/versions/1.9.3-p385/bin/irb:12:in `'

1.8.7:
>> eval("a=1")
=> 1
>> a
=> 1

Edit: Here's a more recent version of the book and it says "local variables".


Disable ssh login on specified time for particular users

Question: I would like to disable ssh login to my system for particular users say X,and Y on specified time 17.00 to 18.00.
so how to achieve this ?
any help would be appreciated.
Answer: The answer for this is pam_time[1]
[1]: http://www.linux-pam.org/Linux-PAM-html/sag-pam_time.html "pam_time". As always, be careful editing pam and back up your config.
Comment: I've append this `login & ssh;*;*;Al1700-1800` in /etc/security/time.conf file is it correct ? because it didn't work. @Andy
Comment: @arzyfex - try just restricting one service to get started. Have you actually added pam_time.so into /etc/pam.d somewhere? "it didn't work" could be better expressed with something more specific. Anything relevant in logs? Did you add "debug" to the line in pam.d?
Comment: @Andy : Yeah I've added `pam_time` in /etc/pam.d/sshd file. and I've also checked log but it doesn't have relevant logs that Im looking for. One thing I wanna tell u that I'm new to linux administration, so excuse me for mistakes.
Comment: I understand, but beyond your original question this isn't a good forum to diagnose the issue in detail. I have no idea why it's not working for you, sorry.
Comment: @Andy : Thank you, I understood this very well now.
Answer: Here's an idea:
In sshd_config add the following line:
AllowUsers USER1 USER3 USER3 USER4

This list should be all users you want to be allowed to ssh to the system. After the list is present restart the sshd service. Then add the following lines to roots crontab:
0 17 * * * /usr/bin/sed -i 's/^AllowUsers.*$/AllowUsers USER1 USER2/' && /sbin/service sshd restart
0 18 * * * /usr/bin/sed -i 's/^AllowUsers.*$/AllowUsers USER1 USER2 USER3 USER4/' && /sbin/service sshd restart

That is written for my centos 6 server, but change paths in your crontab as appropriate as well as the sshd restart command. And of course, replace the user lists with the lists you want at each time.
Comment: @Paul : that works but, if same thing I want to achieve using PAM then what would be the method ?
Comment: I only edited the answer (for readability). It is better to direct your questions to the user that provided the answer, in this case that is @Joe.
Comment: @Paul Thanks for your edit. I'm new to this site and now I know the right way to format.
Comment: @arzyfex It looks like Andy has the answer you are looking for in PAM.
Comment: @Joe : It helped me a lot in other problems, thanks for your help.
Answer: It's a bit less granular but I lockout all users except root for maintenance windows on my Linux hosts.
0 17 * * * echo "System maintenance in progress" > /run/nologin
0 18 * * * rm /run/nologin


connect two keyboards to speaker and possibly computer sometimes

Question: I have two keyboards (well, one digital piano and one synthesizer) that both have stereo out jack outputs. How can I connect both to one set of speakers so that I will also be able to record everything on a computer occasionally?
I have nord monitors for the speakers, they take two mono inputs, left and right.
I also have an audio interface that I use to record the guitar sometimes. It's the UA Arrow and has two in and two out connections as well as headphone and guitar. But that's not enough to hook up my two keyboards because each has 2 out jacks...
I don't think I can I make a connection like keyboard1->keyboard2->speakers because both keyboard's only in connection is midi in and I want to move the sound and not just the midi data.
What about a DI box?
Comment: Unless these are powered speakers, you'll need an amp. too.
Comment: Ill go with the mixer. I was thinking of Behringer MX 400 which has enough inputs but it outputs just in mono, so I might get a Xenyx 502
Comment: You will still need an amp if the speakers are passive! And it's worth considering an 802 to make it futureproof. Mics, guitars, another keyboard - you never know.
Comment: I think they're active speakers
Comment: Active speakers need to be plugged into mains power  normally.
Comment: Is there a difference between "mains" power and just "power"?
Comment: Not particularly - mains power is from the socket in the wall, power may be from batteries, or a wall wart.
Answer: In any case you'll have to buy something. You have basically two options: (1) mixer, or (2) new audio interface with more inputs and routing options.
If you want to keep your current audio interface, you could use a mixer that has enough inputs for all of your devices, and a possibility to route the signals you want to record into the audio interface, and route the output of your audio interface to the monitor speakers and headphones without routing them to the audio interface's input, which would create a feedback loop. One way to do this is with two "aux" outputs. Each channel you want to be able to record, you route to an aux output. And the audio interface's output, you connect to an input that you do not send to the mixer's aux outputs. This setup can be nice, and sometimes it's handy to have some tactile control over what's happening. And you'll be able to play, mix and change levels etc. without having your computer switched on or even connected at all. But you'll have more stuff on your desk, more cables and everything ... and you won't be able to record all of the devices on separate tracks simultaneously.
A different kind of option is a new audio interface that has more inputs, for example Steinberg UR44 or Focusrite Scarlett 6i6 or something even larger. With a bigger audio interface, you can have all your devices connected and routed to separate tracks in a recording software all the time. Reading your description, this might not be needed really. But then again, if you want to play guitar as well - depending on what kind of guitar it is, you might want to take advantage of an audio interface's "Hi-Z" inputs, which are needed for many electric guitars and basses.


A relationship between programming graphs and probability axioms.

Question: I've been looking into both Computer Science algorithms as well as working through a book for applied probability for programming. And I think I thought there might be an interesting interpretation when we have probabilities larger than $1$. As strange as it sounds.
There is a well known algorithm that computes, given an adjacency matrix for a graph, in how many ways it is possible to get to node $B$ from node $A$. What we do is simply compute the adjacency matrix $M$ to the $n-th$ power. The entry $i,j$ will answer how many ways, going around the graph, it is possible to get exactly to the node.
On the other hand, people less concerned with algorithms for graph problems are better acquainted with the probability transition matrix, which for each row $i$, the $j-th$ entry contains the probability $P_{i,j}$, which is the probability of being in state $i$ and then moving to state $j$. In this case, each row of the matrix adds up to one: eventually the node will transition to some other state.
Let's look at an example of an adjacency matrix:
$$
A =
\left(
\begin{array}{ccccc}
1 & 1 & 0 & 1 & 0 \\
0 & 1 & 1 & 0 & 0 \\
1 & 0 & 1 & 0 & 0 \\
1 & 0 & 0 & 0 & 1 \\
1 & 1 & 1 & 0 & 1 \end{array}
\right)
$$
So the graph looks like:

On the other hand, a probability transition matrix may look like:
$$
T =
\left(
\begin{array}{ccccc}
.3 & .3 & 0 & .4 & 0 \\
0 & .5 & .5 & 0 & 0 \\
.7 & 0 & .3 & 0 & 0 \\
.1 & 0 & 0 & 0 & .9 \\
.25 & .25 & .25 & 0 & .25 \end{array}
\right)
$$
Let's try calculating the $3$rd power.
$$
A^3 =
\left(
\begin{array}{ccccc}
 5 & 5 & 4 & 2 & 2 \\
 3 & 2 & 3 & 1 & 0 \\
 4 & 3 & 2 & 2 & 1 \\
 5 & 5 & 4 & 2 & 2 \\
 8 & 7 & 7 & 3 & 2 \\
\end{array}
\right)
$$
$$
T^3 =
\left(
\begin{array}{ccccc}
 0.1755 & 0.1575 & 0.1485 & 0.036 & 0.1485 \\
 0.189 & 0.09 & 0.081 & 0.063 & 0. \\
 0.21 & 0.189 & 0.09 & 0.126 & 0.189 \\
 0.29325 & 0.20925 & 0.20025 & 0.0765 & 0.08325 \\
 0.265625 & 0.173125 & 0.143125 & 0.09375 & 0.083125 \\
\end{array}
\right)
$$
The way to interpret $A^3$ and $T^3$ is oddly similar. Which led me do the question: could we interpret the adjacency matrix as a "parallel worlds" matrix? That would be equivalent to saying that we take all the routes in every step. Which makes some sense.
I have yet to find other processes or algorithms that seems to exploit this "parallel world" probability version.
We could say that if $P(A) = 2$, then the event $A$ will happen in two different $\Omega$. This is over the board, but if someone can make some sense, or completely refute it, I'd appreciate the comments. Is this possible? Is it nonsense?
Answer:
Which led me do the question: could we interpret the adjacency matrix as a "parallel worlds" matrix?

Pretty much so.
A transition matrix is an adjacency matrix but one where the paths from any node are assigned probability measures rather that a Boolean.
An adjacency matrix merely reports which paths are open.   A transition matrix further measures how likely the paths are to be taken at any junction.


Is it OK to use the parameters for the lowest cost?

Question: In a Neural Network training, the cost of the model changes throughout the training process when using gradient descent (or something analogous), this is the point of the algorithm. However the cost might not decrease monotonically. In some points the cost might even increase.

So, Is it OK to keep track of the parameters that output the lowest cost and use those as the best parameters for the model? In the image it would imply using the parameters that output the lowest cost instead of the last parameters found.
Assume that the cost returned by the lowest cost outputs an acceptable model accuracy.
Does doing this cause some kind of problem?
Comment: It is all approximations.  That means it isn't going to be "true" lowest cost, but estimated lowest cost.
Answer: This is a perfectly okay thing to do. Other things you might try are reducing the learning rate to make it more likely that the training cost goes down most of the time and also selecting the model based on the cost on the validation set rather than training.
Comment: Great, thanks for your insights. Do you have any source you can provide to validate this?
Comment: What about mini-batch? After each epoch should I do a comparison? Or how would this work?


Howto prevent snmpd cache results from extend script

Question: On my freebsd box i have snmpd daemon from net-snmp-5.5_4 packet.
In snmpd.conf file i have:
<code>extend .184.108.40.206.4.1.2021.2 inpt /usr/bin/perl /usr/local/etc/snmp/mpdinput.pl
</code>
where mpdinput.pl my script which return input-octets from some mpd5-ng network  interface.
Cacti reading this value and plot such graphs:

I added red lines to graph.
In marked places snmpd cache the result and return prev values, thats why rrdtool plot zero activity. And its a problem for me.
How to prevent caching result in snmpd?
Answer: according to the snmp documentation;
`extend [MIBOID] NAME PROG ARGS works in a similar manner to the exec directive, but with a number of improvements. The MIB tables (nsExtendConfigTable etc) are indexed by the NAME token, so are unaffected by the order in which entries are read from the configuration files. There are two result tables - one (nsExtendOutput1Table) containing the exit status, the first line and full output (as a single string) for each extend entry, and the other (nsExtendOutput2Table) containing the complete output as a series of separate lines.
If MIBOID is specified, then the configuration and result tables will be rooted at this point in the OID tree, but are otherwise structured in exactly the same way. This means that several separate extend directives can specify the same MIBOID root, without conflicting.
The exit status and output is cached for each entry individually, and can be cleared (and the caching behaviour configured) using the nsCacheTable.`
Comment: Yes! Looks like what i need. But: snmpset -v 2c -c private 127.0.0.1 NET-SNMP-AGENT-MIB::nsCacheTimeout.188.8.131.52.4.1.2021.2 i 1  return : Reason: noCreation (That table does not support row creation or that object can not ever be created)
Failed object: NET-SNMP-AGENT-MIB::nsCacheTimeout.184.108.40.206.4.1.2021.2
 dont understood why...
Comment: @KorjavinIvan try to list your mibs first ; `root@ubuntu:/etc/snmp# snmpwalk -v1 -c public localhost 184.108.40.206.4.1.8072.1.5.3.18.104.22.168.220.127.116.11.2.2
iso.188.8.131.52.1.8072.1.5.3.220.127.116.11.22.214.171.124.2.2 = INTEGER: 2
` and then change with `root@ubuntu:/etc/snmp# snmpset -v2c -c public localhost iso.22.214.171.124.1.8072.1.5.3.188.8.131.52.126.96.36.199.2.2 i 1
iso.184.108.40.206.1.8072.1.5.3.188.8.131.52.184.108.40.206.2.2 = INTEGER: 1` if you have any problems with the listing, check comunity access.
Comment: Thanks. I mean: tryed modify all nsCacheTimeout which discovered by snmpwalk, set to 1. But graphs not changed, that why i suppose may be i must create some oid with combination of nsCache and my custom oid for extend - but thats not work.
Comment: @KorjavinIvan what is your NET-SNMP-AGENT-MIB::nsCacheEnabled.0 value ? if this value is true, you could try to disable it ant then check again .
Comment: i just check : NET-SNMP-AGENT-MIB::nsCacheEnabled.0 = INTEGER: false(2) and i never change this. So,looks like i am on wrong way?
Answer: If you can use your perl script as an extension in snmpd then you should also be able to use it as a direct data source in Cacti as well, skipping snmpd altogether.
Comment: sorry, but its a pain. snmpd do alot for me. Auth, check ip, network daemon, etc..  I really dont want reiplement that. (And i know that with perl +mojo its simple, but i want one solution for all monitoring tasks)


Access denied when I try to delete the user menu

Question: How can I delete a user menu in Drupal 7? It throws me an access denied error, though I am logged in as user #1. What's wrong with it?
Answer: You cannot delete any of the following menus; they come with the core. You can delete any menu you created yourself.

To disable them from displaying go to admin/structure/block.
Comment: Disable it in your theme settings.
Comment: From the description given by the OP this is not the problem, as for those menu there isn't a delete link. If the OP would write the URL directly on the browser's URL field, then he should get an "page not found" error, not the "access denied" error.
Comment: There's no blocks in regions that displays @user_menu@
Answer: maybe swithing for another source for secondary links at admin/structure/menu/settings is enough ?
OR
depending on theme you are using: go to theme's .info file and delete secondary links feature // change theme settings


Better mongo db schema to store user related info

Question: Let say I have to store in mongoDB some data (say 'state') related to a particular User account.
What is the better option to do this: Collection state:
1) {id: - that will be the same in Users collection, [state data]}
2) {id: - some unique id for state, userId - id from user collection, [state data]}
So what would be better design decision?
Comment: What is your state data looks like?
Comment: Let say I'm going to store list of items bought by user and some user specific info about each Item.
Answer: It all depends on how you'll be accessing your data for your application. If every time you need to access the state, you will be fetching the User data anyway, it makes sense to store it in the User collection.
Think of Blogs and their Comments, you would never need to fetch the comments just by themselves, you always fetch the Blog entry and the related comments.
EDIT:
Some considerations:

If these lists that you speak of are all independent of each other, and there needs to be no referential integrities between lists of each user, then it makes sense to store them in Users collection
If you think User document will be bigger than 16MB (current max document size) then you might have to consider separating the collections (ie not storing all of the data in one place, Users collection)
Comment: What if I'm going to store list of items bought by user and some user specific Info about each Item? Is it good Idea to store all that stuff in User collection?


Docker SCRATCH container can't find files

Question: I have a very simple dockerfile:
FROM scratch
MAINTAINER "aosmith"
EXPOSE 6379
ADD redis-server /redis-server
ENTRYPOINT ["/redis-server"]

The docker file is in a folder with a statically compiled copy of redis-server.
The build runs find but the container refuses to start:
➜  redis git:(master) ✗ docker run f16
no such file or directory
Error response from daemon: Cannot start container 46be4ed97560cd63fa4f639bed0e25358e807a8229bb3b5a613aa1274e037040: [8] System error: no such file or directory

I've tried various combinations of CMD EXEC ADD and COPY with no luck.
I'm building redis from source like this:
make CFLAGS="-static" EXEEXT="-static" \
MALLOC=libc LDFLAGS="-I/usr/local/include/"

Worth noting I use basically the exact same Dockerfile for go projects without any problems.
Any ideas?
Answer: The "scatch" image is literally empty and can only be used by technologies like go which have near zero dependencies on it's runtime environment.
Try a base image that supplies a set of OS utilities, like bash, etc.  For example
FROM ubuntu
MAINTAINER "aosmith"
EXPOSE 6379
ADD redis-server /redis-server
ENTRYPOINT ["/redis-server"]

Comment: Be careful of over optimization. Docker is a layered file system, the base image will only be downloaded once, no matter how many containers use it. It's why "ubuntu" is not always a bad choice (used by lots of projects). There are smaller base images (eg busybox, or alpine) that give you a bare minimum environment that just as valid a choice. In conclusion "scratch" is only suitable for technologies that truly have zero dependencies. Very few script languages easily fall into this category (Go being an obvious exception).
Comment: I get the idea that scratch depends on nothing, that's why I'm doing static compilation.  This image is going to be several hundred megs whereas the scratch image is only ~20mb.
Comment: @Mark-OConnor I'd only try this with go or c/c++.
Comment: @AlexSmith Understood, but clearly the redis launch is looking for a file that it expects to exist on the target environement. Your next job will be to look thru the source code for this reference. You could also look at what base containers the Redis guys themselves are using.
Comment: Ah I should have included this... the docker run fails because it can't find the entrypoint:
`no such file or directory
Error response from daemon: Cannot start container d70e6f0fbc9965cf8c09c8c61d8e116c7e8b7ea9ee441783020acd297bbfc597: [8] System error: no such file or directory`


AjaxCommand to scroll to top of page?

Question: I have created an AJAX form. After the form is submitted, I call $this->createOkResponse() which updates a section of the page with a message (it replaces the form with the message).
It all works fine except for one thing: the form is quite long and the submit button is at the bottom. When I replace the page content with my ReplaceCommand() the message I am showing comes at the top of the page, but the page doesn't scroll up to the top.
Then I thought I could use a ScrollTopCommand() to force the page to scroll up but it doesn't work. I have checked that the body does have the scroll bars. I have tried with body with html and with html, body as the selector but nothing works.
I have cleared the caches multiple times and I get no error in the error logs or the Javascript console. Just the page doesn't scroll up and my OK message remains hidden until I manually scroll up.
I see that ScrollTopCommand is part of Views. Does that prevent me from using it in a form?
Edit: I have also tried setting views as a dependency in my module info YAML file but that didn't change anything.
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\views\Ajax\ScrollTopCommand;

/**
 * Class InterventionForm.
 */
class InterventionForm extends FormBase
{

  /**
   * Function to update the page with AJAX with an OK
   * @retun $response AjaxResponse
   */
  protected function createOkResponse(): AjaxResponse
  {

    $response = new AjaxResponse();

    $render_array = [
      '#markup' => '
OK Response!
'
    ];

    // Replace content with our response
    $response->addCommand(new ReplaceCommand('#interventionCreate', $render_array));

    // Scroll to top of page
    $response->addCommand(new ScrollTopCommand('body'));

    return $response;
  }
}

Answer: The module dependency doesn't help because Views is already installed by default, but keep it in case someone tries to uninstall the module. The next step is then to include the modules ajax library in buildForm():
$form['#attached']['library'][] = 'views/views.ajax';

Comment: No, ajax.js only runs commands which exist in the command list client-side. Check the new 9.5.x version, much cleaner and easier to read than previous releases: https://git.drupalcode.org/project/drupal/-/blob/9.5.x/core/misc/ajax.es6.js#L1001
Comment: Ah! Thanks a lot! Works like a charm :)
Comment: Should it not trigger a JS error when the library is not included?

AjaxCommand to scroll to top of page?


Give an example of three different points in $\mathbb R^3$ such that there are infinitely many planes in $\mathbb R^3$ passing through all of them.

Question: A past exam question. I'm not certain on the meaning.
I assume it wants a $3$ points on a straight line, one which case there would be infinitely many planes passing through all of them. But that seems too easy as you could make up any straight line equation and find $3$ points which fit it.
Do you have an alternative interpretation?
Note: $\mathbb R^3$ is the vector space.
Comment: I agree with your interpretation.
Comment: This is exactly what they're asking. In fact if they are noncollinear then there can only be one plane passing through all of them.
Answer: you'll get infinitely many planes if and only if they lie on a straight line. It is commonly said that "3 points define a plane" but this is true only if they aren't on a straight line.


NotFoundHttpException after changing webiste hosting, sub-domain

Question: After changing webiste hosting (Drupal 8) I got this very un-intuitive error message:
The website encountered an unexpected error. Please try again later.

Symfony\Component\HttpKernel\Exception\NotFoundHttpException: in Drupal\Core\PathProcessor\PathProcessorFront->processInbound() (line 43 of core/lib/Drupal/Core/PathProcessor/PathProcessorFront.php).

Drupal\Core\PathProcessor\PathProcessorManager->processInbound('/', Object) (Line: 157)
Drupal\Core\Routing\RouteProvider->getRouteCollectionForRequest(Object) (Line: 141)
Symfony\Cmf\Component\Routing\NestedMatcher\NestedMatcher->matchRequest(Object) (Line: 258)
Symfony\Cmf\Component\Routing\DynamicRouter->matchRequest(Object) (Line: 185)
Symfony\Cmf\Component\Routing\ChainRouter->doMatch('/', Object) (Line: 155)
Symfony\Cmf\Component\Routing\ChainRouter->matchRequest(Object) (Line: 84)
Drupal\Core\Routing\AccessAwareRouter->matchRequest(Object) (Line: 154)
Symfony\Component\HttpKernel\EventListener\RouterListener->onKernelRequest(Object, 'kernel.request', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.request', Object) (Line: 120)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 62)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 628)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

The files are all the same, other files on ftp/website (besides Drupal) are accessible and works fine.
I've cleared cache via Admin Panel before moving anything.
I've also tried remove it manually:
TRUNCATE cache_config;
TRUNCATE cache_container;
TRUNCATE cache_data;
TRUNCATE cache_default;
TRUNCATE cache_discovery;
TRUNCATE cache_dynamic_page_cache;
TRUNCATE cache_entity;
TRUNCATE cache_menu;
TRUNCATE cache_render;
TRUNCATE cache_toolbar;

but I guess it's not the point.
The previous website was working on http://someurl1.com/site/ (sub-domain) and new one is under http://someurl.com/ (top domain). But even moving everthing on new hosting to /site/ doesn't change a thing.
When I changed database connection settings in settings.php to something wrong intentionally I get:
PDOException: SQLSTATE[HY000] [1044] Access denied for user...

So I guess it's not the database connection problem either.
Using http://.../core/rebuild.php also didn't help.
Comment: This `NotFoundHttpException` error happened to me after TRUNCATEing all cache* db tables. `drush cr` was no help. It went away when copying the full database with all data.
Comment: Have you checked the apache logs (or what webserver you use) ? Is mod_rewrite enabled ?
Comment: I have the same problem with drupal 8 installations. when I add /install.php to my url i got the drupal installation page and works good to me. http://localhost/mydrupal8website/install.php
Comment: Did you check all paths and names and host patterns in settings.php?
Answer: Check database connection through drush sql-cli and show tables, I got the same error because of an empty database once.
If the data is of no importance, you can run drush site-install to re-install Drupal from scratch.
Answer: I just had this after transferring an entire database (Drupal 8.8.1) from production back to dev.
Same error on every single route I tried.
Clearing the cache alone didn't fix it, but composer dump-autoload ("Generated autoload files containing 565 classes") followed by drush cr did.


Borel Hierarchies

Question: I have started reading about Borel Hierarchies. If I understand correctly, Then $\Sigma_2^0$ is the collection of all sets of the form $A=\bigcup_{n \in \omega}{B_n}^c$, where $B_n$ is open. also, $\Sigma_3^0$  is the collection of all sets of the form $A=\bigcup_{n \in \omega}{B_n}^c$, where $B_n$ is in $\Sigma_{2}^0$ or in $\Sigma_{1}^0$. I can think of a set which is in $\Sigma_{3}^0$ and not in $\Sigma_{2}^0$ or in $\Sigma_{1}^0$, for example, the set, $(-\infty,0] \cup \bigcup_{n \in \mathbb{N}} (\frac{1}{n+1},\frac{1}{n}) \cup (1,\infty)$. But, is there an example of a set which is in $\Sigma_{4}^0$ but not in $\Sigma_{3}^0$ or in $\Sigma_{2}^0$ or in $\Sigma_{1}^0$?
Thank you!
Shir
Answer: You cannot construct a $\Sigma_4^0$ Borel set, for one major reason, it is consistent with the failure of choice that every set is $\Sigma_4^0$.
The main method of showing that there are such sets is to prove the existence of $\Sigma^0_n$-universal sets. You can read my question, and answer, about those here.
Comment: @Andres: Potato, tomato. Both are from the solanum family. :-) I suppose it's that time of the year where I'm somewhat more absent minded and less careful in my choice of words. I'll try to pay more attention to that in the future.
Comment: This is getting more and more fascinating.. I am not sure I yet have the mathematical maturity to fully understand what you wrote there but I'll give it a try. Thank you!
Comment: Of course you can construct the sets, Asaf. What you cannot do is to prove without some appeal to choice that they are properly $\Sigma^0_4$.
Comment: You mean "it is consistent with the failure of choice that every set is $\Sigma^0_3$", (not $\Sigma^0_4$), right?
Comment: @Alex: The question was about $\Sigma^0_4$. I am not claiming this is optimal.
Comment: @Asaf Sure! I just meant that to show that you need choice to exhibit a properly $\Sigma^0_4$ set, you need to know that it's consistent with ZF that every $\Sigma^0_4$ set is $\Sigma^0_3$.

Borel Hierarchies


Apache with suPHP does not honor php_admin_value in vhost config

Question: I just found out that overriding php.ini settings in the vhost configuration does not work on Apache with suPHP.
The system is Ubuntu Server Edition 14.04 LTS and I installed Apache + PHP like this:
apt-get install apache2 apache2-mpm-prefork apache2-utils php5

Without changing any settings, the following vhost works out-of-the-box:
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /var/www/html/public
    php_admin_value open_basedir /var/www/html
</VirtualHost>

One can verify that the "open_basedir" setting is honored with phpinfo().
However, after installing suPHP and suExec:
apt-get install libapache2-mod-suphp apache2-suexec
a2enmod suexec

phpinfo() shows that the values are still those set in "/etc/php5/cgi/php.ini".
This is true not only for open_basedir, but for any setting I try to override with either php_admin_value or php_value.
Is there any way to make the above setup work?
Answer: The documentation at http://www.suphp.org/FAQ.html says suPHP doesn't support the php_admin_value directive.  You might be able to accomplish something similar using a custom php.ini file and the suPHP_ConfigPath directive though (see http://www.suphp.org/DocumentationView.html?file=apache/CONFIG).
Comment: @matpen Were you able to get this resolved?  If so, was this answer helpful?  Thanks
Comment: After researching and testing, I have concluded that the method you suggest is the only viable one. Unfortunately, the main configuration file `/etc/php5/cgi/php.ini` is ignored if the `suPHP_ConfigPath` is specified.
Comment: Will the php.ini specified with `suPHP_ConfigPath` merge with `/etc/php5/cgi/php.ini`. or completely override it? That is, will values unset in `suPHP_ConfigPath` fall back to defaults or to the values set in `/etc/php5/cgi/php.ini`?
Comment: @matpen I'm not sure off the top of my head, but since you're already running it, just capture the output of `` before and after using that directive, and paste the output into WinMerge to easily see what's changed.


Show that no set of nine consecutive integers can be partitioned into two sets with equal products

Question: This problem is meant to be proved using contradiction and I've admittedly been stuck on it for some time. I'd really appreciate a point in the right direction, because it honestly feels like I've been getting nowhere.
This is all I've done:
Assume $x$ denotes the set of nine consecutive integers, and $x_1$ and $x_2$ denote the two partitions. We would have:
$$\prod_{a\ \in \  x_1} a= \prod_{a\ \in \  x_2} a$$
$\implies$ $\prod_{a\ \in \  x_1} a$ and $ \prod_{a\ \in \  x_2} a$ are both even numbers, since both sets $x_1$ and $x_2$ would require to contain strictly odd numbers to have a product that is odd.
$\implies$ $x_1$ has at least two elements, one odd and one even.
$\implies$ $x_2$ has at most seven elements.
And that's where I hit a dead end. I thought perhaps I could represent the set $x$ as
$$x := \{ (a_0, \ (a_0 +1), \ (a_0 +2), \ \dots , \ (a_0 +8) \}$$
where $a_0$ is any integer, then multiply things out and show in each case one side is less than the other, but then I noticed that'd be an extremely tedious route and that some of the more ugly expansions deserve some sort of proof by induction themselves to show whether or not they actually hold.
Please don't supply a proof that relies too heavily on a Number Theory-approach, as I don't have a background in that area therefore shouldn't rely on Theorems there to prove this.
Answer: Suppose we partitioned the set into two sets $A,B$ such that $ A \cup B= \{a, a+1, a+2 ... a+8 \} $ such that the product is equal: $$ \prod_{n\in A}n=\prod_{n\in B}n $$
Here multiplying both sides by $\prod_{n\in B}n $ leads to
$$ \left(\prod_{n\in A}n\right)\left(\prod_{n\in B}n\right)=\left( \prod_{n\in B}n \right)^2 \ \ \ \Longrightarrow \ \ \ a(a+1)(a+2)...(a+8)=\left( \prod_{n\in B}n \right)^2 $$
But by Erdos-Selfridge theorem the product of two or more consecutive integers is never a power, I.E. we have contradiction
Comment: Is there a proof that wouldn't rely on that theorem?
Comment: @AndrewTawfeek Yes there certainly is.  See the answers to the duplicate.


Class arguments not being passed

Question: Updating the emulsify_twig extension for D10, for some reason the class arguments aren't being passed... I'm extending Drupal\Core\Template\TwigExtension and using the same default arguments but for some reason it isn't picking up the arguments, resulting in: ArgumentCountError: Too few arguments to function Drupal\emulsify_twig\BemTwigExtension::__construct(), 0
This is the relevant code from the class:
use Drupal\Core\Template\TwigExtension;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;

class BemTwigExtension extends TwigExtension {
  protected $urlGenerator;
  protected $renderer;
  protected $themeManager;
  protected $dateFormatter;
  protected $fileUrlGenerator;

  public function __construct(RendererInterface $renderer, UrlGeneratorInterface $url_generator, ThemeManagerInterface $theme_manager, DateFormatterInterface $date_formatter, FileUrlGeneratorInterface $file_url_generator = NULL) {
    parent::__construct(
      $renderer,
      $url_generator,
      $theme_manager,
      $date_formatter,
      $file_url_generator
    );

    $this->renderer = $renderer;
    $this->urlGenerator = $url_generator;
    $this->themeManager = $theme_manager;
    $this->dateFormatter = $date_formatter;
    $this->fileUrlGenerator = $file_url_generator;
  }

Any insight as to why the arguments aren't being passed would be appreciated!
Comment: We would have to see the code that is instantiating the object. Is this a a service? Can we see the service definition please?
Answer: The error message states that no arguments are being passed to the Twig filter extension. Drupal Twig filter extensions must be defined in [MODULE].services.yml, and in that service definition, the services to be injected as dependencies are defined. These are the arguments that BemTwigExtension::__construct is expecting, but not finding.
You could do something like the followingin emulsify_twig.services.yml:
services:
  emulsify_twig.bem_twig_extension:
    class: Drupal\emulsify_twig\TwigExtension\BemTwigExtension
    tags:
      - { name: twig.extension }
    arguments:
      - '@renderer'
      - '@url_generator'
      ...

Entering all the dependencies using their service definition names.
However, in the original post, as __construct is passing the arguments to the parent, the parent service definition can be referenced to determine which arguments it expects. In the original ploblem TwigExtension is being extended, and the TwigExtension API page lists one service as using it: twig.extension. Viewing the source on twig.extension API page shows the services expected by the parent class (the arguments):
class: Drupal\Core\Template\TwigExtension
arguments:
  - '@renderer'
  - '@url_generator'
  - '@theme.manager'
  - '@date.formatter'
  - '@file_url_generator'
tags:
  - { name: twig.extension, priority: 100 }

Then, instead of redefining all these arguments in the extended service definition, the parent can be set, using its arguments and tags as a base. So, back to emulsify_twig.services.yml the service would be defined as follows:
services:
  emulsify_twig.bem_twig_extension:
    class: Drupal\emulsify_twig\TwigExtension\BemTwigExtension
    parent: twig.extension
    arguments:
      - '@additional_class.1'
      ...

Where @additional_class.1 is the name of any additional services that the extended class will use, that the parent does not. Note that if the extended class does not require any additional services, the __construct() method does not need to be implemented in the extended class at all, as the parent implementation will be used when not defined in the child class. However, the service will still need to be defined in emulsify_twig.services.yml with the parent set as twig.extension.
Comment: Yep- the only lines I kept were the class declaration & use TwigExtension, then added  parent in services... thanks again!
Comment: Setting the `parent` key in services was the missing piece... and removing the constructor cuts the amount of code in half - thanks!
Comment: You also don't need to declare the protected properties, as the parent defines them as well.

Class arguments not being passed


What is fitted in a GARCH: residual or log-return?

Question: Given a time-series of log-return of SP500, then to obtain the volatility process what should we do? Some people say that we need using the ARMA model to withdraw the residual series, then plug this residual series into the GARCH model to obtain the conditional variance process? Or directly plug the log-return process of SP500 into the GARCH model to obtain the conditional variance?
I saw that in the book Introduction to Time Series in R, the author fits simulated residuals in GARCH model then he fits SP500 log returns in the GARCH model.
I got confused...
Answer: If you use the log returns, you're essentially making the assumption that there is no conditional variation in the mean. In some circumstances you may want to explicitly model both, but other times it may be sufficient to assume a constant mean and focus on the conditional variance. Depends on what you're trying to do.
In addition, if you fit a GARCH model with raw log returns, then you're also implicitly assuming the mean is zero. Centering the data may be important if the mean is large (i.e. especially in lower frequency data).
Comment: I basically want to use an ARMA-GARCH model to model both my mean equation and variance equation. How should I proceed?Can you explain what you mean by no conditional variation in the mean?What would be the implication of that?
Comment: Can you explain mathematically what you meant? thanks
Comment: Basically if the mean varies over time, one way to model that is with ARMA. The residuals from a ARMA regression would (ideally) no longer have a time-varying mean. If it is important to do both, then you can take a pseudo-maximum likelihood approach, fit the ARMA model, get the residuals, then fit the Garch model to the residuals.
Comment: you mean if the series is not stationary in its mean?isn't there differencing for that?
Comment: If I fit the garch model to the log-returns, what would I get?the variance equation?is it possible to get the mean equation if I do that?                                                            If I first model the series using an ARMA,then model the residuals using a GARCH model, would'nt I need to re-estimate the parameters of the ARMA model?I saw in Statistics and Data Analysis for Financial Engineering talking about something like that but i'm not sure. thanks
Comment: I'm trying to discuss it generally. For stock indices, log returns are probably fine by themselves, but you have seemed to really emphasized the importance of ARMA, which could adjust a wide classes of processes for stationarity. You wouldn't need to re-estimate the ARMA model if you're doing pseudo-ML, but a Bayesian estimation or full ML would do both parts together. Why don't you just download a garch package for R or something and play around with it?
Comment: I tried a few garch packages here is what I found. I use SP500 data from the MASS package and tried fitting a garch model to it.Then I used forecast package to fit an arima model to the SP500 data, I then applied a garch model to the residual of the arima model. I get more or less the same coefficients in both cases. But my aim is to ultimately forecast return so I would need my equation for mean return. What do you mean the full ML do both parts together?you mean both the ARMA and GARCH model are estimated in parallel?Is that what is done in garchFit function from fGarch package?thanks
Comment: can I have an answer to that please?"What do you mean the full ML do both parts together?you mean both the ARMA and GARCH model are estimated in parallel?Is that what is done in garchFit function from fGarch package?" thanks
Comment: I mean that your maximum likelihood estimator is calculating all the coefficients to the model at once. Most people don't do it that way. They estimate the ARMA first, then the GARCH. It makes the calculations easier and is good enough.


Transform - a mutating sequence algorithm

Question: I know transform algorithm in C++ is a mutating sequence algorithm. But I have never seen anyone using transform for the purpose of mutating a sequence. Whenever I search for a sample code on internet, what I get is the transform algorithm used similar to for_each algorithm.
Please provide a link or an example , where I can understand the mutating sequence nature.
Edit: I got more confused when I went through  This SO question.
it says for_each is a non-modifying sequence algorithm. So I can modify the elements with for_each not the structure of the container.Is the provided answer incorrect. If for_each can also modify the element, we can replace for_each with transform,and there is no need of for_each algorithm except that it's implementation may be simple.
Answer: mutating sequence algorithm means algorithm will change (modify) the container it works on. In example below, the container foo of type std::vector is modified.
std::string s("hello");
std::vector<int> foo;
std::transform(s.begin(), s.end(), back_inserter(foo), ::toupper);
std::cout << std::string(foo.begin(), foo.end());

The output is "HELLO"
This is not possible with std::for_each

Yes that provided answer sounds correct to me. Also check the list of Nonmodifying Sequence Algorithms and Mutating Sequence Algorithms in C++ to verify your assertions.
Comment: Those two links are not accessible.
Comment: In above example, if I create my own toupper function and pass in for_each function, it does the same operation, doesn't it?
Comment: @InQusitive Possibly links are forbidden in your geography.
Answer: Here is a simple example
#include <iostream>
#include <algorithm>
#include <iterator>

int main()
{
    int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    for ( int x : a ) std::cout << x << ' ';
    std::cout << std::endl;

    std::transform( std::begin( a ), std::end( a ), std::begin( a ),
                    []( int x ) { return x * x; } );

    for ( int x : a ) std::cout << x << ' ';
    std::cout << std::endl;

    return 0;
}

The output is
1 2 3 4 5 6 7 8 9 10
1 4 9 16 25 36 49 64 81 100

The same can be done using algorithm std::for_each
#include <iostream>
#include <algorithm>
#include <iterator>

int main()
{
    int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    for ( int x : a ) std::cout << x << ' ';
    std::cout << std::endl;

    std::for_each( std::begin( a ), std::end( a ),
                   []( int &x ) { x = x * x; } );

    for ( int x : a ) std::cout << x << ' ';
    std::cout << std::endl;

    return 0;
}

Though std::for_each is referred as a non-mutable algorithm nevertheless in its description there is written

2 Effects: Applies f to the result of dereferencing every iterator in
  the range [first,last), starting from first and proceeding to last -
  1. [ Note: If the type of first satisfies the requirements of a mutable iterator, f may apply nonconstant functions through the
  dereferenced iterator.—end note ]

So in fact it can be used as a mutable algorithm.
Both algorithms, std::for_each and std::transform, belong to the category of non-modifying sequence operations becuase they do not change the order of elements of the source sequence..
It seems you mix two notions: mutable algorithms and non-modifying sequence alforithms.:)


Which gamma regression model to use for extrapolation?

Question: I'm looking for a regression model which would satify these requirements:

My target variable follows the exponential distribution, so to my understanding I should use gamma loss function. I have tried this feature from XGBoost, LightGBM, pyGAM, and StatsModels GLM and they all seem to be working well.
The model should be able to at least some extrapolation in respect of features $x_e$. In my training data one feature has only values $x_e$ = 1, 2, 3..., 10, and I'm really interested about the prediction at $x_e$=0. Linear models seem to perform well, but tree based models (XGBoost, LightGBM) are very bad at this.
Some non-linearity or other types of flexibility to fit complex relationships between the input variables. For example with BayesianRidge I get almost the same performance in training and test sets, which tells me that I should be able to build a better model by adding complexity.

My data set contains around one million samples and I'm using 10 features (both discrete and continuous). Potentially I might get 10-20 new features in the future.
Here are the results I have so far. Since I want the model to perform well on the extrapolation in respect of input x, my test set consists of all the samples with $x_e$=1.

Linear regression softplus link  Training loss: 0.949    Test loss: 0.673
Bayesin Ridge regression Training loss: 0.931    Test loss: 0.653
Bayesin Ridge with softplus link Training loss: 0.949    Test loss: 0.673
Elastic Net  Training loss: 0.930    Test loss: 0.660
Elastic Net with softplus link   Training loss: 0.946    Test loss: 0.655
Elastic Net CV   Training loss: 0.931    Test loss: 0.654
Elastic Net CV with log link Training loss: 0.984    Test loss: 0.688
Elastic Net CV with softplus link    Training loss: 0.947    Test loss: 0.664
GLM with log link and gamma loss Training loss: 0.927    Test loss: 0.657
GAM with log link gamma loss   Training loss: 0.927  Test loss: 0.657
LGBMRegressor (max_depth 3)   Training loss: 0.899  Test loss: 0.678

All models seem to have much better loss at the test set than the training set. Normally this would indicate over fitting, but I believe that the reason is that the values at $x_e$=1 are lower than those at other values of $x_e$.
Initially I had a completely randomized train-test split.
Based on those results LGBM was model for interpolation, but now it is clear that the other models outperform it in extrapolation. Based on those results, none of the current models were overfit. I think that the model should have only linear or other very simple dependency to $x_e$, but there is probably potential to increase the model complexity in respect of the other features.
Comment: Just to state the obvious: Why not a GAM with Gamma response? (In R what would be `mgcv::gam` with  `family=Gamma(link=log)`)  It will naturally work with a Gamma distributed response function as well as account for non-linear relations between the response and the explanatory variables.
Comment: Thanks for the advice! I tried this Python library https://pygam.readthedocs.io/en/latest/api/gammagam.html which seems to have similar features as mgcv::gam. When I fit the model with 20 samples I get pretty nice results. However, when I fit it with all 900000 samples in my training set I get error "PIRLS optimization has diverged". Is it even possible to fit a GAM to a large data set like this?
Comment: Yes, it is possible. I commonly use dataset having 1M+ instances (in R). The obvious thing to suggest in your case is to limit the number of basis functions used; this should make the optimisation task solved by PIRLS easier (as it will have smaller dimensionality). This can be done by setting `s(..., n_splines=X)` where `X` is something smaller than the default $20$ in `pyGAM`.
Comment: Sorry, is there a typo at the "LGBMRegressor with gamma objective and max_depth 3"? cause it seems to clearly provide the lowest test loss (0.928) but the text mentions that BRR as the best model where the test loss is higher (0.960). To that extent, GAM with gamma distribution has a lower test loss (0.955) than all other regression routines... (Also, did the `n_splines` worked or you used something else?)
Comment: Yes, with your help on n_splines I got the GAM work properly. I used n_splines=10. And yes, while both LGBM and GAM give better loss than BRR, the extrapolation does not look correct (see requirement 2).
Comment: Ah, OK! I mistook "extrapolation performance" as your "test performance". That being said, this is exactly what should be done. Remove `x = 1` (and occasionally both `x = 1 and x = 2`) from the training set and have them only on the test set. *Then*, see how each algorithm is performing. The test set performance currently mentioned is misleading as it refers (broadly speaking) to interpolation rather than extrapolation.
Comment: That's a great idea! I ran my initial results again and updated the question.
Comment: Cool. (I think you have double mentioned GAM). As it stands I think that the BRR is your best bet; aside just "ridge" can you tried "elastic net" too?
Comment: No, the other result is a Generalized linear model from statsmodels. It just has the exact same loss at the GAM. I added elastic net too and it does pretty well.
Comment: OK, I would get some variability estimates for those losses now (we do not want to overfit our test set after all) and I think the issue is sorted out! :) (I will probably write-up my suggestions in an answer a few days.)
Answer: The OP has done a great job exploring a variety of different techniques. As commented, given that the response variable is Gamma-distributed it makes sense to consider a GLM and/or a GAM for Gamma distributed variables. Particularly for the use of GAMs, if the computation burden appears too much we might want to consider restricted the basis functions used by the GAM (in the case of pyGAM used here that being achieved by  setting s(..., n_splines=X) where X is something smaller than the default $20$.
The main point to rectify is the use of evaluating the error of each method. Simple random resampling by cross-validation is providing us an indication on "interpolation" rather than "extrapolation" performance. Here, given $x_e = \{1, \dots, 10\}$, we focus on predicting $x_e =0$; therefore it is more reasonable to use instances where $x_e = 1$ in our validation set and instances where $x_e =\{2, \dots, 10\}$ in our training set. Note that in-sample errors are rather misleading for a extrapolation task; there is no "overfitting" perse because the validation and training set do not refer to the same sample/population. On that matter, the fact we get simple models (Elastic Net regression and Bayesian Ridge regression) as our top-performing routines is not too surprising. When extrapolating most bets are off (e.g. see the CV thread: What is wrong with extrapolation?) and commonly simple methods outperform complex ones (e.g. see the CV thread Best method for short time-series).
As a final note, it is always prudent to get estimates of the variability of our performance metric. If possible we should set aside a number of observations, fit our candidate models to the remaining data, and evaluate the models in the data we set aside. This should be repeated multiple times.
In effect, what is described is nested cross-validation for model selection; only particular will be that the for each loop the hold-out set is such that $x_e = 1$. (Once again) CV has a great thread on the matter: Nested cross validation for model selection and Model selection and cross-validation: The right way. In short, the outer loop will be used to assess the performance of the particular model (e.g. Ridge regression), and the inner loop will be used to select the best model (the regularisation parameter $\lambda$ for the case of Ridge). A simplified and very succinct Python example of nested CV would be as follows:
myS = cross_val_score(GridSearchCV(linear_model.ElasticNet(), param_grid, cv=5),
                      myX, myY, cv=5)
print("CV scores: ", myS)
print("Mean CV scores & Std. Dev.: {:.3f} {:.3f}".format(myS.mean(), myS.std()))

Answer: Linear models are great for extrapolation, since they don't make too complex assumptions which might not generalize outside of the training set. Unfortunately that also means that they are not able to use the full potential of other features which are not changed when extrapolating.
Since extrapolation is done only in respect of input $x_e$ we can split all inputs $X$ into vector $x_e$ and matrix $X_{other}$.
One approach could be to add more features based on the values of $X_{other}$. These could be interactions between these features, squared values, logarithms etc. This could work well especially if you have a good idea on how each feature should contribute to the output.
One could also build a custom model, which fits only a linear relationship in respect of $x_e$ but uses more complex relations for $X_{other}$. Here is an example model built with keras which fits a neural network with 4 hidden layers for $X_{other}$, then concatenates the last hidden layer with $x_e$ and uses one final layer with softplus activation (to ensure that the predictions are positive). With keras
from keras.layers import Input, Dense, Lambda, concatenate
from keras.models import Model
import keras.backend as K
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

def loss(y_true, y_pred):
    return -K.log(1/y_pred) + y_true/y_pred

all_inputs = Input(shape=(n_inputs,))

# Only the first feature will be used for extrapolation
x_e = Lambda(lambda x: x[:,:1], output_shape=(1,))(all_inputs)

x_other = Lambda(lambda x: x[:,1:], output_shape=(n_inputs-1,))(all_inputs)
hidden_1 = Dense(20, activation='relu')(x_other)
hidden_2 = Dense(20, activation='relu')(hidden_1)
hidden_3 = Dense(10, activation='relu')(hidden_2)
hidden_4 = Dense(20, activation='relu')(hidden_3)

merged = concatenate([hidden_3, x_e])
preds = Dense(1,activation='softplus')(merged)

model = Model(inputs=all_inputs, outputs=preds)
model.compile(optimizer='adam', loss=loss, metrics=['mse'])

model = Pipeline([
        ('scale', StandardScaler()),
        ('keras', model),
])

With a model similar to this (I added some customization to my specific problem) I was able to get a test loss of 0.653. This model also seems to be best so far for interpolation within the training set.


How do I use the definition of partial derivative to get $f_x(x,y) = \frac{y(y^2-x^2)}{(x^2 + y^2)^2}$?

Question: $\\f(x,y) =
\begin{cases}
\frac{xy}{x^2 + y^2},  & \text{if $(x,y) \ne (0,0)$} \\
0, & \text{if $(x,y) = (0,0)$}
\end{cases}$
Using the definition of partial derivatives:
\begin{align*}
    f_x(x,y) &= \lim_{h\to 0} \frac{f(x+h,y) - f(x,y)}{h}
    \\&= \lim_{h\to 0} \frac{\frac{(x+h)y}{(x+h)^2 + y^2}-\frac{xy}{x^2 + y^2}}{h}\\&= \lim_{h\to 0} \frac{(xy + hy)(x^2 +y^2)-xy(x^2 +2hx+ h^2 + y^2)}{h(x^2 + 2hx + h^2 + y^2)(x^2 + y^2)}
    \end{align*}
I'm not sure how to get rid of the variable $h$. I wanted to know exactly how I could further simplify in order to get $\frac{y(y^2-x^2)}{(x^2 + y^2)^2}$?
Answer: Notice that
\begin{align}&\phantom{ = }\frac{(xy + hy)(x^2 +y^2)-xy(x^2 +2hx+ h^2 + y^2)}{h(x^2 + 2hx + h^2 + y^2)(x^2 + y^2)}\\
&= \frac{\color{red}{xy(x^2 +y^2)} + hy(x^2 +y^2) \color{red}{-xy(x^2 + y^2)} - xy(2hx+ h^2)}{h(x^2 + 2hx + h^2 + y^2)(x^2 + y^2)}\\
&=\frac{hx^2y+hy^3 -2hx^2y-h^2xy}{h(x^2 + 2hx + h^2 + y^2)(x^2 + y^2)}\\
&=\frac{hy(y^2 -x^2-hx)}{h(x^2 + 2hx + h^2 + y^2)(x^2 + y^2)}\\
&=\frac{y(y^2 -x^2-hx)}{(x^2 + 2hx + h^2 + y^2)(x^2 + y^2)}\\
&\to \frac{y(y^2-x^2)}{(x^2+y^2)^2},
\end{align}
as $h\to 0$.
Answer: Continuing your calculation, we get
$$f_x(x,y)=\lim_{h\rightarrow 0}\frac{hy(y^2-x^2-hx)}{h(x^2+2hx+h^2+y^2)(x^2+y^2)}.$$
Or
$$f_x(x,y)=\lim_{h\rightarrow 0}\frac{y(y^2-x^2-hx)}{(x^2+2hx+h^2+y^2)(x^2+y^2)}.$$
Can you finish from here?


How to find the integral $\int \tan (5x) \tan (3x) \tan(2x) \ dx $?

Question:
Find the integral $\int \tan (5x) \tan (3x) \tan(2x) \ dx $ .

This question is posted in a maths group in Facebook. What way should we use to solve integral like this?
Comment: http://www.mathsisfun.com/calculus/integration-by-parts.html
Comment: @achillehui This pretty hint could be an answer ;)
Answer: Let $t_n = \tan(nx)$, we have
$$t_5 = \frac{t_3 + t_2}{1 - t_3t_2} \iff t_5 - t_5t_3t_2 = t_3 + t_2
\implies t_5t_3 t_2 = t_5 - t_3 - t_2$$
This leads to
$$\begin{align}\int \tan(5x)\tan(3x)\tan(2x) dx
&= \int \left(\tan(5x) - \tan(3x) - \tan(2x) \right)dx\\
&= \frac12 \log\cos(2x) + \frac13\log\cos(3x) - \frac15\log\cos(5x) + \text{ const. }
\end{align}$$
Comment: Well done!  (+1)
Comment: @achille hui Nice trick! (+1)


Is playing with two hand really necessary in this passage?

Question: This is an except from Hungarian Rhapsody No.2 by Liszt from John Thompson's Grade 3
(The key signature of this section is F Major):

Is playing this passage with two hand really necessary? or is it more a pedagogical decision to arrange it that way?
Answer: It gives a better staccato articulation, there's also always a difference in hands when striking a key.
To me it is also easier to play with two hands, and you can play considerably faster this way.
Comment: can you please elaborate on your claim "there's also always a difference in hands when striking a key"?
Answer: In this passage, the composer is showing that there are actually two lines of music happening simultaneously. The left hand is playing the bass notes while the right hand is playing the melody. By writing it on two staves and requiring two hands to play, this musical feature is ensured to be heard.
Comment: Why would writing it on two staves make a difference to how it sounds, if you're playing all the notes on the same instrument?
Comment: -1: In this example, all notes shown are part of a single melody line. (It's quite recognizable if you play it.) Also notice the cross-staff beaming, which indicates a single line. The two staffs are only being used to indicate which hands should play which notes.
Answer: Mostly it's due to the fingering, which you'll notice only makes use of fingers 1, 2, and 4 on each hand. These are the more powerful fingers, which reflects the quasi-accented nature of these notes in the line. If you played it with one hand you'd likely be using 3 and 5 a lot, and 5 for sure is a weaker finger for most pianists.
I would also say it's being used just to keep the activity more symmetrical between the hands. Those are the only staffs on the score at that point; there's nothing going on, so there's no point in letting the right hand do all the work while the left takes a break. Most fingerings are editorial (especially in academic arrangements), and I've seen this behavior in the edition I have of Bach's WTC I Bb Major Prelude as well.
Comment: -1: It has nothing to do with finger strength (5 is used so often as the melody finger that its strength *must* be sufficient for higher level pianists). It has everything to do with ease of playing. Playing that passage well at speed with one hand is quite difficult.
Comment: @mjibson Really? There are no intervals larger than a 6th in that line. Also consider that this is a pedagogical arrangement, so finger strength is not a certainty. If you have a different rationale for the use of 2-4 instead of 3-5, feel free to supply that in an answer.
Comment: I agree with mjibson. It's not bad in terms of intervals, but try finding a good fingering for it. The best 1 handed fingering I can work out atm is 45242413451512 and that's hard to play fast. Also, it'll be hard to play that staccato.


Dummy variable notation

Question: Consider the following linear regression model that only consists of dummy variables:$$Y_{i}=\beta_{0}+\beta_{1}D_{1i}+\beta_{2}D_{2i}+\epsilon_{i}$$
 whereby $i$  denotes individual, $D_{1i}$ is an indicator that switches on if $i$ is in treatment 1, and $D_{21}$ switches on if the individual is in treatment 2. The reference group is the control group. We know that
$${\beta_{1}}=\mathbb{E}[Y_{i}|D_{1i}=1]-\mathbb{E}[Y_{i}|Control].$$
I am a bit confused regarding notation here. Can we leave it as above or should we be more concrete:
$${\beta_{1}}=\mathbb{E}[Y_{i}|D_{1i}=1,D_{2i}=0]-\mathbb{E}[Y_{i}|D_{1i}=0,D_{2i}=0]?$$
That is, do we need to also condition on the second treatment here? Or is it already assumed because the treatments are mutually exclusive?
Comment: Your notation is wrong. You write $\hat{\beta}_1 = \dots$, but I think you mean $\beta_1$. Putting a hat would indicate an estimate, and not the truth.
Answer: Your model is
$$Y_i = \beta_0 + \beta_1 D_{1i} + \beta_2 D_{2i} + \epsilon. $$
The expectation in this next step is always taken conditioned on all the predictors. So
$$E(Y_i| D_{1i} = d_1, D_{2i} = d_2) = \beta_0 + \beta_1d_1 + \beta_2 d_2.$$
Remember that when inference is made on the regression coefficients, you need to keep all other predictors constant. To write down the equation with a focus on $\beta_1$, we let $d_1 = 0$ and $1$.
$$\beta_1 = E(Y_i| D_{1i} = 1, D_{2i} = d_2) - E(Y_i|D_{1i} = 0, D_{2i} = d_2). $$
So you condition on all predictors.
Comment: Thanks, but in the context of dummy variables, it is redundant on condition on the other variable taking on a value of 0 as these are mutually exclusive categories. More specifically, $E[Y|D_{1i}=1]=E[Y_{i}|D_{1i}=1,D_{2i}=0]$.
  In other words, conditioning on an individual being a man is equivalent to conditioning on an individual being a man and not a woman. Right?
Comment: From your question, it seemed like there were two categorical variables. In any case, what I wrote down is still correct. You can then probably go ahead and use your argument to  write it only in terms of $D_{i1}$.

Dummy variable notation


Issues With Printer GPO

Question: I have a Windows Server 2008 R2 print server. The client machines are Windows 7 Professional. I am having issues with user accounts not getting the printer added when they login.
Things that I have tried:

Installed Printer Driver In Printer Manager
Set Printer To Be Shared
Set the Security Of The Printer To The Correct Group
Added The User To The Correct Group
Created A New GPO
Added The Printer To The GPO (See Image)
Added The Group To The Security Filtering
Ran GPUPDATE /Force

I have another GPO that I have setup for another printer, and it works fine. The only difference between that GPO and the one that I am having issue with is the security filtering. The filtering on the one that works has the following:

Everyone
Domain Users
Authenticated Users

I have also made a video, so that you can see the steps. Please click here to see the video.
Comment: Has the user logged out and back in so that his/her token contains the security group membership?
Comment: Have you tried changing the action to `Create`, from `Update`?  (I know the documentation says that `Update` is supposed to create the connection if it doesn't exist, but my experience has been that this doesn't always work.)
Comment: @HopelessN00b The other GPO uses Update as well, and it works fine.
Comment: @joeqwerty Yes I have logged in and out a hand full of times
Comment: I looked into this method a couple years ago in our initial 7 roll out. Even got it fully implemented. It was always hit or miss even after a lot of hours debugging the processing. In the end I opted for a login script that read the group memberships I was using for item level targeting.

Good luck.
Comment: Are the clients Windows 7 or XP? Does that same client work with the other GPO? If it's XP http://www.microsoft.com/en-us/download/details.aspx?id=3628
Comment: @Nixphoe I updated the question. They are Widnows 7 Pro clients
Answer: On the GPO you need to check the box "Run in logged-in user's security context"
Answer: One one of the computers where the printers are not installed, run the following:
<code>gpresult /h gpresult.html
</code>
From the user's logon context.
This will show the user's group memberships, and the GPO's applied/denied.  This will help you narrow down if it is an issue with group membership not being picked up, or if the GPO's aren't being processed, or if the computer does not even report on the GPO.
Comment: Where is the .html file save @?
Comment: @nate in the same directory that you ran the command from, ie c:\users\username\> gpresult /h > gpresult.html
Would put the file in c:\users\username\gpresult.html
Comment: It says it is inaccessible, on the one the that I am having issues with, and the on that works says empty. I am going to attach the html file to my question
Comment: @nate: Give Authenticated Users Read permission on the GPO, but uncheck the box for Apply Group Policy.
Comment: @nate: Sounds like they aren't pickup up the group membership.

Issues With Printer GPO