pointer array to structure in C++

Question: int noOfEmployee = 0;
cout << "Enter no. of Employee" << endl;
cin >> noOfEmployee;

Ereg = new EMPLOYEE[noOfEmployee];

string defaultName = "Emloyee";

for(int i = 0; i < noOfEmployee; i++) {
    Ereg->regno = i + 1;
    Ereg->name = defaultName;
}

for(int i = 0; i < noOfEmployee; i++) {
    cout << Ereg->regno << "\t"
         << Ereg->name << endl;
}

delete [] Ereg; //segmentation Fault if [] missed

output is:
Enter no. of employee
5
5    Employee
5    Employee
5    Employee
5    Employee

How could access array elements in this or do something like this
Ereg[i]->regno = i;
Ereg[i]->name = defaultName;

Answer: Ereg points to the 1st element of array, so Ereg->regno or Ereg->name will always access the 1st element.

How could access array elements

You should
for(int i = 0; i < noOfEmployee; i++) {
    Ereg[i].regno = i + 1;
    Ereg[i].name = defaultName;
}

for(int i = 0; i < noOfEmployee; i++) {
    cout << Ereg[i].regno << "\t"
         << Ereg[i].name << endl;
}

See subscript operator.
BTW

//segmentation Fault if [] missed

You should use delete[] for array new[]d (and use delete for pointer newd).
Answer: Your output repeats itself because you never use Ereg as an array, only as a pointer to EMPLOYEE.

How could access array elememts in this or do something like this
Ereg[i]->regno = i;
Ereg[i]->name = defaultName;

Use . instead of ->, because Ereg[i] is a struct, not a pointer to struct.

//segmentation Fault if [] missed

This is expected. You need [] because you allocated an array. Hence, delete[] must be used to avoid undefined behavior.
Answer: Thanks for help,
There is one way of doing this i got just now.
As incrementing a structure pointer will move it towards next structure location so I used basic method.
(Ereg + i)->regno = i;
(Ereg + i)->name = defaultname;

And it worked just fine.
Answer: This is some kind of fun....
Also I can do this to use as a array.
As we know
*(Ereg + i) = Ereg[i]
// this is how array works, so I can write this

(Ereg + i) as (&Erg[i])

And this also worked fine.

pointer array to structure in C++


How exactly do exponentials work?

