How to properly test simple effects: factor recode or data subsetting

Question: I have seen people test their simple effects in two different ways and would like to know which is the correct one and why. The two methods are briefly explained below:

recoding method: Involves recoding the moderating factor (e.g. Species) such that the level of interest (e.g. versicolor) is set to 0 and testing the complete model (e.g. Petal.Width ~ Sepal.Width * Species).
subsetting method: Involves subsetting the dataset such that it only contains the level of interest (e.g. versicolor) and testing the simple relation between your IV and DV (e.g. Petal.Width ~ Sepal.Width).

Here is an exemple to illustrate the idea:
library(data.table)
X <- data.table(iris)
X <- X[Species %in% c("setosa", "versicolor"), .(Petal.Width, Sepal.Width, Species)]
X[, Species := factor(Species)] # reset factors

contrasts(X$Species) <- cbind(Setosa_vs_Versicolor = c(-.5, +.5)) # contrast coded
mdl <- lm(data = X, Petal.Width ~ Sepal.Width * Species)

# Simple effect for Species = versicolor
contrasts(X$Species) <- cbind(versicolor = c(1, 0))
mdl.simple1 <- lm(data = X, Petal.Width ~ Sepal.Width * Species)
mdl.simple2 <- lm(data = X[Species == "versicolor"], Petal.Width ~ Sepal.Width)

The interaction is significant:
> summary(mdl)

Call:
lm(formula = Petal.Width ~ Sepal.Width * Species, data = X)

Residuals:
     Min       1Q   Median       3Q      Max
-0.29671 -0.07348 -0.03125  0.06552  0.41251

Coefficients:
                                        Estimate Std. Error t value Pr(>|t|)
(Intercept)                              0.09554    0.11674   0.818    0.415
Sepal.Width                              0.24158    0.03798   6.360 6.82e-09 ***
SpeciesSetosa_vs_Versicolor              0.14273    0.23347   0.611    0.542
Sepal.Width:SpeciesSetosa_vs_Versicolor  0.35374    0.07597   4.657 1.03e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1285 on 96 degrees of freedom
Multiple R-squared:  0.9498,    Adjusted R-squared:  0.9483
F-statistic:   606 on 3 and 96 DF,  p-value: < 2.2e-16

Results for the recoding method:
> summary(mdl.simple1) # factor recoding

Call:
lm(formula = Petal.Width ~ Sepal.Width * Species, data = X)

Residuals:
     Min       1Q   Median       3Q      Max
-0.29671 -0.07348 -0.03125  0.06552  0.41251

Coefficients:
                              Estimate Std. Error t value Pr(>|t|)
(Intercept)                    0.16691    0.16311   1.023    0.309
Sepal.Width                    0.41845    0.05852   7.151 1.70e-10 ***
Speciesversicolor             -0.14273    0.23347  -0.611    0.542
Sepal.Width:Speciesversicolor -0.35374    0.07597  -4.657 1.03e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1285 on 96 degrees of freedom
Multiple R-squared:  0.9498,    Adjusted R-squared:  0.9483
F-statistic:   606 on 3 and 96 DF,  p-value: < 2.2e-16

Results for the subsetting method:
> summary(mdl.simple2) # subsetting

Call:
lm(formula = Petal.Width ~ Sepal.Width, data = X[Species == "versicolor"])

Residuals:
     Min       1Q   Median       3Q      Max
-0.29671 -0.08571 -0.03040  0.07776  0.41251

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)  0.16691    0.18958   0.880    0.383
Sepal.Width  0.41845    0.06801   6.152 1.47e-07 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1494 on 48 degrees of freedom
Multiple R-squared:  0.4409,    Adjusted R-squared:  0.4292
F-statistic: 37.85 on 1 and 48 DF,  p-value: 1.467e-07

As you can see, the coefficients estimates are exactly the same, however the SE is smaller for the recoding method, thus there seem to be a gain of statistical power in this case.
Comment: @IsabellaGhement I edited my post
Comment: In your answer, can you describe in simple words - not just R code - what each of the two methods is intended to do?
Comment: Please see my answer and thank you for your edits.
Answer: In my view best method to estimate simple effects and test their significance following the detection of a significant interaction effect is as illustrated by the R code below. [By best method, I mean a method which allows one to track very clearly what is estimated/tested in relation to the model which included the significant interaction.]  There must be an R package out there which performs this type of estimation/testing, but it's worth it to see the step-by-step method.
#=================================================================
# Fit Full Model
#=================================================================

m <- lm(Petal.Width ~ Sepal.Width * Species, data=iris)

summary(m)

Here is the summary of the full model (with the Parameter column inserted by myself):
> summary(m)

Call:
lm(formula = Petal.Width ~ Sepal.Width * Species, data = iris)

Residuals:
    Min       1Q   Median       3Q      Max
-0.45473 -0.08236 -0.02478  0.09005  0.45368

Coefficients:
                          Parameter Estimate Std. Error t value Pr(>|t|)
(Intercept)                   beta0  0.02418    0.22231   0.109 0.913542
Sepal.Width                   beta1  0.06471    0.06447   1.004 0.317176
Speciesversicolor             beta2  0.14273    0.31071   0.459 0.646666
Speciesvirginica              beta3  0.63988    0.31747   2.016 0.045709
Sepal.Width:Speciesversicolor beta4  0.35374    0.10110   3.499 0.000622
Sepal.Width:Speciesvirginica  beta5  0.39324    0.09949   3.953 0.000121
---

Residual standard error: 0.1711 on 144 degrees of freedom
Multiple R-squared:  0.9513,    Adjusted R-squared:  0.9496
F-statistic: 562.9 on 5 and 144 DF,  p-value: < 0.00000000000000022

#=================================================================
# Test Significance of Interaction between Sepal.Width and Species
#=================================================================

require(car)

Anova(m)

#=================================================================
# Specify the Model Equation for the Full Model
#=================================================================

levels(iris$Species)

# Petal.Width = beta0 + beta1*Sepal.Width +
#               beta2*Speciesversicolor  +
#               beta3*Speciesvirginica +
#               beta4*Sepal.Width:Speciesversicolor +
#               beta5*Sepal.Width:Speciesvirginica + error

#===========================================================================
#  Specify the Model Equation for the Sub-Model
#  Corresponding to Species = setosa
#  [Obtained by setting Speciesversicolor = 0 & Speciesvirginica = 0
#  in the Full Model Equation]
#===========================================================================

# Petal.Width = beta0 + beta1*Sepal.Width + error

# --> simple effect of Sepal.Width on Petal.Width for Species = setosa
#     is given by beta1

#==========================================================================
# Specify the Model Equation for the Sub-Model Corresponding to
# Species = versicolor
# [Obtained by setting Speciesversicolor = 1 & Speciesvirginica = 0 in the
# in the Full Model Equation]
#==========================================================================

# Petal.Width = (beta0 + beta2) + (beta1 + beta4)*Sepal.Width + error

# --> simple effect of Sepal.Width on Petal.Width for Species = setosa
# is given by beta1 + beta4

#==========================================================================
# Specify the Model Equation for the Sub-Model Corresponding to
#  Species = virginica
# [Obtained by setting Speciesversicolor = 0 & Speciesvirginica = 1 in the
# Full Model Equation]
#==========================================================================

# Petal.Width = (beta0 + beta3) + (beta1 + beta5)*Sepal.Width + error
# --> simple effect of Sepal.Width on Petal.Width for Species = setosa
# is given by beta1 + beta5

#===========================================================================
# Plot the simple effect of Sepal.Width on Petal.Width for each
# iris Species
#===========================================================================

require(effects)

plot(allEffects(m), as.table=TRUE, layout = c(3,1))

[![enter image description here][1]][1]

#===========================================================================
# Estimate the simple effects beta1, beta1 + beta4 and beta1 + beta5 and
# also test the null hypothesis Ho: simple effect is zero vs.
# the alernative hypothesis Ha: simple effect is different from zero
# for each simple effect of interest
#===========================================================================

#--- Specify the simple effects as linear combinations of the parameters
# beta0, beta1, beta3, beta4 and beta5 of the Full Model:
#
#   beta1 = 0*beta0 + 1*beta1 + 0*beta2 + 0*beta3 + 0*beta4 + 0*beta5
#
#   beta1 + beta4 = 0*beta0 + 1*beta1 + 0*beta2 + 0*beta3 + 1*beta4 +
#                   0*beta5
#   beta1 + beta5 = 0*beta0 + 1*beta1 + 0*beta2 + 0*beta3 + 0*beta4 +
#                   1*beta5

c <- rep(0, length(coef(m)))
names(c) <- names(coef(m))
names(c)
c

#--- define c1 to contain the coefficients (0 or 1) used to define beta1
#--- where c1 = c(0,1,0,0,0,0)

c1 <- c
c1[names(c) %in% "Sepal.Width"] <- 1
c1

#--- define c2 to contain the coefficients (0 or 1) used to define
#    beta1 + beta4 where c2 = c(0,1,0,0,1,0)
#---

c2 <- c
c2[names(c) %in% "Sepal.Width"] <- 1
c2[names(c) %in% "Sepal.Width:Speciesversicolor"] <- 1
c2

#--- define c3 to contain the coefficients (0 or 1) used to define
#    beta1 + beta5 where c3 = c(0,1,0,0,0,1)
#---

c3 <- c
c3[names(c) %in% "Sepal.Width"] <- 1
c3[names(c) %in% "Sepal.Width:Speciesvirginica"] <- 1
c3

#--- Combine c1, c2 and c3 into one matrix

require(multcomp)

contr <- rbind(c1, c2, c3)

contr

rownames(contr) <- c("c1", "c2", "c3")
colnames(contr) <-  rownames(coef(m))

contr

# Estimate the simple effects and test their significance

glht.obj <- glht(m, linfct = contr)

summary(glht.obj, test = adjusted(type = "none"))

confint(glht.obj)

The subsetting method is indeed less powerful, though it provides a quick and dirty way of investigating the simple effects.
Comment: Thanks for your answer! `plot(allEffects(m))` is a really nice way to visualize the simple effects.


Showing that the product of two integrable complex valued random variables is integrable

Question: Let $S_1 = A_1 + iB_1, S_2 = C_2 + iD_2$ be two integrable complex valued random variables on the probability space $(\Omega, F, P)$, where $A_1, B_1, C_2, D_2$ are real valued r.v.s on the same space. Then, in order to show that the product $S_1S_2$ is integrable, I have to show that the products of the real and imaginary parts of $S_1, S_2$ are integrable as well, i.e. $\mathbb{E}(A_1B_1) < +\infty$ etc. I do know that the product of two independent integrable r.v.s is integrable as well, but evidently we do not have that luxury here. Therefore, I was wondering whether the following applicaiton of the Fubini's theorem would help: Define $T_1, T_2 \in \{A_1, B_1, C_2, D_2\}, T_1 \neq T_2$. Then clearly $\left|T_1\cdot T_2\right|$ is measurable w.r.t. the product sigma-algebra $F \otimes F$ (isn't it true that this would hold event if $S_1$ and $S_2$ would live in different probability spaces?). So now the expected value of the absolute value of the component product is: $\mathbb{E}(|T_1\cdot T_2|) = \int_{\Omega^2}|T_1\cdot T_2|d(P\otimes P) = \int_{\Omega_1}\left(\int_{\Omega_2}|T_1(\omega_1)\cdot T_2(\omega_2)|dP(\omega_2)\right)dP(\omega_1)$ from which we obtain that $\int_{\Omega_1}|T_1(\omega_1)|\left(\int_{\Omega_2}|T_2(\omega_2)|dP(\omega_2)\right)dP(\omega_1) = \int_{\Omega_1}|T_1(\omega_1)|\mathbb{E}(T_2)dP(\omega_1) = \mathbb{E}(T_1)\mathbb{E}(T_2) < +\infty$ by assumption, as $S_1, S_2 \in \mathcal{L}^1(P)$.
If the above reasoning was valid, then this would show that the product of $S_1$ and $S_2$ is integrable as a linear combination of integrable functions.
Answer: Note that by definition, $X \in L^1$ if and only if $|X| \in L^1$. Since $|S_1S_2| = |S_1||S_2|$, this means that you can WLOG assume that $S_1, S_2 \geq 0$. Still, this reduction doesn't help since the proposition is false. The product of two $L^1$ variables is in general not $L^1$. For example, pick $p \in \mathbb{R}$ such that $x^p \in L^1([0, 1])$ but $x^p \cdot x^p = x^{2p} \notin L^1([0, 1])$.
Comment: Hmm, I see. I suppose if we make the assumptions more stringent by requiring that $S_1$ and $S_2$ are independent, then this proof is quite straightforward: By assumption the components of $S_1$ and $S_2$ must be mutually independent since otherwise $S_1$ and $S_2$ would not be independent.


