Alternative proof for subbasis for order topology and product topology: Finite intersections of elements of $\mathscr S$ is a basis

Question: Topology by James Munkres:
Both of the following proofs are at the very end of, respectively, Sections 14 and 15, which seem to correspond with the definition of a subbasis' being at the very end of Section 13.

Open rays are a subbasis for the order topology

Thm 15.2 The following collection $\mathscr S$ is a subbasis for the product topology.

$$\mathscr S = \{\pi_{1}^{-1}(U):\text{U open in X}\}\cup\{\pi_{1}^{-1}(V):\text{V open in Y}\} \tag{*}$$

It seems like the proofs are showing that $\mathscr T = \mathscr T'$ by showing $\mathscr T \subseteq \mathscr T'$ and $\mathscr T \supseteq \mathscr T'$, where $\mathscr T$ is the order topology or the product topology while $\mathscr T'$ is, respectively, the topology generated by the open rays or the topology generated by the $\mathscr S$ in $(*)$, by making use of Lemma 13.3 as follows.

Can we alternatively show that the collection $\mathscr B$ of all finite intersections of elements of $\mathscr S$ is a basis for $\mathscr T$, by making use of Lemma 13.2 as follows? Or are the methods actually equivalent?

My idea is based on, well, the very end of Section 13.
Comment: "Can we alternatively show that the collection $\mathscr B$ of all finite intersections of elements of $\mathscr S$ is a basis for $\mathscr T$...?" Yes, that should work fine. For the open ray topology, starting from the open ray sub-basis you will get the open interval basis. For the product topology, starting from the sub-basis in $(*)$ you will get the open box basis.
Comment: @LeeMosher thanks! Is the method in fact different? Or is it actually equivalent? Also, post as answer?
Answer: In general if $\tau$ denotes a topology and $\mathcal B$ denotes a basis of $\tau$ then - if you want to prove that $\mathcal S$ is a subbasis of $\tau$  and $\mathcal V$ denotes the collection of finite intersections of elements of $\mathcal S$ - it is enough to prove that every $B\in\mathcal B$ can be written as union of elements of $\mathcal V$.
This because then it is guaranteed that also that every $O\in\tau$ (that by definition can be written as union of basis elements) can be written as union of elements of $\mathcal V$.
We get something like:$$O=\bigcup_{i\in I}B_i=\bigcup_{i\in I}\bigcup_{j\in J_i}V_{i,j}$$where the $B_i\in\mathcal B$ and the $V_{i,j}\in\mathcal V$.
In your situation we have $\mathcal B=\mathscr B_{\text{ord}}\subseteq\mathscr B=\mathcal V$ showing directly that every element of $\mathcal B$ can be written as union of elements of $\mathcal V$.
Comment: It shows that your proof concerning the order topology can be made shorter. That's all.
Comment: Thanks drhab! This is now a third way of proving either?
Comment: ok I'll analyse later. Thanks.
Answer: Proof that open rays are a subbasis $\mathscr S$ for the order topology $\mathscr T$ by showing that $\mathscr B$ of all finite intersections of elements of $\mathscr S$ is a basis for $\mathscr T$:
(Hopefully, it is obvious that $\mathscr S$ is a subbasis for some topology on $X$.)
First, $\mathscr B = \{\text{open rays in X}, \text{open intervals in X}\}$, where some (resp: all) open rays are half-open intervals if and only if $X$ has a $\max$ or (resp: and) $\min$. Also, denote $\mathscr B_{\text{ord}}$ to be the basis that defines $\mathscr T$.
To show $\mathscr B$ is a  basis for the order topology $\mathscr T$, we must show that for all $A \in \mathscr T$ and $x \in A$, we can find $B \in \mathscr B$ s.t. $x \in B \subseteq A$.
Let $A \in \mathscr T$ and $x \in A$. By def of $A$'s being an element of $\mathscr T$, we have that for all $x \in A$, we can find $B_{\text{ord}} \in \mathscr B_{\text{ord}}$ s.t. $x \in B_{\text{ord}} \subseteq A$. Now, we wonder how $B_{\text{ord}}$ can help us find $B$. Well, by def of $B_{\text{ord}}$'s being an element of $\mathscr B_{\text{ord}}$, such $B_{\text{ord}}$ is in one of the following three forms:
$1. B_{\text{ord}} = (a,b)$
Obviously, choose $B=B_{\text{ord}}$.
$2. B_{\text{ord}} = [a_0,b)$
This expression makes sense if and only if $X$ has a minimum given by $a_0 := \min X$. Thus, by def of the open ray $(-\infty,b)$, we have that $[a_0,b)=(-\infty,b)$. Therefore, we can choose, but not quite obviously, $B=B_{\text{ord}}$.
$3. B_{\text{ord}} = (a,b_0]$
Similar to (2):
This expression makes sense if and only if $X$ has a maximum given by $b_0 := \max X$. Thus, by def of the open ray $(a,\infty)$, we have that $(a,b_0]=(a,\infty)$. Therefore, we can choose, but not quite obviously, $B=B_{\text{ord}}$.
Therefore, $\mathscr B$ is a  basis for the order topology $\mathscr T$, by Lemma 13.2 (which I'm not quite sure is different from the definition of a basis itself, but whatever).
Therefore, $\mathscr S$ is a subbasis for $\mathscr T$.
QED
Considering that this proof relies on the same observations (the three forms) as the original proof, the proofs look pretty equivalent to me.

Proof that the following collection $\mathscr S$ is a subbasis for the product topology $\mathscr T$
$$\mathscr S = \{\pi_{1}^{-1}(U):\text{U open in X}\}\cup\{\pi_{1}^{-1}(V):\text{V open in Y}\} \tag{*}$$
by showing that $\mathscr B$ of all finite intersections of elements of $\mathscr S$ is a basis for $\mathscr T$:
(Hopefully, it is obvious that $\mathscr S$ in (*) is a subbasis for some topology on $X \times Y$.)
First, $\mathscr B = \{ \bigcap_{i_j=i_1}^{i_n} S_{i_j} : S_{ij} \in \mathscr S \}$. Also, denote $\mathscr B_{X \times Y}$ to be the basis that defines $\mathscr T$ and $\mathscr T_X$ and $\mathscr T_Y$ as any topologies of, resp, $X$ and $Y$.
To show $\mathscr B$ is a  basis for the product topology $\mathscr T$, we must show that for all $A \in \mathscr T$ and $(x,y) \in A$, we can find $B \in \mathscr B$ s.t. $(x,y) \in B \subseteq A$.
Let $A \in \mathscr T$ and $(x,y) \in A$. By def of $A$'s being an element of $\mathscr T$, we have that for all $(x,y) \in A$, we can find $B_{X \times Y} \in \mathscr B_{X \times Y}$ s.t. $(x,y) \in B_{X \times Y} \subseteq A$. Now, we wonder how $B_{X \times Y}$ can help us find $B$. Well, by def of $B_{X \times Y}$'s being an element of $\mathscr B_{X \times Y}$, there exist $U_B \in \mathscr T_X, V_B \in \mathscr T_Y$ s.t. $B_{X \times Y}$ is given by
$$B_{X \times Y} = \pi_{1}^{-1}(U_B) \cap \pi_{2}^{-1}(V_B) \tag{**},$$
where
$\pi_{1}^{-1}(U_B) \in \mathscr S$ for $V=\emptyset=\pi_{1}^{-1}(\emptyset)=X \times \emptyset$ and $\pi_{2}^{-1}(V_B) \in \mathscr S$ for $U=\emptyset=\pi_{2}^{-1}(\emptyset)=\emptyset \times Y$
Therefore, $\mathscr B$ is a  basis for the product topology $\mathscr T$, by Lemma 13.2 (which I'm not quite sure is different from the definition of a basis itself, but whatever).
Therefore, $\mathscr S$ in (*) is a subbasis for $\mathscr T$.
QED
Considering that this proof relies on the same observation $(**)$ as the original proof, the proofs look pretty equivalent to me.


Continuity of integral w.r.t. Lebesgue measure

Question: Let $f:\Bbb R^n\times \Bbb R^m\to \Bbb R$ be a bounded measurable function, and $\mu$ be a probability measure on $\Bbb R^m$ which is absolutely continuous w.r.t. Lebesgue measure $\lambda$, e.g. $m  =\frac{\mathrm d\mu}{\mathrm d\lambda}$ is the density of $\mu$. Define
$$
  g(x):=\int_{\Bbb R^m} f(x,y)\mu(\mathrm dy) = \int_{\Bbb R^m} f(x,y)m(y)\lambda(\mathrm dy).
$$
Under which conditions can we assure that $g$ is a continuous function of $x$? I'm pretty sure this question should have been studied already, as it is pretty basic. I am interested in the case when one can't assume continuity of $f$ as a function of $x$ for fixed $y$, or as a function of $y$ for a fixed $x$.
Clearly, $g$ is not continuous in general. For example, if $f$ does not depend on $y$, then $g(x) = f(x)$ and hence if $f$ is discontinuous so is $g$. The only way to prevent it is to assume that $f$ somehow "always depends" on $y$ in a non-trivial way as it shall help "average" the discontinuities of $f$. At the same time, I am not sure how to formally assume such dependence. Perhaps, claiming that the image measure $(f_x)_*\mu$ is non-atomic for every $x$ shall help.
I hope that assumptions on $\mu$ allow considering such distributions as exponential or uniform (that is, densities that are either $0$ or smooth).
Comment: Denote $h_x(y) = f(x,y)$. Then the dominated convergence theorem tells us that if $\lim_{x\to x_0}h_x = h_{x_0}$ almost surely in $(\Bbb R^m,\mu)$, then $g$ is continuous at $x_0$. Since $f$ is bounded, I believe it suffices that $h_x\to h_{x_0}$ in probability.
Comment: @JonathanY. thanks, that actually could help. The uniform integrability is a necessary and sufficient assumption for the convergence in probability to imply the convergence of expectations, so we have it for bounded $f$.
Answer: A quite general condition on $f$ is that $f(x,y)$ is continuous as a function of $x$ uniformly in $y$, that is, given $x_0\in\mathbb{R}^m$ and $\epsilon>0$ there exists $\delta$ (depending on $x_0$ and $\epsilon$ but not on $y$) such that
$$
|x-x_0|\le\delta \implies |f(x,y)-f(x_0,y)|\le\epsilon\quad\forall y\in\mathbb{R}^m.
$$
Comment: Thank you, although I'm interested in the case when $f$ is continuous in neither of two variables, should have specified it from the beginning. Updated OP now.
Comment: The only thing I can think of is if $f(x,y)=\phi(x-y)$. Then $g=\phi\ast\mu$ will be continuous for a large class of functions $\phi$.


Select no Laravel esta retornando o nome da coluna(Eloquent)

Question: Estou com problema para retornar somente o valor do select no caso é o nome ,mas esta retornando como se fosse string e esta com nome da coluna.

Controller

public function resultadoconsulta(Request $request){
        $nome = $request-> nomeCompleto;
        $data = $request-> dataNascimento;

 $paciente = Pacientes::select('nome_completo')
             ->where('nome_completo',$nome)
             ->where('data_nascimento',$data)
             ->get();

 return view('/resultadoconsultapaciente')->withPaciente(json_encode($paciente));}

View

{{$paciente}}

Resultado

[{"nome_completo":"Leonardo Amancio de Araujo"}]

Comment: Porque tá usando json_encode?
Comment: Pesquisa também poderia ser melhor
Answer:
Controller

public function resultadoconsulta(Request $request){
        $nome = $request-> nomeCompleto;
        $data = $request-> dataNascimento;

 $paciente = Pacientes::select('nome_completo')
             ->where('nome_completo',$nome)
             ->where('data_nascimento',$data)
             ->get()->first();
//use o first() para obter o primeiro registo da lista, caso contrário devolve uma collection

//outra forma de passar a variável à View
 return view('/resultadoconsultapaciente', compact('paciente'))
}

View

{{$paciente->nome_completo}}

Comment: O resultado foi quase o mesmo :
{"nome_completo":"Leonardo Amancio de Araujo"}
Comment: Usou o ->first();  no query?
Comment: usei sim ,consegui resolver assim: coloquei na view
@foreach($paciente as $pac) e depois {{$pac->nome_completo}}


Como configurar vários sistemas em um domínio?

Question: Tenho domínio www.meusite.com.br
Gostaria de hospedar vários sistemas nesse domínio. Ficando assim:
www.meusite.com.br/sistema1
www.meusite.com.br/sistema2
www.meusite.com.br/sistema3
Uso o virtualhost assim:
<VirtualHost *:80>
  ServerName meusite.com.br
  ServerAlias www.meusite.com.br
  ServerAdmin firstname.lastname@example.com
  DocumentRoot "/var/www"

Alias /sistema1 "/var/www/sistema1/public"
   <Directory "/var/www/sistema1/public">
     AllowOverride All
     Options Indexes FollowSymLinks MultiViews
     Order deny,allow
     Allow from all
   </Directory>

Alias /sistema2 "/var/www/sistema2/public"
   <Directory "/var/www/sistema2/public">
     AllowOverride All
     Options Indexes FollowSymLinks MultiViews
     Order deny,allow
     Allow from all
   </Directory>

Troco o .htaccess de cada um dos projetos para isso:
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    Options +FollowSymLinks
    RewriteEngine On
    **RewriteBase /sistema1**

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

O sistema são todos em laravel. Está funcionando dessa forma acima, porém, acho que não é a abordagem correta. Em um outro projeto que estamos usando, está apresentando erros nas url's das chamadas ajax. Sou estudante e preciso configurar esse servidor da universidade para hospedar outros sistemas nesse domínio.
Poderiam me orientar em qual abordagem utilizar ou o que devo começar a estudar? Não tenho muito conhecimento, preciso de uma direção.
Answer: Cada sistema com seu próprio subdomínio e um VirtualHost para cada um:
<VirtualHost *:80>
    ServerAdmin email@example.com

    ServerName sistema1.meusite.com.br

    DocumentRoot /var/www/sistema1/public
    ErrorLog ${APACHE_LOG_DIR}/sistema1-error.log

    <Directory /var/www/sistema1/public>
            Options -Indexes
            AllowOverride All
            Order allow,deny
            Allow from all
    </Directory>
</VirtualHost>

E o próximo é:
<VirtualHost *:80>
    ServerAdmin firstname.lastname@example.com

    ServerName sistema2.meusite.com.br

    DocumentRoot /var/www/sistema2/public
    ErrorLog ${APACHE_LOG_DIR}/sistema2-error.log

    <Directory /var/www/sistema2/public>
            Options -Indexes
            AllowOverride All
            Order allow,deny
            Allow from all
    </Directory>
</VirtualHost>

E cada virtualhost com um arquivo de configuração diferente:
/etc/apache2/sites-available/sistema1.conf
/etc/apache2/sites-available/sistema2.conf
Pode até parecer trabalhoso ficar apontando muitos registros de DNS, mas essa abordagem separa cada uma das aplicações de maneira eficiente. Podendo até mesmo ativar e desativar qualquer um deles sem afetar o todo.
E caso você tenha que compartilhar o núcleo da aplicação entre todos aponte tudo para o mesmo diretório e seu sistema deve verificar qual é o hostname certo e trabalhar com os dados corretos. (Não recomendaria)
E, por fim, você pode escrever um Shell Script que automatize a criação destes arquivos de configuração.
E ainda você não deixa de usar um único domínio, só está utilizando ele de forma diferente de como pensou inicialmente.
Comment: Obrigado pela atenção. Vou fazer como você falou, mas o problema é a coordenadora quer que os endereços sejam como eu falei: www.meusite.com.br/sistema1 etc. É possível fazer de outra forma da que eu fiz ?
Comment: Eu apresentaria para sua coordenadora as vantagens em questões de segurança de isolar uma aplicação de outra, mas pensando na solução que está propondo acredito que a configuração RewriteBase não tem que ter asteriscos. Me fala mais sobre o comportamento que acontece quando roda essa configuração que tento te ajudar..
Comment: Amigo, conseguir resolver meu problema com relação as urls ajax. Vou propor o que você me disse. Abraço


Prove decidability of language in logic

Question:
Let $S$ be a non-empty finite alphabet, Let $S^\ast$ denotes the set of words that can be written using the given alphabet. Let $A_1, A_2 \subseteq S*$ be languages such that membership in each of $A_1, A_2$ is decidable. Prove membership in the following language is decideable:
  $$A_1 \cap A_2 = \{w \in S^\ast | w \in A_1 \land w \in A_2\}$$

Can someone provide a hint? Sounds like the halting problem.
Comment: @EricTowers : $L_1$ and $L_2$ should be $A_1$ and $A_2$.  Also, $A_1$ and $A_2$ should be subsets  of $S*$ (not elements of $S*$).
Comment: @AndreasBlass, right, I'm sorry
Comment: @EricTowers, Andreas is right
Comment: @AndreasBlass, I've fixed the statement. Mind helping out?
Answer: How would one decide if a string is in $A_1$ and $A_2$?  First, is it in $A_1$?  If so, is it also in $A_2$?  If so, it is a member of the intersection, otherwise not.  Do we have a decision procedure for a string being in $A_1$?  (Yes, $A_1$ is decidable.)  Do we have a decision procedure for a string being in $A_2$?  (Yes, also decidable.)  Can we combine two decision procedures so that the result is positive only if both procedures declare membership?
This can initially be hard to work with because the two decision procedures are unnamed.  Let's start by fixing that.  (Much progress just comes from labelling the parts that are on the table in front of us.)  The rest then follows the line of thought above.
Since membership in $A_1, A_2$ is decidable, there are \begin{align*}
d_1 &: S^* \rightarrow \{\text{Member}, \text{Nonmember}\}  \\
d_2 &: S^* \rightarrow \{\text{Member}, \text{Nonmember}\}  \text{,}
\end{align*} which decide whether a given string is a member of $A_1, A_2$, respectively.  Then the function \begin{align*}
    f &: S^* \rightarrow \{\text{Member}, \text{Nonmember}\}  \\
      &: s \mapsto (d_1(s) = \text{Member}) \wedge (d_2(s) = \text{Member})
\end{align*} decides the language $A_1 \cap A_2$.  You should show that the finite conjunction of decision procedures meets your definition of a decision procedure.
Note: There are a large number of specifications of "decision procedure".  You may have to adapt details of the above to match your setting.
Answer: We have a Turing machine $T_1$ that halts on every input and outputs $0$ or $1$ (or goes to one final state (accepting state) or another (rejecting state), depending on what your text's conventions are) that determines membership of $A_1$.
Likewise $T_2$ for $A_2$.
Now just do both of them at the same time in a combined Turing machine (a product or some such construction), and accept if both accept.
Comment: So take an arbitary $w$ and if $w$ is in $A_1$ halt, and if $w$ is in $A_2$ halt. since both halted we know $w \in A_1 \cap A_2$? correct?
Comment: @deri yes , indeed. The Turing machine should always halt for a decidable language, but they should both halt in an accepting state. Similarly the union is decidable too etc.

Prove decidability of language in logic


Integral cohomology. follows from GAGA?

Question: Let $X$ be a projective variety over $\Bbb C$. One can compute the integral cohomology groups $H^p(X,\Bbb Z)$ by looking at the constant sheaf $\Bbb Z$ in the Zariski topology on $X$, but one can do the same with respect the Euclidean topology on $X$ as well.
Question: what guarantees that the two definitions will give the same cohomology groups? My initial reaction was that this should follow from GAGA, but $\Bbb Z$ is not a coherent $\mathcal O_X$-module, so not sure what to do.
I am sure this is addressed in the literature, so a reference would be fine as well.
Comment: I don't see any reason that these should give the same cohomology groups. Even under nice assumptions the former should be capturing information about the homotopy type of the Zariski topology which is very weird and doesn't have much to do with the homotopy type of the Euclidean topology.
Comment: I think the best thing you can get from Zariski cohomology about singular cohomology is that if you want to understand $H^i(X,\mathbb{C})$ then by hodge decomposition it is enough to compute cohomologies of diffrentiel forms and then by G.A.G.A you can compute them in Zariski topology(the $H^i(X,\mathbb{C})$ itself in Zariski topology is useless)
Comment: You may be misremembering the idea that $\mathbb{Z}_p$-cohomology can be computed with respect to the (pro-)etale site and the Euclidean topology.
Answer: These are not at all the same. If $X$ is irreducible, then $H^q_{Zar}(X, \mathbb{Z})$ will be $0$ for $q>0$.
Proof: If $U_1$, $U_2$, ..., $U_n$ are nonempty Zariski open subsets of $X$, then $\bigcap_{i=1}^n U_i$ will be nonempty and connected. So the Cech complex of $\mathbb{Z}$ with respect to any cover with $N$ open sets will be $\mathbb{Z}^{\binom{N}{k+1}}$ in degree $k$. This is the complex which computes the cohomology of an $N$-1 dimensional simplex, and is thus acyclic.
Alternate (probably better) proof: If $X$ is irreducible and $U \subseteq V$ are nonempty open sets, then $U$ and $V$ are both connected, so the restriction map $\underline{\mathbb{Z}}(U) \longrightarrow \underline{\mathbb{Z}}(V)$ is an isomorphism, and thus the sheaf $\underline{\mathbb{Z}}$ is flasque.
If you want to do something algebraic and get the classical betti numbers as an answer, you either have to use something like etale cohomology or else something like the de Rham complex.
EDIT TO ADD: If you want to get cohomology with $\mathbb{Z}$ coefficients as an answer, you honestly have to work analytically, no algebraic method will work. Consider elliptic curves with complex multiplication by $\mathbb{Z}[\sqrt{-5}]$. Over $\mathbb{C}$, there are two isomorphism classes, with $j$-invariants $-632000 \pm 282880 \sqrt{5}$. Let $E_+$ and $E_-$ be the ones with $+$ and $-$ signs. We have $H^1(E_+, \mathbb{Z}) \cong H^1(E_-, \mathbb{Z}) \cong \mathbb{Z}^2$ as abelian groups. However, the complex multiplication structure makes them into modules for $\mathbb{Z}[\sqrt{-5}]$ and they are not isomorphic as such: $H^1(E_-, \mathbb{Z})$ is free of rank $1$, where as $H^1(E_+, \mathbb{Z})$ is isomorphic to the nonprincipal ideal $\langle 2, 1+\sqrt{-5} \rangle$.
Why is this bad news? We can choose a model $\mathcal{E}$ for $E_+$ over $\mathbb{Q}(\sqrt{5})$, with complex multiplication $\phi$ given by a morphism defined over $\mathbb{Q}(\sqrt{5}, \sqrt{-5})$. Let $\sigma$ be a Galois symmetry of $\mathbb{Q}(\sqrt{5}, \sqrt{-5})$ with $\sigma(\sqrt{5}) = - \sqrt{5}$. Then $\sigma(\phi)$ is an endomorphism of $\sigma(\mathcal{E})$, and $\sigma(E_+)$ is $E_-$. If we had an algebraic way of describing the functor $H^1(\ \ , \mathbb{Z})$, then we would have an isomorphism between $H^1(\mathcal{E}, \mathbb{Z})$ and $H^1(\sigma(\mathcal{E}), \mathbb{Z})$, conjugating $\phi^{\ast}$ to $\sigma(\phi^{\ast})$. (Because everything algebraic commutes with Galois symmetries.) But $\sigma(\mathcal{E})$ is $E_-$ as a complex manifold, so this contradicts that $H^1(E_-, \mathbb{Z})$ and $H^1(E_+, \mathbb{Z})$ are not isomorphic as $\mathbb{Z}[\sqrt{-5}]$ modules.

Integral cohomology. follows from GAGA?


Need a crash course in fourier analysis, recommend resources

Question: I need to be able to understand everything about fourier analysis asap. Could you recommend one or two references or books that are considered 'the book' to learn this subject?
Comment: nice responses. pls vote for your favourites! now i have too many books to choose from.
Answer: Dym and McKean's classic Fourier Series And Integrals. That's the book. You really need no other book on the subject-although you certainly might want to pursue it further.
Comment: If the OP needs to understand *everything* about Fourier analysis *asap*, I think a fast-paced book would be required.
Comment: @Jesse It really depends on what you mean by "fast-paced",Jesse. Students with very strong calculus backgrounds(i.e.honors calculus a la Spivak and Hubbard/Hubbard) really shouldn't find Dym and McKean THAT difficult with some effort.More importantly,it gives a very comprehensive account that covers both the pure analytic and physical application aspects of the subject-which are both of equal importance.Most other introductions to the subject focus on one aspect or the other.
Comment: @col Uh-unless you just want to memorize formulas and give the trappings of theory, I don't know if that's really possible.Maybe for some genius who enters Harvard at age 15,but for most of us,no,I doubt it.
Comment: Dym and McKean always struck me as a little fast-paced and difficult for beginners.
Comment: can it be done in a week? with some calculus background
Answer: It really depends on what level of Fourier analysis you're talking about, and whether you're coming at it from the applied  (for example, how to use Fourier series or transforms in solving PDE's) or the pure real/harmonic/functional analysis sides.  On the pure side, I'd recommend Edwards, "Fourier Series: A Modern Introduction" and Rudin, "Fourier Analysis on Groups",
Comment: i think my approach would be on the applied side
Answer: Fourier Analysis: An Introduction by M. Stein and Rami Shakarchi is the book I'd recommend.
I used it to improve my knowledge of Fourier Analysis and I was quite satisfied with it. I think it covers the basic facts and also some rather special issues as for example Fourier Analysis on finite groups which is important in number theory.
Comment: Someone told me Stein/Shakarchi make you want to stab yourself in the guts. : )
Comment: Yeah there where some passages... However, math tends generally to give that feeling, doesn't it? ;)
Comment: It depends : ) (on who is teaching you and other factors, /mesuspects)
Comment: well, this will only be me & the book, so...
Comment: @ool Then make sure to stay away from knives and such while reading Shakarchi and Stein.


How can I designate "internal" modules in my cabal package

Question: I'm using cabal to help organize my dependencies, build process, and testing for a small Haskell project I'm working on. The current cabal file contains lines such as the following:
library
  hs-source-dirs:      src
  exposed-modules:     Project.Exposed1
                       , Project.Exposed2
                         -- pretty please don't use below modules
                       , TestingUtilityFunctions
                       , GenericUtilityFunctions

  other-modules:       Convenient submodule for responsibility separation
                       , Another one

executable E1
  -- relies on Project for both Project.Exposed1 AND GenericUtilityFunctions

testsuite T2
  -- relies on Project for both Project.Exposed2 AND TestingUtilityFunctions

I need to keep TestingUtilityFunctions and GenericUtilityFunctions exposed because they appear in E1 and T2. However, they shouldn't be present in the library since the library's functionality is not to provide generic utility functions (which I change as I see fit), but to provide the interface exposed by Project.Exposed* modules.
Is there any way to make a "private" library (or several, to minimize dependency over-inclusion) which I can use inside my package but across executables and testsuites?
Comment: Is it important that `GenericUtilityFunctions` and `TestingUtilityFunctions` come from the library? I mean, one could imagine building your executable and test suite with their own internal copy of the library and *not* have an actual dependency on the library; this would increase the compile time significantly but would ease the need to expose internal stuff. I probably wouldn't squint too hard if this were needed for the test suite, though if even your executable needs access to internal stuff, that's probably a bad sign that your public API isn't exciting enough.
Comment: (...and if that approach sounds appealing to you, I'd be happy to write it up in enough detail to qualify as an answer.)
Comment: @DanielWagner That answer looks like it takes care of everything I requested in the question, though unfortunately the compilation time is a pretty serious concern, mainly because of the testing dependencies, which take a while.

I think it's worth posting as an answer since it addresses what the question wants.
Comment: @DanielWagner It's actually a bit more complicated - I would agree that it's an API issue if the executable needs access to internals. What's really going on is the following:

The library provides generic functionality X.
The executable provides an important instantiation of X, Y.
Y contains some boring business logic that is not too complex but requires a decent amount of code - it is easily testable and has much to gain from being unit tested. If I want to test Y I need to include Y in the library so that it can be tested...
Comment: It sounds like you just need a single module to be shared between the executable and test suite, and that module doesn't need access to library internals. That to me screams "not part of the library" -- just a file you write once and import in both the executable and test suite. That module will have to get compiled twice, but that probably isn't such a big hit -- certainly smaller than compiling the library three times.
Answer: One approach, used in containers, is to use CPP to expose some bindings conditionally, depending on whether or not a test build is being run.
The correct approach, in most cases, is to expose your internals module, with a name like Blah.Blah.Internal, which lets your users play games behind your back while solemnly (but implicitly) warning them that they'd better be careful and that they can't rely on anything in there staying the same from version to version.
Comment: Considering the scope of the project, it seems like using an internal module is probably the way to go, especially since I don't have another convincing reason to use CPP. Unfortunately this doesn't solve the over-inclusion of dependencies problem.


how to refer to folder's name from configuration file

Question: in the below posted code i want to refer to the folder's name from the configuration file. the name of the folder is saved in the configurarions and i want to refer to it.
the below code when i run it it generates an error.
is it possible to refer to the folder name from the configuration file. if yes, please let me know how.
config
<code>[Grid]
cell_width=10
cell_height=10
geopandas_output_folder=GeopandasOutput
</code>
code:
<code> try:
        os.mkdir('.'/config['Grid']['geopandas_output_folder'])
    except FileExistsError:
        print('folder already exists!')
</code>
update:
<code>    try:
        os.mkdir('./' + config['Grid']['geopandas_output_folder'])
    except FileExistsError:
        print('folder already exists!')
    boundingGeometryAsGDF.to_file(filename='./' + config['Grid']['geopandas_output_folder'] + '/' + 'boundingGeometry2.shp', driver="ESRI Shapefile")
    gdpolygonAsGDFf.to_file(filename='./' + config['Grid']['geopandas_output_folder'] + '/' + 'gdf__%d.shp'%(cnt), driver="ESRI Shapefile")

error received:
File "fiona/ogrext.pyx", line 1054, in fiona.ogrext.WritingSession.start
fiona.errors.DriverIOError: Failed to create file GeopandasOutput\boundingGeometry2.shp: No such file or directory
</code>
Comment: you can read the config file and then split the line to get the folder name
Answer: Maybe the error is due to incorrect string construction:
<code># this gives an error
out_path = '.'/config['Grid']['geopandas_output_folder']

# this doesn't
out_path = './'+config['Grid']['geopandas_output_folder']

# then you do:
try:
    os.mkdir(out_path)
except FileExistsError:
    print('folder already exists!')
</code>
Comment: i tried it but it does not worl.please have a look at the update section added above


Efficiently tuning Hyperparameters in Gaussian Process Regression

Question: I am trying to tune the hyperparameters of the gaussian process regression algorithm I've implemented. I simply want to maximize the log marginal likelihood given by the formula $$\log(\mathbf{y}|X,\mathbf{\theta})=-\frac{1}{2} \mathbf{y}^TK_y^{-1}\mathbf{y}-\frac{1}{2}\log(\det(K))-\frac{n}{2}\log(2\pi)$$
where $K$ is the covariance matrix with the elements $$K_{ij}=k(x_i,x_j)=b^{-1}\exp(-\frac{1}{2}(x_i-x_j)^TM(x_i-x_j))+a^{-1}\delta_{ij}$$ where $M=lI$ and $a,b$ and $l$ are hyperparameters.
partial derivative of the log marginal likelihood w.r.t parameters is given by the following $$\frac{\log(\mathbf{y}|X,\mathbf{\theta})}{d\theta}=\frac{1}{2}\mathrm{trace}(K^{-1}\frac{dK}{d\theta})+\frac{1}{2}(\mathbf{y}\frac{dK}{d\theta}K^{-1}\frac{dK}{d\theta}\mathbf{y})$$
As the entries of $K$ depend on the parameters, so do derivatives and inverse of $K$. This means, when a gradient-based optimizer is employed, evaluating the gradient at a given point (parameter value) will require recomputation of the covariance matrix. In my application, this is not feasible because computing the covariance matrix from scratch and computing its inverse in every iteration of gradient ascent is too expensive. My question is what my options are to find a fairly good combination of these three parameters? and I also don't know which parameter to optimize first and I would appreciate any pointers on this issue as well.
Comment: may i recommend local gaussian processes? https://cran.r-project.org/web/packages/laGP/index.html
Comment: otherwise, if youre comfortable with math, what's hot in the field nowadays are some kinda krylov subspace approach to linear system solution / determinant calculation such as https://arxiv.org/pdf/1803.06058.pdf
Answer: You are right that you need a new covariance matrix computation on every iteration of gradient ascent. So if the matrix computation is not feasible for your setting, then, I think, you cannot use gradient-based marginal likelihood optimization.
My suggestion is to use gradient-free methods for hyperparameter tuning, such as grid search, random search, or Bayesian optimization-based search. These methods are widely used for optimization hyperparameters of other machine learning algorithms e.g. SVMs.
I suggest the grid search for your first try. You basically form a table (grid) of possible hyperparameters, try every one, and look for the best validation performance (or best marginal likelihood).
Grid search would yield a suboptimal set of hyperparameters, and you have to specify grid by yourself.(tip: make grid in a log scale) but far less computation is needed. (and you don't need gradient!)
If you are not familiar with grid search, you can look up Wikipedia:Hyperparameter Optimization - Grid Search
Answer: If solving the linear problem $K\pmb{\alpha} = \textbf{y}$ is too expensive for you at each step of your optimisation, you could resort to approximation techniques such as the Nyström method (e.g., Williams and Seeger 2001), such that if $K \in \mathbb{R}^{n \times n}$, the Nyström method computes a rank $m$ approximation of $K$, where $m < n$. Standard inference in the GP setting has run time complexity $\mathcal{O}(n^3)$, while in the Nyström approach it is $\mathcal{O}(nm^2)$.
In a bit of detail:
First, select $m$ random samples from your inputs, $X_{mm}$, which are then referred to as the active set. Next, compute the relevant (noise free) submatrix $K_{mm} = k(X_{mm},X_{mm})$. The eigenvectors and eigenvalues of $K_{mm}$, denoted $\textbf{u}$ and $\lambda$ respectively, can then be extended to all $n$ data points by the following:
$\tilde{\lambda} = \dfrac{n}{m}\lambda \\
\tilde{\textbf{u}} = \sqrt{\dfrac{m}{n}}\lambda^{-1}K_{nm}\textbf{u}$
where $K_{nm} = k(X,X_{mm})$.
Notice now how the reduced-rank approximation of $K$ is given by:
$\tilde{K} = (\tilde{\lambda}\tilde{\textbf{u}})\tilde{\textbf{u}}^\text{T}$
where in the case $m = n$, $\tilde{K} = K$.
Now, computing the inverse of $\tilde{K} + \pmb{V}$, where $\pmb{V} = a\pmb{I}$, simply requires use of the Woodbury identity:
$(\pmb{V} + (\tilde{\lambda}\tilde{\textbf{u}})\tilde{\textbf{u}}^\text{T})^{-1} = \pmb{V}^{-1} - \pmb{V}^{-1}\tilde{\textbf{u}}\big(diag(\tilde{\lambda}^{-1}) + \tilde{\textbf{u}}^\text{T}\pmb{V}^{-1}\tilde{\textbf{u}}\big)^{-1}\tilde{\textbf{u}}^\text{T}\pmb{V}^{-1}$
which is now much cheaper, because $\big(diag(\tilde{\lambda}^{-1}) + \tilde{\textbf{u}}^\text{T}\pmb{V}^{-1}\tilde{\textbf{u}}\big)^{-1}$ is of size $m \times m$.
Finally, the evaluation of $det(\tilde{K} + \pmb{I}a)$ (required for computing the log marginal likelihood), can also be derived cheaply. First let us define $\tilde{L} = \sqrt{\tilde{\lambda}}\tilde{\textbf{u}}$, such that $\tilde{L}\tilde{L}^\text{T} = \tilde{K}$. Now, we can use Sylvester's determinant theorem, which states that
$det(\pmb{I}_na + \tilde{L}\tilde{L}^\text{T}) = det(\pmb{I}_ma + \tilde{L}^\text{T}\tilde{L})$
The LHS of the above definition has complexity $\mathcal{O}(n^3)$, while on  the RHS it is $\mathcal{O}(m^3)$.
Comment: I would add as a comment here that in practice it may be necessary to set any negative values in $\lambda$ to something small like 1e-12. Furthermore, for the computation of the determinant I would recommend something like numpy.linalg.slogdet if using Python, as this handles very small (or large) determinants better, and the log of the determinant is anyway required for the log marginal likelihood


SAS use a lookup dataset like array in another dataset

Question: I have 1 data set with content description for a school
contents:
num       description
content1  math
content2  spanish
content3  geography
content4  chemistry
content5  history

in another data set (students) i have  the array content1-content5 and i use a flag to indicate content that have each student.
students
name age content1 content2 content3 content4 content5
BOB  15     1        1        1                 1
BRYA 16
CARL 15              1                          1
SUE  17                       1        1        1
LOU  15                                         1

if i use a code like this:
    data students1;
    set students;
    array content[5];
    format allcontents $100.;
    do i=1 to dim(content);
    if content[i]=1 then do;
    allcontents=cat(vname(content[i]),',',allcontents);
    end;
    end;
    run;

the result is:
name age content1 content2 content3 content4 content5 allcontents
BOB  15     1        1        1                 1     content1,content2,content3,content5,
BRYA 16
CARL 15              1                          1     content2,content5,
SUE  17                       1        1        1     content3,content4,content5,
LOU  15                                         1     content5

1) i want to use the name of the lookup table (data set contents) to use the name of the content and not the arrays names of content[1-5] in the variable allcontents. how can i do that?
2) and later i want the result by content description, not by student, like this:
description  name age
math         BOB  15
spanish      BOB  15
geography    BOB  15
history      BOB  15
spanish      CARL 15
history      CARL 15
spanish      SUE  17
chemistry    SUE  17
history      SUE  17
history      LOU  15

is it possible?
thanks.
Answer: First, grab the %create_hash() macro from this post.
Use the hash table to look up the values.
data students1;
set students
array content[5];
format num $32. description $16.;
if _n_ = 1 then do;
   %create_hash(cnt,num,description,"contents");
end;
do i=1 to 5;
   if content[i]=1 then do;
      num = vname(content[i]);
      rc = cnt.find();
      output;
   end;
end;
keep description name age;
run;

Answer: I find proc transpose suitable. Doing once is enough for question 2) and twice for renaming the variables contents1-5 (hence question 1). The key is the ID statement in proc transpose which automatically rename variables by their corresponding transposed orders.
The code below should give you the desired answers (albeit the name are ordered alphabetically, which may not be the same as your original ordering).
/* original data sets */
data names;
    input num $ description $;
    cards;
content1 math
content2 spanish
content3 geography
content4 chemistry
content5 history
;run;

data students;
    input name $ age content1 content2 content3 content4 content5;
    cards;
BOB  15     1        1        1        .        1
BRYA 16     .        .        .        .        .
CARL 15     .        1        .        .        1
SUE  17     .        .        1        1        1
LOU  15     .        .        .        .        1
;run;

/* transpose */
proc sort data=students out=tmp_sorted;
    by name age;
run;

proc transpose data=tmp_sorted out=tmp_transposed;
    by name age;
run;

/* merge the names of content1-5 */
* If you want to preserve ordering from contents1-contents5
* instead of alphabetical ordering of "description" column
* from a-z, do not drop the "num" column for further use.;
proc sql;
    create table tmp_merged as
    select B.description, A.name, A.age, B.num, A.COL1
    from tmp_transposed as A
    left join names as B
        on A._NAME_=B.num
    order by A.name, B.num;
quit;

/* transpose again */
proc transpose data=tmp_merged(drop=num) out=tmp_renamed(drop=_name_);
    by name age;
    ID description; *name the transposed variables;
run;

/* answer (1) */
data ans1;
    set tmp_renamed;
    array content[5] math--history;
    format allcontents $100.;
    do i=1 to dim(content);
        * better use cats (cat does not seem to work);
        if content[i]=1 then allcontents=cats(allcontents,',',vname(content[i]));
    end;
    *kill the leading comma;
    allcontents=substr(allcontents,2,99);
run;

/* answer (2) */
data ans2(drop=num col1);
    set tmp_merged;
    where col1=1;
run;

*cleanup;
proc datasets lib=work nolist;
    delete tmp_:;
quit;


Does Delayed::Worker.new.work_off work jobs with a future run_at time?

Question: I have a controller action that creates 2 background jobs to be run at a future date.
I am trying to test that the background jobs get run
<code>    # spec/controllers/job_controller_spec.rb

    setup
    post :create, {:job => valid_attributes}
    Delayed::Job.count.should == 2
    Delayed::Worker.logger = Rails.logger
    #Delayed::Worker.new.work_off.should == [2,0]

    Delayed::Worker.new.work_off
    Delayed::Job.count.should == 0 # this is where it fails
</code>
This is the error:
<code> 1) JobsController POST create with valid params queues up delayed job and fires
    Failure/Error: Delayed::Job.count.should == 0
    expected: 0
    got: 2 (using ==)
