Doubles serve rules in table tennis

Question:
In doubles table tennis does the rules allow you to serve to the side of the table (red line) or only to the end of the table (green line)?
Player B is serving.
Answer: Rule 2.6.3 of the official rules states:

in doubles, the ball shall touch successively the right half court of server and receiver.

There is no requirement that the ball crosses the end line of the receiving team - but note that the serve you've drawn would result in the server losing the point as it is not touching the right half court of the server and the receiver.
Answer: In doubles, you must always serve from the right half of the server's side  with the ball going diagonally to the right half of the opposite side.
The image in the question is an incorrect serve as you never serve from the left half (Player B in image question), but instead Player B also serves from the right half to the opposite right half but with Player 2 as the receiver (like the yellow arrow).
Comment: @OlaStröm, if you can give a citation for the rule that shows how this is incorrect, that would be helpful.
Comment: Sorry, my bad, I didn't read it carefully enough and misunderstood it. @ChandraJ is correct in that all serves are from the right hand side (half) of the server's table to the right hand side (half) of the receivers table (opposite side).

Doubles serve rules in table tennis


Query with dynamic date intervals

Question: Given a statuses table that holds information about products availability, how do I select the date that corresponds to the 1st day in the latest 20 days that the product has been active?
Yes I know the question is hard to follow. I think another way to put it would be: I want to know how many times each product has been sold in the last 20 days that it was active, meaning the product could have been active for years, but I'd only want the sales count from the latest 20 days that it had a status of "active".
It's something easily doable in the server-side (i.e. getting any collection of products from the DB, iterating them, performing n+1 queries on the statuses table, etc), but I have hundreds of thousands of items so it's imperative to do it in SQL for performance reasons.
table : products
+-------+-----------+
|   id  |   name    |
+-------+-----------+
|   1   |   Apple   |
|   2   |   Banana  |
|   3   |   Grape   |
+-------+-----------+

table : statuses
+-------+-------------+---------------+---------------+
|   id  |     name    |   product_id  |   created_at  |
+-------+-------------+---------------+---------------+
|   1   |   active    |            1  |   2018-01-01  |
|   2   |   inactive  |            1  |   2018-02-01  |
|   3   |   active    |            1  |   2018-03-01  |
|   4   |   inactive  |            1  |   2018-03-15  |
|   6   |   active    |            1  |   2018-04-25  |
|   7   |   active    |            2  |   2018-03-01  |
|   8   |   active    |            3  |   2018-03-10  |
|   9   |   inactive  |            3  |   2018-03-15  |
+-------+-------------+---------------+---------------+

table : items (ordered products)
+-------+---------------+-------------+
|   id  |   product_id  |   order_id  |
+-------+---------------+-------------+
|   1   |            1  |          1  |
|   2   |            1  |          2  |
|   3   |            1  |          3  |
|   4   |            1  |          4  |
|   5   |            1  |          5  |
|   6   |            2  |          3  |
|   7   |            2  |          4  |
|   8   |            2  |          5  |
|   9   |            3  |          5  |
+-------+---------------+-------------+

table : orders
+-------+---------------+
|   id  |   created_at  |
+-------+---------------+
|   1   |   2018-01-02  |
|   2   |   2018-01-15  |
|   3   |   2018-03-02  |
|   4   |   2018-03-10  |
|   5   |   2018-03-13  |
+-------+---------------+

I want my final results to look like this:
+-------+-----------+----------------------+--------------------------------+
|   id  |   name    |  recent_sales_count  |  date_to_start_counting_sales  |
+-------+-----------+----------------------+--------------------------------+
|   1   |   Apple   |                   3  |                    2018-01-30  |
|   2   |   Banana  |                   0  |                    2018-04-09  |
|   3   |   Grape   |                   1  |                    2018-03-10  |
+-------+-----------+----------------------+--------------------------------+

So this is what I mean by latest 20 active days for e.g. Apple:

It was last activated at '2018-04-25'. That's 4 days ago.
Before that, it was inactive since '2018-03-15', so all these days until '2018-04-25' don't count.
Before that, it was active since '2018-03-01'. That's more 14 days until '2018-03-15'.
Before that, inactive since '2018-02-01'.
Finally, it was active since '2018-01-01', so it should only count the missing 2 days (4 + 14 + 2 = 20) backwards from '2018-02-01', resulting in date_to_start_counting_sales = '2018-01-30'.
With the '2018-01-30' date in hand, I'm then able to count Apple orders in the last 20 active days: 3.

Hope that makes sense.
Here is a fiddle with the data provided above.
Comment: this will already be a great help : http://sqlfiddle.com/#!9/b7817f/3  I've still hard to understand the rest, but from my query here, it will be peanuts for sure. I grab a coffee and I finish this
Comment: Can you double check the `date_to_start_counting_sales` in your desired results (assuming your using YYYY-MM-DD)? And please provide an example of what you mean by "the last 20 days that it was active" for one of the products. E.g. Apple was last active '2018-04-25', so all sales from '2018-04-25' to '2018-05-15'? Or do you want the last active time where a full 20 day interval exists? I'm unable work backwards from your data on how you got to those desired results with the info provided.
Comment: man your are giving me a headache on a Sunday morning... ok trying to understand that again. Could you prepare a fiddle with your structure and data so we can have a playground ?
Comment: plsql would still take a lot of time to write, and pretty much impossible to translate to mysql... so i messed with mysql instead. Been wanting to learn it anyway. Hope it helps. Got me obsessed with this problem now! ha
Comment: @krokodilko As I mentioned, I've tried solving the problem in the server-side language, and I have a problem getting it done in SQL. Thanks for your awesome collaboration.
Comment: @Mallorie Thanks! I fixed the dates and added an explanation on how I'd get the desired result for Apple. Please let me know if that still doesn't make sense.
Comment: I have read your "finally" point 5 times : I understand how you get your 20 days of active apples, but I dont understand how you get the date 2018-01-30 from that. Beside this, since MySQL doesn't have window functions, that will be hard to achieve in straight SQL (without a procedure)
Comment: Thanks rzb, that makes sense now. Thinking the same thing as @Thomas G though... could figure something out in PL/SQL (albeit ugly), but mysql seems limited. And mysql is not my forte. I'll ponder it and get back if anything comes to mind.
Comment: @ThomasG I'm just counting 20 active days backwards, ignoring inactive intervals. The total active days would be 49 (since it was first made active at '2018-01-01'), but since I only need 20 days, I only need 2 more at that point. So ''2018-02-01' -2 days. Pretty hard to explain and I guess even harder to understand =(
Comment: @Mallorie If you could post what you got with PL/SQL that would probably be a great starting point for me. Thanks!
Comment: @ThomasG Updated the question with link to **final** fiddle.
Comment: Can products be ordered when they are not active?  Does a product typically get ordered once per day?  If so, just use the last 20 days of orders.  And, why is the question tagged MySQL if you are writing PL/SQL code.  Oracle and MySQL have very different capabilities.
Comment: @GordonLinoff No, they can't be ordered when not active. And they can be ordered 0-N times per day. Some products typically only sell on certain dates. I got what you suggest but unfortunately there's no specific pattern. And no, I'm not writing any PL/SQL code, it's MySQL.
Answer: I've got a standard SQL solution, that does not use any window function as you are on MySQL 5
My solution requires 3 stacked views.
It would have been better with a CTE but your version doesn't support it. Same goes for the stacked Views... I don't like to stack views and always try to avoid it, but sometimes you have no other choice, because MySQL doesn't accept subqueries in FROM clause for Views.
CREATE VIEW VIEW_product_dates AS
(
        SELECT product_id, created_at AS active_date,
                (
                    SELECT created_at
                    FROM statuses ti
                    WHERE name = 'inactive' AND ta.created_at < ti.created_at AND ti.product_id=ta.product_id
                    GROUP BY product_id
                ) AS inactive_date
        FROM statuses ta
        WHERE name = 'active'
);

CREATE VIEW VIEW_product_dates_days AS
(
    SELECT product_id, active_date, inactive_date, datediff(IFNULL(inactive_date, SYSDATE()),active_date) AS nb_days
    FROM VIEW_product_dates
);

CREATE VIEW VIEW_product_dates_days_cumul AS
(
    SELECT product_id, active_date, ifnull(inactive_date,sysdate()) AS inactive_date,  nb_days,
         IFNULL((SELECT SUM(V2.nb_days) + V1.nb_days
                 FROM VIEW_product_dates_days V2
                 WHERE V2.active_date >= IFNULL(V1.inactive_date, SYSDATE()) AND V1.product_id=V2.product_id
                ),V1.nb_days) AS cumul_days
    FROM  VIEW_product_dates_days V1
);

The final view produce this :
| product_id |          active_date |        inactive_date | nb_days | cumul_days |
|------------|----------------------|----------------------|---------|------------|
|          1 | 2018-01-01T00:00:00Z | 2018-02-01T00:00:00Z |      31 |         49 |
|          1 | 2018-03-01T00:00:00Z | 2018-03-15T00:00:00Z |      14 |         18 |
|          1 | 2018-04-25T00:00:00Z | 2018-04-29T11:28:39Z |       4 |          4 |
|          2 | 2018-03-01T00:00:00Z | 2018-04-29T11:28:39Z |      59 |         59 |
|          3 | 2018-03-10T00:00:00Z | 2018-03-15T00:00:00Z |       5 |          5 |

So it aggregates all active periods of all products, it counts the number of days for each period, and the cumulative days of all past active periods since current date.
Then we can query this final view to get the desired date for each product. I set a variable for your 20 days, so you can change that number easily if you want.
SET @cap_days = 20 ;

SELECT PD.id, Pd.name,
       SUM(CASE WHEN o.created_at > PD.date_to_start_counting_sales THEN 1 ELSE 0 END) AS recent_sales_count  ,
       PD.date_to_start_counting_sales
FROM
(
    SELECT p.*,
           (CASE WHEN LowerCap.max_cumul_days IS NULL
                 THEN ADDDATE(ifnull(HigherCap.min_inactive_date,sysdate()),(-@cap_days))
                 ELSE
                 CASE WHEN LowerCap.max_cumul_days < @cap_days AND  HigherCap.min_inactive_date IS NULL
                      THEN ADDDATE(ifnull(LowerCap.max_inactive_date,sysdate()),(-LowerCap.max_cumul_days))
                      ELSE ADDDATE(ifnull(HigherCap.min_inactive_date,sysdate()),(LowerCap.max_cumul_days-@cap_days))
                 END
            END) as date_to_start_counting_sales
    FROM products P
    LEFT JOIN
    (
        SELECT product_id, MAX(cumul_days) AS max_cumul_days, MAX(inactive_date) AS max_inactive_date
        FROM VIEW_product_dates_days_cumul
        WHERE cumul_days <= @cap_days
        GROUP BY product_id
    ) LowerCap ON P.id=LowerCap.product_id
    LEFT JOIN
    (
        SELECT product_id, MIN(cumul_days) AS min_cumul_days, MIN(inactive_date) AS min_inactive_date
        FROM VIEW_product_dates_days_cumul
        WHERE cumul_days > @cap_days
        GROUP BY product_id
    ) HigherCap ON P.id=HigherCap.product_id
) PD
LEFT JOIN items i ON PD.id =  i.product_id
LEFT JOIN orders o ON o.id = i.order_id
GROUP BY PD.id, Pd.name, PD.date_to_start_counting_sales

Returns
| id |   name | recent_sales_count | date_to_start_counting_sales |
|----|--------|--------------------|------------------------------|
|  1 |  Apple |                  3 |         2018-01-30T00:00:00Z |
|  2 | Banana |                  0 |         2018-04-09T20:43:23Z |
|  3 |  Grape |                  1 |         2018-03-10T00:00:00Z |

FIDDLE : http://sqlfiddle.com/#!9/804f52/24
Comment: First try my updated solution (fiddle link updated too), we'll see after that. recent_sales_count will be easy to compute
Comment: I give up. Its late in Europe and  I am too tired to brainstorm this correctly. I know that the final touch is close but I can't see it because I have to sleep.  I still have updated the fiddle and answer with the recent_sales_count. If someone else want to fork my answer to  finish it, please do... a job for @GordonLinoff :)
Comment: Awesome! You made it look simple, ty very much. One thing though would it be possible to show results for currently inactive products too instead of null? I'll try to get my head around this and tweak your queries.
Comment: yes but what would be the logic for inactive products ?
Comment: Same logic: 20 last active days. e.g. if the same Apple product was made inactive today, it should still give the same results forever until it's made active again.
Comment: Also, the really important column is the recent_sales_count (see my "desired results" table). The date_to_start_counting_sales (your target_date) would be there just so I can use it as starting date to count orders.
Comment: Changing the variable to something like 15 gives wrong results. Probably because it's only counting days and ignoring months.
Comment: Nice! That's correct for the inactive products. But if you change cap_days to 10, Apple should have a date_to_start_counting_sales of '2018-03-09', not '2018-01-26'.
Answer: Not sure which version of MySql you're working with, but if you can use 8.0, that version came out with a lot of functionality that makes things slightly more doable (CTE's, row_number(), partition, etc.).
My recommendation would be to create a view like in this DB-Fiddle Example, call the view on server side and iterate programatically. There are ways of doing it in SQL, but it'd be a bear to write, test and likely would be less efficient.
Assumptions:

