Understanding integers as a group in light of a general definition

Question: The place from where I am studying Abstract Algebra recommends the following as the most general understanding of a group, the rest reducible to this.
For a set $T$, the set of all bijections $Sym(T)$ from $T$ to $T$ constitutes a group under the product operation being the composition of these bijections. Identity is then a bijection onto itself.
But when thinking of a group in terms of bijections or transformations, how do we reconcile this understanding to understand the Integers being a group under addition? What is the bijection here?
Am I missing something here?
Comment: This recommendation is probably be a reference to [Cayley's Theorem](https://en.wikipedia.org/wiki/Cayley's_theorem). Note, however, that while it's always true, it's not always the most intuitive way to think of some specific groups.
Comment: I think your confusion has to do with the nature of the elements in each set. The elements of $Sym(T)$ are bijections, the elements of $\mathbb Z$ are integers.
Comment: I think 'd study abstract algebra somewhere else.

For good explanations and a more practical motivation, read Fraleigh's Abstract Algebra.
Comment: @victoria I am looking for a MOOC, or any free online course for this. But I shall look Fraleigh's up.
Answer: That definition is either wrong (as written) or rather misleading. Here is the usual definition:
A group is a set $G$ with a law of composition $\cdot: G^2 \to G$ such that

Identity: there is an element $1\in G$ which satisfies $g\cdot 1 = 1\cdot g=g$ for all $g\in G$
Inverse: for every $g\in G$ there is an element $h\in G$ such that $g\cdot h=h\cdot g = 1$ (this element is unique and is usually written $g^{-1}$)

It is sometimes convenient to write a group as a quadruple $(G,\cdot,-^{-1},1)$.
An example of a group is the group of bijections $\mathrm{Sym}(T)$ on a set $T$, with the law of composition given by function composition.  The identity is exactly the identity map $\mathrm{id}_T$ and the inverse of a bijection $f: T \to T$ is exactly the inverse map $f^{-1}: T \to T$.
There are certainly groups which are not of the above form. One way of seeing this is that for $|T|>2$ the group $\mathrm{Sym}(T)$ is not Abelian, meaning that the law of composition is not commutative. This is because given distinct elements $x,y,z\in T$, we can define bijections $f_1,f_2: T\to T$ by
$$f_1(x)=y,f_1(y)=x, f_1(w)=w \quad \text{for $w\in T\setminus\{x,y\}$}$$
$$f_1(y)=z,f_1(z)=y, f_1(w)=w \quad \text{for $w\in T\setminus\{y,z\}$}$$
In other words, $f_1$ switches $x$ and $y$ and leaves everything else fixed, and $f_2$ switches $y$ and $z$ and leaves everything else fixed. Then
$$(f_1\circ f_2)(y) = f_1(f_2(y))=f_1(z)=z$$
$$(f_2\circ f_1)(y) = f_2(f_1(y))=f_2(x)=x$$
are unequal, so $f_1\circ f_2 \neq f_2 \circ f_1$.
However, $\mathbb{Z}$ with addition is Abelian, so it can't be of the form $\mathrm{Sym}(T)$ for any set $T$.
What is true, however, is that every group is (isomorphic) to a subgroup of $\mathrm{Sym}(T)$ for some set $T$. This is known as Cayley's Theorem.
By subgroup of a group $(G,\cdot,-^{-1},1)$ we mean a subset $H\subset G$ for which $H$ is closed under $\cdot$ and taking inverses, and contains $1$.
Given a group $(G,\cdot,-^{-1},1)$, consider $\mathrm{Sym}(G)$. We then associated each element $g\in G$ to the function $\tau_g: G \to G$ defined by $\tau_g(h) = g\cdot h$. This is invertible, since $\tau_g \circ \tau_{g^{-1}} = \mathrm{id}_G$. More generally, $\tau_g \circ \tau_h = \tau_{g\cdot h}$.  This allows us to treat $G$ like a subgroup of $\mathrm{Sym}(G)$.
Comment: As far as I understand, within the scope of group theory, isomorphic groups are identical. So if all groups can be reduced to a subgroup of a $Sym(T)$, wouldn't the claim I put up in the question be correct with the added caveat of the subgroup. What I meant by a general form was that in this way, all the product operations defined with different groups can be viewed as compositions of bijections, allowing a consistent picture of a group. Otherwise, I am unable to deal with a general meaning of this product operation.
Comment: Yes, with the added caveat of being a subgroup, it is true. While it might be a useful way for understanding abstract groups in general, viewing groups as 'groups of symmetries', personally this context is not how I see groups and can get in the way of dealing with more general structures, where the word 'group' shows up but thinking in terms of 'symmetries' isn't conducive to intuition.  I just think of it as a nice binary operation. There are many binary operations, plus, times, max, composition, concatenation, etc, and groups represent the nicest of those.
Answer: There is a correspondence between integers and a certain class of bijections of $\mathbb{Z}$, as follows. For $n \in \mathbb{Z}$, define $f_n:\mathbb{Z} \to \mathbb{Z}$ by $f_n(k) = k+n$. Then $f_n$ is a bijection. Geometrially, $f_n$ translates $\mathbb{Z}$ to the right by $n$. Then the association $n \mapsto f_n$ is a way to think of each integer as a bijection $\mathbb{Z} \to \mathbb{Z}$.
Answer: The idea is not to think of a group in terms of bijections. It so happens that the elements of $\mathrm{Sym}(T)$ are bijections. The elements of $\mathbb Z$ are integers. Although there is a relationship between the two, the source you are studying from probably mentions $\mathrm{Sym}(T)$ as a good general exemple of group. In which case, the focus is on the group structure and properties, whatever the nature of its elements is.


If $x^2+ax+b=0$ has a rational root, show that it is in fact an integer

Question: I have tried as follows. Please help to double check the proof! Thank you!
Since $x=p/q$ ($p$, $q$ are integers), $(p/q)^2+(p/q)a+b=0$
So, $(p/q)^2=-b-a(p/q)$
then, $p^2=-bq^2-a(p/q)q^2$
and, $p = \dfrac {q(-bq-a(p/q)q)}{p}$
now, it is clear that $q \mid p$, thus $p/q$ must be an integer.
Comment: For number-theoretic stuff, you will usually find it clearer if you do not have division in your expression.
Comment: How is it clear that $q \mid p$? $(-bq - a(p/q)q)/p$ might not be an integer.
Comment: I see. Would this simplification help? q(-bq^2-ap)/p
Answer: You're doing fine up through $p^2=-bq^2-a(p/q)q^2$, but then you go a bit astray. At the end you have something of the form $p=\frac{qn}p$ for an integer $n$; this isn't a multiple of $q$ unless $\frac{n}p$ turns out to be an integer.
Let's simplify a bit to make things more readable: $p^2=-bq^2-apq=-q(bq+ap)$. You can certainly conclude from this that $q\mid p^2$. That's not a contradiction in itself, but you can derive one from it if you further assume that $\frac{p}q$ is in lowest terms. You'll need the fact that if $\gcd(a,b)=1$, and $a\mid bc$, then $a\mid c$. (Alternatively, you can use unique factorization here.)
Comment: I wouldn't phrase this as a proof by contradiction. You legitimately assume that $\gcd(p,q)=1$ and legitimately derive that $q\mid p^2$. These two conditions together can be shown to imply that $q=\pm1$, so that $\frac pq$ is an integer. No contradiction necessary.
Comment: I knew it has something involved with lowest term, but I couldn't recall the implication of p,q then are relatively prime. That was super helpful. Thank you!
Comment: @user1971025: You're welcome!
Comment: @Greg: It's true that no contradiction is necessary; it's also true that it doesn't hurt to phrase the argument that way.
Comment: @BrianM.Scott: In my opinion, unnecessary proofs by contradiction slow the learning process. Regardless of whether one agrees, I'll point out that your answer claims to reach a contradiction but simply does not: $q$ can well equal $\pm1$.
Comment: @Greg: You're mistaken: the argument that I was suggesting **does** reach a contradiction, with the assumption that $\frac{p}q$ is a non-integer rational root in lowest terms. The pædagogical claim really doesn't make much sense. It's nice when one can be significantly improved, and it's good to know as a general principle that many proofs by contradiction can be turned into direct proofs, but a valid proof is a valid proof.
Comment: A chacun son goût.
Answer: Note you forgot to mention that $a,b$ are integers, otherwise it is clearly false as one may divide by the leading coefficient of any quadratic to put it in that form. By the rational roots theorem all roots of $x^2+ax+b$ are of the form
$$
\pm\frac{\text{factor of constant coefficient}}{\text{factor of leading coefficient}} = \frac{\text{factor of constant coefficient}}{1} = \text{factor of constant coefficient}
$$
which is an integer.
Comment: Sorry! I assumed that every letter in number theory is considered integer. Your idea of using root as factor of coefficient is very interesting. Thank you!
Answer: If you use the theorem that if gcd(a,b)=d, then there exist integers x and y such that ax+by=d, it makes the proof much easier. The proof is as follows:
Let $p \over q$ be a solution to $x^2+ax+b=0$, with $gcd(p,q)=1$. We need to show that $q \vert p$.
Then $p^2 \over q^2$+$ap \over q$ + $b =0$.
Multiply by $q^2$:    $p^2 +apq+bq=0$.
Then $q \vert p^2$.
Since $gcd(p,q)=1$, there exist integers m and n such that $pm+qn=1$.
Multiply by p: $p^2m$$+pqn=p$.
Since $q \vert p^2$ and $q \vert pq$, $q \vert p$.


Convergence or Divergence of a Series Using Case Analysis

Question: In the problems below it's asked for which $r \in \Bbb R$ the series converges.
$$
a)\quad\sum_{k=0}^\infty \left( \left(\sum_{l=1}^k \frac1l\right) r^k\right)
$$
$$
b)\quad\sum_{k=0}^\infty \left( 2^{k^2}  r^k \right)
$$
In the first problem I use the summation formula of a finite geometric series $\sum_{k=0}^n a r^k = \frac{a-ar^{n+1}}{1-r}$ and rewrite it as $\lim_{n\to 0} \sum_{k=0}^n ar^k = \lim_{n\to 0} \frac{a-ar^{n+1}}{1-r}$. Now by defining $a:=\sum_{l=1}^k \frac1l$ and using $\lim_{n\to 0} \frac{a-ar^{n+1}}{1-r}$ I can use the case analysis to find for which $r \in \Bbb R$ the series converges.
Case 1: If $|r| > 1$, then the term $r^{n+1}$ grows rapidly and the whole term goes towards  infinity and diverges.
Case 2: If $|r| = 1$, then the term becomes undefined.
Case 3: If $ 0 < |r| < 1$, then the term $r^{n+1}$ gets smaller and smaller and loses its affect on the whole term and the series converges towards $\frac{a}{1-r}$.
By problem b because of $k^2$ and the rapid growth of $2^{k^2}$ towards infinity any value of $r$ either pushes the series even more towards infinity (for $|r| > 1$) or at most slows down the divergence (for $|r| < 1$).
But my question is if this approach is correct and if yes, is it acceptable as a proof or do I have to use criteria like Root test or Leibniz test or etc. to show the convergence?
Thank you for your help.
Comment: You say $\sum_{k=0}^n a r^k = \frac{a-ar^{n+1}}{1-r}$.  But that doesn't generally work if the thing you're calling "$a$" depends on $k$. ${}\qquad{}$
Comment: You mean that $\sum_{l=1}^k \frac1l$ cannot be defined as $a$ because it is not a constant?
Comment: One could write $a=\text{that number}$, but then one could not validly apply that identity.  Thus it would be imprudent to use a notation that doesn't make it clear that the thing you're defining depends on $k$. ${}\qquad{}$
Answer: The first series yields to a ratio test:
$$
\lim_{k\to\infty}\frac{\sum_{\ell=1}^{k+1} \frac1\ell |r^{k+1}|}{\sum_{\ell=1}^k \frac1\ell |r^k|} = |r|\lim_{k\to\infty} \left( 1 + \frac{1/(k+1)}{\sum_{\ell=1}^k 1/\ell} \right)
$$
In the fraction $\dfrac{1/(k+1)}{\sum_{\ell=1}^k 1/\ell}$, the numerator approaches $0$ while the denominator grows.  Therefore, that fraction approaches $0$.
Hence the series converges if $-1<r<1$ and diverges if $|r|>1$.
What happens when $r=1$ or $r=-1$ could bear closer examination.
Now let's look at the second series:
$$
\lim_{k\to\infty}\frac{2^{(k+1)^2} |r|^{k+1}}{2^{k^2} |r|^k} = \lim_{k\to\infty} 2^{2k+1}|r| =\infty\text{ unless }r=0.
$$
So the series diverges unless $r=0$.