</code>
For some reason it seems like it is not firing.
Answer: You can try to use
<code>Delayed::Worker.new(quiet: false).work_off
</code>
to debug the result of your background jobs, this could help you to find out if the fact that they're supposed to run in the future is messing with the assert itself.
Don't forget to take off the "quiet:false" when you're done, otherwise your tests will always output the results of the background jobs.
Comment: Thanks! The tip was useful for me in figuring out a failing test.
Comment: I'm really glad it was useful for anyone besides me :D
Comment: Didn't work for me. Any other idea?


ASP.NET - rewriting all https requests to http

Question: My issue is precisely the one presented here, and I've decided to try rewrite all https requests to http. I've searched long and hard but there doesn't seem to be a definitive way to achieve this - see these questions (no solutions): Redirect https to http using rewrite rule in webconfig file ; https://stackoverflow.com/questions/15214717/iis-rewrite-https-to-http-whilst-keeping-existing-https-rules
I've added the rewrite module to IIS, and tried the following in web.config:
<rewrite>
  <rules>
    <clear />
    <rule name="force http" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

But it still allows the user to access a non-https site with https (essentially accessing a different site).
How do I force all https requests to be http requests?
edit: I've also tried every suggested solution here with no luck. The url rewrite module is definitely successfully installed on IIS!
edit2: Tried the following without success:
<system.webServer>
<rewrite>
  <rules>
    <clear />
    <rule name="force http" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll">
        <add input="{HTTPS}" pattern="on" ignoreCase="true" />
        <add input="{HTTP_HOST}" pattern="^(?:www)?\.test.site\.com$"
            negate="true" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}"
            redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>
