Two steps in proof of the uniqueness of the total derivative

Question: $\newcommand{\R}{\mathbb{R}}$
In page 16 of Spivak's Calculus on Manifolds, he proves the uniqueness of the total derivative as follows:
Suppose $f: \R^n \to \R^m$ is differentiable at $a \in \R^n$ with derivatives the linear maps $\lambda, \mu: \R^n \to \R^m$. Let $d(h) = f(a + h) - f(a)$. Then
\begin{align*}
\color{green}{\lim_{h \to 0}} \frac{|\lambda(h)- \mu(h)|}{|h|} &= \color{green}{\lim_{h \to 0}} \frac{|\lambda(h) - d(h) + d(h) - \mu(h)|}{|h|} \\ &\leq \color{green}{\lim_{h \to 0}} \frac{|\lambda(h) - d(h)|}{|h|} + \color{green}{\lim_{h \to 0}}\frac{|d(h) - \mu(h)|}{|h|} \\ &= 0.
\end{align*}
Now for $0 \neq x \in \R^n$, the sequence $tx \to 0$ as $t \to 0$, so
$$ 0 \color{red}{=} \lim_{t \to 0} \frac{|\lambda(tx) - \mu(tx)|}{|tx|} = \frac{|\lambda(x) - \mu(x)|}{|x|} ,$$
and thus $\lambda(x) = \mu(x)$.
My question is: how do we know the original limit exists? I suspect that the more general fact holds that if $f$ is a linear map then $\lim_{h \to 0} \frac{|f(h)|}{|h|}$ exists, but I am not sure how to prove it.
I also would like to know why equality above in red holds. I assume it has to do with the fact that
$$g \text{ continuous at h} \Longleftrightarrow \left( \forall (a_n)_{n \in \mathbb{N}} : \lim_{n \to \infty} a_n = h \implies \lim_{n \to \infty} g(a_n) = g(h)  \right) ,$$
where $g: \R^n \to \R$ (the function inside the original limit) is defined by
$$ g(h) := \frac{|\lambda(h) - \mu(h)|}{|h|} ,$$
and $a_n \to 0$ is the sequence $a_n = \frac{1}{n} x$. But why can we use this fact when $g$ is not continuous at $0$?
EDIT #1:
For my second question, I see that the following fact would answer my question:
$$\lim_{h \to c} g(h) = d \Longleftrightarrow \left( \forall (a_n)_{n \in \mathbb{N}} : \lim_{n \to \infty} a_n = c \implies \lim_{n \to \infty} g(a_n) = d  \right) ,$$
but I am not sure if it is true as I have never came across it. However, I have come across the weaker version of the above fact when $d = g(c)$ (in which case $g$ is continuous at $c$), which I know to be true.
EDIT #2:
On my first question, if $f: \R^n  \to \R^m$ is a linear map, and
$$ \lim_{h \to 0} \frac{|f(h)|}{|h|} $$
exists, if we denote the function on the inside by
$$ f^*(h) := \frac{|f(h)|}{|h|} ,$$
it is true that $f^*(\lambda h) = \lambda f^*(h)$ for $\lambda \in \R$, hence $f^*$ is constant along any line passing through the origin. But since the limits along any of these lines must be the same, $f^*$ must be a constant map and hence the zero map. I guess this confirms with what was found originally, but we can't reason that
$$ \lim_{h \to 0} \frac{|\lambda(h) - \mu(h)|}{|h|} $$
exists because it is the zero map, as that is what we are trying to prove!
EDIT #3:
I believe EDIT #1 answers my second question, as I have now found a source which proves the fact discussed there.
Some of my reasoning towards the end of EDIT #2, so it has been marked out.
Also, I just realized that if we replace all occurrences of $\lim$ (only the ones marked in green way above) with $\liminf$ or $\limsup$, the same manipulations hold, and this time we know the existence of each quantity is guaranteed, so we would have
$$ \liminf = \limsup = 0 \implies \lim = 0 .$$
Does anyone know if it was an error on Spivak's part to not present the proof this way?
Answer: I think you are right, strictly speaking. But it is easier IMO to use `sandwich rule' then talk about liminfs/limsups.
$$ 0\le \frac{|\lambda(h) - \mu(h)|}{|h|} \le \frac{|\lambda(h) - d(h)|}{|h|} + \frac{|\mu(h) - d(h)|}{|h|}  \xrightarrow[h\to 0]{} 0$$
hence $\lim_{h\to 0}\frac{|\lambda(h) - \mu(h)|}{|h|}$ (exists and) equals $0$. In any case, I suspect that Spivak expects a reader of this book to be happy to make this sort of reasoning rigourous themselves.
As for the second issue it's not exactly sequences but paths, so you'd want something like
$$\lim_{h\to 0}f(h) =  0 \iff \forall g:\mathbb R \to \mathbb R^n \text{ with } \lim_{t\to 0} g(t) = 0, \ \lim_{t\to 0}f(g(t)) = 0.$$
(you can fuss about details e.g. f should be defined on an open neighbourhood of zero...) And to prove this you can just slowly write out epsilons and deltas. E.g. proof of "$LHS\implies RHS$": Let $\epsilon>0$ and fix any such $g$. By assumption, there is $\delta_1>0$ such that $|f(h)|<\epsilon$ for all $|h|<\delta_1$. As $g(t) \to 0$, there is $\delta>0$ such that $|g(t)|<\delta_1$ for all $|t|<\delta$. Result follows. For the other way round you might want to try to prove the contrapositive statement.
Comment: @AbcedDecba Well in this case the sequence is in fact enough for uniqueness, but it would not follow from one sequence limit existing that the limit $t\to 0$ exists!
Comment: Yes, that's right :)
Comment: Great, thanks .
Comment: Thank your for answering my questions. Regarding paths/sequences, my argument involving the sequence $a_n = \frac{1}{n} x$ still works, right? Admittedly it is more awkward this way, but it works out by invoking the fact I discussed in my second edit? I was not aware of limits being preserved along paths, though, thank you for introducing this fact to me.
Comment: Right, but my point is that after establishing the limit (first one marked in green) exists (using either limsup/liminf or the sandwich rule like you described), then the limit along all sequences (and paths) will be the same, so my original argument is (awkward but) correct? Sorry if I am misunderstanding.
Comment: This is slightly unrelated, but would you happen to know if the fact about paths you highlight can be used to prove my fact about sequences through some sort of "discretization" (at least the forward direction)? It really does feel like it should be the case that every convergent sequence (seen as a function $\mathbb{N} \to \mathbb{R}^n$) can be "extended" to some appropriate function $\mathbb{R} \to \mathbb{R}^n$.
Comment: Also, I'd like to mention that the forward direction of the fact about paths is a special case of this more general fact about composition: if $f, g$ are real functions and $a, b \in \mathbb{R}$ are such that $g$ is continuous at $b$ and
$$ \lim_{x \to a} f(x) = b  ,$$
$$ \lim_{x \to b} g(y) = c ,$$
then we have
$$ \lim_{x \to a} (f \circ g)(x) = c .$$
Of course this can be generalized to more than one dimension, I just write it like this for the sake of brevity.
Comment: @AbcedDecba they are both equivalent characterisations of a limit, yes. I only mentioned paths because that's what Spivak uses


Values of Lebesgue integrable function from the integral of the product of a known function.

Question: Let's say we have three complex absolutely integrable and square integrable functions $A,B,C\in \mathbb{L}^1(\mathbb{C})\cup \mathbb{L}^2(\mathbb{C})$ such that the following holds:
$$A(y) = \int_{-\infty}^\infty{B(x)C(xy)dx}$$
If we can measure or calculate $A$ and $C$ for all values, what information (if any) could we learn about the function $B$? I understand that the entire function $B$ can't be extracted in most cases, but is there a systematic way of describing what information can be extracted and how to do so?
Comment: In general, nothing, just think of the case $A=C=0$.
Answer: There is an algorithm similar to what I was looking for in this physics paper. It uses the de-convolution theorem and polynomial series to "invert" the integral. I use the term "invert" loosely here because information is definitely lost, but for some functions it clearly produces decent approximations.


GWT Place objects

Question: I'm new to GWT so this may be a dumb question but I didn't find a good answer to it anywhere.
So, how should I create Place objects in GWT? Should I create them as finals in my ClientFactory and reuse them everywhere?
the Place objects Eclipse generates eat a string token as their parameter what should it be?
My Understanding is the following: I should create final Place objects in my ValueChangeHandler for those pages that are static and reuse them everywhere. Those 'Place's that are relatively dynamic should be created like
new MyCoolPlace("MyCoolPlace" + someRandomNumber)

but isn't it kind of dumb using random numbers each time i need to navigate to a new page? also will the history be cleared of the objects which i created say hours ago?
Currently I have a problem with figuring out how this mechanism works:
I have a form. For, say, adding vegetables to the DB
1. User fills it
2. Submits it
3. Gets to a thank you page
4. Clicks a link in the menu to add a new vegetable
5. Gets to the form but it's already pre-filled with the values user previously entered.
I need the form clean on step 5. But if the user clicks the browser's back button on step 3 I want it to be pre-filled.
Answer: Question about final:
I think not. They are lightweight objects and places can have values into them. They are not a limited set of places values (at least if you parametrize them).
Question about form values:
The normal behaviour is when yo go back to -let's say- "vegForm" you obtain the clean view (it's because your URL token is simple vegForm). What happens I think it's you're reusing your view without initializing. Your presenter (or whatever has the UI logic) should obtain the view and call "reset()" "init()" or whatever you want.
If you want to hold your values when you go back you should save them in your place object. It is: field values. And your PlaceTokenizer (the object that transform between URL and Place object) should take care of them. By example: vegForm/potato,green,... (don't remember the standard notation).
The problem is how do you save into the URL the values you entered before clicking Save?
What I'd do is "saving" that state into the URL before saving:
History.newItem("vegForm/potato,green,...", false);

And that will get into the history stack. When I say false it means that the GWT History mechanism should not react to that URL avoiding loading the view again. When your user clicks back she should go back to form(with values). If your user clicks back again the she should go back to form(clean).
By example:
User clicks add veg -> #vegForm
User fills data and pless save
   -> #vegForm/value1,value2 (saved in History with false argument)
   -> #okPage

User clicks back
   -> #vegForm/value1,value2 (form with saved data)
User clicks back
   -> #vegForm (form without)

Initialization
Take into account you always must initialize the view.

If you don't have params, to clean it (it can be a reused view)
If you have params, to fill the form with them
Comment: Oh, by the way, be careful with the values you put into the token I don't know if GWT encode it or not (if it's not you should encode it manually). And also you should take care of length. Don't make it too long (I think the limit is 1024 chars but I'm not sure).

GWT Place objects


Solving a recurrence relation.

Question: So I extracted this recurrence relation from a problem that I need to solve:
$$
g(n) = 2g(n-1) + \sum_{i=0}^{n-2} g(i) + 1.
$$
with
$$
g(0) = 1.
$$
All I know are two methods of linear homogenous relations, that not it.
Comment: Have you tried computing the first values and plugging them into OEIS? https://oeis.org/
Comment: You can rewrite that in a more pleasing way as $g(n-1)+\sum_{i=0}^{n-1} g(i) + g(0)$. Makes it symmetric-seeming. Not sure how that helps, however.
Answer: There are several ways to approach this. You might notice that
$$\begin{align*}
g(n)&=2g(n-1)+\sum_{i=0}^{n-2}g(i)+1\\
&=2g(n-1)+g(n-2)+\sum_{i=0}^{n-3}g(i)+1\\
&=2g(n-1)+\left(2g(n-2)+\sum_{i=0}^{n-3}g(i)+1\right)-g(n-2)\\
&=2g(n-1)+g(n-1)-g(n-2)\\
&=3g(n-1)-g(n-2)\;,
\end{align*}$$
giving you a linear homogeneous recurrence.
Or you might begin by gathering some data; this is never a bad idea. Here are the first few values of $g(n$):
$$\begin{array}{rcc}
n:&0&1&2&3&4&5\\
g(n):&1&3&8&21&55&144
\end{array}$$
Those number ought to be familiar: they're all Fibonacci numbers. In fact, they're alternate Fibonacci numbers:
$$\begin{array}{rcc}
n:&0&1&2&3&4&5&6&7&8&9&10&11&12\\
F_n:&0&1&\underline1&2&\underline3&5&\underline8&13&\underline{21}&34&\underline{55}&89&\underline{144}
\end{array}$$
This very strongly suggests that $g(n)=F_{2n+2}$. You could prove this by induction and then use the Binet formula to get a closed form for $g(n)$.
Comment: Thank you for the answer! very fast and very nice, i really understood how you did, and it is nice to gather some data, if i would, i would see that those are fibo numbers.
Comment: I will try that on other problems as well.
Comment: @Yves: You're very welcome!
Answer: As shown in my answer to the preceding question, the recurrence relation can be written as
$$
\begin{pmatrix}g(n)\\X(n)\end{pmatrix} = A \begin{pmatrix}g(n-1)\\X(n-1)\end{pmatrix}
$$
with
$$
A = \begin{pmatrix}
2 & 1 \\
1 & 1
\end{pmatrix}\quad\text{and}\quad X(0) = 1\text{.}
$$
Then $$\begin{pmatrix}g(n)\\X(n)\end{pmatrix} = A^n \begin{pmatrix}g(0)\\X(0)\end{pmatrix} = A^n\begin{pmatrix}1\\1\end{pmatrix},$$
so the problem reduces to the computation of $A^n$.
For this, diagonalize $A$.
Comment: Thanks for the answer , It sure looks very elegant, but i havent tested on Linear Algebra yet.
Answer: Another way is to write the equation for $g(n-1)$:
$$
g(n-1) = 2 g(n-2) + \sum_{k=0}^{n-3} g(k) + 1
$$
and subtract it from $g(n)$ to get the linear equation: $g(n) = 3 g(n-1) - g(n-2)$ that can be solved using generating functions.

Solving a recurrence relation.


Having images in a picker for iPhone app

Question: I'm writing an iPhone app in which I have a double component picker. One side has the 13 different card values (ace, 2, 3, 4, etc.) and the other should have images for the four suits (spades, hearts, diamonds, clubs). I dragged the four .png files into the folder.
Can anyone tell me why this code doesn't work? I'm almost exactly following an example from class.
UIImage *spades = [UIImage imageNamed:@"spades.png"];
UIImage *hearts = [UIImage imageNamed:@"hearts.png"];
UIImage *diamonds = [UIImage imageNamed:@"diamonds.png"];
UIImage *clubs = [UIImage imageNamed:@"clubs.png"];

UIImageView *spadesView = [[UIImageView alloc] initWithImage:spades];
UIImageView *heartsView = [[UIImageView alloc] initWithImage:hearts];
UIImageView *diamondsView = [[UIImageView alloc] initWithImage:diamonds];
UIImageView *clubsView = [[UIImageView alloc] initWithImage:clubs];

NSArray *suitsArray = [[NSArray alloc] initWithObjects: spadesView, heartsView, diamondsView, clubsView, nil];
self.suits = suitsArray;

When I just try to do this (below), it works.
NSArray *suitsArray = [[NSArray alloc] initWithObjects: @"Spades", @"Hearts", @"Diamonds", @"Clubs", nil];
self.suits = suitsArray;

Any ideas?
Comment: What's not working? Are you getting an error?
Comment: You do not specify a frame for the image views.
Comment: FreeAsInBeer, sorry I didn't specify... I don't get an error. The simulator loads but then immediately crashes.
Comment: Radek, I'm not sure what you mean by specifying a frame.
Comment: How does it crash? What exception do you get?
Comment: Maybe too big images ? You have to release your UIImageView after putting them in an NSArray, then release you array is your suits variable is a retained property.
Comment: Indeed, we would need to know what kind of error you get. Might be a bad access... Does anything special happen in the setter method for suits?
Answer: Try that, it could do the work :
- (void)populateSuitsArray
{
    NSMutableArray *suitsArray = [[NSMutableArray alloc] initWithCapacity:4];

    UIImage *spades = [UIImage imageNamed:@"spades.png"];
    UIImageView *spadesView = [[UIImageView alloc] initWithImage:spades];
    [suitsArray addObject:spadesView];
    [spadesView release];

    UIImage *hearts = [UIImage imageNamed:@"hearts.png"];
    UIImageView *heartsView = [[UIImageView alloc] initWithImage:hearts];
    [suitsArray addObject:spadesView];
    [spadesView release];

    UIImage *diamonds = [UIImage imageNamed:@"diamonds.png"];
    UIImageView *diamondsView = [[UIImageView alloc] initWithImage:diamonds];
    [suitsArray addObject:diamondsView];
    [diamondsView release];

    UIImage *clubs = [UIImage imageNamed:@"clubs.png"];
    UIImageView *clubsView = [[UIImageView alloc] initWithImage:clubs];
    [suitsArray addObject:clubsView];
    [clubsView release];

    self.suits = suitsArray;
    [suitsArray release]; // According if your suits is a retained property
}

Comment: Thanks so much for the code. It still doesn't seem to work. I get no errors, simulator opens, but the app doesn't even open. I'm starting to think the problem might be with my .png files, though I seem to be following the 57x57 pixel specifications. Any ideas what else can be wrong with the images?


Should I see identical sstables on multiple cassandra nodes?

Question: We're trying to verify the state of replication in our cassandra cluster. My colleague has found that only a small number of sstable files exist on multiple nodes. The others are all unique.
To me, this makes sense. As I understand it, each node should be responsible for a unique set of ranges, and should have sstables that reflect those ranges. But now I'm not sure.
Should we find at least n copies of each sstable with replication factor of n? Or are the copies of the sstables a result of the bootstrap, and haven't yet been compacted?
Answer: SSTable files are created when flush of memtable happen, and when the SSTables are compacted. Every node may have this happen at different times (plus other factors, like, short downtime, etc.).
To have all data correctly replicated you need to have repair process implemented - either by explicitly invoking the nodetool repair, or using some tools, like, DataStax's OpsCenter (only for DSE), or Reaper, (or something like).


Simple queries occasionally running very slowly

Question: I have some very simple queries that occasionally run very slowly.
The table viewed_sites has about 10 - 20 rows.
Running EXPLAIN ANALYZE always gives a runtime of less than 3 milliseconds.
When the query is run automatically (every 10 seconds) it occasionally takes over a second to run.
The query:
INSERT INTO ga.viewed_sites (site_id) VALUES ('gop2')

The table:
CREATE TABLE viewed_sites (
    site_id character varying(4) NOT NULL,
    last_viewed timestamp with time zone DEFAULT now() NOT NULL
);

The (occasional) log result:
2010-05-24 15:47:55 UTC LOG:  duration: 1044.632 ms  statement: INSERT INTO ga.viewed_sites (site_id) VALUES ('gop2')
It's a horribly vague question, but what could be causing this?  I suppose it comes down to CPU, RAM, HDD or some combination of the above.
Postgresql 8.3, Ubuntu 8.04
Intel(R) Core(TM)2 Duo CPU     E6750  @ 2.66GHz
2 GiB RAM
Answer: Or, the table could be locked. Which seems all things given, more likely. Postgresql is very conservative about locks, but it can happen. A one second wait for HD write, or CPU seems extremely unlikely. And, it doesn't take much ram to do an insert. Check the table pg_locks (and pg_stat_activity to see what their doing), and the function pg_lock_status(), or some of the other tables and functions pertaining to lock status.
Comment: Is there a way to log table locks? The chances of the lock being active when I look at those tables is slim.
Answer: Does the table have a foreign key?  If it has to check the foreign key constraint, then it has to go do a table lookup.  Maybe that's slowing it down.
http://wiki.postgresql.org has a ton of tips.
Comment: It does have a foreign key, and a trigger.  I disabled both of these and it had no effect. Thanks for the wiki link though.


About two 'negative' continued fractions whose sum equals $1$

Question: Letting $a_1,a_2,\cdots,a_r$ be integers which are larger than or equal to $2$, let us define
$$[a_1,a_2,\cdots,a_r]=\frac{1}{a_1-\frac{1}{a_2-\frac{1}{\ddots-\frac{1}{a_r}}}}$$
(Note that the negative signs are used)
Also, let $X, Y, Z$ be positive integers which satisfy
$$Z\lt X+Y,\  Z\gt X,\  Z\gt Y$$
and let
$$\frac XZ=[a_1,a_2,\cdots,a_r],\ \frac YZ=[b_1,b_2,\cdots,b_s].$$
Then, here is my question.
Question : Is the following true?
"There exist $r^{\prime}\le r, s^{\prime}\le s$ such that
$$[a_1,a_2,\cdots,a_{r^{\prime}}]+[b_1,b_2,\cdots,b_{s^{\prime}}]=1$$
for any $(X,Y,Z)$."
Remark : Observing the initial numbers is not sufficient because the nearer to $1$ the value $\frac XZ+\frac YZ$ is, the harder it is to find the answer (see example 2).
Examples :

$\frac XZ=\frac 37=[3,2,2]$ and $\frac YZ=\frac 57=[2,2,3]$ leads $[3]+[2,2]=\frac 13+\frac 23=1$ where $\frac 37+\frac 57=\frac 87\approx 1.143$
$\frac XZ=\frac{901}{2067}=[3,2,2,4,2]$ and $\frac YZ=\frac{1170}{2067}=[2,5,2,2,3]$ leads $[3,2,2,4]+[2,5,2,2]=\frac{10}{23}+\frac{13}{23}=1$ where $\frac XZ+\frac YZ=\frac{2071}{2067}\approx 1.002.$

Motivation : I've got an algorithm to find $b_1,b_2,\cdots,b_s$ such that
$$1-x=[b_1,b_2,\cdots,b_s]$$
for any given $x=[a_1,a_2,\cdots,a_r]$.
Algorithm : Supposing that $2^r$ represents $r$-consective $2$s, I'm going to write
$$[a_1,a_2,\cdots,a_r]=[2^{q_1},p_1,2^{q_2},p_2,\cdots,2^{q_s},p_s,2^{q_{s+1}}]$$
where $p_i\ge 3\in \mathbb N, q_i\ge 0\in \mathbb Z$. For example, $[2,2,5,3,2,4]=[2^2,5,2^0,3,2^1,4,2^0]$.
Then, the algorithm is
$$1-[2^{q_1},p_1,2^{q_2},p_2,\cdots,2^{q_s},p_s,2^{q_{s+1}}]$$
$$=[(q_1+2),2^{(p_1-3)},(q_2+3),2^{(p_2-3)},(q_3+3),2^{(p_3-3)},\cdots,(q_s+3),2^{(p_s-3)},(q_{s+1}+2)].$$
After getting this algorithm, I reached the above expectation. I can neither find any counterexample even by using computer nor prove that the expectation is true. Can anyone help?
Update : I crossposted to MO.
Answer: I'm posting an answer just to inform that the question has received an answer by Alexey Ustinov on MO.
https://mathoverflow.net/questions/144996/about-two-negative-continued-fractions-whose-sum-equals-1


Questions on DFS

Question: Question:
if I assume that I ran Depth First Search on a digraph $G,$ and obtained a directed spanning forest $F.$ I am interested in the set $R$ of vertices reachable
from a given fixed vertex $a \in V (G).$ How do I Show in general set $R$ of vertices cannot be obtained from directed spanning forest $F$?
Answer:
Can I answer it by saying that if I have a digraph of $G:= (\{1,2,3,4\},\{(1,2),(3,4)\}$ and i perform Depth First Search $(G, 1)$ which will result in $(\{1,2\}, \{(1,2)\})$ whereby vertex $1$ is unreachable. However, how is it even a directed spanning forest if I only obtain only vertex $1$ and $2$ which is not a directed spanning forest but rather a forest. IF not how should I answer it so that the result is a spanning forest?
Comment: Can you please clarify "How do I Show in general set  of vertices cannot be obtained from directed spanning forest ? " ?
Comment: The definition of a spanning forest is that all nodes are spanned, i.e. reached. Unless restarted fron a non-reached node, DFS will produce a tree rather than a forest.
Comment: the question wants me to show the set R vertices which cannot be obtained from directed spanning forest F after performing DFS
Comment: @A.G. I think that in the question being asked, the graph may have multiple components, so DFS is called on each component separately.
Comment: @A.G. since there is multiple components, then how do I know which set of vertex is non-reachable
Answer: First you pick an initial node. As DFS progresses it will mark vertices as visited or seen. At the end, unseen vertices are those that cannot be reached by starting with the chosen initial node. The set of visited nodes corresponds to the first connected component (that to which the initial node belongs). If you want the full forest you then need to restart DFS from an unvisited node (and repeat).

Questions on DFS


Django_tables2 How to identify each table when using MultiTableMixin in Django Template

Question: I am trying to render two seperate tables on a different location in a Django template. However, the official documentation only shows how to render the tables in a loop:
<code>{% for table in tables %}
    {% render_table table %}
{% endfor %}
</code>
This works, however, I want to render a table based on a identifier(such as name). The following code does this, but it doesn't make it clear what table is being rendered:
<code>{% render_table tables.0%}
</code>
Something among the lines of:
<code>{% render_table tables.newstable %}
</code>
would be ideal.
I've had slight succes by removing the MultiTableMixin class, then rendering the tables using get_context_data(), however, this breaks pagination.
<code>
    def get_context_data(self, **kwargs):
        context = super(TeamHomeTest, self).get_context_data(**kwargs)
        team_id = self.kwargs['teamid']
        team = Tmteams.objects.get(teamid=team_id)
        q1 = Tmteamsusers.objects.all().filter(teamid=team_id)

        category_ids = []

        categories = nwscategory.objects.all().filter(teamId=team_id)

        for cat in categories:
            category_ids.append(cat.categoryid)

        q2 = nwseditor.objects.filter(categorie_id__in=category_ids)

        team_member_list_table = TeamMemberListTable(q1)
        team_news_list_table = TeamNewsTable(q2)

        context['table1'] = team_member_list_table
        context['table2'] = team_news_list_table
        context['team'] = team
        self.tables=[context['table1'], context['table2']]

        return context
</code>
My current get_tables() method looks like this:
<code>    def get_tables(self):
        team_id = self.kwargs['teamid']
        q1 = Tmteamsusers.objects.all().filter(teamid=team_id)

        category_ids = []

        categories = nwscategory.objects.all().filter(teamId=team_id)

        for cat in categories:
            category_ids.append(cat.categoryid)

        q2 = nwseditor.objects.filter(categorie_id__in=category_ids)

        table1 = TeamMemberListTable(q1)
        table1.name = "test"

        tables = [
            table1,
            TeamNewsTable(q2)
        ]

        return tables
</code>
Answer: Inside your template you can render it as:
<code>{% render_table table1 %}
{% render_table table2 %}
</code>
So you can actually name the table anything you want. It does not have to be table1, table2, ...
Just make sure that the names match you assignment in the get_context_data.
Eg:
<code> def get_context_data(self, **kwargs):
        context = super(TeamHomeTest, self).get_context_data(**kwargs)
        team_id = self.kwargs['teamid']
        team = Tmteams.objects.get(teamid=team_id)
        q1 = Tmteamsusers.objects.all().filter(teamid=team_id)

        category_ids = []

        categories = nwscategory.objects.all().filter(teamId=team_id)

        for cat in categories:
            category_ids.append(cat.categoryid)

        q2 = nwseditor.objects.filter(categorie_id__in=category_ids)

        team_member_list_table = TeamMemberListTable(q1)
        team_news_list_table = TeamNewsTable(q2)

        context['table_team_member'] = team_member_list_table
        context['table_team_news'] = team_news_list_table
        context['team'] = team
        self.tables=[context['table_team_member'], context['table_team_news']]

        return context
</code>
HTML:
<code>{% render_table table_team_member %}
{% render_table table_team_news %}
</code>
Hope this helps


Service Worker determine number of clients

Question: We've got a SaaS app that's PWA capable and we're using Workbox for the grunt work. Up to now we've been following the tried and trusted recipe of displaying an update is available banner to the users, prompting them to update their web app.
Viewing usage data (via Sentry.io) we've noticed that most users simply seem to ignore the update banner and continue with the version they're on.
So we're looking at trying something different. This is to perform the update automatically when they change the route in the web app (when we know there's an update available).
Testing this shows that it does work. However there's a side-effect, and that is if they've got web app open in multiple tabs, then all the tabs get updated. This could be problematic for users' if they've got an un-saved form open in one of the tabs in the background - they'll potentially loose their work.
This happens during this piece of code:
// app shell page, created lifecycle hook
document.addEventListener('swUpdated', this.SetPwaRegistration, { once: true })
navigator.serviceWorker.addEventListener('controllerchange', () => {
  if (this.refreshing) {
    return
  }
  this.refreshing = true
  window.location.reload()
})

// app shell page, method in methods collection
SetPwaRegistration (event) {
  // call mutation to pass the registration object to Vuex
  this.PWA_REGISTRATION_SET({ pwaRegistration: event.detail })
}

// main.js
router.afterEach((to, from) => {
  // retrieve the registration object from Vuex
  const pwaRegistration = app.$store.getters.pwaRegistration
  if (pwaRegistration) {
      pwaRegistration.waiting.postMessage('skipWaiting')
  }
})

the above code is from our Vue.js app code-base, the this.refreshing is set to false by default in the data property collection.
What I'd like to know if whether it is possible to determine if the Service Worker has only one client under it's control (i.e. the web app is only open in 1 browser tab), and if this is the case, the auto-update can happen without potential issues. If there's more than one client, then we'll display the update banner as usual.

As a brief update to this, I've come across code examples similar to this:
self.clients.matchAll().then(clients => {
  const clientCount = clients.length
  // store count of client in Vuex
})

Which looks like an option (i.e. count how many clients there are, store this in Vuex store), I'm just not sure where it should be used.
Answer: If you want to orchestrate the reload from entirely within the service worker, you can effectively do that by using the WindowClient interface to programmatically navigate to whatever the current URL is, which is roughly equivalent to a reload.
The two things to keep in in mind that navigate() will only work on WindowClients (not Workers) and that you can only call it if the service worker controls the WindowClient.
Putting that together, here's something to try:
// This can be run, e.g., in a `message` handler in your SW:
self.clients.matchAll({
  // These options are actually the defaults, but just
  // to be explicit:
  includeUncontrolled: false,
  type: 'window',
}).then((clients) => {
  if (clients.length === 1) {
    clients[0].navigate(clients[0].url);
  }
})

Comment: Thanks Jeff. If I understand correctly, would this then only reload that particular tab (client) that 'received' the update? I.e. I would then be able to remove the `navigator.serviceWorker.addEventListener('controllerchange', ...)` bit of code which refreshes all the clients?
Comment: I think I understand.. in the sw.js, where I currently have `if (e.data === 'skipWaiting') { self.skipWaiting() }` I'd replace this with your suggestion?
Comment: You'd still need to keep the logic around triggering `skipWaiting()` if you want to ensure that the newly installed service worker activates first. That code snippet I posted is a direct alternative to calling `window.location.reload()`, ensuring that the reload only happens when there is exactly one controlled client window open. I was under the impression that's what your goal was.
Comment: Thanks again Jeff, yeah, your impression is about right. I'll do some more playing round with the code sample. I'll get there :) Much appreciated.


Create dataframe from dataframe in list with the maximum value in a column in R

Question: I want to select the dataframe with the highest numbers in the 'value' column from a list of dataframes. In this case it will be dataframe3. I want to make a new dataframe from that selected dataframe.
Thanks in advance.
Cheers
Here is an example dataset:
list <- list()

dataframe1 <- data.frame(sample = c("OP2645ii_c","OP5048___e","OP5048___f","OP5046___d","OP2645ii_e","OP2645ii_a","OP5054DNAa","OP5048___c","OP2645ii_d","OP5048___b","OP5047___a","OP5048___h","OP5053DNAb","OP3088i__a","OP5048___g","OP5053DNAa","OP5049___a","OP2645ii_b","OP5046___c","OP5044___c","OP2413iiia","OP5054DNAc","OP5046___e","OP5054DNAb","OP5044___a","OP5046___a","OP5046___b","OP2413iiib","OP5051DNAa","OP5048___d","OP5044___b","OP5049___b","OP5051DNAc","OP5051DNAb","OP5053DNAc","OP5047___b","OP5043___b","OP5043___a","OP5052DNAa"),
                         gr = c("1","1","2","5","4","5","5","3","2","2","2","4","3","1","1","3","2","1","2","5","5","5","2","2","2","1","2","1","1","1","2","1","1","2","2","5","3","3","5"),
                         value = c("14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500"))

list[[1]] <- dataframe1

dataframe2 <- data.frame(sample = c("OP2645ii_c","OP5048___e","OP5048___f","OP5046___d","OP2645ii_e","OP2645ii_a","OP5054DNAa","OP5048___c","OP2645ii_d","OP5048___b","OP5047___a","OP5048___h","OP5053DNAb","OP3088i__a","OP5048___g","OP5053DNAa","OP5049___a","OP2645ii_b","OP5046___c","OP5044___c","OP2413iiia","OP5054DNAc","OP5046___e","OP5054DNAb","OP5044___a","OP5046___a","OP5046___b","OP2413iiib","OP5051DNAa","OP5048___d","OP5044___b","OP5049___b","OP5051DNAc","OP5051DNAb","OP5053DNAc","OP5047___b","OP5043___b","OP5043___a","OP5052DNAa"),
           gr = c("5","4","3","5","4","5","5","3","2","2","2","2","3","1","1","3","2","1","2","5","5","5","2","2","2","1","2","1","1","1","2","1","1","2","4","4","4","4","4"),
           value = c("12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000"))
list[[2]] <- dataframe2

dataframe3 <- data.frame(sample = c("OP2645ii_c","OP5048___e","OP5048___f","OP5046___d","OP2645ii_e","OP2645ii_a","OP5054DNAa","OP5048___c","OP2645ii_d","OP5048___b","OP5047___a","OP5048___h","OP5053DNAb","OP3088i__a","OP5048___g","OP5053DNAa","OP5049___a","OP2645ii_b","OP5046___c","OP5044___c","OP2413iiia","OP5054DNAc","OP5046___e","OP5054DNAb","OP5044___a","OP5046___a","OP5046___b","OP2413iiib","OP5051DNAa","OP5048___d","OP5044___b","OP5049___b","OP5051DNAc","OP5051DNAb","OP5053DNAc","OP5047___b","OP5043___b","OP5043___a","OP5052DNAa"),
                         gr = c("5","3","3","5","5","5","5","3","5","3","3","3","3","3","3","3","2","1","2","1","1","1","2","2","2","1","2","1","1","1","2","1","1","4","4","4","4","4","4"),
                         value = c("20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915"))

list[[3]] <- dataframe3

dataframe4 <- data.frame(sample = c("OP2645ii_c","OP5048___e","OP5048___f","OP5046___d","OP2645ii_e","OP2645ii_a","OP5054DNAa","OP5048___c","OP2645ii_d","OP5048___b","OP5047___a","OP5048___h","OP5053DNAb","OP3088i__a","OP5048___g","OP5053DNAa","OP5049___a","OP2645ii_b","OP5046___c","OP5044___c","OP2413iiia","OP5054DNAc","OP5046___e","OP5054DNAb","OP5044___a","OP5046___a","OP5046___b","OP2413iiib","OP5051DNAa","OP5048___d","OP5044___b","OP5049___b","OP5051DNAc","OP5051DNAb","OP5053DNAc","OP5047___b","OP5043___b","OP5043___a","OP5052DNAa"),
                         gr = c("2","2","2","3","4","5","5","3","2","2","2","4","5","1","1","3","2","1","2","5","5","5","2","2","2","1","2","1","1","1","2","1","1","2","2","5","3","3","5"),
                         value = c("18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500"))

list[[4]] <- dataframe4

Comment: What do you mean by "the highest numbers in the 'value' column"? The highest maximum value per column, or the mean, median, sum, harmonic mean...?
Comment: @IgorF. I might have phrased that badly. I mean the highest value across all the dataframes in the list. Essentially, I want to select the dataframe which has the highest 'value' column. All the values in the same dataframe will be equal because they represent the mean of another process not shown here.
Answer: Convert data to appropriate types (so as to enable maximum calc):
df_list <- lapply(df_list, function(x){type.convert(x)})

Find the index of the df in the list with the maximum value, and extract it out into new df:
new_df <- df_list[[which.max(lapply(df_list, function(x){max(x$value)}))]]

Answer: How about this one:
# max_df is a list to store the max of each value in the list
max_df = vector("list", length(list))

for(i in seq_along(list)){
  max_df[i] <- max(as.numeric(as.character(list[[i]][["value"]])))
}
#max_df now has the max of each list
# which reports the position of the max
which(unlist(max_df) == max(unlist(max_df)))
#[1] 3

Answer: Why does it have to be lists? You can make one big single data frame, with a column frame.nr to identify its place in your list (which you actually don't need), and filter by the highest value. Something like:
library(tidyverse)

dataframe1 %>% mutate(frame.nr=1) %>%
  rbind(dataframe2 %>% mutate(frame.nr=2)) %>%
  rbind(dataframe3 %>% mutate(frame.nr=3)) %>%
  rbind(dataframe4 %>% mutate(frame.nr=4)) %>%
  mutate(value = as.numeric(as.character(value))) -> df.all

df.all %>%
  filter(frame.nr ==
    df.all %>%
      group_by(frame.nr) %>%
      summarise(max.value=max(value)) %>%
      filter(max.value == max(max.value)) %>%
      select(frame.nr) %>%
      unlist
) %>%
  select(-frame.nr)


How to determine cluster on EM-Gaussian Mixture clustering with 2 or more variables

Question: I'm trying to compute EM Gaussian Mixture clustering algorithm.
As I found in Bishop(2009), it explained the algorithm.
Which is we have E-step and M-step in the iteration process. And we could compute the responsibilities for component $k^{th}$ for data point $\textbf{x}_n$ where denoted as $\gamma (z_{nk})$.
$\gamma (z_{nk}) = \frac{\pi_k N(\textbf{x}_n | \mu_k , \sum_k)}    {\sum^{K}_{j=1} \pi_j N(\textbf{x}_n | \mu_j , \sum_j)}$
As I understood, the $\gamma (z_{nk})$ is in 2D matrix form where the row shows the data point and column shows the cluster. After the final iteration, we can determine the cluster for each data point by take the maximum value for each row. However, I'm trying to compute 2 variables, which is I have to expand the matrix into 3D, and my question is how can I figure out the final cluster for each data point since each variable produces different cluster.
For example,
Answer: You don't go into 3d tensors, and you also don't want to assign to the maximum (then you're essentially just doing k-means).
You want to use a multivariate Gaussian, so you still get one density per point and cluster. You normalize these to one, and put every point into every cluster according to these probabilities. So a point inbetween of two clusters will be 0.5 in one ,and 0.5 in the other.
Comment: Thanks for the answer. Right now, the value for calculate the normal pdf is single value, then I have to do the calculation in matrix form? But the output of the normal pdf is gonna be single value..is it correct?
Comment: Yes, the density of a multivariate Gaussian is still a single real number.


Why type cast works while selector doesn't work

Question: I want to loop through view and its subviews to highlight them. Firstly, I use selector like this:
func highlightViewAndSubviews(view: UIView, highlighted: Bool) {
    if view.respondsToSelector(Selector("setHighlighted:")) {
        view.performSelector(Selector("setHighlighted:"), withObject: highlighted)
    }
    for subview in view.subviews {
        highlightViewAndSubviews(subview, highlighted: highlighted)
    }
}

But it doesn't work when highlighted is false or view is UILabel. Then I use type cast to achieve this. It works perfectly.
func highlightViewAndSubviews(view: UIView, highlighted: Bool) {
    if let imageView = view as? UIImageView {
        imageView.highlighted = highlighted
    }
    if let label = view as? UILabel {
        label.highlighted = highlighted
    }
    for subview in view.subviews {
        highlightViewAndSubviews(subview, highlighted: highlighted)
    }
}

My question is why. And I prefer selector because it works for label,button,image view and so on. Many thanks in advance.
Answer: The problem is that this line doesn't do what you think:
view.performSelector(Selector("setHighlighted:"), withObject: highlighted)

Use key-value coding instead:
view.setValue(highlighted, forKey:"highlighted")

Comment: Thanks! Could you please explain what this line does?


How do I theme my module output (specifically the nodes coming out of EntityFieldQuery)?

Question: I'm using EntityFieldQuery to get node IDs that match certain criteria in my module. At the end, I am doing: return node_view_multiple($nodes, 'full'); in the page callback function (which itself is called from hook_menu).
If I use node--content-type.tpl.php, I override the display everywhere. Ideally, I would like an array containing Node information that is sent to the tpl file, (like a controller returning display array to a view in CodeIgniter), which I can loop through and create a custom display.
How do I go about this?
Answer: Instead of doing a full node view you can make a custom node view like
return node_view_multiple($nodes, 'module_name');

That will enable you to create custom logic for this view type and setup field display as well if needed.
To define your own view mode you need to add it using hook_entity_info_alter
/**
 * Implements hook_entity_info_alter().
 */
function hook_entity_info_alter(&$entity_info) {
  $entity_info['node']['view modes']['module'] = array(
    'label' => t('Name'),
    'custom settings' => TRUE,
  );
}

Comment: Thank you for answering - from the point of defining my own node view, how do I get to theming that view with a tpl file?
Comment: @adityamenon You can add logic to the same template and handle the different view modes by doing `if ($view_mode == '...')`, or you could add template suggestions based on the view mode in the `preprocess_node` hook.


CGAffineTransform operations inconsistency in order applied

Question: I am trying to understand the results of chaining two transformations using CGAffineTransform in iOS. Based on Apple's documentation, combining translation and scaling is working as expected, but combining translation and rotation is not.
I think the question in this post was onto the same observation, but I am combining translation with scaling to show the inconsistent behavior. Or is there some consistent way to understand what order the transformations will take place using these methods?
Apple's CGAffineTransform documentation shows transforming a point, represented by a row vector [x y 1], by multiplying on the right by a matrix. To use the notation used in the CGAffineTransform header file, this matrix is [a b c d tx ty] (because the last column is always the transpose of [0 0 1]). Because the matrix is on the right of the row vector, if we have two CGAffineTransform matrices A and B, the product AB applied to a point will first apply the transformation A and then apply the operation B (which is opposite of what typical linear algebra books do).
Using a translation transform t, a scaling transformation s, and a rotation transform r, I have examined the resulting transforms and their effects on views for the following:
 s.translatedBy(x: 100, y: 0) // translates first, then scales
 s.concatenating(t) // scales first, then translates
 t.rotated(by: 45 * .pi/180) // translates first, then rotates
 t.concatenating(r) // rotates first, then translates

I understand that concatenating will perform in the reverse order as you see when performing an operation such as translatedBy. But, per concatenating: documentation, A.concatenatig(B) should give the transformation AB, which as noted above performs transformation A followed by B. That indeed happens on s.concatenating(t), but not t.concatenating(r). Based on the example in Matt's iOS book, here is some code to setup.
 let v1 = UIView(frame:CGRect(20, 111, 132, 194))
 v1.backgroundColor = .red
 view.addSubview(v1)

 let v2 = UIView(frame:v1.bounds)
 v2.backgroundColor = .green
 v1.addSubview(v2)

 let v3 = UIView(frame: v1.bounds)
 v3.backgroundColor = .blue
 v1.addSubview(v3)

 let t = CGAffineTransform(translationX:100, y:0)
 let r = CGAffineTransform(rotationAngle: 45 * .pi/180)
 let s = CGAffineTransform(scaleX: 0.1, y: 0.1)

Then you can add this code to see that translating and scaling works as expected:
 // translates first, then scales
 v2.transform = s.translatedBy(x: 100, y: 0)
 // scales first, then translates
 v3.transform = s.concatenating(t)

Green v2 translates 100 to the right and then is scaled by .1, where blue v3 is scaled by .1 and then translated 100 to the right
However, the behavior for translating and rotating is different:
 // translates first, then rotates
 v2.transform = t.rotated(by: 45 * .pi/180)
 // rotates first, then translates
 v3.transform = t.concatenating(r)

Green v2 is translated first and then rotated by 45 degrees, where as blue v3 is rotated first and then translated
Furthermore, the header doc information for rotated shows
Rotate t by angle radians and return the result:
         t =  [ cos(angle) sin(angle) -sin(angle) cos(angle) 0 0 ] * t

The multiplication should imply the rotation happens first, but the wording makes it seem that the rotation is second. Based on the results above, the wording correct (rotation is second).
The header doc for translatedBy also has wording for translation being second and the matrix multiplication showing the translation is first. But based on the results above, the matrix multiplication is correct (translation is first).
Am I making a mistake in this analysis? Or is there some inconsistency in the order of transformations based on concatenation and the descriptions in the documentation for these transformation and concatenating methods.
Comment: My book is quite clear that translating and then rotating gives a different result from rotating and then translating, and it illustrates both; operations are not commutative (order matters). And I show how concatenation works differently from applying one at a time. So what's not clear?
Comment: And modern example code for all of it is here: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch01p017transform/ch14p371frame/ViewController.swift
Comment: Thanks for commenting. I understand order makes a difference in transformations (and matrix multiplication in general). By looking at both pairs of examples, it appears that there is an inconsistency in the order of the transformations. `s.concatenating(t)` is performed `s` first and then `t`, but `t.concatenating(r)` is performed `r` first and then `t`. According to the documentation for `concatenating`, it should be the case that `A.concatenating(B)` yields `A * B`. So the order should always be `A` followed by `B`. `t.concatenating(r)` violates that.
Comment: There is no inconsistency.
Answer: The problem is that you have misinterpreted the first diagram:

You say:

Green v2 translates 100 to the right and then is scaled by .1, where blue v3 is scaled by .1 and then translated 100 to the right

No. Your words are backwards from what the diagram actually shows.
Remember, a transform takes place around a view's center. Ok, so why is the green view only a tiny bit to the right of its original center?
It's because  first we scaled down to the center and then we moved 10 points right — 10 points because the meaning of a point has first been scaled down to 1/10 of a normal point.
But the blue view is a full 100 points right of its original center, because it translated that 100 points before scaling down.
Comment: Thanks for the feedback. I have realized a mistake I made and I am becoming aware of another mathematical idea. My main mistake is as follows. There are apparently 2 ways of viewing the transformations, and the coordinate system is the thing to pay attention too.For example, the green v2 can be viewed as translating right by 100 and then scaling by .1 (with respect to the v2's original coordinate system). On there other hand, it can also be viewed as scaled by .1 and then translated in v2's new coordinate system. As for why, I am still trying to explore.
Comment: As for why, I think there might be a way to understand it by the following: The transform for v2 is equivalent to `t.concatenating(s)`. So the matrix is `ts` (not `st`). Using `x` as the initial point, we are considering `x' = x*t*s = s *((s^(-1)*t*s)`. The first way is as the docs describe, which is why the transform can be viewed as scale first and the translate. But the second way shows `s` first, followed by what must end up being `t` but in the new coordinates after s. That is pretty interesting. The difficult thing abut this is that the homogeneous coordinates mean we are working in R3
Comment: As for v3 in the rotation example, `v3.transform = t.concatenating(r)`. So the matrix is `tr`. One way to view is to translate by 100 to the right, and then rotate (but do so according to the original center). That is what I did not catch before. The other way is to view it as rotate first, then translate according to its new coordinate system. This also helps me make sense of the methods such as `rotated(by:)`.
Comment: I think I've explained why, however you choose to express it, the two sequences are behaving in completely parallel ways. I'd appreciate your accepting my answer; I went to a lot of trouble with this.
Comment: Your answer pushed me to the right direction. I think there remains some work on my part to understand how. I say that because according the [CGAffineTransform](https://developer.apple.com/documentation/coregraphics/cgaffinetransform) and [concatenating](https://developer.apple.com/documentation/coregraphics/cgaffinetransform/1455996-concatenating), `T1.concatenate(T2)` gets applies to point `[x y 1]` as `[x' y' 1] = [x y 1] * T1 * T2`, which means from an algebraic point of view that `T1` gets applied first, then `T2`. Your insight is a more intuitive geometric view that appears to be correct
Comment: Right, pedagogically I'm explaining it in terms of what you see happen, which anyone can reason about, not in terms of linear algebra which most people didn't take or don't remember.
Comment: @matt Expressing things in terms of the already-transformed space is somewhat confusing, and counter-intuitive to anyone who's used 2D or 3D graphic software— we're used to transforms that a relative to a parent in a object hierarchy (or worldspace if a root-level object) and transform ordering where scale doesn't affect the translation amount; rotation doesn't affect the translated position, and rotation doesn't skew the scale.  Internally, thinking in local space may be what's happening, but user-focused software tends to soften that into a "parent"/"world"-space conceptualization.
Comment: @matt So you're absolutely right about how Apple and OpenGL does this stuff, of course, but _"I'm explaining it in terms of what you see happen, which anyone can reason about, not in terms of linear algebra which most people didn't take or don't remember"_ is backwards.  You're actually explaining it in terms of the linear algebra math (necessary to know to be able to do this programming), **not** the TSR paradigms people are used to in Unity, Unreal, Blender, etc.  I'm sorry, distorting space with each T,S,R operation is not intuitive; having each relative to a parent's space is intuitive.
Comment: @SlippD.Thompson Ignoring the discussion in the comments (which is volatile), can you think of a way in which I might reword my _answer_ more correctly / helpfully? Perhaps I should add a sentence about "this is backwards from..." if I knew how to phrase it. Thanks!
Comment: @matt I don't know.  You answer made sense to me, but I've worked extensively with both 3D software and low-level 3D APIs (e.g. OpenGL).  To me, it's just one of the many equivalent-but-different ways of computing things in 3D (e.g. right-hand vs. left-hand, matrix pre- vs. post-multipliation, column-major vs. row-major matrices, Y-up vs. Z-up).  I'm not even used to thinking about S,R,T order— I'm used to a studio having a standard ordering (TRS, SRT, etc.) and just following the standard all the libs use, instead of thinking through the linear math possibilities of it every time.
Comment: @matt So the best suggestion I'd come up with is to suggest that in order to get the expected result (no skewing, or scaled translations), the programmer always use a certain order of operations with Apple's `CGAffineTransform`— translate 1st, then rotate 2nd, then scale 3rd (assuming you're transforming the space the next operation is working in)… but that's not really relevant to the question, and it sounds like your book covers this stuff pretty well.  Maybe the best improvement would just be to add a black crosshairs to the image in the centerpoint of the red box, with black text "origin".
Comment: @matt Or at the very least, clarifying that most 3D software applies the TRS in that order so the end-result appears to be relative to the parent's coordinate space (since we have a human intuition to scale-rotate an object first, then position it in a space, and if a non-uniform-scale and rotation are in the wrong order, you get skewing).  So yeah, I don't really know how to improve the answer itself, since these are confusing concepts, there's competing methodologies in each engine, and I'm personally not innately familiar with Apple's approach (I've used `CGAffineTransform` rarely).  ;-/


Displaying data by views from external API

Question: I have made 2 db's one is of Drupal 8 and another is external, that is non-SQL(CouchDB) and to manage this DB I made an API with different endpoint(URLs) witch I can axes them by GET/POST methods. After correct request I get response
 in JSON from my API.
When I create for example a user, content(node) or something else I save some info in my Drupal database and some info in my API db (I made alteration of the forms that create content by hook_form_BASE_FORM_ID_alter and this way I create content in my external DB).
My question is how to Display a data from a views, combining information form two Data-Bases?
Comment: Has anyone had problem like this?
Answer: There are modules like Views Database Connector, External Entities in combination with Search API for views integration or Web Service Data - dunno about it's views integration.
Remote Entity API is not yet ported to drupal 8. See: Drupal entity from different database
Ideas for custom coding are: Views Query Backend or Search API (or write your own search api backend - Maybe via solr)

In either way think of caching. As making request to your API in every
  request to your site is very expensive.
Answer: If all you need to do is display data from the API, then you don't need to do anything special within views.  Just select that you'd like to view rendered entities of your users with a specific view mode.

In the preprocess function for that view mode, you can hit the API and load the additional data that you need.  Data loaded within a preprocess function should cache, so it shouldn't hit the API on subsequent page loads.
<code>function mymodule_preprocess_user(&$variables) {
  /** @var $account \Drupal\user\UserInterface */
  $account = $variables['elements']['#user'];

  // custom code to hit the 3rd party API
</code>
Answer: First caveat.  It is difficult to query across databases. Views by default does sql queries. Any kind of query that requires coalescing the data from both is 2 custom queries and a merge in PHP. This is not something that views is going to help you with.
There are a lot of approaches that will help you with this. I'd tackle it by building JSON data sources and using AngularJS to present the data.
2x sql sources. 2 sql data sources can be mapped as per drupal 7.
Use the query interfaces and massage the data. Return as JSON.
The question leads to answers that map database tables to views, and there are tools to do this. I don't think you will get a views 'experience' form separate databases.
Answer: Maybe you can post some more details, but from what I understand you are storing additional information per node on an external database. You already have developed an API system for communicating with that database, so you don't really need to issue queries from Drupal directly to that database. In that case, one way to proceed this would be the following.
Configure Views to use a display mode to render your entity. On your node preprocess function, issue the API request and fetch the additional data from the 2nd database. Prepare the data and make them available to the template.
<code>/**
 * Implements hook_preprocess_node(&$variables).
 */
function MY_THEME_preprocess_node(&$variables) {
  // Issue API request.
  // Process JSON response.
  $variables['additional_field'] = $request_result;
}
</code>
You can then override the template for that entity (node) and display mode to render your additional fields.
If you need Views to use field display instead of rendering a display mode, you will need to develop a custom field handler that fetches the data from the external API. Let us know if you need help with that.
An alternative would be to use Views hooks to prepare the data in a similar way earlier in the process, but there's not really much of an advantage with that approach. That is, unless you have the capability to fetch all data for all results in a single API request. That would be the most efficient so that you don't issue multiple requests, one per node result. Let us know if this the case and if you need help with that.


Normalization the data before applying statistical test for large sample size

Question: From my perspective, the reason p-value of a statistical test isn't useful in large sample scenario is because it will change according to the scale.
E.g. let's focus on chi-square test. In a chi-square test, the chi-square score changes linearly w.r.t. the scale of the frequencies (I mean, multiply 10 for each cell in the contingency table, the chi-square will also be 10 times larger). So when the data size is larger, even it has the same ratio/proportion, the p-value becomes smaller.
Thus, in this sense, why don't we just set a standard value, e.g. 1000 or 10000, and do a linear transform to normalize the total sample size to this value before we do the chi-square test. Then I suppose the p-value makes sense again. And scores like Cramer's V or Cohen's h or odds ratio remains the same.
What's wrong with my idea? (I don't know why the number in the table must be counts for chi-square. If this is a problem, maybe think of other tests like ANOVA)
Any idea/thought is appreciated:)
Answer: The point is that the sample size is actually informative. If you have the same relative departures from row-column independence, but a 10 times larger sample size, you have much more evidence against the null hypothesis of row-column independence. This is rightly reflected in your $\chi^2$ statistic and corresponding p-value.
Artificially changing the sample size as you propose would incorrectly change the certainty on the estimates.
Finally, the entries in the contingency table are counts by construction. The fact that you have counts yields an "automatic" assumption that the variance equals the mean, needed to standardize the residuals in the calculation of the $\chi^2$ statistic.
Comment: Thanks! I think it's a very good point. It's definitely invalid to normalize when data size is small. The sample size is very informative in this case. But is it still the same when the sample is already large enough?