What kind of operation is allowed to be executed during onbeforeunload

Question: I decide to execute some code before popping up the confirm exit dialog box.
However, the behavior of onbeforeunload is quite puzzling. Certain operations are allowed, and certain operations aren't allowed.
function dummy() {
    alert('123');            // Not allowed. Blocked alert('123') during beforeunload
    console.log('123');      // Allowed.
    navigator.vibrate(1000); // Doesn't work in my chrome mobile.
    return "do you exit";
}

window.onbeforeunload = function() {
    return dummy();
}

I was wondering, why there's such behavior? Is there any documentation on this behavior?
Is there any workaround to overcome the restriction post on navigator.vibrate before returning popup string?
Comment: The "why" is pretty straight forward. Why would anybody want websites to aggressively prevent you from leaving them?
Answer: I think you'll find that the Mozilla Development Network does have documentation on this sort of thing.
To quote:

Since 25 May 2011, the HTML5 specification states that calls to window.alert(), window.confirm(), and window.prompt() methods may be ignored during this event.

And as for the why, meager has nicely pointed out in the comments that websites should not be allowed to aggressively prevent you from leaving the page.
navigator.vibrate is a time-based function. "Vibrate for x milliseconds". When the window is unloading, it does not want the user to have to wait for anything. You might be able to find a workaround for this, but I suspect that it will be patched as this is not desirable functionality.


Java Script not working in Google App Script HTML

Question: I am using Google App Script to build a little web app. I'm just at the very start and the page charges well on both Chrome and Firefox but I can't get anything to happen on click of the log in button. I don't get any log to the logger, neither do I get the alert to pop up. Does anyone have any idea why?
This it the HTML of HTML_StockManagementLogInPage:
  <!DOCTYPE html>

      <base target = "_top">

      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

      <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

      <link type="text/css" rel="stylesheet" href="css/materialize.min.css"  media="screen,projection"/>

      <meta name="viewport" content="width=device-width, initial-scale=1.0"/>

 Stock Management Log In

              <input id="first_name" type="text" class="validate">
              First Name

              <input id="last_name" type="text" class="validate">
              Last Name

           <button class="btn waves-effect waves-light" type="submit" id="LogInButton">Log In
             send
           </button>

And this is the code:

function doGet() {

  return HtmlService.createTemplateFromFile("HTML_StockManagementLogInPage").evaluate();
  //return HtmlService.createHtmlOutputFromFile("HTML_StockManagementInterfacePage");

}

function InsertHTMLScript(HTMLFilename) {
  return HtmlService.createHtmlOutputFromFile(HTMLFilename).getContent();
}

function UserLoggedIn(UserName){

  logger.log(UserName.FirstName + " clicked the button");
  return HtmlService.createTemplateFromFile("HTML_StockManagementMenu").evaluate();
}

And just in case, this is the HTML for HTML_StockManagementMenu, just displaying a title for now:
  <!DOCTYPE html>

      <base target = "_top">

      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

      <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

      <link type="text/css" rel="stylesheet" href="css/materialize.min.css"  media="screen,projection"/>

      <meta name="viewport" content="width=device-width, initial-scale=1.0"/>

 Stock Management Menu

I tried earlier to break down my code with the fuction  InsertHTMLScript that I have written in the code, inserting <?!= InsertHTMLScript("HTML_CSS"); ?> into the HTML code and it didn't work either. I don't known if it's related but I just resolve not to use it to make things easier.
Thank you in advance
Comment: I don't think you want to run the `click addEventListener` until after the DOM has loaded. You could avoid the problem by using `onClick="function();"` I realize is kind of old school but it's a lot easier.
Comment: Your function returns HTML. Your client-side HTML never uses the return value of the asynchronous call to the server. You need to update (rewrite) the entire DOM in a success handler.
Answer: You misspelled getElementById
You have:
document.getElementByID('LogInButton');

It should be:
document.getElementById('LogInButton');

Here is the official docs: https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById


How to pass/extract text from span?

Question: How to pass value of span to callback on click ?
Test

function testFunction(e) {
    alert(JSON.stringify(e));
}

But I always get in alert
{}

How to pass/extract text from span ?
I need to have onClick inside tag, not to attach on another way.
Answer: You are passing the element to the function correctly.
It's when you use JSON.stringify that you don't get any result. The element only has members defined in its prototype, it doesn't have any members added to the object instance itself. The stringify function only includes members of the object instance, not from its prototype, so you get the same as if you called it on an empty object.
If you get some property from the element and show that, you see that you actually have a reference to it:
function testFunction(e) {
  alert(e.tagName); // shows "SPAN"
}

or:
function testFunction(e) {
  alert(e.innerHTML); // shows "Test"
}

Demo: http://jsfiddle.net/F3S4T/
Answer: You could do something like:
Test

function testFunction(elem) {
    alert(elem.innerHTML);
}

Answer: To get the content inside the span tags (or any HTML tags) you can use the innerHTML property of the element. E.g,
function testFunction(e) {
    alert(e.innerHTML);
}

How to pass/extract text from span?


Android execution error in AVD

Question: an anyone look at this simple code (?) and tell me what's wrong please?
I'm a complete beginner to android development and I don't understand why my application doesn't even start. I get an unexpected error.. : (
Here it is:
package applicationTest.ppr.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MainClass extends Activity {
    /** Called when the activity is first created. */

    /*Global vars*/
    public static LinearLayout lila;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lila = (LinearLayout) findViewById(R.id.lilay);
        setContentView(lila);
    }

    public void Shortoast(){new Game(this);}

    public static LinearLayout returnLayout(){return lila;}

}

The program doesn't even launch, and I think it might have something to do with how I handle the LinearLayout and setContentView();
anyway thanks very much in advance!
Comment: Do you have layout xml file which defines your R.id.lilay?  Try to use:  setContentView(R.Layout.yourlayout)
Comment: Use `adb logcat`, DDMS, or the DDMS perspective in Eclipse to examine LogCat and look at the stack trace associated with your error.
Comment: yes, i have it in my main.xml, although the main reason I tried this approach was because i want to update some text to the screen from another class. That's why I created the returnLayout method. so that I could do something like MainClass.returnLayout().setView(texView)
Answer: Suggestion: keep it as simple as possible until you fix the issue!
Then you can concentrate on your business logic.
package applicationTest.ppr.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;

public class MainClass extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.id.lilay);
    }

}

Also, is your main activity mapped in the Android manifest?
<application android:icon="@drawable/icon" android:label="@string/app_name">

    <activity android:name="MainClass"
              android:label="@string/app_name">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

</application>

</activity>

--EDIT--
Do you have the lilay.xml file in your res/layout folder?
Comment: lilay is my linearlayout ID, if it was an xml file shouldn't it be R.layout.NAME.xml?
Comment: Well.. i just figured out that I'm not using linearlayout correctly...
my goal is to have some text on screen. and also a button. when the butotn is pressed, that text is altered, without creating a new object...

Android execution error in AVD


What are some methods for generating simulated time series data for use in modeling?

Question: I have a data set which consists of 50 observed years for which I have date and inflow values between a river and a reservoir. The data is formatted as follows:
<code>Day     Inflows
123      4356.0
</code>
I have 50 years of observed data which includes daily values for each day. The plotted data looks as follows:

Ignoring the small outlying patches of green for now, I'm looking for a method of simulating the green portion of the curve (which represents the spring runoff period) so that I can do a series of model runs with simulated spring inflows.
I'm hoping that someone might be able to point me towards a 'standard' method for approaching this kind of problem. There is extensive variation between the years in the data set (drought years show very little change between the blue and green portions of the year). What are some methods for approaching generating data like this?
Let me know if there is any other information that I can provide. I'm quite new to this kind of problem so I'm unsure what would be most useful.
Answer: One possibility is to fit a time series model to the data you are interested in. Then you can resample the residuals from the fitted model and use them to simulate the data. For example, you can fit an ARIMA model, resample the residuals and then generate new data from the fitted ARIMA model. Instead of generating the innovations from a Gaussian distribution you would use the resampled residuals.
You can find further details searching in the literature about bootstrapping time series. See for example this post and the references mentioned there.
You should take care of events that you mention such as drought years or other particular events. Those events may be exceptional for a particular year or may be cyclical patterns observed in the data. You should choose an appropriate intervention variable to capture these effects in the model. Alternatively, you may fit a model for each subsample where the data remain relatively stable with no breaks or changes in the variance.
Answer: I suggest you first determine that you wanna predict for one day, one month or one year, next think about situation that effect in your goal variable, and if you could use variable from external database like temperature, and as our friend says use time-series models. I think preparation data is more important modeling. don't forget that do smoothing on your data.


How do I speed up this for loop?

Question: I am having trouble speeding up the following code that I modified from a Maxwell et al (2018) paper. https://www.pnas.org/content/115/19/4891.short
ttrees.cal3.trees <- list()
n <- 1000
for (i in 1:n){
ttrees.cal3.trees[[i]] <- rtree(10)
ttrees.cal3.trees[[i]]$edge.length <- rep(0, 11)
while (min(ttrees.cal3.trees[[i]]$edge.length) == 0) {
  ttrees.cal3.trees[[i]] <- cal3TimePaleoPhy(tree = tree3, timeData

= ranges, brRate = 1.775051,
    extRate = 1.775051, sampRate = 0.023, ntrees = 1, root.max = 1)
    }

}

,FAD,LAD
H_neanderthalenis,0.25,0.04
H_heidelbergensis,0.6,0.1
H_sapiens,0.335,0
H_antecessor,1,0.396
H_naledi_195,0.335,0.236
Asian_H_erectus_153,1.9,0.03
African_H_erectus_DAN5_P1,1.6,1.5
African_H_erectus,1.9,0.03
Georgian_H_erectus,1.8,1.7
H_rudolfensis_97,2.4,1.6
H_habilis_106,2.4,1.8
Au_sediba_93,1.98,1.97
H_floresiensis_LB1,0.74,0.17
Au_africanus_59,3,2.4
P_aethiopicus_67,2.5,2.3
P_bosei_77,2.3,1.4
P_robustus_89,2,1.5
K_platyops,3.54,3
Au_ghari_63,2.5,2.45
Au_afarensis,3.7,3
Au_afarensis_36,3.7,3
Au_afarensis_37,3.7,3
Au_afarensis_38,3.7,3
Au_anamensis,4.2,3.9
Ar_ramidus,4.5,4.3
S_tchadensis,7,7
P_troglodytes_4,7.3,0
P_troglodytes_5,7.3,0
P_troglodytes_6,7.3,0
G_gorilla,8,0

