Proving convexity for multivariable function

Question: I am trying to prove the convexity for the following function:
$$f(x) = \sqrt{(a^Tx)^2 + (b^Tx)^2}$$ where $\{a,b,x \} \in \mathbb{R}^n$. I showed that the Hessian is positive semidefinite (which proves the convexity), but the equations are very complicated, so I'm trying to use the definition of convex functions to prove it (i.e., $$f(tx_1 + (1-t)x_2) \leq tf(x_1) + (1-t)f(x_2); \ \forall t \in [0,1]$$
However, I cannot seem to be able to prove this. Any ideas on how to approach this? Your help is much appreciated!
Answer: Let $f : \mathbb R^n \to \mathbb R$ be defined by
$$f (\mathrm x) := \sqrt{ (\mathrm a^{\top} \mathrm x)^2 + (\mathrm b^{\top} \mathrm x)^2 } = \sqrt{ \mathrm x^{\top} \mathrm a \mathrm a^{\top} \mathrm x + \mathrm x^{\top} \mathrm b \mathrm b^{\top} \mathrm x } = \sqrt{ \mathrm x^{\top} \left( \mathrm a \mathrm a^{\top} + \mathrm b \mathrm b^{\top} \right) \mathrm x }$$
Let $\mathrm Q := \begin{bmatrix} | & |\\ \mathrm a & \mathrm b\\  | & |\end{bmatrix}$. Hence, $\mathrm a \mathrm a^{\top} + \mathrm b \mathrm b^{\top} = \mathrm Q \mathrm Q^{\top}$ is positive semidefinite and
$$f (\mathrm x) = \sqrt{ \mathrm x^{\top} \left( \mathrm a \mathrm a^{\top} + \mathrm b \mathrm b^{\top} \right) \mathrm x } = \sqrt{ \mathrm x^{\top} \mathrm Q \mathrm Q^{\top} \mathrm x } = \| \mathrm Q^{\top} \mathrm x \|_2$$
Exploiting the subadditivity of $\|\cdot\|_2$,
$$f \left( \gamma \mathrm x_1 + (1-\gamma) \mathrm x_2 \right) = \| \mathrm Q^{\top} (\gamma \mathrm x_1 + (1-\gamma) \mathrm x_2) \|_2 \leq  \gamma \, \| \mathrm Q^{\top} \mathrm x_1 \|_2 + (1-\gamma) \, \| \mathrm Q^{\top} \mathrm x_2 \|_2$$
Thus, $f$ is convex.
Comment: Interesting. Thanks for the response! A couple of questions...I don't understand how the matrix $Q$ is defined...Is it an $n \times 2$ matrix? Also, did you use the triangle inequality in the last equation?
Comment: Yes to both questions.
Comment: Very nice, thanks :)
Answer: Starting out from $f(tx_1 + (1-t)x_2) \leq tf(x_1) + (1-t)f(x_2)$ lets square both sides to get rid of the square-root on the left to get
\begin{align*}
(ta^Tx_1 + (1-t)a^Tx_2)^2 + (tb^Tx_1 + (1-t)b^Tx_2)^2 \leq t^2 ((a^Tx_1)^2 + (b^Tx_1)^2) + (1-t)^2 ((a^Tx_2)^2 + (b^Tx_2)^2) + 2t(1-t)\sqrt{((a^Tx_1)^2 + (b^Tx_1)^2)((a^Tx_2)^2 + (b^Tx_2)^2)}.
\end{align*}
Now writing out the squares on the left and canceling with right hand side terms leaves
\begin{align*}
2ta^Tx_1(1-t)a^Tx_2 + 2tb^Tx_1(1-t)b^Tx_2 \leq 2t(1-t)\sqrt{((a^Tx_1)^2 + (b^Tx_1)^2)((a^Tx_2)^2 + (b^Tx_2)^2)},
\end{align*}
where we can also drop the $2t(1-t)$ factor.
We can square again both sides to get
\begin{align*}
(a^Tx_1 a^Tx_2 + b^Tx_1 b^Tx_2)^2 \leq ((a^Tx_1)^2 + (b^Tx_1)^2)((a^Tx_2)^2 + (b^Tx_2)^2).
\end{align*}
Now multiplying out both sides and canceling leaves
\begin{align*}
2 a^Tx_1 a^Tx_2 b^Tx_1 b^Tx_2 \leq (a^Tx_1)^2(b^Tx_2)^2 + (b^Tx_1)^2 (a^Tx_2)^2,
\end{align*}
which is equivalent to
\begin{align*}
0 \leq (a^Tx_1)^2(b^Tx_2)^2 + (b^Tx_1)^2 (a^Tx_2)^2 - 2 a^Tx_1 a^Tx_2 b^Tx_1 b^Tx_2 = \left(a^Tx_1 b^Tx_2 - b^Tx_1 a^Tx_2\right)^2,
\end{align*}
which now obviously is true and should show the convexity of your function.
Comment: Wow, great! Thanks for the detailed algebra! I thought there was a more simpler explanation (like for example, since we know that $(a^Tx)^2 + (b^Tx)^2$ is convex, then maybe there was a property that stated that the square root of this convex function is convex. But thanks again for the details :)
Comment: I first considered properties of convex functions (https://en.wikipedia.org/wiki/Convex_function) but I didn't see a good way to combine them - the square root complicates things. So I thought I'll instead derive it from first principles. I hope it helps... :-)
Comment: Yes was a great explanation, thanks again :)
Comment: If you think the answer was helpful and it (completely) solves your question you can accept it (set the green check mark) - this way the status is clear to other members of the community that look into it to either provide a different answer or look for the solution :-)


PHP array type hint ignored on my platform

Question: If a PHP function has a parameter type hint (or "type declaration") that says "array", and you call this function with another value, e.g. an integer, there should be a

Fatal error: Uncaught TypeError: Argument 1 passed to foo() must be of the type array, integer given".

Code:
function foo(array $x) {}

foo(5);  // -> Fatal error.

The 3v4l confirms this: https://3v4l.org/7BTtr.
Errors are shown in all relevant PHP versions.

However, I have a local PHP project where the type hint is silently ignored, no error is shown, and subsequent code executes normally.
Some debugging:

If I insert the offending code at the beginning of the script (start of index.php), the error is triggered.
If I insert the offending code some place later in the script, the error no longer appears.

I imagine there is an ini_set() or something which changes the behavior of PHP towards these errors.
But I don't know which PHP setting, if any, would be responsible for ignoring type errors.
Comment: Does code directly after the line where you expect the error to occur get run? If so, then the error isn't happening, so your type declaration has been ignored. If it does not run, but no error shows, then the error occurs but isn't being reported.
Comment: The code after the error was still running, producing a complete web page. See my answer.
Comment: I edited my question to reflect this.
Answer: The problem was caused by a custom error handler function. In PHP 5, if the custom error handler does not return FALSE, the script continues running.
The following demo confirms this: https://3v4l.org/neFdl. Look for the results in PHP5!
In my case it was Drupal 7 with _drupal_error_handler(). This function silently ignores the error and does not show or log anything if it the error code does not match the current value for error_reporting().
This appears stupid, but at least now I know what is happening.
Comment: Seems like a default "Let my page appear slightly broken instead of not at all" function
Comment: Yes, but it also suppresses logging of the error. I wonder what's the rationale behind that.
Comment: Normally, error_reporting() should only specify which errors are displayed, but even those not displayed should still either crash the page or be logged, or both.


Query MySQL to connect 2 databases