</system.webServer>

I restarted IIS and the rewrite rules reflect in inetmgr. Loading https://test.site.com/ still loads with https.
Comment: Do you have the option of adding an additional IP address to the server so the bindings could be changed?
Comment: There is that option but I'd like to get URL rewriting to work as expected
Comment: Asked about the IP Address as it seems that the URL Rewrite module is designed to handle processing only after the request has been assigned to an IIS site, since the match URL starts after the host. I think the IP address is preferably, but it can be accomplished without it. See answer below.
Answer: A couple of things. First the rewrite needs to process when HTTPS is on and not off. Second, for the application that needs to run over HTTPS you will need to exclude it from the rewrite. The revised rewrite rule should look something like this:
<rewrite>
  <rules>
    <clear />
    <rule name="force http" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAll">
        <add input="{HTTPS}" pattern="on" ignoreCase="true" />
        <add input="{HTTP_HOST}" pattern="^example\.com$"
            negate="true" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}"
            redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

This should keep https://example.com/login on https and all other URL's will get redirected to http. For example, https://test.example.com/login will be redirected to http://test.example.com/login. This rewrite rule needs to be placed on the site with the HTTPS binding for the rewrite to work properly.
Please be aware when using a 301 permanent redirect some browsers won't make the request out to the server on subsequent hits so after changing the rule a browser cache clear is required. The network tab may even lie and say the request is made but an external tool like Fiddler or Wireshark will let you know for sure.
Comment: Thanks a lot beavel! The sites are on the same domain: `https://www.site.com/` and `http://test.site.com/` - will this be a problem? Could you maybe update your answer if anything needs to change. I will test soon..
Comment: That shouldn't be an issue as it will match everything that isn't www.site.com. If your users expect to access www.site.com as site.com then the pattern would need to like so: ^(?:www)?\.site\.com$  that will make the www optional.
Comment: I updated my question. Does the pattern account for paths after the `.com`? `https://test.site.com/login`
Comment: @user982119 I've updated my answer to reflect the new pattern and example. Please see my warning about testing as well.
Comment: Cleared cache. It doesn't redirect.
Comment: @user982119 Looking at the question again. This will leave `https://test.site.com/login` and `https://site.com/login` on https and everything else will get redirected. I'm updating my answer to reflect.
Comment: _Everything_ needs to redirect from https to http.
Comment: @user982119 If everything needs to be http, why do you have any bindings for https and an SSL cert assigned? The link you provided describes the problem I have solved of having one SSL site with all remaining sites not loading over SSL. I'm not sure what you are trying to solve at this point. If you don't want to support https, don't register the bindings.
Comment: By everything I mean everything on `test.site.com`. The only site with https bindings is `site.com`, but explicitly loading `https://test.site.com/` should not load anything, or it should force normal http. Right now the user can request `https://test.site.com/` and the server will actually load `site.com`. Don't know how else to explain the problem. If you want a bounty just say so :P
Comment: I've revised my answer again to reflect your most recent explanation. I would recommend being this explicit up front next time as _Everything_ doesn't really help as it is unclear.


