Proving a jigsaw is possible

Question: This is an offshoot of this question.
Suppose we have jigsaw puzzle pieces which are basically squares but where each side can be either straight, concave or convex. An example of three such pieces is shown below:

It is clear that there can be $3^4=81$ different types of pieces. I was wondering, given one of each type (and with no rotations or flips allowed), whether it was possible to create a standard jigsaw puzzle, using just those $81$ pieces. By "standard jigsaw puzzle" I mean one of dimensions $m \times n$ where all perimeter sides are straight.
A $1\times 81$ puzzle is clearly not possible as it would require all $81$ pieces to be straight on both the left and the right side. A similar argument holds for a $81\times 1$ puzzle.
A $3\times 27$ puzzle would require $27$ pieces with a left straight side and $27$ pieces with a right straight edge, and while it is true that such two sets exist, they have an overlap of $9$ pieces. This is therefore not possible. As before, a similar argument holds for a $27\times 3$ puzzle.
This leaves the $9\times 9$ possibility. A priori, I can see no reason this shouldn't be possible. And I think I have a proof that it is possible, which is what I would like your opinion on.
Given a $9\times 9$ puzzle, we have the situation below:

Each black side of a piece is fixed, but each green side of a piece represents a possible connection type. A connection type could be a "straight - straight", "concave - convex" or "convex - concave" type. It seems to me that if we run through every possible connection type for each piece, one of those scenarios must give a puzzle where each of the $81$ piece types is used exactly once.
Am I right?
Comment: What you're describing sounds like a brute-force method of generating a solution if one exists, but definitely not a proof of existence.
Comment: It won't if there isn't a solution.
Comment: If there is such a solution, you'll find it.  If there isn't, you won't.  I see no reason to rule out either possibility.
Comment: @Jens; Robert Israel is saying that while the approach you're suggesting is a valid strategy to find out if there is a solution or not, it is not in itself a proof either way. You could end up trying every single combination and find that none of them actually make a valid grid (though in this particular case, as Bram28 answered, it is in fact possible and therefore brute-force will indeed find a solution when applied). As a silly example of the negative, I could claim a boolean "a" exists such that "a = not a", with the "proof" that if I try every possible value of a, one of them must work.
Comment: "if we run through every possible connection type for each piece" - I'm not sure I understand what you mean. What about the piece with all straight sides, the one with all concave sides, and the one with all convex sides? Could you clarify your statement?
Comment: The "content" of such is essentially that what you wrote is not an existence proof. It's a method of finding a solution, if one exists, but it's obviously possible to modify the puzzle in such a way that the method you described would not generate a solution, such as in the $3\times 27$ case.
Comment: @Michael Lee: All connection types would be tried for all connections, including the ones you mention (as long as they are not on the border).
Comment: @Michael Lee: I am not suggesting an algorithm. I'm basically asking if there is a reason that trying all possible connections should not result in a puzzle consisting only of the $81$ types.
Comment: @Robert Israel: Could you explain why trying every possible connection type would not necessarily result in the conclusion I make?
Answer: I wouldn't call what you say a proof ... It is not immediately clear from what you say that you will indeed get all possible pieces in there.
However, your hunch is correct: this puzzle is indeed solvable. Here is a proof:
Let's call the straight edge $1$, the concave edge $2$, and the convex edge $3$. Now, label the $10$ vertical edges in each row of your $9 \times 9$ board with $1,1,2,3,1,3,3,2,2,1$ in that order.  Note that if you look at two sucessive edges (so these would be the left and right edges of a piece), you get all possible pairings: $11,12,23,31,13,33,32,22,21$. Hence, pieces in the same column will have the same left and right edge, but pieces in different columns will have differen left/right pairings.
Ok, do the same with the horizontal edges from top to bottom. This will give you all possible pairings for top and bottom edges. So, since different rows have different top/bottom pairings, and since different columns will have different left/right pairings, you do indeed get all $81$ possible combinations, and thus all the $81$ different pieces in there.
Here is the resulting solved puzzle (thanks @Frxstrem !)
Comment: By the way, this is a [de Bruijn sequence](https://en.wikipedia.org/wiki/De_Bruijn_sequence) of order 2 on an alphabet of size 3.
Comment: @Bram28 Here's an illustration of your solution, if you want to add it to your answer: https://imgur.com/a/H78uk
Comment: @Jens You're welcome! :) I was playing with those 1,2,3's for a while, and initally had a hard time (which is why it wasn't at all clear to me there had to be a solution) ... But then I found the right way to think about it, and just had to find a sequence with all possible pairings
Comment: I'm convinced. Thank you.
Comment: Is this answer unique (up to reflections/rotations) or are there several options for coding each axis?
Comment: @Neil_UK Ignoring the 11,22, and 33, there are different ways to do the  the 'core' sequence:, e.g. mine was 123132, but you can also have 231213 and 312321 (and those can be mirrored of course). And now you have to double one of the 1's, one of the 2's and one of the 3's, which can be done in different ways. So even with rotations and reflections there are still  various solutions.
Comment: Wow! Thank you @Bram28 and Frxstrem. I did not expect an actual example of a solution. Isn't MSE great? :)
Comment: @Rahul huh!  Didn't know about them ... Cool, thanks!! :)
Comment: @Frxstrem Cool, thanks!  I added it to my post.
Comment: Really elegant, I like your solution!
Comment: @flawr Thanks! :)
Comment: @Jens I agree! Thanks for the problem! :)
Comment: The symmetry is interesting.  Are the other solutions similarly symmetric?
Comment: @Polyergic For the ones I mention in the comment above, yes.  But JeanMarie noted in a comment to her own post that you can interchange chunks to get different solutions yet, and you'll quickly lose symmetry,
Comment: @Bram28 If you have some time, have a look at the little Edit I have written in my solution.
Answer: This answer, complementing the excellent answer of @Bram28, explains the idea with line coloring. It is possible (see figure 1) to place 10 horizontal and 10 vertical lines using (R,G,B) colors in such a way that the $3^4=81$ different squares are present (for example, there is a unique square with North, West, South, East colored (G,B,R,R) resp.: this square is found in the South East corner). Check that the $3^4=81$ different squares are present once and only once.
Now, attribute colors to each side according to its shape:

R$\to$"flat side",
G$\to$"convex side",
B$\to$"concave side".

This gives figure 2.
Remarks:
1) The color sequencing of lines and columns has been chosen different in a deliberate way. In fact there are 24 different color sequencements. Why that ? Identify East and West side of the board ; similarly, identify the North and South sides ; said otherwise, consider our puzzle as drawn on a torus, so  $9 \times 9$ is the number of pieces and as well the number of lines. This number $9$ is, I would say by chance, a power $9=3^2$, allowing to use the framework of de Bruijn sequences here of type $B(3,2)$. Such  a sequence is made of $3^2$ "letters" (imagined arranged in a ring) such that, by sliding a window of width 2 on the sequence, one gets all the words on an alphabet of 3 items : R,G,B. An example : RRGGBBRBG where the window sliding will provide RR,RG,GG,GB,BB,BR,RB,BG and GR, all the words with 2 letters on an alphabet of 3 (please note that the last word, GR, has been obtained by wrapping the sequence). For more, see (https://en.wikipedia.org/wiki/De_Bruijn_sequence) where you will find a formula explaining that there are $6^3/3^2=24$ of them.
2) Out of a solution, one can generally build a big amount of other solutions. For example, in the case of the given figure, consider the two identical $1 \times 4$ (or $1 \times 5$, or $3 \times 3$... !) rectangles in the South West and North East corners of the puzzle : another solution is provided by switching them.