Products cannot be sold during inactive date ranges
Statuses table will always alternate status active/inactive/active for each product. I.e. no date ranges where a certain product is both active and inactive.

View Results:
+------------+-------------+------------+-------------+
| product_id | active_date | end_date   | days_active |
+------------+-------------+------------+-------------+
| 1          | 2018-01-01  | 2018-02-01 | 31          |
+------------+-------------+------------+-------------+
| 1          | 2018-03-01  | 2018-03-15 | 14          |
+------------+-------------+------------+-------------+
| 1          | 2018-04-25  | 2018-04-29 | 4           |
+------------+-------------+------------+-------------+
| 2          | 2018-03-01  | 2018-04-29 | 59          |
+------------+-------------+------------+-------------+
| 3          | 2018-03-10  | 2018-03-15 | 5           |
+------------+-------------+------------+-------------+

View:
CREATE OR REPLACE VIEW days_active AS (
WITH active_rn
     AS (SELECT *, Row_number()
                    OVER ( partition BY NAME, product_id
                    ORDER BY created_at) AS rownum
         FROM   statuses
         WHERE name = 'active'),
     inactive_rn
     AS (SELECT *, Row_number()
                    OVER ( partition BY NAME, product_id
                    ORDER BY created_at) AS rownum
         FROM   statuses
         WHERE name = 'inactive')
SELECT x1.product_id,
       x1.created_at AS active_date,
       CASE WHEN x2.created_at IS NULL
            THEN Curdate()
            ELSE x2.created_at
       END AS end_date,
       CASE WHEN x2.created_at IS NULL
             THEN Datediff(Curdate(), x1.created_at)
            ELSE  Datediff(x2.created_at,x1.created_at)
        END AS days_active
FROM   active_rn x1
       LEFT OUTER JOIN inactive_rn x2
                    ON x1.rownum = x2.rownum
                       AND x1.product_id = x2.product_id ORDER  BY
x1.product_id);

Comment: Thanks! I'm on MySQL 5.7, but I like the view idea. The problem though is the N+1 number of queries needed to count items for each product if I do it on server side. I have hundreds of products and hundreds of thousands of orders/items to calculate recent sales for. It just takes minutes/times out. I could try changing the timeout limit, running the script on a background process, caching the results, etc; but it'd be my last resort due to the required architecture.
Comment: To clarify, i mean use the view results to get the `date_to_start_counting_sales` programatially. That's the part you probably shouldn't attempt in SQL. Once you have that, just pass the date into a  `select` statement. Or look at your iteration again because pretty much any ORM has a way around the n+1 issue. Lots of stack overflow answers on that. Much luck to you!

Query with dynamic date intervals


Limit number of messages sent by Exchange 2007

Question: I have a Exchange 2007, it's possible to limit the number of messages sent by a user?
When a computer is infected by virus or similar, this user sends a lot of emails (about 100.000 emails in a day). If it's possible to limit, I can delete of queue.
Now when I have a issue of emails, domain automatically appears in black lists.
I search a lot of documentation, but I think Exchange 2007 don't have this capability.
Anyone knows how enable this function?
Thank you very much.
Answer: Most of the virus/malware will not use your in house Exchange to send the email, most of them create a local SMTP service on the infected computer to send the mass email.
As such to limit the impact I would close the port 25 to anything except your Exchange's server IP.
I would use another public IP for the outgooing traffic on port 25, thus if a user bypass your firewall restriction somehow, your public IP, used for anything else than Exchange will be blacklisted, but not your Exchange IP.
For your actual question, you could adjust client throttling, or you could route your outgooing email to a smarthost that offer spam scanning.


Showing that $E[X^2] = E[Y^2]$ for two RV following Frechet distributions with different location parameters only

Question: If random variable X follows a Fréchet distribution (https://en.wikipedia.org/wiki/Fr%C3%A9chet_distribution) with shape parameter $\alpha$, scale parameter $s$, and location parameter $m$, that is
$X \sim F(\alpha,s,m)$
Now if we have another random variable $Y \sim F(\alpha,s,m')$ (the only difference being the location parameter $m' \neq m$), could we say that their second-order moments are equal, i.e.
$E[X^2] = E[Y^2]$ ?
Intuitively, I would say yes, but is it possible to demonstrate it?
Comment: I would say no because the variance of Frechet distribution does not depend on the location parameter, but the mean does. And since the second moment can be written as variance plus mean squared, it also depends on $m$.
Comment: I see, it makes sense! thanks for your reply.
Answer: In general, the answer is no they will not be equal to each other. The comment by @misius is an excellent way to show this, but it does assume that the mean and variance are finite and defined. Technically, we can have equality in the sense that $E(X^2) = E(Y^2) = \infty$ whenever $\alpha < 2$. Still, the spirit of the answer is correct.

Here's an additional proof that demonstrates the fact directly.
Without loss of generality, suppose that $X \sim F(a, s, 0)$ and $Y \sim F(a, s, m)$, with $m > 0$. It is obvious that $Y = X+m$ in distribution.
\begin{align*}
E(Y^2) &= E\left((X+m)^2\right) \\
&= E\left(X^2 + 2mX + m^2\right) \\
&= E(X^2) + 2m E(X) + m^2.
\end{align*}
For $E(Y^2) = E(X^2)$ to hold, we need $2mE(X) + m^2 = 0$. But both of these quantities must be positive since we have assumed that $m > 0$ and since $X$ has positive support. Therefore the equality can only hold in the non-interesting case where $E(X^2)$ and $E(Y^2)$ are both infinity.


Handling TCP broken packets over a Secure connections (TLSv2) is failing because I can't see or decrypt content

Question: I am working on a client application that sends transactions to a server over a TLS connection. The applications sends a given number of bytes and receives 1182 bytes as response. It has been working fine until I started increasing the number of transactions per second. After that, some response packets started to be broken and cannot be fully received by the client in only read. When I try to unwrap the packet content, it raises an exception and terminates TLS session.
javax.net.ssl.SSLException: Unrecognized record version (D)TLS-0.0 , plaintext connection?
    at java.base/sun.security.ssl.SSLEngineInputRecord.bytesInCompletePacket(SSLEngineInputRecord.java:98)
    at java.base/sun.security.ssl.SSLEngineInputRecord.bytesInCompletePacket(SSLEngineInputRecord.java:64)
    at java.base/sun.security.ssl.SSLEngineImpl.readRecord(SSLEngineImpl.java:544)
    at java.base/sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:441)
    at java.base/sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:420)
    at java.base/javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:634)
    at MyClass.handleEncryptedTransaction(MyClass.java:297)

I tried to use a buffer to accumulate possible broken packages, however I cannot see the packet content before decripting and can only estimate if it is complete based on its size.
    this.peerNetData.clear();
    int bytesRead = socketChannel.read(this.peerNetData);
    if (bytesRead == DEFAULT_TLS_HANDSHAKE_SIZE) {
        handleEncryptedTransaction(socketChannel, engine);
    } else if (bytesRead > 0) {
        // TLS packet buffering

        byte[] justRead = new byte[this.peerNetData.position()];
        this.peerNetData.flip();
        this.peerNetData.get(justRead);
        this.incompleteTransactionsBuffer.put(justRead);

        // DEFAULT_TLS_TRANSACTION_SIZE == 1182
        if (this.incompleteTransactionsBuffer.position() >= DEFAULT_TLS_TRANSACTION_SIZE) {
            this.incompleteTransactionsBuffer.flip(); // flipping to read mode

            while (this.incompleteTransactionsBuffer.remaining() >= DEFAULT_TLS_TRANSACTION_SIZE) {
                byte[] fullTransaction = new byte[DEFAULT_TLS_TRANSACTION_SIZE];

                this.incompleteTransactionsBuffer.get(fullTransaction, 0, fullTransaction.length);

                this.peerNetData.clear();
                this.peerNetData.put(fullTransaction);

                // This method uses this.peerNetData to unwrap data and
                handleEncryptedTransaction(socketChannel, engine);
            }
            this.incompleteTransactionsBuffer.compact(); // wipe out bytes that had been read and free space of the buffer
        }
    }