Question: Hi all I know that $5^1 = 5$, $5^2 = 25$, $5^3 = 125$.
But why is $5^{1.5} = 11.180339887498949$ ?
How did we get the number $11.180339887498949$ ?
Comment: @Pacerier, how do we know that $\sqrt 5 = 2.23606...$? Well, 2 is too small because its square is 4, but 3 is too big because its square is 9, similarly 2.2 is too small, 2.3 is too big, 2.23 is too small, 2.24 is too big, and so on... Of course, there are [more efficient methods](http://en.wikipedia.org/wiki/Nth_root#Computing_principal_roots). But this is something [you could have invented](http://www.google.com/search?q=you+could+have+invented) yourself, and I find such explanations to be the most edifying.
Comment: $5^{1.5}=5^{1+0.5}=5\times5^{0.5}=5\sqrt{5}$.
Comment: We know that, for example, $(5^2)^3 = 5^{2\times 3}$. So, whatever $5^{1.5}$ might be, we'd like it to satisfy $(5^{1.5})^2 = 5^{1.5\times 2} = 5^3 = 125$. So $5^{1.5}$ ought to be the square root of 125.
Comment: To avoid any confusion about irrationals, consider instead $4^{1.5} = 8$.
Comment: @Lieven, because I felt like the answer this question really ought to get is something long, comprehensive, and impressive, like [André Nicolas' explanation of irrational exponents](http://math.stackexchange.com/a/55078/856), and I didn't have time to write one.
Comment: Have you heard of these logarithms, if not you can read this wikipedia article: http://en.wikipedia.org/wiki/Logarithm
Comment: @Rahul Narain, This is the answer. Why did you make it a comment?
Comment: @RahulNarain But this is like a chicken-egg problem. So I understand that 5^1.5 = 5 × sqrt(5). But how do you solve for sqrt(5) in the first place?
Comment: @RahulNarain do you mean to say that when I key sqrt(5) in my calculator, it's using that algorithm (which you linked) to solve for sqrt(5) ?
Comment: Well, gee, how am I supposed to know how your calculator does it? I don't even know how *my* calculator does it! ...More seriously, I think kahen and I have answered the question you asked in your main post. If you want to know specifically about the algorithms typically used in calculators, you should edit that into your question (or ask a new one) and I'm sure someone with the requisite expertise will come along and answer. (I'd wager a calculator with an $x^y$ function probably computes it [via $\exp(y\log x)$](http://math.stackexchange.com/q/55068/856) instead, but I'm just speculating.)
Comment: @RahulNarain ok thanks for the help =D
Answer: If $a,b$ are positive integers and $x > 0$, then $x^{a/b}$ is defined as $\sqrt[b]{x^a}$, where the $b^\text{th}$ root of $y>0$ is the unique positive real number $r$ such that $r^b = y$. So $5^{1.5}$ = $\sqrt{5^3}$, i.e. it's the number which squared is $125$.
This leaves open many questions such as "why do $n^\text{th}$ roots exist?" and "what about $x^\alpha$ where $\alpha$ is irrational?"
There are no easy answers to those questions which don't involve a first course in real analysis. If you haven't done university level real analysis, you kind of have to take on faith that exponentiation works and obeys the rules given. I know that's a disappointing answer, but it's the only decent answer I have.
Comment: But this is like a chicken-egg problem. So I understand that 5^1.5 = 5 × sqrt(5). But how do you solve for sqrt(5) in the first place?
Comment: Like I said: there is no easy answer. You either construct a [sequence](http://en.wikipedia.org/wiki/Sequence_%28mathematics%29) converging to $\sqrt{5}$ (think of succesively better approximate solutions to the equation $x^2 = 5$) or you realize it as the [supremum](http://en.wikipedia.org/wiki/Supremum) of e.g. the set $\lbrace x \in \mathbb R \;|\; x^2 \leq 5\}$. Here's a note that explains it, but I think it's going to be way over the level you're at: [PDF](http://arxiv.org/pdf/0805.3303). It was not without reason I wrote that you have to take a lot of things on faith about exponentiation
Comment: Thanks for the link to that PDF Kahen!
Answer: $$
2\cdot2\cdot2=8,
$$
so multiplying by $2$ three times is the same as multiplying by $8$,
and multiplying by $8$ one-third of one time is the same as multiplying by $2$.

How exactly do exponentials work?


Get entities that share the Entity Reference field

Question: Let's say that I have two content types:

Article

title
body
field_ref_term

Page

title
body
field_ref_term

Those two content types are sharing the same field: "field_ref_term" wich is a "Entity reference" field type, to another entity called 'Term' (or something else, it doesn't mind).
What I'm trying to do is, when displaying a particular content of type "Page", I would like to be able to display a list of content in a block who shares the same value for the "field_ref_term" field.
It's a bit like the "Content: Has taxonomy term ID" contextual filter from Views and Taxonomies, where you set the value to "Taxonomy term ID from URL" then check "Load default filter from node page, that's good for related taxonomy blocks".
Do you think this is currently possible with Entity Reference ?
Thanks
Answer: I finally found a way to do it, here are the steps:

Add a relationship: Entity Reference: Referenced Entity, select the right field to match, select "Require this relationship"
Add a relationship: Entity Reference: Referencing Entity, selection the just add relationship in step 2 and check "Require this relationship".
Add a contextual filter: Content:nid, provide default value: Content ID from URL
Add a contextual filter: Content:nid, select the relationship "Content referencing...", provide default value: Content ID from URL, then in the More fieldset, check exclude.
Add a field "Title" and select the relationship "Content Referencing..."

I will close this question, feel free to comment or propose other solutions.
Thanks.
Comment: Nice! Very helpful. Thanks for sharing the answer.
Answer: As opposed to the other answer, if you need it programmatically :
a quite dynamic solution (a bit dirty too but I needed it quickly) so you do not need to hard code the name fo the referencing field and it is automatically handled with new referencing field you will add in the future :
in your custom module :
<code>/**
 * Implement hook_field_create_instance().
 */
function MY_CUSTOM_MODULE_field_create_instance() {
  _MY_CUSTOM_MODULE_set_variable_node_back_references();
}

/**
 * Implement hook_field_delete_field().
 */
function MY_CUSTOM_MODULE_field_delete_field() {
  _MY_CUSTOM_MODULE_set_variable_node_back_references();
}

/**
 * Set Variable node_back_references.
 */
function _MY_CUSTOM_MODULE_set_variable_node_back_references() {
  $field_list = db_select('field_config', 'fc')
    ->fields('fc', array('field_name', 'data'))
    ->condition('fc.data', '%"foreign keys";a:1:{s:4:"node"%', 'like')
    ->condition('fc.deleted', 0);
  $field_list->innerJoin('field_config_instance', 'fci', 'fci.field_name = fc.field_name');
  $field_list->rightJoin('node_type', 'n', 'n.type = fci.bundle');
  $fields = $field_list->execute()->fetchAll();

  $fields_array = array();
  foreach ($fields as $field) {
    $unserialized = unserialize($field->data);
    if (isset($unserialized['settings']['handler_settings']['target_bundles'])) {
      foreach ($unserialized['settings']['handler_settings']['target_bundles'] as $bundle) {
        $fields_array[$bundle][] = $field->field_name;
      }
    }
  }

  variable_set('node_back_references', $fields_array);
}

function _MY_CUSTOM_MODULE_get_referencing_nodes($node) {
  $nids = array();
  $fields = variable_get('node_back_references', array());
  if (isset($fields[$node->type])) {
    foreach ($fields[$node->type] as $field) {
      $query = new \EntityFieldQuery();
      $query->entityCondition('entity_type', 'node');
      $query->propertyCondition('status', 1);
      $query->fieldCondition($field, 'target_id', $node->nid);
      $result = $query->execute();
      $nids = isset($result['node']) ? array_merge(array_keys($result['node']), $nids) : $nids;
    }
    $nodes = (!empty($nids)) ? node_load_multiple($nids) : array();

    return $nodes;
  }

  return $nids;
}
</code>
where you need to get the parent nodes given the child node :
<code>$nodes = _MY_CUSTOM_MODULE_get_referencing_nodes($node);
</code>


Dynamically setting div height in CSS

Question: I have the following code in my CSS:
div.h120 {
    height: 120px;
}

Can it be set dynamically so that if I place a class h150 in a div, it now have height 150px?

One way to achieve this would be to create a class to every possible height value, but I would like to know if there's a better way.
Thank you for the attention.
Comment: Are you looking for a responsive website? Or are you looking for the user/client to be able to change the div height?
Comment: you cannot achieve this with pure CSS without writing all of it explicitly.  you need to resort to scss/sass or javascript
Comment: Hey @JD333 I made a dashboard in Python Bokeh and I'll send all the chart information to be consumed by the frontend team via json. I would like to send a CSS to adjust some of the charts heights based on the number of categories being displayed. Yes, I want it to be responsive.
Comment: Got it @sertsedat, I'll research it. Thank you
Answer: You could try using scss/sass for this purpose to create functions to generate classes for you:
div{
  @for $j from 1 through 200 {
        &.h#{$j}{
            height: ($j + px);
        }
    }
 }

Comment: Got it. The end result would be no different had I hardcoded all the 200 classes, right?
But your way sure is better and I'll research it, thank you.
Comment: Yup it gets preprocessed to a regular css file with the 200 classes which you will bundle alongside your production code. It's just about writing code efficiently and less code to review.

Dynamically setting div height in CSS


Apple Push Notification service SSL (Sandbox & Production) - What am I doing wrong?

Question: I am developing a application for IOS 8, 9 and 10, which should receive push notifications.
The certificate for push notifications has been created by using the option "Apple Push Notification service SSL (Sandbox & Production)", but unfortunately when I try to receive the push notification from the Sandbox environment, the notification I'm getting.
In particular, what I do is this.

Generate Push Certificate with the following option to the "Apple Push Notification service SSL (Sandbox & Production)"
Start XCode
I connect the device (iPod IOS 9.3.5) to the Macbook Pro.
The startup project d XCode.
NSLog("tokenString: (tokenString)")
The device token is successfully registered.
Sending the notification from the Java class, but unfortunately, the push notification I don't receive it

If, instead, generate, the ipa (XC Ad Hoc), and then I install everything through iTunes, the device token is successfully registered and when I send the push notification from the same Java class with the same Certificate, the push notification i receive it.
What am I doing wrong ?
Answer: The ad-hoc ipa build is a production build, if pushes are successfully getting sent to that then means that your "the Java class (sic)" is sending the pushes using the production certificate over the production environment.
When you build and run the app via Xcode then the app is being built in development mode, so you need to switch your "the Java class" to use the development certificate and to send the push over the development environment. The url for the development environment is gateway.sandbox.push.apple.com
P.S.
The Xcode build scheme can be changed to produce a production build instead of a debug build, but you want to be able to debug your apps don't you.
Comment: This morning I fired again, the certificate, but I generated the "Apple Push Notification service SSL (sandbox)" and then with the device connected to the USB port of the Macbook Pro, I received the push notification.
Thank you all for the support
Comment: Thanks for you answer. I think that certificate is generate is not correct. Whick is the different between "Apple Push Notification service SSL (sandbox)" and "Apple Push Notification service SSL (Sandbox & Production)"  ?
Comment: I think that option is new as I've not seen it before, but it specifically mentions HTTP/2. Is your server/network HTTP/2, if not better stick with generating dev and prod separately, even if you have two certificates you can still sign your server with both simultaneously.
Comment: Ok...tomorrow i create certificate only for sandbox and try send push notification


For $z+\frac{1}{z}=2\cos\alpha$ calculate $z^n+\frac{1}{z^n}$ If $n$ is a positive integer

Question: For $z+\frac{1}{z}=2\cos\alpha$ calculate   $z^n+\frac{1}{z^n}$ If $n$ is a positive integer
here's my first attempt
$z^2-2\cos\alpha+1=0$ so we have $z=\cos\alpha\pm i\sin\alpha= e^{\pm i\alpha}$
but I thought there is a simple approach
What other approach would you suggest?
Comment: This is the proper approach.  If that is the value of $z$, what is $z^n$?
Comment: $z^n=e^{\pm in\alpha}$
Comment: Which yields a final answer of....?
Comment: Aron, here is where I don't find the answer
Answer: $$\small z+\frac{1}{z}=2\cos{\alpha}\iff\left(z-\cos{\alpha}\right)^{2}+\sin^{2}{\alpha}=0\iff z=\cos{\alpha}\pm\mathrm{i}\sin{\alpha}\iff z=\mathrm{e}^{\pm\mathrm{i}\alpha}\iff z^{n}=\mathrm{e}^{\pm n\mathrm{i}\alpha} $$
Thus : $$ z^{n}+\frac{1}{z^{n}}=\mathrm{e}^{\pm n\mathrm{i}\alpha}+\mathrm{e}^{\mp n\mathrm{i}\alpha}=2\cos{\left(n\alpha\right)} $$
Comment: CHAMSI,So beautiful solution, thanks


Camada de Aplicação

Question: Tendo base essa pergunta e essa outro pergunta, em um ambiente que utiliza o Entity Framework e Asp.net MVC.
Vejo em muitos exemplos a não utilização da camada de aplicação, uma utilização da camada de dados (Entity Framework)  no Controller. Então vêm minha primeira dúvida:
É realmente necessário a utilização dessa camada? E quais as vantagens de sua utilização?
Seguindo o raciocínio, em um ambiente onde não existem exclusões de dados e sim um campo ativo.
Em relação a listar os dados e exclui-los (alterar o valor do campo ativo):
Qual camada é responsável por isso? A camada de acesso aos dados, a camada de aplicação, ou então o Controller?
A última dúvida é em relação aos includes.
Implementando este método na camada de dados (Entity Framework):
 public IQueryable<T> Query(params Expression<Func<T, object>>[] includes)
    {
        IQueryable<T> Set = this.Query();
        foreach (var include in includes)
        {
            Set = Set.Include(include);
        }
        return Set;
    }

Seria dever da camada de aplicação falar quais includes ele irá utilizar ou então o Controller?
No fundo, não está muito claro para mim, qual a real responsabilidade da camada de aplicação.
Até então para mim a sua única responsabilidade era fazer com que a camada de interação com o usuário (Controller) não precisasse ter relação com a camada de dados.
Comment: Vinculada: http://pt.stackoverflow.com/questions/51536/quando-usar-entity-framework-com-repository-pattern/80696#80696
Answer: É realmente necessário a utilização dessa camada (repositório)? E quais as vantagens de sua utilização?
Não é necessária.
A vantagem da camada é poder escrever mais facilmente testes unitários, porque ao invés de injetar nos repositórios classes que efetivamente acessam dados, você pode injetar outras coisas, como por exemplo, outras classes que trabalhem com dados fictícios e controlados.
Qual camada é responsável por isso (persistência de dados)? A camada de acesso aos dados, a camada de aplicação, ou então o Controller?
A camada de acesso a dados apenas abstrai as operações de banco conforme você inclui, altera, apaga ou seleciona objetos. É ela que transforma um objeto em um elemento de dados que seu banco de dados entenda.
A camada de aplicação, num contexto MVC, é quase supérflua, porque a interação de dados de diferentes modelos já é feita pelo Controller.
Portanto, a responsabilidade de alteração dos objetos é do Controller e das regras dos Models, e de efetivamente realizar a operação em banco é de responsabilidade da camada de dados.
Seria dever da camada de aplicação falar quais includes ele irá utilizar ou então o Controller?
Não. Em teoria a camada de aplicação organiza e valida dados vindos da apresentação. O Include faz com que, na seleção, você force o Entity Framework a carregar antecipadamente um determinado dado (aproveitando o recurso de join de um banco relacional, por exemplo, e melhorando o desempenho).
Da forma com que está sendo desenvolvida sua aplicação, o Controller está se tornando uma camada oca, sem função. O correto seria deixar de lado o conceito de camada de aplicação e usar apenas o Controller.

Camada de Aplicação


Is it ok to refer to frequentist statistics as traditional or classical statistics?

Question: I don't know of any statisticians or work in the data science industry so I'm not really in the know about all the lingo.  I am trying to explain stats to others and was wondering if I could use the term "classical" or "traditional" instead of "frequentist", mainly because I do not like the word frequentist.  I am hoping to use the word classical or traditional whenever I want to contrast frequentist stats with Bayesian stats.
Thanks
Comment: Why don't you like the word "frequentist"? It seems more descriptive than "classical" which could mean almost anything. E.g. I would not call random forests "classical".
Comment: The trouble you could encounter is if you use such terminology to refer to a modern frequentist technique. For the most part, though, you're just doing some branding. Call it what gets your point across.
Comment: Good point on not calling random forests classical.  Ok, I guess frequentist it is...
Answer: In short, I'd say your safest using frequentist vs Bayesian. Classical is tricky from a historical perspective because "classical" Bayesian probability actually predates our "classical" frequentist statistical methods. Occasionally I'll hear people refer to frequentist Statistics as "Fisherian", but you're probably best avoiding that.
That said, I don't think anyone would turn up their nose at you using Classical vs Bayesian, but since you asked that's what I'd recommend.


Difference of closures and closure of difference

Question:
Let $A,B$ be subsets of a topological space $X$. Is it true that $\overline{A}-\overline{B}\subseteq\overline{A-B}$?

Suppose $x\in\overline{A}-\overline{B}$. So all open sets containing $X$ also contains an element of $A$. And there exists an open set $U$ containing $X$ that contains no element of $B$. So $U$ contains an element of $A-B$. But this is not enough to conclude that $x\in\overline{A-B}$. So I'm thinking the answer might be negative, but cannot find a counterexample.
Answer: If $x\notin\operatorname{cl}(A\setminus B)$, then $x$ has an open nbhd $U$ such that $U\cap(A\setminus B)=\varnothing$. And $$U\cap(A\setminus B)=(U\cap A)\setminus(U\cap B)\;,$$ so $U\cap A\subseteq U\cap B$. If $x\notin\operatorname{cl}B$, then $x$ has an open nbhd $V$ such that $V\cap B=\varnothing$. Let $W=U\cap V$. Then
$$W\cap A=W\cap(U\cap A)\subseteq W\cap(U\cap B)=W\cap B=\varnothing\;,$$
so $W\cap A=\varnothing$, and $x\notin\operatorname{cl}A$. Thus, if $x\in(\operatorname{cl}A)\setminus\operatorname{cl}B$, then $x\in\operatorname{cl}(A\setminus B)$.
Answer: It is true actually, here is a proof: let $x \in \overline{A} - \overline{B}$ and assume for the sake of contradiction that $x \notin \overline{A-B}$. This last condition is equivalent to "there exists an open set $U$ such that $x \in U$ and $U \cap (A - B) = \emptyset $". Since $x \in \overline{A} - \overline{B} \subset \overline{A}$ then $U \cap A \neq \emptyset$. This implies that $U \subset B$ but then we have a contradiction since $x \in U \subset B$ and $x \in \overline{A} - \overline{B}$.
What is false is the other inclusion: take $A = (0,2)$ and $B = (0,1)$. Then $\overline{A - B} = [1, 2]$ which is not a subset of $\overline{A} - \overline{B} = (1,2]$.
I hope it helps :)
Comment: You seem to have an error, you say that $U\cap A\not=\varnothing$ and $U\cap(A-B)=\varnothing$ implies that $U\subset B$, however I seem to be able to come up with a counter example.

Let $A = (0,2)$, $B = (1,3)$, $x = 1.5$. Now we must choose an open set $U$ that contains $x$ such that it does not intersect $A-B$, let us choose the set $(1.2,1.7)\cup(4,5)$. Now we have an open set that contains $x$, does not intersect $A-B$, does intersect $A$, and is not a subset of $B$.
Answer: I think I got it.
All open sets containing $X$ also contains an element of $A$. There exists an open set $U$ containing $X$ that contains no element of $B$.
Now take any open set $G$ containing $X$. Then $G\cap U$ is also an open set containing $X$. It contains no element from $B$. It must contain an element from $A$. So it contains an element from $A-B$.


Interpretation of absurdly large (but probably correct) Bayes Factors?

Question: I estimated a Bayes factor to compare a hypothetical model against a null-model (which obviously by visual comparison of the posterior predictive with the data) fails to capture a certain aspect in the data. The sampling is rather rigid. I need to finely tune pseudo priors, use a bisection technique to home in on a prior for the model index which leads to roughly equal samples in both models and thin a lot to obtain an effective sample size of 13000 samples for the model index.
Depending on the data, the Bayes factor against the null-model gets as large as 10^10 or 10^12.
I'm now concerned whether there may be something wrong with Bayes factors this large, or is it just a sign that the comparison isn't very reasonable, since the null-model obviously fails on the data?
Any comments are highly welcome!
P.S: Some details:
The models are advanced psychometric functions (that resemble logistic functions, but are conceptually a bit different, so logit analysis won't help). I have models for two competing hypothesis. The null-model is a special case of each hypothetical model. So I first wanted to calculate BFs for each model against the null.
Answer: Let's recall the Bayes factor: $B = \frac{p(d|M_1)}{p(d|M_0)}$, the ratio of model likelihoods $p(d|M_i)$ with data pairs $d = (x,y)$.
These are obtained by marginalising over the respective hyperparameters $\theta_1$ and $\theta_0$:
$p(d|M_i) = \int p(d|Mi,\theta_i) p(M_i|\theta_i) d\theta_i$
A very handwavy argument: regions of the model space that don't explain well the data correspond to very low likelihood; in particular, being the null model at the denominator, this might explain the large ratios you see.
Do you further average out over datasets, to obtain a single scalar as Bayes factor? And perhaps you could tell us a little more about the problem at hand?
Comment: Thanks for the reply.

I first tried to obtain a single BF for four datasets I have (with priors / pseudo priors for each dataset). But the chains were not mixing at all, so I can only obtain BFs for each dataset individually (by properly adjusting the model index priors for each dataset to facilitate sampling the weak model). I was hoping I can average the BFs (or at least draw conclusions if the BFs are large for all sets). I'll add some problem details to the original questions.


Squared Square Root confusion

Question: I am confused about the following:
$$\sqrt{x^2}=?\ (\sqrt x)^2 =?\ x^{2/2}$$
The source of confusion is: Let
$$f(x)=\sqrt {x^2}$$
$$g(x)=\ (\sqrt x)^2 $$
Then
$$f(-3)=3$$
$$g(-3)=\mbox{undefined}?$$
While the range of $f(x)$ and $g(x)$ might be the same, their domain appears to be different ($g(x)$ restricts $x$ to be positive). At the same time, both can be written as $$x^{2/2}$$
What am I missing?
-Thank you :)
Comment: Related http://math.stackexchange.com/questions/1495532/when-is-abc-abc-true/1495550
Answer: The problem is
$$\sqrt{x^2}=(\sqrt{x})^2$$
only if $x \ge 0$. If we are talking about real numbers.
In general we can write:
$$\sqrt{x^2}=\left(\sqrt{|x|}\right)^2 \Leftrightarrow \sqrt{x^2}=|x|$$
Answer: You are not missing anything. In general, the expression $x^a$ for $a \neq 0$ is usually defined only when $x \geq 0$ and on this domain, you have the exponential rule $x^{ab} = (x^a)^b = (x^b)^a$.
Sometimes you can extend the definition of $x^a$ so that it will be defined for all $x \in \mathbb{R}$. This happens for example if $a \in \mathbb{Z} \setminus \{ 0 \}$ or if $a = \frac{1}{n}$ when $n \in \mathbb{N}$ is odd but it doesn't happen if $a = \frac{1}{2}$. In any case, even if you can extend the definition, you noticed that the exponential rule stops holding in general for all $x \in \mathbb{R}$.
Thus, $x = (x^2)^{\frac{1}{2}} = (x^{\frac{1}{2}})^2$ for all $x \geq 0$ but it doesn't work for $x < 0$.
Answer: For all $x \in \mathbb R$: $\sqrt{x^2}=|x|$.

Squared Square Root confusion


Check if the given set is a subspace or not (Set of polynomial functions)

Question: The set of polynomial functions over a field $F$, of order $n$ or less than $n$, that satisfy in each case the given condition:

$2 f(0)- 3 f(1) = 0$

My thinking: This is not a subspace because of the scalar multiplication.
$2 f(0)- 3 f(1) = 0$ can be viewed as: $f(1) = (2/3) f(0)$. And when you have a function $f(x)$, and do the scalar multiplication of an k belonging to the field $F$, the condition works only when $k=1$.

$f(1)+f(2)+f(3)+...+f(k)=0$

I've got no idea how to tackle this part.
Comment: Good to see you've added your thinking. Now, scalar multiplication does no harm to the condition whatsoever, because if $2f(0)=3f(1)$, and you  multiply by some $k$ in the field, then $2(kf)(0)=(2k)f(0)=k(2f(0))=k(3f(1))=3(kf)(1)$, so scalar multiplication doesn't affect your property. Similarly, the other condition additivity doesn't (prove it for yourself), and so your set is infact a subspace. The second one,for similar reasons, should be a subspace as well (put $kf$ in place of $f$, the equality doesn't change, and additivity follows similarly).
Comment: If $f$ is in your first space, do we have that $kf$ is in that space? What about $f+g$, where $g$ is another element?
Answer: Here is an abstract nonsense proof. Think about it!
If $V$ is some  vector space  of functions $f:\>F\to F$, where $F$ is a field, then for any $a\in F$ the evaluation map
$$\delta_a:\quad V\to F,\qquad f\mapsto f(a)$$
is a linear functional on $V$ (hence an element of the dual space $V^*$) by the way addition and scaling of functions is defined. It follows that any linear combination $\phi$ of such $\delta_a$s is in $V^*$, hence ${\rm ker}(\phi)$ is a subspace of $V$.


How to prove this statement about polynomials?

Question: Consider the following statement:

Let $F$ be an infinite field and $g$ be a given non-zero polynomial in $F[x_1,\ldots,x_r]$. If $f(x_1,\ldots,x_r)\in F[x_1,\ldots,x_r]$ and $f(a_1,\ldots,a_r) = 0$ for all $a_i$ such that $g(a_1,\ldots,a_r) \neq 0$, then $f$ is the zero polynomial.

I can see e.g. why this is true for $F = \mathbb R$, but the way I see it is extremely unhelpful - I see it through measure-theoretic means. I would like to prove this for any field and through purely algebraic means. Any hints on how to do this?
Answer: Prove/combine the following

If $h(x_1,x_2,\ldots,x_r)$ is not the zero polynomial then there exists $(a_1,a_2,\ldots,a_r)\in F^r$ such that $h(a_1,a_2,\ldots,a_r)\neq0$ (infinitude of $F$ is needed to prove this).
The ring $F[x_1,x_2,\ldots,x_r]$ has no zero divisors.
Apply item 1 to the product $h=fg$, and then...
Comment: Aha! $fg\equiv 0$.. really nice!
Answer: Let me elaborate a bit on some of the ideas implicit in Jyrki Lahtonen's very slick answer.  In particular, I will give a concrete sense in which the vanishing set of a polynomial is "small", analogous to how the vanishing set of a polynomial over $\mathbb{R}$ has measure zero (in fact, this argument can be used to prove that measure-theoretic fact).
Specifically, we define by induction on $r$ what it means for a subset $V\subseteq F^r$ to be "small".  For $r=0$, we say $V\subseteq F^0$ is small if it is empty.  By induction, we then say that $V\subseteq F^r$ is small iff for all but finitely many $a\in F$, $\{b\in F^{r-1}:(b,a)\in V\}$ is a small subset of $F^{r-1}$.  So for instance, a subset of $F^1$ is small iff it is finite, and a subset of $F^2$ is small iff all but finitely many of its vertical fibers are finite.
Now I claim that if $f\in F[x_1,\dots,x_r]$ is any nonzero polynomial, then the set $V=\{v\in F^r:f(v)=0\}$ is a small subset of $F^r$.  We prove this by induction on $r$; the case $r=0$ is trivial.  Supposing $r>0$, think of $f$ as a polynomial in $x_1,\dots,x_{r-1}$ with coefficients in $F[x_r]$.  Since $f\neq 0$, at least one of these coefficients in $F[x_r]$ is nonzero.  There are thus only finitely many values which can be substituted for $x_r$ which make all the coefficients $0$.  Thus for all but finitely many $a\in F$, the polynomial $f_a(x_1,\dots,x_{r-1})=f(x_1,\dots,x_{r-1},a)$ is nonzero.  By induction, the set $\{b\in F^{r-1}:(b,a)\in V\}=\{b\in F^{r-1}:f_a(b)=0\}$ is small for all but finitely many $a$.  Thus $V$ is small.
Now finally note that the union of two small sets is small (easy by induction on $r$) and that if $F$ is infinite, then $F^r$ is not small as a subset of itself (again, easy by induction on $r$).  Thus if $f$ and $g$ are nonzero polynomials, the union of their vanishing sets is small and in particular is not all of $F^r$.  (Alternatively, as in the other answer, the union of their vanishing sets is the vanishing set of $fg$ and hence is small.)
This approach generalizes to give some stronger results than the simple, more algebraic approach in Jyrki Lahtonen's answer.  For instance, you could replace "finite" by "countable" to get another notion of "small" which is closed under countable unions.  This then shows that if $F$ is uncountable, no countable union of zero sets of polynomials can cover $F^r$.


Fancy plotting tools/libraries in python

Question: I wanted to know if there are any other recent plotting libraries in python other than matplotlib which good visual aesthetics to publish in webpages.
I know pyplot is good at this but, I want to use an offline library not an api.
This question might be a duplicate of this, but it was asked long back in 2008, just wanted to ask if there are any recent ones
Answer: I am not sure but this should help
1.https://github.com/yhat/ggplot
2.https://github.com/Kozea/pygal


lagrange multiplier slope

Question: I was reading the link given in the thread's last comment. I understood initial part. I understand that in case of the hill, if we take any point on that hill, the gradient of the original function will always point towards the peak of the mountain. But I am a bit confused about the gradient of the constraint. Why would gradient of the constraint at constraint's topmost point will point in the direction of hill's peak? I thought that it will be 0 as we have reached the topmost point of the constraint. any explanation?
Answer: You are moving on a level set of the constraint -- $g(x) = 0$ -- and since the gradient of $g$ is always perpendicular to its level sets, $\nabla g$ is always perpendicular to $g(x)=0$.
When you are at the constrained maximum of $f$, $\nabla f$ is also perpendicular to $g(x)$. Therefore $\nabla f$ and $\nabla g$ are parallel, i.e. $\nabla f = \lambda \nabla g$ and since $\nabla f$ points uphill, so does $\nabla g$. But that is a consequence of the fact that the two gradients are parallel, and only true at the top of the constrained hill.
EDIT: No, the gradient is never zero. (In fact, the method of Lagrange multipliers requires zero to be a regular value of $g$, i.e., $\nabla g \neq 0$ whenever $g=0$.)
Here's a concrete example:
$$\max\, x \quad \textrm{s.t.}\quad x^2+y^2=1$$
In other words, you are trying to maximize $x$ over the unit circle. This has obvious solution $x=1, y=0$.
Now the constraint function is $g(x,y) = x^2+y^2-1.$ The gradient is
$$\nabla g = (2x, 2y).$$
At the top of the constrained hill, $(1,0)$, this gradient points further uphill -- in the direction $(2,0)$ -- but is nowhere near zero. In fact it is easy to check that when $g=0$, the gradient of $g$ is never zero:
$$\|\nabla g\| = \sqrt{4x^2+4y^2} = 2\sqrt{g+1} = 2.$$
Comment: let me repeat my question...why at the constraint's maximum, the gradient is not zero? Don't we get slope = zero at the maximum of function? If possible provide me actual function for the hill and the constraint in the question and then we can compare gradients. That will help me understand...thanks
Comment: In case of the constraint, I think that gradient with respect to x1=0 as we are perpendicular to x1 axis and gradient with respect to x2 will change as we travel on the constraint. At the top of the constraint it will be zero
Comment: It is making sense now. But a bit unclear still...I agree that gradient will never be zero. Earlier I was considering the constraint a 2D curve and that's why I felt that gradient will be 0 at the top. But now I understand that in 3D, gradient won't be zero. My confusion is: at the topmost point of the constraint, our gradient will point right along Z direction, am I correct? While on the hill at f=a2 curve, gradient will point to the tip of the mountain. The gradient will not be exactly parallel to Z axis, is that correct? Then the both gradients are not parallel ?
Comment: In my example, both $f$ and $g$ are functions over the $xy$ plane, and so the gradients of both of these are in the $xy$ plane -- $\nabla f$ points in the direction that $f$ increases fastest, but doesn't itself have any $z$ component.
Comment: i am not talking about your example. I am talking about example given in the link. I understand that your example is in xy plane only
Comment: There is no difference between my example and the link's. The example in the link is a "cartoon": it shows the gradient as being tangent to the hill, but this is not mathematically the case. The gradient lies entirely in the $x_1, x_2$ plane.
Comment: More specifically, $\nabla f$ has only two components: $\frac{\partial f}{\partial x_1}$ and $\frac{\partial f}{\partial x_2}$. This is a vector in $\mathbb{R}^2$.
Comment: AHA, thanks friend it is making sense now. In your example can I say this: As mentioned in the attached link, let's say I started walking from point (1,0) towards circle's top, and when I come at point (1/squareroot(2), 1/squareroot(2)) and compare gradient at that point with gradient of objective then I get gradient at that point = (squareroot(2),squareroot(2)), while gradient of objective is (1,0) therefore we need to go further upwards on the circle? At the point (0,1), gradient of the constraint and the objective are the same..
Comment: Yep, that's it.
Comment: I solved your example using Lagrange multiplier. At the solution (1,0) as we discussed earlier gradient of the constraint and the gradient of objective are the same, I also got lambda=0.5, what does that lambda signify? Any good link to understand "magnitude of gradient"?

lagrange multiplier slope


Efficient way to find an available GCP VM configuration

Question: I have a VM in the us-west region. I generally want n1 machines with 2-16 CPUs and 1 GPU. Within this, I don't particularly care about the specifics (GPU type, exact number of CPUs/amount of RAM, etc.) I frequently struggle to find an available machine type (when I try to start the machine, I get an error informing me that requested configuration is not available).
Currently, my approach is to randomly try different configurations via the GUI on console.cloud.google.com until one works. This is time-consuming (can take 30 minutes or more) and frustrating. Is there a better way? I am considering writing a bash script that will use the Cloud SDK to systematically try different configurations until one works, but this seems hacky, and I wonder if there is a more elegant solution. The ideal solution would allow me to start a machine with just one or a few actions. I don't mind waiting a while, it would just be nice to be able to have an automated way to find an available machine, rather than manually going through until I chance upon something that's available.
Answer: As of now, there is no easy way to check the available resources on every location. As the resources were used by different people or customers. One way to ensure availability of resources is creating a reservation.
Answer: I wrote the below code for automatically trying to start a VM with different configurations. I'm very open to other possible solutions to this, but posting here in case it's useful for someone else.
'''
Utils for dealing with Google Cloud Platform (GCP)
Source of below code:
https://cloud.google.com/compute/docs/instances/changing-machine-type-of-stopped-instance#python
'''

from __future__ import annotations
from itertools import product
import sys
from typing import Any
from time import sleep
from tqdm import tqdm

from google.api_core.extended_operation import ExtendedOperation
from google.api_core.exceptions import ServiceUnavailable, BadRequest
from google.cloud import compute_v1

def wait_for_extended_operation(
    operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300
) -> Any:
    """
    Waits for the extended (long-running) operation to complete.

    If the operation is successful, it will return its result.
    If the operation ends with an error, an exception will be raised.
    If there were any warnings during the execution of the operation
    they will be printed to sys.stderr.

    Args:
        operation: a long-running operation you want to wait on.
        verbose_name: (optional) a more verbose name of the operation,
            used only during error and warning reporting.
        timeout: how long (in seconds) to wait for operation to finish.
            If None, wait indefinitely.

    Returns:
        Whatever the operation.result() returns.

    Raises:
        This method will raise the exception received from `operation.exception()`
        or RuntimeError if there is no exception set, but there is an `error_code`
        set for the `operation`.

        In case of an operation taking longer than `timeout` seconds to complete,
        a `concurrent.futures.TimeoutError` will be raised.
    """
    result = operation.result(timeout=timeout)

    if operation.error_code:
        print(
            f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
            file=sys.stderr,
            flush=True,
        )
        print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
        raise operation.exception() or RuntimeError(operation.error_message)

    if operation.warnings:
        print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
        for warning in operation.warnings:
            print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)

    return result

