Como fazer formulario em etapas?

Question: Estou criando um sistema e queria saber como fazer um formulário por etapa , ele funciona por checkbox e input e tipo só queria saber como passar os dados do primeiro formulário para o dois para ser inserido no mysql.

    Refeição
 Quantidade

  <input type="checkbox" name="pedido_refeicao[]" value="Costela de Tambaqui sem Espinha">Costela de Tambaqui sem Espinha
  <input type="text" name="num_refeicao[]" size="7">

  <input type="checkbox" name="pedido_refeicao[]" value="Lombo de Tambaqui Frito sem Espinha">Lombo de Tambaqui Frito sem Espinha
  <input type="text" name="num_refeicao[]" size="7">

  <input type="checkbox" name="pedido_refeicao[]" value="Caldeirada de Tambaqui sem Espinha">Caldeirada de Tambaqui sem Espinha
  <input type="text" name="num_refeicao[]" size="7">

<input class="btn" type="submit" value="Proximo" name="Proximo">
</form>

E na pagina dois do formuário é esse de baixo mas no primeira etapa do formulário trabalho com checkbox e como vou selecionar vários deles terei que usar o implode para pegar todo os checkbox e input como poderia fazer ? Obrigado desde já
     if($_POST)
    {
        $numero_mesa = $_POST['numero_mesa'];
        $pedido_refeicao = implode(', ', $_POST['pedido_refeicao']);
        $num_refeicao = implode(', ', $_POST['num_refeicao']);
}

<?php

session_start();

$nome = isset($_POST['nome']) ? $_POST['nome'] : '';
$_SESSION['nome'] = $nome;

$email = isset($_POST['email']) ? $_POST['email'] : '';
$_SESSION['email'] = $email;

$telefone = isset($_POST['telefone']) ? $_POST['telefone'] : '';
$_SESSION['telefone'] = $telefone;

?>

Comment: Você não precisa criar N formulários, basta dividir e fazer como as abas em *tab*. Submeteu o primeiro bloco, esconde esse e exibe o próximo... ao final quando der *submit*, o formulário completo será enviado.
Answer: Use javascript!
A ideia é como Papa Charlie respondeu no comentário da sua pergunta.
Pensei na seguinte proposta:
Etapa1 - Assim que o imput "submit/next" for acionado, event.preventDefault() não deixa que os dados sejam enviados, enquanto a etapa2 aparece na tela.
Etapa2 - Já na etapa2, assim que acionar "send", os dados são enviados via POST com Fetch API (ou qualquer outra maneira que você desejar).
O restante é com vocês.

