Linux daylight savings notification

Question: I was trying to find a way to receive a notification from the system (Linux) when daylight savings are applied, but I do not seem to be able to find anything like that.
Consider a program sits on a pselect() waiting for a number of timer fd's, all which have exactly 24-hour intervals, but differing start times, which are defined by a user; "07:00 ON, 07:25 OFF" (for example, if it were a coffee maker).
Because the user gives these times in local time and Linux runs on UTC, the time zone adjusted timer fd's need to be readjusted each time a daylight savings occure. (user expects coffee when his daylight savings compliant alarm clock has woken him up...)
Intelligent way to go about this, as I would imagine, would be to register to the system/kernel/init/whatever to be notified when daylight savings are applied, and avoid getting into the messy business of trying to determine such dates and times yourself and hope the system agrees with your results (ie. your resync actions and actual daylight savings happen in the same time).
Is there any way to be notified on DST changes? Or perhaps on any changes to local time (assuming DST change modifies that)?
Answer:
Consider a program sits on a pselect() waiting for a number of timer fd's, all which have exactly 24-hour intervals, but differing start times

Therein lies your fundamental problem. All days are not exactly 24 hours long -- sometimes they are off by an hour (daylight savings time), or by seconds (leap seconds); just like not every February has 28 days.
A much simpler and lightweight (less resources consumed) way is to use a min-heap of future events in UTC, something like
struct trigger {
    /* Details on how the event is defined;
       for example, "each day at 07:00 local time".
    */
};

struct utc_event {
    struct trigger  *trigger;
    time_t           when;
};

struct event_min_heap {
    size_t           max_events;
    size_t           num_events;
    struct utc_event event[];
};

The event C99 flexible array member in struct event_min_heap is an array with num_events events (memory allocated for max_events; can be reallocated if more events are needed) in a min heap keyed by the when field in each event entry. That is, the earliest event is always at the root.
Whenever current time is at least event[0].when, it is "triggered" -- meaning whatever action is to be taken, is taken --, and based on the struct trigger it refers to, the time of the next occurrence of that event is updated to event[0], then it is percolated down in the heap to its proper place. Note that you simply use mktime() to obtain the UTC time from broken-down local time fields.
(If this were a multi-user service, then you can support multiple concurrent timezones, one for each trigger, by setting the TZ environment variable to the respective timezone definition, and calling tzset() before the call to mktime(). Because the environment is shared by all threads in the process, you would need to ensure only one thread does this at a time, if you have a multithreaded process. Normally, stuff like this is perfectly implementable using a single-threaded process.)
When the event in the root (event[0]) is deleted or percolated (sifted), the event with the next smallest when will be at the root. If when is equal or less to current time in UTC, it too is triggered.
When the next when is in the future, the process can sleep the remaining interval.
That is all there is to it. You don't need multiple timers -- which are a system-wide finite resource --, and you don't need to worry about whether some local time is daylight savings time or not; the C library mktime() will take care of such details for you.

Now, if you don't like this approach (which, again, uses fewer resources than the approach you outlined in your question), contact the SystemD developers. If you kiss up to them obsequiously enough, I'm sure they'll provide a dbus signal for you. It's not like there is any sanity in its current design, and one more wart certainly won't make it any worse. Switching to C# is likely to be considered a plus.

It is crucial to understand that mktime() computes the Unix Epoch time (time_t) for the specified moment, applying daylight savings time if it applies at that specific moment. It does not matter whether daylight savings time is in effect when the function is called!
Also, UTC time is Coordinated Universal Time, and is not subject to timezones or daylight savings time.
Consider the following program, mktime-example.c:
#define  _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>

static time_t epoch(struct tm *const tm,
                    const int year, const int month, const int day,
                    const int hour, const int minute, const int second,
                    const int isdst)
{
    struct tm  temp;
    time_t     result;

    memset(&temp, 0, sizeof temp);
    temp.tm_year = year - 1900;
    temp.tm_mon = month - 1;
    temp.tm_mday = day;
    temp.tm_hour = hour;
    temp.tm_min = minute;
    temp.tm_sec = second;
    temp.tm_isdst = isdst;

    result = mktime(&temp);

    if (isdst >= 0 && isdst != temp.tm_isdst) {
        /* The caller is mistaken about DST, and mktime()
         * adjusted the time. We readjust it. */
        temp.tm_year = year - 1900;
        temp.tm_mon = month - 1;
        temp.tm_mday = day;
        temp.tm_hour = hour;
        temp.tm_min = minute;
        temp.tm_sec = second;
        /* Note: tmp.tm_isdst is kept unchanged. */

        result = mktime(&temp);
    }

    if (tm)
        memcpy(tm, &temp, sizeof temp);

    return result;
}

static void show(const time_t t, const struct tm *const tm)
{
    printf("(time_t)%lld = %04d-%02d-%02d %02d:%02d:%02d",
           (long long)t, tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
           tm->tm_hour, tm->tm_min, tm->tm_sec);

    if (tm->tm_isdst == 1)
        printf(", DST in effect");
    else
    if (tm->tm_isdst == 0)
        printf(", DST not in effect");
    else
    if (tm->tm_isdst == -1)
        printf(", Unknown if DST in effect");

    if (tzname[0] && tzname[0][0])
        printf(", %s timezone", tzname[0]);

    printf("\n");
    fflush(stdout);
}