Question: I have two tables: one hostgroup_host and the other one hostgroups. The hostgroups represents the names of all the host groups and has 3 important values (hostgroup_id, name and alias). The hostgroup_host makes the connection between the groups and the hosts and has two values (hostgroup_id and host_id).
Example:
hostgroup_id = 1 and host_id = 3 it means that host which id is number 3 belongs to hostgroup with id = 1 (with a specific name).
I have this:
$name=$_POST['name'];
$alias=$_POST['alias'];
$address=$_POST['address'];
$hostgroup=$_POST['hostgroup'];
mysql_connect('localhost:/usr/local/groundwork/mysql/tmp/mysql.sock', $username ,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO hosts (host_id, name, alias, address, hosttemplate_id) VALUES ('','$name','$alias','$address', '1');
INSERT INTO hostgroup_host (hostgroup_id, host_id) VALUES ((select hostgroup_id from hostgroups where name='$hostgroup'), (SELECT
host_id from hosts where name = '$name'))";
mysql_query($query);
mysql_close();
?>
Why doesn't work?
Comment: what exactly doesn't work? what is the error you are getting?
Answer: well, just join your insert statements:
something like:
INSERT INTO TABLE_A (id,name,alias);
INSERT INTO TABLE_B (id,name);

the ; is seperating commands in my sql,
so basicly when you will run the query, both of the insert statements will be run.
Comment: [mysql_query](http://php.net/manual/en/function.mysql-query.php) will only execute 1 query though (multiple queries are not supported), so you will have to execute every query in a separate call.
Answer: I'm a bit confused, but just use more than one link resource.
$linka = mysql_connect('server1', 'mysql_user', 'mysql_password');
$linkb = mysql_connect('server2', 'mysql_user', 'mysql_password');

Then use
$result = mysql_query('INSERT...', $linka);

or
$result = mysql_query('INSERT...', $linka);

But I fear I may have misunderstood your question completely...
Comment: grim imagine one page to add an host: i have 3 textboxes (name, alias and address) and a dropdown menu which lists all the groups available (the host being added is going to belong to one of the groups). After submit I have another php file which makes all those actions. I just want to know how can I pick the hostgroup_id associated to the name on the dropdown menu (the group name) and insert into hostgroup_host associated to the host id.
Answer: Am I missing something or wouldn't it just be better to have the hostgroup_id and the host_id as the option values in the dropdowns?
<select name="hosts">
  <option value="1">Host 1</option>
  ...
</select>

<select name="hostgroups">
  <option value="15">Host with 15 as ID</option>
  ...
</select>

then just insert the post values

Query MySQL to connect 2 databases


When to use open note or closed note for solo on acoustic guitar for the same pitch?

Question: Suppose you want to hit E5 (which is 0 fret on 1st string) in a passage of your solo, what are the reasons you'd pick the open note or the closed note on 5th fret 2nd string?
In general for electric guitar, I always pick the closed note whether it's distorted or clean tone. I find the extra control on muting very useful for amplified sound.
I am not so sure for the case of acoustic guitar solo. Open note sounds louder and attack is stronger. Since the sustain is nowhere as long as electric guitar, I don't find the sound muddy. However, I find that the synchronization is a bit weird. My left hand and right hand are typically in sync. For fretted note, fret and pick occur at the same time. For open note, my left hand has to "pause" a bit.
Answer: This depends on a particular situation. Open string sounds brighter (the harmonics are louder) than the fretted notes so it's a question of which sound connects better with the other notes. There is no objective answer.
Also you can't bend or vibrate an open string, at least not with the regular techniques. Your remark on muting is valid as well.
Concerning technical difficulty, this is certainly something that you can achieve with a bit of practice.
By the way, the top open string of a guitar is E4, not E5, see:
https://en.wikipedia.org/wiki/Guitar#Standard
Answer: Depending on several factors.
As already noted, the sound of an open string is quite different from fretted strings. Were it a short note, amongst many other short notes, it would not sound out of place, played by someone who knew what to do.
As a long note, it can work, although vibrato or bending , as previously stated, are usually out of the remit. But, if playing that note open means a hand position can be changed (no need to hold on to a fretted note) then it's a good idea.
A good player will be able to play an open string to sound like a fretted one, the picking will be different, but that's quite feasible. And if the piece involves alternating between that open string note and several other notes (there's probably a term for that), that's something which the guitar has used for ever.
The synchronisation shouldn't be an issue to a seasoned player.
Answer: This is extremely situation dependent.
Are you playing a scale run up in the 5th position, or some type of arpeggiated chord pattern in the open string position?  Depending on your answer I think the sensible thing to do is play whatever is closest to the other notes in the pattern and creates continuity of sound in the passage.
If your left hand has to pause then you need to practice.  You are probably not familiar with the technique.  You could also play finger style on the acoustic (and electric) creating more opportunities to be in sync (or out of sync depending on you level of development).
Answer: As already stated in other answers, it's situational and subjective.
One way I find it useful to play open notes is on bass strings, when I want to allow them to sustain and slowly decay while I progress with the melody on treble strings an octave or two higher.
Additionally, I sometimes play open treble strings in a similar way. Generally it's in lower positions, often within a lick that also uses the same note closed, as a way to vary tone within the lick, as well as keep the note ringing while the melody progresses through additional notes.
Here's a quick tab of a G Major blues lick that does this. Apologies if my notation is substandard, I don't often write tabs, but at least this shows the notes and whether they're open or closed:

Allow the open notes to sustain while the rest of the lick is played, especially the last double-stop G/D (the first open B is tough not to mute when you hit that next note on the G string, but that's okay, it still works.)
As you can see, Major 3rd (B, in red) is played at the same pitch, both open and closed, to different effects. The first one, on the open B string, is plucked and sustains at least until the next note is played on adjacent G string, whereas the second one (4th fret G string) can be hammered onto from the preceding Bb, and mutes as soon as the finger is removed to pluck the following open G note. It gives this simple 3-note semitone phrase a different flavor than is typically accomplished by playing that note the same way both times, whether both open or both closed.
Comment: Thans for choosing my answer, I'm glad it's helpful. I've realized since posting that I wrote the tab upside down, and have fixed it. Low E string is now at bottom of tab.


How to find the potential function of this vector field?

Question: The instructions are to use the fundamental theory of line integrals to evaluate $$\int_C \cos x \sin y dx + \sin x \cos y dy$$
Where $C:$ Line segment from $(0, -\pi)$ to $(\dfrac{3\pi}{2}, \dfrac{\pi}{2})$
When I have an integral of the form $\int_C g(x, y)$i $ + h(x, y)$j I can find the potential function by integrating $g$ with respect to $x$ and integrating $h$ with respect to $y$, but I don't know how to find it when I have differentials in the integrand.
Answer: You want to calculate $\int \boldsymbol f(x,y)\cdot\mathrm d\boldsymbol l$, where
$$
\boldsymbol f(x,y)=(\cos x\sin y,\sin x\cos y)
$$
and $\mathrm d\boldsymbol l=(\mathrm dx,\mathrm dy)$. We would like to find some $\phi(x,y)$ such that $\boldsymbol f=\nabla\phi$, that is, such that
$$
\frac{\partial \phi}{\partial x}=\cos x\sin y
$$
and
$$
\frac{\partial \phi}{\partial y}=\sin x\cos y
$$
It is easy to see that $\phi=\sin x\sin y$ works just fine. You can find this $\phi$ by integrating, as you said. Integrate the first equation:
$$
\phi(x,y)=\int\frac{\partial\phi}{\partial x}\mathrm dx=\sin x\sin y+c(y)
$$
and plug into the second one:
$$
\sin x\cos y=\frac{\partial \phi}{\partial y}=\sin x\cos y+c'(y)
$$
so that $c(y)$ is any constant (irrelevant to your integral).
NOTE
How can we know that such a $\phi$ may or may not exist? Well, $\boldsymbol f$ is contiuous, and $\text{curl}\boldsymbol f=0$:
$$
\text{curl}\boldsymbol f=\frac{\partial f_x}{\partial y}-\frac{\partial f_y}{\partial x}=\cos x\cos y-\cos x\cos y=0
$$
So that $\phi$ is guaranteed to exist.
NOTE II
I believe you know how to use this $\phi$ to calculate your integral, but just in case, you need the formula
$$
\int \nabla\phi\cdot\mathrm d\boldsymbol l=\phi(P_2)-\phi(P_1)
$$
where $P_1$ and $P_2$ are the boundary points of your segment.
Comment: Keep in mind that $\vec{f}$ has to be continuously differentiable in a simply connected region for curl $\vec{f} = 0$ to be a sufficient and necessary condition for $\vec{f}$ to be conservative in that region.


Independence results that cannot be established by forcing.

Question: I read the Wikipedia article on Absoluteness recently and found mention of Shoenfield's Absoluteness Theorem, which states that if $ \phi $ is any $ \Sigma^{1}_{2} $- or $ \Pi^{1}_{2} $-sentence of second-order arithmetic, then $ \phi $ is absolute between any two models of $ \sf ZF $ that have the same ordinals. The article says that the Riemann Hypothesis and the $ \sf P $-versus-$ \sf NP $ Problem can be expressed as $ \Pi^{1}_{2} $-sentences, so if these two statements happen to be independent of $ \sf ZF $, then it is not possible to use the method of forcing to establish their independence.
Ever since Cohen invented forcing in 1963, no other essentially new technique has been found of creating models of set theory (maybe inner model theory counts as a new technique in the sense of the post-1970 works of John Steel and Ronald Jensen on the core model, but I believe that the main idea behind inner model theory goes back to either von Neumann or Gödel). Hence, to cut the long story short, my question is:

Do current experts in set theory have any idea about what new methods of proving
    independence results would involve? Do they believe that finding a new method of
    proving independence results is within the reach of current technology?
Comment: There are actually at least two very interesting programmes that run orthogonal to developing new techniques of establishing independence from ZFC.  Woodin's ["Ultimate $L$" programme](http://www.andyross.net/woodin.htm) (see also the [posts from Andrés Caicedo's blog](http://andrescaicedo.wordpress.com/2010/10/19/luminy-hugh-woodin-ultimate-l-i/)) and Sy Friedman's ["Hyperuniverse" programme](http://www.logic.univie.ac.at/~sdf/jtf.hyper.description.pdf) have the aim of _refining/restricting_ our concept of the "universe" of sets.  (In particular, Ultimate $L$ would imply that CH is true!)
Comment: Inner models began with von Neumann and his proof that regularity can be added or removed without affecting the consistency of the theory.
Comment: @Asaf: It's always a pleasure to receive your quick responses! :) Thanks for clarifying that point.
Comment: I also have two problems with the question posted *here*. First of all, not that many *experts* roam this site. There are a few, and there are a few more in MathOverflow. But I don't know if enough people to actually answer. The second issue is that many problems which are "of interest of set theorists" are not number theoretically expressible. That is, they are not problems one can write in a $\Pi^1_2$ statement, or so, in the language of arithmetic. So even if a new method would arise, there is no guarantee that it will have any implications on RH and so on.
Comment: And lastly, to drive that second point home, what Shoenfield's theorem is really telling us that if you want to prove that RH is independent of ZF you will have to come up with a way of producing models with different ordinals. Otherwise the same issues as with forcing come up again. But set theorists love the fact that the ordinals between a model and its extensions are the same. For example the rank is absolute, so if you have the same ordinals then the amount of new sets that were added was "reasonable". If you add new ordinals, or change them altogether, how can you control the additions?
Comment: Would it then be appropriate to migrate this to MathOverflow? I don't know how to do that.
Comment: Well, I'd have asked it on MO to begin with, but you can leave it here for a week or so, and see what sort of answers you gather (after all, there *are* expert set theorists using this site on a regular basis). To migrate you have to repost this on MathOverflow manually. The networks are currently disjoint and no transfer of "goods" is possible.
Answer: Nice question. Let me first point out that the Riemann Hypothesis and $\mathsf{P}$-vs-$\mathsf{NP}$ are much simpler than $\Pi^1_2$: The former is $\Pi^0_1$, see this MO question, and the assertion that $\mathsf{P}=\mathsf{NP}$ is a $\Pi^0_2$ statement ("for every code for a machine of such and such kind there is a code for a machine of such other kind such that ..."). The difference in the superscript is that these are arithmetic statements, so we do not need to quantify over reals.
I think versions of your question have been asked before here and on MO, so you may find additional remarks if you search there for a bit. In particular, let me point you to this question, and my answer there. Briefly:
A natural approach to showing that a statement is independent of $\mathsf{ZFC}$ is to show that it holds in a small inner model, such as $L$, while its negation follows from appropriate large cardinal hypotheses. For example, projective determinacy and many of its consequences are statements of this kind. In fact, more and more levels of the projective hierarchy are forcing invariant as we climb through the consistency strength hierarchy. (For a bit more on this, see this MO question.) There is much more that can be said on the role of large cardinals, the inner model hypothesis, the ultimate $L$ program, etc, but I do not think these are examples of the developments your question is about, and this still does not address statements that are $\Pi^1_2$ or of lower complexity.
It would be a major breakthrough (larger even than the development of forcing) if we figure out a technique that changes (truth in) the standard model of arithmetic.

If at all possible, I do not think we are anywhere near achieving that.

Short of it, work for establishing consistency/independence of  arithmetic or low-level projective statements focuses on producing non-standard models, in fact, models that are not $\omega$-models (that is, models whose version of $\omega$ is non-standard).
Harvey Friedman has been particularly successful here, and he has produced a body of examples from finite combinatorics that are independent of $\mathsf{ZFC}$. The origin of this idea comes from the Paris-Harrington theorem showing the independence of a strengthening of Ramsey's theorem from $\mathsf{PA}$ via the method of indicators.
Most of his approach is explained in his monograph on Boolean Relation Theory, available from his page. His results tend to involve large cardinals as well. The idea is that we use the large cardinals to prove the combinatorial statement under consideration. For arithmetic statements $\phi$, this of course means that the result is true in $\omega$. We then produce appropriate non-$\omega$-models of $\mathsf{ZFC}$ where the statement fails. The construction here is delicate, and not as malleable as using forcing. But his monograph and many of his papers show plenty of applications.
You see why this approach is slightly unsatisfactory, since we know which statement ($\phi$ or $\lnot\phi$) is true, regardless of whether $\mathsf{ZFC}$ is strong enough to establish it. (This is why it would be so significant to have "forcing-like" tools affecting the standard model of arithmetic.) On the other hand, the large cardinals typically involved in many of his examples are "small" (in the neighborhood of Mahlo cardinals), so we may as well think of his approach as producing examples of statements independent of $V=L$, which in itself is remarkable.
Still, we are far from what would be ideal. There is a quote from the Boolean Relation Theory book that I included in one of the links above, but let me repeat it here:

There were no candidates for Concrete Mathematical Incompleteness from ZFC being offered. In fact, to this day, no candidates for Concrete Mathematical Incompleteness have arisen from the natural course of mathematics.

To drive the point home: I do not see any way of adapting the known techniques to establish the independence of "natural" problems such as the Riemann hypothesis, $\mathsf{P}=\mathsf{NP}$, or many of their humbler relatives. Even forgetting $\mathsf{ZFC}$ and doing this in the context of $\mathsf{PA}$ seems out of reach, even though there is a large literature on models of $\mathsf{PA}$ and (arithmetic) combinatorial statements independent of $\mathsf{PA}$, with plenty of techniques not available for set theory.
Comment: Thank you so much for this wealth of information!


How to convert SOAP methods to restful API?

Question: I am new to this world of Rest services, I have simple SOAP service with names
withDrawCashFromAccount(int id, int amountToWithDraw)
depositCashToAccount(int id, int amountToDeposit)

which are self explanatory, this was a simple service as the management team has decided to move to restful architecture, i face the following problems on how to convert about two methods in restful API

In order to change a single column in a resource, do we need send the
complete JSON structure of the resource ?.

resource name "Account"
e.g Json structure:

        id : 1,
        name : "awesome customer",
        Amount : 500

In order to have above two type of simple use cases where user can
withdraw cash or user can deposit, I am puzzled on how can i design the
end points for rest service. Can I name them
/account/depositCash/id,
/account/withdrawCash/id

Anything else I need to think like Idempotent nature of above calls?

Currently using spring boot as from this example
Comment: If you'd follow the REST architecture you'd model a balance resource where the actions performed on that resource would lead to the deposit or withdrawal as a consequence. REST furthermore uses a backing transportation protocol (HTTP) to interact with the resources. The semantics of these operations define the available toolset. Each operation has its own semantics, `POST` i.e. is defined by the server implementor himself. The server should though support clients in generating requests, how you do this is totally up to you and your use case.
Comment: @RomanVottner Any specific reason to mode a "balance" resource  as it will only have only one variable as similar as Account
Comment: It all depends on how you model something. If you log into your online bank account the balance sheet might include recent deposits or withdrawals which sum up to your current balance amount. Each transaction could be drilled down further via usage of HATEAOS (links). User meta data like name, customer number or what not could be exposed also via its own resource that links to the balance resource and stuff like that. Why to do it that way? Because performing operations on different resources convey different semantics and you might reuse resources later on naturally anyways.
Answer: You do not necessarily need to send the entire Account object, and you should be able to name your endpoints whatever you like, as long as they stem from the same base URL; it all depends on how you have your services and POJOS configured.
For instance, using JAX-RS, you can annotate a service class with @Path("path/you/want/here") to specify a base path for all methods accessible in that service, followed by more specific @Path annotations for each method to give them unique paths (the method level @Path annotation gets added to the end of the class level one). You can annotate your public methods with HTTP verb annotations like @GET or @POST to specify which verb that method will take.
In your REST endpoint methods, you can deserialize the incoming JSON into POJOs using Jackson, going as far as to specify which attributes are required, how they should be deserialized/serialized, and so on. As long as you don't need the attributes or you are expecting them to not always be there, you can simply allow missing POJO fields to deserialize to null.
Comment: Any inputs on how to handle transactions in Rest world ?
Comment: Depends on whether you want to be truly stateless or not. If yes, then you would need to store temporary data (user shopping carts, for example) in your backend in memory or a backend cache, or re-send the same data with every REST request. If no, then you can get away with handling session variables or sticking the info in cookies.

I am no transactional expert, though, so take my advice in this comment with a grain of salt and do some research before committing to a decision.
Comment: What do you mean with rest endpoint methods? In this case would they be microservices, given that you are not treating it as resources?


Training loss after last epoch differs from training loss (same data!) during evaluation

Question: I am building a deep convolutional model with a custom loss function. As a first step, I am trying to bring training loss down as far as possible to see if my model can overfit.
Training with on only one batch, the model can reduce the training loss to almost zero. But when I evaluate it on the same data it has been trained on, the loss is by magnitudes larger than even the test loss. It is even considerably larger than the loss for randomly generated predictions.
For training and evaluation I use the standard Keras model.fit and model.evaluate functions:
history = model.fit(x=training_batch_generator,
                epochs=500,
                validation_data=validation_batch_generator,
                callbacks = [stop_early, tensorboard, checkpoints])

training_loss = model.evaluate(x=yolo_training_batch_generator)

I use subclassed keras.utils.Sequence for the data:
class YoloSequence(Sequence):

def __init__(self, x_set, y_set, batch_size, grid_len):
    self.x, self.y = x_set, y_set
    self.batch_size = batch_size
    self.grid_len = grid_len

def __len__(self):
    return (np.ceil(len(self.x) / self.batch_size)).astype(np.int)

def __getitem__(self, idx):
    batch_x = self.x[idx * self.batch_size : (idx + 1) * self.batch_size]
    batch_y = self.y[idx * self.batch_size : (idx + 1) * self.batch_size]

    image_batch = [preprocess_image(path) for path in batch_x]
    label_batch = [preprocess_label(path, self.grid_len) for path in batch_y]

    return np.array(image_batch), np.array(label_batch)

And a custom loss function:
class YoloLoss(keras.losses.Loss):
def __init__(self, name="yolo_loss", **kwargs):
    super().__init__(name=name, **kwargs)

def call(self, y_true, y_pred):
    # shape of y_true: batch-size, GRID_SIZE, GRID_SIZE, 19
    # 19: [conf, x, y, w, h, 0, 0, 0, 0, 0, p[0], p[1], ..., p[8]]

    # get y_pred into same format as y_true:
    y_pred = tf.cast(K.reshape(y_pred, (-1, GRID_LEN, GRID_LEN, 19)), dtype=tf.float32)
    y_true = tf.cast(y_true, dtype=tf.float32)

    # compute ious (each iou of shape [1, batchsize, gridsize, gridsize, 1], one iou for each cell):
    iou_bb1 = K.expand_dims(self.compute_iou(y_pred[..., 1:5], y_true[..., 1:5]), axis=0)
    iou_bb2 = K.expand_dims(self.compute_iou(y_pred[..., 6:10], y_true[..., 1:5]), axis=0)

    ious = K.concatenate([iou_bb1, iou_bb2], axis=0) # shape:  [2, batchsize, gridsize, gridsize, 1]

    # bestbox: box that is responsible for a given cell [batchsize, gridsize, gridsize, 1]:
    bestbox = K.cast(K.argmax(ious, axis=0), dtype=tf.float32)

    # exists_box: for each cell in every batch, does there exist a box? shape: [batchsize, gridsize, gridsize, 1]
    exists_box = K.expand_dims(y_true[..., 0], axis=3)

    ################
    ### box loss ###
    ################
    # if a box exists, use predictions of best box:
    xy_pred = (bestbox * y_pred[..., 6:8]) + ((1 - bestbox) * y_pred[..., 1:3])
    box_predictions_xy = (exists_box * xy_pred)
    box_targets_xy = (exists_box * y_true[..., 1:3])

    # square-root of width and height(same change is less important in larger box):
    wh_pred = ((bestbox * y_pred[..., 8:10]) + (1 - bestbox) * y_pred[..., 3:5])
    box_predictions_wh = (K.sign(exists_box * wh_pred) * K.sqrt(K.abs(exists_box * wh_pred)+ 1e-6)) # derivative of squareroot as you go to zero: infinity, so add 1e-6 for numerical stability
    box_targets_wh = (K.sqrt(exists_box * y_true[..., 3:5]))

    mse = tf.keras.losses.MeanSquaredError(reduction=tf.keras.losses.Reduction.SUM)
    box_loss = mse(box_predictions_xy, box_targets_xy) + mse(box_predictions_wh, box_targets_wh)

    ###################
    ### object loss ###
    ###################
    confidence = (bestbox * y_pred[..., 5:6]) + ((1 - bestbox) * y_pred[..., 0:1])
    best_ious = tf.where(
        K.cast(bestbox, tf.bool),
        K.reshape(iou_bb2, (-1, GRID_LEN, GRID_LEN, 1)),
        K.reshape(iou_bb1, (-1, GRID_LEN, GRID_LEN, 1))
    )
    object_loss = mse((exists_box * confidence), (best_ious * y_true[..., 0:1]))

    ######################
    ### no object loss ###
    ######################
    no_object_loss = mse(((1 - exists_box) * confidence), ((1 - exists_box) * y_true[..., 0:1])) # second term is all zeros

    ##################
    ### class loss ###
    ##################
    class_loss = mse((exists_box * y_pred[..., 10:]), (exists_box * y_pred[..., 10:]))

    ##################
    ### total loss ###
    ##################
    lambda_coord = 5
    lambda_noobj = 0.5
    loss = (
        lambda_coord * box_loss
        + object_loss
        + lambda_noobj * no_object_loss
        + class_loss
    )

    return loss

Comment: Does the network use any elements which change behavior between training and evaluation, such as dropout or batch norm?
Comment: Yes, I'm using batch norm after conv layers. Why would that change what happens between training and evaluation?
Answer: In comments, OP writes that the network uses batch-normalization. This explains the observed behavior, because  neural networks with batch norm change how statistics are computed, depending on whether the network is in training mode or evaluation mode.
During training, batch norm updates a running estimate of the mean and standard deviation of the inputs. When computing results outside of training (e.g. when applied validation or production data), these values are not changed by the input, so there will be a different result.

In Keras, this behavior is exposed as the difference between model.fit(), model.evaluate() and model.predict(). See: https://stackoverflow.com/questions/44843581/what-is-the-difference-between-model-fit-an-model-evaluate-in-keras

In pytorch, you can set this behavior using model.train() to set train mode, or model.eval() to set evaluation mode (colloquially "eval mode"). See: https://stackoverflow.com/questions/60018578/what-does-model-eval-do-in-pytorch

More information can be found in Ioffe, Sergey, and Christian Szegedy. "Batch normalization: Accelerating deep network training by reducing internal covariate shift." International conference on machine learning. PMLR, 2015.
Comment: Thank you, that helped! Using the same model without batch norm yields very similar training and evaluation loss on training set (0.245 and 0.284 respectively)


How can I force Jquery animations to occur when the window is not focused

Question: Okay,
So I am building an ajax based game. Anyway it is multiplayer, connects to a server. It polls the server, getting updates all the time. Anyway the game has different game states, and must animate the GUI based on the game state.
The problem is, however; When the window is not focused, jquery animations queue up and do not render. This means say a user walks away for 5 mins and comes back, a huge queue of obsolete animations start to run through one by one.
So I was wondering if there is any way to force jquery animations to run when they are called and not wait for a focused window?
For further information on why this happens, read Additional Notes, in the jquery documentation here http://api.jquery.com/fadeIn/
Cheers, Josh
Comment: I think that's a feature of the browser that helps to reduce memory footprint or resource usage.  You might not be able to fix that inside of page code.
Answer: Depending on the animations, you could add onfocus event on the body element that gets the animation queue by calling .queue() and if it's longer than one element, you could call .clearQueue(). Of course you'd have to be careful not to mess up the layout, since it removes all the animations.
Also you could try doing jQuery.fx.off = true if the list is longer than one element. This should set the duration of all animations to 0, which should result in all of the queued animations being finished instantly, and to enable animations once again, set the value to false. However, since I haven't tried it myself, I'm not sure if it sets the duration only on the animations that haven't yet been run (already in a queue) or on only those that haven't yet been added to a queue. If the latter case is true, you could loop through the queue and set each duration to 0 manually.
Comment: I have tried :

 $(window).focus(function(){jQuery.fx.off = false;});

 $(window).blur(function(){jQuery.fx.off = true;});

 will post back if it keeps working.
Answer: Similar to Zanfa's answer - you could use the focus event, but you could also use it in combination with the blur event.
So rather than trying to clean everything up on focus, you could use the blur event to cleanly pause the game, stop any animations and then later use the focus to get things going again.


Expressing non-polynomial functions in formal system of elementary number theory

Question: I am considering formal system of elementary number theory as given in Kleene's "Introduction to Metamathematics". If you want me to specify axioms please let me know. In his book "Mathematical logic" he writes that "Although only polynomials can be expressed in this formal system by means of terms, the representing predicates $F(x_1, ..., x_n, y)$ of a vastly greater class of functions can be expressed." He gives an example that "Let $F(x,y)$ express $x!=y$. Take the proposition $(x+1)! = (x+1)x!$, which is an informal theorem about factorial function. This can be paraphased in terms of predicate $F(x,y)$ e.g. thus : $$\exists u \exists v (F(x+1,u) \land F(x,v) \land u=v\cdot (x+1))."$$
My question is how one expresses $x! = y$ as $F(x,y)$? Also, for other functions like $x^{n}=u$ how does one express them using formulas of the formal system?
Answer: These are primitive recursive functions, so can be implemented in arithmetic (if not already in the language) using the beta function trick.
The beta function is an arithmetical function such that if $(a_0,a_1,\ldots, a_n)$ is a sequence, then there are natural numbers $a,b$ such that $\beta(a,b,i)=a_i$ for all $0\le i \le n.$
For $x!=y,$ we say "there is a sequence $a_0,a_1,\ldots, a_x$ such that $a_0=1,$ $a_x=y$ and for all $i < x,$ $a_{i+1} = (i+1)\cdot a_i$". In terms of the beta function, this is $$ \exists a,b(\beta(a,b,0)=1\land \beta(a,b,x)=y\land \forall (i< x)(\beta(a,b,i+1)=(i+1)\cdot\beta(a,b,i)).$$


PHP: Funções, Parâmetros, SOAP

Question: Eu tenho o seguinte código com duas funções:
public function getClientTemplates(){
    $client_id = 31;

    $this->Send('client_get', compact(array('client_id')));

}

public function Send($action, $post){

    extract($post);

    try {
        $data = $this->soap->$action($post);
    } catch (SoapFault $e) {
        $this->addError('ISPConfig said: SOAP Error. '.$e->getMessage());
    }
}

Eu tenho uma chamada SOAP que necessita de alguns parâmetros, mas não tenho como saber quantos parâmetros a função da vez pede.
Como está ai no código eu compacto e extraio as variáveis, mas não sei como envia-las sem saber quais e os nomes delas.
Answer: no seu método Send(), você pode tratar o retorno de __getFunctions para obter a lista de argumentos necessários. Com este retorno você pode criar um hash onde cada argumento é uma chave. Depois disto de um merge neste hash criado com o seu compact.
Talvez não seja a melhor solução, mas na falta de algo melhor é uma opção.
ex. ("de cabeça" e não testado pois estou sem PHP aqui e não programo PHP há muito tempo):
public function Send($action, $post){

  extract($post);

  $methods = $this->soap->__getFunctions();
  foreach ($methods as $method) {
    preg_match_all('/[ \t\r\n]*[ \t\r\n]?(.+)[ \t\r\n]*\(([^\)]*)\)/', $method, $matches);
    $method_name = $matches[2];
    $method_args = $matches[3];
    if ($method_name == $action) {
      try {
        $data = $this->soap->$action(array_merge($method_args, $post));
      } catch (SoapFault $e) {
        $this->addError('ISPConfig said: SOAP Error. '.$e->getMessage());
      }
      break;
    }
  }
}

Comment: É uma solução válida sim, valeu Daniel

PHP: Funções, Parâmetros, SOAP


hook_menu() (edit) not working

Question: I am doing something wrong and am not able to find what.
I have defined the hook_menu for my module, add and delete are working fine, but edit and view are not. Is like are never being called.
function nutrition_menu()
{
  $items['admin/structure/ingredients'] = array(
    'title' => 'Manage Ingredients.',
    'description' => 'Manage Ingredients.',
    'access arguments' => array('administer ingredients'),
  );

  $items['ingredient/%ingredient'] = array(
    'title callback' => 'ingredient_page_title',
    'title arguments' => array(1),
    'page callback' => 'ingredient_page_view',
    'page arguments' => array(1),
    'access arguments' => array('view ingredients'),
    'type' => MENU_CALLBACK,

  );

  $items['ingredient/%ingredient/view'] = array(
    'title' => 'View',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,

  );

  $items['ingredient/%ingredient/edit'] = array(
    'title' => 'Edit',
    'page callbacks' => 'ingredient_page_edit',
    'page arguments' => array(1),
    'access arguments' => array('update ingredients'),
    'weight' => 2,
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,

  );

  $items['ingredient/add'] = array(
    'title' => 'Add new Ingredient:',
    'page callback' => 'ingredient_add',
    'access arguments' => array('create ingredients'),
    'weight' => 1,
    'menu_name' => 'management',
    'file' => 'ingredient.pages.inc',
  );

  $items['ingredient/%ingredient/delete'] = array(
    'title' => 'Delete',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('ingredient_delete_confirm',1),
    'access arguments' => array('delete ingredients'),
    'weight' => 1,
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,

  );

  return $items;
}

load functions
function ingredient_load( $iid = NULL, $reset = NULL)
{
  $iids = (isset($iid) ? array($iid) : array() );
  $conditions = array();
  $ingredient = ingredient_load_multiple($iids, $conditions, $reset);
  return $ingredient ? reset($ingredient) : FALSE;
}

function ingredient_load_multiple($iids=array(),$conditions=array(),$reset=FALSE)
{
  return entity_load('ingredient', $iids, $conditions, $reset);
}

Callbacks
function ingredient_page_view($ingredient,$view_mode='full')
{
  $ingredient->content = array();

  field_attach_prepare_view('ingredient', array($ingredient->iid=>$ingredient),$view_mode);
  entity_prepare_view('ingredient',array($ingredient->iid=>$ingredient));
  $ingredient->content += field_attach_view('ingredient',$ingredient,$view_mode);

  return $ingredient->content;
}

function ingredient_page_edit($ingredient)
{
  var_dump($ingredient);

  drupal_set_title(t('Edit @name', array('@name' => $ingredient->name)), PASS_THROUGH);

  return drupal_get_form('ingredient_form',$ingredient);
}

Basically what I see is the two tabs ( View, Edit ) with no information bellow, the title isn't changing either.
Any idea?
Comment: What do you want to achieve? To me this looks like you're building an ingredient content type. If yes, almost all the code you show is not necessary. Have a look at the example module about how (http://drupal.org/project/examples) to build content types. There's even a good chance, that fields will do everything for you, which means you don't have to code anything.
Comment: I am creating a module ( entities included ) this is just a piece of code I copied and adapted from a book. Thanks for the advise though
Answer: The 'edit' page probably isn't working because you've mis-spelled page callback (it's currently spelt page callbacks).
Comment: Thank you @Clive. The Only thing missing is that view is not showing anything. Thanks!

hook_menu() (edit) not working


Roslyn to insert nodes after specified node

Question: I'm writing a code analyzer which inverts an if statement to reduce nesting.
I'm able to generate a new if node and replace it to the document root. However I must move all content(statements) coming from this if statement to below it. Let me show what I've achieved so far:
var ifNode = @if;
var ifStatement = @if.Statement as BlockSyntax;
var returnNode = (ifNode.Parent as BlockSyntax).Statements.Last() as ReturnStatementSyntax ?? SyntaxFactory.ReturnStatement();
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var invertedIf = ifNode.WithCondition(Negate(ifNode.Condition, semanticModel, cancellationToken))
.WithStatement(returnNode)
.WithAdditionalAnnotations(Formatter.Annotation);
var root = await document.GetSyntaxRootAsync(cancellationToken);
var newRoot = root.ReplaceNode(ifNode, invertedIf);
newRoot = newRoot.InsertNodesAfter(invertedIf, ifStatement.Statements); //It seems no to be working. There's no code after specified node.

return document.WithSyntaxRoot(newRoot);

Before:
public int Foo()
{
    if (true)
    {
        var a = 3;
        return a;
     }

     return 0;
}

After:
public int Foo()
{
    if (false)
        return 0;

    var a = 3;
    return a;
}

Comment: It's a little tough (maybe just for me) to see what you're trying to achieve. Could you provide a before and after of what you'd like to achieve?
Comment: As @JoshVarty asked there's an example for before/after.
Comment: How are you doing the @If syntax?
Answer: Carlos, the problem is that after you ReplaceNode you generated a new node. When you go InsertNodeAfter and pass a node from the original root node, the new node can't find it.
In an analyzer you need to either do all the changes at once, or annotate or track the nodes so you can come back to them later.
But since you are replacing a node first, the new node will be exactly at the same place. So you can shortcut and FindNode, like this:
newRoot = newRoot.InsertNodesAfter(newRoot.FindNode(ifNode.Span), ifStatement.Statements);

I haven't tested this code, but it should work.


Computing a summation of products more efficiently via factorization

Question: Let $P = \{p_1, \dots, p_n\}$ (may be a multiset). I wish to compute $\sum\limits_{S \subseteq P \\ |S| = k} \prod\limits_{i \in S} i \prod\limits_{i \not\in S} (1-i)$ in an efficient manner. The naive approach takes time proportional to $\binom{n}{k},$ which makes it too slow for the case $n=200, k=100.$
The case where all elements of $P$ are equal is trivial, but even the case where $n$ elements are $p$ and $n$ elements are $q$ reduces to computing $\sum\limits_{i=0}^k \binom{n}{i}\binom{n}{k-i}p^i (1-p)^{n-i} q^{k-i} (1-q)^{n-k+i},$ for which the evaluation of the tricky summation $\sum\limits_{i=0}^k \binom{n}{i}\binom{n}{k-i} x^i$ is required.
Comment: Nice – I didn't know that you can get a large font-size for two-line limits by using `\\ ` instead of `\atop`. Ah, but they don't get centred properly then.
Answer: You can factor this one element at a time. First let's simplify the problem by dividing by $\prod_{i\in P}(1-i)$; then you just need to determine the sum
$$
a(Q,k)=\sum_{S\subseteq Q\atop|S|=k}\prod_{i\in S}i
$$
for $Q=f(P)$, with $f(x)=\frac x{1-x}$. Start with the first element and write
$$
a(Q,k)=q_1a\left(Q\setminus\{q_1\},k-1\right)+a\left(Q\setminus\{q_1\},k\right)\;,
$$
and continue recursively for all elements. That yields a Pascalesque triangle in which you only have to perform $O\left(n^2\right)$ operations. Instead of just adding two elements as you do for Pascal's triangle, here you multiply the left element with a factor $q_i$ associated with the row before adding it to the right element.
Another way to think about it is in terms of generating functions: You're looking for the coefficient of $x^k$ in
$$
\prod_{i\in Q}(1+ix)\;,
$$
or, in your original formulation with $P$, the coefficient of $x^k$ in
$$
\prod_{i\in P}(1-i+ix)\;.
$$
Of course you can also write the triangle approach directly in terms of $P$:
$$
a(P,k)=p_1a\left(P\setminus\{p_1\},k-1\right)+(1-p_1)a\left(P\setminus\{p_1\},k\right)\;;
$$
it just seemed easier to think of it at first if we only have factors for the selected elements.
Comment: I found that recursion, but the time estimate seems to still be $\binom{n}{k}.$ Denoting $T(n,k)$ as the time taken to calculate $a(P,k)$ when $|P|=n,$ we have $T(n,k) = T(n-1,k-1) + T(n-1,k),$ which has the solution $T(n,k) = \binom{n}{k}.$
Comment: I see how to make the recursion $O(n^2)$ now. You left out the important step of computing from the bottom up: compute $a(\emptyset, 0),$ then $a(\{p_n\}, 0), a(\{p_n\}, 1),$ then $a(\{p_{n-1}, p_n\}, 0), a(\{p_{n-1}, p_n\}, 1), a(\{p_{n-1}, p_n\}, 2),$ then ...
Comment: @Displayname: I don't understand. How other than from the bottom up could you apply the recurrence relation? (Also, I thought that the analogy to Pascal's triangle made it quite explicit how to do the calculation. How do you generate Pascal's triangle by adding two elements, if not from the bottom up (or, as it's usually oriented, from the tip down)?)
Comment: Given the recursion $f(m,n) = af(m-1,n-1)+bf(m-1,n),$ one approach is to compute $f(m-1,n-1), f(m-1,n)$ using the recursion and then perform the necessary arithmetic to get $f(m,n).$ I didn't consider the bottom up approach until I remembered that you do the same with linear recurrences.
Comment: @Displayname: I'm afraid we're talking past each other. I don't see what else you could do than compute $f(m-1,n-1)$, $f(m-1,n)$ using the recursion and then perform the necessary arithmetic to get $f(m,n)$. I get the impression that perhaps you mean something else: That it hadn't occurred to you to memoize the intermediate values and you were recalculating them each time you needed them at some stage. That would indeed be very inefficient. But that's not a question of top-down or bottom-up; it's a question of not calculating the same thing more than once.
Comment: @Displayname: In other words: There are only $O\left(n^2\right)$ different values to be calculated here, no matter how you organize the calculation. The only thing you can do wrong to get complexity greater than $O\left(n^2\right)$ is to calculate them more than once.
Answer: We can make use of generating functions here.
Suppose we are given two lists or multisets of equal length
$\, P = (p_1,\ldots,p_n)\,$ and $\, Q = (q_1,\dots,q_n).\,$
Use the useful notation $\,[n] := \{1,\ldots,n\}.\,$
Define the sum of products
$$ a_k(P,Q) = a_k := \sum_{S\subseteq [n],|S|=k}
 \prod_{i\in S}P_i \prod_{i\notin S}Q_i. $$
Define the generating function of $\,a_k\,$ to be
the polynomial
$$ f(x) := \sum_{k=0}^n a_k(P,Q)x^k = \prod_{i=1}^n (x\,p_i+y_i) $$
where the equality to the product follows from the
definition of $\,a_k\,$ and the distributivity of
multiplication over addition. We want to calculate some
of the coefficients of $\,f(x).\,$ That is,
 $\,a_k=[x^k]f(x).\,$ An efficient approach is to truncate
$\,f(x)\,$ and keep coefficients up to order $\,x^k\,$
when evaluating the product. This is a generalization of
the binomial theorem for evaluating $\,(p+q)^n\,$ using
binomial coefficients and computing them by the usual
Pascal triangle recursion.


Qual a diferença entre build e linkagem?

Question: Já vi algumas representações em que o build está no mesmo lugar de quando acontece a linkagem, os dois são a mesma coisa?
Comment: A resposta resolveu sua dúvida? Acha que pode aceitá-la? Veja o [tour] se não souber como faz. Isso ajudaria muito indicar que a solução foi útil para você. Também pode votar em qualquer pergunta ou resposta que achar útil no site todo.
Answer: Não são o mesmo. A linkagem é um dos passos que o build realiza. O build pode realizar muitas coisas, até construir a aplicação ou solução completa, alguns desses passos que nem são parte do processo de criação do executável, mas certamente gerar o executável é o mais importante.
Em geral você tem pelo dois passos muito claros: a compilação básica; a linkagem.
Mas algumas tecnologias podem não ser bem assim, ou por serem interpretadas ou a linkagem não conseguir ser dissociada da compilação, ainda que internamente sejam distintas.
O termo desta forma é mais usado em linguagens que são usadas de forma mais direta e clássica como C, C++ ou Rust.


Why can't we factor $a^n + b^n$ for $n$ even in same way as $a^n + b^n$ for $n$ odd?

Question: Why can't we factor $a^n + b^n$ for $n$ even in same way as $a^n + b^n$ for $n$ odd?
For $n$ odd we have $a^n + b^n = (a + b)(a^{n - 1} - a^{n-2}b + \ldots - ab^{n - 2} + b^{n - 1})$. Not sure why we can't do the same for $a^n + b^n$ when $n$ is even.
Comment: Does this answer your question? [Factorization of $a^n+b^n$](https://math.stackexchange.com/questions/3684655/factorization-of-anbn)
Answer: If $n$ is even, the second factor no longer has a $b^{n-1}$ term; it's
$$a^{n-1}-a^{n-2}b+\ldots\color{red}+ab^{n-2}\color{red}-b^{n-1}\,,$$
and when you multiply that by $a+b$ you get $a^n-b^n$, not $a^n+b^n$.
Answer: You can often factor $a^n+b^n$ even when $n$ is even if $n$ has a odd factor.But we can't do it the same way for $n$ odd case.
It is because $a+b$ doesn't divide $a^n+b^n$ in general for even $n$.(use $a\equiv -b \pmod{a+b}$ )
Answer: You could do it by using complex numbers. For instance
$$
a^2 + b^2 = a^2 - (ib)^2 = (a +ib)(a-ib)
$$
and more generally,
$$
a^n + b^n = (a-z_1b)(a-z_2b) \dotsm (a-z_nb)
$$
where $z_1, \ldots, z_n$ are the complex roots of the equation $z^n = -1$.


extracting check-box data from PDFs with Azure Read/OCR API

Question: I have 1000s of survey forms which I need to scan and then upload onto my C# system in order to extract the data and enter it into a database. The surveys are a mix of hand-written 1) text boxes and 2) checkboxes. I am currently using the the Azure Read Api to extract hand-written text which should work fine e.g. question #4 below returns 'Python' and 'coding'.
So my question; will any Azure API (Read or OCR etc.) give me the capability to extract data for which checkbox is marked? e.g. see question #1 below - I need a string back saying 'disagree', is this possible with any Azure API or will I need to look elsewhere? If so, what API or library can I use to get hand-written checkbox data?
Can somebody with iText7 or IronOCR tell me if these libraries would allow me to extract the checkbox data below?
Survey Example:
Comment: I think for what you need to do you need to rasterize the PDF to an image and then find a known anchor point on the page to look for. Then go looking for each of your check boxes relative to that anchor point. You would have to set bounds around each checkbox to say that "if the pixels within these bounds have > 10% pixels that are not white, then it is checked". Something like that? It's a bit slow, tedious and manual, but achievable! Edit, link to the rasterize method: https://ironpdf.com/examples/rasterize-a-pdf-to-images/
Comment: If you use the IronOCR method `Bitmap[] pageImages = pdf.ToBitmap();` it will create an array of Bitmaps. Once you have the bitmaps you can use standard System.Drawing methods on the bitmap. One of the System.Drawing methods is bitmap.GetPixel() which will return a Color object. https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap.getpixel?view=netcore-2.2
Comment: I've got GIMP installed on my mac which shows the cursor location in x,y coords, although I'm sure any decent image editor should do it as well. I'll post a more complete answer with all these pieces, although I still think we're a bit away from a legit answer! :)
Comment: unless you ask to mark an 'X' as the answer, I don't think it would work for you. You'll need to use a kind of pixel checking to figure out if it's marked as answer or not.
Comment: Thanks Thiago. What's the easiest way to use pixel checking with PDFs?
Comment: @Thiago do you think maybe I could do something like: use pixel checking on each checkbox and if there is anything other than 'o' returned then that means the box is marked?
Comment: Nevermind, just noticed some boxes are square so the reader will not return 'o' in them cases. Would it be possible to check for whitespace if I pass in the pixel cords of inside the boxes ?
Comment: Do you only have 1 form type with 2 pages? If so manually checking each checkbox shouldn't be too difficult - tedious I would say!
Comment: https://www.nuget.org/packages/PDF.Core/ this seems like it could be useful for your problem.
Comment: Azure impresses again. They have a Form Recogniser product in preview too: https://azure.microsoft.com/en-in/services/cognitive-services/form-recognizer/
Comment: @DavidC Thank you for the replies. I have looked at Azure Form Recognizer and unfortunately it does not currently support 'Checkboxes or radio buttons'. I will have to look into IronPDF and see if it offers me something with checkboxes
Comment: @DavidC to answer your first question, I have 4 form types, each form has 2 pages. Could you recommend a way to go about extracting data from checkboxes with the IronOCR library you linked?
Comment: @DavidC Hi again David, thanks a lot for that! It looks like exactly what I am looking for. If you want to post an answer with the above in it I will accept it as the answer
Comment: Out of curiosity, do you know of an easy way I can find x,y bounds for my PDFs?
Comment: I am a lot closer now thanks to you! I am trying to look for some c# sample code to check if color of pixel is above x% in an image. Does this work best with bitmaps or can I use jpg also?
Answer: The answer for this isn't overly straightforward and involves creating custom code to parse the PDF yourself via a 3rd party library.
Since your forms are of a known shape, you know the locations of the checkboxes. You should construct a dictionary of "Checkbox name" and "Checkbox data" for each checkbox on the page. The data object could be an object that looks like:
<code>public class CheckboxData {
    public int startX { get; set; }
    public int startY { get; set; }
    public int endX { get; set; }
    public int endY { get; set; }
    public bool IsChecked { get; set; }
}
</code>
I would recommend using IronOCR to rasterize the PDF to an Image.
With your image, iterate over the checkbox dictionary and using the bounding points, move pixel by pixel and get the colour of the pixel. Store the colours in a list and then get the average colour of all pixels within the checkbox. If the average is above a threshold value for determining whether it's checked, set the IsChecked boolean.
For radio styled checkboxes, you will probably need a different data object and store the centre pixel of the circle. For the circles, you should store the centreX and centreY, along with the radius of the circle and use Bresenham Circle algorithm to know what pixels around that to check.
Below is an example of getting the pixel coordinates in GIMP for where the cursor is.
Comment: Thanks again David, really appreciate the help
Comment: Hi again mate, I have got all the hand-written text extraction working so now I am going to start looking at the checkboxes. Should I also store the average pixel color threshold in CheckboxData? By dictionary, would something like this work: Dictionary checkboxes = new Dictionary? string being a name for a specific checkbox? I am just trying to figure out the easiest way to iterate over all off my survey images. I assume bitmap.GetPixel() allows me to pass in the xy cords from CheckboxData? Or how would you suggest doing it?


Remove or rename menu's  class .last

Question: I need to remove class .last from menu's
. I browsed this nice website for a solution and find some code that removes ALL classes:
function THEME_menu_link(array $variables) {
  //unset all the classes
  unset($variables['element']['#attributes']['class']);

  $element = $variables['element'];

  if($variables['element']['#attributes'])

  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '
' . $output . $sub_menu . "
\n";
}

And:
function mymod_menu_tree__menu_about_us($variables) {
  return '
' . $variables['tree'] . '
';
}

function mymod_menu_link__menu_about_us(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';
  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  $class = "";
  $classes_of_this_li = $element['#attributes']['class'];
  if (in_array($classes_of_this_li, 'active-trail')) {
    $class = 'active';
  }
  $output = l($element['#title'], $element['#href']);
  return '
' . $output . $sub_menu . "
\n";
}

I need to remove only .last class and also somehow I need it to work only on a selected region. What I mean is that the function must apply to menus that are rendered i.e. in Sidebar region.
Comment: have you tried using str_replace function to replace the word last in the class variable. some thing like. str_replace(" last", " ", $class);
Comment: Can you give me a full code please? I am a beginner on this.
Answer: You can do it like this:
/**
 * Theme menu links.
 */
function THEME_NAME_menu_link(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';

  // This is the part that removes or changes the last class.
  if (!empty($element['#attributes']['class'])) {
    foreach ($element['#attributes']['class'] as $key => $class) {
      if ($class == 'last') {
        // Use only one of the two following lines.

        // To change the last class.
        $element['#attributes']['class'][$key] = 'new-last-class';
        // To remove the last class.
        unset($element['#attributes']['class'][$key]);
      }
    }
  }

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '
' . $output . $sub_menu . "
\n";
}

However, if you are referring to the primary links and secondary links as printed out by default in most themes, they do not run through that function, because they are generated by menu_main_menu() and menu_secondary_menu(), which in turn use theme_links() to generate the links instead of theme_menu_link() - very annoying.
So to get to those you either need to stop using theme default theme menus and use the menu blocks provided by default by the menu module. These blocks use the theme_menu_link() function to generate links so will be affected by the above code.
Alternatively, you would have to override theme_links() also, like so:
/**
 * Theme links.
 */
function THEME_NAME_links($variables) {
  $links = $variables['links'];
  $attributes = $variables['attributes'];
  $heading = $variables['heading'];
  global $language_url;
  $output = '';

  if (count($links) > 0) {
    $output = '';

    // Treat the heading first if it is present to prepend it to the
    // list of links.
    if (!empty($heading)) {
      if (is_string($heading)) {
        // Prepare the array that will be used when the passed heading
        // is a string.
        $heading = array(
          'text' => $heading,
          // Set the default level of the heading.
          'level' => 'h2',
        );
      }
      $output .= '<' . $heading['level'];
      if (!empty($heading['class'])) {
        $output .= drupal_attributes(array('class' => $heading['class']));
      }
      $output .= '>' . check_plain($heading['text']) . '</' . $heading['level'] . '>';
    }

    $output .= '
';

    $num_links = count($links);
    $i = 1;

    foreach ($links as $key => $link) {
      $class = array($key);

      // Add first, last and active classes to the list of links to help out themers.
      if ($i == 1) {
        $class[] = 'first';
      }
      if ($i == $num_links) {
        // *** Use this line to change the last class. ***
        // *** Or comment it out to have no last class. ***
        $class[] = 'new-last-class';
      }
      if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
           && (empty($link['language']) || $link['language']->language == $language_url->language)) {
        $class[] = 'active';
      }
      $output .= '
 $class)) . '>';

      if (isset($link['href'])) {
        // Pass in $link as $options, they share the same keys.
        $output .= l($link['title'], $link['href'], $link);
      }
      elseif (!empty($link['title'])) {
        // Some links are actually not links, but we wrap these in  for adding title and class attributes.
        if (empty($link['html'])) {
          $link['title'] = check_plain($link['title']);
        }
        $span_attributes = '';
        if (isset($link['attributes'])) {
          $span_attributes = drupal_attributes($link['attributes']);
        }
        $output .= '' . $link['title'] . '';
      }

      $i++;
      $output .= "
\n";
    }

    $output .= '
';
  }

  return $output;
}