For large sample size, the problem is actually the significance indicated by p-value is exaggerated. I don't think it makes a huge difference between 10,000 and 100,000 samples, I guess, the growth of information from the sample size will decrease as the sample size becomes larger. Maybe once it's larger than certain threshold, the sample size doesn't matter that much.
Comment: My point is: the information we get from sample size is not a linear function of the sample size. In classical statistical measure tools, e.g. Cramer's V for chi-square, it normalized the chi-squared score by number of samples, which (I think) assumes a linear relationship between information gain and sample size. But I believe it's invalid for large sample. Is the information difference between 10 and 100 samples the same as 100,000 and 1,000,000 samples? Maybe not. Finding the real relationship and normalizing correspondingly provides a way to better use these statistical tools for big data.


$3\times3\times3$ hypermatrix multiplication

Question: Here's an image showing what I am trying to do:

The two hypermatrices are multiplied together by taking appropriate slices from each hypermatrix, and realised into a vector by an associated vector, given by (a,b,c) in this case.
But I can't figure out how to do the simple matrix multiplication. For example red times green times vector equals blue, but there doesn't seem to be an obvious orientation for the matrices.
Comment: What do you want your multiplication operation to be useful for? For example ordinary matrix multiplication can be motivated as composing the linear transformations that the operand matrices represent -- do you have a similar interpretation for your hypermatrices that can be used to guide your choice of multiplication operation?
Comment: A multi-dimensional matrix or vector is called [tensor](https://en.wikipedia.org/wiki/Tensor), so maybe you should check out [tensor product](https://en.wikipedia.org/wiki/Tensor_product).
Comment: What does this look like in terms of sums and indices?
Comment: @HenningMakholm; i'm just chuffed it seems to work at the moment. if i bring both colors to the front, we get the usual presentation
Comment: I believe this is a case where it is easier to define the resulting "hypermatrix" by using summation notation and indexes.  A preferred orientation of the matrices may be hard to impute by the pictorial method.  Presumably you want the horizontal $3\times 3$ slice and the vertical $3\times 3$ slice to produce one-ninth of the result, but I don't see a criterion that would give use the three blue dots in your image.
Comment: I have tried embedding a hypercomplex number system into the matrices in an analogous way that complex numbers sit in 2x2 matrices (or 3x3 matrices) and then use the hypercomplex multiplication to motivate a generalization for 3x3x3 matrices.  I'm still working on it.
Comment: @hardmath; red$\cap$green?
Comment: @JonMarkPerry: But before you can conclude that it "seems to work", you need to come up with a criterion for what "work" _means_ in this context. Simply generalizing the rank-2 case when one of the dimensions is $1$ doesn't mean much unless you have something that actually _generalizes_ to other dimensions..
Comment: Yes, but I suspect it would be clearer to write it out so you produce one dot at a time. The front three red dots "dotted" with the front three green dots give us the front blue dot, etc.
Comment: "Work" could mean that the multiplication is associative and that the resulting sytem produces a determinant that is multiplicative.
Comment: @hardmath: The problem with using the kind of summations usually considered in multilinear algebra is that we have 6 indices to the left and 3 to the right, so we have to get rid of an _odd_ number of indices by contracting, which is a bit unconventional. When the dimension is 3 we could throw in an $\epsilon_{ijk}$, corresponding to multiplying matrices with 3d vectors as elements and using cross products in place of products in the ordinary formula -- but that doesn't immediately generalize to dimensions other than 3.
Comment: @hardmath; i see your point, but i want to know how to do it my way
Comment: Note that currently you're diagram simply describes 2-d matrix multiplication 3 times "deep". I.e. ignore all but the front face and you see normal 2-d matrix multiplication, and then you can see it again in the middle slice and finally in the back face.

You need to specify what you want the result to be or do; i.e how it is to behave, and/or at least mention how much of the final result should vary when varying each of the points in the multiplicands.
Comment: I think the main problem is that we ought to expect that, regardless of the orientation of the multiplication, the result should always be the same, which I imagine will prove to be difficult.
Comment: Is there a continuous version of hypermatrices that allows for a similar product?
Answer: Think of two 2x2 matricies. There's no 'intrinsic' orientation either. Depending on your SPECIFIC matrix definitions you INTERPRET a secondary concept of orientation based on you application, but structurally, all that matters is 'associative order' (which matrix is on the left vs right).
Beyond that question, this can be seen as a basic abstraction where all operational structures increase by 1 dimension. So, in 2d, have 1d horizontal slice (row) x 1d verical slice (column) = 0d scalar (element in resulting 2d matrix). Here its 2d horizontal slice (row sheet/plane) x 2d vertical slice (column sheet) = 1d element in resulting 3d matrix).
Similarly, you'd expect absraction to nd via n-1 horizontal slice x n-1 verical slice = n-2 element in resulting nd matrix. Tensorially it'd be like a tensor product followed by a contraction (for tensors, contraction reduces dimension (rank) by 2)
Comment: Welcome to Math.SE.  You will find the ability to [post with mathematical expressions](https://math.stackexchange.com/help/notation) useful as you contribute Questions and Answers.  I don't get much illumination out of what you wrote, but then the Question itself was largely pictorial in presentation and lacked the clarification requested as to what the intended "multiplication" would apply to.  Certainly there is a central issue of how two $3\times 3$ "slices" should produce a $3\times 1$ result, a concern highlighted in the Question itself. A good Answer would shed some light on that issue.


Deterministic trend and noise

Question: I have a chicken-egg problem in time series:
How do we remove the deterministic trend in time series when you have a guess that the noise is non-normal? How do we check if the noise is non-normal without removing the deterministic trend?
For my time-series, when I plot the histogram of the raw values and fit a density a log-normal seems to fit very well. However, this is raw data, and it has a trend, and potential seasonality. If I remove the trend and seasonality, would the resulting density still remain a log-normal? How would I remove the trend and seasonality if I don't know the noise distribution? If my noise is indeed log-normal, then I don't think I can use an OLS regression, and have to use a GLM (which seems not to handle log-normal, somehow, but that's a separate question). Any thoughts on this, please?
I hope I have made my  question clear, but please comment if you need more information.
Answer: If a series $\{ x_t\}_{t=1}^\infty$ has a linear deterministic trend, it can be written $x_t=y_t+k t$ . An estimator for $k$ is $k=\frac{X_T - X_t}{T-t}$. To remove the trend from data, you take $y_t=x_t-kt$.
Comment: You may need a further explanation to show that OP's concern about chicken and egg is not a problem here.
Comment: Thanks for your comments. In line with @Aksakal, even if you assume the trend is linear, you need an assumption on the noise to show that $k$ or an averaged or OLS version of $k$ is the right estimate. For instance, if your distribution looks lognormal, then we may prefer X_T/X_t as opposed to a subtraction (not claiming this is how it should be done, but just an example).

Deterministic trend and noise


databricks avro schema cannot be converted to a Spark SQL structtype

Question: we have kakfa hdfs connector writing into hdfs in default avro format. A sample o/p:

Obj^A^B^Vavro.schema"["null","string"]^@$ͳø{<9d>¾Ã^X:<8d>uV^K^H5^F°^F^B<8a>^B{"severity":"notice","message":"Test message","facility":"kern","syslog-tag":"sawmill_test:","timestamp":"2017-01-31T20:15:00+00:00"}^B<8a>^B{"severity":"notice","message":"Test message","facility":"kern","syslog-tag":"sawmill_test:","timestamp":"2017-01-31T20:15:00+00:00"}^B<8a>^B{"severity":"notice","message":"Test message","facility":"kern","syslog-tag":"sawmill_test:","timestamp":"2017-01-31T20:15:00+00:00"}$ͳø{<9d>¾Ã^X:<8d>uV^K^H5

Trying to read using:
import com.databricks.spark.avro._
    val df = spark.read.avro("..path to avro file")

we get the following error

java.lang.RuntimeException: Avro schema cannot be converted to a Spark SQL StructType:
  [ "null", "string" ]
    at com.databricks.spark.avro.DefaultSource.inferSchema(DefaultSource.scala:93)
    at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$7.apply(DataSource.scala:184)
    at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$7.apply(DataSource.scala:184)
    at scala.Option.orElse(Option.scala:289)
    at org.apache.spark.sql.execution.datasources.DataSource.org$apache$spark$sql$execution$datasources$DataSource$$getOrInferFileFormatSchema(DataSource.scala:183)
    at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:387)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:152)
    at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:135)
    at com.databricks.spark.avro.package$AvroDataFrameReader$$anonfun$avro$2.apply(package.scala:34)
    at com.databricks.spark.avro.package$AvroDataFrameReader$$anonfun$avro$2.apply(package.scala:34)

