No Python, há algum jeito além de `numpy` e `float('nan')` de obter a constante especial `nan`?

Question: Estive lendo o site do Underhanded C Contest, em que o objetivo é escrever código sutilmente malicioso, que pareça normal a primeira vista. Uma das técnicas comuns mencionadas foi o uso de not a number, ou nan, constante que tem algumas propriedades especiais; notavelmente, qualquer tipo de comparação com nan resulta em False.
Pensando em uma prova de conceito em Python, cheguei no seguinte:
def maior_que_10():
    entrada = input('Digite um número: ')
    try:
        entrada_float = float(entrada)
    except ValueError:
        print('Erro!')
        return
    if entrada_float > 10:
        print('Maior que 10!')
        return
    elif entrada_float <= 10:
        print('Não é maior que 10!')
        return

    print('Inesperado!')

while True:
    maior_que_10()

A função corretamente trata de valores numéricos inválidos emitindo um erro e ao olhar desatento, parece nunca chegar ao print('Inesperado!') porque verifica > 10 e <= 10, mas ao ter a entrada "nan" executa a última linha:
Digite um número: 11
Maior que 10!
Digite um número: 9
Não é maior que 10!
Digite um número: 10
Não é maior que 10!
Digite um número: foobar
Erro!
Digite um número: nan
Inesperado!

Teoricamente, em código menos trivial, poderia esconder-se código malicioso depois dos dois if. Isso, porém, depende de se ter uma entrada do usuário passada pro float.
Existe alguma operação entre variáveis que gere um nan de outro modo?
Pensei em divisão por zero ou raiz de número negativo, mas resultam em exceções, e não nan:
>>> math.sqrt(-1)
 ValueError: math domain error
>>> 1/0
 ZeroDivisionError: division by zero

Answer: (* relendo a questão inteira, vi que escrevi uma resposta extensa sobre como verificar uma entrada de ponto decimal, mas que não responde bem a sua pergunta específica - desculpe. Vou manter a resposta por que pode ajudar iniciantes que caiam aqui por conta do título da pergunta)
Em versões mais recentes do Python é possível fazer from math import nan - isso coloca no namespace a variável nan que contém um número nan.
Em versões mais antigas (anteriores à 3.5 do Python), o recomendado era colocar no seu código:
nan = float('nan')