404 and HTTPHandler Errors when deploying service stack

Question: I am currently having difficulty deploying a Service Stack service to our UAT production server. In our development and testing environments the service functions correctly and can be accessed as expected.
When the website is deployed to the UAT server however, the service is unavailable and returns a 404 error.
When the application was first deployed, an exception was raised due to missing MVC dlls. These dlls were all placed into the bin folder and the application returned a 404 error.
In the UAT environment the service is added, as an application to an IIS website.
It maybe that the application error page is returning the wrong error message, as if the service is deployed as a standalone site the application runs and returns an HTTPHandler issue for a GET request exactly like this issue. However, the config file is already setup as the answer suggests.
Please can anyone help as this is causing a real problem for me at the moment?
Comment: Environment issues like these are really hard for others to troubleshoot. We would need more information about the IIS settings and your imporatant web.config settings to even get started.
Comment: The configuration of the config files was causing a problem, however there was also another issue regarding the app pool in which the application was running. This issue is now closed and I will mark it as such shortly. Thankyou for looking at this post.
Answer: The issue was caused by a number of factors.
1:) The parent site web config had not been setup correctly with  tags around the appropriat setions ( and  )
<code><location path="xxxxxxx">
    <system.web>
  <customErrors mode="Off"/>
        <compilation targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
        <httpHandlers>
          <add path="*" type="ServiceStack.WebHost.EndPoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
        </httpHandlers>
  </system.web>
  <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
        <validation validateIntegratedModeConfiguration="false" />
        <handlers>
          <add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*"     preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
        </handlers>
   </system.webServer>
</loaction>
</code>
2:) One of the dlls had been built to the wrong architecture for the app pool. Enabling 32 bit applications for the app pool solved this.
In short there were a number of configuration issues that had not been setup before deployment to the testing environment.


multiple output layer in tensorflow

Question: I have code in Tensorflow using CNN model to detect text. The Model contain 9-conv layers flowed with RELU activation and 4-max pooling layers with window and stride equal to 2.
image size is 224*224*3, and the label for each image is (x,y,width,height); where the x and y are the text coordinate(location) in image, width and height are the bounding box size.
Now, how can I use the (x,y,w,h) in Dens regression layers? Should I uses 4-dens layer (one layer for each value in the coordinate and size). If I can do this, it will be correct to use 4-loss function (MSE error) which each loss function for one Dens layer?
also should I use an optimizer for each one?
Or is there another way to use just one Dens layer?
Answer:
Should I uses 4-dens layer (one layer for each value in the coordinate and size). If I can do this, it will be correct to use 4-loss function (MSE error) which each loss function for one Dens layer? also should I use an optimizer for each one?

Yes, you will need to calculate four losses and combine them (tf.reduce_sum or tf.reduce_mean) to build a final loss function and pass it to the optimizer. Only a single optimizer will suffice.
You can do this with one dense layer also. You can use a dense layer with units=4 and a sigmoid activation function to scale the values between [0, 1].
The fractional output will give you the fraction of the image to consider. For e.g. x=0.2 will mean that the x coordinate is ~0.2*W and h=0.5 will mean that the height of the bounding box is ~0.5*H. (H, W are height and width of the image and must be constant for the model)
Hope this helps.
Comment: If you want to combine different training ops, use `tf.group(train_op1, train_op2, ...)` and run that.

multiple output layer in tensorflow


Year on Year growth percentages vs share price returns

Question: Say I have 2 time series A and B.
A is a time series of year-on-year growth numbers at quarterly intervals (I don't have the index levels). For example a data point as at 30-Sep-20 of +17.7% represents the year on year growth from 30-Sep-19.
B is a time series of total returns of a stock at quarterly intervals. A data point as at 30-Sep-20 of +10% represents the total return 3 months to 30-Sep-20.
If I wanted to test whether A is a leading indicator of B using a granger causality test or test if there is a relationship between the 2 time series, what would be the best way to make both series comparable?
Does it make sense to turn the time series of year-on-year growth numbers into an index starting from 100? Can think of it as a seasonally adjusted index.
Or should the share price returns also be converted into year-on-year numbers. Ie do time series analysis by converting B into a time series of year-on-year stock returns.
Please see the example below which to visualize it:

Date
A
B
Indexed A
Indexed B

31/03/2020

100
100

30/06/2020
15
5
115
105

30/09/2020
17.7
10
132
115.5
Comment: *"B is a time series of total returns of a stock at quarterly intervals. A data point as at 30-Sep-20 of +10% represents the total return 3 months to 30-Sep-20.* - did you mean 30-Jun-20 here?
Comment: No the 10% represents the total return from 30-Jun-20 to 30-Sep-20
Answer: First, note that converting to index doesn't change the inherent information of the series so wouldn't make any difference. In fact, you are just making the series non-stationary, which then again will have to be differenced (or log differenced) for analysis.
Second, whether the series are comparable, is not the right question, I think. You can just think of them as two series that can be compared the way you like.
The problem here is that if for the series A, call it $x_t$, you had QoQ rates, it would have contained more information and would have been better (an interesting thing is that suppose the QoQ rate was available as another series C, it would be a leading indicator of A). Unfortunately, the QoQ information is lost.
Third, converting B to YoY (say the new series is now D) is again losing some information. Yes it might appear now that A is a leading indicator of D, but information gain from A now may be less than the information lost in the process of converting B to D.
Finally, it would be better to just use the current data as it is (though you should appropriately seasonally adjust the series B). This is perhaps the most efficient way of using the available information.
Comment: Thanks I understand that converting to index doesn't change inherent information of the series but does it makes sense to convert a time series of YoY changes like that? Normally with an index you would not want the % changes to overlap.
Comment: @user3381431: whether creating an index makes sense of not depends on what you are making index of. In your question, you seem to be trying to regenerate the underlying index - which is not possible unless you have data for index level for $x_{-4}$ to $x_{-1}$. On the other hand, if you are creating the index of inflation series itself, then you'll have to define a base. Say you assume that inflation in period 1, is 100. Then the rate of change of index changes completely. It now tells you by what percent has the inflation rate changed. That's more like acceleration rate of underlying series.
Comment: Ok with the stock return series I can create a series of YoY returns. Should it be transformed like so if I want to use both in time series analysis, given that the first series can only be YoY numbers?
Comment: @user3381431: I don't think it's necessary to transform your series B to YoY. The frequency of both series is quarterly and can very well be used. It's just that in QoQ form series B has more noise but then it also has more info.


Qual a diferença de tipos de exportação em React?

Question: Sou um iniciante em React e estou com uma dúvida bem simples, eu quero saber qual a diferença a criação e exportação de um componente, acontece que já vi 3 formas diferentes e quero saber se existe diferença entre elas.
//Primeira forma:
export default function Home() {</
    return()
}

//Segunda forma:
function Home(){
  return()
}
export default Home;

//Terceira forma:
const Home = () => {
   return()
}
export default Home;

Answer: Na verdade, sua pergunta não é sobre React, e sim sobre funções e módulos.
1. A parte export se refere aos módulos
(Documentação da MDN sobre módulos)
Os módulos foram integrados ao ECMAscript/javascript há poucos anos, e decorrem da necessidade de maior organização de partes reutilizáveis de código que, antes, tinham que ser feitos com bibliotecas de terceiros ou referenciados com a tag script, que precisavam ser declaradas seguindo estritamente a ordem de dependências para seu script não quebrar.
Em síntese, você apresentou duas formas diferentes de disponibilizar um módulo: colocar o export antes do nome da função ou declarar a função normalmente e colocar os exports após o nome da função a ser exportada, ou todos juntos no final do módulo. O resultado é o mesmo, a diferença é só do ponto de vista de organização de código (que geralmente é um consenso da equipe que trabalha em um projeto; as grandes empresas costumam ter guias de estilo, e alguns até viram referência para além da empresa).
Portanto, quanto aos exports, as duas formas que você apresentou são idênticas.
Duas observações:

Um módulo pode ter mais de uma função ou variável exportada. A explicação refoge ao âmbito da pergunta, mas uma costuma (embora não seja obrigatório) ser marcada como default.

As opções de exportação têm o mesmo resultado, mas a importação não. É possível fazer a importação na raiz do módulo (nesse caso, o outro módulo será carregado de imediato), mas também é possível fazer a importação em locais posteriores do seu código, o que pode melhorar a performance (por exemplo, só importar uma biblioteca de frontend de ferramentas de admin se o usuário for um admin, etc). Segue documentação (em inglês).

2. A outra parte se refere a como declarar uma função
(Documentação da MDN sobre function expressions, com link para arrow functions).
As duas primeiras formas que você apontou usam uma function expression; a terceira, uma arrow function. São apenas formas diferentes de declarar funções. A principal diferença é que, na arrow function (a terceira forma), a função não tem um contexto próprio e usa o contexto do lugar onde foi declarada; porém, se é um componente React declarado em um módulo, não fará diferença.
Observação: existe uma forma diferente de declarar um componente, aí sim seria uma pergunta típica do React: você pode ter componentes de classe e componentes funcionais. Todos os seus 3 exemplos são componentes funcionais (declarados mediante funções). Existe também a possibilidade de declará-los como classes. Essa era, inclusive, a única forma. Os componentes funcionais são relativamente recentes no React, e foram criados com a intenção de substituir os componentes de classe (embora isso não seja uma regra obrigatória, hoje em dia quem usa componentes de classe o faz por padronização quando tem um código legado, ou porque, por qualquer motivo, está mais familiarizado com eles, mas a tendência clara é usar componentes funcionais, e não de classe).
Portanto, sim: no âmbito do React, as três formas que você apontou são similares.
Answer: Man, na verdade se resume a duas maneiras de exportar, basicamente é sobre o uso do default.

IMPLÍCITA:

export default function Home() {
 // code...
}

onde você importa usando o nome que quiser, ex:
import NomeQueQuiser from './home'

NOMEADA:

export function Home() {
 // code...
}

já dessa maneira, o modo de importar o modulo/função muda, obrigando a utilizar o mesmo nome da função, inclusive podendo usar destructuring, ex:
import { Home } from './home'

Sobre a forma que você declara a função, é indiferente para a exportação. ex:
// declarativa (function declaration)
function Home () {}

// seta (arrow function)
const Home = () => {}

Espero ter ajudado, man!
abraço!


Salvar estado dos fragments que esta em um viewPager

Question: Sempre que o dispositivo é rotacionado estou perdendo o conteúdo dos fragments que estão em três tabs, já fiz uso do onSaveInstanceState em Activity mais agora estou levando um banho porque de minha Activity principal estou chamando um Mainfragement este por sua vez recupera os três objetos
    private TabLayout mTabLayout;
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

e infla a view que tem três Fragments, o A, B e C, inicialmente eu tentei fazer a recuperação em cada fragment mais não é isto pois continuo perdendo os dados de cada view então com certeza tenho que atuar no MainFragment
Estou tentando alterar o adaptador para solucionar o problema, procurando ainda referencias na documentação se alguém puder dar uma força
Aqui esta o meu adaptador
class SectionsPagerAdapter extends FragmentPagerAdapter {

   private Fragment[] currentFragment ;
   private String[] mTabTiles;
   private Context mContext;

   public SectionsPagerAdapter(FragmentManager fm, Context context, String[] tabTiles) {
      super(fm);
      this.mTabTiles = tabTiles;
      this.mContext = context;
      this.currentFragment = new Fragment[this.mTabTiles.length];
   }

   @Override
   public Fragment getItem(int position) {

       Fragment frag = null;

       if (position==0){
          frag = new FragmentA();
        } else if(position==1){
          frag = new FragmentB();
        } else if(position==2){
          frag = new FragmentC();
      }

     currentFragment[position] = frag;

     Bundle b = new Bundle();
     b.putInt("position",position);

     frag.setArguments(b);

    return frag;
   }

   @Override
   public int getCount() {
       return this.mTabTiles.length;
   }

   @Override
   public CharSequence getPageTitle(int position) {

       return this.mTabTiles[position] ;
   }

}
e o MainFragment
public class MainFragment extends Fragment {

   private static final String TAG = "MainFragment";

   private TabLayout mTabLayout;
   private SectionsPagerAdapter mSectionsPagerAdapter;
   private ViewPager mViewPager;

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_main, container, false);

       Log.i(TAG, "onCreateView()");

       mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager(), getActivity(), getResources().getStringArray(R.array.tab_titulo));

       mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
       // Set up the ViewPager with the sections adapter.
       mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
       mViewPager.setAdapter(mSectionsPagerAdapter);
       mTabLayout.setupWithViewPager(mViewPager);

       return view;
}

   @Override
   public void onSaveInstanceState(Bundle outState) {
       super.onSaveInstanceState(outState);

       Log.i(TAG, "onSaveInstanceState");

       //outState.putInt("currentTab", mTabLayout.getCurrentTab());
       outState.putInt("position", mTabLayout.getSelectedTabPosition());
       outState.putInt("currentPage", mViewPager.getCurrentItem());
   }

   @Override
   public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
       super.onViewStateRestored(savedInstanceState);

       Log.i(TAG, "onViewStateRestored");

       if(savedInstanceState!=null) {

           Log.i(TAG, "onViewStateRestored -> Posição: " +    savedInstanceState.getInt("position"));

           mViewPager.setCurrentItem(savedInstanceState.getInt("position"));

           Log.i(TAG, "onViewStateRestored -> Posição: " + savedInstanceState.getInt("position") + " -> mViewPager.setCurrentItem()");

         mSectionsPagerAdapter.getItem(savedInstanceState.getInt("position"));

           Log.i(TAG, "onViewStateRestored -> Posição: " + savedInstanceState.getInt("position") + " -> mSectionsPagerAdapter.getItem()");

       }
   }