Is there anyway to check if a TCP packet over TLS is complete? I tried to read the first 1182 bytes but it doesn't seem to work. Interestingly, this code work when I get multiple packets in the response, such as (N * 1182), where N varies from 2 to 7. Maybe I should wait for another socket read and get another piece of information?
I suppose this problem occurs because of packet retransmissions caused by heavy traffic. Is there any other way to deal with TLS packet retransmissions in low level socket connections in Java?
Comment: TCP does not have the concept of 'message' - it's just a stream of bytes (i.e. there's no correlation between the number of 'sends' and  the number of 'recvs' at the other end) - so if you want 'messages' you have to invent them.  TLS invents the 'TLS record' via a header which includes the length of the record. [See e.g. Wikipedia](https://en.wikipedia.org/wiki/Transport_Layer_Security#TLS_record).  Your concern about 'retriansmissons' is wrong - TCP handles that, not layers above TCP.
Comment: Thanks for the info. I understood TCP is responsible for retransmissions and I don't need to handle this in the app. However, I do receive half of some TLS record on the way and can not identify it, that is the problem. For example: a TLS record I am waiting has 1000 bytes but I get only 700 bytes in the socket read. The last 300 bytes needs to be received on another read, but I can't know the size of the current TLS record. Is there any way to do it in Java?
Comment: As long as you've got the TLS header (a fixed size) you can find the TLS record length.  But in the cases I've seen, I think an SSLEngine is used to handle such details.
Comment: The error occurs exactly inside SSLEngine when I try to unwrap an amount of bytes I just read from socket. The SSLEngine returns an exception saying: "SSLException: Unrecognized record version" basically because the TLS record may be incomplete
Comment: If the SSLEngine needs more data it tells you, and then you have to read more data from the socket and give it to the SSLEngine.
Comment: @PresidentJamesK.Polk does the SSLEngine tells it by raising an exception? Or there is any way to check it before handling any exception?
Answer: After getting comments and better understanding TLS protocol, I could work out a solution for the problem by implementing a buffer and getting the exact size of a TLS Record to wait for other TCP reads.
A TLS record "might be split into multiple TCP fragments or TCP fragments might also contain multiple TLS records in full, in half or whatever. These TCP fragments then might even be cut into multiple IP packets although TCP tries hard to avoid this.", from Determine packet size of TLS packet Java/Android. However, that post mentions first 2 bytes and it is not right. According to https://hpbn.co/transport-layer-security-tls/#tls-record-diagram:

Maximum TLS record size is 16 KB
Each record contains a 5-byte header, a MAC (up to 20 bytes for SSLv3,
TLS 1.0, TLS 1.1, and up to 32 bytes for TLS 1.2), and padding if a
block cipher is used.
To decrypt and verify the record, the entire record must be available.

The TLS record size lies on the 3rd and 4th bytes:

The code ended up being like this:
protected synchronized void read(SocketChannel socketChannel, SSLEngine engine) throws IOException {

    this.peerNetData.clear();
    int bytesRead = socketChannel.read(this.peerNetData);
    if (bytesRead > 0) {
        // TLS records buffering
        this.peerNetData.flip();
        byte[] justRead = new byte[this.peerNetData.limit()];
        this.peerNetData.get(justRead);
        this.tlsRecordsReadBuffer.put(justRead);
        byte[] fullTlsRecord;

        // Process every TLS record available until buffer is empty or the last record is yet not complete
        while ( (fullTlsRecord = this.getFullTlsRecordFromBufferAndDeleteIt()) != null ) {
                this.peerNetData.clear();
                this.peerNetData.put(fullTlsRecord);
                handleEncryptedTransaction(socketChannel, engine);
        }
    } else if (bytesRead < 0) {
        handleEndOfStream(socketChannel, engine);
    }
}

private synchronized byte[] getFullTlsRecordFromBufferAndDeleteIt() {
    byte[] result = null;
    this.tlsRecordsReadBuffer.flip();

    if (this.tlsRecordsReadBuffer.limit() > DEFAULT_TLS_HEADER_SIZE) {
        // Read only the first 5 bytes (5 = DEFAULT_TLS_HEADER_SIZE) which contains TLS record length
        byte[] tlsHeader = new byte[DEFAULT_TLS_HEADER_SIZE];
        this.tlsRecordsReadBuffer.get(tlsHeader);

        **// read 3rd and 4th bytes to get TLS record length in big endian notation
        int tlsRecordSize = ((tlsHeader[3] & 0xff) << 8) | (tlsHeader[4] & 0xff);**
        // 0xff IS NECESSARY because it removes one-bit negative representation
        // Set  position back to the beginning
        this.tlsRecordsReadBuffer.position(0);
        if (this.tlsRecordsReadBuffer.limit() >= (tlsRecordSize + DEFAULT_TLS_HEADER_SIZE)) {
            // Then we have a complete TLS record
            result = new byte[tlsRecordSize + DEFAULT_TLS_HEADER_SIZE];
            this.tlsRecordsReadBuffer.get(result);
        }
    }

    // remove record and get back to write mode
    this.tlsRecordsReadBuffer.compact();
    return result;
}


How to perform Drop operation into DataGridColumn?

Question: I have a project where must import data from excel to an SQL database, Excel info is not formatted and must be validated and paired to SQL column's table into my app.
I have a ListView where SQL columns are represented, and a Datagrid (AutoGenerateColumns = true) where excel info is represented.
Now user must pair ListView Items with DataGrid columns, I 'm trying to do it using DragDrop, all at ListView Is done, but Datagrid is generated dynamically and can't program each column to Set DragDrop operations.
How Can I Do To set DragDrop operations over those autogenerated DataGridColumns ?
Any Suggestions ?
I was looking for DataGrid property where I can force DataGrid to use my own ImporDataGridColumn : DataGridColumn But can't found where to do it.
UPDATE:
Finally Can add my own columns but now can't found AllowDrop on columns.... what a Nightmare.
Answer: When DataGrid's AutoGenerateColumns= False, You can use its Columns property to add Columns in runtime.
Comment: Do you have a sample ?
Comment: https://www.codeproject.com/Articles/891995/Dynamic-Columns-in-a-WPF-DataGrid-Control-Part
Answer: Finally Can't found a direct way to perform my drop over a column...
Then i set AllowDrop = true to my data grid.
When the drop is performed to my data grid, I search the column using the columns widths. Here  is the code (tks to  @mm8) who help me with the Horizontal scroll offset
    private static T GetChildOfType<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj == null)
            return null;

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            var child = VisualTreeHelper.GetChild(depObj, i);

            var result = (child as T) ?? GetChildOfType<T>(child);
            if (result != null)
                return result;
        }
        return null;
    }

    private void DGrdDatosImportar_Drop(object sender, DragEventArgs e)
    {

        ScrollViewer sv = GetChildOfType<ScrollViewer>(DGrdDatosImportar);
        if (sv != null)
        {
            double horizontalOffset = sv.HorizontalOffset;
            var dropPos = e.GetPosition(DGrdDatosImportar);
            double relativeXPos = dropPos.X + horizontalOffset;
            double RefPos = DGrdDatosImportar.RowHeaderActualWidth;
            DataGridColumn SelecteCol = null;
            foreach (DataGridColumn Col in DGrdDatosImportar.Columns.ToList())
            {
                double ColWidth = Col.ActualWidth;
                if (relativeXPos >= RefPos && relativeXPos <= (RefPos + ColWidth))
                {
                    SelecteCol = Col;
                    break;
                }
                RefPos += ColWidth;
            }
            if (SelecteCol != null)
            {
                if (e.Data.GetDataPresent("IImportProperty"))
                {
                   ...
                }
            }
        }
    }


Validação de Senha com Python

Question: Tenho a seguinte exigência para validar uma senha no cadastro:

Tamanho: 10 caracteres
Maiúscula: 2
Minúscula: 2
Número: 1
Símbolo: 2

Como faço essa verificação com python?
Por enquanto só consegui fazer isso, sendo que a parte da letra maiúscula eu só testei para ver se há alguma na senha:
if len(password or ()) < 10:
        raise forms.ValidationError("Senha mínimo 10 caracteres")
    if any(x.isupper() for x in password):
        raise forms.ValidationError("Senha tem letra maiúscula")
    if password and password_confirm:
        if password != password_confirm:
            raise forms.ValidationError("As senhas não conferem")

Answer: Para esses casos você deve usar expressões regulares.
Olha esse site depois. https://docs.python.org/3/library/re.html
import re
from django.forms import forms

def test_password(password):
    minimal_number = 2
    minimal_upper_char = 2
    minimal_lower_char = 2
    minimal_special_char = 1
    minimal_len_char = 10
    if len(password or ()) < minimal_len_char:
        raise forms.ValidationError('Senha tem que ter no mínimo '+str(minimal_len_char)+' caracteres')
    if len(re.findall(r"[A-Z]", password)) < minimal_upper_char:
        raise forms.ValidationError('Senha tem que ter no mínimo '+str(minimal_upper_char)+' letras maiusculas')
    if len(re.findall(r"[a-z]", password)) < minimal_lower_char:
        raise forms.ValidationError('Senha tem que ter no mínimo '+str(minimal_lower_char)+' letras minusculas')
    if len(re.findall(r"[0-9]", password)) < minimal_number:
        raise forms.ValidationError('Senha tem que ter no mínimo '+str(minimal_number)+' numeros')
    if len(re.findall(r"[~`!@#$%^&*()_+=-{};:'><]", password)) < minimal_special_char:
        raise forms.ValidationError('Senha tem que ter no mínimo '+str(minimal_special_char)+' caracteres especiais')

Comment: E se o usuário colocar letras acentuadas na senha? A expressão regular não vai pegá-las.
Comment: Obrigada Ytalo, apenas o de caracteres especiais não funcionou... mas entendi a lógica!
Answer: Acho que a melhor opção é você escrever o próprio validator e adicionar ele no seu settings.py. Aqui na doc do Django você pode ver como fazer, mas seria algo tipo esse rascunho que fiz abaixo (adicionei somente um exemplo, faltando implementar todos os métodos para validaçōes):
from django.core.exceptions import ValidationError
from django.utils.translation import gettext as _

class MyCustomValidator:
    def __init__(self):
        self.length = 10
        self.max_upper_case = 2
        self.max_lower_case = 2
        self.max_int_case = 1
        self.max_symbols_case = 2

    def validate_length_password(self, password):
        if len(self.password) < self.length:
            return True

        return False

    def validate_upper_case(self, password):
        pass

    def validate_lower_case(self, password):
        pass

    def validate_int_case(self, password):
        pass

    def validate_max_symbols_case(self, password):
        pass

    def validate(self, password, user=None):
        if self.validate_length_password(password):
            raise ValidationError(
                _("This password must contain at least "
                    "%(length)d characters."),
                code='password_too_short',
                params={'length': self.length},
            )

    def get_help_text(self):
        return _(
            "Your password must contain at least %(length)d characters."
            % {'length': self.length}
        )

Daí lá no seu settings.py no AUTH_PASSWORD_VALIDATORS você adiciona essa classe, tipo:
AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'path.onde.ta.o.arquivo.do.seu.validator.finalizando.com.a.classe.MyCustomValidator',
    },
]

Validação de Senha com Python


Is there a media query type way to switch js files based on device?

Question: I'm attempting to get my mobile layout working for my site and while the media query with the different stylesheet appears to be working I can't seem to figure out if it's even possible to load a separate javascript file attached to that media query.
Clarification:
<link rel="stylesheet" type="text/css" href="desktop.css">
<link rel="stylesheet" type="text/css"
      media="only screen and (max-width: 720px) and (-webkit-min-device-pixel-ratio: 2.0)" href="jesusmobile.css">

and at the end of my html doc before the /body

Basically I want to have it so that when my site uses the "mobile.css" stylesheet I can also tell it to use a different .js file. Is this possible?
Thanks!
Comment: http://stackoverflow.com/questions/17022222/appending-script-src-script-to-head-based-on-screen-width
Comment: thanks! seems to be what i'm looking for, although I'm pretty noob to js so I'm not too sure I can get it to work.. thanks though!
Answer: You can refer this link http://www.quirksmode.org/blog/archives/2010/08/combining_media.html
if (screen.width < 600) {
    // don't download complicated script
    // use low-source images instead of full-source ones
}

or
if (document.documentElement.clientWidth < 900) {
    // scripts
}


Divergent and Convergent sums

Question: Does sum convergence or divergence indicates anything for we can determine if the sum has an explicit formula?
i.e  $\sum_{k=1}^n \frac 1k$ is divergent, does this mean we can/can't devise an explicit formula for this sum?
Comment: No, consider just sum $\sum_{k=1}^n k$
Comment: @kingW3 Or just $\sum_{k=1}^n 1$
Answer: No, it does not mean anything. In fact, we define the harmonic numbers as follows:
$$H_n=\sum_{k=1}^{n} \frac{1}{k}$$
Because it does not have a nice closed form. But some of them have, for example:
$$\sum_{i=1}^{n} i=\frac{n(n+1)}{2}$$
Or
$$\sum_{i=1}^{n} i^2=\frac{n(n+1)(2n+1)}{6}$$
Comment: (+1) that should suffice
Comment: Thank you, @MarkViola :)
Answer: There is an asymptotic estimate for this sum, usually denoted by $H_n$, namely
$$H_n=\ln n+\gamma+\frac1{2n}-\frac1{12n^2}+\frac1{120n^4}+O\left(\frac1{n^6}\right)
$$
Up to the Landau big Oh, this is an explicit formula.
Comment: … that is not a closed form.  It is an asymptotic expansion.
Comment: Yes, I read what you wrote.  But it does answer the OP's question.
Comment: @MarkViola Yes, as I said, up to the Landau big Oh.

