Same code compiled with different versions provides different result

Question: I am using an external library which I do not control nor know the internals (lets call it proprietarycallbacks).
I know that I have a class called callbacks that has two bool variables:
class callbacks : public proprietarycallbacks {
  bool a = false;
  bool b = false;
  virtual callbackHandler() {
    cout "callback received\n";
    b = true;
  }
}

then I have another class which inherits from the callbacks class:
class MyObject : public callbacks {

  void test() {
    while (!b) {
      cout << "test " << a << " " << b << endl;
      usleep(100000);
    }
  }
}

This code compiles correctly in two different linux versions, with two different GCC versions and LIBC versions.
On the most recent one (linux mint, GCC 5.4 LIBC 2.23), I run the app, see the cout in the while and when the callback is called, the code exists the while.
On the older one (debian, GCC 4.9.2, LIBC 2.19), the while never exists, the variable is always false, even though I can see the print from inside the callback.
Is there something wrong with the way I am structuring the code, and the variable inheritance, or does this have something to do with the software versions I am using?
Thank you for your time
Comment: Is the setting of `b` and the testing of `a` a typo?  Also is the callback and loop on the same thread?
Comment: If you change your variables to `std::atomic_bool`, does the difference disappear?
Comment: (assuming the aforementioned typo is, in fact, a typo) Supposing `test()` and `callbackHandler()` are called from different threads, you would be doing concurrent read and write to `a`, which, in short, is not guaranteed to work as expected. You could see what happens with `std::atomic_bool`, as suggested, or even just declaring `a` to be `volatile`; it is very possible that one compiler version decides that `a` is not changing in the loop and keeps reading it from e.g. a register instead of memory.
Comment: @jdehesa `volatile` is not for solving threading problems.
Comment: @RichardCritten Right, but I think it would make the enough difference in this case to get both compilers to produce programs behave similarly (and reveal why it was not the case before). Of course, it would probably not be enough for the real application.
Comment: @RichardCritten yes its a typo, I will correct it.
Comment: @jdehesa I never used atomic_bool before and I don't know how to declare and initialize atomic_bools. I tried a few options but since I was not sure this was the case, I tried different things. I tried volatile and the issue remains.
Answer: It is obvious from your test() method that there are multiple execution threads involved. test() is in one execution thread, and the callbacks get invoked by the other execution thread.
Setting the bool flags in the other execution thread, and reading the same flags in the test() execution thread are not sequenced with each other.
The usual solution is to either use std::atomic_bool; or use a std::mutex to implement sequencing, and accessing the variables, either to set them in one execution thread, or read their current values in the other execution thread, only while the mutex is locked.
Comment: can you ellaborate more? I agree that the lack of a mutex means that I can read a variable with an incorrect value, while it is being changed. Nonetheless, on the next while iteration, shouldn't the read had the updated value?
Comment: @BlunT: Why should it ?
Comment: when the read is made while a write is being made, I understand that the value being read is incorrect. But on subsequent reads, shouldn't the value be retrieved from the object, which has been updated in the meanwhile? Why then? Is the value cached for undefined time?
Comment: A C++ compiler is allowed to generate any code that's optimized in any way that does not change observable results. `int a=b+c; int d=b+e;` The C++ compiler is permitted to generate code that reads `b` only once, even though it's used twice. This has no observable changes in the results, since nothing changes the value of `b`. There's nothing in `test()` that changes the observable value of `b`, so the compiler is permitted to generate the code that reads `b` once, if `true` returns from the function, otherwise it enters an infinite loop.
Comment: @SamVarshavchik Ok, in that case, how can I tell the compiler to get its hands out of my code? :) How can I make it re-read the value of those variables everytime?
Comment: I told you already: use a mutex or an atomic variable. That tells the compiler that another execution thread might modify the `bool` at any time. Even if the compiler generates explicit code, unless you're using a single-core CPU, you're still at the whim's of your CPU's multi-core caches. Only explicit sequencing/atomic variables is going to issue special CPU instructions that makes everything happen correctly.


WebDriver: explicitly wait doesn`t work by click to element

Question: My code with implicitly wait works well. But I read  information about waits and understood, that I need using explicitly wait in my projects. That`s why I am trying implement my test project with it.
When step of my alhorithm equel click to button, I have error : http://joxi.ru/BA0GMyDhnY0n2y
Please, help me with it.
Base class:
using NUnit.Framework;
using System;
using LinkedinAddContacts.Pages;
using LinkedinAddContacts.TestData;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

namespace LinkedinAddContacts
{
    [TestFixture]
    public class TestClass
    {
        private IWebDriver webDriver;
        private WebDriverWait waitDriver;

        [SetUp]
        public void InitializeBrowser()
        {
            webDriver = new ChromeDriver();
            waitDriver = new WebDriverWait(webDriver, TimeSpan.FromSeconds(10));
            webDriver.Manage().Window.Maximize();
            webDriver.Manage().Timeouts().PageLoad =  TimeSpan.FromSeconds(30);
            webDriver.Navigate().GoToUrl("https://www.linkedin.com/");
        }
        [Test]
        public void TestMethod()
        {
            Authorization authorizationData = new Authorization();
            StartPage objStartPage = new StartPage(waitDriver);
            NetworkPage objNetworkPage = new NetworkPage(waitDriver);

            objStartPage.EntrySystem(authorizationData);
            objNetworkPage.ConnectPeople();
        }

        [TearDown]
        public void CloseBrowser()
        {
            webDriver.Quit();
        }
    }
}

Secondary class:
using NUnit.Framework;
using LinkedinAddContacts.TestData;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

namespace LinkedinAddContacts.Pages
{
    public class StartPage
    {
      //  private IWebDriver webDriver;
        private WebDriverWait waitDriver;
        #region Objects

        public StartPage(WebDriverWait waitDriver)
        {
            this.waitDriver = waitDriver;
        }

        private IWebElement EmailInput
        {
            get
            {
                return waitDriver.Until(ExpectedConditions.ElementToBeClickable(By.Name("session_key")));
                //return webDriver.FindElement(By.Name("session_key"));
            }
        }

        private IWebElement PasswordInput
        {
            get
            {
                return waitDriver.Until(ExpectedConditions.ElementToBeClickable(By.Name("session_password")));
               // return webDriver.FindElement(By.Name("session_password"));
            }
        }

        private IWebElement LoginButton
        {
            get
            {
                return waitDriver.Until(ExpectedConditions.ElementToBeClickable(By.Name("login-submit")));
                //return webDriver.FindElement(By.Id("login-submit"));
            }
        }

        private IWebElement RegistrationForm
        {
            get
            {
                return waitDriver.Until(ExpectedConditions.ElementToBeClickable(By.Id("regForm")));
            //    return webDriver.FindElement(By.Id("regForm"));
            }
        }
        #endregion
        #region Methods
        public void CloseRegistrationForm()
        {
            IJavaScriptExecutor js = waitDriver as IJavaScriptExecutor;
            js.ExecuteScript("document.getElementById('regForm').style.display = 'none';");
            //  ((IJavascriptExecutor)driver).executeScript("scroll(0,400)");

        }

        public void EntrySystem(Authorization authorizationData)
        {
           // CloseRegistrationForm();
            EmailInput.SendKeys(authorizationData.Email);
            PasswordInput.SendKeys(authorizationData.Password);
            LoginButton.Click();
        }
        #endregion
    }
}

Error there:
 public void EntrySystem(Authorization authorizationData)
    {
       // CloseRegistrationForm();
        EmailInput.SendKeys(authorizationData.Email);
        PasswordInput.SendKeys(authorizationData.Password);
        LoginButton.Click();
    }

Answer: It is important to notice which web driver you use.
First of all, As @Robert says, its better to find by Id whenever it is available to you.
Second, I think LoginButton.Click() does not work. I had such a problem with chrome driver. When page scale(zooming) is changed, the Click method does not work properly, or clicks elsewhere on the page.
I recommend you to use SendKeys for any click action.
Just like this:
LoginButton.SendKeys(Keys.Enter);// or Keys.Return

