SearchView is inside overflow menu when using androidx libraries

Question: Hi I recently changed my android project to use androidx libraries due to some conflicts.
But now, the SearchView in my Toolbar is shown inside the overflow menu ( just shows 'Search' inside it, nothing happens when I click it )
which wasn't the case when using the old support libraries.
Click here for an example
I need the old iconified Search View back. Please help.
activity_main.xml
<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolBar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:logo="@drawable/logo"
    app:layout_scrollFlags="scroll|enterAlways|snap"/>

my menu.xml
<item
    android:id="@+id/action_search"
    android:icon="@drawable/search_icon"
    android:showAsAction="always|collapseActionView"
    android:actionViewClass="androidx.appcompat.widget.SearchView"
    android:title="@string/search"/>

and in onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main_menu,menu);

    SearchView searchView = (SearchView) menu.findItem( R.id.action_search).getActionView();

    return true;
}

If it helps, my activity is also a androidx AppCompatActivity
I went through androidx documentation in android developers website and also many stackoverflow questions but no luck. This issue is a first it seems..
Answer:
nothing happens when I click it

Nothing happens because you set it always|collapseActionView which means you should place the SearchView like this:
<item
        android:id="@+id/action_search"
        android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
        android:title="@string/srch"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="ifRoom|collapseActionView" />

Check my complete answer here: https://stackoverflow.com/a/34825224/4409113
Comment: This didn't work as I have stated, I am using the **androidx** support libraries and in it, adding searchview to toolbar places it inside a overflow menu although there is only one action in the toolbar. Thank you for replying.
Comment: Oh my bad, didn't notice the Androidx one. How about updated answer? Would you try and see what happens?
Comment: I changed **always** to **ifRoom** in the menu item and that didn't change a thing.
Comment: It does not opens or collapse when you click on it? Do you have to use `androidx` `actionViewClass`? Because i think it should work with the provided link.
Comment: Yes because I am also using other widgets from androidx. Do you think this could be a bug or something in the androidx library ?? Thanks for helping !
Comment: Well i have never seen or used this class: `androidx.appcompat.widget.SearchView` might be a new one but i think, in the link which i have provided in the question should help and will work with `android.support.v7.widget.SearchView`. If it didn't, might be a bug or sth and i'll give it a try.
Comment: I think you need to use `app:actionViewClass="androidx.appcompat.widget.SearchView"` instead of `android:actionViewClass...`


Sub query returning multiple results within an insert

Question: I have the below SQL which is trying to insert into the financial_history_details table using sub queries as I need the statement to pull data from other parts of the table.
This is ok if there is one line of information. My issue is that I am trying to insert thousands of lines of data, with certain bits of information (batch_number, contact_number) pulled from different tables.
I thought about using a subquery as seen in this statement, but as I've already said it doesn't work amazingly well as it only inserts one piece of data at a time.
INSERT INTO financial_history (batch_number, transaction_number, contact_number, transaction_date,
            transaction_type, amount, payment_method, posted, address_number, currency_amount)
VALUES ((select batch_number from event_bookings where batch_number not in (select batch_number from batches)), 1,
(select contact_number from event_bookings where batch_number not in (select batch_number from batches)),
'20-sep-2017', 'P', 0, 'CASH', '20-sep-2017',
(select address_number from event_bookings where batch_number not in (select batch_number from batches)), 0) ;

I have also tried using import from CSV but this is causing a number of problems which make it an impractical solution:
BULK
INSERT batches
FROM 'C:\batches.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO

Are there any other ways to do this?
Answer: You seem to want an insert . . . select:
insert into financial_history (batch_number, transaction_number, contact_number, transaction_date,
            transaction_type, amount, payment_method, posted, address_number, currency_amount)
    select . . .
    from event_bookings
    where batch_number not in (select batch_number from batches);

It is unclear what values you want for the columns, though.
Answer: You can use, using a single SELECT and avoid repeating multiple accesses to the same table (you can also use SELECT without insert to check output)
INSERT INTO financial_history (batch_number, transaction_number, contact_number, transaction_date,
            transaction_type, amount, payment_method, posted, address_number, currency_amount)
SELECT batch_number, 1, contact_number, '20-sep-2017', 'P', 0, 'CASH', '20-sep-2017', address_number from event_bookings
WHERE batch_number not in (select batch_number from batches);

Hints:

use CONVERT for dates and do not rely on implicit conversion (supposing your destination columns are datetime datatype: eg. CONVERT(datetime,'20/09/2017', 103) 106)   )

A more readable form of the same query:
INSERT INTO financial_history (batch_number, transaction_number, contact_number, transaction_date,
            transaction_type, amount, payment_method, posted, address_number, currency_amount)
SELECT batch_number
  , 1 AS TRANSACTION_NUMBER
  , contact_number
  , '20-sep-2017' AS TRANSACTION_DATE
  , 'P' AS TRANSACTION TYPE
  , 0 AS AMOUNT
  , 'CASH' AS PAYMENT_METHOD
  , '20-sep-2017' AS POSTED
  , address_number
  FROM event_bookings
  WHERE batch_number not in (select batch_number from batches);


how to run an onchange function when page is loaded if the selected box is already selected

Question: Im running into a problem where i have an ajax driven page that is drawn when a user selects something from a simple drop down:
<select id = "selectdepartment">
    <option id = "default">Select an option...</option>
    ....
</select>

and the remainder of the page is drawn using the jquery .change() :
$('#selectdepartment').change(function(){

});

Which then runs some ajax to php script. everything works great, the problem is when i submit a form that was drawn with ajax (using $_SERVER['PHP_SELF'];), the data gets submited, the page reloads, and the page is cleared but the select box is still left where it was. The user has to move to a different option then back to the one the selected originally to re-fire the .change(). that sucks.
I could fix this by passing a php variable in all of my forms, then checking to see the variable set on every page load and if it is draw the page parts then, but this would lead to pretty messy code and it's less than desirable.
There has to be a way to do this with the jquery library, though my knowledge of the javascript language in general is not what i would like it to be. If anyone has any helpful hints please share, dont do it for me though, i wont learn that way :)
edit: code with .trigger
$('#selectdepartment').change(function(){
var department = $('#selectdepartment').val();
var day = $('#data').data('day');
var month = $('#data').data('month');
var year = $('#data').data('year');
//alert (department);

if(department === "Select an option..."){
    $('.hiddenuntildepartmentisselected').css({"display":"none"});
}
else{
$('.hiddenuntildepartmentisselected').css({"display":"block"});
}

showpoints(department);
drawpointstable(department, day, month, year);
displaytheuseresforselecteddepartment(department, '');

$('#sendthedepartment').val(''+department+'');
$('#hiddendepartmentidforaddinganewpoint').val(''+department+'');

}).trigger('change');//end run functions

Answer: You can use the .trigger() function to immediately trigger the change event handler when the page has loaded:
$('#selectdepartment').change(function() {
    // code here
}).trigger('change');

Or if you need to call it elsewhere via JavaScript/jQuery:
$('#selectdepartment').trigger('change'); // or just .change() as a shorthand

Comment: @Nick Yes. It's not immediately visible to the user (so less intrusive), doesn't block script execution (there's no popup to confirm), displays objects in a useful format (though it's browser dependent). The only downside is that Internet Explorer will throw an error on `console.log` statements if the Developer Tools (F12) haven't been opened.
Comment: Did you put a console.log inside the change function to see if the trigger was getting called or not?
Comment: Im certain that this works, but now for some reason its not holding the spot it was on in the select. oh well, time to start tossing php around. thanks!
Comment: @Nick - you just type `console.log('your message')` in your code. It will then show up in the chrome developer tools console tab.
Comment: @EmmyS is it supperior to `alert()` in any way?
Comment: @Nick - it's a matter of preference for the most part, although the console does allow a whole lot of things that a basic alert doesn't. The Chrome console isn't quite as full-featured as Firebug. See the Firebug API (https://getfirebug.com/wiki/index.php/Console_API) for more info.
Answer: Updated
Your button for the form could make use of the onClick attribute, which would invoke a method to parse the form fields and post the data to your php script via .ajax().
In the success event method you then check any flags you need to and modify the element as you desire if needed.
Basic example:
Inside of .ajax():
...

url: 'xxx.xxx.xxx',
async: true,
type: 'POST',
dataType: 'html',
data: JSON.stringify( form_fields ),
beforeSend: function()
{
     // Pre send stuff, like starting a loading gif
},
success: function( data, textStatus, xhr )
{
    // Be sure you load the page with the content first
    $( '#containing-div' ).html( data );

    // Do  your check here, and modify your element if needed here
    if( flagtocheck === 'xxx' )
    {
        // Modify the element in question...
    }

    // I call a custom method termed `.ctrls()` here that makes any
    // adjustments to the DOM once its loaded/updated.
},
error: function( xhr, textStatus, errorThrown )
{
}

Of course, you'll want to set flagtocheck appropriately in your case.
Hope that helps!
Note regarding edit
This post was edited to be a little more descriptive and more easily understood. Since the person asking the question is already using the .ajax() method, the success event method is the ideal place for doing what the person asking the question is requesting.  It is 1 less method invocation to directly modify the element there than using it to call .trigger() or .change() which then also directly modifies the element.
Comment: If they use AJAX to submit the form then there shouldn't be any need to trigger the `change` event anyway, since all of the page sections that are drawn by that would still be there. It's not a bad suggestion, but may be a non-trivial amount of work for them to do.
Comment: @anthony-grist I never suggested triggering the `CHANGE` event while already using `AJAX` to submit the form.  I suggested using `ONSUCCESS` event of the `AJAX` method to directly set the element as if .change() had been invoked.  The only difference in my answer is I did not provide another alternative for them or give them an in-depth explanation.  I don't agree with using extra events when not necessary because it is another method invocation and is less efficient and no easier to read.


WPF - Metro Dialog Settings (mahapps) on the top of the screen?

Question: I have a WPF application, and I use a metro dialog to show messages. Can I show the message on the top of the window? Can I modify the location of the popup?
Comment: Are you using any UI framework like MahApps to get the "Metro" theme?  Dialogs are usually pretty straightforward to use.
Comment: Yes, I use the MahApps Metro for the "Metro" theme.
Answer: I found a way:
try
{
     var mySettings = new MetroDialogSettings
     {
         AffirmativeButtonText = "Yes"
     };

     var cd = new CustomDialog
     {
         VerticalAlignment = VerticalAlignment.Top,
         VerticalContentAlignment = VerticalAlignment.Top
     };

     await DialogManager.ShowMetroDialogAsync(this, cd, mySettings);
}
catch (Exception ex)
{
}


How much of a season must be played for it to count under an NBA pension?