#NEXUS

BEGIN TREES;
    TRANSLATE
[0]     1   'H_neanderthalenis',
[1]     2   'H_heidelbergensis',
[2]     3   'H_sapiens',
[3]     4   'H_antecessor',
[4]     5   'H_naledi_195',
[5]     6   'Asian_H_erectus_153',
[6]     7   'African_H_erectus_DAN5_P1',
[7]     8   'African_H_erectus',
[8]     9   'Georgian_H_erectus',
[9]     10  'H_rudolfensis_97',
[10]    11  'H_habilis_106',
[11]    12  'Au_sediba_93',
[12]    13  'H_floresiensis_LB1',
[13]    14  'Au_africanus_59',
[14]    15  'P_aethiopicus_67',
[15]    16  'P_robustus_89',
[16]    17  'P_bosei_77',
[17]    18  'K_platyops',
[18]    19  'Au_ghari_63',
[19]    20  'Au_afarensis',
[20]    21  'Au_afarensis_36',
[21]    22  'Au_afarensis_37',
[22]    23  'Au_afarensis_38',
[23]    24  'Au_anamensis',
[24]    25  'Ar_ramidus',
[25]    26  'S_tchadensis',
[26]    27  'P_troglodytes_4',
[27]    28  'P_troglodytes_5',
[28]    29  'P_troglodytes_6',
[29]    30  'G_gorilla'
    ;

TREE = (((((((((((((((((1,2),(3)),4),5),(13,6)),7,8),9),10),(11,12)),(14,(15,(17,16)))),
18),(19,(22,21,23,20))),24),25),26),(28,29,27)),30);
END;

tree3<-read.nexus("third-app.nex")
ranges<-read.csv("time.csv", header=T, row.names=1)
ranges <- as.data.frame(ranges, row.names = row.names(ranges))

I am unsure of how to change the methods to get the code to work using the apply family. Any assistance would be much appreciated.
Answer: You could rewrite your code as an lapply as follows:
ttrees.cal3.trees <- lapply(seq(1000), function(i) {
  tmp <- rtree(10)
  tmp$edge.length <- rep(0, 11)
  while (min(tmp$edge.length) == 0) {
    tmp <- cal3TimePaleoPhy(tree = tree3,
                            timeData = ranges,
                            brRate = 1.775051,
                            extRate = 1.775051,
                            sampRate = 0.023,
                            ntrees = 1,
                            root.max = 1)
  }
  return(tmp)
})

However, I doubt this will speed up the calculation much. It is not iterating through a list that is the resource bottleneck here, but the repeated calls to cal3TimePaleoPhy until a random sample is drawn with no zero-length edges. Without having the data you are using, it's not possible to test other ways in which this might be achieved.
Comment: Hi Allan, I have copied that data and the method I used to read it.

How do I speed up this for loop?


How to cache an image from firebase storage in Flutter

Question: I wrote a flutter app that trying to cache an image from firebase storage using cached_network_image when the app is staring, but I'm keep getting the same error when I try to wait until the image is done loading and I can use her. if someone know what is my problem please let me know.
this is the mention error:

The argument type 'Future<'dynamic'>' can't be assigned to the parameter type 'Iterable<Future<'dynamic'>>'.

this is my code that supposed to cache the image:
/// imports

class ClientHomePage extends StatefulWidget {
  const ClientHomePage({Key? key, this.uid}) : super(key: key);
  final uid;
  @override
  _ClientHomePageState createState() => _ClientHomePageState(uid);
}

class _ClientHomePageState extends State<ClientHomePage> {
  final FirebaseAuth _auth = FirebaseAuth.instance;
  late final uid;
  int _currentIndex = 0;
  bool isAppBar = true;
  _ClientHomePageState(this.uid);

  late List<Widget> pages;

  @override
  void initState() {
    super.initState();
    pages = <Widget>[
      const HomePageC(),
      const SearchPageC(),
      AccountPageC(
        uid: uid,
      ),
    ];
    WidgetsBinding.instance.addPostFrameCallback((_) => cachedProfilePic(uid));
  }

  Future cachedProfilePic(uid) async {
    String url = await getStorageImage(uid, "/profile-pic.png");
    await Future.wait(cacheImage(context, url)); // here I am getting the error
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
       // all of my code...
    );
  }
}

this is the helping functions that the other code is using:
// imports...

Future<String> getStorageImage(String uid, String imageName) async {
  String url = await FirebaseStorage.instance
      .ref(uid)
      .child("/" + imageName)
      .getDownloadURL();
  return url;
}

Future cacheImage(BuildContext context, String url) => precacheImage(
      CachedNetworkImageProvider(url),
      context,
    );

Answer: this widget is for fetching, caching, and presenting an image from the network.
CachedNetworkImage(imageUrl: 'your image link here',)
Comment: where do you want me to put it, I don't really understand.
Comment: I'm sorry, you are already using it, I didn't notice, really sorry.
Answer: Flutter Future.wait is used to await and return multiple futures, why not just await the method call?
await cacheImage(context, url);

https://api.flutter.dev/flutter/dart-async/Future/wait.html


Migrating embedded SQL to LinqToSql vs stored procedure

Question: I am migrating some code from ColdFusion to .NET LinqToSQL. The following query is currently embedded into ColdFusion. Rather than trying to get LinqToSQL to this, I am considering writing a stored procedure. Note: the ## in CF in this context is really a # in SQL. I am not creating a global temp table
<code><cfquery name="qryTrafficDetails">
    DECLARE @startDate date
    DECLARE @endDate date
    SET     @startDate = <cfqueryparam cfsqltype="CF_SQL_date" value="#filter.StartDate#">
    SET     @endDate = <cfqueryparam cfsqltype="CF_SQL_date" value="#filter.endDate#">

    SELECT  CONVERT(date, CreateDate) AS CreateDate, url_vars.value('(search)[1]', 'nvarchar(max)') AS Item, remote_addr
    INTO    ##tempTraffic
    FROM    dbo.Traffic WITH (NOLOCK)

    WHERE   Createdate BETWEEN @startDate AND @endDate
    AND     url_vars.exist('.[search]') = 1

    SELECT Area, AreaSort, Item, CalendarDate, isNull(Visitor, 0) AS Visitor, ISNULL(Hit,0) AS Hit
    FROM    dbo.Calendar LEFT JOIN
        (

        SELECT AreaSort = 10, 'Search' AS Area, FullGrid.Item AS Item, FullGrid.CalendarDate AS CreateDate,

                COUNT(DISTINCT remote_addr) AS visitor,
                COUNT(remote_addr) as hit

        FROM  (
            SELECT DISTINCT CalendarDate, Item
            FROM dbo.Calendar, ##TempTraffic
            WHERE   CalendarDate  BETWEEN @startDate AND @endDate
            ) FullGrid

        LEFT JOIN ##TempTraffic AS DataItem
            ON FullGrid.CalendarDate = CreateDate
            AND FullGrid.Item = DataItem.Item

        GROUP BY FullGrid.Item, FullGrid.CalendarDate

        ) AS SummaryDetail

    ON  CalendarDate = SummaryDetail.CreateDate

    WHERE   CalendarDate  BETWEEN @startDate AND @endDate
    AND AreaSort IS NOT NULL

    ORDER BY AreaSort, Item, CalendarDate
    </cfquery>

    <cfreturn qryTrafficDetails>
</code>
Some of the issues I am concerned with are:

How to deal with temp tables
Does LinqToSql cover everything?
Is this an example of where LinqToSql is not appropriate?
Performance
Answer: I'm not completely sure about the intentions and motivations of your sql code but seems to be a summary report. In this case probably linq to sql is not the ideal solution.
I don't think that you can create temp table from linq 2 sql. You can create sub query or maybe you can use linq 2 object to join the results of previously executed query.
But my suggestion is to check if there isn't a better way to query your data using a more concise query that can be executed also on linq 2 sql.
If you really need this kind of query probably it's better to put it in a stored procedure.


Contact form block with data from the page the blocks featured on

Question: So I have a content type "Real Estate." On the view for a "Real Estate" node, I want to have a contact block in the right side bar that has a feild for the node id of this specific "Real Estate" node.
An example of the feature I'm trying to make can be seen here
This problem has two parts which I'm not sure how to do. The first is that I don't know how to make a block for a contact form. I know drupal has a default Contact module but I'm not sure how to turn this into a block or even if that is the best way to go (maybe WebForms module can help?) Second is that I'm not sure how to pre-load the node id into the form. I'm sure that this would require a hook of some kind but seeing as I don't know how to make the contact form block, I don't know what hook to use.
Any help would be much appreciated. Thanks!
Answer: If you don't want to write custom theming and you want less coding, you need to create a custom page with 2 columns using panels module.

Install the panels and ctools modules if you don't have.
Enable Chaos tools, Page manager and Panels.
Create a custom page at admin/structure/pages/add with the path "estate/%node" which will be a new mirror page of the existing path "node/%node".
Assign Node: ID context to the argument %node.
Choose two column layout.
Add Existing node to the left column. To use a NID from the URL, use %1 in the title box.
For the right column, add the contact form from Widgets.

To get node information in the contact form, you need to write a hook hook_form_FORM_ID_alter in your custom module using the argument NID.
You can study custom moduling here.
Here is the example code snippet.
<code><?php
/**
 * Implementation of hook_form_alter().
 * @see http://api.drupal.org/api/function/hook_form_alter
 */