def change_machine_type_and_start(
    project_id: str, zone: str, instance_name: str, new_machine_type: str,
    num_gpus: int, gpu_type: str
) -> None:
    """
    Changes the machine type of VM. The VM needs to be in the 'TERMINATED' state for this operation to be successful.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        zone: name of the zone your instance belongs to.
        instance_name: name of the VM you want to modify.
        new_machine_type: the new machine type you want to use for the VM.
            For example: `e2-standard-8`, `e2-custom-4-2048` or `m1-ultramem-40`
            More about machine types: https://cloud.google.com/compute/docs/machine-resource
    """
    client = compute_v1.InstancesClient()
    instance = client.get(project=project_id, zone=zone, instance=instance_name)

    if instance.status != compute_v1.Instance.Status.TERMINATED.name:
        raise RuntimeError(
            f"Only machines in TERMINATED state can have their machine type changed. "
            f"{instance.name} is in {instance.status}({instance.status_message}) state."
        )

    machine_type = compute_v1.InstancesSetMachineTypeRequest()
    machine_type.machine_type = (
        f"projects/{project_id}/zones/{zone}/machineTypes/{new_machine_type}"
    )

    operation = client.set_machine_type(
        project=project_id,
        zone=zone,
        instance=instance_name,
        instances_set_machine_type_request_resource=machine_type,
    )

    wait_for_extended_operation(operation, "changing machine type")

    # this section from here:
    # https://cloud.google.com/python/docs/reference/compute/latest/google.cloud.compute_v1.services.instances.InstancesClient
    if num_gpus > 0:
        accelerator_config = compute_v1.AcceleratorConfig()
        accelerator_config.accelerator_count = num_gpus
        accelerator_config.accelerator_type = (
            f'projects/{project_id}/zones/{zone}/acceleratorTypes/{gpu_type}'
        )
        accelerator_request = compute_v1.InstancesSetMachineResourcesRequest()
        accelerator_request.guest_accelerators = [accelerator_config]
        # accelerator_request = {
        #     'accelerator_count': num_gpus,
        #     'accelerator_type': f'projects/{project_id}/zones/{zone}/acceleratorTypes/{gpu_type}'
        # }
        accelerators = compute_v1.SetMachineResourcesInstanceRequest(
            project=project_id,
            zone=zone,
            instance=instance_name
        )
        accelerators.instances_set_machine_resources_request_resource = accelerator_request

        operation = client.set_machine_resources(request=accelerators)
        wait_for_extended_operation(operation, 'changing gpus')

    # try to start the VM
    operation = client.start(
        project=project_id, zone=zone, instance=instance_name
    )
    wait_for_extended_operation(operation, 'instance starting')