(function () {

const form = document.getElementById('form');
const etapa1 = document.getElementById('etapa1');
const etapa2 = document.getElementById('etapa2');
const URL = ""; //colocar a url desejada

form.addEventListener('submit', function (event) {
  event.preventDefault();
  submit(this)//recuperar dados enviados
  .then(trocarEtapa) //trocar a div do form
  .then(post)//enviar dados
  .catch(error => console.error(error));
});

async function submit(context) {
  const formData = new FormData(context);
  return formatFormData(formData);
}

function trocarEtapa(data){
     etapa1.style.display = "none"; //esconder etapa1
     etapa2.style.display = "block"; //mostrar etapa2
     return data;
}

function post(data) {
    document.getElementById('btn-send').addEventListener('click', function () {
        fetch(URL,
            {
               method: 'POST',
               headers: {
                           'Content-Type': 'application/x-www-form-urlencoded'
               },
               body: 'json=' + JSON.stringify(data)
            }
       ).then(response => {
            if (response.status === 200) {
            } else if (response.status === 403)
                console.log('Access denied');
              else if (response.status === 404)
                console.log('Page not found');
              else if (response.status === 503)
                console.log('Be right back');
        });
   });

 })(document);
<form id="form">

             <input class="btn" id="submit" type="submit" value="Next" name="Next">

             <input class="btn" id="btn-send" type="button" value="Send" name="Send">

</form>
Answer: Você pode usar a variável SESSION[ ] do PHP
<?php

session_start();

if(isset($_POST['Proximo1'])){
    $_SESSION["v1"] = "xx";
    $_SESSION['refeicao'] = $_POST['refeicao'];
}
if(isset($_POST['Proximo2'])){
    $_SESSION["v2"] = "xx";
    $_SESSION['refeicao1'] = $_POST['refeicao1'];
}
?>
<form action=""  method="post">

            Refeição
            Quantidade

            <input type="checkbox" name="pedido_refeicao[]" value="Caldeirada">Caldeirada
            <input type="text" name="refeicao" size="15">
            <input class="btn" type="submit" value="Proximo" name="Proximo1">

</form>
<?php

if(isset($_SESSION["v1"])) {
    ?>
    <form action=""  method="post">

<?php echo $_SESSION['refeicao']; ?>

                Refeição
                Quantidade

                <input type="checkbox" name="pedido_refeicao[]" value="Caldeirada">Caldeirada de Tambaqui
                <input type="text" name="refeicao1" size="15">
                <input class="btn" type="submit" value="Proximo" name="Proximo2">

    </form>
    <?php
}
if(isset($_SESSION["v2"])) {
    ?>
    <form action=""  method="post">

<?php echo $_SESSION['refeicao1']; ?>

                Refeição
                Quantidade

                <input type="checkbox" name="pedido_refeicao[]" value="Caldeirada">Caldeirada de Tambaqui sem Espinha
                <input type="text" name="num_refeicao[]" size="7">
                <input class="btn" type="submit" value="Proximo" name="Proximo">

    </form>
    <?php
    session_destroy();

}

Answer: Não sei se é isso que você quer, mas acho que você tem que abrir um form da seguinte maneira:

Aonde está escrito formulario2 seria sua pagina em PHP que você criou para dar um insert no MySQL com o PHP.

<form action="formulario2.php"  method="post">

            Refeição
            Quantidade

            <input type="checkbox" name="pedido_refeicao[]" value="Costela de Tambaqui sem Espinha">Costela de Tambaqui sem Espinha
            <input type="text" name="num_refeicao[]" size="7">

            <input type="checkbox" name="pedido_refeicao[]" value="Lombo de Tambaqui Frito sem Espinha">Lombo de Tambaqui Frito sem Espinha
            <input type="text" name="num_refeicao[]" size="7">

            <input type="checkbox" name="pedido_refeicao[]" value="Caldeirada de Tambaqui sem Espinha">Caldeirada de Tambaqui sem Espinha
            <input type="text" name="num_refeicao[]" size="7">

    <input class="btn" type="submit" value="Proximo" name="Proximo">
</form>

Espero que seja isso que você está à procura! Abraços e boa sorte.

Como fazer formulario em etapas?


How to show integral is related to complementary error function?

Question: I have to somehow show that:
$$
\int_0^\infty{\frac{e^{-a t}}{t^{1/2}(t+x)}} \textrm{d}t=\frac{\pi}{\sqrt{x}} e^{a x} \textrm{erfc}(\sqrt{ax})
$$
I've tried substituting $u=\sqrt{t}$ to get
$$
2 \int_0^\infty{\frac{e^{-a u^2}}{u^2+x}} \textrm{d}u
$$
which at least has the Gaussian but I don't know where to go from here.
I've also tried looking at the problem backwards but I don't seem to be able to get the polynomial in the denominator.
What's the approach for these problems involving the error function?
Thank you!
Answer: Consider the integral
$$\int_0^{\infty} du \frac{e^{-a u^2}}{u^2+x} = e^{a x} \int_0^{\infty} du \frac{e^{-a (u^2+x)}}{u^2+x} = e^{a x} J(a)$$
Then
$$J'(a) = \int_0^{\infty} du e^{-a (u^2+x)} = \sqrt{\pi} e^{-a x} a^{-1/2}$$
Integrating
$$J(a) = \sqrt{\pi} \int_0^a da' \, a'^{-1/2}\, e^{-a' x} = \sqrt{\frac{\pi}{x}}\int_0^{\sqrt{a x}} dt\,e^{-t^2} = \frac{\pi}{2 \sqrt{x}} \operatorname*{erf}{\left ( \sqrt{ax}\right)}$$
Comment: Thanks! I did not even think of doing something like $J(a)$.


Conditionally removing from array if last character is []

Question: I'm trying to get all stock tickers/abbreviation from a page. But when I scrape the page, some unwanted tickers come along, basically they all end with F or (space) for example "BDLL4F " or "QCOM34F" . I managed to remove them using gsub() and regex.
stocktickers = c("PETR4", "VALE3", "MDNE3", "BDLL4F ", "QCOM34F", "SANB11", "USIM5")
stocktickers = gsub("(.*[ F]$)","NULL",stocktickers)
stocktickers = stocktickers[stocktickers!="NULL"]

> stocktickers
[1] "PETR4"  "VALE3"  "MDNE3"  "SANB11" "USIM5"

Is there any function that would conditionally drop the string from the array if the value begins or ends with a specified character or integer?
Comment: if you're trying to parse html, you'll have better luck with the `rvest` package instead of regex. If you're onlu trying to sort out a regex matching question, consider simplifying your question to focus on that, with an example of input and desired output
Answer: You can remove the unwanted tickers using regex.
For example, with grep :
stocktickers = c("PETR4", "VALE3", "MDNE3","BDLL4F ","QCOM34F", "SANB11","USIM5")
grep("(F|\\s)$",stocktickers, value = TRUE, invert = TRUE)
#[1] "PETR4"  "VALE3"  "MDNE3"  "SANB11" "USIM5"

This removes the values from stocktickers where it ends either with "F" or whitespace.
Answer: An option with endsWith in base R
stocktickers[!endsWith(trimws(stocktickers), "F")]
#[1] "PETR4"  "VALE3"  "MDNE3"  "SANB11" "USIM5"

data
stocktickers = c("PETR4", "VALE3", "MDNE3","BDLL4F ","QCOM34F", "SANB11","USIM5")


Is CRON secure?

Question: I want to install cron on my live web server to run a daily backup script of a local folder. Are there any security considerations I need to be aware of when installing cron?
My web server uses Ubuntu Linux 10.10.
Comment: Which OS?  Please be specific.  Windows Cron is as insecure as Windows.  Linux cron is still Linux and has all the security of the OS installation.
Comment: What are you afraid of?
Comment: @S.Lott Windows isn't insecure. Bad Windows admins make Windows insecure. If you let me admin a linux box it'll get broken into in about 2 minutes.
Comment: Maybe I'm just a bit paranoid because it is the live system. It did not came pre-installed, so I was unsure if there are some security considerations that needed to be made.
Comment: @mrdenny♦: "Windows Cron is as insecure as Windows" can be read as "Windows Cron is as secure as Windows".  It sure seemed like a trivial isomorphism.
Answer: In essence it's secure, but also it is another way for an attacker to, once compromised the system, make some backdoor persistent and/or auto-open it anytime you close it.
You can use the files /etc/cron.allow and /etc/cron.deny to just make your user able to use it. Both have the same format: 1 username per line.

If /etc/cron.allow exists, only the users listed there would be able to have a crontab. No more files are taken into account. Kind of a whitelist.
If /etc/cron.allow does not exist, but /etc/cron.deny does, then anyone but those listed there can have a crontab. Kind of a blacklist.
If neither of them exist, then depending on the UNIX/Linux version then anyone may be allowed to use it, or just the super user (Debian/Ubuntu allow anybody, while redhat based versions seems to only allow root).

In ubuntu by default /etc/cron.deny exists. You can create /etc/cron.allow and put there just your user.
Take into account that these files only manage the users allowed to have a personal crontab (ie. execute crontab -e). The system-wide crontab (/etc/crontab, /etc/cron.d/*,  /etc/cron.daily/*.  /etc/cron.weekly/*,  /etc/cron.monthly/*) will work regardless of the cron.allow/cron.deny files.
Comment: I'm not convinced by your third statement "3. If neither of them exist, then anyone can use it.". The crontab(1) man pages from different systems I have seem to all agree that "If neither of these files exists then only the super user will be allowed to use crontab.".
Comment: @GZBK it may depend on the system. On debian & ubuntu if these files do not exist then all users can use cron. Going to edit...
Comment: On my side I was referring to OpenBSD and OpenSUSE. Interesting enough I see now that FreeBSD document the behavior is "depending on site-dependant on configuration parameters" without expliciting the actual involved parameters. At last, the POSIX man page states that under such circumstances "only a process with appropriate privileges shall be allowed to submit a job", the concept of "appropriate privileges" remaining undefined as far as I know. Therefore in such circumstances I think the behavior is just variable and I would definitively recommend to avoid having none of these files present.
Answer: Yes, it's secure. Just make sure the scripts you run with it are secure. Review them yourself and give them only the rights they need.

Is CRON secure?


Make a task run periodically with time given by user

Question: I have to built a Django application, to send e-mail notifications to the user. But the user wants to set the time period at which their e-mail is sent. I read that celery has a periodic task functionality, but the time must be set beforehand, so how can i make a periodic task with time given by user, with celery?
class UserMail(models.Model):
    user_mail = models.EmailField()
    auto_send_mail = models.BooleanField(default=False)
    time_set = models.TimeField(blank=True,null=True)

Time set to get a time given by the user when they post in the form.
Comment: You can use celery ETA, so when the user save the time_set you can add task to celery with timedelta.
Answer: Use threading.
You can get more details at https://docs.python.org/3/library/threading.html
Answer: I think this article can help with what you need.
Another option is to instead design a periodic task running each 1 min, that will query UserMail to check if some records do have time_set within during minute and send emails.
Also note that periodic tasks scheduler in celery is not designed to handle thousands of scheduled tasks.
Answer: So Basically you want to create the Task with the time inputted by the user. What O have is an API, and I take the input from the user in the form of a POST request.
Here, I coped the data sent by the user in a variable named response.
Then, We create a schedule with the time given by the user in minutes.
and Finally we make the required periodic task. In your case, if you have a function called send_mail in tasks.py file in your app named resources, the code will look like this.
response = request.data.copy()

schedule, created = IntervalSchedule.objects.get_or_create(
    every=response['interval'],
    period=IntervalSchedule.MINUTES,
)

task = PeriodicTask.objects.create(
    interval=schedule,
    name=f'{send mail task for user {self.request.user.id}| '
    task='urlmonitor.tasks.send_mail',
    args=json.dumps([self.request.user.email]),
    # expires=datetime.utcnow() + timedelta(seconds=30)
)

Comment: Hi, please fix your indentation!
Comment: Hi @Adam, Thank you for notifying. I fixed the indentation.


How to control the family-wise error rate testing simultaneous hypothesis in two-way Anova?

Question: I guess explaining with an example would be best. Let's say we are researching which type of fertilizer and planting density produces the greatest crop yield in a field experiment. We assign different plots in a field to a combination of fertilizer type (1, 2, or 3) and planting density (1=low density, 2=high density), and measure the final crop yield in bushels per acre at harvest time.
So, how can we test the 2 null hypotheses simultaneously while controlling the family wise error-rate at alpha = 0.05:
H01: There is no difference between the effects of the fertilizer type on the amount of harvest.
H02: There is no difference between the effects of the planting density on the amount of harvest.
I know how we can do this type of test in one way anova, but I am confused for the two-way case.
Thanks!
Answer: A simple solution is to use Bonferroni here. Just require .025 thresholds for the p-values. Because of the 2 way structure, your test statistics should not be highly correlated, so Bonferroni will not lose much power relative to the more powerful (but harder to implement) solution that utilizes correlation structure.
Comment: I don't see why hypothesis testing is relevant here (as opposed to estimation).  I suggest computing simultaneous 0.95 compatibility (aka confidence) intervals for all contrasts of interest.  Simultaneous confidence intervals are a more statistically unique way to control for multiplicity (as opposed to the arbitrary choice of p-value adjustments).
Comment: Except one of the hypotheses involved multiple parameters. I agree, Frank, but I was just answering the question that was asked. If you want to get CIs for all the parameters, there will be four inferences rather than two, and the correlation issue will be more substantial. There are examples in my various books showing how to utilize such correlations to get the right intervals for these four parameters.
Comment: There is a general approach to this that doesn't require explicit estimation of those correlations.  For linear models and approximately for GLMs in general the R `rms` package `contrast` function with `type='simultaneous'` makes this painless.
Comment: No need to estimate the correlations as they are known exactly here (or more precisely, the dependence structure is known). And if you don't use that information, the inferences will not achieve the nominal FWER for the simultaneous intervals.


how to remove single quote from the value of hidden field in javascript

Question: I am reading hidden field value using javascript. The value I am getting is within single quote( '78963' ). I can i remove this single quote ? I want the value without single quote(  78963  ). Please help me to solve the problem.
Comment: Do you mean simply this: `yourValue.replace(/'/g, "");`? Or am I missing something? What are the possible values of the string?
Answer: I guess you simply want to convert your string value to numeric.
Just use parseInt():
parseInt("78963", 10);  // 78963

If the value pretends to be floating, there is parseFloat() method:
parseFloat("78963.1");  // 78963.1

And one more shortcut to make casting:
+"78963";  // 78963

In case if you simply want to replace single quotes, you may use:
"'78963'".replace(/'/g, "");  // "78963"

(as stated by others) or do tricky split:
"'78963'".split("'")[1];  // "78963"

Comment: This doesn't seem correct given something like ``, which is how I interpret the question.
Comment: @PeterHerdenborg Well, it can be. Updated my answer.
Comment: Great. Your original interpretation may well have been correct. The question is a bit vague.
Answer: You can use .replace() function on strings: http://www.w3schools.com/jsref/jsref_replace.asp
Answer: str.substr(1, str.length - 2);

Answer: Just $('#element').val(), you will get exact value. here element will be the id of hidden box.
Answer: If it's true that the value of the hidden input field contains single quotes, the (in my opinion) best answer is the one which JamesAllardice put in a comment:
yourValue.replace(/'/g, "");


100 coins made up of pennies, dimes and quarters worth $5.00

Question: I'm currently working through the book 'Elementary Number Theory' by Underwood Dudley, and I came across an example of seeing whether or not we can have 100 cents, dimes and quarters be worth exactly $5.00.
Before I begin, I know the above task is impossible, but I want to know the logic behind the steps we took to arrive at that conclusion.
In the example we derived 2 equations,
$c+d+q = 100$ and $c+10d+25q=500$
Which was relatively straightforward. But what I have trouble with is the following:
When we subtract the 1st equation from the 2nd, we get $9d+24q=400$, which I know is to cancel out the c, but why?
The 2 values are of different types, the first equation deals with the number of coins, the second deals with the number of cents, if we subtract the 2 equations, then aren't we subtracting coins from cents, which shouldn't be possible?
Then the example goes to show that it's not possible to have exactly 5 dollars with 100 coins comprised pennies, dimes, etc.. by applying a previously defined lemma on the equation $9d+24q=400$, which is out of the scope of this question, so what I'm after is:
Is there a way to visualize what we're doing to the equations in terms of the objects in this question? (coins, pennies, etc...)? I just want an intuitive understanding of what we're doing here.
I've tried to visualize the 2 equations as scales that we need to balance, but I can't quite fit my model into this question as the scales differ in units of measurement (coins vs pennies).
Comment: when you turn them into equations sometimes they lose their literal meaning
Answer: One point of view is, once you have abstracted the "real-world" quantities (number of coins, number of cents) into equations with named variables, you can go at it with algebra and never mind what the next equation "means" in "reality."
You may have to take this view some day for some problem.
For this problem, in the equation $9d+24q=400,$ you know that every coin contributes at least one cent to the total value; the $9$ represents the number of extra cents that each dime contributes, and the $24$ represents the number of extra cents that each quarter contributes.
If the $100$ coins were all pennies we would be $400$ cents short of five dollars, so the $d$ dimes and $q$ quarters need to contribute a total of $400$ "extra" cents:
$$9d+24q=400.$$
Comment: If we say that the 100 coins were all pennies, then does the symbols d and q lose meaning in the equation c + d + q = 100? Because originally the equation represented the number of coins, not the worth of the coins in terms of cents. And also why do have to say if the 100 coins were all pennies, what happens in the case they're not? Do we reach a dead end then?
Comment: Ok so after thinking about your answer I think I got somewhere. If we treat c + d + q = 100 to be all cents, then we would have 100 coins, but if we added a value of 9 "extra" cents to each dime(d) and 24 "extra" cents to each quarter in c + d + q = 100, then we'd have c + 10d + 25q = 500, or 5 dollars. So now we STILL have 100 coins, but due to the extra value placed on dimes and quarters the 100 coins are now worth 500 cents. Am I on the right track? Sorry if this comment was too long.
Comment: Your second comment makes sense to me. You might even be explaining it better than I did.
Answer: The variables $c,d$ and $q$ are unitless. Their domain is $\{0,1,2, \dots\}$

The number of pennies is $c$ coin.
The number of dimes is $d$ coin.
The number of quarters is $q$ coin.

$$c \ \text{coin} + d \ \text{coin} + q \ \text{coin} = 100 \ \text{coin}
  \implies c + d + q = 100$$

The value of $c$ pennies is
$c \ \text{coin} \times \dfrac{1 \ \text{cent}}{\text{coin}}
    = c \ \text{cent}$
The value of $d \ $ dimes is
$d \ \text{coin} \times \dfrac{10 \ \text{cent}}{\text{coin}}
    = 10d \ \text{cent}$
The value of $q \ $ quarters is
$q \ \text{coin} \times \dfrac{25 \ \text{cent}}{\text{coin}}
    = 25q \ \text{cent}$

$$c \ \text{cent} + 10d \ \text{cent} + 25q \ \text{cent} = 500 \ \text{cent}
  \implies c + 10d + 25q = 500$$
Answer: The logic is this:

$c+d+q=100$ represents number of coins
$c+10d+25q=500$ represents number of coins, weighted by number of cents each is worth (not just number of cents).

Therefore they are consistent and can be subtracted.
Comment: sidenote using nickels, dimes, and quarters, there's 29 ways to give back a dollar.
Answer: First, we display the equations as a matrix:
$$ \left[
\begin{array}{ccc|c}
  1&1&1&100\\
  1&10&25&500\\
  0&0&0&0
\end{array}
\right] $$
After some work, we arrive at the following canonical matrix:
$$ \left[
\begin{array}{ccc|c}
  1&1&1&100\\
  0&1&\frac{24}{9}&\frac{400}{9}\\
  0&0&0&0
\end{array}
\right] $$
The number of variables, n, is equal to 3, and note that $rank(A)=rank(A|B)$ - so on paper, this set of equations is supposed to have an infinite amount of solutions, which is given by:
$$
    \begin{pmatrix}
    \frac{10}{9} \\
    -\frac{24}{9} \\
    1 \\
    \end{pmatrix}t+
\begin{pmatrix}
    \frac{500}{9} \\
    \frac{400}{9} \\
    0 \\
    \end{pmatrix}
$$
Now, this is true for any $t$, but from the question, $t$ has to be a natural number!
Next, from the second row of the solution, we get that $-24t+400\geqslant0$ (the number of dimes can't be negative), and after a simplification: $t\leqslant 16\frac{2}{3}$, which means that $0\leqslant t\leqslant16$.
Now, a quick check for all values of t which are natural numbers, we find no possible value which gives us a natural number (which is supposed to be d, the number of dimes).
Thus we finally conclude that there is no solution to this question.
Answer: One way to visualize things is to imagine replacing each dime with a stack of ten pennies and each quarter with a stack of twenty-five pennies.  When you subtract the equation $c+d+q=100$ from $c+10d+25q=500$ to get $9d+24q=400$, what you are doing in effect is to remove one penny from each stack, where $q$ of the stacks started with $25$ pennies, $d$ started with $10$ pennies, and $c$ of the stacks consisted of a single penny.
(Note, the impossibility resides in the fact that $9d+24q$ is divisible by $3$ whereas $400$ is not.)


(Mongoose) How to search Users collection for a token that is inside an object inside the sessions array property

Question: So here is my User.model.js file:
const mongoose = require('mongoose');
const jwt = require('jsonwebtoken');
const crypto = require('crypto');

const UserSchema = new mongoose.Schema({
    username: {
        required: true,
        type: String,
        minlength: 4,
        maxlength: 128,
        trim: true
    },
    displayname: {
        required: true,
        type: String,
        minlength: 4,
        maxlength: 128,
        trim: true
    },
    email: {
        required: true,
        type: String,
        minlength: 8,
        maxlength: 256,
    },
    password: {
        required: true,
        type: String,
        minlength: 8
    },
    sessions: [
        {
            token: {
                type: String,
                required: true
            },
            expiresAt: {
                type: Number,
                required: true
            }
        }
    ]
});

// Some irrelevant functions

const User = mongoose.model('User', UserSchema);

module.exports = User;

So what i would like to do is find a User by using only the session token but it's proving a little difficult because there can be multiple objects in the session array for each User, all of which i want to search through.
Answer: You can query like this
const user = await User.findOne({"sessions.token": session_token})

or
const user = await User.findOne({sessions: {$elemMatch: { token: session_token }}})

For more info, you can refer documentation.
https://docs.mongodb.com/manual/tutorial/query-array-of-documents/


PROLOG CLPFD How to express this via constraints?

Question: Given the following sample of code:
example(Ls) :-
    Ls = [X,Y],
    Ls ins 1..2,
    Cost #= max((X #= 1)*3 + (Y #= 1)*5,
                (X #= 2)*3 + (Y #= 2)*5),
    labeling([minimize(Cost)], Ls).

The idea is to find the assignment to variables of Ls that minimizes Cost (in this simple example, it would be either X=1 and Y=2, or X=2 and Y=1).
I am trying to use the fact that the constraint #=/2 is reifiable, which means reflecting their truth values into Boolean values represented by the integers 0 and 1. (taken from the manual http://www.swi-prolog.org/man/clpfd.html).
However, it doesn't work. I get the following error:
ERROR: Domain error: `clpfd_expression' expected, found `_G3154#=1'

What would be an equivalent, correct version?
Answer: Reification involves separate constraints (#<==>/2, #==>/2 etc.), you can use them for example like:
example(Ls) :-
    Ls = [X,Y],
    Ls ins 1..2,
    X #= 1 #<==> B1,
    Y #= 1 #<==> B2,
    X #= 2 #<==> B3,
    Y #= 2 #<==> B4,
    Cost #= max(B1*3 + B2*5, B3*3 + B4*5),
    labeling([min(Cost)], Ls).

Sample query and its result:
?- example(Ls).
Ls = [1, 2] ;
Ls = [2, 1] ;
Ls = [1, 1] ;
Ls = [2, 2] ;
false.

Comment: I have used as many as 10,000 such intermediate variables (for example, for animating the search process when solving the 100-queens problem on a 100x100 chessboard) without any problems. So, I suggest you first try whether it works, and try to find more efficient formulations only if you really need them. Of course, in a larger example, you do not manually introduce these intermediate variables, but set them up for example via an auxiliary predicate that relates them to the list of variables and values via some input data you specify or compute.
Comment: So the usual ways to make it faster apply: First, I would try a different labeling strategy, like `ff`: `labeling([min(Cost),ff], Ls)`. I would then experiment also with other options, like `down`, `bisect` etc. Please keep in mind that you are already trying to solve a problem over a search space of potential size 10^7 = 10,000,000, and whether or not you have 100 or 1000 additional auxiliary variables will not affect this. You may, however, be able to find a stronger formulation or stronger constraints so that the search space can be further reduced, which will typically help a lot more.
Comment: Is there any way to the same without using the intermediate variables B1..B4? In the real program, there would be over 200 of them.
Comment: Using the method in your example, I got it to work. However, with over 10 variables in Ls and 7 posible values for each element, it takes over a minute to finish.
Comment: I meant 7^10 = 282,475,249.
Answer: As an alternative to using reification you could also use additional arithmetic constraints for "capturing" equality in an expression:
example([X,Y]) :-
    X in 1..2,
    Y in 1..2,
    Cost #= max(3*(1-min(1,abs(X-1))) + 5*(1-min(1,abs(Y-1))),
                3*(1-min(1,abs(X-2))) + 5*(1-min(1,abs(Y-2)))),
    labeling([min(Cost)], [X,Y]).

Note that the expression inside Cost #= max(...) can be slightly simplified.
Sample use:
?- example(Ls).
  Ls = [1,2]
; Ls = [2,1]
; Ls = [1,1]
; Ls = [2,2]
; false.


does window.open fail to open up a popup window sometimes?

Question: There's an update process for something.
And I need to call a service from a third party, so that their database will be updated as well.
My Ajax works fine.
Here's the inner code snippet of success call back.
<code>.
$.ajax({
.
.
     success : funtion(result) {
          .
          .
          .

          var disposalWindow = window.open('http://10.10.10.10:8070/exp_call?YMD='+ymd+'&HMS='+hms+'&CID='+cid+'&SEND_DATE='+senddate+'&ACODE='+inlineNumber+'&CALL_TYPE='+calltype, '');
          disposalWindow.close();
     },
.
.
.
</code>
This has worked perfectly so far. But the update process for third party was failed for the first time today.
I asked the provider to check out the log and see if the page was called or not and it processed right or not.
And the answer was "you called nothing at the time, log told me."
Could this happen?
It's been 3 month since the project was finished.
Using Ajax for calling the page isn't acceptable because of CORS.
But I thought using window.open would be no problem, which turns out to be problem, seemingly.
How can I deal with it, in this situation?
Is it an inappropriate way to use window.open for this purpose?
Comment: That said, browser usually block window.open calls unless it is being called in a click event handler. A safer way to solve your problem would be adding an iframe or an img tag to the body and removing it after load.
Comment: Popup blockers basically come with web browsers these days
Comment: and if your `ajax` doesn't succeed the `success` function won't fire. so it's much more likely that your `ajax` function did not succeed than that `window.open` has failed to work randomly.
Comment: Yes but, the users are supposed to allow popup in their browser. The client decided to do that way.
Comment: with this typo it hasn't been called ever: `success : funtion(result) {` -> `function` ...
Comment: @northkildonan Ajax was processed correctly. Our server was updated by the result. That proves the ajax was successful.
Comment: No it doesn't. A HTTP 200 response proves that the request was successful. Your server code might be failing after you update your database or log.
Comment: @UmurKontacı There's no server side process for updating the third party data. Client side, I mean scripts on browser, takes the job entirely. By the way, iFrame !! I've never thought about it. Thanks, I'm going to give it a shot.
Answer: I've test a lot to find out what's the exact cause of this random work of url-call via window.open.
It looks like the network condition affects the result.
I hooked off the LAN cable when window.open was underway and the same error was reproduced.
Otherwise it works fine.

UPDATED.
Here's my code to solve this problem, inspired by Umur Kontacı's comment.
<code>$.ajax({
.
.
     success : funtion(result) {
          .
          .
          .

          $("<iframe id='disposalIframe' src='http://10.10.10.10:8070/exp_callback?YMD=20141110&HMS=114113&CID=0517277176+&SEND_DATE=20141110115023&ACODE=1161&CALL_TYPE=4' width='0' height='0'></iframe>").appendTo(document.body);

          $('#disposalIframe').load(function(e) {
                $('#disposalIframe').remove();
          });

          //var disposalWindow = window.open('http://10.10.10.10:8070/exp_call?YMD='+ymd+'&HMS='+hms+'&CID='+cid+'&SEND_DATE='+senddate+'&ACODE='+inlineNumber+'&CALL_TYPE='+calltype, '');
          //disposalWindow.close();
     },
.
.
.
</code>
I see it waits until the network becomes available again when I hooked off and on the cable.
In case of disconnection of network, however, it doesn't guarantee the successful call of the service.
But It makes it latency-free. I tested it.
Comment: Dear a downvoter, give me some reasons for that please ?
Comment: I also encounter `window.open` sometimes not open a tab. And I tried what you said "disconnect the network" and there is right no tab is opened in this case. However, if it is caused by network, then whatever we did, the corresponding solution still not work because our network down, right?


Why does these 2 similar for loop have different results when adding ActionListener into JButton?

Question: Originally I have a JFrame have 3 buttons: which button 0 prints 0 when clicked, button 1 prints 1 and so on:
JFrame jframe=new JFrame();
jframe.getContentPane().setLayout(new GridLayout(3,1));
for(int i=0;i<3;i++){
    final int j=i;
    JButton jbutton=new JButton(""+j);
    jframe.getContentPane().add(jbutton);
    jbutton.setPreferredSize(new Dimension(100,100));
    jbutton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            System.out.println(""+j);
        }
    });
}
jframe.setVisible(true);
jframe.pack();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

I want to reduce 1 line due to the final variable, and hence refactor the for-loop as this:
JFrame jframe=new JFrame();
jframe.getContentPane().setLayout(new GridLayout(3,1));
for(final int[] arr={0};arr[0]<3;arr[0]++){
    JButton jbutton=new JButton(""+arr[0]);
    jframe.getContentPane().add(jbutton);
    jbutton.setPreferredSize(new Dimension(100,100));
    jbutton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            System.out.println(""+arr[0]);
        }
    });
}
jframe.setVisible(true);
jframe.pack();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

but this time all buttons print 3 even the button index is still 0,1,2, what is the reason?
Comment: why are you using an array `for(final int[] arr={0};arr[0]<3;arr[0]++){` ?
Comment: `arr[0]++` ... what is the value at `arr[0]` when the loop completes?
Answer: After the loop ends, i[0] is 3. The ActionListeners all hold a reference to i and will read the same value for i[0] which is 3.
jbutton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        System.out.println(""+arr[0]);
    }
});

If you want to correct this, assign the value of i[0] to a local final variable and us that for the output like:
for(int[] arr = {0}; arr[0] < 3; arr[0]++) {
    final int val = arr[0];
    ...
    jbutton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            System.out.println("" + val);
        }
    });
}

Answer: In the second program, you are passing the same variable to all three buttons. And then you increment the value of the variable with each iteration. So the value gets updated for all three buttons.
Let me give you a simple example,
class Resource{
    int a = 10;
}
class A{
    Resource r;
}
class B{
    public static void main(String args[]){
        Resource r = new Resource();

        A o1 = new A();
        o1.r = r;

        A o2 = new A();
        o2.r = r;

        o1.r.a = 20;

        System.out.println(o1.r.a);  // prints 20
        System.out.println(o2.r.a);  // prints 20 too
    }
}

Answer: The proper way is to get the event source, and then you do not need final int
    public static void main(String [] args)
    {
        JFrame jframe=new JFrame();
        jframe.getContentPane().setLayout(new GridLayout(3,1));
        for(int i=0;i<3;i++){
            JButton jbutton=new JButton(""+ i);
            jframe.getContentPane().add(jbutton);
            jbutton.setPreferredSize(new Dimension(100,100));
            jbutton.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    JButton myButton = (JButton)e.getSource();
                    System.out.println(""+ myButton.getText());
                }
            });
        }
        jframe.setVisible(true);
        jframe.pack();
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


How can JavaScript arrays have non-numeric keys?

Question: What I have learned is an array is a type of object. Objects are a collection of properties with key/value pairs. I always thought arrays are a collection of items that are numerically indexed starting at 0. Just recently, I was able to add a non-numeric key to an array.
let arr = ['cribriform plate','mastoid','hyoid'];
arr.eyes = 'brown';
arr.skin = 'white';

That resulted in
['cribriform plate','mastoid','hyoid',eyes : 'brown', skin : 'white'];

The for...in loop of arr yielded:
for(let i in arr){
    console.log(i);
    //0,1,2,3,eyes,skin
}

The for...of loop yielded:
for(let i of arr){
     console.log(i);
     //0,1,2,3
}

I was able to iterate over all the keys of an array using the for...in loop. However, when I used the for...of loop, I was only able to iterate over the numerically indexed keys. Why is that?
And, what is the most accurate definition of an array?
Comment: Hi, @hayden, will you add to array additionally?
Answer: With a for..of loop, the object's Symbol.iterator property is called. In the case of an array, this is equivalent to the array's .values() method, which contains the values for each index in the array. Non-numeric properties are not included - arrays generally don't have arbitrary non-numeric properties, and code that does assign arbitrary non-numeric properties to an array is likely in need of refactoring.
A for..in loop iterates over all enumerable properties on an object, including those inherited from the prototype. Thus, for..of on an array will exclude non-numeric properties on an array that a for..in loop will include.
Arrays, being objects, can have arbitrary properties assigned to them, for the most part, just like properties can be assigned to ordinary functions - it's just not a very good idea.
Comment: You said the for...in loop iterates over all properties of an object, included inherited ones, but aren't most methods and properties that are inherited non-enumerable? And then, does that make the numerical-indexed keys properties?
Comment: For arrays, yes. It has many methods inherited from its prototype (like `.forEach`, `.join`) and it also inherits from `Object`, which gives methods like `.hasOwnProperty`, `.valueOf`. But all of these are non-enumerable. On a normal array, it has no enumerable properties by default - the only enumerable properties it will have are array indicies, or arbitrary properties that happen to get assigned to (not a good idea).
Answer: Arrays are a type of objects in javascript. When you do something like arr.skin = 'white';, you're basically setting a variable to the array's object property collection. That's why you can access it with in for...of, which iterates over the enumerable properties of the object.
However, since this new property is not part of the array's list of elements, it cannot be accessed through for...in
Taken from MDN web docs for Arrays:

Setting or accessing via non-integers using bracket notation (or dot notation) will not set or retrieve an element from the array list itself, but will set or access a variable associated with that array's object property collection. The array's object properties and list of array elements are separate, and the array's traversal and mutation operations cannot be applied to these named properties.


When are these inequalities consistent?

Question: Let $x_i,y_i \in \mathbb{R}$. Consider the following set of inequalities.
\begin{align}
x_1-x_2 &\leqslant y_1 \\
x_2-x_3 &\leqslant y_2 \\
&\vdots \\
x_{n-1}-x_n &\leqslant y_{n-1} \\
x_n-x_1 &\leqslant y_n \\
\end{align}
If they are consistent, necessarily $\sum_{i=1}^n y_i \geqslant 0$.
Is that also a sufficient condition of feasibility?
Answer: Hint: consider $x_1=y_1+y_2+\ldots+y_n$, $x_2= y_2+\ldots+y_n$, $x_3= y_3+\ldots+y_n$, ... , $x_n = y_n$.

When are these inequalities consistent?


CKEDITOR is not defined?

Question: I've installed Wysiwyg with CKEditor and set up a profile for the editor. However, the CKEditor toolbar isn't showing up. When I add new content Firebug shows the following script error:
CKEDITOR is not defined

It highlights line 144 of /sites/all/modules/wysiwyg/wysiwyg.js:
CKEDITOR.config.customConfig = '';

I'm really not sure how to solve this problem. Do I need to manually define CKEDITOR.config.customConfig? If so, what should its value be?
I should add that this is a fresh Drupal install (version 7.27) and that I'm using Wysiwyg 7.x-2.2-dev with CKEditor 4.4.0 (standard version). The reason for using the development version of Wysiwyg is that the stable version is unable to detect CKEditor.
Comment: And no mention about installing javascript library in your site's status report? Care to provide screenshots of your config? (remember to take them at browser windows's width about 600px, for best readability here).
Comment: The status report doesn't show any errors (nothing in red) but does say that 'Upload progress' is not enabled (and recommends installing the PECL uploadprogress library or APC). Not sure what screenshots you'd like to see - do you want screenshots of the 'Wysiwyg profiles' menu?
Comment: http://i.stack.imgur.com/yTEoX.png - yours looks similar? Version printed makes sense etc?
Comment: Yes, I following the installation instructions carefully. Under Wysiwyg profiles the CKEditor is showing (CKEditor 18.104.22.168daef5) for 'Filtered HTML' and I've selected a bunch of buttons under 'CKEditor profile for Filtered HTML'. And, when I add a new page I've made sure the text format is set to 'Filtered HTML'. I've also made sure to clear the cache (both in Drupal and in my browser).
Comment: Try latest 3.x, I had a serious problems with 4.x line in Drupal./
Comment: I've deleted the old profile; deleted the ckeditor directory from /sites/all/libraries and added two alternative editors: CKEditor 18.104.22.168 and TinyMCE 3.5.11. I've cleared the cache and have set it up so that CKEditor is used for 'Filtered HTML' and TinyMCE for 'Full HTML' (and I've selected buttons for both of them). When I add content using either profile I get exactly the same error: 'CKEDITOR is not defined'. I'll next try the stable version of Wysiwyg with TinyMCE 3.5.11 ;(
Comment: The problem is: this *should* work. CKEditor 188.8.131.52 is exactly what I use and I use it with no problems at all. Good luck, but for now I'm out of ideas.
Comment: I'm now using the latest stable version of Wysiwyg (7.x-2.2). TinyMCE 3.5.11 is working; CKEditor 22.214.171.124 still triggers the same error. At least that got me somewhere - thanks for your help.
Comment: I would suggest just using the CKeditor module instead of wysiwyg + ckeditor.
Answer: Yeah, I can confirm Molots statement.
Try to use CKEditor 3.5.3 with the newest Wysiwyg.
Even with patches I could not solve the problems between CKE 4 and wysiwyg.
regards,
SuperEngineer 64
EDIT: Also possible is to install the Develpoment-Version of Wysiwyg. I use it mit CKE4 now.
Comment: Many thanks, I've just tried Wysiwyg 7.x-2.2 with CKEditor 3.5.3 and it's all working. I'm surprised the Wysiwyg module needs an editor that's over three years old but glad I now got something functional.

CKEDITOR is not defined?


is there any way to solve" project=Project.objects.get(pk=pk) "

Question: why is it that when I visit http://127.0.0.1:8000/project/1/ an error occur it says DoesNotExist and
when I try http://127.0.0.1:8000/project/2/ it works without any error.i think the problem is either in urls.py or views.py
my url code :
`
urlpatterns =[
url(r'^$', views.firstindex, name='firstindex'),
url(r'^(?P<pk>[0-9]+)/',views.project_detail, name='project_detail'),]

and this is the view.py
`def project_detail(request,pk):
project=Project.objects.get(pk=pk)
context={
    'project':project
}
return render(request,'project/detail.html')

.
Comment: It means there is no `Project` with `id=1`, that's it.
Comment: If you're using sqlite as your database I'd suggest getting https://sqlitebrowser.org so you can inspect your data and verify whether the project exists or not.
Answer:
i think the problem is either in urls.py or views.py

There is no logical error. A Model.objects.get(..) will raise an error (1) it can not find any record for the given filter; or (2) it can find multiple records for the given error. DoesNotExist thus means it did not find a Project with that primary key. If you look into the database, you will likely find out there is no Project with id=1.
You can use the get_object_or_404(..) function [Django-doc] to return a HTTP 404 in the case it can not find such project, like:
from django.shortcuts import get_object_or_404

def project_detail(request, pk):
    project = get_object_or_404(Project, pk=pk)
    context = {
        'project': project
    }
    return render(request, 'project/detail.html', context)
Note that you forgot to pass your context to the render call.
Comment: Simple and straightforward answer. I like it.
Comment: yeah, thank you very much, it's working now. I like it.


Combination of same delimiters in awk

Question: I have a String
1__2_3__4_5_6

I want to set '__'(2 underscore) as delimiter in AWK.
$1 should be 1
$2 should be 2_3
$3 should be 4_5_6

Answer: Just set __ as FS value. You could also pass a regex as  FS value.
$ echo '1__2_3__4_5_6' | awk -v FS="__" '{print $1}'
1
$ echo '1__2_3__4_5_6' | awk -v FS="__" '{print $2}'
2_3
$ echo '1__2_3__4_5_6' | awk -v FS="__" '{print $3}'
4_5_6
$ echo '1__2_3__4_5_6' | awk -v FS="_{2}" '{print $3}'
4_5_6

_{2} matches exactly two underscores.
Comment: Correct answer but I can't help wondering why the OP didn't know that you can set FS. It's step 1 in learning awk. @Mahesh - that error message means you are using old, broken awk (/usr/bin/awk on Solaris). Do not use that awk. On Solaris use /usr/xpg4/bin/awk (or nawk, but it's not as close to POSIX).
Comment: That's not a bad instinct (except I'd prefer single quotes unless I wanted to use shell variables), but you could still use `-F '__'` or `-F "__"`.
Comment: Why `-v FS="__"` instead of `-F __`?
Comment: hi Avinash,
I am getting below error:
$ echo '1__2_3__4_5_6' | awk -v FS="__" '{print $1}'
awk: syntax error near line 1
awk: bailing out near line 1
Comment: try `awk 'BEGIN{FS="__"}{print $3}' file`
Comment: No syntax erorr,but i am not getting expected result
$ echo '1__2_3__4_5_6' | awk 'BEGIN{FS="__"} {print $2}'

$
Comment: Darn - didn't even notice that. May as well mention that RE intervals `{2}` will only work in some versions of some awks too.
Comment: @Wintermute i think it's better to put the FS value(multiple chars) inside double quotes (ie, using -v param) because shell will treat some chars differently.

Combination of same delimiters in awk


Permissions required for an SQL Server account

Question: We're trying to get SQL running under a domain account, however we're not sure what permissions will be required for the service to start.
At present it is just a standard user in the active directory domain (which won't let the service start), I assume this user account will need permissions to the Program Files\Microsoft SQL Server directory.
But does it need permissions to access anything else?
Thank you.
Myles.
Answer: See http://support.microsoft.com/kb/283811
JR
Comment: as a side note, don't forget to give the user account the "manage volume" permission to allow instant file initalization
Answer: For SQL 2005 and 2008 the KB article quoted by JR is obsolete and quite incomplete. Since SQL 2005 the setup creates local user groups and grants all needed privileges to run each service to the respective local group. For example the SQL Server service account needs to be added to the SQLServer2005MSSQLUser$ComputerName$MSSQLSERVER user group (default instance) or SQLServer2005MSSQLUser$ComputerName$InstanceName user group (named instance). The user groups have all the needed priviledges (SeServiceLogonRight, SeBatchLogonRight, SeAssignPrimaryTokenPrivilege etc etc) and all the needed access rights on the registry keys and files and folders used by the respective service. The ones 'missing' are the SeManageVolumePrivilege as Nick already pointed out and the SeLockMemoryPrivilege ("lock pages in memory"): these need be granted explicitly as are not considered mandatory.
So in SQL 2005/2008 case the solution is not to grant access to the folders and hives quoted in the KB, but instead add the desired domain user to the local user group SQLServer2005MSSQLUser$ComputerName$MSSQLSERVER (or the named instance equivalent).
You can read all the details at Setting Up Windows Service Accounts in BOL.
Comment: btw not knowing if the OP is on SQL 2000 or not, this advice of course may or may not apply. I just wanted to make sure somebody stumbling on this is aware that since 2005 there is a better way to deal with this issue. In fact in 2K5 there is even an UI wizard to accomplish the task: http://msdn.microsoft.com/en-us/library/ms345578(SQL.90).aspx
Comment: Worked great in SQL Server 2008. Fixed my issue


How do you position a custom search box in the main menu?

Question: I'm using the Drupal 7 Mayo theme for my site. The theme comes with a search box that you can place in the header but no where else, so I made a custom search box block, which I would like to place to the right of the main menu bar, which has a horizontal list of dropdown menu links. When I try to move the search block to the main menu, however, it either pushes the links to a line below or appears on a line below the links depending on how I set the weight. I can't get them to match up. Is there a way to fix this?
Answer: Menu Inject Block module was built exactly for the same purpose.

This module allows you to inject blocks as menu items. It facilitates easier use of JavaScript based dropdowns and panel like navigation. Most users employ this module to create dropdown panels using other modules to handle the dropdowns and this module to inject the blocks as needed.

To use this module:

Enable the module
Create or Edit your menu item (/admin/build/menu)
Enter in  for the Path
Give your menu item a Title
Select a Block from the "BLOCK TO INJECT:" drop down (in your case the search block).
Save your menu item

You don't need to create a separate search block in case you use the module. You can place the default search block that Drupal provides, into step 5 mentioned above.


How could I prove that the cardinality of the union of two sets is equal to R? $|T U S| = |T| = |\mathbb{R}|$

Question: I have to prove that $|T \cup S|$ where $T$ is infinite and $S$ is countable, equal to $|T|$, and this is also $|\mathbb{R}|$.
How can I approach this?
$|T \cup  S| = |T| = |\mathbb{R}|$
I tried to approach it by considering the fact that $|S|$ is $N_{0}$. But after that I couldn't advance much.
Comment: It's true that $|T\cup S|=|T|$ if $T$ is infinite and $S$ is countable, but it's not necessarily true that $|T|=|\Bbb R|$.
Answer: You can't prove what you would like to prove. But let's see what we can say given the information.
We will assume that we have $AC$ so that sizes of sets are well behaved. Without $AC$ this would get a lot trickier.
We can then see that $|A\cup B|$ for any two set $A,B$ such that at least one is infinite is just $\max\{|A|,|B|\}$. This can be shown using an easy construction where for every odd ordinal we take an element of $A$ and for every even ordinal we take an element of $B$. If we run out of elements in one of the sets then we can just choose one we allready chose and that gives us a function that's not one-to-one but onto. That's enough since we get an upper bound on the size and the lower bound will be given by the size of the bigger inifinite set. The two will then coincide.
Once we know that the size of a union of two sets at least one of which is infinite is just max it's not hard to show that in your case $|S\cup T|=|T|$. Since both of the sets are inifinite and $|S|=\aleph_0=|\mathbb{N}|$ which is the smallest possible infinite set then the size of the union must be at least as large as $|T|$ since $|T|\geq \aleph_0$. That last claim is equivalent under AC to claiming that $T$ is inifinite.
So now to explain why you can't get the result you want. Well for one $|T|$ could just be $\aleph_0$. I.e. $T$ could just be a countable set itself in which case $|S\cup T|=\aleph_0$. You could also have $T=\mathcal{P}(\mathbb{R})$ in which case $|T|>|\mathbb{R}|$. Last but not least if you are working in a model where $\neg CH$ holds you might have a bunch of sizes between $\aleph_0$ and $|\mathbb{R}|=\mathfrak{c}$ and $|T|$ could be any one of those.
Comment: "AC" what is "AC"? :D
Comment: @JOX To be honest I don't know how much choice you need for the conclusions I wrote up. The way I wrote it up probably a decent amount since you want a choice function on both $A$ and $B$. But it might be that if you're careful you can avoid some of it. Having Axiom of Choice makes all these arguments a lot easier though since you can just assume that each of your sets has some proper cardinality.


Log-Linear Output Help

Question: I ran a loglinear model with 3 variables. Internet Use (Y/N), Nervous Breakdown(Y/N), and Happiness (3 levels). I understand by two way interactions are significant, but I am getting lost trying to understand the coefficents table. Thank you so much for any help!
Answer: The coefficients or parameter estimates table is giving you the estimated model coefficients for the saturated model (with all possible interactions among your three variables). The loglinear model is very similar to a factorial analysis of variance (ANOVA) model, except that the response that is modeled in a loglinear model is the natural logarithm of the cell count for each cell defined by the combinations of levels of your three variables.
The general equation for a three-way loglinear model is:
$$ln(m_{ijk})=\mu+\lambda^{i}+\lambda^{j}+\lambda^{k}+\lambda^{ij}+\lambda^{ik}+\lambda^{jk}+\lambda^{ijk}$$
where $m_{ijk}$ is the cell count in the cell at the $i^{th}$ level of the first factor, the $j^{th}$ level of the second factor, and the $k^{th}$ level of the third factor, $\mu$ is a grand mean or intercept term, $\lambda^i$ is the main effect of the first factor at level $i$, $\lambda^j$ is the main effect of the second factor at level $j$, $\lambda^k$ is the main effect of the third factor at level $k$, $\lambda^{ij}$ is the two-way interaction effect of the first two factors at the $ij^{th}$ combination, $\lambda^{ik}$ is the two-way interaction effect of the first and third factors at the $ik^{th}$ combination, $\lambda^{jk}$ is the two-way interaction effect of the second and third factors at the $jk^{th}$ combination, and $\lambda^{ijk}$ is the three-way interaction effect at the $ijk^{th}$ combination.
In order to know the specifics of how to interpret your parameter estimates, you have to know how the model has been parameterized. The solution presented for saturated models in the HILOGLINEAR or Model Selection Loglinear procedure in SPSS treats the intercept $\mu$ as the average of the logarithms of the cell counts, and the $\lambda$ coefficients indicate deviations from this grand mean.
(Note: By default a $\delta$ of .5 has been added to each cell before taking the logs to prevent problems in models with 0 cell counts. The $\delta$ value can be specified by the user, including 0 if appropriate.)
The procedure unfortunately does not present the intercept or grand mean estimate $\hat{\mu}$ in the output. You have to calculate this manually if you want to evaluate the equation using the parameter estimates. To do this, take the observed cell counts from the Cell Counts and Residuals table for the saturated model, take the natural logarithm of each one, and calculate the mean of these logged values. That's your $\hat{\mu}$ value.
To calculate the predicted value for the log of the cell count for the first cell, where each of your three variables are at their first level, you would simply add the appropriate coefficients for all the terms for that cell:
$$ln(\hat{m}_{111})=\hat{\mu}+.506-.210-.411+.077+.123-.132+.123+.006=\hat{\mu}-.041$$
(I would generally suggest drilling into the values and using more than three decimals if you want to reproduce the cell counts to acceptable precision.)
This parameterization is often referred to in ANOVA models as sum-to-0 restrictions. The .506 value for the first level of INTUSE indicates an increase of .506 from the mean. The value for the second level of INTUSE wouuld be -.506. For HAPPY you have -.210 and .622 for the first two levels. The value for the third level is then the negative of the sum of these two levels, or -.412. This summing to 0 of parameter estimates across levels of factors extends to rows and columns (and layers) of crosses of factors in the interaction effects.
Some good information on these models, with particular attention to SPSS, is available in the Advanced Statistical Procedures Companion written by Marija Norusis (see here). Even though the newest version of this book is many releases old, these concepts have not changed.
Since in your situation the best model would seem to be one that excludes the three-way interaction, if you want to be able to look at parameter estimates for that model, you'll have to use a different procedure, GENLOG, or Analyze>Loglinear>General in the menus. The HILOGLINEAR procedure uses IPF (iterative proportional fitting) for its estimation, which doesn't produce parameter estimates, only estimates of the cell counts under different models. Parameter estimates for saturated models can be calculated using closed-form expressions, but this is generally not the case for simpler loglinear models. GENLOG uses Newton-Raphson iterative estimation and is able to produce parameter estimates for any loglinear model appropriate to a given set of data.
GENLOG uses a different kind of parameterization, often referred to as set-to-0 restrictions, and it produces parameter estimates for each parameter in the model, including the constant or intercept for Poisson loglinear models, and the redundant parameters (for which all estimates are 0). It's easier to reproduce the modeled logs of cell counts using the parameter estimates from GENLOG, because you only have to add the appropriate values (no calculating negatives of sums of earlier ones) and the output labels them for you more explicitly than is done in the HILOGLINEAR output. In this parameterization, the constant or intercept term estimates the value for the last cell in the design instead of the average over cells, and other estimates show increases or decreases relative to that value.

Log-Linear Output Help


If I store the output of nested for loop only the last elements of the loop are printed, but print function prints all the combinations

Question: l1 = ['a','b']
l2 = ['c','d']
for x in l1:
   for y in l2:
       (a,b)=(x,y)

When I print(a,b), all I get is ('b','d'), but when I enter the code like
for x in l1:
   for y in l2:
       print((x,y))

all possible combinations of l1 and l2 are printed. Why is this happening and how do I go about this?
Comment: You printed `(a,b)` outside loops.
Comment: How many times does a and b change between your calls to print? Now how many times do x and y change between calls?
Answer: The following code will give you all possible combinations
l1 = ['a','b']
l2 = ['c','d']

for x in l1:
   for y in l2:
       (a, b) = (x, y)
       print(a, b)

Also, take a look at the alternative solution
from itertools import product

l1 = ['a','b']
l2 = ['c','d']

for (a, b) in product(l1, l2):
    print(a, b)

Comment: The question asks why is it happening, the op already has a working solution
Comment: @Sayse since his solution doesn't print all possible combinations it doesn't work, but my does. The answer to the question appears after comparing the non-working solution and the working one.
Answer: When you do (a,b)=(x,y) it sets the value of a equal to the value of x and b equal to the value of y. Everytime you run through the for loop a and b get over ridden. The print statement on the other hand just writes to stdout whatever the value of those two variables is at the time. If you want to store those values for use after the for loop I would recommend appending them to a list.
l1 = ['a','b']
l2 = ['c','d']

combinations = []
for x in l1:
   for y in l2:
       combinations.append((x, y))

Or as in Roman Dryndik's answer you could use the product function.
Answer: The simple answer is that when you do
l1 = ['a','b']
l2 = ['c','d']
for x in l1:
   for y in l2:
       (a,b)=(x,y)

I assume you are calling the print function outside the for loop (you didn't show where you called it in your pasted code). In your code, the values of a and b are reassigned during each iteration, so when you call it after the for loop it will just print what a and b were last assigned to.
And when you did:
l1 = ['a','b']
l2 = ['c','d']

for (a, b) in product(l1, l2):
    print(a, b)

You called the print function inside the for loop. This means it is reached during every iteration, so it makes sense that it will print the values of a and b during each iteration.


Passar texto de um textbox via parametro no onclick

Question: Preciso passar como parâmetro o conteúdo que esta em um textbox, no onclick de um botão. Como posso fazer isso?
Esse é o código Html:

        <input type="submit" value="Fechar X" id="FecharModal" onclick="fecharDialog()" />

        <input type="submit" value="Sim" id="ValidaSim" onclick="ValidaSim()" />
        <input type="submit" value="Não" id="ValidaNao" onclick="ValidaNao()" />

        <input type="text" id="txtValid" />
        <input type="text" id="txtDrop" />
        <input type="text" id="txtMatricula" />

No onclick do input ValidaSim preciso passar o texto que esta no textbox txtValid, como parametro. Ja no onclick do input ValidaNao preciso passar o texto de todos os textbox abaixo, como parametros.
<input type="text" id="txtValid" />
<input type="text" id="txtDrop" />
<input type="text" id="txtMatricula" />

Comment: Passar para onde?
Comment: Um aviso, não é uma boa pratica inserir a tag style dentro do elemento, as normas do css pedem para que ele esteja localizado em um aquivo .css
Answer: Segue uma forma bem simplista de fazer oque você deseja

var baseUri = 'http://urldoseusite.com/?'

function ValidaSim() {

  var uri = baseUri + "parametro1=" + $('#txtValid').val();
  alert(uri); // Remover esta linha depois quando implementar em seu projeto.
  window.location = uri;

}

function ValidaNao() {

  var uri = baseUri +
    "parametro1=" + $('#txtValid').val() +
    "&parametro2=" + $('#txtDrop').val() +
    "&parametro3=" + $('#txtMatricula').val();
  alert(uri); // Remover esta linha depois quando implementar em seu projeto.
  window.location = uri;

}

        <input type="submit" value="Fechar X" id="FecharModal" onclick="fecharDialog()" />

        <input type="submit" value="Sim" id="ValidaSim" onclick="ValidaSim()" />

        <input type="submit" value="Não" id="ValidaNao" onclick="ValidaNao()" />

        <input type="text" id="txtValid" value="valor da txt valid" />

        <input type="text" id="txtDrop" value="valor da txt drop" />

        <input type="text" id="txtMatricula" value="valor da txt matricula" />


Template not being picked up?

Question: I added a node-product.tpl.php file to my theme and it is completely ignored.
I reset the caches, multiple times, and tested with devel + devel_themer to see that the name was correct and it is. It says it searches for:

node-product.tpl.php
node-product.tpl.php
node.tpl.php

Why would the core theme() function not find that template?
My only answer would be that somehow the theme path is not yet available in the theme() function at the time the product is being generated.
Answer: (Assuming you are on Drupal 6, as it seems) you also need to copy the default node.tpl.php (e.g. the one from your base theme) in the same theme directory you are putting the override file (node-product.tpl.php) in order for the theme engine to pick it up:

If you are implementing a suggestion, rename the file to the
  suggestion name it should have. Note that some suggestion files will
  not be recognized unless their base file is also present -- for
  instance, if you want to make a node content type suggestion file for
  your content type, you need to have node.tpl.php present in your theme
  directory order for your node-my_content_type.tpl.php file to be
  recognized.  (Beginners guide to overriding themable output)
Comment: Great! 8-) I think I saw that before, but it's just a bad gotcha... Thank you for taking the time to answer.

Template not being picked up?


Can Major or Minor chord be inferred from sound electronically?

Question: I use a tuner app on iphone called "Cleartune" where you just strum a note and it tells you what note it is, based on frequency. It's useful for tuning.
An interesting thing I stumbled on is if I play a chord, it's pretty good at telling me what the chord is too. I think it's because a chord just harmonizes the root of the chord, but that root is the most prevalent. So that's the main frequency.
Now one weird thing is, If I play a A-major chord, or an A-minor chord, it still comes up as "A".
But I'm wondering if there's some kind of way for an app or some way to tell me what Chord I'm playing. So an app that would know the difference between a minor sounding to a major sounding. Or is that impossible to infer that electronically?
Answer: It is possible, but not with 100% accuracy. For example, Transcribe claims to be able to do this (at some level).
It works in broadly the same way as a tuner app. Each note has a frequency, and you can use some signal processing maths to turn a recording into a list of frequencies. Associate these frequencies with the notes, and you can electronically determine the chord.
If that sounds too easy, that's because it is. When you play a note, it produces not only the main frequency, but a collection of other ones (called harmonics). Each instrument does that differently, which is why a piano playing an A sounds different to a flute playing exactly the same note. That's often referred to as timbre.
So, yes, it's possible, but it's not perfect. It gets harder as you add more instruments to the recording, because you have to distinguish the actual notes from the harmonics that are produced. Add some non-pitched instruments, and it gets harder still. So your ears are still  much better than a piece of software.
Answer: Any physical sound that has a sense of pitch(a "frequency") is composed of a root/base/fundamental frequency that we feel as the foundational pitch.
Now, it is proven that these physical sounds all have a special mathematical relationship between the root and the other "frequencies" that are heard.
The relationship is very easy: it is always an integral multiple of the fundamental. This special series is called the overtone series.
e.g., suppose you "hear" a sound that you estimate to be 500hz(say you record and then slow down and count the oscillations). Then, that tone will have, generally, other frequencies with it but, if it be a musical pitch, they will be the frequencies 1000, 1500, 2000, 2500, 3000, etc...
"Real world" pitches generally are not perfectly mathematical and other non-musical sounds could be mixed in. Regardless, the mathematical theory is highly accurate for most musical sounds. The strength of these overtones give the character to the sound we hear. A volin playing C and a guitar playing C both have a fundamental pitch of C and all the overtones are in both sounds... only the relative strengths of those pitches are different.
Now a chord is multiple pitches together. Each pitch has its own "overtone series".
For a tuner to recognize chords, it must resolve each fundamental of the chord.  To do this, all frequencies must be found(through Fourier Analysis, say), then those frequencies must be tied to some overtone series.
This is not difficult to do mathematically but difficult in practice. The more notes or the more layers of instruments, the harder it is because the complexity goes exponentially.
So, yes, it could theoretically be done but no one has yet created a real device to do so. Some are working them. Some tuners now exist for guitars(polytuner, etc). At this point they generally are easily confused but anomalous inputs and definitely do not work in general.
The reason why your simple tuner does not recognize differences between major and minor is probably that it looks for the lowest note(that is easy to do) and just assumes that is the root. Since it excepts you not to play more than one note, and you do, you can't expect it to make sense. e.g., play a first inversion A chord and it should give you C, etc.
Since general music is much more complex you won't find any general analysis device or app(not yet, anyways).  E.g., Say you play A5, is it a major or minor chord? it depends on the music that came before and what comes after. if you play C A5 Dm then it is an implied Am and will sound minor.  If you play F#m A5 D then it is an implied A and will sound major. The tuners would have to listen to the music to figure this out. Currently only the human mind does this well and it involves training ones ear to know what it is hearing. Eventually someone will develop a device that is capable but I haven't seen anything that even comes close yet.
Comment: So, if that A or Am was played as a second inversion (with E at the bottom), would the tuner think it was still an A chord, due to the harmonics involved, or err more on the E side, since that would be the lowest note? Probably depends on the parameters of the individual tuner...
Comment: @tim no, again, most tuners only recgonize the lowest pitch and report that. That is, they report the bass note, not the root. In fact, they simply report the lowest note that they can detect. This is why sometimes they report the wrong note completely.  As I mentioned, and AFAIK, there is no true mathematically correct chord detection algorithm. It requires a lot of work. You have to extract all the potential frequencies, fit them to the overtone series, and then deal with ambiguities.
Comment: For example, suppose you have your A/E chord. Say is the notes E2 A2 C3. The overtone series is essentially E2: E3 B3 E4 G4 B4 D5 + A2: A3 E4 A4 C5 E5 G5 + C3: C4 G4 C5 E5 G5 Bb5. Notice that we have 3 E4's. So each note in the chord produces overtones that could be part of another overtone series. The software would have to figure out which overtones go with which note(actually how much of each). In the case of simple chords it is not that difficult but in the general case it is because the methods to extract the pitch content from the audio is not perfect itself.
Comment: e.g., suppose the algorithms say that E4 has a relative strength of 9 out of 10. There is no way to know how to split that up in to now how much to give each fundamental for it's E4. E.g., does the E2 pitch get 3, the A2 pitch get 3 and the C3 pitch get 3? or E2 pitch get 7 and the A2 pitch get 1 and the C3 pitch get 1? The only way to know this is to be able to know the timbre of the instrument(which tells us, in general, the amplitude profile of the overtones).  It starts getting really complex really quick(Neural networks probably can handle it with enough training but no one has done it)
Comment: I should also mention that real pitched sounds tend to deviate slightly from the overtone series. This further complicates exacting the details.


expressing contour integral in different form

Question: Hi I have a short question regarding contour integration: Given that $f(z)$ is a continuous function over a rectifiable contour $z = x + iy$. If $f(z) = u(x,y) + iv(x,y)$, why does it follow that the contour integral can be expressed as $$\int_{C}f(z)dz = \int_{C}(udx-vdy) + i \int_{C}(vdx +udy)$$
where on the right-hand side, $C$ is a rectifiable curve in the $xy$-plane.
Comment: $dz = dx + i dy$ and $f = u+v$ hence $f dz = (u+v)(dx+idy) = \ldots$
Answer: Hint: $$\int_{C}f(z)dz = \int_{C}[u(x,y)+iv(x,y)](dx+idy)$$
Answer: Another way to see it is by expanding the integral
$$
\int_\gamma f(z)dz = \int_a^bf(\gamma(t))\gamma'(t)dt = \int_a^b(u(\gamma_1(t),\gamma_2(t)) + i v(\gamma_1(t),\gamma_2(t)))(\gamma_1(t) + i\gamma_2(t))dt,
$$
where $\gamma(t) = (\gamma_1(t),\gamma_2(t)), t\in [a,b],$
and applying the definition of the line integrals
$$
\int_\gamma p dx = \int_a^b p(\gamma(t))\gamma_1'(t)dt
$$
and
$$
\int_\gamma q dy = \int_a^b q(\gamma(t))\gamma_2'(t)dt
$$
from calculus.


Linearization of Nonlinear System

Question: I am trying to linearize the following nonlinear system to determine the type of equilibrium point present. The system is
$x'=a-x-(4xy/(1+x^2))$
$y'=bx(1-(y/(1+x^2)))$
Do I do this by creating the jacobian, evaluating at the c.p., and solving for the eigenvalues?  With the two parameters a,b things get really messy when I try to solve for the eigenvalues. I thought there was a simpler way by simply ignoring the nonlinear terms...
Comment: You should use Latex/Mathjax to format your question so it is easier to understand.
Comment: To user75514:  I edited your post by putting "$" signs around the equations so they would be rendered as Latex.  Hope this is OK.
Comment: @user75514: Is the system written correctly?
Comment: @Amzoti:  Well, now that I look *carefully* at it, I think you are probably correct.  As I wrote, I stuck in the "$" signs and nothing else.  Someone should probably fix it.  user75514 hasn't been around since he/she posted, judging by the user profile.
Comment: @Amzoti:  OK, I altered it to read with $x^2$ instead of $x2$.
Comment: @Amzoti: I certainly don't feel *threatened*! ;-)
Comment: @Amzoti:  For all you know, *I* am trick-or-treating; for all I know, *you* are!  Happy Hallowe'en to one and all! ;-)
Answer: You have a differential equation of the form
$$q' = f(q)$$
where $f$ is some nonlinear function. I assume you are starting from some known equilibrium point $q_0$ where $f(q_0) = 0$. Now suppose you are a small displacement $q(t) = q_0 + \delta q(t)$ away from $q_0$. Plugging in you get
$$\delta q' = f(q_0 + \delta q).$$
Apply Taylor's theorem on the right-hand side to get
$$\delta q' = f(q_0) + \nabla f \cdot \delta q + O(\delta q^2).$$
Assuming small displacements, and that the Taylor coefficients of $f$ decay sufficiently quickly, etc. we can ignore the higher-order terms, to get the approximate, linearized ODE
$$\delta q' = 0 + \nabla f \cdot \delta q.$$
Solve this in the usual way, i.e. find the eigenvectors $v_i$ of $\nabla f$ with eigenvalues $\lambda_i$, and notice these give you solutions of the form
$$\delta q = Ae^{\lambda_i t}v_i.$$
Comment: When you are dealing with a system of differential equations, this amount to  taking the Jacobian of the system, evaluating at the critical point, and then solving for the eigenvalues and vectors.  I guess things just get really messy because of the two parameters present...

Linearization of Nonlinear System


Mysql union query duplicate rows

Question: I am working on a dating site and thanks to the great answers I managed to make this query. I am totally a newb:)
The problem with this union query is that it does not seem to remove duplicate entries and I have no idea why.
username - is the name of people who join the site;
willingness - is about what they are willing to do, part of their profile page.
For example, if I search for the word "lunch" (like in the query bellow), the query finds people that have the chars "lunch" in their username or in their willingness profile.
But sometimes, someone has both the word "lunch" in his username and in it's willingness profile.
How can i remove these duplicate rows?
I use this query for a php navigation code.
Can you please help?
result = mysql_query("

(SELECT 1 as sort_col,username,pic0 FROM ".$table." WHERE username IS NOT NULL AND username LIKE '%lunch%')

UNION

(SELECT 2 as sort_col,username,pic0 FROM ".$table." WHERE username IS NOT NULL AND age BETWEEN '28' AND '45' AND willingness LIKE '%lunch%' )

ORDER BY sort_col

LIMIT ".$offset.", ".$rowsperpage."");

Any ideas?
Thank you!
Answer: Your sort_col guarantees that the rows between the two queries will always be distinct (1 vs. 2), so no duplicates will occur.
Comment: @webmasters As I said, the row where username is "lunchy" gets a sort_col value of 1 and the row where profile contains "lunch" gets a sort_col value of 2. Hence the two rows are not exact duplicates and are not deduped by the UNION operation.
Comment: This is strage, I am seeing them when I actually use the code, any idea why? keep in mind that the term "lunch" and the duplicate rows appear when the username is lets say "lunchy" and also that name as "lunch" in his profile. So the query lists them twice.
Comment: Ty very much for clearing it up. Is there a way that for the username "lunchy" to delete it's profile row from the query, or something like that? Basicly find a solution to work around this?
Answer: Workaround. You could use:
SELECT username, pic0
FROM ".$table."
WHERE ( username LIKE '%lunch%' )
   OR ( username IS NOT NULL
      AND age BETWEEN 28 AND 45
      AND willingness LIKE '%lunch%'
      )
ORDER BY ( username LIKE '%lunch%' ) DESC

Mysql union query duplicate rows


How to correctly use the GPML Matlab code for an actual (non-demo) problem?

Question: I have downloaded the most recent GPML Matlab code GPML Matlab code and I have read the documentation and ran the regression demo without any problems. However, I am having difficulty understanding how to apply it to a regression problem that I am faced with.
The regression problem is defined as follows:

Let $\mathbf{x}_i \in \mathbb{R}^{20}$ be an input vector and $\mathbf{y}_i \in \mathbb{R}^{25}$ be its corresponding target. The set of $M$ inputs are arranged into a matrix $\mathbf{X} = [\mathbf{x}_1, \dots, \mathbf{x}_M]^\top$ and their corresponding targets are stored in a matrix $\mathbf{Y} = [\mathbf{y}_1 - \mathbf{\bar{y}}, \dots, \mathbf{y}_M-\mathbf{\bar{y}}]^\top$, with $\mathbf{\bar{y}}$ being the mean target value in $\mathbf{Y}$.
I wish to train a GPR model $\mathcal{G} = \lbrace \mathbf{X}, \mathbf{Y}, \theta \rbrace$ using the squared exponential function:
$k(\mathbf{x}_i, \mathbf{x}_j) = \alpha^2 \text{exp} \left( - \frac{1}{2\beta^2}(\mathbf{x}_i - \mathbf{x}_j)^2\right) + \gamma^2\delta_{ij}$,
where $\delta_{ij}$ equals $1$ if $i = j$ and $0$ otherwise. The hyperparameters are $\theta = (\alpha, \beta, \gamma)$ with $\gamma$ being the assumed noise level in the training data and $\beta$ is the length-scale.
To train the model, I need to minimise the negative log marginal likelihood with respect to the hyperparameters:
$-\text{log}\, p(\mathbf{Y} \mid \mathbf{X}, \theta) = \frac{1}{2} \text{tr}(\mathbf{Y}^\top\mathbf{K}^{-1}\mathbf{Y}) + \frac{1}{2}\text{log}\mid\mathbf{K}\mid + \,c,$
where c is a constant and the matrix $\mathbf{K}$ is a function of the hyperparameters (see equation k(xi,xj) = ...).

Based on the demo given on the GPML website, my attempt at implementing this using the GPML Matlab code is below.
covfunc = @covSEiso;
likfunc = @likGauss;
sn = 0.1;
hyp.lik = log(sn);
hyp2.cov = [0;0];
hyp2.lik = log(0.1);
hyp2 = minimize(hyp2, @gp, -100, @infExact, [], covfunc, likfunc, X1, Y1(:, n));
exp(hyp2.lik)
nlml2 = gp(hyp2, @infExact, [], covfunc, likfunc, X1, Y1(:, n));
[m s2] = gp(hyp2, @infExact, [], covfunc, likfunc, X1, Y1(:, n), X2);
Y2r(:, n) = m;

X1 contains the training inputs
X2 contains the test inputs
Y1 contains the training targets
Y2r are the estimates from applying the model
n is the index used to regress each element in the output vector
Given the problem, is this the correct way to train and apply the GPR model? If not, what do I need to change?
Answer: The GP does a good job for your problem's training data. However, it's not so great for the test data. You've probably already ran something like the following yourself:
load('../XYdata_01_01_ab.mat');

for N = 1 : 25
    % normalize
    m = mean(Y1(N,:));
    s = std(Y1(N,:));
    Y1(N,:) = 1/s * (Y1(N,:) - m);
    Y2(N,:) = 1/s * (Y2(N,:) - m);

    covfunc = @covSEiso;
    ell = 2;
    sf = 1;
    hyp.cov = [ log(ell); log(sf)];

    likfunc = @likGauss;
    sn = 1;
    hyp.lik = log(sn);

    hyp = minimize(hyp, @gp, -100, @infExact, [], covfunc, likfunc, X1', Y1(N,:)');
    [m s2] = gp(hyp, @infExact, [], covfunc, likfunc, X1', Y1(N,:)', X1');
    figure;
    subplot(2,1,1); hold on;
    title(['N = ' num2str(N)]);
    f = [m+2*sqrt(s2); flipdim(m-2*sqrt(s2),1)];
    x = [1:length(m)];
    fill([x'; flipdim(x',1)], f, [7 7 7]/8);
    plot(Y1(N,:)', 'b');
    plot(m, 'r');
    mse_train = mse(Y1(N,:)' - m);

    [m s2] = gp(hyp, @infExact, [], covfunc, likfunc, X1', Y1(N,:)', X2');
    subplot(2,1,2); hold on;
    f = [m+2*sqrt(s2); flipdim(m-2*sqrt(s2),1)];
    x = [1:length(m)];
    fill([x'; flipdim(x',1)], f, [7 7 7]/8);
    plot(Y2(N,:)', 'b');
    plot(m, 'r');
    mse_test = mse(Y2(N,:)' - m);

    disp(sprintf('N = %d -- train = %5.2f   test = %5.2f', N, mse_train, mse_test));
end

Tuning the hyperparameters manually and not using the minimize function it is possible to balance the train and test error somewhat, but tuning the method by looking at the test error is not what you're supposed to do. I think what's happening is heavy overfitting to your three subjects that generated the training data. No method will out-of-the-box do a good job here, and how could it? You provide the training data, so the method tries to get as good as possible on the training data without overfitting. And it fact, it doesn't overfit in the classical sense. It doesn't overfit to the data, but it overfits to the three training subjects. E.g., cross-validating with the training set would tell us that there's no overfitting. Still, your test set will be explained poorly.
What you can do is:

Get data from more subjects for training. This way your fourth person will be less likely to look like an "outlier" as it does currently. Also, you have just one sequence of each person, right? Maybe it would help to record the sequence multiple times.
Somehow incorporate prior knowledge about your task that would keep a method from overfitting to specific subjects. In a GP that could be done via the covariance function, but it's probably not that easy to do ...
If I'm not mistaken, the sequences are in fact time-series. Maybe it would make sense to exploit the temporal relations, for instance using recurrent neural networks.

There's most definitely more, but those are the things I can think of right now.
Comment: @Masood You're right, but when $n>30$ the Student t distribution is very close to the gaussian distribution. Even with n>20, we generally have a good approximation.
Comment: I am assuming a zero-mean Gaussian process. Since the targets do not have a zero mean I centre them by subtracting their mean. You're right about the redundancy; I have removed those two lines. I don't believe the covariance function is correct given the problem, and the I'm not confident about the intitialisation of the hyperparameters. My doubts come about from the results. The residuals are practically the same as those for ridge regression, and my data is known to be highly nonlinear.
Comment: You're right about the substraction; it shouldn't hurt in any case. I'd normally add that to the covariance function, like `covfunc = { 'covSum', { 'covSEiso' } }` I don't quite see how this takes care of noisy data now, it seems the toolbox has changed quite a lot since I last used it, will have a closer look at it later.
Comment: What do you know about your problem that makes you think that the covSEiso isn't a reasonable choice? And the ridge regression you use, is that a kernlized one or linear? If you use kernels, it's not that surprising that you get similar results.
Comment: Can you provide sample data of your problem? That would make things a bit easier, perhaps with just one target dimension.
Comment: The only reason why I think covSEiso is unreasonable is due to the poor performance. I expected better performance for two reasons: (1) the improvement over linear ridge regression was hardly noticeable; (2) a number of publications doing similar work (almost the same data) obtain significantly better results with GPR. I have placed a sample of my data here: http://www.megaupload.com/?d=7MSTQXUF Training data is X1/Y1, test data X2/Y2 (note: observations are column-wise). If you wish to pick one target dimension, I suggest the 15th dim. Targets are angles in degs (wrapped to -180/+180). Thanks
Comment: I just had a quick look, but I believe you're posing a pretty hard problem here, since the statistical properties of your training and test data are quite different. I trained a neural network (that's what I'm most comfortable with) using the 15th target dimension from Y1 for X1 as input and divided the training data randomly into 50% training and 50% validation set. On both sets the fit was quite nice, and with 50% validation size I would rule out overfitting. On the test data however, it looked horrible. Will have another look tomorrow ...
Comment: Just thought about it again, the different statistical properties don't have to be a problem. It's late, will look at it later. One question though: where do the training and test sets come from?
Comment: The problem relates to human pose estimation (motion capture). The inputs relate to 5 inertial sensors worn by the subject (wrists/ankles/upper-back), each giving the orientation of the sensor as a 4D quaternion (rotational offset to a global coordinate system), concatenated to 20D vectors per pose ("frame"). The outputs are the pose representation, a 25D vector of joint angles (in degrees), the first 3 are overall orientation, then torso (4-5), left arm (6-10), right arm (11-15), left leg(16-20), right leg (21-25). The sequence you have is a walking sequence.
Comment: The reason why the test set would give horrible results is that it is of an unseen person. The training set contains the motions of 3 different subjects, and the test set contains a 4th subject. I have had a lot of difficulty training models to generalise to unseen subjects; training models to generalise to unseen poses of the same subject are quite good though, as you have seen. Thanks for all the effort you have put in :-)
Comment: You mentioned papers that do something similar and have better results. Any idea how people achieve this? Anything from my list maybe?
Comment: I think they achieve it by using more data (6 reps, I have 1). The data also seems to have less variance and noise, but I cannot be certain as I am only going by their published qualitative results, which look a bit stiff and unnatural. Also, they train subject-specific models, which also corresponds to your suspicions of over-fitting to the three subjects in my training set. Unfortunately, nobody (to my knowledge) has produced a comparison of same-subject and inter-subject model performance, which is very surprising given how topical this is for the application domains.
Comment: It maybe a silly question but why we use 2 for this prediction interval:
f = [m+2*sqrt(s2); flipdim(m-2*sqrt(s2),1)]; If we assume each point a Gaussian random variable with unknown mean and variance, it should be computed using t-student distribution
Comment: I noticed that you normalize the targets in the code Do we always need to normalize targets when building Gaussian Process predictors?
Comment: @ahans Nice answer. Could you please elaborate a bit on your 2nd suggestion? i.e. incorporating prior knowledge via a covariance function?
Answer: I think the problem may be one of model mis-specification.  If your targets are angles wrapped to +-180 degrees, then the "noise process" for your data may be sufficiently non-Guassian that the Baysian evidence is not a good way to optimise the hyper-parameters.  For instance, consider what happens when "noise" causes the signal to wrap-around.  In that case it may be wise to either perform model selection by minimising the cross-validation error (there is a public domain implementation of the Nelder-Mead simplex method here if you don't have the optimisation toolbox).  The cross-validation estimate of performance is not so sensitive to model mis-specification as it is a direct estimate of test performance, whereas the marginal likelihood of the model is the evidence in suport of the model given that the model assumptions are correct.  See the discussion starting on page 123 of Rasmussen and Williams' book.
Another approach would be to re-code the outputs so that a Gaussian noise model is more appropriate.  One thing you could do is some form of unsupervised dimensionality reduction, as there are non-linear relationships between your targets (as there are only a limited way in which a body can move), so there will be a lower-dimensional manifold that your targets live on, and it would be better to regress the co-ordinates of that manifold rather than the angles themselves (there may be fewer targets that way as well).
Also some sort of Procrustes analysis might be a good idea to normalise the differences between subjects before training the model.
You may find some of the work done by Neil Lawrence on human pose recovery of interest.  I remember seeing a demo of this at a conference a few years ago and was very impressed.
Comment: From my analysis, I have noticed that the discontinuities in the output space cause a number of problems. I have considered using joint locations rather than joint angles to overcome this problem. By dimensionality reduction, did you have a particular method in mind? Unlike image-based approaches, I don't see how the differences in subjects (other than their movement patterns) would effect the training of the model, given that I am using orientations of IMU sensors which are consistently placed and post-processed to be aligned between subjects.
Comment: I have come across Lawrence's paper before. Since only 1 sequence was mentioned in the paper, it seems that some form of k-fold CV was performed. In which case, the problem becomes almost trivial. Same-subject mappings of an activity, particular one that is cyclical, are typically straightforward to obtain decent pose estimates. I have compared same-subject and inter-subject mappings, and the difference is very significant. Unfortunately, research in this area is basically incomparable due to everyone using their own regression framework, mocap data, error metrics, and input/output structures.


Extract values from xarray dataset using geopandas multilinestring

Question: I have a few hundred geopandas multilinestrings that trace along an object of interest (one line each week over a few years tracing the Gulf Stream) and I want to use those lines to extract values from a few other xarray datasets to know sea surface temperature, chlorophyll-a, and other variables along this path each week.
I'm unsure though how exactly to use these geopandas lines to extract values from the xarray datasets. I have thought about breaking them into points and grabbing the dataset values at each point but that seems a bit cumbersome. Is there any straightforward way to do this operation?
Answer: Breaking the lines into points and then extracting the point is quite straightforward actually!
import geopandas as gpd
import numpy as np
import shapely.geometry as sg
import xarray as xr

# Setup an example DataArray:
y = np.arange(20.0)
x = np.arange(20.0)

da = xr.DataArray(
    data=np.random.rand(y.size, x.size),
    coords={"y": y, "x": x},
    dims=["y", "x"],
)

# Setup an example geodataframe:
gdf = gpd.GeoDataFrame(
    geometry=[
        sg.LineString([(0.0, 0.0), (5.0, 5.0)]),
        sg.LineString([(10.0, 10.0), (15.0, 15.0)]),
    ]
)

# Get the centroids, and create the indexers for the DataArray:
centroids = gdf.centroid
x_indexer = xr.DataArray(centroids.x, dims=["point"])
y_indexer = xr.DataArray(centroids.y, dims=["point"])

# Grab the results:
da.sel(x=x_indexer, y=y_indexer, method="nearest")

<xarray.DataArray (point: 2)>
array([0.80121949, 0.34728138])
Coordinates:
    y        (point) float64 3.0 13.0
    x        (point) float64 3.0 13.0
  * point    (point) int64 0 1

The main thing is to decide on which point you'd like to sample, or how many points, etc.
Note that the geometry objects in the geodataframe also have an interpolation method, if you'd like draw values at specific points along the trajectory:
https://shapely.readthedocs.io/en/stable/manual.html#object.interpolate
In such a case, .apply can come in handy:
gdf.geometry.apply(lambda geom: geom.interpolate(3.0))

0      POINT (2.12132 2.12132)
1    POINT (12.12132 12.12132)
Name: geometry, dtype: geometry

Answer: I have used regionmask and it is pretty fast and easy to use. The mask_geopandas method is what you need.
Answer: Since GeoPandas uses the same conventions as Pandas, the best way is to unify the data type when you're working on it.  You can do this in xarray with:
xr.Dataset.from_dataframe(df)


Can I use synthetic division??

Question: Find the quotient and remainder when $2x^4+7x^3+x^2+x+4$ is divided by $2x+1$
Here coefficient of $x$ is $2$ in divisor.
Can we use here division algorithm??
Taking $x=-1/2$ and using synthetic division I got
Remainder is $3$
And quotient $2x^3+6x^2-2x+2$
Which I can simply to $x^3+3x^2-x+1$ (since $-1/2$ is a divisor of 2$)
Which is the answer
Guys what is your opinion??
Answer: Note that
$$(2x+1)(x^3+3x^2-x+1)=2x^4+7x^3+x^2+x+1$$ so the remainder is equal to $$3$$
Comment: Then my process will be right?
Comment: Yes, the remainder is #3#, but i can not see what you've written on you sheet.
Comment: OK I forgot to add an image but thanks for the help
Comment: But you know now, that your result $3$ is right.
Answer: When you use synthetics division with $$x=-1/2$$ you get $$ P(x) = (x+1/2)Q(x) +R$$
Thus to get your original division you need to modify your Q(x).
$$ P(x) = (x+1/2)Q(x) +R \iff P(x) = (2x+1)Q^* (x) +R$$
Where $$Q^* (x) = (1/2) Q(x)$$
In our problem $$Q(x) = 2x^3+6x^2-2x+2$$ and $R=3$
Thus we get $$ Q^*(x) = x^3+3x^2-x+1$$ with $R=3$

Can I use synthetic division??


sql/oracle select values seperated by comma with grouping

Question: I have first table like this: table_1

date
group_number
c_id
rate

01.01.2020
A
001
12.0

02.01.2020
A
001
12.0

01.01.2020
A
002
10.0

01.01.2020
B
103
8.0

01.01.2020
B
101
8.0

01.01.2020
C
203
11.0

And have second table_2 with name of group with date of records:

date
group_number

01.01.2020
A

02.02.2020
A

03.03.2020
A

01.01.2020
B

01.02.2020
B

01.01.2020
C

The task is to write to new column in table_2 the rates of each c_id seperated by comma, grouped by group_number. I need to add new column to table_2 as next:

date
group_number
rate_for_groups

01.01.2020
A
12.0, 10.0

02.02.2020
A
12.0, 10.0

03.03.2020
A
12.0, 10.0

01.01.2020
B
8.0, 8.0

01.02.2020
B
8.0, 8.0

01.01.2020
C
11.0

I have tried to do smth like this:
select *,
  listagg(rate, ',') within group (order by C_ID) as rates
from table_1
group by group_number

but it raised the error "not a group by expression"
Comment: select date, group_number, .. group by date, group_number
Comment: After posting my answer, I see that I may not have understood the task correctly. Why are you showing `8.0, 8.0` for all B rows, but `12.0, 10.0` rather than `12.0, 12.0, 10.0` for the A rows? Does the date come into play here? If so, how exactly?
Comment: becuz in A row there are two same c_id's, in B the c_id's are different so 8.0 are added twice.
Comment: From the [docs](https://docs.oracle.com/cd/B10501_01/server.920/a96525/e900.htm#1004903): ***Cause**: The GROUP BY clause does not contain all the expressions in the SELECT clause. SELECT expressions that are not included in a group function, such as AVG, COUNT, MAX, MIN, SUM, STDDEV, or VARIANCE, must be listed in the GROUP BY clause.* It is quite clear description.
Answer: Your query shows only half the task: You are only looking at table_1. With GROUP BY group_number you tell the DBMS to select one row per group_number only. That is fine for that table. But you cannot SELECT * then, because there are several rows per group_number. How is the DBMS supposed to know which row's values to display then for a group_number?
Remove the * from that query to get it valid. select the group_number instead. Then join this result to table_2.
select *
from table_2 t2
left outer join
(
  select
    group_number,
    listagg(rate, ',') within group (order by c_id) as rates
  from table_1
  group by group_number
) t1 on t1.group_number = t2.group_number
order by t2.group_number, t2.date;