Edit : (Feb 8 '18)
Related :
http://erikdemaine.org/papers/Jigsaw_GC/paper.pdf
What is known about this jigsaw combinatorics problem?
Somewhat related :
https://puzzling.stackexchange.com/a/48864
I found an old reference mentionning an analogous type of pieces in an old-fashioned delicious booklet entitled "New Mathematical Pastimes" by Major McMahon, Cambridge, 1921
 (https://archive.org/details/newmathematicalp00macmuoft) (page 69) .
Comment: Cool, thanks!! Yes, that's a nice illustration :)
Comment: Based on the OP's 1x81 and 3x27 proofs, I think that the intention is for all outside edges of the 9x9 puzzle to be straight.  Your solution would fit that criteria if the top three rows were removed from the top to the bottom of the puzzle.
Comment: @James You are right. I am going to modify my example.
Comment: While it's not triggering the flickering dots of the [classic B&W version (halfway down)](https://www.sciencealert.com/here-s-why-you-can-t-see-all-12-black-dots-in-this-crazy-optical-illusion), your colored grid of lines is throwing me for a mental loop.  My subconscious is insisting that something should be going on there even though nothing's making its way up to my conscious sight.
Comment: @Dan Neely I have modified my figures, but the effect should still be present. On the "subconscient" side, this set of 81 pieces could take place on a cylinder (by gluing the two vertical sides) or even a torus (by gluing as well the two horizontal sides).
Comment: Thank you for your example. I'm now wondering how many solutions actually exist.
Comment: Out of a  solution like mine, you can build others. Consider for example the two horizontal 1 x 4 rectangles in the South West and North East part of the puzzle : they are interchangeable and are different, yielding another solution. I think that we can do that again and again giving a hugo number of solutions...
Comment: @Jens A curious correspondence with the alternating sign matrices  [here](https://en.wikipedia.org/wiki/Alternating_sign_matrix). Is it a track towards the answer to your question ?

Proving a jigsaw is possible


What's an intuitive way to understand how KL divergence differs from other similarity metrics?

Question: The general intuition I have seen for KL divergence is that it computes the difference in expected length sampling from distribution $P$ with an optimal code for $P$ versus sampling from distribution $P$ with an optimal code for $Q$.
This makes sense as a general intuition as to why it's a similarity metric between two distributions, but there are a number of similarity metrics between two distributions.  There must be some underlying assumptions based on how it chooses to assign distance versus other metrics.
This seems fundamental to understanding when to use KL divergence.  Is there a good intuition for understanding how KL divergence differs from other similarity metrics?
Comment: Given your explanation in comments, I have reframed your question to remove reference to 'bias' and more accurately capture your interest.  Please check the new wording to see if it correctly captures what you want to know.
Comment: This should help you:  https://stats.stackexchange.com/questions/188903/intuition-on-the-kullback-leibler-kl-divergence/189758#189758
Comment: By bias, do you mean why people seem to like KL divergence (have a bias for KL over other distribution difference measures) or a bias in the statistical sense of the expected value of a parameter estimate not equaling the parameter? (I assume the former, but bias is a technical term in statistics.)
Comment: Ah great distinction I mean the former. I'm really asking if you need a distance metric, what features of the problem should lead you to use KL divergence instead of another metric.
Comment: Does this answer your question? [Intuition on the Kullback-Leibler (KL) Divergence](https://stats.stackexchange.com/questions/188903/intuition-on-the-kullback-leibler-kl-divergence)
Answer: A very short answer; there are too many similarity metrics (or divergences) proposed to even try looking at more than a few. I will try to say a little about why use specific ones.

Kullback-Leibler divergence: See Intuition on the Kullback-Leibler (KL) Divergence, I will not rewrite here. Short summary, KL divergence is natural when interest is in hypothesis testing, as it is the expected value under the alternative hypothesis of the log likelihood ratio. Some other divergences look at other functions of the likelihood ratio, but log is natural given its role in statistical inference.

Earth Mover distance, see Difference between Hausdorff and earth mover (EMD) distance and Wikipedia. The ideas here are very different from KL divergence, and I cannot see obvious connection to inference. The wikipedia article gives the following example:

An early application of the EMD in computer science was to compare two
grayscale images that may differ due to dithering, blurring, or local
deformations.[10] In this case, the region is the image's domain, and
the total amount of light (or ink) is the "dirt" to be rearranged.

This seems similar to dynamic time warping used in time series.

Bhattacharyaa distance, see Intuition of the Bhattacharya Coefficient and the Bhattacharya distance?. This is also related to inference, it is the expectation under the null hypothesis of the square root of the likelihood ratio. To me it is unclear why it is interesting, but it can be seen as a generalization of the Mahalanobis distance to nonnormal distributions. Note that starting from $\int \left(\sqrt{f(x)} - \sqrt{g(x)}\right)^2 \; dx \geq 0$ a little manipulation gives $\int \sqrt{f(x) g(x) }\; dx \le 1$ for densities $f, g$. That might give some intuition.

Chisquare distance can be found here have a lot of tradition and seems natural with discrete data. An example of use is correspondence analysis.

Probably many divergences are mostly used technically in proofs, and intuition must then come from their use. An interesting paper.


Microsoft Teams: how can I give read access to anybody in the organization *WITHOUT* adding them to the Teams Channel?

Question: PROBLEM:

We've posted documents to an internal MS Teams Channel (e.g. .pdfs).
We have an IIS-based intranet web site.
We'd like to be able to post a link to the documents, giving anybody who can access the intranet site read-only access to the document.
Creating a link for "read only > anybody in the organization" does NOT work for anybody who's not a member of the Teams channel.  They get an annoying pop-up saying they have to be "granted access" :(

Q: Is there ANY way to grant read-only access for documents posted in an MS Teams channel to ANYBODY in the organization?  Without users having to explicitly "request access"?
I'm sure there's a simple answer ... but I haven't been able to find it.
PS: Our enterprise has an on-prem Active Directory, if that helps.
Comment: the backend for the Teams channel is still Sharepoint, have you given them read-only access to the Sharepoint library itself inside Sharepoint?  Alternatively is to use org-wide Teams channel but doesn't specifically answer your question.
Comment: @TheCleaner: thank you.  I'm pretty much coming to the same conclusion: posting directly on SharePoint (vs. dealing with MS Teams Channels, which are a layer over SP) is probably the answer.  My next challenge is working with my organization for the appropriate SharePoint access...
Answer: One solution would be to grant access to the document by adding the entire organization as a guest to the Teams channel. To do this, you would need to be an owner of the Teams channel and follow these steps:

Go to the Teams channel and click on the "Add members" button.
In the "Add members" window, click on the "Add guests" button.
In the "Add guests" window, enter the email address of your
organization (e.g. @yourcompany.com) and click "Add".
Confirm that you want to add the entire organization as guests by
clicking "Add".

This will allow anyone in the organization to access the documents in the Teams channel without having to request access. Note that this may not work if your organization has strict guest access policies in place.
Comment: Thank you for the response.   I happen to be an "Owner" of the Teams Channel in question.  I go into "Manage Teams", and see "Add Member".  I can look up individuals by name.  Under Member "Roles", I only see "Owner" and "Member" - no "Guest".  Q:  How do I add a "Guest"?  Q: What should I search on for "Everybody"?
Comment: 1. Go to the Teams channel and click on the "Add members" button.
2. In the "Add members" window, click on the "Add guests" button.
3. In the "Add guests" window, enter the email address of the guest that you want to add to the channel and click "Add".
4. Confirm that you want to add the guest to the channel by clicking "Add".
Comment: To add the entire organization as guests to the Teams channel:

1. Go to the Teams channel and click on the "Manage team" button.
2. In the "Manage team" window, click on the "Members" tab.
3. Click on the "Add members" button.
4. In the "Add members" window, enter the email address of your organization in the search field and click "Search".
5. Select the organization from the search results and click "Add".
6. In the "Set member permissions" window, select the "Guest" role for the organization and click "Add".

If you can't locate 'Guest' it may be due to your organization policies.
Comment: Ah ... my "Manage Teams" UI doesn't happen to have any "Add guests".  Nor do any of the "Role" dropdowns have any "Guest" option - only "Owner" or "Member".  So I'm guessing my organization probably defined some policy somewhere that eliminated the "Guest" option.  Anyway  -time to "accept" ... and award you your well-earned bounty! Thank you for the help!
Comment: Thank you, kind Sir!


How to set file upload directory by parent node content type and node id

Question: I am trying to set the upload directory for a file field to contain the content type (and node id) as part of the path.
The desired path for my public files field is publication_documents/pub/content_type/node_id_of_node_this_was_uploaded_to/filename
I have tokens installed, but it doesn't seem to offer me tokens related to the parent node in the upload directory setting:

Slide 30 of This slideshow seems to indicate that it is possible, but how can I do it?
In case it matters, I'm using the media and file_entity modules.
Answer: File (Field) Paths module should help you.
Comment: That seems like exactly what I need, but I still have one issue.  Is there a token or a way to get the machine name of the content type instead of the Human Friendly version.  My migration is already copying the files over using the machine names.
Comment: nevermind, found it! [node:content-type:machine-name]


How can I setup ProFTP and SSHD_config to FTP and SFTP to the same directory on Ubuntu 12.04?

Question: I'm having troubles with a chrooted environment on Ubuntu 12.04.
Objective
Set up FTP/SFTP server where users part of a new 'sftp' group can sftp and ftp to the same directory
ProFTP Setup
The ProFTP setup is a base build in Ubuntu and it works as expected. User 'test1' can log into the server via ftp on tcp 21 and is chrooted to his directory /home/test1/ftproot.
in /etc/proftpd/proftpd.conf
# Use this to jail all users in their homes
DefaultRoot                     ~/ftproot

When the user test1 logs in via ftp, he sees only / (that's what I want).
But when the same user logs in via sftp, and it's properly chrooted using sshd_config, because the /home/test1 directory MUST be owned by root, the user logs in via sftp and sees:
/
/ftproot
/.bashrc

What this means is that the user does NOT end up in the same /ftproot directory. test1 will end up in his /home/test1 home directory by default which is actually owned by root. Therefore, he cannot write anything via sftp until he changes directories to /ftproot.
How do i setup the sftp server so by default he ends up in the same directory?
Here are my sshd_config relevant settings:
#Subsystem sftp /usr/lib/openssh/sftp-server
Subsystem sftp internal-sftp -f AUTH -1 VERBOSE
Match Group sftp
ChrootDirectory %h
ForceCommand internal-sftp
#AllowTcpForwarding No

ChrootDirectory %h/ftproot does not work.
Answer: Now that proftpd has its own SFTP implementation via the mod_sftp module, using that for your SFTP needs (rather than OpenSSH) might neatly address this issue.
Answer: You need to change what folder the user 'test1' arrives at with SSH.  Look at the answer to this question: https://stackoverflow.com/questions/3530433/putty-change-default-ssh-login-directory;
Add cd subdir/subdir to ~/.bashrc or ~/.bash_profile, or
change the home directory of the user.
To change a user's home directory: usermod -d /path/to/dir/ username
Comment: I tried both files and neither worked for me. Perhaps I've done something incorrect? I added cd ftproot (to change into /home/test1/ftproot). I also tried the full path. I read up on this and I'm wondering if this is applicable: http://askubuntu.com/questions/121073/why-bash-profile-is-not-getting-sourced-when-opening-a-terminal


$\begin{cases} x^3-y^3=19(x-y) \\ x^3+y^3=7(x+y) \end{cases}$

Question:
I should solve the following system: $$\begin{cases} x^3-y^3=19(x-y) \\
 x^3+y^3=7(x+y) \end{cases}$$
  by reducing the system to a system of second degree.

We can factor:
 $$\begin{cases} (x-y)(x^2+xy+y^2)=19(x-y) \\
 (x+y)(x^2-xy+y^2)=7(x+y) \end{cases}$$
I really don't want to divide the equations by $x-y$ and $x+y$, respectively. I am taught to divide by expressions containing variables only in special cases. Is there any other way here?
Answer: You can divide by variables if you ensure they are not zero.  Here, you can consider the cases $x=y$ and $x=-y$ first.  If $x=y$ the first equation is trivial and the second becomes $2x^3=14x$ or $x=0,\pm \sqrt 7$.  You can do the same for $x=-y$ and find a pair of solutions.
Then decree that $x+y \neq 0, x-y \neq 0$ and divide away.  Once you do that, you can subtract the two equations to get $2xy=12$ and use that to get expressions for $(x+y)^2, (x-y)^2$
Comment: I left that for you in the second one:).  Actually I missed it and have edited it in.
Comment: Thank you for the response! $2x^3=14x$ and I think you missed $x=0$.
Answer: After considering of cases $x=y$ or $x=-y$ use
$$7(x^2+xy+y^2)=19(x^2-xy+y^2).$$
Comment: Can down-voter explain us, why did you do it?
Answer: The thing that catches my eye is that $7,19$ are primes congruent to $1 \pmod 3,$ therefore integrally represented by both $x^2 + xy+y^2, \; x^2 -xy + y^2,$ meaning there are integer points on the two ellipses. It is worth drawing both, by hand (a valuable skill), to see whether that gives a simplified answer when $y \neq \pm x.$
Answer: To avoid all possible problems, add the two equations to get
$$x^3-13 x+6 y=0 \implies y=\frac{1}{6} \left(13 x-x^3\right)$$ Plug in the first equation to end with
$$x \left(x^8-39 x^6+507 x^4-2665 x^2+4788\right)=0$$ So $x=0$ if a root.
For the remaining, let $z=x^2$ to make
$$z^4-39z^3+507z^2-2665z+4788=0$$ By inspection, $z=4$ and $z=7$ are solutions. Now, long division
$$\frac{z^4-39z^3+507z^2-2665z+4788 } {(z-4)(z-7) }=z^2-28 z+171=(z-19) (z-9)$$


Is this Lie algebra decomposition always true: $\mathcal{G} = \text{Ker}(ad_{a}) \oplus \text{Im}(ad_{a})$

Question: I am reading: https://i.stack.imgur.com/QPS4m.png and am not understanding their decomposition $$\mathcal{G} = \text{Ker}(ad_{a}) \oplus \text{Im}(ad_{a})$$ where $\mathcal{G}$ is a Lie algebra and $ad_{a} := [a, \cdot]$. Since a Lie algebra is a vector space plus some conditions and bracket...then isn't this always true by the classic Linear Algebra rank-nullity theorem? If so, why say this?
Answer: If $a$ is a nilpotent element, i.e., say one where the adjoint map squares to $0$, then the image must be contained in the kernel. The dimensions adding does not mean that one is a complement to the other.
For example, the map $f:\mathbb{R}^2\to \mathbb{R}^2$ given by $(x,y)\mapsto (0,x)$ does not satisfy your statement. The image is generated by $(0,1)$, and so is the kernel.
Edit: As Torsten Schoeneberg mentioned, the map $f$ is $\mathrm{ad}_x$ for the $2$-dimensional Lie algebra with basis $x,y$, and Lie bracket $[x,y]=x$.
Comment: To make this answer perfect, add that this map $f$ "is" $ad_a$ in the two-dimensional Lie algebra with basis $a,b$ and bracket $[a,b]=a$.
Comment: @TorstenSchoeneberg I was actually just giving it as an example of a nilpotent map, I didn't think about whether it is actually an adjoint map for a Lie algebra!


Let $p_n$ denote the $n$th prime and $c_n$ the $n$th composite number. Determine all positive integers $n$ such that $|p_n − c_n| = 1$.

Question: The only ones with the property in title I could find are $n = 5$ and $n = 6$: $|11 − 10| = 1$  and $|13 − 12| = 1$. Past the $20$th prime, the list of primes grows too fast for composite numbers. Is there more general way to show this?
Comment: Even numbers ($>2$) and numbers divisible by $3$ ($>3$) are not prime. How many of the numbers $1,2,3,\ldots,n$ have this property?
Comment: The number of n-digit even integers(barring $2$) is $(9 \cdot 10^n \cdot 5) - 1$ for some $n$. The number of n-digit integers divisible by 3 (except $3$) is $(9 \cdot 10^n \cdot 3) - 1$ for some $n$. Is that correct?
Answer: If $|p_n-c_n| = 1$ then there must exist a $N$ such that the set $\{2,3,\ldots,N\}$ contains an equal number of primes and composite numbers.
The number of composite numbers in the $N-1$ element set $\{2,\ldots, N\}$ is larger than (numbers divisible by $2$ + numbers divisible by $3$ - numbers divisible by $2\cdot 3$ since these have been counted twice)
$$M = \left\lfloor\frac{N-1}{2}\right\rfloor + \left\lfloor\frac{N-1}{3}\right\rfloor - \left\lfloor\frac{N-1}{6}\right\rfloor - 2$$
where we have subtracted $2$ for the two primes $2$ and $3$.
When $N>25$ we have $M \geq \frac{4N-21}{6} > \frac{N-1}{2} + 1$ so there will always be at least two more composite numbers than prime numbers in the set $\{2,3,\ldots,N\}$ and since $p_{10} = 29 > 25$ it follows that $|p_n-c_n|=1$ has no solutions for $n\geq 10$.
Answer: In general $p_n \sim n \ln n$. This can prove useful for your question.


Pacotes do Scapy

Question: Eu estou com algumas dúvidas que não consigo sanar sobre o scapy, vocês poderiam me ajudar por favor?
Dúvidas:

Para declarar um pacote no Scapy eu faço: pck = IP()/TCP(). Com isso eu posso consultar as camadas de pkt, com por exemplo: pkt[IP] ou pkt[0], ai surge uma dúvida:

Que tipo de dado é pkt? Um dicionário? Como ele usa uma classe como index(no caso IP) ou um número?

Como o Scapy trata o "/" de IP()/TCP() para que o Python não entenda como o divisor?
Answer: Aqui ele fala um pouco.
http://www.secdev.org/projects/scapy/doc/usage.html#stacking-layers
1.
Para o tipo tenta dá um
print(type(pkt))

Mas na doc quando instancia um IP ele retorna um <IP ... >
2.
Mas com relação ao operador, muito provavelmente no código houve sobrecarga de operadores, sobrescrevendo o comportamento padrão da divisão usando o Data Model do python [1] .
Referência :
[1] https://docs.python.org/3/reference/datamodel.html#object.truediv
Comment: Ah entendi, legal. Muito obrigado pela ajuda. Só uma coisa, referente à index que, no caso pkt usa, essa index seria a própria classe IP, pois não é uma string como nos dicionários? Vi que da pra usar número como index também.

Pacotes do Scapy


AppEngine Query.fetch_async not very asynchronous?

Question: I'm trying to reduce the execution time of an AppEngine query by running multiple sub-queries asynchronously, using query.fetch_async(). However, it seems that the gain is minimal compared to running the queries serially.
Below is some minimal sample code (in Python) illustrating the problem - first a function for the asynchronous run:
def run_parallel(self, repeats):
    start = datetime.utcnow()

    futures = []
    for i in xrange(0, repeats):
        q = User.query()
        f = q.fetch_async(300, keys_only=True)
        futures.append(f)

    while futures:
        f = ndb.Future.wait_any(futures)
        futures.remove(f)
        results = f.get_result()
        delta_secs = (datetime.utcnow() - start).total_seconds()
        self.response.out.write("Got %d results, delta_sec: %f
\n" %(len(results), delta_secs))

Then a function for the corresponding serial run:
def run_serial(self, repeats):
    start = datetime.utcnow()
    for i in xrange(0, repeats):
        q = User.query()
        results = q.fetch(300, keys_only=True)
        delta_secs = (datetime.utcnow() - start).total_seconds()
        self.response.out.write("Got %d results, delta_sec: %f
\n" %(len(results), delta_secs))

The output of running these two functions 10 times each (not on the dev-server), i.e. of the following calls:
run_parallel(10)
run_serial(10)

is as follows:
Running parallel queries...
Got 300 results, delta_sec: 0.401090
Got 300 results, delta_sec: 0.501700
Got 300 results, delta_sec: 0.596110
Got 300 results, delta_sec: 0.686120
Got 300 results, delta_sec: 0.709220
Got 300 results, delta_sec: 0.792070
Got 300 results, delta_sec: 0.816500
Got 300 results, delta_sec: 0.904360
Got 300 results, delta_sec: 0.993600
Got 300 results, delta_sec: 1.017320

Running serial queries...
Got 300 results, delta_sec: 0.114950
Got 300 results, delta_sec: 0.269010
Got 300 results, delta_sec: 0.370590
Got 300 results, delta_sec: 0.472090
Got 300 results, delta_sec: 0.575130
Got 300 results, delta_sec: 0.678900
Got 300 results, delta_sec: 0.782540
Got 300 results, delta_sec: 0.883960
Got 300 results, delta_sec: 0.986370
Got 300 results, delta_sec: 1.086500
Hence the parallel and serial versions take roughly the same time, around 1 second. The Appstat are as follows, where the first 10 queries are the parallel ones and the following 10 are the serial ones:

From these stats it looks like the 10 first queries are indeed running in parallel, but that they each are taking a disproportional amount of time compared to the individual serial queries. It looks like they may be blocking somehow, waiting for each other to complete.
So my question: Is there anything wrong with my code for running asynchronous queries? Or is there an inherent limitation in the efficiency of asynchronous queries on AppEngine?
I wondered if the behaviour could be caused by one of the following:

Running asynchronous queries on the same entity type. However, a similar example using multiple different entity types shows similar results.
Running identical queries, somehow locking sections of the index. However, a similar example in which each query is different (returning disjoint result sets) yields similar results.

So, I'm at a bit of a loss. Any suggestions would be greatly appreciated.
Update 1
Following Bruyere's suggestion I've tried using db rather than ndb, and I've tried swapping the order of the parallel and serial versions. The results are the same.
Update 2
Here's a related post concerned with the same issue; still no answer as to why parallel queries are so inefficient:
Best practice to query large number of ndb entities from datastore
Update 3
The corresponding code using the Java SDK is parallelised very neatly. Here are the Java appstats:

To be precise, this Java implementation is explicitly multi-threaded, running queries in separate threads; this is necessary because, contrary to what the AppEngine documentation claims, using query iterators does not actually result in queries being executed in parallel.
I've tried to use explicit multi-threading with synchronous query calls in the Python version, but with the same poor results as the original Python version.
The fact that the Java version performs as expected implies that the poor Python async performance is not caused by an AppEngine CPU bottleneck.
The only alternative explanation that I can think of is that Python's Global Interpreter Lock is causing thrashing. This is supported by the fact that decreasing the GIL check interval (using sys.setcheckinterval) exasperates the poor async performance.
This is surprising though: the GIL shouldn't have such a severe impact given that queries are IO bound. I speculate that perhaps the RPC input buffers are small enough that async calls resume frequently during retrieval of results, which could perhaps cause GIL thrashing. I've had a look at the Python AppEngine library code, but the low-level RPC calls are made by _apphosting_runtime___python__apiproxy.MakeCall() which seems to be closed-source.
Alas, my conclusion is that the Python AppEngine runtime is not suited for the kind of parallel querying that I require, leaving me with no other option than moving to the Java runtime. I would really like to avoid this, and so I really hope that I'm wrong and have missed something obvious. Any suggestions or pointers would be greatly appreciated.
Thanks!
Comment: I´m having the same problems and have been experimenting with sys.setcheckinterval, parallel running queries, but nothing did did have a significant impact on performance. Getting 9k items out of the data store takes 19 seconds on F2 Instances and 10 sec on F3 Instances. Seems the only way to cope is to throw hardware at it?
Comment: PS: Tested F1 Instances for comparision... 27 seconds. Unbearable.
Comment: PS: I´m trying to find a more general solution [here #26759950](https://stackoverflow.com/questions/26759950/gae-aggregate-work-results-from-tasks-for-a-gae-query-performance-issue)
Comment: Thanks for the update and the link Thomas. FYI, I ended up switching to Java, running both Java and Python modules for the app, which obviously isn't ideal. I also filed a bug report: https://code.google.com/p/googleappengine/issues/detail?id=11306
Answer: Are you always running run_parallel before run_serial? If so ndb caches the results and is able to pull the information much faster. Try flipping the results or even better try with DB, as ndb is just a wrapper to include memcache results.
Comment: Bruyere, thanks for your suggestion, much appreciated. However, swapping run_parallel and run_serial makes no difference. The results are symmetrical. Regarding the use of ndb: to my knowledge no caching takes place for queries, at least not in the keys-only case. Am I wrong? If so I could try setting up a separate test case (the functions listed above run against existing ndb entities).
Comment: @MichaelPedersen I believe you're correct that no caching takes place for queries. Docs: "Query results are written back to the in-context cache if the cache policy says so (but never to Memcache)." https://cloud.google.com/appengine/docs/python/ndb/cache. The only time I've seen a memcache hit in AppStats was from a direct key lookup (eg. ndb.Key get(), ndb.Model get_by_id(), ndb.get_multi()). I'm not sure how to measure the in-context cache hits though.
Answer: The main problem is that your example is mostly CPU-bound as opposed to IO-bound. In particular, most of the time is likely spent in decoding RPC results which isn't done efficiently in python due to the GIL. One of the problems with Appstats is that it measures RPC timing from when the RPC is sent to when get_result() is called. This means that time spent before get_result is called will appear to be coming from the RPCs.
If you instead issue IO-bound RPCs (i.e. queries that make the Datastore work harder) you will start to see the performance gains of parallel queries.
Comment: Thanks Patrick, I have also reached this conclusion and have accepted your answer. What I find surprising is that there is no better solution: I see no reason why the API shouldn't be able to better avoid GIL thrashing by 1) issuing all requests asynchronously and 2) synchronising to fetch results sequentially.


Recommended 256 GB SSD for Linux servers

Question: I need some relatively-affordable 256 GB SSD drives for a Linux application where fast seek performance (many IOPS) is more important than sequential throughput.  The ratio of reads to writes is more than 10,000 to 1.
I like Intel X-25 drives but can't wait for their 320 GB MLC so I'm looking at others.
OCZ, Corsair, Patriot, Super Talent, Kingston, etc. have 256 GB MLC drives in the $600-$700 range but I haven't found high quality or Linux-oriented performance metrics for them.  Can anyone point me to good benchmarks or describe their own testing with these drives?
Which of them have support for updating the firmware in Linux?  Booting an ISO to do the firmware update is possible but annoying.  Running a Windows executable is not an option.
Thanks!
Comment: I decided to go with the Patriot Torqx 256 GB drives ($695 each).  We'll see how they do..
Answer: The only real MLC solution to look at is the Intel X25-M SSD drives, they only come in 80 and 160GB flavours, but they are definitely above the other desktop-grade drives in terms of performance and life-span.
Comment: I use a lot of X25-M and X-25E but this project requires single drives with at least 256 GB which is why I'm considering other makers.
Answer: Anandtech is the place I go for current commentary on SSDs.
The most recent article is The SSD Relapse: Understanding and Choosing the Best SSD
The short version is; bigger drives are faster.  Intel makes the best, Indilinx based drives are a close second.  As to firmware updates, I have yet to see anyone make a tool to do it for anything but Windows.
I have had very good experience with SuperTalent UltraDrive's sized at 64 and 128gb and would buy a 256gb version of the drive in a heartbeat if I had a need for that size.
Comment: Which of the UltraDrive models do you like?  SX, RX, ME?
Comment: Specifically, the SuperTalent UltraDrive GX. I am not sure what controllers are in the other models.
Comment: Intel supply bootable ISO images for firmware flashing, and you can get hold of the tool to do it from your own dos environment. This still isn't from within linux, but it's not from Windows only, which is why I'm raising the point :)
Answer: You will find a lot of good info on the state of play for SSD drives (SLC\MLC Flash, RAM and hybrid) on StorageSearch's SSD section. They are strongly biased towards enterprise class storage systems but even so the information you can find there is relevant - you want drives that can perform under server type loads.
My general advice is be careful with the affordable stuff that is out there right now, even the X-25M which is an awesome drive in a desktop\notebook environment. Most of the commentary you will find is carried out by, and aimed at, people interested in SSD's for desktop\user computing environments and most of the characteristics that are best suited to those environments are not the same as those desired by someone looking to build high IOPS tier-0 storage layer for servers.  Consumer grade SSD's have highly erratic performance characteristics under sustained load in particular.


Conditional distribution of Normal r.v.

Question: Let X be N($\mu$,Q) with $\mu$= (1,1) and Q= $\begin{bmatrix}
3 & 1 \\
1 & 2 \\
\end{bmatrix}$.
I would like to find the distribution of Y = $X_1 + X_2$ given Z =  $X_1 - X_2 = 0$ .
Therefore I should obtain that Y = $2X_1$ or, equally, that Y = $2X_2$, but since they have a different variance I don't understand how it could be possible.
Continuing with the solution I should find $f_{Z=0}(y)$ being normal with $\mu=2$ and $\sigma^2=20/3$. But still I'm not able to obtain this result.
Could someone help?
Answer: All variables in your problem are normal, therefore all the information you need is encoded in the covariance matrix (and means).
You can first compute:
$$Var(Y) = Var(X_1+X_2) = 3 + 2 + 2 = 7$$
and
$$Var(Z) = Var(X_1-X_2) = 3 + 2 - 2 = 3$$
and finally:
$$Cov(Y,Z) = Cov(X_1+X_2,X_1-X_2) = 3 - 1 + 1 - 2 = 1$$
This gives joint distribution of $Y,Z$ being normal
$ \mathcal{N}((2,0), \begin{bmatrix}
6 & 5 \\
5 & 4 \\
\end{bmatrix} ) $
From this you can find pdf of $Y|Z=0$ explicitly and read off mean and variance.
Comment: you have: $Var(Y) = Var(X_1+X_2) = 3 + 2 + 1 = 6$ but should be $Var(Y) = Var(X_1+X_2) = 3 + 2 + 2 = 7$ because of $2*cov(X_1,X_2)$?
Comment: and in: $Cov(Y,Z) = Cov(X_1+X_2,X_1-X_2) = 3 - 1 + 1 - 2 = 5$, shouldn't be: $Cov(Y,Z) = Cov(X_1+X_2,X_1-X_2) = 3 + 1 - 1 - 2 = 1$
Comment: and third how do you know that (Y,Z) is Multivariate Normal. having each r.v. normal does not imply that the random vector is normal too
Comment: Ad 1. Yes it should be $2 Cov(A,B)$, already fixed.
Ad 2. Yes I made a typo, fixed again, thank you.
Ad 3. $(Y,Z)$ is a result of linear mapping of $(X_1,X_2)$ and linear maps preserve 'multivariate normalness'
Comment: what u are saying is that the linear combination of normal r.v. give raise always to multivariate normal? I didn't get this point. Please see also https://math.stackexchange.com/questions/3376039/rn-r-v-with-each-component-normal-that-however-is-not-multivariate-normal?noredirect=1#comment6948204_3376039. There is the proof that what I said in the first row was a wrong question, I don't know if u meant that.
Comment: I'm *not* saying linear combination of just any normal r.v.'s is multivariate normal. It's precisely linear combination of *multivariate* normal r.v.'s (i.e. *jointly* normal) that are guaranteed to be multivariate normal again after linear mapping.

Conditional distribution of Normal r.v.


deleting one line of the textfile. html table messes up

Question: On clicking delete link the delete button will stay and it will start messing things up and delete the wrong lines.
<?php
$delete = @$_GET['delete'];
$textFile = file("test.txt");
$lines = count($textFile);

if($delete != "" && $delete >! $lines || $delete === '1') {
 $textFile[$delete] = "";
 $fileUpdate = fopen("test.txt", "wb");
 for($a=0; $a<$lines; $a++) {
        fwrite($fileUpdate, $textFile[$a]);
 }
 fclose($fileUpdate);

}

foreach($textFile as $key => $val) {
$line = @$line . "
$key	$val	" . " Delete

";

}
error_reporting(0);
echo $line;

exit;
?>

Answer: i have fixed the problem.. hope it helps someone
<?php
$delete = @$_GET['delete'];
$file_name = "test.txt";
$size = filesize($file_name);
$textFile = file($file_name);
$lines = count($textFile);
if($size == "0")
{
echo "Nothing to display, Seems like file is empty. Try adding some data to it!!!";
exit;
}
if($delete != "" && $delete >! $lines || $delete === '0') {
$textFile[$delete] = "";
$fileUpdate = fopen($file_name, "wb");
for($a=0; $a< $lines; $a++) {
       fwrite($fileUpdate, $textFile[$a]);
}
fclose($fileUpdate);
header("Location:del.php");
exit;
} else

foreach($textFile as $key => $val) {
$line = @$line . "
$key	$val	" . " Delete

";
}
echo $line;
?>


Huge delay on first request due to many slow SQL queries

Question:
Possible Duplicate:
Performance Problem: Delay on first request

The first page of my site takes up to 15 secs to load and then the site become responsive. If I leave the site for 20 min or so, the first request is very long again.
Following recommendations found on this similar question, I checked the Devel query logs and there is a whole bunch of queries related to field_sql_storage_field_storage_load taking more than 250ms.
I am not sure it is purely related to field_sql_storage_field_storage_load though as there is another language_list query with a simple SELECT * FROM languages ORDER BY weight ASC, name ASC taking 186ms.
My questions are: why do I encounter such delays and how can I reduce them?
Comment: I've started a bounty on the other question, hopefully that'll get this issue some attention; it would be nice to get a good answer for it
Comment: Just an idea, have you try your site configuration in another server? At the end it might be MySQL or DB configuration error. Also if the DB is on a separated server check for connectivity and firewall issues.
Comment: @NathanZ can you tell whats the result of this `select count(*) from node where promote = 1`
Answer: So this is how I fixed the problem. It is not a real solution as I could not nail the exact source of the problem (if there is one).
1) Aggregate CSS (cache settings). This reduced the latency by half
2) Set cron to never (and run it externally)
3) Set up a cron job that calls the home page with Lynx every 30 minutes


Meu formulario de contato esta chegando no email de destino como "undefined"

Question: estava funcionando tudo perfeitamente, porém, quando preencho as informações do formulário e clico em enviar, no e-mail chega assim:
Nome: undefined
Email: undefined
Mensagem: undefined
Alguém pode me ajudar por favor, segue abaixo meu código html e php:
PHP
<?php
$errorMSG = "";

if (empty($_POST["name"])) {
    $errorMSG = "Name is required ";
} else {
    $name = $_POST["name"];
}

if (empty($_POST["email"])) {
    $errorMSG = "Email is required ";
} else {
    $email = $_POST["email"];
}

if (empty($_POST["message"])) {
    $errorMSG = "Message is required ";
} else {
    $message = $_POST["message"];
}

if (empty($_POST["terms"])) {
    $errorMSG = "Terms is required ";
} else {
    $terms = $_POST["terms"];
}

$to = "email@example.com";
$subject = "Contato - Supremotec Tecnologia";
$body = "Nome: " . $name . "\n" . "Email: " . $email . "\n" . "Mensagem: " . $message;

// prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $name; $Body .= "\n"; $Body .= "Email: "; $Body .= $email; $Body .= "\n"; $Body .= "Menssagem: "; $Body .= $message; $Body .= "\n";

// send email

$header = "From:email@example.com" . "\r\n" . "Reply-To:" . $email . "\e\n" . "X=Mailer:PHP/" . phpversion(); {

    if (mail($to, $subject, $body, $header)) {

        echo ("Email enviado com sucesso!");
    } else {
        echo ("O email não pode ser enviado");
    }
}
?>

HTML
                <form id="contactForm" data-toggle="validator" data-focus="false">

                        <input type="text" class="form-control-input" id="name" required>
                        Nome

                        <input type="email" class="form-control-input" id="email" required>
                        Email

                        <textarea class="form-control-textarea" id="message" required></textarea>
                        Sua mensagem

                        <button type="submit" class="form-control-submit-button">ENVIAR MENSAGEM</button>

                </form>

Obs. Desde já agradeço a ajuda!
Comment: Edite a questão e coloque o código php em texto. e se possível, coloque o código do erro completo.
Comment: Me desculpe, não estou conseguindo editar  a questão.
Comment: Enfim, consegui postar o código em php adequadamente na pergunta.
Answer: ATUALIZADO
Encontrei alguns erros no seu formulário.

Está faltando o atributo name nos inputs do seu formulário hmtl.
está faltando o atributo action no seu formulário com a rota para o arquivo php
Está faltando o atributo method="post" no seu formulário.

<form id="contactForm" data-toggle="validator" data-focus="false" action="mail.php" method="POST">

            <input type="text" class="form-control-input" id="name" name="name" required>
            Nome

            <input type="email" class="form-control-input" id="email" name="email" required>
            Email

            <textarea class="form-control-textarea" id="message" name="message" required></textarea>
            Sua mensagem

            <button type="submit" class="form-control-submit-button">ENVIAR MENSAGEM</button>

    </form>
Comment:

Nome
Comment: Continua a chegar o os dados do formulario via email como "Undefined*
Comment: editei a resposta, adicionando mais algumas coisas que estão faltando no formulário


How to fix simple Javascript drop-down error?

Question: I'm having trouble with this drop down, i'm editing code for a client and I trimmed the Dropdown groups from 7 to 3 this is my code
What I am trying to do is activate the 3rd dropdown and add the css so that the rounded edges disappear when dropped down
I know it has to do with the script here:
function toggleGroup(group){
                    var isShown = document.getElementById("group"+group).style.display;

                    if (!isShown || isShown=="none"){

                        for (i = 1; i < 4; i++) {

                            if (i != group){
                                document.getElementById("group"+i).style.display="none";
                                document.getElementById("toggle"+i).src ="img/moreIcon.png";
                                location.hash = "#myAnchor";
                                    if(i=="3"){
                                    document.getElementById("backgroundSpeech").style.borderRadius="0 0 20px 20px";
                                    }
                            }
                            else{
                                document.getElementById("group"+i).style.display="block";
                                document.getElementById("toggle"+group).src ="img/lessIcon.png";
                                location.hash = "#myAnchor";
                                if(group=="3"){
                                    document.getElementById("backgroundSpeech").style.borderRadius="0px";
                                    }

                            }
                        }
                    }

                    else{
                                document.getElementById("group"+group).style.display="none";
                                document.getElementById("toggle"+group).src ="img/moreIcon.png";
                                    if(group=="3"){
                                    document.getElementById("backgroundSpeech").style.borderRadius="0 0 20px 20px";
                                    }
                            }

                }

        function toggleMy(my){
                    var isShown = document.getElementById(my).style.display;
                    if (!isShown || isShown=="none"){
                        document.getElementById(my).style.display="block";
                        var toWrite = "read less";
                        document.getElementById("more"+my).innerHTML=toWrite;

}
                    else{
                        document.getElementById(my).style.display="none";
                        var toWrite = "read more";
                        document.getElementById("more"+my).innerHTML=toWrite;

                    }

                                                }

Thanks in advance!
Answer: I wasn't sure whether you wanted no rounded corners at all but i removed all the border radius

function toggleGroup(group) {
  var isShown = document.getElementById("group" + group).style.display;

  if (!isShown || isShown == "none") {

    for (i = 1; i <= 3; i++) {

      if (i != group) {
        document.getElementById("group" + i).style.display = "none";
        document.getElementById("toggle" + i).src = "img/moreIcon.png";
        location.hash = "#myAnchor";
        if (i == "3") {
          document.getElementById("backgroundSpeech").style.borderRadius = "0 0 0px 0px";
        }
      } else {
        document.getElementById("group" + i).style.display = "block";
        document.getElementById("toggle" + group).src = "img/lessIcon.png";
        location.hash = "#myAnchor";
        if (group == "3") {
          document.getElementById("backgroundSpeech").style.borderRadius = "0px";
        }

      }
    }
  } else {
    document.getElementById("group" + group).style.display = "none";
    document.getElementById("toggle" + group).src = "img/moreIcon.png";
    if (group == "3") {
      document.getElementById("backgroundSpeech").style.borderRadius = "0 0 0px 0px";
    }
  }

}

function toggleMy(my) {
  var isShown = document.getElementById(my).style.display;
  if (!isShown || isShown == "none") {
    document.getElementById(my).style.display = "block";
    var toWrite = "read less";
    document.getElementById("more" + my).innerHTML = toWrite;

  } else {
    document.getElementById(my).style.display = "none";
    var toWrite = "read more";
    document.getElementById("more" + my).innerHTML = toWrite;

  }

}
.myGroup {
  font-family: Arial;
  color: #red;
  background: #3399ff;
  font-size: 18px;
  line-height: 30px;
  padding: 5px 0 0 10px;
  text-align: left;
  margin-bottom: 0px;
}
.myGroup img {
  float: right;
  padding: 5px 10px 0 0;
  border: none;
}
.myGroup {
  border: 0 none;
}
.group {
  border-bottom: 1px solid #ffffff;
}
.group a,
.group a:hover,
.group a:active,
.group a:visited {
  box-sizing: unset;
  outline: none;
  text-decoration: none;
}
.first {
  border-radius: 0 0px 0 0;
}
.last {
  border-radius: 0 0 0px 0px;
}
.noDisplay {
  display: none;
}
.my {
  background-color: #eee;
  width: 100%;
  padding: 15px 15px 30px;
  border-bottom: 1px solid #ffffff;
}
.myIm {
  float: left;
  width: 20%
}
.myBi {
  float: left;
  width: 80%;
  color: #363636;
  font-size: 12px;
}
.myName {
  float: right;
  width: 80%;
  color: #363636;
  font-size: 12px;
  text-align: left;
}
.myName h1 {
  font-size: 18px;
  color: #363636;
  text-decoration: underline;
  line-height: 18px;
  font-family: 'arial';
}
.myBi span,
.myBi span a {
  float: right;
  color: #b00f3a;
  text-decoration: underline;
}

TEST

TEST

TEST

TEST

TEST

TEST

TEST

TEST

TEST

Comment: doesn't work in the snippet - cos of the embedded js i imagine
Comment: Thanks! I did want the rounded corners though
Comment: you can fix that in the css it was the border-radius


Understanding double sums without matrices

Question: In our maths class we are currently studying sums, as an exercice we were given the task of proving the following :
$$\sum_{1 \le j \lt k \le n} (a_k - a_j)(b_k-b_j) =\dfrac{1}{2}\sum_{1 \le j,k \le n} (a_k - a_j)(b_k-b_j)$$
Where $a_k , b_k$ are random reals.
I did some research and found people explaining these double sums using matrices but I can't understand them.
To be specific i'm looking for a more algebraic proof of this equality and grasping the meaning behind the double sum notations and the difference between the two sums shown above. Any help would be greatly appreciated!
Answer: $\sum_{1\le j,k \le n} (a_k-a_j)(b_k-b_j)$ = $\sum_{1\le j \lt k \le n} (a_k-a_j)(b_k-b_j)$ + $\sum_{1\le k\lt j \le n} (a_k-a_j)(b_k-b_j)$ + $\sum_{1\le k=j \le n} (a_k-a_j)(b_k-b_j)$

$\sum_{1\le j \lt k \le n} (a_k-a_j)(b_k-b_j)$ = $\sum_{1\le k\lt j \le n} (a_k-a_j)(b_k-b_j)$
As  $(a_k-a_j)(b_k-b_j)= (a_j-a_k)(b_j-b_k)$

$\sum_{1\le k=j \le n} (a_k-a_j)(b_k-b_j)$ = 0
Comment: Exactly what I needed. Thank you!
Answer: Writing down explicitly an example for a small $n$ can help facilitate both the explanation of the notation as well as the proof.
For $n=3$,
\begin{align}
\sum_{1 \le j < k \le n} (a_k - a_j)(b_k - b_j)
&= (a_2 - a_1)(b_2 - b_1) + (a_3 - a_1)(b_3 - b_1) + (a_3 - a_2)(b_3 - b_2)
\\
\sum_{1 \le j,k \le n} (a_k - a_j)(b_k - b_j)
&=
(a_1 - a_1)(b_1 - b_1) + (a_1 - a_2)(b_1 - b_2) + (a_1 - a_3)(b_1 - b_3)
\\
&\qquad + (a_2 - a_1)(b_2 - b_1) + (a_2 - a_2)(b_2 - b_2) + (a_2 - a_3)(b_2 - b_3)
\\
&\qquad  + (a_3 - a_1)(b_3 - b_1) + (a_3 - a_2)(b_3 - b_2) + (a_3 - a_3)(b_3 - b_3)
\end{align}
The difference in the two summation notations is that the first one only involves terms where $k > j$, while the second line considers all pairs $(j,k)$. Verifying the equation for $n=3$ should be fairly straightforward from here, and you can generalize to arbitrary $n$.
Comment: Thanks for your input!


Windows Server 2008 R2 Backup from Networked Drive

Question: Can someone give a solution similar to the old NT Backup that allows backing up networked drives on Windows Server 2008 R2?
I have one backup server running Windows Server 2008 R2 and 4 servers running Windows Server 2000 that have their drives mapped to the backup server.
Comment: It sounds like you're trying to do a "pull" backup of the W2K servers from the W2K8 server. Why not do a "push" backup from the W2K servers to the W2K8 server?
Comment: Create a snapshot with VSS, and use robocopy?
Comment: joeqwerty: my boss doesn't want the process running on the W2K servers..
Comment: if process is local and dump vs a remote process that copy, the I/O is the same. Your boss goal are illogical.. as how would you restore the image ???? as a system state you must boot, and after apply the copy... you would apply a copy created by a win2008r2's version on a 2k ntbackup's tool ?
Answer: What's wrong with Windows Server Backup that's built into 2008 R2?
Edit: I misread the question. I think you're doing it a little backwards. You should probably have an NTBackup script on each 2000 Server and have the drive to the backup box mapped on each server. You should dump the NTBackups from the 2000 servers to the 2008 box.
You couldn't have been getting system state and other important stuff doing it the other way around. Then you can use Windows Server Backup on the 2008 box to back itself up and the NTBackups from the other servers onto external media.
Comment: it won't let you backup networked drives
Comment: @Matt Updated my answer. I misunderstood what you are trying to do.
Answer: Yes, I missed NTBackup in the recent Windows versions, too.  It was very flexible.
Have you tried StrArc/StrArc64 by Olof Lagerkvist from LTRData?  It uses the NT Backup API and performs practically the same kind of backup as NT Backup, but with more flexibility with its command line itnerface.  I just tested it, and it seems to have backed up Windows networked resources correctly.
Answer: Umm. What's wrong with wbadmin?
Comment: Ah, I see - you don't want to back up **to** network shares, you want to back up the shares themselves... In this case, virtually any [third party](http://en.wikipedia.org/wiki/List_of_backup_software) will do.


Derivation of Regularized Linear Regression Cost Function per Coursera Machine Learning Course

Question: I took Andrew Ng's course "Machine Learning" via Coursera a few months back, not paying attention to most of the math/derivations and instead focusing on implementation and practicality. Since then I have started going back to study some of the underlying theory, and have revisited some of Prof. Ng's lectures. I was reading through his lecture on "Regularized Linear Regression", and saw that he gave the following cost function:
$$J(\theta) = \frac{1}{2m}[\sum_{i=1}^m(h_\theta (x^{(i)}) - y^{(i)})^2 + \lambda\sum_{j=1}^n\theta^2_j]$$
Then, he gives the following gradient for this cost function:
$$\frac{\partial}{\partial \theta_j}J(\theta) = \frac{1}{m}[\sum_{i=1}^m(h_\theta (x^{(i)}) - y^{(i)})x^{(i)}_j - \lambda\theta_j]$$
I am a little confused about how he gets from one to the other. When I tried to do my own derivation, I had the following result:
$$\frac{\partial}{\partial \theta_j}J(\theta) = \frac{1}{m}[\sum_{i=1}^m(h_\theta (x^{(i)}) + y^{(i)})x^{(i)}_j + \lambda\theta_j]$$
The difference is the 'plus' sign between the original cost function and the regularization parameter in Prof. Ng's formula changing into a 'minus' sign in his gradient function, whereas that is not happening in my result.
Intuitively I understand why it's negative: we are reducing the theta parameter by the gradient figure, and we want the regularization parameter to reduce the amount that we are changing the parameter to avoid overfitting. I am just a little stuck on the calculus that backs this intuition.
FYI, you can find the deck here, on slides 15 and 16.
Comment: In your result you have a "**+**" preceding the y^(i)--is that a typo?
Answer: $J(\theta) = \frac{1}{2m}[\sum_{i=1}^m(h_\theta (x^{(i)}) - y^{(i)})^2 + \lambda\sum_{j=1}^n\theta^2_j]$
Now
$\frac{\partial}{\partial \theta_j}(h_\theta (x^{(i)}) - y^{(i)})^2=2[(h_\theta (x^{(i)}) - y^{(i)})\frac{\partial}{\partial \theta_j}\{h_\theta(x^{(i)})\}]$
Note that in a linear model (being discussed on the pages you mention), $\frac{\partial}{\partial \theta_j}(h_\theta(x^{(i)})=[x^{(i)}]_j$
$\frac{\partial}{\partial \theta_j}\lambda\sum_{j=1}^n\theta_j^2=2\lambda\theta_j$
So for the linear case
$\frac{\partial}{\partial \theta_j}J(\theta) = \frac{1}{m}[\sum_{i=1}^m(h_\theta (x^{(i)}) - y^{(i)})x^{(i)}_j + \lambda\theta_j]$
Looks like perhaps both you and Andrew might have typos. Well, at least two of the three of us seem to.
Comment: $\frac{\partial}{\partial x} (x^2 + y^2 + z^2) = 2x$ ... why would you expect to see a sum involving the terms in $y$ and $z$?
Comment: its confirmed, just a typo on the Andrew's note, it should be a + sign. And Prof correctly explain everything correctly including the intuition θ(1-α(λ/m)) meaning every time this shrink θ then minus the usual part before regularisation being introduced.
Comment: How does the derivative of the regularization term work out to 2λθj? I understand the power rule, but I'm not sure what happens to the inner function because I thought the ∑ notation would appear in the resulting derivative.
Comment: That makes sense. Thank you.
Comment: To add more context on how ∑ goes off:

`(d/dw_j) Σ[j=1 to n] w_j^2 = (d/dw_j) (w_1^2 + w_2^2 + ... + w_j^2 + ... + w_n^2)
= 0 + 0 + ... + 2w_j + ... + 0
= 2w_j`
Answer: Actually if you check the lecture notes just after the video , it shows the formula correctly .
The slides that you have lined here shows the exact slide of the video.
Comment: https://www.coursera.org/learn/machine-learning/supplement/pKAsc/regularized-linear-regression here its the link to the notes right after the video showing correct formula.
Answer: Actually, I think that's just a typo.
On slide #16 he writes the derivative of the cost function (with the regularization term) with respect to theta but it's in the context of the Gradient Descent algorithm. Hence, he's also multiplying this derivative by $-\alpha$. Notice: On the second line (of slide 16) he has $-\lambda\theta$ (as you've written), multiplied by $-\alpha$. However, by the third line the multiplied term is still negative even though--if the second line were correct--the negative signs would've cancelled out.
Make sense?


Hausman test fails to reject the H0 (and the instrument is not weak), should I still use the 2SLS based on knowledge that the variable is endogenous?

Question: I have two questions.
I am conducting the Hausman test to check the endogeneity of a variable.
If the Hausman test fails to reject the null hypothesis, there is no difference between OLS (my reference model) and 2SLS estimates. The OLS estimates are consistent.
Someone argued that even if we do not reject the null (and the instruments are valid and not weak), we cannot necessarily rule out endogeneity.
In other words, this person claimed that, if we have reasons to believe that the variable is endogenous, we have to use the 2SLS, even if there is no evidence of endogeneity (based on the results from the Hausman test).
1) Is that correct?
I am thinking that an important question is about the magnitude of the endogeneity. For example, if you have a sample of 100k people that is representative of the population of interest, and the variable you are interested in is endogenous for, say, 100 people (these 100 people have a common, observable, and known characteristic, and they are able to manipulate the value of that variable), the OLS might still give estimates statistically indistinguishable from the 2SLS (i.e. the OLS is consistent). There is endogeneity, but its magnitude is so small that the OLS gives the same estimates as the 2SLS.
2) Should you still use the 2SLS based on logical grounds (i.e. you know there is endogeneity caused by a known characteristic)?
[there is this one similar question, but it is never addressed the case when the instrument is not weak and it is; plus, it is not asked whether it is theoretically possible to have endogeneity, but so small that the OLS could appear to be just fine]
Answer: Let me begin my answer to question 1) by noting that the Hausman test, as with any other statistical test, relies on certain assumptions being true/good approximations. For example, in IV settings, an assumption that can really go awry is if you have a "weak" instrument in the sense that your instrument does not do a very good job of explaining $X$ (and in particular, is barely statistically distinguishable from having no effect on $X$ at all). You should always be wary that this can cause your test to incorrectly reject a null hypothesis too. Assuming that you are sure about your assumptions, your intuition that the "magnitude of endogeneity" complicates the analysis is correct, but this concern potentially cuts deeper than you realize. Recall that if a Hausman test fails to reject that the 2SLS slope is different from the OLS slope implies that the data does not contain sufficient evidence to reject the null hypothesis that the two slopes are identical. In general, there are two reasons why you might get a null result in hypothesis testing: the null hypothesis is true and you correctly failed to reject it, or the null hypothesis is false, but you incorrectly failed to reject it.
The main concern, as you seem to have correctly identified, is that you might fail to reject the null for the second reason. In your example, the reason why you fail to reject the null hypothesis is that the null of no endogeneity is technically false, but the magnitude of violation is so small that for all intents and purposes, it would be unreasonable to expect a test to pick up on this difference. In your example, this does not seem so concerning - you failed to correctly reject a null hypothesis that was technically wrong, but for all practical intents and purposes, the null was not even that strongly violated, so you would hardly even make much of an error proceeding as if it was true. However, there are other examples where you fail to correct reject a false null hypothesis for much more concerning reasons. To take an extreme example, suppose that OLS a very precise point estimate of $\beta_1 = 1$ with a standard error of $0.01$. On the other hand, suppose that your IV estimate gave you a point estimate of $\beta = -1$, but with a huge standard error of $10$. In this case, the 95% confidence interval for IV could be anywhere between -21 and 19, which would cause a statistical test of whether the two slopes are identical to fail to reject. But notice that in this extreme example, this test is really not telling us much at all. This conclusion that we cannot reject the null hypothesis would occur in cases as polar as OLS being badly biased downwards (e.g. $\beta_1 = 10$ in reality) or OLS giving the wrong sign (e.g. $\beta_1 = -5$ in reality).
In general, the concerns I raised above can be mitigated by being more transparent about what is going on. For example, rather than mechanically relying on the results of the Hausman test, it is possible to get an intuitive feel for what the Hausman test is actually picking up on by separately looking at point estimates and standard errors for IV and OLS respectively. Do the point estimates look similar? How wide do the confidence intervals look? However, even if you do that, you should be aware that the Hausman test is asking the question of whether your particular 2SLS estimates are different from your OLS estimates. If your 2SLS estimates themselves suffer from endogeneity problems, then you have not ruled out endogeneity and have just shown that the endogeneity plaguing 2SLS seems to be similar to the endogeneity affecting OLS.
I think giving an answer to 2) that works in every scenario is difficult to provide. Causal inference is difficult and highly contingent on domain knowledge about the idiosyncrasies of your setting, but some general guidelines might be useful.

To cite an overused quote, "All models are wrong, but some are useful". For OLS to have a causal interpretation, your model of the world has to be something of the form "there is no systematic relationship between the assignment of my treatment $X$ and other factors that might affect my outcome $Y$". If you truly believe this is a good model of the world (or at least a close enough model), then you can use OLS (perhaps with some caution). I should point out though, that at least in social sciences, there are few reasons to believe that such an assumption is a good one unless $X$ is experimentally manipulated. To the extent that people have an ability to control $X$, you can typically expect them to do their best to satisfy whatever objectives they might have, so when $Y$ is an outcome people care about, you expect people to choose $X$ based on their anticipated outcome $Y$, which will cause the "other factors" to systematically be related to $X$.
If you had known and measurable characteristics that are causing endogeneity, you should do your best to control for these influences.
If you have unmeasurable characteristics causing endogeneity, then you know OLS will be biased. An instrument that truly satisfied all of the usual IV assumptions and in addition does not suffer from statistical deficiencies such as a "weak instrument" problem will typically be a more reliable estimate than OLS, but when you think your IV is not literally satisfying its assumptions but merely "less wrong" than OLS, the cure may be worse than the disease. For example, even if you do not have a weak instrument problem in the statistical sense, in general, it will be the case that the weaker the instrument, the more that endogeneity problems of the instrument itself will be "magnified". This can be seen in the single instrument case by noting that $\hat\beta_{IV} = Cov(Z,Y) / Cov(Z,X)$. Since $Cov(Z,X)$ is on the denominator, any endogeneity problem will get divided by a smaller number, the weaker an instrument is. Thus, even if $Cov(Z,X)$ is bounded away from 0, it can still cause problems when it is small.
Something to be aware of is that a lot of the traditional textbook theory of IV implicitly assumes homogeneous treatment effects. Under the presence of heterogeneous treatment effects, the analysis is even more tricky. Good resources to look at to at least be aware of these issues involved are Imbens and Angrist (1994) and Imbens, Angrist, and Graddy (2000).
As a bottom line, it is your job to do your homework and convince your audience that what you are estimating is reasonable for your particular situation. If justifying OLS, you should be prepared to argue why $X$ is not correlated with other factors affecting $Y$. In particular, you should be prepared for critics to think up myriad counterexamples of such confounding variables. To the extent that you can control for those, you should. If you wanted to use an instrument, you should be prepared to justify why it makes sense to use it. Why can we treat the instrument $Z$ as "as good as randomly assigned" when we could not assume that of $X$? Does the instrument suffer from obvious statistical deficiency tat makes inference with it invalid?


Como ordenar e desordenar um datatable em JSF clicando na coluna?

Question: Bom galera, eu tenho uma aplicação em JSF, gostaria de saber como faço para ordenar e desordenar datatable ao clicar na coluna, nesse tabela existe 6 colunas e 3 delas gostaria que eu ao clicar ela ordenasse de acordo com a coluna clicada?
*consulta por demanda
Comment: o que exatamente seria `desordenar`? você quer deixar suas linhas aleatórias? qual a dificuldade você está tendo? poderia mostrar a parte relevante do seu código?
Comment: ORDENAR: crescente: 123456 decrescente: 654321
DESORDENA: 643521
Comment: Provavelmente você vai querer usar collection.sort() no seu bean. Mas você pode fazer isso de várias maneiras, segue abaixo alguns exemplos inclusive com código de exemplo pra download. Este exemplo aqui deve se enquadrar melhor no seu caso de consulta por demanda
http://uaihebert.com/paginacao-de-datatable-lazy-com-jsf-primefaces/ http://www.mkyong.com/jsf2/jsf-2-datatable-sorting-example/ http://www.guj.com.br/java/42554-jsf--ordenacao-de-datatable
Comment: A parte de desordenar fica meio complicadinho usando o "sortBy" do "primefaces". Terias de implementar o próprio métodos de ordenação e gerar, dentre os possíveis valores, linhas aleatórias.
Answer: Se estiver usando ou puder usar o Primefaces, basta incluir a propriedade "sortBy" em seu column
<code><p:dataTable value="#{testeMB.objetos}" var="o">
  <p:column headerText="Nome" sortBy="#{o.nome}">
    #{o.nome}
  </p:column>
</p:dataTable>
</code>
Comment: Noto apenas que o "objetos" precisa ser um atributo de uma classe(incluindo a Managed ou Named) e não apenas um método que retorna a lista de objetos.


Twilio c# helper, How do I use this method signature? void AddOutgoingCallerId (phoneNumber, friendlyName, callDelay, callback)

Question: I'm trying to follow the Twilio docs for adding a Caller ID as a verified out going telephone number.
The Twilio c# helper library has a method signature which looks like so
void AddOutgoingCallerId (phoneNumber, friendlyName, callDelay, callback)
where callback is Action<ValidationRequestResult>
I don't understand this. I'm trying to set a URL that Twilio can post back to once the verification has taken place.
Help appreciated.
Answer: You need to implement a method that takes a ValidationRequestResult and returns void. That's the method that will get called. You provide that method as the callback parameter.
Here's your call:
AddOutgoingCallerId (phoneNumber, friendlyName, callDelay, HandleAddOutgoingCallerId)

And here's the method that you need to create.
private void HandleAddOutgoingCallerId(ValidationRequestResult result)
{
    // Do something (or just return).
}

Comment: Thanks for you help.  I get a red squiggly line saying "The best overloaded method match for Twilio.TwlioRestClient.AddOutGoingCallerID(string,string,int,string, System.Action) has some invalid arguments.
Comment: That's five parameters. In your question, you show four. I would need to see how your calling the method with the five parameters.
Comment: See if that works. `HandleAddOutgoingCallerId()` needs to take a `ValidationRequestResult`, not an `Action`.


Custom Post Type doesn't show featured image

Question: I can't get the "featured image" to show up in the editor. This is in features.php. When I go to the admin backend, end edit the CPT, I get the regular edit screen without a featured image. I thought supports thumbnail was the way to go.
function createStyleWiseCPT() {

    $labels = array(
        'name'                => _x( 'Style Wise', 'Post Type General Name', 'text_domain' ),
        'singular_name'       => _x( 'Style Wise', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'           => __( 'Style Wise', 'text_domain' ),
        'name_admin_bar'      => __( 'Style Wise', 'text_domain' ),
        'parent_item_colon'   => __( 'Parent Item:', 'text_domain' ),
        'all_items'           => __( 'All Style Questions', 'text_domain' ),
        'add_new_item'        => __( 'Add New Style Question', 'text_domain' ),
        'add_new'             => __( 'Add New Style Question', 'text_domain' ),
        'new_item'            => __( 'New Style Question', 'text_domain' ),
        'edit_item'           => __( 'Edit Style Question', 'text_domain' ),
        'update_item'         => __( 'Update Style Question', 'text_domain' ),
        'view_item'           => __( 'View Style Question', 'text_domain' ),
        'search_items'        => __( 'Search Style Question', 'text_domain' ),
        'not_found'           => __( ' Style Question Not found', 'text_domain' ),
        'not_found_in_trash'  => __( ' Style Question Not found in Trash', 'text_domain' ),
    );
    $args = array(
        'label'               => __( 'style-wise', 'text_domain' ),
        'description'         => __( 'A styleish post', 'text_domain' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ),
        'taxonomies'          => array( 'style-wise' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'menu_position'       => 5,
        'show_in_admin_bar'   => true,
        'show_in_nav_menus'   => true,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );
    register_post_type( 'style-wise', $args );

}

Comment: try to add this in function add_theme_support( 'post-thumbnails', array( 'post','custom-post-type-here','another-custom-post-type' ) )
Comment: That worked! I don't know WHY though. lol.
Answer: try to add this in function
add_theme_support( 'post-thumbnails', array( 'post','custom-post-type-here','another-custom-post-type' ) )

you can also check the codex for reference
Comment: Yeah, but WHY isn't it working when I've set 'supports'      =>array('thumbnail')
Comment: @JimMaguire It does work. If you do only use `add_theme_support( 'post-thumbnails');` it enables featured image for all post types. It's only when you supply an array that you need to specify each one. Think of this as a master switch. Withoout it "on", the 'supports' directive on a CPT won't have any effect.


Does continuous injective functions preserve disconnectedness?

Question: It is well known result that continuous functions send connected spaces to connected spaces. It is also known that if $f:\mathbb{R}^n\rightarrow\mathbb{R}^n$ is continuous and injective, then if $A\subset\mathbb{R}^n$ is open, so it is $f(A)$. And it is also known that a continuous bijective function between topological spaces doesn't necessarily have to be an open function.
So, what if we give a little twist to all these results?

Let $f:\mathbb{R}^n\rightarrow\mathbb{R}^m$ be a continuous injective function and let $A\subset\mathbb{R}^n$ be an open and disconnected set. Does $f(A)$ need to be open or disconnected?

I already know that $f(A)$ isn't necessarily open, since you can consider inclusion $i: \mathbb{R} \rightarrow\mathbb{R}^2$ with $i(x) = (x,0)$. Clearly $i$ is continuous and injective and any open set in $\mathbb{R}$ will be sent to a line segment in $\mathbb{R}^2$ which isn't an open set under its standard metric topology.
But now I'm having troubles with either proving or finding a counterexample for disconnectedness. If there is a counterexample out there, then due to the Invariance of Domain Theorem, $m\neq n$ necessarily. But I couldn't think of any.
Then I've also tried to give a proof, but I got stuck. Here's what I've tried:
Take $f$ and $A$ as stated. Since $A$ is disconnected, there are two open sets $B,C$ such that $A=B\cup C$ and $B\cap C=\emptyset$, hence $\overline{B}\cap C=B\cap\overline{C}=\emptyset$ (where $\overline B$ means the closure of $B$). This means that $f(A)=f(B)\cup f(C)$ and due to injectivity we can easily check that $f(B)\cap f(C)=\emptyset$. But $f(B),f(C)$ aren't necessarily open in $\mathbb{R}^m$ as shown by the previous example, so we can't conclude that $f(A)$ is disconnected. I would need to show that $\overline{f(B)}\cap f(C)=f(B)\cap \overline{f(C)}=\emptyset$
Let $z\in\overline{f(B)}\cap f(C)$. Then there is $c\in C$ such that $f(c)=z$. Due to continuity, we also know that $f(\overline B)\subset \overline{f(B)}$. If $z\in f(\overline B)$ there is $b\in\overline B$ such that $f(b)=z$. Due to injectivity, $b=c$, hence $\overline{f(B)}\cap C \neq \emptyset$ which is a contradiction.
Therefore, $z\in\overline{f(B)}\setminus f(\overline B)$... and here's where I'm stuck.
Any help will be dearly appreciated.
Comment: Let $n = 1 < m$, and take a curve that ends up touching itself.
Comment: But if a curve touches itself in point $p$, that would mean that $p$ has at least two preimages, hence it can't be an injective function as stated in the hypothesis. Am I right?
Comment: No, not if the point where the curve touches itself would be the image of $+\infty$. Since $+\infty \notin \mathbb{R}$, that point can have only one preimage.
Comment: Standard typesetting conventions call for a bit of space to the left and right of $\text{"}+\text{''}$ when it is a binary operator, as in $3+5$, and NOT when it is a unary operator, as in $+5$, and the same is true of subtraction of sets, and accordingly I changed $z\in\overline{f(B)} \backslash f(\overline B)$ to $z\in\overline{f(B)}\setminus f(\overline B)$, which, as you see, has a conspicuously different appearance. It uses \setminus rather than \backslash.  The latter doesn't follow binary-operator conventions. ${}\qquad{}$
Answer: By invariance of domain, a continuous injective $f \colon \mathbb{R}^n \to \mathbb{R}^m$ can only exist for $m \geqslant n$. Also by invariance of domain, for $m = n$, such a function would be open, hence a homeomorphism to its image, which is then open in $\mathbb{R}^n$, and therefore preserve disconnectedness.
So we need $n < m$. The simplest examples are for $n = 1,\, m = 2$. It's still more convenient to take a bounded open interval $(a,b)$ for the domain than $\mathbb{R}$. So define $f \colon (-2\pi,\pi) \to \mathbb{R}^2$ by
$$f(t) = \begin{cases} (t + \pi - 1, 0) &, t \leqslant -\pi \\ (\cos t, \sin t) &, t > -\pi.\end{cases}$$
It is easily verified that $f$ is continuous and injective, but the disconnected open set $(-2\pi, -\pi/2) \cup (\pi/2,\pi)$ is mapped to a connected set.
Comment: Thanks. Now I'm thinking how to extend $f$'s (or a similar function) domain to the whole real line.
Comment: Extending wouldn't work at the touching end (the other end is no problem of course). Compose $f$ with a homeomorphism $h \colon \mathbb{R} \to (-2\pi,\pi)$, for example $h(x) = 3\arctan (x) - \frac{\pi}{2}$. Then $f\circ h$ is what you want.


Methods in a Sequence Diagram

Question: I am creating this sequence diagram, and wondered exactly what methods to include. I have included all methods in every method, for example, the
handleCustomerAccountAction()

method, got a method from another class called
getListOfCustomers()
Is that right to do, or do you only have to include the method and not all the methods it uses inside it?
Also, is it okay to show the same method multiple times on other methods? For example the method
getListOfCustomers()

is shown three times (the one with a loop around it), but from different methods.
Here is the image of my sequence diagram:
Answer: It always depends on what you want to show. A SD shows a certain collaboration you want to explain in detail. A SD should focus on a certain aspect and must not show each and every message (e.g. certain call branches can simply be left out). However, if a method is called twice in a sequence you must show it if it's of importance.
Comment: Thank you for the answer

Methods in a Sequence Diagram


Creating a lower-triangular matrix when $A[n,n] =0$?

Question: As far as I learned in my limited experience in linear algebra, to convert a matrix to upper triangular form through Gaussian Elimination, you would add/subtract multiples of the first row so that $A[2,1]$ would be $0$, and so on.
I was going to do the same basic idea to convert a matrix to lower-triangular, but the $4\times 4$ matrix I need to do this for has $A[4,4]=0$.
$$
A = \begin{bmatrix}3 & 4 & 1 & 2 \\ -1 & 2 & 0 & -2 \\ 1 & 1 & 2 & -1 \\ 2 & -3 & 1 & 0\end{bmatrix}
$$
How would I do GE to make this matrix lower-triangular, then?
Comment: This is a problem I am supposed to code. I just don't understand how it's supposed to work mathematically. Which I obviously need to understand first. My professor insists that this is possible, so I must have a hole in my math knowledge.
Answer: This is a version of Gaussian elimination. Gaussian elimination usually refers to the procedure of creating an upper triangular matrix, but I think you can figure out how to modify to get a lower-triangular matrix.

OK, so the problem is when $A[n,n]=0$. But then I think you missed this part of the algorithm:
<code>i_max  := argmax (i = 1 ... k , abs(A[i, k]))
if A[i_max, k] = 0
     k = k - 1
     continue
swap rows(k, i_max)
</code>
Basically, when you encounter a $0$ entry, you try to search in the same column for a non-zero entry. Then you would swap the two rows. For example, in this case, we would swap the first row and the last row to get
$$\left[\begin{array}{rrrr}
2 & -3 & 1 & 0\\
-1 & 2 & 0 & -2\\
1 & 1 & 2 & -1\\
3 & 4 & 1 & 2
\end{array}\right]$$
and continue with eliminating the rest of the column.
Comment: I don't think I can if that's exactly the question I'm asking???
Comment: Please read the Wiki link. There is a pseudo-code for the algorithm. Try to play with it.
Comment: I've already written code almost exactly like that code to create an upper triangular matrix. I've noodled around with it and I can't get it to create a lower-triangular matrix, which is why I was asking, _mathematically_, how it is supposed to work. Because I don't understand it _mathematically_.
Comment: See the update if you can understand it?
Comment: Oh! That makes sense. And now that seems silly. I haven't taken LA nor really used it in a year so I forgot such a simple thing. Thanks!


Como enviar dados de um formulário sem botão submit e sem atualizar a página

Question: Olá,
Sou um programador relativamente novo em php e estou desenvolvendo um site. É um site onde há uma lista de empresas. E eu queria implementar nele, filtros de pesquisa, o primeiro deles sendo um select "Selecione sua cidade":
    <form method="post" action="lista.php" id="Cidade" name="Cidade">
        Selecione sua Cidade
        <select id="cCidade" name="tCidade">
            <option>Selecione a Cidade</option>
            <option value="Farroupilha">Farroupilha</option>
            <option value="Caxias do Sul"default>Caxias do Sul</option>
        </select>
    </form>

Eu quero que quando o usuário utilizar o select, os resultados são enviados para um iframe na tela (o "lista.php"), que é a listagem das empresas como falei anteriormente.
Porém eu quero que esse envio, não atualize a página e nem precise de um botão para ser enviado, ou seja, sempre que o usuário usar outra option do select, a pesquisa mude totalmente.
Comment: como você imagina que o script pode identificar que o usuário já acabou de preencher o formulário e o mesmo já pode ser enviado?
Answer: Bom a forma mais simples de fazer isso é usando jquery e o serializeArray
Fiz um exemplo pra vc ver como funciona.

Pego o form com o seletor $()
Executo o  event.preventDefault() para parar a ação de envio do formulário
serializeArray() no form , que pega todos os campos desse formulário e transforma num objeto
Envio por ajax usando o metódo POST que é geralmente o mesmo utilizando no formulário

caso ainda tenha dúvida, tem uma referência nesse link

$("#form").submit(function() {
  event.preventDefault();

  var url = $(this).attr("action");
  /*serializeArray vai pegar todos os campos do array*/
  var formData = $(form).serializeArray();
  console.log(formData);
  $.post(url, formData).done(function(data) {
    console.log(data); //resultado do envio para o servidor
  });

});

<form name="form" id="form" action="enviar.php">

    Name:
    <input type="text" id="name" name="user_name" value="nome" />

    E-mail:
    <input type="email" id="mail" name="user_mail" value="firstname.lastname@example.com" />

    carro:
    <select name="carro">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="opel">Opel</option>
      <option value="audi">Audi</option>
    </select>

    Message:
    <textarea id="msg" name="user_message">Mensagem que será enviada</textarea>

    <button type="submit">Send your message</button>

</form>
Comment: Olá amigo! Obrigado por responder! Só tenho 2 dúvidas. 1 - Esse exemplo que tu mandaste, ele não atualiza a página né? 2 - E como mandar os dados do formulário sem o "input submit" ?
Comment: Isso ele não atualiza a página. A outra dá pra fazer da seguinte forma

`
function enviar(){

  var url = $(this).attr("action");

  var formData = $(form).serializeArray();
  $.post(url, formData).done(function(data) {
  });

}
`

e no html vc pode colocar algo com chamando essa função

`Enviar`

Ou qualquer objeto usando onclick

Enviar
Comment: Amigo! Atualizei a pergunta, para ser mais fácil de entender o que quero!
Answer: esses são os famosos Selects, via jQuery.
Vou explicar e tenho um exemplo funcionando sobre isso:
inclua o jquery no head:

E no body:
<form method="post" action="lista.php" id="Cidade" name="Cidade">
    Selecione sua Cidade
    <select id="cCidade" name="tCidade">
        <option value="">Selecione a Cidade</option>
        <option value="Farroupilha">Farroupilha</option>
        <option value="Caxias do Sul">Caxias do Sul</option>
    </select>
</form>

E crie o arquivo lista.php somente com a consulta e retorno:
//inclua o arquivo de conexão do php e valide se há conexão foi bem sucedida.
$sql = "SELECT empresas FROM empresas";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {

    echo "
".$row['empresas']."
";

  }
}else{
 echo "
Não há filiais nesta região.
";
}

Assim sempre que ele alterar o select a consulta roda e resultado muda na DIV#Resultado. Espero ter ajudado.


The definition of the derivative of $f$. Matrix vs. Linear Mapping.

Question: The following definitions are two definitions of the derivative of $f$.

Definition. (Definition 1) Let $A\subset\mathbb{R}^m$, let $f:A\to\mathbb{R}^n$. Suppose $A$ contains a neighborhood of $a$. We say that $f$ is differentiable at $a$ if there is an $n$ by $m$ matrix $B$ such that $$\frac{f(a+h)-f(a)-B\cdot h}{|h|}\to 0\,\,\,\,\,\,\text{as}\,\,\,\,\,\,h\to0.$$ The matrix $B$, which is unique, is called the derivative of $f$ at $a$; it is denoted $Df(a)$.

Another Definition. (Definition 2) Let $A\subset\mathbb{R}^m$, let $f:A\to\mathbb{R}^n$. Suppose $A$ contains a neighborhood of $a$. We say that $f$ is differentiable at $a$ if there is a linear mapping $B$ such that $$\frac{f(a+h)-f(a)-B(h)}{|h|}\to 0\,\,\,\,\,\,\text{as}\,\,\,\,\,\,h\to0.$$ The linear mapping $B$, which is unique, is called the derivative of $f$ at $a$; it is denoted $Df(a)$.

Suppose we must solve the following problem.

Let $f:\mathbb{R}^2\to\mathbb{R}^2$ be a function such that $f(\begin{pmatrix}x\\y\end{pmatrix})=\begin{pmatrix}e^x\sin y\\x^2 e^y\end{pmatrix}$.
Let $c:=\begin{pmatrix}a\\b\end{pmatrix}$.
Find $Df(c)$.

If we adopt the Definition 1, our answer is like the following:

$Df(c)=\begin{pmatrix}e^a\sin b&e^a \cos b\\2a e^b&a^2 e^b\end{pmatrix}.$

If we adopt the Definition 2, our answer is like the following:

$Df(c)$ is the lienar mapping such that $\mathbb{R}^2\ni \begin{pmatrix}x\\y\end{pmatrix}\to\begin{pmatrix}e^a\sin b&e^a \cos b\\2a e^b&a^2 e^b\end{pmatrix}\begin{pmatrix}x\\y\end{pmatrix}\in\mathbb{R}^2.$

I think Definition 1 is better than Definition 2.
But some authors adopt Definition 2.
I want to know an advantage of Definition 2.

peek-a-boo, Thank you very much for your kind answer.
Let $f:GL(2,\mathbb{R})\ni A\to A^{-1}\in GL(2,\mathbb{R})$.
I checked $Df_A(\xi)=-A^{-1}\xi A^{-1}$ holds when $n=2$ by Wolfram Engine. (Please see the answer by peek-a-boo.)
Comment: I guess matrices force a coordinate system.
Comment: @AndrewZhang Thank you very much for your answer. But I don't understand why forcing a coordinate system is bad.
Answer: Definition 2 using linear transformations is better. This applies more generally even when your vector spaces are not $\Bbb{R}^n,\Bbb{R}^m$. It holds for arbitrary finite-dimensional and even infinite-dimensional vector spaces.
Matrices are a pain conceptually. And computationally, they're a mess to deal with if you're not dealing with $\Bbb{R}^n,\Bbb{R}^m$. If for example you modify your domain to a finite-dimensional space of matrices, even something as innocuous as $M_{a\times b}(\Bbb{R})$, and your target to $\Bbb{R}^n$ or something else, then trying to work with the derivative as a matrix is a mess, because you must first choose a basis for the domain and target, and this is not canonical. If you don't believe me, then look online for the dozens of "matrix calculus" formulae, or just search on this site for the hundreds of matrix-calculus based questions where people are confused by rows vs columns and other silly matters. So many different conventions for the ordering, layouts, transpose issues etc etc. It's all a mess. Of course, to code it into a computer, you have no other choice, but conceptually, and even for many practical hands-on calculations, it's a nightmare; it's only decent if you're restricting yourself to first derivatives, and maps $f:\Bbb{R}^n\to\Bbb{R}^m$. If you want to go beyond first derivatives, or these specific domains and targets, the matrix approach quickly becomes intractable.
Linear transformations, by their very definition are more general, so more widely applicable. Also, it emphasizes the fundamental idea of differential calculus: local approximation by linear functions. Higher derivatives become easier to handle conceptually (they're multilinear maps, and by Schwarz's theorem, they're symmetric). Taylor's theorem becomes easy to state, and it's almost identical to the single-variable case (see here and the links within). And surely, you'd agree Taylor's theorem was important in one-dimension, so understanding it in higher dimensions is all the more important.

Here's a concrete example. Consider the inversion map $f:GL(n,\Bbb{F})\to GL(n,\Bbb{F})$, $f(A)=A^{-1}$ where $\Bbb{F}$ is either $\Bbb{R}$ or $\Bbb{C}$. This is a differentiable map (actually it is analytic), and its derivative at a point $A\in GL(n,\Bbb{F})$ evaluated on $\xi\in M_{n\times n}(\Bbb{F})$ is
\begin{align}
Df_A(\xi)&=-A^{-1}\xi A^{-1}.
\end{align}
(In the case $n=1$, this is like saying $\frac{1}{x}$ has derivative $-\frac{1}{x^2}$, but this way of writing takes the non-commutativity of matrices into account).
Or, if we use differential-forms notation, then
\begin{align}
d(A^{-1})&=-A^{-1}\cdot dA\cdot A^{-1}.
\end{align}
You see, things are very easily expressible and you can understand what's going on. Trying to write the linear transformation $Df_A$ as a matrix requires an $n^2\times n^2$ matrix, which is a complete hassle, and completely obscures what's going on.
Next, an infinite-dimensional example: a whole chunk of calculus of variations can be dealt with using the Frechet derivative, in the context of say the Banach space $C^1([a,b])$. In infinite-dimensions, there are no matrices. Anyway, I don't want to use infinite-dimensionality as a motivation for the linear-transformation approach; even in finite dimensions, the fact that a matrix-representation requires a choice of basis already makes it highly inconvenient.
Comment: the bottom line of my answer is that linear transformations are better because you don't need a basis to talk about them. Why is unnecessarily choosing a basis bad? You should have already seen the answer in a linear algebra course, but one reason is because it is an extra choice, and not canonical. You and your friend might be doing the exact same mathematics, but your answers may look completely different.
Comment: peek-a-boo, Thank you very much for your answer.  I don't understand completely what you are saying, but by your nice example $f(A)=A^{-1}$, I could imagine what you are saying. I will try to check $Df_A(\xi)=-A^{-1}\xi A^{-1}$. Thank you very much for your kind answer.


Is a finite sum of square roots of integers algebraic over the field of rational numbers?

Question: I'm trying to prove it, but it doesn't work: Is the sum of square roots of integers algebraic over the field of rational numbers?
Update: In the discussion, we came to the conclusion that the sum of two algebraic numbers is algebraic. But how to prove that the root of an integer is algebraic?
Comment: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/133422/discussion-on-question-by-n2k-is-a-finite-sum-of-square-roots-of-integers-algebr).
Answer: $p$ and $q$ integers.
Let $x-\sqrt{p}-\sqrt{q}=0$
$x-\sqrt{p}=\sqrt{q}$
$x^2+p-q=2x\sqrt{p}$
$x^4+(p-q)^2+2x^2(p-q)=4x^2p$
$x^4-2x^2(p+q)+(p-q)^2=0$
Now given that $e=1+1+\frac{1}{2!}+\frac{1}{3!}+...$ we have an example of a transcendental number resulting from the sum of infinite algebraics.
While the sum of any two integers' square roots is algebraic, I'm skeptical their infinite sum would be. Not sure how to prove it.
Comment: 1) What have we denoted for x?
2) We prove that infinite sum is algebraic in linked post to my question
Comment: I think those arguments address Finite Extensions. Extending to all integers' square roots would be an infinite extension, no?
Comment: How can this be proved?
Comment: Even if you ignore composite numbers, adding an additional extension extension for each prime will lead to an infinite field extension.

$Q[\sqrt{2}]$ is algebraic. So is $Q[\sqrt{2}][\sqrt{3}][\sqrt{5}]$ ... for any finite number of terms, the sum of those terms is algebraic by the above. That's likely to break down for an infinite number of terms.