def change_machine_type_and_start_loop(
    project_id: str, zone: str, instance_name: str, new_machine_types: list,
    gpu_nums: list, gpu_types: list
):
    running = False
    i = 1
    while not running:
        pbar = tqdm()
        combs = product(new_machine_types, gpu_types, gpu_nums)
        for new_machine_type, gpu_type, num_gpus in combs:
            machine_desc = f'{new_machine_type} with {num_gpus} {gpu_type}'
            try:
                pbar.set_description(f'Trying {machine_desc}... (attempt {i})')
                # print(f'Trying {machine_desc}... (attempt {i})')
                change_machine_type_and_start(
                    project_id, zone, instance_name, new_machine_type, num_gpus,
                    gpu_type
                )
                running = True
                break
            except (ServiceUnavailable, BadRequest):
                i += 1

    print('Instance started!')
    print(f'Config: {machine_desc}')

The code can be run with something like the follwing:
project_id = 'my-project'
zone = 'us-west1-b'
instance_name = 'my-vm'
new_machine_types = [f'n1-standard-{n}' for n in [8, 16, 32]]
num_gpus = [1]
gpu_types = [f'nvidia-tesla-{g}' for g in ['t4', 'v100','p100']]

change_machine_type_and_start_loop(
    project_id, zone, instance_name, new_machine_types, num_gpus, gpu_types
)


How to compare image similarity with position, rotation & scale invariant features?

Question: I already put this question in stackoverflow.com. But still didn't get an answer. I want to compare similarity between below images. Acording to my requirements I want to identify all of these images as similar, since it has use the same color, same clip art. The only difference in these images are rotation ,scale and the placement of the clip art. Since all 3 t-shirts has used the same color and clip art I want to identify all 3 images as similar. I tried out the method described in hackerfactor.com. But it doesn't give me correct result acording to my requirements. How to identify all these images as similar? Thanks in advance.
Comment: If to speak about general statistical techniques rather than specialized image-analytical algorithms then _procrustes analysis_ can show that the three clipart objects (extracted from the t-shirts) are identical; can compare them, in general. The main problem with procrustes is that it needs to know beforehand which point on one object corresponds to the given point of another object. For example, when you digitize (invent X-Y coordinates) of the three faces, the beak end "landmark" must be point `i` in all the three datasets;
Comment: (cont.) the cirrus endpoint must be point `j` in all the three datasets, and so on. So, the problem of finding or settint the correspondence of the "same" points must be already solved somehow, this is the main challenge. It could be solved by various approaches, including those engaged in computation of a distance matrix between points within each face.
Answer: Try the SIFT or SURF features. They are scale, rotation and translation invariant. Run k-means on the descriptors and go from feature vectors to integer codes (cluster centers). Then build Bag of Words representation per image, basically counting each cluster codes. It will be beneficial to apply TF-IDF normalization on those counts. The three images should translate into some global representation which among them should be very similar (e.g. cosine similarity).
Comment: Is there any libraries for php to achieve SIFT/SURF features? If so, can you provide me a link?
Comment: `PHPimage` seems to have some abilities but I would recommend you check out Python modules for the matter... CV people code in Python routinely and you will have far more options to choose from.
Answer: Try to use feature fusion technique by fusing colour, texture, and shape features using one of standard feature fusion methods (Early feature fusion or Late feature fusion). Before any fusion process you should normalize the feature values to be in the same scale (0,1). Try also to read the paper in:
http://ajbasweb.com/old/ajbas/2015/106-117.pdf


