Understanding steps of calculating discriminants.

Question: Here is the question I am asking about some steps in its keen answer:
For $n =3,$ write $\Delta^2$ as an element of $A = \mathbb{Q}[e_{1}, e_{2}, e_{3}.]$(manually)
The answer is given below:
What a strange hint. There's a much better way to do it, and I have no idea why the author didn't set $a_3 = 1$ from the start.
I learned this argument from David Speyer. Computing the discriminant as a polynomial in the $e_i$ is equivalent to computing the discriminant of the polynomial $f(x) = x^3 - e_1 x^2 + e_2 x - e_3$. Up to a translation, which does not affect the discriminant, we can remove the quadratic term from this polynomial: if we substitute $x = y + \frac{e_1}{3}$, then we get a new polynomial (brace yourself)
$$\begin{align} g(y) &= \left( y^3 + e_1 y^2 + \frac{e_1^2}{3} y + \frac{e_1^3}{27} \right) - e_1 \left( y^2 + \frac{2e_1}{3} y + \frac{e_1^2}{9} \right) + e_2 \left( y + \frac{e_1}{3} \right) - e_3 \\
 &= y^3 + \left( - \frac{e_1^2}{3} + e_2 \right) y + \left( -\frac{2e_1^3}{27} + \frac{e_1 e_2}{3} - e_3 \right). \end{align}$$
Set $p = - \frac{e_1^2}{3} + e_2, q = -\frac{2e_1^3}{27} + \frac{e_1 e_2}{3} - e_3$, so that this new polynomial can be written $g(y) = y^3 + py + q$. Now it suffices to compute the discriminant of this new polynomial as a function of $p$ and $q$, then plug in these expressions for $p$ and $q$ in terms of the $e_i$.
The point of doing this is that we've now removed most of the terms from the discriminant: $p$ has degree $2$, $q$ has degree $3$, and $\Delta^2$ has degree $6$, which implies that it must be some linear combination $ap^3 + bq^2$ of $p^3$ and $q^2$. So we only have two coefficients to compute. We can actually compute these coefficients by computing the discriminant of two specific polynomials:

Set $p = -1, q = 0$. Then $g(y) = y^3 - y = y(y + 1)(y - 1)$ has roots $0, \pm 1$, so its discriminant is $\left( (0 - 1)(1 - (-1))( (-1) - 0) \right)^2 = 4$, which gives $a = -4$.
Set $p = 0, q = -1$. Then $g(y) = y^3 - 1 = (y - 1)(y - \omega)(y - \omega^2)$ has roots $1, \omega, \omega^2$ where $\omega$ is a primitive third root of unity (satisfying $1 + \omega + \omega^2 = 0$), so its discriminant is $\left( (1 - \omega)(\omega - \omega^2)(\omega^2 - 1) \right)^2 = (1 - \omega)^6 = (- 3 \omega)^3 = -27$, which gives $b = -27$.

So we get $\boxed{ \Delta^2 = -4p^3 - 27q^2 }$ (already a useful formula in practice), and substituting $e_1, e_2, e_3$ in gives the full cubic discriminant (of more dubious value; I have never used it).
My questions are:
In this part of the solution: "  * Set $p = -1, q = 0$. Then $g(y) = y^3 - y = y(y + 1)(y - 1)$ has roots $0, \pm 1$, so its discriminant is $\left( (0 - 1)(1 - (-1))( (-1) - 0) \right)^2 = 4$, which gives $a = -4$.

Set $p = 0, q = -1$. Then $g(y) = y^3 - 1 = (y - 1)(y - \omega)(y - \omega^2)$ has roots $1, \omega, \omega^2$ where $\omega$ is a primitive third root of unity (satisfying $1 + \omega + \omega^2 = 0$), so its discriminant is $\left( (1 - \omega)(\omega - \omega^2)(\omega^2 - 1) \right)^2 = (1 - \omega)^6 = (- 3 \omega)^3 = -27$, which gives $b = -27$.
"
1 -in the first paragraph why is the discriminant looks like that $\left( (0 - 1)(1 - (-1))( (-1) - 0) \right)^2 = 4$,, what are the first terms $0,1,(-1)$ in the brackets representing and similarly what are the second terms $0,1,(-1)$ representing? should not $a = -2$ instead?

2-In the second paragraph, why we are factorizing  $y^3 - 1$ in terms of roots of unity, why we needed that? why this root of unity satisfies $1 + \omega + \omega^2 = 0$?
3-In the second paragraph, How in generally we calculate the discriminant of roots of unity?
Could anyone help me answer those questions please?
Answer: I think it is always easier to calculate the discriminant of a trinomial by noting that $\Delta^2$ is equal to the Vandermonde Matrix times its transpose.
If the roots are $\alpha_1,\alpha_2,\dots,\alpha_n$ then we have
$$
\Delta^2=
\begin{bmatrix}
1 & 1 &\dots &1\\
\alpha_1&\alpha_2&\dots&\alpha_n\\
\vdots &\vdots &\ddots&\vdots\\
\alpha_1^{n-1}&\alpha_2^{n-1}&\dots&\alpha_n^{n-1}\\
\end{bmatrix}
\begin{bmatrix}
1 & 1 &\dots &1\\
\alpha_1&\alpha_2&\dots&\alpha_n\\
\vdots &\vdots &\ddots&\vdots\\
\alpha_1^{n-1}&\alpha_2^{n-1}&\dots&\alpha_n^{n-1}\\
\end{bmatrix}^{T}
$$
which is equal to
$$
\begin{bmatrix}
s_0 &s_1 &\dots&s_{n-1}\\
s_1& s_2 &\dots &s_{n}\\
\vdots &\vdots&\ddots&\vdots\\
s_{n-1}&s_{n}&\dots&s_{2n-2}
\end{bmatrix}
$$
where the $s_k:=\sum_{i=1}^{n}\alpha_{i}^{k}$ are the sums of the $k$-th powers of the roots. Hence $s_0=\sum_{i=1}^{n}1=n$, $s_1=\sum_{i=1}^{n}\alpha_i=e_1$ (in your notation), and so on.
In the case of $X^{3}-1$ the roots are $1,\omega,\omega^2$, where $\omega:=e^{\frac{2\pi i}{3}}$.  As the coefficient of $X^2$ is $0$ we have that the sum of the roots is $0$.
Hence we see that $s_0=3$, $s_1=0$, $s_2=1+\omega^2+\omega^4=1+\omega^2+\omega=0$; and then recursively $s_3=3$, $s_4=0$ and so
$$
\Delta^2=
\begin{bmatrix}
3 &0 & 0\\
0& 0 & 3\\
0 & 3& 0\\
\end{bmatrix}=-27.
$$
We could as easily have done the cubic $X^3+pX+q$ this way: find $s_1$ and $s_2$ by hand the rest follow recursively as $X^{k+3}+pX^{k+1}+qX^{k}=0$ at the roots.
It is almost as easy to work out the discriminant of $X^n-1$: I leave it for you to check.
Comment: I have annotated the answer. I think you should ask the person who answered your question last time about their solution.
Comment: In the case of $X^{3}-1$ why the roots are $1,\omega,\omega^2$?
Comment: Does $k$ ranges from 0 to $n-1$?
Comment: I do not see why "$s_0=3$, $s_1=0$, $s_2=0$" .... could you please elaborate that?
Comment: Can you please explain to me what is the way used in the link I mentioned?
Comment: what do you mean by $s_{k}$ is the sums of kth power of the roots? do you mean  $1 + \alpha_{1} + .... + \alpha_{1}^{n-1} = s_{n-1}$?
Comment: Thank you so much ..... I already did .... but after you gave me this answer I understood the other answer :)


Find the maximum value of expression $13\sqrt{(x^2-x^4)}+9\sqrt{(x^2+x^4)}$

Question:
Find the maximum value of expression $$13\sqrt{(x^2-x^4)}+9\sqrt{(x^2+x^4)}$$ for $0\le x \le 1$

My Attempt
$(x^2)(1 - x^2) \le \frac{x^2+(1 - x^2)}{2}=\frac{1}{2} \Rightarrow 13\sqrt{(x^2-x^4)} \le \frac{13}{\sqrt 2}$
Same way we obtain $9\sqrt{(x^2+x^4)} \le \frac{9}{\sqrt 2}\sqrt{1+2x^2} $
As $0\le x \le 1$ so $\sqrt{1+2x^2} \le \sqrt{3}$ .
Hence, maximum is $\frac{1}{\sqrt{2}}(13+9\sqrt{3})$ occurs at $x=\frac{1}{\sqrt{2}}$.
I then checked the value in wolfram alfa and found that maximum is $16$ and occurs at $x=\frac{2}{\sqrt{5}}$.
Can anyone point my mistake. Thanks in advance.
Answer: Yes, for $x=\frac{2}{\sqrt5}$ we get a value $16$.
We'll prove that it's a maximal value.
We need to prove that
$$13x\sqrt{1-x^2}+9x\sqrt{1+x^2}\leq16$$ or
$$44x^4-125x^2+128\geq117x^2\sqrt{1-x^4}$$ or
$$(5x^2-4)^2(625x^4+560x^2+1024)\geq0.$$
Also, we can use C-S and AM-GM:
Let $x=\frac{2}{\sqrt5}t$.
Thus,
$$13x\sqrt{1-x^2}+9x\sqrt{1+x^2}=\frac{2}{5}t\left(13\sqrt{5-4t^2}+9\sqrt{5+4t^2}\right)=$$
$$=\frac{2}{5}t\left(13\sqrt{5-4t^2}+27\sqrt{\frac{5}{9}+\frac{4}{9}t^2}\right)\leq$$
$$\leq\frac{2}{5}t\sqrt{(13+27)\left(13(5-4t^2)+27\left(\frac{5}{9}+\frac{4}{9}t^2\right)\right)}=$$
$$=16t\sqrt{2-t^2}\leq16\cdot\frac{t^2+2-t^2}{2}=16.$$
Comment: Where's the proof?
Comment: @ParclyTaxel He'll edit the post in a few seconds and include it
Comment: @Benjamin Moss I am sorry! I found a mistake in my proof, but I fixed it. Now, this is a proof.
Comment: You're good, I was merely remarking that you were getting your foot in the door by writing the first few lines to indicate you were formulating your proof, and then you were going to post it, which I am okay with.
Comment: @MichaelRozenberg I am bit confused. It is a contest problem. The knowledge that maximum is 16 and occur at $x=\frac{2}{\sqrt(5)}$ is not possible to guess in exam structure. I believe there must be a proof which will directly arrive at these numbers. Any way thanks for your proof.
Comment: @rugi But you can see that I proved it! See my second proof. The value $\frac{2}{\sqrt5}$ we can get, when we think about an equality occurring in AM-GM and in C-S. I really don't understand you!
Answer: $f(x)=13\sqrt{x^2-x^4}+9\sqrt{x^2+x^4},\;x\in[0,1]$
$$f'(x)=\frac{9 \left(4 x^3+2 x\right)}{2 \sqrt{x^4+x^2}}+\frac{13 \left(2 x-4 x^3\right)}{2 \sqrt{x^2-x^4}}=0$$
as $x>0$ we can simplify
$$\frac{18 x \left(2 x^2+1\right)}{2 x \sqrt{x^2+1}}+\frac{26 x \left(1-2 x^2\right)}{2 x \sqrt{1-x^2}}=0 \to \frac{9 \left(2 x^2+1\right)}{\sqrt{x^2+1}}=\frac{13 \left(2 x^2-1\right)}{\sqrt{1-x^2}}$$
Substitute $x^2= u-1$ as $0\le x^2\le 1$ then $1\le u\le 2$
$$\frac{9 (2 (u-1)+1)}{\sqrt{u}}=\frac{13 (2 (u-1)-1)}{\sqrt{2-u}}$$
square both sides, reorder and simplify
$500 u^3-1500 u^2+1125 u-81=0$
Factor $(5 u-9) \left(100 u^2-120 u+9\right)=0$
gives $u_1=\frac{9}{5},u_2=\frac{3}{10} \left(2-\sqrt{3}\right),u_3=\frac{3}{10} \left(2+\sqrt{3}\right)$
$u_2$ is out of the interval $(1,2)$
so we have $x^2=u_1-1=\dfrac{4}{5} \to x_1=\dfrac{2}{\sqrt 5}$  and
$x^2=u_3-1 \to x_2=\dfrac{1}{10} \sqrt{30 \sqrt{3}-40}$
$f(x_1)=16;\;f(x_3)=\dfrac{1}{10} \sqrt{7974 \sqrt{3}-8168}\approx 7.51$
Maximum value of $f(x)$ in $[0,1]$ is $f\left(\dfrac{2}{\sqrt 5}\right)=16$
hope it is useful
Comment: Thanks a lot for showing all the steps. It is now clear to me.