Question: To qualify for an NBA retirement pension you need to have played at least three seasons in the league.
What is the last date a player can join a team during the season, and still get credit for that season with respect to pension qualification?
Answer:
How much of a season must be played for it to count under an NBA pension?

If you have signed a contract for a team for at least one day, you will be credited with a year of NBA service.

What is the last date a player can join a team during the season, and still get credit for that season with respect to pension qualification?

February 2nd, but this explicitly applies to 10-Day and Two-Way contracts.

Years of Service
The 2017 NBA CBA defines "Years of Service":

"Years of Service" means the number of years of NBA service credited to a player in accordance with the following:

A player will be credited with one (1) year of NBA service for each year that he is on an NBA Active List or Inactive List for one (1) or more days during the Regular Season.
Notwithstanding the above, a player will not receive credit for a Year of Service for any year in which he:

(i) withholds playing services called for by a Player Contract or this Agreement for more than thirty (30) days after the Season begins, or
(ii) is a Restricted Free Agent, has been tendered a Qualifying Offer by his Prior Team and the Prior Team has extended the date by which the player may accept the Qualifying Offer until March 1 in accordance with the Article XI, Section 4(c)(i), and has not signed a Player Contract with any Team by March 1.

In addition, notwithstanding the above, a player will not receive credit for a Year of Service for being on an NBA Active List or Inactive List as a result of signing a Player Contract that is disapproved by the Commissioner.
In no event can a player be credited with more than one (1) Year of Service with respect to any one NBA Season.
A Year of Service will be credited to a player on the June 30 following the Season with respect to which it is being credited.
Under no circumstances shall the definition of Years of Service herein be used for purposes of determining a player's years of credited eligibility, benefit, and/or vesting service under any benefit plan or program provided for under Article IV of this Agreement, including, without limitation, the Pension Plan, 401(k) Plan, Health and Welfare Benefit Plan (including the Retiree Medical Plan, HRA Benefit, and tuition reimbursement program), or Post-Career Income Plan.
Players shall be credited with Years of Service pursuant to this Section 1(iiii) only in respect of Seasons covered by this Agreement. Years of Service credit for Seasons prior to the 2005 NBA/NBPA Collective Bargaining Agreement shall be determined in accordance with the provisions of the 1999 NBA/NBPA Collective Bargaining Agreement.

Consideration Toward Being On a Roster For a Regular Season
According to Article 4.1 a iv, The NBA CBA defines a cutoff date for consideration toward being on a roster for a regular season, but explicitly states this applies to 10-Day or Two-Way contracts:

The Pension Plan shall be amended to provide that a player shall not be considered to be on the Roster for a Regular Season solely because he was under a 10-Day Contract or Two-Way Contract as of February 2nd of such Regular Season.

Uniform Player Contract
Contracts are further defined here.


How to calculate the mean and variance of a cumulative probability graph?

Question: I'm given a cumulative probability graph, however i can't post any images. Thus, the X and Y coordinates of the graph are:
X:
2.5
7.5
12.5
17.5
22.5
27.5
32.5
37.5
42.5
47.5
52.5
57.5
Y:
0
0
0
0.07
0.17
0.27
0.37
0.47
0.57
0.67
0.77
0.87
How do i calculate the mean and variance of this cumulative probability graph?
Thanks alot!!!!
EDIT:
Now that i have enough rep points, i can post the picture of the graph.

Given D = 0.07
Comment: Hint: For your problem, where $X$ takes on only positive values, $E[X]$ equals the area between the curve $F(x)$ and the line $y = 1$ for nonnegative $x$ from $0$ to $\infty$.  Thus, area of rectangular region with opposing corners at $(0,0)$ and $(2.5,1)$ plus area of rectangular region with opposing corners $(2.5,0)$ and $(7.5, 1)$ plus...   Note that what the area is between $x=7.5$ and $x=12.5$ depends on the graph of $F(x)$ which may increase linearly or as some smooth rise from $(7.5,0)$ to $(12.5,0.07)$, or in a sudden jump at $x=12.5$, etc.
Comment: As well as Dilip's points, you need to know about the right tail of the distribution (or be able to estimate it), i.e. when the cumulative probability reaches $1$
Answer: You need to interpolate between those values and extrapolate beyond them.
One possibility is that what you actually have is a uniform distribution on the interval $[14,64]$ in which case the mean would be $\dfrac{14+64}{2}=39$ and variance $\dfrac{(64-14)^2}{12}\approx 208.3333$.
In most cases life will not be as simple as this, and you will need numerical methods. You have incomplete data, so suppose your $X_i$ finish with 62.5 67.5 and your $Y_i$ finish with 0.97 1. Then you can estimate, by putting the probability at the centre of each interval, the first moment with $$\sum_i (Y_i-Y_{i-1})\left(\frac{X_{i-1}+X_i}{2}\right)$$ which in this case would be $39$, and estimate the second moment with  $$\sum_i (Y_i-Y_{i-1})\left(\frac{X_{i-1}+X_i}{2}\right)^2$$ which in this case would be $1732.5$, giving an estimate of the variance of $1732.5-39^2=211.5.$
Comment: Thanks alot Henry. I have posted the graph, please take a look :)
Comment: @ben: My first calculation (a uniform distribution) would have a straight diagonal line, while my second would look like your graph, but with additional vertical steps at 60 and 65 as part of the extrapolation, both otherwise is the same as your graph.


Prove The Divisibility rule of 19