Please help
Spark version: 2.11
Spark-avro version: 2.11-3.2.0
kafka version: 0.10.2.1
Answer: your file in avro format is not proper. The schema must be type:record.  Am unable to attach a valid avro format file sorry for that.
But you can refer this link. Sample Avro format file
avro.schema¨{"type":"record"

And from spark what ever you have done is fine.
import com.databricks.spark.avro._
sqlContext.read.avro(..paht of file)


Is $ f_n=\frac{(x+1)^n-(x^n+1)}{x}$ irreducible over $\mathbf{Z}$ for arbitrary $n$?

Question:
In this document on page $3$ I found an interesting polynomial:
  $$f_n=\frac{(x+1)^n-(x^n+1)}{x}.$$
  Question is whether this polynomial is irreducible over $\mathbf{Q}$ for arbitrary $n \geq 1$.

In the document you may find a proof that polynomial is irreducible whenever $n=2p , p \in \mathbf{P}$ and below is my attempt to prove that polynomial isn't irreducible whenever $n$ is odd number greater than $3$. Assuming that my proof is correct my question is :
Is this polynomial irreducible over $\mathbf{Q}$ when $n=2k , k\in \mathbf{Z^+}$ \ $\mathbf{P} $ ?

Lemma 1: For odd $n$, $a^n + b^n = (a+b)(a^{n-1} - a^{n-2} b + \cdots + b^{n-1})$.
Binomial expansion: $(a+b)^n = \binom{n}{0} a^n + \binom{n}{1} a^{n-1} b + \cdots + \binom{n}{n} b^n$.
$$\begin{align*} f(x)  &= \frac{(x+1)^n - x^n-1}{x} = \frac{(x+1)^n - (x^n+1)}{x}
\\ &= \frac{(x+1) \cdot (x+1)^{n-1} - (x+1)(x^{n-1}-x^{n-2}+ \cdots + 1)}{x}
\\ &= \frac{(x+1) \cdot (x+1)^{n-1} - (x+1)(x^{n-1}-x^{n-2}+ \cdots + 1)}{x}
\\ &= \frac{(x+1) \cdot \left[ \Bigl(\binom{n-1}{0}x^{n-1}+ \binom{n-1}{1} x^{n-2} + \cdots + 1 \Bigr) - (x^{n-1}-x^{n-2}+ \cdots + 1) \right]}{x}
\\ &= \frac{(x+1) \cdot \left[ \Bigl(\binom{n-1}{0}x^{n-1}+ \binom{n-1}{1} x^{n-2} + \cdots + \binom{n-1}{n-2} x \Bigr) - (x^{n-1}-x^{n-2}+ \cdots - x) \right]}{x}
\\ &= (x+1) \cdot \small{\left[ \left(\binom{n-1}{0}x^{n-2}+ \binom{n-1}{1} x^{n-3} + \cdots + \binom{n-1}{n-2}  \right) - (x^{n-2}-x^{n-3}+ \cdots - 1) \right]} \end{align*}$$
So, when $n$ is an odd number greater than $1$, $f_n$ has factor $x+1$. Therefore, $f_n$ isn't irreducible whenever $n$ is an odd number greater than $3$.
Comment: About the case when $n$ is odd: $f_n(-1)=0$ hence $x+1$ divides $f_n(x)$, end of the proof.
Comment: The document says that it is an open problem.
Comment: It is open because it has not yet been determined for all even $n$.
Comment: When you ask for $f_n$ to be irreducible, does this include constant factors?  It appears that the content of $f_n$ is $1$ unless $n=p^k$ is a prime power, in which case the content is $p$.
Comment: @A. Walker: You are right. It is only interesting to ask for irreducibility over $\mathbb{Q}$. From $f_4 = 2 (2 x^2 + 3 x + 2)$ I infer that actually this was meant, therefore I've edited the question.
Comment: Lol @Did that was nice.
Comment: Is this problem still open?
Comment: I don't know if this is a dumb comment, but the form caught my eye because without dividing by $x$, its in the form of the AKS test, $(x+1)^{n}-x^{n}-1,$ which is congruent mod $p$ iff $n=p^{k}$ for prime $p$. Ignore me if this doesn't help.
Comment: The link in the question is dead. According to Sage $f_n$ is irreducible for all even $n$ from $4$ to $1000$.
Answer: For even $n$ we bound the function from below to show it has no real roots and thus no rational roots.
First, note that for $x\ge 0$
$$f_n(x) = \sum_{k=0}^{n-2}{n\choose k+1} x^k
= n + \ldots
\ge n,$$
where the function expressed by $\ldots$ is clearly positive semidefinite.
(We take the sum above to be the definition of $f_n$ as implied by the problem statement, i.e, $f_n(0)=n$.)
Thus, the function has no real roots for $x\ge 0$.
One can show that
$$f_n(x) = \sum_{j=0}^{(n-2)/2}
\frac{n(n-j-2)!}{(j+1)!(n-2j-2)!}
(-x)^j (x+1)^{n-2j-2}.$$
For even $n$ and $x\in (-\infty,-1)\cup(-1,0)$ each term in the sum is explicitly positive definite.
In addition, $f_n(-1)=2$.
Therefore, for even $n$, $f_n$ has no real roots.


Posso rodar o ASP.NET no Nginx sem o Kestrel?

Question: É possível hospedar o meu site ASP.NET diretamente no Nginx, igual faria com o PHP? Sem a necessidade de usar o Kestrel e Nginx apenas como um proxy reverso.
Comment: A resposta resolveu o que estava em dúvida? Precisa que algo mais seja melhorado? Acha que é possível aceitá-la agora?
Answer: Boa pergunta.
Isso indica que não, mas não tenho certeza se for usar o Nginx de forma tradicional. Só que eu procurei e não achei nada que confirmasse que dê.
O que eu posso te dizer com certeza é que se for usar o Nginx como proxy reverso, o Kestrel ou outro mecanismo semelhante a ele, é obrigatório.
Dá para fazer se você criar sua própria implementação do OWIN, mas não sei se é o que deseja, ou se vale a pena. Pra falar a verdade eu acho que não é necessário fazer isso. Hoje eu acho que apenas não dá, de forma tradicional, só porque ninguém criou um OWIN para isso.
O que eu sei é que logo poderá usar o YARP e talvez seja o melhor caminho, estão criado ele porque o Nginx se mostrou limitante. Então não tome o Kestrel como um dificultador, abrace-o.


CKEditor unwanted   characters

Question: How can I disable CKEditor to get me every time  , when i don't want them? I'm using CKEditor with jQuery adapter.
I don't want to have any   tags.
Comment: It also adds   in empty table cells which breaks some email designs.
Comment: How will it know which ones you want and which ones you don't want? Do you want to remove them *all*, or reduce multiples, or something else? How are they getting there in the first place?
Comment: I just don't want them to be added.. all of them
Comment: OK, just wanted to clarify. So when a user presses the space bar, it will never add more than one space? In other words, you don't want to have to do this on the server side, correct? I suppose the best way would be to turn them all into regular spaces (just in case some are separating two words), but I'm not sure how to do it with ckeditor specifically.
Comment: When i load some html, go to the wysywyg part of the editor, then get back to the html part of the editor - it's adding nbsp tags, to lot's of places, i know why it adds them, I've searched about the problem, but no solution worked
Answer: After some research I might shed some light on this issue - unfortunately there is no out-of-the-box solution.
In the CKEditor there are four ways a no-break space can occur (anybody know more?):

Automatic filling of empty blocks. This can be disabled in the config:
config.fillEmptyBlocks = false;

Automatic insertion when pressing TAB-key. This can be disabled in the config:
config.tabSpaces = 0;

Converting double spaces to SPACE+NBSP. This is a browser behavior and will thus not be fixed by the CKEditor team. It could be fixed serverside or by a clientside javascript onunload. Maybe this php is a start:
preg_replace('/\s \s/ig', ' ', $text);

By copy & paste. If you paste a UTF-8 no-break space or double-spaces CKEditor will convert it automatically. The only solution I see here is doing a regex as above. config.forcePasteAsPlainText = true; doesn't help.

Summary: To get rid of all no-break spaces you need to write an additional function that cleans user input.
Comments and further suggestions are greatly appreciated! (I'm using ckeditor 3.6.4)
Comment: `config.fillEmptyBlocks = false;` is Not stopping the problem for me
Comment: config.fillEmptyBlocks = false; takes care of   auto-inserted into empty table cells. Thank you!
Comment: I think perhaps your regex should be `/\s* \s*/ig` to replace more than 1 instance within a string and also to account for more scenarios of mixing space(s) with ` `. So, something like `watermelon  flavored  drink` (which can happen often) would get replaced, whereas with your regex it would not. Also the * should be used in front of each `\s` since html only renders one space anyways.
Comment: A [Webkit bug](https://bugs.webkit.org/show_bug.cgi?id=123163) when formatting or deleting mid-word, see [this CK-ticket](http://dev.ckeditor.com/ticket/9929#comment:15) for clear example-descriptions.
Comment: for my CKEditors, 'config.fillEmptyBlocks = false;' did not work but 'config.basicEntities = false;' did
Answer: There is another way that a non breaking space character can occur. By simply entering a space at the end of a sentence.
CKEditor escapes basic HTML entities along with latin and greek entities.
Add these config options to prevent this (you can also add them in your config file):
CKEDITOR.on( 'instanceCreated', function( event ) {
 editor.on( 'configLoaded', function() {

  editor.config.basicEntities = false;
  editor.config.entities_greek = false;
  editor.config.entities_latin = false;
  editor.config.entities_additional = '';

 });
});

These options will prevent CKEditor from escaping nbsp gt lt amp ' " an other latin and greek characters.
Sources:
http://docs.ckeditor.com/#!/api/CKEDITOR.config
http://docs.ckeditor.com/source/plugin48.html#CKEDITOR-config-cfg-basicEntities
Comment: config.basicEntities = false will prevent typed HTML from being escaped and so people will simply be able to type in HTML that would otherwise be filtered / rendered harmless. (That's implicit as you said it stops gt and lt being escaped, and really you shouldn't trust any user-entered data, not even if it came via ckEditor, but thought it worth mentioning.)
Answer: Try:

config.basicEntities = false;

for me fixed the problem.
Comment: See also my comment on basicEntities=false letting through arbitrary HTML on @gabriel's answer.
Comment: i believe that his answer was straight to the point, and this line alone solved the problem without needless distractions
Comment: @gabriel already included this in his answer 17 months before you added this. A comment (and upvote) on his answer would have been better because repeating it is redundant and wastes readers' time, not to mention that it makes it look like you didn't check other answers.
Answer: in config.js:
CKEDITOR.editorConfig = function( config ) {
  config.enterMode = CKEDITOR.ENTER_BR; //

 to

  config.entities = false;
  config.basicEntities = false;
};

It work for me, after you can print text with php: html_entity_decode( $someText );
Answer: I noticed some text editing operations, like deleting a character (by hitting Backspace button) are spliting edited text node into two. Hitting Space Bar at the end of such newly created text node is always resulting into   instead of normal space. I am calling normalize() http://www.w3schools.com/jsref/met_node_normalize.asp to changed element after change:
CKEDITOR.on('instanceReady', function (ck) {
    ck.editor.on("change", function (e) {
        var sel = ck.editor.getSelection();
        if (sel) {
            var selected = sel.getStartElement();
            if (selected && selected.$)
                sel.getStartElement().$.normalize();
        }
    });
 });

Answer: This is a bad solution
config.basicEntities = false;

Because it does not allow you to insert the JS code in the form as text. Like that
In addition, empty blocks like

 сan not specify an indent in the text since the character   will be deleted (config.fillEmptyBlocks = true;)

This is the right solution
$text = preg_replace("#([^>]) #ui", "$1 ", $text);

This is a PHP function that replaces all the characters   on a space, except those that are inside the tag like

The function code is not the most elegant, you can suggest your own version.
Answer: I had the same problems creating some tables. What I saw was that if i created the tables with the css rule align="left" the

 are added, but if i changed the css rule to align="center" i could edit the paragraphs out and they were not added again.
Answer: I had already had to play around with config.js,  so in order to fix '?' showing up in safari I ended up with 3 lines in config.js
config.fillEmptyBlocks = function (element) {
return true; // DON'T DO ANYTHING!!!!!};
config.entities = false;
config.basicEntities = false;

Answer: If you're using PHP you can use the following :
preg_replace("/[\<]p[\>][\s]+ [\<][\/]p[\>]/" , " " , $pre_comment);

This will remove : "

"
Enjoy :)
Maxwell
Comment: I don't want to downvote this answer because you're new here but please keep in mind... `1.` question is already answered correctly! (when you don't hold better answer don't answer at all, especially on old questions) `2.` this pattern contains unnecessary `[]` brackets (all of them), unnecessary escape sequences `\>`, and most probably `\<` and unnecessary `+` after `\s` (pattern `~\s ~` looks shorter right?). `3.` if you want to be sure there's one regular space inside `P` tags, replacing `~\s* \s*~i` case-insensitively with SPACE is better suggestion (and shorter).
Answer: Add this to your config.js
config.enterMode = CKEDITOR.ENTER_BR,

CKEditor unwanted   characters


bsearch() comparing by name

Question: I have a struct employee comparators and main:
#define MAX_SIZE 20
typedef struct Employee{
    char name[MAX_SIZE];
    int salary;
    int experience;
} employee_t;

void main()
{
    int i;

    employee_t** employeeArray = (employee_t**)malloc(sizeof(employee_t*)*5);
    employee_t nonExstingEmployee = {"v"};

    for(i=0 ; i < 2 ; i++)
    {
        employeeArray[i] = (employee_t*)malloc(sizeof(employee_t));  //alocate each employee
        readEmployee( employeeArray[i] );
    }

    puts("beforesort\n");

    for(i=0; i <2 ; i++)
    {
        printEmployee(employeeArray[i]);
    }

    puts("after salary sort\n");
    qsort(employeeArray, 2, sizeof(employee_t*), compareEmployeesBySalary);
    for(i=0; i < 2 ; i++)
        printEmployee( employeeArray[i] );

    if (bsearch(&nonExstingEmployee, employeeArray, MAX_SIZE, sizeof(employee_t), compareEmployeesByName) == 0)
        puts("employee found");
    else
        puts("no employee found");

}

    int compareEmployeesBySalary(const void* a, const void* b){
            employee_t* one = *(employee_t **)a;
            employee_t* two = *(employee_t **)b;

            if (one->salary == two->salary)
                return 0;
            else if (one->salary > two->salary)
                return 1;
            else
                return -1;
    }

    int compareEmployeesByName(const void* a,const void* b)
    {
        employee_t* one = *(employee_t**)a;
        employee_t* two = *(employee_t**)b;

        if(strcmp(&one->name, &two->name) == 0)
        {
            return 1;
        }

        return 0;
    }

I need to use bsearch comparing the names (with some employee that does not exist in the array), my qsort function works(comparing by salary)
no matter how I initialize the employee for bsearch it always says its not there (I'm trying to test this with existing employee first),
How should I initialise my employee (or what should I change?)

[update from comment:]
This is my readEmployee():
void readEmployee(employee_t *emp)
{
  fflush(stdin);
  printf("Please enter Employee's name:(max 19 chars)");
  scanf("%s", emp->name);

  fflush(stdin);
  printf("Please enter Employee's salary:");
  scanf("%d", &(emp->salary));

  fflush(stdin);
  printf("Please enter Employee's experience:");
  scanf("%d", &(emp->experience));
}

I'm comparing the nonExstingEmployee (which exist in the array for testing) and my employeeArray.
Comment: For starters: This `if(strcmp(&one->name, &two->name) == 0)` does not look good, The compiler should have yelled a warning out you.
Answer: The problem is simple: you qsort by salary yet bsearch by name. It won't work.
bsearch performs binary search in sorted array. Not any sorted array but an array sorted according to the search criteria. You must either previously perform qsort by name or stick to lsearch (that is linear search which doesn't depend upon previous sorting).
Comment: Doh, yes, could catch! Totally missed this ... :}
Answer:
How should I initialise my employee

You might want to show us how you do it (readEmployee()?), and we would comment on this.

(or what should I change?)

Two issues:

The array to be searched is not MAX_SIZE long, but 2.
So this line
if (bsearch(&nonExstingEmployee, employeeArray, MAX_SIZE, sizeof(employee_t), compareEmployeesByName) == 0)

should be
if (bsearch(&nonExstingEmployee, employeeArray, 2, sizeof(employee_t), compareEmployeesByName) == 0)

In compareEmployeesByName() you pass the "wrong" address.
This line
if(strcmp(&one->name, &two->name) == 0)

should be
if(strcmp(one->name, two->name) == 0)

Comment: this is my readEmployee:
`void readEmployee(employee_t *emp)
{
 fflush(stdin);
    printf("Please enter Employee's name:(max 19 chars)");
    scanf("%s", emp->name);
 fflush(stdin);
    printf("Please enter Employee's salary:");
    scanf("%d", &(emp->salary));
 fflush(stdin);
    printf("Please enter Employee's experience:");
    scanf("%d", &(emp->experience));

}`
im comparing the nonExstingEmployee  (which exist in the array for testing) and my employeeArray

bsearch() comparing by name


Trying to set up haproxy letsencrypt for ssl termination. gateway not answering dns

Question: trying to set up ssl termination with letsencrypt, but some weirdness is happening with dns. I am routing tcp to the existing host to allow the website at adomain.com to manage its own certs until the proxy can --dry-run without errors.
here is my haproxy.conf as is:
frontend http-frontend
    bind *:80
    timeout client 1m
    mode http
    default_backend letsencrypt-backend

frontend https-frontend
    bind *:443
    timeout client 2m
    option tcplog
    mode tcp

    acl is_knight hdr_end(host) -i adomain.com
    acl is_oer  hdr_end(host) -i adifferentdomain.com

    default_backend knight

backend letsencrypt-backend
    mode http
    timeout server 1m
    server letsencrypt 127.0.0.1:8888

backend knight
    timeout server 1m
    mode tcp
    server knightwww 192.168.0.2:443

and I'm trying to get ssl certs with
sudo certbot certonly --standalone -d adomain.com -d knight.jr1.ca --non-interactive --agree-tos --email firstname.lastname@example.com --http-01-port=8888 --dry-run

and the response is
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='acme-staging-v02.api.letsencrypt.org', port=443): Max retries exceeded with url: /directory (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f813819fb00>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))

This had me worried, so I ran a test on leafDNS
The parent nameservers do not have your nameservers listed
This means the name does not exist or your registrar is experiencing technical difficulties.

I just checked the nameservers listed and they contain A-records pointing to the gateway's public IP address
What am I missing?
Answer: This was caused by something overwriting /etc/resolve.conf..
after some hunting the answer was here:
https://askubuntu.com/a/1001295/946188


Android - How to request location update in the new API?

Question: I was playing a bit around the new location API from google (to get latitude and longitude values):
private void getLastLocation(){
    FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    try {
        mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if (location != null) {
                    double latitude = location.getLatitude();
                    double longitude = location.getLongitude();
                    Toast.makeText(getApplicationContext(), String.valueOf(latitude) + "/" + String.valueOf(longitude), Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Cannot get location", Toast.LENGTH_LONG).show();
                }
            }
        })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d("LocationFetch", "Error trying to get last GPS location");
                        e.printStackTrace();
                    }
                });
    } catch (SecurityException e){
        Log.d("LocationFetch", "Permission missing");
    }
}