function yourmodule_form_contact_mail_page_alter(&$form, &$form_state, $form_id) {

    if(arg(0) == 'estate' && is_numeric(arg(1)){
        $node = node_load(arg(1));
    }

}
?>
</code>
You can also see how can you customize the contact form here:
http://www.metaltoad.com/blog/how-add-field-drupal-contact-form
http://www.isegura.es/blog/modifying-contact-form-drupal-how-add-field
http://www.pensamientosdivergentes.net/blog/modifying-contact-form-drupal-how-add-field
The drawback of this solution is you have to use the new path "estate/%node" to the node view page instead of using the default existing path "node/%node".
Any better answer is welcome.


keep clients notified of List via datagram/multicast

Question: I'm building a client-server application.
The server keeps a list of objects:
 LinkedList<MyObject> objects;

Every time an object is added or deleted, the clients should know this.
They joined the multicast group already.
What is the best way to keep them notified of changes?
Should I make a class that extends LinkedList and Implement it with Serializable?
Comment: I would think it would be much easier and reliable to code your change notifications to some event driven protocol (i.e. higher level).  Then simply configure the implementation of that protocol to use an appropriate network protocol, whether that is multicast or multiple TCP connections.
Answer: Multi-casting the updated list each time it changes is certainly simple, but there may be issues.

If the list gets large or the updates are frequent, scalability may be a concern.  Consider sending the changes to the list as "deltas" rather than sending the entire list each time.
Serialization using ObjectOutputStream can be expensive, though you can improve the performance by writing custom readObject and writeObject methods.  (Serialization of lists is already optimized ...)
You don't need to subclass LinkedList to do this.  It is already Serializable.
You most likely need to synchronize the list structure to avoid race conditions and mayhem, but that will create a potential bottleneck.  Consider replacing the List with a concurrent implementation of Queue or Deque.
Comment: good collection of issues.  one more you might want to add is that multicast doesn't preserve order for you, so you could get updates out of order.
Comment: @jtahlborn - Good point, though there is a simple work-around for that problem.  (A bigger concern would be loss of messages, especially if deltas are sent instead of full lists.)
Comment: yes, i was thinking of both loss of messages as well as out of order.  loss of messages affects both deltas and full updates (although a future full update can make up for a previous lost update).


Changing text size in xcode version 6.3

Question: It is very common knowing changing font/text in xcode. I found it difficult for me to do that in version 6.3. Any help please?
Comment: What are you trying to do? And please update to Xcode 6.4.
Answer: If you trying to say about Xcode Editor then here it is .
As mention by drdiv in this post How to Increase Font Size in the xcode editor?

Easisest solution:

Close any open projects.
Xcode > Preferences > Font & Colors
Make sure to press CMD+A to select all possible text types. Then change the font size from the picker.

Note:- Of if you want to change the fornt and size of any other like
  UICONTROL or etc then please explain your question in detail.

Changing text size in xcode version 6.3


Showing dimension of a subspace

Question: Let $W$ is a set of functions with degree at most 1, and so $$W_1=\{a_0 +a_1 \cdot x | a_0,a_1 \in \mathbb{R} \}$$
I know that the basis of this is the set $B_1 = \{1,x\}$. But I am having trouble showing linearly independance. I know we choose scalers $c_1, c_2$ such that $c_1(1) + c_2(x) = 0$ but I dont know where to go from here. Also I know the dimension is 2 but why is this? Is this because the number of elements in the basis is 2 or because dim(W) = n+1 for polynomials.
Comment: "A set of functions", or "a set of **polynomials with real coefficients**"? (Not the same thing!)
Comment: The dimension of a vector space is the number of vectors in a basis for that space. The reason that $\dim W_n=n+1$ is that $\{1,x,x^2,\dots,x^n\}$ is a basis for $W_n$ that has $n+1$ members.
Answer: $c_1(1) + c_2(x) = 0$ is an equality of functions. So, evaluate both sides at $x=0$ to get $c_1=0$ and then at $x=1$ to get $c_2=0$.
Comment: @Tyler: They span $W$ because $W$ is defined as the span of $1$ and $x$ (your definition).
Comment: So that shows in linearly independant. How would I show span(1, x) = W?
Comment: Hi, can you show how evaluating them is the right way to do it. I thought we are supposed to show linear independance for ALL x. so why would concrete values for x
Comment: @Tyler, there are only 2 unknowns here: $c_1$ and $c_2$. So you only need 2 equations to find them. Actually, any two different values if $x$ will do.
Answer: The expression $c_1(1) + c_2(x) = 0$ is an equality of two polynomials. But two polynomials are equal if and only if they are identical; that is,
$$a_0 + a_1x + \cdots + a_nx^n = b_0+b_1x + \cdots b_nx^n$$
if and only if $a_0=b_0$, $a_1=b_1,\ldots,a_n=b_n$.
Here you have $c_1(1) + c_2(x) = 0 = 0(1) + 0(x)$, so $c_1=c_2=0$.
To prove they span $W$, let $a+bx$ be an arbitrary element of $W$. Then $a+bx = a(1)+b(x) \in \mathrm{span}(1,x)$, so $W\subseteq \mathrm{span}(1,x)$. Since each of $1$ and $x$ lie in $W$, $\mathrm{span}(1,x)\subseteq W$, giving equality.

Showing dimension of a subspace


Applying Recency In Logistic Regression

Question: Are there any statistical concepts or theories on how to effectively measure recency to where recent events are given more weight than older ones. I'm creating a logistic regression model and would like to apply an adjustment to various factors based on the events recency.
...or, is it purely up to me to come up with an arbitrary formula?
For example: One of my projects is predicting via logistic regression the performance of pro golfers in upcoming tournaments. Their recent form (how they played last week) is generally more important than how they played six months ago. Are there any specific techniques/approaches that tap into this concept?
Comment: It will help people answer your question if you could provide more detail on why you want to do this.
Comment: I provided an example from one of my projects.
Answer: It doesn't necessarily have to be arbitrary; you could, for example, assume exponential discounting and look for the coefficient that has the best predictive value.
That is, at time $t$ you might say that the weight, $w_{t-k} \propto p^k$ or equivalently $w_{t-k} \propto \exp{(-\alpha k)}$
(where $0<p<1$ and $\alpha>0$).
In the above, $p$ or $\alpha$ are free parameters, but rather than choose them arbitrarily, you can compare their predictive performance across different values of $p$ (or equivalently, across different $\alpha$), perhaps via comparing sums of squares of one step ahead prediction errors, or whatever other criterion (loss function) you regard as most valuable/interesting/useful.
Alternatively you could use a similar approach but where you apply some other form of discounting, such as hyperbolic discounting.
Comment: I've been out of town but your answer was helpful. Thank you. Doing some digging now...
Comment: See also '[exponential smoothing](http://en.wikipedia.org/wiki/Exponential_smoothing)'.

Applying Recency In Logistic Regression


Must one be born with a sense of rhythm to learn to play a drum set with both feet and both hands?

Question: I am a semi pro musician (only because folks are generous enough to pay me to perform on occasion - not because of great talent) and can play guitar well enough to accompany my singing and play a little on keyboard (piano).
I have always been fascinated by drums - particularly the drum set or kit consisting of a kick drum and high hat and various other drums played with sticks or brushes.
If I ever learn to play set, it will be to add live drum tracks to recordings of my original songs that I produce in my home "recording studio".  I think live drums gives the song a more organic natural "live" feel than digitally synthesized drum tracks.
I have tried playing full kits before.  I don't have much problem with two distinct beats playing with just the sticks (or hands on a djembe or cajone' or bongos), but when I try to add the kick drum and high hat into the mix, something goes awry in my brain and I can't seem to get both hands and both feet in an any semblance of synchronization or coordination.
My daughter (who was a cheerleader at the time) bought herself a drum kit and could instantly play in perfect coordination using both hands and both feet with no lessons and very little practice.  After she showed me how "easy it was", she disassembled the kit and sold it on e-bay and despite the fact that she quickly became a proficient drummer, she has never played drums again.
So my question is, are some people just born with an innate natural ability to keep a rhythm with both hands and both feet simultaneously, and therefore possess the ability to play a full drum kit?  If I don't have a God given sense of rhythm, can I develop it with practice and lessons - to the point where I could play full kit effectively, or would this take tons of time and energy and effort (or even be impossible given my apparent rhythmic challenges)?
For whatever it's worth, playing guitar did not exactly come naturally or easy for me.  I had to work at it much harder than most of my guitar playing friends admit to.
Comment: Nurtured by Love is the book I was thinking of.
Comment: George Harrison was not naturally talented like, say, Paul Macartney, but he did okay. Some people need extra practice and willpower.
Comment: Nothing involving music is god-given. It is all a skill that anyone can learn. Music is a trade. If you want to hear the founder of a international music organisation expose this view then you can read the book nurturing with love by Dr Suzuki.
Answer: Yes, you absolutely can.
As with so many things, this is not a black-or-white thing. There's not some kind of gene that says you'll either be a master at playing the drums, or totally suck at it. Many things come into play here:

The ability to concentrate.
The ability to control your motions accurately.
Eye-hand coordination.
Stamina.
Willpower.

These are just a few -- I'm just listing these off the top of my head. As you know, these are qualities that everybody is born with, with varying skill in each of them. Now, when you speak of someone with "God given sense of rhythm", you're actually thinking of somebody who has a God given sense of all these things listed above. Almost nobody has a God given talent for all of these things though, which is why the last bullet point is so important. Willpower -- more accurately, dedication during practice -- can take you a very long way. Even the most talented musicians spend hours upon hours practicing, because that's simply the best way to become good at something. Even if you are born with the talent to play drums, you'll still need to polish this skill by practicing enough.
Long story short: many talents contribute to your ability to play the drums, but in the end, practice will always be necessary. However, being born with these talents will definitely make life easier for you, and lacking these talents probably means that there's a certain "limit" of how good a drummer you can become.
Comment: I played the piano and violin for 10 years before picking up the drums.  It took about 8 hours of practice before using three limbs on the drums felt 'natural', never mind actually playing a beat.  Practice will overcome any initial problems for sure.
Answer: I would separate the sense of rhythm from the distinct skill of limb-independence.
What you are actually trying to achieve right now isn't timing, or rhythm, it's the separation of one task [hands] from another [feet]
With no sense of rhythm whatsoever, you can practise patting your head & rubbing your stomach… then swap hands!
Once you can do that at will, repeatedly, without ever getting it wrong, you've developed one aspect of limb-independence - though that in itself won't develop any sense of rhythm.
To apply that to a drum kit, the first & simplest thing to do is to tap out a simple 4 on the hi-hats. Once that is steady, add in the snare, on 2 & 4 - just like you've heard on a million records.
The first thing beginners do is 'forget' to play the hat each time they want to hit the snare. There is a tendency to instead of hit both together, that they will instead lift the right [hat] hand instead of drop it. There is nothing you can really do to fix that except practise until you can do it.
That's an exercise you can do on a table top, car dashboard - anywhere that people won't actually throw things at you while you practise ;)
You don't need sticks to learn that bit. Sometimes not having the sticks makes you learn the independence without an added distraction.
Don't even think about adding a foot until you can do that.
Comment: ahh, that wasn't clear from the question, sorry, you said 'adding kick & hat'. The issue, though, is the same thing - you just already did 101 & are now on the next step. If you can already do pat/rub/swap & hat/snare [& other existing instrumental skills], I would assume you have advanced hands & beginner feet. Treat them like beginners. Slowly does it. They've never tried this before.
Comment: Thanks.  As I said in my question, I can do a 4 on one hand and a 2 & 4 beat on the other and keep that going.  No problems at all with the rub head pat stomach or vice-versa in either direction.  Where my problem kicks in (excuse the pun) is when I try to add feet into the mix.
Answer: I am 63 and have been playing drums for (Gulp!) 54 years, starting with snare drum in the school band. I don't feel that I had the natural sense of rhythm, I really had to work at it. I really had to practice a lot to not be feeble. But now, years later, I'm a very good drummer with a wide range of music I can and like to play. I have seen some people really almost fall into playimng drums naturally, but that was not my experience.
I can proficiently play music of Yes, Genesis, ELP and lots of aggressive and complicated drum parts. It makes me smile when I play along with a complicated song and nail it. (especially at 63!) Big Smiles!
My suggestion, Practice the thing you suck at! Really, it's very rewarding. I used to avoid things I wasn't good at until I read an article to the contrary. It's good for the soul to concur things you would typically avoid. And that's another father in your cap.
Answer: No, you don't need to be born with a special talent (although it wouldn't hurt.)

...when I try to add the kick drum and high hat into the mix, something goes awry in my brain...