Is there anyway I can limit my forecast to a upper limit using bsts package?

Question: I am using bsts package for forecasting. I need my forecast to be less than the upper limit. Like shown in the below figure.

FaceBook Prophet allows this by an option. I would like to do the similar stuff using bsts package. Is there anyway I can limit my forecast to a upper limit using bsts package?
Answer: As with any other time series method you can forecast a transformation of $z = g(y)$ instead of $y$ directly. Such that the inverse of the transformation is bounded.
If your example has upper $b$ and lower $a$ limits you can use the following transformation:
$$
z =  g(y) = \log\left(\frac{y-a}{b-y}\right)
$$
with
$$
y = g^{-1}(z) = (b-a)\cdot \frac{e^z}{1+e^z} + a
$$
as discussed here https://robjhyndman.com/hyndsight/forecasting-within-limits/
If it only has a lower bound $a$ (and no upper bound) you can predict:
$$
z = g(y) = \log\left(y - a\right)
$$
and then apply the inverse
$$
y = e^z + a
$$
If it only has an upper bound $b$ (and no lower bound) then $-y$ has a lower bound ($-b$) and you can predict:
$$
z = g(y) = \log\left(b - y\right)
$$
and then apply the inverse
$$
y = b - e^z
$$


How can we clear kendo UI Grid for angular 2 when we change paging of grid

Question: We had prepared one demo of Kendo UI Grid binding with angular 2.
We had used Custom paging with pagesize of 250 in this demo.
When we move to the next page in Grid, the memory usage increses every time.
At some point scrolling gets slower and application also gets crashed.
So is there any way to clear grid data when i move to next or pervious page?
Or any other way to get better performance in grid with large amount of pagesize more then 500.
we have also tried this.gridData.next([])  to clear grid data. But .next() funtion is not supporting for Observable.
Can please provide demo for it.
Following is the code we are using to bind grid and custom paging.
Code in app.module.ts file.
public query(config: any): void {
    this.getTask(config)
        .subscribe(x => super.next(x));

}
private getTask(config: any): Observable<GridDataResult> {
    let params = new URLSearchParams();
    params.set('Skip', config.skip);
    params.set('Take', config.take);

    this.http.get(`${this.TaskCount_URL}`)
        .map(response => response.json())
        .subscribe(result => this.total = result);

    return this.http.get(`${this.TaskDetail_URL}`, { search: params })
        .map(this.extractData)
        .map(response => (<GridDataResult>{
            data: response,
            total: this.total
        }));
}

Code in app.Component.ts file
public onNextClick(event: any) {
    this.skip = this.skip + 1000;
    this.gridData = this.service;
    this.service.query({ skip: this.skip, take: this.pageSize, sort: this.sort });
}

Comment: Is this similar to - http://stackoverflow.com/questions/41140186/how-to-clear-data-on-kendo-ui-angular-2-grid
Comment: knikolov no, that is when navigating away from the grid, this question is when changing page of the grid
Answer: Change your query function slightly.
From this.
public query(config: any): void {
    this.getTask(config)
        .subscribe(x => super.next(x));

}

To this.
public query(config: any): void {
    super.next(data: [], total: 0);
    this.getTask(config)
        .subscribe(x => super.next(x));
}

This will set the grid to nothing while its waiting for the next page of data.
The other caveat is to make sure you never send an error to the Behaviour Subject. If you do, it will stop processing anything else. Best practice is probably along the lines for this.
public query(config: any): void {
    super.next(data: [], total: 0);
    this.getTask(config)
        .subscribe(
            x => super.next(x),
            err => {
                //notify user of error some other way.
                alert("Error retrieving grid data");
                super.next(data: [], total: 0);
            }
        );
}


Site Hosted in IIS7 Continuously Prompting with Windows Authentication

Question: I've read most of the articles posted about this topic already and nothing has resolved this issue, so I figured it was time to post.
I have an intranet site that uses Windows Authentication within our network.  Everything was working fine for a few months, but all of a sudden when I came back from the weekend it will prompt users forever to enter their username & password.  Even if you use valid credentials it will not work.  An error message is never shown, even if you cancel the prompt (which will keep asking for credentials forever).  Tracing shows that a 401.1 is being returned, but I can guarantee that valid credentials are being sent.  I don't understand 1) why it's prompting in the first place and 2) why it won't accept credentials anymore.
Here are the the steps that I've taken this far:

Made sure that NTLM is first in the provider list
Made sure that Anon auth is disabled
Disable of loopback
Rebooting

I have also made sure that there are no Authentication Failures in the audit Security log.  I did enable Failed Request Tracing and the only thing I see in the logs related to Windows Authentication is a message saying:

no credentials are available in the security package

However, many articles mention that this has to do with Kerberos, which I don't even have as a provider, so I'm not sure what this is referring.
Any ideas?  I can post the tracing logs if you guys want.
Answer: Turns out there is actually a bug with Server 2008 R2 when connected to a domain and either the password of the identity of the application pool is changed or the computer password itself is changed.  This causes all authentication methods to fail due to the following cause by Microsoft:

This issue occurs because a flag is set in a global credential incorrectly after the computer password is changed.  Therefore, authentication fails after the IIS service is restarted.

The resolution is provided as a hotfix from Microsoft.  Here is the link to the KB article as well as where the hotfix can be downloaded:  Users cannot access an IIS-hosted website after the computer password for the server is changed in Windows 7 or Server 2008 R2


How to push checkbox values in a array?

Question: I am building a Treeview application in react and I have some problem that I am not solving.
When I click Apply button I want to push in a array all selected values, but when I click Cancel button I want to remove all the last selected values.
My code is as below, please can anyone modify the code and help the for the solution?
export default class TreeView extends Component {

constructor(props) {
    super(props);
    this.state = {
        checked: [],
        expanded: [],
        keyword:"",
        pushOn:[],
    };
}

onSearchInputChange = (event, data, searchedNodes) => {
    this.setState(prevState => {
        if (prevState.keyword.trim() && !data.value.trim()) {
            return {
                expanded: [],
                keyword: data.value
            };
        }
        return {
            expanded: this.getAllValuesFromNodes(searchedNodes, true),
            keyword: data.value
        };
    });
};

getHighlightText = (text, keyword) => {
    const startIndex = text.indexOf(keyword);
    return startIndex !== -1 ? (

        {text.substring(0, startIndex)}

          {text.substring(startIndex, startIndex + keyword.length)}

        {text.substring(startIndex + keyword.length)}

    ) : (
        {text}
      );
  };

keywordFilter = (nodes, keyword) => {
    let newNodes = [];
    for (let n of nodes) {
        if(n.children) {
            const nextNodes = this.keywordFilter(n.children, keyword);
            if (nextNodes.length > 0) {
            n.children = nextNodes;
            } else if (n.label.toLowerCase().includes(keyword.toLowerCase())) {
            n.children = nextNodes.length > 0 ? nextNodes : [];
            }
            if (
            nextNodes.length > 0 ||
            n.label.toLowerCase().includes(keyword.toLowerCase())
            ) {
            n.label = this.getHighlightText(n.label, keyword);
            newNodes.push(n);
            }
        }else {
            if(n.label.toLowerCase().includes(keyword.toLowerCase())) {
                n.label = this.getHighlightText(n.label, keyword);
                newNodes.push(n);
            }
        }
    }
    return newNodes;
};

getAllValuesFromNodes = (nodes, firstLevel) => {
    if (firstLevel) {
        const values = [];
        for (let n of nodes) {
            values.push(n.value);
            if(n.children) {
                values.push(...this.getAllValuesFromNodes(n.children, false));
            }
        }
        return values;
    } else {
        const values = [];
        for (let n of nodes) {
            values.push(n.value);
            if(n.children) {
                values.push(...this.getAllValuesFromNodes(n.children, false));
            }
        }
        return values;
    }
};

shouldComponentUpdate(nextProps, nextState) {
    if(this.state.keyword !== nextState.keyword) {
        return true;
    }
    if ( !lodash.isEqual(this.state.checked, nextState.checked)) {
        return true;
    }
    if ( !lodash.isEqual(this.state.expanded, nextState.expanded)) {
        return true;
    }
    return true;
}

render () {
    let searchedNodes = this.state.keyword.trim()
  ? this.keywordFilter(lodash.cloneDeep(nodesData), this.state.keyword)
  : nodesData;
    return (

                <Input style={{marginBottom:"10px", width:"100%"}}
                        icon="fas fa-search"
                        placeholder="Search Categories"
                        iconPosition="left"
                        onChange={(event, data) => {
                            this.onSearchInputChange(event, data, searchedNodes);
                        }}
                        className="Change"
                />

                <CheckboxTree
                        nodes={searchedNodes}
                        checked={this.state.checked}
                        expanded={this.state.expanded}
                        onCheck={checked => this.setState({ checked })}
                        onExpand={expanded => this.setState({ expanded })}
                        expandOnClick
                        onClick = { () => { console.log("clicked"); }}
                        showNodeIcon={false}
                        icons={{
                            expandClose: ,
                            expandOpen: ,
                        }}
                        nameAsArray={true}
                />

               <form class="butt">
                    <button type="button" onClick={this.cancel}>Cancel</button>
                    <button  onClick={this.pushOnArray}>Apply</button>
               </form>

    )
}

}
Thanks in advance!
Comment: It's hard to tell for sure based on what you provided but basically you should set state property checked to this.props.checked
Comment: @Moonjist can you provide me a little example?
Comment: To provide solution to your problem I would like to know few things:
1. What props come in from parent.
2. What do you mean by 'I want to remove all the last selected values.'
Comment: Please I will send in private the full description
Comment: Nah, describe it here if you want help
Comment: For example I check some values and I save it to the empty array, after first save I want to check some other values. After check some other values I press cancel button, If I press cancel it must uncheck values that are not saved.
Comment: @Moonjsit For example I check some values and press apply button I save it to the empty array. If I click on cancel, reverts the changes selection back to the last applied selection.
Answer: So I don't know what your project setup is but the easiest way to go with what I know is:
export default class TreeViewParent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      checked: [],
  };
  onSave = (newCheckedValues) => {
    this.setState({checked: newCheckedValues})
    // perform action based on selected values here
  }
  render() {
    return <TreeView checked={this.state.checked} save={this.onSave} />
  }
}

