Scala/Spark: Caused by: java.lang.ClassNotFoundException: org.apache.spark.Logging

Question: I am pretty new to scala and spark. Trying to fix my set-up of spark/scala development. I am confused by the versions and missing jars. I searched on stackoverflow, but still stuck in this issue. Maybe something missing or mis-configured.
Running commands:
me@Mycomputer:~/spark-2.1.0$ bin/spark-submit --class ETLApp /home/me/src/etl/target/scala-2.10/etl-assembly-0.1.0.jar

Output:
...
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/Logging
...
Caused by: java.lang.ClassNotFoundException: org.apache.spark.Logging

build.sbt:
name := "etl"

version := "0.1.0"
scalaVersion := "2.10.5"
javacOptions ++= Seq("-source", "1.8", "-target", "1.8")
mainClass := Some("ETLApp")

libraryDependencies += "org.apache.spark" %% "spark-core" % "1.5.2" % "provided";
libraryDependencies += "org.apache.spark" %% "spark-sql" % "1.5.2" % "provided";
libraryDependencies += "org.apache.spark" %% "spark-streaming" % "1.5.2" % "provided";
libraryDependencies += "org.apache.spark" %% "spark-streaming-kafka" % "1.5.2";
libraryDependencies += "com.datastax.spark"  %% "spark-cassandra-connector" % "1.5.0-M2";
libraryDependencies += "org.apache.curator" % "curator-recipes" % "2.6.0"
libraryDependencies += "org.apache.curator" % "curator-test" % "2.6.0"
libraryDependencies += "args4j" % "args4j" % "2.32"

java -version
java version "1.8.0_101"
scala -version
2.10.5
spark version
2.1.0
Any hints welcomed. Thanks
Answer: in that case, your jar must bring all dependend classes along when being submitted to spark.
in maven this would be possible with the assembly plugin and the jar-with-dependencies descriptor. with sbt a quick google found this: https://github.com/sbt/sbt-assembly
Answer: you can change your build.sbt as follows:
name := "etl"

version := "0.1.0"

scalaVersion := "2.10.5"

scalacOptions ++= Seq("-deprecation",
  "-feature",
  "-Xfuture",
  "-encoding",
  "UTF-8",
  "-unchecked",
  "-language:postfixOps")

libraryDependencies ++= Seq(
"org.apache.spark" %% "spark-core" % "1.5.2" % Provided,
"org.apache.spark" %% "spark-sql" % "1.5.2" % Provided,
"org.apache.spark" %% "spark-streaming" % "1.5.2" %  Provided,
"org.apache.spark" %% "spark-streaming-kafka" % "1.5.2" % Provided,
"com.datastax.spark"  %% "spark-cassandra-connector" % "1.5.0-M2",
"org.apache.curator" % "curator-recipes" % "2.6.0",
"org.apache.curator" % "curator-test" % "2.6.0",
"args4j" % "args4j" % "2.32")

mainClass in assembly := Some("your.package.name.ETLApp")

assemblyJarName in assembly := s"${name.value}-${version.value}.jar"

assemblyMergeStrategy in assembly := {
  case m if m.toLowerCase.endsWith("manifest.mf") => MergeStrategy.discard
  case m if m.toLowerCase.matches("meta-inf.*\\.sf$") => MergeStrategy.discard
  case "reference.conf" => MergeStrategy.concat
  case x: String if x.contains("UnusedStubClass.class") => MergeStrategy.first
  case _ => MergeStrategy.first
}

add the sbt-assembly plugin to your plugins.sbt file under the project directory in your Project's Root directory. Running sbt assembly in the Terminal(Linux) or CMD(Windows) in the root directory of your project would download all the dependencies for you and create an U


Aprimorar código para evitar lentidão na colisão de partículas

Question: Preciso repartir o espaço em células. Cada célula deve ter aproximadamente o tamanho do raio das partículas para que uma partícula não ocupe mais que 4 células como na ilustração abaixo:

A partícula de raio r pode ocupar até 4 células. A sua própria
célula (onde o centro (x,y) da partícula está localizado) e mais outras 3 células vizinhas.
Motivo da otimização: Com o atual código, quando aumento a quantidade de partículas, o programa fica muito lento e as colisões entre elas não ocorrem de forma fluída.
canvas.cpp
#include "canvas.h"
#include <QPainter>

canvas::canvas(QWidget *parent) :
QWidget(parent)
{
m_particulas.resize(50);

int n = m_particulas.size();
for(int i = 0 ; i<n ; ++i)
{
    m_particulas[i].init();
}

startTimer(5);
}

void canvas::paintEvent(QPaintEvent * event)
{
(void) event;

QPainter painter(this);
painter.setWindow(0,0,1000,1000);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 2, Qt::SolidLine, Qt::RoundCap));
painter.setBrush(QBrush(Qt::darkCyan, Qt::SolidPattern));

int n = m_particulas.size();
for(int i = 0 ; i<n ; ++i)
{
    double x = m_particulas[i].x();
    double y = m_particulas[i].y();
    double L = m_particulas[i].r();
    painter.drawEllipse(QPointF(x, y), L, L);
}
}

void canvas::timerEvent(QTimerEvent *event)
{
(void) event;

int n = m_particulas.size();

for(int i = 0 ; i<n ; ++i)
    m_particulas[i].andar();

for(int i = 0 ; i<n ; ++i)
{
    Particula &pi = m_particulas[i];

    for(int j = 0 ; j < n ; ++j)
    {
        if (i == j) continue; //(MODO 2)

        Particula &pj = m_particulas[j];

        if (pi.testa_colisao(pj))
        {
            pi.calcula_colisao(pj);
        }
    }
}
update();
}

particula.cpp
#include "particula.h"
#include <stdlib.h>     /* srand, rand */
#include <cmath>

Particula::Particula()
{

}

void Particula::init()
{
m_r = 20;

m_x = 900.0*rand()/RAND_MAX + 50;
m_y = 900.0*rand()/RAND_MAX + 50;

m_vx = 2.0 * rand()/RAND_MAX - 1;
m_vy = 2.0 * rand()/RAND_MAX - 1;

double norma = sqrt(m_vx*m_vx + m_vy*m_vy);
m_vx /= norma;
m_vy /= norma;
}

void Particula::normaliza(double &vx, double &vy)
{
double norma = sqrt(vx*vx + vy*vy);

if (norma > 0)
{
    vx /= norma;
    vy /= norma;
}
}

bool Particula::testa_colisao (Particula &p)
{
    double dx = x() - p.x();
    double dy = y() - p.y();

    double dist2 = dx*dx + dy*dy;
    double somaRaios = r() + p.r();
    return dist2 <= somaRaios * somaRaios;
}

void Particula::calcula_colisao(Particula &p)
{
    double vx = p.x() - x();
    double vy = p.y() - y();
    normaliza(vx,vy);

    p.m_vx += vx;
    p.m_vy += vy;
    normaliza(p.m_vx,p.m_vy);

    m_vx -= vx;
    m_vy -= vy;
    normaliza(m_vx, m_vy);

   do
    {
        andar();
        p.andar();
    } while (testa_colisao(p));
}

double Particula::x() const
{
return m_x;
}

double Particula::y() const
{
return m_y;
}

double Particula::r() const
{
return m_r;
}

void Particula::andar()
{
m_x += m_vx;
m_y += m_vy;

if(m_x > 1000-m_r) m_vx *= -1; //inferior - multiplicado por -1 para inverter a     direção...
if(m_y > 1000-m_r) m_vy *= -1; //direita
if(m_x < 0+m_r) m_vx *= -1; //esquerda
if(m_y < 0+m_r) m_vy *= -1; //superior
}

Comment: Qual a ordem de magnitude que estamos falando? Quantas partículas pretende usar e qual o tamanho da sua malha?
Comment: Eu não sei se entendi direito a pergunta: você não sabe como verificar a colisão usando esse conceito de partições, é isso? Ah, talvez você não precise repartir o espaço nesse nível. Já tentou repartí-lo em 4 ou 16 grandes áreas quadradas?
Comment: Não consegui ver como funciona seu código, está um pouco complexo. Eu faria usando conceitos de geometria analítica com equação da circunferência, já que você tem o raio, o X do centro e o Y do centro. Então veria retas que são secantes à circunferência, ignorando as retas tangentes e as que estão fora. Talvez usando distância entre ponto e reta e comparando o raio com algumas coisas da equação da circunferência, fique um código bastante pequeno, legível e rápido.
Comment: Quero fazer de uma forma que não seja mais necessário calcular a distância de uma partícula para todas as outras partículas, mas somente para aquelas que estiverem próximas. Evitando assim a lentidão nas colisões. Segue imagem como exemplo: http://goo.gl/D1B5lV
Comment: Algo em torno de 5 mil partículas com o raio entre 5 e 10.
Answer: Para efeito de ilustração, considere uma divisão simples em que o seu Canvas é partido em 4x4 quadrados menores, como na figura abaixo:

O seu código testa a colisão dentro do método canvas::timerEvent, hoje verificando-a entre cada possível combinação das n particulas: p1 x p2. Por isso, ele tem uma complexidade quadrática (O(n2)) e pode demorar bastante pra um grande número de partículas.
Usando uma divisão simples como a ilustrada, você pode fazer um único laço para localizar as partículas nos diferentes quadrados e assim "filtrar" apenas aquelas no mesmo quadrado (ou nos quadrados vizinhos, como preferir) de uma partícula de interesse.
A filtragem é simples de ser realizada usando um único laço para percorrer todas as partículas existentes (a complexidade é O(n), e você pode utilizar o mesmo laço que já usa para fazer as partículas andarem):

Divida as dimensões do canvas em comprimento (width) e altura (height) respectivamente pelo número de quadrados em uma linha e em uma coluna (no exemplo ilustrado, esse número é 4 para ambox). Isso resultará nas dimensões em pixels de um quadrado (isto é, comprimento e altura de um quadrado em pixels).
No laço de localização, para a partícula atual pi, divida os valores de suas coordenadas em x e em y respectivamente pelas dimensões comprimento e largura de um quadrado anteriormente calculadas, e tome apenas a parte inteira (trunque o resultado). Isso resultará no índice (iniciado em 0) do quadrado em que a partícula está localizada nos eixos X e Y, que pode ser usado para armazenar e referenciar as partículas em um array bidimensional que represente os quadrados.

Exemplo:
Considere que o canvas tenha 100 x 100 pixels e que uma partícula A se
  encontra na coordenada (x:80, y:25) e outra partícula B se encontra na
  coordenada (x:48, y:77).
Como são 4 quadrados em uma linha do canvas, o comprimento de um
  quadrado é 100/4=25 pixels. Como são 4 quadrados em uma coluna do
  canvas, a altura de um quadrado é 100/4=25 pixels. Assim, um quadrado
  tem 25 x 25 pixels; no eixo X do canvas, o primeiro quadrado vai do
  pixel 0 ao pixel 24, o segundo do pixel 25 ao pixel 49, o terceiro do
  pixel 50 ao pixel 74, e o último do pixel 75 ao pixel 99; como a
  altura é igual ao comprimento, os mesmos valores valem para as
  coordenadas dos quadrados no eixo Y do canvas.
Para a partícula A. Ao dividir seu valor de x = 80 pelo comprimento do
  quadrado resulta em: 80/25 = 3,2. Extraindo-se a parte inteira, tem-se
  o valor 3. Esse valor indica o índice do quadrado em que a particula
  se encontra no eixo X, isto é, no quarto e último quadrado da esquerda
  para a direita (lembrando que a indexação é a partir de 0). Ao dividir
  seu valor de y = 25 pela altura do quadrado resulta em: 25/25 = 1.
  Extraindo-se a parte inteira, tem-se o valor 1 o que indica que a
  partícula está no segundo quadrado de cima para baixo. Ou seja, usando
  um array bidimensional para representar os quadrados, a particula está
  no quadrado CANVAS[3,1].
Para a partícula B. Ao dividir seu valor de x = 48 pelo comprimento do
  quadrado tem-se: 48/25 = 1,92 com parte inteira igual a 1. Ao dividir
  seu valor de y = 77 pela altura do quadrado tem-se: 77/25 = 3,08 com
  parte inteira igual a 3. Ou seja, a particula está no quadrado
  CANVAS[1,3].