When I first tested this code, the location was always returning null. However, after opening Instagram (which did a location update on my phone - the geo icon appeared briefly), the location was returning my relevant longitude and latitude values.
How do I request a location update on my app using the new API to prevent location from being null or retrieve very old locations? (getLastLocation() is not enough, possibly LocationRequest?)
It is worth noting that I do not want interval updates, I want for it to happen within an Android Service when invoked once.
Comment: use GPSTracker its very essay
Comment: Check this url may be it will help's you
https://javapapers.com/android/android-location-fused-provider/
Comment: Take a look here https://stackoverflow.com/questions/53300064/how-to-get-current-location-and-not-last-location/53300121#53300121 and here https://stackoverflow.com/questions/53276698/android-my-app-can-get-location-information-until-opening-google-maps-although/53276863#53276863
Comment: Basically you need to create your own location request. Because getLastLocation tries to get location from other apps, and if it exist you will get null.
Answer: Firstly add this implementation in your build.gradle file
implementation 'com.google.android.gms:play-services-location:11.2.0'//include the latest version of play services

After that implement, (implements LocationListener) in your activity or fragment and after that implement its function and then
call this method in your onCreate() getLocation();
and then in this function add these lines
protected void getLocation() {
    if (isLocationEnabled(MainActivity.this)) {
        locationManager = (LocationManager)  this.getSystemService(Context.LOCATION_SERVICE);
        criteria = new Criteria();
        bestProvider = String.valueOf(locationManager.getBestProvider(criteria, true)).toString();

        //You can still do this if you like, you might get lucky:
        Location location = locationManager.getLastKnownLocation(bestProvider);
        if (location != null) {
            Log.e("TAG", "GPS is on");
            latitude = location.getLatitude();
            longitude = location.getLongitude();
            Toast.makeText(MainActivity.this, "latitude:" + latitude + " longitude:" + longitude, Toast.LENGTH_SHORT).show();
            searchNearestPlace(voice2text);
        }
        else{
            //This is what you need:
            locationManager.requestLocationUpdates(bestProvider, 1000, 0, this);
        }
    }
    else
    {
        //prompt user to enable location....
        //.................
    }
}