calculating previous k-average

Question: Given a number flow  $a_1, a_2, ... a_N$, $N$ is a large number for example $N = 1000$, the goal is to calculate the average value of previous $n$. For example, now we are at state $k$, then if $n = 20$ then $Avg(k) = (a_k + a_{k-1} + ... + a_{k-19})/20$, $k \ge 20$.
1) given an equation to calculate this $Avg(k)$
My answer: $Avg(k) = Avg(k-1) + (a_k - a_{k-n})/n$
2)  Calculate $Avg(k)$ without using substraction
My answer: build an array that has the $Avg(1), Avg(2) ... Avg(k)$, update each one for each step, but it has complexity of $O(Nn)$. Do anybody know an $O(N)$ algorithm for this?
Answer: You can keep track of the previous 19 numbers (in a 20 element array), and sum them with the current number, and the replace the oldest number in the array with the current one and move on. To compute the size-$n$ averages for all $N-n$ reasonable slots then takes time $O(Nn)$ as in your solution.
But suppose that instead you keep track of the last 19 numbers, but also keep the sum of the last 1, the next 2, the next 4, the next 8, ... up to the largest power of 2 less than N. You move forward, and now you have the sum of the last 2, the next 2, the next 4, ...which you can update by keeping the last 2, but replaceing the next 2 with the sum of your first two elements, so that you have
last 2, next 4, next 4, next 8, ...
and you can cascade this. And you can tack on the last 1 to the first of the list. In log n time, you've updated this list. In log n time, you can compute the sum of more than half of the previous elements....but computing the sum for the remaining few still takes time O(n/2). But what if you kept a SECOND logarithmic list to help you do that bunch fast as well? Then you'd need to do at most $c * n/4$ work at the end. And so on....
In this manner, you keep $\log n$ lists, each of size $\log n$, and compute the sum in time that looks like (I think) $\log^2 n$, so your overall runtime ends up being $O(N \log^2 n)$. That's not $O(N)$, but it's a good deal better than $O(Nn)$.
Answer: The solution for your first question is to keep track of the cumulative sum of the elements in a new array. Now when you are asked to find average of elements between say 'i' and 'j' indices, you can directly get the sum by of elements between these indices by subtracting cumulative sum of 'j' elements from cumulative sum of 'i' elements and divide it by n to get the average. In your case 'i' and 'j' are 'k-n' and 'k' respectively.
Now, time complexity for above algorithm is O(N), because you have to traverse the entire array once and build the cumulative sum array. And the space complexity is O(N), because you have to keep track of cumulative sum of all the elements.
Your second question can be solved by performing subtraction using bitwise operators. Here(https://www.geeksforgeeks.org/subtract-two-numbers-without-using-arithmetic-operators/) is the link to perform subtraction using bitwise operator. Any way time complexity and space complexity will not differ.

calculating previous k-average


Reject Nginx request with no authentication in general

Question: Is it possible to reject all nginx request, which don't send basic authentication data with them?
I have requests like this:
xxx.xxx.xxx.xxx - - [24/Oct/2012:10:32:12 +0200] "POST /path HTTP/1.1" 401 1387 "-" "Apache-HttpClient/4.1.1 (java 1.5)" "-"
xxx.xxx.xxx.xxx - paul [24/Oct/2012:10:32:12 +0200] "POST /path HTTP/1.1" 200 192 "-" "Apache-HttpClient/4.1.1 (java 1.5)" "-"

and i want reject the first one, which don't provide the user. As you can see it already sends the 401 HTTP code, but this code is generated not directly from nginx but from an underlying application server. What I want to do is to directly deny it in nginx. Is there a way to check if a user has been provided?
And is this possible without setting up normal basic authentication directly in nginx?
Answer: Nginx stores the username derived from authentication in $remote_user. You should be able to test that it is not empty, and accordingly perform the desired action (e.g. return a 401 error). Something like:
if ($remote_user = ''){
    return 401;
}

By the looks of it, you are already populating the $remote_user variable (since it shows up in your logs) - as far as I know, you must use Nginx's HTTP Auth Basic Module to populate that variable.
Basic authentication sets the Authorization header. In Nginx, the HTTP request headers are accessible through $http_HEADER, so in this case $http_authorization. If you implement basic authentication outside of Nginx and set that header, you can test that the header is not empty. (Alternatively, depending on how you implement authentication, you could set a custom header that you test).


Doing post-hoc after a not significant interaction in mixed ANOVA

Question: I conducted a mixed design ANOVA with two within-subjects factors: FactorA (2 levels), FactorB (2 levels), and one between-subjects factor: Group (2 levels). My main hypothesis regards the interaction FactorAFactorBGroup.
This is the ANOVA table:
Type III Repeated Measures MANOVA Tests: Pillai test statistic
                                   Df test stat approx F num Df den Df    Pr(>F)
(Intercept)                         1   0.99424   4830.5      1     28 < 2.2e-16 ***
Group                               1   0.01375      0.4      1     28   0.53715
FactorA                             1   0.46649     24.5      1     28 3.197e-05 ***
Groups:FactorA                      1   0.00685      0.2      1     28   0.66367
FactorB                             1   0.14451      4.7      1     28   0.03825 *
Group:FactorB                       1   0.15108      5.0      1     28   0.03378 *
FactorA:FactorB                     1   0.09930      3.1      1     28   0.08985 .
Group:FactorA:FactorB               1   0.02737      0.8      1     28   0.38232
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

As you can see, there is a significant GroupFactorB effect, but the interaction of my interest (GroupFactorA*FactorB) is not significant.
However, if I try to conduct a post-hoc analysis on the GroupFactorAFactorB:
my_data$Interaction <- interaction(my_data$FactorA, my_data$FactorB)
my_data$Interaction <- factor(my_data$Interaction)

post_hoc_model <- lme(value~interaction, random=~1|Subject/FactorB/FactorB,data=my_data)
planned_post_hoc <- summary(glht(post_hoc_model,linfct=mcp(interaction=c(
  "Control.Level1.Level1-Control.Level2.Level1==0",
  "Experimental.Level1.Level1-Experimental.Level2.Level1==0",
  "Control.Level1.Level2-Control.Level2.Level2==0",
  "Experimental.Level1.Level2-Experimental.Level2.Level2==0"))),
  test=adjusted("BY"))

I obtain these results:
Linear Hypotheses:
                                                            Estimate Std. Error z value Pr(>|z|)
Control.Level1.Level1-Control.Level2.Level1==0              0.053125   0.019228   2.763   0.0477 *
Experimental.Level1.Level1-Experimental.Level2.Level1==0   -0.018750   0.027192  -0.690   1.0000
Control.Level1.Level2-Control.Level2.Level2==0              0.003125   0.019228   0.163   1.0000
Experimental.Level1.Level2-Experimental.Level2.Level2==0   -0.025000   0.027192  -0.919   1.0000

Specifically, the first comparison is significant, after correcting for False Discovery Rate, and this is coherent with my hypothesis and with the simple inspection of the means.

Probably, the interaction was not significant due to my small sample sizes.
I know that, after a not-significant interaction, one does not have to do post-hoc for that. So I'm wondering how much this methodology is criticable by a referee. I thought to specify that the post-hoc comparisons should be interpretate with caution because the interaction was not significant. Moreover, I found this practice in a lot of researches.
Do you think this way of proceeding is totally wrong?
How can I eventually defend my analysis from a referee critique (maybe by referring to published statistical papers)?
Comment: Ok, fixed. BTW, these typos do not exist in my original script.
Comment: Your lme command as provided has an apparent typo.  Does that typo exist in your original script?  Does correcting it change your result at all?
Comment: do you refer to `Interaction` rather than `interaction`? I fixed it now.
Answer: As a reviewer there would be several things here that would concern me.
Assuming we were looking at the set of possible two-way interactions in your post-hocs (the next rational step in a decomposition from a three-way interaction), then a significant effect for one two-way interaction (but not for the others) would not necessitate a three way interaction per se.  For example, one two-way interaction may have a statistically significant effect size greater than 0 and the others may have effects in the same direction, but not large enough to be greater than 0.  Nevertheless, because all are going in the same direction, then there might not be sufficient evidence to suggest that they are sufficiently different from each other to reject the null hypothesis that they are the same (i.e., not a statistically significant three-way interaction).
That being said, I don't see your post-hocs here as testing the differences between two-way interactions (i.e. differences in the differences).  You seem to be testing a subset of possible main effects (differences manipulating only a single variable while holding the levels of other variables fixed).  For example, none of your comparisons involve both the Experimental and Control groups.
What does your result actually indicate?  I think it indicates a statistically significant difference between those two particular conditions (Control, 1, 1 and Control, 2, 1).
Regardless, you should know that your lack of a three-way interaction here is probably not a power issue.  If it were simply a power issue, then the F ratio for your three way interaction would exceed 1.  As it is, there is less variance in the three-way interaction that would be expected on average if the null hypothesis were true.
Finally, assuming the comparisons you did perform were of interest, then I would expect to see the comparisons done as a priori... a planned post-hoc makes no sense to me.  That being said, I also know some reviewers are very post-hoc correction happy.  The most important part here is that I would want to see those results interpreted appropriately (and not alluded to as a three way interaction).
Edit:  Oh, and I should acknowledge that I've seen plenty of people interpret significant results consistent with a desired interaction as being evidence in strong favor of the interaction.  I've even seen this in top tier journals.  That being said, I strongly recommend against it (then again, I have a particular problem with this misbehavior, c.f. https://stats.stackexchange.com/a/4572/196).
Comment: Specifically, my study tests a particular human phenomenon given by the interaction between `FactorA` and `FactorB`. My hypothesis, confirmed by post-hocs, is that the phenomenon is not present in the experimental group. Yes, I have an a priori hypothesis for my research. So, do you mean I should make only corrected comparisons, without ANOVA?
Comment: So what I hear you saying is that according to your hypothesis there should be an interaction effect of Factor A and Factor B for the Control group but not the experimental group?
Comment: Exactly. This is also coherent with the inspection of the individual means. I update also a bar plot inside my question, in order to make this aspect clearer.
Comment: I wouldn't say that your hypothesis is confirmed by post-hocs.  The confirmation you'd want and need to make such a strong statement is going to come from a three-way interaction (which you do not have).
Comment: What you have is some planned comparisons that are consistent with your hypothesis.  It is appropriate to provide those without p-value corrections, but only as very weak evidence.  I would prefer to see an effect size and confidence interval of the three-way interaction... that to me is always an appropriate way to provide results and (within this framework) best reflects the level of uncertainty I should have.
Comment: Ok. I will consider your advices. I also think that it is more correct to provide effect size also for comparisons (in my case I would use Cohen's d). I have not done it because in my field of study I seldom find effect sizes for post hoc p-values.


Solve $\sqrt{1 + \sqrt{1-x^{2}}}\left(\sqrt{(1+x)^{3}} + \sqrt{(1-x)^{3}} \right) = 2 + \sqrt{1-x^{2}} $

Question: Solve $$\sqrt{1 + \sqrt{1-x^{2}}}\left(\sqrt{(1+x)^{3}} + \sqrt{(1-x)^{3}} \right) = 2 + \sqrt{1-x^{2}} $$

My attempt:
Let $A = \sqrt{1+x}, B = \sqrt{1-x}$ and then by squaring the problematic equation we get:
$$(1+AB)(A^{3} + B^{3})^{2} = (AB)^{2} + 4AB + 4 $$
$$ A^{6} + B^{6} + BA^{7}  + A B^{7} = -2 (AB)^{4} - 2(AB)^{3} + (AB)^{2} + 4AB + 4 $$
I also have tried using$A = (1+x), B = (1-x)$ , and some others, but none solves the problem.

I am now trying $A = (1+x)$ and $(1-x) = -(1+x) + 2 = 2 - A$, so:
$$\sqrt{1 + \sqrt{A(2-A)}}\left(\sqrt{(A)^{3}} + \sqrt{(2-A)^{3}} \right) = 2 + \sqrt{A(2-A)} $$
Comment: First try may progress if you notice that $A^2+B^2=2$.
Comment: Note: if you square the equation you need to be careful as you will likely get some additional solutions which will not be solutions to the original. This is because both $X=Y$ and $X=-Y$ square to $X^2=Y^2$
Comment: Where is this problem from?
Comment: Shouldn't it be $A^3 + B^3 $ in the line $(1+AB)(A^{3} - B^{3})^{2} = (AB)^{2} + 4AB + 4$
Comment: @IshanDeo thanks, right
Comment: @TobyMak It is from a winter olympiad camp problems
Answer: First, this is stated as an equation to solve (for $x$) rather than an identity to be shown.
So with $a=\sqrt {1+x}$ and $b=\sqrt {1-x}$ we have $$a^2+b^2=2$$ and $$(a+b)^2=a^2+2ab+b^2=2(1+ab)$$ and $$a^3+b^3=(a+b)(a^2-ab+b^2)=(a+b)(2-ab)$$
Then $$\sqrt {1+ab}\cdot (a^3+b^3)=\frac {\sqrt 2}2(a+b)(a+b)(2-ab)=\sqrt 2(1+ab)(2-ab)$$
If we then put $c=ab$ the equation to solve is then $$\sqrt 2(1+c)(2-c)=2+c$$ which is a straightforward quadratic in $c$. Then solve for $x$ by noting $c^2=1-x^2$
Comment: It would be $2+c$, not $1+c$ as $a^2+b^2=2$.
Answer: Great substitution technique. Notice that $\sf{A=\sqrt{1+x}}$ and $\sf{B=\sqrt{1-x}}$ leads to $$\sf{(A^3+B^3)\sqrt{1+AB}}=2+AB$$ and since $\sf{A^3+B^3=(A+B)(A^2-AB+B^2)}$ and $\sf{A^2+B^2=2\implies (A+B)^2=2(1+AB)}$, we get $$\sf{(A+B)\sqrt{1+AB}=\frac{2+AB}{2-AB}}\implies (1+AB)\sqrt2=1+\frac{2AB}{2-AB}$$ which can be solved for $\sf{AB}$, and thus $\sf{x}$.
Answer: Substituting $$a=\sqrt{1+x},b=\sqrt{1-x}$$ and using that $$a^2+b^2=2$$ and $$a^3+b^3=(a+b)(a^2+b^2-ab)$$ we get$$\sqrt{1+ab}(a+b)(2-ab)=2+ab$$
And we get by squaring $$(1+ab)(2+2ab)(2-ab)^2=(2+ab)^2$$
and with $$u=ab$$ we get
$$2(1+u)^3(2-u)^2=(2+u)^2$$
The solutions are $$\left\{\left\{x\to
   -\sqrt{-\frac{7}{4}+\frac{3}{\sqrt{2}}-\frac{1}{4}
   \sqrt{97-68 \sqrt{2}}}\right\},\left\{x\to
   \sqrt{-\frac{7}{4}+\frac{3}{\sqrt{2}}-\frac{1}{4}
   \sqrt{97-68 \sqrt{2}}}\right\}\right\}$$
Comment: I think the RHS should be $2 + ab$
Comment: Sorry this was a typo.
Comment: But $$a^3+b^3=(a+b)(a^2+b^2-ab)=(a+b)(2-ab)$$
Comment: Where is the factor in your line?
Comment: $$(a^3+b^3)=(a+b)(a^2+b^2-ab)=(a+b)(2-ab)$$ when we use that $$a^2+b^2=2$$
Comment: This answer seems to be correct based on numeric computations: http://www.wolframalpha.com/input/?i=%5Csqrt%7B1%20%2B%20%5Csqrt%7B1-x%5E%7B2%7D%7D%7D%5Cleft%28%5Csqrt%7B%281%2Bx%29%5E%7B3%7D%7D%20%2B%20%5Csqrt%7B%281-x%29%5E%7B3%7D%7D%20%5Cright%29%20-2%20-%20%5Csqrt%7B1-x%5E%7B2%7D%7D%20for%20x%3D%20-%5Csqrt%7B-%5Cfrac%7B7%7D%7B4%7D%2B%5Cfrac%7B3%7D%7B%5Csqrt%7B2%7D%7D-%5Cfrac%7B1%7D%7B4%7D%20%20%20%20%5Csqrt%7B97-68%20%5Csqrt%7B2%7D%7D%7D
Answer: As $-1\le x\le1$
WLOG $x=\cos2t,0\le2t\le\pi,\sin2t=\sqrt{1-x^2}$
$$\implies\sqrt{1+\sin2t}[(2\cos^2t)^{3/2}+(2\sin^2t)^{3/2}]=2+\sin2t$$
As $\sin t,\cos t\ge0$ and $(\sin t+\cos t)^2=1+\sin2t$
$$2\sqrt2(\cos t+\sin t)(\cos^3t+\sin^3t)=2+\sin2t$$
$$\sqrt2(1+\sin2t)(2-\sin2t)=2+\sin2t$$ which is on rearrangement,  a Quadratic Equation in $\sin2t(\ge0)$
as $\cos^3t+\sin^3t=(\cos t+\sin t)(\cos^2t+\sin^2t-\sin t\cos t)=\dfrac{(\cos t+\sin t)(2-\sin2t)}2$
Comment: Nice point of view.
Comment: @trancelocation,  As $0\le y\le\dfrac\pi2$  $$\sqrt{1+x}=\sqrt2\cos t$$ etc.
Comment: @trancelocation, $$(\sqrt2\cos t)^3=2\sqrt2\cos^3t$$ etc.
Answer: Let us first simplify the expression. Assume $a^2=1-x$ $ $ & $ $ $b^2=1+x$
Please note that $a^2+b^2=2$
Now our equation becomes $\sqrt{1+ab}(a^3+b^3)=2+ab$
$\sqrt{\frac{a^2+b^2+2ab}{2}}(a+b)(a^2+b^2-ab)=2+ab$  which further simplifies to
$\frac{(a+b)^2}{\sqrt{2}}(2-ab)=2+ab$
$\frac{(2+2ab)(2-ab)}{\sqrt{2}}=2+ab$
Find $ab$ and use the equation $a^2b^2=1-x^2$
Hope this will be helpful!
Comment: $(a+b)^2=2+2ab$, not $2+ab$.
Comment: Typo Corrected!
Answer: Note: $1-x^2\ge 0 \Rightarrow -1\le x\le 1$.
Square both sides and denote $1-x^2=t^2, -1\le t\le 1$:
$$\sqrt{1 + \sqrt{1-x^{2}}}\left(\sqrt{(1+x)^{3}} + \sqrt{(1-x)^{3}} \right) = 2 + \sqrt{1-x^{2}} \Rightarrow \\
(1+\sqrt{1-x^2})(2+6x^2+2\sqrt{(1-x^2)^3}=5-x^2+4\sqrt{1-x^2} \Rightarrow \\
(1+t)(2+6(1-t^2)+2t^3)=4+t^2+4t \Rightarrow \\
2t^4-4t^3-7t^2+4t+4=0 \Rightarrow \\
t\approx 0.93, x\approx \pm 0.38.$$
Note: Other roots are rejected as they are outside of domain.
Comment: Something must have gone wrong since $0.77$ does not seem to be close to a solution: http://www.wolframalpha.com/input/?i=%5Csqrt%7B1%20%2B%20%5Csqrt%7B1-x%5E%7B2%7D%7D%7D%5Cleft%28%5Csqrt%7B%281%2Bx%29%5E%7B3%7D%7D%20%2B%20%5Csqrt%7B%281-x%29%5E%7B3%7D%7D%20%5Cright%29%20-2%20-%20%5Csqrt%7B1-x%5E%7B2%7D%7D%20for%20x%3D0.77
Comment: Right, $\pm 0.77$ don't fit, so they are gone now.
Comment: $+0.38$ should also be a solution
Comment: Right again, since it is within the doman $x\in [-1,1]$. Thank you
Answer: Let $p = A+B = \sqrt{1+x}+\sqrt{1-x}$. We have
$$ p^2 = 2+2\sqrt{1-x^2}$$
$$ p^3 = \sqrt{(1-x)^3}+\sqrt{(1+x)^3} + 3p\sqrt{1-x^2}$$
so
$$ 1+ \sqrt{1-x^2} = \frac12 p^2$$
$$ \sqrt{(1-x)^3}+\sqrt{(1+x)^3} = -\frac12 p^3 +3p$$
and we get
$$ \frac{p}{\sqrt{2}} (-\frac12 p^3 +3p) = 1 + \frac12 p^2$$
$$ - \frac{1}{2\sqrt{2}}p^4 +\frac{3\sqrt{2}-1}{2} p^2 - 1 = 0$$
which can be solved for $p$.


Redirect using .htaccess to remove a part of filename in Apache

Question: I know there are plenty of questions how to replace part of URL with something else (or nothing) using .htaccess but I really suck both in regular expressions and in .htaccess.
How can I to convert URL like /v0/A8B9DEBF512F929144257AEE00262C16/$File/IMG_8819.jpg to /v0/A8B9DEBF512F929144257AEE00262C16/IMG_8819.jpg? (Without $File/, instead of A8B9DEBF512F929144257AEE00262C16 and IMG_8819.jpg there may be any combination of digits and letters).
Comment: Some sort of `htacess` regex like this: `[^\/]+\/([^\/]+)$` could help .. _https://regex101.com/r/aU7gD2/1_
Comment: @WashingtonGuedes, this doesn't seem sufficient. Maybe there should be more?
Answer: You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On

RewriteRule ^(v0/[^/]+)/\$File/(.*)$ /$1/$2 [L,NC,R=301]

Comment: This code either doesn't work or I'm doing something wrong. Is there something else to do besides just adding this code to the file.
Comment: `http://126.96.36.199:9090/v0/A8B9DEBF512F929144257AEE00262C16/$File/IMG_8819.jpg` this one. There is something strange in filename in the string. Nevermind that, some stackoverflow bug, apparently.
Comment: Is your .htaccess located in `/v0/` directory? Can you show your full .htaccess in question?
Comment: http://pastebin.com/nSQxKT4c

Here's the file. No, it's located one level above, like /v0/../
Comment: So this is a level above `/v0/`? Does `/v0/` also have a .htaccess by any chance? This rule worked fine on my Apache.
Comment: This directory doesn't have its own .htaccess, but I believe it is possible to give it one. Should I? If so, what else must it contain?
Comment: No if `/v0/.htaccess` doesn't exist then don't create it, I've tested this rule on my Apache it redirected `http://localhost/v0/A8B9DEBF512F929144257AEE00262C16/$File/IMG_8819.jpg` to `http://localhost/v0/A8B9DEBF512F929144257AEE00262C16/IMG_8819.jpg`
Comment: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93138/discussion-between-moorindal-and-anubhava).


Using cross correlation to infer dependence, can it be done?

Question: I have a very particular question, I have seen a similar one here, but my knowledge is too limited to make use of it. I will try to explain myself as clearly as possible... Wish me luck!
I have a sequence of (a priori) probabilities of a binary variable, we could say $X_i \sim Ber(p_i)$, i.e. $P(X_i = 1) = 1 - P(X_i = 0)=p_i$. I know that $X_i$ are not independent, but I don't know exactly $P(X_i|X_{j\neq i})$ nor $P(X_i|X_{j_1\neq i},...,X_{j_n\neq i})$ (I don't know anything about their dependence), and I want to know $$P(\sum_{j=-K}^{j=K} X_{i+j} =0)$$
What kind of knowledge/hypothesis do you think I need in order to approximate this more efficiently? Assuming that they are independent does not work well enough. What would you do if you found a similar situation? I am not an expert at all in this matters, and the more I learn about statistics the less I know!
For example, I can see that the probabilities form small "triangle" shapes, so maybe something like $P(X_i = 1 | X_{i-1} = 1, X_{i-2} = 0) = P(X_i = 0 | X_{i-1} = 0, X_{i-2} = 1) $ can help? If so, is there any way to use this? As I said, I have no idea...
Thank you very much for your help!
edit: I think the title is not very good, but I don't know how to explain it better... One more question, can I say $Y_i = X_i - X_{i-1}$ and try to see $P(X_i|Y_{i-1})$?
Comment: You can give a (perhaps too) rough estimate by noting that the event you are after is the event where all the Bernoulli RVs are zero. So consider the smallest $p_i$. Also, if you assume the markov property, you can try to estimate how many "effectively independent samples" you have. One reference for  this kind of calculations may be found at http://www.stat.unc.edu/faculty/cji/Sokal.pdf (at the bottom of page 7).
Answer: O.K., I think I found a way of doing it with the correct assumptions. Although it is only useful for my particular problem, maybe somebody can tell me if I am being too "sloppy", correct me or maybe my approach might be useful to somebody in the future.
First of all, I had not realized that the "triangles" account for "long  independent events". This means that $P(X_i = 0 | \sum_{j\neq i, |j-i| < D} X_j = 1) = 1$ in my notation ($X_i$ represents that an event starts in the moment $i$). What I started doing was a smoothing of $2D$ of $p_i$, so I took the average (can I do this?) in windows. This gave me a way of seeing how many events are there in a sequence.

The orange line is the original $p_i$ sequence, the black one is the smoothed. After this, in a window of length $2K$, I count how many peaks are there (how many possible events), and the probability of each event is the sum of probabilities from the beginning until the end of the "hill", although, as can be seen in the picture, sometimes they can overlap, but I have no idea of how can I take that into account. Then, the probability of no event happening in the window of length $2K$ is the product of probabilities of no "long event" happening in this window.
Do you think it is a good answer? Do you have any comment/suggestion? Thank you very much.


How to explain the result of this polynomial equation?

Question: Lets say that I predicted plant productivity (logy) by precipitation (logx1) and moisture content of soil (logx2). my original data gives me the best result only after taking logarithm on both side of variables.
mod <- lm(logy ~ poly(logx1*logx2, 2, raw=TRUE)

then I got following coefficients from the model:
intercept=6.773;
logx1*logx2 poly(1) = 2.511;
logx1*logx2 poly(2) = 0.458

With regard to equation, I have 2 questions:
1. How I can explain the result of this equation
2. How to find the value when precipitation is 0.
Thanks in advance!
Comment: How much data do you have? You'd mostly likely do better to have logx1, logx2, their product, their squares, & the product of their squares. (Ie, `lm(logy ~ logx1 + logx2 + logx1^2 + logx2^2 + logx1*logx2 + logx1^2*logx2^2)`.) This is a highly constrained model that is likely to not fit as well. Is there a very well grounded physical theory to justify this version?
Comment: @ James Phillips. I am sorry how to add data here?
Comment: Would you please post - or link to - the original non-log data?
Comment: You can paste it into the question or link to it.
Comment: I have updated above
Comment: `poly()` produces orthogonal polynomials by default, so the coefficients don't map onto the original variables. You should manually enter each term you want rather than using `poly()` to expand the polynomials.
Answer: As you can see from the 3D scatterplot, the posted data seems rather noisy with both a "vertical" section and a "flat" section. The best I could find from an equation search was "Y = a * pow(X1, b) + c * pow(X2, d) + Offset" with fitted parameters a = 2.6908392707633102E+03, b = 3.4211996168376331E+00, c = 2.8379254883958050E+05, d = 2.8577605809712245E-04, and Offset = -2.8395222685511003E+05 giving RMSE = 154.6 and R-squared = 0.38
Comment: @ James Phillips. Thank you so much. yes, data has a lot of noice. That is why I tried to use logarithmic transformation. I don't know that was right way to handle this kind of problem. Did you use specific software to do this or R? Actually all this thing are exactly what I want. to obtain RMSE & MR (%) etc. Can you tell me how to run this model in R?
Comment: thanks again for your kind help and sharing this very usefull website as well.
Comment: My zunzun.com open source Python online curve fitting and surface fitting web site has a 3D "function finder" that I used for the equation search. I pasted your data into the online interface for this equation at http://zunzun.com/Equation/3/Power/Power%20D%20With%20Offset/ and then hit the "Submit" button.


How to access last modified date/time for file on exFAT with 10 ms resolution?

Question: From my research, exFAT has a 2-second resolution for the last modified date/time of files. Also, there is another field for the file storing the offset to the last modified date/time with a 10-millisecond resolution. Am I correct about this?
When I display file properties in Windows Explorer, or use QFileInfo::lastModified() (from the Qt framework) to analyze the file, both methods only display the last modified date/time down to a 2-second resolution.
If I am correct about the above, how do I access the more precise timestamp using Qt, or the Win32 API, or Windows Explorer?
Comment: I just created 12 new files on the exFAT drive and all of them had even second last modified timestamps. Also when I use QFileInfo::lastmodified and convert it to msecs it's always divisible by 2000 msecs.
Comment: "*Am I correct about this?*" - not quite. The 2nd field you refer to is not an **offset** to a separate date/time timestamp. It is just additional milliseconds that are added to the 2-second timestamp to give it more precision. Together, these 2 fields form a single date/time timestamp, expressed in local time. There is a **3rd** field which *IS* an offset, containing a UTC offset, which is added to the local timestamp to create a date/time timestamp in UTC.
Comment: Thanks Remy. Any idea how to access this more precise timestamp using Qt or the Win API?
Comment: And how come Windows Explorer doesn't show it? In my testing all the timestamps were even seconds.
Comment: "*how come Windows Explorer doesn't show [this more precise timestamp]?*" - did you verify that the 10ms fields are not simply 0 to begin with?
Comment: That is not what I asked.  Did you verify *in the exFAT metadata itself* whether the 10ms increments are zero vs non-zero? Not what the OS or Qt is giving you, but what exFAT itself is reporting. I'm sure you can find a tool that is able to read and display the raw exFAT data directly (I'm not aware of any Win32 API to get this info without reading the filesystem manually).
Comment: I *suspect* (but can't find) that `QFileInfo` is likely using either `GetFileTimes()` or `GetFileAttributesEx()` to get the timestamp, so it would be at the mercy of whatever Windows decides to report. And it sounds like Windows is not handling the full timestamp precision that is possible with exFAT.
Comment: I just verified with custom GetFileTime() code that the Win API is reading the modified timestamp to 2 seconds resolution for exFAT.
Comment: Nirsoft's Property System View reports 2 second resolution as well.
Comment: Broke out my Ubuntu virtual machine and sure enough it has 10 ms resolution. Windows schmindos
Comment: Last evolution of this thread... I used wmic in Windows to get the modified timestamp of 12 files created in Ubuntu and 12 in Windows. The Ubuntu ones were resolution down to 10 ms while the Windows ones were resolution down to 2 seconds.
Answer: According to FAT32 and exFAT, which file timestamps are supported?:

Timestamp granularity of 10 ms for Create and Modified times (down from 2 s of FAT, but not as fine as NTFS's 100 ns)
Timestamp granularity for Last Access time to double seconds (FAT had date only).

Do you HAVE to use lastModified? Does Qt expose two other fields?
Note: that "double seconds" granularity matches your observed "divisible by 2000 msecs"
Comment: What two other fields could Qt expose?
Comment: @riverofwind according to https://doc.qt.io/qt-5/qfileinfo.html, `lastRead` and `birthTime`
Comment: OK I see what you're talking about. I'm making a file sync utility so the modified date is the field to compare files with.
Comment: @riverofwind then you have to figure out what Qt maps the `Modified` time to. Judging by your post, it looks like `QFileInfo::lastModified()` is exFAT's `Last Access` time (based on 2 seconds granularity). Do other Qt's timestamp provide 10ms granularity?
Comment: the NTFS modified timestamp granularity is to the ms
Comment: @riverofwind but your question was about exFAT, right?
Comment: I thought you were asking for other timestamps for other filesystems, not other timestamps specifically for exFAT
Answer: Here's the response from a Microsoft rep for what it's worth.


Mathematical Formulas for Game Battle Calculations

Question: I am from a programming background and trying to write a game for fun.
I am trying to write a battle calculator and which ever way I think about it I seem to run into trouble.
Basically the scenario is this:
Both the attacking force and the defending force have, lets say soldiers.  Each side can have, but not always, lets say mercenaries to help.  Each side holds an arsenal of 10 possible weapons, each of which give a different hit force.  Each side also has a small random force to bring in an element of luck.
After the battle I need to work out how many of the total kills were made by both player and mercenaries (if they join in) on both sides.
My simple approach to this so far is to work out the best possible weapons each side can have.  That might be 50% weapon 7, 20% weapon 6 and the rest weapon 5 for example.  I then calculate the total hit force this give and divide by the total number of soldier to give an average.  I add any other hit mods the player qualifies for and use that to work out the kill amount.  So if the total hit force is 3.6 I divide 3.6 by 100 then use number of soldiers * my hit force to give number of kills.  I am sure this isn't the best way to do it though.
I am not expecting anyone to do this for me however handy that might be. On the face of it it seems a relatively simple calculation and maybe it is for someone who totally understands maths. I am at a loss to know what kind of maths I should be looking at.  Can anyone make a suggestion as to how to approach this?
If anyone know of a website I can go to pay for this to be done for me I would be interested.
Comment: Let me ask a a clarifying question: Are you looking for pseudocode to implement your system, a better system, modifications to what you currently have, or consultation with the mathematics you need with the current idea? The previous comment of mine gives a link to different approach. Please clarify what you are looking for.
Comment: You need to clarify the type of battle you have. In the simplest scenario, with just two opposing soldiers, what do you want the effect of one side having a higher hit force to be?
Comment: You need to clarify the type of battle you have. In the simplest scenario, with just two opposing soldiers, what do you want the effect of one side having a higher hit force to be? Options include (1) the soldier with higher hit force always wins (2) the soldier with higher hit force has a higher probability of winning.
Comment: @tomi the side with the higher hit force always wins unless... it is only marginally higher say 5% then its just higher probability.
Comment: @TommiBrander to be honest I am open to suggestions from someone that knows better than I do about the best way to implement a battle calc.  This game is a learning exercise before I attempt a more complex game.
Comment: Ok. You then need to define what is meant by a win. Is a total kill of the opposition? Is there a body count on the winning side?
Comment: Ok. You then need to define what is meant by a win. Is a total kill of the opposition? Is there a body count on the winning side? By your definition' two soldiers with hit force 1 weapons could be defeated by one soldier with a hit force 3 weapon. Are they both dead? That would reflect accurately a situation where a soldier with a revolver is against two soldiers with knives. But one soldier with a hit force 3 revolver would be beaten by ten soldiers with hit force 1 knives. Maybe that's realistic, but he would kill many of the ten before being killed himself.
Comment: I think you maybe need to use relative hit force to deal with the basic question of who wins. Then start thinkng about the lethality of the weapons as a separate matter to work out the number of survivors that your winning side has.
Comment: @Fred If you are interested, I can give further consultation. Contact me at email@example.com, with the first and last name as in my user name.
Comment: @TommiBrander I will email you after work. I think you have exactly what I am looking for as long as I can work out how to translate it into c# code! :)
Answer: Suppose $w_1, w_2, \ldots, w_{10}$ are the weapon effectiveness numbers, and $m_j$ are the amount of people equipped with $w_j$.
To calculate the total effectiveness of a force, first calculate the total number of combatants $M$, which with the notation I use is
$$M = m_1 + \ldots + m_{10} = \sum_{j=1}^{10} m_j.$$
According to Lanchester's square law http://en.wikipedia.org/wiki/Lanchester%27s_laws, the effectiveness of a force is proportional to the square of combatants, so we use $M^2$.
We want to involve the weapon technology in a linear manner. The simplest way that comes to mind is to multiply $M^2$ by the weighted average of weapon effectiveness, which would be
$$\frac{1}{M} (m_1 w_1 + \ldots + m_{10}w_{10}) = \frac{1}{M}\sum_{j=1}^{10}m_j w_j,$$
so the total effectiveness $E$ of a force would be
$$E = M^2 \frac{1}{M}\sum_{j=1}^{10}m_j w_j = M\sum_{j=1}^{10}m_j w_j.$$
An alternative, and I think theoretically nicer, way of using weapon effectiveness would be to have
$$E = (\sum_{j=1}^{10} \sqrt{w_j} m_j)^2.$$
There are other fairly similar methods. You would need to be more precise with what you want in order to get more specific answers.
You can add other factors (random factors, morale, etc.) in similar way to weapon effectiveness - just add more multipliers.

You also asked for the ratio of kills between the main force and the mercenaries. To get it, first calculate the effectiveness of the main force (I write it as $E_s$) and the mercenaries $E_m$ as above. Then figure out the total number of kills in whatever way you do; let us write it as $K$.
The number of kills committed by the main force would then be
$$\frac{E_s}{E_s+E_m}K$$
and the number of kills by mercenaries would be
$$\frac{E_m}{E_s+E_m}K.$$


Invalid Operation Exception was unhandled

Question: got this error Items collection must be empty before using ItemsSource.
this error is found in this line:
BuildstreamComboBox.ItemsSource = dz;
the code:
    public void PopulatebuildstreamFromXMLFile()
    {
        if (BuildmachineComboBox.SelectedIndex == 0)
        {
            ds3.Clear();
            ds3.ReadXml(@"C:\GUI\buildermanageremail.xml");

            DataView dy = ds3.Tables["buildstream4"].DefaultView;

            BuildstreamComboBox.ItemsSource = dy; //Sets the collection of items from which to populate
            BuildstreamComboBox.DisplayMemberPath = "value"; //Sets the path within an item to use for display
        }
        if (BuildmachineComboBox.SelectedIndex == 1)
        {
            ds3.Clear();
            ds3.ReadXml(@"C:\GUI\buildermanageremail.xml");

            DataView dz = ds3.Tables["buildstream5"].DefaultView;

            BuildstreamComboBox.ItemsSource = dz; //Sets the collection of items from which to populate
            BuildstreamComboBox.DisplayMemberPath = "value"; //Sets the path within an item to use for display
        }
    }

where ds3 is definded:
DataSet ds3 = new DataSet();
anybody has any idea?
Comment: How does your BuildstreamComboBox look in XAML? Does it have child elements?
Comment: oh yes.. it does have the previous item source. forgot to remove them thanks for the reminder!
Answer: Your question itself contains your answer, you need to clear the items collection prior of assigning it a new items source lke this - BuildstreamComboBox.Items.Clear()


Erro na geração de Relatorio JasperReport + Netbeans net.sf.jasperreports.engine.JRException: java.net.MalformedURLException

Question: Bom dia, estou tentando gerar um relatório fazendo apenas o design no ireport, ai coloco o .jrxml no netbeans e tento compilar por lá, ai na hora de pegar o caminho do relatório ele lança as seguintes exceptions:
 javax.faces.FacesException: #{relatorioControle.visualizaPdf()}: net.sf.jasperreports.engine.JRException: java.net.MalformedURLException
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    at org.springframework.faces.webflow.FlowActionListener.processAction(FlowActionListener.java:71)
    at org.springframework.faces.model.SelectionTrackingActionListener.processAction(SelectionTrackingActionListener.java:64)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.faces.el.EvaluationException: net.sf.jasperreports.engine.JRException: java.net.MalformedURLException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    ... 64 more
Caused by: net.sf.jasperreports.engine.JRException: java.net.MalformedURLException
    at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:305)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:284)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:273)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:247)
    at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:338)
    at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:605)
    at relatorio.RelatorioControle.init(RelatorioControle.java:58)
    at relatorio.RelatorioControle.visualizaPdf(RelatorioControle.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at javax.el.ELUtil.invokeMethod(ELUtil.java:326)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:536)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:269)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 65 more