o que eu consegui pesquisar eu atualizei o método onSaveInstanceState e criei criei o onViewStateRestored na classe MainFragment que já atualizei no código acima, embora esta alteração não tenha dado o resultado esperado, eu estou chamando o Adapter da ViewPage com a posição da Tab e observo no logcat os eventos quando eu rotaciono o dispositivo
// Salvando os estados
I/MainFragment: onSaveInstanceState
I/FragmentA: onSaveInstanceState()
I/FragmentB: onSaveInstanceState()

I/MainFragment: onCreateView()
// Nova instancia do adaptador
I/SectionsPagerAdapter: SectionsPagerAdapter()
// Aqui vejo que foi obtida a posição 0 da Tab ou seja a primeira Tab
I/MainFragment: onViewStateRestored
I/MainFragment: onViewStateRestored -> Posição: 0
I/MainFragment: onViewStateRestored -> Posição: 0 ->     mViewPager.setCurrentItem()
I/SectionsPagerAdapter: getItem( 0)
I/MainFragment: onViewStateRestored -> Posição: 0 ->    mSectionsPagerAdapter.getItem()
I/FragmentA: onCreateView()
I/FragmentA: onActivityCreated()

I/MainFragment: onCreateView()
I/SectionsPagerAdapter: SectionsPagerAdapter()
I/MainFragment: onViewStateRestored

Comment: Fiz um teste criei outro projeto, e sem Fragments na atividade principal, os fragments são só para os Tabs, no xml da activity o ViewPage e realmente tudo funcionou como deveria e eu nem precisei salvar estado de nada na activity, nada, ou seja o rolo ocorreu qdo eu "migrei" o Tabs e o ViewPage para um fragment, então ramaral vc esta certo quado disse "Isso não deveria acontecer".
Comment: Seja mais explicito em relação a quais valores pretende guardar.
Comment: Olá Ramaral, então eu preciso quando rotacionar o dispositivo as informações que tenho nas Tabs continuem só isto. Quando isto ocorre eu tenho que selecionar Tab por Tab uma a uma para que o seu conteúdo apareça.
Comment: Não entendi. Se ao seleccionar a tab o conteúdo aparece é porque ele foi guardado.
Comment: Quando eu seleciono a Tab manualmente sim claro aparece isto porque o Adaptador ( SectionsPagerAdapter ) é chamado e executa o getItem(). O que eu quero e acredito que seja este comportamento de todo app é quando eu acesso uma Atividade e rotaciono o dispositivo o Android reconstroi a Tela, então usando os métodos para guardar eu posso restaurar a tela e é este o meu intento, vou atualizar o que consegui pesquisando estes dias.
Comment: Fiz uma atualização da classe MainFragment, atualizei também minha pergunta com o log dos eventos, embora eu esteja pesquisando o TabLayout e procurando salvar a posição getSelectedTabPosition() esperava muito que o viewPager.setCurrentItem(savedInstanceState.getInt(posicao)) pudesse resolver o problema e restaurar os dados quando eu rotacionasse o dispositivo pois foi o que eu entendi, mais estou na pesquisa
Comment: Quando diz que "não aparece" quando roda o dispositivo, quer dizer que a tela fica em branco? Por que tem o TabLayout no layout de um fragment e não no da activity?
Comment: Coloquei uma imagem da tela rotacionada acredito que agora dê para exemplificar o que esta ocorrendo, na orientação retrato todas as tabs tinham conteúdo qdo eu rotaciono eles desaparecem mesmo eu salvando e recuperando o estado.
Comment: Isso não devia acontecer. As views são recuperadas automaticamente pelo sistema. O único facto estranho(nunca vi fazer assim) é o TabLayout  estar no layout de um fragment e não no da activity, poderá ser essa a razão desse comportamento anómalo.
Comment: ramaral, o motivo de estar em um fragment é o projeto foi criado com o Navigation Drawer, a atividade principal continha ao Tabs e o ViewPager, só que eu coloquei outras atividades no menu do Drawer e chamando estas eu perdia o Menu Drawer, ficou feio então transformei tudo em fragments e o que estava na activity principal os tabs eu passei para um fragmentMain, no inicio do projeto eu não testei a rotação do dispositivo mais vou fazer isto voltando o código.
Comment: Crie uma activity com o Drawer, depois herde as outras activities dessa.
Answer: Fiz um outro projeto no qual eu faço uso do tabs com TabLayout, ViewPager chamando de uma activity com esta abordagem o posicionamento do dispositivo não ocasionou nenhum problema na view tudo funcionou corretamente, então conclui que eu ter migrado os objetos tablayout e viewpage para um fragment foi o que ocasionou o comportamento.


Enable / Disable Fields on Client Side in Razor view

Question: I am trying to add code so when a user checks a box a certain field is enabled and another disabled and vice versa if the check box is not checked.  I have looked on here and found this code:

@Html.EditorFor(model => model.UseForecast, new { htmlAttributes = new { @class = "form-control" } })

@Html.EditorFor(model => model.MinimumOnHandQuantity, new { htmlAttributes = new { @class = "form-control" } })

@Html.EditorFor(model => model.ForecastMultiplier, new { htmlAttributes = new { @class = "form-control" } })