Uma vez que você tenha as particulas localizadas em seus diferentes quadrados, a verificação da colizão pode ser feita apenas para as partículas dentro de cada quadrado. Você pode fazer isso simplesmente percorrendo os quadrados em um array bidimensional e fazendo o teste de colisão apenas para as partículas armazenadas dentro do quadrado atual (ou seja, dentro de CANVAS[i,j]).
Ainda que dentro de cada quadrado a comparação tenha complexidade quadrática, no resultado geral o custo computacional vai ser bem menor, proporcional à sua divisão (imagine que você tenha 100 partículas; para comparar todas entre si são necessárias 100x100 = 10000 comparações; com apenas 4 células, se tiverem 25 partículas em cada célula serão necessárias apenas quatro comparações locais de 25x25 partículas, ou seja, 4x25x25 = 2500 comparações - apenas 25% do que seria necessário sem a divisão).
Você pode não precisar de uma divisão muito grande. Talvez usar 16 células (divisão 4x4 como a ilustrada) seja suficiente para o seu propósito. Além disso, talvez você não precise recalcular a localização das partículas no mesmo intervalo de tempo em que as move, mas isso vai depender da velocidade delas e do ajuste fino que você possa vir a precisar. Aconselho a começar com as soluções mais simples (menos quadrados, relocalização a cada movimento), e aplicar as mudanças conforme sentir necessidade.
P.S.: Note também que na minha sugestão eu não estou considerando as dimensões das partículas (que foi uma preocupação sua na pergunta, mas que eu julguei desnecessária). Se tiver algum motivo pra isso (talvez vindo da referência de onde você mesmo tirou uma imagem postada nos comentários), acho importante você especificar na pergunta. :)
Comment: A dimensão das partículas é apenas para um visão nítida das colisões entre elas. Isso se trata de um trabalho para a faculdade, onde o professor pede para ir aumentando a quantidade de partículas e diminuindo o seu raio proporcionalmente, para critério de avaliação da eficiência do código.

Foi de grande ajuda, tentarei fazer o que sugeriu. Obrigado.


How to access tag's attribute value with BeautifulSoup

Question: I'm using BeautifulSoup and requests for web scraping. I know how to extract attribute between tags, but if what I want is the number '4.31' below in a tag, any idea how to get it?

<svg
 ...
</svg>

I've tried:
soup.find('div',{'class':'starRating'})
soup.find('title')

which returns nothing, so the number is basically the tag...
Answer: You can read the attribute title value like this:
from bs4 import BeautifulSoup

response = """

<svg>
</svg>

"""

soup = BeautifulSoup(response, 'lxml')
print(soup.find('div', {'class': 'starRating'})['title'])

Outputs:
4.31