In this case, the thing to do is the same as any other training scenario: practice to improve weak points. Most people don't use their feet for more than walking. Coordinating them on a drum kit will probably feel weird to most. I imagine your cheerleading daughter took to the drum kit, because she had dance related training in cheerleading. She had to learn foot and leg movements that many people don't.
Try just playing with your feet, hi-hat and bass, simple patterns. As you develop the feel, you can try to keep a steady beat on the hi-hat, not with the stick, but just the foot pedal, then use the free stick to do something else like ride the floor tom or hit cymbal/tom accents. Basically shift the focus to your feet at the level you can manage and build up gradually.

...to the point where I could play full kit effectively...

Depends what "effectively" means, but considering comments from gigging musicians, just keeping a steady tempo and remembering rehearsed tempos is a much appreciated skill. Who are you trying to effect? If you're just playing for your own enjoyment, I think you can do it with modest but focused attention. Maybe you will only play a few basic beats and hit a few fills, but IMO that is effective.
If you don't already know the concept of composite rhythm, look into it. It's when multiple separate rhythms get combined into one overall (composite) rhythm. For example if you play down beat 1 and 3 on the bass, down beats 2 and 4 on snare, and all 4 up beats on a cymbal, the composite rhythm is steady eight notes. When a pattern is tricky you can sometimes get the feel of it by thinking of it as a single composite rhythm distributed around the hands and feet.
Comment: I remember reading some research (I already forgot the source) that concluded that most drummers actually think more in single composite rhythm than independent arms and legs.
Comment: Thank you for your insight.  Plus one for the "composite rhythm idea".
Comment: Sure thing! FWIW, I don't have particularly good rhythm. But the last few years, when the family would get together at my brother's house, I would try playing the simplest backbeat on his drum set. The point there being it was _small doses_ of practice. Eventually I got it. Recently I got my own entry level kit so now play it periodically to clear my mind. It is do-able, and fun.


Regression using circular variable (hour from 0~23) as predictor

Question: My question originally arises from reading this post
Use of circular predictors in linear regression.
Right now, I'm trying construct linear regression using
"Bike Sharing dataset" from
https://archive.ics.uci.edu/ml/datasets/bike+sharing+dataset
which basically tries to regression bike rental count on different variables
One of the predictor that I have question is on using "Hour" of when the rental occurred, which takes value from 0 to 23.
The original post suggests transforming the circular data (time of day) using sine function to maintain the circular characteristic.
I was trying to apply to same methodology to my situation to transform the Hour variable. However,transforming 0~23 using sin(π hour/180) lets 00:00 and 12:00 to have 0. But I think people will certainly display different behavior when renting bike at midnight(00:00) and afternoon(12:00)
In this case, is it better to just use 23 dummy variables to account for hour
or am I misunderstanding the concept of circular regression?
Comment: I doubt bike sharing data of any sort are well represented by a simple sine wave.  For more flexibility consider using a [circular spline](https://stats.stackexchange.com/search?q=circular+spline) instead.
Answer: Circular regression most often would refer to regression with a circular outcome.
In this case, we have linear regression with a circular predictor. In that case, we would add both the sine and the cosine of the angle to the regression, so that we predict the outcome as $\hat{y} = \beta_1\cos(\pi * \text{hour} / 12) +  \beta_2\sin(\pi * \text{hour} / 12).$ Adding both the sine and cosine naturally resolves the issue you mention. Note that here, different than you, I've assumed that you represent hour in hours rather than degrees.
For a more elaborate answer on how to do this and what it means, please see the answer to this SO question.
Answer: You want to map the interval $(0,24)$ to the interval $(0,2\pi)$ - a full cycle -; the function to do so is
$$2\pi  \frac{\mathrm{hour}}{24}$$
You then need two terms in your linear model (recall that an equivalent non-linear parametrization uses phase & amplitude):
$$\beta_1  \sin\left(2\pi\frac{\mathrm{hour}}{24}\right) + \beta_2 \cos\left(2\pi \frac{\mathrm{hour}}{24}\right)$$
Noon & midnight aren't constrained to result in equal predictor values because the phase is estimated from your data. Noon might be at the peak and midnight at the trough of the wave.
And you can continue with harmonics in an analogous way to higher-order polynomial terms: $$\ldots +\beta_3  \sin\left(2\times 2\pi\frac{\mathrm{hour}}{24}\right) + \beta_4 \cos\left(2\times 2\pi \frac{\mathrm{hour}}{24}\right)+\ldots$$
Comment: harmonic regression
Comment: @skan: They'll typically be correlated in observational data, but not especially so. You can orthogonalize them if you want.
Comment: Won't these two terms be correlated or collinear and then harm the model?


Keep menu open for custom content entities

Question: I am writing modules that define content entities in Drupal 8. The Drupal console creates the menu items for the list builder. I would like the menu to stay open (having the menu-item--active-trail class) when I access the view or edit form page of, say, the content entity with id 4.
The breadcrumb navigation works perfectly out of the box, but the menu is completely closed for the content entity with id 4.
I am ok with writing a Menu Plugin or using a contributed module.
Answer: My solution turned out to be the contributed module Menu Trail By Path: because I can fine-tune the actual URLs in all of my entities' YAML files, I can define the hierarchy easily.


Find columns with non zero values in Hive

Question: Let's say you have a database with n columns, from col1 to coln, where n is large.
Would it be possible to find all rows such that at least one column from colk to coln has a non zero value (assuming columns are  non-negative numbers, and numbers may be missing)?
Comment: what do you mean, "missing". some of the columns contain nulls?
Answer: You could use Hive's coalesce function (which takes the first non-null value of a series of column inputs) combined with an if statement, something like:
select *
from table
where coalesce(if(col1 > 0, 1, null), if(col2 > 0, 1, null)...) = 1
;

The above will return any row where at least one column specified in the coalesce function returned a value of 1. Let me know if this works for you.
Edit: Another method which is a little cleaner (doesn't require you to list all columns) but less flexible:
select *
from table tb
where sort_array(array(tb.*))[n-1] > 0
;

The above will sort the array ascending so you can check if the largest value in the array is greater than zero and return only those rows.
Comment: I edited my original answer to include a method which wouldn't require you to list all the columns.
Comment: If I have many columns, is it possible to tell the function to try all of them without me listing them all?
Comment: sorry to bug you again. what does [n-1] mean?
Comment: n = the number of columns in your table. The last element of an array of those columns can be accessed by array[n-1] since Hive arrays start at 0.
Answer: why so many complicated queries use this;
INSERT INTO DB.1.table_name_1
SELECT * FROM DB.2_table_name_2
WHERE table_name_1.COL_NAME > 0
INSERT OVERWRITE DB.1.table_name_1
SELECT * FROM DB.2_table_name_2
WHERE table_name_1.COL_NAME > 0


Permit a user to create new accounts

Question: I am looking for a way to allow one type of users to create a second type of users.
Can anyone advise me on how to do this?
I am using the profile module but creating a profile does not create a corresponding user who owns the profile.
Comment: By "type of users" do you mean users with a specific role? Are you interested only to a module, or are you interested also to code you can write to reach that purpose?
Comment: Yes, I meant users with specific roles and I would prefer a module solution, thanks
Answer: Use the Role Delegation module.

This module allows site administrators to grant some roles the
  authority to assign selected roles to users, without them needing the
  administer permissions permission.
For each role, Role Delegation provides a new assign ROLE role
  permission to allow the assignment of that role.
The module also adds an assign all roles permission. Enabling this
  permission for a role is a convenient way to allow the assignment of
  any other role without having to check all the assign ROLE role
  permissions in the Permissions page.
If an administrator has the administer users permission, a role
  assignment widget gets displayed in the account creation or editing
  form, and bulk add/remove role operations become available on the user
  administration page. Otherwise, if s/he has at least the access user
  profiles permission, the module adds its own Roles tab to the user
  profile so that roles can be assigned.
Answer: In Drupal 7, go to admin/people/permissions and way down at the bottom under 'Users,' there is an option for 'Administer users.' Pretty sure it's similar in D6. The pattern would be to create a new group, with a name like 'Officer' and give officers this power.
The super ultra mega downside to this is that any user with this power can delete user 1. So don't do it. :-) I suggest an invitation system instead: http://drupal.org/project/invite.
Comment: Does the Invite module allow to create users with a specific role, where the role is decided from who sends the invite, or where the role is decided upon the role of who sends the invite?
Comment: Not sure about a new role for invited people.

Permit a user to create new accounts


How to configure S3 or DNS to handle incomplete name (sans www) for web site?

Question: I have a set up a bucket called "www.mydomainname.com" to host my website and I have configured the CNAME such that "www.mydomainname.com" points to the my endopint http://www.mydomainname.com.s3-website-us-east-1.amazonaws.com/
It works and when people who type the the full url "www.mydomainname.com" are able to see my index page
But most people are in the habit of typing incoplete domain name -- they just type "mydomainname.com" and their browser fails to find my site.
Is there a way to configure CName or S3 bucket such that typing "mydomainname.com" take them to my s3 website ?
(I am using Networksolutions as my DNS provider).
Answer: You would just add a CNAME for mydomainname.com pointing to www.mydomainname.com.s3-website-us-east-1.amazonaws.com.
Comment: This question has a more comprehensive discussion: http://serverfault.com/questions/62527/how-to-create-a-cname-for-a-domains-root-name


Exponential of matrix if $A^2=A$

Question: So the question is: if $A^2=A$ where A is a matrix find $e^A$. I know I should start with series expansion: $$e^A=1+A+ \frac{A^2}{2!} + \frac{A^3}{3!} +... $$
but I don't know how to proceed.
Thanks!
Comment: Can the downvoters explain something, please?
Comment: @ajotatxe Indeed, there seems to be unnecessary hostility here.
Answer: Since $A^2=A$, then $A^3=AA^2=AA=A^2=A$, and in general it follows that $A^n=A$. Thus
$$e^A=I+A+\frac{A}{2!}+\frac{A}{3!}+\cdots=I+A(1+\tfrac{1}{2!}+\tfrac1{3!}+\cdots)=\boxed{I+(e-1)A}.$$
Comment: Thank you very much!
Answer: By induction, we know $A^n = A$ for all $n \geq 1$. Thus \begin{align*} e^A &= I + A + \frac{A^2}{2!} + \frac{A^3}{3!} + \cdots  \\ &=I+(\sum_{n=0}^{\infty}\frac{1}{n!}-1)A \\ &=I+(e-1)A\end{align*}
Comment: Thank you very much!

Exponential of matrix if $A^2=A$


How to find common data in replicates?

Question: I have three .csv files and each files first column is Gene identification numbers (AGI: my experimental targets). the three files consisted with different experimental out comes. i.e. first file 250 targets, second ~500, and third ~ 2000.
Each file consisted with other information for the identified targets, e.g.

I want to identify common targets (AGIs) in these three .csv files and make a new working table combining other details of the common AGIs.
How can I identify the common targets (AGIs) using R programming?
Comment: Please don't post your data as an screenshot.
Answer: I would do something like this
# Read in CSV files and store in list
csv_filenames <- c("file1.csv", "file2.csv", "file3.csv")
lst <- lapply(csv_filenames, read.csv)

# Return the intersect of AGI entries across all data.frames in the list
ovlp <- Reduce(intersect, lapply(lst, function(x) x$AGI))

ovlp is a vector with AGI IDs that are present in all CSV files; it's then easy to pull out relevant entries from the individual data.frames by matching AGI entries from ovlp with those from the data.frames in the list.

To give a more explicit example, let's generate some fake data
set.seed(2017);
lst <- list(
    data.frame(AGI = letters[1:10], value = runif(10)),
    data.frame(AGI = letters[4:13], value = runif(10)),
    data.frame(AGI = letters[8:17], value = runif(10)));

Then
ovlp <- Reduce(intersect, lapply(lst, function(x) x$AGI));
ovlp;
#[1] "h" "i" "j"

The entries in ovlp are those AGI entries that are present in all three data.frames in lst.

An alternative approach could be to merge data from all data.frames, and then filter rows based on the absence/presence of entries across all data.frames.

Update
In response to your comment, here is something that should get you started.
First off, let's join the data.frames in the list based on the "AGI" values in a merged data.frame called df.
df <- Reduce(function(x, y) dplyr::full_join(x, y, by = "AGI"), lst);

Let's take a look at the merged data.frame
df;
#    AGI    value.x     value.y      value
#1    a 0.92424261          NA         NA
#2    b 0.53717641          NA         NA
#3    c 0.46919565          NA         NA
#4    d 0.28862618 0.674331481         NA
#5    e 0.77008816 0.002020766         NA
#6    f 0.77276871 0.025093514         NA
#7    g 0.03932234 0.432077786         NA
#8    h 0.43490560 0.499391912 0.63411352
#9    i 0.47216639 0.388681932 0.37986744
#10   j 0.27383312 0.395375316 0.94207403
#11   k         NA 0.715707325 0.75499369
#12   l         NA 0.940999879 0.22761184
#13   m         NA 0.827229161 0.91466603
#14   n         NA          NA 0.62044504
#15   o         NA          NA 0.31910458
#16   p         NA          NA 0.07628881
#17   q         NA          NA 0.26083932

We can now select rows that are "complete" (i.e. have values from all three data.frame) and then average values for every AGI.
library(tidyverse);
df[complete.cases(df), ] %>%
    gather(key, value, -AGI) %>%
    group_by(AGI) %>%
    summarise(value.mean = mean(value))
## A tibble: 3 x 2
#  AGI   value.mean
#  <chr>      <dbl>
#1 h          0.523
#2 i          0.414
#3 j          0.537

Comment: You're very welcome @Oncidium; I've included an update to address the problem statement in your comment. The general idea is to merge the data, select "complete" rows (where we have data from all three data sets), and then calculate some summary statistic(s) (here the mean). This should get you started. Good luck!
Comment: Thank you very much. Great help :) Appreciate.
Comment: Thank you very much Dr. Evers,

This is gonna helpful for me.
It's great you are bio-informatician and I can explain well.
These three .csv files are results of three replicates of one experiment. So my aim is filter the common AGIs in three .CSV files and average the ria data for each AGIs.
What my final average O/P should look like is
AGI (express in all 3 reps) and average ria (of three reps)

How to find common data in replicates?


Two parallel planes

Question: When two planes have the same perpendicular vector $\overrightarrow{v}$, then they are parallel, right??

We have that the two planes $Ax+By+Cz+D_1=0$ and $Ax+By+Cz+D_2=0$ are parallel, since they have the same perpendicular vector $\overrightarrow{v}=(A, B, C)$.
The line that contains $\overrightarrow{v}$ intersects the two planes at the points $K, M$.
Then $\overrightarrow{OK}=\lambda \overrightarrow{v}=(\lambda A, \lambda B, \lambda C), \overrightarrow{OM}=\mu \overrightarrow{v}=(\mu A, \mu B, \mu C)$.
Then $\lambda A^2 +\lambda B^2 +\lambda C^2=-D_1 \Rightarrow \lambda=-\frac{D_1}{A^2+B^2+C^2}$
$\mu A^2 +\mu B^2 +\mu C^2=-D_1 \Rightarrow \mu=-\frac{D_2}{A^2+B^2+C^2}$
So, the distance between the two planes is equal to $$||\overrightarrow{KM}||=||(\mu -\lambda ) \overrightarrow{v}||=|\lambda -\mu | ||\overrightarrow{v}||=\frac{|D_1-D_2|}{\sqrt{A^2+B^2+C^2}}$$
$$$$
My questions are the following:

At the sentence " The line that contains $\overrightarrow{v}$ intersects the two planes at the points $K, M$. " what does it mean the line that contains the vector??
Why do we calculate the vectors $\overrightarrow{OK}$ and $\overrightarrow{OM}$ ??
Answer:
By itself "the line that contains the vector $\bf v$" doesn't make a lot of sense.  But from the following text it appears that what the writer means is "the line through the origin in the direction of $\bf v$".
The line segment $KM$ is a perpendicular to both planes, therefore its length is equal to the distance between the planes.  We can find $KM$ as $OM-OK$.  That's why we calculate those vectors.
Comment: I understand!! Thank you so much!!! :-)