What am I missing?
Which I modified to include the ids of the fields on my page.  UseForecast is the id of the checkbox, MinimumOnHandQuantity is the first textbox, and ForecastMultiplier is the id for the second textbox.  When I try to step through the code, it stops when the page initially loads but it will not run whenever I click on the check box.
Comment: You need a document load handler OR move the script below the html elements. Also double check the id attributes to make sure the helpers are rendering the id as you expect.
Comment: Are you sure UseForecast is a `CheckBox`?
Comment: @Jasen, where below? at the bottom of the page?
Comment: @Jasen I put it at the bottom of the page and it still does nothing but it now crashes when the page initially loads
Comment: @AT-2017, I just double checked in an inspect element and it says type="checkbox"
Comment: Review [this question](https://stackoverflow.com/questions/14328449/when-do-you-put-javascript-in-body-when-in-head-and-when-use-doc-load) and the related links.
Answer: Putting ready method handling block before click event works as expected, no need to move script tag outside the head element:

Note: Remember to include jQuery script file inside head element either through script element (


cosine similarity = 1.0 even when source/input string has extra tokens not seen in the corpus?

Question: I am using TfidfVectorizer and cosine_similarity from scikit-learn.  When I have a new string and I try to find the cosine similarity to the strings in the original training corpus, I notice that the cosine similarity is 1.0 even when the string is an exact match plus additional novel tokens in the new string, no matter how many such additional tokens there are.
For example a new string of the form "a b x y z" will have cosine similarity 1.0 to an original string "a b" if x, y, and z are not in the original corpus at all.
I understand how this happens, because the novel tokens are ignored when vectorizing the new string according to the features established by the training corpus, but I want to be able to detect that "a b x y z" is NOT really a "perfect" match to "a b".
Any ideas on how I could incorporate something into the matching that would be sensitive to this type of difference (presence of novel tokens)?

Edit: here is an illustration based on the comments of @Arash:
The scenario I am describing tries to match novel input to the trained corpus:
<code>corpus = (
        "The sky is blue",
        "The sun is bright",
        "The sun in the sky is bright",
        "We can see the shining sun, the bright sun"
        )
input = ("The sky is blue",
        "They say the sky is blue you know",
        )
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_vectorizer = TfidfVectorizer()
tfidf_corpus = tfidf_vectorizer.fit_transform(corpus)
print(tfidf_corpus.shape)

tfidf_input = tfidf_vectorizer.transform(input)
print(tfidf_input.shape)

from sklearn.metrics.pairwise import cosine_similarity
cosine_similarity(tfidf_input, tfidf_corpus)
</code>
The output is
<code>(4, 11)
(2, 11)
array([[1.        , 0.36651513, 0.52305744, 0.13448867],
       [1.        , 0.36651513, 0.52305744, 0.13448867]])
</code>
So you can see that both input strings/documents get a perfect similarity of 1.0 to the corpus's "The sky is blue" even though the second input ("They say the sky is blue you know") has several non-matching words (which happen not to appear in the corpus).
I would have expected that the cosine similarity for my second input item could be computed on an extended vector that adds elements for the novel words (each of which has a document frequency of 0 and so maximal IDF), so that the similarity would come out lower.
Simply including the new input data in the corpus is not a good general solution since we might receive new inputs one at a time in the future and we don't want to have to re-train the entire corpus every time we want to match one new input document/string.
Comment: This seems more appropriate for [Cross-validated](https://stats.stackexchange.com) as it asks for a methodology and not a programming issue in scikit-learn per se.
Answer: I can't replicate what you are describing. Try this:
<code>    documents = (
    "The sky is blue",
    "The sky is blue you know",
    "The sun is bright",
    "The sun in the sky is bright",
    "We can see the shining sun, the bright sun"
    )

    from sklearn.feature_extraction.text import TfidfVectorizer
    tfidf_vectorizer = TfidfVectorizer()
    tfidf_matrix = tfidf_vectorizer.fit_transform(documents)
    print(tfidf_matrix.shape)

    from sklearn.metrics.pairwise import cosine_similarity
    cosine_similarity(tfidf_matrix[0:1], tfidf_matrix)
</code>
Your output will be
<code>    array([[1.        , 0.67166626, 0.35369001, 0.50353381, 0.13245011]])
</code>
This indicates that the similarity between the first and second sentences is 0.67, not 1.0

EDITED
The issue that you are describing is that the vectors are identical. You want the TF-IDF to take the new words and the frequency of the new words into account.
So, you can just do this:
<code>    # refit tfidf_vectorizer to the corpus and new documents
    tfidf_vectorizer.fit(input + corpus)
    # transform using the new model
    tfidf_input = tfidf_vectorizer.transform(input)
</code>
Comment: That's not quite my scenario.  I have modified your example and added it to the original question to clarify my question.
Comment: @dabru: maybe you're confusing yourself by looking at the cosine similarity. In your updated question, just look at `tfidf_input.toarray()`. To paraphrase your question,  the two vectors are identical and you don't want that because you want the new vocabulary to be taken into account. SO, just do exactly that. You'll need to add the new documents to the old ones and fit/transform again.
Comment: Take a real-time scenario where a new sentence comes in every few milliseconds and we must find its best match within the existing corpus and send it out within a few more milliseconds.  We can't re-train the entire vocabulary for every input sentence.  We wouldn't have to if vectorizer.transform() were to automatically extend its output by adding columns for any novel words/ngrams in its input, and then if cosine_similarity were to pad the shorter vector (the one from original corpus) with zeros before calculating the similarity.
Comment: You can do it, but you won't be dealing with TF-IDF vectors anymore. If you do re-fit it, all the vectors will be different.
Here's what you can do: since the probability of getting new words will be smaller and smaller as your data grows, first tokenize your words by setting out of vocab (e.g. here https://stackoverflow.com/questions/48432300/using-keras-tokenizer-for-new-words-not-in-training-set) and then do TF-IDF transform. You can refit tokenizer and TF-IDF periodically to improve the accuracy of your distances.
Comment: See if ^ will have a reasonable output
Comment: Okay so you are saying that I could use Keras tokenizer which supports an "out of vocabulary" token, whereas scikit-learn lacks this capability?
Comment: I'd generally NOT rely on sklearn for my NLP tasks. Search for NLTK or Spacy with out of vocabulary or OOV and you'll find plenty of posts.


Removing one message from a topic in Kafka

Question: Can I delete only ONE message from a topic if I know the topic, the offset and the partition?
And if not is there any alternative?
Answer: It is not possible to remove a single message from a Kafka topic, even though you know its partition and offset.
Keep in mind, that Kafka is not a key/value store but a topic is rather an append-only(!) log that represents a stream of data.
If you are looking for alternatives to remove a single message you may

Have your consumer clients ignore that message

Enable log compaction and send a tombstone message

Write a simple job (KafkaStreams) to consume the data, filter out that one message and produce all messages to a new topic.
Comment: Considering our event stream context, there's a fourth option that aligns more with the event-sourcing paradigm. Instead of attempting to tombstone an erroneous message, you could consider appending a new `Failed` event to the end of the topic. This approach involves shifting the pointer within your consumer. The `Failed` event could encompass a reference to the original unprocessed event along with a record of the attempted actions. This technique allows the consumer's pointer to progress while retaining the means to retry the problematic event :)
Comment: P.S. the above solution would only be appropriate if you wanted to advance the consumer pointer and retry at the end of the current stream, but hopefully that's clear from context.


Why can't getaddrinfo be found when compiling with gcc and std=c99

Question: I have the following code which I was trying to compile. When I tried with std=c99 it failed with warnings about "implicit declaration of type struct addrinfo" and "implicit declaration of function getaddrinfo". It works with std=gnu99.
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

int fails(const char *host, const char *port, struct addrinfo *hints)
{
        int rc;
        struct addrinfo *results;

        // can't find this function??
        rc = getaddrinfo(host, port, hints, &results);

        // free memory in this important application
        freeaddrinfo(results);

        return rc;
}

The commands I used to compile is:
gcc -c -o fail.o -Wall -Werror -std=c99 -save-temps fail.c
gcc -c -o fail.o -Wall -Werror -std=gnu99 -save-temps fail.c

Looking at fail.i (preprocessed header) I see that compiler is right: those types haven't been declared in the headers pulled in.
So I went to the headers and noticed that getaddrinfo is surrounded by a guard #ifdef __USE_POSIX, which is obviously not declared when compiling with c99.
How do I tell gcc that I want to use c99 and POSIX? I don't really want to use gnu99 in case I decide to switch compilers later (eg Clang or icc).
Comment: The double-underscore macros are for internal glibc use only. You're supposed to define `_POSIX_C_SOURCE`, `_XOPEN_SOURCE`, or `_POSIX_SOURCE` like it says at the top of the getaddrinfo man page under "Feature Test Macro Requirements". And please do read the related feature_test_macros(7).
Comment: ah, the glibc ifdef maze strikes again
Comment: Have you tried `gcc -c -ofail.o -Wall -Werror -std=c99 -D__POSIX -save-temps fail.c`? That or make a point to `#define __POSIX` at the top of `fail.c`.
Comment: By the way, are you sure it's `__POSIX` and not `_POSIX_C_SOURCE`?
Comment: I just checked again and it's ___USE_POSIX
Comment: @AlanCurry: Thanks, I'd read the man page but skimmed over that section (and definitely hadn't read the feature_test_macros(7) man page). That's the answer I was looking for.
Answer: Simply because getaddrinfo (POSIX.1g extension) is not part of the standard c99:
http://www.schweikhardt.net/identifiers.html
stay with -std=gnu99 or -D_POSIX_C_SOURCE=200112L


Unable to stop RabbitMQ from listening on port 25672 on all interfaces

Question: I'm currently working on an application using celery in combination with rabbitmq, due to security concerns no services that don't require any connection with the internet should only listen on localhost.
After looking around I found this question which shows how I can make most ports listen only on localhost, however for some reason port 25672 remains open, which I determined to be part of rabbitmq:
$ nmap -sV -p25672 -T5 <my server>

Starting Nmap 7.50 ( https://nmap.org ) at 2018-08-04 23:54 CEST
Nmap scan report for <my server> (<my server>)
Host is up (0.011s latency).

PORT      STATE  SERVICE VERSION
25672/tcp open   unknown

Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 124.87 seconds

On the server:
$ lsof -i :25672
COMMAND   PID     USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
beam.smp 4513 rabbitmq   12u  IPv4 473675236      0t0  TCP *:25672 (LISTEN)

The man entry for rabbitmq does not state how to change this, nor can I find out how to do so online. Could anyone point me in the right direction on how to make it listen on 127.0.0.1 rather than 0.0.0.0?

Edit: Fuck it, iptables will do.
iptables -A INPUT -p tcp -s localhost --dport 25672 -j ACCEPT
iptables -A INPUT -p tcp --dport 25672 -j DROP

Answer: I found it in the documentation in five seconds after searching for rabbitmq port...

listeners.tcp.1 = 127.0.0.1:5672
listeners.tcp.2 = ::1:5672

Or in the classic config format:
[
  {rabbit, [
    {tcp_listeners, [{"127.0.0.1", 5672},
                     {"::1",       5672}]}
  ]}
].

(Warning: You probably need both, as localhost resolves to ::1 on all OSes released in the last decade or so, and binding only to 127.0.0.1 could cause some apps to have problems.)
Comment: That simply does the same as [this](https://serverfault.com/questions/235669/how-do-i-make-rabbitmq-listen-only-to-localhost) question does, while this closed port `4369` for me, for whatever reason port `25672` won't seem to close when doing this
Comment: Oh, I see. Well, you can't run a cluster, then. That's the cluster communication port, and it must be open so that the other cluster nodes can communicate. Check your config and make sure you haven't got anything that might configure a cluster. You might also have to `rabbitmqctl reset`. If you intended to have a cluster, you'll need to configure a host firewall.


Generate an XML representation of the contents of a folder

Question: I have a folder full of text files called MainFolder, and I want to convert the folder to an XML representation so that every file in the folder can be edited as a single text file. Would it be possible to generate an XML representation of a folder structure, as shown here, with files inside <file> tags and folders inside folder tags?
<folder name = "MainFolder">
    <file name = "README.txt">
        This is a text file inside a folder called MainFolder. This is the content
        of README.txt.
    </file>
    <folder name = "Java samples">
        <file name = "HelloWorld.java">
            public class HelloWorld{
                public static void main(String[] args){
                    System.out.println("Hello World!");
                }
            }
        </file>
    </folder>
</folder>

Comment: Maybe you could take some inspiration from http://stackoverflow.com/questions/316590/how-do-you-use-cat-recursively
Comment: I tagged this question as "language-agnostic" because I know what I'm trying to accomplish, but I don't know which programming language would be the best fit for the job.
Comment: The contents would have to be enclosed in a `<![CDATA[` section. But what if one of those files was an XML file and had a `<![CDATA[` section?
Comment: Why would you need this?
Comment: @Cratylus I want to create and edit multiple text files at once using a single text file, since it's easier than opening multiple windows and constantly switching back and forth between them.
Answer: Try this utility:
http://code.google.com/p/xml-dir-listing/
(padded because Stackoverflow doesn't like short answers)


Richardson extrapolation: how to derive an approximation for $f'(x)$ with Euler backward/forward methods

Question: We are told that the starting point is the difference
$ f(x+h) - f(x-h) $ (each of which represents it's own series; the last an alternating series) for getting the Richardson extrapolation.
When I do this subtraction the even exponential terms cancel out because of the difference sign while the odd exponential terms ones sum up yielding the following:
$ f(x+h) - f(x-h) = 2hf'(x)+{2h^{3}\over 3!}f'''(x)+... $
So the expression for the derivative yields if i understand it correctly, where now the h's get even exponentials:
$f'(x) = \frac{f(x+h) - f(x-h)}{2h} = f'(x) + {h^{2}\over 3!}f'''(x)+...$
Making the stepsize h half as big by replacing every $h$ with $h/2$ in the last expression yields
(where $4 = 2^2$ and $16 = 2^4$) :
$f'(x) =  \frac{f(x+h/2) - f(x-h/2)}{h} + {h^{2}\over 4*3!}f'''(x)+{h^{4}\over 16*5!}f'''''(x)...$
Now we are supposed to get a series on the form to get an approximation for the derivative:
$f'(x) = \frac{4(f(x+h/2) - f(x-h/2))}{3*h} - \frac{(f(x+h) - f(x-h))}{3*2h} -\frac{1}{4}a_4 h^4 + \frac{5}{16}a_6 h^6 + ...$
Here the a's are constants.
I'm wondering how the numerical values before each term are achieved. The deviation from my result as far as I can see are the numbers above the fractions, and the 2 first terms. I don't really see how to get the last expression, any suggestions?
Comment: Note that $f(x+h) - f(x-h) = 2 hf'(x)+{2h^{3}\over 3!}f'''(x)+...$
Comment: This looks wrong $$f'(x) = \frac{f(x+h) - f(x-h)}{2h} = \frac{f(x+h) - f(x-h)}{h} + {h^{2}\over 4*3!}f'''(x)+{h^{4}\over 16*5!}f'''''(x)...$$ you mention the replacement of $h$ with $h/2$, may be you want to use it in your formula?
Comment: Thx added, typos sneak in when head is warmed up:-)
Comment: should you use fix the last expression as well?
Comment: aah yeah the 1st term in the last expression should now be correct, sorry
Answer: Since
$$ \frac{f(x+h) - f(x-h)}{2h} = f'(x) + {h^{2}\over 3!}f'''(x)+
{h^{4}\over 5!}f'''''(x)...$$
and
$$ \frac{f(x+h/2) - f(x-h/2)}{h}= f'(x) + {h^{2}\over 4*3!}f'''(x)+{h^{4}\over 16*5!}f'''''(x)...$$
One write
$$\frac{4(f(x+h/2) - f(x-h/2))}{3*h} - \frac{(f(x+h) - f(x-h))}{3*2h} =
\frac{4}{3}
\left(f'(x) + {h^{2}\over 4*3!}f'''(x)+{h^{4}\over 16*5!}f'''''(x) ...\right)-
\frac{1}{3} \left(f'(x) + {h^{2}\over 3!}f'''(x) + {h^{4}\over 5!}f'''''(x) ...\right) =
f'(x) + \frac{4}{3}{h^{4}\over 16*5!}f'''''(x) - \frac{1}{3}{h^{4}\over 5!}f'''''(x)...
=
f'(x)  - \frac{1}{4}{h^{4}\over 5!}f'''''(x)...
$$
Hope this helps.
Connection to Richardson Extrapolation
Richardson Extrapolation for a method defined as
$$A(h)=A_0+Ch^n+o(h^{n+1})$$
is defined as
$$R(h,k)=\frac{k^nA(h)-A(k h)}{k^n-1}$$
In the question above we have $k=2$, $n=2$ and
 $$A(h)=\frac{f(x+h/2) - f(x-h/2)}{h} + Ch^2...$$
Comment: Thx, I see the relation now. moving the derivative on the other side of the equal sign and equally for the expressions with the (x+h) and (x-h) terms we get what is to be shown. But still the question arises about how one should come up with the ratios 4/3 and 1/3 assuming they are not given since we are to end up with them. Or maybe just a misinterpretation from my side.
Comment: I've edited my answer to answer your question, to get the coefficients you
evaluate $\frac{k^n}{k^n-1}=\frac{4}3$ and $\frac{1}{k^n-1}=\frac{1}3$
Comment: Thanks, that makes the connections easier to see, so iow there is a recipe we follow and this last part shows the direct relation between our series and the theory..


Auto add nodes on save to a queue using entityqueue

Question: I am using entity queue module, Created a queue with a name ABC.
I want to add nodes automatically to this queue for a specific content type while creating new nodes.
Is there a way to do this?
Answer: Try Auto Entityqueue module to Automatically add entities to entityqueues.
Answer: Create a custom rule  which calls a function like
<code>/**
* Add a node to a subqueue
*/
function abc_queue_add_node_to_entityqueue($subqueue, $node){
  $existing_nodes = abc_entity_queue_get_subqeueue_nodes($subqueue);
  if (!in_array($node->nid, $existing_nodes)) {
    $subqueue->eq_node[LANGUAGE_NONE][] = array('target_id' => $node->nid);
    entityqueue_subqueue_save($subqueue);
  }
}
</code>


Cardinality of a power set is given be $2^n$. Why?

Question: Cardinality of power set of $A$ is determined by $2^n$, where n is the number of elements in Set $A$. How does this formula work?
I was taught that there are $n$ elements in Set $A$ and each element has a choice of either being in the power set or not i.e., $2$ choices. Hence $\underbrace{ 2\times 2\times 2\times 2 \cdots}_{n\; times}  $.
But I don't understand why the element's choice of either being in the power set or not helps us in determining the cardinality of the power set.
Comment: That is exactly the argument, except they have a choice of being in a subset which is in the power set.
Comment: @Kaushik I mean that your explanation is right.
Comment: So why does the above formula work?
Comment: @FFjet Sorry, I am not sure I understand you.
Comment: Please check the edited question. Thanks!
Comment: We prove it; see [Cardinality of the Power-set](https://en.wikipedia.org/wiki/Power_set#Properties).
Answer:
I was taught that there are n elements in Set A and each element has a choice of either being in the power set or not i.e., 2 choices.

By definition, the power set of $A$ is the set of all subsets of $A$. The power set of $A$ does not contain elements of $A$, it contains subsets of $A$.
In how many ways can you choose a subset (say, $X$) of $A$? Well, every element in $A$ has a choice of either being in $X$ or not, i.e. $2$ choices. Thus there are $2^n$ ways you can form a subset $X$. Thus the total number of subsets is $2^n$.
To summarize: your mistake seems to be that you have swapped "choice being in $X$" (where $X$ is a subset) to "choice being in the power set".
Comment: Let's go by example. One subset of $A$ is the empty set $X=\emptyset$. This is the result if all elements of $A$ "choose" to *not* be in $X$. Another subset of $A$ is the whole $X=A$. This is the result if all elements of $A$ "choose" to *be* in $X$. Pick another subset $X\subseteq A$. This is the result if those elements that are in $X$ "choose" to *be* in $X$, and those that are not "choose" to *not* be. Every possible "choice" of the elements gives you a subset and vice versa. Thus the number of possible subsets is the same as the number of possible "choices".
Comment: But I don't understand why the element's choice of either being in the subset or not helps us in determining the cardinality of the power set.
Answer: This is a nice exercise for mathematical induction. It follows from these observations:
Assume that $X$ is a nonempty set and let $x_0\in X$ be a fixed element. Now let
$$T_{x_0}=\{A\subset X\ |\ x_0\in A\}$$
$$U_{x_0}=\{B\subset X\ |\ x_0\not\in B\}$$
The following properties are straightforward:
$$U_{x_0}=P(X\backslash\{x_0\})$$
$$P(X)=T_{x_0}\cup U_{x_0}$$
$$T_{x_0}\cap U_{x_0}=\emptyset$$
The crucial observation is that
$$|T_{x_0}|=|U_{x_0}|$$
I encourage you to construct such a bijection by yourself. All those facts together give
$$|P(X)|=2\cdot |P(X\backslash\{x_0\})|$$
and now you apply mathematical induction. Can you complete the proof?
Answer: Impose an order on $A$. You can think of each subset in terms of the $n$-bit string that tells you which elements it has, with the $i$th bit $1$ if the $i$th element of $A$ is to be included, or $0$ if it isn't. If $A$ is infinite so that $A$ is a transfinite cardinal, this argument still works (but then your string is infinite, and if uncountable you shouldn't even think of it as a string, but rather a function from elements of $A$ to $\{0,\,1\}$ - which also means we don't need to order $A$ any more), so @freakish's use of induction, which wouldn't carry over to that case, isn't needed. However, this is really how we define $2^n$ for $n$ transfinite.
Answer: Computer scientists favorite proof...
Let $A = \{a_1, \ldots, a_n\}$ be a set. A subset of $B$ can be represented by a bit vector $b \in \{0,1\}^n$ where
$$
b_i = \begin{cases}
1 &\text{if $a_i \in B$}\\
0 &\text{if $a_i \notin B$}
\end{cases}
$$
For instance, the subset $\{2,3, 5, 7\}$ of $\{1, 2, \ldots, 10\}$ is represented by the bit vector $(0,1,1,0,1,0,1,0,0,0)$. This gives you a bijection between $\mathcal{P}(A)$ and $\{0,1\}^n$ and thus $|\mathcal{P}(A)| = 2^n$.


Matching multiple pattern in Ant replaceregexp task

Question: I have a task in my Ant build script for adding a parameter to the src attribute to all


Udemy multiple monitor issue while connecting through docking station

Question: I am using multiple monitors connected through a docking station. When I am using Udemy, the external monitor gets disconnected whenever it navigates to the next video after it completes playing the current video.
I have to unplug-plug again the docking station to reconnect to the external monitor again.
Any solution for this problem?
Comment: Hi did you get a fix for the problem ?
Answer: set monitor refresh rate(s) to 60hz :)
got blackscreens at video start cuz my external monitor was at 75hz
Comment: Did the same but did not fix the problem
Comment: I can confirm this does not work for me either i already have tried 59.95hz or 60.01hz.
Answer: this solution worked for me :
settings -> search for hardware acceleration -> disable
Comment: I did the same thing but my display's still get disconnected. Right now i am having an acer swift 3 core with an intel 8th gen cpu and having two P2419H dell monitors attached to my system. OS : windows 11 home. I am facing the same problems when it comes to netflix as well as other apps. any other fix would be really appreciated.
Answer: I was using two monitors with my docking station, which has two HDMI ports. One of the monitors (a Dell monitor) was turning off whenever I was playing Udemy videos.
I tried disabling hardware acceleration in the settings, but this didn't work for me.
To resolve the issue, I connected an HDMI cable directly to my laptop's HDMI port instead of using the docking station's HDMI port. The other monitor was still connected to the docking station's HDMI port."


Is this posture correct for classical guitar?

Question: I bought a 2 foot stool to play classical guitar. I'm 6 feet. I've been trying to play using the stool and a foot stand and I feel very comfortable playing the guitar, but something just doesn't look right when I play.
I feel that maybe the stool is too high up and I'm holding the guitar at a smaller angle than I should be - I've read that the head of the guitar should be at the same level as my head.
In summary, I'm attaching two photos of me playing the guitar. Is there something wrong with my posture, or is this an acceptable position to be playing classical guitar?
Answer: Your position looks fine to me, and all the advice about head of guitar being at the same height as your head is after all only approximate, as everyone's physique differs to some extent.
The most important thing is that the position feels totally comfortable to you.  You are likely to spend many hours playing the guitar, especially if you are keen enough to go to the extent of posting photos here(!), and one of the most vital things to avoid at this stage is stress. Any kind of stress in you arms, neck or back can be a serious problem, and is the kind of thing that can creep up on you almost unnoticed, as you get so absorbed in your playing (getting the correct frets with one hand, and playing the right sort of stroke on the right strings with the other) that is is all too easy not to notice that you are playing with tension.
Having examined the photos a little more carefully, my only possible criticism would be the angle of your right hand where it meets the line of the strings. I would prefer the angle that the wrist makes with the line of the strings to more like a right angle, so that the right hand fingers are striking the string perpendicularly to their length. That tends to produce the clearest tone from the strings.
Good luck!
Answer: For me the sign of whether your foot stool is in a wrong position is whether your right foot lifts to meet the guitar. SOmetime I don't have a foot rest handy and then I just use my guitar case as a makeshift stool and I can really feel it in the ankles after a while from how I have to arch my one foot upwards to meet the guitar.
What I'm really wondering about is that chair. It seems to be some sort of upright bar stool. That chair may be too high and that is what is making you feel akward.
For classical guitar we want just a plain regular chair that has no padding for you to sink into. You need a certain amount of sturdiness so that your body is sitting solidly against the chair and your guitar sitting on your lap.
You may want to sway a little bit with the movement of the music but for the most part you want your arms to be the only moving parts.
Answer: My first reaction is your stool is awfully high. When concert guitarists play onstage they usually use a piano stool. When I sit on too high of a seat I end up having to use my hands to hold the guitar in my lap. It should be stable in your lap without having to hold it.
Compare with the posture of a few others:

Andres Segovia. Much lower stool. The whole guitar is much higher on his body. Segovia is shorter and rounder than you, so,

Christopher Parkening. He is tall, built more like you. Also using a lower stool with the guitar higher on his torso. He is holding the guitar at a slightly higher angle.

Julian Bream. Once again, lower seat.
All their left knees are at a higher angle than your photos and the body of the guitar is higher.
That said, Old John has a very good point that you must be able to sit in that position for long periods of time without back or shoulder problems. The angle of your wrist is also a concern. Your left wrist looks like it's bent a little farther than I would suggest. Spending a lot of time with your wrist bent too far can cause long-term problems. That's one problem I have on electric guitar now that I'm older. I get a bit of carpal tunnel from my wrist angle and end up with numb fingers.


Grant mysql access to remote servers without IP

Question: I'm setting up a hosting scheme that will involve a master server and clone servers all accessing a remote mysql DB on its own server. As traffic increases/decreases I will be adding or removing clone servers.
As I understand it, you have to grant permissions to each new IP that will be accessing a DB when it is not being accessed locally. But I am wondering if it is possible to grant privileges in a secure way that I don't have to do it every time I add a clone server.
If it has any relevance, I'm doing this on Rackspace, using cloud servers.
Comment: I haven't worked with scaling, but every instance will have the IP address in the `/etc/hosts` file. Maybe you could write a script as part of the image which will read the file and communicate its IP address to the DB server?
Answer: If you have a range of IP addresses, you can just add a single entry for the range as well as other options.  It should be pretty easy for you to figure out by looking at the MySQL docs, specifically the first table listed here.
Comment: What about using a hostname, ie. `email@example.com` instead of `firstname.lastname@example.com`?
Comment: What about it? Hostname examples are listed along with IP addresses at the link.  What aren't you understanding?
Comment: Do you favor one solution over the other? I'm guessing that with hostname, DNS resolving to an IP might slow down the connection, but it seems like a much easier solution to implement. By the way, what link are you talking about.
Comment: Then I think you will be using the public facing ethernet port. I think it would be better to use the private port?
Comment: @Paul yea, I am using the private IP (Rackspace service IP). Does that preclude using a hostname then?
Answer: Since I'm using Rackspace, and I am accessing the database on a server in the same region, I can use the service IP - therefore I can grant access to a user with the <code>%</code> wildcard character to match every host on a subnet like this:
<code>'user'@'10.%'
</code>
MySQL account name documentation
Answer: You have to allow only SSL-connections to the servers from any IP-address that have valid SSL key.
Refer to the MySQL manual chapter 6.3.6 for details.
Keep in mind that SSL produce some overhead and can decrease overall performance.


Why use index 0 when caching an ID in jquery?

Question: Why is an index of 0 used in the code below?
var $one = $('#one')[0];

Why not just use var $one = $('#one'); ?
REFERENCE
I found the above code while reading jQuery, how to use multiple cached elements
Comment: jQuery collection vs DOM element.
Answer: That's because $('#one') is a jQuery object, which is an "array-like" object containing your element. Since a jQuery object is an "array-like" object, adding the [0] retrieves the first and only element in the object.
As for optimization, operating on selectors makes jQuery (or Sizzle to be exact) run through the DOM and look for that one and only element in the DOM. Imagine that happening every time you do:
$('#one')

That's why, instead of operating on selectors and running through the DOM everytime, they cached the DOM element instead. You can easily recreate jQuery objects using existing references of elements, which isn't that much of a overhead compared to running through the DOM again.
var one = $('#one')[0]

// or synonymously
var one = document.getElementById('one');

// No running anymore through the DOM in search for "one" because jQuery simply
// uses this reference instead.
var theOne = $(one);

//Another jQuery object, but uses the same "one" fetched earlier
var anotherOne = $(one);

Answer: They are not equivalent.
var $one = $('#one')[0];

Caches the first DOM node matching the selector #one. In order to convert this back to a jQuery object, you would need to do $($one) in order to use any jQuery functions, e.g. $($one).addClass("example");.
var $one = $('#one');

Caches the jQuery object, so you could call: $one.addClass("example");
Which one is right depends on the requirements for the $one object and how it is used.
jsFiddle for the above.
Answer: var $one = $('#one')[0]; will return the actual DOM element
var $one = $('#one'); will return the wrapped jQuery object.


"Changing" standard deviation?

Question: I will admit to being just a hair about a novice when it comes to stats, but I feel like I have a decent working knowledge of things such as normal distribution, linear curves, standard deviations, etc. That said, I have a colleague who is proposing something that just doesn't make sense to me, and I'd like some input from people who know better:
I work in education, and my department recently gave midterm exams. This year, we used new assessments, and as such, we didn't know exactly what to expect in terms of how the students would perform.
The grades for some classes were about what we might have anticipated, but for several classes, the students performed much lower than similar cohorts have on past midterms. So, as a department, we agreed to apply a curve to these scores to account for the different and give a boost to the groups that struggled.
My colleague took it upon herself to spearhead the curving. While I think applying a linear curve would have been best, she felt that applying a normal distribution curve made more sense. In explaining her methodology, she writes:

"I set the standard deviation on the curve to mirror how similarly each particular group performed on each exam.  However, if the top score was pushed over 100, then I modified the number slightly until the top score was at or less than 100.  This, too, can be altered.  If we decide that the top student should always earn a perfect/nearly perfect score, then the standard deviation can be altered to push that top score closer to 100."

Am I missing something? I didn't think standard deviation was something that could be "set," as I understood it to be a reflection of the raw data available in the sample set.
I can't find evidence anywhere online of anyone changing or modifying a standard deviation to make a curve fit a desired outcome.
Can anyone clarify what might be going on here?
Comment: Whether you're "applying a linear curve" or a normal one, in the end you're just making stuff up.  Although your purpose might be noble and reasonable, you haven't articulated any objective criteria that could be used to guide your process.  That will make it difficult for anyone to develop an objective or defensible response.
Comment: Oh, that's definitely true. She's more or less unilaterally decided the average score should be 82%. So if that's the case, that might explain why she's manipulating the standard deviation?
Comment: I essentially agree with you. Typically, I don't like to apply curves at all. I'm of the mind that if the numbers you get are bad, usually that means that either the assessment is bad, the students weren't adequately prepared, or both. (There is, of course, the chance that the students just didn't study, but in this case, the numbers don't really suggest that.)

But again, this was a department decision.

My question remains, can one "change" a standard deviation?
Comment: One can only guess.  A possibility is that your colleague is performing a traditional "curving" of the grades by forcing them to adopt a bell-shaped (Normal) distribution.  Such distributions are determined by their location (average) and their spread--which is usually expressed as a standard deviation (SD).  Narrowing the SD will cause all the "curved" scores to bunch up more.  Perhaps that's what she's doing.  If so, then it appears she has already determined what the average will be and is unwilling to change it.
Comment: First come up with criteria to use on the re-grading function `g` that maps the old grade to the new grade.  Presumably `g` should be monotonic.  Also note that bell curving can violate `g(x) >= x` and actually lower a score.  Other approaches not in that framework can also be considered. It might be that the lower scores are due to just one or a few questions.  In that case re-score the test by re-weighting the questions.  Another possibility is to let students re-do the questions they did not get and give them partial credit for the re-done questions to boost the scores.
Comment: Shouldn't the average be C? A excellent, B good, C average, D needs work, F failing.
Answer: While I have no complete picture of the problem at hand, I believe the following comments may be of help:

It is reasonable to assume a roughly normal distributed shape for the marks, at least if: (a) the sample sizes are large enough to allow an assessment by eye that supports this assumption, or at least does not glaringly contradict it, and (b) if some outliers are ignored.

You write: "While I think applying a linear curve would have been best..." An affine-linear transformation actually blends very well with a normal distribution, since if X is normally distributed, then so is Y=a*X+b for any constants a,b. Here the (random) exam mark is modeled by X, and the "adjusted" mark by Y. So in this application we need a>0. The standard deviation
of Y is then a times the standard deviation of X.

Since Y does not represent the raw data, its mean and standard deviation can be changed freely via the choice of the above affine-linear transformation.

If your colleague only used a (that is, if she took b=0) to
transform the data, it would not raise the average mark and thus not
remedy the problem of poor overall performance; it would merely widen
the gap between strongest and weakest results (if a>1).

If the marks were to be raised by changing the constant b, this may necessitate reducing one or more top marks to below the maximum of 100.
Comment: Your comment #1 seems to make a [common error about the central limit theorem](https://stats.stackexchange.com/questions/473455/debunking-wrong-clt-statement).
Comment: I concede that my statement #1 was potentially misleading. As stated in the thread you refer, if data are non-normal, they will remain so no matter the sample size. But a reasonably large sample size could give at least some confidence about the normal distribution being a workable model in the first place (say through inspection by eye). This is especially so if one decides to discard outliers (which should never be done without reason).
Comment: Sure, but that's not at all what point #1 seems to say.
Comment: I have rewritten #1 to clarify what was meant.

"Changing" standard deviation?


If $A,B$ are positive definite, then $\sum a_{ij}b_{ij}\geq 0$

Question: I wish to prove it without using Schur product theorem. Consider the case for $2\times 2$ matrices. I have to show $$a_{11}b_{11}+a_{12}b_{12}+a_{21}b_{21}+a_{22}b_{22}\geq 0$$
Since $B$ is positive definite I can write $b_{11}=b_1b_1$ and $b_{22}=b_2b_2$. Then I have $$a_{11}b_1b_1+a_{12}b_1b_2+a_{21}b_2b_1+a_{22}b_2b_2+a_{12}b_{12}+a_{21}b_{21}-a_{12}b_1b_2-a_{21}b_2b_1$$ and $$a_{11}b_1b_1+a_{12}b_1b_2+a_{21}b_2b_1+a_{22}b_2b_2\geq 0$$
But this doesn't seem to go anywhere. I also have $a_{11}+a_{22}\geq a_{12}+a_{21}$ but that doesn't help me either. Any hint would be appreciated.
Comment: @RodrigodeAzevedo ... So if $A$ is positive definite, we may replace $B$ by $-B$ and expect the same conclusion?
Comment: One only needs one of the matrices to be PD, right?
Comment: Use the Cholesky decomposition of A and B.
Comment: @GEdgar Good point. Lack of paper & pencil leads to such idiocy.
Answer: Use the formula $S=\sum a_{ij}b_{ij} = \mathrm{tr}(AB)$ to write $S=\mathrm{tr} (UDU' B)  =\mathrm{tr} (D U'B U)$, where $D$ is diagonal and $U$ is orthogonal with $A=UDU'$.  The diagonal entries of $D$ and of $U'BU$ are positive, so $S>0$.


Solve for $x$ in terms of $m$: $|mx+m+2|=x+3$

Question: The problem is about the equation $|mx+m+2|=x+3$.
I have to solve this equation but I don't know how to do it. The teacher gave us the solution. Let $S$ be the set of solutions for $x$:

if $m \in (-\infty , -1) \cup (1, +\infty)$, then $S=\{-1, -((m+5)/(m+1))\}$
if $m \in [-1 , 1)$, then $S=\{-1\}$
if $m \in \{1\}$, then $S=[-3,+\infty)$.

I don't even know where to begin nor how to continue. Sorry to bother you all, have a nice evening. Oh, and I'm new over here so sorry if my formatting isn't what it should be, did my best.
Comment: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/104225/discussion-on-question-by-xaterys-solve-for-x-in-terms-of-m-mxm2x3).
Answer: First of all, as you observed, it must be $x\geq -3$. In this hypothesis, the given equation is equivalent to that obtained by squaring, that is:
$$ |mx+m+2|^2 = (x+3)^2 $$
or, calculating and simplifying:
$$ (m-1)(m+1)x^2 + 2(m-1)(m+3)x + (m-1)(m+5) = 0. $$

If $m=1$, equation reduces to $0=0$, which is satisfied for every $x$, for every $x\geq -3$ of course.
If $m\neq 1$, equation becomes

$$ (m+1)x^2 + 2(m+3)x + m+5 = 0. $$
Two cases:

If $m=-1$, we obtain $4x+4 = 0$, $\hskip1ex$ i.e. $\hskip1ex$ $x=-1.$
If $m\neq -1$, we can use the well known formula for quadratic equations, obtaining:
$$ x = \frac{-(m+3) \pm \sqrt{(m+3)^2-(m+1)(m+5)}}{m+1} = \frac{-m-3 \pm 2}{m+1} $$

that is $x = -1 \hskip2ex \text{or} \hskip2ex x = \displaystyle -\frac{m+5}{m+1}.\hskip1ex$ Now, we just have to see when the last solution is acceptable, i.e. when
$$ -\frac{m+5}{m+1} \geq -3. $$
This inequality is equivalent to the union of the following two systems:
$$ \left\{ \begin{array}{c} m+5 \leq 3(m+1) \\ m+1 > 0 \end{array} \right. \hskip4ex
\text{and} \hskip4ex \left\{ \begin{array}{c} m+5 \geq 3(m+1) \\ m+1 < 0 \end{array} \right. $$
whose solutions are respectively $m\geq 1$ and $m\leq -1$. Remembering that currently is $m\neq 1$ and $m\neq -1$, we obtain that both solutions of the quadratic equation above are acceptable if and only if $m\in [-\infty,-1[\,\cup\,]1,+\infty[$. Otherwise, that is in the interval $]-1,1[$, only $x=-1$ is acceptable.
You can immediately see that the teacher's solution is thus obtained.


DNF to CNF conversion

Question: So i tried to convert this
(ABCD+AB¬C¬D+¬A¬CD+¬AC¬D+¬B¬CD+¬BC¬D)
into CNF form but im stuck at
(¬A+¬B+¬C+D).(¬A+¬B+C+¬D).(A+¬B+C+D).(A+¬B+¬C+¬D).(A+C+D).(A+¬C+¬D).(B+C+D).(B+¬C+¬D)
and the right answer should be (¬A+¬B+¬C+D).(¬A+¬B+C+¬D).(A+C+D).(A+¬C+¬D).(B+C+D).(B+¬C+¬D).
Any help would be great.
Answer: Your answer is logically equivalent, but has some redundant terms
Your answer:
$(\neg A \lor \neg B \lor \neg C \lor D) \land (\neg A \lor \neg B \lor C \lor \neg D) \land (A \lor \neg B \lor C \lor D) \land (A \lor \neg B \lor \neg C \lor \neg D) \land (A \lor C \lor D) \land (A \lor \neg C \lor \neg D) \land (B \lor C \lor D) \land (B \lor \neg C \lor \neg D)$
The book's answer:
$(\neg A \lor \neg B \lor \neg C \lor D) \land (\neg A \lor \neg B \lor C \lor \neg D) \land (A \lor C \lor D) \land (A \lor \neg C \lor \neg D) \land (B \lor C \lor D) \land (B \lor \neg C \lor \neg D)$
Note the only difference between your answer are the two terms below:
$(A \lor \neg B \lor C \lor D) \land (A \lor \neg B \lor \neg C \lor \neg D)$
These terms are made redundant by
$(A \lor C \lor D)$ and $(A \lor \neg C \lor \neg D)$ respectively
Do you see how $(A \lor C \lor D)$ is equivalent to $(A \lor \neg B \lor C \lor D) \land (A \lor B \lor C \lor D)$? This means it is equivalent and redundant to an extra term you included.

DNF to CNF conversion


Batch Script - Query Filenames From Server Printer

Question:
Client: Windows XP
Print Server: Ubuntu via CUPS

Is there a way to query the printer queue on the server from a client to retrieve the name of the file being printed?
Code example would be something like:
FOR /F "tokens=* USEBACKQ" %%F IN (`COMMAND TO QUERY PRINT JOBS ^| FIND "FILENAME"`) DO (
SET var=%%F
)
IF NOT DEFINED var (
ECHO Print Job Does Not Exist
) ELSE (
ECHO Print Job Completed
)

Comment: Doesn't it appear in the printer properties/what's printing window, like it does for a regular Windows based printer?
Comment: Keep in mind I want to pull this information from a batch script (command line) and not the GUI.  I need to be able to pull the string containing the name of the file being printed to create an argument... updating the question with a sample of what the code COULD look like.
Answer: I totally forgot to update this =D.
My solution ended up being querying the local print queue on the workstation.
CSCRIPT "C:\WINDOWS\system32\PRNJOBS.vbs" -l ^| FIND /I "printed file"

Running that queries the local print jobs with their file names, then I search for the file in the output to see if it successfully made it to the print queue. This is the closest that I could come to my solution and it has been working thusfar.


Intuition for multiplying by x for independent solutions

Question: I'm taking a differential equations course, and I was wondering what the intuition behind multiplying by x to get a linearly independent solution to a 2nd order homogeneous linear ODE is. Consider the DE:
$$
y^{\prime\prime} + 4y^{\prime} + 4y = 0
$$
Evaluating the characteristic polynomial:
$$
\lambda^2 + 4\lambda + 4 = 0\\
(\lambda+2)^2=0, \lambda=-2\ \text{(double root)}\\
y=c_1e^{-2x}+c_2xe^{-2x}
$$
I'm trying to get a hold of why multiplying by $x$ makes sense. I understand the  derivation via variation of coefficients, but I don't get why it makes sense. Why does multiplying a solution to a differential equation by $x$ to get another solution that is linearly independent make sense? Is it a guess, or is there some deeper intuition for why this makes sense in the case of a double root? Does multiplying a function of $x$ by $x$ always produce a new linearly independent function?
My teacher mentioned something about it being the first term of some taylor series expansion for $f(\epsilon)$ when nudging $x$ by $\epsilon$ in the first solution to the DE, but I didn't really understand that explanation. Is there more to that explanation?
Answer: Suppose we have the ODE
$$ y'' - (2r+\varepsilon)y' +r(r+\varepsilon) y = 0 ,  $$
of which the characteristic equation is obviously $(\lambda-r)(\lambda-r-\varepsilon)=0$, so it has general solution $Ae^{rx} + Be^{(r+\varepsilon )x}$. Suppose also that we have initial conditions $ y(0) = a $ and $ y'(0) = b $. Then, by our general solution we find
$$ A+B = a , \qquad rA+(r+\varepsilon)B = b \implies \varepsilon B = b-ra , $$
so the specific solution is
$$ y = \frac{a(r+\varepsilon)-b}{\varepsilon} e^{rx} + \frac{b-ar}{\varepsilon} e^{(r+\varepsilon)x} . $$
Written like this, it doesn't look very promising, but considering that we are interested in the limit as $\varepsilon \to 0$, let's instead collect the powers of $\varepsilon$:
$$ y =  \left( a + (b-ar)\frac{e^{\varepsilon x}-1}{\varepsilon} \right) e^{rx} . $$
Now, if we take the limit, $(e^{\varepsilon x}-1)/\varepsilon \to x$, because this is the limit in the definition of the derivative with respect to $t$ of $e^{xt}$ at $t=0$.
You may find it instructive to draw graphs of the solution for a fixed $a$ and $b$ and various small $\varepsilon$, to see the behaviour.
As for when the multiply-by-$x$ rule works for linear differential equations: let
$$ y'' + py' + qy = 0 \tag{1} $$
be solved by $y=u$ and $y=xu$. Then we have
$$ u'' + pu' + qu = 0 \qquad xu'' + 2u' + p(xu'+u) + qxu = 0 , $$
and subtracting, we find that
$$ 0 = 2u' + pu , \tag{2} $$
which is certainly necessary, but not sufficient, in the case of constant coefficients we considered above. Can we say more? Differentiating, we find
$$ 0 = 2u'' + p'u + pu' = 0 , $$
or using the differential equation,
$$ 0 = -2(pu'+qu) + p'u + pu' \implies 0 = -pu' - 2qu + p'u = p^2 u/2 - 2q u + p' u . $$
Assuming that $u \neq 0$, we therefore must have
$$ 2p' + p^2 - 4q = 0 . \tag{3} $$
Again, this is true for the constant coefficients case since in that case $p'=0$ and we are left with the discriminant of the characteristic equation. Indeed, this shows that in the case of constant coefficients, this is sufficient as well as necessary (although we knew that).
Indeed, it is sufficient in the general case, providing we assume $p$ and $q$ are sufficiently well-behaved: if $(3)$ is satisfied, $ u(x) = \exp{(\frac{1}{2}\int p(x) \, dx)}$ satisfies the differential equation $(1)$ (plugging it into $(1)$ gives $(3)$). It is easy to check that $ 2u' + pu = 0 $, and then plugging $xu(x)$ into the operator in $(1)$ gives
$$ xu'' + 2u' + p(xu'+u) + qxu = x(u''+pu'+qu) + (2u'+pu) = 0 , $$
as the $u$ we defined satisfies $(1)$ and $(2)$.
Answer: Here's an explanation as to why all solutions of the differential equation
$$\sum_{k=0}^n \binom{n}{k}(-\lambda)^{n-k} y^{(k)}=0$$
must be of the form $$c_1e^{\lambda x}+c_2xe^{\lambda x}+c_3x^2e^{\lambda x}+\cdots + c_n x^{n-1}e^{\lambda x}$$
for some constants $c_1,c_2,c_3\ldots, c_n$.
Note that the characteristic polynomial  of the above differential equation is $(x-\lambda)^n$, so hopefully this helps with your inquiry (in particular by setting $n=2$).
We first prove a short lemma: If $y$ is a $n$-times differentiable function, then
$$\frac{d^n}{dx^n}\left(ye^{-\lambda x}\right)=e^{-\lambda x}\sum_{k=0}^n\binom{n}{k}(-\lambda)^{n-k}y^{(k)}.$$
We'll prove this by induction. The base case $n=0$ is easily verifiable. Now, if we assume the statement is true for some particular $n$, then
\begin{align*}
\frac{d^{n+1}}{dx^{n+1}}\left(ye^{-\lambda x}\right)&=\frac{d}{dx}\left[e^{-\lambda x}\sum_{k=0}^n\binom{n}{k}(-\lambda)^{n-k}y^{(k)}\right]\\
&=e^{-\lambda x}\left[\sum_{k=0}^n\binom{n}{k}(-\lambda)^{n-k}y^{(k+1)}\right]+e^{-\lambda x}\left[\sum_{k=0}^n\binom{n}{k}(-\lambda)^{n+1-k}y^{(k)}\right]\\
&=e^{-\lambda x}\sum_{k=0}^{n+1}\binom{n+1}{k}(-\lambda)^{n+1-k}y^{(k)}.
\end{align*}
Hence, the lemma is proven. If $y$ satisfies the differential equation above, then by the lemma we have
$$\frac{d^n}{dx^n}\left(ye^{-\lambda x}\right)=0.$$
Since the only functions whose $n$th derivative is zero are polynomials of degree strictly less than $n$, it follows that
$$ye^{-\lambda x}=c_1+c_2x+c_3x^2+\cdots + c_n x^{n-1}$$
for some constants $c_1,c_2,c_3\ldots, c_n$. This yields the desired result.


Drupal Ubercart to Commerce Migration: any solution for uc_file (file downloads)?

Question: I've recently upgraded a Drupal site from 6 to 7, and now want to migrate from Ubercart to Drupal commerce. The Ubercart upgrade has been completed already just in case.
Ubercart has a module called uc_file that allows files to be assigned to products.
The module has three tables:
1. uc_files: contains the file id (fid) and the name of the file
2. uc_file_products: contains fid, feature product id (fpid), something called pfid, and some other fields for the description, etc.
3. uc_file_users: contains fid, uid, fpid, fuid (?), and other fields.
Has anyone migrated these tables, and if so, what approach did you take? The most important data I need to migrate is the actual line item purchases, so the users will be able to access their purchased downloads.
I have commerce_migrate_ubercart installed, but it doesn't appear to handle the Ubercart File Downloads, which is the entire store.
Answer: Well as it turns out, no code was needed.
In case anyone runs into this issue, here's what you need to do:

Download the dev version of commerce_migrate_ubercart and apply the payment patch if it hasn't already been applied.
Disable all Ubercart modules
Carefully follow the instructions in the README.txt file. You'll
want to migrate every available table starting with products, and
working your way to payments (transactions). Save the database early
and often along the way.
Download/configure commerce_file and commerce_license.
Be sure you are able to go end-to-end with your cart and download privileged files in your user/uid/my_files tab.
Download/enable commerce_vbo_views and views_bulk_operations
Edit the rule "Activates licenses of an order", and add the event "After updating an existing commerce order".
Go to your Store's order list page, select the desired order states
that have completed orders, then select all rows in the table, then
click modify order values, and just set the exact same state as
selected before. What we're after here is to force a re-save of the
order to trigger our rule.
You should now be able to view a user and click the "Files" tab to see their downloads assuming permissions are correctly set and the migration worked.
Edit the rule again to remove the added event and you're done.

Easy peasy :)


EarlyStopping in combination with GridSearchCV für hyperparameter tuning?

Question: I want to find the optimal hyperparameter (dropout rate, learning rate, number of epochs) for training an CNN-architecture.
Does it make sense to integrate EarlyStopping already in GridSearchCV? Or should EarlyStopping only be used for the final model?
Can you make recommendations?
Answer: You likely assume that early stopping would influence the results. It is possible that early stopping would interact with other parameters (would act better for some combinations of parameters, then for others). So it makes sense to integrate early stopping into grid search. Moreover, it would make training faster, so grid search will take faster. Additionally, you may treat enabling early stopping, or not, as additional parameter in search space and actually check if it helps.
Comment: @CodeNow sorry I'm not familiar with keras sklearn API.
Comment: Perhaps you still allow me the question of whether you know a sample code to which I can orient myself (unfortunately I´m still a beginner in ML)? Because in https://machinelearningmastery.com/grid-search-hyperparameters-deep-learning-models-python-keras/ is GridSearchCV unfortunately demonstrated only without EarlyStopping.


Read a file one char at a time until newline character using read(2) and write(2)

Question: I'm trying to a read text file until the first newline character is found and print out the first line up until that first newline character.
I am using the read(2) and write(2) unix system calls to complete this. Do not want to use getline() function for this. I wanted to go through each character in the buffer array and print the elements out until I get to the first newline character in the first line. Also trying to figure out to do the same for the second line and also the last line in the text file. Each line will be displayed in their own separate program if that make sense.
Here is what I have so far for the first line program....
#define BUFFSIZE 4
int main(int argc, char** argv)
{
   if(argc != 2)
   {
      cout << argv[0] << "need filename" << endl;
      exit(4);
   }

 int n = 0;
 char buf[BUFFSIZE];
 int fd = open(argv[1], O_RDONLY);

 while((n = read(fd, buf, BUFFSIZE))>0)
 {
    if[buf[0] != '\n')
     {
        cout << buf[0];
     }
    if[buf[1] != '\n')
     {
        cout << buf[1];
     }
    if[buf[2] != '\n')
     {
        cout << buf[2];
     }
    if[buf[3] != '\n')
     {
        cout << buf[3];
     }
 }
close(fd);
}

I am taking a input file as a command argument. I have several lines in the input text file that goes like...
Purple is a cool color
Blue is the color of the sky
Green is the color of the grass

When I run this program, it prints all the lines into one long line in command window and with no space in between each sentence.
Output
Purple is a cool colorBlue is the color of the skyGreen is the color of the grass

I want to be able to print the first line only when the first newline character is found and I am having a hard time figuring out how to print each element in the array until the newline character is read.
Comment: So? Your code literally prints each character except if it's a newline. Hence, your output consists of everything on one line. So, what exactly is your question? P.S. If the file's size is not an even multiple of four, the output will be slightly wrong.
Comment: My question is that how do I print each element in the array until the first newline character is found in the first line? My program is printing all the lines into one line in output.
Comment: I was trying to read blocks of data at one time.
Comment: Why do you read in `4` chars at a time? Why not just read in `1` char at a time?
Comment: @Nicole Internally the `read()` function will read the file in blocks for efficiency. I just thought it would simplify the program reading just one char at a time.
Answer: The way your program is formed you are literally telling it to print everything until a newline char is found, and when one is found to omit it.
Introduce an else for those if != where you print whatever it is that you want in place of newline (e.g. 2x newline).
The listing also seems malformed (conditionals followed by a square bracket), but I assume that's typos. It's always best to post code that will compile when asking for help.
Also as posted in comments you are reading 4-at-a-time with no conditionals, you will eventually get into a crash or garbage input.


Slow writes for VM in Xen?

Question: I have a server running Xen and a couple of VMs. I'm trying to set up a RAID array that's dedicated to one of the VMs in particular, which will be used for various storage-intensive purposes. Currently, I'm having a very odd performance drop when writing from the domU, which is a Debian PV guest with plenty of vcpus and memory.
Right now, the setup is that I have three 3TB WD Red hard drives arranged in a (software) RAID 5 array on the dom0. Currently, the dom0 is exposing the /dev/mdX block as /dev/xvdb on the virtual machine (the VM has /dev/xvda from a LVM volume). The relevant bit of the xen config:
disk = [ <LVM stuff for xvda>, 'phy:/dev/md0,xvdb,w' ]

The /dev/md0 has a ext4 filesystem on it, with the noatime, nodiratime options in use. When I do a speed test on the filesystem from the domU, I get something like this:
# dd if=/dev/zero of=a_file bs=1M count=1024 conv=fsync
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 16.2694 s, 66.0 MB/s
# dd if=some_other_noncached_file of=/dev/null bs=1M
4096+0 records in
4096+0 records out
4294967296 bytes (4.3 GB) copied, 26.379 s, 163 MB/s

However, in dom0, I get:
# dd if=/dev/zero of=a_file_somewhere bs=1M count=1024 conv=fsync
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 6.80677 s, 158 MB/s

... which, clearly, is a very large speed difference. While 66 MB/s should be good enough for now, I'd really appreciate it if anyone can come up with an explanation for why I'm losing 60% of my write performance? I'd be expectant of 10% or so, but not 60%.
It's not dom0 resource starvation, because I've been giving it significantly more resources than it should require and the problem still occurs. It's not domU resource starvation, either, and I've pinned CPUs to stay on the same NUMA node, and the same thing results.
Here's some maybe-relevant things from xl info:
host                   : <hostname>
release                : 3.16.0-4-amd64
version                : #1 SMP Debian 3.16.7-ckt11-1+deb8u3 (2015-08-04)
machine                : x86_64
nr_cpus                : 24
max_cpu_id             : 63
nr_nodes               : 2
cores_per_socket       : 6
threads_per_core       : 2
cpu_mhz                : 2660
hw_caps                : bfebfbff:2c100800:00000000:00003f00:029ee3ff:00000000:00000001:00000000
virt_caps              : hvm
total_memory           : 24573
free_memory            : 12011
sharing_freed_memory   : 0
sharing_used_memory    : 0
outstanding_claims     : 0
free_cpus              : 0
xen_major              : 4
xen_minor              : 4
xen_extra              : .1
xen_version            : 4.4.1
xen_caps               : xen-3.0-x86_64 xen-3.0-x86_32p hvm-3.0-x86_32 hvm-3.0-x86_32p hvm-3.0-x86_64
xen_scheduler          : credit
xen_pagesize           : 4096
platform_params        : virt_start=0xffff800000000000
xen_changeset          :
xen_commandline        : placeholder dom0_mem=8192M dom0_max_vcpus=8 dom0_vcpus_pin
cc_compiler            : gcc (Debian 4.9.2-10) 4.9.2
cc_compile_by          : ultrotter
cc_compile_domain      : debian.org
cc_compile_date        : Thu Jun 11 18:24:17 EEST 2015
xend_config_format     : 4

If there's anything else that might be useful, please let me know and I'll update the question.
Thanks!
Answer: I've seen near native disk performance with PV guests, with the MD work being done at both the dom0 and domU levels.
If you can, try setting up guest as paravirtualized (PV) instead of a hardware virtual machine (HVM).
Also, for either PV or HVM, try assigning the whole drives to the guest and having it handle the RAID stuff (MD).
See what works best for you.
Comment: I'd say within 15% difference would be a reasonable goal.
Comment: Good to know that I should be shooting for close to 100%. I'll try moving the RAID into the domU instead of the dom0. Also, forgot to mention, it's currently set up to use PV; I'll update the original question.
Answer: Some more investigation turned up the fact that this is not a unique issue (see for example https://bugzilla.redhat.com/show_bug.cgi?id=500145). Following some suggestions there, further investigation with iostat showed that the average write size on the domU was somewhere lower than 128KiB, while on the dom0 was about 512KiB (found by dividing wkB/s / w/s).
Some Googling turned up the fact that this is a known issue, with a patch submitted back in December to "fix" the problem by raising the default value of the kernel module parameter xen_blkfront.max from 32 to 128. By overriding the default value myself (in the GRUB configuration for the VM), the write speed on the domU jumped from about 65MiB/s to about 115MiB/s, which is enough of a bonus that I'm not going to look too much further.
So while there's still more territory to cover (from 115 MiB/s to the dom0 speed of 150 MiB/s), the major annoying part has been fixed, so I'm marking this as the solution (once I can). I'll update with any more tweaks or whatnot that I find which provide significant impact.
Comment: Can you please post your complete grub command and where to apply it?

Slow writes for VM in Xen?