Caused by: java.net.MalformedURLException
    at java.net.URL.<init>(URL.java:619)
    at java.net.URL.<init>(URL.java:482)
    at java.net.URL.<init>(URL.java:431)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:610)
    at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:189)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:812)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:648)
    at org.apache.commons.digester.Digester.parse(Digester.java:1892)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:297)
    ... 85 more
Caused by: java.lang.NullPointerException
    at java.net.URL.<init>(URL.java:524)
    ... 96 more

A minha chamada ao relatório está da seguinte maneira:
InputStream fonte = RelatorioControle.class.getResourceAsStream("RealtorioMedicamentosEntregues.jrxml");
        JasperReport path = JasperCompileManager.compileReport(fonte);

Já tentei de varias formar, o jrxml esta no mesmo pacote do MB que chama o relatorio, ja coloquei
 "/RealtorioMedicamentosEntregues.jrxml", "../RealtorioMedicamentosEntregues.jrxml", "relatorio/RealtorioMedicamentosEntregues.jrxml", "../relatorio/RealtorioMedicamentosEntregues.jrxml", e nenhuma funcionou.
Obrigado se alguem puder me ajudar.
Comment: Para acessar por este caminho, ja tentou colocar o arquivo na pasta web-inf?
Answer: Consegui da seguinte maneira:
coloquei o relatorio na pasta WEB-INF;
e na chamada do relatorio fiz assim:
FacesContext context = FacesContext.getCurrentInstance();
        String fonte = context.getExternalContext().getRealPath("WEB-INF/RelatorioMedicamentosEntregues.jrxml");

        jasperReport = JasperCompileManager.compileReport(fonte);