Never ever use Click method
Comment: Thank you for advice ,  I take it to my skills. I didn`t talk about it anywhere.
Comment: Your advice  helps me in another project. I suffered with unclickable button more one week. Thanks again
Answer: When I understand it properly your code crashes at this line:
return waitDriver.Until(ExpectedConditions.ElementToBeClickable(By.Name("login-submit")));

Now, taking a look at the startpage of linkedIn reveals that the login-submit button doesn't have a name attribute defined, but you can use it's id instead.
<input tabindex="1" id="login-submit" class="login submit-button" type="submit" value="Einloggen">

So you should be using By.id() instead of By.name().


Korg SP-300 .. 21 volts?

Question: We have laying around one of the Korg SP-300 (which were made in Italy - they weigh a ton!)

Have misplaced the original power supply. That thing was a HUGE black cube, getting on for the size of a small toaster.  (We did in fact purchase it in I think France if that matters, maybe they are bigger there!)

Notice it is marked "21" volts.  (And a whole amp, too.) Can this really be correct? I may just be completely wrong but device inputs are usually some small amount of volts?

Checking sweetwater etc, you really can't buy the original any more it seems

You can easily google "replacement for SP-300 power supply".  But ............

.... example, link they are all weenie little boxes like this -

I've found in the past if you buy a "weenie" replacement for any device (example, a cheap weenie replacement for a modern laptop that needs a lot of juice), they just hopelessly fail.
(Indeed notice as well for some reason the voltage is not even marked on those.)
My questions are two,

Does anyone know if it's a hard fact that it needs "21V" input; perhaps someone else has one and/or the original (perhaps European) power supply

Does anyone happen to have tried one of the "weenie" replacements, which are only like $20 - will it actually even work?
Comment: Have you spoken to Korg yet..?
Comment: Note the polarity on that thing. The label next the plug hole says the *outside* is positive, while in my experience the inside is quite commonly positive. Check before you try.
Comment: @Tim it's a good point, i will phone them.  But I surely checked their product range and they basically don't bother selling power supplies ("ac adaptors") now - they offer like one crappy one!   I was really surprised that some company doesn't offer, you know "stage and studio quality" power supplies for stuff  - I figured all you pros would say "oh yes, just go to blah" for the best in ac adaptors! But no!
Answer: Best guess: the old one was entirely old-school coil-wound. New one is a switch-mode supply. So long as it's correctly earthed to reduce hum*, it should be fine. That's actually quite a substantial supply for a switch-mode, so it presumably has sufficient rectifying/power smoothing.
*This may not be an absolute requirement, but checking pictures of similar supplies on other sites (eBay etc), it does look like the unit itself is earthed, not just 2-pin.
BTW, yes, it's a hard fact that it needs 21v and at least 1A. You can use a supply of more than 1A, as amps are 'pulled' by the equipment not 'pushed' by the power supply. Voltage, on the other hand has no such tolerance. You might get away with 20 or 22v, but really you should be looking for the correct nominal voltage.
Additionally - but now we're getting into esoterica best dealt with by competent electronics experts, not me;) - you cannot measure voltage on a power supply by any consumer-available means. Voltage must be measured under load, not just by sticking a multi-meter in the plug. The power supply, therefore, has been calculated by someone smarter than me to deliver 21v whilst under load from that specific piece of equipment (or at least calculated to be within tolerance).
The reason the picture doesn't have a model number or voltage marked is one of convenience for the supplier - they probably make 15 versions in the same casing. One set of info-free pictures is all they need to sell any variant.
Comment: @Tim - Thing is, how are you going to gauge what is 'in tolerance' with no way to measure it? Plug it in & see if smoke comes out? It probably won't but why risk it? PSU is earthed, equipment can't be, there are only two wires going from PSU to keyboard. Polarity is critical, it's DC not AC where you can get away with it. It should be diode-protected in case of inversion, but again, do you want to risk it?
Comment: It's unlikely to make any real difference compared to a half-decent modern switch-mode. PSU design has come a long way since we all got mobile phones. The only 'general guide' is that the heft of the unit still has some bearing on its power-smoothing ability, so don't go for some 5 buck weeny wall-wart from eBay. Think of the difference between a laptop supply & the one for your xmas LED lights ;)
Comment: I'd have thought that a 24VDC psu would do the job. That'e 15% over, which is within tolerance. 1Amp isn't a huge amount of current. How the unit is earthed - do you mean the psu or the Korg? And that polarity is critical.
Comment: This is amazing info, blokes!   Aside - in fact, are the old-school "WOUND" thingies you talk of actually *better* ??!  Do musicians / engineers prefer them?  Can you still buy them "even if more expensive" sort of thing?  Cheers !
Comment: tolerance - indeed notice some of the ones you see on EBay are like "22.5V" which makes me nervous :O
Comment: Right.  I think the only issue is really I can't find a "better" (heftier, more expensive) one than the ~$30 items that seem commonly available (example "UpBright" label - of course its all just OEM stuff with various labels slapped on it).  I'll buy one and see how it goes.  It's a shame for such a quality device, to have a crappy power supply!
Comment: notice that unfortunately the "$30 items" look to be ***not*** earthed, which is a shame.  as you say, the original "enormous toaster" one I do remember was in fact earthed.
Comment: How did you decide they weren't earthed? Pictures of them I found showed a 3-pin mains connector. I have a box-full of similar PSUs, but 12v 4A units, all earthed.
Answer: The interesting thing about 21V DC is that it is 12V + 9V. These are two commonly found voltages for musical equipment, e.g. many power supplies for guitar pedals offer both 9V and 12V.
There are two ways to combine two outputs of a power supply, each with their own type of Y-cable. One that puts the outputs in series will combine the voltage, one that puts them in parallel will combine the current (ampere).
If you find a guitar pedal power supply with both 9V and 12V outputs of at least 1A, and the right voltage-summing Y-cable, this could be an easy solution. These power supplies are also specifically made with audio equipment in mind, so one from a reputable manufacturer should have decent shielding against electro-magnetic interference and other things that may introduce unwanted noise into the signal path.
More info at e.g. https://stinkfoot.se/archives/4242
Answer: Result ...
For anyone ever googling here who has this problem.
I did grab one of the "cheap-ass" ones, and, it does seem to be fine. It does not conk out after hours of use; volume seems the same as much as I remember.
So, the cheap ones are amazingly poorly made, the plug barely fits etc, but it gets the job done!
(I have no knowledge about whether a cheap one introduces hum, or such, that might be relevant in a pro setting.)

Korg SP-300 .. 21 volts?


Attribute a static public ip for a pod in kubernetes and specify in service

Question: As per Link , we can create pods with multiples networks , if the application opens port with non-default network (eth2,3 etc) How can we expose it as service? In service yaml i do not find any way to expose the service other than default.
Answer: Multus allows you to attach network devices to your Pods, though we should note these interfaces are not part of our Kubernetes cluster SDN.
With Multus based devices: you would allocate IPs to your Pods, either using DHCP, static IPs, ... up to you. Bearing in mind those addresses are independent from your Kubernetes SDN.
If other containers in that cluster need to connect those endpoints, then their traffic would to leave your SDN, going through their usual default gateway, which in turn should have some knowledge of (route to) your Multus addresses subnet(s).
However, you may still create Services, designating addresses out of your SDN. This would be done by creating an Endpoints object, alongside your Service, such as:
<code>---
apiVersion: v1
kind: Endpoints
metadata:
  name: svc-out-sdn
  namespace: ns
subsets:
- addresses:
  - ip: 10.0.0.1
  ports:
  - name: tcp-80
    port: 80
    protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: svc-out-sdn
  namespace: ns
spec:
  clusterIP: None
  ports:
  - name: tcp-80
    port: 80
</code>
Comment: 1/ correct. up to client to iterate over IPs returned by DNS resolution, should one of them fail. 2/ it could have, and then your nodes firewall would have redirected that Service IP to the actual backends addresses: but here, `spec.clusterIP=None` ensures that no Service IP would be allocated => internal DNS would return with your endpoint backend addresses instead.
Comment: LB is what i have now, but issue is it needs manual intervention when new nodes are added. So auto scaling is the problem.
Comment: If we are explicitly specifying EP then if one pod goes down service will be still returning the IP until we manually update it correct?
Comment: Also when the service is created it will still have clusterIP which is of main interface?
Comment: I think this can not be solution but still this is one possible workaround, why it can not be solution because pods can go up/down and would need application to handle the error when downed ips used.
Comment: Would you consider setting up a LoadBalancer in between? Maybe in Kubernetes as well? Some haproxy that does TCP health checks (or whatever, haproxy checks are pretty modular) to your actual endpoints addresses, and make your Service/clients point to that LB?


get_terms with Redis Object Cache question

Question: By default, hide_empty => is set to be true. If Redis object cache is disabled, the below code works just fine. But when Redis is enabled, the empty term will not be hidden. It just shows all the terms. I have tried to flush the cache. The issue remains.
Any idea what may cause the issue? thanks
$parentid = 182;
$args = array(
    'parent' => $parentid
);
$terms = get_terms( 'product_cat', $args );

if ( $terms ) {
echo '
';
foreach ( $terms as $term ) {
    echo '
';
        echo 'slug . '">';
        echo $term->name;
        echo '';
    echo '
';
}
echo '
';
}

Comment: Can you try echoing `$term->count` to see what's in there for those terms? If you've done some manually updating of the database that can definitely get out of sync. You might also want to look into [`wp_update_term_count_now`](https://developer.wordpress.org/reference/functions/wp_update_term_count_now/)
Comment: They are in 0 counts. And I managed to resolve it with $term->count > 0, thanks for the hints
Answer: I managed to solve it with term count
$parentid = 182;
$args = array(
    'parent' => $parentid
);
$terms = get_terms( 'product_cat', $args );
$count = count($terms);
if ( $terms ) {
echo '
';
foreach ( $terms as $term ) {
        if ($term->count > 0){
    echo '
';
        echo 'slug . '">';
        echo $term->name;
        echo '';
    echo '
';
}
}
echo '
';
}


Mysql Count datediff total rows if field value more than 1 and not less than zero

Question: the purpose of creating a post is, I can not find an example of a case in accordance with the case that I'm experiencing right now, I want to take the amount from the due date for more than 1 instead of minus or equal to zero
I'm sorry if it had already existed posting the same, because it does not find an example of an appropriate case. I just need an example in accordance with the example of my case
my db
id          userid          duedate                status
1           lexter          2016-01-27          not yet paid
2           jemmy           2016-02-01          not yet paid
3           zaya            2016-03-02          already paid
4           randy           2016-08-09          not yet paid
5           max             2016-03-08          not yet paid
6           neo             2016-03-14          already paid
7           nemo            2016-01-09          not yet paid

this mysql query
SELECT duedate,
     current_date() as datenow,
     count(datediff(current_date(), duedate) as late_payment) as late_payment
FROM
    mydb
WHERE
    status='not yet paid'

my query only count all of record although he is less than zero or greater than zero, and i want only the record greater than zero and not yet paid will be count.
Answer: This is called conditional counting and you can do it via sum() or count(). You need to place the case statement or if() within the group by functions. I'll show the example for the count():
SELECT duedate,
     current_date() as datenow,
     count(case when datediff(current_date(), duedate) >0 then 1 else null end) as late_payment_count
FROM
    mydb
WHERE
    status='not yet paid'
group by duedate, current_date()

You can simplify the condition, because datediff(current_date(), duedate) >0is actually the same as current_date() > duedate.
If your are interested in the number of late payments only, then you can further simplify your query as
SELECT duedate,
     current_date() as datenow,
     count(*) as late_payment_count
FROM
    mydb
WHERE
    status='not yet paid' and current_date() > duedate
group by duedate, current_date()

Comment: If you want just a single number, then leave just the count in the select list and remove the group by clause.
Comment: thanks for your reply this is awesome, but how to count all record where late_payment not zero and >=1 ?
Comment: If the answer provided a solution to your question, then pls mark it as accepted, so other viewers will know that the issue has been resolved.


Generalisation of infinite Ramsey's theorem to countably infinite or variable edge arities

Question: The usual statement of the infinite Ramsey's Theorem, as appears e.g. in
Wikipedia page on Ramsey's Theorem
is (paraphrasing slightly):

If $X$ is an infinite set, and $C$ is a finite set, and $n$ is a finite number, and we colour the $n$-element subsets of $X$ with colours from $C$, then $X$ has an infinite monochromatic subset $M$, meaning that every $n$-element subset of $M$ has the same colour.

My questions are:

Is there a generalisation to the case where n is infinite, e.g. if $n$ is $ω$ and we are colouring countably infinite subsets of $X$ (we may need to assume that $X$ is much larger than countably infinite)?
Is there a generalisation to the case where we fix a finite range $1\leq n'\leq n\in\mathbb N$ and colour subsets of $X$ whose size is no less than $n'$ and no greater than $n$ (instead of subsets of $X$ whose size is precisely $n$)?
Answer: For part $2$, the answer is yes. You can ensure this inductively: Say you are coloring all $n'$-sized subsets of $X$ for $1\le n'\le n$. First, extract from $X$ an infinite subset $X_1$ with all $1$-element subsets of the same color. Now extract from $X_1$ an infinite subset $X_2$, all of whose $2$-element subsets have the same color, and so on.
(Note we cannot ensure for $i\ne j$ that the color of the $i$-element sets is the same as the color of the $j$-element sets. This is simply not possible, as our coloring may explicitly depend on the size of the set being colored.)
Ramsey's theorem admits strengthenings where the homogeneous set (the set $M$ in the notation from the question) is uncountable. For each specific uncountable cardinal $\kappa$ and $n>0$ there is a cardinal $\lambda$ such that any coloring of the $n$-sized subsets of a set of size $\lambda$ admits a homogeneous set of size $\kappa$. The cardinal $\lambda$ is typically larger than $\kappa$. Inductively we see that, picking $\lambda$ large enough, we can ensure the generalization of $2$ to hold as well, we just need the sets $X_1,\dots$ as above to each be large enough, and the precise size can be computed easily.
One could ask about the more general question of coloring, for all positive $n\in \mathbb N$, all $n$-sized subsets and asking for a set simultaneously homogeneous for all $n$. The argument above would not work (there are no infinite decreasing sequences of cardinals under the axiom of choice). However, these versions still hold, as long as we admit the existence of certain large cardinals (the set $X$ we begin with must indeed be very large).
For part $1$ the answer is no. The axiom of choice is needed here. That it is not possible to have a Ramsey's theorem for $\omega$-tuples is a result of Erdős and Rado, see

MR0065615 (16,455d). Erdős, P.; Rado, R. Combinatorial theorems on classifications of subsets of a given set. Proc. London Math. Soc. (3) 2, (1952). 417-439.

Given a set $X$, consider a well-ordering $<$ of the collection $[X]^{\aleph_0}$ of countable subsets of $X$, and color a subset $A$ with color $0$ if $A$ is the $<$-smallest member of $[A]^{\aleph_0}$ and with color $1$ otherwise. If there is an infinite homogeneous set $H$ for this coloring, one quickly reaches a contradiction: If $A$ is the $<$-smallest member of $[H]^{\aleph_0}$ (which exists, since $<$ is a well-ordering), then $A$ has color 0 (by definition) so, since $H$ is homogeneous, any countable subset of $H$ has color 0. Now, if $B\subset C$ are both countable infinite subsets if $H$, we have $C<B$
since $C$ has color 0, so it is the $<$-smallest member of $[C]^{\aleph_0}$. But we can easily find an increasing sequence $A_0\subset A_1\subset\dots$ of countable infinite subsets of $H$. This gives us a decreasing sequence $A_0>A_1>\dots$ in the well-ordering.
Note that the proof gives the strongest possible negative result: The original set $X$ can be as large as you want, and we are only coloring the countable subsets of $X$ (one could imagine coloring larger infinite subsets; but if there is a Ramsey's theorem for such a version, one easily derives from it a Ramsey's theorem for countable infinite subsets), and we are only looking for a countable homogeneous set.
That choice is needed is a consequence of results of Mathias that shows that in Solovay's model where all sets of reals are Lebesgue measurable, Ramsey's theorem holds for colorings of the countable infinite subsets of $\mathbb N$, see

MR0491197 (58 #10462). Mathias, A. R. D. Happy families. Ann. Math. Logic 12 (1977), no. 1, 59–111.

There is some pretty and interesting combinatorics of versions of Ramsey's theorem for colorings of infinite subsets in the absence of choice and particularly in the context of the axiom of determinacy. These results are also closely connected to the theory of large cardinals.
Comment: Andrés, I'm pretty sure that on the Erdős actual article the name appearing is correct, and in the MR (and therefore in your, and countless other citations probably) it is incorrect.
Comment: Many thanks for this detailed response.  Are you able to be more specific about where (e.g. theorem or lemma number) in [Happy families](https://www.sciencedirect.com/science/article/pii/0003484377900067) the result "in Solovay's model where all sets of reals are Lebesgue measurable, Ramsey's theorem holds for colorings of the countable infinite subsets of N" appears, please?
Comment: @Jim It is not stated cleanly this way, but it is the content of Metatheorem 5.1 and its proof, which uses results of section 3. (I think the idea is that people reading the paper are expected to be familiar with Solovay's construction, since in the introduction, Mathias makes it clear that this is what he proves in section 5.) By the way, it is also in section 5 that he conjectures that there are no MAD families in ZF+DC under the Ramsey theoretic assumption we are discussing. This was finally settled earlier this year by Schrittesser and Törnquist.
Comment: @Asaf Ugh. I checked; you are right, thanks. We'll fix it.
Comment: Always a pleasure!


How does surjectivity imply the equality of the image set to the codomain?

Question: Graphically, it seems that surjective functions have the codomain and the image equal to one another. But, working only with the definition of surjectivity, I can only understand how the all quantifier in "$\forall y\in Y \exists x\in X: f(x) = y$" implied the entirety of the set $Y$ and not necessarily the entirety of $X$ to actually form $f(X) = Y$, and therefore have the supposed equality proven. Am I wrong to assume such equality? Much thanks in advance.
Answer:
I can only understand how the all quantifier in "$\forall y \in Y \exists x \in X \colon f(x) = y$" implied the entirety of the set $Y$ and not necessarily the entirety of $X$ to actually form $f(X)=Y$

I'm not entirely sure what you're trying to say here, but I assume you seem to see that the image of $X$ under $f$ is a subset of $Y$, and the converse is the problem?

Note: I assume 0 is a natural number.
A function takes an element in the domain and maps it to a unique element in the codomain. The image of the domain under the function is the set of points that the function actually maps to. For example, define $$f \colon \mathbb{N} \rightarrow \mathbb{Z}$$ where $$f(n) = n$$ Then $\mathbb{N}$ is the domain, $\mathbb{Z}$ is the codomain. What is the image of $\mathbb{N}$ under $f$? We have to work a little bit to figure this out: take any element in $\mathbb{Z}$, $m$ say. If there is a natural number $n$ such that $f(n)=m$ then $m$ is in the image; otherwise it isn't. So, for our choice of $f$, the image of $\mathbb{N}$ under $f$ is just $\mathbb{N}$. No negative integer, however, is hit by $f$. The negative integers are in the codomain, but not in the image. Clearly, the image of $f$ is a subset of the domain. This map is not surjective.
Now, with domain and codomain as above, what if we define $g(0) = 0, g(2n+1)=n+1, g(2n)=-n$? You can verify that this function $g$ maps 0 to 0, 1 to 1, 2 to -1, 3 to 2, 4 to -2, etc. What is special about this function is that the image equals the codomain: indeed, any integer is hit by $g$ this time. Or, in other words, no integer can avoid the image of $g$.
This latter map is an example of a surjective map. You can think of being surjective as "the image covers the codomain" -- I always think of a blanket: if the domain is "large" enough and the domain can be "stretched" by $f$ so that it covers the codomain, then it is surjective. (For an example where the domain is "too small", take any finite set as the domain, the codomain still $\mathbb{Z}$; no function will be able to stretch this finite domain to cover an infinite set. So there exists no surjection.)

Visually, I always find diagrams like this helpful (taken from https://en.wikipedia.org/wiki/Codomain):

Here, red is the domain, blue the codomain, yellow the image. So, your intuition is right! If $f$ can stretch the domain far enough, then it is surjective; that equates to covering the blue codomain with the yellow blanket.

Now for a proof, we assume $f \colon X \rightarrow Y$ is surjective. Write $\mathrm{Im}(X)$ for the image of $X$ under $f$. We show $\mathrm{Im}(X) = Y$.

It follows from the definition that the image is a subset of the codomain, so there is nothing to do to show that $\mathrm{Im}(X) \subset Y$.
For the other direction, suppose $y \in Y$. We have to show $y$ is in the image of $f$ under $X$. Since $f$ is surjective, there is an $x$ in $X$ with $f(x) = y$. The image of $X$ under $f$ is exactly the set of points $f(z)$ for all $z \in X$. So, in particular, $f(x) \in \mathrm{Im}(X)$. But we established that $f(x) = y$, and so $y$ is in the image of $X$ under $f$. As $y$ was chosen arbitrarily, we are done.
Comment: Thanks for the answer! I meant that I didn't understand how $f(X) = Y$ can be derived from the definition of surjectivity, since the all quantifier relates to $Y$.
Comment: @Jobran Not all quantifiers relate to $Y$, only the leading one does. That shows that surjectivity really is a property of both the domain and the codomain. You can change the domain of $g$ to any proper subset of $\mathbb{N}$, for example, and $g$ will fail to be surjective. Likewise, if you properly extend the codomain of $g$ (to the real numbers $\mathbb{R}$ for example), surjectivity fails, too.
Answer: If f(X) = Y, then for all y in Y, exists x with f(x) = y.
Assume for all y in Y, exists x with f(x) = y.
Thus Y subset f(X).  Clearly, f(X) subset Y.
Consequently f(X) = Y.
I have no idea how to show any of this graphically.
Comment: Thanks for your answer. I'm trying to understand why $f(X) \subset Y$ und $Y \subset f(X)$ given that the all quantifier relates to Y.
Comment: @Jobran  For all x in X, f assigns a value in Y.
Answer: By definition, $y \in Y$ lies in the image of $X$ if and only if $\exists x \in X : f(x) = y$.
For arbitrary $y$, let's call this statement $\phi(y)$. $\phi$ is true for a particular $y$ if and only if it lies in the image.
Now it should be clear that your definition of surjectivity is just $\forall y \in Y \phi(y)$. This is exactly the statement that all elements of the codomain are in the image: $Y \subseteq F(X)$. Clearly we have $F(X) \subseteq Y$ and so $F(X) = Y$.

The reason the universal quantifier ($\forall$) refers to $Y$ only is because surjectivity of $f$ is a statement about the images of elements in $X$, and hence about elements in the set $Y$.
However, if you like, we can rephrase the definition to talk about elements of $X$:
$f$ is surjective if (and only if) $\not \exists y \in Y$ such that $\forall x \in X f(x) \neq y$.


Apparently Unexplained Load Average

Question: I have been experiencing very strange server load, but for no obvious reason. Could anyone explain the cause/how to debug further?

One Minute      - 22.9
Five Minutes    - 17.98
Fifteen Minutes - 10.02

top - 20:34:28 up 22 days, 7:51, 0 users, load average: 22.55, 22.49, 14.51
Tasks: 131 total,   3 running, 128 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.1%us,  0.0%sy,  0.0%ni, 98.6%id,  1.3%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   2097152k total,   596576k used,  1500576k free,        0k buffers
Swap:        0k total,        0k used,        0k free,        0k cached

 PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
11854 root      18   0  2444  980  720 R  2.0  0.0   0:00.01 top
11856 root      18   0  2444  988  720 R  2.0  0.0   0:00.01 top
   1 root      15   0  2156  592  564 S  0.0  0.0   0:10.26 init
3393 apache    18   0 50276  33m 1888 S  0.0  1.6   0:00.00 httpd
3445 pegpro    18   0 17872 3304 2368 D  0.0  0.2   0:00.03 php-cgi
3446 root      18   0  5040 1056  852 S  0.0  0.1   0:00.00 crond
3723 apache    15   0 50276  33m 1896 S  0.0  1.6   0:00.01 httpd
3735 pegpro    18   0 17872 3308 2368 D  0.0  0.2   0:00.03 php-cgi
3752 root      18   0  9152 2068 1740 S  0.0  0.1   0:00.01 dataskq
3956 root      18   0  5040 1128  852 S  0.0  0.1   0:00.00 crond
5138 root      18   0 20380  15m 1712 S  0.0  0.8   0:00.05 lfd
5279 root      18   0  9152 2084 1752 S  0.0  0.1   0:00.05 dataskq
5331 root      18   0  5040 1108  852 S  0.0  0.1   0:00.00 crond
5496 admin     18   0 17872 3308 2368 D  0.0  0.2   0:00.01 php-cgi
5637 root      18   0  9152 2080 1752 S  0.0  0.1   0:00.01 dataskq
5641 apache    16   0 50276  33m 1896 S  0.0  1.6   0:00.03 httpd
5648 root      18   0 49988  33m 2036 S  0.0  1.6   0:00.67 httpd
5702 apache    18   0 50280  33m 1820 S  0.0  1.6   0:00.03 httpd
5851 admin     18   0 17872 3304 2368 D  0.0  0.2   0:00.01 php-cgi
7256 mail      16   0 10364 2700 2176 D  0.0  0.1   0:00.02 exim
7287 apache    15   0 50276  33m 1876 S  0.0  1.6   0:00.00 httpd
7379 root      18   0  5040 1128  860 S  0.0  0.1   0:00.02 crond
7474 apache    16   0 50280  33m 1836 S  0.0  1.6   0:00.00 httpd

One Minute      - 22.9
Five Minutes    - 17.98
Fifteen Minutes - 10.02

top - 20:34:28 up 22 days,  7:51,  0 users,  load average: 22.51, 22.49, 14.55
Tasks: 131 total,   3 running, 128 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.1%us,  0.0%sy,  0.0%ni, 98.6%id,  1.3%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   2097152k total,   596576k used,  1500576k free,        0k buffers
Swap:        0k total,        0k used,        0k free,        0k cached

 PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
11856 root      18   0  2444  988  720 R  2.0  0.0   0:00.01 top
   1 root      15   0  2156  592  564 S  0.0  0.0   0:10.26 init
3393 apache    18   0 50276  33m 1888 S  0.0  1.6   0:00.00 httpd
3445 pegpro    18   0 17872 3304 2368 D  0.0  0.2   0:00.03 php-cgi
3446 root      18   0  5040 1056  852 S  0.0  0.1   0:00.00 crond
3723 apache    15   0 50276  33m 1896 S  0.0  1.6   0:00.01 httpd
3735 pegpro    18   0 17872 3308 2368 D  0.0  0.2   0:00.03 php-cgi
3752 root      18   0  9152 2068 1740 S  0.0  0.1   0:00.01 dataskq
3956 root      18   0  5040 1128  852 S  0.0  0.1   0:00.00 crond
5138 root      18   0 20380  15m 1712 S  0.0  0.8   0:00.05 lfd
5279 root      18   0  9152 2084 1752 S  0.0  0.1   0:00.05 dataskq
5331 root      18   0  5040 1108  852 S  0.0  0.1   0:00.00 crond
5496 admin     18   0 17872 3308 2368 D  0.0  0.2   0:00.01 php-cgi
5637 root      18   0  9152 2080 1752 S  0.0  0.1   0:00.01 dataskq
5641 apache    16   0 50276  33m 1896 S  0.0  1.6   0:00.03 httpd
5648 root      18   0 49988  33m 2036 S  0.0  1.6   0:00.67 httpd
5702 apache    18   0 50280  33m 1820 S  0.0  1.6   0:00.03 httpd
5851 admin     18   0 17872 3304 2368 D  0.0  0.2   0:00.01 php-cgi
7256 mail      16   0 10364 2700 2176 D  0.0  0.1   0:00.02 exim
7287 apache    15   0 50276  33m 1876 S  0.0  1.6   0:00.00 httpd
7379 root      18   0  5040 1128  860 S  0.0  0.1   0:00.02 crond
7474 apache    16   0 50280  33m 1836 S  0.0  1.6   0:00.00 httpd
7550 apache    18   0 50276  33m 1924 S  0.0  1.6   0:00.00 httpd

Comment: Check out `dmesg` output, and copy'n paste relevant parts to here (for example OOM or driver crash can cause similar behavior).
Comment: Not too sure what this should be, it just contains lots of firewall info? =================== Firewall: *TCP_IN Blocked* IN=eth0 OUT= MAC= SRC=188.8.131.52 DST=22.214.171.124 LEN=48 TOS=0x00 PREC=0x00 TTL=120 ID=58736 DF PROTO=TCP SPT=2959 DPT=8443 WINDOW=16384 RES=0x00 SYN URGP=0
Answer: If you look at both top outputs you'll notice a fair number of processes in state(S) 'D'.  This means they are waiting for disk io.  In most modern UNIX environments the load average number is a combination of both runable processes(waiting for CPU) and processes waiting for disk io.  It appears as if your server has probably saturated the available IO subsystem for the demand placed on it.  You can verify this with tools like iostat (try to add the iostat or sysstat packages on your system).  Then run:
# iostat -x 1

and watch for the busy %

Apparently Unexplained Load Average


How to express a variable in terms of other variables in a system of equations?

Question: I want to know how to express $a$ in terms of $x$, $y$ and $z$ in the following system (i.e., I want to find the function $a(x,y,z)$). And how to investigate (and to play with the equations) to know whether a variable can expressed in terms of $x, y, z$ or not in similar situations?
$$\begin{cases}
x + 2y -z = 2\\
2x + 5y -(a+2)z = 3\\
-x + (a+5)y + z = 1
\end{cases}$$
P.S.: I would enjoy the solutions written in Maple or Mathematica format too.
Comment: I think you may want to express $x,y$ and $z$ in terms of $a$. If you still insist in doing it the other way around, solving the problem like an ordinary linear system in the unknown variables $x,y$ and $z$ is still the way to go. You can always invert the relationships afterward if necessary.
Comment: The try the following link. You'll have to copy the rest of the line into your browser: http://www.wolframalpha.com/input/?i=solve+x%2B2y-z%3D2%2C+2x%2B5y−%28a%2B2%29z%3D3%2C+−x%2B%28a%2B5%29y%2Bz%3D1++for+a
Comment: Here's @lhf's link in a single clickable version: http://wolframalpha.com/input/?i=solve+x%2B2y-z%3D2%2C+2x%2B5y%2D%28a%2B2%29z%3D3%2C+%2Dx%2B%28a%2B5%29y%2Bz%3D1++for+a
Comment: @Theo, thanks. How did you do it?
Comment: @lhf: I tweaked the link using percent encoding (I replaced two or three `-` signs by `%2D`), see http://en.wikipedia.org/wiki/Percent-encoding for an explanation. You can test if a link works in an answer field and wherever the link breaks in the preview you need to do something.
Answer: Solving the system for $x$, $y$, and $z$ in terms of $a$ gives equations that relate $a$ to each of $x$, $y$, and $z$, each of which can be solved for $a$ in terms of one of the other variables.  (Having three equations and four unknowns, it is most likely that any three of the variables can be expressed in terms of the fourth, or alternately that any one variable can be expressed solely in terms of each of the other three variables.)
Using Mathematica:
<code>Solve[
 x + 2 y - z == 2 &&
  2 x + 5 y - (a + 2) z == 3 && -x + (a + 5) y + z == 1, {x, y, z}]

(*
==> {{x -> -((-10 - 9 a - 2 a^2)/(a (7 + a))), y -> 3/(7 + a),
  z -> -2 + 15/(7 + a) + 10/(a (7 + a)) + (2 a)/(7 + a)}}
*)
</code>
So, for example, $y=\frac{3}{7+a}$, which gives $a=\frac{3-7y}{y}$.


Why isn't the #ajax property available for more Form API elements?

Question: Looking at https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7.x because I haven't seen a page updated for Drupal 8, it seems like only a select few form elements are allowed to have #ajax on them.  Isn't this too limiting?  I want to do Ajax on a markup element.  There are a lot of scenarios in which I would want to do Ajax with my own custom HTML, but I don't want to lose the functionality of the Form API, like getting the $form and $form_state.
The way I see it, there are two ways of doing Ajax in Drupal, either normally by just calling a controller, or by using an element in the Form API with an Ajax callback.  If I do Ajax normally, I can create data in Javascript however I want and send it to php, but I can't get the data in Drupal's $form object or call some of Drupal's special Ajax functions like entity autocomplete.  If I use the Form API, I lose the ability to respond to any events on certain elements of the form.
Why is it like this? And, is there a way to get around it without modifying core?
Answer: In Drupal 8 render elements are plugins and you can define a custom render element in a module without modifying core.
The scenarios you describe probably don't need a custom render element. A #markup with an event attached to it is like a link or a button, where you remove the styling with css.
Additionally you can hook into ajax js and react on events with your own javascript. See ajax.js, the hooks are called prototypes.
Comment: None of this seems to address my primary concern, which is, if I construct data with logic in Javascript, and want to send that to php via Ajax, is that possible? I looked in core and found an example of Drupal.AjaxCommands.prototype and it looks like the Ajax command that allows you to do your own javascript is triggered by php, which means I've already passed the point where I should have grabbed my data from the original javascript.  Does that make sense?  Drupal Ajax seems to be grabbing the form data, but I need to grab data I've created in JS.
Comment: To answer your question, I have data in javascript, how I send it to php via Ajax? Send a post request with your js data to drupal, define a controller with a route, which sends back a symfony response. Client side this is standard jquery ajax, drupal part is a simple controller.
Comment: I'm already doing this, but that's what led me to my first problem, I don't know how to get the $form and $form_state objects by doing it this way.
Comment: The only way I know how to transfer js generated data in a form is the hidden element.


Sum Pyramid with backtracking

Question: I'm trying to solve this problem and I'm new to backtracking algorithms,
The problem is about making a pyramid like this so that a number sitting on two numbers is the sum of them. Every number in the pyramid has to be different and less than 100. Like this:
<code>     88
   39  49
  15  24  25
 4  11  13  12
1  3   8   5   7
</code>
Any pointers on how to do this using backtracking?
Comment: I think it would help if you provided more instructions, such as how many total numbers there should be in the pyramid, or any other requirement.
Comment: I assume the whole thing is about: given a number N (N<100) creates the tallest pyramid possible such as a number sitting on two numbers is the sum of them.
Comment: ... and the resulting pyramid has N as its top...
Answer: Not necessarily backtracking but the property you are asking for is interestingly very similar to the Pascal Triangle property.
The Pascal Triangle (http://en.wikipedia.org/wiki/Pascal's_triangle), which is used for efficient computation of binomial coefficient among other things, is a pyramid where a number is equal to the sum of the two numbers above it with the top being 1.
As you can see you are asking the opposite property where a number is the sum of the numbers below it.
<code>                1
              1   1
            1   2   1
          1   3   3   1
        1   4   6   4   1
      1   5  10  10   5   1
    1   6  15  20  15   6   1
  1   7  21  35  35  21   7   1
1   8  28  56  70  56  28   8   1
</code>
For instance in the Pascal Triangle above, if you wanted the top of your pyramid to be 56, your pyramid will be a reconstruction bottom up of the Pascal Triangle starting from 56 and that will give something like:
<code>                 56
              21    35
            6    15    20
         1     5    10    10
</code>
Again that's not a backtracking solution and this might not give you a good enough solution for every single N though I thought this was an interesting approximation that was worth noting.

Sum Pyramid with backtracking


MySQL: Error Code: 1411. Incorrect datetime value: '24102018915G' for function str_to_date 0.000 sec

Question: I am trying to do some reverse engineering the following SQL is a working version. I am trying to create a staging database and the insert statement is failing.
Any idea what parameters I need to check to make it work in temp db??
INSERT INTO
SECURITY_STAGING (INSERT_TYPE, ADDED_DTTM, SECURITY_CODE, SECURITY_NAME, SECURITY_GROUP, LAST_TRADED_PRICE, CHANGE_AMT, CHANGE_PER)
VALUES ('GAINER',STR_TO_DATE('24102018915G','%d%m%Y%H%i'),'532524','PTC','A','78.00','3.10','4.14')

Error Code: 1411. Incorrect datetime value: '24102018915G' for
  function str_to_date  0.000 sec
Comment: how do you get the date time of '24102018915G' ? the error is clear about what causing the error.
Comment: Are you sure about the "g" letter of your string?
Comment: 24102018915G is picked up from the file name generated as per datestamp...seems the leading 0 for the time is missing.
Answer: This produces a valid timestamp:
select STR_TO_DATE('241020180915','%d%m%Y%H%i')

This does not
select STR_TO_DATE('24102018915G','%d%m%Y%H%i')

You are missing a zero and have a G instead.


Estimate group averages

Question: If I have 100 numbers from two separate groups $X$ and $Y$.
How can I manually estimate or derive an algorithm to automatically estimate $AVG(X)$ and $AVG(Y)$?
I know all the numbers, but I don't know which numbers belong to which group. Is it really possible to give a proper answer to this question?
Comment: It may be possible if you provide more information Eg if you specify the family of distributions from which the sample comes... You might want to look at expectation maximisation (EM) algorithm
Comment: I guess I can assume that follow a normal distribution. I have just read a bit about EM algorithm, but in the EM algorithm, it seems that I know which results belong to which group.
Comment: No,  you start with a guess of the parameters of the two normal distributions ,  based on these you allocate the data to your two normal distributions (by max likelihood) ,  then you recalculate the parameters of the distributions using your data split,  and repeat until convergence.
Comment: Can you give an example if I just have 10 numbers: $[1, 2, 8, 7, 4, 3, 2, 4, 9, 1]$?
Answer: As I understand your question, you have a vector of datapoints that come from two distributions $f_X$ and $f_Y$, where you do not know their group memberships. You can think of this data as of mixture of $f_X$ and $f_Y$ distributions and to solve it you can use one of the clustering algorithms designed for such problems. The simple non-parametric approach would be to use algorithms such as k-means, but you could use also model-based approach such as finite mixture model (e.g. Mclust). It is not a problem for either of the algorithms that the values are shuffled.
Let me illustrate those approaches with an example with simulated data.
set.seed(123)

x <- rnorm(50, 1, 5)
y <- rnorm(50, 5, 2)

ord <- sample(100)
xy <- c(x, y)[ord]

For this dataset k-means returns
Cluster means:
       [,1]
1 -1.842147
2  5.512294

Mclust algorithm implemented in mclust library for one-dimensional data assuming variable variance estimates means
$mean
       1        2
1.397002 5.416184

and variances
$variance$sigmasq
[1] 20.451195  2.700587

or estimated using flexmix library (see Leisch, 2004 and Grün and Leisch, 2008) returns
       Comp.1   Comp.2
mean 5.403225 1.437003
sd   1.651631 4.562311

Each of the methods classified correctly group membership 78-79% of cases. In this example groups were equal-sized but this does not have to be the case for any of the algorithms described. If you have knowledge of variances in both groups this would make estimation easier. Here it was assumed that both groups are normally distributted, but you can make other distributional assumptions in finite mixture model as well. It is most popular to use E-M algorithm or Bayesian approach to estimate finite mixture models.

Estimate group averages


On two-stage-least-squares regression

Question: My professor has just covered the concept of two-stage-least-squares (TSLS) regression today.
In particular, we explored the over-identified case where we have a single endogenous regressor, $X$, with $m$ instruments and $r$ controls. It was explained that, in the first stage, we regress $X$ on the $m$ instruments and $r$ controls to get the ordinary-least-squares estimators of $X$, say, $\hat{X}$. Subsequently, in the second stage, we regress $Y$ on $\hat{X}$ and the $r$ controls to get the TSLS estimators overall.
Now, I noticed that we were regressing on the $r$ controls in both stages of the TSLS regression and I wondered why this was so, since my thoughts are that the effects of the $r$ controls are already captured in $\hat{X}$ by the first stage, so regressing on these $r$ controls again in the second stage would either be redundant or "double-counting the effects of the $r$ controls".
I discussed this with my professor and he seemed stumped by this too; he said that this had not crossed his mind before and he could not come up with a good answer for me. Thus, I am hoping that some of the great minds here can shed some light on this intuitively :)
Answer: I have not thought about this, but let me have a crack at it.
The first stage of 2-SLS is an attempt to remove the bias caused by endogeneity. For $ \hat X $ to be a good instrument for $ X $, it must be correlated with $ X $ but not with the error/residuals of the main equation.
As with any other regression, we need to include all the independent variables that explain variations in $ X $ in the first stage. Then, we can obtain a good instrument $ \hat X $ and use it in the second stage. Therefore, we will need to include some instruments $ m $ in the first stage.
These instruments are the variables that affect $ X $ and also have some association with $ Y $ through $ X $.
Along with these instruments, we also include all the control variables (all exogenous variables) in the first stage. Why? Because they belong to a system of equations and are interrelated. All these variables affect $ Y  $ and $ X $ either directly or indirectly through other variables in the system. This means that they also affect $ X $ either directly or indirectly and we need them in the first stage to get a good instrumental variable $ \hat X $.

As put by A. Koutsoyiannis in "Theory of Econometrics":
Every exogenous variable, regardless of the equation in which it
appears, affects all the endogenous variables of the system either
directly (if it is present in the equation) or indirectly (by
influencing other endogenous variables in the equation in which it
does not appear explicitly).
Comment: Hello! Thank you for your answer! I guess you have explained why we need to include the controls in the first stage, but I suppose it still does not really explain why we do also need to include them in the second stage? Hopefully others can chip in too!
Comment: @EthanMark It's for the same reason. If you read the quote carefully, ALL variables are interrelated in a system of equations. The control variables help explain both X and Y, so they have to be included in both. X and Y are different variables. In the first stage, they explain variations in X and their indirect effect on Y through X. In the second stage, they explain the variations in Y (direct).

On two-stage-least-squares regression


DHCPv6 not working when UFW is enabled (Ubuntu 16.04 LTS)

Question: So, as the title says, I setup a isc-dhcp-server for DHCPv6, which is working as long as UFW is disabled.
Once enabled, though I have all the necessary rules in place, and UFW is enabled for v6, it stops working.
My rules are:
<code>xxx@deadpool:/etc/ufw# grep '546\|547' *
after6.rules:-A ufw6-after-input -p udp --dport 546 -j ufw6-skip-to-policy-input
after6.rules:-A ufw6-after-input -p udp --dport 547 -j ufw6-skip-to-policy-input
before6.rules:-A ufw6-before-input -p udp -s fe80::/10 --sport 547 -d fe80::/10 --dport 546 -j ACCEPT
</code>
I found two related but fixed bugs on launchpad:
https://bugs.launchpad.net/ubuntu/+source/ufw/+bug/947416
https://bugs.launchpad.net/ubuntu/+source/ufw/+bug/1007326
Any ideas?
Comment: i had to `ufw allow proto udp from any to any port 546 &&
ufw allow proto udp from any to any port 547` to get it to work.
can't figure out why for the heck of it. only link local addresses should be required for DHCPv6 anyways ...
Answer: I see an existing rule for a DHCP client:
<code># allow dhcp client to work
-A ufw6-before-input -p udp -s fe80::/10 --sport 547 -d fe80::/10 --dport 546 -j ACCEPT
</code>
I think you need to reverse the source/destination ports for your *server:
<code># dhcp server
-A ufw6-before-input -p udp -s fe00::/7 --sport 546 -d fe00::/7 --dport 547 -j ACCEPT
</code>
Answer: Change it to fe00::/7, you have to allow IPv6 multicast addresses as well.  Your server should be listening on ff05::1:3 and ff02::1:2.
See: https://en.wikipedia.org/wiki/Multicast_address#IPv6


How do I delete a related object with a 1 to 1 relationship in the Entity Framework?

Question: I'm using EF Model First and in my Employee class I have a Manager property with a 1 to 1 relationship to itself, the Employee class:
public partial class Employee
{
    public Employee()
    {
    }

    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public virtual Employee Manager { get; set; }
}

Now, when I get an existing object from the database and change the manager with:
employee.Manager = otherEmployeeInstance;
Context.Entry(employee).State = System.Data.EntityState.Modified;
Context.SaveChanges();

It works just fine; however, if I want to remove the manager this will not work:
employee.Manager = null;

It looks to me that I first need to "load" the manager (Employee) instance into the context since this makes it work:
var dummyVar = employee.Manager.Id;
employee.Manager = null;

So the question is, what's the best (proper) way to remove a related object?
Answer: obviously, when you want to delete an entity of the one-to-one relationship, the EF doesn't allow to delete it without preparing it before. that is, removing any dependent entity information from that entity. Remember, we are using Relational Database system and any breaking relation without any coordination causes to anomaly and failure.
When you want to remove a relation, you can use this at your controller:
employee.Remove(Manager);//Automatically Removes Navigational Properties at both entities
db.SaveChanges();

Instead of this :
employee.Manager = null;

Comment: With model-first, VS2010 generates a POCO object. As you may see in my code, Employee does not have a remove method. Am I missing something else?
Comment: not a remote method. but any relationship between entities is considerable here. in this problem there is no differences between any design methods like model-first or code-first. make sure you have removed any dependent properties before deleting an entity.
Comment: What do you mean with "make sure you have removed any dependent properties before deleting an entity"? In my case I'd like to remove the relationship since an employee may not be the manager's subordinate anymore. However, I don't want to remove the "Manager" instance from the database.
Comment: In relational operations you need to remove a relation in correct way and complete manner. If you want to store previews states, so save the track of deleted relation or save it at other tables, maybe.


How many functions $f$ from $A$ to $B$ are not injective, not surjective?

Question: I have a problem specific to discrete mathematics, more exactly: functions.
Let's say, we have two finite non-empty sets: $A$ and $B$. Moreover, we have a function $f$ from $A$ to $B$.
Finally, we denote $|A|$ to be the cardinal of the set $A$, so we might have $|A|=n$ and $|B|=m$.
How many functions $f$ from $A$ to $B$ are not injective, not surjective?
I need a generalized rule for computing the number of not injections, not surjections.
Answer: Hint:the number of functions from $A$ to $B$ is $n^m$.
Now find the number of functions that are injective and surjective.Let's say there are $k$ in number.
Then what you ask is $n^m-k$.
Of course,if $n\neq m$ we have that $k=0$.
Now, if your intention was to ask how many functions are only not injections,just find the number of injections and do the same ask above.
Same for the number of only not surjections.
Comment: You helped me a lot! Thank you. I forgot about the number of functions.
Comment: The number of injections when $n \le m$ is straightforward, but the number of surjections when $n \ge m$ is trickier.


Regd. Riemann Rearrangement

Question: Assume that A is an arbitrary set and there exists a bijection $\phi : B \rightarrow A$ and $x_{\alpha} \in [0,\infty]$, the book says that
$$\Sigma_{\alpha \in A} x_{\alpha}= \Sigma_{\beta\in B}  x_{\phi(\beta)}$$
can fail if the series is not absolutely convergent and we are dealing with signed sums.
I fail to see why this is true.
Comment: What does «Regd.» mean? The little bit of time one saves when using an abbreviation is paid, with a huge interest, by all those reading it...
Comment: Do you mean $x_\alpha\in [0,\infty]$ for each $\alpha\in A$?
Answer: If a series is not absolutely convergent, then it only converges because some positive and negative terms cancel in the right way as you continue summing. However, upon rearrangement, you may find that the positive and negative terms do not cancel the same way. Consider the series
$$ 1 - \frac{1}{2} - \frac{1}{2} + \frac{1}{4} + \frac{1}{4} + \frac{1}{4} + \frac{1}{4} - \frac{1}{8} - \frac{1}{8} - \frac{1}{8} - \frac{1}{8} - \frac{1}{8} - \frac{1}{8} - \frac{1}{8} - \frac{1}{8} + \ldots$$
You can see that this sum does not converge because the partial sums oscillate between $0$ and $1$. However, let us rearrange the terms in the following manner:
$$ 1 - \frac{1}{2} + \left(- \frac{1}{2} + \frac{1}{4} + \frac{1}{4}\right) + \left(\frac{1}{4} - \frac{1}{8}- \frac{1}{8}\right) + \left( \frac{1}{4} - \frac{1}{8} - \frac{1}{8}\right) + \left( - \frac{1}{8} + \frac{1}{16} + \frac{1}{16} \right) - \ldots$$
If you check this you will find that the terms of this series are the same as the terms of the previous series, but in a different order. However, this second series does converge.
What we can see here, intuitively, is that in the first series, too many positive and negative terms are grouped together, so they cancel each other out in "huge chunks", causing the oscillation between $0$ and $1$. However, in the second series, the positive and negative terms are interleaved in such a way that they cancel each other out in small "small chunks", causing oscillations that exponentially decrease in size.
Comment: Awesome! Thanks!

Regd. Riemann Rearrangement


C# Validation control

Question: Is it possible to use asp:RequiredFieldValidator and asp:RegularExpressionValidator for a single textbox.
Comment: You can always try that. This, for one, will take much less time than you spend waiting for an answer on SO. Plus, I believe that a properly constructed regex will disallow empty text to be entered in a textbox, thus rendering `asp:RequiredFieldValidator` unneccessary.
Comment: It may not be necessary in absolute terms, but if it means you can provide more meaningful error messages to the user i.e. distinguishing in the validation text between "required but missing" and "supplied but wrong" then it may be desirable. You're absolutely right about trying being faster than asking in this case!
Answer: Yes you can, just bind both to the same TextBox/whatever control you want to validate.
Answer: yes. Just put them both on your page and set their controlToValidate to the same control

C# Validation control


Difference between select * into newtable and create table newtable AS

Question: I am working with huge table which has 9 million records. I am trying to take  backup of the table.  As below.
First Query
Create table newtable1 as  select * from hugetable  ;

Second Query
Select * into newtable2  from hugetable  ;

Here first query seems to be quite fast. I want to know the functional difference and Why it is fast compare to second query?,  Which one is to be preferred?
Answer: Quote from the manual:

This command (create table as) is functionally similar to SELECT INTO, but it is preferred since it is less likely to be confused with other uses of the SELECT INTO syntax. Furthermore, CREATE TABLE AS offers a superset of the functionality offered by SELECT INTO.

So both do the same thing, but CREATE TABLE AS is part of the SQL standard (and thus works on other DBMS as well), whereas the SELECT INTO is non-standard. As the manuals says, you should prefer CREATE TABLE AS over the non-standard syntax.
Performance wise both are identical, the differences you see are probably related to caching or other activities on your system.


Properties of a quotient.

Question: If $A$ is a closed topological subspace of $B$, is the quotient map $\pi: B\rightarrow B/A$ a closed map ? I do mean that if $F$ is closed subset of $B$ then $\pi(F)$ is a closed subset of $B/A$ ? If not what should be the optimal condition on $A$ and $B$ to have a closed quotient map $\pi$
Answer: If $D$ is a subset of $B/A,$ then $D$ is closed if and only if $\pi^{-1}(D)$ is closed in $B.$
If $C$ is a subset of $B$ then $$\pi^{-1}(\pi(C))=\begin{cases}C&C\cap A=\emptyset\\
C\cup A&C\cap A\neq \emptyset\end{cases}$$
So when $A$ and $C$ are closed in $B$, then $\pi^{-1}(\pi(C))$ is closed, and hence $\pi(C)$ is closed in $B/A.$
This would not necessarily be true if $A$ were not closed. For example, if $A$ is not closed but has a closed non-empty subset, $F,$ then $\pi^{-1}(\pi(F))=A$ is not closed.

Properties of a quotient.


$\sum\frac{(k!)^2}{2k!}$ serie converge or diverge

Question: Does the following series diverge or converge.
$\sum\frac{(k!)^2}{2k!}$
I decide to apply the powerful ratio test.
I did
$\frac{(k+1)!^2}{2(k+1)!}$
which then becomes
$\frac{(k+1)!(k+1)!}{(2k+2)!}$*$\frac{2k!}{(k!)(k!)}$
I get the
$k\rightarrow\infty$
$\frac{(k+1)(k+1)}{(2k+2)(2k+2)}$
=$\frac{1}{4}$
$\frac{1}{4}$<1
convergent?
Comment: Is the denominator intended to be $2(k!)$ or $(2k)!$?
Comment: let me see it is$(k!)^2$
Comment: I mean sorry it is $(2k)!$
Comment: Then the only error in everything is that you need the parentheses; $2k! = 2(k!)$, the factorial has higher precedence than multiplication.
Comment: @FernandoMartinez : please fix your _question_, don't just explain the mistake in a comment
Answer: Assuming you're working with the series:
$$\sum\frac{(k!)^2}{(2k)!},$$ then you should be able to reduce the limit of the ratio to $$\lim_{k \to \infty} \frac{(k+1)(k+1)}{(2k+2)(2k+1)}$$
The limit is still $\frac 14$, and so, yes, by the ratio test, we have that the series converges.
Comment: Yes, correct: the $(2k)!$ factorial;-)
Comment: Oh yes I see because $(2k+2)(2k+1)$ are the only ones not cancel out in the $2k!$ factorial yes I think i made a typo
Answer: Fernando, I took a course on series last year; they taught us how to sum EVERYTHING. Even if a series is divergent, it still may be possible to sum! Sum is $\frac{1}{3} + \frac{2 \pi}{9\sqrt{3}}$ . Remember, you must sum first, converge later.
Comment: @Peter : If you could sum everything, life would be wayyy easier in mathematics. I think you're just claiming you managed to learn a lot of stuff. :P
Comment: But the task isn't to find the sum.
Comment: In my course on series, the task was ALWAYS to find the sum. I took a course on convergence the next semester and we learned a theorem to the effect that if the sum can be calculated, then the series is convergent.
Comment: but only a few series can sum like telescopic and geometric series unless you guys know more advanced series
Comment: No, every series can converge under a suitable interpretation, you must renormalize it. I think this is the answer your teacher is looking for. My prof once asked this as a trick question and many people said that some series do not converge, but there is always a renormailzation, you must search hard. For example $\sum_{n=1}^{\infty} n =-1/12$


Proof that the localization $R_S$ is naturally isomorphic to the localization at the saturation $R_{\overline{S}}$?

Question: Localizations have the universal property that if $S$ is a multiplicative subset of a commutative ring $R$, and $i\colon S\to R$ is the canonical embedding, then if $g\colon R\to T$ is any map such that $g(s)$ is a unit in $T$ for all $s$, then there exists a unique homomorphism $h\colon R_S\to T$ such that $g=hf$.
I was reading a handout of Brian Conrad's that said that the localizing at $S$ is the same as localizing at the saturation $\overline{S}$ of $S$, as can be seen "instantly" by the universal property, since the universal property of $R_S$ implies that this ring also has the universal property to be the localization with respect to $\overline{S}$.
How can you use the universal property to see this instantly? Taking $f: R\to R_S$ and $\bar{f}:R\to R_\overline{S}$ to be the standard embeddings, and $g:R\to R_\overline{S}:r\mapsto rs/s$ and $\bar{g}:R\to R_S:r\mapsto r\bar{s}/\bar{s}$ to essentially also be standard embeddings, so that they send elements of $S$ and $\overline{S}$ to units, I found unique $h$ and $\bar{h}$ such that $g=hf$ and $\bar{g}=\bar{h}\bar{f}$ in hopes that $h$ and $\bar{h}$ would be isomorphisms between $R_S$ and $R_\overline{S}$, but it doens't seem to be working.
Comment: Anyone interested in the one-line-proof with the Yoneda Lemma?
Comment: @MartinBrandenburg I'm sorry, I never saw your comment. I would like to see it if you post it.
Answer: Since $\bar f$ maps the elements of $S$ to invertible elements, it factorizes uniquely through $f$ by the universal property of the latter. On the other hand, since every element of $\bar S$ is mapped by $f$ to an invertible element of $R_S$, $f$ factors uniquely through $\bar f$.
You can check that the two compositions of the maps which establish these factorings are identities, using again the universal properties.
Comment: $f:R\to R_S$ is a map which maps the elements of $S$ to invertible elements, so there exists exactly one morphism $u:R_S\to R_S$ such that $f=uf$. But $u=\mathrm{id}$ works and $\bar hh$ also works, so...
Comment: Thanks Mariano. So I know there exist unique maps $h$ and $\bar{h}$ such that $\bar{f}=hf$ and $f=\bar{h}\bar{f}$. How do you use universal properties to show they are isomorphisms? I played around and found $\bar{f}=h\bar{h}\bar{f}$ and $f=\bar{h}hf$.
Comment: Well, if $f=\bar h h f$ then $\bar hh$ must be the identity.
Comment: Why doesn't it only imply that $\bar{h}h$ is the identity on the image of $f$?
Comment: Oh I see now. You apply the property to $f$ itself to get that unique $u$...sorry, thanks.
Comment: Don't be sorry!
Comment: All those people who still think that it's a good idea to repeat the Yoneda Lemma in 100 special cases instead of proving it in the lectures should be sorry.
Comment: @MartinBrandenburg, I am of the opposite opinion. One should only learn the abstract-nonsensy version of things at the point where one can, in all honstly, say «duh». Category theory should only be learned when one already knows it.
Comment: (I know too many people you can talk on cue for 30 minutes about generalities for which they cannot name an example... Mr. Miyagi would have made a great algebra professor)
Answer: As Brian writes in his handout, use the universal property: on the one hand, $\overline{S}$ has invertible image in $R_S$, by definition.
Now given any morphism $R \to R'$ for which the elements of $\overline{S}$ become
 invertible in $R'$,
we see in particular that the elements of $S$ become invertible in $R'$.  Thus (by the universal property) we get a uniquely determined morphism $R_S \to R'$.
Thus $R_S$ satisfies the same universal property as $R_{\overline{S}}$, and so
they are canonically isomorphic.
(Note that Mariano's proof is just a proof of this canonical isomorphism adapted to the case at hand.)


xpath with node(), how to express `node()[.//x]` condition?

Question: I have a XPath that must match text and tags, except the tag <aa>; so,
./node()[name()!='aa']
is the correct xpath.
But it is insufficient for cases where tag aa is into the node, I need something like,
./node()[name()!='aa' and not(.//aa)]
but this xpath not works (!).

NOTE
I used
./*[not(self::aa or .//aa)] | ./text()
but it lost the original sequence order of the nodes. This problem is more evident when working with XSLT, example:
     <xsl:for-each select="./*[not(self::aa or .//aa)] | ./text()">
            <xsl:copy-of select="."/>
     <xsl:for-each>

not works as expected (the order of nodes is not  ensured).   When using ./node() the order is always correct.
PS: with XSLT we have a solution using all the explained xpaths,
 <xsl:for-each select="./node()[name()!='aa']">
        <xsl:if test="not(.//aa)"><xsl:copy-of select="."/><xsl:if>
 <xsl:for-each>

but the ideal/simplest one not works with the same result (when processing big and complex inputs),
            <xsl:copy-of select="*[not(self::aa or .//aa)] | ./text()"/>

Comment: Strange, XSLT should always process node-sets in document order. Which XSLT processor do you use?
Comment: The second expression looks good. How does it fail exactly?
Comment: I edited, adding an XSLT example.
Comment: Hum.. Thanks, well, I am using [XMLlib2](http://www.xmlsoft.org/), with PHP, for both [XPath](http://www.php.net/manual/en/domxpath.query.php) and [XSLT](http://php.net/manual/en/class.xsltprocessor.php)... Ok, my next "homework": get an concrete example, simplify and post here.
Answer: I'm imagining your file looks like:
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <aa/>

    <aa/>

  <c>

      <aa/>

  </c>
  <d/>
  <e>

  </e>
</root>

Then the expression
//node()[not(descendant-or-self::aa)]

returns all nodes (including the whitespace text nodes) that are not themselves an <aa> element or have an <aa> descendant.  Children of <aa> are matched as well.
You'll probably want to do something like
<xsl:copy-of select="node()[not(descendant-or-self::aa)]"/>

Comment: YES, it is XPath-v1 and works fine!  ... Well, now that my software is working and is optimized, a curiosity.  I not understand WHY `not(descendant-or-self::aa)` works and `not(self::aa or .//aa)` not works, you have an theoretical explanation for this syntatic decision of the xpath language?
Comment: I don't see a difference in meaning. On my test document, both expressions return the same nodes.
Comment: Ops, sorry, re-writing, `"./node()[name()!='aa' and not(.//aa)]"` vs `"./node()[not(descendant-or-self::aa)]"` ... Both returns the same result? (if return I must delete the question?)


uninitialized constant Mercury

Question: I can't get Mercury set up.
I did the steps in the ReadMe, installed the gem and generated the Mercury assets.
Nothing happens when I access the /editor path though, and, when I run rake routes, I get this error:
uninitialized constant Mercury

I'm on Rails 3.2.11 and Ruby 1.9.3, running on a Mongoid server.
Did I overlook something obvious or is it an error in the gem?
Comment: did you run bundle after installing? how did you install it? is it required in your gemfile?
Comment: I most definitely did. What is the usual reason that "uninitialized constant" error occurs?
Answer: Here's what I did to make it work. Not totally sure why it worked though:
Seems like I have to require mercury in my application.rb:
require 'mercury/rails'

Do you think this is how it should be done or is my mercury gem acting funny?
Comment: It is as if there is a `require: false` following the gem name in the Gemfile.  Is this the case?

uninitialized constant Mercury


Applications of ARCH models

Question: As self-study, I'm interested in learning about ARCH models and its extensions/variations, and I've been looking for journal-published articles about its possible real-life modeling and applications to get a hang of it. However, I've found little-to no articles that use linear univariate ARCH (q) models for their estimation (I've found a few regarding GARCH, ARCH-M, etc.)
Does anyone know (or can anyone recommend) some interesting journal articles that apply exclusively this model on real-life data (finance, economics, politics, society, or other subject), and that are easy to read and understand for a beginner like myself?
Answer: At least, we have the original paper of Engle (1982, Autoregressive Conditional Heteroskedasticity with Estimates of the Variance of United Kingdom Inflation), which includes an application that looks at the variance of UK inflation.
The ARCH(p) model, but more often a GARCH(1,1) model, is used as a benchmark in many papers that try to extend the original ARCH and GARCH framework. See e.g. the GARCH paper of Bollerslev (1986, Generalized autoregressive conditional heteroskedasticity).
However, you are right in the statement that ARCH(p) models rarely are applied as the only model. The GARCH(1,1) model is often considered a good benchmark model - the title of the this paper may explain why (2005, A forecast comparison of volatility models: does anything beat a GARCH(1,1)?).
Comment: @RichardHardy Hope this is ok - is there a better way of doing it?
Answer: Introduction to (Generalized) Autoregressive
Conditional Heteroskedasticity Models in Time Series
Econometrics (Wong, 2014) is a good introduction to ARCH/GARCH models.

Applications of ARCH models


PHP - Obter diferentes dados da base de dados do mesmo botão

Question: Bom o problema é o seguinte:
Eu tenho o seguinte código HTML/PHP que me permite ir buscar os últimos quatros dados inseridos na base de dados de forma a mostrar os últimos adicionados:
$getLastFour = mysqli_query($dbc,"Select * From products Order By id_product DESC LIMIT 4") or die(mysqli_error($dbc));

while($row = mysqli_fetch_array($getLastFour)){
        $idProd = $row["id_product"];
        $name = $row["name_Product"];
        $price = $row["prod_price"];
        $description = $row["prod_description"];

?>

<?php echo $name ?>

<?php echo $price.'€' ?>

Comprar

<?php
        }
        ?>

Até aqui tudo bem funciona perfeitamente. Mas como podem ver no código ao carregar no imagem de tag name showDescr supostamente aparece uma modal que mostrará a descrição do produto.
O problema é que não posso o seguinte código da modal dentro do ciclo while senão mostrará todos os resultados ao mesmo tempo (em supbreposição) e fora mostrará somente um. O código da modal é  seguinte:

                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>

Descrição do Produto

<?php echo $description ?>

Será que tenho que dar um nome diferente à imagem que ao carregar aparecerá a modal na tag name exemplo:
name="<?php echo $idProd ?>"

Comment: Como estás a carregar a modal ? Apenas com link ou ajax ?
Comment: Com o uso do data target, bootstrap
Answer: usa um form com input hidden com value "id" na botão onde queres abrir o modal e no modal para ler os dados usa um função em PHP que te faça a leitura do id com a variável Post com MySQL query usando o id para segmentar o conteúdo que queres. eu tenho a mesmo a funcionar mas em Lavarel.
verificas a inserção com: if(isset($_POST['minha_modal'])){} e no form coloca assim : "> #
Answer: Consegui acabar por resolver o problema. Então fiz o seguinte:
No botão (que neste caso é uma imagem) para abrir a modal no atributo data-targetpara alem do nome que atribui a modal dei-lhe tambem o id proveniente do busca na base de dados. E no código da modal criei um atributo id e dei-lhe o respectivo nome para que o target funciona-se.
Código alterado:

                                <img src="images/link-ico.png" data-toggle="modal" data-target="#MyModal<?php echo $idImage; ?>" class="img-responsive"  alt=""/>

<div class="modal fade bs-example-modal-md light-box" tabindex="-1" role="dialog"
             aria-labelledby="mySmallModalLabel" aria-hidden="true" id="MyModal<?php echo $idImage;?>">

                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>

Descrição do Produto

<?php echo $description ?>

P.S: O código da modal terá que estar obrigatoriamente dentro do while (apresentado na descrição da pergunta em cima) para iterar sobre os vários id's existentes


Initialize Event machine. Thin server stopped

Question: I am using Websocket Rails. On my local server it works correct(in production and development env). But when I deploy my app to remote server, I have an error when I have tried to connect:
RuntimeError (eventmachine not initialized: evma_install_oneshot_timer):

I find this solve Websockets not working in my Rails app when I run on Unicorn server, but works on a Thin server
I write to config/initializers/eventmachine.rb this code:
Thread.new { EventMachine.run } unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?

Now It working correct on remote server with passenger, but when I start it local with "rails s", the server is stopped at once:
=> Booting Thin
=> Rails 4.2.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
"Starting resque-web against Resque server - 127.0.0.1:6379"
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on localhost:3000, CTRL+C to stop
Exiting

How can I solve this problem?
My Gemfile:
source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.1'
gem 'coffee-rails', '~> 4.0.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

gem 'jbuilder', '~> 2.0'

gem 'money'
gem 'google_currency'
gem 'resque'
gem 'resque-scheduler'
gem 'resque-web', require: 'resque_web'
gem 'unread'
gem 'geocoder'
gem 'i18n_data'
gem 'yandex-translator'

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring

group :development do
  gem 'capistrano', '~> 3.0.1'
  gem 'capistrano-bundler'
  gem 'capistrano-rails', '~> 1.1.0'
  gem 'capistrano-rails-console'
  gem 'capistrano-rvm', '~> 0.1.1'

  gem 'spring'
  gem "awesome_print"
  gem 'pry'
  gem 'pry-remote'
  gem 'pry-rails'
  gem 'pry-stack_explorer'
  gem 'pry-byebug'
  gem 'quiet_assets'
  gem 'bullet'
end

gem 'factory_girl_rails'
gem 'faker'

gem 'pg'
gem 'redis'
gem 'redis-rails'
gem 'hiredis'
gem 'easy_translate'

gem 'russian'
gem "i18n-js"
gem 'slim'
gem 'slim-rails'
gem 'therubyracer'
gem 'less-rails'
gem 'rmagick'
gem 'carrierwave'
gem 'devise'
gem 'gon'
gem 'rmagick'
gem 'carrierwave'
gem "squeel"
gem 'mysql2',          '~> 0.3.13', :platform => :ruby
gem 'thinking-sphinx', '~> 3.1.0'
gem "websocket-rails"
gem 'aasm'

Comment: Yes, websocket_rails.pid
Comment: show me your gemfile.
Answer: Make sure that you have opened port 6379 in the remote server


DESAFIO: algoritmo(s) de spline com continuidade de derivada(s)

Question: Sabe-se que um polinômio fn:x->y=c0+x*c1+x²*c2+x³*c3+...+x^n*cn pode atender a N=n+1 condições e a partir de N=2 há conveniência para usar em fórmulas de spline, como por exemplo f1 pode atender às condições f1(x0)=y0 e f1(x1)=y1, f2 pode atender às condições f2(x0)=y0, f2(x1)=y1 e mais uma, f3 pode atender às condições f3(x0)=y0, f3(x1)=y1 e outras duas, daí em diante, fn(x0)=y0 com fn(x1)=y1 e mais N-2 adicionais.
Pergunta
Podemos fazer isso para, a partir de uma sequência associada de M=m+1 argumentos e resultados [x0,x1,...,xm]->[y0,y1,...,ym] e um grau n de polinômio com uma fórmula fn, criar splines com m trechos de xi a xf (x0...x1, x1...x2, ..., x(m-1)...xm) atendendo às duas condições fn(xi)=yi e fn(xf)=yf e ainda adicionalmente as condições de em cada uma das D=m-1 divisória de trechos xd (x1, x2, ..., x(m-1)) ter-se continuidade de derivadas de ordens 1,2,...,N-2 no spline?
Imagino que resulte em um sistema de equações lineares. Mas seria possível resolver não em complexidade cúbica mas sim quadrática, talvez linear? Como fazer isso? Há algoritmos conhecidos voltados especificamente para coisas como essa?
Comment: sim é possível em qualquer uma das complexidades que vc falou, como fazer ?  (é algum exercício de faculdade ou curso? se eu resolver por vc vai perder a graça de vc estudar e entender certo ?), onde usamos isso ? em interpolações lineares, cúbicas ou quadrática... imagine um áudio sampleado em 44100Hz, podemos interpolar up ou down usando essas fórmulas ai, o mesmo em imagens, podemos reduzir ou aumentar uma imagem interpolando, cada uma dessas complexidades (cúbica, quadrática ou linear), vai te dar um melhor resultado ou não em sua interpolação, a interpolação linear é a mais simples
Comment: Não é exercício de faculdade, é uma matéria de estudo de interesse despertado ao programar uma modelagem matemática para mecânica de atributos de jogo digital. Spline é muito útil em vários campos que envolvem jogos digitais, desde o sample até os diversos estados, inclusive de animação.
Comment: Não sei se você percebeu, mas o título é superficial e o texto que explica bem a pergunta é muito específico. Se refere à busca de exatamente um spline que com um polinômio de grau "n" tenha continuidade em todas as derivadas até ordem "n-1". Se der pra fazer isso com complexidade linear, com certeza vou ficar bastante surpreso, será demais!
Comment: Em áudio usamos muito o spline para fazer resample/interpolação, para fazer um downsample de áudio de modo geral dá para aplicar um filtro passa baixa seguido de uma interpolação Spline, isso vai evitar aliasing no áudio x->y ... dá para fazer o mesmo usando complexidade linear e acredite se quiser em áudio até que funciona bem e é simples de mais, é ótimo de se fazer para testar algum conceito antes de aplicar uma interpolação cúbica ...
Comment: Entendo. O spline não tem só relevância sensorial, de sons a movimentos. Também tem no campo da matemática. As vezes encontramos fórmulas muito pesadas de se processar num algoritmo para fazer a análise, aí a rápida construção de aproximações razoáveis pode depois reduzir custo de processamento e tornar a busca de fórmulas e tabelas mais praticável. Essas fórmulas podem precisar de algumas condições e no meu caso eu uso muito a primeira ordem de derivada e as vezes considero a segunda.
Answer: Não conheço algoritmo de spline amplamente estudado com esse propósito, portanto tenho a intenção de desenvolver um novo aos poucos.
O que me motivou a perguntar, resolver e ainda registrar aqui no stackoverflow foi eu não conseguir encontrar nem criar um algoritmo eficiente sem estudo aprofundado como esse. De fato, precisei disso porque tive problemas de desempenho por trabalhar com problemas muito grandes com enorme complexidade de operações dos algoritmos. Agora o resultado ficará aqui registrado para quando precisarmos revisar.
Modelando o problema
Considero que o algoritmo tem os parâmetros M ou m, N ou n, x[0, 1, ..., m] e y[0, 1, ..., m], ou seja, define-se número M de associações xi->yi, especifica cada uma e o grau desejado do polinômio por intervalo da spline.
Para começar, já que:

fórmulas fm isoladas atendem a M condições (como fm(x0)=y0, fm(x1)=y1, ..., fm(xm)=ym) conservando continuidade em derivadas de todas as ordens e
polinômios fm com m<=n na forma descrita no enunciado de ordens estão dentro do modelo fn ao considerar zero os coeficientes c(m+1), c(m+2), ..., cn (de graus m+1, m+2, ..., n),

então uma só fórmula fm para todo o domínio atende aos requisitos desejados se m<=n. Podemos nos focar então em problemas onde m<n, que uma só não atende a todas as condições de associação da spline.
Já que cada trecho do spline se limita a argumentos entre dois valores vizinhos de x, o algoritmo desejado atende às seguintes condições.

Tem N=n+1 coeficientes na fórmula f(x) = polinômio de grau n (não necessariamente o fn do enunciado da questão, deve ser possível resolver os sistemas de equações).
Resultado é uma função condicional (spline) que associa cada um de m intervalos (X0=[x0,x1], X1=[x1,x2], ..., X(m-1)=[x(m-1),xm], portanto F=m-1 fronteiras x1, x2, ..., x(m-1)) a uma fórmula com modelo f e coeficientes c já calculados.
Spline tem M=m+1 associações de argumentos e valores de spline (x0->y0, x1->y1, ..., xm->ym) respeitados pelas associações de intervalos às fórmulas com coeficientes adequados.
Spline tem pelo menos até ordem D=n-1 de derivadas contínuas (f, f', f'', ..., f^(D)) mesmo nas fronteiras.
Portanto considera-se {D,F,m,M,n,N} c {Inteiros}, sequência x crescente, 0 <= D < n < N <= m < M e F < m.

Leve em conta que não necessariamente deverá se restringir os cálculos ao intervalo [x0,xm], portanto mesmo durante a construção do algoritmo o primeiro intervalo sendo [x0,x1] e o último [x(m-1),xm] na prática o primeiro intevalo do spline final é [-inf,x1] e o último é [x(m-1),+inf].
Reduzindo o problema: pontos iniciais e finais de intervalos
Sendo assim, para cada intervalo Xi = [xi,x(i+1)] = [xi,xf] podemos calcular os N=n+1 coeficientes de f específicos do intervalo. Respeitando as condições f(xi)=yi e f(xf) = y(i+1) = yf, encontramos dois em função dos outros, mas restarão outros n-1 coeficientes a serem calculados em cada trecho.
Se por conveniência escrevermos o polinômio numa forma incomum, f(x) = c0*(xf-x) + (x-xi)*( c1 + (xf-x)*( c2 + c3*x + c4*x² + c5*x³ + ... + cn*x^(n-2) ) ) (por mais confuso que pareça), e resolvermos c0 e c1 com essas duas condições significa a solução do sistema linear { f(xi)=yi , f(x(i+1))=y(i+1) } ser simplesmente { c0=yi/(xf-xi) , c1=yf/(xf-xi) }.
Com isso, o problema se resume em a cada intervalo Xi achar esses n-1 coeficientes c2, c3, ..., cn de f(x) = yi*((xf-x)/(xf-xi)) + (x-xi)*( yf/(xf-xi) + (xf-x)*( c2 + c3*x + c4*x² + c5*x³ + ... + cn*x^(n-2) ) ) respeitando o critério das fronteiras de trechos mantiverem a continuidade de derivadas nas fronteiras.
Reduzindo o problema: modelando o problema das fronteiras
Se há m intervalos (com D=n-1 coeficientes cada a serem calculados), então há F=m-1 fronteiras para ajustar continuidades de D=n-1 derivadas, portanto D*m-D*F coeficientes restantes a serem solucionados, ou seja, restarão D coeficientes, o equivalente aos de um intervalo (como se um ficasse de fora).
Precisaremos adaptar o problema a essa realidade. Uma maneira de fazer isso é criar mais um conjunto de equações que façam o papel de uma fronteira adicional para "preencher as condições do intervalo restante". Outra maneira é substituir os D*m coeficientes por outros valores que correspondam a D*F coeficientes.
Além disso, cada intervalo tem fórmula f com coeficientes de valores distintos das outras e precisamos diferenciá-los. Para isso, cada intervalo Xi terá a fórmula fi com coeficientes ci2, ci3, ..., cin.
Decidi reduzir o número de coeficientes. O modo de redução que escolhi foi para cada uma das D=n-1 ordens o=2,3,...,n substituir os coeficientes c por k de uma das duas seguintes maneiras.

{ c0o=k0o+k1o , c1o=k1o+k2o , ... , c(m-1)o=k(m-1)o+kmo }.
{ c0o=k0o-k1o , c1o=k1p-k2o , ... , c(m-1)o=k(m-1)o-kmo }.

Sendo que em ambas opções considera-se k0o = kmo = 0, restando k1o, k2o, ..., k(m-1)o indefinidos, resultando em F coeficientes por ordem o e assim totalizando F*D. Sendo assim, as fórmulas fi seguem um dos modelos seguintes.

fi(x) = yi*(x(i+1)-x)/(x(i+1)-xi) + (x-xi)*( y(i+1)/(x(i+1)-xi) + (x(i+1)-x)*( (ki2+k(i+1)2) + (ki3+k(i+1)3)*x + (ki4+k(i+1)4)*x² + (ki5+k(i+1)5)*x³ + ... + (kin+k(i+1)n)*x^(n-2) ) ).
fi(x) = yi*((x(i+1)-x)/(x(i+1)-xi)) + (x-xi)*( y(i+1)/(x(i+1)-xi) + (x(i+1)-x)*( (ki2-k(i+1)2) + (ki3-k(i+1)3)*x + (ki4-k(i+1)4)*x² + (ki5-k(i+1)5)*x³ + ... + (kin-k(i+1)n)*x^(n-2) ) ).

Com isso, são F=m-1 fronteiras x1, x2, ..., xF e F*D=(m-1)*(n-1) coeficientes k. Para cada fronteira, criar D equações para igualar as derivadas de ordem 1, 2, ..., D leva a um sistema de F*D equações lineares, correspondendo ao número de coeficientes.
Para facilitar, as fórmulas foram implementadas no maple usando o número M de associações de argumentos e resultados, a ordem polinomial n, o índice i do intervalo e o fator kf = ±1 do segundo coeficiente k que compõe coeficiente c (já zerando coeficientes k nulos dos extremos).

Por exemplo, quando M=5 e n=3 as duas fórmulas de fi para cada um dos n=4 intervalos (i=0,1,2,3) são as que estão logo abaixo.

Reduzindo o problema: montando o sistema
Sendo assim, para cada fronteira xi e { x1, x2, ..., xF } tem-se as equações do sistema f(i-1)'(xi) = fi'(xi), f(i-1)''(xi) = fi''(xi), ..., f(i-1)^(D)(xi) = fi^(D)(xi). Para facilitar, foi implementado um procedimento que forma para cada fronteira as equações já com as variáveis e coeficientes reunidos no lado esquedo da equação e os termos independentes a direita, ou seja, o sistema de equações lineares da forma mais legível que pode (ainda que nem tanto quando o problema resolvido é grande) e exibe "fronteira, ordem de derivada, equação correspondente". Lembrando que D = n-1 e F = m-1 = M-2.

Por exemplo, os dois sistemas no exemplo anterior com M=5 e n=3 são exibidos da forma a seguir.

Reduzindo o problema: entendendo o sistema
Alterando o procedimento, reparamos que a representação dos sistemas em forma de matriz parece ser de uma matriz esparsa (preenchida com muitos zeros) segundo algum padrão. No exemplo, as matrizes ficam assim se as colunas se associarem às variáveis na sequência ki_o e { k1_2, k1_3, k2_2, k2_3, k3_2, k3_3 }.

Verificando se cada coeficiente é zero ou outra expressão, podemos observar a esparsidade de matrizes e isso é mais facilmente feito se for exibido na célula ao invés da expressão algum marcador curto que indica se é zero ou não. Alterando o procedimento de modo que isso seja aplicado, observamos algo parecido com o que é apresentado a seguir.

Assim pode-se observar matrizes bem maiores e possivelmente entender o padrão. Além disso, lembremos que cada equação iguala ou subtrai duas fórmulas (uma do intervalo a esquerda da fronteira e a outra da direita), cada uma com até 2*D coeficientes e, ainda assim, metade deles são em comum com a outra, totalizando até 3*D coeficientes k tratadas como variáveis na equação do sistema.
Como será então a matriz nessas condições? Como será quando M=9 e n=2? Será que encontramos a cada linha no máximo três (3*D = 3*(n-1) = 3) céluas não nulas e próximas?

Hmm... aparentemente, sim. E se M=9 e n=3? No máximo seis células não nulas por linha?

E se M=9 e n=4?

Aparentemente, as matrizes somente têm células a cada linha que sejam não nulas numa distância menor que 3*D células a partir da que está na diagonal principal. Pode-se aplicar algoritmos genéricos como o uso de matrizes inversas ou obtenção de forma escalonada reduzida, mas a complexidade de operações é cúbica em relação ao parâmetro F e adaptar um algoritmo que evita trabalhar com todas as células diminui a complexidade em relação a F, podendo assim trabalhar muito melhor com grande número de associação de argumentos e valores de spline principalmente em baixas ordens de polinômios.
Resolvendo o problema
Percebe-se que há F "blocos" de D linhas onde por fora não é esparso. A razão disso é o fato de cada bloco corresponder com uma fronteira, que tem D linhas e trabalha com 2*D ou 3*D coeficientes. O primeiro e o último bloco têm 2*D colunas e os outros têm 3*D.
Aparentemente, é possível separar os blocos como se fossem sistemas distintos. Seria como resolver os sistemas encontrando os valores das variáveis associadas às diagonais principais da matriz inteira, cada uma em função das outras variáveis que se incluem no sistema do bloco. Isso significa 2 sistemas de D equações e 2*D variáveis e F-2 sistemas de D equações e 3*D variáveis, resultando na complexidade F*D³.
Além disso, as D variáveis obtidas de um sistema podem ser usados no próximo para reduzir D colunas (operações de complexidade D² ocorrendo em todos os blocos diferentes do primeiro, logo complexidade F*D²), significando F-1 sistemas de D equações e 2*D variáveis e 1 sistema (o último) de D equações e 3*D variáveis, conservando a complexidade F*D³ mas ainda assim melhorando o desempenho.
Como o último sistema é DxD, espera-se que se obtenha os valores das últimas variáveis. Como os outros são Dx(2*D) e têm valores das variáveis do próximo, é possível obter um sistema DxD delas com operações D² por bloco de maneira similar ao passo anterior.
Isso corresponde com o escalonamento da matriz para a forma escalonada reduzida da matriz onde as operações de linha se restringem às linhas do mesmo bloco e do próximo na primeira etapa e às linhas do mesmo bloco e do anterior na segunda etapa. Essa simples adaptação resolve e reduz a complexidade de (F*D)³ para F*D³.
Com o sistema resolvido, tem-se os coeficientes k, com isso tem-se coeficientes c e finalmente tem-se cada fórmula de cada intervalo com coeficientes calculados, assim obtendo a spline em complexidade F*D³ atendendo às condições de continuidade e associação de argumentos e valores de spline.
Implementando e testando
Usando o maple, pude implementar uma função completa de spline usando kf=1 e testar plotando o gráfico de spline (e suas derivadas) construído com n=3, x=1,3,5,7,9 e y=1,1,2,4,6. O nome escolhido do argumento do spline foi arg. Atente-se ao fato dos nomes de incógnitas estarem diferentes e a abordagem de solução de sistemas escolhida foi simbólica e não matricial.

Observa-se não só neste exemplo mas em outros testes que fiz que os valores estão corretos e as derivadas que devem ser contínuas de fato são. O resultado foi satisfatório. Além disso, fiz uma modificação no algoritmo (passei a tratar k0o = z e usei esse parâmetro z para suavizar a curva) e refiz os mesmos testes. Veja o exemplo anterior com o novo algoritmo.

A suavização ocorre encontrando o z que equilibra os valores absolutos das derivadas de ordem n do primeiro intervalo e do último. Como resultado, encontramos uma nova curva que parece até mais realista que a anterior. Enquanto não encontro problemas com este algoritmo, considero-o satisfatório.