Iam working on a GitHub code DatabaseManager_For_Android and I am getting error at the getActivity() method

Question: I am working on a GitHub code DatabaseManager_For_Android on android studio and I am getting error at the getActivity() method.
The error as following :

Error:(40, 47) error: cannot find symbol method getActivity()

Any help please?
The code:
package com.example.toshiba.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

import java.util.List;

public class MainActivity extends Activity {

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sButton = (Button) findViewById(R.id.button_l);
        TextView tv = (TextView) findViewById(R.id.text_id);

        sButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent dbmanager = new Intent(getActivity(), AndroidDatabaseManager.class);
                startActivity(dbmanager);
            }
        });
        tv.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent dbmanager = new Intent(getActivity(), AndroidDatabaseManager.class);
                startActivity(dbmanager);
            }
        });
        DBHandler db = new DBHandler(this);

// Inserting Shop/Rows
        Log.d("Insert: ", "Inserting ..");
        db.addShop(new Shop("Dockers", " 475 Brannan St #330, San Francisco, CA 94107, United States"));
        db.addShop(new Shop("Dunkin Donuts", "White Plains, NY 10601"));
        db.addShop(new Shop("Pizza Porlar", "North West Avenue, Boston , USA"));
        db.addShop(new Shop("Town Bakers", "Beverly Hills, CA 90210, USA"));

// Reading all shops
        Log.d("Reading: ", "Reading all shops..");
        List<Shop> shops = db.getAllShops();

        for (Shop shop : shops) {
            String log = "Id: " + shop.getId() + " ,Name: " + shop.getName() + " ,Address: " + shop.getAddress();
// Writing shops to log
            Log.d("Shop: : ", log);
        }

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.example.toshiba.myapplication/http/host/path")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app deep link URI is correct.
                Uri.parse("android-app://com.example.toshiba.myapplication/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }
}

Comment: Where is `getActivity()` defined?
Comment: sorry but I am not sure what you mean, can you pleas explain to me? i will be really thankful :)
Answer: Replace the method getActivity() with this or name of the Activity class: MainActivity.this
So the line becomes:
Intent dbmanager = new Intent(MainActivity.this, AndroidDatabaseManager.class);
Comment: i tried replacing the getActivity() with this only before but didn't work out, however the second suggestion did work out :) thank you very much


Why eigenvectors of $\mathbf{A}^T\mathbf{A}$ are in row space of $\mathbf{A}$?

Question: I'm following SVD proof and I can't get why eigenvectors of $\mathbf{A}^T\mathbf{A}$ are in rowspace of $\mathbf{A}$. I can understand further why these eigenvectors are basis for the row space of $\mathbf{A}$, if they are in the row space of  $\mathbf{A}$, but I miss this very step of proof about belonging to the rowspace of $\mathbf{A}$. I know it must be very simple, so the books I use don't specify it or specified it earlier in the book.
Comment: If $\mathbf A ^{\mathrm T} \mathbf A \mathbf v = \mathbf A^{\mathrm T}(\mathbf {Av}) = c \mathbf v$, then where does $\mathbf v$ belong now?
Comment: Oh, I see, $\mathbf{v}$ belong to row space of $\mathbf{A}$ and column space of $\mathbf{A}^T$. Thank you!
Comment: Hint: what is the relation of the row space of $\mathbf A$ and the column space of $\mathbf A^{\mathrm T}$?
Comment: They are the same
Comment: You are welcome. Glad to help!
Answer: Posting the answer of @xbh given in the comments above:

If $\mathbf{A}^T\mathbf{A}\mathbf{v} = \mathbf{A}^T(\mathbf{A}\mathbf{v}) = c\mathbf{v}$ then $\mathbf{v}$ belongs to the row space of $\mathbf{A}$ and the column space of $\mathbf{A}^T$.
Comment: This assumes $c \ne 0$. Though I guess the question is only for non zero eigenvalued eigenvectors
Answer: Let $\mathbf A$ be a matrix; then $\forall \boldsymbol x$, $\mathbf A \boldsymbol x$ is in the column space of $\mathbf A$.
Thus $\mathbf A^T (\mathbf A \boldsymbol v)$ is in the column space of $\mathbf A^T$.
But $\mathbf A^T \mathbf A \boldsymbol v = c \boldsymbol v$.
Hence $c \boldsymbol v$ (or $\boldsymbol v$ if $c \ne 0$) is also in the column space of $\mathbf A^T$.
Comment: Your answer would be a fine alternative to the accepted answer, except for the pointless opening remark.


Fourier transform of signum function

Question: If we treat fourier transform as an operator on $L^1(\mathbb{R})$, then its image under fourier transform is the set of continuous functions which will vanish at infinity.
It is well known that the fourier transform of signum function is
$$\mathcal{F} (sgn)(u) =\frac{2}{ui}. $$
I know that signum function is not integrable over the real line. So, to evaluate its fourier transform, one can use limiting argument, say a sequence of functions that converges to signum function, because fourier transform is a bounded linear operator, and hence is continuous.
What puzzles me is that when using continuity, don't we need to ensure that the fourier transform is defined on the limiting function?
In this case, fourier transform of signum function is not defined due to reason given above. If this is the case, how would one obtain formula of the fourier transform of signum function?
Comment: The image of $L^1$ by Fourier transform is included in the set of continuous functions vanishing at infinity, but different from it!
Answer: 1. BOTTOM LINE UP FRONT: Treat it as a distribution.

Since the signum function is not integrable on $\mathbb{R}$, it may be useful to view it as a tempered distribution.
Such "generalized functions" are bounded linear functionals on a class of very-well-behaved functions called Schwartz functions. One of Laurent Schwartz's achievements was finding a collection $\mathcal{S}$ of functions on $\mathbb{R}^n$ such that the set of Fourier transforms of these functions is $\mathcal{S}$ itself. That put the original functions and their Fourier transforms on equal footing.

2. Fourier transform of a distribution

Why is this useful? It means that every tempered distribution has a Fourier transform that is also a tempered distribution. It also provides some useful notation to derive expressions and properties of the Fourier transform of a known tempered distribution.

Given any distribution $\mathsf{T}$ we write the result of applying it to a Schwartz function $\varphi$ as $\left<\mathsf{T},\varphi\right>$, but it is to be understood that this is not an inner product of two objects of the same kind. The Fourier transform of the distribution $\mathsf{T}$ is the distribution $\widehat{\mathsf{T}}$ for which
\begin{equation}
\left<\widehat{\mathsf{T}},\varphi\right> = \left<\mathsf{T},\widehat{\varphi}\right>
\end{equation}
for every $\varphi\in\mathcal{S}$, where $\widehat{\varphi}$ is the Fourier transform of $\varphi$. Since $\varphi\in\mathcal{S}$, $\widehat{\varphi}\in\mathcal{S}$, too.

3. Fourier transform of signum

How is this related to the signum function? If $\mathsf{T}$ is the signum function viewed as a distribution, then
\begin{equation}
\left<\mathsf{T},\varphi\right> = \int\textrm{sgn}(x)\varphi(x)dx.
\end{equation}
The Fourier transform of this distribution satisfies (or is defined by)
\begin{equation}
\begin{split}
\left<\widehat{\mathsf{T}},\varphi\right> &=~
\left<\mathsf{T},\widehat{\varphi}\right>\\
&=~ \int\textrm{sgn}(x)\widehat{\varphi}(x)dx\\
&=~ -\int_{-\infty}^{0}\widehat{\varphi}(x)dx + \int_{0}^{\infty}\widehat{\varphi}(x)dx.
\end{split}
\end{equation}

4. Changing order of integration

Let's consider the integral for positive reals. The very good behavior of $\varphi$ allows changing orders of integration in many, many situations.
\begin{equation}
\begin{split}
\int_{0}^{\infty}\widehat{\varphi}(x)dx
&=~
\int_{0}^{\infty}\left[\int\varphi(k)e^{-ixk}dk\right]dx\\
&=~
\lim_{R\to\infty}\int_{0}^{R}\left[\int\varphi(k)e^{-ixk}dk\right]dx\\
&=~
\lim_{R\to\infty}\int\left[\int_{0}^{R}e^{-ixk}dx\right]\varphi(k)dk
\end{split}
\end{equation}
We do something very similar for the negative reals.
\begin{equation}
\begin{split}
-\int_{-\infty}^{0}\widehat{\varphi}(x)dx
&=~
-\int_{-\infty}^{0}\left[\int\varphi(k)e^{-ixk}dk\right]dx\\
&=~\lim_{R\to\infty}-\int_{-R}^{0}\left[\int\varphi(k)e^{-ixk}dk\right]dx\\
&=~
\lim_{R\to\infty}-\int\left[\int_{-R}^{0}e^{-ixk}dx\right]\varphi(k)dk
\end{split}
\end{equation}
We now address the sum of the $R$-dependent integrals.
\begin{equation}
\begin{split}
\int_{0}^{R}e^{-ikx}dx - \int_{-R}^{0}e^{-ikx}dx
&=~
\left.\frac{e^{-ikx}}{-ik}\right|_{x=0}^{x=R}
-
\left.\frac{e^{-ikx}}{-ik}\right|_{x=-R}^{x=0}\\
&=~
\frac{1 - e^{-ikR}}{-ik}
-
\frac{e^{ikR} - 1}{-ik}\\
&=~
\frac{e^{ikR} + e^{-ikR}}{ik}
-
\frac{2}{ik}
\end{split}
\end{equation}

5. Singularity at $k = 0$; Riemann-Lebesgue Lemma

The $k$ in the denominator will be a problem at $k=0$. But we know that the original integrals coverge. We must consider the new one as the limit of integrals from $\epsilon$ to $\infty$ and from $-\infty$ to $-\epsilon$.
\begin{equation}
\int_{|k|>\epsilon}\frac{e^{ikR} + e^{-ikR}}{ik}\varphi(k)dk
=
\int 1_{\{k:|k|>\epsilon\}}(k)\frac{\varphi(k)}{ik}\left(e^{ikR} + e^{-ikR}\right)dk
\end{equation}
For each $\epsilon >0$, the function $1_{\{k:|k|>\epsilon\}}(k)\frac{\varphi(k)}{ik}$ is in $L^1(\mathbb{R})$, so this integral is that function's Fourier transform evaluated at $\omega = R$ plus the same Fourier transform evaluated at $\omega = -R$. The Riemann-Lebesgue Lemma shows that if $f\in L^1(\mathbb{R})$, then $\lim_{|R|\to\infty}\widehat{f}(R) = 0$. Hence, these $R$-dependent terms vanish as $R\to\infty$.
It is worth noting that this shows that we must take the $R$-limit first and then take the $\epsilon$-limit. The opposite order would not work.

6. Cauchy Principal Value