Moq setup for interface with function argument

Question: I want to write an UnitTest with Moq 4.14.7. The method to be tested is as follows:
public IFoo Get(Guid id)
{
    Func<IFoo> getData = () => _repository.Get(id);
    if (_cache.TryGetAndSet(vaultId, getData, out var result))
    {
        return result;
    }
}

To briefly explain. I have a generic cache class. It checks in the cache if an entity with the passed guid exists. If this is not the case, the repository is called.
Now I have the following setup for the cache:
_cache.Setup
(
    x => x.TryGetAndSet
    (
        _vaultId,
        It.IsAny<Func<IFoo>>(),
        out foo
    )
)
.Returns(true);

The test works so far, but I do not achieve 100% coverage with it, because the func is not covered.
Is there a better way here than to work with It.IsAny and get 100% coverage?
Comment: That test covers 100% of the code you've shown. What does "because the func is not covered" mean? I'm guessing you need another test that returns false?
Comment: but isn´t the func what you want to mck? So why should it be covered at all?
Comment: Well without a copy of this tool I don't know what it' complaining about. If you add another test that returns false does it stop complaining? BTW [100% code coverage is a nonsense](https://martinfowler.com/bliki/TestCoverage.html)
Comment: No with the ReSharper UnitTestCoverage tool only 88% coverage is achieved and the func is shown as not covered.
Comment: your original `GetData`-func surely is never executed - and thus not covered - because you mock the only calling function - `TryGetAndSet`.
Comment: @HimBromBeere if I do a `_repo.Setup(x => x.Get(id)).Returns(It.IsAny);` before, it is still not called, because I have an `It.IsAny>()` in the cache mock.
Comment: @Liam No unfortunately it still complains.
Comment: the delegate will never be executed, because the only function that ever would call it is mocked away. You essentially say: replace **every** call to `TryGetAndSet` by something completely different that won´t ever touch the delegate at all.
Comment: I have also tried in the cache mock instead of the `It.IsAny<>` a `() => _repository.Setup(m => m.Get(id)).Returns(foo)` but it expects an IFoo as return value, but gets the repo.
Answer: Capture the arguments passed to the member and invoke the function as desired.
//...

IFoo mockedFoo = Mock.Of<IFoo>();

IFoo foo = null;

_repo.Setup(_ => _.Get(It.IsAny<Guid>()).Returns(mockedFoo);

_cache
    .Setup(_ => _.TryGetAndSet(It.IsAny<Guid>(), It.IsAny<Func<IFoo>>(), out foo))
    .Returns((Guid vid, Func<IFoo> func, IFoo f) => {
        foo = func(); //<-- invoke the function and assign to out parameter.
        return true;
    });

//...

//Assertion can later verify that the repository.Get was invoked
//Assertion can later assert that foo equals mockedFoo

TryGetAndSet will return true, and the out argument will return the mockedFoo, lazy evaluated
Reference Moq Quickstart


how to exclude PHPUnit based functional tests from runnning?

Question: I am trying to run only unit test for my custom module.
./vendor/bin/phpunit -c ./web/core ./web/modules/custom/testing_example --testsuite=unit .
But I keep getting the following error:
There were 2 errors:

1) Drupal\Tests\testing_example\Kernel\ExampleFixtureManagementTest::testNodeCreation
Exception: There is no database connection so no tests can be run. You must provide a SIMPLETEST_DB environment variable to run PHPUnit based functional tests outside of run-tests.sh. See https://www.drupal.org/node/2116263#skipped-tests for more information.

I only want to run PhpUnit tests and not functional/browser tests.
Answer: Since those tests are grouped by namespace/directory, you can just pass the respective path to phpunit, simply add tests/src/Unit to your existing path.
Comment: I want to run all the unit tests in the custom modules directory I'm not sure if you mean I can add it as an extra path parameters or this as path/to/module/tests/src/Unit?
Comment: I was wondering about that, but your question specifically mentiones a single module and provides the path to that ;) Not sure if combining testsuite and a path is the problem, I would suggest you create your own phpunit.xml based on the example, adjust the include path and leave out the path in the command
Answer: To run only certain tests in phpunit, append --filter (https://phpunit.de/manual/6.5/en/textui.html) in your phpunit call whether directly in cli or in an IDE phpunit config.
To filter to only Unit tests across all modules but exclude Functional and Kernel tests, you can pass in something that matches the well-structured Drupal tests path like @Berdir was getting at like --filter Unit.
I suspect there's a more specific way to narrow it down like --filter tests/src/Unit but I didn't figure it out.  I assume one can also combine filters to pick and chose among Unit and Kernel, for instance, but I also didn't play with that. :)


How Do I write a client using axis2 to send a serialized xml object to a web service?

Question: I'm having a conceptual problem preventing me from solving a trivial problem. I need to send an object to a web service. I have an endpoint, and I have code that can serialize the object, so I can create an org.jdom.Document or a byte[] object containing the serialized object.
I can also create a client snippet that uses axis2 to invoke the web service.
Finally I have tried sending a manually created message to the web service (it has no WSDL ;( )
AND I have used Charles to see what is going out (the request).
What I don't know how to do is convert the byte[] or org.jdom.Document object to an OMElement object. Evidently the serviceClient.sendReceive(elem) takes an OMElement parameter.
Here is what I tried so far (I removed the OMElement that I sent out once I was convinced it was going out):
<code>package testAxis2Client01;

import java.util.Map;

import javax.xml.stream.XMLStreamException;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;

public class testAxis2Client01 {

               private static final int MXMOCONNECTIONTIMEOUT = 2;//don't really know what this should be.
               /**
               * @param args
               */
               public static void main(String[] args) {
                              try {
                                             callAxisWS();
                              } catch (XMLStreamException e) {
                                             e.printStackTrace();
                              } catch (Exception e) {
                                             e.printStackTrace();
                              }

               }
public static void callAxisWS() throws XMLStreamException, Exception {

                              //Axis2 client code to call a WS

                              OMElement response=null;
                              try{
                                             OMFactory factory = OMAbstractFactory.getSOAP11Factory();
                                             SOAPEnvelope theEnvelope = OMAbstractFactory.getSOAP12Factory().getDefaultEnvelope();
                                             theEnvelope.declareNamespace("http://www.w3.org/2001/XMLSchema","xsd");
                                             theEnvelope.declareNamespace("http://www.w3.org/2001/XMLSchema-instance","xsi");

                                             ServiceClient serviceClient = new ServiceClient();
                                             Options options = serviceClient.getOptions();
                                             options.setProperty(HTTPConstants.AUTO_RELEASE_CONNECTION, true);               // Another API to release connection.
                                             options.setTimeOutInMilliSeconds(10000); // Setting the connection timeout.
                                             EndpointReference targetEPR = new EndpointReference(theUrl);
                                             options.setTo(targetEPR);
                                             options.setAction("processDocument");

                                             serviceClient.setOptions(options);
                                             //response = serviceClient.sendReceive(myOMElement);
                                             response = serviceClient.sendReceive(elem)
                                             if (response != null) {
                                                            System.out.println("SUCCESS!!");
                                                            System.out.println(response.toStringWithConsume());
                                             }
                              }catch(Exception af){
                                            af.printStackTrace();
                                            System.out.println(af.getMessage());
                              }

               }
}
</code>
Answer: The point of using axis2 is that it takes care of everything. You only have to provide a wsdl file and it will generate client stubs.
If you do not have an original wsdl, you can still make one yourself.
The best way for you is to create the wsdl file manually, generate the client stub and call the stub directly.
Comment: There is no WSDL. They are unable to supply a WSDL. This work was done a long time ago.
Comment: this code seems to work however occasionally, if the request gets to a certain size I notice the response is missing many children:


Order of an element of direct product of groups.

Question: Let $(a_1,a_2,\cdots,a_n) \in G_1 \oplus \cdots \oplus G_n$. Give a necessary and sufficient condition for $|(a_1,a_2,\dots,a_n)| = \infty$.
I think if one $a_i$ has order $\infty$, then this would give a necessary condition. It is sufficient for the order of the direct product to be $\infty$ when only one of the $a_i$ has order $\infty$?
Thanks,
Answer: The order of $(a_{1},a_{2}, \cdots , a_{n}) \in \oplus_{i} G_{i}$ is the least common multiple of the orders of the $a_{i} \in G_i$, so it is bounded by the product of the orders. Does this help you answer your question?
Comment: So, if one of a_i has order $\infty$, we are done. is that correct?
Comment: That is correct.


Cabeçalho pdf com posição absoluta

Question: Boa tarde a todos!!
Eu estou gerando o pdf de uns dados com o mpdf, e tudo funciona exceto pelo cabeçalho que é gerado e parece estar configurado com 'position:absolute", tem como mudar isso?
o resto do conteudo está em div....faz alguma diferença??
o código que gera o cabeçalho é o:
<code>$mpdf->SetHTMLHeader($Header,'O');
</code>
Sem mais,obrigado a todos pela atenção
Answer: Bom dia a todos!!
Consegui solucionar esse problema aplicando configurações ao construtor da classe da seguinte forma:
<code>$mpdf = new mPDF('utf-8', '','','',5,5,70,5);
</code>
Aplicando essas configurações, consegui posicionar adequadamente o topo!

Cabeçalho pdf com posição absoluta


Convert nested dictionary to csv

Question: I have a particular case where I want to create a csv using the inner values of a nested dictionary as the keys and the inner keys as the header. The 'healthy' key can contain more subkeys other than 'height' and 'weight', but the 'unhealthy' will either ever contain None or a string of values.
My current dictionary looks like this:
{0: {'healthy': {'height': 160,
                 'weight': 180},
    'unhealthy': None},
 1: {'healthy': {'height': 170,
                'weight': 250},
    'unhealthy': 'alcohol, smoking, overweight'}
}

How would I convert this to this csv:
+------+--------+----------------------------+
|height|  weight|                   unhealthy|
+------+--------+----------------------------+
|160   |     180|                            |
+------+--------+----------------------------+
|170   |     250|alcohol, smoking, overweight|
+------+--------+----------------------------+

Is there anyway of not hardcoding this and doing this without Pandas and saving it to a location?
Comment: Please share what you have attempted and the outcome, including source code and errors.
Comment: @learningtoprogram123 If you find a solution here, please accept the answer so that people can see what answer help you resolve the issue.
Answer: With D being your dictionary you can pass D.values() to pandas.json_normalize() and rename the columns if needed.
>>> import pandas as pd
>>> print(pd.json_normalize(D.values()).to_markdown(tablefmt='psql'))
+╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+
|    | unhealthy                    |   healthy.height |   healthy.weight |
|╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌|
|  0 |                              |              160 |              180 |
|  1 | alcohol, smoking, overweight |              170 |              250 |
+╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌+

Answer: So this may be very dumb way to do this, but if your dictionary has this structure and you don't mind about hardcoding the actual values, this might be the way
import csv

dictionary = {0: {'healthy': {'height': 160,
                              'weight': 180},
                  'unhealthy': None},
              1: {'healthy': {'height': 170,
                              'weight': 250},
                  'unhealthy': 'alcohol, smoking, overweight'}
              }

with open("out.csv", "w") as f:
    writer = csv.writer(f)
    writer.writerow(['height', 'weight', 'unhealthy'])
    writer.writerows([
        [value['healthy']['height'],
         value['healthy']['weight'],
         value['unhealthy']
         ] for key, value in dictionary.items()])

So the point is you just create an array of [<height>, <weight>, <unhealthy>] arrays and write it to csv file using python's builtin module's csv.writer.writerows()
Comment: I like this 'cause it's not Pandas, but I think the list comprehension is hard to comprehend/hides meaning. How would you feel about editing to show an explicit loop?
Answer: I used pandas to deal with the value and to save it as a csv file.

I loaded the json format data as a dataframe.
By transposing the data, I got 'unhealty' columne
By using json_normalize(), I parsed the nested dictionary data, 'healthy' and generated two columns into 'height' and 'weight'
concat the 'healthy' and 'height', 'weight' columns
saved the dataframe as a csv file.

import pandas as pd

val = {
    0: {'healthy': {'height': 160, 'weight': 180},
        'unhealthy': None},
    1: {'healthy': {'height': 170, 'weight': 250},
        'unhealthy': 'alcohol, smoking, overweight'}
}

df = pd.DataFrame.from_dict(val)
df = df.transpose()
df = pd.concat([pd.json_normalize(df['healthy'], max_level=1), df['unhealthy']], axis=1)
df.to_csv('filename.csv', index=False) # A csv file generated.

This is the csv file I generated (I opend it using MS Excel).

Convert nested dictionary to csv


Springdoc Open API UI - BaseLayout doesn't works

Question: I went through: https://springdoc.org/springdoc-properties.html#springdoc-openapi-core-properties before raisng the question, but look like things are not working.
I want to use BaseLayout and also wanted to change the theme of the Open API Specification response as its in black.
I am using Spring Boot v2.2.2.RELEASE and springdoc-openapi-ui V1.2.30.
# Open API Configurations
springdoc.api-docs.path=/api-docs
springdoc.swagger-ui.path=/swagger-ui-ref.html
springdoc.swagger-ui.display-request-duration=true
springdoc.swagger-ui.tagsSorter=alpha
springdoc.swagger-ui.doc-expansion=no
springdoc.swagger-ui.layout=BaseLayout

Even after putting the above configurations, default configuration is not taking into an effect.
Answer: This issue is fixed since v1.2.31:

https://github.com/springdoc/springdoc-openapi/issues/536


Docker Compose Dev and Production Environments Best Workflow

Question: I've built a simple Docker Compose project as a development environment. I have PHP-FPM, Nginx, MongoDB, and Code containers.
Now I want to automate the process and deploy to production.
The docker-compose.yml can be extended and can define multiple environments. See https://docs.docker.com/compose/extends/ for more information.
However, there are Dockerfiles for my containers. And for the dev environment are needed more packages than in production.
The main question is should I use separate dockerfiles for dev and prod and manage them in docker-compose.yml and production.yml ?
Separate dockerfiles are easy approach but there is code duplication.
The other solution is to use environment variables and somehow handle them from bash script (maybe as entrypoint ?).
I am searching for other ideas.
Answer: According to the official docs:

... you'll probably want to define a separate Compose file, say
  production.yml, which specifies production-appropriate configuration.
Note: The extends keyword is useful for maintaining multiple Compose
  files which re-use common services without having to manually copy and
  paste.
Comment: Yes, I did but I still have to install some packages for dev and prod environments and this is done in Dockerfile. But dockerfiles doesn't have control structures like other languages so you can't control what to install depending on the environment.
Comment: in that case, you might want to consider using ansible. it works like this: use a Dockerfile just to bootstrap the image with ansible, then let ansible take over. If you want to go all the way and automate everything then, you would start with ansible playbooks to create Docker images on your local machine; then push them to a private (or public) registry (also run within docker :) then write ansible playbooks to fetch those images on remote hosts and start containers
Comment: That could be solved by having separate Dockerfiles for development and production (I guess you could have a common base as well).
Answer: In docker-compose version >= 1.5.0 you can use environment variables, may be this suits you?
Answer: If the packages needed for development aren't too heavy (i.e. the image size isn't significally bigger) you could just create Dockerfiles that include all the components and then decide whether to activate them based on the value of an environment variable in the entrypoint.
That way you would could have the main docker-compose.yml providing the production environment while development.yml would just add the correct environment variable value where needed.
Answer: In this situation it might be worth considering using an "onbuild" image to handle the commonalities among environments, then using separate images to handle the specifics. Some official images have onbuild versions, e.g., Node. Or you can create your own.


Finding the numbers of functions $ f : \{1, 2, 3, 4, 5\} \to \{1, 2, 3, 4, 5\} $ such that $ |f[A]| = 2 $

Question: Let $ A = \{1, 2, 3, 4, 5\} $. I want to find the number functions $ f : A \to A $ such that $ \left|f\left[A\right]\right| = 2$. Did I do it correctly?
Choose $ 2 $ random elements, $ i, j $ from $ A $ - there are $ \binom{5}2 = 10 \, $ ways to do so.
$ \forall x \in A, f\left(x\right) = i \lor f\left(x\right) = j $. Therefore, there are $ 10\cdot2^5 $ such functions. However, we need to subtract the cases where $ f\left[A\right] = \{i\} \lor f\left[A\right] = \{j\}, $ there are $ 5  $ such cases and therefore there are, overall, $ 315 $ functions that satisfy the requirement.
Comment: the first pass counts each constant function multiple times, not just twice.
Answer: Not quite, but very close. For a given $i$ and $j$, you need to substract the cases where $ f\left[A\right] = \{i\} \lor f\left[A\right] = \{j\}$. There are $2$ such cases: either all elements map to $i$ or all map to $j$. (Thus, for a given $i$ and $j$, there are $2^5-2$ functions with $f\left[A\right] = \{i,j\}$.)
All in all, there are $10\cdot \left(2^5-2\right) = 300$ functions $f$ with this property.


Hide DIV if all option disabled

Question: Simple question but and I need simple solution.
I need to hide div if all option are disabled.

.HIDE select option:disabled {
  display: none;
}

  <select>
    <option disabled>1</option>
    <option disabled>2</option>
    <option disabled>3</option>
  </select>

I need to hide  than all  are disabled.
Here is fiddle demo: http://jsfiddle.net/beauto/fey1x52o/6/
Comment: CSS can't help you here. You would need to use JavaScript or hide the div before generating the HTML. Presumably you have some logic that disables the options that could be leveraged?
Answer: You can use jquery

function check(){
  var disabled=false;
  $.each($("option"), function( i, l ){
   if($(l).attr("disabled")!="disabled"){
      disabled=true
   }
  });
  console.log(disabled);
  if(!disabled){$("select").css({"display":"none"})}
}
check();
.HIDE > select option:disabled {
    display:none;
}

<select>
<option disabled>1</option>
<option disabled>2</option>
<option disabled>3</option>
</select>

Comment: OP is looking for a CSS only solution, thats why. and That's why the question is closed
Comment: @pc_coder possible somehow hide  if one value selected others disabled?
Demo: http://jsfiddle.net/beauto/fey1x52o/13/
Comment: Yes possible http://jsfiddle.net/gywc3h5j/ just checking if count of not disabled elements equals one .

Hide DIV if all option disabled


Ruby pdf-reader adds blank rows where none exist when parsing PDF

Question: I'm building a tool to parse PDFs of CVs downloaded from Linkedin. But when I parse the document, pdf-reader finds blank rows where none exist to the eye.
I'm wondering if this is a problem with the PDF or the PDF Reader?
Script
require 'pdf-reader'

reader = PDF::Reader.new("CV.pdf")

reader.pages.each_with_index do |page, page_num|
    bits = page.text.split("\n")

    bits.each_with_index do |bit, index|
        puts bit
    end
end

Snapshot of CV

Parsed Output
Senior Analyst
September 2013 - August 2015 (1 year 11 months)
  - Managed a team of analysts to drive operational improvement across all departments of a subsidiary

   company, including a 33% increase in conversions on their e-commerce platform and a 8% decrease in
   logistics costs within my first year

You'll notice in the outputted text that there is a blank line added to the middle of the sentence.
It seems odd, because if you look at the screenshot of the PDF, it doesn't appear to have any blank row where the pdf reader finds one.
Does anyone have experience with what might cause this?
Answer: After trying other libraries, it appears the problem was pdf-reader, not the PDF.
So I decided on another gem called Yomu (see: https://github.com/Erol/yomu), which avoided the issue.
Its a little bit slower, but acceptable given the increased predictability of output.


Why do discretised predictors have lower statistical power than continuous predictors?

Question: In designing an analysis, I'd like to decide between using discretised variables versus using the original, continuous variable (the reason being that in this particular case, collecting discretised data per se would be cheaper and easier).
However, my understanding is that discretised predictors lack statistical power in comparison with the continuous case. I.e. using a discretised predictor, I would have to collect substantially more data to identify an effect on a dependent variable.
My question: Is this true and what is the theoretical basis for the lack of statistical power of discretised variables?
I have found some evidence in a 1983 article "The Cost of Dichotomization" (link), but was wondering if there are alternative lines of argumentation that can be applied?
Comment: [Here is an illustration of discretizing the time of day into 24 hourly buckets.](https://stats.stackexchange.com/a/478175/1352)
Comment: You are losing information, and it is not a surprise that some of what you lose might have been useful
Answer: Smaller Variance Leads To Smaller Power
Discretizing predictors is a bad idea as Frank Harrell explains here.  As to your titular question, let's examine a simple scenario.
Let's say I have a predictor which has standard normal distribution.  I'd like test the effect of this predictor on an outcome which is also normally distributed conditional on the predictor, with unit variance.  Were I to discretize the predictor, I would have a T test on my hands.  Leaving the predictor as is would just be a regression.  Further more, let's say I collect 25 observations and am interested in a smallest effect of 1 unit of whatever I am measuring.
The power for each test is given by
$$\gamma=1-\Phi\left[z_{1-\alpha / 2}-\left|\beta_{j}^{a}\right| \sigma_{x_{j}} \sqrt{n\left(1-\rho_{j}^{2}\right)} / \sigma_{y \mid \mathbf{x}}\right]$$
Where $\Phi$ is the normal CDF, $\alpha$ is the FPR, $
\beta_j^\alpha$ is smallest meaningful effect size, $\sigma_{x_j}$ is the variance of the predictor, $n$ is the sample size, $1/(1-\rho^2)$ is the variance inflation factor, and $\sigma_{y \vert x}$ is the residual standard deviation.
Let's plug in some of the terms I've mentioned here for economy of thought
$$\gamma=1-\Phi\left[1.96- 5 \sigma_{x_{j}}\right]$$
Because I've picked nice numbers, the power depends only on the variance of the predictor.  It isn't hard to see that this is a monotonic function in $\sigma$, hence larger variability in the predictor means greater power.  When you dichotomize, the largest $\sigma$ can be is 0.5.  In this example, dichotomizing would reduce my power from 99% to nearly 70%.
This fact is true across many GLMs, as the power equations are more or less similar.  Smaller variance in the predictor leads to smaller power for the associated test.  This is rather intuitive.  If you are interested in testing for the slope of a continuous predictor, you'd want that predictor to be as spread out as possible.  You can't estimate the slope of you have a little band in which to see the variation in $y$ due to $x$.
Additional Pitfalls
Dichotomizing is also a bad idea because it leads to residual confounding and discontinuity in effects.  For example, I'm very confident a woman who is 1 month pregnant is very different than a woman who is 9 months pregnant, but using a variable like is_pregnant treats them as if they were the same.  So even if it is the case that you can collect more data by discretizing, the quality of the estimate suffers.
Comment: @COOLSerdash https://link.springer.com/book/10.1007/978-1-4614-1353-0 End of each chapter in this book
Comment: Thanks @DemetriPananos for the exhaustive and helpful answer.
Comment: +1. Could you provide a reference for the power function? I'd be very interested in reading more about that. Thank you.
Comment: Perfect, thank you!
Comment: Harrell's RMS book does mention that it can be beneficial to bin variables if it means a bunch of data. The example I remember is that people might not respond at all if you ask how much money they make, but if you ask them for the range into which they fall (perhaps a tax bracket or some similar set of ranges), you might find your increase in data quantity makes up for the binning. I found that interesting.
Comment: @Dave The approach depends on the problem at hand.  From an information theoretic perspective, dichotomous variables have the least information and continuous ones have the most (multiple categories naturally are in between).  In the case where nothing impedes estimating the effect of the continuous variable, then one should do so.  If categorizing means I can avoid pernicious missing data, then I would gladly do so with "enough" categories (whatever that means)


How to perform async continuous reading from socket instead of poll TcpClient with while(true)

Question: I'm working with an Access Control Device, that allows or denies access to rooms, verifying rights through biometric data. I need to listen indefinitely for data on a connected TcpClient(Socket). But how to do this without the following approach:
byte[] bb = new byte[1024]
while(true)
{
    if (tcpClient.Client.Available > 0)
    {
        tcpClient.Client.Receive(bb, bb.Length, SocketFlags.None);
        int k = tcpClient.Client.Receive(bb);
        string result = Encoding.Default.GetString(bb.Take(k).ToArray());
        // do sth here, rise an event, etc...
    }

    Thread.Sleep(500);
}

I'm following this example, did some adaptation (see below), but only the first chunk of data is read (if the device sends something again, the sent data are not read). I think that the EndReceive method is closing the socket or else, but I want to read the socket continuously, to raise an event every time socket reads data.
...
TcpClient tcpClient // Connected with BeginConnect
byte[] BufferData = new byte[1024]
public IAsyncResult StartReceivingData()
{
    //some code here
    return tcpClient.Client.BeginReceive(DataBuffer, 0,
        DataBuffer.Length, SocketFlags.None,
        new AsyncCallback(ReceivedData), tcpClient);
}

public void ReceivedData(IAsyncResult callerResult)
{
    TcpClient remote = (TcpClient)callerResult.AsyncState;
    int recv = remote.Client.EndReceive(callerResult);
    string stringData = Encoding.ASCII.GetString(BufferDados, 0, recv);
    //rise an event here containing stringData
}

EDIT
I'm working with .NET 3.5 and I can't upgrade to .NET 4/4.5, because this is part of a legacy system.
Comment: I think you forgot to post the most interesting part of your async sample where you are starting new `BeginReceive`... Side note: consider is `async`/`awiat` (Net 4.5) would work for you as code will *look* much simple.
Answer: TcpClient has a very useful method GetStream. I would use it.
var buf = new byte[0x10000];
var stream  = tcpClient.GetStream();
int len = await stream.ReadAsync(buf, 0, buf.Length);
while (len > 0)
{
    //your work ....
    len = await stream.ReadAsync(buf, 0, buf.Length);
}

If you are getting strings separated by newline chars, then you can also use this
var stream = new StreamReader(tcpClient.GetStream());
string line = await stream.ReadLineAsync();
while (line!=null)
{
    //...
    line = await stream.ReadLineAsync();
}

EDIT
but only the first chunk of data is read It is because BeginReceive doesn't mean you'll get a callback for every data you receive. You should call it everytime you receive your data (for ex, in ReceivedData method)
Comment: @CezarLamann -1 for taking time of people by not mentioning your requirements in question. Now I expect you to explain your reason for downvote.
Comment: Thank you for answering, @EZI . I think that even using the "await" statement, I'll still block the thread with the while loop, and as I said, I want to avoid these loops.
Comment: @CezarLamann,  No it won't. This is the whole point in using async/await.  BTW: this is much better than *busy waiting* (`if (tcpClient.Client.Available > 0)` + `Thread.Sleep`)
Comment: @CezarLamann I think you have a blocking problem `but only the first chunk of data is read`. First solve it, than you can think about the next problem if you have any.
Comment: @EZI As I said: `I need to listen indefinitely for data on a connected TcpClient(Socket). But how to do this without the "while approach"`
Comment: @CezarLamann I say, this approach doesn't block any thread. So can you justify the reason `I don't want to use while-loop`. In this form, it seems just childish. *I don't want* ... *I don't wan't* ..... :)
Comment: @CezarLamann Just forget it, and don't use my answer. I leave it here just because someone else can get benefit of it.
Comment: @EZI Since I'm working with a legacy .NET 3.5 code, that can't be upgraded to .NET 4/4.5 (which have support for async/await (.NET 4 with Bcl)) for many reasons, I couldn't do that this way. I forgot to mention this in my previous comment. It's not for a "childish" reason. I've edited the question.
Comment: @EZI Even before I mention about .NET Framework, you answered with something that I told I wouldn't use. Since I wrote I was using IAsync things instead of while loops (like you answered), why don't you answered aligned with the question?
Comment: @CezarLamann This is the reason for downvote? Then Noone should show  alternative ways to the question. Just post a code that OP thinks(with his limited knowledge) it is a solution to his problem.
Comment: @EZI Ok, you win. Edit it, I'll give your vote back.
Comment: @CezarLamann I am not interested in your -2 points (don't do it). But, since you didn't get any answer in 2 hours other than this, it think it is enough time for *redemption*


Move the axis ticks, but not the labels

Question: Is there an easy way to move the ticks of an axis with discrete values so that they are on the plot limits and midway between two labels?
My code:
p2 <- ggplot(diamonds, aes(cut, price)) +
  geom_boxplot() +
  theme_bw() +
  theme(panel.grid = element_blank())

p2 +
  geom_vline(xintercept = c(1.5, 2.5, 3.5, 4.5),
             colour = "gray92") +
  scale_x_discrete(expand = c(0,0.5))

What I would like (notice ticks on x axis):
Answer: One option or hack would be to use a continuous x scale which allows to set the breaks and ticks midway between two labels and add the "axis labels" via a geom_text or a geom_label as I do in my code below. To this end convert variable to be mapped on x to a numeric.
library(ggplot2)

ggplot(diamonds, aes(as.numeric(factor(cut)), price, group = cut)) +
  geom_boxplot() +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  geom_vline(xintercept = c(1.5, 2.5, 3.5, 4.5),
             colour = "gray92") +
  scale_x_continuous(breaks = 0:5 + .5, labels = ~ rep("", length(.x)), expand = c(0, .125)) +
  geom_label(aes(label = cut, y = -Inf), data = ~ dplyr::distinct(.x, cut),
             fill = NA, vjust = 1,
             label.size = 0,
             label.padding = unit(5, "pt")) +
  coord_cartesian(clip = "off")

Move the axis ticks, but not the labels


Prove that among any 12 consecutive positive integers there is at least one which is smaller than the sum of its proper divisors

Question: Prove that among any 12 consecutive positive integers
there is at least one which is smaller than the sum of
its proper divisors. (The proper divisors of a positive
integer n are all positive integers other than 1 and n
which divide n. For example, the proper divisors of 14 are 2
and 7)
Answer: Hint: Among any $12$ consecutive positive integers, there is one that is a multiple of $12$.
Can you show that $12n$ is smaller than the sum of its divisors for any positive integer $n$?
Comment: $d(12)=2+3+4+6=15>12$... Ooops, I read the question "opposite" (which is why I had a hard time proving it)
Comment: ^Yes, $12$ is smaller than the sum of its proper divisors. EDIT: Yeah, I initially read the question the other way as well, which caused me to take a long time with it.
Comment: In fact, among any 6 consecutive integers _greater than 6_ there is one such number, because $6n$ is strictly [abundant](https://en.wikipedia.org/wiki/Abundant_number) for any $n$ strictly greater than 1.
Comment: @barakmanos Since every 6th number, starting from 12, is abundant, there is a limit to the number of consecutive _deficient_ number you can get. But if you reverse the inequality, there is no limit to the number of consecutive _abundant_ numbers you can have. This is proved in comments by Jianing Song in [A094268](https://oeis.org/A094268).


RegEx para capturar espaços, exceto dentro de aspas

Question: Gostaria de uma expressão regular (PHP) para excluir os espaços duplos fora de aspas.
update tabela set¬
   nome='alberto da   silva',¬
   telefone='1234'¬
)

Quero capturar espaços duplos, enters, tabs fora das aspas.
Eu dei uma lida, e só cheguei até /\s{2,}/.
Mas como eu condiciono isso a não pegar dentro das aspas?
Comment: Fabio, a resposta abaixo resolveu o problema ou ficou faltando alguma coisa? Caso tenha resolvido, você pode aceitar a resposta, [**veja aqui**](https://pt.meta.stackoverflow.com/q/1078/112052) como e porque fazê-lo. Não é obrigatório, mas é uma boa prática do site, para indicar a futuros visitantes que aquilo resolveu o problema. Caso não tenha resolvido, basta [edit] a pergunta explicando o que faltou.
Answer: Antes de mais nada, vale lembrar que \s corresponde a espaços, TABs, quebras de linha, entre outros caracteres. Então se tiver um espaço e depois uma quebra de linha, \s{2,} vai considerar esses dois caracteres como uma coisa só.
Antes a pergunta dizia para capturar esses espaços, depois pediu para remover. Abaixo tem uma solução para ambos os casos.

Uma maneira de resolver isso é tentar capturar algo que esteja entre aspas, e caso consiga, descartar este match. Ou, se não tiver nada entre aspas, pegar a sequência de dois ou mais \s:
//coloquei 2 espaços depois de "set"
$str = "update tabela set
   nome='alberto da   silva',
   telefone='1234'
)";
if (preg_match_all('/\'[^\']*\'(*SKIP)(*F)|[^\'\s]*(\s{2,})[^\s\']*/', $str, $matches, PREG_SET_ORDER, 0)) {
    foreach ($matches as $m) {
        if (count($m) > 1) { // grupo de captura preenchido (número não está entre aspas)
            var_dump($m[1]);
        }
    }
}

A regex usa alternância (o caractere |, que significa ou), com 2 opções.
A primeira é \'[^\']*\'(*SKIP)(*F):

\'[^\']*\': começa e termina com aspas (que por estar dentro de uma string delimitada por ', deve ser escrito como \'), e entre as aspas tem [^\']* (zero ou mais caracteres que não são ')
(*SKIP)(*F): são "verbos de controle" que... controlam a engine de regex. Neste caso, elas fazem com que a regex "desista" do match encontrado (ou seja, ela descarta o texto que está entre aspas) - existe uma explicação mais detalhada sobre estes "verbos" aqui. Buscar o texto e depois descartá-lo me pareceu mais simples que fazer várias verificações antes e depois dos espaços para detectar que eles fazem parte de um texto que está entre aspas.

A segunda opção é [^\'\s]*(\s{2,})[^\s\']*:

[^\'\s]*: zero ou mais caracteres que não sejam \s nem '
(\s{2,}): dois ou mais \s, entre parênteses para formar um grupo de captura

Como a regex só pode pegar a palavra entre aspas ou a sequência de \s, há casos em que o grupo de captura estará preenchido (quando pegar os \s) e casos em que não estará (quando não encontrar 2 ou mais \s, ou encontrar texto entre aspas).
Sendo assim, dentro do foreach eu verifico se existe o grupo de captura (o array $m terá mais de um elemento). A saída é:
string(7) "
   "
string(5) "
   "

Ou seja, 2 ocorrências: uma corresponde aos espaços depois de "set", mais a quebra de linha, mais os espaços antes de "nome". A segunda é a quebra de linha depois de "silva'," mais os espaços antes de telefone".
Obs: se a string tiver uma quebra de linha do Windows, que consiste de 2 caracteres (\r\n), então estas também serão capturadas por \s{2,}.
Veja aqui a regex funcionando.

Para ficar um pouco mais fácil de "ver" os caracteres, você pode imprimir o valor da tabela ASCII de cada caractere:
if (preg_match_all('/\'[^\']*\'(*SKIP)(*F)|[^\'\s]*(\s{2,})[^\s\']*/', $str, $matches, PREG_SET_ORDER, 0)) {
    foreach ($matches as $m) {
        if (count($m) > 1) { // grupo de captura preenchido (número não está entre aspas)
            for($i = 0; $i < strlen($m[1]); $i++) {
                echo ord($m[1][$i]), ",";
            }
            echo PHP_EOL;
        }
    }
}

Saída:
32,32,10,32,32,32,
10,32,32,32,

Sendo que 32 é o espaço e 10 é o new line (\n).

Nos comentários você disse que quer usar preg_replace, então na verdade você não quer capturar (como estava anteriormente na pergunta, antes de ser editada), e sim substituir estes espaços.
Bom, eliminar totalmente os espaços não parece ser a opção mais adequada, pois a string tem uma query e se deixar sem espaços ela ficará inválida. Então, supondo que você queira trocar os espaços/quebras de linha/etc por apenas um espaço, daria para adaptar a regex acima:
echo preg_replace('/\'[^\']*\'(*SKIP)(*F)|([^\'\s]*)\s{2,}([^\s\']*)/', '$1 $2', $str);

É muito parecida, só que agora eu coloco os grupos de captura em volta dos [^\'\s]* antes e depois do \s{2,}. E eu substituo por '$1 $2' (o conteúdo do primeiro par de parênteses, seguido de um único espaço, seguido do conteúdo do segundo par de parênteses). Assim, eu troco a sequência de dois ou mais \s por um único espaço. A saída é:
update tabela set nome='alberto da   silva', telefone='1234'
)

Se quiser eliminar somente os espaços (mas não as quebras de linha), mude para:
echo preg_replace('/\'[^\']*\'(*SKIP)(*F)|([^\' ]*) {2,}([^ \']*)/', '$1$2', $str);

A saída passa a ser:
update tabela set
nome='alberto da   silva',
telefone='1234'
)

Mas se a ideia é remover dois ou mais espaços, ou um TAB, ou uma quebra de linha, uma opção é:
echo preg_replace('/\'[^\']*\'(*SKIP)(*F)|([^\'\s]*)(?: {2,}|[\t\n\r])+([^\'\s]*)/', '$1 $2', $str);

O trecho (?: {2,}|[\t\n\r]) é uma alternância com duas opções:

 {2,}: dois ou mais espaços (repare no espaço antes do {)
[\t\n\r]: um TAB ou quebra de linha (incluindo o \r para pegar as quebras de linha do Windows

Todo este trecho pode ocorrer uma ou mais vezes (pois tem um + depois indicando a repetição). Tudo isso é trocado por um único espaço (pois eu coloquei espaço entre $1 e $2).
Tudo isso está entre um grupo de não-captura (marcado pelo (?:), assim eu não crio grupos extras e posso continuar usando $1 $2 no segundo parâmetro (se eu não usasse ?:, seria criado outro grupo e eu teria que mudar o parâmetro para $1 $3, já que agora a regex teria 3 grupos).
Saída:
update tabela set nome='alberto da   silva', telefone='1234' )

Por fim, se a ideia é remover exatamente dois espaços (e não "dois ou mais"), basta trocar {2,} por {2}.
Comment: @FabioCarpi vc disse que queria capturar, não substituir. Por favor edite a pergunta e coloque exatamente o que vc quer fazer
Comment: @FabioCarpi Atualizei a resposta
Comment: @FabioCarpi O que exatamente você quer substituir? Dois ou mais espaços? Exatamente 2 espaços? E troca por apenas um espaço ou por nenhum? Tabs, quebras de linha, e outros, também são trocados? De qualquer forma, eu dei 2 opções de replace, uma que troca tudo por um único espaço, e outra que troca apenas 2 ou mais espaços por nenhum (e mantém tabs e quebra de linha). Mas se o seu caso for "exatamente 2 espaços" (e não "2 ou mais"), troque o `{2,}` por `{2}`
Comment: @FabioCarpi Coloquei mais uma opção de replace na resposta
Comment: @FabioCarpi Vi que tinha um erro no último replace, já corrigi
Comment: a ideia era usar um preg_replace...
Comment: Obrigado. Eu disse na pergunta "espaços DUPLOS", porque no meu editor, a tabulação é substituída por 2 espaços...
Comment: Desculpe amigo. Eu query tratar as querys pra fazer um log. Então quero tirar espaços{2,}, tabs, enters, fora das aspas.


Javascript Onclick funcionar multiplas vezes

Question: preciso fazer uma border aparecer e sumir sempre que clicar. Nao posso usar jQuery nem eventlist.
Sei que devo usar IF mas nao sei como. O que consigo ate agora eh clicar e fazer ela aparecer. Deve ser ao redor dos paragrafos.
Agradeco ajuda
 var elements = document.getElementsByTagName("p");
for (var i = 0; i < elements.length; i++){
    elements[i].onclick=function(){
    this.style.border="dashed black 1px";

}

}
Comment: Ela deve sumir quando? Um determinado tempo depois de aparecer?
Answer: É só verificar se o elemento possui a borda
if (this.style.border) {
    this.style.border="";
} else {
    this.style.border="dashed black 1px";
}

Exemplo

var elements = document.getElementsByTagName("p");
for (var i = 0; i < elements.length; i++){
    elements[i].onclick=function(){
      if (this.style.border) {
        this.style.border="";
      } else {
        this.style.border="dashed black 1px";
      }
    }

}

Elemento

Elemento

Elemento

Elemento

Elemento

Elemento

Elemento

Elemento

Elemento

Elemento

Elemento

Comment: Obrigada!!! Perfeito!
Comment: Mais recomendo usar a resposta do @Oeslei.
Answer: Utilize css para exibir a borda. Assim, basta utilizar o método toggle para adicionar / remover a classe responsável por adicionar a borda.
No JS:
this.classList.toggle('com-borda');

No CSS:
.com-borda {
    border: dashed black 1px;
}


View Controller opening (via Show segue) in a weird way

Question: As you can see in the video here, the view controller opens in a weird way (stretched with grey/black colour at the top) and then gets back normal.
 https://drive.google.com/file/d/1K9NQkEs7xxaHfEx83XTgcWd7j200tBXp/view?usp=sharing
I want to avoid this. I want View Controller normally as it would. I am unable to understand how to proceed. What may be the possible reasons behind it and how do I do correct it?
Comment: How are you pushing the second viewcontroller? Can you please show us some code?
Answer: Seems as though it is laying out for a large title in the navigation bar.
Try adding this to the viewDidLoad method of the controller.
navigationItem.largeTitleDisplayMode = .never

Answer: Just disable large title in viewDidLoad. And do it only since iOS 11:
    func viewDidLoad() {
       super.viewDidLoad()
              if #available(iOS 11, *) {
                self.navigationItem.largeTitleDisplayMode = .never
              }
     ...

Comment: Instead of duplicating @Callam's answer. You can improve his answer.


Expected loss from using a worker

Question: Let's suppose I have a worker that checks if a specific component in a car is flawed or not...
The worker's accuracy is 90%. If the component is flawed, he will tell with 90% probability that the component is flawed. Likewise when the component is not flawed.
The probability that the component is flawed is 5%.
Imagine we have 500 cars. For each car that we sell with a flawed component, will lose 500,000\$. For each car that we don't sell, when the component is not flawed, we lose 50,000\$.
What's the expected loss for the company, when we hire this worker?
here's my reasoning:
WF - worker states it's flawed, F - component is flawed.
$P(\bar{WF}|\bar F)=P(WF|F)=0.9$ and $E(L)=500*(P(\bar{WF}|F)*P(F)*500,000+P(WF|\bar F)*P(\bar F)*50,000)$
Am I correct?
Comment: Yes, I believe it's correct. You are just calculating the average loss per car and then multiplying by the number of cars. Since the worker's process is i.i.d., your expectation will be as it is.
Comment: The $90\%$ is not consistent with the two $95\%$s
Comment: @Henry My bad! :'D
Answer: Given a car, the expected loss is
$$E[L]=0\cdot P(\textrm{Sell}\cap \textrm{No flaw})+0\cdot P(\textrm{No Sell}\cap \textrm{Flaw})+\\
+500000\cdot P(\textrm{Sell}\cap\textrm{ Flaw} )+50000\cdot P(\textrm{No sell}\cap \textrm{ No flaw})$$
We find
$$P(\textrm{Sell}\cap\textrm{ Flaw})=P(\textrm{Worker says no flaw}\cap\textrm{Flaw})=\\=P(\textrm{Worker says no flaw}|\textrm{Flaw})P(\textrm{Flaw})=0.1\cdot 0.05$$
$$P(\textrm{No sell}\cap\textrm{No flaw})=P(\textrm{Worker says flaw}\cap\textrm{No
flaw})=\\=P(\textrm{Worker says flaw}|\textrm{No flaw})P(\textrm{No flaw})=0.1\cdot 0.95$$
For $500$ cars, the result is $500E[L]$.
Comment: @Henry that's true, but I assumed that the worker is always there and he alone decides if it's to be sold or not
Comment: @Snoop by the way, in one of my first edits, I changed the values of the probabilities.
Comment: You do not seem to have considered the cost from not having the worker and so selling cars with flawed components
Comment: @Henry I think that possibility is outside the scope of the question... I asked for if we hire the worker, then... But maybe I'm missing something.
Comment: @Anoldmaninthesea.   I would understand "the expected loss for the company if we hire this worker" as meaning the expected difference in cost between hiring the worker and not hiring the worker
Comment: @Henry I've changed the english to reflect my design. I hope now it's clearer. But if my english still needs to be corrected tell me. ;)

Expected loss from using a worker