After that in your onLocationChanged(Location location)
add these lines of code
@Override
public void onLocationChanged(Location location) {
    //Hey, a non null location! Sweet!

    //remove location callback:
    locationManager.removeUpdates(this);

    //open the map:
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    Toast.makeText(MainActivity.this, "latitude:" + latitude + " longitude:" + longitude, Toast.LENGTH_SHORT).show();
    searchNearestPlace(voice2text);
}

and you are set to go!!!!
Cheers Happy Coding
Answer: It's work for me. Based on Official docs, every 10 seconds updates the Toast and shows your location:
private FusedLocationProviderClient fusedLocationClient;
private LocationRequest locationRequest;
private LocationCallback locationCallback;

@Override
protected void onResume() {
    super.onResume();
    startLocationUpdates();

}

private void startLocationUpdates() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    Activity#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for Activity#requestPermissions for more details.
            return;
        }
    }
    fusedLocationClient.requestLocationUpdates(locationRequest,
            locationCallback,
            Looper.getMainLooper());
}

@Override
protected void onPause() {
    super.onPause();
    stopLocationUpdates();
}

private void stopLocationUpdates() {
    fusedLocationClient.removeLocationUpdates(locationCallback);
}

add bellow codes to onCreate method:
    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(2*5000);

    locationCallback=new LocationCallback(){
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                return;
            }
            for (Location location : locationResult.getLocations()) {
               // if (location != null) {

                    String Lat = String.valueOf(location.getLatitude());
                    String Lon = String.valueOf(location.getLongitude());

                    Toast.makeText(getApplicationContext(), Lat + " - " + Lon, Toast.LENGTH_SHORT).show();
            //    }
            }
        }
    };