Two parallel planes


How to dynamically create a view, and then export to PDF

Question: I'd like to dynamically create a view for each of my content types/items, and then export that View's data to PDF format.
My (Drupal 7) website tracks mileage, gas usage, engine usage, and other items for a fleet of cars. Let's say I want to run a report on Car A. I'd like to have a single view that could pull in only the information for Car A when I click a link on Car A's content page. Then, if I click a link on Car B's page, it would pull in only the information for Car B. The same would be true for Engine A, Engine B, etc. The purpose would be to see a snapshot of the usage of that car, engine, etc.
I use the Entity Reference module to link individual usage content items to each of the "master" content items (cars, engines, etc). The Usage content items have the following fields: the VIN/serial number of the car/engine (which links the usage content item back to the car/engine), miles driven since last visit, gas remaining since last visit, etc. These usage items are the most important thing for this usage report. There will be other fields from the Car, Engine, etc (such as the color of the car, starting mileage, etc) that will appear on the report, but those are really just information; the usage items are key.
Is all of this possible with Views out of the box? Or would I need an additional module to accomplish this?
Thanks!
Answer: I was able to get the output that I needed by using the Reference module and the Viewfield module.

Created Content type A and content types B, C, and D. B, C, and D are all associated with Content type A via Reference fields.
Created contextual/relationshipped views for B, C, and D, relating it to the title field of Content Type A.
Created a Viewfield on Content Type A for each of the sub-content types (B, C, and D).
Created a View for Content Type A, contextualized with Content:Title, and added the Viewfields to this new View (using reverse relationships with the node referenced field). I then added a PDF Page view using Views PDF, and that did the trick!

I can provide more detail if needed. Just let me know...
Answer: Views can do the basic reporting with some tinkering.
Look into these possible modules for outputting a PDF of a report view:

Views PDF
Print
Comment: Thanks, tenken. I am playing around with Views PDF already, and it seems to be the solution for me.

Do you have any input on how I can build a view dynamically, as I described? Basically, pulling in info/content based on data in Entity Reference fields.
Comment: you just add a relation to the view on the referenced entity. Its under the "Relations" area of the far right column for a views edit display.
Comment: I added a relationship for Entity Reference: Car serial number the engine is assigned to. I created the View as a block, and added that block to the Content area. When I visit the car with serial number ABC123, the block shows me the engine for serial ABC123, as well as all the other engines for the other cars. So it's apparently not requiring the relationship to the Content entity referenced from that car serial number entity field, even though I have that "Required" box checked. I'm sure I'm doing something wrong, but I'm not sure what. Any ideas on what I might be missing?
Comment: sorry. i'm not gonna help you debug a view much. A Relation is to another piece of content in Views. You could use a Filter (on a related field of the content) to limit what your showing. I suggest you go on IRC or look for Views tutorials.
Comment: tenken, would you be interested in taking on some of these tasks as a paid project? I feel like I am at the end of the line with my capabilities to get this (and a few other pieces of related functionality) to work. Please let me know, and I can provide you with my contact info. Thanks!
Comment: sorry, I cant take on extra work. I'm currently completely booked.


Mudar fonte padrão

Question: Estou tentando mudar a fonte de uma página que estou montando com bootstrap, mas por algum motivo ela não aceita. Fiz os seguintes passos:
No HTML:
<link href='https://fonts.googleapis.com/css?family=Indie+Flower' rel='stylesheet' type='text/css'>

No bootstrap.css:
html {
  font-family: 'Indie Flower', sans-serif;
  -webkit-text-size-adjust: 100%;
      -ms-text-size-adjust: 100%;
}

No bootstrap.min.css:
{font-family: 'Indie Flower', sans-serif;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}

Alguma sugestão?
Comment: Será que ela está sendo sobrescrita em algum lugar? Teste com !important por via das dúvidas, se funcionar procure onde ela está sendo substituída,
Comment: Adicionando !important no css deve funcionar. Verifica também se a url das fontes está correta.
Comment: Procure a tag body no bootstrap.min.css e coloque a fonte de sua escolha como primeira opção.
Answer: O ideal, é você não editar o código fonte do bootstrap, assim evita de mexer em coisas que não gostaria. Para trabalhar com um outra fonte padrão, você pode fazer como fez no próprio html:
<link href='https://fonts.googleapis.com/css?family=Indie+Flower' rel='stylesheet' type='text/css'>

E editar a fonte padrão na customização que o próprio bootstrap oferece aqui.

Mudar fonte padrão


LilyPond : how to write numbers before staves?

Question: I'm writing some exercises, one per line, and I would like to write a number before each one.
I can't figure out how to do that, so a little help would be appreciated!
My code is pretty simple :
\language "english"
\header {title = "C Major"}

{
  \time 4/4
c' d' e' f' d' e' f' g' e' f' g' a' f' g' a' b' g' a' b' c'' a' b' c'' d'' b' c'' d'' e'' c'' d'' e'' f''
}

{
  \time 4/4
f'' e'' d'' c'' e'' d'' c'' b' d'' c'' b' a' c'' b' a' g' b' a' g' f' a' g' f' e' g' f' e' d' f' e' d' c'
}

Comment: *\markup* allows arbitrary text (above or below) which should work fine. But I would definitely add note lengths and bars, and I can't imagine, that your apostrophes are as intended (at least if relative notation is used, which simplifies the source somewhat).
Answer: The standard way of putting anything before a staff is by setting its instrumentName property.
You can put
\set Staff.instrumentName = "1"

into the music, or declare the staff adding a 'with' clause:
\new Staff \with { instrumentName = "1" } { \time 4/4 c' d' e' }

\new Staff \with { instrumentName = "2" } { \time 4/4 f'' e'' d'' }

Answer: If you want to number the staves automatically, you can write some Scheme and label each staff with instrumentName = #(score-number):
\version "2.19.81"

#(define sn 0)

#(define (score-number)
   (set! sn (1+ sn))
   (string-append (number->string sn)))  % "."