// I am omitting parts of this component that are not related to the problem
export default class TreeView extends Component {
  constructor(props) {
    super(props);
    this.state = {
      checked: this.props.checked,
      expanded: [],
      keyword: "",
      pushOn:[],
    };
  }
  cancel = () => {
    this.props.save(this.props.checked)
    this.setState({checked: this.props.checked})
  }
  save = () => {this.props.save(this.state.checked)}
  render () {
    let searchedNodes = this.state.keyword.trim()
      ? this.keywordFilter(lodash.cloneDeep(nodesData), this.state.keyword)
      : nodesData;
    return (

            <Input style={{marginBottom:"10px", width:"100%"}}
              icon="fas fa-search"
              placeholder="Search Categories"
              iconPosition="left"
              onChange={(event, data) => {
                this.onSearchInputChange(event, data, searchedNodes);
              }}
              className="Change"
            />

            <CheckboxTree
              nodes={searchedNodes}
              checked={this.state.checked}
              expanded={this.state.expanded}
              onCheck={checked => this.setState({ checked })}
              onExpand={expanded => this.setState({ expanded })}
              expandOnClick
              onClick = { () => { console.log("clicked"); }}
              showNodeIcon={false}
              icons={{
                expandClose: ,
                expandOpen: ,
              }}
              nameAsArray={true}
            />

            <form class="butt">
              <button type="button" onClick={this.cancel}>Cancel</button>
              <button  onClick={this.pushOnArray}>Apply</button>
           </form>

     )
}

Additionally I see you use some "advanced" stuff like cloneDeep while you do not understand basics of React. I recommend you to read the react docs, cuz answer I provided is the entry level concept of react.

How to push checkbox values in a array?


Postgresql:exporting data from local database to remote server in csv

Question: I want to send data from my local postgresql to a remote server in csv form..
something similar to
copy (select * from table) to /home/ubuntu/a.csv with csv
But in place of local direcory, I want to take this csv dump in other server
Comment: Is not supported by `COPY` but there is some workarounds. Describe better you scenario. Are you using psql? Take a look here: http://dba.stackexchange.com/questions/36235/export-postgres-table-on-remote-server-to-csv-file-on-local-machine
Answer: As far as I know copy command reads/writes from local path/location. To generate the output file on the remote server, you need to use a script(bash/python) to execute copy command on remote server.
Comment: `copy` always writes the file on the **server**. Don't confuse it with psql's `\copy` which writes the file to the client.
Answer: Use the psql client's \copy feature; this does exactly what you want.


Numeric approximation for fitting a Gamma Distribution with a single parameter

Question: Given a series of $N$ observations $\left(x_1, \ldots, x_N\right)$ that follow a Gamma distribution with a single parameter, $ \text{Gamma}(k, k)$, what is the maximum likelihood estimate of $ k $?. It should be noted that $k$ is both a shape and scale parameter, not a rate parameter.
My analysis boiled down to approximating the solution for the following equation: $$ \ln k + \psi(k) = \frac{1}{N} \sum_{i = 1}^N \ln x_i + \frac{1}{Nk^2}\sum_{i = 1}^N x_i - 1 $$
How should I estimate the solution for this equation?
Comment: The derivation of the first-order condition for an ML estimate of $k$ is correct. We then usually let the computers do the rest (numerical iterative solution). What is it that you are looking for?
Comment: I was looking for a simple non-iterative approximation. For example, when performing maximum likelihood for a Gamma distribution with 2 independent parameters, one can estimate with a single expression: http://en.wikipedia.org/wiki/Gamma_distribution#Maximum_likelihood_estimation
Answer: Note that
$$\ln k + \psi(k) = E(\ln X) \approx \frac{1}{N} \sum_{i = 1}^N \ln x_i $$
Substituting in the equation for the MLE, you have
$$\frac{1}{N} \sum_{i = 1}^N \ln x_i = \frac{1}{N} \sum_{i = 1}^N \ln x_i + \frac{1}{Nk^2}\sum_{i = 1}^N x_i - 1 $$
$$\Rightarrow 1=\frac{1}{Nk^2}\sum_{i = 1}^N x_i \Rightarrow \hat k = \sqrt {\frac{1}{N}\sum_{i = 1}^N x_i }$$
Comment: Thank you. This was exactly the clever machination I was looking for.


Localization of Finitely Generated $k$-algebra is Catenary (Vakil FOAG Exercise 11.2.F)

Question: A ring $A$ is catenary if for any two prime ideals $\mathfrak{p} \subset \mathfrak{q}$ of $A$, any maximal chain of prime ideals between them has the same length. The exercise asks to show that any localization of finitely generated algebra over a field $k$ is catenary. Clearly the quotient and localization of a catenary ring is catenary (by the correspondence theorems, for quotient and localization respectively). Thus, the exercise easily reduced to the case where $A = k[x_1, \cdots, x_n]$.
Note that the exercise does not assume any knowledge of Cohen-Macaulay nor regular ring, but instead it is supposed to be an algebraic translation of the following geometric fact:

(Theorem 11.2.9) If $X$ is an irreducible $k$-variety with a irreducible closed subset $Y$ with generic point $\eta$, then $\dim X = \dim Y + \dim \mathcal{O}_{X, \eta}$

What I don't see is how to use this theorem. Using this theorem (with $X=\operatorname{Spec} A$), we have
$$\dim A = \dim V(\mathfrak{p}) + \dim A_\mathfrak{p}$$
$$\dim A = \dim V(\mathfrak{q}) + \dim A_\mathfrak{q}$$
But I'm stuck here. In particular, the dimension is defined as the supremum among the length of all chains, so how can we use it to say something about the exact length of all chains?
Answer: The key idea is to show that given a chain which is too short, we can always insert a prime somewhere in it and increase the length. I'll give you the geometric version first then talk about the translation to algebra.
It suffices to show that if $X$ is an irreducible $k$-variety, then the maximal proper irreducible $k$-subvarieties are of dimension $\dim X-1$. We may immediately reduce to the affine case $X=\operatorname{Spec} A$. If $Y=V(\mathfrak{p})$ is a maximal proper $k$-subvariety $\dim Y<\dim X-1$, then the local ring $A_\mathfrak{p}$ has dimension $>1$ by the theorem. But this means there is some ideal in $A_\mathfrak{p}$ sitting properly between $0$ and $\mathfrak{p}_\mathfrak{p}$ which lifts to a prime ideal between $0$ and $\mathfrak{p}$, so $Y$ wasn't maximal.
Translating to algebra, the point is that if we ever have a chain of prime ideals $$P_0\subset \cdots \subset P_m\subset R$$ with $m< \dim R$, then (after assuming $P_0$ is minimal and $P_m$ is maximal) there must be some place where $\dim V(P_i) = \dim R/P_i > \dim R/P_{i+1} + 1 = \dim V(P_{i+1})+1$. Then the work above shows that we can always insert a prime between $P_i$ and $P_{i+1}$, hence extending our chain.


Find the derivative of 9x^2 f(x)

Question: I don't think I fully understand the question. If I knew f(x) I would use the chain rule or product rule, but since I don't, how do I know what to do? Thanks for any help I appreciate it.
Comment: I'd suggest merely writing $f'(x)$ for $\dfrac d {dx} f(x)$
Answer: You can use the product rule for this provided $f$ is differentiable. Write your function as
$$9 \cdot x^2 \cdot f(x)$$
Then by linearity,
$$\frac{d}{dx} 9x^2f(x) = 9 \cdot \frac{d}{dx} \left( x^2 \cdot f(x) \right)$$
You can use the product rule here as normal.

Example: Say we want to find the derivative of $\sin(x)g(x)$ where $g$ is a differentiable function. By the product rule,
$$\frac{d}{dx} \sin(x)g(x) = \cos(x)g(x) + \sin(x)g'(x)$$

More generally, as long as $f,g$ are differentiable,
$$\frac{d}{dx} f(x)g(x) = f'(x)g(x) + f(x)g'(x)$$
In our example, we simply set $f(x) = \sin(x)$. In your case, you could set $g(x) = x^2$, as another means of looking at it.

Find the derivative of 9x^2 f(x)


Spring 3 validation: using messages.property entry inside errorArgs?

Question: what I'm looking for is very easy to explain:
I have a form which must support both italian and english. Let's suppose I have a First Name input text field. The form label will be:

for EN: "First Name"
for IT: "Nome"

what I want is a validation error message like these

for EN: "the First Name field is required"
for IT: "il campo Nome è richiesto"

please note I'm quoting the field name inside the error message.
I have the messages_en.properties with the lines:
errors.empty.field = the {0} field is required
registration.form.firstname = First Name

and of course the messages_it.properties with the lines:
errors.empty.field = il campo {0} è richiesto
registration.form.firstname = Nome

Now, inside my custom validator class I have
ValidationUtils.rejectIfEmpty(errors, "firstname", "errors.empty.field", new Object[]{"registration.form.firstname"}, "the First Name is required");

which doesn't works properly. The output is the "registration.form.username" string as it is. I'd like to inject the field name with the proper locale at runtime. Is there a way to do so?
Comment: sorry i wanted to delete my stupid question..but i miss the track of your question..could you delete your comment..
Comment: actually i asked this question at time when i do not know what message Source is..
Answer: This is one approach, there should be better one's out there though:
Inject the messageSource into the Controller or the validator:
@Autowired MessageSource messageSource

In your controllers @RequestMapping method, take in Locale as a parameter, Spring will populate it for you.
@RequestMapping(...)
public String myMethod(..., Locale locale){
   ValidationUtils.rejectIfEmpty(errors, "firstname", "errors.empty.field", new Object[]{messageSource.getMessage("registration.form.firstname", new Object[]{}, locale)}, "the First Name is required");
....
}

Update:
You can get a handle on Locale in Validator using LocaleContextHolder
Comment: there's thing I don't get. My CustomValidator class implements Validator form org.springframework.validation.Validator. CustomValidator must define a validate(Object target, Errors errors) which doesn't allow a Locale parameter. how can I pass the locale object to validator?
Comment: I have added an update, you can use `LocaleContextHolder.getLocale()` static call. It does feel a little round about, would recommend waiting for more answers to come in, there has to be a simpler way to do this.


iTunes Podcast RSS PHP Tracking - Not Downloading

Question: I am trying to track downloads of MP3 Podcasts in iTunes. I have created a simple codeignitor site which generates URLS like:
www.site.com/podcasts/download/[id].mp3
The script takes the ID out of the URL, checks it exists in the DB - Gets the URL of the files and then does the following:
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 01 Jan 2011 01:01:01 GMT");
header('Content-type: audio/mpeg');
header('Content-Disposition: attachment; filename=episode'.$id.'.mp3"');
readfile($this->podcast_model->get_strPodcastFileName());

If you hit the URL in a browser it works and the file starts to download, but when putting 1 link into iTunes it starts saying "Downloading 0%" but does not download?
The 1 thing to note is the method: $this->podcast_model->get_strPodcastFileName() returns a HTTP:// link and not a link to a file on the server, also the HTTP address is not on the same server as the script. Although this is working when downloading the files via the browser as I said above.
Does anyone have any ideas?
Thanks
James
Answer: I fixed it by instead of using readfile(...) I simply did a header 301 redirect to the URL like this:
header("HTTP/1.1 301 Moved Permanently" );
header("Location: http://www.url_to_mp3_file/1.mp3");

One point for anyone else who is trying this. I would suggest you do a check on database for previous downloads in last X minutes (I check again IP address, URL and timestamp) as some MP3/Audio players make several requests to buffer the file.
Hope this helps.


Self Study Qn on Exponential Distribution

Question: I am currently working on a self-study question relating to exponential distribution. I made an attempt but was not able to make sense of the answer. When I looked up the reference guide, it offered the answer at 0.1068.
Appreciate any guidance and advice on where has my method gone wrong. Many thanks.
Question

When a truck arrives at a checkpoint, each truck will be checked by customs agents. The times are exponentially distributed with a service rate of 10 trucks per hour. What is the probability that a truck requires less than 15 minutes but more than 10 minutes to be checked?

Attempt
Comment: Replace lambda value with 1/6 instead of 6 which is the mean. Wrong formula corresponds to wrong result!
Comment: @Epaminondas Thanks for the enlightenment. Clearly a very careless mistake by me. Thank you so much. I was wondering if you will be able to add your comment as an answer so that I may mark it as the answer to my question and give due credit? Thanks
Comment: With great pleasure. Best regards!
Answer: Replace lambda value with 1/6 instead of 6 which is the mean. Wrong formula corresponds to wrong result!


Encrypting only specific tables / fields of an SQLite database

Question: Is it possible?
I read about SQLCipher, but it seems to encrypt the whole database.
Is there a way to configure it to encrypt only specific parts of it?
A table or a field will do just fine.
Comment: Android is quite secure regarding SQLite. From what do you want to keep data encrypted?
Comment: Why not encrypt the whole database? I've used SQLCipher it's pretty quick. The only downside is it adds 4MB to .apk size
Comment: I just want to store the app client's ID and secret provided by Facebook securely.
Answer: In the end I didn't need to use SQLite or any other techniques for encryption, because I found out I don't have to store the app client's secret at all, just the ID which is public.
Answer: You could use expand the DBHelper and run certain queries through a simple encryption util like the one noted in this answer.
Or rip out the CrypoHelper from android-passwordsafe written by Steven Osborn.


Is it "moderator" and "independent variable (predictor)" interchangeable?

Question: I'm reading the answer to this question: "Moderation" versus "interaction"?
The first answer is

"The way to think about what an interaction is, is that if you were to
  explain your findings to someone you would use the word 'depends'. I
  will make up a story using your variables (I have no way of knowing if
  this is accurate or even plausible): Lets say someone asks you, "if
  people research a product, do they purchase it?" You might respond,
  "Well, it depends. For men, if they research a product, they typically
  end up buying one, but women enjoy looking at and thinking about
  products for its own sake; often, a woman will research a product, but
  have no intention of buying it. So, the relationship between
  researching a product and buying that product depends on sex." In this
  story, there is an interaction between product research and sex, or
  sex moderates the relationship between research and purchasing.
  (Again, I don't know if this story is remotely correct, and I hope no
  one is offended by it. I only use men and women because that's in the
  question. I don't mean to push any stereotypes.)"

I'm wondering whether the moderator and predictor are interchangeable? Can we say the search moderates the relationship between sex and purchasing?
If they are interchangeable, I'm very confused about the picture like this (using the software WarpPLS):

(Source: http://www.icommercecentral.com/open-access/the-moderating-role-of-risk-security-and-trust-applied-to-thetam-model-in-the-offer-of-banking-financial-services-in-canada.php?aid=37979)
Why in this picture, they put Risk as moderator to that two arrows? Why not the other way around? Is that marketing research is more about making up stories rather than "show me the data"?
Answer:
I'm wondering whether the moderator and predictor are interchangeable?

The math beyond moderation is a simple interaction: y ~ x1 + x2 + x1*x2. That means, mathematically, the predictor (IV) and moderator (M) can be seen as mathematically interchangeable. A lot of times, people rely on theory to tell what the primary variable of interest is (IV) and the moderator that the relationship between the IV and DV depends on. When I analyze interactions in my work, I probe the interactions in both ways. First, I look as if x1 is the IV and x2 is the moderator; then, I look as if x2 is the IV and x1 is the moderator.
But because the interaction term is simply multiplication, they are mathematically the same.

Why in this picture, they put Risk as moderator to that two arrows? Why not the other way around?

They did not do it the other way around because of their theoretical knowledge. They know that ease of use increases perceived usefulness and attitudes toward using, which in turn increase intention to use.
They seem to be looking at: Well, what about risk? Where does attitudes about risk come in? They show that the relationship between the mediators perceived use and attitudes toward using) depend on risk.

Is that marketing research is more about making up stories rather than "show me the data"?

This is a loaded question, but one that is very good to ask. Often these "path models" imply that there is some causal effect going on. However, they are often done in cross-sectional data. That means that there are other ways to arrange these variables that might yield similar model fit.
Here lies the value of replication: If someone can show that this model holds with different participants in a different situation and/or using slightly different methods that represent the same hypothetical constructs, than it adds to their argument.
It would be even better if they, in a series of experiments, manipulated the independent variables and/or moderators of interest to see if the relationships hold.
Answer: Moderation is interaction. In the answer you quoted, not only does sex moderate the relationship between research and purchasing, but research moderates the relationship between sex and purchasing.
The diagram appears to not be one of moderation but mediation.
Comment: @PeterFlom there is interaction in this diagram. In psychology at least, an arrow pointing perpendicular to another path represents moderation. (example: http://afhayes.com/public/templates.pdf). So this diagram shows that risk is moderating the relationship between attitudes towards using and intention to use as well as perceived usefulness and intention to use.
Comment: Thanks for the confirmation that they are interchangeable. Do you mean the attitude towards using ... in the diagram is mediation? Is the Risk in the diagram is a moderation?
Comment: There doesn't seem to be any moderation in the diagram.  And yes, attitude toward using is shown as a mediator.
Comment: @MarkWhite good point.


CUDA: CPU code in parallel to GPU code

Question: I have a program where I do a bunch of calculations on GPU, then I do memory operations with those results on CPU, then I take the next batch if data and do the same all over. Now it would be a lot faster if I could do the first set of calculations and then start with the second batch whilst my CPU churned away at the memory operations. How would I do that?
Comment: "memory operations" - you mean the transfer of the data to/from the card, or something independent of the card? Don't you just need one thread to manage the card then you can do whatever you like with the rest of the CPU?
Comment: Well both I guess. I transfer the results to the host and then do something with the data, so it's not really independent.
Answer: All CUDA kernel calls (e.g. function<<<blocks, threads>>>()) are asynchronous -- they return control immediately to the calling host thread.  Therefore you can always perform CPU work in parallel with GPU work just by putting the CPU work after the kernel call.
If you also need to transfer data from GPU to CPU at the same time, you will need a GPU that has the deviceOverlap field set to true (check using cudaGetDeviceProperties()), and you need to use cudaMemcpyAsync() from a separate CUDA stream.
There are examples to demonstrate this functionality in the NVIDIA CUDA SDK -- For example the "simpleStreams" and "asyncAPI" examples.
Comment: Ok, thanks didnt know that. In that case if i have:

`<<>myCUDAfunc();
cudaMemcpy();`

How do I know that myCUDAfunc() has finished before cudaMemcpy starts copying the results back?
Comment: There are functions to synchronize like cudaDeviceSynchronize, cudaStreamSynchronize and cudaEventSynchronize. This is pretty easy to use but you should check the CUDA C Programming Guide from nVidia available in the CUDA downloads page. It's only 187 pages and gives you much needed information on working with CUDA.
Comment: @user810045, to be clear: CUDA has the concept of "streams".  A stream is a sequence of CUDA runtime API commands that are executed in the order they are issued.  If you don't specify a stream identifier to a kernel (the optional 4th parameter in the `<<<>>>`), it runs in the default stream, which is always synchronous with other default stream CUDA commands.  `cudaMemcpy`, which is also synchronous, will always therefore execute after the kernel if it is called after the kernel.  If you want an asynchronous copy, you have to use cudaMemcpyAsync, which requires a stream identifier argument.
Answer: The basic idea can be something like this:

Do 1st batch of calculations on GPU
Enter a loop: {
Copy results from device mem to host mem
Do next batch of calculations in GPU (the kernel launch is assynchronous and the control returns immediately to the CPU)
Process results of the previous iteration on CPU
}
Copy results from last iteration from device mem to host mem
Process results of last iteration

You can get finer control over asynchronous work between CPU and GPU by using cudaMemcpyAsync, cudaStream and cudaEvent.
As @harrism said you need your device to support deviceOverlap to do memory transfers and execute kernels at the same time but even if it does not have that option you can at least execute a kernel asynchronously with other computations on the CPU.
edit: deviceOverlap has been deprecated, one should use asyncEngineCount property.
Comment: Yup jmsu, that's exactly what I want to do. Glad to know that will work :)

CUDA: CPU code in parallel to GPU code


Why does the Null Hypothesis have to be "equals to" and not "greater than or equal to"?

Question: In an online lecture I saw an example of hypothesis testing:

The mean (of something) is at least 12
Claim: $\mu \geq 12 \implies H_0: \mu = 12$
Opposite: $\mu \lt 12 \implies H_A: \mu < 12$

And the lecturer insisted that we must put the $=$ and not $\geq$ in the $H_0$. But why?
Furthermore, if we end up rejecting $H_0$, sure it could mean that $\mu < 12$, but doesn't that also mean the it's possible that $\mu > 12$ ?
Answer: The reason for using $H_0: \mu = 12$ is that, among the set of values that correspond to $\mu \geq 12$, $\mu = 12$ is the most conservative (also called least favorable) configuration.
Let us be more precise what conservative means here. Say we set certain value of the observed statistic $\hat{\mu}$ at which we are willing to consider the null hypothesis as false (also called critical value $c$). $c$ should naturally be smaller than 12 to provide evidence against the null. Since $\hat{\mu}$ is just one of many possible realizations of the statistic, there is always some possibility of observing a value of $\hat{\mu}$ even if $\mu \geq 12$. Luckily, if we know the distribution of the test statistic, wecan compute the probability of observing a value of $\hat{\mu}$ that is smaller than or equal to $c$. This probability is called the probability of Type 1 error.
You can compute the probability of Type 1 error for all configurations that correspond to the hypothesis $\mu \geq 12$. In the figure I plot the distributions of the test statistics under two different such configurations $C_1: \mu = 12$ and $C_2: \mu = 13$. I also plot the probabilities of Type 1 error under the critical value $10.36$ for the two hypotheses (the shaded area under the respective curve) . It is easy to see that the probability of Type 1 error is always bigger for the configuration $C_1: \mu = 12$ than for any other $C_2$ that would also correspond to the hypothesis $\mu \geq 12$. I assumed normality here, but this result holds for any distribution that the test statistic can take.
To sum up, the common practice (which also makes a lot of sense!) is to choose, within the set of configurations of the test that correspond to the null hypothesis, the one that gives you the highest probability of Type 1 error.
Comment: @bluejimmy, The point was that shifting the red distribution to the right in the graph can only reduce the shaded area (which corresponds to the probability of rejecting under $H_0$ at the fixed critical value of slightly more than $10$). This does not depend on the distribution being normal. (And yes, you can use test statistics outside of the normal distribution. The normal distribution is so widespread for test statistics because it is often hard to get the exact distribution of a test statistic, in which case we can use a normal approximation).
Comment: Sorry, I can't understand the sentences with "Say we set certain value" and "of observing a value of $\hat{\mu}$"
Comment: Fill in "Say we set a critical value c." and "Since $\hat{\mu}$ is a random variable..."
Comment: @MatthiasSchmidtblaicher What does " I assumed normality here, but this result holds for any distribution that the test statistic can take." mean? I thought that z and t test statistics only work with normal distributions like mean and proportion? Is there other uses of test statistics outside of normal distribution data?


How to remove decimal values display from form field that is automatically coming from backend

Question: In my form field i want to enter only integer values. i am using ruby moneyrails gem. my field name in db is amount_paisas. so when it is displayng in form it shows 0.00 . i need only 0 to dispaly there. how to avoid decimal values . i tried to give placeholder but no changes.
This is what iam getting in form display.here i need to avoid values after point

This value is automatically coming from backend. how to remove this?
my input field
= f.input :amount, label: 'Amount'+'('+@cart.currency+')', :input_html=>{:'data-validation' => 'required', :'data-validation-error-msg' => 'Please Select Amount'}
here iam not giving any value. but it is automatically getting 0.00 as its placeholder value from  backend. this i need to avoid.
when i inspectin this form field  i am getting like this
<input data-validation="required" data-validation-error-msg="Please Select Amount" class="string optional form-control valid" type="text" value="0.00" name="e_gift_card[amount]" id="e_gift_card_amount">
Any help is appreciatable ..
Answer: You have 2 options here:
Configure default formatting
Create config/initializers/money.rb file with the following content:
MoneyRails.configure do |config|

  config.default_format = {
    :no_cents_if_whole => true,
  #  :symbol => nil,
  #  :sign_before_symbol => nil
  }

end

This configuration won't output cents if you have whole amount, i.e. $5 instead of $5.00.
Use custom helpers
If you don't want your cents at all, just use custom helpers:
money_without_cents(@money_object) # => 6
money_without_cents_and_with_symbol(@money_object) # => $6

To use it in your ruby code:
= f.input :amount, label: 'Amount'+'('+@cart.currency+')', :input_html=>{ :value => money_without_cents(@cart.amount), :'data-validation' => 'required', :'data-validation-error-msg' => 'Please Select Amount'}

Comment: Try moving my `value: money_without_cents(@cart.amount)` to `input_html { }` block
Comment: This is my form input field,appearing like a placeholder value. if i use money_without_cents(@money_object) then i will get dispay value that it fetches from database. but i require here is form input value to change
Comment: If you just want it to fill with default value without using any existing `@money_object`, such as `0`, well, just use `0` without any dots: ``
Comment: my form field is in ruby.like this . here iam not using any value field. `= f.input :amount, label: 'Amount'+'('+@cart.currency+')', :input_html=>{:'data-validation' => 'required', :'data-validation-error-msg' => 'Please Select Amount'}`
Comment: please edit your answer above. so that i can accept it


How to scroll the result of PHP-Postgres SELECT FROM query?

Question: I call a simple PHP-Postgres database lookup function from jQuery.  I use a SELECT FROM query in  Postgres. I want to verify that the input password matches the password in the record and return yes or no to jQuery.  For that, I need to scroll through the result of the query.
Here's the part of the PHP program that does the query:
// Query the database

    $email_field = $_POST['email_field'];
    $password = $_POST['password'];

try {

$email_field = $_POST['email_field'];
$password = $_POST['password'];

$data = [
    'email_field' => $email_field,
];

$sql = "select * from tbl01 where email=:email_field";

$stmt= $pdo->prepare($sql,[PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL]);
$stmt->execute($data);

echo "Statement selected";

print_r($stmt->errorInfo());

$no=$stmt->rowCount();
echo " No of records = ".$no;
echo PHP_EOL;

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
   echo $row;
   echo PHP_EOL; }

} catch (PDOException $e) {
    error_log($e->getMessage());
}

$pdo = null;

?>

The lines above execute correctly.  The echo "Statement selected"; shows in the dev console, but no echo from:
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo $row; }

So my question is how do I retrieve the rows (one by one) from this query?  I know there is one row in the table that matches the query I am sending because I can see it on the Putty command line when I issue the query directly.
I think this is an elementary question, but I haven't found the answer.
Thanks for any help.
EDIT:  per comment from Nick (below), this is the error returned by print_r($stmt->errorInfo());
Statement selectedArray
(
    [0] => 42703
    [1] => 7
    [2] => ERROR:  column "email_field" does not exist
LINE 1: ...SOR WITH HOLD FOR select * from tbl01 where email=email_fiel...
                                                        ^
)

Latest message, finally it succeeded thanks to Nick (see below):
Array
(
    [0] => 00000
    [1] =>
    [2] =>
)
1
Array
(
    [date] => 2019-08-27
    [0] => 2019-08-27
    [email] => email@example.com
    [1] => email@example.com
    [firstname] => Janis
    [2] => Janis
    [lastname] => Joplin
    [3] => Joplin
    [password] => pwdph
    [4] => pwdph
    [comments] => pwdph
    [5] => pwdph
    [sendupdates] => Yes
    [6] => Yes
)

Comment: Why are you sending the entire row as output? At a minimum you're going to be leaking password hashes, to say nothing of what other PII might be in there.
Comment: I'm testing and developing right now.  I'll deal with security after I resolve this issue.
Answer: There's a problem with your query string, you are not declaring the parameter correctly, it should be :email_field:
$sql = "select * from tbl01 where email=:email_field";

Also you have too many variables in the array being passed to execute. Only values actually used in the query should be in there, so remove the password value.
Note also that $row in while($row = $stmt->fetch(PDO::FETCH_ASSOC)) is an array, so you will need to print_r or var_dump it, echo $row; will simply output Array.
Comment: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/198620/discussion-on-answer-by-nick-how-to-scroll-the-result-of-php-postgres-select-fro).


