Gerar String a partir de conteúdo da lista

Question: Tenho uma classe chamada CoreFiltroSearch que tem uma lista de ICoreFiltro chamada filtros. Segue a classe e a interface:
public class CoreFiltroSearch {

    private List<ICoreFiltro> filtros = new ArrayList<>();
    private String            order   = "asc";
    private String            sort    = "id";
    private Integer           page    = 0;
    private Integer           size    = 40;
}

public interface ICoreFiltro {

    String getChave();

    String getValor();

    Boolean isQFilter();

    Boolean isDateFilter();

}

A partir da lista filtros, preciso criar uma String usando os campos getChave() e getvalor() que são do elemento ICoreFiltro.
Eu tentei fazer usando uma solução que encontrei em um blog, porém não consigo sequer subir o servidor usando essa solução,  coloquei a pergunta aqui, a solução que usei na tentativa de conseguir criar essa String foi essa:
private String montarQueryString(CoreFiltroSearch filter) {
    return filter.getFiltros().stream()
            .map(p -> urlEncodeUTF8(p.getChave()) + "=" + urlEncodeUTF8(p.getValor()))
            .reduce((p1, p2) -> p1 + "&" + p2)
            .map(s -> "?" + s).orElse("");
}

static String urlEncodeUTF8(String s) {
    try {
        return URLEncoder.encode(s, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new UnsupportedOperationException(e);
    }
}

Se alguém poder me ajudar com uma solução melhor eu agradeço, mas acho que não existe problema nessa lógica.
Comment: https://gitlab.com/geosales-open-source/tc-http-conn/blob/master/src/main/java/br/com/softsite/httpconn/HttpConn.java no método `serializeFormContent`. Estou tendo dificuldades de mandar o link da linha exata por conta do navegador do smartphone não ajudar, mas é nesse método que faço a magia.
Comment: Eu fiz isso semana passada, mas sem "reduzir". Apenas mapeei e coletei no final. Vou passar o link do projeto depois explico numa resposta
Answer: Passamos por uma situação semelhante. Como gerar a string de queryParam a partir de um conjunto de parametrizações? Mais especificamente, a partir de um conjunto de chaves e valores?
Como mencionei no comentário, passei por essa necessidade em um projeto aberto da empresa.
A ideia de começar de uma lista de chave/valor tivemos bem parecidas, então é um bom começo. Assumi no meu projeto que possam existir parâmetros de query que sejam simplesmente indicados pela presença, então tratei a possibilidade do "valor" associado à "chave" ser nulo. Mas não parece ser esse o seu caso. Vou assumir que sempre são preenchidos.
Então, mapeamos essa stream de chave/valor em uma stream de strings. Até aí concordamos perfeitamente:
filter.getFiltros().stream()
    .map(p -> urlEncodeUTF8(p.getChave()) + "=" + urlEncodeUTF8(p.getValor()))

Depois disso, precisamos agora transformar isso em uma grande string. Como eu tive de conhecer grande parte da biblioteca de stream do Java 8 para dar suporte no TotalCross a algo suficientemente semelhante, já imaginei isso como sendo uma coleção usando o coletor joining.
Existem 3 variações do joining:

a que não recebe argumentos, a saída são as strings uma do lado da outrasemnadanomeio
a que recebe um argumento, que delimita o-separador-entre-as-strings-da-stream
a que recebe 3 argumentos, semelhante a variação anterior, porém adicionando uma sequência de carácteres no começo e outra no final

Como não desejamos alterar nada nem no começo, nem no final, mas alternar esses pares de chave/valor com "&", então devemos usar o collect(Collectors.joining("&")). Ficando assim o código:
filter.getFiltros().stream()
    .map(p -> urlEncodeUTF8(p.getChave()) + "=" + urlEncodeUTF8(p.getValor()))
    .collect(Collectors.joining("&"));

Se quiser se aprofundar um pouco mais nas questões de reduzir/coletar, lembro dessas duas questões aqui no SOpt que tratam disso:

Em um map/reduce no Java, a operação de acumulação deve gerar um novo objeto? Ou posso aproveitar o antigo?
Como utilizar Stream.reduce() de forma apropriada


Como actualizar valor de um campo da linha de documento na V9?

Question: Estava vendo exemplos de códigos da V10 e encontrei isso,
<code> // Add new line to the invoice and set all the item related information.
 PriEngine.Engine.Vendas.Documentos.AdicionaLinha(invoice, item.SubItems[0].Text, ref qtd);
 linhas = invoice.Linhas;
 linhas.GetEdita(1).Desconto1 = 10;
</code>
Gostaria de atingir o mesmo resultado utilizando os interops da V9, de modo que consiga actualizar o campo CCusto das linhas de um documento de compra.
Alguém sabe como posso proceder? obrigado!
Answer: Boa tarde, pode ser feito assim:
<code>PriEngine.BSO.Comercial.Compras.AdicionaLinha(invoice, "A0001", 1);
invoice.get_Linhas()[1].set_CCustoCBL("valor");
</code>
Comment: Obrigado! Funcionou


Probability of an event vs probability of random variable

Question: How does the probability of an event differ from the probability of a random variable?  For example, consider the event where there are 4 babies born.

Sample space:
BBBB, BBBG, BBGB, BGBB, GBBB, GBBG, GBGB, GGBB,
BGGB, BGBG, BBGG, BGGG, GBGG, GGBG, GGGB, GGGG
P(no of girls born is 2): 6/16

Let X be the random variable

X = no of girls born (takes values from 0 to 4).
P(X = 2) = 6/16

So what is the use of defining a random variable and going through all the process of probability distribution? In essence it is same as 9th grade probability.
Comment: Re your "in essence" statement: That's precisely the point.  Probability calculations are notoriously tricky.  In complex situations (mathematical finance, weather prediction, etc) there's no avoiding the use of random variables; indeed, they are usually the objects of interest (think of an option price--a random variable--versus the myriad ways in which the underlying asset's price can evolve--an event). By studying toy problems amenable to analyses any ninth-grader can conduct, we establish that the random variable approach works, is a realistic model, and accords with intuition.
Comment: Thanks a ton!! So if I understand correctly when the random variable helps when the sample space cannot not defined comprehensively.. as in case of options where we don't know how the underlying will change... The random variable will help in notation simplification and studying the distribution of random variable can help in prediction without even the complete sample space...
Comment: What is your question?
Comment: I am not able to understand the need of defining a random variable. I calculate the same probability with defining a sample space and calculating the favourable outcomes(as stated in the question example either X=2 or no of girls is 2). When it is all same then why random variable?  why do we say that a random variable quantifies the outcome of a random event. What are we actually trying to achieve?
Answer: A random variable is a function taking its value in $\mathbb{R}$. So $P(X)$ doesn't make any sense. So the problem here is how to define your random variable $X$.
You can define it as : $X$ is the number of girls born. Then, $\mathbb{P}[X=2]$ is the probability that $2$ girls are born. So you can also compute $\mathbb{P}[X=0], \mathbb{P}[X=1]$ and so on.
Of course here, $\mathbb{P}[X=2] = 6/16$ and you can notice that it is exactly the same to say {no of girl born is 2} or $\{X=2\}$.
$\{X=2\} = \{\text{no of girl born is } 2\}$
Answer: Theoretically (relative to what you are probably experiencing right now) speaking, we are talking about the preimages of a random variable, which are events.
In the case of your interest, if $X$ is a random variable whose values are interpreted as the number of girls born, then the event that $X = 2$ is the event under consideration. By inspection we see that the set of all the points in the B-G space at which $X = 2$ is precisely the six points that have 2 G's; so we arrive at the same conclusion.
Answer: Consider the sample space $S$. Then the expression
$$
P(X=x_0)
$$
means $P(\bigcup\limits_{f(E)=x_0}^{} E)$
Here $E$ is a basic event. (Set of exactly one occurrence).
where $f$ is the measure by which you find the value of the random variable. It is a function which takes in a basic event (occurrence) as an input and has range $R$. (In your example, this function outputs the number of girls in the event).
Comment: This doesn't make sense.  Maybe you meant to write $\Pr(X=x_0) = f\left(\bigcup_{X(\omega)=x_0}\{\omega\}\right)$? One ordinarily would express the latter as $f\left(\{\omega\mid X(\omega)=x_0\}\right):$ that's both simpler and more general (your basic events don't always exist).


Django: Formulário não salva no banco de dados do django

Question: Quando eu clico em cadastrar a página simplesmente atualiza e nada é salvo no banco de dados. No console do django recebo a mensagem:

"GET /pessoas/ HTTP/1.1" 200 898
  "POST /pessoa-add/ HTTP/1.1" 200 898

views.py
from django.shortcuts import render, redirect
from .models import *
from .forms import *

def pessoas(request):
    dados = {'pessoas': Pessoa.objects.all(), 'form': PessoaForm}
    return render(request,'pessoas.html', dados)

def PessoaAdd(request):
    form = PessoaForm(request.POST or None)
    if form.is_valid():
        form.save()
    return redirect(request, 'core_pessoa')

forms.py
from django.forms import ModelForm
from .models import Pessoa

class PessoaForm(ModelForm):
    class Meta:
        model = Pessoa
        fields = '__all__'

urls.py
from django.urls import path
from .views import *

urlpatterns = [
    path('', home),
    path('pessoas/', pessoas, name='core_pessoa'),
    path('pessoa-add/', pessoas, name='core_pessoa_add'),

pagina html
<!DOCTYPE html>

    <meta charset="UTF-8">
    <title>Pessoas</title>

Lista de Pessoas

        {% for pessoa in pessoas %}

{{ pessoa }}

        {%  endfor %}

    <form action="{% url 'core_pessoa_add' %}" method="post">
        {% csrf_token %}
        {{form.as_p}}
        <button type="submit">Cadastrar</button>
    </form>

Answer: Tens a tua rota a chamar a função errada. Altera o teu urls.py para:
from django.urls import path
from .views import *

urlpatterns = [
    path('', home),
    path('pessoas/', pessoas, name='core_pessoa'),
    path('pessoa-add/', PessoaAdd, name='core_pessoa_add'), # altera nesta linha pessoas para PessoaAdd


Getting rich via in-group signals

Question: There is an answer on the main site which purports to portray the sitting American president as a bit of a simpleton (which I may or may not agree with). The problem is that while doing this the answer sources a quote to the president which he did not say. While in Economics we often tell fables to get a point across, I find this problematic.
My reasoning is that it gets us closer to sympathy based political voting and that can distort which one is the best answer.
In this case I find that the answer is great other than this problem. I still don't think that is an excuse. The poster of the answer rolled back edits that did nothing but removed this part. Rather he edited in a part which states that the quote is false. In my opinion this is still a bad work around.
Are there any suggestions on how to proceed?
Answer: The default answer is:
Vox populi
Give the masses what they want and let the answer stand as is!
Answer: I suggest that the answer be edited again to remove the parts referring to the president and the statement that the quote is false, and then locked to prevent any further editing (moderators have the power to do the latter as stated here).
While some may perceive otherwise, I wouldn't see this as censorship.  It's just a matter of what is appropriate content for a site that aims to build up a set of definitive answers to questions on economics.
An effect of locking is that it also prevents further voting.  If there were a way to prevent editing while still permitting voting I would suggest that, but it seems there isn't.
Comment: For the record, on 21 March when the answer on the main site was locked, this answer had received net +4 votes.
Comment: @EconJohn.    Now locked - thank you.
Comment: It seems unreasonable to lock the question. It prevents editing, voting, and new answers.
Comment: @psn  It's just the one answer that has been locked.  The question and other existing answers can be edited and voted on, and (subject to having 10 rep) new answers can still be posted.
Comment: And it still has 4 upvotes while the other answer has 2, so I am not changing the accepted status. Unfortunately the angry boys could adapt to my decision rule and start upvoting the alternative, so I am unlikely to change the accepted answer ever.

Getting rich via in-group signals


Is my understanding of cosmosdb pricing correct?

Question: I'm struggling to understand how the pricing mechanism for RU/s works. Specifically my confusion comes in when the word "first" is used.
I've done my research here:https://devblogs.microsoft.com/cosmosdb/build-apps-for-free-with-azure-cosmos-db-free-tier/?WT.mc_id=aaronpowell-blog-aapowell
In the second paragraph it's mentioned:
"With Azure Cosmos DB Free Tier enabled, you'll get the first 400 RU/s throughput and 5 GB storage in your account for free each month, for the lifetime of the account."
So hypothetically speaking if I have an app that does one query and that 1 query evaluates to 1RU. Can I safely assume that

400 users can execute the query once per second for free?

500 users can execute the query once per second and I will only be charged for 100RU

If the RU is consistently less than 401 per second, there will be no charge

Please do make mention if there's any other costing I should be aware of. Ie. Any cosmosDb dependencies, or app service costing
Answer: You're not really thinking about RU/sec correctly.
If you have, say, 400 RU/sec, then that is your allocated # of RU within a one-second window. It has nothing to do with # of users (as I doubt you're giving end-users direct access to your Cosmos DB instance).

in the case of operations only costing 1 RU, then yes, you should be able to execute 400 operations within a 1-second window, without issue (although there is only one type of operation costing 1 RU, and that's a point-read).
in the case you run some type of operation that puts you over the 400RU quota for that 1-second period, that operation completes, but now you're "in debt" so to speak: you will be throttled until the end of the 1-second period, and likely a bit of time into the next period (or, depending on how deep in RU-debt you went, maybe a few seconds).
when you exceed your RU/sec allocation, you do not get charged. In your example, you asked what happens if you try to consume 500 RU in a 1-second window, and asserted you'd be charged for 100 RU. Nope. You'll just be throttled after exhausting the 400 RU allocation.

The only way you'll end up being charged for more RU/sec, is if you increase your RU/sec allocation.
Comment: You'll get a 429, along with  # of milliseconds that you'll throttle by. And during that time period, any operation you attempt will just get you another 429 (with an updated throttle time).
Comment: Thanks! Think I've got it now. But In the case of being throttled, is there a hard definition of what that is? Ie. just a delay in execution? Requests suspended?
Comment: Thanks, owe you a coffee!
Answer: There is some more reading out there you can do:
azure cosmos db free tier
pricing examples
Comment: Please don't post link-only answers.


unaligned sequence labeling

Question: How to extract semantic slots like: person="Obama", attribute="birthday" for questions"what is the birthday of Obama" and "when did Obama born". As far as I know, models like CRF, RNN can't label sentences which don't contain slotvalue like "birthday" in "when did Obama born?". What kind of model can I use?
Answer:
How to extract semantic slots like: person="Obama", attribute="birthday" for

The task is referred to as slot filling. Many models have been explored, e.g.:

Vu, Ngoc Thang. "Sequential convolutional neural networks for slot filling in spoken language understanding." arXiv preprint arXiv:1606.07783 (2016). https://arxiv.org/abs/1606.07783
Mesnil, Grégoire, Yann Dauphin, Kaisheng Yao, Yoshua Bengio, Li Deng, Dilek Hakkani-Tur, Xiaodong He et al. "Using recurrent neural networks for slot filling in spoken language understanding." IEEE/ACM Transactions on Audio, Speech, and Language Processing 23, no. 3 (2015): 530-539. https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/44628.pdf

There are some shared tasks that focus on it, e.g.:

Dialog State Tracking Challenge (DSTC) http://www.colips.org/workshop/dstc4/

unaligned sequence labeling


Interaction not significant in Cox regression model?

Question: I ran a cox model with an interaction between two categorical variables (sex and treatment group) included like:
coxph(Surv(time, status) ~ sex*treat+age+edu, data=data)

And there is no significant interaction (coef=0.11, p = .30)
but I find it odd that when I actually run the same model separated by sex, treatment effect is significant and very strongly among women (coef=-0.22, p = .005), but not at all among men (coef = -0.03, p = .84).
I'm wondering if this looks ok, or if there's something wrong with the way I tested it.
With such a big difference, I keep wondering why the interaction didn't come out as significant.
Could this be because of the difference in the DV among men and women? (likelihood of getting diagnosed?)
Among women, number of event is almost 700 out of 2000,
among men, it's 300 out of 2300, so very different.
Answer: The models are not equivalent. When you split the data set you implicitly fit the model below (although you won't get coefficients for sex and the interactions in the subset models).
In your original model you assume that age and edu have the same effect in both sex groups, in the subsets models (and the model below) you allow age and edu to have different effects depending on sex. Additionally the subset models allow the baseline hazard to be different (and non-proportional).
So the model you fit using subsets is more something like this:
coxph(Surv(time, status) ~ strata(sex) + treat + age + edu + (treat + age + edu):sex, data = data)

Comment: Oh I see. Thanks a lot! Is it okay then to just report the subset models?? (i.e., women and men separately) or would that be problematic??
Comment: Can I even trust that interaction p value?? Is that even the right way to test interaction? I kind of get what you mean by the baseline hazard being different... but I'm not sure if then the interaction (the way I tested it) is really testing whether the treatment effects differ by gender
Comment: Well, all of these questions are not trivial to answer and depend on context and goal of the analyses. When in doubt I'd advice to post another Q giving more detail about your analysis goals and ask for help on how to achieve that.
Comment: The general danger is to try different analyses until you get a result you like (e.g. initial model vs. subset models). In any case I would report all analyses you tried and discuss the different results. Based on this exploratory analyses it would be best to then test your conclusion from exploration on a new, independent data set.


SELECT AVERAGE ACROSS COLUMNS

Question: I hope one of you will be able to help me with my question.
I am building a simple course management program, which is able to store trainee data such as names, start_date, course_duration etc. One of the key fields is the choice of courses, their dates and assessment scores.
Now I would like to create a function which retrieves the average for all assessment courses that a user has taken.
I have structures my table in the following way:
user_id | name | start_date | team | course1 | course1_date | course1_assessment | course2 | course2_date | course2 assessment | course3 | course3_date | course3_assessment | etc...
Now I'm having a problem building the SELECT function that gives me the right Average for all assessments. It's all fine when all fields are populated but it might happen that the trainee hasn't taken some of the courses yet, but the average needs to be pulled out anywya (if i.e. course2_assessment field is NULL)
I'm trying to do sth. like:
"SELECT COALESCE(course1_assessment, course2_assessment, course3_assessment)/ (only a number of fields from the COALESCE(Array) with VALUES != NULL) FROM table WHERE name='$name'";
If someone can suggest a better way, like creating a larger number of tables please feel free to suggest.
Regards
Jacek
Comment: The better way is to normalize your schema to start with.
Answer: You may want to revamp your data model.  I can see at least three tables that could be broken out from the table you mention.
create table user(
   user_id int,
   name varchar
);

create table course(
    course_id int,
    name varchar
);

create table assessment(
    assessment_id int,
    user_id int,
    course_id int,
    name varchar,
    grade int
);

Then to get the average grade:
select avg(grade) as grade, user_id
from assessment
group by user_id;

This data model will prevent you from adding columns to the table every time a new course/assessment is created.  Instead, new assessments will result in a row in the assessment table.
Answer: The best way is to normalize your tables, as has been suggested.
As a second answer, this lets you keep your current schema.
There is not PIVOT in MYSQL like in SQL Server, but that would have been nice.
SELECT AVG(assessment)
FROM (
    SELECT name, course1_assesment assessment FROM `table` WHERE course1_assessment IS NOT NULL
    UNION ALL
    SELECT name, course2_assesment FROM `table` WHERE course2_assessment IS NOT NULL
    UNION ALL
    SELECT name, course3_assesment FROM `table` WHERE course3_assessment IS NOT NULL
) t
WHERE name = 'name'

Comment: Thanks guys for your suggestions. Now my question is, would i really need the course table? It seems to replicate the fields from the assessment table.
Comment: Was that the question to me? If so I wasn't criticising anybody, was just wondering if that's the best approach, since i want to avoid creating too many tables.
Comment: @Jack If a course never has more than one assessment then you can put the grades on the course table.  If courses have more than one assessment, assessment and course must be split.  You definitely should have course, user.
Comment: Thanks Kevin; there will be one assessment per course indeed so I suppose two tables will do in this case. I want to be able to do the following: 1. enter trainee details(name, start_date, departm.), 2. pull out trainee details (name, start_date, departm., courses attended + relevant assessment scores), 3. Pull out various matrix: i.e. average assessment score for all courses taken by a trainee, average assessment score for a given course taken by all trainees, 4. Show the number of fails (85% will be the pass mark) per course, 5. Create an alert system when a trainee is due to attend a course

SELECT AVERAGE ACROSS COLUMNS


Looping for creating graphs

Question: I am seeking some guidance on how to best automate a loop to create graphical visualization of some calculations made from dict.
I've pieced together the following code to create a single graph, but need to produce many similar graphs (using different variables) and would rather not type out each variable multiple times (there will be 100s of variables).
For a single graph, I have the following code (see below) where Calclist is a dict and variable1 is a specific column within that dict:
 import pandas as pd
 import numpy as np
 import matplotlib.pyplot as plt

 Sets = {}
 labels = ['0 - 5','5 - 25','25 - 50','50 - 75','75 - 100']
 blocks = [-1,5,25,50,75,100]

 for i in Calclist:

     out = pd.cut(Calclist[i]['variable1'], bins = blocks)
     Sets[i] = (pd.value_counts(out)/Calclist[i]['variable1'].count())*100

 df = pd.DataFrame(Sets)
 df.reset_index(level=0, inplace=True)
 df.rename(index = str, columns = {'index':'blocks'}, inplace=True)

 ax = df.plot.bar(title='One iteration - works well')
 ax.set_xlabel("x-axis label")
 ax.set_ylabel("y-axis label")
 ax.set_xticklabels(labels, rotation=45)

So far, so good - this is what the code will produce:

What I would really like to do is iterate through variable1 (to variable2, variable3, .....).
I have tried a couple of things, and think I'm close but likely missing something fundamental.
Specifically, I tried nesting another loop that iterates over a series using "Parameter", which holds the variable names that I'm interested in visualizing:
 Sets = {}
 labels = ['0 - 5','5 - 25','25 - 50','50 - 75','75 - 100']
 blocks = [-1,5,25,50,75,100]

 Parameter = pd.Series("variable1","variable2")

 for j in Parameter:

     for i in Calclist:

         out = pd.cut(Calclist[i][Parameter[j]], bins = blocks)
         Sets[i] = (pd.value_counts(out)/Calclist[i]
         [Parameter[j]].count())*100

but I get the following error:
 TypeError: Index(...) must be called with a collection of some kind,
 'powertotal_total' was passed

Any and all suggestions are greatly appreciated.
Comment: your code is not really reproducable because you don't give example data for `Calclist`... if you could update your code with dummy `Calclist` that would be helpful
Answer: Your error message is caused by the way you are initializing Parameters:
parameter = pd.Series('variable1', 'variable2')
...
TypeError: Index(...) must be called with a collection of some kind,
'variable2' was passed

You must pass array-like, dict, or scalar value to pd.Series. Also, iterating over Parameters will return its values. Finally, you should initialize Sets for each parameter:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

labels = ['0 - 5','5 - 25','25 - 50','50 - 75','75 - 100']
blocks = [-1,5,25,50,75,100]

Parameters = pd.Series(['variable1', 'variable2'])

for parameter in Parameters:
    Sets = {}
    for i in Calclist:
        out = pd.cut(Calclist[i][parameter], bins = blocks)
        Sets[i] = (pd.value_counts(out)/Calclist[i][parameter].count())*100

    df = pd.DataFrame(Sets)
    df.reset_index(level=0, inplace=True)
    df.rename(index=str, columns={'index': 'blocks'}, inplace=True)

    ax = df.plot.bar(title=parameter)
    ax.set_xlabel("x-axis label")
    ax.set_ylabel("y-axis label")
    ax.set_xticklabels(labels, rotation=45)

Comment: This worked perfectly, especially if I remove the plotting from the Calclist loop (via backspace). Thank you for the help with the code as well as the education on pd.Series.
Comment: I changed the code so that the plotting is outside the Calclist loop.
Answer: You can't iterate over the pandas series in that way.
Try
for j in ["variable1","variable2"]:

or some variation of that
edit: or look up how to iterate over a pandas series
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.iteritems.html

Looping for creating graphs


Spivak's Calculus - Chapter 5 Problem 8

Question: (For making things simple - everywhere where I've written "$\lim f(x)$" I meant $\lim_{x\to a} f(x)$)
I'm trying to do this excercise:

And apparently the answers in the answer book are yes, yes, no, no. I have not read explanations yet but I came to a different conclusion and need info whether my logic is flawed. So let's assume $f(x)$ is defined everywhere "near" $a$ and $g(x)$ is not (numbers near $a$ are not in the domain). That means $\lim g(x)$ does not exist. Then, clearly $\lim [f(x)+g(x)]$ exists and it's the same as $\lim f(x)$, same for $\lim f(x)g(x)$. So following this logic the answers should be yes, no, yes, no. But I think I might have done something that is not allowed when trying to solve the problem this way. Am I right?
Comment: If $g$ is not defined on some nhood of $a$, $f+g$ is neither, as $\def\dom{\mathop{\rm dom}}\dom(f+g) = \dom f \cap \dom g$, same for the product. So your argumentation is wrong.
Comment: @user36346 When we talk about limits of a function at a point $a$, we usually require that $f$ be defined on some open neighborhood of $a$. Or at the very least that $a$ be a limit point of $\mathrm{dom}(f)$. This is to avoid vacuous limits.
Answer: As explained in the comment on your post, if you can't approach $a$ with $g$ then you can't approach it in neither $g(x)f(x)$ nor $g(x) + f(x)$.
With this said here are some hints:
(a) Consider $f(x) = \frac{1}{x-a}, g(x) = \frac{-1}{x-a}$ for addition, and two step functions where $f$ is $1$ for $x<a$ and $2$ for $x \ge a$ and $g$ is $2$ for $x < a$ and $1$ for $x \ge a$ for multiplication.
(b) $g(x) = f(x) + g(x) - f(x)$
(c) Assume for contradiction that $\lim_{x \to a} \left[ f(x) + g(x) \right]$ does exist and work like in (b)
(d) Consider $f(x) = 0, g(x) = \frac{1}{x-a}$
Answer: So it seems you and Mr. Spivak disagree on (b) and (c). Let's see.
Assume $\lim f(x)$ and $\lim (f(x)+g(x))$ exist. Since
$$
g(x) = (f(x)+g(x))-f(x),
$$
we deduce that $\lim g(x)$ exists by the standard rule for the limit of a sum: the answer to (b) is yes.
Assume, for the sake of contradiction, that $\lim (f(x)+g(x))$ exists. Then we apply (b) and discover that $\lim g(x)$ exists. This contradicts the assumption that $\lim g(x)$ does not exist: the answer to (c) is no.

Spivak's Calculus - Chapter 5 Problem 8


Realm Error Encrypted interprocess sharing is currently unsupported

Question: Crashlytics showing error:

Encrypted interprocess sharing is currently unsupported.SharedGroup has been opened by pid: 15277. Current pid is 23384. in io_realm_internal_SharedGroup.cpp line 113.

But, unable to reproduce the error. Done google search but not found any satisfactory answer
Comment: See the information here https://github.com/realm/realm-java/issues/1477#issuecomment-235147984 . I think it is only happened once after user updates the apk, right?
Comment: actually you are creating the Realm object in a thread and using it in another one so this is not supported for now... create a new Realm instance and use it
Comment: It says you have an encrypted Realm, and you are trying to access it from multiple processes.
Comment: Actually i am unable to reproduce this issue. I found this on crashlytics
Comment: General cleanup, allowing the error and request to be read more efficiently.
Comment: When  we make the call on same file more than once on encrypted Ream then i am getting this issue
Answer: I was facing this issue on simulator and I solved this by copying the realm files from Device directory to Home directory.


How to add jQuery code only to the node add/edit page?

Question: I'd like to execute some jQuery code on a particular element in the node add/edit page of a particular content type. Currently I do that by putting
$(document).ready(function() {
    if (window.location.href.indexOf("node/add/CONTENT_TYPE") > -1) {
        ACTION;
    }
});

in the theme's .js file. It works, but I think the approach is probably not appropriate, as the test unnecessarily runs everywhere.
Could you please suggest an appropriate way?
Answer: First, you need to convert your script to be a behavior. Drupal will call defined behaviors, so document ready is not necessary.
Second, you can add this script to specific form pages with drupal_add_js and hook_form_alter.

Use hook_form_alter to hook into node add and node edit forms. You can do a check on the $form_id argument to ensure it is a node form.
Use drupal_add_js to add your script file.

Example:
function mymodule_form_alter(&$form, $form_state, $form_id) {
  if (preg_match('/node_form/i', $form_id) {
    drupal_add_js(drupal_get_path('module', 'my_module') . '/script.js');
  }
}

This file will only load when these forms are built, so, you can also remove the line that is checking the window href in the javascript.
The code above is untested, but a general start in the right direction. This code can be placed in a custom module, or, if your admin uses the same theme, you can place it in the template.php of the theme. I strongly recommend a simple custom module.
Edit
I believe the form id is preprended by the content type in 6. So, to isolate it to one content type:
function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'content_type_node_form') {
    drupal_add_js(drupal_get_path('module', 'my_module') . '/script.js');
  }
}

Or you can use hook_form_FORM_ID_alter to target specific forms too:
function mymodule_form_content_type_form_alter(&$form, $form_state, $form_id) {
  drupal_add_js(drupal_get_path('module', 'my_module') . '/script.js');
}

Where content_type is the name of any given content type (article, page, etc).
Example behavior (mymodule/script.js):
(function ($) {
  // you can define multiple behaviors

  Drupal.behaviors.mymodule_behaviorname_one = function(context) {
    // this code should fire automatically when the page has loaded
    console.log('Hello!');
    alert('Hello!');

    // attaching to an element
    $("a", context).click(function(e) {
      e.preventDefault();
      alert('You clicked a link.');
    }
  }

  Drupal.behaviors.mymodule_behaviorname_two = function(context) {
    // this code should fire automatically when the page has loaded
    console.log('Behavior 2');
  }
}(jQuery));

More on JS in Drupal (there are some examples in here for 6.x): https://www.drupal.org/docs/7/theming/working-with-javascript-and-jquery
Comment: I am not sure you need to worry about the field, you just want to get the script loaded in the page. After that, you can target js for elements with the standard jQuery selector.
Comment: The module will literally only consist of the hook_form_alter code, and the js script file. It will be a great entry point into both learning how to make a custom module, and interact with Drupal in one of its most powerful hooks.
Comment: The form id is not the literal form attribute - it is the Drupal identifier of the form. You can see what the form ID is by 'print $form_id; exit;' - or by using the devel module to debug.
Comment: Here is another question that is similar regarding understanding what form id is, even though it is for D8, it still applies: http://drupal.stackexchange.com/a/229641/57
Comment: Without a debugger it is virtually impossible to develop in Drupal (or any platform for that matter). Devel is a major help. Otherwise it is like flying blind. I will add an example behavior.
Comment: For instance, you can figure out the form id quickly by doing `dpm($form_id);` - boom, next. No more guessing.
Comment: Within hook_form_alter in your custom module.
Comment: I'm working on implementing your approach now. (Never created my own module before.) What if that element I'm targeting is a CCK field? https://www.drupal.org/node/726282 seems to suggest an issue.
Comment: The ID of the form is quite generic (``). How do I be specific to a particular content type?
Comment: Gosh replacing that inappropriate code of mine seems to require climbing quite a steep learning curve. I have to figure out how to use `Drupal.behaviors`, then a custom module, then the Devel module. Would it be possible for you to give me an example code in `my_module/script.js` and `my_module/my_module.module` if for example I'd like to display `alert("Hello World!")` when the node add page of a custom content type whose machine-readable name is `foofoo` is loaded?
Comment: Thank you very much. As to the debugger, yeah I couldn't agree more. I managed to get by with just Contemplate and Views Custom Field for quite a long time. Looks like I can't avoid deeper stuff any longer.
Comment: Please pardon my ignorance for now... Where exactly do I execute `dpm($form_id);`?
Comment: Thank you very much for your help and patience. I understand what's going on and what to do now, and feel much less limited about what I can do with the website. By the way a closing parenthesis is missing for the `$("a", context).click` block.


Migrating Angular JS 1.6.10 to 1.7.8

Question: I need to migrate my AngularJS version from 1.6.10 to 1.7.8. I would like to know will there be any code breakage after migration?
If anyone could provide the list of differences between 1.6.x and 1.7.x would be much more helpful.
Comment: did you referred these documents?
https://docs.angularjs.org/guide/migration
https://github.com/angular/angular.js/blob/master/CHANGELOG.md
Comment: Yes Jeff, thank you.
Answer: Migrating from (AngularJS) 1.6 to 1.7 contains the information you're looking for. The most notable changes are:

$resource has now support for request and requestError interceptors
Several deprecated features have been removed:

the $controllerProvider.allowGlobals() flag
the $compileProvider.preAssignBindingsEnabled() flag
the angular.lowercase and angular.uppercase methods
the $cookieStore service from the ngCookies module
the ngClick override directive and corresponding services from the ngTouch module
the complete ngScenario module

In the linked resource there is also the full list of breaking changes.
The link for the specific version 1.7.8 is: https://code.angularjs.org/1.7.8/docs/guide/migration
Comment: Thanks @lealceldeiro It's really helpful.

Migrating Angular JS 1.6.10 to 1.7.8


erro ao executar run-android com react-native

Question: Bom, após conseguir executar o comando inicial e criar o projeto(react-native init ) e estando com meu emulador android rodando na maquina (Genymotion) porém ao executar run-android ele acusa esso erro :
<code>
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Could not find tools.jar. Please check that C:\Program Files\Java\jre1.8.0_211 contains a valid JDK installation.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.4.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 3m 16s
12 actionable tasks: 1 executed, 11 up-to-date
error Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/getting-started.html
error Command failed: gradlew.bat app:installDebug. Run CLI with --verbose flag for more details.```
</code>
Answer: A configuração do React Native é cheia de nuances. Por esse motivo, recomendo você refazer novamente as configurações utilizando a própria documentação do React Native. Segue o link da documentação: https://facebook.github.io/react-native/docs/getting-started (Só não esqueça de selecionar o React Native CLI Quickstart).


Kalman filter transition matrix

Question: Hi guys I am trying to writ e a code on python to correct forecast data using Kalman Filter.
I am following the equations and recommendations in this link : http://www.swarthmore.edu/NatSci/echeeve1/Ref/Kalman/ScalarKalman.html.
At some point, I have written a function which satisfies me since I don't know whatsoever how to use the pykalman module to correct estimations.
I am using just one parameter (unidimensional and univariate time series), so my matrices form will be a numerical value.
My main problem is what is the transition matrix? In many paper they just use a transition matrix of 1 but I think this is not rigorous since it would mean that:
forecast(t) = A * forecast(t-1)
We do not have a constant value of forecast so what would be my transition matrix?
Hypothesis 1:
Can I just try to see a correlation coefficient between two estimated values then take the mean of those coefficients for my first day of forecast.By the way this transition matrix will be just an initialization and will change for each forecast following
Comment: what's your model?
Comment: I like the welch description and notation.  https://www.cs.unc.edu/~welch/media/pdf/kalman_intro.pdf
Answer: The transition matrix relates state t and state t-1.
If we write the temporal coherence equation like this
$$ x_t = \Psi x_{t-1} + \epsilon_p $$
This is the temporal model. This model tells you what is the tendency of your system. When no  measurement is found, the system will follow this tendency. When it is found, there is a trade-off between where the measurement says the track should go and where the temporal model says it should go.
$\Psi$ is the transition matrix then.
You can have different types of transition matrix, for instance, temporal brownian motion, where $\Psi = I $, meaning that the next state is the last one plus some noise.
Another possibility would be constant velocity.
Imagine an easy example in 1d. We are tracking the position of an object and its velocity. It is just the same equation as above, in this particular case.
$$ \begin{bmatrix}
x_t\\
vel_t
\end{bmatrix}
=
\begin{bmatrix}
1 & 1\\
0 & 1
\end{bmatrix}
\begin{bmatrix}
x_t\\
vel_{t-1}
\end{bmatrix}
+ \epsilon_p
$$
Then, if you multiply terms, you get
$$ x_t = x_t + vel_t + \epsilon_{p,x}$$
$$ vel_t = vel_{t-1} + \epsilon_{p,vel}$$
This example would be, as the second equation tells us, a constant velocity model.
If you still have doubts, there is a nice explanation of Kalman Filter  here:
http://web4.cs.ucl.ac.uk/staff/s.prince/book/book.pdf
Chapter 19.
Answer: I think you should consult a clearer description. (I like a lot the one from Matlab (which you can easily port).
Especially your formula is wrong it is:
predicted state space $\tilde X(t) = A * $corrected state space $\hat X(t-1)$
the forecast would be $h(X)$.
Most important even though you just observe one dimension your state space can be much bigger. Your idea with correlation coefficient would imply a two dimensional one.
Comment: Thanks can you help me on one of my post here:http://stackoverflow.com/questions/24736263/what-does-observation-offset-and-predicted-state-mean-mean-in-pykalman-stand
Comment: Sorry - I'm not a python user. So you will need someone else.

Kalman filter transition matrix


What is a good fitness function for an AI of a zero-sum game?

Question: I am making an AI for a zero-sum 4-player board game. It's actually not zero-sum (the 4 players will "die" when they lose all their lives, so there will be a player who died first, second, third and a player who survived. However, I am telling the AI that only surviving counts as a win and anything else is a lose) After some research, I figured I would use a minimax algorithm in combination with a heuristic function. I came across this question and decided to do the same as the OP of that question - write an evolutionary algorithm that gives me the best weights.
However, my heuristic function is different from the one the OP of that question had. Mine takes 9 weights and is a lot slower, so I can't let the agents play 1000 games (takes too much time) or breed them with the crossover method (how do I do a crossover with 9 weights?).
So I decided to come up with my own method of determining fitness and breeding. And this question is only about the fitness function.
Here are my attempts at this.
First Attempt
For each agent A in a randomly generated population of 50 agents, select 3 more agents from the population (with replacement but not the same agent as A itself) and let the 4 agents play a game where A is the first player. Select another 3 and play a game where A is the second player, and so on. For each of these 4 games, if A died first, its fitness does not change. If A died second, its fitness is increased by 1. If it died third, its fitness is increased by 2. If it survived, its fitness is increased by 3. Therefore, I concluded that the highest fitness one can get is 12 (surviving/wining all 4 games -> 3 + 3 + 3 + 3).
I ran this for 7 generations and starting from the first generation, the highest fitness is as high as 10. And I calculated the average fitness of the top 10 agents, but the average didn't increase a bit throughout the 7 generations. It even decreased a little.
I think the reason why this didn't work is because there's gotta be a few agents that got lucky and got some poor performing agents as its opponents.
Second Attempt
The game setups are the same as my first attempt but instead of measuring the results of each game, I decided to measure how many moves did that agent make before it died.
After 7 generations the average fitness of top 10 does increase but still not increasing as much as I think it should.
I think the reason why this failed is that the game is finite, so there is a finite number of moves you can make before you die and the top performing agents pretty much reached that limit. There is no room for growth. Another reason is that the fitness of the player who survived and the fitness of the player who died third differs little.
What I want
From my understanding of EAs (correct me if I'm wrong), the average fitness should increase and the top performing individual's fitness should not decrease over time.
My two attempts failed at both of these. Since the opponents are randomly selected, the top performing agent in generation 1 might get stronger opponents in the next generation, and thus its fitness decreases.
Notes
In my attempts, the agents play 200 games each generation and each generation takes up to 3 hours, so I don't want to let them play too many games.
How can I write a fitness function like this?
Answer: Seven generations doesn't seem like nearly enough to get a useful result. Especially for a game, I would expect something like 200+ generations to be more realistic. You could do a number of things:

Implement elitism in order to ensure the survival of the best individual(s).
The strength of evolution stems from repeated mutation and crossover, so I'd recommend letting the agents play only a few games per generation (say, 5 ~ 10), at least at the beginning, and then evolve the population. You might even want to do only one game per generation.
In this regard, you could adopt a continuous evolution strategy. What this means is that as soon as an agent dies, they are subjected to mutation, and as soon as an agent wins, they can produce offspring. Or any combination of the two. The point is that the tournament is ongoing, everyone can play against anyone else. This is a little more "organic" in the sense that it does not have strictly defined generations, but it should speed up the process (especially if you can parallelise the evaluation).

I hope that helps. The accepted answer in the post you referenced has a good suggestion about the way you could implement crossover.
Comment: But what I am seeing here is that the agents' fitnesses don't improve. I kinda want to see the agents' "growth". If I use continuous evolution I can't get the fitness of each agent right?
Comment: Also, I have 9 weights as my chromosome. How do I do a crossover with 9 weights? Is it just randomly pick x weights from one parent and (9-x) weights from the other parent?
Comment: Unless I'm missing something, based on how you described the game, you would be able to get the fitness of any individual as soon as they die or win, isn't that right? Say you have a population of 64 (just to make it divisible by 4). You make 16 random groups of 4 agents, each group playing a game. The first one to die in any group gets a fitness of 0, the second gets 1 and so on. But as soon as an agent dies, they are free to mutate and join in another game, as long as there are 3 other free agents (who either died or won, either way).
Comment: Oh I see what you mean now :). What about the crossover? Do I just pick a few weights from one parent and another few from the other parent and use these to create the offspring?
Comment: About crossover: yes, you would randomly pick x weights from one parent and (9-x) from the other. There might be a case for what the person in the referenced post had (each weight in the offspring is the average of the corresponding weights from the two parents). My gut feeling is that the offspring wouldn't be able to improve on the fitter parent, but you'd have to see if that is the case in practice.
Comment: Also, looking at the average fitness of the agents can be deceptive. Usually you want one of the agents to solve the problem at hand - be it a game or some optimisation problem - but you don't expect the population to kind of "reach consensus" about the right solution. One of the strengths of evolution is that it is good at exploration, which means that some agents will necessarily be better than others. So that's why I would suggest a smaller number of games per generation and more generations to give the agents a chance to optimise their behaviour.
Comment: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157657/discussion-between-sweeper-and-cantordust).
Comment: Just to clarify my comment above about crossover. When I said that I think the offspring wouldn't be able to improve on the fitter parent, I was referring to the strategy of taking the average of the weights. One thing I have found to work well in practice is to select a weight from one parent or the other in proportion to their fitness. Say one parent has a fitness of 6, the other 2. So you are three times more likely to select a weight from the fitter parent than from the less fit one.


Only Numbers, dots, comma and slash

Question: I am trying to create a regex that will accept as values only the following
1
1.1
.1
1,1
,1
1/1
11
111
1111
and so on...

or else: A number that may be followed by a dot OR a comma OR a slash and a number at the end.
\d*(?:\.|\,|\/)\d

using this regex I only capture the 1.1 .1 1,1 ,1 but I cant capture if the user will enter only a number eg 1 or 11 or any integer number with unknown number of digits.
Comment: Which version of regex are you using?
Comment: I want to use it as a pattern in a HTML input box I don't know the version.
Answer: The regex can be modified as
\d*([.,\/]?\d+)

Regex Demo
Comment: Thanks for the answer, while testing this regexpr in regexr.com and in expresso software it doesn't capture the values I want.
Comment: @user3677132 I had a small typo in the answer. Corrected. Please see the edit
Comment: I checked it again and it worked. It doesn't although capture the .1 or ,1 but that's not mandatory for the project I create. Thanks a lot.
Comment: I am happy with the first one no need to use the second one, you have got me covered. Thank you very, very much.
Answer: Here's a short one that I think meets your needs:
\d?[.,\/]?\d+

The pattern is
\d? : one optional digit to match 1.1, 1/1 or 1,1. If 1111.1111 etc are valid, change to \d*
[.,/]? : an optional single period, comma or slash
\d+ : one or more digits.
reading "or a dot (...) and a number at the end", this might be more accurate (maybe you mean one and only one digit after the dot/slash/comma?)
\d*[.,\/]?\d

Which is
\d* : Any number of digits
[.,/]?: An optional delimiter
\d : a single digit
Answer: If you are validating a user input on keypress or something,
you will need this form  1, too.
This should cover all the bases.
 #  ^(?:\d+(?:[.,/]\d*)?|[.,/]\d+)$

 ^
 (?:
      \d+                 # Forms 1 or 1, or 1,1
      (?: [.,/] \d* )?
   |                    # or,
      [.,/] \d+           # Form ,1 only
 )
 $

Answer: \d*(?:\.|\,|\/\d+)*

Try this.See demo.
https://www.regex101.com/r/fG5pZ8/21
Comment: Interesting. I'm not sure if the non-capturing group is needed, or whether that grouping needs to be greedy. I think it depends on whether `126.96.36.199.18.104.22.168.1` or similar would be part of the test data

Only Numbers, dots, comma and slash


Relatório com date_trunc incluindo valores zerados

Question: Estou fazendo uma consulta no postgres, porém ela só trás os resultados aonde a instância de fiscalizações não está zerada, precisava que essa consulta trouxesse todos os resultados das 8 as 18, inclusive os zerados:
COMO TRAZ:

COMO QUERIA QUE TROUXESSE:

QUERY:
select (
          date_trunc('hour', data)

           )                 intervalo_data,
       count(id) fiscalizacoes
from fiscalizacoes
where data >= '2019-06-21 08:00:0.000000'
  and data <= '2019-06-21 18:00:00.000000'
group by intervalo_data
order by intervalo_data;

Comment: está usando `date_trunc` no select mas está agrupando com a data normal? não parece que vai funcionar como espera, tem que deixar o group by igual ao select
Comment: O agrupamento está funcionando, eu executei e devolveu  o que eu esperava, até porque tenho que trazer o número de fiscalizações por intervalo de 1 hora, das 8 as 18.
Comment: não é que não esteja funcionando, mas ele agrupa pela data inteira sem truncar horas. Existem todas essas horas que não está retornando, por exemplo 11:00:00?
Comment: agrupei pelo date_trunc('hour', data) e continua com o mesmo resultado, não está ignorando os resultados
Comment: mas se não agrupar, existe registro nas horas que estão faltando?
Comment: não existe, é esta a minha pergunta, se tem como eu retornar 0 para as horas que não possuem instância.
Comment: ah agora entendi :)
Answer: Sim é possível gerar as datas todas usando a função generate_series:
generate_series(timestamp '2019-06-21 00:00:0.00000', '2019-06-21 23:00:0.00000', '1 hour')
Basicamente gera uma série de datas(timestamp, mas poderia ser int, e gerar um intervalo de números inteiros por exemplo), tem o início e fim, e ao final o intervalo desejado, que é em horas (cada 1 hora).
A partir daí, basta fazer um LEFT JOIN com a tabela (LEFT pois queremos preservar as datas que não houverem na tabela "fiscalizacoes") e agrupar normalmente:
SELECT  date_trunc('hour', datas.data) intervalo_data,
 count(f.id) fiscalizacoes
  FROM (
       SELECT day::timestamp as data
         FROM generate_series(timestamp '2019-06-21 00:00:0.00000', '2019-06-21 23:00:0.00000', '1 hour') day
       ) datas
LEFT JOIN fiscalizacoes f ON f.data = datas.data
GROUP BY intervalo_data
ORDER BY intervalo_data;

Coloquei um exemplo funcionando aqui: SQL Fiddle
Comment: eu tb agradeço, eu tinha visto essa função para números inteiros e não sabia se gerava datas tb, ai pesquisei e montei essa resposta, acabei aprendendo algo novo :)
Comment: valeu muito obrigado, funcionou.


Text recommendations for linear algebra (tensors, jordan forms)

Question: I'm having extreme difficulty trying to understand to topic of tensor products, freespaces, and jordan forms.
Are there any text books that take an elementary approach to these topics that you may recommend?
I am currently using advanced linear algebra by roman, but other sources would be appreciated. Thanks!
Answer: Peter Lax's book is usually highly recommended for Linear Algebra. I believe it covers Jordan Forms and Tensor Products, but only does so in the appendices.
For tensors, any good book on general relativity would serve you well (Das, Schutz, to name a few). However, you can also check a Quick introduction to tensor analysis which is self contained and quite good.
I've heard very good things about Matrix Analysis by Horn, which seems to cover Jordan forms in depth.
I'm sorry to say I don't have anything for free spaces.
Comment: Thanks for the suggestions! I'll look into them.
Comment: Nice reference! +1


SSH key fingerprint not matching after server hack (Bluehost Wordpress). How to identify issue?

Question: Detected a hack from this morning on a Bluehost server Wordpress installation. Someone used credentials from a Wordpress user account with admin rights, I'm dealing with the aftermath now. I know, I failed restricting other peoples rights in time. Sigh.
Restored right away a clean Wordpress backup (via phpmyadmin database import) and removed the affected admin account right after. Removed all ftp accounts I didn't setup. Looking pretty clean again now at least from the cpanel view from Bluehost. I'm the only admin.
I haven't done a SSH session yet. Because the SSH fingerprint key is not matching anymore. My ssh client had previously stored the public key in .ssh/known_hosts. DNS spoof excluded (I control domain and have same problem with SSH calling the IP address).
server: ssh-keygen -l -f id_rsa
2048 SHA256:F6Q/VrkvaIM4e/2Z49c5VLzbI43eMa3POwENPnQVSuA id_rsa.pub (RSA)
client: ssh  (after removing previous public key from .ssh/known_hosts)
ED25519 key fingerprint is SHA256:VPqWe8U9SxpgSvw/dlhsrfMuGf6EVwsnPzTOrdqSJso.
servers .ssh/id_rsa.pub file date since is from when generated those keys years back. However it doesn't match exactly the key stored in clients .ssh/known_hosts Interesting is that the first 28 characters do match, rest is different.
Why is there a fingerprint mismatch?
Could something else be altered which I haven't found yet?
Concerned of accessing the server via ssh with new keys (?) and entering my admin password in a ssh.
Any help greatly appreciated.
Thanks!
Dirk
Comment: I am not sure that is the current status or situation, but i dont worry to much regarding the fingerprint, I would suggest clear all keys on your server and sure when you connect the first time you will see the fingerprint, but at least you are sure you have a clear keys in place and no one can access on your server using those keys
Comment: Additionally, questions involving web management panels like cpanel are off topic.
Answer: In .ssh/known_hosts you keep a fingerprint key of your server, not a public part of ssh key.
Most probably you have a rootkit on your server.
It means that restoring data from the backup will not have effect.
You need a full re-installation the server(order a brand new server) or run a  script which locally checks for signs of rootkit.
Comment: I wouldn't suggest a rootkit check script as an equal option to full re-installation of the server. There is still relatively high chance that the rootkit detection doesn't detect the particular rootkit that there is. Full re-installation is the only reasonable option.


Receber um array no controller a partir do javascript

Question: Estou a criar um array com uma lista de dados em javascript:
var myArray = gvSortingListagemGARs.keys;
Onde tenho o resultado:
[278, 279, 280, 281, 282]

Que são id's de elementos de uma tabela Devexpress. Agora estou a tentar passar este array para o o meu controller, só está sempre a receber null.
A minha função js
function downloadListaGARSTratadas() {
    var myArray = gvSortingListagemGARs.keys;
    alert(myArray);
    window.location.href = "/GAR/downloadListaGARsTratadas?GarsFiltro=" + myArray +"";
}

Função no controller
public ActionResult downloadListaGARsTratadas(int[] GarsFiltro)
{ ... }

Caso faça string em vez de int[] recebo 278, 279, 280, 281, 282, ou seja a variável. Não é possível receber mesmo com um array de inteiros?
Comment: Você dever especificar que se trata de um array assim: `window.location.href = "/GAR/downloadListaGARsTratadas?GarsFiltro[]=" + myArray +""`, de uma olhada [nesse link](http://stackoverflow.com/questions/6243051/how-to-pass-an-array-within-a-query-string) do SO-EN e [nesse](http://stackoverflow.com/questions/1763508/passing-arrays-as-url-parameter) que tratam esse assunto.
Comment: Sim, e até faz sentido usar `GarsFiltro[]=" + myArray +""`, mas continuo a receber `null` na mesma
Comment: Ok. é que esqueci de uma detalhe vou postar como resposta se não resolver eu removo.
Comment: Você não esta utilizando o JQuery? Você teria mais facilidade e controle fazendo uma chamada ajax.
Comment: Sim @lionbtt, estou a utilizar jquery. Acabei por fazer com javascript, mas também posso tentar com ajax. Apesar de não achar que o problema seja daí
Answer: Você dever especificar que se trata de um array assim e passar cada valor como um parâmetro, só que todos com o mesmo nome, por exemplo:
/?arr[]=foo+bar&arr[]=baz&arr[]=foo

Ou ainda assim para algums casos (como o ASP MVC):
/?arr=foo+bar&arr=baz&arr=foo

Como demostrado nesse e nesse tópico do SO-EN e nesse post
Então em seu caso você deve fazer algo do tipo:
window.location.href = "/GAR/downloadListaGARsTratadas?GarsFiltro=1&GarsFiltro=2&GarsFiltro=3"

Para gerar esse parâmetro de forma dinâmica criei esse método:
function formatQueryStringURLParamArray(key, array){
    var param = "";
    for(var item in array){
        if(param.length > 0)
            param += "&";
        param += key + "=" + item;
    }
    return param;
}

Que pode ser chamado dessa forma, retornando a querystring formatada:
var param = formatQueryStringURLParamArray("key", myArray);

Aqui tem um exemplo online.
Exemplo com Web API
Tente o seguinte teste (eu utilizo WebApi ASP MVC, mas é bem similar a MVC):
Criei o seguinte ApiController:
public class Test2Controller : ApiController
{
    [HttpGet]
    public virtual int Get([FromUri]int[] i)
    {
        return i.Length;
    }
}

E fiz a seguinte requisição via url em meu browser:
http://localhost:59402/api/test2/?i[]=1&i[]=2&i[]=3

E recebi corretamente o array de inteiros.

Não sei se MVC usa isso, pois não trabalho com MVC apenas WebAPi, mas tente adicionar [FromUri] antes de seu parâmetro de array no método do Controller.

Exemplo com ASP MVC
Criei esse Controller:
public class Test3Controller : Controller
{
    [System.Web.Http.HttpGet]
    public ActionResult Index([FromUri]int[] i)
    {
        return Json(i, JsonRequestBehavior.AllowGet);
    }
}

E fiz a seguinte requisição via url em meu browser:
http://localhost:59402/test3/?i=1&i=2&i=3

Note: não sei por que razão o MVC não entendia quando eu passava i[], então se eu passo apenas i={valor}, ele entende. Já Web API funcionava das duas formas: i={valor} e i[]={valor}.
Comment: O que estas a dizer, é que para cada elemento do array tenho de fazer um `GarsFiltro[]=` ?
Comment: @CesarMiguel, Sim, não encontrei documentação que diga isso, mas é assim que se faz para passar array por querystring. Só não sei se com asp mvc isso funciona. Tentou?
Comment: Sim tentei agora, não recebo na mesma o `[1,2,3]` que devia receber usando o teu exemplo
Comment: @CesarMiguel, está a receber nulo? Ou outra coisa?
Comment: Estou a receber nulo
Comment: @CesarMiguel, de uma olhada no exemplo que fiz com WebApi, na edição da resposta, e verifique seu controller.
Comment: @CesarMiguel, de uma olhada na edição adicionei um exemplo com ASP MVC.
Comment: já vi a tua solução e continuava a não conseguir... Vou postar aqui uma resposta com uma "solução" deste problema
Answer: @CesarMiguel, o erro que você esta tendo não tem nada a ver, porém segue uma segunda opção de realizar esse processo via ajax e utilizando Json.
var myArray = gvSortingListagemGARs.keys;
var jsonString = JSON.stringify(myArray);
   $.ajax({
        type: "POST",
        url: "/GAR/downloadListaGARsTratadas",
        data: {data : jsonString},
        cache: false,

        success: function(){
            alert("OK");
        }
});

Comment: Por ajax continua a dar o mesmo erro. Tive de adicionar `jQuery.ajaxSettings.traditional = true;` antes de fazer o pedido (funcionou)
Answer: Estava com um problema semelhante e encontrei o seguinte link:
https://stackoverflow.com/questions/21288462/c-sharp-mvc-4-passing-javascript-array-in-view-to-controller
Veja se isso te ajuda:
Existe um parâmetro, chamado traditional, que você configura no Ajax como true que envia o array javascript certo para o seu controller. traditional : true,
Tenho um array, chamado turno, no meu javascript que irei passar para o meu Controller via Ajax.
Turno é um array com os dias da semana... turno = ["Sunday", "Monday", "Tuesday", 4 mais...]
No javascript tenho a chamada via Ajax...
$.ajax({
        url: getBaseUrl() +  "GraphicsReport/FiltraDadosGrafico1",
        type: "POST",
        dataType: "json",
        traditional : true,
        data: {cDataInicial: dataInicial, cDataFinal: dataFinal, cLinha: linha, cTurno: turno, cModelo: modelo},
        cache: false,
        success: function (data) {
            //Faz alguma coisa...
        },
        error: function (data) {
           //Faz alguma coisa...
        }
    });

No meu controller recebo...
public JsonResult FiltraDadosGrafico1(string cDataInicial, string cDataFinal, string cLinha, Array cTurno, string cModelo)
{
  string retCode = "Success";

  //Faz alguma coisa e retorna um Json

  return Json(new { retorno = retCode } );
}

Veja se isso pode te ajudar. Para mim, resolveu...
Answer: Como não consegui resolver este problema retornando uma lista de inteiros para o controller, vou deixar aqui uma resposta com a minha solução (se bem que não era assim que gostava de resolver):
Na função javascript retorno o array para o controller como já tinha feito:
function downloadListaGARSTratadas() {
    var myArray = gvSortingListagemGARs.keys;
    window.location.href = "/GAR/downloadListaGARsTratadas?GarsFiltro=" +myArray +"";
}

No controller em vez de receber um array de inteiros, mandei receber uma string, devolvendo os valores como 201,202,203,204:
public FileResult downloadListaGARsTratadas(string GarsFiltro)
{
   var arr = GarsFiltro.Split(',');
}

Fiz um split para a variável arr e ele cria o array
Comment: Cesar, o nome disso é "Gambiarra", hehe, isso não é uma boa abordagem, eu testei aqui com WebApi e MVC no Asp.net (MVC 4) e funcionou [minha solução](http://pt.stackoverflow.com/a/27225/2998) perfeitamente, recomendo refazer [meu exemplo simples](http://pt.stackoverflow.com/a/27225/2998) em seu ambiente e verificar por que não está funcionando (Te garanto que funciona). Se tiver alguma duvida posso te ajudar.
Comment: @Fernando, fui testar agora o teu exemplo e realmente funciona!


Prove Axiom $10$ (Vector Spaces) independent of the others

Question:
Possible Duplicate:
Is it possible to construct a quasi-vectorial space without an identity element?

In Apostol Multivariable Calculus, $1.5$ exercise $30 b$, he asks the reader to prove that Axiom $10$ is independent of the other axioms for vector spaces.  Axiom $10$ says that $$1 * x=x$$
To do this, it is equivalent to prove that $(-1)*x\not = -x$, since $(-1)*x=-x$ implies that $1*x=x$ (we can prove it using the other axioms.  My approach is to find $(V,+,*)$ that satisfies the first $9$ axioms and contradicts the axiom $10$.
My attempts so far:
First, I tried setting $V=\mathbb{R}$, redefining $a*x$ to be variations on multiplication.  For example, I tried $a*x=|ax|$ but ran into problems with $|a(x+y)|\not=|ax|+|ay|$ in general.
Then, I tried setting $V=\mathbb{R^2}$ and $a*x=ax^T$ if $a$ is positive and $a*x=ax$ if $a$ is not positive.  However, I ran into problems with $(a+b)*x\not=a*x+b*x$ for $a=-1$,$b=1$,$x=(1,0)$.
Answer: Take any additive group $V$ and let $F$ be a field. Define a scalar product by $k \cdot v = 0$ for all $k \in F$ and $v \in V$, I believe this satisfies all the other axioms except $1 \cdot v = v$.
Comment: Soo simple...I can't believe I missed it.


How do I write "piano style" cadences?

Question: I'm currently undertaking the AMEB theory grades. I'm up to grade 4 and am working on different types of cadences but I STILL don't understand how "piano style" (the one with the bass note in the bass and the other 3 notes in the treble) cadences work, how to write them, or the general rules of them. I seem to almost ALWAYS get the answer wrong. Every time I go to Google it, I can't find anything relevant because I guess the words 'piano' + 'style' put together aren't very distinct. Can anyone point me in the right direction?
Comment: "Piano style" sounds like a term specific to your teacher / school. It isn't a standard musical term. Talk to your teacher about your confusion.
Comment: also know as "Pianoforte Style" cadences
Answer: I believe the term you're looking for is keyboard style. To help your Internet searches, try "keyboard style voice leading."
In keyboard style, you write three-voice chords in the upper staff and a single bass voice in the lower staff; this is an attempt to mimic a standard keyboard texture:

This is in contrast to chorale style, sometimes called SATB style, where you mimic the soprano–alto–tenor–bass orchestration of a choir. Oftentimes chorale style simply moves one voice of keyboard style by an octave:

As for rules for writing cadences, it's a bit too broad to pinpoint exactly what you're asking, but here are some general rules:

The leading tone (scale-degree 7) must resolve up by step to tonic when it is in an outer voice (soprano or bass). If it is in an inner voice (alto or tenor), it can move down to scale-degree 5 (or, more rarely, up to scale-degree 3). Not knowing the AMEB system, I recommend resolving all leading tones up to tonic.
Since scale-degree 5 is present in both the V and I chords, just keep scale-degree 5 where it is (except for the bass, which will tend to move to scale-degree 1).
If you have a chordal seventh, this chordal seventh must resolve down by step. In a V7 chord, this will be scale-degree 4 moving down to scale-degree 3.
The remaining scale degree, scale-degree 2, can move up to scale-degree 3 or down to scale-degree 1.

The above are just broad tips assuming you're looking at V(7)–I cadences. But most importantly, the term keyboard style should help you access the necessary AMEB information online. Best of luck!
Comment: It is worth noting that for keyboard/pianoforte style:  the notes in the treble staff must all be within one octave (so you can reach all the keys with one hand)

How do I write "piano style" cadences?


How to evaluate $\lim \limits_{n \to \infty} ((n+1)\ln(n+1))^c-(n\ln(n))^c$ where $c \in (0,1)$?

Question: The original question is that I want to evaluate
$$\lim \limits_{n \to \infty} ((n+1)\ln(n+1))^{\frac{1}{2}}-(n\ln(n))^{\frac{1}{2}}.$$
Now I want to find if for any $c \in (0,1)$, $$\lim \limits_{n \to \infty} ((n+1)\ln(n+1))^c-(n\ln(n))^c=0$$ holds.
I prefer to evaluate it by definition, Cauchy sequence, or squeeze lemma. I don't want to use L'Hospital rule. I think I only need a hint.
For the original question, I have tried to represent $$((n+1)\ln(n+1))^{\frac{1}{2}}-(n\ln(n))^{\frac{1}{2}}$$ as $$(n\ln(n))^{\frac{1}{2}}(((1+\frac{1}{n})(1+\frac{\ln(1+\frac{1}{n})}{\ln(n)}))^{\frac{1}{2}}-1)$$
but lost insight to continue. That is a dull idea. But was it not $\frac{1}{2}$, this kind of idea would turn to be the only way I can think, again. Am I on the right way?
Comment: One way is to use $f(n+1) - f(n) = f'(n + \theta_n)$ for some $\theta_n\in (0,1)$, since the function $x \mapsto (x\ln x)^c$ is differentiable on $(1,+\infty)$.
Comment: Hint: often the trick helps $\sqrt{a}-\sqrt{b}=\frac{a-b}{\sqrt{a}+\sqrt{b}}$.
Comment: @A.G. Ah, how can I didn't notice it at all... but if it is not $\frac{1}{2}$, this trick seems to fail...
Comment: Still possible to use the idea that $x^n-y^n=(x-y)(x^{n-1}+\ldots+y^{n-1})$, but it becomes messier.
Comment: @A.G., $c$ does not need to be integer.
Comment: @DanielFischer That seems great! But if... If I want to keep the function idea away (which is, I think, in some degree more general then concept of sequence), I want to know if there is still a way? ;)
Comment: I don't see any neat tricks for general $c\in (0,1)$. Writing the difference as $$(n\ln n)^c\bigl( (1 + R_n)^c - 1\bigr)$$ works, of course. But it's not particularly exciting.
Answer: By Lagrange's theorem
$$ ((n+1)\log(n+1))^c- (n\log(n))^c $$
is the value of the derivative of $f(x)=\left(x\log x\right)^{c}$ at some point $x_0\in(n,n+1)$.
Since $f'(x) = c(\log x+1) \left(x\log x\right)^{c-1}$,
$$ \lim_{n\to +\infty} ((n+1)\log(n+1))^c- (n\log(n))^c = 0 $$
for any $c\in(0,1)$.
Comment: Mine agrees with yours. Comforting.
Answer: $\begin{array}\\
((n+1)\log(n+1))^c- (n\log(n))^c
&=(n+1)^c(\log n+\log(1+1/n))^c- (n\log(n))^c\\
&=n^c(1+1/n)^c(\log n)^c(1+\log(1+1/n)/\log n)^c- (n\log(n))^c\\
&\approx n^c(1+c/n)(\log n)^c(1+1/(n\log n))^c- (n\log(n))^c\\
&\approx n^c(1+c/n)(\log n)^c(1+c/(n\log n))- (n\log(n))^c\\
&\approx n^c(\log n)^c(1+c/n+c/(n\log n)+c/(n^2\log n))- (n\log(n))^c\\
&= n^c(\log n)^c(c/n+c/(n\log n)+c/(n^2\log n))\\
&= cn^{c-1}(\log n)^c(1+1/(\log n)+1/(n\log n))\\
&\approx cn^{c-1}(\log n)^c\\
&\to 0
\qquad\text{since } 0 < c < 1\\
\end{array}
$


What statistical test should I use to compare before and after drug treatment in a group of 8 subjects?

Question: I have a group of 8 subjects. For each subject I measure their heart rate 30,20, and 10 min before and after they take a drug. I then average the heart rate measurements in each subject to get one value for before and after the drug was taken. As a result, I have 8 before and 8 after averaged heart rate values. I  want to compare whether there is a difference in heart rate after the drug was taken. What test would be recommended in this scenario?
I currently use JMP software and conducted an ANOVA on the groups using 'fit Y by X' and received an F value of 0.157. However, when I blocked by the subjects, and used the 'Fit Model' option, the F value was 0.035. If I blocked by subject and used the 'fit Y by X' and conducted an ANOVA, this also yielded significance. One method returned a significant result while the other did not. Why is this? Which is correct? Is it correct to block in this scenario? Also, which statistical test should I use?
Answer: No am not familiar with JMP software. The first analysis you describe assumes that the after measurements are independent of the before measurements and this is clearly not the cases (you have paired data). Blocking by subject will provide you with the correct test of the effect of the treatment. A simpler approach, however, is a paired t-test. This tests the null hypothesis that the average within-subject change over time is zero. There are plenty of examples and info on the paired t-test online.


Event History Analysis (Discrete Time)-Substantive Effects Interpretation

Question: I am working on an event history analysis project.  I am using a discrete time survival model.  Specifically, I am using a logit model.  I am interested in examining the substantive effects. Can you plot the predicted probabilities?  So far, the only way I have seen scholars interpret the substantive effects is through the odds-ratios.  However, I generally find predicted probabilities more intuitive in interpreting GLM models.
Any insight would be greatly appreciated.
Answer: It turns out that the inverse cumulative distribution function (so-called quantile function) of the logistic distribution is a generalization of the logit function, see Wikipedia discussion here.
So, if you assume that the probabilities occurring in the logit ratio (log(p/1-p)) are following a uniform distribution, then one can interpret the logits in turns of the standardized (a mean of zero and a scale factor of one) cumulative logistic distribution, hence a probabilistic interpretation of the log-odds ratio.
Here is Wikipedia on this characterization of the logistic distribution:

Its cumulative distribution function is the logistic function, which appears in logistic regression and feedforward neural networks. It resembles the normal distribution in shape but has heavier tails (higher kurtosis). The logistic distribution is a special case of the Tukey lambda distribution.

Note, a Monte Carlo inversion method to simulate a Logistic random deviate is to replace p in the logistic function (and more generally than the standardized distribution, employing the quantile function) with a randomly generated uniform random deviate. I have employed this quite successfully in practice and on this forum used it to explore nonlinear estimators for Logistic distribution parameters.


Simplification of Taylor Series of ln(x) about 1

Question: While calculating the Taylor series of $ln(x)$ about the point $x_0=1$ I found the following:
$f(x) = \frac{1}{1!}(x-1)^1-\frac{1}{2!}(x-1)^2+\frac{2}{3!}(x-1)^3+…$
From this, I generalized the summation as $\sum\limits_{k=1}^{\infty}  (-1)^{n+1}\frac{(x-1)^n}{n!}(x-1)^n$
However, when I tried to verify my calculation on Wolfram Alpha this sum was written without the factorial in the denominator. Where exactly does the factorial go to, then?
Answer: Your numerators are actually $(n-1)!$, so $(n-1)!/n!=1/n$.


Quick Integration Clarification

Question: $ \int_ {-\pi}^{\pi}\ f(x) \ dx  $ is equivalent to integrating $f(x)$ over $-\pi < x < \pi$ (strictly less than) and also $-\pi \le x \le \pi$ (less than or equal). So I just need to reassure myself that the bounds of integration don't change when considering either less than or less than or equal. A quick confirmation will do.
Cheers.
UPDATE:Just applying this to a question to do with fourier series: $\
f(x) = \begin{cases}x+π & -π<x\le 0 \\ π & 0 < x < π\\  \end{cases} $ with 2π periodicity.
Can I proceed to compute:
$a_0= {1\over 2L}[\displaystyle \int \limits_{-π}^{0} \ x+π \ dx + \int \limits_{0}^{π} π\ dx]$
Answer: No, the bounds are not included which is crucial when evaluating improper integrals. For example, if $ f(x) = \sqrt{\left|\tan\left(\frac{x}{2}\right)\right|} $, then the integral converges despite the integrand not being defined at the bounds.
Answer: With respect to your edit, Yes, you can compute this particular integral (and evaluate "at" the bounds $I(x) \big|_b^a$, but keep in mind Jon Claus's answer: that what you are really doing is evaluating the limit of $I(x)$ as $x \to a^-, x\to b^+$, and that's crucial to remember in the case of improper integrals.
Answer: Yes that is true (assuming a proper riemann integral, otherwise you must take a limit). Think of it in terms of the area under a curve, ie. you want to calculate the area under the curve f(x) where the end points are $-\pi$, and $\pi$. Clearly the end point don't contribute a nonzero amount to the area.
Comment: The Riemann integral is only defined on closed sets.

Quick Integration Clarification


Neural network can only follow increasing function

Question: I am trying to program a simple neural network using python.  For some reason my code is only working on functions which are increasing.  The network I am using has 1 input, 1 output, and 2 layers of 20 neurons.  When I train it on the function sin(x) where 0 < x < 1 the output nearly perfectly matches the calculated sin(x).  I then tried cos(x) and the output from my network is a constant.  I also tried linear functions.  These too only work if the function is increasing.

Here is my python code.  I am using the cross entropy cost function but I also get the same result with the quadratic cost function.
import numpy as np
import random

def sigma(z):
    return 1.0 / (1 + np.exp(z))

def sigma_prime(z):
    ez = np.exp(z)
    return -ez / (ez + 1)**2

class NeuralNetwork:

    def __init__(self, *sizes):
        self.w = []
        self.b = []
        last = sizes[0]
        for s in sizes[1:]:
            self.w.append(np.full((s, last), 1.0 / (s * last)))
            self.b.append(np.zeros(s))
            last = s

    def proporgate(self, vin):
        a = np.array(vin).T
        for w, b in zip(self.w, self.b):
            a = np.dot(w, a) + b;
            a = sigma(a)
        return a.flatten()

    def randomize(self):
        for i in range(len(self.w)):
            self.w[i] = np.random.randn(self.w[i].shape[0], self.w[i].shape[1])
            self.b[i] = np.random.randn(self.b[i].shape[0])

    def train(self, learning_rate, vin, vout):
        a = [None] * len(self.w)
        z = [None] * len(self.w)

        z[0] = np.dot(self.w[0], np.array(vin).T) + self.b[0]
        a[0] = sigma(z[0])

        for i in range(1, len(self.w)):
            z[i] = np.dot(self.w[i], a[i - 1]) + self.b[i]
            a[i] = sigma(z[i])

        d = [None] * len(self.w)
        y = np.array(vout).T
        #d[-1] = (a[-1] - y) * sigma_prime(z[-1])
        d[-1] = ((1 - y) / (1 - a[-1]) - y / a[-1]) * sigma_prime(z[-1])

        for i in reversed(range(len(self.w) - 1)):
            d[i] = np.dot(self.w[i + 1].T, d[i + 1]) * sigma_prime(z[i])

        for i in range(len(self.w)):
            self.b[i] -= learning_rate * d[i]
            self.w[i] -= learning_rate * np.dot(d[i], a[i].T)

import matplotlib.pyplot as plt

n = NeuralNetwork(1, 20, 20, 1)
n.randomize()
iters = 1000000
for i in range(iters):
    x = random.random()
    n.train(5 * (1.0 - i / iters), [x], [np.cos(x)])

x = np.linspace(0, 1, 1000)
y = np.zeros_like(x)
for i in range(y.shape[0]):
    y[i] = n.proporgate([x[i]])

plt.plot(x, y, x, np.cos(x))
plt.title('cos(x)')
plt.show()

Answer: It looks like your network returns the mean of the function, that why you got the line. I checked that mean should be equal approximately 0.84 which is very close to your line. I run your code and check the weights. The weights from the last layer just blow up and become extremely huge.
>>> n.w[-1]
array([[ 53403.18717113,  53402.33451232,  53403.58075363,  53402.85629078,
         53403.56506679,  53402.75971252,  53403.78749293,  53402.87328528,
         53401.35358122,  53402.36034322,  53402.48372256,  53403.46650581,
         53402.50228206,  53401.9487225 ,  53402.93887686,  53402.70398353,
         53402.39407981,  53403.3953328 ,  53402.21630115,  53402.72207552]])

You should also get a waring message in the terminal that tell you that something went wrong with you calculations.
nn.py:9: RuntimeWarning: overflow encountered in square

You need to train it with less number of epochs and use smaller step to prevent this behaviour. I used 20,000 epochs and decrease your step by a factor of 5
n.train((1.0 - i / iters), [x], [np.cos(x)])

That's the result that I've gotten.
Comment: Thanks.  Do you know why the weights tend to blow up?
Comment: Your learning rate is too big and you trained network for too long. You can also use weight decay regularisation to prevent this behaviour.
Comment: Do you know if it is bad practice to update the weights after running every training example or should I batch a few training examples and then update the weights and biases?
Comment: batch will give you the average gradient and it will be more accurate than for the one sample. What you are trying to do it's a stochastic gradient descent (SGD). The gradients for the SGD contain a lot of noise. Usualy SGD gives better results, because your make update for each instance separately, but it should defence on the problem. Some algorithms wouldn't work properly in case of mini-batch (or batch with one sample in your case). There are a some pros and cons for both methods.


Remote logging to specific directory structure with rsyslog

Question: I'm currently in the process of having all of our various servers send their syslog messages to a central server.  I'd like to have the following directory structure on the central server that logs are sent to.
<root_dir>/<server_name>/<year>/<month>/<day>/{messages, mail, auth, etc}.log

Is this something I can do with rsyslog templates alone or do I need to use another utility to move the files into the proper file location?
Answer: I don't know about creating the directories, but the rest should be handled by filters and dynamic file names.
Normally in a case like this I would log file pattern like.
/var/log/<server_name>{messages,mail,auth,etc}.YYYYMMDD

Having the logs separated by server may make tracing conditions across servers difficult.  The log messages typically include the originating server, so it is easy to retrieve messages for a particular server with awk, grep or any other pattern matching tool.  Scaling out that many log files may also be difficult, and you may hit resource limits.
Typically log rotation is handled by a program like logrotate.  This allows you to control the frequency of log rotations for particular files, and to control the number of versions you keep.  It's postrotate action could be used to split out date from the just rotated files into the desired form.  This may scale better.


Conditionally setting orderBy Angularjs

Question: I have an array of users, I want to have my ng-repeat ordered by last name when first loaded.  After a new user is added have the ng-repeat ordered by dated added then last name.  Essentially I want the newest users pushed to the top of the ng-repeat.

Name

In my JS...
_this.sortColumn = 1;
_this.orderByField = 'lastName';
_this.reverseSort = false;

 _this.menuFilter = function(section, column) {
      _this.orderByField = section;
      _this.reverseSort = !_this.reverseSort;
      _this.sortColumn = column;
};

//my attempt to reset the order by created at date
if( _this.isRefreshing ) {
        _this.orderByField = ['createdAt', 'lastName'];
}

Basically this code is not doing anything.  I think I am missing a step in the HTML.
Thanks!
Comment: How/Where is `isRefreshing` updated ?
Answer: I think this is easiest done by sorting the array in pure javascript and then using a ng-repeat without the orderBy attribute.
HTML:

{{user}}

<input type="text" ng-model="name"></input>
<button ng-click="addName()">Add name</button>

JS:
$scope.users = ["Rusty", "Shackleford", "Dale", "Gribble"];
$scope.users.sort();

$scope.addName = function() {
   $scope.users.unshift($scope.name);
}

JSFiddle: http://jsfiddle.net/asWF9/2/
This answer may help to sort your array: https://stackoverflow.com/a/6712080/3675149
Answer: Try using "unshift" instead of 'push' to add an item into the array. The unshift in js enables us to insert an item to the top of an array.

Conditionally setting orderBy Angularjs


1.20 (b) Baby Rudin: $y-x>0 \rightarrow \exists n \, n(y-x)>1$ short question

Question: I found the following in Baby Rudin:

Theorem
If $x,y\in \Bbb R, x>0$ then there exists a natural number $n$ such that
  $$nx>y$$

After this, he proves another theorem using the following

Let $x<y$, we have $y-x>0$ then the theorem above yields that a natural number $n$ exists such that
  $$
n(y-x)>1
$$

I'm not sure how that theorem gives that result, could someone explain this? I'm pretty  sure it must be really simple, but I'm not getting it.
E: I said in the comments this was not a duplicate to the suggested question. My doubt here was even more elementary than that question's.
Comment: The $y - x$ is the "$x$" and 1 is the "$y$"
Comment: @AngeloMark It's not a duplicate of that! I'm asking a question about the same theorem but different parts.
Comment: @Nitin Oh... Thanks. I'm really slow. You should post that as an answer haha.
Answer: Per OP's suggestion,

The $y - x$ is the "$x$" and 1 is the "$y$"
Comment: Indeed, the OP asked to post this as an answer. So ok.


Delaying function return until Ajax request completes

Question: I'm writing a simple utility to handle the numerous JSON calls I have to make in my app, but the problem with the readJson function below is that it completes before the inner function that gets the Ajax has got the data, so it returns undefined. Can anyone point me to an existing answer to this issue, or offer any advice on how to better structure this function so that it doesn't return until the Ajax request is complete?
json_manager = {
    thisJson:'',
    readJson:function(theFileName, thePathName) {

        var theUrl=(thePathName+theFileName);
        $.getJSON(theUrl).done(function(theJSON) {
                json_manager.thisJson=theJSON;
        });
        return(json_manager.thisJson)
    },
        //more functions
    }

Answer: The only way you can serialize things so that the ajax call finishes before the function returns is by making the ajax call synchronous.  And, that is NOT recommended because it often creates a seriously bad user experience for the end-user where the browser is locked up and not processing events during your ajax call.  Do not do it that way.
Other than that, the "A" in AJAX stands for asynchronous and you need to learn to program in asynchronous fashion using callbacks to carry out the work that needs the result from the AAX call.  Because Ajax is normally asynchronous, your readJson() function will return BEFORE the ajax call is complete.  Thus, you cannot return the result of the ajax call in that function.
To restructure your code to work with asynchronous ajax, you must move all the work that needs the result from the $.getJSON() into a callback and call that callback function with the result as an argument from the success/completion handler of the ajax call.
You could do that like this:
json_manager = {
    thisJson:'',
    readJson:function(theFileName, thePathName, successHandlerFn) {

        var theUrl=(thePathName+theFileName);
        $.getJSON(theUrl).done(function(theJSON) {
                successHandlerFn(theJSON);
        });
    },
        //more functions
}

Comment: Thanks - yes, I understand the asynch issue - just didn't have a way to work around it - I'll check this out too - looks like a similar solution to the one I accepted so +1 :)
Answer: You're running into a classic asynchronous problem. What's happening is the $.getJSON call doesn nothing more than setup the call to the server. It doesn't actually execute the call until the currently executing function returns control back to the browser.
In this situation, you have to structure your code to tell where the data goes, not ask for the data.
Something like this:
json_manager = {

    readJson:function(theFileName, thePathName, callback) {

        var theUrl=(thePathName+theFileName);
        $.getJSON(theUrl).done(callback);

    },
        //more functions
}

Then you can call it like:
json_manager.readJson('file', 'path', function(data) {
   // do something with the returned json
});

This way, you're providing the readJson function with instructions on what to do with the data it gets from the server.
Comment: Thank you, rossipedia - worked perfectly - and an excellent explanation - I appreciate it.


Generalised linear models (for dummies)

Question: I'm trying to get to grips with this topic, and it's proving tough. Could anyone point me in the direction of some good web based sources to read? I'm looking for good explanations of the theory and also good explanations of how to put it into practice (in R if possible)
Answer: This is a self-citation, but I think it is a good match to what you're after:
Dunn, P. K., and Smyth, G. K, (2018). Generalized linear models with examples in R. Springer, New York, NY. (Published 11 November 2018)
https://doi.org/10.1007/978-1-4419-0118-7
https://www.amazon.com/dp/1441901175
Answer: Try the following:
Books:
Generalized, Linear, and Mixed Models 2nd Edition (a personal favorite of mine)
Applied Regression Analysis and Generalized Linear Models Third Edition
Generalized Linear Models and Extensions, Third Edition 3rd Edition
Generalized Linear Mixed Models: Modern Concepts, Methods and Applications (Chapman & Hall/CRC Texts in Statistical Science) 1st Edition
Online Course Materials:
Princeton Course on Generalized Linear Statistical Models by German Rodriguez
Iowa State University Course on Linear Models by Dan Nettleton (See Slide Deck 33 in particular)
Penn State STAT 504 notes
MIT Open Courseware.  Statistics for Applications, Lecture 21

Generalised linear models (for dummies)


What is the need to have the Unicode class?

Question: Why is it necessary to have the Unicode class?
What does Unicode::strtoupper() or Unicode::strtolower() do that cannot be done just using native the PHP functions strtoupper() or strtolower()?
Comment: I am going to guess that using Unicode provides a common implementation everyone can use that solves or handles typical/common problems from using the php function on their own implementations. Note that in previous versions we had drupal_strtolower. https://api.drupal.org/api/drupal/includes!unicode.inc/function/drupal_strtolower/7.x
Answer: The reasons to have its own replacement of strtoupper and strtolower() is to support multi-bytes string in those sites where the Multibyte String extension is installed, and in those sites where that extension is not installed.
strtoupper() and strtolower() don't support multi-byte strings. Even in the case Drupal would not try to use the Multibyte String extension when installed, Unicode::strtoupper() and Unicode::strtolower() would not be reduced to simply calls to strtoupper() and strtolower(). Their code would probably be the following one.
public static function strtoupper($text) {
  // Use C-locale for ASCII-only uppercase.
  $text = strtoupper($text);
  // Case flip Latin-1 accented letters.
  $text = preg_replace_callback('/\xC3[\xA0-\xB6\xB8-\xBE]/', '\Drupal\Component\Utility\Unicode::caseFlip', $text);
  return $text;
}

public static function strtolower($text) {
  // Use C-locale for ASCII-only lowercase.
  $text = strtolower($text);
  // Case flip Latin-1 accented letters.
  $text = preg_replace_callback('/\xC3[\x80-\x96\x98-\x9E]/', '\Drupal\Component\Utility\Unicode::caseFlip', $text);
  return $text;
}

If Drupal required the Multibyte String extension to be present, then Unicode::strtoupper() and Unicode::strtolower() would not be necessary, since Drupal would use directly mb_strtoupper() and mb_strtolower().
That is not different from what happened in previous versions, except they implemented drupal_strtouppper() and drupal_strtolower(). If you look at the code, it is not much different.
function drupal_strtoupper($text) {
  global $multibyte;
  if ($multibyte == UNICODE_MULTIBYTE) {
    return mb_strtoupper($text);
  }
  else {
    // Use C-locale for ASCII-only uppercase
    $text = strtoupper($text);
    // Case flip Latin-1 accented letters
    $text = preg_replace_callback('/\xC3[\xA0-\xB6\xB8-\xBE]/', '_unicode_caseflip', $text);
    return $text;
  }
}

public static function strtoupper($text) {
  if (static::getStatus() == static::STATUS_MULTIBYTE) {
    return mb_strtoupper($text);
  }
  else {
    // Use C-locale for ASCII-only uppercase.
    $text = strtoupper($text);
    // Case flip Latin-1 accented letters.
    $text = preg_replace_callback('/\xC3[\xA0-\xB6\xB8-\xBE]/', '\Drupal\Component\Utility\Unicode::caseFlip', $text);
    return $text;
  }
}

Comment: Concise yet thorough.


Questions about the reflection operator on an inner product space

Question: Let $V$ be an $n$-dimensional real inner product space. Suppose $u \in V$ has norm 1, and define the reflection in the direction of $u$ as $$r_u(v) = v - 2 \langle v, u \rangle u = v - 2proj_{span(u)}(v)$$ for all $v \in V$.
a) What is the trace and determinant of $r_u$?
b) Show that $r_u$ is an isometry.
c) Show that if $v, w \in V$, then there exists a unique $r_u \in \mathcal{L}(V)$ for which $r_u(v)=w$.
For (a), I tried solving for the determinant and trace on a 3- and 4-dim vector space $V$, but I wasn't sure how to generalize to $n$ dimensions. As for (b) and (c), I'm unsure on where to begin. Any help and guidance would be lovely, thanks!
EDIT: Fixed question (c).
Answer: a) The trace and determinant can be computed if you know the eigenvalues of $r_u$. (Hint: the eigenvalues are $-1$ and $1$, but you need to figure out how many of each there are.)
b) Have you written down the definition of isometry and tried to verify that $r_u$ satisfies it?
c) Draw a picture (say, in 2-dimensions) of some arbitrary $v$ and $w$, and think about what kind of reflection sends $v$ to $w$. Can you draw what $u$ should be for that reflection? (Hint: the line in direction $u$ should perpendicularly bisect the segment between $v$ and $w$.)


Mouse point to viewer coordinates

Question: I have successfully added a cricle into the viewer using the following code:

function addCircle(){

  cameraHUD = new THREE.OrthographicCamera(-width / 2, width / 2, height / 2, -height / 2, 0, 30);
  viewer.impl.createOverlayScene('leaderOverlay-circle',null,null, cameraHUD);

  var circle_geometry = new THREE.CircleGeometry(10, 32);
  var circle_material = new THREE.MeshBasicMaterial({color: 'rgb(1,1,1)'});
  var circle = new THREE.Mesh(circle_geometry, circle_material);
  circle.position.set(100,100,0);

  viewer.impl.addOverlay('leaderOverlay-circle', circle);
  viewer.impl.invalidate(true)

}

I now  want to add this circle at a point that is clicked in the viewer. My attempts to do this are as follows but the coordiantes are not accurate to the point being clicked on the screen:

function onClick(ev) {

  var wtc = viewer.clientToViewport(ev.clientX - bounds.left, ev.clientY - bounds.top);
  var projectedPoint = new THREE.Vector3(leader.endPoint.x, leader.endPoint.y, leader.endPoint.z);
  projectedPoint.unproject(cameraHUD);
  var circlePosition = viewer.worldToClient(new THREE.Vector3(point.x, point.y, point.z));

  cameraHUD = new THREE.OrthographicCamera(-width / 2, width / 2, height / 2, -height / 2, 0, 30);
  viewer.impl.createOverlayScene('leaderOverlay-circle',null,null, cameraHUD);

  var circle_geometry = new THREE.CircleGeometry(10, 32);
  var circle_material = new THREE.MeshBasicMaterial({color: 'rgb(1,1,1)'});
  var circle = new THREE.Mesh(circle_geometry, circle_material);
  circle.position.set(circlePosition);

  viewer.impl.addOverlay('leaderOverlay-circle', circle);
  viewer.impl.invalidate(true);

}
Answer: Refer to here to project your canvas coordinates to the scene world - see live sample here:
const camera= this.viewer.navigation.getCamera();
const vec = new THREE.Vector3();
const pos = new THREE.Vector3();

vec.set(
  ( event.clientX / window.innerWidth ) * 2 - 1,
  - ( event.clientY / window.innerHeight ) * 2 + 1,
  0.5 );

vec.unproject( camera );
vec.sub( camera.position ).normalize();
const distance = - camera.position.z / vec.z;
pos.copy( camera.position ).add( vec.multiplyScalar( distance ) );

...
circle.position.set(pos.x,pos.y,pos.z);

Mouse point to viewer coordinates