Divergent and Convergent sums


How to wait for async method to return?

Question: I'm trying to create an app which uses single sign on with live connect and registers the user automatically based on his live connect information.
Right when my application starts I call my LiveConnectViewModel and call those two functions:
        public async void AuthInitialize()
    {
        string[] scopes = new[] { "wl.signin", "wl.basic", "wl.photos", "wl.emails", "wl.skydrive" };

        liveAuthClient = new LiveAuthClient();
        LiveLoginResult liveLoginResult = await liveAuthClient.InitializeAsync(scopes);

        if (liveLoginResult.Status == LiveConnectSessionStatus.Connected)
        {
            App.Session = liveLoginResult.Session;
        }
        else
        {
            liveLoginResult = await liveAuthClient.LoginAsync(scopes);
            App.Session = liveLoginResult.Session;
        }
        await LoadProfileAsync();
    }

    public async Task LoadProfileAsync()
    {
        LiveConnectClient client = new LiveConnectClient(App.Session);
        LiveOperationResult liveOperationResult = await client.GetAsync("me");
        LiveOperationResult liveOperationResultPicture = await client.GetAsync("me/picture");
        dynamic dynResult = liveOperationResult.Result;
        dynResult.picture = liveOperationResultPicture.Result;
        User currentUser = await UserViewModel.findOrCreateUserAsync(dynResult);
        App.currentUser = currentUser;
    }

Right after that another method is invoked when the splash screen disappeared and my View comes up.
This execute that code:
        public ProfilePageViewModel()
    {
        this.CurrentUser = App.currentUser;
    }

Now my overall problem is the timing here. When I debug my application I can see that my LiveConnectViewModel gets called first and makes the initial connect, then the LoadProfileAsync gets executed until my program reaches the first await statement.
Immediately when reaching this point my application stops working on this call and goes on. Now when it comes to my ProfilePageViewModel and it should set my "CurrentUser" Property the await still didn't returned my User to my App.CurrentUser Property which is why it says it's null.
Normally I would just wait until the App.CurrentUser Property is set to something else than null, but I can't get rid of the strange feeling that I either missed to implement the MVVM Pattern right or that I did something extremely wrong with await and async...
Can anyone help me clarify this behavior?
Comment: Remove the `await` from in front of the call to LoadProfileAsync(); The `await`ing is already being done inside LoadProfileAsync().
Comment: Or vice-versa, if that suits your business need more appropriately. There is no need to await twice, ad doing so only confuses matters.
Answer: You should avoid async void, as I describe in a recent MSDN article.
Once you make AuthInitializeAsync return Task instead of void, then you can await it before creating your view model.
Comment: I have a blog entry on [`async` constructors](http://blog.stephencleary.com/2013/01/async-oop-2-constructors.html). In my MVVM projects I follow the VM-first pattern with `async` factory methods. But if you're using an IoC or V-first pattern then you'll need to use the Async Initialization Pattern as described on my blog.
Comment: Hi Stephen, thanks for your answer. I already read your MSDN article in another post here on stackoverflow. My problem is, if I would change the return type to Task I could not call this function correctly in my constructor (which is what I'm actually doing). Is there any way to make this still work in my constructor?

How to wait for async method to return?


Column with list values, if list has more than 1 element, decode both values and include in a list in the same row in new column

Question: I have two columns, cat_1 and cat_2, that have gibberish for values in them. This is because I will be referencing a python library to derive info from those gibberish values.
If a list has more than 1 element in a row, whether in cat_1 or cat_2, I want to go ahead and fill the row of a new column, results_1 or results_2, with its values separated by a comma. Examples shown below.

How can I get from cat_1 and cat_2... to results_1 and results_2?
Answer: You need some kind of mapper dictionary that tell us which gibberish word is which English word. Something like:
mapper = {'klys': 'apple', 'chrs': 'orange', 'pqor': 'bag'}

Given such a mapper, you can:
df[['results_1','results_2']] = df[['cat_1','cat_2']].stack().str.split(', ').explode().map(mapper).fillna('').groupby(level=[0,1]).apply(lambda x: list(x) if len(x)>1 else (x.values[0] if len(x)==1 else '')).unstack()


How to pass claims mapping options to IdentityServerJwt in ASP.NET Core 3.0 Preview 5?

Question: Inspired by an article on custom claims, I've added a tenant id custom claim to my Identity server sign in process as follows:
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using MyNamespace.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using MyNamespace.Data;
using MyNamespace.Constants;

namespace MyNamespace.Factories
{

    public class TenantClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser>
    {
        public TenantClaimsPrincipalFactory(
            UserManager<ApplicationUser> userManager,
            IOptions<IdentityOptions> optionsAccessor)
            : base(userManager, optionsAccessor) {
        }

        // TODO: Remove hard binding to application db context
        protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user) {
            var identity = await base.GenerateClaimsAsync(user);
            var tenantId = ApplicationDbContext.DefaultTenantId;
            if (user.TenantId != Guid.Empty) {
                tenantId = user.TenantId;
            }
            identity.AddClaim(new Claim(CustomClaimTypes.TenantId, tenantId.ToString()));
            return identity;
        }
    }

}

The claims generating method is executed at login and claims are added to the identity, so this part seems ok. Later I try to read out the claim later in my tenant provider service as follows
using System;
using MyNamespace.Data;
using Microsoft.AspNetCore.Http;
using System.Security.Claims;
using System.Linq;
using MyNamespace.Constants;

namespace MyNamespace.Services
{

    public interface ITenantProvider
    {
        Guid GetTenantId();
    }

    public class TenantProvider : ITenantProvider
    {
        private IHttpContextAccessor _httpContextAccessor;

        public TenantProvider(IHttpContextAccessor httpContextAccessor
        {
            _httpContextAccessor = httpContextAccessor;
        }

        // TODO: Remove hard binding to application db context
        public Guid GetTenantId()
        {
            var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            var user = _httpContextAccessor.HttpContext.User;
            var tenantId = _httpContextAccessor.HttpContext.User.FindFirst(CustomClaimTypes.TenantId).Value;

            Guid tenantGuid = ApplicationDbContext.DefaultTenantId;
            Guid.TryParse(tenantId, out tenantGuid);

            return tenantGuid;
        }
    }

}

As far as I understand, however, the claim identified by CustomClaimTypes.TenantId is not automatically mapped by the Identity server. My question is this: how can I map
options.ClaimActions.MapUniqueJsonKey(CustomClaimTypes.TenantId, CustomClaimTypes.TenantId);

from Startup.cs where I add the Identity server the my dependencies:
services.AddAuthentication()
            .AddIdentityServerJwt();

Answer: So, in the end I ended up with a different solution than what I sought originally. Instead of mapping the claims as created by the factory, I came across another post here at StackOverflow. Basically, what I did was the following. I implemented the following ProfileService
namespace MyNamespace.Services
{

    public class ProfileService : IProfileService
    {
        protected UserManager<ApplicationUser> _userManager;

        public ProfileService(UserManager<ApplicationUser> userManager)
        {
            _userManager = userManager;
        }

        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            var user = await _userManager.GetUserAsync(context.Subject);

            var claims = new List<Claim>
            {
                new Claim(CustomClaimTypes.TenantId, user.TenantId.ToString()),
            };

            context.IssuedClaims.AddRange(claims);
        }

        public async Task IsActiveAsync(IsActiveContext context)
        {
            var user = await _userManager.GetUserAsync(context.Subject);

            context.IsActive = (user != null) && user.IsActive;
        }
    }

}

Then, I added the service in the DI Container at Configure:
services.AddIdentityServer()
                .AddApiAuthorization<ApplicationUser, ApplicationDbContext>()
                .AddProfileService<ProfileService>();

services.AddAuthentication()
                .AddIdentityServerJwt();

So, I still have a good time letting AddIdentityServerJwt setting up IdentityServer4, while getting my claims at the same time.
Comment: @MarkRedman are you trying to access the identity info from the server side? If you want to access the user session from one of your controllers, you could use `var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;`. Be sure to pass in `IHttpContextAccessor httpContextAccessor` in your controller constructor first, retaining a reference to `httpContextAccessor` in a private field.
Comment: I have started using the same preview example, the client app logs in but I cant seem to get any Identity info (user name, claims etc) How do I reference the ProfileService and what do I pass in for the context?
Comment: I have no claims coming through (and manually added to AspNetUserClaims to test)  I just know the user is AUthenticated, this is the default implementation of:  https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity-api-authorization?view=aspnetcore-3.0, using react.
Comment: Hmm - I don't know, man. Maybe it'd be easier to assist you if you wrote a separate question on the issue you're facing, telling us how you manually added to AspNetUserClaims and so on. Give me a heads up, and I'll look into it. ;)
Comment: I will do some more research and see if I am missing any additional configuration...


Logrotate rotating daily but should be rotating weekly

Question: For some reason logrotate is rotating logs daily instead of weekly, even though all the config files for logrotate seem to be set to weekly. Any ideas?
/etc/cron.daily/logrotate
#!/bin/sh

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf

/etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# uncomment this if you want your log files compressed
#compress

# packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
    missingok
    monthly
    create 0664 root utmp
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0660 root utmp
    rotate 1
}

# system-specific logs may be configured here