Propostional Functions

Question:
Let $P$ stand for the set of people and let $p \in P$. $C(p)$ is a propositional function that is true when person $p$ plays cricket; $R(p)$ is a propositional function that is true when $p$ plays rugby; and $F(p)$ is true when $p$ plays football. Formalise the following statements:
1) Someone plays football and rugby.
2) No-one[sic] plays rugby and cricket.
3) If someone plays cricket, then that person also plays either football or rugby.
4) Everyone either plays cricket and football, or they play no sport at all.

Can someone please give me answers to this so that I can see if I am right or not? And if possible explain like one of the questions how they got there?
Cant post on here my answer as i cant find symbols and notations
if someone could provide the answers it would be helpful, not that I haven't put the effort in, because obviously I am going to do the exam and there is no point in just getting the answers, I just need to see if I am right, will take too much time having to add it in on a computer, much appreciated.
I am new to this so I apologise.
Comment: don't just post a link. This doesn't show any of your effort, you've surely invested already...
Comment: Sorry it is just hard to type the notation in
Comment: Go here http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference
Comment: Hmm, seems complicated, if someone could provide the answers it would be helpful, not that I haven't put the effort in, because obviously I am going to do the exam and there is no point in just getting the answers, I just need to see if I am right, will take too much time having to add it in on a computer, much appreciated
Answer: I'm assuming you're getting your chops busted because you're asking for others to put in effort to answer your question but you're unwilling to put in the effort to ask your question in a way that is helpful. Rather than being able to type an answer while looking at the question, I have to have another window open that I'm tabbing back and forth between. So, for others, here is the original question:

Let $P$ stand for the set of people and let $p \in P$. $C(p)$ is a propositional function that is true when person $p$ plays cricket; $R(p)$ is a propositional function that is true when $p$ plays rugby; and $F(p)$ is true when $p$ plays football. Formalise the following statements:
1) Someone plays football and rugby.
2) No-one[sic] plays rugby and cricket.
3) If someone plays cricket, then that person also plays either football or rugby.
4) Everyone either plays cricket and football, or they play no sport at all.

That said, the following is a way to answer the questions, but certainly not the only way (there are certainly other answers that may look different but are logically equivalent). Also, I don't like my answer to #4; I might have to rethink it.

$\exists p \in P: F(p) \wedge R(p)$
$\forall p \in P: \neg (R(p) \wedge C(p)$
$\forall p \in P: C(p) \implies F(p) \vee R(p)$
$\forall p \in P: (C(p) \wedge F(p)) \vee \neg(C(p) \vee R(p) \vee F(p))$
Comment: Sorry i really do apologize for not giving enough detail in my question, it's just that I am new to this and can't see how the symbols are added, now I can use these symbols by copying and pasting next time, yeah the fourth one i have a different answer, I probably did it wrong, going to try do it again.

Propostional Functions


When is a penalty enforced by half the distance to goal in american football

Question: When getting close to the goal line in american football a penalty is enforced by half the distance to goal.
From what yards does that start or does that depend on the penalty (5/10/15 yards)?
Answer: This penalty situation will vary depending on the penalty, and where the penalty is enforced.
Long and short of it: if the Penalty distance is more than half the distance from the spot the foul is enforced to the offender's goal line, it is "Half distance to the Goal".
So, for example, if there was an Offensive Holding call, and the offence is on their own 19 yard line. Normally this is a 10 yard penalty, but because they are inside the 20 yard line (10 yards > (19 yards / 2)) this would be given as a "half-distance" penalty.
From the 2019 NFL Rulebook Rule 14.2.1:

ARTICLE 1. HALF-DISTANCE PENALTY
If the enforcement of a distance penalty would move the ball more than half the distance from the spot of enforcement to the offender's goal line, the penalty shall be half the distance from the spot of enforcement to its goal line. This general rule supersedes any other general or specific enforcement of a distance penalty.

This would also be called if there is Defensive Pass Interference inside the end-zone, and the line of scrimmage is inside the 2 yard line.

Penalty: For pass interference by the defense: [...] If the interference is behind the defensive goal line, it is first down for the offensive team on the defense's one-yard line, or, if the previous spot was inside the two-yard line, halfway between the previous spot and the goal line.


Squealing string when muting after strumming

Question: I have a Jackson guitar with a floyd rose.
Recently I have changed the tuning from C# standard to E standard which involved a lot of screwing of the springs which hold the floyd rose mechanism.
So after that and changing strings, a weird squealing noise started to appear when I strum the 5th string and then immediately mute it with my palm. An example of that is in the following SoundCloud link:
SoundCloud
This also happens when the guitar is not connected to an amp and the other strings seem to be OK.
Does anyone know what's going on?
Answer: On the assumption it only happens on the open A string - and possibly other A notes, it sounds like one of the springs just happens to be at a tension which makes it vibrate sympathetically with that pitch.
Solution: change the tension of the springs very slightly so that none will produce that afternoise.
Comment: Another simple thing to try to remove this possible "wolf tone" is to wrap a rubber band around the springs
Comment: @DoktorMayhem - yes, I did consider a piece of sponge around the springs, if the tension is *exactly* what's needed, but I'm certain that a particular tension isn't that critical. And, by damping the springs, wolf tones are eliminated on all pitches.
Comment: @Tim I'll try that as soon as i get home and report the results, thanks!
Comment: @Tim I've tried to change the spring tension (both tightening and loosening) and still i get this sound. I should note that other A notes are fine - It only happens with the A string.
I've tried to hold the spring in question with my hand while strumming the string and the noise did stop. I'll try the dampening method next. Thanks.
Comment: Yo are talking about the springs that are connected to the claws inside the body? Which generally will be 2 or 3 for the whole block.
Comment: @Tim Yeah, there are 3 springs, the top most is making that noise.
Comment: I think there are several options for placing the springs. Might be worth flicking the offending one out and re-locating it.
Comment: @Tim I've relocated the offending spring as suggested and it worked, no more noise on any string or note. Thanks.


How to correct for sampling bias in one population when comparing against another

Question: I have two populations that I'd like to compare across certain metrics. However, most members of population A did not respond to our request for data, and those respondents that did are not representative of the whole.
On the other hand, we have data for every member of population B.
I have trained ML models to determine which characteristics are most important to discern between those members of population A that did respond vs. those that didn't. How can I apply this information to correct for this sampling bias when I compare the data from population A to the data from population B?
My current plan is to apply my ML model to all members of population B and see whether or not we'd predict them to have provided data if they had been members of population A. I'd subset all responses from population B to only those members that are predicted to have provided data. Is this a valid approach?
Comment: Just to clarify, when you say "most members of population A did not respond to our request for data", do you mean that they did not respond to some questions (but did answer some others)? Or do you mean that they did not respond at all to your survey?
Comment: Any members of population A who did respond gave answers to all questions :)
Comment: unfortunately I don't have the time to enter into the details of your case, but you should definitely look into survey weighting methods. Look in particular for the term "unit non-response". A couple of resources you might find useful: https://bookdown.org/jespasareig/Book_How_to_weight_a_survey/nonresponse-weights.html  https://cran.r-project.org/web/packages/IRexamples/vignettes/Ex-02-Adjusting-For-Survey-Non-Response-Using-Weights.html
Answer: After further research, I realized this is similar to propensity score matching.
However, I should not use the subset of all members of population B that the ML model predicts as positive responses (i.e. they look similar to members of population A). Instead, I should run all members of both population A and B through the model and match on the "propensity score", which is the probability output of the model.