See https://www.crummy.com/software/BeautifulSoup/bs4/doc/#attributes
`

A tag may have any number of attributes. The tag  has an attribute "id" whose value is "boldest". You can access a tag's attributes by treating the tag like a dictionary
Answer: You can use a lambda to query elements with the matching title attribute, then use the ["title"] key to extract the data you want:
>>> soup.find(lambda x: x.name == "div" and "title" in x.attrs)["title"]
'4.31'

Or use a CSS selector:
>>> soup.select_one("div[title]")

Even easier, use the target attribute as a kwarg:
>>> soup.find("div", title=True)

Attempting to pull the title attribute out of an element that doesn't have it will raise a KeyError, so it's worth filtering ahead of time. Use find_all or select if you'd like an iterable of multiple results.


Prove that multiplication with positive semidefinite matrix is a kernel

Question: Let $A \in \mathbb{R}^{d \times d}$ be a symmetric and positive semi-definite matrix.
Prove that
$$
k(\textbf{x}, \textbf{x}') = \textbf{x}^T A \textbf{x}'
$$
is a kernel.
My first thought when I saw this question was that $A$ can be interpreted to be the kernel matrix of $k$ and that's it. But $A$ and the kernel matrix of $k$ can be completely different, making this idea not work.
This can be proven either by showing that the kernel matrix of $k$ is positive semidefinite or by utilizing a kernel feature map $\phi$. I assume that the first approach makes more sense here, but I don't see how this can be proven just yet.
How can it be proven that $k$ is a kernel?
Answer: Since $A$ is symmetric and PSD, it can be written as $$A=Q\Lambda Q^T=(Q\Lambda^{1/2})(Q\Lambda^{1/2})^T=M^TM$$ where $M=(Q\Lambda^{1/2})^T$. So, the kernel can be expressed as
$$k(x,x')=x^TM^TMx'=(Mx)^T(Mx')=<Mx,Mx'>$$
So, the corresponding transformation is $\phi(x)=Mx$, and $k$ is a kernel.
Comment: That was very nice but you have a typo ( $Xx ^{\prime}$ ) when you write it as a dot product.
Comment: @mlofton thank you, I fixed it.


$f(x+\frac{y}{2})-f(x-\frac{y}{2})=2x^2y+5y^2$. Find $\frac{d f(3)}{dx}= f'(3)=?$

Question:
$f(x+\frac{y}{2})-f(x-\frac{y}{2})=2x^2y+5y^2$
$\frac{d f(3)}{dx}= f'(3)=?$

As there is no information on whether $y$ is a function or a constant, I believe it must be treated as a constant. Then, we need to know the value of $y$ in order to set $x+\frac{y}{2}=3$ and find the derivative.
How can we solve this problem?
Comment: The way to interpret $x$ and $y$ here is that you can put any values for $x$ and $y$ (independently) and then $f$ will satisfy the given equation. To solve your problem, plug in $x=3$, divide the equation by $y$ and then consider the limit as $y$ goes to zero.
Comment: @quarague So, I got. $\lim_{y\to 0} f(3+\frac{y}{2})-f(3-\frac{y}{2})=\lim_{y\to 0} 18 + 5y$. But then $f(3)-f(3)=18$. Can you please tell me what am I missing?
Comment: Check your algebra, you need to divide both sides of the equation by $y$.
Comment: Oh, yes, I got it. Thanks
Answer: $$
\frac{df(x)}{dx} = \lim_{y \to 0} \left(\frac{f(x + \frac{y}{2}) - f(x - \frac{y}{2})}{y}\right) = \lim_{y \to 0} \frac{2x^2y + 5y^2}{y} = \lim_{y \to 0} 2x^2 + 5y = 2x^2
$$
Therefore $f'(3) = 18$


Calculating 1/r*d/dr(r*f) numerically in python when r=0. f is a function of r

Question: Usually when you do this by hand there's no problem as the 1/r usually gets cancelled with another r. But doing this numerically with scipy.misc.derivative works like a charm for r different from zero. But of course, as soon as I ask for r = 0, I get division by zero, which I expected. So how else could you calculate this numerically. I insist on the fact that everything has to be done numerically as my function are now so complicated that I won't be able to find a derivative manually. Thank you!
My code:
rAtheta = lambda _r: _r*Atheta(_r,theta,z,t)
if r != 0:
    return derivative(rAtheta,r,dx=1e-10,order=3)/r
else:
    #What should go here so that it doesn't blow up when calculating the gradient?

Comment: well this just seem like a limit issue, so just compute it a a very small value like `r=1e-10` and you can check that it's small enough by confirming the value at `r=1e-12` is basically the same... Assuming your function is C1 of course.
Comment: It depends on what you plan to do with the returned value. How about returning a `float("nan")`?
Comment: @Julien yes I thought about that, but I wasn't sure how accurate that would be and specially when r would be small enough. What's specially cumbersome is that I'm a lot more interested on the values at r=0 or very close to it, than at any other values.
Comment: As I said just experiment with smaller and smaller values until you can see the values converge...
Comment: @DYZ I want to plot the different values that the function provides me and I'm specially interested with the values at r=0 since it's one of the few places where I can compare the result to the analytical solution. It's the simulation of a magnetic field created by a finite solenoid with an alternating current going through it.
Comment: are you aware of automatic differentiation ?
Answer: tl;dr: use symbolic differentiation, or complex step differentiation if that fails
If you insist on using numerical methods, you really have to approximate the limit of the derivative as r->0 one way or the other.
I suggest trying complex step differentiation. The idea is to use complex arguments inside the function you're trying to differentiate, but it usually gets rid of the numerical instability that is imposed by standard finite difference schemes. The result is a procedure that needs complex arithmetic (hooray numpy, and python in general!) but in turn can be much more stable at small dx values.
Here's another point: complex step differentiation uses
F′(x0) = Im(F(x0+ih))/h + O(h^2)

Let's apply this to your r=0 case:
F′(0) = Im(F(ih))/h + O(h^2)

There are no singularities even for r=0! Choose a small h, possibly the same dx you're passing to your function, and use that:
def rAtheta(_r):
    # note that named lambdas are usually frowned upon
    return _r*Atheta(_r,theta,z,t)

tol = 1e-10
dr = 1e-12

if np.abs(r) > tol: # or math.abs or your favourite other abs
    return derivative(rAtheta,r,dx=dr,order=3)/r
else:
    return rAtheta(r + 1j*dr).imag/dr/r

Here is the above in action for f = r*ln(r):

The result is straightforwardly smooth, even though the points below r=1e-10 were computed with complex step differentiation.
Very important note: notice the separation between tol and dr in the code. The former is used to determine when to switch between methods, and the latter is used as a step in complex step differentiation. Look what happens when tol=dr=1e-10:

the result is a smoothly wrong function below r=1e-10! That's why you always have to be careful with numerical differentiation. And I wouldn't advise going too much below that in dr, as machine precision will bite you sooner or later.
But why stop here? I'm fairly certain that your functions could be written in a vectorized way, i.e. they could accept an array of radial points. Using complex step differentiation you don't have to loop over the radial points (which you would have to resort to using scipy.misc.derivative). Example:
import numpy as np
import matplotlib.pyplot as plt

def Atheta(r,*args):
    return r*np.log(r) # <-- vectorized expression

def rAtheta(r):
    return r*Atheta(r) #,theta,z,t) # <-- vectorized as much as Atheta is

def vectorized_difffun(rlist):
    r = np.asarray(rlist)
    dr = 1e-12

    return (rAtheta(r + 1j*dr)).imag/dr/r

rarr = np.logspace(-12,-2,20)
darr = vectorized_difffun(rarr)
plt.figure()
plt.loglog(rarr,np.abs(darr),'.-')
plt.xlabel(r'$r$')
plt.ylabel(r'$|\frac{1}{r} \frac{d}{dr}(r^2 \ln r)|$')
plt.tight_layout()

plt.show()

The result should be familiar:

Having cleared the fun weirdness that is complex step differentiation, I should note that you should strongly consider using symbolic math. In cases like this when 1/r factors disappear exactly, it wouldn't hurt if you reached this conclusion exactly. After all double precision is still just double precision.
For this you'll need the sympy module, define your function symbolically once, differentiate it symbolically once, turn your simplified result into a numpy function using sympy.lambdify, and use this numerical function as much as you need (assuming that this whole process runs in finite time and the resulting function is not too slow to use). Example:
import sympy as sym

# only needed for the example:
import numpy as np
import matplotlib.pyplot as plt

r = sym.symbols('r')
f = r*sym.ln(r)
df = sym.diff(r*f,r)
res_sym = sym.simplify(df/r)
res_num = sym.lambdify(r,res_sym,'numpy')

rarr = np.logspace(-12,-2,20)
darr = res_num(rarr)

plt.figure()
plt.loglog(rarr,np.abs(darr),'.-')
plt.xlabel(r'$r$')
plt.ylabel(r'$|\frac{1}{r} \frac{d}{dr}(r^2 \ln r)|$')
plt.tight_layout()
plt.show()

resulting in

As you see, the resulting function was vectorized thanks to lambdify using numpy during the conversion from symbolic to numeric function. Obviously, the best solution is the symbolic one as long as the resulting function is not so complicated to make its practical use impossible. I urge you to first try the symbolic version, and if for some reason it's not applicable, switch to complex step differentiation, with due caution.
Comment: would automatic differentiation be an option ? I guess it would be much slower ?
Comment: @Moritz (ignore my previous comment if you saw it, sorry, I got confused). I'm completely unfamiliar with automatic differentiation, so I can't say anything :)


Do I need to delete arrays explicitly in C++ for memory conservation?

Question: In one function. I create a local arrray.  char arr[20];
And before end of this function.
Will compiler do garbage collection for me? Or I need to do delete by myself?
Answer: There is no Garbage Collection in C++.
However, if you use automatic variables, they will be destroyed when they fall out of scope.
As a rule, there should be 1 delete call for every new.  If you have no new, you don't delete.
Answer: You don't have to delete this array since you create it on stack.
If you created the array using new then you would have to use delete to clean up.
Answer: Local variables are destroyed at the end of the block (not necessarily function) in which they're created. For example:
void myfunc() {
   int x[some_size];

   if (something) {
       std::vector<std::string> y;
       // ...
   } // y will be destroyed here
   // more code
} // x will be destroyed here

If you want your array destroyed earlier than the exit from the function, you may want to make use of the same:
void f() {
    // come code here

    {
        int x[size];

         // code that uses x
    } // `x` gets destroyed here

    // more code
 }

I should add, however, that destroying the variable at that point may not affect memory usage. The memory isn't needed after you exit from the inner block, but it may not be released immediately either.
On the other hand, if you use something like std::vector instead of explicit dynamic allocation, destroying the object will (immediately) free the memory that was being used to store the object's data.
Answer: Any local variables (including arrays) are created on the stack, and therefore are reclaimed when the function returns.
You can think of this similar to garbage collection, but the details are very different. If you do more of any kind of programming, you should learn more about other languages (it is similar in most languages).
So no, you do not need to do anything with your local array.
Answer: Variables only exist within the function they're defined in. Once that function terminates, they're gone. You only need to delete/free your variables if you manually allocated the memory with a new or malloc type command.
Comment: -1:  you do not `delete` something you have `malloc`ed.  Moreover, you should not `malloc` in C++ in the first place.
Comment: Yes, I know. You `free` thing that are `malloc`ed. I hadn't meant to literally delete in that case, just the idea of freeing up the memory that was allocated. And you're correct, you shouldn't use `malloc` in C++, but sometimes people do so I figured I'd touch on it as well.


How to calculate exact probability of an event from normal distribution?

Question: Here is the problem:
Average mark in a class test is 11±3. What is the probability (P) for a student to get 4 marks at 0.05 Significance Level?
I am not interested to calculate P for a student to get 4 marks or lesser (which is a cumulative function). I want to calculate the exact Probability for a student to get 4 marks. Could you please help?
Thanks!
Answer: Obviously, the true number of marks isn't Normally distributed, but it could be modelled as the integer closest to a Normal variable, so you want $$P(3.5\le X\le 4.5|X\sim N(11,\,9)).$$Which is just as well, because a genuinely Normal variable has zero probability of being exactly equal to any value you name.


Regex to parse entire path?

Question: Is there a regex which can parse a string which points to a file or folder (from the root e.g. C:)?
Language is Javascript.
Thanks
Answer: Well, actually there are several, but it depends on what exactly you want to parse. Do you want something like: "C:/.../.../nameOfFile.extension"? Or without the name of the file? Can you be more specific about the problem? Which is the input?
I made this code, really simple, which for a given path, gives you the path without the root.
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestProgram {

    static String path = "C:\\Folder1\\Folder2\\file.extension";

    private static void parsePath() {
      String newPath = "";
      String root = "C:";
      Matcher regexMatcher;
      regexMatcher = Pattern.compile("\\\\").matcher(path);
      newPath = regexMatcher.replaceAll("/");
      regexMatcher = Pattern.compile(root).matcher(newPath);
      newPath = regexMatcher.replaceAll("");
      System.out.println(newPath);
    }

    public static void main(String[] args) {
       parsePath();
    }
}

The output is:

/Folder1/Folder2/file.extension

I don't know if this is what you want, but you just have to play with methods. You will eventually reach the solution.
Answer: Why not skip the regex and just use split?
var path = "C:\\Users\\Joe.Blow\\Documents\\Pictures\\NotPr0n\\testimage1.jpg"
var arrPath = path.split("\\");
var filename = arrPath[arrPath.length - 1];
var drive = arrPath[0];

etc.

Regex to parse entire path?


Can we load delimited text file in spark data frame without creating schema?

Question: i have well formatted text file like bellow .
TimeStamp|^|LineItem_organizationId|^|LineItem_lineItemId|^|StatementTypeCode|^|LineItemName|^|LocalLanguageLabel|^|FinancialConceptLocal|^|FinancialConceptGlobal|^|IsDimensional|^|InstrumentId|^|LineItemSequence|^|PhysicalMeasureId|^|FinancialConceptCodeGlobalSecondary|^|IsRangeAllowed|^|IsSegmentedByOrigin|^|SegmentGroupDescription|^|SegmentChildDescription|^|SegmentChildLocalLanguageLabel|^|LocalLanguageLabel.languageId|^|LineItemName.languageId|^|SegmentChildDescription.languageId|^|SegmentChildLocalLanguageLabel.languageId|^|SegmentGroupDescription.languageId|^|SegmentMultipleFundbDescription|^|SegmentMultipleFundbDescription.languageId|^|IsCredit|^|FinancialConceptLocalId|^|FinancialConceptGlobalId|^|FinancialConceptCodeGlobalSecondaryId|^|FFAction|!|
1506702452474|^|4295876606|^|1|^|BAL|^|Cash And Deposits|^|2|^||^|ACAE|^|false|^||^||^||^||^|false|^||^||^||^||^|505126|^|505074|^||^||^||^||^||^||^||^|3018759|^||^|I|!|
1506702452475|^|4295876606|^|4|^|BAL|^|Raw Materials And Supplies|^||^||^|AIRM|^|false|^||^||^||^||^|false|^||^||^||^||^|505126|^|505074|^||^||^||^||^||^||^||^|3018830|^||^|I|!|
1506702452476|^|4295876606|^|10|^|BAL|^|Total current assets|^||^||^|XTCA|^|false|^||^||^||^||^|false|^||^||^||^||^|505126|^|505074|^||^||^||^||^||^||^||^|3019590|^||^|I|!|
1506702452477|^|4295876606|^|53|^|BAL|^|Deferred Assets Total|^||^||^|ADFN|^|false|^||^||^||^||^|false|^||^||^||^||^|505126|^|505074|^||^||^||^||^||^||^||^|3014598|^||^|I|!|
1506702452478|^|4295876606|^|54|^|BAL|^|Total Assets|^||^||^|XTOT|^|false|^||^||^||^||^|false|^||^||^||^||^|505126|^|505074|^||^||^||^||^||^||^||^|3016350|^||^|I|!|
1506702452479|^|4295876606|^|107|^|BAL|^|Total Number Of Treasury Stock|^||^||^|XTCTI|^|false|^||^||^||^||^|false|^||^||^||^||^|505126|^|505074|^||^||^||^||^||^||^||^|3016331|^||^|I|!|
1506702452480|^|4295876606|^|108|^|BAL|^|Total Number Of Issued Shares|^||^||^|XTCII|^|false|^||^||^||^||^|false|^||^||^||^||^|505126|^|505074|^||^||^||^||^||^||^||^|3016326|^||^|I|!|
1506702452481|^|4295876606|^|109|^|BAL|^|Total Number Of Issued Preferred Stock A|^||^||^|XTPII|^|false|^||^||^||^||^|false|^||^||^||^||^|505126|^|505074|^||^||^||^||^||^||^||^|3016352|^||^|I|!|
1506702452482|^|4295876606|^|111|^|CAS|^|Loss before income taxes|^||^||^|ONET|^|false|^||^||^||^||^|false|^||^||^||^||^|505126|^|505074|^||^||^||^||^||^||^||^|3019196|^||^|I|!|
1506702452483|^|4295876606|^|130|^|CAS|^|Subtotal|^||^||^|FFFF|^|false|^||^||^||^||^|false|^||^||^||^||^|505126|^|505074|^||^||^||^||^||^||^||^|3014929|^||^|I|!|
1506702452484|^|4295876606|^|132|^|CAS|^|Net cash provided by (used in) operating activities|^||^||^|XTLO|^|false|^||^||^||^||^|false|^||^||^||^||^|505126|^|505074|^||^||^||^||^||^||^||^|3016344|^||^|I|!|
1506702452485|^|4295876606|^|133|^|CAS|^|Purchase of property, plant and equipment|^||^||^|ICEX|^|false|^||^||^||^||^|false|^||^||^||^||^|505126|^|505074|^||^||^||^||^||^||^||^|3014949|^||^|I|!|
1506702452486|^|4295876606|^|143|^|CAS|^|Net cash provided by (used in) investing activities|^||^||^|XTLI|^|false|^||^||^||^||^|false|^||^||^||^||^|505126|^|505074|^||^||^||^||^||^||^||^|3016342|^||^|I|!|
1506702452487|^|4295876606|^|145|^|CAS|^|Proceeds from long-term loans payable|^||^||^|FLDI|^|false|^||^||^||^||^|false|^||^||^||^||^|505126|^|505074|^||^||^||^||^||^||^||^|3014931|^||^|I|!|

Now i have to load this text file into spark data frame .
I can do it like this
val schema = StructType(Array(

      StructField("OrgId", StringType),
      StructField("LineItemId", StringType),
      StructField("SegmentId", StringType),
      StructField("SequenceId", StringType),
      StructField("Action", StringType)))

 val textRdd1 = sc.textFile("s3://trfsdisu/SPARK/Text1.txt")
 val rowRdd1 = textRdd1.map(line => Row.fromSeq(line.split("\\|\\^\\|", -1)))
 var df1 = sqlContext.createDataFrame(rowRdd1, schema).drop("index")

But in this way i have create schema,so for example if i have text file that has 100 columns i have to write 100 times this .
so what i need like loading files like csv .
val df1 = spark.read.format("csv").option("header", "true").option("mode", "DROPMALFORMED").load("s3://sdi/SPARK/FinancialLineItem/MAIN")

But this not working for me because i have text file which in not in csv format .
So is there any way to load text file in csv style in spark data frame ?
val dfMainOutput = df1result.join(latestForEachKey, Seq("LineItem_organizationId", "LineItem_lineItemId"), "outer")
      .select($"LineItem_organizationId", $"LineItem_lineItemId",$"DataPartiotion",
        when($"StatementTypeCode_1".isNotNull, $"StatementTypeCode_1").otherwise($"StatementTypeCode").as("StatementTypeCode"),
        when($"StatementTypeCode_1".isNotNull, $"StatementTypeCode_1").otherwise($"StatementTypeCode").alias("StatementtypeCode"),
        when($"LineItemName_1".isNotNull, $"LineItemName_1").otherwise($"LineItemName").as("LineItemName"),
        when($"LocalLanguageLabel_1".isNotNull, $"LocalLanguageLabel_1").otherwise($"LocalLanguageLabel").as("LocalLanguageLabel"),
        when($"FinancialConceptLocal_1".isNotNull, $"FinancialConceptLocal_1").otherwise($"FinancialConceptLocal").as("FinancialConceptLocal"),
        when($"FinancialConceptGlobal_1".isNotNull, $"FinancialConceptGlobal_1").otherwise($"FinancialConceptGlobal").as("FinancialConceptGlobal"),
        when($"IsDimensional_1".isNotNull, $"IsDimensional_1").otherwise($"IsDimensional").as("IsDimensional"),
        when($"InstrumentId_1".isNotNull, $"InstrumentId_1").otherwise($"InstrumentId").as("InstrumentId"),
        when($"LineItemSequence_1".isNotNull, $"LineItemSequence_1").otherwise($"LineItemSequence").as("LineItemSequence"),
        when($"PhysicalMeasureId_1".isNotNull, $"PhysicalMeasureId_1").otherwise($"PhysicalMeasureId").as("PhysicalMeasureId"),
        when($"FinancialConceptCodeGlobalSecondary_1".isNotNull, $"FinancialConceptCodeGlobalSecondary_1").otherwise($"FinancialConceptCodeGlobalSecondary").as("FinancialConceptCodeGlobalSecondary"),
        when($"IsRangeAllowed_1".isNotNull, $"IsRangeAllowed_1").otherwise($"IsRangeAllowed".cast(DataTypes.StringType)).as("IsRangeAllowed"),
        when($"IsSegmentedByOrigin_1".isNotNull, $"IsSegmentedByOrigin_1").otherwise($"IsSegmentedByOrigin".cast(DataTypes.StringType)).as("IsSegmentedByOrigin"),
        when($"SegmentGroupDescription".isNotNull, $"SegmentGroupDescription").otherwise($"SegmentGroupDescription").as("SegmentGroupDescription"),
        when($"SegmentChildDescription_1".isNotNull, $"SegmentChildDescription_1").otherwise($"SegmentChildDescription").as("SegmentChildDescription"),
        when($"SegmentChildLocalLanguageLabel_1".isNotNull, $"SegmentChildLocalLanguageLabel_1").otherwise($"SegmentChildLocalLanguageLabel").as("SegmentChildLocalLanguageLabel"),
        when($"LocalLanguageLabel_languageId_1".isNotNull, $"LocalLanguageLabel_languageId_1").otherwise($"LocalLanguageLabel_languageId").as("LocalLanguageLabel_languageId"),
        when($"LineItemName_languageId_1".isNotNull, $"LineItemName_languageId_1").otherwise($"LineItemName_languageId").as("LineItemName_languageId"),
        when($"SegmentChildDescription_languageId_1".isNotNull, $"SegmentChildDescription_languageId_1").otherwise($"SegmentChildDescription_languageId").as("SegmentChildDescription_languageId"),
        when($"SegmentChildLocalLanguageLabel_languageId_1".isNotNull, $"SegmentChildLocalLanguageLabel_languageId_1").otherwise($"SegmentChildLocalLanguageLabel_languageId").as("SegmentChildLocalLanguageLabel_languageId"),
        when($"SegmentGroupDescription_languageId_1".isNotNull, $"SegmentGroupDescription_languageId_1").otherwise($"SegmentGroupDescription_languageId").as("SegmentGroupDescription_languageId"),
        when($"SegmentMultipleFundbDescription_1".isNotNull, $"SegmentMultipleFundbDescription_1").otherwise($"SegmentMultipleFundbDescription").as("SegmentMultipleFundbDescription"),
        when($"SegmentMultipleFundbDescription_languageId_1".isNotNull, $"SegmentMultipleFundbDescription_languageId_1").otherwise($"SegmentMultipleFundbDescription_languageId").as("SegmentMultipleFundbDescription_languageId"),
        when($"IsCredit_1".isNotNull, $"IsCredit_1").otherwise($"IsCredit".cast(DataTypes.StringType)).as("IsCredit"),
        when($"FinancialConceptLocalId_1".isNotNull, $"FinancialConceptLocalId_1").otherwise($"FinancialConceptLocalId").as("FinancialConceptLocalId"),
        when($"FinancialConceptGlobalId_1".isNotNull, $"FinancialConceptGlobalId_1").otherwise($"FinancialConceptGlobalId").as("FinancialConceptGlobalId"),
        when($"FinancialConceptCodeGlobalSecondaryId_1".isNotNull, $"FinancialConceptCodeGlobalSecondaryId_1").otherwise($"FinancialConceptCodeGlobalSecondaryId").as("FinancialConceptCodeGlobalSecondaryId"),
        when($"FFAction_1".isNotNull, $"FFAction_1").otherwise($"FFAction").as("FFAction"))
        .filter(!$"FFAction".contains("D"))

Answer: While trying to resolve your question, the first problem I faced is that with spark-csv, you can only use a character delimiter and not a string delimiter.
The solution I found is a little bit tricky:
Load the data from CSV using | as a delimiter.
import org.apache.spark.sql.SQLContext
val sqlContext = new SQLContext(sc);

val df = sqlContext.read.format("csv")
            .option("header", "true")
            .option("delimiter", "|")
            .option("inferSchema",true")
            .load("/home/robin/Bureau/Spark/csv_strange_delimiter.csv")

This will create a dataframe looking like this:
df.show
+----------+---+----------+---+---------+---+----------+---+------+
|     OrgId| ^1|LineItemId| ^3|SegmentId| ^5|SequenceId| ^7|Action|
+----------+---+----------+---+---------+---+----------+---+------+
|4295877341|  ^|       136|  ^|        4|  ^|         1|  ^|     I|
|4295877346|  ^|       136|  ^|        4|  ^|         1|  ^|     I|
|4295877341|  ^|       138|  ^|        2|  ^|         1|  ^|     I|
|4295877341|  ^|       141|  ^|        4|  ^|         1|  ^|     I|
|4295877341|  ^|       143|  ^|        2|  ^|         1|  ^|     I|
|4295877341|  ^|       145|  ^|       14|  ^|         1|  ^|     I|
| 123456789|  ^|       145|  ^|       14|  ^|         1|  ^|     I|
+----------+---+----------+---+---------+---+----------+---+------+

2 : Remove the columns containing "^"
val column_to_keep = df.columns.filter(v => (!v.contains("^"))).toSeq
val result = df.select(column_to_keep.head, column_to_keep.tail: _*)

result.show
+----------+----------+---------+----------+------+
|     OrgId|LineItemId|SegmentId|SequenceId|Action|
+----------+----------+---------+----------+------+
|4295877341|       136|        4|         1|  I|!||
|4295877346|       136|        4|         1|  I|!||
|4295877341|       138|        2|         1|  I|!||
|4295877341|       141|        4|         1|  I|!||
|4295877341|       143|        2|         1|  I|!||
|4295877341|       145|       14|         1|  I|!||
| 123456789|       145|       14|         1|  I|!||
+----------+----------+---------+----------+------+

Comment: You can use the concate function as explained here : https://stackoverflow.com/a/31452109/6138873
Comment: Also can you please tell me how can i add |!| in action columns for all records i have updated my code
Comment: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156010/discussion-between-sudarshan-and-jeanr).
Comment: So it tried concat function but schema of the data frame is changed   I tried this val dfMainOutputFinal=dfMainOutput.select(concat($"FFAction", lit("|!|")))


Buzz problem in Ibanez RG350dxz

Question: I was cleaning up my guitar and removed my edge ii tremelo bridge and the springs holding it along with the stud screws holding the blade. But now when I reconnected everything, there is no sound other than a buzz whose intensity changes when I touch the pickups. Please help me figure this out. Any suggestion would be helpful.. It's killing me now.
Answer: When you removed the screws, there should have been a ground strap attached to the claw that the springs connect to, or possibly to the tremelo itself.
If this is still attached then look at where the other end is connected - usually the ground plane or one of the pots. It may have come loose when you removed the trem.
Comment: Thanks. :D  Funny thing is, it was my cable that was damaged.. It was really stupid of me to have not checked properly..
Comment: Well, hope it's all good now. If the answer was useful, you can upvote and use the check mark to accept the answer. I hope you stick around - loads of guitarists here... I learn something new all the time.

Buzz problem in Ibanez RG350dxz


Double read from socket

Question: Go version: go1.14.4 darwin/amd64
TCP server
package main

import (
    "fmt"
    "io"
    "net"
    "time"
)

const (
    maxBufLen int = 4 * 1024
)

type Server struct {
}

func (s *Server) Start() {
    listener, err := net.Listen("tcp", "localhost:9001")
    if err != nil {
        panic(err)
    }

    var connections []net.Conn
    defer func() {
        for _, conn := range connections {
            // ignore
            _ = conn.Close()
        }
    }()

    // loop...
    for {
        connected, err := listener.Accept()
        if err != nil {
            if ne, ok := err.(net.Error); ok && ne.Temporary() {
                // temporary err will bi ignored
                continue
            } else {
                fmt.Println(err)
                return
            }
        }

        go handleConn(connected, true)

        connections = append(connections, connected)
    }
}

func handleConn(conn net.Conn, server bool) {
    buf := make([]byte, maxBufLen)

    for {
        // read
        setTimeout(conn)
        _, err := conn.Read(buf)
        if err != nil {
            if ne, ok := err.(net.Error); ok && (ne.Timeout() || ne.Temporary()) {
                fmt.Println("need continue...")
                continue
            }

            if err == io.EOF {
                fmt.Println("EOF")
                break
            }

            // other...
            panic(err)
        }

        // handle recv msg.
        s := string(buf)
        if server {
            //fmt.Println("server recv req ", s)
        } else {
            fmt.Println("client recv resp ", s)
        }

        if server {
            output := "hi " + s
            ob := []byte(output)
            _, err := conn.Write(ob)
            if err != nil {
                fmt.Println(err)
                break
            }
        }
    }
}

func setTimeout(conn net.Conn) {
    setErr := conn.SetReadDeadline(time.Now().Add(20 * time.Second))
    if setErr != nil {
        panic(setErr)
    }
}

TCP client
package main

import (
    "net"
    "time"
)

type Client struct {
    Exit chan struct{}
}

func (c *Client) Start() {
    conn, err := net.Dial("tcp", "localhost:9001")
    if err != nil {
        panic(err)
    }

    defer conn.Close()

    go handleWrite(conn)
    go handleConn(conn, false)

    <-c.Exit
}

func handleWrite(conn net.Conn) {
    for {
        input := "carryxyh"
        _, err := conn.Write([]byte(input))
        if err != nil {
            panic(err)
        }

        <-time.After(100 * time.Second)
    }
}

Main function
package main

import (
    "fmt"
    "os"
    "os/signal"
    "syscall"
)

type Starter interface {
    Start()
}

func main() {
    var s Server
    var c Client

    go s.Start()
    go c.Start()

    sigs := make(chan os.Signal)
    signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
    <-sigs

    c.Exit <- struct{}{}

    fmt.Println("exit")
}

Running package main will print :
client recv resp  hi carryxyh
client recv resp  carryxyh

Printed twice. But from the program point of view, the server only writes information to the client once, and the content of the information should be hi carryxyh. But the client printed carryxyh in addition to hi carryxyh, which made me very confused.
During the investigation, I accidentally modified a line of code, and the program immediately returned to normal:
modeifycode
As shown above, I modified the server response information: output := "hi "+ s => output := "hi ", at this time the program only prints client recv resp hi.
This makes me completely confused, can anyone help me solve this problem? It would be better if there is a troubleshooting idea.
Comment: Use the length returned from `_, err := conn.Read(buf)`.  You are seeing data from the previous read.
Comment: Hi, thanks for your comment. But my confusion is why `fmt.Println("client recv resp ", s)` is triggered twice.
When I use `output := "hi "`, it will only be triggered once.

Or, can you give me more information to describe it?
Comment: Thanks for your comment. This seems to be very effective.

But I don't understand why I don't pay attention to the returned length, which will cause conn.Read to be triggered twice. It would be great if you could give me an explanation.
Answer: enter image description here
这里你忽略了读取的字节长度,可能返回0字节,你又把buf的内容 又回写给client了.
_, err := conn.Read(buf) 改成 bytesRead, err := conn.Read(buf)   ;
if bytesRead <= 0 { // 没有buf 可以读取 }
--
eg:
if bytesRead == 0 && err == nil {
err = io.EOF
log.Errorf("[network] ReadOnce maybe always return (0, nil) and causes dead loop, Connection = %d, Local Address = %+v, Remote Address = %+v",
c.id, c.rawConnection.LocalAddr(), c.RemoteAddr())
}

Double read from socket


bernoulli distribution finding p from standard deviation

Question: I have a binomial distribution and my n=20 and I am not give either p or q. I know that the standard deviation is 1/2 and I am asked the value of p.
But after solving the equation of 1/2=sqrt(20p*(1-p)) I have 2 different values of p and I dont know what I should do next to solve this.
Comment: If you did the algebra correctly, both values are solutions.  After all, the standard deviation of a Binomial$(n,p)$ distribution and the SD of a Binomial$(n,1-p)$ distribution are always the same.
Comment: While mathematics technically suggests there are two possible answers, if you are in a consulting role, I would bring context into consideration as to what constitutes a rational acceptable solution value for p. Clearly cite and discard what you suspect is an invalid solution in the current context.
Answer: Both solutuions are merely an interchange of 0 and 1. If the variance is $s^2$, then your solutions are
$$p_{1/2} = \frac{1}{2}\pm\sqrt{\frac{1}{4}-\frac{s^2}{n}}$$
and thus $p_2=1-p_1$.
Comment: And, if you knew, per my comment above, that we are discussing a virus with a very high (or very low) infection rate, are there still two equally 'good'  answers?
Comment: From the original question, it is not clear whether this is a real situation at all. The standard estimator for the variance requires an estimator of the mean to be known, so I do not know whether the problem can actually occur. I wonder whether I shoudl delete my answer, because it answers the question entirely, which is ianapprpriate for homework questions AFAIK.
Comment: @AJKOER In what conceivable consulting engagement would your client know the standard deviation of a Bernoulli variable but not know the mean?
Comment: True, instead a consider a situation where one has a very large sample and one is interested in the method of moments (MOM) non-linear estimator (corrected for bias) for the mean. One already has an idea (per the sample) of the correct size of its expected value, and thus, just employing this information in selecting the correct root as input into the derivation of possibly better/alternate non-linear based estimate (and, yes an MOM estimator can produce an improved estimate as I presented in one of my threads). Also, my answer is very appropriate for a teacher seeking a thought based answer.


Probability of balls in boxes

Question: If $12$ balls are thrown at random into $20$ boxes, what is the probability that no box will receive more than $1$ ball?
So my book says the answer is: $\displaystyle \frac{20!}{8!20^{12}}$
However I am having some trouble understanding this result and was hoping for some clarification.
There's a formula in my book: $\displaystyle p=\frac{n!}{(n-k)!n^k}$ so it's apparent that $n=20$ and $k=12$, but I don't understand the reasoning behind it.
Comment: Would it be clearer if you noted ${20!\over 8!}=20\cdot19\cdot18\cdot\, \cdots\,\cdot 9$?
Comment: @DavidMitra Oh! I think I get that part now...so "ball 1" had 20 choices for a box, then "ball 2" has 19 choices (because it can't go in the box ball 1 is in) etc, is this right?
Comment: Yes, that's right.
Answer: Outline:

Count the number of ways to throw $12$ balls into $20$ boxes
Count the number of ways to throw $12$ balls into $20$ boxes with at most one ball per box.
Divide the number of desired ways by the total number of ways.

Ways to throw $12$ balls into $20$ boxes
For each of the $12$ balls, there are $20$ options of where it can be thrown.  (The first ball could be thrown into any of $20$ boxes, the second could be thrown into any of $20$ boxes, etc.)  So, there are:
$$\underbrace{20\cdot20\cdots 20}_{12\text{ times}} = 20^{12}\text{ ways}$$
...that the balls could have been thrown.
Ways with at most one ball per box

For the first ball, we have $20$ options of where to throw it (because all the boxes are empty).
For the second ball, we can throw it in any box except the one taken by the first ball, so there are $19$ options for the second ball.
For the third ball, we can't throw it in the boxes occupied by the first or second balls, so there are only $18$ options.
$\quad\vdots$
For the twelfth ball, we can throw it in any box *except those occupied by balls $1, 2, 3\ldots, 11$.  So, there are $9$ options for the twelfth ball.

All together, then, there are:
\begin{align*}
20\cdot 19 \cdot 18\cdots 9 &= \frac{(20\cdot 19 \cdot 18\cdots 9)\color{red}{(8\cdot 7\cdots2\cdot 1)}}{\color{red}{(8\cdot 7\cdots2\cdot 1)}}\\
&= \frac{20!}{8!}\text{ ways}
\end{align*}
Divide to get probability
The probability (when dealing with equally likely outcomes) is the number of desired outcomes divided by the total number of outcomes.  Here, we desire one ball per box.  So, the probability is:
$$\frac{\frac{20!}{8!}}{20^{12}} = \frac{20!}{8!20^{12}}$$
Answer: Each of the twelve balls can go into any box, so the total number of outcomes is $20^{12}$. The number of ways such that each box gets at most one ball is the same as chosing wich of the twenty boxes get a ball, where the order is important since we distinguish both, balls and boxes. This is $\underbrace{20\cdot 19\cdot \ldots \cdot 9}_{12 \text{ factors}} = \frac{20!}{8!}$. Thus assuming uniform distribution,
$$P = \frac{\frac{20!}{8!}}{20^{12}} = \frac{20!}{8! 20^{12}}$$
Comment: @user181304 Yes, because you _independently_ go through the things, selecting one of the places each time. In this example:
$$20\cdot20\cdot\ldots\cdot20 = 20^{12}$$
The independence of these choices allowes us to multiply the factors without losing anything.
Comment: So in general when I have a "something", like a ball, and I am assigning it a place, the total number of outcomes = #of places^(# of things), is that right?
Answer: I've been toiling over a complete understanding of this problem and all of the other possible outcomes where boxes can have more than one ball.
In general, suppose $k$ balls are thrown into $n$ boxes. Let $b_t$ represent the number of boxes containing $t$ balls so that we have $$\displaystyle\sum_{j=0}^k b_j=n \quad \text{ and } \quad \displaystyle\sum_{j=0}^n j b_j=k.$$ Given $\{b_t\}_{t\geq0}$, let $a_m$ represent the number of balls in box $m$ for a particular realization of this experiment so that $$\displaystyle\sum_{i=0}^n a_i=k.$$ It does not matter which specific realization we choose for the $\{a_m\}_{m\geq1}$. I prefer to have $a_1\geq a_2 \geq ... \geq a_n$. In other words, the order of the boxes doesn't matter.
We want to know the probability of $\{b_0, b_1, ... b_k\}$ (i.e. that $b_0$ boxes have no balls, $b_1$ boxes have 1 ball each, $b_2$ boxes have 2 balls each, etc.). The desired probability is
$$P\left(\{b_0, b_1, ... b_k\}\right)=
\frac{\left(
\begin{array}{c}
n\\
b_0, b_1, ... b_k
\end{array}
\right)
\left(
\begin{array}{c}
k\\
a_1, a_2, ... a_n
\end{array}
\right)}{n^k}.$$
The question asked in the original post is for $k=12$, $n=20$, $b_0=8$, $b_1=12$, and $b_t=0$ for $1<t\leq 12$. This is from DeGroot and Schervish $\S 1.7$ exercise $\# 7$ (4th ed.). Within the framework outlined above the desired probability is
$$\displaystyle P\left(\{8,12,\underbrace{0,...,0}_{10\text{ zeros}}\}\right)=
\frac{\left(
\begin{array}{c}
20\\
8,12,\underbrace{0,...,0}_{10\text{ zeros}}
\end{array}
\right)
\left(
\begin{array}{c}
12\\
\underbrace{1,...,1}_{12\text{ ones}},\underbrace{0,...,0}_{8\text{ zeros}}
\end{array}
\right)}{20^{12}}
= \frac{\frac{20!}{8!12!}\frac{12!}{1!1!\cdots 1!}}{20^{12}}= \frac{20!}{8!20^{12}}
$$
Just as expected from the above answer.
For an illustration, let's experiment by throwing 5 balls into 6 boxes. What is the probability that one box gets 3 balls and 2 boxes get one ball each?
Since 3 boxes get no balls, $b_0=3$, $b_1=2$, $b_2=0$, $b_3=1$, and $b_4=b_5=0$. Let $a_1=3$, $a_2=a_3=1$, and $a_4=a_5=a_6=0$, without loss of generality (we could re-order the boxes any way we please). The desired probability is
$$P\left(\{3,2,0,1,0,0\}\right)=
\frac{\left(
\begin{array}{c}
6\\
3,2,0,1,0,0
\end{array}
\right)
\left(
\begin{array}{c}
5\\
3,1,1,0,0,0
\end{array}
\right)}{6^5}.$$
Note that the zeros in the multinomial coefficient are redundant, so this simplifies to
$$P\left(\{3,2,0,1,0,0\}\right)=
\frac{\left(
\begin{array}{c}
6\\
3,2,1
\end{array}
\right)
\left(
\begin{array}{c}
5\\
3,1,1
\end{array}
\right)}{6^5}
=
\frac{\frac{6\cdot5\cdot4}{2}\cdot5\cdot4}{6^5}=\frac{200}{6^4}\approx0.1543.$$
Also, note that the number of balls is allowed to be larger than the number of boxes.
I haven't tried to prove this formula yet, but it has worked for all examples I've tried (running numerical simulations).
Answer: Model the situation as mappings from the set $\{1,\dots,12\}$ into the set $\{1, \dots, 20\}$.
The total number of possibilities (mappings) is $20^{12}$.
The requirement amounts  to having an injection. So the number of favourable cases is
$$\frac{20!}{(20-12)!}$$
Thus the probability is $$p=\frac{20!}{8!\,20^{12}}\approx0.0147. $$
Comment: There are more injections. Precisely $\binom{20}{12} \cdot 12!$ thus the probability is as outlined in the other answers.
Comment: Oops! Mixed the binomiial coefficients with the number of arrangements. I'll fix that in a moment. Thanks for pointing it.
Comment: How do we get the insight that the question assumes distinguishability of balls and boxes. I mean i get the answer but just this distinguishability part is not clear

Probability of balls in boxes


When writing C# ODATA controller with ODataRoutePrefixAttribute how to bind parameters to properties of the class instead of parameter in the method?

Question: In the documentation of ODATA's WebAPI there is a page about Attribute Routing.
In this page, there is an example about using ODataRoutePrefixAttribute when all requests to a particular controller have the same prefix, and this prefix can include a parameter. In the sample, all action methods declare the same parameter. From their sample:
[ODataRoutePrefix("Customers({id})")]
public class MyController : ODataController
{
    [ODataRoute("Address")]
    public IHttpActionResult GetAddress(int id)
    {
        ......
    }

    [ODataRoute("Address/City")]
    public IHttpActionResult GetCity(int id)
    {
        ......
    }

    [ODataRoute("/Order")]
    public IHttpActionResult GetOrder(int id)
    {
        ......
    }
}

I would like to avoid repeating the parameter in each and every method and just have it be a property of the class, like this:
[ODataRoutePrefix("Customers({id})")]
public class MyController : ODataController
{
    public int Id
    {
        get { ... }
    }

    [ODataRoute("Address")]
    public IHttpActionResult GetAddress()
    {
        ......
    }
}

How to get the value of the id parameter from the URL when it is not passed as parameter to the action method?
Answer: I found out that I could implement the property getter by reading the value from the RequestContext.RouteData.Values:
public string Id => (string)this.RequestContext.RouteData.Values["id"];

One drawback of this solution is that route data values do not seem to be available during the controller's Initialize method, so one needs to be cautious not to depend on such properties in there.


Route packets from one VPN to another

Question: I have two OpenVPN servers (10.8.0.0 and 10.9.0.0) set up on my OpenSUSE server. Within one network, each computer is visible to any other one, but I'd like to make it so that computers are visible across networks. I'd like to route the packets like this: when a user (say 10.8.0.6) pings an address on the other VPN (10.9.0.6), the packets are routed to the 10.9.0.1 and then to the appropriate computer in this VPN. How do I achieve this using iptables or a different tool? I tried the commands at the end of this section with no avail.
Answer: The DD-WRT project has some OpenVPN examples on its wiki that has a sample configuration that should do exactly what you want in the Advanced Configuration: Multiple routed networks section.
The key parts that you'll want to keep in mind are in this excerpt:

Since both will be connecting to the same server, you cannot use the
  same port number for both clients, so we will be giving port 1999 for
  the first client and 2000 for the second client. Also, we need to tell
  Client1 how to reach Client2's subnet and vice-versa. This means
  including a second routing entry in our configuration.

What you'll most likely need to add to your existing configuration is the following:
OpenVPN config for Clients on 10.8.0.0
port 2000
# Create routes
route add -net 10.8.0.0 netmask 255.255.255.0 gw 10.8.0.1
route add -net 10.9.0.0 netmask 255.255.255.0 gw 10.8.0.1

iptables config for Clients on 10.8.0.0
# Open firewall holes
iptables -I INPUT 2 -p udp --dport 2000 -j ACCEPT
iptables -I FORWARD -i br0 -o tun0 -j ACCEPT
iptables -I FORWARD -i tun0 -o br0 -j ACCEPT

OpenVPN config for Clients on 10.9.0.0
port 1999
# Create routes
route add -net 10.9.0.0 netmask 255.255.255.0 gw 10.9.0.1
route add -net 10.8.0.0 netmask 255.255.255.0 gw 10.9.0.1

iptables config for Clients on 10.9.0.0
# Open firewall holes
iptables -I INPUT 2 -p udp --dport 1999 -j ACCEPT
iptables -I FORWARD -i br0 -o tun0 -j ACCEPT
iptables -I FORWARD -i tun0 -o br0 -j ACCEPT

OpenVPN config for Server
# Create routes
route add -net 10.8.0.0 netmask 255.255.255.0 gw 10.8.0.6
route add -net 10.9.0.0 netmask 255.255.255.0 gw 10.9.0.6

iptables config for Server
# Open firewall holes for Client1
iptables -I INPUT 2 -p udp --dport 2000 -j ACCEPT
iptables -I FORWARD -i br0 -o tun0 -j ACCEPT
iptables -I FORWARD -i tun0 -o br0 -j ACCEPT

# Open firewall holes for Client2
iptables -I INPUT 2 -p udp --dport 1999 -j ACCEPT
iptables -I FORWARD -i br0 -o tun1 -j ACCEPT
iptables -I FORWARD -i tun1 -o br0 -j ACCEPT

# Allow Forwarding packets between Client1 and Client2
iptables -I FORWARD -i tun0 -o tun1 -j ACCEPT
iptables -I FORWARD -i tun1 -o tun0 -j ACCEPT

Note that these instructions are for setting up Ethernet Routing which is easier to set up and probably what you want for your case. You should, however, review the differences between bridging and routing, and the overview on how to set up Ethernet Bridging if you think you need the features that bridging gives you.

Route packets from one VPN to another


How does InnoDB compute table_rows for information_schema?

Question: Background
So, I understand that for InnoDB tables, table_rows from information_schema is "only a rough estimate", and that counting the exact number of rows is nontrivial because of transactions.
But I have some tables for which table_rows is within a few % of the true count, and some tables like this:
mysql> SELECT table_rows FROM information_schema.tables WHERE table_name="__unit_previews";
+------------+
| table_rows |
+------------+
|  226992266 |
+------------+
1 row in set (0.03 sec)

mysql> SELECT COUNT(*) FROM __unit_previews;
+----------+
| COUNT(*) |
+----------+
|   144156 |
+----------+
1 row in set (0.14 sec)

I think you'd have to be pretty generous to call this a "rough estimate".
There are no open transactions; I didn't secretly delete a few hundred million rows; I ran analyze table to make sure the information schema was up-to-date.
I'm running MySQL 5.6.13 (@@innodb_version also says 5.6.13) and this table has row_format=dynamic with about 400kB of blob attributes per row. information_schema also reports that data_length is 58020446208 and avg_row_length is 255.
Questions
So how does InnoDB calculate table_rows for information_schema.tables?
Possibly related: how does it determine data_length and avg_row_length? Does this include "external" blob storage (i.e. off the B-tree page but still managed by InnoDB)?
If table_rows is being used in SQL optimization, should I be worried that it's off by 3 orders of magnitude?
Is there a better way to estimate the number of rows from the attributes available in information_schema?
Comment: Disclaimer: I have no clue how to answer your questions, but I know one thing - you can't rely on information_schema for data size or number of rows. Ever. I use a simple trigger that increments/decrements a counter for a specific table whose row count I have to know. Apart from that approach, I haven't found anything better. As for size of the data, the amount allocated and spent are never the same. Thus, I simply ask my OS to tell me the size on disk. It doesn't answer your questions sadly, but until someone more knowledgeable jumps in.. :)
Answer: The DELETE you did has an impact.
InnoDB knows exactly what the value of Data_length.  From probes (8 in old versions), it has some feel (granted, a very poor feel) for the distribution of stuff.  I think it gets an estimate of Avg_row_length, then divides to get table_rows.
Run ANALYZE again; the first number will stay the same; the other two will change.
TEXT and BLOB fields (etc) are stored into an off-block storage in different ways, depending on ROW_FORMAT.  This adds to the confusion and calculations.
Newer versions (since 5.6.6?) do a slightly better job.
Being off by a factor of 1000 is really bad.  I rarely see more than a factor of 2 (either direction).
I just tried one of my tables similar to yours, and got a factor of 7.  Hehe - ANALYZE made the row count farther from the truth.  OPTIMIZE made it better, but still off by 5x.  Oh, well.
Suggest you post a bug on http://bugs.mysql.com.
Comment: Sorry about the misunderstanding--I was trying to say that I did not do a `DELETE` (in fact this table is almost never deleted or updated). I suspect you're right that it divides `data_length` by `avg_row_length`, and that the problem arises from how off-block storage is counted--it seems to be included in `data_length` but not included in the estimate of `avg_row_length`. I'll see about posting a bug report.


Angular Material => set value of mat-select from formControlName

Question: My angular version is 7. I am creating a form where have one drop down select to choose a option. My list is working for selecting but when i load page it is not showing pre-defined value. The page is in form to edit user data. Below is my html code,
<ng-container matColumnDef="name">
    <mat-header-cell *matHeaderCellDef>Server Name</mat-header-cell>
    <mat-cell *matCellDef="let row; let index = index" [formGroupName]="index">
        <mat-form-field>

            <mat-select matSelect [formControl]="name">
                <mat-option *ngFor="let item of arr" value="item">{{item}}</mat-option>
            </mat-select>
            <mat-error *ngIf="name.touched && !name.required">
                Server Name is required
            </mat-error>
        </mat-form-field>
    </mat-cell>
</ng-container>

When i take the <input> in place of <mat-select>, it works and display the value whichever comes as initial. So i think formControlName="name" is working fine. But it does not work with <mat-select>.
Please suggest.
Comment: What do you set on start when you init Control? This value has to be element of `arr` array.
Comment: You should also share the component code. How you set the initial value?
Answer: change value="item" to [value]="item"
Answer: You used [formControl]="name" instead of formControlName="name". Value setting on start for this control has to be an element of arr array.


Is there any way to profile the performance of views in ASP.NET MVC?

Question: I am profiling my MVC app and the speed from the Performance Explorer is around 500 ms (50 ms code, 450ms SQL roundtrips that won't exist in deployment).
However, the time taken waiting on a response to a GET request is around 1200ms.
I've managed to discover through trial and error that this is due to a particularly slow partial view.
However, is there any way for me to discover this through profiling in the future?
If it helps I am using precompiled Spark views but a solution that works for this or the default view engine would be appreciated.
Thanks
Answer: Use a profiler. What stops you from just attaching a profiler for ASP.NET and look where the time is spent?
Comment: Any examples of a profiler to use? The point I was making was that I do have the Visual Studio profiler attached to the ASP.NET code and it ignores view execution time.
Comment: Well, I use the Visual Studio Team System 2010 profiler ;) I can attach it to code (running in debug mode preferably) and / or just start a profiling run directly from the IDE.
Comment: It must be because my views are precompiled. I can't get it to attach to view rendering, only controller execution. Thanks though.


Code for reading a .dat file gives indexing error

Question: I have a source code that is supposed to read a .dat file. the code is in Matlab. There is an error I am getting every time I try to read a .dat file:
This is the .dat file: https://drive.google.com/file/d/1iQM4P__FQqLCvHHdvkLwbo-jhHNNRXAl/view

Error using zeros
Size inputs must be integers.
Error in readBasebandFile (line 49)
hdrMat = zeros(numFrames, NumHdrs);

function [ hdrMat, FrameMat ] = readBasebandFile( file )
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here

NumHdrs = 6;

fid = fopen(file, 'rb');
if fid < 3
disp(['couldnt read file ' file]);
return
end

f = dir(file);
fsize = f.bytes;

% read first frame
ctr = 0;
hdrMat = [];
FrameMat = [];

while (1)
if feof(fid)
    break
end

% read header
%frame counter
frameCtr = fread(fid, 1,'uint32');
numBins = fread(fid, 1,'uint32');
binLength = fread(fid, 1,'single');
% sampling frequency which defines the range resolution through
% binLength = C/Fs/2, where C is the speed of light in the medium.
Fs = fread(fid, 1,'single');
% carrier frequency
Fc = fread(fid, 1,'single');
RangeOffset = fread(fid, 1, 'single');

% check valid header read
if isempty(frameCtr) || isempty(numBins) || isempty(binLength) || isempty(Fs) ...
        || isempty(Fc) || isempty(RangeOffset)
    break;
end

% read data
data = fread(fid, 2*numBins, 'single');
if ctr==0
    % 2 because it's complex values and 4 because 'single' is 4 bytes.
   numFrames = fsize / (4*(NumHdrs + 2*numBins));
   hdrMat = zeros(numFrames, NumHdrs);
   FrameMat = zeros(2*numBins, numFrames);
end
ctr = ctr + 1;

hdrMat(ctr,:) = [double(frameCtr) double(numBins) binLength Fs Fc
RangeOffset];
FrameMat(:,ctr) = data;

end

[n,m] = size(hdrMat);
disp([file ' read. NumFrames=' num2str(n)]);
fclose(fid);

Comment: The error message is quite informative here. The `zeros()` function requires integer inputs. In your case, `NumHdrs` is set explicitly to 6, so it must be `numFrames` that is not an integer.
Comment: what should I change ?
Answer: Change
numFrames = fsize / (4*(NumHdrs + 2*numBins));

with
numFrames = round(fsize / (4*(NumHdrs + 2*numBins)));

Alternatively use ceil or floor instead of round depending on what you need.
As the error message indicates, when you initialize an array, your size inputs must be integer. For example, you can have a 2x2 matrix but there is no such thing as 2.1x2.3 matrix. Mathematically illogical.
Comment: After I changed the code, I recieved this error.
Subscripted assignment dimension mismatch.

Error in readBasebandFile (line 55)
    FrameMat(:,ctr) = data;
Comment: Since I don't know what kind of data you are processing I cannot comment on the details. But in principle you have to make sure the row size of `FrameMat` is equal to the size of the `data` vector. Also make sure `data` is a column vector since you are assigning this variable to all rows of `FrameMat` for a given `ctr` column. Read more here: https://www.mathworks.com/matlabcentral/answers/93586-why-do-i-get-the-subscripted-assignment-dimension-mismatch-error-message
Comment: I do not know how I can upload the data file to stack overflow for people to see. do you have a hint of how can I see how many rows in the .dat file
Comment: Try `length(data)` or `size(data)`
Comment: I still could not open the file please find it attached.https://drive.google.com/file/d/1SchIf40XM9BIxsa5sfK9k13bh_6G_a8j/view
Comment: what do you mean by (data)?


Add PHP code to block's content

Question: Is there a module that allows a php text filter in drupal 8? This was a possibility in drupal-7 but I can not find something similar to 8.
Answer: The PHP filter module was removed from core, because of the inherent security problems it presents.
It now exists as a separate contrib module, but beware that even the module description itself warns against using it:

Warning
Enabling this module can cause security and performance issues as it allows users to execute PHP code on your site. There are better alternatives out there that do not expose such vulnerabilities on your site.
Comment: I guess the better alternative would be to create a custom module that creates a custom block.
Comment: `even the module description itself warns against using it` yet you're recommending it? :O
Comment: @NoSssweat Nope, not recommending, just informing :)

Add PHP code to block's content


Somar dinamicamente todos os campos do FormBuilder. Front-end Angular

Question: eu tenho 5 campos do formBuilder. os 5 são preenchidos com dados que vem do banco de dados. mas esses dados podem ser alterados pelo usuario
formNum: FormGroup;
recebeDados(){
   this.serviceEndpoint.pegarValores().subscribe(res=> {
       this.formulario(res.data);
  });
}

formulario(result: Dados){
    this.formNum = this.fb.group({
      campo1: result.dado1,
      campo2: result.dado2,
      campo2: result.dado3,
      campo2 :result.dado4,
      campo2: result.dado5
    });
}

preciso formar dinamicamente esse form builder. por exemplo, esses inputs tem seus valores iniciais:
input1 => 0.3
input1 => 0.7
input1 => 5
total=> 6

mas o usuário foi e alterou um campo:
input1 => 1
input1 => 0.7
input1 => 5

e o total precisa alterar junto: total=> 6.7
alguma ideia?
Answer: Beleza é até bem simples, vou tentar passar uma explicação mais ou menos para chegar no resultado final...
Você pode começar fazendo um map reduce nos valores do FormBuilder, vamos melhorar essa explicação:
A classe FormBuilder tem uma propriedade que fornece acesso a classe FormGroup, classe que possui um array com os FormControls e seus respectivos values.
Parece confuso mas o FormBuilder é organizado assim, dentro dele possuímos o FormGroup  esse é responsável por agrupar os FormControls. A classe FormControls é a representação de um campo do formulário, pois nela conseguimos pegar informações sobre o campo.
Então complementando o que eu disse anteriormente, adicione um eventListerner nos campos desse form por exemplo o listerner onChange.
Toda vez que o onChange for disparado, faça o map reduce para obter o total de todos os campos do form.
Após obter o total, use o FormGroup para encontrar o FormControl do campo total.
Pela classe FormControl é possível setar o valor do campo, assim é só setar o valor total.
Claro se o campo total também faz parte do FormGroup na hora do map reduce, você vai precisar ignorar ele dá conta.

Se tem duvidas sobre o que é Map Reduce da uma olhada aqui no próprio stackoverflow tem muita coisa legal, você vai aprender que Map Reduce tem haver com programação funcional.

Doc FormBuilder
Doc FormGroup
Doc FormControl


mock method with 11 parameters with gmock

Question: I'm using gmock to mock my dependencies in legacy code. One of the class have a method with 11 parameters. When I tried to use MOCK_METHOD11_WITH_CALLTYPE to mock it, I found this macro doesn't exist. gmock only supoort up to 10 parameters. What do you suggest for this? Do I implement this method with dummy body? Or copy & extend the macro? Thanks!
PS, I don't need to mock this method in my tests right now, but probably need to do so in the future.
Best regards,
Answer: Methods with more than 10 parameters may be a sign of trouble.  I can suggest a workaround which will help your specific case but which may also be a good idea apart from mocking.  Take several of the parameters that make sense as a group, and aggregate them in a struct.  Then pass an instance of that struct as an argument to the method.  So instead of 11 arguments you might then have 3 or 4.  Not only does this help with the mock library problem you're having, it may improve the usability of your class, since methods with so many arguments are usually difficult to read at the call site.
Comment: Legacy code is not a problem: just add a new method taking a struct, keep the old way too, and have one of them call the other.  No big deal to make it backward compatible and still accommodate your tests.
Comment: I agree that is kind of code smell. I appreciate your suggestion. But it is a legacy code base and as the method is part of API, so I'll try something else. Now, I'm overriding a trivial version.
Answer: In case someone needs more gmock arguments here is a header-only extension: gmock-more-args
Answer: Method you are trying to mock is pure virtual.
Here is what I did without changing my existing code:
<code>struct ParamsMoreThanTen
{
  Param_Type param_1;
  Param_Type param_N;
};

MOCK_METHOD1(methodWithMoreThanTenParms, methodReturnType(ParamsMoreThanTen params));

methodReturnType methodWithMoreThanTenParms(
    Param_Type param_1,
    Param_Type param_N) override
{

  return    methodWithMoreThanTenParms(ParamsMoreThanTen
  {
    Param_Type param_1,
    Param_Type param_N
  });

};
</code>


Debian reboot suggested after kernel update since Bullseye

Question: So I've been using Stretch as a production server for a small academic research office.
Had a nice ~500 or so days of uptime, and I kept it up to date as updates rolled.
This summer, Stretch was phased out from LTS. So I updated to 10, then 11 (Bullseye). Since then, I feel like I have to reboot after a kernel update maybe, once a month ? (Rebooted twice since June when I upgraded the OS).
I am a bit annoyed at being 'forced' to reboot that often for changes to be applied. I need to find time windows and/or advise with userbase to prevent service outage. And well, it is new to me that a Linux distro needed to be rebooted that often to be keep up to date. (Also, but not that important, I think about my uptime record that need to be bested!)
There is this post here saying it is recommended to reboot after kernel upgrades Debian: Kernel update using APT - reboot required to take effect? which I understand, but it was not like that before, isn't?
Is there a new paradigm for Debian where reboots were not mandatory before Bullseye but now are?
Answer: I don't get what is your concern. Yes, to update kernel you need to reboot. It was always like this, 25 years ago, 20 years ago, 10 years ago, 5 years ago, now. There were attempts to implement live kernel patching to avoid this reboot in other Linux distros, but this is not widespread and Debian never supported any of it.
Debian doesn't require you to reboot. It will continue to run on old kernel, until you actually reboot. Again, it was always like this. Maybe they now saying it more clear and more loud, I don't know. I don't feel something changed.
If you want to be able to have a service available without interruption, you have to implement a cluster (multiple servers) with rolling update. Then you'll be able to reboot/upgrade/whatever your systems one by one. Single server solution could not possibly be highly available anyway.
And remember, big uptime is like big penis: probably looks nice (for geeks), but doesn't mean you're superior. Absolutely useless metric. Better concentrate on something more valuable, like customer satisfaction.
Comment: Maybe kernel development accelerated, thus more bugs are introduced and consequently more of them are fixed, so the package gets updated more often. Which is actually very good. Never hesitate to reboot. This is the only way to find out if your system is able to boot actually. [Netflix has a service](https://www.gremlin.com/chaos-monkey/) which reboots random servers just to be sure the overall system is robust to self-heal.
Comment: Great thoughts, just that I am impressed at how often it is happening compared to previous OS versions, was especially wanting to check if I was over-viewing something


Node tagged with more than one taxonomy, how to keep expanded the specific taxonomy menu the user has used to reach a node

Question: I have nodes associated to more than one taxonomy vocabulary, so the same node can be listed/reached in different ways, for example:
Brand > Family line > model > NODES
Veicles > category > NODES
e.g.
Ferrari > Top-Luxury > Testarossa > NODES > NODE-A
Cars > Supercars > NODES > NODE-A
By Design every node can have only and only one URL: it's ok for me (and for SEO)
Now I have created different taxonomy menus (based on the above taxonomies) to retrieve nodes in different ways:
TAXONOMY MENU A: Ferrari > Top-Luxury > Testarossa > NODE-A
TAXONOMY MENU B: Cars > Supercars > NODE-A
The question is: is there a way to keep expanded the specific taxonomy menu the user has used to reach the node?
So:
if user has reached NODE-A trough MENU A then MENU A is showed and expanded in the page
if user has reached NODE-A trough MENU B then MENU B is showed and expanded in the page
Is there a way to accomplish this?
May be using SESSION variables? (ref: http://drupal.org/node/1443210 )
Thank you very much for any help
Answer: I've done this in my modules hook_init() implementation by using sessions, menu_get_object and menu_tree_set_path(). Here's some example code showing my general approach:
<code>function mymodule_init() {
  // Save the active path for users when on taxonomy term pages.
  if (arg(0) == 'taxonomy' && arg(1) == 'term' && arg(2)) {
    $_SESSION['mymodule_path'] = $_GET['q'];
  }
  // The user is on a node page.
  if (arg(0) == 'node' && arg(1)) {
    $node = menu_get_object();
    // Assuming all the nodes in the taxonomy menu are of the same specific type.
    if ($node->type == 'mynodetype') {
      if (isset($_SESSION['mymodule_path'])) {
        menu_tree_set_path('menu-mymenu', $_SESSION['mymodule_path']);
      }
    }
  }
</code>


How to change images without removing and re-uploading via GUI back end?

Question: I have some images I need to change in batches on the website.
For example, I want to replace all color header images with grayscale images.
What I did was subsitute the old images with the new ones in folder:
<code>C:\patch\sites\default\files\styles\slideshow_1920_x_700_scale\public
</code>
Now the problem is that after doing this, even if I restart the computer(using aquia dev desktop), clear all caches ect...
The color images still show up on the website, even if the grayscale ones are in the folder.
The only way to get the grayscale images to show is to go in the content item, remove the image, and reload the identical grayscale one.
Is there a way to batch do this WITHOUT having to manually remove and re-upload?
What if I had to change 1000 images...?
Answer: Derivative images (the files that result from using Image Styles) are auto-generated; you need to replace the original image file, then flush the images already created for the style(s) you're interested in, so the derivatives be re-generated next time they're requested.
For the example path you've given, the originals will be in the root of the public file system (/sites/default/files), so just replace those with the new images, and flush the styles that use them.
Probably the easiest way to do the last part is to install the Image style flush module.


Let $E \subset \mathbb{R}^n$ be a convex set. Prove there exists an open set $A \subset \mathbb{R}^n$ equivalent to $E$

Question: Let $E \subset \mathbb{R}^n$ be a convex set. I need to prove there exists an open set $A \subset \mathbb{R}^n$ equivalent to $E$, that means such $\mathcal{L}^{n}  (A \triangle E)= 0$, where $E \triangle A = (E \setminus A ) \cup ( A \setminus E) $.
My idea: I know that for all $\epsilon >0$ there exist an open set $A \supset E$ such that $\mathcal{L}^{n}  (A \setminus E) < \epsilon$, but it doesn't help because, in this way, the measure of the symmetric difference $A \triangle E$ is less than $\epsilon$ but it could be grater than $0$.
My second idea: If I set $A := Int(E)$ then $A$ is open and it holds $\mathcal{L}^{n}  (A \triangle E)=  \mathcal{L}^{n}  (E \setminus A) \leq \mathcal{L}^{n} (\partial E)$ (in the case $E$ is closed we have the equality). But in general we do NOT have $\mathcal{L}^{n}( \partial E) = 0$.
In both attempts I haven't used the fact that $E$ is convext, but I don't know how to use it. Does this fact imply that $\mathcal{L}^{n}( \partial E) = 0$? If it is true, do you know a simple proof of it?
Thank you
Answer: Claim: if $E$ is convex, then $\mathcal{L}^n(\partial E) = 0$.
Proof: Suppose otherwise. Then there is $x \in \partial E$ such that $\limsup_{r \downarrow 0} \frac{|\partial E \cap B_r(x)|}{|B_r(x)|} = 1$. That is, $\partial E$ contains most points of small balls around $x$. But by convexity, this means $E$ contains a small ball around $x$. This contradicts $x \in \partial E$.


role of onehotencoding in Feature selection

Question: I need to clarify about feature selection.
I am working on Kaggle breast cancer dataset (https://www.kaggle.com/datasets/reihanenamdari/breast-cancer). It is a categorical dataset. There are 15 features including one class level. So, to encode the categorical columns I need some encoding. I used labelencoder and onehotencoder. Now, if I go with label encoder I got the feature selection score for 15 features. However, if I go with onehotencoder I got feature selection for 39 features. I know why this was happened. Because onehotencoding transfer the dataset 0 1 format for all responses of a category.
Now the categorical dataset is a nominal categorical dataset. So, it is better to use onehotencoding rather than labelencoder.
code block for labelencoder and feature selection
fs = SelectKBest(score_func=chi2, k='all')
fs.fit(X_train, y_train)
X_train_fs = fs.transform(X_train)
X_test_fs = fs.transform(X_test)

for i in range(len(fs.scores_)):
    print('Feature %d: %f' % (i, fs.scores_[i]))

Output for feature selection when using labelencoder
Feature 0: 18.397683
Feature 1: 0.907448
Feature 2: 1.948516
Feature 3: 61.664194
Feature 4: 270.739282
Feature 5: 290.261719
Feature 6: 4.682429
Feature 7: 12.392248
Feature 8: 0.668526
Feature 9: 921.736979
Feature 10: 7.633741
Feature 11: 19.629019
Feature 12: 20.830701
Feature 13: 1481.246548
Feature 14: 5428.386792

Code block for onehotencoding with feature selection
s = SelectKBest(score_func=chi2, k='all')
fs.fit(X_train, y_train)
X_train_fs = fs.transform(X_train)
X_test_fs = fs.transform(X_test)
for i in range(len(fs.scores_)):
    print('Feature %d: %f' % (i, fs.scores_[i]))

Output for onehotencoding and featureselection
Feature 0: 14.659096
Feature 1: 850.014288
Feature 2: 21.873525
Feature 3: 1485.277925
Feature 4: 5993.193209
Feature 5: 0.512006
Feature 6: 20.922138
Feature 7: 4.021212
Feature 8: 5.601705
Feature 9: 1.133983
Feature 10: 1.224111
Feature 11: 9.484409
Feature 12: 10.513141
Feature 13: 27.287016
Feature 14: 2.181016
Feature 15: 12.428941
Feature 16: 45.034507
Feature 17: 51.032404
Feature 18: 9.335082
Feature 19: 167.640306
Feature 20: 46.638949
Feature 21: 1.276569
Feature 22: 167.640306
Feature 23: 8.832149
Feature 24: 10.731865
Feature 25: 51.766011
Feature 26: 11.010064
Feature 27: 16.180921
Feature 28: 11.367710
Feature 29: 51.766011
Feature 30: 11.010064
Feature 31: 16.180921
Feature 32: 11.367710
Feature 33: 0.928991
Feature 34: 35.539577
Feature 35: 9.218108
Feature 36: 128.157306
Feature 37: 20.555553
Feature 38: 99.097837

My question is how could I use onehotencoding for feature selection?
Answer: OneHotEncoding is not used for feature selection. It is used for encoding the categorical variables. If you want to perform feature selection  there are a lot of techniques out there.
Here is a list of some of them.
Here is an article which describes which technique to use when.
PS: OneHotEncoding is good for learning purpose but it is not the widely used technique when it comes to real world applications. There are other much better encoding techniques out there!
Here is a pip library which contains almost all encoding techniques. Simple install using pip and start using!
Cheers!
Comment: @Encipher Sure. I've edited my answer to include a pip library for categorical encoders
Comment: Can you please share the link for better encoding techniques as well? Thank you.
Comment: strongly disagree with "it is not the widely used technique when it comes to real world applications"
Comment: @BenReiniger Being a Data Scientitst myself I've yet to come across an algorithm where the person has used OHE. Also the drawbacks of OHE outweigh any advantages it has so I am of the above opinion. But it is not to say that it is not used at all.


Are there any benefits of directing CSS to an ID rather than using just the ID?

Question: Take this example HTML markup:

I'd normally just reference the ID using just the ID and nothing more:
#mySpan { ... }

Other than making the code easier to understand, are there any benefits in directing the CSS to the element instead?
.myDivider p #mySpan { ... }

...or with even more specific direction:
div.myDivider > p > span#mySpan { ... }

Comment: Sure, if you want that element to have certain styles only when appearing in particular contexts.
Comment: In my opinion, there is no sense in "directing css" to a ID which is unique.
Comment: @Mr_Green, there is if you use the same css for multiple pages which may contain the same id but in different places
Comment: One of the considerations would be if you are going to re-use that CSS. If you specifie a CSS rule their attributes will overwrite the attributes of the less specific ones. If you import a CSS full of ID's you can reuse them into your HTML structure, but if you extend a CSS full of specific rules, your HTML will have to use the same structure, it can be a handicap when creating a new theme.
Answer: Quickly reading this article: http://csswizardry.com/2011/09/writing-efficient-css-selectors/
/*
Ignore this:
My best guess is that the same with ID's being found slightly faster than classes it's the same with the specifications. Making it clear to your css where to look will make your loading times slightly faster.
*/
After reading it more thoroughly I'm discovering some errors in my guesses. The browser reads the css from the right to the left. It first checks if the right hand element exists in the html and then works it's way up to make sure it is in the right position. So the specifications are mostly just a way to make sure you select the correct element. They do not improve performance, on the contrary.
I suggest you read the article, it's really interesting
Answer: You could try to optimize for efficiency, but it's not usually worthwhile.

Is all this really necessary?The short answer is; probably not. (CSS
  Wizardry, regarding CSS efficiency optimization)

On the other hand, if you have some real-world example wherein a particular element can only appear once-per page, but can appear in different locations within the document, you may wish to style it differently. You can accomplish that by specifying rules even more specific than just the ID.
I can't think of a "real" reason I'd ever need to do this, but here's a silly example -- an easter egg hunt, wherein some egg image appears once per page and needs to scale with particular parent content:
header > #egg {
  width: 64px;
  height: 64px;
}

.navigation > #egg {
  width: 32px
  height: 32px;
}

footer > #egg {
  width: 16px;
  height: 16px;
}

#egg .cleverly-hidden {
  width: 16px;
  height: 16px;
  opacity: 0.5;
  filter: alpha(opacity=50);
}

etc.
It's notable, however, that even though CSS optimization usually yields unnoticeable results, you should still keep things as simple as possible. For your own sanity, and for the fringe chance that your site hits a level of complexity wherein "endless" selector chains in the context of a dynamic document do become noticeable.


Jquery not loading properly

Question: I have this code: https://jsfiddle.net/g412rft4/1/ which as you can see works perfectly.
However I am trying to replicate it on my desktop with a test.html file and a Common.js file as well and it doesn't works. It seems like Jquery is never loaded and I don't get why.
This is the code for the .js file:
$( "#SaeReplacerBuilder" ).click(function() {
  alert("ok");
});

And this is the code for the .html file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<title>Whatever</title>

'''CLICK Here'''

Comment: You can load the jquery from here: `https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js`. This is the latest version, from the Google API. Also, if you are not doing this on a website, that is, you are using a file on your computer, if you are not online the jquery will not load.
Comment: wrap the code in `$(function(){ /* code here*/})`. This is pretty basic for all jQuery. Your code is running before elememts exist
Comment: Yup it works now. Thank you, both.
Answer: Protocol-relative links (those that begin with //) only work when the page is being served by a web server. I'm assuming you're just viewing the file from your hard-drive.
Add a protocol and it would work:
https://code.jquery.com/jquery-2.1.4.js

If what you've shown is the exact contents of your Common.js file, you need to either:
a) Wrap the code in a ready event:
$(function(){
    $( "#SaeReplacerBuilder" ).click(function() {
        alert("ok");
    });
})

b) Move your Common.js script to the bottom of the page:
<!DOCTYPE html>

        <title>Whatever</title>

'''CLICK Here'''

Otherwise, you're attempting to bind a click event before the DOM is ready. I normally do both (use the ready event and move the scripts to the bottom), just to be safe.
Comment: Made that change. Still doesn't works. And yes i'm just viewing from my hard-drive.
Comment: Is the code in Common.js exactly what's shown after "This is the code for the .js file"? If so, you need to also wrap your code in the ready event handler that @charlietfl mentions.
Comment: Uhm... can you explain why it works in the jsfiddle without that change then? And yes, both files are **exactly** as shown.
Comment: Order. jsFiddle is arranging your references in the correct order for you when it builds the results iframe.
Answer: When your Common.js Javascript file is run the document looks like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<title>Whatever</title>

You have to wait until the document has fully loaded before adding click handlers. jQuery gives you a way to easily do that:
$(function () { // This function will be run when the document is done loading
    $( "#SaeReplacerBuilder" ).click(function() {
        alert("ok");
    });
});

Further more, it is generally a good idea to put your Javascript near the bottom of your HTML code, like so:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <title>Whatever</title>

'''CLICK Here'''

Putting it in the  stops the rendering of the website until the Javascript file has been loaded. Putting it near the bottom allows the website to render the content (which is usually very fast) before the Javascript is loaded, which makes the website appear to load faster.
Answer: The script tag needs to be relative to your html file, and I'm guessing this isn't loading locally:

Try adding the protocol like this:

Answer: For security reasons, script loaded by script tag doesn't run in a navigator context when loaded from a file://
You have to upload it on a real server and acces it with http://

Jquery not loading properly


vaadin, getting null pointer when trying to add item to sqlContainer

Question: I'm very new to vaadin ( and to java).
i have an table that has an SQLcontainer like so :
public class ProjectTable extends Table {
      public ProjectTable(final DocumentmanagerApplication app) {
          setSizeFull();
          setContainerDataSource(app.getDbHelp().getProjectContainer());
          setImmediate(true);

          commit();
          setSelectable(true);

      }
    }

i have a button and a TextField , to fill data in the table
public void buttonClick(ClickEvent event)
    {
        SQLContainer cont = h.getAssetContainer();
        String dataResult = tf.getValue().toString(); // TEXT FIELD
        System.out.println(dataResult);

        Object itemId = cont.addItem(); // cont is the container
        **cont.getContainerProperty(itemId , "id").setValue(dataResult);      // BUG IS HERE !!! **

try {
            cont.commit();
        } catch (UnsupportedOperationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

i keep getting a " null pointer exception" no matter what i do. on the line **cont.getContainerProperty(itemId , "id").setValue(dataResult);
am i doing anything wrong ? and what is null pointer ?
please inform me if anything was unclear.
please help , thanks in advance.
Answer: BTW, if you have a Vaadin Table and you applied a Filter to it, you need to remove it before, if not probably you will get a null pointer exception in the getContainerProperty(itemId, property) method
Comment: Its right. Filter will not allow the item to add. Although `container.addItem()` will return you an `itemId` but actually if you observe , the size of container won't change. As a solution you can temporally `removeContainerFilter` and then `addItem` and then `addContainerFilter` again.
Answer: This expression returns null:
cont.getContainerProperty(itemId , "id")

And then you try to invoke a method on null. This causes the NullPointerException. So have a look, why the container does not provide a non-null value for the key at the time you invoke it.
Comment: The container doesn't contain the item "itemId" *or* the object "itemId" doesn't have a property `id`. That's the rule to return `null`. `getContainerPropertyIds` should not return null but an empty list (at worst case).
Comment: container.getContainerPropertyIds also has null pointer issue. :( i dont have any clue why is that or what is going on. however. itemId is not null , (checked it ).


Why is there an offset difference with overlay placement or bounds V3 from V2?

Question: I have many (100s) of maps in a databse made for V2. Now migrating to V3 and they're all offeset by quite a long way than they were before.
Have I done something wrong? Or is there a bug or difference that I have to correct? I don't really want to have to re-make all my maps for the new version.
See an example here: http://www.trebrown.com/plant_info_temp.php?species=Bismarckia+nobilis+sp.+silver Scroll down the page to se the map, and then zoom out 1 or 2 stops, you'll then see the Madagascar map out in the middle of the Indian ocean.
Here's my code:
var overlay;
TrebrownOverlay.prototype = new google.maps.OverlayView();
function initialize() {
    var marker;
    var sp_id = "353";
    function createMarker(point,infowindow) {
     var marker = new google.maps.Marker({ position: point, map: map, title:"" });
     google.maps.event.addListener(marker, "click", function() {
      infowindow.open(map,marker);
      });
      return marker;
     }
   var localitiy = new google.maps.LatLng(-19.311144, 46.362305);
   var mapOptions = {
      zoom: 5,
      center: localitiy,
      mapTypeId: google.maps.MapTypeId.TERRAIN
      };
      var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
      var swBound = new google.maps.LatLng(-27.371767300523032, 33.1787109375);
      var neBound = new google.maps.LatLng(-10.83330598364249, 59.5458984375);
      var bounds = new google.maps.LatLngBounds(swBound, neBound);
      var srcImage = 'http://www.trebrown.com/images/maps/bismarckia-nobilis.png';
      overlay = new TrebrownOverlay(bounds, srcImage, map);
}
function TrebrownOverlay(bounds, image, map) {
  this.bounds_ = bounds;
  this.image_ = image;
  this.map_ = map;
  this.div_ = null;
  this.setMap(map);
}
TrebrownOverlay.prototype.onAdd = function() {
  var div = document.createElement('div');
  div.style.borderStyle = 'none';
  div.style.borderWidth = '0px';
  div.style.position = 'absolute';
  var img = document.createElement('img');
  img.src = this.image_;
  img.style.width = '100%';
  img.style.height = '100%';
  img.style.position = 'absolute';
  div.appendChild(img);
  this.div_ = div;
  var panes = this.getPanes();
  panes.overlayLayer.appendChild(div);
}
TrebrownOverlay.prototype.draw = function() {
  var overlayProjection = this.getProjection();
  var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest());
  var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());
  var div = this.div_;
  div.style.left = sw.x + 'px';
  div.style.top = ne.y + 'px';
  div.style.width = (ne.x - sw.x) + 'px';
  div.style.height = (sw.y - ne.y) + 'px';
}
TrebrownOverlay.prototype.onRemove = function() {
  this.div_.parentNode.removeChild(this.div_);
  this.div_ = null;
}
google.maps.event.addDomListener(window, 'load', initialize);

Answer: It's a CSS-issue, there is a interaction with the style of the parent-element of #map-canvas
remove the format text-align: right; from the parent div.morespace
or override it by applying the style text-align:left for #map-canvas