int main(int argc, char *argv[])
{
    struct tm  tm;
    time_t     t;
    long long  secs;
    int        arg, year, month, day, hour, min, sec, isdst, n;
    char       ch;

    if (argc < 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
        fprintf(stderr, "Usage: %s [ -h | --help ]\n", argv[0]);
        fprintf(stderr, "       %s [ :REGION/CITY | =TIMEZONE ] @EPOCH | YYYYMMDD-HHMMSS[+-] ...\n", argv[0]);
        fprintf(stderr, "Where:\n");
        fprintf(stderr, "       EPOCH is in UTC seconds since 19700101T000000,\n");
        fprintf(stderr, "       + after time indicates you prefer daylight savings time,\n");
        fprintf(stderr, "       - after time indicates you prefer standard time.\n");
        fprintf(stderr, "\n");
        return EXIT_FAILURE;
    }

    for (arg = 1; arg < argc; arg++) {

        if (argv[arg][0] == ':') {
            if (argv[arg][1])
                setenv("TZ", argv[arg], 1);
            else
                unsetenv("TZ");
            tzset();
            continue;
        }

        if (argv[arg][0] == '=') {
            if (argv[arg][1])
                setenv("TZ", argv[arg] + 1, 1);
            else
                unsetenv("TZ");
            tzset();
            continue;
        }

        if (argv[arg][0] == '@') {
            if (sscanf(argv[arg] + 1, " %lld %c", &secs, &ch) == 1) {
                t = (time_t)secs;
                if (localtime_r(&t, &tm)) {
                    show(t, &tm);
                    continue;
                }
            }
        }

        n = sscanf(argv[arg], " %04d %02d %02d %*[-Tt] %02d %02d %02d %c",
                              &year, &month, &day, &hour, &min, &sec, &ch);
        if (n >= 6) {
            if (n == 6)
                isdst = -1;
            else
            if (ch == '+')
                isdst = +1; /* DST */
            else
            if (ch == '-')
                isdst = 0;  /* Not DST */
            else
                isdst = -1;

            t = epoch(&tm, year, month, day, hour, min, sec, isdst);
            if (t != (time_t)-1) {
                show(t, &tm);
                continue;
            }
        }

        fflush(stdout);
        fprintf(stderr, "%s: Cannot parse parameter.\n", argv[arg]);
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

Compile it using e.g.
gcc -Wall -O2 mktime-example.c -o mktime-example

Run it without arguments to see the command-line usage. Run
./mktime-example :Europe/Helsinki 20161030-035959+ 20161030-030000- 20161030-030000+ 20161030-035959- 20161030-040000-

to examine the Unix timestamps around the time when DST ends in 2016 in Helsinki, Finland. The command will output
(time_t)1477789199 = 2016-10-30 03:59:59, DST in effect, EET timezone
(time_t)1477789200 = 2016-10-30 03:00:00, DST not in effect, EET timezone
(time_t)1477785600 = 2016-10-30 03:00:00, DST in effect, EET timezone
(time_t)1477792799 = 2016-10-30 03:59:59, DST not in effect, EET timezone
(time_t)1477792800 = 2016-10-30 04:00:00, DST not in effect, EET timezone

The output will be the same regardless of whether at the time of running this DST is in effect in some timezone or not!
When calling mktime() with .tm_isdst = 0 or .tm_isdst = 1, and mktime() changes it, it also changes the time specified (by the daylight savings time). When .tm_isdst = -1, it means caller is unaware of whether DST is applied or not, and the library will find out; but if there is both a valid standard time and DST time, the C library will pick one (you should assume it does so randomly). The epoch() function above corrects for this when necessary, un-adjusting the time if the user is not correct about DST.
Comment: If I understood fully, the main gist is to reschedule after each event expiration, but just one (the next to trigger) in order to conserve timers. Rescheduling would keep events in effective local time and at worst case I would be looking at 23-25h periods, twice a year, when an event does not trigger at a correct time. (case when running just one event triggering 23:59 and getting rescheduled that one minute before local time jumps DST). And if I would know for sure that DST jumps always, regardless of TZ, at midnight, I could do a daily check and readjust if I find `.tm_gmtoff` changed.
Comment: @Tammi: **No!** Whenever an event triggers, you use `mktime()` to find out the UTC time of the next time that event should trigger. The C library timezone handling is intelligent enough to determine that time in UTC, including whether daylight savings time applies. *All events will trigger at their correct times.* I shall try to add a practical example in my answer.
Comment: @Tammi: In POSIXy systems, including Linux and Mac OS, daylight savings time is not some global flag that is turned on and off by some operating system service. It is a timezone property. The C library `mktime()` function takes broken-down local time fields, and converts it to UTC (Epoch), applying the current timezone rules, including DST *for the target time*. It does not matter whether DST applies at the calling time. When you use UTC timestamps, "DST jumps" are irrelevant: *we prepare for them beforehand*, so there really are no "jumps" at all.
Comment: I think I learned a valuable lesson here (though it escaped me the first time around) With `mktime()`'s capabilities (unrecognized by me until now) there is indeed no need for DST notifications (nor rescheduling). This is **better** than I imagined. Thank you very much all the time you took to help and great example code.
Comment: It later occured to me that "better than I imagined" might be understood incorrectly - I mean that @nominal-animal 's solution is much better than the best possible solution I was looking for or expecting to find. :-)
Comment: @Tammi, timezone adjustments for daylight savings apply on some regions and not in others, some regions (mainly in the past) had even four corrections per year (first one hour, then two)  as it depends on how far is the region from the equator.  Some countries apply the daylight correction in some years and not in others. look at `zic(1)` command to get some idea on how local time is calculated.
Comment: @Tammi: Excellent to hear. `mktime()` and related functions often trip people up -- the interface is unintuitive, even if it is very useful. Experimenting with a test program like mine above helps one build up a complete picture of the functionality. The hard part is solving the ambiquities (*"what does the user actually mean by that"*) near DST changes; that's what the extra `+` and `-` are for after a timestamp in my example program. We should always find an user interface that works, then worry about the code...
Comment: @Luis Colorado, yes you're absolutely correct. Messy business that most would prefer to somehow avoid in their own projects. I am very happy that `mktime()` (and related) shield me from all that. Now that I have updated my small personal project accordingly, I feel pretty good about how it ended up. And even more so having learned something very useful.
Answer: Unix/linux systems only deal with UTC, and they use the time_t data (the number of seconds since 00:00h jan, 1st of 1970 UTC till now) as the internal time.  Conversions to local time (with the complexities due to exceptions, variations for summer-winter periods, etc.) is done only when displaying the information to the user, so only on converting to local time it is done.  As said, no provision to schedule something or preparation for it is made in the unix system.
From zdump(1) you can get all the info you want, per timezone, and use it to construct a crontab to notify you when the switch is to be made.  It consults the local database of timezones and extracts all the info about switching (including historic) from winter to summer or the reverse.
$ zdump -v Europe/Madrid
Europe/Madrid  Fri Dec 13 20:45:52 1901 UTC = Fri Dec 13 20:45:52 1901 WET isdst=0 gmtoff=0
Europe/Madrid  Sat Dec 14 20:45:52 1901 UTC = Sat Dec 14 20:45:52 1901 WET isdst=0 gmtoff=0
Europe/Madrid  Sat May  5 22:59:59 1917 UTC = Sat May  5 22:59:59 1917 WET isdst=0 gmtoff=0
Europe/Madrid  Sat May  5 23:00:00 1917 UTC = Sun May  6 00:00:00 1917 WEST isdst=1 gmtoff=3600
Europe/Madrid  Sat Oct  6 22:59:59 1917 UTC = Sat Oct  6 23:59:59 1917 WEST isdst=1 gmtoff=3600
Europe/Madrid  Sat Oct  6 23:00:00 1917 UTC = Sat Oct  6 23:00:00 1917 WET isdst=0 gmtoff=0
Europe/Madrid  Mon Apr 15 22:59:59 1918 UTC = Mon Apr 15 22:59:59 1918 WET isdst=0 gmtoff=0
Europe/Madrid  Mon Apr 15 23:00:00 1918 UTC = Tue Apr 16 00:00:00 1918 WEST isdst=1 gmtoff=3600
Europe/Madrid  Sun Oct  6 22:59:59 1918 UTC = Sun Oct  6 23:59:59 1918 WEST isdst=1 gmtoff=3600
Europe/Madrid  Sun Oct  6 23:00:00 1918 UTC = Sun Oct  6 23:00:00 1918 WET isdst=0 gmtoff=0
Europe/Madrid  Sat Apr  5 22:59:59 1919 UTC = Sat Apr  5 22:59:59 1919 WET isdst=0 gmtoff=0
Europe/Madrid  Sat Apr  5 23:00:00 1919 UTC = Sun Apr  6 00:00:00 1919 WEST isdst=1 gmtoff=3600
Europe/Madrid  Mon Oct  6 22:59:59 1919 UTC = Mon Oct  6 23:59:59 1919 WEST isdst=1 gmtoff=3600
Europe/Madrid  Mon Oct  6 23:00:00 1919 UTC = Mon Oct  6 23:00:00 1919 WET isdst=0 gmtoff=0
Europe/Madrid  Wed Apr 16 22:59:59 1924 UTC = Wed Apr 16 22:59:59 1924 WET isdst=0 gmtoff=0
Europe/Madrid  Wed Apr 16 23:00:00 1924 UTC = Thu Apr 17 00:00:00 1924 WEST isdst=1 gmtoff=3600
Europe/Madrid  Sat Oct  4 22:59:59 1924 UTC = Sat Oct  4 23:59:59 1924 WEST isdst=1 gmtoff=3600
Europe/Madrid  Sat Oct  4 23:00:00 1924 UTC = Sat Oct  4 23:00:00 1924 WET isdst=0 gmtoff=0
Europe/Madrid  Sat Apr 17 22:59:59 1926 UTC = Sat Apr 17 22:59:59 1926 WET isdst=0 gmtoff=0
Europe/Madrid  Sat Apr 17 23:00:00 1926 UTC = Sun Apr 18 00:00:00 1926 WEST isdst=1 gmtoff=3600
Europe/Madrid  Sat Oct  2 22:59:59 1926 UTC = Sat Oct  2 23:59:59 1926 WEST isdst=1 gmtoff=3600
Europe/Madrid  Sat Oct  2 23:00:00 1926 UTC = Sat Oct  2 23:00:00 1926 WET isdst=0 gmtoff=0
Europe/Madrid  Sat Apr  9 22:59:59 1927 UTC = Sat Apr  9 22:59:59 1927 WET isdst=0 gmtoff=0
...

By the way, if you want to be advised of an imminent localtime change, you can use the previous info to construct a crontab file, including all the info, or simply construct a crontab file that include the rules that apply at your localty.  For example, if I want to be advised one day before a switch change in Spain (it changes on last sunday of march/october, at 02/03h) you can add some rules in your crontab file:
0 0 24-30 3,10 5 echo Time daylight savings change scheduled for tomorrow | mail $firstname.lastname@example.com

and a mail will be sent to you in on every saturday(5) that happens to be in the week from 24-30th of march and october (3,10 part) of each year at 00:00h (localtime).  I'm sure you'll be able to adapt this example to your localty or time of advance  (so, the day before a time change happens).
Comment: Good information to have under this question - others searching for similar information may still want an actual notification.
Comment: How to know who can be interested on this information?  A good way to know is to upvote it :)
Comment: "Unix/linux systems only deal with UTC, and .... time_t data (the number of seconds since 00:00h jan, 1st of 1970 UTC till now) as the internal time." is amiss.  [UTC considers leap seconds](https://en.wikipedia.org/wiki/Coordinated_Universal_Time#Current_number_of_leap_seconds), `time_t` rarely does that.
Comment: @chux, it should be easy to internally use TAI instead of UTC and implement the leap seconds correction in the timezone correction database (I've seen the opposite example in the tz database files) (and by that reason the TAY->UTC conversion) allowing the internal time to flow without disturbation.  But I've seen no reference to this possibility.
Comment: @LuisColorado A singular problem with UTC as `time_t` is the the list of leap seconds is historic and not suitable for far future calculations.  TAI sounds great except  civil use of time _does_ count leap seconds.  Instead I suspect UNIX `time_t, struct tm`, etc. will continue to pretend leap seconds do not exist.
Comment: TAI corrections are known with weeks/months beforehand.  Automatic schedule for corrections file is available for download beforehand.  FreeBSD at least, has automatic update (or at least warns you of due status of last file)  The thing here is not to perturbate the internal clock discipline (more on systems with precise timing demands) and make all corrections at once (leap second, local time, even solar adjustments can be made)  see [https://www.timeanddate.com/time/leapseconds.html](https://www.timeanddate.com/time/leapseconds.html) for more info.

Linux daylight savings notification


Pulando entre aplicações remotas usando o cordova (problema complexo)

Question: Olá,
Tenho 3 aplicações e que cada uma se encontra disponível para iOS e Android e gostava de criar uma app para aceder a todas elas.
Para conseguir isso preciso pegar o código de cada uma das aplicações e movê-lo para um servidor de modo a que esta quarta aplicação possa fazer um redirect entre as apps.
É importante mencionar que cada uma das aplicações precisa aceder aos controlos nativos do phonegap e como tal precisa de ter importado no source code o ficheiro cordova.js ou phonegap.js
Acontece que quando compilamos uma aplicação esse ficheiro é injectado no directório de acordo com a plataforma para a qual estamos a compilar.
Ao mover as aplicações para diferentes servidores surgem alguns problemas que se devem a estes simples requisitos funcionais:

A aplicação local permite abrir aplicações remotas
As aplicações remotas conseguem aceder aos controlos do aparelho e aos plugins nele instalados
A partir de qualquer aplicação remota deve ser possível regressar à aplicação local

Problemas:
- As minhas aplicações remotas estão alojadas no seu próprio domínio tais como a.com, b.com, c.com. Todas as aplicações comunicam com uma api que se encontra no dominio d.com. Como o administrador não pode autorizar o CORS, quando abro a aplicação no InAppBrowser as aplicações não conseguem comunicar com a API, pois o dominio da aplicação que estamos a aceder é diferente do dominio da api (ex: A.com != c.com logo => Temos CORS). Tipicamente, este problema não existe se pegarmos no código que se encontra em qualquer uma dessas aplicações: ("a.com"... "c.com") e a compilarmos porque as aplicações deixam de estar alojadas num servidor.

Posto isto, e sem resolver o problema de como ter acesso aos controlos da aplicação surge-me a ideia de colocar diferentes versões da aplicação no servidor em que cada uma delas faz host do ficheiro phonegap.js para a plataforma que fez o pedido.

Exemplo: Ao abrir a aplicação 4 a aplicação detecta que estamos a usar um android e vai criar links dinâmicos que me permitem ao projecto que tem no seu directório o phonegap.js para androids.
por outras palavras:
a.com/android <---- possui um phonegap.js para androids
a.com/ios <---- possui um phonegap.js para ios
Mesmo que esta solução funcione continuo sem saber como voltar a aplicação local.
Alguém tem ideias que possam ajudar a resolver estes problemas?
Espero não ter sido muito confuso, o problema é complexo
Answer: Realmente o problema é bem complexo, na verdade ficou até um pouco confuso.
Porém acredito que tenha atendido a sua pergunta.
Você possui diversas aplicações para iOS e Android, você deseja criar uma forma de integração entre as aplicações, por isso desenvolveu uma nova aplicação remota a fim de alternar entre as aplicações locais (ou instaladas nos dispositivos).
O primeiro ponto que devo levantar é em relação ao seguinte:

Para conseguir isso preciso pegar o código de cada uma das aplicações e movê-lo para um servidor de modo a que esta quarta aplicação possa fazer um redirect entre as apps.

Não exatamente, você deve adaptar uma aplicação (ou portar) para que ela funcione em um Servidor, para isso você pode utilizar a linguagem que desejar, o ponto é que em algum momento nesta aplicação dentro do Servidor você devera fazer chamadas as aplicações dentro dos dispositivos. Para isso você pode utilizar URI Schemas, existe até Plugins já prontos para Cordova:
https://github.com/EddyVerbruggen/Custom-URL-scheme
Cabe citar que existe muita gente que não recomenda definir URI Scheme já que vai contra os padrões, mais informações neste link, porém com isto você consegue enviar informações entre uma aplicações no servidor.
Se você está fazendo uma aplicação com o Cordova e apenas colocando os arquivos (HTML, CSS, JavaScript) em um servidor e chamando eles no Cordova, talvez você deva repensar sua estratégia, além dos inúmeros problemas que isso pode causar, como falta de compatibilidade e arquivos quebrados. O que você deve fazer é criar uma aplicação que apenas requisite os dados (ou mesmo as páginas completas) de um servidor, porém a aplicação tenha autonomia para validar/verificar os dados e informar erros (falta de conexão, arquivos quebrados).
Sobre o problema do CORS, se o administrador do servidor não autoriza CORS, a melhor solução seria trocar de servidor, existe formas de "bypass" utilizando um proxy das requisições em outro servidor com outras tecnologias, porém se o problema mesmo é o administrador não permitindo, ele provavelmente também irá bloquear qualquer outra alternativa.
Sobre sua ideia de tornar os JavaScript locais para o domínio alterando a requisição do mesmo e evitando o uso de CORS. Você realmente pode fazer o Cordova identificar a plataforma e fornecer o melhor arquivo para ela, porém não é a melhor solução, você está tornando o problema que já é complexo em algo mais complexo ainda e adicionado mais riscos de problemas com compatibilidade de versões.
Do meu ponto de vista a melhor estratégia é criar um WebService apenas para trocar as informações entre os aplicativos locais. Nada de requisições de arquivos JS ou qualquer outro formato, apenas dados. E dentro de cada aplicação aconteceria o tratamento das informações, verificações de versões dos dispositivos, chamadas para outros aplicativos e/ou telas. A fim de centralizar seus dados em um único local e manter aplicações diferentes em seus próprios escopo.
Se não entendi sua pergunta, ou me confundi em algo, por favor desconsidere.
Comment: Valeu pela resposta henrique. Irei verificar com atenção as suas sugestões e volto para dar feedback


Why are inverted prefix codes uniquely decodable?

Question: My professor stated that inverted prefix codes are always uniquely decodable but did not state the reason why?
Could anyone please explain to me why this would be the case?
Comment: See [Polish notation](https://en.wikipedia.org/wiki/Polish_notation#Evaluation_algorithm).
Answer: Lemma: if $\mathcal{C}$ is UD (uniquely decodable code) then $\mathcal{C'}$ (inverted code) is also UD.
Proof (sketch). Let $A,B,C \cdots $ be the codewords of $\mathcal{C}$,
and let $A',B',C' \cdots $ be the codewords of $\mathcal{C'}$.
Suppose that $\mathcal{C'}$ is not UD. Then there must exist some pair of sequences (of inverted codewords) that produce the same concatenation, say for example $(A',B',A',D')=(E',C',A')$. But then, inverting, we would have $(D,A,B,A)=(A,C,E)$, and hence $\mathcal{C}$ would not be UD either.
Now, a prefix code is UD. Hence applying the lemma, the inverted prefix code is UD.


Why is $ (\frac{1}{0} )$ not defined?

Question: Why is $ (\frac{a}{0} ), a \in R $ not defined?
if :$a=0$ what?
Comment: I answered that question [here](http://math.stackexchange.com/questions/1751303/is-there-an-answer-to-1-0/1751313#1751313), and I'm certain it's been asked many times even before then. Basically, if you want your multiplication to be reasonable, and division to be the inverse of multiplication, then $\frac a0$ cannot be assigned a value in any consistent way.
Comment: It is an assumption
Comment: Siri has a [fun answer](https://www.washingtonpost.com/news/morning-mix/wp/2015/06/30/ask-siri-what-is-zero-divided-by-zero-and-she-will-send-you-to-the-burn-ward/) to this question.
Answer: First you need to know one thing:

What does "$\frac{a}{m}$" mean?

The answer is: "$a/m$ is the one and only unique solution to the equation $mr = a$." ($r$ is the unknown that I have considered).
Given that, let's consider the case under consideration:
For $m=0$, the equation clearly states that it has no solutions: for something to be equal to $\frac{a}{0}$, with $a\neq 0$, we would need $0\cdot r = a$. But $0\cdot r = 0$ for any $r$, so there are no solutions to the equation and since there are no unique solutions to the equation, there is no such thing as $\frac{a}{0}$. So $\frac{a}{0}$ does not represent any mathematical number.
Rigorously speaking, we simply do not define "division by $0$" for real analysis.
Answer: By $R$, I assume that you mean the real numbers $\mathbb R$.
Let's think about what division means.
We know that $14 \div 7 = 2$ because you add $7$ together twice to get $14$, i.e. $7 \times 2 = 14$.
We know that $100 \div 5 = 20$ because you add $5$ together twenty times to get $100$, i.e. $20 \times 5 = 100$
What about $1 \div 0$? How many times do you add zero together to get one? Solve $0 \times x = 1$.
You can't do it. It doesn't make sense.
Answer: Another interesting variation is the following: In commutative algebra, if you are allowed fractions (via the construction known as localisation), then you are always allowed to expand a fraction by any number that is a valid denominator. Within that framework, if you allow $0$ as a valid denominator, you automatically say that any fraction may be expanded by $0$. If we take an arbitrary fraction $\frac ab$, and use that, we get
$$
\frac{a}{b} = \frac{0\cdot a}{0\cdot b} = \frac{0}{0}
$$
which means that every single number is equal to $\frac00$. That's kind of boring, don't you think?

Why is $ (\frac{1}{0} )$ not defined?


Setting Position of NSWindow before Display

Question: Right now I'm setting the position of a window that is about to open like this:
-(void) setActiveNodeDialog:(ISKNodeDialogController *)dialog
{
    if (activeNodeDialog)
        [[activeNodeDialog window] close];
    activeNodeDialog = dialog;
    if (activeNodeDialog) {
        [activeNodeDialog setMainWindowController:self];
        NSRect windowRect = [[self window] frame];
        NSRect dialogRect = [[activeNodeDialog window] frame];
        NSPoint pos;
        pos.x = windowRect.origin.x + windowRect.size.width - dialogRect.size.width - 10;
        pos.y = windowRect.origin.y + 32;
        [[activeNodeDialog window] setFrameOrigin:pos];
        [[activeNodeDialog window] makeKeyAndOrderFront:nil];
    }
}

The problem with that is, that the window will "jump" when shown.  And that even though I set the position before showing the window with "makeKeyAndOrderFront".  The window is a NSPanel *.  Anyone any ideas how to fix the jumping?
Setting the position in awakeFromNib is not an option because the main controller is set later.
Answer: In Interface Builder, is "visible at launch" checked for the window? If so uncheck it and then you won't even need this code [[activeNodeDialog window] close];. Basically if that is checked then the window is automatically shown when the xib is instantiated... which you don't want.
Comment: Indeed, that was causing that problem. Thanks


How does SpaCy make its dependency trees?

Question: I discovered that SpaCy had the ability to make dependency trees. For instance given a question "To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?"
We can create its dependency tree with the following code:

I know that it is a set of edges $E$ which represents the relations of labeled dependencies of the particular analysis of $G$. More precisely, an edges $w_i, r, w_j \in E$ represents a relationship of dependence of head $w_i$ towards $w_j$ labeled with the type of relationship. A dependency graph $G$ is therefore a set of dependency relations between tokens in $S$.
For instance we present the dependency graph that follows
 by the following graph:

$G = (V, E)$
$V = \{\mbox{It, is, a, replica, of, the, grotto, at, Lourdes, France, where, the, Virgin, Mary}\\ \mbox{reputedly, appeared, to, Saint, Bernadette ...}\}$
$E=\{\mbox{(is, NSUBJ, It), (is, ATTR, a replica), (a replica, PREP, of),...}\}$

But I was wondering how does $E$ was made with Spacy? How does the dependency tree method chose which node is the root and which aren't?
I have heard from this French paper that, on the one hand that $E \subseteq V \times R \times V$ where $R = {r_1, ..., r_m}$ is a finite set of dependency possible names that we call dependency relationship. They can link two tokens in a sentence. A dependency name $r \in R$ is also called a labeled edge. For example, a relationship between a head verb and its subject can be noted with $r = SBJ$.
On the other hand it was an analysis problem of finding the dependency graph that has the highest score for an input sentence.
Yet I haven't seen where there is a child-children ordering creation.

The context is that I am using SpaCy dependency trees to get the root of sentences of a paragraph and match it with the root of a question.
For the question given above we have to find the answer in the following paragraph :

Context — "Architecturally, the school has a Catholic character. Atop
  the Main Building\'s gold dome is a golden statue of the Virgin Mary.
  Immediately in front of the Main Building and facing it, is a copper
  statue of Christ with arms upraised with the legend "Venite Ad Me
  Omnes". Next to the Main Building is the Basilica of the Sacred Heart.
  Immediately behind the basilica is the Grotto, a Marian place of
  prayer and reflection. It is a replica of the grotto at Lourdes,
  France where the Virgin Mary reputedly appeared to Saint Bernadette
  Soubirous in 1858. At the end of the main drive (and in a direct line
  that connects through 3 statues and the Gold Dome), is a simple,
  modern stone statue of Mary."

The expected answer is  "Saint Bernadette Soubirous".
The sentence having the answer — is "It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858". Its dependency tree of the sentence containing the answer is :

I initialy posted this question on Computer Science SE, without success.
Answer: Matthew Honnibal, who created Spacy, answered me :

The full answer is rather complicated :). I've written a few blog
  posts over the years to make it more accessible. The best place to
  start is this blog post:
https://explosion.ai/blog/parsing-english-in-python
Since v2, we use a more complicated machine learning strategy,
  following the general architecture described here:
  https://explosion.ai/blog/deep-learning-formula-nlp . More specifics
  about the model architecture can be found in this description of the
  NER model: https://www.youtube.com/watch?v=sqDHBH9IjRU
The transition system is mostly described by this paper:
  http://aclweb.org/anthology/D15-1162 , but with some modifications for
  sentence segmentation.

I already started with the first one which I didn't completly understand yet. I will update this post through my reading progress but leave the question open if a concise question comes to your mind.


JSON Message to SOAP Request in Mule

Question: I just found this:
How can I transform an incoming SOAP response into a JSON document using WSO2 ESB 4.0.3?
and basically I'd like to do something similar - just in Mule:
I've got a JSON message and I'd like to transform it into a SOAP request.
The JSON message is a list of customers from Magento and I'd like to insert them via SOAP into our CRM.
Is there a documentation or example or similar on how to do something like this?
Thanks,
Chris
Answer:
Generate a CXF client for your SOAP service: http://www.mulesoft.org/documentation/display/current/Consuming+Web+Services+with+CXF
Transform your JSON string in Java data structures (List and Maps) with:
<json:json-to-object-transformer returnClass="java.lang.Object" />

Use an <expression-transformer> to create the object(s) used by the SOAP operations out of the Java data structures, using the convenience of MEL syntax for map/list navigation: http://www.mulesoft.org/documentation/display/current/MEL+Cheat+Sheet
Answer: Another option will be, after using <json:json-to-object-transformer/> , extract the values and store it in variables..
Then you can use XSLT transformer, you can pass the variables value in iy and can create Dynamic SOAP request.
reference of XSLT transformer in Mule :- https://developer.mulesoft.com/docs/display/current/XSLT+Transformer

JSON Message to SOAP Request in Mule


are they complete metric spaces?

Question: I need to know whether the following spaces are complete or not

Space of all continuos real valued functions with compact support with supnorm metric
The space $C^1[0,1]$ with metric $d(f,g)=max_{t\in[0,1]}|f(t)-g(t)|$

Please help, I have no clue. Thank you.
Comment: Generally speaking, do you know the completion of the space of continuous real functions with compact support with regards to the supremum norm?
Comment: @AymanHourieh, I have no idea.
Comment: OK, do you know that the limit of a uniformly convergent sequence of continuous functions is also continuous?
Comment: @AymanHourieh Yes That I know :)
Answer: 1.
Consider the sequence functions
$$f_n(x)=\max\left\{0,\frac1{1+x^2}-\frac1n\right\}$$
2.
Let $f(x)=\left\vert x-\frac12\right\vert$. Can you find functions $f_n\in C^1([0,1])$ with $\max_{t\in[0,1]}|f_n(t)-f(t)|<\frac1n$? What does that impliy?
Answer:
is complete. A proof you can find in baby Rudin or Stokey Lucas Prescott.
For 2. consider the sequence of functions  $
   f_n(x) = \left\{
     \begin{array}{lr}
       -x & : x <-\frac{1}{n}\\
       \frac{1}{2n}+\frac{n}{2} x^2 & : -\frac{1}{n}\leq x \leq\frac{1}{n}\\
        x & : x>\frac{1}{n}
     \end{array}
   \right.$
Comment: Presumably you mean the space of continuous functions on a compact space in 1., which is indeed a complete space. In general, this is not the same as the space of functions with compact support (the support of a function is the closure of the set of points where $f$ takes nonzero values). The functions $f_n\colon \mathbb{R} \to \mathbb{R}$ from part 1. of Hagen von Eitzen's answer have compact support and they form a Cauchy sequence, yet they do not converge to a function with compact support.
Comment: you are right, I obviously missed that point. Hope my answer to 2. is ok tho.
Comment: The answer to 2 is almost okay. You "smoothen out" the corner of the absolute value at zero and the sequence converges uniformly to the absolute value which fails to be differentiable at $0$. The only minor point is that the problem asked about $C^1[0,1]$, not $C^{1}[-1,1]$, but that's not a big deal, just replace $f_n(x)$ by $f_{n}(x-1/2)$.

are they complete metric spaces?


Solution to an inhomogeneous heat equation with nonnegative initial data is also nonnegative

Question: Suppose $u$ is a smooth solution to $u_t -\Delta u +c(t,x)u =0$ and $u(x,0)=g(x)\ge 0$, where $x\in D\subset \mathbb{R}^n$ is smooth with zero boundary conditions. Assume $g$ is smooth and $c$ is smooth and bounded by $M$. We want to show $u(x,t)\ge 0$ for all $t\ge 0$ and $x\in D$.
In the normal heat equation, I know it follows from the maximum principle, so the idea here I think is to let $v(x,t)=e^{at}u(x,t)$ for some constant $a$; indeed, this was given as a hint. Initially, I thought letting $a=-M$ would give us that $v$ is a subsolution, and hence the result follows. But I now see that the inequality $$v_t -\Delta v= e^{-Mt}(u_t -\Delta u -Mu) \le e^{-Mt}(u_t-\Delta u+cu)=0$$ I had used doesn't work because it actually assumes $u$ is positive to begin with. So now I'm a bit stuck. Any suggestions?
Comment: @zaq Nope, only assumption on $c$ isbthat it is a bounded smooth function
Comment: Do you happen to remember where this problem comes from?
Answer: You basically have the right idea. Set $v = e^{at} u$ and compute
$$v_t - \Delta v = e^{at}(u_t - \Delta u+ au) = e^{at}(a-c)u= (a-c)v.$$
Then choose $a=-M$ like you said, so that $a-c \leq -M+M = 0$. Then use the usual maximum principle for $v$.
EDIT: Let me add a few lines in case you are not familiar with maximum principle arguments. Let's actually set $a=-M-\epsilon$ for $\epsilon>0$. Then
$$v_t - \Delta v + (c+M+\epsilon)v = 0.$$
If the minimum of $v$ over $\bar{D}\times [0,T]$ is attained on the sides $\partial D\times [0,T]$ or base $D\times \{t=0\}$ then $v\geq 0$ and we are done. Hence we may assume the minimum occurs at some $(x,t) \in D \times (0,T]$. The necessary conditions for a maximum are $v_t(x,t) \leq 0$ and $\Delta v(x,t) \geq 0$. Hence
$$0=v_t(x,t) - \Delta v(x,t) + (c(x,t)+M+\epsilon)v(x,t) \leq 0 + (c(x,t)+M+\epsilon)v(x,t).$$
Therefore $(c+M+\epsilon)v(x,t)\geq 0$. Since $c(x,t)+M+\epsilon>0$ we must have $v(x,t) \geq 0$. So the minimum of $v$ over $\bar{D}\times [0,T]$ is non-negative, hence $v\geq 0$ everywhere.
Comment: But this has the same issue as my method though, right? Since bounding $a-c$ by $0$ relies on $u$ being nonnegative.
Comment: Sorry, let me clarify. $a-c \le 0$, but we can't say $(a-c)u \le 0$ unless we already know $u\ge 0$
Comment: The maximum principle holds for $v_t - \Delta v + cv = 0$ provided $c\geq 0$. You just need the coefficient of the zeroth order term to have the right sign. If you haven't seen this version of the maximum principle, it is covered in Chapter 7 of Evans PDE book.


CRM 2011 - Multiple Forms against one Entity

Question: Entity:
Degree
Forms:

Graduation
Post Graduation

Requirement:
When user clicks on New, the Graduation form opens up. Now, using the left navigation pane, I can choose Post Graduation and then the Post Graduation form opens up. I have also written the JS which will flip between the two forms (based on the value selected in the new_degreetype) for existing records. It is undoubtedly ugly because it loads the form twice.
Question:

How do I get this to work with new forms. That is, when the user
clicks on New, I want to display a pop up where the user is supposed
to pick Graduation or Post Graduation and depending upon the value
chosen there, I open the right form in Create mode.
How can I prevent the user from choosing the other form via the Left
    Navigation pane. So, when the user tries to change the form from
    Graduation to Post Graduation, display a Message and say "Sorry!
    This is not allowed". Alternatively, can I remove that completely -
    not give the user the option to be able to flip the form.
Answer: What about the form differs?  It would probably be easier to have a single form, and use javascript to change the way it looks (hidding/showing/enabling/disabling certain fields).  The control over which forms the user sees is based on security roles and how you define the hierarchy.  Trying to get it to do something it wasn't designed to do, may be more pain than it is worth.
Comment: @Kanini - I'm not aware of a supported method of accomplishing it.  Microsoft created a whole JavaScript library to provide a supported way of hidding/showing sections/fields.  I'd personally challenge the Solution Architect, on why not, and query how you're supposed to do it otherwise.
Comment: Thanks, but that is an argument that I have already lost. This is my first Dynamics CRM project and the Solution Architect would not agree to having one form with sections that are made visible / hidden based on  Degree type. Also, all users should be able to use both the forms - the only difference is to open the right form based on Degree Type. Irrespective of the pain, is there a supported way to get it accomplished?
Comment: Where can I find this whole JavaScript library? Is it part of the SDK - because, within the SDK/samplecode/js/formscripts, I find quite a lot of JS files including one formselectorsamples and other ones such as formitemsamples / tabsamples / uisamples etc., Is it one of them?
Comment: @Kanini I'm talking about the built in Library that is a part of CRM itself.  Check out this cheat sheet: http://danielcai.blogspot.com/2011/04/microsoft-dynamics-crm-2011-javascript.html.


Upper bounds for the least $a$ in $p \nmid x^3-a$

Question: Are there any bounds for $a$ when for all $x$, $p \nmid x^3-a$ and $p$ is a prime number?
There have been numerous studies on the smallest quadratic non residue.
However, are there such bounds for cubes as well?
It is known that no such $a$ exists when $p-1$ is coprime to $3$.
However, what happens if $p-1$ is not coprime to $3$?
Because of Thue`s Lemma, there exists $x \equiv ky \pmod p$ where $x,y$ has a absolute value less than $\sqrt {p}$ and $k$ is a number coprime to $p$.
If we set $k$ as a primitive root of $p$, and if all numbers smaller than $\sqrt {p}$ was not such a $a$, it would be a contradiction.
My Question is Thus: For $p \equiv 1 \pmod 3$ is it true that $a<2\sqrt [ 3 ]{ p }$?
This seems to be true, but I cannot prove it, nor do I have any idea how to solve it.
Any help would be appreciated.
Comment: The phrase "If $n$ was $3$" seems a typo (or misplaced) since there is no other mention of $n$ in the Question.
Answer: If $3 \mid p-1$ then let $g$ be a primitive root modulo $p$. Write $a=g^k$ then
$$x^3 \equiv g^k \mod{p} \Leftrightarrow 3 |k$$
So the question you ask is the following: Find the smallest positive integer $a$ which is in  the set
$$A:=\{ g^k | 3 \nmid k \}$$
Therefore, the order of magnitude of primitive roots link here give you some upperbounds. But since $A$ can contain (especially when $p-1$ has many divisors) many non-primitive roots, it is likely that a better upperbound can be found, but I don't see any obvious one.
Comment: Sorry, but don't you mean $3|p-1$?
Comment: The OP dispenses with the case $3$ coprime to $p-1$ since cubing is then an automorphism on the multiplicative group $\mathbb{Z}_p^*$ (and thus every nonzero residue is a cube).
Comment: @ChadShin Ty fixed it. That's exactly what I meant.
Comment: @N.S. So is it unlikely that $a<2\sqrt [ 3 ]{ p }$ can be proved?
Comment: @ChadShin It is not clear to me.... Note that $A$ has roughly $2/3p$ elements and missing  $1/3 p$ elements, but those elements cannot really be anything...Might be better to ask the question on MathOverflow....
Comment: @N.S. Can I ask this question on Math Overflow? I thought that you could only ask highly professional questions there.
Comment: @ChadShin I am not an expert in the field, but this question looks closer to a research level question than to homework, so it is likely appropriate there. If you ask it, don't forget to link this question in your post, and mention that it was not answered here.


How to setup allure-report in nightwatch?

Question: I am pretty new to allure-report. Can someone share the setup and configuration part of allure-report in nightwatch ?.

global module
const path = require('path');
const cwd = process.cwd();

module.exports = {
    reporter: function(results , done) {
      let srcResult = path.join(cwd,'/reports/client','*.xml');
      console.log('path to the reports'+srcResult);
      var generation = allure(["generate", "--clean", srcResult]);
      generation.on ('exit',()=>{
        done();
      });
    }
};

path to the reportsC:\uiautomation\reports\client\*.xml
C:\uiautomation\reports\client\CHROME_80.0.3987.122_Windows_user_registration.xml is not a directory
Report successfully generated to allure-report```

Answer: you can try to use allure-commandline for this.
try this:

run npm install allure-commandline --save-dev
create external global.js with following code:

var allure = require("allure-commandline");

module.exports = {
    reporter: function(results , done) {
      let srcResult = "../path/to/your/xml/result";
      var generation = allure(["generate", "--clean", srcResult]);
      generation.on ('exit',()=>{
        done();
      });
    }
};

after this, you should be able to find allure result folder , and to open them u can use allure open command. also please remember to configure your globals_path in your nightwatch conf file.
Comment: your console log path should be this `C:\uiautomation\reports\client\*` do not include the .xml extension ( try: `let srcResult = path.join(cwd,'/reports/client','*');`
Comment: I tried this and it is generating the allure-results directory. when i run `allure serve` it is opening the report but nothing is displaying in the report. please find the attached image. i opened in the Firefox as well. same results. please find the attached scree-shot in the question.
Comment: i cannot find your screenshot. however, if you already have the report generated using `allure generate` , you should use `allure open ` to open it instead of `allure serve`.
Comment: I tried with just `allure generate` and 'allure open'. Its same results i am getting. I've attached the image file in the main question.
Comment: The only 2 reasons i can think of given ur screenshot.

[i]. your path to the nightwatch generated XML report is incorrect when running `allure generate ` , please double check if it's pointing correctly.

[ii] the nightwatch xml reports are stored in nested directories (eq. test_outputs/test1/mytest.xml), you can try to put the wildcard at to ensure it grab everything under the tests_output directory eq. `allure generate tests_output/*` ,
lemme know if this helps.
Comment: I tried exactly the way you have in the global module. As you mentioned , I believe my path to the xml report is not correct. I am getting the an exception when i use  wildcard. please check the main post for more info. i cannot post in the comments.
Comment: try to use `let srcResult = path.join ( "path" , "to" , "your" , "xml" , "*" );` , note that the last one is wildcard. and `console.log (srcResult);` to ensure it's indeed pointing to the root directory of nightwatch xml report
Comment: I've tried this time with the correct path and console.log shows the correct path to the report but however it returns the specified path is not a directory. I still have the same problem which i showed in the screen-shot earlier. I also see that report didn't generate allure_result folder before creates the html report. Do i need `nightwatch-allure-adapter` module or `allure-commandline` is enough to create the html report ? Please find in the main post for the latest.
Comment: @HendryAng allure report counts Suite name as a test case. Suppose I have a suite, "Testing", and inside which I wrote 2 test cases, "test1" and "test2". Now, after execution, the Allure report shows the test case count as 1, which is "Testing".  It should show 2 test cases (1 and 2). What to do?


CCK Nodereference individual imagefields

Question: Is there a way to use a nodereference field on each individual image in a node? The images are CCK imagefields.
Comment: do you want to reference only the imagefields of nodes from another node?
Comment: Just the images inside of a node. A gallery node, with cck imagefields, 'tagging' Users in the photo similar to Facebook.
Answer: I don't think so, no. A node reference as its name suggests references an entire node. I've not heard of a CCK type that references another CCK field, though I could find a reference to a module that did that for D5. If that's your requirement, my recommendation would be to use an image-as-node method.
Comment: But then you can't use something like Image FUpload, correct? For bulk uploading images into an album? Or can Image FUpload do that on its own?
Comment: @Kevin I don't know about Image FUpload, but I don't see anything on the project page suggesting it can't. I've used [Imagefield Import](http://drupal.org/project/imagefield_import) before and it worked well, if not as snazzy as the Image FUpload flash doobry looks.
Answer: Looks like there was a d5 module for that: fieldreference. Here you'll find a discussion about a d6 port. Maybe you could use viewreference or noderefenrece in conbination with a view to solve your problem as stated in the discussion.


MCAR test for large number variables and small sample size

Question: I have a dataset with 101 observations and 402 columns (those columns comprise several multiple-item questionnaires). Among those 402 columns, 10 of them are categorical and the remaining are continuous. There are 82 observations without missing values, which means 18% of the entire sample (N=101) has one or more missing values. There are 14 missing patterns with 10 of them containing only one observation.
I would like to examine if the missing data mechanism is MCAR in this dataset. I have tried LittleMCAR ("BaylorEdPsych" package) and TestMCARNormality ("MissMech" package) to test for MCAR using RStudio (version 1.1.442). However, LittleMCAR only allows a maximum of 50 variables; TestMCARNormality did not work out either. The code for TestMCARNormality is shown as below:

## df[1:10] were excluded due to their class of categorical variables; del.lesscases was set to 1 because default for this parameter was 6, and if set to default it would have no missing pattern in the test.
TestMCARNormality(df[11:402], del.lesscases = 1)

After I ran this code, it showed the following error:

Error in solve.default(sigoo) :
    system is computationally singular: reciprocal condition number = 1.37693e-17

I wasn't sure why this happen and what it meant. I was wondering if anyone knows how I can address this issue and how I can test MCAR for my dataset. Thank you in advance for your time; sincerely appreciated!!
Comment: Although this is about R, I think the fundamental issue is statistical so I am voting to leave it open.
Comment: Thank you for keeping this open, Peter. While some literatures did encourage a test for MCAR for determining a proper approach for dealing with missing data, some other literatures did also indicate that multiple imputation can deal with both MCAR and MAR (Enders, 2010). In such a case, it seems that I can proceed to the phase of imputation regardless MCAR or MAR (except for MNAR). I just don't understand why a test of MCAR is critical if I were able to deal with missing data by multiple imputation.
Answer: MissMech imputes missing data in order to do its MCAR test. Having more variables (columns) than observations (rows) can cause problems for data imputation, which I would guess is what is happening here.
Also, when you do an MCAR test, you are assuming that the missing data mechanism is random, i.e. you're assuming MCAR, and the p-value tells you how consistent the missing data pattern is with that assumption.
You have to use your knowledge about the subject at hand to justify that assumption. I.e. you could have theoretical reasons to believe the missing data is MCAR, MAR or MNAR.
In the absence of any strong theoretical reasons, you could take a more exploratory approach to missing data mechanisms, see Tierney et al. (2014).


powershell to restart local computer and resume script

Question: I am trying to re-install antivirus on multiple computers which requires reboot. I have a script to uninstall the antivirus and reboot but for installation, I have to run another script. Is there anyway to ran only one script which will perform UNINSTALL+REBOOT+INSTALL
Comment: Does this answer your question? [Running a Powershell script, restarting and then continue to run](https://stackoverflow.com/questions/24469265/running-a-powershell-script-restarting-and-then-continue-to-run)
Comment: https://stackoverflow.com/questions/24469265/running-a-powershell-script-restarting-and-then-continue-to-run
Answer: Use this:
<code>cd HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce
new-itemproperty . TheKey -propertytype String -value "Powershell.exe Powershell script"
Restart-Computer
</code>
Comment: https://community.spiceworks.com/topic/1334887-restart-a-computer-remotely-using-powershell-and-prompt-for-computer-name-and-un


How to monitor Solaris network interface bandwidth?

Question: I would like to see x MB/s, like freebsd has ifstat command which is pretty cool. I need something similar
Answer: You might want to look into BWMng for real-time monitoring, but I can live with SNMP-based graphing and reporting through munin or cricket.
On a side note, I understand that ifstat itself can be compiled for Solaris.
Oh, for throughout, detailed, protocol-level statistic dissections, ntop can't be beaten, but it requires a lot of resources and is overkill for most cases.
Comment: Thanks, i found this only:

 dladm show-link -s -i 10 bge1
Answer: You can look at the "raw" kernel stats using kstat:
kstat -p -m ce (or whatever you interface is e.g. bg)
Answer: One of the nicest tools that I've found is nicstat.  It helped me to quickly rule-out the network when we were trying to diagnose what we believed to be an NFS problem related to the network.


Regressão Múltipla com R

Question: Eu tenho uma tabela com algumas colunas de fatores que variam com o passar do tempo. Com a regressão múltipla eu posso avaliar a influência de um grupo de fatores na variação de 1. Como posso fazer isso em R?
Comment: A pergunta não está clara, você que saber apenas como fazer uma regressão no R?
Comment: Sim, eu gostaria de saber como fazer a regressão múltipla no R e se possível, a linear também.
Comment: Você algum código que já tentou desenvolver ou esta tentando? Colaca ele na pergunta, vai melhorar o entendimento do pessoal e vão poder ajudar melhor.
Comment: É só você seguir o passo a passo da resposta abaixo do Carlos Cinelli. Segui os passos e consegui fazer perfeitamente os cálculos e obtive os dados que eu precisava.
Comment: [Nós nao fazemos o seu trabalho de graca](https://pt.meta.stackoverflow.com/q/5483/) . O que é que voce já tentou?
Answer: Você pode rodar uma regressão no R usando a função lm. Usando a base mtcars que já vem no R como exemplo:
regressao <- lm(mpg ~ cyl, data = mtcars)

Primeiramente passamos a para a função lm fórmula da regressão mpg ~ cyl  e em seguida a base de dados data = mtcars. A fórmula mpg ~ cyl significa que estamos regredindo a variável mpg (milhas por galão) contra a variável cyl (cilindradas), seria equivalente à equação mph = b0 + b1*cyl + e, e você está estimando os parâmetros b0 (constante) e b1 (coeficiente angular). O resultado da regressão foi salvo no objeto regressao.
Ao dar summary você vê os principais resultados da regressão:
summary(regressao)

Call:
lm(formula = mpg ~ cyl, data = mtcars)

Residuals:
    Min      1Q  Median      3Q     Max
-4.9814 -2.1185  0.2217  1.0717  7.5186

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)  37.8846     2.0738   18.27  < 2e-16 ***
cyl          -2.8758     0.3224   -8.92 6.11e-10 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 3.206 on 30 degrees of freedom
Multiple R-squared:  0.7262,    Adjusted R-squared:  0.7171
F-statistic: 79.56 on 1 and 30 DF,  p-value: 6.113e-10

Para fazer uma regressão múltipla, basta incluir mais variáveis após o ~. Mais especificamente, o elemento à esquerda do ~ é a variável dependente (o seu y) e todas variáveis à direita do ~ são variáveis explicativas (os X). Por exemplo:
regressao_multipla <- lm(mpg ~ cyl + disp + wt + hp , data = mtcars)

Aqui rodamos uma regressão com 4 variáveis explicativas: cyl, disp, wt e hp, todas no data.frame mtcars. Para ver os principais resultados, use summary novamente:
summary(regressao_multipla)
Call:
lm(formula = mpg ~ cyl + disp + wt + hp, data = mtcars)

Residuals:
    Min      1Q  Median      3Q     Max
-4.0562 -1.4636 -0.4281  1.2854  5.8269

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept) 40.82854    2.75747  14.807 1.76e-14 ***
cyl         -1.29332    0.65588  -1.972 0.058947 .
disp         0.01160    0.01173   0.989 0.331386
wt          -3.85390    1.01547  -3.795 0.000759 ***
hp          -0.02054    0.01215  -1.691 0.102379
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.513 on 27 degrees of freedom
Multiple R-squared:  0.8486,    Adjusted R-squared:  0.8262
F-statistic: 37.84 on 4 and 27 DF,  p-value: 1.061e-10

Há diversas outras funções para trabalhar com regressões no R. O objeto que a função lm retorna é da classe lm, para você ter uma idéia dos métodos disponíveis para a classe você pode rodar methods(class = "lm").
Comment: Nossa, muito obrigado pela explicação! Me ajudou muito!
Comment: Testei a função com uma base de dados que estou trabalhando e funcionou muito bem.

Regressão Múltipla com R


what data type to use to stored these characters: '●▬ஜ۩卐۩ஜ▬●'

Question: How can I deal with ( select and print ) the following set of characters:
DECLARE  @V NVARCHAR(108)
SELECT @V = '●▬▬▬▬▬▬▬▬▬ஜ۩卐۩ஜ▬▬▬▬▬▬▬▬▬●'

SELECT @V
PRINT @V

even when debugging the contents of the variable is not showing properly.
Answer: You missed N before code..read here more on why we need to append N'' before characters..https://softwareengineering.stackexchange.com/questions/155859/why-do-we-need-to-put-n-before-strings-in-microsoft-sql-server
DECLARE  @V NVARCHAR(1080)
SELECT @V = N'●▬▬▬▬▬▬▬▬▬ஜ۩卐۩ஜ▬▬▬▬▬▬▬▬▬●'

SELECT @V
PRINT @V

Comment: of course, that's right. So obvious now that you have said it!!! - tired DBA


A criterion to prove that a topological space is simple connected.

Question: Let $\{U_i\}$ be an open covering of the space $X$ having the following properties:
(a) There exist a point $x_0$ such that $x_0\in U_i$ for all $i$.
(b) Each $U_i$ is simply-connected.
(c) If $i\not= j$ then $U_i\cap U_j$ is arcwise connected. Prove that $X$ is simple connected.
Given any loop $f:I\rightarrow X$ based at $x_0$ I have to prove that this is homotopic to the constant map $e(t)=x_0$
Hint: Consider the open cover $\{f^{-1}(U_i)\}$ of the compact metric space space $I$ and make use of the Lebesgue number of this covering.
What I have done: Take $r$ as the Lebesgue number of the open cover $\{f^{-1}(U_i)\}$ and consider a division of $I$ as $[t_0=0,t_1],..., [t_{n-1},1=t_n]$ where $t_{j}-t_{j-1}<r$ for each $j$. Then for each $1\leq j\leq n$ there exist an $i$ such that $[t_j,t_{j-1}]\subset f^{-1}(U_i)$ the $f[t_j,t_{j-1}]\subset U_i$. Therefore we have a finite family $\{U_1,...,U_n\}$ of open subset of the open cover of $X$ given such that $f[t_j,t_{j-1}]\subset U_j$. I don't what to do with at this point
Any advice?
Thanks!
Answer: For every $i=0,\dots n$, choose a path
$$\gamma_i:\{t_i\}\times I\to U_i\cap U_{i+1}$$ from $\gamma_i(1)=f(t_i)$ to $\gamma_i(0)=x_0$. For $i=0,n$, you can let $\gamma_i$ be the constant path at $x_0$. This gives you a map from the subspace
$$I\times\{1,0\}\cup\{t_0,\dots,t_n\}\times I$$ of $I\times I$ to $X$ which on $I\times\{1\}$ coincides with $f$ and on $I\times\{0\}$ coincides with the constant map at $x_0$. On each rectangle $[t_{i-1},t_i]\times I$, we can extend the map from its boundary to the entire rectangle since $U_i$ is simply-connected. Do you see how this gives a homotopy from $f$ to the constant map?
Comment: It was a nice construction. And continue the way I was following!
Answer: Observe that $I$ is compact and its image will lie in finitely many $U_i$ (as you figured out). Also note that Van Kampens theorem will tell you that the union of those finitely many $U_i$ is generated by the fundamental groups of those $U_i$. So you should arrive quickly at your goal.


Tensorflow Autoencoder - How To Calculate Reconstruction Error?

Question: I've implemented the following Autoencoder in Tensorflow as shown below.  It basically takes MNIST digits as inputs, learns the structure of the data and reproduces the input at its output.
from __future__ import division, print_function, absolute_import

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)

# Parameters
learning_rate = 0.01
training_epochs = 20
batch_size = 256
display_step = 1
examples_to_show = 10

# Network Parameters
n_hidden_1 = 256 # 1st layer num features
n_hidden_2 = 128 # 2nd layer num features
n_input = 784 # MNIST data input (img shape: 28*28)

# tf Graph input (only pictures)
X = tf.placeholder("float", [None, n_input])

weights = {
    'encoder_h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
    'encoder_h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'decoder_h1': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_1])),
    'decoder_h2': tf.Variable(tf.random_normal([n_hidden_1, n_input])),
}
biases = {
    'encoder_b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'encoder_b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'decoder_b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'decoder_b2': tf.Variable(tf.random_normal([n_input])),
}

# Building the encoder
def encoder(x):
    # Encoder Hidden layer with sigmoid activation #1
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['encoder_h1']),
                                   biases['encoder_b1']))
    # Decoder Hidden layer with sigmoid activation #2
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['encoder_h2']),
                                   biases['encoder_b2']))
    return layer_2

# Building the decoder
def decoder(x):
    # Encoder Hidden layer with sigmoid activation #1
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['decoder_h1']),
                                   biases['decoder_b1']))
    # Decoder Hidden layer with sigmoid activation #2
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['decoder_h2']),
                                   biases['decoder_b2']))
    return layer_2