\new Staff \with { instrumentName = #(score-number) } { \time 4/4 c' d' e' }

\new Staff \with { instrumentName = #(score-number) } { \time 4/4 c' d' e' }

Comment: Thank you Richard and Paco Vila for your  answers, that's perfect. Could you tell me where in the docs you finded those informations ?
Comment: instrumentName is documented here: http://lilypond.org/doc/v2.19/Documentation/notation/writing-parts#instrument-names

the set command is in http://lilypond.org/doc/v2.19/Documentation/notation/the-set-command


Como Listar mais de uma linha do banco em uma Listview usando JDBC?

Question: Como listar mais de 1 dado do banco usando JDBC em uma ListView em Android? Tenho em um usuário 13 linhas, e gostaria de recuperar as 13 e não apenas 1. Abaixo o código que utilizo:
<code>import android.os.Bundle;

import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public  class Tela2 extends MyActivity {

    static TextView useron;

    static ListView listgeral;

    static EditText email, descricao, codigo;

    Button atualiza;

    TabHost.TabSpec spec1, spec2;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tela2);

        //Crio um objeto tabhost
        TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
        tabHost.setup();
        //pego a váriavel do tabhost para setar as configurações
        spec1 = tabHost.newTabSpec("Aba 1");
        spec1.setContent(R.id.tab1);
        spec1.setIndicator("Meus Pacotes");

        spec2 = tabHost.newTabSpec("Aba 2");
        spec2.setContent(R.id.tab2);
        spec2.setIndicator("Novo rastreamento");

        tabHost.addTab(spec1);
        tabHost.addTab(spec2);

        //Crio uma váriavel contendo os componentes "TextView"
        listgeral = (ListView) findViewById(R.id.valor1);

        atualiza = (Button) (findViewById(R.id.atualiza));

        //Crio um TextView de usuario online
        useron = (TextView) findViewById(R.id.usuario);

        //Seto o valor que é recebido da classe "MyActivity"
        useron.setText(dominios);

        String url = "jdbc:mysql://localhost";

        String driver = "com.mysql.jdbc.Driver";
        //String contendo o usu�rio do banco
        String userbanco = "kappauni";

        //String contendo a senha do usuario do banco
        String passbanco = "teste";

        java.sql.Connection con = null;

        try {
            Class.forName(driver).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            con = DriverManager.getConnection(url, userbanco, passbanco);
            Statement st = null;
            st = con.createStatement();
            ResultSet rs = null;

            rs = st.executeQuery("SELECT * FROM encomendas where dominio ='"+useron.getText()+"'");

while (rs.next()){

            String valorcodigo = rs.getString("codigorastreio");
                String  valordescricao = rs.getString("descricao");

                String[] stringcodigo = {valorcodigo.toString()+"\t"+"\t"+"\t"+"\t"+"\t"+valordescricao.toString()};

                ArrayAdapter<String> arraycodigo = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,stringcodigo);

                listgeral.setAdapter(arraycodigo);

}

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    //Método que ao clicar no botão adicionar, ele vai adicionar os valores no banco

    public void Adicionar(View add) throws Exception {

        email = (EditText) findViewById(R.id.form1);

        descricao = (EditText) findViewById(R.id.form2);

        codigo = (EditText) findViewById(R.id.form3);

        BancoInsert conexaoinsert = new BancoInsert();

        conexaoinsert.insert();

    }

//BOtão de atualizar

    public void atualizar(View upd){

        Toast.makeText(Tela2.this,"Atualizou",Toast.LENGTH_SHORT).show();

    }

        }
</code>
Comment: Você dá esse monte de espaço em seu código mesmo ou ficou mal formatado quando postou aqui? Fica difícil acompanhar o código.
Comment: Usando banco MySQL localhost em um dispositivo Android? O próprio Android tem o SQLite, porque não usa ele?
Comment: É apenas para teste, por que o mesmo vai ser usado em minha hospedagem como já utilizava.
Answer: Olá, acredito que você seja novo no mundo Android. Não é possível e nem recomendado acessar um banco externo diretamente a partir de um aplicativo Android. A forma recomendada de acessar dados externos da sua aplicação é através de requisições REST de seu web server, o seu app vai consumir esses dados, podendo fazer outras requisições para atualizar o banco, mas sempre através de um web server. Veja esse post (em inglês) sobre isso:

https://stackoverflow.com/a/12233178/2129877

Uma lib muito boa e simples para fazer chamadas REST é o retrofit
http://square.github.io/retrofit/
Sobre o seu código, o setAdapter() só precisa ser feito uma única vez, por isso não precisa estar dentro do "for". O Correto seria você passar um array de strings onde cada string é a tupla (linha) do banco.
[]s
Comment: Possível até que é... Mas, como você falou, não é recomendado. :)


Joint Density of $\sin(2\pi U)$ and $\cos(2\pi U)$,$ U$ uniform $(0,1)$

Question: I need help with finding the joint density of $X$ and $Y$ where
$$X=\sin(2\pi U),\quad Y=\cos(2\pi U)$$
where $U$ is uniformly distributed on (0,1).
Comment: Related: https://math.stackexchange.com/questions/79684/absolute-continuity-of-a-distribution-function?rq=1.
Answer: Since $(X,Y)$ is almost surely on the unit circle whose Lebesgue measure is zero, the distribution of $(X,Y)$ has no density with respect to the Lebesgue measure on the plane $\mathbb R^2$.
To describe the distribution of $(X,Y)$ one usually says that $(X,Y)$ is uniformly distributed on the unit circle, which is another way of saying that, for every measurable function $a$ on $\mathbb R^2$,
$$
E(a(X,Y))=\int_0^{2\pi}a(\cos t,\sin t)\frac{\mathrm dt}{2\pi}.
$$
Comment: Thanks Did. Does it have a joint cdf at least?
Comment: Every pair of random variables has a CDF. The CDF of (X,Y) is a mess (and quite uninteresting).


Hive check elements in array

Question: I have an Hive table made of user_id and item_id (id of items that have been purchased by the user).
I want to get a list of all the users who purchased item 1 but not item 2 and 3.
To do this I wrote the simple query:
SELECT user_id, collect_set(item_id) itemslist FROM mytable
WHERE item_id in (1, 2)
GROUP BY user_id
HAVING -- what should I put here???