mesmo (ou usar a expressão float('nan') diretamente.
Além disso é importante ter em mente ao lidar com NaN's que um valor NaN nunca é igual a outro, quando comparado com == (nem igual a si mesmo). A melhor forma de saber se um valor é um NaN é usar a função isnan do módulo math:
from math import nan, isnam

isnan(nan)

imprime True.
Dito isso sobre NaNs - há mais coisas a considerar sobre o uso de float direto em cima de uma string que o usuário digita.  Em particular, valores infinitos podem ser expressos com float('inf') (e infinidade negativa com "-inf"), e também são aceitos números com notação científica, em que um fator expoente de "10" pode ser adicionado ao número depois da letra "e":
In [95]: float("1e3")
Out[95]: 1000.0

Então, se você quer relamente limitar a entrada a números positivos ou negativos, com pontos decimais, é melhor fazer o "parsing" dos mesmos com mais cuidado do que simplesmente chamar float(entrada).
Em geral, quando falamos em "fazer o parse", muitas pessoas pensam primeiro em expressões regulares. Eu considero que expressões regulares são de díficil leitura e manutenção, e as pessoas tendem a colocar expressões simples, que não correspondem à todas as possibilidades de dados.
verificando o dado digitado com expressões regulares:
Python é uma boa linguagem para expressões regulares por que felizmente não inventaram de misturar as mesmas com a intaxe da linguagem - você chama funções normaizinhas e passa uma string com a expressão regular que quer comparar com o seu texto - há várias funções no módulo re de expressões regulares - por exemplo para "encontrar todas as ocorrências" (re.findall) ou substituir (re.sub). Nesse caso, queremos simplesmente ver se uma expressão casa com a entrada do usuário.
E na pressa alguém poderia pensar "quero verse o usuário digitou um ou mais dígitos, seguido de um ponto opcional, seguido de um ou mais dígitos" - essa expressão pode ser escrita como "[0-9]+\.?[0-9]+"  - basta olhar pra isso e ver que não está bom: e se o usuário digitar um sinal de "-"? E se houver apenas um dígito? (a segunda parte espera mais um dígito depois do ponto - apesar do ponto ser opcional) - resultado - enquanto que essa expressão pode casar com "11", "23.2", "0.1", não vai casar com "1", "-1", ".23", etc...
PAra encurtar a história, a expressão regular que verifica um número decimal, com sinal opcional, com pelo menos um dígito válido, ou nenhum dígito se houver ponto decimal, e se houver ponto decimal pelo menos um dígito depois dele é:
c = r"-?(?:[0-9]+|(?=\.))(?:\.[0-9]+)?$"

(A documentação de regexps em Python está aqui - https://docs.python.org/3/library/re.html )
E você poderia fazer no seu código:
import re

def maior_que_10():
    entrada = input('Digite um número: ')
    if not re.match(r"-?(?:[0-9]+|(?=\.))(?:\.[0-9]+)?$", entrada):
        print('Erro!')
        return
    entrada_float = float(entrada)
    ...

Verificação da entrada com código Python
Então, em nome da legibilidade, e saber o que se está fazendo, pode valer a pena usar as funçẽos de manipula~ao de string do Python: split, find,
 count,  isdigit  para fazer uma função que verifica se uma string é um decimal bem formatado antes de tentar converte-lo em float.
Dá pra fazer algo do tipo:
verifica_decimal(text):
   if not text:  # string vazia
      return False
   filtered = text.replace('-', '').replace('.', '')
   if not filtered.isdigit(): # há caracteres que não são nem dígito nem - nem .
       return False
   if '-' in text[1:]: # sinal 'perdido' no meio do número.
       return False
   if text.count('.') > 1 or text[-1] == '.': # Mais de um '.', ou '.' na última casa
       return False
   return True

def maior_que_10():
    entrada = input('Digite um número: ')
    if not verifica_decimal(entrada):
        print('Erro!')
        return
    entrada_float = float(entrada)
    ...

Answer:
Existe alguma operação entre variáveis que gere um nan de outro modo?

No final da documentação do módulo math do Python você encontra:

A NaN will not be returned from any of the functions above unless one
  or more of the input arguments was a NaN;

Ou seja - em geral não. Mas é possível você chegar a um NaN, por exemplo se tentar subrair float.inf de float.inf - só que nesse caso seu problema passa a ser gerar o  float.inf:
In [100]: a = float("inf")

In [101]: a - a
Out[101]: nan

Uma outra forma é escrever é anotar a forma binária de um "nan" como um objeto do tpo bytes, ou um número inteiro - e usar o módulo "struct" para converter esses bytes de volta num ponto flutuante, que então será um NaN:
 struct.unpack("d", struct.pack("Q", ((2 ** 12 - 1) << 52) + 1))[0]

Ou usando ctypes:
In [199]: import ctypes

In [200]: class A(ctypes.Union):
     ...:     _fields_ = [('i', ctypes.c_uint64), ('f', ctypes.c_double)]
     ...:

In [202]: A(i=((2 ** 12 - 1) << 52) + 1).f
Out[202]: nan


How to create VectorDrawable with gradient and multiple Colors with Jetpack Compose?

Question: This is a share your knowledge, Q&A-style question inspired from this question but it enhances the answer with gradient, fill/empty animation and shimmer effect for VectorDrawables with aim as in gif below

First drawable
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24"
    android:tint="?attr/colorControlNormal">
  <path
      android:fillColor="#EC407A"
      android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"/>
</vector>

Clock
<
vector
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:width="24dp"
  android:height="24dp"
  android:tint="?attr/colorControlNormal"
  android:viewportHeight="24"
  android:viewportWidth="24">
  <group
    android:name="button"
    android:translateX="12"
    android:translateY="12">
    <group
      android:name="button_pivot"
      android:translateX="-12"
      android:translateY="-12">
      <group
        android:name="right_button"
        android:translateX="19.0722"
        android:translateY="4.5758">
        <path
          android:name="path_1"
          android:fillAlpha="1"
          android:fillColor="@android:color/white"
          android:pathData="M 2.9409942627,1.16249084473 c 0,0 -4.59599304199,-3.85699462891 -4.59599304199,-3.85699462891 c 0,0 -1.2859954834,1.53300476074 -1.2859954834,1.53300476074 c 0,0 4.59599304199,3.85600280762 4.59599304199,3.85600280762 c 0,0 1.2859954834,-1.53201293945 1.2859954834,-1.53201293945 Z"/>
      </group>
      <group
        android:name="left_button"
        android:translateX="4.9262"
        android:translateY="4.5729">
        <path
          android:name="left_button_path"
          android:fillAlpha="1"
          android:fillColor="@android:color/white"
          android:pathData="M 2.9409942627,-1.16250610352 c 0,0 -1.2859954834,-1.53199768066 -1.2859954834,-1.53199768066 c 0,0 -4.59599304199,3.85600280762 -4.59599304199,3.85600280762 c 0,0 1.2859954834,1.53300476074 1.2859954834,1.53300476074 c 0,0 4.59599304199,-3.8570098877 4.59599304199,-3.8570098877 Z"/>
      </group>
    </group>
  </group>
  <group
    android:name="alarm"
    android:translateX="12"
    android:translateY="12">
    <group
      android:name="alarm_pivot"
      android:translateX="-12"
      android:translateY="-12">
      <group
        android:name="alarm_hands"
        android:translateX="13.75"
        android:translateY="12.4473">
        <path
          android:name="alarm_hands_path"
          android:fillAlpha="1"
          android:fillColor="@android:color/white"
          android:pathData="M -1.25,-4.42700195312 c 0,0 -1.5,0 -1.5,0 c 0,0 0,6.0 0,6.0 c 0,0 4.74699401855,2.85400390625 4.74699401855,2.85400390625 c 0,0 0.753005981445,-1.23199462891 0.753005981445,-1.23199462891 c 0,0 -4.0,-2.37200927734 -4.0,-2.37200927734 c 0,0 0,-5.25 0,-5.25 Z"/>
      </group>
      <group
        android:name="alarm_body"
        android:translateX="12"
        android:translateY="13.0203">
        <path
          android:name="alarm_outline_path"
          android:fillAlpha="1"
          android:fillColor="@android:color/white"
          android:pathData="M -0.0050048828125,-9 c -4.97399902344,0 -8.99499511719,4.0299987793 -8.99499511719,9 c 0,4.9700012207 4.02099609375,9 8.99499511719,9 c 4.97399902344,0 9.00500488281,-4.0299987793 9.00500488281,-9 c 0,-4.9700012207 -4.03100585938,-9 -9.00500488281,-9 Z M 0,7 c -3.86700439453,0 -7,-3.13400268555 -7,-7 c 0,-3.86599731445 3.13299560547,-7 7,-7 c 3.86700439453,0 7,3.13400268555 7,7 c 0,3.86599731445 -3.13299560547,7 -7,7 Z"/>
      </group>
    </group>
  </group>
</vector>

Answer: As in the answer in the link what is basically done is to create a destination rectangle with blendMode = BlendMode.SrcIn and update this rectangle with touch or animation.
First one is done by drawing a rectangle that covers half of the height of the Icon
@Composable
private fun ClipIconSample() {
    val vectorRes2: Painter = painterResource(id = R.drawable.vd_dashboard_active)
    Icon(
        vectorRes2,
        modifier = Modifier
            .drawWithContent {
                val height = size.height

                with(drawContext.canvas.nativeCanvas) {
                    val checkPoint = saveLayer(null, null)

                    // Destination
                    drawContent()

                    // Source
                    drawRect(
                        Color(0xffEC407A),
                        topLeft = Offset(0f, height / 2),
                        size = Size(size.width, size.height / 2),
                        blendMode = BlendMode.SrcIn

                    )

                    restoreToCount(checkPoint)

                }
            }
            .size(100.dp),
        contentDescription = null
    )
}

For the second one replace color with Brush as
@Composable
private fun GradientClipIconSample() {
    val vectorRes2: Painter = painterResource(id = R.drawable.vd_dashboard_active)
    Icon(
        vectorRes2,
        modifier = Modifier
            .drawWithContent {
                with(drawContext.canvas.nativeCanvas) {
                    val checkPoint = saveLayer(null, null)

                    // Destination
                    drawContent()

                    // Source
                    drawRect(
                        brush = Brush.sweepGradient(
                            colors = listOf(
                                Color.Green,
                                Color.Cyan,
                                Color.Red,
                                Color.Blue,
                                Color.Yellow,
                                Color.Magenta,
                            )
                        ),
                        blendMode = BlendMode.SrcIn
                    )

                    restoreToCount(checkPoint)

                }
            }
            .size(100.dp),
        contentDescription = null
    )
}

Third one changes targetValue on touch as
var targetValue by remember {
    mutableStateOf(0f)
}
val progress by animateFloatAsState(
    targetValue = targetValue,
    animationSpec = tween(1000)
)

Full implementation
@Composable
private fun FillIconSample() {
    val vectorRes2: Painter = painterResource(id = R.drawable.vd_dashboard_active)
    var targetValue by remember {
        mutableStateOf(0f)
    }
    val progress by animateFloatAsState(
        targetValue = targetValue,
        animationSpec = tween(1000)
    )
    Icon(
        vectorRes2,
        modifier = Modifier
            .clickable(
                interactionSource = MutableInteractionSource(),
                indication = null,
            ) {
                targetValue = if (targetValue == 0f) {
                    1f
                } else {
                    0f
                }
            }
            .drawWithContent {
                val height = size.height * progress

                with(drawContext.canvas.nativeCanvas) {
                    val checkPoint = saveLayer(null, null)

                    val totalHeight = size.height
                    val filledHeight = totalHeight * progress

                    // Destination
                    drawContent()

                    // Source
                    drawRect(
                        Color(0xffEC407A),
                        topLeft = Offset(0f, totalHeight - height),
                        size = Size(size.width, height),
                        blendMode = BlendMode.SrcIn

                    )

                    restoreToCount(checkPoint)

                }
            }
            .size(100.dp),
        contentDescription = null
    )
}

Shimmer effect is a gradient where progress is changed as
@Composable
private fun ShimmerIconSample() {

    val vectorRes2: Painter = painterResource(id = R.drawable.vd_clock_alarm)

    val transition = rememberInfiniteTransition()

    val progress by transition.animateFloat(
        initialValue = 0f,
        targetValue = 1f,
        animationSpec = infiniteRepeatable(
            animation = tween(1500, easing = LinearEasing),
            repeatMode = RepeatMode.Restart
        )
    )

    Icon(
        vectorRes2,
        modifier = Modifier
            .drawWithContent {
                with(drawContext.canvas.nativeCanvas) {
                    val checkPoint = saveLayer(null, null)
                    val canvasWidth = size.width

                    val brush = Brush.horizontalGradient(
                        colors = listOf(
                            Color(0xff616161),
                            Color.White,
                            Color(0xff9E9E9E),
                            Color(0xff616161),
                        ),
                        startX = canvasWidth * progress,
                        endX = canvasWidth * progress * 2f
                    )

                    // Destination
                    drawContent()

                    // Source
                    drawRect(
                        brush = brush,
                        blendMode = BlendMode.SrcIn
                    )

                    restoreToCount(checkPoint)

                }
            }
            .size(100.dp),
        contentDescription = null
    )
}

Comment: you are genius. It's pretty useful. Thanks for sharing the knowledge.
Comment: @VivekModi i hope it helps, i might post a wave animation next
Comment: Ohh nice to hear that. I am very eager to see that cool stuff.


Driving a Novation UltraNova synth via a Yahama synth guitar (G10 ?)

Question: Would it be possible to play on a Yahama syntheziser guitar, and get the sounds and instruments to actually come out of the sound bank of the Novation UltraNovation synthesizer ?
(please feel free to add HTTP links to the relevant pics / instrument websites to enrich this question)
Answer: The UltraNovation can be set up to trigger on the MIDI input as well as, or instead of the keyboard so you can assign any patches you like to the G10 as a trigger.
The G10 is designed this way, so any MIDI synth can provide the sound - the advantage is that you can do all the usual things you would do on a guitar - whammy, string bends etc
Answer: Yes, that is what guitar controllers with MIDI are for. You just connect one to the other with a MIDI cable.


Need to disable Auto Preview in views for local development in settings.php

Question: Can I add a setting in settings.php to disable Auto Preview in views. I know this can be changed under /views/settings/. But I don't want this configuration added to Git. I only want this change on my local site. Something like this.
<code>$config['system??']['foo']['bar'] = FALSE;
</code>
I unchecked 'Automatically update preview on changes' in Views settings and exported the changes. The result is that it updated  'views.settings' Below is part of views.settings.yml
<code>     ui:
      show:
        additional_queries: false
        advanced_column: true
        master_display: true
        performance_statistics: false
        preview_information: true
        sql_query:
          enabled: true
          where: below
        display_embed: false
      always_live_preview: false
      exposed_filter_any_label: old_any
</code>
The setting may look something like this.
<code>$config['views.settings']['ui']['always_live_preview'] = FALSE;
</code>
Comment: Did you set it off and export to see what changed?
Comment: Good idea, I did not. I will edit my post and add this information
Answer: This should do it, make sure you clear cache:
<code>$config['views.settings']['ui']['always_live_preview'] = 'false';
</code>


Como Detecto o click no angular e passo a informação para o electron?

Question: Estou trabalhando em uma aplicação Angular2 que executa junto ao Electron, preciso que ao clicar em um link específico do angular, a aplicação Electron finalize. Como faço esse tipo de comunicação, para que o Electron entenda um comando vindo do angular?
Comment: Adicone o seu código com um exemplo verificável do problema para que o comportamento possa ser reproduzido.
Comment: Não tenho tempo de postar uma resposta, mas se souber inglês, [leia este trecho do manual do electron](https://electronjs.org/docs/tutorial/application-architecture).
Answer: Você terá que usar o módulo ipcRenderer do Electron. Independente do framework JavaScript que você estiver usando a comunicação entre o navegador Chromiun e o Node no Electron é feita por envio de mensagem para os processos.
Então, o que acontece é o seguinte:
voce tem o Chromiun que é um navegador e tem o Node que é o backend de uma aplicação Electron. Se você quer pegar uma informação do navegador e manipular em seu main.js do Electron você precisa usar o ipcRenderer.
Vou te mostrar um exemplo, amigo:
Imagine que eu tenha um form e um botão e queira pegar o nome do arquivo selecionado por uma input do tipo file. Veja abaixo como eu teria que fazer no HTML de exibição da aplicação:

Veja que o primeiro parâmetro do método .send do módulo ipcRenderer é o nome do identificador da sua mensagem. No seu main.js você pode pegar a informação da seguinte forma:
const {ipcMain} = electron;

ipcMain.on('obterNomeArquivo', (event, path)=> {
    console.log(path)
});

Espero ter te ajudado, amigo! Qualquer coisa só comunicar! Para finalizar, você pode ver a documentação do ipcRenderer, aqui:
https://www.electronjs.org/docs/api/ipc-renderer


How to create a new role with authenticated user permissions

Question: I want to create a new role. New role should have all the permissions of authenticated user as well as the some new permissions.
I used the following code,
<code>$role = new stdClass();
$role->name = 'New role';

// load the authenticated user role
$auth_role = user_role_load(DRUPAL_AUTHENTICATED_RID);
$role->weight = $auth_role->weight + 1;

user_role_save($role);
</code>
and I will assign the permissions using user_role_change_permissions() or user_role_grant_permissions()
Now I want to assign the authenticated user permissions to this new role. How to achieve this?
Answer: Got it. following code will achieve the this functionality,
<code>$role = new stdClass();
$role->name = 'New role';
// load the authenticated user role
$auth_role = user_role_load(DRUPAL_AUTHENTICATED_RID);
// Give the weight more than authenticate user, so that
// New role will have all the authenticated user permissions
// by default.
$role->weight = $auth_role->weight + 1;
user_role_save($role);
$auth_permissions = user_role_permissions(array(DRUPAL_AUTHENTICATED_RID => 'authenticated users'));
$auth_permissions += array('other permissions');
// Grant permissions to our 'New role' role
user_role_grant_permissions($role->rid, array_keys($auth_permissions[DRUPAL_AUTHENTICATED_RID]));
</code>
Comment: The code didn't work for me, but changing the second $auth_permissions line to this fixed it:   $auth_permissions[DRUPAL_AUTHENTICATED_RID] += array('other permissions' => 1);
Answer: Here is code that will assign new role to user:
<code>global $user;
$key = array_search('your role', $user->roles);
if ($key == FALSE) {
  $roles = user_roles(TRUE);
  $rid = array_search('your role', $roles);
  if ($rid != FALSE) {
    /* change role in current session*/
    $user->roles[$rid] = 'your role';
    /*save role in db*/
    $new_role[$rid] = 'your role';
    user_save($user, array('roles' => $new_role));
  }
}
</code>
Comment: -1 This is not what I want. I'm talking about creating a new user role, not assigning a new role to the user.
Comment: Your question sounds "Now I want to assign the authenticated user permissions to this new role. How to achieve this?" So i think you want assign this new role to user.


Setting a maximum server load for apache2

Question: Is there a way to configure apache2 to stop serving requests if a specific value of server load (like the value that you see when using htop) is reached? Ideally a predefined static page should be served in such case?
If this doesn't make sense then what is the correct way of letting the server rest if it's getting overloaded? Except for correctly configuring all the values in apache2 configuration of course. I need a temporary rescue solution.
Answer: Sounds like you're running into scalability issues. Unfortunately, I don't think Apache has this mechanism. The correct way I've seen it done is by having a reverse proxy of sorts (nginx or even HAProxy) sit in front of Apache and check the response times. If it's too slow, it can display a "sorry" page and let Apache cool off.
Hope this guides you in the right direction.
Answer: You can see if monit can help you. It has a load trigger that you can use to restart servers, run custom scripts and etc... Maybe you can create a custom script to change apache config files.
Varnish has a Saint mode with Grace time, it will show the last version of the cache if a backend takes too long to respond, maybe it is better than just showing a "sorry" page. OTOH, varnish is not trivial to configure.


Angular Build For MultiProject Workspace

Question: I have a angular project workspace which consist of multiple projects and library.
...
e2e
projects
   authentication-project
   vendor-project
   customer-project
   shared-library
angular.json
package.json
tsconfig.json
...

When I build project I get a get coupled of hashed file with minified content.
Is their a way i can tell cli to to bundle the project build with specific project-prefix with hash file names so that only specific project bundle needs to replaced without impacting other project bundle files.
Answer: You can always run the build command with specific project name.
Example: ng build vendor-project.
Editing the angular.json will give you more control on the hash names and other options. If you want more control then you can always write your own npm scripts or  eject the project and access the underlying webpack config .
Also, have a look at Nx workspaces if required. https://nx.dev/
Comment: They way I am trying put the build such that....
Their is one master project which is being importing features from other sub-projects...A build to this project is complete clean build but for a hot deployment a specific project needs to build and bundle file for that specific project should be get replaced on server so that existing features should not get impacted.
Comment: Eject can might give possibility..something to be explore...but is their any easy and straight way to achieve it?


Why do agents have a pool of threads?

Question: In clojure documentation I see that agent use a pool of thread to process data. But I read that (always in documentation) :

The actions of all Agents get interleaved amongst threads in a thread
  pool. At any point in time, at most one action for each Agent is being
  executed.

Why does an agent have a pool of thread and not a single thread to process the "queue" of sended function ?
Thanks.
Answer: An agent does not 'have a pool of threads'. There are two thread pools (for send and send-off actions), to which agent actions get assigned.
This design decision is the optimal choice for CPU-bound tasks, and a best-effort approach for IO-bound tasks.
For the latter case, providing your own pool with send-via will be the optimal choice (assuming you know what you're doing).

Why do agents have a pool of threads?


Views contexual filter based on nid (not from URL)

Question: I am using Viewfield module to display taxonomy terms of a node is referencing to. I am not using the default taxonomy field available in views because I want more freedom on manipulating the rendered taxonomy terms.
When I am in the node page I use contextual filters and I provide the default value from the URL. Easy. But...
For listing nodes of a taxonomy term I use views also and I want the same functionality in there.
The only way I can do this is by providing the default value from the nid of every node (row). So how can I get the nid as a contextual filter in such case?
Thanks
Answer: Use the Views Field View module and add Content: Nid as a field and check mark ☑ exclude from display.
Add Global: View a field and pass [%nid] token/replacement pattern as an argument as contextual filter.
Note: make sure it's arranged above your View field view so you have access to the [%nid] replacement pattern/token.
Comment: Nop...doesn't do the trick and what I did is added nid and viewfield in my fields and put the token of nid to the contextual filters as a fixed value. See this [link]http://i.imgur.com/PLE5zRU.png
Maybe is the fixed value the issue?
Comment: @pierostz updated my answer, you should use this other module.
Comment: Well getting closer. I have created a "child" view as described in the module's readme file. I have added this view as a "Global : View" and put it under the nid field. In the Global: View settings I have added [!nid] as a contextual filter (I also tried [nid] as you say) while choosing the correct child view, but I get the full list of my taxonomy terms. It seems like the view isn't using my contextual filter. Also see image [link]http://imgur.com/fc9TtDY
Comment: @pierostz try with `[%nid]`
Comment: @pierostz also, for the view you're adding as a field, make sure the contextual filter is [like this](http://i.stack.imgur.com/7SMOB.jpg)
Comment: and BOOM!!! Thanks No Sssweat got the point now and I have managed it. Ashame I don't got reputation so I can upvote.


Are World Records set in the Tokyo 2020 Olympics not considered Olympic records?

Question: As the title is asking, why is this?  Why aren't records set in the Tokyo 2020 Olympics considered Olympic records?  This IS the Olympics that we're watching on tv right now and that's going on in Tokyo right now.
I saw this in the Men's 100m Fly with the gold medal winner setting a World Record, and not an Olympic record.
This also happened in the Women's 200m Free Relay.  The line on tv showed WR and not OR as we were following along during the race to see who won.
So again, why are these not considered Olympic records and only considered as World Records?
Answer: Those are Olympic records. They're both World Records (WRs) and the Olympics Records (ORs). A WR set in the Olympics is a WR and an OR.
WRs are not bounded to the Olympics and so can happen at other events too, and so WRs are always better than or equal to ORs. So not all ORs are WR. Since WRs are harder to come by, when an Olympics performance breaks the world record, WR is displayed. But in reality, it's also an OR since it happened in an Olympics.
This is not new at the Tokyo Olympics, showing WR for a world record at an Olympics is common practice. The OR (and also national record, seasonal best, and personal/team best) for such performances are implicitly implied.
Comment: The TV prompts don't unfortunately, which is what the OP is asking about cause the confusion.
Comment: The official results will usually show "WR OR" or similar.


How to interpret this "hammer on" in this guitar tab?

Question: Does anyone know how I would interpret this hammer on in the guitar tab seen in the image?

I would usually read this as "play the third string on the fourth fret, hammer on 5, hammer on 4" but this doesn't make sense in this case since you would usually hammer on higher up the neck in relation to the previous note hammered on.
Thanks in advance for any replies.
Answer: It's a hammer-on fret 4 to 5 immediately followed by a pull-off fret 5 to 4. You just pick the note once, but do a hammer-on followed by a pull-off. Hammer-ons and pull-offs both use the same slur notation (the curved line); therefore, the only way to distinguish between them, is whether you are going from a lower-to-higher or higher-to-lower fret.
And yes, you can have multiple hammer-ons and pull-offs under one slur. This hammer-on, pull-off back to the first note, would be termed a mordent in classical terminology. If you repeatedly alternate between two notes under a slur, this would be a trill:
Answer: In addition to Bob's answer, it's a slur mark. That means the three notes are played 'in one breath' - without any more than one pluck of the right hand. That's legato playing.
As Bob says, it's usually executed by plucking the first note, hammering-on the second (one fret higher), then pulling-off with the same finger that hammered-on, back to the first note pitch.
That's the usual way to play slurs on guitar. However, another way is to slide - where the first note is plucked, then the second is slid to, then the third slid back to. That way, the transition between notes is smooth, which is, after all, what slurring is supposed to do.
So, two ways in which to play a 'slur' in guitar music. Often, in tab, it's indicated, but not always - sometimes left to the discretion of the performer.
Comment: Slides are usually indicated with a straight diagonal line, so the notation is not ambiguous.
Comment: I don't think that's correct. A slur always means a hammer/pull on plucked string instruments.  A slide is always marked with a [glissando](https://en.wikipedia.org/wiki/Glissando).
Comment: Maybe you should click that link... either that or we have a serious difference of opinion about what "real music" means.
Comment: @J... - in the absence of anything except a slur mark, either is an option.
Comment: @J... - on tab, true, but on real music...


Apache virtual host pointing to wrong document root

Question: I am running Linux mint and I'm trying to setup up a virtual host with apache.
I have added the following file in /etc/apache2/sites-available/ ( it's copied from the 'default' file in that directory)
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName testsite.dev
        DocumentRoot /home/chris/Projects/web/testsite

        <Directory /home/chris/Projects/web/testsite>
            Allow from all
            AllowOverride All
            Order allow,deny
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
            AllowOverride None
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Order allow,deny
            Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

        Alias /doc/ "/usr/share/doc/"
        <Directory "/usr/share/doc/">
            Options Indexes MultiViews FollowSymLinks
            AllowOverride None
            Order deny,allow
            Deny from all
            Allow from 127.0.0.0/255.0.0.0 ::1/128
        </Directory>
    </VirtualHost>

I have also added an entry in my hosts file (/etc/hosts) which would be:
127.0.0.1    testsite.dev

I've also enabled the site and  reloaded the apache service with:
a2ensite testsite
service apache2 reload

However when I browse to http://testsite.dev it's serving pages from /var/www/ instead of /home/chris/Projects/web/testsite.
What am I doing wrong?
Comment: try replacing what you have in default with what you have in testsite and realoading. if that works, it means that what is in the default vhost file is winning for some reason over your new site when receiving a request for the testsite.dev
Answer: A few things I would check:

Ensure that you have a "NameVirtualHost ***:80" in your config. If the "*:80" is different it may conflict with the value in "VirtualHost" (in general there are less issues if they are the same).
Ensure you don't have other "VirtualHost" defined somewhere that may be conflicting with this one (I assume everything in "sites-available" as well as any other Apache config file).
Check the error log to make sure nothing "bad" is happening. Enabling and checking the access log may also be useful.
Double check that the files/content in the two directories is what you think it is. If you have them somehow mixed up it could be working as expected.
Stop and start Apache service. In theory reloading should work but just in case (it wouldn't be the first time I've seen reloading fail but stopping/starting work).

If you run through all this and still can't seem to get what you want I would create a minimal set of Apache configs (move all existing configs out and create temporary ones) and start changing things a step at a time to see where things are going wrong.


how to update a node field value with jQuery callback the drupal way?

Question: added the callback for jQuery to the hook_menu in my module.
// callback for jQuery with nid and new status as arguments
$items['vacancies_manage/status_callback/%/%'] = array(
    'page callback' => '_vacancies_manage_status_update_callback',
    'page arguments' => array(1,2),
    'access callback' => array('manage own vacancies'),
    'type' => MENU_CALLBACK,
);

return $items;

but how do I call this callback from the module using jQuery instead of just the link? Is there a drupal way for this?
Comment: This isn't really a Drupal question. It's more "how do I use jQuery to hit a URL?". See http://api.jquery.com/jquery.ajax
Comment: I think this is a Drupal question because there might me a drupal hook or something for it. I'll rephrase my question.
Answer: The jQuery would be pretty simple, something like this:
$('#link-id').click(function() {
  $.ajax({
    url: '/vacancies_manage/status_callback/' + id1 + '/' + 'id2',
    data: {'arg1': 1, 'arg2': 2},
    success : function(data) {
      alert(data);
    }
  });
});

You'll need to replace id1 and id2 with the arguments appropriate to your situation, and arg1 and arg2 with whatever you want your POST vars to contain.
Then you just need to make sure that you callback actually prints directly to the page, and doesn't return the entire rendered Drupal page so that the jQuery code can receive a response:
function _vacancies_manage_status_update_callback() {
  // Do something with $_POST vars here

  // Print something, perhaps a status message to be picked up in jQuery
  print 'OK';
  drupal_exit();
}

Answer: The correct way to do this is by using jQuery.ajax. There is a Drupal specific version of this, called Drupal.ajax.


What to choose from BIC/AIC/ridge/elastic net?

Question: I have the following regression problem
I have about 60 independent variables; some of them have a high correlation with others. I have around 3 million observations
(1) - My main goal is out-of-sample-prediction, so my main question is: which regularization method should I use in this case?
Some more questions (assumptions I have, probably a little confused)
(2) - Ridge regression, while not completely removing coefficients, would keep those coefficients low that lasso/elastic net/BIC would completely remove; is that correct? (If it doesn't, would that be a problem?)
(3) - If I wanted to use AIC/BIC in this case, I would have to test all possible combinations of the 60 independent variables?
(4) - Would it make sense to start with AIC/BIC, then later do ridge regression with the remaining independent variables? (I guess ridge regression after AIC/BIC might make sense because some of the independent variables correlate with others?)
Thanks
Comment: With 3 million observations, and 60 features, there is no ostensible reason to perform variable selection, unless you are running into numerical problems. Particularly if you are interested in prediction.
Comment: You don't specify why you need parsimony in your model. Is it difficult or expensive to collect all the 60 variables? Just like @fgnu asked, are you running into numerical problems?
Answer: There seem to be two related issues here: 1) Overfitting and 2) Collinearity. As @fg said in a comment, with so many observations overfitting is not likely to be a real problem. However, collinearity may be.
High correlations among IVs is often a sign of problematic collinearity - that is, collinearity that can cause the model to be poorly estimated - but it is neither a necessary nor a sufficient condition for that. Since you are estimating a linear model I suggest getting the condition indices and proportion of variance explained matrix (you don't say if you are using R, SAS, SPSS or what, but this is available in all three of those and probably others).  High condition indices (30 or so is a recommended threshold) that have associated high proportion of variance explained on two or more variables may cause problems.
An alternative to this (which also works well for nonlinear models), if you are using R, is the perturb package.
The main problem caused is that small changes in the input can cause large changes in the model (and, thus, possibly  large changes in predictions).
Comment: What about point 3. in [here](http://robjhyndman.com/hyndsight/to-explain-or-predict/) (Rob J. Hyndman's blog): *"Multicollinearity has a very different impact if your goal is prediction from when your goal is estimation. When predicting, multicollinearity is not really a problem provided the values of your predictors lie within the hyper-region of the predictors used when estimating the model."* In the question, it is stated that the goal is prediction; so maybe multicollinearity is not a problem?
Comment: I think there could still be a problem because 1) The post says the main goal is prediction, which sort of implies that he's also interested in explanation 2) He wants to predict out of sample, which could get out of the hyper-region.


select list - content outputs key instead of label

Question: Drupal 7 v. 7.34 - content type. In my content type I use at least 3 fields with select lists. In each case the key is output instead of the label. I would expect the label to be displayed to the user.
E.g. the field 'theme' has the following available options:
wedding|wedding first dance
dancepromo|let's dance
The content shows 'wedding' instead of 'wedding first dance' and 'dancepromo' instead of 'let's dance'.
What am I doing wrong ?
Should I change the key|label order to label|key ?
Answer: Go to the display settings for your content type, and look at the display formatter for the field. it should be set to Default. The other option is Key.


Foreach com duas listas do bd. blade do Laravel

Question: Li alguns artigos e vi desta maneira, porém não está correto dando o seguinte erro:

(1/1) ErrorException  Undefined offset: 1

@foreach($palpites, $confrontos) as ($p, $c))

     {{ $p->diadojogo}}
     {{ $p->timea }}
     {{ $c->diadojogo}}
     {{ $c->resultado}}

@endforeach

Comment: Quais artigos você leu? Buscou na [documentação oficial](https://laravel.com/docs/5.5/blade#the-loop-variable)? Nela nem consta esta sintaxe que utilizou.
Comment: Poste o artigo aonde o `foreach` faz a interação em duas listas?
Answer: Você pode utilizar desta forma, acredito que ira funcionar para a sua situação:

            coluna1P
            coluna2P
            coluna1C
            coluna2C

    @foreach($palpites as $index => $palpite)

             {{ $palpite['diajogo'] }}
             {{ $palpite['timea'] }}
             {{ $confrontos[$index]['diajogo'] }}
             {{ $confrontos[$index]['resultado'] }}

    @endforeach

Acredito que só não ira funcionar se as quantidades de palpites e confrontos não forem a mesma.
Comment: O seu codigo dá erro. tanto uma parte quanto a outra isolada.
@ foreach($palpites as $index => $palpite)
(1/1) FatalErrorException
Cannot use object of type stdClass as array

 {{ $palpite['diadojogo'] }}
 {{ $palpite['timea'] }}

  @ endforeach

@ foreach($palpites as $index => $palpite)

 {{ $confrontos[$index]['diajogo'] }}
 {{ $confrontos[$index]['resultado'] }}

    @ endforeach
Comment: O erro ocorre devido eu estar trabalhando com arrays e você está tentando acessar como objeto.


Slow transactions in sub-domain

Question: Wonder if you can help solve this little problem.
I administer a system that has Wordpress in the parent domain and an api being served from a sub-domain.
Normally, the api returns results to its client < 1 sec. However, when the api gets 'busy', the transactions appear to queue. We've proved this by the use of timers on the client and on the server. When the server gets a request it processes it < 1 sec and returns data to the client instantly thereafter. When busy it can take 10 sec for the transaction to reach the server process.
Interestingly, this only affects the sub-domain, the primary domain is serving pages with no delays whatsoever.
I'm assuming it is not an application problem as the requests are being returned to client quickly, when they arrive. I'm guessing that it's not an Apache issue as apache is covering at least four domains on the same server and it is only the sub-domain serving the API that is affected.
So, my question is .. where is this delay/queuing occurring? I'm afraid my knowledge of servers & networks has now been exhausted.
Thanks in advance.
Answer: After a lot of digging in dark holes I think I've solved the issue and wish to pass on in case anyone else gets the issue.
I found that PHP-FPM was causing the issue. Our PLESK managed server has its own PHP environment and PHP-FPM configuration. As traffic built up on the API sub-domain its 'processors' were quickly taken up and a queue formed. However on the parent domain, no effects were seen because not all of its 'processors' were in use.
So a change to the pm.max_children parameter increased the number of 'processors' in the API sub-domain. Of course, use of this parameter should be considered as this will have an impact on the system memory of your server, so don't go mad with it.

Slow transactions in sub-domain


Are these Bernoulli variables independent?

Question: I was reading a paper in which it was assumed that $\varepsilon_1,\cdots,\varepsilon_n$ conditional on $X$ possess serial (non-linear) dependence, such that
\begin{equation}
P[\varepsilon_t\geq0\mid\varepsilon_1,\cdots,\varepsilon_{t-1},X]=P[\varepsilon_t<0\mid\varepsilon_1,\cdots,\varepsilon_{t-1},X]=\frac{1}{2}
\end{equation}
Then the signs $s(\varepsilon_1),\cdots,s(\varepsilon_n)$ are i.i.d and distributed as $Bi(1,0.5)$. Proof: We can write the likelihood function of the signs conditional on X as
\begin{eqnarray}
l(s(\varepsilon_1),\cdots,s(\varepsilon_n)\mid X)&=&\prod\limits_{t=1}^{n}P[\varepsilon_t\geq0\mid\varepsilon_1,\cdots,\varepsilon_{t-1},X]^{s(\varepsilon_t)}P[\varepsilon_t<0\mid\varepsilon_1,\cdots,\varepsilon_{t-1},X]^{1-s(\varepsilon_t)}\\
&=&\left(\frac{1}{2}\right)^{s(\varepsilon_t)}\left(\frac{1}{2}\right)^{1-s(\varepsilon_t)}=\left(\frac{1}{2}\right)^n
\end{eqnarray}
This holds for any combination of $t=1,\cdots,n$, if there is a permutation $\pi:i\rightarrow j$ such that the earlier assumption on the conditional median holds. Now instead lets assume we are interested in the signs $s(\varepsilon_1+\beta x_1),\cdots,s(\varepsilon_n+\beta x_n)$. Intuitively, conditional on $X$, as $\beta x_1,\cdots,\beta x_n$ are constant, and since $s(\varepsilon_1),\cdots,s(\varepsilon_n)$ are independent, then the signs $s(\varepsilon_1+\beta x_1),\cdots,s(\varepsilon_n+\beta x_n)$ should also be independent. However, if we write the likelihood function, we would not observe this
\begin{equation}
l(s(\varepsilon_1+\beta x_1),\cdots,s(\varepsilon_n+\beta x_n)\mid X)=\\
\prod\limits_{t=1}^{n}P[\varepsilon_t\geq-\beta x_{t}\mid\varepsilon_1,\cdots,\varepsilon_{t-1},X]^{s(\varepsilon_t+\beta x_t)}P[\varepsilon_t<-\beta x_t\mid\varepsilon_1,\cdots,\varepsilon_{t-1},X]^{1-s(\varepsilon_t+\beta x_t)}
\end{equation}
and as before since no assumptions exists on the median of $\varepsilon_t+\beta x_t$ conditional on its own past and $X$, then the joint probabilities $P[\varepsilon_t\geq-\beta x_{t}\mid\varepsilon_1,\cdots,\varepsilon_{t-1},X]$ vary across time. Is my conclusion correct that thus, the signs $s(\varepsilon_1+\beta x_1),\cdots,s(\varepsilon_n+\beta x_n)$ cannot be concluded to be independent? Or am I missing something here?
Thanks in advance.
Answer: Your conclusion is correct. If signs $s(\varepsilon_1),\cdots,s(\varepsilon_n)$ are i.i.d and distributed as $Bi(1,0.5)$, you cannot conclude that signs $s(\varepsilon_1+\beta x_1),\cdots,s(\varepsilon_n+\beta x_n)$ are independend (or identically distributed). For example, if $\beta x_i > 0$, it is possible that $\varepsilon_n+\beta x_i$ is always positive, whatever $s(\varepsilon_i)$ is.  If all elements of $\beta x$ are positive, you can randomly select signs of $s(\varepsilon_1),\cdots,s(\varepsilon_n)$, and then select these variables in such way that if $\varepsilon_{i-1}+\beta x_{i-1}<0$ then $\varepsilon_i+\beta x_i>0$.
Comment: I couldn't follow the relevance of your final statement to the question.  Could there be some typos in it?
Comment: @whuber - yes, fixed.


How can I log in as admin to my Commerce-kickstart demo site?

Question: I am new to Drupal and I have completed the installation process for Commerce Kickstart in my XAMPP environment.
When I try to access the admin page of my sample commerce-kickstart demo eshop, I get an error like so:

error 403 access denied

I am using the predefined Username (admin) and password and PHP 5.3.6.
Can anyone help me to get logged in as admin?
Answer: Check if anything in this list of typical things might help:

Check if you get the same problem using another browser. If with another browser it works, then it might be a cookie problem. To solve that, delete the cookie in the browser where you have the problem.
Make sure "clean urls" is enabled. Refer to "Clean URL leads to duplicate URL after migration to another hosting" for more details on that.
Make sure the value of "base_url" is set correctly (in your settings.php).
Answer: For all Drupal installs you can use /users to /user/login to access login form, unless the paths have been changed.


Union of same type in C++

Question: Whenever I see examples of union, they are always different types. For example, from MSDN:
// declaring_a_union.cpp
union DATATYPE    // Declare union type
{
    char   ch;
    int    i;
    long   l;
    float  f;
    double d;
} var1;          // Optional declaration of union variable

int main()
{
}

What happens if I have a union (in this case anonymous, but that shouldn't matter) like this:
union
{
    float m_1stVar;
    float m_1stVarAlternateName;
};

Regardless of whether this is good practice or not, will this cause any issues?
Comment: What issues do you think this might cause?
Comment: I am getting heap corruption, and the answer to this question will lead to another question :) ... I want to eliminate all possibilities. In this case, I don't see any issues, but as they say 'you never know'.
Comment: I guess, you are already using some tools apart from mere code inspection? On unix-likes valgrind is incredibly useful, not sure about alternatives on Windows (http://stackoverflow.com/questions/413477/is-there-a-good-valgrind-substitute-for-windows). Finding heap corruption by code inspection can be very time-consuming.
Answer: No, this won't cause any issues. The reason you don't see it more often is that it's pointless - both names refer to the same value of the same type.
Comment: True, but sometimes you may not know whether the types are the same (like if one of them is a template argument).
Comment: @leftaroundabout, very good point that I hadn't considered. Thanks.
Comment: Sorry to necro this, but can this be backed up? Is it defined somewhere? Obviously it should work, but is it _guaranteed_?
Comment: @Shaggi since it follows the required syntax, the only way it wouldn't work is if it were specifically disallowed. I've never been much of a language lawyer, so I'm not really keen to search the standard for something that probably isn't there.
Comment: I should have specified: Will there be a problem if I write to the first field, and read from the second? I was going to ask this question, but I'm pretty sure it would be closed, being a duplicate of this.
Comment: @Shaggi I've heard that writing to one element of a union and reading from another is actually undefined behavior, although you might get away with it.
Comment: @fat_flying_pigs http://stackoverflow.com/questions/34677343/accessing-same-type-inactive-member-in-unions
Comment: @Shaggi can you link your posted question
Comment: @Shaggi Your question is different. This one doesn't have structs.
Answer: This is legal and very useful in situations where you have different contexts, where different names would be more appropriate. Take a four member vector vec4 type for example (similar to what you'd find in GLSL):
vec4 v(1., 1., 1., 1.);

// 1. Access as spatial coordinates
v.x;
v.y;
v.z;
v.w;

// 2. Access as color coordinates (red, green, blue, alpha)
v.r;
v.g;
v.b;
v.a;

// 3 Access as texture coordinates
v.s;
v.t;
v.p;
v.q;

A vec4 may only have four members of the same type, but you'd use different names to refer to the same objects, depending on the context.
Case in point, data contained in the GLM (OpenGL mathematics) vector use this feature, albeit the code is a bit more intriticate:
#       if GLM_CONFIG_XYZW_ONLY
            T x, y;
#           if GLM_CONFIG_SWIZZLE == GLM_SWIZZLE_FUNCTION
            GLM_SWIZZLE_GEN_VEC_FROM_VEC2_COMP(T, Q, x, y)
#           endif//GLM_CONFIG_SWIZZLE
#       elif GLM_CONFIG_ANONYMOUS_STRUCT == GLM_ENABLE
            union
            {
                struct{ T x, y; };
                struct{ T r, g; };
                struct{ T s, t; };

                typename detail::storage<2, T, detail::is_aligned<Q>::value>::type data;

#               if GLM_CONFIG_SWIZZLE == GLM_SWIZZLE_OPERATOR
                    GLM_SWIZZLE2_2_MEMBERS(T, Q, x, y)
                    GLM_SWIZZLE2_2_MEMBERS(T, Q, r, g)
                    GLM_SWIZZLE2_2_MEMBERS(T, Q, s, t)
                    GLM_SWIZZLE2_3_MEMBERS(T, Q, x, y)
                    GLM_SWIZZLE2_3_MEMBERS(T, Q, r, g)
                    GLM_SWIZZLE2_3_MEMBERS(T, Q, s, t)
                    GLM_SWIZZLE2_4_MEMBERS(T, Q, x, y)
                    GLM_SWIZZLE2_4_MEMBERS(T, Q, r, g)
                    GLM_SWIZZLE2_4_MEMBERS(T, Q, s, t)
#               endif
            };
#       else
            union {T x, r, s;};
            union {T y, g, t;};

#           if GLM_CONFIG_SWIZZLE == GLM_SWIZZLE_FUNCTION
                GLM_SWIZZLE_GEN_VEC_FROM_VEC2(T, Q)
#           endif//GLM_CONFIG_SWIZZLE
#       endif

Answer: An issue would arise only if you want to have unique values for the two variables. In your use-case, it should work fine.

Union of same type in C++


Equation for the variance inflation factors

Question: Following a question asked earlier, the variance inflation factors (VIFs) can be expressed as
$$
\textrm{VIF}_j = \frac{\textrm{Var}(\hat{b}_j)}{\sigma^2} =
[\mathbf{w}_j^{\prime} \mathbf{w}_j - \mathbf{w}_j^{\prime}
\mathbf{W}_{-j} (\mathbf{W}_{-j}^{\prime} \mathbf{W}_{-j})^{-1}
\mathbf{W}_{-j}^{\prime} \mathbf{w}_j]^{-1}
$$
$\mathbf{W}$ is the unit length scaled version of $\mathbf{X}$
Can anyone show me how to get from here to the equation
$$
\textrm{VIF}_j = \frac{1}{1-R_j^2}
$$
$R_j^2$ is the coefficient of multiple determination obtained from regressing $x_j$ on the other regressor variables.
I'm having a lot of troubles getting these matrix operations right...
Answer: Assume all $X$ variables are standardized by the correlation transformation, like you mentioned, unit length scaled version of $\mathbf{X}$. The standardized model does not change the correlation between $X$ variables. $VIF$ can be calculated when standardized transformation of the original linear model is made. Let's denote the design matrix after standardized transformation as
\begin{align*}
\mathbf{X^*} = \begin{bmatrix} 1& X_{11}& \ldots &X_{1,p-1} \\ 1& X_{21}& \ldots &X_{2,p-1} \\ \vdots & \vdots & \vdots & \vdots \\ 1& X_{n1}& \ldots &X_{n,p-1} \\ \end{bmatrix}.
\end{align*}
Then
\begin{align*}
\mathbf{X^{*'}X^*} = \begin{bmatrix} n & \mathbf{0}' \\ \mathbf{0} & \mathbf{r}_{XX} \end{bmatrix},
\end{align*}
where $\mathbf{r}_{XX}$ is the correlation matrix of $X$ variables. We also know that
\begin{align*}
\sigma^2\{\hat{\beta}\}
& = \sigma^2 (\mathbf{X^{*'}X^*})^{-1}\\
& = \sigma^2 \begin{bmatrix} \frac{1}{n} & \mathbf{0}' \\ \mathbf{0} & \mathbf{r}^{-1}_{XX}. \end{bmatrix}\\
\end{align*}
$VIF_k$ for $k=1,2,\ldots,p-1$ is the $k$-th diagonal term of $\mathbf{r}^{-1}_{XX}$. We only need to prove this for $k = 1$ because you can permute the rows and columns of $r_{XX}$ to get the result for other $k$.
Let's define:
\begin{align*}
\mathbf{X}_{(-1)} = \begin{bmatrix} X_{12}&\ldots&X_{1,p-1} \\X_{22}&\ldots&X_{2,p-1}\\ \vdots & \vdots & \vdots \\ X_{n2}&\ldots&X_{n,p-1} \\ \end{bmatrix}, \mathbf{X}_1 = \begin{bmatrix} X_{11} \\ X_{21} \\ \vdots \\ X_{n1} \\ \end{bmatrix}.
\end{align*}
Note that both matrices are different from design matrices. Since we only care about the coefficients of $X$ variables, the $1$-vector of a design matrix can be ignored in our calculation. Hence, by using Schur's complement,
\begin{align*}
r^{-1}_{XX} (1,1)
& = (r_{11} - r_{1\mathbf{X}_{(-1)}} r^{-1}_{\mathbf{X}_{(-1)}\mathbf{X}_{(-1)}} r_{\mathbf{X}_{(-1)}1})^{-1} \\
& = (r_{11} - [r_{1\mathbf{X}_{(-1)}} r^{-1}_{\mathbf{X}_{(-1)}\mathbf{X}_{(-1)}}] r_{\mathbf{X}_{(-1)}\mathbf{X}_{(-1)}} [r^{-1}_{\mathbf{X}_{(-1)}\mathbf{X}_{(-1)}} r_{\mathbf{X}_{(-1)}1}])^{-1} \\
& = (1-\beta_{1\mathbf{X}_{(-1)}}' \mathbf{X}_{(-1)}' \mathbf{X}_{(-1)} \beta_{1\mathbf{X}_{(-1)}} )^{-1},
\end{align*}
where $\beta_{1\mathbf{X}_{(-1)}}$ is the regression coefficients of $X_1$ on $X_2, \ldots, X_{p-1}$ except the intercept. In fact, the intercept should be the origin, since all $X$ variables are standardized with mean zero.
On the other hand, (it would be more straightforward if we can write everything in explicit matrix form)
\begin{align*}
R_1^2
& = \frac{SSR}{SSTO} = \frac{\beta_{1\mathbf{X}_{(-1)}}' \mathbf{X}_{(-1)}' \mathbf{X}_{(-1)} \beta_{1\mathbf{X}_{(-1)}}}{1} \\
& = \beta_{1\mathbf{X}_{(-1)}}' \mathbf{X}_{(-1)}' \mathbf{X}_{(-1)} \beta_{1\mathbf{X}_{(-1)}}.
\end{align*}
Therefore
\begin{align*}
VIF_1 = r^{-1}_{XX} (1,1) = \frac{1}{1-R_1^2}.
\end{align*}


Probability of intersection from multiple sampling of the same population

Question: Here is an example case:

I have a population of 10,000 items. Each item has an unique id.
I randomly pick 100 items and record down the ids
I put the 100 items back into the population
I randomly pick 100 items again, record down the ids and replace.
In total, I repeat this random sampling 5 times

What is the probability that $X$ number of items appear in all 5 random samplings?
I am not very well versed in statistics. Would this be correct for $X = 10$?

For each sampling, the number of possible combinations of 100 items from 10,000 is ${\rm binom}(10000, 100)$
Out of all possible combinations of 100 items, ${\rm binom}(9990, 90) * {\rm binom}(100, 10)$ combinations contain 10 specific items
The probability of having 10 specific items is $({\rm binom}(9990, 90) * {\rm binom}(100, 10)) / {\rm binom}(10000, 100)$
The calculated probability to the power of 5 would represent 5 indepenent samplings.

So essentially we are just calculating 5 independent hypergeometric probabilities and then multiplying them together? I feel like I am missing a step somewhere.
Comment: If you repeat something once, it means you do it twice in all. Doesn't repeating something 5 times imply you do it 6 times?
Answer: Compute the chances recursively.
Let $p_s(x)$ be the probability that exactly $x$ values, $0 \le x \le k$, are selected in all $s\ge 1$ independent draws of $k$ items (without replacement) from a population of $n \ge k \gt 0$ members. (Let's hold $n$ and $k$ fixed for the duration of the analysis so they don't have to be mentioned explicitly.)
Let $p_s(x\mid y)$ be  the probability that if exactly $y$ values are selected in the first $s-1$ draws, then $x \le y$ of them are selected in the last draw. Then because there are $\binom{y}{x}$ subsets of $x$ elements of those $y$ elements, and $\binom{n-y}{k-x}$ subsets of the remaining $k-x$ elements are separately selected out of the other $n-y$ members of the population,
$$p_s(x\mid y) = \frac{\binom{y}{x}\binom{n-y}{k-x}}{ \binom{n}{k}}.$$
The law of total probability asserts
$$p_s(x) = \sum_{y=x}^k p_s(x\mid y) p_{s-1}(y).$$
For $s=1$, it's a certainty that $x=k$: this is the starting distribution.
The total computation needed to obtain the full distribution up through $s$ repetitions is $O(k^2 s)$.  Not only is that reasonably quick, the algorithm is easy.  One pitfall awaiting the unwary programmer is that these probabilities can become extremely small and underflow floating-point calculations.  The following R implementation avoids this by computing the values of $\log(p_s(x))$ in columns $1, 2, \ldots, s$ of an array.
lp <- function(s, n, k) {
  P <- matrix(NA, nrow=k+1, ncol=s, dimnames=list(0:k, 1:s))
  P[, 1] <- c(rep(-Inf, k), 0)
  for (u in 2:s)
    for (i in 0:k) {
      q <- P[i:k+1, u-1] + lchoose(i:k, i) + lchoose(n-(i:k), k-i) - lchoose(n, k)
      q.0 <- max(q, na.rm=TRUE)
      P[i+1, u] <- q.0 + log(sum(exp(q - q.0)))
    }
  return(P)
}
p <- function(...) zapsmall(exp(lp(...)))

The answer to the question is obtained by letting $s=5,$ $n=10000=10^4$, and $k=100=10^2$.  The output is a $101\times 5$ array, but most of the numbers are so small we may focus on very small $x$.  Here are the first four rows corresponding to $x=0,1,2,3$:
p(5, 1e4, 1e2)[1:4, ]

The output is
  1         2         3      4        5
0 0 0.3641945 0.9900484 0.9999 0.999999
1 0 0.3715891 0.0099034 0.0001 0.000001
2 0 0.1857756 0.0000481 0.0000 0.000000
3 0 0.0606681 0.0000002 0.0000 0.000000

Values of $x$ label the rows while values of $s$ label the columns.
Column 5 shows the chance that one element appears in all five samples is minuscule (about one in a million) and there's essentially no chance that two or more elements appear in all five samples.
If you would like to see just how small these chances are, look at their logarithms.  Base 10 is convenient and we don't need many digits:
u <- lp(5, 1e4, 1e2)[, 5]
signif(-u[-1] / log(10), 3)

The output tells us how many zeros there are after the decimal point:
    1     2     3     4     5     6     7     8     9    10  ...   97    98    99   100
  6.0  12.3  18.8  25.5  32.3  39.2  46.2  53.2  60.4  67.6 ... 917.0 933.0 949.0 967.0

Numbers in the top row are values of $x$.  For instance, the chance of exactly three values showing up in all five samples is found by computing exp(u[4]), giving $0.000\,000\,000\,000\,000\,000\,1434419\ldots$ and indeed this has $18$ zeros before the first significant digit.  As a check, the last value $967.0$ is a rounded version of $967.26$. $\binom{10000}{100}^{-4}$ (which counts the chances that the first sample reappears in the next four samples) equals $10^{-967.26}.$
Answer: I just ran into a similar problem and, even though I also don't know if this is the correct solution, approached it like this:
You are interested in the occurrence of $X$ items in 5 samples á $100$ items of $10,000$ items total. You could think of an urn with $X$ white balls and $10,000-X$ black balls. $100$ balls are taken out and $p_h$ is the probability that you have all $X$ white balls in your set. If you do this $5$ times (independently), I would multiply it: $p = {p_h}^5$.
I could even think of one step further and wrap it around the binomial distribution: If you have a coin which comes up head with probability $p_h$ (the probability that you have all items in your set) and you toss it $5$ times, what is the probability of getting $5$ heads? $p = {5\choose 5}{p_h}^5 (1-{p_h})^{5-5} = {p_h}^5$.
Answer:
What is the probability that $X$ number of items appear in all 5 random samplings?

Building on what Hans said, you want to always get the same $X$ ids in each sample of 100 and 100-$X$ ids from among the remaining 10000-$X$. The probability of doing so for a given sample is given by the hypergeometric function for $X$ successes in a draw of 100 from a population of 10000 with $X$ possible success states: $P = \frac{{X \choose X}{10000-X \choose 100-X}}{10000 \choose 100}$. For 5 samples, you would take $P^5$.
However, that we presuppose knowing the $X$ ids that are shared, and there are $10000 \choose X$ ways to select those $X$ ids. So your final answer would be ${10000 \choose X} P^5$.
Comment: What is "$x$"?  It's impossible to understand this answer, much less check it, until you reveal that!
Comment: I don't remember, as it was 3 years ago, but presumably the same X as in the question?
Comment: OK.  But what justifies your formula?  Simple checks, such as the case $X=0$ (for which your formula tells us the probability is $1$, thereby completely ruling out any other possibilities!), indicate it's incorrect.


git - Limit on what can be stored?

Question: I have been having intermittent issues with git.  I am using it to version all of my files that are not volatile.
The error:
fatal: confused by unstable object source data for
Now, I originally had this problem on another drive and when I did, I saw an error in dmesg indicating that EXT-4 was unable to read some meta-data.  That led me to believe the drive was failing.  So, I unmounted the drive and ran the fsck tools and everything checked out okay.  Do I need to do anything more in depth since git is complaining?
Secondly, I just got this error on a newer drive and did NOT get any errors in dmesg indicating any problems reading meta-data in the file system.  I am certain that no other processes are writing to it as I used lsof.
Any ideas on how to proceed?  I am storing large, binary files in one case (several GB) and a mix of files in the other.
Similar post:
git says "fatal: confused by unstable object source data"
Walter
Comment: GIT isnt' an acronym :) It is Git
Comment: `git fsck --full` is always worth a try to test the consistency of the git storage. Also, checking the RAM of the machine is often worth a try :)
Comment: @Jefromi: I don't care about the spelling, but it seems to me that http://www.git-scm.com says `Git` - except in the logo of the page.
Comment: @Mohamed: I used to edit everyone's questions to fix that, and Git vs. git (it's git, not Git), but it's a losing battle...
Comment: I have 6GB of ram and NO swap, I am guessing that I probably need swap for this to work as my file is larger than 6GB ...
Comment: @Frerich: Looks like I got burned by something I read in a git manpage or commit message somewhere. My bad.
Answer: Either you are trying to add always-changing volatile file or something low-level is failing.
git says "fatal: confused by unstable object source data"
Answer: Maybe something to do with Copy-On-Write feature in ext4 ?
See https://stackoverflow.com/a/42299634/7491491

git - Limit on what can be stored?


Homotopy Theory and extensions/liftings.

Question: I found the statement:  suppose that in the extension problem we have a map f': A -> E homotopic to f, and f' extends. Then it does not follow that f extends. Similarly, if the map g in the lifting problem is homotopic to a map that lifts, it does not follow that g itself lifts. The reader can easily supply counterexamples in both cases.
I don't see what a counterexample would be.
What I do understand that if, in the extension case, if there is another map i:A -> X then we may be able to find some h:X -> E that extends f'.  I can't seem to picture how if we have another map H(1) = f:A -> E (as opposed to H(0) = f') where f and f' are homotopic to each other, that one wouldn't have an extension of f also as by the definition(?) of a homotopy there is a continuous mapping between f and f'.  But furthermore since they are both mapping to and from the same spaces A and E, why they would be different.
Any thoughts?
Thanks,
Brian
Answer: The key concept here is the homotopy extension property; let me quote this from the Wikipedia article of the same name:

Let $X\,\!$ be a topological space, and let $A \subset X$. We say
  that the pair $(X,A)\,\!$ has the homotopy extension property if,
  given a homotopy $f_t\colon A \rightarrow Y$ and a map
  $\tilde{f}_0\colon X \rightarrow Y$ such that $\tilde{f}_0 |_A = f_0$,
  there exists an ''extension'' of $\tilde{f}_0$ to the homotopy
  $\tilde{f}_t\colon X \rightarrow Y$ such that $\tilde{f}_t|_A = f_t$.

Let's assume that the pair $(X,A)$ in your question satisfies the homotopy extension property. Let's say that $f:A\to Y$ and $f':A\to Y$ are homotopic via a homotopy $f_t:A\to Y$ and that $f'$ extends to a map $\tilde{f'}:X\to Y$. The homotopy extension property states precisely in this context that there exists an extension $\tilde{f_t}:X\to Y$ of the homotopy $f_t:A\to Y$ such that $\tilde{f_0}=f'$. Of course, in this case $\tilde{f_1}:X\to Y$ is an extension of $f:A\to Y$. Therefore, if $f':A\to Y$ extends, then so does $f:A\to Y$.
Theorem If $(X,A)$ is a CW pair, then $(X,A)$ satisfies the homotopy extension property.
So, any counterexample to your claim cannot be a CW pair.
The entire discussion above also has an analogue which applies to the corresponding question for liftings. In this case, the relevant concept is the homotopy lifting property; e.g., see http://en.wikipedia.org/wiki/Homotopy_lifting_property .
Exercise 1: Show that there is no counterexample that you seek if the pair $(X,A)$ satisfies the homotopy lifting property.
Definition 1 A map $h:E\to B$ is a fibration if it satisfies the homotopy lifting property with respect to every pair $(X,A)$. It is a Serre fibration if it satisfies the homotopy lifting property with respect to every CW pair $(X,A)$.
Definition 2 A map $i:A\to X$ is a cofibration if it satisfies the homotopy extension property with respect to all spaces $Y$.
In other words, you're looking for maps which aren't fibrations and for maps which aren't cofibrations.
I hope this helps!
Comment: Yes, it does help Amitesh.  Thanks.  But here is a question:  What is the difference of an extension and a lifting?
Comment: @Relative0 Brian, an extension is essentially "extending" the domain of a continuous function. E.g., if $f:A\to Y$ is a continuous function and $A\subseteq X$, then an extension of $f$ is a continuous map $g:X\to Y$ such that $g|_{A}=f$. A lifting is essentially "extending or expanding or pushing (to another space)" the range of a continuous function. E.g., if $f:X\to B$ and $p:E\to B$ are continuous functions, then a lifting of $f$ is a continuous map $g:X\to E$ such that $p\circ g=f$. Does that answer your question? It's good, as well, to find examples of your own of these two phenomena ...


I dont want background image to take up the full page CSS

Question: I currently have the following css and I dont want the background image to take up the full height of the screen but I want the full width
.login-page, .register-page {
    background: url('https:/image/ex.jpg');
    background-position: center;
    background-size: cover;
}

Is there and easy way to do this or is it possible?
Answer: You need to change the background-size property from cover to 100% auto:
.login-page, .register-page {
    background: url('https:/image/ex.jpg');
    background-position: center;
    background-size: 100% auto;
}

Which means: 100% width and auto for the height.
Answer: You can set background-size: 100% 60px; For full width, you can give 100% and for the height, you can specify how much you want to cover. If you specify auto then it will take the height automatically based on your screen.

.login-page, .register-page {
    background: url("https://d2v9y0dukr6mq2.cloudfront.net/video/thumbnail/blue-background-with-reflective-lines_-13idtdxs__F0000.png");
   background-size: 100% auto;
    background-repeat: no-repeat;
    padding-top: 40px;
}

Comment: This is exactly what I want but I dont want the image to stretch to that size if that makes sense?
Comment: Like I want a specific height and then the image to cover this height]
Comment: Ok .. so in this case, you can use background-size: 100% auto; which will take automatically. I have updated the snippet as well as.
Answer: Give background-size: 100% auto;.
Answer: You can set width: 100%;background-size: 100% auto; for the dom, and the background-image will expand to the relative height refer to the resolution ratio of the image.
Answer: Yea you can use background-size: 100% auto; and maybe add some background-repeat: no-repeat;
here is my fiddle example
https://jsfiddle.net/6w80jfc3/
good luck...


git orphan branches with same commits

Question: I want to create an orphan branch that copies all the commits across.
Basically I need to end up with master and dev, both orphans but both identical (until I make further commits).
I have created the orphan master (empty) and have the dev with all my commits. How do I copy all these commits to master?
Comment: Don't use orphan branches. Just use regular branches. Orphan branches will preclude clean merging, which defeats the point of having branches in the first place. Also, use tags for versions, not branches.
Comment: Why do you want an orphan branch with all your commits? If I understand you correctly, you want a second branch with all-new commits (i.e. not the same SHAs). This will be nontrivial to create if your `dev` history is not a straight line, because merge commits &c will be very tricky to recreate. Maybe it would help if you described what you need this for.
Comment: jQuery is NOT using orphan branches. They are using regular branches. The SHA1 of the branches diverged due to different merge patterns, but they share a common development history.
Comment: No...that makes a *regular* branch. `git checkout --orphan dev` would make an orphan branch. But, in your situation (new to git), I'd stay the hell away from orphan branches. That's mainly aimed at power users who need totally disconnected development histories for special uses.
Comment: Because DEV will be the rolling dev, and master will be the most current stable code, while ill then have other orphan branches for the versions.

If you look at jQuery here in github they have it setup as I describe.

Master = https://github.com/jquery/jquery/commits/master?page=5
, "dev" = https://github.com/jquery/jquery/commits/1.9-stable
. Both have the same commits for March 26 2013 etc
Comment: Okay. How do I do tags for versions?
And why is what you suggest better than jQuery's and others setup? Also thinking out what you say, means I can't have a generic dev branch, it would have to be named for the proposed version and then how do you know its dev and not stable?
Comment: They are also using branches for major versions that are being maintained in parallel. These are "version branches" -- think branches for 1.8.x, 1.9.x, 2.0.x, etc. Multiple releases may be made off a single version branch. But, specific versions (e.g. 1.9.1) are tagged because no further development occurs for a released version.
Comment: Also, I never said you couldn't have a generic dev branch. You can have a dev branch, easy: `git checkout -b dev master`, and then you can make changes specifically on the dev branch, merging them back to master when appropriate.
Comment: @nneonneo - I may not be explaining it right as I am new to git. Would that command make dev an orphan branch?
Comment: I see your point but feel it will become too messy. Much cleaner to have a orphan dev and orphan master. And have the commits copied over including the log of them. I already have made the orphan master (dev currently has all the code and commits).
Comment: So you want to just manually copy commits back and forth? I think this would very much negate 99% of the utility of `git` (pretty much every tool that works across branches expects to see regular branches, not orphan branches). But hey, it's your repo, not mine. If you want to make it a bunch of orphan branches, then it's your decision.
Comment: @nneonneo I just dont see how you would keep the repo clean. Is there a repo you can link me to that shows nicely exactly the type of implementation you think I should go for?
Comment: Here's what jQuery is actually doing, because I think you misunderstand their development process. This page shows a bunch of merge commits: https://github.com/jquery/jquery/commits/master?page=10. This is from them merging in several feature branches which were independently developed, to make a mainline 2.0 branch.
Comment: By the way, the normal workflow for most projects is that `master` (and most of the feature branches) are unstable, by virtue of being actively developed upon. Version branches are expected to be more stable, and the most stable code is obtained from tagged releases. The master is thus usually considered the generic "dev" branch.
Comment: @nneonneo Ok so if we say master is where the work is going on. Then the orphan branch is the version with all the commits? or you saying branch it out into a sub-branch?
Comment: Seriously, stop talking about orphan branches. What the heck do you mean by "all the commits"?
Comment: Ok. They are dead. So you are saying this ....

User master to develop on. Then "git branch v1" to make a version branch and then keep coding in the master until v2 is needed?
Comment: http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging
Comment: But basically, if you're making **versions** of your software, you use tags (tag a released version). A **version branch** is a parallel track of development to support specific version lines, and you only see those on big projects with many versions concurrently supported.
Comment: @nneonneo - all understood. Now how do I back date a tag so all my current commits are tagged to it?
Comment: $ git tag -a v1.2 -m 'version 1.2' 9fceb02
??
Comment: @nneonneo above code tag didnt seem to work
Comment: Just `git tag v1.2 [commit-id]` will suffice.
Comment: @nneonneo - notice that tags auto create release I can then modify in github :/ Thats not so good.
Comment: @nneonneo also when i merge my dev branch into master the commit log will get added to master?
Comment: Yes, merging will bring all the commits from the dev branch into master (`git log` will show both). Better yet, `git log --graph` will show a history of merges, so you can have a good idea of what your development schedule is like.
Answer: (Not judgin if orphan branch is the right solution here, just trying to answer the initial question which was about duplicating history.
As nneonneo comments regular branches are probably fine)
The problem is that if the SHA1 are the same between the two branches, the second branch will always be a simple pointer to a commit of the first branch.
What could work is:

create a clone
on that clone, insert an empty commit as the first commit of your master branch.
See "git: how to insert a commit as the first, shifting all the others?"
back on your original repo, import that master branch as a new one (my favorite trick to create orphan branch, as I explain in "Backup then reset master branch with git?".

That would looks like:
cd /original/repo
git fetch /path/to/clone master:dev

And that would create an orphan branch dev, with all the (same) commits than master (on top of an empty one)

git orphan branches with same commits


What do Single brackets on notes in guitar music mean?

Question:
Anyone got any idea what these single brackets on notes mean in this guitar music?
Comment: You mean the thing that looks like parentheses?
Comment: It's probably a typo. Unless there's a companion earlier on that is not shown in the snippet.
Comment: I have about ten or so pieces all with similar types of marking in them. They're all anonymous composers from some sheet music that I picked up from Freecycle a while ago. I sight read lots of pieces and have recently started wondering what it is that I'm missing by these markings.
Answer: I've just gone through a number of baroque books and looked at buying the Neumann book mentioned above (Not going to happen at that price though). I finally found the answer though at http://baroqueguitar.homestead.com/The_Baroque_Guitar.htm which states that the bracket means a mordent. I hope this is helpful to anyone else who has been puzzled by this.
Comment: Well, that's more or less consistent with what a keyboard player like me would have done with it - but the first one might have been a trill with a written-out turn, depending on the tempo. I wonder why guitar ornaments were written after the note rather than above or before it like most other instruments. But music notation has never been logical!
Comment: See also http://music.stackexchange.com/a/30812/2639 -- there are ) like ornaments indicated in this answer.
Comment: Would this symbol apply to specific guitar technique to execute the mordent (e.g. some kind of pull-off/hammer-on combination)? Is the normal mordent symbol used in current day classical guitar scores with the same meaning?
Comment: It seems it is to do with guitar music (well lute etc... as well). If Alephzero hadn't mentioned baroque it wouldn't even have occurred to me to check it out. Yes, the normal mordent symbol is used in modern day classical guitar music.    In response to Alephzero's wondering about the ornament being written after the note, in the massive amount of reading I've done the last few days trying to figure this out, there were different symbols placed in different places around the note meaning different things. Not that I particularly remember much about them.
Answer: It could be some type of ornament from the baroque or renaissance period. It would help to identify the piece, the composer, and the edition of the music the example came from. The sign is certainly included in the recent comprehensive standard for music fonts - see glpyh U+E572 here: https://w3c.github.io/smufl/gitbook/tables/other-baroque-ornaments.html
A comprehensive reference (which I don't have access to right now) is
Neumann, Frederick. Ornamentation in Baroque and Post-Baroque Music. Princeton, NJ: Princeton University Press, 1978.
Comment: Thanks alephzero. I shall check that out if I can find a copy. I read Hugo Cole's book Sounds and Signs : Aspects of Musical Notation this afternoon but still I'm no clearer on the matter


Firebase synchronize background job that writes documents to Firestore

Question: I have a background job that runs every minute to update documents.
If an update takes longer than a minute I need to skip the next background job.
What's the best way to synchronize this logic?
I currently have
export const pickWinnerBackgroundJob = functions.pubsub.schedule('every 1 minutes').onRun(async (context) => {
    const lock = await admin.firestore()
        .collection(`lock`)
        .doc(BG_JOB_LOCK_DOCUMENT_ID)
        .get();

    if (lock.exists && lock.data()?.locked == true) {
        return;
    }

    await admin.firestore()
        .collection(LOCK_COLLECTION_NAME)
        .doc(BG_JOB_LOCK_DOCUMENT_ID)
        .set({ locked: true })

    var promises: Promise<any>;
    // Add document update promises and execute job

    return Promise.all(promises).then(() => {
        return admin.firestore()
            .collection(LOCK_COLLECTION_NAME)
            .doc(BG_JOB_LOCK_DOCUMENT_ID)
            .set({ locked: false })
    });
});

Is this a valid approach?
Answer: The approach looks reasonable, but you'll have to look out for race conditions. Since you likely can't prevent them completely, you'll have to pick a strategy that fits best with your needs, so: either you may skip one minute too many when there's contention, or two instances may run in parallel occasionally.
Alternatively, you could set the maximum number of instances for this Cloud Function to 1, and not worry about it in code. It looks like the syntax in Firebase's wrapper would be:
functions.pubsub.schedule('every 1 minutes').runWith({ maxInstances: 1 }).onRun(async (context) => {

Comment: Amazing, maxInstances is exactly what I needed, thank you
Comment: actually `Property 'runWith' does not exist on type 'ScheduleBuilder'.ts(2339)`
Comment: Yeah, I was wondering about that. I don't see any exception or requirements for that optoin though: https://github.com/firebase/firebase-functions/blob/98bf467aa436f7e27969a8ad3887abe0345f66d3/src/cloud-functions.ts#L516
Answer: You can also use each function invocation to schedule the next one for a minute after the current one.  Can't ever over-run that way.  (You do need to make sure they keep running, so you may also need a watch-dog)


Prove that every element in a normal subgroup N is also in the conjugate of N

Question: I've been been working through Socratica's video on normal subgroups and factor groups and they show how $y^{-1}Ny \subseteq N$ for some normal subgroup $N \trianglelefteq G$ and $y \in G$. They leave it as an exercise to show $y^{-1}Ny$ and $N$ are equal by demonstrating that $N \subseteq y^{-1}Ny$. I tried starting this proof by picking some $n_1 \cdot y^{-1} \in Ny^{-1}$ and $n_2 \cdot y \in Ny$, then stating that their product is in $Ny^{-1}y$. Then
$(n_1 \cdot y^{-1}) \cdot (n_2 \cdot y) = n_3 \cdot y^{-1} \cdot y$
$n_1 \cdot (y^{-1} \cdot n_2 \cdot y) = n_3 $
$n_1^{-1} \cdot n_1 \cdot (y^{-1} \cdot n_2 \cdot y) = n_1^{-1} \cdot n_3 $
$(y^{-1} \cdot n_2 \cdot y) = n_4$
To me, the last line reads that for any element in $N$ there is a corresponding element in $y^{-1}Ny$. But I'm not sure if I'm any closer to proving it since I'm pretty sure that last line is something we could state just by defining $n_2$ and $n_4$ as elements in $N$.
Comment: This is badly phrased. From $y^{-1}Ny\subseteq N$ for a specific $y$ you cannot conclude that $N\subseteq y^{-1}Ny$ ( you can in a finite group by counting, but not in an infinite one; there are explicit counterexamples). That is true is that if $y^{-1}Ny\subseteq N$ **for every $y$**, then you can conclude that $N\subseteq y^{--1}Ny$. Which one was assumed?
Answer: You need to start with an element $n_1 \in N$
$$
n_1 = y^{-1} y n_1 y^{-1} y\\
$$
Let $x=y^{-1}$ and use that $x^{-1} N x \subseteq N$ because the statement you had was for all $y \in G$ including $x$.
$$
y n_1 y^{-1} = x^{-1} n_1 x\\
= n_2\\
$$
$$
n_1 = y^{-1} n_2 y \\
n_1 \in y^{-1} N y
$$
Comment: Got it. Yeah I guess we need to start by picking from N since we are trying to prove that this holds for any element in N. I also didn't think to make use of the previously proven statement $x^{-1} N x \subseteq N$. Thanks.


Why does package yahoo_earnings_calendar stop working?

Question: I am using package yahoo_earnings_calendar to get earnings calendar from Yahoo and it worked fine one month ago.
<code>from yahoo_earnings_calendar import YahooEarningsCalendar
yec = YahooEarningsCalendar()
yec.get_earnings_of('box')
</code>
But today I find it will raise an error. I printed the error and it send me this html message.

    Yahoohtml{height:100%}body{background:#fafafc url(https://s.yimg.com/nn/img/sad-panda-201402200631.png) 50% 50%;background-size:cover;height:100%;text-align:center;font:300 18px "helvetica neue",helvetica,verdana,tahoma,arial,sans-serif}table{height:100%;width:100%;table-layout:fixed;border-collapse:collapse;border-spacing:0;border:0}h1{font-size:42px;font-weight:400;color:#400090}p{color:#1a1a1a}#message-1{font-weight:bold;margin:0}#message-2{display:inline-block;*display:inline;zoom:1;max-width:17em;_width:17em}document.write('&test="+encodeURIComponent("%")+'" width="0px" height="0px"/>');var beacon=new Image();beacon.src="//bcn.fp.yahoo.com/p?s=1197757129&t="+new Date().getTime()+"&src=aws&err_url="+encodeURIComponent(document.URL)+"&err=%&test="+encodeURIComponent("%");Will be right back...Thank you for your patience.Our engineers are working quickly to resolve the issue.

I find other similar question. It seems that Yahoo do not want us scrape their data. But those posts do not solve my code block. Can somebody help me? Thanks a lot!
Answer: I had a similar problem.  Here's the link to my question that was answered today.  Yahoo redid their site, so now you have to upgrade the package for the changes.
Error when creating graph with mplfinance
Comment: Hi I tried and it does not work. I am using package yahoo_earnings_calendar not yfinance. But I think both problem result from yahoo's web scrape policy.
Comment: @ZhibaiLuo  When you say you tried, *did you include* **`pip uninstall yahoo-earnings-calendar`** and **`pip install --upgrade --no-cache-dir yahoo-earnings-calendar`** ??


How to fit maximum likelihood estimates of normal distribution using subset of dataset in R?

Question: I have dataset of numbers given as
$Y = x $ if $ x >= 5 $ and $x<=9$
and $x$~$N(\mu,\sigma^{2})$. Or we can say that we have the data-set as subset of normally distributed data-set.
Now for the given data(histogram given below), how do i estimate those unknown parameters $\mu$ and $\sigma^{2}$ using MLE in R?

Answer: The truncated density is the normal distribution but multiplied with 1/(F(right)-F(left)) so that its integral is 1. So one idea would be to use this truncated density for the calculation of the loglikelihood.
For example, in R:
ll1 <- function(param, dat, left, right) {
    mu <- param[[1]]
    sigma <- param[[2]]
    -sum(dnorm(dat, mean=mu, sd=sigma, log = TRUE)-log(pnorm(right, mean=mu, sd=sigma)-pnorm(left, mean=mu, sd=sigma)))
}

set.seed(1)
x <- rnorm(1000, mean=6, sd=1.2)
y <- x[x>=5 & x <= 9]
optim(par=c(mean(y), sd(y)), fn=ll1, dat=y, left=5, right=9)[["par"]]
# [1] 5.960276 1.280664

However, after getting downvoted I am not sure if that is the right thing to do. Looking at the chart of the loglikelihood (the red dot represents the true parameters we happen to know), something seems wrong:

It looks to me as if the loglikelihood does not have a minimum at the true parameter. On the other hand, repeating the fitting again and again with different input data, yields histograms that look good:
Comment: Unless I'm mistaken, this code is doing the right thing. To be a proper answer though, you will need to describe *why* it is the right thing to do.
Comment: What exactly is this code doing?
Comment: Well, I tried to calculate the density of the truncated distribution, ...
Comment: Right, it was my downvote, I'll remove it since my point was made.  Sorry, I should have been more clear in my comment.  I think it will be a good answer with a full description of the algorithm.
Comment: Thanks for solution. Could you please help me selecting type of optimization method for optim R function i.e when to use which optimization?
Comment: Since this is a convex optimization problem, the default Nelder-Mead should work just fine. If time is a factor, you could provide a gradient function and change to conjugate gradient method ("CG"), which should be faster.


how to change user agent in selenium with .net

Question: I want to test a web app written in .NET with different agents (iPhone, iPad, Android), I'm using NUnit for the tests and Selenium.
Anyone has a sample to change the agent (for example for iPad or iPhone) in Selenium with c# or VB?
Answer: Example UA (You may need Google yourself to find the ones suit your purpose):

Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25

C# Firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("general.useragent.override", "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25");
IWebDriver driver = new FirefoxDriver(profile);

C# Chrome:
ChromeOptions options = new ChromeOptions();
options.AddArgument("--user-agent=Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25");
IWebDriver driver = new ChromeDriver(options);

C# PhantomJS (untested code):
PhantomJSOptions options = new PhantomJSOptions();
options.AddAdditionalCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25");
IWebDriver driver = new PhantomJSDriver(options);

C# IE: Sorry, I don't think this is possible natively from Selenium.
Further reading: Set user agent using Selenium WebDriver C# and Ruby
Answer: In c#:
public static FirefoxProfile myFireProfile = new FirefoxProfile();
myFireProfile.SetPreference("general.useragent.override", "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10");
public IWebDriver driver= new FirefoxDriver(myFireProfile);


Awkward/wrong PostgreSQL foreign-key definition

Question: As a database developer, I experienced this notice when I tried to make a data-only dump a PostgreSQL(10.1) database 'tlesson'.
Notice =>
<code>  pg_dump: NOTICE: there are circular foreign-key constraints on this table:
  pg_dump: members
</code>
Dump command =>
<code>  $ pg_dump -U postgres -d translesson -a
</code>
A 'tlesson' table 'members' constraint =>
<code>  ALTER TABLE ONLY members
    ADD CONSTRAINT friend_fk FOREIGN KEY (friend_id) REFERENCES members(member_id);
</code>

That is, 'friend_id' column refers own table's primary key as the foreign-key.

Should I drop the 'friend_fk' constraint to remove the notice I'm having?
Answer: If you always drop the entire database then this isn't a problem, because the generated SQL (or pg_restore) will enable (create) foreign keys only after all the data was loaded, so there is no problem in that case.
However if you only dump a single table without the FKs then, importing is only going to work if you manually drop the FK before restoring, then re-create it afterwards.
The reason is that it's nearly impossible to generate INSERT statements in the correct order if you have circular references


JPA Hibernate merge performs insert instead of update

Question: I have a simple test, where I am trying to update the object, but merge seems to be performing an insert instead of update.
<code>@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/app-context.xml","classpath:spring/testdb-context.xml"})
public class UserJPATest {
@Test
public void testUpdate() throws Exception {
    System.out.println("update");

    User entity = ObjectManager.USER_DAO.findById(3L);
    entity.setUsername("harryUpdate");

    ObjectManager.USER_DAO.update(entity);

    User selEntity = ObjectManager.USER_DAO.findById(3L);
    Assert.assertEquals(entity.getUsername(),selEntity.getUsername());
}
</code>
}
This is my update method
<code>@Override
@Transactional(propagation= Propagation.REQUIRES_NEW)
public T update(T entity) throws Exception {
    try {
        T merged = entityManager.merge(entity);
        return merged;
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception(e);
    }
}
</code>
Update to code
<code>@Override
@Transactional(propagation= Propagation.REQUIRES_NEW)
public T update(T entity) throws Exception {
    try {
        T merged = null;
        BaseEntity baseEntity = null;
        if(entity instanceof BaseEntity){
            baseEntity = (BaseEntity)entity;
            merged = entityManager.find(entityClass, baseEntity.getId());
        }
        merged = entityManager.merge(entity);
        entityManager.flush();
        return merged;
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception(e);
    }
}
</code>

Now I get the following error Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
Comment: How do you determine that it performs insert?
Comment: I don't **really** like the **REQUIRES_NEW** propagation, you might be creating the entity in a TX and then doing a "merge or update" in a different TX, which doesn't know about the original entity.
Comment: I checked the logs and it tries to do a insert
Comment: To Augusto....that makes sense. I changed the code, but now I get Transaction marked as rollbackonly. I have updated the code above
Answer: I had a version column which was not set when seed data was inserted into database. Hence all the problems with update and delete
Comment: i know this is old thread but even today... many developers face same problem....so i am posting, one possible reason for this problem...  http://www.programmingforfuture.com/2011/02/hibernate-merge-may-insert-new-record.html
Comment: Thanks user373201, I had been struggling with this and I came across your suggestion, as soon as I changed the version, it started working as it was suppose to. Thanks.
Comment: version column ,,which one , im also having same issue,i have set all column of database, but it still inserting record instead of updating.help
Comment: more than 10 years after this answer was posted it's still helpful.
Answer: Merge will update the row ONLY IF it founds the id in the database, so if you create a new object and forgot to set its ID to the same ID of the desired row, it will try to insert a new one. In case your database table doesn't have any unique field, probably this insert will be successful as there will be no sql error.
I was able to update the desired row once I set the new object with the ID of that row.


Application of Divergence theorem to a non-smooth surface

Question: Divergence theorem that I learnt in analysis:
Let $U \rightarrow \mathbb R^n$ be an open, bounded, regular set and let $f : \bar{U} \rightarrow \mathbb R^n$ be such that $f$ is bounded and continuous in $U$ and there exist the partial derivatives of $f$ in $\mathbb R^n$ at all $x \in U$ and they are continuous and bounded. Then
$$\int_U \operatorname{Div} f(x) dx = \int_{\partial U} f(x) \cdot \nu d\mathcal H^{n-1},$$
where $\nu$ is normal vector.
If I go back to calc 3, I see many instances where I "exploit" divergence theorem on non-smooth surfaces (such as polygons bounded by the first octant and plane $\{x+y+z = 1\}$). How can I justify divergence theorem on such non-smooth surfaces?
Let $V$ be the non-smooth open set where divergence theorem is applied. Should I take a sequence of regular set $\{U_n\}_{n \in \mathbb N}$ with $U_n \subset V$ such that $\mu(V - U_n) \rightarrow 0$ and take apply divergence theorem on $U_n$. I am assuming the contribution of vector field on $(V - U_n)$ would be small with $\mu(V - U_n) < \epsilon$. However, I am bogged down by the possibility that something strange can happen near the sharp corner of the boundary of $V$. Can anyone justify how I can ignore the "sharp" corner points?
Thanks in advance.
Answer: As Spivak develops the generalized Stokes's theorem in Calculus on Manifolds, integration is really about differential forms on smooth singular chains. That is, we're not integrating over manifolds per se, but instead integrating over formal integer linear combinations of images of cubes or simplices under smooth maps.
The polygonal/polyhedral regions in multivariable calculus fit immedietaly into this framework.