# Construct model
encoder_op = encoder(X)
decoder_op = decoder(encoder_op)

# Prediction
y_pred = decoder_op
# Targets (Labels) are the input data.
y_true = X

# Define loss and optimizer, minimize the squared error
cost = tf.reduce_mean(tf.pow(y_true - y_pred, 2))
optimizer = tf.train.RMSPropOptimizer(learning_rate).minimize(cost)

# Initializing the variables
init = tf.global_variables_initializer()

# Launch the graph
with tf.Session() as sess:
    sess.run(init)
    total_batch = int(mnist.train.num_examples/batch_size)
    # Training cycle
    for epoch in range(training_epochs):
        # Loop over all batches
        for i in range(total_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            # Run optimization op (backprop) and cost op (to get loss value)
            _, c = sess.run([optimizer, cost], feed_dict={X: batch_xs})
        # Display logs per epoch step
        if epoch % display_step == 0:
            print("Epoch:", '%04d' % (epoch+1),
                  "cost=", "{:.9f}".format(c))

    print("Optimization Finished!")

    # Applying encode and decode over test set
    encode_decode = sess.run(
        y_pred, feed_dict={X: mnist.test.images[:examples_to_show]})
    # Compare original images with their reconstructions
    f, a = plt.subplots(2, 10, figsize=(10, 2))
    for i in range(examples_to_show):
        a[0][i].imshow(np.reshape(mnist.test.images[i], (28, 28)))
        a[1][i].imshow(np.reshape(encode_decode[i], (28, 28)))
    f.show()
    plt.draw()
    plt.waitforbuttonpress()

When I am encoding and decoding over the test set, how do I calculate the reconstruction error (i.e. the Mean Squared Error/Loss) for each sample?
In other words I'd like to see how well the Autoencoder is able to reconstruct its input so that I can use the Autoencoder as a single-class classifier.
Many thanks in advance.
Barry
Answer: You can take the output of the decoder and take the difference with the true image and take the average.
Say y is the output of the decoder and the original test image is x then you can do something like for each of the examples and take an average over it:
tf.square(y-x)

This will be your reconstruction cost for the test set.
Comment: Why do you square the diff?


Expected number of draws of m objects at a time out of n objects with replacement

Question: The question is as follows: Consider an urn of n distinct objects. At each time, m (m < n) objects are drawn randomly at once. The objects obtained are noted and then placed back in the urn. What is the expected number of draws I have to make until all of the objects have drawn at least once?
Comment: Can we find the expected number of draws before a particular object is drawn, and then use linearity of expectation somehow?
Comment: @GTonyJacobs I am not sure about it. What do you have in mind?
Comment: Well, any particular object has probability $m/n$ of being chosen on one draw. The expectation for how many draws *it* will take to be chosen is thus $n/m$. That's the same for each object..... does that get us closer to the answer?
Comment: @GTonyJacobs the number of draws could be as large as possible. How can you express it as sum of random variables?
Comment: The support of the distribution in question would be from $\lceil n/m\rceil$ to infinity...
Comment: The following [MSE link](https://math.stackexchange.com/questions/2147576/) may be relevant.
Comment: In the special case where $m=n-1$, the expected number of draws is $2+\frac{1}{m}$
Comment: I think $m=1$ might also be easy to calculate, but it's a little more complicated than $m=n-1$.
Comment: [This MSE question](https://math.stackexchange.com/questions/131664/coupon-collector-problem-with-batched-selections) is almost the same, but allows for repetition within each batch, which is impossible here.
Answer: Let's take a look at a random draw log. In each line, we have all $n$ objects, where $1$ means it has been drawn and $0$ means it hasn't been. Here, $n=5$ and $m=3$:
<code>1 1 1 0 0
0 1 1 0 1
1 0 1 0 1
0 1 1 1 0
</code>
Now we will try to find the probability, that all objects have been drawn after $k$ draws. This is achieved when each object has been drawn at least once. We can denote this as $1-\text{[probability that it has never been drawn]}$. In each draw, there is probability $\frac{n-m}{n}$ that the object isn't drawn, so after $k$ draws, there is probability $\left(\frac{n-m}{n}\right)^k$ that the object hasn't been drawn.
Okay, so we know that each object is after $k$ draws drawn at least once with probability $1-\left(\frac{n-m}{n}\right)^k$. If we have $n$ objects, then probability, that all of them have been drawn at least once after $k$ draws is obviously $\left(1-\left(\frac{n-m}{n}\right)^k\right)^n$.
If we want to know, what is the probability, that we got the state "each object has been drawn at least once" exactly after $k$ draws, we need to subtract probability, that it was achieved after $k-1$ draws or sooner:
$$P_{\text{exactly after $k$}}=\left(1-\left(\frac{n-m}{n}\right)^k\right)^n-\left(1-\left(\frac{n-m}{n}\right)^{k-1}\right)^n$$
Now if you lok at definition of expected value, our expected number of draws is sum of expressions $k\times\text{[probability of ending exactly after $k$]}$. Namely,
$$\sum_{k=0}^\infty k\cdot\left(\left(1-\left(\frac{n-m}{n}\right)^k\right)^n-\left(1-\left(\frac{n-m}{n}\right)^{k-1}\right)^n\right)$$
And this can be simplified to:
$$\sum_{k=0}^\infty 1-\left(1-\left(\frac{n-m}{n}\right)^k\right)^n$$
I'm convinced it can't be simplified any more (and WolframAlpha agrees). If you're interested in any specific step of this solution, just write in comments.
Edit: My answer is not totally correct, see comments.
Comment: This doesn't work for $n=2$, $m=1$. That case is easy to calculate by hand, and the expected value is $3$, but this formula gives $\frac83$.
Comment: Argh, you're right. The problem is that for $k=1$ the real probability is $0$, but my approach gives $\left(\frac{n-m}{n}\right)^n$, because it assumes *average* $m$ drawn objects, but the problem states *exactly* $m$ objects. So my solution is right for this variation of the problem. Maybe for large number the two results converge. (?)
Answer: This answer is a work in progress.
Case 1: $m=n-1$
In this case, all but one are drawn on the first draw, and we're just looking for the expected number of attempts to get the last one. Since the probability of getting it on any particular draw is $\frac{n-1}{n}$, the expected number of draws after the first to get it is $\frac{n}{n-1}=1+\frac{1}{m}$. Adding to this the original draw, we have $E=2+\frac{1}{m}$.

Case 2: $m=1$
The probability that the number of draws required is $k$ is given by $n\times\frac1n\times K$, where $K$ is the probability that the other $n-1$ objects were all drawn in the first $k-1$ draws. (The first $n$ picks the final object drawn, and the $\frac1n$ gives us that it shows up in draw number $k$.) We can write a formula for $K$ using inclusion-exclusion:
$$K=\frac{(n-1)^{k-1} - \binom{n-1}{n-2}(n-2)^{k-1}+\binom{n-1}{n-3}(n-3)^{k-1}+\cdots + (-1)^n\binom{n-1}{1}(1)^{k-1}}{n^{k-1}}$$
Letting $E_n$ denote the resulting expected value for $n$ objects drawn $m=1$ at a time, we have:
$$E_2=3, E_3=\frac{11}{2}, E_4=\frac{25}{3}, E_5=\frac{137}{12}, E_6=\frac{147}{10}$$
I don't yet have a general formula for $E_n$ in the $m=1$ case, but it seems possibly tractable.
Edit: The case $m=1$ is the Coupon collector's problem, which is a wheel I'm going to now stop re-inventing. We have $E_n=n(1+\frac12+\cdots+\frac1n)$, which confirms the answers I'd found so far.

Another approach: The number of objects not yet chosen after $k$ steps is approximately $U_k=n\cdot\left(\frac{n-m}{n}\right)^k$. (That value is actually precise for $k=0$ and $k=1$, and approximate after that.) We might expect all objects to have been chosen at some point where $U_k<1$. How far below $1$ is not entirely clear, but maybe we can get an idea by looking at cases where we know the answer. Thus, for $m=n-1$, we have $U_{E_n}=n\cdot\left(\frac1{n}\right)^{2+\frac1{n-1}}$. This quantity is $\frac14$ for $n=2$, and goes to $0$ as $n\to\infty$. On the other hand, for $m=1$, the quantity $U_{E_n}$ goes up from $\frac14$ and approaches $\frac12$ as $n\to\infty$.


JQuery setting a number format

Question: I'm using JQuery and I'm having a problem trying to sort out how to increase a number.
The record number is something like 1364-14-1234.
The number format works like this:
1364 - Member number
14 - Year in 2 digit format
1234 - in the number which needs to be increased.
The problem is how do I add a leading zero to the number to keep a 4 digit number if the number is 0123.

1364-14-0001

var data = $('#member_id').text();
var arr = data.split('-');
var num = arr[2];
num++;
$("#member_id").html(arr[0] + " - " + arr[1] + " - " + num);

My JSfiddle
Comment: have you seen http://stackoverflow.com/questions/1267283/how-can-i-create-a-zerofilled-value-using-javascript ?
Answer: Something like this maybe
function pad(numb, len) {
    while (numb.toString().length < len) numb = '0' + numb;
    return numb;
}

$('#member_id').text(function(_, txt) {
    var arr = txt.split('-'),
        len = arr[2].length;

    arr[2] = pad(+(arr[2]) + 1, len);
    return arr.join('-')
});

FIDDLE

JQuery setting a number format


UART Transmitter only functions when embedded logic analyzer is running

Question: I have been trying to implement a UART in order to communicate between my Lattice MachXO3D board and my computer. At the moment I am attempting to implement the transmission from the FPGA.
Upon testing on the hardware, I encountered a very strange issue. If run normally, it will function for a few seconds and then it will suddenly stop functioning (The CH340 connected to my computer will report it is receiving messages containing 0x0). However, if I embed a logic analyzer onto the FPGA through the Lattice Diamond software, and I run the analyzer, it will function perfectly for an extended period of time.
Sadly, I don't have a logic analyzer, so the embedded logic analyzer is my only chance at knowing what is actually being transmitted.
These are the files related to my implementation:
baud_gen
LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
USE IEEE.numeric_std.ALL;
-- Generates 16 ticks per bit
ENTITY baud_gen IS
  GENERIC(divider: INTEGER := 13 -- 24M/115200*16
          );
  PORT(
    clk, reset: IN STD_LOGIC;
    s_tick: OUT STD_LOGIC
    );
END baud_gen;

ARCHITECTURE working OF baud_gen IS

BEGIN
  PROCESS(clk)
    VARIABLE counter: UNSIGNED(3 DOWNTO 0) := to_unsigned(0,4);
  BEGIN
    IF clk'EVENT AND clk='1' THEN
      IF reset='1' THEN
        s_tick <= '0';
        counter := to_unsigned(0,4);
      ELSIF counter=to_unsigned(divider-1,4) then
        s_tick <= '1';
        counter:= to_unsigned(0,4);
      ELSE
        s_tick <= '0';
        counter := counter + 1;
      END IF;
    END IF;
  END PROCESS;
END working;

rs232_tx
LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
USE IEEE.numeric_std.ALL;

ENTITY rs232_tx IS
  PORT(clk: IN std_logic;
       tx: OUT std_logic;
       rst: IN std_logic;
       fifo_empty: IN std_logic;
       fifo_RdEn, fifo_RdClock: OUT std_logic;
       fifo_data: IN STD_LOGIC_VECTOR(7 DOWNTO 0);
       Q: OUT STD_LOGIC_VECTOR(1 DOWNTO 0));
END rs232_tx;

ARCHITECTURE working OF rs232_tx IS
  TYPE state IS (idle,start,data);
  SIGNAL tx_pulse: STD_LOGIC := '1';
  SIGNAL s_tick: STD_LOGIC;
  SIGNAL pr_state, nx_state: state := idle;
  SIGNAL data_val: std_logic_vector(7 DOWNTO 0):=(others=>'0');
  SIGNAL data_count: unsigned(2 DOWNTO 0):=to_unsigned(0,3);
BEGIN
  process(s_tick, rst)
    VARIABLE count: unsigned(3 DOWNTO 0):= to_unsigned(0,4);
  BEGIN
    IF rising_edge(s_tick) THEN
      count := count + to_unsigned(1,4);

      IF count = to_unsigned(15,4) THEN
        tx_pulse <= '1';
      ELSE
        tx_pulse <='0';
      END IF;
    END IF;
  END PROCESS;

  process(tx_pulse,rst)
  BEGIN
    IF rising_edge(tx_pulse) THEN
      IF rst='1' THEN
        pr_state <= idle;
        data_val <= (others=>'0');
        data_count <= to_unsigned(0,3);
      ELSE
        pr_state <= nx_state;
        CASE pr_state IS
          WHEN idle =>
            data_count <= to_unsigned(0,3);
          WHEN data =>
            data_count <= data_count + to_unsigned(1,3);
          WHEN start =>
            data_val <= fifo_data;
          WHEN OTHERS =>
        END case;

      END IF;
    END IF;
  END process;

  process(fifo_empty,rst,data_count,pr_state,data_count)
  BEGIN
    case pr_state is
      when idle =>
        Q <= ('1','1');
        fifo_RdEn <= '0';
        tx <= '1';
        IF fifo_empty='0' AND rst='0' THEN
          nx_state <= start;
        ELSE
          nx_state <= idle;
        END IF;
      WHEN start =>
        Q <= ('1','0');
        fifo_RdEn <= '1';
        tx <= '0';
        nx_state <= data;
      WHEN data =>
        Q <= ('0','1');
        fifo_RdEn <= '0';
        tx <= data_val(to_integer(data_count));
        if data_count=to_unsigned(7,3) then
          nx_state <= idle;
        ELSE
          nx_state <= data;
        end if;

    end case;
  END process;

  fifo_RdClock <= tx_pulse;
  baud_gen: ENTITY work.baud_gen
    PORT MAP(clk,reset=>'0',s_tick=>s_tick);
END working;

testbench
LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
USE IEEE.numeric_std.ALL;

ENTITY rs232_tx_test IS
  GENERIC(clk_period: TIME := 41666666.7 fs;
          baud_period: TIME := 8680.55556 ns);
END rs232_tx_test;

ARCHITECTURE working OF rs232_tx_test IS
  SIGNAL clk: STD_LOGIC := '0';
  SIGNAL tx, rst, fifo_empty, fifo_RdEn, fifo_RdClock: STD_LOGIC;
  SIGNAL fifo_data: STD_LOGIC_VECTOR(7 DOWNTO 0);
BEGIN
  clk <= NOT clk AFTER clk_period/2;
  rst <= '1', '0' AFTER 100 ns;
  PROCESS
  BEGIN
    fifo_empty <= '1';
    WAIT FOR baud_period;
    fifo_empty <= '0';
    WAIT FOR baud_period*16;
  END PROCESS;

  fifo_data <= ('1','1','0','0','1','0','1','1') WHEN fifo_RdEn='1' ELSE (others=>'0');
  dut: ENTITY work.rs232_tx
    PORT MAP(clk,tx,rst,fifo_empty,fifo_RdEn,fifo_RdClock,fifo_data);
END working;

EDIT
I have tested another UART design I found online @ 9600 bps and it fails in the same way. It can send a constant character, in this case 'a', to a terminal on my computer, and then it suddenly stops sending anything. However, if I start listening to the soft logic analyzer I generated in Lattice Diamond, it works without a problem and does not fail.
Comment: I note you're using a logic generated clock `s_tick` as an actual clock. This is not recommended in FPGAs as logic generated clocks can cause timing issues. It is much better to use the same clock for all logic and us s_tick like a clock enable instead.
Comment: @BrianDrummond Yes, I do realize it. This is because I want the counter to stay at 0 for one tick after the state is changed to it. Should I have designed it differently?
Comment: Did you simulate it?
Comment: @MatthewTaylor Of course, under simulation it works perfectly. On the hardware, it fails after about 2 seconds. I have even simulated it for 5 seconds, which took quite a long time with a step size of 1fs, and even then it worked well. The problem is definitely something which occurs after synthesis but not in simulation
Comment: I'm sorry - stupid question - there's a testbench. How about STA and/or a gate level simulation?
Comment: Possibly related to the 2-process form of state machine. But also, at `pr_state <= nx_state;    CASE pr_state IS` you do realise the CASE sees the OLD value of pr_state, right?
Comment: also, are you aware your `count` counter, being a variable is updated immediately and hence the check is done on the new value, rather than the current value? this means the adder is in the compare logic chain. If you used a signal instead, the check path would not have the adder in it, as this purely updates the counter value. (apart from possibly putting the TX pulse in the wrong place, with longer logic chains you reduce the fmax of the circuit).
Comment: you also generate a logic clock `tx_pulse` from another logic clock `s_tick`, probably compounding the issue I mentioned above.
Comment: The DUT produces a `fifo_rdclock` but the testbench makes no use of it. Im sure the real system likely uses it.
Comment: @MatthewTaylor I am using GHDL for simulation and Lattice LSE for synthesis. I am not sure how to get the timing information out of Lattice in order to do so, but once I figure it out I'll try it.
Comment: @Tricky How would using s_tick as a clock enable look like? Would I have my process act on the rising edge of clk and then check if s_tick='1'?
Comment: @FranciscoAyalaLeBrun Yes - but you need to make sure s_tick is only 1 clock wide.
Answer: This smells like a classic timing issue.
In the comments, others have explained where there are weaknesses in the code that could indirectly lead to this. I'm going to concentrate on what is occurring.
As you have stated, in simulation your code works as you expect. However, that is only half the story. To create an FPGA bit stream the build tools take your code and several other files, synthesises, and conducts Place & Route. Your timing issue occurs during P&R. This is why your simulation doesn't pick up on any errors, as I'm assuming it's an RTL (pre-place & route) simulation.
During P&R the tools lay the logic in the best way to fit the timing model of the device so all paths meet their timing requirements. The path timing requirements are derived from explicit statements in a Timing Constraints file and inferred from your code (that's why your coding style matters btw).
Once P&R is complete, the tools will put the build artefact through a static timing analyser (STA) tool and report back whether the build fails to meet the timing requirements.
This leads to two questions:

Does the build report a timing error?
Do you have a Timing Constraints file - if you're unsure, the answer is no.

The way to debug your problem is to use the Timing Report generated by Lattice Diamond to see where the failures are.
If there are no reported failures it means your timing model is wrong because of a lack of appropriate timing constraints. As a minimum, you will need to constrain all IO in the design and describe all the clocks in your design.
Here is a good document to help you out:
https://www.latticesemi.com/-/media/LatticeSemi/Documents/UserManuals/RZ/Timing_Closure_Document.ashx?document_id=45588
The reason that your design works when you use the embedded Logic Analyser is that extra logic has been placed in the design, which changes the timing model. The P&R tools lay out the design differently, and by luck have placed the design in such a way as meet the real timing requirements on it.
As I often say to my Software Engineer colleagues, software languages create a set of instructions, HDL creates a set of suggestions.
Comment: I managed to use the FT2232 on the board instead of the CH340, and it works now. The only thing that is different are the pins that I are used, as well as the distance the signal has to travel so I believe it was indeed a timing issue. I will have to find a way to do timing simulations on Linux.


Why does $\frac{\sum X_i^2}{n} \rightarrow \sigma^2+\mu^2$?

Question: I cannot find a reference to the proof of this, but I found
$$\frac{\sum X_i^2}{n} \rightarrow \sigma^2+\mu^2$$
From: https://math.stackexchange.com/a/1416911/248602
So why does it converge to that?
Comment: Oh, that's the "source", right? Please edit the link into your question. The proof is right there ("by [strong law of large numbers](https://en.wikipedia.org/wiki/Law_of_large_numbers#Strong_law) (SLLN)"), applied exactly as I suggested above. I can write it as an answer if you like.
Comment: @JuhoKokkala The usual statistical meanings. $X_i$ is a random variable, $\sigma^2=Var(X_i)$ and $\mu$ is a mean.
Comment: @Glen_b http://math.stackexchange.com/a/1416911/248602
Comment: The question is missing definitions of $X_i$, $\sigma^2$ and $\mu$.
Answer: I presume you're already happy to accept that -- in the limit as $n\to\infty$ -- $\bar{Y} \to E(Y)$ (as long as $E(Y)$ is finite.
Now let $Y_i=X_i^2$. Then clearly also (in the limit) $\overline{X^2}\to E(X^2)$ (again, as long as the expectation of $Y$ is finite).
But $\text{Var}(X) = E(X^2) - E(X)^2$ so $E(X^2) =  \sigma_X^2 + \mu_X^2$.
Hence by the strong law of large numbers, $\lim_{n\to\infty}
\overline{X^2}\to \sigma_X^2 + \mu_X^2$.
Comment: Could you clarify, why $Var(X) \rightarrow \sigma^2$?
Comment: Also what's the meaning of $\bar{Y} \rightarrow E(Y)$? I mean how to interpret it to "be the same" as $\bar{X} \rightarrow \mu$?
Comment: Strictly speaking it's shorthand but read it as "As $n\to\infty$ sample means  converge to their expectation". *Obviously* it doesn't matter whether the variable is called $X$ or $Y$.
Comment: @mavavilj Var(X) doesn't converge to $\sigma^2$, it *is* $\sigma^2$.


Can a child use Colab?

Question: My child, who is aged 8, wants to use Google Colab to explore Python and machine learning. When he tries to access it, he receives the error that:
"This service is unavailable for Google Workspace for Education users who are under 18 and children under 13 (or the age of consent in your country)."
His account is managed through Google Family Link, hence Google knows his age. The browser is Chrome running on Android.
The Terms of Service do not prevent access by children; on the contrary, the preamble seems specifically written to allow for the possibility of a parent entering into the agreement on behalf of a child. It seems strange that Google should provide a free resource to those wanting to learn, but deny access to children. So my question is, is something wrong with our configuration, or is this Google's policy? If the latter, what is the rationale for it? I ask this question so I can decide whether it is justified to circumvent the restriction, which appears at first sight to be technical rather than legal.
Comment: I'm voting to close this question because it is not related to programming
Comment: I admit I'm confused. Google Colab, Python and Machine Learning are very much programming,
Answer: Actually, what I think that Google is doing right. As I'm also a school Student I can understand the curiosity your child is having regarding exploring cool stuff. But on the other hand, it can also be harmful to his mind.
Google collab is specially designed to collab mainly in the field of Machine Learning, Data Analysis, etc. and if your child who is just 8 years old try to do that stuff at that age then for sure he will fail to do it which can impact negatively on your child's brain and if he finds that these fields are very-difficult then for sure your child will try not to pursue a career in that field.
I understand that doing programming is good for all ages but you as a father you need to make sure that your child does not go too much into it at that age as for the child of 8 years it is really important to do physical activity also.
And the simple and direct answer to your question according to me is that - Ya, a child of 8 years old can do programming but try not to use online tools like Google Collab or online IDEs as it can distract your child.
Please also note that I'm not a psychologist, I'm answering this question because somewhere in my life I was also stuck in this situation and I preferred doing my primary tasks first and pursuing programming as a hobby. But definitely, I'll say that it totally depends on person-to-person so at the end of the day it is your choice to let it do or not.
From the next time please don't ask these types of questions here ask them on Quora
Comment: OK. It's just the Google help for Colab explicitly asks for them to be asked here. Hey ho.
Answer: I also want to use Google Colab, but since I am 12 years old, the same error appeared, so I just asked my Mom to lend her account to me, and I just used it.
I mean you could lend your child your account just for that, but it's your choice.

Can a child use Colab?


Strictly formal proof of $ \displaystyle \lim_{x \to 0} \frac{\sin(x)}{x} = 1 $.

Question: I'm looking for a proof of $ \displaystyle \lim_{x \to 0} \frac{\sin(x)}{x} = 1 $ that does not use other trigonometric functions or any first-order approximation to the sine function. Is this possible? The other proofs that I've seen on this website don't really fit these stringent requirements, so I was hoping to see a different kind of demonstration altogether. Thanks!!
Comment: What definition of sin(x) are you taking here?
Comment: A functional approach is taken in "A New Approach to Circular Functions, II and lim (sin x)/x" by Gerson B. Robison in
*Mathematics Magazine*, Vol. 41, No. 2 (Mar., 1968), pp. 66-70
Comment: @Azmoti: It's not an exact duplicate because the OP seems to want to avoid Taylor series. :)
Comment: The limit should read $ \displaystyle \lim_{x \to 0} \frac{\sin(x)}{x} = 1 $.
Comment: Oh, so you don't want to use $\tan $ and $\cos$, for instance...Otherwise, this would do: http://www.proofwiki.org/wiki/Limit_of_Sine_of_X_over_X/Geometric_Proof
Comment: I'm looking for a definition of the sine function that doesn't involve any Taylor series... Because that would sort of give a first-order approximation already.
Comment: @HaskellCurry as you take $$\sin(x)=\frac{1}{2i} (e^{ix}-e^{-ix})$$ you already have the taylor series
Comment: possible duplicate
Comment: @Byron: That seems to be an interesting article!
Comment: Since $\sin(0)=0$ then $$\lim_0 \frac{\sin(x)}{x}=\lim_0\frac{\sin(x)-\sin(0)}{x-0},$$
so the limit appear as the derivative of $\sin$ at $0$ and then we have
$$\lim_0 \frac{\sin(x)}{x}=\cos(0)=1.$$
Answer: An interesting proof that sees no trigonometry or Maclaurin series until the very final conclusion (at the cost of doing Riemann integration):
Define a function $ \theta: [-1,1] \to \mathbb{R} $ as follows:
$$
\forall a \in [-1,1]: \quad \theta(a) \stackrel{\text{def}}{=}
                            \int_{0}^{a} \sqrt{1 - x^{2}} ~ d{x}.
$$
For each $ a \in [-1,1] $, we can interpret $ \theta(a) $ as the signed arc-length of the parametrized curve
$$
\left\{
\left( x,\sqrt{1 - x^{2}} \right) \in \mathbb{S}^{1} ~ \Big| ~
\text{$ x $ is between $ 0 $ and $ a $}
\right\}.
$$
Observe that $ \theta $ has the following properties:

$ \theta(0) = 0 $ and
$ \theta: [-1,1] \to \mathbb{R} $ is a strictly increasing continuous function.

There is thus an inverse function $ \theta^{-1}: \text{Range}(\theta) \to [-1,1] $, and we define the sine function (or at least part of it) by

$$ \sin \stackrel{\text{def}}{=} \theta^{-1}. $$

Hence,
\begin{align}
   \lim_{a \to 0} \frac{\sin(\theta(a))}{\theta(a)}
&= \lim_{a \to 0} \frac{{\theta^{-1}}(\theta(a))}{\theta(a)} \\
&= \lim_{a \to 0} \frac{a}{\theta(a)} \\
&= \lim_{a \to 0} \frac{a}{\displaystyle \int_{0}^{a} \sqrt{1 - x^{2}} ~ d{x}}
   \quad (\text{By definition.}) \\
&= \lim_{a \to 0} \frac{1}{\sqrt{1 - a^{2}}} \quad (\text{By l'Hôpital's Rule.}) \\
&= 1.
\end{align}
Therefore,
$$
\lim_{\theta \to 0} \frac{\sin(\theta)}{\theta} = 1.
$$
Comment: @enzotib, We aren't allowed to use l'Hôpital's Rule because that would assume that $\frac{d}{dx} \sin x = \cos x$. However, that result follows from $\lim_{x\to 0} \frac{\sin x}{x} = 1$, so by assuming $\sin\prime = \cos$, we are assuming the limit we want to prove.
Comment: @enzotib: If you use l'Hôpital's Rule on $ \sin $, you get $ \cos $, which the OP wants to avoid. :)
Comment: @anonymous, you cannot take sine as a power series without first knowing the derivative of sine.
Comment: Cannot l'Hôpital's Rule be used directly on $\sin(x)/x$?
Comment: Not necessarily.  We typically take the definition of sin(x) as a power series.  From this definition, it's straightforward to prove that sin'(x) = cos(x) without proving that sin(x)/x tends to 1.
Comment: Is there a way how to see that this definition of sine captures our intuitive idea of the behavior of the sine function?
Answer: Taking the definition
$$\sin(x)=\frac{1}{2i} (e^{ix} - e^{-ix}) $$
Than you get from the exponentialfunction the identity
$$\sin(x)=\sum_{k=0}^\infty  (-1)^n \frac{x^{2n+1}}{(2n+1)!}=x-\frac{x^3}{3!}+\frac{x^5}{5!}\mp \cdots$$
So we have
$$\lim_{x\rightarrow 0 } \frac{\sin(x)}{x}=\lim_{x\rightarrow 0} 1-\frac{x^2}{3!} + \frac{x^4}{5!}\mp \cdots=1$$
Else you could define
$$\sin(x)=x\prod_{k=1}^\infty \left(1-\frac{x^2}{k^2 \pi^2 } \right)$$
Than
$$\lim_{x\rightarrow 0 } \frac{\sin(x)}{x} =\lim_{x\rightarrow 0} \prod_{k=1}^\infty \left(1-\frac{x^2}{k^2\pi^2}\right)$$
Comment: I think you could, instead of expanding the exponential function, directly take the limit of $e^{ix}/x$, with the aid of L'Hôpital.
Answer: If you're not defining $\sin$ via Taylor series, the obvious other rigorous alternative would be to define it (near $0$) as the inverse function of $\arcsin$, where we take $\arcsin y = \int_0^y \frac{dt}{\sqrt{1-t^2}}$. Then
\begin{eqnarray}
\int_0^y \frac{dt}{\sqrt{1-t^2}} &<& \int_0^y \frac{dt}{\sqrt{1-y^2}} = \frac{y}{\sqrt{1-y^2}} \\
\int_0^y \frac{dt}{\sqrt{1-t^2}} &>& \int_0^y \, dt = y
\end{eqnarray}
So $\sqrt{1-y^2}<\frac{y}{\arcsin y}<1$, and thus $\lim_{y \to 0} \frac{y}{\arcsin y} =1$ by the squeeze theorem.
After making the change of variables $x=\arcsin y$ in this last limit (justified because $\arcsin y$ is continuous and monotone), it follows that $\lim_{x \to 0} \frac{\sin x}{x}=1$.
Comment: @DominicMichaelis: It's the integral of something positive.
Comment: why it is so obvious that your function is injective  ?
Answer: I'm assuming that OP uses the geometric (slightly non-rigorous) definition of the trigonometric functions, i.e., $(\cos(t),\sin(t))$ is the point on the unit circle forming angle $t$ with the positive $X$ axis).
The requested limit (known as the first fundamental limit) can't be computed by any method that relies on the fact that $\sin'(x)=\cos(x)$ (in fact, this limit is required in the proof of that fact), and I think this is the point of this exercise.
Here is a geometric proof, legitimate along these lines.
Comment: You just hit the nail on the head, Ittay. However, if we can give a formal definition of 'angle', then the problem of semi-rigor is solved. One can define 'angle' using arc-length, which is what I did above, but of course, I needed to resort to Riemann integration.
Comment: and you just drove the nail all the way into the wood @HaskellCurry :)


PrimeFaces DataTable row coloring not working

Question: I want to Change the color of a row based on the status column.
I have looked at the example on the primefaces website
http://www.primefaces.org/showcase/ui/datatableColoring.jsf
and looked up examples online but I cant seem to get it working with my code.
The problem is it is highlighting everything rather than just the rows where the status is set to 'Closed'
here is my code
<p:dataTable id="ComplaintTable" var="complainer"
                    rowKey="#{complainer.id}"
                    selection="#{bigComplaintsDAO.selectedComplaintRow}"
                    widgetVar="complaintsTableSearch"
                    paginator="True" rows="15"
                    paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                    rowsPerPageTemplate="15,20,30,40,50,100"
                    resizableColumns="true"
                    filteredValue="#{bigComplaintsDAO.filteredComplaintRow}"
                    selectionMode="single" value="#{bigComplaintsDAO.complaintList}"
                    style="font-size:10px;width:100%;"
                    rowStyleClass="#{(complaint.status) eq Closed ? 'Closed' : null}">

and the CSS

Answer: Solved it!
I changed my code to
rowStyleClass="#{(complainer.status) eq 'Closed' ? 'Closed' : null}"

Hope this helps someone else in the future!
Answer: In my case, rowStyleClass did not work if the datatable was placed within a p:panelGrid.
I switched to a p:panel and then it worked.
Comment: Sure that the rowStyleClass was not applied (independent of the css selwctpr working or not)? I see no reason a panelGrid would break things
Comment: Yes Sir I am sure. If I tried to set the style in a column it worked correctly. Then I only changed the container from panelGrid to panel and it worked fine. Very weird. If you need details about the versions I use let me know.
Answer: For me, the following code worked:
DataTable (HTML)

Component (TS)
  getRowClass() {
    return 'my-new-class';
  }

Comment: This is not PrimeFaces but PrimeNG