critical region of a binomial population

Question: I have the following homework problem:
The number of successes in $n$ trials is to be used to
test the null hypothesis that the parameter $\theta$ of a binomial population equals 0.5 against the alternative that it does not equal 0.5.
a) find the likelihood ratio statistic.
b) use the result of part (a) to show that the critical region of the likelihood ratio test can be written as $x·lnx+(n−x)·ln(n−x) \geq K$.
Here's my attempt:
a)
$\lambda =\frac{0.5^{n}}{(\frac{x}{n})^{x}(1-\frac{x}{n})^{n-x}}$ where I got the denominator from the fact that $\theta _{mle}=\frac{x}{n}$
b)
the critical region is $\frac{0.5^{n}}{(\frac{x}{n})^{x}(1-\frac{x}{n})^{n-x}}\leq k$ which implies that ${(\frac{x}{n})^{x}(1-\frac{x}{n})^{n-x}}\leq \frac{0.5^{n}}{k}$. Then I tried taking the $log$ of both sides but the $log$ of the left side does not match what the question is asking for. Can anyone help?
Answer: Hint:  If $n = 20,$ then ${(\frac{x}{n})^{x}(1-\frac{x}{n})^{n-x}}$ is
minimized when $x/n = 1/2$ or $x = 10.$ Computation in R:
<code>n = 20;  x = 1:(n-1);  p = x/n
fcn = p^x*(1-p)^{n-x}
plot(x, log(fcn), type="b")
</code>


Public SQLite Database for android users

Question:
If I create an android database using SQLite it will be local, just for the phone in which the database is stored?
And how can I create a database which could be used and updated from different phones? For example, I have database with friends online and if someone turns on the program then it stores an entry in table which is seen by all of the users friends?
Maybe, some tutorials on creating remote database (if included database is not public).
I searched google, but the only thing I found was simple database tutorials, which, as I suppose, are stored in phones memory and that means accessable by just that particular phone.
Thank You in advance,
Daniels
Answer: If you wanted to have a remote database that multiple people could connect to you would have to set up something like MySQL or similar. At that point, you would have to set up some sort of API that you could connect through, then you have to worry about access control as well.
The most common one I see around is a PHP frontend so that the entire database connection runs over HTTP example here : PHP API
Comment: PHP file must be placed in remote server too? And this looks like quite easy to use variant, so thank you :)
Comment: Yep, or at least, that's the easiest way to do it.
Answer: I believe there is a way to do what you asked. You can create a web service that will communicate with your local database and store the data of your local db in an online db using synchronization. The online db doesnt need to be in SQLite.


Building a preconditioned iterative method