Comment: Por favor, se a resposta realmente resolve o problema, marque-a como "certa", clicando ao lado da resposta até ficar com um *check* verde.


Resample doesn't recognize my index as datetime

Question: I am using a datetime.date field as my index and it doesn't seem to agree that it is datetime for some reason.
First I will verity that the field is indeed datetime.date format:
In [1]: df[date][0]

Out [1]: datetime.date(2010, 1, 1)

Now I try to resample by week and plot the mean:
In [2]: df.set_index(date)[var].resample('W', how='mean').plot()

/python2.7/site-packages/pandas/core/generic.pyc in resample(self, rule, how, axis, fill_method, closed, label, convention, kind, loffset, limit, base)
   2878                               fill_method=fill_method,    convention=convention,
   2879                               limit=limit, base=base)
-> 2880         return sampler.resample(self).__finalize__(self)
   2881
   2882     def first(self, offset):

/python2.7/site-packages/pandas/tseries/resample.pyc in resample(self, obj)
    100             return self.obj
    101         else:  # pragma: no cover
--> 102             raise TypeError('Only valid with DatetimeIndex or PeriodIndex')
    103
    104         rs_axis = rs._get_axis(self.axis)

TypeError: Only valid with DatetimeIndex or PeriodIndex

I can't provide sample data because it's proprietary.  Any thoughts on what might be going on here?
Comment: just to check does `df1 = df.set_index(date)  df1[var].resample('W', how='mean').plot()` work?
Comment: Can you try explicitly converting to a datetime index so your code would look like:`df.set_index(pd.DatetimeIndex(date))[var].resample('W', how='mean').plot()`
Answer: I think you want to convert the date column to a pandas time series first.
Try:
df['date'] = pd.to_datetime(df.date)
df.set_index('date')[var]....

I've been told by Pandas dev that datetime objects in pandas indices are inefficient are to be avoided where possible.
Comment: I execute this earlier in the code when I import the data.  Let me try to convert a second time to see if that clears it up.  Give me a few minutes.