Answer: Try this code.
In gradle
implementation 'com.google.android.gms:play-services-location:16.0.0'

In code
 private void turnGPSOn() {
    LocationRequest mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(10 * 1000)
            .setFastestInterval(1 * 1000);

    LocationSettingsRequest.Builder settingsBuilder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);
    settingsBuilder.setAlwaysShow(true);

    Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(this)
            .checkLocationSettings(settingsBuilder.build());

    result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
        @Override
        public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
            try {
                LocationSettingsResponse response =
                        task.getResult(ApiException.class);

                if (gps.canGetLocation()) {
                    lat = String.valueOf(gps.getLatitude());
                    lon = String.valueOf(gps.getLongitude());
                }

            } catch (ApiException ex) {
                switch (ex.getStatusCode()) {
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        try {
                            ResolvableApiException resolvableApiException =
                                    (ResolvableApiException) ex;
                            resolvableApiException
                                    .startResolutionForResult(SignupActivity.this,
                                            REQUEST_CHECK_SETTINGS);
                        } catch (IntentSender.SendIntentException e) {

                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:

                        break;
                }
            }
        }
    });
}


Como eu pego o id do depois do select?

Question: o usuario seleciona um user pelo nome, eu preciso descobrir qual o id escolhido. Obrigado pessoal, sou novo em Laravel!
Como eu pego o valor de users->id?
<code>{!! Form::select(
    'id_users',
    $users->pluck('name'),
    $users->pluck('id'),
    [],
    ['class' => 'form-control']
) !!}
</code>
Comment: Muito Obrigado pelas correções Guilherme Cosamilam!!!!
Answer: Uau! Depois de dois dias descobri o erro... apenas no meu form original tinha uma virgula sobrando. O Código está correto e o $users->pluck('id') é o código que eu preciso na função! Obrigado a todos aqueles que tentaram me ajudar...

Como eu pego o id do depois do select?