Question: Consider a preconditioned iterative method of least residuals for solving a system of linear equations $Ax=b$ with positive definite matrix $A$:
$$
\tag{1}
r_n = b - A x_n; \\
\alpha_n = \frac{(A M^{-1} r_n, r_n)}{(A M^{-1} r_n, A M^{-1} r_n)}; \\
x_{n+1} = x_n - \alpha_n M^{-1} r_n.
$$
Its non-preconditioned version (when $M = E$) looks like this:
$$
\tag{2}
r_n = b - A x_n; \\
\alpha_n = \frac{(A r_n, r_n)}{(A r_n, A r_n)}; \\
x_{n+1} = x_n - \alpha_n r_n.
$$
Let's write out an algorithm (2) for the system $A' x = b'$, where $A' = M^{-1} A$, $b' = M^{-1} b$:
$$
\tag{3}
r'_n = b' - A' x_n = M^{-1} (b - A x_n) = M^{-1} r_n; \\
\alpha_n = \frac{(A' r'_n, r'_n)}{(A' r'_n, A' r'_n)} = \frac{(M^{-1} A M^{-1} r_n, M^{-1} r_n)}{(M^{-1} A M^{-1} r_n, M^{-1} A M^{-1} r_n)}; \\
x_{n+1} = x_n - \alpha_n r'_n = x_n - \alpha_n M^{-1} r_n.
$$
It can be seen that the formulas for $\alpha_n$ in (1) and in (3) differ. This suggests that there is a mistake somewhere in the reasoning, but I can't find it. Where am I wrong?
Perhaps there are no errors. But then equality
$$\frac{(M^{-1} x, M^{-1} y)}{(M^{-1} z, M^{-1} w)} = \frac{(x, y)}{(z, w)}$$
must be satisfied, but I don't understand how to prove it.
Some may have a question why I built (3) from (2) if I already had the algorithm (1) obtained in some way. The answer is simple: I wanted to make sure that the algorithm (1) was constructed correctly by constructing it in a different way.
Answer: There is an error in the update formula: it should be $x_{n+1} = x_n+\alpha_n r_n$ when the residual is defined with $x_n$ having the negative sign.
Otherwise, your reasoning is almost correct, except that in $(3)$ you wrote an algorithm for the left preconditioned system:
$$
M^{-1}Ax = M^{-1}b
$$
On the other hand, the formula in $(1)$ is for right preconditioned system:
$$
AM^{-1}Mx = b
$$
You can derive the formula with similar substition of variables, this time with $A'=AM^{-1}$ and $x'=Mx$. In this case, $r_n$ is unchanged, as
$$
\begin{aligned}
r_n &= b - A'x' \\
    &= b - AM^{-1}Mx \\
    &= b - Ax
\end{aligned}
$$
On the other hand, the step multiplier is
$$
\begin{aligned}
\alpha_n &= {(A'r_n,r_n)\over(A'r_n,A'r_n)} \\
         &= {(AM^{-1}r_n,r_n)\over(AM^{-1}r_n,AM^{-1}r_n)}
\end{aligned}
$$
while the update formula is
$$
\begin{aligned}
x'_{n+1} &= x'_n + \alpha_nr_n \\
Mx_{n+1} &= Mx_n + \alpha_nr_n \\
\therefore x_{n+1} &=  x_n + \alpha_nM^{-1}r_n \\
\end{aligned}
$$


Pytorch: How to concatenate lists within a tensor?

Question: I have a tensor of size (2, b, h) and I want to change it to the following size: (b, 2*h), where the corresponding lists are concatenated, for example:
a = torch.tensor([[[1, 2, 3], [4, 5, 6], [4, 4, 4]],
                  [[4, 5, 6], [7, 8, 9], [5, 5, 5]]])

I want:
b = tensor([[1, 2, 3, 4, 5, 6],
            [4, 5, 6, 7, 8, 9],
            [4, 4, 4, 5, 5, 5]])

Answer: Use permute first to change order of dimensions, then contiguous to prevent strides within the permuted tensor and finally use view to reshape the tensor.
b = a.permute(1,0,2).contiguous().view(a.shape[1],-1)

Answer: In order to concatenate tensors in pytorch, you can use the torch.cat function which concatenates tensors along a chosen axis.
In this example, you can do:
a = torch.tensor([[[1, 2, 3], [4, 5, 6], [4, 4, 4]],
                  [[4, 5, 6], [7, 8, 9], [5, 5, 5]]])

b = torch.cat((a[0], a[1]), dim=1)

Out:
tensor([[1, 2, 3, 4, 5, 6],
        [4, 5, 6, 7, 8, 9],
        [4, 4, 4, 5, 5, 5]])

Comment: Also doable. Might be harder to follow for the general case though. Furthermore, might be a bit slower than my proposition.


Executar arquivo Appimage em background no ubuntu

Question: Efetuei o download da aplicação cerebro para o ubuntu 16.04 porém para executa-lo eu sempre preciso abrir o terminal e escrever:
`./cerebro-0.2.6-x86_64.AppImage`

E então o software é aberto porém o terminal fica "travado" (sim eu sei que posso abrir outro com CTRL + ALT + T e outra aba com CTRL + SHIFT + T).
Tentei adicionar a tarefa para executar quando o S.O for carregado em gnone-session-preferences mas a aplicação precisa de permissão sudo para ser executada, logo isso gera um erro.
Como posso fazer para esse arquivo .AppImage seja executado em background e inicie toda vez que o S.O for iniciado também?
Answer: Você pode usar o upstart que é usado pra iniciar aplicativos no boot ou em determinados momentos do sistema:
# Start

description "Start App"

start on (net-device-up
          and local-filesystems)
script
<caminho>/cerebro-0.2.6-x86_64.AppImage
end script

Este modelo deve te atender, você deve criar um arquivo com a extenção .conf e colocar ele na pasta /etc/init.
Consulte a documentação pra saber mais : http://upstart.ubuntu.com/getting-started.html
Nota: Em algumas versões do ubuntu, o upstart se encontrava inabilitado, para habilitar, basta seguir este tutorial: https://www.maketecheasier.com/re-enable-upstart-ubuntu/


is the right approach to use the binary entropy function to get a $O(c^n)$ approximation?

Question: I just finished the implementation of an algorithm and wanted to report its approximate run-time in a O*($c^n$) format, the time analysis results is O*$n/2 \choose n/4$. I'm using the binary entropy function.
O*$n/2 \choose n/4$ $\approx$ O*($2^{\frac n 2 H(1/4)}$) $\approx$ O*($2^{\frac n 2 0.811}$) $\approx$ O*($1.324^n$)
Should I do this in a different way to do this accurately?
Answer: If you use the Stirling's approximation in the expression of the binomial O${n/2} \choose {n/4} $ for every factorial you come up with asymptotic complexity:
$$ \frac{2^{n/2}}{\sqrt{n}} $$
This is not asymptotic to any expression of type $c^n$. This would anyway be a very good form for the reported asymptotic limit.
Indeed, if you use the fact proved in this question (which I guess is what you used to get to your result), you obtain that
$$ {n/2 \choose n/4 } \sim 2^{(H(1/2)^{2n})} $$
which is different from what you stated (you can't bring the exponent of $H$ down as exponent of $2$).


Debiasing word embeddings

Question: I'm reading the paper titled "man to computer programmer is woman to homemaker. Debiasing word embeddings". I'm right now trying to figure out the math and logic behind it and was doing OK till I got to the neutralizing and equalizing part. Can anyone explain the equation in Step3?
I understand that it's some how taking the average of the bias directions out of the set's average and then for each word adds this debiased average to the debiased word vector, but I can't seem to get the math between them where it says $\sqrt{1-||v||^2}.$

Thanks in advance!
Answer: It looks like the term $\sqrt{1-||\nu||^2}$ was added in order to ensure that the norm of $\vec{w}$ is $1$.
Since $\nu$ and $\vec{w}_B-\mu_B$ are orthogonal, in the red-underlined formula you get $||\vec{w}||^2 = ||\nu||^2 + (1-||\nu||^2)\frac{||\vec{w}_B-\mu_B||^2}{||\vec{w}_B-\mu_B||^2} = 1$.

Debiasing word embeddings


The non-trivial zeros of $L(s, \chi)$ are symmetrically placed with respect to the critical line

Question: Let $\chi$ be a primitive Dirichlet character modulo $q$. My book says that "The non-trivial zeros of $L(s, \chi)$ are symmetrically placed with respect to the critical line $\sigma=1/2$". Now, if $\rho$ is a non-trivial zero of $L(s, \chi)$, then, by the functional equation, one has that $1-\rho$ is a zero of $L(s, \overline{\chi})$, and so $1-\overline{\rho}$ is a zero of $L(s, \chi)$ (since $\overline{L(s, \chi)}=L(\overline{s}, \overline{\chi})$). But, in general, $\rho$ and $1-\overline{\rho}$ are not symmetrically placed with respect to the critical line. Am I wrong?
Comment: This is why we write the functional equation as $\Lambda(s) = Q^{-s} \Gamma(s+a)L(s,\chi) = \overline{\Lambda(1-\overline{s})}$. It shows the non-trivial zeros come in pair $\sigma+it, 1-\sigma+it$ and that $\Lambda(1/2+it)$ is real for $t$ real
Comment: @reuns it is *not* true in your comment that $\Lambda(1/2 + it)$ is real for $t$ real for $L$-functions of nonquadratic characters.  You left out the root number $W(\chi)$ on the right side of the functional equation: this number need not be $1$.  When $u$ is a square root of $W(\chi)$, which is only well-defined up to a sign, $u\overline{u} = 1$ and the ratio $\Lambda(1/2 + it)/u$ is real for $t$ real.
Comment: @KCd Yes thank you I'm aware of that, makes sense to add the (square) root number into $\Lambda$
Answer: Write $\rho = \beta + i \gamma$. Then $1- \overline{\rho} = (1-\beta) + i \gamma$ is indeed the reflection of $\rho$ with respect to the critical line $\mathrm{Re}(s) = 1/2$.
Notice that if $\chi$ is a real character, then $L(\rho, \chi) = 0$ iff $1 - \rho$ is also a zero of $L(s, \chi)$ iff $\overline{\rho}$ is a zero of $L(s, \chi)$.


Ember Communication Between Components

Question: I'm developing a webapp to display and search documents. I've laid out the main wrapper div like this:

    {{searchComponent searchComponent=searchComponent}}
    {{pdfViewer pdfSearchComponent=searchComponent}}

This allows me to later add other types of viewers in there, like so:
{{otherViewer otherSearchComponent=searchComponent}}

The outer wrapper is an ember component as well. So it's controller looks like:
Ember.controller.extend({
   searchComponent: null,
   .
   .
   otherProperties,
   actions: { }
});

And the searching component binds itself on itialization, as inspired from this source: http://www.samselikoff.com/blog/getting-ember-components-to-respond-to-actions/
Ember.controller.extend({
    searchComponent: null,
    .
    .
    onStart: function(){
       this.searchComponent = this;
    }.on('init'),
    .
    .
    actions: {
        someAction: function() {
            //do something
        }
    }
}

So I can now reference the component from the main pdfViewer like this:
this.get('searchComponent').send('someAction')

To get the response, right now I bind another property to all the controllers / templates, and then watch for a change on that property in the viewer controller, after which I can place the results where they need to be.
Is there a way to send a 'message' from my 'pdfViewer' to my 'searchComponent' and receive a 'response' without explicitly binding them together, as above?
Comment: No, granted they are also tightly coupled in your implementation, and maybe one of them should be consuming the other instead of them being siblings. Or maybe the results from one of them should be bound as a property on the dependent component
Answer: You could consider using pub/sub through a Service event bus, in which your searchComponent and pdfViewer both emit and listen for messages, so can talk to each other. Sure, there is a dependency on the service, but from what I see your components are pretty application-specific anyway.
Something like:
_listen: function() {
  this.get('eventBus').on('message', this, 'handleMessage');
}.on('init'),

actions: {
  click() { this.get('eventBus').trigger('message', message); }
}

A few weeks ago I evaluated several approaches to parent-children component communication: http://emberigniter.com/parent-to-children-component-communication/, perhaps this helps somewhat.
Comment: I took a look at your post. It's an interesting read! Thanks for the solution!

Ember Communication Between Components


How to count when bass and treble clefs share notes or chords

Question: I'm totally new to piano. I have a background in guitar, programming and some math, therefore I tend to think a little in an "exact number of boxes" fashion or "an array of numbers" as in programming.
From the little I've learnt so far, I've become used to finding a complete subdivision of the time signature on each clef (bass and treble) such as this:

where it is very clear that both the bass clef can be divided into 8 eighth notes. The bass clef has 8 eighth notes and the treble clef has 8 eighth notes.
With some effort I could eventually figure out how to count and play this.
Now, in the following case:

I'm expecting to find 16 eighth notes on each clef, but I can't possibly figure out to match the notes+silences present to a 16 eighth note measure on each clef. The treble clef adds up to 12 eighth notes (4 eighths missing which are somehow equal to the bottom dotted quarter plus the subsequent eighth??).
The bass clef adds up to 10 eighth notes (6 eighths missing which are equal to the treble's eighth silence + subsequent eighth note chord + subsequent quarter note chord). You get the idea, and I assume the math is pointing somewhere very clear but I can't yet grasp how I would both clefs together.
I'm managing to play this part by listening to the recorded song, but without that aid I would have failed to figure out the proper rhythm.
My attempt at re-writing the bass and treble parts as complete 16 eighth note measures failed completely.
Comment: You're miscounting. There are eight eighths in both staves, as expected.
Comment: Thanks you guys for your answers. Sure I have this messed up in various ways. Yep, I was indeed confused by the overlaps. I was also messing up 8ths for 16ths because in some Spanish teaching methods 8th notes are called a "half time" note.
Comment: Maybe you've been confused by the fact that some of the notes "overlap"; e.g. the left hand changes halfway through a quarter note that the right hand is sustaining. You're on the right track to try writing it out as a measure full of eighth notes; perhaps post your attempt as a separate question and we can see what went wrong. (Also, why do we keep talking about "16 eighth notes on each clef"? There should be 8 in a measure.)
Comment: To put it in programming terms: the treble of the first example is an array that contains some null elements. The second example contains only one null element, but it contains some that occupy more than one of the smallest unit; in an Excel sense, they're like "merged cells." But the two arrays aren't dependent on each other. Make sure you're counting each one accurately independently before you try to merge them. The results of merging treble with bass are simply the results of executing both independently and simultaneously.
Answer: Why are you expecting to find 16 8th notes per stave in a 4/4 bar? 4/4 is 4 quarter notes per bar (or 8 8th notes or 16 16th notes, etc).
You are also miscounting. The staves are independent of each other and there are 8 8th notes in each stave, just like in your first example, which is probably also in 4/4. Rests count the same as notes, dotted notes (notes with a dot following them) are 1.5 times their value and tied notes are the sum of the two values. The dots and lines above or below the notes are staccato or legato articulations and do not affect the durations of the notes:
Treble clef: 1+1+2+2+1+1=8
Bass clef: 3+3+1+1=8
Also, regarding your title question, in this case the treble and bass staves do not share notes, although that is sometimes done in more complex piano music.
Comment: Thanks you guys for your answers. Sure I have this messed up in various ways. Yep, I was indeed confused by the overlaps. I was also messing up 8ths for 16ths because in some Spanish teaching methods 8th notes are called a "half time" note.
Comment: @geeheeb as I read your question I was expecting something a little different. Sometimes, a right hand note or a few will be written on the lower staff or a left hand note or two on the upper, and when this is done there is typically no rest written (because the hand is doing something; it's just written somewhere else). It's not strictly important to be able to consider each staff separately in adding up the time values in each bar; what matters is the overall part. Sometimes more than one logical voice is written on each staff, and in some pieces the number of logical voices is not constant.


Is there a name for this trope in music writing

Question: It's something I now hear everywhere, once I heard it a few times.
Take a song like Papa Roach - Between Angels And Insects. As the chord progresses in the first few riffs, there is a repeated melody fragment during each chord - F E G F E F. So you get a Dm overall chord plus this fragment, then Bb plus this fragment and so on until it starts repeating. In other songs the respective fragment doesn't stay the same throughout the entire progression.
To give an example in a completely different genre, take Мария ft. X - Сто нюанса розово - it introduces a melody fragment in the first chord, then reuses it verbatim in the second and fourth chord, but uses something else in the third chord, so it mixes the formula a bit.
It doesn't seem that the melody itself matters, only that it goes well with each chord in the progression. The overall feel is that repeating a melody fragment in several chords makes the melody feel more salient. The usage of repeated melody fragments while the bass changes seems to be a common occurrence in popular music and is apparently commonplace in "hooks" and choruses.
I want to find out what this trope is called and why it works.
Answer: The term Ostinato is usually used to indicate a repeated melodic phrase in classical (and other more "formal") music.  In rock, you'd usually just refer to it as a riff.  To me, the guitar riff in the Papa Roach example fits; but I don't quite hear the melodic aspect in the Russian(?) example.  Here's a piano example.
Here are some more rock based examples of involving a prominent ostinato
Comment: Your piano example does not really include changing the changing harmonies described by the OP. Otherwise, nice find of the term "ostinato".
Comment: The piano example most definitely changes harmonies... first chord I, second chord IV 6.4 and so on
Comment: A classic example, https://www.youtube.com/watch?v=CvXXm4RkLP0, with the ostinato in the bass (in this case, also known as a ground).
Answer: I don't know if there is a name for it, but I can explain why it works. (EDIT: Dave's "ostinato" seems plausible.)
The notes you describe (FEGFEF) seem to center around the F. Excuse the awkward english phrasing, I mean that all notes except the F are some kind of embellishment. In classical music terminology we would simply call them 'neighboring notes', since that's what they are.
If the Gs and Es are embellishments, that means we can ignore them as far as the harmony (or chord, in more popular terminology) is concerned. When we do this, it becomes clear that the F is a chordal tone in both D minor, and B flat major (extremely non-exhaustive list); that's what makes this 'trope' work.
Of course, this only works if the Fs are supported by the context, for example by the meter (all Fs on downbeats). I can also think of a case where the central note would be the E, in the same motive. In this case, though, you describe the motive as sounding well with two chords containing an F and not an E, so I think my assumption in the second paragraph is correct.
Answer: It seems that repetition is one of the factors that makes music in general sound "beautiful".
As a counterpoint to this argument, Scott Rickard presented the "World's ugliest music" piece,  (repetition-free, that arguably only a mathematician could write), while challenging the audience "to find something beautiful about this piece and revel in the fact that you can't".
Answer: The commonly-held rule of thumb describing predictability in music goes along the lines of, 'most people like music that is predictable half the time'. Then there's the corollary, 'sophisticated listeners like it less predictable and the more philistinic crave more predictability. Whether cultural variables are taken into account I do not know. Recent research, using an android phone app by Ekeus et al
http://webprojects.eecs.qmul.ac.uk/hekeus/stuff/papers/SMC.pdf appears to support the rule of thumb, if not necessarily the corollary. My Life Coach/Financial Adviser/noted tin ear/wife needs to hear a song three times before she can decide if she likes it or not. All this is by way of saying that predictability in terms of melody, rhythm and tone colour will counteract dissonance in the harmony of your example-for most people. Of course, repetition is the key to acceptance; how else could we accept stiletto heels or flared pants?


Concatenar arrays usando módulo numpy

Question: Uma dúvida, para a solução de sistemas lineares: como concatenar um array (matriz) A, um array (vetor coluna) b, de forma que se tenha a matriz "aumentada" do sistema, A~ = [A b], usando numpy?
Answer: Sugestão de solução (as matrizes de exemplo A e b poderiam ser criadas de forma mais automática, mas assim eu achei que o exemplo ficaria mais didático):
import numpy as np

A = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16] ])
b = np.array([17, 18, 19, 20])

# Cria uma nova matriz com 1 coluna a mais
Ab = np.zeros((A.shape[0], A.shape[1]+1))

# Copia a matriz original para a nova matriz
Ab[:,:-1] = A

# Copia o vetor, convertido em uma matriz de uma coluna, para a nova matriz
Ab[:,-1:] = b.reshape(A.shape[0], 1)

print('A:')
print(A)
print('')

print('b:')
print(b)
print('')

print('[A b]:')
print(Ab)

Saída desse código:
A:
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]

b:
[17 18 19 20]

[A b]:
[[  1.   2.   3.   4.  17.]
 [  5.   6.   7.   8.  18.]
 [  9.  10.  11.  12.  19.]
 [ 13.  14.  15.  16.  20.]]

Answer: Outra maneira é:
import numpy as np

a = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16] ])
b = np.array([[17, 18, 19, 20]])