Question: I'm a beginner in number theory, I was trying to prove the divisibility rule of 19, can someone help me to complete my proof
Let $n$ be $10a + b$ where b is the unit digit and 10a is the rest; 10 is coprime to 19,
$n \equiv 0 \pmod{19} \iff a+2b \equiv 0 \pmod {19} \iff x\cdot a + x\cdot 2b \equiv 0 \pmod{19} \iff 10a + b \equiv 0 \pmod{19}$
Where it holds that $10a + b - (x\cdot a + x\cdot 2b) = 19k \space or \\ 10a + b - (x\cdot a + x\cdot 2b) \equiv 0 \pmod{19}$
but I have a problem finding the $x$ multiplier.
I've visited A divisibility rule for 19, Didn't quite understand what has been done there; and the important factor is that I want the approach I'm taking completed.
If my proof is completely meaningless etc, I'd appreciate a proof like it.
Thanks
Comment: Are you sure you want $xa + xb$? Typically they go for $a + xb$, so you're adding x times the last digit to the rest of the number.
Comment: Welcome to Mathematics Stack Exchange.  Try $x=10$.  Cf. [this question](https://math.stackexchange.com/questions/1898380/a-divisibility-rule-for-19).  And it's confusing to let $a$ be $10a+b$
Comment: Maybe, I tried to take the approach used for 17, and 13, I'm like really beginner
Comment: Sure, can you write up the approach used for 17? That will help us tell if you make an error trying to generalize the approach.
Comment: @CalvinLin [Proof the divisibility rule of 17](https://math.stackexchange.com/questions/174175/proof-of-the-divisibility-rule-of-17)
Comment: (Apart from your $a$ doing double duty like Tanner pointed out) You see what I mean by "Typically they go for $a  + xb$?" In the link, they say that $17 \mid a - 5 b \Leftrightarrow 17 \mid 10a + b$.
Comment: I edited the question so it isn't 2 a's in use
You see the approach taken in the 17(as I understood) is that they're subtracting $x$ from $17k$ to get to $17j$ resulting in x also a multiply of 17
I tried to take the same approach
But I don't know what Should be $x$ resulting in a $19j$
Comment: As $2$ is the multiplicative inverse of $10$ modulo $19$, the result follows from [this more general result](https://math.stackexchange.com/q/1961065/11619). Not voting to close this as a duplicate yet, because A) I happened to answer that query and B) it is possible that's not the first incarnation on our site.
Comment: By the [general divisibility test](https://math.stackexchange.com/a/2989299/242) in the linked dupe:
$$ \color{#0a0}{19}\mid \color{#c00}{10}\,a+b \!\iff\! 19\mid a+\color{#c00}c\,b,\ \ {\rm for}\ \ \,  \color{#c00}{c\equiv  \dfrac{1}{10}}\equiv\dfrac{2}{20}\equiv\color{#c00}{\dfrac{2}{1}}\!\!\!\pmod{\!\color{#0a0}{19}}\qquad$$ See the detailed proofs there (which are no simpler in this special case). $\ \ $
Answer: Thanks to user J. W. Tanner. The answer for $x$ is 10, Giving us this
$a+2b \equiv 0 \pmod {19} \iff 10a + 20b \equiv 0 \pmod{19} \iff 10a + b \equiv 0 \pmod{19} \iff n \equiv 0 \pmod{19}$
This is Because:
$10a + b - (10a + 20b) = -19b \equiv 0 \pmod{19} \iff x \equiv 0 \pmod{19}$
Comment: I think that after the second double implication, the expression should be $10a+20b$ (rather than $20a+20b$).
Comment: I think you don't want to begin your chain of implications with $n \equiv 0 \pmod{19}$ since that is what you are trying to prove. It's where the chain ends.
Comment: Oh, yes thanks i prob made a mistake while typing it
Comment: I think you meant $10a+b-(10a+20b)=\color{red}-19b$, but it's still $\equiv0\pmod{19}$; and the choice $x=10$ was rather obvious when you wrote $a+2b\dots\iff 10a\dots$ in the question
Comment: Please strive not to post more (dupe) answers to dupes of FAQs. This is enforced site policy, see [here](https://math.meta.stackexchange.com/q/33508/242).

Prove The Divisibility rule of 19


How many tones are in a human voice at the same time?

Question: When I play a note on my synthesizer or my guitar, it is exactly one frequency plus the overtones and subfrequencies which are doubles and halves of the tone.
But when I listen to a vocalist, it sounds like the vocals are not just "one frequency" but that the voice has many different frequencies at the same time.
Is this something that can be confirmed or am I just hearing it wrong?
Comment: tbh, your initial premise is incorrect; not just doubles & halves, a myriad other frequencies, some only tenuously related. That's how we can tell a violin from a cello, if both are playing the same note.
Answer: Every instrument has its unique voice - except, perhaps, synthesiser, which has the propensity to produce many different voices.
The main difference between each is the number of overtones or harmonics produced by that instrument, along with the fundamental ADSR envelope, which is often variable. Listen to a trumpet and a violin play the same pitch, and the difference is recognisable immediately.
Vocals have a unique set of overtones, but also the ability to make different vowel, and to a degree, consonant sounds. Also, quite capable of reproducing some instrumental sounds too. So, where does that leave us?
Answer to header question, many, variable.
Comment: Human voice is perhaps the only one that can change its resonances on the fly, without even really thinking about it.
Comment: @Tetsujin - that was one of the points in the answer, and not many instruments can do that. Or can they? I feel a question coming on...
Comment: Well... strings you can move the excite point... but there's something about the voice that somehow requires less conscious involvement - the floor is open for the further question, for sure :)
Answer: You're working from a false premise.  There's inharmonicity in every real instrument's sound.   Partly in the source, partly in the playback environment.  Let me know when you design a system that will really deliver a mixture of harmonic pure sine waves to the ear!
https://en.wikipedia.org/wiki/Inharmonicity
But maybe less in an instrument like electric guitar than in a human voice.  Which is why guitarists typically add effects and choose deliberately distorting amps and speakers, where a vocalist often prefers 'clean' amplification.
Answer:
When I play a note on my synthesizer or my guitar, it is exactly one frequency plus the overtones and subfrequencies which are doubles and halves of the tone.

As per Tetsujin's comment, that's not always the case; there will often be inharmonic partials, variations in the frequency of individual partials, and regions of energy that perhaps don't have a distinct frequency. But you're right it is also often possible to create sounds that stay close to perfectly periodic (and therefore have only harmonic overtones) at least for a short time.

when I listen to a vocalist, it sounds like the vocals are not just "one frequency" but that the voice has many different frequencies at the same time.

The human voice also tends to be an unstable sound - the singer may be deliberately varying the air pressure, pitch, resonances of the vocal tract and mouth (which will emphasize different frequencies within the note) - or these may be varying involuntarily. As well as the basic frequency of the sung note, there may be a periodic vibrato taking place at a different (much lower, non-'audio') frequency, and if the vocalist is making a growling sound, you may hear the aryepiglottic folds vibrating at yet another frequency. Additionally, there is a lot of turbulence and 'noise' in a human vocal sound.
However, though the human voice is complex, there isn't any essential characteristic of it that can't be found in other instruments or sounds; I don't think you're hearing the human voice wrong, but perhaps you haven't noticed the subtle complexities in other sounds yet.


Age and residual life time of the Poisson process

Question: Original Question
Let $N(t)$ be a Poisson process with intensity $\lambda$. Let $T_1<T_2<...$ be the occurrence times. Let $T_0=0$. For any $t>0$, define the $age$ random variable to be
$A_t := t-T_{N(t)} $
the $residual$ $life$ $time$ random variable to be
$R_t:=T_{N(t)+1}-t$
In other words, $age$ is the time elapsed since the last occurrence and $residual$ $life$ $time$ is the time remaining until the next occurrence.
Using a similar logic to the first answer of Relationship between poisson and exponential distribution, one can show that $A_t\sim$ Exp($\lambda$). Below is the reproduction of the logic:
$A_t>a \Leftrightarrow T_{N(t)}<T_{N(t)}+a<t \Leftrightarrow N(T_{N(t)})=N(T_{N(t)}+a)=N(t)$
I understand that the second $\Leftarrow$ logic requires the last equation to ALWAYS hold.
Continue on with user28's proof
$P(A_t>a)=P[N(T_{N(t)}+a)-N(T_{N(t)})=0]=P[N(a)=0]=e^{-\lambda a}$
However if I admit that $A_t\sim$ Exp($\lambda$), I will quickly run into the following paradox:
Since the exponential distribution is memoryless, one can easily prove that $R_t\sim$ Exp($\lambda$) and is independent of $A_t$. This means that two independent Exp($\lambda$) exponential random variables $A_t$ and $R_t$ sum to a different Exp($\lambda$) exponential random variable -- the interarrival time of the Poisson process.
According to Wikipedia https://en.wikipedia.org/wiki/Exponential_distribution, the sum, however, should be Gamma(2,$\lambda$) (in the ($\alpha,\beta$) parameterization).
What did I do wrong here?
Was I too ambitious claiming $T_{N(t)}<T_{N(t)}+a<t \Leftrightarrow N(T_{N(t)})=N(T_{N(t)}+a)=N(t)$
?
If the $\Leftarrow$ can not be claimed, then I can only get $P(A_t>a)>e^{-\lambda a}$. How should I get the distribution for $A_t$ in this case?

First Edit
I take the Poisson process as a limiting process of the Bernoulli process and arrive at the same conclusion $A_t\sim$ Exp($\lambda$). The key steps are as follows:
Step 1: Review the Law of Small Numbers for Bernoulli processes

Step 2: Let $t\in\mathbb{N}$ for the Bernoulli process $\{X_r^m\}_{r\in\mathbb{N}/m}$. Then by time $t$, there are a total of $mt$ trials, and $N^m(t)$ of them are successes. For $N^m(t)>0$, the time that the last success takes place, $T_{N^m(t)}$, takes any value between $\frac{1}{m},\frac{2}{m},...,t$. Let $k\in\mathbb{N}$ such that $0<k<mt$
$P(t-T_{N^m(t)}=\frac{k}{m})$
$=P($ the last $k+1$ trials of the total $mt$ trials are one success and $k$ failures $)$
$=p_m(1-p_m)^k$
When $k=mt$, $P(t-T_{N^m(t)}=\frac{k}{m})=$$P($ all $mt$ trials are failures $)=(1-p_m)^{mt}$
Therefore
\begin{split}
P \Big(t-T_{N^m(t)}>\frac{k}{m} \Big) &= \sum_{i=k+1}^{mt-1}P\Big(t-T_{N^m(t)}=\frac{i}{m}\Big) + P\Big(t-T_{N^m(t)}=\frac{mt}{m}\Big) \\
& = p_m(1-p_m)^{k+1} + ... + p_m(1-p_m)^{mt-1} + (1-p_m)^{mt} \\
& = (1-p_m)^{k+1}
\end{split}
Taking $x=\frac{k}{m}$ and the limit $\lim_{m\to\infty} mp_m = \lambda$ yields
\begin{split}
P (t-T_{N^m(t)}>x) = \Big(1-\frac{\lambda}{m}\Big)^{mx+1} \to e^{-\lambda x}
\end{split}
This again leads to the conclusion that $A_t\sim$ Exp($\lambda$) and the same paradox in the original question.
Answer: Yes you are correct in suspecting that here the if and only if condition does not hold i.e.,
$A_t>a  \Rightarrow N(T_{N(t)})=N(T_{N(t)}+a)=N(t)$ holds but not the other way around.
This can be seen as follows. Take $a$ such that $t<T_{N(t)}+a<T_{N(t)+1}.$ Then we still have the event $N(T_{N(t)})=N(T_{N(t)}+a)=N(t),$ but now $A_t<a.$

Edit 1:
An attempt:
Let $X_t = T_{N(t)+1}-T_{N(t)}.$ We know $X_t \sim Exp(\lambda).$ Also from the memoryless property of exponential distribution $R_t \sim Exp(\lambda).$ However we know
$$
A_t = X_t - R_t
$$
Using chain rule of probability.
\begin{eqnarray*}
P(A_t>a)&=& P(X_t-R_t>a)=\int_a^\infty P(R_t<x_t-a|X_t=x_t) dP(X_t = x_t)\\
&=& \int_a^\infty P(R_t<x_t-a|X_t=x_t) \lambda e^{-\lambda x_t} dx_t
\end{eqnarray*}
Till this step we do not require any dependence condition. Now we need to figure out the distribution $P(R_t<x_t-a|X_t=x_t)$ since $X_t,R_t$ are not independent.

Edit 2: I think I understand the basic problem, but still need to think a bit more. The following statement is true, "Consider a poisson process on a line, then randomly choose a point in the line. Then the distribution of the time to both the next and previous arrival point will be $Exp(\lambda)$, i.e. the process looks poisson in both direction." This is called the "Hitchhiker's paradox". But actually this is not a paradox, and can be proven, because we are inherently conditioning on the fact that we randomly arrive at a point $T_{N(t)}<t<T_{N(t)+1},$ and the probability of arriving at a larger interval is more than arriving at a smaller interval. So we observe a distance biased distribution. Its slightly non-intuitive. I suspect a similar thing happening here.
Comment: Thank you A. Ray. In order to use convolution though, Xt and Rt must be independent. In this case, cov(Xt, Rt) = cov(Rt+At, Rt)=var(Rt)>0. Therefore, Xt and Rt are dependent, and I can not use convolution.
Comment: @YeTian  Yes you are correct, X_t and R_t are not independent. My mistake. Let me go back and check my queuing theory notes as I try to figure out the answer :)
Comment: Thank you! I don't disagree with your assessment in the first part. However, is it possible that the probability of the other A_t<a case is zero? I now have another method pointing to the same conclusion.
Comment: No it cannot be zero. It will of course depend on the value of $a.$ $P(A_t<a)=0$ for all $a$ is a very strong statement. Just consider $a \to \infty,$ then $P(A_t<a)=0$ would imply the arrivals never occur !
Comment: Please check my Edit 2.
Comment: Yes. I can see the "arrival never occur" case and how that's a problem. I am reading about the "Hitchhiker's paradox". It seems that this is exactly what I am asking?
Comment: The problem with no arrival is that we know expected inter-arrival time $E(T_{N(t)+1}-T_{N(t)})=\lambda$ is finite. Therefore $P(A_t<\infty)=0$ cannot happen, since that would have implied that the expected arrival time is infinity.
Comment: Only difference I see with Hitchhiker's paradox is that it considers observing a random time point $t.$ I am not sure if that is the same as in your case, since it seems you wanna find $A_t$ distribution for any $t$ without treating $t$ itself as a random variable. Hence the confusion.
Comment: Correct. In my case t is not a random variable. In fact I have a very difficult time persuading myself that At depends on t. Why should it?
Comment: Yes I agree, it shouldn't depend on t.


toLocaleString R$ brasileiro

Question: Estou com uma dúvida simples sobre o toLocaleString, eu não conhecia esse prototype e fui testa-lo ao invés de fazer o bom e velho split e replace

var a = 10000.50
var b = a.toLocaleString('pt-BR')
console.log(b)

A saída deste código deveria ser 10.000,50, e não 10.000,5. Como faz ele não ignorar os centavos? Porque isso são 50 centavos, e não 5.
Obrigado!
Comment: Ele não sabe que você quer formatação de moeda.
Answer: Isso é configurável com o minimumFractionDigits e maximumFractionDigits. Se quiseres ter o numero de casa decimais fixo então dá o mesmo número aos dois. Esses valores podem ir de 0 a 20.

var a = 10000.50
var b = a.toLocaleString('pt-BR', {
  minimumFractionDigits: 3,
  maximumFractionDigits: 3
})
console.log(b); // 10.000,500
Comment: Perfeito Sergio! Sensacional, cara! Obrigado =)
Answer: Com a introdução da API Intl, o toLocaleString pode deixar de ser usado, em favor dessa nova interface, bem mais completa.
Um exemplo:

const number = 1234567.89;

const formatter = new Intl.NumberFormat('pt-BR', {
  style: 'currency',
  currency: 'BRL'
});

const formatted = formatter.format(number);

console.log(formatted); // R$ 1.234.567,89

Para saber mais, não deixe de consultar a documentação.
Answer:

var a = 10000.50
var b = a.toLocaleString('pt-br')
console.log(`${b}0`)
Comment: É falsa a afirmação de que o código produz a saída `10.000,50`. Isso pode ser facilmente verificado.
Comment: Oi, Augusto, eu sou nova nesse meio, e peço desculpas caso a explicação que eu tenha dado esteja errada, mas eu tentei no console.log e me veio o valor 10.000,50, assim como previsto.
Comment: resultado: https://imgur.com/a/HlSw9Jg

toLocaleString R$ brasileiro


Is multiple logistic regression the right choice or should I use univariate logistic regression?

Question: I have a set of data (~ 90 cases) and an outcome of a diagnostic test. I have collected factors that were determined before the test that could predict the outcome of the test. Now some of the data are binary, some are continuous (lab tests), one is categorical (the original diagnosis leading to the symptom with 5 categories).
The statistician did (in SPSS) a multiple logistic regression on most of the parameters and then a backwards stepwise selection, which produced a model, one of the factors was strongly significant (<0.001), the others insignificant (two of them in the 0.01-0.05 range). The question from my colleagues was natural - why was this or that possible factor not included, and "these two factors are actually significant, or can we say nearing significance". I must say I don't like to interpret "nearing significance", but I have to work with my colleagues. I asked the statistician to perform another analysis, this time with the full set of factors, and the result is different, only one of the factors (the most significant one) stayed in the new model, is still strongly significant but there are other factors, also in the 0.01-0.05 range, which will bring me to the "nearing significance" problem again. I also tried with R and Rcmdr with the same data, and stepwise selection based on AIC or BIC produces different results (only the one factor remains the same).
Now I see that given that various stepwise selection methods produce different models with insignificant factors, the presence of these factors in the model is just random. That's why I would like not to include them. Most of all, I don't want to interpret their presence in the model because the colleagues think that I should as they are "nearing significance".
Question 1: To make things simpler, is it possible to run a "battery" of univariate logistic regressions on all the variables? This would solve both my problems - I would have one simple significant model with the most significant variable and wouldn't have to deal with "nearing significance" interpretation in the others.
Question 2: Is there any correction of the significance to be used in multiple logistic regression as it is with multiple comparisons, so I can deal with the "near significance" argumentation?
Answer: It is obviously not correct to rerun models until by chance they give what you expect...
Ideally, the model structure (i.e. selection of predictors, transformations, interactions) is chosen based on a number of points before computing the model:

Expert knowledge such as publications and research questions. This includes thinking.
Example: if you are mainly interested in the effect of a particular
variable $X$ on the response $Y$ and you know that $X$ has a strong
causal effect on $Z$, then it would be quite stupid to include $Z$ in
the model along with $X$, because this would partially hide the
effect of $X$ onto $Y$. That is maybe the case in your model.
Univariate distributions of variables (e.g. excluding potential predictor "sex" if there is only one male or if most values are missing; log-transform some right-skewed
variables with outliers if it makes scientific sense etc.)
Bivariate distributions of the predictors (e.g. if both potential predictors "age" and "experience" are highly correlated, it might suffice to include just one of
the two)

The hidden message of the above points: Don't take into consideration the association between the response variable and the potential predictors at this point. This will tend to bias the model to fit your expectations. It also answers Question 1: Such univariate screening is not suitable for variable selection. It might be part of the analysis though as complement to the multivariate model. This depends on the research question.
The answer to Question 2 depend on what the research question or the objective of the analysis is:

You could, for instance, be interested in testing some specific hypotheses. Then the "borderline significance" and multiple testing problem becomes an issue.
(And) or you might want to have a good predictive model. Then cross-validation (or similar) of the performance of the model is of much higher relevance than p values.
(And) or you might be interested in estimating the effects of some particular predictor.
Comment: Thank you for the answer. In my Question 1 I was not looking for a way to select the variables, I was rather asking if it is appropriate not to do multiple regression, but instead, to perform a "battery" of univariate regressions - and use this as a result.
As to your comment on Question 2 - i know that cross-validation of the performance of the model is of higher relevance than p-values, but you would have to explain this to a lot of people. In medical science, p-value is all that matters because noone understands the rest.
Comment: Your post touches so many aspects that it is difficult to provide a good answer. Wether your univariate regressions are useful really depends on the aim of the analysis.
Comment: In this type of research, the question is always the same. Can we predict the result of the test / procedure by using variables known before we perform the test / procedure? I know its wrong to do it this way. But the papers are all the same. You collect a bunch of variables, do multiple regression and see what becomes "significant", then you publish. That is also the reason why there are so many conflicting results.
Answer: To deal more specifically with the "'battery' of univariate logistic regressions on all the variables" than @Michael's excellent answer: If the purpose of fitting a model is to make predictions, then it's unclear why you're so concerned with "significance"† - (validated) model performance is what's important, & this will rarely be improved by selecting a model based on a single predictor & ignoring all the others. If the purpose is to estimate individual coefficients for each predictor (perhaps for tentative identification of causes) then you want to control for the effects of other predictors, which you don't by looking at each in its own separate model.
If the full model's too big for the data, then data reduction on the predictors prior to fitting & shrinkage methods such as ridge regression can improve its predictive power more reliably than stepwise. Read some good books on how to build predictive models:
Harrell (2001), Regression Modelling Strategies
Steyerberg (2009), Clinical Prediction Models
Steyerberg et al. (2000), "Prognostic modelling with logistic regression analysis: a comparison of selection and estimation methods in small data sets", Statistics in Medicine, 19, pp 1059-79 is especially relevant to the situation you describe.
† And there's no sense at all in performing hypothesis tests after stepwise procedures.
Comment: Thank you for the helpful answer. Just to make clear, why I am so concerned with "significance": this is **medical** research. The reviewer will ask for the significance and that is the only thing that will matter to him / her. The same applies to my colleagues / the boss who has to allow me to try to publish the paper. I have even reviewed a paper with p values in description of measurements in 3 patients. The reason was that the paper got rejected in another journal, one of the reasons was that the "statistics is not well done" and "p values are missing". Read 10.1371/journal.pmed.0020124.


Some major keys are brighter than others?

Question: In this video, in 3:00

The author suggests some scales in the circle of fifth are brighter than others. Despite having the same intervals.
In the demonstration there, it does sounds brighter.
So, In what sense is it true for scales? Why did the played chord progression become increasingly brighter?
Comment: Didn't get as far in the video as some keys are brighter. Tirned off at 'bigger intervals sound brighter'. So a major triad has a M3 +m3, but a minor triad has m3+M3. Logic seems lacking! Spurious stuff on the 'net again. Dv'd due to poor premises.
Comment: Having watched part of the video I am not sure it's well done.  In his 2 example the chords may be similar but his voicing is different and this could be the cause of the perceived change in brightness.  Also, in standard 12TET tuning the circle comes around on itself eventually so I'm not sure how the trend can be universal.
Comment: @Tim Neely's point is regarding all chord intervals as based from the root, so a major triad has a major third and a perfect fifth, whereas a minor triad has a minor third and perfect fifth, clearly making a major chord contain bigger intervals in that regard.
Comment: There are statements to this effect in old music theory texts.  G major is generally majestic while C invokes child-like care free happiness.  I am not sure how valid these statements are.
Comment: I wonder whether this point of key-character has not been already discussed here. Maybe a duplicate?
Comment: @user45266 - I missed that bit. Maybe 'cos he didn't state it.
Comment: There are questions like https://music.stackexchange.com/questions/3486/does-it-really-make-difference-to-play-a-song-in-a-different-key, which discuss whether the same tonalities based on different roots sound different. But I don't think this video is based on that idea - at least not the first few minutes that this question seems to be asked about.
Comment: @Tim Fully agree about some of the "hey look, I can edit all the breaths out" videos out there, but isn't it a good thing that OP is here inviting an evaluation of the video, rather than blindly accepting it?
Comment: @user45266 if you are going to regard the important intervals as being the ones from the root, you have to support that with logic as to why one of the notes in a chord would particularly be heard *as* the root.
Answer: This is a poor video, in my opinion. It seems to be oriented around some concept of 'brightness' that doesn't seem well-defined by the presenter, and doesn't quite seem to correspond to any other commonly agreed-on definition of 'brightness'.
He gets off to a bad start by claiming that a major chord sounds brighter than a minor one because it has a major third in it. Well, guess what - a minor chord also has an interval of a major third in it too!
He also attempts to claim that a consonant sound comes from it having nether too much nor too little 'brightness' - totally ignoring all the other more standard reasons that things are heard as consonant.
The idea that different modes of the major scale have different levels of brightness does perhaps have some coherence, but that doesn't support the idea that going around the major keys arranged in the circle of fifths makes things "brighter" or "darker".
There is also some logic to the idea that playing chord progressions that go round the circle of fifths can make a piece sound like it's going 'down' or 'up', but the way it works is not because you're genuinely going from a 'brighter' place to a 'darker' place (or vice-versa), but because the successive chord motions are interpreted by the ear as going downward or upward. It's rather like this famous illusion:

Over a small range of movement, going round the circle of fifths 'sounds like' you're going down (or up). But ultimately, you get back to where you came from (hence... 'circle').
Comment: What a super video. He'll never disappear up his own axis.
Comment: @user2679290 "brightness relations of church modes based on the same key are widely accepted" - I can find some links talking about the relative brightness of modes that do make a bit more sense, but they don't seem to be expanding the idea to the range of points that this video tries to make.
Comment: For example, it doesn't seem to me to support the idea that going round the *major keys* in the circle of fifths represents a gradient in terms of absolute brightness?
Comment: I will just say that you have to assume in the logic presented here that you regard the interval between the lowest played note and each note for brightness calculation. So you sum all the intervals and it is well defined.
Comment: The brightness relations of church modes based on the same key are widely accepted, and is estimated in this way, from what I understand.
Comment: @user2679290 if it was the interval between the lowest played note and each note, then a minor triad in first inversion would be *brighter* than a major triad in first inversion - is that a correct interpretation?
Comment: The GIF you posted is not a famous illusion; the drawing on which the poor man is rolling around is a famous artwork by [M. C. Escher]( https://en.m.wikipedia.org/wiki/M._C._Escher).
Answer: There is a long history of the character of keys and scales. As ggcg says it comes before the time of equal temperature where the different keys had actually a specific sound (because of the pythagorean tuning.
Even the Greek had their theories about the character of the modes. This has been overtaken even in the medieval church music and later, when the names of the modes were the same but they weren't referring to the same hexachords anymore!
To me this question is really an opinion based thing. You'll find a lot of discussion also among composers in the last centuries.
In my opinion people who are swearing about this theory are influence by tradition (and associative learning) or it could be a certain function of perfect pitch.
Edit:
As phoog correctly says, the modes of the Greek were tetrachords, but the idea that certain  keys had a special character has been derived and developed of those theories about modes. It is also correct that later a-minor and d-minor were built or C-Ionian and G-Ionian have been built on identical tetrachords. But the tuning of the Renaissance give them a special unique sound which was not an  argument later when equal temperature came up.
Comment: Greek music was based on tetrachords.  Hexachords came later.  But modal differences are analogous to the differences between major and minor keys, while the question here is about differences between different major keys, that is, between different keys that nominally are the same mode.
Answer: Firstly, no two pianos are tuned exactly a like. Using one instrument to make a broad claim is not scientific. Even if a large sample of pianos were used, then the results would be valid for piano.
Now consider that different major keys are rooted at a different pitch. C# major is
half a step higher than C major in the same octave range.  Simply, higher is "brighter". Lots of pop songs shift key up a half or full step toward the end to create a bright mood shift, with all else being the same (chord progression, melody, rhythm).
Musicians often describe tones with less high frequency content (for instance treble knobs rolled off on amplifiers, or over-wound electric guitar pickups) as "dark" and their opposites are called "bright".
Comment: It doesn't sound like a complete explanation, as for example, you could play something like low-B that has the same features as high-B... And it is supposedly darker than C


Convexity of $A \mapsto |A^{T}A|^{2}$

Question: This is an extension to Convexity of $|A^TA|$. Let $A \in \mathbb{R}^{m \times n}$ and let $|\cdot|$ denote the Frobenius (matrix) norm. Define function $f : \mathbb{R}^{m \times n} \to \mathbb{R}$ as $f(A) := |A^{T} A|^{2}$. Is $f$ convex?
Answer: $f(x)=x^2$ is convex and increasing, also $g(A) = \lvert A^T A\rvert$  is convex, therefore $h(A)=f(g(A)) = \lvert A^T A\rvert^2$ is convex by composition rules.

Convexity of $A \mapsto |A^{T}A|^{2}$


GraphDB free does not run on Windows 10

Question: I have tried to install GraphDB free 9.0.0 on a Windows 10 laptop (x64). The installation finishes succesfully but when I double click GraphDB it does not run. What should I check in order to fiind out why it does not run?
Thank you
Comment: Are you running the native application (exe) or the standalone server (zip)? One possible cause for the problem is if you don't have java installed.
Answer: I install GDB regularly on Windows 10 using the installer (eg GraphDB_Free-9.3.0.exe). It comes bundled with Java. Here's the version:
<code>cd C:\Users\vladimir.alexiev.000\AppData\Local\GraphDB Free\runtime\bin
java --version
openjdk 11.0.7 2020-04-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.7+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.7+10, mixed mode)
</code>
It's convenient because after starting, it opens Workbench in your browser, and a small control panel where you can see the log etc, and a tray icon.
If you use one of the other distributions, check your Java version.

GraphDB free does not run on Windows 10


Query result to display in Views

Question: i have created a custom view. its just showing nodes of a particular type. Now i want to do something with it. I am writing a SQL query:
global $user;
$query = db_select('challenges','c');
$query->join('node','n','n.nid = c.cid');
$query->join('field_data_body','f','n.nid = f.entity_id');
$query->join('field_data_field_challenge_pic', 'fp','n.nid = fp.entity_id');
$query->join('file_managed','fm','fp.field_challenge_pic_fid = fm.fid');
$query->fields('n',array('nid','title','created'))
      ->field('f',array('f','body_value'))
      ->field('fp',array('fm','uri'))
      ->condition('c.cid','$user->uid','=');
$result = $query->execute();

Where should i place this query in my custom module and how can i get my view to display this? I tried writing this code in a tpl file for the view and then did print_r and render() but no effect. I think it should be done through views.
Answer: write code in your module like this way.
Declaring the block
function mymodule_block_info() {
  $blocks['current_posts'] = array(
    'info' => t('Current posts'), //The name that will appear in the block list.
    'cache' => DRUPAL_CACHE_PER_ROLE, //Default
  );
  return $blocks;
}

Retrieving data
    function mymodule_contents(){
      $query = db_select('challenges','c')
$query = db_select('challenges','c');
$query->join('node','n','n.nid = c.cid');
$query->join('field_data_body','f','n.nid = f.entity_id');
$query->join('field_data_field_challenge_pic', 'fp','n.nid = fp.entity_id');
$query->join('file_managed','fm','fp.field_challenge_pic_fid = fm.fid');
$query->fields('n',array('nid','title','created'))
      ->field('f',array('f','body_value'))
      ->field('fp',array('fm','uri'))
      ->condition('c.cid','$user->uid','=')
        ->execute();
      return $query;
    }

Generating block content
function mymodule_block_view($delta = '') {
  switch($delta){
    case 'current_posts':
      $block['subject'] = t('Posts');
      if(user_access('access content')){
        //Use our custom function to retrieve data.
        $result = mymodule_contents();
        //Array to contain items for the block to render.
        $items = array();
        //Iterate over the resultset and format as links.
        foreach ($result as $post){
          print $post;
        }

        if (empty($items)) { //No content in the last week.
          $block['content'] = t('No posts available.');
        }
        else {
          //Pass data through theme function.
          $block['content'] = theme('item_list', array(
            'items' => $items));
        }
      }
    return $block;
  }

}

Comment: hi...thanks but this is to display a block...i want to create a view
Comment: do u mean views page???
Comment: yes...i am trying to create a custom view
Comment: are you not using views module???
Comment: what i am trying to achieve cant be done through the views UI...as you may gathered i have created a custom table 'challenges'. Here through a custom form submit action i am entering some values...node id of the page on which the form is, and the user id of the user who clicked the form. Now i want that the user be able to look at all his challenges he accepted by clicking the submit button
Comment: where can i write these queries for my custom views

Query result to display in Views


multivariate classification r

Question: I am analyzing a dataset with 5 factors(Y1,Y2,Y3,Y4,Y5).
ID          Y1    Y2    Y3    Y4   Y5
1           5     1     2     9    40
2           6     1    17     9    49
3           5     1     6    10    25
4           5     1    14     6    69
5           7     1    19    15    66
6           5     1     6     7    24
.           .     .     .     .     .
.           .     .     .     .     .
300         6     1     2    12    28

The mean and standard deviation of each factor(y_i) is as follows
avg1   avg2   avg3   avg4   avg5
5.39   1.02   11.8   9.61   42.1

sd1   sd2    sd3   sd4   sd5
1.22  0.145  10.1  3.61  14.5

I have two new observations
   ID         Y1    Y2    Y3    Y4   X5
   *          6     1    18     7    36
   **         3     5     1     3    37

What statistical method should I use if my goal is to determine which out of these two observations are closer or most similar to the sample. Thanks.
Answer: While this might be a terrible assumption for your case, if you are willing to make an assumption that your data are multivariate normal, then you might be looking for the Mahalanobis distance.
$$
d(\vec x) = \sqrt{
(\vec x - \vec \mu)^TS^{-1}(\vec x - \vec \mu)
}
$$
Each of your two five-dimensional points would be an $\vec x$. The multivariate mean is $\vec\mu$, so $\vec\mu = (5.39,   1.02,   11.8,   9.61,   42.1  )^T$. $S$ is the covariance matrix of your five $Y$ variables.
The intuition is as follows:
In a univariate Gaussian, we know that $\sim68\%$ of the density is contained within one standard deviation of the mean, $\sim95\%$ of the density if within two standard deviations of the mean, etc. In that sense, standard deviation gives a standardized measurement: no matter how big or small the standard deviation, you know that being $0.1\sigma$ from the mean is a small deviation. In the univariate case, Mahalanobis distance is exactly the z-score.
$S$ is the variance, which is just a number. The $(\vec x - \vec \mu)$ is a number, so the transpose is not so meaningful, and I am content to drop it (just for the univariate case). All of these quantities are just numbers, so we are free to multiply them in whatever order we wish. Therefore:
$$
d(x) = \sqrt{
(x - \mu)^T\mathbb Var(X)^{-1}(x - \mu)
}\\
=
\sqrt{
\dfrac{(x-\mu)^2}{Var(X)}
}\\
=
\dfrac{x - \mu}{\sigma}
$$
This is exactly the z-score!
In that sense, Mahalanobis distance is somewhat of a generalization of z-score. Mahalanobis distance measures how far you are from the mainstream of the density, accounting for the fact that the multivariate density might be longer in some dimensions than others (different univariate variances) and rotated (due to correlations).
Note that the rule about how much density is within $1$ or $2$ (or any other number) is not as simple in the univariate case, and it depends on the dimension of the vector.
Comment: Thanks Dave, I was thinking about this approach did not know how to implement, your explanation clears some cobweb in my mind. For multivariate normality assumption, i am planning to check for normality at univariate level for each Y using QQ Plots
Comment: QQ plots are a nice visualization to check univariate normality. It's also important to have joint normality, and I don't have a good solution to checking that in five dimensions. That could be a nice topic for a new question!

multivariate classification r


Disk IO causing high load on Xen/CentOS guest

Question: Background
I'm having serious issues with a xen based server, this is on the guest partition. It's a paravirtualized CentOS 5.5. I'm not sure if it's hardware or software related, or in between (drivers).
Basic information
Updated controller firmware (this was done as the last step)
Smart Array 6i in Slot 0
   Hardware Revision: Rev B
   Firmware Version: 2.84

Updated kernel
Linux domU 2.6.18-194.32.1.el5xen #1 SMP Wed Jan 5 19:32:33 EST 2011 i686 i686 i386 GNU/Linux

The problem is in disk write speed.
Baseline performance is

dom0 ~30MB/s
domU ~4MB/s (small files)
domU ~1.5MB/s (large files)

The following numbers are taken from top while copying a large file over the network.
If i copy the file another time the speed decreases in relation to load average. So the second time it's half the speed of the first time.
It needs some time to cool off after this. Load average slowly decreases until it's once again usable. ls / takes about 30 seconds.
top - 13:26:44 up 13 days, 21:44,  2 users,  load average: 7.03, 5.08, 3.15
Tasks: 134 total,   2 running, 132 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.1%sy,  0.0%ni, 25.3%id, 74.5%wa,  0.0%hi,  0.0%si,  0.1%st
Mem:   1048752k total,  1041460k used,     7292k free,     3116k buffers
Swap:  2129912k total,       40k used,  2129872k free,   904740k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1506 root      10  -5     0    0    0 S  0.3  0.0   0:03.94 cifsd
    1 root      15   0  2172  644  556 S  0.0  0.1   0:00.08 init

Meanwhile the host is ~0.5 load avg and steady over time. ~50% wait
Server hardware is dual xeon, 3gb ram, 170gb scsi 320 10k rpm, and shouldn't have any problems with copying files over the network.
disk = [ "tap:aio:/vm/domU.img,xvda,w" ]

I also get these in the log
INFO: task syslogd:1350 blocked for more than 120 seconds.
"echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.
syslogd       D 00062E4F  2208  1350      1          1353  1312 (NOTLB)
       c0ef0ed0 00000286 6e71a411 00062e4f c0ef0f18 00000009 c0f20000 6e738bfd
       00062e4f 0001e7ec c0f2010c c181a724 c1abd200 00000000 ffffffff c0ef0ecc
       c041a180 00000000 c0ef0ed8 c03d6a50 00000000 00000000 c03d6a00 00000000
Call Trace:
 [<c041a180>] __wake_up+0x2a/0x3d
 [<ee06a1ea>] log_wait_commit+0x80/0xc7 [jbd]
 [<c043128b>] autoremove_wake_function+0x0/0x2d
 [<ee065661>] journal_stop+0x195/0x1ba [jbd]
 [<c0490a32>] __writeback_single_inode+0x1a3/0x2af
 [<c04568ea>] do_writepages+0x2b/0x32
 [<c045239b>] __filemap_fdatawrite_range+0x66/0x72
 [<c04910ce>] sync_inode+0x19/0x24
 [<ee09b007>] ext3_sync_file+0xaf/0xc4 [ext3]
 [<c047426f>] do_fsync+0x41/0x83
 [<c04742ce>] __do_fsync+0x1d/0x2b
 [<c0405413>] syscall_call+0x7/0xb
 =======================

I have tried disabling irqbalanced as suggested here but it does not seem to make any difference.
Updates:
domU# cat /sys/block/xvda/queue/scheduler
[noop] anticipatory deadline cfq

Copying files from/to the disk causes load to remain < 4. Subsequent copying causes load to increase.
Copying files over the network causes load > 4 in the first run, subsequent copying causes the server to halt almost completely, demanding time to cool off, it never goes down, just give it 10-15 mins to get back up. But it's really not viable for a server to behave like that.
The network traffic is in itself not causing any trouble running iperf for instance does not produce any measurable effect. Reported bandwidth is > 1gbit.
Write performance on dom0 is OK
dom0# dd if=/dev/zero of=./test1024M bs=1024k count=1024 conv=fsync
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 34.9725 seconds, 30.7 MB/s

Write performance on domU is sluggish
domU# dd if=/dev/zero of=./test1024M bs=1024k count=1024 conv=fsync
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 622.163 seconds, 1.7 MB/s

This is ~95% performance loss.
Read performance is OK
dom0# hdparm -tT /dev/cciss/c0d0p1

/dev/cciss/c0d0p1:
 Timing cached reads:   3352 MB in  2.00 seconds = 1676.70 MB/sec
 Timing buffered disk reads:  100 MB in  2.59 seconds =  38.57 MB/sec

domU# hdparm -tT /dev/xvda

/dev/xvda:
 Timing cached reads:   3144 MB in  2.00 seconds = 1571.51 MB/sec
 Timing buffered disk reads:  120 MB in  3.03 seconds =  39.67 MB/sec

Update:
So it appears that this was hardware related after all. But it didn't show up until running xen. The battery was not charging ok, this led to the cache being disabled. This in combination with a VM running intensive IO led to high wait times.
Now immediately after upgrading firmware nothing had changed, but since the firmware upgrade, the battery is now charging correctly. And after the battery is fully charged, the write speeds are now acceptable, for small files the exceed those of dom0, no clue as to why that happens.
domU# dd if=/dev/zero of=./test1024M bs=1024k count=1024 conv=fsync
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 39.1087 seconds, 27.5 MB/s

Comment: What is the OS of the host? On the guest what is the output of `cat /sys/block/xvda/queue/scheduler`?
Comment: OS is Centos 5.5, i'll have to check the output later.
Comment: What scheduler am i supposed to use?
Comment: You are supposed to use the noop, which you are.
Comment: But this can't be expected, can it? I mean the performance is really bad. Can it have something to do with preallocated/sparse files? If i do ls on the file it reports 50GB but if i do df it only reports the part of the file that's being used, say 10GB.
Comment: This looks like a bug report for Xen/CentOS, I would take your problem to both of them and see what ideas they might have. Check against their known bugs database first.
Comment: I thought i'd try to narrow the problem down to software before reporting it as a bug. I'll order a new battery for the cache module before reporting it.
Comment: So this has been resolved. But i'll leave it up here for future reference.
Comment: So didn't your RAID controller raise some kind of alarm?
Comment: It did during POST, but who watches that on a server in an off-limits server room? :) The company i work for doesnt have ILO licenses either. It wasn't until I started fiddling with the firmware that I noticed really. It was a fun excercise though.
Answer: I would blame it on the old xen-version used on your host (CentOS 5.5).
I can really recommend using SLES 10 or 11. 11 has XEN 4.x built in.
I have no performance issues with my CentOS 5 DomUs running on SLES 10 Dom0s.


Find a base for which the matrix of a linear transformation is diagonal

Question: Define $T : R^2→R^2 $
by $T(x)=Ax$
Find a basis $B$ for $R^2$ with the property that $[T]_B$ is diagonal.
$$A=\left(\begin{matrix}1 & -7\\-5&3\end{matrix}\right)$$
I have worked out the eigenvalues: $\lambda =-4, 8$
I am confused as to what they're asking, what should I work out and try to prove? So I must find a vector(basis) $B$ so that $T$ with relation to $B$ is diagonal? Must I find the eigenvalues?
Comment: You have to diagonalize the matrix: the eigenvectors form a base where the matrix is diagonal. Here the matrix is certainly diagonalizable since every eigenvalue has multiplicity $1$. Thus, you have to solve the two systems $(A-\lambda I)(x_1,x_2)^T=0$.
Comment: Once you have an eigenvector $u$, any $ku$ with real $k$ is also an eigenvector: there is actually an eigen*space*. So both are correct, yes. However, your first is not correct, it should be $(7/5,1)^T$.
Comment: @Jean-ClaudeArbaut my second basis is $(-1,1)^T$ which is correct but then a answer for the first basis ($\lambda = -4$)is $(7,5)^T$. I reduce $A + 4.I$ and get the following equation: $5x_1 -7x_2 = 0$. How do you get the 7 and 5 from that equation? I guess if you do $5x_1 = 7$ by letting $x_2 = 1$, but then shouldnt  $x_1 = 7/5$, or are both answers correct i.e $(7/5,5/7)^T$ and $(7,5)^T$
Answer: First, to find the eigenvalues, you compute $\det A-\lambda I=(1-\lambda)(3-\lambda)-35=(\lambda-8)(\lambda+4)$. Hence the eigenvalues are $8$ and $-4$.
To find an eigenvector associated with $\lambda=8$, you solve the system
$$\left(\begin{matrix}-7&-7\\-5&-5\end{matrix}\right)\left(\begin{matrix}x_1\\x_2\end{matrix}\right)=\left(\begin{matrix}0\\0\end{matrix}\right)$$
This is equivalent to the single equation $x_1+x_2=0$, so there is an arbitrary parameter, say $x_2$, and $x_1=-x_2$. With $x_2=-1$, that yields the eigenvector $(1,-1)^T$.
For the eigenvalue $-4$, the system is
$$\left(\begin{matrix}5&-7\\-5&7\end{matrix}\right)\left(\begin{matrix}x_1\\x_2\end{matrix}\right)=\left(\begin{matrix}0\\0\end{matrix}\right)$$
This is equivalent to the single equation $5x_1-7x_2=0$, so there is also an arbitrary parameter. With $x_2=5$, you get $x_1=7$, so the eigenvector $(7,5)^T$.
Now, the matrix of your new base is $P=\left(\begin{matrix}1&7\\-1&5\end{matrix}\right)$. In this base, the matrix of the transformation $T$ is diagonal: $P^{-1}AP=\left(\begin{matrix}8&0\\0&-4\end{matrix}\right)$.


batch script to copy most recent file in directory

Question: Hi here is what I need to get accomplished and not sure how. I have a SQL sever that does daily backup of the transaction logs to a local E drive on the sql server. What I need to do is copy the most recent log file from that server to a remote server. Once I get the script to work I will then setup as a schedule task. Can anyone give me advice on how to copy the most recent file to a different server. This will run daily as well so the file name date will change that is why I need the most recent file in the directory where the backup are going.
Answer: Use in the batch file these command lines:
for /F "delims=" %%I in ('dir "C:\Path To\Source Folder\*" /A-D-H /B /O-D /TW 2^>nul') do copy /B /Y "C:\Path To\Source Folder\%%I" "X:\Path To\Destination Folder\" >nul & goto FileCopied

:FileCopied
rem More commands after copying file with newest last modification date.

This code is explained for example at Get newest file in directory with specific extension. Command DIR excludes here also files with hidden attribute set as command COPY does not copy hidden files. The destination folder must already exist.
Enhanced version with real folder paths:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceFolder=E:\MSSQL\Backup\AaaImaging"
set "TargetFolder=\\servername\F\MSSQL\Backup\Pablo"
for /F "delims=" %%I in ('dir "%SourceFolder%\*" /A-D-H /B /O-D /TW 2^>nul') do copy /B /Y "%SourceFolder%\%%I" "%TargetFolder%\" >nul & goto FileCopied

:FileCopied
rem More commands after copying file with newest last modification date.
endlocal

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

copy /?
dir /?
echo /?
endlocal /?
for /?
goto /?
rem /?
set /?
setlocal /?

Read also the Microsoft documentation page about Using command redirection operators and Single line with multiple commands using Windows batch file.


Drupal function field_get_items vs user_load

Question: In Drupal, I want to read a custom field value for a group of users while logged into an admin account. I only need 1 field value from each user, the same field and I do not need this information cached.
When I use user_load it requests the user object as I only need one field this is very inefficient.
So I investigated field_get_items, but I cannot see how to use this function without first using user_load.  I have attached my code and MARKED the line that is giving me an issue.

how can I use field_get_items without using user_load, if not possible is there a efficient way of doing this?  Please don't mention user_load_multiple
with user_load does setting , $reset = TRUE prevent Drupal caching the data or does it force a cache reset, get the data from the DB and then re-cache the new data?

Code I use:
$user_a = array();  //  This is the array of URIs we need to query
foreach ($user_a AS $uid){
  $account = user_load($uid, $reset = TRUE);  // access the user objects, one at a time
  $fullname[$uid] = $account->field_fullname[LANGUAGE_NONE][0]['value']; // extract value
}

$user_a = array();  //  This is the array of URIs we need to query
foreach ($user_a AS $uid){
  **$account = user_load($uid, $reset = TRUE);**
  $fullname[$uid] = field_get_items('user', $account, 'field_fullname');  // extract array
  foreach ($fullname[$uid] as $item) {
    $fullname[$uid] = $item['value'];  // extract value
  }
}

Answer: No, You can't use field_get_items without the loaded user object. As the entity has to be passed as the second argument as illustrated in documentation.
Any reason why you hate user_load_multiple? As user_load itself uses user_load_multiple.
If you don't want to retrieve that huge amount of arrays. You can consider building your own query to retrieve just the field you want.
Comment: I don't hate user_load_multiple.  In this case it has the same problem as user_load, in that I need one value and it is retrieving the larger user object.  I can write a query to access the one value, I just hoped that Drupal would already ahve a way of doing this.
Comment: Unfortunately, it doesn't. You have to do it the hard way.
Comment: I guess you are the right answer then.  Any advice on part (1) alternatives or part (2) caching
Comment: I guess there's no alternative, as I told you that I had the same issue and I had to write a custom query
Comment: Yes, there is an alternative which does not require querying the DB: field_attach_load(). If not usable, user_load_multiple() may load too much data but at least it will perform better since it reduces the number of DB queries required to retrieve the data (because it can uses queries for all user).
Answer: You can uses field_attach_load() to load the value of a single field for multiple entities (users are entities). You will need an array of stub entities for your users, which can be build using entity_create_stub_entity(). You will also need the field ID of your field, which can found using field_info_field() or field_info_read().
After using field_attach_load(), you should be able to get the values of the field using field_get_items().
Note the field_attach_load() may inject data for other fields into the stub entities, for example if they are read from a cache.
Comment: Think there is a miss type, "may object data for other fields".  This sounds like something I should be aware of, can you edit/elaborate?
Comment: This took a lot of working out, I found the drupal explanations of the various functions very unhelpful.  I had to resort to deconstructing the code for each to work out what they were doing.
Comment: Indeed, the documentation of the functions is not clear and does not provide any indication on how to use them together. Contributions are always welcome on https://drupal.org/node/443536 and in the code, so hopefully one day we may get a better documentation.


Servidor apache slave

Question: A dúvida é bem simples, portanto desculpe -me pela ignorância. Estou montando um website com o apache e o mesmo vai receber  milhares de acesso, a dúvida seria, o que fazer quando o limite de acesso do apache se esgotar? Existe alguma forma de colocar servidores simultâneos?
Answer: Quando assim você tem que criar um cluster.
O que é um cluster?
Um cluster de computador é um conjunto de computadores conectados que trabalham juntos para que, em muitos aspectos, possam ser vistos como um único sistema. Ao contrário dos computadores conectados simplesmente em rede onde os computadores se comportam como vários indivíduos, os clusters de computadores têm cada nó(máquina) configurado para executar a mesma tarefa, controlada e programada pelo software o que faz com que todos ajam como um.
Os componentes de um cluster são normalmente conectados uns aos outros através de redes locais rápidas, com cada nó (computador usado como servidor) executando sua própria instância de um sistema operacional. Na maioria das circunstâncias, todos os nós usam o mesmo hardware, Cluster Beowulf.
Para lhe informar os detalhes de como fazer o cluster preciso saber o OS, pois só informar Linux não é o suficiente pois nem todos os Linux podem ser diretamente clusteados portanto precisando de software especializado, normalmente infraestrutura para softwares de MPI(Message Passing Interface).
Tipos de clusters:

Cluster de Alto Desempenho: denominado, também, de Alta Performance (High Performance Computing - HPC), sua característica é o grande volume de processamento de dados em computadores convencionais, que garante baixo custo na construção, e com processamento na ordem de gigaflops. Os servidores deste cluster trabalham com a tecnologia de paralelismo, dividindo o processamento com as outras máquinas, buscando a otimização e desempenho de um supercomputador.
Cluster de Alta Disponibilidade (High Availability - HA): são caracterizados por se manterem em pleno funcionamento por um longo período de tempo, utilizando redundância para manter um serviço ativo e se proteger de falhas, geralmente são computadores convencionais que disponibilizam o mesmo recurso em todas as máquinas da rede, configuradas com prioridades diferentes, onde existe um servidor ativo e os outros ociosos.
Cluster de Balanceamento de Carga (Horizontal Scaling - HS): são caracterizados por dividirem, de forma equilibrada, as tarefas entre os membros do cluster, onde cada nó atenda a uma requisição e não, necessariamente, que divida uma tarefa com outras máquinas.

Solução:
Como já escrevi cada sistema tem suas particularidades. Então para para você ter uma noção aqui tem um tutorial de como criar um cluster de Balanceamento de Carga no Ubuntu com servidor Apache. É um tutorial de duas páginas. Não é difícil de segui-lo porém é trabalhoso.
Answer: Você pode implementar um load balancer para dividir as requisições em mais servidores, pode usar também um servidor de cache intermediário para diminui as conexões com o apache.
Att.

Servidor apache slave


How to chain order, group, mutate, pivot in data.table?

Question: I am new to data.table & trying to replicate some dplyr code but having issues with pivot / reshaping data in data.table code.
libs
library(data.table)
library(lubridate)
library(tidyverse)

df
test_df <- data.frame(id = c(1234, 1234, 5678, 5678),
           date = c("2021-10-10","2021-10-10", "2021-8-10", "2021-8-15"),
           Amount  = c(54767, 96896, 34534, 79870)) %>%

  mutate(date = ymd(date))

dplyr code:
test_df %>%
  group_by(id) %>%
  arrange(date) %>%
  mutate(Amt_first = first(Amount),
         Amt_last = last(Amount)) %>%
  ungroup() %>%

  pivot_longer(cols = c(Amt_first:Amt_last), names_to = "Amt_catg",
  values_to = "Amt_val")

results:
# A tibble: 8 x 5
     id date       Amount Amt_catg  Amt_val
  <dbl> <date>      <dbl> <chr>       <dbl>
1  5678 2021-08-10  34534 Amt_first   34534
2  5678 2021-08-10  34534 Amt_last    79870
3  5678 2021-08-15  79870 Amt_first   34534
4  5678 2021-08-15  79870 Amt_last    79870
5  1234 2021-10-10  54767 Amt_first   54767
6  1234 2021-10-10  54767 Amt_last    96896
7  1234 2021-10-10  96896 Amt_first   54767
8  1234 2021-10-10  96896 Amt_last    96896

data.table attempt:
setDT(test_df)[order(date),
             `:=`(Amt_first = data.table::first(Amount),
                   Amt_last = data.table::last(Amount)),
             by = id] %>%

    # https://cran.r-project.org/web/packages/data.table/vignettes/datatable-reshape.html
  data.table::melt(measure.vars = c("Amt_first","Amt_Last"),
                   variable.name = "Amt_catg", value.name = "Amt_val")

I am getting an error here.
Comment: *"I am getting an error here"*. **Please** don't keep secrets from us. If you are getting an error, include it in the question, especially when the premise of the question is based on that one error. In this case Waldi found it, maybe without having to run the code. In this case, I suspect that if we immediately saw `One or more values in 'measure.vars' is invalid`, somebody would have checked your `measure.vars = c("Amt_first","Amt_Last")` to do a quick comparison with previous code/data.
Answer: The output can be achieved with a full join:
setDT(test_df)

merge(
  test_df,
  test_df[order(date),
          .(Amt_catg = c('Amt_first', 'Amt_last'),
            Amt_val = Amount[c(1L, .N)]),
          by = id]
)

#      id       date Amount  Amt_catg Amt_val
# 1: 1234 2021-10-10  54767 Amt_first   54767
# 2: 1234 2021-10-10  54767  Amt_last   96896
# 3: 1234 2021-10-10  96896 Amt_first   54767
# 4: 1234 2021-10-10  96896  Amt_last   96896
# 5: 5678 2021-08-10  34534 Amt_first   34534
# 6: 5678 2021-08-10  34534  Amt_last   79870
# 7: 5678 2021-08-15  79870 Amt_first   34534
# 8: 5678 2021-08-15  79870  Amt_last   79870

Answer: You get an error because you misspelled Amt_last :
setDT(test_df)[order(date),
                 `:=`(Amt_first = data.table::first(Amount),
                      Amt_last = data.table::last(Amount)),
                 by = id] %>%
  data.table::melt(id.vars=c('id','date'),measure.vars = c("Amt_first","Amt_last"),
                   variable.name = "Amt_catg", value.name = "Amt_val")

     id       date Amount  Amt_catg Amt_val
1: 1234 2021-10-10  54767 Amt_first   54767
2: 1234 2021-10-10  96896 Amt_first   54767
3: 5678 2021-08-10  34534 Amt_first   34534
4: 5678 2021-08-15  79870 Amt_first   34534
5: 1234 2021-10-10  54767  Amt_last   96896
6: 1234 2021-10-10  96896  Amt_last   96896
7: 5678 2021-08-10  34534  Amt_last   79870
8: 5678 2021-08-15  79870  Amt_last   79870


Encode Custom Data Type to Lazy Byte String

Question: when you want to convert a custom type into bytestring you would do following:
data Foo = Foo {value1 :: Int}
instance Binary Foo where
   get =liftM Foo get
   put b = put (value1 b)

and if you have a type with multiple viable values as such:
data Foo2 = Foo2A Int | Foo2B Int
instance Binary Foo2 where
   get = do flag <- getWord8
         case flag of
               0 -> fmap Foo2A get
               1 -> fmap Foo2B get
   put (Foo2A i) = do put (0 :: Word8)
                      put i
   put (Foo2B i) = do put (1 :: Word8)
                      put i

but if you have a type as such (following...) how would I do this?:
data Foo3 = Foo3A Int | Foo3B
instance Binary Foo3 where
   get = do flag <- getWord8
            case flag of
                 0 -> fmap Foo3A get
                 1 -> ....????? Foo3B has no value - only Data Constructor
   put (Foo3A i) = do put (0 :: Word8)
                      put i
   put (Foo3B)   = put (1 :: Word8)

Answer: To match what you wrote for put, you want pure Foo3B there in get.
Answer: You can also derive these instances:
newtype Foo = Foo {value1 :: Int}
  deriving newtype Binary

data Foo2 = Foo2A Int | Foo2B Int
  deriving stock Generic
  deriving anyclass Binary

data Foo3 = Foo3A Int | Foo3B
   deriving stock Generic
   deriving anyclass Binary

Comment: @ThomasMeyer That's because you called `decode` at the wrong type. Try adding a type signature.
Comment: with this example the result of (decode . encode) Foo3B is emtpy (=() ) for me


How to solve $\int_0 ^{2\pi} \frac{1}{4{\cos}^2t+9{\sin}^2t}dt$

Question: $$\int_0 ^{2\pi} \frac{1}{4{\cos}^2t+9{\sin}^2t}dt$$
Using trigonometric identity, I changed to $\frac{1}{4+5{\sin}^2t}$
Then I'm lost..
what should I do to solve this question?
Comment: make the substitution $t=\arctan(u)$
Comment: You can compute this using the residue formula in complex analysis.
Comment: @DamianRössler that'll be an overkill
Answer: $$
\begin{aligned}
\int_0^{2 \pi} \frac{1}{4 \cos ^2 t+9 \sin ^2 t} d t &=4 \int_0^{\frac{\pi}{2}} \frac{\sec ^2 t}{4+9 \tan ^2 t} d t \\
&=4 \int_0^{\infty} \frac{d y}{2^2+(3 y)^2},\quad \textrm{ where }y=\tan t \\
&=\frac{4}{6}\left[\tan ^{-1}\left(\frac{3 y}{2}\right)\right]_0^{\infty} \\
&=\frac{\pi}{3}
\end{aligned}
$$
Answer: Let $t=x$
$$\int\frac{1}{5\sin^2 x+4}dx$$
$\displaystyle\sin x=\frac{\tan x}{\sec x}$
$$=\int\sec^2 x\cdot\frac{1}{9\tan^2 x+4}dx$$
Do $u=\tan x\implies\displaystyle\frac{du}{dx}=\sec^2 x$
$$=\int\frac{1}{9u^2+4}du$$
Do $\displaystyle v=\frac{3u}{2}\implies\frac{dv}{du}=\frac{3}{2}$
$$=\int\frac{2}{3(4v^2+4)}dv$$
$$=\int\frac16\frac{1}{v^2+1}dv$$
$$=\frac{\arctan v}{6}$$
$$=\dfrac{\arctan\left(\frac{3\tan\left(x\right)}{2}\right)}{6}$$
$$\dfrac{\arctan\left(\frac{3\tan\left(2\pi\right)}{2}\right)}{6}-\dfrac{\arctan\left(\frac{3\tan\left(0\right)}{2}\right)}{6}=\frac{\pi}{3}$$


Is there a term for songs that have a "surprise change" in their rhythm?

Question: There are many songs that begin with a repeating beat/instrumental that implies a certain rhythm (let's call it "Rhythm A"), but then when a later instrument enters the song, it turns out that the song actually has "Rhythm B", into which A is interleaved/mixed.
I notice this a lot with albums I hear for the first time — where I'm unaware of Rhythm B for the first 10–20 seconds — and it's a neat surprise to have what I assumed was Rhythm A turn out to be a component of a pretty-different Rhythm B.
One example of this is the opening to Luvofbahia by Wilczynski.
Is there a term for this?
Comment: ah man i love it. once the bass comes in it flips on its head, but now when i go back i can only hear it way it is with the bass.  do you happen to know if that is a sample (the guitar)?
Comment: soul coughing "white girl" https://www.youtube.com/watch?v=9WlXipLy6nI is a good example. also check out this video about radiohead's "videotape": https://www.youtube.com/watch?v=p_IHotHxIl8  however, i don't know the name of this technique.
Comment: Does the opening to Beethoven's V count?  Practically all non-orchestra members think the first notes are on downbeats.
Answer: It's apparently called "Turn the Beat Around" https://music.stackexchange.com/a/21984/51932
(Thanks to @leftaroundabout for the suggestion!)

Is there a term for songs that have a “surprise change” in their rhythm?


Passing custom fields as parameters in a REST query

Question: I am creating a REST Get request using Services 3.3 module on Drupal 7.15 which should get all the nodes filtered by a custom field field_asset_type
http://sharedrive/?q=rest/node&parameters[field_asset_type]=13
It returns following error
Invalid query provided, double check that the fields and parameters you defined are correct and exist. SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_asset_type' in 'where clause'
Kindly tell me how to resolve this problem. The articles on the internet explains how to pass built-in fields which is working but I also need to pass custom fields.
Please help
Answer: The default functionality only works with node properties, not fields. Fortunately you can use the Services Entity API module to get what you need:

Integration of Services and Entity API.
You can define an endpoint via Services and publish any entity data relying in Entity API.
This module allows to get entities by properties or fields and also paging and related entities that are defined properly.
Comment: Well the workaround is to use services_views module which allows a view to be accessed through services module
Comment: Just be careful with that module, it's pretty unstable in my experience.
Comment: I have custom fields available now but I would also want to filter sub-fields of the custom field. For example I only want field_content['und']['fid'] instead of the whole field_content.
Comment: Your best bet will be to submit a feature request to the module, a topic that broad isn't something we can deal with here I'm afraid


Error when saving data to MSSQL file

Question: Thanks to some help I fixed some of my previous problems but when i click the register button(button1) I get the following error:
ERROR = ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
My code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Odbc;

public partial class CompanyLogin : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
    OdbcConnection conn = new OdbcConnection();
    conn.ConnectionString = @".\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\VCtemps.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

    string sql = "insert into company values(@CompName, @BusinessType, @Pword)";
    OdbcCommand cmd = new OdbcCommand(sql);
    string CompName = txtCompName.Text;
    string BusinessType = DropDownList1.Text;
    string Pword = txtPassword.Text;
    conn.Open();
    cmd.Connection = conn;

    cmd.CommandText = "insert into company(CompName, BusinessType, Pword) Values(@CompName,@BusinessType,@Pword);";
    cmd.Parameters.AddWithValue("@CompName",SqlDbType.VarChar);
    cmd.Parameters.AddWithValue("@BusinessType",SqlDbType.VarChar);
    cmd.Parameters.AddWithValue("@Pword",SqlDbType.VarChar);

cmd.ExecuteNonQuery();

    conn.Close();

    txtCompName.Text = "";
    txtPassword.Text = "";
    DropDownList1.Text = "";
}
}

Comment: note that you're setting the SQL text twice. You should remove one of them (the first one - which is incorrect) to avoid confusion.
Comment: ..and also, all of your AddWithValue lines are incorrect. You've passed in an SqlDbType rahter than the actual value. You should be passing the value to this function.

eg: cmd.Parameters.AddWithValue("@CompName", CompName);
Answer: pretty self explanatory error message. you need to specify a data source and a driver in your connection string. visit Connection Strings for Database to find what a connection string should look like for the database you're targeting.
Comment: Apart from the issue I raised with the double-escaping it exactly matches the connection string for SQLEXPRESS on the site you linked to, which is:

Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;
Answer: In your connection string, you've escaped your blackslashes AND you've used the @ sign in front of the string. Possibly the problem is that you've done both. Either remove the @ or remove the double slashes in both places.
@".\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\VCtemps.mdf...etc

...but to be honest, you shouldn't hard-code your connection string like that anyway. You should add your connection string to the web.config file to the ConnectionStrings section:
<configuration>
    <connectionStrings>
       <add name="ConnectionString" connectionString=".\SQLEXPRESS;AttachDbFilename=|DataDirectory|\VCtemps.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" />
    </connectionStrings>
...

Then change your code to:
conn.ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

Error when saving data to MSSQL file


Passing note vs changed chord

Question: I'm pretty new to theory, so apologies if this is a stupid question but I can't seem to figure this out.
Let's assume I am in the key of C major and I want to play the following progression on guitar:
Dm (ii) - Fmaj (IV) - Cmaj (I)
What happens if, before I switch from the Dm to the Fmaj chord, I lift my ring finger and change the F in the Dm chord to an E during the strumming pattern for an entire bar before changing to the Fmaj.
Does this fundamentally change the chord progression to something like this (and how does that relate to diatonic chords in C if it does):
Dm - Dsus2 - Fmaj - Cmaj
Or is that change considered a passing chord?
Also, assuming I arpeggiate through the chords in that order, will that then be considered a passing note?
Comment: a passing note would be just one beat and only the bass note  and not strumming the full chord.
Comment: *Dm - Fmaj - Cmaj* hasn't to be necessarily classified as *C major*. This progression can also be *Dm*. Examples, Pink Floyd *another brick in the wall* album is full with this progression.
Answer: If a chord - of any kind - lasts for a whole bar, it's more than a passing chord, and can have its own label for that bar - as you did in your example. Even if a chord is different for one beat in a bar, it's often shown in the chord chart as such. In simplified versions, though, this is the sort of detail that gets omitted, for fairly obvious reasons. One of which can be the inability of beginners to actually change for one beat.
Answer: @Tim's rule of thumb about duration is definitely solid. The rate of chord changes - called harmonic rhythm - is generally around two chords per bar for a fast harmonic rhythm to about one chord for two bars for a slow harmonic rhythm.
But, I would apply two other guides:

keep to the simplest harmonic description that shows the clear tonal functions
and Schoenberg's adage: follow the bass.

Your example...
Dm - Dsus2 - Fmaj - Cmaj
I assume these fingerings...

Dm    xx0231
Dsus2 xx0230
F     x33211
C     x32010

Apply the other two guides...
Simple harmonic functions: is there a functional difference between Dm and Dsus2? No. The root doesn't change which is a pretty good indication that harmonically nothing is happening. Additionally, in the major/minor system of harmony where fundamental chords are major, minor and diminished triads, is Dsus2 really a bona-fide chord? No.
Apply Schoenberg's adage. Literally, follow the bass, ignore the details in the upper voice(s). Again the bass isn't changing, the root isn't changing. There isn't a harmonic event.
Do not misunderstand this to mean the Dsus2 is not important.
The point is Dsus2 represents melodic events rather than harmonic events.
Duration doesn't matter too much. You could play Dm to Dsus2 as a quick pull off and go straight to F. Or, you could play all four "chords" with a quarter note stroke and give a full bar to each. Either way the sus2 part is just a non-chord tone decorating the proper chords Dm - Dm - F - C.
In everyday speaking Dsus2 will be called a "chord." That's just plain ol' natural speaking. But in the context of this question of passing note versus chord - it's not a chord, it's a neighbor tone.

Passing note vs changed chord