/etc/logrotate.d/apache
/var/log/apache2/*.log {
        weekly
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        postrotate
                if [ -f "`. /etc/apache2/envvars ; echo ${APACHE_PID_FILE:-/var/run/apache2.pid}`" ]; then
                        /etc/init.d/apache2 reload > /dev/null
                fi
        endscript
}

Comment: I'm having the exact same problem, were you able to solve it?
Answer: Notice this line in /etc/logrorate.conf:
include /etc/logrotate.d

Perhaps you have some stale or backup files lying around in that directory, which get included, and specify daily rotation?
As a workaround, and if you still can't figure it out, and if you only ever want to have your files rotated weekly, then you can change the frequency with which logrotate is executed through cron, thus changing the precision of log rotation from daily to weekly.
Comment: The program-specific logrotate files in /etc/logrotate.d are actually what I want to run, my particular interest in this case being the Apache logrotate config file I posted above. There is only one apache file in that directory. No forgotten backups. Also, I'm trying to avoid using cron.weekly for this, as the impression I get is that that violates best practices.
Comment: For me, in `/etc/logrotate.d/apache2`, the rotation was configured to occur `daily`.
Answer: You have to force a rotation manually so it reloads the configuration:
sudo logrotate -f /etc/logrotate.conf

Also... don't force it on your crontab, there keep it without flags:
#logrotate
0  0    * * *   /usr/sbin/logrotate /etc/logrotate.conf


Set a html5 game to an iframe

Question: Im trying to set a html5 game to an Iframe.

The problem is the game url needs authentication header (token) and I can't set token on iframe src.(as I know so far)
So I fetched the given game url using axios. and then passed em to Iframe srcdoc attribute no surprise I got errors. So what is the solution ?
here is what I get from the api on postman in which i can send http headers like authentication tokens. But how can i set it on iframe?
<!DOCTYPE html>

   <meta charset="utf-8">
   <title>Cocos Creator | FlappyTest1</title>

   <meta name="viewport"content="width=device-width,user-scalable=no,initial-scale=1, minimum scale=1,maximum-scale=1" />

   <canvas id="GameCanvas" oncontextmenu="event.preventDefault()" tabindex="0"></canvas>

Im using React and redux  but I think it doesn't matter
Comment: Is that the exact HTML that was returned by the API? There is a missing `<` in the first script tag.
Comment: @Turnip No that's a huge html file. I just wanted to show  that  the api result contains
Comment: Consider reading this: [mre]
Comment: @tevemadar although it was clear. i made some changes to make what i want clear
Comment: Does this answer your question? [Is it possible to add Request Headers to an iframe src request?](https://stackoverflow.com/questions/13432821/is-it-possible-to-add-request-headers-to-an-iframe-src-request)
Comment: At this very moment both script tags refer to local JavaScript files, `main.62040.js` is expected to be immediately next to your HTML and `settings.718ea.js` in a folder called `src`. If those files exist, they should get loaded and then something happens or not. If those files do not exist, they obviously won't get loaded and nothing will happen. You tell about errors, but forgot describing them. If you see 404-s, that means the files do not exist, and simply get them. Repeat the same with all missing files, use the developer console to see them.
Comment: @tevemadar man my problem is not with the api result. My problem is first how to set http header on iframe and as i know it's kinda impossible to set header on it. so we should use fetch and GET mehod to get data with included header. Now the we got api result (which I posted em on top of post) how to show the api result in an iframe?
Answer: I didn't find any solution for this. Even making a blob from that url doesn't change anything. The solution that I found is kinda related to back-end, where we can pass authentication token as part of url paramaters on src of iframe

Set a html5 game to an iframe


Which test cases/tests should I run?

Question: I'm a bit confused about best practices for testing in Drupal 7.
When I'm going to deploy, should I run the entire test suite—including all Drupal core tests—every time? This seems like a bit of overkill, especially given how long each test takes to run.
At the same time, it seems logical that I should run all tests every time, since code changes I make in my custom modules could break something in core (via hooks, etc.). The problem is, each test runs in a sandbox with only its dependencies enabled, so core tests won't even be aware of my custom modules and thus won't fail even if they should.
I'm accustomed to testing in Rails, in which tests are run against the entire application as a cohesive unit, with all "modules" (gems) being enabled at test runtime. It seems that in Drupal, only certain pieces are tested at a time, which prevents an accurate assessment of whether the site will work in production.
So I guess my question is twofold: A) Should I run the entire Drupal test suite (including core tests) every time I deploy? B) How do I check whether my custom code causes core tests to fail, given that core tests don't take custom modules into account?
Answer: No, running Drupal core test suite is wrong. You can assume that your code won't messed up drupal core and if you so - you'll get exception for that and then the fun will start :).
If you came from the ruby world then you probably familiar with the Behat solution and if not you should check this - very easy to use QA platform and a lot of posts were written in order to test your site with behat. But there is also selenium that can help you.
And i'll give a you a tip on how to start the testing suite for your site: For now you probably have a running site. You need to take a pen and paper or a google doc and start what features you create for your site: what happens when the use login, what happens when he create node. After you wrote your tests you can start to write them in test - behat, selenium simple test... what ever! As long as those tests testing your features.
Another tip is: when ever you have a bug and you fixed this, you write a test that verity that the bug can't be reproduced. This the way all of the big projects i know works: OG, Message stack, Open scholar,
Comment: Thanks! We currently have an RSpec/Capybara integration test suite for our site, but I had thought it'd make sense to convert it to native Drupal testing. So, SimpleTest is really more for unit test and for testing patches against, then?
Comment: You can write your test suite with simpletest but it won't be simple. It's because simple build the database from scratch and you need to enable your modules and features and settings and you need to do that programaticlly.

Instead, when using a behat/selenium against a development environment with the latest DB the the tests are more faster and easier. I would recommend on behat. For now, this the QA platform were using but any BDD solution would be great.
Comment: Sure, but again, what is the purpose of having SimpleTest, then?
Comment: I think the only reason is exists is for the module development. People  want to test their site easily. Now we have JS as integrated part and simple test is not the tool for that.

Which test cases/tests should I run?


Definition of Ito integral: Progressively measurable vs. measurable/adapted

Question: Consider the probability space $(\Omega,\mathcal{F}\,P)$ and the filtration $\mathcal{F}_t$.
In Oksendal's book the Ito integral $I(f)(\omega)=\int_S^T f(t,\omega)dB_t(\omega)$ is defined on a space $\mathcal{V}(S,T)$. This is the space of functions such that

$(t,\omega)\to f(t,\omega)$ is $\mathcal{B}\times\mathcal{F}$ measurable where $\mathcal{B}$ is the borel sigma algebra on $[0,\infty)$.
$f(t,\cdot)$ is $\mathcal{F}_t$ adapted.
$E[\int_S^Tf^2(t,\omega)dt]<\infty$

 Then the Ito integral of $f$ is defined by $$\int_S^T f(t,\omega)dB_t(\omega):=\lim_{n\to \infty}\int_S^T \phi_n (t,\omega) dB_t(\omega)\,\,in\,\, L^2(P)$$ where $\{\phi_n\}$ is sequence of elementary functions such that $$E\Big[\int_S^T (f(t,\omega)-\phi_n(t,\omega))^2 dt\Big]\to 0 \,\,as\,\,n\to\infty$$
In some other text books I found that instead of this space $\mathcal{V}$ the space (say $\mathcal{W}$) of progressively measurable processes such that $(3)$ holds. Now I am quite sure that $\mathcal{W}\subset \mathcal{V}$. But I am wondering why the difference in approaches? As far as I have noticed, they lead to the same assertions ( the integrals are martingales etc etc.). Or is there any striking difference between these two approaches?
Answer: In general, this problem has been addressed in this question on Math Overflow. In particular, I quote a comment:

If the process has continuous sample paths (actually just left
  continuous or right continuous is enough), then adapted and
  progressively measurable are the same thing. See Proposition 4.8 in
  Revuz & Yor.

The author of the comment is Martin Hairer, so I don't doubt it's correct.


$e^{mx}$ in solving second order differential equations

Question: In a book I am reading on differential equations, the author writes about the solution to a homogenous, linear, second order differential equation with constant coefficients. The author says something like, "Let us suppose that the solution is of the form $y=e^{mx}$ " . After this the author introduces the characteristic equation of a differential equation of the form mentioned above, and proceeds to describe how to solve it w/ undetermined coefficients, variation of parameters, etc.
How did mathematicians first come up with this "assumption" that the solution was of the form $y=e^{mx}$? And, are there any other forms of solutions for these types of equations?
Comment: Well, exponentials are good first guess. And it turns out they work, together with the appropriate treatment for repeated roots. Then there are theorems that guarantee that once we have found such solutions there are no other solutions. Keep studying ODE theory & real analysis and you'll eventually get to those results.
Comment: This is a good question, and it's always good to try to figure out how people came up with ideas. But I do not think there is a satisfying answer other than "Someone thought to try it, and it works every time." This wouldn't be the first or the last time that problems get solved in that way.
Comment: It seems if  you ask a question like this here, you get guesses.  Maybe to get actual references on "how did they first come up with that?", you should ask in http://hsm.stackexchange.com .
Comment: Like a lot of things in mathematics, you start with a good guess, and then prove that guess.
Answer: I like to think of it as an extension of the solution of the differential equation
$$
\frac{\mathrm{d}y}{\mathrm{d}x}=ky
$$
which, as can be easily checked using separation of variables, is
$$
y=y_0\mathrm{e}^{kx}.
$$
Now if we have a second order ODE
$$
\frac{\mathrm{d}^2y}{\mathrm{d}x^2}+a\frac{\mathrm{d}y}{\mathrm{d}x}+by=0
$$
where $a,b\in\mathbb{R}$, it is possible to re-write this as a pair of first-order ODEs. If we set $z=\frac{\mathrm{d}y}{\mathrm{d}x}$ (one of our equations) then we have the system of coupled ODEs
$$\cases{\dfrac{\mathrm{d}y}{\mathrm{d}x}=z \\\dfrac{\mathrm{d}z}{\mathrm{d}x}=-by-az}
$$
and this can be solved by solving the equation $\dfrac{\mathrm{d}\boldsymbol{y}}{\mathrm{d}x}=A\boldsymbol{y}$, where $\boldsymbol{y}=(y(x),z(x))^\top$ and $$A=\left(\begin{matrix}0 & 1\\
-b & -a
\end{matrix}\right)$$
and since this is a linear ODE (in more than one variable), it can be solved in an analogous way (using an integrating factor method)
\begin{aligned}\dfrac{{\rm d}\boldsymbol{y}}{{\rm d}x}-A\boldsymbol{y} & =0,\\
{\rm e}^{-\int A{\rm d}x}\dfrac{{\rm d}\boldsymbol{y}}{{\rm d}x}-A\boldsymbol{y}{\rm e}^{-\int A{\rm d}x} & =0,\\
\dfrac{{\rm d}}{{\rm d}x}\left(\boldsymbol{y}{\rm e}^{-\int A{\rm d}x}\right) & =0,\\
\dfrac{{\rm d}}{{\rm d}x}\left(\boldsymbol{y}{\rm e}^{-Ax}\right) & =0,\\
\boldsymbol{y}{\rm e}^{-Ax} & =\boldsymbol{C},\\
\therefore\boldsymbol{y}(x) & =\boldsymbol{C}{\rm e}^{Ax}.
\end{aligned}
I'm sure with some more effort, it will be possible to recover linear combinations of exponentials in the individual components of the solution. Probably seems a bit rambly, but this is one way I like to convince myself where the exponential solutions come from - it is the solution to an aforementioned first-order differential equation, so by linearity, more exponentials will appear in the solution. (Don't know if that explains things well enough.)
Comment: There is a wrong sign between the first and second equation.
Comment: Sorted now, cheers!
Answer: Probably the idea comes from the first order equation, where
$$
y'+ky=0
$$
could be solved by separation of variables
$$
\frac{y'}{y}=-k\implies \ln y=-kx+c\implies y=Ce^{-kx}.
$$
Answer: $e^{mx}$ turns out to be the general form for solutions to linear differential equations with constant coefficients.  One can also show that solutions to "equipotential" equations, equations of the form $ax^2y''+ bxy'+ cy= 0$ tend to be $x^n$ for some number $n$.
Comment: The TeX delimiter are `$` for inline equation, `$$` for centered equation. `**` to delimit bold.
Answer: This captures previous comments in a slightly different formalism.
Taking a derivative is a linear operator:Given functions $f$ and $g$, and real constants $\alpha$ and $\beta$, $$d(\alpha f + \beta g)/dx=\alpha df/dx+\beta dg/dx$$
If a linear operator returns a scalar multiple of its input, the input is an Eigen Vector and the scalar multiple is an Eigen Value.
Given $$d(e^{mx})/dx=me^{mx}$$, $m$ is the Eigen Value and $e^{mx}$ is the Eigen Vector, or Eigen Function in this case.
This allows for various concepts of linear algebra to be applied to the solutions of these problems.


Changing product price depending on node field in commerce node checkout

Question: i am using comerce_node_checkout with drupal_commerce
I want to change product price depending on node field .
eg,
I have a field as field_gender if we select male then price will be 1000.00 or if we select female then it will be 500.
Any help is most appriciable.
Thanks in advance .
Comment: Wow, women go half price? Think I'll be wearing a dress to your website...
Answer: Ohhh!!!!!!!!
Power Of Rules
 { "rules_update_amount_female" : {
    "LABEL" : "Update amount Female",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "Update amount" ],
    "REQUIRES" : [
      "rules",
      "commerce_line_item",
      "commerce_product_reference",
      "commerce_cart"
    ],
    "ON" : {
      "commerce_product_calculate_sell_price" : [],
      "commerce_cart_product_prepare" : []
    },
    "IF" : [
      { "entity_has_field" : {
          "entity" : [ "commerce-line-item" ],
          "field" : "commerce_node_checkout_node"
        }
      },
      { "entity_has_field" : {
          "entity" : [ "commerce-line-item:commerce-node-checkout-node" ],
          "field" : "field_ap_gender"
        }
      },
      { "data_is" : {
          "data" : [ "commerce-line-item:commerce-node-checkout-node:field-ap-gender" ],
          "value" : "Female"
        }
      }
    ],
    "DO" : [
      { "commerce_line_item_unit_price_amount" : {
          "commerce_line_item" : [ "commerce_line_item" ],
          "amount" : "500",
          "component_name" : "base_price",
          "round_mode" : "1"
        }
      }
    ]
  }
}


Missing or insufficient permissions firestore database rules

Question: I'm self studying firestore and I could not figure out a way to only allow a user to update, delete or read only the collections added by them.
This is the structure I'm using:

I use firebase auth for user handing. I save the currentUser.uid as user_id in the database for each collection.
These are the rules I'm using
service cloud.firestore {
  match /databases/{database}/documents {

    match /tasks{
      allow read, update, delete: if request.auth.uid == resource.data.user_id;
      allow create: if request.auth.uid != null;
    }
  }

When I try to read/get the data I get Missing or insufficient permissions error.
I'm using the web api (JavaScript) for firestore. This is the code I'm using to read data.
function read() {

    db.collection("tasks").get().then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
            var newLI = document.createElement('li');

            newLI.appendChild(document.createTextNode(doc.data().task));

            dataList.appendChild(newLI);

        });
    });

}

Answer: the error was in my JavaScript I was getting all without filtering by user
function read() {
    let taskColletion = db.collection("tasks");

    taskColletion.where("user_id", "==", firebase.auth().currentUser.uid).get().then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
            var newLI = document.createElement('li');

            newLI.appendChild(document.createTextNode(doc.data().task));

            dataList.appendChild(newLI);
        });

    });

}

Comment: I found the same. The security rule filter must be added to the where clause. Even in cases where you wouldn't have the where clause otherwise (such as query a hierarchical sub-collection with a specific parent document id that all the children are known to match the where clause)
Comment: To elaborate on the root cause behind why your answer is correct [Rules are not Filters](https://firebase.google.com/docs/firestore/security/rules-conditions#rules_are_not_filters)

> You cannot write a query for all the documents in a collection and expect Cloud Firestore to return only the documents that the current client has permission to access.
Answer: This is actually explained on the Firestore Documentation(I recommend reading it).
You're missing a wildcard after /tasks:
service cloud.firestore {
  match /databases/{database}/documents {
    match /tasks/{task} {
      allow read, update, delete: if request.auth.uid == resource.data.user_id;
      allow create: if request.auth.uid != null;
    }
  }
}

Comment: I've tried that already it but it's still giving me the `Missing or insufficient permissions`


Averages of the two closest pairs out of a set of four observations

Question: Four random numbers are drawn at random from a standard normal distribution. They are grouped in two pairs of closest numbers, $\{x_1, x_2\}$ and $\{x_3, x_4\}$ so that $x_1\le x_2 \le x_3 \le x_4$, and the average of each pair is computed. What are the expected averages of each pair? I found papers dealing with the averages of 2 out 3 observations (e.g., here), but I wasn't able to find anything for this specific problem.
Comment: It looks like you might have overcomplicated your question.  Because the $x_i$ are ordered, the symmetry of the standard Normal distribution implies $(x_1,x_2)$ and $(-x_4,-x_3)$ are identically distributed.  Therefore you only have to answer the question for the two smallest out of four. Moreover, the expected average is the average of the expectations of $x_1$ and $x_2$ separately. These expectations can be found in the same manner as for any order statistic in a sample of $n.$ For $n\le 5$ (as I recall) there may be exact formulas; for larger $n,$ numerical integration is required.
Comment: Further to @whuber's comment, [Harter (1961)](https://doi.org/10.2307/2333139) gives an explicit formula (use $n=4$ and $p=1$) involving an improper integral. There is likely something newer out there.
Answer: Let $\Phi$ be the standard Normal distribution and
$$\phi(z) = \frac{\mathrm{d}}{\mathrm{d}z}\Phi(z) = \frac{1}{\sqrt{2\pi}}e^{-z^2/2}$$
be its density function.  The the largest of a sample $(x_1,x_2,x_3,x_4)$ from this distribution, often written $x_{4;4},$ has distribution function
$$\Pr(x_{4;4}\le z) = \Pr(x_1\le z;x_2\le z; x_3\le z;x_4\le z) = \Phi(z)^4$$
(the latter arising from the independence of the $x_i$) and therefore has density
$$\phi_{4;4}(z) = \frac{\mathrm{d}}{\mathrm{d}z} \Phi(z)^4 =  4\Phi(z)^{3}\phi(z).$$
Its expectation can be simplified a little via integration by parts upon exploiting the basic relation $\phi^\prime(z) = -z\phi(z),$ whence
$$\int z\,\Phi(z)^{3}\phi(z)\mathrm{d}z = -\phi(z)\Phi^{3}(z)\bigg|_{-\infty}^\infty + \int 3\Phi(z)^{2} \phi(z)^2\mathrm{d}z.$$
At this juncture we need a "trick"--a new idea.  One that works is to introduce a new variable $a$ and analyze the function
$$g_2(a) = \int \Phi(a z)^2 \phi(z)\,\mathrm{d}z.$$
This is differentiable, with a derivative we may compute mechanically as
$$\begin{aligned}
\frac{\mathrm{d}}{\mathrm{d}z}  g_2(a) &=  \int \frac{\mathrm{d}}{\mathrm{d}z}  \Phi(a z)^2 \phi(z)\,\mathrm{d}z \\
&= 2\int  z \phi(az)  \Phi(a z) \phi(z)\,\mathrm{d}z \\
&= \frac{2}{2\pi}\int  z  \Phi(a z)\exp\left(-(z^2 + a^2z^2)/2\right) \,\mathrm{d}z \\
&= \frac{2}{2\pi(1+a^2)}\int  z(1+a^2)  \Phi(a z)\exp\left(-(z^2 + a^2z^2)/2\right) \,\mathrm{d}z \\
&= \frac{2}{2\pi(1+a^2)}\int  \Phi(a z)\,\frac{\mathrm{d}}{\mathrm{d}z} \exp\left(-(z^2 + a^2z^2)/2\right) \,\mathrm{d}z
\end{aligned}$$
This last expression is a lovely candidate for an integration by parts: because the exponential decreases rapidly for large $|z|,$ the resulting integral is
$$\begin{aligned}
\frac{\mathrm{d}}{\mathrm{d}z}  g_2(a) &= \frac{2}{2\pi(1+a^2)}\int  \frac{\mathrm{d}}{\mathrm{d}z} \Phi(a z)\,\exp\left(-(z^2 + a^2z^2)/2\right) \,\mathrm{d}z \\
 &= \frac{2a}{2\pi(1+a^2)\sqrt{2\pi}}\int \exp(-a^2z^2/2)\,\exp\left(-(z^2 + a^2z^2)/2\right) \,\mathrm{d}z \\
&= \frac{2a}{2\pi(1+a^2)\sqrt{1+2a^2}}.
\end{aligned}$$
The last evaluation arises by noting the integrand is a multiple of a Normal density function (with variance $1+2a^2$).
$g_2(a)$ can be recovered by integrating this expression with respect to $a.$  It can be computed via elementary means (substitutions), yielding
$$g_2(a) = C + \frac{1}{\pi} \tan^{-1}\sqrt{1 + 2a^2}$$
and, since $g_2(0) = \Phi(0)^2\int \phi(z)\mathrm{d}z = 1/4,$ the constant of integration is $C=0.$
This applies to our previous analysis to produce
$$E[x_{4;4}] = \int z \phi_{4;4}(z)\,\mathrm{d}z = 4 \int 3\Phi(z)^2\phi(z)^2\,\mathrm{d}z = \frac{12}{{2\pi}} \int \Phi(z)^2 e^{-z^2}\,\mathrm{d}z.$$
Substituting $z = u/\sqrt{2}$ puts this into the form

$$E[x_{4;4}]  = \frac{12}{\sqrt{2\pi}}\frac{1}{\sqrt 2}\, g_2\left(\frac{1}{\sqrt 2}\right) = \frac{6}{\pi \sqrt{\pi}}\tan^{-1}\sqrt{2} \approx 1.029375\ldots$$

This is the only integral we need compute, because the distribution function of the second-highest sample value is
$$\Pr(x_{4;3} \le z) = 12 \Phi(z)^2(1-\Phi(z))\phi(z),$$
giving
$$E[x_{4;3}] = 12 \int z\left[\Phi(z)^2(1-\Phi(z))\phi(z)\right]\,\mathrm{d}z = 4E[x_{3;3}] - 3E[x_{4;4}].$$
To find the expectation of $x_{3;3},$ integrate by parts as before to produce an integral in the form of How can I calculate $\int^{\infty}_{-\infty}\Phi\left(\frac{w-a}{b}\right)\phi(w)\,\mathrm dw$.  The result is $3/(2\sqrt{\pi}).$  Thus

$$E[x_{4;3}] = 4\frac{3}{2\sqrt\pi} - 3\frac{6}{\pi \sqrt{\pi}}\tan^{-1}\sqrt{2} \approx 0.29701138\ldots$$

Finally, the question asks for the expectations

$$-E[(x_{4;1} + x_{4;2})/2] = E[(x_{4;4} + x_{4;3})/2] = \frac{3}{\pi\sqrt\pi} \tan^{-1}\sqrt{2} - \frac{3}{2\sqrt \pi} \approx 0.69424101\ldots$$

These are negatives of each other because the standard Normal distribution has a mean of zero, which implies
$$\begin{aligned}
0 &= (E[x_1] + E[x_2])/2 + (E[X_3] + E[x_4])/2 \\
&= E[x_1+\cdots + x_4]/2 \\&= E[x_{4;1} + \cdots x_{4;4}]/2 \\&= E[(x_{4;1}+x_{4;2})/2] + E[(x_{4;3}+x_{4;4})/2].\end{aligned}$$
Reference
Similar techniques can be applied to find positive integral moments of order statistics from random Normal samples of sizes $n=1,2,3,4,5.$  For larger sample sizes, numerical integration appears to be required.
Balakrishnan, N. and A. Clifford Cohen, Order Statistics and Inference.  Academic Press, 1991: Section 3.9.


Integrating over the distribution of a function (Lebesgue integral)

Question: Let $F(b)=\mu(\{ t \in [T, 2T]: \ln| f(2+it)| \geq b\})$.
$$ \int_{T}^{2T} |f(2+it)|^2 dt= -\int_{-\infty}^{\infty} e^{2b} dF(b).$$
Can someone help explain how we get the integral on the RHS? I know that we can write $|f(2+it)|^2 = e^{\ln |f(2+it)|^2} = e^{2 \ln |f(2+it)|}.$  I read about Lebesgue integrals and tried applying what I know, but I am stuck. I don't get why there is a negative sign, how we get an infinite integral, etc. Please help!
Answer: Hint: Let $g(t)=|f(2+it)|^{2}$. Show that $\int \phi (t) d\mu \circ g^{-1}(t)=-\int \phi (b)dF(b)$ for all non-negative measurable functions $\phi$ by first checking that this holds when $\phi$ is the charcateristic function of an interval $(a,b]$. [You can then pass to finite disjoint unions of such intervals and then  use standard measure theoretic arguments to show that the equation holds for all non-negative measurable functiosn $\phi$]. Now you get your result by taking $\phi (t)=e^{2t}$.
Comment: Yes, it is a change of variable formula.  @blueskyscroll
Comment: @blueskyscroll In the defintion of $F(b)$ if we had $\leq b$ on the right side (which is standard way of defining distribution functions in Probablity Theory) then we wouldn't have the minus sign.
Comment: Hi Kavi, thank you for the hint. Did you mean $\int g(t) d \mu(t) = - \int \phi(b) dF(b)$? Also, when you say "characteristic function", is that the same as the indicator function? I will have to think about your hint… I'm not competent enough to follow your hint right away! :( Thank you though.
Comment: I have corrected the equation. Yes, characteristic function (used in measure theory) is same as indicator function (used in Probability Theory). @blueskyscroll
Comment: Ah okay! Thank you. Lastly, is that the change of variable formula in probability?? I had a hard time understanding that proof, but it seems relevant to my problem so I must try harder. Thank you.
Comment: Okay, I don't see why we have a negative, but I will focus now and think about it. THANK YOU SO MUCH! :)
Comment: Right! thank you for that second hint :)


Properties of this ODE

Question: Given the ODE $$
    \{e^{\alpha\sin\theta}\Psi'(\theta)\}'+
     e^{\alpha\sin\theta}\beta\cos^{2}\theta \Psi(\theta) =
     \lambda e^{\alpha\sin\theta}\Psi(\theta).
$$
where $\theta \in [-\pi,\pi]$, $||\Psi||_{L^2} < \infty$.
I was wondering whether there is anything we can say about this problem:
Does it have a (unique) solution?
Does it have analytic solutions (besides trivial ones)?
What can we say about the eigenvalues $\lambda$ and so on?
The problem is that I am not very familiar with ODEs (and Sturm-Liouville problems) so I thought that many people here might directly see what this ODE means?
If anything is unclear or not precisely specified, please do not hesitate to ask that
Answer: Normally, there are two conditions required for a Sturm-Liouville problem, such as
$$
   \Psi(-\pi)=\Psi(\pi),\;\;\; \Psi'(-\pi)=\Psi(\pi)\;\;(or\; \Psi'(-\pi)=-\Psi'(\pi)).
$$
Periodic problems are a little trickier than problems with separated conditions, but the problem here is a regular problem because the coefficient of the highest-order derivative does not vanish, and the other coefficients are regular on $[-\pi,\pi]$. Other periodic-type conditions are also possible, but the above are the most common.
If $\Psi_{1},\Psi_{2}$ are solutions with the same endpoint conditions, but with different parameters $\lambda_{1},\lambda_{2}$, then
$$
     \int_{-\pi}^{\pi}e^{\eta\sin\theta}\Psi_{1}(\theta)\Psi_{2}(\theta)\,d\theta = 0.
$$
The orthogonality condition follows from putting the differential equation into the symmetric form
$$
    \{e^{\eta\sin\theta}\Psi'(\theta)\}'+
     e^{\eta\sin\theta}\xi\cos^{2}\theta \Psi(\theta) =
     \lambda e^{\eta\sin\theta}\Psi(\theta).
$$
(A similar form holds if $\eta$ is not constant.) To see how the orthogonality condition arises, define the operator $L$ by
$$
           Lf = \{e^{\eta\sin\theta}f'(\theta)\}'.
$$
The corresponding adjoint equation is
$$
\begin{align}
  (Lf)g-f(Lg) & = \{e^{\eta\sin\theta}f'\}'g-f\{e^{\eta\sin\theta}g'\}' \\
     & = \{ e^{\eta\sin\theta}(f'g-fg')\}'.
\end{align}
$$
Therefore, if $\Psi_{j}$ are eigenfunctions with eigenvalues $\lambda_{j}$ which satisfy the same periodic-type conditions,
$$
\begin{align}
 (\lambda_{1}-\lambda_{2})\int_{-\pi}^{\pi}e^{\eta\sin\theta}\Psi_{1}\Psi_{2}d\theta
      &= \int_{-\pi}^{\pi} (L\Psi_{1})\Psi_{2}-\Psi_{1}(L\Psi_{2})\,d\theta \\
      &= e^{\eta\sin\theta}(f'g-fg')|_{-\pi}^{\pi} = 0.
\end{align}
$$
Assuming $\lambda_{1}\ne \lambda_{2}$, the stated orthogonality condition holds:
$$
            \int_{-\pi}^{\pi}e^{\eta\sin\theta}\Phi_{1}\Phi_{2}\,d\theta=0.
$$
Assuming $\eta, \xi$ are real, the possible eigenvalues $\lambda$ are real, and the eigenspace corresponding to eigenvalue $\lambda$ are either one- or two-dimensional.
Added: For your problem, there are unique classical solutions $f_{\lambda}$, $g_{\lambda}$ which satisfy
$$
      \begin{array}\\ f_{\lambda}(-\pi)=1 \\ f_{\lambda}'(-\pi)=0 \end{array}\;\;\;
      \begin{array}\\  g_{\lambda}(-\pi)=0 \\ g_{\lambda}'(-\pi)=1 \end{array}.
$$
The function $h=\cos\alpha f+\sin\alpha g$ is a solution of the equation, and will satisfy the conditions $h(-\pi)=h(\pi)$, $h'(-\pi)=h'(\pi)$ for some $\alpha \in [0,2\pi)$ iff the following determinant is $0$:
$$
       D(\lambda) = \left|\begin{array}\\ f_{\lambda}(\pi)-f_{\lambda}(-\pi) & g_{\lambda}(\pi)-g_{\lambda}(-\pi) \\
            f_{\lambda}'(\pi)-f_{\lambda}'(-\pi) & g_{\lambda}'(\pi)-g_{\lambda}'(-\pi)\end{array}\right|
$$
You can verify that $D(\lambda)=0$ iff there exists $\alpha\in[0,2\pi)$ such that
$$
            \left[\begin{array}\\ \cos\alpha \\ \sin\alpha\end{array}\right]
$$
is in the null space of the $2\times 2$ matrix whose determinant is $D$. Equivalently,
$h=\cos\alpha f_{\lambda}+\sin\alpha g_{\lambda}$ is periodic, along with $h'$. The order of the zero of $D$ should be the dimension of the eigenspace corresponding to the eigenvalue $\lambda$. The set of $\lambda$ for which you will have a solution at all for your equation should be a discrete set of real $\lambda$, and it is possible that $D'(\lambda)=0$, in which case the solution might not be unique because both $f_{\lambda}$ and $g_{\lambda}$ are already periodic, along with their first derivatives.
Comment: The classical equation $\frac{d^{2}}{dx^{2}}f+\lambda f=0$ has two-dimensional eigenspaces when you impose periodic conditions, except for $\lambda=0$ where you have a one-dimensional eigenspace. The dimension cannot be greater than 2 because you have a second-order equation. That already shows that the periodic case is not as simple as the case of separated endpoint conditions.
Comment: Most sources tend to omit discussions of the periodic cases, especially singular ones. Physicists, however, do study periodic equations because of crystalline structures. The existence of eigenvalues for regular cases can be deduced from spectral theory of compact operators.
Comment: I added more info for you.
Comment: @Lipschitz: I learned these things because of my interest in Physics, too. Unfortunately, I learned them over a long period of time, little bits at a time, and I don't know where to find a coherent treatment. I started with a very old classic: E. C. Titchmarsh's book on "Eigenfunction Expansions"; his methods are foundational and rely only on Calculus and some Complex Function Theory. He discusses existence of $f_{\lambda}$, $g_{\lambda}$ right away, and uses these to construct resolvent operators (these are compact integral operators for regular problems on finite intervals.)

Properties of this ODE


Passing data from view to controller in yii

Question: I have a text field in my view and I want to pass the value of textbox to the controller, but I don't know how to do it.
I have tried googling it but it only gives ides about passing data from conntroller to view in  yii, so please give an example of doing this with ajax.
Comment: use ajax for that or else do a form submit by specifying the action to the form.
Comment: This will help you http://api.jquery.com/jQuery.ajax/
Comment: what would be the `url` in this situation and how to get that data in `controller`?
Comment: url must be the url of your controller/action and you can get all the data in $_POST variable in the specified action
Comment: thnx deepu for $_POST
Answer: follow the steps:
in form:

<?php echo $form->labelEx($model,'order_id', array('class' => 'control-label col-lg-4')); ?>

    <?php echo $form->textField($model,'order_id',array('class' => 'form-control',
    'ajax' =>
        array('type'=>'POST',
            'url'=>$this->createUrl('recieveValue'), // write in controller this action
            'update'=>'#price',
            'data'=>array('order_id'=>'js:this.value'),
        )

    )); ?>

<?php echo $form->error($model,'order_id'); ?>

In controller:
public function actionRecieveValue(){
echo $_POST['order_id'];
}

At the top of same controller:
array('allow', // allow authenticated user to perform 'create' and 'update' actions
            'actions'=>array('create','update','recieveValue'),
            'users'=>array('@'),
),

Explanation:
Here text field id is order_id, controller action is recieveValuewhat I wrote in the ajax URL as 'url'=>$this->createUrl('recieveValue'),. Go to the controller and write action name as actionRecieveValue just add action before recieveValue. Now go to the top of the controller in the method accessRules and allow it recieveValue into array. Now check through firebug console. Type something into the text box and move the mouse from the textbox. You will find that your textbox value will be recieved into the controller.


Build an app stack that has no single point of failure and is fault tolerant

Question: Considering a three-tier app ( web server, app server and database).
[Apache Web server -> Tomcat app server -> database]
How to build an app stack (leaving out the database part) that has no single point of failure and is fault tolerant ?
Answer: IMHO, this is quite an open-ended question.  How specific is a single point of failure - single app server, single physical server, single data centre, network?
A starting point would be to run your Tomcat and Apache servers in clusters.  Alternatively, you can run separate instances, fronted with a load balancer such as HAProxy - except, to avoid a single point of failure, you will need redundancy on the load balancer as well.  I recently worked on a project where we had two instances of a load balancer, fronted with a virtual IP (VIP).  The load balancers communicated with two different app server instances, using a round-robin approach.  Clients connected to the VIP in order to use the application, and they were completely oblivious to the fact that there were multiple servers behind it.
As an additional comment, you may also want to look at space-based architecture - https://en.wikipedia.org/wiki/Space-based_architecture.


Spring Security request matcher is not working with regex

Question: Using Spring MVC + Security I have a business requirement that the users from SEC (Security team) has full access to the application and FRAUD (Anti-fraud team) has only access to the pages that URL not contains the words "block" or "update" with case insensitive.
Below, a test of the regular expressions used in spring-security.xml (I'm not a regex specialist, improvements are welcome =]):
import java.util.Arrays;
import java.util.List;

public class RegexTest {

    public static void main(String[] args) {

        List<String> pathSamples = Arrays.asList(
                "/index",
                "/index.*",
                "/index/",
                "/cellphone/block",
                "/cellphone/block.*",
                "/cellphone/block/",
                "/cellphone/confirmBlock",
                "/cellphone/confirmBlock.*",
                "/cellphone/confirmBlock/",
                "/user/update",
                "/user/update.*",
                "/user/update/",
                "/user/index",
                "/user/index.*",
                "/user/index/",
                "/search",
                "/search.*",
                "/search/",
                "/doSearch",
                "/doSearch.*",
                "/doSearch/");

        for (String pathSample : pathSamples) {
            System.out.println("Path sample: " + pathSample
                    + " - SEC: " + pathSample.matches("^.*$")
                    + " | FRAUD: " + pathSample.matches("^(?!.*(?i)(block|update)).*$"));
        }
    }
}

Bellow, the console result of Java class above:
Path sample: /index - SEC: true | FRAUD: true
Path sample: /index.* - SEC: true | FRAUD: true
Path sample: /index/ - SEC: true | FRAUD: true
Path sample: /cellphone/block - SEC: true | FRAUD: false
Path sample: /cellphone/block.* - SEC: true | FRAUD: false
Path sample: /cellphone/block/ - SEC: true | FRAUD: false
Path sample: /cellphone/confirmBlock - SEC: true | FRAUD: false
Path sample: /cellphone/confirmBlock.* - SEC: true | FRAUD: false
Path sample: /cellphone/confirmBlock/ - SEC: true | FRAUD: false
Path sample: /user/update - SEC: true | FRAUD: false
Path sample: /user/update.* - SEC: true | FRAUD: false
Path sample: /user/update/ - SEC: true | FRAUD: false
Path sample: /user/index - SEC: true | FRAUD: true
Path sample: /user/index.* - SEC: true | FRAUD: true
Path sample: /user/index/ - SEC: true | FRAUD: true
Path sample: /search - SEC: true | FRAUD: true
Path sample: /search.* - SEC: true | FRAUD: true
Path sample: /search/ - SEC: true | FRAUD: true
Path sample: /doSearch - SEC: true | FRAUD: true
Path sample: /doSearch.* - SEC: true | FRAUD: true
Path sample: /doSearch/ - SEC: true | FRAUD: true

Tests
Scenario 1
Bellow, the important part of spring-security.xml:
<security:http entry-point-ref="entryPoint" request-matcher="regex">

    <security:intercept-url pattern="^.*$" access="ROLE_SEC" />
    <security:intercept-url pattern="^(?!.*(?i)(block|update)).*$" access="ROLE_FRAUD" />

    <security:access-denied-handler error-page="/access-denied.html" />

    <security:form-login always-use-default-target="false"
        login-processing-url="/doLogin.html"
        authentication-failure-handler-ref="authFailHandler"
        authentication-success-handler-ref="authSuccessHandler" />
    <security:logout logout-url="/logout.html"
        success-handler-ref="logoutSuccessHandler" />
</security:http>

Behaviour:

FRAUD group **can't" access any page
SEC group works fine

Scenario 2
NOTE that I only changed the order of intercept-url in spring-security.xml bellow:
<security:http entry-point-ref="entryPoint" request-matcher="regex">

    <security:intercept-url pattern="^(?!.*(?i)(block|update)).*$" access="ROLE_FRAUD" />
    <security:intercept-url pattern="^.*$" access="ROLE_SEC" />

    <security:access-denied-handler error-page="/access-denied.html" />

    <security:form-login always-use-default-target="false"
        login-processing-url="/doLogin.html"
        authentication-failure-handler-ref="authFailHandler"
        authentication-success-handler-ref="authSuccessHandler" />
    <security:logout logout-url="/logout.html"
        success-handler-ref="logoutSuccessHandler" />
</security:http>

Behaviour:

SEC group **can't" access any page
FRAUD group works fine

Conclusion
I did something wrong or spring-security have a bug.
The problem already was solved in a very bad way, but I need to fix it quickly. Anyone knows some tricks to debug better it without open the frameworks code?
Cheers,
Felipe
Comment: Please clarify what isn't working. What is the URL in the debug log, and what do you expect it to match? Make sure you have tested your regular expressions separately. Also make sure the ordering is correct - that you have the most specific patterns listed first.
Comment: Luke Taylor: thanks by your attention, I improved the problems description.
Answer: The first configuration is wrong because the universal matcher is first so the second one will be ignored. The second configuration will exclude the SEC role from anything the first matches, so it sounds like you want something more like
<intercept-url pattern="^(?!.*(?i)(block|update)).*$" access="ROLE_FRAUD,ROLE_SEC" />
<intercept-url pattern="^.*$" access="ROLE_SEC" />

which has the SEC attribute in both patterns.
If that's still not what you require then please post the relevant debug log, showing Spring Security selecting the attributes for a particular request (this is logged in detail) and explain how it differs from what you expect.


Aggregating data to show latest result?

Question: I'm currently using the Elasticsearch Connector to get my data from Elasticsearch for displaying. I've managed to set up some basic pages and display the data successfully, but am now looking for a way to group it.
for example, if I have several results but they are all from the same server address, I would like to group them by server and show only the lastest input.
I've looked through options on views, and the aggregator module but can't seem to manage it.
Is this something that's possible, and I've missed?
Or is that not possible in drupal anyway?
Answer: In Views you can group by a particular field if the display allows fields (table, not teaser, etc). Under the Settings for Table for example you can choose the field to group the results:

The field has to be present in the list of Fields, but can be excluded from display if desired.
Answer: You can use aggregation query in elastic search to group data.
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html

Aggregating data to show latest result?


JavaScript html forms calculator not working

Question: im trying to make a simple perimeter calculator for a projector screen.
The code should take the response from a radio button input and the diagonal length to calculate the perimeter accordingly.
here is my code atm:

        <title>Calculator</title>

    <form name="Calculator">
        A
            4:3: <input name="A" type="radio" onChange="calc()" value="0" checked>
            16:9: <input name="A" type="radio" onChange="calc()" value="1">
            2.39:1: <input name="A" type="radio" onChange="calc()" value="2">
        B
            Screen Size: <input name="B" type="text" onChange="calc()" checked>
        Total
            <input type="text" name="Total" onChange="calc()" readonly size="10">

        <input type="submit" value="Submit Calculation"> <input type="reset" value="Clear">
    </form>

Comment: what exactly is it doing that makes it not work correctly?
Comment: I would suggest start by removeing the `` tags.
Answer: Your function getSelectedValue is wrong. It loops trough an array of elements to see which one is checked. You are trying to do the same for B which is only one element so that is not going trough the loop and thus returns ''.
So instead of
var B = Number(getSelectedValue(form.elements.B));

try
var B = Number(form.elements.B.value);

Also, your if else statements are wrong. It should be == instead of =:
if (A == 0) {
    L = 0.8;
    H = 0.6;
} else if (A == 1) {
    L = 0.872;
    H = 0.49;
} else {
    L = 0.922;
    H = 0.386;
}


k folds cross-validation: Is it necessary to randomize the training set before split 2?

Question: I guess I understand the basic idea of cross-validated, partitioning a training set into k folds, fitting a model and computing the score k consecutive times.

I am trying to figure out the details. Take iris dataset as our example

partition 150 instances into training set of 90 and test set of 60.
partition 90 instances into 5 folds,

What is the detailed procedure of the following code?
<code>>>> scores = cross_val_score(clf, X, y, cv=5)
>>> scores
array([0.96..., 1.  ..., 0.96..., 0.96..., 1.        ])
</code>
Does the detailed procedure run this way?
split 1: perform training on fold2 to fold5, perform validating on the remaining part, fold1 in this case.
split 2: perform training on fold1, fold3 to fold5, perform validating on the remaining part, fold2 in this case.
Are the fold1s in split 1 and split 2 the same fold? In other words, is it necessary to randomize the training set before split 2?
Answer: Yes you got the procedure correct. They are the same fold, it does not randomize the set again.
There is a variant of cross validation, where after you finish one round like the above, you randomize and split the data into a different 5 fold, this is called repeated cross validation


Decrypting the HTTP Live Streaming content

Question: I am trying to decrypt the AES 128 bit Video stream from Iphone captured on Wireshark. I have found the KEY files within the bin file mentioned in the m3u8 files on the wireshark traffic but i am not able to find how to decrypt that video data using these keys.
As per specs, IV will be the sequence number, AES will be 128 bit with CBC mode.
When I use the given key and IV to decrypt the TS captured using the OpenSSL, it fails and returns the Bad Decrypt error.
When i try to use a tool kpbe based on Bouncy Castle API, it returns the error as pad block corrupted. SO, i am completely stuck in this.
Do anyone knows about any library in c# which can help me in decoding this AES-128 bit encryption of data.
Or is there any way that i can validate that the data i have captured is correctly encrypted(which i think is correct).
Any sort of help/suggestion will be highly appreciated.
Comment: You are assuming rather much about the knowledge of this forum, please provide more information about the used protocols, and (e.g.) if you have tried any C# libs already... Without that, you might not get an answer.
Comment: OK, i have extracted the data form the HTTP Live streaming. Extracted the TS, the Key file sand got he seq. number form the m3u8.

ALready tried using OpenSSl using the AEs-CBC mode with VI as the sequence number and the Key extracted.

Also tried using the BouncyCastle based tool kpbe but the API return the error as pad block corrupted.
Answer: I have used the OPenSSL library and then finally able to decrypt the content.
Comment: Could you show more detail? A user asked about it (in an answer that was naturally deleted due to lack of rep). Thanks


Frailty Models for Death Rate

Question: I need to create a frailty-model (gompertz-gamma) for deathratesdue heterogeneity, having a dataset of people grouped by age and year of calendary with the following variables:
year, age , deaths, exposure
I have seen the R-Package "Parfm" , but the model requires a dependant variable:
Surv(time,event)
In the best scenario, is there a way to create this kind of model using GLM?
Is it possible to use the parfm model for deathrates? If possible, how can I re-write my dataset to make it ready for "Parfm" model?
If not possible, considering my low competences in R, is there another way to procede?
Answer: Not sure why this was migrated from SO, but here goes:
Surv(time, event) is used just as written, only time should be the name of the column in your dataset that stores survival times, and event should be an indicator column telling whether the individual died at that time, or was censored. See ?Surv for more details.
Also, I am not sure if your dataset is in long format now, i.e. one row per individual? You can use the example dataset veteran in R to see how it should look, and to try calling survival tests:
> head(veteran)
  trt celltype time status karno diagtime age prior
1   1 squamous   72      1    60        7  69     0
2   1 squamous  411      1    70        5  64    10
3   1 squamous  228      1    60        3  38     0
4   1 squamous  126      1    60        9  63    10
5   1 squamous  118      1    70       11  65    10
6   1 squamous   10      1    20        5  49     0

The formula for a survival model on this dataset could be something like Surv(time, status) ~ celltype + trt.

Frailty Models for Death Rate


Is it possible to remove classes from a field that uses the Rendered entity format?

Question: I have an entity reference field that is being handled by Display Suite. Even when setting the field template to expert, I can't completely strip away the default classes. The markup still has a div wrapper like so:

      Configure

Edit

Delete

Manage display

    Chicken Breast Tenderloins

Is there any way to remove those classes? Like maybe using a preprocess function or something?
Answer: Generally:
You could modify the display of the content type referenced by the Entity reference field. There you can use Display suite again for formatting the fields of the referenced content type and have it styled your way.
Especially, regarding this:

  Configure

Edit

Delete

Manage display

There is an option "Display Links" on the Rendered Entity. If you uncheck this, you can get rid of this code. This is done in the content type where  the Entity Reference field is included, not in the referenced conten type.
If you want to change this:

Go to the content type you are referencing and check the display. It seems you are using the 1 Col Display, so you can use ds-1col.tpl.php template. There you can change the classes.
Comment: Actually that part isn't the part I want to get rid of. It's this line:

    ``
Comment: Then look at what I have written at generally in first paragraph. Go to the content type you are referencing and format display there.
Comment: Ah, you're right. I changed the content type I was referencing to use the Reset layout. All is good now, thanks for this.