out = np.concatenate((a,b.T), axis = 1)

Concatenar arrays usando módulo numpy


How to solve the PDF of lognormal distribution using the Normal Distribution

Question: Let $X$ be $N (\mu,\sigma^2)$.Define the random variable $Y=e^x$ and find its probability distribution function. My solution is this, let $G(y)= P(Y\le y)=P(e^x\le y) =P(X\le ln y)$.Let $f(x)=\frac{1}{\sigma\sqrt{2\pi}} \exp((x-\mu)^2/2\sigma^2))$.Thus,$$P(X\le ln y)=\int_{-\infty}^{\ln y} f(x)\,dx$$Please help me with the chain rule.
 How can find the pdf?Thank you
Answer: Hint: You have $P(X\leq ln(y))$. This is $F_x(ln(y))$. $F_x(ln(y))$ is the cdf of normal distribution with $X\sim \mathcal N(\mu_x,\sigma_x^2)$
To find $g_Y(y)$ you have to differentiate $F_x(ln(y))$. Don´t forget using the chain rule.
Comment: Please help me with the chain rule. thanks
Comment: It is $g_Y(y)=\frac{d}{dy}F_x(ln(y))=f_x(ln(y))\cdot \frac{1}{y}$, because $\left( ln(y)\right)'=\frac{1}{y}$
Answer: This follows by the change-of-variables formula for densities: if $X$ has density $f_X$ and $g$ is one-to-one, then $Y=g(X)$ has density
$$
f_Y(y)=\bigg|\frac{\mathrm d}{\mathrm d y}g^{-1}(y)\bigg| f_X(g^{-1}(y)).
$$


Why do i get a core dump error with my hash table when it exceeds a certain size?

Question: I implemented an algorithm that prints all positive integer solutions to equation a^3 + b^3 = c^3 + d^3 where a,b,c,d are integers between 1 and 1000.
To do so, I used a hash table to drop the complexity to O(N2). H = c^3 + d^3
Below is the hash table that i used (list of integer pairs):
int n = 10;
int m = pow(n,3)+pow(n,3);
list< pair<int,int> > hashTable[m+1]; //(*) Core Dump here if n>50. Why?

My code works correctly when n<=50. But I am unable to run my algorithm for n>50. So, I am unable to solve the problem for n=1000. I get a core dump in line (*). Can you explain why this is happening, and help me implement my hash table in a better way?
Thank you for your time!
Comment: what language is this written in?
Answer: The problem is that you tried to allocate hashTable array on stack, which has size limitations (couple of megabytes by default). With n=50 and sizeof(std::list<std::pair<int, int> >) = 16, required memory size is only 2MB. Larger than that -- and you run out of stack size.
To fix it, allocate hashTable array dynamically or use std::unordered_set instead of implementing your own hash table.
Comment: When you use the method `push_back()`, I think we allocate the memory dynamically. I don't understand your answer. Can you please be more specific?

**Note: I replaced `list` by `vector` but it did not work either.
Comment: The list object itself is allocated on stack (it contains pointers to the first and last nodes), nodes are allocated in dynamic memory. There is enough stack for 250K such lists (n=50), but not enough for 2M lists (n=100). You need to create list objects also in dynamic memory, for example by replacing array with vector: `std::vector > > hashTable(m+1);`
Comment: Thank you. It worked with vector of vector of pairs. I can allocate a bigger size of memory, but there is a limit. I have `bad_aloc` if i try to create a vector of size 2*1000^3
Comment: Do you really have so much free memory? 2*1000^3 elements would require 32GB on x86_64.
Comment: I have 8GB of RAM.


An illegal choice has been detected. Please contact the site administrator.in dynamic select list dependent dropdown

Question: using hook_form_alter and custom form elements below
$form['get-quotes-shipping']['country'] = array(
      '#type' => 'select',
      '#options' => array('_none' => '- None -') + country_get_list(),
      '#multiple' => FALSE,
      '#weight' => 3,
      '#title'=>t('Country'),
      '#default_value' => (isset($_SESSION['selected_country'])) ? $_SESSION['selected_country'] : '_none',
      '#ajax' => array(
          'event' => 'change',
            'wrapper' => 'squadron-wrapper',
            'callback' => '_get_state_field',
            'method' => 'replace',
       ),
    );

    $form['get-quotes-shipping2']['actions']['get_quotes'] = array(
      '#type' =>'submit',
      '#value' => 'Get Quotes',
       '#submit' => array('custom_changes_get_quotes_submit'),
      '#validate' => array('custom_changes_get_quotes_validate'),
      '#weight' => 1,
    );

  $squadron_options = array();
   if (isset($_SESSION['selected_country'])) {
       module_load_include('inc', 'location');
       $states = location_get_provinces($_SESSION['selected_country']);
       $squadron_options =  $states;
   }
   $form['get-quotes-shipping0']['states']['#type'] = 'select';
     $form['get-quotes-shipping0']['states']['#title'] = t('State/Province');

   $form['get-quotes-shipping0']['states']['#prefix'] = '
';
   $form['get-quotes-shipping0']['states']['#suffix'] = '
';
    $form['get-quotes-shipping0']['states']['#options'] = array('_none' => '- None -') + $squadron_options;
    if (isset($squadron_options[0])) {
      $form['get-quotes-shipping0']['states']['#default_value'] =  $squadron_options[0];
    }

Callback function as given below.
function _get_state_field(&$form,&$form_state) {
  module_load_include('inc', 'location');
  if ($form_state['values']['country'] == '_none') {
    unset($form['get-quotes-shipping0']['states']['#options']);
    $form['get-quotes-shipping0']['states']['#default_value'] = '_none';
     return $form['get-quotes-shipping0']['states'];

  }
  else {
      $states = location_get_provinces($form_state['values']['country']);
 $form['get-quotes-shipping0']['states']['#options'] = $states;
     return $form['get-quotes-shipping0']['states'];

  }

}

When I select Country and then state at first it gives proper values
but when I select country again it gives
An illegal choice has been detected
What is the issue with the above code?
can anyone point out?
Comment: Since you are unsetting `$form['get-quotes-shipping0']['states']['#options']`, that form element doesn't have anymore options; therefore, even the option you select after is correct, because it is not listed in `$form['get-quotes-shipping0']['states']['#options']`.
Comment: Thanks for the comments @kiamlalumno but even if I comment $form['get-quotes-shipping0']['states']['#options'] this it gives same error.I would appreciate your help again
Answer: i recently made a 3 level dependent dropdown and had the same issue with some of the problems i found on the way. First you need to re-organize your code a little bit more. have in mind that the callback function should normaly look something like this:
//callback
    function _get_state_field(&$form,&$form_state) {

     return $form['get-quotes-shipping0']['states'];
}

Any code inside the callback has the potential to intervene with the proccess, with some exceptions.
Without taking a longer look to your code i think it should look somethink like this:
<?php

 module_load_include('inc', 'location');
  if ($form_state['values']['country'] == '_none') {
    unset($form['get-quotes-shipping0']['states']['#options']);
    $form['get-quotes-shipping0']['states']['#default_value'] = '_none';
     return $form['get-quotes-shipping0']['states'];

  }
  else {
      $states = location_get_provinces($form_state['values']['country']);
 $form['get-quotes-shipping0']['states']['#options'] = $states;
  }

$form['get-quotes-shipping']['country'] = array(
      '#type' => 'select',
      '#options' => array('_none' => '- None -') + country_get_list(),
      '#multiple' => FALSE,
      '#weight' => 3,
      '#title'=>t('Country'),
      '#default_value' => (isset($_SESSION['selected_country'])) ? $_SESSION['selected_country'] : '_none',
      '#ajax' => array(
          'event' => 'change',
            'wrapper' => 'squadron-wrapper',
            'callback' => '_get_state_field',
            'method' => 'replace',
       ),
    );

    $form['get-quotes-shipping2']['actions']['get_quotes'] = array(
      '#type' =>'submit',
      '#value' => 'Get Quotes',
       '#submit' => array('custom_changes_get_quotes_submit'),
      '#validate' => array('custom_changes_get_quotes_validate'),
      '#weight' => 1,
    );

   $form['get-quotes-shipping0']['states']['#type'] = 'select';
     $form['get-quotes-shipping0']['states']['#title'] = t('State/Province');

   $form['get-quotes-shipping0']['states']['#prefix'] = '
';
   $form['get-quotes-shipping0']['states']['#suffix'] = '
';
    $form['get-quotes-shipping0']['states']['#options'] = array('_none' => '- None -') + $squadron_options;
    if (isset($squadron_options[0])) {
      $form['get-quotes-shipping0']['states']['#default_value'] =  $squadron_options[0];
    }

    //callback
    function _get_state_field(&$form,&$form_state) {

     return $form['get-quotes-shipping0']['states'];
}
?>

This is the code i wrote, try to follow the same structure it's working for me perfectly.
Tell us how it went, have a good coding!


How to extract data after equality sign?

Question: I wrote this regex by far but I can't get the correct data from :
Some text: TST = Data I need, TSA = More that I need to compare,
SMT = more that that I need
It's 2-3 capital letters before equality sign and then data for it until comma.
preg_match_all("/Some\s+text:\s+(?:([A-Z]{2,3})(?:\s+\=(\s\w+)*))(?:\,\s+([A-Z]{2,3})(?:\s+\=(\s\w+)*))*/mi" , $input_lines, $output_array);

Regex extracts(text, TST, need, SMT, need) but I'm trying to get:

0.text 1.TST 2.Data I need 3.TSA 4.More that I need to compare 5.SMT 6.more that that I need

Can someone please explain what mistakes I'm doing and how should I correct this regex?
Comment: I'd suggest this: [`(?:(Some\s+text):\s*|(?!^)\G)\s*([A-Z]{2,3})\s+\=\s*((?:\w+\s?)+),?`](https://regex101.com/r/mM7fL2/1).
Comment: how does "Data" look like? Also please go over your post once more, since it's very confusing.
Answer: Given your original description: It's 2-3 capital letters before equality sign and then data for it until comma., you can construct a relatively simple expression like so: [A-Z]{2,3}\s*=\s*(.+?)(,|$) (example available here).
This should expect to find 2 or 3 upper case letters, followed by 0 or more white spaces, followed by an equality sign, followed by 0 or more white spaces followed by some text. It will stop matching either when it finds a comma or the end of the string.
This should capture the data you are after into regular expression groups (should be within $output_array).
Comment: in your explantation, there is missing: "followed by an equality sign" between "followed by 0 or more white spaces, followed by 0 or more white spaces" :)
Answer: Try this regex:
([A-Z]{2,3})\s=\s([^,]+)

Demo
Here \s can be replace with \s*.
Answer: Try this regex: ([A-Z]{2,3}\s=\s)([\w\s]+)
Since you do not tell us what kind of data is stored in these variables, we can't help you with these. I'm using [\w\s]+, but you will need to tweak this.
Your fields are in match's group 1.
Live demo here at regex101.com