I have added * to the start and end of comments for the line you need to be interested in.
The problem with this approach (maybe it isn't a problem for your site) is that this affects a lot of other lists of links on the site, not just the two menus in question.
It is possible to use
    if ($attributes['id'] == 'main-menu-links')
and
    if ($attributes['id'] == 'secondary-menu-links')
to distinguish between the main menus however that isn't foolproof and isn't the best idea.
For example, the important part of the above theme_links() would be:
if ($i == $num_links) {
  // This would skip the last class for main & secondary menus
  // but would keep it for other links lists.
  if ($attributes['id'] != 'main-menu-links' && $attributes['id'] != 'secondary-menu-links') {
    $class[] = 'last';
  }
}

Remove or rename menu's  class .last


IBM AppDOS.ConnectionClose vulnerability

Question: IBM Appscan source scanner detected AppDOS.ConnectionClose vulnerability in below source.
I fixed the issue by closing the connection in finally block but still it is reporting the same. Is there any pattern to close db connection?
Connection nCnn = null;

try
{
    nCnn = getConnection();

    /* some database operation */

    nCnn.close() /* Reporting AppDOS.ConnectionClose vulnerability */

} catch (Exception e) {
    throw new SQLException("Connection close", e);
}
finally
{
    try {
        if (nCnn != null)

            nCnn.close(); /* Reporting AppDOS.ConnectionClose vulnerability */

    }
    catch (Exception e) {
        logger.error(e);
    }

}

Answer: Your fix should have mitigated the issue. The fact that Appscan Source is still reporting the issue after the fix is likely a defect. A mitigation example by Appscan Source is as follows:
try
{
    Connection connection = DriverManager.getConnection(some_connection_string);
} catch (Exception e) {
    log(e);
} finally {
    connection.close();
}


Unwanted black borders when trying to expand image to full size

Question: When taking a screenshot of my scene in JavaFx, I save the BufferedImage to a file as a PNG/JPG. When I try to maximize the image size to its full length, I get Black borders on the picture from the left of the image to the bottom without the image increasing its size at all.
The size of the image only increases until I set my dimensions to 1300x700 as shown below.
 BufferedImage image = new BufferedImage(1300, 700, BufferedImage.TYPE_INT_RGB);

However once I increase the dimensions greater than 1300x700, the black borders appear.
The following picture is set to
  BufferedImage image = new BufferedImage(1500, 900, BufferedImage.TYPE_INT_RGB);

As you can see, part of the image is still cut off and there is now a big black border next to the image rather than the actual full sized image.
The following picture is set to
  BufferedImage image = new BufferedImage(1300, 700, BufferedImage.TYPE_INT_RGB);

As you can see, the image is still cut off at the same spot as before but there is no black border along side with it.
How can I fit the whole snapshot of my current scene into one file without these borders and without any of the content getting cut off?
Here is my code:
    File fa = new File("test.jpg");
    snapshot = quotes.getScene().snapshot(null);

    RenderedImage renderedImage = SwingFXUtils.fromFXImage(snapshot, null);
    BufferedImage image = new BufferedImage(1300, 700, BufferedImage.TYPE_INT_RGB);
    image.setData(renderedImage.getData());
    ImageIO.write(image, "jpg", fa);

Comment: Try to set WritableImage as parameter for snapshot method, something like this: `quotes.getScene().snapshot(snapshot);`
Answer: The black border comes from uninitialized pixel buffer, inside your BufferedImage object.
So, I guess the renderedImage itself does not contains the right part of your scene.
The scene may not yet be properly resized when you are taking the snapshot. Try to give an appropriate WritableImage to the snapshot method:
snapshot = quotes.getScene().snapshot(new WritableImage(1500, 900));


Python Error when implementing Hello Analytics API

Question: Basically, I am following the following tutorial:
https://developers.google.com/analytics/solutions/articles/hello-analytics-api#introduction
To be honest, I have no idea what I was doing, even after following the tutorial step by step.
When I implemented all of these codes, everything was going fine until I added the following line to the script hello_analytics_api_v3.py.
if __name__ == '__main__':
  main(sys.argv)

I got the following error, which I have no idea what it means.
Traceback (most recent call last):
  File "/Users/Python Projects/hello_analytics_api_v3.py", line 83, in <module>
    main(sys.argv)
  File "/Users/Python Projects/hello_analytics_api_v3.py", line 15, in main
    service = hello_analytics_api_v3_auth.initialize_service()
  File "/Users/Python Projects/hello_analytics_api_v3_auth.py", line 39, in initialize_service
    credentials = prepare_credentials()
  File "/Users/Python Projects/hello_analytics_api_v3_auth.py", line 32, in prepare_credentials
    credentials = run(FLOW, storage)
  File "build/bdist.macosx-10.9-intel/egg/oauth2client/tools.py", line 241, in run
NotImplementedError: The gflags library must be installed to use tools.run(). Please install gflags or preferrably switch to using tools.run_flow().

Can someone tell me what went wrong ?
Answer: Install the gflags library:
pip install --upgrade python-gflags

or
easy_install --upgrade python-gflags


Why is the output of this program -4? (Java - Datatypes Byte)

Question: I have a doubt in the following code:
public class test {
public static void main(String args[]) {
    byte x = 3;
        x = (byte)~x;
        System.out.println(x);

    }

}

The output is: -4
Comment: Do you understand what binary negation is? What did you expect the output to be?
Comment: [How does the bitwise complement (~) operator work? Why is it that ~2 is -3?](http://stackoverflow.com/q/791328/995714)
Comment: @Vinay, the negation of 011 in a byte is 11111100, not 100.
Comment: 3 is 011 in binary world. Its negation is 100 which amount to 4 in base 10 and in Java it will be not(~) of a 32 bit long 011.
Comment: Thats why I have added that it will be a 32 bit negation
Comment: Thanks, @LưuVĩnhPhúc, for the link. I understand the explanation given there much better than anything written in this thread (sorry, folks).
Answer: Binary negation of numver is the same as taking negative of number+1. Here's why:
Binary 3 is 0011 (leading bits skipped to make it more readable).
Binary negation ~3 is 1100.
Now, binary 4 is 0100
To determine -4, firstly write binary negation 1011.
and then add one (see wiki) 1100


Why is polynomial regression used to demonstrate overfitting and underfitting?

Question: WhenI try to research overfitting and underfitting, the most common algorithm and explanation I see revolves around polynomial regression. Why is this so? Is it just because it can be easily visualised like the graphs here:
https://scikit-learn.org/stable/auto_examples/model_selection/plot_underfitting_overfitting.html
Why is polynomial regression deemed suitable to give an insight into these overfitting/underfitting concepts? Are there any other similar algorithms that could be used?
Comment: Try halfway down https://www.geeksforgeeks.org/underfitting-and-overfitting-in-machine-learning/ for an illustration of underfitting and overfitting with classification, as well as polynomial regression
Comment: See the spinoff question over at ds.se: https://datascience.stackexchange.com/q/94146/55122
Answer: Easy visualization is a huge point in favor of using polynomial regression for illustration. (Note that both "illustration" and "demonstration", etymologically, have to do with showing pictures!) It also helps that the degree of the polynomial controls the amount of overfitting, and that polynomial regression allows looking at bona fide nonlinearity in the relationship (although splines are a better way of dealing with this).
An alternative that is also commonly employed is to simply add spurious predictors. Your true data generating process might be $y=b_1x_1+b_2x_2+\epsilon$, so a model $y=\beta_1x_1$ is underfitted, $y=\beta_1x_1+\beta_2x_2$ is fitted correctly, and $y=\beta_1x_1+\beta_2x_2+\beta_3x_3$ is overfitted - but it's hard to visualize the increased variability of fits from the overfitted model.
Comment: One somewhat boring point is: most people already know what polynomials are, since they are standard educational material in (I suspect) all high school level education throughout the world.
Comment: @MatthewDrury: good point! "Boring" is good, because if we understand the tools intuitively, then we can focus on the parts that are less intuitive, like over-/underfitting.
Answer: A more colloquial answer to this would be that the polynomial regression, the higher the order, has more flexibility to learn but also the tendency to memorize every data point in the training set, but with unneccessary and unpredictable wriggly waves, that makes forecasting nearly impossible as algorithms can get stuck in one area and extrapolation is quite problematic. Thus polynomial regression memorizes data wich is equal to overfitting, and does not learn to deal with new data, it only catches a certain situation. And as Stephan highlighted, the visualization helps a lot here.
Update just to make it clear:
Overfit just means a model that learns its data very well, which can be very well shown with a polynomial. Actually we wouldn't need a train/test split to see it, because our mind should say that we cant explain something so perfectly. Look how the polynom tries to catch every data point in both pics. Just to make it more clear due to the comments below my post, I added another picture:
ML Overfitting:

Picture by Jannis Seemann Udemy

The overfitting polynom is the red line.
Grey crosses are train data
Blue crosses are test data
Green line is a simpler model, (e.g. a power function) that doesnt catch the data exactly, but learns the general trend or a picture of a model situation.

You can see that the red polynom learnt the data very well, but behaves bad on the test data. The simpler model, not catches the whole train data but behaves well overall. If you are aware of this behavior you wont need a visual train/test split to be sure if your model overfits, if you choose to a high polynom.
Polynom behavior in general
This picture shows a general comparison of a overfitted model with a high order polynom and a polynom of a much lower order. You can already see, that the model with the higher order overfits, although you havent seen the test data.
To feel the power of a simple overfit, you can do pictures like that yourself:
Use a simple scatterplot. Got to Microsoft excel, click on fitting the scatter plot with a linear trend. Than change to polynomial und increase polynomials to the highest order. The polynomial gives you the freedom to fit the data near perfectly, but that squiggly line does not normally fit on the test data, its totally useless (blue one). The red line although a much lower fit, may be extrapolated better and shows "maybe" the truth, that there is not much to expect, Your model will be bad, which is part of antoher discussion, but not overfitted.

Image by researchgate https://www.researchgate.net/profile/Scott-James-5/publication/255205930/figure/fig1/AS:670698729123859@1536918446824/The-green-squares-represent-the-original-data-set-are-fit-using-two-conceptual-models-a.png
Comment: Your picture example is not clear. What is the difference between square/round, red/blue?
Comment: You are right, it is not exactly fitting. The blue line focuses on the green dots, and shows the overfitting process of learning data. You can see how the high grade polynom moves through every dot, showing how data gets learned, but with unneccessary squiggly lines at the cost. The blue dots may be from a different scatter plot than the green, but thats not the point: you can see how the first three blue dots are "in line" with the green dots up to the 4th point. And it shows that fitting a line with a much lesser polynomial can result in a way badder R² although it makes more sense
Comment: I guess I mean I know what you are trying to show, but it isn't very clear. Like which is the training, which is the test? Maybe the colors could match?
Comment: I updated my answer. But you do not need a train/test split to say or see if a model is overfitted. Although i highlighted it again with a picture. You can just see that the model makes squiggly lines to exactly hit every data point. That is learning your data instead of identifying a general trend. But the new pic should capture it better.
Comment: I was using terms loosely. I know what you're trying to show, but for a reader who doesn't, I just think the explanation isn't clear (i.e. presumably the test is green squares, but that's not stated)
Comment: thats why I added a new picture
Answer: This is a less statsy answer, and more of a practical/human-oriented answer. While all the other answers are correct in their own manner. I think it is more to do with your audience. Let me explain:
If you're learning about overfitting then you're going to be pretty new to ML/Stats/Data Science. Therefore, not everyone taking said class, reading said blog post or watching said video is going to understand more complex models like GBMs, SVMs, GMMs and so on...
However, if you are learning ML you should sure as hell will understand the concept of linear regression (or line of best fit) and one would hope you'd understand polynomials (otherwise maybe you should be taking high school maths classes not trying to learn ML just yet).
Finally, you can create and recover the generative model precisely. This, in a learning phase, helps people fully understand what is going on and allow them to easy replicate results and play with the models themselves.

NB: Maybe the real question is "Why aren't more ML concepts introduced as polynomial regression examples, since they are so intuitive, expressive and accessible by everyone with a high school mathematics education?".
The honest answer to this is probably: "Because GBMs, SVMs, GMMs etc are freaking cool!".


Let be $\lambda$ and $\mu$ complex numbers. Find the general solution $(x,y,z)\in \mathbb{C}^{3}$ to the system of equations:

Question:
Let be $\lambda$ and $\mu$ complex numbers. Find the general solution
$(x,y,z)\in \mathbb{C}^{3}$ to the system of equations:
\begin{align*} \lambda x+y+z&=1\\ x+\lambda y+z&=\mu\\ x+y+\lambda z
 &=\mu^{2} \end{align*}

I found that the determinant of the coefficient matrix is distinct to zero, this is:
\begin{align}
\begin{vmatrix}
\lambda & 1 & 1\\
 1& \lambda & 1\\
1 & 1 & \lambda
\end{vmatrix}=\lambda^3+2-3\lambda=\lambda(\lambda^2-3)+2\neq0 \ \iff\lambda\neq0
\end{align}
So, there should be an unique solution if $\lambda\neq0$. By otherside
\begin{align*}
x=\frac{1-y-z}{\lambda}
\end{align*}
I solved it using $z$ as a paramter, and I got that the general solution is:
\begin{pmatrix}
\mu^2-\lambda z-\left [ \mu-z-\frac{1-z}{\lambda} \right ]\frac{\lambda}{\lambda^2-1}\\
\left [ \mu-z-\frac{1-z}{\lambda} \right ]\frac{\lambda}{\lambda^2-1}\\
z
\end{pmatrix}
My question is, how can I analyze the system of equations to know how to solve it? I mean, with this problem I decide to use the equations and I "luckely" solved it, but I would like to know how to proceed in every case. Hope you can help me to understand how to decide how to solve a system of equations, or at least in this case how could I knew how to solve it?. I would really appreciate your help!
Answer: There are some problems here. You have computed, correctly,
$$\begin{vmatrix}
\lambda & 1 & 1\\
 1& \lambda & 1\\
1 & 1 & \lambda
\end{vmatrix}=\lambda^3-3\lambda + 2.$$
However, this is not $0$ if and only if $\lambda = 0$. Indeed, substituting $\lambda = 0$ into this produces a determinant of $2$.
Further, if the determinant is non-zero, as you say, you'll get a unique solution. This means you can't use $z$ (or any of the variables) as a free parameter. The solution is unique, hence there must be one and only one value of $z$ that produces a solution.
Let's look at the polynomial $\lambda^3-3\lambda + 2$. We can solve this via the usual methods: guess a root, use the factor theorem, then solve the remaining quadratic. If we look at the matrix itself, it jumps out at me that $\lambda = 1$ must be a root, as the corresponding matrix would have all the same rows/columns, making the rows/columns linearly dependent. So, I expect $\lambda = 1$ to be a root (it is, check it!), and $\lambda - 1$ to be a factor. Using polynomial division,
$$\lambda^3 - 3\lambda + 2 = (\lambda - 1)(\lambda^2 + \lambda - 2).$$
Solving/factorising the quadratic by whatever means you prefer, you should then get
$$\lambda^3 - 3\lambda + 2 = (\lambda - 1)^2(\lambda - 2).$$

While this information will be important, it will not, by itself, help us solve the system in place. Cramer's Rule could be used when $\lambda \neq -2, 1$, but just because the system doesn't have a unique solution doesn't mean it can't be solved.
Instead, just set up an augmented matrix, as usual:
$$\left[\begin{array}{ccc|c}
\lambda & 1 & 1 & 1 \\
1 & \lambda & 1 & \mu \\
1 & 1 & \lambda & \mu^2
\end{array}\right].$$
We can now perform all the usual elementary row operations. Remember, $\lambda$ is just a scalar. Though, we must be careful when dividing a row by a scalar expression in terms of $\lambda$, in case that expression happens to be $0$. First, let's swap row $1$ and $3$, to get $1$ in the top left:
$$\left[\begin{array}{ccc|c}
1 & 1 & \lambda & \mu^2 \\
1 & \lambda & 1 & \mu \\
\lambda & 1 & 1 & 1
\end{array}\right].$$
Subtract row $1$ from $2$, and $\lambda$ lots of row $1$ from row $3$:
$$\left[\begin{array}{ccc|c}
1 & 1 & \lambda & \mu^2 \\
0 & \lambda - 1 & 1 - \lambda & \mu - \mu^2 \\
0 & 1 - \lambda & 1 - \lambda^2 & 1 - \lambda\mu^2
\end{array}\right]. \tag{1}$$
The next step I'd want to do is divide a row by $\lambda - 1$ or $1 - \lambda$. But, what if this scalar is $0$, i.e. if $\lambda = 1$? Then the matrix turns into:
$$\left[\begin{array}{ccc|c}
1 & 1 & 1 & \mu^2 \\
0 & 0 & 0 & \mu - \mu^2 \\
0 & 0 & 0 & 1 - \mu^2
\end{array}\right],$$
which can only have a solution if $1 - \mu^2 = 0$ and $\mu - \mu^2 = 0$. The first equation is satisfied if and only if $\mu = \pm 1$, while the second is satisfied if and only if $\mu = 0$ or $1$. Thus, $\mu = 1$ is the only possible value that produces solution. In this case, we get one non-zero row:
$$x + y + z = 1,$$
which we solve by making $y$ and $z$ both free parameters.
Moving back to $(1)$, we can now divide rows $2$ and $3$ by $1 - \lambda$, giving us,
$$\left[\begin{array}{ccc|c}
1 & 1 & \lambda & \mu^2 \\
0 & -1 & 1 & \frac{\mu - \mu^2}{1 - \lambda} \\
0 & 1 & \lambda + 1 & \frac{1 - \lambda\mu^2}{1 - \lambda}
\end{array}\right].$$
Add row $2$ to row $3$, then multiply row $2$ by $-1$ to get
$$\left[\begin{array}{ccc|c}
1 & 1 & \lambda & \mu^2 \\
0 & 1 & -1 & \frac{\mu^2 - \mu}{1 - \lambda} \\
0 & 0 & \lambda + 2 & \frac{1 - \lambda\mu^2 + \mu - \mu^2}{1 - \lambda}
\end{array}\right].$$
Again, we find ourselves wanting to divide by the scalar $\lambda + 2$, but we don't know if it's $0$. Try substituting $\lambda = -2$ into the matrix, and find the two values of $\mu$ that produce solutions. This time, when $\mu$ is one of those two values, there will only be a one-parameter solution.
Otherwise, $\lambda + 2 \neq 0$, and we get
$$\left[\begin{array}{ccc|c}
1 & 1 & \lambda & \mu^2 \\
0 & 1 & -1 & \frac{\mu^2 - \mu}{1 - \lambda} \\
0 & 0 & 1 & \frac{1 - \lambda\mu^2 + \mu - \mu^2}{(1 - \lambda)(\lambda + 2)}
\end{array}\right].$$
This gives us immediately our unique value of $z$ (assuming $\lambda \neq 1, -2$), and the other values can be found through back-substitution.
It's a long question for sure, but it can be done with all the elementary techniques.


Plotting a polynomial regression with its confidence interval of 95% in R

Question: I have been trying for a while plotting a polynomial regression using R. I have read several libraries, as ggplot2, qplot, etc, with no succeed.
The next are my data:
I normally use the R GUI called R commander. Is there any option to get this plot without writing commands directly in the prompt line by using this GUI?
X   Y
97.6470588235   5.98290598291
92.9411764706   14.5299145299
90,00   22.2222222222
85.2941176471   29.9145299145
80,00   37.6068376068
74.1176470588   46.1538461538
68.8235294118   52.9914529915
64.1176470588   58.9743589744
58.2352941176   64.9572649573
52.3529411765   70.9401709402
47.0588235294   76.0683760684
41.7647058824   81.1965811966
36.4705882353   84.6153846154
31.1764705882   88.8888888889
25.8823529412   92.3076923077
19.4117647059   95.7264957265
77.6470588235   41.0256410256
83.5294117647   33.3333333333
96.4705882353   9.40170940171
88.8235294118   24.7863247863
100,00  0,00
96.186440678    7.27272727273
91.9491525424   15.7575757576
88.9830508475   21.8181818182
85.1694915254   28.4848484848
80.9322033898   36.3636363636
78.813559322    39.3939393939
74.5762711864   46.0606060606
70.3389830508   52.1212121212
64.406779661    61.2121212121
56.7796610169   70.303030303
49.5762711864   78.1818181818
42.7966101695   84.8484848485
36.8644067797   89.696969697
31.7796610169   93.3333333333
26.2711864407   97.5757575758
21.186440678    100,00
61.4406779661   64.8484848485
68.2203389831   55.7575757576
53.813559322    73.3333333333

I obtained its quadratic regression and the coefficients for the confidence intervals:
Call:
lm(formula = Y ~ X + I(X^2), data = Datos2)

Residuals:
    Min      1Q  Median      3Q     Max
-3.2599 -1.3415  0.1541  1.3610  3.2911

Coefficients:
              Estimate Std. Error t value Pr(>|t|)
(Intercept)  1.013e+02  1.971e+00  51.419   <2e-16 ***
X           -8.086e-03  7.069e-02  -0.114     0.91
I(X^2)      -9.886e-03  5.727e-04 -17.263   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.824 on 37 degrees of freedom
Multiple R-squared:  0.9964,    Adjusted R-squared:  0.9962
F-statistic:  5132 on 2 and 37 DF,  p-value: < 2.2e-16

> Confint(LinearModel.3, level=0.95)
                 Estimate       2.5 %        97.5 %
(Intercept) 101.323700913 97.33098252 105.316419306
X            -0.008086099 -0.15131294   0.135140742
I(X^2)       -0.009885921 -0.01104624  -0.008725598

However, trying to plot the quadratic regression and its Confidence intervals resulted in a more than expected hard work.
Which could be the correct command (or route using R commander) to get a professional plot view? I tried several ggplot and qplot with no succeed.
On the other hand, substituting particular X values in those three equations I get something which may be wrong. I mean. On the left side, the confidence interval must be wider than on the right side. However, what I see is the contrary, narrower the left and wider the right. A quick plot using LibreOffice Calc showing this:

The distance between the red and blue lines (95%CI) must be narrower on the right side than on the left side.
Thanks in advance for your comments.
Mario
Comment: Could you elaborate on why you think the distance must be narrower on the right side (larger independent values) than on the left?  And what "three equations" did you substitute the x values into?
Comment: Plot the original data and you will see that it is unlikely that the right side would be wider than the left side. Ohterwise, when I plot the raw data using other statistical software (PAST), the confidence interval which appears is exactly in the same way as I am describing.
Comment: The original data tell you nothing about the relative widths of confidence bands at different locations, because they are not computed locally: they summarize *all* the data.  The second part of your comment is important: it suggests you are not correctly computing the confidence bands at all.  Unfortunately, you haven't really indicated how you are computing them, making it impossible to suggest what exactly is wrong.
Comment: Thanks whuber for your comment. I want a plot where the quadratic regression is represented and also its confidence interval (at 95%). I did it with other software but I want to do it with R, and learning howto do it.
Answer: This StackOverflow question on plotting confidence intervals may help you with the R code.
I think a base point of confusion is that the confidence interval limits for a quadratic fit are not themselves quadratics. The interval limits need separate evaluation. Here's a plot made with different software. The data fits so well, I had to crank the alpha level down to 0.001 to get any sense of the confidence interval.

Using a small subset of the data shows the phenomenon better:

Separately of interest: as you can sort of tell from the original data plot and you can strongly tell from the residuals plot, there seems to be something else going on, such as two interleaved functions selected by some hidden variable.

Guessing at the two populations gives two even stronger looking fits, at least for the left side:


Display inline-block affects line height hover

Question: I have created some "buttons" out of three divs and I have set a hover to each div so that the text drops by 5px when the user hovers over the div.
I'm using display:inline-block as I don't want to use float:left
The only problem is that when I use display:inline-block on the divs, it lowers the other divs instead.
Here is how it should work (using float:left)
WORKING
And this is what happens when I used display:inline-block
NOT WORKING
Is there a fix for this or am I going to have to use a different solution?
Comment: set explicit `vertical-align:middle` (or top, bottom, ...) for all the inline-block elements http://jsfiddle.net/viphalongpro/y47P7/1/
Comment: `inline-block` elements are `vertical-align:baseline;` by default.
Comment: @KingKing this solution works but when I try and add the buttons into the rest of my code it goes back to not working :/
Comment: @SaturnsEye not sure what you mean, so what's the new updated fiddle showing it does not work?
Answer: Sorted the problem!
Although KingKing's answer was correct, using .wkd > * {vertical-align:middle;} didn't seem to work well with the rest of my code so I had to alter it slightly so it worked like so:
.won, .keep, .discard {
    vertical-align:middle;
}

Still, KingKing's answer helped me figure this out so thank you :)
Comment: I guess u need to read @kingking's comment "set explicit vertical-align:middle (or top, bottom, ...) for all the inline-block elements",
he said it for all inline-block elements, although he doesn't include that in fiddle.
Comment: looks like your actual code is different, in your demo code, all the `.won, .keep, .discard` are ***direct*** children of the `.wkd`, but in your actual code, they may not. So it won't work.
Comment: @KingKing they were direct children in my proper code too but for some reason it just didn't take effect


Proof: A convergent Sequence is bounded

Question: A part of the proof says that if $n\le N$, then, the sequence
$x_n \le \max\{|x_1|,|x_2|,....,|x_{N-1}|\}$.
I'm not capturing the intuition of the above. Even more perplexing is, if it the case where the sequence is monotonic decreasing, then for every $n\le N$, it is obvious that $x_n$ will not be $\le \max\{|x_1|,|x_2|,....,|x_{N-1}|\}$.
I greatly appreciate some input.
Comment: I think it should be $\forall n\leq N$, we have $x_n\leq\max\{|x_1|,\dots,|x_N|\}$
Comment: Could you kindly explain the intuition behind
xn≤max{|x1|,…,|xN|}?
Answer: Well, $|x_n| \le \max\{|x_1|,\ldots, |x_{N-1}|\}$ for all $n \color{red}{<} N$ because if $n < N$, $|x_n|$ is one of the numbers $|x_1|,\ldots, |x_{N-1}|$, and some number in this list is the largest, i.e., $\max\{|x_1|,\ldots, |x_{N-1}|\}$.
Comment: I'm still lost.
Perhaps, I'm seeing it the wrong way.
Here's how I'm seeing it: there exists some n <= N on the x-axis.
Suppose then that n = 1, x_(n=1) = 8 and where N= 8,
x_(n=N=8) = 5.
Is this not the case then that it can be true that for n<= N, x_n >= x_N?
Comment: @guest given real numbers $a_1,a_2,\ldots, a_m$, the number $\max\{a_1,\ldots, a_m\}$ is defined as the largest of the numbers $a_1,\ldots, a_m$. By definition, $a_i \le \max\{a_1,\ldots, a_m\}$ for $i = 1,2,\ldots, m$. Makes sense?
Comment: Yes it does.
I've added a comment to this topic a while ago clarifying my issues.
It is the way the that the notes were worded in natural language (which is oft exposure to confusion and ambiguity).
In sum, all I had done was set M as a set of plausible bounds from n=1 to some finite large n and take the max of the bound.
Answer: I think it is more useful to just think of this in an arbitrary metric space than to worry about properties of the real numbers such as order (i.e. monotone sequences), because it's not actually relevant to the fact that a convergent sequence is bounded.
If $\lim_{n\to\infty} x_n=x$, then for all $\varepsilon>0$, there exists $N$ such that $n\geqslant N$ implies $d(x,x_n)<\varepsilon$.
So take $\varepsilon=1$. Choose $N$ so that $n\geqslant N$ implies $d(x,x_n)<1$. Then for any $n$,
$$d(x,x_n) \leqslant \max\left\{1, \max_{1\leqslant m\leqslant N}\{d(x,x_m)\}\right\}. $$
Hence, $\{x_n\}$ is bounded.
For a more intuitive, but less formalized argument; if the sequence is convergent, then we can draw an open ball around the limit that contains all but finitely many points of the sequence. So, draw an open ball around those finitely many points, and take the union of those two balls. Draw an even bigger ball that contains that union. Then you have a ball that contains the entire sequence.
For yet another argument: suppose $\{x_n\}$ isn't bounded. Then no open ball centered at $x$ contains all points of the sequence. So for each $k$, we can find a point $x_{n_k}$ such that $d(x,x_{n_k})>k$. Take $\varepsilon=1$. Then for any $N$, we have a positive integer $n_k$ such that $d(x,x_{n_k})>k>1$. Hence, $x_n$ does not converge to $x$, a contradiction.
Answer: I see what's going on.
My lecturer could have done a better job by stating, instead of
"If n < N", "for all n in the set of Natural number"
Since 1+|L| is a bound for x_n for which n>=N, the bound 1+|L| might not necessarily be the bound for x_n since we have not taken into account yet x_n for which n < N.
Comment: That's right, but it would've been easier for us to help you if you showed in more detail what your lecturer's proof was and the issues you had with it. The part you asked about (i.e., $x_n \le \max\{|x_1|,\ldots, |x_{N-1}|\}$ for $n \le N$) has nothing to do with the bound $1 + |L|$.
Answer: If $\{a_n\}$ be a convergent then for any $\epsilon>0$: then there exist a positive integer $n_0$ such that
$$
|a_n-l|\le \epsilon\:\text{  for all }\:n> n_0
$$
and this implies that $|a_n|\le\epsilon +l$.
Now let $M=\{|a_1|,|a_2|,|a_3|,\ldots,|a_n| ,|\epsilon+l|\}$: then $|a_n|\le M$ is bounded for all $n\in\Bbb N$ and hence sequence $\{a_n\}$ is bounded.

Proof: A convergent Sequence is bounded


What weights to use

Question: I want to do a poisson multilevel analysis of item nonresponse over 27 countries. The model holds 3 levels: respondent, interviewer, and country.
I am using data from the European social survey, and these data have two weights:
Design weight: Several of the sample designs used by countries participating in the ESS were not able to give all individuals in the population aged 15+ precisely the same chance of selection.The design weight corrects for these slightly different probabilities of selection, thereby making the sample more representative of a 'true' sample of individuals aged 15+ in each country.
Population weights: This weight corrects for the fact that most countries taking part in the ESS have very similar sample sizes, no matter how large or small their population. The population size weight makes an adjustment to ensure that each country is represented in proportion to its population size.
Now I was going to use the product of these two weights as the weighting at the first level. (Second and third level I'll use a weight of 1) But I got the advice to only use the design weight, since country is a variable in my model. And when weighting for population the country differences are going to dominate the data, masking other significant effects.
However, the website of the ESS advices: "when combining countries into a group, such as 'accession countries' or 'EU member states', both design and population size weights should be applied.". And I am combining all the 27 countries into one group.
Does anyone have any good explanation as to why I should not use the population weights, when country is accounted for in the model. Is this a 'golden rule'?
Answer: I think the biggest issue here is sample-design, I would use the design weight, and depending on the values it takes you may have to normalize it, ie wt/mean(wt)
Here is a good reference on using design weights in multilevel models:
A. C. Carle, "Fitting multilevel models in complex survey data with design weights: recommendations," BMC Medical Research Methodology, vol. 9, no. 1, article 49, 2009.
Comment: Thanks, the reference is very helpful. The values range from 0 to 4 with a mean of 1 and SD of 0.5. So I think normalising is a good idea.
Comment: actually that's not so bad, sometimes weights are in the thousands, but those are typically population weights.

What weights to use