As you can see, I don't know how to check whether the array itemslist contains 1 and not 2.
How do you do this?
If there is some more efficient way can you please tell me both (or more) methods?
Answer: There are some collection functions in Hive  `(See collection functions here : https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF ) which can use here.
You can use the array_contains(Array<T>, value) function to check if item 1 is present and  the size(Array<T>) function to make sure the length is 1. If both conditions are satisfied, you will get the desired output.
Comment: array_contains is not working with regex pattern. For instance, if I want to check there is any item in array that have substring foo.*. Do you have any suggestion?
Comment: what if I wanted to display the item found in the select statement, how would I go about that?

Hive check elements in array


20 Gifts and four stacks, How many Gifts in each pile?

Question: There are twenty gifts stacked up into $4$ piles.
The first pile has $3$ less than the second pile.
The second pile has $2$ more than the third pile.
The fourth pile has twice as many as the second pile.
How many gifts are in each pile?
This is for my $8$ year old son... Embarassing that I can't figure it out.
Answer: At that age they are pretty clever, but usually innocent of algebra. So the expected approach is trial and error, in the schools often called "guess and check." But one might as well be reasonably smart about it.
Let's visualize the piles of gifts. The second pile is bigger than its two neighbours, and the fourth is twice as big as the second. So the fourth is quite big, and has an even number of gifts. Let's guess that the fourth has $12$ gifts. Then the second has $6$, the third has $4$, and the first has $3$. Add up, we get $25$. Too bad, not right.
So $12$ for the fourth is too big. Let's try for $10$. Then the second has $5$, the third has $3$, the first has $2$. Add up. Bingo!
One can work in this way from any of the piles. For example, the first is clearly the smallest. If we guess it is of size $1$, we get $1, 4, 2, 8$, too small a sum. But $2, 5, 3, 10$ get us to the right place.
Comment: You are welcome. Good "guessing," and appropriate adjustment if the guess is wrong, is a useful tool.
Comment: So is algebra a guilty pleasure?
Comment: Wow thank you so much!God bless
Answer: Let
$$\begin{align*}
A & =\text{number of gifts in the first pile}\\
B & =\text{number of gifts in the second pile}\\
C & =\text{number of gifts in the third pile}\\
D & =\text{number of gifts in the fourth pile}
\end{align*}$$
Then the information given is that
$$A=B-3,\quad B=C+2,\quad D=2B,\quad A+B+C+D=20.$$
We can re-express $B=C+2$ as $C=B-2$. Replacing term by term:
$$\underbrace{A}_{B-3}+B+\underbrace{C}_{B-2}+\underbrace{D}_{2B}=20$$
we see that
$$(B-3)+B+(B-2)+2B=5B-5=20$$
which implies $B=5$, and then $A=2$, $C=3$, and $D=10$.
Answer: Besides André's approach, I can also imagine a bright child starting with an empty first pile, three gifts in the second pile, and one gift in the third, easily fulfilling the first three requirements, and then working out that there must be six gifts in the fourth pile. That's the smallest arrangement that meets the requirements on the relative sizes of the piles; unfortunately, it uses only $10$ of the $20$ gifts.
The first three piles change in step with one another: add one gift to one of them, and you clearly must add one gift to each of them. And every time you add one gift to the second pile, you must add two gifts to the fourth pile. Thus, adding one gift to any of the first three piles forces you to add a total of five gifts. Aha! Do this twice, and you've used all $20$ gifts. You've now added two gifts to each of the first three piles and four gifts to the fourth pile, so the piles now contain $2$, $5$, $3$, and $10$ gifts, respectively.


Trying to solve differential equation $y'=\frac{3x-y+4}{x+y}$

Question: ÊDIT: Found the second mistake (failed by calculating $u_{1,2}$)!
I might have made a mistake, however, I am not able to detect it. Here we go:

$$y'=\frac{3x-y+4}{x+y}, \quad y(1)=1$$

1.) set $x = X+a$ and $y=Y+b$, so $$\frac{dY}{dX} = \frac{3X-Y+(3a-b+4)}{X+Y+(a+b)}$$
2.) Choose $a,b$ with $a=-1$ and $b=1$, then the differential equation is:
$$\frac{dY}{dX} = \frac{3X-Y}{X+Y} = \frac{3-\frac{Y}{X}}{1+\frac{Y}{X}}$$
3.) Now substitution: $Y = uX$, so $Y' = u+ Xu'$ and we have
$$\frac{dY}{dX} = \frac{3-u}{1+u} = u+Xu'$$
4.) Solve this, ending with:
$$\frac{1+u}{-u^2-2u+3} du = \frac{dX}{X}$$
5.) Solving this by integration, ending with:
$$-\frac{1}{2} \ln (-u^2-2u+3) = \ln(X) + \ln(C)$$
$$-u^2-2u+3 = \exp(-2(\ln(CX))) = e^{\ln((CX)^{-2})} = \frac{1}{(CX)^2}$$
$$-u^2-2u+3-\frac{1}{(CX)^2} = 0$$
6.) So I get $$u_{1,2} = -1 \pm \sqrt{16-\frac{4}{(CX)^2}}$$
Because of the substitution and by having chosen $Y=y-1$ and $X=x+1$ it follows that
$$y-1 = -x-1 \pm (x+1)\cdot \sqrt{16-\frac{4}{(C(x+1))^2}}$$
Finally:
$$y(x) = -x \pm \sqrt{16(x+1)^2-\frac{4}{C^2}}$$
7.) With the initial values I get
$$C=\frac{1}{\sqrt{15}}$$
8.) However, my result is not true for the differential equation I started with, since left hand side and right hand side are not equal.
Thanks for any advice/hint :)
Comment: Replacing $-2(\ln(x)+C)$ by $\frac1{x^2}+C^*$ is faulty. (I did not check the rest.)
Comment: Let $C^*=\ln C$.
Comment: this equation is from Abel type
Comment: $u=\dfrac{y-1}{x+1}$ so $u(1)=0$ shows $C=\dfrac{1}{\sqrt{3}}$.
Comment: $u_{1,2} = -1 \pm \sqrt{4-\frac{1}{(CX)^2}}$
Answer: Your mistake started from 6 (I'm writing $c=C^2$ here)
$$ u^2 + 2u = 3 - \frac{1}{cX^2} $$
$$ (u+1)^2 = 4 - \frac{1}{cX^2} $$
$$ u= -1 \pm \sqrt{4-\frac{1}{cX^2}} $$
or
$$ \frac{y-1}{x+1} =-1 \pm \sqrt{4-\frac{1}{c(x+1)^2}} $$
Using the condition $x = 1, y = 1$ we get
$$ -1 \pm \sqrt{4-\frac{1}{4c}} = 0 $$
Only the plus sign satisfies
$$ c = \frac{1}{12} $$
Final solution
$$ y(x) = 1 + (x+1)\left(-1 + \sqrt{4-\frac{12}{(x+1)^2}} \right) $$
Or
$$ y(x)= -x + 2\sqrt{x^2+2x-2}$$
Comment: Thank you so much - I have already found the mistake earlier, wanting to edit the correct steps now, but you were faster. Thank's a lot!


Search API Index view shows all content pages, instead of just products commerce Kickstart

Question: I'm using Commerce Kickstart and I have set up a search index using mysql rather than Apache Solr. Faceted search works perfectly. However if no facets are selected the results show all pages, including Terms of Use, 404 and shipping fees, to name but a few. I have tried setting up filters, but then it doesn't show any products. I'm attaching a screenshot of the results. I'm sure this is an easy fix, but I have spent hours testing and reading within the forums to try and find a solution, to no avail.
Many Thanks in advance.
Answer: You want to filter results by content type? If yes, you can just set filter in tab "workflow" of your search index. It's "bundle filter". Good luck.


linear model using significant outcomes of linear model

Question: I am getting confused about a couple of techniques that I'd like to discuss:
I am using an R package that in turn depends on the limma package. I tried reading the docs, but I can't grasp what is going on. I understand that, for example, instead of performing a t-test for each feature of a set, it uses a linear model - I have no idea whether this will be important. In any case, the package uses limma under the hood to find differential features out of let's say 400K features for 1K samples
I know how to find differential features using my package based on limma, but for 400K samples we assume about 20K (5%) significant outcomes - which is about what I end up with.
But what If you need a small panel of features that can predict the outcome: the sample status? I can't seem to find the answer to this question: Can or should you build a (penalized) model with predictors you already know are differential between your samples?
And in case creating a model from differential features is not a good idea: Should you instead build a model using the original 400K features, or can I go straight to sampling a set of the 20K differential features to predict outcome and comparing ROC AUC values?
Answer: There are both statistical and practical issues here.
LASSO is a well established procedure for selecting a predictively useful subset from a large number of features. In your application, with a yes/no outcome, this would be done in the context of a logistic regression model. The number of features selected depends on a penalty factor, whose value is typically chosen to optimize a measure of performance as assessed by cross-validation. See An Introduction to Statistical Learning for an introduction to this and to many other modeling approaches.
Note that AUC or classification accuracy are not usually the best measures of performance for selecting the penalty factor; minimizing deviance, the default in the glmnet package in R, is preferred. With a logistic model developed this way, providing probabilities of class membership instead of a simple yes/no prediction, you can trade off between false-positive and false-negative classifications based on their different costs.
A potential difficulty with LASSO or any other feature-selection method is that the particular features selected may vary widely from study to study. With more features than samples there are necessarily correlations among different features, so those chosen from a set of highly correlated features will depend on the luck of the draw in terms of the samples, random analysis errors, and so forth. That's not necessarily a drawback for predictive ability, but it does mean that you should refrain from claiming that the selected features are the only important features.
If you are trying to develop a limited test panel to replace a large-scale analysis like the microarrays for which limma was designed, you also need to take into account practical issues in designing and performing the new tests. For example, in microarray studies, a gene high up on the differentially expressed list or selected by LASSO might only be expressed at a very low level and thus difficult to use in practice. So there will necessarily be some back-and-forth between statistics and experiments in coming up with a useful limited panel of predictors.
You might want to read this paper on breast cancer classification, in particular on tests to predict the probability of disease recurrence. Molecular classifications originally developed from microarray data have subsequently been reduced to small panels of genes whose expression can be determined with commercial tests on standard pathology specimens. As the paper notes, few particular genes are shared among the competing commercial tests, emphasizing the somewhat arbitrary nature of the selection of a useful set of features from a very large set.


About simple linear regression

Question: If we assume a vector $y$ which has normal distribution with mean m and covariance matrix M.
A simple linear regression model is written as $y = \beta_0 + X\beta + e$.
If the mean of y is non zero and has to be estimated, so what can be its estimated value?
In other terms, is there a relationship between the mean vector and the estimated regression coefficients?
Furthermore, what is the $\beta_0$ ? why they consider in most cases that it is equal to 0?
Any help will be very appreciated!
Answer: Let me address your questions in inverse order.
Firstly, in most sources I have encountered so far the task is more conveniently reformulated as $y = X^*\beta^* + \epsilon$, where $X^*$ is just $X$ with a column vector of ones appended to the right and $\beta^*$ is your $\beta$ with $\beta_0$ appended to it. In such way, you essentially consider the same model but the explicit intercept term $\beta_0$ now has disappeared. Maybe that's what you took as $\beta_0=0$?
Secondly, the most straightforward and classic way to estimate the explanatory variables $\beta$ is to use the Least Squares method. The solution is then given in terms of the right Moore - Penrose pseudoinverse:
$$
\beta^*_{LS}=(X^{*T}X^*)^{-1}X^{*T}y
$$
As you can see, the sample mean is not explicitly present here.
However, I may be missing something obvious in your question so please correct me if I'm wrong.
EDIT: After reading your question again I've noticed that in your original formulation the errors $e$ are not uncorrelated but rather have a covariance matrix $M$. So, to stay formal I would like to notice that this case can actually be reduced to the non-corellated case by a linear change of variables: write your errors as $e=\sqrt{M}e^*$, where $\sqrt{M}$ is, for example, the Cholesky square root of $M$ and $e^* \sim N(0, I)$, and multiply by $(\sqrt{M})^{-1}$.
Comment: Thank you for the answer. But what is the estimated value of the mean? does is just the sample mean computed as the maximum likelihood under gaussian hypothesis?
Comment: @Christina Taking the expectation of your equation, we immediately find that the expectation of y is (X*)*(\beta*). In order to estimate it, you can simply calculate the sample mean which is asymptotically the best possible unbiased estimator. However, by doing so you would not solve the regression task because you would only estimate (X*)*(\beta*) and not \beta* itself.

About simple linear regression


How to perform these ornaments?

Question:
How do you play the "opppsite" acciaccatura?
And what does it mean by Glissafter?
Comment: Don't understand why some have an explanation, but not all.
Answer: This appears to be from the manual of a piece of software written for one individual musician to use.
I suggest you contact the author to find out what some of these non-standard terms are supposed to mean. His contact details are in https://mellowood.ca/mma/online-docs/ref/node1.html#SECTION00110000000000000000
To quote that page:

This manual most likely has lots of errors. Spelling, grammar, and
  probably a number of the examples need fixing

I don't think we can second-guess what he intended to mean here.

How to perform these ornaments?


github php buildpack supporting DB2 and IBM Cloud php app

Question: We have a PHP app on IBM Cloud connected to an instance of DB2 (also on IBM Cloud) that has been working flawlessly for 2 years
Up until Sept 16th, we used the buildbpack with our PHP app (so that it could connect to DB2 via the db2_connect PHP function.)
Now after the cflinuxfs3 upgrade that buildpack does not work and we are forced to use the

" "php_buildpack" community buildpack already installed in Cloud Foundry".

however, This new buildpack does not support DB2 connections. When trying to use this buildpack I run a log I find this error :

"undefined function : db2_connect()".

I've searched high and low for a buildpack that would both support connections to DB2 and be compatible with cflinuxfx3 on IBM Cloud Foundry.
Does anyone know of such a buildpack?
Comment: Show more information about the runtime environment, for example, Show php extensions by running command `php -m`.
Answer: There was an announcement:

Swift Buildpack on IBM Cloud Now Supports cflinuxfs3 and Includes New Runtimes
https://www.ibm.com/cloud/blog/announcements/the-swift-buildpack-versions-2020-and-210-are-now-available-on-ibm-cloud
Here is snipped from the URL:

The Swift buildpack versions 2.0.20 and 2.1.0 are now available on IBM Cloud.
These buildpacks provide Swift version 5.0 as the default runtime and caches version 4.2.4. An existing application will not be affected by the new buildpack until you redeploy it. After you redeploy, existing applications should continue to run "as is" without any additional changes. New applications will automatically use the new buildpack. Swift buildpack verison 2.1.0 supports cflinuxfs3 only and version 2.0.20 supports cflinuxfs2 only.

Hope this helps.
Comment: thank you for the effort but those are SWIFT buildpacks and are not compatible with php apps. I need a php buildpack here


Every non-unit in a non-commutative ring with unity is contained in a maximal left ideal.

Question: Suppose that $x$ is a non-unit in a non-commutative ring $R$ with unity $1$. Is it true that $x$ is contained in some maximal left ideal of $R$. I tried to prove the statement by Zorn's Lemma by defining $\mathcal{L}= \lbrace I \subsetneqq R \,\text{ left ideal} : x\in I \rbrace$. I just need to show that $\mathcal{L} \neq \emptyset$. Since $x$ is non-unit, at least one of $Rx$ and $xR$ is proper. It $Rx$ is proper, then $\mathcal{L} \neq \emptyset$ and we can proceed. But what if $xR$ is proper (and $Rx$ is not guaranteed to be proper) ?!.
I need any help. Thanks in a advance.
Comment: There exist rings with elements $a,b$ such that $ab=1$ and $ba\neq 1$. In this case, $b$ is not contained in any maximal left ideal, and $b$ isn't a unit (since $baR\nsubseteq R$).  The best you can say is that a noninvertible element is in a maximal left ideal OR a maximal right ideal.
Answer: The statement as claimed is false. It can happen that $x$ has a left inverse, but no right inverse. For example, if $R=M_{\mathbb N}(\mathbb R)$ is the ring of infinite matricies with coefficients in $\mathbb{R}$, consider the element
$$x=\begin{pmatrix}0&0&0&\cdots\\1&0&0&\cdots\\0&1&0&\cdots\\\vdots&\vdots&\vdots&\ddots\end{pmatrix}.$$
Then
$$y=\begin{pmatrix}0&1&0&\cdots\\0&0&1&\cdots\\0&0&0&\cdots\\\vdots&\vdots&\vdots&\ddots\end{pmatrix}$$
is a left-inverse of $x$. But if $a\in R$ is arbitrary, we have
$$xa=\begin{pmatrix}0&*&*&\cdots\\0&*&*&\cdots\\0&*&*&\cdots\\\vdots&\vdots&\vdots&\ddots\end{pmatrix},$$
so in particular $xa\neq 1$. Hence $x$ is not a unit (since it has no right inverse), but $Rx=R$, so $x$ isn't contained in any left maximal ideal.