We are left with
\begin{equation}
\lim_{\epsilon\to 0}2i\int_{|k|>\epsilon}\frac{\varphi(k)}{k}dk.
\end{equation}
This is the Cauchy Principal Value of this integral. This shows that we must interpret the Fourier transform of the signum function very carefully, but we can do it in the sene of distributions: if $\textrm{sgn}(x)$ is the signum of $x$, then
\begin{equation}
\widehat{\textrm{sgn}}(k) = 2i~\mathsf{PV}\left(\frac{1}{k}\right).
\end{equation}
Comment: Just to be clear, I am talking about going from $\lim_{R \to \infty} \lim_{\epsilon \to 0^+}$ to $\lim_{\epsilon \to 0^+} \lim_{R \to \infty}$, not about changing $\int_0^R \int_{\mathbb R}$ to $\int_{\mathbb R} \int_0^R$. The latter is straightforward because $\iint_{\mathbb R \times [0, R]} |\phi(k) e^{-i k x}| \, d(k, x)$ is finite. For the former, since we know that the single limits exist, it would suffice to show uniform convergence of one of the single limits.
Comment: Hi Joe.  You beat me to this way forward.  I had written up to the result in your Section 3 when I noticed your very well-written post (+1).  The analysis reminds me of the development in [This Answer](https://math.stackexchange.com/questions/2825297/fourier-transform-of-t-ht/2825495#2825495).  Your post is of exceptional value and I wish I could give more than one up vote.
Comment: Joe, I took the liberty of posting a fairly compact development using bracket notation that relies on some distributional relationships.  It is, therefore, a bit different in kind from yours and serves as a complementary way forward.  Let me know your thoughts.
Comment: @mark-viola: You are too kind. If I had not remembered the Riemann-Lebesgue Lemma, I would still be worrying about the terms that vanish as $R\to\infty$. Using that result felt like cheating, but I had spent enough time on it that I decided not to seek a more direct method.
Comment: Thank you my friend!  Much appreciated.  I took for granted several well-known relationships that require some background in the theory of generalized functions.  Once equipped with that background, then all proceeds nicely.  I really like your post inasmuch as you explain everything along the way for a reader who is less familiar with tempered distributions and the Fourier Transform.
Comment: You should get the opposite sign in the final result because
$$\int_0^R e^{-i k x} dx - \int_{-R}^0 e^{-i k x} dx =
-\frac {2 \cos R k - 2} {i k}.$$
Also, you've obtained
$$\int_{\mathbb R} \hat \phi(x) \operatorname {sgn} x \, dx =
\lim_{R \to \infty} \lim_{\epsilon \to 0^+} \int_{|k| > \epsilon}
 \frac {2 \cos R k} {i k} \phi(k) dk -
\operatorname {v. \! p.} \int_{\mathbb R} \frac {2 \phi(k)} {i k} dk,$$
so it's not that we cannot take the limit wrt $\epsilon$ first (this is exactly what we have) but rather that we need to show that the two limits can be interchanged.
Comment: @Maxim First, Happy New Year.  I hope that you are doing well and staying safe and healthy.  On your last comment, we need not interchange the order of those limits. Integration by parts facilitates showing that the iterated limit of the first integral is indeed zero.
Comment: @MarkViola Thank you, Mark. The answer is evaluating $\lim_{\epsilon \to 0^+} \lim_{R \to \infty}$, so I'm just pointing out that there is a step of interchanging the limits that needs to be justified. Or, as you suggest, avoided.
Comment: @Maxim: Thank you for the correction. I chose one of numerous possible explanations. Some require change of order of integration; some don't. I thought the OP was unfamiliar with tempered distributions, so I stayed away from its machinery (other than definitions) for as long as I could. After converting to a definite integral, I skipped an explicit justification of change of order of integration because..well, because I got lazy. But I also wanted readers to know that Schwartz functions are extra-ordinarily well-behaved, and many operations with them need little justification.
Comment: @Maxim: I missed your point at first, but now I see. I will certainly need a moment to grok the proper argument. Thank you.
Comment: I think there is a mistake on the last few lines of **4** where the evaluation order of $x=R, x=0$ and likewise $x=0, x=-R$ has been swapped. The final result should then be
\begin{align}
\frac{2}{i}PV(\frac{1}{k})
\end{align}
Comment: @JoeMack There is now an addendum to my original post that follows your approach, but is a bit different.  I would enjoy hearing your thoughts.
Answer: Let $\phi(x)$ be a Schwartz function and $\displaystyle \psi(x)=\text{PV}\left(\frac1{ix}\right)$.
Using the distributional relationships $\langle d',\phi\rangle =-\langle d,\phi'\rangle $ for any tempered distribution $d$ and $\text{sgn}'(x)=2\delta(x)$, we have
$$\begin{align}
\langle  \mathscr{F}\{\text{sgn}\},\phi \rangle &=\langle \text{sgn},\mathscr{F}\{\phi\}\rangle\\\\
&=\langle \text{sgn},\left(\mathscr{F}\{\phi \psi\}\right)'\rangle\\\\
& =-\langle(\text{sgn})', \mathscr{F}\{\phi \psi\}\rangle\\\\
& =-2\langle \delta, \mathscr{F}\{\phi\psi\}\rangle\\\\
&=2i\text{PV}\left(\int_{-\infty}^\infty \frac{\phi(x)}{x}\,dx\right)
\end{align}$$
Hence, we can write the distributional relationship
$$\bbox[5px,border:2px solid #C0A000]{\mathscr{F}\{\text{sgn}\}(x)=\text{PV}\left(\frac{2i}{x}\right)}$$

ADDENDUM TO ORIGNAL POST:
Here we present an approach very similar to that of @JoeMack.
For any $\delta>0$ we have
$$\begin{align}
\langle \mathscr{F}\{\text{sgn}\},\phi\rangle &=\langle \text{sgn}, \mathscr{F}\{\phi\}\rangle\\\\
&=\int_0^\infty \int_{-\infty}^\infty \phi(k)e^{ikx}\,dk\,dx-\int_{-\infty}^0 \int_{-\infty}^\infty \phi(k)e^{ikx}\,dk\,dx\\\\
&=2i\int_0^\infty \int_{-\infty}^\infty \phi(k)\sin(kx)\,dk\,dx\\\\
&=\lim_{L\to \infty}\int_{-\infty}^\infty \phi(k)\frac{1-\cos(kL)}{k}\,dk\\\\
&=\lim_{L\to \infty}2i\left(\int_{|k|\ge \delta}\phi(k)\frac{1-\cos(kL)}{k}\,dk+\int_{|k|\le \delta}\phi(k)\frac{1-\cos(kL)}{k}\,dk\right)\\\\
&=2i\int_{|k|\ge \delta}\frac{\phi(k)}{k}\,dk+O(\delta)\tag1
\end{align}$$
Letting $\delta\to 0$ on the right-hand side of $(1)$, we find that
$$\langle \mathscr{F}\{\text{sgn}\},\phi\rangle=\lim_{\delta\to 0}\int_{|k|\ge \delta}\frac{2i\phi(k)}{k}\,dk\tag2$$
The right-hand side of $(2)$ is the Cauchy Principal Value of $\int_{-\infty}^\infty \frac{2i\phi(k)}{k}\,dk$.   Therefore, we find that in distribution
$$\mathscr{F}\{\text{sgn}\}(x)=\text{PV}\left(\frac{2i}x\right)$$
Comment: That is pretty slick; I like it.
Comment: Thank you Joe!  Much appreciative.
Comment: Strictly speaking, $\mathcal F[\phi \psi]$ is not a Schwartz function, $(f, \mathcal F[\phi \psi])$ is not well-defined.
Comment: @Maxim How would you propose to salvage the development?
Comment: I don't think we can get $\mathcal F[\operatorname {sgn}]$ (or the solution of $x f = 1$) in a purely algebraic way. If we already know that $\mathcal F[f]$ is the limit of $\int_{|k| < R} f(k) e^{i x k} dk$ if $\int_{\mathbb R} |f(k)| (1 + |k|)^{-m} dk$ is finite for some $m \geq 0$, then we have
$$F(x) = \int_{|k| < R} e^{i x k} \operatorname {sgn} k \, dk =
\frac {2 i (1 - \cos R x)} x, \\
(F, \phi) = 2 i \int_{\mathbb R^+} \frac {\phi(x) - \phi(-x)} x dx -
 2 i \int_{\mathbb R^+} \frac {\phi(x) - \phi(-x)} x \cos R x \, dx.$$
The first term is $2 i (\mathcal P(1/x), \phi)$.
Comment: @Maxim Yes, I had written a solution using the same approach as Joe, was ready to post, but noticed that Joe had already posted.  I thought that I would pursue another approach and posted this accordingly.  There might be a work around here, but as you pointed out, the Fourier Transform of $\phi \psi\notin \mathbb{S}$
Comment: @Maxim I hope that you're doing well.  There is now an addendum to my original post that follows Joe Mack's approach a bit, but is different.  I would enjoy hearing your thoughts.

Fourier transform of signum function


Cálculo de comissão de vendedor javascript

Question: Estou desenvolvendo um projeto e preciso calcular a comissão. Exemplo:  (ao digitar o valor total do cerviço tem que dar resultado de custo de serviço no segundo input e no terceiro input o valor que o cliente ira receber, no codigo abaixo consegui calcular o custo de servico, mas nao consigo calcular o valor que o cliente vai receber,) como eu poderia fazer isso em javascript ?
exemplo
[1]: https://i.stack.imgur.com/FHHzn.png

    <!DOCTYPE html>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>cálculo</title >

Orçamento

      <input value="20"  id="valorPorcentagem" hidden>
      Valor total:
      <input id="valor_vendedor" onkeyup="calcular()">

Custo de serviço:

      <input type="text" name="custo" id="comissao" readonly>

        O que voce receberá
      <input type="text" name="pago"  id="sera_pago" readonly>

Answer:

function calcular() {
    var comissao = (parseFloat($("#valorPorcentagem").val()) / 100) * parseFloat($("#valor_vendedor").val());
    $("#comissao").val(comissao);
    $("#sera_pago").val($("#valor_vendedor").val() - $("#comissao").val())
}
<!DOCTYPE html>

      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Calculos</title>

Orçamento

         <input value="20"  id="valorPorcentagem" hidden>
         Valor total:
         <input id="valor_vendedor" onkeyup="calcular()">

Custo de serviço:
         <input type="text" name="custo" id="comissao" readonly>

         O que voce receberá
         <input type="text" name="pago"  id="sera_pago" readonly>

Comment: Qual a necessidade do _listener_ no script se já tem um no HTML?
Comment: Bem observado. Fiz o ajuste. Obrigado.
Comment: Muito obrigado Rodrigo, você me ajudou muito


How to model a conditional probability without estimating joint PDF?

Question: I've around 3e3 two-dimensional data points,
   x,   d
1  1, 0.1
2  3, 0.1
3  2, 0.2
4  1, 0.5

range(x) = [-600, 600], range(d) = [0, 1]

We are trying to model d with a probability model. I believe the distribution of d should be dependent on x. So I'm up to model $Pr(d|x)$ rather than $Pr(d)$.
To do this, I am now using kernel density distribution to estimate the joint distribution of $Pr(d, x)$ and divide it by $Pr(x)$.
However, the number of data is enough to model $Pr(d)$ with kernel density estimation while insufficient to model $Pr(d,x)$.
So I'm wondering about this:
Is there any model you suggest that can model d in a conditional way without modeling the joint distrubution?
I have the following thoughts:
I can use SVM to deal with the short of data. However is there a way to train a "conditional SVM"?
Answer: You are searching for regression. A wide definition of a regression model is conditional modeling, see Definition and delimitation of regression model.  So to model the conditional expectation of $D \mid X$ you can use some kind of regression model, maybe a nonparametric one. You have not given sufficient details or context to say much more ... but for an example of copula regression, see Conditional expectation of two identical marginal normal random variables.
You could simply start with a scatterplot smoother, such as lowess, or at least show us a plot of your data!


Not sure which SPSS method to use to evaluate results!

Question: I have two main groups of mice: Control diet and high fat/high sucrose (HF/HS).
Within each group, I have three genotypes.
So for example:
Control group: TG, KO, WT
HF/HS group: TG, KO, WT
I have cycle threshold data for their gene expressions (10 results for each group).
My question is: if I am investigating how the change in diet affects each genotype, shall I use one-way ANOVA to do so? And then paired t-test to just compare one genotype between the two main groups?
Comment: I'd cut "SPSS" from your title. It cuts down on readership. Evidently you're not asking for code, which is good, but mentioning any software is thus quite unneeded.
Answer: I'm assuming that when you say you have "10 results for each group", you mean you have 10 independent mice in each group, and one measurement/mouse. Also, the assumption is that you don't have a priori predictions about specific comparisons you want to make.
If so, then you need to run a factoral analysis of variance (if you are using the menu system in SPSS, go to Analyze > General Linear Model > Univariate...; NOTE: That is GENERAL Linear Model, not GENERALIZED Linear Model).
Again, if you don't have specific comparisons that you are predicting a priori, then you might want to chose the option for post hoc contrasts to compare the 6 groups (2 diets X 3 genotypes). You might want to find out what post hoc method is most common in your discipline, but Tukey's HSD (Called just Tukey, in SPSS I believe) is pretty popular. The Bonferroni is also a pretty widely accepted method. These will make all pair-wise comparisons between your 6 groups, controlling for the inflation in your Type I error rate because you are making multiple comparisons and because you are doing this post hoc (i.e., after you already know that at least some of the groups are different because you have the ANOVA results).
Comment: Thank you for your help! Yes basically my objective is to look at the effects of a hormone on gene expression. 3 gene types for the hormone (Knockout, transgenic and normal wild type) are fed a control diet. 3 gene types for the hormone (knockout, transgenic and normal wild type) are fed an obesity inducing diet (HF/HS). I was thinking I could compare the gene expression fold changes as follows: WT-control vs. WT-HFHS using an unpaired t-test...then the same for KO-control vs. KO-HFHS and TG-control vs TG-HFHS. I have seen ANOVA being used a lot but was unsure whether it would be suitable!
Comment: Also, sorry it I didn't make it clear. I have 30 in each diet group, 10 for each genotype (WT, TG or KO) :)


Reverse string without reversing custom characters swift

Question: Need to reverse strings without affecting characters which user can add by themself. Can somebody rewrite my code and suggest the solution?
func reverseWithoutFilter(FullText:String, TextToIgnore:String) -> String {
    let fullTextArray = FullText.components(separatedBy: " ")
    let textToIgnoreArray = TextToIgnore.components(separatedBy: " ")
    let result = fullTextArray.map{!textToIgnoreArray.contains($0) ? String($0.reversed()) : $0}
    return result.joined(separator: " ")
}

var result = reverseWithoutFilter(FullText: "FOX is good", TextToIgnore: "FOX")

// result will be "FOX si doog"

var result1 = reverseWithoutFilter(FullText: "Fox is good", TextToIgnore: "Fx")

// result will be "xoF si doog " characters "F" and "x" are not ignored

Comment: it's unclear what 'text to to ignore' means. Should "Fx" just match "Fox" to give "xoF" or should it also match the first and last characters of "For once upon a time there lived a badger called Nix".  And what about "For once upon a time Far away from the lynx there lived a Fox called Foxie".  Using regex would make the intention far clearer.
Comment: Can you tell what result you expect in each case ? I get the ones you show… except I get FOX si doog and not Fox si doog
Comment: @claude31
For example input: "Alloha 123" i want to reverse all characters except "h"and "3" so output will be: "aollhA 213"
Comment: @JoakimDanielson
Yes
Comment: That's not clear. What do you mean by ignoring some text ? Each character to be kept in position ?
Comment: @claude31
by ignoring some text i mean the characters which will stay in old position and do not swap in string.
Comment: @flanker I believe each word should be treated separately so the reversal logic is applied once per word. So Fox, For, Far will be changed to Fox, Fro and Fro no matter where in the sentence they are or how many times they occur.
Comment: That was my best guess too, but the Q could have been clearer.
Answer: You should

extract components in fullText, as you did
in each, get the the positions of each char in textToIgnore
remove those chars in each component
reverse each component
insert back the removed chars in the reverted components

Here is a code for this. Can be optimized for sure, but it works.
extension StringProtocol {
    subscript(offset: Int) -> Character {
        self[index(startIndex, offsetBy: offset)]
    }
}

func reverseWithoutFilter2(fullText: String, textToIgnore: String) -> String {

    // For example: "Alloha 123", excluding "h3"
    let fullTextArray = fullText.components(separatedBy: " ")
    // fullTextArray = ["Alloha", "123"]
    var posOfChars : [[String:[Int]]] = [] // posOfChars[i] is a dict [String:[Int]], giving the positions of each excluded chars in component i
    // keep positions of excluded chars
    for (i, comp) in fullTextArray.enumerated() {
        posOfChars.append( [:] )
        for c in textToIgnore {
            for (pos, charInComp) in comp.enumerated() {
                if charInComp == c {
                    if posOfChars[i][String(c)] == nil {
                        posOfChars[i][String(c)] = [pos]
                    } else {
                        posOfChars[i][String(c)]!.append(pos)
                    }
                }
            }
        }
    }
    // posOfChars[0]["h"] = [4]
    // posOfChars[1]["3"] = [2]

    var newTextArray = fullTextArray
    // Remove excluded chars in each comp
    for (i, comp) in fullTextArray.enumerated() {
        for c in textToIgnore {
            newTextArray[i] = newTextArray[i].replacingOccurrences(of: String(c), with: "")
        }
    }
    // newTextArray = ["Alloa", "12"]

    // Now we reverse each component
    newTextArray = newTextArray.map() { String($0.reversed()) }
    // newTextArray = ["aollA", "21"]

    // Put back the excluded chars
    for i in 0..<newTextArray.count {
        for toRestore in posOfChars[i] { // ["h": [4] ]
            let str = toRestore.key // "h" but need to get a char
            let firstChar = str[0]
            for pos in toRestore.value {
                let posIndex = newTextArray[i].index(newTextArray[i].startIndex, offsetBy: pos)
                newTextArray[i].insert(firstChar, at: posIndex)
            }
        }
    }

    return newTextArray.joined(separator: " ")
}

You may have noticed it doesn't work for
let result = reverseWithoutFilter2(fullText: "B3lloha 123", textToIgnore: "h3")
Need to change logic a little:
func reverseWithoutFilter2(fullText: String, textToIgnore: String) -> String {

    // For example: "B3lloha 123", excluding "h3"
    let fullTextArray = fullText.components(separatedBy: " ")
    // fullTextArray = ["B3lloha", "123"]
    var posOfChars : [[String:[Int]]] = [] // posOfChars[i] is a dict [String:[Int]], giving the positions of each excluded chars in component i
    // keep positions of excluded chars
    for (i, comp) in fullTextArray.enumerated() {
        posOfChars.append( [:] )
        for c in textToIgnore {
            for (pos, charInComp) in comp.enumerated() {
                if charInComp == c {
                    if posOfChars[i][String(c)] == nil {
                        posOfChars[i][String(c)] = [pos]
                    } else {
                        posOfChars[i][String(c)]!.append(pos)
                    }
                }
            }
        }
    }

    // posOfChars[0]["h"] = [5]
    // posOfChars[0]["3"] = [1]
    // posOfChars[1]["3"] = [2]

    var newTextArray = fullTextArray
    // Remove excluded chars in each comp
    for (i, comp) in fullTextArray.enumerated() {
        for c in textToIgnore {
            newTextArray[i] = newTextArray[i].replacingOccurrences(of: String(c), with: "")
        }
    }
    // newTextArray = ["Blloa", "12"]

    // Now we reverse each component
    newTextArray = newTextArray.map() { String($0.reversed()) }
    // newTextArray = ["aollB", "21"]

    // Put back the excluded chars

    for i in 0..<fullTextArray.count { // Get each component
        // let us build the array of keys, in correct order (1, "3"), (5, "h")
        var orderedArray = [(Int, String)] ()
        let maxIndex = newTextArray[i].count // 7 for B3lloha
        for toRestore in posOfChars[i] { // ["h": [5], "3": [1] ]
            for item in toRestore.value {
                orderedArray.append((item, toRestore.key))
            }
            orderedArray = orderedArray.sorted() { $0.0 < $1.0 } // orderedArray [(1, "3"), (5, "h")]

        }
        for (index, str) in orderedArray {
            let posIndex = newTextArray[i].index(newTextArray[i].startIndex, offsetBy: index)
            let char = str[0]
            newTextArray[i].insert(char, at: posIndex)
        }
    }

    return newTextArray.joined(separator: " ")
}


Inverse Quadratic Interpolation and the secant method

Question: I am currently completing a maths project that aims to approximate the roots of functions using MATLAB.
The two root finding methods that I have used are inverse quadratic interpolation and the secant method.
I have written scripts for both methods and have obtained approximations to the roots of different functions.
I was wondering if it was possible to graphically illustrate the various iterations for a specific function, on the same graph, to show the method converging to the actual root value?
If this is possible I would like to be able to complete it on MATLAB if not then excel?
Does anyone know if this is possible or even how to go about completing it?
Thank you!!
Comment: I can't tell you how, but it is doable, no doubt. I would just like to stress that you won't be able to show many iterations as such, because the drawing accuracy will quickly be reached. It is advisable to plot the logarithm of the (absolute) difference to the exact root (which you will precompute), to magnify the scale.
Answer: This is just a suggestion. Create a table $[n_1,x_{n_1}]$ where $n_1$ is the iteration number and $x_n$ the predicted value for the solution using method $1$. Do the same for method $2$ creating a table  $[n_2,x_{n_2}]$. Now, plot the values of $x_{n_1}$ and $x_{n_2}$ as a function of $n$. Eventually, add to the plot the horizontal line corresponding to the exact solution.
Is this totally stupid or undoable (I don't know Matlab) ?
Comment: Hey! Thanks for your suggestion!

I don't think I explained myself particularly well so apologies!!

Basically, I have created scripts using MATLAB that will compute + then output an approximation for the root of a function using  the secant method and inverse quadratic interpolation (IQI) method. These methods use initial root approximations and then keep repeating the process, using the new approximation and the latter one (secant) or two (IQI) until convergence occurrs.

I was wondering if there was anyway to display all the iterations on the one graph just to illustrate convergence.
Comment: For the secant method for example; I would like to have a graph of the function I am investigating and then on this graph I was wondering if it was possible to show all the different lines that MATLAB used to compute the root?
Comment: ! http://commons.wikimedia.org/wiki/File:Secant_method_2.svg

I was hoping that I could achieve something like this image where the blue lines represent the different iterations?

Thank you very much for replying though! :)


Prove that $x\in \mathbb{R}>0 \rightarrow \exists y\in \mathbb{R}>1$ s.t. $ xy>1$.

Question: I am going through my practice sheet before finals in my proofs class, and I would like critique, please.
My understanding of the If-Then and the "hidden implications" is that each real number $x>0$ has at least one $y>1$ such that  $xy>1$. If this is the case, then it is true, and here is my formal proof:
Let $x \in \mathbb{R}$ and let $y \in \mathbb{R} s.t. y=\dfrac {1} {x}+1$. Since $x>0, \dfrac {1} {x}>0$, and $y=\dfrac{1} {x}+1>1,$ and $xy = x(\dfrac {1} {x} + 1) = 1 + x >1$.
Thanks!
Comment: Your choice of $y$ is a good one, but you have proved that $y>1$.  You need instead to prove that $xy>1$.
Comment: Also, xy = 1+x, not 1+1/x. But both exceed 1.
Comment: I'll fix it, thanks.
Comment: @vadim123 I did fix that. Thank you.
Comment: @martycohen, I figure I'll eventually get it right! Thanks.
Answer: Yes, that proof is correct. Well done.


Poco andante: a little medium?

Question: I'm working towards ABRSM grade 5 music theory. In the Music Theory in Practice workbook there is a piece of music marked "Poco andante", with the following question: "Suggest a suitable metronome mark for this piece".
The definition ABRSM give for "poco" is "a litte", and for "andante" is "at a medium ('walking') speed".
So I interpret "Poco andante" as "at a little medium speed".
I could get my head around a little fast or a little slow, but a little medium?
How should this be interpreted?
Comment: Related question: https://music.stackexchange.com/questions/53339/andante-assai-usual-interpretation?rq=1
Comment: How it should be interpreted depends quite a bit on when it was written (as suggested by [Tim's answer](https://music.stackexchange.com/a/75462/2257)), perhaps on where it was written, and probably on by whom it was written.  Can you add any of this information to the question?
Answer: Yes, 'poco andante' defies literal analysis, doesn't it!   And if you Google it you'll find plenty of examples of composers using it, but precious few definitions.
THIS explores the subject, but reaches no conclusion.
It's a mischievous question for a Grade V theory paper.  I suggest you answer it as if the 'poco' wasn't there.
Answer: Actually poco andante is most sensibly used modifying another speed indication, so Adagio, poco andante could indicate make it a bit faster than Adagio but the opposite direction from Allegro/Allegretto would equally work. If your example misses the reference point, you could try to deduce one from the title or form of the piece, e.g. a menuet is more on the slow side, other dances or marches also suggest a default speed.
Without any reference the numerically lower Andante range of the metronome (perhaps 80) is the best guess.
Answer: One of my metronomes - 150+ yrs old - says andante is 120-150bpm. Another quotes 76-108.Poco means, as you say, a little, maybe a little less. So a reasonable answer will be at the bottom of the tempos, say 100bpm.
Tempo words were meant to be vague, thus the margin for each. To allow for individual interpretations of performance speed. From the info. on my metronomes, vague isn't exactly the right word!
Comment: @TimH - remember, it was 150 years ago. legs were shorter then...
Comment: I think 120-150bpm is very fast for an andante! That's not a walking speed, it's almost running.
Comment: I think usually the second range you cited is a more agreed-upon standard for andante.
Comment: @user 45266 - the reason I quoted what my first metronome said was andante is that it would have been used closer to the period when these sort of tempo marks were more commonly used. (over 150 years ago).
Comment: @Tim Oh, really? I didn;t know that. That's interesting!

Poco andante: a little medium?


R to N mapping and Cantor's diagonal argument

Question: Consider a function f that takes all the supraunitary digits of the real number and assign them on the odd positions of the natural number and all the subunitary digits in reverse order and assign them on the even positions of the natural number. This mapping is unique.
How would Cantor's diagonal argument work on this mapping? Surely for any real number r, constructed using this argument, f would construct a unique natural number, which is part of the mapping, contradicting that r isn't mapped.
A more formal definition for the proposed function and its inverse:
Let $f\colon\mathbb{R}\to\mathbb{N}$ such as for any real number with the decimal representation $a_n ... a_2 a_1. b_1 b_2 ... b_m$ with $a_n \neq 0$ and $b_m \neq 0$:
\begin{equation}
f(a_n ... a_2 a_1. b_1 b_2 ... b_m)=
\begin{cases}
 b_m 0 \dots b_2 a_2 b_1 a_1& \text{if}\ n<m\\
 b_n a_n \dots b_2 a_2 b_1 a_1& \text{if}\ n=m\\
 a_n 0 \dots b_2 a_2 b_1 a_1& \text{if}\ n>m\\
\end{cases}\label{fdef}
\end{equation}
Let $f^{-1}\colon\mathbb{N}\to\mathbb{R}$ such as for any natural number with the decimal representation $\dots x_6 x_5 x_4 x_3 x_2 x_1$:
\begin{equation}
f^{-1}(\dots x_6 x_5 x_4 x_3 x_2 x_1)= \dots x_5 x_3 x_1. x_2 x_4 x_6 \dots\label{finvdef}
\end{equation}
Comment: "Surely for any real number r, constructed using this argument, f would construct a unique natural number." Sentences like that are *exactly* where to look for the mistake - in particular, $f$ does **not** in general yield a natural number, as stated above and below. When you're trying to prove something, always focus special attention on the claims that are "too obvious" to warrant explicit proof (and this doesn't just apply to you; I've been nailed by this more times than I am comfortable admitting).
Comment: I hope you agree that every non-empty set of natural numbers has a minimal element. What is the smallest natural number to have infinitely many digits?
Comment: @ctapus "The set of natural numbers is infinite, so there are naturals number with an infinity of digits." That's nonsense. Every natural number is finite - so every natural number has finitely many digits. Just because the *set* of natural numbers is infinite, doesn't mean any *individual* natural number is infinite. (Incidentally, there *are* number systems where "infinite leftward expansions" make sense - look at the $p$-adics, for instance - but $\mathbb{N}$ isn't one of them.)
Comment: @ctapus Re: your comment to me, *those* are - but something like "$...3030303030$" isn't. (Incidentally, if you insist on such things being in your number system, how do you compare them? Which is larger: $...121212$ or $...212121$? Note that they "keep switching": $2>1$, $21>12$, $212>121$, .... So even if you insist on a number system with such weird objects (which, to reiterate, **will not be $\mathbb{N}$**), things are going to get ugly.)
Comment: I have negative idea what you're talking about. Note that 1/3 is ***not*** a natural number, nor its integral part (which is 0, by the way) has infinitely many non-zero digits. What is the next number after 1/3, by the way? You got me curious.
Comment: How would it work? You have the literal mapping. Show how it works.
Comment: Also, last time I checked, the integral part of a real number had only finitely many decimals. I might be wrong about that, though.
Comment: But every natural number has only finite number of digts.
Comment: @AsafKaragila The mapping would work like this: 1 is mapped to 1; 0,1 is mapped to 10; 10 is mapped to 100; 123.456 is mapped to 415263; etc.
Comment: @ZoranLoncarevic The set of natural numbers is infinite, so there are naturals number with an infinity of digits.
Comment: @NoahSchweber By its definition f will yield a string of digits without any decimals, eg f(1.2)=21; f(12)=102; why are those not natural numbers?
Comment: @NoahSchweber "Every natural number is finite - so every natural number has finitely many digits." Thanks for this insight, it would be great if you could provide me with a resource on this topic. I've seen this statement in few other topics on mathflow, but they don't go into much details. However, going back to Cantor's argument, he's also allowing numbers with infinitely many digits. Or am I reading this wrong?
https://en.wikipedia.org/wiki/Cantor's_diagonal_argument
Comment: @AsafKaragila This is interesting, but the fact that you can only think about something only in abstract terms doesn't mean it doesn't exist? I can ask for example what's the next number after 1/3 (or 0.33... to stay in decimal notation). You can't express it but it does exist.
Comment: @AsafKaragila Like I said, it can't be expressed, but you also agree that it exists ( http://math.stackexchange.com/questions/696043/why-there-is-not-the-next-real-number ) I was just pointing to the fact that for real numbers we are happy to accept them having infinitely many digits, even if we are unable to use some of the number system's properties. This comment was made also in regards with the assertions that natural numbers have to have a finite number of digits, otherwise we are not able to make use of some of their properties (like ordering).
Comment: I have *no* idea why you say that I agree that it exists. I literally explained that it does not exist. And my point is that the "number" that you map 1/3 will have infinitely many digits in its integral part. Which is therefore *not* a natural number.
Comment: Probably a confirmation bias when I read this: <>
And from wikipedia (https://en.wikipedia.org/wiki/Well-order#Reals) "[...] it is consistent with ZFC that a definable well ordering of the reals exists".
Answer: The problem is that almost all real numbers will have infinitely many nonzero digits $b_i$ following their decimal point. These numbers are not mapped to natural numbers by your scheme, since they have an infinite number of digits, while all natural numbers only have a finite number of digits.
For instance $1/3$ gets mapped to $\dots 30303030$ with an infinite number of copies of $30$, which is not a natural number.
Comment: Why 30303030... is not a natural number? Every natural number has a successor, which means that there are infinitely big natural numbers, meaning there are natural numbers with an infinity of digits.
And wouldn't Cantor's proof for countability of rational numbers would suffer from the same issue? Given the fact that there are infinitely many prime numbers, in his ordering schema you will get to a rational number that is the ratio of two infinite prime numbers.
Comment: @ctapus "Every natural number has a successor, which means that there are infinitely big natural numbers, meaning there are natural numbers with an infinity of digits." That's incorrect. And incidentally, the "number" $f$ gives on input ${1\over 3}$ isn't $303030...$ but rather $...303030$: it has a last term, but no first term.
Comment: @NoahSchweber Thank you for the insight, Noah. If I may draw a similarity with the proof of countability of Q, why can't real numbers be ordered by their number of digits? So first you will have real numbers with 1 digit (0..9), followed by numbers with 2 digits(10..99, with 0.1 .. 0.9) followed by real numbers with 3 digits, etc? Is it be because of numbers like 0.(3)? I mean if all the real numbers will be listed, 0.(3) should be there, mapped to a number with an infinite number of digits. This is what I was actually trying to capture in f's definition.
Comment: @ctapus Yes, the problem is numbers like ${1\over 3}$ - they won't appear anywhere in your proposed list. Indeed, no matter how you define $f$, it will miss most of the reals; this is *suggested* by the experience you get playing around with a bunch of possible $f$s (and seeing how they each break), and *proved* by Cantor's argument.


2x2 Grid equal height despite box content amount

Question: I am trying to create a 2x2 grid where the height of the four individual boxes (red, blue, green, yellow) are all equal despite how much content they contain.

#rowTwo {
  background-color: pink;
  min-height: 25%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

#grid2x2 {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  background-color: orange;
  width: 80%;
  justify-content: center;
}

.box {
  display: flex;
  flex-basis: calc(50% - 20px);
  margin: 5px;
  justify-content: center;
  padding-top: 5%;
  overflow: hidden;
}

  <Heading title="My Title"/>

        <DescriptionBoxItemWithHeading item={this.state.sectionTwoItems[0]}/>

        <DescriptionBoxItemWithHeading item={this.state.sectionTwoItems[1]}/>

        <DescriptionBoxItemWithHeading item={this.state.sectionTwoItems[2]}/>

        <DescriptionBoxItemWithHeading item={this.state.sectionTwoItems[3]}/>

The issue I face is that a box expands to the height of the largest content in a row.
Here is an image outlining my problem:

Would anyone know how I could achieve this?
Comment: Please provide final generated HTML without any dynamic templating code...
Comment: This is not possible with flexbox. It can only equalise dimensions in one direction. You can have equal heights (in rows) or widths (in columns) but you can't have both at the same time. You need JS.
Comment: overflow-y: auto?
Comment: @Paulie_D How could I use JS to achieve this? Thanks
Answer: You can do this in CSS by using padding-top to add the height and then absolute positioning to draw your content on top of this padding.
Here is an example: https://codepen.io/anon/pen/xpwooz
#grid2x2 {
    display: flex;
    flex-wrap: wrap;
    background-color: orange;
}

.box {
    display: flex;
    flex-basis: calc(50% - 20px);
    margin: 5px;
    justify-content: center;
    overflow: hidden;
    background: red;
    padding-top: 100px;
    position: relative;
}

.content {
  position: absolute;
  background: green;
  top: 0;
  width: 100%;
}

The problem is you can't change this height dynamically based on the content height. You can either set it to a fixed size or set it to a percentage and have it change size based on it's parents width. I think you'll need to use javascript if you want the height to change based on the largest content element.


How do I modify TIFF physical resolution metadata

Question: I have several pyramidal, tiled TIFF images that were converted from a different format. The converter program wrote incorrect data to the XResolution and YResolution TIFF metadata. How can I modify these fields?
tiff.ResolutionUnit: 'centimeter'
tiff.XResolution: '0.34703996762331574'
tiff.YResolution: '0.34704136833246829'

Ideally I would like to use Python or a command-line tool.
Comment: Thanks @cgohlke. I love tifffile. Thanks so much for developing it. I often use the Tifffile loader in kitware's Large Image package.
Answer: One can use tifftools.tiff_set from Tiff Tools.
import tifftools

tifftools.tiff_set(
    PATH_TO_ORIG_IMAGE,
    PATH_TO_NEW_IMAGE,
    overwrite=False,
    setlist=[
        (
            tifftools.Tag.RESOLUTIONUNIT,
            tifftools.constants.ResolutionUnit.CENTIMETER.value,
        ),
        (tifftools.Tag.XRESOLUTION, xresolution),
        (tifftools.Tag.YRESOLUTION, yresolution),
    ],
)

Replace xresolution and yresolution with the desired values. These values must be floats. In this example, the resolution unit is centimeter.

This is also possible with the excellent tifffile package. In fact there is an example of this use case in the README.
with TiffFile('temp.tif', mode='r+') as tif:
    _ = tif.pages[0].tags['XResolution'].overwrite((96000, 1000))

Be aware that this will overwrite the original image. If this is not desired, make a copy of the image first and then overwrite the tags.


Qualification to Vote to Close a Question

Question: On seeing this question, a point that occurred to me is that on this site it sometimes takes a while (from memory up to a day or so) for off-topic questions to be closed. If such questions were closed more quickly, this might reduce the frequency of answers to off-topic questions.  This led me to check how many participants are able to vote to close questions, and I am puzzled by what I found.
The Privileges page in the Help Centre says that it needs 500+ rep.  This would include about 100 people.  But when voting myself to close a question, I have noticed an indicator that seems to be counting the possible voters down from 25 (ie if one person has already voted it will show 24 votes remaining). Is there a reason for this apparent discrepancy, or is it an error?
Comment: This question made me look at the [Close Vote statistics](http://economics.stackexchange.com/review/close/stats) and they are scary. Top 8 reviewers make up 62% of the votes. I have never seen the top reviewer cast a vote that was not 'Leave Open' so I guess her 11% of votes should not be counted when we are looking at how fast off-topic questions could be closed. There are 79 people eligible to vote. Where are you guys...?
Comment: @denesp  I've actually cast 5 close votes since 14 Jan (eg here (http://economics.stackexchange.com/questions/15120/help-please-i-cant-work-out-this-equation)), but as has been my habit I did this via 'Close' under each question, not via the Review queue.  When I went to the Close Vote Statistics under Review, it showed my all time score as zero, so it looks as if these stats only count votes cast via the Review queue.
Comment: Btw Adam I don't see any of your votes in recent history...
Answer: I think that counter just means you can have a maximum of 25 close votes simoultaneously. After registering a close vote try to see where the counter starts from on another random question. (You don't really have to vote to close it.) The counter for me is currently at 23.
Comment: Yes, I can  see it now, here (http://economics.stackexchange.com/help/privileges/close-questions)


Can eBPF's perf_submit() be used in a socket_filter program as well?

Question: So I was trying to send some data from the kernel space program to the user space program using perf_submit.
I've done some studies and here(https://github.com/iovisor/bcc/issues/2423), yonghong-song answered(the last comment) that a socket_filter program can not access bpf_perf_event_output helper and therefore it can only be used for tracing program types.
However, on BCC reference site(https://github.com/iovisor/bcc/blob/master/docs/reference_guide.md#2-bpf_perf_output), if you ctrl+f and search for : 3. perf_submit()
, it says on the fifth line that "for SOCKET_FILTER programs, the struct __sk_buff *skb must be used instead."
I believe this infers that perf_submit() can be used for socket_filter programs as well?
So I have hard time figuring out if perf_submit() can indeed be used for a socket filter program. Maybe some functionalities have been added since Yonghong-song answered the question above?
I'm checking if perf_submit() would work with a socket filter and there's not really a line of code that grabs the data output by perf_submit because just addint perf_submit() in the kernel program already omitted an error.
Here's the code for my program :
from bcc import BPF

# Network interface to be monoitored
INTERFACE = "br-netrome"

bpf_text = """

#include <uapi/linux/ptrace.h>
#include <net/sock.h>
#include <bcc/proto.h>
#include <linux/bpf.h>

#define IP_TCP 6
#define IP_UDP 17
#define IP_ICMP 1
#define ETH_HLEN 14

BPF_PERF_OUTPUT(events);    // has to be delcared outside any function

int packet_monitor(struct __sk_buff *skb) {
    u8 *cursor = 0;
    u64 saddr;
    u64 daddr;
    u64 ttl;
    u64 hchecksum;

    struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
    if (!(ethernet -> type == 0x0800)) {
        return 0; // drop
    }

    struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
    /*
    if (ip->nextp != IP_TCP)
    {
        if (ip -> nextp != IP_UDP)
        {
            if (ip -> nextp != IP_ICMP)
                return 0;
        }
    }
    */

    saddr = ip -> src;
    daddr = ip -> dst;
    ttl = ip -> ttl;
    hchecksum = ip -> hchecksum;

    events.perf_submit(skb, &saddr, sizeof(saddr));

//    bpf_trace_printk("saddr = %llu, daddr = %llu, ttl = %llu", saddr, daddr, ttl); // only three arguments can be passed using printk

//    bpf_trace_printk("Incoming packet!!\\n");
    return -1;
}

and here is the error code :
 R0=inv2048 R6=ctx(id=0,off=0,imm=0) R7=inv0 R10=fp0,call_-1
4: (20) r0 = *(u32 *)skb[26]
5: (7b) *(u64 *)(r10 -8) = r0
6: (18) r2 = 0xffff9bde204ffa00
8: (18) r7 = 0xffffffff
10: (bf) r4 = r10
11: (07) r4 += -8
12: (bf) r1 = r6
13: (18) r3 = 0xffffffff
15: (b7) r5 = 8
16: (85) call bpf_perf_event_output#25
unknown func bpf_perf_event_output#25

Traceback (most recent call last):
  File "packet_monitor.py", line 68, in <module>
    function_skb_matching = bpf.load_func("packet_monitor", BPF.SOCKET_FILTER)
  File "/usr/lib/python2.7/dist-packages/bcc/__init__.py", line 397, in load_func
    (func_name, errstr))

Comment: What's your kernel version?
Comment: @pchaigno I am using '5.0.0-050000-generic' !
Comment: Side note: there's a copy paste error in your program. The beginning is duplicated. And the end is missing.
Comment: @pchaigno Thanks for letting me know! I edited the code part :)
Answer: TL;DR. BPF programs of type BPF_PROG_TYPE_SOCKET_FILTER can use bpf_perf_event_output only starting with Linux 5.4.

Which helpers a given BPF program has access to is defined by the get_func_proto member of objects struct bpf_verifier_ops. You can find which bpf_verifier_ops object corresponds to which program type by reading function find_prog_type() and file bpf_types.h. In the case of BPF_PROG_TYPE_SOCKET_FILTER, the corresponding function is sk_filter_func_proto().
If you git blame that function on recent kernel sources, you will get something like the following (you can do the same with GitHub's blame feature):
$ git blame net/core/filter.c
[...]
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6080) static const struct bpf_func_proto *
5e43f899b03a3 (Andrey Ignatov           2018-03-30 15:08:00 -0700 6081) sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6082) {
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6083)         switch (func_id) {
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6084)         case BPF_FUNC_skb_load_bytes:
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6085)                 return &bpf_skb_load_bytes_proto;
4e1ec56cdc597 (Daniel Borkmann          2018-05-04 01:08:15 +0200 6086)         case BPF_FUNC_skb_load_bytes_relative:
4e1ec56cdc597 (Daniel Borkmann          2018-05-04 01:08:15 +0200 6087)                 return &bpf_skb_load_bytes_relative_proto;
91b8270f2a4d1 (Chenbo Feng              2017-03-22 17:27:34 -0700 6088)         case BPF_FUNC_get_socket_cookie:
91b8270f2a4d1 (Chenbo Feng              2017-03-22 17:27:34 -0700 6089)                 return &bpf_get_socket_cookie_proto;
6acc5c2910689 (Chenbo Feng              2017-03-22 17:27:35 -0700 6090)         case BPF_FUNC_get_socket_uid:
6acc5c2910689 (Chenbo Feng              2017-03-22 17:27:35 -0700 6091)                 return &bpf_get_socket_uid_proto;
7c4b90d79d0f4 (Allan Zhang              2019-07-23 17:07:24 -0700 6092)         case BPF_FUNC_perf_event_output:
7c4b90d79d0f4 (Allan Zhang              2019-07-23 17:07:24 -0700 6093)                 return &bpf_skb_event_output_proto;
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6094)         default:
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6095)                 return bpf_base_func_proto(func_id);
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6096)         }
2492d3b867043 (Daniel Borkmann          2017-01-24 01:06:27 +0100 6097) }
[...]

As you can see, BPF_FUNC_perf_event_output was only recently added to the list of helpers these BPF programs can call. The commit which added this support, 7c4b90d79d0f4, was merged in Linux v5.4:
$ git describe --contains 7c4b90d79d0f4
v5.4-rc1~131^2~248^2~20

Comment: I read a lot of documentation on BPF, used it daily, and read the kernel sources when needed. Then, of course, taking the time to answer StackOverflow questions helps a lot too!
Comment: This was why! Thank you so much for your help! Now I will make sure to check kernel history too before I ask questions! and this is off the topic but, did you study about eBPF by reading a book or by going through websites and studying about them? I always get a lot of help from you and I just wonder how you came up to be at this level. You don't have to tell me if you feel uncomfortable because I think this could be a bit personal. Thank you so much!


Baselines comparison for Cox regression & Kaplan-Meier

Question: If I am not mistaken baselines for Cox regression & Kaplan-Meier are equal
library("survival")
res.cox <- coxph(Surv(time, status) ~ 1, data =  lung)
quantile(survfit(res.cox), conf.int = F)
25  50  75
170 310 550

km = survfit(Surv(time, status) ~ 1, data = lung)
quantile(km, conf.int = F)
25  50  75
170 310 550

quantile(survfit(res.cox), conf.int = F) = quantile(km, conf.int = F)

That's ok. But in my own dataset I get
w = coxph(Surv(stag, event) ~ 1, data = df)
> quantile(survfit(w), conf.int = F)
       25        50        75
 19.94251  50.72690 106.84189
km = survfit(Surv(stag,event) ~ 1, data = df)
quantile(km, conf.int = F)
 25       50       75
 19.8768  50.7269 106.8419

The values are almost equal but there is a difference! Why is this happening, what are the differences in the calculations baselines for Cox regression & Kaplan-Meier?
Answer: The difference might come from the handling of tied survival times in the two approaches. The Kaplan-Meier estimator simply uses the number of events at each event time in its calculations. To accommodate the multiple covariates that need to be evaluated in general in a Cox model, there needs to be special handling of multiple events occurring at the same time. There are 3 options for handling ties in coxph().
So my guess is that you had some early time point with more than 1 event, leading to the very slight discrepancy (less than 1%) for the 25th percentile time. The 50th and 75th percentile times are identical except for rounding.


How 5-vertex cover is in P?

Question: If 5-vertex cover is in P with O(n^5), then 100 vertex cover is in P with O(n^100) - so generally we can say vertex cover problem is in P!
But why is the vertex cover problem in NP?
Answer: First, let's rephrase the vertex cover problem as a decision problem instead of an optimization problem. The decision problem format asks the question "Is there a vertex cover to graph G using k vertices?" where the input includes both G and k - the maximal number of vertices required. We will denote by n, the number of vertices of the graph G.
Sure, for any fixed value of k, there is a solution in O(n^k), but the problem, is k is an input to the problem and may be as large as n/2. If you were to take a naive approach and test with an O(n^k) algorithm, you would require in your worst case, (n^(n/2)), which is certainly not a polynomial over the size of the inputs.

How 5-vertex cover is in P?


Does there exist a surjective function from $(0,1)$ to $[0,1]$?

Question: Can I say that cardinality of $(0,1)$ is less than the cardinality $[0,1]$ ?
Comment: There exists a **bijective** function from $(0,1)$ to $[0,1]$.
Comment: In the title, do you mean *injective* function? It takes an injection $A \hookrightarrow B$ to say that the cardinality of $A$ is *at most* the cardinality of $B$.
Answer: $ x \mapsto \min( \max( 2x - \frac{1}{2}, 0 ), 1 )$
Answer: $f(x) = \sin^2 (2\pi x)$ will do the job.


drupal 7 - views conditional not working

Question: I have a content type that has an External Link (field type: Link) which is an optional field. So users can either type in a url or not. I've created a View where I want to show the latest 2 content types.
I want to be able to display the nodes where its Title either links to the External Link or the url of the content. I already have Views Conditional enabled but I dont think Im setting it up correctly because the nodes arent using the proper url. The url is "http://localhost.test:8888/[path]. The 2nd node is correct though, it has http://yahoo.ca which is the External Link that I used.

If anyone has any clue why the settings arent correct, please let me know why. THanks.
Comment: I find it easier to achieve this `title links to external URL (if there) or local site url if not there` using a custom View Field Template and a php preprocessor function. Trying to do this in the Views Admin UI is a nightmare.
Answer: If I correctly understand your point, you would like a view to display two latest nodes of a specific content type. While displaying these nodes the view should check:

External link is provided or not

If External link is provided then output that as a field of view, if External link is not provided then output the URL Path of the content.
I can achieve this with just Views without the need of the Conditional one, unless I am misunderstanding your questions. I have the following steps for you to achieve what I stated here,

Create a content type with link field and either Pathauto or custom URL supplied
Create a view with content type as the one created
Choose Field for Format Show and the select whichever field of the content type you are interested including the External Link & Path fields
Click on the external field link, once opened change the title to New Title or whatever fancy
You would need to use the NO RESULT BEHAVIOR settings of this field
In NO RESULT BEHAVIOR supply the placeholder for PATH which is [path] - The path should be included among the fields
Save and there you go the result, if the node is not having an External link then it will display the path as value of external field, otherwise it preserves the supplied external URL. Screenshot for more detail

Create content types as follow:

Configure view as follow

Configure the NO RESULT BEHAVIOUR as follow

There you go the result of your view, first content types does not have external URL and the second content types does have (don't forget to include the path as one of the fields or you won't be able to access it)


Ace Code Editor : Caching mode object

Question: I have a webapp where I have to show the ace editor in multiple places. So I have a common routine that loads the ace editor with a given root element (code in coffee)
<code>editor = window.ace.edit(root)
editor.setTheme("ace/theme/github")
editor.getSession().setMode("ace/mode/json")
</code>
The issue I have is that every time I set the mode, the json-worker is fetched from the server. It seems setting the mode initiates the web worker and the web worker has to be fetch the code from a url.
I have tried caching an instance of the mode and setting that but that does not seem to change the behavior at all.
Is there any way to make the worker js load once and then reuse it in subsequent uses, without reloading it from the server?
Answer: This isn't possible since every session creates it's own worker. Loading source code of worker in the main thread as text, and using that to create blob object will be hacky and less performant than loading it multiple times in a worker.
If you are only concerned about network fetch, try setting proper cache headers for that file. If you want only one worker for all editors, you have to modify ace. You can use github to create feature or pull request.
Comment: Thanks. I figured as much by reading the code more thoroughly. It seems the best thing we can do at this point is to set the cache headers for the worker file.

Ace Code Editor : Caching mode object


Fastest way to find sub-lists of a fixed length, from a given list of values, whose elements sum equals a defined number

Question: In Python 3.6, suppose that I have a list of numbers L, and that I want to find all possible sub-lists S of a given pre-chosen length |S|, such that:

any S has to have length smaller than L, that is |S| < |L|
any S can only contain numbers present in L
numbers in S do not have to be unique (they can appear repeatedly)
the sum of all numbers in S should be equal to a pre-determined number N

A trivial solution for this can be found using the Cartesian Product with  itertools.product. For example, suppose L is a simple list of all integers between 1 and 10 (inclusive) and |S| is chosen to be 3. Then:
import itertools
L = range(1,11)
N = 8
Slength = 3
result = [list(seq) for seq in itertools.product(L, repeat=Slength) if sum(seq) == N]

However, as larger lists L are chosen, and or larger |S|, the above approach becomes extremely slow. In fact, even for L = range(1,101) with |S|=5 and N=80, the computer almost freezes and it takes approximately an hour to compute the result.
My take is that:

there is a lot of unnecessary computations going on there under the hood, given the condition that sub-lists should sum to N
there is a ton of cache misses due to iterating over possibly millions of lists generated by itertools.product to just keep much much fewer

So, my question/challenge is: is there a way I can do this in a more computationally efficient way? Unless we are talking hundreds of Gigabytes, speed to me is more critical than memory, so the challenge focuses more on speed, even if considerations for memory efficiency are a welcome bonus.
Comment: As far as I know, solutions that exist for this problem are O(`N` ** (`Slength` - 1)), which will become unmanageable for large `Slength`.
Comment: @Tomothy32 I guess you meant `|L|` where you wrote `N`, right? It should not depend on the value of `N` in my example above. I wonder if the restriction that the sub-lists' elements having to sum to a given number can be used for early exits (and to avoid wasteful memory writes) when generating the product results
Comment: Sorry, I did mean the length of `L`. Another thing to take into consideration is language. If pure speed is the aim, you should be using something like C++, Java, etc. as opposed to Python. Anyway, I came across a similar problem on LeetCode once, and I digging around I found [this](https://leetcode.com/problems/4sum/discuss/8545/Python-140ms-beats-100-and-works-for-N-sum-(Ngreater2)), which could be helpful.
Comment: Your comprehension runs through **all** 'products', which grows with L and S. It does the sum test on each one.  If only a few pass and are saved memory shouldn't be an issue.  `product` is a generator.
Comment: So I guess the numbers are all positive integers, or at least 0. And it should work for arbitrary numbers or are they distributed in a special way? For example, for a sum N=8 with size S=3 you may eliminate in a first step all numbers of size > 8-3+1, because 6+1+1 would be the only solution for a maximum M=6. For 7 there is no solution.
Comment: @userunknown Yeah, I think I can impose that numbers are non-negative for easiness of handling this. Thus, including zero - in which case there is always a solution provided that `L` is generated from a range of values that starts at minimum at zero at maximum at `N`.
Comment: So S can contain multiple instances of a number, while L doesn't? That's surprising. That 0 and N are always contained in L is surprising too. I would have expected, that L could even contain elements exceeding N. Like (2, 3, 3, 5, 10, 12) should produce a sublist of length 3 and sum 9.
Answer: So given an input list and a target length and sum, you want all the permutations of the numbers in the input list such that:

The sum equals the target sum
The length equals the target length

The following code should be faster:
# Input
input_list = range(1,101)

# Targets
target_sum = 15
target_length = 5

# Available numbers
numbers = set(input_list)

# Initialize the stack
stack = [[num] for num in numbers]

result = []

# Loop until we run out of permutations
while stack:
    # Get a permutation from the stack
    current = stack.pop()

    # If it's too short
    if len(current) < target_length:
        # And the sum is too small
        if sum(current) < target_sum:
            # Then for each available number
            for num in numbers:
                # Append said number and put the resulting permutation back into the stack
                stack.append(current + [num])

    # If it's not too short and the sum equals the target, add to the result!
    elif sum(current) == target_sum:
        result.append(current)

print(len(result))

Comment: This does not do what I described. Try with the values from my example and you will see. It is not combinations, what I am looking for.
Comment: Thanks! I'm exploring this solution. So far, I notice that it is worse than the naive version for `|S| < 4` but it seems to cut runtime in almost half for `|S| >= 4`, regardless of the size of `|L|`


Suggestions/Tricks for Throttling a PHP script

Question: I have a scheduled task that runs a script on a regular basis (every hour). This script does some heavy interaction with the database and filesystem and regularly takes several minutes to run. The problem is, the server's cpu-usage spikes while the script is running and slows down normal operations. Is there a way to throttle this process so that it takes longer but does not consume as many resources?
I've looked at different configuration options for PHP but there does not appear to be any that fit my needs.
Setting memory_limit in php.ini to something lower causes my data objects to overflow quite easily.
I've seen similar posts where people suggested using sleep() at certain points in the script but that does not prevent the script from spiking the server.
The optimal solution would be some way to tell the Lamp (in this case Wamp) stack to only use 10% max cpu utilization. I'm not concerned at all about runtime and would prefer that it take longer if it means saving cpu cycles per second. My alternate solution would be to setup a different server with database replication so the cron could go to town without slowing everything else down.
Environment: Windows Server 2k3, Apache 2.2.11, PHP 5.2.9, MySQL 5.1
I appreciate any insight to this situation.
EDIT: I appreciate all the answers, even the ones that are *nix-specific. It's still early enough in my situation to change the hosting environment. Hopefully this question will help others out regardless of the OS.
Answer: This is a tricky problem.  If you're running the PHP script via the command line, you can set the process's scheduling priority to low (start /low php.exe myscript.php I believe).  If your PHP script itself is actually doing most of the processing that's eating your CPU, this might work.  However, you said you are doing some heavy database and filesystem interaction, which this solution will not help.  It looks like there is a MySQL hint "LOW_PRIORITY" for INSERT and UPDATE queries that may help you there, but I have not tried those.
Answer: You can set processes in Windows to be a lower priority. I'm not sure how the process is being kicked off, but if you set the process to be a low priority, whatever wants CPU resources will get them if you set the priority to be really low.
Answer: In UNIX (LAMP) I managed to solve the problem by checking the load of the server before continuing the loop
function get_server_load($windows = 0) {
    $os = strtolower(PHP_OS);
    if(strpos($os, "win") === false) {
        if(file_exists("/proc/loadavg")) {
         $load = file_get_contents("/proc/loadavg");
         $load = explode(' ', $load);
         return $load;
        }
        elseif(function_exists("shell_exec")) {
         $load = explode(' ', `uptime`);
         return $load;
        }
        else {
         return "";
        }
    }
}

for(... ... ...){
    $data = get_server_load();
    if($data[0] < 0.2){
     // continue
    }else{
        sleep(1);
    }
}

This function should work also on windows but I can't guarantee it. On linux it gives you back an array with the load of the last 1 minute, 5 minutes and 15 minutes
Also, consider to start your scripts (if by CLI) with a lower priority (in Linux, use "nice")
You can also use other values before continuing the loop, like the number of Apache active processes (you can parse the page 127.0.0.1/server_status?auto if you enabled the mod_status in httpd.conf), or also the MySQL situation (active connections?)
Answer: Can you alter your cron entry to launch your script using nice?
Comment: Oops, sorry. I see you're running Windows. I was thrown off by the 'lamp' and 'cron' tags!
Comment: Thanks for your answer anyway. It's still possible in my situation to change the environment so I still found your solution helpful.
Answer: Not a good idea to use a server for serving clients and analyse data.
So if you are looking for a final solution, make a few redesign of your application and offload the data analysis from the frontends and the live database to another system dedicated to this task.
Even if you can successfully throttle the analyzer, it would use up precious resources otherwise would be available to serve the users.
Answer: This might be a difficult change but it may worth it refactoring your data structures into iterators. Also, if you have circular references in your code, provide a method like clearReferences() that unsets these objects. This is a problem that is solved in PHP 5.3 by the way.
So if you have:
class Row
{
    protected $_table;

    public function __construct($table)
    {
        $this->_table = $table;
    }
}

class Table
{
    protected $_row;

    public function __construct()
    {
        $this->_row = new Row($this);
    }
}

Add a clearReferences() method to the Row class:
class Row
{
    public function clearReferences()
    {
        $this->_table = null;
    }
}

That's all I can come up with for the moment.
Answer: I have a bunch of scripts that I run from cron in a similar way using nice:
0 * * * * nice -n19 php myscript.php
This won't help the RAM utilization (only changing the way the script is written can do that), but it only uses the CPU that would otherwise be idle.
EDIT: didn't see that the question involved a Windows environment, sorry... leaving this in for any *nix users having the same problem..
Answer: Perhaps what your script is simply trying to do too much all at once. Would it do less if it ran three times an hour?
Another solution might be to setup an additional server just for running this sort of 'backend' processing. This would particularly effective if it is not putting undue load in the database, just the web server.
Yet another approach to look at is whether it's work can be divided in a different direction. These sorts of scripts often have a few big SQL statements that generate results used to generate a whole lot of little SQL statements. If the latter could be put aside somewhere, they can be run against the database as a later step. Such an approach might also let you use an unbuffered query to fetch the pre-processing data which could cut down significantly on memory consumption by the PHP code.
Answer: If you have it(Apache) running as a service, you can change the priority
settings in the Win control center /services.
Your CPU usage will spike anyway, but other programs will be preferred
by the scheduler.
Also try putting the database/server on a different hd than your
Applications.


Find the Blazor component class corresponding to a given path

Question: In a Blazor server app, how can I find the class (return as Type) of the razor component that matches a given path (in string, say "/controller/action")? I don't want to navigate to the actual page; I only wish to find the corresponding class and perform some reflection-type stuff with it. It appears to me that the way Blazor handles route is very different from that of MVC and I couldn't quite find any documentation that helps.
Answer: You can use reflection to find all components with a RouteAttribute attribute. @page directive are transformed into RouteAttribute.
var pageTypeList = Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => t.GetCustomAttribute(typeof(Microsoft.AspNetCore.Components.RouteAttribute)) != null);

Comment: Looks like we're on the right direction; however, in my use case, it is possible that `@page` templates contain parameters. I could try to write my own parser to compare the templates with the given path, but is there a built-in way to do this?
Comment: No, but when you get page type, you can query for the attribute value
Comment: That can inspired you : https://github.com/dotnet/aspnetcore/blob/4a7bf75e89e3dfe7264a641ed041f50788d28d20/src/Components/Components/src/Routing/RouteTableFactory.cs
Comment: Aha, that most certainly looks like what I'm looking for. I'll study those code and see if I can figure things out. Thanks.
Comment: I just came up with a solution; check it out if you're interested.
Comment: The issue with your solution it's it can be broken in a next release of blazor because you call non public members and thoses members can be removed or changed. It's more a hack than viable solution. Do you agree ?
Comment: I certainly do agree, but for now since the built-in parsing mechanism is all non-public, I don't really have a better option. Writing my own parser is equally non-viable as the template syntax is also subject to change.
Answer: Note: This is more of a comment for the accepted answer, but I don't have the rep.
TL;DR - RouteData.PageType will solve the original problem for most simple use cases. If you are experiencing inconsistent results with this - use an updated version of Mu-Tsun Tsai's answer, clarified below.
I had a similar situation that lead me here. I used Mu-Tsun Tsai's answer. While I was doing this, I found that the RouteData.PageType provides a similar value - which probably works in most simple cases.
Unfortunately, my case needed that PageType in order to determine if the navigation was valid - and if not, then navigate to a default page. I suspect that the nested navigation caused a race condition, that wasn't picked up by RouteData.PageType - for some reason, it returned the PageType of the referring page, instead of the PageType of the destination page in limited and inconsistent circumstances.
I ultimately reverted to Mu-Tsun Tsai's answer because it is able to determine the type based on the destination page.
Caveats

The use of reflection has already broken the original answer. An update needs to be made to use routes.GetType().GetMethod("Route", BindingFlags.Instance | BindingFlags.Public).Invoke(routes, new[] { context });
The path needs to be relative or it will not detect the page type
Comment: While trying to update my code-base that used this solution, I came across [this answer](https://stackoverflow.com/a/63225118/13350243) by @enet. It removes the need for using the fragile reflection. I think that solution is the best for reliability and maintainability, and works well with Blazor components. I've already incorporated it into my code with minimal changes from what has been pasted in these answers.
Answer: After carefully studying the source code provided by agua from mars, I came up with the following solution. First step is to obtain the Router object created by the Blazor framework, and I do so by using CascadingValue in my App.razor:
<Router AppAssembly="@typeof(Program).Assembly" @ref="router">
    <Found Context="routeData">
        <CascadingValue Value="@router">
            <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
                ...
            </RouteView>
        </CascadingValue>
    </Found>
    <NotFound>
         ...
    </NotFound>
</Router>

@code {
    private Router router;
}

So now in any page of my app I can obtain the Router object using CascadingParameterAttribute. Next comes reflection (since a lot of things involved here are defined as internal):
public Type Find(Router router, string path) {
    var assm = typeof(Router).Assembly;
    var routes = typeof(Router).GetProperty("Routes", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(router);
    var type = assm.GetTypes().FirstOrDefault(t => t.Name == "RouteContext");
    var context = Activator.CreateInstance(type, new[] { path });
    routes.GetType().GetMethod("Route", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(routes, new[] { context });
    return type.GetProperty("Handler").GetValue(context) as Type;
}

And this function achieves exactly what I need.
Comment: This looks like exactly what I need, but could you provide an example of usage?
Comment: Well, I used to use these code in the following scenario: in the navigation menu, where I have a bunch of links to the various pages of my site, I create a custom `menuItem` component which, given only the link url, it locates the corresponding page component, automatically generates title text (by a custom attribute on my page), and decides whether to show the link based on authorization settings of that page.
Comment: But later I thought, hey, why don't I just pass the page type to my `menuItem`, and generates the link url based on the first route setting of that page instead? This way I can avoid using the `Find` function given above, which uses reflections to access non-public members (not exactly good practice). So eventual I don't use these codes anymore.


postgresql: time stored as text. how to query with respect to time

Question: I have a table called device with following data types
device table:

column        Type
id            integer
created       text
name          text

Here the time is stored in text type instead of timestamp
Eg: created value 12/19/2020 20:40:23
I try to query with this date time.
SELECT "device"."id",
       "device"."created",
       "device"."name",
FROM "device"
WHERE "device"."created" < '12/19/2020 20:40:23'
LIMIT 21

the results are not as per datetime order. it might be comparing some text string.
So what is the best solution in this case to get data w.r.t time eventhough its stored as text
Comment: This is  a **really**, really bad idea. Do you have a chance to fix that broken database design?
Comment: yes, I can. What do you suggest to do.Is it better to convert it into timestamp with zone. Also i have to index this column in future, its a lot of data, every sec two rows get added. P
Comment: Yes you should convert it to a `timestamptz` (or at least `timestamp`)
Answer: That's because strings are compared lexicographically. Use to_timestamp with a format specification string to convert text to timestamp and then compare timestamps.
If you format date-time strings according to ISO-8601 then you can cast them directly to timestamp.
Unless this is a (foreign?) table out of your control do not store timestamps as formatted text.
SELECT id, created, name
FROM device
WHERE to_timestamp(created, 'mm/dd/yyyy hh24:mi:ss') < '2020-12-19 20:40:23'::timestamp
LIMIT 21;

And btw you do not need quoting.
Answer: You need to convert the text to timestamp.  In this case a simple convertion suffices.
SELECT device.id,
       device.created,
       device.name,
FROM device
WHERE device.created::timestamp < '12/19/2020 20:40:23'
LIMIT 21

Answer: The best solution in this case would be to convert the created column from text to timestamp using the ALTER TABLE statement. This will allow you to use proper date and time functions and operators when querying the table, ensuring that the results are sorted in the correct order.
For example, you can use the following SQL statement to convert the created column to timestamp:
ALTER TABLE device
ALTER COLUMN created SET DATA TYPE TIMESTAMP USING
to_timestamp(created, 'MM/DD/YYYY HH24:MI:SS');

This statement uses the to_timestamp function to convert the text values in the created column to timestamps using the format MM/DD/YYYY HH24:MI:SS which match the format of your text date.
Once you have made this change, you can now query the table using proper date and time functions and operators, like so:
SELECT "device"."id",
       "device"."created",
       "device"."name",
FROM "device"
WHERE "device"."created" < '12/19/2020 20:40:23'::timestamp
ORDER BY created
LIMIT 21


AWS NVMe Storage - hosting a database

Question: Now that AWS are offering NVMe through the i3 range of servers, is there a best practice for hosting a database on the instance storage of one of these?
My understanding is that if the instance is stopped, the storage may be completely wiped.  This doesn't appear to be the case if the server reboots, intentionally or unintentionally, but you are still one button press away from wiping important data so this is quite scary.
My understanding of the underlying infrastructure is that this is because the NVMe storage is directly attached to the physical host, and therefore if Amazon decide to move your VM to another host you would lose your data.  Also it would be bad to store mission-critical data on a single hardware device AWS aside.
But given the performance benefits of NVMe over EBS (SAN?) storage, what would a recommended setup be? VM Replicas, transaction log backups to permanent storage, etc.
Answer: It is possible to turn the NVMe SSDs on i3 instances into persistent highly available storage.
Options:
1) Mirroring between NVMe SSDs on 2 or 3 instances
2) Mirroring between NVMe SSDs and EBS (EBS can be on a different instance) with reads primarily from NVMe SSDs.
While write performance will still be limited by network or EBS, you do get full read performance of NVMe. In most cases read bandwidth is what large databases really need for running heavy queries.
However, there are still questions about failing over the database between the instances and restoring redundancy after instance stop/start of failure.
Check this whitepaper and page 9 specifically for details about how it is done for Oracle database clusters:
https://www.flashgrid.io/wp-content/sideuploads/resources/FlashGrid_OracleRAC_on_AWS.pdf
The paper is focused on Oracle RAC databases, but same solution works for single-instance Oracle and also for any other Linux-based database. Although, you would still need Oracle Clusterware (free).
Comment: Thanks, that paper looks fascinating.  Nice fist post, if you registered just to answer this question I appreciate it!

AWS NVMe Storage - hosting a database


Do C++ ranges support projections in views?

Question: I know algorithms (e.g. sort) in ranges support projection, but it seems to me that there is no way to get that functionality for views...
Am I right?
As an example consider following working code:
#include <algorithm>
#include <ranges>
#include <vector>
#include <iostream>

enum Color {
   Red,
   Green,
   Blue
};
struct Cat {
   int age;
   Color color;
};

int main() {
    std::vector<Cat> cats{{.age = 10,.color=Color::Red}, {.age = 20,.color=Color::Blue}, {.age = 30,.color=Color::Green}};
    auto is_red = [](const auto& cat) {return cat.color == Color::Red;};
    for (const auto& cat: cats | std::views::filter(is_red)) {
        std::cout << cat.age << std::endl;
    }
}

Is there a way to remove the lambda and do something like:
for (const auto& cat: cats | std::views::filter(&Cat::color, Color::Red) {

note: my question is for member variable projection, but obviously in real code member function calls would be also needed.
Comment: If allowed, syntax would be (to match algorithm) `std::views::filter([](Color color){ return color == Color::Red; }, &Cat::color);`.
Comment: @Jarod42 IDK, I see your point, but it ruins the left to right flow... :)
Comment: `views::transform` + `views::filter` + `.base()` may help.
Comment: @康桓瑋 I don't see how I can go from the transformed filtered range back to the untransformed elements that where filtered in using `.base()`.
Comment: @Yakk-AdamNevraumont. https://godbolt.org/z/6jvaK6PY1
Comment: @康桓瑋 So we need an iterator remapping view maybe.  Like, `std::views::transform(&Cat::color) | filter_equal(Color::Red) | base_elements | base_elements`
Answer:
Do C++ ranges support projections in views?

No (although range-v3 did†).

Is there a way to remove the lambda and do something like:
std::views::filter(&Cat::color, Color::Red)

That wouldn't really be how this would work with projections anyway. It would have been:
filter([](Color c){ return c == Color::Red; }, &Cat::color)

Which you can reduce this if you have an equals that returns a predicate:
filter(equals(Color::Red), &Cat::color)

But adding projections to algorithms is kind of unnecessary. You can always provide a projection manually. Using Boost.Hof's appropriately-named proj function adapter, which satisfies proj(p, f)(xs...) == f(p(xs)...) (i.e. we're applying p on each argument before passing them into f):
filter(proj(&Cat::color, [](Color c){ return c == Color::Red; }))

or, shorter:
filter(proj(&Cat::color, _ == Color::Red))

Demo.

†Even in the range-v3 implementation, it's not that remove_if_view had explicit support for projections. It's that the overload that took a projection manually composed the predicate for you as compose(pred, proj). In range-v3, compose(f, g)(xs...) can mean either f(g(xs...)) or f(g(xs)...) depending on how g is invocable. So in this case, it's a projection rather than function composition. In Boost.Hof, there is are distinct compose and proj adaptors for the two cases.
Comment: Yes, my use of projection was wrong, I was looking for something like projection + nonexisting filter (in another universe where current filter is named filter_if and filter is checking values, not predicates).
Comment: BTW just noticed the _ in answer, it is nice, but I am a bit concerned that so many libs(e.g. gmock) use it...  But actually that or lambda2 are great for simple cases.
Answer: Other answer is correct since my use of projection was wrong, I wanted a projection and also equivalent of (assuming std::filter in C++ is named std::filter_if) imaginary std::filter that uses == for comparison, like we have std::count/std::count_if std::find/std::find_if pairs in STL.
If somebody needs something like this this seems to work, but it has a potential to be slow since perfect forwarding in lambdas is tricky to do, so I did not bother.
( I use keep since it is nicer name for me, but it can also be named filter_memb).
template<typename Member, typename Val>
auto keep_memb(const Member& memb, const Val& val){
    return std::views::filter([=](auto& object){return std::invoke(memb, object) == val; } );
}

godbolt


Drums: Playing consecutive beats with the same hand compared to alternating hands

Question: I am currently learning the Seven Stroke Roll ,
Drum tune : Right  , Right  , Left  Left ....

Will playing the above tune : Right , Left , Right , Left instead of Right , Right , Left ,  Left result in a different tune being played or there is no difference???
Comment: If you play them on different heads, then sure it will.
Comment: At the risk of being absurd: if your left and right hands have any difference in attack speed or strength, there will be some difference in the perceived cadence.
Comment: @TimSeguine different head??
Comment: I mean each hand on a different drum
Answer: At this stage, it's learning how to control the bounce of the stick - the second hit comes straight after the first, same hand, but after a bounce. Doing it this way also frees up the right hand after the roll, to maybe hit a crash. Alternating puts the last hit with right hand, so cuts down on time to reach a cymbal.
However, every roll or fill you learn should be learnt in several different ways, so that you could, if you want, finish it with a cymbal played by either hand.If you can get each hand to play a drum and the sound is the same, that's got to be a bonus, hasn't it ? Because most people will make a slightly different sound with l/r stick.
Answer: The indicated sticking (RRLLRRL) can be played at a higher speed by utilizing stick bounce, like you would in the standard double-stroke roll, than could you could easily achieve with single strokes (RLRLRLR) This is just a set length. This Seven Stroke Roll is just a double-stroke roll of finite length with a single stroke at the end.
The other thing that's nice about this sticking is that it can easily accommodate added musicalilty. For instance, in set drumming, you might use a rudiment like this as a small fill or flourish. You might then want to shape the rudiment to put an emphasis on that last stroke. diga-diga-diga-DAH! The fact that there are two right hand strokes before the last stroke gives your left hand time to wind up a harder stroke or rimshot, or move to another drum or crash cymbal.
In theory there is no difference between (RRLLRRL) and (RLRLRLR), but in practice there always will be. They may not even sound different, but they will feel different to you. It's good to practice both. (RRLLRRL) I think is easier to play quickly and easier to shape, but it's difficult to play quickly on drums that don't bounce well (like toms). (RLRLRLR) is easier to play on drums that don't bounce well or to play slowly, where bouncing is cumbersome. It's also easier to play the whole thing at max volume, like if you wanted all seven strokes to be rimshots. So ideally you want to practice until both styles come completely natural to you, and you can easily transition from one to the other. That way you can choose which one you think better fits the context of what you're playing.
So to explicitly answer your question, you aren't really playing a different tune when you play (RRLLRRL) vs (RLRLRLR) if they are both played in exactly the same way. But the two different ways lend themselves more easily to playing different tunes.
Answer: Theoretically if you master this roll, there should be no difference between the two versions you mention, so it's of course good to practice both versions. However, as others already have indicated there are other considerations. I'll add another: from what I know, this exercise, among others similar, is also an introduction to playing tremolo with double strokes. Double strokes make handling and exploiting the bouncing effect easier.
Answer: That depends on what you mean with "different tune". It will sound different for sure, which you probably have noticed, and it will not be the intended exercise.


How do I release an InMemoryWorkspaceFactory in the ESRI Map Control?

Question: I am embedding the ESRI Map Control into a custom ActiveX control written in MFC/C++. The custom ActiveX control serves as a Map Control wrapper so I can embed it into a specific environment that is runtime only and non-relational. Thanks to this site, I am able to load feature points into an in-memory workspace. However, the hosting environment now gets stuck in memory when closed, and it only does this when I am using the InMemoryWorkspaceFactory. My conclusion, therefore, is that the factory has locked a file or resource and, by not releasing it, is causing the host environment to never close.
Is there a way I can manually delete all features and feature classes during my ActiveX control's WM_DESTROY message and release the workspace completely? There doesn't seem to be a counterpart to the IWorkspaceFactory's Create method. My code is releasing all interfaces to the workspace factory, but apparently there are still references internally in ArcObjects.
Answer: I discovered the answer to my question. It wasn't the in-memory workspace that needed to be closed, it was ArcObjects in general. The solution is to make sure to call the Shutdown method of the IAoInitialize interface during the WM_DESTROY message.